]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Disable DC states around GMBUS on GLK
[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
465c120c 52/* Primary plane formats for gen <= 3 */
568db4f2 53static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
54 DRM_FORMAT_C8,
55 DRM_FORMAT_RGB565,
465c120c 56 DRM_FORMAT_XRGB1555,
67fe7dc5 57 DRM_FORMAT_XRGB8888,
465c120c
MR
58};
59
60/* Primary plane formats for gen >= 4 */
568db4f2 61static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
62 DRM_FORMAT_C8,
63 DRM_FORMAT_RGB565,
64 DRM_FORMAT_XRGB8888,
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_XRGB2101010,
67 DRM_FORMAT_XBGR2101010,
68};
69
714244e2
BW
70static const uint64_t i9xx_format_modifiers[] = {
71 I915_FORMAT_MOD_X_TILED,
72 DRM_FORMAT_MOD_LINEAR,
73 DRM_FORMAT_MOD_INVALID
74};
75
6c0fd451 76static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
77 DRM_FORMAT_C8,
78 DRM_FORMAT_RGB565,
79 DRM_FORMAT_XRGB8888,
465c120c 80 DRM_FORMAT_XBGR8888,
67fe7dc5 81 DRM_FORMAT_ARGB8888,
465c120c
MR
82 DRM_FORMAT_ABGR8888,
83 DRM_FORMAT_XRGB2101010,
465c120c 84 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
85 DRM_FORMAT_YUYV,
86 DRM_FORMAT_YVYU,
87 DRM_FORMAT_UYVY,
88 DRM_FORMAT_VYUY,
465c120c
MR
89};
90
714244e2
BW
91static const uint64_t skl_format_modifiers_noccs[] = {
92 I915_FORMAT_MOD_Yf_TILED,
93 I915_FORMAT_MOD_Y_TILED,
94 I915_FORMAT_MOD_X_TILED,
95 DRM_FORMAT_MOD_LINEAR,
96 DRM_FORMAT_MOD_INVALID
97};
98
99static const uint64_t skl_format_modifiers_ccs[] = {
100 I915_FORMAT_MOD_Yf_TILED_CCS,
101 I915_FORMAT_MOD_Y_TILED_CCS,
102 I915_FORMAT_MOD_Yf_TILED,
103 I915_FORMAT_MOD_Y_TILED,
104 I915_FORMAT_MOD_X_TILED,
105 DRM_FORMAT_MOD_LINEAR,
106 DRM_FORMAT_MOD_INVALID
107};
108
3d7d6510
MR
109/* Cursor formats */
110static const uint32_t intel_cursor_formats[] = {
111 DRM_FORMAT_ARGB8888,
112};
113
714244e2
BW
114static const uint64_t cursor_format_modifiers[] = {
115 DRM_FORMAT_MOD_LINEAR,
116 DRM_FORMAT_MOD_INVALID
117};
118
f1f644dc 119static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 120 struct intel_crtc_state *pipe_config);
18442d08 121static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 122 struct intel_crtc_state *pipe_config);
f1f644dc 123
24dbf51a
CW
124static int intel_framebuffer_init(struct intel_framebuffer *ifb,
125 struct drm_i915_gem_object *obj,
126 struct drm_mode_fb_cmd2 *mode_cmd);
5b18e57c
DV
127static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
128static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 129static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 130static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
131 struct intel_link_m_n *m_n,
132 struct intel_link_m_n *m2_n2);
29407aab 133static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 134static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 135static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 136static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 137 const struct intel_crtc_state *pipe_config);
d288f65f 138static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 139 const struct intel_crtc_state *pipe_config);
5a21b665
DV
140static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
141static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
1c74eeaf
NM
142static void intel_crtc_init_scalers(struct intel_crtc *crtc,
143 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
144static void skylake_pfit_enable(struct intel_crtc *crtc);
145static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
146static void ironlake_pfit_enable(struct intel_crtc *crtc);
aecd36b8
VS
147static void intel_modeset_setup_hw_state(struct drm_device *dev,
148 struct drm_modeset_acquire_ctx *ctx);
2622a081 149static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 150
d4906093 151struct intel_limit {
4c5def93
ACO
152 struct {
153 int min, max;
154 } dot, vco, n, m, m1, m2, p, p1;
155
156 struct {
157 int dot_limit;
158 int p2_slow, p2_fast;
159 } p2;
d4906093 160};
79e53945 161
bfa7df01 162/* returns HPLL frequency in kHz */
49cd97a3 163int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
164{
165 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
166
167 /* Obtain SKU information */
168 mutex_lock(&dev_priv->sb_lock);
169 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
170 CCK_FUSE_HPLL_FREQ_MASK;
171 mutex_unlock(&dev_priv->sb_lock);
172
173 return vco_freq[hpll_freq] * 1000;
174}
175
c30fec65
VS
176int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
177 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
178{
179 u32 val;
180 int divider;
181
bfa7df01
VS
182 mutex_lock(&dev_priv->sb_lock);
183 val = vlv_cck_read(dev_priv, reg);
184 mutex_unlock(&dev_priv->sb_lock);
185
186 divider = val & CCK_FREQUENCY_VALUES;
187
188 WARN((val & CCK_FREQUENCY_STATUS) !=
189 (divider << CCK_FREQUENCY_STATUS_SHIFT),
190 "%s change in progress\n", name);
191
c30fec65
VS
192 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
193}
194
7ff89ca2
VS
195int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
196 const char *name, u32 reg)
c30fec65
VS
197{
198 if (dev_priv->hpll_freq == 0)
49cd97a3 199 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
200
201 return vlv_get_cck_clock(dev_priv, name, reg,
202 dev_priv->hpll_freq);
bfa7df01
VS
203}
204
bfa7df01
VS
205static void intel_update_czclk(struct drm_i915_private *dev_priv)
206{
666a4537 207 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
208 return;
209
210 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
211 CCK_CZ_CLOCK_CONTROL);
212
213 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
214}
215
021357ac 216static inline u32 /* units of 100MHz */
21a727b3
VS
217intel_fdi_link_freq(struct drm_i915_private *dev_priv,
218 const struct intel_crtc_state *pipe_config)
021357ac 219{
21a727b3
VS
220 if (HAS_DDI(dev_priv))
221 return pipe_config->port_clock; /* SPLL */
222 else if (IS_GEN5(dev_priv))
223 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 224 else
21a727b3 225 return 270000;
021357ac
CW
226}
227
1b6f4958 228static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 229 .dot = { .min = 25000, .max = 350000 },
9c333719 230 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 231 .n = { .min = 2, .max = 16 },
0206e353
AJ
232 .m = { .min = 96, .max = 140 },
233 .m1 = { .min = 18, .max = 26 },
234 .m2 = { .min = 6, .max = 16 },
235 .p = { .min = 4, .max = 128 },
236 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
237 .p2 = { .dot_limit = 165000,
238 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
239};
240
1b6f4958 241static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 242 .dot = { .min = 25000, .max = 350000 },
9c333719 243 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 244 .n = { .min = 2, .max = 16 },
5d536e28
DV
245 .m = { .min = 96, .max = 140 },
246 .m1 = { .min = 18, .max = 26 },
247 .m2 = { .min = 6, .max = 16 },
248 .p = { .min = 4, .max = 128 },
249 .p1 = { .min = 2, .max = 33 },
250 .p2 = { .dot_limit = 165000,
251 .p2_slow = 4, .p2_fast = 4 },
252};
253
1b6f4958 254static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 255 .dot = { .min = 25000, .max = 350000 },
9c333719 256 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 257 .n = { .min = 2, .max = 16 },
0206e353
AJ
258 .m = { .min = 96, .max = 140 },
259 .m1 = { .min = 18, .max = 26 },
260 .m2 = { .min = 6, .max = 16 },
261 .p = { .min = 4, .max = 128 },
262 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
263 .p2 = { .dot_limit = 165000,
264 .p2_slow = 14, .p2_fast = 7 },
e4b36699 265};
273e27ca 266
1b6f4958 267static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
268 .dot = { .min = 20000, .max = 400000 },
269 .vco = { .min = 1400000, .max = 2800000 },
270 .n = { .min = 1, .max = 6 },
271 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
272 .m1 = { .min = 8, .max = 18 },
273 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
274 .p = { .min = 5, .max = 80 },
275 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
276 .p2 = { .dot_limit = 200000,
277 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
278};
279
1b6f4958 280static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
281 .dot = { .min = 20000, .max = 400000 },
282 .vco = { .min = 1400000, .max = 2800000 },
283 .n = { .min = 1, .max = 6 },
284 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
285 .m1 = { .min = 8, .max = 18 },
286 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
287 .p = { .min = 7, .max = 98 },
288 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
289 .p2 = { .dot_limit = 112000,
290 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
291};
292
273e27ca 293
1b6f4958 294static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
295 .dot = { .min = 25000, .max = 270000 },
296 .vco = { .min = 1750000, .max = 3500000},
297 .n = { .min = 1, .max = 4 },
298 .m = { .min = 104, .max = 138 },
299 .m1 = { .min = 17, .max = 23 },
300 .m2 = { .min = 5, .max = 11 },
301 .p = { .min = 10, .max = 30 },
302 .p1 = { .min = 1, .max = 3},
303 .p2 = { .dot_limit = 270000,
304 .p2_slow = 10,
305 .p2_fast = 10
044c7c41 306 },
e4b36699
KP
307};
308
1b6f4958 309static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
310 .dot = { .min = 22000, .max = 400000 },
311 .vco = { .min = 1750000, .max = 3500000},
312 .n = { .min = 1, .max = 4 },
313 .m = { .min = 104, .max = 138 },
314 .m1 = { .min = 16, .max = 23 },
315 .m2 = { .min = 5, .max = 11 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8},
318 .p2 = { .dot_limit = 165000,
319 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
320};
321
1b6f4958 322static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
323 .dot = { .min = 20000, .max = 115000 },
324 .vco = { .min = 1750000, .max = 3500000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 104, .max = 138 },
327 .m1 = { .min = 17, .max = 23 },
328 .m2 = { .min = 5, .max = 11 },
329 .p = { .min = 28, .max = 112 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 0,
332 .p2_slow = 14, .p2_fast = 14
044c7c41 333 },
e4b36699
KP
334};
335
1b6f4958 336static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
337 .dot = { .min = 80000, .max = 224000 },
338 .vco = { .min = 1750000, .max = 3500000 },
339 .n = { .min = 1, .max = 3 },
340 .m = { .min = 104, .max = 138 },
341 .m1 = { .min = 17, .max = 23 },
342 .m2 = { .min = 5, .max = 11 },
343 .p = { .min = 14, .max = 42 },
344 .p1 = { .min = 2, .max = 6 },
345 .p2 = { .dot_limit = 0,
346 .p2_slow = 7, .p2_fast = 7
044c7c41 347 },
e4b36699
KP
348};
349
1b6f4958 350static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
351 .dot = { .min = 20000, .max = 400000},
352 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 353 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
354 .n = { .min = 3, .max = 6 },
355 .m = { .min = 2, .max = 256 },
273e27ca 356 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
357 .m1 = { .min = 0, .max = 0 },
358 .m2 = { .min = 0, .max = 254 },
359 .p = { .min = 5, .max = 80 },
360 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
361 .p2 = { .dot_limit = 200000,
362 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
363};
364
1b6f4958 365static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
366 .dot = { .min = 20000, .max = 400000 },
367 .vco = { .min = 1700000, .max = 3500000 },
368 .n = { .min = 3, .max = 6 },
369 .m = { .min = 2, .max = 256 },
370 .m1 = { .min = 0, .max = 0 },
371 .m2 = { .min = 0, .max = 254 },
372 .p = { .min = 7, .max = 112 },
373 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
374 .p2 = { .dot_limit = 112000,
375 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
376};
377
273e27ca
EA
378/* Ironlake / Sandybridge
379 *
380 * We calculate clock using (register_value + 2) for N/M1/M2, so here
381 * the range value for them is (actual_value - 2).
382 */
1b6f4958 383static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
384 .dot = { .min = 25000, .max = 350000 },
385 .vco = { .min = 1760000, .max = 3510000 },
386 .n = { .min = 1, .max = 5 },
387 .m = { .min = 79, .max = 127 },
388 .m1 = { .min = 12, .max = 22 },
389 .m2 = { .min = 5, .max = 9 },
390 .p = { .min = 5, .max = 80 },
391 .p1 = { .min = 1, .max = 8 },
392 .p2 = { .dot_limit = 225000,
393 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
394};
395
1b6f4958 396static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
397 .dot = { .min = 25000, .max = 350000 },
398 .vco = { .min = 1760000, .max = 3510000 },
399 .n = { .min = 1, .max = 3 },
400 .m = { .min = 79, .max = 118 },
401 .m1 = { .min = 12, .max = 22 },
402 .m2 = { .min = 5, .max = 9 },
403 .p = { .min = 28, .max = 112 },
404 .p1 = { .min = 2, .max = 8 },
405 .p2 = { .dot_limit = 225000,
406 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
407};
408
1b6f4958 409static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
410 .dot = { .min = 25000, .max = 350000 },
411 .vco = { .min = 1760000, .max = 3510000 },
412 .n = { .min = 1, .max = 3 },
413 .m = { .min = 79, .max = 127 },
414 .m1 = { .min = 12, .max = 22 },
415 .m2 = { .min = 5, .max = 9 },
416 .p = { .min = 14, .max = 56 },
417 .p1 = { .min = 2, .max = 8 },
418 .p2 = { .dot_limit = 225000,
419 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
420};
421
273e27ca 422/* LVDS 100mhz refclk limits. */
1b6f4958 423static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
424 .dot = { .min = 25000, .max = 350000 },
425 .vco = { .min = 1760000, .max = 3510000 },
426 .n = { .min = 1, .max = 2 },
427 .m = { .min = 79, .max = 126 },
428 .m1 = { .min = 12, .max = 22 },
429 .m2 = { .min = 5, .max = 9 },
430 .p = { .min = 28, .max = 112 },
0206e353 431 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
432 .p2 = { .dot_limit = 225000,
433 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
434};
435
1b6f4958 436static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
437 .dot = { .min = 25000, .max = 350000 },
438 .vco = { .min = 1760000, .max = 3510000 },
439 .n = { .min = 1, .max = 3 },
440 .m = { .min = 79, .max = 126 },
441 .m1 = { .min = 12, .max = 22 },
442 .m2 = { .min = 5, .max = 9 },
443 .p = { .min = 14, .max = 42 },
0206e353 444 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
445 .p2 = { .dot_limit = 225000,
446 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
447};
448
1b6f4958 449static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
450 /*
451 * These are the data rate limits (measured in fast clocks)
452 * since those are the strictest limits we have. The fast
453 * clock and actual rate limits are more relaxed, so checking
454 * them would make no difference.
455 */
456 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 457 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 458 .n = { .min = 1, .max = 7 },
a0c4da24
JB
459 .m1 = { .min = 2, .max = 3 },
460 .m2 = { .min = 11, .max = 156 },
b99ab663 461 .p1 = { .min = 2, .max = 3 },
5fdc9c49 462 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
463};
464
1b6f4958 465static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
466 /*
467 * These are the data rate limits (measured in fast clocks)
468 * since those are the strictest limits we have. The fast
469 * clock and actual rate limits are more relaxed, so checking
470 * them would make no difference.
471 */
472 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 473 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
474 .n = { .min = 1, .max = 1 },
475 .m1 = { .min = 2, .max = 2 },
476 .m2 = { .min = 24 << 22, .max = 175 << 22 },
477 .p1 = { .min = 2, .max = 4 },
478 .p2 = { .p2_slow = 1, .p2_fast = 14 },
479};
480
1b6f4958 481static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
482 /* FIXME: find real dot limits */
483 .dot = { .min = 0, .max = INT_MAX },
e6292556 484 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
485 .n = { .min = 1, .max = 1 },
486 .m1 = { .min = 2, .max = 2 },
487 /* FIXME: find real m2 limits */
488 .m2 = { .min = 2 << 22, .max = 255 << 22 },
489 .p1 = { .min = 2, .max = 4 },
490 .p2 = { .p2_slow = 1, .p2_fast = 20 },
491};
492
cdba954e
ACO
493static bool
494needs_modeset(struct drm_crtc_state *state)
495{
fc596660 496 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
497}
498
dccbea3b
ID
499/*
500 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
501 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
502 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
503 * The helpers' return value is the rate of the clock that is fed to the
504 * display engine's pipe which can be the above fast dot clock rate or a
505 * divided-down version of it.
506 */
f2b115e6 507/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 508static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 509{
2177832f
SL
510 clock->m = clock->m2 + 2;
511 clock->p = clock->p1 * clock->p2;
ed5ca77e 512 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 513 return 0;
fb03ac01
VS
514 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
515 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
516
517 return clock->dot;
2177832f
SL
518}
519
7429e9d4
DV
520static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
521{
522 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
523}
524
9e2c8475 525static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 526{
7429e9d4 527 clock->m = i9xx_dpll_compute_m(clock);
79e53945 528 clock->p = clock->p1 * clock->p2;
ed5ca77e 529 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 530 return 0;
fb03ac01
VS
531 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
532 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
533
534 return clock->dot;
79e53945
JB
535}
536
9e2c8475 537static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
538{
539 clock->m = clock->m1 * clock->m2;
540 clock->p = clock->p1 * clock->p2;
541 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 542 return 0;
589eca67
ID
543 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
544 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
545
546 return clock->dot / 5;
589eca67
ID
547}
548
9e2c8475 549int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
550{
551 clock->m = clock->m1 * clock->m2;
552 clock->p = clock->p1 * clock->p2;
553 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 554 return 0;
ef9348c8
CML
555 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
556 clock->n << 22);
557 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
558
559 return clock->dot / 5;
ef9348c8
CML
560}
561
7c04d1d9 562#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
563/**
564 * Returns whether the given set of divisors are valid for a given refclk with
565 * the given connectors.
566 */
567
e2d214ae 568static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 569 const struct intel_limit *limit,
9e2c8475 570 const struct dpll *clock)
79e53945 571{
f01b7962
VS
572 if (clock->n < limit->n.min || limit->n.max < clock->n)
573 INTELPllInvalid("n out of range\n");
79e53945 574 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 575 INTELPllInvalid("p1 out of range\n");
79e53945 576 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 577 INTELPllInvalid("m2 out of range\n");
79e53945 578 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 579 INTELPllInvalid("m1 out of range\n");
f01b7962 580
e2d214ae 581 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 582 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
583 if (clock->m1 <= clock->m2)
584 INTELPllInvalid("m1 <= m2\n");
585
e2d214ae 586 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 587 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
588 if (clock->p < limit->p.min || limit->p.max < clock->p)
589 INTELPllInvalid("p out of range\n");
590 if (clock->m < limit->m.min || limit->m.max < clock->m)
591 INTELPllInvalid("m out of range\n");
592 }
593
79e53945 594 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 595 INTELPllInvalid("vco out of range\n");
79e53945
JB
596 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
597 * connector, etc., rather than just a single range.
598 */
599 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 600 INTELPllInvalid("dot out of range\n");
79e53945
JB
601
602 return true;
603}
604
3b1429d9 605static int
1b6f4958 606i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
607 const struct intel_crtc_state *crtc_state,
608 int target)
79e53945 609{
3b1429d9 610 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 611
2d84d2b3 612 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 613 /*
a210b028
DV
614 * For LVDS just rely on its current settings for dual-channel.
615 * We haven't figured out how to reliably set up different
616 * single/dual channel state, if we even can.
79e53945 617 */
1974cad0 618 if (intel_is_dual_link_lvds(dev))
3b1429d9 619 return limit->p2.p2_fast;
79e53945 620 else
3b1429d9 621 return limit->p2.p2_slow;
79e53945
JB
622 } else {
623 if (target < limit->p2.dot_limit)
3b1429d9 624 return limit->p2.p2_slow;
79e53945 625 else
3b1429d9 626 return limit->p2.p2_fast;
79e53945 627 }
3b1429d9
VS
628}
629
70e8aa21
ACO
630/*
631 * Returns a set of divisors for the desired target clock with the given
632 * refclk, or FALSE. The returned values represent the clock equation:
633 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
634 *
635 * Target and reference clocks are specified in kHz.
636 *
637 * If match_clock is provided, then best_clock P divider must match the P
638 * divider from @match_clock used for LVDS downclocking.
639 */
3b1429d9 640static bool
1b6f4958 641i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 642 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
643 int target, int refclk, struct dpll *match_clock,
644 struct dpll *best_clock)
3b1429d9
VS
645{
646 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 647 struct dpll clock;
3b1429d9 648 int err = target;
79e53945 649
0206e353 650 memset(best_clock, 0, sizeof(*best_clock));
79e53945 651
3b1429d9
VS
652 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
653
42158660
ZY
654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
655 clock.m1++) {
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 658 if (clock.m2 >= clock.m1)
42158660
ZY
659 break;
660 for (clock.n = limit->n.min;
661 clock.n <= limit->n.max; clock.n++) {
662 for (clock.p1 = limit->p1.min;
663 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
664 int this_err;
665
dccbea3b 666 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
667 if (!intel_PLL_is_valid(to_i915(dev),
668 limit,
ac58c3f0
DV
669 &clock))
670 continue;
671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
70e8aa21
ACO
688/*
689 * Returns a set of divisors for the desired target clock with the given
690 * refclk, or FALSE. The returned values represent the clock equation:
691 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
692 *
693 * Target and reference clocks are specified in kHz.
694 *
695 * If match_clock is provided, then best_clock P divider must match the P
696 * divider from @match_clock used for LVDS downclocking.
697 */
ac58c3f0 698static bool
1b6f4958 699pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 700 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
701 int target, int refclk, struct dpll *match_clock,
702 struct dpll *best_clock)
79e53945 703{
3b1429d9 704 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 705 struct dpll clock;
79e53945
JB
706 int err = target;
707
0206e353 708 memset(best_clock, 0, sizeof(*best_clock));
79e53945 709
3b1429d9
VS
710 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
711
42158660
ZY
712 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
713 clock.m1++) {
714 for (clock.m2 = limit->m2.min;
715 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
716 for (clock.n = limit->n.min;
717 clock.n <= limit->n.max; clock.n++) {
718 for (clock.p1 = limit->p1.min;
719 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
720 int this_err;
721
dccbea3b 722 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
723 if (!intel_PLL_is_valid(to_i915(dev),
724 limit,
1b894b59 725 &clock))
79e53945 726 continue;
cec2f356
SP
727 if (match_clock &&
728 clock.p != match_clock->p)
729 continue;
79e53945
JB
730
731 this_err = abs(clock.dot - target);
732 if (this_err < err) {
733 *best_clock = clock;
734 err = this_err;
735 }
736 }
737 }
738 }
739 }
740
741 return (err != target);
742}
743
997c030c
ACO
744/*
745 * Returns a set of divisors for the desired target clock with the given
746 * refclk, or FALSE. The returned values represent the clock equation:
747 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
748 *
749 * Target and reference clocks are specified in kHz.
750 *
751 * If match_clock is provided, then best_clock P divider must match the P
752 * divider from @match_clock used for LVDS downclocking.
997c030c 753 */
d4906093 754static bool
1b6f4958 755g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 756 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
757 int target, int refclk, struct dpll *match_clock,
758 struct dpll *best_clock)
d4906093 759{
3b1429d9 760 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 761 struct dpll clock;
d4906093 762 int max_n;
3b1429d9 763 bool found = false;
6ba770dc
AJ
764 /* approximately equals target * 0.00585 */
765 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
766
767 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
768
769 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
770
d4906093 771 max_n = limit->n.max;
f77f13e2 772 /* based on hardware requirement, prefer smaller n to precision */
d4906093 773 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 774 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
775 for (clock.m1 = limit->m1.max;
776 clock.m1 >= limit->m1.min; clock.m1--) {
777 for (clock.m2 = limit->m2.max;
778 clock.m2 >= limit->m2.min; clock.m2--) {
779 for (clock.p1 = limit->p1.max;
780 clock.p1 >= limit->p1.min; clock.p1--) {
781 int this_err;
782
dccbea3b 783 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
784 if (!intel_PLL_is_valid(to_i915(dev),
785 limit,
1b894b59 786 &clock))
d4906093 787 continue;
1b894b59
CW
788
789 this_err = abs(clock.dot - target);
d4906093
ML
790 if (this_err < err_most) {
791 *best_clock = clock;
792 err_most = this_err;
793 max_n = clock.n;
794 found = true;
795 }
796 }
797 }
798 }
799 }
2c07245f
ZW
800 return found;
801}
802
d5dd62bd
ID
803/*
804 * Check if the calculated PLL configuration is more optimal compared to the
805 * best configuration and error found so far. Return the calculated error.
806 */
807static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
808 const struct dpll *calculated_clock,
809 const struct dpll *best_clock,
d5dd62bd
ID
810 unsigned int best_error_ppm,
811 unsigned int *error_ppm)
812{
9ca3ba01
ID
813 /*
814 * For CHV ignore the error and consider only the P value.
815 * Prefer a bigger P value based on HW requirements.
816 */
920a14b2 817 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
818 *error_ppm = 0;
819
820 return calculated_clock->p > best_clock->p;
821 }
822
24be4e46
ID
823 if (WARN_ON_ONCE(!target_freq))
824 return false;
825
d5dd62bd
ID
826 *error_ppm = div_u64(1000000ULL *
827 abs(target_freq - calculated_clock->dot),
828 target_freq);
829 /*
830 * Prefer a better P value over a better (smaller) error if the error
831 * is small. Ensure this preference for future configurations too by
832 * setting the error to 0.
833 */
834 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
835 *error_ppm = 0;
836
837 return true;
838 }
839
840 return *error_ppm + 10 < best_error_ppm;
841}
842
65b3d6a9
ACO
843/*
844 * Returns a set of divisors for the desired target clock with the given
845 * refclk, or FALSE. The returned values represent the clock equation:
846 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
847 */
a0c4da24 848static bool
1b6f4958 849vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 850 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
851 int target, int refclk, struct dpll *match_clock,
852 struct dpll *best_clock)
a0c4da24 853{
a93e255f 854 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 855 struct drm_device *dev = crtc->base.dev;
9e2c8475 856 struct dpll clock;
69e4f900 857 unsigned int bestppm = 1000000;
27e639bf
VS
858 /* min update 19.2 MHz */
859 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 860 bool found = false;
a0c4da24 861
6b4bf1c4
VS
862 target *= 5; /* fast clock */
863
864 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
865
866 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 867 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 868 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 869 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 870 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 871 clock.p = clock.p1 * clock.p2;
a0c4da24 872 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 873 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 874 unsigned int ppm;
69e4f900 875
6b4bf1c4
VS
876 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
877 refclk * clock.m1);
878
dccbea3b 879 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 880
e2d214ae
TU
881 if (!intel_PLL_is_valid(to_i915(dev),
882 limit,
f01b7962 883 &clock))
43b0ac53
VS
884 continue;
885
d5dd62bd
ID
886 if (!vlv_PLL_is_optimal(dev, target,
887 &clock,
888 best_clock,
889 bestppm, &ppm))
890 continue;
6b4bf1c4 891
d5dd62bd
ID
892 *best_clock = clock;
893 bestppm = ppm;
894 found = true;
a0c4da24
JB
895 }
896 }
897 }
898 }
a0c4da24 899
49e497ef 900 return found;
a0c4da24 901}
a4fc5ed6 902
65b3d6a9
ACO
903/*
904 * Returns a set of divisors for the desired target clock with the given
905 * refclk, or FALSE. The returned values represent the clock equation:
906 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
907 */
ef9348c8 908static bool
1b6f4958 909chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 910 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
911 int target, int refclk, struct dpll *match_clock,
912 struct dpll *best_clock)
ef9348c8 913{
a93e255f 914 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 915 struct drm_device *dev = crtc->base.dev;
9ca3ba01 916 unsigned int best_error_ppm;
9e2c8475 917 struct dpll clock;
ef9348c8
CML
918 uint64_t m2;
919 int found = false;
920
921 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 922 best_error_ppm = 1000000;
ef9348c8
CML
923
924 /*
925 * Based on hardware doc, the n always set to 1, and m1 always
926 * set to 2. If requires to support 200Mhz refclk, we need to
927 * revisit this because n may not 1 anymore.
928 */
929 clock.n = 1, clock.m1 = 2;
930 target *= 5; /* fast clock */
931
932 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
933 for (clock.p2 = limit->p2.p2_fast;
934 clock.p2 >= limit->p2.p2_slow;
935 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 936 unsigned int error_ppm;
ef9348c8
CML
937
938 clock.p = clock.p1 * clock.p2;
939
940 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
941 clock.n) << 22, refclk * clock.m1);
942
943 if (m2 > INT_MAX/clock.m1)
944 continue;
945
946 clock.m2 = m2;
947
dccbea3b 948 chv_calc_dpll_params(refclk, &clock);
ef9348c8 949
e2d214ae 950 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
951 continue;
952
9ca3ba01
ID
953 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
954 best_error_ppm, &error_ppm))
955 continue;
956
957 *best_clock = clock;
958 best_error_ppm = error_ppm;
959 found = true;
ef9348c8
CML
960 }
961 }
962
963 return found;
964}
965
5ab7b0b7 966bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 967 struct dpll *best_clock)
5ab7b0b7 968{
65b3d6a9 969 int refclk = 100000;
1b6f4958 970 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 971
65b3d6a9 972 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
973 target_clock, refclk, NULL, best_clock);
974}
975
525b9311 976bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 977{
20ddf665
VS
978 /* Be paranoid as we can arrive here with only partial
979 * state retrieved from the hardware during setup.
980 *
241bfc38 981 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
982 * as Haswell has gained clock readout/fastboot support.
983 *
66e514c1 984 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 985 * properly reconstruct framebuffers.
c3d1f436
MR
986 *
987 * FIXME: The intel_crtc->active here should be switched to
988 * crtc->state->active once we have proper CRTC states wired up
989 * for atomic.
20ddf665 990 */
525b9311
VS
991 return crtc->active && crtc->base.primary->state->fb &&
992 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
993}
994
a5c961d1
PZ
995enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
996 enum pipe pipe)
997{
98187836 998 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 999
e2af48c6 1000 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1001}
1002
a87e55f8
VS
1003static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1004 enum pipe pipe)
fbf49ea2 1005{
f0f59a00 1006 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1007 u32 line1, line2;
1008 u32 line_mask;
1009
5db94019 1010 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1011 line_mask = DSL_LINEMASK_GEN2;
1012 else
1013 line_mask = DSL_LINEMASK_GEN3;
1014
1015 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1016 msleep(5);
fbf49ea2
VS
1017 line2 = I915_READ(reg) & line_mask;
1018
a87e55f8
VS
1019 return line1 != line2;
1020}
1021
1022static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1023{
1024 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1025 enum pipe pipe = crtc->pipe;
1026
1027 /* Wait for the display line to settle/start moving */
1028 if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1029 DRM_ERROR("pipe %c scanline %s wait timed out\n",
1030 pipe_name(pipe), onoff(state));
1031}
1032
1033static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1034{
1035 wait_for_pipe_scanline_moving(crtc, false);
1036}
1037
1038static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1039{
1040 wait_for_pipe_scanline_moving(crtc, true);
fbf49ea2
VS
1041}
1042
ab7ad7f6
KP
1043/*
1044 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1045 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1046 *
1047 * After disabling a pipe, we can't wait for vblank in the usual way,
1048 * spinning on the vblank interrupt status bit, since we won't actually
1049 * see an interrupt when the pipe is disabled.
1050 *
ab7ad7f6
KP
1051 * On Gen4 and above:
1052 * wait for the pipe register state bit to turn off
1053 *
1054 * Otherwise:
1055 * wait for the display line value to settle (it usually
1056 * ends up stopping at the start of the next frame).
58e10eb9 1057 *
9d0498a2 1058 */
575f7ab7 1059static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1060{
6315b5d3 1061 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1062 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
ab7ad7f6 1063
6315b5d3 1064 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1065 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1066
1067 /* Wait for the Pipe State to go off */
b8511f53
CW
1068 if (intel_wait_for_register(dev_priv,
1069 reg, I965_PIPECONF_ACTIVE, 0,
1070 100))
284637d9 1071 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1072 } else {
a87e55f8 1073 intel_wait_for_pipe_scanline_stopped(crtc);
ab7ad7f6 1074 }
79e53945
JB
1075}
1076
b24e7179 1077/* Only for pre-ILK configs */
55607e8a
DV
1078void assert_pll(struct drm_i915_private *dev_priv,
1079 enum pipe pipe, bool state)
b24e7179 1080{
b24e7179
JB
1081 u32 val;
1082 bool cur_state;
1083
649636ef 1084 val = I915_READ(DPLL(pipe));
b24e7179 1085 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1086 I915_STATE_WARN(cur_state != state,
b24e7179 1087 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1088 onoff(state), onoff(cur_state));
b24e7179 1089}
b24e7179 1090
23538ef1 1091/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1092void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1093{
1094 u32 val;
1095 bool cur_state;
1096
a580516d 1097 mutex_lock(&dev_priv->sb_lock);
23538ef1 1098 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1099 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1100
1101 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1102 I915_STATE_WARN(cur_state != state,
23538ef1 1103 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1104 onoff(state), onoff(cur_state));
23538ef1 1105}
23538ef1 1106
040484af
JB
1107static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
1109{
040484af 1110 bool cur_state;
ad80a810
PZ
1111 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1112 pipe);
040484af 1113
2d1fe073 1114 if (HAS_DDI(dev_priv)) {
affa9354 1115 /* DDI does not have a specific FDI_TX register */
649636ef 1116 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1117 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1118 } else {
649636ef 1119 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1120 cur_state = !!(val & FDI_TX_ENABLE);
1121 }
e2c719b7 1122 I915_STATE_WARN(cur_state != state,
040484af 1123 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1124 onoff(state), onoff(cur_state));
040484af
JB
1125}
1126#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1127#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1128
1129static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1130 enum pipe pipe, bool state)
1131{
040484af
JB
1132 u32 val;
1133 bool cur_state;
1134
649636ef 1135 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1136 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1137 I915_STATE_WARN(cur_state != state,
040484af 1138 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1139 onoff(state), onoff(cur_state));
040484af
JB
1140}
1141#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1142#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1143
1144static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1145 enum pipe pipe)
1146{
040484af
JB
1147 u32 val;
1148
1149 /* ILK FDI PLL is always enabled */
7e22dbbb 1150 if (IS_GEN5(dev_priv))
040484af
JB
1151 return;
1152
bf507ef7 1153 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1154 if (HAS_DDI(dev_priv))
bf507ef7
ED
1155 return;
1156
649636ef 1157 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1158 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1159}
1160
55607e8a
DV
1161void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1162 enum pipe pipe, bool state)
040484af 1163{
040484af 1164 u32 val;
55607e8a 1165 bool cur_state;
040484af 1166
649636ef 1167 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1168 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1169 I915_STATE_WARN(cur_state != state,
55607e8a 1170 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1171 onoff(state), onoff(cur_state));
040484af
JB
1172}
1173
4f8036a2 1174void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1175{
f0f59a00 1176 i915_reg_t pp_reg;
ea0760cf
JB
1177 u32 val;
1178 enum pipe panel_pipe = PIPE_A;
0de3b485 1179 bool locked = true;
ea0760cf 1180
4f8036a2 1181 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1182 return;
1183
4f8036a2 1184 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1185 u32 port_sel;
1186
44cb734c
ID
1187 pp_reg = PP_CONTROL(0);
1188 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1189
1190 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1191 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1192 panel_pipe = PIPE_B;
1193 /* XXX: else fix for eDP */
4f8036a2 1194 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1195 /* presumably write lock depends on pipe, not port select */
44cb734c 1196 pp_reg = PP_CONTROL(pipe);
bedd4dba 1197 panel_pipe = pipe;
ea0760cf 1198 } else {
44cb734c 1199 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1200 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1201 panel_pipe = PIPE_B;
ea0760cf
JB
1202 }
1203
1204 val = I915_READ(pp_reg);
1205 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1206 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1207 locked = false;
1208
e2c719b7 1209 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1210 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1211 pipe_name(pipe));
ea0760cf
JB
1212}
1213
93ce0ba6
JN
1214static void assert_cursor(struct drm_i915_private *dev_priv,
1215 enum pipe pipe, bool state)
1216{
93ce0ba6
JN
1217 bool cur_state;
1218
2a307c2e 1219 if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1220 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1221 else
5efb3e28 1222 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1223
e2c719b7 1224 I915_STATE_WARN(cur_state != state,
93ce0ba6 1225 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1226 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1227}
1228#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1229#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1230
b840d907
JB
1231void assert_pipe(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
b24e7179 1233{
63d7bbe9 1234 bool cur_state;
702e7a56
PZ
1235 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1236 pipe);
4feed0eb 1237 enum intel_display_power_domain power_domain;
b24e7179 1238
e56134bc
VS
1239 /* we keep both pipes enabled on 830 */
1240 if (IS_I830(dev_priv))
8e636784
DV
1241 state = true;
1242
4feed0eb
ID
1243 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1244 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1245 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1246 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1247
1248 intel_display_power_put(dev_priv, power_domain);
1249 } else {
1250 cur_state = false;
69310161
PZ
1251 }
1252
e2c719b7 1253 I915_STATE_WARN(cur_state != state,
63d7bbe9 1254 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1255 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1256}
1257
931872fc
CW
1258static void assert_plane(struct drm_i915_private *dev_priv,
1259 enum plane plane, bool state)
b24e7179 1260{
b24e7179 1261 u32 val;
931872fc 1262 bool cur_state;
b24e7179 1263
649636ef 1264 val = I915_READ(DSPCNTR(plane));
931872fc 1265 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1266 I915_STATE_WARN(cur_state != state,
931872fc 1267 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1268 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1269}
1270
931872fc
CW
1271#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1272#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1273
b24e7179
JB
1274static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1275 enum pipe pipe)
1276{
649636ef 1277 int i;
b24e7179 1278
653e1026 1279 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1280 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1281 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1282 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1283 "plane %c assertion failure, should be disabled but not\n",
1284 plane_name(pipe));
19ec1358 1285 return;
28c05794 1286 }
19ec1358 1287
b24e7179 1288 /* Need to check both planes against the pipe */
055e393f 1289 for_each_pipe(dev_priv, i) {
649636ef
VS
1290 u32 val = I915_READ(DSPCNTR(i));
1291 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1292 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1293 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1294 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1295 plane_name(i), pipe_name(pipe));
b24e7179
JB
1296 }
1297}
1298
19332d7a
JB
1299static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1300 enum pipe pipe)
1301{
649636ef 1302 int sprite;
19332d7a 1303
6315b5d3 1304 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1305 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1306 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1307 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1308 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1309 sprite, pipe_name(pipe));
1310 }
920a14b2 1311 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1312 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1313 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1314 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1315 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1316 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1317 }
6315b5d3 1318 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1319 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1320 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1321 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1322 plane_name(pipe), pipe_name(pipe));
ab33081a 1323 } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
649636ef 1324 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1325 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1326 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1327 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1328 }
1329}
1330
08c71e5e
VS
1331static void assert_vblank_disabled(struct drm_crtc *crtc)
1332{
e2c719b7 1333 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1334 drm_crtc_vblank_put(crtc);
1335}
1336
7abd4b35
ACO
1337void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1338 enum pipe pipe)
92f2584a 1339{
92f2584a
JB
1340 u32 val;
1341 bool enabled;
1342
649636ef 1343 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1344 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1345 I915_STATE_WARN(enabled,
9db4a9c7
JB
1346 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1347 pipe_name(pipe));
92f2584a
JB
1348}
1349
4e634389
KP
1350static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1351 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1352{
1353 if ((val & DP_PORT_EN) == 0)
1354 return false;
1355
2d1fe073 1356 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1357 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1358 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1359 return false;
2d1fe073 1360 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1361 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1362 return false;
f0575e92
KP
1363 } else {
1364 if ((val & DP_PIPE_MASK) != (pipe << 30))
1365 return false;
1366 }
1367 return true;
1368}
1369
1519b995
KP
1370static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1371 enum pipe pipe, u32 val)
1372{
dc0fa718 1373 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1374 return false;
1375
2d1fe073 1376 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1377 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1378 return false;
2d1fe073 1379 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1380 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1381 return false;
1519b995 1382 } else {
dc0fa718 1383 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1384 return false;
1385 }
1386 return true;
1387}
1388
1389static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1390 enum pipe pipe, u32 val)
1391{
1392 if ((val & LVDS_PORT_EN) == 0)
1393 return false;
1394
2d1fe073 1395 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1396 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1397 return false;
1398 } else {
1399 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1400 return false;
1401 }
1402 return true;
1403}
1404
1405static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1406 enum pipe pipe, u32 val)
1407{
1408 if ((val & ADPA_DAC_ENABLE) == 0)
1409 return false;
2d1fe073 1410 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1411 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1412 return false;
1413 } else {
1414 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1415 return false;
1416 }
1417 return true;
1418}
1419
291906f1 1420static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1421 enum pipe pipe, i915_reg_t reg,
1422 u32 port_sel)
291906f1 1423{
47a05eca 1424 u32 val = I915_READ(reg);
e2c719b7 1425 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1426 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1427 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1428
2d1fe073 1429 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1430 && (val & DP_PIPEB_SELECT),
de9a35ab 1431 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1432}
1433
1434static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1435 enum pipe pipe, i915_reg_t reg)
291906f1 1436{
47a05eca 1437 u32 val = I915_READ(reg);
e2c719b7 1438 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1439 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1440 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1441
2d1fe073 1442 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1443 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1444 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1445}
1446
1447static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1448 enum pipe pipe)
1449{
291906f1 1450 u32 val;
291906f1 1451
f0575e92
KP
1452 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1453 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1454 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1455
649636ef 1456 val = I915_READ(PCH_ADPA);
e2c719b7 1457 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1458 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1459 pipe_name(pipe));
291906f1 1460
649636ef 1461 val = I915_READ(PCH_LVDS);
e2c719b7 1462 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1463 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1464 pipe_name(pipe));
291906f1 1465
e2debe91
PZ
1466 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1467 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1468 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1469}
1470
cd2d34d9
VS
1471static void _vlv_enable_pll(struct intel_crtc *crtc,
1472 const struct intel_crtc_state *pipe_config)
1473{
1474 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1475 enum pipe pipe = crtc->pipe;
1476
1477 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1478 POSTING_READ(DPLL(pipe));
1479 udelay(150);
1480
2c30b43b
CW
1481 if (intel_wait_for_register(dev_priv,
1482 DPLL(pipe),
1483 DPLL_LOCK_VLV,
1484 DPLL_LOCK_VLV,
1485 1))
cd2d34d9
VS
1486 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1487}
1488
d288f65f 1489static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1490 const struct intel_crtc_state *pipe_config)
87442f73 1491{
cd2d34d9 1492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1493 enum pipe pipe = crtc->pipe;
87442f73 1494
8bd3f301 1495 assert_pipe_disabled(dev_priv, pipe);
87442f73 1496
87442f73 1497 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1498 assert_panel_unlocked(dev_priv, pipe);
87442f73 1499
cd2d34d9
VS
1500 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1501 _vlv_enable_pll(crtc, pipe_config);
426115cf 1502
8bd3f301
VS
1503 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1504 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1505}
1506
cd2d34d9
VS
1507
1508static void _chv_enable_pll(struct intel_crtc *crtc,
1509 const struct intel_crtc_state *pipe_config)
9d556c99 1510{
cd2d34d9 1511 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1512 enum pipe pipe = crtc->pipe;
9d556c99 1513 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1514 u32 tmp;
1515
a580516d 1516 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1517
1518 /* Enable back the 10bit clock to display controller */
1519 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1520 tmp |= DPIO_DCLKP_EN;
1521 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1522
54433e91
VS
1523 mutex_unlock(&dev_priv->sb_lock);
1524
9d556c99
CML
1525 /*
1526 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1527 */
1528 udelay(1);
1529
1530 /* Enable PLL */
d288f65f 1531 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1532
1533 /* Check PLL is locked */
6b18826a
CW
1534 if (intel_wait_for_register(dev_priv,
1535 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1536 1))
9d556c99 1537 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1538}
1539
1540static void chv_enable_pll(struct intel_crtc *crtc,
1541 const struct intel_crtc_state *pipe_config)
1542{
1543 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1544 enum pipe pipe = crtc->pipe;
1545
1546 assert_pipe_disabled(dev_priv, pipe);
1547
1548 /* PLL is protected by panel, make sure we can write it */
1549 assert_panel_unlocked(dev_priv, pipe);
1550
1551 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1552 _chv_enable_pll(crtc, pipe_config);
9d556c99 1553
c231775c
VS
1554 if (pipe != PIPE_A) {
1555 /*
1556 * WaPixelRepeatModeFixForC0:chv
1557 *
1558 * DPLLCMD is AWOL. Use chicken bits to propagate
1559 * the value from DPLLBMD to either pipe B or C.
1560 */
dfa311f0 1561 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
c231775c
VS
1562 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1563 I915_WRITE(CBR4_VLV, 0);
1564 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1565
1566 /*
1567 * DPLLB VGA mode also seems to cause problems.
1568 * We should always have it disabled.
1569 */
1570 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1571 } else {
1572 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1573 POSTING_READ(DPLL_MD(pipe));
1574 }
9d556c99
CML
1575}
1576
6315b5d3 1577static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1578{
1579 struct intel_crtc *crtc;
1580 int count = 0;
1581
6315b5d3 1582 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1583 count += crtc->base.state->active &&
2d84d2b3
VS
1584 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1585 }
1c4e0274
VS
1586
1587 return count;
1588}
1589
939994da
VS
1590static void i9xx_enable_pll(struct intel_crtc *crtc,
1591 const struct intel_crtc_state *crtc_state)
63d7bbe9 1592{
6315b5d3 1593 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1594 i915_reg_t reg = DPLL(crtc->pipe);
939994da 1595 u32 dpll = crtc_state->dpll_hw_state.dpll;
bb408dd2 1596 int i;
63d7bbe9 1597
66e3d5c0 1598 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1599
63d7bbe9 1600 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1601 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1602 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1603
1c4e0274 1604 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1605 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1606 /*
1607 * It appears to be important that we don't enable this
1608 * for the current pipe before otherwise configuring the
1609 * PLL. No idea how this should be handled if multiple
1610 * DVO outputs are enabled simultaneosly.
1611 */
1612 dpll |= DPLL_DVO_2X_MODE;
1613 I915_WRITE(DPLL(!crtc->pipe),
1614 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1615 }
66e3d5c0 1616
c2b63374
VS
1617 /*
1618 * Apparently we need to have VGA mode enabled prior to changing
1619 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1620 * dividers, even though the register value does change.
1621 */
1622 I915_WRITE(reg, 0);
1623
8e7a65aa
VS
1624 I915_WRITE(reg, dpll);
1625
66e3d5c0
DV
1626 /* Wait for the clocks to stabilize. */
1627 POSTING_READ(reg);
1628 udelay(150);
1629
6315b5d3 1630 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1631 I915_WRITE(DPLL_MD(crtc->pipe),
939994da 1632 crtc_state->dpll_hw_state.dpll_md);
66e3d5c0
DV
1633 } else {
1634 /* The pixel multiplier can only be updated once the
1635 * DPLL is enabled and the clocks are stable.
1636 *
1637 * So write it again.
1638 */
1639 I915_WRITE(reg, dpll);
1640 }
63d7bbe9
JB
1641
1642 /* We do this three times for luck */
bb408dd2
VS
1643 for (i = 0; i < 3; i++) {
1644 I915_WRITE(reg, dpll);
1645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
1647 }
63d7bbe9
JB
1648}
1649
1c4e0274 1650static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1651{
6315b5d3 1652 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1653 enum pipe pipe = crtc->pipe;
1654
1655 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1656 if (IS_I830(dev_priv) &&
2d84d2b3 1657 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1658 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1659 I915_WRITE(DPLL(PIPE_B),
1660 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1661 I915_WRITE(DPLL(PIPE_A),
1662 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1663 }
1664
b6b5d049 1665 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1666 if (IS_I830(dev_priv))
63d7bbe9
JB
1667 return;
1668
1669 /* Make sure the pipe isn't still relying on us */
1670 assert_pipe_disabled(dev_priv, pipe);
1671
b8afb911 1672 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1673 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1674}
1675
f6071166
JB
1676static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1677{
b8afb911 1678 u32 val;
f6071166
JB
1679
1680 /* Make sure the pipe isn't still relying on us */
1681 assert_pipe_disabled(dev_priv, pipe);
1682
03ed5cbf
VS
1683 val = DPLL_INTEGRATED_REF_CLK_VLV |
1684 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1685 if (pipe != PIPE_A)
1686 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1687
f6071166
JB
1688 I915_WRITE(DPLL(pipe), val);
1689 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1690}
1691
1692static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1693{
d752048d 1694 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1695 u32 val;
1696
a11b0703
VS
1697 /* Make sure the pipe isn't still relying on us */
1698 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1699
60bfe44f
VS
1700 val = DPLL_SSC_REF_CLK_CHV |
1701 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1702 if (pipe != PIPE_A)
1703 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1704
a11b0703
VS
1705 I915_WRITE(DPLL(pipe), val);
1706 POSTING_READ(DPLL(pipe));
d752048d 1707
a580516d 1708 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1709
1710 /* Disable 10bit clock to display controller */
1711 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1712 val &= ~DPIO_DCLKP_EN;
1713 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1714
a580516d 1715 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1716}
1717
e4607fcf 1718void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1719 struct intel_digital_port *dport,
1720 unsigned int expected_mask)
89b667f8
JB
1721{
1722 u32 port_mask;
f0f59a00 1723 i915_reg_t dpll_reg;
89b667f8 1724
e4607fcf
CML
1725 switch (dport->port) {
1726 case PORT_B:
89b667f8 1727 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1728 dpll_reg = DPLL(0);
e4607fcf
CML
1729 break;
1730 case PORT_C:
89b667f8 1731 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1732 dpll_reg = DPLL(0);
9b6de0a1 1733 expected_mask <<= 4;
00fc31b7
CML
1734 break;
1735 case PORT_D:
1736 port_mask = DPLL_PORTD_READY_MASK;
1737 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1738 break;
1739 default:
1740 BUG();
1741 }
89b667f8 1742
370004d3
CW
1743 if (intel_wait_for_register(dev_priv,
1744 dpll_reg, port_mask, expected_mask,
1745 1000))
9b6de0a1
VS
1746 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1747 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1748}
1749
b8a4f404
PZ
1750static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1751 enum pipe pipe)
040484af 1752{
98187836
VS
1753 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1754 pipe);
f0f59a00
VS
1755 i915_reg_t reg;
1756 uint32_t val, pipeconf_val;
040484af 1757
040484af 1758 /* Make sure PCH DPLL is enabled */
8106ddbd 1759 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1760
1761 /* FDI must be feeding us bits for PCH ports */
1762 assert_fdi_tx_enabled(dev_priv, pipe);
1763 assert_fdi_rx_enabled(dev_priv, pipe);
1764
6e266956 1765 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1766 /* Workaround: Set the timing override bit before enabling the
1767 * pch transcoder. */
1768 reg = TRANS_CHICKEN2(pipe);
1769 val = I915_READ(reg);
1770 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1771 I915_WRITE(reg, val);
59c859d6 1772 }
23670b32 1773
ab9412ba 1774 reg = PCH_TRANSCONF(pipe);
040484af 1775 val = I915_READ(reg);
5f7f726d 1776 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1777
2d1fe073 1778 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1779 /*
c5de7c6f
VS
1780 * Make the BPC in transcoder be consistent with
1781 * that in pipeconf reg. For HDMI we must use 8bpc
1782 * here for both 8bpc and 12bpc.
e9bcff5c 1783 */
dfd07d72 1784 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1785 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1786 val |= PIPECONF_8BPC;
1787 else
1788 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1789 }
5f7f726d
PZ
1790
1791 val &= ~TRANS_INTERLACE_MASK;
1792 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1793 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1794 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1795 val |= TRANS_LEGACY_INTERLACED_ILK;
1796 else
1797 val |= TRANS_INTERLACED;
5f7f726d
PZ
1798 else
1799 val |= TRANS_PROGRESSIVE;
1800
040484af 1801 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1802 if (intel_wait_for_register(dev_priv,
1803 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1804 100))
4bb6f1f3 1805 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1806}
1807
8fb033d7 1808static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1809 enum transcoder cpu_transcoder)
040484af 1810{
8fb033d7 1811 u32 val, pipeconf_val;
8fb033d7 1812
8fb033d7 1813 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1814 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
a2196033 1815 assert_fdi_rx_enabled(dev_priv, PIPE_A);
8fb033d7 1816
223a6fdf 1817 /* Workaround: set timing override bit. */
36c0d0cf 1818 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1819 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1820 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1821
25f3ef11 1822 val = TRANS_ENABLE;
937bb610 1823 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1824
9a76b1c6
PZ
1825 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1826 PIPECONF_INTERLACED_ILK)
a35f2679 1827 val |= TRANS_INTERLACED;
8fb033d7
PZ
1828 else
1829 val |= TRANS_PROGRESSIVE;
1830
ab9412ba 1831 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1832 if (intel_wait_for_register(dev_priv,
1833 LPT_TRANSCONF,
1834 TRANS_STATE_ENABLE,
1835 TRANS_STATE_ENABLE,
1836 100))
937bb610 1837 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1838}
1839
b8a4f404
PZ
1840static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1841 enum pipe pipe)
040484af 1842{
f0f59a00
VS
1843 i915_reg_t reg;
1844 uint32_t val;
040484af
JB
1845
1846 /* FDI relies on the transcoder */
1847 assert_fdi_tx_disabled(dev_priv, pipe);
1848 assert_fdi_rx_disabled(dev_priv, pipe);
1849
291906f1
JB
1850 /* Ports must be off as well */
1851 assert_pch_ports_disabled(dev_priv, pipe);
1852
ab9412ba 1853 reg = PCH_TRANSCONF(pipe);
040484af
JB
1854 val = I915_READ(reg);
1855 val &= ~TRANS_ENABLE;
1856 I915_WRITE(reg, val);
1857 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1858 if (intel_wait_for_register(dev_priv,
1859 reg, TRANS_STATE_ENABLE, 0,
1860 50))
4bb6f1f3 1861 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1862
6e266956 1863 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1864 /* Workaround: Clear the timing override chicken bit again. */
1865 reg = TRANS_CHICKEN2(pipe);
1866 val = I915_READ(reg);
1867 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1868 I915_WRITE(reg, val);
1869 }
040484af
JB
1870}
1871
b7076546 1872void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1873{
8fb033d7
PZ
1874 u32 val;
1875
ab9412ba 1876 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1877 val &= ~TRANS_ENABLE;
ab9412ba 1878 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1879 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1880 if (intel_wait_for_register(dev_priv,
1881 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1882 50))
8a52fd9f 1883 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1884
1885 /* Workaround: clear timing override bit. */
36c0d0cf 1886 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1887 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1888 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1889}
1890
a2196033 1891enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
65f2130c
VS
1892{
1893 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1894
1895 WARN_ON(!crtc->config->has_pch_encoder);
1896
1897 if (HAS_PCH_LPT(dev_priv))
a2196033 1898 return PIPE_A;
65f2130c 1899 else
a2196033 1900 return crtc->pipe;
65f2130c
VS
1901}
1902
b24e7179 1903/**
309cfea8 1904 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1905 * @crtc: crtc responsible for the pipe
b24e7179 1906 *
0372264a 1907 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1908 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1909 */
e1fdc473 1910static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1911{
0372264a 1912 struct drm_device *dev = crtc->base.dev;
fac5e23e 1913 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1914 enum pipe pipe = crtc->pipe;
1a70a728 1915 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1916 i915_reg_t reg;
b24e7179
JB
1917 u32 val;
1918
9e2ee2dd
VS
1919 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1920
58c6eaa2 1921 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1922 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1923 assert_sprites_disabled(dev_priv, pipe);
1924
b24e7179
JB
1925 /*
1926 * A pipe without a PLL won't actually be able to drive bits from
1927 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1928 * need the check.
1929 */
09fa8bb9 1930 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1931 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1932 assert_dsi_pll_enabled(dev_priv);
1933 else
1934 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1935 } else {
6e3c9717 1936 if (crtc->config->has_pch_encoder) {
040484af 1937 /* if driving the PCH, we need FDI enabled */
65f2130c 1938 assert_fdi_rx_pll_enabled(dev_priv,
a2196033 1939 intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1940 assert_fdi_tx_pll_enabled(dev_priv,
1941 (enum pipe) cpu_transcoder);
040484af
JB
1942 }
1943 /* FIXME: assert CPU port conditions for SNB+ */
1944 }
b24e7179 1945
702e7a56 1946 reg = PIPECONF(cpu_transcoder);
b24e7179 1947 val = I915_READ(reg);
7ad25d48 1948 if (val & PIPECONF_ENABLE) {
e56134bc
VS
1949 /* we keep both pipes enabled on 830 */
1950 WARN_ON(!IS_I830(dev_priv));
00d70b15 1951 return;
7ad25d48 1952 }
00d70b15
CW
1953
1954 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1955 POSTING_READ(reg);
b7792d8b
VS
1956
1957 /*
a87e55f8
VS
1958 * Until the pipe starts PIPEDSL reads will return a stale value,
1959 * which causes an apparent vblank timestamp jump when PIPEDSL
1960 * resets to its proper value. That also messes up the frame count
1961 * when it's derived from the timestamps. So let's wait for the
1962 * pipe to start properly before we call drm_crtc_vblank_on()
b7792d8b 1963 */
a87e55f8
VS
1964 if (dev->max_vblank_count == 0)
1965 intel_wait_for_pipe_scanline_moving(crtc);
b24e7179
JB
1966}
1967
1968/**
309cfea8 1969 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1970 * @crtc: crtc whose pipes is to be disabled
b24e7179 1971 *
575f7ab7
VS
1972 * Disable the pipe of @crtc, making sure that various hardware
1973 * specific requirements are met, if applicable, e.g. plane
1974 * disabled, panel fitter off, etc.
b24e7179
JB
1975 *
1976 * Will wait until the pipe has shut down before returning.
1977 */
575f7ab7 1978static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1979{
fac5e23e 1980 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1981 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1982 enum pipe pipe = crtc->pipe;
f0f59a00 1983 i915_reg_t reg;
b24e7179
JB
1984 u32 val;
1985
9e2ee2dd
VS
1986 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1987
b24e7179
JB
1988 /*
1989 * Make sure planes won't keep trying to pump pixels to us,
1990 * or we might hang the display.
1991 */
1992 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1993 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1994 assert_sprites_disabled(dev_priv, pipe);
b24e7179 1995
702e7a56 1996 reg = PIPECONF(cpu_transcoder);
b24e7179 1997 val = I915_READ(reg);
00d70b15
CW
1998 if ((val & PIPECONF_ENABLE) == 0)
1999 return;
2000
67adc644
VS
2001 /*
2002 * Double wide has implications for planes
2003 * so best keep it disabled when not needed.
2004 */
6e3c9717 2005 if (crtc->config->double_wide)
67adc644
VS
2006 val &= ~PIPECONF_DOUBLE_WIDE;
2007
2008 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 2009 if (!IS_I830(dev_priv))
67adc644
VS
2010 val &= ~PIPECONF_ENABLE;
2011
2012 I915_WRITE(reg, val);
2013 if ((val & PIPECONF_ENABLE) == 0)
2014 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2015}
2016
832be82f
VS
2017static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2018{
2019 return IS_GEN2(dev_priv) ? 2048 : 4096;
2020}
2021
d88c4afd
VS
2022static unsigned int
2023intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
7b49f948 2024{
d88c4afd
VS
2025 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2026 unsigned int cpp = fb->format->cpp[plane];
2027
2028 switch (fb->modifier) {
2f075565 2029 case DRM_FORMAT_MOD_LINEAR:
7b49f948
VS
2030 return cpp;
2031 case I915_FORMAT_MOD_X_TILED:
2032 if (IS_GEN2(dev_priv))
2033 return 128;
2034 else
2035 return 512;
2e2adb05
VS
2036 case I915_FORMAT_MOD_Y_TILED_CCS:
2037 if (plane == 1)
2038 return 128;
2039 /* fall through */
7b49f948
VS
2040 case I915_FORMAT_MOD_Y_TILED:
2041 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2042 return 128;
2043 else
2044 return 512;
2e2adb05
VS
2045 case I915_FORMAT_MOD_Yf_TILED_CCS:
2046 if (plane == 1)
2047 return 128;
2048 /* fall through */
7b49f948
VS
2049 case I915_FORMAT_MOD_Yf_TILED:
2050 switch (cpp) {
2051 case 1:
2052 return 64;
2053 case 2:
2054 case 4:
2055 return 128;
2056 case 8:
2057 case 16:
2058 return 256;
2059 default:
2060 MISSING_CASE(cpp);
2061 return cpp;
2062 }
2063 break;
2064 default:
d88c4afd 2065 MISSING_CASE(fb->modifier);
7b49f948
VS
2066 return cpp;
2067 }
2068}
2069
d88c4afd
VS
2070static unsigned int
2071intel_tile_height(const struct drm_framebuffer *fb, int plane)
a57ce0b2 2072{
2f075565 2073 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
832be82f
VS
2074 return 1;
2075 else
d88c4afd
VS
2076 return intel_tile_size(to_i915(fb->dev)) /
2077 intel_tile_width_bytes(fb, plane);
6761dd31
TU
2078}
2079
8d0deca8 2080/* Return the tile dimensions in pixel units */
d88c4afd 2081static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
8d0deca8 2082 unsigned int *tile_width,
d88c4afd 2083 unsigned int *tile_height)
8d0deca8 2084{
d88c4afd
VS
2085 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2086 unsigned int cpp = fb->format->cpp[plane];
8d0deca8
VS
2087
2088 *tile_width = tile_width_bytes / cpp;
d88c4afd 2089 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
2090}
2091
6761dd31 2092unsigned int
d88c4afd
VS
2093intel_fb_align_height(const struct drm_framebuffer *fb,
2094 int plane, unsigned int height)
6761dd31 2095{
d88c4afd 2096 unsigned int tile_height = intel_tile_height(fb, plane);
832be82f
VS
2097
2098 return ALIGN(height, tile_height);
a57ce0b2
JB
2099}
2100
1663b9d6
VS
2101unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2102{
2103 unsigned int size = 0;
2104 int i;
2105
2106 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2107 size += rot_info->plane[i].width * rot_info->plane[i].height;
2108
2109 return size;
2110}
2111
75c82a53 2112static void
3465c580
VS
2113intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2114 const struct drm_framebuffer *fb,
2115 unsigned int rotation)
f64b98cd 2116{
7b92c047 2117 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2118 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2119 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2120 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2121 }
2122}
50470bb0 2123
fabac484
VS
2124static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2125{
2126 if (IS_I830(dev_priv))
2127 return 16 * 1024;
2128 else if (IS_I85X(dev_priv))
2129 return 256;
d9e1551e
VS
2130 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2131 return 32;
fabac484
VS
2132 else
2133 return 4 * 1024;
2134}
2135
603525d7 2136static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2137{
2138 if (INTEL_INFO(dev_priv)->gen >= 9)
2139 return 256 * 1024;
c0f86832 2140 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2141 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2142 return 128 * 1024;
2143 else if (INTEL_INFO(dev_priv)->gen >= 4)
2144 return 4 * 1024;
2145 else
44c5905e 2146 return 0;
4e9a86b6
VS
2147}
2148
d88c4afd
VS
2149static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2150 int plane)
603525d7 2151{
d88c4afd
VS
2152 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2153
b90c1ee1 2154 /* AUX_DIST needs only 4K alignment */
2e2adb05 2155 if (plane == 1)
b90c1ee1
VS
2156 return 4096;
2157
d88c4afd 2158 switch (fb->modifier) {
2f075565 2159 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2160 return intel_linear_alignment(dev_priv);
2161 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2162 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2163 return 256 * 1024;
2164 return 0;
2e2adb05
VS
2165 case I915_FORMAT_MOD_Y_TILED_CCS:
2166 case I915_FORMAT_MOD_Yf_TILED_CCS:
603525d7
VS
2167 case I915_FORMAT_MOD_Y_TILED:
2168 case I915_FORMAT_MOD_Yf_TILED:
2169 return 1 * 1024 * 1024;
2170 default:
d88c4afd 2171 MISSING_CASE(fb->modifier);
603525d7
VS
2172 return 0;
2173 }
2174}
2175
058d88c4
CW
2176struct i915_vma *
2177intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2178{
850c4cdc 2179 struct drm_device *dev = fb->dev;
fac5e23e 2180 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2181 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2182 struct i915_ggtt_view view;
058d88c4 2183 struct i915_vma *vma;
6b95a207 2184 u32 alignment;
6b95a207 2185
ebcdd39e
MR
2186 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2187
d88c4afd 2188 alignment = intel_surf_alignment(fb, 0);
6b95a207 2189
3465c580 2190 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2191
693db184
CW
2192 /* Note that the w/a also requires 64 PTE of padding following the
2193 * bo. We currently fill all unused PTE with the shadow page and so
2194 * we should always have valid PTE following the scanout preventing
2195 * the VT-d warning.
2196 */
48f112fe 2197 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2198 alignment = 256 * 1024;
2199
d6dd6843
PZ
2200 /*
2201 * Global gtt pte registers are special registers which actually forward
2202 * writes to a chunk of system memory. Which means that there is no risk
2203 * that the register values disappear as soon as we call
2204 * intel_runtime_pm_put(), so it is correct to wrap only the
2205 * pin/unpin/fence and not more.
2206 */
2207 intel_runtime_pm_get(dev_priv);
2208
9db529aa
DV
2209 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2210
058d88c4 2211 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2212 if (IS_ERR(vma))
2213 goto err;
6b95a207 2214
05a20d09 2215 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2216 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2217 * fence, whereas 965+ only requires a fence if using
2218 * framebuffer compression. For simplicity, we always, when
2219 * possible, install a fence as the cost is not that onerous.
2220 *
2221 * If we fail to fence the tiled scanout, then either the
2222 * modeset will reject the change (which is highly unlikely as
2223 * the affected systems, all but one, do not have unmappable
2224 * space) or we will not be able to enable full powersaving
2225 * techniques (also likely not to apply due to various limits
2226 * FBC and the like impose on the size of the buffer, which
2227 * presumably we violated anyway with this unmappable buffer).
2228 * Anyway, it is presumably better to stumble onwards with
2229 * something and try to run the system in a "less than optimal"
2230 * mode that matches the user configuration.
2231 */
3bd40735 2232 i915_vma_pin_fence(vma);
9807216f 2233 }
6b95a207 2234
be1e3415 2235 i915_vma_get(vma);
49ef5294 2236err:
9db529aa
DV
2237 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2238
d6dd6843 2239 intel_runtime_pm_put(dev_priv);
058d88c4 2240 return vma;
6b95a207
KH
2241}
2242
be1e3415 2243void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2244{
be1e3415 2245 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2246
49ef5294 2247 i915_vma_unpin_fence(vma);
058d88c4 2248 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2249 i915_vma_put(vma);
1690e1eb
CW
2250}
2251
ef78ec94
VS
2252static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2253 unsigned int rotation)
2254{
bd2ef25d 2255 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2256 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2257 else
2258 return fb->pitches[plane];
2259}
2260
6687c906
VS
2261/*
2262 * Convert the x/y offsets into a linear offset.
2263 * Only valid with 0/180 degree rotation, which is fine since linear
2264 * offset is only used with linear buffers on pre-hsw and tiled buffers
2265 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2266 */
2267u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2268 const struct intel_plane_state *state,
2269 int plane)
6687c906 2270{
2949056c 2271 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2272 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2273 unsigned int pitch = fb->pitches[plane];
2274
2275 return y * pitch + x * cpp;
2276}
2277
2278/*
2279 * Add the x/y offsets derived from fb->offsets[] to the user
2280 * specified plane src x/y offsets. The resulting x/y offsets
2281 * specify the start of scanout from the beginning of the gtt mapping.
2282 */
2283void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2284 const struct intel_plane_state *state,
2285 int plane)
6687c906
VS
2286
2287{
2949056c
VS
2288 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2289 unsigned int rotation = state->base.rotation;
6687c906 2290
bd2ef25d 2291 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2292 *x += intel_fb->rotated[plane].x;
2293 *y += intel_fb->rotated[plane].y;
2294 } else {
2295 *x += intel_fb->normal[plane].x;
2296 *y += intel_fb->normal[plane].y;
2297 }
2298}
2299
e8837d98
VS
2300static u32 __intel_adjust_tile_offset(int *x, int *y,
2301 unsigned int tile_width,
2302 unsigned int tile_height,
2303 unsigned int tile_size,
2304 unsigned int pitch_tiles,
2305 u32 old_offset,
2306 u32 new_offset)
29cf9491 2307{
b9b24038 2308 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2309 unsigned int tiles;
2310
2311 WARN_ON(old_offset & (tile_size - 1));
2312 WARN_ON(new_offset & (tile_size - 1));
2313 WARN_ON(new_offset > old_offset);
2314
2315 tiles = (old_offset - new_offset) / tile_size;
2316
2317 *y += tiles / pitch_tiles * tile_height;
2318 *x += tiles % pitch_tiles * tile_width;
2319
b9b24038
VS
2320 /* minimize x in case it got needlessly big */
2321 *y += *x / pitch_pixels * tile_height;
2322 *x %= pitch_pixels;
2323
29cf9491
VS
2324 return new_offset;
2325}
2326
e8837d98
VS
2327static u32 _intel_adjust_tile_offset(int *x, int *y,
2328 const struct drm_framebuffer *fb, int plane,
2329 unsigned int rotation,
2330 u32 old_offset, u32 new_offset)
66a2d927 2331{
e8837d98 2332 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
353c8598 2333 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2334 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2335
2336 WARN_ON(new_offset > old_offset);
2337
2f075565 2338 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
66a2d927
VS
2339 unsigned int tile_size, tile_width, tile_height;
2340 unsigned int pitch_tiles;
2341
2342 tile_size = intel_tile_size(dev_priv);
d88c4afd 2343 intel_tile_dims(fb, plane, &tile_width, &tile_height);
66a2d927 2344
bd2ef25d 2345 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2346 pitch_tiles = pitch / tile_height;
2347 swap(tile_width, tile_height);
2348 } else {
2349 pitch_tiles = pitch / (tile_width * cpp);
2350 }
2351
e8837d98
VS
2352 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2353 tile_size, pitch_tiles,
2354 old_offset, new_offset);
66a2d927
VS
2355 } else {
2356 old_offset += *y * pitch + *x * cpp;
2357
2358 *y = (old_offset - new_offset) / pitch;
2359 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2360 }
2361
2362 return new_offset;
2363}
2364
e8837d98
VS
2365/*
2366 * Adjust the tile offset by moving the difference into
2367 * the x/y offsets.
2368 */
2369static u32 intel_adjust_tile_offset(int *x, int *y,
2370 const struct intel_plane_state *state, int plane,
2371 u32 old_offset, u32 new_offset)
2372{
2373 return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
2374 state->base.rotation,
2375 old_offset, new_offset);
2376}
2377
8d0deca8
VS
2378/*
2379 * Computes the linear offset to the base tile and adjusts
2380 * x, y. bytes per pixel is assumed to be a power-of-two.
2381 *
2382 * In the 90/270 rotated case, x and y are assumed
2383 * to be already rotated to match the rotated GTT view, and
2384 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2385 *
2386 * This function is used when computing the derived information
2387 * under intel_framebuffer, so using any of that information
2388 * here is not allowed. Anything under drm_framebuffer can be
2389 * used. This is why the user has to pass in the pitch since it
2390 * is specified in the rotated orientation.
8d0deca8 2391 */
6687c906
VS
2392static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2393 int *x, int *y,
2394 const struct drm_framebuffer *fb, int plane,
2395 unsigned int pitch,
2396 unsigned int rotation,
2397 u32 alignment)
c2c75131 2398{
bae781b2 2399 uint64_t fb_modifier = fb->modifier;
353c8598 2400 unsigned int cpp = fb->format->cpp[plane];
6687c906 2401 u32 offset, offset_aligned;
29cf9491 2402
29cf9491
VS
2403 if (alignment)
2404 alignment--;
2405
2f075565 2406 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
8d0deca8
VS
2407 unsigned int tile_size, tile_width, tile_height;
2408 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2409
d843310d 2410 tile_size = intel_tile_size(dev_priv);
d88c4afd 2411 intel_tile_dims(fb, plane, &tile_width, &tile_height);
8d0deca8 2412
bd2ef25d 2413 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2414 pitch_tiles = pitch / tile_height;
2415 swap(tile_width, tile_height);
2416 } else {
2417 pitch_tiles = pitch / (tile_width * cpp);
2418 }
d843310d
VS
2419
2420 tile_rows = *y / tile_height;
2421 *y %= tile_height;
c2c75131 2422
8d0deca8
VS
2423 tiles = *x / tile_width;
2424 *x %= tile_width;
bc752862 2425
29cf9491
VS
2426 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2427 offset_aligned = offset & ~alignment;
bc752862 2428
e8837d98
VS
2429 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2430 tile_size, pitch_tiles,
2431 offset, offset_aligned);
29cf9491 2432 } else {
bc752862 2433 offset = *y * pitch + *x * cpp;
29cf9491
VS
2434 offset_aligned = offset & ~alignment;
2435
4e9a86b6
VS
2436 *y = (offset & alignment) / pitch;
2437 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2438 }
29cf9491
VS
2439
2440 return offset_aligned;
c2c75131
DV
2441}
2442
6687c906 2443u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2444 const struct intel_plane_state *state,
2445 int plane)
6687c906 2446{
1e7b4fd8
VS
2447 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2448 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2949056c
VS
2449 const struct drm_framebuffer *fb = state->base.fb;
2450 unsigned int rotation = state->base.rotation;
ef78ec94 2451 int pitch = intel_fb_pitch(fb, plane, rotation);
1e7b4fd8
VS
2452 u32 alignment;
2453
2454 if (intel_plane->id == PLANE_CURSOR)
2455 alignment = intel_cursor_alignment(dev_priv);
2456 else
2457 alignment = intel_surf_alignment(fb, plane);
6687c906
VS
2458
2459 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2460 rotation, alignment);
2461}
2462
e8837d98
VS
2463/* Convert the fb->offset[] into x/y offsets */
2464static int intel_fb_offset_to_xy(int *x, int *y,
2465 const struct drm_framebuffer *fb, int plane)
6687c906 2466{
e8837d98
VS
2467 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2468
2469 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
2470 fb->offsets[plane] % intel_tile_size(dev_priv))
2471 return -EINVAL;
2472
2473 *x = 0;
2474 *y = 0;
6687c906 2475
e8837d98
VS
2476 _intel_adjust_tile_offset(x, y,
2477 fb, plane, DRM_MODE_ROTATE_0,
2478 fb->offsets[plane], 0);
2479
2480 return 0;
6687c906
VS
2481}
2482
72618ebf
VS
2483static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2484{
2485 switch (fb_modifier) {
2486 case I915_FORMAT_MOD_X_TILED:
2487 return I915_TILING_X;
2488 case I915_FORMAT_MOD_Y_TILED:
2e2adb05 2489 case I915_FORMAT_MOD_Y_TILED_CCS:
72618ebf
VS
2490 return I915_TILING_Y;
2491 default:
2492 return I915_TILING_NONE;
2493 }
2494}
2495
bbfb6ce8
VS
2496static const struct drm_format_info ccs_formats[] = {
2497 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2498 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2499 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2500 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2501};
2502
2503static const struct drm_format_info *
2504lookup_format_info(const struct drm_format_info formats[],
2505 int num_formats, u32 format)
2506{
2507 int i;
2508
2509 for (i = 0; i < num_formats; i++) {
2510 if (formats[i].format == format)
2511 return &formats[i];
2512 }
2513
2514 return NULL;
2515}
2516
2517static const struct drm_format_info *
2518intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2519{
2520 switch (cmd->modifier[0]) {
2521 case I915_FORMAT_MOD_Y_TILED_CCS:
2522 case I915_FORMAT_MOD_Yf_TILED_CCS:
2523 return lookup_format_info(ccs_formats,
2524 ARRAY_SIZE(ccs_formats),
2525 cmd->pixel_format);
2526 default:
2527 return NULL;
2528 }
2529}
2530
6687c906
VS
2531static int
2532intel_fill_fb_info(struct drm_i915_private *dev_priv,
2533 struct drm_framebuffer *fb)
2534{
2535 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2536 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2537 u32 gtt_offset_rotated = 0;
2538 unsigned int max_size = 0;
bcb0b461 2539 int i, num_planes = fb->format->num_planes;
6687c906
VS
2540 unsigned int tile_size = intel_tile_size(dev_priv);
2541
2542 for (i = 0; i < num_planes; i++) {
2543 unsigned int width, height;
2544 unsigned int cpp, size;
2545 u32 offset;
2546 int x, y;
e8837d98 2547 int ret;
6687c906 2548
353c8598 2549 cpp = fb->format->cpp[i];
145fcb11
VS
2550 width = drm_framebuffer_plane_width(fb->width, fb, i);
2551 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906 2552
e8837d98
VS
2553 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2554 if (ret) {
2555 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2556 i, fb->offsets[i]);
2557 return ret;
2558 }
6687c906 2559
2e2adb05
VS
2560 if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2561 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
2562 int hsub = fb->format->hsub;
2563 int vsub = fb->format->vsub;
2564 int tile_width, tile_height;
2565 int main_x, main_y;
2566 int ccs_x, ccs_y;
2567
2568 intel_tile_dims(fb, i, &tile_width, &tile_height);
e8837d98
VS
2569 tile_width *= hsub;
2570 tile_height *= vsub;
2e2adb05 2571
e8837d98
VS
2572 ccs_x = (x * hsub) % tile_width;
2573 ccs_y = (y * vsub) % tile_height;
2574 main_x = intel_fb->normal[0].x % tile_width;
2575 main_y = intel_fb->normal[0].y % tile_height;
2e2adb05
VS
2576
2577 /*
2578 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2579 * x/y offsets must match between CCS and the main surface.
2580 */
2581 if (main_x != ccs_x || main_y != ccs_y) {
2582 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2583 main_x, main_y,
2584 ccs_x, ccs_y,
2585 intel_fb->normal[0].x,
2586 intel_fb->normal[0].y,
2587 x, y);
2588 return -EINVAL;
2589 }
2590 }
2591
60d5f2a4
VS
2592 /*
2593 * The fence (if used) is aligned to the start of the object
2594 * so having the framebuffer wrap around across the edge of the
2595 * fenced region doesn't really work. We have no API to configure
2596 * the fence start offset within the object (nor could we probably
2597 * on gen2/3). So it's just easier if we just require that the
2598 * fb layout agrees with the fence layout. We already check that the
2599 * fb stride matches the fence stride elsewhere.
2600 */
18db229d 2601 if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
60d5f2a4 2602 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2603 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2604 i, fb->offsets[i]);
60d5f2a4
VS
2605 return -EINVAL;
2606 }
2607
6687c906
VS
2608 /*
2609 * First pixel of the framebuffer from
2610 * the start of the normal gtt mapping.
2611 */
2612 intel_fb->normal[i].x = x;
2613 intel_fb->normal[i].y = y;
2614
2615 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
3ca46c0a 2616 fb, i, fb->pitches[i],
c2c446ad 2617 DRM_MODE_ROTATE_0, tile_size);
6687c906
VS
2618 offset /= tile_size;
2619
2f075565 2620 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
6687c906
VS
2621 unsigned int tile_width, tile_height;
2622 unsigned int pitch_tiles;
2623 struct drm_rect r;
2624
d88c4afd 2625 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2626
2627 rot_info->plane[i].offset = offset;
2628 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2629 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2630 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2631
2632 intel_fb->rotated[i].pitch =
2633 rot_info->plane[i].height * tile_height;
2634
2635 /* how many tiles does this plane need */
2636 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2637 /*
2638 * If the plane isn't horizontally tile aligned,
2639 * we need one more tile.
2640 */
2641 if (x != 0)
2642 size++;
2643
2644 /* rotate the x/y offsets to match the GTT view */
2645 r.x1 = x;
2646 r.y1 = y;
2647 r.x2 = x + width;
2648 r.y2 = y + height;
2649 drm_rect_rotate(&r,
2650 rot_info->plane[i].width * tile_width,
2651 rot_info->plane[i].height * tile_height,
c2c446ad 2652 DRM_MODE_ROTATE_270);
6687c906
VS
2653 x = r.x1;
2654 y = r.y1;
2655
2656 /* rotate the tile dimensions to match the GTT view */
2657 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2658 swap(tile_width, tile_height);
2659
2660 /*
2661 * We only keep the x/y offsets, so push all of the
2662 * gtt offset into the x/y offsets.
2663 */
e8837d98
VS
2664 __intel_adjust_tile_offset(&x, &y,
2665 tile_width, tile_height,
2666 tile_size, pitch_tiles,
2667 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2668
2669 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2670
2671 /*
2672 * First pixel of the framebuffer from
2673 * the start of the rotated gtt mapping.
2674 */
2675 intel_fb->rotated[i].x = x;
2676 intel_fb->rotated[i].y = y;
2677 } else {
2678 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2679 x * cpp, tile_size);
2680 }
2681
2682 /* how many tiles in total needed in the bo */
2683 max_size = max(max_size, offset + size);
2684 }
2685
144cc143
VS
2686 if (max_size * tile_size > intel_fb->obj->base.size) {
2687 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2688 max_size * tile_size, intel_fb->obj->base.size);
6687c906
VS
2689 return -EINVAL;
2690 }
2691
2692 return 0;
2693}
2694
b35d63fa 2695static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2696{
2697 switch (format) {
2698 case DISPPLANE_8BPP:
2699 return DRM_FORMAT_C8;
2700 case DISPPLANE_BGRX555:
2701 return DRM_FORMAT_XRGB1555;
2702 case DISPPLANE_BGRX565:
2703 return DRM_FORMAT_RGB565;
2704 default:
2705 case DISPPLANE_BGRX888:
2706 return DRM_FORMAT_XRGB8888;
2707 case DISPPLANE_RGBX888:
2708 return DRM_FORMAT_XBGR8888;
2709 case DISPPLANE_BGRX101010:
2710 return DRM_FORMAT_XRGB2101010;
2711 case DISPPLANE_RGBX101010:
2712 return DRM_FORMAT_XBGR2101010;
2713 }
2714}
2715
bc8d7dff
DL
2716static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2717{
2718 switch (format) {
2719 case PLANE_CTL_FORMAT_RGB_565:
2720 return DRM_FORMAT_RGB565;
2721 default:
2722 case PLANE_CTL_FORMAT_XRGB_8888:
2723 if (rgb_order) {
2724 if (alpha)
2725 return DRM_FORMAT_ABGR8888;
2726 else
2727 return DRM_FORMAT_XBGR8888;
2728 } else {
2729 if (alpha)
2730 return DRM_FORMAT_ARGB8888;
2731 else
2732 return DRM_FORMAT_XRGB8888;
2733 }
2734 case PLANE_CTL_FORMAT_XRGB_2101010:
2735 if (rgb_order)
2736 return DRM_FORMAT_XBGR2101010;
2737 else
2738 return DRM_FORMAT_XRGB2101010;
2739 }
2740}
2741
5724dbd1 2742static bool
f6936e29
DV
2743intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2744 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2745{
2746 struct drm_device *dev = crtc->base.dev;
3badb49f 2747 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2748 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2749 struct drm_i915_gem_object *obj = NULL;
2750 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2751 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2752 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2753 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2754 PAGE_SIZE);
2755
2756 size_aligned -= base_aligned;
46f297fb 2757
ff2652ea
CW
2758 if (plane_config->size == 0)
2759 return false;
2760
3badb49f
PZ
2761 /* If the FB is too big, just don't use it since fbdev is not very
2762 * important and we should probably use that space with FBC or other
2763 * features. */
72e96d64 2764 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2765 return false;
2766
12c83d99 2767 mutex_lock(&dev->struct_mutex);
187685cb 2768 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2769 base_aligned,
2770 base_aligned,
2771 size_aligned);
24dbf51a
CW
2772 mutex_unlock(&dev->struct_mutex);
2773 if (!obj)
484b41dd 2774 return false;
46f297fb 2775
3e510a8e
CW
2776 if (plane_config->tiling == I915_TILING_X)
2777 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2778
438b74a5 2779 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2780 mode_cmd.width = fb->width;
2781 mode_cmd.height = fb->height;
2782 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2783 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2784 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2785
24dbf51a 2786 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2787 DRM_DEBUG_KMS("intel fb init failed\n");
2788 goto out_unref_obj;
2789 }
12c83d99 2790
484b41dd 2791
f6936e29 2792 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2793 return true;
46f297fb
JB
2794
2795out_unref_obj:
f8c417cd 2796 i915_gem_object_put(obj);
484b41dd
JB
2797 return false;
2798}
2799
e9728bd8
VS
2800static void
2801intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2802 struct intel_plane_state *plane_state,
2803 bool visible)
2804{
2805 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2806
2807 plane_state->base.visible = visible;
2808
2809 /* FIXME pre-g4x don't work like this */
2810 if (visible) {
2811 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2812 crtc_state->active_planes |= BIT(plane->id);
2813 } else {
2814 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2815 crtc_state->active_planes &= ~BIT(plane->id);
2816 }
2817
2818 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2819 crtc_state->base.crtc->name,
2820 crtc_state->active_planes);
2821}
2822
5724dbd1 2823static void
f6936e29
DV
2824intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2825 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2826{
2827 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2828 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2829 struct drm_crtc *c;
2ff8fde1 2830 struct drm_i915_gem_object *obj;
88595ac9 2831 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2832 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2833 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2834 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2835 struct intel_plane_state *intel_state =
2836 to_intel_plane_state(plane_state);
88595ac9 2837 struct drm_framebuffer *fb;
484b41dd 2838
2d14030b 2839 if (!plane_config->fb)
484b41dd
JB
2840 return;
2841
f6936e29 2842 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2843 fb = &plane_config->fb->base;
2844 goto valid_fb;
f55548b5 2845 }
484b41dd 2846
2d14030b 2847 kfree(plane_config->fb);
484b41dd
JB
2848
2849 /*
2850 * Failed to alloc the obj, check to see if we should share
2851 * an fb with another CRTC instead
2852 */
70e1e0ec 2853 for_each_crtc(dev, c) {
be1e3415 2854 struct intel_plane_state *state;
484b41dd
JB
2855
2856 if (c == &intel_crtc->base)
2857 continue;
2858
be1e3415 2859 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2860 continue;
2861
be1e3415
CW
2862 state = to_intel_plane_state(c->primary->state);
2863 if (!state->vma)
484b41dd
JB
2864 continue;
2865
be1e3415
CW
2866 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2867 fb = c->primary->fb;
c3ed1103 2868 drm_framebuffer_get(fb);
88595ac9 2869 goto valid_fb;
484b41dd
JB
2870 }
2871 }
88595ac9 2872
200757f5
MR
2873 /*
2874 * We've failed to reconstruct the BIOS FB. Current display state
2875 * indicates that the primary plane is visible, but has a NULL FB,
2876 * which will lead to problems later if we don't fix it up. The
2877 * simplest solution is to just disable the primary plane now and
2878 * pretend the BIOS never had it enabled.
2879 */
e9728bd8
VS
2880 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2881 to_intel_plane_state(plane_state),
2882 false);
2622a081 2883 intel_pre_disable_primary_noatomic(&intel_crtc->base);
72259536 2884 trace_intel_disable_plane(primary, intel_crtc);
282dbf9b 2885 intel_plane->disable_plane(intel_plane, intel_crtc);
200757f5 2886
88595ac9
DV
2887 return;
2888
2889valid_fb:
be1e3415
CW
2890 mutex_lock(&dev->struct_mutex);
2891 intel_state->vma =
2892 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2893 mutex_unlock(&dev->struct_mutex);
2894 if (IS_ERR(intel_state->vma)) {
2895 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2896 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2897
2898 intel_state->vma = NULL;
c3ed1103 2899 drm_framebuffer_put(fb);
be1e3415
CW
2900 return;
2901 }
2902
f44e2659
VS
2903 plane_state->src_x = 0;
2904 plane_state->src_y = 0;
be5651f2
ML
2905 plane_state->src_w = fb->width << 16;
2906 plane_state->src_h = fb->height << 16;
2907
f44e2659
VS
2908 plane_state->crtc_x = 0;
2909 plane_state->crtc_y = 0;
be5651f2
ML
2910 plane_state->crtc_w = fb->width;
2911 plane_state->crtc_h = fb->height;
2912
1638d30c
RC
2913 intel_state->base.src = drm_plane_state_src(plane_state);
2914 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2915
88595ac9 2916 obj = intel_fb_obj(fb);
3e510a8e 2917 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2918 dev_priv->preserve_bios_swizzle = true;
2919
c3ed1103 2920 drm_framebuffer_get(fb);
be5651f2 2921 primary->fb = primary->state->fb = fb;
36750f28 2922 primary->crtc = primary->state->crtc = &intel_crtc->base;
e9728bd8
VS
2923
2924 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2925 to_intel_plane_state(plane_state),
2926 true);
2927
faf5bf0a
CW
2928 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2929 &obj->frontbuffer_bits);
46f297fb
JB
2930}
2931
b63a16f6
VS
2932static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2933 unsigned int rotation)
2934{
353c8598 2935 int cpp = fb->format->cpp[plane];
b63a16f6 2936
bae781b2 2937 switch (fb->modifier) {
2f075565 2938 case DRM_FORMAT_MOD_LINEAR:
b63a16f6
VS
2939 case I915_FORMAT_MOD_X_TILED:
2940 switch (cpp) {
2941 case 8:
2942 return 4096;
2943 case 4:
2944 case 2:
2945 case 1:
2946 return 8192;
2947 default:
2948 MISSING_CASE(cpp);
2949 break;
2950 }
2951 break;
2e2adb05
VS
2952 case I915_FORMAT_MOD_Y_TILED_CCS:
2953 case I915_FORMAT_MOD_Yf_TILED_CCS:
2954 /* FIXME AUX plane? */
b63a16f6
VS
2955 case I915_FORMAT_MOD_Y_TILED:
2956 case I915_FORMAT_MOD_Yf_TILED:
2957 switch (cpp) {
2958 case 8:
2959 return 2048;
2960 case 4:
2961 return 4096;
2962 case 2:
2963 case 1:
2964 return 8192;
2965 default:
2966 MISSING_CASE(cpp);
2967 break;
2968 }
2969 break;
2970 default:
bae781b2 2971 MISSING_CASE(fb->modifier);
b63a16f6
VS
2972 }
2973
2974 return 2048;
2975}
2976
2e2adb05
VS
2977static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2978 int main_x, int main_y, u32 main_offset)
2979{
2980 const struct drm_framebuffer *fb = plane_state->base.fb;
2981 int hsub = fb->format->hsub;
2982 int vsub = fb->format->vsub;
2983 int aux_x = plane_state->aux.x;
2984 int aux_y = plane_state->aux.y;
2985 u32 aux_offset = plane_state->aux.offset;
2986 u32 alignment = intel_surf_alignment(fb, 1);
2987
2988 while (aux_offset >= main_offset && aux_y <= main_y) {
2989 int x, y;
2990
2991 if (aux_x == main_x && aux_y == main_y)
2992 break;
2993
2994 if (aux_offset == 0)
2995 break;
2996
2997 x = aux_x / hsub;
2998 y = aux_y / vsub;
2999 aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
3000 aux_offset, aux_offset - alignment);
3001 aux_x = x * hsub + aux_x % hsub;
3002 aux_y = y * vsub + aux_y % vsub;
3003 }
3004
3005 if (aux_x != main_x || aux_y != main_y)
3006 return false;
3007
3008 plane_state->aux.offset = aux_offset;
3009 plane_state->aux.x = aux_x;
3010 plane_state->aux.y = aux_y;
3011
3012 return true;
3013}
3014
b63a16f6
VS
3015static int skl_check_main_surface(struct intel_plane_state *plane_state)
3016{
b63a16f6
VS
3017 const struct drm_framebuffer *fb = plane_state->base.fb;
3018 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
3019 int x = plane_state->base.src.x1 >> 16;
3020 int y = plane_state->base.src.y1 >> 16;
3021 int w = drm_rect_width(&plane_state->base.src) >> 16;
3022 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
3023 int max_width = skl_max_plane_width(fb, 0, rotation);
3024 int max_height = 4096;
8d970654 3025 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
3026
3027 if (w > max_width || h > max_height) {
3028 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3029 w, h, max_width, max_height);
3030 return -EINVAL;
3031 }
3032
3033 intel_add_fb_offsets(&x, &y, plane_state, 0);
3034 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
d88c4afd 3035 alignment = intel_surf_alignment(fb, 0);
b63a16f6 3036
8d970654
VS
3037 /*
3038 * AUX surface offset is specified as the distance from the
3039 * main surface offset, and it must be non-negative. Make
3040 * sure that is what we will get.
3041 */
3042 if (offset > aux_offset)
3043 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3044 offset, aux_offset & ~(alignment - 1));
3045
b63a16f6
VS
3046 /*
3047 * When using an X-tiled surface, the plane blows up
3048 * if the x offset + width exceed the stride.
3049 *
3050 * TODO: linear and Y-tiled seem fine, Yf untested,
3051 */
bae781b2 3052 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 3053 int cpp = fb->format->cpp[0];
b63a16f6
VS
3054
3055 while ((x + w) * cpp > fb->pitches[0]) {
3056 if (offset == 0) {
2e2adb05 3057 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
b63a16f6
VS
3058 return -EINVAL;
3059 }
3060
3061 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3062 offset, offset - alignment);
3063 }
3064 }
3065
2e2adb05
VS
3066 /*
3067 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3068 * they match with the main surface x/y offsets.
3069 */
3070 if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3071 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3072 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3073 if (offset == 0)
3074 break;
3075
3076 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3077 offset, offset - alignment);
3078 }
3079
3080 if (x != plane_state->aux.x || y != plane_state->aux.y) {
3081 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3082 return -EINVAL;
3083 }
3084 }
3085
b63a16f6
VS
3086 plane_state->main.offset = offset;
3087 plane_state->main.x = x;
3088 plane_state->main.y = y;
3089
3090 return 0;
3091}
3092
8d970654
VS
3093static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3094{
3095 const struct drm_framebuffer *fb = plane_state->base.fb;
3096 unsigned int rotation = plane_state->base.rotation;
3097 int max_width = skl_max_plane_width(fb, 1, rotation);
3098 int max_height = 4096;
cc926387
DV
3099 int x = plane_state->base.src.x1 >> 17;
3100 int y = plane_state->base.src.y1 >> 17;
3101 int w = drm_rect_width(&plane_state->base.src) >> 17;
3102 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
3103 u32 offset;
3104
3105 intel_add_fb_offsets(&x, &y, plane_state, 1);
3106 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3107
3108 /* FIXME not quite sure how/if these apply to the chroma plane */
3109 if (w > max_width || h > max_height) {
3110 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3111 w, h, max_width, max_height);
3112 return -EINVAL;
3113 }
3114
3115 plane_state->aux.offset = offset;
3116 plane_state->aux.x = x;
3117 plane_state->aux.y = y;
3118
3119 return 0;
3120}
3121
2e2adb05
VS
3122static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3123{
3124 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
3125 struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
3126 const struct drm_framebuffer *fb = plane_state->base.fb;
3127 int src_x = plane_state->base.src.x1 >> 16;
3128 int src_y = plane_state->base.src.y1 >> 16;
3129 int hsub = fb->format->hsub;
3130 int vsub = fb->format->vsub;
3131 int x = src_x / hsub;
3132 int y = src_y / vsub;
3133 u32 offset;
3134
3135 switch (plane->id) {
3136 case PLANE_PRIMARY:
3137 case PLANE_SPRITE0:
3138 break;
3139 default:
3140 DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
3141 return -EINVAL;
3142 }
3143
3144 if (crtc->pipe == PIPE_C) {
3145 DRM_DEBUG_KMS("No RC support on pipe C\n");
3146 return -EINVAL;
3147 }
3148
3149 if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
3150 DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
3151 plane_state->base.rotation);
3152 return -EINVAL;
3153 }
3154
3155 intel_add_fb_offsets(&x, &y, plane_state, 1);
3156 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3157
3158 plane_state->aux.offset = offset;
3159 plane_state->aux.x = x * hsub + src_x % hsub;
3160 plane_state->aux.y = y * vsub + src_y % vsub;
3161
3162 return 0;
3163}
3164
b63a16f6
VS
3165int skl_check_plane_surface(struct intel_plane_state *plane_state)
3166{
3167 const struct drm_framebuffer *fb = plane_state->base.fb;
3168 unsigned int rotation = plane_state->base.rotation;
3169 int ret;
3170
a5e4c7d0
VS
3171 if (!plane_state->base.visible)
3172 return 0;
3173
b63a16f6 3174 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 3175 if (drm_rotation_90_or_270(rotation))
cc926387 3176 drm_rect_rotate(&plane_state->base.src,
da064b47 3177 fb->width << 16, fb->height << 16,
c2c446ad 3178 DRM_MODE_ROTATE_270);
b63a16f6 3179
8d970654
VS
3180 /*
3181 * Handle the AUX surface first since
3182 * the main surface setup depends on it.
3183 */
438b74a5 3184 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
3185 ret = skl_check_nv12_aux_surface(plane_state);
3186 if (ret)
3187 return ret;
2e2adb05
VS
3188 } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3189 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3190 ret = skl_check_ccs_aux_surface(plane_state);
3191 if (ret)
3192 return ret;
8d970654
VS
3193 } else {
3194 plane_state->aux.offset = ~0xfff;
3195 plane_state->aux.x = 0;
3196 plane_state->aux.y = 0;
3197 }
3198
b63a16f6
VS
3199 ret = skl_check_main_surface(plane_state);
3200 if (ret)
3201 return ret;
3202
3203 return 0;
3204}
3205
7145f60a
VS
3206static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3207 const struct intel_plane_state *plane_state)
81255565 3208{
7145f60a
VS
3209 struct drm_i915_private *dev_priv =
3210 to_i915(plane_state->base.plane->dev);
3211 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3212 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 3213 unsigned int rotation = plane_state->base.rotation;
7145f60a 3214 u32 dspcntr;
c9ba6fad 3215
7145f60a 3216 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 3217
6a4407a6
VS
3218 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3219 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
7145f60a 3220 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 3221
6a4407a6
VS
3222 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3223 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
f45651ba 3224
d509e28b
VS
3225 if (INTEL_GEN(dev_priv) < 4)
3226 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
81255565 3227
438b74a5 3228 switch (fb->format->format) {
57779d06 3229 case DRM_FORMAT_C8:
81255565
JB
3230 dspcntr |= DISPPLANE_8BPP;
3231 break;
57779d06 3232 case DRM_FORMAT_XRGB1555:
57779d06 3233 dspcntr |= DISPPLANE_BGRX555;
81255565 3234 break;
57779d06
VS
3235 case DRM_FORMAT_RGB565:
3236 dspcntr |= DISPPLANE_BGRX565;
3237 break;
3238 case DRM_FORMAT_XRGB8888:
57779d06
VS
3239 dspcntr |= DISPPLANE_BGRX888;
3240 break;
3241 case DRM_FORMAT_XBGR8888:
57779d06
VS
3242 dspcntr |= DISPPLANE_RGBX888;
3243 break;
3244 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3245 dspcntr |= DISPPLANE_BGRX101010;
3246 break;
3247 case DRM_FORMAT_XBGR2101010:
57779d06 3248 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3249 break;
3250 default:
7145f60a
VS
3251 MISSING_CASE(fb->format->format);
3252 return 0;
81255565 3253 }
57779d06 3254
72618ebf 3255 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3256 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3257 dspcntr |= DISPPLANE_TILED;
81255565 3258
c2c446ad 3259 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
3260 dspcntr |= DISPPLANE_ROTATE_180;
3261
c2c446ad 3262 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
3263 dspcntr |= DISPPLANE_MIRROR;
3264
7145f60a
VS
3265 return dspcntr;
3266}
de1aa629 3267
f9407ae1 3268int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
3269{
3270 struct drm_i915_private *dev_priv =
3271 to_i915(plane_state->base.plane->dev);
3272 int src_x = plane_state->base.src.x1 >> 16;
3273 int src_y = plane_state->base.src.y1 >> 16;
3274 u32 offset;
81255565 3275
5b7fcc44 3276 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 3277
5b7fcc44
VS
3278 if (INTEL_GEN(dev_priv) >= 4)
3279 offset = intel_compute_tile_offset(&src_x, &src_y,
3280 plane_state, 0);
3281 else
3282 offset = 0;
3283
3284 /* HSW/BDW do this automagically in hardware */
3285 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3286 unsigned int rotation = plane_state->base.rotation;
3287 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3288 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3289
c2c446ad 3290 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
3291 src_x += src_w - 1;
3292 src_y += src_h - 1;
c2c446ad 3293 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
3294 src_x += src_w - 1;
3295 }
48404c1e
SJ
3296 }
3297
5b7fcc44
VS
3298 plane_state->main.offset = offset;
3299 plane_state->main.x = src_x;
3300 plane_state->main.y = src_y;
3301
3302 return 0;
3303}
3304
282dbf9b 3305static void i9xx_update_primary_plane(struct intel_plane *primary,
7145f60a
VS
3306 const struct intel_crtc_state *crtc_state,
3307 const struct intel_plane_state *plane_state)
3308{
282dbf9b 3309 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
282dbf9b
VS
3310 const struct drm_framebuffer *fb = plane_state->base.fb;
3311 enum plane plane = primary->plane;
7145f60a 3312 u32 linear_offset;
a0864d59 3313 u32 dspcntr = plane_state->ctl;
7145f60a 3314 i915_reg_t reg = DSPCNTR(plane);
5b7fcc44
VS
3315 int x = plane_state->main.x;
3316 int y = plane_state->main.y;
7145f60a 3317 unsigned long irqflags;
e288881b 3318 u32 dspaddr_offset;
7145f60a 3319
2949056c 3320 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3321
5b7fcc44 3322 if (INTEL_GEN(dev_priv) >= 4)
e288881b 3323 dspaddr_offset = plane_state->main.offset;
5b7fcc44 3324 else
e288881b 3325 dspaddr_offset = linear_offset;
2db3366b 3326
dd584fc0
VS
3327 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3328
78587de2
VS
3329 if (INTEL_GEN(dev_priv) < 4) {
3330 /* pipesrc and dspsize control the size that is scaled from,
3331 * which should always be the user's requested size.
3332 */
dd584fc0
VS
3333 I915_WRITE_FW(DSPSIZE(plane),
3334 ((crtc_state->pipe_src_h - 1) << 16) |
3335 (crtc_state->pipe_src_w - 1));
3336 I915_WRITE_FW(DSPPOS(plane), 0);
78587de2 3337 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
dd584fc0
VS
3338 I915_WRITE_FW(PRIMSIZE(plane),
3339 ((crtc_state->pipe_src_h - 1) << 16) |
3340 (crtc_state->pipe_src_w - 1));
3341 I915_WRITE_FW(PRIMPOS(plane), 0);
3342 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
78587de2
VS
3343 }
3344
dd584fc0 3345 I915_WRITE_FW(reg, dspcntr);
48404c1e 3346
dd584fc0 3347 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
3ba35e53
VS
3348 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3349 I915_WRITE_FW(DSPSURF(plane),
3350 intel_plane_ggtt_offset(plane_state) +
e288881b 3351 dspaddr_offset);
3ba35e53
VS
3352 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
3353 } else if (INTEL_GEN(dev_priv) >= 4) {
dd584fc0
VS
3354 I915_WRITE_FW(DSPSURF(plane),
3355 intel_plane_ggtt_offset(plane_state) +
e288881b 3356 dspaddr_offset);
dd584fc0
VS
3357 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3358 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bfb81049 3359 } else {
dd584fc0
VS
3360 I915_WRITE_FW(DSPADDR(plane),
3361 intel_plane_ggtt_offset(plane_state) +
e288881b 3362 dspaddr_offset);
bfb81049 3363 }
dd584fc0
VS
3364 POSTING_READ_FW(reg);
3365
3366 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3367}
3368
282dbf9b
VS
3369static void i9xx_disable_primary_plane(struct intel_plane *primary,
3370 struct intel_crtc *crtc)
17638cd6 3371{
282dbf9b
VS
3372 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3373 enum plane plane = primary->plane;
dd584fc0
VS
3374 unsigned long irqflags;
3375
3376 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3377
dd584fc0 3378 I915_WRITE_FW(DSPCNTR(plane), 0);
a8d201af 3379 if (INTEL_INFO(dev_priv)->gen >= 4)
dd584fc0 3380 I915_WRITE_FW(DSPSURF(plane), 0);
a8d201af 3381 else
dd584fc0
VS
3382 I915_WRITE_FW(DSPADDR(plane), 0);
3383 POSTING_READ_FW(DSPCNTR(plane));
3384
3385 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3386}
c9ba6fad 3387
d88c4afd
VS
3388static u32
3389intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
b321803d 3390{
2f075565 3391 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
b321803d 3392 return 64;
d88c4afd
VS
3393 else
3394 return intel_tile_width_bytes(fb, plane);
b321803d
DL
3395}
3396
e435d6e5
ML
3397static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3398{
3399 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3400 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3401
3402 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3403 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3404 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3405}
3406
a1b2278e
CK
3407/*
3408 * This function detaches (aka. unbinds) unused scalers in hardware
3409 */
0583236e 3410static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3411{
a1b2278e
CK
3412 struct intel_crtc_scaler_state *scaler_state;
3413 int i;
3414
a1b2278e
CK
3415 scaler_state = &intel_crtc->config->scaler_state;
3416
3417 /* loop through and disable scalers that aren't in use */
3418 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3419 if (!scaler_state->scalers[i].in_use)
3420 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3421 }
3422}
3423
d2196774
VS
3424u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3425 unsigned int rotation)
3426{
1b500535
VS
3427 u32 stride;
3428
3429 if (plane >= fb->format->num_planes)
3430 return 0;
3431
3432 stride = intel_fb_pitch(fb, plane, rotation);
d2196774
VS
3433
3434 /*
3435 * The stride is either expressed as a multiple of 64 bytes chunks for
3436 * linear buffers or in number of tiles for tiled buffers.
3437 */
d88c4afd
VS
3438 if (drm_rotation_90_or_270(rotation))
3439 stride /= intel_tile_height(fb, plane);
3440 else
3441 stride /= intel_fb_stride_alignment(fb, plane);
d2196774
VS
3442
3443 return stride;
3444}
3445
2e881264 3446static u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3447{
6156a456 3448 switch (pixel_format) {
d161cf7a 3449 case DRM_FORMAT_C8:
c34ce3d1 3450 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3451 case DRM_FORMAT_RGB565:
c34ce3d1 3452 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3453 case DRM_FORMAT_XBGR8888:
c34ce3d1 3454 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3455 case DRM_FORMAT_XRGB8888:
c34ce3d1 3456 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3457 /*
3458 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3459 * to be already pre-multiplied. We need to add a knob (or a different
3460 * DRM_FORMAT) for user-space to configure that.
3461 */
f75fb42a 3462 case DRM_FORMAT_ABGR8888:
c34ce3d1 3463 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3464 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3465 case DRM_FORMAT_ARGB8888:
c34ce3d1 3466 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3467 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3468 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3469 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3470 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3471 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3472 case DRM_FORMAT_YUYV:
c34ce3d1 3473 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3474 case DRM_FORMAT_YVYU:
c34ce3d1 3475 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3476 case DRM_FORMAT_UYVY:
c34ce3d1 3477 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3478 case DRM_FORMAT_VYUY:
c34ce3d1 3479 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3480 default:
4249eeef 3481 MISSING_CASE(pixel_format);
70d21f0e 3482 }
8cfcba41 3483
c34ce3d1 3484 return 0;
6156a456 3485}
70d21f0e 3486
2e881264 3487static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3488{
6156a456 3489 switch (fb_modifier) {
2f075565 3490 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 3491 break;
30af77c4 3492 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3493 return PLANE_CTL_TILED_X;
b321803d 3494 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3495 return PLANE_CTL_TILED_Y;
2e2adb05
VS
3496 case I915_FORMAT_MOD_Y_TILED_CCS:
3497 return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
b321803d 3498 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3499 return PLANE_CTL_TILED_YF;
2e2adb05
VS
3500 case I915_FORMAT_MOD_Yf_TILED_CCS:
3501 return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
70d21f0e 3502 default:
6156a456 3503 MISSING_CASE(fb_modifier);
70d21f0e 3504 }
8cfcba41 3505
c34ce3d1 3506 return 0;
6156a456 3507}
70d21f0e 3508
2e881264 3509static u32 skl_plane_ctl_rotation(unsigned int rotation)
6156a456 3510{
3b7a5119 3511 switch (rotation) {
c2c446ad 3512 case DRM_MODE_ROTATE_0:
6156a456 3513 break;
1e8df167 3514 /*
c2c446ad 3515 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
3516 * while i915 HW rotation is clockwise, thats why this swapping.
3517 */
c2c446ad 3518 case DRM_MODE_ROTATE_90:
1e8df167 3519 return PLANE_CTL_ROTATE_270;
c2c446ad 3520 case DRM_MODE_ROTATE_180:
c34ce3d1 3521 return PLANE_CTL_ROTATE_180;
c2c446ad 3522 case DRM_MODE_ROTATE_270:
1e8df167 3523 return PLANE_CTL_ROTATE_90;
6156a456
CK
3524 default:
3525 MISSING_CASE(rotation);
3526 }
3527
c34ce3d1 3528 return 0;
6156a456
CK
3529}
3530
2e881264
VS
3531u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3532 const struct intel_plane_state *plane_state)
46f788ba
VS
3533{
3534 struct drm_i915_private *dev_priv =
3535 to_i915(plane_state->base.plane->dev);
3536 const struct drm_framebuffer *fb = plane_state->base.fb;
3537 unsigned int rotation = plane_state->base.rotation;
2e881264 3538 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3539 u32 plane_ctl;
3540
3541 plane_ctl = PLANE_CTL_ENABLE;
3542
6602be0e 3543 if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
46f788ba
VS
3544 plane_ctl |=
3545 PLANE_CTL_PIPE_GAMMA_ENABLE |
3546 PLANE_CTL_PIPE_CSC_ENABLE |
3547 PLANE_CTL_PLANE_GAMMA_DISABLE;
3548 }
3549
3550 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3551 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
3552 plane_ctl |= skl_plane_ctl_rotation(rotation);
3553
2e881264
VS
3554 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3555 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3556 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3557 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3558
46f788ba
VS
3559 return plane_ctl;
3560}
3561
73974893
ML
3562static int
3563__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3564 struct drm_atomic_state *state,
3565 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3566{
3567 struct drm_crtc_state *crtc_state;
3568 struct drm_crtc *crtc;
3569 int i, ret;
11c22da6 3570
aecd36b8 3571 intel_modeset_setup_hw_state(dev, ctx);
29b74b7f 3572 i915_redisable_vga(to_i915(dev));
73974893
ML
3573
3574 if (!state)
3575 return 0;
3576
aa5e9b47
ML
3577 /*
3578 * We've duplicated the state, pointers to the old state are invalid.
3579 *
3580 * Don't attempt to use the old state until we commit the duplicated state.
3581 */
3582 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3583 /*
3584 * Force recalculation even if we restore
3585 * current state. With fast modeset this may not result
3586 * in a modeset when the state is compatible.
3587 */
3588 crtc_state->mode_changed = true;
96a02917 3589 }
73974893
ML
3590
3591 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3592 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3593 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3594
581e49fe 3595 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3596
3597 WARN_ON(ret == -EDEADLK);
3598 return ret;
96a02917
VS
3599}
3600
4ac2ba2f
VS
3601static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3602{
ae98104b
VS
3603 return intel_has_gpu_reset(dev_priv) &&
3604 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3605}
3606
c033666a 3607void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3608{
73974893
ML
3609 struct drm_device *dev = &dev_priv->drm;
3610 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3611 struct drm_atomic_state *state;
3612 int ret;
3613
ce87ea15
DV
3614
3615 /* reset doesn't touch the display */
4f044a88 3616 if (!i915_modparams.force_reset_modeset_test &&
ce87ea15
DV
3617 !gpu_reset_clobbers_display(dev_priv))
3618 return;
3619
9db529aa
DV
3620 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3621 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3622 wake_up_all(&dev_priv->gpu_error.wait_queue);
3623
3624 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3625 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3626 i915_gem_set_wedged(dev_priv);
3627 }
97154ec2 3628
73974893
ML
3629 /*
3630 * Need mode_config.mutex so that we don't
3631 * trample ongoing ->detect() and whatnot.
3632 */
3633 mutex_lock(&dev->mode_config.mutex);
3634 drm_modeset_acquire_init(ctx, 0);
3635 while (1) {
3636 ret = drm_modeset_lock_all_ctx(dev, ctx);
3637 if (ret != -EDEADLK)
3638 break;
3639
3640 drm_modeset_backoff(ctx);
3641 }
f98ce92f
VS
3642 /*
3643 * Disabling the crtcs gracefully seems nicer. Also the
3644 * g33 docs say we should at least disable all the planes.
3645 */
73974893
ML
3646 state = drm_atomic_helper_duplicate_state(dev, ctx);
3647 if (IS_ERR(state)) {
3648 ret = PTR_ERR(state);
73974893 3649 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3650 return;
73974893
ML
3651 }
3652
3653 ret = drm_atomic_helper_disable_all(dev, ctx);
3654 if (ret) {
3655 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3656 drm_atomic_state_put(state);
3657 return;
73974893
ML
3658 }
3659
3660 dev_priv->modeset_restore_state = state;
3661 state->acquire_ctx = ctx;
7514747d
VS
3662}
3663
c033666a 3664void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3665{
73974893
ML
3666 struct drm_device *dev = &dev_priv->drm;
3667 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3668 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3669 int ret;
3670
ce87ea15 3671 /* reset doesn't touch the display */
4f044a88 3672 if (!i915_modparams.force_reset_modeset_test &&
ce87ea15
DV
3673 !gpu_reset_clobbers_display(dev_priv))
3674 return;
3675
3676 if (!state)
3677 goto unlock;
3678
73974893
ML
3679 dev_priv->modeset_restore_state = NULL;
3680
7514747d 3681 /* reset doesn't touch the display */
4ac2ba2f 3682 if (!gpu_reset_clobbers_display(dev_priv)) {
ce87ea15
DV
3683 /* for testing only restore the display */
3684 ret = __intel_display_resume(dev, state, ctx);
942d5d0d
CW
3685 if (ret)
3686 DRM_ERROR("Restoring old state failed with %i\n", ret);
73974893
ML
3687 } else {
3688 /*
3689 * The display has been reset as well,
3690 * so need a full re-initialization.
3691 */
3692 intel_runtime_pm_disable_interrupts(dev_priv);
3693 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3694
51f59205 3695 intel_pps_unlock_regs_wa(dev_priv);
73974893 3696 intel_modeset_init_hw(dev);
6ac43272 3697 intel_init_clock_gating(dev_priv);
7514747d 3698
73974893
ML
3699 spin_lock_irq(&dev_priv->irq_lock);
3700 if (dev_priv->display.hpd_irq_setup)
3701 dev_priv->display.hpd_irq_setup(dev_priv);
3702 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3703
581e49fe 3704 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3705 if (ret)
3706 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3707
73974893
ML
3708 intel_hpd_init(dev_priv);
3709 }
7514747d 3710
ce87ea15
DV
3711 drm_atomic_state_put(state);
3712unlock:
73974893
ML
3713 drm_modeset_drop_locks(ctx);
3714 drm_modeset_acquire_fini(ctx);
3715 mutex_unlock(&dev->mode_config.mutex);
9db529aa
DV
3716
3717 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
7514747d
VS
3718}
3719
1a15b77b
VS
3720static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3721 const struct intel_crtc_state *new_crtc_state)
e30e8f75 3722{
1a15b77b 3723 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
6315b5d3 3724 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
e30e8f75 3725
bfd16b2a 3726 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
1a15b77b 3727 crtc->base.mode = new_crtc_state->base.mode;
bfd16b2a 3728
e30e8f75
GP
3729 /*
3730 * Update pipe size and adjust fitter if needed: the reason for this is
3731 * that in compute_mode_changes we check the native mode (not the pfit
3732 * mode) to see if we can flip rather than do a full mode set. In the
3733 * fastboot case, we'll flip, but if we don't update the pipesrc and
3734 * pfit state, we'll end up with a big fb scanned out into the wrong
3735 * sized surface.
e30e8f75
GP
3736 */
3737
e30e8f75 3738 I915_WRITE(PIPESRC(crtc->pipe),
1a15b77b
VS
3739 ((new_crtc_state->pipe_src_w - 1) << 16) |
3740 (new_crtc_state->pipe_src_h - 1));
bfd16b2a
ML
3741
3742 /* on skylake this is done by detaching scalers */
6315b5d3 3743 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3744 skl_detach_scalers(crtc);
3745
1a15b77b 3746 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a 3747 skylake_pfit_enable(crtc);
6e266956 3748 } else if (HAS_PCH_SPLIT(dev_priv)) {
1a15b77b 3749 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a
ML
3750 ironlake_pfit_enable(crtc);
3751 else if (old_crtc_state->pch_pfit.enabled)
3752 ironlake_pfit_disable(crtc, true);
e30e8f75 3753 }
e30e8f75
GP
3754}
3755
4cbe4b2b 3756static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3757{
4cbe4b2b 3758 struct drm_device *dev = crtc->base.dev;
fac5e23e 3759 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3760 int pipe = crtc->pipe;
f0f59a00
VS
3761 i915_reg_t reg;
3762 u32 temp;
5e84e1a4
ZW
3763
3764 /* enable normal train */
3765 reg = FDI_TX_CTL(pipe);
3766 temp = I915_READ(reg);
fd6b8f43 3767 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3768 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3769 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3770 } else {
3771 temp &= ~FDI_LINK_TRAIN_NONE;
3772 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3773 }
5e84e1a4
ZW
3774 I915_WRITE(reg, temp);
3775
3776 reg = FDI_RX_CTL(pipe);
3777 temp = I915_READ(reg);
6e266956 3778 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3779 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3780 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3781 } else {
3782 temp &= ~FDI_LINK_TRAIN_NONE;
3783 temp |= FDI_LINK_TRAIN_NONE;
3784 }
3785 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3786
3787 /* wait one idle pattern time */
3788 POSTING_READ(reg);
3789 udelay(1000);
357555c0
JB
3790
3791 /* IVB wants error correction enabled */
fd6b8f43 3792 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3793 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3794 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3795}
3796
8db9d77b 3797/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3798static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3799 const struct intel_crtc_state *crtc_state)
8db9d77b 3800{
4cbe4b2b 3801 struct drm_device *dev = crtc->base.dev;
fac5e23e 3802 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3803 int pipe = crtc->pipe;
f0f59a00
VS
3804 i915_reg_t reg;
3805 u32 temp, tries;
8db9d77b 3806
1c8562f6 3807 /* FDI needs bits from pipe first */
0fc932b8 3808 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3809
e1a44743
AJ
3810 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3811 for train result */
5eddb70b
CW
3812 reg = FDI_RX_IMR(pipe);
3813 temp = I915_READ(reg);
e1a44743
AJ
3814 temp &= ~FDI_RX_SYMBOL_LOCK;
3815 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3816 I915_WRITE(reg, temp);
3817 I915_READ(reg);
e1a44743
AJ
3818 udelay(150);
3819
8db9d77b 3820 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3821 reg = FDI_TX_CTL(pipe);
3822 temp = I915_READ(reg);
627eb5a3 3823 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3824 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3825 temp &= ~FDI_LINK_TRAIN_NONE;
3826 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3827 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3828
5eddb70b
CW
3829 reg = FDI_RX_CTL(pipe);
3830 temp = I915_READ(reg);
8db9d77b
ZW
3831 temp &= ~FDI_LINK_TRAIN_NONE;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3833 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3834
3835 POSTING_READ(reg);
8db9d77b
ZW
3836 udelay(150);
3837
5b2adf89 3838 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3839 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3840 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3841 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3842
5eddb70b 3843 reg = FDI_RX_IIR(pipe);
e1a44743 3844 for (tries = 0; tries < 5; tries++) {
5eddb70b 3845 temp = I915_READ(reg);
8db9d77b
ZW
3846 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3847
3848 if ((temp & FDI_RX_BIT_LOCK)) {
3849 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3850 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3851 break;
3852 }
8db9d77b 3853 }
e1a44743 3854 if (tries == 5)
5eddb70b 3855 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3856
3857 /* Train 2 */
5eddb70b
CW
3858 reg = FDI_TX_CTL(pipe);
3859 temp = I915_READ(reg);
8db9d77b
ZW
3860 temp &= ~FDI_LINK_TRAIN_NONE;
3861 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3862 I915_WRITE(reg, temp);
8db9d77b 3863
5eddb70b
CW
3864 reg = FDI_RX_CTL(pipe);
3865 temp = I915_READ(reg);
8db9d77b
ZW
3866 temp &= ~FDI_LINK_TRAIN_NONE;
3867 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3868 I915_WRITE(reg, temp);
8db9d77b 3869
5eddb70b
CW
3870 POSTING_READ(reg);
3871 udelay(150);
8db9d77b 3872
5eddb70b 3873 reg = FDI_RX_IIR(pipe);
e1a44743 3874 for (tries = 0; tries < 5; tries++) {
5eddb70b 3875 temp = I915_READ(reg);
8db9d77b
ZW
3876 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3877
3878 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3879 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3880 DRM_DEBUG_KMS("FDI train 2 done.\n");
3881 break;
3882 }
8db9d77b 3883 }
e1a44743 3884 if (tries == 5)
5eddb70b 3885 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3886
3887 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3888
8db9d77b
ZW
3889}
3890
0206e353 3891static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3892 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3893 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3894 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3895 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3896};
3897
3898/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3899static void gen6_fdi_link_train(struct intel_crtc *crtc,
3900 const struct intel_crtc_state *crtc_state)
8db9d77b 3901{
4cbe4b2b 3902 struct drm_device *dev = crtc->base.dev;
fac5e23e 3903 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3904 int pipe = crtc->pipe;
f0f59a00
VS
3905 i915_reg_t reg;
3906 u32 temp, i, retry;
8db9d77b 3907
e1a44743
AJ
3908 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3909 for train result */
5eddb70b
CW
3910 reg = FDI_RX_IMR(pipe);
3911 temp = I915_READ(reg);
e1a44743
AJ
3912 temp &= ~FDI_RX_SYMBOL_LOCK;
3913 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3914 I915_WRITE(reg, temp);
3915
3916 POSTING_READ(reg);
e1a44743
AJ
3917 udelay(150);
3918
8db9d77b 3919 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3920 reg = FDI_TX_CTL(pipe);
3921 temp = I915_READ(reg);
627eb5a3 3922 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3923 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3924 temp &= ~FDI_LINK_TRAIN_NONE;
3925 temp |= FDI_LINK_TRAIN_PATTERN_1;
3926 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3927 /* SNB-B */
3928 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3929 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3930
d74cf324
DV
3931 I915_WRITE(FDI_RX_MISC(pipe),
3932 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3933
5eddb70b
CW
3934 reg = FDI_RX_CTL(pipe);
3935 temp = I915_READ(reg);
6e266956 3936 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3937 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3938 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3939 } else {
3940 temp &= ~FDI_LINK_TRAIN_NONE;
3941 temp |= FDI_LINK_TRAIN_PATTERN_1;
3942 }
5eddb70b
CW
3943 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3944
3945 POSTING_READ(reg);
8db9d77b
ZW
3946 udelay(150);
3947
0206e353 3948 for (i = 0; i < 4; i++) {
5eddb70b
CW
3949 reg = FDI_TX_CTL(pipe);
3950 temp = I915_READ(reg);
8db9d77b
ZW
3951 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3952 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3953 I915_WRITE(reg, temp);
3954
3955 POSTING_READ(reg);
8db9d77b
ZW
3956 udelay(500);
3957
fa37d39e
SP
3958 for (retry = 0; retry < 5; retry++) {
3959 reg = FDI_RX_IIR(pipe);
3960 temp = I915_READ(reg);
3961 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3962 if (temp & FDI_RX_BIT_LOCK) {
3963 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3964 DRM_DEBUG_KMS("FDI train 1 done.\n");
3965 break;
3966 }
3967 udelay(50);
8db9d77b 3968 }
fa37d39e
SP
3969 if (retry < 5)
3970 break;
8db9d77b
ZW
3971 }
3972 if (i == 4)
5eddb70b 3973 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3974
3975 /* Train 2 */
5eddb70b
CW
3976 reg = FDI_TX_CTL(pipe);
3977 temp = I915_READ(reg);
8db9d77b
ZW
3978 temp &= ~FDI_LINK_TRAIN_NONE;
3979 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3980 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3981 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3982 /* SNB-B */
3983 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3984 }
5eddb70b 3985 I915_WRITE(reg, temp);
8db9d77b 3986
5eddb70b
CW
3987 reg = FDI_RX_CTL(pipe);
3988 temp = I915_READ(reg);
6e266956 3989 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3990 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3991 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3992 } else {
3993 temp &= ~FDI_LINK_TRAIN_NONE;
3994 temp |= FDI_LINK_TRAIN_PATTERN_2;
3995 }
5eddb70b
CW
3996 I915_WRITE(reg, temp);
3997
3998 POSTING_READ(reg);
8db9d77b
ZW
3999 udelay(150);
4000
0206e353 4001 for (i = 0; i < 4; i++) {
5eddb70b
CW
4002 reg = FDI_TX_CTL(pipe);
4003 temp = I915_READ(reg);
8db9d77b
ZW
4004 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4005 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
4006 I915_WRITE(reg, temp);
4007
4008 POSTING_READ(reg);
8db9d77b
ZW
4009 udelay(500);
4010
fa37d39e
SP
4011 for (retry = 0; retry < 5; retry++) {
4012 reg = FDI_RX_IIR(pipe);
4013 temp = I915_READ(reg);
4014 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4015 if (temp & FDI_RX_SYMBOL_LOCK) {
4016 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4017 DRM_DEBUG_KMS("FDI train 2 done.\n");
4018 break;
4019 }
4020 udelay(50);
8db9d77b 4021 }
fa37d39e
SP
4022 if (retry < 5)
4023 break;
8db9d77b
ZW
4024 }
4025 if (i == 4)
5eddb70b 4026 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4027
4028 DRM_DEBUG_KMS("FDI train done.\n");
4029}
4030
357555c0 4031/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
4032static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4033 const struct intel_crtc_state *crtc_state)
357555c0 4034{
4cbe4b2b 4035 struct drm_device *dev = crtc->base.dev;
fac5e23e 4036 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4037 int pipe = crtc->pipe;
f0f59a00
VS
4038 i915_reg_t reg;
4039 u32 temp, i, j;
357555c0
JB
4040
4041 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4042 for train result */
4043 reg = FDI_RX_IMR(pipe);
4044 temp = I915_READ(reg);
4045 temp &= ~FDI_RX_SYMBOL_LOCK;
4046 temp &= ~FDI_RX_BIT_LOCK;
4047 I915_WRITE(reg, temp);
4048
4049 POSTING_READ(reg);
4050 udelay(150);
4051
01a415fd
DV
4052 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4053 I915_READ(FDI_RX_IIR(pipe)));
4054
139ccd3f
JB
4055 /* Try each vswing and preemphasis setting twice before moving on */
4056 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4057 /* disable first in case we need to retry */
4058 reg = FDI_TX_CTL(pipe);
4059 temp = I915_READ(reg);
4060 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4061 temp &= ~FDI_TX_ENABLE;
4062 I915_WRITE(reg, temp);
357555c0 4063
139ccd3f
JB
4064 reg = FDI_RX_CTL(pipe);
4065 temp = I915_READ(reg);
4066 temp &= ~FDI_LINK_TRAIN_AUTO;
4067 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4068 temp &= ~FDI_RX_ENABLE;
4069 I915_WRITE(reg, temp);
357555c0 4070
139ccd3f 4071 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4072 reg = FDI_TX_CTL(pipe);
4073 temp = I915_READ(reg);
139ccd3f 4074 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4075 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 4076 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4077 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4078 temp |= snb_b_fdi_train_param[j/2];
4079 temp |= FDI_COMPOSITE_SYNC;
4080 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4081
139ccd3f
JB
4082 I915_WRITE(FDI_RX_MISC(pipe),
4083 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4084
139ccd3f 4085 reg = FDI_RX_CTL(pipe);
357555c0 4086 temp = I915_READ(reg);
139ccd3f
JB
4087 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4088 temp |= FDI_COMPOSITE_SYNC;
4089 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4090
139ccd3f
JB
4091 POSTING_READ(reg);
4092 udelay(1); /* should be 0.5us */
357555c0 4093
139ccd3f
JB
4094 for (i = 0; i < 4; i++) {
4095 reg = FDI_RX_IIR(pipe);
4096 temp = I915_READ(reg);
4097 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4098
139ccd3f
JB
4099 if (temp & FDI_RX_BIT_LOCK ||
4100 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4101 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4102 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4103 i);
4104 break;
4105 }
4106 udelay(1); /* should be 0.5us */
4107 }
4108 if (i == 4) {
4109 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4110 continue;
4111 }
357555c0 4112
139ccd3f 4113 /* Train 2 */
357555c0
JB
4114 reg = FDI_TX_CTL(pipe);
4115 temp = I915_READ(reg);
139ccd3f
JB
4116 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4117 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4118 I915_WRITE(reg, temp);
4119
4120 reg = FDI_RX_CTL(pipe);
4121 temp = I915_READ(reg);
4122 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4123 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4124 I915_WRITE(reg, temp);
4125
4126 POSTING_READ(reg);
139ccd3f 4127 udelay(2); /* should be 1.5us */
357555c0 4128
139ccd3f
JB
4129 for (i = 0; i < 4; i++) {
4130 reg = FDI_RX_IIR(pipe);
4131 temp = I915_READ(reg);
4132 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4133
139ccd3f
JB
4134 if (temp & FDI_RX_SYMBOL_LOCK ||
4135 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4136 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4137 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4138 i);
4139 goto train_done;
4140 }
4141 udelay(2); /* should be 1.5us */
357555c0 4142 }
139ccd3f
JB
4143 if (i == 4)
4144 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4145 }
357555c0 4146
139ccd3f 4147train_done:
357555c0
JB
4148 DRM_DEBUG_KMS("FDI train done.\n");
4149}
4150
88cefb6c 4151static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4152{
88cefb6c 4153 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4154 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4155 int pipe = intel_crtc->pipe;
f0f59a00
VS
4156 i915_reg_t reg;
4157 u32 temp;
c64e311e 4158
c98e9dcf 4159 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4160 reg = FDI_RX_CTL(pipe);
4161 temp = I915_READ(reg);
627eb5a3 4162 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4163 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4164 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4165 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4166
4167 POSTING_READ(reg);
c98e9dcf
JB
4168 udelay(200);
4169
4170 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4171 temp = I915_READ(reg);
4172 I915_WRITE(reg, temp | FDI_PCDCLK);
4173
4174 POSTING_READ(reg);
c98e9dcf
JB
4175 udelay(200);
4176
20749730
PZ
4177 /* Enable CPU FDI TX PLL, always on for Ironlake */
4178 reg = FDI_TX_CTL(pipe);
4179 temp = I915_READ(reg);
4180 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4181 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4182
20749730
PZ
4183 POSTING_READ(reg);
4184 udelay(100);
6be4a607 4185 }
0e23b99d
JB
4186}
4187
88cefb6c
DV
4188static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4189{
4190 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4191 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4192 int pipe = intel_crtc->pipe;
f0f59a00
VS
4193 i915_reg_t reg;
4194 u32 temp;
88cefb6c
DV
4195
4196 /* Switch from PCDclk to Rawclk */
4197 reg = FDI_RX_CTL(pipe);
4198 temp = I915_READ(reg);
4199 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4200
4201 /* Disable CPU FDI TX PLL */
4202 reg = FDI_TX_CTL(pipe);
4203 temp = I915_READ(reg);
4204 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4205
4206 POSTING_READ(reg);
4207 udelay(100);
4208
4209 reg = FDI_RX_CTL(pipe);
4210 temp = I915_READ(reg);
4211 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4212
4213 /* Wait for the clocks to turn off. */
4214 POSTING_READ(reg);
4215 udelay(100);
4216}
4217
0fc932b8
JB
4218static void ironlake_fdi_disable(struct drm_crtc *crtc)
4219{
4220 struct drm_device *dev = crtc->dev;
fac5e23e 4221 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4223 int pipe = intel_crtc->pipe;
f0f59a00
VS
4224 i915_reg_t reg;
4225 u32 temp;
0fc932b8
JB
4226
4227 /* disable CPU FDI tx and PCH FDI rx */
4228 reg = FDI_TX_CTL(pipe);
4229 temp = I915_READ(reg);
4230 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4231 POSTING_READ(reg);
4232
4233 reg = FDI_RX_CTL(pipe);
4234 temp = I915_READ(reg);
4235 temp &= ~(0x7 << 16);
dfd07d72 4236 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4237 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4238
4239 POSTING_READ(reg);
4240 udelay(100);
4241
4242 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4243 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4244 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4245
4246 /* still set train pattern 1 */
4247 reg = FDI_TX_CTL(pipe);
4248 temp = I915_READ(reg);
4249 temp &= ~FDI_LINK_TRAIN_NONE;
4250 temp |= FDI_LINK_TRAIN_PATTERN_1;
4251 I915_WRITE(reg, temp);
4252
4253 reg = FDI_RX_CTL(pipe);
4254 temp = I915_READ(reg);
6e266956 4255 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4256 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4257 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4258 } else {
4259 temp &= ~FDI_LINK_TRAIN_NONE;
4260 temp |= FDI_LINK_TRAIN_PATTERN_1;
4261 }
4262 /* BPC in FDI rx is consistent with that in PIPECONF */
4263 temp &= ~(0x07 << 16);
dfd07d72 4264 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4265 I915_WRITE(reg, temp);
4266
4267 POSTING_READ(reg);
4268 udelay(100);
4269}
4270
49d73912 4271bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93 4272{
fa05887a
DV
4273 struct drm_crtc *crtc;
4274 bool cleanup_done;
4275
4276 drm_for_each_crtc(crtc, &dev_priv->drm) {
4277 struct drm_crtc_commit *commit;
4278 spin_lock(&crtc->commit_lock);
4279 commit = list_first_entry_or_null(&crtc->commit_list,
4280 struct drm_crtc_commit, commit_entry);
4281 cleanup_done = commit ?
4282 try_wait_for_completion(&commit->cleanup_done) : true;
4283 spin_unlock(&crtc->commit_lock);
4284
4285 if (cleanup_done)
5dce5b93
CW
4286 continue;
4287
fa05887a 4288 drm_crtc_wait_one_vblank(crtc);
5dce5b93
CW
4289
4290 return true;
4291 }
4292
4293 return false;
4294}
4295
b7076546 4296void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4297{
4298 u32 temp;
4299
4300 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4301
4302 mutex_lock(&dev_priv->sb_lock);
4303
4304 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4305 temp |= SBI_SSCCTL_DISABLE;
4306 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4307
4308 mutex_unlock(&dev_priv->sb_lock);
4309}
4310
e615efe4 4311/* Program iCLKIP clock to the desired frequency */
0dcdc382 4312static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4313{
0dcdc382
ACO
4314 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4315 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4316 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4317 u32 temp;
4318
060f02d8 4319 lpt_disable_iclkip(dev_priv);
e615efe4 4320
64b46a06
VS
4321 /* The iCLK virtual clock root frequency is in MHz,
4322 * but the adjusted_mode->crtc_clock in in KHz. To get the
4323 * divisors, it is necessary to divide one by another, so we
4324 * convert the virtual clock precision to KHz here for higher
4325 * precision.
4326 */
4327 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4328 u32 iclk_virtual_root_freq = 172800 * 1000;
4329 u32 iclk_pi_range = 64;
64b46a06 4330 u32 desired_divisor;
e615efe4 4331
64b46a06
VS
4332 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4333 clock << auxdiv);
4334 divsel = (desired_divisor / iclk_pi_range) - 2;
4335 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4336
64b46a06
VS
4337 /*
4338 * Near 20MHz is a corner case which is
4339 * out of range for the 7-bit divisor
4340 */
4341 if (divsel <= 0x7f)
4342 break;
e615efe4
ED
4343 }
4344
4345 /* This should not happen with any sane values */
4346 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4347 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4348 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4349 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4350
4351 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4352 clock,
e615efe4
ED
4353 auxdiv,
4354 divsel,
4355 phasedir,
4356 phaseinc);
4357
060f02d8
VS
4358 mutex_lock(&dev_priv->sb_lock);
4359
e615efe4 4360 /* Program SSCDIVINTPHASE6 */
988d6ee8 4361 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4362 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4363 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4364 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4365 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4366 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4367 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4368 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4369
4370 /* Program SSCAUXDIV */
988d6ee8 4371 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4372 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4373 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4374 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4375
4376 /* Enable modulator and associated divider */
988d6ee8 4377 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4378 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4379 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4380
060f02d8
VS
4381 mutex_unlock(&dev_priv->sb_lock);
4382
e615efe4
ED
4383 /* Wait for initialization time */
4384 udelay(24);
4385
4386 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4387}
4388
8802e5b6
VS
4389int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4390{
4391 u32 divsel, phaseinc, auxdiv;
4392 u32 iclk_virtual_root_freq = 172800 * 1000;
4393 u32 iclk_pi_range = 64;
4394 u32 desired_divisor;
4395 u32 temp;
4396
4397 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4398 return 0;
4399
4400 mutex_lock(&dev_priv->sb_lock);
4401
4402 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4403 if (temp & SBI_SSCCTL_DISABLE) {
4404 mutex_unlock(&dev_priv->sb_lock);
4405 return 0;
4406 }
4407
4408 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4409 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4410 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4411 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4412 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4413
4414 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4415 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4416 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4417
4418 mutex_unlock(&dev_priv->sb_lock);
4419
4420 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4421
4422 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4423 desired_divisor << auxdiv);
4424}
4425
275f01b2
DV
4426static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4427 enum pipe pch_transcoder)
4428{
4429 struct drm_device *dev = crtc->base.dev;
fac5e23e 4430 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4431 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4432
4433 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4434 I915_READ(HTOTAL(cpu_transcoder)));
4435 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4436 I915_READ(HBLANK(cpu_transcoder)));
4437 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4438 I915_READ(HSYNC(cpu_transcoder)));
4439
4440 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4441 I915_READ(VTOTAL(cpu_transcoder)));
4442 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4443 I915_READ(VBLANK(cpu_transcoder)));
4444 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4445 I915_READ(VSYNC(cpu_transcoder)));
4446 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4447 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4448}
4449
003632d9 4450static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4451{
fac5e23e 4452 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4453 uint32_t temp;
4454
4455 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4456 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4457 return;
4458
4459 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4460 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4461
003632d9
ACO
4462 temp &= ~FDI_BC_BIFURCATION_SELECT;
4463 if (enable)
4464 temp |= FDI_BC_BIFURCATION_SELECT;
4465
4466 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4467 I915_WRITE(SOUTH_CHICKEN1, temp);
4468 POSTING_READ(SOUTH_CHICKEN1);
4469}
4470
4471static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4472{
4473 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4474
4475 switch (intel_crtc->pipe) {
4476 case PIPE_A:
4477 break;
4478 case PIPE_B:
6e3c9717 4479 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4480 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4481 else
003632d9 4482 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4483
4484 break;
4485 case PIPE_C:
003632d9 4486 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4487
4488 break;
4489 default:
4490 BUG();
4491 }
4492}
4493
c48b5305
VS
4494/* Return which DP Port should be selected for Transcoder DP control */
4495static enum port
4cbe4b2b 4496intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4497{
4cbe4b2b 4498 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4499 struct intel_encoder *encoder;
4500
4cbe4b2b 4501 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4502 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4503 encoder->type == INTEL_OUTPUT_EDP)
4504 return enc_to_dig_port(&encoder->base)->port;
4505 }
4506
4507 return -1;
4508}
4509
f67a559d
JB
4510/*
4511 * Enable PCH resources required for PCH ports:
4512 * - PCH PLLs
4513 * - FDI training & RX/TX
4514 * - update transcoder timings
4515 * - DP transcoding bits
4516 * - transcoder
4517 */
2ce42273 4518static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4519{
2ce42273 4520 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4521 struct drm_device *dev = crtc->base.dev;
fac5e23e 4522 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4523 int pipe = crtc->pipe;
f0f59a00 4524 u32 temp;
2c07245f 4525
ab9412ba 4526 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4527
fd6b8f43 4528 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4529 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4530
cd986abb
DV
4531 /* Write the TU size bits before fdi link training, so that error
4532 * detection works. */
4533 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4534 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4535
c98e9dcf 4536 /* For PCH output, training FDI link */
dc4a1094 4537 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4538
3ad8a208
DV
4539 /* We need to program the right clock selection before writing the pixel
4540 * mutliplier into the DPLL. */
6e266956 4541 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4542 u32 sel;
4b645f14 4543
c98e9dcf 4544 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4545 temp |= TRANS_DPLL_ENABLE(pipe);
4546 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4547 if (crtc_state->shared_dpll ==
8106ddbd 4548 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4549 temp |= sel;
4550 else
4551 temp &= ~sel;
c98e9dcf 4552 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4553 }
5eddb70b 4554
3ad8a208
DV
4555 /* XXX: pch pll's can be enabled any time before we enable the PCH
4556 * transcoder, and we actually should do this to not upset any PCH
4557 * transcoder that already use the clock when we share it.
4558 *
4559 * Note that enable_shared_dpll tries to do the right thing, but
4560 * get_shared_dpll unconditionally resets the pll - we need that to have
4561 * the right LVDS enable sequence. */
4cbe4b2b 4562 intel_enable_shared_dpll(crtc);
3ad8a208 4563
d9b6cb56
JB
4564 /* set transcoder timing, panel must allow it */
4565 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4566 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4567
303b81e0 4568 intel_fdi_normal_train(crtc);
5e84e1a4 4569
c98e9dcf 4570 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4571 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4572 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4573 const struct drm_display_mode *adjusted_mode =
2ce42273 4574 &crtc_state->base.adjusted_mode;
dfd07d72 4575 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4576 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4577 temp = I915_READ(reg);
4578 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4579 TRANS_DP_SYNC_MASK |
4580 TRANS_DP_BPC_MASK);
e3ef4479 4581 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4582 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4583
9c4edaee 4584 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4585 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4586 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4587 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4588
4589 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4590 case PORT_B:
5eddb70b 4591 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4592 break;
c48b5305 4593 case PORT_C:
5eddb70b 4594 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4595 break;
c48b5305 4596 case PORT_D:
5eddb70b 4597 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4598 break;
4599 default:
e95d41e1 4600 BUG();
32f9d658 4601 }
2c07245f 4602
5eddb70b 4603 I915_WRITE(reg, temp);
6be4a607 4604 }
b52eb4dc 4605
b8a4f404 4606 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4607}
4608
2ce42273 4609static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4610{
2ce42273 4611 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4612 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4613 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4614
a2196033 4615 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1507e5bd 4616
8c52b5e8 4617 lpt_program_iclkip(crtc);
1507e5bd 4618
0540e488 4619 /* Set transcoder timing. */
0dcdc382 4620 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4621
937bb610 4622 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4623}
4624
a1520318 4625static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4626{
fac5e23e 4627 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4628 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4629 u32 temp;
4630
4631 temp = I915_READ(dslreg);
4632 udelay(500);
4633 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4634 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4635 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4636 }
4637}
4638
86adf9d7
ML
4639static int
4640skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 4641 unsigned int scaler_user, int *scaler_id,
86adf9d7 4642 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4643{
86adf9d7
ML
4644 struct intel_crtc_scaler_state *scaler_state =
4645 &crtc_state->scaler_state;
4646 struct intel_crtc *intel_crtc =
4647 to_intel_crtc(crtc_state->base.crtc);
7f58cbb1
MK
4648 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4649 const struct drm_display_mode *adjusted_mode =
4650 &crtc_state->base.adjusted_mode;
a1b2278e 4651 int need_scaling;
6156a456 4652
d96a7d2a
VS
4653 /*
4654 * Src coordinates are already rotated by 270 degrees for
4655 * the 90/270 degree plane rotation cases (to match the
4656 * GTT mapping), hence no need to account for rotation here.
4657 */
4658 need_scaling = src_w != dst_w || src_h != dst_h;
a1b2278e 4659
e5c05931
SS
4660 if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
4661 need_scaling = true;
4662
7f58cbb1
MK
4663 /*
4664 * Scaling/fitting not supported in IF-ID mode in GEN9+
4665 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4666 * Once NV12 is enabled, handle it here while allocating scaler
4667 * for NV12.
4668 */
4669 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4670 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4671 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4672 return -EINVAL;
4673 }
4674
a1b2278e
CK
4675 /*
4676 * if plane is being disabled or scaler is no more required or force detach
4677 * - free scaler binded to this plane/crtc
4678 * - in order to do this, update crtc->scaler_usage
4679 *
4680 * Here scaler state in crtc_state is set free so that
4681 * scaler can be assigned to other user. Actual register
4682 * update to free the scaler is done in plane/panel-fit programming.
4683 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4684 */
86adf9d7 4685 if (force_detach || !need_scaling) {
a1b2278e 4686 if (*scaler_id >= 0) {
86adf9d7 4687 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4688 scaler_state->scalers[*scaler_id].in_use = 0;
4689
86adf9d7
ML
4690 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4691 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4692 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4693 scaler_state->scaler_users);
4694 *scaler_id = -1;
4695 }
4696 return 0;
4697 }
4698
4699 /* range checks */
4700 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4701 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4702
4703 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4704 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4705 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4706 "size is out of scaler range\n",
86adf9d7 4707 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4708 return -EINVAL;
4709 }
4710
86adf9d7
ML
4711 /* mark this plane as a scaler user in crtc_state */
4712 scaler_state->scaler_users |= (1 << scaler_user);
4713 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4714 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4715 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4716 scaler_state->scaler_users);
4717
4718 return 0;
4719}
4720
4721/**
4722 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4723 *
4724 * @state: crtc's scaler state
86adf9d7
ML
4725 *
4726 * Return
4727 * 0 - scaler_usage updated successfully
4728 * error - requested scaling cannot be supported or other error condition
4729 */
e435d6e5 4730int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4731{
7c5f93b0 4732 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4733
e435d6e5 4734 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
d96a7d2a 4735 &state->scaler_state.scaler_id,
86adf9d7 4736 state->pipe_src_w, state->pipe_src_h,
aad941d5 4737 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4738}
4739
4740/**
4741 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4742 *
4743 * @state: crtc's scaler state
86adf9d7
ML
4744 * @plane_state: atomic plane state to update
4745 *
4746 * Return
4747 * 0 - scaler_usage updated successfully
4748 * error - requested scaling cannot be supported or other error condition
4749 */
da20eabd
ML
4750static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4751 struct intel_plane_state *plane_state)
86adf9d7
ML
4752{
4753
da20eabd
ML
4754 struct intel_plane *intel_plane =
4755 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4756 struct drm_framebuffer *fb = plane_state->base.fb;
4757 int ret;
4758
936e71e3 4759 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4760
86adf9d7
ML
4761 ret = skl_update_scaler(crtc_state, force_detach,
4762 drm_plane_index(&intel_plane->base),
4763 &plane_state->scaler_id,
936e71e3
VS
4764 drm_rect_width(&plane_state->base.src) >> 16,
4765 drm_rect_height(&plane_state->base.src) >> 16,
4766 drm_rect_width(&plane_state->base.dst),
4767 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4768
4769 if (ret || plane_state->scaler_id < 0)
4770 return ret;
4771
a1b2278e 4772 /* check colorkey */
818ed961 4773 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4774 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4775 intel_plane->base.base.id,
4776 intel_plane->base.name);
a1b2278e
CK
4777 return -EINVAL;
4778 }
4779
4780 /* Check src format */
438b74a5 4781 switch (fb->format->format) {
86adf9d7
ML
4782 case DRM_FORMAT_RGB565:
4783 case DRM_FORMAT_XBGR8888:
4784 case DRM_FORMAT_XRGB8888:
4785 case DRM_FORMAT_ABGR8888:
4786 case DRM_FORMAT_ARGB8888:
4787 case DRM_FORMAT_XRGB2101010:
4788 case DRM_FORMAT_XBGR2101010:
4789 case DRM_FORMAT_YUYV:
4790 case DRM_FORMAT_YVYU:
4791 case DRM_FORMAT_UYVY:
4792 case DRM_FORMAT_VYUY:
4793 break;
4794 default:
72660ce0
VS
4795 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4796 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4797 fb->base.id, fb->format->format);
86adf9d7 4798 return -EINVAL;
a1b2278e
CK
4799 }
4800
a1b2278e
CK
4801 return 0;
4802}
4803
e435d6e5
ML
4804static void skylake_scaler_disable(struct intel_crtc *crtc)
4805{
4806 int i;
4807
4808 for (i = 0; i < crtc->num_scalers; i++)
4809 skl_detach_scaler(crtc, i);
4810}
4811
4812static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4813{
4814 struct drm_device *dev = crtc->base.dev;
fac5e23e 4815 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4816 int pipe = crtc->pipe;
a1b2278e
CK
4817 struct intel_crtc_scaler_state *scaler_state =
4818 &crtc->config->scaler_state;
4819
6e3c9717 4820 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4821 int id;
4822
c3f8ad57 4823 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 4824 return;
a1b2278e
CK
4825
4826 id = scaler_state->scaler_id;
4827 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4828 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4829 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4830 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
4831 }
4832}
4833
b074cec8
JB
4834static void ironlake_pfit_enable(struct intel_crtc *crtc)
4835{
4836 struct drm_device *dev = crtc->base.dev;
fac5e23e 4837 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4838 int pipe = crtc->pipe;
4839
6e3c9717 4840 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4841 /* Force use of hard-coded filter coefficients
4842 * as some pre-programmed values are broken,
4843 * e.g. x201.
4844 */
fd6b8f43 4845 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4846 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4847 PF_PIPE_SEL_IVB(pipe));
4848 else
4849 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4850 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4851 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4852 }
4853}
4854
20bc8673 4855void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4856{
cea165c3 4857 struct drm_device *dev = crtc->base.dev;
fac5e23e 4858 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4859
6e3c9717 4860 if (!crtc->config->ips_enabled)
d77e4531
PZ
4861 return;
4862
307e4498
ML
4863 /*
4864 * We can only enable IPS after we enable a plane and wait for a vblank
4865 * This function is called from post_plane_update, which is run after
4866 * a vblank wait.
4867 */
cea165c3 4868
d77e4531 4869 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4870 if (IS_BROADWELL(dev_priv)) {
9f817501 4871 mutex_lock(&dev_priv->pcu_lock);
61843f0e
VS
4872 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
4873 IPS_ENABLE | IPS_PCODE_CONTROL));
9f817501 4874 mutex_unlock(&dev_priv->pcu_lock);
2a114cc1
BW
4875 /* Quoting Art Runyan: "its not safe to expect any particular
4876 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4877 * mailbox." Moreover, the mailbox may return a bogus state,
4878 * so we need to just enable it and continue on.
2a114cc1
BW
4879 */
4880 } else {
4881 I915_WRITE(IPS_CTL, IPS_ENABLE);
4882 /* The bit only becomes 1 in the next vblank, so this wait here
4883 * is essentially intel_wait_for_vblank. If we don't have this
4884 * and don't wait for vblanks until the end of crtc_enable, then
4885 * the HW state readout code will complain that the expected
4886 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4887 if (intel_wait_for_register(dev_priv,
4888 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4889 50))
2a114cc1
BW
4890 DRM_ERROR("Timed out waiting for IPS enable\n");
4891 }
d77e4531
PZ
4892}
4893
20bc8673 4894void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4895{
4896 struct drm_device *dev = crtc->base.dev;
fac5e23e 4897 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4898
6e3c9717 4899 if (!crtc->config->ips_enabled)
d77e4531
PZ
4900 return;
4901
4902 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4903 if (IS_BROADWELL(dev_priv)) {
9f817501 4904 mutex_lock(&dev_priv->pcu_lock);
2a114cc1 4905 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
9f817501 4906 mutex_unlock(&dev_priv->pcu_lock);
23d0b130 4907 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4908 if (intel_wait_for_register(dev_priv,
4909 IPS_CTL, IPS_ENABLE, 0,
4910 42))
23d0b130 4911 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4912 } else {
2a114cc1 4913 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4914 POSTING_READ(IPS_CTL);
4915 }
d77e4531
PZ
4916
4917 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4918 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4919}
4920
7cac945f 4921static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4922{
7cac945f 4923 if (intel_crtc->overlay) {
d3eedb1a 4924 struct drm_device *dev = intel_crtc->base.dev;
d3eedb1a
VS
4925
4926 mutex_lock(&dev->struct_mutex);
d3eedb1a 4927 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
4928 mutex_unlock(&dev->struct_mutex);
4929 }
4930
4931 /* Let userspace switch the overlay on again. In most cases userspace
4932 * has to recompute where to put it anyway.
4933 */
4934}
4935
87d4300a
ML
4936/**
4937 * intel_post_enable_primary - Perform operations after enabling primary plane
4938 * @crtc: the CRTC whose primary plane was just enabled
4939 *
4940 * Performs potentially sleeping operations that must be done after the primary
4941 * plane is enabled, such as updating FBC and IPS. Note that this may be
4942 * called due to an explicit primary plane update, or due to an implicit
4943 * re-enable that is caused when a sprite plane is updated to no longer
4944 * completely hide the primary plane.
4945 */
4946static void
4947intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4948{
4949 struct drm_device *dev = crtc->dev;
fac5e23e 4950 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4952 int pipe = intel_crtc->pipe;
a5c4d7bc 4953
87d4300a
ML
4954 /*
4955 * FIXME IPS should be fine as long as one plane is
4956 * enabled, but in practice it seems to have problems
4957 * when going from primary only to sprite only and vice
4958 * versa.
4959 */
a5c4d7bc
VS
4960 hsw_enable_ips(intel_crtc);
4961
f99d7069 4962 /*
87d4300a
ML
4963 * Gen2 reports pipe underruns whenever all planes are disabled.
4964 * So don't enable underrun reporting before at least some planes
4965 * are enabled.
4966 * FIXME: Need to fix the logic to work when we turn off all planes
4967 * but leave the pipe running.
f99d7069 4968 */
5db94019 4969 if (IS_GEN2(dev_priv))
87d4300a
ML
4970 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4971
aca7b684
VS
4972 /* Underruns don't always raise interrupts, so check manually. */
4973 intel_check_cpu_fifo_underruns(dev_priv);
4974 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4975}
4976
2622a081 4977/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4978static void
4979intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4980{
4981 struct drm_device *dev = crtc->dev;
fac5e23e 4982 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 int pipe = intel_crtc->pipe;
a5c4d7bc 4985
87d4300a
ML
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
5db94019 4992 if (IS_GEN2(dev_priv))
87d4300a 4993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4994
2622a081
VS
4995 /*
4996 * FIXME IPS should be fine as long as one plane is
4997 * enabled, but in practice it seems to have problems
4998 * when going from primary only to sprite only and vice
4999 * versa.
5000 */
5001 hsw_disable_ips(intel_crtc);
5002}
5003
5004/* FIXME get rid of this and use pre_plane_update */
5005static void
5006intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
fac5e23e 5009 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 int pipe = intel_crtc->pipe;
5012
5013 intel_pre_disable_primary(crtc);
5014
87d4300a
ML
5015 /*
5016 * Vblank time updates from the shadow to live plane control register
5017 * are blocked if the memory self-refresh mode is active at that
5018 * moment. So to make sure the plane gets truly disabled, disable
5019 * first the self-refresh mode. The self-refresh enable bit in turn
5020 * will be checked/applied by the HW only at the next frame start
5021 * event which is after the vblank start event, so we need to have a
5022 * wait-for-vblank between disabling the plane and the pipe.
5023 */
11a85d6a
VS
5024 if (HAS_GMCH_DISPLAY(dev_priv) &&
5025 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5026 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
5027}
5028
5a21b665
DV
5029static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5030{
5031 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5032 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5033 struct intel_crtc_state *pipe_config =
f9a8c149
VS
5034 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5035 crtc);
5a21b665
DV
5036 struct drm_plane *primary = crtc->base.primary;
5037 struct drm_plane_state *old_pri_state =
5038 drm_atomic_get_existing_plane_state(old_state, primary);
5039
5748b6a1 5040 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 5041
5a21b665 5042 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5043 intel_update_watermarks(crtc);
5a21b665
DV
5044
5045 if (old_pri_state) {
5046 struct intel_plane_state *primary_state =
f9a8c149
VS
5047 intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
5048 to_intel_plane(primary));
5a21b665
DV
5049 struct intel_plane_state *old_primary_state =
5050 to_intel_plane_state(old_pri_state);
5051
5052 intel_fbc_post_update(crtc);
5053
936e71e3 5054 if (primary_state->base.visible &&
5a21b665 5055 (needs_modeset(&pipe_config->base) ||
936e71e3 5056 !old_primary_state->base.visible))
5a21b665
DV
5057 intel_post_enable_primary(&crtc->base);
5058 }
5059}
5060
aa5e9b47
ML
5061static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5062 struct intel_crtc_state *pipe_config)
ac21b225 5063{
5c74cd73 5064 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5065 struct drm_device *dev = crtc->base.dev;
fac5e23e 5066 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5067 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5068 struct drm_plane *primary = crtc->base.primary;
5069 struct drm_plane_state *old_pri_state =
5070 drm_atomic_get_existing_plane_state(old_state, primary);
5071 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5072 struct intel_atomic_state *old_intel_state =
5073 to_intel_atomic_state(old_state);
ac21b225 5074
5c74cd73
ML
5075 if (old_pri_state) {
5076 struct intel_plane_state *primary_state =
f9a8c149
VS
5077 intel_atomic_get_new_plane_state(old_intel_state,
5078 to_intel_plane(primary));
5c74cd73
ML
5079 struct intel_plane_state *old_primary_state =
5080 to_intel_plane_state(old_pri_state);
5081
faf68d92 5082 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5083
936e71e3
VS
5084 if (old_primary_state->base.visible &&
5085 (modeset || !primary_state->base.visible))
5c74cd73
ML
5086 intel_pre_disable_primary(&crtc->base);
5087 }
852eb00d 5088
5eeb798b
VS
5089 /*
5090 * Vblank time updates from the shadow to live plane control register
5091 * are blocked if the memory self-refresh mode is active at that
5092 * moment. So to make sure the plane gets truly disabled, disable
5093 * first the self-refresh mode. The self-refresh enable bit in turn
5094 * will be checked/applied by the HW only at the next frame start
5095 * event which is after the vblank start event, so we need to have a
5096 * wait-for-vblank between disabling the plane and the pipe.
5097 */
5098 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5099 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5100 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5101
ed4a6a7c
MR
5102 /*
5103 * IVB workaround: must disable low power watermarks for at least
5104 * one frame before enabling scaling. LP watermarks can be re-enabled
5105 * when scaling is disabled.
5106 *
5107 * WaCxSRDisabledForSpriteScaling:ivb
5108 */
ddd2b792 5109 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5110 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5111
5112 /*
5113 * If we're doing a modeset, we're done. No need to do any pre-vblank
5114 * watermark programming here.
5115 */
5116 if (needs_modeset(&pipe_config->base))
5117 return;
5118
5119 /*
5120 * For platforms that support atomic watermarks, program the
5121 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5122 * will be the intermediate values that are safe for both pre- and
5123 * post- vblank; when vblank happens, the 'active' values will be set
5124 * to the final 'target' values and we'll do this again to get the
5125 * optimal watermarks. For gen9+ platforms, the values we program here
5126 * will be the final target values which will get automatically latched
5127 * at vblank time; no further programming will be necessary.
5128 *
5129 * If a platform hasn't been transitioned to atomic watermarks yet,
5130 * we'll continue to update watermarks the old way, if flags tell
5131 * us to.
5132 */
5133 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5134 dev_priv->display.initial_watermarks(old_intel_state,
5135 pipe_config);
caed361d 5136 else if (pipe_config->update_wm_pre)
432081bc 5137 intel_update_watermarks(crtc);
ac21b225
ML
5138}
5139
d032ffa0 5140static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5141{
5142 struct drm_device *dev = crtc->dev;
5143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5144 struct drm_plane *p;
87d4300a
ML
5145 int pipe = intel_crtc->pipe;
5146
7cac945f 5147 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5148
d032ffa0 5149 drm_for_each_plane_mask(p, dev, plane_mask)
282dbf9b 5150 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
f98551ae 5151
f99d7069
DV
5152 /*
5153 * FIXME: Once we grow proper nuclear flip support out of this we need
5154 * to compute the mask of flip planes precisely. For the time being
5155 * consider this a flip to a NULL plane.
5156 */
5748b6a1 5157 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5158}
5159
fb1c98b1 5160static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5161 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5162 struct drm_atomic_state *old_state)
5163{
aa5e9b47 5164 struct drm_connector_state *conn_state;
fb1c98b1
ML
5165 struct drm_connector *conn;
5166 int i;
5167
aa5e9b47 5168 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5169 struct intel_encoder *encoder =
5170 to_intel_encoder(conn_state->best_encoder);
5171
5172 if (conn_state->crtc != crtc)
5173 continue;
5174
5175 if (encoder->pre_pll_enable)
fd6bbda9 5176 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5177 }
5178}
5179
5180static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5181 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5182 struct drm_atomic_state *old_state)
5183{
aa5e9b47 5184 struct drm_connector_state *conn_state;
fb1c98b1
ML
5185 struct drm_connector *conn;
5186 int i;
5187
aa5e9b47 5188 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5189 struct intel_encoder *encoder =
5190 to_intel_encoder(conn_state->best_encoder);
5191
5192 if (conn_state->crtc != crtc)
5193 continue;
5194
5195 if (encoder->pre_enable)
fd6bbda9 5196 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5197 }
5198}
5199
5200static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5201 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5202 struct drm_atomic_state *old_state)
5203{
aa5e9b47 5204 struct drm_connector_state *conn_state;
fb1c98b1
ML
5205 struct drm_connector *conn;
5206 int i;
5207
aa5e9b47 5208 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5209 struct intel_encoder *encoder =
5210 to_intel_encoder(conn_state->best_encoder);
5211
5212 if (conn_state->crtc != crtc)
5213 continue;
5214
fd6bbda9 5215 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5216 intel_opregion_notify_encoder(encoder, true);
5217 }
5218}
5219
5220static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5221 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5222 struct drm_atomic_state *old_state)
5223{
5224 struct drm_connector_state *old_conn_state;
5225 struct drm_connector *conn;
5226 int i;
5227
aa5e9b47 5228 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5229 struct intel_encoder *encoder =
5230 to_intel_encoder(old_conn_state->best_encoder);
5231
5232 if (old_conn_state->crtc != crtc)
5233 continue;
5234
5235 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5236 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5237 }
5238}
5239
5240static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5241 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5242 struct drm_atomic_state *old_state)
5243{
5244 struct drm_connector_state *old_conn_state;
5245 struct drm_connector *conn;
5246 int i;
5247
aa5e9b47 5248 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5249 struct intel_encoder *encoder =
5250 to_intel_encoder(old_conn_state->best_encoder);
5251
5252 if (old_conn_state->crtc != crtc)
5253 continue;
5254
5255 if (encoder->post_disable)
fd6bbda9 5256 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5257 }
5258}
5259
5260static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5261 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5262 struct drm_atomic_state *old_state)
5263{
5264 struct drm_connector_state *old_conn_state;
5265 struct drm_connector *conn;
5266 int i;
5267
aa5e9b47 5268 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5269 struct intel_encoder *encoder =
5270 to_intel_encoder(old_conn_state->best_encoder);
5271
5272 if (old_conn_state->crtc != crtc)
5273 continue;
5274
5275 if (encoder->post_pll_disable)
fd6bbda9 5276 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5277 }
5278}
5279
4a806558
ML
5280static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5281 struct drm_atomic_state *old_state)
f67a559d 5282{
4a806558 5283 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5284 struct drm_device *dev = crtc->dev;
fac5e23e 5285 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5287 int pipe = intel_crtc->pipe;
ccf010fb
ML
5288 struct intel_atomic_state *old_intel_state =
5289 to_intel_atomic_state(old_state);
f67a559d 5290
53d9f4e9 5291 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5292 return;
5293
b2c0593a
VS
5294 /*
5295 * Sometimes spurious CPU pipe underruns happen during FDI
5296 * training, at least with VGA+HDMI cloning. Suppress them.
5297 *
5298 * On ILK we get an occasional spurious CPU pipe underruns
5299 * between eDP port A enable and vdd enable. Also PCH port
5300 * enable seems to result in the occasional CPU pipe underrun.
5301 *
5302 * Spurious PCH underruns also occur during PCH enabling.
5303 */
5304 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5305 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5306 if (intel_crtc->config->has_pch_encoder)
5307 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5308
6e3c9717 5309 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5310 intel_prepare_shared_dpll(intel_crtc);
5311
37a5650b 5312 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5313 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5314
5315 intel_set_pipe_timings(intel_crtc);
bc58be60 5316 intel_set_pipe_src_size(intel_crtc);
29407aab 5317
6e3c9717 5318 if (intel_crtc->config->has_pch_encoder) {
29407aab 5319 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5320 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5321 }
5322
5323 ironlake_set_pipeconf(crtc);
5324
f67a559d 5325 intel_crtc->active = true;
8664281b 5326
fd6bbda9 5327 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5328
6e3c9717 5329 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5330 /* Note: FDI PLL enabling _must_ be done before we enable the
5331 * cpu pipes, hence this is separate from all the other fdi/pch
5332 * enabling. */
88cefb6c 5333 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5334 } else {
5335 assert_fdi_tx_disabled(dev_priv, pipe);
5336 assert_fdi_rx_disabled(dev_priv, pipe);
5337 }
f67a559d 5338
b074cec8 5339 ironlake_pfit_enable(intel_crtc);
f67a559d 5340
9c54c0dd
JB
5341 /*
5342 * On ILK+ LUT must be loaded before the pipe is running but with
5343 * clocks enabled
5344 */
b95c5321 5345 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5346
1d5bf5d9 5347 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5348 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5349 intel_enable_pipe(intel_crtc);
f67a559d 5350
6e3c9717 5351 if (intel_crtc->config->has_pch_encoder)
2ce42273 5352 ironlake_pch_enable(pipe_config);
c98e9dcf 5353
f9b61ff6
DV
5354 assert_vblank_disabled(crtc);
5355 drm_crtc_vblank_on(crtc);
5356
fd6bbda9 5357 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5358
6e266956 5359 if (HAS_PCH_CPT(dev_priv))
a1520318 5360 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5361
5362 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5363 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5364 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5365 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5366 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5367}
5368
42db64ef
PZ
5369/* IPS only exists on ULT machines and is tied to pipe A. */
5370static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5371{
50a0bc90 5372 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5373}
5374
ed69cd40
ID
5375static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5376 enum pipe pipe, bool apply)
5377{
5378 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5379 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5380
5381 if (apply)
5382 val |= mask;
5383 else
5384 val &= ~mask;
5385
5386 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5387}
5388
4a806558
ML
5389static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5390 struct drm_atomic_state *old_state)
4f771f10 5391{
4a806558 5392 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5393 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5395 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5396 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5397 struct intel_atomic_state *old_intel_state =
5398 to_intel_atomic_state(old_state);
ed69cd40 5399 bool psl_clkgate_wa;
4f771f10 5400
53d9f4e9 5401 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5402 return;
5403
fd6bbda9 5404 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5405
8106ddbd 5406 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5407 intel_enable_shared_dpll(intel_crtc);
5408
37a5650b 5409 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5410 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5411
d7edc4e5 5412 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5413 intel_set_pipe_timings(intel_crtc);
5414
bc58be60 5415 intel_set_pipe_src_size(intel_crtc);
229fca97 5416
4d1de975
JN
5417 if (cpu_transcoder != TRANSCODER_EDP &&
5418 !transcoder_is_dsi(cpu_transcoder)) {
5419 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5420 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5421 }
5422
6e3c9717 5423 if (intel_crtc->config->has_pch_encoder) {
229fca97 5424 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5425 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5426 }
5427
d7edc4e5 5428 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5429 haswell_set_pipeconf(crtc);
5430
391bf048 5431 haswell_set_pipemisc(crtc);
229fca97 5432
b95c5321 5433 intel_color_set_csc(&pipe_config->base);
229fca97 5434
4f771f10 5435 intel_crtc->active = true;
8664281b 5436
fd6bbda9 5437 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5438
d7edc4e5 5439 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5440 intel_ddi_enable_pipe_clock(pipe_config);
4f771f10 5441
ed69cd40
ID
5442 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5443 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
5444 intel_crtc->config->pch_pfit.enabled;
5445 if (psl_clkgate_wa)
5446 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5447
6315b5d3 5448 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5449 skylake_pfit_enable(intel_crtc);
ff6d9f55 5450 else
1c132b44 5451 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5452
5453 /*
5454 * On ILK+ LUT must be loaded before the pipe is running but with
5455 * clocks enabled
5456 */
b95c5321 5457 intel_color_load_luts(&pipe_config->base);
4f771f10 5458
3dc38eea 5459 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5460 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5461 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5462
1d5bf5d9 5463 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5464 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5465
5466 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5467 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5468 intel_enable_pipe(intel_crtc);
42db64ef 5469
6e3c9717 5470 if (intel_crtc->config->has_pch_encoder)
2ce42273 5471 lpt_pch_enable(pipe_config);
4f771f10 5472
0037071d 5473 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5474 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5475
f9b61ff6
DV
5476 assert_vblank_disabled(crtc);
5477 drm_crtc_vblank_on(crtc);
5478
fd6bbda9 5479 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5480
ed69cd40 5481 if (psl_clkgate_wa) {
0f0f74bc 5482 intel_wait_for_vblank(dev_priv, pipe);
ed69cd40 5483 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
6b698516 5484 }
d2d65408 5485
e4916946
PZ
5486 /* If we change the relative order between pipe/planes enabling, we need
5487 * to change the workaround. */
99d736a2 5488 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5489 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5490 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5491 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5492 }
4f771f10
PZ
5493}
5494
bfd16b2a 5495static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5496{
5497 struct drm_device *dev = crtc->base.dev;
fac5e23e 5498 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5499 int pipe = crtc->pipe;
5500
5501 /* To avoid upsetting the power well on haswell only disable the pfit if
5502 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5503 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5504 I915_WRITE(PF_CTL(pipe), 0);
5505 I915_WRITE(PF_WIN_POS(pipe), 0);
5506 I915_WRITE(PF_WIN_SZ(pipe), 0);
5507 }
5508}
5509
4a806558
ML
5510static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5511 struct drm_atomic_state *old_state)
6be4a607 5512{
4a806558 5513 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5514 struct drm_device *dev = crtc->dev;
fac5e23e 5515 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5517 int pipe = intel_crtc->pipe;
b52eb4dc 5518
b2c0593a
VS
5519 /*
5520 * Sometimes spurious CPU pipe underruns happen when the
5521 * pipe is already disabled, but FDI RX/TX is still enabled.
5522 * Happens at least with VGA+HDMI cloning. Suppress them.
5523 */
5524 if (intel_crtc->config->has_pch_encoder) {
5525 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5526 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5527 }
37ca8d4c 5528
fd6bbda9 5529 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5530
f9b61ff6
DV
5531 drm_crtc_vblank_off(crtc);
5532 assert_vblank_disabled(crtc);
5533
575f7ab7 5534 intel_disable_pipe(intel_crtc);
32f9d658 5535
bfd16b2a 5536 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5537
b2c0593a 5538 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5539 ironlake_fdi_disable(crtc);
5540
fd6bbda9 5541 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5542
6e3c9717 5543 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5544 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5545
6e266956 5546 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5547 i915_reg_t reg;
5548 u32 temp;
5549
d925c59a
DV
5550 /* disable TRANS_DP_CTL */
5551 reg = TRANS_DP_CTL(pipe);
5552 temp = I915_READ(reg);
5553 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5554 TRANS_DP_PORT_SEL_MASK);
5555 temp |= TRANS_DP_PORT_SEL_NONE;
5556 I915_WRITE(reg, temp);
5557
5558 /* disable DPLL_SEL */
5559 temp = I915_READ(PCH_DPLL_SEL);
11887397 5560 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5561 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5562 }
e3421a18 5563
d925c59a
DV
5564 ironlake_fdi_pll_disable(intel_crtc);
5565 }
81b088ca 5566
b2c0593a 5567 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5568 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5569}
1b3c7a47 5570
4a806558
ML
5571static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5572 struct drm_atomic_state *old_state)
ee7b9f93 5573{
4a806558 5574 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5575 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5577 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5578
fd6bbda9 5579 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5580
f9b61ff6
DV
5581 drm_crtc_vblank_off(crtc);
5582 assert_vblank_disabled(crtc);
5583
4d1de975 5584 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5585 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5586 intel_disable_pipe(intel_crtc);
4f771f10 5587
0037071d 5588 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5589 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
a4bf214f 5590
d7edc4e5 5591 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5592 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5593
6315b5d3 5594 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5595 skylake_scaler_disable(intel_crtc);
ff6d9f55 5596 else
bfd16b2a 5597 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5598
d7edc4e5 5599 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5600 intel_ddi_disable_pipe_clock(intel_crtc->config);
4f771f10 5601
fd6bbda9 5602 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
4f771f10
PZ
5603}
5604
2dd24552
JB
5605static void i9xx_pfit_enable(struct intel_crtc *crtc)
5606{
5607 struct drm_device *dev = crtc->base.dev;
fac5e23e 5608 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5609 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5610
681a8504 5611 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5612 return;
5613
2dd24552 5614 /*
c0b03411
DV
5615 * The panel fitter should only be adjusted whilst the pipe is disabled,
5616 * according to register description and PRM.
2dd24552 5617 */
c0b03411
DV
5618 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5619 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5620
b074cec8
JB
5621 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5622 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5623
5624 /* Border color in case we don't scale up to the full screen. Black by
5625 * default, change to something else for debugging. */
5626 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5627}
5628
79f255a0 5629enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5630{
5631 switch (port) {
5632 case PORT_A:
6331a704 5633 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5634 case PORT_B:
6331a704 5635 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5636 case PORT_C:
6331a704 5637 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5638 case PORT_D:
6331a704 5639 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5640 case PORT_E:
6331a704 5641 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5642 default:
b9fec167 5643 MISSING_CASE(port);
d05410f9
DA
5644 return POWER_DOMAIN_PORT_OTHER;
5645 }
5646}
5647
d8fc70b7
ACO
5648static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5649 struct intel_crtc_state *crtc_state)
77d22dca 5650{
319be8ae 5651 struct drm_device *dev = crtc->dev;
37255d8d 5652 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5653 struct drm_encoder *encoder;
319be8ae
ID
5654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5655 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5656 u64 mask;
74bff5f9 5657 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5658
74bff5f9 5659 if (!crtc_state->base.active)
292b990e
ML
5660 return 0;
5661
77d22dca
ID
5662 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5663 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5664 if (crtc_state->pch_pfit.enabled ||
5665 crtc_state->pch_pfit.force_thru)
d8fc70b7 5666 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5667
74bff5f9
ML
5668 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5669 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5670
79f255a0 5671 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5672 }
319be8ae 5673
37255d8d
ML
5674 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5675 mask |= BIT(POWER_DOMAIN_AUDIO);
5676
15e7ec29 5677 if (crtc_state->shared_dpll)
d8fc70b7 5678 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5679
77d22dca
ID
5680 return mask;
5681}
5682
d2d15016 5683static u64
74bff5f9
ML
5684modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5685 struct intel_crtc_state *crtc_state)
77d22dca 5686{
fac5e23e 5687 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5689 enum intel_display_power_domain domain;
d8fc70b7 5690 u64 domains, new_domains, old_domains;
77d22dca 5691
292b990e 5692 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5693 intel_crtc->enabled_power_domains = new_domains =
5694 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5695
5a21b665 5696 domains = new_domains & ~old_domains;
292b990e
ML
5697
5698 for_each_power_domain(domain, domains)
5699 intel_display_power_get(dev_priv, domain);
5700
5a21b665 5701 return old_domains & ~new_domains;
292b990e
ML
5702}
5703
5704static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5705 u64 domains)
292b990e
ML
5706{
5707 enum intel_display_power_domain domain;
5708
5709 for_each_power_domain(domain, domains)
5710 intel_display_power_put(dev_priv, domain);
5711}
77d22dca 5712
7ff89ca2
VS
5713static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5714 struct drm_atomic_state *old_state)
adafdc6f 5715{
ff32c54e
VS
5716 struct intel_atomic_state *old_intel_state =
5717 to_intel_atomic_state(old_state);
7ff89ca2
VS
5718 struct drm_crtc *crtc = pipe_config->base.crtc;
5719 struct drm_device *dev = crtc->dev;
5720 struct drm_i915_private *dev_priv = to_i915(dev);
5721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5722 int pipe = intel_crtc->pipe;
adafdc6f 5723
7ff89ca2
VS
5724 if (WARN_ON(intel_crtc->active))
5725 return;
adafdc6f 5726
7ff89ca2
VS
5727 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5728 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5729
7ff89ca2
VS
5730 intel_set_pipe_timings(intel_crtc);
5731 intel_set_pipe_src_size(intel_crtc);
b2045352 5732
7ff89ca2
VS
5733 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5734 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5735
7ff89ca2
VS
5736 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5737 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5738 }
5739
7ff89ca2 5740 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5741
7ff89ca2 5742 intel_crtc->active = true;
92891e45 5743
7ff89ca2 5744 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5745
7ff89ca2 5746 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5747
7ff89ca2
VS
5748 if (IS_CHERRYVIEW(dev_priv)) {
5749 chv_prepare_pll(intel_crtc, intel_crtc->config);
5750 chv_enable_pll(intel_crtc, intel_crtc->config);
5751 } else {
5752 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5753 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5754 }
5755
7ff89ca2 5756 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5757
7ff89ca2 5758 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5759
7ff89ca2 5760 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5761
ff32c54e
VS
5762 dev_priv->display.initial_watermarks(old_intel_state,
5763 pipe_config);
7ff89ca2
VS
5764 intel_enable_pipe(intel_crtc);
5765
5766 assert_vblank_disabled(crtc);
5767 drm_crtc_vblank_on(crtc);
89b3c3c7 5768
7ff89ca2 5769 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5770}
5771
7ff89ca2 5772static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5773{
7ff89ca2
VS
5774 struct drm_device *dev = crtc->base.dev;
5775 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5776
7ff89ca2
VS
5777 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5778 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5779}
5780
7ff89ca2
VS
5781static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5782 struct drm_atomic_state *old_state)
2b73001e 5783{
04548cba
VS
5784 struct intel_atomic_state *old_intel_state =
5785 to_intel_atomic_state(old_state);
7ff89ca2
VS
5786 struct drm_crtc *crtc = pipe_config->base.crtc;
5787 struct drm_device *dev = crtc->dev;
5788 struct drm_i915_private *dev_priv = to_i915(dev);
5789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5790 enum pipe pipe = intel_crtc->pipe;
2b73001e 5791
7ff89ca2
VS
5792 if (WARN_ON(intel_crtc->active))
5793 return;
2b73001e 5794
7ff89ca2 5795 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5796
7ff89ca2
VS
5797 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5798 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5799
7ff89ca2
VS
5800 intel_set_pipe_timings(intel_crtc);
5801 intel_set_pipe_src_size(intel_crtc);
2b73001e 5802
7ff89ca2 5803 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5804
7ff89ca2 5805 intel_crtc->active = true;
5f199dfa 5806
7ff89ca2
VS
5807 if (!IS_GEN2(dev_priv))
5808 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5809
7ff89ca2 5810 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5811
939994da 5812 i9xx_enable_pll(intel_crtc, pipe_config);
f8437dd1 5813
7ff89ca2 5814 i9xx_pfit_enable(intel_crtc);
f8437dd1 5815
7ff89ca2 5816 intel_color_load_luts(&pipe_config->base);
f8437dd1 5817
04548cba
VS
5818 if (dev_priv->display.initial_watermarks != NULL)
5819 dev_priv->display.initial_watermarks(old_intel_state,
5820 intel_crtc->config);
5821 else
5822 intel_update_watermarks(intel_crtc);
7ff89ca2 5823 intel_enable_pipe(intel_crtc);
f8437dd1 5824
7ff89ca2
VS
5825 assert_vblank_disabled(crtc);
5826 drm_crtc_vblank_on(crtc);
f8437dd1 5827
7ff89ca2
VS
5828 intel_encoders_enable(crtc, pipe_config, old_state);
5829}
f8437dd1 5830
7ff89ca2
VS
5831static void i9xx_pfit_disable(struct intel_crtc *crtc)
5832{
5833 struct drm_device *dev = crtc->base.dev;
5834 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5835
7ff89ca2 5836 if (!crtc->config->gmch_pfit.control)
f8437dd1 5837 return;
f8437dd1 5838
7ff89ca2
VS
5839 assert_pipe_disabled(dev_priv, crtc->pipe);
5840
5841 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5842 I915_READ(PFIT_CONTROL));
5843 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5844}
5845
7ff89ca2
VS
5846static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5847 struct drm_atomic_state *old_state)
f8437dd1 5848{
7ff89ca2
VS
5849 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5850 struct drm_device *dev = crtc->dev;
5851 struct drm_i915_private *dev_priv = to_i915(dev);
5852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5853 int pipe = intel_crtc->pipe;
d66a2194 5854
d66a2194 5855 /*
7ff89ca2
VS
5856 * On gen2 planes are double buffered but the pipe isn't, so we must
5857 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5858 */
7ff89ca2
VS
5859 if (IS_GEN2(dev_priv))
5860 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5861
7ff89ca2 5862 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5863
7ff89ca2
VS
5864 drm_crtc_vblank_off(crtc);
5865 assert_vblank_disabled(crtc);
d66a2194 5866
7ff89ca2 5867 intel_disable_pipe(intel_crtc);
d66a2194 5868
7ff89ca2 5869 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5870
7ff89ca2 5871 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5872
7ff89ca2
VS
5873 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5874 if (IS_CHERRYVIEW(dev_priv))
5875 chv_disable_pll(dev_priv, pipe);
5876 else if (IS_VALLEYVIEW(dev_priv))
5877 vlv_disable_pll(dev_priv, pipe);
5878 else
5879 i9xx_disable_pll(intel_crtc);
5880 }
c2e001ef 5881
7ff89ca2 5882 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5883
7ff89ca2
VS
5884 if (!IS_GEN2(dev_priv))
5885 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
5886
5887 if (!dev_priv->display.initial_watermarks)
5888 intel_update_watermarks(intel_crtc);
2ee0da16
VS
5889
5890 /* clock the pipe down to 640x480@60 to potentially save power */
5891 if (IS_I830(dev_priv))
5892 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
5893}
5894
da1d0e26
VS
5895static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
5896 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 5897{
7ff89ca2
VS
5898 struct intel_encoder *encoder;
5899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5900 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5901 enum intel_display_power_domain domain;
d2d15016 5902 u64 domains;
7ff89ca2
VS
5903 struct drm_atomic_state *state;
5904 struct intel_crtc_state *crtc_state;
5905 int ret;
f8437dd1 5906
7ff89ca2
VS
5907 if (!intel_crtc->active)
5908 return;
a8ca4934 5909
7ff89ca2 5910 if (crtc->primary->state->visible) {
7ff89ca2 5911 intel_pre_disable_primary_noatomic(crtc);
709e05c3 5912
7ff89ca2
VS
5913 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
5914 crtc->primary->state->visible = false;
5915 }
5d96d8af 5916
7ff89ca2
VS
5917 state = drm_atomic_state_alloc(crtc->dev);
5918 if (!state) {
5919 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5920 crtc->base.id, crtc->name);
1c3f7700 5921 return;
7ff89ca2 5922 }
9f7eb31a 5923
da1d0e26 5924 state->acquire_ctx = ctx;
ea61791e 5925
7ff89ca2
VS
5926 /* Everything's already locked, -EDEADLK can't happen. */
5927 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5928 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5929
7ff89ca2 5930 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5931
7ff89ca2 5932 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5933
0853695c 5934 drm_atomic_state_put(state);
842e0307 5935
78108b7c
VS
5936 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5937 crtc->base.id, crtc->name);
842e0307
ML
5938
5939 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5940 crtc->state->active = false;
37d9078b 5941 intel_crtc->active = false;
842e0307
ML
5942 crtc->enabled = false;
5943 crtc->state->connector_mask = 0;
5944 crtc->state->encoder_mask = 0;
5945
5946 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5947 encoder->base.crtc = NULL;
5948
58f9c0bc 5949 intel_fbc_disable(intel_crtc);
432081bc 5950 intel_update_watermarks(intel_crtc);
1f7457b1 5951 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5952
5953 domains = intel_crtc->enabled_power_domains;
5954 for_each_power_domain(domain, domains)
5955 intel_display_power_put(dev_priv, domain);
5956 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5957
5958 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
d305e061 5959 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5960}
5961
6b72d486
ML
5962/*
5963 * turn all crtc's off, but do not adjust state
5964 * This has to be paired with a call to intel_modeset_setup_hw_state.
5965 */
70e0bd74 5966int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5967{
e2c8b870 5968 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5969 struct drm_atomic_state *state;
e2c8b870 5970 int ret;
70e0bd74 5971
e2c8b870
ML
5972 state = drm_atomic_helper_suspend(dev);
5973 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5974 if (ret)
5975 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5976 else
5977 dev_priv->modeset_restore_state = state;
70e0bd74 5978 return ret;
ee7b9f93
JB
5979}
5980
ea5b213a 5981void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5982{
4ef69c7a 5983 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5984
ea5b213a
CW
5985 drm_encoder_cleanup(encoder);
5986 kfree(intel_encoder);
7e7d76c3
JB
5987}
5988
0a91ca29
DV
5989/* Cross check the actual hw state with our own modeset state tracking (and it's
5990 * internal consistency). */
749d98b8
ML
5991static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
5992 struct drm_connector_state *conn_state)
79e53945 5993{
749d98b8 5994 struct intel_connector *connector = to_intel_connector(conn_state->connector);
35dd3c64
ML
5995
5996 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5997 connector->base.base.id,
5998 connector->base.name);
5999
0a91ca29 6000 if (connector->get_hw_state(connector)) {
e85376cb 6001 struct intel_encoder *encoder = connector->encoder;
0a91ca29 6002
749d98b8 6003 I915_STATE_WARN(!crtc_state,
35dd3c64 6004 "connector enabled without attached crtc\n");
0a91ca29 6005
749d98b8 6006 if (!crtc_state)
35dd3c64
ML
6007 return;
6008
749d98b8 6009 I915_STATE_WARN(!crtc_state->active,
35dd3c64
ML
6010 "connector is active, but attached crtc isn't\n");
6011
e85376cb 6012 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6013 return;
6014
e85376cb 6015 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6016 "atomic encoder doesn't match attached encoder\n");
6017
e85376cb 6018 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6019 "attached encoder crtc differs from connector crtc\n");
6020 } else {
749d98b8 6021 I915_STATE_WARN(crtc_state && crtc_state->active,
4d688a2a 6022 "attached crtc is active, but connector isn't\n");
749d98b8 6023 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 6024 "best encoder set without crtc!\n");
0a91ca29 6025 }
79e53945
JB
6026}
6027
08d9bc92
ACO
6028int intel_connector_init(struct intel_connector *connector)
6029{
11c1a9ec 6030 struct intel_digital_connector_state *conn_state;
08d9bc92 6031
11c1a9ec
ML
6032 /*
6033 * Allocate enough memory to hold intel_digital_connector_state,
6034 * This might be a few bytes too many, but for connectors that don't
6035 * need it we'll free the state and allocate a smaller one on the first
6036 * succesful commit anyway.
6037 */
6038 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6039 if (!conn_state)
08d9bc92
ACO
6040 return -ENOMEM;
6041
11c1a9ec
ML
6042 __drm_atomic_helper_connector_reset(&connector->base,
6043 &conn_state->base);
6044
08d9bc92
ACO
6045 return 0;
6046}
6047
6048struct intel_connector *intel_connector_alloc(void)
6049{
6050 struct intel_connector *connector;
6051
6052 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6053 if (!connector)
6054 return NULL;
6055
6056 if (intel_connector_init(connector) < 0) {
6057 kfree(connector);
6058 return NULL;
6059 }
6060
6061 return connector;
6062}
6063
091a4f91
JA
6064/*
6065 * Free the bits allocated by intel_connector_alloc.
6066 * This should only be used after intel_connector_alloc has returned
6067 * successfully, and before drm_connector_init returns successfully.
6068 * Otherwise the destroy callbacks for the connector and the state should
6069 * take care of proper cleanup/free
6070 */
6071void intel_connector_free(struct intel_connector *connector)
6072{
6073 kfree(to_intel_digital_connector_state(connector->base.state));
6074 kfree(connector);
6075}
6076
f0947c37
DV
6077/* Simple connector->get_hw_state implementation for encoders that support only
6078 * one connector and no cloning and hence the encoder state determines the state
6079 * of the connector. */
6080bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6081{
24929352 6082 enum pipe pipe = 0;
f0947c37 6083 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6084
f0947c37 6085 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6086}
6087
6d293983 6088static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6089{
6d293983
ACO
6090 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6091 return crtc_state->fdi_lanes;
d272ddfa
VS
6092
6093 return 0;
6094}
6095
6d293983 6096static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6097 struct intel_crtc_state *pipe_config)
1857e1da 6098{
8652744b 6099 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6100 struct drm_atomic_state *state = pipe_config->base.state;
6101 struct intel_crtc *other_crtc;
6102 struct intel_crtc_state *other_crtc_state;
6103
1857e1da
DV
6104 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6105 pipe_name(pipe), pipe_config->fdi_lanes);
6106 if (pipe_config->fdi_lanes > 4) {
6107 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6108 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6109 return -EINVAL;
1857e1da
DV
6110 }
6111
8652744b 6112 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6113 if (pipe_config->fdi_lanes > 2) {
6114 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6115 pipe_config->fdi_lanes);
6d293983 6116 return -EINVAL;
1857e1da 6117 } else {
6d293983 6118 return 0;
1857e1da
DV
6119 }
6120 }
6121
b7f05d4a 6122 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6123 return 0;
1857e1da
DV
6124
6125 /* Ivybridge 3 pipe is really complicated */
6126 switch (pipe) {
6127 case PIPE_A:
6d293983 6128 return 0;
1857e1da 6129 case PIPE_B:
6d293983
ACO
6130 if (pipe_config->fdi_lanes <= 2)
6131 return 0;
6132
b91eb5cc 6133 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6134 other_crtc_state =
6135 intel_atomic_get_crtc_state(state, other_crtc);
6136 if (IS_ERR(other_crtc_state))
6137 return PTR_ERR(other_crtc_state);
6138
6139 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6140 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6141 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6142 return -EINVAL;
1857e1da 6143 }
6d293983 6144 return 0;
1857e1da 6145 case PIPE_C:
251cc67c
VS
6146 if (pipe_config->fdi_lanes > 2) {
6147 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6148 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6149 return -EINVAL;
251cc67c 6150 }
6d293983 6151
b91eb5cc 6152 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6153 other_crtc_state =
6154 intel_atomic_get_crtc_state(state, other_crtc);
6155 if (IS_ERR(other_crtc_state))
6156 return PTR_ERR(other_crtc_state);
6157
6158 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6159 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6160 return -EINVAL;
1857e1da 6161 }
6d293983 6162 return 0;
1857e1da
DV
6163 default:
6164 BUG();
6165 }
6166}
6167
e29c22c0
DV
6168#define RETRY 1
6169static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6170 struct intel_crtc_state *pipe_config)
877d48d5 6171{
1857e1da 6172 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6173 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6174 int lane, link_bw, fdi_dotclock, ret;
6175 bool needs_recompute = false;
877d48d5 6176
e29c22c0 6177retry:
877d48d5
DV
6178 /* FDI is a binary signal running at ~2.7GHz, encoding
6179 * each output octet as 10 bits. The actual frequency
6180 * is stored as a divider into a 100MHz clock, and the
6181 * mode pixel clock is stored in units of 1KHz.
6182 * Hence the bw of each lane in terms of the mode signal
6183 * is:
6184 */
21a727b3 6185 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6186
241bfc38 6187 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6188
2bd89a07 6189 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6190 pipe_config->pipe_bpp);
6191
6192 pipe_config->fdi_lanes = lane;
6193
2bd89a07 6194 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
b31e85ed 6195 link_bw, &pipe_config->fdi_m_n, false);
1857e1da 6196
e3b247da 6197 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6198 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6199 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6200 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6201 pipe_config->pipe_bpp);
6202 needs_recompute = true;
6203 pipe_config->bw_constrained = true;
257a7ffc 6204
7ff89ca2 6205 goto retry;
257a7ffc 6206 }
79e53945 6207
7ff89ca2
VS
6208 if (needs_recompute)
6209 return RETRY;
e70236a8 6210
7ff89ca2 6211 return ret;
e70236a8
JB
6212}
6213
7ff89ca2
VS
6214static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6215 struct intel_crtc_state *pipe_config)
e70236a8 6216{
6e644626
VS
6217 if (pipe_config->ips_force_disable)
6218 return false;
6219
7ff89ca2
VS
6220 if (pipe_config->pipe_bpp > 24)
6221 return false;
e70236a8 6222
7ff89ca2
VS
6223 /* HSW can handle pixel rate up to cdclk? */
6224 if (IS_HASWELL(dev_priv))
6225 return true;
1b1d2716 6226
65cd2b3f 6227 /*
7ff89ca2
VS
6228 * We compare against max which means we must take
6229 * the increased cdclk requirement into account when
6230 * calculating the new cdclk.
6231 *
6232 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6233 */
7ff89ca2
VS
6234 return pipe_config->pixel_rate <=
6235 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6236}
79e53945 6237
7ff89ca2
VS
6238static void hsw_compute_ips_config(struct intel_crtc *crtc,
6239 struct intel_crtc_state *pipe_config)
6240{
6241 struct drm_device *dev = crtc->base.dev;
6242 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6243
4f044a88 6244 pipe_config->ips_enabled = i915_modparams.enable_ips &&
7ff89ca2
VS
6245 hsw_crtc_supports_ips(crtc) &&
6246 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6247}
6248
7ff89ca2 6249static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6250{
7ff89ca2 6251 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6252
7ff89ca2
VS
6253 /* GDG double wide on either pipe, otherwise pipe A only */
6254 return INTEL_INFO(dev_priv)->gen < 4 &&
6255 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6256}
6257
ceb99320
VS
6258static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6259{
6260 uint32_t pixel_rate;
6261
6262 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6263
6264 /*
6265 * We only use IF-ID interlacing. If we ever use
6266 * PF-ID we'll need to adjust the pixel_rate here.
6267 */
6268
6269 if (pipe_config->pch_pfit.enabled) {
6270 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6271 uint32_t pfit_size = pipe_config->pch_pfit.size;
6272
6273 pipe_w = pipe_config->pipe_src_w;
6274 pipe_h = pipe_config->pipe_src_h;
6275
6276 pfit_w = (pfit_size >> 16) & 0xFFFF;
6277 pfit_h = pfit_size & 0xFFFF;
6278 if (pipe_w < pfit_w)
6279 pipe_w = pfit_w;
6280 if (pipe_h < pfit_h)
6281 pipe_h = pfit_h;
6282
6283 if (WARN_ON(!pfit_w || !pfit_h))
6284 return pixel_rate;
6285
6286 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6287 pfit_w * pfit_h);
6288 }
6289
6290 return pixel_rate;
6291}
6292
7ff89ca2 6293static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6294{
7ff89ca2 6295 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6296
7ff89ca2
VS
6297 if (HAS_GMCH_DISPLAY(dev_priv))
6298 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6299 crtc_state->pixel_rate =
6300 crtc_state->base.adjusted_mode.crtc_clock;
6301 else
6302 crtc_state->pixel_rate =
6303 ilk_pipe_pixel_rate(crtc_state);
6304}
34edce2f 6305
7ff89ca2
VS
6306static int intel_crtc_compute_config(struct intel_crtc *crtc,
6307 struct intel_crtc_state *pipe_config)
6308{
6309 struct drm_device *dev = crtc->base.dev;
6310 struct drm_i915_private *dev_priv = to_i915(dev);
6311 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6312 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6313
7ff89ca2
VS
6314 if (INTEL_GEN(dev_priv) < 4) {
6315 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6316
7ff89ca2
VS
6317 /*
6318 * Enable double wide mode when the dot clock
6319 * is > 90% of the (display) core speed.
6320 */
6321 if (intel_crtc_supports_double_wide(crtc) &&
6322 adjusted_mode->crtc_clock > clock_limit) {
6323 clock_limit = dev_priv->max_dotclk_freq;
6324 pipe_config->double_wide = true;
6325 }
34edce2f
VS
6326 }
6327
7ff89ca2
VS
6328 if (adjusted_mode->crtc_clock > clock_limit) {
6329 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6330 adjusted_mode->crtc_clock, clock_limit,
6331 yesno(pipe_config->double_wide));
6332 return -EINVAL;
6333 }
34edce2f 6334
25edf915
SS
6335 if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
6336 /*
6337 * There is only one pipe CSC unit per pipe, and we need that
6338 * for output conversion from RGB->YCBCR. So if CTM is already
6339 * applied we can't support YCBCR420 output.
6340 */
6341 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6342 return -EINVAL;
6343 }
6344
7ff89ca2
VS
6345 /*
6346 * Pipe horizontal size must be even in:
6347 * - DVO ganged mode
6348 * - LVDS dual channel mode
6349 * - Double wide pipe
6350 */
6351 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6352 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6353 pipe_config->pipe_src_w &= ~1;
34edce2f 6354
7ff89ca2
VS
6355 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6356 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6357 */
6358 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6359 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6360 return -EINVAL;
34edce2f 6361
7ff89ca2 6362 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6363
7ff89ca2
VS
6364 if (HAS_IPS(dev_priv))
6365 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6366
7ff89ca2
VS
6367 if (pipe_config->has_pch_encoder)
6368 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6369
7ff89ca2 6370 return 0;
34edce2f
VS
6371}
6372
2c07245f 6373static void
a65851af 6374intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6375{
a65851af
VS
6376 while (*num > DATA_LINK_M_N_MASK ||
6377 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6378 *num >>= 1;
6379 *den >>= 1;
6380 }
6381}
6382
a65851af 6383static void compute_m_n(unsigned int m, unsigned int n,
b31e85ed
JN
6384 uint32_t *ret_m, uint32_t *ret_n,
6385 bool reduce_m_n)
a65851af 6386{
9a86cda0
JN
6387 /*
6388 * Reduce M/N as much as possible without loss in precision. Several DP
6389 * dongles in particular seem to be fussy about too large *link* M/N
6390 * values. The passed in values are more likely to have the least
6391 * significant bits zero than M after rounding below, so do this first.
6392 */
b31e85ed
JN
6393 if (reduce_m_n) {
6394 while ((m & 1) == 0 && (n & 1) == 0) {
6395 m >>= 1;
6396 n >>= 1;
6397 }
9a86cda0
JN
6398 }
6399
a65851af
VS
6400 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6401 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6402 intel_reduce_m_n_ratio(ret_m, ret_n);
6403}
6404
e69d0bc1
DV
6405void
6406intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6407 int pixel_clock, int link_clock,
b31e85ed
JN
6408 struct intel_link_m_n *m_n,
6409 bool reduce_m_n)
2c07245f 6410{
e69d0bc1 6411 m_n->tu = 64;
a65851af
VS
6412
6413 compute_m_n(bits_per_pixel * pixel_clock,
6414 link_clock * nlanes * 8,
b31e85ed
JN
6415 &m_n->gmch_m, &m_n->gmch_n,
6416 reduce_m_n);
a65851af
VS
6417
6418 compute_m_n(pixel_clock, link_clock,
b31e85ed
JN
6419 &m_n->link_m, &m_n->link_n,
6420 reduce_m_n);
2c07245f
ZW
6421}
6422
a7615030
CW
6423static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6424{
4f044a88
MW
6425 if (i915_modparams.panel_use_ssc >= 0)
6426 return i915_modparams.panel_use_ssc != 0;
41aa3448 6427 return dev_priv->vbt.lvds_use_ssc
435793df 6428 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6429}
6430
7429e9d4 6431static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6432{
7df00d7a 6433 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6434}
f47709a9 6435
7429e9d4
DV
6436static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6437{
6438 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6439}
6440
f47709a9 6441static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6442 struct intel_crtc_state *crtc_state,
9e2c8475 6443 struct dpll *reduced_clock)
a7516a05 6444{
9b1e14f4 6445 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6446 u32 fp, fp2 = 0;
6447
9b1e14f4 6448 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6449 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6450 if (reduced_clock)
7429e9d4 6451 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6452 } else {
190f68c5 6453 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6454 if (reduced_clock)
7429e9d4 6455 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6456 }
6457
190f68c5 6458 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6459
2d84d2b3 6460 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6461 reduced_clock) {
190f68c5 6462 crtc_state->dpll_hw_state.fp1 = fp2;
a7516a05 6463 } else {
190f68c5 6464 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6465 }
6466}
6467
5e69f97f
CML
6468static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6469 pipe)
89b667f8
JB
6470{
6471 u32 reg_val;
6472
6473 /*
6474 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6475 * and set it to a reasonable value instead.
6476 */
ab3c759a 6477 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6478 reg_val &= 0xffffff00;
6479 reg_val |= 0x00000030;
ab3c759a 6480 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6481
ab3c759a 6482 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
6483 reg_val &= 0x00ffffff;
6484 reg_val |= 0x8c000000;
ab3c759a 6485 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6486
ab3c759a 6487 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6488 reg_val &= 0xffffff00;
ab3c759a 6489 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6490
ab3c759a 6491 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6492 reg_val &= 0x00ffffff;
6493 reg_val |= 0xb0000000;
ab3c759a 6494 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6495}
6496
b551842d
DV
6497static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6498 struct intel_link_m_n *m_n)
6499{
6500 struct drm_device *dev = crtc->base.dev;
fac5e23e 6501 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6502 int pipe = crtc->pipe;
6503
e3b95f1e
DV
6504 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6505 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6506 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6507 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6508}
6509
6510static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6511 struct intel_link_m_n *m_n,
6512 struct intel_link_m_n *m2_n2)
b551842d 6513{
6315b5d3 6514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6515 int pipe = crtc->pipe;
6e3c9717 6516 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6517
6315b5d3 6518 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6519 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6520 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6521 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6522 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6523 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6524 * for gen < 8) and if DRRS is supported (to make sure the
6525 * registers are not unnecessarily accessed).
6526 */
920a14b2
TU
6527 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6528 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6529 I915_WRITE(PIPE_DATA_M2(transcoder),
6530 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6531 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6532 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6533 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6534 }
b551842d 6535 } else {
e3b95f1e
DV
6536 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6537 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6538 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6539 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6540 }
6541}
6542
fe3cd48d 6543void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6544{
fe3cd48d
R
6545 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6546
6547 if (m_n == M1_N1) {
6548 dp_m_n = &crtc->config->dp_m_n;
6549 dp_m2_n2 = &crtc->config->dp_m2_n2;
6550 } else if (m_n == M2_N2) {
6551
6552 /*
6553 * M2_N2 registers are not supported. Hence m2_n2 divider value
6554 * needs to be programmed into M1_N1.
6555 */
6556 dp_m_n = &crtc->config->dp_m2_n2;
6557 } else {
6558 DRM_ERROR("Unsupported divider value\n");
6559 return;
6560 }
6561
6e3c9717
ACO
6562 if (crtc->config->has_pch_encoder)
6563 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6564 else
fe3cd48d 6565 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6566}
6567
251ac862
DV
6568static void vlv_compute_dpll(struct intel_crtc *crtc,
6569 struct intel_crtc_state *pipe_config)
bdd4b6a6 6570{
03ed5cbf 6571 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6572 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6573 if (crtc->pipe != PIPE_A)
6574 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6575
cd2d34d9 6576 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6577 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6578 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6579 DPLL_EXT_BUFFER_ENABLE_VLV;
6580
03ed5cbf
VS
6581 pipe_config->dpll_hw_state.dpll_md =
6582 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6583}
bdd4b6a6 6584
03ed5cbf
VS
6585static void chv_compute_dpll(struct intel_crtc *crtc,
6586 struct intel_crtc_state *pipe_config)
6587{
6588 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6589 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6590 if (crtc->pipe != PIPE_A)
6591 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6592
cd2d34d9 6593 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6594 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6595 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6596
03ed5cbf
VS
6597 pipe_config->dpll_hw_state.dpll_md =
6598 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6599}
6600
d288f65f 6601static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6602 const struct intel_crtc_state *pipe_config)
a0c4da24 6603{
f47709a9 6604 struct drm_device *dev = crtc->base.dev;
fac5e23e 6605 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6606 enum pipe pipe = crtc->pipe;
bdd4b6a6 6607 u32 mdiv;
a0c4da24 6608 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6609 u32 coreclk, reg_val;
a0c4da24 6610
cd2d34d9
VS
6611 /* Enable Refclk */
6612 I915_WRITE(DPLL(pipe),
6613 pipe_config->dpll_hw_state.dpll &
6614 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6615
6616 /* No need to actually set up the DPLL with DSI */
6617 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6618 return;
6619
a580516d 6620 mutex_lock(&dev_priv->sb_lock);
09153000 6621
d288f65f
VS
6622 bestn = pipe_config->dpll.n;
6623 bestm1 = pipe_config->dpll.m1;
6624 bestm2 = pipe_config->dpll.m2;
6625 bestp1 = pipe_config->dpll.p1;
6626 bestp2 = pipe_config->dpll.p2;
a0c4da24 6627
89b667f8
JB
6628 /* See eDP HDMI DPIO driver vbios notes doc */
6629
6630 /* PLL B needs special handling */
bdd4b6a6 6631 if (pipe == PIPE_B)
5e69f97f 6632 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6633
6634 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6635 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6636
6637 /* Disable target IRef on PLL */
ab3c759a 6638 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6639 reg_val &= 0x00ffffff;
ab3c759a 6640 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6641
6642 /* Disable fast lock */
ab3c759a 6643 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6644
6645 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6646 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6647 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6648 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6649 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6650
6651 /*
6652 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6653 * but we don't support that).
6654 * Note: don't use the DAC post divider as it seems unstable.
6655 */
6656 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6657 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6658
a0c4da24 6659 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6660 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6661
89b667f8 6662 /* Set HBR and RBR LPF coefficients */
d288f65f 6663 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6664 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6665 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6666 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6667 0x009f0003);
89b667f8 6668 else
ab3c759a 6669 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6670 0x00d0000f);
6671
37a5650b 6672 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6673 /* Use SSC source */
bdd4b6a6 6674 if (pipe == PIPE_A)
ab3c759a 6675 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6676 0x0df40000);
6677 else
ab3c759a 6678 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6679 0x0df70000);
6680 } else { /* HDMI or VGA */
6681 /* Use bend source */
bdd4b6a6 6682 if (pipe == PIPE_A)
ab3c759a 6683 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6684 0x0df70000);
6685 else
ab3c759a 6686 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6687 0x0df40000);
6688 }
a0c4da24 6689
ab3c759a 6690 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6691 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6692 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6693 coreclk |= 0x01000000;
ab3c759a 6694 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6695
ab3c759a 6696 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6697 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6698}
6699
d288f65f 6700static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6701 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6702{
6703 struct drm_device *dev = crtc->base.dev;
fac5e23e 6704 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6705 enum pipe pipe = crtc->pipe;
9d556c99 6706 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6707 u32 loopfilter, tribuf_calcntr;
9d556c99 6708 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6709 u32 dpio_val;
9cbe40c1 6710 int vco;
9d556c99 6711
cd2d34d9
VS
6712 /* Enable Refclk and SSC */
6713 I915_WRITE(DPLL(pipe),
6714 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6715
6716 /* No need to actually set up the DPLL with DSI */
6717 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6718 return;
6719
d288f65f
VS
6720 bestn = pipe_config->dpll.n;
6721 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6722 bestm1 = pipe_config->dpll.m1;
6723 bestm2 = pipe_config->dpll.m2 >> 22;
6724 bestp1 = pipe_config->dpll.p1;
6725 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6726 vco = pipe_config->dpll.vco;
a945ce7e 6727 dpio_val = 0;
9cbe40c1 6728 loopfilter = 0;
9d556c99 6729
a580516d 6730 mutex_lock(&dev_priv->sb_lock);
9d556c99 6731
9d556c99
CML
6732 /* p1 and p2 divider */
6733 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6734 5 << DPIO_CHV_S1_DIV_SHIFT |
6735 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6736 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6737 1 << DPIO_CHV_K_DIV_SHIFT);
6738
6739 /* Feedback post-divider - m2 */
6740 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6741
6742 /* Feedback refclk divider - n and m1 */
6743 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6744 DPIO_CHV_M1_DIV_BY_2 |
6745 1 << DPIO_CHV_N_DIV_SHIFT);
6746
6747 /* M2 fraction division */
25a25dfc 6748 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6749
6750 /* M2 fraction division enable */
a945ce7e
VP
6751 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6752 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6753 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6754 if (bestm2_frac)
6755 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6756 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6757
de3a0fde
VP
6758 /* Program digital lock detect threshold */
6759 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6760 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6761 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6762 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6763 if (!bestm2_frac)
6764 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6765 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6766
9d556c99 6767 /* Loop filter */
9cbe40c1
VP
6768 if (vco == 5400000) {
6769 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6770 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6771 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6772 tribuf_calcntr = 0x9;
6773 } else if (vco <= 6200000) {
6774 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6775 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6776 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6777 tribuf_calcntr = 0x9;
6778 } else if (vco <= 6480000) {
6779 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6780 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6781 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6782 tribuf_calcntr = 0x8;
6783 } else {
6784 /* Not supported. Apply the same limits as in the max case */
6785 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6786 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6787 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6788 tribuf_calcntr = 0;
6789 }
9d556c99
CML
6790 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6791
968040b2 6792 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6793 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6794 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6795 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6796
9d556c99
CML
6797 /* AFC Recal */
6798 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6799 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6800 DPIO_AFC_RECAL);
6801
a580516d 6802 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6803}
6804
d288f65f
VS
6805/**
6806 * vlv_force_pll_on - forcibly enable just the PLL
6807 * @dev_priv: i915 private structure
6808 * @pipe: pipe PLL to enable
6809 * @dpll: PLL configuration
6810 *
6811 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6812 * in cases where we need the PLL enabled even when @pipe is not going to
6813 * be enabled.
6814 */
30ad9814 6815int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6816 const struct dpll *dpll)
d288f65f 6817{
b91eb5cc 6818 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6819 struct intel_crtc_state *pipe_config;
6820
6821 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6822 if (!pipe_config)
6823 return -ENOMEM;
6824
6825 pipe_config->base.crtc = &crtc->base;
6826 pipe_config->pixel_multiplier = 1;
6827 pipe_config->dpll = *dpll;
d288f65f 6828
30ad9814 6829 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6830 chv_compute_dpll(crtc, pipe_config);
6831 chv_prepare_pll(crtc, pipe_config);
6832 chv_enable_pll(crtc, pipe_config);
d288f65f 6833 } else {
3f36b937
TU
6834 vlv_compute_dpll(crtc, pipe_config);
6835 vlv_prepare_pll(crtc, pipe_config);
6836 vlv_enable_pll(crtc, pipe_config);
d288f65f 6837 }
3f36b937
TU
6838
6839 kfree(pipe_config);
6840
6841 return 0;
d288f65f
VS
6842}
6843
6844/**
6845 * vlv_force_pll_off - forcibly disable just the PLL
6846 * @dev_priv: i915 private structure
6847 * @pipe: pipe PLL to disable
6848 *
6849 * Disable the PLL for @pipe. To be used in cases where we need
6850 * the PLL enabled even when @pipe is not going to be enabled.
6851 */
30ad9814 6852void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6853{
30ad9814
VS
6854 if (IS_CHERRYVIEW(dev_priv))
6855 chv_disable_pll(dev_priv, pipe);
d288f65f 6856 else
30ad9814 6857 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6858}
6859
251ac862
DV
6860static void i9xx_compute_dpll(struct intel_crtc *crtc,
6861 struct intel_crtc_state *crtc_state,
9e2c8475 6862 struct dpll *reduced_clock)
eb1cbe48 6863{
9b1e14f4 6864 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6865 u32 dpll;
190f68c5 6866 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6867
190f68c5 6868 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6869
eb1cbe48
DV
6870 dpll = DPLL_VGA_MODE_DIS;
6871
2d84d2b3 6872 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6873 dpll |= DPLLB_MODE_LVDS;
6874 else
6875 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6876
73f67aa8
JN
6877 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6878 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6879 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6880 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6881 }
198a037f 6882
3d6e9ee0
VS
6883 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6884 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6885 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6886
37a5650b 6887 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6888 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6889
6890 /* compute bitmask from p1 value */
9b1e14f4 6891 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6892 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6893 else {
6894 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6895 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6896 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6897 }
6898 switch (clock->p2) {
6899 case 5:
6900 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6901 break;
6902 case 7:
6903 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6904 break;
6905 case 10:
6906 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6907 break;
6908 case 14:
6909 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6910 break;
6911 }
9b1e14f4 6912 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6913 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6914
190f68c5 6915 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6916 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6917 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6918 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6919 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6920 else
6921 dpll |= PLL_REF_INPUT_DREFCLK;
6922
6923 dpll |= DPLL_VCO_ENABLE;
190f68c5 6924 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6925
9b1e14f4 6926 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6927 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6928 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6929 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6930 }
6931}
6932
251ac862
DV
6933static void i8xx_compute_dpll(struct intel_crtc *crtc,
6934 struct intel_crtc_state *crtc_state,
9e2c8475 6935 struct dpll *reduced_clock)
eb1cbe48 6936{
f47709a9 6937 struct drm_device *dev = crtc->base.dev;
fac5e23e 6938 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6939 u32 dpll;
190f68c5 6940 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6941
190f68c5 6942 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6943
eb1cbe48
DV
6944 dpll = DPLL_VGA_MODE_DIS;
6945
2d84d2b3 6946 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6947 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6948 } else {
6949 if (clock->p1 == 2)
6950 dpll |= PLL_P1_DIVIDE_BY_TWO;
6951 else
6952 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6953 if (clock->p2 == 4)
6954 dpll |= PLL_P2_DIVIDE_BY_4;
6955 }
6956
50a0bc90
TU
6957 if (!IS_I830(dev_priv) &&
6958 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6959 dpll |= DPLL_DVO_2X_MODE;
6960
2d84d2b3 6961 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6962 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6963 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6964 else
6965 dpll |= PLL_REF_INPUT_DREFCLK;
6966
6967 dpll |= DPLL_VCO_ENABLE;
190f68c5 6968 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6969}
6970
8a654f3b 6971static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6972{
6315b5d3 6973 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6974 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6975 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6976 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6977 uint32_t crtc_vtotal, crtc_vblank_end;
6978 int vsyncshift = 0;
4d8a62ea
DV
6979
6980 /* We need to be careful not to changed the adjusted mode, for otherwise
6981 * the hw state checker will get angry at the mismatch. */
6982 crtc_vtotal = adjusted_mode->crtc_vtotal;
6983 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6984
609aeaca 6985 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6986 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6987 crtc_vtotal -= 1;
6988 crtc_vblank_end -= 1;
609aeaca 6989
2d84d2b3 6990 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6991 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6992 else
6993 vsyncshift = adjusted_mode->crtc_hsync_start -
6994 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6995 if (vsyncshift < 0)
6996 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6997 }
6998
6315b5d3 6999 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 7000 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7001
fe2b8f9d 7002 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7003 (adjusted_mode->crtc_hdisplay - 1) |
7004 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7005 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7006 (adjusted_mode->crtc_hblank_start - 1) |
7007 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7008 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7009 (adjusted_mode->crtc_hsync_start - 1) |
7010 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7011
fe2b8f9d 7012 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7013 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7014 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7015 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7016 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7017 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7018 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7019 (adjusted_mode->crtc_vsync_start - 1) |
7020 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7021
b5e508d4
PZ
7022 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7023 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7024 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7025 * bits. */
772c2a51 7026 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
7027 (pipe == PIPE_B || pipe == PIPE_C))
7028 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7029
bc58be60
JN
7030}
7031
7032static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7033{
7034 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7035 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7036 enum pipe pipe = intel_crtc->pipe;
7037
b0e77b9c
PZ
7038 /* pipesrc controls the size that is scaled from, which should
7039 * always be the user's requested size.
7040 */
7041 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7042 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7043 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7044}
7045
1bd1bd80 7046static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7047 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7048{
7049 struct drm_device *dev = crtc->base.dev;
fac5e23e 7050 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7051 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7052 uint32_t tmp;
7053
7054 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7055 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7056 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7057 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7058 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7059 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7060 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7061 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7062 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7063
7064 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7065 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7066 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7067 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7068 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7069 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7070 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7071 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7072 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7073
7074 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7075 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7076 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7077 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7078 }
bc58be60
JN
7079}
7080
7081static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7082 struct intel_crtc_state *pipe_config)
7083{
7084 struct drm_device *dev = crtc->base.dev;
fac5e23e 7085 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7086 u32 tmp;
1bd1bd80
DV
7087
7088 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7089 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7090 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7091
2d112de7
ACO
7092 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7093 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7094}
7095
f6a83288 7096void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7097 struct intel_crtc_state *pipe_config)
babea61d 7098{
2d112de7
ACO
7099 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7100 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7101 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7102 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7103
2d112de7
ACO
7104 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7105 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7106 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7107 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7108
2d112de7 7109 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7110 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7111
2d112de7 7112 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7113
7114 mode->hsync = drm_mode_hsync(mode);
7115 mode->vrefresh = drm_mode_vrefresh(mode);
7116 drm_mode_set_name(mode);
babea61d
JB
7117}
7118
84b046f3
DV
7119static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7120{
6315b5d3 7121 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7122 uint32_t pipeconf;
7123
9f11a9e4 7124 pipeconf = 0;
84b046f3 7125
e56134bc
VS
7126 /* we keep both pipes enabled on 830 */
7127 if (IS_I830(dev_priv))
b6b5d049 7128 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7129
6e3c9717 7130 if (intel_crtc->config->double_wide)
cf532bb2 7131 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7132
ff9ce46e 7133 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7134 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7135 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7136 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7137 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7138 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7139 PIPECONF_DITHER_TYPE_SP;
84b046f3 7140
6e3c9717 7141 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7142 case 18:
7143 pipeconf |= PIPECONF_6BPC;
7144 break;
7145 case 24:
7146 pipeconf |= PIPECONF_8BPC;
7147 break;
7148 case 30:
7149 pipeconf |= PIPECONF_10BPC;
7150 break;
7151 default:
7152 /* Case prevented by intel_choose_pipe_bpp_dither. */
7153 BUG();
84b046f3
DV
7154 }
7155 }
7156
6e3c9717 7157 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7158 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7159 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7160 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7161 else
7162 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7163 } else
84b046f3
DV
7164 pipeconf |= PIPECONF_PROGRESSIVE;
7165
920a14b2 7166 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7167 intel_crtc->config->limited_color_range)
9f11a9e4 7168 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7169
84b046f3
DV
7170 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7171 POSTING_READ(PIPECONF(intel_crtc->pipe));
7172}
7173
81c97f52
ACO
7174static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7175 struct intel_crtc_state *crtc_state)
7176{
7177 struct drm_device *dev = crtc->base.dev;
fac5e23e 7178 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7179 const struct intel_limit *limit;
81c97f52
ACO
7180 int refclk = 48000;
7181
7182 memset(&crtc_state->dpll_hw_state, 0,
7183 sizeof(crtc_state->dpll_hw_state));
7184
2d84d2b3 7185 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7186 if (intel_panel_use_ssc(dev_priv)) {
7187 refclk = dev_priv->vbt.lvds_ssc_freq;
7188 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7189 }
7190
7191 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7192 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7193 limit = &intel_limits_i8xx_dvo;
7194 } else {
7195 limit = &intel_limits_i8xx_dac;
7196 }
7197
7198 if (!crtc_state->clock_set &&
7199 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7200 refclk, NULL, &crtc_state->dpll)) {
7201 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7202 return -EINVAL;
7203 }
7204
7205 i8xx_compute_dpll(crtc, crtc_state, NULL);
7206
7207 return 0;
7208}
7209
19ec6693
ACO
7210static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7211 struct intel_crtc_state *crtc_state)
7212{
7213 struct drm_device *dev = crtc->base.dev;
fac5e23e 7214 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7215 const struct intel_limit *limit;
19ec6693
ACO
7216 int refclk = 96000;
7217
7218 memset(&crtc_state->dpll_hw_state, 0,
7219 sizeof(crtc_state->dpll_hw_state));
7220
2d84d2b3 7221 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7222 if (intel_panel_use_ssc(dev_priv)) {
7223 refclk = dev_priv->vbt.lvds_ssc_freq;
7224 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7225 }
7226
7227 if (intel_is_dual_link_lvds(dev))
7228 limit = &intel_limits_g4x_dual_channel_lvds;
7229 else
7230 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7231 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7232 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7233 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7234 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7235 limit = &intel_limits_g4x_sdvo;
7236 } else {
7237 /* The option is for other outputs */
7238 limit = &intel_limits_i9xx_sdvo;
7239 }
7240
7241 if (!crtc_state->clock_set &&
7242 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7243 refclk, NULL, &crtc_state->dpll)) {
7244 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7245 return -EINVAL;
7246 }
7247
7248 i9xx_compute_dpll(crtc, crtc_state, NULL);
7249
7250 return 0;
7251}
7252
70e8aa21
ACO
7253static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7254 struct intel_crtc_state *crtc_state)
7255{
7256 struct drm_device *dev = crtc->base.dev;
fac5e23e 7257 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7258 const struct intel_limit *limit;
70e8aa21
ACO
7259 int refclk = 96000;
7260
7261 memset(&crtc_state->dpll_hw_state, 0,
7262 sizeof(crtc_state->dpll_hw_state));
7263
2d84d2b3 7264 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7265 if (intel_panel_use_ssc(dev_priv)) {
7266 refclk = dev_priv->vbt.lvds_ssc_freq;
7267 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7268 }
7269
7270 limit = &intel_limits_pineview_lvds;
7271 } else {
7272 limit = &intel_limits_pineview_sdvo;
7273 }
7274
7275 if (!crtc_state->clock_set &&
7276 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7277 refclk, NULL, &crtc_state->dpll)) {
7278 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7279 return -EINVAL;
7280 }
7281
7282 i9xx_compute_dpll(crtc, crtc_state, NULL);
7283
7284 return 0;
7285}
7286
190f68c5
ACO
7287static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7288 struct intel_crtc_state *crtc_state)
79e53945 7289{
c7653199 7290 struct drm_device *dev = crtc->base.dev;
fac5e23e 7291 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7292 const struct intel_limit *limit;
81c97f52 7293 int refclk = 96000;
79e53945 7294
dd3cd74a
ACO
7295 memset(&crtc_state->dpll_hw_state, 0,
7296 sizeof(crtc_state->dpll_hw_state));
7297
2d84d2b3 7298 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7299 if (intel_panel_use_ssc(dev_priv)) {
7300 refclk = dev_priv->vbt.lvds_ssc_freq;
7301 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7302 }
43565a06 7303
70e8aa21
ACO
7304 limit = &intel_limits_i9xx_lvds;
7305 } else {
7306 limit = &intel_limits_i9xx_sdvo;
81c97f52 7307 }
79e53945 7308
70e8aa21
ACO
7309 if (!crtc_state->clock_set &&
7310 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7311 refclk, NULL, &crtc_state->dpll)) {
7312 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7313 return -EINVAL;
f47709a9 7314 }
7026d4ac 7315
81c97f52 7316 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7317
c8f7a0db 7318 return 0;
f564048e
EA
7319}
7320
65b3d6a9
ACO
7321static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7322 struct intel_crtc_state *crtc_state)
7323{
7324 int refclk = 100000;
1b6f4958 7325 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7326
7327 memset(&crtc_state->dpll_hw_state, 0,
7328 sizeof(crtc_state->dpll_hw_state));
7329
65b3d6a9
ACO
7330 if (!crtc_state->clock_set &&
7331 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7332 refclk, NULL, &crtc_state->dpll)) {
7333 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7334 return -EINVAL;
7335 }
7336
7337 chv_compute_dpll(crtc, crtc_state);
7338
7339 return 0;
7340}
7341
7342static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7343 struct intel_crtc_state *crtc_state)
7344{
7345 int refclk = 100000;
1b6f4958 7346 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7347
7348 memset(&crtc_state->dpll_hw_state, 0,
7349 sizeof(crtc_state->dpll_hw_state));
7350
65b3d6a9
ACO
7351 if (!crtc_state->clock_set &&
7352 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7353 refclk, NULL, &crtc_state->dpll)) {
7354 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7355 return -EINVAL;
7356 }
7357
7358 vlv_compute_dpll(crtc, crtc_state);
7359
7360 return 0;
7361}
7362
2fa2fe9a 7363static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7364 struct intel_crtc_state *pipe_config)
2fa2fe9a 7365{
6315b5d3 7366 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7367 uint32_t tmp;
7368
50a0bc90
TU
7369 if (INTEL_GEN(dev_priv) <= 3 &&
7370 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7371 return;
7372
2fa2fe9a 7373 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7374 if (!(tmp & PFIT_ENABLE))
7375 return;
2fa2fe9a 7376
06922821 7377 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7378 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7379 if (crtc->pipe != PIPE_B)
7380 return;
2fa2fe9a
DV
7381 } else {
7382 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7383 return;
7384 }
7385
06922821 7386 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7387 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7388}
7389
acbec814 7390static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7391 struct intel_crtc_state *pipe_config)
acbec814
JB
7392{
7393 struct drm_device *dev = crtc->base.dev;
fac5e23e 7394 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7395 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7396 struct dpll clock;
acbec814 7397 u32 mdiv;
662c6ecb 7398 int refclk = 100000;
acbec814 7399
b521973b
VS
7400 /* In case of DSI, DPLL will not be used */
7401 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7402 return;
7403
a580516d 7404 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7405 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7406 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7407
7408 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7409 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7410 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7411 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7412 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7413
dccbea3b 7414 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7415}
7416
5724dbd1
DL
7417static void
7418i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7419 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7420{
7421 struct drm_device *dev = crtc->base.dev;
fac5e23e 7422 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7423 u32 val, base, offset;
7424 int pipe = crtc->pipe, plane = crtc->plane;
7425 int fourcc, pixel_format;
6761dd31 7426 unsigned int aligned_height;
b113d5ee 7427 struct drm_framebuffer *fb;
1b842c89 7428 struct intel_framebuffer *intel_fb;
1ad292b5 7429
42a7b088
DL
7430 val = I915_READ(DSPCNTR(plane));
7431 if (!(val & DISPLAY_PLANE_ENABLE))
7432 return;
7433
d9806c9f 7434 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7435 if (!intel_fb) {
1ad292b5
JB
7436 DRM_DEBUG_KMS("failed to alloc fb\n");
7437 return;
7438 }
7439
1b842c89
DL
7440 fb = &intel_fb->base;
7441
d2e9f5fc
VS
7442 fb->dev = dev;
7443
6315b5d3 7444 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7445 if (val & DISPPLANE_TILED) {
49af449b 7446 plane_config->tiling = I915_TILING_X;
bae781b2 7447 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7448 }
7449 }
1ad292b5
JB
7450
7451 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7452 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7453 fb->format = drm_format_info(fourcc);
1ad292b5 7454
6315b5d3 7455 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7456 if (plane_config->tiling)
1ad292b5
JB
7457 offset = I915_READ(DSPTILEOFF(plane));
7458 else
7459 offset = I915_READ(DSPLINOFF(plane));
7460 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7461 } else {
7462 base = I915_READ(DSPADDR(plane));
7463 }
7464 plane_config->base = base;
7465
7466 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7467 fb->width = ((val >> 16) & 0xfff) + 1;
7468 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7469
7470 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7471 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7472
d88c4afd 7473 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7474
f37b5c2b 7475 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7476
2844a921
DL
7477 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7478 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7479 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7480 plane_config->size);
1ad292b5 7481
2d14030b 7482 plane_config->fb = intel_fb;
1ad292b5
JB
7483}
7484
70b23a98 7485static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7486 struct intel_crtc_state *pipe_config)
70b23a98
VS
7487{
7488 struct drm_device *dev = crtc->base.dev;
fac5e23e 7489 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7490 int pipe = pipe_config->cpu_transcoder;
7491 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7492 struct dpll clock;
0d7b6b11 7493 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7494 int refclk = 100000;
7495
b521973b
VS
7496 /* In case of DSI, DPLL will not be used */
7497 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7498 return;
7499
a580516d 7500 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7501 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7502 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7503 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7504 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7505 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7506 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7507
7508 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7509 clock.m2 = (pll_dw0 & 0xff) << 22;
7510 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7511 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7512 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7513 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7514 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7515
dccbea3b 7516 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7517}
7518
0e8ffe1b 7519static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7520 struct intel_crtc_state *pipe_config)
0e8ffe1b 7521{
6315b5d3 7522 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7523 enum intel_display_power_domain power_domain;
0e8ffe1b 7524 uint32_t tmp;
1729050e 7525 bool ret;
0e8ffe1b 7526
1729050e
ID
7527 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7528 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7529 return false;
7530
e143a21c 7531 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7532 pipe_config->shared_dpll = NULL;
eccb140b 7533
1729050e
ID
7534 ret = false;
7535
0e8ffe1b
DV
7536 tmp = I915_READ(PIPECONF(crtc->pipe));
7537 if (!(tmp & PIPECONF_ENABLE))
1729050e 7538 goto out;
0e8ffe1b 7539
9beb5fea
TU
7540 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7541 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7542 switch (tmp & PIPECONF_BPC_MASK) {
7543 case PIPECONF_6BPC:
7544 pipe_config->pipe_bpp = 18;
7545 break;
7546 case PIPECONF_8BPC:
7547 pipe_config->pipe_bpp = 24;
7548 break;
7549 case PIPECONF_10BPC:
7550 pipe_config->pipe_bpp = 30;
7551 break;
7552 default:
7553 break;
7554 }
7555 }
7556
920a14b2 7557 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7558 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7559 pipe_config->limited_color_range = true;
7560
6315b5d3 7561 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7562 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7563
1bd1bd80 7564 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7565 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7566
2fa2fe9a
DV
7567 i9xx_get_pfit_config(crtc, pipe_config);
7568
6315b5d3 7569 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7570 /* No way to read it out on pipes B and C */
920a14b2 7571 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7572 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7573 else
7574 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7575 pipe_config->pixel_multiplier =
7576 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7577 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7578 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7579 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7580 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7581 tmp = I915_READ(DPLL(crtc->pipe));
7582 pipe_config->pixel_multiplier =
7583 ((tmp & SDVO_MULTIPLIER_MASK)
7584 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7585 } else {
7586 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7587 * port and will be fixed up in the encoder->get_config
7588 * function. */
7589 pipe_config->pixel_multiplier = 1;
7590 }
8bcc2795 7591 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7592 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7593 /*
7594 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7595 * on 830. Filter it out here so that we don't
7596 * report errors due to that.
7597 */
50a0bc90 7598 if (IS_I830(dev_priv))
1c4e0274
VS
7599 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7600
8bcc2795
DV
7601 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7602 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7603 } else {
7604 /* Mask out read-only status bits. */
7605 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7606 DPLL_PORTC_READY_MASK |
7607 DPLL_PORTB_READY_MASK);
8bcc2795 7608 }
6c49f241 7609
920a14b2 7610 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7611 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7612 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7613 vlv_crtc_clock_get(crtc, pipe_config);
7614 else
7615 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7616
0f64614d
VS
7617 /*
7618 * Normally the dotclock is filled in by the encoder .get_config()
7619 * but in case the pipe is enabled w/o any ports we need a sane
7620 * default.
7621 */
7622 pipe_config->base.adjusted_mode.crtc_clock =
7623 pipe_config->port_clock / pipe_config->pixel_multiplier;
7624
1729050e
ID
7625 ret = true;
7626
7627out:
7628 intel_display_power_put(dev_priv, power_domain);
7629
7630 return ret;
0e8ffe1b
DV
7631}
7632
c39055b0 7633static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7634{
13d83a67 7635 struct intel_encoder *encoder;
1c1a24d2 7636 int i;
74cfd7ac 7637 u32 val, final;
13d83a67 7638 bool has_lvds = false;
199e5d79 7639 bool has_cpu_edp = false;
199e5d79 7640 bool has_panel = false;
99eb6a01
KP
7641 bool has_ck505 = false;
7642 bool can_ssc = false;
1c1a24d2 7643 bool using_ssc_source = false;
13d83a67
JB
7644
7645 /* We need to take the global config into account */
c39055b0 7646 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7647 switch (encoder->type) {
7648 case INTEL_OUTPUT_LVDS:
7649 has_panel = true;
7650 has_lvds = true;
7651 break;
7652 case INTEL_OUTPUT_EDP:
7653 has_panel = true;
2de6905f 7654 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7655 has_cpu_edp = true;
7656 break;
6847d71b
PZ
7657 default:
7658 break;
13d83a67
JB
7659 }
7660 }
7661
6e266956 7662 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7663 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7664 can_ssc = has_ck505;
7665 } else {
7666 has_ck505 = false;
7667 can_ssc = true;
7668 }
7669
1c1a24d2
L
7670 /* Check if any DPLLs are using the SSC source */
7671 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7672 u32 temp = I915_READ(PCH_DPLL(i));
7673
7674 if (!(temp & DPLL_VCO_ENABLE))
7675 continue;
7676
7677 if ((temp & PLL_REF_INPUT_MASK) ==
7678 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7679 using_ssc_source = true;
7680 break;
7681 }
7682 }
7683
7684 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7685 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7686
7687 /* Ironlake: try to setup display ref clock before DPLL
7688 * enabling. This is only under driver's control after
7689 * PCH B stepping, previous chipset stepping should be
7690 * ignoring this setting.
7691 */
74cfd7ac
CW
7692 val = I915_READ(PCH_DREF_CONTROL);
7693
7694 /* As we must carefully and slowly disable/enable each source in turn,
7695 * compute the final state we want first and check if we need to
7696 * make any changes at all.
7697 */
7698 final = val;
7699 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7700 if (has_ck505)
7701 final |= DREF_NONSPREAD_CK505_ENABLE;
7702 else
7703 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7704
8c07eb68 7705 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7706 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7707 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7708
7709 if (has_panel) {
7710 final |= DREF_SSC_SOURCE_ENABLE;
7711
7712 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7713 final |= DREF_SSC1_ENABLE;
7714
7715 if (has_cpu_edp) {
7716 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7717 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7718 else
7719 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7720 } else
7721 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7722 } else if (using_ssc_source) {
7723 final |= DREF_SSC_SOURCE_ENABLE;
7724 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7725 }
7726
7727 if (final == val)
7728 return;
7729
13d83a67 7730 /* Always enable nonspread source */
74cfd7ac 7731 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7732
99eb6a01 7733 if (has_ck505)
74cfd7ac 7734 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7735 else
74cfd7ac 7736 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7737
199e5d79 7738 if (has_panel) {
74cfd7ac
CW
7739 val &= ~DREF_SSC_SOURCE_MASK;
7740 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7741
199e5d79 7742 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7743 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7744 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7745 val |= DREF_SSC1_ENABLE;
e77166b5 7746 } else
74cfd7ac 7747 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7748
7749 /* Get SSC going before enabling the outputs */
74cfd7ac 7750 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7751 POSTING_READ(PCH_DREF_CONTROL);
7752 udelay(200);
7753
74cfd7ac 7754 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7755
7756 /* Enable CPU source on CPU attached eDP */
199e5d79 7757 if (has_cpu_edp) {
99eb6a01 7758 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7759 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7760 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7761 } else
74cfd7ac 7762 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7763 } else
74cfd7ac 7764 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7765
74cfd7ac 7766 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7767 POSTING_READ(PCH_DREF_CONTROL);
7768 udelay(200);
7769 } else {
1c1a24d2 7770 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7771
74cfd7ac 7772 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7773
7774 /* Turn off CPU output */
74cfd7ac 7775 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7776
74cfd7ac 7777 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7778 POSTING_READ(PCH_DREF_CONTROL);
7779 udelay(200);
7780
1c1a24d2
L
7781 if (!using_ssc_source) {
7782 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7783
1c1a24d2
L
7784 /* Turn off the SSC source */
7785 val &= ~DREF_SSC_SOURCE_MASK;
7786 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7787
1c1a24d2
L
7788 /* Turn off SSC1 */
7789 val &= ~DREF_SSC1_ENABLE;
7790
7791 I915_WRITE(PCH_DREF_CONTROL, val);
7792 POSTING_READ(PCH_DREF_CONTROL);
7793 udelay(200);
7794 }
13d83a67 7795 }
74cfd7ac
CW
7796
7797 BUG_ON(val != final);
13d83a67
JB
7798}
7799
f31f2d55 7800static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7801{
f31f2d55 7802 uint32_t tmp;
dde86e2d 7803
0ff066a9
PZ
7804 tmp = I915_READ(SOUTH_CHICKEN2);
7805 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7806 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7807
cf3598c2
ID
7808 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7809 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7810 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7811
0ff066a9
PZ
7812 tmp = I915_READ(SOUTH_CHICKEN2);
7813 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7814 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7815
cf3598c2
ID
7816 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7817 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7818 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7819}
7820
7821/* WaMPhyProgramming:hsw */
7822static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7823{
7824 uint32_t tmp;
dde86e2d
PZ
7825
7826 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7827 tmp &= ~(0xFF << 24);
7828 tmp |= (0x12 << 24);
7829 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7830
dde86e2d
PZ
7831 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7832 tmp |= (1 << 11);
7833 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7834
7835 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7836 tmp |= (1 << 11);
7837 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7838
dde86e2d
PZ
7839 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7840 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7841 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7842
7843 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7844 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7845 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7846
0ff066a9
PZ
7847 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7848 tmp &= ~(7 << 13);
7849 tmp |= (5 << 13);
7850 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7851
0ff066a9
PZ
7852 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7853 tmp &= ~(7 << 13);
7854 tmp |= (5 << 13);
7855 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7856
7857 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7858 tmp &= ~0xFF;
7859 tmp |= 0x1C;
7860 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7861
7862 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7863 tmp &= ~0xFF;
7864 tmp |= 0x1C;
7865 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7866
7867 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7868 tmp &= ~(0xFF << 16);
7869 tmp |= (0x1C << 16);
7870 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7871
7872 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7873 tmp &= ~(0xFF << 16);
7874 tmp |= (0x1C << 16);
7875 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7876
0ff066a9
PZ
7877 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7878 tmp |= (1 << 27);
7879 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7880
0ff066a9
PZ
7881 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7882 tmp |= (1 << 27);
7883 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7884
0ff066a9
PZ
7885 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7886 tmp &= ~(0xF << 28);
7887 tmp |= (4 << 28);
7888 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7889
0ff066a9
PZ
7890 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7891 tmp &= ~(0xF << 28);
7892 tmp |= (4 << 28);
7893 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7894}
7895
2fa86a1f
PZ
7896/* Implements 3 different sequences from BSpec chapter "Display iCLK
7897 * Programming" based on the parameters passed:
7898 * - Sequence to enable CLKOUT_DP
7899 * - Sequence to enable CLKOUT_DP without spread
7900 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7901 */
c39055b0
ACO
7902static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7903 bool with_spread, bool with_fdi)
f31f2d55 7904{
2fa86a1f
PZ
7905 uint32_t reg, tmp;
7906
7907 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7908 with_spread = true;
4f8036a2
TU
7909 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7910 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7911 with_fdi = false;
f31f2d55 7912
a580516d 7913 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7914
7915 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7916 tmp &= ~SBI_SSCCTL_DISABLE;
7917 tmp |= SBI_SSCCTL_PATHALT;
7918 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7919
7920 udelay(24);
7921
2fa86a1f
PZ
7922 if (with_spread) {
7923 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7924 tmp &= ~SBI_SSCCTL_PATHALT;
7925 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7926
2fa86a1f
PZ
7927 if (with_fdi) {
7928 lpt_reset_fdi_mphy(dev_priv);
7929 lpt_program_fdi_mphy(dev_priv);
7930 }
7931 }
dde86e2d 7932
4f8036a2 7933 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7934 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7935 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7936 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7937
a580516d 7938 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7939}
7940
47701c3b 7941/* Sequence to disable CLKOUT_DP */
c39055b0 7942static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7943{
47701c3b
PZ
7944 uint32_t reg, tmp;
7945
a580516d 7946 mutex_lock(&dev_priv->sb_lock);
47701c3b 7947
4f8036a2 7948 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7949 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7950 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7951 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7952
7953 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7954 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7955 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7956 tmp |= SBI_SSCCTL_PATHALT;
7957 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7958 udelay(32);
7959 }
7960 tmp |= SBI_SSCCTL_DISABLE;
7961 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7962 }
7963
a580516d 7964 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7965}
7966
f7be2c21
VS
7967#define BEND_IDX(steps) ((50 + (steps)) / 5)
7968
7969static const uint16_t sscdivintphase[] = {
7970 [BEND_IDX( 50)] = 0x3B23,
7971 [BEND_IDX( 45)] = 0x3B23,
7972 [BEND_IDX( 40)] = 0x3C23,
7973 [BEND_IDX( 35)] = 0x3C23,
7974 [BEND_IDX( 30)] = 0x3D23,
7975 [BEND_IDX( 25)] = 0x3D23,
7976 [BEND_IDX( 20)] = 0x3E23,
7977 [BEND_IDX( 15)] = 0x3E23,
7978 [BEND_IDX( 10)] = 0x3F23,
7979 [BEND_IDX( 5)] = 0x3F23,
7980 [BEND_IDX( 0)] = 0x0025,
7981 [BEND_IDX( -5)] = 0x0025,
7982 [BEND_IDX(-10)] = 0x0125,
7983 [BEND_IDX(-15)] = 0x0125,
7984 [BEND_IDX(-20)] = 0x0225,
7985 [BEND_IDX(-25)] = 0x0225,
7986 [BEND_IDX(-30)] = 0x0325,
7987 [BEND_IDX(-35)] = 0x0325,
7988 [BEND_IDX(-40)] = 0x0425,
7989 [BEND_IDX(-45)] = 0x0425,
7990 [BEND_IDX(-50)] = 0x0525,
7991};
7992
7993/*
7994 * Bend CLKOUT_DP
7995 * steps -50 to 50 inclusive, in steps of 5
7996 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7997 * change in clock period = -(steps / 10) * 5.787 ps
7998 */
7999static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8000{
8001 uint32_t tmp;
8002 int idx = BEND_IDX(steps);
8003
8004 if (WARN_ON(steps % 5 != 0))
8005 return;
8006
8007 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8008 return;
8009
8010 mutex_lock(&dev_priv->sb_lock);
8011
8012 if (steps % 10 != 0)
8013 tmp = 0xAAAAAAAB;
8014 else
8015 tmp = 0x00000000;
8016 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8017
8018 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8019 tmp &= 0xffff0000;
8020 tmp |= sscdivintphase[idx];
8021 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8022
8023 mutex_unlock(&dev_priv->sb_lock);
8024}
8025
8026#undef BEND_IDX
8027
c39055b0 8028static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 8029{
bf8fa3d3
PZ
8030 struct intel_encoder *encoder;
8031 bool has_vga = false;
8032
c39055b0 8033 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
8034 switch (encoder->type) {
8035 case INTEL_OUTPUT_ANALOG:
8036 has_vga = true;
8037 break;
6847d71b
PZ
8038 default:
8039 break;
bf8fa3d3
PZ
8040 }
8041 }
8042
f7be2c21 8043 if (has_vga) {
c39055b0
ACO
8044 lpt_bend_clkout_dp(dev_priv, 0);
8045 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 8046 } else {
c39055b0 8047 lpt_disable_clkout_dp(dev_priv);
f7be2c21 8048 }
bf8fa3d3
PZ
8049}
8050
dde86e2d
PZ
8051/*
8052 * Initialize reference clocks when the driver loads
8053 */
c39055b0 8054void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8055{
6e266956 8056 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8057 ironlake_init_pch_refclk(dev_priv);
6e266956 8058 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8059 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8060}
8061
6ff93609 8062static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8063{
fac5e23e 8064 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8066 int pipe = intel_crtc->pipe;
c8203565
PZ
8067 uint32_t val;
8068
78114071 8069 val = 0;
c8203565 8070
6e3c9717 8071 switch (intel_crtc->config->pipe_bpp) {
c8203565 8072 case 18:
dfd07d72 8073 val |= PIPECONF_6BPC;
c8203565
PZ
8074 break;
8075 case 24:
dfd07d72 8076 val |= PIPECONF_8BPC;
c8203565
PZ
8077 break;
8078 case 30:
dfd07d72 8079 val |= PIPECONF_10BPC;
c8203565
PZ
8080 break;
8081 case 36:
dfd07d72 8082 val |= PIPECONF_12BPC;
c8203565
PZ
8083 break;
8084 default:
cc769b62
PZ
8085 /* Case prevented by intel_choose_pipe_bpp_dither. */
8086 BUG();
c8203565
PZ
8087 }
8088
6e3c9717 8089 if (intel_crtc->config->dither)
c8203565
PZ
8090 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8091
6e3c9717 8092 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8093 val |= PIPECONF_INTERLACED_ILK;
8094 else
8095 val |= PIPECONF_PROGRESSIVE;
8096
6e3c9717 8097 if (intel_crtc->config->limited_color_range)
3685a8f3 8098 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8099
c8203565
PZ
8100 I915_WRITE(PIPECONF(pipe), val);
8101 POSTING_READ(PIPECONF(pipe));
8102}
8103
6ff93609 8104static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8105{
fac5e23e 8106 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8108 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8109 u32 val = 0;
ee2b0b38 8110
391bf048 8111 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8112 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8113
6e3c9717 8114 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8115 val |= PIPECONF_INTERLACED_ILK;
8116 else
8117 val |= PIPECONF_PROGRESSIVE;
8118
702e7a56
PZ
8119 I915_WRITE(PIPECONF(cpu_transcoder), val);
8120 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8121}
8122
391bf048
JN
8123static void haswell_set_pipemisc(struct drm_crtc *crtc)
8124{
fac5e23e 8125 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b22ca995 8127 struct intel_crtc_state *config = intel_crtc->config;
756f85cf 8128
391bf048
JN
8129 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8130 u32 val = 0;
756f85cf 8131
6e3c9717 8132 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8133 case 18:
8134 val |= PIPEMISC_DITHER_6_BPC;
8135 break;
8136 case 24:
8137 val |= PIPEMISC_DITHER_8_BPC;
8138 break;
8139 case 30:
8140 val |= PIPEMISC_DITHER_10_BPC;
8141 break;
8142 case 36:
8143 val |= PIPEMISC_DITHER_12_BPC;
8144 break;
8145 default:
8146 /* Case prevented by pipe_config_set_bpp. */
8147 BUG();
8148 }
8149
6e3c9717 8150 if (intel_crtc->config->dither)
756f85cf
PZ
8151 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8152
b22ca995
SS
8153 if (config->ycbcr420) {
8154 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
8155 PIPEMISC_YUV420_ENABLE |
8156 PIPEMISC_YUV420_MODE_FULL_BLEND;
8157 }
8158
391bf048 8159 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8160 }
ee2b0b38
PZ
8161}
8162
d4b1931c
PZ
8163int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8164{
8165 /*
8166 * Account for spread spectrum to avoid
8167 * oversubscribing the link. Max center spread
8168 * is 2.5%; use 5% for safety's sake.
8169 */
8170 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8171 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8172}
8173
7429e9d4 8174static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8175{
7429e9d4 8176 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8177}
8178
b75ca6f6
ACO
8179static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8180 struct intel_crtc_state *crtc_state,
9e2c8475 8181 struct dpll *reduced_clock)
79e53945 8182{
de13a2e3 8183 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8184 struct drm_device *dev = crtc->dev;
fac5e23e 8185 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8186 u32 dpll, fp, fp2;
3d6e9ee0 8187 int factor;
79e53945 8188
c1858123 8189 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8190 factor = 21;
3d6e9ee0 8191 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8192 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8193 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8194 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8195 factor = 25;
190f68c5 8196 } else if (crtc_state->sdvo_tv_clock)
8febb297 8197 factor = 20;
c1858123 8198
b75ca6f6
ACO
8199 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8200
190f68c5 8201 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8202 fp |= FP_CB_TUNE;
8203
8204 if (reduced_clock) {
8205 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8206
b75ca6f6
ACO
8207 if (reduced_clock->m < factor * reduced_clock->n)
8208 fp2 |= FP_CB_TUNE;
8209 } else {
8210 fp2 = fp;
8211 }
9a7c7890 8212
5eddb70b 8213 dpll = 0;
2c07245f 8214
3d6e9ee0 8215 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8216 dpll |= DPLLB_MODE_LVDS;
8217 else
8218 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8219
190f68c5 8220 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8221 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8222
3d6e9ee0
VS
8223 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8224 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8225 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8226
37a5650b 8227 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8228 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8229
7d7f8633
VS
8230 /*
8231 * The high speed IO clock is only really required for
8232 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8233 * possible to share the DPLL between CRT and HDMI. Enabling
8234 * the clock needlessly does no real harm, except use up a
8235 * bit of power potentially.
8236 *
8237 * We'll limit this to IVB with 3 pipes, since it has only two
8238 * DPLLs and so DPLL sharing is the only way to get three pipes
8239 * driving PCH ports at the same time. On SNB we could do this,
8240 * and potentially avoid enabling the second DPLL, but it's not
8241 * clear if it''s a win or loss power wise. No point in doing
8242 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8243 */
8244 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8245 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8246 dpll |= DPLL_SDVO_HIGH_SPEED;
8247
a07d6787 8248 /* compute bitmask from p1 value */
190f68c5 8249 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8250 /* also FPA1 */
190f68c5 8251 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8252
190f68c5 8253 switch (crtc_state->dpll.p2) {
a07d6787
EA
8254 case 5:
8255 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8256 break;
8257 case 7:
8258 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8259 break;
8260 case 10:
8261 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8262 break;
8263 case 14:
8264 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8265 break;
79e53945
JB
8266 }
8267
3d6e9ee0
VS
8268 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8269 intel_panel_use_ssc(dev_priv))
43565a06 8270 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8271 else
8272 dpll |= PLL_REF_INPUT_DREFCLK;
8273
b75ca6f6
ACO
8274 dpll |= DPLL_VCO_ENABLE;
8275
8276 crtc_state->dpll_hw_state.dpll = dpll;
8277 crtc_state->dpll_hw_state.fp0 = fp;
8278 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8279}
8280
190f68c5
ACO
8281static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8282 struct intel_crtc_state *crtc_state)
de13a2e3 8283{
997c030c 8284 struct drm_device *dev = crtc->base.dev;
fac5e23e 8285 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8286 const struct intel_limit *limit;
997c030c 8287 int refclk = 120000;
de13a2e3 8288
dd3cd74a
ACO
8289 memset(&crtc_state->dpll_hw_state, 0,
8290 sizeof(crtc_state->dpll_hw_state));
8291
ded220e2
ACO
8292 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8293 if (!crtc_state->has_pch_encoder)
8294 return 0;
79e53945 8295
2d84d2b3 8296 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8297 if (intel_panel_use_ssc(dev_priv)) {
8298 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8299 dev_priv->vbt.lvds_ssc_freq);
8300 refclk = dev_priv->vbt.lvds_ssc_freq;
8301 }
8302
8303 if (intel_is_dual_link_lvds(dev)) {
8304 if (refclk == 100000)
8305 limit = &intel_limits_ironlake_dual_lvds_100m;
8306 else
8307 limit = &intel_limits_ironlake_dual_lvds;
8308 } else {
8309 if (refclk == 100000)
8310 limit = &intel_limits_ironlake_single_lvds_100m;
8311 else
8312 limit = &intel_limits_ironlake_single_lvds;
8313 }
8314 } else {
8315 limit = &intel_limits_ironlake_dac;
8316 }
8317
364ee29d 8318 if (!crtc_state->clock_set &&
997c030c
ACO
8319 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8320 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8321 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8322 return -EINVAL;
f47709a9 8323 }
79e53945 8324
cbaa3315 8325 ironlake_compute_dpll(crtc, crtc_state, NULL);
66e985c0 8326
efd38b68 8327 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
ded220e2
ACO
8328 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8329 pipe_name(crtc->pipe));
8330 return -EINVAL;
3fb37703 8331 }
79e53945 8332
c8f7a0db 8333 return 0;
79e53945
JB
8334}
8335
eb14cb74
VS
8336static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8337 struct intel_link_m_n *m_n)
8338{
8339 struct drm_device *dev = crtc->base.dev;
fac5e23e 8340 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8341 enum pipe pipe = crtc->pipe;
8342
8343 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8344 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8345 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8346 & ~TU_SIZE_MASK;
8347 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8348 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8349 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8350}
8351
8352static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8353 enum transcoder transcoder,
b95af8be
VK
8354 struct intel_link_m_n *m_n,
8355 struct intel_link_m_n *m2_n2)
72419203 8356{
6315b5d3 8357 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8358 enum pipe pipe = crtc->pipe;
72419203 8359
6315b5d3 8360 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8361 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8362 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8363 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8364 & ~TU_SIZE_MASK;
8365 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8366 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8367 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8368 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8369 * gen < 8) and if DRRS is supported (to make sure the
8370 * registers are not unnecessarily read).
8371 */
6315b5d3 8372 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8373 crtc->config->has_drrs) {
b95af8be
VK
8374 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8375 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8376 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8377 & ~TU_SIZE_MASK;
8378 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8379 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8380 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8381 }
eb14cb74
VS
8382 } else {
8383 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8384 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8385 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8386 & ~TU_SIZE_MASK;
8387 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8388 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8389 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8390 }
8391}
8392
8393void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8394 struct intel_crtc_state *pipe_config)
eb14cb74 8395{
681a8504 8396 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8397 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8398 else
8399 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8400 &pipe_config->dp_m_n,
8401 &pipe_config->dp_m2_n2);
eb14cb74 8402}
72419203 8403
eb14cb74 8404static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8405 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8406{
8407 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8408 &pipe_config->fdi_m_n, NULL);
72419203
DV
8409}
8410
bd2e244f 8411static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8412 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8413{
8414 struct drm_device *dev = crtc->base.dev;
fac5e23e 8415 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8416 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8417 uint32_t ps_ctrl = 0;
8418 int id = -1;
8419 int i;
bd2e244f 8420
a1b2278e
CK
8421 /* find scaler attached to this pipe */
8422 for (i = 0; i < crtc->num_scalers; i++) {
8423 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8424 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8425 id = i;
8426 pipe_config->pch_pfit.enabled = true;
8427 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8428 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8429 break;
8430 }
8431 }
bd2e244f 8432
a1b2278e
CK
8433 scaler_state->scaler_id = id;
8434 if (id >= 0) {
8435 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8436 } else {
8437 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8438 }
8439}
8440
5724dbd1
DL
8441static void
8442skylake_get_initial_plane_config(struct intel_crtc *crtc,
8443 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8444{
8445 struct drm_device *dev = crtc->base.dev;
fac5e23e 8446 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8447 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8448 int pipe = crtc->pipe;
8449 int fourcc, pixel_format;
6761dd31 8450 unsigned int aligned_height;
bc8d7dff 8451 struct drm_framebuffer *fb;
1b842c89 8452 struct intel_framebuffer *intel_fb;
bc8d7dff 8453
d9806c9f 8454 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8455 if (!intel_fb) {
bc8d7dff
DL
8456 DRM_DEBUG_KMS("failed to alloc fb\n");
8457 return;
8458 }
8459
1b842c89
DL
8460 fb = &intel_fb->base;
8461
d2e9f5fc
VS
8462 fb->dev = dev;
8463
bc8d7dff 8464 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8465 if (!(val & PLANE_CTL_ENABLE))
8466 goto error;
8467
bc8d7dff
DL
8468 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8469 fourcc = skl_format_to_fourcc(pixel_format,
8470 val & PLANE_CTL_ORDER_RGBX,
8471 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8472 fb->format = drm_format_info(fourcc);
bc8d7dff 8473
40f46283
DL
8474 tiling = val & PLANE_CTL_TILED_MASK;
8475 switch (tiling) {
8476 case PLANE_CTL_TILED_LINEAR:
2f075565 8477 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
8478 break;
8479 case PLANE_CTL_TILED_X:
8480 plane_config->tiling = I915_TILING_X;
bae781b2 8481 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8482 break;
8483 case PLANE_CTL_TILED_Y:
2e2adb05
VS
8484 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8485 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8486 else
8487 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8488 break;
8489 case PLANE_CTL_TILED_YF:
2e2adb05
VS
8490 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8491 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8492 else
8493 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8494 break;
8495 default:
8496 MISSING_CASE(tiling);
8497 goto error;
8498 }
8499
bc8d7dff
DL
8500 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8501 plane_config->base = base;
8502
8503 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8504
8505 val = I915_READ(PLANE_SIZE(pipe, 0));
8506 fb->height = ((val >> 16) & 0xfff) + 1;
8507 fb->width = ((val >> 0) & 0x1fff) + 1;
8508
8509 val = I915_READ(PLANE_STRIDE(pipe, 0));
d88c4afd 8510 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8511 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8512
d88c4afd 8513 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 8514
f37b5c2b 8515 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8516
8517 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8518 pipe_name(pipe), fb->width, fb->height,
272725c7 8519 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8520 plane_config->size);
8521
2d14030b 8522 plane_config->fb = intel_fb;
bc8d7dff
DL
8523 return;
8524
8525error:
d1a3a036 8526 kfree(intel_fb);
bc8d7dff
DL
8527}
8528
2fa2fe9a 8529static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8530 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8531{
8532 struct drm_device *dev = crtc->base.dev;
fac5e23e 8533 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8534 uint32_t tmp;
8535
8536 tmp = I915_READ(PF_CTL(crtc->pipe));
8537
8538 if (tmp & PF_ENABLE) {
fd4daa9c 8539 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8540 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8541 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8542
8543 /* We currently do not free assignements of panel fitters on
8544 * ivb/hsw (since we don't use the higher upscaling modes which
8545 * differentiates them) so just WARN about this case for now. */
5db94019 8546 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8547 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8548 PF_PIPE_SEL_IVB(crtc->pipe));
8549 }
2fa2fe9a 8550 }
79e53945
JB
8551}
8552
5724dbd1
DL
8553static void
8554ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8555 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8556{
8557 struct drm_device *dev = crtc->base.dev;
fac5e23e 8558 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8559 u32 val, base, offset;
aeee5a49 8560 int pipe = crtc->pipe;
4c6baa59 8561 int fourcc, pixel_format;
6761dd31 8562 unsigned int aligned_height;
b113d5ee 8563 struct drm_framebuffer *fb;
1b842c89 8564 struct intel_framebuffer *intel_fb;
4c6baa59 8565
42a7b088
DL
8566 val = I915_READ(DSPCNTR(pipe));
8567 if (!(val & DISPLAY_PLANE_ENABLE))
8568 return;
8569
d9806c9f 8570 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8571 if (!intel_fb) {
4c6baa59
JB
8572 DRM_DEBUG_KMS("failed to alloc fb\n");
8573 return;
8574 }
8575
1b842c89
DL
8576 fb = &intel_fb->base;
8577
d2e9f5fc
VS
8578 fb->dev = dev;
8579
6315b5d3 8580 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8581 if (val & DISPPLANE_TILED) {
49af449b 8582 plane_config->tiling = I915_TILING_X;
bae781b2 8583 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8584 }
8585 }
4c6baa59
JB
8586
8587 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8588 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8589 fb->format = drm_format_info(fourcc);
4c6baa59 8590
aeee5a49 8591 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8592 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8593 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8594 } else {
49af449b 8595 if (plane_config->tiling)
aeee5a49 8596 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8597 else
aeee5a49 8598 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8599 }
8600 plane_config->base = base;
8601
8602 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8603 fb->width = ((val >> 16) & 0xfff) + 1;
8604 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8605
8606 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8607 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8608
d88c4afd 8609 aligned_height = intel_fb_align_height(fb, 0, fb->height);
4c6baa59 8610
f37b5c2b 8611 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8612
2844a921
DL
8613 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8614 pipe_name(pipe), fb->width, fb->height,
272725c7 8615 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8616 plane_config->size);
b113d5ee 8617
2d14030b 8618 plane_config->fb = intel_fb;
4c6baa59
JB
8619}
8620
0e8ffe1b 8621static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8622 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8623{
8624 struct drm_device *dev = crtc->base.dev;
fac5e23e 8625 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8626 enum intel_display_power_domain power_domain;
0e8ffe1b 8627 uint32_t tmp;
1729050e 8628 bool ret;
0e8ffe1b 8629
1729050e
ID
8630 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8631 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8632 return false;
8633
e143a21c 8634 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8635 pipe_config->shared_dpll = NULL;
eccb140b 8636
1729050e 8637 ret = false;
0e8ffe1b
DV
8638 tmp = I915_READ(PIPECONF(crtc->pipe));
8639 if (!(tmp & PIPECONF_ENABLE))
1729050e 8640 goto out;
0e8ffe1b 8641
42571aef
VS
8642 switch (tmp & PIPECONF_BPC_MASK) {
8643 case PIPECONF_6BPC:
8644 pipe_config->pipe_bpp = 18;
8645 break;
8646 case PIPECONF_8BPC:
8647 pipe_config->pipe_bpp = 24;
8648 break;
8649 case PIPECONF_10BPC:
8650 pipe_config->pipe_bpp = 30;
8651 break;
8652 case PIPECONF_12BPC:
8653 pipe_config->pipe_bpp = 36;
8654 break;
8655 default:
8656 break;
8657 }
8658
b5a9fa09
DV
8659 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8660 pipe_config->limited_color_range = true;
8661
ab9412ba 8662 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8663 struct intel_shared_dpll *pll;
8106ddbd 8664 enum intel_dpll_id pll_id;
66e985c0 8665
88adfff1
DV
8666 pipe_config->has_pch_encoder = true;
8667
627eb5a3
DV
8668 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8669 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8670 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8671
8672 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8673
2d1fe073 8674 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8675 /*
8676 * The pipe->pch transcoder and pch transcoder->pll
8677 * mapping is fixed.
8678 */
8106ddbd 8679 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8680 } else {
8681 tmp = I915_READ(PCH_DPLL_SEL);
8682 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8683 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8684 else
8106ddbd 8685 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8686 }
66e985c0 8687
8106ddbd
ACO
8688 pipe_config->shared_dpll =
8689 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8690 pll = pipe_config->shared_dpll;
66e985c0 8691
2edd6443
ACO
8692 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8693 &pipe_config->dpll_hw_state));
c93f54cf
DV
8694
8695 tmp = pipe_config->dpll_hw_state.dpll;
8696 pipe_config->pixel_multiplier =
8697 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8698 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8699
8700 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8701 } else {
8702 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8703 }
8704
1bd1bd80 8705 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8706 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8707
2fa2fe9a
DV
8708 ironlake_get_pfit_config(crtc, pipe_config);
8709
1729050e
ID
8710 ret = true;
8711
8712out:
8713 intel_display_power_put(dev_priv, power_domain);
8714
8715 return ret;
0e8ffe1b
DV
8716}
8717
be256dc7
PZ
8718static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8719{
91c8a326 8720 struct drm_device *dev = &dev_priv->drm;
be256dc7 8721 struct intel_crtc *crtc;
be256dc7 8722
d3fcc808 8723 for_each_intel_crtc(dev, crtc)
e2c719b7 8724 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8725 pipe_name(crtc->pipe));
8726
9c3a16c8
ID
8727 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
8728 "Display power well on\n");
e2c719b7 8729 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8730 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8731 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8732 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8733 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8734 "CPU PWM1 enabled\n");
772c2a51 8735 if (IS_HASWELL(dev_priv))
e2c719b7 8736 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8737 "CPU PWM2 enabled\n");
e2c719b7 8738 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8739 "PCH PWM1 enabled\n");
e2c719b7 8740 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8741 "Utility pin enabled\n");
e2c719b7 8742 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8743
9926ada1
PZ
8744 /*
8745 * In theory we can still leave IRQs enabled, as long as only the HPD
8746 * interrupts remain enabled. We used to check for that, but since it's
8747 * gen-specific and since we only disable LCPLL after we fully disable
8748 * the interrupts, the check below should be enough.
8749 */
e2c719b7 8750 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8751}
8752
9ccd5aeb
PZ
8753static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8754{
772c2a51 8755 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8756 return I915_READ(D_COMP_HSW);
8757 else
8758 return I915_READ(D_COMP_BDW);
8759}
8760
3c4c9b81
PZ
8761static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8762{
772c2a51 8763 if (IS_HASWELL(dev_priv)) {
9f817501 8764 mutex_lock(&dev_priv->pcu_lock);
3c4c9b81
PZ
8765 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8766 val))
79cf219a 8767 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
9f817501 8768 mutex_unlock(&dev_priv->pcu_lock);
3c4c9b81 8769 } else {
9ccd5aeb
PZ
8770 I915_WRITE(D_COMP_BDW, val);
8771 POSTING_READ(D_COMP_BDW);
3c4c9b81 8772 }
be256dc7
PZ
8773}
8774
8775/*
8776 * This function implements pieces of two sequences from BSpec:
8777 * - Sequence for display software to disable LCPLL
8778 * - Sequence for display software to allow package C8+
8779 * The steps implemented here are just the steps that actually touch the LCPLL
8780 * register. Callers should take care of disabling all the display engine
8781 * functions, doing the mode unset, fixing interrupts, etc.
8782 */
6ff58d53
PZ
8783static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8784 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8785{
8786 uint32_t val;
8787
8788 assert_can_disable_lcpll(dev_priv);
8789
8790 val = I915_READ(LCPLL_CTL);
8791
8792 if (switch_to_fclk) {
8793 val |= LCPLL_CD_SOURCE_FCLK;
8794 I915_WRITE(LCPLL_CTL, val);
8795
f53dd63f
ID
8796 if (wait_for_us(I915_READ(LCPLL_CTL) &
8797 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8798 DRM_ERROR("Switching to FCLK failed\n");
8799
8800 val = I915_READ(LCPLL_CTL);
8801 }
8802
8803 val |= LCPLL_PLL_DISABLE;
8804 I915_WRITE(LCPLL_CTL, val);
8805 POSTING_READ(LCPLL_CTL);
8806
24d8441d 8807 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8808 DRM_ERROR("LCPLL still locked\n");
8809
9ccd5aeb 8810 val = hsw_read_dcomp(dev_priv);
be256dc7 8811 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8812 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8813 ndelay(100);
8814
9ccd5aeb
PZ
8815 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8816 1))
be256dc7
PZ
8817 DRM_ERROR("D_COMP RCOMP still in progress\n");
8818
8819 if (allow_power_down) {
8820 val = I915_READ(LCPLL_CTL);
8821 val |= LCPLL_POWER_DOWN_ALLOW;
8822 I915_WRITE(LCPLL_CTL, val);
8823 POSTING_READ(LCPLL_CTL);
8824 }
8825}
8826
8827/*
8828 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8829 * source.
8830 */
6ff58d53 8831static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8832{
8833 uint32_t val;
8834
8835 val = I915_READ(LCPLL_CTL);
8836
8837 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8838 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8839 return;
8840
a8a8bd54
PZ
8841 /*
8842 * Make sure we're not on PC8 state before disabling PC8, otherwise
8843 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8844 */
59bad947 8845 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8846
be256dc7
PZ
8847 if (val & LCPLL_POWER_DOWN_ALLOW) {
8848 val &= ~LCPLL_POWER_DOWN_ALLOW;
8849 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8850 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8851 }
8852
9ccd5aeb 8853 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8854 val |= D_COMP_COMP_FORCE;
8855 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8856 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8857
8858 val = I915_READ(LCPLL_CTL);
8859 val &= ~LCPLL_PLL_DISABLE;
8860 I915_WRITE(LCPLL_CTL, val);
8861
93220c08
CW
8862 if (intel_wait_for_register(dev_priv,
8863 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8864 5))
be256dc7
PZ
8865 DRM_ERROR("LCPLL not locked yet\n");
8866
8867 if (val & LCPLL_CD_SOURCE_FCLK) {
8868 val = I915_READ(LCPLL_CTL);
8869 val &= ~LCPLL_CD_SOURCE_FCLK;
8870 I915_WRITE(LCPLL_CTL, val);
8871
f53dd63f
ID
8872 if (wait_for_us((I915_READ(LCPLL_CTL) &
8873 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8874 DRM_ERROR("Switching back to LCPLL failed\n");
8875 }
215733fa 8876
59bad947 8877 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8878 intel_update_cdclk(dev_priv);
be256dc7
PZ
8879}
8880
765dab67
PZ
8881/*
8882 * Package states C8 and deeper are really deep PC states that can only be
8883 * reached when all the devices on the system allow it, so even if the graphics
8884 * device allows PC8+, it doesn't mean the system will actually get to these
8885 * states. Our driver only allows PC8+ when going into runtime PM.
8886 *
8887 * The requirements for PC8+ are that all the outputs are disabled, the power
8888 * well is disabled and most interrupts are disabled, and these are also
8889 * requirements for runtime PM. When these conditions are met, we manually do
8890 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8891 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8892 * hang the machine.
8893 *
8894 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8895 * the state of some registers, so when we come back from PC8+ we need to
8896 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8897 * need to take care of the registers kept by RC6. Notice that this happens even
8898 * if we don't put the device in PCI D3 state (which is what currently happens
8899 * because of the runtime PM support).
8900 *
8901 * For more, read "Display Sequences for Package C8" on the hardware
8902 * documentation.
8903 */
a14cb6fc 8904void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8905{
c67a470b
PZ
8906 uint32_t val;
8907
c67a470b
PZ
8908 DRM_DEBUG_KMS("Enabling package C8+\n");
8909
4f8036a2 8910 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8911 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8912 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8913 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8914 }
8915
c39055b0 8916 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8917 hsw_disable_lcpll(dev_priv, true, true);
8918}
8919
a14cb6fc 8920void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8921{
c67a470b
PZ
8922 uint32_t val;
8923
c67a470b
PZ
8924 DRM_DEBUG_KMS("Disabling package C8+\n");
8925
8926 hsw_restore_lcpll(dev_priv);
c39055b0 8927 lpt_init_pch_refclk(dev_priv);
c67a470b 8928
4f8036a2 8929 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8930 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8931 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8932 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8933 }
c67a470b
PZ
8934}
8935
190f68c5
ACO
8936static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8937 struct intel_crtc_state *crtc_state)
09b4ddf9 8938{
d7edc4e5 8939 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
44a126ba
PZ
8940 struct intel_encoder *encoder =
8941 intel_ddi_get_crtc_new_encoder(crtc_state);
8942
8943 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
8944 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8945 pipe_name(crtc->pipe));
af3997b5 8946 return -EINVAL;
44a126ba 8947 }
af3997b5 8948 }
716c2e55 8949
c8f7a0db 8950 return 0;
79e53945
JB
8951}
8952
8b0f7e06
KM
8953static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
8954 enum port port,
8955 struct intel_crtc_state *pipe_config)
8956{
8957 enum intel_dpll_id id;
8958 u32 temp;
8959
8960 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
dfbd4508 8961 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
8b0f7e06
KM
8962
8963 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
8964 return;
8965
8966 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
8967}
8968
3760b59c
S
8969static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8970 enum port port,
8971 struct intel_crtc_state *pipe_config)
8972{
8106ddbd
ACO
8973 enum intel_dpll_id id;
8974
3760b59c
S
8975 switch (port) {
8976 case PORT_A:
08250c4b 8977 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8978 break;
8979 case PORT_B:
08250c4b 8980 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8981 break;
8982 case PORT_C:
08250c4b 8983 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8984 break;
8985 default:
8986 DRM_ERROR("Incorrect port type\n");
8106ddbd 8987 return;
3760b59c 8988 }
8106ddbd
ACO
8989
8990 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8991}
8992
96b7dfb7
S
8993static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8994 enum port port,
5cec258b 8995 struct intel_crtc_state *pipe_config)
96b7dfb7 8996{
8106ddbd 8997 enum intel_dpll_id id;
a3c988ea 8998 u32 temp;
96b7dfb7
S
8999
9000 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 9001 id = temp >> (port * 3 + 1);
96b7dfb7 9002
c856052a 9003 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 9004 return;
8106ddbd
ACO
9005
9006 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9007}
9008
7d2c8175
DL
9009static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9010 enum port port,
5cec258b 9011 struct intel_crtc_state *pipe_config)
7d2c8175 9012{
8106ddbd 9013 enum intel_dpll_id id;
c856052a 9014 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 9015
c856052a 9016 switch (ddi_pll_sel) {
7d2c8175 9017 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9018 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9019 break;
9020 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9021 id = DPLL_ID_WRPLL2;
7d2c8175 9022 break;
00490c22 9023 case PORT_CLK_SEL_SPLL:
8106ddbd 9024 id = DPLL_ID_SPLL;
79bd23da 9025 break;
9d16da65
ACO
9026 case PORT_CLK_SEL_LCPLL_810:
9027 id = DPLL_ID_LCPLL_810;
9028 break;
9029 case PORT_CLK_SEL_LCPLL_1350:
9030 id = DPLL_ID_LCPLL_1350;
9031 break;
9032 case PORT_CLK_SEL_LCPLL_2700:
9033 id = DPLL_ID_LCPLL_2700;
9034 break;
8106ddbd 9035 default:
c856052a 9036 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
9037 /* fall through */
9038 case PORT_CLK_SEL_NONE:
8106ddbd 9039 return;
7d2c8175 9040 }
8106ddbd
ACO
9041
9042 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9043}
9044
cf30429e
JN
9045static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9046 struct intel_crtc_state *pipe_config,
d8fc70b7 9047 u64 *power_domain_mask)
cf30429e
JN
9048{
9049 struct drm_device *dev = crtc->base.dev;
fac5e23e 9050 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9051 enum intel_display_power_domain power_domain;
9052 u32 tmp;
9053
d9a7bc67
ID
9054 /*
9055 * The pipe->transcoder mapping is fixed with the exception of the eDP
9056 * transcoder handled below.
9057 */
cf30429e
JN
9058 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9059
9060 /*
9061 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9062 * consistency and less surprising code; it's in always on power).
9063 */
9064 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9065 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9066 enum pipe trans_edp_pipe;
9067 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9068 default:
9069 WARN(1, "unknown pipe linked to edp transcoder\n");
9070 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9071 case TRANS_DDI_EDP_INPUT_A_ON:
9072 trans_edp_pipe = PIPE_A;
9073 break;
9074 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9075 trans_edp_pipe = PIPE_B;
9076 break;
9077 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9078 trans_edp_pipe = PIPE_C;
9079 break;
9080 }
9081
9082 if (trans_edp_pipe == crtc->pipe)
9083 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9084 }
9085
9086 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9087 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9088 return false;
d8fc70b7 9089 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9090
9091 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9092
9093 return tmp & PIPECONF_ENABLE;
9094}
9095
4d1de975
JN
9096static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9097 struct intel_crtc_state *pipe_config,
d8fc70b7 9098 u64 *power_domain_mask)
4d1de975
JN
9099{
9100 struct drm_device *dev = crtc->base.dev;
fac5e23e 9101 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9102 enum intel_display_power_domain power_domain;
9103 enum port port;
9104 enum transcoder cpu_transcoder;
9105 u32 tmp;
9106
4d1de975
JN
9107 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9108 if (port == PORT_A)
9109 cpu_transcoder = TRANSCODER_DSI_A;
9110 else
9111 cpu_transcoder = TRANSCODER_DSI_C;
9112
9113 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9114 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9115 continue;
d8fc70b7 9116 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9117
db18b6a6
ID
9118 /*
9119 * The PLL needs to be enabled with a valid divider
9120 * configuration, otherwise accessing DSI registers will hang
9121 * the machine. See BSpec North Display Engine
9122 * registers/MIPI[BXT]. We can break out here early, since we
9123 * need the same DSI PLL to be enabled for both DSI ports.
9124 */
9125 if (!intel_dsi_pll_is_enabled(dev_priv))
9126 break;
9127
4d1de975
JN
9128 /* XXX: this works for video mode only */
9129 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9130 if (!(tmp & DPI_ENABLE))
9131 continue;
9132
9133 tmp = I915_READ(MIPI_CTRL(port));
9134 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9135 continue;
9136
9137 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9138 break;
9139 }
9140
d7edc4e5 9141 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9142}
9143
26804afd 9144static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9145 struct intel_crtc_state *pipe_config)
26804afd 9146{
6315b5d3 9147 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9148 struct intel_shared_dpll *pll;
26804afd
DV
9149 enum port port;
9150 uint32_t tmp;
9151
9152 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9153
9154 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9155
8b0f7e06
KM
9156 if (IS_CANNONLAKE(dev_priv))
9157 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9158 else if (IS_GEN9_BC(dev_priv))
96b7dfb7 9159 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9160 else if (IS_GEN9_LP(dev_priv))
3760b59c 9161 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9162 else
9163 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9164
8106ddbd
ACO
9165 pll = pipe_config->shared_dpll;
9166 if (pll) {
2edd6443
ACO
9167 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9168 &pipe_config->dpll_hw_state));
d452c5b6
DV
9169 }
9170
26804afd
DV
9171 /*
9172 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9173 * DDI E. So just check whether this pipe is wired to DDI E and whether
9174 * the PCH transcoder is on.
9175 */
6315b5d3 9176 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9177 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9178 pipe_config->has_pch_encoder = true;
9179
9180 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9181 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9182 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9183
9184 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9185 }
9186}
9187
0e8ffe1b 9188static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9189 struct intel_crtc_state *pipe_config)
0e8ffe1b 9190{
6315b5d3 9191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9192 enum intel_display_power_domain power_domain;
d8fc70b7 9193 u64 power_domain_mask;
cf30429e 9194 bool active;
0e8ffe1b 9195
e79dfb51 9196 intel_crtc_init_scalers(crtc, pipe_config);
5fb9dadf 9197
1729050e
ID
9198 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9199 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9200 return false;
d8fc70b7 9201 power_domain_mask = BIT_ULL(power_domain);
1729050e 9202
8106ddbd 9203 pipe_config->shared_dpll = NULL;
c0d43d62 9204
cf30429e 9205 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9206
cc3f90f0 9207 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9208 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9209 WARN_ON(active);
9210 active = true;
4d1de975
JN
9211 }
9212
cf30429e 9213 if (!active)
1729050e 9214 goto out;
0e8ffe1b 9215
d7edc4e5 9216 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9217 haswell_get_ddi_port_state(crtc, pipe_config);
9218 intel_get_pipe_timings(crtc, pipe_config);
9219 }
627eb5a3 9220
bc58be60 9221 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9222
05dc698c
LL
9223 pipe_config->gamma_mode =
9224 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9225
bd30ca2d 9226 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
b22ca995
SS
9227 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
9228 bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
9229
bd30ca2d 9230 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
b22ca995
SS
9231 bool blend_mode_420 = tmp &
9232 PIPEMISC_YUV420_MODE_FULL_BLEND;
9233
9234 pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
9235 if (pipe_config->ycbcr420 != clrspace_yuv ||
9236 pipe_config->ycbcr420 != blend_mode_420)
9237 DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
9238 } else if (clrspace_yuv) {
9239 DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
9240 }
9241 }
9242
1729050e
ID
9243 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9244 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9245 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9246 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9247 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9248 else
1c132b44 9249 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9250 }
88adfff1 9251
772c2a51 9252 if (IS_HASWELL(dev_priv))
e59150dc
JB
9253 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9254 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9255
4d1de975
JN
9256 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9257 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9258 pipe_config->pixel_multiplier =
9259 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9260 } else {
9261 pipe_config->pixel_multiplier = 1;
9262 }
6c49f241 9263
1729050e
ID
9264out:
9265 for_each_power_domain(power_domain, power_domain_mask)
9266 intel_display_power_put(dev_priv, power_domain);
9267
cf30429e 9268 return active;
0e8ffe1b
DV
9269}
9270
cd5dcbf1 9271static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
9272{
9273 struct drm_i915_private *dev_priv =
9274 to_i915(plane_state->base.plane->dev);
9275 const struct drm_framebuffer *fb = plane_state->base.fb;
9276 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9277 u32 base;
9278
9279 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9280 base = obj->phys_handle->busaddr;
9281 else
9282 base = intel_plane_ggtt_offset(plane_state);
9283
1e7b4fd8
VS
9284 base += plane_state->main.offset;
9285
1cecc830
VS
9286 /* ILK+ do this automagically */
9287 if (HAS_GMCH_DISPLAY(dev_priv) &&
a82256bc 9288 plane_state->base.rotation & DRM_MODE_ROTATE_180)
1cecc830
VS
9289 base += (plane_state->base.crtc_h *
9290 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9291
9292 return base;
9293}
9294
ed270223
VS
9295static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9296{
9297 int x = plane_state->base.crtc_x;
9298 int y = plane_state->base.crtc_y;
9299 u32 pos = 0;
9300
9301 if (x < 0) {
9302 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9303 x = -x;
9304 }
9305 pos |= x << CURSOR_X_SHIFT;
9306
9307 if (y < 0) {
9308 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9309 y = -y;
9310 }
9311 pos |= y << CURSOR_Y_SHIFT;
9312
9313 return pos;
9314}
9315
3637ecf0
VS
9316static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9317{
9318 const struct drm_mode_config *config =
9319 &plane_state->base.plane->dev->mode_config;
9320 int width = plane_state->base.crtc_w;
9321 int height = plane_state->base.crtc_h;
9322
9323 return width > 0 && width <= config->cursor_width &&
9324 height > 0 && height <= config->cursor_height;
9325}
9326
659056f2
VS
9327static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9328 struct intel_plane_state *plane_state)
9329{
9330 const struct drm_framebuffer *fb = plane_state->base.fb;
1e7b4fd8
VS
9331 int src_x, src_y;
9332 u32 offset;
659056f2
VS
9333 int ret;
9334
9335 ret = drm_plane_helper_check_state(&plane_state->base,
9336 &plane_state->clip,
9337 DRM_PLANE_HELPER_NO_SCALING,
9338 DRM_PLANE_HELPER_NO_SCALING,
9339 true, true);
9340 if (ret)
9341 return ret;
9342
9343 if (!fb)
9344 return 0;
9345
9346 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9347 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9348 return -EINVAL;
9349 }
9350
1e7b4fd8
VS
9351 src_x = plane_state->base.src_x >> 16;
9352 src_y = plane_state->base.src_y >> 16;
9353
9354 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9355 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9356
9357 if (src_x != 0 || src_y != 0) {
9358 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9359 return -EINVAL;
9360 }
9361
9362 plane_state->main.offset = offset;
9363
659056f2
VS
9364 return 0;
9365}
9366
292889e1
VS
9367static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9368 const struct intel_plane_state *plane_state)
9369{
1e1bb871 9370 const struct drm_framebuffer *fb = plane_state->base.fb;
292889e1 9371
292889e1
VS
9372 return CURSOR_ENABLE |
9373 CURSOR_GAMMA_ENABLE |
9374 CURSOR_FORMAT_ARGB |
1e1bb871 9375 CURSOR_STRIDE(fb->pitches[0]);
292889e1
VS
9376}
9377
659056f2
VS
9378static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9379{
659056f2 9380 int width = plane_state->base.crtc_w;
659056f2
VS
9381
9382 /*
9383 * 845g/865g are only limited by the width of their cursors,
9384 * the height is arbitrary up to the precision of the register.
9385 */
3637ecf0 9386 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
9387}
9388
9389static int i845_check_cursor(struct intel_plane *plane,
9390 struct intel_crtc_state *crtc_state,
9391 struct intel_plane_state *plane_state)
9392{
9393 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9394 int ret;
9395
9396 ret = intel_check_cursor(crtc_state, plane_state);
9397 if (ret)
9398 return ret;
9399
9400 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9401 if (!fb)
659056f2
VS
9402 return 0;
9403
9404 /* Check for which cursor types we support */
9405 if (!i845_cursor_size_ok(plane_state)) {
9406 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9407 plane_state->base.crtc_w,
9408 plane_state->base.crtc_h);
9409 return -EINVAL;
9410 }
9411
1e1bb871 9412 switch (fb->pitches[0]) {
292889e1
VS
9413 case 256:
9414 case 512:
9415 case 1024:
9416 case 2048:
9417 break;
1e1bb871
VS
9418 default:
9419 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9420 fb->pitches[0]);
9421 return -EINVAL;
292889e1
VS
9422 }
9423
659056f2
VS
9424 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9425
9426 return 0;
292889e1
VS
9427}
9428
b2d03b0d
VS
9429static void i845_update_cursor(struct intel_plane *plane,
9430 const struct intel_crtc_state *crtc_state,
55a08b3f 9431 const struct intel_plane_state *plane_state)
560b85bb 9432{
cd5dcbf1 9433 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
9434 u32 cntl = 0, base = 0, pos = 0, size = 0;
9435 unsigned long irqflags;
560b85bb 9436
936e71e3 9437 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9438 unsigned int width = plane_state->base.crtc_w;
9439 unsigned int height = plane_state->base.crtc_h;
dc41c154 9440
a0864d59 9441 cntl = plane_state->ctl;
dc41c154 9442 size = (height << 12) | width;
560b85bb 9443
b2d03b0d
VS
9444 base = intel_cursor_base(plane_state);
9445 pos = intel_cursor_position(plane_state);
4b0e333e 9446 }
560b85bb 9447
b2d03b0d 9448 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 9449
e11ffddb
VS
9450 /* On these chipsets we can only modify the base/size/stride
9451 * whilst the cursor is disabled.
9452 */
9453 if (plane->cursor.base != base ||
9454 plane->cursor.size != size ||
9455 plane->cursor.cntl != cntl) {
dd584fc0 9456 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
dd584fc0 9457 I915_WRITE_FW(CURBASE(PIPE_A), base);
dd584fc0 9458 I915_WRITE_FW(CURSIZE, size);
b2d03b0d 9459 I915_WRITE_FW(CURPOS(PIPE_A), pos);
dd584fc0 9460 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
75343a44 9461
e11ffddb
VS
9462 plane->cursor.base = base;
9463 plane->cursor.size = size;
9464 plane->cursor.cntl = cntl;
9465 } else {
9466 I915_WRITE_FW(CURPOS(PIPE_A), pos);
560b85bb 9467 }
e11ffddb 9468
75343a44 9469 POSTING_READ_FW(CURCNTR(PIPE_A));
b2d03b0d
VS
9470
9471 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9472}
9473
9474static void i845_disable_cursor(struct intel_plane *plane,
9475 struct intel_crtc *crtc)
9476{
9477 i845_update_cursor(plane, NULL, NULL);
560b85bb
CW
9478}
9479
292889e1
VS
9480static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9481 const struct intel_plane_state *plane_state)
9482{
9483 struct drm_i915_private *dev_priv =
9484 to_i915(plane_state->base.plane->dev);
9485 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
292889e1
VS
9486 u32 cntl;
9487
9488 cntl = MCURSOR_GAMMA_ENABLE;
9489
9490 if (HAS_DDI(dev_priv))
9491 cntl |= CURSOR_PIPE_CSC_ENABLE;
9492
d509e28b 9493 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1
VS
9494
9495 switch (plane_state->base.crtc_w) {
9496 case 64:
9497 cntl |= CURSOR_MODE_64_ARGB_AX;
9498 break;
9499 case 128:
9500 cntl |= CURSOR_MODE_128_ARGB_AX;
9501 break;
9502 case 256:
9503 cntl |= CURSOR_MODE_256_ARGB_AX;
9504 break;
9505 default:
9506 MISSING_CASE(plane_state->base.crtc_w);
9507 return 0;
9508 }
9509
c2c446ad 9510 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
292889e1
VS
9511 cntl |= CURSOR_ROTATE_180;
9512
9513 return cntl;
9514}
9515
659056f2 9516static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 9517{
024faac7
VS
9518 struct drm_i915_private *dev_priv =
9519 to_i915(plane_state->base.plane->dev);
659056f2
VS
9520 int width = plane_state->base.crtc_w;
9521 int height = plane_state->base.crtc_h;
4b0e333e 9522
3637ecf0 9523 if (!intel_cursor_size_ok(plane_state))
659056f2 9524 return false;
4398ad45 9525
024faac7
VS
9526 /* Cursor width is limited to a few power-of-two sizes */
9527 switch (width) {
659056f2
VS
9528 case 256:
9529 case 128:
659056f2
VS
9530 case 64:
9531 break;
9532 default:
9533 return false;
65a21cd6 9534 }
4b0e333e 9535
024faac7
VS
9536 /*
9537 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9538 * height from 8 lines up to the cursor width, when the
9539 * cursor is not rotated. Everything else requires square
9540 * cursors.
9541 */
9542 if (HAS_CUR_FBC(dev_priv) &&
a82256bc 9543 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
9544 if (height < 8 || height > width)
9545 return false;
9546 } else {
9547 if (height != width)
9548 return false;
9549 }
99d1f387 9550
659056f2 9551 return true;
65a21cd6
JB
9552}
9553
659056f2
VS
9554static int i9xx_check_cursor(struct intel_plane *plane,
9555 struct intel_crtc_state *crtc_state,
9556 struct intel_plane_state *plane_state)
cda4b7d3 9557{
659056f2
VS
9558 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9559 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2 9560 enum pipe pipe = plane->pipe;
659056f2 9561 int ret;
cda4b7d3 9562
659056f2
VS
9563 ret = intel_check_cursor(crtc_state, plane_state);
9564 if (ret)
9565 return ret;
cda4b7d3 9566
659056f2 9567 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9568 if (!fb)
659056f2 9569 return 0;
55a08b3f 9570
659056f2
VS
9571 /* Check for which cursor types we support */
9572 if (!i9xx_cursor_size_ok(plane_state)) {
9573 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9574 plane_state->base.crtc_w,
9575 plane_state->base.crtc_h);
9576 return -EINVAL;
cda4b7d3 9577 }
cda4b7d3 9578
1e1bb871
VS
9579 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9580 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9581 fb->pitches[0], plane_state->base.crtc_w);
9582 return -EINVAL;
659056f2 9583 }
dd584fc0 9584
659056f2
VS
9585 /*
9586 * There's something wrong with the cursor on CHV pipe C.
9587 * If it straddles the left edge of the screen then
9588 * moving it away from the edge or disabling it often
9589 * results in a pipe underrun, and often that can lead to
9590 * dead pipe (constant underrun reported, and it scans
9591 * out just a solid color). To recover from that, the
9592 * display power well must be turned off and on again.
9593 * Refuse the put the cursor into that compromised position.
9594 */
9595 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9596 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9597 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9598 return -EINVAL;
9599 }
5efb3e28 9600
659056f2 9601 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 9602
659056f2 9603 return 0;
cda4b7d3
CW
9604}
9605
b2d03b0d
VS
9606static void i9xx_update_cursor(struct intel_plane *plane,
9607 const struct intel_crtc_state *crtc_state,
55a08b3f 9608 const struct intel_plane_state *plane_state)
dc41c154 9609{
cd5dcbf1
VS
9610 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9611 enum pipe pipe = plane->pipe;
024faac7 9612 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 9613 unsigned long irqflags;
dc41c154 9614
b2d03b0d 9615 if (plane_state && plane_state->base.visible) {
a0864d59 9616 cntl = plane_state->ctl;
dc41c154 9617
024faac7
VS
9618 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9619 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
dc41c154 9620
b2d03b0d
VS
9621 base = intel_cursor_base(plane_state);
9622 pos = intel_cursor_position(plane_state);
9623 }
9624
9625 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9626
e11ffddb
VS
9627 /*
9628 * On some platforms writing CURCNTR first will also
9629 * cause CURPOS to be armed by the CURBASE write.
9630 * Without the CURCNTR write the CURPOS write would
8753d2bc
VS
9631 * arm itself. Thus we always start the full update
9632 * with a CURCNTR write.
9633 *
9634 * On other platforms CURPOS always requires the
9635 * CURBASE write to arm the update. Additonally
9636 * a write to any of the cursor register will cancel
9637 * an already armed cursor update. Thus leaving out
9638 * the CURBASE write after CURPOS could lead to a
9639 * cursor that doesn't appear to move, or even change
9640 * shape. Thus we always write CURBASE.
e11ffddb
VS
9641 *
9642 * CURCNTR and CUR_FBC_CTL are always
9643 * armed by the CURBASE write only.
9644 */
9645 if (plane->cursor.base != base ||
9646 plane->cursor.size != fbc_ctl ||
9647 plane->cursor.cntl != cntl) {
dd584fc0 9648 I915_WRITE_FW(CURCNTR(pipe), cntl);
e11ffddb
VS
9649 if (HAS_CUR_FBC(dev_priv))
9650 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
b2d03b0d 9651 I915_WRITE_FW(CURPOS(pipe), pos);
75343a44
VS
9652 I915_WRITE_FW(CURBASE(pipe), base);
9653
e11ffddb
VS
9654 plane->cursor.base = base;
9655 plane->cursor.size = fbc_ctl;
9656 plane->cursor.cntl = cntl;
dc41c154 9657 } else {
e11ffddb 9658 I915_WRITE_FW(CURPOS(pipe), pos);
8753d2bc 9659 I915_WRITE_FW(CURBASE(pipe), base);
dc41c154
VS
9660 }
9661
dd584fc0 9662 POSTING_READ_FW(CURBASE(pipe));
99d1f387 9663
b2d03b0d 9664 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
9665}
9666
b2d03b0d
VS
9667static void i9xx_disable_cursor(struct intel_plane *plane,
9668 struct intel_crtc *crtc)
cda4b7d3 9669{
b2d03b0d 9670 i9xx_update_cursor(plane, NULL, NULL);
dc41c154
VS
9671}
9672
dc41c154 9673
79e53945 9674/* VESA 640x480x72Hz mode to set on the pipe */
bacdcd55 9675static const struct drm_display_mode load_detect_mode = {
79e53945
JB
9676 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9677 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9678};
9679
a8bb6818 9680struct drm_framebuffer *
24dbf51a
CW
9681intel_framebuffer_create(struct drm_i915_gem_object *obj,
9682 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9683{
9684 struct intel_framebuffer *intel_fb;
9685 int ret;
9686
9687 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9688 if (!intel_fb)
d2dff872 9689 return ERR_PTR(-ENOMEM);
d2dff872 9690
24dbf51a 9691 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9692 if (ret)
9693 goto err;
d2dff872
CW
9694
9695 return &intel_fb->base;
dcb1394e 9696
dd4916c5 9697err:
dd4916c5 9698 kfree(intel_fb);
dd4916c5 9699 return ERR_PTR(ret);
d2dff872
CW
9700}
9701
9702static u32
9703intel_framebuffer_pitch_for_width(int width, int bpp)
9704{
9705 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9706 return ALIGN(pitch, 64);
9707}
9708
9709static u32
bacdcd55 9710intel_framebuffer_size_for_mode(const struct drm_display_mode *mode, int bpp)
d2dff872
CW
9711{
9712 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9713 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9714}
9715
9716static struct drm_framebuffer *
9717intel_framebuffer_create_for_mode(struct drm_device *dev,
bacdcd55 9718 const struct drm_display_mode *mode,
d2dff872
CW
9719 int depth, int bpp)
9720{
dcb1394e 9721 struct drm_framebuffer *fb;
d2dff872 9722 struct drm_i915_gem_object *obj;
0fed39bd 9723 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9724
12d79d78 9725 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9726 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9727 if (IS_ERR(obj))
9728 return ERR_CAST(obj);
d2dff872
CW
9729
9730 mode_cmd.width = mode->hdisplay;
9731 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9732 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9733 bpp);
5ca0c34a 9734 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9735
24dbf51a 9736 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9737 if (IS_ERR(fb))
f0cd5182 9738 i915_gem_object_put(obj);
dcb1394e
LW
9739
9740 return fb;
d2dff872
CW
9741}
9742
9743static struct drm_framebuffer *
9744mode_fits_in_fbdev(struct drm_device *dev,
bacdcd55 9745 const struct drm_display_mode *mode)
d2dff872 9746{
0695726e 9747#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9748 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9749 struct drm_i915_gem_object *obj;
9750 struct drm_framebuffer *fb;
9751
4c0e5528 9752 if (!dev_priv->fbdev)
d2dff872
CW
9753 return NULL;
9754
4c0e5528 9755 if (!dev_priv->fbdev->fb)
d2dff872
CW
9756 return NULL;
9757
4c0e5528
DV
9758 obj = dev_priv->fbdev->fb->obj;
9759 BUG_ON(!obj);
9760
8bcd4553 9761 fb = &dev_priv->fbdev->fb->base;
01f2c773 9762 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9763 fb->format->cpp[0] * 8))
d2dff872
CW
9764 return NULL;
9765
01f2c773 9766 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9767 return NULL;
9768
c3ed1103 9769 drm_framebuffer_get(fb);
d2dff872 9770 return fb;
4520f53a
DV
9771#else
9772 return NULL;
9773#endif
d2dff872
CW
9774}
9775
d3a40d1b
ACO
9776static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9777 struct drm_crtc *crtc,
bacdcd55 9778 const struct drm_display_mode *mode,
d3a40d1b
ACO
9779 struct drm_framebuffer *fb,
9780 int x, int y)
9781{
9782 struct drm_plane_state *plane_state;
9783 int hdisplay, vdisplay;
9784 int ret;
9785
9786 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9787 if (IS_ERR(plane_state))
9788 return PTR_ERR(plane_state);
9789
9790 if (mode)
196cd5d3 9791 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9792 else
9793 hdisplay = vdisplay = 0;
9794
9795 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9796 if (ret)
9797 return ret;
9798 drm_atomic_set_fb_for_plane(plane_state, fb);
9799 plane_state->crtc_x = 0;
9800 plane_state->crtc_y = 0;
9801 plane_state->crtc_w = hdisplay;
9802 plane_state->crtc_h = vdisplay;
9803 plane_state->src_x = x << 16;
9804 plane_state->src_y = y << 16;
9805 plane_state->src_w = hdisplay << 16;
9806 plane_state->src_h = vdisplay << 16;
9807
9808 return 0;
9809}
9810
6c5ed5ae 9811int intel_get_load_detect_pipe(struct drm_connector *connector,
bacdcd55 9812 const struct drm_display_mode *mode,
6c5ed5ae
ML
9813 struct intel_load_detect_pipe *old,
9814 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9815{
9816 struct intel_crtc *intel_crtc;
d2434ab7
DV
9817 struct intel_encoder *intel_encoder =
9818 intel_attached_encoder(connector);
79e53945 9819 struct drm_crtc *possible_crtc;
4ef69c7a 9820 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9821 struct drm_crtc *crtc = NULL;
9822 struct drm_device *dev = encoder->dev;
0f0f74bc 9823 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9824 struct drm_framebuffer *fb;
51fd371b 9825 struct drm_mode_config *config = &dev->mode_config;
edde3617 9826 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9827 struct drm_connector_state *connector_state;
4be07317 9828 struct intel_crtc_state *crtc_state;
51fd371b 9829 int ret, i = -1;
79e53945 9830
d2dff872 9831 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9832 connector->base.id, connector->name,
8e329a03 9833 encoder->base.id, encoder->name);
d2dff872 9834
edde3617
ML
9835 old->restore_state = NULL;
9836
6c5ed5ae 9837 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 9838
79e53945
JB
9839 /*
9840 * Algorithm gets a little messy:
7a5e4805 9841 *
79e53945
JB
9842 * - if the connector already has an assigned crtc, use it (but make
9843 * sure it's on first)
7a5e4805 9844 *
79e53945
JB
9845 * - try to find the first unused crtc that can drive this connector,
9846 * and use that if we find one
79e53945
JB
9847 */
9848
9849 /* See if we already have a CRTC for this connector */
edde3617
ML
9850 if (connector->state->crtc) {
9851 crtc = connector->state->crtc;
8261b191 9852
51fd371b 9853 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9854 if (ret)
ad3c558f 9855 goto fail;
8261b191
CW
9856
9857 /* Make sure the crtc and connector are running */
edde3617 9858 goto found;
79e53945
JB
9859 }
9860
9861 /* Find an unused one (if possible) */
70e1e0ec 9862 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9863 i++;
9864 if (!(encoder->possible_crtcs & (1 << i)))
9865 continue;
edde3617
ML
9866
9867 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9868 if (ret)
9869 goto fail;
9870
9871 if (possible_crtc->state->enable) {
9872 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9873 continue;
edde3617 9874 }
a459249c
VS
9875
9876 crtc = possible_crtc;
9877 break;
79e53945
JB
9878 }
9879
9880 /*
9881 * If we didn't find an unused CRTC, don't use any.
9882 */
9883 if (!crtc) {
7173188d 9884 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f4bf77b4 9885 ret = -ENODEV;
ad3c558f 9886 goto fail;
79e53945
JB
9887 }
9888
edde3617
ML
9889found:
9890 intel_crtc = to_intel_crtc(crtc);
9891
4d02e2de
DV
9892 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9893 if (ret)
ad3c558f 9894 goto fail;
79e53945 9895
83a57153 9896 state = drm_atomic_state_alloc(dev);
edde3617
ML
9897 restore_state = drm_atomic_state_alloc(dev);
9898 if (!state || !restore_state) {
9899 ret = -ENOMEM;
9900 goto fail;
9901 }
83a57153
ACO
9902
9903 state->acquire_ctx = ctx;
edde3617 9904 restore_state->acquire_ctx = ctx;
83a57153 9905
944b0c76
ACO
9906 connector_state = drm_atomic_get_connector_state(state, connector);
9907 if (IS_ERR(connector_state)) {
9908 ret = PTR_ERR(connector_state);
9909 goto fail;
9910 }
9911
edde3617
ML
9912 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9913 if (ret)
9914 goto fail;
944b0c76 9915
4be07317
ACO
9916 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9917 if (IS_ERR(crtc_state)) {
9918 ret = PTR_ERR(crtc_state);
9919 goto fail;
9920 }
9921
49d6fa21 9922 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9923
6492711d
CW
9924 if (!mode)
9925 mode = &load_detect_mode;
79e53945 9926
d2dff872
CW
9927 /* We need a framebuffer large enough to accommodate all accesses
9928 * that the plane may generate whilst we perform load detection.
9929 * We can not rely on the fbcon either being present (we get called
9930 * during its initialisation to detect all boot displays, or it may
9931 * not even exist) or that it is large enough to satisfy the
9932 * requested mode.
9933 */
94352cf9
DV
9934 fb = mode_fits_in_fbdev(dev, mode);
9935 if (fb == NULL) {
d2dff872 9936 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9937 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9938 } else
9939 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9940 if (IS_ERR(fb)) {
d2dff872 9941 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
f4bf77b4 9942 ret = PTR_ERR(fb);
412b61d8 9943 goto fail;
79e53945 9944 }
79e53945 9945
d3a40d1b 9946 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
2b3a2e9f 9947 drm_framebuffer_put(fb);
d3a40d1b
ACO
9948 if (ret)
9949 goto fail;
9950
edde3617
ML
9951 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9952 if (ret)
9953 goto fail;
9954
9955 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9956 if (!ret)
9957 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9958 if (!ret)
9959 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9960 if (ret) {
9961 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9962 goto fail;
9963 }
8c7b5ccb 9964
3ba86073
ML
9965 ret = drm_atomic_commit(state);
9966 if (ret) {
6492711d 9967 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9968 goto fail;
79e53945 9969 }
edde3617
ML
9970
9971 old->restore_state = restore_state;
7abbd11f 9972 drm_atomic_state_put(state);
7173188d 9973
79e53945 9974 /* let the connector get through one full cycle before testing */
0f0f74bc 9975 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9976 return true;
412b61d8 9977
ad3c558f 9978fail:
7fb71c8f
CW
9979 if (state) {
9980 drm_atomic_state_put(state);
9981 state = NULL;
9982 }
9983 if (restore_state) {
9984 drm_atomic_state_put(restore_state);
9985 restore_state = NULL;
9986 }
83a57153 9987
6c5ed5ae
ML
9988 if (ret == -EDEADLK)
9989 return ret;
51fd371b 9990
412b61d8 9991 return false;
79e53945
JB
9992}
9993
d2434ab7 9994void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9995 struct intel_load_detect_pipe *old,
9996 struct drm_modeset_acquire_ctx *ctx)
79e53945 9997{
d2434ab7
DV
9998 struct intel_encoder *intel_encoder =
9999 intel_attached_encoder(connector);
4ef69c7a 10000 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10001 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10002 int ret;
79e53945 10003
d2dff872 10004 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10005 connector->base.id, connector->name,
8e329a03 10006 encoder->base.id, encoder->name);
d2dff872 10007
edde3617 10008 if (!state)
0622a53c 10009 return;
79e53945 10010
581e49fe 10011 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 10012 if (ret)
edde3617 10013 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 10014 drm_atomic_state_put(state);
79e53945
JB
10015}
10016
da4a1efa 10017static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10018 const struct intel_crtc_state *pipe_config)
da4a1efa 10019{
fac5e23e 10020 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10021 u32 dpll = pipe_config->dpll_hw_state.dpll;
10022
10023 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10024 return dev_priv->vbt.lvds_ssc_freq;
6e266956 10025 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 10026 return 120000;
5db94019 10027 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
10028 return 96000;
10029 else
10030 return 48000;
10031}
10032
79e53945 10033/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10034static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10035 struct intel_crtc_state *pipe_config)
79e53945 10036{
f1f644dc 10037 struct drm_device *dev = crtc->base.dev;
fac5e23e 10038 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10039 int pipe = pipe_config->cpu_transcoder;
293623f7 10040 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10041 u32 fp;
9e2c8475 10042 struct dpll clock;
dccbea3b 10043 int port_clock;
da4a1efa 10044 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10045
10046 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10047 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10048 else
293623f7 10049 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10050
10051 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 10052 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
10053 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10054 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10055 } else {
10056 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10057 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10058 }
10059
5db94019 10060 if (!IS_GEN2(dev_priv)) {
9b1e14f4 10061 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
10062 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10063 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10064 else
10065 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10066 DPLL_FPA01_P1_POST_DIV_SHIFT);
10067
10068 switch (dpll & DPLL_MODE_MASK) {
10069 case DPLLB_MODE_DAC_SERIAL:
10070 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10071 5 : 10;
10072 break;
10073 case DPLLB_MODE_LVDS:
10074 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10075 7 : 14;
10076 break;
10077 default:
28c97730 10078 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10079 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10080 return;
79e53945
JB
10081 }
10082
9b1e14f4 10083 if (IS_PINEVIEW(dev_priv))
dccbea3b 10084 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10085 else
dccbea3b 10086 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10087 } else {
50a0bc90 10088 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 10089 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10090
10091 if (is_lvds) {
10092 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10093 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10094
10095 if (lvds & LVDS_CLKB_POWER_UP)
10096 clock.p2 = 7;
10097 else
10098 clock.p2 = 14;
79e53945
JB
10099 } else {
10100 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10101 clock.p1 = 2;
10102 else {
10103 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10104 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10105 }
10106 if (dpll & PLL_P2_DIVIDE_BY_4)
10107 clock.p2 = 4;
10108 else
10109 clock.p2 = 2;
79e53945 10110 }
da4a1efa 10111
dccbea3b 10112 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10113 }
10114
18442d08
VS
10115 /*
10116 * This value includes pixel_multiplier. We will use
241bfc38 10117 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10118 * encoder's get_config() function.
10119 */
dccbea3b 10120 pipe_config->port_clock = port_clock;
f1f644dc
JB
10121}
10122
6878da05
VS
10123int intel_dotclock_calculate(int link_freq,
10124 const struct intel_link_m_n *m_n)
f1f644dc 10125{
f1f644dc
JB
10126 /*
10127 * The calculation for the data clock is:
1041a02f 10128 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10129 * But we want to avoid losing precison if possible, so:
1041a02f 10130 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10131 *
10132 * and the link clock is simpler:
1041a02f 10133 * link_clock = (m * link_clock) / n
f1f644dc
JB
10134 */
10135
6878da05
VS
10136 if (!m_n->link_n)
10137 return 0;
f1f644dc 10138
3123698f 10139 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6878da05 10140}
f1f644dc 10141
18442d08 10142static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10143 struct intel_crtc_state *pipe_config)
6878da05 10144{
e3b247da 10145 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10146
18442d08
VS
10147 /* read out port_clock from the DPLL */
10148 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10149
f1f644dc 10150 /*
e3b247da
VS
10151 * In case there is an active pipe without active ports,
10152 * we may need some idea for the dotclock anyway.
10153 * Calculate one based on the FDI configuration.
79e53945 10154 */
2d112de7 10155 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10156 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10157 &pipe_config->fdi_m_n);
79e53945
JB
10158}
10159
de330815
VS
10160/* Returns the currently programmed mode of the given encoder. */
10161struct drm_display_mode *
10162intel_encoder_current_mode(struct intel_encoder *encoder)
79e53945 10163{
de330815
VS
10164 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10165 struct intel_crtc_state *crtc_state;
79e53945 10166 struct drm_display_mode *mode;
de330815
VS
10167 struct intel_crtc *crtc;
10168 enum pipe pipe;
10169
10170 if (!encoder->get_hw_state(encoder, &pipe))
10171 return NULL;
10172
10173 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
79e53945
JB
10174
10175 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10176 if (!mode)
10177 return NULL;
10178
de330815
VS
10179 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10180 if (!crtc_state) {
3f36b937
TU
10181 kfree(mode);
10182 return NULL;
10183 }
10184
de330815 10185 crtc_state->base.crtc = &crtc->base;
79e53945 10186
de330815
VS
10187 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10188 kfree(crtc_state);
10189 kfree(mode);
10190 return NULL;
10191 }
79e53945 10192
de330815 10193 encoder->get_config(encoder, crtc_state);
79e53945 10194
de330815 10195 intel_mode_from_pipe_config(mode, crtc_state);
79e53945 10196
de330815 10197 kfree(crtc_state);
3f36b937 10198
79e53945
JB
10199 return mode;
10200}
10201
10202static void intel_crtc_destroy(struct drm_crtc *crtc)
10203{
10204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10205
10206 drm_crtc_cleanup(crtc);
10207 kfree(intel_crtc);
10208}
10209
5a21b665
DV
10210/**
10211 * intel_wm_need_update - Check whether watermarks need updating
10212 * @plane: drm plane
10213 * @state: new plane state
10214 *
10215 * Check current plane state versus the new one to determine whether
10216 * watermarks need to be recalculated.
10217 *
10218 * Returns true or false.
10219 */
10220static bool intel_wm_need_update(struct drm_plane *plane,
10221 struct drm_plane_state *state)
10222{
10223 struct intel_plane_state *new = to_intel_plane_state(state);
10224 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10225
10226 /* Update watermarks on tiling or size changes. */
936e71e3 10227 if (new->base.visible != cur->base.visible)
5a21b665
DV
10228 return true;
10229
10230 if (!cur->base.fb || !new->base.fb)
10231 return false;
10232
bae781b2 10233 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10234 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10235 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10236 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10237 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10238 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10239 return true;
10240
10241 return false;
10242}
10243
b2b55502 10244static bool needs_scaling(const struct intel_plane_state *state)
5a21b665 10245{
936e71e3
VS
10246 int src_w = drm_rect_width(&state->base.src) >> 16;
10247 int src_h = drm_rect_height(&state->base.src) >> 16;
10248 int dst_w = drm_rect_width(&state->base.dst);
10249 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10250
10251 return (src_w != dst_w || src_h != dst_h);
10252}
d21fbe87 10253
b2b55502
VS
10254int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10255 struct drm_crtc_state *crtc_state,
10256 const struct intel_plane_state *old_plane_state,
da20eabd
ML
10257 struct drm_plane_state *plane_state)
10258{
ab1d3a0e 10259 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10260 struct drm_crtc *crtc = crtc_state->crtc;
10261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 10262 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 10263 struct drm_device *dev = crtc->dev;
ed4a6a7c 10264 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 10265 bool mode_changed = needs_modeset(crtc_state);
b2b55502 10266 bool was_crtc_enabled = old_crtc_state->base.active;
da20eabd 10267 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10268 bool turn_off, turn_on, visible, was_visible;
10269 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10270 int ret;
da20eabd 10271
e9728bd8 10272 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
10273 ret = skl_update_scaler_plane(
10274 to_intel_crtc_state(crtc_state),
10275 to_intel_plane_state(plane_state));
10276 if (ret)
10277 return ret;
10278 }
10279
936e71e3 10280 was_visible = old_plane_state->base.visible;
1d4258db 10281 visible = plane_state->visible;
da20eabd
ML
10282
10283 if (!was_crtc_enabled && WARN_ON(was_visible))
10284 was_visible = false;
10285
35c08f43
ML
10286 /*
10287 * Visibility is calculated as if the crtc was on, but
10288 * after scaler setup everything depends on it being off
10289 * when the crtc isn't active.
f818ffea
VS
10290 *
10291 * FIXME this is wrong for watermarks. Watermarks should also
10292 * be computed as if the pipe would be active. Perhaps move
10293 * per-plane wm computation to the .check_plane() hook, and
10294 * only combine the results from all planes in the current place?
35c08f43 10295 */
e9728bd8 10296 if (!is_crtc_enabled) {
1d4258db 10297 plane_state->visible = visible = false;
e9728bd8
VS
10298 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10299 }
da20eabd
ML
10300
10301 if (!was_visible && !visible)
10302 return 0;
10303
e8861675
ML
10304 if (fb != old_plane_state->base.fb)
10305 pipe_config->fb_changed = true;
10306
da20eabd
ML
10307 turn_off = was_visible && (!visible || mode_changed);
10308 turn_on = visible && (!was_visible || mode_changed);
10309
72660ce0 10310 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
10311 intel_crtc->base.base.id, intel_crtc->base.name,
10312 plane->base.base.id, plane->base.name,
72660ce0 10313 fb ? fb->base.id : -1);
da20eabd 10314
72660ce0 10315 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 10316 plane->base.base.id, plane->base.name,
72660ce0 10317 was_visible, visible,
da20eabd
ML
10318 turn_off, turn_on, mode_changed);
10319
caed361d 10320 if (turn_on) {
04548cba 10321 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10322 pipe_config->update_wm_pre = true;
caed361d
VS
10323
10324 /* must disable cxsr around plane enable/disable */
e9728bd8 10325 if (plane->id != PLANE_CURSOR)
caed361d
VS
10326 pipe_config->disable_cxsr = true;
10327 } else if (turn_off) {
04548cba 10328 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10329 pipe_config->update_wm_post = true;
92826fcd 10330
852eb00d 10331 /* must disable cxsr around plane enable/disable */
e9728bd8 10332 if (plane->id != PLANE_CURSOR)
ab1d3a0e 10333 pipe_config->disable_cxsr = true;
e9728bd8 10334 } else if (intel_wm_need_update(&plane->base, plane_state)) {
04548cba 10335 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df
VS
10336 /* FIXME bollocks */
10337 pipe_config->update_wm_pre = true;
10338 pipe_config->update_wm_post = true;
10339 }
852eb00d 10340 }
da20eabd 10341
8be6ca85 10342 if (visible || was_visible)
e9728bd8 10343 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 10344
31ae71fc
ML
10345 /*
10346 * WaCxSRDisabledForSpriteScaling:ivb
10347 *
10348 * cstate->update_wm was already set above, so this flag will
10349 * take effect when we commit and program watermarks.
10350 */
e9728bd8 10351 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10352 needs_scaling(to_intel_plane_state(plane_state)) &&
10353 !needs_scaling(old_plane_state))
10354 pipe_config->disable_lp_wm = true;
d21fbe87 10355
da20eabd
ML
10356 return 0;
10357}
10358
6d3a1ce7
ML
10359static bool encoders_cloneable(const struct intel_encoder *a,
10360 const struct intel_encoder *b)
10361{
10362 /* masks could be asymmetric, so check both ways */
10363 return a == b || (a->cloneable & (1 << b->type) &&
10364 b->cloneable & (1 << a->type));
10365}
10366
10367static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10368 struct intel_crtc *crtc,
10369 struct intel_encoder *encoder)
10370{
10371 struct intel_encoder *source_encoder;
10372 struct drm_connector *connector;
10373 struct drm_connector_state *connector_state;
10374 int i;
10375
aa5e9b47 10376 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
10377 if (connector_state->crtc != &crtc->base)
10378 continue;
10379
10380 source_encoder =
10381 to_intel_encoder(connector_state->best_encoder);
10382 if (!encoders_cloneable(encoder, source_encoder))
10383 return false;
10384 }
10385
10386 return true;
10387}
10388
6d3a1ce7
ML
10389static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10390 struct drm_crtc_state *crtc_state)
10391{
cf5a15be 10392 struct drm_device *dev = crtc->dev;
fac5e23e 10393 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10395 struct intel_crtc_state *pipe_config =
10396 to_intel_crtc_state(crtc_state);
6d3a1ce7 10397 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10398 int ret;
6d3a1ce7
ML
10399 bool mode_changed = needs_modeset(crtc_state);
10400
852eb00d 10401 if (mode_changed && !crtc_state->active)
caed361d 10402 pipe_config->update_wm_post = true;
eddfcbcd 10403
ad421372
ML
10404 if (mode_changed && crtc_state->enable &&
10405 dev_priv->display.crtc_compute_clock &&
8106ddbd 10406 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10407 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10408 pipe_config);
10409 if (ret)
10410 return ret;
10411 }
10412
82cf435b
LL
10413 if (crtc_state->color_mgmt_changed) {
10414 ret = intel_color_check(crtc, crtc_state);
10415 if (ret)
10416 return ret;
e7852a4b
LL
10417
10418 /*
10419 * Changing color management on Intel hardware is
10420 * handled as part of planes update.
10421 */
10422 crtc_state->planes_changed = true;
82cf435b
LL
10423 }
10424
e435d6e5 10425 ret = 0;
86c8bbbe 10426 if (dev_priv->display.compute_pipe_wm) {
e3bddded 10427 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
10428 if (ret) {
10429 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
10430 return ret;
10431 }
10432 }
10433
10434 if (dev_priv->display.compute_intermediate_wm &&
10435 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10436 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10437 return 0;
10438
10439 /*
10440 * Calculate 'intermediate' watermarks that satisfy both the
10441 * old state and the new state. We can program these
10442 * immediately.
10443 */
6315b5d3 10444 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
10445 intel_crtc,
10446 pipe_config);
10447 if (ret) {
10448 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 10449 return ret;
ed4a6a7c 10450 }
e3d5457c
VS
10451 } else if (dev_priv->display.compute_intermediate_wm) {
10452 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10453 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
10454 }
10455
6315b5d3 10456 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
10457 if (mode_changed)
10458 ret = skl_update_scaler_crtc(pipe_config);
10459
73b0ca8e
MK
10460 if (!ret)
10461 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10462 pipe_config);
e435d6e5 10463 if (!ret)
6ebc6923 10464 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
10465 pipe_config);
10466 }
10467
10468 return ret;
6d3a1ce7
ML
10469}
10470
65b38e0d 10471static const struct drm_crtc_helper_funcs intel_helper_funcs = {
5a21b665
DV
10472 .atomic_begin = intel_begin_crtc_commit,
10473 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 10474 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
10475};
10476
d29b2f9d
ACO
10477static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10478{
10479 struct intel_connector *connector;
f9e905ca 10480 struct drm_connector_list_iter conn_iter;
d29b2f9d 10481
f9e905ca
DV
10482 drm_connector_list_iter_begin(dev, &conn_iter);
10483 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f
DV
10484 if (connector->base.state->crtc)
10485 drm_connector_unreference(&connector->base);
10486
d29b2f9d
ACO
10487 if (connector->base.encoder) {
10488 connector->base.state->best_encoder =
10489 connector->base.encoder;
10490 connector->base.state->crtc =
10491 connector->base.encoder->crtc;
8863dc7f
DV
10492
10493 drm_connector_reference(&connector->base);
d29b2f9d
ACO
10494 } else {
10495 connector->base.state->best_encoder = NULL;
10496 connector->base.state->crtc = NULL;
10497 }
10498 }
f9e905ca 10499 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
10500}
10501
050f7aeb 10502static void
eba905b2 10503connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 10504 struct intel_crtc_state *pipe_config)
050f7aeb 10505{
6a2a5c5d 10506 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
10507 int bpp = pipe_config->pipe_bpp;
10508
10509 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
10510 connector->base.base.id,
10511 connector->base.name);
050f7aeb
DV
10512
10513 /* Don't use an invalid EDID bpc value */
6a2a5c5d 10514 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 10515 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
10516 bpp, info->bpc * 3);
10517 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
10518 }
10519
196f954e 10520 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 10521 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
10522 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10523 bpp);
10524 pipe_config->pipe_bpp = 24;
050f7aeb
DV
10525 }
10526}
10527
4e53c2e0 10528static int
050f7aeb 10529compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 10530 struct intel_crtc_state *pipe_config)
4e53c2e0 10531{
9beb5fea 10532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 10533 struct drm_atomic_state *state;
da3ced29
ACO
10534 struct drm_connector *connector;
10535 struct drm_connector_state *connector_state;
1486017f 10536 int bpp, i;
4e53c2e0 10537
9beb5fea
TU
10538 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10539 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 10540 bpp = 10*3;
9beb5fea 10541 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
10542 bpp = 12*3;
10543 else
10544 bpp = 8*3;
10545
4e53c2e0 10546
4e53c2e0
DV
10547 pipe_config->pipe_bpp = bpp;
10548
1486017f
ACO
10549 state = pipe_config->base.state;
10550
4e53c2e0 10551 /* Clamp display bpp to EDID value */
aa5e9b47 10552 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 10553 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
10554 continue;
10555
da3ced29
ACO
10556 connected_sink_compute_bpp(to_intel_connector(connector),
10557 pipe_config);
4e53c2e0
DV
10558 }
10559
10560 return bpp;
10561}
10562
644db711
DV
10563static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10564{
10565 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10566 "type: 0x%x flags: 0x%x\n",
1342830c 10567 mode->crtc_clock,
644db711
DV
10568 mode->crtc_hdisplay, mode->crtc_hsync_start,
10569 mode->crtc_hsync_end, mode->crtc_htotal,
10570 mode->crtc_vdisplay, mode->crtc_vsync_start,
10571 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10572}
10573
f6982332
TU
10574static inline void
10575intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 10576 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 10577{
a4309657
TU
10578 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10579 id, lane_count,
f6982332
TU
10580 m_n->gmch_m, m_n->gmch_n,
10581 m_n->link_m, m_n->link_n, m_n->tu);
10582}
10583
40b2be41
VS
10584#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
10585
10586static const char * const output_type_str[] = {
10587 OUTPUT_TYPE(UNUSED),
10588 OUTPUT_TYPE(ANALOG),
10589 OUTPUT_TYPE(DVO),
10590 OUTPUT_TYPE(SDVO),
10591 OUTPUT_TYPE(LVDS),
10592 OUTPUT_TYPE(TVOUT),
10593 OUTPUT_TYPE(HDMI),
10594 OUTPUT_TYPE(DP),
10595 OUTPUT_TYPE(EDP),
10596 OUTPUT_TYPE(DSI),
10597 OUTPUT_TYPE(UNKNOWN),
10598 OUTPUT_TYPE(DP_MST),
10599};
10600
10601#undef OUTPUT_TYPE
10602
10603static void snprintf_output_types(char *buf, size_t len,
10604 unsigned int output_types)
10605{
10606 char *str = buf;
10607 int i;
10608
10609 str[0] = '\0';
10610
10611 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
10612 int r;
10613
10614 if ((output_types & BIT(i)) == 0)
10615 continue;
10616
10617 r = snprintf(str, len, "%s%s",
10618 str != buf ? "," : "", output_type_str[i]);
10619 if (r >= len)
10620 break;
10621 str += r;
10622 len -= r;
10623
10624 output_types &= ~BIT(i);
10625 }
10626
10627 WARN_ON_ONCE(output_types != 0);
10628}
10629
c0b03411 10630static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 10631 struct intel_crtc_state *pipe_config,
c0b03411
DV
10632 const char *context)
10633{
6a60cd87 10634 struct drm_device *dev = crtc->base.dev;
4f8036a2 10635 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
10636 struct drm_plane *plane;
10637 struct intel_plane *intel_plane;
10638 struct intel_plane_state *state;
10639 struct drm_framebuffer *fb;
40b2be41 10640 char buf[64];
6a60cd87 10641
66766e4f
TU
10642 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
10643 crtc->base.base.id, crtc->base.name, context);
c0b03411 10644
40b2be41
VS
10645 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
10646 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
10647 buf, pipe_config->output_types);
10648
2c89429e
TU
10649 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
10650 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 10651 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
10652
10653 if (pipe_config->has_pch_encoder)
10654 intel_dump_m_n_config(pipe_config, "fdi",
10655 pipe_config->fdi_lanes,
10656 &pipe_config->fdi_m_n);
f6982332 10657
b22ca995
SS
10658 if (pipe_config->ycbcr420)
10659 DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
10660
f6982332 10661 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
10662 intel_dump_m_n_config(pipe_config, "dp m_n",
10663 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
10664 if (pipe_config->has_drrs)
10665 intel_dump_m_n_config(pipe_config, "dp m2_n2",
10666 pipe_config->lane_count,
10667 &pipe_config->dp_m2_n2);
f6982332 10668 }
b95af8be 10669
55072d19 10670 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 10671 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 10672
c0b03411 10673 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 10674 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 10675 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
10676 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10677 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 10678 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 10679 pipe_config->port_clock,
a7d1b3f4
VS
10680 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
10681 pipe_config->pixel_rate);
dd2f616d
TU
10682
10683 if (INTEL_GEN(dev_priv) >= 9)
10684 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
10685 crtc->num_scalers,
10686 pipe_config->scaler_state.scaler_users,
10687 pipe_config->scaler_state.scaler_id);
a74f8375
TU
10688
10689 if (HAS_GMCH_DISPLAY(dev_priv))
10690 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10691 pipe_config->gmch_pfit.control,
10692 pipe_config->gmch_pfit.pgm_ratios,
10693 pipe_config->gmch_pfit.lvds_border_bits);
10694 else
10695 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
10696 pipe_config->pch_pfit.pos,
10697 pipe_config->pch_pfit.size,
08c4d7fc 10698 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 10699
2c89429e
TU
10700 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
10701 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 10702
f50b79f0 10703 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 10704
6a60cd87
CK
10705 DRM_DEBUG_KMS("planes on this crtc\n");
10706 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 10707 struct drm_format_name_buf format_name;
6a60cd87
CK
10708 intel_plane = to_intel_plane(plane);
10709 if (intel_plane->pipe != crtc->pipe)
10710 continue;
10711
10712 state = to_intel_plane_state(plane->state);
10713 fb = state->base.fb;
10714 if (!fb) {
1d577e02
VS
10715 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
10716 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
10717 continue;
10718 }
10719
dd2f616d
TU
10720 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
10721 plane->base.id, plane->name,
b3c11ac2 10722 fb->base.id, fb->width, fb->height,
438b74a5 10723 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
10724 if (INTEL_GEN(dev_priv) >= 9)
10725 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
10726 state->scaler_id,
10727 state->base.src.x1 >> 16,
10728 state->base.src.y1 >> 16,
10729 drm_rect_width(&state->base.src) >> 16,
10730 drm_rect_height(&state->base.src) >> 16,
10731 state->base.dst.x1, state->base.dst.y1,
10732 drm_rect_width(&state->base.dst),
10733 drm_rect_height(&state->base.dst));
6a60cd87 10734 }
c0b03411
DV
10735}
10736
5448a00d 10737static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 10738{
5448a00d 10739 struct drm_device *dev = state->dev;
da3ced29 10740 struct drm_connector *connector;
2fd96b41 10741 struct drm_connector_list_iter conn_iter;
00f0b378 10742 unsigned int used_ports = 0;
477321e0 10743 unsigned int used_mst_ports = 0;
00f0b378
VS
10744
10745 /*
10746 * Walk the connector list instead of the encoder
10747 * list to detect the problem on ddi platforms
10748 * where there's just one encoder per digital port.
10749 */
2fd96b41
GP
10750 drm_connector_list_iter_begin(dev, &conn_iter);
10751 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
10752 struct drm_connector_state *connector_state;
10753 struct intel_encoder *encoder;
10754
10755 connector_state = drm_atomic_get_existing_connector_state(state, connector);
10756 if (!connector_state)
10757 connector_state = connector->state;
10758
5448a00d 10759 if (!connector_state->best_encoder)
00f0b378
VS
10760 continue;
10761
5448a00d
ACO
10762 encoder = to_intel_encoder(connector_state->best_encoder);
10763
10764 WARN_ON(!connector_state->crtc);
00f0b378
VS
10765
10766 switch (encoder->type) {
10767 unsigned int port_mask;
10768 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 10769 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 10770 break;
cca0502b 10771 case INTEL_OUTPUT_DP:
00f0b378
VS
10772 case INTEL_OUTPUT_HDMI:
10773 case INTEL_OUTPUT_EDP:
10774 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10775
10776 /* the same port mustn't appear more than once */
10777 if (used_ports & port_mask)
10778 return false;
10779
10780 used_ports |= port_mask;
477321e0
VS
10781 break;
10782 case INTEL_OUTPUT_DP_MST:
10783 used_mst_ports |=
10784 1 << enc_to_mst(&encoder->base)->primary->port;
10785 break;
00f0b378
VS
10786 default:
10787 break;
10788 }
10789 }
2fd96b41 10790 drm_connector_list_iter_end(&conn_iter);
00f0b378 10791
477321e0
VS
10792 /* can't mix MST and SST/HDMI on the same port */
10793 if (used_ports & used_mst_ports)
10794 return false;
10795
00f0b378
VS
10796 return true;
10797}
10798
83a57153
ACO
10799static void
10800clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10801{
ff32c54e
VS
10802 struct drm_i915_private *dev_priv =
10803 to_i915(crtc_state->base.crtc->dev);
663a3640 10804 struct intel_crtc_scaler_state scaler_state;
4978cc93 10805 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 10806 struct intel_shared_dpll *shared_dpll;
ff32c54e 10807 struct intel_crtc_wm_state wm_state;
6e644626 10808 bool force_thru, ips_force_disable;
83a57153 10809
7546a384
ACO
10810 /* FIXME: before the switch to atomic started, a new pipe_config was
10811 * kzalloc'd. Code that depends on any field being zero should be
10812 * fixed, so that the crtc_state can be safely duplicated. For now,
10813 * only fields that are know to not cause problems are preserved. */
10814
663a3640 10815 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
10816 shared_dpll = crtc_state->shared_dpll;
10817 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 10818 force_thru = crtc_state->pch_pfit.force_thru;
6e644626 10819 ips_force_disable = crtc_state->ips_force_disable;
04548cba
VS
10820 if (IS_G4X(dev_priv) ||
10821 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 10822 wm_state = crtc_state->wm;
4978cc93 10823
d2fa80a5
CW
10824 /* Keep base drm_crtc_state intact, only clear our extended struct */
10825 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
10826 memset(&crtc_state->base + 1, 0,
10827 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 10828
663a3640 10829 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
10830 crtc_state->shared_dpll = shared_dpll;
10831 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 10832 crtc_state->pch_pfit.force_thru = force_thru;
6e644626 10833 crtc_state->ips_force_disable = ips_force_disable;
04548cba
VS
10834 if (IS_G4X(dev_priv) ||
10835 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 10836 crtc_state->wm = wm_state;
83a57153
ACO
10837}
10838
548ee15b 10839static int
b8cecdf5 10840intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 10841 struct intel_crtc_state *pipe_config)
ee7b9f93 10842{
b359283a 10843 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 10844 struct intel_encoder *encoder;
da3ced29 10845 struct drm_connector *connector;
0b901879 10846 struct drm_connector_state *connector_state;
d328c9d7 10847 int base_bpp, ret = -EINVAL;
0b901879 10848 int i;
e29c22c0 10849 bool retry = true;
ee7b9f93 10850
83a57153 10851 clear_intel_crtc_state(pipe_config);
7758a113 10852
e143a21c
DV
10853 pipe_config->cpu_transcoder =
10854 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 10855
2960bc9c
ID
10856 /*
10857 * Sanitize sync polarity flags based on requested ones. If neither
10858 * positive or negative polarity is requested, treat this as meaning
10859 * negative polarity.
10860 */
2d112de7 10861 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10862 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 10863 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 10864
2d112de7 10865 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10866 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 10867 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 10868
d328c9d7
DV
10869 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10870 pipe_config);
10871 if (base_bpp < 0)
4e53c2e0
DV
10872 goto fail;
10873
e41a56be
VS
10874 /*
10875 * Determine the real pipe dimensions. Note that stereo modes can
10876 * increase the actual pipe size due to the frame doubling and
10877 * insertion of additional space for blanks between the frame. This
10878 * is stored in the crtc timings. We use the requested mode to do this
10879 * computation to clearly distinguish it from the adjusted mode, which
10880 * can be changed by the connectors in the below retry loop.
10881 */
196cd5d3 10882 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
10883 &pipe_config->pipe_src_w,
10884 &pipe_config->pipe_src_h);
e41a56be 10885
aa5e9b47 10886 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
10887 if (connector_state->crtc != crtc)
10888 continue;
10889
10890 encoder = to_intel_encoder(connector_state->best_encoder);
10891
e25148d0
VS
10892 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
10893 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10894 goto fail;
10895 }
10896
253c84c8
VS
10897 /*
10898 * Determine output_types before calling the .compute_config()
10899 * hooks so that the hooks can use this information safely.
10900 */
10901 pipe_config->output_types |= 1 << encoder->type;
10902 }
10903
e29c22c0 10904encoder_retry:
ef1b460d 10905 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10906 pipe_config->port_clock = 0;
ef1b460d 10907 pipe_config->pixel_multiplier = 1;
ff9a6750 10908
135c81b8 10909 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
10910 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10911 CRTC_STEREO_DOUBLE);
135c81b8 10912
7758a113
DV
10913 /* Pass our mode to the connectors and the CRTC to give them a chance to
10914 * adjust it according to limitations or connector properties, and also
10915 * a chance to reject the mode entirely.
47f1c6c9 10916 */
aa5e9b47 10917 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 10918 if (connector_state->crtc != crtc)
7758a113 10919 continue;
7ae89233 10920
0b901879
ACO
10921 encoder = to_intel_encoder(connector_state->best_encoder);
10922
0a478c27 10923 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 10924 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10925 goto fail;
10926 }
ee7b9f93 10927 }
47f1c6c9 10928
ff9a6750
DV
10929 /* Set default port clock if not overwritten by the encoder. Needs to be
10930 * done afterwards in case the encoder adjusts the mode. */
10931 if (!pipe_config->port_clock)
2d112de7 10932 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 10933 * pipe_config->pixel_multiplier;
ff9a6750 10934
a43f6e0f 10935 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10936 if (ret < 0) {
7758a113
DV
10937 DRM_DEBUG_KMS("CRTC fixup failed\n");
10938 goto fail;
ee7b9f93 10939 }
e29c22c0
DV
10940
10941 if (ret == RETRY) {
10942 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10943 ret = -EINVAL;
10944 goto fail;
10945 }
10946
10947 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10948 retry = false;
10949 goto encoder_retry;
10950 }
10951
e8fa4270 10952 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
10953 * only enable it on 6bpc panels and when its not a compliance
10954 * test requesting 6bpc video pattern.
10955 */
10956 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
10957 !pipe_config->dither_force_disable;
62f0ace5 10958 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 10959 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 10960
7758a113 10961fail:
548ee15b 10962 return ret;
ee7b9f93 10963}
47f1c6c9 10964
ea9d758d 10965static void
4740b0f2 10966intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 10967{
0a9ab303 10968 struct drm_crtc *crtc;
aa5e9b47 10969 struct drm_crtc_state *new_crtc_state;
8a75d157 10970 int i;
ea9d758d 10971
7668851f 10972 /* Double check state. */
aa5e9b47
ML
10973 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10974 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
fc467a22 10975
61067a5e
ML
10976 /*
10977 * Update legacy state to satisfy fbc code. This can
10978 * be removed when fbc uses the atomic state.
10979 */
10980 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
10981 struct drm_plane_state *plane_state = crtc->primary->state;
10982
10983 crtc->primary->fb = plane_state->fb;
10984 crtc->x = plane_state->src_x >> 16;
10985 crtc->y = plane_state->src_y >> 16;
10986 }
ea9d758d 10987 }
ea9d758d
DV
10988}
10989
3bd26263 10990static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10991{
3bd26263 10992 int diff;
f1f644dc
JB
10993
10994 if (clock1 == clock2)
10995 return true;
10996
10997 if (!clock1 || !clock2)
10998 return false;
10999
11000 diff = abs(clock1 - clock2);
11001
11002 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11003 return true;
11004
11005 return false;
11006}
11007
cfb23ed6
ML
11008static bool
11009intel_compare_m_n(unsigned int m, unsigned int n,
11010 unsigned int m2, unsigned int n2,
11011 bool exact)
11012{
11013 if (m == m2 && n == n2)
11014 return true;
11015
11016 if (exact || !m || !n || !m2 || !n2)
11017 return false;
11018
11019 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11020
31d10b57
ML
11021 if (n > n2) {
11022 while (n > n2) {
cfb23ed6
ML
11023 m2 <<= 1;
11024 n2 <<= 1;
11025 }
31d10b57
ML
11026 } else if (n < n2) {
11027 while (n < n2) {
cfb23ed6
ML
11028 m <<= 1;
11029 n <<= 1;
11030 }
11031 }
11032
31d10b57
ML
11033 if (n != n2)
11034 return false;
11035
11036 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11037}
11038
11039static bool
11040intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11041 struct intel_link_m_n *m2_n2,
11042 bool adjust)
11043{
11044 if (m_n->tu == m2_n2->tu &&
11045 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11046 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11047 intel_compare_m_n(m_n->link_m, m_n->link_n,
11048 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11049 if (adjust)
11050 *m2_n2 = *m_n;
11051
11052 return true;
11053 }
11054
11055 return false;
11056}
11057
4e8048f8
TU
11058static void __printf(3, 4)
11059pipe_config_err(bool adjust, const char *name, const char *format, ...)
11060{
11061 char *level;
11062 unsigned int category;
11063 struct va_format vaf;
11064 va_list args;
11065
11066 if (adjust) {
11067 level = KERN_DEBUG;
11068 category = DRM_UT_KMS;
11069 } else {
11070 level = KERN_ERR;
11071 category = DRM_UT_NONE;
11072 }
11073
11074 va_start(args, format);
11075 vaf.fmt = format;
11076 vaf.va = &args;
11077
11078 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11079
11080 va_end(args);
11081}
11082
0e8ffe1b 11083static bool
6315b5d3 11084intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11085 struct intel_crtc_state *current_config,
cfb23ed6
ML
11086 struct intel_crtc_state *pipe_config,
11087 bool adjust)
0e8ffe1b 11088{
cfb23ed6
ML
11089 bool ret = true;
11090
66e985c0
DV
11091#define PIPE_CONF_CHECK_X(name) \
11092 if (current_config->name != pipe_config->name) { \
4e8048f8 11093 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11094 "(expected 0x%08x, found 0x%08x)\n", \
11095 current_config->name, \
11096 pipe_config->name); \
cfb23ed6 11097 ret = false; \
66e985c0
DV
11098 }
11099
08a24034
DV
11100#define PIPE_CONF_CHECK_I(name) \
11101 if (current_config->name != pipe_config->name) { \
4e8048f8 11102 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11103 "(expected %i, found %i)\n", \
11104 current_config->name, \
11105 pipe_config->name); \
cfb23ed6
ML
11106 ret = false; \
11107 }
11108
8106ddbd
ACO
11109#define PIPE_CONF_CHECK_P(name) \
11110 if (current_config->name != pipe_config->name) { \
4e8048f8 11111 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11112 "(expected %p, found %p)\n", \
11113 current_config->name, \
11114 pipe_config->name); \
11115 ret = false; \
11116 }
11117
cfb23ed6
ML
11118#define PIPE_CONF_CHECK_M_N(name) \
11119 if (!intel_compare_link_m_n(&current_config->name, \
11120 &pipe_config->name,\
11121 adjust)) { \
4e8048f8 11122 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11123 "(expected tu %i gmch %i/%i link %i/%i, " \
11124 "found tu %i, gmch %i/%i link %i/%i)\n", \
11125 current_config->name.tu, \
11126 current_config->name.gmch_m, \
11127 current_config->name.gmch_n, \
11128 current_config->name.link_m, \
11129 current_config->name.link_n, \
11130 pipe_config->name.tu, \
11131 pipe_config->name.gmch_m, \
11132 pipe_config->name.gmch_n, \
11133 pipe_config->name.link_m, \
11134 pipe_config->name.link_n); \
11135 ret = false; \
11136 }
11137
55c561a7
DV
11138/* This is required for BDW+ where there is only one set of registers for
11139 * switching between high and low RR.
11140 * This macro can be used whenever a comparison has to be made between one
11141 * hw state and multiple sw state variables.
11142 */
cfb23ed6
ML
11143#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11144 if (!intel_compare_link_m_n(&current_config->name, \
11145 &pipe_config->name, adjust) && \
11146 !intel_compare_link_m_n(&current_config->alt_name, \
11147 &pipe_config->name, adjust)) { \
4e8048f8 11148 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11149 "(expected tu %i gmch %i/%i link %i/%i, " \
11150 "or tu %i gmch %i/%i link %i/%i, " \
11151 "found tu %i, gmch %i/%i link %i/%i)\n", \
11152 current_config->name.tu, \
11153 current_config->name.gmch_m, \
11154 current_config->name.gmch_n, \
11155 current_config->name.link_m, \
11156 current_config->name.link_n, \
11157 current_config->alt_name.tu, \
11158 current_config->alt_name.gmch_m, \
11159 current_config->alt_name.gmch_n, \
11160 current_config->alt_name.link_m, \
11161 current_config->alt_name.link_n, \
11162 pipe_config->name.tu, \
11163 pipe_config->name.gmch_m, \
11164 pipe_config->name.gmch_n, \
11165 pipe_config->name.link_m, \
11166 pipe_config->name.link_n); \
11167 ret = false; \
88adfff1
DV
11168 }
11169
1bd1bd80
DV
11170#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11171 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11172 pipe_config_err(adjust, __stringify(name), \
11173 "(%x) (expected %i, found %i)\n", \
11174 (mask), \
1bd1bd80
DV
11175 current_config->name & (mask), \
11176 pipe_config->name & (mask)); \
cfb23ed6 11177 ret = false; \
1bd1bd80
DV
11178 }
11179
5e550656
VS
11180#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11181 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11182 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11183 "(expected %i, found %i)\n", \
11184 current_config->name, \
11185 pipe_config->name); \
cfb23ed6 11186 ret = false; \
5e550656
VS
11187 }
11188
bb760063
DV
11189#define PIPE_CONF_QUIRK(quirk) \
11190 ((current_config->quirks | pipe_config->quirks) & (quirk))
11191
eccb140b
DV
11192 PIPE_CONF_CHECK_I(cpu_transcoder);
11193
08a24034
DV
11194 PIPE_CONF_CHECK_I(has_pch_encoder);
11195 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11196 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11197
90a6b7b0 11198 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11199 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11200
6315b5d3 11201 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11202 PIPE_CONF_CHECK_M_N(dp_m_n);
11203
cfb23ed6
ML
11204 if (current_config->has_drrs)
11205 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11206 } else
11207 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11208
253c84c8 11209 PIPE_CONF_CHECK_X(output_types);
a65347ba 11210
2d112de7
ACO
11211 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11212 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11213 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11214 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11215 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11216 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11217
2d112de7
ACO
11218 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11219 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11220 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11221 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11222 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11223 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11224
c93f54cf 11225 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11226 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11227 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11228 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11229 PIPE_CONF_CHECK_I(limited_color_range);
15953637
SS
11230
11231 PIPE_CONF_CHECK_I(hdmi_scrambling);
11232 PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
e43823ec 11233 PIPE_CONF_CHECK_I(has_infoframe);
60436fd4 11234 PIPE_CONF_CHECK_I(ycbcr420);
6c49f241 11235
9ed109a7
DV
11236 PIPE_CONF_CHECK_I(has_audio);
11237
2d112de7 11238 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11239 DRM_MODE_FLAG_INTERLACE);
11240
bb760063 11241 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11242 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11243 DRM_MODE_FLAG_PHSYNC);
2d112de7 11244 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11245 DRM_MODE_FLAG_NHSYNC);
2d112de7 11246 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11247 DRM_MODE_FLAG_PVSYNC);
2d112de7 11248 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11249 DRM_MODE_FLAG_NVSYNC);
11250 }
045ac3b5 11251
333b8ca8 11252 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11253 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11254 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11255 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11256 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11257
bfd16b2a
ML
11258 if (!adjust) {
11259 PIPE_CONF_CHECK_I(pipe_src_w);
11260 PIPE_CONF_CHECK_I(pipe_src_h);
11261
11262 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11263 if (current_config->pch_pfit.enabled) {
11264 PIPE_CONF_CHECK_X(pch_pfit.pos);
11265 PIPE_CONF_CHECK_X(pch_pfit.size);
11266 }
2fa2fe9a 11267
7aefe2b5 11268 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11269 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11270 }
a1b2278e 11271
e59150dc 11272 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11273 if (IS_HASWELL(dev_priv))
e59150dc 11274 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11275
282740f7
VS
11276 PIPE_CONF_CHECK_I(double_wide);
11277
8106ddbd 11278 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11279 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11280 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11281 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11282 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11283 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11284 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11285 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11286 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11287 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
2de38138
PZ
11288 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11289 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11290 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11291 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11292 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11293 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11294 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11295 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11296 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11297 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11298 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11299 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
c0d43d62 11300
47eacbab
VS
11301 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11302 PIPE_CONF_CHECK_X(dsi_pll.div);
11303
9beb5fea 11304 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11305 PIPE_CONF_CHECK_I(pipe_bpp);
11306
2d112de7 11307 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11308 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11309
66e985c0 11310#undef PIPE_CONF_CHECK_X
08a24034 11311#undef PIPE_CONF_CHECK_I
8106ddbd 11312#undef PIPE_CONF_CHECK_P
1bd1bd80 11313#undef PIPE_CONF_CHECK_FLAGS
5e550656 11314#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11315#undef PIPE_CONF_QUIRK
88adfff1 11316
cfb23ed6 11317 return ret;
0e8ffe1b
DV
11318}
11319
e3b247da
VS
11320static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11321 const struct intel_crtc_state *pipe_config)
11322{
11323 if (pipe_config->has_pch_encoder) {
21a727b3 11324 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11325 &pipe_config->fdi_m_n);
11326 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11327
11328 /*
11329 * FDI already provided one idea for the dotclock.
11330 * Yell if the encoder disagrees.
11331 */
11332 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11333 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11334 fdi_dotclock, dotclock);
11335 }
11336}
11337
c0ead703
ML
11338static void verify_wm_state(struct drm_crtc *crtc,
11339 struct drm_crtc_state *new_state)
08db6652 11340{
6315b5d3 11341 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11342 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11343 struct skl_pipe_wm hw_wm, *sw_wm;
11344 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11345 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11347 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11348 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11349
6315b5d3 11350 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11351 return;
11352
3de8a14c 11353 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11354 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11355
08db6652
DL
11356 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11357 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11358
e7c84544 11359 /* planes */
8b364b41 11360 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11361 hw_plane_wm = &hw_wm.planes[plane];
11362 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11363
3de8a14c 11364 /* Watermarks */
11365 for (level = 0; level <= max_level; level++) {
11366 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11367 &sw_plane_wm->wm[level]))
11368 continue;
11369
11370 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",
11371 pipe_name(pipe), plane + 1, level,
11372 sw_plane_wm->wm[level].plane_en,
11373 sw_plane_wm->wm[level].plane_res_b,
11374 sw_plane_wm->wm[level].plane_res_l,
11375 hw_plane_wm->wm[level].plane_en,
11376 hw_plane_wm->wm[level].plane_res_b,
11377 hw_plane_wm->wm[level].plane_res_l);
11378 }
08db6652 11379
3de8a14c 11380 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11381 &sw_plane_wm->trans_wm)) {
11382 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",
11383 pipe_name(pipe), plane + 1,
11384 sw_plane_wm->trans_wm.plane_en,
11385 sw_plane_wm->trans_wm.plane_res_b,
11386 sw_plane_wm->trans_wm.plane_res_l,
11387 hw_plane_wm->trans_wm.plane_en,
11388 hw_plane_wm->trans_wm.plane_res_b,
11389 hw_plane_wm->trans_wm.plane_res_l);
11390 }
11391
11392 /* DDB */
11393 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11394 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11395
11396 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11397 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11398 pipe_name(pipe), plane + 1,
11399 sw_ddb_entry->start, sw_ddb_entry->end,
11400 hw_ddb_entry->start, hw_ddb_entry->end);
11401 }
e7c84544 11402 }
08db6652 11403
27082493
L
11404 /*
11405 * cursor
11406 * If the cursor plane isn't active, we may not have updated it's ddb
11407 * allocation. In that case since the ddb allocation will be updated
11408 * once the plane becomes visible, we can skip this check
11409 */
cd5dcbf1 11410 if (1) {
3de8a14c 11411 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11412 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11413
11414 /* Watermarks */
11415 for (level = 0; level <= max_level; level++) {
11416 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11417 &sw_plane_wm->wm[level]))
11418 continue;
11419
11420 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",
11421 pipe_name(pipe), level,
11422 sw_plane_wm->wm[level].plane_en,
11423 sw_plane_wm->wm[level].plane_res_b,
11424 sw_plane_wm->wm[level].plane_res_l,
11425 hw_plane_wm->wm[level].plane_en,
11426 hw_plane_wm->wm[level].plane_res_b,
11427 hw_plane_wm->wm[level].plane_res_l);
11428 }
11429
11430 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11431 &sw_plane_wm->trans_wm)) {
11432 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11433 pipe_name(pipe),
11434 sw_plane_wm->trans_wm.plane_en,
11435 sw_plane_wm->trans_wm.plane_res_b,
11436 sw_plane_wm->trans_wm.plane_res_l,
11437 hw_plane_wm->trans_wm.plane_en,
11438 hw_plane_wm->trans_wm.plane_res_b,
11439 hw_plane_wm->trans_wm.plane_res_l);
11440 }
11441
11442 /* DDB */
11443 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11444 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11445
3de8a14c 11446 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11447 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11448 pipe_name(pipe),
3de8a14c 11449 sw_ddb_entry->start, sw_ddb_entry->end,
11450 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11451 }
08db6652
DL
11452 }
11453}
11454
91d1b4bd 11455static void
677100ce
ML
11456verify_connector_state(struct drm_device *dev,
11457 struct drm_atomic_state *state,
11458 struct drm_crtc *crtc)
8af6cf88 11459{
35dd3c64 11460 struct drm_connector *connector;
aa5e9b47 11461 struct drm_connector_state *new_conn_state;
677100ce 11462 int i;
8af6cf88 11463
aa5e9b47 11464 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 11465 struct drm_encoder *encoder = connector->encoder;
749d98b8 11466 struct drm_crtc_state *crtc_state = NULL;
ad3c558f 11467
aa5e9b47 11468 if (new_conn_state->crtc != crtc)
e7c84544
ML
11469 continue;
11470
749d98b8
ML
11471 if (crtc)
11472 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11473
11474 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 11475
aa5e9b47 11476 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 11477 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11478 }
91d1b4bd
DV
11479}
11480
11481static void
86b04268 11482verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
DV
11483{
11484 struct intel_encoder *encoder;
86b04268
DV
11485 struct drm_connector *connector;
11486 struct drm_connector_state *old_conn_state, *new_conn_state;
11487 int i;
8af6cf88 11488
b2784e15 11489 for_each_intel_encoder(dev, encoder) {
86b04268 11490 bool enabled = false, found = false;
4d20cd86 11491 enum pipe pipe;
8af6cf88
DV
11492
11493 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11494 encoder->base.base.id,
8e329a03 11495 encoder->base.name);
8af6cf88 11496
86b04268
DV
11497 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11498 new_conn_state, i) {
11499 if (old_conn_state->best_encoder == &encoder->base)
11500 found = true;
11501
11502 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 11503 continue;
86b04268 11504 found = enabled = true;
ad3c558f 11505
86b04268 11506 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
11507 encoder->base.crtc,
11508 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 11509 }
86b04268
DV
11510
11511 if (!found)
11512 continue;
0e32b39c 11513
e2c719b7 11514 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11515 "encoder's enabled state mismatch "
11516 "(expected %i, found %i)\n",
11517 !!encoder->base.crtc, enabled);
7c60d198
ML
11518
11519 if (!encoder->base.crtc) {
4d20cd86 11520 bool active;
7c60d198 11521
4d20cd86
ML
11522 active = encoder->get_hw_state(encoder, &pipe);
11523 I915_STATE_WARN(active,
11524 "encoder detached but still enabled on pipe %c.\n",
11525 pipe_name(pipe));
7c60d198 11526 }
8af6cf88 11527 }
91d1b4bd
DV
11528}
11529
11530static void
c0ead703
ML
11531verify_crtc_state(struct drm_crtc *crtc,
11532 struct drm_crtc_state *old_crtc_state,
11533 struct drm_crtc_state *new_crtc_state)
91d1b4bd 11534{
e7c84544 11535 struct drm_device *dev = crtc->dev;
fac5e23e 11536 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 11537 struct intel_encoder *encoder;
e7c84544
ML
11538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11539 struct intel_crtc_state *pipe_config, *sw_config;
11540 struct drm_atomic_state *old_state;
11541 bool active;
045ac3b5 11542
e7c84544 11543 old_state = old_crtc_state->state;
ec2dc6a0 11544 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
11545 pipe_config = to_intel_crtc_state(old_crtc_state);
11546 memset(pipe_config, 0, sizeof(*pipe_config));
11547 pipe_config->base.crtc = crtc;
11548 pipe_config->base.state = old_state;
8af6cf88 11549
78108b7c 11550 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 11551
e7c84544 11552 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 11553
e56134bc
VS
11554 /* we keep both pipes enabled on 830 */
11555 if (IS_I830(dev_priv))
e7c84544 11556 active = new_crtc_state->active;
6c49f241 11557
e7c84544
ML
11558 I915_STATE_WARN(new_crtc_state->active != active,
11559 "crtc active state doesn't match with hw state "
11560 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 11561
e7c84544
ML
11562 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11563 "transitional active state does not match atomic hw state "
11564 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 11565
e7c84544
ML
11566 for_each_encoder_on_crtc(dev, crtc, encoder) {
11567 enum pipe pipe;
4d20cd86 11568
e7c84544
ML
11569 active = encoder->get_hw_state(encoder, &pipe);
11570 I915_STATE_WARN(active != new_crtc_state->active,
11571 "[ENCODER:%i] active %i with crtc active %i\n",
11572 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 11573
e7c84544
ML
11574 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11575 "Encoder connected to wrong pipe %c\n",
11576 pipe_name(pipe));
4d20cd86 11577
253c84c8
VS
11578 if (active) {
11579 pipe_config->output_types |= 1 << encoder->type;
e7c84544 11580 encoder->get_config(encoder, pipe_config);
253c84c8 11581 }
e7c84544 11582 }
53d9f4e9 11583
a7d1b3f4
VS
11584 intel_crtc_compute_pixel_rate(pipe_config);
11585
e7c84544
ML
11586 if (!new_crtc_state->active)
11587 return;
cfb23ed6 11588
e7c84544 11589 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 11590
749d98b8 11591 sw_config = to_intel_crtc_state(new_crtc_state);
6315b5d3 11592 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
11593 pipe_config, false)) {
11594 I915_STATE_WARN(1, "pipe state doesn't match!\n");
11595 intel_dump_pipe_config(intel_crtc, pipe_config,
11596 "[hw state]");
11597 intel_dump_pipe_config(intel_crtc, sw_config,
11598 "[sw state]");
8af6cf88
DV
11599 }
11600}
11601
91d1b4bd 11602static void
c0ead703
ML
11603verify_single_dpll_state(struct drm_i915_private *dev_priv,
11604 struct intel_shared_dpll *pll,
11605 struct drm_crtc *crtc,
11606 struct drm_crtc_state *new_state)
91d1b4bd 11607{
91d1b4bd 11608 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
11609 unsigned crtc_mask;
11610 bool active;
5358901f 11611
e7c84544 11612 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 11613
e7c84544 11614 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 11615
e7c84544 11616 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 11617
e7c84544
ML
11618 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
11619 I915_STATE_WARN(!pll->on && pll->active_mask,
11620 "pll in active use but not on in sw tracking\n");
11621 I915_STATE_WARN(pll->on && !pll->active_mask,
11622 "pll is on but not used by any active crtc\n");
11623 I915_STATE_WARN(pll->on != active,
11624 "pll on state mismatch (expected %i, found %i)\n",
11625 pll->on, active);
11626 }
5358901f 11627
e7c84544 11628 if (!crtc) {
2c42e535 11629 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 11630 "more active pll users than references: %x vs %x\n",
2c42e535 11631 pll->active_mask, pll->state.crtc_mask);
5358901f 11632
e7c84544
ML
11633 return;
11634 }
11635
11636 crtc_mask = 1 << drm_crtc_index(crtc);
11637
11638 if (new_state->active)
11639 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
11640 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
11641 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
11642 else
11643 I915_STATE_WARN(pll->active_mask & crtc_mask,
11644 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
11645 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 11646
2c42e535 11647 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 11648 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 11649 crtc_mask, pll->state.crtc_mask);
66e985c0 11650
2c42e535 11651 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
11652 &dpll_hw_state,
11653 sizeof(dpll_hw_state)),
11654 "pll hw state mismatch\n");
11655}
11656
11657static void
c0ead703
ML
11658verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
11659 struct drm_crtc_state *old_crtc_state,
11660 struct drm_crtc_state *new_crtc_state)
e7c84544 11661{
fac5e23e 11662 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
11663 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
11664 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
11665
11666 if (new_state->shared_dpll)
c0ead703 11667 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
11668
11669 if (old_state->shared_dpll &&
11670 old_state->shared_dpll != new_state->shared_dpll) {
11671 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
11672 struct intel_shared_dpll *pll = old_state->shared_dpll;
11673
11674 I915_STATE_WARN(pll->active_mask & crtc_mask,
11675 "pll active mismatch (didn't expect pipe %c in active mask)\n",
11676 pipe_name(drm_crtc_index(crtc)));
2c42e535 11677 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
11678 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
11679 pipe_name(drm_crtc_index(crtc)));
5358901f 11680 }
8af6cf88
DV
11681}
11682
e7c84544 11683static void
c0ead703 11684intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
11685 struct drm_atomic_state *state,
11686 struct drm_crtc_state *old_state,
11687 struct drm_crtc_state *new_state)
e7c84544 11688{
5a21b665
DV
11689 if (!needs_modeset(new_state) &&
11690 !to_intel_crtc_state(new_state)->update_pipe)
11691 return;
11692
c0ead703 11693 verify_wm_state(crtc, new_state);
677100ce 11694 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
11695 verify_crtc_state(crtc, old_state, new_state);
11696 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
11697}
11698
11699static void
c0ead703 11700verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 11701{
fac5e23e 11702 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
11703 int i;
11704
11705 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 11706 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
11707}
11708
11709static void
677100ce
ML
11710intel_modeset_verify_disabled(struct drm_device *dev,
11711 struct drm_atomic_state *state)
e7c84544 11712{
86b04268 11713 verify_encoder_state(dev, state);
677100ce 11714 verify_connector_state(dev, state, NULL);
c0ead703 11715 verify_disabled_dpll_state(dev);
e7c84544
ML
11716}
11717
80715b2f
VS
11718static void update_scanline_offset(struct intel_crtc *crtc)
11719{
4f8036a2 11720 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
11721
11722 /*
11723 * The scanline counter increments at the leading edge of hsync.
11724 *
11725 * On most platforms it starts counting from vtotal-1 on the
11726 * first active line. That means the scanline counter value is
11727 * always one less than what we would expect. Ie. just after
11728 * start of vblank, which also occurs at start of hsync (on the
11729 * last active line), the scanline counter will read vblank_start-1.
11730 *
11731 * On gen2 the scanline counter starts counting from 1 instead
11732 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11733 * to keep the value positive), instead of adding one.
11734 *
11735 * On HSW+ the behaviour of the scanline counter depends on the output
11736 * type. For DP ports it behaves like most other platforms, but on HDMI
11737 * there's an extra 1 line difference. So we need to add two instead of
11738 * one to the value.
ec1b4ee2
VS
11739 *
11740 * On VLV/CHV DSI the scanline counter would appear to increment
11741 * approx. 1/3 of a scanline before start of vblank. Unfortunately
11742 * that means we can't tell whether we're in vblank or not while
11743 * we're on that particular line. We must still set scanline_offset
11744 * to 1 so that the vblank timestamps come out correct when we query
11745 * the scanline counter from within the vblank interrupt handler.
11746 * However if queried just before the start of vblank we'll get an
11747 * answer that's slightly in the future.
80715b2f 11748 */
4f8036a2 11749 if (IS_GEN2(dev_priv)) {
124abe07 11750 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
11751 int vtotal;
11752
124abe07
VS
11753 vtotal = adjusted_mode->crtc_vtotal;
11754 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
11755 vtotal /= 2;
11756
11757 crtc->scanline_offset = vtotal - 1;
4f8036a2 11758 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 11759 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
11760 crtc->scanline_offset = 2;
11761 } else
11762 crtc->scanline_offset = 1;
11763}
11764
ad421372 11765static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 11766{
225da59b 11767 struct drm_device *dev = state->dev;
ed6739ef 11768 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 11769 struct drm_crtc *crtc;
aa5e9b47 11770 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 11771 int i;
ed6739ef
ACO
11772
11773 if (!dev_priv->display.crtc_compute_clock)
ad421372 11774 return;
ed6739ef 11775
aa5e9b47 11776 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 11777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 11778 struct intel_shared_dpll *old_dpll =
aa5e9b47 11779 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 11780
aa5e9b47 11781 if (!needs_modeset(new_crtc_state))
225da59b
ACO
11782 continue;
11783
aa5e9b47 11784 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 11785
8106ddbd 11786 if (!old_dpll)
fb1a38a9 11787 continue;
0a9ab303 11788
a1c414ee 11789 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 11790 }
ed6739ef
ACO
11791}
11792
99d736a2
ML
11793/*
11794 * This implements the workaround described in the "notes" section of the mode
11795 * set sequence documentation. When going from no pipes or single pipe to
11796 * multiple pipes, and planes are enabled after the pipe, we need to wait at
11797 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
11798 */
11799static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
11800{
11801 struct drm_crtc_state *crtc_state;
11802 struct intel_crtc *intel_crtc;
11803 struct drm_crtc *crtc;
11804 struct intel_crtc_state *first_crtc_state = NULL;
11805 struct intel_crtc_state *other_crtc_state = NULL;
11806 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
11807 int i;
11808
11809 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 11810 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
11811 intel_crtc = to_intel_crtc(crtc);
11812
11813 if (!crtc_state->active || !needs_modeset(crtc_state))
11814 continue;
11815
11816 if (first_crtc_state) {
11817 other_crtc_state = to_intel_crtc_state(crtc_state);
11818 break;
11819 } else {
11820 first_crtc_state = to_intel_crtc_state(crtc_state);
11821 first_pipe = intel_crtc->pipe;
11822 }
11823 }
11824
11825 /* No workaround needed? */
11826 if (!first_crtc_state)
11827 return 0;
11828
11829 /* w/a possibly needed, check how many crtc's are already enabled. */
11830 for_each_intel_crtc(state->dev, intel_crtc) {
11831 struct intel_crtc_state *pipe_config;
11832
11833 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11834 if (IS_ERR(pipe_config))
11835 return PTR_ERR(pipe_config);
11836
11837 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
11838
11839 if (!pipe_config->base.active ||
11840 needs_modeset(&pipe_config->base))
11841 continue;
11842
11843 /* 2 or more enabled crtcs means no need for w/a */
11844 if (enabled_pipe != INVALID_PIPE)
11845 return 0;
11846
11847 enabled_pipe = intel_crtc->pipe;
11848 }
11849
11850 if (enabled_pipe != INVALID_PIPE)
11851 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
11852 else if (other_crtc_state)
11853 other_crtc_state->hsw_workaround_pipe = first_pipe;
11854
11855 return 0;
11856}
11857
8d96561a
VS
11858static int intel_lock_all_pipes(struct drm_atomic_state *state)
11859{
11860 struct drm_crtc *crtc;
11861
11862 /* Add all pipes to the state */
11863 for_each_crtc(state->dev, crtc) {
11864 struct drm_crtc_state *crtc_state;
11865
11866 crtc_state = drm_atomic_get_crtc_state(state, crtc);
11867 if (IS_ERR(crtc_state))
11868 return PTR_ERR(crtc_state);
11869 }
11870
11871 return 0;
11872}
11873
27c329ed
ML
11874static int intel_modeset_all_pipes(struct drm_atomic_state *state)
11875{
11876 struct drm_crtc *crtc;
27c329ed 11877
8d96561a
VS
11878 /*
11879 * Add all pipes to the state, and force
11880 * a modeset on all the active ones.
11881 */
27c329ed 11882 for_each_crtc(state->dev, crtc) {
9780aad5
VS
11883 struct drm_crtc_state *crtc_state;
11884 int ret;
11885
27c329ed
ML
11886 crtc_state = drm_atomic_get_crtc_state(state, crtc);
11887 if (IS_ERR(crtc_state))
11888 return PTR_ERR(crtc_state);
11889
11890 if (!crtc_state->active || needs_modeset(crtc_state))
11891 continue;
11892
11893 crtc_state->mode_changed = true;
11894
11895 ret = drm_atomic_add_affected_connectors(state, crtc);
11896 if (ret)
9780aad5 11897 return ret;
27c329ed
ML
11898
11899 ret = drm_atomic_add_affected_planes(state, crtc);
11900 if (ret)
9780aad5 11901 return ret;
27c329ed
ML
11902 }
11903
9780aad5 11904 return 0;
27c329ed
ML
11905}
11906
c347a676 11907static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 11908{
565602d7 11909 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 11910 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 11911 struct drm_crtc *crtc;
aa5e9b47 11912 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 11913 int ret = 0, i;
054518dd 11914
b359283a
ML
11915 if (!check_digital_port_conflicts(state)) {
11916 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11917 return -EINVAL;
11918 }
11919
565602d7
ML
11920 intel_state->modeset = true;
11921 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
11922 intel_state->cdclk.logical = dev_priv->cdclk.logical;
11923 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 11924
aa5e9b47
ML
11925 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11926 if (new_crtc_state->active)
565602d7
ML
11927 intel_state->active_crtcs |= 1 << i;
11928 else
11929 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 11930
aa5e9b47 11931 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 11932 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
11933 }
11934
054518dd
ACO
11935 /*
11936 * See if the config requires any additional preparation, e.g.
11937 * to adjust global state with pipes off. We need to do this
11938 * here so we can get the modeset_pipe updated config for the new
11939 * mode set on this crtc. For other crtcs we need to use the
11940 * adjusted_mode bits in the crtc directly.
11941 */
27c329ed 11942 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 11943 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
11944 if (ret < 0)
11945 return ret;
27c329ed 11946
8d96561a 11947 /*
bb0f4aab 11948 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
11949 * holding all the crtc locks, even if we don't end up
11950 * touching the hardware
11951 */
bb0f4aab
VS
11952 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
11953 &intel_state->cdclk.logical)) {
8d96561a
VS
11954 ret = intel_lock_all_pipes(state);
11955 if (ret < 0)
11956 return ret;
11957 }
11958
11959 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
11960 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
11961 &intel_state->cdclk.actual)) {
27c329ed 11962 ret = intel_modeset_all_pipes(state);
8d96561a
VS
11963 if (ret < 0)
11964 return ret;
11965 }
e8788cbc 11966
bb0f4aab
VS
11967 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
11968 intel_state->cdclk.logical.cdclk,
11969 intel_state->cdclk.actual.cdclk);
e0ca7a6b 11970 } else {
bb0f4aab 11971 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 11972 }
054518dd 11973
ad421372 11974 intel_modeset_clear_plls(state);
054518dd 11975
565602d7 11976 if (IS_HASWELL(dev_priv))
ad421372 11977 return haswell_mode_set_planes_workaround(state);
99d736a2 11978
ad421372 11979 return 0;
c347a676
ACO
11980}
11981
aa363136
MR
11982/*
11983 * Handle calculation of various watermark data at the end of the atomic check
11984 * phase. The code here should be run after the per-crtc and per-plane 'check'
11985 * handlers to ensure that all derived state has been updated.
11986 */
55994c2c 11987static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
11988{
11989 struct drm_device *dev = state->dev;
98d39494 11990 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
11991
11992 /* Is there platform-specific watermark information to calculate? */
11993 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
11994 return dev_priv->display.compute_global_watermarks(state);
11995
11996 return 0;
aa363136
MR
11997}
11998
74c090b1
ML
11999/**
12000 * intel_atomic_check - validate state object
12001 * @dev: drm device
12002 * @state: state to validate
12003 */
12004static int intel_atomic_check(struct drm_device *dev,
12005 struct drm_atomic_state *state)
c347a676 12006{
dd8b3bdb 12007 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12008 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12009 struct drm_crtc *crtc;
aa5e9b47 12010 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12011 int ret, i;
61333b60 12012 bool any_ms = false;
c347a676 12013
74c090b1 12014 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12015 if (ret)
12016 return ret;
12017
aa5e9b47 12018 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12019 struct intel_crtc_state *pipe_config =
12020 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12021
12022 /* Catch I915_MODE_FLAG_INHERITED */
aa5e9b47 12023 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
1ed51de9 12024 crtc_state->mode_changed = true;
cfb23ed6 12025
af4a879e 12026 if (!needs_modeset(crtc_state))
c347a676
ACO
12027 continue;
12028
af4a879e
DV
12029 if (!crtc_state->enable) {
12030 any_ms = true;
cfb23ed6 12031 continue;
af4a879e 12032 }
cfb23ed6 12033
26495481
DV
12034 /* FIXME: For only active_changed we shouldn't need to do any
12035 * state recomputation at all. */
12036
1ed51de9
DV
12037 ret = drm_atomic_add_affected_connectors(state, crtc);
12038 if (ret)
12039 return ret;
b359283a 12040
cfb23ed6 12041 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12042 if (ret) {
12043 intel_dump_pipe_config(to_intel_crtc(crtc),
12044 pipe_config, "[failed]");
c347a676 12045 return ret;
25aa1c39 12046 }
c347a676 12047
4f044a88 12048 if (i915_modparams.fastboot &&
6315b5d3 12049 intel_pipe_config_compare(dev_priv,
aa5e9b47 12050 to_intel_crtc_state(old_crtc_state),
1ed51de9 12051 pipe_config, true)) {
26495481 12052 crtc_state->mode_changed = false;
aa5e9b47 12053 pipe_config->update_pipe = true;
26495481
DV
12054 }
12055
af4a879e 12056 if (needs_modeset(crtc_state))
26495481 12057 any_ms = true;
cfb23ed6 12058
af4a879e
DV
12059 ret = drm_atomic_add_affected_planes(state, crtc);
12060 if (ret)
12061 return ret;
61333b60 12062
26495481
DV
12063 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12064 needs_modeset(crtc_state) ?
12065 "[modeset]" : "[fastset]");
c347a676
ACO
12066 }
12067
61333b60
ML
12068 if (any_ms) {
12069 ret = intel_modeset_checks(state);
12070
12071 if (ret)
12072 return ret;
e0ca7a6b 12073 } else {
bb0f4aab 12074 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12075 }
76305b1a 12076
dd8b3bdb 12077 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12078 if (ret)
12079 return ret;
12080
f51be2e0 12081 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12082 return calc_watermark_data(state);
054518dd
ACO
12083}
12084
5008e874 12085static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12086 struct drm_atomic_state *state)
5008e874 12087{
fd70075f 12088 return drm_atomic_helper_prepare_planes(dev, state);
5008e874
ML
12089}
12090
a2991414
ML
12091u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12092{
12093 struct drm_device *dev = crtc->base.dev;
12094
12095 if (!dev->max_vblank_count)
ca814b25 12096 return drm_crtc_accurate_vblank_count(&crtc->base);
a2991414
ML
12097
12098 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12099}
12100
896e5bb0
L
12101static void intel_update_crtc(struct drm_crtc *crtc,
12102 struct drm_atomic_state *state,
12103 struct drm_crtc_state *old_crtc_state,
b44d5c0c 12104 struct drm_crtc_state *new_crtc_state)
896e5bb0
L
12105{
12106 struct drm_device *dev = crtc->dev;
12107 struct drm_i915_private *dev_priv = to_i915(dev);
12108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12109 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12110 bool modeset = needs_modeset(new_crtc_state);
896e5bb0
L
12111
12112 if (modeset) {
12113 update_scanline_offset(intel_crtc);
12114 dev_priv->display.crtc_enable(pipe_config, state);
12115 } else {
aa5e9b47
ML
12116 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12117 pipe_config);
896e5bb0
L
12118 }
12119
12120 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12121 intel_fbc_enable(
12122 intel_crtc, pipe_config,
12123 to_intel_plane_state(crtc->primary->state));
12124 }
12125
12126 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
896e5bb0
L
12127}
12128
b44d5c0c 12129static void intel_update_crtcs(struct drm_atomic_state *state)
896e5bb0
L
12130{
12131 struct drm_crtc *crtc;
aa5e9b47 12132 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12133 int i;
12134
aa5e9b47
ML
12135 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12136 if (!new_crtc_state->active)
896e5bb0
L
12137 continue;
12138
12139 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12140 new_crtc_state);
896e5bb0
L
12141 }
12142}
12143
b44d5c0c 12144static void skl_update_crtcs(struct drm_atomic_state *state)
27082493 12145{
0f0f74bc 12146 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12147 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12148 struct drm_crtc *crtc;
ce0ba283 12149 struct intel_crtc *intel_crtc;
aa5e9b47 12150 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12151 struct intel_crtc_state *cstate;
27082493
L
12152 unsigned int updated = 0;
12153 bool progress;
12154 enum pipe pipe;
5eff503b
ML
12155 int i;
12156
12157 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12158
aa5e9b47 12159 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12160 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12161 if (new_crtc_state->active)
5eff503b 12162 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12163
12164 /*
12165 * Whenever the number of active pipes changes, we need to make sure we
12166 * update the pipes in the right order so that their ddb allocations
12167 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12168 * cause pipe underruns and other bad stuff.
12169 */
12170 do {
27082493
L
12171 progress = false;
12172
aa5e9b47 12173 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12174 bool vbl_wait = false;
12175 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12176
12177 intel_crtc = to_intel_crtc(crtc);
21794813 12178 cstate = to_intel_crtc_state(new_crtc_state);
ce0ba283 12179 pipe = intel_crtc->pipe;
27082493 12180
5eff503b 12181 if (updated & cmask || !cstate->base.active)
27082493 12182 continue;
5eff503b 12183
2b68504b
MK
12184 if (skl_ddb_allocation_overlaps(dev_priv,
12185 entries,
12186 &cstate->wm.skl.ddb,
12187 i))
27082493
L
12188 continue;
12189
12190 updated |= cmask;
5eff503b 12191 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12192
12193 /*
12194 * If this is an already active pipe, it's DDB changed,
12195 * and this isn't the last pipe that needs updating
12196 * then we need to wait for a vblank to pass for the
12197 * new ddb allocation to take effect.
12198 */
ce0ba283 12199 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12200 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12201 !new_crtc_state->active_changed &&
27082493
L
12202 intel_state->wm_results.dirty_pipes != updated)
12203 vbl_wait = true;
12204
12205 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12206 new_crtc_state);
27082493
L
12207
12208 if (vbl_wait)
0f0f74bc 12209 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12210
12211 progress = true;
12212 }
12213 } while (progress);
12214}
12215
ba318c61
CW
12216static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12217{
12218 struct intel_atomic_state *state, *next;
12219 struct llist_node *freed;
12220
12221 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12222 llist_for_each_entry_safe(state, next, freed, freed)
12223 drm_atomic_state_put(&state->base);
12224}
12225
12226static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12227{
12228 struct drm_i915_private *dev_priv =
12229 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12230
12231 intel_atomic_helper_free_state(dev_priv);
12232}
12233
9db529aa
DV
12234static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12235{
12236 struct wait_queue_entry wait_fence, wait_reset;
12237 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12238
12239 init_wait_entry(&wait_fence, 0);
12240 init_wait_entry(&wait_reset, 0);
12241 for (;;) {
12242 prepare_to_wait(&intel_state->commit_ready.wait,
12243 &wait_fence, TASK_UNINTERRUPTIBLE);
12244 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12245 &wait_reset, TASK_UNINTERRUPTIBLE);
12246
12247
12248 if (i915_sw_fence_done(&intel_state->commit_ready)
12249 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12250 break;
12251
12252 schedule();
12253 }
12254 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12255 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12256}
12257
94f05024 12258static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12259{
94f05024 12260 struct drm_device *dev = state->dev;
565602d7 12261 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12262 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 12263 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 12264 struct drm_crtc *crtc;
5a21b665 12265 struct intel_crtc_state *intel_cstate;
d8fc70b7 12266 u64 put_domains[I915_MAX_PIPES] = {};
e95433c7 12267 int i;
a6778b3c 12268
9db529aa 12269 intel_atomic_commit_fence_wait(intel_state);
42b062b0 12270
ea0000f0
DV
12271 drm_atomic_helper_wait_for_dependencies(state);
12272
c3b32658 12273 if (intel_state->modeset)
5a21b665 12274 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12275
aa5e9b47 12276 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
12277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12278
aa5e9b47
ML
12279 if (needs_modeset(new_crtc_state) ||
12280 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
DV
12281
12282 put_domains[to_intel_crtc(crtc)->pipe] =
12283 modeset_get_crtc_power_domains(crtc,
aa5e9b47 12284 to_intel_crtc_state(new_crtc_state));
5a21b665
DV
12285 }
12286
aa5e9b47 12287 if (!needs_modeset(new_crtc_state))
61333b60
ML
12288 continue;
12289
aa5e9b47
ML
12290 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12291 to_intel_crtc_state(new_crtc_state));
460da916 12292
29ceb0e6
VS
12293 if (old_crtc_state->active) {
12294 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12295 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12296 intel_crtc->active = false;
58f9c0bc 12297 intel_fbc_disable(intel_crtc);
eddfcbcd 12298 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12299
12300 /*
12301 * Underruns don't always raise
12302 * interrupts, so check manually.
12303 */
12304 intel_check_cpu_fifo_underruns(dev_priv);
12305 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12306
21794813 12307 if (!new_crtc_state->active) {
e62929b3
ML
12308 /*
12309 * Make sure we don't call initial_watermarks
12310 * for ILK-style watermark updates.
ff32c54e
VS
12311 *
12312 * No clue what this is supposed to achieve.
e62929b3 12313 */
ff32c54e 12314 if (INTEL_GEN(dev_priv) >= 9)
e62929b3 12315 dev_priv->display.initial_watermarks(intel_state,
21794813 12316 to_intel_crtc_state(new_crtc_state));
e62929b3 12317 }
a539205a 12318 }
b8cecdf5 12319 }
7758a113 12320
ea9d758d
DV
12321 /* Only after disabling all output pipelines that will be changed can we
12322 * update the the output configuration. */
4740b0f2 12323 intel_modeset_update_crtc_state(state);
f6e5b160 12324
565602d7 12325 if (intel_state->modeset) {
4740b0f2 12326 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12327
b0587e4d 12328 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12329
656d1b89
L
12330 /*
12331 * SKL workaround: bspec recommends we disable the SAGV when we
12332 * have more then one pipe enabled
12333 */
56feca91 12334 if (!intel_can_enable_sagv(state))
16dcdc4e 12335 intel_disable_sagv(dev_priv);
656d1b89 12336
677100ce 12337 intel_modeset_verify_disabled(dev, state);
4740b0f2 12338 }
47fab737 12339
896e5bb0 12340 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
12341 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12342 bool modeset = needs_modeset(new_crtc_state);
80715b2f 12343
1f7528c4 12344 /* Complete events for now disable pipes here. */
aa5e9b47 12345 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 12346 spin_lock_irq(&dev->event_lock);
aa5e9b47 12347 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
DV
12348 spin_unlock_irq(&dev->event_lock);
12349
aa5e9b47 12350 new_crtc_state->event = NULL;
1f7528c4 12351 }
177246a8
MR
12352 }
12353
896e5bb0 12354 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
b44d5c0c 12355 dev_priv->display.update_crtcs(state);
896e5bb0 12356
94f05024
DV
12357 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12358 * already, but still need the state for the delayed optimization. To
12359 * fix this:
12360 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12361 * - schedule that vblank worker _before_ calling hw_done
12362 * - at the start of commit_tail, cancel it _synchrously
12363 * - switch over to the vblank wait helper in the core after that since
12364 * we don't need out special handling any more.
12365 */
b44d5c0c 12366 drm_atomic_helper_wait_for_flip_done(dev, state);
5a21b665
DV
12367
12368 /*
12369 * Now that the vblank has passed, we can go ahead and program the
12370 * optimal watermarks on platforms that need two-step watermark
12371 * programming.
12372 *
12373 * TODO: Move this (and other cleanup) to an async worker eventually.
12374 */
aa5e9b47
ML
12375 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12376 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
DV
12377
12378 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12379 dev_priv->display.optimize_watermarks(intel_state,
12380 intel_cstate);
5a21b665
DV
12381 }
12382
aa5e9b47 12383 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
DV
12384 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12385
12386 if (put_domains[i])
12387 modeset_put_power_domains(dev_priv, put_domains[i]);
12388
aa5e9b47 12389 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
DV
12390 }
12391
56feca91 12392 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12393 intel_enable_sagv(dev_priv);
656d1b89 12394
94f05024
DV
12395 drm_atomic_helper_commit_hw_done(state);
12396
d5553c09
CW
12397 if (intel_state->modeset) {
12398 /* As one of the primary mmio accessors, KMS has a high
12399 * likelihood of triggering bugs in unclaimed access. After we
12400 * finish modesetting, see if an error has been flagged, and if
12401 * so enable debugging for the next modeset - and hope we catch
12402 * the culprit.
12403 */
12404 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
5a21b665 12405 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
d5553c09 12406 }
5a21b665 12407
5a21b665 12408 drm_atomic_helper_cleanup_planes(dev, state);
5a21b665 12409
ea0000f0
DV
12410 drm_atomic_helper_commit_cleanup_done(state);
12411
0853695c 12412 drm_atomic_state_put(state);
f30da187 12413
ba318c61 12414 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
12415}
12416
12417static void intel_atomic_commit_work(struct work_struct *work)
12418{
c004a90b
CW
12419 struct drm_atomic_state *state =
12420 container_of(work, struct drm_atomic_state, commit_work);
12421
94f05024
DV
12422 intel_atomic_commit_tail(state);
12423}
12424
c004a90b
CW
12425static int __i915_sw_fence_call
12426intel_atomic_commit_ready(struct i915_sw_fence *fence,
12427 enum i915_sw_fence_notify notify)
12428{
12429 struct intel_atomic_state *state =
12430 container_of(fence, struct intel_atomic_state, commit_ready);
12431
12432 switch (notify) {
12433 case FENCE_COMPLETE:
42b062b0 12434 /* we do blocking waits in the worker, nothing to do here */
c004a90b 12435 break;
c004a90b 12436 case FENCE_FREE:
eb955eee
CW
12437 {
12438 struct intel_atomic_helper *helper =
12439 &to_i915(state->base.dev)->atomic_helper;
12440
12441 if (llist_add(&state->freed, &helper->free_list))
12442 schedule_work(&helper->free_work);
12443 break;
12444 }
c004a90b
CW
12445 }
12446
12447 return NOTIFY_DONE;
12448}
12449
6c9c1b38
DV
12450static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12451{
aa5e9b47 12452 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 12453 struct drm_plane *plane;
6c9c1b38
DV
12454 int i;
12455
aa5e9b47 12456 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 12457 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 12458 intel_fb_obj(new_plane_state->fb),
faf5bf0a 12459 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
12460}
12461
94f05024
DV
12462/**
12463 * intel_atomic_commit - commit validated state object
12464 * @dev: DRM device
12465 * @state: the top-level driver state object
12466 * @nonblock: nonblocking commit
12467 *
12468 * This function commits a top-level state object that has been validated
12469 * with drm_atomic_helper_check().
12470 *
94f05024
DV
12471 * RETURNS
12472 * Zero for success or -errno.
12473 */
12474static int intel_atomic_commit(struct drm_device *dev,
12475 struct drm_atomic_state *state,
12476 bool nonblock)
12477{
12478 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12479 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
12480 int ret = 0;
12481
c004a90b
CW
12482 drm_atomic_state_get(state);
12483 i915_sw_fence_init(&intel_state->commit_ready,
12484 intel_atomic_commit_ready);
94f05024 12485
440df938
VS
12486 /*
12487 * The intel_legacy_cursor_update() fast path takes care
12488 * of avoiding the vblank waits for simple cursor
12489 * movement and flips. For cursor on/off and size changes,
12490 * we want to perform the vblank waits so that watermark
12491 * updates happen during the correct frames. Gen9+ have
12492 * double buffered watermarks and so shouldn't need this.
12493 *
3cf50c63
ML
12494 * Unset state->legacy_cursor_update before the call to
12495 * drm_atomic_helper_setup_commit() because otherwise
12496 * drm_atomic_helper_wait_for_flip_done() is a noop and
12497 * we get FIFO underruns because we didn't wait
12498 * for vblank.
440df938
VS
12499 *
12500 * FIXME doing watermarks and fb cleanup from a vblank worker
12501 * (assuming we had any) would solve these problems.
12502 */
213f1bd0
ML
12503 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
12504 struct intel_crtc_state *new_crtc_state;
12505 struct intel_crtc *crtc;
12506 int i;
12507
12508 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
12509 if (new_crtc_state->wm.need_postvbl_update ||
12510 new_crtc_state->update_wm_post)
12511 state->legacy_cursor_update = false;
12512 }
440df938 12513
3cf50c63
ML
12514 ret = intel_atomic_prepare_commit(dev, state);
12515 if (ret) {
12516 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
12517 i915_sw_fence_commit(&intel_state->commit_ready);
12518 return ret;
12519 }
12520
12521 ret = drm_atomic_helper_setup_commit(state, nonblock);
12522 if (!ret)
12523 ret = drm_atomic_helper_swap_state(state, true);
440df938 12524
0806f4ee
ML
12525 if (ret) {
12526 i915_sw_fence_commit(&intel_state->commit_ready);
12527
0806f4ee 12528 drm_atomic_helper_cleanup_planes(dev, state);
0806f4ee
ML
12529 return ret;
12530 }
94f05024 12531 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 12532 intel_shared_dpll_swap_state(state);
6c9c1b38 12533 intel_atomic_track_fbs(state);
94f05024 12534
c3b32658 12535 if (intel_state->modeset) {
d305e061
VS
12536 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
12537 sizeof(intel_state->min_cdclk));
c3b32658 12538 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
12539 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12540 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
12541 }
12542
0853695c 12543 drm_atomic_state_get(state);
42b062b0 12544 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
c004a90b
CW
12545
12546 i915_sw_fence_commit(&intel_state->commit_ready);
42b062b0
DV
12547 if (nonblock)
12548 queue_work(system_unbound_wq, &state->commit_work);
12549 else
94f05024 12550 intel_atomic_commit_tail(state);
42b062b0 12551
75714940 12552
74c090b1 12553 return 0;
7f27126e
JB
12554}
12555
f6e5b160 12556static const struct drm_crtc_funcs intel_crtc_funcs = {
3fab2f09 12557 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 12558 .set_config = drm_atomic_helper_set_config,
f6e5b160 12559 .destroy = intel_crtc_destroy,
4c01ded5 12560 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
12561 .atomic_duplicate_state = intel_crtc_duplicate_state,
12562 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 12563 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
12564};
12565
74d290f8
CW
12566struct wait_rps_boost {
12567 struct wait_queue_entry wait;
12568
12569 struct drm_crtc *crtc;
12570 struct drm_i915_gem_request *request;
12571};
12572
12573static int do_rps_boost(struct wait_queue_entry *_wait,
12574 unsigned mode, int sync, void *key)
12575{
12576 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
12577 struct drm_i915_gem_request *rq = wait->request;
12578
12579 gen6_rps_boost(rq, NULL);
12580 i915_gem_request_put(rq);
12581
12582 drm_crtc_vblank_put(wait->crtc);
12583
12584 list_del(&wait->wait.entry);
12585 kfree(wait);
12586 return 1;
12587}
12588
12589static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
12590 struct dma_fence *fence)
12591{
12592 struct wait_rps_boost *wait;
12593
12594 if (!dma_fence_is_i915(fence))
12595 return;
12596
12597 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
12598 return;
12599
12600 if (drm_crtc_vblank_get(crtc))
12601 return;
12602
12603 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
12604 if (!wait) {
12605 drm_crtc_vblank_put(crtc);
12606 return;
12607 }
12608
12609 wait->request = to_request(dma_fence_get(fence));
12610 wait->crtc = crtc;
12611
12612 wait->wait.func = do_rps_boost;
12613 wait->wait.flags = 0;
12614
12615 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
12616}
12617
6beb8c23
MR
12618/**
12619 * intel_prepare_plane_fb - Prepare fb for usage on plane
12620 * @plane: drm plane to prepare for
12621 * @fb: framebuffer to prepare for presentation
12622 *
12623 * Prepares a framebuffer for usage on a display plane. Generally this
12624 * involves pinning the underlying object and updating the frontbuffer tracking
12625 * bits. Some older platforms need special physical address handling for
12626 * cursor planes.
12627 *
f935675f
ML
12628 * Must be called with struct_mutex held.
12629 *
6beb8c23
MR
12630 * Returns 0 on success, negative error code on failure.
12631 */
12632int
12633intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 12634 struct drm_plane_state *new_state)
465c120c 12635{
c004a90b
CW
12636 struct intel_atomic_state *intel_state =
12637 to_intel_atomic_state(new_state->state);
b7f05d4a 12638 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 12639 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 12640 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 12641 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 12642 int ret;
465c120c 12643
5008e874
ML
12644 if (old_obj) {
12645 struct drm_crtc_state *crtc_state =
c004a90b
CW
12646 drm_atomic_get_existing_crtc_state(new_state->state,
12647 plane->state->crtc);
5008e874
ML
12648
12649 /* Big Hammer, we also need to ensure that any pending
12650 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
12651 * current scanout is retired before unpinning the old
12652 * framebuffer. Note that we rely on userspace rendering
12653 * into the buffer attached to the pipe they are waiting
12654 * on. If not, userspace generates a GPU hang with IPEHR
12655 * point to the MI_WAIT_FOR_EVENT.
12656 *
12657 * This should only fail upon a hung GPU, in which case we
12658 * can safely continue.
12659 */
c004a90b
CW
12660 if (needs_modeset(crtc_state)) {
12661 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12662 old_obj->resv, NULL,
12663 false, 0,
12664 GFP_KERNEL);
12665 if (ret < 0)
12666 return ret;
f4457ae7 12667 }
5008e874
ML
12668 }
12669
c004a90b
CW
12670 if (new_state->fence) { /* explicit fencing */
12671 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
12672 new_state->fence,
12673 I915_FENCE_TIMEOUT,
12674 GFP_KERNEL);
12675 if (ret < 0)
12676 return ret;
12677 }
12678
c37efb99
CW
12679 if (!obj)
12680 return 0;
12681
4d3088c7 12682 ret = i915_gem_object_pin_pages(obj);
fd70075f
CW
12683 if (ret)
12684 return ret;
12685
4d3088c7
CW
12686 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
12687 if (ret) {
12688 i915_gem_object_unpin_pages(obj);
12689 return ret;
12690 }
12691
fd70075f
CW
12692 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12693 INTEL_INFO(dev_priv)->cursor_needs_physical) {
12694 const int align = intel_cursor_alignment(dev_priv);
12695
12696 ret = i915_gem_object_attach_phys(obj, align);
12697 } else {
12698 struct i915_vma *vma;
12699
12700 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
12701 if (!IS_ERR(vma))
12702 to_intel_plane_state(new_state)->vma = vma;
12703 else
12704 ret = PTR_ERR(vma);
12705 }
12706
12707 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
12708
12709 mutex_unlock(&dev_priv->drm.struct_mutex);
4d3088c7 12710 i915_gem_object_unpin_pages(obj);
fd70075f
CW
12711 if (ret)
12712 return ret;
12713
c004a90b 12714 if (!new_state->fence) { /* implicit fencing */
74d290f8
CW
12715 struct dma_fence *fence;
12716
c004a90b
CW
12717 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12718 obj->resv, NULL,
12719 false, I915_FENCE_TIMEOUT,
12720 GFP_KERNEL);
12721 if (ret < 0)
12722 return ret;
74d290f8
CW
12723
12724 fence = reservation_object_get_excl_rcu(obj->resv);
12725 if (fence) {
12726 add_rps_boost_after_vblank(new_state->crtc, fence);
12727 dma_fence_put(fence);
12728 }
12729 } else {
12730 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
c004a90b 12731 }
5a21b665 12732
d07f0e59 12733 return 0;
6beb8c23
MR
12734}
12735
38f3ce3a
MR
12736/**
12737 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12738 * @plane: drm plane to clean up for
12739 * @fb: old framebuffer that was on plane
12740 *
12741 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
12742 *
12743 * Must be called with struct_mutex held.
38f3ce3a
MR
12744 */
12745void
12746intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 12747 struct drm_plane_state *old_state)
38f3ce3a 12748{
be1e3415 12749 struct i915_vma *vma;
38f3ce3a 12750
be1e3415
CW
12751 /* Should only be called after a successful intel_prepare_plane_fb()! */
12752 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
fd70075f
CW
12753 if (vma) {
12754 mutex_lock(&plane->dev->struct_mutex);
be1e3415 12755 intel_unpin_fb_vma(vma);
fd70075f
CW
12756 mutex_unlock(&plane->dev->struct_mutex);
12757 }
465c120c
MR
12758}
12759
6156a456
CK
12760int
12761skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
12762{
5b7280f0 12763 struct drm_i915_private *dev_priv;
6156a456 12764 int max_scale;
5b7280f0 12765 int crtc_clock, max_dotclk;
6156a456 12766
bf8a0af0 12767 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
12768 return DRM_PLANE_HELPER_NO_SCALING;
12769
5b7280f0
ACO
12770 dev_priv = to_i915(intel_crtc->base.dev);
12771
6156a456 12772 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
12773 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
12774
12775 if (IS_GEMINILAKE(dev_priv))
12776 max_dotclk *= 2;
6156a456 12777
5b7280f0 12778 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
12779 return DRM_PLANE_HELPER_NO_SCALING;
12780
12781 /*
12782 * skl max scale is lower of:
12783 * close to 3 but not 3, -1 is for that purpose
12784 * or
12785 * cdclk/crtc_clock
12786 */
5b7280f0
ACO
12787 max_scale = min((1 << 16) * 3 - 1,
12788 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
12789
12790 return max_scale;
12791}
12792
465c120c 12793static int
282dbf9b 12794intel_check_primary_plane(struct intel_plane *plane,
061e4b8d 12795 struct intel_crtc_state *crtc_state,
3c692a41
GP
12796 struct intel_plane_state *state)
12797{
282dbf9b 12798 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2b875c22 12799 struct drm_crtc *crtc = state->base.crtc;
6156a456 12800 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
12801 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
12802 bool can_position = false;
b63a16f6 12803 int ret;
465c120c 12804
b63a16f6 12805 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
12806 /* use scaler when colorkey is not required */
12807 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
12808 min_scale = 1;
12809 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
12810 }
d8106366 12811 can_position = true;
6156a456 12812 }
d8106366 12813
cc926387
DV
12814 ret = drm_plane_helper_check_state(&state->base,
12815 &state->clip,
12816 min_scale, max_scale,
12817 can_position, true);
b63a16f6
VS
12818 if (ret)
12819 return ret;
12820
cc926387 12821 if (!state->base.fb)
b63a16f6
VS
12822 return 0;
12823
12824 if (INTEL_GEN(dev_priv) >= 9) {
12825 ret = skl_check_plane_surface(state);
12826 if (ret)
12827 return ret;
a0864d59
VS
12828
12829 state->ctl = skl_plane_ctl(crtc_state, state);
12830 } else {
5b7fcc44
VS
12831 ret = i9xx_check_plane_surface(state);
12832 if (ret)
12833 return ret;
12834
a0864d59 12835 state->ctl = i9xx_plane_ctl(crtc_state, state);
b63a16f6
VS
12836 }
12837
12838 return 0;
14af293f
GP
12839}
12840
5a21b665
DV
12841static void intel_begin_crtc_commit(struct drm_crtc *crtc,
12842 struct drm_crtc_state *old_crtc_state)
12843{
12844 struct drm_device *dev = crtc->dev;
62e0fb88 12845 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 12846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ccf010fb 12847 struct intel_crtc_state *old_intel_cstate =
5a21b665 12848 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
12849 struct intel_atomic_state *old_intel_state =
12850 to_intel_atomic_state(old_crtc_state->state);
d3a8fb32
VS
12851 struct intel_crtc_state *intel_cstate =
12852 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
12853 bool modeset = needs_modeset(&intel_cstate->base);
5a21b665 12854
567f0792
ML
12855 if (!modeset &&
12856 (intel_cstate->base.color_mgmt_changed ||
12857 intel_cstate->update_pipe)) {
5c857e60
VS
12858 intel_color_set_csc(&intel_cstate->base);
12859 intel_color_load_luts(&intel_cstate->base);
567f0792
ML
12860 }
12861
5a21b665 12862 /* Perform vblank evasion around commit operation */
d3a8fb32 12863 intel_pipe_update_start(intel_cstate);
5a21b665
DV
12864
12865 if (modeset)
e62929b3 12866 goto out;
5a21b665 12867
ccf010fb 12868 if (intel_cstate->update_pipe)
1a15b77b 12869 intel_update_pipe_config(old_intel_cstate, intel_cstate);
ccf010fb 12870 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 12871 skl_detach_scalers(intel_crtc);
62e0fb88 12872
e62929b3 12873out:
ccf010fb
ML
12874 if (dev_priv->display.atomic_update_watermarks)
12875 dev_priv->display.atomic_update_watermarks(old_intel_state,
12876 intel_cstate);
5a21b665
DV
12877}
12878
12879static void intel_finish_crtc_commit(struct drm_crtc *crtc,
12880 struct drm_crtc_state *old_crtc_state)
12881{
12882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d3a8fb32
VS
12883 struct intel_atomic_state *old_intel_state =
12884 to_intel_atomic_state(old_crtc_state->state);
12885 struct intel_crtc_state *new_crtc_state =
12886 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
5a21b665 12887
d3a8fb32 12888 intel_pipe_update_end(new_crtc_state);
5a21b665
DV
12889}
12890
cf4c7c12 12891/**
4a3b8769
MR
12892 * intel_plane_destroy - destroy a plane
12893 * @plane: plane to destroy
cf4c7c12 12894 *
4a3b8769
MR
12895 * Common destruction function for all types of planes (primary, cursor,
12896 * sprite).
cf4c7c12 12897 */
4a3b8769 12898void intel_plane_destroy(struct drm_plane *plane)
465c120c 12899{
465c120c 12900 drm_plane_cleanup(plane);
69ae561f 12901 kfree(to_intel_plane(plane));
465c120c
MR
12902}
12903
714244e2
BW
12904static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
12905{
12906 switch (format) {
12907 case DRM_FORMAT_C8:
12908 case DRM_FORMAT_RGB565:
12909 case DRM_FORMAT_XRGB1555:
12910 case DRM_FORMAT_XRGB8888:
12911 return modifier == DRM_FORMAT_MOD_LINEAR ||
12912 modifier == I915_FORMAT_MOD_X_TILED;
12913 default:
12914 return false;
12915 }
12916}
12917
12918static bool i965_mod_supported(uint32_t format, uint64_t modifier)
12919{
12920 switch (format) {
12921 case DRM_FORMAT_C8:
12922 case DRM_FORMAT_RGB565:
12923 case DRM_FORMAT_XRGB8888:
12924 case DRM_FORMAT_XBGR8888:
12925 case DRM_FORMAT_XRGB2101010:
12926 case DRM_FORMAT_XBGR2101010:
12927 return modifier == DRM_FORMAT_MOD_LINEAR ||
12928 modifier == I915_FORMAT_MOD_X_TILED;
12929 default:
12930 return false;
12931 }
12932}
12933
12934static bool skl_mod_supported(uint32_t format, uint64_t modifier)
12935{
12936 switch (format) {
12937 case DRM_FORMAT_XRGB8888:
12938 case DRM_FORMAT_XBGR8888:
12939 case DRM_FORMAT_ARGB8888:
12940 case DRM_FORMAT_ABGR8888:
12941 if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
12942 modifier == I915_FORMAT_MOD_Y_TILED_CCS)
12943 return true;
12944 /* fall through */
12945 case DRM_FORMAT_RGB565:
12946 case DRM_FORMAT_XRGB2101010:
12947 case DRM_FORMAT_XBGR2101010:
12948 case DRM_FORMAT_YUYV:
12949 case DRM_FORMAT_YVYU:
12950 case DRM_FORMAT_UYVY:
12951 case DRM_FORMAT_VYUY:
12952 if (modifier == I915_FORMAT_MOD_Yf_TILED)
12953 return true;
12954 /* fall through */
12955 case DRM_FORMAT_C8:
12956 if (modifier == DRM_FORMAT_MOD_LINEAR ||
12957 modifier == I915_FORMAT_MOD_X_TILED ||
12958 modifier == I915_FORMAT_MOD_Y_TILED)
12959 return true;
12960 /* fall through */
12961 default:
12962 return false;
12963 }
12964}
12965
12966static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
12967 uint32_t format,
12968 uint64_t modifier)
12969{
12970 struct drm_i915_private *dev_priv = to_i915(plane->dev);
12971
12972 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
12973 return false;
12974
12975 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
12976 modifier != DRM_FORMAT_MOD_LINEAR)
12977 return false;
12978
12979 if (INTEL_GEN(dev_priv) >= 9)
12980 return skl_mod_supported(format, modifier);
12981 else if (INTEL_GEN(dev_priv) >= 4)
12982 return i965_mod_supported(format, modifier);
12983 else
12984 return i8xx_mod_supported(format, modifier);
12985
12986 unreachable();
12987}
12988
12989static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
12990 uint32_t format,
12991 uint64_t modifier)
12992{
12993 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
12994 return false;
12995
12996 return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
12997}
12998
12999static struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13000 .update_plane = drm_atomic_helper_update_plane,
13001 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13002 .destroy = intel_plane_destroy,
a98b3431
MR
13003 .atomic_get_property = intel_plane_atomic_get_property,
13004 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13005 .atomic_duplicate_state = intel_plane_duplicate_state,
13006 .atomic_destroy_state = intel_plane_destroy_state,
714244e2 13007 .format_mod_supported = intel_primary_plane_format_mod_supported,
465c120c
MR
13008};
13009
f79f2692
ML
13010static int
13011intel_legacy_cursor_update(struct drm_plane *plane,
13012 struct drm_crtc *crtc,
13013 struct drm_framebuffer *fb,
13014 int crtc_x, int crtc_y,
13015 unsigned int crtc_w, unsigned int crtc_h,
13016 uint32_t src_x, uint32_t src_y,
34a2ab5e
DV
13017 uint32_t src_w, uint32_t src_h,
13018 struct drm_modeset_acquire_ctx *ctx)
f79f2692
ML
13019{
13020 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13021 int ret;
13022 struct drm_plane_state *old_plane_state, *new_plane_state;
13023 struct intel_plane *intel_plane = to_intel_plane(plane);
13024 struct drm_framebuffer *old_fb;
13025 struct drm_crtc_state *crtc_state = crtc->state;
fd70075f 13026 struct i915_vma *old_vma, *vma;
f79f2692
ML
13027
13028 /*
13029 * When crtc is inactive or there is a modeset pending,
13030 * wait for it to complete in the slowpath
13031 */
13032 if (!crtc_state->active || needs_modeset(crtc_state) ||
13033 to_intel_crtc_state(crtc_state)->update_pipe)
13034 goto slow;
13035
13036 old_plane_state = plane->state;
669c9215
ML
13037 /*
13038 * Don't do an async update if there is an outstanding commit modifying
13039 * the plane. This prevents our async update's changes from getting
13040 * overridden by a previous synchronous update's state.
13041 */
13042 if (old_plane_state->commit &&
13043 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13044 goto slow;
f79f2692
ML
13045
13046 /*
13047 * If any parameters change that may affect watermarks,
13048 * take the slowpath. Only changing fb or position should be
13049 * in the fastpath.
13050 */
13051 if (old_plane_state->crtc != crtc ||
13052 old_plane_state->src_w != src_w ||
13053 old_plane_state->src_h != src_h ||
13054 old_plane_state->crtc_w != crtc_w ||
13055 old_plane_state->crtc_h != crtc_h ||
a5509abd 13056 !old_plane_state->fb != !fb)
f79f2692
ML
13057 goto slow;
13058
13059 new_plane_state = intel_plane_duplicate_state(plane);
13060 if (!new_plane_state)
13061 return -ENOMEM;
13062
13063 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13064
13065 new_plane_state->src_x = src_x;
13066 new_plane_state->src_y = src_y;
13067 new_plane_state->src_w = src_w;
13068 new_plane_state->src_h = src_h;
13069 new_plane_state->crtc_x = crtc_x;
13070 new_plane_state->crtc_y = crtc_y;
13071 new_plane_state->crtc_w = crtc_w;
13072 new_plane_state->crtc_h = crtc_h;
13073
13074 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
b2b55502
VS
13075 to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
13076 to_intel_plane_state(plane->state),
f79f2692
ML
13077 to_intel_plane_state(new_plane_state));
13078 if (ret)
13079 goto out_free;
13080
f79f2692
ML
13081 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13082 if (ret)
13083 goto out_free;
13084
13085 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13086 int align = intel_cursor_alignment(dev_priv);
f79f2692
ML
13087
13088 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13089 if (ret) {
13090 DRM_DEBUG_KMS("failed to attach phys object\n");
13091 goto out_unlock;
13092 }
13093 } else {
f79f2692
ML
13094 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13095 if (IS_ERR(vma)) {
13096 DRM_DEBUG_KMS("failed to pin object\n");
13097
13098 ret = PTR_ERR(vma);
13099 goto out_unlock;
13100 }
be1e3415
CW
13101
13102 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13103 }
13104
13105 old_fb = old_plane_state->fb;
13106
13107 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13108 intel_plane->frontbuffer_bit);
13109
13110 /* Swap plane state */
669c9215 13111 plane->state = new_plane_state;
f79f2692 13112
72259536
VS
13113 if (plane->state->visible) {
13114 trace_intel_update_plane(plane, to_intel_crtc(crtc));
282dbf9b 13115 intel_plane->update_plane(intel_plane,
a5509abd
VS
13116 to_intel_crtc_state(crtc->state),
13117 to_intel_plane_state(plane->state));
72259536
VS
13118 } else {
13119 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
282dbf9b 13120 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
72259536 13121 }
f79f2692 13122
669c9215 13123 old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
fd70075f
CW
13124 if (old_vma)
13125 intel_unpin_fb_vma(old_vma);
f79f2692
ML
13126
13127out_unlock:
13128 mutex_unlock(&dev_priv->drm.struct_mutex);
13129out_free:
669c9215
ML
13130 if (ret)
13131 intel_plane_destroy_state(plane, new_plane_state);
13132 else
13133 intel_plane_destroy_state(plane, old_plane_state);
f79f2692
ML
13134 return ret;
13135
f79f2692
ML
13136slow:
13137 return drm_atomic_helper_update_plane(plane, crtc, fb,
13138 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 13139 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
13140}
13141
13142static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13143 .update_plane = intel_legacy_cursor_update,
13144 .disable_plane = drm_atomic_helper_disable_plane,
13145 .destroy = intel_plane_destroy,
f79f2692
ML
13146 .atomic_get_property = intel_plane_atomic_get_property,
13147 .atomic_set_property = intel_plane_atomic_set_property,
13148 .atomic_duplicate_state = intel_plane_duplicate_state,
13149 .atomic_destroy_state = intel_plane_destroy_state,
714244e2 13150 .format_mod_supported = intel_cursor_plane_format_mod_supported,
f79f2692
ML
13151};
13152
b079bd17 13153static struct intel_plane *
580503c7 13154intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13155{
fca0ce2a
VS
13156 struct intel_plane *primary = NULL;
13157 struct intel_plane_state *state = NULL;
465c120c 13158 const uint32_t *intel_primary_formats;
93ca7e00 13159 unsigned int supported_rotations;
45e3743a 13160 unsigned int num_formats;
714244e2 13161 const uint64_t *modifiers;
fca0ce2a 13162 int ret;
465c120c
MR
13163
13164 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13165 if (!primary) {
13166 ret = -ENOMEM;
fca0ce2a 13167 goto fail;
b079bd17 13168 }
465c120c 13169
8e7d688b 13170 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13171 if (!state) {
13172 ret = -ENOMEM;
fca0ce2a 13173 goto fail;
b079bd17
VS
13174 }
13175
8e7d688b 13176 primary->base.state = &state->base;
ea2c67bb 13177
465c120c
MR
13178 primary->can_scale = false;
13179 primary->max_downscale = 1;
580503c7 13180 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13181 primary->can_scale = true;
af99ceda 13182 state->scaler_id = -1;
6156a456 13183 }
465c120c 13184 primary->pipe = pipe;
e3c566df
VS
13185 /*
13186 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13187 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13188 */
13189 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13190 primary->plane = (enum plane) !pipe;
13191 else
13192 primary->plane = (enum plane) pipe;
b14e5848 13193 primary->id = PLANE_PRIMARY;
a9ff8714 13194 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13195 primary->check_plane = intel_check_primary_plane;
465c120c 13196
8bc0d7ac 13197 if (INTEL_GEN(dev_priv) >= 10) {
6c0fd451
DL
13198 intel_primary_formats = skl_primary_formats;
13199 num_formats = ARRAY_SIZE(skl_primary_formats);
714244e2
BW
13200 modifiers = skl_format_modifiers_ccs;
13201
9a8cc576 13202 primary->update_plane = skl_update_plane;
779d4d8f 13203 primary->disable_plane = skl_disable_plane;
714244e2
BW
13204 } else if (INTEL_GEN(dev_priv) >= 9) {
13205 intel_primary_formats = skl_primary_formats;
13206 num_formats = ARRAY_SIZE(skl_primary_formats);
13207 if (pipe < PIPE_C)
13208 modifiers = skl_format_modifiers_ccs;
13209 else
13210 modifiers = skl_format_modifiers_noccs;
a8d201af 13211
9a8cc576 13212 primary->update_plane = skl_update_plane;
779d4d8f 13213 primary->disable_plane = skl_disable_plane;
580503c7 13214 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13215 intel_primary_formats = i965_primary_formats;
13216 num_formats = ARRAY_SIZE(i965_primary_formats);
714244e2 13217 modifiers = i9xx_format_modifiers;
a8d201af
ML
13218
13219 primary->update_plane = i9xx_update_primary_plane;
13220 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
13221 } else {
13222 intel_primary_formats = i8xx_primary_formats;
13223 num_formats = ARRAY_SIZE(i8xx_primary_formats);
714244e2 13224 modifiers = i9xx_format_modifiers;
a8d201af
ML
13225
13226 primary->update_plane = i9xx_update_primary_plane;
13227 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
13228 }
13229
580503c7
VS
13230 if (INTEL_GEN(dev_priv) >= 9)
13231 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13232 0, &intel_plane_funcs,
38573dc1 13233 intel_primary_formats, num_formats,
714244e2 13234 modifiers,
38573dc1
VS
13235 DRM_PLANE_TYPE_PRIMARY,
13236 "plane 1%c", pipe_name(pipe));
9beb5fea 13237 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13238 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13239 0, &intel_plane_funcs,
38573dc1 13240 intel_primary_formats, num_formats,
714244e2 13241 modifiers,
38573dc1
VS
13242 DRM_PLANE_TYPE_PRIMARY,
13243 "primary %c", pipe_name(pipe));
13244 else
580503c7
VS
13245 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13246 0, &intel_plane_funcs,
38573dc1 13247 intel_primary_formats, num_formats,
714244e2 13248 modifiers,
38573dc1
VS
13249 DRM_PLANE_TYPE_PRIMARY,
13250 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13251 if (ret)
13252 goto fail;
48404c1e 13253
5481e27f 13254 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 13255 supported_rotations =
c2c446ad
RF
13256 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13257 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
13258 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13259 supported_rotations =
c2c446ad
RF
13260 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13261 DRM_MODE_REFLECT_X;
5481e27f 13262 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 13263 supported_rotations =
c2c446ad 13264 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 13265 } else {
c2c446ad 13266 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
13267 }
13268
5481e27f 13269 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13270 drm_plane_create_rotation_property(&primary->base,
c2c446ad 13271 DRM_MODE_ROTATE_0,
93ca7e00 13272 supported_rotations);
48404c1e 13273
ea2c67bb
MR
13274 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13275
b079bd17 13276 return primary;
fca0ce2a
VS
13277
13278fail:
13279 kfree(state);
13280 kfree(primary);
13281
b079bd17 13282 return ERR_PTR(ret);
465c120c
MR
13283}
13284
b079bd17 13285static struct intel_plane *
b2d03b0d
VS
13286intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13287 enum pipe pipe)
3d7d6510 13288{
fca0ce2a
VS
13289 struct intel_plane *cursor = NULL;
13290 struct intel_plane_state *state = NULL;
13291 int ret;
3d7d6510
MR
13292
13293 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13294 if (!cursor) {
13295 ret = -ENOMEM;
fca0ce2a 13296 goto fail;
b079bd17 13297 }
3d7d6510 13298
8e7d688b 13299 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13300 if (!state) {
13301 ret = -ENOMEM;
fca0ce2a 13302 goto fail;
b079bd17
VS
13303 }
13304
8e7d688b 13305 cursor->base.state = &state->base;
ea2c67bb 13306
3d7d6510
MR
13307 cursor->can_scale = false;
13308 cursor->max_downscale = 1;
13309 cursor->pipe = pipe;
13310 cursor->plane = pipe;
b14e5848 13311 cursor->id = PLANE_CURSOR;
a9ff8714 13312 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
b2d03b0d
VS
13313
13314 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13315 cursor->update_plane = i845_update_cursor;
13316 cursor->disable_plane = i845_disable_cursor;
659056f2 13317 cursor->check_plane = i845_check_cursor;
b2d03b0d
VS
13318 } else {
13319 cursor->update_plane = i9xx_update_cursor;
13320 cursor->disable_plane = i9xx_disable_cursor;
659056f2 13321 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 13322 }
3d7d6510 13323
cd5dcbf1
VS
13324 cursor->cursor.base = ~0;
13325 cursor->cursor.cntl = ~0;
024faac7
VS
13326
13327 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13328 cursor->cursor.size = ~0;
3d7d6510 13329
580503c7 13330 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13331 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13332 intel_cursor_formats,
13333 ARRAY_SIZE(intel_cursor_formats),
714244e2
BW
13334 cursor_format_modifiers,
13335 DRM_PLANE_TYPE_CURSOR,
38573dc1 13336 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13337 if (ret)
13338 goto fail;
4398ad45 13339
5481e27f 13340 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13341 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
13342 DRM_MODE_ROTATE_0,
13343 DRM_MODE_ROTATE_0 |
13344 DRM_MODE_ROTATE_180);
4398ad45 13345
580503c7 13346 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13347 state->scaler_id = -1;
13348
ea2c67bb
MR
13349 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13350
b079bd17 13351 return cursor;
fca0ce2a
VS
13352
13353fail:
13354 kfree(state);
13355 kfree(cursor);
13356
b079bd17 13357 return ERR_PTR(ret);
3d7d6510
MR
13358}
13359
1c74eeaf
NM
13360static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13361 struct intel_crtc_state *crtc_state)
549e2bfb 13362{
65edccce
VS
13363 struct intel_crtc_scaler_state *scaler_state =
13364 &crtc_state->scaler_state;
1c74eeaf 13365 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13366 int i;
549e2bfb 13367
1c74eeaf
NM
13368 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13369 if (!crtc->num_scalers)
13370 return;
13371
65edccce
VS
13372 for (i = 0; i < crtc->num_scalers; i++) {
13373 struct intel_scaler *scaler = &scaler_state->scalers[i];
13374
13375 scaler->in_use = 0;
13376 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13377 }
13378
13379 scaler_state->scaler_id = -1;
13380}
13381
5ab0d85b 13382static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13383{
13384 struct intel_crtc *intel_crtc;
f5de6e07 13385 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13386 struct intel_plane *primary = NULL;
13387 struct intel_plane *cursor = NULL;
a81d6fa0 13388 int sprite, ret;
79e53945 13389
955382f3 13390 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13391 if (!intel_crtc)
13392 return -ENOMEM;
79e53945 13393
f5de6e07 13394 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13395 if (!crtc_state) {
13396 ret = -ENOMEM;
f5de6e07 13397 goto fail;
b079bd17 13398 }
550acefd
ACO
13399 intel_crtc->config = crtc_state;
13400 intel_crtc->base.state = &crtc_state->base;
07878248 13401 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13402
580503c7 13403 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13404 if (IS_ERR(primary)) {
13405 ret = PTR_ERR(primary);
3d7d6510 13406 goto fail;
b079bd17 13407 }
d97d7b48 13408 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13409
a81d6fa0 13410 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13411 struct intel_plane *plane;
13412
580503c7 13413 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13414 if (IS_ERR(plane)) {
b079bd17
VS
13415 ret = PTR_ERR(plane);
13416 goto fail;
13417 }
d97d7b48 13418 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13419 }
13420
580503c7 13421 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13422 if (IS_ERR(cursor)) {
b079bd17 13423 ret = PTR_ERR(cursor);
3d7d6510 13424 goto fail;
b079bd17 13425 }
d97d7b48 13426 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13427
5ab0d85b 13428 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13429 &primary->base, &cursor->base,
13430 &intel_crtc_funcs,
4d5d72b7 13431 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13432 if (ret)
13433 goto fail;
79e53945 13434
80824003 13435 intel_crtc->pipe = pipe;
e3c566df 13436 intel_crtc->plane = primary->plane;
80824003 13437
1c74eeaf
NM
13438 /* initialize shared scalers */
13439 intel_crtc_init_scalers(intel_crtc, crtc_state);
13440
22fd0fab
JB
13441 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13442 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13443 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13444 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13445
79e53945 13446 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13447
8563b1e8
LL
13448 intel_color_init(&intel_crtc->base);
13449
87b6b101 13450 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13451
13452 return 0;
3d7d6510
MR
13453
13454fail:
b079bd17
VS
13455 /*
13456 * drm_mode_config_cleanup() will free up any
13457 * crtcs/planes already initialized.
13458 */
f5de6e07 13459 kfree(crtc_state);
3d7d6510 13460 kfree(intel_crtc);
b079bd17
VS
13461
13462 return ret;
79e53945
JB
13463}
13464
752aa88a
JB
13465enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13466{
6e9f798d 13467 struct drm_device *dev = connector->base.dev;
752aa88a 13468
51fd371b 13469 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13470
51ec53da 13471 if (!connector->base.state->crtc)
752aa88a
JB
13472 return INVALID_PIPE;
13473
51ec53da 13474 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
13475}
13476
08d7b3d1 13477int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13478 struct drm_file *file)
08d7b3d1 13479{
08d7b3d1 13480 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13481 struct drm_crtc *drmmode_crtc;
c05422d5 13482 struct intel_crtc *crtc;
08d7b3d1 13483
418da172 13484 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
71240ed2 13485 if (!drmmode_crtc)
3f2c2057 13486 return -ENOENT;
08d7b3d1 13487
7707e653 13488 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13489 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13490
c05422d5 13491 return 0;
08d7b3d1
CW
13492}
13493
66a9278e 13494static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13495{
66a9278e
DV
13496 struct drm_device *dev = encoder->base.dev;
13497 struct intel_encoder *source_encoder;
79e53945 13498 int index_mask = 0;
79e53945
JB
13499 int entry = 0;
13500
b2784e15 13501 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13502 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13503 index_mask |= (1 << entry);
13504
79e53945
JB
13505 entry++;
13506 }
4ef69c7a 13507
79e53945
JB
13508 return index_mask;
13509}
13510
646d5772 13511static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 13512{
646d5772 13513 if (!IS_MOBILE(dev_priv))
4d302442
CW
13514 return false;
13515
13516 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13517 return false;
13518
5db94019 13519 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13520 return false;
13521
13522 return true;
13523}
13524
6315b5d3 13525static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 13526{
6315b5d3 13527 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
13528 return false;
13529
50a0bc90 13530 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
13531 return false;
13532
920a14b2 13533 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
13534 return false;
13535
4f8036a2
TU
13536 if (HAS_PCH_LPT_H(dev_priv) &&
13537 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
13538 return false;
13539
70ac54d0 13540 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 13541 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
13542 return false;
13543
e4abb733 13544 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
13545 return false;
13546
13547 return true;
13548}
13549
8090ba8c
ID
13550void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13551{
13552 int pps_num;
13553 int pps_idx;
13554
13555 if (HAS_DDI(dev_priv))
13556 return;
13557 /*
13558 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13559 * everywhere where registers can be write protected.
13560 */
13561 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13562 pps_num = 2;
13563 else
13564 pps_num = 1;
13565
13566 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13567 u32 val = I915_READ(PP_CONTROL(pps_idx));
13568
13569 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13570 I915_WRITE(PP_CONTROL(pps_idx), val);
13571 }
13572}
13573
44cb734c
ID
13574static void intel_pps_init(struct drm_i915_private *dev_priv)
13575{
cc3f90f0 13576 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
13577 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13578 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13579 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13580 else
13581 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
13582
13583 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
13584}
13585
c39055b0 13586static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 13587{
4ef69c7a 13588 struct intel_encoder *encoder;
cb0953d7 13589 bool dpd_is_edp = false;
79e53945 13590
44cb734c
ID
13591 intel_pps_init(dev_priv);
13592
97a824e1
ID
13593 /*
13594 * intel_edp_init_connector() depends on this completing first, to
13595 * prevent the registeration of both eDP and LVDS and the incorrect
13596 * sharing of the PPS.
13597 */
c39055b0 13598 intel_lvds_init(dev_priv);
79e53945 13599
6315b5d3 13600 if (intel_crt_present(dev_priv))
c39055b0 13601 intel_crt_init(dev_priv);
cb0953d7 13602
cc3f90f0 13603 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
13604 /*
13605 * FIXME: Broxton doesn't support port detection via the
13606 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13607 * detect the ports.
13608 */
c39055b0
ACO
13609 intel_ddi_init(dev_priv, PORT_A);
13610 intel_ddi_init(dev_priv, PORT_B);
13611 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 13612
c39055b0 13613 intel_dsi_init(dev_priv);
4f8036a2 13614 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
13615 int found;
13616
de31facd
JB
13617 /*
13618 * Haswell uses DDI functions to detect digital outputs.
13619 * On SKL pre-D0 the strap isn't connected, so we assume
13620 * it's there.
13621 */
77179400 13622 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 13623 /* WaIgnoreDDIAStrap: skl */
b976dc53 13624 if (found || IS_GEN9_BC(dev_priv))
c39055b0 13625 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
13626
13627 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13628 * register */
13629 found = I915_READ(SFUSE_STRAP);
13630
13631 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 13632 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 13633 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 13634 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 13635 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 13636 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
13637 /*
13638 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13639 */
b976dc53 13640 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
13641 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
13642 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
13643 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 13644 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 13645
6e266956 13646 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 13647 int found;
7b91bf7f 13648 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
270b3042 13649
646d5772 13650 if (has_edp_a(dev_priv))
c39055b0 13651 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 13652
dc0fa718 13653 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13654 /* PCH SDVOB multiplex with HDMIB */
c39055b0 13655 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 13656 if (!found)
c39055b0 13657 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 13658 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 13659 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
13660 }
13661
dc0fa718 13662 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 13663 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 13664
dc0fa718 13665 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 13666 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 13667
5eb08b69 13668 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 13669 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 13670
270b3042 13671 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 13672 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 13673 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 13674 bool has_edp, has_port;
457c52d8 13675
e17ac6db
VS
13676 /*
13677 * The DP_DETECTED bit is the latched state of the DDC
13678 * SDA pin at boot. However since eDP doesn't require DDC
13679 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13680 * eDP ports may have been muxed to an alternate function.
13681 * Thus we can't rely on the DP_DETECTED bit alone to detect
13682 * eDP ports. Consult the VBT as well as DP_DETECTED to
13683 * detect eDP ports.
22f35042
VS
13684 *
13685 * Sadly the straps seem to be missing sometimes even for HDMI
13686 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
13687 * and VBT for the presence of the port. Additionally we can't
13688 * trust the port type the VBT declares as we've seen at least
13689 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 13690 */
7b91bf7f 13691 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
22f35042
VS
13692 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
13693 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 13694 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 13695 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 13696 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 13697
7b91bf7f 13698 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
22f35042
VS
13699 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
13700 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 13701 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 13702 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 13703 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 13704
920a14b2 13705 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
13706 /*
13707 * eDP not supported on port D,
13708 * so no need to worry about it
13709 */
13710 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
13711 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 13712 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 13713 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 13714 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
13715 }
13716
c39055b0 13717 intel_dsi_init(dev_priv);
5db94019 13718 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 13719 bool found = false;
7d57382e 13720
e2debe91 13721 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13722 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 13723 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 13724 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 13725 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 13726 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 13727 }
27185ae1 13728
9beb5fea 13729 if (!found && IS_G4X(dev_priv))
c39055b0 13730 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 13731 }
13520b05
KH
13732
13733 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 13734
e2debe91 13735 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13736 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 13737 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 13738 }
27185ae1 13739
e2debe91 13740 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 13741
9beb5fea 13742 if (IS_G4X(dev_priv)) {
b01f2c3a 13743 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 13744 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 13745 }
9beb5fea 13746 if (IS_G4X(dev_priv))
c39055b0 13747 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 13748 }
27185ae1 13749
9beb5fea 13750 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 13751 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 13752 } else if (IS_GEN2(dev_priv))
c39055b0 13753 intel_dvo_init(dev_priv);
79e53945 13754
56b857a5 13755 if (SUPPORTS_TV(dev_priv))
c39055b0 13756 intel_tv_init(dev_priv);
79e53945 13757
c39055b0 13758 intel_psr_init(dev_priv);
7c8f8a70 13759
c39055b0 13760 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
13761 encoder->base.possible_crtcs = encoder->crtc_mask;
13762 encoder->base.possible_clones =
66a9278e 13763 intel_encoder_clones(encoder);
79e53945 13764 }
47356eb6 13765
c39055b0 13766 intel_init_pch_refclk(dev_priv);
270b3042 13767
c39055b0 13768 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
13769}
13770
13771static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13772{
13773 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 13774
ef2d633e 13775 drm_framebuffer_cleanup(fb);
70001cd2 13776
dd689287
CW
13777 i915_gem_object_lock(intel_fb->obj);
13778 WARN_ON(!intel_fb->obj->framebuffer_references--);
13779 i915_gem_object_unlock(intel_fb->obj);
13780
f8c417cd 13781 i915_gem_object_put(intel_fb->obj);
70001cd2 13782
79e53945
JB
13783 kfree(intel_fb);
13784}
13785
13786static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 13787 struct drm_file *file,
79e53945
JB
13788 unsigned int *handle)
13789{
13790 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 13791 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 13792
cc917ab4
CW
13793 if (obj->userptr.mm) {
13794 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
13795 return -EINVAL;
13796 }
13797
05394f39 13798 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
13799}
13800
86c98588
RV
13801static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
13802 struct drm_file *file,
13803 unsigned flags, unsigned color,
13804 struct drm_clip_rect *clips,
13805 unsigned num_clips)
13806{
5a97bcc6 13807 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 13808
5a97bcc6 13809 i915_gem_object_flush_if_display(obj);
d59b21ec 13810 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
13811
13812 return 0;
13813}
13814
79e53945
JB
13815static const struct drm_framebuffer_funcs intel_fb_funcs = {
13816 .destroy = intel_user_framebuffer_destroy,
13817 .create_handle = intel_user_framebuffer_create_handle,
86c98588 13818 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
13819};
13820
b321803d 13821static
920a14b2
TU
13822u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
13823 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 13824{
24dbf51a 13825 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
13826
13827 if (gen >= 9) {
ac484963
VS
13828 int cpp = drm_format_plane_cpp(pixel_format, 0);
13829
b321803d
DL
13830 /* "The stride in bytes must not exceed the of the size of 8K
13831 * pixels and 32K bytes."
13832 */
ac484963 13833 return min(8192 * cpp, 32768);
6401c37d 13834 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
13835 return 32*1024;
13836 } else if (gen >= 4) {
13837 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13838 return 16*1024;
13839 else
13840 return 32*1024;
13841 } else if (gen >= 3) {
13842 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13843 return 8*1024;
13844 else
13845 return 16*1024;
13846 } else {
13847 /* XXX DSPC is limited to 4k tiled */
13848 return 8*1024;
13849 }
13850}
13851
24dbf51a
CW
13852static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
13853 struct drm_i915_gem_object *obj,
13854 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 13855{
24dbf51a 13856 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2e2adb05 13857 struct drm_framebuffer *fb = &intel_fb->base;
b3c11ac2 13858 struct drm_format_name_buf format_name;
2e2adb05 13859 u32 pitch_limit;
dd689287 13860 unsigned int tiling, stride;
24dbf51a 13861 int ret = -EINVAL;
2e2adb05 13862 int i;
79e53945 13863
dd689287
CW
13864 i915_gem_object_lock(obj);
13865 obj->framebuffer_references++;
13866 tiling = i915_gem_object_get_tiling(obj);
13867 stride = i915_gem_object_get_stride(obj);
13868 i915_gem_object_unlock(obj);
dd4916c5 13869
2a80eada 13870 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
13871 /*
13872 * If there's a fence, enforce that
13873 * the fb modifier and tiling mode match.
13874 */
13875 if (tiling != I915_TILING_NONE &&
13876 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 13877 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 13878 goto err;
2a80eada
DV
13879 }
13880 } else {
c2ff7370 13881 if (tiling == I915_TILING_X) {
2a80eada 13882 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 13883 } else if (tiling == I915_TILING_Y) {
144cc143 13884 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 13885 goto err;
2a80eada
DV
13886 }
13887 }
13888
9a8f0a12
TU
13889 /* Passed in modifier sanity checking. */
13890 switch (mode_cmd->modifier[0]) {
2e2adb05
VS
13891 case I915_FORMAT_MOD_Y_TILED_CCS:
13892 case I915_FORMAT_MOD_Yf_TILED_CCS:
13893 switch (mode_cmd->pixel_format) {
13894 case DRM_FORMAT_XBGR8888:
13895 case DRM_FORMAT_ABGR8888:
13896 case DRM_FORMAT_XRGB8888:
13897 case DRM_FORMAT_ARGB8888:
13898 break;
13899 default:
13900 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
13901 goto err;
13902 }
13903 /* fall through */
9a8f0a12
TU
13904 case I915_FORMAT_MOD_Y_TILED:
13905 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 13906 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
13907 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
13908 mode_cmd->modifier[0]);
24dbf51a 13909 goto err;
9a8f0a12 13910 }
2f075565 13911 case DRM_FORMAT_MOD_LINEAR:
9a8f0a12
TU
13912 case I915_FORMAT_MOD_X_TILED:
13913 break;
13914 default:
144cc143
VS
13915 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
13916 mode_cmd->modifier[0]);
24dbf51a 13917 goto err;
c16ed4be 13918 }
57cd6508 13919
c2ff7370
VS
13920 /*
13921 * gen2/3 display engine uses the fence if present,
13922 * so the tiling mode must match the fb modifier exactly.
13923 */
13924 if (INTEL_INFO(dev_priv)->gen < 4 &&
13925 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 13926 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 13927 goto err;
c2ff7370
VS
13928 }
13929
920a14b2 13930 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 13931 mode_cmd->pixel_format);
a35cdaa0 13932 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143 13933 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
2f075565 13934 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
144cc143
VS
13935 "tiled" : "linear",
13936 mode_cmd->pitches[0], pitch_limit);
24dbf51a 13937 goto err;
c16ed4be 13938 }
5d7bd705 13939
c2ff7370
VS
13940 /*
13941 * If there's a fence, enforce that
13942 * the fb pitch and fence stride match.
13943 */
144cc143
VS
13944 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
13945 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
13946 mode_cmd->pitches[0], stride);
24dbf51a 13947 goto err;
c16ed4be 13948 }
5d7bd705 13949
57779d06 13950 /* Reject formats not supported by any plane early. */
308e5bcb 13951 switch (mode_cmd->pixel_format) {
57779d06 13952 case DRM_FORMAT_C8:
04b3924d
VS
13953 case DRM_FORMAT_RGB565:
13954 case DRM_FORMAT_XRGB8888:
13955 case DRM_FORMAT_ARGB8888:
57779d06
VS
13956 break;
13957 case DRM_FORMAT_XRGB1555:
6315b5d3 13958 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
13959 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13960 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13961 goto err;
c16ed4be 13962 }
57779d06 13963 break;
57779d06 13964 case DRM_FORMAT_ABGR8888:
920a14b2 13965 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 13966 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
13967 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13968 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13969 goto err;
6c0fd451
DL
13970 }
13971 break;
13972 case DRM_FORMAT_XBGR8888:
04b3924d 13973 case DRM_FORMAT_XRGB2101010:
57779d06 13974 case DRM_FORMAT_XBGR2101010:
6315b5d3 13975 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
13976 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13977 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13978 goto err;
c16ed4be 13979 }
b5626747 13980 break;
7531208b 13981 case DRM_FORMAT_ABGR2101010:
920a14b2 13982 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
13983 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13984 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13985 goto err;
7531208b
DL
13986 }
13987 break;
04b3924d
VS
13988 case DRM_FORMAT_YUYV:
13989 case DRM_FORMAT_UYVY:
13990 case DRM_FORMAT_YVYU:
13991 case DRM_FORMAT_VYUY:
ab33081a 13992 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
144cc143
VS
13993 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13994 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13995 goto err;
c16ed4be 13996 }
57cd6508
CW
13997 break;
13998 default:
144cc143
VS
13999 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14000 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14001 goto err;
57cd6508
CW
14002 }
14003
90f9a336
VS
14004 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14005 if (mode_cmd->offsets[0] != 0)
24dbf51a 14006 goto err;
90f9a336 14007
2e2adb05 14008 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
d88c4afd 14009
2e2adb05
VS
14010 for (i = 0; i < fb->format->num_planes; i++) {
14011 u32 stride_alignment;
14012
14013 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14014 DRM_DEBUG_KMS("bad plane %d handle\n", i);
814feed3 14015 goto err;
2e2adb05
VS
14016 }
14017
14018 stride_alignment = intel_fb_stride_alignment(fb, i);
14019
14020 /*
14021 * Display WA #0531: skl,bxt,kbl,glk
14022 *
14023 * Render decompression and plane width > 3840
14024 * combined with horizontal panning requires the
14025 * plane stride to be a multiple of 4. We'll just
14026 * require the entire fb to accommodate that to avoid
14027 * potential runtime errors at plane configuration time.
14028 */
14029 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
14030 (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
14031 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
14032 stride_alignment *= 4;
14033
14034 if (fb->pitches[i] & (stride_alignment - 1)) {
14035 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14036 i, fb->pitches[i], stride_alignment);
14037 goto err;
14038 }
d88c4afd
VS
14039 }
14040
c7d73f6a
DV
14041 intel_fb->obj = obj;
14042
2e2adb05 14043 ret = intel_fill_fb_info(dev_priv, fb);
6687c906 14044 if (ret)
9aceb5c1 14045 goto err;
2d7a215f 14046
2e2adb05 14047 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
79e53945
JB
14048 if (ret) {
14049 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14050 goto err;
79e53945
JB
14051 }
14052
79e53945 14053 return 0;
24dbf51a
CW
14054
14055err:
dd689287
CW
14056 i915_gem_object_lock(obj);
14057 obj->framebuffer_references--;
14058 i915_gem_object_unlock(obj);
24dbf51a 14059 return ret;
79e53945
JB
14060}
14061
79e53945
JB
14062static struct drm_framebuffer *
14063intel_user_framebuffer_create(struct drm_device *dev,
14064 struct drm_file *filp,
1eb83451 14065 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14066{
dcb1394e 14067 struct drm_framebuffer *fb;
05394f39 14068 struct drm_i915_gem_object *obj;
76dc3769 14069 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14070
03ac0642
CW
14071 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14072 if (!obj)
cce13ff7 14073 return ERR_PTR(-ENOENT);
79e53945 14074
24dbf51a 14075 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14076 if (IS_ERR(fb))
f0cd5182 14077 i915_gem_object_put(obj);
dcb1394e
LW
14078
14079 return fb;
79e53945
JB
14080}
14081
778e23a9
CW
14082static void intel_atomic_state_free(struct drm_atomic_state *state)
14083{
14084 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14085
14086 drm_atomic_state_default_release(state);
14087
14088 i915_sw_fence_fini(&intel_state->commit_ready);
14089
14090 kfree(state);
14091}
14092
79e53945 14093static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14094 .fb_create = intel_user_framebuffer_create,
bbfb6ce8 14095 .get_format_info = intel_get_format_info,
0632fef6 14096 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14097 .atomic_check = intel_atomic_check,
14098 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14099 .atomic_state_alloc = intel_atomic_state_alloc,
14100 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14101 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14102};
14103
88212941
ID
14104/**
14105 * intel_init_display_hooks - initialize the display modesetting hooks
14106 * @dev_priv: device private
14107 */
14108void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14109{
7ff89ca2
VS
14110 intel_init_cdclk_hooks(dev_priv);
14111
88212941 14112 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14113 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14114 dev_priv->display.get_initial_plane_config =
14115 skylake_get_initial_plane_config;
bc8d7dff
DL
14116 dev_priv->display.crtc_compute_clock =
14117 haswell_crtc_compute_clock;
14118 dev_priv->display.crtc_enable = haswell_crtc_enable;
14119 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14120 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14121 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14122 dev_priv->display.get_initial_plane_config =
14123 ironlake_get_initial_plane_config;
797d0259
ACO
14124 dev_priv->display.crtc_compute_clock =
14125 haswell_crtc_compute_clock;
4f771f10
PZ
14126 dev_priv->display.crtc_enable = haswell_crtc_enable;
14127 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14128 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14129 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14130 dev_priv->display.get_initial_plane_config =
14131 ironlake_get_initial_plane_config;
3fb37703
ACO
14132 dev_priv->display.crtc_compute_clock =
14133 ironlake_crtc_compute_clock;
76e5a89c
DV
14134 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14135 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14136 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14137 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14138 dev_priv->display.get_initial_plane_config =
14139 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14140 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14141 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14142 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14143 } else if (IS_VALLEYVIEW(dev_priv)) {
14144 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14145 dev_priv->display.get_initial_plane_config =
14146 i9xx_get_initial_plane_config;
14147 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14148 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14149 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14150 } else if (IS_G4X(dev_priv)) {
14151 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14152 dev_priv->display.get_initial_plane_config =
14153 i9xx_get_initial_plane_config;
14154 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14155 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14156 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14157 } else if (IS_PINEVIEW(dev_priv)) {
14158 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14159 dev_priv->display.get_initial_plane_config =
14160 i9xx_get_initial_plane_config;
14161 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14162 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14163 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14164 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14165 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14166 dev_priv->display.get_initial_plane_config =
14167 i9xx_get_initial_plane_config;
d6dfee7a 14168 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14169 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14170 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14171 } else {
14172 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14173 dev_priv->display.get_initial_plane_config =
14174 i9xx_get_initial_plane_config;
14175 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14176 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14177 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14178 }
e70236a8 14179
88212941 14180 if (IS_GEN5(dev_priv)) {
3bb11b53 14181 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14182 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14183 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14184 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14185 /* FIXME: detect B0+ stepping and use auto training */
14186 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14187 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14188 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14189 }
14190
bd30ca2d 14191 if (INTEL_GEN(dev_priv) >= 9)
27082493
L
14192 dev_priv->display.update_crtcs = skl_update_crtcs;
14193 else
14194 dev_priv->display.update_crtcs = intel_update_crtcs;
e70236a8
JB
14195}
14196
435793df
KP
14197/*
14198 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14199 */
14200static void quirk_ssc_force_disable(struct drm_device *dev)
14201{
fac5e23e 14202 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14203 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14204 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14205}
14206
4dca20ef 14207/*
5a15ab5b
CE
14208 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14209 * brightness value
4dca20ef
CE
14210 */
14211static void quirk_invert_brightness(struct drm_device *dev)
14212{
fac5e23e 14213 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14214 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14215 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14216}
14217
9c72cc6f
SD
14218/* Some VBT's incorrectly indicate no backlight is present */
14219static void quirk_backlight_present(struct drm_device *dev)
14220{
fac5e23e 14221 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14222 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14223 DRM_INFO("applying backlight present quirk\n");
14224}
14225
c99a259b
MN
14226/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14227 * which is 300 ms greater than eDP spec T12 min.
14228 */
14229static void quirk_increase_t12_delay(struct drm_device *dev)
14230{
14231 struct drm_i915_private *dev_priv = to_i915(dev);
14232
14233 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14234 DRM_INFO("Applying T12 delay quirk\n");
14235}
14236
b690e96c
JB
14237struct intel_quirk {
14238 int device;
14239 int subsystem_vendor;
14240 int subsystem_device;
14241 void (*hook)(struct drm_device *dev);
14242};
14243
5f85f176
EE
14244/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14245struct intel_dmi_quirk {
14246 void (*hook)(struct drm_device *dev);
14247 const struct dmi_system_id (*dmi_id_list)[];
14248};
14249
14250static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14251{
14252 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14253 return 1;
14254}
14255
14256static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14257 {
14258 .dmi_id_list = &(const struct dmi_system_id[]) {
14259 {
14260 .callback = intel_dmi_reverse_brightness,
14261 .ident = "NCR Corporation",
14262 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14263 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14264 },
14265 },
14266 { } /* terminating entry */
14267 },
14268 .hook = quirk_invert_brightness,
14269 },
14270};
14271
c43b5634 14272static struct intel_quirk intel_quirks[] = {
435793df
KP
14273 /* Lenovo U160 cannot use SSC on LVDS */
14274 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14275
14276 /* Sony Vaio Y cannot use SSC on LVDS */
14277 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14278
be505f64
AH
14279 /* Acer Aspire 5734Z must invert backlight brightness */
14280 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14281
14282 /* Acer/eMachines G725 */
14283 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14284
14285 /* Acer/eMachines e725 */
14286 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14287
14288 /* Acer/Packard Bell NCL20 */
14289 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14290
14291 /* Acer Aspire 4736Z */
14292 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14293
14294 /* Acer Aspire 5336 */
14295 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14296
14297 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14298 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14299
dfb3d47b
SD
14300 /* Acer C720 Chromebook (Core i3 4005U) */
14301 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14302
b2a9601c 14303 /* Apple Macbook 2,1 (Core 2 T7400) */
14304 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14305
1b9448b0
JN
14306 /* Apple Macbook 4,1 */
14307 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14308
d4967d8c
SD
14309 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14310 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14311
14312 /* HP Chromebook 14 (Celeron 2955U) */
14313 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14314
14315 /* Dell Chromebook 11 */
14316 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14317
14318 /* Dell Chromebook 11 (2015 version) */
14319 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
c99a259b
MN
14320
14321 /* Toshiba Satellite P50-C-18C */
14322 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
b690e96c
JB
14323};
14324
14325static void intel_init_quirks(struct drm_device *dev)
14326{
14327 struct pci_dev *d = dev->pdev;
14328 int i;
14329
14330 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14331 struct intel_quirk *q = &intel_quirks[i];
14332
14333 if (d->device == q->device &&
14334 (d->subsystem_vendor == q->subsystem_vendor ||
14335 q->subsystem_vendor == PCI_ANY_ID) &&
14336 (d->subsystem_device == q->subsystem_device ||
14337 q->subsystem_device == PCI_ANY_ID))
14338 q->hook(dev);
14339 }
5f85f176
EE
14340 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14341 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14342 intel_dmi_quirks[i].hook(dev);
14343 }
b690e96c
JB
14344}
14345
9cce37f4 14346/* Disable the VGA plane that we never use */
29b74b7f 14347static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14348{
52a05c30 14349 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14350 u8 sr1;
920a14b2 14351 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14352
2b37c616 14353 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14354 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14355 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14356 sr1 = inb(VGA_SR_DATA);
14357 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14358 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14359 udelay(300);
14360
01f5a626 14361 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14362 POSTING_READ(vga_reg);
14363}
14364
f817586c
DV
14365void intel_modeset_init_hw(struct drm_device *dev)
14366{
fac5e23e 14367 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14368
4c75b940 14369 intel_update_cdclk(dev_priv);
bb0f4aab 14370 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
f817586c
DV
14371}
14372
d93c0372
MR
14373/*
14374 * Calculate what we think the watermarks should be for the state we've read
14375 * out of the hardware and then immediately program those watermarks so that
14376 * we ensure the hardware settings match our internal state.
14377 *
14378 * We can calculate what we think WM's should be by creating a duplicate of the
14379 * current state (which was constructed during hardware readout) and running it
14380 * through the atomic check code to calculate new watermark values in the
14381 * state object.
14382 */
14383static void sanitize_watermarks(struct drm_device *dev)
14384{
14385 struct drm_i915_private *dev_priv = to_i915(dev);
14386 struct drm_atomic_state *state;
ccf010fb 14387 struct intel_atomic_state *intel_state;
d93c0372
MR
14388 struct drm_crtc *crtc;
14389 struct drm_crtc_state *cstate;
14390 struct drm_modeset_acquire_ctx ctx;
14391 int ret;
14392 int i;
14393
14394 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14395 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14396 return;
14397
14398 /*
14399 * We need to hold connection_mutex before calling duplicate_state so
14400 * that the connector loop is protected.
14401 */
14402 drm_modeset_acquire_init(&ctx, 0);
14403retry:
0cd1262d 14404 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14405 if (ret == -EDEADLK) {
14406 drm_modeset_backoff(&ctx);
14407 goto retry;
14408 } else if (WARN_ON(ret)) {
0cd1262d 14409 goto fail;
d93c0372
MR
14410 }
14411
14412 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14413 if (WARN_ON(IS_ERR(state)))
0cd1262d 14414 goto fail;
d93c0372 14415
ccf010fb
ML
14416 intel_state = to_intel_atomic_state(state);
14417
ed4a6a7c
MR
14418 /*
14419 * Hardware readout is the only time we don't want to calculate
14420 * intermediate watermarks (since we don't trust the current
14421 * watermarks).
14422 */
602ae835
VS
14423 if (!HAS_GMCH_DISPLAY(dev_priv))
14424 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14425
d93c0372
MR
14426 ret = intel_atomic_check(dev, state);
14427 if (ret) {
14428 /*
14429 * If we fail here, it means that the hardware appears to be
14430 * programmed in a way that shouldn't be possible, given our
14431 * understanding of watermark requirements. This might mean a
14432 * mistake in the hardware readout code or a mistake in the
14433 * watermark calculations for a given platform. Raise a WARN
14434 * so that this is noticeable.
14435 *
14436 * If this actually happens, we'll have to just leave the
14437 * BIOS-programmed watermarks untouched and hope for the best.
14438 */
14439 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14440 goto put_state;
d93c0372
MR
14441 }
14442
14443 /* Write calculated watermark values back */
aa5e9b47 14444 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
14445 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14446
ed4a6a7c 14447 cs->wm.need_postvbl_update = true;
ccf010fb 14448 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
14449 }
14450
b9a1b717 14451put_state:
0853695c 14452 drm_atomic_state_put(state);
0cd1262d 14453fail:
d93c0372
MR
14454 drm_modeset_drop_locks(&ctx);
14455 drm_modeset_acquire_fini(&ctx);
14456}
14457
b079bd17 14458int intel_modeset_init(struct drm_device *dev)
79e53945 14459{
72e96d64
JL
14460 struct drm_i915_private *dev_priv = to_i915(dev);
14461 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14462 enum pipe pipe;
46f297fb 14463 struct intel_crtc *crtc;
79e53945
JB
14464
14465 drm_mode_config_init(dev);
14466
14467 dev->mode_config.min_width = 0;
14468 dev->mode_config.min_height = 0;
14469
019d96cb
DA
14470 dev->mode_config.preferred_depth = 24;
14471 dev->mode_config.prefer_shadow = 1;
14472
25bab385
TU
14473 dev->mode_config.allow_fb_modifiers = true;
14474
e6ecefaa 14475 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14476
400c19d9 14477 init_llist_head(&dev_priv->atomic_helper.free_list);
eb955eee 14478 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 14479 intel_atomic_helper_free_state_worker);
eb955eee 14480
b690e96c
JB
14481 intel_init_quirks(dev);
14482
62d75df7 14483 intel_init_pm(dev_priv);
1fa61106 14484
b7f05d4a 14485 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 14486 return 0;
e3c74757 14487
69f92f67
LW
14488 /*
14489 * There may be no VBT; and if the BIOS enabled SSC we can
14490 * just keep using it to avoid unnecessary flicker. Whereas if the
14491 * BIOS isn't using it, don't assume it will work even if the VBT
14492 * indicates as much.
14493 */
6e266956 14494 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
14495 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14496 DREF_SSC1_ENABLE);
14497
14498 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14499 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14500 bios_lvds_use_ssc ? "en" : "dis",
14501 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14502 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14503 }
14504 }
14505
5db94019 14506 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
14507 dev->mode_config.max_width = 2048;
14508 dev->mode_config.max_height = 2048;
5db94019 14509 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
14510 dev->mode_config.max_width = 4096;
14511 dev->mode_config.max_height = 4096;
79e53945 14512 } else {
a6c45cf0
CW
14513 dev->mode_config.max_width = 8192;
14514 dev->mode_config.max_height = 8192;
79e53945 14515 }
068be561 14516
2a307c2e
JN
14517 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14518 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 14519 dev->mode_config.cursor_height = 1023;
5db94019 14520 } else if (IS_GEN2(dev_priv)) {
068be561
DL
14521 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14522 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14523 } else {
14524 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14525 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14526 }
14527
72e96d64 14528 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 14529
28c97730 14530 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
14531 INTEL_INFO(dev_priv)->num_pipes,
14532 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 14533
055e393f 14534 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
14535 int ret;
14536
5ab0d85b 14537 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
14538 if (ret) {
14539 drm_mode_config_cleanup(dev);
14540 return ret;
14541 }
79e53945
JB
14542 }
14543
e72f9fbf 14544 intel_shared_dpll_init(dev);
ee7b9f93 14545
5be6e334
VS
14546 intel_update_czclk(dev_priv);
14547 intel_modeset_init_hw(dev);
14548
b2045352 14549 if (dev_priv->max_cdclk_freq == 0)
4c75b940 14550 intel_update_max_cdclk(dev_priv);
b2045352 14551
9cce37f4 14552 /* Just disable it once at startup */
29b74b7f 14553 i915_disable_vga(dev_priv);
c39055b0 14554 intel_setup_outputs(dev_priv);
11be49eb 14555
6e9f798d 14556 drm_modeset_lock_all(dev);
aecd36b8 14557 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 14558 drm_modeset_unlock_all(dev);
46f297fb 14559
d3fcc808 14560 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
14561 struct intel_initial_plane_config plane_config = {};
14562
46f297fb
JB
14563 if (!crtc->active)
14564 continue;
14565
46f297fb 14566 /*
46f297fb
JB
14567 * Note that reserving the BIOS fb up front prevents us
14568 * from stuffing other stolen allocations like the ring
14569 * on top. This prevents some ugliness at boot time, and
14570 * can even allow for smooth boot transitions if the BIOS
14571 * fb is large enough for the active pipe configuration.
14572 */
eeebeac5
ML
14573 dev_priv->display.get_initial_plane_config(crtc,
14574 &plane_config);
14575
14576 /*
14577 * If the fb is shared between multiple heads, we'll
14578 * just get the first one.
14579 */
14580 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 14581 }
d93c0372
MR
14582
14583 /*
14584 * Make sure hardware watermarks really match the state we read out.
14585 * Note that we need to do this after reconstructing the BIOS fb's
14586 * since the watermark calculation done here will use pstate->fb.
14587 */
602ae835
VS
14588 if (!HAS_GMCH_DISPLAY(dev_priv))
14589 sanitize_watermarks(dev);
b079bd17
VS
14590
14591 return 0;
2c7111db
CW
14592}
14593
2ee0da16
VS
14594void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
14595{
14596 /* 640x480@60Hz, ~25175 kHz */
14597 struct dpll clock = {
14598 .m1 = 18,
14599 .m2 = 7,
14600 .p1 = 13,
14601 .p2 = 4,
14602 .n = 2,
14603 };
14604 u32 dpll, fp;
14605 int i;
14606
14607 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
14608
14609 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
14610 pipe_name(pipe), clock.vco, clock.dot);
14611
14612 fp = i9xx_dpll_compute_fp(&clock);
14613 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
14614 DPLL_VGA_MODE_DIS |
14615 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
14616 PLL_P2_DIVIDE_BY_4 |
14617 PLL_REF_INPUT_DREFCLK |
14618 DPLL_VCO_ENABLE;
14619
14620 I915_WRITE(FP0(pipe), fp);
14621 I915_WRITE(FP1(pipe), fp);
14622
14623 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
14624 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
14625 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
14626 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
14627 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
14628 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
14629 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
14630
14631 /*
14632 * Apparently we need to have VGA mode enabled prior to changing
14633 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
14634 * dividers, even though the register value does change.
14635 */
14636 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
14637 I915_WRITE(DPLL(pipe), dpll);
14638
14639 /* Wait for the clocks to stabilize. */
14640 POSTING_READ(DPLL(pipe));
14641 udelay(150);
14642
14643 /* The pixel multiplier can only be updated once the
14644 * DPLL is enabled and the clocks are stable.
14645 *
14646 * So write it again.
14647 */
14648 I915_WRITE(DPLL(pipe), dpll);
14649
14650 /* We do this three times for luck */
14651 for (i = 0; i < 3 ; i++) {
14652 I915_WRITE(DPLL(pipe), dpll);
14653 POSTING_READ(DPLL(pipe));
14654 udelay(150); /* wait for warmup */
14655 }
14656
14657 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
14658 POSTING_READ(PIPECONF(pipe));
14659}
14660
14661void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
14662{
a87e55f8
VS
14663 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
14664
2ee0da16
VS
14665 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
14666 pipe_name(pipe));
14667
14668 assert_plane_disabled(dev_priv, PLANE_A);
14669 assert_plane_disabled(dev_priv, PLANE_B);
14670
14671 I915_WRITE(PIPECONF(pipe), 0);
14672 POSTING_READ(PIPECONF(pipe));
14673
a87e55f8 14674 intel_wait_for_pipe_scanline_stopped(crtc);
2ee0da16
VS
14675
14676 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
14677 POSTING_READ(DPLL(pipe));
14678}
14679
fa555837
DV
14680static bool
14681intel_check_plane_mapping(struct intel_crtc *crtc)
14682{
b7f05d4a 14683 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 14684 u32 val;
fa555837 14685
b7f05d4a 14686 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
DV
14687 return true;
14688
649636ef 14689 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
14690
14691 if ((val & DISPLAY_PLANE_ENABLE) &&
14692 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14693 return false;
14694
14695 return true;
14696}
14697
02e93c35
VS
14698static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14699{
14700 struct drm_device *dev = crtc->base.dev;
14701 struct intel_encoder *encoder;
14702
14703 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14704 return true;
14705
14706 return false;
14707}
14708
496b0fc3
ML
14709static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
14710{
14711 struct drm_device *dev = encoder->base.dev;
14712 struct intel_connector *connector;
14713
14714 for_each_connector_on_encoder(dev, &encoder->base, connector)
14715 return connector;
14716
14717 return NULL;
14718}
14719
a168f5b3 14720static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
ecf837d9 14721 enum pipe pch_transcoder)
a168f5b3
VS
14722{
14723 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
ecf837d9 14724 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
a168f5b3
VS
14725}
14726
aecd36b8
VS
14727static void intel_sanitize_crtc(struct intel_crtc *crtc,
14728 struct drm_modeset_acquire_ctx *ctx)
24929352
DV
14729{
14730 struct drm_device *dev = crtc->base.dev;
fac5e23e 14731 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 14732 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 14733
24929352 14734 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
14735 if (!transcoder_is_dsi(cpu_transcoder)) {
14736 i915_reg_t reg = PIPECONF(cpu_transcoder);
14737
14738 I915_WRITE(reg,
14739 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14740 }
24929352 14741
d3eaf884 14742 /* restore vblank interrupts to correct state */
9625604c 14743 drm_crtc_vblank_reset(&crtc->base);
d297e103 14744 if (crtc->active) {
f9cd7b88
VS
14745 struct intel_plane *plane;
14746
9625604c 14747 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
14748
14749 /* Disable everything but the primary plane */
14750 for_each_intel_plane_on_crtc(dev, crtc, plane) {
14751 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
14752 continue;
14753
72259536 14754 trace_intel_disable_plane(&plane->base, crtc);
282dbf9b 14755 plane->disable_plane(plane, crtc);
f9cd7b88 14756 }
9625604c 14757 }
d3eaf884 14758
24929352 14759 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14760 * disable the crtc (and hence change the state) if it is wrong. Note
14761 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 14762 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14763 bool plane;
14764
78108b7c
VS
14765 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
14766 crtc->base.base.id, crtc->base.name);
24929352
DV
14767
14768 /* Pipe has the wrong plane attached and the plane is active.
14769 * Temporarily change the plane mapping and disable everything
14770 * ... */
14771 plane = crtc->plane;
1d4258db 14772 crtc->base.primary->state->visible = true;
24929352 14773 crtc->plane = !plane;
da1d0e26 14774 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 14775 crtc->plane = plane;
24929352 14776 }
24929352
DV
14777
14778 /* Adjust the state of the output pipe according to whether we
14779 * have active connectors/encoders. */
842e0307 14780 if (crtc->active && !intel_crtc_has_encoders(crtc))
da1d0e26 14781 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 14782
49cff963 14783 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
14784 /*
14785 * We start out with underrun reporting disabled to avoid races.
14786 * For correct bookkeeping mark this on active crtcs.
14787 *
c5ab3bc0
DV
14788 * Also on gmch platforms we dont have any hardware bits to
14789 * disable the underrun reporting. Which means we need to start
14790 * out with underrun reporting disabled also on inactive pipes,
14791 * since otherwise we'll complain about the garbage we read when
14792 * e.g. coming up after runtime pm.
14793 *
4cc31489
DV
14794 * No protection against concurrent access is required - at
14795 * worst a fifo underrun happens which also sets this to false.
14796 */
14797 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
14798 /*
14799 * We track the PCH trancoder underrun reporting state
14800 * within the crtc. With crtc for pipe A housing the underrun
14801 * reporting state for PCH transcoder A, crtc for pipe B housing
14802 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
14803 * and marking underrun reporting as disabled for the non-existing
14804 * PCH transcoders B and C would prevent enabling the south
14805 * error interrupt (see cpt_can_enable_serr_int()).
14806 */
ecf837d9 14807 if (has_pch_trancoder(dev_priv, crtc->pipe))
a168f5b3 14808 crtc->pch_fifo_underrun_disabled = true;
4cc31489 14809 }
24929352
DV
14810}
14811
14812static void intel_sanitize_encoder(struct intel_encoder *encoder)
14813{
14814 struct intel_connector *connector;
24929352
DV
14815
14816 /* We need to check both for a crtc link (meaning that the
14817 * encoder is active and trying to read from a pipe) and the
14818 * pipe itself being active. */
14819 bool has_active_crtc = encoder->base.crtc &&
14820 to_intel_crtc(encoder->base.crtc)->active;
14821
496b0fc3
ML
14822 connector = intel_encoder_find_connector(encoder);
14823 if (connector && !has_active_crtc) {
24929352
DV
14824 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14825 encoder->base.base.id,
8e329a03 14826 encoder->base.name);
24929352
DV
14827
14828 /* Connector is active, but has no active pipe. This is
14829 * fallout from our resume register restoring. Disable
14830 * the encoder manually again. */
14831 if (encoder->base.crtc) {
fd6bbda9
ML
14832 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
14833
24929352
DV
14834 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14835 encoder->base.base.id,
8e329a03 14836 encoder->base.name);
fd6bbda9 14837 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 14838 if (encoder->post_disable)
fd6bbda9 14839 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 14840 }
7f1950fb 14841 encoder->base.crtc = NULL;
24929352
DV
14842
14843 /* Inconsistent output/port/pipe state happens presumably due to
14844 * a bug in one of the get_hw_state functions. Or someplace else
14845 * in our code, like the register restore mess on resume. Clamp
14846 * things to off as a safer default. */
fd6bbda9
ML
14847
14848 connector->base.dpms = DRM_MODE_DPMS_OFF;
14849 connector->base.encoder = NULL;
24929352
DV
14850 }
14851 /* Enabled encoders without active connectors will be fixed in
14852 * the crtc fixup. */
14853}
14854
29b74b7f 14855void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 14856{
920a14b2 14857 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 14858
04098753
ID
14859 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14860 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 14861 i915_disable_vga(dev_priv);
04098753
ID
14862 }
14863}
14864
29b74b7f 14865void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 14866{
8dc8a27c
PZ
14867 /* This function can be called both from intel_modeset_setup_hw_state or
14868 * at a very early point in our resume sequence, where the power well
14869 * structures are not yet restored. Since this function is at a very
14870 * paranoid "someone might have enabled VGA while we were not looking"
14871 * level, just check if the power well is enabled instead of trying to
14872 * follow the "don't touch the power well if we don't need it" policy
14873 * the rest of the driver uses. */
6392f847 14874 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14875 return;
14876
29b74b7f 14877 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
14878
14879 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
14880}
14881
f9cd7b88 14882static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 14883{
f9cd7b88 14884 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 14885
f9cd7b88 14886 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
14887}
14888
f9cd7b88
VS
14889/* FIXME read out full plane state for all planes */
14890static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 14891{
e9728bd8
VS
14892 struct intel_plane *primary = to_intel_plane(crtc->base.primary);
14893 bool visible;
d032ffa0 14894
e9728bd8 14895 visible = crtc->active && primary_get_hw_state(primary);
b26d3ea3 14896
e9728bd8
VS
14897 intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
14898 to_intel_plane_state(primary->base.state),
14899 visible);
98ec7739
VS
14900}
14901
30e984df 14902static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 14903{
fac5e23e 14904 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 14905 enum pipe pipe;
24929352
DV
14906 struct intel_crtc *crtc;
14907 struct intel_encoder *encoder;
14908 struct intel_connector *connector;
f9e905ca 14909 struct drm_connector_list_iter conn_iter;
5358901f 14910 int i;
24929352 14911
565602d7
ML
14912 dev_priv->active_crtcs = 0;
14913
d3fcc808 14914 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
14915 struct intel_crtc_state *crtc_state =
14916 to_intel_crtc_state(crtc->base.state);
3b117c8f 14917
ec2dc6a0 14918 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
14919 memset(crtc_state, 0, sizeof(*crtc_state));
14920 crtc_state->base.crtc = &crtc->base;
24929352 14921
565602d7
ML
14922 crtc_state->base.active = crtc_state->base.enable =
14923 dev_priv->display.get_pipe_config(crtc, crtc_state);
14924
14925 crtc->base.enabled = crtc_state->base.enable;
14926 crtc->active = crtc_state->base.active;
14927
aca1ebf4 14928 if (crtc_state->base.active)
565602d7
ML
14929 dev_priv->active_crtcs |= 1 << crtc->pipe;
14930
f9cd7b88 14931 readout_plane_state(crtc);
24929352 14932
78108b7c
VS
14933 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
14934 crtc->base.base.id, crtc->base.name,
a8cd6da0 14935 enableddisabled(crtc_state->base.active));
24929352
DV
14936 }
14937
5358901f
DV
14938 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14939 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14940
2edd6443 14941 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
14942 &pll->state.hw_state);
14943 pll->state.crtc_mask = 0;
d3fcc808 14944 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
14945 struct intel_crtc_state *crtc_state =
14946 to_intel_crtc_state(crtc->base.state);
14947
14948 if (crtc_state->base.active &&
14949 crtc_state->shared_dpll == pll)
2c42e535 14950 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 14951 }
2c42e535 14952 pll->active_mask = pll->state.crtc_mask;
5358901f 14953
1e6f2ddc 14954 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 14955 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
14956 }
14957
b2784e15 14958 for_each_intel_encoder(dev, encoder) {
24929352
DV
14959 pipe = 0;
14960
14961 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
14962 struct intel_crtc_state *crtc_state;
14963
98187836 14964 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 14965 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 14966
045ac3b5 14967 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
14968 crtc_state->output_types |= 1 << encoder->type;
14969 encoder->get_config(encoder, crtc_state);
24929352
DV
14970 } else {
14971 encoder->base.crtc = NULL;
14972 }
14973
6f2bcceb 14974 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
14975 encoder->base.base.id, encoder->base.name,
14976 enableddisabled(encoder->base.crtc),
6f2bcceb 14977 pipe_name(pipe));
24929352
DV
14978 }
14979
f9e905ca
DV
14980 drm_connector_list_iter_begin(dev, &conn_iter);
14981 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
DV
14982 if (connector->get_hw_state(connector)) {
14983 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
14984
14985 encoder = connector->encoder;
14986 connector->base.encoder = &encoder->base;
14987
14988 if (encoder->base.crtc &&
14989 encoder->base.crtc->state->active) {
14990 /*
14991 * This has to be done during hardware readout
14992 * because anything calling .crtc_disable may
14993 * rely on the connector_mask being accurate.
14994 */
14995 encoder->base.crtc->state->connector_mask |=
14996 1 << drm_connector_index(&connector->base);
e87a52b3
ML
14997 encoder->base.crtc->state->encoder_mask |=
14998 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
14999 }
15000
24929352
DV
15001 } else {
15002 connector->base.dpms = DRM_MODE_DPMS_OFF;
15003 connector->base.encoder = NULL;
15004 }
15005 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15006 connector->base.base.id, connector->base.name,
15007 enableddisabled(connector->base.encoder));
24929352 15008 }
f9e905ca 15009 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15010
15011 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15012 struct intel_crtc_state *crtc_state =
15013 to_intel_crtc_state(crtc->base.state);
d305e061 15014 int min_cdclk = 0;
aca1ebf4 15015
7f4c6284 15016 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15017 if (crtc_state->base.active) {
15018 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15019 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15020 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15021
15022 /*
15023 * The initial mode needs to be set in order to keep
15024 * the atomic core happy. It wants a valid mode if the
15025 * crtc's enabled, so we do the above call.
15026 *
7800fb69
DV
15027 * But we don't set all the derived state fully, hence
15028 * set a flag to indicate that a full recalculation is
15029 * needed on the next commit.
7f4c6284 15030 */
a8cd6da0 15031 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15032
a7d1b3f4
VS
15033 intel_crtc_compute_pixel_rate(crtc_state);
15034
9c61de4c 15035 if (dev_priv->display.modeset_calc_cdclk) {
d305e061 15036 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
9c61de4c
VS
15037 if (WARN_ON(min_cdclk < 0))
15038 min_cdclk = 0;
15039 }
aca1ebf4 15040
5caa0fea
DV
15041 drm_calc_timestamping_constants(&crtc->base,
15042 &crtc_state->base.adjusted_mode);
9eca6832 15043 update_scanline_offset(crtc);
7f4c6284 15044 }
e3b247da 15045
d305e061 15046 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
aca1ebf4 15047
a8cd6da0 15048 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15049 }
30e984df
DV
15050}
15051
62b69566
ACO
15052static void
15053get_encoder_power_domains(struct drm_i915_private *dev_priv)
15054{
15055 struct intel_encoder *encoder;
15056
15057 for_each_intel_encoder(&dev_priv->drm, encoder) {
15058 u64 get_domains;
15059 enum intel_display_power_domain domain;
15060
15061 if (!encoder->get_power_domains)
15062 continue;
15063
15064 get_domains = encoder->get_power_domains(encoder);
15065 for_each_power_domain(domain, get_domains)
15066 intel_display_power_get(dev_priv, domain);
15067 }
15068}
15069
043e9bda
ML
15070/* Scan out the current hw modeset state,
15071 * and sanitizes it to the current state
15072 */
15073static void
aecd36b8
VS
15074intel_modeset_setup_hw_state(struct drm_device *dev,
15075 struct drm_modeset_acquire_ctx *ctx)
30e984df 15076{
fac5e23e 15077 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15078 enum pipe pipe;
30e984df
DV
15079 struct intel_crtc *crtc;
15080 struct intel_encoder *encoder;
35c95375 15081 int i;
30e984df 15082
6ac43272
VS
15083 if (IS_HASWELL(dev_priv)) {
15084 /*
15085 * WaRsPkgCStateDisplayPMReq:hsw
15086 * System hang if this isn't done before disabling all planes!
15087 */
15088 I915_WRITE(CHICKEN_PAR1_1,
15089 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15090 }
15091
30e984df 15092 intel_modeset_readout_hw_state(dev);
24929352
DV
15093
15094 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15095 get_encoder_power_domains(dev_priv);
15096
b2784e15 15097 for_each_intel_encoder(dev, encoder) {
24929352
DV
15098 intel_sanitize_encoder(encoder);
15099 }
15100
055e393f 15101 for_each_pipe(dev_priv, pipe) {
98187836 15102 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15103
aecd36b8 15104 intel_sanitize_crtc(crtc, ctx);
6e3c9717
ACO
15105 intel_dump_pipe_config(crtc, crtc->config,
15106 "[setup_hw_state]");
24929352 15107 }
9a935856 15108
d29b2f9d
ACO
15109 intel_modeset_update_connector_atomic_state(dev);
15110
35c95375
DV
15111 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15112 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15113
2dd66ebd 15114 if (!pll->on || pll->active_mask)
35c95375
DV
15115 continue;
15116
15117 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15118
2edd6443 15119 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15120 pll->on = false;
15121 }
15122
04548cba
VS
15123 if (IS_G4X(dev_priv)) {
15124 g4x_wm_get_hw_state(dev);
15125 g4x_wm_sanitize(dev_priv);
15126 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 15127 vlv_wm_get_hw_state(dev);
602ae835 15128 vlv_wm_sanitize(dev_priv);
a029fa4d 15129 } else if (INTEL_GEN(dev_priv) >= 9) {
3078999f 15130 skl_wm_get_hw_state(dev);
602ae835 15131 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 15132 ilk_wm_get_hw_state(dev);
602ae835 15133 }
292b990e
ML
15134
15135 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15136 u64 put_domains;
292b990e 15137
74bff5f9 15138 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15139 if (WARN_ON(put_domains))
15140 modeset_put_power_domains(dev_priv, put_domains);
15141 }
15142 intel_display_set_init_power(dev_priv, false);
010cf73d 15143
8d8c386c
ID
15144 intel_power_domains_verify_state(dev_priv);
15145
010cf73d 15146 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15147}
7d0bc1ea 15148
043e9bda
ML
15149void intel_display_resume(struct drm_device *dev)
15150{
e2c8b870
ML
15151 struct drm_i915_private *dev_priv = to_i915(dev);
15152 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15153 struct drm_modeset_acquire_ctx ctx;
043e9bda 15154 int ret;
f30da187 15155
e2c8b870 15156 dev_priv->modeset_restore_state = NULL;
73974893
ML
15157 if (state)
15158 state->acquire_ctx = &ctx;
043e9bda 15159
e2c8b870 15160 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15161
73974893
ML
15162 while (1) {
15163 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15164 if (ret != -EDEADLK)
15165 break;
043e9bda 15166
e2c8b870 15167 drm_modeset_backoff(&ctx);
e2c8b870 15168 }
043e9bda 15169
73974893 15170 if (!ret)
581e49fe 15171 ret = __intel_display_resume(dev, state, &ctx);
73974893 15172
2503a0fe 15173 intel_enable_ipc(dev_priv);
e2c8b870
ML
15174 drm_modeset_drop_locks(&ctx);
15175 drm_modeset_acquire_fini(&ctx);
043e9bda 15176
0853695c 15177 if (ret)
e2c8b870 15178 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15179 if (state)
15180 drm_atomic_state_put(state);
2c7111db
CW
15181}
15182
15183void intel_modeset_gem_init(struct drm_device *dev)
15184{
dc97997a 15185 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15186
dc97997a 15187 intel_init_gt_powersave(dev_priv);
ae48434c 15188
6ac43272
VS
15189 intel_init_clock_gating(dev_priv);
15190
1ee8da6d 15191 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15192}
15193
15194int intel_connector_register(struct drm_connector *connector)
15195{
15196 struct intel_connector *intel_connector = to_intel_connector(connector);
15197 int ret;
15198
15199 ret = intel_backlight_device_register(intel_connector);
15200 if (ret)
15201 goto err;
15202
15203 return 0;
0962c3c9 15204
1ebaa0b9
CW
15205err:
15206 return ret;
79e53945
JB
15207}
15208
c191eca1 15209void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15210{
e63d87c0 15211 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15212
e63d87c0 15213 intel_backlight_device_unregister(intel_connector);
4932e2c3 15214 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15215}
15216
713946d1
MN
15217static void intel_hpd_poll_fini(struct drm_device *dev)
15218{
15219 struct intel_connector *connector;
15220 struct drm_connector_list_iter conn_iter;
15221
15222 /* First disable polling... */
15223 drm_kms_helper_poll_fini(dev);
15224
15225 /* Then kill the work that may have been queued by hpd. */
15226 drm_connector_list_iter_begin(dev, &conn_iter);
15227 for_each_intel_connector_iter(connector, &conn_iter) {
15228 if (connector->modeset_retry_work.func)
15229 cancel_work_sync(&connector->modeset_retry_work);
15230 }
15231 drm_connector_list_iter_end(&conn_iter);
15232}
15233
79e53945
JB
15234void intel_modeset_cleanup(struct drm_device *dev)
15235{
fac5e23e 15236 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15237
eb955eee
CW
15238 flush_work(&dev_priv->atomic_helper.free_work);
15239 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15240
dc97997a 15241 intel_disable_gt_powersave(dev_priv);
2eb5252e 15242
fd0c0642
DV
15243 /*
15244 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15245 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15246 * experience fancy races otherwise.
15247 */
2aeb7d3a 15248 intel_irq_uninstall(dev_priv);
eb21b92b 15249
fd0c0642
DV
15250 /*
15251 * Due to the hpd irq storm handling the hotplug work can re-arm the
15252 * poll handlers. Hence disable polling after hpd handling is shut down.
15253 */
713946d1 15254 intel_hpd_poll_fini(dev);
fd0c0642 15255
4f256d82
DV
15256 /* poll work can call into fbdev, hence clean that up afterwards */
15257 intel_fbdev_fini(dev_priv);
15258
723bfd70
JB
15259 intel_unregister_dsm_handler();
15260
c937ab3e 15261 intel_fbc_global_disable(dev_priv);
69341a5e 15262
1630fe75
CW
15263 /* flush any delayed tasks or pending work */
15264 flush_scheduled_work();
15265
79e53945 15266 drm_mode_config_cleanup(dev);
4d7bb011 15267
1ee8da6d 15268 intel_cleanup_overlay(dev_priv);
ae48434c 15269
dc97997a 15270 intel_cleanup_gt_powersave(dev_priv);
f5949141 15271
40196446 15272 intel_teardown_gmbus(dev_priv);
79e53945
JB
15273}
15274
df0e9248
CW
15275void intel_connector_attach_encoder(struct intel_connector *connector,
15276 struct intel_encoder *encoder)
15277{
15278 connector->encoder = encoder;
15279 drm_mode_connector_attach_encoder(&connector->base,
15280 &encoder->base);
79e53945 15281}
28d52043
DA
15282
15283/*
15284 * set vga decode state - true == enable VGA decode
15285 */
6315b5d3 15286int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15287{
6315b5d3 15288 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15289 u16 gmch_ctrl;
15290
75fa041d
CW
15291 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15292 DRM_ERROR("failed to read control word\n");
15293 return -EIO;
15294 }
15295
c0cc8a55
CW
15296 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15297 return 0;
15298
28d52043
DA
15299 if (state)
15300 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15301 else
15302 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15303
15304 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15305 DRM_ERROR("failed to write control word\n");
15306 return -EIO;
15307 }
15308
28d52043
DA
15309 return 0;
15310}
c4a1d9e4 15311
98a2f411
CW
15312#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15313
c4a1d9e4 15314struct intel_display_error_state {
ff57f1b0
PZ
15315
15316 u32 power_well_driver;
15317
63b66e5b
CW
15318 int num_transcoders;
15319
c4a1d9e4
CW
15320 struct intel_cursor_error_state {
15321 u32 control;
15322 u32 position;
15323 u32 base;
15324 u32 size;
52331309 15325 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15326
15327 struct intel_pipe_error_state {
ddf9c536 15328 bool power_domain_on;
c4a1d9e4 15329 u32 source;
f301b1e1 15330 u32 stat;
52331309 15331 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15332
15333 struct intel_plane_error_state {
15334 u32 control;
15335 u32 stride;
15336 u32 size;
15337 u32 pos;
15338 u32 addr;
15339 u32 surface;
15340 u32 tile_offset;
52331309 15341 } plane[I915_MAX_PIPES];
63b66e5b
CW
15342
15343 struct intel_transcoder_error_state {
ddf9c536 15344 bool power_domain_on;
63b66e5b
CW
15345 enum transcoder cpu_transcoder;
15346
15347 u32 conf;
15348
15349 u32 htotal;
15350 u32 hblank;
15351 u32 hsync;
15352 u32 vtotal;
15353 u32 vblank;
15354 u32 vsync;
15355 } transcoder[4];
c4a1d9e4
CW
15356};
15357
15358struct intel_display_error_state *
c033666a 15359intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15360{
c4a1d9e4 15361 struct intel_display_error_state *error;
63b66e5b
CW
15362 int transcoders[] = {
15363 TRANSCODER_A,
15364 TRANSCODER_B,
15365 TRANSCODER_C,
15366 TRANSCODER_EDP,
15367 };
c4a1d9e4
CW
15368 int i;
15369
c033666a 15370 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15371 return NULL;
15372
9d1cb914 15373 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15374 if (error == NULL)
15375 return NULL;
15376
c033666a 15377 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9c3a16c8
ID
15378 error->power_well_driver =
15379 I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
ff57f1b0 15380
055e393f 15381 for_each_pipe(dev_priv, i) {
ddf9c536 15382 error->pipe[i].power_domain_on =
f458ebbc
DV
15383 __intel_display_power_is_enabled(dev_priv,
15384 POWER_DOMAIN_PIPE(i));
ddf9c536 15385 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15386 continue;
15387
5efb3e28
VS
15388 error->cursor[i].control = I915_READ(CURCNTR(i));
15389 error->cursor[i].position = I915_READ(CURPOS(i));
15390 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15391
15392 error->plane[i].control = I915_READ(DSPCNTR(i));
15393 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15394 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15395 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15396 error->plane[i].pos = I915_READ(DSPPOS(i));
15397 }
c033666a 15398 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15399 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15400 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15401 error->plane[i].surface = I915_READ(DSPSURF(i));
15402 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15403 }
15404
c4a1d9e4 15405 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15406
c033666a 15407 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15408 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15409 }
15410
4d1de975 15411 /* Note: this does not include DSI transcoders. */
c033666a 15412 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15413 if (HAS_DDI(dev_priv))
63b66e5b
CW
15414 error->num_transcoders++; /* Account for eDP. */
15415
15416 for (i = 0; i < error->num_transcoders; i++) {
15417 enum transcoder cpu_transcoder = transcoders[i];
15418
ddf9c536 15419 error->transcoder[i].power_domain_on =
f458ebbc 15420 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15421 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15422 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15423 continue;
15424
63b66e5b
CW
15425 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15426
15427 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15428 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15429 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15430 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15431 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15432 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15433 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15434 }
15435
15436 return error;
15437}
15438
edc3d884
MK
15439#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15440
c4a1d9e4 15441void
edc3d884 15442intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15443 struct intel_display_error_state *error)
15444{
5a4c6f1b 15445 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15446 int i;
15447
63b66e5b
CW
15448 if (!error)
15449 return;
15450
b7f05d4a 15451 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15452 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15453 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15454 error->power_well_driver);
055e393f 15455 for_each_pipe(dev_priv, i) {
edc3d884 15456 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15457 err_printf(m, " Power: %s\n",
87ad3212 15458 onoff(error->pipe[i].power_domain_on));
edc3d884 15459 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15460 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15461
15462 err_printf(m, "Plane [%d]:\n", i);
15463 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15464 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15465 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15466 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15467 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15468 }
772c2a51 15469 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15470 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15471 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15472 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15473 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15474 }
15475
edc3d884
MK
15476 err_printf(m, "Cursor [%d]:\n", i);
15477 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15478 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15479 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15480 }
63b66e5b
CW
15481
15482 for (i = 0; i < error->num_transcoders; i++) {
da205630 15483 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15484 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15485 err_printf(m, " Power: %s\n",
87ad3212 15486 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15487 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15488 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15489 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15490 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15491 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15492 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15493 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15494 }
c4a1d9e4 15495}
98a2f411
CW
15496
15497#endif