]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Extract i9xx_plane_ctl() and ironlake_plane_ctl()
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
57822dc6 40#include "i915_gem_clflush.h"
db18b6a6 41#include "intel_dsi.h"
e5510fac 42#include "i915_trace.h"
319c1d42 43#include <drm/drm_atomic.h>
c196e1d6 44#include <drm/drm_atomic_helper.h>
760285e7
DH
45#include <drm/drm_dp_helper.h>
46#include <drm/drm_crtc_helper.h>
465c120c
MR
47#include <drm/drm_plane_helper.h>
48#include <drm/drm_rect.h>
c0f372b3 49#include <linux/dma_remapping.h>
fd8e058a 50#include <linux/reservation.h>
79e53945 51
5a21b665
DV
52static bool is_mmio_work(struct intel_flip_work *work)
53{
54 return work->mmio_work.func;
55}
56
465c120c 57/* Primary plane formats for gen <= 3 */
568db4f2 58static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
465c120c 61 DRM_FORMAT_XRGB1555,
67fe7dc5 62 DRM_FORMAT_XRGB8888,
465c120c
MR
63};
64
65/* Primary plane formats for gen >= 4 */
568db4f2 66static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
71 DRM_FORMAT_XRGB2101010,
72 DRM_FORMAT_XBGR2101010,
73};
74
75static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
76 DRM_FORMAT_C8,
77 DRM_FORMAT_RGB565,
78 DRM_FORMAT_XRGB8888,
465c120c 79 DRM_FORMAT_XBGR8888,
67fe7dc5 80 DRM_FORMAT_ARGB8888,
465c120c
MR
81 DRM_FORMAT_ABGR8888,
82 DRM_FORMAT_XRGB2101010,
465c120c 83 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
84 DRM_FORMAT_YUYV,
85 DRM_FORMAT_YVYU,
86 DRM_FORMAT_UYVY,
87 DRM_FORMAT_VYUY,
465c120c
MR
88};
89
3d7d6510
MR
90/* Cursor formats */
91static const uint32_t intel_cursor_formats[] = {
92 DRM_FORMAT_ARGB8888,
93};
94
f1f644dc 95static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 96 struct intel_crtc_state *pipe_config);
18442d08 97static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 98 struct intel_crtc_state *pipe_config);
f1f644dc 99
24dbf51a
CW
100static int intel_framebuffer_init(struct intel_framebuffer *ifb,
101 struct drm_i915_gem_object *obj,
102 struct drm_mode_fb_cmd2 *mode_cmd);
5b18e57c
DV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
DV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
1c74eeaf
NM
118static void intel_crtc_init_scalers(struct intel_crtc *crtc,
119 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 123static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 125
d4906093 126struct intel_limit {
4c5def93
ACO
127 struct {
128 int min, max;
129 } dot, vco, n, m, m1, m2, p, p1;
130
131 struct {
132 int dot_limit;
133 int p2_slow, p2_fast;
134 } p2;
d4906093 135};
79e53945 136
bfa7df01 137/* returns HPLL frequency in kHz */
49cd97a3 138int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
139{
140 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
141
142 /* Obtain SKU information */
143 mutex_lock(&dev_priv->sb_lock);
144 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
145 CCK_FUSE_HPLL_FREQ_MASK;
146 mutex_unlock(&dev_priv->sb_lock);
147
148 return vco_freq[hpll_freq] * 1000;
149}
150
c30fec65
VS
151int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
152 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
153{
154 u32 val;
155 int divider;
156
bfa7df01
VS
157 mutex_lock(&dev_priv->sb_lock);
158 val = vlv_cck_read(dev_priv, reg);
159 mutex_unlock(&dev_priv->sb_lock);
160
161 divider = val & CCK_FREQUENCY_VALUES;
162
163 WARN((val & CCK_FREQUENCY_STATUS) !=
164 (divider << CCK_FREQUENCY_STATUS_SHIFT),
165 "%s change in progress\n", name);
166
c30fec65
VS
167 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
168}
169
7ff89ca2
VS
170int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
171 const char *name, u32 reg)
c30fec65
VS
172{
173 if (dev_priv->hpll_freq == 0)
49cd97a3 174 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
175
176 return vlv_get_cck_clock(dev_priv, name, reg,
177 dev_priv->hpll_freq);
bfa7df01
VS
178}
179
bfa7df01
VS
180static void intel_update_czclk(struct drm_i915_private *dev_priv)
181{
666a4537 182 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
183 return;
184
185 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
186 CCK_CZ_CLOCK_CONTROL);
187
188 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
189}
190
021357ac 191static inline u32 /* units of 100MHz */
21a727b3
VS
192intel_fdi_link_freq(struct drm_i915_private *dev_priv,
193 const struct intel_crtc_state *pipe_config)
021357ac 194{
21a727b3
VS
195 if (HAS_DDI(dev_priv))
196 return pipe_config->port_clock; /* SPLL */
197 else if (IS_GEN5(dev_priv))
198 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 199 else
21a727b3 200 return 270000;
021357ac
CW
201}
202
1b6f4958 203static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 204 .dot = { .min = 25000, .max = 350000 },
9c333719 205 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 206 .n = { .min = 2, .max = 16 },
0206e353
AJ
207 .m = { .min = 96, .max = 140 },
208 .m1 = { .min = 18, .max = 26 },
209 .m2 = { .min = 6, .max = 16 },
210 .p = { .min = 4, .max = 128 },
211 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
212 .p2 = { .dot_limit = 165000,
213 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
214};
215
1b6f4958 216static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 217 .dot = { .min = 25000, .max = 350000 },
9c333719 218 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 219 .n = { .min = 2, .max = 16 },
5d536e28
DV
220 .m = { .min = 96, .max = 140 },
221 .m1 = { .min = 18, .max = 26 },
222 .m2 = { .min = 6, .max = 16 },
223 .p = { .min = 4, .max = 128 },
224 .p1 = { .min = 2, .max = 33 },
225 .p2 = { .dot_limit = 165000,
226 .p2_slow = 4, .p2_fast = 4 },
227};
228
1b6f4958 229static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 230 .dot = { .min = 25000, .max = 350000 },
9c333719 231 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 232 .n = { .min = 2, .max = 16 },
0206e353
AJ
233 .m = { .min = 96, .max = 140 },
234 .m1 = { .min = 18, .max = 26 },
235 .m2 = { .min = 6, .max = 16 },
236 .p = { .min = 4, .max = 128 },
237 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 14, .p2_fast = 7 },
e4b36699 240};
273e27ca 241
1b6f4958 242static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
243 .dot = { .min = 20000, .max = 400000 },
244 .vco = { .min = 1400000, .max = 2800000 },
245 .n = { .min = 1, .max = 6 },
246 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
247 .m1 = { .min = 8, .max = 18 },
248 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
249 .p = { .min = 5, .max = 80 },
250 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
251 .p2 = { .dot_limit = 200000,
252 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
253};
254
1b6f4958 255static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
256 .dot = { .min = 20000, .max = 400000 },
257 .vco = { .min = 1400000, .max = 2800000 },
258 .n = { .min = 1, .max = 6 },
259 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
260 .m1 = { .min = 8, .max = 18 },
261 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
262 .p = { .min = 7, .max = 98 },
263 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
264 .p2 = { .dot_limit = 112000,
265 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
266};
267
273e27ca 268
1b6f4958 269static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
270 .dot = { .min = 25000, .max = 270000 },
271 .vco = { .min = 1750000, .max = 3500000},
272 .n = { .min = 1, .max = 4 },
273 .m = { .min = 104, .max = 138 },
274 .m1 = { .min = 17, .max = 23 },
275 .m2 = { .min = 5, .max = 11 },
276 .p = { .min = 10, .max = 30 },
277 .p1 = { .min = 1, .max = 3},
278 .p2 = { .dot_limit = 270000,
279 .p2_slow = 10,
280 .p2_fast = 10
044c7c41 281 },
e4b36699
KP
282};
283
1b6f4958 284static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
285 .dot = { .min = 22000, .max = 400000 },
286 .vco = { .min = 1750000, .max = 3500000},
287 .n = { .min = 1, .max = 4 },
288 .m = { .min = 104, .max = 138 },
289 .m1 = { .min = 16, .max = 23 },
290 .m2 = { .min = 5, .max = 11 },
291 .p = { .min = 5, .max = 80 },
292 .p1 = { .min = 1, .max = 8},
293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
295};
296
1b6f4958 297static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
298 .dot = { .min = 20000, .max = 115000 },
299 .vco = { .min = 1750000, .max = 3500000 },
300 .n = { .min = 1, .max = 3 },
301 .m = { .min = 104, .max = 138 },
302 .m1 = { .min = 17, .max = 23 },
303 .m2 = { .min = 5, .max = 11 },
304 .p = { .min = 28, .max = 112 },
305 .p1 = { .min = 2, .max = 8 },
306 .p2 = { .dot_limit = 0,
307 .p2_slow = 14, .p2_fast = 14
044c7c41 308 },
e4b36699
KP
309};
310
1b6f4958 311static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
312 .dot = { .min = 80000, .max = 224000 },
313 .vco = { .min = 1750000, .max = 3500000 },
314 .n = { .min = 1, .max = 3 },
315 .m = { .min = 104, .max = 138 },
316 .m1 = { .min = 17, .max = 23 },
317 .m2 = { .min = 5, .max = 11 },
318 .p = { .min = 14, .max = 42 },
319 .p1 = { .min = 2, .max = 6 },
320 .p2 = { .dot_limit = 0,
321 .p2_slow = 7, .p2_fast = 7
044c7c41 322 },
e4b36699
KP
323};
324
1b6f4958 325static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
326 .dot = { .min = 20000, .max = 400000},
327 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 328 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
329 .n = { .min = 3, .max = 6 },
330 .m = { .min = 2, .max = 256 },
273e27ca 331 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
332 .m1 = { .min = 0, .max = 0 },
333 .m2 = { .min = 0, .max = 254 },
334 .p = { .min = 5, .max = 80 },
335 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
336 .p2 = { .dot_limit = 200000,
337 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
338};
339
1b6f4958 340static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
341 .dot = { .min = 20000, .max = 400000 },
342 .vco = { .min = 1700000, .max = 3500000 },
343 .n = { .min = 3, .max = 6 },
344 .m = { .min = 2, .max = 256 },
345 .m1 = { .min = 0, .max = 0 },
346 .m2 = { .min = 0, .max = 254 },
347 .p = { .min = 7, .max = 112 },
348 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
349 .p2 = { .dot_limit = 112000,
350 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
351};
352
273e27ca
EA
353/* Ironlake / Sandybridge
354 *
355 * We calculate clock using (register_value + 2) for N/M1/M2, so here
356 * the range value for them is (actual_value - 2).
357 */
1b6f4958 358static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 5 },
362 .m = { .min = 79, .max = 127 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 5, .max = 80 },
366 .p1 = { .min = 1, .max = 8 },
367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
369};
370
1b6f4958 371static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
372 .dot = { .min = 25000, .max = 350000 },
373 .vco = { .min = 1760000, .max = 3510000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 79, .max = 118 },
376 .m1 = { .min = 12, .max = 22 },
377 .m2 = { .min = 5, .max = 9 },
378 .p = { .min = 28, .max = 112 },
379 .p1 = { .min = 2, .max = 8 },
380 .p2 = { .dot_limit = 225000,
381 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
382};
383
1b6f4958 384static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
385 .dot = { .min = 25000, .max = 350000 },
386 .vco = { .min = 1760000, .max = 3510000 },
387 .n = { .min = 1, .max = 3 },
388 .m = { .min = 79, .max = 127 },
389 .m1 = { .min = 12, .max = 22 },
390 .m2 = { .min = 5, .max = 9 },
391 .p = { .min = 14, .max = 56 },
392 .p1 = { .min = 2, .max = 8 },
393 .p2 = { .dot_limit = 225000,
394 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
395};
396
273e27ca 397/* LVDS 100mhz refclk limits. */
1b6f4958 398static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
399 .dot = { .min = 25000, .max = 350000 },
400 .vco = { .min = 1760000, .max = 3510000 },
401 .n = { .min = 1, .max = 2 },
402 .m = { .min = 79, .max = 126 },
403 .m1 = { .min = 12, .max = 22 },
404 .m2 = { .min = 5, .max = 9 },
405 .p = { .min = 28, .max = 112 },
0206e353 406 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
407 .p2 = { .dot_limit = 225000,
408 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
409};
410
1b6f4958 411static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 3 },
415 .m = { .min = 79, .max = 126 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 14, .max = 42 },
0206e353 419 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
422};
423
1b6f4958 424static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
425 /*
426 * These are the data rate limits (measured in fast clocks)
427 * since those are the strictest limits we have. The fast
428 * clock and actual rate limits are more relaxed, so checking
429 * them would make no difference.
430 */
431 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 432 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 433 .n = { .min = 1, .max = 7 },
a0c4da24
JB
434 .m1 = { .min = 2, .max = 3 },
435 .m2 = { .min = 11, .max = 156 },
b99ab663 436 .p1 = { .min = 2, .max = 3 },
5fdc9c49 437 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
438};
439
1b6f4958 440static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
441 /*
442 * These are the data rate limits (measured in fast clocks)
443 * since those are the strictest limits we have. The fast
444 * clock and actual rate limits are more relaxed, so checking
445 * them would make no difference.
446 */
447 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 448 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
449 .n = { .min = 1, .max = 1 },
450 .m1 = { .min = 2, .max = 2 },
451 .m2 = { .min = 24 << 22, .max = 175 << 22 },
452 .p1 = { .min = 2, .max = 4 },
453 .p2 = { .p2_slow = 1, .p2_fast = 14 },
454};
455
1b6f4958 456static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
457 /* FIXME: find real dot limits */
458 .dot = { .min = 0, .max = INT_MAX },
e6292556 459 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
460 .n = { .min = 1, .max = 1 },
461 .m1 = { .min = 2, .max = 2 },
462 /* FIXME: find real m2 limits */
463 .m2 = { .min = 2 << 22, .max = 255 << 22 },
464 .p1 = { .min = 2, .max = 4 },
465 .p2 = { .p2_slow = 1, .p2_fast = 20 },
466};
467
cdba954e
ACO
468static bool
469needs_modeset(struct drm_crtc_state *state)
470{
fc596660 471 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
472}
473
dccbea3b
ID
474/*
475 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
476 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
477 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
478 * The helpers' return value is the rate of the clock that is fed to the
479 * display engine's pipe which can be the above fast dot clock rate or a
480 * divided-down version of it.
481 */
f2b115e6 482/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 483static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 484{
2177832f
SL
485 clock->m = clock->m2 + 2;
486 clock->p = clock->p1 * clock->p2;
ed5ca77e 487 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 488 return 0;
fb03ac01
VS
489 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
490 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
491
492 return clock->dot;
2177832f
SL
493}
494
7429e9d4
DV
495static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
496{
497 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
498}
499
9e2c8475 500static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 501{
7429e9d4 502 clock->m = i9xx_dpll_compute_m(clock);
79e53945 503 clock->p = clock->p1 * clock->p2;
ed5ca77e 504 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 505 return 0;
fb03ac01
VS
506 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
507 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
508
509 return clock->dot;
79e53945
JB
510}
511
9e2c8475 512static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
513{
514 clock->m = clock->m1 * clock->m2;
515 clock->p = clock->p1 * clock->p2;
516 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 517 return 0;
589eca67
ID
518 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
519 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
520
521 return clock->dot / 5;
589eca67
ID
522}
523
9e2c8475 524int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
525{
526 clock->m = clock->m1 * clock->m2;
527 clock->p = clock->p1 * clock->p2;
528 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 529 return 0;
ef9348c8
CML
530 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
531 clock->n << 22);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
533
534 return clock->dot / 5;
ef9348c8
CML
535}
536
7c04d1d9 537#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
538/**
539 * Returns whether the given set of divisors are valid for a given refclk with
540 * the given connectors.
541 */
542
e2d214ae 543static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 544 const struct intel_limit *limit,
9e2c8475 545 const struct dpll *clock)
79e53945 546{
f01b7962
VS
547 if (clock->n < limit->n.min || limit->n.max < clock->n)
548 INTELPllInvalid("n out of range\n");
79e53945 549 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 550 INTELPllInvalid("p1 out of range\n");
79e53945 551 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 552 INTELPllInvalid("m2 out of range\n");
79e53945 553 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 554 INTELPllInvalid("m1 out of range\n");
f01b7962 555
e2d214ae 556 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 557 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
558 if (clock->m1 <= clock->m2)
559 INTELPllInvalid("m1 <= m2\n");
560
e2d214ae 561 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 562 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
79e53945 569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 570 INTELPllInvalid("vco out of range\n");
79e53945
JB
571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 575 INTELPllInvalid("dot out of range\n");
79e53945
JB
576
577 return true;
578}
579
3b1429d9 580static int
1b6f4958 581i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
582 const struct intel_crtc_state *crtc_state,
583 int target)
79e53945 584{
3b1429d9 585 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 586
2d84d2b3 587 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 588 /*
a210b028
DV
589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
79e53945 592 */
1974cad0 593 if (intel_is_dual_link_lvds(dev))
3b1429d9 594 return limit->p2.p2_fast;
79e53945 595 else
3b1429d9 596 return limit->p2.p2_slow;
79e53945
JB
597 } else {
598 if (target < limit->p2.dot_limit)
3b1429d9 599 return limit->p2.p2_slow;
79e53945 600 else
3b1429d9 601 return limit->p2.p2_fast;
79e53945 602 }
3b1429d9
VS
603}
604
70e8aa21
ACO
605/*
606 * Returns a set of divisors for the desired target clock with the given
607 * refclk, or FALSE. The returned values represent the clock equation:
608 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
609 *
610 * Target and reference clocks are specified in kHz.
611 *
612 * If match_clock is provided, then best_clock P divider must match the P
613 * divider from @match_clock used for LVDS downclocking.
614 */
3b1429d9 615static bool
1b6f4958 616i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 617 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
618 int target, int refclk, struct dpll *match_clock,
619 struct dpll *best_clock)
3b1429d9
VS
620{
621 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 622 struct dpll clock;
3b1429d9 623 int err = target;
79e53945 624
0206e353 625 memset(best_clock, 0, sizeof(*best_clock));
79e53945 626
3b1429d9
VS
627 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
628
42158660
ZY
629 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
630 clock.m1++) {
631 for (clock.m2 = limit->m2.min;
632 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 633 if (clock.m2 >= clock.m1)
42158660
ZY
634 break;
635 for (clock.n = limit->n.min;
636 clock.n <= limit->n.max; clock.n++) {
637 for (clock.p1 = limit->p1.min;
638 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
639 int this_err;
640
dccbea3b 641 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
642 if (!intel_PLL_is_valid(to_i915(dev),
643 limit,
ac58c3f0
DV
644 &clock))
645 continue;
646 if (match_clock &&
647 clock.p != match_clock->p)
648 continue;
649
650 this_err = abs(clock.dot - target);
651 if (this_err < err) {
652 *best_clock = clock;
653 err = this_err;
654 }
655 }
656 }
657 }
658 }
659
660 return (err != target);
661}
662
70e8aa21
ACO
663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
ac58c3f0 673static bool
1b6f4958 674pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 675 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
79e53945 678{
3b1429d9 679 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 680 struct dpll clock;
79e53945
JB
681 int err = target;
682
0206e353 683 memset(best_clock, 0, sizeof(*best_clock));
79e53945 684
3b1429d9
VS
685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
42158660
ZY
687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
691 for (clock.n = limit->n.min;
692 clock.n <= limit->n.max; clock.n++) {
693 for (clock.p1 = limit->p1.min;
694 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
695 int this_err;
696
dccbea3b 697 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
698 if (!intel_PLL_is_valid(to_i915(dev),
699 limit,
1b894b59 700 &clock))
79e53945 701 continue;
cec2f356
SP
702 if (match_clock &&
703 clock.p != match_clock->p)
704 continue;
79e53945
JB
705
706 this_err = abs(clock.dot - target);
707 if (this_err < err) {
708 *best_clock = clock;
709 err = this_err;
710 }
711 }
712 }
713 }
714 }
715
716 return (err != target);
717}
718
997c030c
ACO
719/*
720 * Returns a set of divisors for the desired target clock with the given
721 * refclk, or FALSE. The returned values represent the clock equation:
722 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
723 *
724 * Target and reference clocks are specified in kHz.
725 *
726 * If match_clock is provided, then best_clock P divider must match the P
727 * divider from @match_clock used for LVDS downclocking.
997c030c 728 */
d4906093 729static bool
1b6f4958 730g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 731 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
732 int target, int refclk, struct dpll *match_clock,
733 struct dpll *best_clock)
d4906093 734{
3b1429d9 735 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 736 struct dpll clock;
d4906093 737 int max_n;
3b1429d9 738 bool found = false;
6ba770dc
AJ
739 /* approximately equals target * 0.00585 */
740 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
741
742 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
743
744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
d4906093 746 max_n = limit->n.max;
f77f13e2 747 /* based on hardware requirement, prefer smaller n to precision */
d4906093 748 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 749 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
750 for (clock.m1 = limit->m1.max;
751 clock.m1 >= limit->m1.min; clock.m1--) {
752 for (clock.m2 = limit->m2.max;
753 clock.m2 >= limit->m2.min; clock.m2--) {
754 for (clock.p1 = limit->p1.max;
755 clock.p1 >= limit->p1.min; clock.p1--) {
756 int this_err;
757
dccbea3b 758 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
1b894b59 761 &clock))
d4906093 762 continue;
1b894b59
CW
763
764 this_err = abs(clock.dot - target);
d4906093
ML
765 if (this_err < err_most) {
766 *best_clock = clock;
767 err_most = this_err;
768 max_n = clock.n;
769 found = true;
770 }
771 }
772 }
773 }
774 }
2c07245f
ZW
775 return found;
776}
777
d5dd62bd
ID
778/*
779 * Check if the calculated PLL configuration is more optimal compared to the
780 * best configuration and error found so far. Return the calculated error.
781 */
782static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
783 const struct dpll *calculated_clock,
784 const struct dpll *best_clock,
d5dd62bd
ID
785 unsigned int best_error_ppm,
786 unsigned int *error_ppm)
787{
9ca3ba01
ID
788 /*
789 * For CHV ignore the error and consider only the P value.
790 * Prefer a bigger P value based on HW requirements.
791 */
920a14b2 792 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
793 *error_ppm = 0;
794
795 return calculated_clock->p > best_clock->p;
796 }
797
24be4e46
ID
798 if (WARN_ON_ONCE(!target_freq))
799 return false;
800
d5dd62bd
ID
801 *error_ppm = div_u64(1000000ULL *
802 abs(target_freq - calculated_clock->dot),
803 target_freq);
804 /*
805 * Prefer a better P value over a better (smaller) error if the error
806 * is small. Ensure this preference for future configurations too by
807 * setting the error to 0.
808 */
809 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
810 *error_ppm = 0;
811
812 return true;
813 }
814
815 return *error_ppm + 10 < best_error_ppm;
816}
817
65b3d6a9
ACO
818/*
819 * Returns a set of divisors for the desired target clock with the given
820 * refclk, or FALSE. The returned values represent the clock equation:
821 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
822 */
a0c4da24 823static bool
1b6f4958 824vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 825 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
826 int target, int refclk, struct dpll *match_clock,
827 struct dpll *best_clock)
a0c4da24 828{
a93e255f 829 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 830 struct drm_device *dev = crtc->base.dev;
9e2c8475 831 struct dpll clock;
69e4f900 832 unsigned int bestppm = 1000000;
27e639bf
VS
833 /* min update 19.2 MHz */
834 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 835 bool found = false;
a0c4da24 836
6b4bf1c4
VS
837 target *= 5; /* fast clock */
838
839 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
840
841 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 842 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 843 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 844 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 845 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 846 clock.p = clock.p1 * clock.p2;
a0c4da24 847 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 848 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 849 unsigned int ppm;
69e4f900 850
6b4bf1c4
VS
851 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
852 refclk * clock.m1);
853
dccbea3b 854 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 855
e2d214ae
TU
856 if (!intel_PLL_is_valid(to_i915(dev),
857 limit,
f01b7962 858 &clock))
43b0ac53
VS
859 continue;
860
d5dd62bd
ID
861 if (!vlv_PLL_is_optimal(dev, target,
862 &clock,
863 best_clock,
864 bestppm, &ppm))
865 continue;
6b4bf1c4 866
d5dd62bd
ID
867 *best_clock = clock;
868 bestppm = ppm;
869 found = true;
a0c4da24
JB
870 }
871 }
872 }
873 }
a0c4da24 874
49e497ef 875 return found;
a0c4da24 876}
a4fc5ed6 877
65b3d6a9
ACO
878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
ef9348c8 883static bool
1b6f4958 884chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 885 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
ef9348c8 888{
a93e255f 889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 890 struct drm_device *dev = crtc->base.dev;
9ca3ba01 891 unsigned int best_error_ppm;
9e2c8475 892 struct dpll clock;
ef9348c8
CML
893 uint64_t m2;
894 int found = false;
895
896 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 897 best_error_ppm = 1000000;
ef9348c8
CML
898
899 /*
900 * Based on hardware doc, the n always set to 1, and m1 always
901 * set to 2. If requires to support 200Mhz refclk, we need to
902 * revisit this because n may not 1 anymore.
903 */
904 clock.n = 1, clock.m1 = 2;
905 target *= 5; /* fast clock */
906
907 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
908 for (clock.p2 = limit->p2.p2_fast;
909 clock.p2 >= limit->p2.p2_slow;
910 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 911 unsigned int error_ppm;
ef9348c8
CML
912
913 clock.p = clock.p1 * clock.p2;
914
915 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
916 clock.n) << 22, refclk * clock.m1);
917
918 if (m2 > INT_MAX/clock.m1)
919 continue;
920
921 clock.m2 = m2;
922
dccbea3b 923 chv_calc_dpll_params(refclk, &clock);
ef9348c8 924
e2d214ae 925 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
926 continue;
927
9ca3ba01
ID
928 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
929 best_error_ppm, &error_ppm))
930 continue;
931
932 *best_clock = clock;
933 best_error_ppm = error_ppm;
934 found = true;
ef9348c8
CML
935 }
936 }
937
938 return found;
939}
940
5ab7b0b7 941bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 942 struct dpll *best_clock)
5ab7b0b7 943{
65b3d6a9 944 int refclk = 100000;
1b6f4958 945 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 946
65b3d6a9 947 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
948 target_clock, refclk, NULL, best_clock);
949}
950
525b9311 951bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 952{
20ddf665
VS
953 /* Be paranoid as we can arrive here with only partial
954 * state retrieved from the hardware during setup.
955 *
241bfc38 956 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
957 * as Haswell has gained clock readout/fastboot support.
958 *
66e514c1 959 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 960 * properly reconstruct framebuffers.
c3d1f436
MR
961 *
962 * FIXME: The intel_crtc->active here should be switched to
963 * crtc->state->active once we have proper CRTC states wired up
964 * for atomic.
20ddf665 965 */
525b9311
VS
966 return crtc->active && crtc->base.primary->state->fb &&
967 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
968}
969
a5c961d1
PZ
970enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
971 enum pipe pipe)
972{
98187836 973 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 974
e2af48c6 975 return crtc->config->cpu_transcoder;
a5c961d1
PZ
976}
977
6315b5d3 978static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 979{
f0f59a00 980 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
981 u32 line1, line2;
982 u32 line_mask;
983
5db94019 984 if (IS_GEN2(dev_priv))
fbf49ea2
VS
985 line_mask = DSL_LINEMASK_GEN2;
986 else
987 line_mask = DSL_LINEMASK_GEN3;
988
989 line1 = I915_READ(reg) & line_mask;
6adfb1ef 990 msleep(5);
fbf49ea2
VS
991 line2 = I915_READ(reg) & line_mask;
992
993 return line1 == line2;
994}
995
ab7ad7f6
KP
996/*
997 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 998 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
999 *
1000 * After disabling a pipe, we can't wait for vblank in the usual way,
1001 * spinning on the vblank interrupt status bit, since we won't actually
1002 * see an interrupt when the pipe is disabled.
1003 *
ab7ad7f6
KP
1004 * On Gen4 and above:
1005 * wait for the pipe register state bit to turn off
1006 *
1007 * Otherwise:
1008 * wait for the display line value to settle (it usually
1009 * ends up stopping at the start of the next frame).
58e10eb9 1010 *
9d0498a2 1011 */
575f7ab7 1012static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1013{
6315b5d3 1014 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1015 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1016 enum pipe pipe = crtc->pipe;
ab7ad7f6 1017
6315b5d3 1018 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1019 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1020
1021 /* Wait for the Pipe State to go off */
b8511f53
CW
1022 if (intel_wait_for_register(dev_priv,
1023 reg, I965_PIPECONF_ACTIVE, 0,
1024 100))
284637d9 1025 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1026 } else {
ab7ad7f6 1027 /* Wait for the display line to settle */
6315b5d3 1028 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1029 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1030 }
79e53945
JB
1031}
1032
b24e7179 1033/* Only for pre-ILK configs */
55607e8a
DV
1034void assert_pll(struct drm_i915_private *dev_priv,
1035 enum pipe pipe, bool state)
b24e7179 1036{
b24e7179
JB
1037 u32 val;
1038 bool cur_state;
1039
649636ef 1040 val = I915_READ(DPLL(pipe));
b24e7179 1041 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1042 I915_STATE_WARN(cur_state != state,
b24e7179 1043 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1044 onoff(state), onoff(cur_state));
b24e7179 1045}
b24e7179 1046
23538ef1 1047/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1048void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1049{
1050 u32 val;
1051 bool cur_state;
1052
a580516d 1053 mutex_lock(&dev_priv->sb_lock);
23538ef1 1054 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1055 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1056
1057 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1058 I915_STATE_WARN(cur_state != state,
23538ef1 1059 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1060 onoff(state), onoff(cur_state));
23538ef1 1061}
23538ef1 1062
040484af
JB
1063static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1064 enum pipe pipe, bool state)
1065{
040484af 1066 bool cur_state;
ad80a810
PZ
1067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
040484af 1069
2d1fe073 1070 if (HAS_DDI(dev_priv)) {
affa9354 1071 /* DDI does not have a specific FDI_TX register */
649636ef 1072 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1073 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1074 } else {
649636ef 1075 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1076 cur_state = !!(val & FDI_TX_ENABLE);
1077 }
e2c719b7 1078 I915_STATE_WARN(cur_state != state,
040484af 1079 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1080 onoff(state), onoff(cur_state));
040484af
JB
1081}
1082#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1083#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1084
1085static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1086 enum pipe pipe, bool state)
1087{
040484af
JB
1088 u32 val;
1089 bool cur_state;
1090
649636ef 1091 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1092 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1093 I915_STATE_WARN(cur_state != state,
040484af 1094 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1095 onoff(state), onoff(cur_state));
040484af
JB
1096}
1097#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1098#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1099
1100static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
040484af
JB
1103 u32 val;
1104
1105 /* ILK FDI PLL is always enabled */
7e22dbbb 1106 if (IS_GEN5(dev_priv))
040484af
JB
1107 return;
1108
bf507ef7 1109 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1110 if (HAS_DDI(dev_priv))
bf507ef7
ED
1111 return;
1112
649636ef 1113 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1114 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1115}
1116
55607e8a
DV
1117void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
040484af 1119{
040484af 1120 u32 val;
55607e8a 1121 bool cur_state;
040484af 1122
649636ef 1123 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1124 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1125 I915_STATE_WARN(cur_state != state,
55607e8a 1126 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1127 onoff(state), onoff(cur_state));
040484af
JB
1128}
1129
4f8036a2 1130void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1131{
f0f59a00 1132 i915_reg_t pp_reg;
ea0760cf
JB
1133 u32 val;
1134 enum pipe panel_pipe = PIPE_A;
0de3b485 1135 bool locked = true;
ea0760cf 1136
4f8036a2 1137 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1138 return;
1139
4f8036a2 1140 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1141 u32 port_sel;
1142
44cb734c
ID
1143 pp_reg = PP_CONTROL(0);
1144 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1145
1146 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1147 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1148 panel_pipe = PIPE_B;
1149 /* XXX: else fix for eDP */
4f8036a2 1150 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1151 /* presumably write lock depends on pipe, not port select */
44cb734c 1152 pp_reg = PP_CONTROL(pipe);
bedd4dba 1153 panel_pipe = pipe;
ea0760cf 1154 } else {
44cb734c 1155 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1156 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1157 panel_pipe = PIPE_B;
ea0760cf
JB
1158 }
1159
1160 val = I915_READ(pp_reg);
1161 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1162 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1163 locked = false;
1164
e2c719b7 1165 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1166 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1167 pipe_name(pipe));
ea0760cf
JB
1168}
1169
93ce0ba6
JN
1170static void assert_cursor(struct drm_i915_private *dev_priv,
1171 enum pipe pipe, bool state)
1172{
93ce0ba6
JN
1173 bool cur_state;
1174
2a307c2e 1175 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1176 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1177 else
5efb3e28 1178 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1179
e2c719b7 1180 I915_STATE_WARN(cur_state != state,
93ce0ba6 1181 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1182 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1183}
1184#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1185#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1186
b840d907
JB
1187void assert_pipe(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
b24e7179 1189{
63d7bbe9 1190 bool cur_state;
702e7a56
PZ
1191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
4feed0eb 1193 enum intel_display_power_domain power_domain;
b24e7179 1194
b6b5d049
VS
1195 /* if we need the pipe quirk it must be always on */
1196 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1197 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1198 state = true;
1199
4feed0eb
ID
1200 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1201 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1202 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1203 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1204
1205 intel_display_power_put(dev_priv, power_domain);
1206 } else {
1207 cur_state = false;
69310161
PZ
1208 }
1209
e2c719b7 1210 I915_STATE_WARN(cur_state != state,
63d7bbe9 1211 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1212 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1213}
1214
931872fc
CW
1215static void assert_plane(struct drm_i915_private *dev_priv,
1216 enum plane plane, bool state)
b24e7179 1217{
b24e7179 1218 u32 val;
931872fc 1219 bool cur_state;
b24e7179 1220
649636ef 1221 val = I915_READ(DSPCNTR(plane));
931872fc 1222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1223 I915_STATE_WARN(cur_state != state,
931872fc 1224 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1225 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1226}
1227
931872fc
CW
1228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
b24e7179
JB
1231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
649636ef 1234 int i;
b24e7179 1235
653e1026 1236 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1237 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1238 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1239 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1240 "plane %c assertion failure, should be disabled but not\n",
1241 plane_name(pipe));
19ec1358 1242 return;
28c05794 1243 }
19ec1358 1244
b24e7179 1245 /* Need to check both planes against the pipe */
055e393f 1246 for_each_pipe(dev_priv, i) {
649636ef
VS
1247 u32 val = I915_READ(DSPCNTR(i));
1248 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1249 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1250 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1251 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1252 plane_name(i), pipe_name(pipe));
b24e7179
JB
1253 }
1254}
1255
19332d7a
JB
1256static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
1258{
649636ef 1259 int sprite;
19332d7a 1260
6315b5d3 1261 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1262 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1263 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1264 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1265 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1266 sprite, pipe_name(pipe));
1267 }
920a14b2 1268 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1269 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1270 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1271 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1272 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1273 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1274 }
6315b5d3 1275 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1276 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1277 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1278 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1279 plane_name(pipe), pipe_name(pipe));
6315b5d3 1280 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1281 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1282 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1284 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1285 }
1286}
1287
08c71e5e
VS
1288static void assert_vblank_disabled(struct drm_crtc *crtc)
1289{
e2c719b7 1290 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1291 drm_crtc_vblank_put(crtc);
1292}
1293
7abd4b35
ACO
1294void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
92f2584a 1296{
92f2584a
JB
1297 u32 val;
1298 bool enabled;
1299
649636ef 1300 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1301 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1302 I915_STATE_WARN(enabled,
9db4a9c7
JB
1303 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1304 pipe_name(pipe));
92f2584a
JB
1305}
1306
4e634389
KP
1307static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1308 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1309{
1310 if ((val & DP_PORT_EN) == 0)
1311 return false;
1312
2d1fe073 1313 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1314 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1315 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1316 return false;
2d1fe073 1317 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1318 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1319 return false;
f0575e92
KP
1320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
1519b995
KP
1327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
dc0fa718 1330 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1331 return false;
1332
2d1fe073 1333 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1334 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1335 return false;
2d1fe073 1336 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1337 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1338 return false;
1519b995 1339 } else {
dc0fa718 1340 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1341 return false;
1342 }
1343 return true;
1344}
1345
1346static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1347 enum pipe pipe, u32 val)
1348{
1349 if ((val & LVDS_PORT_EN) == 0)
1350 return false;
1351
2d1fe073 1352 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1353 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1354 return false;
1355 } else {
1356 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1357 return false;
1358 }
1359 return true;
1360}
1361
1362static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
1365 if ((val & ADPA_DAC_ENABLE) == 0)
1366 return false;
2d1fe073 1367 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1368 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1369 return false;
1370 } else {
1371 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1372 return false;
1373 }
1374 return true;
1375}
1376
291906f1 1377static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1378 enum pipe pipe, i915_reg_t reg,
1379 u32 port_sel)
291906f1 1380{
47a05eca 1381 u32 val = I915_READ(reg);
e2c719b7 1382 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1383 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1384 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1385
2d1fe073 1386 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1387 && (val & DP_PIPEB_SELECT),
de9a35ab 1388 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1389}
1390
1391static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1392 enum pipe pipe, i915_reg_t reg)
291906f1 1393{
47a05eca 1394 u32 val = I915_READ(reg);
e2c719b7 1395 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1396 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1397 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1398
2d1fe073 1399 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1400 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1401 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1402}
1403
1404static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1405 enum pipe pipe)
1406{
291906f1 1407 u32 val;
291906f1 1408
f0575e92
KP
1409 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1410 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1411 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1412
649636ef 1413 val = I915_READ(PCH_ADPA);
e2c719b7 1414 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1415 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1416 pipe_name(pipe));
291906f1 1417
649636ef 1418 val = I915_READ(PCH_LVDS);
e2c719b7 1419 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1420 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1421 pipe_name(pipe));
291906f1 1422
e2debe91
PZ
1423 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1425 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1426}
1427
cd2d34d9
VS
1428static void _vlv_enable_pll(struct intel_crtc *crtc,
1429 const struct intel_crtc_state *pipe_config)
1430{
1431 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1432 enum pipe pipe = crtc->pipe;
1433
1434 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1435 POSTING_READ(DPLL(pipe));
1436 udelay(150);
1437
2c30b43b
CW
1438 if (intel_wait_for_register(dev_priv,
1439 DPLL(pipe),
1440 DPLL_LOCK_VLV,
1441 DPLL_LOCK_VLV,
1442 1))
cd2d34d9
VS
1443 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1444}
1445
d288f65f 1446static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1447 const struct intel_crtc_state *pipe_config)
87442f73 1448{
cd2d34d9 1449 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1450 enum pipe pipe = crtc->pipe;
87442f73 1451
8bd3f301 1452 assert_pipe_disabled(dev_priv, pipe);
87442f73 1453
87442f73 1454 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1455 assert_panel_unlocked(dev_priv, pipe);
87442f73 1456
cd2d34d9
VS
1457 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1458 _vlv_enable_pll(crtc, pipe_config);
426115cf 1459
8bd3f301
VS
1460 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1461 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1462}
1463
cd2d34d9
VS
1464
1465static void _chv_enable_pll(struct intel_crtc *crtc,
1466 const struct intel_crtc_state *pipe_config)
9d556c99 1467{
cd2d34d9 1468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1469 enum pipe pipe = crtc->pipe;
9d556c99 1470 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1471 u32 tmp;
1472
a580516d 1473 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1474
1475 /* Enable back the 10bit clock to display controller */
1476 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1477 tmp |= DPIO_DCLKP_EN;
1478 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1479
54433e91
VS
1480 mutex_unlock(&dev_priv->sb_lock);
1481
9d556c99
CML
1482 /*
1483 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1484 */
1485 udelay(1);
1486
1487 /* Enable PLL */
d288f65f 1488 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1489
1490 /* Check PLL is locked */
6b18826a
CW
1491 if (intel_wait_for_register(dev_priv,
1492 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1493 1))
9d556c99 1494 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1495}
1496
1497static void chv_enable_pll(struct intel_crtc *crtc,
1498 const struct intel_crtc_state *pipe_config)
1499{
1500 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1501 enum pipe pipe = crtc->pipe;
1502
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 /* PLL is protected by panel, make sure we can write it */
1506 assert_panel_unlocked(dev_priv, pipe);
1507
1508 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1509 _chv_enable_pll(crtc, pipe_config);
9d556c99 1510
c231775c
VS
1511 if (pipe != PIPE_A) {
1512 /*
1513 * WaPixelRepeatModeFixForC0:chv
1514 *
1515 * DPLLCMD is AWOL. Use chicken bits to propagate
1516 * the value from DPLLBMD to either pipe B or C.
1517 */
1518 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1519 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1520 I915_WRITE(CBR4_VLV, 0);
1521 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1522
1523 /*
1524 * DPLLB VGA mode also seems to cause problems.
1525 * We should always have it disabled.
1526 */
1527 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1528 } else {
1529 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1530 POSTING_READ(DPLL_MD(pipe));
1531 }
9d556c99
CML
1532}
1533
6315b5d3 1534static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1535{
1536 struct intel_crtc *crtc;
1537 int count = 0;
1538
6315b5d3 1539 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1540 count += crtc->base.state->active &&
2d84d2b3
VS
1541 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1542 }
1c4e0274
VS
1543
1544 return count;
1545}
1546
66e3d5c0 1547static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1548{
6315b5d3 1549 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1550 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1551 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1552
66e3d5c0 1553 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1554
63d7bbe9 1555 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1556 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1557 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1558
1c4e0274 1559 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1560 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1561 /*
1562 * It appears to be important that we don't enable this
1563 * for the current pipe before otherwise configuring the
1564 * PLL. No idea how this should be handled if multiple
1565 * DVO outputs are enabled simultaneosly.
1566 */
1567 dpll |= DPLL_DVO_2X_MODE;
1568 I915_WRITE(DPLL(!crtc->pipe),
1569 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1570 }
66e3d5c0 1571
c2b63374
VS
1572 /*
1573 * Apparently we need to have VGA mode enabled prior to changing
1574 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1575 * dividers, even though the register value does change.
1576 */
1577 I915_WRITE(reg, 0);
1578
8e7a65aa
VS
1579 I915_WRITE(reg, dpll);
1580
66e3d5c0
DV
1581 /* Wait for the clocks to stabilize. */
1582 POSTING_READ(reg);
1583 udelay(150);
1584
6315b5d3 1585 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1586 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1587 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1588 } else {
1589 /* The pixel multiplier can only be updated once the
1590 * DPLL is enabled and the clocks are stable.
1591 *
1592 * So write it again.
1593 */
1594 I915_WRITE(reg, dpll);
1595 }
63d7bbe9
JB
1596
1597 /* We do this three times for luck */
66e3d5c0 1598 I915_WRITE(reg, dpll);
63d7bbe9
JB
1599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
66e3d5c0 1601 I915_WRITE(reg, dpll);
63d7bbe9
JB
1602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
66e3d5c0 1604 I915_WRITE(reg, dpll);
63d7bbe9
JB
1605 POSTING_READ(reg);
1606 udelay(150); /* wait for warmup */
1607}
1608
1609/**
50b44a44 1610 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1611 * @dev_priv: i915 private structure
1612 * @pipe: pipe PLL to disable
1613 *
1614 * Disable the PLL for @pipe, making sure the pipe is off first.
1615 *
1616 * Note! This is for pre-ILK only.
1617 */
1c4e0274 1618static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1619{
6315b5d3 1620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1621 enum pipe pipe = crtc->pipe;
1622
1623 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1624 if (IS_I830(dev_priv) &&
2d84d2b3 1625 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1626 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1627 I915_WRITE(DPLL(PIPE_B),
1628 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1629 I915_WRITE(DPLL(PIPE_A),
1630 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1631 }
1632
b6b5d049
VS
1633 /* Don't disable pipe or pipe PLLs if needed */
1634 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1635 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1636 return;
1637
1638 /* Make sure the pipe isn't still relying on us */
1639 assert_pipe_disabled(dev_priv, pipe);
1640
b8afb911 1641 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1642 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1643}
1644
f6071166
JB
1645static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1646{
b8afb911 1647 u32 val;
f6071166
JB
1648
1649 /* Make sure the pipe isn't still relying on us */
1650 assert_pipe_disabled(dev_priv, pipe);
1651
03ed5cbf
VS
1652 val = DPLL_INTEGRATED_REF_CLK_VLV |
1653 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1654 if (pipe != PIPE_A)
1655 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1656
f6071166
JB
1657 I915_WRITE(DPLL(pipe), val);
1658 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1659}
1660
1661static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1662{
d752048d 1663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1664 u32 val;
1665
a11b0703
VS
1666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1668
60bfe44f
VS
1669 val = DPLL_SSC_REF_CLK_CHV |
1670 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1671 if (pipe != PIPE_A)
1672 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1673
a11b0703
VS
1674 I915_WRITE(DPLL(pipe), val);
1675 POSTING_READ(DPLL(pipe));
d752048d 1676
a580516d 1677 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1678
1679 /* Disable 10bit clock to display controller */
1680 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1681 val &= ~DPIO_DCLKP_EN;
1682 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1683
a580516d 1684 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1685}
1686
e4607fcf 1687void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1688 struct intel_digital_port *dport,
1689 unsigned int expected_mask)
89b667f8
JB
1690{
1691 u32 port_mask;
f0f59a00 1692 i915_reg_t dpll_reg;
89b667f8 1693
e4607fcf
CML
1694 switch (dport->port) {
1695 case PORT_B:
89b667f8 1696 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1697 dpll_reg = DPLL(0);
e4607fcf
CML
1698 break;
1699 case PORT_C:
89b667f8 1700 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1701 dpll_reg = DPLL(0);
9b6de0a1 1702 expected_mask <<= 4;
00fc31b7
CML
1703 break;
1704 case PORT_D:
1705 port_mask = DPLL_PORTD_READY_MASK;
1706 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1707 break;
1708 default:
1709 BUG();
1710 }
89b667f8 1711
370004d3
CW
1712 if (intel_wait_for_register(dev_priv,
1713 dpll_reg, port_mask, expected_mask,
1714 1000))
9b6de0a1
VS
1715 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1716 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1717}
1718
b8a4f404
PZ
1719static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1720 enum pipe pipe)
040484af 1721{
98187836
VS
1722 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1723 pipe);
f0f59a00
VS
1724 i915_reg_t reg;
1725 uint32_t val, pipeconf_val;
040484af 1726
040484af 1727 /* Make sure PCH DPLL is enabled */
8106ddbd 1728 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1729
1730 /* FDI must be feeding us bits for PCH ports */
1731 assert_fdi_tx_enabled(dev_priv, pipe);
1732 assert_fdi_rx_enabled(dev_priv, pipe);
1733
6e266956 1734 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1735 /* Workaround: Set the timing override bit before enabling the
1736 * pch transcoder. */
1737 reg = TRANS_CHICKEN2(pipe);
1738 val = I915_READ(reg);
1739 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1740 I915_WRITE(reg, val);
59c859d6 1741 }
23670b32 1742
ab9412ba 1743 reg = PCH_TRANSCONF(pipe);
040484af 1744 val = I915_READ(reg);
5f7f726d 1745 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1746
2d1fe073 1747 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1748 /*
c5de7c6f
VS
1749 * Make the BPC in transcoder be consistent with
1750 * that in pipeconf reg. For HDMI we must use 8bpc
1751 * here for both 8bpc and 12bpc.
e9bcff5c 1752 */
dfd07d72 1753 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1754 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1755 val |= PIPECONF_8BPC;
1756 else
1757 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1758 }
5f7f726d
PZ
1759
1760 val &= ~TRANS_INTERLACE_MASK;
1761 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1762 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1763 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1764 val |= TRANS_LEGACY_INTERLACED_ILK;
1765 else
1766 val |= TRANS_INTERLACED;
5f7f726d
PZ
1767 else
1768 val |= TRANS_PROGRESSIVE;
1769
040484af 1770 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1771 if (intel_wait_for_register(dev_priv,
1772 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1773 100))
4bb6f1f3 1774 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1775}
1776
8fb033d7 1777static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1778 enum transcoder cpu_transcoder)
040484af 1779{
8fb033d7 1780 u32 val, pipeconf_val;
8fb033d7 1781
8fb033d7 1782 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1783 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1784 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1785
223a6fdf 1786 /* Workaround: set timing override bit. */
36c0d0cf 1787 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1788 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1789 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1790
25f3ef11 1791 val = TRANS_ENABLE;
937bb610 1792 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1793
9a76b1c6
PZ
1794 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1795 PIPECONF_INTERLACED_ILK)
a35f2679 1796 val |= TRANS_INTERLACED;
8fb033d7
PZ
1797 else
1798 val |= TRANS_PROGRESSIVE;
1799
ab9412ba 1800 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1801 if (intel_wait_for_register(dev_priv,
1802 LPT_TRANSCONF,
1803 TRANS_STATE_ENABLE,
1804 TRANS_STATE_ENABLE,
1805 100))
937bb610 1806 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1807}
1808
b8a4f404
PZ
1809static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1810 enum pipe pipe)
040484af 1811{
f0f59a00
VS
1812 i915_reg_t reg;
1813 uint32_t val;
040484af
JB
1814
1815 /* FDI relies on the transcoder */
1816 assert_fdi_tx_disabled(dev_priv, pipe);
1817 assert_fdi_rx_disabled(dev_priv, pipe);
1818
291906f1
JB
1819 /* Ports must be off as well */
1820 assert_pch_ports_disabled(dev_priv, pipe);
1821
ab9412ba 1822 reg = PCH_TRANSCONF(pipe);
040484af
JB
1823 val = I915_READ(reg);
1824 val &= ~TRANS_ENABLE;
1825 I915_WRITE(reg, val);
1826 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1827 if (intel_wait_for_register(dev_priv,
1828 reg, TRANS_STATE_ENABLE, 0,
1829 50))
4bb6f1f3 1830 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1831
6e266956 1832 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1833 /* Workaround: Clear the timing override chicken bit again. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
1838 }
040484af
JB
1839}
1840
b7076546 1841void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1842{
8fb033d7
PZ
1843 u32 val;
1844
ab9412ba 1845 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1846 val &= ~TRANS_ENABLE;
ab9412ba 1847 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1848 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1849 if (intel_wait_for_register(dev_priv,
1850 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1851 50))
8a52fd9f 1852 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1853
1854 /* Workaround: clear timing override bit. */
36c0d0cf 1855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1856 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1858}
1859
65f2130c
VS
1860enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1861{
1862 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1863
1864 WARN_ON(!crtc->config->has_pch_encoder);
1865
1866 if (HAS_PCH_LPT(dev_priv))
1867 return TRANSCODER_A;
1868 else
1869 return (enum transcoder) crtc->pipe;
1870}
1871
b24e7179 1872/**
309cfea8 1873 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1874 * @crtc: crtc responsible for the pipe
b24e7179 1875 *
0372264a 1876 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1877 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1878 */
e1fdc473 1879static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1880{
0372264a 1881 struct drm_device *dev = crtc->base.dev;
fac5e23e 1882 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1883 enum pipe pipe = crtc->pipe;
1a70a728 1884 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1885 i915_reg_t reg;
b24e7179
JB
1886 u32 val;
1887
9e2ee2dd
VS
1888 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1889
58c6eaa2 1890 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1891 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1892 assert_sprites_disabled(dev_priv, pipe);
1893
b24e7179
JB
1894 /*
1895 * A pipe without a PLL won't actually be able to drive bits from
1896 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1897 * need the check.
1898 */
09fa8bb9 1899 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1900 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1901 assert_dsi_pll_enabled(dev_priv);
1902 else
1903 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1904 } else {
6e3c9717 1905 if (crtc->config->has_pch_encoder) {
040484af 1906 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1907 assert_fdi_rx_pll_enabled(dev_priv,
1908 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1909 assert_fdi_tx_pll_enabled(dev_priv,
1910 (enum pipe) cpu_transcoder);
040484af
JB
1911 }
1912 /* FIXME: assert CPU port conditions for SNB+ */
1913 }
b24e7179 1914
702e7a56 1915 reg = PIPECONF(cpu_transcoder);
b24e7179 1916 val = I915_READ(reg);
7ad25d48 1917 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1918 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1919 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1920 return;
7ad25d48 1921 }
00d70b15
CW
1922
1923 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1924 POSTING_READ(reg);
b7792d8b
VS
1925
1926 /*
1927 * Until the pipe starts DSL will read as 0, which would cause
1928 * an apparent vblank timestamp jump, which messes up also the
1929 * frame count when it's derived from the timestamps. So let's
1930 * wait for the pipe to start properly before we call
1931 * drm_crtc_vblank_on()
1932 */
1933 if (dev->max_vblank_count == 0 &&
1934 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1935 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1936}
1937
1938/**
309cfea8 1939 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1940 * @crtc: crtc whose pipes is to be disabled
b24e7179 1941 *
575f7ab7
VS
1942 * Disable the pipe of @crtc, making sure that various hardware
1943 * specific requirements are met, if applicable, e.g. plane
1944 * disabled, panel fitter off, etc.
b24e7179
JB
1945 *
1946 * Will wait until the pipe has shut down before returning.
1947 */
575f7ab7 1948static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1949{
fac5e23e 1950 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1951 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1952 enum pipe pipe = crtc->pipe;
f0f59a00 1953 i915_reg_t reg;
b24e7179
JB
1954 u32 val;
1955
9e2ee2dd
VS
1956 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1957
b24e7179
JB
1958 /*
1959 * Make sure planes won't keep trying to pump pixels to us,
1960 * or we might hang the display.
1961 */
1962 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1963 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1964 assert_sprites_disabled(dev_priv, pipe);
b24e7179 1965
702e7a56 1966 reg = PIPECONF(cpu_transcoder);
b24e7179 1967 val = I915_READ(reg);
00d70b15
CW
1968 if ((val & PIPECONF_ENABLE) == 0)
1969 return;
1970
67adc644
VS
1971 /*
1972 * Double wide has implications for planes
1973 * so best keep it disabled when not needed.
1974 */
6e3c9717 1975 if (crtc->config->double_wide)
67adc644
VS
1976 val &= ~PIPECONF_DOUBLE_WIDE;
1977
1978 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
1979 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
1980 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
1981 val &= ~PIPECONF_ENABLE;
1982
1983 I915_WRITE(reg, val);
1984 if ((val & PIPECONF_ENABLE) == 0)
1985 intel_wait_for_pipe_off(crtc);
b24e7179
JB
1986}
1987
832be82f
VS
1988static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1989{
1990 return IS_GEN2(dev_priv) ? 2048 : 4096;
1991}
1992
d88c4afd
VS
1993static unsigned int
1994intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
7b49f948 1995{
d88c4afd
VS
1996 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1997 unsigned int cpp = fb->format->cpp[plane];
1998
1999 switch (fb->modifier) {
7b49f948
VS
2000 case DRM_FORMAT_MOD_NONE:
2001 return cpp;
2002 case I915_FORMAT_MOD_X_TILED:
2003 if (IS_GEN2(dev_priv))
2004 return 128;
2005 else
2006 return 512;
2007 case I915_FORMAT_MOD_Y_TILED:
2008 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2009 return 128;
2010 else
2011 return 512;
2012 case I915_FORMAT_MOD_Yf_TILED:
2013 switch (cpp) {
2014 case 1:
2015 return 64;
2016 case 2:
2017 case 4:
2018 return 128;
2019 case 8:
2020 case 16:
2021 return 256;
2022 default:
2023 MISSING_CASE(cpp);
2024 return cpp;
2025 }
2026 break;
2027 default:
d88c4afd 2028 MISSING_CASE(fb->modifier);
7b49f948
VS
2029 return cpp;
2030 }
2031}
2032
d88c4afd
VS
2033static unsigned int
2034intel_tile_height(const struct drm_framebuffer *fb, int plane)
a57ce0b2 2035{
d88c4afd 2036 if (fb->modifier == DRM_FORMAT_MOD_NONE)
832be82f
VS
2037 return 1;
2038 else
d88c4afd
VS
2039 return intel_tile_size(to_i915(fb->dev)) /
2040 intel_tile_width_bytes(fb, plane);
6761dd31
TU
2041}
2042
8d0deca8 2043/* Return the tile dimensions in pixel units */
d88c4afd 2044static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
8d0deca8 2045 unsigned int *tile_width,
d88c4afd 2046 unsigned int *tile_height)
8d0deca8 2047{
d88c4afd
VS
2048 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2049 unsigned int cpp = fb->format->cpp[plane];
8d0deca8
VS
2050
2051 *tile_width = tile_width_bytes / cpp;
d88c4afd 2052 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
2053}
2054
6761dd31 2055unsigned int
d88c4afd
VS
2056intel_fb_align_height(const struct drm_framebuffer *fb,
2057 int plane, unsigned int height)
6761dd31 2058{
d88c4afd 2059 unsigned int tile_height = intel_tile_height(fb, plane);
832be82f
VS
2060
2061 return ALIGN(height, tile_height);
a57ce0b2
JB
2062}
2063
1663b9d6
VS
2064unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2065{
2066 unsigned int size = 0;
2067 int i;
2068
2069 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2070 size += rot_info->plane[i].width * rot_info->plane[i].height;
2071
2072 return size;
2073}
2074
75c82a53 2075static void
3465c580
VS
2076intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2077 const struct drm_framebuffer *fb,
2078 unsigned int rotation)
f64b98cd 2079{
7b92c047 2080 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2081 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2082 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2083 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2084 }
2085}
50470bb0 2086
603525d7 2087static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2088{
2089 if (INTEL_INFO(dev_priv)->gen >= 9)
2090 return 256 * 1024;
c0f86832 2091 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2092 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2093 return 128 * 1024;
2094 else if (INTEL_INFO(dev_priv)->gen >= 4)
2095 return 4 * 1024;
2096 else
44c5905e 2097 return 0;
4e9a86b6
VS
2098}
2099
d88c4afd
VS
2100static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2101 int plane)
603525d7 2102{
d88c4afd
VS
2103 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2104
b90c1ee1
VS
2105 /* AUX_DIST needs only 4K alignment */
2106 if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
2107 return 4096;
2108
d88c4afd 2109 switch (fb->modifier) {
603525d7
VS
2110 case DRM_FORMAT_MOD_NONE:
2111 return intel_linear_alignment(dev_priv);
2112 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2113 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2114 return 256 * 1024;
2115 return 0;
2116 case I915_FORMAT_MOD_Y_TILED:
2117 case I915_FORMAT_MOD_Yf_TILED:
2118 return 1 * 1024 * 1024;
2119 default:
d88c4afd 2120 MISSING_CASE(fb->modifier);
603525d7
VS
2121 return 0;
2122 }
2123}
2124
058d88c4
CW
2125struct i915_vma *
2126intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2127{
850c4cdc 2128 struct drm_device *dev = fb->dev;
fac5e23e 2129 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2130 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2131 struct i915_ggtt_view view;
058d88c4 2132 struct i915_vma *vma;
6b95a207 2133 u32 alignment;
6b95a207 2134
ebcdd39e
MR
2135 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2136
d88c4afd 2137 alignment = intel_surf_alignment(fb, 0);
6b95a207 2138
3465c580 2139 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2140
693db184
CW
2141 /* Note that the w/a also requires 64 PTE of padding following the
2142 * bo. We currently fill all unused PTE with the shadow page and so
2143 * we should always have valid PTE following the scanout preventing
2144 * the VT-d warning.
2145 */
48f112fe 2146 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2147 alignment = 256 * 1024;
2148
d6dd6843
PZ
2149 /*
2150 * Global gtt pte registers are special registers which actually forward
2151 * writes to a chunk of system memory. Which means that there is no risk
2152 * that the register values disappear as soon as we call
2153 * intel_runtime_pm_put(), so it is correct to wrap only the
2154 * pin/unpin/fence and not more.
2155 */
2156 intel_runtime_pm_get(dev_priv);
2157
058d88c4 2158 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2159 if (IS_ERR(vma))
2160 goto err;
6b95a207 2161
05a20d09 2162 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2163 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2164 * fence, whereas 965+ only requires a fence if using
2165 * framebuffer compression. For simplicity, we always, when
2166 * possible, install a fence as the cost is not that onerous.
2167 *
2168 * If we fail to fence the tiled scanout, then either the
2169 * modeset will reject the change (which is highly unlikely as
2170 * the affected systems, all but one, do not have unmappable
2171 * space) or we will not be able to enable full powersaving
2172 * techniques (also likely not to apply due to various limits
2173 * FBC and the like impose on the size of the buffer, which
2174 * presumably we violated anyway with this unmappable buffer).
2175 * Anyway, it is presumably better to stumble onwards with
2176 * something and try to run the system in a "less than optimal"
2177 * mode that matches the user configuration.
2178 */
2179 if (i915_vma_get_fence(vma) == 0)
2180 i915_vma_pin_fence(vma);
9807216f 2181 }
6b95a207 2182
be1e3415 2183 i915_vma_get(vma);
49ef5294 2184err:
d6dd6843 2185 intel_runtime_pm_put(dev_priv);
058d88c4 2186 return vma;
6b95a207
KH
2187}
2188
be1e3415 2189void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2190{
be1e3415 2191 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2192
49ef5294 2193 i915_vma_unpin_fence(vma);
058d88c4 2194 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2195 i915_vma_put(vma);
1690e1eb
CW
2196}
2197
ef78ec94
VS
2198static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2199 unsigned int rotation)
2200{
bd2ef25d 2201 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2202 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2203 else
2204 return fb->pitches[plane];
2205}
2206
6687c906
VS
2207/*
2208 * Convert the x/y offsets into a linear offset.
2209 * Only valid with 0/180 degree rotation, which is fine since linear
2210 * offset is only used with linear buffers on pre-hsw and tiled buffers
2211 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2212 */
2213u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2214 const struct intel_plane_state *state,
2215 int plane)
6687c906 2216{
2949056c 2217 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2218 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2219 unsigned int pitch = fb->pitches[plane];
2220
2221 return y * pitch + x * cpp;
2222}
2223
2224/*
2225 * Add the x/y offsets derived from fb->offsets[] to the user
2226 * specified plane src x/y offsets. The resulting x/y offsets
2227 * specify the start of scanout from the beginning of the gtt mapping.
2228 */
2229void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2230 const struct intel_plane_state *state,
2231 int plane)
6687c906
VS
2232
2233{
2949056c
VS
2234 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2235 unsigned int rotation = state->base.rotation;
6687c906 2236
bd2ef25d 2237 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2238 *x += intel_fb->rotated[plane].x;
2239 *y += intel_fb->rotated[plane].y;
2240 } else {
2241 *x += intel_fb->normal[plane].x;
2242 *y += intel_fb->normal[plane].y;
2243 }
2244}
2245
29cf9491 2246/*
29cf9491
VS
2247 * Input tile dimensions and pitch must already be
2248 * rotated to match x and y, and in pixel units.
2249 */
66a2d927
VS
2250static u32 _intel_adjust_tile_offset(int *x, int *y,
2251 unsigned int tile_width,
2252 unsigned int tile_height,
2253 unsigned int tile_size,
2254 unsigned int pitch_tiles,
2255 u32 old_offset,
2256 u32 new_offset)
29cf9491 2257{
b9b24038 2258 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2259 unsigned int tiles;
2260
2261 WARN_ON(old_offset & (tile_size - 1));
2262 WARN_ON(new_offset & (tile_size - 1));
2263 WARN_ON(new_offset > old_offset);
2264
2265 tiles = (old_offset - new_offset) / tile_size;
2266
2267 *y += tiles / pitch_tiles * tile_height;
2268 *x += tiles % pitch_tiles * tile_width;
2269
b9b24038
VS
2270 /* minimize x in case it got needlessly big */
2271 *y += *x / pitch_pixels * tile_height;
2272 *x %= pitch_pixels;
2273
29cf9491
VS
2274 return new_offset;
2275}
2276
66a2d927
VS
2277/*
2278 * Adjust the tile offset by moving the difference into
2279 * the x/y offsets.
2280 */
2281static u32 intel_adjust_tile_offset(int *x, int *y,
2282 const struct intel_plane_state *state, int plane,
2283 u32 old_offset, u32 new_offset)
2284{
2285 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2286 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2287 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2288 unsigned int rotation = state->base.rotation;
2289 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2290
2291 WARN_ON(new_offset > old_offset);
2292
bae781b2 2293 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2294 unsigned int tile_size, tile_width, tile_height;
2295 unsigned int pitch_tiles;
2296
2297 tile_size = intel_tile_size(dev_priv);
d88c4afd 2298 intel_tile_dims(fb, plane, &tile_width, &tile_height);
66a2d927 2299
bd2ef25d 2300 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2301 pitch_tiles = pitch / tile_height;
2302 swap(tile_width, tile_height);
2303 } else {
2304 pitch_tiles = pitch / (tile_width * cpp);
2305 }
2306
2307 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2308 tile_size, pitch_tiles,
2309 old_offset, new_offset);
2310 } else {
2311 old_offset += *y * pitch + *x * cpp;
2312
2313 *y = (old_offset - new_offset) / pitch;
2314 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2315 }
2316
2317 return new_offset;
2318}
2319
8d0deca8
VS
2320/*
2321 * Computes the linear offset to the base tile and adjusts
2322 * x, y. bytes per pixel is assumed to be a power-of-two.
2323 *
2324 * In the 90/270 rotated case, x and y are assumed
2325 * to be already rotated to match the rotated GTT view, and
2326 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2327 *
2328 * This function is used when computing the derived information
2329 * under intel_framebuffer, so using any of that information
2330 * here is not allowed. Anything under drm_framebuffer can be
2331 * used. This is why the user has to pass in the pitch since it
2332 * is specified in the rotated orientation.
8d0deca8 2333 */
6687c906
VS
2334static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2335 int *x, int *y,
2336 const struct drm_framebuffer *fb, int plane,
2337 unsigned int pitch,
2338 unsigned int rotation,
2339 u32 alignment)
c2c75131 2340{
bae781b2 2341 uint64_t fb_modifier = fb->modifier;
353c8598 2342 unsigned int cpp = fb->format->cpp[plane];
6687c906 2343 u32 offset, offset_aligned;
29cf9491 2344
29cf9491
VS
2345 if (alignment)
2346 alignment--;
2347
b5c65338 2348 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2349 unsigned int tile_size, tile_width, tile_height;
2350 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2351
d843310d 2352 tile_size = intel_tile_size(dev_priv);
d88c4afd 2353 intel_tile_dims(fb, plane, &tile_width, &tile_height);
8d0deca8 2354
bd2ef25d 2355 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2356 pitch_tiles = pitch / tile_height;
2357 swap(tile_width, tile_height);
2358 } else {
2359 pitch_tiles = pitch / (tile_width * cpp);
2360 }
d843310d
VS
2361
2362 tile_rows = *y / tile_height;
2363 *y %= tile_height;
c2c75131 2364
8d0deca8
VS
2365 tiles = *x / tile_width;
2366 *x %= tile_width;
bc752862 2367
29cf9491
VS
2368 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2369 offset_aligned = offset & ~alignment;
bc752862 2370
66a2d927
VS
2371 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2372 tile_size, pitch_tiles,
2373 offset, offset_aligned);
29cf9491 2374 } else {
bc752862 2375 offset = *y * pitch + *x * cpp;
29cf9491
VS
2376 offset_aligned = offset & ~alignment;
2377
4e9a86b6
VS
2378 *y = (offset & alignment) / pitch;
2379 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2380 }
29cf9491
VS
2381
2382 return offset_aligned;
c2c75131
DV
2383}
2384
6687c906 2385u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2386 const struct intel_plane_state *state,
2387 int plane)
6687c906 2388{
2949056c
VS
2389 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2390 const struct drm_framebuffer *fb = state->base.fb;
2391 unsigned int rotation = state->base.rotation;
ef78ec94 2392 int pitch = intel_fb_pitch(fb, plane, rotation);
b90c1ee1 2393 u32 alignment = intel_surf_alignment(fb, plane);
6687c906
VS
2394
2395 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2396 rotation, alignment);
2397}
2398
2399/* Convert the fb->offset[] linear offset into x/y offsets */
2400static void intel_fb_offset_to_xy(int *x, int *y,
2401 const struct drm_framebuffer *fb, int plane)
2402{
353c8598 2403 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2404 unsigned int pitch = fb->pitches[plane];
2405 u32 linear_offset = fb->offsets[plane];
2406
2407 *y = linear_offset / pitch;
2408 *x = linear_offset % pitch / cpp;
2409}
2410
72618ebf
VS
2411static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2412{
2413 switch (fb_modifier) {
2414 case I915_FORMAT_MOD_X_TILED:
2415 return I915_TILING_X;
2416 case I915_FORMAT_MOD_Y_TILED:
2417 return I915_TILING_Y;
2418 default:
2419 return I915_TILING_NONE;
2420 }
2421}
2422
6687c906
VS
2423static int
2424intel_fill_fb_info(struct drm_i915_private *dev_priv,
2425 struct drm_framebuffer *fb)
2426{
2427 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2428 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2429 u32 gtt_offset_rotated = 0;
2430 unsigned int max_size = 0;
bcb0b461 2431 int i, num_planes = fb->format->num_planes;
6687c906
VS
2432 unsigned int tile_size = intel_tile_size(dev_priv);
2433
2434 for (i = 0; i < num_planes; i++) {
2435 unsigned int width, height;
2436 unsigned int cpp, size;
2437 u32 offset;
2438 int x, y;
2439
353c8598 2440 cpp = fb->format->cpp[i];
145fcb11
VS
2441 width = drm_framebuffer_plane_width(fb->width, fb, i);
2442 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906
VS
2443
2444 intel_fb_offset_to_xy(&x, &y, fb, i);
2445
60d5f2a4
VS
2446 /*
2447 * The fence (if used) is aligned to the start of the object
2448 * so having the framebuffer wrap around across the edge of the
2449 * fenced region doesn't really work. We have no API to configure
2450 * the fence start offset within the object (nor could we probably
2451 * on gen2/3). So it's just easier if we just require that the
2452 * fb layout agrees with the fence layout. We already check that the
2453 * fb stride matches the fence stride elsewhere.
2454 */
2455 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2456 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2457 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2458 i, fb->offsets[i]);
60d5f2a4
VS
2459 return -EINVAL;
2460 }
2461
6687c906
VS
2462 /*
2463 * First pixel of the framebuffer from
2464 * the start of the normal gtt mapping.
2465 */
2466 intel_fb->normal[i].x = x;
2467 intel_fb->normal[i].y = y;
2468
2469 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
3ca46c0a 2470 fb, i, fb->pitches[i],
cc926387 2471 DRM_ROTATE_0, tile_size);
6687c906
VS
2472 offset /= tile_size;
2473
bae781b2 2474 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2475 unsigned int tile_width, tile_height;
2476 unsigned int pitch_tiles;
2477 struct drm_rect r;
2478
d88c4afd 2479 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2480
2481 rot_info->plane[i].offset = offset;
2482 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2483 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2484 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2485
2486 intel_fb->rotated[i].pitch =
2487 rot_info->plane[i].height * tile_height;
2488
2489 /* how many tiles does this plane need */
2490 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2491 /*
2492 * If the plane isn't horizontally tile aligned,
2493 * we need one more tile.
2494 */
2495 if (x != 0)
2496 size++;
2497
2498 /* rotate the x/y offsets to match the GTT view */
2499 r.x1 = x;
2500 r.y1 = y;
2501 r.x2 = x + width;
2502 r.y2 = y + height;
2503 drm_rect_rotate(&r,
2504 rot_info->plane[i].width * tile_width,
2505 rot_info->plane[i].height * tile_height,
cc926387 2506 DRM_ROTATE_270);
6687c906
VS
2507 x = r.x1;
2508 y = r.y1;
2509
2510 /* rotate the tile dimensions to match the GTT view */
2511 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2512 swap(tile_width, tile_height);
2513
2514 /*
2515 * We only keep the x/y offsets, so push all of the
2516 * gtt offset into the x/y offsets.
2517 */
46a1bd28
ACO
2518 _intel_adjust_tile_offset(&x, &y,
2519 tile_width, tile_height,
2520 tile_size, pitch_tiles,
66a2d927 2521 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2522
2523 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2524
2525 /*
2526 * First pixel of the framebuffer from
2527 * the start of the rotated gtt mapping.
2528 */
2529 intel_fb->rotated[i].x = x;
2530 intel_fb->rotated[i].y = y;
2531 } else {
2532 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2533 x * cpp, tile_size);
2534 }
2535
2536 /* how many tiles in total needed in the bo */
2537 max_size = max(max_size, offset + size);
2538 }
2539
144cc143
VS
2540 if (max_size * tile_size > intel_fb->obj->base.size) {
2541 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2542 max_size * tile_size, intel_fb->obj->base.size);
6687c906
VS
2543 return -EINVAL;
2544 }
2545
2546 return 0;
2547}
2548
b35d63fa 2549static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2550{
2551 switch (format) {
2552 case DISPPLANE_8BPP:
2553 return DRM_FORMAT_C8;
2554 case DISPPLANE_BGRX555:
2555 return DRM_FORMAT_XRGB1555;
2556 case DISPPLANE_BGRX565:
2557 return DRM_FORMAT_RGB565;
2558 default:
2559 case DISPPLANE_BGRX888:
2560 return DRM_FORMAT_XRGB8888;
2561 case DISPPLANE_RGBX888:
2562 return DRM_FORMAT_XBGR8888;
2563 case DISPPLANE_BGRX101010:
2564 return DRM_FORMAT_XRGB2101010;
2565 case DISPPLANE_RGBX101010:
2566 return DRM_FORMAT_XBGR2101010;
2567 }
2568}
2569
bc8d7dff
DL
2570static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2571{
2572 switch (format) {
2573 case PLANE_CTL_FORMAT_RGB_565:
2574 return DRM_FORMAT_RGB565;
2575 default:
2576 case PLANE_CTL_FORMAT_XRGB_8888:
2577 if (rgb_order) {
2578 if (alpha)
2579 return DRM_FORMAT_ABGR8888;
2580 else
2581 return DRM_FORMAT_XBGR8888;
2582 } else {
2583 if (alpha)
2584 return DRM_FORMAT_ARGB8888;
2585 else
2586 return DRM_FORMAT_XRGB8888;
2587 }
2588 case PLANE_CTL_FORMAT_XRGB_2101010:
2589 if (rgb_order)
2590 return DRM_FORMAT_XBGR2101010;
2591 else
2592 return DRM_FORMAT_XRGB2101010;
2593 }
2594}
2595
5724dbd1 2596static bool
f6936e29
DV
2597intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2598 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2599{
2600 struct drm_device *dev = crtc->base.dev;
3badb49f 2601 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2602 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2603 struct drm_i915_gem_object *obj = NULL;
2604 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2605 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2606 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2607 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2608 PAGE_SIZE);
2609
2610 size_aligned -= base_aligned;
46f297fb 2611
ff2652ea
CW
2612 if (plane_config->size == 0)
2613 return false;
2614
3badb49f
PZ
2615 /* If the FB is too big, just don't use it since fbdev is not very
2616 * important and we should probably use that space with FBC or other
2617 * features. */
72e96d64 2618 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2619 return false;
2620
12c83d99 2621 mutex_lock(&dev->struct_mutex);
187685cb 2622 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2623 base_aligned,
2624 base_aligned,
2625 size_aligned);
24dbf51a
CW
2626 mutex_unlock(&dev->struct_mutex);
2627 if (!obj)
484b41dd 2628 return false;
46f297fb 2629
3e510a8e
CW
2630 if (plane_config->tiling == I915_TILING_X)
2631 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2632
438b74a5 2633 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2634 mode_cmd.width = fb->width;
2635 mode_cmd.height = fb->height;
2636 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2637 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2638 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2639
24dbf51a 2640 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2641 DRM_DEBUG_KMS("intel fb init failed\n");
2642 goto out_unref_obj;
2643 }
12c83d99 2644
484b41dd 2645
f6936e29 2646 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2647 return true;
46f297fb
JB
2648
2649out_unref_obj:
f8c417cd 2650 i915_gem_object_put(obj);
484b41dd
JB
2651 return false;
2652}
2653
5a21b665
DV
2654/* Update plane->state->fb to match plane->fb after driver-internal updates */
2655static void
2656update_state_fb(struct drm_plane *plane)
2657{
2658 if (plane->fb == plane->state->fb)
2659 return;
2660
2661 if (plane->state->fb)
2662 drm_framebuffer_unreference(plane->state->fb);
2663 plane->state->fb = plane->fb;
2664 if (plane->state->fb)
2665 drm_framebuffer_reference(plane->state->fb);
2666}
2667
e9728bd8
VS
2668static void
2669intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2670 struct intel_plane_state *plane_state,
2671 bool visible)
2672{
2673 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2674
2675 plane_state->base.visible = visible;
2676
2677 /* FIXME pre-g4x don't work like this */
2678 if (visible) {
2679 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2680 crtc_state->active_planes |= BIT(plane->id);
2681 } else {
2682 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2683 crtc_state->active_planes &= ~BIT(plane->id);
2684 }
2685
2686 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2687 crtc_state->base.crtc->name,
2688 crtc_state->active_planes);
2689}
2690
5724dbd1 2691static void
f6936e29
DV
2692intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2693 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2694{
2695 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2696 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2697 struct drm_crtc *c;
2ff8fde1 2698 struct drm_i915_gem_object *obj;
88595ac9 2699 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2700 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2701 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2702 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2703 struct intel_plane_state *intel_state =
2704 to_intel_plane_state(plane_state);
88595ac9 2705 struct drm_framebuffer *fb;
484b41dd 2706
2d14030b 2707 if (!plane_config->fb)
484b41dd
JB
2708 return;
2709
f6936e29 2710 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2711 fb = &plane_config->fb->base;
2712 goto valid_fb;
f55548b5 2713 }
484b41dd 2714
2d14030b 2715 kfree(plane_config->fb);
484b41dd
JB
2716
2717 /*
2718 * Failed to alloc the obj, check to see if we should share
2719 * an fb with another CRTC instead
2720 */
70e1e0ec 2721 for_each_crtc(dev, c) {
be1e3415 2722 struct intel_plane_state *state;
484b41dd
JB
2723
2724 if (c == &intel_crtc->base)
2725 continue;
2726
be1e3415 2727 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2728 continue;
2729
be1e3415
CW
2730 state = to_intel_plane_state(c->primary->state);
2731 if (!state->vma)
484b41dd
JB
2732 continue;
2733
be1e3415
CW
2734 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2735 fb = c->primary->fb;
88595ac9
DV
2736 drm_framebuffer_reference(fb);
2737 goto valid_fb;
484b41dd
JB
2738 }
2739 }
88595ac9 2740
200757f5
MR
2741 /*
2742 * We've failed to reconstruct the BIOS FB. Current display state
2743 * indicates that the primary plane is visible, but has a NULL FB,
2744 * which will lead to problems later if we don't fix it up. The
2745 * simplest solution is to just disable the primary plane now and
2746 * pretend the BIOS never had it enabled.
2747 */
e9728bd8
VS
2748 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2749 to_intel_plane_state(plane_state),
2750 false);
2622a081 2751 intel_pre_disable_primary_noatomic(&intel_crtc->base);
72259536 2752 trace_intel_disable_plane(primary, intel_crtc);
200757f5
MR
2753 intel_plane->disable_plane(primary, &intel_crtc->base);
2754
88595ac9
DV
2755 return;
2756
2757valid_fb:
be1e3415
CW
2758 mutex_lock(&dev->struct_mutex);
2759 intel_state->vma =
2760 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2761 mutex_unlock(&dev->struct_mutex);
2762 if (IS_ERR(intel_state->vma)) {
2763 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2764 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2765
2766 intel_state->vma = NULL;
2767 drm_framebuffer_unreference(fb);
2768 return;
2769 }
2770
f44e2659
VS
2771 plane_state->src_x = 0;
2772 plane_state->src_y = 0;
be5651f2
ML
2773 plane_state->src_w = fb->width << 16;
2774 plane_state->src_h = fb->height << 16;
2775
f44e2659
VS
2776 plane_state->crtc_x = 0;
2777 plane_state->crtc_y = 0;
be5651f2
ML
2778 plane_state->crtc_w = fb->width;
2779 plane_state->crtc_h = fb->height;
2780
1638d30c
RC
2781 intel_state->base.src = drm_plane_state_src(plane_state);
2782 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2783
88595ac9 2784 obj = intel_fb_obj(fb);
3e510a8e 2785 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2786 dev_priv->preserve_bios_swizzle = true;
2787
be5651f2
ML
2788 drm_framebuffer_reference(fb);
2789 primary->fb = primary->state->fb = fb;
36750f28 2790 primary->crtc = primary->state->crtc = &intel_crtc->base;
e9728bd8
VS
2791
2792 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2793 to_intel_plane_state(plane_state),
2794 true);
2795
faf5bf0a
CW
2796 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2797 &obj->frontbuffer_bits);
46f297fb
JB
2798}
2799
b63a16f6
VS
2800static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2801 unsigned int rotation)
2802{
353c8598 2803 int cpp = fb->format->cpp[plane];
b63a16f6 2804
bae781b2 2805 switch (fb->modifier) {
b63a16f6
VS
2806 case DRM_FORMAT_MOD_NONE:
2807 case I915_FORMAT_MOD_X_TILED:
2808 switch (cpp) {
2809 case 8:
2810 return 4096;
2811 case 4:
2812 case 2:
2813 case 1:
2814 return 8192;
2815 default:
2816 MISSING_CASE(cpp);
2817 break;
2818 }
2819 break;
2820 case I915_FORMAT_MOD_Y_TILED:
2821 case I915_FORMAT_MOD_Yf_TILED:
2822 switch (cpp) {
2823 case 8:
2824 return 2048;
2825 case 4:
2826 return 4096;
2827 case 2:
2828 case 1:
2829 return 8192;
2830 default:
2831 MISSING_CASE(cpp);
2832 break;
2833 }
2834 break;
2835 default:
bae781b2 2836 MISSING_CASE(fb->modifier);
b63a16f6
VS
2837 }
2838
2839 return 2048;
2840}
2841
2842static int skl_check_main_surface(struct intel_plane_state *plane_state)
2843{
b63a16f6
VS
2844 const struct drm_framebuffer *fb = plane_state->base.fb;
2845 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2846 int x = plane_state->base.src.x1 >> 16;
2847 int y = plane_state->base.src.y1 >> 16;
2848 int w = drm_rect_width(&plane_state->base.src) >> 16;
2849 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2850 int max_width = skl_max_plane_width(fb, 0, rotation);
2851 int max_height = 4096;
8d970654 2852 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2853
2854 if (w > max_width || h > max_height) {
2855 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2856 w, h, max_width, max_height);
2857 return -EINVAL;
2858 }
2859
2860 intel_add_fb_offsets(&x, &y, plane_state, 0);
2861 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
d88c4afd 2862 alignment = intel_surf_alignment(fb, 0);
b63a16f6 2863
8d970654
VS
2864 /*
2865 * AUX surface offset is specified as the distance from the
2866 * main surface offset, and it must be non-negative. Make
2867 * sure that is what we will get.
2868 */
2869 if (offset > aux_offset)
2870 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2871 offset, aux_offset & ~(alignment - 1));
2872
b63a16f6
VS
2873 /*
2874 * When using an X-tiled surface, the plane blows up
2875 * if the x offset + width exceed the stride.
2876 *
2877 * TODO: linear and Y-tiled seem fine, Yf untested,
2878 */
bae781b2 2879 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2880 int cpp = fb->format->cpp[0];
b63a16f6
VS
2881
2882 while ((x + w) * cpp > fb->pitches[0]) {
2883 if (offset == 0) {
2884 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2885 return -EINVAL;
2886 }
2887
2888 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2889 offset, offset - alignment);
2890 }
2891 }
2892
2893 plane_state->main.offset = offset;
2894 plane_state->main.x = x;
2895 plane_state->main.y = y;
2896
2897 return 0;
2898}
2899
8d970654
VS
2900static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2901{
2902 const struct drm_framebuffer *fb = plane_state->base.fb;
2903 unsigned int rotation = plane_state->base.rotation;
2904 int max_width = skl_max_plane_width(fb, 1, rotation);
2905 int max_height = 4096;
cc926387
DV
2906 int x = plane_state->base.src.x1 >> 17;
2907 int y = plane_state->base.src.y1 >> 17;
2908 int w = drm_rect_width(&plane_state->base.src) >> 17;
2909 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2910 u32 offset;
2911
2912 intel_add_fb_offsets(&x, &y, plane_state, 1);
2913 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2914
2915 /* FIXME not quite sure how/if these apply to the chroma plane */
2916 if (w > max_width || h > max_height) {
2917 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2918 w, h, max_width, max_height);
2919 return -EINVAL;
2920 }
2921
2922 plane_state->aux.offset = offset;
2923 plane_state->aux.x = x;
2924 plane_state->aux.y = y;
2925
2926 return 0;
2927}
2928
b63a16f6
VS
2929int skl_check_plane_surface(struct intel_plane_state *plane_state)
2930{
2931 const struct drm_framebuffer *fb = plane_state->base.fb;
2932 unsigned int rotation = plane_state->base.rotation;
2933 int ret;
2934
a5e4c7d0
VS
2935 if (!plane_state->base.visible)
2936 return 0;
2937
b63a16f6 2938 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2939 if (drm_rotation_90_or_270(rotation))
cc926387 2940 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2941 fb->width << 16, fb->height << 16,
2942 DRM_ROTATE_270);
b63a16f6 2943
8d970654
VS
2944 /*
2945 * Handle the AUX surface first since
2946 * the main surface setup depends on it.
2947 */
438b74a5 2948 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
2949 ret = skl_check_nv12_aux_surface(plane_state);
2950 if (ret)
2951 return ret;
2952 } else {
2953 plane_state->aux.offset = ~0xfff;
2954 plane_state->aux.x = 0;
2955 plane_state->aux.y = 0;
2956 }
2957
b63a16f6
VS
2958 ret = skl_check_main_surface(plane_state);
2959 if (ret)
2960 return ret;
2961
2962 return 0;
2963}
2964
7145f60a
VS
2965static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
2966 const struct intel_plane_state *plane_state)
81255565 2967{
7145f60a
VS
2968 struct drm_i915_private *dev_priv =
2969 to_i915(plane_state->base.plane->dev);
2970 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2971 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 2972 unsigned int rotation = plane_state->base.rotation;
7145f60a 2973 u32 dspcntr;
c9ba6fad 2974
7145f60a 2975 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 2976
7145f60a
VS
2977 if (IS_G4X(dev_priv))
2978 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 2979
6315b5d3 2980 if (INTEL_GEN(dev_priv) < 4) {
7145f60a 2981 if (crtc->pipe == PIPE_B)
f45651ba 2982 dspcntr |= DISPPLANE_SEL_PIPE_B;
f45651ba 2983 }
81255565 2984
438b74a5 2985 switch (fb->format->format) {
57779d06 2986 case DRM_FORMAT_C8:
81255565
JB
2987 dspcntr |= DISPPLANE_8BPP;
2988 break;
57779d06 2989 case DRM_FORMAT_XRGB1555:
57779d06 2990 dspcntr |= DISPPLANE_BGRX555;
81255565 2991 break;
57779d06
VS
2992 case DRM_FORMAT_RGB565:
2993 dspcntr |= DISPPLANE_BGRX565;
2994 break;
2995 case DRM_FORMAT_XRGB8888:
57779d06
VS
2996 dspcntr |= DISPPLANE_BGRX888;
2997 break;
2998 case DRM_FORMAT_XBGR8888:
57779d06
VS
2999 dspcntr |= DISPPLANE_RGBX888;
3000 break;
3001 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3002 dspcntr |= DISPPLANE_BGRX101010;
3003 break;
3004 case DRM_FORMAT_XBGR2101010:
57779d06 3005 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3006 break;
3007 default:
7145f60a
VS
3008 MISSING_CASE(fb->format->format);
3009 return 0;
81255565 3010 }
57779d06 3011
72618ebf 3012 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3013 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3014 dspcntr |= DISPPLANE_TILED;
81255565 3015
df0cd455
VS
3016 if (rotation & DRM_ROTATE_180)
3017 dspcntr |= DISPPLANE_ROTATE_180;
3018
4ea7be2b
VS
3019 if (rotation & DRM_REFLECT_X)
3020 dspcntr |= DISPPLANE_MIRROR;
3021
7145f60a
VS
3022 return dspcntr;
3023}
3024
3025static void i9xx_update_primary_plane(struct drm_plane *primary,
3026 const struct intel_crtc_state *crtc_state,
3027 const struct intel_plane_state *plane_state)
3028{
3029 struct drm_i915_private *dev_priv = to_i915(primary->dev);
3030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3031 struct drm_framebuffer *fb = plane_state->base.fb;
3032 int plane = intel_crtc->plane;
3033 u32 linear_offset;
3034 u32 dspcntr;
3035 i915_reg_t reg = DSPCNTR(plane);
3036 unsigned int rotation = plane_state->base.rotation;
3037 int x = plane_state->base.src.x1 >> 16;
3038 int y = plane_state->base.src.y1 >> 16;
3039 unsigned long irqflags;
3040
3041 dspcntr = i9xx_plane_ctl(crtc_state, plane_state);
de1aa629 3042
2949056c 3043 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3044
6315b5d3 3045 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3046 intel_crtc->dspaddr_offset =
2949056c 3047 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3048
f22aa143 3049 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3050 x += crtc_state->pipe_src_w - 1;
3051 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3052 } else if (rotation & DRM_REFLECT_X) {
3053 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3054 }
3055
2949056c 3056 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3057
6315b5d3 3058 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3059 intel_crtc->dspaddr_offset = linear_offset;
3060
2db3366b
PZ
3061 intel_crtc->adjusted_x = x;
3062 intel_crtc->adjusted_y = y;
3063
dd584fc0
VS
3064 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3065
78587de2
VS
3066 if (INTEL_GEN(dev_priv) < 4) {
3067 /* pipesrc and dspsize control the size that is scaled from,
3068 * which should always be the user's requested size.
3069 */
dd584fc0
VS
3070 I915_WRITE_FW(DSPSIZE(plane),
3071 ((crtc_state->pipe_src_h - 1) << 16) |
3072 (crtc_state->pipe_src_w - 1));
3073 I915_WRITE_FW(DSPPOS(plane), 0);
78587de2 3074 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
dd584fc0
VS
3075 I915_WRITE_FW(PRIMSIZE(plane),
3076 ((crtc_state->pipe_src_h - 1) << 16) |
3077 (crtc_state->pipe_src_w - 1));
3078 I915_WRITE_FW(PRIMPOS(plane), 0);
3079 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
78587de2
VS
3080 }
3081
dd584fc0 3082 I915_WRITE_FW(reg, dspcntr);
48404c1e 3083
dd584fc0 3084 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3085 if (INTEL_GEN(dev_priv) >= 4) {
dd584fc0
VS
3086 I915_WRITE_FW(DSPSURF(plane),
3087 intel_plane_ggtt_offset(plane_state) +
3088 intel_crtc->dspaddr_offset);
3089 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3090 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bfb81049 3091 } else {
dd584fc0
VS
3092 I915_WRITE_FW(DSPADDR(plane),
3093 intel_plane_ggtt_offset(plane_state) +
3094 intel_crtc->dspaddr_offset);
bfb81049 3095 }
dd584fc0
VS
3096 POSTING_READ_FW(reg);
3097
3098 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3099}
3100
a8d201af
ML
3101static void i9xx_disable_primary_plane(struct drm_plane *primary,
3102 struct drm_crtc *crtc)
17638cd6
JB
3103{
3104 struct drm_device *dev = crtc->dev;
fac5e23e 3105 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3107 int plane = intel_crtc->plane;
dd584fc0
VS
3108 unsigned long irqflags;
3109
3110 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3111
dd584fc0 3112 I915_WRITE_FW(DSPCNTR(plane), 0);
a8d201af 3113 if (INTEL_INFO(dev_priv)->gen >= 4)
dd584fc0 3114 I915_WRITE_FW(DSPSURF(plane), 0);
a8d201af 3115 else
dd584fc0
VS
3116 I915_WRITE_FW(DSPADDR(plane), 0);
3117 POSTING_READ_FW(DSPCNTR(plane));
3118
3119 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3120}
c9ba6fad 3121
7145f60a
VS
3122static u32 ironlake_plane_ctl(const struct intel_crtc_state *crtc_state,
3123 const struct intel_plane_state *plane_state)
a8d201af 3124{
7145f60a
VS
3125 struct drm_i915_private *dev_priv =
3126 to_i915(plane_state->base.plane->dev);
3127 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 3128 unsigned int rotation = plane_state->base.rotation;
7145f60a
VS
3129 u32 dspcntr;
3130
3131 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
c9ba6fad 3132
7145f60a
VS
3133 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
3134 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 3135
8652744b 3136 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3137 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3138
438b74a5 3139 switch (fb->format->format) {
57779d06 3140 case DRM_FORMAT_C8:
17638cd6
JB
3141 dspcntr |= DISPPLANE_8BPP;
3142 break;
57779d06
VS
3143 case DRM_FORMAT_RGB565:
3144 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3145 break;
57779d06 3146 case DRM_FORMAT_XRGB8888:
57779d06
VS
3147 dspcntr |= DISPPLANE_BGRX888;
3148 break;
3149 case DRM_FORMAT_XBGR8888:
57779d06
VS
3150 dspcntr |= DISPPLANE_RGBX888;
3151 break;
3152 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3153 dspcntr |= DISPPLANE_BGRX101010;
3154 break;
3155 case DRM_FORMAT_XBGR2101010:
57779d06 3156 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3157 break;
3158 default:
7145f60a
VS
3159 MISSING_CASE(fb->format->format);
3160 return 0;
17638cd6
JB
3161 }
3162
bae781b2 3163 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3164 dspcntr |= DISPPLANE_TILED;
17638cd6 3165
df0cd455
VS
3166 if (rotation & DRM_ROTATE_180)
3167 dspcntr |= DISPPLANE_ROTATE_180;
3168
7145f60a
VS
3169 return dspcntr;
3170}
3171
3172static void ironlake_update_primary_plane(struct drm_plane *primary,
3173 const struct intel_crtc_state *crtc_state,
3174 const struct intel_plane_state *plane_state)
3175{
3176 struct drm_device *dev = primary->dev;
3177 struct drm_i915_private *dev_priv = to_i915(dev);
3178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3179 struct drm_framebuffer *fb = plane_state->base.fb;
3180 int plane = intel_crtc->plane;
3181 u32 linear_offset;
3182 u32 dspcntr;
3183 i915_reg_t reg = DSPCNTR(plane);
3184 unsigned int rotation = plane_state->base.rotation;
3185 int x = plane_state->base.src.x1 >> 16;
3186 int y = plane_state->base.src.y1 >> 16;
3187 unsigned long irqflags;
3188
3189 dspcntr = ironlake_plane_ctl(crtc_state, plane_state);
17638cd6 3190
2949056c 3191 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3192
c2c75131 3193 intel_crtc->dspaddr_offset =
2949056c 3194 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3195
df0cd455
VS
3196 /* HSW+ does this automagically in hardware */
3197 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3198 rotation & DRM_ROTATE_180) {
3199 x += crtc_state->pipe_src_w - 1;
3200 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3201 }
3202
2949056c 3203 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3204
2db3366b
PZ
3205 intel_crtc->adjusted_x = x;
3206 intel_crtc->adjusted_y = y;
3207
dd584fc0
VS
3208 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3209
3210 I915_WRITE_FW(reg, dspcntr);
17638cd6 3211
dd584fc0
VS
3212 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
3213 I915_WRITE_FW(DSPSURF(plane),
3214 intel_plane_ggtt_offset(plane_state) +
3215 intel_crtc->dspaddr_offset);
8652744b 3216 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
dd584fc0 3217 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
bc1c91eb 3218 } else {
dd584fc0
VS
3219 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3220 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bc1c91eb 3221 }
dd584fc0
VS
3222 POSTING_READ_FW(reg);
3223
3224 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3225}
3226
d88c4afd
VS
3227static u32
3228intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
b321803d 3229{
d88c4afd 3230 if (fb->modifier == DRM_FORMAT_MOD_NONE)
b321803d 3231 return 64;
d88c4afd
VS
3232 else
3233 return intel_tile_width_bytes(fb, plane);
b321803d
DL
3234}
3235
e435d6e5
ML
3236static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3237{
3238 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3239 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3240
3241 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3242 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3243 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3244}
3245
a1b2278e
CK
3246/*
3247 * This function detaches (aka. unbinds) unused scalers in hardware
3248 */
0583236e 3249static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3250{
a1b2278e
CK
3251 struct intel_crtc_scaler_state *scaler_state;
3252 int i;
3253
a1b2278e
CK
3254 scaler_state = &intel_crtc->config->scaler_state;
3255
3256 /* loop through and disable scalers that aren't in use */
3257 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3258 if (!scaler_state->scalers[i].in_use)
3259 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3260 }
3261}
3262
d2196774
VS
3263u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3264 unsigned int rotation)
3265{
1b500535
VS
3266 u32 stride;
3267
3268 if (plane >= fb->format->num_planes)
3269 return 0;
3270
3271 stride = intel_fb_pitch(fb, plane, rotation);
d2196774
VS
3272
3273 /*
3274 * The stride is either expressed as a multiple of 64 bytes chunks for
3275 * linear buffers or in number of tiles for tiled buffers.
3276 */
d88c4afd
VS
3277 if (drm_rotation_90_or_270(rotation))
3278 stride /= intel_tile_height(fb, plane);
3279 else
3280 stride /= intel_fb_stride_alignment(fb, plane);
d2196774
VS
3281
3282 return stride;
3283}
3284
2e881264 3285static u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3286{
6156a456 3287 switch (pixel_format) {
d161cf7a 3288 case DRM_FORMAT_C8:
c34ce3d1 3289 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3290 case DRM_FORMAT_RGB565:
c34ce3d1 3291 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3292 case DRM_FORMAT_XBGR8888:
c34ce3d1 3293 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3294 case DRM_FORMAT_XRGB8888:
c34ce3d1 3295 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3296 /*
3297 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3298 * to be already pre-multiplied. We need to add a knob (or a different
3299 * DRM_FORMAT) for user-space to configure that.
3300 */
f75fb42a 3301 case DRM_FORMAT_ABGR8888:
c34ce3d1 3302 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3303 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3304 case DRM_FORMAT_ARGB8888:
c34ce3d1 3305 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3306 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3307 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3308 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3309 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3310 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3311 case DRM_FORMAT_YUYV:
c34ce3d1 3312 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3313 case DRM_FORMAT_YVYU:
c34ce3d1 3314 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3315 case DRM_FORMAT_UYVY:
c34ce3d1 3316 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3317 case DRM_FORMAT_VYUY:
c34ce3d1 3318 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3319 default:
4249eeef 3320 MISSING_CASE(pixel_format);
70d21f0e 3321 }
8cfcba41 3322
c34ce3d1 3323 return 0;
6156a456 3324}
70d21f0e 3325
2e881264 3326static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3327{
6156a456 3328 switch (fb_modifier) {
30af77c4 3329 case DRM_FORMAT_MOD_NONE:
70d21f0e 3330 break;
30af77c4 3331 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3332 return PLANE_CTL_TILED_X;
b321803d 3333 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3334 return PLANE_CTL_TILED_Y;
b321803d 3335 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3336 return PLANE_CTL_TILED_YF;
70d21f0e 3337 default:
6156a456 3338 MISSING_CASE(fb_modifier);
70d21f0e 3339 }
8cfcba41 3340
c34ce3d1 3341 return 0;
6156a456 3342}
70d21f0e 3343
2e881264 3344static u32 skl_plane_ctl_rotation(unsigned int rotation)
6156a456 3345{
3b7a5119 3346 switch (rotation) {
31ad61e4 3347 case DRM_ROTATE_0:
6156a456 3348 break;
1e8df167
SJ
3349 /*
3350 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3351 * while i915 HW rotation is clockwise, thats why this swapping.
3352 */
31ad61e4 3353 case DRM_ROTATE_90:
1e8df167 3354 return PLANE_CTL_ROTATE_270;
31ad61e4 3355 case DRM_ROTATE_180:
c34ce3d1 3356 return PLANE_CTL_ROTATE_180;
31ad61e4 3357 case DRM_ROTATE_270:
1e8df167 3358 return PLANE_CTL_ROTATE_90;
6156a456
CK
3359 default:
3360 MISSING_CASE(rotation);
3361 }
3362
c34ce3d1 3363 return 0;
6156a456
CK
3364}
3365
2e881264
VS
3366u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3367 const struct intel_plane_state *plane_state)
46f788ba
VS
3368{
3369 struct drm_i915_private *dev_priv =
3370 to_i915(plane_state->base.plane->dev);
3371 const struct drm_framebuffer *fb = plane_state->base.fb;
3372 unsigned int rotation = plane_state->base.rotation;
2e881264 3373 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3374 u32 plane_ctl;
3375
3376 plane_ctl = PLANE_CTL_ENABLE;
3377
3378 if (!IS_GEMINILAKE(dev_priv)) {
3379 plane_ctl |=
3380 PLANE_CTL_PIPE_GAMMA_ENABLE |
3381 PLANE_CTL_PIPE_CSC_ENABLE |
3382 PLANE_CTL_PLANE_GAMMA_DISABLE;
3383 }
3384
3385 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3386 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
3387 plane_ctl |= skl_plane_ctl_rotation(rotation);
3388
2e881264
VS
3389 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3390 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3391 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3392 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3393
46f788ba
VS
3394 return plane_ctl;
3395}
3396
a8d201af
ML
3397static void skylake_update_primary_plane(struct drm_plane *plane,
3398 const struct intel_crtc_state *crtc_state,
3399 const struct intel_plane_state *plane_state)
6156a456 3400{
a8d201af 3401 struct drm_device *dev = plane->dev;
fac5e23e 3402 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3404 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3405 enum plane_id plane_id = to_intel_plane(plane)->id;
3406 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3407 u32 plane_ctl;
a8d201af 3408 unsigned int rotation = plane_state->base.rotation;
d2196774 3409 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3410 u32 surf_addr = plane_state->main.offset;
a8d201af 3411 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3412 int src_x = plane_state->main.x;
3413 int src_y = plane_state->main.y;
936e71e3
VS
3414 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3415 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3416 int dst_x = plane_state->base.dst.x1;
3417 int dst_y = plane_state->base.dst.y1;
3418 int dst_w = drm_rect_width(&plane_state->base.dst);
3419 int dst_h = drm_rect_height(&plane_state->base.dst);
dd584fc0 3420 unsigned long irqflags;
70d21f0e 3421
46f788ba 3422 plane_ctl = skl_plane_ctl(crtc_state, plane_state);
6156a456 3423
6687c906
VS
3424 /* Sizes are 0 based */
3425 src_w--;
3426 src_h--;
3427 dst_w--;
3428 dst_h--;
3429
4c0b8a8b
PZ
3430 intel_crtc->dspaddr_offset = surf_addr;
3431
6687c906
VS
3432 intel_crtc->adjusted_x = src_x;
3433 intel_crtc->adjusted_y = src_y;
2db3366b 3434
dd584fc0
VS
3435 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3436
78587de2 3437 if (IS_GEMINILAKE(dev_priv)) {
dd584fc0
VS
3438 I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
3439 PLANE_COLOR_PIPE_GAMMA_ENABLE |
3440 PLANE_COLOR_PIPE_CSC_ENABLE |
3441 PLANE_COLOR_PLANE_GAMMA_DISABLE);
78587de2
VS
3442 }
3443
dd584fc0
VS
3444 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
3445 I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3446 I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
3447 I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3448
3449 if (scaler_id >= 0) {
3450 uint32_t ps_ctrl = 0;
3451
3452 WARN_ON(!dst_w || !dst_h);
8e816bb4 3453 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456 3454 crtc_state->scaler_state.scalers[scaler_id].mode;
dd584fc0
VS
3455 I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3456 I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3457 I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3458 I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3459 I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
6156a456 3460 } else {
dd584fc0 3461 I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3462 }
3463
dd584fc0
VS
3464 I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
3465 intel_plane_ggtt_offset(plane_state) + surf_addr);
70d21f0e 3466
dd584fc0
VS
3467 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3468
3469 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
70d21f0e
DL
3470}
3471
a8d201af
ML
3472static void skylake_disable_primary_plane(struct drm_plane *primary,
3473 struct drm_crtc *crtc)
17638cd6
JB
3474{
3475 struct drm_device *dev = crtc->dev;
fac5e23e 3476 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3477 enum plane_id plane_id = to_intel_plane(primary)->id;
3478 enum pipe pipe = to_intel_plane(primary)->pipe;
dd584fc0
VS
3479 unsigned long irqflags;
3480
3481 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
62e0fb88 3482
dd584fc0
VS
3483 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
3484 I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
3485 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
3486
3487 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3488}
29b9bde6 3489
a8d201af
ML
3490/* Assume fb object is pinned & idle & fenced and just update base pointers */
3491static int
3492intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3493 int x, int y, enum mode_set_atomic state)
3494{
3495 /* Support for kgdboc is disabled, this needs a major rework. */
3496 DRM_ERROR("legacy panic handler not supported any more.\n");
3497
3498 return -ENODEV;
81255565
JB
3499}
3500
5a21b665
DV
3501static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3502{
3503 struct intel_crtc *crtc;
3504
91c8a326 3505 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
DV
3506 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3507}
3508
7514747d
VS
3509static void intel_update_primary_planes(struct drm_device *dev)
3510{
7514747d 3511 struct drm_crtc *crtc;
96a02917 3512
70e1e0ec 3513 for_each_crtc(dev, crtc) {
11c22da6 3514 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3515 struct intel_plane_state *plane_state =
3516 to_intel_plane_state(plane->base.state);
11c22da6 3517
72259536
VS
3518 if (plane_state->base.visible) {
3519 trace_intel_update_plane(&plane->base,
3520 to_intel_crtc(crtc));
3521
a8d201af
ML
3522 plane->update_plane(&plane->base,
3523 to_intel_crtc_state(crtc->state),
3524 plane_state);
72259536 3525 }
73974893
ML
3526 }
3527}
3528
3529static int
3530__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3531 struct drm_atomic_state *state,
3532 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3533{
3534 struct drm_crtc_state *crtc_state;
3535 struct drm_crtc *crtc;
3536 int i, ret;
11c22da6 3537
73974893 3538 intel_modeset_setup_hw_state(dev);
29b74b7f 3539 i915_redisable_vga(to_i915(dev));
73974893
ML
3540
3541 if (!state)
3542 return 0;
3543
aa5e9b47
ML
3544 /*
3545 * We've duplicated the state, pointers to the old state are invalid.
3546 *
3547 * Don't attempt to use the old state until we commit the duplicated state.
3548 */
3549 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3550 /*
3551 * Force recalculation even if we restore
3552 * current state. With fast modeset this may not result
3553 * in a modeset when the state is compatible.
3554 */
3555 crtc_state->mode_changed = true;
96a02917 3556 }
73974893
ML
3557
3558 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3559 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3560 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3561
581e49fe 3562 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3563
3564 WARN_ON(ret == -EDEADLK);
3565 return ret;
96a02917
VS
3566}
3567
4ac2ba2f
VS
3568static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3569{
ae98104b
VS
3570 return intel_has_gpu_reset(dev_priv) &&
3571 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3572}
3573
c033666a 3574void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3575{
73974893
ML
3576 struct drm_device *dev = &dev_priv->drm;
3577 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3578 struct drm_atomic_state *state;
3579 int ret;
3580
73974893
ML
3581 /*
3582 * Need mode_config.mutex so that we don't
3583 * trample ongoing ->detect() and whatnot.
3584 */
3585 mutex_lock(&dev->mode_config.mutex);
3586 drm_modeset_acquire_init(ctx, 0);
3587 while (1) {
3588 ret = drm_modeset_lock_all_ctx(dev, ctx);
3589 if (ret != -EDEADLK)
3590 break;
3591
3592 drm_modeset_backoff(ctx);
3593 }
3594
3595 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3596 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3597 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3598 return;
3599
f98ce92f
VS
3600 /*
3601 * Disabling the crtcs gracefully seems nicer. Also the
3602 * g33 docs say we should at least disable all the planes.
3603 */
73974893
ML
3604 state = drm_atomic_helper_duplicate_state(dev, ctx);
3605 if (IS_ERR(state)) {
3606 ret = PTR_ERR(state);
73974893 3607 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3608 return;
73974893
ML
3609 }
3610
3611 ret = drm_atomic_helper_disable_all(dev, ctx);
3612 if (ret) {
3613 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3614 drm_atomic_state_put(state);
3615 return;
73974893
ML
3616 }
3617
3618 dev_priv->modeset_restore_state = state;
3619 state->acquire_ctx = ctx;
7514747d
VS
3620}
3621
c033666a 3622void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3623{
73974893
ML
3624 struct drm_device *dev = &dev_priv->drm;
3625 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3626 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3627 int ret;
3628
5a21b665
DV
3629 /*
3630 * Flips in the rings will be nuked by the reset,
3631 * so complete all pending flips so that user space
3632 * will get its events and not get stuck.
3633 */
3634 intel_complete_page_flips(dev_priv);
3635
73974893
ML
3636 dev_priv->modeset_restore_state = NULL;
3637
7514747d 3638 /* reset doesn't touch the display */
4ac2ba2f 3639 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3640 if (!state) {
3641 /*
3642 * Flips in the rings have been nuked by the reset,
3643 * so update the base address of all primary
3644 * planes to the the last fb to make sure we're
3645 * showing the correct fb after a reset.
3646 *
3647 * FIXME: Atomic will make this obsolete since we won't schedule
3648 * CS-based flips (which might get lost in gpu resets) any more.
3649 */
3650 intel_update_primary_planes(dev);
3651 } else {
581e49fe 3652 ret = __intel_display_resume(dev, state, ctx);
522a63de
ML
3653 if (ret)
3654 DRM_ERROR("Restoring old state failed with %i\n", ret);
3655 }
73974893
ML
3656 } else {
3657 /*
3658 * The display has been reset as well,
3659 * so need a full re-initialization.
3660 */
3661 intel_runtime_pm_disable_interrupts(dev_priv);
3662 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3663
51f59205 3664 intel_pps_unlock_regs_wa(dev_priv);
73974893 3665 intel_modeset_init_hw(dev);
7514747d 3666
73974893
ML
3667 spin_lock_irq(&dev_priv->irq_lock);
3668 if (dev_priv->display.hpd_irq_setup)
3669 dev_priv->display.hpd_irq_setup(dev_priv);
3670 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3671
581e49fe 3672 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3673 if (ret)
3674 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3675
73974893
ML
3676 intel_hpd_init(dev_priv);
3677 }
7514747d 3678
0853695c
CW
3679 if (state)
3680 drm_atomic_state_put(state);
73974893
ML
3681 drm_modeset_drop_locks(ctx);
3682 drm_modeset_acquire_fini(ctx);
3683 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3684}
3685
8af29b0c
CW
3686static bool abort_flip_on_reset(struct intel_crtc *crtc)
3687{
3688 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3689
8c185eca 3690 if (i915_reset_backoff(error))
8af29b0c
CW
3691 return true;
3692
3693 if (crtc->reset_count != i915_reset_count(error))
3694 return true;
3695
3696 return false;
3697}
3698
7d5e3799
CW
3699static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3700{
5a21b665
DV
3701 struct drm_device *dev = crtc->dev;
3702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
DV
3703 bool pending;
3704
8af29b0c 3705 if (abort_flip_on_reset(intel_crtc))
5a21b665
DV
3706 return false;
3707
3708 spin_lock_irq(&dev->event_lock);
3709 pending = to_intel_crtc(crtc)->flip_work != NULL;
3710 spin_unlock_irq(&dev->event_lock);
3711
3712 return pending;
7d5e3799
CW
3713}
3714
bfd16b2a
ML
3715static void intel_update_pipe_config(struct intel_crtc *crtc,
3716 struct intel_crtc_state *old_crtc_state)
e30e8f75 3717{
6315b5d3 3718 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3719 struct intel_crtc_state *pipe_config =
3720 to_intel_crtc_state(crtc->base.state);
e30e8f75 3721
bfd16b2a
ML
3722 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3723 crtc->base.mode = crtc->base.state->mode;
3724
e30e8f75
GP
3725 /*
3726 * Update pipe size and adjust fitter if needed: the reason for this is
3727 * that in compute_mode_changes we check the native mode (not the pfit
3728 * mode) to see if we can flip rather than do a full mode set. In the
3729 * fastboot case, we'll flip, but if we don't update the pipesrc and
3730 * pfit state, we'll end up with a big fb scanned out into the wrong
3731 * sized surface.
e30e8f75
GP
3732 */
3733
e30e8f75 3734 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3735 ((pipe_config->pipe_src_w - 1) << 16) |
3736 (pipe_config->pipe_src_h - 1));
3737
3738 /* on skylake this is done by detaching scalers */
6315b5d3 3739 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3740 skl_detach_scalers(crtc);
3741
3742 if (pipe_config->pch_pfit.enabled)
3743 skylake_pfit_enable(crtc);
6e266956 3744 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3745 if (pipe_config->pch_pfit.enabled)
3746 ironlake_pfit_enable(crtc);
3747 else if (old_crtc_state->pch_pfit.enabled)
3748 ironlake_pfit_disable(crtc, true);
e30e8f75 3749 }
e30e8f75
GP
3750}
3751
4cbe4b2b 3752static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3753{
4cbe4b2b 3754 struct drm_device *dev = crtc->base.dev;
fac5e23e 3755 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3756 int pipe = crtc->pipe;
f0f59a00
VS
3757 i915_reg_t reg;
3758 u32 temp;
5e84e1a4
ZW
3759
3760 /* enable normal train */
3761 reg = FDI_TX_CTL(pipe);
3762 temp = I915_READ(reg);
fd6b8f43 3763 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3764 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3765 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3766 } else {
3767 temp &= ~FDI_LINK_TRAIN_NONE;
3768 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3769 }
5e84e1a4
ZW
3770 I915_WRITE(reg, temp);
3771
3772 reg = FDI_RX_CTL(pipe);
3773 temp = I915_READ(reg);
6e266956 3774 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3775 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3776 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3777 } else {
3778 temp &= ~FDI_LINK_TRAIN_NONE;
3779 temp |= FDI_LINK_TRAIN_NONE;
3780 }
3781 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3782
3783 /* wait one idle pattern time */
3784 POSTING_READ(reg);
3785 udelay(1000);
357555c0
JB
3786
3787 /* IVB wants error correction enabled */
fd6b8f43 3788 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3789 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3790 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3791}
3792
8db9d77b 3793/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3794static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3795 const struct intel_crtc_state *crtc_state)
8db9d77b 3796{
4cbe4b2b 3797 struct drm_device *dev = crtc->base.dev;
fac5e23e 3798 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3799 int pipe = crtc->pipe;
f0f59a00
VS
3800 i915_reg_t reg;
3801 u32 temp, tries;
8db9d77b 3802
1c8562f6 3803 /* FDI needs bits from pipe first */
0fc932b8 3804 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3805
e1a44743
AJ
3806 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3807 for train result */
5eddb70b
CW
3808 reg = FDI_RX_IMR(pipe);
3809 temp = I915_READ(reg);
e1a44743
AJ
3810 temp &= ~FDI_RX_SYMBOL_LOCK;
3811 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3812 I915_WRITE(reg, temp);
3813 I915_READ(reg);
e1a44743
AJ
3814 udelay(150);
3815
8db9d77b 3816 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3817 reg = FDI_TX_CTL(pipe);
3818 temp = I915_READ(reg);
627eb5a3 3819 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3820 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3823 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3824
5eddb70b
CW
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
8db9d77b
ZW
3827 temp &= ~FDI_LINK_TRAIN_NONE;
3828 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3829 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3830
3831 POSTING_READ(reg);
8db9d77b
ZW
3832 udelay(150);
3833
5b2adf89 3834 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3835 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3836 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3837 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3838
5eddb70b 3839 reg = FDI_RX_IIR(pipe);
e1a44743 3840 for (tries = 0; tries < 5; tries++) {
5eddb70b 3841 temp = I915_READ(reg);
8db9d77b
ZW
3842 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3843
3844 if ((temp & FDI_RX_BIT_LOCK)) {
3845 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3846 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3847 break;
3848 }
8db9d77b 3849 }
e1a44743 3850 if (tries == 5)
5eddb70b 3851 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3852
3853 /* Train 2 */
5eddb70b
CW
3854 reg = FDI_TX_CTL(pipe);
3855 temp = I915_READ(reg);
8db9d77b
ZW
3856 temp &= ~FDI_LINK_TRAIN_NONE;
3857 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3858 I915_WRITE(reg, temp);
8db9d77b 3859
5eddb70b
CW
3860 reg = FDI_RX_CTL(pipe);
3861 temp = I915_READ(reg);
8db9d77b
ZW
3862 temp &= ~FDI_LINK_TRAIN_NONE;
3863 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3864 I915_WRITE(reg, temp);
8db9d77b 3865
5eddb70b
CW
3866 POSTING_READ(reg);
3867 udelay(150);
8db9d77b 3868
5eddb70b 3869 reg = FDI_RX_IIR(pipe);
e1a44743 3870 for (tries = 0; tries < 5; tries++) {
5eddb70b 3871 temp = I915_READ(reg);
8db9d77b
ZW
3872 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3873
3874 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3875 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3876 DRM_DEBUG_KMS("FDI train 2 done.\n");
3877 break;
3878 }
8db9d77b 3879 }
e1a44743 3880 if (tries == 5)
5eddb70b 3881 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3882
3883 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3884
8db9d77b
ZW
3885}
3886
0206e353 3887static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3888 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3889 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3890 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3891 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3892};
3893
3894/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3895static void gen6_fdi_link_train(struct intel_crtc *crtc,
3896 const struct intel_crtc_state *crtc_state)
8db9d77b 3897{
4cbe4b2b 3898 struct drm_device *dev = crtc->base.dev;
fac5e23e 3899 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3900 int pipe = crtc->pipe;
f0f59a00
VS
3901 i915_reg_t reg;
3902 u32 temp, i, retry;
8db9d77b 3903
e1a44743
AJ
3904 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3905 for train result */
5eddb70b
CW
3906 reg = FDI_RX_IMR(pipe);
3907 temp = I915_READ(reg);
e1a44743
AJ
3908 temp &= ~FDI_RX_SYMBOL_LOCK;
3909 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3910 I915_WRITE(reg, temp);
3911
3912 POSTING_READ(reg);
e1a44743
AJ
3913 udelay(150);
3914
8db9d77b 3915 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3916 reg = FDI_TX_CTL(pipe);
3917 temp = I915_READ(reg);
627eb5a3 3918 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3919 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3920 temp &= ~FDI_LINK_TRAIN_NONE;
3921 temp |= FDI_LINK_TRAIN_PATTERN_1;
3922 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3923 /* SNB-B */
3924 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3925 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3926
d74cf324
DV
3927 I915_WRITE(FDI_RX_MISC(pipe),
3928 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3929
5eddb70b
CW
3930 reg = FDI_RX_CTL(pipe);
3931 temp = I915_READ(reg);
6e266956 3932 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3933 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3934 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3935 } else {
3936 temp &= ~FDI_LINK_TRAIN_NONE;
3937 temp |= FDI_LINK_TRAIN_PATTERN_1;
3938 }
5eddb70b
CW
3939 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3940
3941 POSTING_READ(reg);
8db9d77b
ZW
3942 udelay(150);
3943
0206e353 3944 for (i = 0; i < 4; i++) {
5eddb70b
CW
3945 reg = FDI_TX_CTL(pipe);
3946 temp = I915_READ(reg);
8db9d77b
ZW
3947 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3948 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3949 I915_WRITE(reg, temp);
3950
3951 POSTING_READ(reg);
8db9d77b
ZW
3952 udelay(500);
3953
fa37d39e
SP
3954 for (retry = 0; retry < 5; retry++) {
3955 reg = FDI_RX_IIR(pipe);
3956 temp = I915_READ(reg);
3957 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3958 if (temp & FDI_RX_BIT_LOCK) {
3959 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3960 DRM_DEBUG_KMS("FDI train 1 done.\n");
3961 break;
3962 }
3963 udelay(50);
8db9d77b 3964 }
fa37d39e
SP
3965 if (retry < 5)
3966 break;
8db9d77b
ZW
3967 }
3968 if (i == 4)
5eddb70b 3969 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3970
3971 /* Train 2 */
5eddb70b
CW
3972 reg = FDI_TX_CTL(pipe);
3973 temp = I915_READ(reg);
8db9d77b
ZW
3974 temp &= ~FDI_LINK_TRAIN_NONE;
3975 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3976 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3977 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3978 /* SNB-B */
3979 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3980 }
5eddb70b 3981 I915_WRITE(reg, temp);
8db9d77b 3982
5eddb70b
CW
3983 reg = FDI_RX_CTL(pipe);
3984 temp = I915_READ(reg);
6e266956 3985 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3986 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3987 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3988 } else {
3989 temp &= ~FDI_LINK_TRAIN_NONE;
3990 temp |= FDI_LINK_TRAIN_PATTERN_2;
3991 }
5eddb70b
CW
3992 I915_WRITE(reg, temp);
3993
3994 POSTING_READ(reg);
8db9d77b
ZW
3995 udelay(150);
3996
0206e353 3997 for (i = 0; i < 4; i++) {
5eddb70b
CW
3998 reg = FDI_TX_CTL(pipe);
3999 temp = I915_READ(reg);
8db9d77b
ZW
4000 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4001 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
4002 I915_WRITE(reg, temp);
4003
4004 POSTING_READ(reg);
8db9d77b
ZW
4005 udelay(500);
4006
fa37d39e
SP
4007 for (retry = 0; retry < 5; retry++) {
4008 reg = FDI_RX_IIR(pipe);
4009 temp = I915_READ(reg);
4010 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4011 if (temp & FDI_RX_SYMBOL_LOCK) {
4012 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4013 DRM_DEBUG_KMS("FDI train 2 done.\n");
4014 break;
4015 }
4016 udelay(50);
8db9d77b 4017 }
fa37d39e
SP
4018 if (retry < 5)
4019 break;
8db9d77b
ZW
4020 }
4021 if (i == 4)
5eddb70b 4022 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4023
4024 DRM_DEBUG_KMS("FDI train done.\n");
4025}
4026
357555c0 4027/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
4028static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4029 const struct intel_crtc_state *crtc_state)
357555c0 4030{
4cbe4b2b 4031 struct drm_device *dev = crtc->base.dev;
fac5e23e 4032 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4033 int pipe = crtc->pipe;
f0f59a00
VS
4034 i915_reg_t reg;
4035 u32 temp, i, j;
357555c0
JB
4036
4037 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4038 for train result */
4039 reg = FDI_RX_IMR(pipe);
4040 temp = I915_READ(reg);
4041 temp &= ~FDI_RX_SYMBOL_LOCK;
4042 temp &= ~FDI_RX_BIT_LOCK;
4043 I915_WRITE(reg, temp);
4044
4045 POSTING_READ(reg);
4046 udelay(150);
4047
01a415fd
DV
4048 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4049 I915_READ(FDI_RX_IIR(pipe)));
4050
139ccd3f
JB
4051 /* Try each vswing and preemphasis setting twice before moving on */
4052 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4053 /* disable first in case we need to retry */
4054 reg = FDI_TX_CTL(pipe);
4055 temp = I915_READ(reg);
4056 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4057 temp &= ~FDI_TX_ENABLE;
4058 I915_WRITE(reg, temp);
357555c0 4059
139ccd3f
JB
4060 reg = FDI_RX_CTL(pipe);
4061 temp = I915_READ(reg);
4062 temp &= ~FDI_LINK_TRAIN_AUTO;
4063 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4064 temp &= ~FDI_RX_ENABLE;
4065 I915_WRITE(reg, temp);
357555c0 4066
139ccd3f 4067 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4068 reg = FDI_TX_CTL(pipe);
4069 temp = I915_READ(reg);
139ccd3f 4070 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4071 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 4072 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4073 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4074 temp |= snb_b_fdi_train_param[j/2];
4075 temp |= FDI_COMPOSITE_SYNC;
4076 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4077
139ccd3f
JB
4078 I915_WRITE(FDI_RX_MISC(pipe),
4079 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4080
139ccd3f 4081 reg = FDI_RX_CTL(pipe);
357555c0 4082 temp = I915_READ(reg);
139ccd3f
JB
4083 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4084 temp |= FDI_COMPOSITE_SYNC;
4085 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4086
139ccd3f
JB
4087 POSTING_READ(reg);
4088 udelay(1); /* should be 0.5us */
357555c0 4089
139ccd3f
JB
4090 for (i = 0; i < 4; i++) {
4091 reg = FDI_RX_IIR(pipe);
4092 temp = I915_READ(reg);
4093 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4094
139ccd3f
JB
4095 if (temp & FDI_RX_BIT_LOCK ||
4096 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4097 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4098 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4099 i);
4100 break;
4101 }
4102 udelay(1); /* should be 0.5us */
4103 }
4104 if (i == 4) {
4105 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4106 continue;
4107 }
357555c0 4108
139ccd3f 4109 /* Train 2 */
357555c0
JB
4110 reg = FDI_TX_CTL(pipe);
4111 temp = I915_READ(reg);
139ccd3f
JB
4112 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4113 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4114 I915_WRITE(reg, temp);
4115
4116 reg = FDI_RX_CTL(pipe);
4117 temp = I915_READ(reg);
4118 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4119 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4120 I915_WRITE(reg, temp);
4121
4122 POSTING_READ(reg);
139ccd3f 4123 udelay(2); /* should be 1.5us */
357555c0 4124
139ccd3f
JB
4125 for (i = 0; i < 4; i++) {
4126 reg = FDI_RX_IIR(pipe);
4127 temp = I915_READ(reg);
4128 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4129
139ccd3f
JB
4130 if (temp & FDI_RX_SYMBOL_LOCK ||
4131 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4132 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4133 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4134 i);
4135 goto train_done;
4136 }
4137 udelay(2); /* should be 1.5us */
357555c0 4138 }
139ccd3f
JB
4139 if (i == 4)
4140 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4141 }
357555c0 4142
139ccd3f 4143train_done:
357555c0
JB
4144 DRM_DEBUG_KMS("FDI train done.\n");
4145}
4146
88cefb6c 4147static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4148{
88cefb6c 4149 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4150 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4151 int pipe = intel_crtc->pipe;
f0f59a00
VS
4152 i915_reg_t reg;
4153 u32 temp;
c64e311e 4154
c98e9dcf 4155 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4156 reg = FDI_RX_CTL(pipe);
4157 temp = I915_READ(reg);
627eb5a3 4158 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4159 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4160 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4161 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4162
4163 POSTING_READ(reg);
c98e9dcf
JB
4164 udelay(200);
4165
4166 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4167 temp = I915_READ(reg);
4168 I915_WRITE(reg, temp | FDI_PCDCLK);
4169
4170 POSTING_READ(reg);
c98e9dcf
JB
4171 udelay(200);
4172
20749730
PZ
4173 /* Enable CPU FDI TX PLL, always on for Ironlake */
4174 reg = FDI_TX_CTL(pipe);
4175 temp = I915_READ(reg);
4176 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4177 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4178
20749730
PZ
4179 POSTING_READ(reg);
4180 udelay(100);
6be4a607 4181 }
0e23b99d
JB
4182}
4183
88cefb6c
DV
4184static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4185{
4186 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4187 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4188 int pipe = intel_crtc->pipe;
f0f59a00
VS
4189 i915_reg_t reg;
4190 u32 temp;
88cefb6c
DV
4191
4192 /* Switch from PCDclk to Rawclk */
4193 reg = FDI_RX_CTL(pipe);
4194 temp = I915_READ(reg);
4195 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4196
4197 /* Disable CPU FDI TX PLL */
4198 reg = FDI_TX_CTL(pipe);
4199 temp = I915_READ(reg);
4200 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4201
4202 POSTING_READ(reg);
4203 udelay(100);
4204
4205 reg = FDI_RX_CTL(pipe);
4206 temp = I915_READ(reg);
4207 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4208
4209 /* Wait for the clocks to turn off. */
4210 POSTING_READ(reg);
4211 udelay(100);
4212}
4213
0fc932b8
JB
4214static void ironlake_fdi_disable(struct drm_crtc *crtc)
4215{
4216 struct drm_device *dev = crtc->dev;
fac5e23e 4217 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4219 int pipe = intel_crtc->pipe;
f0f59a00
VS
4220 i915_reg_t reg;
4221 u32 temp;
0fc932b8
JB
4222
4223 /* disable CPU FDI tx and PCH FDI rx */
4224 reg = FDI_TX_CTL(pipe);
4225 temp = I915_READ(reg);
4226 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4227 POSTING_READ(reg);
4228
4229 reg = FDI_RX_CTL(pipe);
4230 temp = I915_READ(reg);
4231 temp &= ~(0x7 << 16);
dfd07d72 4232 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4233 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4234
4235 POSTING_READ(reg);
4236 udelay(100);
4237
4238 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4239 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4240 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4241
4242 /* still set train pattern 1 */
4243 reg = FDI_TX_CTL(pipe);
4244 temp = I915_READ(reg);
4245 temp &= ~FDI_LINK_TRAIN_NONE;
4246 temp |= FDI_LINK_TRAIN_PATTERN_1;
4247 I915_WRITE(reg, temp);
4248
4249 reg = FDI_RX_CTL(pipe);
4250 temp = I915_READ(reg);
6e266956 4251 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4252 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4253 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4254 } else {
4255 temp &= ~FDI_LINK_TRAIN_NONE;
4256 temp |= FDI_LINK_TRAIN_PATTERN_1;
4257 }
4258 /* BPC in FDI rx is consistent with that in PIPECONF */
4259 temp &= ~(0x07 << 16);
dfd07d72 4260 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4261 I915_WRITE(reg, temp);
4262
4263 POSTING_READ(reg);
4264 udelay(100);
4265}
4266
49d73912 4267bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4268{
4269 struct intel_crtc *crtc;
4270
4271 /* Note that we don't need to be called with mode_config.lock here
4272 * as our list of CRTC objects is static for the lifetime of the
4273 * device and so cannot disappear as we iterate. Similarly, we can
4274 * happily treat the predicates as racy, atomic checks as userspace
4275 * cannot claim and pin a new fb without at least acquring the
4276 * struct_mutex and so serialising with us.
4277 */
49d73912 4278 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4279 if (atomic_read(&crtc->unpin_work_count) == 0)
4280 continue;
4281
5a21b665 4282 if (crtc->flip_work)
0f0f74bc 4283 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4284
4285 return true;
4286 }
4287
4288 return false;
4289}
4290
5a21b665 4291static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4292{
4293 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
DV
4294 struct intel_flip_work *work = intel_crtc->flip_work;
4295
4296 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4297
4298 if (work->event)
560ce1dc 4299 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4300
4301 drm_crtc_vblank_put(&intel_crtc->base);
4302
5a21b665 4303 wake_up_all(&dev_priv->pending_flip_queue);
5a21b665
DV
4304 trace_i915_flip_complete(intel_crtc->plane,
4305 work->pending_flip_obj);
05c41f92
AR
4306
4307 queue_work(dev_priv->wq, &work->unpin_work);
d6bbafa1
CW
4308}
4309
5008e874 4310static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4311{
0f91128d 4312 struct drm_device *dev = crtc->dev;
fac5e23e 4313 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4314 long ret;
e6c3a2a6 4315
2c10d571 4316 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4317
4318 ret = wait_event_interruptible_timeout(
4319 dev_priv->pending_flip_queue,
4320 !intel_crtc_has_pending_flip(crtc),
4321 60*HZ);
4322
4323 if (ret < 0)
4324 return ret;
4325
5a21b665
DV
4326 if (ret == 0) {
4327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4328 struct intel_flip_work *work;
4329
4330 spin_lock_irq(&dev->event_lock);
4331 work = intel_crtc->flip_work;
4332 if (work && !is_mmio_work(work)) {
4333 WARN_ONCE(1, "Removing stuck page flip\n");
4334 page_flip_completed(intel_crtc);
4335 }
4336 spin_unlock_irq(&dev->event_lock);
4337 }
5bb61643 4338
5008e874 4339 return 0;
e6c3a2a6
CW
4340}
4341
b7076546 4342void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4343{
4344 u32 temp;
4345
4346 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4347
4348 mutex_lock(&dev_priv->sb_lock);
4349
4350 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4351 temp |= SBI_SSCCTL_DISABLE;
4352 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4353
4354 mutex_unlock(&dev_priv->sb_lock);
4355}
4356
e615efe4 4357/* Program iCLKIP clock to the desired frequency */
0dcdc382 4358static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4359{
0dcdc382
ACO
4360 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4361 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4362 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4363 u32 temp;
4364
060f02d8 4365 lpt_disable_iclkip(dev_priv);
e615efe4 4366
64b46a06
VS
4367 /* The iCLK virtual clock root frequency is in MHz,
4368 * but the adjusted_mode->crtc_clock in in KHz. To get the
4369 * divisors, it is necessary to divide one by another, so we
4370 * convert the virtual clock precision to KHz here for higher
4371 * precision.
4372 */
4373 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4374 u32 iclk_virtual_root_freq = 172800 * 1000;
4375 u32 iclk_pi_range = 64;
64b46a06 4376 u32 desired_divisor;
e615efe4 4377
64b46a06
VS
4378 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4379 clock << auxdiv);
4380 divsel = (desired_divisor / iclk_pi_range) - 2;
4381 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4382
64b46a06
VS
4383 /*
4384 * Near 20MHz is a corner case which is
4385 * out of range for the 7-bit divisor
4386 */
4387 if (divsel <= 0x7f)
4388 break;
e615efe4
ED
4389 }
4390
4391 /* This should not happen with any sane values */
4392 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4393 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4394 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4395 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4396
4397 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4398 clock,
e615efe4
ED
4399 auxdiv,
4400 divsel,
4401 phasedir,
4402 phaseinc);
4403
060f02d8
VS
4404 mutex_lock(&dev_priv->sb_lock);
4405
e615efe4 4406 /* Program SSCDIVINTPHASE6 */
988d6ee8 4407 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4408 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4409 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4410 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4411 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4412 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4413 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4414 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4415
4416 /* Program SSCAUXDIV */
988d6ee8 4417 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4418 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4419 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4420 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4421
4422 /* Enable modulator and associated divider */
988d6ee8 4423 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4424 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4425 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4426
060f02d8
VS
4427 mutex_unlock(&dev_priv->sb_lock);
4428
e615efe4
ED
4429 /* Wait for initialization time */
4430 udelay(24);
4431
4432 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4433}
4434
8802e5b6
VS
4435int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4436{
4437 u32 divsel, phaseinc, auxdiv;
4438 u32 iclk_virtual_root_freq = 172800 * 1000;
4439 u32 iclk_pi_range = 64;
4440 u32 desired_divisor;
4441 u32 temp;
4442
4443 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4444 return 0;
4445
4446 mutex_lock(&dev_priv->sb_lock);
4447
4448 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4449 if (temp & SBI_SSCCTL_DISABLE) {
4450 mutex_unlock(&dev_priv->sb_lock);
4451 return 0;
4452 }
4453
4454 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4455 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4456 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4457 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4458 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4459
4460 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4461 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4462 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4463
4464 mutex_unlock(&dev_priv->sb_lock);
4465
4466 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4467
4468 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4469 desired_divisor << auxdiv);
4470}
4471
275f01b2
DV
4472static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4473 enum pipe pch_transcoder)
4474{
4475 struct drm_device *dev = crtc->base.dev;
fac5e23e 4476 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4477 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4478
4479 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4480 I915_READ(HTOTAL(cpu_transcoder)));
4481 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4482 I915_READ(HBLANK(cpu_transcoder)));
4483 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4484 I915_READ(HSYNC(cpu_transcoder)));
4485
4486 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4487 I915_READ(VTOTAL(cpu_transcoder)));
4488 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4489 I915_READ(VBLANK(cpu_transcoder)));
4490 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4491 I915_READ(VSYNC(cpu_transcoder)));
4492 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4493 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4494}
4495
003632d9 4496static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4497{
fac5e23e 4498 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4499 uint32_t temp;
4500
4501 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4502 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4503 return;
4504
4505 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4506 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4507
003632d9
ACO
4508 temp &= ~FDI_BC_BIFURCATION_SELECT;
4509 if (enable)
4510 temp |= FDI_BC_BIFURCATION_SELECT;
4511
4512 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4513 I915_WRITE(SOUTH_CHICKEN1, temp);
4514 POSTING_READ(SOUTH_CHICKEN1);
4515}
4516
4517static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4518{
4519 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4520
4521 switch (intel_crtc->pipe) {
4522 case PIPE_A:
4523 break;
4524 case PIPE_B:
6e3c9717 4525 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4526 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4527 else
003632d9 4528 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4529
4530 break;
4531 case PIPE_C:
003632d9 4532 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4533
4534 break;
4535 default:
4536 BUG();
4537 }
4538}
4539
c48b5305
VS
4540/* Return which DP Port should be selected for Transcoder DP control */
4541static enum port
4cbe4b2b 4542intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4543{
4cbe4b2b 4544 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4545 struct intel_encoder *encoder;
4546
4cbe4b2b 4547 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4548 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4549 encoder->type == INTEL_OUTPUT_EDP)
4550 return enc_to_dig_port(&encoder->base)->port;
4551 }
4552
4553 return -1;
4554}
4555
f67a559d
JB
4556/*
4557 * Enable PCH resources required for PCH ports:
4558 * - PCH PLLs
4559 * - FDI training & RX/TX
4560 * - update transcoder timings
4561 * - DP transcoding bits
4562 * - transcoder
4563 */
2ce42273 4564static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4565{
2ce42273 4566 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4567 struct drm_device *dev = crtc->base.dev;
fac5e23e 4568 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4569 int pipe = crtc->pipe;
f0f59a00 4570 u32 temp;
2c07245f 4571
ab9412ba 4572 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4573
fd6b8f43 4574 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4575 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4576
cd986abb
DV
4577 /* Write the TU size bits before fdi link training, so that error
4578 * detection works. */
4579 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4580 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4581
c98e9dcf 4582 /* For PCH output, training FDI link */
dc4a1094 4583 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4584
3ad8a208
DV
4585 /* We need to program the right clock selection before writing the pixel
4586 * mutliplier into the DPLL. */
6e266956 4587 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4588 u32 sel;
4b645f14 4589
c98e9dcf 4590 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4591 temp |= TRANS_DPLL_ENABLE(pipe);
4592 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4593 if (crtc_state->shared_dpll ==
8106ddbd 4594 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4595 temp |= sel;
4596 else
4597 temp &= ~sel;
c98e9dcf 4598 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4599 }
5eddb70b 4600
3ad8a208
DV
4601 /* XXX: pch pll's can be enabled any time before we enable the PCH
4602 * transcoder, and we actually should do this to not upset any PCH
4603 * transcoder that already use the clock when we share it.
4604 *
4605 * Note that enable_shared_dpll tries to do the right thing, but
4606 * get_shared_dpll unconditionally resets the pll - we need that to have
4607 * the right LVDS enable sequence. */
4cbe4b2b 4608 intel_enable_shared_dpll(crtc);
3ad8a208 4609
d9b6cb56
JB
4610 /* set transcoder timing, panel must allow it */
4611 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4612 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4613
303b81e0 4614 intel_fdi_normal_train(crtc);
5e84e1a4 4615
c98e9dcf 4616 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4617 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4618 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4619 const struct drm_display_mode *adjusted_mode =
2ce42273 4620 &crtc_state->base.adjusted_mode;
dfd07d72 4621 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4622 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4623 temp = I915_READ(reg);
4624 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4625 TRANS_DP_SYNC_MASK |
4626 TRANS_DP_BPC_MASK);
e3ef4479 4627 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4628 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4629
9c4edaee 4630 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4631 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4632 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4633 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4634
4635 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4636 case PORT_B:
5eddb70b 4637 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4638 break;
c48b5305 4639 case PORT_C:
5eddb70b 4640 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4641 break;
c48b5305 4642 case PORT_D:
5eddb70b 4643 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4644 break;
4645 default:
e95d41e1 4646 BUG();
32f9d658 4647 }
2c07245f 4648
5eddb70b 4649 I915_WRITE(reg, temp);
6be4a607 4650 }
b52eb4dc 4651
b8a4f404 4652 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4653}
4654
2ce42273 4655static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4656{
2ce42273 4657 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4658 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4659 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4660
ab9412ba 4661 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4662
8c52b5e8 4663 lpt_program_iclkip(crtc);
1507e5bd 4664
0540e488 4665 /* Set transcoder timing. */
0dcdc382 4666 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4667
937bb610 4668 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4669}
4670
a1520318 4671static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4672{
fac5e23e 4673 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4674 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4675 u32 temp;
4676
4677 temp = I915_READ(dslreg);
4678 udelay(500);
4679 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4680 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4681 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4682 }
4683}
4684
86adf9d7
ML
4685static int
4686skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4687 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4688 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4689{
86adf9d7
ML
4690 struct intel_crtc_scaler_state *scaler_state =
4691 &crtc_state->scaler_state;
4692 struct intel_crtc *intel_crtc =
4693 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4694 int need_scaling;
6156a456 4695
bd2ef25d 4696 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4697 (src_h != dst_w || src_w != dst_h):
4698 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4699
4700 /*
4701 * if plane is being disabled or scaler is no more required or force detach
4702 * - free scaler binded to this plane/crtc
4703 * - in order to do this, update crtc->scaler_usage
4704 *
4705 * Here scaler state in crtc_state is set free so that
4706 * scaler can be assigned to other user. Actual register
4707 * update to free the scaler is done in plane/panel-fit programming.
4708 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4709 */
86adf9d7 4710 if (force_detach || !need_scaling) {
a1b2278e 4711 if (*scaler_id >= 0) {
86adf9d7 4712 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4713 scaler_state->scalers[*scaler_id].in_use = 0;
4714
86adf9d7
ML
4715 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4716 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4717 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4718 scaler_state->scaler_users);
4719 *scaler_id = -1;
4720 }
4721 return 0;
4722 }
4723
4724 /* range checks */
4725 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4726 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4727
4728 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4729 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4730 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4731 "size is out of scaler range\n",
86adf9d7 4732 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4733 return -EINVAL;
4734 }
4735
86adf9d7
ML
4736 /* mark this plane as a scaler user in crtc_state */
4737 scaler_state->scaler_users |= (1 << scaler_user);
4738 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4739 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4740 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4741 scaler_state->scaler_users);
4742
4743 return 0;
4744}
4745
4746/**
4747 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4748 *
4749 * @state: crtc's scaler state
86adf9d7
ML
4750 *
4751 * Return
4752 * 0 - scaler_usage updated successfully
4753 * error - requested scaling cannot be supported or other error condition
4754 */
e435d6e5 4755int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4756{
7c5f93b0 4757 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4758
e435d6e5 4759 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4760 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4761 state->pipe_src_w, state->pipe_src_h,
aad941d5 4762 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4763}
4764
4765/**
4766 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4767 *
4768 * @state: crtc's scaler state
86adf9d7
ML
4769 * @plane_state: atomic plane state to update
4770 *
4771 * Return
4772 * 0 - scaler_usage updated successfully
4773 * error - requested scaling cannot be supported or other error condition
4774 */
da20eabd
ML
4775static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4776 struct intel_plane_state *plane_state)
86adf9d7
ML
4777{
4778
da20eabd
ML
4779 struct intel_plane *intel_plane =
4780 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4781 struct drm_framebuffer *fb = plane_state->base.fb;
4782 int ret;
4783
936e71e3 4784 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4785
86adf9d7
ML
4786 ret = skl_update_scaler(crtc_state, force_detach,
4787 drm_plane_index(&intel_plane->base),
4788 &plane_state->scaler_id,
4789 plane_state->base.rotation,
936e71e3
VS
4790 drm_rect_width(&plane_state->base.src) >> 16,
4791 drm_rect_height(&plane_state->base.src) >> 16,
4792 drm_rect_width(&plane_state->base.dst),
4793 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4794
4795 if (ret || plane_state->scaler_id < 0)
4796 return ret;
4797
a1b2278e 4798 /* check colorkey */
818ed961 4799 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4800 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4801 intel_plane->base.base.id,
4802 intel_plane->base.name);
a1b2278e
CK
4803 return -EINVAL;
4804 }
4805
4806 /* Check src format */
438b74a5 4807 switch (fb->format->format) {
86adf9d7
ML
4808 case DRM_FORMAT_RGB565:
4809 case DRM_FORMAT_XBGR8888:
4810 case DRM_FORMAT_XRGB8888:
4811 case DRM_FORMAT_ABGR8888:
4812 case DRM_FORMAT_ARGB8888:
4813 case DRM_FORMAT_XRGB2101010:
4814 case DRM_FORMAT_XBGR2101010:
4815 case DRM_FORMAT_YUYV:
4816 case DRM_FORMAT_YVYU:
4817 case DRM_FORMAT_UYVY:
4818 case DRM_FORMAT_VYUY:
4819 break;
4820 default:
72660ce0
VS
4821 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4822 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4823 fb->base.id, fb->format->format);
86adf9d7 4824 return -EINVAL;
a1b2278e
CK
4825 }
4826
a1b2278e
CK
4827 return 0;
4828}
4829
e435d6e5
ML
4830static void skylake_scaler_disable(struct intel_crtc *crtc)
4831{
4832 int i;
4833
4834 for (i = 0; i < crtc->num_scalers; i++)
4835 skl_detach_scaler(crtc, i);
4836}
4837
4838static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4839{
4840 struct drm_device *dev = crtc->base.dev;
fac5e23e 4841 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4842 int pipe = crtc->pipe;
a1b2278e
CK
4843 struct intel_crtc_scaler_state *scaler_state =
4844 &crtc->config->scaler_state;
4845
6e3c9717 4846 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4847 int id;
4848
c3f8ad57 4849 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 4850 return;
a1b2278e
CK
4851
4852 id = scaler_state->scaler_id;
4853 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4854 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4855 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4856 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
4857 }
4858}
4859
b074cec8
JB
4860static void ironlake_pfit_enable(struct intel_crtc *crtc)
4861{
4862 struct drm_device *dev = crtc->base.dev;
fac5e23e 4863 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4864 int pipe = crtc->pipe;
4865
6e3c9717 4866 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4867 /* Force use of hard-coded filter coefficients
4868 * as some pre-programmed values are broken,
4869 * e.g. x201.
4870 */
fd6b8f43 4871 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4872 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4873 PF_PIPE_SEL_IVB(pipe));
4874 else
4875 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4876 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4877 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4878 }
4879}
4880
20bc8673 4881void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4882{
cea165c3 4883 struct drm_device *dev = crtc->base.dev;
fac5e23e 4884 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4885
6e3c9717 4886 if (!crtc->config->ips_enabled)
d77e4531
PZ
4887 return;
4888
307e4498
ML
4889 /*
4890 * We can only enable IPS after we enable a plane and wait for a vblank
4891 * This function is called from post_plane_update, which is run after
4892 * a vblank wait.
4893 */
cea165c3 4894
d77e4531 4895 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4896 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4897 mutex_lock(&dev_priv->rps.hw_lock);
4898 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4899 mutex_unlock(&dev_priv->rps.hw_lock);
4900 /* Quoting Art Runyan: "its not safe to expect any particular
4901 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4902 * mailbox." Moreover, the mailbox may return a bogus state,
4903 * so we need to just enable it and continue on.
2a114cc1
BW
4904 */
4905 } else {
4906 I915_WRITE(IPS_CTL, IPS_ENABLE);
4907 /* The bit only becomes 1 in the next vblank, so this wait here
4908 * is essentially intel_wait_for_vblank. If we don't have this
4909 * and don't wait for vblanks until the end of crtc_enable, then
4910 * the HW state readout code will complain that the expected
4911 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4912 if (intel_wait_for_register(dev_priv,
4913 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4914 50))
2a114cc1
BW
4915 DRM_ERROR("Timed out waiting for IPS enable\n");
4916 }
d77e4531
PZ
4917}
4918
20bc8673 4919void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4920{
4921 struct drm_device *dev = crtc->base.dev;
fac5e23e 4922 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4923
6e3c9717 4924 if (!crtc->config->ips_enabled)
d77e4531
PZ
4925 return;
4926
4927 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4928 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4929 mutex_lock(&dev_priv->rps.hw_lock);
4930 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4931 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4932 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4933 if (intel_wait_for_register(dev_priv,
4934 IPS_CTL, IPS_ENABLE, 0,
4935 42))
23d0b130 4936 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4937 } else {
2a114cc1 4938 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4939 POSTING_READ(IPS_CTL);
4940 }
d77e4531
PZ
4941
4942 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4943 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4944}
4945
7cac945f 4946static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4947{
7cac945f 4948 if (intel_crtc->overlay) {
d3eedb1a 4949 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4950 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4951
4952 mutex_lock(&dev->struct_mutex);
4953 dev_priv->mm.interruptible = false;
4954 (void) intel_overlay_switch_off(intel_crtc->overlay);
4955 dev_priv->mm.interruptible = true;
4956 mutex_unlock(&dev->struct_mutex);
4957 }
4958
4959 /* Let userspace switch the overlay on again. In most cases userspace
4960 * has to recompute where to put it anyway.
4961 */
4962}
4963
87d4300a
ML
4964/**
4965 * intel_post_enable_primary - Perform operations after enabling primary plane
4966 * @crtc: the CRTC whose primary plane was just enabled
4967 *
4968 * Performs potentially sleeping operations that must be done after the primary
4969 * plane is enabled, such as updating FBC and IPS. Note that this may be
4970 * called due to an explicit primary plane update, or due to an implicit
4971 * re-enable that is caused when a sprite plane is updated to no longer
4972 * completely hide the primary plane.
4973 */
4974static void
4975intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4976{
4977 struct drm_device *dev = crtc->dev;
fac5e23e 4978 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4980 int pipe = intel_crtc->pipe;
a5c4d7bc 4981
87d4300a
ML
4982 /*
4983 * FIXME IPS should be fine as long as one plane is
4984 * enabled, but in practice it seems to have problems
4985 * when going from primary only to sprite only and vice
4986 * versa.
4987 */
a5c4d7bc
VS
4988 hsw_enable_ips(intel_crtc);
4989
f99d7069 4990 /*
87d4300a
ML
4991 * Gen2 reports pipe underruns whenever all planes are disabled.
4992 * So don't enable underrun reporting before at least some planes
4993 * are enabled.
4994 * FIXME: Need to fix the logic to work when we turn off all planes
4995 * but leave the pipe running.
f99d7069 4996 */
5db94019 4997 if (IS_GEN2(dev_priv))
87d4300a
ML
4998 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4999
aca7b684
VS
5000 /* Underruns don't always raise interrupts, so check manually. */
5001 intel_check_cpu_fifo_underruns(dev_priv);
5002 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
5003}
5004
2622a081 5005/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
5006static void
5007intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
5008{
5009 struct drm_device *dev = crtc->dev;
fac5e23e 5010 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
5011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5012 int pipe = intel_crtc->pipe;
a5c4d7bc 5013
87d4300a
ML
5014 /*
5015 * Gen2 reports pipe underruns whenever all planes are disabled.
5016 * So diasble underrun reporting before all the planes get disabled.
5017 * FIXME: Need to fix the logic to work when we turn off all planes
5018 * but leave the pipe running.
5019 */
5db94019 5020 if (IS_GEN2(dev_priv))
87d4300a 5021 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 5022
2622a081
VS
5023 /*
5024 * FIXME IPS should be fine as long as one plane is
5025 * enabled, but in practice it seems to have problems
5026 * when going from primary only to sprite only and vice
5027 * versa.
5028 */
5029 hsw_disable_ips(intel_crtc);
5030}
5031
5032/* FIXME get rid of this and use pre_plane_update */
5033static void
5034intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5035{
5036 struct drm_device *dev = crtc->dev;
fac5e23e 5037 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5039 int pipe = intel_crtc->pipe;
5040
5041 intel_pre_disable_primary(crtc);
5042
87d4300a
ML
5043 /*
5044 * Vblank time updates from the shadow to live plane control register
5045 * are blocked if the memory self-refresh mode is active at that
5046 * moment. So to make sure the plane gets truly disabled, disable
5047 * first the self-refresh mode. The self-refresh enable bit in turn
5048 * will be checked/applied by the HW only at the next frame start
5049 * event which is after the vblank start event, so we need to have a
5050 * wait-for-vblank between disabling the plane and the pipe.
5051 */
11a85d6a
VS
5052 if (HAS_GMCH_DISPLAY(dev_priv) &&
5053 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5054 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
5055}
5056
5a21b665
DV
5057static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5058{
5059 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5060 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5061 struct intel_crtc_state *pipe_config =
5062 to_intel_crtc_state(crtc->base.state);
5a21b665
DV
5063 struct drm_plane *primary = crtc->base.primary;
5064 struct drm_plane_state *old_pri_state =
5065 drm_atomic_get_existing_plane_state(old_state, primary);
5066
5748b6a1 5067 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 5068
5a21b665 5069 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5070 intel_update_watermarks(crtc);
5a21b665
DV
5071
5072 if (old_pri_state) {
5073 struct intel_plane_state *primary_state =
5074 to_intel_plane_state(primary->state);
5075 struct intel_plane_state *old_primary_state =
5076 to_intel_plane_state(old_pri_state);
5077
5078 intel_fbc_post_update(crtc);
5079
936e71e3 5080 if (primary_state->base.visible &&
5a21b665 5081 (needs_modeset(&pipe_config->base) ||
936e71e3 5082 !old_primary_state->base.visible))
5a21b665
DV
5083 intel_post_enable_primary(&crtc->base);
5084 }
5085}
5086
aa5e9b47
ML
5087static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5088 struct intel_crtc_state *pipe_config)
ac21b225 5089{
5c74cd73 5090 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5091 struct drm_device *dev = crtc->base.dev;
fac5e23e 5092 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5093 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5094 struct drm_plane *primary = crtc->base.primary;
5095 struct drm_plane_state *old_pri_state =
5096 drm_atomic_get_existing_plane_state(old_state, primary);
5097 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5098 struct intel_atomic_state *old_intel_state =
5099 to_intel_atomic_state(old_state);
ac21b225 5100
5c74cd73
ML
5101 if (old_pri_state) {
5102 struct intel_plane_state *primary_state =
5103 to_intel_plane_state(primary->state);
5104 struct intel_plane_state *old_primary_state =
5105 to_intel_plane_state(old_pri_state);
5106
faf68d92 5107 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5108
936e71e3
VS
5109 if (old_primary_state->base.visible &&
5110 (modeset || !primary_state->base.visible))
5c74cd73
ML
5111 intel_pre_disable_primary(&crtc->base);
5112 }
852eb00d 5113
5eeb798b
VS
5114 /*
5115 * Vblank time updates from the shadow to live plane control register
5116 * are blocked if the memory self-refresh mode is active at that
5117 * moment. So to make sure the plane gets truly disabled, disable
5118 * first the self-refresh mode. The self-refresh enable bit in turn
5119 * will be checked/applied by the HW only at the next frame start
5120 * event which is after the vblank start event, so we need to have a
5121 * wait-for-vblank between disabling the plane and the pipe.
5122 */
5123 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5124 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5125 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5126
ed4a6a7c
MR
5127 /*
5128 * IVB workaround: must disable low power watermarks for at least
5129 * one frame before enabling scaling. LP watermarks can be re-enabled
5130 * when scaling is disabled.
5131 *
5132 * WaCxSRDisabledForSpriteScaling:ivb
5133 */
ddd2b792 5134 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5135 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5136
5137 /*
5138 * If we're doing a modeset, we're done. No need to do any pre-vblank
5139 * watermark programming here.
5140 */
5141 if (needs_modeset(&pipe_config->base))
5142 return;
5143
5144 /*
5145 * For platforms that support atomic watermarks, program the
5146 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5147 * will be the intermediate values that are safe for both pre- and
5148 * post- vblank; when vblank happens, the 'active' values will be set
5149 * to the final 'target' values and we'll do this again to get the
5150 * optimal watermarks. For gen9+ platforms, the values we program here
5151 * will be the final target values which will get automatically latched
5152 * at vblank time; no further programming will be necessary.
5153 *
5154 * If a platform hasn't been transitioned to atomic watermarks yet,
5155 * we'll continue to update watermarks the old way, if flags tell
5156 * us to.
5157 */
5158 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5159 dev_priv->display.initial_watermarks(old_intel_state,
5160 pipe_config);
caed361d 5161 else if (pipe_config->update_wm_pre)
432081bc 5162 intel_update_watermarks(crtc);
ac21b225
ML
5163}
5164
d032ffa0 5165static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5166{
5167 struct drm_device *dev = crtc->dev;
5168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5169 struct drm_plane *p;
87d4300a
ML
5170 int pipe = intel_crtc->pipe;
5171
7cac945f 5172 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5173
d032ffa0
ML
5174 drm_for_each_plane_mask(p, dev, plane_mask)
5175 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5176
f99d7069
DV
5177 /*
5178 * FIXME: Once we grow proper nuclear flip support out of this we need
5179 * to compute the mask of flip planes precisely. For the time being
5180 * consider this a flip to a NULL plane.
5181 */
5748b6a1 5182 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5183}
5184
fb1c98b1 5185static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5186 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5187 struct drm_atomic_state *old_state)
5188{
aa5e9b47 5189 struct drm_connector_state *conn_state;
fb1c98b1
ML
5190 struct drm_connector *conn;
5191 int i;
5192
aa5e9b47 5193 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5194 struct intel_encoder *encoder =
5195 to_intel_encoder(conn_state->best_encoder);
5196
5197 if (conn_state->crtc != crtc)
5198 continue;
5199
5200 if (encoder->pre_pll_enable)
fd6bbda9 5201 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5202 }
5203}
5204
5205static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5206 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5207 struct drm_atomic_state *old_state)
5208{
aa5e9b47 5209 struct drm_connector_state *conn_state;
fb1c98b1
ML
5210 struct drm_connector *conn;
5211 int i;
5212
aa5e9b47 5213 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5214 struct intel_encoder *encoder =
5215 to_intel_encoder(conn_state->best_encoder);
5216
5217 if (conn_state->crtc != crtc)
5218 continue;
5219
5220 if (encoder->pre_enable)
fd6bbda9 5221 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5222 }
5223}
5224
5225static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5226 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5227 struct drm_atomic_state *old_state)
5228{
aa5e9b47 5229 struct drm_connector_state *conn_state;
fb1c98b1
ML
5230 struct drm_connector *conn;
5231 int i;
5232
aa5e9b47 5233 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5234 struct intel_encoder *encoder =
5235 to_intel_encoder(conn_state->best_encoder);
5236
5237 if (conn_state->crtc != crtc)
5238 continue;
5239
fd6bbda9 5240 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5241 intel_opregion_notify_encoder(encoder, true);
5242 }
5243}
5244
5245static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5246 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5247 struct drm_atomic_state *old_state)
5248{
5249 struct drm_connector_state *old_conn_state;
5250 struct drm_connector *conn;
5251 int i;
5252
aa5e9b47 5253 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5254 struct intel_encoder *encoder =
5255 to_intel_encoder(old_conn_state->best_encoder);
5256
5257 if (old_conn_state->crtc != crtc)
5258 continue;
5259
5260 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5261 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5262 }
5263}
5264
5265static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5266 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5267 struct drm_atomic_state *old_state)
5268{
5269 struct drm_connector_state *old_conn_state;
5270 struct drm_connector *conn;
5271 int i;
5272
aa5e9b47 5273 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5274 struct intel_encoder *encoder =
5275 to_intel_encoder(old_conn_state->best_encoder);
5276
5277 if (old_conn_state->crtc != crtc)
5278 continue;
5279
5280 if (encoder->post_disable)
fd6bbda9 5281 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5282 }
5283}
5284
5285static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5286 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5287 struct drm_atomic_state *old_state)
5288{
5289 struct drm_connector_state *old_conn_state;
5290 struct drm_connector *conn;
5291 int i;
5292
aa5e9b47 5293 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5294 struct intel_encoder *encoder =
5295 to_intel_encoder(old_conn_state->best_encoder);
5296
5297 if (old_conn_state->crtc != crtc)
5298 continue;
5299
5300 if (encoder->post_pll_disable)
fd6bbda9 5301 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5302 }
5303}
5304
4a806558
ML
5305static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5306 struct drm_atomic_state *old_state)
f67a559d 5307{
4a806558 5308 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5309 struct drm_device *dev = crtc->dev;
fac5e23e 5310 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5312 int pipe = intel_crtc->pipe;
ccf010fb
ML
5313 struct intel_atomic_state *old_intel_state =
5314 to_intel_atomic_state(old_state);
f67a559d 5315
53d9f4e9 5316 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5317 return;
5318
b2c0593a
VS
5319 /*
5320 * Sometimes spurious CPU pipe underruns happen during FDI
5321 * training, at least with VGA+HDMI cloning. Suppress them.
5322 *
5323 * On ILK we get an occasional spurious CPU pipe underruns
5324 * between eDP port A enable and vdd enable. Also PCH port
5325 * enable seems to result in the occasional CPU pipe underrun.
5326 *
5327 * Spurious PCH underruns also occur during PCH enabling.
5328 */
5329 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5330 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5331 if (intel_crtc->config->has_pch_encoder)
5332 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5333
6e3c9717 5334 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5335 intel_prepare_shared_dpll(intel_crtc);
5336
37a5650b 5337 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5338 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5339
5340 intel_set_pipe_timings(intel_crtc);
bc58be60 5341 intel_set_pipe_src_size(intel_crtc);
29407aab 5342
6e3c9717 5343 if (intel_crtc->config->has_pch_encoder) {
29407aab 5344 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5345 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5346 }
5347
5348 ironlake_set_pipeconf(crtc);
5349
f67a559d 5350 intel_crtc->active = true;
8664281b 5351
fd6bbda9 5352 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5353
6e3c9717 5354 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5355 /* Note: FDI PLL enabling _must_ be done before we enable the
5356 * cpu pipes, hence this is separate from all the other fdi/pch
5357 * enabling. */
88cefb6c 5358 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5359 } else {
5360 assert_fdi_tx_disabled(dev_priv, pipe);
5361 assert_fdi_rx_disabled(dev_priv, pipe);
5362 }
f67a559d 5363
b074cec8 5364 ironlake_pfit_enable(intel_crtc);
f67a559d 5365
9c54c0dd
JB
5366 /*
5367 * On ILK+ LUT must be loaded before the pipe is running but with
5368 * clocks enabled
5369 */
b95c5321 5370 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5371
1d5bf5d9 5372 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5373 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5374 intel_enable_pipe(intel_crtc);
f67a559d 5375
6e3c9717 5376 if (intel_crtc->config->has_pch_encoder)
2ce42273 5377 ironlake_pch_enable(pipe_config);
c98e9dcf 5378
f9b61ff6
DV
5379 assert_vblank_disabled(crtc);
5380 drm_crtc_vblank_on(crtc);
5381
fd6bbda9 5382 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5383
6e266956 5384 if (HAS_PCH_CPT(dev_priv))
a1520318 5385 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5386
5387 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5388 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5389 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5390 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5391 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5392}
5393
42db64ef
PZ
5394/* IPS only exists on ULT machines and is tied to pipe A. */
5395static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5396{
50a0bc90 5397 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5398}
5399
4a806558
ML
5400static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5401 struct drm_atomic_state *old_state)
4f771f10 5402{
4a806558 5403 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5404 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5406 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5407 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5408 struct intel_atomic_state *old_intel_state =
5409 to_intel_atomic_state(old_state);
4f771f10 5410
53d9f4e9 5411 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5412 return;
5413
81b088ca
VS
5414 if (intel_crtc->config->has_pch_encoder)
5415 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5416 false);
5417
fd6bbda9 5418 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5419
8106ddbd 5420 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5421 intel_enable_shared_dpll(intel_crtc);
5422
37a5650b 5423 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5424 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5425
d7edc4e5 5426 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5427 intel_set_pipe_timings(intel_crtc);
5428
bc58be60 5429 intel_set_pipe_src_size(intel_crtc);
229fca97 5430
4d1de975
JN
5431 if (cpu_transcoder != TRANSCODER_EDP &&
5432 !transcoder_is_dsi(cpu_transcoder)) {
5433 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5434 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5435 }
5436
6e3c9717 5437 if (intel_crtc->config->has_pch_encoder) {
229fca97 5438 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5439 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5440 }
5441
d7edc4e5 5442 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5443 haswell_set_pipeconf(crtc);
5444
391bf048 5445 haswell_set_pipemisc(crtc);
229fca97 5446
b95c5321 5447 intel_color_set_csc(&pipe_config->base);
229fca97 5448
4f771f10 5449 intel_crtc->active = true;
8664281b 5450
6b698516
DV
5451 if (intel_crtc->config->has_pch_encoder)
5452 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5453 else
5454 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5455
fd6bbda9 5456 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5457
d2d65408 5458 if (intel_crtc->config->has_pch_encoder)
dc4a1094 5459 dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
4fe9467d 5460
d7edc4e5 5461 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5462 intel_ddi_enable_pipe_clock(pipe_config);
4f771f10 5463
6315b5d3 5464 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5465 skylake_pfit_enable(intel_crtc);
ff6d9f55 5466 else
1c132b44 5467 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5468
5469 /*
5470 * On ILK+ LUT must be loaded before the pipe is running but with
5471 * clocks enabled
5472 */
b95c5321 5473 intel_color_load_luts(&pipe_config->base);
4f771f10 5474
3dc38eea 5475 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5476 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5477 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5478
1d5bf5d9 5479 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5480 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5481
5482 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5483 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5484 intel_enable_pipe(intel_crtc);
42db64ef 5485
6e3c9717 5486 if (intel_crtc->config->has_pch_encoder)
2ce42273 5487 lpt_pch_enable(pipe_config);
4f771f10 5488
0037071d 5489 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5490 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5491
f9b61ff6
DV
5492 assert_vblank_disabled(crtc);
5493 drm_crtc_vblank_on(crtc);
5494
fd6bbda9 5495 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5496
6b698516 5497 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5498 intel_wait_for_vblank(dev_priv, pipe);
5499 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5501 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5502 true);
6b698516 5503 }
d2d65408 5504
e4916946
PZ
5505 /* If we change the relative order between pipe/planes enabling, we need
5506 * to change the workaround. */
99d736a2 5507 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5508 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5509 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5510 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5511 }
4f771f10
PZ
5512}
5513
bfd16b2a 5514static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5515{
5516 struct drm_device *dev = crtc->base.dev;
fac5e23e 5517 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5518 int pipe = crtc->pipe;
5519
5520 /* To avoid upsetting the power well on haswell only disable the pfit if
5521 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5522 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5523 I915_WRITE(PF_CTL(pipe), 0);
5524 I915_WRITE(PF_WIN_POS(pipe), 0);
5525 I915_WRITE(PF_WIN_SZ(pipe), 0);
5526 }
5527}
5528
4a806558
ML
5529static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5530 struct drm_atomic_state *old_state)
6be4a607 5531{
4a806558 5532 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5533 struct drm_device *dev = crtc->dev;
fac5e23e 5534 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5536 int pipe = intel_crtc->pipe;
b52eb4dc 5537
b2c0593a
VS
5538 /*
5539 * Sometimes spurious CPU pipe underruns happen when the
5540 * pipe is already disabled, but FDI RX/TX is still enabled.
5541 * Happens at least with VGA+HDMI cloning. Suppress them.
5542 */
5543 if (intel_crtc->config->has_pch_encoder) {
5544 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5545 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5546 }
37ca8d4c 5547
fd6bbda9 5548 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5549
f9b61ff6
DV
5550 drm_crtc_vblank_off(crtc);
5551 assert_vblank_disabled(crtc);
5552
575f7ab7 5553 intel_disable_pipe(intel_crtc);
32f9d658 5554
bfd16b2a 5555 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5556
b2c0593a 5557 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5558 ironlake_fdi_disable(crtc);
5559
fd6bbda9 5560 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5561
6e3c9717 5562 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5563 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5564
6e266956 5565 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5566 i915_reg_t reg;
5567 u32 temp;
5568
d925c59a
DV
5569 /* disable TRANS_DP_CTL */
5570 reg = TRANS_DP_CTL(pipe);
5571 temp = I915_READ(reg);
5572 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5573 TRANS_DP_PORT_SEL_MASK);
5574 temp |= TRANS_DP_PORT_SEL_NONE;
5575 I915_WRITE(reg, temp);
5576
5577 /* disable DPLL_SEL */
5578 temp = I915_READ(PCH_DPLL_SEL);
11887397 5579 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5580 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5581 }
e3421a18 5582
d925c59a
DV
5583 ironlake_fdi_pll_disable(intel_crtc);
5584 }
81b088ca 5585
b2c0593a 5586 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5587 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5588}
1b3c7a47 5589
4a806558
ML
5590static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5591 struct drm_atomic_state *old_state)
ee7b9f93 5592{
4a806558 5593 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5594 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5596 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5597
d2d65408
VS
5598 if (intel_crtc->config->has_pch_encoder)
5599 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5600 false);
5601
fd6bbda9 5602 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5603
f9b61ff6
DV
5604 drm_crtc_vblank_off(crtc);
5605 assert_vblank_disabled(crtc);
5606
4d1de975 5607 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5608 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5609 intel_disable_pipe(intel_crtc);
4f771f10 5610
0037071d 5611 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5612 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
a4bf214f 5613
d7edc4e5 5614 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5615 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5616
6315b5d3 5617 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5618 skylake_scaler_disable(intel_crtc);
ff6d9f55 5619 else
bfd16b2a 5620 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5621
d7edc4e5 5622 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5623 intel_ddi_disable_pipe_clock(intel_crtc->config);
4f771f10 5624
fd6bbda9 5625 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5626
b7076546 5627 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5628 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5629 true);
4f771f10
PZ
5630}
5631
2dd24552
JB
5632static void i9xx_pfit_enable(struct intel_crtc *crtc)
5633{
5634 struct drm_device *dev = crtc->base.dev;
fac5e23e 5635 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5636 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5637
681a8504 5638 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5639 return;
5640
2dd24552 5641 /*
c0b03411
DV
5642 * The panel fitter should only be adjusted whilst the pipe is disabled,
5643 * according to register description and PRM.
2dd24552 5644 */
c0b03411
DV
5645 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5646 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5647
b074cec8
JB
5648 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5649 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5650
5651 /* Border color in case we don't scale up to the full screen. Black by
5652 * default, change to something else for debugging. */
5653 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5654}
5655
79f255a0 5656enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5657{
5658 switch (port) {
5659 case PORT_A:
6331a704 5660 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5661 case PORT_B:
6331a704 5662 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5663 case PORT_C:
6331a704 5664 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5665 case PORT_D:
6331a704 5666 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5667 case PORT_E:
6331a704 5668 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5669 default:
b9fec167 5670 MISSING_CASE(port);
d05410f9
DA
5671 return POWER_DOMAIN_PORT_OTHER;
5672 }
5673}
5674
d8fc70b7
ACO
5675static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5676 struct intel_crtc_state *crtc_state)
77d22dca 5677{
319be8ae 5678 struct drm_device *dev = crtc->dev;
37255d8d 5679 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5680 struct drm_encoder *encoder;
319be8ae
ID
5681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5682 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5683 u64 mask;
74bff5f9 5684 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5685
74bff5f9 5686 if (!crtc_state->base.active)
292b990e
ML
5687 return 0;
5688
77d22dca
ID
5689 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5690 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5691 if (crtc_state->pch_pfit.enabled ||
5692 crtc_state->pch_pfit.force_thru)
d8fc70b7 5693 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5694
74bff5f9
ML
5695 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5696 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5697
79f255a0 5698 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5699 }
319be8ae 5700
37255d8d
ML
5701 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5702 mask |= BIT(POWER_DOMAIN_AUDIO);
5703
15e7ec29 5704 if (crtc_state->shared_dpll)
d8fc70b7 5705 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5706
77d22dca
ID
5707 return mask;
5708}
5709
d2d15016 5710static u64
74bff5f9
ML
5711modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5712 struct intel_crtc_state *crtc_state)
77d22dca 5713{
fac5e23e 5714 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5716 enum intel_display_power_domain domain;
d8fc70b7 5717 u64 domains, new_domains, old_domains;
77d22dca 5718
292b990e 5719 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5720 intel_crtc->enabled_power_domains = new_domains =
5721 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5722
5a21b665 5723 domains = new_domains & ~old_domains;
292b990e
ML
5724
5725 for_each_power_domain(domain, domains)
5726 intel_display_power_get(dev_priv, domain);
5727
5a21b665 5728 return old_domains & ~new_domains;
292b990e
ML
5729}
5730
5731static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5732 u64 domains)
292b990e
ML
5733{
5734 enum intel_display_power_domain domain;
5735
5736 for_each_power_domain(domain, domains)
5737 intel_display_power_put(dev_priv, domain);
5738}
77d22dca 5739
7ff89ca2
VS
5740static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5741 struct drm_atomic_state *old_state)
adafdc6f 5742{
ff32c54e
VS
5743 struct intel_atomic_state *old_intel_state =
5744 to_intel_atomic_state(old_state);
7ff89ca2
VS
5745 struct drm_crtc *crtc = pipe_config->base.crtc;
5746 struct drm_device *dev = crtc->dev;
5747 struct drm_i915_private *dev_priv = to_i915(dev);
5748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5749 int pipe = intel_crtc->pipe;
adafdc6f 5750
7ff89ca2
VS
5751 if (WARN_ON(intel_crtc->active))
5752 return;
adafdc6f 5753
7ff89ca2
VS
5754 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5755 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5756
7ff89ca2
VS
5757 intel_set_pipe_timings(intel_crtc);
5758 intel_set_pipe_src_size(intel_crtc);
b2045352 5759
7ff89ca2
VS
5760 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5761 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5762
7ff89ca2
VS
5763 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5764 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5765 }
5766
7ff89ca2 5767 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5768
7ff89ca2 5769 intel_crtc->active = true;
92891e45 5770
7ff89ca2 5771 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5772
7ff89ca2 5773 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5774
7ff89ca2
VS
5775 if (IS_CHERRYVIEW(dev_priv)) {
5776 chv_prepare_pll(intel_crtc, intel_crtc->config);
5777 chv_enable_pll(intel_crtc, intel_crtc->config);
5778 } else {
5779 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5780 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5781 }
5782
7ff89ca2 5783 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5784
7ff89ca2 5785 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5786
7ff89ca2 5787 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5788
ff32c54e
VS
5789 dev_priv->display.initial_watermarks(old_intel_state,
5790 pipe_config);
7ff89ca2
VS
5791 intel_enable_pipe(intel_crtc);
5792
5793 assert_vblank_disabled(crtc);
5794 drm_crtc_vblank_on(crtc);
89b3c3c7 5795
7ff89ca2 5796 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5797}
5798
7ff89ca2 5799static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5800{
7ff89ca2
VS
5801 struct drm_device *dev = crtc->base.dev;
5802 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5803
7ff89ca2
VS
5804 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5805 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5806}
5807
7ff89ca2
VS
5808static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5809 struct drm_atomic_state *old_state)
2b73001e 5810{
7ff89ca2
VS
5811 struct drm_crtc *crtc = pipe_config->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 enum pipe pipe = intel_crtc->pipe;
2b73001e 5816
7ff89ca2
VS
5817 if (WARN_ON(intel_crtc->active))
5818 return;
2b73001e 5819
7ff89ca2 5820 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5821
7ff89ca2
VS
5822 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5823 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5824
7ff89ca2
VS
5825 intel_set_pipe_timings(intel_crtc);
5826 intel_set_pipe_src_size(intel_crtc);
2b73001e 5827
7ff89ca2 5828 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5829
7ff89ca2 5830 intel_crtc->active = true;
5f199dfa 5831
7ff89ca2
VS
5832 if (!IS_GEN2(dev_priv))
5833 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5834
7ff89ca2 5835 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5836
7ff89ca2 5837 i9xx_enable_pll(intel_crtc);
f8437dd1 5838
7ff89ca2 5839 i9xx_pfit_enable(intel_crtc);
f8437dd1 5840
7ff89ca2 5841 intel_color_load_luts(&pipe_config->base);
f8437dd1 5842
7ff89ca2
VS
5843 intel_update_watermarks(intel_crtc);
5844 intel_enable_pipe(intel_crtc);
f8437dd1 5845
7ff89ca2
VS
5846 assert_vblank_disabled(crtc);
5847 drm_crtc_vblank_on(crtc);
f8437dd1 5848
7ff89ca2
VS
5849 intel_encoders_enable(crtc, pipe_config, old_state);
5850}
f8437dd1 5851
7ff89ca2
VS
5852static void i9xx_pfit_disable(struct intel_crtc *crtc)
5853{
5854 struct drm_device *dev = crtc->base.dev;
5855 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5856
7ff89ca2 5857 if (!crtc->config->gmch_pfit.control)
f8437dd1 5858 return;
f8437dd1 5859
7ff89ca2
VS
5860 assert_pipe_disabled(dev_priv, crtc->pipe);
5861
5862 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5863 I915_READ(PFIT_CONTROL));
5864 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5865}
5866
7ff89ca2
VS
5867static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5868 struct drm_atomic_state *old_state)
f8437dd1 5869{
7ff89ca2
VS
5870 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5871 struct drm_device *dev = crtc->dev;
5872 struct drm_i915_private *dev_priv = to_i915(dev);
5873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5874 int pipe = intel_crtc->pipe;
d66a2194 5875
d66a2194 5876 /*
7ff89ca2
VS
5877 * On gen2 planes are double buffered but the pipe isn't, so we must
5878 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5879 */
7ff89ca2
VS
5880 if (IS_GEN2(dev_priv))
5881 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5882
7ff89ca2 5883 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5884
7ff89ca2
VS
5885 drm_crtc_vblank_off(crtc);
5886 assert_vblank_disabled(crtc);
d66a2194 5887
7ff89ca2 5888 intel_disable_pipe(intel_crtc);
d66a2194 5889
7ff89ca2 5890 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5891
7ff89ca2 5892 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5893
7ff89ca2
VS
5894 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5895 if (IS_CHERRYVIEW(dev_priv))
5896 chv_disable_pll(dev_priv, pipe);
5897 else if (IS_VALLEYVIEW(dev_priv))
5898 vlv_disable_pll(dev_priv, pipe);
5899 else
5900 i9xx_disable_pll(intel_crtc);
5901 }
c2e001ef 5902
7ff89ca2 5903 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5904
7ff89ca2
VS
5905 if (!IS_GEN2(dev_priv))
5906 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
5907
5908 if (!dev_priv->display.initial_watermarks)
5909 intel_update_watermarks(intel_crtc);
f8437dd1
VK
5910}
5911
7ff89ca2 5912static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
f8437dd1 5913{
7ff89ca2
VS
5914 struct intel_encoder *encoder;
5915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5916 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5917 enum intel_display_power_domain domain;
d2d15016 5918 u64 domains;
7ff89ca2
VS
5919 struct drm_atomic_state *state;
5920 struct intel_crtc_state *crtc_state;
5921 int ret;
f8437dd1 5922
7ff89ca2
VS
5923 if (!intel_crtc->active)
5924 return;
a8ca4934 5925
7ff89ca2
VS
5926 if (crtc->primary->state->visible) {
5927 WARN_ON(intel_crtc->flip_work);
5d96d8af 5928
7ff89ca2 5929 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5930
7ff89ca2
VS
5931 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5932 crtc->primary->state->visible = false;
5933 }
5d96d8af 5934
7ff89ca2
VS
5935 state = drm_atomic_state_alloc(crtc->dev);
5936 if (!state) {
5937 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5938 crtc->base.id, crtc->name);
1c3f7700 5939 return;
7ff89ca2 5940 }
9f7eb31a 5941
7ff89ca2 5942 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
ea61791e 5943
7ff89ca2
VS
5944 /* Everything's already locked, -EDEADLK can't happen. */
5945 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5946 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5947
7ff89ca2 5948 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5949
7ff89ca2 5950 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5951
0853695c 5952 drm_atomic_state_put(state);
842e0307 5953
78108b7c
VS
5954 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5955 crtc->base.id, crtc->name);
842e0307
ML
5956
5957 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5958 crtc->state->active = false;
37d9078b 5959 intel_crtc->active = false;
842e0307
ML
5960 crtc->enabled = false;
5961 crtc->state->connector_mask = 0;
5962 crtc->state->encoder_mask = 0;
5963
5964 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5965 encoder->base.crtc = NULL;
5966
58f9c0bc 5967 intel_fbc_disable(intel_crtc);
432081bc 5968 intel_update_watermarks(intel_crtc);
1f7457b1 5969 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5970
5971 domains = intel_crtc->enabled_power_domains;
5972 for_each_power_domain(domain, domains)
5973 intel_display_power_put(dev_priv, domain);
5974 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5975
5976 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
5977 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5978}
5979
6b72d486
ML
5980/*
5981 * turn all crtc's off, but do not adjust state
5982 * This has to be paired with a call to intel_modeset_setup_hw_state.
5983 */
70e0bd74 5984int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5985{
e2c8b870 5986 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5987 struct drm_atomic_state *state;
e2c8b870 5988 int ret;
70e0bd74 5989
e2c8b870
ML
5990 state = drm_atomic_helper_suspend(dev);
5991 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5992 if (ret)
5993 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5994 else
5995 dev_priv->modeset_restore_state = state;
70e0bd74 5996 return ret;
ee7b9f93
JB
5997}
5998
ea5b213a 5999void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6000{
4ef69c7a 6001 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6002
ea5b213a
CW
6003 drm_encoder_cleanup(encoder);
6004 kfree(intel_encoder);
7e7d76c3
JB
6005}
6006
0a91ca29
DV
6007/* Cross check the actual hw state with our own modeset state tracking (and it's
6008 * internal consistency). */
5a21b665 6009static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6010{
5a21b665 6011 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6012
6013 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6014 connector->base.base.id,
6015 connector->base.name);
6016
0a91ca29 6017 if (connector->get_hw_state(connector)) {
e85376cb 6018 struct intel_encoder *encoder = connector->encoder;
5a21b665 6019 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6020
35dd3c64
ML
6021 I915_STATE_WARN(!crtc,
6022 "connector enabled without attached crtc\n");
0a91ca29 6023
35dd3c64
ML
6024 if (!crtc)
6025 return;
6026
6027 I915_STATE_WARN(!crtc->state->active,
6028 "connector is active, but attached crtc isn't\n");
6029
e85376cb 6030 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6031 return;
6032
e85376cb 6033 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6034 "atomic encoder doesn't match attached encoder\n");
6035
e85376cb 6036 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6037 "attached encoder crtc differs from connector crtc\n");
6038 } else {
4d688a2a
ML
6039 I915_STATE_WARN(crtc && crtc->state->active,
6040 "attached crtc is active, but connector isn't\n");
5a21b665 6041 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 6042 "best encoder set without crtc!\n");
0a91ca29 6043 }
79e53945
JB
6044}
6045
08d9bc92
ACO
6046int intel_connector_init(struct intel_connector *connector)
6047{
5350a031 6048 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6049
5350a031 6050 if (!connector->base.state)
08d9bc92
ACO
6051 return -ENOMEM;
6052
08d9bc92
ACO
6053 return 0;
6054}
6055
6056struct intel_connector *intel_connector_alloc(void)
6057{
6058 struct intel_connector *connector;
6059
6060 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6061 if (!connector)
6062 return NULL;
6063
6064 if (intel_connector_init(connector) < 0) {
6065 kfree(connector);
6066 return NULL;
6067 }
6068
6069 return connector;
6070}
6071
f0947c37
DV
6072/* Simple connector->get_hw_state implementation for encoders that support only
6073 * one connector and no cloning and hence the encoder state determines the state
6074 * of the connector. */
6075bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6076{
24929352 6077 enum pipe pipe = 0;
f0947c37 6078 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6079
f0947c37 6080 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6081}
6082
6d293983 6083static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6084{
6d293983
ACO
6085 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6086 return crtc_state->fdi_lanes;
d272ddfa
VS
6087
6088 return 0;
6089}
6090
6d293983 6091static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6092 struct intel_crtc_state *pipe_config)
1857e1da 6093{
8652744b 6094 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6095 struct drm_atomic_state *state = pipe_config->base.state;
6096 struct intel_crtc *other_crtc;
6097 struct intel_crtc_state *other_crtc_state;
6098
1857e1da
DV
6099 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6100 pipe_name(pipe), pipe_config->fdi_lanes);
6101 if (pipe_config->fdi_lanes > 4) {
6102 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6103 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6104 return -EINVAL;
1857e1da
DV
6105 }
6106
8652744b 6107 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6108 if (pipe_config->fdi_lanes > 2) {
6109 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6110 pipe_config->fdi_lanes);
6d293983 6111 return -EINVAL;
1857e1da 6112 } else {
6d293983 6113 return 0;
1857e1da
DV
6114 }
6115 }
6116
b7f05d4a 6117 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6118 return 0;
1857e1da
DV
6119
6120 /* Ivybridge 3 pipe is really complicated */
6121 switch (pipe) {
6122 case PIPE_A:
6d293983 6123 return 0;
1857e1da 6124 case PIPE_B:
6d293983
ACO
6125 if (pipe_config->fdi_lanes <= 2)
6126 return 0;
6127
b91eb5cc 6128 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6129 other_crtc_state =
6130 intel_atomic_get_crtc_state(state, other_crtc);
6131 if (IS_ERR(other_crtc_state))
6132 return PTR_ERR(other_crtc_state);
6133
6134 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6135 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6136 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6137 return -EINVAL;
1857e1da 6138 }
6d293983 6139 return 0;
1857e1da 6140 case PIPE_C:
251cc67c
VS
6141 if (pipe_config->fdi_lanes > 2) {
6142 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6143 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6144 return -EINVAL;
251cc67c 6145 }
6d293983 6146
b91eb5cc 6147 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6148 other_crtc_state =
6149 intel_atomic_get_crtc_state(state, other_crtc);
6150 if (IS_ERR(other_crtc_state))
6151 return PTR_ERR(other_crtc_state);
6152
6153 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6154 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6155 return -EINVAL;
1857e1da 6156 }
6d293983 6157 return 0;
1857e1da
DV
6158 default:
6159 BUG();
6160 }
6161}
6162
e29c22c0
DV
6163#define RETRY 1
6164static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6165 struct intel_crtc_state *pipe_config)
877d48d5 6166{
1857e1da 6167 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6168 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6169 int lane, link_bw, fdi_dotclock, ret;
6170 bool needs_recompute = false;
877d48d5 6171
e29c22c0 6172retry:
877d48d5
DV
6173 /* FDI is a binary signal running at ~2.7GHz, encoding
6174 * each output octet as 10 bits. The actual frequency
6175 * is stored as a divider into a 100MHz clock, and the
6176 * mode pixel clock is stored in units of 1KHz.
6177 * Hence the bw of each lane in terms of the mode signal
6178 * is:
6179 */
21a727b3 6180 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6181
241bfc38 6182 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6183
2bd89a07 6184 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6185 pipe_config->pipe_bpp);
6186
6187 pipe_config->fdi_lanes = lane;
6188
2bd89a07 6189 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6190 link_bw, &pipe_config->fdi_m_n);
1857e1da 6191
e3b247da 6192 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6193 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6194 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6195 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6196 pipe_config->pipe_bpp);
6197 needs_recompute = true;
6198 pipe_config->bw_constrained = true;
257a7ffc 6199
7ff89ca2 6200 goto retry;
257a7ffc 6201 }
79e53945 6202
7ff89ca2
VS
6203 if (needs_recompute)
6204 return RETRY;
e70236a8 6205
7ff89ca2 6206 return ret;
e70236a8
JB
6207}
6208
7ff89ca2
VS
6209static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6210 struct intel_crtc_state *pipe_config)
e70236a8 6211{
7ff89ca2
VS
6212 if (pipe_config->pipe_bpp > 24)
6213 return false;
e70236a8 6214
7ff89ca2
VS
6215 /* HSW can handle pixel rate up to cdclk? */
6216 if (IS_HASWELL(dev_priv))
6217 return true;
1b1d2716 6218
65cd2b3f 6219 /*
7ff89ca2
VS
6220 * We compare against max which means we must take
6221 * the increased cdclk requirement into account when
6222 * calculating the new cdclk.
6223 *
6224 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6225 */
7ff89ca2
VS
6226 return pipe_config->pixel_rate <=
6227 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6228}
79e53945 6229
7ff89ca2
VS
6230static void hsw_compute_ips_config(struct intel_crtc *crtc,
6231 struct intel_crtc_state *pipe_config)
6232{
6233 struct drm_device *dev = crtc->base.dev;
6234 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6235
7ff89ca2
VS
6236 pipe_config->ips_enabled = i915.enable_ips &&
6237 hsw_crtc_supports_ips(crtc) &&
6238 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6239}
6240
7ff89ca2 6241static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6242{
7ff89ca2 6243 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6244
7ff89ca2
VS
6245 /* GDG double wide on either pipe, otherwise pipe A only */
6246 return INTEL_INFO(dev_priv)->gen < 4 &&
6247 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6248}
6249
ceb99320
VS
6250static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6251{
6252 uint32_t pixel_rate;
6253
6254 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6255
6256 /*
6257 * We only use IF-ID interlacing. If we ever use
6258 * PF-ID we'll need to adjust the pixel_rate here.
6259 */
6260
6261 if (pipe_config->pch_pfit.enabled) {
6262 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6263 uint32_t pfit_size = pipe_config->pch_pfit.size;
6264
6265 pipe_w = pipe_config->pipe_src_w;
6266 pipe_h = pipe_config->pipe_src_h;
6267
6268 pfit_w = (pfit_size >> 16) & 0xFFFF;
6269 pfit_h = pfit_size & 0xFFFF;
6270 if (pipe_w < pfit_w)
6271 pipe_w = pfit_w;
6272 if (pipe_h < pfit_h)
6273 pipe_h = pfit_h;
6274
6275 if (WARN_ON(!pfit_w || !pfit_h))
6276 return pixel_rate;
6277
6278 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6279 pfit_w * pfit_h);
6280 }
6281
6282 return pixel_rate;
6283}
6284
7ff89ca2 6285static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6286{
7ff89ca2 6287 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6288
7ff89ca2
VS
6289 if (HAS_GMCH_DISPLAY(dev_priv))
6290 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6291 crtc_state->pixel_rate =
6292 crtc_state->base.adjusted_mode.crtc_clock;
6293 else
6294 crtc_state->pixel_rate =
6295 ilk_pipe_pixel_rate(crtc_state);
6296}
34edce2f 6297
7ff89ca2
VS
6298static int intel_crtc_compute_config(struct intel_crtc *crtc,
6299 struct intel_crtc_state *pipe_config)
6300{
6301 struct drm_device *dev = crtc->base.dev;
6302 struct drm_i915_private *dev_priv = to_i915(dev);
6303 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6304 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6305
7ff89ca2
VS
6306 if (INTEL_GEN(dev_priv) < 4) {
6307 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6308
7ff89ca2
VS
6309 /*
6310 * Enable double wide mode when the dot clock
6311 * is > 90% of the (display) core speed.
6312 */
6313 if (intel_crtc_supports_double_wide(crtc) &&
6314 adjusted_mode->crtc_clock > clock_limit) {
6315 clock_limit = dev_priv->max_dotclk_freq;
6316 pipe_config->double_wide = true;
6317 }
34edce2f
VS
6318 }
6319
7ff89ca2
VS
6320 if (adjusted_mode->crtc_clock > clock_limit) {
6321 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6322 adjusted_mode->crtc_clock, clock_limit,
6323 yesno(pipe_config->double_wide));
6324 return -EINVAL;
6325 }
34edce2f 6326
7ff89ca2
VS
6327 /*
6328 * Pipe horizontal size must be even in:
6329 * - DVO ganged mode
6330 * - LVDS dual channel mode
6331 * - Double wide pipe
6332 */
6333 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6334 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6335 pipe_config->pipe_src_w &= ~1;
34edce2f 6336
7ff89ca2
VS
6337 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6338 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6339 */
6340 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6341 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6342 return -EINVAL;
34edce2f 6343
7ff89ca2 6344 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6345
7ff89ca2
VS
6346 if (HAS_IPS(dev_priv))
6347 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6348
7ff89ca2
VS
6349 if (pipe_config->has_pch_encoder)
6350 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6351
7ff89ca2 6352 return 0;
34edce2f
VS
6353}
6354
2c07245f 6355static void
a65851af 6356intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6357{
a65851af
VS
6358 while (*num > DATA_LINK_M_N_MASK ||
6359 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6360 *num >>= 1;
6361 *den >>= 1;
6362 }
6363}
6364
a65851af
VS
6365static void compute_m_n(unsigned int m, unsigned int n,
6366 uint32_t *ret_m, uint32_t *ret_n)
6367{
6368 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6369 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6370 intel_reduce_m_n_ratio(ret_m, ret_n);
6371}
6372
e69d0bc1
DV
6373void
6374intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6375 int pixel_clock, int link_clock,
6376 struct intel_link_m_n *m_n)
2c07245f 6377{
e69d0bc1 6378 m_n->tu = 64;
a65851af
VS
6379
6380 compute_m_n(bits_per_pixel * pixel_clock,
6381 link_clock * nlanes * 8,
6382 &m_n->gmch_m, &m_n->gmch_n);
6383
6384 compute_m_n(pixel_clock, link_clock,
6385 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6386}
6387
a7615030
CW
6388static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6389{
d330a953
JN
6390 if (i915.panel_use_ssc >= 0)
6391 return i915.panel_use_ssc != 0;
41aa3448 6392 return dev_priv->vbt.lvds_use_ssc
435793df 6393 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6394}
6395
7429e9d4 6396static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6397{
7df00d7a 6398 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6399}
f47709a9 6400
7429e9d4
DV
6401static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6402{
6403 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6404}
6405
f47709a9 6406static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6407 struct intel_crtc_state *crtc_state,
9e2c8475 6408 struct dpll *reduced_clock)
a7516a05 6409{
9b1e14f4 6410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6411 u32 fp, fp2 = 0;
6412
9b1e14f4 6413 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6414 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6415 if (reduced_clock)
7429e9d4 6416 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6417 } else {
190f68c5 6418 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6419 if (reduced_clock)
7429e9d4 6420 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6421 }
6422
190f68c5 6423 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6424
f47709a9 6425 crtc->lowfreq_avail = false;
2d84d2b3 6426 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6427 reduced_clock) {
190f68c5 6428 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6429 crtc->lowfreq_avail = true;
a7516a05 6430 } else {
190f68c5 6431 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6432 }
6433}
6434
5e69f97f
CML
6435static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6436 pipe)
89b667f8
JB
6437{
6438 u32 reg_val;
6439
6440 /*
6441 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6442 * and set it to a reasonable value instead.
6443 */
ab3c759a 6444 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6445 reg_val &= 0xffffff00;
6446 reg_val |= 0x00000030;
ab3c759a 6447 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6448
ab3c759a 6449 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6450 reg_val &= 0x8cffffff;
6451 reg_val = 0x8c000000;
ab3c759a 6452 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6453
ab3c759a 6454 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6455 reg_val &= 0xffffff00;
ab3c759a 6456 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6457
ab3c759a 6458 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6459 reg_val &= 0x00ffffff;
6460 reg_val |= 0xb0000000;
ab3c759a 6461 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6462}
6463
b551842d
DV
6464static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6465 struct intel_link_m_n *m_n)
6466{
6467 struct drm_device *dev = crtc->base.dev;
fac5e23e 6468 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6469 int pipe = crtc->pipe;
6470
e3b95f1e
DV
6471 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6472 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6473 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6474 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6475}
6476
6477static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6478 struct intel_link_m_n *m_n,
6479 struct intel_link_m_n *m2_n2)
b551842d 6480{
6315b5d3 6481 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6482 int pipe = crtc->pipe;
6e3c9717 6483 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6484
6315b5d3 6485 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6486 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6487 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6488 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6489 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6490 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6491 * for gen < 8) and if DRRS is supported (to make sure the
6492 * registers are not unnecessarily accessed).
6493 */
920a14b2
TU
6494 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6495 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6496 I915_WRITE(PIPE_DATA_M2(transcoder),
6497 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6498 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6499 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6500 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6501 }
b551842d 6502 } else {
e3b95f1e
DV
6503 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6504 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6505 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6506 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6507 }
6508}
6509
fe3cd48d 6510void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6511{
fe3cd48d
R
6512 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6513
6514 if (m_n == M1_N1) {
6515 dp_m_n = &crtc->config->dp_m_n;
6516 dp_m2_n2 = &crtc->config->dp_m2_n2;
6517 } else if (m_n == M2_N2) {
6518
6519 /*
6520 * M2_N2 registers are not supported. Hence m2_n2 divider value
6521 * needs to be programmed into M1_N1.
6522 */
6523 dp_m_n = &crtc->config->dp_m2_n2;
6524 } else {
6525 DRM_ERROR("Unsupported divider value\n");
6526 return;
6527 }
6528
6e3c9717
ACO
6529 if (crtc->config->has_pch_encoder)
6530 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6531 else
fe3cd48d 6532 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6533}
6534
251ac862
DV
6535static void vlv_compute_dpll(struct intel_crtc *crtc,
6536 struct intel_crtc_state *pipe_config)
bdd4b6a6 6537{
03ed5cbf 6538 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6539 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6540 if (crtc->pipe != PIPE_A)
6541 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6542
cd2d34d9 6543 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6544 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6545 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6546 DPLL_EXT_BUFFER_ENABLE_VLV;
6547
03ed5cbf
VS
6548 pipe_config->dpll_hw_state.dpll_md =
6549 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6550}
bdd4b6a6 6551
03ed5cbf
VS
6552static void chv_compute_dpll(struct intel_crtc *crtc,
6553 struct intel_crtc_state *pipe_config)
6554{
6555 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6556 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6557 if (crtc->pipe != PIPE_A)
6558 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6559
cd2d34d9 6560 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6561 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6562 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6563
03ed5cbf
VS
6564 pipe_config->dpll_hw_state.dpll_md =
6565 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6566}
6567
d288f65f 6568static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6569 const struct intel_crtc_state *pipe_config)
a0c4da24 6570{
f47709a9 6571 struct drm_device *dev = crtc->base.dev;
fac5e23e 6572 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6573 enum pipe pipe = crtc->pipe;
bdd4b6a6 6574 u32 mdiv;
a0c4da24 6575 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6576 u32 coreclk, reg_val;
a0c4da24 6577
cd2d34d9
VS
6578 /* Enable Refclk */
6579 I915_WRITE(DPLL(pipe),
6580 pipe_config->dpll_hw_state.dpll &
6581 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6582
6583 /* No need to actually set up the DPLL with DSI */
6584 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6585 return;
6586
a580516d 6587 mutex_lock(&dev_priv->sb_lock);
09153000 6588
d288f65f
VS
6589 bestn = pipe_config->dpll.n;
6590 bestm1 = pipe_config->dpll.m1;
6591 bestm2 = pipe_config->dpll.m2;
6592 bestp1 = pipe_config->dpll.p1;
6593 bestp2 = pipe_config->dpll.p2;
a0c4da24 6594
89b667f8
JB
6595 /* See eDP HDMI DPIO driver vbios notes doc */
6596
6597 /* PLL B needs special handling */
bdd4b6a6 6598 if (pipe == PIPE_B)
5e69f97f 6599 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6600
6601 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6602 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6603
6604 /* Disable target IRef on PLL */
ab3c759a 6605 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6606 reg_val &= 0x00ffffff;
ab3c759a 6607 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6608
6609 /* Disable fast lock */
ab3c759a 6610 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6611
6612 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6613 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6614 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6615 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6616 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6617
6618 /*
6619 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6620 * but we don't support that).
6621 * Note: don't use the DAC post divider as it seems unstable.
6622 */
6623 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6624 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6625
a0c4da24 6626 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6627 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6628
89b667f8 6629 /* Set HBR and RBR LPF coefficients */
d288f65f 6630 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6631 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6632 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6633 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6634 0x009f0003);
89b667f8 6635 else
ab3c759a 6636 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6637 0x00d0000f);
6638
37a5650b 6639 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6640 /* Use SSC source */
bdd4b6a6 6641 if (pipe == PIPE_A)
ab3c759a 6642 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6643 0x0df40000);
6644 else
ab3c759a 6645 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6646 0x0df70000);
6647 } else { /* HDMI or VGA */
6648 /* Use bend source */
bdd4b6a6 6649 if (pipe == PIPE_A)
ab3c759a 6650 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6651 0x0df70000);
6652 else
ab3c759a 6653 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6654 0x0df40000);
6655 }
a0c4da24 6656
ab3c759a 6657 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6658 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6659 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6660 coreclk |= 0x01000000;
ab3c759a 6661 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6662
ab3c759a 6663 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6664 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6665}
6666
d288f65f 6667static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6668 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6669{
6670 struct drm_device *dev = crtc->base.dev;
fac5e23e 6671 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6672 enum pipe pipe = crtc->pipe;
9d556c99 6673 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6674 u32 loopfilter, tribuf_calcntr;
9d556c99 6675 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6676 u32 dpio_val;
9cbe40c1 6677 int vco;
9d556c99 6678
cd2d34d9
VS
6679 /* Enable Refclk and SSC */
6680 I915_WRITE(DPLL(pipe),
6681 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6682
6683 /* No need to actually set up the DPLL with DSI */
6684 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6685 return;
6686
d288f65f
VS
6687 bestn = pipe_config->dpll.n;
6688 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6689 bestm1 = pipe_config->dpll.m1;
6690 bestm2 = pipe_config->dpll.m2 >> 22;
6691 bestp1 = pipe_config->dpll.p1;
6692 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6693 vco = pipe_config->dpll.vco;
a945ce7e 6694 dpio_val = 0;
9cbe40c1 6695 loopfilter = 0;
9d556c99 6696
a580516d 6697 mutex_lock(&dev_priv->sb_lock);
9d556c99 6698
9d556c99
CML
6699 /* p1 and p2 divider */
6700 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6701 5 << DPIO_CHV_S1_DIV_SHIFT |
6702 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6703 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6704 1 << DPIO_CHV_K_DIV_SHIFT);
6705
6706 /* Feedback post-divider - m2 */
6707 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6708
6709 /* Feedback refclk divider - n and m1 */
6710 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6711 DPIO_CHV_M1_DIV_BY_2 |
6712 1 << DPIO_CHV_N_DIV_SHIFT);
6713
6714 /* M2 fraction division */
25a25dfc 6715 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6716
6717 /* M2 fraction division enable */
a945ce7e
VP
6718 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6719 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6720 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6721 if (bestm2_frac)
6722 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6723 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6724
de3a0fde
VP
6725 /* Program digital lock detect threshold */
6726 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6727 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6728 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6729 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6730 if (!bestm2_frac)
6731 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6732 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6733
9d556c99 6734 /* Loop filter */
9cbe40c1
VP
6735 if (vco == 5400000) {
6736 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6737 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6738 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6739 tribuf_calcntr = 0x9;
6740 } else if (vco <= 6200000) {
6741 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6742 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6743 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6744 tribuf_calcntr = 0x9;
6745 } else if (vco <= 6480000) {
6746 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6747 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6748 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6749 tribuf_calcntr = 0x8;
6750 } else {
6751 /* Not supported. Apply the same limits as in the max case */
6752 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6753 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6754 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6755 tribuf_calcntr = 0;
6756 }
9d556c99
CML
6757 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6758
968040b2 6759 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6760 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6761 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6762 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6763
9d556c99
CML
6764 /* AFC Recal */
6765 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6766 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6767 DPIO_AFC_RECAL);
6768
a580516d 6769 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6770}
6771
d288f65f
VS
6772/**
6773 * vlv_force_pll_on - forcibly enable just the PLL
6774 * @dev_priv: i915 private structure
6775 * @pipe: pipe PLL to enable
6776 * @dpll: PLL configuration
6777 *
6778 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6779 * in cases where we need the PLL enabled even when @pipe is not going to
6780 * be enabled.
6781 */
30ad9814 6782int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6783 const struct dpll *dpll)
d288f65f 6784{
b91eb5cc 6785 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6786 struct intel_crtc_state *pipe_config;
6787
6788 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6789 if (!pipe_config)
6790 return -ENOMEM;
6791
6792 pipe_config->base.crtc = &crtc->base;
6793 pipe_config->pixel_multiplier = 1;
6794 pipe_config->dpll = *dpll;
d288f65f 6795
30ad9814 6796 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6797 chv_compute_dpll(crtc, pipe_config);
6798 chv_prepare_pll(crtc, pipe_config);
6799 chv_enable_pll(crtc, pipe_config);
d288f65f 6800 } else {
3f36b937
TU
6801 vlv_compute_dpll(crtc, pipe_config);
6802 vlv_prepare_pll(crtc, pipe_config);
6803 vlv_enable_pll(crtc, pipe_config);
d288f65f 6804 }
3f36b937
TU
6805
6806 kfree(pipe_config);
6807
6808 return 0;
d288f65f
VS
6809}
6810
6811/**
6812 * vlv_force_pll_off - forcibly disable just the PLL
6813 * @dev_priv: i915 private structure
6814 * @pipe: pipe PLL to disable
6815 *
6816 * Disable the PLL for @pipe. To be used in cases where we need
6817 * the PLL enabled even when @pipe is not going to be enabled.
6818 */
30ad9814 6819void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6820{
30ad9814
VS
6821 if (IS_CHERRYVIEW(dev_priv))
6822 chv_disable_pll(dev_priv, pipe);
d288f65f 6823 else
30ad9814 6824 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6825}
6826
251ac862
DV
6827static void i9xx_compute_dpll(struct intel_crtc *crtc,
6828 struct intel_crtc_state *crtc_state,
9e2c8475 6829 struct dpll *reduced_clock)
eb1cbe48 6830{
9b1e14f4 6831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6832 u32 dpll;
190f68c5 6833 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6834
190f68c5 6835 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6836
eb1cbe48
DV
6837 dpll = DPLL_VGA_MODE_DIS;
6838
2d84d2b3 6839 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6840 dpll |= DPLLB_MODE_LVDS;
6841 else
6842 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6843
73f67aa8
JN
6844 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6845 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6846 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6847 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6848 }
198a037f 6849
3d6e9ee0
VS
6850 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6851 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6852 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6853
37a5650b 6854 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6855 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6856
6857 /* compute bitmask from p1 value */
9b1e14f4 6858 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6859 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6860 else {
6861 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6862 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6863 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6864 }
6865 switch (clock->p2) {
6866 case 5:
6867 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6868 break;
6869 case 7:
6870 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6871 break;
6872 case 10:
6873 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6874 break;
6875 case 14:
6876 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6877 break;
6878 }
9b1e14f4 6879 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6880 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6881
190f68c5 6882 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6883 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6884 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6885 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6886 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6887 else
6888 dpll |= PLL_REF_INPUT_DREFCLK;
6889
6890 dpll |= DPLL_VCO_ENABLE;
190f68c5 6891 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6892
9b1e14f4 6893 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6894 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6895 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6896 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6897 }
6898}
6899
251ac862
DV
6900static void i8xx_compute_dpll(struct intel_crtc *crtc,
6901 struct intel_crtc_state *crtc_state,
9e2c8475 6902 struct dpll *reduced_clock)
eb1cbe48 6903{
f47709a9 6904 struct drm_device *dev = crtc->base.dev;
fac5e23e 6905 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6906 u32 dpll;
190f68c5 6907 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6908
190f68c5 6909 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6910
eb1cbe48
DV
6911 dpll = DPLL_VGA_MODE_DIS;
6912
2d84d2b3 6913 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6914 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6915 } else {
6916 if (clock->p1 == 2)
6917 dpll |= PLL_P1_DIVIDE_BY_TWO;
6918 else
6919 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6920 if (clock->p2 == 4)
6921 dpll |= PLL_P2_DIVIDE_BY_4;
6922 }
6923
50a0bc90
TU
6924 if (!IS_I830(dev_priv) &&
6925 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6926 dpll |= DPLL_DVO_2X_MODE;
6927
2d84d2b3 6928 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6929 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6930 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6931 else
6932 dpll |= PLL_REF_INPUT_DREFCLK;
6933
6934 dpll |= DPLL_VCO_ENABLE;
190f68c5 6935 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6936}
6937
8a654f3b 6938static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6939{
6315b5d3 6940 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6941 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6942 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6943 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6944 uint32_t crtc_vtotal, crtc_vblank_end;
6945 int vsyncshift = 0;
4d8a62ea
DV
6946
6947 /* We need to be careful not to changed the adjusted mode, for otherwise
6948 * the hw state checker will get angry at the mismatch. */
6949 crtc_vtotal = adjusted_mode->crtc_vtotal;
6950 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6951
609aeaca 6952 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6953 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6954 crtc_vtotal -= 1;
6955 crtc_vblank_end -= 1;
609aeaca 6956
2d84d2b3 6957 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6958 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6959 else
6960 vsyncshift = adjusted_mode->crtc_hsync_start -
6961 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6962 if (vsyncshift < 0)
6963 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6964 }
6965
6315b5d3 6966 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6967 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6968
fe2b8f9d 6969 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6970 (adjusted_mode->crtc_hdisplay - 1) |
6971 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6972 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6973 (adjusted_mode->crtc_hblank_start - 1) |
6974 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6975 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6976 (adjusted_mode->crtc_hsync_start - 1) |
6977 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6978
fe2b8f9d 6979 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6980 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6981 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6982 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6983 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6984 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6985 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6986 (adjusted_mode->crtc_vsync_start - 1) |
6987 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6988
b5e508d4
PZ
6989 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6990 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6991 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6992 * bits. */
772c2a51 6993 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6994 (pipe == PIPE_B || pipe == PIPE_C))
6995 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6996
bc58be60
JN
6997}
6998
6999static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7000{
7001 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7002 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7003 enum pipe pipe = intel_crtc->pipe;
7004
b0e77b9c
PZ
7005 /* pipesrc controls the size that is scaled from, which should
7006 * always be the user's requested size.
7007 */
7008 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7009 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7010 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7011}
7012
1bd1bd80 7013static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7014 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7015{
7016 struct drm_device *dev = crtc->base.dev;
fac5e23e 7017 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7018 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7019 uint32_t tmp;
7020
7021 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7022 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7023 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7024 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7025 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7026 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7027 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7028 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7029 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7030
7031 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7032 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7033 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7034 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7035 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7036 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7037 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7038 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7039 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7040
7041 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7042 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7043 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7044 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7045 }
bc58be60
JN
7046}
7047
7048static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7049 struct intel_crtc_state *pipe_config)
7050{
7051 struct drm_device *dev = crtc->base.dev;
fac5e23e 7052 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7053 u32 tmp;
1bd1bd80
DV
7054
7055 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7056 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7057 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7058
2d112de7
ACO
7059 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7060 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7061}
7062
f6a83288 7063void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7064 struct intel_crtc_state *pipe_config)
babea61d 7065{
2d112de7
ACO
7066 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7067 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7068 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7069 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7070
2d112de7
ACO
7071 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7072 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7073 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7074 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7075
2d112de7 7076 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7077 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7078
2d112de7 7079 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7080
7081 mode->hsync = drm_mode_hsync(mode);
7082 mode->vrefresh = drm_mode_vrefresh(mode);
7083 drm_mode_set_name(mode);
babea61d
JB
7084}
7085
84b046f3
DV
7086static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7087{
6315b5d3 7088 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7089 uint32_t pipeconf;
7090
9f11a9e4 7091 pipeconf = 0;
84b046f3 7092
b6b5d049
VS
7093 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7094 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7095 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7096
6e3c9717 7097 if (intel_crtc->config->double_wide)
cf532bb2 7098 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7099
ff9ce46e 7100 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7101 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7102 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7103 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7104 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7105 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7106 PIPECONF_DITHER_TYPE_SP;
84b046f3 7107
6e3c9717 7108 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7109 case 18:
7110 pipeconf |= PIPECONF_6BPC;
7111 break;
7112 case 24:
7113 pipeconf |= PIPECONF_8BPC;
7114 break;
7115 case 30:
7116 pipeconf |= PIPECONF_10BPC;
7117 break;
7118 default:
7119 /* Case prevented by intel_choose_pipe_bpp_dither. */
7120 BUG();
84b046f3
DV
7121 }
7122 }
7123
56b857a5 7124 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
DV
7125 if (intel_crtc->lowfreq_avail) {
7126 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7127 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7128 } else {
7129 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7130 }
7131 }
7132
6e3c9717 7133 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7134 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7135 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7136 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7137 else
7138 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7139 } else
84b046f3
DV
7140 pipeconf |= PIPECONF_PROGRESSIVE;
7141
920a14b2 7142 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7143 intel_crtc->config->limited_color_range)
9f11a9e4 7144 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7145
84b046f3
DV
7146 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7147 POSTING_READ(PIPECONF(intel_crtc->pipe));
7148}
7149
81c97f52
ACO
7150static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7151 struct intel_crtc_state *crtc_state)
7152{
7153 struct drm_device *dev = crtc->base.dev;
fac5e23e 7154 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7155 const struct intel_limit *limit;
81c97f52
ACO
7156 int refclk = 48000;
7157
7158 memset(&crtc_state->dpll_hw_state, 0,
7159 sizeof(crtc_state->dpll_hw_state));
7160
2d84d2b3 7161 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7162 if (intel_panel_use_ssc(dev_priv)) {
7163 refclk = dev_priv->vbt.lvds_ssc_freq;
7164 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7165 }
7166
7167 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7168 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7169 limit = &intel_limits_i8xx_dvo;
7170 } else {
7171 limit = &intel_limits_i8xx_dac;
7172 }
7173
7174 if (!crtc_state->clock_set &&
7175 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7176 refclk, NULL, &crtc_state->dpll)) {
7177 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7178 return -EINVAL;
7179 }
7180
7181 i8xx_compute_dpll(crtc, crtc_state, NULL);
7182
7183 return 0;
7184}
7185
19ec6693
ACO
7186static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7187 struct intel_crtc_state *crtc_state)
7188{
7189 struct drm_device *dev = crtc->base.dev;
fac5e23e 7190 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7191 const struct intel_limit *limit;
19ec6693
ACO
7192 int refclk = 96000;
7193
7194 memset(&crtc_state->dpll_hw_state, 0,
7195 sizeof(crtc_state->dpll_hw_state));
7196
2d84d2b3 7197 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7198 if (intel_panel_use_ssc(dev_priv)) {
7199 refclk = dev_priv->vbt.lvds_ssc_freq;
7200 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7201 }
7202
7203 if (intel_is_dual_link_lvds(dev))
7204 limit = &intel_limits_g4x_dual_channel_lvds;
7205 else
7206 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7207 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7208 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7209 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7210 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7211 limit = &intel_limits_g4x_sdvo;
7212 } else {
7213 /* The option is for other outputs */
7214 limit = &intel_limits_i9xx_sdvo;
7215 }
7216
7217 if (!crtc_state->clock_set &&
7218 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7219 refclk, NULL, &crtc_state->dpll)) {
7220 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7221 return -EINVAL;
7222 }
7223
7224 i9xx_compute_dpll(crtc, crtc_state, NULL);
7225
7226 return 0;
7227}
7228
70e8aa21
ACO
7229static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7230 struct intel_crtc_state *crtc_state)
7231{
7232 struct drm_device *dev = crtc->base.dev;
fac5e23e 7233 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7234 const struct intel_limit *limit;
70e8aa21
ACO
7235 int refclk = 96000;
7236
7237 memset(&crtc_state->dpll_hw_state, 0,
7238 sizeof(crtc_state->dpll_hw_state));
7239
2d84d2b3 7240 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7241 if (intel_panel_use_ssc(dev_priv)) {
7242 refclk = dev_priv->vbt.lvds_ssc_freq;
7243 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7244 }
7245
7246 limit = &intel_limits_pineview_lvds;
7247 } else {
7248 limit = &intel_limits_pineview_sdvo;
7249 }
7250
7251 if (!crtc_state->clock_set &&
7252 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7253 refclk, NULL, &crtc_state->dpll)) {
7254 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7255 return -EINVAL;
7256 }
7257
7258 i9xx_compute_dpll(crtc, crtc_state, NULL);
7259
7260 return 0;
7261}
7262
190f68c5
ACO
7263static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7264 struct intel_crtc_state *crtc_state)
79e53945 7265{
c7653199 7266 struct drm_device *dev = crtc->base.dev;
fac5e23e 7267 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7268 const struct intel_limit *limit;
81c97f52 7269 int refclk = 96000;
79e53945 7270
dd3cd74a
ACO
7271 memset(&crtc_state->dpll_hw_state, 0,
7272 sizeof(crtc_state->dpll_hw_state));
7273
2d84d2b3 7274 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7275 if (intel_panel_use_ssc(dev_priv)) {
7276 refclk = dev_priv->vbt.lvds_ssc_freq;
7277 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7278 }
43565a06 7279
70e8aa21
ACO
7280 limit = &intel_limits_i9xx_lvds;
7281 } else {
7282 limit = &intel_limits_i9xx_sdvo;
81c97f52 7283 }
79e53945 7284
70e8aa21
ACO
7285 if (!crtc_state->clock_set &&
7286 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7287 refclk, NULL, &crtc_state->dpll)) {
7288 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7289 return -EINVAL;
f47709a9 7290 }
7026d4ac 7291
81c97f52 7292 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7293
c8f7a0db 7294 return 0;
f564048e
EA
7295}
7296
65b3d6a9
ACO
7297static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7298 struct intel_crtc_state *crtc_state)
7299{
7300 int refclk = 100000;
1b6f4958 7301 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7302
7303 memset(&crtc_state->dpll_hw_state, 0,
7304 sizeof(crtc_state->dpll_hw_state));
7305
65b3d6a9
ACO
7306 if (!crtc_state->clock_set &&
7307 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7308 refclk, NULL, &crtc_state->dpll)) {
7309 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7310 return -EINVAL;
7311 }
7312
7313 chv_compute_dpll(crtc, crtc_state);
7314
7315 return 0;
7316}
7317
7318static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7319 struct intel_crtc_state *crtc_state)
7320{
7321 int refclk = 100000;
1b6f4958 7322 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7323
7324 memset(&crtc_state->dpll_hw_state, 0,
7325 sizeof(crtc_state->dpll_hw_state));
7326
65b3d6a9
ACO
7327 if (!crtc_state->clock_set &&
7328 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7329 refclk, NULL, &crtc_state->dpll)) {
7330 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7331 return -EINVAL;
7332 }
7333
7334 vlv_compute_dpll(crtc, crtc_state);
7335
7336 return 0;
7337}
7338
2fa2fe9a 7339static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7340 struct intel_crtc_state *pipe_config)
2fa2fe9a 7341{
6315b5d3 7342 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7343 uint32_t tmp;
7344
50a0bc90
TU
7345 if (INTEL_GEN(dev_priv) <= 3 &&
7346 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7347 return;
7348
2fa2fe9a 7349 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7350 if (!(tmp & PFIT_ENABLE))
7351 return;
2fa2fe9a 7352
06922821 7353 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7354 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7355 if (crtc->pipe != PIPE_B)
7356 return;
2fa2fe9a
DV
7357 } else {
7358 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7359 return;
7360 }
7361
06922821 7362 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7363 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7364}
7365
acbec814 7366static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7367 struct intel_crtc_state *pipe_config)
acbec814
JB
7368{
7369 struct drm_device *dev = crtc->base.dev;
fac5e23e 7370 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7371 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7372 struct dpll clock;
acbec814 7373 u32 mdiv;
662c6ecb 7374 int refclk = 100000;
acbec814 7375
b521973b
VS
7376 /* In case of DSI, DPLL will not be used */
7377 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7378 return;
7379
a580516d 7380 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7381 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7382 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7383
7384 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7385 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7386 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7387 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7388 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7389
dccbea3b 7390 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7391}
7392
5724dbd1
DL
7393static void
7394i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7395 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7396{
7397 struct drm_device *dev = crtc->base.dev;
fac5e23e 7398 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7399 u32 val, base, offset;
7400 int pipe = crtc->pipe, plane = crtc->plane;
7401 int fourcc, pixel_format;
6761dd31 7402 unsigned int aligned_height;
b113d5ee 7403 struct drm_framebuffer *fb;
1b842c89 7404 struct intel_framebuffer *intel_fb;
1ad292b5 7405
42a7b088
DL
7406 val = I915_READ(DSPCNTR(plane));
7407 if (!(val & DISPLAY_PLANE_ENABLE))
7408 return;
7409
d9806c9f 7410 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7411 if (!intel_fb) {
1ad292b5
JB
7412 DRM_DEBUG_KMS("failed to alloc fb\n");
7413 return;
7414 }
7415
1b842c89
DL
7416 fb = &intel_fb->base;
7417
d2e9f5fc
VS
7418 fb->dev = dev;
7419
6315b5d3 7420 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7421 if (val & DISPPLANE_TILED) {
49af449b 7422 plane_config->tiling = I915_TILING_X;
bae781b2 7423 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7424 }
7425 }
1ad292b5
JB
7426
7427 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7428 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7429 fb->format = drm_format_info(fourcc);
1ad292b5 7430
6315b5d3 7431 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7432 if (plane_config->tiling)
1ad292b5
JB
7433 offset = I915_READ(DSPTILEOFF(plane));
7434 else
7435 offset = I915_READ(DSPLINOFF(plane));
7436 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7437 } else {
7438 base = I915_READ(DSPADDR(plane));
7439 }
7440 plane_config->base = base;
7441
7442 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7443 fb->width = ((val >> 16) & 0xfff) + 1;
7444 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7445
7446 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7447 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7448
d88c4afd 7449 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7450
f37b5c2b 7451 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7452
2844a921
DL
7453 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7454 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7455 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7456 plane_config->size);
1ad292b5 7457
2d14030b 7458 plane_config->fb = intel_fb;
1ad292b5
JB
7459}
7460
70b23a98 7461static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7462 struct intel_crtc_state *pipe_config)
70b23a98
VS
7463{
7464 struct drm_device *dev = crtc->base.dev;
fac5e23e 7465 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7466 int pipe = pipe_config->cpu_transcoder;
7467 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7468 struct dpll clock;
0d7b6b11 7469 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7470 int refclk = 100000;
7471
b521973b
VS
7472 /* In case of DSI, DPLL will not be used */
7473 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7474 return;
7475
a580516d 7476 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7477 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7478 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7479 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7480 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7481 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7482 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7483
7484 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7485 clock.m2 = (pll_dw0 & 0xff) << 22;
7486 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7487 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7488 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7489 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7490 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7491
dccbea3b 7492 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7493}
7494
0e8ffe1b 7495static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7496 struct intel_crtc_state *pipe_config)
0e8ffe1b 7497{
6315b5d3 7498 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7499 enum intel_display_power_domain power_domain;
0e8ffe1b 7500 uint32_t tmp;
1729050e 7501 bool ret;
0e8ffe1b 7502
1729050e
ID
7503 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7504 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7505 return false;
7506
e143a21c 7507 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7508 pipe_config->shared_dpll = NULL;
eccb140b 7509
1729050e
ID
7510 ret = false;
7511
0e8ffe1b
DV
7512 tmp = I915_READ(PIPECONF(crtc->pipe));
7513 if (!(tmp & PIPECONF_ENABLE))
1729050e 7514 goto out;
0e8ffe1b 7515
9beb5fea
TU
7516 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7517 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7518 switch (tmp & PIPECONF_BPC_MASK) {
7519 case PIPECONF_6BPC:
7520 pipe_config->pipe_bpp = 18;
7521 break;
7522 case PIPECONF_8BPC:
7523 pipe_config->pipe_bpp = 24;
7524 break;
7525 case PIPECONF_10BPC:
7526 pipe_config->pipe_bpp = 30;
7527 break;
7528 default:
7529 break;
7530 }
7531 }
7532
920a14b2 7533 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7534 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7535 pipe_config->limited_color_range = true;
7536
6315b5d3 7537 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7538 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7539
1bd1bd80 7540 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7541 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7542
2fa2fe9a
DV
7543 i9xx_get_pfit_config(crtc, pipe_config);
7544
6315b5d3 7545 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7546 /* No way to read it out on pipes B and C */
920a14b2 7547 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7548 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7549 else
7550 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7551 pipe_config->pixel_multiplier =
7552 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7553 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7554 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7555 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7556 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7557 tmp = I915_READ(DPLL(crtc->pipe));
7558 pipe_config->pixel_multiplier =
7559 ((tmp & SDVO_MULTIPLIER_MASK)
7560 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7561 } else {
7562 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7563 * port and will be fixed up in the encoder->get_config
7564 * function. */
7565 pipe_config->pixel_multiplier = 1;
7566 }
8bcc2795 7567 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7568 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7569 /*
7570 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7571 * on 830. Filter it out here so that we don't
7572 * report errors due to that.
7573 */
50a0bc90 7574 if (IS_I830(dev_priv))
1c4e0274
VS
7575 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7576
8bcc2795
DV
7577 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7578 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7579 } else {
7580 /* Mask out read-only status bits. */
7581 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7582 DPLL_PORTC_READY_MASK |
7583 DPLL_PORTB_READY_MASK);
8bcc2795 7584 }
6c49f241 7585
920a14b2 7586 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7587 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7588 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7589 vlv_crtc_clock_get(crtc, pipe_config);
7590 else
7591 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7592
0f64614d
VS
7593 /*
7594 * Normally the dotclock is filled in by the encoder .get_config()
7595 * but in case the pipe is enabled w/o any ports we need a sane
7596 * default.
7597 */
7598 pipe_config->base.adjusted_mode.crtc_clock =
7599 pipe_config->port_clock / pipe_config->pixel_multiplier;
7600
1729050e
ID
7601 ret = true;
7602
7603out:
7604 intel_display_power_put(dev_priv, power_domain);
7605
7606 return ret;
0e8ffe1b
DV
7607}
7608
c39055b0 7609static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7610{
13d83a67 7611 struct intel_encoder *encoder;
1c1a24d2 7612 int i;
74cfd7ac 7613 u32 val, final;
13d83a67 7614 bool has_lvds = false;
199e5d79 7615 bool has_cpu_edp = false;
199e5d79 7616 bool has_panel = false;
99eb6a01
KP
7617 bool has_ck505 = false;
7618 bool can_ssc = false;
1c1a24d2 7619 bool using_ssc_source = false;
13d83a67
JB
7620
7621 /* We need to take the global config into account */
c39055b0 7622 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7623 switch (encoder->type) {
7624 case INTEL_OUTPUT_LVDS:
7625 has_panel = true;
7626 has_lvds = true;
7627 break;
7628 case INTEL_OUTPUT_EDP:
7629 has_panel = true;
2de6905f 7630 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7631 has_cpu_edp = true;
7632 break;
6847d71b
PZ
7633 default:
7634 break;
13d83a67
JB
7635 }
7636 }
7637
6e266956 7638 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7639 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7640 can_ssc = has_ck505;
7641 } else {
7642 has_ck505 = false;
7643 can_ssc = true;
7644 }
7645
1c1a24d2
L
7646 /* Check if any DPLLs are using the SSC source */
7647 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7648 u32 temp = I915_READ(PCH_DPLL(i));
7649
7650 if (!(temp & DPLL_VCO_ENABLE))
7651 continue;
7652
7653 if ((temp & PLL_REF_INPUT_MASK) ==
7654 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7655 using_ssc_source = true;
7656 break;
7657 }
7658 }
7659
7660 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7661 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7662
7663 /* Ironlake: try to setup display ref clock before DPLL
7664 * enabling. This is only under driver's control after
7665 * PCH B stepping, previous chipset stepping should be
7666 * ignoring this setting.
7667 */
74cfd7ac
CW
7668 val = I915_READ(PCH_DREF_CONTROL);
7669
7670 /* As we must carefully and slowly disable/enable each source in turn,
7671 * compute the final state we want first and check if we need to
7672 * make any changes at all.
7673 */
7674 final = val;
7675 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7676 if (has_ck505)
7677 final |= DREF_NONSPREAD_CK505_ENABLE;
7678 else
7679 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7680
8c07eb68 7681 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7682 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7683 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7684
7685 if (has_panel) {
7686 final |= DREF_SSC_SOURCE_ENABLE;
7687
7688 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7689 final |= DREF_SSC1_ENABLE;
7690
7691 if (has_cpu_edp) {
7692 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7693 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7694 else
7695 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7696 } else
7697 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7698 } else if (using_ssc_source) {
7699 final |= DREF_SSC_SOURCE_ENABLE;
7700 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7701 }
7702
7703 if (final == val)
7704 return;
7705
13d83a67 7706 /* Always enable nonspread source */
74cfd7ac 7707 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7708
99eb6a01 7709 if (has_ck505)
74cfd7ac 7710 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7711 else
74cfd7ac 7712 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7713
199e5d79 7714 if (has_panel) {
74cfd7ac
CW
7715 val &= ~DREF_SSC_SOURCE_MASK;
7716 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7717
199e5d79 7718 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7719 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7720 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7721 val |= DREF_SSC1_ENABLE;
e77166b5 7722 } else
74cfd7ac 7723 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7724
7725 /* Get SSC going before enabling the outputs */
74cfd7ac 7726 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7727 POSTING_READ(PCH_DREF_CONTROL);
7728 udelay(200);
7729
74cfd7ac 7730 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7731
7732 /* Enable CPU source on CPU attached eDP */
199e5d79 7733 if (has_cpu_edp) {
99eb6a01 7734 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7735 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7736 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7737 } else
74cfd7ac 7738 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7739 } else
74cfd7ac 7740 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7741
74cfd7ac 7742 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7743 POSTING_READ(PCH_DREF_CONTROL);
7744 udelay(200);
7745 } else {
1c1a24d2 7746 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7747
74cfd7ac 7748 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7749
7750 /* Turn off CPU output */
74cfd7ac 7751 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7752
74cfd7ac 7753 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7754 POSTING_READ(PCH_DREF_CONTROL);
7755 udelay(200);
7756
1c1a24d2
L
7757 if (!using_ssc_source) {
7758 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7759
1c1a24d2
L
7760 /* Turn off the SSC source */
7761 val &= ~DREF_SSC_SOURCE_MASK;
7762 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7763
1c1a24d2
L
7764 /* Turn off SSC1 */
7765 val &= ~DREF_SSC1_ENABLE;
7766
7767 I915_WRITE(PCH_DREF_CONTROL, val);
7768 POSTING_READ(PCH_DREF_CONTROL);
7769 udelay(200);
7770 }
13d83a67 7771 }
74cfd7ac
CW
7772
7773 BUG_ON(val != final);
13d83a67
JB
7774}
7775
f31f2d55 7776static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7777{
f31f2d55 7778 uint32_t tmp;
dde86e2d 7779
0ff066a9
PZ
7780 tmp = I915_READ(SOUTH_CHICKEN2);
7781 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7782 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7783
cf3598c2
ID
7784 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7785 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7786 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7787
0ff066a9
PZ
7788 tmp = I915_READ(SOUTH_CHICKEN2);
7789 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7790 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7791
cf3598c2
ID
7792 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7793 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7794 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7795}
7796
7797/* WaMPhyProgramming:hsw */
7798static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7799{
7800 uint32_t tmp;
dde86e2d
PZ
7801
7802 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7803 tmp &= ~(0xFF << 24);
7804 tmp |= (0x12 << 24);
7805 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7806
dde86e2d
PZ
7807 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7808 tmp |= (1 << 11);
7809 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7810
7811 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7812 tmp |= (1 << 11);
7813 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7814
dde86e2d
PZ
7815 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7816 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7817 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7818
7819 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7820 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7821 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7822
0ff066a9
PZ
7823 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7824 tmp &= ~(7 << 13);
7825 tmp |= (5 << 13);
7826 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7827
0ff066a9
PZ
7828 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7829 tmp &= ~(7 << 13);
7830 tmp |= (5 << 13);
7831 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7832
7833 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7834 tmp &= ~0xFF;
7835 tmp |= 0x1C;
7836 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7837
7838 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7839 tmp &= ~0xFF;
7840 tmp |= 0x1C;
7841 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7842
7843 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7844 tmp &= ~(0xFF << 16);
7845 tmp |= (0x1C << 16);
7846 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7847
7848 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7849 tmp &= ~(0xFF << 16);
7850 tmp |= (0x1C << 16);
7851 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7852
0ff066a9
PZ
7853 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7854 tmp |= (1 << 27);
7855 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7856
0ff066a9
PZ
7857 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7858 tmp |= (1 << 27);
7859 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7860
0ff066a9
PZ
7861 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7862 tmp &= ~(0xF << 28);
7863 tmp |= (4 << 28);
7864 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7865
0ff066a9
PZ
7866 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7867 tmp &= ~(0xF << 28);
7868 tmp |= (4 << 28);
7869 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7870}
7871
2fa86a1f
PZ
7872/* Implements 3 different sequences from BSpec chapter "Display iCLK
7873 * Programming" based on the parameters passed:
7874 * - Sequence to enable CLKOUT_DP
7875 * - Sequence to enable CLKOUT_DP without spread
7876 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7877 */
c39055b0
ACO
7878static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7879 bool with_spread, bool with_fdi)
f31f2d55 7880{
2fa86a1f
PZ
7881 uint32_t reg, tmp;
7882
7883 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7884 with_spread = true;
4f8036a2
TU
7885 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7886 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7887 with_fdi = false;
f31f2d55 7888
a580516d 7889 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7890
7891 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7892 tmp &= ~SBI_SSCCTL_DISABLE;
7893 tmp |= SBI_SSCCTL_PATHALT;
7894 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7895
7896 udelay(24);
7897
2fa86a1f
PZ
7898 if (with_spread) {
7899 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7900 tmp &= ~SBI_SSCCTL_PATHALT;
7901 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7902
2fa86a1f
PZ
7903 if (with_fdi) {
7904 lpt_reset_fdi_mphy(dev_priv);
7905 lpt_program_fdi_mphy(dev_priv);
7906 }
7907 }
dde86e2d 7908
4f8036a2 7909 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7910 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7911 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7912 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7913
a580516d 7914 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7915}
7916
47701c3b 7917/* Sequence to disable CLKOUT_DP */
c39055b0 7918static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7919{
47701c3b
PZ
7920 uint32_t reg, tmp;
7921
a580516d 7922 mutex_lock(&dev_priv->sb_lock);
47701c3b 7923
4f8036a2 7924 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7925 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7926 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7927 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7928
7929 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7930 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7931 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7932 tmp |= SBI_SSCCTL_PATHALT;
7933 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7934 udelay(32);
7935 }
7936 tmp |= SBI_SSCCTL_DISABLE;
7937 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7938 }
7939
a580516d 7940 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7941}
7942
f7be2c21
VS
7943#define BEND_IDX(steps) ((50 + (steps)) / 5)
7944
7945static const uint16_t sscdivintphase[] = {
7946 [BEND_IDX( 50)] = 0x3B23,
7947 [BEND_IDX( 45)] = 0x3B23,
7948 [BEND_IDX( 40)] = 0x3C23,
7949 [BEND_IDX( 35)] = 0x3C23,
7950 [BEND_IDX( 30)] = 0x3D23,
7951 [BEND_IDX( 25)] = 0x3D23,
7952 [BEND_IDX( 20)] = 0x3E23,
7953 [BEND_IDX( 15)] = 0x3E23,
7954 [BEND_IDX( 10)] = 0x3F23,
7955 [BEND_IDX( 5)] = 0x3F23,
7956 [BEND_IDX( 0)] = 0x0025,
7957 [BEND_IDX( -5)] = 0x0025,
7958 [BEND_IDX(-10)] = 0x0125,
7959 [BEND_IDX(-15)] = 0x0125,
7960 [BEND_IDX(-20)] = 0x0225,
7961 [BEND_IDX(-25)] = 0x0225,
7962 [BEND_IDX(-30)] = 0x0325,
7963 [BEND_IDX(-35)] = 0x0325,
7964 [BEND_IDX(-40)] = 0x0425,
7965 [BEND_IDX(-45)] = 0x0425,
7966 [BEND_IDX(-50)] = 0x0525,
7967};
7968
7969/*
7970 * Bend CLKOUT_DP
7971 * steps -50 to 50 inclusive, in steps of 5
7972 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7973 * change in clock period = -(steps / 10) * 5.787 ps
7974 */
7975static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7976{
7977 uint32_t tmp;
7978 int idx = BEND_IDX(steps);
7979
7980 if (WARN_ON(steps % 5 != 0))
7981 return;
7982
7983 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7984 return;
7985
7986 mutex_lock(&dev_priv->sb_lock);
7987
7988 if (steps % 10 != 0)
7989 tmp = 0xAAAAAAAB;
7990 else
7991 tmp = 0x00000000;
7992 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7993
7994 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7995 tmp &= 0xffff0000;
7996 tmp |= sscdivintphase[idx];
7997 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7998
7999 mutex_unlock(&dev_priv->sb_lock);
8000}
8001
8002#undef BEND_IDX
8003
c39055b0 8004static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 8005{
bf8fa3d3
PZ
8006 struct intel_encoder *encoder;
8007 bool has_vga = false;
8008
c39055b0 8009 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
8010 switch (encoder->type) {
8011 case INTEL_OUTPUT_ANALOG:
8012 has_vga = true;
8013 break;
6847d71b
PZ
8014 default:
8015 break;
bf8fa3d3
PZ
8016 }
8017 }
8018
f7be2c21 8019 if (has_vga) {
c39055b0
ACO
8020 lpt_bend_clkout_dp(dev_priv, 0);
8021 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 8022 } else {
c39055b0 8023 lpt_disable_clkout_dp(dev_priv);
f7be2c21 8024 }
bf8fa3d3
PZ
8025}
8026
dde86e2d
PZ
8027/*
8028 * Initialize reference clocks when the driver loads
8029 */
c39055b0 8030void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8031{
6e266956 8032 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8033 ironlake_init_pch_refclk(dev_priv);
6e266956 8034 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8035 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8036}
8037
6ff93609 8038static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8039{
fac5e23e 8040 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8042 int pipe = intel_crtc->pipe;
c8203565
PZ
8043 uint32_t val;
8044
78114071 8045 val = 0;
c8203565 8046
6e3c9717 8047 switch (intel_crtc->config->pipe_bpp) {
c8203565 8048 case 18:
dfd07d72 8049 val |= PIPECONF_6BPC;
c8203565
PZ
8050 break;
8051 case 24:
dfd07d72 8052 val |= PIPECONF_8BPC;
c8203565
PZ
8053 break;
8054 case 30:
dfd07d72 8055 val |= PIPECONF_10BPC;
c8203565
PZ
8056 break;
8057 case 36:
dfd07d72 8058 val |= PIPECONF_12BPC;
c8203565
PZ
8059 break;
8060 default:
cc769b62
PZ
8061 /* Case prevented by intel_choose_pipe_bpp_dither. */
8062 BUG();
c8203565
PZ
8063 }
8064
6e3c9717 8065 if (intel_crtc->config->dither)
c8203565
PZ
8066 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8067
6e3c9717 8068 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8069 val |= PIPECONF_INTERLACED_ILK;
8070 else
8071 val |= PIPECONF_PROGRESSIVE;
8072
6e3c9717 8073 if (intel_crtc->config->limited_color_range)
3685a8f3 8074 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8075
c8203565
PZ
8076 I915_WRITE(PIPECONF(pipe), val);
8077 POSTING_READ(PIPECONF(pipe));
8078}
8079
6ff93609 8080static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8081{
fac5e23e 8082 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8084 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8085 u32 val = 0;
ee2b0b38 8086
391bf048 8087 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8088 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8089
6e3c9717 8090 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8091 val |= PIPECONF_INTERLACED_ILK;
8092 else
8093 val |= PIPECONF_PROGRESSIVE;
8094
702e7a56
PZ
8095 I915_WRITE(PIPECONF(cpu_transcoder), val);
8096 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8097}
8098
391bf048
JN
8099static void haswell_set_pipemisc(struct drm_crtc *crtc)
8100{
fac5e23e 8101 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8103
391bf048
JN
8104 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8105 u32 val = 0;
756f85cf 8106
6e3c9717 8107 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8108 case 18:
8109 val |= PIPEMISC_DITHER_6_BPC;
8110 break;
8111 case 24:
8112 val |= PIPEMISC_DITHER_8_BPC;
8113 break;
8114 case 30:
8115 val |= PIPEMISC_DITHER_10_BPC;
8116 break;
8117 case 36:
8118 val |= PIPEMISC_DITHER_12_BPC;
8119 break;
8120 default:
8121 /* Case prevented by pipe_config_set_bpp. */
8122 BUG();
8123 }
8124
6e3c9717 8125 if (intel_crtc->config->dither)
756f85cf
PZ
8126 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8127
391bf048 8128 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8129 }
ee2b0b38
PZ
8130}
8131
d4b1931c
PZ
8132int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8133{
8134 /*
8135 * Account for spread spectrum to avoid
8136 * oversubscribing the link. Max center spread
8137 * is 2.5%; use 5% for safety's sake.
8138 */
8139 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8140 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8141}
8142
7429e9d4 8143static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8144{
7429e9d4 8145 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8146}
8147
b75ca6f6
ACO
8148static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8149 struct intel_crtc_state *crtc_state,
9e2c8475 8150 struct dpll *reduced_clock)
79e53945 8151{
de13a2e3 8152 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8153 struct drm_device *dev = crtc->dev;
fac5e23e 8154 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8155 u32 dpll, fp, fp2;
3d6e9ee0 8156 int factor;
79e53945 8157
c1858123 8158 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8159 factor = 21;
3d6e9ee0 8160 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8161 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8162 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8163 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8164 factor = 25;
190f68c5 8165 } else if (crtc_state->sdvo_tv_clock)
8febb297 8166 factor = 20;
c1858123 8167
b75ca6f6
ACO
8168 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8169
190f68c5 8170 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8171 fp |= FP_CB_TUNE;
8172
8173 if (reduced_clock) {
8174 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8175
b75ca6f6
ACO
8176 if (reduced_clock->m < factor * reduced_clock->n)
8177 fp2 |= FP_CB_TUNE;
8178 } else {
8179 fp2 = fp;
8180 }
9a7c7890 8181
5eddb70b 8182 dpll = 0;
2c07245f 8183
3d6e9ee0 8184 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8185 dpll |= DPLLB_MODE_LVDS;
8186 else
8187 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8188
190f68c5 8189 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8190 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8191
3d6e9ee0
VS
8192 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8193 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8194 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8195
37a5650b 8196 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8197 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8198
7d7f8633
VS
8199 /*
8200 * The high speed IO clock is only really required for
8201 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8202 * possible to share the DPLL between CRT and HDMI. Enabling
8203 * the clock needlessly does no real harm, except use up a
8204 * bit of power potentially.
8205 *
8206 * We'll limit this to IVB with 3 pipes, since it has only two
8207 * DPLLs and so DPLL sharing is the only way to get three pipes
8208 * driving PCH ports at the same time. On SNB we could do this,
8209 * and potentially avoid enabling the second DPLL, but it's not
8210 * clear if it''s a win or loss power wise. No point in doing
8211 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8212 */
8213 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8214 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8215 dpll |= DPLL_SDVO_HIGH_SPEED;
8216
a07d6787 8217 /* compute bitmask from p1 value */
190f68c5 8218 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8219 /* also FPA1 */
190f68c5 8220 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8221
190f68c5 8222 switch (crtc_state->dpll.p2) {
a07d6787
EA
8223 case 5:
8224 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8225 break;
8226 case 7:
8227 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8228 break;
8229 case 10:
8230 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8231 break;
8232 case 14:
8233 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8234 break;
79e53945
JB
8235 }
8236
3d6e9ee0
VS
8237 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8238 intel_panel_use_ssc(dev_priv))
43565a06 8239 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8240 else
8241 dpll |= PLL_REF_INPUT_DREFCLK;
8242
b75ca6f6
ACO
8243 dpll |= DPLL_VCO_ENABLE;
8244
8245 crtc_state->dpll_hw_state.dpll = dpll;
8246 crtc_state->dpll_hw_state.fp0 = fp;
8247 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8248}
8249
190f68c5
ACO
8250static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8251 struct intel_crtc_state *crtc_state)
de13a2e3 8252{
997c030c 8253 struct drm_device *dev = crtc->base.dev;
fac5e23e 8254 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 8255 struct dpll reduced_clock;
7ed9f894 8256 bool has_reduced_clock = false;
e2b78267 8257 struct intel_shared_dpll *pll;
1b6f4958 8258 const struct intel_limit *limit;
997c030c 8259 int refclk = 120000;
de13a2e3 8260
dd3cd74a
ACO
8261 memset(&crtc_state->dpll_hw_state, 0,
8262 sizeof(crtc_state->dpll_hw_state));
8263
ded220e2
ACO
8264 crtc->lowfreq_avail = false;
8265
8266 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8267 if (!crtc_state->has_pch_encoder)
8268 return 0;
79e53945 8269
2d84d2b3 8270 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8271 if (intel_panel_use_ssc(dev_priv)) {
8272 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8273 dev_priv->vbt.lvds_ssc_freq);
8274 refclk = dev_priv->vbt.lvds_ssc_freq;
8275 }
8276
8277 if (intel_is_dual_link_lvds(dev)) {
8278 if (refclk == 100000)
8279 limit = &intel_limits_ironlake_dual_lvds_100m;
8280 else
8281 limit = &intel_limits_ironlake_dual_lvds;
8282 } else {
8283 if (refclk == 100000)
8284 limit = &intel_limits_ironlake_single_lvds_100m;
8285 else
8286 limit = &intel_limits_ironlake_single_lvds;
8287 }
8288 } else {
8289 limit = &intel_limits_ironlake_dac;
8290 }
8291
364ee29d 8292 if (!crtc_state->clock_set &&
997c030c
ACO
8293 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8294 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8295 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8296 return -EINVAL;
f47709a9 8297 }
79e53945 8298
b75ca6f6
ACO
8299 ironlake_compute_dpll(crtc, crtc_state,
8300 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 8301
ded220e2
ACO
8302 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8303 if (pll == NULL) {
8304 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8305 pipe_name(crtc->pipe));
8306 return -EINVAL;
3fb37703 8307 }
79e53945 8308
2d84d2b3 8309 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 8310 has_reduced_clock)
c7653199 8311 crtc->lowfreq_avail = true;
e2b78267 8312
c8f7a0db 8313 return 0;
79e53945
JB
8314}
8315
eb14cb74
VS
8316static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8317 struct intel_link_m_n *m_n)
8318{
8319 struct drm_device *dev = crtc->base.dev;
fac5e23e 8320 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8321 enum pipe pipe = crtc->pipe;
8322
8323 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8324 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8325 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8326 & ~TU_SIZE_MASK;
8327 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8328 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8329 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8330}
8331
8332static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8333 enum transcoder transcoder,
b95af8be
VK
8334 struct intel_link_m_n *m_n,
8335 struct intel_link_m_n *m2_n2)
72419203 8336{
6315b5d3 8337 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8338 enum pipe pipe = crtc->pipe;
72419203 8339
6315b5d3 8340 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8341 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8342 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8343 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8344 & ~TU_SIZE_MASK;
8345 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8346 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8347 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8348 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8349 * gen < 8) and if DRRS is supported (to make sure the
8350 * registers are not unnecessarily read).
8351 */
6315b5d3 8352 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8353 crtc->config->has_drrs) {
b95af8be
VK
8354 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8355 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8356 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8357 & ~TU_SIZE_MASK;
8358 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8359 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8360 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8361 }
eb14cb74
VS
8362 } else {
8363 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8364 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8365 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8366 & ~TU_SIZE_MASK;
8367 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8368 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8369 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8370 }
8371}
8372
8373void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8374 struct intel_crtc_state *pipe_config)
eb14cb74 8375{
681a8504 8376 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8377 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8378 else
8379 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8380 &pipe_config->dp_m_n,
8381 &pipe_config->dp_m2_n2);
eb14cb74 8382}
72419203 8383
eb14cb74 8384static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8385 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8386{
8387 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8388 &pipe_config->fdi_m_n, NULL);
72419203
DV
8389}
8390
bd2e244f 8391static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8392 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8393{
8394 struct drm_device *dev = crtc->base.dev;
fac5e23e 8395 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8396 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8397 uint32_t ps_ctrl = 0;
8398 int id = -1;
8399 int i;
bd2e244f 8400
a1b2278e
CK
8401 /* find scaler attached to this pipe */
8402 for (i = 0; i < crtc->num_scalers; i++) {
8403 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8404 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8405 id = i;
8406 pipe_config->pch_pfit.enabled = true;
8407 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8408 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8409 break;
8410 }
8411 }
bd2e244f 8412
a1b2278e
CK
8413 scaler_state->scaler_id = id;
8414 if (id >= 0) {
8415 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8416 } else {
8417 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8418 }
8419}
8420
5724dbd1
DL
8421static void
8422skylake_get_initial_plane_config(struct intel_crtc *crtc,
8423 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8424{
8425 struct drm_device *dev = crtc->base.dev;
fac5e23e 8426 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8427 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8428 int pipe = crtc->pipe;
8429 int fourcc, pixel_format;
6761dd31 8430 unsigned int aligned_height;
bc8d7dff 8431 struct drm_framebuffer *fb;
1b842c89 8432 struct intel_framebuffer *intel_fb;
bc8d7dff 8433
d9806c9f 8434 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8435 if (!intel_fb) {
bc8d7dff
DL
8436 DRM_DEBUG_KMS("failed to alloc fb\n");
8437 return;
8438 }
8439
1b842c89
DL
8440 fb = &intel_fb->base;
8441
d2e9f5fc
VS
8442 fb->dev = dev;
8443
bc8d7dff 8444 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8445 if (!(val & PLANE_CTL_ENABLE))
8446 goto error;
8447
bc8d7dff
DL
8448 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8449 fourcc = skl_format_to_fourcc(pixel_format,
8450 val & PLANE_CTL_ORDER_RGBX,
8451 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8452 fb->format = drm_format_info(fourcc);
bc8d7dff 8453
40f46283
DL
8454 tiling = val & PLANE_CTL_TILED_MASK;
8455 switch (tiling) {
8456 case PLANE_CTL_TILED_LINEAR:
bae781b2 8457 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
8458 break;
8459 case PLANE_CTL_TILED_X:
8460 plane_config->tiling = I915_TILING_X;
bae781b2 8461 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8462 break;
8463 case PLANE_CTL_TILED_Y:
bae781b2 8464 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8465 break;
8466 case PLANE_CTL_TILED_YF:
bae781b2 8467 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8468 break;
8469 default:
8470 MISSING_CASE(tiling);
8471 goto error;
8472 }
8473
bc8d7dff
DL
8474 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8475 plane_config->base = base;
8476
8477 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8478
8479 val = I915_READ(PLANE_SIZE(pipe, 0));
8480 fb->height = ((val >> 16) & 0xfff) + 1;
8481 fb->width = ((val >> 0) & 0x1fff) + 1;
8482
8483 val = I915_READ(PLANE_STRIDE(pipe, 0));
d88c4afd 8484 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8485 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8486
d88c4afd 8487 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 8488
f37b5c2b 8489 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8490
8491 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8492 pipe_name(pipe), fb->width, fb->height,
272725c7 8493 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8494 plane_config->size);
8495
2d14030b 8496 plane_config->fb = intel_fb;
bc8d7dff
DL
8497 return;
8498
8499error:
d1a3a036 8500 kfree(intel_fb);
bc8d7dff
DL
8501}
8502
2fa2fe9a 8503static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8504 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8505{
8506 struct drm_device *dev = crtc->base.dev;
fac5e23e 8507 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8508 uint32_t tmp;
8509
8510 tmp = I915_READ(PF_CTL(crtc->pipe));
8511
8512 if (tmp & PF_ENABLE) {
fd4daa9c 8513 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8514 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8515 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8516
8517 /* We currently do not free assignements of panel fitters on
8518 * ivb/hsw (since we don't use the higher upscaling modes which
8519 * differentiates them) so just WARN about this case for now. */
5db94019 8520 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8521 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8522 PF_PIPE_SEL_IVB(crtc->pipe));
8523 }
2fa2fe9a 8524 }
79e53945
JB
8525}
8526
5724dbd1
DL
8527static void
8528ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8529 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8530{
8531 struct drm_device *dev = crtc->base.dev;
fac5e23e 8532 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8533 u32 val, base, offset;
aeee5a49 8534 int pipe = crtc->pipe;
4c6baa59 8535 int fourcc, pixel_format;
6761dd31 8536 unsigned int aligned_height;
b113d5ee 8537 struct drm_framebuffer *fb;
1b842c89 8538 struct intel_framebuffer *intel_fb;
4c6baa59 8539
42a7b088
DL
8540 val = I915_READ(DSPCNTR(pipe));
8541 if (!(val & DISPLAY_PLANE_ENABLE))
8542 return;
8543
d9806c9f 8544 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8545 if (!intel_fb) {
4c6baa59
JB
8546 DRM_DEBUG_KMS("failed to alloc fb\n");
8547 return;
8548 }
8549
1b842c89
DL
8550 fb = &intel_fb->base;
8551
d2e9f5fc
VS
8552 fb->dev = dev;
8553
6315b5d3 8554 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8555 if (val & DISPPLANE_TILED) {
49af449b 8556 plane_config->tiling = I915_TILING_X;
bae781b2 8557 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8558 }
8559 }
4c6baa59
JB
8560
8561 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8562 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8563 fb->format = drm_format_info(fourcc);
4c6baa59 8564
aeee5a49 8565 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8566 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8567 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8568 } else {
49af449b 8569 if (plane_config->tiling)
aeee5a49 8570 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8571 else
aeee5a49 8572 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8573 }
8574 plane_config->base = base;
8575
8576 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8577 fb->width = ((val >> 16) & 0xfff) + 1;
8578 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8579
8580 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8581 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8582
d88c4afd 8583 aligned_height = intel_fb_align_height(fb, 0, fb->height);
4c6baa59 8584
f37b5c2b 8585 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8586
2844a921
DL
8587 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8588 pipe_name(pipe), fb->width, fb->height,
272725c7 8589 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8590 plane_config->size);
b113d5ee 8591
2d14030b 8592 plane_config->fb = intel_fb;
4c6baa59
JB
8593}
8594
0e8ffe1b 8595static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8596 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8597{
8598 struct drm_device *dev = crtc->base.dev;
fac5e23e 8599 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8600 enum intel_display_power_domain power_domain;
0e8ffe1b 8601 uint32_t tmp;
1729050e 8602 bool ret;
0e8ffe1b 8603
1729050e
ID
8604 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8605 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8606 return false;
8607
e143a21c 8608 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8609 pipe_config->shared_dpll = NULL;
eccb140b 8610
1729050e 8611 ret = false;
0e8ffe1b
DV
8612 tmp = I915_READ(PIPECONF(crtc->pipe));
8613 if (!(tmp & PIPECONF_ENABLE))
1729050e 8614 goto out;
0e8ffe1b 8615
42571aef
VS
8616 switch (tmp & PIPECONF_BPC_MASK) {
8617 case PIPECONF_6BPC:
8618 pipe_config->pipe_bpp = 18;
8619 break;
8620 case PIPECONF_8BPC:
8621 pipe_config->pipe_bpp = 24;
8622 break;
8623 case PIPECONF_10BPC:
8624 pipe_config->pipe_bpp = 30;
8625 break;
8626 case PIPECONF_12BPC:
8627 pipe_config->pipe_bpp = 36;
8628 break;
8629 default:
8630 break;
8631 }
8632
b5a9fa09
DV
8633 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8634 pipe_config->limited_color_range = true;
8635
ab9412ba 8636 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8637 struct intel_shared_dpll *pll;
8106ddbd 8638 enum intel_dpll_id pll_id;
66e985c0 8639
88adfff1
DV
8640 pipe_config->has_pch_encoder = true;
8641
627eb5a3
DV
8642 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8643 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8644 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8645
8646 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8647
2d1fe073 8648 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8649 /*
8650 * The pipe->pch transcoder and pch transcoder->pll
8651 * mapping is fixed.
8652 */
8106ddbd 8653 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8654 } else {
8655 tmp = I915_READ(PCH_DPLL_SEL);
8656 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8657 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8658 else
8106ddbd 8659 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8660 }
66e985c0 8661
8106ddbd
ACO
8662 pipe_config->shared_dpll =
8663 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8664 pll = pipe_config->shared_dpll;
66e985c0 8665
2edd6443
ACO
8666 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8667 &pipe_config->dpll_hw_state));
c93f54cf
DV
8668
8669 tmp = pipe_config->dpll_hw_state.dpll;
8670 pipe_config->pixel_multiplier =
8671 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8672 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8673
8674 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8675 } else {
8676 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8677 }
8678
1bd1bd80 8679 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8680 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8681
2fa2fe9a
DV
8682 ironlake_get_pfit_config(crtc, pipe_config);
8683
1729050e
ID
8684 ret = true;
8685
8686out:
8687 intel_display_power_put(dev_priv, power_domain);
8688
8689 return ret;
0e8ffe1b
DV
8690}
8691
be256dc7
PZ
8692static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8693{
91c8a326 8694 struct drm_device *dev = &dev_priv->drm;
be256dc7 8695 struct intel_crtc *crtc;
be256dc7 8696
d3fcc808 8697 for_each_intel_crtc(dev, crtc)
e2c719b7 8698 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8699 pipe_name(crtc->pipe));
8700
e2c719b7
RC
8701 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8702 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8703 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8704 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8705 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8706 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8707 "CPU PWM1 enabled\n");
772c2a51 8708 if (IS_HASWELL(dev_priv))
e2c719b7 8709 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8710 "CPU PWM2 enabled\n");
e2c719b7 8711 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8712 "PCH PWM1 enabled\n");
e2c719b7 8713 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8714 "Utility pin enabled\n");
e2c719b7 8715 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8716
9926ada1
PZ
8717 /*
8718 * In theory we can still leave IRQs enabled, as long as only the HPD
8719 * interrupts remain enabled. We used to check for that, but since it's
8720 * gen-specific and since we only disable LCPLL after we fully disable
8721 * the interrupts, the check below should be enough.
8722 */
e2c719b7 8723 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8724}
8725
9ccd5aeb
PZ
8726static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8727{
772c2a51 8728 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8729 return I915_READ(D_COMP_HSW);
8730 else
8731 return I915_READ(D_COMP_BDW);
8732}
8733
3c4c9b81
PZ
8734static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8735{
772c2a51 8736 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
8737 mutex_lock(&dev_priv->rps.hw_lock);
8738 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8739 val))
79cf219a 8740 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
8741 mutex_unlock(&dev_priv->rps.hw_lock);
8742 } else {
9ccd5aeb
PZ
8743 I915_WRITE(D_COMP_BDW, val);
8744 POSTING_READ(D_COMP_BDW);
3c4c9b81 8745 }
be256dc7
PZ
8746}
8747
8748/*
8749 * This function implements pieces of two sequences from BSpec:
8750 * - Sequence for display software to disable LCPLL
8751 * - Sequence for display software to allow package C8+
8752 * The steps implemented here are just the steps that actually touch the LCPLL
8753 * register. Callers should take care of disabling all the display engine
8754 * functions, doing the mode unset, fixing interrupts, etc.
8755 */
6ff58d53
PZ
8756static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8757 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8758{
8759 uint32_t val;
8760
8761 assert_can_disable_lcpll(dev_priv);
8762
8763 val = I915_READ(LCPLL_CTL);
8764
8765 if (switch_to_fclk) {
8766 val |= LCPLL_CD_SOURCE_FCLK;
8767 I915_WRITE(LCPLL_CTL, val);
8768
f53dd63f
ID
8769 if (wait_for_us(I915_READ(LCPLL_CTL) &
8770 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8771 DRM_ERROR("Switching to FCLK failed\n");
8772
8773 val = I915_READ(LCPLL_CTL);
8774 }
8775
8776 val |= LCPLL_PLL_DISABLE;
8777 I915_WRITE(LCPLL_CTL, val);
8778 POSTING_READ(LCPLL_CTL);
8779
24d8441d 8780 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8781 DRM_ERROR("LCPLL still locked\n");
8782
9ccd5aeb 8783 val = hsw_read_dcomp(dev_priv);
be256dc7 8784 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8785 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8786 ndelay(100);
8787
9ccd5aeb
PZ
8788 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8789 1))
be256dc7
PZ
8790 DRM_ERROR("D_COMP RCOMP still in progress\n");
8791
8792 if (allow_power_down) {
8793 val = I915_READ(LCPLL_CTL);
8794 val |= LCPLL_POWER_DOWN_ALLOW;
8795 I915_WRITE(LCPLL_CTL, val);
8796 POSTING_READ(LCPLL_CTL);
8797 }
8798}
8799
8800/*
8801 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8802 * source.
8803 */
6ff58d53 8804static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8805{
8806 uint32_t val;
8807
8808 val = I915_READ(LCPLL_CTL);
8809
8810 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8811 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8812 return;
8813
a8a8bd54
PZ
8814 /*
8815 * Make sure we're not on PC8 state before disabling PC8, otherwise
8816 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8817 */
59bad947 8818 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8819
be256dc7
PZ
8820 if (val & LCPLL_POWER_DOWN_ALLOW) {
8821 val &= ~LCPLL_POWER_DOWN_ALLOW;
8822 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8823 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8824 }
8825
9ccd5aeb 8826 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8827 val |= D_COMP_COMP_FORCE;
8828 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8829 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8830
8831 val = I915_READ(LCPLL_CTL);
8832 val &= ~LCPLL_PLL_DISABLE;
8833 I915_WRITE(LCPLL_CTL, val);
8834
93220c08
CW
8835 if (intel_wait_for_register(dev_priv,
8836 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8837 5))
be256dc7
PZ
8838 DRM_ERROR("LCPLL not locked yet\n");
8839
8840 if (val & LCPLL_CD_SOURCE_FCLK) {
8841 val = I915_READ(LCPLL_CTL);
8842 val &= ~LCPLL_CD_SOURCE_FCLK;
8843 I915_WRITE(LCPLL_CTL, val);
8844
f53dd63f
ID
8845 if (wait_for_us((I915_READ(LCPLL_CTL) &
8846 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8847 DRM_ERROR("Switching back to LCPLL failed\n");
8848 }
215733fa 8849
59bad947 8850 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8851 intel_update_cdclk(dev_priv);
be256dc7
PZ
8852}
8853
765dab67
PZ
8854/*
8855 * Package states C8 and deeper are really deep PC states that can only be
8856 * reached when all the devices on the system allow it, so even if the graphics
8857 * device allows PC8+, it doesn't mean the system will actually get to these
8858 * states. Our driver only allows PC8+ when going into runtime PM.
8859 *
8860 * The requirements for PC8+ are that all the outputs are disabled, the power
8861 * well is disabled and most interrupts are disabled, and these are also
8862 * requirements for runtime PM. When these conditions are met, we manually do
8863 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8864 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8865 * hang the machine.
8866 *
8867 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8868 * the state of some registers, so when we come back from PC8+ we need to
8869 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8870 * need to take care of the registers kept by RC6. Notice that this happens even
8871 * if we don't put the device in PCI D3 state (which is what currently happens
8872 * because of the runtime PM support).
8873 *
8874 * For more, read "Display Sequences for Package C8" on the hardware
8875 * documentation.
8876 */
a14cb6fc 8877void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8878{
c67a470b
PZ
8879 uint32_t val;
8880
c67a470b
PZ
8881 DRM_DEBUG_KMS("Enabling package C8+\n");
8882
4f8036a2 8883 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8884 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8885 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8886 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8887 }
8888
c39055b0 8889 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8890 hsw_disable_lcpll(dev_priv, true, true);
8891}
8892
a14cb6fc 8893void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8894{
c67a470b
PZ
8895 uint32_t val;
8896
c67a470b
PZ
8897 DRM_DEBUG_KMS("Disabling package C8+\n");
8898
8899 hsw_restore_lcpll(dev_priv);
c39055b0 8900 lpt_init_pch_refclk(dev_priv);
c67a470b 8901
4f8036a2 8902 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8903 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8904 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8905 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8906 }
c67a470b
PZ
8907}
8908
190f68c5
ACO
8909static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8910 struct intel_crtc_state *crtc_state)
09b4ddf9 8911{
d7edc4e5 8912 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
8913 if (!intel_ddi_pll_select(crtc, crtc_state))
8914 return -EINVAL;
8915 }
716c2e55 8916
c7653199 8917 crtc->lowfreq_avail = false;
644cef34 8918
c8f7a0db 8919 return 0;
79e53945
JB
8920}
8921
3760b59c
S
8922static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8923 enum port port,
8924 struct intel_crtc_state *pipe_config)
8925{
8106ddbd
ACO
8926 enum intel_dpll_id id;
8927
3760b59c
S
8928 switch (port) {
8929 case PORT_A:
08250c4b 8930 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8931 break;
8932 case PORT_B:
08250c4b 8933 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8934 break;
8935 case PORT_C:
08250c4b 8936 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8937 break;
8938 default:
8939 DRM_ERROR("Incorrect port type\n");
8106ddbd 8940 return;
3760b59c 8941 }
8106ddbd
ACO
8942
8943 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8944}
8945
96b7dfb7
S
8946static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8947 enum port port,
5cec258b 8948 struct intel_crtc_state *pipe_config)
96b7dfb7 8949{
8106ddbd 8950 enum intel_dpll_id id;
a3c988ea 8951 u32 temp;
96b7dfb7
S
8952
8953 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8954 id = temp >> (port * 3 + 1);
96b7dfb7 8955
c856052a 8956 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8957 return;
8106ddbd
ACO
8958
8959 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8960}
8961
7d2c8175
DL
8962static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8963 enum port port,
5cec258b 8964 struct intel_crtc_state *pipe_config)
7d2c8175 8965{
8106ddbd 8966 enum intel_dpll_id id;
c856052a 8967 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8968
c856052a 8969 switch (ddi_pll_sel) {
7d2c8175 8970 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8971 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8972 break;
8973 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8974 id = DPLL_ID_WRPLL2;
7d2c8175 8975 break;
00490c22 8976 case PORT_CLK_SEL_SPLL:
8106ddbd 8977 id = DPLL_ID_SPLL;
79bd23da 8978 break;
9d16da65
ACO
8979 case PORT_CLK_SEL_LCPLL_810:
8980 id = DPLL_ID_LCPLL_810;
8981 break;
8982 case PORT_CLK_SEL_LCPLL_1350:
8983 id = DPLL_ID_LCPLL_1350;
8984 break;
8985 case PORT_CLK_SEL_LCPLL_2700:
8986 id = DPLL_ID_LCPLL_2700;
8987 break;
8106ddbd 8988 default:
c856052a 8989 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
8990 /* fall through */
8991 case PORT_CLK_SEL_NONE:
8106ddbd 8992 return;
7d2c8175 8993 }
8106ddbd
ACO
8994
8995 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
8996}
8997
cf30429e
JN
8998static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
8999 struct intel_crtc_state *pipe_config,
d8fc70b7 9000 u64 *power_domain_mask)
cf30429e
JN
9001{
9002 struct drm_device *dev = crtc->base.dev;
fac5e23e 9003 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9004 enum intel_display_power_domain power_domain;
9005 u32 tmp;
9006
d9a7bc67
ID
9007 /*
9008 * The pipe->transcoder mapping is fixed with the exception of the eDP
9009 * transcoder handled below.
9010 */
cf30429e
JN
9011 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9012
9013 /*
9014 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9015 * consistency and less surprising code; it's in always on power).
9016 */
9017 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9018 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9019 enum pipe trans_edp_pipe;
9020 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9021 default:
9022 WARN(1, "unknown pipe linked to edp transcoder\n");
9023 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9024 case TRANS_DDI_EDP_INPUT_A_ON:
9025 trans_edp_pipe = PIPE_A;
9026 break;
9027 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9028 trans_edp_pipe = PIPE_B;
9029 break;
9030 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9031 trans_edp_pipe = PIPE_C;
9032 break;
9033 }
9034
9035 if (trans_edp_pipe == crtc->pipe)
9036 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9037 }
9038
9039 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9040 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9041 return false;
d8fc70b7 9042 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9043
9044 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9045
9046 return tmp & PIPECONF_ENABLE;
9047}
9048
4d1de975
JN
9049static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9050 struct intel_crtc_state *pipe_config,
d8fc70b7 9051 u64 *power_domain_mask)
4d1de975
JN
9052{
9053 struct drm_device *dev = crtc->base.dev;
fac5e23e 9054 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9055 enum intel_display_power_domain power_domain;
9056 enum port port;
9057 enum transcoder cpu_transcoder;
9058 u32 tmp;
9059
4d1de975
JN
9060 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9061 if (port == PORT_A)
9062 cpu_transcoder = TRANSCODER_DSI_A;
9063 else
9064 cpu_transcoder = TRANSCODER_DSI_C;
9065
9066 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9067 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9068 continue;
d8fc70b7 9069 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9070
db18b6a6
ID
9071 /*
9072 * The PLL needs to be enabled with a valid divider
9073 * configuration, otherwise accessing DSI registers will hang
9074 * the machine. See BSpec North Display Engine
9075 * registers/MIPI[BXT]. We can break out here early, since we
9076 * need the same DSI PLL to be enabled for both DSI ports.
9077 */
9078 if (!intel_dsi_pll_is_enabled(dev_priv))
9079 break;
9080
4d1de975
JN
9081 /* XXX: this works for video mode only */
9082 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9083 if (!(tmp & DPI_ENABLE))
9084 continue;
9085
9086 tmp = I915_READ(MIPI_CTRL(port));
9087 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9088 continue;
9089
9090 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9091 break;
9092 }
9093
d7edc4e5 9094 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9095}
9096
26804afd 9097static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9098 struct intel_crtc_state *pipe_config)
26804afd 9099{
6315b5d3 9100 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9101 struct intel_shared_dpll *pll;
26804afd
DV
9102 enum port port;
9103 uint32_t tmp;
9104
9105 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9106
9107 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9108
b976dc53 9109 if (IS_GEN9_BC(dev_priv))
96b7dfb7 9110 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9111 else if (IS_GEN9_LP(dev_priv))
3760b59c 9112 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9113 else
9114 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9115
8106ddbd
ACO
9116 pll = pipe_config->shared_dpll;
9117 if (pll) {
2edd6443
ACO
9118 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9119 &pipe_config->dpll_hw_state));
d452c5b6
DV
9120 }
9121
26804afd
DV
9122 /*
9123 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9124 * DDI E. So just check whether this pipe is wired to DDI E and whether
9125 * the PCH transcoder is on.
9126 */
6315b5d3 9127 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9128 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9129 pipe_config->has_pch_encoder = true;
9130
9131 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9132 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9133 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9134
9135 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9136 }
9137}
9138
0e8ffe1b 9139static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9140 struct intel_crtc_state *pipe_config)
0e8ffe1b 9141{
6315b5d3 9142 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9143 enum intel_display_power_domain power_domain;
d8fc70b7 9144 u64 power_domain_mask;
cf30429e 9145 bool active;
0e8ffe1b 9146
1729050e
ID
9147 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9148 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9149 return false;
d8fc70b7 9150 power_domain_mask = BIT_ULL(power_domain);
1729050e 9151
8106ddbd 9152 pipe_config->shared_dpll = NULL;
c0d43d62 9153
cf30429e 9154 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9155
cc3f90f0 9156 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9157 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9158 WARN_ON(active);
9159 active = true;
4d1de975
JN
9160 }
9161
cf30429e 9162 if (!active)
1729050e 9163 goto out;
0e8ffe1b 9164
d7edc4e5 9165 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9166 haswell_get_ddi_port_state(crtc, pipe_config);
9167 intel_get_pipe_timings(crtc, pipe_config);
9168 }
627eb5a3 9169
bc58be60 9170 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9171
05dc698c
LL
9172 pipe_config->gamma_mode =
9173 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9174
6315b5d3 9175 if (INTEL_GEN(dev_priv) >= 9) {
1c74eeaf 9176 intel_crtc_init_scalers(crtc, pipe_config);
a1b2278e 9177
af99ceda
CK
9178 pipe_config->scaler_state.scaler_id = -1;
9179 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9180 }
9181
1729050e
ID
9182 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9183 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9184 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9185 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9186 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9187 else
1c132b44 9188 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9189 }
88adfff1 9190
772c2a51 9191 if (IS_HASWELL(dev_priv))
e59150dc
JB
9192 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9193 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9194
4d1de975
JN
9195 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9196 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9197 pipe_config->pixel_multiplier =
9198 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9199 } else {
9200 pipe_config->pixel_multiplier = 1;
9201 }
6c49f241 9202
1729050e
ID
9203out:
9204 for_each_power_domain(power_domain, power_domain_mask)
9205 intel_display_power_put(dev_priv, power_domain);
9206
cf30429e 9207 return active;
0e8ffe1b
DV
9208}
9209
292889e1
VS
9210static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9211 const struct intel_plane_state *plane_state)
9212{
9213 unsigned int width = plane_state->base.crtc_w;
9214 unsigned int stride = roundup_pow_of_two(width) * 4;
9215
9216 switch (stride) {
9217 default:
9218 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9219 width, stride);
9220 stride = 256;
9221 /* fallthrough */
9222 case 256:
9223 case 512:
9224 case 1024:
9225 case 2048:
9226 break;
9227 }
9228
9229 return CURSOR_ENABLE |
9230 CURSOR_GAMMA_ENABLE |
9231 CURSOR_FORMAT_ARGB |
9232 CURSOR_STRIDE(stride);
9233}
9234
55a08b3f 9235static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
292889e1 9236 const struct intel_crtc_state *crtc_state,
55a08b3f 9237 const struct intel_plane_state *plane_state)
560b85bb
CW
9238{
9239 struct drm_device *dev = crtc->dev;
fac5e23e 9240 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 9241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9242 uint32_t cntl = 0, size = 0;
560b85bb 9243
936e71e3 9244 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9245 unsigned int width = plane_state->base.crtc_w;
9246 unsigned int height = plane_state->base.crtc_h;
dc41c154 9247
292889e1 9248 cntl = i845_cursor_ctl(crtc_state, plane_state);
dc41c154 9249 size = (height << 12) | width;
4b0e333e 9250 }
560b85bb 9251
dc41c154
VS
9252 if (intel_crtc->cursor_cntl != 0 &&
9253 (intel_crtc->cursor_base != base ||
9254 intel_crtc->cursor_size != size ||
9255 intel_crtc->cursor_cntl != cntl)) {
9256 /* On these chipsets we can only modify the base/size/stride
9257 * whilst the cursor is disabled.
9258 */
dd584fc0
VS
9259 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
9260 POSTING_READ_FW(CURCNTR(PIPE_A));
dc41c154 9261 intel_crtc->cursor_cntl = 0;
4b0e333e 9262 }
560b85bb 9263
99d1f387 9264 if (intel_crtc->cursor_base != base) {
dd584fc0 9265 I915_WRITE_FW(CURBASE(PIPE_A), base);
99d1f387
VS
9266 intel_crtc->cursor_base = base;
9267 }
4726e0b0 9268
dc41c154 9269 if (intel_crtc->cursor_size != size) {
dd584fc0 9270 I915_WRITE_FW(CURSIZE, size);
dc41c154 9271 intel_crtc->cursor_size = size;
4b0e333e 9272 }
560b85bb 9273
4b0e333e 9274 if (intel_crtc->cursor_cntl != cntl) {
dd584fc0
VS
9275 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
9276 POSTING_READ_FW(CURCNTR(PIPE_A));
4b0e333e 9277 intel_crtc->cursor_cntl = cntl;
560b85bb 9278 }
560b85bb
CW
9279}
9280
292889e1
VS
9281static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9282 const struct intel_plane_state *plane_state)
9283{
9284 struct drm_i915_private *dev_priv =
9285 to_i915(plane_state->base.plane->dev);
9286 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
9287 enum pipe pipe = crtc->pipe;
9288 u32 cntl;
9289
9290 cntl = MCURSOR_GAMMA_ENABLE;
9291
9292 if (HAS_DDI(dev_priv))
9293 cntl |= CURSOR_PIPE_CSC_ENABLE;
9294
9295 cntl |= pipe << 28; /* Connect to correct pipe */
9296
9297 switch (plane_state->base.crtc_w) {
9298 case 64:
9299 cntl |= CURSOR_MODE_64_ARGB_AX;
9300 break;
9301 case 128:
9302 cntl |= CURSOR_MODE_128_ARGB_AX;
9303 break;
9304 case 256:
9305 cntl |= CURSOR_MODE_256_ARGB_AX;
9306 break;
9307 default:
9308 MISSING_CASE(plane_state->base.crtc_w);
9309 return 0;
9310 }
9311
9312 if (plane_state->base.rotation & DRM_ROTATE_180)
9313 cntl |= CURSOR_ROTATE_180;
9314
9315 return cntl;
9316}
9317
55a08b3f 9318static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
292889e1 9319 const struct intel_crtc_state *crtc_state,
55a08b3f 9320 const struct intel_plane_state *plane_state)
65a21cd6
JB
9321{
9322 struct drm_device *dev = crtc->dev;
fac5e23e 9323 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
9324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9325 int pipe = intel_crtc->pipe;
663f3122 9326 uint32_t cntl = 0;
4b0e333e 9327
292889e1
VS
9328 if (plane_state && plane_state->base.visible)
9329 cntl = i9xx_cursor_ctl(crtc_state, plane_state);
4398ad45 9330
4b0e333e 9331 if (intel_crtc->cursor_cntl != cntl) {
dd584fc0
VS
9332 I915_WRITE_FW(CURCNTR(pipe), cntl);
9333 POSTING_READ_FW(CURCNTR(pipe));
4b0e333e 9334 intel_crtc->cursor_cntl = cntl;
65a21cd6 9335 }
4b0e333e 9336
65a21cd6 9337 /* and commit changes on next vblank */
dd584fc0
VS
9338 I915_WRITE_FW(CURBASE(pipe), base);
9339 POSTING_READ_FW(CURBASE(pipe));
99d1f387
VS
9340
9341 intel_crtc->cursor_base = base;
65a21cd6
JB
9342}
9343
cda4b7d3 9344/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 9345static void intel_crtc_update_cursor(struct drm_crtc *crtc,
292889e1 9346 const struct intel_crtc_state *crtc_state,
55a08b3f 9347 const struct intel_plane_state *plane_state)
cda4b7d3
CW
9348{
9349 struct drm_device *dev = crtc->dev;
fac5e23e 9350 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
9351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9352 int pipe = intel_crtc->pipe;
55a08b3f 9353 u32 base = intel_crtc->cursor_addr;
dd584fc0 9354 unsigned long irqflags;
55a08b3f 9355 u32 pos = 0;
cda4b7d3 9356
55a08b3f
ML
9357 if (plane_state) {
9358 int x = plane_state->base.crtc_x;
9359 int y = plane_state->base.crtc_y;
cda4b7d3 9360
55a08b3f
ML
9361 if (x < 0) {
9362 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9363 x = -x;
9364 }
9365 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 9366
55a08b3f
ML
9367 if (y < 0) {
9368 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9369 y = -y;
9370 }
9371 pos |= y << CURSOR_Y_SHIFT;
9372
9373 /* ILK+ do this automagically */
49cff963 9374 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 9375 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
9376 base += (plane_state->base.crtc_h *
9377 plane_state->base.crtc_w - 1) * 4;
9378 }
cda4b7d3 9379 }
cda4b7d3 9380
dd584fc0
VS
9381 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9382
9383 I915_WRITE_FW(CURPOS(pipe), pos);
5efb3e28 9384
2a307c2e 9385 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
292889e1 9386 i845_update_cursor(crtc, base, crtc_state, plane_state);
5efb3e28 9387 else
292889e1 9388 i9xx_update_cursor(crtc, base, crtc_state, plane_state);
dd584fc0
VS
9389
9390 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
cda4b7d3
CW
9391}
9392
50a0bc90 9393static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
9394 uint32_t width, uint32_t height)
9395{
9396 if (width == 0 || height == 0)
9397 return false;
9398
9399 /*
9400 * 845g/865g are special in that they are only limited by
9401 * the width of their cursors, the height is arbitrary up to
9402 * the precision of the register. Everything else requires
9403 * square cursors, limited to a few power-of-two sizes.
9404 */
2a307c2e 9405 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
9406 if ((width & 63) != 0)
9407 return false;
9408
2a307c2e 9409 if (width > (IS_I845G(dev_priv) ? 64 : 512))
dc41c154
VS
9410 return false;
9411
9412 if (height > 1023)
9413 return false;
9414 } else {
9415 switch (width | height) {
9416 case 256:
9417 case 128:
50a0bc90 9418 if (IS_GEN2(dev_priv))
dc41c154
VS
9419 return false;
9420 case 64:
9421 break;
9422 default:
9423 return false;
9424 }
9425 }
9426
9427 return true;
9428}
9429
79e53945
JB
9430/* VESA 640x480x72Hz mode to set on the pipe */
9431static struct drm_display_mode load_detect_mode = {
9432 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9433 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9434};
9435
a8bb6818 9436struct drm_framebuffer *
24dbf51a
CW
9437intel_framebuffer_create(struct drm_i915_gem_object *obj,
9438 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9439{
9440 struct intel_framebuffer *intel_fb;
9441 int ret;
9442
9443 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9444 if (!intel_fb)
d2dff872 9445 return ERR_PTR(-ENOMEM);
d2dff872 9446
24dbf51a 9447 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9448 if (ret)
9449 goto err;
d2dff872
CW
9450
9451 return &intel_fb->base;
dcb1394e 9452
dd4916c5 9453err:
dd4916c5 9454 kfree(intel_fb);
dd4916c5 9455 return ERR_PTR(ret);
d2dff872
CW
9456}
9457
9458static u32
9459intel_framebuffer_pitch_for_width(int width, int bpp)
9460{
9461 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9462 return ALIGN(pitch, 64);
9463}
9464
9465static u32
9466intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9467{
9468 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9469 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9470}
9471
9472static struct drm_framebuffer *
9473intel_framebuffer_create_for_mode(struct drm_device *dev,
9474 struct drm_display_mode *mode,
9475 int depth, int bpp)
9476{
dcb1394e 9477 struct drm_framebuffer *fb;
d2dff872 9478 struct drm_i915_gem_object *obj;
0fed39bd 9479 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9480
12d79d78 9481 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9482 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9483 if (IS_ERR(obj))
9484 return ERR_CAST(obj);
d2dff872
CW
9485
9486 mode_cmd.width = mode->hdisplay;
9487 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9488 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9489 bpp);
5ca0c34a 9490 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9491
24dbf51a 9492 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9493 if (IS_ERR(fb))
f0cd5182 9494 i915_gem_object_put(obj);
dcb1394e
LW
9495
9496 return fb;
d2dff872
CW
9497}
9498
9499static struct drm_framebuffer *
9500mode_fits_in_fbdev(struct drm_device *dev,
9501 struct drm_display_mode *mode)
9502{
0695726e 9503#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9504 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9505 struct drm_i915_gem_object *obj;
9506 struct drm_framebuffer *fb;
9507
4c0e5528 9508 if (!dev_priv->fbdev)
d2dff872
CW
9509 return NULL;
9510
4c0e5528 9511 if (!dev_priv->fbdev->fb)
d2dff872
CW
9512 return NULL;
9513
4c0e5528
DV
9514 obj = dev_priv->fbdev->fb->obj;
9515 BUG_ON(!obj);
9516
8bcd4553 9517 fb = &dev_priv->fbdev->fb->base;
01f2c773 9518 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9519 fb->format->cpp[0] * 8))
d2dff872
CW
9520 return NULL;
9521
01f2c773 9522 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9523 return NULL;
9524
edde3617 9525 drm_framebuffer_reference(fb);
d2dff872 9526 return fb;
4520f53a
DV
9527#else
9528 return NULL;
9529#endif
d2dff872
CW
9530}
9531
d3a40d1b
ACO
9532static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9533 struct drm_crtc *crtc,
9534 struct drm_display_mode *mode,
9535 struct drm_framebuffer *fb,
9536 int x, int y)
9537{
9538 struct drm_plane_state *plane_state;
9539 int hdisplay, vdisplay;
9540 int ret;
9541
9542 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9543 if (IS_ERR(plane_state))
9544 return PTR_ERR(plane_state);
9545
9546 if (mode)
196cd5d3 9547 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9548 else
9549 hdisplay = vdisplay = 0;
9550
9551 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9552 if (ret)
9553 return ret;
9554 drm_atomic_set_fb_for_plane(plane_state, fb);
9555 plane_state->crtc_x = 0;
9556 plane_state->crtc_y = 0;
9557 plane_state->crtc_w = hdisplay;
9558 plane_state->crtc_h = vdisplay;
9559 plane_state->src_x = x << 16;
9560 plane_state->src_y = y << 16;
9561 plane_state->src_w = hdisplay << 16;
9562 plane_state->src_h = vdisplay << 16;
9563
9564 return 0;
9565}
9566
d2434ab7 9567bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9568 struct drm_display_mode *mode,
51fd371b
RC
9569 struct intel_load_detect_pipe *old,
9570 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9571{
9572 struct intel_crtc *intel_crtc;
d2434ab7
DV
9573 struct intel_encoder *intel_encoder =
9574 intel_attached_encoder(connector);
79e53945 9575 struct drm_crtc *possible_crtc;
4ef69c7a 9576 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9577 struct drm_crtc *crtc = NULL;
9578 struct drm_device *dev = encoder->dev;
0f0f74bc 9579 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9580 struct drm_framebuffer *fb;
51fd371b 9581 struct drm_mode_config *config = &dev->mode_config;
edde3617 9582 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9583 struct drm_connector_state *connector_state;
4be07317 9584 struct intel_crtc_state *crtc_state;
51fd371b 9585 int ret, i = -1;
79e53945 9586
d2dff872 9587 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9588 connector->base.id, connector->name,
8e329a03 9589 encoder->base.id, encoder->name);
d2dff872 9590
edde3617
ML
9591 old->restore_state = NULL;
9592
51fd371b
RC
9593retry:
9594 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9595 if (ret)
ad3c558f 9596 goto fail;
6e9f798d 9597
79e53945
JB
9598 /*
9599 * Algorithm gets a little messy:
7a5e4805 9600 *
79e53945
JB
9601 * - if the connector already has an assigned crtc, use it (but make
9602 * sure it's on first)
7a5e4805 9603 *
79e53945
JB
9604 * - try to find the first unused crtc that can drive this connector,
9605 * and use that if we find one
79e53945
JB
9606 */
9607
9608 /* See if we already have a CRTC for this connector */
edde3617
ML
9609 if (connector->state->crtc) {
9610 crtc = connector->state->crtc;
8261b191 9611
51fd371b 9612 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9613 if (ret)
ad3c558f 9614 goto fail;
8261b191
CW
9615
9616 /* Make sure the crtc and connector are running */
edde3617 9617 goto found;
79e53945
JB
9618 }
9619
9620 /* Find an unused one (if possible) */
70e1e0ec 9621 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9622 i++;
9623 if (!(encoder->possible_crtcs & (1 << i)))
9624 continue;
edde3617
ML
9625
9626 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9627 if (ret)
9628 goto fail;
9629
9630 if (possible_crtc->state->enable) {
9631 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9632 continue;
edde3617 9633 }
a459249c
VS
9634
9635 crtc = possible_crtc;
9636 break;
79e53945
JB
9637 }
9638
9639 /*
9640 * If we didn't find an unused CRTC, don't use any.
9641 */
9642 if (!crtc) {
7173188d 9643 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 9644 goto fail;
79e53945
JB
9645 }
9646
edde3617
ML
9647found:
9648 intel_crtc = to_intel_crtc(crtc);
9649
4d02e2de
DV
9650 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9651 if (ret)
ad3c558f 9652 goto fail;
79e53945 9653
83a57153 9654 state = drm_atomic_state_alloc(dev);
edde3617
ML
9655 restore_state = drm_atomic_state_alloc(dev);
9656 if (!state || !restore_state) {
9657 ret = -ENOMEM;
9658 goto fail;
9659 }
83a57153
ACO
9660
9661 state->acquire_ctx = ctx;
edde3617 9662 restore_state->acquire_ctx = ctx;
83a57153 9663
944b0c76
ACO
9664 connector_state = drm_atomic_get_connector_state(state, connector);
9665 if (IS_ERR(connector_state)) {
9666 ret = PTR_ERR(connector_state);
9667 goto fail;
9668 }
9669
edde3617
ML
9670 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9671 if (ret)
9672 goto fail;
944b0c76 9673
4be07317
ACO
9674 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9675 if (IS_ERR(crtc_state)) {
9676 ret = PTR_ERR(crtc_state);
9677 goto fail;
9678 }
9679
49d6fa21 9680 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9681
6492711d
CW
9682 if (!mode)
9683 mode = &load_detect_mode;
79e53945 9684
d2dff872
CW
9685 /* We need a framebuffer large enough to accommodate all accesses
9686 * that the plane may generate whilst we perform load detection.
9687 * We can not rely on the fbcon either being present (we get called
9688 * during its initialisation to detect all boot displays, or it may
9689 * not even exist) or that it is large enough to satisfy the
9690 * requested mode.
9691 */
94352cf9
DV
9692 fb = mode_fits_in_fbdev(dev, mode);
9693 if (fb == NULL) {
d2dff872 9694 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9695 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9696 } else
9697 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9698 if (IS_ERR(fb)) {
d2dff872 9699 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9700 goto fail;
79e53945 9701 }
79e53945 9702
d3a40d1b
ACO
9703 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9704 if (ret)
9705 goto fail;
9706
edde3617
ML
9707 drm_framebuffer_unreference(fb);
9708
9709 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9710 if (ret)
9711 goto fail;
9712
9713 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9714 if (!ret)
9715 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9716 if (!ret)
9717 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9718 if (ret) {
9719 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9720 goto fail;
9721 }
8c7b5ccb 9722
3ba86073
ML
9723 ret = drm_atomic_commit(state);
9724 if (ret) {
6492711d 9725 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9726 goto fail;
79e53945 9727 }
edde3617
ML
9728
9729 old->restore_state = restore_state;
7abbd11f 9730 drm_atomic_state_put(state);
7173188d 9731
79e53945 9732 /* let the connector get through one full cycle before testing */
0f0f74bc 9733 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9734 return true;
412b61d8 9735
ad3c558f 9736fail:
7fb71c8f
CW
9737 if (state) {
9738 drm_atomic_state_put(state);
9739 state = NULL;
9740 }
9741 if (restore_state) {
9742 drm_atomic_state_put(restore_state);
9743 restore_state = NULL;
9744 }
83a57153 9745
51fd371b
RC
9746 if (ret == -EDEADLK) {
9747 drm_modeset_backoff(ctx);
9748 goto retry;
9749 }
9750
412b61d8 9751 return false;
79e53945
JB
9752}
9753
d2434ab7 9754void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9755 struct intel_load_detect_pipe *old,
9756 struct drm_modeset_acquire_ctx *ctx)
79e53945 9757{
d2434ab7
DV
9758 struct intel_encoder *intel_encoder =
9759 intel_attached_encoder(connector);
4ef69c7a 9760 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 9761 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 9762 int ret;
79e53945 9763
d2dff872 9764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9765 connector->base.id, connector->name,
8e329a03 9766 encoder->base.id, encoder->name);
d2dff872 9767
edde3617 9768 if (!state)
0622a53c 9769 return;
79e53945 9770
581e49fe 9771 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 9772 if (ret)
edde3617 9773 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 9774 drm_atomic_state_put(state);
79e53945
JB
9775}
9776
da4a1efa 9777static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9778 const struct intel_crtc_state *pipe_config)
da4a1efa 9779{
fac5e23e 9780 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
9781 u32 dpll = pipe_config->dpll_hw_state.dpll;
9782
9783 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9784 return dev_priv->vbt.lvds_ssc_freq;
6e266956 9785 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 9786 return 120000;
5db94019 9787 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
9788 return 96000;
9789 else
9790 return 48000;
9791}
9792
79e53945 9793/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9794static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9795 struct intel_crtc_state *pipe_config)
79e53945 9796{
f1f644dc 9797 struct drm_device *dev = crtc->base.dev;
fac5e23e 9798 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 9799 int pipe = pipe_config->cpu_transcoder;
293623f7 9800 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 9801 u32 fp;
9e2c8475 9802 struct dpll clock;
dccbea3b 9803 int port_clock;
da4a1efa 9804 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9805
9806 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9807 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9808 else
293623f7 9809 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9810
9811 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 9812 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
9813 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9814 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9815 } else {
9816 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9817 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9818 }
9819
5db94019 9820 if (!IS_GEN2(dev_priv)) {
9b1e14f4 9821 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
9822 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9823 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9824 else
9825 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9826 DPLL_FPA01_P1_POST_DIV_SHIFT);
9827
9828 switch (dpll & DPLL_MODE_MASK) {
9829 case DPLLB_MODE_DAC_SERIAL:
9830 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9831 5 : 10;
9832 break;
9833 case DPLLB_MODE_LVDS:
9834 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9835 7 : 14;
9836 break;
9837 default:
28c97730 9838 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9839 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9840 return;
79e53945
JB
9841 }
9842
9b1e14f4 9843 if (IS_PINEVIEW(dev_priv))
dccbea3b 9844 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 9845 else
dccbea3b 9846 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 9847 } else {
50a0bc90 9848 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 9849 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
9850
9851 if (is_lvds) {
9852 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9853 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
9854
9855 if (lvds & LVDS_CLKB_POWER_UP)
9856 clock.p2 = 7;
9857 else
9858 clock.p2 = 14;
79e53945
JB
9859 } else {
9860 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9861 clock.p1 = 2;
9862 else {
9863 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9864 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9865 }
9866 if (dpll & PLL_P2_DIVIDE_BY_4)
9867 clock.p2 = 4;
9868 else
9869 clock.p2 = 2;
79e53945 9870 }
da4a1efa 9871
dccbea3b 9872 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
9873 }
9874
18442d08
VS
9875 /*
9876 * This value includes pixel_multiplier. We will use
241bfc38 9877 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
9878 * encoder's get_config() function.
9879 */
dccbea3b 9880 pipe_config->port_clock = port_clock;
f1f644dc
JB
9881}
9882
6878da05
VS
9883int intel_dotclock_calculate(int link_freq,
9884 const struct intel_link_m_n *m_n)
f1f644dc 9885{
f1f644dc
JB
9886 /*
9887 * The calculation for the data clock is:
1041a02f 9888 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 9889 * But we want to avoid losing precison if possible, so:
1041a02f 9890 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
9891 *
9892 * and the link clock is simpler:
1041a02f 9893 * link_clock = (m * link_clock) / n
f1f644dc
JB
9894 */
9895
6878da05
VS
9896 if (!m_n->link_n)
9897 return 0;
f1f644dc 9898
6878da05
VS
9899 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9900}
f1f644dc 9901
18442d08 9902static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 9903 struct intel_crtc_state *pipe_config)
6878da05 9904{
e3b247da 9905 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 9906
18442d08
VS
9907 /* read out port_clock from the DPLL */
9908 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9909
f1f644dc 9910 /*
e3b247da
VS
9911 * In case there is an active pipe without active ports,
9912 * we may need some idea for the dotclock anyway.
9913 * Calculate one based on the FDI configuration.
79e53945 9914 */
2d112de7 9915 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 9916 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 9917 &pipe_config->fdi_m_n);
79e53945
JB
9918}
9919
9920/** Returns the currently programmed mode of the given pipe. */
9921struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9922 struct drm_crtc *crtc)
9923{
fac5e23e 9924 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 9925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9926 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 9927 struct drm_display_mode *mode;
3f36b937 9928 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
9929 int htot = I915_READ(HTOTAL(cpu_transcoder));
9930 int hsync = I915_READ(HSYNC(cpu_transcoder));
9931 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9932 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9933 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9934
9935 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9936 if (!mode)
9937 return NULL;
9938
3f36b937
TU
9939 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9940 if (!pipe_config) {
9941 kfree(mode);
9942 return NULL;
9943 }
9944
f1f644dc
JB
9945 /*
9946 * Construct a pipe_config sufficient for getting the clock info
9947 * back out of crtc_clock_get.
9948 *
9949 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9950 * to use a real value here instead.
9951 */
3f36b937
TU
9952 pipe_config->cpu_transcoder = (enum transcoder) pipe;
9953 pipe_config->pixel_multiplier = 1;
9954 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9955 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9956 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9957 i9xx_crtc_clock_get(intel_crtc, pipe_config);
9958
9959 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
9960 mode->hdisplay = (htot & 0xffff) + 1;
9961 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9962 mode->hsync_start = (hsync & 0xffff) + 1;
9963 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9964 mode->vdisplay = (vtot & 0xffff) + 1;
9965 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9966 mode->vsync_start = (vsync & 0xffff) + 1;
9967 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9968
9969 drm_mode_set_name(mode);
79e53945 9970
3f36b937
TU
9971 kfree(pipe_config);
9972
79e53945
JB
9973 return mode;
9974}
9975
9976static void intel_crtc_destroy(struct drm_crtc *crtc)
9977{
9978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 9979 struct drm_device *dev = crtc->dev;
51cbaf01 9980 struct intel_flip_work *work;
67e77c5a 9981
5e2d7afc 9982 spin_lock_irq(&dev->event_lock);
5a21b665
DV
9983 work = intel_crtc->flip_work;
9984 intel_crtc->flip_work = NULL;
9985 spin_unlock_irq(&dev->event_lock);
67e77c5a 9986
5a21b665 9987 if (work) {
51cbaf01
ML
9988 cancel_work_sync(&work->mmio_work);
9989 cancel_work_sync(&work->unpin_work);
5a21b665 9990 kfree(work);
67e77c5a 9991 }
79e53945
JB
9992
9993 drm_crtc_cleanup(crtc);
67e77c5a 9994
79e53945
JB
9995 kfree(intel_crtc);
9996}
9997
6b95a207
KH
9998static void intel_unpin_work_fn(struct work_struct *__work)
9999{
51cbaf01
ML
10000 struct intel_flip_work *work =
10001 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
DV
10002 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10003 struct drm_device *dev = crtc->base.dev;
10004 struct drm_plane *primary = crtc->base.primary;
03f476e1 10005
5a21b665
DV
10006 if (is_mmio_work(work))
10007 flush_work(&work->mmio_work);
03f476e1 10008
5a21b665 10009 mutex_lock(&dev->struct_mutex);
be1e3415 10010 intel_unpin_fb_vma(work->old_vma);
f8c417cd 10011 i915_gem_object_put(work->pending_flip_obj);
5a21b665 10012 mutex_unlock(&dev->struct_mutex);
143f73b3 10013
e8a261ea
CW
10014 i915_gem_request_put(work->flip_queued_req);
10015
5748b6a1
CW
10016 intel_frontbuffer_flip_complete(to_i915(dev),
10017 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
DV
10018 intel_fbc_post_update(crtc);
10019 drm_framebuffer_unreference(work->old_fb);
143f73b3 10020
5a21b665
DV
10021 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10022 atomic_dec(&crtc->unpin_work_count);
a6747b73 10023
5a21b665
DV
10024 kfree(work);
10025}
d9e86c0e 10026
5a21b665
DV
10027/* Is 'a' after or equal to 'b'? */
10028static bool g4x_flip_count_after_eq(u32 a, u32 b)
10029{
10030 return !((a - b) & 0x80000000);
10031}
143f73b3 10032
5a21b665
DV
10033static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10034 struct intel_flip_work *work)
10035{
10036 struct drm_device *dev = crtc->base.dev;
fac5e23e 10037 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 10038
8af29b0c 10039 if (abort_flip_on_reset(crtc))
5a21b665 10040 return true;
143f73b3 10041
5a21b665
DV
10042 /*
10043 * The relevant registers doen't exist on pre-ctg.
10044 * As the flip done interrupt doesn't trigger for mmio
10045 * flips on gmch platforms, a flip count check isn't
10046 * really needed there. But since ctg has the registers,
10047 * include it in the check anyway.
10048 */
9beb5fea 10049 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 10050 return true;
b4a98e57 10051
5a21b665
DV
10052 /*
10053 * BDW signals flip done immediately if the plane
10054 * is disabled, even if the plane enable is already
10055 * armed to occur at the next vblank :(
10056 */
f99d7069 10057
5a21b665
DV
10058 /*
10059 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10060 * used the same base address. In that case the mmio flip might
10061 * have completed, but the CS hasn't even executed the flip yet.
10062 *
10063 * A flip count check isn't enough as the CS might have updated
10064 * the base address just after start of vblank, but before we
10065 * managed to process the interrupt. This means we'd complete the
10066 * CS flip too soon.
10067 *
10068 * Combining both checks should get us a good enough result. It may
10069 * still happen that the CS flip has been executed, but has not
10070 * yet actually completed. But in case the base address is the same
10071 * anyway, we don't really care.
10072 */
10073 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10074 crtc->flip_work->gtt_offset &&
10075 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10076 crtc->flip_work->flip_count);
10077}
b4a98e57 10078
5a21b665
DV
10079static bool
10080__pageflip_finished_mmio(struct intel_crtc *crtc,
10081 struct intel_flip_work *work)
10082{
10083 /*
10084 * MMIO work completes when vblank is different from
10085 * flip_queued_vblank.
10086 *
10087 * Reset counter value doesn't matter, this is handled by
10088 * i915_wait_request finishing early, so no need to handle
10089 * reset here.
10090 */
10091 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
10092}
10093
51cbaf01
ML
10094
10095static bool pageflip_finished(struct intel_crtc *crtc,
10096 struct intel_flip_work *work)
10097{
10098 if (!atomic_read(&work->pending))
10099 return false;
10100
10101 smp_rmb();
10102
5a21b665
DV
10103 if (is_mmio_work(work))
10104 return __pageflip_finished_mmio(crtc, work);
10105 else
10106 return __pageflip_finished_cs(crtc, work);
10107}
10108
10109void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
10110{
91c8a326 10111 struct drm_device *dev = &dev_priv->drm;
98187836 10112 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10113 struct intel_flip_work *work;
10114 unsigned long flags;
10115
10116 /* Ignore early vblank irqs */
10117 if (!crtc)
10118 return;
10119
51cbaf01 10120 /*
5a21b665
DV
10121 * This is called both by irq handlers and the reset code (to complete
10122 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 10123 */
5a21b665 10124 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10125 work = crtc->flip_work;
5a21b665
DV
10126
10127 if (work != NULL &&
10128 !is_mmio_work(work) &&
e2af48c6
VS
10129 pageflip_finished(crtc, work))
10130 page_flip_completed(crtc);
5a21b665
DV
10131
10132 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
10133}
10134
51cbaf01 10135void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 10136{
91c8a326 10137 struct drm_device *dev = &dev_priv->drm;
98187836 10138 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 10139 struct intel_flip_work *work;
6b95a207
KH
10140 unsigned long flags;
10141
5251f04e
ML
10142 /* Ignore early vblank irqs */
10143 if (!crtc)
10144 return;
f326038a
DV
10145
10146 /*
10147 * This is called both by irq handlers and the reset code (to complete
10148 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 10149 */
6b95a207 10150 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 10151 work = crtc->flip_work;
5251f04e 10152
5a21b665
DV
10153 if (work != NULL &&
10154 is_mmio_work(work) &&
e2af48c6
VS
10155 pageflip_finished(crtc, work))
10156 page_flip_completed(crtc);
5251f04e 10157
6b95a207
KH
10158 spin_unlock_irqrestore(&dev->event_lock, flags);
10159}
10160
5a21b665
DV
10161static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
10162 struct intel_flip_work *work)
84c33a64 10163{
5a21b665 10164 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 10165
5a21b665
DV
10166 /* Ensure that the work item is consistent when activating it ... */
10167 smp_mb__before_atomic();
10168 atomic_set(&work->pending, 1);
10169}
a6747b73 10170
5a21b665
DV
10171static int intel_gen2_queue_flip(struct drm_device *dev,
10172 struct drm_crtc *crtc,
10173 struct drm_framebuffer *fb,
10174 struct drm_i915_gem_object *obj,
10175 struct drm_i915_gem_request *req,
10176 uint32_t flags)
10177{
5a21b665 10178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10179 u32 flip_mask, *cs;
143f73b3 10180
73dec95e
TU
10181 cs = intel_ring_begin(req, 6);
10182 if (IS_ERR(cs))
10183 return PTR_ERR(cs);
143f73b3 10184
5a21b665
DV
10185 /* Can't queue multiple flips, so wait for the previous
10186 * one to finish before executing the next.
10187 */
10188 if (intel_crtc->plane)
10189 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10190 else
10191 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10192 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10193 *cs++ = MI_NOOP;
10194 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10195 *cs++ = fb->pitches[0];
10196 *cs++ = intel_crtc->flip_work->gtt_offset;
10197 *cs++ = 0; /* aux display base address, unused */
143f73b3 10198
5a21b665
DV
10199 return 0;
10200}
84c33a64 10201
5a21b665
DV
10202static int intel_gen3_queue_flip(struct drm_device *dev,
10203 struct drm_crtc *crtc,
10204 struct drm_framebuffer *fb,
10205 struct drm_i915_gem_object *obj,
10206 struct drm_i915_gem_request *req,
10207 uint32_t flags)
10208{
5a21b665 10209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10210 u32 flip_mask, *cs;
d55dbd06 10211
73dec95e
TU
10212 cs = intel_ring_begin(req, 6);
10213 if (IS_ERR(cs))
10214 return PTR_ERR(cs);
d55dbd06 10215
5a21b665
DV
10216 if (intel_crtc->plane)
10217 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10218 else
10219 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
73dec95e
TU
10220 *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
10221 *cs++ = MI_NOOP;
10222 *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10223 *cs++ = fb->pitches[0];
10224 *cs++ = intel_crtc->flip_work->gtt_offset;
10225 *cs++ = MI_NOOP;
fd8e058a 10226
5a21b665
DV
10227 return 0;
10228}
84c33a64 10229
5a21b665
DV
10230static int intel_gen4_queue_flip(struct drm_device *dev,
10231 struct drm_crtc *crtc,
10232 struct drm_framebuffer *fb,
10233 struct drm_i915_gem_object *obj,
10234 struct drm_i915_gem_request *req,
10235 uint32_t flags)
10236{
fac5e23e 10237 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10239 u32 pf, pipesrc, *cs;
143f73b3 10240
73dec95e
TU
10241 cs = intel_ring_begin(req, 4);
10242 if (IS_ERR(cs))
10243 return PTR_ERR(cs);
143f73b3 10244
5a21b665
DV
10245 /* i965+ uses the linear or tiled offsets from the
10246 * Display Registers (which do not change across a page-flip)
10247 * so we need only reprogram the base address.
10248 */
73dec95e
TU
10249 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10250 *cs++ = fb->pitches[0];
10251 *cs++ = intel_crtc->flip_work->gtt_offset |
10252 intel_fb_modifier_to_tiling(fb->modifier);
5a21b665
DV
10253
10254 /* XXX Enabling the panel-fitter across page-flip is so far
10255 * untested on non-native modes, so ignore it for now.
10256 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10257 */
10258 pf = 0;
10259 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10260 *cs++ = pf | pipesrc;
143f73b3 10261
5a21b665 10262 return 0;
8c9f3aaf
JB
10263}
10264
5a21b665
DV
10265static int intel_gen6_queue_flip(struct drm_device *dev,
10266 struct drm_crtc *crtc,
10267 struct drm_framebuffer *fb,
10268 struct drm_i915_gem_object *obj,
10269 struct drm_i915_gem_request *req,
10270 uint32_t flags)
da20eabd 10271{
fac5e23e 10272 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10274 u32 pf, pipesrc, *cs;
d21fbe87 10275
73dec95e
TU
10276 cs = intel_ring_begin(req, 4);
10277 if (IS_ERR(cs))
10278 return PTR_ERR(cs);
92826fcd 10279
73dec95e
TU
10280 *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
10281 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10282 *cs++ = intel_crtc->flip_work->gtt_offset;
92826fcd 10283
5a21b665
DV
10284 /* Contrary to the suggestions in the documentation,
10285 * "Enable Panel Fitter" does not seem to be required when page
10286 * flipping with a non-native mode, and worse causes a normal
10287 * modeset to fail.
10288 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10289 */
10290 pf = 0;
10291 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
73dec95e 10292 *cs++ = pf | pipesrc;
7809e5ae 10293
5a21b665 10294 return 0;
7809e5ae
MR
10295}
10296
5a21b665
DV
10297static int intel_gen7_queue_flip(struct drm_device *dev,
10298 struct drm_crtc *crtc,
10299 struct drm_framebuffer *fb,
10300 struct drm_i915_gem_object *obj,
10301 struct drm_i915_gem_request *req,
10302 uint32_t flags)
d21fbe87 10303{
5db94019 10304 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 10305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73dec95e 10306 u32 *cs, plane_bit = 0;
5a21b665 10307 int len, ret;
d21fbe87 10308
5a21b665
DV
10309 switch (intel_crtc->plane) {
10310 case PLANE_A:
10311 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10312 break;
10313 case PLANE_B:
10314 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10315 break;
10316 case PLANE_C:
10317 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10318 break;
10319 default:
10320 WARN_ONCE(1, "unknown plane in flip command\n");
10321 return -ENODEV;
10322 }
10323
10324 len = 4;
b5321f30 10325 if (req->engine->id == RCS) {
5a21b665
DV
10326 len += 6;
10327 /*
10328 * On Gen 8, SRM is now taking an extra dword to accommodate
10329 * 48bits addresses, and we need a NOOP for the batch size to
10330 * stay even.
10331 */
5db94019 10332 if (IS_GEN8(dev_priv))
5a21b665
DV
10333 len += 2;
10334 }
10335
10336 /*
10337 * BSpec MI_DISPLAY_FLIP for IVB:
10338 * "The full packet must be contained within the same cache line."
10339 *
10340 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10341 * cacheline, if we ever start emitting more commands before
10342 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10343 * then do the cacheline alignment, and finally emit the
10344 * MI_DISPLAY_FLIP.
10345 */
10346 ret = intel_ring_cacheline_align(req);
10347 if (ret)
10348 return ret;
10349
73dec95e
TU
10350 cs = intel_ring_begin(req, len);
10351 if (IS_ERR(cs))
10352 return PTR_ERR(cs);
5a21b665
DV
10353
10354 /* Unmask the flip-done completion message. Note that the bspec says that
10355 * we should do this for both the BCS and RCS, and that we must not unmask
10356 * more than one flip event at any time (or ensure that one flip message
10357 * can be sent by waiting for flip-done prior to queueing new flips).
10358 * Experimentation says that BCS works despite DERRMR masking all
10359 * flip-done completion events and that unmasking all planes at once
10360 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10361 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10362 */
b5321f30 10363 if (req->engine->id == RCS) {
73dec95e
TU
10364 *cs++ = MI_LOAD_REGISTER_IMM(1);
10365 *cs++ = i915_mmio_reg_offset(DERRMR);
10366 *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10367 DERRMR_PIPEB_PRI_FLIP_DONE |
10368 DERRMR_PIPEC_PRI_FLIP_DONE);
5db94019 10369 if (IS_GEN8(dev_priv))
73dec95e
TU
10370 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
10371 MI_SRM_LRM_GLOBAL_GTT;
5a21b665 10372 else
73dec95e
TU
10373 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
10374 *cs++ = i915_mmio_reg_offset(DERRMR);
10375 *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
5db94019 10376 if (IS_GEN8(dev_priv)) {
73dec95e
TU
10377 *cs++ = 0;
10378 *cs++ = MI_NOOP;
5a21b665
DV
10379 }
10380 }
10381
73dec95e
TU
10382 *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
10383 *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
10384 *cs++ = intel_crtc->flip_work->gtt_offset;
10385 *cs++ = MI_NOOP;
5a21b665
DV
10386
10387 return 0;
10388}
10389
10390static bool use_mmio_flip(struct intel_engine_cs *engine,
10391 struct drm_i915_gem_object *obj)
10392{
10393 /*
10394 * This is not being used for older platforms, because
10395 * non-availability of flip done interrupt forces us to use
10396 * CS flips. Older platforms derive flip done using some clever
10397 * tricks involving the flip_pending status bits and vblank irqs.
10398 * So using MMIO flips there would disrupt this mechanism.
10399 */
10400
10401 if (engine == NULL)
10402 return true;
10403
10404 if (INTEL_GEN(engine->i915) < 5)
10405 return false;
10406
10407 if (i915.use_mmio_flip < 0)
10408 return false;
10409 else if (i915.use_mmio_flip > 0)
10410 return true;
10411 else if (i915.enable_execlists)
10412 return true;
c37efb99 10413
d07f0e59 10414 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
DV
10415}
10416
10417static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
10418 unsigned int rotation,
10419 struct intel_flip_work *work)
10420{
10421 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10422 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10423 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10424 const enum pipe pipe = intel_crtc->pipe;
d2196774 10425 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
DV
10426
10427 ctl = I915_READ(PLANE_CTL(pipe, 0));
10428 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 10429 switch (fb->modifier) {
5a21b665
DV
10430 case DRM_FORMAT_MOD_NONE:
10431 break;
10432 case I915_FORMAT_MOD_X_TILED:
10433 ctl |= PLANE_CTL_TILED_X;
10434 break;
10435 case I915_FORMAT_MOD_Y_TILED:
10436 ctl |= PLANE_CTL_TILED_Y;
10437 break;
10438 case I915_FORMAT_MOD_Yf_TILED:
10439 ctl |= PLANE_CTL_TILED_YF;
10440 break;
10441 default:
bae781b2 10442 MISSING_CASE(fb->modifier);
5a21b665
DV
10443 }
10444
5a21b665
DV
10445 /*
10446 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10447 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10448 */
10449 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10450 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10451
10452 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
10453 POSTING_READ(PLANE_SURF(pipe, 0));
10454}
10455
10456static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
10457 struct intel_flip_work *work)
10458{
10459 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 10460 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 10461 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
DV
10462 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
10463 u32 dspcntr;
10464
10465 dspcntr = I915_READ(reg);
10466
bae781b2 10467 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
DV
10468 dspcntr |= DISPPLANE_TILED;
10469 else
10470 dspcntr &= ~DISPPLANE_TILED;
10471
10472 I915_WRITE(reg, dspcntr);
10473
10474 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
10475 POSTING_READ(DSPSURF(intel_crtc->plane));
10476}
10477
10478static void intel_mmio_flip_work_func(struct work_struct *w)
10479{
10480 struct intel_flip_work *work =
10481 container_of(w, struct intel_flip_work, mmio_work);
10482 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10483 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10484 struct intel_framebuffer *intel_fb =
10485 to_intel_framebuffer(crtc->base.primary->fb);
10486 struct drm_i915_gem_object *obj = intel_fb->obj;
10487
d07f0e59 10488 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
DV
10489
10490 intel_pipe_update_start(crtc);
10491
10492 if (INTEL_GEN(dev_priv) >= 9)
10493 skl_do_mmio_flip(crtc, work->rotation, work);
10494 else
10495 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10496 ilk_do_mmio_flip(crtc, work);
10497
10498 intel_pipe_update_end(crtc, work);
10499}
10500
10501static int intel_default_queue_flip(struct drm_device *dev,
10502 struct drm_crtc *crtc,
10503 struct drm_framebuffer *fb,
10504 struct drm_i915_gem_object *obj,
10505 struct drm_i915_gem_request *req,
10506 uint32_t flags)
10507{
10508 return -ENODEV;
10509}
10510
10511static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
10512 struct intel_crtc *intel_crtc,
10513 struct intel_flip_work *work)
10514{
10515 u32 addr, vblank;
10516
10517 if (!atomic_read(&work->pending))
10518 return false;
10519
10520 smp_rmb();
10521
10522 vblank = intel_crtc_get_vblank_counter(intel_crtc);
10523 if (work->flip_ready_vblank == 0) {
10524 if (work->flip_queued_req &&
f69a02c9 10525 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
DV
10526 return false;
10527
10528 work->flip_ready_vblank = vblank;
10529 }
10530
10531 if (vblank - work->flip_ready_vblank < 3)
10532 return false;
10533
10534 /* Potential stall - if we see that the flip has happened,
10535 * assume a missed interrupt. */
10536 if (INTEL_GEN(dev_priv) >= 4)
10537 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10538 else
10539 addr = I915_READ(DSPADDR(intel_crtc->plane));
10540
10541 /* There is a potential issue here with a false positive after a flip
10542 * to the same address. We could address this by checking for a
10543 * non-incrementing frame counter.
10544 */
10545 return addr == work->gtt_offset;
10546}
10547
10548void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
10549{
91c8a326 10550 struct drm_device *dev = &dev_priv->drm;
98187836 10551 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
DV
10552 struct intel_flip_work *work;
10553
10554 WARN_ON(!in_interrupt());
10555
10556 if (crtc == NULL)
10557 return;
10558
10559 spin_lock(&dev->event_lock);
e2af48c6 10560 work = crtc->flip_work;
5a21b665
DV
10561
10562 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10563 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
DV
10564 WARN_ONCE(1,
10565 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
10566 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
10567 page_flip_completed(crtc);
5a21b665
DV
10568 work = NULL;
10569 }
10570
10571 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 10572 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
DV
10573 intel_queue_rps_boost_for_request(work->flip_queued_req);
10574 spin_unlock(&dev->event_lock);
10575}
10576
4c01ded5 10577__maybe_unused
5a21b665
DV
10578static int intel_crtc_page_flip(struct drm_crtc *crtc,
10579 struct drm_framebuffer *fb,
10580 struct drm_pending_vblank_event *event,
10581 uint32_t page_flip_flags)
10582{
10583 struct drm_device *dev = crtc->dev;
fac5e23e 10584 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
DV
10585 struct drm_framebuffer *old_fb = crtc->primary->fb;
10586 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10588 struct drm_plane *primary = crtc->primary;
10589 enum pipe pipe = intel_crtc->pipe;
10590 struct intel_flip_work *work;
10591 struct intel_engine_cs *engine;
10592 bool mmio_flip;
8e637178 10593 struct drm_i915_gem_request *request;
058d88c4 10594 struct i915_vma *vma;
5a21b665
DV
10595 int ret;
10596
10597 /*
10598 * drm_mode_page_flip_ioctl() should already catch this, but double
10599 * check to be safe. In the future we may enable pageflipping from
10600 * a disabled primary plane.
10601 */
10602 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10603 return -EBUSY;
10604
10605 /* Can't change pixel format via MI display flips. */
dbd4d576 10606 if (fb->format != crtc->primary->fb->format)
5a21b665
DV
10607 return -EINVAL;
10608
10609 /*
10610 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10611 * Note that pitch changes could also affect these register.
10612 */
6315b5d3 10613 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
DV
10614 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10615 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10616 return -EINVAL;
10617
10618 if (i915_terminally_wedged(&dev_priv->gpu_error))
10619 goto out_hang;
10620
10621 work = kzalloc(sizeof(*work), GFP_KERNEL);
10622 if (work == NULL)
10623 return -ENOMEM;
10624
10625 work->event = event;
10626 work->crtc = crtc;
10627 work->old_fb = old_fb;
10628 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
10629
10630 ret = drm_crtc_vblank_get(crtc);
10631 if (ret)
10632 goto free_work;
10633
10634 /* We borrow the event spin lock for protecting flip_work */
10635 spin_lock_irq(&dev->event_lock);
10636 if (intel_crtc->flip_work) {
10637 /* Before declaring the flip queue wedged, check if
10638 * the hardware completed the operation behind our backs.
10639 */
10640 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
10641 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10642 page_flip_completed(intel_crtc);
10643 } else {
10644 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10645 spin_unlock_irq(&dev->event_lock);
10646
10647 drm_crtc_vblank_put(crtc);
10648 kfree(work);
10649 return -EBUSY;
10650 }
10651 }
10652 intel_crtc->flip_work = work;
10653 spin_unlock_irq(&dev->event_lock);
10654
10655 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10656 flush_workqueue(dev_priv->wq);
10657
10658 /* Reference the objects for the scheduled work. */
10659 drm_framebuffer_reference(work->old_fb);
5a21b665
DV
10660
10661 crtc->primary->fb = fb;
10662 update_state_fb(crtc->primary);
faf68d92 10663
25dc556a 10664 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
DV
10665
10666 ret = i915_mutex_lock_interruptible(dev);
10667 if (ret)
10668 goto cleanup;
10669
8af29b0c 10670 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
8c185eca 10671 if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
5a21b665 10672 ret = -EIO;
ddbb271a 10673 goto unlock;
5a21b665
DV
10674 }
10675
10676 atomic_inc(&intel_crtc->unpin_work_count);
10677
9beb5fea 10678 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
DV
10679 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
10680
920a14b2 10681 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 10682 engine = dev_priv->engine[BCS];
bae781b2 10683 if (fb->modifier != old_fb->modifier)
5a21b665
DV
10684 /* vlv: DISPLAY_FLIP fails to change tiling */
10685 engine = NULL;
fd6b8f43 10686 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 10687 engine = dev_priv->engine[BCS];
6315b5d3 10688 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 10689 engine = i915_gem_object_last_write_engine(obj);
5a21b665 10690 if (engine == NULL || engine->id != RCS)
3b3f1650 10691 engine = dev_priv->engine[BCS];
5a21b665 10692 } else {
3b3f1650 10693 engine = dev_priv->engine[RCS];
5a21b665
DV
10694 }
10695
10696 mmio_flip = use_mmio_flip(engine, obj);
10697
058d88c4
CW
10698 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
10699 if (IS_ERR(vma)) {
10700 ret = PTR_ERR(vma);
5a21b665 10701 goto cleanup_pending;
058d88c4 10702 }
5a21b665 10703
be1e3415
CW
10704 work->old_vma = to_intel_plane_state(primary->state)->vma;
10705 to_intel_plane_state(primary->state)->vma = vma;
10706
10707 work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
5a21b665
DV
10708 work->rotation = crtc->primary->state->rotation;
10709
1f061316
PZ
10710 /*
10711 * There's the potential that the next frame will not be compatible with
10712 * FBC, so we want to call pre_update() before the actual page flip.
10713 * The problem is that pre_update() caches some information about the fb
10714 * object, so we want to do this only after the object is pinned. Let's
10715 * be on the safe side and do this immediately before scheduling the
10716 * flip.
10717 */
10718 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
10719 to_intel_plane_state(primary->state));
10720
5a21b665
DV
10721 if (mmio_flip) {
10722 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 10723 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 10724 } else {
e8a9c58f
CW
10725 request = i915_gem_request_alloc(engine,
10726 dev_priv->kernel_context);
8e637178
CW
10727 if (IS_ERR(request)) {
10728 ret = PTR_ERR(request);
10729 goto cleanup_unpin;
10730 }
10731
a2bc4695 10732 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
10733 if (ret)
10734 goto cleanup_request;
10735
5a21b665
DV
10736 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
10737 page_flip_flags);
10738 if (ret)
8e637178 10739 goto cleanup_request;
5a21b665
DV
10740
10741 intel_mark_page_flip_active(intel_crtc, work);
10742
8e637178 10743 work->flip_queued_req = i915_gem_request_get(request);
e642c85b 10744 i915_add_request(request);
5a21b665
DV
10745 }
10746
92117f0b 10747 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
DV
10748 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
10749 to_intel_plane(primary)->frontbuffer_bit);
10750 mutex_unlock(&dev->struct_mutex);
10751
5748b6a1 10752 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
DV
10753 to_intel_plane(primary)->frontbuffer_bit);
10754
10755 trace_i915_flip_request(intel_crtc->plane, obj);
10756
10757 return 0;
10758
8e637178 10759cleanup_request:
e642c85b 10760 i915_add_request(request);
5a21b665 10761cleanup_unpin:
be1e3415
CW
10762 to_intel_plane_state(primary->state)->vma = work->old_vma;
10763 intel_unpin_fb_vma(vma);
5a21b665 10764cleanup_pending:
5a21b665 10765 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 10766unlock:
5a21b665
DV
10767 mutex_unlock(&dev->struct_mutex);
10768cleanup:
10769 crtc->primary->fb = old_fb;
10770 update_state_fb(crtc->primary);
10771
f0cd5182 10772 i915_gem_object_put(obj);
5a21b665
DV
10773 drm_framebuffer_unreference(work->old_fb);
10774
10775 spin_lock_irq(&dev->event_lock);
10776 intel_crtc->flip_work = NULL;
10777 spin_unlock_irq(&dev->event_lock);
10778
10779 drm_crtc_vblank_put(crtc);
10780free_work:
10781 kfree(work);
10782
10783 if (ret == -EIO) {
10784 struct drm_atomic_state *state;
10785 struct drm_plane_state *plane_state;
10786
10787out_hang:
10788 state = drm_atomic_state_alloc(dev);
10789 if (!state)
10790 return -ENOMEM;
10791 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
10792
10793retry:
10794 plane_state = drm_atomic_get_plane_state(state, primary);
10795 ret = PTR_ERR_OR_ZERO(plane_state);
10796 if (!ret) {
10797 drm_atomic_set_fb_for_plane(plane_state, fb);
10798
10799 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
10800 if (!ret)
10801 ret = drm_atomic_commit(state);
10802 }
10803
10804 if (ret == -EDEADLK) {
10805 drm_modeset_backoff(state->acquire_ctx);
10806 drm_atomic_state_clear(state);
10807 goto retry;
10808 }
10809
0853695c 10810 drm_atomic_state_put(state);
5a21b665
DV
10811
10812 if (ret == 0 && event) {
10813 spin_lock_irq(&dev->event_lock);
10814 drm_crtc_send_vblank_event(crtc, event);
10815 spin_unlock_irq(&dev->event_lock);
10816 }
10817 }
10818 return ret;
10819}
10820
10821
10822/**
10823 * intel_wm_need_update - Check whether watermarks need updating
10824 * @plane: drm plane
10825 * @state: new plane state
10826 *
10827 * Check current plane state versus the new one to determine whether
10828 * watermarks need to be recalculated.
10829 *
10830 * Returns true or false.
10831 */
10832static bool intel_wm_need_update(struct drm_plane *plane,
10833 struct drm_plane_state *state)
10834{
10835 struct intel_plane_state *new = to_intel_plane_state(state);
10836 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10837
10838 /* Update watermarks on tiling or size changes. */
936e71e3 10839 if (new->base.visible != cur->base.visible)
5a21b665
DV
10840 return true;
10841
10842 if (!cur->base.fb || !new->base.fb)
10843 return false;
10844
bae781b2 10845 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10846 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10847 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10848 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10849 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10850 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10851 return true;
10852
10853 return false;
10854}
10855
10856static bool needs_scaling(struct intel_plane_state *state)
10857{
936e71e3
VS
10858 int src_w = drm_rect_width(&state->base.src) >> 16;
10859 int src_h = drm_rect_height(&state->base.src) >> 16;
10860 int dst_w = drm_rect_width(&state->base.dst);
10861 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10862
10863 return (src_w != dst_w || src_h != dst_h);
10864}
d21fbe87 10865
da20eabd
ML
10866int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
10867 struct drm_plane_state *plane_state)
10868{
ab1d3a0e 10869 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10870 struct drm_crtc *crtc = crtc_state->crtc;
10871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 10872 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 10873 struct drm_device *dev = crtc->dev;
ed4a6a7c 10874 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 10875 struct intel_plane_state *old_plane_state =
e9728bd8 10876 to_intel_plane_state(plane->base.state);
da20eabd
ML
10877 bool mode_changed = needs_modeset(crtc_state);
10878 bool was_crtc_enabled = crtc->state->active;
10879 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10880 bool turn_off, turn_on, visible, was_visible;
10881 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10882 int ret;
da20eabd 10883
e9728bd8 10884 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
10885 ret = skl_update_scaler_plane(
10886 to_intel_crtc_state(crtc_state),
10887 to_intel_plane_state(plane_state));
10888 if (ret)
10889 return ret;
10890 }
10891
936e71e3 10892 was_visible = old_plane_state->base.visible;
1d4258db 10893 visible = plane_state->visible;
da20eabd
ML
10894
10895 if (!was_crtc_enabled && WARN_ON(was_visible))
10896 was_visible = false;
10897
35c08f43
ML
10898 /*
10899 * Visibility is calculated as if the crtc was on, but
10900 * after scaler setup everything depends on it being off
10901 * when the crtc isn't active.
f818ffea
VS
10902 *
10903 * FIXME this is wrong for watermarks. Watermarks should also
10904 * be computed as if the pipe would be active. Perhaps move
10905 * per-plane wm computation to the .check_plane() hook, and
10906 * only combine the results from all planes in the current place?
35c08f43 10907 */
e9728bd8 10908 if (!is_crtc_enabled) {
1d4258db 10909 plane_state->visible = visible = false;
e9728bd8
VS
10910 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10911 }
da20eabd
ML
10912
10913 if (!was_visible && !visible)
10914 return 0;
10915
e8861675
ML
10916 if (fb != old_plane_state->base.fb)
10917 pipe_config->fb_changed = true;
10918
da20eabd
ML
10919 turn_off = was_visible && (!visible || mode_changed);
10920 turn_on = visible && (!was_visible || mode_changed);
10921
72660ce0 10922 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
10923 intel_crtc->base.base.id, intel_crtc->base.name,
10924 plane->base.base.id, plane->base.name,
72660ce0 10925 fb ? fb->base.id : -1);
da20eabd 10926
72660ce0 10927 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 10928 plane->base.base.id, plane->base.name,
72660ce0 10929 was_visible, visible,
da20eabd
ML
10930 turn_off, turn_on, mode_changed);
10931
caed361d 10932 if (turn_on) {
b4ede6df
VS
10933 if (INTEL_GEN(dev_priv) < 5)
10934 pipe_config->update_wm_pre = true;
caed361d
VS
10935
10936 /* must disable cxsr around plane enable/disable */
e9728bd8 10937 if (plane->id != PLANE_CURSOR)
caed361d
VS
10938 pipe_config->disable_cxsr = true;
10939 } else if (turn_off) {
b4ede6df
VS
10940 if (INTEL_GEN(dev_priv) < 5)
10941 pipe_config->update_wm_post = true;
92826fcd 10942
852eb00d 10943 /* must disable cxsr around plane enable/disable */
e9728bd8 10944 if (plane->id != PLANE_CURSOR)
ab1d3a0e 10945 pipe_config->disable_cxsr = true;
e9728bd8 10946 } else if (intel_wm_need_update(&plane->base, plane_state)) {
b4ede6df
VS
10947 if (INTEL_GEN(dev_priv) < 5) {
10948 /* FIXME bollocks */
10949 pipe_config->update_wm_pre = true;
10950 pipe_config->update_wm_post = true;
10951 }
852eb00d 10952 }
da20eabd 10953
8be6ca85 10954 if (visible || was_visible)
e9728bd8 10955 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 10956
31ae71fc
ML
10957 /*
10958 * WaCxSRDisabledForSpriteScaling:ivb
10959 *
10960 * cstate->update_wm was already set above, so this flag will
10961 * take effect when we commit and program watermarks.
10962 */
e9728bd8 10963 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10964 needs_scaling(to_intel_plane_state(plane_state)) &&
10965 !needs_scaling(old_plane_state))
10966 pipe_config->disable_lp_wm = true;
d21fbe87 10967
da20eabd
ML
10968 return 0;
10969}
10970
6d3a1ce7
ML
10971static bool encoders_cloneable(const struct intel_encoder *a,
10972 const struct intel_encoder *b)
10973{
10974 /* masks could be asymmetric, so check both ways */
10975 return a == b || (a->cloneable & (1 << b->type) &&
10976 b->cloneable & (1 << a->type));
10977}
10978
10979static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10980 struct intel_crtc *crtc,
10981 struct intel_encoder *encoder)
10982{
10983 struct intel_encoder *source_encoder;
10984 struct drm_connector *connector;
10985 struct drm_connector_state *connector_state;
10986 int i;
10987
aa5e9b47 10988 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
10989 if (connector_state->crtc != &crtc->base)
10990 continue;
10991
10992 source_encoder =
10993 to_intel_encoder(connector_state->best_encoder);
10994 if (!encoders_cloneable(encoder, source_encoder))
10995 return false;
10996 }
10997
10998 return true;
10999}
11000
6d3a1ce7
ML
11001static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11002 struct drm_crtc_state *crtc_state)
11003{
cf5a15be 11004 struct drm_device *dev = crtc->dev;
fac5e23e 11005 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 11006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11007 struct intel_crtc_state *pipe_config =
11008 to_intel_crtc_state(crtc_state);
6d3a1ce7 11009 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11010 int ret;
6d3a1ce7
ML
11011 bool mode_changed = needs_modeset(crtc_state);
11012
852eb00d 11013 if (mode_changed && !crtc_state->active)
caed361d 11014 pipe_config->update_wm_post = true;
eddfcbcd 11015
ad421372
ML
11016 if (mode_changed && crtc_state->enable &&
11017 dev_priv->display.crtc_compute_clock &&
8106ddbd 11018 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11019 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11020 pipe_config);
11021 if (ret)
11022 return ret;
11023 }
11024
82cf435b
LL
11025 if (crtc_state->color_mgmt_changed) {
11026 ret = intel_color_check(crtc, crtc_state);
11027 if (ret)
11028 return ret;
e7852a4b
LL
11029
11030 /*
11031 * Changing color management on Intel hardware is
11032 * handled as part of planes update.
11033 */
11034 crtc_state->planes_changed = true;
82cf435b
LL
11035 }
11036
e435d6e5 11037 ret = 0;
86c8bbbe 11038 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11039 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11040 if (ret) {
11041 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11042 return ret;
11043 }
11044 }
11045
11046 if (dev_priv->display.compute_intermediate_wm &&
11047 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11048 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11049 return 0;
11050
11051 /*
11052 * Calculate 'intermediate' watermarks that satisfy both the
11053 * old state and the new state. We can program these
11054 * immediately.
11055 */
6315b5d3 11056 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
11057 intel_crtc,
11058 pipe_config);
11059 if (ret) {
11060 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11061 return ret;
ed4a6a7c 11062 }
e3d5457c
VS
11063 } else if (dev_priv->display.compute_intermediate_wm) {
11064 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11065 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
11066 }
11067
6315b5d3 11068 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
11069 if (mode_changed)
11070 ret = skl_update_scaler_crtc(pipe_config);
11071
11072 if (!ret)
6ebc6923 11073 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
11074 pipe_config);
11075 }
11076
11077 return ret;
6d3a1ce7
ML
11078}
11079
65b38e0d 11080static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 11081 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
DV
11082 .atomic_begin = intel_begin_crtc_commit,
11083 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11084 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11085};
11086
d29b2f9d
ACO
11087static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11088{
11089 struct intel_connector *connector;
f9e905ca 11090 struct drm_connector_list_iter conn_iter;
d29b2f9d 11091
f9e905ca
DV
11092 drm_connector_list_iter_begin(dev, &conn_iter);
11093 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f
DV
11094 if (connector->base.state->crtc)
11095 drm_connector_unreference(&connector->base);
11096
d29b2f9d
ACO
11097 if (connector->base.encoder) {
11098 connector->base.state->best_encoder =
11099 connector->base.encoder;
11100 connector->base.state->crtc =
11101 connector->base.encoder->crtc;
8863dc7f
DV
11102
11103 drm_connector_reference(&connector->base);
d29b2f9d
ACO
11104 } else {
11105 connector->base.state->best_encoder = NULL;
11106 connector->base.state->crtc = NULL;
11107 }
11108 }
f9e905ca 11109 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
11110}
11111
050f7aeb 11112static void
eba905b2 11113connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11114 struct intel_crtc_state *pipe_config)
050f7aeb 11115{
6a2a5c5d 11116 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
11117 int bpp = pipe_config->pipe_bpp;
11118
11119 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
11120 connector->base.base.id,
11121 connector->base.name);
050f7aeb
DV
11122
11123 /* Don't use an invalid EDID bpc value */
6a2a5c5d 11124 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 11125 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
11126 bpp, info->bpc * 3);
11127 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
11128 }
11129
196f954e 11130 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 11131 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
11132 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11133 bpp);
11134 pipe_config->pipe_bpp = 24;
050f7aeb
DV
11135 }
11136}
11137
4e53c2e0 11138static int
050f7aeb 11139compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11140 struct intel_crtc_state *pipe_config)
4e53c2e0 11141{
9beb5fea 11142 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11143 struct drm_atomic_state *state;
da3ced29
ACO
11144 struct drm_connector *connector;
11145 struct drm_connector_state *connector_state;
1486017f 11146 int bpp, i;
4e53c2e0 11147
9beb5fea
TU
11148 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11149 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11150 bpp = 10*3;
9beb5fea 11151 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
11152 bpp = 12*3;
11153 else
11154 bpp = 8*3;
11155
4e53c2e0 11156
4e53c2e0
DV
11157 pipe_config->pipe_bpp = bpp;
11158
1486017f
ACO
11159 state = pipe_config->base.state;
11160
4e53c2e0 11161 /* Clamp display bpp to EDID value */
aa5e9b47 11162 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 11163 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11164 continue;
11165
da3ced29
ACO
11166 connected_sink_compute_bpp(to_intel_connector(connector),
11167 pipe_config);
4e53c2e0
DV
11168 }
11169
11170 return bpp;
11171}
11172
644db711
DV
11173static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11174{
11175 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11176 "type: 0x%x flags: 0x%x\n",
1342830c 11177 mode->crtc_clock,
644db711
DV
11178 mode->crtc_hdisplay, mode->crtc_hsync_start,
11179 mode->crtc_hsync_end, mode->crtc_htotal,
11180 mode->crtc_vdisplay, mode->crtc_vsync_start,
11181 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11182}
11183
f6982332
TU
11184static inline void
11185intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11186 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11187{
a4309657
TU
11188 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11189 id, lane_count,
f6982332
TU
11190 m_n->gmch_m, m_n->gmch_n,
11191 m_n->link_m, m_n->link_n, m_n->tu);
11192}
11193
c0b03411 11194static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11195 struct intel_crtc_state *pipe_config,
c0b03411
DV
11196 const char *context)
11197{
6a60cd87 11198 struct drm_device *dev = crtc->base.dev;
4f8036a2 11199 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11200 struct drm_plane *plane;
11201 struct intel_plane *intel_plane;
11202 struct intel_plane_state *state;
11203 struct drm_framebuffer *fb;
11204
66766e4f
TU
11205 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11206 crtc->base.base.id, crtc->base.name, context);
c0b03411 11207
2c89429e
TU
11208 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11209 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11210 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11211
11212 if (pipe_config->has_pch_encoder)
11213 intel_dump_m_n_config(pipe_config, "fdi",
11214 pipe_config->fdi_lanes,
11215 &pipe_config->fdi_m_n);
f6982332
TU
11216
11217 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11218 intel_dump_m_n_config(pipe_config, "dp m_n",
11219 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11220 if (pipe_config->has_drrs)
11221 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11222 pipe_config->lane_count,
11223 &pipe_config->dp_m2_n2);
f6982332 11224 }
b95af8be 11225
55072d19 11226 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11227 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11228
c0b03411 11229 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11230 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11231 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11232 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11233 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11234 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11235 pipe_config->port_clock,
a7d1b3f4
VS
11236 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11237 pipe_config->pixel_rate);
dd2f616d
TU
11238
11239 if (INTEL_GEN(dev_priv) >= 9)
11240 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11241 crtc->num_scalers,
11242 pipe_config->scaler_state.scaler_users,
11243 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11244
11245 if (HAS_GMCH_DISPLAY(dev_priv))
11246 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11247 pipe_config->gmch_pfit.control,
11248 pipe_config->gmch_pfit.pgm_ratios,
11249 pipe_config->gmch_pfit.lvds_border_bits);
11250 else
11251 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11252 pipe_config->pch_pfit.pos,
11253 pipe_config->pch_pfit.size,
08c4d7fc 11254 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11255
2c89429e
TU
11256 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11257 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11258
f50b79f0 11259 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11260
6a60cd87
CK
11261 DRM_DEBUG_KMS("planes on this crtc\n");
11262 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11263 struct drm_format_name_buf format_name;
6a60cd87
CK
11264 intel_plane = to_intel_plane(plane);
11265 if (intel_plane->pipe != crtc->pipe)
11266 continue;
11267
11268 state = to_intel_plane_state(plane->state);
11269 fb = state->base.fb;
11270 if (!fb) {
1d577e02
VS
11271 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11272 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11273 continue;
11274 }
11275
dd2f616d
TU
11276 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11277 plane->base.id, plane->name,
b3c11ac2 11278 fb->base.id, fb->width, fb->height,
438b74a5 11279 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11280 if (INTEL_GEN(dev_priv) >= 9)
11281 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11282 state->scaler_id,
11283 state->base.src.x1 >> 16,
11284 state->base.src.y1 >> 16,
11285 drm_rect_width(&state->base.src) >> 16,
11286 drm_rect_height(&state->base.src) >> 16,
11287 state->base.dst.x1, state->base.dst.y1,
11288 drm_rect_width(&state->base.dst),
11289 drm_rect_height(&state->base.dst));
6a60cd87 11290 }
c0b03411
DV
11291}
11292
5448a00d 11293static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11294{
5448a00d 11295 struct drm_device *dev = state->dev;
da3ced29 11296 struct drm_connector *connector;
00f0b378 11297 unsigned int used_ports = 0;
477321e0 11298 unsigned int used_mst_ports = 0;
00f0b378
VS
11299
11300 /*
11301 * Walk the connector list instead of the encoder
11302 * list to detect the problem on ddi platforms
11303 * where there's just one encoder per digital port.
11304 */
0bff4858
VS
11305 drm_for_each_connector(connector, dev) {
11306 struct drm_connector_state *connector_state;
11307 struct intel_encoder *encoder;
11308
11309 connector_state = drm_atomic_get_existing_connector_state(state, connector);
11310 if (!connector_state)
11311 connector_state = connector->state;
11312
5448a00d 11313 if (!connector_state->best_encoder)
00f0b378
VS
11314 continue;
11315
5448a00d
ACO
11316 encoder = to_intel_encoder(connector_state->best_encoder);
11317
11318 WARN_ON(!connector_state->crtc);
00f0b378
VS
11319
11320 switch (encoder->type) {
11321 unsigned int port_mask;
11322 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 11323 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11324 break;
cca0502b 11325 case INTEL_OUTPUT_DP:
00f0b378
VS
11326 case INTEL_OUTPUT_HDMI:
11327 case INTEL_OUTPUT_EDP:
11328 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11329
11330 /* the same port mustn't appear more than once */
11331 if (used_ports & port_mask)
11332 return false;
11333
11334 used_ports |= port_mask;
477321e0
VS
11335 break;
11336 case INTEL_OUTPUT_DP_MST:
11337 used_mst_ports |=
11338 1 << enc_to_mst(&encoder->base)->primary->port;
11339 break;
00f0b378
VS
11340 default:
11341 break;
11342 }
11343 }
11344
477321e0
VS
11345 /* can't mix MST and SST/HDMI on the same port */
11346 if (used_ports & used_mst_ports)
11347 return false;
11348
00f0b378
VS
11349 return true;
11350}
11351
83a57153
ACO
11352static void
11353clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11354{
ff32c54e
VS
11355 struct drm_i915_private *dev_priv =
11356 to_i915(crtc_state->base.crtc->dev);
663a3640 11357 struct intel_crtc_scaler_state scaler_state;
4978cc93 11358 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11359 struct intel_shared_dpll *shared_dpll;
ff32c54e 11360 struct intel_crtc_wm_state wm_state;
c4e2d043 11361 bool force_thru;
83a57153 11362
7546a384
ACO
11363 /* FIXME: before the switch to atomic started, a new pipe_config was
11364 * kzalloc'd. Code that depends on any field being zero should be
11365 * fixed, so that the crtc_state can be safely duplicated. For now,
11366 * only fields that are know to not cause problems are preserved. */
11367
663a3640 11368 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11369 shared_dpll = crtc_state->shared_dpll;
11370 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11371 force_thru = crtc_state->pch_pfit.force_thru;
ff32c54e
VS
11372 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
11373 wm_state = crtc_state->wm;
4978cc93 11374
d2fa80a5
CW
11375 /* Keep base drm_crtc_state intact, only clear our extended struct */
11376 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11377 memset(&crtc_state->base + 1, 0,
11378 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 11379
663a3640 11380 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11381 crtc_state->shared_dpll = shared_dpll;
11382 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11383 crtc_state->pch_pfit.force_thru = force_thru;
ff32c54e
VS
11384 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
11385 crtc_state->wm = wm_state;
83a57153
ACO
11386}
11387
548ee15b 11388static int
b8cecdf5 11389intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11390 struct intel_crtc_state *pipe_config)
ee7b9f93 11391{
b359283a 11392 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11393 struct intel_encoder *encoder;
da3ced29 11394 struct drm_connector *connector;
0b901879 11395 struct drm_connector_state *connector_state;
d328c9d7 11396 int base_bpp, ret = -EINVAL;
0b901879 11397 int i;
e29c22c0 11398 bool retry = true;
ee7b9f93 11399
83a57153 11400 clear_intel_crtc_state(pipe_config);
7758a113 11401
e143a21c
DV
11402 pipe_config->cpu_transcoder =
11403 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11404
2960bc9c
ID
11405 /*
11406 * Sanitize sync polarity flags based on requested ones. If neither
11407 * positive or negative polarity is requested, treat this as meaning
11408 * negative polarity.
11409 */
2d112de7 11410 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11411 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11412 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11413
2d112de7 11414 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11415 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11416 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11417
d328c9d7
DV
11418 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11419 pipe_config);
11420 if (base_bpp < 0)
4e53c2e0
DV
11421 goto fail;
11422
e41a56be
VS
11423 /*
11424 * Determine the real pipe dimensions. Note that stereo modes can
11425 * increase the actual pipe size due to the frame doubling and
11426 * insertion of additional space for blanks between the frame. This
11427 * is stored in the crtc timings. We use the requested mode to do this
11428 * computation to clearly distinguish it from the adjusted mode, which
11429 * can be changed by the connectors in the below retry loop.
11430 */
196cd5d3 11431 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11432 &pipe_config->pipe_src_w,
11433 &pipe_config->pipe_src_h);
e41a56be 11434
aa5e9b47 11435 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
11436 if (connector_state->crtc != crtc)
11437 continue;
11438
11439 encoder = to_intel_encoder(connector_state->best_encoder);
11440
e25148d0
VS
11441 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11442 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11443 goto fail;
11444 }
11445
253c84c8
VS
11446 /*
11447 * Determine output_types before calling the .compute_config()
11448 * hooks so that the hooks can use this information safely.
11449 */
11450 pipe_config->output_types |= 1 << encoder->type;
11451 }
11452
e29c22c0 11453encoder_retry:
ef1b460d 11454 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11455 pipe_config->port_clock = 0;
ef1b460d 11456 pipe_config->pixel_multiplier = 1;
ff9a6750 11457
135c81b8 11458 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11459 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11460 CRTC_STEREO_DOUBLE);
135c81b8 11461
7758a113
DV
11462 /* Pass our mode to the connectors and the CRTC to give them a chance to
11463 * adjust it according to limitations or connector properties, and also
11464 * a chance to reject the mode entirely.
47f1c6c9 11465 */
aa5e9b47 11466 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 11467 if (connector_state->crtc != crtc)
7758a113 11468 continue;
7ae89233 11469
0b901879
ACO
11470 encoder = to_intel_encoder(connector_state->best_encoder);
11471
0a478c27 11472 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11473 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11474 goto fail;
11475 }
ee7b9f93 11476 }
47f1c6c9 11477
ff9a6750
DV
11478 /* Set default port clock if not overwritten by the encoder. Needs to be
11479 * done afterwards in case the encoder adjusts the mode. */
11480 if (!pipe_config->port_clock)
2d112de7 11481 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11482 * pipe_config->pixel_multiplier;
ff9a6750 11483
a43f6e0f 11484 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11485 if (ret < 0) {
7758a113
DV
11486 DRM_DEBUG_KMS("CRTC fixup failed\n");
11487 goto fail;
ee7b9f93 11488 }
e29c22c0
DV
11489
11490 if (ret == RETRY) {
11491 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11492 ret = -EINVAL;
11493 goto fail;
11494 }
11495
11496 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11497 retry = false;
11498 goto encoder_retry;
11499 }
11500
e8fa4270 11501 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11502 * only enable it on 6bpc panels and when its not a compliance
11503 * test requesting 6bpc video pattern.
11504 */
11505 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11506 !pipe_config->dither_force_disable;
62f0ace5 11507 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11508 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11509
7758a113 11510fail:
548ee15b 11511 return ret;
ee7b9f93 11512}
47f1c6c9 11513
ea9d758d 11514static void
4740b0f2 11515intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 11516{
0a9ab303 11517 struct drm_crtc *crtc;
aa5e9b47 11518 struct drm_crtc_state *new_crtc_state;
8a75d157 11519 int i;
ea9d758d 11520
7668851f 11521 /* Double check state. */
aa5e9b47
ML
11522 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11523 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
fc467a22
ML
11524
11525 /* Update hwmode for vblank functions */
aa5e9b47
ML
11526 if (new_crtc_state->active)
11527 crtc->hwmode = new_crtc_state->adjusted_mode;
fc467a22
ML
11528 else
11529 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
11530
11531 /*
11532 * Update legacy state to satisfy fbc code. This can
11533 * be removed when fbc uses the atomic state.
11534 */
11535 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
11536 struct drm_plane_state *plane_state = crtc->primary->state;
11537
11538 crtc->primary->fb = plane_state->fb;
11539 crtc->x = plane_state->src_x >> 16;
11540 crtc->y = plane_state->src_y >> 16;
11541 }
ea9d758d 11542 }
ea9d758d
DV
11543}
11544
3bd26263 11545static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11546{
3bd26263 11547 int diff;
f1f644dc
JB
11548
11549 if (clock1 == clock2)
11550 return true;
11551
11552 if (!clock1 || !clock2)
11553 return false;
11554
11555 diff = abs(clock1 - clock2);
11556
11557 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11558 return true;
11559
11560 return false;
11561}
11562
cfb23ed6
ML
11563static bool
11564intel_compare_m_n(unsigned int m, unsigned int n,
11565 unsigned int m2, unsigned int n2,
11566 bool exact)
11567{
11568 if (m == m2 && n == n2)
11569 return true;
11570
11571 if (exact || !m || !n || !m2 || !n2)
11572 return false;
11573
11574 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11575
31d10b57
ML
11576 if (n > n2) {
11577 while (n > n2) {
cfb23ed6
ML
11578 m2 <<= 1;
11579 n2 <<= 1;
11580 }
31d10b57
ML
11581 } else if (n < n2) {
11582 while (n < n2) {
cfb23ed6
ML
11583 m <<= 1;
11584 n <<= 1;
11585 }
11586 }
11587
31d10b57
ML
11588 if (n != n2)
11589 return false;
11590
11591 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11592}
11593
11594static bool
11595intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11596 struct intel_link_m_n *m2_n2,
11597 bool adjust)
11598{
11599 if (m_n->tu == m2_n2->tu &&
11600 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11601 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11602 intel_compare_m_n(m_n->link_m, m_n->link_n,
11603 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11604 if (adjust)
11605 *m2_n2 = *m_n;
11606
11607 return true;
11608 }
11609
11610 return false;
11611}
11612
4e8048f8
TU
11613static void __printf(3, 4)
11614pipe_config_err(bool adjust, const char *name, const char *format, ...)
11615{
11616 char *level;
11617 unsigned int category;
11618 struct va_format vaf;
11619 va_list args;
11620
11621 if (adjust) {
11622 level = KERN_DEBUG;
11623 category = DRM_UT_KMS;
11624 } else {
11625 level = KERN_ERR;
11626 category = DRM_UT_NONE;
11627 }
11628
11629 va_start(args, format);
11630 vaf.fmt = format;
11631 vaf.va = &args;
11632
11633 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11634
11635 va_end(args);
11636}
11637
0e8ffe1b 11638static bool
6315b5d3 11639intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11640 struct intel_crtc_state *current_config,
cfb23ed6
ML
11641 struct intel_crtc_state *pipe_config,
11642 bool adjust)
0e8ffe1b 11643{
cfb23ed6
ML
11644 bool ret = true;
11645
66e985c0
DV
11646#define PIPE_CONF_CHECK_X(name) \
11647 if (current_config->name != pipe_config->name) { \
4e8048f8 11648 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11649 "(expected 0x%08x, found 0x%08x)\n", \
11650 current_config->name, \
11651 pipe_config->name); \
cfb23ed6 11652 ret = false; \
66e985c0
DV
11653 }
11654
08a24034
DV
11655#define PIPE_CONF_CHECK_I(name) \
11656 if (current_config->name != pipe_config->name) { \
4e8048f8 11657 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11658 "(expected %i, found %i)\n", \
11659 current_config->name, \
11660 pipe_config->name); \
cfb23ed6
ML
11661 ret = false; \
11662 }
11663
8106ddbd
ACO
11664#define PIPE_CONF_CHECK_P(name) \
11665 if (current_config->name != pipe_config->name) { \
4e8048f8 11666 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11667 "(expected %p, found %p)\n", \
11668 current_config->name, \
11669 pipe_config->name); \
11670 ret = false; \
11671 }
11672
cfb23ed6
ML
11673#define PIPE_CONF_CHECK_M_N(name) \
11674 if (!intel_compare_link_m_n(&current_config->name, \
11675 &pipe_config->name,\
11676 adjust)) { \
4e8048f8 11677 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11678 "(expected tu %i gmch %i/%i link %i/%i, " \
11679 "found tu %i, gmch %i/%i link %i/%i)\n", \
11680 current_config->name.tu, \
11681 current_config->name.gmch_m, \
11682 current_config->name.gmch_n, \
11683 current_config->name.link_m, \
11684 current_config->name.link_n, \
11685 pipe_config->name.tu, \
11686 pipe_config->name.gmch_m, \
11687 pipe_config->name.gmch_n, \
11688 pipe_config->name.link_m, \
11689 pipe_config->name.link_n); \
11690 ret = false; \
11691 }
11692
55c561a7
DV
11693/* This is required for BDW+ where there is only one set of registers for
11694 * switching between high and low RR.
11695 * This macro can be used whenever a comparison has to be made between one
11696 * hw state and multiple sw state variables.
11697 */
cfb23ed6
ML
11698#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11699 if (!intel_compare_link_m_n(&current_config->name, \
11700 &pipe_config->name, adjust) && \
11701 !intel_compare_link_m_n(&current_config->alt_name, \
11702 &pipe_config->name, adjust)) { \
4e8048f8 11703 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11704 "(expected tu %i gmch %i/%i link %i/%i, " \
11705 "or tu %i gmch %i/%i link %i/%i, " \
11706 "found tu %i, gmch %i/%i link %i/%i)\n", \
11707 current_config->name.tu, \
11708 current_config->name.gmch_m, \
11709 current_config->name.gmch_n, \
11710 current_config->name.link_m, \
11711 current_config->name.link_n, \
11712 current_config->alt_name.tu, \
11713 current_config->alt_name.gmch_m, \
11714 current_config->alt_name.gmch_n, \
11715 current_config->alt_name.link_m, \
11716 current_config->alt_name.link_n, \
11717 pipe_config->name.tu, \
11718 pipe_config->name.gmch_m, \
11719 pipe_config->name.gmch_n, \
11720 pipe_config->name.link_m, \
11721 pipe_config->name.link_n); \
11722 ret = false; \
88adfff1
DV
11723 }
11724
1bd1bd80
DV
11725#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11726 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11727 pipe_config_err(adjust, __stringify(name), \
11728 "(%x) (expected %i, found %i)\n", \
11729 (mask), \
1bd1bd80
DV
11730 current_config->name & (mask), \
11731 pipe_config->name & (mask)); \
cfb23ed6 11732 ret = false; \
1bd1bd80
DV
11733 }
11734
5e550656
VS
11735#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11736 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11737 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11738 "(expected %i, found %i)\n", \
11739 current_config->name, \
11740 pipe_config->name); \
cfb23ed6 11741 ret = false; \
5e550656
VS
11742 }
11743
bb760063
DV
11744#define PIPE_CONF_QUIRK(quirk) \
11745 ((current_config->quirks | pipe_config->quirks) & (quirk))
11746
eccb140b
DV
11747 PIPE_CONF_CHECK_I(cpu_transcoder);
11748
08a24034
DV
11749 PIPE_CONF_CHECK_I(has_pch_encoder);
11750 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11751 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11752
90a6b7b0 11753 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11754 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11755
6315b5d3 11756 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11757 PIPE_CONF_CHECK_M_N(dp_m_n);
11758
cfb23ed6
ML
11759 if (current_config->has_drrs)
11760 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11761 } else
11762 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11763
253c84c8 11764 PIPE_CONF_CHECK_X(output_types);
a65347ba 11765
2d112de7
ACO
11766 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11767 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11768 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11769 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11770 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11771 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11772
2d112de7
ACO
11773 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11774 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11775 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11776 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11777 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11779
c93f54cf 11780 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11781 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11782 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11783 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11784 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11785 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11786
9ed109a7
DV
11787 PIPE_CONF_CHECK_I(has_audio);
11788
2d112de7 11789 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11790 DRM_MODE_FLAG_INTERLACE);
11791
bb760063 11792 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11793 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11794 DRM_MODE_FLAG_PHSYNC);
2d112de7 11795 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11796 DRM_MODE_FLAG_NHSYNC);
2d112de7 11797 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11798 DRM_MODE_FLAG_PVSYNC);
2d112de7 11799 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11800 DRM_MODE_FLAG_NVSYNC);
11801 }
045ac3b5 11802
333b8ca8 11803 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11804 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11805 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11806 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11807 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11808
bfd16b2a
ML
11809 if (!adjust) {
11810 PIPE_CONF_CHECK_I(pipe_src_w);
11811 PIPE_CONF_CHECK_I(pipe_src_h);
11812
11813 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11814 if (current_config->pch_pfit.enabled) {
11815 PIPE_CONF_CHECK_X(pch_pfit.pos);
11816 PIPE_CONF_CHECK_X(pch_pfit.size);
11817 }
2fa2fe9a 11818
7aefe2b5 11819 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11820 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11821 }
a1b2278e 11822
e59150dc 11823 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11824 if (IS_HASWELL(dev_priv))
e59150dc 11825 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11826
282740f7
VS
11827 PIPE_CONF_CHECK_I(double_wide);
11828
8106ddbd 11829 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11830 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11831 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11832 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11833 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11834 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11835 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11836 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11837 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11838 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11839
47eacbab
VS
11840 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11841 PIPE_CONF_CHECK_X(dsi_pll.div);
11842
9beb5fea 11843 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11844 PIPE_CONF_CHECK_I(pipe_bpp);
11845
2d112de7 11846 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11847 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11848
66e985c0 11849#undef PIPE_CONF_CHECK_X
08a24034 11850#undef PIPE_CONF_CHECK_I
8106ddbd 11851#undef PIPE_CONF_CHECK_P
1bd1bd80 11852#undef PIPE_CONF_CHECK_FLAGS
5e550656 11853#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11854#undef PIPE_CONF_QUIRK
88adfff1 11855
cfb23ed6 11856 return ret;
0e8ffe1b
DV
11857}
11858
e3b247da
VS
11859static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11860 const struct intel_crtc_state *pipe_config)
11861{
11862 if (pipe_config->has_pch_encoder) {
21a727b3 11863 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11864 &pipe_config->fdi_m_n);
11865 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11866
11867 /*
11868 * FDI already provided one idea for the dotclock.
11869 * Yell if the encoder disagrees.
11870 */
11871 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11872 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11873 fdi_dotclock, dotclock);
11874 }
11875}
11876
c0ead703
ML
11877static void verify_wm_state(struct drm_crtc *crtc,
11878 struct drm_crtc_state *new_state)
08db6652 11879{
6315b5d3 11880 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11881 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11882 struct skl_pipe_wm hw_wm, *sw_wm;
11883 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11884 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11886 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11887 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11888
6315b5d3 11889 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11890 return;
11891
3de8a14c 11892 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11893 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11894
08db6652
DL
11895 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11896 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11897
e7c84544 11898 /* planes */
8b364b41 11899 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11900 hw_plane_wm = &hw_wm.planes[plane];
11901 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11902
3de8a14c 11903 /* Watermarks */
11904 for (level = 0; level <= max_level; level++) {
11905 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11906 &sw_plane_wm->wm[level]))
11907 continue;
11908
11909 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",
11910 pipe_name(pipe), plane + 1, level,
11911 sw_plane_wm->wm[level].plane_en,
11912 sw_plane_wm->wm[level].plane_res_b,
11913 sw_plane_wm->wm[level].plane_res_l,
11914 hw_plane_wm->wm[level].plane_en,
11915 hw_plane_wm->wm[level].plane_res_b,
11916 hw_plane_wm->wm[level].plane_res_l);
11917 }
08db6652 11918
3de8a14c 11919 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11920 &sw_plane_wm->trans_wm)) {
11921 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",
11922 pipe_name(pipe), plane + 1,
11923 sw_plane_wm->trans_wm.plane_en,
11924 sw_plane_wm->trans_wm.plane_res_b,
11925 sw_plane_wm->trans_wm.plane_res_l,
11926 hw_plane_wm->trans_wm.plane_en,
11927 hw_plane_wm->trans_wm.plane_res_b,
11928 hw_plane_wm->trans_wm.plane_res_l);
11929 }
11930
11931 /* DDB */
11932 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11933 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11934
11935 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11936 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11937 pipe_name(pipe), plane + 1,
11938 sw_ddb_entry->start, sw_ddb_entry->end,
11939 hw_ddb_entry->start, hw_ddb_entry->end);
11940 }
e7c84544 11941 }
08db6652 11942
27082493
L
11943 /*
11944 * cursor
11945 * If the cursor plane isn't active, we may not have updated it's ddb
11946 * allocation. In that case since the ddb allocation will be updated
11947 * once the plane becomes visible, we can skip this check
11948 */
11949 if (intel_crtc->cursor_addr) {
3de8a14c 11950 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11951 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11952
11953 /* Watermarks */
11954 for (level = 0; level <= max_level; level++) {
11955 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11956 &sw_plane_wm->wm[level]))
11957 continue;
11958
11959 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",
11960 pipe_name(pipe), level,
11961 sw_plane_wm->wm[level].plane_en,
11962 sw_plane_wm->wm[level].plane_res_b,
11963 sw_plane_wm->wm[level].plane_res_l,
11964 hw_plane_wm->wm[level].plane_en,
11965 hw_plane_wm->wm[level].plane_res_b,
11966 hw_plane_wm->wm[level].plane_res_l);
11967 }
11968
11969 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11970 &sw_plane_wm->trans_wm)) {
11971 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11972 pipe_name(pipe),
11973 sw_plane_wm->trans_wm.plane_en,
11974 sw_plane_wm->trans_wm.plane_res_b,
11975 sw_plane_wm->trans_wm.plane_res_l,
11976 hw_plane_wm->trans_wm.plane_en,
11977 hw_plane_wm->trans_wm.plane_res_b,
11978 hw_plane_wm->trans_wm.plane_res_l);
11979 }
11980
11981 /* DDB */
11982 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11983 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11984
3de8a14c 11985 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11986 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11987 pipe_name(pipe),
3de8a14c 11988 sw_ddb_entry->start, sw_ddb_entry->end,
11989 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11990 }
08db6652
DL
11991 }
11992}
11993
91d1b4bd 11994static void
677100ce
ML
11995verify_connector_state(struct drm_device *dev,
11996 struct drm_atomic_state *state,
11997 struct drm_crtc *crtc)
8af6cf88 11998{
35dd3c64 11999 struct drm_connector *connector;
aa5e9b47 12000 struct drm_connector_state *new_conn_state;
677100ce 12001 int i;
8af6cf88 12002
aa5e9b47 12003 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 12004 struct drm_encoder *encoder = connector->encoder;
ad3c558f 12005
aa5e9b47 12006 if (new_conn_state->crtc != crtc)
e7c84544
ML
12007 continue;
12008
5a21b665 12009 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 12010
aa5e9b47 12011 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 12012 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12013 }
91d1b4bd
DV
12014}
12015
12016static void
86b04268 12017verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
DV
12018{
12019 struct intel_encoder *encoder;
86b04268
DV
12020 struct drm_connector *connector;
12021 struct drm_connector_state *old_conn_state, *new_conn_state;
12022 int i;
8af6cf88 12023
b2784e15 12024 for_each_intel_encoder(dev, encoder) {
86b04268 12025 bool enabled = false, found = false;
4d20cd86 12026 enum pipe pipe;
8af6cf88
DV
12027
12028 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12029 encoder->base.base.id,
8e329a03 12030 encoder->base.name);
8af6cf88 12031
86b04268
DV
12032 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
12033 new_conn_state, i) {
12034 if (old_conn_state->best_encoder == &encoder->base)
12035 found = true;
12036
12037 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 12038 continue;
86b04268 12039 found = enabled = true;
ad3c558f 12040
86b04268 12041 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
12042 encoder->base.crtc,
12043 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12044 }
86b04268
DV
12045
12046 if (!found)
12047 continue;
0e32b39c 12048
e2c719b7 12049 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12050 "encoder's enabled state mismatch "
12051 "(expected %i, found %i)\n",
12052 !!encoder->base.crtc, enabled);
7c60d198
ML
12053
12054 if (!encoder->base.crtc) {
4d20cd86 12055 bool active;
7c60d198 12056
4d20cd86
ML
12057 active = encoder->get_hw_state(encoder, &pipe);
12058 I915_STATE_WARN(active,
12059 "encoder detached but still enabled on pipe %c.\n",
12060 pipe_name(pipe));
7c60d198 12061 }
8af6cf88 12062 }
91d1b4bd
DV
12063}
12064
12065static void
c0ead703
ML
12066verify_crtc_state(struct drm_crtc *crtc,
12067 struct drm_crtc_state *old_crtc_state,
12068 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12069{
e7c84544 12070 struct drm_device *dev = crtc->dev;
fac5e23e 12071 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12072 struct intel_encoder *encoder;
e7c84544
ML
12073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12074 struct intel_crtc_state *pipe_config, *sw_config;
12075 struct drm_atomic_state *old_state;
12076 bool active;
045ac3b5 12077
e7c84544 12078 old_state = old_crtc_state->state;
ec2dc6a0 12079 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12080 pipe_config = to_intel_crtc_state(old_crtc_state);
12081 memset(pipe_config, 0, sizeof(*pipe_config));
12082 pipe_config->base.crtc = crtc;
12083 pipe_config->base.state = old_state;
8af6cf88 12084
78108b7c 12085 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12086
e7c84544 12087 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12088
e7c84544
ML
12089 /* hw state is inconsistent with the pipe quirk */
12090 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12091 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12092 active = new_crtc_state->active;
6c49f241 12093
e7c84544
ML
12094 I915_STATE_WARN(new_crtc_state->active != active,
12095 "crtc active state doesn't match with hw state "
12096 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12097
e7c84544
ML
12098 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12099 "transitional active state does not match atomic hw state "
12100 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12101
e7c84544
ML
12102 for_each_encoder_on_crtc(dev, crtc, encoder) {
12103 enum pipe pipe;
4d20cd86 12104
e7c84544
ML
12105 active = encoder->get_hw_state(encoder, &pipe);
12106 I915_STATE_WARN(active != new_crtc_state->active,
12107 "[ENCODER:%i] active %i with crtc active %i\n",
12108 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12109
e7c84544
ML
12110 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12111 "Encoder connected to wrong pipe %c\n",
12112 pipe_name(pipe));
4d20cd86 12113
253c84c8
VS
12114 if (active) {
12115 pipe_config->output_types |= 1 << encoder->type;
e7c84544 12116 encoder->get_config(encoder, pipe_config);
253c84c8 12117 }
e7c84544 12118 }
53d9f4e9 12119
a7d1b3f4
VS
12120 intel_crtc_compute_pixel_rate(pipe_config);
12121
e7c84544
ML
12122 if (!new_crtc_state->active)
12123 return;
cfb23ed6 12124
e7c84544 12125 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12126
e7c84544 12127 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 12128 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12129 pipe_config, false)) {
12130 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12131 intel_dump_pipe_config(intel_crtc, pipe_config,
12132 "[hw state]");
12133 intel_dump_pipe_config(intel_crtc, sw_config,
12134 "[sw state]");
8af6cf88
DV
12135 }
12136}
12137
91d1b4bd 12138static void
c0ead703
ML
12139verify_single_dpll_state(struct drm_i915_private *dev_priv,
12140 struct intel_shared_dpll *pll,
12141 struct drm_crtc *crtc,
12142 struct drm_crtc_state *new_state)
91d1b4bd 12143{
91d1b4bd 12144 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
12145 unsigned crtc_mask;
12146 bool active;
5358901f 12147
e7c84544 12148 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12149
e7c84544 12150 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 12151
e7c84544 12152 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12153
e7c84544
ML
12154 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12155 I915_STATE_WARN(!pll->on && pll->active_mask,
12156 "pll in active use but not on in sw tracking\n");
12157 I915_STATE_WARN(pll->on && !pll->active_mask,
12158 "pll is on but not used by any active crtc\n");
12159 I915_STATE_WARN(pll->on != active,
12160 "pll on state mismatch (expected %i, found %i)\n",
12161 pll->on, active);
12162 }
5358901f 12163
e7c84544 12164 if (!crtc) {
2c42e535 12165 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12166 "more active pll users than references: %x vs %x\n",
2c42e535 12167 pll->active_mask, pll->state.crtc_mask);
5358901f 12168
e7c84544
ML
12169 return;
12170 }
12171
12172 crtc_mask = 1 << drm_crtc_index(crtc);
12173
12174 if (new_state->active)
12175 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12176 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12177 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12178 else
12179 I915_STATE_WARN(pll->active_mask & crtc_mask,
12180 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12181 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12182
2c42e535 12183 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12184 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12185 crtc_mask, pll->state.crtc_mask);
66e985c0 12186
2c42e535 12187 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12188 &dpll_hw_state,
12189 sizeof(dpll_hw_state)),
12190 "pll hw state mismatch\n");
12191}
12192
12193static void
c0ead703
ML
12194verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12195 struct drm_crtc_state *old_crtc_state,
12196 struct drm_crtc_state *new_crtc_state)
e7c84544 12197{
fac5e23e 12198 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12199 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12200 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12201
12202 if (new_state->shared_dpll)
c0ead703 12203 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12204
12205 if (old_state->shared_dpll &&
12206 old_state->shared_dpll != new_state->shared_dpll) {
12207 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12208 struct intel_shared_dpll *pll = old_state->shared_dpll;
12209
12210 I915_STATE_WARN(pll->active_mask & crtc_mask,
12211 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12212 pipe_name(drm_crtc_index(crtc)));
2c42e535 12213 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12214 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12215 pipe_name(drm_crtc_index(crtc)));
5358901f 12216 }
8af6cf88
DV
12217}
12218
e7c84544 12219static void
c0ead703 12220intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12221 struct drm_atomic_state *state,
12222 struct drm_crtc_state *old_state,
12223 struct drm_crtc_state *new_state)
e7c84544 12224{
5a21b665
DV
12225 if (!needs_modeset(new_state) &&
12226 !to_intel_crtc_state(new_state)->update_pipe)
12227 return;
12228
c0ead703 12229 verify_wm_state(crtc, new_state);
677100ce 12230 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12231 verify_crtc_state(crtc, old_state, new_state);
12232 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12233}
12234
12235static void
c0ead703 12236verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12237{
fac5e23e 12238 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12239 int i;
12240
12241 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12242 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12243}
12244
12245static void
677100ce
ML
12246intel_modeset_verify_disabled(struct drm_device *dev,
12247 struct drm_atomic_state *state)
e7c84544 12248{
86b04268 12249 verify_encoder_state(dev, state);
677100ce 12250 verify_connector_state(dev, state, NULL);
c0ead703 12251 verify_disabled_dpll_state(dev);
e7c84544
ML
12252}
12253
80715b2f
VS
12254static void update_scanline_offset(struct intel_crtc *crtc)
12255{
4f8036a2 12256 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12257
12258 /*
12259 * The scanline counter increments at the leading edge of hsync.
12260 *
12261 * On most platforms it starts counting from vtotal-1 on the
12262 * first active line. That means the scanline counter value is
12263 * always one less than what we would expect. Ie. just after
12264 * start of vblank, which also occurs at start of hsync (on the
12265 * last active line), the scanline counter will read vblank_start-1.
12266 *
12267 * On gen2 the scanline counter starts counting from 1 instead
12268 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12269 * to keep the value positive), instead of adding one.
12270 *
12271 * On HSW+ the behaviour of the scanline counter depends on the output
12272 * type. For DP ports it behaves like most other platforms, but on HDMI
12273 * there's an extra 1 line difference. So we need to add two instead of
12274 * one to the value.
12275 */
4f8036a2 12276 if (IS_GEN2(dev_priv)) {
124abe07 12277 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12278 int vtotal;
12279
124abe07
VS
12280 vtotal = adjusted_mode->crtc_vtotal;
12281 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12282 vtotal /= 2;
12283
12284 crtc->scanline_offset = vtotal - 1;
4f8036a2 12285 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12286 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12287 crtc->scanline_offset = 2;
12288 } else
12289 crtc->scanline_offset = 1;
12290}
12291
ad421372 12292static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12293{
225da59b 12294 struct drm_device *dev = state->dev;
ed6739ef 12295 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12296 struct drm_crtc *crtc;
aa5e9b47 12297 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 12298 int i;
ed6739ef
ACO
12299
12300 if (!dev_priv->display.crtc_compute_clock)
ad421372 12301 return;
ed6739ef 12302
aa5e9b47 12303 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 12304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 12305 struct intel_shared_dpll *old_dpll =
aa5e9b47 12306 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 12307
aa5e9b47 12308 if (!needs_modeset(new_crtc_state))
225da59b
ACO
12309 continue;
12310
aa5e9b47 12311 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 12312
8106ddbd 12313 if (!old_dpll)
fb1a38a9 12314 continue;
0a9ab303 12315
a1c414ee 12316 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12317 }
ed6739ef
ACO
12318}
12319
99d736a2
ML
12320/*
12321 * This implements the workaround described in the "notes" section of the mode
12322 * set sequence documentation. When going from no pipes or single pipe to
12323 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12324 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12325 */
12326static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12327{
12328 struct drm_crtc_state *crtc_state;
12329 struct intel_crtc *intel_crtc;
12330 struct drm_crtc *crtc;
12331 struct intel_crtc_state *first_crtc_state = NULL;
12332 struct intel_crtc_state *other_crtc_state = NULL;
12333 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12334 int i;
12335
12336 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 12337 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
12338 intel_crtc = to_intel_crtc(crtc);
12339
12340 if (!crtc_state->active || !needs_modeset(crtc_state))
12341 continue;
12342
12343 if (first_crtc_state) {
12344 other_crtc_state = to_intel_crtc_state(crtc_state);
12345 break;
12346 } else {
12347 first_crtc_state = to_intel_crtc_state(crtc_state);
12348 first_pipe = intel_crtc->pipe;
12349 }
12350 }
12351
12352 /* No workaround needed? */
12353 if (!first_crtc_state)
12354 return 0;
12355
12356 /* w/a possibly needed, check how many crtc's are already enabled. */
12357 for_each_intel_crtc(state->dev, intel_crtc) {
12358 struct intel_crtc_state *pipe_config;
12359
12360 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12361 if (IS_ERR(pipe_config))
12362 return PTR_ERR(pipe_config);
12363
12364 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12365
12366 if (!pipe_config->base.active ||
12367 needs_modeset(&pipe_config->base))
12368 continue;
12369
12370 /* 2 or more enabled crtcs means no need for w/a */
12371 if (enabled_pipe != INVALID_PIPE)
12372 return 0;
12373
12374 enabled_pipe = intel_crtc->pipe;
12375 }
12376
12377 if (enabled_pipe != INVALID_PIPE)
12378 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12379 else if (other_crtc_state)
12380 other_crtc_state->hsw_workaround_pipe = first_pipe;
12381
12382 return 0;
12383}
12384
8d96561a
VS
12385static int intel_lock_all_pipes(struct drm_atomic_state *state)
12386{
12387 struct drm_crtc *crtc;
12388
12389 /* Add all pipes to the state */
12390 for_each_crtc(state->dev, crtc) {
12391 struct drm_crtc_state *crtc_state;
12392
12393 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12394 if (IS_ERR(crtc_state))
12395 return PTR_ERR(crtc_state);
12396 }
12397
12398 return 0;
12399}
12400
27c329ed
ML
12401static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12402{
12403 struct drm_crtc *crtc;
27c329ed 12404
8d96561a
VS
12405 /*
12406 * Add all pipes to the state, and force
12407 * a modeset on all the active ones.
12408 */
27c329ed 12409 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12410 struct drm_crtc_state *crtc_state;
12411 int ret;
12412
27c329ed
ML
12413 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12414 if (IS_ERR(crtc_state))
12415 return PTR_ERR(crtc_state);
12416
12417 if (!crtc_state->active || needs_modeset(crtc_state))
12418 continue;
12419
12420 crtc_state->mode_changed = true;
12421
12422 ret = drm_atomic_add_affected_connectors(state, crtc);
12423 if (ret)
9780aad5 12424 return ret;
27c329ed
ML
12425
12426 ret = drm_atomic_add_affected_planes(state, crtc);
12427 if (ret)
9780aad5 12428 return ret;
27c329ed
ML
12429 }
12430
9780aad5 12431 return 0;
27c329ed
ML
12432}
12433
c347a676 12434static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12435{
565602d7 12436 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12437 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 12438 struct drm_crtc *crtc;
aa5e9b47 12439 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 12440 int ret = 0, i;
054518dd 12441
b359283a
ML
12442 if (!check_digital_port_conflicts(state)) {
12443 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12444 return -EINVAL;
12445 }
12446
565602d7
ML
12447 intel_state->modeset = true;
12448 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12449 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12450 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 12451
aa5e9b47
ML
12452 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12453 if (new_crtc_state->active)
565602d7
ML
12454 intel_state->active_crtcs |= 1 << i;
12455 else
12456 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 12457
aa5e9b47 12458 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 12459 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12460 }
12461
054518dd
ACO
12462 /*
12463 * See if the config requires any additional preparation, e.g.
12464 * to adjust global state with pipes off. We need to do this
12465 * here so we can get the modeset_pipe updated config for the new
12466 * mode set on this crtc. For other crtcs we need to use the
12467 * adjusted_mode bits in the crtc directly.
12468 */
27c329ed 12469 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12470 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12471 if (ret < 0)
12472 return ret;
27c329ed 12473
8d96561a 12474 /*
bb0f4aab 12475 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12476 * holding all the crtc locks, even if we don't end up
12477 * touching the hardware
12478 */
bb0f4aab
VS
12479 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
12480 &intel_state->cdclk.logical)) {
8d96561a
VS
12481 ret = intel_lock_all_pipes(state);
12482 if (ret < 0)
12483 return ret;
12484 }
12485
12486 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
12487 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
12488 &intel_state->cdclk.actual)) {
27c329ed 12489 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12490 if (ret < 0)
12491 return ret;
12492 }
e8788cbc 12493
bb0f4aab
VS
12494 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12495 intel_state->cdclk.logical.cdclk,
12496 intel_state->cdclk.actual.cdclk);
e0ca7a6b 12497 } else {
bb0f4aab 12498 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12499 }
054518dd 12500
ad421372 12501 intel_modeset_clear_plls(state);
054518dd 12502
565602d7 12503 if (IS_HASWELL(dev_priv))
ad421372 12504 return haswell_mode_set_planes_workaround(state);
99d736a2 12505
ad421372 12506 return 0;
c347a676
ACO
12507}
12508
aa363136
MR
12509/*
12510 * Handle calculation of various watermark data at the end of the atomic check
12511 * phase. The code here should be run after the per-crtc and per-plane 'check'
12512 * handlers to ensure that all derived state has been updated.
12513 */
55994c2c 12514static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12515{
12516 struct drm_device *dev = state->dev;
98d39494 12517 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12518
12519 /* Is there platform-specific watermark information to calculate? */
12520 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12521 return dev_priv->display.compute_global_watermarks(state);
12522
12523 return 0;
aa363136
MR
12524}
12525
74c090b1
ML
12526/**
12527 * intel_atomic_check - validate state object
12528 * @dev: drm device
12529 * @state: state to validate
12530 */
12531static int intel_atomic_check(struct drm_device *dev,
12532 struct drm_atomic_state *state)
c347a676 12533{
dd8b3bdb 12534 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12535 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12536 struct drm_crtc *crtc;
aa5e9b47 12537 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12538 int ret, i;
61333b60 12539 bool any_ms = false;
c347a676 12540
74c090b1 12541 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12542 if (ret)
12543 return ret;
12544
aa5e9b47 12545 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12546 struct intel_crtc_state *pipe_config =
12547 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12548
12549 /* Catch I915_MODE_FLAG_INHERITED */
aa5e9b47 12550 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
1ed51de9 12551 crtc_state->mode_changed = true;
cfb23ed6 12552
af4a879e 12553 if (!needs_modeset(crtc_state))
c347a676
ACO
12554 continue;
12555
af4a879e
DV
12556 if (!crtc_state->enable) {
12557 any_ms = true;
cfb23ed6 12558 continue;
af4a879e 12559 }
cfb23ed6 12560
26495481
DV
12561 /* FIXME: For only active_changed we shouldn't need to do any
12562 * state recomputation at all. */
12563
1ed51de9
DV
12564 ret = drm_atomic_add_affected_connectors(state, crtc);
12565 if (ret)
12566 return ret;
b359283a 12567
cfb23ed6 12568 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12569 if (ret) {
12570 intel_dump_pipe_config(to_intel_crtc(crtc),
12571 pipe_config, "[failed]");
c347a676 12572 return ret;
25aa1c39 12573 }
c347a676 12574
73831236 12575 if (i915.fastboot &&
6315b5d3 12576 intel_pipe_config_compare(dev_priv,
aa5e9b47 12577 to_intel_crtc_state(old_crtc_state),
1ed51de9 12578 pipe_config, true)) {
26495481 12579 crtc_state->mode_changed = false;
aa5e9b47 12580 pipe_config->update_pipe = true;
26495481
DV
12581 }
12582
af4a879e 12583 if (needs_modeset(crtc_state))
26495481 12584 any_ms = true;
cfb23ed6 12585
af4a879e
DV
12586 ret = drm_atomic_add_affected_planes(state, crtc);
12587 if (ret)
12588 return ret;
61333b60 12589
26495481
DV
12590 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12591 needs_modeset(crtc_state) ?
12592 "[modeset]" : "[fastset]");
c347a676
ACO
12593 }
12594
61333b60
ML
12595 if (any_ms) {
12596 ret = intel_modeset_checks(state);
12597
12598 if (ret)
12599 return ret;
e0ca7a6b 12600 } else {
bb0f4aab 12601 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12602 }
76305b1a 12603
dd8b3bdb 12604 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12605 if (ret)
12606 return ret;
12607
f51be2e0 12608 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12609 return calc_watermark_data(state);
054518dd
ACO
12610}
12611
5008e874 12612static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12613 struct drm_atomic_state *state)
5008e874 12614{
fac5e23e 12615 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
12616 struct drm_crtc_state *crtc_state;
12617 struct drm_crtc *crtc;
12618 int i, ret;
12619
aa5e9b47 12620 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
5a21b665 12621 if (state->legacy_cursor_update)
a6747b73
ML
12622 continue;
12623
5a21b665
DV
12624 ret = intel_crtc_wait_for_pending_flips(crtc);
12625 if (ret)
12626 return ret;
5008e874 12627
5a21b665
DV
12628 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
12629 flush_workqueue(dev_priv->wq);
d55dbd06
ML
12630 }
12631
f935675f
ML
12632 ret = mutex_lock_interruptible(&dev->struct_mutex);
12633 if (ret)
12634 return ret;
12635
5008e874 12636 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 12637 mutex_unlock(&dev->struct_mutex);
7580d774 12638
5008e874
ML
12639 return ret;
12640}
12641
a2991414
ML
12642u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12643{
12644 struct drm_device *dev = crtc->base.dev;
12645
12646 if (!dev->max_vblank_count)
12647 return drm_accurate_vblank_count(&crtc->base);
12648
12649 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12650}
12651
5a21b665
DV
12652static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
12653 struct drm_i915_private *dev_priv,
12654 unsigned crtc_mask)
e8861675 12655{
5a21b665
DV
12656 unsigned last_vblank_count[I915_MAX_PIPES];
12657 enum pipe pipe;
12658 int ret;
e8861675 12659
5a21b665
DV
12660 if (!crtc_mask)
12661 return;
e8861675 12662
5a21b665 12663 for_each_pipe(dev_priv, pipe) {
98187836
VS
12664 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12665 pipe);
e8861675 12666
5a21b665 12667 if (!((1 << pipe) & crtc_mask))
e8861675
ML
12668 continue;
12669
e2af48c6 12670 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
DV
12671 if (WARN_ON(ret != 0)) {
12672 crtc_mask &= ~(1 << pipe);
12673 continue;
e8861675
ML
12674 }
12675
e2af48c6 12676 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
12677 }
12678
5a21b665 12679 for_each_pipe(dev_priv, pipe) {
98187836
VS
12680 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
12681 pipe);
5a21b665 12682 long lret;
e8861675 12683
5a21b665
DV
12684 if (!((1 << pipe) & crtc_mask))
12685 continue;
d55dbd06 12686
5a21b665
DV
12687 lret = wait_event_timeout(dev->vblank[pipe].queue,
12688 last_vblank_count[pipe] !=
e2af48c6 12689 drm_crtc_vblank_count(&crtc->base),
5a21b665 12690 msecs_to_jiffies(50));
d55dbd06 12691
5a21b665 12692 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 12693
e2af48c6 12694 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
12695 }
12696}
12697
5a21b665 12698static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 12699{
5a21b665
DV
12700 /* fb updated, need to unpin old fb */
12701 if (crtc_state->fb_changed)
12702 return true;
a6747b73 12703
5a21b665
DV
12704 /* wm changes, need vblank before final wm's */
12705 if (crtc_state->update_wm_post)
12706 return true;
a6747b73 12707
5eeb798b 12708 if (crtc_state->wm.need_postvbl_update)
5a21b665 12709 return true;
a6747b73 12710
5a21b665 12711 return false;
e8861675
ML
12712}
12713
896e5bb0
L
12714static void intel_update_crtc(struct drm_crtc *crtc,
12715 struct drm_atomic_state *state,
12716 struct drm_crtc_state *old_crtc_state,
aa5e9b47 12717 struct drm_crtc_state *new_crtc_state,
896e5bb0
L
12718 unsigned int *crtc_vblank_mask)
12719{
12720 struct drm_device *dev = crtc->dev;
12721 struct drm_i915_private *dev_priv = to_i915(dev);
12722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12723 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12724 bool modeset = needs_modeset(new_crtc_state);
896e5bb0
L
12725
12726 if (modeset) {
12727 update_scanline_offset(intel_crtc);
12728 dev_priv->display.crtc_enable(pipe_config, state);
12729 } else {
aa5e9b47
ML
12730 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12731 pipe_config);
896e5bb0
L
12732 }
12733
12734 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12735 intel_fbc_enable(
12736 intel_crtc, pipe_config,
12737 to_intel_plane_state(crtc->primary->state));
12738 }
12739
12740 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
12741
12742 if (needs_vblank_wait(pipe_config))
12743 *crtc_vblank_mask |= drm_crtc_mask(crtc);
12744}
12745
12746static void intel_update_crtcs(struct drm_atomic_state *state,
12747 unsigned int *crtc_vblank_mask)
12748{
12749 struct drm_crtc *crtc;
aa5e9b47 12750 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12751 int i;
12752
aa5e9b47
ML
12753 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12754 if (!new_crtc_state->active)
896e5bb0
L
12755 continue;
12756
12757 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12758 new_crtc_state, crtc_vblank_mask);
896e5bb0
L
12759 }
12760}
12761
27082493
L
12762static void skl_update_crtcs(struct drm_atomic_state *state,
12763 unsigned int *crtc_vblank_mask)
12764{
0f0f74bc 12765 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12766 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12767 struct drm_crtc *crtc;
ce0ba283 12768 struct intel_crtc *intel_crtc;
aa5e9b47 12769 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12770 struct intel_crtc_state *cstate;
27082493
L
12771 unsigned int updated = 0;
12772 bool progress;
12773 enum pipe pipe;
5eff503b
ML
12774 int i;
12775
12776 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12777
aa5e9b47 12778 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12779 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12780 if (new_crtc_state->active)
5eff503b 12781 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12782
12783 /*
12784 * Whenever the number of active pipes changes, we need to make sure we
12785 * update the pipes in the right order so that their ddb allocations
12786 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12787 * cause pipe underruns and other bad stuff.
12788 */
12789 do {
27082493
L
12790 progress = false;
12791
aa5e9b47 12792 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12793 bool vbl_wait = false;
12794 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12795
12796 intel_crtc = to_intel_crtc(crtc);
12797 cstate = to_intel_crtc_state(crtc->state);
12798 pipe = intel_crtc->pipe;
27082493 12799
5eff503b 12800 if (updated & cmask || !cstate->base.active)
27082493 12801 continue;
5eff503b
ML
12802
12803 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
12804 continue;
12805
12806 updated |= cmask;
5eff503b 12807 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12808
12809 /*
12810 * If this is an already active pipe, it's DDB changed,
12811 * and this isn't the last pipe that needs updating
12812 * then we need to wait for a vblank to pass for the
12813 * new ddb allocation to take effect.
12814 */
ce0ba283 12815 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12816 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12817 !new_crtc_state->active_changed &&
27082493
L
12818 intel_state->wm_results.dirty_pipes != updated)
12819 vbl_wait = true;
12820
12821 intel_update_crtc(crtc, state, old_crtc_state,
aa5e9b47 12822 new_crtc_state, crtc_vblank_mask);
27082493
L
12823
12824 if (vbl_wait)
0f0f74bc 12825 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12826
12827 progress = true;
12828 }
12829 } while (progress);
12830}
12831
ba318c61
CW
12832static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12833{
12834 struct intel_atomic_state *state, *next;
12835 struct llist_node *freed;
12836
12837 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12838 llist_for_each_entry_safe(state, next, freed, freed)
12839 drm_atomic_state_put(&state->base);
12840}
12841
12842static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12843{
12844 struct drm_i915_private *dev_priv =
12845 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12846
12847 intel_atomic_helper_free_state(dev_priv);
12848}
12849
94f05024 12850static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12851{
94f05024 12852 struct drm_device *dev = state->dev;
565602d7 12853 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12854 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 12855 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 12856 struct drm_crtc *crtc;
5a21b665 12857 struct intel_crtc_state *intel_cstate;
5a21b665 12858 bool hw_check = intel_state->modeset;
d8fc70b7 12859 u64 put_domains[I915_MAX_PIPES] = {};
5a21b665 12860 unsigned crtc_vblank_mask = 0;
e95433c7 12861 int i;
a6778b3c 12862
ea0000f0
DV
12863 drm_atomic_helper_wait_for_dependencies(state);
12864
c3b32658 12865 if (intel_state->modeset)
5a21b665 12866 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12867
aa5e9b47 12868 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
12869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12870
aa5e9b47
ML
12871 if (needs_modeset(new_crtc_state) ||
12872 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
DV
12873 hw_check = true;
12874
12875 put_domains[to_intel_crtc(crtc)->pipe] =
12876 modeset_get_crtc_power_domains(crtc,
aa5e9b47 12877 to_intel_crtc_state(new_crtc_state));
5a21b665
DV
12878 }
12879
aa5e9b47 12880 if (!needs_modeset(new_crtc_state))
61333b60
ML
12881 continue;
12882
aa5e9b47
ML
12883 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12884 to_intel_crtc_state(new_crtc_state));
460da916 12885
29ceb0e6
VS
12886 if (old_crtc_state->active) {
12887 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12888 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12889 intel_crtc->active = false;
58f9c0bc 12890 intel_fbc_disable(intel_crtc);
eddfcbcd 12891 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12892
12893 /*
12894 * Underruns don't always raise
12895 * interrupts, so check manually.
12896 */
12897 intel_check_cpu_fifo_underruns(dev_priv);
12898 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12899
e62929b3
ML
12900 if (!crtc->state->active) {
12901 /*
12902 * Make sure we don't call initial_watermarks
12903 * for ILK-style watermark updates.
ff32c54e
VS
12904 *
12905 * No clue what this is supposed to achieve.
e62929b3 12906 */
ff32c54e 12907 if (INTEL_GEN(dev_priv) >= 9)
e62929b3
ML
12908 dev_priv->display.initial_watermarks(intel_state,
12909 to_intel_crtc_state(crtc->state));
e62929b3 12910 }
a539205a 12911 }
b8cecdf5 12912 }
7758a113 12913
ea9d758d
DV
12914 /* Only after disabling all output pipelines that will be changed can we
12915 * update the the output configuration. */
4740b0f2 12916 intel_modeset_update_crtc_state(state);
f6e5b160 12917
565602d7 12918 if (intel_state->modeset) {
4740b0f2 12919 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12920
b0587e4d 12921 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12922
656d1b89
L
12923 /*
12924 * SKL workaround: bspec recommends we disable the SAGV when we
12925 * have more then one pipe enabled
12926 */
56feca91 12927 if (!intel_can_enable_sagv(state))
16dcdc4e 12928 intel_disable_sagv(dev_priv);
656d1b89 12929
677100ce 12930 intel_modeset_verify_disabled(dev, state);
4740b0f2 12931 }
47fab737 12932
896e5bb0 12933 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
12934 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12935 bool modeset = needs_modeset(new_crtc_state);
80715b2f 12936
1f7528c4 12937 /* Complete events for now disable pipes here. */
aa5e9b47 12938 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 12939 spin_lock_irq(&dev->event_lock);
aa5e9b47 12940 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
DV
12941 spin_unlock_irq(&dev->event_lock);
12942
aa5e9b47 12943 new_crtc_state->event = NULL;
1f7528c4 12944 }
177246a8
MR
12945 }
12946
896e5bb0
L
12947 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12948 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
12949
94f05024
DV
12950 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12951 * already, but still need the state for the delayed optimization. To
12952 * fix this:
12953 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12954 * - schedule that vblank worker _before_ calling hw_done
12955 * - at the start of commit_tail, cancel it _synchrously
12956 * - switch over to the vblank wait helper in the core after that since
12957 * we don't need out special handling any more.
12958 */
5a21b665
DV
12959 if (!state->legacy_cursor_update)
12960 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
12961
12962 /*
12963 * Now that the vblank has passed, we can go ahead and program the
12964 * optimal watermarks on platforms that need two-step watermark
12965 * programming.
12966 *
12967 * TODO: Move this (and other cleanup) to an async worker eventually.
12968 */
aa5e9b47
ML
12969 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12970 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
DV
12971
12972 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12973 dev_priv->display.optimize_watermarks(intel_state,
12974 intel_cstate);
5a21b665
DV
12975 }
12976
aa5e9b47 12977 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
DV
12978 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12979
12980 if (put_domains[i])
12981 modeset_put_power_domains(dev_priv, put_domains[i]);
12982
aa5e9b47 12983 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
DV
12984 }
12985
56feca91 12986 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12987 intel_enable_sagv(dev_priv);
656d1b89 12988
94f05024
DV
12989 drm_atomic_helper_commit_hw_done(state);
12990
5a21b665
DV
12991 if (intel_state->modeset)
12992 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
12993
12994 mutex_lock(&dev->struct_mutex);
12995 drm_atomic_helper_cleanup_planes(dev, state);
12996 mutex_unlock(&dev->struct_mutex);
12997
ea0000f0
DV
12998 drm_atomic_helper_commit_cleanup_done(state);
12999
0853695c 13000 drm_atomic_state_put(state);
f30da187 13001
75714940
MK
13002 /* As one of the primary mmio accessors, KMS has a high likelihood
13003 * of triggering bugs in unclaimed access. After we finish
13004 * modesetting, see if an error has been flagged, and if so
13005 * enable debugging for the next modeset - and hope we catch
13006 * the culprit.
13007 *
13008 * XXX note that we assume display power is on at this point.
13009 * This might hold true now but we need to add pm helper to check
13010 * unclaimed only when the hardware is on, as atomic commits
13011 * can happen also when the device is completely off.
13012 */
13013 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
ba318c61
CW
13014
13015 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
13016}
13017
13018static void intel_atomic_commit_work(struct work_struct *work)
13019{
c004a90b
CW
13020 struct drm_atomic_state *state =
13021 container_of(work, struct drm_atomic_state, commit_work);
13022
94f05024
DV
13023 intel_atomic_commit_tail(state);
13024}
13025
c004a90b
CW
13026static int __i915_sw_fence_call
13027intel_atomic_commit_ready(struct i915_sw_fence *fence,
13028 enum i915_sw_fence_notify notify)
13029{
13030 struct intel_atomic_state *state =
13031 container_of(fence, struct intel_atomic_state, commit_ready);
13032
13033 switch (notify) {
13034 case FENCE_COMPLETE:
13035 if (state->base.commit_work.func)
13036 queue_work(system_unbound_wq, &state->base.commit_work);
13037 break;
13038
13039 case FENCE_FREE:
eb955eee
CW
13040 {
13041 struct intel_atomic_helper *helper =
13042 &to_i915(state->base.dev)->atomic_helper;
13043
13044 if (llist_add(&state->freed, &helper->free_list))
13045 schedule_work(&helper->free_work);
13046 break;
13047 }
c004a90b
CW
13048 }
13049
13050 return NOTIFY_DONE;
13051}
13052
6c9c1b38
DV
13053static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13054{
aa5e9b47 13055 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 13056 struct drm_plane *plane;
6c9c1b38
DV
13057 int i;
13058
aa5e9b47 13059 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 13060 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 13061 intel_fb_obj(new_plane_state->fb),
faf5bf0a 13062 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
13063}
13064
94f05024
DV
13065/**
13066 * intel_atomic_commit - commit validated state object
13067 * @dev: DRM device
13068 * @state: the top-level driver state object
13069 * @nonblock: nonblocking commit
13070 *
13071 * This function commits a top-level state object that has been validated
13072 * with drm_atomic_helper_check().
13073 *
94f05024
DV
13074 * RETURNS
13075 * Zero for success or -errno.
13076 */
13077static int intel_atomic_commit(struct drm_device *dev,
13078 struct drm_atomic_state *state,
13079 bool nonblock)
13080{
13081 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13082 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
13083 int ret = 0;
13084
a5509abd
VS
13085 /*
13086 * The intel_legacy_cursor_update() fast path takes care
13087 * of avoiding the vblank waits for simple cursor
13088 * movement and flips. For cursor on/off and size changes,
13089 * we want to perform the vblank waits so that watermark
13090 * updates happen during the correct frames. Gen9+ have
13091 * double buffered watermarks and so shouldn't need this.
13092 */
13093 if (INTEL_GEN(dev_priv) < 9)
13094 state->legacy_cursor_update = false;
13095
94f05024
DV
13096 ret = drm_atomic_helper_setup_commit(state, nonblock);
13097 if (ret)
13098 return ret;
13099
c004a90b
CW
13100 drm_atomic_state_get(state);
13101 i915_sw_fence_init(&intel_state->commit_ready,
13102 intel_atomic_commit_ready);
94f05024 13103
d07f0e59 13104 ret = intel_atomic_prepare_commit(dev, state);
94f05024
DV
13105 if (ret) {
13106 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 13107 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
DV
13108 return ret;
13109 }
13110
13111 drm_atomic_helper_swap_state(state, true);
13112 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 13113 intel_shared_dpll_swap_state(state);
6c9c1b38 13114 intel_atomic_track_fbs(state);
94f05024 13115
c3b32658
ML
13116 if (intel_state->modeset) {
13117 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13118 sizeof(intel_state->min_pixclk));
13119 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
13120 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13121 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
13122 }
13123
0853695c 13124 drm_atomic_state_get(state);
c004a90b
CW
13125 INIT_WORK(&state->commit_work,
13126 nonblock ? intel_atomic_commit_work : NULL);
13127
13128 i915_sw_fence_commit(&intel_state->commit_ready);
13129 if (!nonblock) {
13130 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 13131 intel_atomic_commit_tail(state);
c004a90b 13132 }
75714940 13133
74c090b1 13134 return 0;
7f27126e
JB
13135}
13136
c0c36b94
CW
13137void intel_crtc_restore_mode(struct drm_crtc *crtc)
13138{
83a57153
ACO
13139 struct drm_device *dev = crtc->dev;
13140 struct drm_atomic_state *state;
e694eb02 13141 struct drm_crtc_state *crtc_state;
2bfb4627 13142 int ret;
83a57153
ACO
13143
13144 state = drm_atomic_state_alloc(dev);
13145 if (!state) {
78108b7c
VS
13146 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13147 crtc->base.id, crtc->name);
83a57153
ACO
13148 return;
13149 }
13150
e694eb02 13151 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13152
e694eb02
ML
13153retry:
13154 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13155 ret = PTR_ERR_OR_ZERO(crtc_state);
13156 if (!ret) {
13157 if (!crtc_state->active)
13158 goto out;
83a57153 13159
e694eb02 13160 crtc_state->mode_changed = true;
74c090b1 13161 ret = drm_atomic_commit(state);
83a57153
ACO
13162 }
13163
e694eb02
ML
13164 if (ret == -EDEADLK) {
13165 drm_atomic_state_clear(state);
13166 drm_modeset_backoff(state->acquire_ctx);
13167 goto retry;
4ed9fb37 13168 }
4be07317 13169
e694eb02 13170out:
0853695c 13171 drm_atomic_state_put(state);
c0c36b94
CW
13172}
13173
a8784875
BP
13174/*
13175 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13176 * drm_atomic_helper_legacy_gamma_set() directly.
13177 */
13178static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13179 u16 *red, u16 *green, u16 *blue,
13180 uint32_t size)
13181{
13182 struct drm_device *dev = crtc->dev;
13183 struct drm_mode_config *config = &dev->mode_config;
13184 struct drm_crtc_state *state;
13185 int ret;
13186
13187 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13188 if (ret)
13189 return ret;
13190
13191 /*
13192 * Make sure we update the legacy properties so this works when
13193 * atomic is not enabled.
13194 */
13195
13196 state = crtc->state;
13197
13198 drm_object_property_set_value(&crtc->base,
13199 config->degamma_lut_property,
13200 (state->degamma_lut) ?
13201 state->degamma_lut->base.id : 0);
13202
13203 drm_object_property_set_value(&crtc->base,
13204 config->ctm_property,
13205 (state->ctm) ?
13206 state->ctm->base.id : 0);
13207
13208 drm_object_property_set_value(&crtc->base,
13209 config->gamma_lut_property,
13210 (state->gamma_lut) ?
13211 state->gamma_lut->base.id : 0);
13212
13213 return 0;
13214}
13215
f6e5b160 13216static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 13217 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 13218 .set_config = drm_atomic_helper_set_config,
82cf435b 13219 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 13220 .destroy = intel_crtc_destroy,
4c01ded5 13221 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13222 .atomic_duplicate_state = intel_crtc_duplicate_state,
13223 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13224 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
13225};
13226
6beb8c23
MR
13227/**
13228 * intel_prepare_plane_fb - Prepare fb for usage on plane
13229 * @plane: drm plane to prepare for
13230 * @fb: framebuffer to prepare for presentation
13231 *
13232 * Prepares a framebuffer for usage on a display plane. Generally this
13233 * involves pinning the underlying object and updating the frontbuffer tracking
13234 * bits. Some older platforms need special physical address handling for
13235 * cursor planes.
13236 *
f935675f
ML
13237 * Must be called with struct_mutex held.
13238 *
6beb8c23
MR
13239 * Returns 0 on success, negative error code on failure.
13240 */
13241int
13242intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13243 struct drm_plane_state *new_state)
465c120c 13244{
c004a90b
CW
13245 struct intel_atomic_state *intel_state =
13246 to_intel_atomic_state(new_state->state);
b7f05d4a 13247 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13248 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13249 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13250 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13251 int ret;
465c120c 13252
57822dc6
CW
13253 if (obj) {
13254 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13255 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13256 const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13257
13258 ret = i915_gem_object_attach_phys(obj, align);
13259 if (ret) {
13260 DRM_DEBUG_KMS("failed to attach phys object\n");
13261 return ret;
13262 }
13263 } else {
13264 struct i915_vma *vma;
13265
13266 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
13267 if (IS_ERR(vma)) {
13268 DRM_DEBUG_KMS("failed to pin object\n");
13269 return PTR_ERR(vma);
13270 }
13271
13272 to_intel_plane_state(new_state)->vma = vma;
13273 }
13274 }
13275
1ee49399 13276 if (!obj && !old_obj)
465c120c
MR
13277 return 0;
13278
5008e874
ML
13279 if (old_obj) {
13280 struct drm_crtc_state *crtc_state =
c004a90b
CW
13281 drm_atomic_get_existing_crtc_state(new_state->state,
13282 plane->state->crtc);
5008e874
ML
13283
13284 /* Big Hammer, we also need to ensure that any pending
13285 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13286 * current scanout is retired before unpinning the old
13287 * framebuffer. Note that we rely on userspace rendering
13288 * into the buffer attached to the pipe they are waiting
13289 * on. If not, userspace generates a GPU hang with IPEHR
13290 * point to the MI_WAIT_FOR_EVENT.
13291 *
13292 * This should only fail upon a hung GPU, in which case we
13293 * can safely continue.
13294 */
c004a90b
CW
13295 if (needs_modeset(crtc_state)) {
13296 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13297 old_obj->resv, NULL,
13298 false, 0,
13299 GFP_KERNEL);
13300 if (ret < 0)
13301 return ret;
f4457ae7 13302 }
5008e874
ML
13303 }
13304
c004a90b
CW
13305 if (new_state->fence) { /* explicit fencing */
13306 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13307 new_state->fence,
13308 I915_FENCE_TIMEOUT,
13309 GFP_KERNEL);
13310 if (ret < 0)
13311 return ret;
13312 }
13313
c37efb99
CW
13314 if (!obj)
13315 return 0;
13316
c004a90b
CW
13317 if (!new_state->fence) { /* implicit fencing */
13318 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13319 obj->resv, NULL,
13320 false, I915_FENCE_TIMEOUT,
13321 GFP_KERNEL);
13322 if (ret < 0)
13323 return ret;
6b5e90f5
CW
13324
13325 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 13326 }
5a21b665 13327
d07f0e59 13328 return 0;
6beb8c23
MR
13329}
13330
38f3ce3a
MR
13331/**
13332 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13333 * @plane: drm plane to clean up for
13334 * @fb: old framebuffer that was on plane
13335 *
13336 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13337 *
13338 * Must be called with struct_mutex held.
38f3ce3a
MR
13339 */
13340void
13341intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13342 struct drm_plane_state *old_state)
38f3ce3a 13343{
be1e3415 13344 struct i915_vma *vma;
38f3ce3a 13345
be1e3415
CW
13346 /* Should only be called after a successful intel_prepare_plane_fb()! */
13347 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
13348 if (vma)
13349 intel_unpin_fb_vma(vma);
465c120c
MR
13350}
13351
6156a456
CK
13352int
13353skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13354{
5b7280f0 13355 struct drm_i915_private *dev_priv;
6156a456 13356 int max_scale;
5b7280f0 13357 int crtc_clock, max_dotclk;
6156a456 13358
bf8a0af0 13359 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13360 return DRM_PLANE_HELPER_NO_SCALING;
13361
5b7280f0
ACO
13362 dev_priv = to_i915(intel_crtc->base.dev);
13363
6156a456 13364 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13365 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13366
13367 if (IS_GEMINILAKE(dev_priv))
13368 max_dotclk *= 2;
6156a456 13369
5b7280f0 13370 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13371 return DRM_PLANE_HELPER_NO_SCALING;
13372
13373 /*
13374 * skl max scale is lower of:
13375 * close to 3 but not 3, -1 is for that purpose
13376 * or
13377 * cdclk/crtc_clock
13378 */
5b7280f0
ACO
13379 max_scale = min((1 << 16) * 3 - 1,
13380 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
13381
13382 return max_scale;
13383}
13384
465c120c 13385static int
3c692a41 13386intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13387 struct intel_crtc_state *crtc_state,
3c692a41
GP
13388 struct intel_plane_state *state)
13389{
b63a16f6 13390 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13391 struct drm_crtc *crtc = state->base.crtc;
6156a456 13392 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13393 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13394 bool can_position = false;
b63a16f6 13395 int ret;
465c120c 13396
b63a16f6 13397 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
13398 /* use scaler when colorkey is not required */
13399 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13400 min_scale = 1;
13401 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13402 }
d8106366 13403 can_position = true;
6156a456 13404 }
d8106366 13405
cc926387
DV
13406 ret = drm_plane_helper_check_state(&state->base,
13407 &state->clip,
13408 min_scale, max_scale,
13409 can_position, true);
b63a16f6
VS
13410 if (ret)
13411 return ret;
13412
cc926387 13413 if (!state->base.fb)
b63a16f6
VS
13414 return 0;
13415
13416 if (INTEL_GEN(dev_priv) >= 9) {
13417 ret = skl_check_plane_surface(state);
13418 if (ret)
13419 return ret;
13420 }
13421
13422 return 0;
14af293f
GP
13423}
13424
5a21b665
DV
13425static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13426 struct drm_crtc_state *old_crtc_state)
13427{
13428 struct drm_device *dev = crtc->dev;
62e0fb88 13429 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13430 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
13431 struct intel_crtc_state *intel_cstate =
13432 to_intel_crtc_state(crtc->state);
ccf010fb 13433 struct intel_crtc_state *old_intel_cstate =
5a21b665 13434 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13435 struct intel_atomic_state *old_intel_state =
13436 to_intel_atomic_state(old_crtc_state->state);
5a21b665
DV
13437 bool modeset = needs_modeset(crtc->state);
13438
567f0792
ML
13439 if (!modeset &&
13440 (intel_cstate->base.color_mgmt_changed ||
13441 intel_cstate->update_pipe)) {
13442 intel_color_set_csc(crtc->state);
13443 intel_color_load_luts(crtc->state);
13444 }
13445
5a21b665
DV
13446 /* Perform vblank evasion around commit operation */
13447 intel_pipe_update_start(intel_crtc);
13448
13449 if (modeset)
e62929b3 13450 goto out;
5a21b665 13451
ccf010fb
ML
13452 if (intel_cstate->update_pipe)
13453 intel_update_pipe_config(intel_crtc, old_intel_cstate);
13454 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13455 skl_detach_scalers(intel_crtc);
62e0fb88 13456
e62929b3 13457out:
ccf010fb
ML
13458 if (dev_priv->display.atomic_update_watermarks)
13459 dev_priv->display.atomic_update_watermarks(old_intel_state,
13460 intel_cstate);
5a21b665
DV
13461}
13462
13463static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13464 struct drm_crtc_state *old_crtc_state)
13465{
13466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13467
13468 intel_pipe_update_end(intel_crtc, NULL);
13469}
13470
cf4c7c12 13471/**
4a3b8769
MR
13472 * intel_plane_destroy - destroy a plane
13473 * @plane: plane to destroy
cf4c7c12 13474 *
4a3b8769
MR
13475 * Common destruction function for all types of planes (primary, cursor,
13476 * sprite).
cf4c7c12 13477 */
4a3b8769 13478void intel_plane_destroy(struct drm_plane *plane)
465c120c 13479{
465c120c 13480 drm_plane_cleanup(plane);
69ae561f 13481 kfree(to_intel_plane(plane));
465c120c
MR
13482}
13483
65a3fea0 13484const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13485 .update_plane = drm_atomic_helper_update_plane,
13486 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13487 .destroy = intel_plane_destroy,
c196e1d6 13488 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13489 .atomic_get_property = intel_plane_atomic_get_property,
13490 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13491 .atomic_duplicate_state = intel_plane_duplicate_state,
13492 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
13493};
13494
f79f2692
ML
13495static int
13496intel_legacy_cursor_update(struct drm_plane *plane,
13497 struct drm_crtc *crtc,
13498 struct drm_framebuffer *fb,
13499 int crtc_x, int crtc_y,
13500 unsigned int crtc_w, unsigned int crtc_h,
13501 uint32_t src_x, uint32_t src_y,
13502 uint32_t src_w, uint32_t src_h)
13503{
13504 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13505 int ret;
13506 struct drm_plane_state *old_plane_state, *new_plane_state;
13507 struct intel_plane *intel_plane = to_intel_plane(plane);
13508 struct drm_framebuffer *old_fb;
13509 struct drm_crtc_state *crtc_state = crtc->state;
be1e3415 13510 struct i915_vma *old_vma;
f79f2692
ML
13511
13512 /*
13513 * When crtc is inactive or there is a modeset pending,
13514 * wait for it to complete in the slowpath
13515 */
13516 if (!crtc_state->active || needs_modeset(crtc_state) ||
13517 to_intel_crtc_state(crtc_state)->update_pipe)
13518 goto slow;
13519
13520 old_plane_state = plane->state;
13521
13522 /*
13523 * If any parameters change that may affect watermarks,
13524 * take the slowpath. Only changing fb or position should be
13525 * in the fastpath.
13526 */
13527 if (old_plane_state->crtc != crtc ||
13528 old_plane_state->src_w != src_w ||
13529 old_plane_state->src_h != src_h ||
13530 old_plane_state->crtc_w != crtc_w ||
13531 old_plane_state->crtc_h != crtc_h ||
a5509abd 13532 !old_plane_state->fb != !fb)
f79f2692
ML
13533 goto slow;
13534
13535 new_plane_state = intel_plane_duplicate_state(plane);
13536 if (!new_plane_state)
13537 return -ENOMEM;
13538
13539 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13540
13541 new_plane_state->src_x = src_x;
13542 new_plane_state->src_y = src_y;
13543 new_plane_state->src_w = src_w;
13544 new_plane_state->src_h = src_h;
13545 new_plane_state->crtc_x = crtc_x;
13546 new_plane_state->crtc_y = crtc_y;
13547 new_plane_state->crtc_w = crtc_w;
13548 new_plane_state->crtc_h = crtc_h;
13549
13550 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
13551 to_intel_plane_state(new_plane_state));
13552 if (ret)
13553 goto out_free;
13554
f79f2692
ML
13555 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13556 if (ret)
13557 goto out_free;
13558
13559 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
13560 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
13561
13562 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13563 if (ret) {
13564 DRM_DEBUG_KMS("failed to attach phys object\n");
13565 goto out_unlock;
13566 }
13567 } else {
13568 struct i915_vma *vma;
13569
13570 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13571 if (IS_ERR(vma)) {
13572 DRM_DEBUG_KMS("failed to pin object\n");
13573
13574 ret = PTR_ERR(vma);
13575 goto out_unlock;
13576 }
be1e3415
CW
13577
13578 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13579 }
13580
13581 old_fb = old_plane_state->fb;
be1e3415 13582 old_vma = to_intel_plane_state(old_plane_state)->vma;
f79f2692
ML
13583
13584 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13585 intel_plane->frontbuffer_bit);
13586
13587 /* Swap plane state */
13588 new_plane_state->fence = old_plane_state->fence;
13589 *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
13590 new_plane_state->fence = NULL;
13591 new_plane_state->fb = old_fb;
be1e3415 13592 to_intel_plane_state(new_plane_state)->vma = old_vma;
f79f2692 13593
72259536
VS
13594 if (plane->state->visible) {
13595 trace_intel_update_plane(plane, to_intel_crtc(crtc));
a5509abd
VS
13596 intel_plane->update_plane(plane,
13597 to_intel_crtc_state(crtc->state),
13598 to_intel_plane_state(plane->state));
72259536
VS
13599 } else {
13600 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
a5509abd 13601 intel_plane->disable_plane(plane, crtc);
72259536 13602 }
f79f2692
ML
13603
13604 intel_cleanup_plane_fb(plane, new_plane_state);
13605
13606out_unlock:
13607 mutex_unlock(&dev_priv->drm.struct_mutex);
13608out_free:
13609 intel_plane_destroy_state(plane, new_plane_state);
13610 return ret;
13611
f79f2692
ML
13612slow:
13613 return drm_atomic_helper_update_plane(plane, crtc, fb,
13614 crtc_x, crtc_y, crtc_w, crtc_h,
13615 src_x, src_y, src_w, src_h);
13616}
13617
13618static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13619 .update_plane = intel_legacy_cursor_update,
13620 .disable_plane = drm_atomic_helper_disable_plane,
13621 .destroy = intel_plane_destroy,
13622 .set_property = drm_atomic_helper_plane_set_property,
13623 .atomic_get_property = intel_plane_atomic_get_property,
13624 .atomic_set_property = intel_plane_atomic_set_property,
13625 .atomic_duplicate_state = intel_plane_duplicate_state,
13626 .atomic_destroy_state = intel_plane_destroy_state,
13627};
13628
b079bd17 13629static struct intel_plane *
580503c7 13630intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13631{
fca0ce2a
VS
13632 struct intel_plane *primary = NULL;
13633 struct intel_plane_state *state = NULL;
465c120c 13634 const uint32_t *intel_primary_formats;
93ca7e00 13635 unsigned int supported_rotations;
45e3743a 13636 unsigned int num_formats;
fca0ce2a 13637 int ret;
465c120c
MR
13638
13639 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13640 if (!primary) {
13641 ret = -ENOMEM;
fca0ce2a 13642 goto fail;
b079bd17 13643 }
465c120c 13644
8e7d688b 13645 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13646 if (!state) {
13647 ret = -ENOMEM;
fca0ce2a 13648 goto fail;
b079bd17
VS
13649 }
13650
8e7d688b 13651 primary->base.state = &state->base;
ea2c67bb 13652
465c120c
MR
13653 primary->can_scale = false;
13654 primary->max_downscale = 1;
580503c7 13655 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13656 primary->can_scale = true;
af99ceda 13657 state->scaler_id = -1;
6156a456 13658 }
465c120c 13659 primary->pipe = pipe;
e3c566df
VS
13660 /*
13661 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13662 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13663 */
13664 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13665 primary->plane = (enum plane) !pipe;
13666 else
13667 primary->plane = (enum plane) pipe;
b14e5848 13668 primary->id = PLANE_PRIMARY;
a9ff8714 13669 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13670 primary->check_plane = intel_check_primary_plane;
465c120c 13671
580503c7 13672 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
13673 intel_primary_formats = skl_primary_formats;
13674 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
13675
13676 primary->update_plane = skylake_update_primary_plane;
13677 primary->disable_plane = skylake_disable_primary_plane;
6e266956 13678 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
13679 intel_primary_formats = i965_primary_formats;
13680 num_formats = ARRAY_SIZE(i965_primary_formats);
13681
13682 primary->update_plane = ironlake_update_primary_plane;
13683 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 13684 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13685 intel_primary_formats = i965_primary_formats;
13686 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
13687
13688 primary->update_plane = i9xx_update_primary_plane;
13689 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13690 } else {
13691 intel_primary_formats = i8xx_primary_formats;
13692 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
13693
13694 primary->update_plane = i9xx_update_primary_plane;
13695 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13696 }
13697
580503c7
VS
13698 if (INTEL_GEN(dev_priv) >= 9)
13699 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13700 0, &intel_plane_funcs,
38573dc1
VS
13701 intel_primary_formats, num_formats,
13702 DRM_PLANE_TYPE_PRIMARY,
13703 "plane 1%c", pipe_name(pipe));
9beb5fea 13704 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13705 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13706 0, &intel_plane_funcs,
38573dc1
VS
13707 intel_primary_formats, num_formats,
13708 DRM_PLANE_TYPE_PRIMARY,
13709 "primary %c", pipe_name(pipe));
13710 else
580503c7
VS
13711 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13712 0, &intel_plane_funcs,
38573dc1
VS
13713 intel_primary_formats, num_formats,
13714 DRM_PLANE_TYPE_PRIMARY,
13715 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13716 if (ret)
13717 goto fail;
48404c1e 13718
5481e27f 13719 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
13720 supported_rotations =
13721 DRM_ROTATE_0 | DRM_ROTATE_90 |
13722 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
13723 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13724 supported_rotations =
13725 DRM_ROTATE_0 | DRM_ROTATE_180 |
13726 DRM_REFLECT_X;
5481e27f 13727 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
13728 supported_rotations =
13729 DRM_ROTATE_0 | DRM_ROTATE_180;
13730 } else {
13731 supported_rotations = DRM_ROTATE_0;
13732 }
13733
5481e27f 13734 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13735 drm_plane_create_rotation_property(&primary->base,
13736 DRM_ROTATE_0,
13737 supported_rotations);
48404c1e 13738
ea2c67bb
MR
13739 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13740
b079bd17 13741 return primary;
fca0ce2a
VS
13742
13743fail:
13744 kfree(state);
13745 kfree(primary);
13746
b079bd17 13747 return ERR_PTR(ret);
465c120c
MR
13748}
13749
3d7d6510 13750static int
852e787c 13751intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13752 struct intel_crtc_state *crtc_state,
852e787c 13753 struct intel_plane_state *state)
3d7d6510 13754{
2b875c22 13755 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13756 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 13757 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
13758 unsigned stride;
13759 int ret;
3d7d6510 13760
f8856a44
VS
13761 ret = drm_plane_helper_check_state(&state->base,
13762 &state->clip,
13763 DRM_PLANE_HELPER_NO_SCALING,
13764 DRM_PLANE_HELPER_NO_SCALING,
13765 true, true);
757f9a3e
GP
13766 if (ret)
13767 return ret;
13768
757f9a3e
GP
13769 /* if we want to turn off the cursor ignore width and height */
13770 if (!obj)
da20eabd 13771 return 0;
757f9a3e 13772
757f9a3e 13773 /* Check for which cursor types we support */
50a0bc90
TU
13774 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
13775 state->base.crtc_h)) {
ea2c67bb
MR
13776 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13777 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13778 return -EINVAL;
13779 }
13780
ea2c67bb
MR
13781 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13782 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13783 DRM_DEBUG_KMS("buffer is too small\n");
13784 return -ENOMEM;
13785 }
13786
bae781b2 13787 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 13788 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13789 return -EINVAL;
32b7eeec
MR
13790 }
13791
b29ec92c
VS
13792 /*
13793 * There's something wrong with the cursor on CHV pipe C.
13794 * If it straddles the left edge of the screen then
13795 * moving it away from the edge or disabling it often
13796 * results in a pipe underrun, and often that can lead to
13797 * dead pipe (constant underrun reported, and it scans
13798 * out just a solid color). To recover from that, the
13799 * display power well must be turned off and on again.
13800 * Refuse the put the cursor into that compromised position.
13801 */
920a14b2 13802 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 13803 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
13804 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13805 return -EINVAL;
13806 }
13807
da20eabd 13808 return 0;
852e787c 13809}
3d7d6510 13810
a8ad0d8e
ML
13811static void
13812intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13813 struct drm_crtc *crtc)
a8ad0d8e 13814{
f2858021
ML
13815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13816
13817 intel_crtc->cursor_addr = 0;
292889e1 13818 intel_crtc_update_cursor(crtc, NULL, NULL);
a8ad0d8e
ML
13819}
13820
f4a2cf29 13821static void
55a08b3f
ML
13822intel_update_cursor_plane(struct drm_plane *plane,
13823 const struct intel_crtc_state *crtc_state,
13824 const struct intel_plane_state *state)
852e787c 13825{
55a08b3f
ML
13826 struct drm_crtc *crtc = crtc_state->base.crtc;
13827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 13828 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 13829 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13830 uint32_t addr;
852e787c 13831
f4a2cf29 13832 if (!obj)
a912f12f 13833 addr = 0;
b7f05d4a 13834 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
be1e3415 13835 addr = intel_plane_ggtt_offset(state);
f4a2cf29 13836 else
a912f12f 13837 addr = obj->phys_handle->busaddr;
852e787c 13838
a912f12f 13839 intel_crtc->cursor_addr = addr;
292889e1 13840 intel_crtc_update_cursor(crtc, crtc_state, state);
852e787c
GP
13841}
13842
b079bd17 13843static struct intel_plane *
580503c7 13844intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 13845{
fca0ce2a
VS
13846 struct intel_plane *cursor = NULL;
13847 struct intel_plane_state *state = NULL;
13848 int ret;
3d7d6510
MR
13849
13850 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13851 if (!cursor) {
13852 ret = -ENOMEM;
fca0ce2a 13853 goto fail;
b079bd17 13854 }
3d7d6510 13855
8e7d688b 13856 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13857 if (!state) {
13858 ret = -ENOMEM;
fca0ce2a 13859 goto fail;
b079bd17
VS
13860 }
13861
8e7d688b 13862 cursor->base.state = &state->base;
ea2c67bb 13863
3d7d6510
MR
13864 cursor->can_scale = false;
13865 cursor->max_downscale = 1;
13866 cursor->pipe = pipe;
13867 cursor->plane = pipe;
b14e5848 13868 cursor->id = PLANE_CURSOR;
a9ff8714 13869 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 13870 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 13871 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 13872 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 13873
580503c7 13874 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13875 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13876 intel_cursor_formats,
13877 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
13878 DRM_PLANE_TYPE_CURSOR,
13879 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13880 if (ret)
13881 goto fail;
4398ad45 13882
5481e27f 13883 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
13884 drm_plane_create_rotation_property(&cursor->base,
13885 DRM_ROTATE_0,
13886 DRM_ROTATE_0 |
13887 DRM_ROTATE_180);
4398ad45 13888
580503c7 13889 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13890 state->scaler_id = -1;
13891
ea2c67bb
MR
13892 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13893
b079bd17 13894 return cursor;
fca0ce2a
VS
13895
13896fail:
13897 kfree(state);
13898 kfree(cursor);
13899
b079bd17 13900 return ERR_PTR(ret);
3d7d6510
MR
13901}
13902
1c74eeaf
NM
13903static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13904 struct intel_crtc_state *crtc_state)
549e2bfb 13905{
65edccce
VS
13906 struct intel_crtc_scaler_state *scaler_state =
13907 &crtc_state->scaler_state;
1c74eeaf 13908 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13909 int i;
549e2bfb 13910
1c74eeaf
NM
13911 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13912 if (!crtc->num_scalers)
13913 return;
13914
65edccce
VS
13915 for (i = 0; i < crtc->num_scalers; i++) {
13916 struct intel_scaler *scaler = &scaler_state->scalers[i];
13917
13918 scaler->in_use = 0;
13919 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13920 }
13921
13922 scaler_state->scaler_id = -1;
13923}
13924
5ab0d85b 13925static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13926{
13927 struct intel_crtc *intel_crtc;
f5de6e07 13928 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13929 struct intel_plane *primary = NULL;
13930 struct intel_plane *cursor = NULL;
a81d6fa0 13931 int sprite, ret;
79e53945 13932
955382f3 13933 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13934 if (!intel_crtc)
13935 return -ENOMEM;
79e53945 13936
f5de6e07 13937 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13938 if (!crtc_state) {
13939 ret = -ENOMEM;
f5de6e07 13940 goto fail;
b079bd17 13941 }
550acefd
ACO
13942 intel_crtc->config = crtc_state;
13943 intel_crtc->base.state = &crtc_state->base;
07878248 13944 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13945
580503c7 13946 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13947 if (IS_ERR(primary)) {
13948 ret = PTR_ERR(primary);
3d7d6510 13949 goto fail;
b079bd17 13950 }
d97d7b48 13951 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13952
a81d6fa0 13953 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13954 struct intel_plane *plane;
13955
580503c7 13956 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13957 if (IS_ERR(plane)) {
b079bd17
VS
13958 ret = PTR_ERR(plane);
13959 goto fail;
13960 }
d97d7b48 13961 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13962 }
13963
580503c7 13964 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13965 if (IS_ERR(cursor)) {
b079bd17 13966 ret = PTR_ERR(cursor);
3d7d6510 13967 goto fail;
b079bd17 13968 }
d97d7b48 13969 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13970
5ab0d85b 13971 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13972 &primary->base, &cursor->base,
13973 &intel_crtc_funcs,
4d5d72b7 13974 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13975 if (ret)
13976 goto fail;
79e53945 13977
80824003 13978 intel_crtc->pipe = pipe;
e3c566df 13979 intel_crtc->plane = primary->plane;
80824003 13980
4b0e333e
CW
13981 intel_crtc->cursor_base = ~0;
13982 intel_crtc->cursor_cntl = ~0;
dc41c154 13983 intel_crtc->cursor_size = ~0;
8d7849db 13984
1c74eeaf
NM
13985 /* initialize shared scalers */
13986 intel_crtc_init_scalers(intel_crtc, crtc_state);
13987
22fd0fab
JB
13988 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13989 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13990 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13991 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13992
79e53945 13993 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13994
8563b1e8
LL
13995 intel_color_init(&intel_crtc->base);
13996
87b6b101 13997 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13998
13999 return 0;
3d7d6510
MR
14000
14001fail:
b079bd17
VS
14002 /*
14003 * drm_mode_config_cleanup() will free up any
14004 * crtcs/planes already initialized.
14005 */
f5de6e07 14006 kfree(crtc_state);
3d7d6510 14007 kfree(intel_crtc);
b079bd17
VS
14008
14009 return ret;
79e53945
JB
14010}
14011
752aa88a
JB
14012enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14013{
6e9f798d 14014 struct drm_device *dev = connector->base.dev;
752aa88a 14015
51fd371b 14016 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14017
51ec53da 14018 if (!connector->base.state->crtc)
752aa88a
JB
14019 return INVALID_PIPE;
14020
51ec53da 14021 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
14022}
14023
08d7b3d1 14024int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14025 struct drm_file *file)
08d7b3d1 14026{
08d7b3d1 14027 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14028 struct drm_crtc *drmmode_crtc;
c05422d5 14029 struct intel_crtc *crtc;
08d7b3d1 14030
7707e653 14031 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 14032 if (!drmmode_crtc)
3f2c2057 14033 return -ENOENT;
08d7b3d1 14034
7707e653 14035 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14036 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14037
c05422d5 14038 return 0;
08d7b3d1
CW
14039}
14040
66a9278e 14041static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14042{
66a9278e
DV
14043 struct drm_device *dev = encoder->base.dev;
14044 struct intel_encoder *source_encoder;
79e53945 14045 int index_mask = 0;
79e53945
JB
14046 int entry = 0;
14047
b2784e15 14048 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14049 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14050 index_mask |= (1 << entry);
14051
79e53945
JB
14052 entry++;
14053 }
4ef69c7a 14054
79e53945
JB
14055 return index_mask;
14056}
14057
646d5772 14058static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 14059{
646d5772 14060 if (!IS_MOBILE(dev_priv))
4d302442
CW
14061 return false;
14062
14063 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14064 return false;
14065
5db94019 14066 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14067 return false;
14068
14069 return true;
14070}
14071
6315b5d3 14072static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 14073{
6315b5d3 14074 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
14075 return false;
14076
50a0bc90 14077 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
14078 return false;
14079
920a14b2 14080 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
14081 return false;
14082
4f8036a2
TU
14083 if (HAS_PCH_LPT_H(dev_priv) &&
14084 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
14085 return false;
14086
70ac54d0 14087 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 14088 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
14089 return false;
14090
e4abb733 14091 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14092 return false;
14093
14094 return true;
14095}
14096
8090ba8c
ID
14097void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14098{
14099 int pps_num;
14100 int pps_idx;
14101
14102 if (HAS_DDI(dev_priv))
14103 return;
14104 /*
14105 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14106 * everywhere where registers can be write protected.
14107 */
14108 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14109 pps_num = 2;
14110 else
14111 pps_num = 1;
14112
14113 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14114 u32 val = I915_READ(PP_CONTROL(pps_idx));
14115
14116 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14117 I915_WRITE(PP_CONTROL(pps_idx), val);
14118 }
14119}
14120
44cb734c
ID
14121static void intel_pps_init(struct drm_i915_private *dev_priv)
14122{
cc3f90f0 14123 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
14124 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14125 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14126 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14127 else
14128 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14129
14130 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14131}
14132
c39055b0 14133static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14134{
4ef69c7a 14135 struct intel_encoder *encoder;
cb0953d7 14136 bool dpd_is_edp = false;
79e53945 14137
44cb734c
ID
14138 intel_pps_init(dev_priv);
14139
97a824e1
ID
14140 /*
14141 * intel_edp_init_connector() depends on this completing first, to
14142 * prevent the registeration of both eDP and LVDS and the incorrect
14143 * sharing of the PPS.
14144 */
c39055b0 14145 intel_lvds_init(dev_priv);
79e53945 14146
6315b5d3 14147 if (intel_crt_present(dev_priv))
c39055b0 14148 intel_crt_init(dev_priv);
cb0953d7 14149
cc3f90f0 14150 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14151 /*
14152 * FIXME: Broxton doesn't support port detection via the
14153 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14154 * detect the ports.
14155 */
c39055b0
ACO
14156 intel_ddi_init(dev_priv, PORT_A);
14157 intel_ddi_init(dev_priv, PORT_B);
14158 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14159
c39055b0 14160 intel_dsi_init(dev_priv);
4f8036a2 14161 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14162 int found;
14163
de31facd
JB
14164 /*
14165 * Haswell uses DDI functions to detect digital outputs.
14166 * On SKL pre-D0 the strap isn't connected, so we assume
14167 * it's there.
14168 */
77179400 14169 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14170 /* WaIgnoreDDIAStrap: skl */
b976dc53 14171 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14172 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
14173
14174 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14175 * register */
14176 found = I915_READ(SFUSE_STRAP);
14177
14178 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14179 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14180 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14181 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14182 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14183 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
14184 /*
14185 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14186 */
b976dc53 14187 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14188 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14189 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14190 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14191 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14192
6e266956 14193 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14194 int found;
dd11bc10 14195 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 14196
646d5772 14197 if (has_edp_a(dev_priv))
c39055b0 14198 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14199
dc0fa718 14200 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14201 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14202 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14203 if (!found)
c39055b0 14204 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14205 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14206 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14207 }
14208
dc0fa718 14209 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14210 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14211
dc0fa718 14212 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14213 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14214
5eb08b69 14215 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14216 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14217
270b3042 14218 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14219 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14220 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14221 bool has_edp, has_port;
457c52d8 14222
e17ac6db
VS
14223 /*
14224 * The DP_DETECTED bit is the latched state of the DDC
14225 * SDA pin at boot. However since eDP doesn't require DDC
14226 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14227 * eDP ports may have been muxed to an alternate function.
14228 * Thus we can't rely on the DP_DETECTED bit alone to detect
14229 * eDP ports. Consult the VBT as well as DP_DETECTED to
14230 * detect eDP ports.
22f35042
VS
14231 *
14232 * Sadly the straps seem to be missing sometimes even for HDMI
14233 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14234 * and VBT for the presence of the port. Additionally we can't
14235 * trust the port type the VBT declares as we've seen at least
14236 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14237 */
dd11bc10 14238 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
14239 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14240 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14241 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14242 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14243 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14244
dd11bc10 14245 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
14246 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14247 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14248 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14249 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14250 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14251
920a14b2 14252 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14253 /*
14254 * eDP not supported on port D,
14255 * so no need to worry about it
14256 */
14257 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14258 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14259 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14260 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14261 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14262 }
14263
c39055b0 14264 intel_dsi_init(dev_priv);
5db94019 14265 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14266 bool found = false;
7d57382e 14267
e2debe91 14268 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14269 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14270 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14271 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14272 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14273 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14274 }
27185ae1 14275
9beb5fea 14276 if (!found && IS_G4X(dev_priv))
c39055b0 14277 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14278 }
13520b05
KH
14279
14280 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14281
e2debe91 14282 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14283 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14284 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14285 }
27185ae1 14286
e2debe91 14287 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14288
9beb5fea 14289 if (IS_G4X(dev_priv)) {
b01f2c3a 14290 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14291 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14292 }
9beb5fea 14293 if (IS_G4X(dev_priv))
c39055b0 14294 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14295 }
27185ae1 14296
9beb5fea 14297 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14298 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14299 } else if (IS_GEN2(dev_priv))
c39055b0 14300 intel_dvo_init(dev_priv);
79e53945 14301
56b857a5 14302 if (SUPPORTS_TV(dev_priv))
c39055b0 14303 intel_tv_init(dev_priv);
79e53945 14304
c39055b0 14305 intel_psr_init(dev_priv);
7c8f8a70 14306
c39055b0 14307 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14308 encoder->base.possible_crtcs = encoder->crtc_mask;
14309 encoder->base.possible_clones =
66a9278e 14310 intel_encoder_clones(encoder);
79e53945 14311 }
47356eb6 14312
c39055b0 14313 intel_init_pch_refclk(dev_priv);
270b3042 14314
c39055b0 14315 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14316}
14317
14318static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14319{
14320 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14321
ef2d633e 14322 drm_framebuffer_cleanup(fb);
70001cd2 14323
dd689287
CW
14324 i915_gem_object_lock(intel_fb->obj);
14325 WARN_ON(!intel_fb->obj->framebuffer_references--);
14326 i915_gem_object_unlock(intel_fb->obj);
14327
f8c417cd 14328 i915_gem_object_put(intel_fb->obj);
70001cd2 14329
79e53945
JB
14330 kfree(intel_fb);
14331}
14332
14333static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14334 struct drm_file *file,
79e53945
JB
14335 unsigned int *handle)
14336{
14337 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14338 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14339
cc917ab4
CW
14340 if (obj->userptr.mm) {
14341 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14342 return -EINVAL;
14343 }
14344
05394f39 14345 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14346}
14347
86c98588
RV
14348static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14349 struct drm_file *file,
14350 unsigned flags, unsigned color,
14351 struct drm_clip_rect *clips,
14352 unsigned num_clips)
14353{
5a97bcc6 14354 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14355
5a97bcc6 14356 i915_gem_object_flush_if_display(obj);
d59b21ec 14357 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14358
14359 return 0;
14360}
14361
79e53945
JB
14362static const struct drm_framebuffer_funcs intel_fb_funcs = {
14363 .destroy = intel_user_framebuffer_destroy,
14364 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14365 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14366};
14367
b321803d 14368static
920a14b2
TU
14369u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14370 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14371{
24dbf51a 14372 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
14373
14374 if (gen >= 9) {
ac484963
VS
14375 int cpp = drm_format_plane_cpp(pixel_format, 0);
14376
b321803d
DL
14377 /* "The stride in bytes must not exceed the of the size of 8K
14378 * pixels and 32K bytes."
14379 */
ac484963 14380 return min(8192 * cpp, 32768);
6401c37d 14381 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
14382 return 32*1024;
14383 } else if (gen >= 4) {
14384 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14385 return 16*1024;
14386 else
14387 return 32*1024;
14388 } else if (gen >= 3) {
14389 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14390 return 8*1024;
14391 else
14392 return 16*1024;
14393 } else {
14394 /* XXX DSPC is limited to 4k tiled */
14395 return 8*1024;
14396 }
14397}
14398
24dbf51a
CW
14399static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14400 struct drm_i915_gem_object *obj,
14401 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14402{
24dbf51a 14403 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
b3c11ac2 14404 struct drm_format_name_buf format_name;
dd689287
CW
14405 u32 pitch_limit, stride_alignment;
14406 unsigned int tiling, stride;
24dbf51a 14407 int ret = -EINVAL;
79e53945 14408
dd689287
CW
14409 i915_gem_object_lock(obj);
14410 obj->framebuffer_references++;
14411 tiling = i915_gem_object_get_tiling(obj);
14412 stride = i915_gem_object_get_stride(obj);
14413 i915_gem_object_unlock(obj);
dd4916c5 14414
2a80eada 14415 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14416 /*
14417 * If there's a fence, enforce that
14418 * the fb modifier and tiling mode match.
14419 */
14420 if (tiling != I915_TILING_NONE &&
14421 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14422 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 14423 goto err;
2a80eada
DV
14424 }
14425 } else {
c2ff7370 14426 if (tiling == I915_TILING_X) {
2a80eada 14427 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14428 } else if (tiling == I915_TILING_Y) {
144cc143 14429 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 14430 goto err;
2a80eada
DV
14431 }
14432 }
14433
9a8f0a12
TU
14434 /* Passed in modifier sanity checking. */
14435 switch (mode_cmd->modifier[0]) {
14436 case I915_FORMAT_MOD_Y_TILED:
14437 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14438 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14439 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14440 mode_cmd->modifier[0]);
24dbf51a 14441 goto err;
9a8f0a12
TU
14442 }
14443 case DRM_FORMAT_MOD_NONE:
14444 case I915_FORMAT_MOD_X_TILED:
14445 break;
14446 default:
144cc143
VS
14447 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14448 mode_cmd->modifier[0]);
24dbf51a 14449 goto err;
c16ed4be 14450 }
57cd6508 14451
c2ff7370
VS
14452 /*
14453 * gen2/3 display engine uses the fence if present,
14454 * so the tiling mode must match the fb modifier exactly.
14455 */
14456 if (INTEL_INFO(dev_priv)->gen < 4 &&
14457 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14458 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14459 goto err;
c2ff7370
VS
14460 }
14461
920a14b2 14462 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14463 mode_cmd->pixel_format);
a35cdaa0 14464 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143
VS
14465 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
14466 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
14467 "tiled" : "linear",
14468 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14469 goto err;
c16ed4be 14470 }
5d7bd705 14471
c2ff7370
VS
14472 /*
14473 * If there's a fence, enforce that
14474 * the fb pitch and fence stride match.
14475 */
144cc143
VS
14476 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14477 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14478 mode_cmd->pitches[0], stride);
24dbf51a 14479 goto err;
c16ed4be 14480 }
5d7bd705 14481
57779d06 14482 /* Reject formats not supported by any plane early. */
308e5bcb 14483 switch (mode_cmd->pixel_format) {
57779d06 14484 case DRM_FORMAT_C8:
04b3924d
VS
14485 case DRM_FORMAT_RGB565:
14486 case DRM_FORMAT_XRGB8888:
14487 case DRM_FORMAT_ARGB8888:
57779d06
VS
14488 break;
14489 case DRM_FORMAT_XRGB1555:
6315b5d3 14490 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
14491 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14492 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14493 goto err;
c16ed4be 14494 }
57779d06 14495 break;
57779d06 14496 case DRM_FORMAT_ABGR8888:
920a14b2 14497 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14498 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14499 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14500 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14501 goto err;
6c0fd451
DL
14502 }
14503 break;
14504 case DRM_FORMAT_XBGR8888:
04b3924d 14505 case DRM_FORMAT_XRGB2101010:
57779d06 14506 case DRM_FORMAT_XBGR2101010:
6315b5d3 14507 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
14508 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14509 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14510 goto err;
c16ed4be 14511 }
b5626747 14512 break;
7531208b 14513 case DRM_FORMAT_ABGR2101010:
920a14b2 14514 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
14515 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14516 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14517 goto err;
7531208b
DL
14518 }
14519 break;
04b3924d
VS
14520 case DRM_FORMAT_YUYV:
14521 case DRM_FORMAT_UYVY:
14522 case DRM_FORMAT_YVYU:
14523 case DRM_FORMAT_VYUY:
6315b5d3 14524 if (INTEL_GEN(dev_priv) < 5) {
144cc143
VS
14525 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14526 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14527 goto err;
c16ed4be 14528 }
57cd6508
CW
14529 break;
14530 default:
144cc143
VS
14531 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14532 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14533 goto err;
57cd6508
CW
14534 }
14535
90f9a336
VS
14536 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14537 if (mode_cmd->offsets[0] != 0)
24dbf51a 14538 goto err;
90f9a336 14539
24dbf51a
CW
14540 drm_helper_mode_fill_fb_struct(&dev_priv->drm,
14541 &intel_fb->base, mode_cmd);
d88c4afd
VS
14542
14543 stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
14544 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
144cc143
VS
14545 DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
14546 mode_cmd->pitches[0], stride_alignment);
d88c4afd
VS
14547 goto err;
14548 }
14549
c7d73f6a
DV
14550 intel_fb->obj = obj;
14551
6687c906
VS
14552 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
14553 if (ret)
9aceb5c1 14554 goto err;
2d7a215f 14555
24dbf51a
CW
14556 ret = drm_framebuffer_init(obj->base.dev,
14557 &intel_fb->base,
14558 &intel_fb_funcs);
79e53945
JB
14559 if (ret) {
14560 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14561 goto err;
79e53945
JB
14562 }
14563
79e53945 14564 return 0;
24dbf51a
CW
14565
14566err:
dd689287
CW
14567 i915_gem_object_lock(obj);
14568 obj->framebuffer_references--;
14569 i915_gem_object_unlock(obj);
24dbf51a 14570 return ret;
79e53945
JB
14571}
14572
79e53945
JB
14573static struct drm_framebuffer *
14574intel_user_framebuffer_create(struct drm_device *dev,
14575 struct drm_file *filp,
1eb83451 14576 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14577{
dcb1394e 14578 struct drm_framebuffer *fb;
05394f39 14579 struct drm_i915_gem_object *obj;
76dc3769 14580 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14581
03ac0642
CW
14582 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14583 if (!obj)
cce13ff7 14584 return ERR_PTR(-ENOENT);
79e53945 14585
24dbf51a 14586 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14587 if (IS_ERR(fb))
f0cd5182 14588 i915_gem_object_put(obj);
dcb1394e
LW
14589
14590 return fb;
79e53945
JB
14591}
14592
778e23a9
CW
14593static void intel_atomic_state_free(struct drm_atomic_state *state)
14594{
14595 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14596
14597 drm_atomic_state_default_release(state);
14598
14599 i915_sw_fence_fini(&intel_state->commit_ready);
14600
14601 kfree(state);
14602}
14603
79e53945 14604static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14605 .fb_create = intel_user_framebuffer_create,
0632fef6 14606 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14607 .atomic_check = intel_atomic_check,
14608 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14609 .atomic_state_alloc = intel_atomic_state_alloc,
14610 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14611 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14612};
14613
88212941
ID
14614/**
14615 * intel_init_display_hooks - initialize the display modesetting hooks
14616 * @dev_priv: device private
14617 */
14618void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14619{
7ff89ca2
VS
14620 intel_init_cdclk_hooks(dev_priv);
14621
88212941 14622 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14623 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14624 dev_priv->display.get_initial_plane_config =
14625 skylake_get_initial_plane_config;
bc8d7dff
DL
14626 dev_priv->display.crtc_compute_clock =
14627 haswell_crtc_compute_clock;
14628 dev_priv->display.crtc_enable = haswell_crtc_enable;
14629 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14630 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14631 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14632 dev_priv->display.get_initial_plane_config =
14633 ironlake_get_initial_plane_config;
797d0259
ACO
14634 dev_priv->display.crtc_compute_clock =
14635 haswell_crtc_compute_clock;
4f771f10
PZ
14636 dev_priv->display.crtc_enable = haswell_crtc_enable;
14637 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14638 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14639 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14640 dev_priv->display.get_initial_plane_config =
14641 ironlake_get_initial_plane_config;
3fb37703
ACO
14642 dev_priv->display.crtc_compute_clock =
14643 ironlake_crtc_compute_clock;
76e5a89c
DV
14644 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14645 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14646 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14647 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14648 dev_priv->display.get_initial_plane_config =
14649 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14650 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14651 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14652 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14653 } else if (IS_VALLEYVIEW(dev_priv)) {
14654 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14655 dev_priv->display.get_initial_plane_config =
14656 i9xx_get_initial_plane_config;
14657 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14658 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14659 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14660 } else if (IS_G4X(dev_priv)) {
14661 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14662 dev_priv->display.get_initial_plane_config =
14663 i9xx_get_initial_plane_config;
14664 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14665 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14666 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14667 } else if (IS_PINEVIEW(dev_priv)) {
14668 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14669 dev_priv->display.get_initial_plane_config =
14670 i9xx_get_initial_plane_config;
14671 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14672 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14673 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14674 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14675 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14676 dev_priv->display.get_initial_plane_config =
14677 i9xx_get_initial_plane_config;
d6dfee7a 14678 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14679 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14680 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14681 } else {
14682 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14683 dev_priv->display.get_initial_plane_config =
14684 i9xx_get_initial_plane_config;
14685 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14686 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14687 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14688 }
e70236a8 14689
88212941 14690 if (IS_GEN5(dev_priv)) {
3bb11b53 14691 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14692 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14693 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14694 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14695 /* FIXME: detect B0+ stepping and use auto training */
14696 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14697 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14698 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14699 }
14700
27082493
L
14701 if (dev_priv->info.gen >= 9)
14702 dev_priv->display.update_crtcs = skl_update_crtcs;
14703 else
14704 dev_priv->display.update_crtcs = intel_update_crtcs;
14705
5a21b665
DV
14706 switch (INTEL_INFO(dev_priv)->gen) {
14707 case 2:
14708 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14709 break;
14710
14711 case 3:
14712 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14713 break;
14714
14715 case 4:
14716 case 5:
14717 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14718 break;
14719
14720 case 6:
14721 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14722 break;
14723 case 7:
14724 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14725 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14726 break;
14727 case 9:
14728 /* Drop through - unsupported since execlist only. */
14729 default:
14730 /* Default just returns -ENODEV to indicate unsupported */
14731 dev_priv->display.queue_flip = intel_default_queue_flip;
14732 }
e70236a8
JB
14733}
14734
b690e96c
JB
14735/*
14736 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14737 * resume, or other times. This quirk makes sure that's the case for
14738 * affected systems.
14739 */
0206e353 14740static void quirk_pipea_force(struct drm_device *dev)
b690e96c 14741{
fac5e23e 14742 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
14743
14744 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14745 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14746}
14747
b6b5d049
VS
14748static void quirk_pipeb_force(struct drm_device *dev)
14749{
fac5e23e 14750 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
14751
14752 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14753 DRM_INFO("applying pipe b force quirk\n");
14754}
14755
435793df
KP
14756/*
14757 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14758 */
14759static void quirk_ssc_force_disable(struct drm_device *dev)
14760{
fac5e23e 14761 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14762 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14763 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14764}
14765
4dca20ef 14766/*
5a15ab5b
CE
14767 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14768 * brightness value
4dca20ef
CE
14769 */
14770static void quirk_invert_brightness(struct drm_device *dev)
14771{
fac5e23e 14772 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14773 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14774 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14775}
14776
9c72cc6f
SD
14777/* Some VBT's incorrectly indicate no backlight is present */
14778static void quirk_backlight_present(struct drm_device *dev)
14779{
fac5e23e 14780 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14781 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14782 DRM_INFO("applying backlight present quirk\n");
14783}
14784
b690e96c
JB
14785struct intel_quirk {
14786 int device;
14787 int subsystem_vendor;
14788 int subsystem_device;
14789 void (*hook)(struct drm_device *dev);
14790};
14791
5f85f176
EE
14792/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14793struct intel_dmi_quirk {
14794 void (*hook)(struct drm_device *dev);
14795 const struct dmi_system_id (*dmi_id_list)[];
14796};
14797
14798static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14799{
14800 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14801 return 1;
14802}
14803
14804static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14805 {
14806 .dmi_id_list = &(const struct dmi_system_id[]) {
14807 {
14808 .callback = intel_dmi_reverse_brightness,
14809 .ident = "NCR Corporation",
14810 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14811 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14812 },
14813 },
14814 { } /* terminating entry */
14815 },
14816 .hook = quirk_invert_brightness,
14817 },
14818};
14819
c43b5634 14820static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14821 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14822 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14823
b690e96c
JB
14824 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14825 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14826
5f080c0f
VS
14827 /* 830 needs to leave pipe A & dpll A up */
14828 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14829
b6b5d049
VS
14830 /* 830 needs to leave pipe B & dpll B up */
14831 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14832
435793df
KP
14833 /* Lenovo U160 cannot use SSC on LVDS */
14834 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14835
14836 /* Sony Vaio Y cannot use SSC on LVDS */
14837 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14838
be505f64
AH
14839 /* Acer Aspire 5734Z must invert backlight brightness */
14840 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14841
14842 /* Acer/eMachines G725 */
14843 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14844
14845 /* Acer/eMachines e725 */
14846 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14847
14848 /* Acer/Packard Bell NCL20 */
14849 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14850
14851 /* Acer Aspire 4736Z */
14852 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14853
14854 /* Acer Aspire 5336 */
14855 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14856
14857 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14858 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14859
dfb3d47b
SD
14860 /* Acer C720 Chromebook (Core i3 4005U) */
14861 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14862
b2a9601c 14863 /* Apple Macbook 2,1 (Core 2 T7400) */
14864 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14865
1b9448b0
JN
14866 /* Apple Macbook 4,1 */
14867 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14868
d4967d8c
SD
14869 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14870 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14871
14872 /* HP Chromebook 14 (Celeron 2955U) */
14873 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14874
14875 /* Dell Chromebook 11 */
14876 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14877
14878 /* Dell Chromebook 11 (2015 version) */
14879 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14880};
14881
14882static void intel_init_quirks(struct drm_device *dev)
14883{
14884 struct pci_dev *d = dev->pdev;
14885 int i;
14886
14887 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14888 struct intel_quirk *q = &intel_quirks[i];
14889
14890 if (d->device == q->device &&
14891 (d->subsystem_vendor == q->subsystem_vendor ||
14892 q->subsystem_vendor == PCI_ANY_ID) &&
14893 (d->subsystem_device == q->subsystem_device ||
14894 q->subsystem_device == PCI_ANY_ID))
14895 q->hook(dev);
14896 }
5f85f176
EE
14897 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14898 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14899 intel_dmi_quirks[i].hook(dev);
14900 }
b690e96c
JB
14901}
14902
9cce37f4 14903/* Disable the VGA plane that we never use */
29b74b7f 14904static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14905{
52a05c30 14906 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14907 u8 sr1;
920a14b2 14908 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14909
2b37c616 14910 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14911 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14912 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14913 sr1 = inb(VGA_SR_DATA);
14914 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14915 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14916 udelay(300);
14917
01f5a626 14918 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14919 POSTING_READ(vga_reg);
14920}
14921
f817586c
DV
14922void intel_modeset_init_hw(struct drm_device *dev)
14923{
fac5e23e 14924 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14925
4c75b940 14926 intel_update_cdclk(dev_priv);
bb0f4aab 14927 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
1a617b77 14928
46f16e63 14929 intel_init_clock_gating(dev_priv);
f817586c
DV
14930}
14931
d93c0372
MR
14932/*
14933 * Calculate what we think the watermarks should be for the state we've read
14934 * out of the hardware and then immediately program those watermarks so that
14935 * we ensure the hardware settings match our internal state.
14936 *
14937 * We can calculate what we think WM's should be by creating a duplicate of the
14938 * current state (which was constructed during hardware readout) and running it
14939 * through the atomic check code to calculate new watermark values in the
14940 * state object.
14941 */
14942static void sanitize_watermarks(struct drm_device *dev)
14943{
14944 struct drm_i915_private *dev_priv = to_i915(dev);
14945 struct drm_atomic_state *state;
ccf010fb 14946 struct intel_atomic_state *intel_state;
d93c0372
MR
14947 struct drm_crtc *crtc;
14948 struct drm_crtc_state *cstate;
14949 struct drm_modeset_acquire_ctx ctx;
14950 int ret;
14951 int i;
14952
14953 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14954 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14955 return;
14956
14957 /*
14958 * We need to hold connection_mutex before calling duplicate_state so
14959 * that the connector loop is protected.
14960 */
14961 drm_modeset_acquire_init(&ctx, 0);
14962retry:
0cd1262d 14963 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14964 if (ret == -EDEADLK) {
14965 drm_modeset_backoff(&ctx);
14966 goto retry;
14967 } else if (WARN_ON(ret)) {
0cd1262d 14968 goto fail;
d93c0372
MR
14969 }
14970
14971 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14972 if (WARN_ON(IS_ERR(state)))
0cd1262d 14973 goto fail;
d93c0372 14974
ccf010fb
ML
14975 intel_state = to_intel_atomic_state(state);
14976
ed4a6a7c
MR
14977 /*
14978 * Hardware readout is the only time we don't want to calculate
14979 * intermediate watermarks (since we don't trust the current
14980 * watermarks).
14981 */
602ae835
VS
14982 if (!HAS_GMCH_DISPLAY(dev_priv))
14983 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14984
d93c0372
MR
14985 ret = intel_atomic_check(dev, state);
14986 if (ret) {
14987 /*
14988 * If we fail here, it means that the hardware appears to be
14989 * programmed in a way that shouldn't be possible, given our
14990 * understanding of watermark requirements. This might mean a
14991 * mistake in the hardware readout code or a mistake in the
14992 * watermark calculations for a given platform. Raise a WARN
14993 * so that this is noticeable.
14994 *
14995 * If this actually happens, we'll have to just leave the
14996 * BIOS-programmed watermarks untouched and hope for the best.
14997 */
14998 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14999 goto put_state;
d93c0372
MR
15000 }
15001
15002 /* Write calculated watermark values back */
aa5e9b47 15003 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
15004 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15005
ed4a6a7c 15006 cs->wm.need_postvbl_update = true;
ccf010fb 15007 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
15008 }
15009
b9a1b717 15010put_state:
0853695c 15011 drm_atomic_state_put(state);
0cd1262d 15012fail:
d93c0372
MR
15013 drm_modeset_drop_locks(&ctx);
15014 drm_modeset_acquire_fini(&ctx);
15015}
15016
b079bd17 15017int intel_modeset_init(struct drm_device *dev)
79e53945 15018{
72e96d64
JL
15019 struct drm_i915_private *dev_priv = to_i915(dev);
15020 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 15021 enum pipe pipe;
46f297fb 15022 struct intel_crtc *crtc;
79e53945
JB
15023
15024 drm_mode_config_init(dev);
15025
15026 dev->mode_config.min_width = 0;
15027 dev->mode_config.min_height = 0;
15028
019d96cb
DA
15029 dev->mode_config.preferred_depth = 24;
15030 dev->mode_config.prefer_shadow = 1;
15031
25bab385
TU
15032 dev->mode_config.allow_fb_modifiers = true;
15033
e6ecefaa 15034 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15035
eb955eee 15036 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 15037 intel_atomic_helper_free_state_worker);
eb955eee 15038
b690e96c
JB
15039 intel_init_quirks(dev);
15040
62d75df7 15041 intel_init_pm(dev_priv);
1fa61106 15042
b7f05d4a 15043 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 15044 return 0;
e3c74757 15045
69f92f67
LW
15046 /*
15047 * There may be no VBT; and if the BIOS enabled SSC we can
15048 * just keep using it to avoid unnecessary flicker. Whereas if the
15049 * BIOS isn't using it, don't assume it will work even if the VBT
15050 * indicates as much.
15051 */
6e266956 15052 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
15053 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15054 DREF_SSC1_ENABLE);
15055
15056 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15057 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15058 bios_lvds_use_ssc ? "en" : "dis",
15059 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15060 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15061 }
15062 }
15063
5db94019 15064 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
15065 dev->mode_config.max_width = 2048;
15066 dev->mode_config.max_height = 2048;
5db94019 15067 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
15068 dev->mode_config.max_width = 4096;
15069 dev->mode_config.max_height = 4096;
79e53945 15070 } else {
a6c45cf0
CW
15071 dev->mode_config.max_width = 8192;
15072 dev->mode_config.max_height = 8192;
79e53945 15073 }
068be561 15074
2a307c2e
JN
15075 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15076 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 15077 dev->mode_config.cursor_height = 1023;
5db94019 15078 } else if (IS_GEN2(dev_priv)) {
068be561
DL
15079 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15080 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15081 } else {
15082 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15083 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15084 }
15085
72e96d64 15086 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 15087
28c97730 15088 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
15089 INTEL_INFO(dev_priv)->num_pipes,
15090 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 15091
055e393f 15092 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
15093 int ret;
15094
5ab0d85b 15095 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
15096 if (ret) {
15097 drm_mode_config_cleanup(dev);
15098 return ret;
15099 }
79e53945
JB
15100 }
15101
e72f9fbf 15102 intel_shared_dpll_init(dev);
ee7b9f93 15103
5be6e334
VS
15104 intel_update_czclk(dev_priv);
15105 intel_modeset_init_hw(dev);
15106
b2045352 15107 if (dev_priv->max_cdclk_freq == 0)
4c75b940 15108 intel_update_max_cdclk(dev_priv);
b2045352 15109
9cce37f4 15110 /* Just disable it once at startup */
29b74b7f 15111 i915_disable_vga(dev_priv);
c39055b0 15112 intel_setup_outputs(dev_priv);
11be49eb 15113
6e9f798d 15114 drm_modeset_lock_all(dev);
043e9bda 15115 intel_modeset_setup_hw_state(dev);
6e9f798d 15116 drm_modeset_unlock_all(dev);
46f297fb 15117
d3fcc808 15118 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15119 struct intel_initial_plane_config plane_config = {};
15120
46f297fb
JB
15121 if (!crtc->active)
15122 continue;
15123
46f297fb 15124 /*
46f297fb
JB
15125 * Note that reserving the BIOS fb up front prevents us
15126 * from stuffing other stolen allocations like the ring
15127 * on top. This prevents some ugliness at boot time, and
15128 * can even allow for smooth boot transitions if the BIOS
15129 * fb is large enough for the active pipe configuration.
15130 */
eeebeac5
ML
15131 dev_priv->display.get_initial_plane_config(crtc,
15132 &plane_config);
15133
15134 /*
15135 * If the fb is shared between multiple heads, we'll
15136 * just get the first one.
15137 */
15138 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15139 }
d93c0372
MR
15140
15141 /*
15142 * Make sure hardware watermarks really match the state we read out.
15143 * Note that we need to do this after reconstructing the BIOS fb's
15144 * since the watermark calculation done here will use pstate->fb.
15145 */
602ae835
VS
15146 if (!HAS_GMCH_DISPLAY(dev_priv))
15147 sanitize_watermarks(dev);
b079bd17
VS
15148
15149 return 0;
2c7111db
CW
15150}
15151
7fad798e
DV
15152static void intel_enable_pipe_a(struct drm_device *dev)
15153{
15154 struct intel_connector *connector;
f9e905ca 15155 struct drm_connector_list_iter conn_iter;
7fad798e
DV
15156 struct drm_connector *crt = NULL;
15157 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15158 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15159
15160 /* We can't just switch on the pipe A, we need to set things up with a
15161 * proper mode and output configuration. As a gross hack, enable pipe A
15162 * by enabling the load detect pipe once. */
f9e905ca
DV
15163 drm_connector_list_iter_begin(dev, &conn_iter);
15164 for_each_intel_connector_iter(connector, &conn_iter) {
7fad798e
DV
15165 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15166 crt = &connector->base;
15167 break;
15168 }
15169 }
f9e905ca 15170 drm_connector_list_iter_end(&conn_iter);
7fad798e
DV
15171
15172 if (!crt)
15173 return;
15174
208bf9fd 15175 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15176 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15177}
15178
fa555837
DV
15179static bool
15180intel_check_plane_mapping(struct intel_crtc *crtc)
15181{
b7f05d4a 15182 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 15183 u32 val;
fa555837 15184
b7f05d4a 15185 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
15186 return true;
15187
649636ef 15188 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15189
15190 if ((val & DISPLAY_PLANE_ENABLE) &&
15191 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15192 return false;
15193
15194 return true;
15195}
15196
02e93c35
VS
15197static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15198{
15199 struct drm_device *dev = crtc->base.dev;
15200 struct intel_encoder *encoder;
15201
15202 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15203 return true;
15204
15205 return false;
15206}
15207
496b0fc3
ML
15208static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15209{
15210 struct drm_device *dev = encoder->base.dev;
15211 struct intel_connector *connector;
15212
15213 for_each_connector_on_encoder(dev, &encoder->base, connector)
15214 return connector;
15215
15216 return NULL;
15217}
15218
a168f5b3
VS
15219static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
15220 enum transcoder pch_transcoder)
15221{
15222 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
15223 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
15224}
15225
24929352
DV
15226static void intel_sanitize_crtc(struct intel_crtc *crtc)
15227{
15228 struct drm_device *dev = crtc->base.dev;
fac5e23e 15229 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15230 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15231
24929352 15232 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
15233 if (!transcoder_is_dsi(cpu_transcoder)) {
15234 i915_reg_t reg = PIPECONF(cpu_transcoder);
15235
15236 I915_WRITE(reg,
15237 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15238 }
24929352 15239
d3eaf884 15240 /* restore vblank interrupts to correct state */
9625604c 15241 drm_crtc_vblank_reset(&crtc->base);
d297e103 15242 if (crtc->active) {
f9cd7b88
VS
15243 struct intel_plane *plane;
15244
9625604c 15245 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15246
15247 /* Disable everything but the primary plane */
15248 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15249 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15250 continue;
15251
72259536 15252 trace_intel_disable_plane(&plane->base, crtc);
f9cd7b88
VS
15253 plane->disable_plane(&plane->base, &crtc->base);
15254 }
9625604c 15255 }
d3eaf884 15256
24929352 15257 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15258 * disable the crtc (and hence change the state) if it is wrong. Note
15259 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 15260 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15261 bool plane;
15262
78108b7c
VS
15263 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15264 crtc->base.base.id, crtc->base.name);
24929352
DV
15265
15266 /* Pipe has the wrong plane attached and the plane is active.
15267 * Temporarily change the plane mapping and disable everything
15268 * ... */
15269 plane = crtc->plane;
1d4258db 15270 crtc->base.primary->state->visible = true;
24929352 15271 crtc->plane = !plane;
b17d48e2 15272 intel_crtc_disable_noatomic(&crtc->base);
24929352 15273 crtc->plane = plane;
24929352 15274 }
24929352 15275
7fad798e
DV
15276 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15277 crtc->pipe == PIPE_A && !crtc->active) {
15278 /* BIOS forgot to enable pipe A, this mostly happens after
15279 * resume. Force-enable the pipe to fix this, the update_dpms
15280 * call below we restore the pipe to the right state, but leave
15281 * the required bits on. */
15282 intel_enable_pipe_a(dev);
15283 }
15284
24929352
DV
15285 /* Adjust the state of the output pipe according to whether we
15286 * have active connectors/encoders. */
842e0307 15287 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15288 intel_crtc_disable_noatomic(&crtc->base);
24929352 15289
49cff963 15290 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
15291 /*
15292 * We start out with underrun reporting disabled to avoid races.
15293 * For correct bookkeeping mark this on active crtcs.
15294 *
c5ab3bc0
DV
15295 * Also on gmch platforms we dont have any hardware bits to
15296 * disable the underrun reporting. Which means we need to start
15297 * out with underrun reporting disabled also on inactive pipes,
15298 * since otherwise we'll complain about the garbage we read when
15299 * e.g. coming up after runtime pm.
15300 *
4cc31489
DV
15301 * No protection against concurrent access is required - at
15302 * worst a fifo underrun happens which also sets this to false.
15303 */
15304 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15305 /*
15306 * We track the PCH trancoder underrun reporting state
15307 * within the crtc. With crtc for pipe A housing the underrun
15308 * reporting state for PCH transcoder A, crtc for pipe B housing
15309 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15310 * and marking underrun reporting as disabled for the non-existing
15311 * PCH transcoders B and C would prevent enabling the south
15312 * error interrupt (see cpt_can_enable_serr_int()).
15313 */
15314 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
15315 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15316 }
24929352
DV
15317}
15318
15319static void intel_sanitize_encoder(struct intel_encoder *encoder)
15320{
15321 struct intel_connector *connector;
24929352
DV
15322
15323 /* We need to check both for a crtc link (meaning that the
15324 * encoder is active and trying to read from a pipe) and the
15325 * pipe itself being active. */
15326 bool has_active_crtc = encoder->base.crtc &&
15327 to_intel_crtc(encoder->base.crtc)->active;
15328
496b0fc3
ML
15329 connector = intel_encoder_find_connector(encoder);
15330 if (connector && !has_active_crtc) {
24929352
DV
15331 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15332 encoder->base.base.id,
8e329a03 15333 encoder->base.name);
24929352
DV
15334
15335 /* Connector is active, but has no active pipe. This is
15336 * fallout from our resume register restoring. Disable
15337 * the encoder manually again. */
15338 if (encoder->base.crtc) {
fd6bbda9
ML
15339 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15340
24929352
DV
15341 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15342 encoder->base.base.id,
8e329a03 15343 encoder->base.name);
fd6bbda9 15344 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15345 if (encoder->post_disable)
fd6bbda9 15346 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15347 }
7f1950fb 15348 encoder->base.crtc = NULL;
24929352
DV
15349
15350 /* Inconsistent output/port/pipe state happens presumably due to
15351 * a bug in one of the get_hw_state functions. Or someplace else
15352 * in our code, like the register restore mess on resume. Clamp
15353 * things to off as a safer default. */
fd6bbda9
ML
15354
15355 connector->base.dpms = DRM_MODE_DPMS_OFF;
15356 connector->base.encoder = NULL;
24929352
DV
15357 }
15358 /* Enabled encoders without active connectors will be fixed in
15359 * the crtc fixup. */
15360}
15361
29b74b7f 15362void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15363{
920a14b2 15364 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15365
04098753
ID
15366 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15367 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15368 i915_disable_vga(dev_priv);
04098753
ID
15369 }
15370}
15371
29b74b7f 15372void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15373{
8dc8a27c
PZ
15374 /* This function can be called both from intel_modeset_setup_hw_state or
15375 * at a very early point in our resume sequence, where the power well
15376 * structures are not yet restored. Since this function is at a very
15377 * paranoid "someone might have enabled VGA while we were not looking"
15378 * level, just check if the power well is enabled instead of trying to
15379 * follow the "don't touch the power well if we don't need it" policy
15380 * the rest of the driver uses. */
6392f847 15381 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15382 return;
15383
29b74b7f 15384 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15385
15386 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15387}
15388
f9cd7b88 15389static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15390{
f9cd7b88 15391 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15392
f9cd7b88 15393 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15394}
15395
f9cd7b88
VS
15396/* FIXME read out full plane state for all planes */
15397static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15398{
e9728bd8
VS
15399 struct intel_plane *primary = to_intel_plane(crtc->base.primary);
15400 bool visible;
d032ffa0 15401
e9728bd8 15402 visible = crtc->active && primary_get_hw_state(primary);
b26d3ea3 15403
e9728bd8
VS
15404 intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
15405 to_intel_plane_state(primary->base.state),
15406 visible);
98ec7739
VS
15407}
15408
30e984df 15409static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15410{
fac5e23e 15411 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15412 enum pipe pipe;
24929352
DV
15413 struct intel_crtc *crtc;
15414 struct intel_encoder *encoder;
15415 struct intel_connector *connector;
f9e905ca 15416 struct drm_connector_list_iter conn_iter;
5358901f 15417 int i;
24929352 15418
565602d7
ML
15419 dev_priv->active_crtcs = 0;
15420
d3fcc808 15421 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15422 struct intel_crtc_state *crtc_state =
15423 to_intel_crtc_state(crtc->base.state);
3b117c8f 15424
ec2dc6a0 15425 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15426 memset(crtc_state, 0, sizeof(*crtc_state));
15427 crtc_state->base.crtc = &crtc->base;
24929352 15428
565602d7
ML
15429 crtc_state->base.active = crtc_state->base.enable =
15430 dev_priv->display.get_pipe_config(crtc, crtc_state);
15431
15432 crtc->base.enabled = crtc_state->base.enable;
15433 crtc->active = crtc_state->base.active;
15434
aca1ebf4 15435 if (crtc_state->base.active)
565602d7
ML
15436 dev_priv->active_crtcs |= 1 << crtc->pipe;
15437
f9cd7b88 15438 readout_plane_state(crtc);
24929352 15439
78108b7c
VS
15440 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15441 crtc->base.base.id, crtc->base.name,
a8cd6da0 15442 enableddisabled(crtc_state->base.active));
24929352
DV
15443 }
15444
5358901f
DV
15445 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15446 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15447
2edd6443 15448 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
15449 &pll->state.hw_state);
15450 pll->state.crtc_mask = 0;
d3fcc808 15451 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15452 struct intel_crtc_state *crtc_state =
15453 to_intel_crtc_state(crtc->base.state);
15454
15455 if (crtc_state->base.active &&
15456 crtc_state->shared_dpll == pll)
2c42e535 15457 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15458 }
2c42e535 15459 pll->active_mask = pll->state.crtc_mask;
5358901f 15460
1e6f2ddc 15461 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 15462 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15463 }
15464
b2784e15 15465 for_each_intel_encoder(dev, encoder) {
24929352
DV
15466 pipe = 0;
15467
15468 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15469 struct intel_crtc_state *crtc_state;
15470
98187836 15471 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15472 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15473
045ac3b5 15474 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15475 crtc_state->output_types |= 1 << encoder->type;
15476 encoder->get_config(encoder, crtc_state);
24929352
DV
15477 } else {
15478 encoder->base.crtc = NULL;
15479 }
15480
6f2bcceb 15481 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15482 encoder->base.base.id, encoder->base.name,
15483 enableddisabled(encoder->base.crtc),
6f2bcceb 15484 pipe_name(pipe));
24929352
DV
15485 }
15486
f9e905ca
DV
15487 drm_connector_list_iter_begin(dev, &conn_iter);
15488 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
DV
15489 if (connector->get_hw_state(connector)) {
15490 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15491
15492 encoder = connector->encoder;
15493 connector->base.encoder = &encoder->base;
15494
15495 if (encoder->base.crtc &&
15496 encoder->base.crtc->state->active) {
15497 /*
15498 * This has to be done during hardware readout
15499 * because anything calling .crtc_disable may
15500 * rely on the connector_mask being accurate.
15501 */
15502 encoder->base.crtc->state->connector_mask |=
15503 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15504 encoder->base.crtc->state->encoder_mask |=
15505 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15506 }
15507
24929352
DV
15508 } else {
15509 connector->base.dpms = DRM_MODE_DPMS_OFF;
15510 connector->base.encoder = NULL;
15511 }
15512 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15513 connector->base.base.id, connector->base.name,
15514 enableddisabled(connector->base.encoder));
24929352 15515 }
f9e905ca 15516 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15517
15518 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15519 struct intel_crtc_state *crtc_state =
15520 to_intel_crtc_state(crtc->base.state);
aca1ebf4
VS
15521 int pixclk = 0;
15522
a8cd6da0 15523 crtc->base.hwmode = crtc_state->base.adjusted_mode;
7f4c6284
VS
15524
15525 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15526 if (crtc_state->base.active) {
15527 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15528 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15529 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15530
15531 /*
15532 * The initial mode needs to be set in order to keep
15533 * the atomic core happy. It wants a valid mode if the
15534 * crtc's enabled, so we do the above call.
15535 *
7800fb69
DV
15536 * But we don't set all the derived state fully, hence
15537 * set a flag to indicate that a full recalculation is
15538 * needed on the next commit.
7f4c6284 15539 */
a8cd6da0 15540 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15541
a7d1b3f4
VS
15542 intel_crtc_compute_pixel_rate(crtc_state);
15543
15544 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
15545 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15546 pixclk = crtc_state->pixel_rate;
aca1ebf4
VS
15547 else
15548 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15549
15550 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
a8cd6da0 15551 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
aca1ebf4
VS
15552 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15553
9eca6832
VS
15554 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15555 update_scanline_offset(crtc);
7f4c6284 15556 }
e3b247da 15557
aca1ebf4
VS
15558 dev_priv->min_pixclk[crtc->pipe] = pixclk;
15559
a8cd6da0 15560 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15561 }
30e984df
DV
15562}
15563
62b69566
ACO
15564static void
15565get_encoder_power_domains(struct drm_i915_private *dev_priv)
15566{
15567 struct intel_encoder *encoder;
15568
15569 for_each_intel_encoder(&dev_priv->drm, encoder) {
15570 u64 get_domains;
15571 enum intel_display_power_domain domain;
15572
15573 if (!encoder->get_power_domains)
15574 continue;
15575
15576 get_domains = encoder->get_power_domains(encoder);
15577 for_each_power_domain(domain, get_domains)
15578 intel_display_power_get(dev_priv, domain);
15579 }
15580}
15581
043e9bda
ML
15582/* Scan out the current hw modeset state,
15583 * and sanitizes it to the current state
15584 */
15585static void
15586intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 15587{
fac5e23e 15588 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15589 enum pipe pipe;
30e984df
DV
15590 struct intel_crtc *crtc;
15591 struct intel_encoder *encoder;
35c95375 15592 int i;
30e984df
DV
15593
15594 intel_modeset_readout_hw_state(dev);
24929352
DV
15595
15596 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15597 get_encoder_power_domains(dev_priv);
15598
b2784e15 15599 for_each_intel_encoder(dev, encoder) {
24929352
DV
15600 intel_sanitize_encoder(encoder);
15601 }
15602
055e393f 15603 for_each_pipe(dev_priv, pipe) {
98187836 15604 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15605
24929352 15606 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15607 intel_dump_pipe_config(crtc, crtc->config,
15608 "[setup_hw_state]");
24929352 15609 }
9a935856 15610
d29b2f9d
ACO
15611 intel_modeset_update_connector_atomic_state(dev);
15612
35c95375
DV
15613 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15614 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15615
2dd66ebd 15616 if (!pll->on || pll->active_mask)
35c95375
DV
15617 continue;
15618
15619 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15620
2edd6443 15621 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15622 pll->on = false;
15623 }
15624
602ae835 15625 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 15626 vlv_wm_get_hw_state(dev);
602ae835
VS
15627 vlv_wm_sanitize(dev_priv);
15628 } else if (IS_GEN9(dev_priv)) {
3078999f 15629 skl_wm_get_hw_state(dev);
602ae835 15630 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 15631 ilk_wm_get_hw_state(dev);
602ae835 15632 }
292b990e
ML
15633
15634 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15635 u64 put_domains;
292b990e 15636
74bff5f9 15637 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15638 if (WARN_ON(put_domains))
15639 modeset_put_power_domains(dev_priv, put_domains);
15640 }
15641 intel_display_set_init_power(dev_priv, false);
010cf73d 15642
8d8c386c
ID
15643 intel_power_domains_verify_state(dev_priv);
15644
010cf73d 15645 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15646}
7d0bc1ea 15647
043e9bda
ML
15648void intel_display_resume(struct drm_device *dev)
15649{
e2c8b870
ML
15650 struct drm_i915_private *dev_priv = to_i915(dev);
15651 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15652 struct drm_modeset_acquire_ctx ctx;
043e9bda 15653 int ret;
f30da187 15654
e2c8b870 15655 dev_priv->modeset_restore_state = NULL;
73974893
ML
15656 if (state)
15657 state->acquire_ctx = &ctx;
043e9bda 15658
ea49c9ac
ML
15659 /*
15660 * This is a cludge because with real atomic modeset mode_config.mutex
15661 * won't be taken. Unfortunately some probed state like
15662 * audio_codec_enable is still protected by mode_config.mutex, so lock
15663 * it here for now.
15664 */
15665 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15666 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15667
73974893
ML
15668 while (1) {
15669 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15670 if (ret != -EDEADLK)
15671 break;
043e9bda 15672
e2c8b870 15673 drm_modeset_backoff(&ctx);
e2c8b870 15674 }
043e9bda 15675
73974893 15676 if (!ret)
581e49fe 15677 ret = __intel_display_resume(dev, state, &ctx);
73974893 15678
e2c8b870
ML
15679 drm_modeset_drop_locks(&ctx);
15680 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15681 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15682
0853695c 15683 if (ret)
e2c8b870 15684 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15685 if (state)
15686 drm_atomic_state_put(state);
2c7111db
CW
15687}
15688
15689void intel_modeset_gem_init(struct drm_device *dev)
15690{
dc97997a 15691 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15692
dc97997a 15693 intel_init_gt_powersave(dev_priv);
ae48434c 15694
1ee8da6d 15695 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15696}
15697
15698int intel_connector_register(struct drm_connector *connector)
15699{
15700 struct intel_connector *intel_connector = to_intel_connector(connector);
15701 int ret;
15702
15703 ret = intel_backlight_device_register(intel_connector);
15704 if (ret)
15705 goto err;
15706
15707 return 0;
0962c3c9 15708
1ebaa0b9
CW
15709err:
15710 return ret;
79e53945
JB
15711}
15712
c191eca1 15713void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15714{
e63d87c0 15715 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15716
e63d87c0 15717 intel_backlight_device_unregister(intel_connector);
4932e2c3 15718 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15719}
15720
79e53945
JB
15721void intel_modeset_cleanup(struct drm_device *dev)
15722{
fac5e23e 15723 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15724
eb955eee
CW
15725 flush_work(&dev_priv->atomic_helper.free_work);
15726 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15727
dc97997a 15728 intel_disable_gt_powersave(dev_priv);
2eb5252e 15729
fd0c0642
DV
15730 /*
15731 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15732 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15733 * experience fancy races otherwise.
15734 */
2aeb7d3a 15735 intel_irq_uninstall(dev_priv);
eb21b92b 15736
fd0c0642
DV
15737 /*
15738 * Due to the hpd irq storm handling the hotplug work can re-arm the
15739 * poll handlers. Hence disable polling after hpd handling is shut down.
15740 */
f87ea761 15741 drm_kms_helper_poll_fini(dev);
fd0c0642 15742
723bfd70
JB
15743 intel_unregister_dsm_handler();
15744
c937ab3e 15745 intel_fbc_global_disable(dev_priv);
69341a5e 15746
1630fe75
CW
15747 /* flush any delayed tasks or pending work */
15748 flush_scheduled_work();
15749
79e53945 15750 drm_mode_config_cleanup(dev);
4d7bb011 15751
1ee8da6d 15752 intel_cleanup_overlay(dev_priv);
ae48434c 15753
dc97997a 15754 intel_cleanup_gt_powersave(dev_priv);
f5949141 15755
40196446 15756 intel_teardown_gmbus(dev_priv);
79e53945
JB
15757}
15758
df0e9248
CW
15759void intel_connector_attach_encoder(struct intel_connector *connector,
15760 struct intel_encoder *encoder)
15761{
15762 connector->encoder = encoder;
15763 drm_mode_connector_attach_encoder(&connector->base,
15764 &encoder->base);
79e53945 15765}
28d52043
DA
15766
15767/*
15768 * set vga decode state - true == enable VGA decode
15769 */
6315b5d3 15770int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15771{
6315b5d3 15772 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15773 u16 gmch_ctrl;
15774
75fa041d
CW
15775 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15776 DRM_ERROR("failed to read control word\n");
15777 return -EIO;
15778 }
15779
c0cc8a55
CW
15780 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15781 return 0;
15782
28d52043
DA
15783 if (state)
15784 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15785 else
15786 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15787
15788 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15789 DRM_ERROR("failed to write control word\n");
15790 return -EIO;
15791 }
15792
28d52043
DA
15793 return 0;
15794}
c4a1d9e4 15795
98a2f411
CW
15796#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15797
c4a1d9e4 15798struct intel_display_error_state {
ff57f1b0
PZ
15799
15800 u32 power_well_driver;
15801
63b66e5b
CW
15802 int num_transcoders;
15803
c4a1d9e4
CW
15804 struct intel_cursor_error_state {
15805 u32 control;
15806 u32 position;
15807 u32 base;
15808 u32 size;
52331309 15809 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15810
15811 struct intel_pipe_error_state {
ddf9c536 15812 bool power_domain_on;
c4a1d9e4 15813 u32 source;
f301b1e1 15814 u32 stat;
52331309 15815 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15816
15817 struct intel_plane_error_state {
15818 u32 control;
15819 u32 stride;
15820 u32 size;
15821 u32 pos;
15822 u32 addr;
15823 u32 surface;
15824 u32 tile_offset;
52331309 15825 } plane[I915_MAX_PIPES];
63b66e5b
CW
15826
15827 struct intel_transcoder_error_state {
ddf9c536 15828 bool power_domain_on;
63b66e5b
CW
15829 enum transcoder cpu_transcoder;
15830
15831 u32 conf;
15832
15833 u32 htotal;
15834 u32 hblank;
15835 u32 hsync;
15836 u32 vtotal;
15837 u32 vblank;
15838 u32 vsync;
15839 } transcoder[4];
c4a1d9e4
CW
15840};
15841
15842struct intel_display_error_state *
c033666a 15843intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15844{
c4a1d9e4 15845 struct intel_display_error_state *error;
63b66e5b
CW
15846 int transcoders[] = {
15847 TRANSCODER_A,
15848 TRANSCODER_B,
15849 TRANSCODER_C,
15850 TRANSCODER_EDP,
15851 };
c4a1d9e4
CW
15852 int i;
15853
c033666a 15854 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15855 return NULL;
15856
9d1cb914 15857 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15858 if (error == NULL)
15859 return NULL;
15860
c033666a 15861 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
15862 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15863
055e393f 15864 for_each_pipe(dev_priv, i) {
ddf9c536 15865 error->pipe[i].power_domain_on =
f458ebbc
DV
15866 __intel_display_power_is_enabled(dev_priv,
15867 POWER_DOMAIN_PIPE(i));
ddf9c536 15868 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15869 continue;
15870
5efb3e28
VS
15871 error->cursor[i].control = I915_READ(CURCNTR(i));
15872 error->cursor[i].position = I915_READ(CURPOS(i));
15873 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15874
15875 error->plane[i].control = I915_READ(DSPCNTR(i));
15876 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15877 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15878 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15879 error->plane[i].pos = I915_READ(DSPPOS(i));
15880 }
c033666a 15881 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15882 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15883 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15884 error->plane[i].surface = I915_READ(DSPSURF(i));
15885 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15886 }
15887
c4a1d9e4 15888 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15889
c033666a 15890 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15891 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15892 }
15893
4d1de975 15894 /* Note: this does not include DSI transcoders. */
c033666a 15895 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15896 if (HAS_DDI(dev_priv))
63b66e5b
CW
15897 error->num_transcoders++; /* Account for eDP. */
15898
15899 for (i = 0; i < error->num_transcoders; i++) {
15900 enum transcoder cpu_transcoder = transcoders[i];
15901
ddf9c536 15902 error->transcoder[i].power_domain_on =
f458ebbc 15903 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15904 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15905 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15906 continue;
15907
63b66e5b
CW
15908 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15909
15910 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15911 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15912 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15913 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15914 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15915 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15916 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15917 }
15918
15919 return error;
15920}
15921
edc3d884
MK
15922#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15923
c4a1d9e4 15924void
edc3d884 15925intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15926 struct intel_display_error_state *error)
15927{
5a4c6f1b 15928 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15929 int i;
15930
63b66e5b
CW
15931 if (!error)
15932 return;
15933
b7f05d4a 15934 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15935 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15936 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15937 error->power_well_driver);
055e393f 15938 for_each_pipe(dev_priv, i) {
edc3d884 15939 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15940 err_printf(m, " Power: %s\n",
87ad3212 15941 onoff(error->pipe[i].power_domain_on));
edc3d884 15942 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15943 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15944
15945 err_printf(m, "Plane [%d]:\n", i);
15946 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15947 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15948 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15949 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15950 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15951 }
772c2a51 15952 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15953 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15954 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15955 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15956 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15957 }
15958
edc3d884
MK
15959 err_printf(m, "Cursor [%d]:\n", i);
15960 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15961 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15962 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15963 }
63b66e5b
CW
15964
15965 for (i = 0; i < error->num_transcoders; i++) {
da205630 15966 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15967 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15968 err_printf(m, " Power: %s\n",
87ad3212 15969 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15970 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15971 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15972 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15973 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15974 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15975 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15976 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15977 }
c4a1d9e4 15978}
98a2f411
CW
15979
15980#endif