]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Force 2*96 MHz cdclk on glk/cnl when audio power is enabled
[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
b840d907
JB
1214void assert_pipe(struct drm_i915_private *dev_priv,
1215 enum pipe pipe, bool state)
b24e7179 1216{
63d7bbe9 1217 bool cur_state;
702e7a56
PZ
1218 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1219 pipe);
4feed0eb 1220 enum intel_display_power_domain power_domain;
b24e7179 1221
e56134bc
VS
1222 /* we keep both pipes enabled on 830 */
1223 if (IS_I830(dev_priv))
8e636784
DV
1224 state = true;
1225
4feed0eb
ID
1226 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1227 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1228 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1229 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1230
1231 intel_display_power_put(dev_priv, power_domain);
1232 } else {
1233 cur_state = false;
69310161
PZ
1234 }
1235
e2c719b7 1236 I915_STATE_WARN(cur_state != state,
63d7bbe9 1237 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1238 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1239}
1240
d87ce764 1241static void assert_plane(struct intel_plane *plane, bool state)
b24e7179 1242{
d87ce764 1243 bool cur_state = plane->get_hw_state(plane);
b24e7179 1244
e2c719b7 1245 I915_STATE_WARN(cur_state != state,
d87ce764
VS
1246 "%s assertion failure (expected %s, current %s)\n",
1247 plane->base.name, onoff(state), onoff(cur_state));
b24e7179
JB
1248}
1249
d87ce764
VS
1250#define assert_plane_enabled(p) assert_plane(p, true)
1251#define assert_plane_disabled(p) assert_plane(p, false)
931872fc 1252
d87ce764 1253static void assert_planes_disabled(struct intel_crtc *crtc)
b24e7179 1254{
d87ce764
VS
1255 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1256 struct intel_plane *plane;
19332d7a 1257
d87ce764
VS
1258 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1259 assert_plane_disabled(plane);
19332d7a
JB
1260}
1261
08c71e5e
VS
1262static void assert_vblank_disabled(struct drm_crtc *crtc)
1263{
e2c719b7 1264 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1265 drm_crtc_vblank_put(crtc);
1266}
1267
7abd4b35
ACO
1268void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1269 enum pipe pipe)
92f2584a 1270{
92f2584a
JB
1271 u32 val;
1272 bool enabled;
1273
649636ef 1274 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1275 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1276 I915_STATE_WARN(enabled,
9db4a9c7
JB
1277 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1278 pipe_name(pipe));
92f2584a
JB
1279}
1280
4e634389
KP
1281static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1282 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1283{
1284 if ((val & DP_PORT_EN) == 0)
1285 return false;
1286
2d1fe073 1287 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1288 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1289 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1290 return false;
2d1fe073 1291 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1292 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1293 return false;
f0575e92
KP
1294 } else {
1295 if ((val & DP_PIPE_MASK) != (pipe << 30))
1296 return false;
1297 }
1298 return true;
1299}
1300
1519b995
KP
1301static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, u32 val)
1303{
dc0fa718 1304 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1305 return false;
1306
2d1fe073 1307 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1308 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1309 return false;
2d1fe073 1310 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1311 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1312 return false;
1519b995 1313 } else {
dc0fa718 1314 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1315 return false;
1316 }
1317 return true;
1318}
1319
1320static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1321 enum pipe pipe, u32 val)
1322{
1323 if ((val & LVDS_PORT_EN) == 0)
1324 return false;
1325
2d1fe073 1326 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1327 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1328 return false;
1329 } else {
1330 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1331 return false;
1332 }
1333 return true;
1334}
1335
1336static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe, u32 val)
1338{
1339 if ((val & ADPA_DAC_ENABLE) == 0)
1340 return false;
2d1fe073 1341 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1342 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1343 return false;
1344 } else {
1345 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1346 return false;
1347 }
1348 return true;
1349}
1350
291906f1 1351static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1352 enum pipe pipe, i915_reg_t reg,
1353 u32 port_sel)
291906f1 1354{
47a05eca 1355 u32 val = I915_READ(reg);
e2c719b7 1356 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1357 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1358 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1359
2d1fe073 1360 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1361 && (val & DP_PIPEB_SELECT),
de9a35ab 1362 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1363}
1364
1365static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1366 enum pipe pipe, i915_reg_t reg)
291906f1 1367{
47a05eca 1368 u32 val = I915_READ(reg);
e2c719b7 1369 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1370 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1371 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1372
2d1fe073 1373 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1374 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1375 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1376}
1377
1378static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1379 enum pipe pipe)
1380{
291906f1 1381 u32 val;
291906f1 1382
f0575e92
KP
1383 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1384 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1385 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1386
649636ef 1387 val = I915_READ(PCH_ADPA);
e2c719b7 1388 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1389 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1390 pipe_name(pipe));
291906f1 1391
649636ef 1392 val = I915_READ(PCH_LVDS);
e2c719b7 1393 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1394 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1395 pipe_name(pipe));
291906f1 1396
e2debe91
PZ
1397 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1398 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1399 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1400}
1401
cd2d34d9
VS
1402static void _vlv_enable_pll(struct intel_crtc *crtc,
1403 const struct intel_crtc_state *pipe_config)
1404{
1405 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1406 enum pipe pipe = crtc->pipe;
1407
1408 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1409 POSTING_READ(DPLL(pipe));
1410 udelay(150);
1411
2c30b43b
CW
1412 if (intel_wait_for_register(dev_priv,
1413 DPLL(pipe),
1414 DPLL_LOCK_VLV,
1415 DPLL_LOCK_VLV,
1416 1))
cd2d34d9
VS
1417 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1418}
1419
d288f65f 1420static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1421 const struct intel_crtc_state *pipe_config)
87442f73 1422{
cd2d34d9 1423 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1424 enum pipe pipe = crtc->pipe;
87442f73 1425
8bd3f301 1426 assert_pipe_disabled(dev_priv, pipe);
87442f73 1427
87442f73 1428 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1429 assert_panel_unlocked(dev_priv, pipe);
87442f73 1430
cd2d34d9
VS
1431 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1432 _vlv_enable_pll(crtc, pipe_config);
426115cf 1433
8bd3f301
VS
1434 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1435 POSTING_READ(DPLL_MD(pipe));
87442f73
DV
1436}
1437
cd2d34d9
VS
1438
1439static void _chv_enable_pll(struct intel_crtc *crtc,
1440 const struct intel_crtc_state *pipe_config)
9d556c99 1441{
cd2d34d9 1442 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1443 enum pipe pipe = crtc->pipe;
9d556c99 1444 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1445 u32 tmp;
1446
a580516d 1447 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1448
1449 /* Enable back the 10bit clock to display controller */
1450 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1451 tmp |= DPIO_DCLKP_EN;
1452 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1453
54433e91
VS
1454 mutex_unlock(&dev_priv->sb_lock);
1455
9d556c99
CML
1456 /*
1457 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1458 */
1459 udelay(1);
1460
1461 /* Enable PLL */
d288f65f 1462 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1463
1464 /* Check PLL is locked */
6b18826a
CW
1465 if (intel_wait_for_register(dev_priv,
1466 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1467 1))
9d556c99 1468 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1469}
1470
1471static void chv_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 assert_pipe_disabled(dev_priv, pipe);
1478
1479 /* PLL is protected by panel, make sure we can write it */
1480 assert_panel_unlocked(dev_priv, pipe);
1481
1482 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1483 _chv_enable_pll(crtc, pipe_config);
9d556c99 1484
c231775c
VS
1485 if (pipe != PIPE_A) {
1486 /*
1487 * WaPixelRepeatModeFixForC0:chv
1488 *
1489 * DPLLCMD is AWOL. Use chicken bits to propagate
1490 * the value from DPLLBMD to either pipe B or C.
1491 */
dfa311f0 1492 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
c231775c
VS
1493 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1494 I915_WRITE(CBR4_VLV, 0);
1495 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1496
1497 /*
1498 * DPLLB VGA mode also seems to cause problems.
1499 * We should always have it disabled.
1500 */
1501 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1502 } else {
1503 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1504 POSTING_READ(DPLL_MD(pipe));
1505 }
9d556c99
CML
1506}
1507
6315b5d3 1508static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1509{
1510 struct intel_crtc *crtc;
1511 int count = 0;
1512
6315b5d3 1513 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1514 count += crtc->base.state->active &&
2d84d2b3
VS
1515 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1516 }
1c4e0274
VS
1517
1518 return count;
1519}
1520
939994da
VS
1521static void i9xx_enable_pll(struct intel_crtc *crtc,
1522 const struct intel_crtc_state *crtc_state)
63d7bbe9 1523{
6315b5d3 1524 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1525 i915_reg_t reg = DPLL(crtc->pipe);
939994da 1526 u32 dpll = crtc_state->dpll_hw_state.dpll;
bb408dd2 1527 int i;
63d7bbe9 1528
66e3d5c0 1529 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1530
63d7bbe9 1531 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1532 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1533 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1534
1c4e0274 1535 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1536 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1537 /*
1538 * It appears to be important that we don't enable this
1539 * for the current pipe before otherwise configuring the
1540 * PLL. No idea how this should be handled if multiple
1541 * DVO outputs are enabled simultaneosly.
1542 */
1543 dpll |= DPLL_DVO_2X_MODE;
1544 I915_WRITE(DPLL(!crtc->pipe),
1545 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1546 }
66e3d5c0 1547
c2b63374
VS
1548 /*
1549 * Apparently we need to have VGA mode enabled prior to changing
1550 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1551 * dividers, even though the register value does change.
1552 */
1553 I915_WRITE(reg, 0);
1554
8e7a65aa
VS
1555 I915_WRITE(reg, dpll);
1556
66e3d5c0
DV
1557 /* Wait for the clocks to stabilize. */
1558 POSTING_READ(reg);
1559 udelay(150);
1560
6315b5d3 1561 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1562 I915_WRITE(DPLL_MD(crtc->pipe),
939994da 1563 crtc_state->dpll_hw_state.dpll_md);
66e3d5c0
DV
1564 } else {
1565 /* The pixel multiplier can only be updated once the
1566 * DPLL is enabled and the clocks are stable.
1567 *
1568 * So write it again.
1569 */
1570 I915_WRITE(reg, dpll);
1571 }
63d7bbe9
JB
1572
1573 /* We do this three times for luck */
bb408dd2
VS
1574 for (i = 0; i < 3; i++) {
1575 I915_WRITE(reg, dpll);
1576 POSTING_READ(reg);
1577 udelay(150); /* wait for warmup */
1578 }
63d7bbe9
JB
1579}
1580
1c4e0274 1581static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1582{
6315b5d3 1583 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1584 enum pipe pipe = crtc->pipe;
1585
1586 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1587 if (IS_I830(dev_priv) &&
2d84d2b3 1588 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1589 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1590 I915_WRITE(DPLL(PIPE_B),
1591 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1592 I915_WRITE(DPLL(PIPE_A),
1593 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1594 }
1595
b6b5d049 1596 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1597 if (IS_I830(dev_priv))
63d7bbe9
JB
1598 return;
1599
1600 /* Make sure the pipe isn't still relying on us */
1601 assert_pipe_disabled(dev_priv, pipe);
1602
b8afb911 1603 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1604 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1605}
1606
f6071166
JB
1607static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1608{
b8afb911 1609 u32 val;
f6071166
JB
1610
1611 /* Make sure the pipe isn't still relying on us */
1612 assert_pipe_disabled(dev_priv, pipe);
1613
03ed5cbf
VS
1614 val = DPLL_INTEGRATED_REF_CLK_VLV |
1615 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1616 if (pipe != PIPE_A)
1617 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1618
f6071166
JB
1619 I915_WRITE(DPLL(pipe), val);
1620 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1621}
1622
1623static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1624{
d752048d 1625 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1626 u32 val;
1627
a11b0703
VS
1628 /* Make sure the pipe isn't still relying on us */
1629 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1630
60bfe44f
VS
1631 val = DPLL_SSC_REF_CLK_CHV |
1632 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1633 if (pipe != PIPE_A)
1634 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1635
a11b0703
VS
1636 I915_WRITE(DPLL(pipe), val);
1637 POSTING_READ(DPLL(pipe));
d752048d 1638
a580516d 1639 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1640
1641 /* Disable 10bit clock to display controller */
1642 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1643 val &= ~DPIO_DCLKP_EN;
1644 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1645
a580516d 1646 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1647}
1648
e4607fcf 1649void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1650 struct intel_digital_port *dport,
1651 unsigned int expected_mask)
89b667f8
JB
1652{
1653 u32 port_mask;
f0f59a00 1654 i915_reg_t dpll_reg;
89b667f8 1655
e4607fcf
CML
1656 switch (dport->port) {
1657 case PORT_B:
89b667f8 1658 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1659 dpll_reg = DPLL(0);
e4607fcf
CML
1660 break;
1661 case PORT_C:
89b667f8 1662 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1663 dpll_reg = DPLL(0);
9b6de0a1 1664 expected_mask <<= 4;
00fc31b7
CML
1665 break;
1666 case PORT_D:
1667 port_mask = DPLL_PORTD_READY_MASK;
1668 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1669 break;
1670 default:
1671 BUG();
1672 }
89b667f8 1673
370004d3
CW
1674 if (intel_wait_for_register(dev_priv,
1675 dpll_reg, port_mask, expected_mask,
1676 1000))
9b6de0a1
VS
1677 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1678 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1679}
1680
b8a4f404
PZ
1681static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1682 enum pipe pipe)
040484af 1683{
98187836
VS
1684 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1685 pipe);
f0f59a00
VS
1686 i915_reg_t reg;
1687 uint32_t val, pipeconf_val;
040484af 1688
040484af 1689 /* Make sure PCH DPLL is enabled */
8106ddbd 1690 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1691
1692 /* FDI must be feeding us bits for PCH ports */
1693 assert_fdi_tx_enabled(dev_priv, pipe);
1694 assert_fdi_rx_enabled(dev_priv, pipe);
1695
6e266956 1696 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1697 /* Workaround: Set the timing override bit before enabling the
1698 * pch transcoder. */
1699 reg = TRANS_CHICKEN2(pipe);
1700 val = I915_READ(reg);
1701 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1702 I915_WRITE(reg, val);
59c859d6 1703 }
23670b32 1704
ab9412ba 1705 reg = PCH_TRANSCONF(pipe);
040484af 1706 val = I915_READ(reg);
5f7f726d 1707 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1708
2d1fe073 1709 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1710 /*
c5de7c6f
VS
1711 * Make the BPC in transcoder be consistent with
1712 * that in pipeconf reg. For HDMI we must use 8bpc
1713 * here for both 8bpc and 12bpc.
e9bcff5c 1714 */
dfd07d72 1715 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1716 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1717 val |= PIPECONF_8BPC;
1718 else
1719 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1720 }
5f7f726d
PZ
1721
1722 val &= ~TRANS_INTERLACE_MASK;
1723 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1724 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1725 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1726 val |= TRANS_LEGACY_INTERLACED_ILK;
1727 else
1728 val |= TRANS_INTERLACED;
5f7f726d
PZ
1729 else
1730 val |= TRANS_PROGRESSIVE;
1731
040484af 1732 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1733 if (intel_wait_for_register(dev_priv,
1734 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1735 100))
4bb6f1f3 1736 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1737}
1738
8fb033d7 1739static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1740 enum transcoder cpu_transcoder)
040484af 1741{
8fb033d7 1742 u32 val, pipeconf_val;
8fb033d7 1743
8fb033d7 1744 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1745 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
a2196033 1746 assert_fdi_rx_enabled(dev_priv, PIPE_A);
8fb033d7 1747
223a6fdf 1748 /* Workaround: set timing override bit. */
36c0d0cf 1749 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1750 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1751 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1752
25f3ef11 1753 val = TRANS_ENABLE;
937bb610 1754 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1755
9a76b1c6
PZ
1756 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1757 PIPECONF_INTERLACED_ILK)
a35f2679 1758 val |= TRANS_INTERLACED;
8fb033d7
PZ
1759 else
1760 val |= TRANS_PROGRESSIVE;
1761
ab9412ba 1762 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1763 if (intel_wait_for_register(dev_priv,
1764 LPT_TRANSCONF,
1765 TRANS_STATE_ENABLE,
1766 TRANS_STATE_ENABLE,
1767 100))
937bb610 1768 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1769}
1770
b8a4f404
PZ
1771static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1772 enum pipe pipe)
040484af 1773{
f0f59a00
VS
1774 i915_reg_t reg;
1775 uint32_t val;
040484af
JB
1776
1777 /* FDI relies on the transcoder */
1778 assert_fdi_tx_disabled(dev_priv, pipe);
1779 assert_fdi_rx_disabled(dev_priv, pipe);
1780
291906f1
JB
1781 /* Ports must be off as well */
1782 assert_pch_ports_disabled(dev_priv, pipe);
1783
ab9412ba 1784 reg = PCH_TRANSCONF(pipe);
040484af
JB
1785 val = I915_READ(reg);
1786 val &= ~TRANS_ENABLE;
1787 I915_WRITE(reg, val);
1788 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1789 if (intel_wait_for_register(dev_priv,
1790 reg, TRANS_STATE_ENABLE, 0,
1791 50))
4bb6f1f3 1792 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1793
6e266956 1794 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1795 /* Workaround: Clear the timing override chicken bit again. */
1796 reg = TRANS_CHICKEN2(pipe);
1797 val = I915_READ(reg);
1798 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1799 I915_WRITE(reg, val);
1800 }
040484af
JB
1801}
1802
b7076546 1803void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1804{
8fb033d7
PZ
1805 u32 val;
1806
ab9412ba 1807 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1808 val &= ~TRANS_ENABLE;
ab9412ba 1809 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1810 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1811 if (intel_wait_for_register(dev_priv,
1812 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1813 50))
8a52fd9f 1814 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1815
1816 /* Workaround: clear timing override bit. */
36c0d0cf 1817 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1818 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1819 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1820}
1821
a2196033 1822enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
65f2130c
VS
1823{
1824 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1825
1826 WARN_ON(!crtc->config->has_pch_encoder);
1827
1828 if (HAS_PCH_LPT(dev_priv))
a2196033 1829 return PIPE_A;
65f2130c 1830 else
a2196033 1831 return crtc->pipe;
65f2130c
VS
1832}
1833
b24e7179 1834/**
309cfea8 1835 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1836 * @crtc: crtc responsible for the pipe
b24e7179 1837 *
0372264a 1838 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1839 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1840 */
e1fdc473 1841static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1842{
0372264a 1843 struct drm_device *dev = crtc->base.dev;
fac5e23e 1844 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1845 enum pipe pipe = crtc->pipe;
1a70a728 1846 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1847 i915_reg_t reg;
b24e7179
JB
1848 u32 val;
1849
9e2ee2dd
VS
1850 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1851
d87ce764 1852 assert_planes_disabled(crtc);
58c6eaa2 1853
b24e7179
JB
1854 /*
1855 * A pipe without a PLL won't actually be able to drive bits from
1856 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1857 * need the check.
1858 */
09fa8bb9 1859 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1860 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1861 assert_dsi_pll_enabled(dev_priv);
1862 else
1863 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1864 } else {
6e3c9717 1865 if (crtc->config->has_pch_encoder) {
040484af 1866 /* if driving the PCH, we need FDI enabled */
65f2130c 1867 assert_fdi_rx_pll_enabled(dev_priv,
a2196033 1868 intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1869 assert_fdi_tx_pll_enabled(dev_priv,
1870 (enum pipe) cpu_transcoder);
040484af
JB
1871 }
1872 /* FIXME: assert CPU port conditions for SNB+ */
1873 }
b24e7179 1874
702e7a56 1875 reg = PIPECONF(cpu_transcoder);
b24e7179 1876 val = I915_READ(reg);
7ad25d48 1877 if (val & PIPECONF_ENABLE) {
e56134bc
VS
1878 /* we keep both pipes enabled on 830 */
1879 WARN_ON(!IS_I830(dev_priv));
00d70b15 1880 return;
7ad25d48 1881 }
00d70b15
CW
1882
1883 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1884 POSTING_READ(reg);
b7792d8b
VS
1885
1886 /*
a87e55f8
VS
1887 * Until the pipe starts PIPEDSL reads will return a stale value,
1888 * which causes an apparent vblank timestamp jump when PIPEDSL
1889 * resets to its proper value. That also messes up the frame count
1890 * when it's derived from the timestamps. So let's wait for the
1891 * pipe to start properly before we call drm_crtc_vblank_on()
b7792d8b 1892 */
a87e55f8
VS
1893 if (dev->max_vblank_count == 0)
1894 intel_wait_for_pipe_scanline_moving(crtc);
b24e7179
JB
1895}
1896
1897/**
309cfea8 1898 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 1899 * @crtc: crtc whose pipes is to be disabled
b24e7179 1900 *
575f7ab7
VS
1901 * Disable the pipe of @crtc, making sure that various hardware
1902 * specific requirements are met, if applicable, e.g. plane
1903 * disabled, panel fitter off, etc.
b24e7179
JB
1904 *
1905 * Will wait until the pipe has shut down before returning.
1906 */
575f7ab7 1907static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 1908{
fac5e23e 1909 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1910 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1911 enum pipe pipe = crtc->pipe;
f0f59a00 1912 i915_reg_t reg;
b24e7179
JB
1913 u32 val;
1914
9e2ee2dd
VS
1915 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1916
b24e7179
JB
1917 /*
1918 * Make sure planes won't keep trying to pump pixels to us,
1919 * or we might hang the display.
1920 */
d87ce764 1921 assert_planes_disabled(crtc);
b24e7179 1922
702e7a56 1923 reg = PIPECONF(cpu_transcoder);
b24e7179 1924 val = I915_READ(reg);
00d70b15
CW
1925 if ((val & PIPECONF_ENABLE) == 0)
1926 return;
1927
67adc644
VS
1928 /*
1929 * Double wide has implications for planes
1930 * so best keep it disabled when not needed.
1931 */
6e3c9717 1932 if (crtc->config->double_wide)
67adc644
VS
1933 val &= ~PIPECONF_DOUBLE_WIDE;
1934
1935 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1936 if (!IS_I830(dev_priv))
67adc644
VS
1937 val &= ~PIPECONF_ENABLE;
1938
1939 I915_WRITE(reg, val);
1940 if ((val & PIPECONF_ENABLE) == 0)
1941 intel_wait_for_pipe_off(crtc);
b24e7179
JB
1942}
1943
832be82f
VS
1944static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1945{
1946 return IS_GEN2(dev_priv) ? 2048 : 4096;
1947}
1948
d88c4afd
VS
1949static unsigned int
1950intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
7b49f948 1951{
d88c4afd
VS
1952 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1953 unsigned int cpp = fb->format->cpp[plane];
1954
1955 switch (fb->modifier) {
2f075565 1956 case DRM_FORMAT_MOD_LINEAR:
7b49f948
VS
1957 return cpp;
1958 case I915_FORMAT_MOD_X_TILED:
1959 if (IS_GEN2(dev_priv))
1960 return 128;
1961 else
1962 return 512;
2e2adb05
VS
1963 case I915_FORMAT_MOD_Y_TILED_CCS:
1964 if (plane == 1)
1965 return 128;
1966 /* fall through */
7b49f948
VS
1967 case I915_FORMAT_MOD_Y_TILED:
1968 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
1969 return 128;
1970 else
1971 return 512;
2e2adb05
VS
1972 case I915_FORMAT_MOD_Yf_TILED_CCS:
1973 if (plane == 1)
1974 return 128;
1975 /* fall through */
7b49f948
VS
1976 case I915_FORMAT_MOD_Yf_TILED:
1977 switch (cpp) {
1978 case 1:
1979 return 64;
1980 case 2:
1981 case 4:
1982 return 128;
1983 case 8:
1984 case 16:
1985 return 256;
1986 default:
1987 MISSING_CASE(cpp);
1988 return cpp;
1989 }
1990 break;
1991 default:
d88c4afd 1992 MISSING_CASE(fb->modifier);
7b49f948
VS
1993 return cpp;
1994 }
1995}
1996
d88c4afd
VS
1997static unsigned int
1998intel_tile_height(const struct drm_framebuffer *fb, int plane)
a57ce0b2 1999{
2f075565 2000 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
832be82f
VS
2001 return 1;
2002 else
d88c4afd
VS
2003 return intel_tile_size(to_i915(fb->dev)) /
2004 intel_tile_width_bytes(fb, plane);
6761dd31
TU
2005}
2006
8d0deca8 2007/* Return the tile dimensions in pixel units */
d88c4afd 2008static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
8d0deca8 2009 unsigned int *tile_width,
d88c4afd 2010 unsigned int *tile_height)
8d0deca8 2011{
d88c4afd
VS
2012 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
2013 unsigned int cpp = fb->format->cpp[plane];
8d0deca8
VS
2014
2015 *tile_width = tile_width_bytes / cpp;
d88c4afd 2016 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
2017}
2018
6761dd31 2019unsigned int
d88c4afd
VS
2020intel_fb_align_height(const struct drm_framebuffer *fb,
2021 int plane, unsigned int height)
6761dd31 2022{
d88c4afd 2023 unsigned int tile_height = intel_tile_height(fb, plane);
832be82f
VS
2024
2025 return ALIGN(height, tile_height);
a57ce0b2
JB
2026}
2027
1663b9d6
VS
2028unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2029{
2030 unsigned int size = 0;
2031 int i;
2032
2033 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2034 size += rot_info->plane[i].width * rot_info->plane[i].height;
2035
2036 return size;
2037}
2038
75c82a53 2039static void
3465c580
VS
2040intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2041 const struct drm_framebuffer *fb,
2042 unsigned int rotation)
f64b98cd 2043{
7b92c047 2044 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2045 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2046 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2047 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2048 }
2049}
50470bb0 2050
fabac484
VS
2051static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2052{
2053 if (IS_I830(dev_priv))
2054 return 16 * 1024;
2055 else if (IS_I85X(dev_priv))
2056 return 256;
d9e1551e
VS
2057 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2058 return 32;
fabac484
VS
2059 else
2060 return 4 * 1024;
2061}
2062
603525d7 2063static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2064{
2065 if (INTEL_INFO(dev_priv)->gen >= 9)
2066 return 256 * 1024;
c0f86832 2067 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2068 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2069 return 128 * 1024;
2070 else if (INTEL_INFO(dev_priv)->gen >= 4)
2071 return 4 * 1024;
2072 else
44c5905e 2073 return 0;
4e9a86b6
VS
2074}
2075
d88c4afd
VS
2076static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2077 int plane)
603525d7 2078{
d88c4afd
VS
2079 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2080
b90c1ee1 2081 /* AUX_DIST needs only 4K alignment */
2e2adb05 2082 if (plane == 1)
b90c1ee1
VS
2083 return 4096;
2084
d88c4afd 2085 switch (fb->modifier) {
2f075565 2086 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2087 return intel_linear_alignment(dev_priv);
2088 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2089 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2090 return 256 * 1024;
2091 return 0;
2e2adb05
VS
2092 case I915_FORMAT_MOD_Y_TILED_CCS:
2093 case I915_FORMAT_MOD_Yf_TILED_CCS:
603525d7
VS
2094 case I915_FORMAT_MOD_Y_TILED:
2095 case I915_FORMAT_MOD_Yf_TILED:
2096 return 1 * 1024 * 1024;
2097 default:
d88c4afd 2098 MISSING_CASE(fb->modifier);
603525d7
VS
2099 return 0;
2100 }
2101}
2102
058d88c4
CW
2103struct i915_vma *
2104intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2105{
850c4cdc 2106 struct drm_device *dev = fb->dev;
fac5e23e 2107 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2108 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2109 struct i915_ggtt_view view;
058d88c4 2110 struct i915_vma *vma;
6b95a207 2111 u32 alignment;
6b95a207 2112
ebcdd39e
MR
2113 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2114
d88c4afd 2115 alignment = intel_surf_alignment(fb, 0);
6b95a207 2116
3465c580 2117 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2118
693db184
CW
2119 /* Note that the w/a also requires 64 PTE of padding following the
2120 * bo. We currently fill all unused PTE with the shadow page and so
2121 * we should always have valid PTE following the scanout preventing
2122 * the VT-d warning.
2123 */
48f112fe 2124 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2125 alignment = 256 * 1024;
2126
d6dd6843
PZ
2127 /*
2128 * Global gtt pte registers are special registers which actually forward
2129 * writes to a chunk of system memory. Which means that there is no risk
2130 * that the register values disappear as soon as we call
2131 * intel_runtime_pm_put(), so it is correct to wrap only the
2132 * pin/unpin/fence and not more.
2133 */
2134 intel_runtime_pm_get(dev_priv);
2135
9db529aa
DV
2136 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2137
058d88c4 2138 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2139 if (IS_ERR(vma))
2140 goto err;
6b95a207 2141
05a20d09 2142 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2143 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2144 * fence, whereas 965+ only requires a fence if using
2145 * framebuffer compression. For simplicity, we always, when
2146 * possible, install a fence as the cost is not that onerous.
2147 *
2148 * If we fail to fence the tiled scanout, then either the
2149 * modeset will reject the change (which is highly unlikely as
2150 * the affected systems, all but one, do not have unmappable
2151 * space) or we will not be able to enable full powersaving
2152 * techniques (also likely not to apply due to various limits
2153 * FBC and the like impose on the size of the buffer, which
2154 * presumably we violated anyway with this unmappable buffer).
2155 * Anyway, it is presumably better to stumble onwards with
2156 * something and try to run the system in a "less than optimal"
2157 * mode that matches the user configuration.
2158 */
3bd40735 2159 i915_vma_pin_fence(vma);
9807216f 2160 }
6b95a207 2161
be1e3415 2162 i915_vma_get(vma);
49ef5294 2163err:
9db529aa
DV
2164 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2165
d6dd6843 2166 intel_runtime_pm_put(dev_priv);
058d88c4 2167 return vma;
6b95a207
KH
2168}
2169
be1e3415 2170void intel_unpin_fb_vma(struct i915_vma *vma)
1690e1eb 2171{
be1e3415 2172 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2173
49ef5294 2174 i915_vma_unpin_fence(vma);
058d88c4 2175 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2176 i915_vma_put(vma);
1690e1eb
CW
2177}
2178
ef78ec94
VS
2179static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2180 unsigned int rotation)
2181{
bd2ef25d 2182 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2183 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2184 else
2185 return fb->pitches[plane];
2186}
2187
6687c906
VS
2188/*
2189 * Convert the x/y offsets into a linear offset.
2190 * Only valid with 0/180 degree rotation, which is fine since linear
2191 * offset is only used with linear buffers on pre-hsw and tiled buffers
2192 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2193 */
2194u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2195 const struct intel_plane_state *state,
2196 int plane)
6687c906 2197{
2949056c 2198 const struct drm_framebuffer *fb = state->base.fb;
353c8598 2199 unsigned int cpp = fb->format->cpp[plane];
6687c906
VS
2200 unsigned int pitch = fb->pitches[plane];
2201
2202 return y * pitch + x * cpp;
2203}
2204
2205/*
2206 * Add the x/y offsets derived from fb->offsets[] to the user
2207 * specified plane src x/y offsets. The resulting x/y offsets
2208 * specify the start of scanout from the beginning of the gtt mapping.
2209 */
2210void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2211 const struct intel_plane_state *state,
2212 int plane)
6687c906
VS
2213
2214{
2949056c
VS
2215 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2216 unsigned int rotation = state->base.rotation;
6687c906 2217
bd2ef25d 2218 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2219 *x += intel_fb->rotated[plane].x;
2220 *y += intel_fb->rotated[plane].y;
2221 } else {
2222 *x += intel_fb->normal[plane].x;
2223 *y += intel_fb->normal[plane].y;
2224 }
2225}
2226
e8837d98
VS
2227static u32 __intel_adjust_tile_offset(int *x, int *y,
2228 unsigned int tile_width,
2229 unsigned int tile_height,
2230 unsigned int tile_size,
2231 unsigned int pitch_tiles,
2232 u32 old_offset,
2233 u32 new_offset)
29cf9491 2234{
b9b24038 2235 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2236 unsigned int tiles;
2237
2238 WARN_ON(old_offset & (tile_size - 1));
2239 WARN_ON(new_offset & (tile_size - 1));
2240 WARN_ON(new_offset > old_offset);
2241
2242 tiles = (old_offset - new_offset) / tile_size;
2243
2244 *y += tiles / pitch_tiles * tile_height;
2245 *x += tiles % pitch_tiles * tile_width;
2246
b9b24038
VS
2247 /* minimize x in case it got needlessly big */
2248 *y += *x / pitch_pixels * tile_height;
2249 *x %= pitch_pixels;
2250
29cf9491
VS
2251 return new_offset;
2252}
2253
e8837d98
VS
2254static u32 _intel_adjust_tile_offset(int *x, int *y,
2255 const struct drm_framebuffer *fb, int plane,
2256 unsigned int rotation,
2257 u32 old_offset, u32 new_offset)
66a2d927 2258{
e8837d98 2259 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
353c8598 2260 unsigned int cpp = fb->format->cpp[plane];
66a2d927
VS
2261 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2262
2263 WARN_ON(new_offset > old_offset);
2264
2f075565 2265 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
66a2d927
VS
2266 unsigned int tile_size, tile_width, tile_height;
2267 unsigned int pitch_tiles;
2268
2269 tile_size = intel_tile_size(dev_priv);
d88c4afd 2270 intel_tile_dims(fb, plane, &tile_width, &tile_height);
66a2d927 2271
bd2ef25d 2272 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2273 pitch_tiles = pitch / tile_height;
2274 swap(tile_width, tile_height);
2275 } else {
2276 pitch_tiles = pitch / (tile_width * cpp);
2277 }
2278
e8837d98
VS
2279 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2280 tile_size, pitch_tiles,
2281 old_offset, new_offset);
66a2d927
VS
2282 } else {
2283 old_offset += *y * pitch + *x * cpp;
2284
2285 *y = (old_offset - new_offset) / pitch;
2286 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2287 }
2288
2289 return new_offset;
2290}
2291
e8837d98
VS
2292/*
2293 * Adjust the tile offset by moving the difference into
2294 * the x/y offsets.
2295 */
2296static u32 intel_adjust_tile_offset(int *x, int *y,
2297 const struct intel_plane_state *state, int plane,
2298 u32 old_offset, u32 new_offset)
2299{
2300 return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
2301 state->base.rotation,
2302 old_offset, new_offset);
2303}
2304
8d0deca8
VS
2305/*
2306 * Computes the linear offset to the base tile and adjusts
2307 * x, y. bytes per pixel is assumed to be a power-of-two.
2308 *
2309 * In the 90/270 rotated case, x and y are assumed
2310 * to be already rotated to match the rotated GTT view, and
2311 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2312 *
2313 * This function is used when computing the derived information
2314 * under intel_framebuffer, so using any of that information
2315 * here is not allowed. Anything under drm_framebuffer can be
2316 * used. This is why the user has to pass in the pitch since it
2317 * is specified in the rotated orientation.
8d0deca8 2318 */
6687c906
VS
2319static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2320 int *x, int *y,
2321 const struct drm_framebuffer *fb, int plane,
2322 unsigned int pitch,
2323 unsigned int rotation,
2324 u32 alignment)
c2c75131 2325{
bae781b2 2326 uint64_t fb_modifier = fb->modifier;
353c8598 2327 unsigned int cpp = fb->format->cpp[plane];
6687c906 2328 u32 offset, offset_aligned;
29cf9491 2329
29cf9491
VS
2330 if (alignment)
2331 alignment--;
2332
2f075565 2333 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
8d0deca8
VS
2334 unsigned int tile_size, tile_width, tile_height;
2335 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2336
d843310d 2337 tile_size = intel_tile_size(dev_priv);
d88c4afd 2338 intel_tile_dims(fb, plane, &tile_width, &tile_height);
8d0deca8 2339
bd2ef25d 2340 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2341 pitch_tiles = pitch / tile_height;
2342 swap(tile_width, tile_height);
2343 } else {
2344 pitch_tiles = pitch / (tile_width * cpp);
2345 }
d843310d
VS
2346
2347 tile_rows = *y / tile_height;
2348 *y %= tile_height;
c2c75131 2349
8d0deca8
VS
2350 tiles = *x / tile_width;
2351 *x %= tile_width;
bc752862 2352
29cf9491
VS
2353 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2354 offset_aligned = offset & ~alignment;
bc752862 2355
e8837d98
VS
2356 __intel_adjust_tile_offset(x, y, tile_width, tile_height,
2357 tile_size, pitch_tiles,
2358 offset, offset_aligned);
29cf9491 2359 } else {
bc752862 2360 offset = *y * pitch + *x * cpp;
29cf9491
VS
2361 offset_aligned = offset & ~alignment;
2362
4e9a86b6
VS
2363 *y = (offset & alignment) / pitch;
2364 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2365 }
29cf9491
VS
2366
2367 return offset_aligned;
c2c75131
DV
2368}
2369
6687c906 2370u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2371 const struct intel_plane_state *state,
2372 int plane)
6687c906 2373{
1e7b4fd8
VS
2374 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2375 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2949056c
VS
2376 const struct drm_framebuffer *fb = state->base.fb;
2377 unsigned int rotation = state->base.rotation;
ef78ec94 2378 int pitch = intel_fb_pitch(fb, plane, rotation);
1e7b4fd8
VS
2379 u32 alignment;
2380
2381 if (intel_plane->id == PLANE_CURSOR)
2382 alignment = intel_cursor_alignment(dev_priv);
2383 else
2384 alignment = intel_surf_alignment(fb, plane);
6687c906
VS
2385
2386 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2387 rotation, alignment);
2388}
2389
e8837d98
VS
2390/* Convert the fb->offset[] into x/y offsets */
2391static int intel_fb_offset_to_xy(int *x, int *y,
2392 const struct drm_framebuffer *fb, int plane)
6687c906 2393{
e8837d98
VS
2394 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2395
2396 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
2397 fb->offsets[plane] % intel_tile_size(dev_priv))
2398 return -EINVAL;
2399
2400 *x = 0;
2401 *y = 0;
6687c906 2402
e8837d98
VS
2403 _intel_adjust_tile_offset(x, y,
2404 fb, plane, DRM_MODE_ROTATE_0,
2405 fb->offsets[plane], 0);
2406
2407 return 0;
6687c906
VS
2408}
2409
72618ebf
VS
2410static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2411{
2412 switch (fb_modifier) {
2413 case I915_FORMAT_MOD_X_TILED:
2414 return I915_TILING_X;
2415 case I915_FORMAT_MOD_Y_TILED:
2e2adb05 2416 case I915_FORMAT_MOD_Y_TILED_CCS:
72618ebf
VS
2417 return I915_TILING_Y;
2418 default:
2419 return I915_TILING_NONE;
2420 }
2421}
2422
bbfb6ce8
VS
2423static const struct drm_format_info ccs_formats[] = {
2424 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2425 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2426 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2427 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2428};
2429
2430static const struct drm_format_info *
2431lookup_format_info(const struct drm_format_info formats[],
2432 int num_formats, u32 format)
2433{
2434 int i;
2435
2436 for (i = 0; i < num_formats; i++) {
2437 if (formats[i].format == format)
2438 return &formats[i];
2439 }
2440
2441 return NULL;
2442}
2443
2444static const struct drm_format_info *
2445intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2446{
2447 switch (cmd->modifier[0]) {
2448 case I915_FORMAT_MOD_Y_TILED_CCS:
2449 case I915_FORMAT_MOD_Yf_TILED_CCS:
2450 return lookup_format_info(ccs_formats,
2451 ARRAY_SIZE(ccs_formats),
2452 cmd->pixel_format);
2453 default:
2454 return NULL;
2455 }
2456}
2457
6687c906
VS
2458static int
2459intel_fill_fb_info(struct drm_i915_private *dev_priv,
2460 struct drm_framebuffer *fb)
2461{
2462 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2463 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2464 u32 gtt_offset_rotated = 0;
2465 unsigned int max_size = 0;
bcb0b461 2466 int i, num_planes = fb->format->num_planes;
6687c906
VS
2467 unsigned int tile_size = intel_tile_size(dev_priv);
2468
2469 for (i = 0; i < num_planes; i++) {
2470 unsigned int width, height;
2471 unsigned int cpp, size;
2472 u32 offset;
2473 int x, y;
e8837d98 2474 int ret;
6687c906 2475
353c8598 2476 cpp = fb->format->cpp[i];
145fcb11
VS
2477 width = drm_framebuffer_plane_width(fb->width, fb, i);
2478 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906 2479
e8837d98
VS
2480 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2481 if (ret) {
2482 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2483 i, fb->offsets[i]);
2484 return ret;
2485 }
6687c906 2486
2e2adb05
VS
2487 if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2488 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
2489 int hsub = fb->format->hsub;
2490 int vsub = fb->format->vsub;
2491 int tile_width, tile_height;
2492 int main_x, main_y;
2493 int ccs_x, ccs_y;
2494
2495 intel_tile_dims(fb, i, &tile_width, &tile_height);
e8837d98
VS
2496 tile_width *= hsub;
2497 tile_height *= vsub;
2e2adb05 2498
e8837d98
VS
2499 ccs_x = (x * hsub) % tile_width;
2500 ccs_y = (y * vsub) % tile_height;
2501 main_x = intel_fb->normal[0].x % tile_width;
2502 main_y = intel_fb->normal[0].y % tile_height;
2e2adb05
VS
2503
2504 /*
2505 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2506 * x/y offsets must match between CCS and the main surface.
2507 */
2508 if (main_x != ccs_x || main_y != ccs_y) {
2509 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2510 main_x, main_y,
2511 ccs_x, ccs_y,
2512 intel_fb->normal[0].x,
2513 intel_fb->normal[0].y,
2514 x, y);
2515 return -EINVAL;
2516 }
2517 }
2518
60d5f2a4
VS
2519 /*
2520 * The fence (if used) is aligned to the start of the object
2521 * so having the framebuffer wrap around across the edge of the
2522 * fenced region doesn't really work. We have no API to configure
2523 * the fence start offset within the object (nor could we probably
2524 * on gen2/3). So it's just easier if we just require that the
2525 * fb layout agrees with the fence layout. We already check that the
2526 * fb stride matches the fence stride elsewhere.
2527 */
18db229d 2528 if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
60d5f2a4 2529 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2530 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2531 i, fb->offsets[i]);
60d5f2a4
VS
2532 return -EINVAL;
2533 }
2534
6687c906
VS
2535 /*
2536 * First pixel of the framebuffer from
2537 * the start of the normal gtt mapping.
2538 */
2539 intel_fb->normal[i].x = x;
2540 intel_fb->normal[i].y = y;
2541
2542 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
3ca46c0a 2543 fb, i, fb->pitches[i],
c2c446ad 2544 DRM_MODE_ROTATE_0, tile_size);
6687c906
VS
2545 offset /= tile_size;
2546
2f075565 2547 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
6687c906
VS
2548 unsigned int tile_width, tile_height;
2549 unsigned int pitch_tiles;
2550 struct drm_rect r;
2551
d88c4afd 2552 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2553
2554 rot_info->plane[i].offset = offset;
2555 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2556 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2557 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2558
2559 intel_fb->rotated[i].pitch =
2560 rot_info->plane[i].height * tile_height;
2561
2562 /* how many tiles does this plane need */
2563 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2564 /*
2565 * If the plane isn't horizontally tile aligned,
2566 * we need one more tile.
2567 */
2568 if (x != 0)
2569 size++;
2570
2571 /* rotate the x/y offsets to match the GTT view */
2572 r.x1 = x;
2573 r.y1 = y;
2574 r.x2 = x + width;
2575 r.y2 = y + height;
2576 drm_rect_rotate(&r,
2577 rot_info->plane[i].width * tile_width,
2578 rot_info->plane[i].height * tile_height,
c2c446ad 2579 DRM_MODE_ROTATE_270);
6687c906
VS
2580 x = r.x1;
2581 y = r.y1;
2582
2583 /* rotate the tile dimensions to match the GTT view */
2584 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2585 swap(tile_width, tile_height);
2586
2587 /*
2588 * We only keep the x/y offsets, so push all of the
2589 * gtt offset into the x/y offsets.
2590 */
e8837d98
VS
2591 __intel_adjust_tile_offset(&x, &y,
2592 tile_width, tile_height,
2593 tile_size, pitch_tiles,
2594 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2595
2596 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2597
2598 /*
2599 * First pixel of the framebuffer from
2600 * the start of the rotated gtt mapping.
2601 */
2602 intel_fb->rotated[i].x = x;
2603 intel_fb->rotated[i].y = y;
2604 } else {
2605 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2606 x * cpp, tile_size);
2607 }
2608
2609 /* how many tiles in total needed in the bo */
2610 max_size = max(max_size, offset + size);
2611 }
2612
144cc143
VS
2613 if (max_size * tile_size > intel_fb->obj->base.size) {
2614 DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
2615 max_size * tile_size, intel_fb->obj->base.size);
6687c906
VS
2616 return -EINVAL;
2617 }
2618
2619 return 0;
2620}
2621
b35d63fa 2622static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2623{
2624 switch (format) {
2625 case DISPPLANE_8BPP:
2626 return DRM_FORMAT_C8;
2627 case DISPPLANE_BGRX555:
2628 return DRM_FORMAT_XRGB1555;
2629 case DISPPLANE_BGRX565:
2630 return DRM_FORMAT_RGB565;
2631 default:
2632 case DISPPLANE_BGRX888:
2633 return DRM_FORMAT_XRGB8888;
2634 case DISPPLANE_RGBX888:
2635 return DRM_FORMAT_XBGR8888;
2636 case DISPPLANE_BGRX101010:
2637 return DRM_FORMAT_XRGB2101010;
2638 case DISPPLANE_RGBX101010:
2639 return DRM_FORMAT_XBGR2101010;
2640 }
2641}
2642
bc8d7dff
DL
2643static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2644{
2645 switch (format) {
2646 case PLANE_CTL_FORMAT_RGB_565:
2647 return DRM_FORMAT_RGB565;
2648 default:
2649 case PLANE_CTL_FORMAT_XRGB_8888:
2650 if (rgb_order) {
2651 if (alpha)
2652 return DRM_FORMAT_ABGR8888;
2653 else
2654 return DRM_FORMAT_XBGR8888;
2655 } else {
2656 if (alpha)
2657 return DRM_FORMAT_ARGB8888;
2658 else
2659 return DRM_FORMAT_XRGB8888;
2660 }
2661 case PLANE_CTL_FORMAT_XRGB_2101010:
2662 if (rgb_order)
2663 return DRM_FORMAT_XBGR2101010;
2664 else
2665 return DRM_FORMAT_XRGB2101010;
2666 }
2667}
2668
5724dbd1 2669static bool
f6936e29
DV
2670intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2671 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2672{
2673 struct drm_device *dev = crtc->base.dev;
3badb49f 2674 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2675 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2676 struct drm_i915_gem_object *obj = NULL;
2677 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2678 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2679 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2680 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2681 PAGE_SIZE);
2682
2683 size_aligned -= base_aligned;
46f297fb 2684
ff2652ea
CW
2685 if (plane_config->size == 0)
2686 return false;
2687
3badb49f
PZ
2688 /* If the FB is too big, just don't use it since fbdev is not very
2689 * important and we should probably use that space with FBC or other
2690 * features. */
72e96d64 2691 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2692 return false;
2693
12c83d99 2694 mutex_lock(&dev->struct_mutex);
187685cb 2695 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
DV
2696 base_aligned,
2697 base_aligned,
2698 size_aligned);
24dbf51a
CW
2699 mutex_unlock(&dev->struct_mutex);
2700 if (!obj)
484b41dd 2701 return false;
46f297fb 2702
3e510a8e
CW
2703 if (plane_config->tiling == I915_TILING_X)
2704 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2705
438b74a5 2706 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2707 mode_cmd.width = fb->width;
2708 mode_cmd.height = fb->height;
2709 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2710 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2711 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2712
24dbf51a 2713 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2714 DRM_DEBUG_KMS("intel fb init failed\n");
2715 goto out_unref_obj;
2716 }
12c83d99 2717
484b41dd 2718
f6936e29 2719 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2720 return true;
46f297fb
JB
2721
2722out_unref_obj:
f8c417cd 2723 i915_gem_object_put(obj);
484b41dd
JB
2724 return false;
2725}
2726
e9728bd8
VS
2727static void
2728intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2729 struct intel_plane_state *plane_state,
2730 bool visible)
2731{
2732 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2733
2734 plane_state->base.visible = visible;
2735
2736 /* FIXME pre-g4x don't work like this */
2737 if (visible) {
2738 crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
2739 crtc_state->active_planes |= BIT(plane->id);
2740 } else {
2741 crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
2742 crtc_state->active_planes &= ~BIT(plane->id);
2743 }
2744
2745 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2746 crtc_state->base.crtc->name,
2747 crtc_state->active_planes);
2748}
2749
23ac1273
VS
2750static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2751 struct intel_plane *plane)
2752{
2753 struct intel_crtc_state *crtc_state =
2754 to_intel_crtc_state(crtc->base.state);
2755 struct intel_plane_state *plane_state =
2756 to_intel_plane_state(plane->base.state);
2757
2758 intel_set_plane_visible(crtc_state, plane_state, false);
2759
2760 if (plane->id == PLANE_PRIMARY)
2761 intel_pre_disable_primary_noatomic(&crtc->base);
2762
2763 trace_intel_disable_plane(&plane->base, crtc);
2764 plane->disable_plane(plane, crtc);
2765}
2766
5724dbd1 2767static void
f6936e29
DV
2768intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2769 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2770{
2771 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2772 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2773 struct drm_crtc *c;
2ff8fde1 2774 struct drm_i915_gem_object *obj;
88595ac9 2775 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2776 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2777 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2778 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2779 struct intel_plane_state *intel_state =
2780 to_intel_plane_state(plane_state);
88595ac9 2781 struct drm_framebuffer *fb;
484b41dd 2782
2d14030b 2783 if (!plane_config->fb)
484b41dd
JB
2784 return;
2785
f6936e29 2786 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2787 fb = &plane_config->fb->base;
2788 goto valid_fb;
f55548b5 2789 }
484b41dd 2790
2d14030b 2791 kfree(plane_config->fb);
484b41dd
JB
2792
2793 /*
2794 * Failed to alloc the obj, check to see if we should share
2795 * an fb with another CRTC instead
2796 */
70e1e0ec 2797 for_each_crtc(dev, c) {
be1e3415 2798 struct intel_plane_state *state;
484b41dd
JB
2799
2800 if (c == &intel_crtc->base)
2801 continue;
2802
be1e3415 2803 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2804 continue;
2805
be1e3415
CW
2806 state = to_intel_plane_state(c->primary->state);
2807 if (!state->vma)
484b41dd
JB
2808 continue;
2809
be1e3415
CW
2810 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2811 fb = c->primary->fb;
c3ed1103 2812 drm_framebuffer_get(fb);
88595ac9 2813 goto valid_fb;
484b41dd
JB
2814 }
2815 }
88595ac9 2816
200757f5
MR
2817 /*
2818 * We've failed to reconstruct the BIOS FB. Current display state
2819 * indicates that the primary plane is visible, but has a NULL FB,
2820 * which will lead to problems later if we don't fix it up. The
2821 * simplest solution is to just disable the primary plane now and
2822 * pretend the BIOS never had it enabled.
2823 */
23ac1273 2824 intel_plane_disable_noatomic(intel_crtc, intel_plane);
200757f5 2825
88595ac9
DV
2826 return;
2827
2828valid_fb:
be1e3415
CW
2829 mutex_lock(&dev->struct_mutex);
2830 intel_state->vma =
2831 intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
2832 mutex_unlock(&dev->struct_mutex);
2833 if (IS_ERR(intel_state->vma)) {
2834 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2835 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2836
2837 intel_state->vma = NULL;
c3ed1103 2838 drm_framebuffer_put(fb);
be1e3415
CW
2839 return;
2840 }
2841
f44e2659
VS
2842 plane_state->src_x = 0;
2843 plane_state->src_y = 0;
be5651f2
ML
2844 plane_state->src_w = fb->width << 16;
2845 plane_state->src_h = fb->height << 16;
2846
f44e2659
VS
2847 plane_state->crtc_x = 0;
2848 plane_state->crtc_y = 0;
be5651f2
ML
2849 plane_state->crtc_w = fb->width;
2850 plane_state->crtc_h = fb->height;
2851
1638d30c
RC
2852 intel_state->base.src = drm_plane_state_src(plane_state);
2853 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2854
88595ac9 2855 obj = intel_fb_obj(fb);
3e510a8e 2856 if (i915_gem_object_is_tiled(obj))
88595ac9
DV
2857 dev_priv->preserve_bios_swizzle = true;
2858
c3ed1103 2859 drm_framebuffer_get(fb);
be5651f2 2860 primary->fb = primary->state->fb = fb;
36750f28 2861 primary->crtc = primary->state->crtc = &intel_crtc->base;
e9728bd8
VS
2862
2863 intel_set_plane_visible(to_intel_crtc_state(crtc_state),
2864 to_intel_plane_state(plane_state),
2865 true);
2866
faf5bf0a
CW
2867 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2868 &obj->frontbuffer_bits);
46f297fb
JB
2869}
2870
b63a16f6
VS
2871static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2872 unsigned int rotation)
2873{
353c8598 2874 int cpp = fb->format->cpp[plane];
b63a16f6 2875
bae781b2 2876 switch (fb->modifier) {
2f075565 2877 case DRM_FORMAT_MOD_LINEAR:
b63a16f6
VS
2878 case I915_FORMAT_MOD_X_TILED:
2879 switch (cpp) {
2880 case 8:
2881 return 4096;
2882 case 4:
2883 case 2:
2884 case 1:
2885 return 8192;
2886 default:
2887 MISSING_CASE(cpp);
2888 break;
2889 }
2890 break;
2e2adb05
VS
2891 case I915_FORMAT_MOD_Y_TILED_CCS:
2892 case I915_FORMAT_MOD_Yf_TILED_CCS:
2893 /* FIXME AUX plane? */
b63a16f6
VS
2894 case I915_FORMAT_MOD_Y_TILED:
2895 case I915_FORMAT_MOD_Yf_TILED:
2896 switch (cpp) {
2897 case 8:
2898 return 2048;
2899 case 4:
2900 return 4096;
2901 case 2:
2902 case 1:
2903 return 8192;
2904 default:
2905 MISSING_CASE(cpp);
2906 break;
2907 }
2908 break;
2909 default:
bae781b2 2910 MISSING_CASE(fb->modifier);
b63a16f6
VS
2911 }
2912
2913 return 2048;
2914}
2915
2e2adb05
VS
2916static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2917 int main_x, int main_y, u32 main_offset)
2918{
2919 const struct drm_framebuffer *fb = plane_state->base.fb;
2920 int hsub = fb->format->hsub;
2921 int vsub = fb->format->vsub;
2922 int aux_x = plane_state->aux.x;
2923 int aux_y = plane_state->aux.y;
2924 u32 aux_offset = plane_state->aux.offset;
2925 u32 alignment = intel_surf_alignment(fb, 1);
2926
2927 while (aux_offset >= main_offset && aux_y <= main_y) {
2928 int x, y;
2929
2930 if (aux_x == main_x && aux_y == main_y)
2931 break;
2932
2933 if (aux_offset == 0)
2934 break;
2935
2936 x = aux_x / hsub;
2937 y = aux_y / vsub;
2938 aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
2939 aux_offset, aux_offset - alignment);
2940 aux_x = x * hsub + aux_x % hsub;
2941 aux_y = y * vsub + aux_y % vsub;
2942 }
2943
2944 if (aux_x != main_x || aux_y != main_y)
2945 return false;
2946
2947 plane_state->aux.offset = aux_offset;
2948 plane_state->aux.x = aux_x;
2949 plane_state->aux.y = aux_y;
2950
2951 return true;
2952}
2953
b63a16f6
VS
2954static int skl_check_main_surface(struct intel_plane_state *plane_state)
2955{
b63a16f6
VS
2956 const struct drm_framebuffer *fb = plane_state->base.fb;
2957 unsigned int rotation = plane_state->base.rotation;
cc926387
DV
2958 int x = plane_state->base.src.x1 >> 16;
2959 int y = plane_state->base.src.y1 >> 16;
2960 int w = drm_rect_width(&plane_state->base.src) >> 16;
2961 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2962 int max_width = skl_max_plane_width(fb, 0, rotation);
2963 int max_height = 4096;
8d970654 2964 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2965
2966 if (w > max_width || h > max_height) {
2967 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2968 w, h, max_width, max_height);
2969 return -EINVAL;
2970 }
2971
2972 intel_add_fb_offsets(&x, &y, plane_state, 0);
2973 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
d88c4afd 2974 alignment = intel_surf_alignment(fb, 0);
b63a16f6 2975
8d970654
VS
2976 /*
2977 * AUX surface offset is specified as the distance from the
2978 * main surface offset, and it must be non-negative. Make
2979 * sure that is what we will get.
2980 */
2981 if (offset > aux_offset)
2982 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2983 offset, aux_offset & ~(alignment - 1));
2984
b63a16f6
VS
2985 /*
2986 * When using an X-tiled surface, the plane blows up
2987 * if the x offset + width exceed the stride.
2988 *
2989 * TODO: linear and Y-tiled seem fine, Yf untested,
2990 */
bae781b2 2991 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 2992 int cpp = fb->format->cpp[0];
b63a16f6
VS
2993
2994 while ((x + w) * cpp > fb->pitches[0]) {
2995 if (offset == 0) {
2e2adb05 2996 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
b63a16f6
VS
2997 return -EINVAL;
2998 }
2999
3000 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3001 offset, offset - alignment);
3002 }
3003 }
3004
2e2adb05
VS
3005 /*
3006 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3007 * they match with the main surface x/y offsets.
3008 */
3009 if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3010 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3011 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3012 if (offset == 0)
3013 break;
3014
3015 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
3016 offset, offset - alignment);
3017 }
3018
3019 if (x != plane_state->aux.x || y != plane_state->aux.y) {
3020 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3021 return -EINVAL;
3022 }
3023 }
3024
b63a16f6
VS
3025 plane_state->main.offset = offset;
3026 plane_state->main.x = x;
3027 plane_state->main.y = y;
3028
3029 return 0;
3030}
3031
8d970654
VS
3032static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3033{
3034 const struct drm_framebuffer *fb = plane_state->base.fb;
3035 unsigned int rotation = plane_state->base.rotation;
3036 int max_width = skl_max_plane_width(fb, 1, rotation);
3037 int max_height = 4096;
cc926387
DV
3038 int x = plane_state->base.src.x1 >> 17;
3039 int y = plane_state->base.src.y1 >> 17;
3040 int w = drm_rect_width(&plane_state->base.src) >> 17;
3041 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
3042 u32 offset;
3043
3044 intel_add_fb_offsets(&x, &y, plane_state, 1);
3045 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3046
3047 /* FIXME not quite sure how/if these apply to the chroma plane */
3048 if (w > max_width || h > max_height) {
3049 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3050 w, h, max_width, max_height);
3051 return -EINVAL;
3052 }
3053
3054 plane_state->aux.offset = offset;
3055 plane_state->aux.x = x;
3056 plane_state->aux.y = y;
3057
3058 return 0;
3059}
3060
2e2adb05
VS
3061static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3062{
3063 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
3064 struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
3065 const struct drm_framebuffer *fb = plane_state->base.fb;
3066 int src_x = plane_state->base.src.x1 >> 16;
3067 int src_y = plane_state->base.src.y1 >> 16;
3068 int hsub = fb->format->hsub;
3069 int vsub = fb->format->vsub;
3070 int x = src_x / hsub;
3071 int y = src_y / vsub;
3072 u32 offset;
3073
3074 switch (plane->id) {
3075 case PLANE_PRIMARY:
3076 case PLANE_SPRITE0:
3077 break;
3078 default:
3079 DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
3080 return -EINVAL;
3081 }
3082
3083 if (crtc->pipe == PIPE_C) {
3084 DRM_DEBUG_KMS("No RC support on pipe C\n");
3085 return -EINVAL;
3086 }
3087
3088 if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
3089 DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
3090 plane_state->base.rotation);
3091 return -EINVAL;
3092 }
3093
3094 intel_add_fb_offsets(&x, &y, plane_state, 1);
3095 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
3096
3097 plane_state->aux.offset = offset;
3098 plane_state->aux.x = x * hsub + src_x % hsub;
3099 plane_state->aux.y = y * vsub + src_y % vsub;
3100
3101 return 0;
3102}
3103
b63a16f6
VS
3104int skl_check_plane_surface(struct intel_plane_state *plane_state)
3105{
3106 const struct drm_framebuffer *fb = plane_state->base.fb;
3107 unsigned int rotation = plane_state->base.rotation;
3108 int ret;
3109
a5e4c7d0
VS
3110 if (!plane_state->base.visible)
3111 return 0;
3112
b63a16f6 3113 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 3114 if (drm_rotation_90_or_270(rotation))
cc926387 3115 drm_rect_rotate(&plane_state->base.src,
da064b47 3116 fb->width << 16, fb->height << 16,
c2c446ad 3117 DRM_MODE_ROTATE_270);
b63a16f6 3118
8d970654
VS
3119 /*
3120 * Handle the AUX surface first since
3121 * the main surface setup depends on it.
3122 */
438b74a5 3123 if (fb->format->format == DRM_FORMAT_NV12) {
8d970654
VS
3124 ret = skl_check_nv12_aux_surface(plane_state);
3125 if (ret)
3126 return ret;
2e2adb05
VS
3127 } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
3128 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
3129 ret = skl_check_ccs_aux_surface(plane_state);
3130 if (ret)
3131 return ret;
8d970654
VS
3132 } else {
3133 plane_state->aux.offset = ~0xfff;
3134 plane_state->aux.x = 0;
3135 plane_state->aux.y = 0;
3136 }
3137
b63a16f6
VS
3138 ret = skl_check_main_surface(plane_state);
3139 if (ret)
3140 return ret;
3141
3142 return 0;
3143}
3144
7145f60a
VS
3145static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3146 const struct intel_plane_state *plane_state)
81255565 3147{
7145f60a
VS
3148 struct drm_i915_private *dev_priv =
3149 to_i915(plane_state->base.plane->dev);
3150 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3151 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 3152 unsigned int rotation = plane_state->base.rotation;
7145f60a 3153 u32 dspcntr;
c9ba6fad 3154
7145f60a 3155 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 3156
6a4407a6
VS
3157 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3158 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
7145f60a 3159 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 3160
6a4407a6
VS
3161 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3162 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
f45651ba 3163
d509e28b
VS
3164 if (INTEL_GEN(dev_priv) < 4)
3165 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
81255565 3166
438b74a5 3167 switch (fb->format->format) {
57779d06 3168 case DRM_FORMAT_C8:
81255565
JB
3169 dspcntr |= DISPPLANE_8BPP;
3170 break;
57779d06 3171 case DRM_FORMAT_XRGB1555:
57779d06 3172 dspcntr |= DISPPLANE_BGRX555;
81255565 3173 break;
57779d06
VS
3174 case DRM_FORMAT_RGB565:
3175 dspcntr |= DISPPLANE_BGRX565;
3176 break;
3177 case DRM_FORMAT_XRGB8888:
57779d06
VS
3178 dspcntr |= DISPPLANE_BGRX888;
3179 break;
3180 case DRM_FORMAT_XBGR8888:
57779d06
VS
3181 dspcntr |= DISPPLANE_RGBX888;
3182 break;
3183 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3184 dspcntr |= DISPPLANE_BGRX101010;
3185 break;
3186 case DRM_FORMAT_XBGR2101010:
57779d06 3187 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3188 break;
3189 default:
7145f60a
VS
3190 MISSING_CASE(fb->format->format);
3191 return 0;
81255565 3192 }
57779d06 3193
72618ebf 3194 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3195 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3196 dspcntr |= DISPPLANE_TILED;
81255565 3197
c2c446ad 3198 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
3199 dspcntr |= DISPPLANE_ROTATE_180;
3200
c2c446ad 3201 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
3202 dspcntr |= DISPPLANE_MIRROR;
3203
7145f60a
VS
3204 return dspcntr;
3205}
de1aa629 3206
f9407ae1 3207int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
3208{
3209 struct drm_i915_private *dev_priv =
3210 to_i915(plane_state->base.plane->dev);
3211 int src_x = plane_state->base.src.x1 >> 16;
3212 int src_y = plane_state->base.src.y1 >> 16;
3213 u32 offset;
81255565 3214
5b7fcc44 3215 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 3216
5b7fcc44
VS
3217 if (INTEL_GEN(dev_priv) >= 4)
3218 offset = intel_compute_tile_offset(&src_x, &src_y,
3219 plane_state, 0);
3220 else
3221 offset = 0;
3222
3223 /* HSW/BDW do this automagically in hardware */
3224 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3225 unsigned int rotation = plane_state->base.rotation;
3226 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3227 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3228
c2c446ad 3229 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
3230 src_x += src_w - 1;
3231 src_y += src_h - 1;
c2c446ad 3232 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
3233 src_x += src_w - 1;
3234 }
48404c1e
SJ
3235 }
3236
5b7fcc44
VS
3237 plane_state->main.offset = offset;
3238 plane_state->main.x = src_x;
3239 plane_state->main.y = src_y;
3240
3241 return 0;
3242}
3243
282dbf9b 3244static void i9xx_update_primary_plane(struct intel_plane *primary,
7145f60a
VS
3245 const struct intel_crtc_state *crtc_state,
3246 const struct intel_plane_state *plane_state)
3247{
282dbf9b 3248 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
282dbf9b
VS
3249 const struct drm_framebuffer *fb = plane_state->base.fb;
3250 enum plane plane = primary->plane;
7145f60a 3251 u32 linear_offset;
a0864d59 3252 u32 dspcntr = plane_state->ctl;
7145f60a 3253 i915_reg_t reg = DSPCNTR(plane);
5b7fcc44
VS
3254 int x = plane_state->main.x;
3255 int y = plane_state->main.y;
7145f60a 3256 unsigned long irqflags;
e288881b 3257 u32 dspaddr_offset;
7145f60a 3258
2949056c 3259 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3260
5b7fcc44 3261 if (INTEL_GEN(dev_priv) >= 4)
e288881b 3262 dspaddr_offset = plane_state->main.offset;
5b7fcc44 3263 else
e288881b 3264 dspaddr_offset = linear_offset;
2db3366b 3265
dd584fc0
VS
3266 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3267
78587de2
VS
3268 if (INTEL_GEN(dev_priv) < 4) {
3269 /* pipesrc and dspsize control the size that is scaled from,
3270 * which should always be the user's requested size.
3271 */
dd584fc0
VS
3272 I915_WRITE_FW(DSPSIZE(plane),
3273 ((crtc_state->pipe_src_h - 1) << 16) |
3274 (crtc_state->pipe_src_w - 1));
3275 I915_WRITE_FW(DSPPOS(plane), 0);
78587de2 3276 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
dd584fc0
VS
3277 I915_WRITE_FW(PRIMSIZE(plane),
3278 ((crtc_state->pipe_src_h - 1) << 16) |
3279 (crtc_state->pipe_src_w - 1));
3280 I915_WRITE_FW(PRIMPOS(plane), 0);
3281 I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
78587de2
VS
3282 }
3283
dd584fc0 3284 I915_WRITE_FW(reg, dspcntr);
48404c1e 3285
dd584fc0 3286 I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
3ba35e53
VS
3287 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3288 I915_WRITE_FW(DSPSURF(plane),
3289 intel_plane_ggtt_offset(plane_state) +
e288881b 3290 dspaddr_offset);
3ba35e53
VS
3291 I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
3292 } else if (INTEL_GEN(dev_priv) >= 4) {
dd584fc0
VS
3293 I915_WRITE_FW(DSPSURF(plane),
3294 intel_plane_ggtt_offset(plane_state) +
e288881b 3295 dspaddr_offset);
dd584fc0
VS
3296 I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
3297 I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
bfb81049 3298 } else {
dd584fc0
VS
3299 I915_WRITE_FW(DSPADDR(plane),
3300 intel_plane_ggtt_offset(plane_state) +
e288881b 3301 dspaddr_offset);
bfb81049 3302 }
dd584fc0
VS
3303 POSTING_READ_FW(reg);
3304
3305 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3306}
3307
282dbf9b
VS
3308static void i9xx_disable_primary_plane(struct intel_plane *primary,
3309 struct intel_crtc *crtc)
17638cd6 3310{
282dbf9b
VS
3311 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3312 enum plane plane = primary->plane;
dd584fc0
VS
3313 unsigned long irqflags;
3314
3315 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3316
dd584fc0 3317 I915_WRITE_FW(DSPCNTR(plane), 0);
a8d201af 3318 if (INTEL_INFO(dev_priv)->gen >= 4)
dd584fc0 3319 I915_WRITE_FW(DSPSURF(plane), 0);
a8d201af 3320 else
dd584fc0
VS
3321 I915_WRITE_FW(DSPADDR(plane), 0);
3322 POSTING_READ_FW(DSPCNTR(plane));
3323
3324 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3325}
c9ba6fad 3326
d87ce764
VS
3327static bool i9xx_plane_get_hw_state(struct intel_plane *primary)
3328{
3329
3330 struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
3331 enum intel_display_power_domain power_domain;
3332 enum plane plane = primary->plane;
3333 enum pipe pipe = primary->pipe;
3334 bool ret;
3335
3336 /*
3337 * Not 100% correct for planes that can move between pipes,
3338 * but that's only the case for gen2-4 which don't have any
3339 * display power wells.
3340 */
3341 power_domain = POWER_DOMAIN_PIPE(pipe);
3342 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3343 return false;
3344
3345 ret = I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE;
3346
3347 intel_display_power_put(dev_priv, power_domain);
3348
3349 return ret;
3350}
3351
d88c4afd
VS
3352static u32
3353intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
b321803d 3354{
2f075565 3355 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
b321803d 3356 return 64;
d88c4afd
VS
3357 else
3358 return intel_tile_width_bytes(fb, plane);
b321803d
DL
3359}
3360
e435d6e5
ML
3361static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3362{
3363 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3364 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3365
3366 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3367 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3368 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3369}
3370
a1b2278e
CK
3371/*
3372 * This function detaches (aka. unbinds) unused scalers in hardware
3373 */
0583236e 3374static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3375{
a1b2278e
CK
3376 struct intel_crtc_scaler_state *scaler_state;
3377 int i;
3378
a1b2278e
CK
3379 scaler_state = &intel_crtc->config->scaler_state;
3380
3381 /* loop through and disable scalers that aren't in use */
3382 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3383 if (!scaler_state->scalers[i].in_use)
3384 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3385 }
3386}
3387
d2196774
VS
3388u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3389 unsigned int rotation)
3390{
1b500535
VS
3391 u32 stride;
3392
3393 if (plane >= fb->format->num_planes)
3394 return 0;
3395
3396 stride = intel_fb_pitch(fb, plane, rotation);
d2196774
VS
3397
3398 /*
3399 * The stride is either expressed as a multiple of 64 bytes chunks for
3400 * linear buffers or in number of tiles for tiled buffers.
3401 */
d88c4afd
VS
3402 if (drm_rotation_90_or_270(rotation))
3403 stride /= intel_tile_height(fb, plane);
3404 else
3405 stride /= intel_fb_stride_alignment(fb, plane);
d2196774
VS
3406
3407 return stride;
3408}
3409
2e881264 3410static u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3411{
6156a456 3412 switch (pixel_format) {
d161cf7a 3413 case DRM_FORMAT_C8:
c34ce3d1 3414 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3415 case DRM_FORMAT_RGB565:
c34ce3d1 3416 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3417 case DRM_FORMAT_XBGR8888:
c34ce3d1 3418 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3419 case DRM_FORMAT_XRGB8888:
c34ce3d1 3420 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3421 /*
3422 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3423 * to be already pre-multiplied. We need to add a knob (or a different
3424 * DRM_FORMAT) for user-space to configure that.
3425 */
f75fb42a 3426 case DRM_FORMAT_ABGR8888:
c34ce3d1 3427 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3428 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3429 case DRM_FORMAT_ARGB8888:
c34ce3d1 3430 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3431 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3432 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3433 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3434 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3435 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3436 case DRM_FORMAT_YUYV:
c34ce3d1 3437 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3438 case DRM_FORMAT_YVYU:
c34ce3d1 3439 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3440 case DRM_FORMAT_UYVY:
c34ce3d1 3441 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3442 case DRM_FORMAT_VYUY:
c34ce3d1 3443 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3444 default:
4249eeef 3445 MISSING_CASE(pixel_format);
70d21f0e 3446 }
8cfcba41 3447
c34ce3d1 3448 return 0;
6156a456 3449}
70d21f0e 3450
2e881264 3451static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3452{
6156a456 3453 switch (fb_modifier) {
2f075565 3454 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 3455 break;
30af77c4 3456 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3457 return PLANE_CTL_TILED_X;
b321803d 3458 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3459 return PLANE_CTL_TILED_Y;
2e2adb05
VS
3460 case I915_FORMAT_MOD_Y_TILED_CCS:
3461 return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
b321803d 3462 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3463 return PLANE_CTL_TILED_YF;
2e2adb05
VS
3464 case I915_FORMAT_MOD_Yf_TILED_CCS:
3465 return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
70d21f0e 3466 default:
6156a456 3467 MISSING_CASE(fb_modifier);
70d21f0e 3468 }
8cfcba41 3469
c34ce3d1 3470 return 0;
6156a456 3471}
70d21f0e 3472
2e881264 3473static u32 skl_plane_ctl_rotation(unsigned int rotation)
6156a456 3474{
3b7a5119 3475 switch (rotation) {
c2c446ad 3476 case DRM_MODE_ROTATE_0:
6156a456 3477 break;
1e8df167 3478 /*
c2c446ad 3479 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
3480 * while i915 HW rotation is clockwise, thats why this swapping.
3481 */
c2c446ad 3482 case DRM_MODE_ROTATE_90:
1e8df167 3483 return PLANE_CTL_ROTATE_270;
c2c446ad 3484 case DRM_MODE_ROTATE_180:
c34ce3d1 3485 return PLANE_CTL_ROTATE_180;
c2c446ad 3486 case DRM_MODE_ROTATE_270:
1e8df167 3487 return PLANE_CTL_ROTATE_90;
6156a456
CK
3488 default:
3489 MISSING_CASE(rotation);
3490 }
3491
c34ce3d1 3492 return 0;
6156a456
CK
3493}
3494
2e881264
VS
3495u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3496 const struct intel_plane_state *plane_state)
46f788ba
VS
3497{
3498 struct drm_i915_private *dev_priv =
3499 to_i915(plane_state->base.plane->dev);
3500 const struct drm_framebuffer *fb = plane_state->base.fb;
3501 unsigned int rotation = plane_state->base.rotation;
2e881264 3502 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3503 u32 plane_ctl;
3504
3505 plane_ctl = PLANE_CTL_ENABLE;
3506
6602be0e 3507 if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
46f788ba
VS
3508 plane_ctl |=
3509 PLANE_CTL_PIPE_GAMMA_ENABLE |
3510 PLANE_CTL_PIPE_CSC_ENABLE |
3511 PLANE_CTL_PLANE_GAMMA_DISABLE;
3512 }
3513
3514 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3515 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
3516 plane_ctl |= skl_plane_ctl_rotation(rotation);
3517
2e881264
VS
3518 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3519 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3520 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3521 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3522
46f788ba
VS
3523 return plane_ctl;
3524}
3525
73974893
ML
3526static int
3527__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3528 struct drm_atomic_state *state,
3529 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3530{
3531 struct drm_crtc_state *crtc_state;
3532 struct drm_crtc *crtc;
3533 int i, ret;
11c22da6 3534
aecd36b8 3535 intel_modeset_setup_hw_state(dev, ctx);
29b74b7f 3536 i915_redisable_vga(to_i915(dev));
73974893
ML
3537
3538 if (!state)
3539 return 0;
3540
aa5e9b47
ML
3541 /*
3542 * We've duplicated the state, pointers to the old state are invalid.
3543 *
3544 * Don't attempt to use the old state until we commit the duplicated state.
3545 */
3546 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3547 /*
3548 * Force recalculation even if we restore
3549 * current state. With fast modeset this may not result
3550 * in a modeset when the state is compatible.
3551 */
3552 crtc_state->mode_changed = true;
96a02917 3553 }
73974893
ML
3554
3555 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3556 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3557 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3558
581e49fe 3559 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3560
3561 WARN_ON(ret == -EDEADLK);
3562 return ret;
96a02917
VS
3563}
3564
4ac2ba2f
VS
3565static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3566{
ae98104b
VS
3567 return intel_has_gpu_reset(dev_priv) &&
3568 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3569}
3570
c033666a 3571void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3572{
73974893
ML
3573 struct drm_device *dev = &dev_priv->drm;
3574 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3575 struct drm_atomic_state *state;
3576 int ret;
3577
ce87ea15
DV
3578
3579 /* reset doesn't touch the display */
4f044a88 3580 if (!i915_modparams.force_reset_modeset_test &&
ce87ea15
DV
3581 !gpu_reset_clobbers_display(dev_priv))
3582 return;
3583
9db529aa
DV
3584 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3585 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3586 wake_up_all(&dev_priv->gpu_error.wait_queue);
3587
3588 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3589 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3590 i915_gem_set_wedged(dev_priv);
3591 }
97154ec2 3592
73974893
ML
3593 /*
3594 * Need mode_config.mutex so that we don't
3595 * trample ongoing ->detect() and whatnot.
3596 */
3597 mutex_lock(&dev->mode_config.mutex);
3598 drm_modeset_acquire_init(ctx, 0);
3599 while (1) {
3600 ret = drm_modeset_lock_all_ctx(dev, ctx);
3601 if (ret != -EDEADLK)
3602 break;
3603
3604 drm_modeset_backoff(ctx);
3605 }
f98ce92f
VS
3606 /*
3607 * Disabling the crtcs gracefully seems nicer. Also the
3608 * g33 docs say we should at least disable all the planes.
3609 */
73974893
ML
3610 state = drm_atomic_helper_duplicate_state(dev, ctx);
3611 if (IS_ERR(state)) {
3612 ret = PTR_ERR(state);
73974893 3613 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3614 return;
73974893
ML
3615 }
3616
3617 ret = drm_atomic_helper_disable_all(dev, ctx);
3618 if (ret) {
3619 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3620 drm_atomic_state_put(state);
3621 return;
73974893
ML
3622 }
3623
3624 dev_priv->modeset_restore_state = state;
3625 state->acquire_ctx = ctx;
7514747d
VS
3626}
3627
c033666a 3628void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3629{
73974893
ML
3630 struct drm_device *dev = &dev_priv->drm;
3631 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3632 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3633 int ret;
3634
ce87ea15 3635 /* reset doesn't touch the display */
4f044a88 3636 if (!i915_modparams.force_reset_modeset_test &&
ce87ea15
DV
3637 !gpu_reset_clobbers_display(dev_priv))
3638 return;
3639
3640 if (!state)
3641 goto unlock;
3642
73974893
ML
3643 dev_priv->modeset_restore_state = NULL;
3644
7514747d 3645 /* reset doesn't touch the display */
4ac2ba2f 3646 if (!gpu_reset_clobbers_display(dev_priv)) {
ce87ea15
DV
3647 /* for testing only restore the display */
3648 ret = __intel_display_resume(dev, state, ctx);
942d5d0d
CW
3649 if (ret)
3650 DRM_ERROR("Restoring old state failed with %i\n", ret);
73974893
ML
3651 } else {
3652 /*
3653 * The display has been reset as well,
3654 * so need a full re-initialization.
3655 */
3656 intel_runtime_pm_disable_interrupts(dev_priv);
3657 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3658
51f59205 3659 intel_pps_unlock_regs_wa(dev_priv);
73974893 3660 intel_modeset_init_hw(dev);
6ac43272 3661 intel_init_clock_gating(dev_priv);
7514747d 3662
73974893
ML
3663 spin_lock_irq(&dev_priv->irq_lock);
3664 if (dev_priv->display.hpd_irq_setup)
3665 dev_priv->display.hpd_irq_setup(dev_priv);
3666 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3667
581e49fe 3668 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3669 if (ret)
3670 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3671
73974893
ML
3672 intel_hpd_init(dev_priv);
3673 }
7514747d 3674
ce87ea15
DV
3675 drm_atomic_state_put(state);
3676unlock:
73974893
ML
3677 drm_modeset_drop_locks(ctx);
3678 drm_modeset_acquire_fini(ctx);
3679 mutex_unlock(&dev->mode_config.mutex);
9db529aa
DV
3680
3681 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
7514747d
VS
3682}
3683
1a15b77b
VS
3684static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3685 const struct intel_crtc_state *new_crtc_state)
e30e8f75 3686{
1a15b77b 3687 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
6315b5d3 3688 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
e30e8f75 3689
bfd16b2a 3690 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
1a15b77b 3691 crtc->base.mode = new_crtc_state->base.mode;
bfd16b2a 3692
e30e8f75
GP
3693 /*
3694 * Update pipe size and adjust fitter if needed: the reason for this is
3695 * that in compute_mode_changes we check the native mode (not the pfit
3696 * mode) to see if we can flip rather than do a full mode set. In the
3697 * fastboot case, we'll flip, but if we don't update the pipesrc and
3698 * pfit state, we'll end up with a big fb scanned out into the wrong
3699 * sized surface.
e30e8f75
GP
3700 */
3701
e30e8f75 3702 I915_WRITE(PIPESRC(crtc->pipe),
1a15b77b
VS
3703 ((new_crtc_state->pipe_src_w - 1) << 16) |
3704 (new_crtc_state->pipe_src_h - 1));
bfd16b2a
ML
3705
3706 /* on skylake this is done by detaching scalers */
6315b5d3 3707 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3708 skl_detach_scalers(crtc);
3709
1a15b77b 3710 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a 3711 skylake_pfit_enable(crtc);
6e266956 3712 } else if (HAS_PCH_SPLIT(dev_priv)) {
1a15b77b 3713 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a
ML
3714 ironlake_pfit_enable(crtc);
3715 else if (old_crtc_state->pch_pfit.enabled)
3716 ironlake_pfit_disable(crtc, true);
e30e8f75 3717 }
e30e8f75
GP
3718}
3719
4cbe4b2b 3720static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3721{
4cbe4b2b 3722 struct drm_device *dev = crtc->base.dev;
fac5e23e 3723 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3724 int pipe = crtc->pipe;
f0f59a00
VS
3725 i915_reg_t reg;
3726 u32 temp;
5e84e1a4
ZW
3727
3728 /* enable normal train */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
fd6b8f43 3731 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3732 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3733 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3734 } else {
3735 temp &= ~FDI_LINK_TRAIN_NONE;
3736 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3737 }
5e84e1a4
ZW
3738 I915_WRITE(reg, temp);
3739
3740 reg = FDI_RX_CTL(pipe);
3741 temp = I915_READ(reg);
6e266956 3742 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3743 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3744 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3745 } else {
3746 temp &= ~FDI_LINK_TRAIN_NONE;
3747 temp |= FDI_LINK_TRAIN_NONE;
3748 }
3749 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3750
3751 /* wait one idle pattern time */
3752 POSTING_READ(reg);
3753 udelay(1000);
357555c0
JB
3754
3755 /* IVB wants error correction enabled */
fd6b8f43 3756 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3757 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3758 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3759}
3760
8db9d77b 3761/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3762static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3763 const struct intel_crtc_state *crtc_state)
8db9d77b 3764{
4cbe4b2b 3765 struct drm_device *dev = crtc->base.dev;
fac5e23e 3766 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3767 int pipe = crtc->pipe;
f0f59a00
VS
3768 i915_reg_t reg;
3769 u32 temp, tries;
8db9d77b 3770
1c8562f6 3771 /* FDI needs bits from pipe first */
0fc932b8 3772 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3773
e1a44743
AJ
3774 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3775 for train result */
5eddb70b
CW
3776 reg = FDI_RX_IMR(pipe);
3777 temp = I915_READ(reg);
e1a44743
AJ
3778 temp &= ~FDI_RX_SYMBOL_LOCK;
3779 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3780 I915_WRITE(reg, temp);
3781 I915_READ(reg);
e1a44743
AJ
3782 udelay(150);
3783
8db9d77b 3784 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
627eb5a3 3787 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3788 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3789 temp &= ~FDI_LINK_TRAIN_NONE;
3790 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3791 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3792
5eddb70b
CW
3793 reg = FDI_RX_CTL(pipe);
3794 temp = I915_READ(reg);
8db9d77b
ZW
3795 temp &= ~FDI_LINK_TRAIN_NONE;
3796 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3797 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3798
3799 POSTING_READ(reg);
8db9d77b
ZW
3800 udelay(150);
3801
5b2adf89 3802 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3803 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3804 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3805 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3806
5eddb70b 3807 reg = FDI_RX_IIR(pipe);
e1a44743 3808 for (tries = 0; tries < 5; tries++) {
5eddb70b 3809 temp = I915_READ(reg);
8db9d77b
ZW
3810 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3811
3812 if ((temp & FDI_RX_BIT_LOCK)) {
3813 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3814 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3815 break;
3816 }
8db9d77b 3817 }
e1a44743 3818 if (tries == 5)
5eddb70b 3819 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3820
3821 /* Train 2 */
5eddb70b
CW
3822 reg = FDI_TX_CTL(pipe);
3823 temp = I915_READ(reg);
8db9d77b
ZW
3824 temp &= ~FDI_LINK_TRAIN_NONE;
3825 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3826 I915_WRITE(reg, temp);
8db9d77b 3827
5eddb70b
CW
3828 reg = FDI_RX_CTL(pipe);
3829 temp = I915_READ(reg);
8db9d77b
ZW
3830 temp &= ~FDI_LINK_TRAIN_NONE;
3831 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3832 I915_WRITE(reg, temp);
8db9d77b 3833
5eddb70b
CW
3834 POSTING_READ(reg);
3835 udelay(150);
8db9d77b 3836
5eddb70b 3837 reg = FDI_RX_IIR(pipe);
e1a44743 3838 for (tries = 0; tries < 5; tries++) {
5eddb70b 3839 temp = I915_READ(reg);
8db9d77b
ZW
3840 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3841
3842 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3843 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3844 DRM_DEBUG_KMS("FDI train 2 done.\n");
3845 break;
3846 }
8db9d77b 3847 }
e1a44743 3848 if (tries == 5)
5eddb70b 3849 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3850
3851 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3852
8db9d77b
ZW
3853}
3854
0206e353 3855static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3856 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3857 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3858 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3859 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3860};
3861
3862/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
3863static void gen6_fdi_link_train(struct intel_crtc *crtc,
3864 const struct intel_crtc_state *crtc_state)
8db9d77b 3865{
4cbe4b2b 3866 struct drm_device *dev = crtc->base.dev;
fac5e23e 3867 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3868 int pipe = crtc->pipe;
f0f59a00
VS
3869 i915_reg_t reg;
3870 u32 temp, i, retry;
8db9d77b 3871
e1a44743
AJ
3872 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3873 for train result */
5eddb70b
CW
3874 reg = FDI_RX_IMR(pipe);
3875 temp = I915_READ(reg);
e1a44743
AJ
3876 temp &= ~FDI_RX_SYMBOL_LOCK;
3877 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3878 I915_WRITE(reg, temp);
3879
3880 POSTING_READ(reg);
e1a44743
AJ
3881 udelay(150);
3882
8db9d77b 3883 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3884 reg = FDI_TX_CTL(pipe);
3885 temp = I915_READ(reg);
627eb5a3 3886 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 3887 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
3888 temp &= ~FDI_LINK_TRAIN_NONE;
3889 temp |= FDI_LINK_TRAIN_PATTERN_1;
3890 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3891 /* SNB-B */
3892 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3893 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3894
d74cf324
DV
3895 I915_WRITE(FDI_RX_MISC(pipe),
3896 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3897
5eddb70b
CW
3898 reg = FDI_RX_CTL(pipe);
3899 temp = I915_READ(reg);
6e266956 3900 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3901 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3902 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3903 } else {
3904 temp &= ~FDI_LINK_TRAIN_NONE;
3905 temp |= FDI_LINK_TRAIN_PATTERN_1;
3906 }
5eddb70b
CW
3907 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3908
3909 POSTING_READ(reg);
8db9d77b
ZW
3910 udelay(150);
3911
0206e353 3912 for (i = 0; i < 4; i++) {
5eddb70b
CW
3913 reg = FDI_TX_CTL(pipe);
3914 temp = I915_READ(reg);
8db9d77b
ZW
3915 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3916 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3917 I915_WRITE(reg, temp);
3918
3919 POSTING_READ(reg);
8db9d77b
ZW
3920 udelay(500);
3921
fa37d39e
SP
3922 for (retry = 0; retry < 5; retry++) {
3923 reg = FDI_RX_IIR(pipe);
3924 temp = I915_READ(reg);
3925 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3926 if (temp & FDI_RX_BIT_LOCK) {
3927 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3928 DRM_DEBUG_KMS("FDI train 1 done.\n");
3929 break;
3930 }
3931 udelay(50);
8db9d77b 3932 }
fa37d39e
SP
3933 if (retry < 5)
3934 break;
8db9d77b
ZW
3935 }
3936 if (i == 4)
5eddb70b 3937 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3938
3939 /* Train 2 */
5eddb70b
CW
3940 reg = FDI_TX_CTL(pipe);
3941 temp = I915_READ(reg);
8db9d77b
ZW
3942 temp &= ~FDI_LINK_TRAIN_NONE;
3943 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3944 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3945 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3946 /* SNB-B */
3947 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3948 }
5eddb70b 3949 I915_WRITE(reg, temp);
8db9d77b 3950
5eddb70b
CW
3951 reg = FDI_RX_CTL(pipe);
3952 temp = I915_READ(reg);
6e266956 3953 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3954 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3955 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3956 } else {
3957 temp &= ~FDI_LINK_TRAIN_NONE;
3958 temp |= FDI_LINK_TRAIN_PATTERN_2;
3959 }
5eddb70b
CW
3960 I915_WRITE(reg, temp);
3961
3962 POSTING_READ(reg);
8db9d77b
ZW
3963 udelay(150);
3964
0206e353 3965 for (i = 0; i < 4; i++) {
5eddb70b
CW
3966 reg = FDI_TX_CTL(pipe);
3967 temp = I915_READ(reg);
8db9d77b
ZW
3968 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3969 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3970 I915_WRITE(reg, temp);
3971
3972 POSTING_READ(reg);
8db9d77b
ZW
3973 udelay(500);
3974
fa37d39e
SP
3975 for (retry = 0; retry < 5; retry++) {
3976 reg = FDI_RX_IIR(pipe);
3977 temp = I915_READ(reg);
3978 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3979 if (temp & FDI_RX_SYMBOL_LOCK) {
3980 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3981 DRM_DEBUG_KMS("FDI train 2 done.\n");
3982 break;
3983 }
3984 udelay(50);
8db9d77b 3985 }
fa37d39e
SP
3986 if (retry < 5)
3987 break;
8db9d77b
ZW
3988 }
3989 if (i == 4)
5eddb70b 3990 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3991
3992 DRM_DEBUG_KMS("FDI train done.\n");
3993}
3994
357555c0 3995/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
3996static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
3997 const struct intel_crtc_state *crtc_state)
357555c0 3998{
4cbe4b2b 3999 struct drm_device *dev = crtc->base.dev;
fac5e23e 4000 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4001 int pipe = crtc->pipe;
f0f59a00
VS
4002 i915_reg_t reg;
4003 u32 temp, i, j;
357555c0
JB
4004
4005 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4006 for train result */
4007 reg = FDI_RX_IMR(pipe);
4008 temp = I915_READ(reg);
4009 temp &= ~FDI_RX_SYMBOL_LOCK;
4010 temp &= ~FDI_RX_BIT_LOCK;
4011 I915_WRITE(reg, temp);
4012
4013 POSTING_READ(reg);
4014 udelay(150);
4015
01a415fd
DV
4016 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4017 I915_READ(FDI_RX_IIR(pipe)));
4018
139ccd3f
JB
4019 /* Try each vswing and preemphasis setting twice before moving on */
4020 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4021 /* disable first in case we need to retry */
4022 reg = FDI_TX_CTL(pipe);
4023 temp = I915_READ(reg);
4024 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4025 temp &= ~FDI_TX_ENABLE;
4026 I915_WRITE(reg, temp);
357555c0 4027
139ccd3f
JB
4028 reg = FDI_RX_CTL(pipe);
4029 temp = I915_READ(reg);
4030 temp &= ~FDI_LINK_TRAIN_AUTO;
4031 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4032 temp &= ~FDI_RX_ENABLE;
4033 I915_WRITE(reg, temp);
357555c0 4034
139ccd3f 4035 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4036 reg = FDI_TX_CTL(pipe);
4037 temp = I915_READ(reg);
139ccd3f 4038 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4039 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 4040 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4041 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4042 temp |= snb_b_fdi_train_param[j/2];
4043 temp |= FDI_COMPOSITE_SYNC;
4044 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4045
139ccd3f
JB
4046 I915_WRITE(FDI_RX_MISC(pipe),
4047 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4048
139ccd3f 4049 reg = FDI_RX_CTL(pipe);
357555c0 4050 temp = I915_READ(reg);
139ccd3f
JB
4051 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4052 temp |= FDI_COMPOSITE_SYNC;
4053 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4054
139ccd3f
JB
4055 POSTING_READ(reg);
4056 udelay(1); /* should be 0.5us */
357555c0 4057
139ccd3f
JB
4058 for (i = 0; i < 4; i++) {
4059 reg = FDI_RX_IIR(pipe);
4060 temp = I915_READ(reg);
4061 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4062
139ccd3f
JB
4063 if (temp & FDI_RX_BIT_LOCK ||
4064 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4065 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4066 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4067 i);
4068 break;
4069 }
4070 udelay(1); /* should be 0.5us */
4071 }
4072 if (i == 4) {
4073 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4074 continue;
4075 }
357555c0 4076
139ccd3f 4077 /* Train 2 */
357555c0
JB
4078 reg = FDI_TX_CTL(pipe);
4079 temp = I915_READ(reg);
139ccd3f
JB
4080 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4081 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4082 I915_WRITE(reg, temp);
4083
4084 reg = FDI_RX_CTL(pipe);
4085 temp = I915_READ(reg);
4086 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4087 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4088 I915_WRITE(reg, temp);
4089
4090 POSTING_READ(reg);
139ccd3f 4091 udelay(2); /* should be 1.5us */
357555c0 4092
139ccd3f
JB
4093 for (i = 0; i < 4; i++) {
4094 reg = FDI_RX_IIR(pipe);
4095 temp = I915_READ(reg);
4096 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4097
139ccd3f
JB
4098 if (temp & FDI_RX_SYMBOL_LOCK ||
4099 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4100 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4101 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4102 i);
4103 goto train_done;
4104 }
4105 udelay(2); /* should be 1.5us */
357555c0 4106 }
139ccd3f
JB
4107 if (i == 4)
4108 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4109 }
357555c0 4110
139ccd3f 4111train_done:
357555c0
JB
4112 DRM_DEBUG_KMS("FDI train done.\n");
4113}
4114
88cefb6c 4115static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4116{
88cefb6c 4117 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4118 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4119 int pipe = intel_crtc->pipe;
f0f59a00
VS
4120 i915_reg_t reg;
4121 u32 temp;
c64e311e 4122
c98e9dcf 4123 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4124 reg = FDI_RX_CTL(pipe);
4125 temp = I915_READ(reg);
627eb5a3 4126 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4127 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4128 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4129 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4130
4131 POSTING_READ(reg);
c98e9dcf
JB
4132 udelay(200);
4133
4134 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4135 temp = I915_READ(reg);
4136 I915_WRITE(reg, temp | FDI_PCDCLK);
4137
4138 POSTING_READ(reg);
c98e9dcf
JB
4139 udelay(200);
4140
20749730
PZ
4141 /* Enable CPU FDI TX PLL, always on for Ironlake */
4142 reg = FDI_TX_CTL(pipe);
4143 temp = I915_READ(reg);
4144 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4145 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4146
20749730
PZ
4147 POSTING_READ(reg);
4148 udelay(100);
6be4a607 4149 }
0e23b99d
JB
4150}
4151
88cefb6c
DV
4152static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4153{
4154 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4155 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4156 int pipe = intel_crtc->pipe;
f0f59a00
VS
4157 i915_reg_t reg;
4158 u32 temp;
88cefb6c
DV
4159
4160 /* Switch from PCDclk to Rawclk */
4161 reg = FDI_RX_CTL(pipe);
4162 temp = I915_READ(reg);
4163 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4164
4165 /* Disable CPU FDI TX PLL */
4166 reg = FDI_TX_CTL(pipe);
4167 temp = I915_READ(reg);
4168 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4169
4170 POSTING_READ(reg);
4171 udelay(100);
4172
4173 reg = FDI_RX_CTL(pipe);
4174 temp = I915_READ(reg);
4175 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4176
4177 /* Wait for the clocks to turn off. */
4178 POSTING_READ(reg);
4179 udelay(100);
4180}
4181
0fc932b8
JB
4182static void ironlake_fdi_disable(struct drm_crtc *crtc)
4183{
4184 struct drm_device *dev = crtc->dev;
fac5e23e 4185 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4187 int pipe = intel_crtc->pipe;
f0f59a00
VS
4188 i915_reg_t reg;
4189 u32 temp;
0fc932b8
JB
4190
4191 /* disable CPU FDI tx and PCH FDI rx */
4192 reg = FDI_TX_CTL(pipe);
4193 temp = I915_READ(reg);
4194 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4195 POSTING_READ(reg);
4196
4197 reg = FDI_RX_CTL(pipe);
4198 temp = I915_READ(reg);
4199 temp &= ~(0x7 << 16);
dfd07d72 4200 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4201 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4202
4203 POSTING_READ(reg);
4204 udelay(100);
4205
4206 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4207 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4208 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4209
4210 /* still set train pattern 1 */
4211 reg = FDI_TX_CTL(pipe);
4212 temp = I915_READ(reg);
4213 temp &= ~FDI_LINK_TRAIN_NONE;
4214 temp |= FDI_LINK_TRAIN_PATTERN_1;
4215 I915_WRITE(reg, temp);
4216
4217 reg = FDI_RX_CTL(pipe);
4218 temp = I915_READ(reg);
6e266956 4219 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4220 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4221 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4222 } else {
4223 temp &= ~FDI_LINK_TRAIN_NONE;
4224 temp |= FDI_LINK_TRAIN_PATTERN_1;
4225 }
4226 /* BPC in FDI rx is consistent with that in PIPECONF */
4227 temp &= ~(0x07 << 16);
dfd07d72 4228 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4229 I915_WRITE(reg, temp);
4230
4231 POSTING_READ(reg);
4232 udelay(100);
4233}
4234
49d73912 4235bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93 4236{
fa05887a
DV
4237 struct drm_crtc *crtc;
4238 bool cleanup_done;
4239
4240 drm_for_each_crtc(crtc, &dev_priv->drm) {
4241 struct drm_crtc_commit *commit;
4242 spin_lock(&crtc->commit_lock);
4243 commit = list_first_entry_or_null(&crtc->commit_list,
4244 struct drm_crtc_commit, commit_entry);
4245 cleanup_done = commit ?
4246 try_wait_for_completion(&commit->cleanup_done) : true;
4247 spin_unlock(&crtc->commit_lock);
4248
4249 if (cleanup_done)
5dce5b93
CW
4250 continue;
4251
fa05887a 4252 drm_crtc_wait_one_vblank(crtc);
5dce5b93
CW
4253
4254 return true;
4255 }
4256
4257 return false;
4258}
4259
b7076546 4260void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4261{
4262 u32 temp;
4263
4264 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4265
4266 mutex_lock(&dev_priv->sb_lock);
4267
4268 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4269 temp |= SBI_SSCCTL_DISABLE;
4270 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4271
4272 mutex_unlock(&dev_priv->sb_lock);
4273}
4274
e615efe4 4275/* Program iCLKIP clock to the desired frequency */
0dcdc382 4276static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4277{
0dcdc382
ACO
4278 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4279 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4280 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4281 u32 temp;
4282
060f02d8 4283 lpt_disable_iclkip(dev_priv);
e615efe4 4284
64b46a06
VS
4285 /* The iCLK virtual clock root frequency is in MHz,
4286 * but the adjusted_mode->crtc_clock in in KHz. To get the
4287 * divisors, it is necessary to divide one by another, so we
4288 * convert the virtual clock precision to KHz here for higher
4289 * precision.
4290 */
4291 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4292 u32 iclk_virtual_root_freq = 172800 * 1000;
4293 u32 iclk_pi_range = 64;
64b46a06 4294 u32 desired_divisor;
e615efe4 4295
64b46a06
VS
4296 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4297 clock << auxdiv);
4298 divsel = (desired_divisor / iclk_pi_range) - 2;
4299 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4300
64b46a06
VS
4301 /*
4302 * Near 20MHz is a corner case which is
4303 * out of range for the 7-bit divisor
4304 */
4305 if (divsel <= 0x7f)
4306 break;
e615efe4
ED
4307 }
4308
4309 /* This should not happen with any sane values */
4310 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4311 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4312 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4313 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4314
4315 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4316 clock,
e615efe4
ED
4317 auxdiv,
4318 divsel,
4319 phasedir,
4320 phaseinc);
4321
060f02d8
VS
4322 mutex_lock(&dev_priv->sb_lock);
4323
e615efe4 4324 /* Program SSCDIVINTPHASE6 */
988d6ee8 4325 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4326 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4327 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4328 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4329 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4330 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4331 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4332 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4333
4334 /* Program SSCAUXDIV */
988d6ee8 4335 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4336 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4337 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4338 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4339
4340 /* Enable modulator and associated divider */
988d6ee8 4341 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4342 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4343 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4344
060f02d8
VS
4345 mutex_unlock(&dev_priv->sb_lock);
4346
e615efe4
ED
4347 /* Wait for initialization time */
4348 udelay(24);
4349
4350 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4351}
4352
8802e5b6
VS
4353int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4354{
4355 u32 divsel, phaseinc, auxdiv;
4356 u32 iclk_virtual_root_freq = 172800 * 1000;
4357 u32 iclk_pi_range = 64;
4358 u32 desired_divisor;
4359 u32 temp;
4360
4361 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4362 return 0;
4363
4364 mutex_lock(&dev_priv->sb_lock);
4365
4366 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4367 if (temp & SBI_SSCCTL_DISABLE) {
4368 mutex_unlock(&dev_priv->sb_lock);
4369 return 0;
4370 }
4371
4372 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4373 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4374 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4375 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4376 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4377
4378 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4379 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4380 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4381
4382 mutex_unlock(&dev_priv->sb_lock);
4383
4384 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4385
4386 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4387 desired_divisor << auxdiv);
4388}
4389
275f01b2
DV
4390static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4391 enum pipe pch_transcoder)
4392{
4393 struct drm_device *dev = crtc->base.dev;
fac5e23e 4394 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4395 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4396
4397 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4398 I915_READ(HTOTAL(cpu_transcoder)));
4399 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4400 I915_READ(HBLANK(cpu_transcoder)));
4401 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4402 I915_READ(HSYNC(cpu_transcoder)));
4403
4404 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4405 I915_READ(VTOTAL(cpu_transcoder)));
4406 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4407 I915_READ(VBLANK(cpu_transcoder)));
4408 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4409 I915_READ(VSYNC(cpu_transcoder)));
4410 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4411 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4412}
4413
003632d9 4414static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4415{
fac5e23e 4416 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
DV
4417 uint32_t temp;
4418
4419 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4420 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4421 return;
4422
4423 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4424 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4425
003632d9
ACO
4426 temp &= ~FDI_BC_BIFURCATION_SELECT;
4427 if (enable)
4428 temp |= FDI_BC_BIFURCATION_SELECT;
4429
4430 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4431 I915_WRITE(SOUTH_CHICKEN1, temp);
4432 POSTING_READ(SOUTH_CHICKEN1);
4433}
4434
4435static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4436{
4437 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4438
4439 switch (intel_crtc->pipe) {
4440 case PIPE_A:
4441 break;
4442 case PIPE_B:
6e3c9717 4443 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4444 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4445 else
003632d9 4446 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4447
4448 break;
4449 case PIPE_C:
003632d9 4450 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4451
4452 break;
4453 default:
4454 BUG();
4455 }
4456}
4457
c48b5305
VS
4458/* Return which DP Port should be selected for Transcoder DP control */
4459static enum port
4cbe4b2b 4460intel_trans_dp_port_sel(struct intel_crtc *crtc)
c48b5305 4461{
4cbe4b2b 4462 struct drm_device *dev = crtc->base.dev;
c48b5305
VS
4463 struct intel_encoder *encoder;
4464
4cbe4b2b 4465 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
cca0502b 4466 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4467 encoder->type == INTEL_OUTPUT_EDP)
4468 return enc_to_dig_port(&encoder->base)->port;
4469 }
4470
4471 return -1;
4472}
4473
f67a559d
JB
4474/*
4475 * Enable PCH resources required for PCH ports:
4476 * - PCH PLLs
4477 * - FDI training & RX/TX
4478 * - update transcoder timings
4479 * - DP transcoding bits
4480 * - transcoder
4481 */
2ce42273 4482static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
0e23b99d 4483{
2ce42273 4484 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4485 struct drm_device *dev = crtc->base.dev;
fac5e23e 4486 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4487 int pipe = crtc->pipe;
f0f59a00 4488 u32 temp;
2c07245f 4489
ab9412ba 4490 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4491
fd6b8f43 4492 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4493 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4494
cd986abb
DV
4495 /* Write the TU size bits before fdi link training, so that error
4496 * detection works. */
4497 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4498 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4499
c98e9dcf 4500 /* For PCH output, training FDI link */
dc4a1094 4501 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4502
3ad8a208
DV
4503 /* We need to program the right clock selection before writing the pixel
4504 * mutliplier into the DPLL. */
6e266956 4505 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4506 u32 sel;
4b645f14 4507
c98e9dcf 4508 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4509 temp |= TRANS_DPLL_ENABLE(pipe);
4510 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4511 if (crtc_state->shared_dpll ==
8106ddbd 4512 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4513 temp |= sel;
4514 else
4515 temp &= ~sel;
c98e9dcf 4516 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4517 }
5eddb70b 4518
3ad8a208
DV
4519 /* XXX: pch pll's can be enabled any time before we enable the PCH
4520 * transcoder, and we actually should do this to not upset any PCH
4521 * transcoder that already use the clock when we share it.
4522 *
4523 * Note that enable_shared_dpll tries to do the right thing, but
4524 * get_shared_dpll unconditionally resets the pll - we need that to have
4525 * the right LVDS enable sequence. */
4cbe4b2b 4526 intel_enable_shared_dpll(crtc);
3ad8a208 4527
d9b6cb56
JB
4528 /* set transcoder timing, panel must allow it */
4529 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4530 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4531
303b81e0 4532 intel_fdi_normal_train(crtc);
5e84e1a4 4533
c98e9dcf 4534 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4535 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4536 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4537 const struct drm_display_mode *adjusted_mode =
2ce42273 4538 &crtc_state->base.adjusted_mode;
dfd07d72 4539 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4540 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4541 temp = I915_READ(reg);
4542 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4543 TRANS_DP_SYNC_MASK |
4544 TRANS_DP_BPC_MASK);
e3ef4479 4545 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4546 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4547
9c4edaee 4548 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4549 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4550 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4551 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4552
4553 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4554 case PORT_B:
5eddb70b 4555 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4556 break;
c48b5305 4557 case PORT_C:
5eddb70b 4558 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4559 break;
c48b5305 4560 case PORT_D:
5eddb70b 4561 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4562 break;
4563 default:
e95d41e1 4564 BUG();
32f9d658 4565 }
2c07245f 4566
5eddb70b 4567 I915_WRITE(reg, temp);
6be4a607 4568 }
b52eb4dc 4569
b8a4f404 4570 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4571}
4572
2ce42273 4573static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 4574{
2ce42273 4575 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4576 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4577 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4578
a2196033 4579 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1507e5bd 4580
8c52b5e8 4581 lpt_program_iclkip(crtc);
1507e5bd 4582
0540e488 4583 /* Set transcoder timing. */
0dcdc382 4584 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4585
937bb610 4586 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4587}
4588
a1520318 4589static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4590{
fac5e23e 4591 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4592 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4593 u32 temp;
4594
4595 temp = I915_READ(dslreg);
4596 udelay(500);
4597 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4598 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4599 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4600 }
4601}
4602
86adf9d7
ML
4603static int
4604skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 4605 unsigned int scaler_user, int *scaler_id,
86adf9d7 4606 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4607{
86adf9d7
ML
4608 struct intel_crtc_scaler_state *scaler_state =
4609 &crtc_state->scaler_state;
4610 struct intel_crtc *intel_crtc =
4611 to_intel_crtc(crtc_state->base.crtc);
7f58cbb1
MK
4612 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4613 const struct drm_display_mode *adjusted_mode =
4614 &crtc_state->base.adjusted_mode;
a1b2278e 4615 int need_scaling;
6156a456 4616
d96a7d2a
VS
4617 /*
4618 * Src coordinates are already rotated by 270 degrees for
4619 * the 90/270 degree plane rotation cases (to match the
4620 * GTT mapping), hence no need to account for rotation here.
4621 */
4622 need_scaling = src_w != dst_w || src_h != dst_h;
a1b2278e 4623
e5c05931
SS
4624 if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
4625 need_scaling = true;
4626
7f58cbb1
MK
4627 /*
4628 * Scaling/fitting not supported in IF-ID mode in GEN9+
4629 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4630 * Once NV12 is enabled, handle it here while allocating scaler
4631 * for NV12.
4632 */
4633 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4634 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4635 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4636 return -EINVAL;
4637 }
4638
a1b2278e
CK
4639 /*
4640 * if plane is being disabled or scaler is no more required or force detach
4641 * - free scaler binded to this plane/crtc
4642 * - in order to do this, update crtc->scaler_usage
4643 *
4644 * Here scaler state in crtc_state is set free so that
4645 * scaler can be assigned to other user. Actual register
4646 * update to free the scaler is done in plane/panel-fit programming.
4647 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4648 */
86adf9d7 4649 if (force_detach || !need_scaling) {
a1b2278e 4650 if (*scaler_id >= 0) {
86adf9d7 4651 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4652 scaler_state->scalers[*scaler_id].in_use = 0;
4653
86adf9d7
ML
4654 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4655 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4656 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4657 scaler_state->scaler_users);
4658 *scaler_id = -1;
4659 }
4660 return 0;
4661 }
4662
4663 /* range checks */
4664 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4665 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4666
4667 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4668 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4669 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4670 "size is out of scaler range\n",
86adf9d7 4671 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4672 return -EINVAL;
4673 }
4674
86adf9d7
ML
4675 /* mark this plane as a scaler user in crtc_state */
4676 scaler_state->scaler_users |= (1 << scaler_user);
4677 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4678 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4679 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4680 scaler_state->scaler_users);
4681
4682 return 0;
4683}
4684
4685/**
4686 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4687 *
4688 * @state: crtc's scaler state
86adf9d7
ML
4689 *
4690 * Return
4691 * 0 - scaler_usage updated successfully
4692 * error - requested scaling cannot be supported or other error condition
4693 */
e435d6e5 4694int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4695{
7c5f93b0 4696 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4697
e435d6e5 4698 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
d96a7d2a 4699 &state->scaler_state.scaler_id,
86adf9d7 4700 state->pipe_src_w, state->pipe_src_h,
aad941d5 4701 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4702}
4703
4704/**
4705 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4706 *
4707 * @state: crtc's scaler state
86adf9d7
ML
4708 * @plane_state: atomic plane state to update
4709 *
4710 * Return
4711 * 0 - scaler_usage updated successfully
4712 * error - requested scaling cannot be supported or other error condition
4713 */
da20eabd
ML
4714static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4715 struct intel_plane_state *plane_state)
86adf9d7
ML
4716{
4717
da20eabd
ML
4718 struct intel_plane *intel_plane =
4719 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4720 struct drm_framebuffer *fb = plane_state->base.fb;
4721 int ret;
4722
936e71e3 4723 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4724
86adf9d7
ML
4725 ret = skl_update_scaler(crtc_state, force_detach,
4726 drm_plane_index(&intel_plane->base),
4727 &plane_state->scaler_id,
936e71e3
VS
4728 drm_rect_width(&plane_state->base.src) >> 16,
4729 drm_rect_height(&plane_state->base.src) >> 16,
4730 drm_rect_width(&plane_state->base.dst),
4731 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4732
4733 if (ret || plane_state->scaler_id < 0)
4734 return ret;
4735
a1b2278e 4736 /* check colorkey */
818ed961 4737 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4738 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4739 intel_plane->base.base.id,
4740 intel_plane->base.name);
a1b2278e
CK
4741 return -EINVAL;
4742 }
4743
4744 /* Check src format */
438b74a5 4745 switch (fb->format->format) {
86adf9d7
ML
4746 case DRM_FORMAT_RGB565:
4747 case DRM_FORMAT_XBGR8888:
4748 case DRM_FORMAT_XRGB8888:
4749 case DRM_FORMAT_ABGR8888:
4750 case DRM_FORMAT_ARGB8888:
4751 case DRM_FORMAT_XRGB2101010:
4752 case DRM_FORMAT_XBGR2101010:
4753 case DRM_FORMAT_YUYV:
4754 case DRM_FORMAT_YVYU:
4755 case DRM_FORMAT_UYVY:
4756 case DRM_FORMAT_VYUY:
4757 break;
4758 default:
72660ce0
VS
4759 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4760 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 4761 fb->base.id, fb->format->format);
86adf9d7 4762 return -EINVAL;
a1b2278e
CK
4763 }
4764
a1b2278e
CK
4765 return 0;
4766}
4767
e435d6e5
ML
4768static void skylake_scaler_disable(struct intel_crtc *crtc)
4769{
4770 int i;
4771
4772 for (i = 0; i < crtc->num_scalers; i++)
4773 skl_detach_scaler(crtc, i);
4774}
4775
4776static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4777{
4778 struct drm_device *dev = crtc->base.dev;
fac5e23e 4779 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4780 int pipe = crtc->pipe;
a1b2278e
CK
4781 struct intel_crtc_scaler_state *scaler_state =
4782 &crtc->config->scaler_state;
4783
6e3c9717 4784 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4785 int id;
4786
c3f8ad57 4787 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 4788 return;
a1b2278e
CK
4789
4790 id = scaler_state->scaler_id;
4791 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4792 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4793 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4794 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
4795 }
4796}
4797
b074cec8
JB
4798static void ironlake_pfit_enable(struct intel_crtc *crtc)
4799{
4800 struct drm_device *dev = crtc->base.dev;
fac5e23e 4801 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4802 int pipe = crtc->pipe;
4803
6e3c9717 4804 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4805 /* Force use of hard-coded filter coefficients
4806 * as some pre-programmed values are broken,
4807 * e.g. x201.
4808 */
fd6b8f43 4809 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4810 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4811 PF_PIPE_SEL_IVB(pipe));
4812 else
4813 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4814 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4815 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4816 }
4817}
4818
20bc8673 4819void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4820{
cea165c3 4821 struct drm_device *dev = crtc->base.dev;
fac5e23e 4822 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4823
6e3c9717 4824 if (!crtc->config->ips_enabled)
d77e4531
PZ
4825 return;
4826
307e4498
ML
4827 /*
4828 * We can only enable IPS after we enable a plane and wait for a vblank
4829 * This function is called from post_plane_update, which is run after
4830 * a vblank wait.
4831 */
cea165c3 4832
d87ce764
VS
4833 assert_plane_enabled(to_intel_plane(crtc->base.primary));
4834
8652744b 4835 if (IS_BROADWELL(dev_priv)) {
9f817501 4836 mutex_lock(&dev_priv->pcu_lock);
61843f0e
VS
4837 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
4838 IPS_ENABLE | IPS_PCODE_CONTROL));
9f817501 4839 mutex_unlock(&dev_priv->pcu_lock);
2a114cc1
BW
4840 /* Quoting Art Runyan: "its not safe to expect any particular
4841 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4842 * mailbox." Moreover, the mailbox may return a bogus state,
4843 * so we need to just enable it and continue on.
2a114cc1
BW
4844 */
4845 } else {
4846 I915_WRITE(IPS_CTL, IPS_ENABLE);
4847 /* The bit only becomes 1 in the next vblank, so this wait here
4848 * is essentially intel_wait_for_vblank. If we don't have this
4849 * and don't wait for vblanks until the end of crtc_enable, then
4850 * the HW state readout code will complain that the expected
4851 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4852 if (intel_wait_for_register(dev_priv,
4853 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4854 50))
2a114cc1
BW
4855 DRM_ERROR("Timed out waiting for IPS enable\n");
4856 }
d77e4531
PZ
4857}
4858
20bc8673 4859void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4860{
4861 struct drm_device *dev = crtc->base.dev;
fac5e23e 4862 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4863
6e3c9717 4864 if (!crtc->config->ips_enabled)
d77e4531
PZ
4865 return;
4866
d87ce764
VS
4867 assert_plane_enabled(to_intel_plane(crtc->base.primary));
4868
8652744b 4869 if (IS_BROADWELL(dev_priv)) {
9f817501 4870 mutex_lock(&dev_priv->pcu_lock);
2a114cc1 4871 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
9f817501 4872 mutex_unlock(&dev_priv->pcu_lock);
23d0b130 4873 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4874 if (intel_wait_for_register(dev_priv,
4875 IPS_CTL, IPS_ENABLE, 0,
4876 42))
23d0b130 4877 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4878 } else {
2a114cc1 4879 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4880 POSTING_READ(IPS_CTL);
4881 }
d77e4531
PZ
4882
4883 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4884 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4885}
4886
7cac945f 4887static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4888{
7cac945f 4889 if (intel_crtc->overlay) {
d3eedb1a 4890 struct drm_device *dev = intel_crtc->base.dev;
d3eedb1a
VS
4891
4892 mutex_lock(&dev->struct_mutex);
d3eedb1a 4893 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
4894 mutex_unlock(&dev->struct_mutex);
4895 }
4896
4897 /* Let userspace switch the overlay on again. In most cases userspace
4898 * has to recompute where to put it anyway.
4899 */
4900}
4901
87d4300a
ML
4902/**
4903 * intel_post_enable_primary - Perform operations after enabling primary plane
4904 * @crtc: the CRTC whose primary plane was just enabled
4905 *
4906 * Performs potentially sleeping operations that must be done after the primary
4907 * plane is enabled, such as updating FBC and IPS. Note that this may be
4908 * called due to an explicit primary plane update, or due to an implicit
4909 * re-enable that is caused when a sprite plane is updated to no longer
4910 * completely hide the primary plane.
4911 */
4912static void
4913intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4914{
4915 struct drm_device *dev = crtc->dev;
fac5e23e 4916 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4918 int pipe = intel_crtc->pipe;
a5c4d7bc 4919
87d4300a
ML
4920 /*
4921 * FIXME IPS should be fine as long as one plane is
4922 * enabled, but in practice it seems to have problems
4923 * when going from primary only to sprite only and vice
4924 * versa.
4925 */
a5c4d7bc
VS
4926 hsw_enable_ips(intel_crtc);
4927
f99d7069 4928 /*
87d4300a
ML
4929 * Gen2 reports pipe underruns whenever all planes are disabled.
4930 * So don't enable underrun reporting before at least some planes
4931 * are enabled.
4932 * FIXME: Need to fix the logic to work when we turn off all planes
4933 * but leave the pipe running.
f99d7069 4934 */
5db94019 4935 if (IS_GEN2(dev_priv))
87d4300a
ML
4936 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4937
aca7b684
VS
4938 /* Underruns don't always raise interrupts, so check manually. */
4939 intel_check_cpu_fifo_underruns(dev_priv);
4940 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4941}
4942
2622a081 4943/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4944static void
4945intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4946{
4947 struct drm_device *dev = crtc->dev;
fac5e23e 4948 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4950 int pipe = intel_crtc->pipe;
a5c4d7bc 4951
87d4300a
ML
4952 /*
4953 * Gen2 reports pipe underruns whenever all planes are disabled.
4954 * So diasble underrun reporting before all the planes get disabled.
4955 * FIXME: Need to fix the logic to work when we turn off all planes
4956 * but leave the pipe running.
4957 */
5db94019 4958 if (IS_GEN2(dev_priv))
87d4300a 4959 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4960
2622a081
VS
4961 /*
4962 * FIXME IPS should be fine as long as one plane is
4963 * enabled, but in practice it seems to have problems
4964 * when going from primary only to sprite only and vice
4965 * versa.
4966 */
4967 hsw_disable_ips(intel_crtc);
4968}
4969
4970/* FIXME get rid of this and use pre_plane_update */
4971static void
4972intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4973{
4974 struct drm_device *dev = crtc->dev;
fac5e23e 4975 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
4976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4977 int pipe = intel_crtc->pipe;
4978
4979 intel_pre_disable_primary(crtc);
4980
87d4300a
ML
4981 /*
4982 * Vblank time updates from the shadow to live plane control register
4983 * are blocked if the memory self-refresh mode is active at that
4984 * moment. So to make sure the plane gets truly disabled, disable
4985 * first the self-refresh mode. The self-refresh enable bit in turn
4986 * will be checked/applied by the HW only at the next frame start
4987 * event which is after the vblank start event, so we need to have a
4988 * wait-for-vblank between disabling the plane and the pipe.
4989 */
11a85d6a
VS
4990 if (HAS_GMCH_DISPLAY(dev_priv) &&
4991 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 4992 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
4993}
4994
5a21b665
DV
4995static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4996{
4997 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4998 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4999 struct intel_crtc_state *pipe_config =
f9a8c149
VS
5000 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5001 crtc);
5a21b665
DV
5002 struct drm_plane *primary = crtc->base.primary;
5003 struct drm_plane_state *old_pri_state =
5004 drm_atomic_get_existing_plane_state(old_state, primary);
5005
5748b6a1 5006 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 5007
5a21b665 5008 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5009 intel_update_watermarks(crtc);
5a21b665
DV
5010
5011 if (old_pri_state) {
5012 struct intel_plane_state *primary_state =
f9a8c149
VS
5013 intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
5014 to_intel_plane(primary));
5a21b665
DV
5015 struct intel_plane_state *old_primary_state =
5016 to_intel_plane_state(old_pri_state);
5017
5018 intel_fbc_post_update(crtc);
5019
936e71e3 5020 if (primary_state->base.visible &&
5a21b665 5021 (needs_modeset(&pipe_config->base) ||
936e71e3 5022 !old_primary_state->base.visible))
5a21b665
DV
5023 intel_post_enable_primary(&crtc->base);
5024 }
5025}
5026
aa5e9b47
ML
5027static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5028 struct intel_crtc_state *pipe_config)
ac21b225 5029{
5c74cd73 5030 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5031 struct drm_device *dev = crtc->base.dev;
fac5e23e 5032 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5033 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5034 struct drm_plane *primary = crtc->base.primary;
5035 struct drm_plane_state *old_pri_state =
5036 drm_atomic_get_existing_plane_state(old_state, primary);
5037 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5038 struct intel_atomic_state *old_intel_state =
5039 to_intel_atomic_state(old_state);
ac21b225 5040
5c74cd73
ML
5041 if (old_pri_state) {
5042 struct intel_plane_state *primary_state =
f9a8c149
VS
5043 intel_atomic_get_new_plane_state(old_intel_state,
5044 to_intel_plane(primary));
5c74cd73
ML
5045 struct intel_plane_state *old_primary_state =
5046 to_intel_plane_state(old_pri_state);
5047
faf68d92 5048 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5049
936e71e3
VS
5050 if (old_primary_state->base.visible &&
5051 (modeset || !primary_state->base.visible))
5c74cd73
ML
5052 intel_pre_disable_primary(&crtc->base);
5053 }
852eb00d 5054
5eeb798b
VS
5055 /*
5056 * Vblank time updates from the shadow to live plane control register
5057 * are blocked if the memory self-refresh mode is active at that
5058 * moment. So to make sure the plane gets truly disabled, disable
5059 * first the self-refresh mode. The self-refresh enable bit in turn
5060 * will be checked/applied by the HW only at the next frame start
5061 * event which is after the vblank start event, so we need to have a
5062 * wait-for-vblank between disabling the plane and the pipe.
5063 */
5064 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5065 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5066 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5067
ed4a6a7c
MR
5068 /*
5069 * IVB workaround: must disable low power watermarks for at least
5070 * one frame before enabling scaling. LP watermarks can be re-enabled
5071 * when scaling is disabled.
5072 *
5073 * WaCxSRDisabledForSpriteScaling:ivb
5074 */
ddd2b792 5075 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5076 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5077
5078 /*
5079 * If we're doing a modeset, we're done. No need to do any pre-vblank
5080 * watermark programming here.
5081 */
5082 if (needs_modeset(&pipe_config->base))
5083 return;
5084
5085 /*
5086 * For platforms that support atomic watermarks, program the
5087 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5088 * will be the intermediate values that are safe for both pre- and
5089 * post- vblank; when vblank happens, the 'active' values will be set
5090 * to the final 'target' values and we'll do this again to get the
5091 * optimal watermarks. For gen9+ platforms, the values we program here
5092 * will be the final target values which will get automatically latched
5093 * at vblank time; no further programming will be necessary.
5094 *
5095 * If a platform hasn't been transitioned to atomic watermarks yet,
5096 * we'll continue to update watermarks the old way, if flags tell
5097 * us to.
5098 */
5099 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5100 dev_priv->display.initial_watermarks(old_intel_state,
5101 pipe_config);
caed361d 5102 else if (pipe_config->update_wm_pre)
432081bc 5103 intel_update_watermarks(crtc);
ac21b225
ML
5104}
5105
d032ffa0 5106static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5107{
5108 struct drm_device *dev = crtc->dev;
5109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5110 struct drm_plane *p;
87d4300a
ML
5111 int pipe = intel_crtc->pipe;
5112
7cac945f 5113 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5114
d032ffa0 5115 drm_for_each_plane_mask(p, dev, plane_mask)
282dbf9b 5116 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
f98551ae 5117
f99d7069
DV
5118 /*
5119 * FIXME: Once we grow proper nuclear flip support out of this we need
5120 * to compute the mask of flip planes precisely. For the time being
5121 * consider this a flip to a NULL plane.
5122 */
5748b6a1 5123 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5124}
5125
fb1c98b1 5126static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5127 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5128 struct drm_atomic_state *old_state)
5129{
aa5e9b47 5130 struct drm_connector_state *conn_state;
fb1c98b1
ML
5131 struct drm_connector *conn;
5132 int i;
5133
aa5e9b47 5134 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5135 struct intel_encoder *encoder =
5136 to_intel_encoder(conn_state->best_encoder);
5137
5138 if (conn_state->crtc != crtc)
5139 continue;
5140
5141 if (encoder->pre_pll_enable)
fd6bbda9 5142 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5143 }
5144}
5145
5146static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5147 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5148 struct drm_atomic_state *old_state)
5149{
aa5e9b47 5150 struct drm_connector_state *conn_state;
fb1c98b1
ML
5151 struct drm_connector *conn;
5152 int i;
5153
aa5e9b47 5154 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5155 struct intel_encoder *encoder =
5156 to_intel_encoder(conn_state->best_encoder);
5157
5158 if (conn_state->crtc != crtc)
5159 continue;
5160
5161 if (encoder->pre_enable)
fd6bbda9 5162 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5163 }
5164}
5165
5166static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5167 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5168 struct drm_atomic_state *old_state)
5169{
aa5e9b47 5170 struct drm_connector_state *conn_state;
fb1c98b1
ML
5171 struct drm_connector *conn;
5172 int i;
5173
aa5e9b47 5174 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5175 struct intel_encoder *encoder =
5176 to_intel_encoder(conn_state->best_encoder);
5177
5178 if (conn_state->crtc != crtc)
5179 continue;
5180
fd6bbda9 5181 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5182 intel_opregion_notify_encoder(encoder, true);
5183 }
5184}
5185
5186static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5187 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5188 struct drm_atomic_state *old_state)
5189{
5190 struct drm_connector_state *old_conn_state;
5191 struct drm_connector *conn;
5192 int i;
5193
aa5e9b47 5194 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5195 struct intel_encoder *encoder =
5196 to_intel_encoder(old_conn_state->best_encoder);
5197
5198 if (old_conn_state->crtc != crtc)
5199 continue;
5200
5201 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5202 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5203 }
5204}
5205
5206static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5207 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5208 struct drm_atomic_state *old_state)
5209{
5210 struct drm_connector_state *old_conn_state;
5211 struct drm_connector *conn;
5212 int i;
5213
aa5e9b47 5214 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5215 struct intel_encoder *encoder =
5216 to_intel_encoder(old_conn_state->best_encoder);
5217
5218 if (old_conn_state->crtc != crtc)
5219 continue;
5220
5221 if (encoder->post_disable)
fd6bbda9 5222 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5223 }
5224}
5225
5226static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5227 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5228 struct drm_atomic_state *old_state)
5229{
5230 struct drm_connector_state *old_conn_state;
5231 struct drm_connector *conn;
5232 int i;
5233
aa5e9b47 5234 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5235 struct intel_encoder *encoder =
5236 to_intel_encoder(old_conn_state->best_encoder);
5237
5238 if (old_conn_state->crtc != crtc)
5239 continue;
5240
5241 if (encoder->post_pll_disable)
fd6bbda9 5242 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5243 }
5244}
5245
4a806558
ML
5246static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5247 struct drm_atomic_state *old_state)
f67a559d 5248{
4a806558 5249 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5250 struct drm_device *dev = crtc->dev;
fac5e23e 5251 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5253 int pipe = intel_crtc->pipe;
ccf010fb
ML
5254 struct intel_atomic_state *old_intel_state =
5255 to_intel_atomic_state(old_state);
f67a559d 5256
53d9f4e9 5257 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5258 return;
5259
b2c0593a
VS
5260 /*
5261 * Sometimes spurious CPU pipe underruns happen during FDI
5262 * training, at least with VGA+HDMI cloning. Suppress them.
5263 *
5264 * On ILK we get an occasional spurious CPU pipe underruns
5265 * between eDP port A enable and vdd enable. Also PCH port
5266 * enable seems to result in the occasional CPU pipe underrun.
5267 *
5268 * Spurious PCH underruns also occur during PCH enabling.
5269 */
5270 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5271 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5272 if (intel_crtc->config->has_pch_encoder)
5273 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5274
6e3c9717 5275 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
5276 intel_prepare_shared_dpll(intel_crtc);
5277
37a5650b 5278 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5279 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
5280
5281 intel_set_pipe_timings(intel_crtc);
bc58be60 5282 intel_set_pipe_src_size(intel_crtc);
29407aab 5283
6e3c9717 5284 if (intel_crtc->config->has_pch_encoder) {
29407aab 5285 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5286 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
5287 }
5288
5289 ironlake_set_pipeconf(crtc);
5290
f67a559d 5291 intel_crtc->active = true;
8664281b 5292
fd6bbda9 5293 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5294
6e3c9717 5295 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
5296 /* Note: FDI PLL enabling _must_ be done before we enable the
5297 * cpu pipes, hence this is separate from all the other fdi/pch
5298 * enabling. */
88cefb6c 5299 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
5300 } else {
5301 assert_fdi_tx_disabled(dev_priv, pipe);
5302 assert_fdi_rx_disabled(dev_priv, pipe);
5303 }
f67a559d 5304
b074cec8 5305 ironlake_pfit_enable(intel_crtc);
f67a559d 5306
9c54c0dd
JB
5307 /*
5308 * On ILK+ LUT must be loaded before the pipe is running but with
5309 * clocks enabled
5310 */
b95c5321 5311 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5312
1d5bf5d9 5313 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5314 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5315 intel_enable_pipe(intel_crtc);
f67a559d 5316
6e3c9717 5317 if (intel_crtc->config->has_pch_encoder)
2ce42273 5318 ironlake_pch_enable(pipe_config);
c98e9dcf 5319
f9b61ff6
DV
5320 assert_vblank_disabled(crtc);
5321 drm_crtc_vblank_on(crtc);
5322
fd6bbda9 5323 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5324
6e266956 5325 if (HAS_PCH_CPT(dev_priv))
a1520318 5326 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5327
5328 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5329 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5330 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5331 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5332 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5333}
5334
42db64ef
PZ
5335/* IPS only exists on ULT machines and is tied to pipe A. */
5336static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5337{
50a0bc90 5338 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5339}
5340
ed69cd40
ID
5341static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5342 enum pipe pipe, bool apply)
5343{
5344 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5345 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5346
5347 if (apply)
5348 val |= mask;
5349 else
5350 val &= ~mask;
5351
5352 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5353}
5354
4a806558
ML
5355static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5356 struct drm_atomic_state *old_state)
4f771f10 5357{
4a806558 5358 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5359 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5361 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5362 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5363 struct intel_atomic_state *old_intel_state =
5364 to_intel_atomic_state(old_state);
ed69cd40 5365 bool psl_clkgate_wa;
4f771f10 5366
53d9f4e9 5367 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5368 return;
5369
fd6bbda9 5370 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5371
8106ddbd 5372 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
5373 intel_enable_shared_dpll(intel_crtc);
5374
37a5650b 5375 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5376 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5377
d7edc4e5 5378 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5379 intel_set_pipe_timings(intel_crtc);
5380
bc58be60 5381 intel_set_pipe_src_size(intel_crtc);
229fca97 5382
4d1de975
JN
5383 if (cpu_transcoder != TRANSCODER_EDP &&
5384 !transcoder_is_dsi(cpu_transcoder)) {
5385 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5386 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5387 }
5388
6e3c9717 5389 if (intel_crtc->config->has_pch_encoder) {
229fca97 5390 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5391 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5392 }
5393
d7edc4e5 5394 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5395 haswell_set_pipeconf(crtc);
5396
391bf048 5397 haswell_set_pipemisc(crtc);
229fca97 5398
b95c5321 5399 intel_color_set_csc(&pipe_config->base);
229fca97 5400
4f771f10 5401 intel_crtc->active = true;
8664281b 5402
fd6bbda9 5403 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5404
d7edc4e5 5405 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5406 intel_ddi_enable_pipe_clock(pipe_config);
4f771f10 5407
ed69cd40
ID
5408 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5409 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
5410 intel_crtc->config->pch_pfit.enabled;
5411 if (psl_clkgate_wa)
5412 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5413
6315b5d3 5414 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5415 skylake_pfit_enable(intel_crtc);
ff6d9f55 5416 else
1c132b44 5417 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5418
5419 /*
5420 * On ILK+ LUT must be loaded before the pipe is running but with
5421 * clocks enabled
5422 */
b95c5321 5423 intel_color_load_luts(&pipe_config->base);
4f771f10 5424
3dc38eea 5425 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5426 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5427 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5428
1d5bf5d9 5429 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5430 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975
JN
5431
5432 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5433 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5434 intel_enable_pipe(intel_crtc);
42db64ef 5435
6e3c9717 5436 if (intel_crtc->config->has_pch_encoder)
2ce42273 5437 lpt_pch_enable(pipe_config);
4f771f10 5438
0037071d 5439 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5440 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5441
f9b61ff6
DV
5442 assert_vblank_disabled(crtc);
5443 drm_crtc_vblank_on(crtc);
5444
fd6bbda9 5445 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5446
ed69cd40 5447 if (psl_clkgate_wa) {
0f0f74bc 5448 intel_wait_for_vblank(dev_priv, pipe);
ed69cd40 5449 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
6b698516 5450 }
d2d65408 5451
e4916946
PZ
5452 /* If we change the relative order between pipe/planes enabling, we need
5453 * to change the workaround. */
99d736a2 5454 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5455 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5456 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5457 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5458 }
4f771f10
PZ
5459}
5460
bfd16b2a 5461static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5462{
5463 struct drm_device *dev = crtc->base.dev;
fac5e23e 5464 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
DV
5465 int pipe = crtc->pipe;
5466
5467 /* To avoid upsetting the power well on haswell only disable the pfit if
5468 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5469 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5470 I915_WRITE(PF_CTL(pipe), 0);
5471 I915_WRITE(PF_WIN_POS(pipe), 0);
5472 I915_WRITE(PF_WIN_SZ(pipe), 0);
5473 }
5474}
5475
4a806558
ML
5476static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5477 struct drm_atomic_state *old_state)
6be4a607 5478{
4a806558 5479 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5480 struct drm_device *dev = crtc->dev;
fac5e23e 5481 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5483 int pipe = intel_crtc->pipe;
b52eb4dc 5484
b2c0593a
VS
5485 /*
5486 * Sometimes spurious CPU pipe underruns happen when the
5487 * pipe is already disabled, but FDI RX/TX is still enabled.
5488 * Happens at least with VGA+HDMI cloning. Suppress them.
5489 */
5490 if (intel_crtc->config->has_pch_encoder) {
5491 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5492 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5493 }
37ca8d4c 5494
fd6bbda9 5495 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5496
f9b61ff6
DV
5497 drm_crtc_vblank_off(crtc);
5498 assert_vblank_disabled(crtc);
5499
575f7ab7 5500 intel_disable_pipe(intel_crtc);
32f9d658 5501
bfd16b2a 5502 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5503
b2c0593a 5504 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5505 ironlake_fdi_disable(crtc);
5506
fd6bbda9 5507 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5508
6e3c9717 5509 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5510 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5511
6e266956 5512 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5513 i915_reg_t reg;
5514 u32 temp;
5515
d925c59a
DV
5516 /* disable TRANS_DP_CTL */
5517 reg = TRANS_DP_CTL(pipe);
5518 temp = I915_READ(reg);
5519 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5520 TRANS_DP_PORT_SEL_MASK);
5521 temp |= TRANS_DP_PORT_SEL_NONE;
5522 I915_WRITE(reg, temp);
5523
5524 /* disable DPLL_SEL */
5525 temp = I915_READ(PCH_DPLL_SEL);
11887397 5526 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5527 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5528 }
e3421a18 5529
d925c59a
DV
5530 ironlake_fdi_pll_disable(intel_crtc);
5531 }
81b088ca 5532
b2c0593a 5533 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5534 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5535}
1b3c7a47 5536
4a806558
ML
5537static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5538 struct drm_atomic_state *old_state)
ee7b9f93 5539{
4a806558 5540 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5541 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5543 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5544
fd6bbda9 5545 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5546
f9b61ff6
DV
5547 drm_crtc_vblank_off(crtc);
5548 assert_vblank_disabled(crtc);
5549
4d1de975 5550 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5551 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5552 intel_disable_pipe(intel_crtc);
4f771f10 5553
0037071d 5554 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5555 intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
a4bf214f 5556
d7edc4e5 5557 if (!transcoder_is_dsi(cpu_transcoder))
af25065b 5558 intel_ddi_disable_transcoder_func(old_crtc_state);
4f771f10 5559
6315b5d3 5560 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5561 skylake_scaler_disable(intel_crtc);
ff6d9f55 5562 else
bfd16b2a 5563 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5564
d7edc4e5 5565 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5566 intel_ddi_disable_pipe_clock(intel_crtc->config);
4f771f10 5567
fd6bbda9 5568 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
4f771f10
PZ
5569}
5570
2dd24552
JB
5571static void i9xx_pfit_enable(struct intel_crtc *crtc)
5572{
5573 struct drm_device *dev = crtc->base.dev;
fac5e23e 5574 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5575 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5576
681a8504 5577 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5578 return;
5579
2dd24552 5580 /*
c0b03411
DV
5581 * The panel fitter should only be adjusted whilst the pipe is disabled,
5582 * according to register description and PRM.
2dd24552 5583 */
c0b03411
DV
5584 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5585 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5586
b074cec8
JB
5587 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5588 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5589
5590 /* Border color in case we don't scale up to the full screen. Black by
5591 * default, change to something else for debugging. */
5592 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5593}
5594
79f255a0 5595enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
5596{
5597 switch (port) {
5598 case PORT_A:
6331a704 5599 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5600 case PORT_B:
6331a704 5601 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5602 case PORT_C:
6331a704 5603 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5604 case PORT_D:
6331a704 5605 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5606 case PORT_E:
6331a704 5607 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5608 default:
b9fec167 5609 MISSING_CASE(port);
d05410f9
DA
5610 return POWER_DOMAIN_PORT_OTHER;
5611 }
5612}
5613
d8fc70b7
ACO
5614static u64 get_crtc_power_domains(struct drm_crtc *crtc,
5615 struct intel_crtc_state *crtc_state)
77d22dca 5616{
319be8ae 5617 struct drm_device *dev = crtc->dev;
37255d8d 5618 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 5619 struct drm_encoder *encoder;
319be8ae
ID
5620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5621 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 5622 u64 mask;
74bff5f9 5623 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5624
74bff5f9 5625 if (!crtc_state->base.active)
292b990e
ML
5626 return 0;
5627
77d22dca
ID
5628 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5629 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5630 if (crtc_state->pch_pfit.enabled ||
5631 crtc_state->pch_pfit.force_thru)
d8fc70b7 5632 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 5633
74bff5f9
ML
5634 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5635 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5636
79f255a0 5637 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 5638 }
319be8ae 5639
37255d8d
ML
5640 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
5641 mask |= BIT(POWER_DOMAIN_AUDIO);
5642
15e7ec29 5643 if (crtc_state->shared_dpll)
d8fc70b7 5644 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 5645
77d22dca
ID
5646 return mask;
5647}
5648
d2d15016 5649static u64
74bff5f9
ML
5650modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5651 struct intel_crtc_state *crtc_state)
77d22dca 5652{
fac5e23e 5653 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5655 enum intel_display_power_domain domain;
d8fc70b7 5656 u64 domains, new_domains, old_domains;
77d22dca 5657
292b990e 5658 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5659 intel_crtc->enabled_power_domains = new_domains =
5660 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5661
5a21b665 5662 domains = new_domains & ~old_domains;
292b990e
ML
5663
5664 for_each_power_domain(domain, domains)
5665 intel_display_power_get(dev_priv, domain);
5666
5a21b665 5667 return old_domains & ~new_domains;
292b990e
ML
5668}
5669
5670static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 5671 u64 domains)
292b990e
ML
5672{
5673 enum intel_display_power_domain domain;
5674
5675 for_each_power_domain(domain, domains)
5676 intel_display_power_put(dev_priv, domain);
5677}
77d22dca 5678
7ff89ca2
VS
5679static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
5680 struct drm_atomic_state *old_state)
adafdc6f 5681{
ff32c54e
VS
5682 struct intel_atomic_state *old_intel_state =
5683 to_intel_atomic_state(old_state);
7ff89ca2
VS
5684 struct drm_crtc *crtc = pipe_config->base.crtc;
5685 struct drm_device *dev = crtc->dev;
5686 struct drm_i915_private *dev_priv = to_i915(dev);
5687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5688 int pipe = intel_crtc->pipe;
adafdc6f 5689
7ff89ca2
VS
5690 if (WARN_ON(intel_crtc->active))
5691 return;
adafdc6f 5692
7ff89ca2
VS
5693 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5694 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 5695
7ff89ca2
VS
5696 intel_set_pipe_timings(intel_crtc);
5697 intel_set_pipe_src_size(intel_crtc);
b2045352 5698
7ff89ca2
VS
5699 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
5700 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 5701
7ff89ca2
VS
5702 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5703 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
5704 }
5705
7ff89ca2 5706 i9xx_set_pipeconf(intel_crtc);
560a7ae4 5707
7ff89ca2 5708 intel_crtc->active = true;
92891e45 5709
7ff89ca2 5710 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5711
7ff89ca2 5712 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 5713
7ff89ca2
VS
5714 if (IS_CHERRYVIEW(dev_priv)) {
5715 chv_prepare_pll(intel_crtc, intel_crtc->config);
5716 chv_enable_pll(intel_crtc, intel_crtc->config);
5717 } else {
5718 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5719 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
5720 }
5721
7ff89ca2 5722 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 5723
7ff89ca2 5724 i9xx_pfit_enable(intel_crtc);
89b3c3c7 5725
7ff89ca2 5726 intel_color_load_luts(&pipe_config->base);
89b3c3c7 5727
ff32c54e
VS
5728 dev_priv->display.initial_watermarks(old_intel_state,
5729 pipe_config);
7ff89ca2
VS
5730 intel_enable_pipe(intel_crtc);
5731
5732 assert_vblank_disabled(crtc);
5733 drm_crtc_vblank_on(crtc);
89b3c3c7 5734
7ff89ca2 5735 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
5736}
5737
7ff89ca2 5738static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 5739{
7ff89ca2
VS
5740 struct drm_device *dev = crtc->base.dev;
5741 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 5742
7ff89ca2
VS
5743 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5744 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
5745}
5746
7ff89ca2
VS
5747static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
5748 struct drm_atomic_state *old_state)
2b73001e 5749{
04548cba
VS
5750 struct intel_atomic_state *old_intel_state =
5751 to_intel_atomic_state(old_state);
7ff89ca2
VS
5752 struct drm_crtc *crtc = pipe_config->base.crtc;
5753 struct drm_device *dev = crtc->dev;
5754 struct drm_i915_private *dev_priv = to_i915(dev);
5755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5756 enum pipe pipe = intel_crtc->pipe;
2b73001e 5757
7ff89ca2
VS
5758 if (WARN_ON(intel_crtc->active))
5759 return;
2b73001e 5760
7ff89ca2 5761 i9xx_set_pll_dividers(intel_crtc);
2b73001e 5762
7ff89ca2
VS
5763 if (intel_crtc_has_dp_encoder(intel_crtc->config))
5764 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 5765
7ff89ca2
VS
5766 intel_set_pipe_timings(intel_crtc);
5767 intel_set_pipe_src_size(intel_crtc);
2b73001e 5768
7ff89ca2 5769 i9xx_set_pipeconf(intel_crtc);
f8437dd1 5770
7ff89ca2 5771 intel_crtc->active = true;
5f199dfa 5772
7ff89ca2
VS
5773 if (!IS_GEN2(dev_priv))
5774 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 5775
7ff89ca2 5776 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 5777
939994da 5778 i9xx_enable_pll(intel_crtc, pipe_config);
f8437dd1 5779
7ff89ca2 5780 i9xx_pfit_enable(intel_crtc);
f8437dd1 5781
7ff89ca2 5782 intel_color_load_luts(&pipe_config->base);
f8437dd1 5783
04548cba
VS
5784 if (dev_priv->display.initial_watermarks != NULL)
5785 dev_priv->display.initial_watermarks(old_intel_state,
5786 intel_crtc->config);
5787 else
5788 intel_update_watermarks(intel_crtc);
7ff89ca2 5789 intel_enable_pipe(intel_crtc);
f8437dd1 5790
7ff89ca2
VS
5791 assert_vblank_disabled(crtc);
5792 drm_crtc_vblank_on(crtc);
f8437dd1 5793
7ff89ca2
VS
5794 intel_encoders_enable(crtc, pipe_config, old_state);
5795}
f8437dd1 5796
7ff89ca2
VS
5797static void i9xx_pfit_disable(struct intel_crtc *crtc)
5798{
5799 struct drm_device *dev = crtc->base.dev;
5800 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 5801
7ff89ca2 5802 if (!crtc->config->gmch_pfit.control)
f8437dd1 5803 return;
f8437dd1 5804
7ff89ca2
VS
5805 assert_pipe_disabled(dev_priv, crtc->pipe);
5806
5807 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5808 I915_READ(PFIT_CONTROL));
5809 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
5810}
5811
7ff89ca2
VS
5812static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
5813 struct drm_atomic_state *old_state)
f8437dd1 5814{
7ff89ca2
VS
5815 struct drm_crtc *crtc = old_crtc_state->base.crtc;
5816 struct drm_device *dev = crtc->dev;
5817 struct drm_i915_private *dev_priv = to_i915(dev);
5818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5819 int pipe = intel_crtc->pipe;
d66a2194 5820
d66a2194 5821 /*
7ff89ca2
VS
5822 * On gen2 planes are double buffered but the pipe isn't, so we must
5823 * wait for planes to fully turn off before disabling the pipe.
d66a2194 5824 */
7ff89ca2
VS
5825 if (IS_GEN2(dev_priv))
5826 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 5827
7ff89ca2 5828 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 5829
7ff89ca2
VS
5830 drm_crtc_vblank_off(crtc);
5831 assert_vblank_disabled(crtc);
d66a2194 5832
7ff89ca2 5833 intel_disable_pipe(intel_crtc);
d66a2194 5834
7ff89ca2 5835 i9xx_pfit_disable(intel_crtc);
89b3c3c7 5836
7ff89ca2 5837 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 5838
7ff89ca2
VS
5839 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
5840 if (IS_CHERRYVIEW(dev_priv))
5841 chv_disable_pll(dev_priv, pipe);
5842 else if (IS_VALLEYVIEW(dev_priv))
5843 vlv_disable_pll(dev_priv, pipe);
5844 else
5845 i9xx_disable_pll(intel_crtc);
5846 }
c2e001ef 5847
7ff89ca2 5848 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 5849
7ff89ca2
VS
5850 if (!IS_GEN2(dev_priv))
5851 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
5852
5853 if (!dev_priv->display.initial_watermarks)
5854 intel_update_watermarks(intel_crtc);
2ee0da16
VS
5855
5856 /* clock the pipe down to 640x480@60 to potentially save power */
5857 if (IS_I830(dev_priv))
5858 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
5859}
5860
da1d0e26
VS
5861static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
5862 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 5863{
7ff89ca2
VS
5864 struct intel_encoder *encoder;
5865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5866 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
5867 enum intel_display_power_domain domain;
23ac1273 5868 struct intel_plane *plane;
d2d15016 5869 u64 domains;
7ff89ca2
VS
5870 struct drm_atomic_state *state;
5871 struct intel_crtc_state *crtc_state;
5872 int ret;
f8437dd1 5873
7ff89ca2
VS
5874 if (!intel_crtc->active)
5875 return;
a8ca4934 5876
23ac1273
VS
5877 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
5878 const struct intel_plane_state *plane_state =
5879 to_intel_plane_state(plane->base.state);
709e05c3 5880
23ac1273
VS
5881 if (plane_state->base.visible)
5882 intel_plane_disable_noatomic(intel_crtc, plane);
7ff89ca2 5883 }
5d96d8af 5884
7ff89ca2
VS
5885 state = drm_atomic_state_alloc(crtc->dev);
5886 if (!state) {
5887 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
5888 crtc->base.id, crtc->name);
1c3f7700 5889 return;
7ff89ca2 5890 }
9f7eb31a 5891
da1d0e26 5892 state->acquire_ctx = ctx;
ea61791e 5893
7ff89ca2
VS
5894 /* Everything's already locked, -EDEADLK can't happen. */
5895 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5896 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 5897
7ff89ca2 5898 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 5899
7ff89ca2 5900 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 5901
0853695c 5902 drm_atomic_state_put(state);
842e0307 5903
78108b7c
VS
5904 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
5905 crtc->base.id, crtc->name);
842e0307
ML
5906
5907 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
5908 crtc->state->active = false;
37d9078b 5909 intel_crtc->active = false;
842e0307
ML
5910 crtc->enabled = false;
5911 crtc->state->connector_mask = 0;
5912 crtc->state->encoder_mask = 0;
5913
5914 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
5915 encoder->base.crtc = NULL;
5916
58f9c0bc 5917 intel_fbc_disable(intel_crtc);
432081bc 5918 intel_update_watermarks(intel_crtc);
1f7457b1 5919 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
5920
5921 domains = intel_crtc->enabled_power_domains;
5922 for_each_power_domain(domain, domains)
5923 intel_display_power_put(dev_priv, domain);
5924 intel_crtc->enabled_power_domains = 0;
565602d7
ML
5925
5926 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
d305e061 5927 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
b17d48e2
ML
5928}
5929
6b72d486
ML
5930/*
5931 * turn all crtc's off, but do not adjust state
5932 * This has to be paired with a call to intel_modeset_setup_hw_state.
5933 */
70e0bd74 5934int intel_display_suspend(struct drm_device *dev)
ee7b9f93 5935{
e2c8b870 5936 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 5937 struct drm_atomic_state *state;
e2c8b870 5938 int ret;
70e0bd74 5939
e2c8b870
ML
5940 state = drm_atomic_helper_suspend(dev);
5941 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
5942 if (ret)
5943 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
5944 else
5945 dev_priv->modeset_restore_state = state;
70e0bd74 5946 return ret;
ee7b9f93
JB
5947}
5948
ea5b213a 5949void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5950{
4ef69c7a 5951 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5952
ea5b213a
CW
5953 drm_encoder_cleanup(encoder);
5954 kfree(intel_encoder);
7e7d76c3
JB
5955}
5956
0a91ca29
DV
5957/* Cross check the actual hw state with our own modeset state tracking (and it's
5958 * internal consistency). */
749d98b8
ML
5959static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
5960 struct drm_connector_state *conn_state)
79e53945 5961{
749d98b8 5962 struct intel_connector *connector = to_intel_connector(conn_state->connector);
35dd3c64
ML
5963
5964 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5965 connector->base.base.id,
5966 connector->base.name);
5967
0a91ca29 5968 if (connector->get_hw_state(connector)) {
e85376cb 5969 struct intel_encoder *encoder = connector->encoder;
0a91ca29 5970
749d98b8 5971 I915_STATE_WARN(!crtc_state,
35dd3c64 5972 "connector enabled without attached crtc\n");
0a91ca29 5973
749d98b8 5974 if (!crtc_state)
35dd3c64
ML
5975 return;
5976
749d98b8 5977 I915_STATE_WARN(!crtc_state->active,
35dd3c64
ML
5978 "connector is active, but attached crtc isn't\n");
5979
e85376cb 5980 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
5981 return;
5982
e85376cb 5983 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
5984 "atomic encoder doesn't match attached encoder\n");
5985
e85376cb 5986 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
5987 "attached encoder crtc differs from connector crtc\n");
5988 } else {
749d98b8 5989 I915_STATE_WARN(crtc_state && crtc_state->active,
4d688a2a 5990 "attached crtc is active, but connector isn't\n");
749d98b8 5991 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 5992 "best encoder set without crtc!\n");
0a91ca29 5993 }
79e53945
JB
5994}
5995
08d9bc92
ACO
5996int intel_connector_init(struct intel_connector *connector)
5997{
11c1a9ec 5998 struct intel_digital_connector_state *conn_state;
08d9bc92 5999
11c1a9ec
ML
6000 /*
6001 * Allocate enough memory to hold intel_digital_connector_state,
6002 * This might be a few bytes too many, but for connectors that don't
6003 * need it we'll free the state and allocate a smaller one on the first
6004 * succesful commit anyway.
6005 */
6006 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6007 if (!conn_state)
08d9bc92
ACO
6008 return -ENOMEM;
6009
11c1a9ec
ML
6010 __drm_atomic_helper_connector_reset(&connector->base,
6011 &conn_state->base);
6012
08d9bc92
ACO
6013 return 0;
6014}
6015
6016struct intel_connector *intel_connector_alloc(void)
6017{
6018 struct intel_connector *connector;
6019
6020 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6021 if (!connector)
6022 return NULL;
6023
6024 if (intel_connector_init(connector) < 0) {
6025 kfree(connector);
6026 return NULL;
6027 }
6028
6029 return connector;
6030}
6031
091a4f91
JA
6032/*
6033 * Free the bits allocated by intel_connector_alloc.
6034 * This should only be used after intel_connector_alloc has returned
6035 * successfully, and before drm_connector_init returns successfully.
6036 * Otherwise the destroy callbacks for the connector and the state should
6037 * take care of proper cleanup/free
6038 */
6039void intel_connector_free(struct intel_connector *connector)
6040{
6041 kfree(to_intel_digital_connector_state(connector->base.state));
6042 kfree(connector);
6043}
6044
f0947c37
DV
6045/* Simple connector->get_hw_state implementation for encoders that support only
6046 * one connector and no cloning and hence the encoder state determines the state
6047 * of the connector. */
6048bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6049{
24929352 6050 enum pipe pipe = 0;
f0947c37 6051 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6052
f0947c37 6053 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6054}
6055
6d293983 6056static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6057{
6d293983
ACO
6058 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6059 return crtc_state->fdi_lanes;
d272ddfa
VS
6060
6061 return 0;
6062}
6063
6d293983 6064static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6065 struct intel_crtc_state *pipe_config)
1857e1da 6066{
8652744b 6067 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6068 struct drm_atomic_state *state = pipe_config->base.state;
6069 struct intel_crtc *other_crtc;
6070 struct intel_crtc_state *other_crtc_state;
6071
1857e1da
DV
6072 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6073 pipe_name(pipe), pipe_config->fdi_lanes);
6074 if (pipe_config->fdi_lanes > 4) {
6075 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6076 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6077 return -EINVAL;
1857e1da
DV
6078 }
6079
8652744b 6080 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
DV
6081 if (pipe_config->fdi_lanes > 2) {
6082 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6083 pipe_config->fdi_lanes);
6d293983 6084 return -EINVAL;
1857e1da 6085 } else {
6d293983 6086 return 0;
1857e1da
DV
6087 }
6088 }
6089
b7f05d4a 6090 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6091 return 0;
1857e1da
DV
6092
6093 /* Ivybridge 3 pipe is really complicated */
6094 switch (pipe) {
6095 case PIPE_A:
6d293983 6096 return 0;
1857e1da 6097 case PIPE_B:
6d293983
ACO
6098 if (pipe_config->fdi_lanes <= 2)
6099 return 0;
6100
b91eb5cc 6101 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6102 other_crtc_state =
6103 intel_atomic_get_crtc_state(state, other_crtc);
6104 if (IS_ERR(other_crtc_state))
6105 return PTR_ERR(other_crtc_state);
6106
6107 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6108 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6109 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6110 return -EINVAL;
1857e1da 6111 }
6d293983 6112 return 0;
1857e1da 6113 case PIPE_C:
251cc67c
VS
6114 if (pipe_config->fdi_lanes > 2) {
6115 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6116 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6117 return -EINVAL;
251cc67c 6118 }
6d293983 6119
b91eb5cc 6120 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6121 other_crtc_state =
6122 intel_atomic_get_crtc_state(state, other_crtc);
6123 if (IS_ERR(other_crtc_state))
6124 return PTR_ERR(other_crtc_state);
6125
6126 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6127 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6128 return -EINVAL;
1857e1da 6129 }
6d293983 6130 return 0;
1857e1da
DV
6131 default:
6132 BUG();
6133 }
6134}
6135
e29c22c0
DV
6136#define RETRY 1
6137static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6138 struct intel_crtc_state *pipe_config)
877d48d5 6139{
1857e1da 6140 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6141 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6142 int lane, link_bw, fdi_dotclock, ret;
6143 bool needs_recompute = false;
877d48d5 6144
e29c22c0 6145retry:
877d48d5
DV
6146 /* FDI is a binary signal running at ~2.7GHz, encoding
6147 * each output octet as 10 bits. The actual frequency
6148 * is stored as a divider into a 100MHz clock, and the
6149 * mode pixel clock is stored in units of 1KHz.
6150 * Hence the bw of each lane in terms of the mode signal
6151 * is:
6152 */
21a727b3 6153 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6154
241bfc38 6155 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6156
2bd89a07 6157 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6158 pipe_config->pipe_bpp);
6159
6160 pipe_config->fdi_lanes = lane;
6161
2bd89a07 6162 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
b31e85ed 6163 link_bw, &pipe_config->fdi_m_n, false);
1857e1da 6164
e3b247da 6165 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6166 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6167 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6168 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6169 pipe_config->pipe_bpp);
6170 needs_recompute = true;
6171 pipe_config->bw_constrained = true;
257a7ffc 6172
7ff89ca2 6173 goto retry;
257a7ffc 6174 }
79e53945 6175
7ff89ca2
VS
6176 if (needs_recompute)
6177 return RETRY;
e70236a8 6178
7ff89ca2 6179 return ret;
e70236a8
JB
6180}
6181
7ff89ca2
VS
6182static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6183 struct intel_crtc_state *pipe_config)
e70236a8 6184{
6e644626
VS
6185 if (pipe_config->ips_force_disable)
6186 return false;
6187
7ff89ca2
VS
6188 if (pipe_config->pipe_bpp > 24)
6189 return false;
e70236a8 6190
7ff89ca2
VS
6191 /* HSW can handle pixel rate up to cdclk? */
6192 if (IS_HASWELL(dev_priv))
6193 return true;
1b1d2716 6194
65cd2b3f 6195 /*
7ff89ca2
VS
6196 * We compare against max which means we must take
6197 * the increased cdclk requirement into account when
6198 * calculating the new cdclk.
6199 *
6200 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6201 */
7ff89ca2
VS
6202 return pipe_config->pixel_rate <=
6203 dev_priv->max_cdclk_freq * 95 / 100;
e70236a8 6204}
79e53945 6205
7ff89ca2
VS
6206static void hsw_compute_ips_config(struct intel_crtc *crtc,
6207 struct intel_crtc_state *pipe_config)
6208{
6209 struct drm_device *dev = crtc->base.dev;
6210 struct drm_i915_private *dev_priv = to_i915(dev);
34edce2f 6211
4f044a88 6212 pipe_config->ips_enabled = i915_modparams.enable_ips &&
7ff89ca2
VS
6213 hsw_crtc_supports_ips(crtc) &&
6214 pipe_config_supports_ips(dev_priv, pipe_config);
34edce2f
VS
6215}
6216
7ff89ca2 6217static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6218{
7ff89ca2 6219 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6220
7ff89ca2
VS
6221 /* GDG double wide on either pipe, otherwise pipe A only */
6222 return INTEL_INFO(dev_priv)->gen < 4 &&
6223 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6224}
6225
ceb99320
VS
6226static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6227{
6228 uint32_t pixel_rate;
6229
6230 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6231
6232 /*
6233 * We only use IF-ID interlacing. If we ever use
6234 * PF-ID we'll need to adjust the pixel_rate here.
6235 */
6236
6237 if (pipe_config->pch_pfit.enabled) {
6238 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6239 uint32_t pfit_size = pipe_config->pch_pfit.size;
6240
6241 pipe_w = pipe_config->pipe_src_w;
6242 pipe_h = pipe_config->pipe_src_h;
6243
6244 pfit_w = (pfit_size >> 16) & 0xFFFF;
6245 pfit_h = pfit_size & 0xFFFF;
6246 if (pipe_w < pfit_w)
6247 pipe_w = pfit_w;
6248 if (pipe_h < pfit_h)
6249 pipe_h = pfit_h;
6250
6251 if (WARN_ON(!pfit_w || !pfit_h))
6252 return pixel_rate;
6253
6254 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6255 pfit_w * pfit_h);
6256 }
6257
6258 return pixel_rate;
6259}
6260
7ff89ca2 6261static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6262{
7ff89ca2 6263 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6264
7ff89ca2
VS
6265 if (HAS_GMCH_DISPLAY(dev_priv))
6266 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6267 crtc_state->pixel_rate =
6268 crtc_state->base.adjusted_mode.crtc_clock;
6269 else
6270 crtc_state->pixel_rate =
6271 ilk_pipe_pixel_rate(crtc_state);
6272}
34edce2f 6273
7ff89ca2
VS
6274static int intel_crtc_compute_config(struct intel_crtc *crtc,
6275 struct intel_crtc_state *pipe_config)
6276{
6277 struct drm_device *dev = crtc->base.dev;
6278 struct drm_i915_private *dev_priv = to_i915(dev);
6279 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6280 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6281
7ff89ca2
VS
6282 if (INTEL_GEN(dev_priv) < 4) {
6283 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6284
7ff89ca2
VS
6285 /*
6286 * Enable double wide mode when the dot clock
6287 * is > 90% of the (display) core speed.
6288 */
6289 if (intel_crtc_supports_double_wide(crtc) &&
6290 adjusted_mode->crtc_clock > clock_limit) {
6291 clock_limit = dev_priv->max_dotclk_freq;
6292 pipe_config->double_wide = true;
6293 }
34edce2f
VS
6294 }
6295
7ff89ca2
VS
6296 if (adjusted_mode->crtc_clock > clock_limit) {
6297 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6298 adjusted_mode->crtc_clock, clock_limit,
6299 yesno(pipe_config->double_wide));
6300 return -EINVAL;
6301 }
34edce2f 6302
25edf915
SS
6303 if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
6304 /*
6305 * There is only one pipe CSC unit per pipe, and we need that
6306 * for output conversion from RGB->YCBCR. So if CTM is already
6307 * applied we can't support YCBCR420 output.
6308 */
6309 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6310 return -EINVAL;
6311 }
6312
7ff89ca2
VS
6313 /*
6314 * Pipe horizontal size must be even in:
6315 * - DVO ganged mode
6316 * - LVDS dual channel mode
6317 * - Double wide pipe
6318 */
6319 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6320 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6321 pipe_config->pipe_src_w &= ~1;
34edce2f 6322
7ff89ca2
VS
6323 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6324 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6325 */
6326 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6327 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6328 return -EINVAL;
34edce2f 6329
7ff89ca2 6330 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6331
7ff89ca2
VS
6332 if (HAS_IPS(dev_priv))
6333 hsw_compute_ips_config(crtc, pipe_config);
34edce2f 6334
7ff89ca2
VS
6335 if (pipe_config->has_pch_encoder)
6336 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6337
7ff89ca2 6338 return 0;
34edce2f
VS
6339}
6340
2c07245f 6341static void
a65851af 6342intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6343{
a65851af
VS
6344 while (*num > DATA_LINK_M_N_MASK ||
6345 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6346 *num >>= 1;
6347 *den >>= 1;
6348 }
6349}
6350
a65851af 6351static void compute_m_n(unsigned int m, unsigned int n,
b31e85ed
JN
6352 uint32_t *ret_m, uint32_t *ret_n,
6353 bool reduce_m_n)
a65851af 6354{
9a86cda0
JN
6355 /*
6356 * Reduce M/N as much as possible without loss in precision. Several DP
6357 * dongles in particular seem to be fussy about too large *link* M/N
6358 * values. The passed in values are more likely to have the least
6359 * significant bits zero than M after rounding below, so do this first.
6360 */
b31e85ed
JN
6361 if (reduce_m_n) {
6362 while ((m & 1) == 0 && (n & 1) == 0) {
6363 m >>= 1;
6364 n >>= 1;
6365 }
9a86cda0
JN
6366 }
6367
a65851af
VS
6368 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6369 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6370 intel_reduce_m_n_ratio(ret_m, ret_n);
6371}
6372
e69d0bc1
DV
6373void
6374intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6375 int pixel_clock, int link_clock,
b31e85ed
JN
6376 struct intel_link_m_n *m_n,
6377 bool reduce_m_n)
2c07245f 6378{
e69d0bc1 6379 m_n->tu = 64;
a65851af
VS
6380
6381 compute_m_n(bits_per_pixel * pixel_clock,
6382 link_clock * nlanes * 8,
b31e85ed
JN
6383 &m_n->gmch_m, &m_n->gmch_n,
6384 reduce_m_n);
a65851af
VS
6385
6386 compute_m_n(pixel_clock, link_clock,
b31e85ed
JN
6387 &m_n->link_m, &m_n->link_n,
6388 reduce_m_n);
2c07245f
ZW
6389}
6390
a7615030
CW
6391static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6392{
4f044a88
MW
6393 if (i915_modparams.panel_use_ssc >= 0)
6394 return i915_modparams.panel_use_ssc != 0;
41aa3448 6395 return dev_priv->vbt.lvds_use_ssc
435793df 6396 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6397}
6398
7429e9d4 6399static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6400{
7df00d7a 6401 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6402}
f47709a9 6403
7429e9d4
DV
6404static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6405{
6406 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6407}
6408
f47709a9 6409static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6410 struct intel_crtc_state *crtc_state,
9e2c8475 6411 struct dpll *reduced_clock)
a7516a05 6412{
9b1e14f4 6413 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6414 u32 fp, fp2 = 0;
6415
9b1e14f4 6416 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6417 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6418 if (reduced_clock)
7429e9d4 6419 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6420 } else {
190f68c5 6421 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6422 if (reduced_clock)
7429e9d4 6423 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6424 }
6425
190f68c5 6426 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6427
2d84d2b3 6428 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6429 reduced_clock) {
190f68c5 6430 crtc_state->dpll_hw_state.fp1 = fp2;
a7516a05 6431 } else {
190f68c5 6432 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6433 }
6434}
6435
5e69f97f
CML
6436static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6437 pipe)
89b667f8
JB
6438{
6439 u32 reg_val;
6440
6441 /*
6442 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6443 * and set it to a reasonable value instead.
6444 */
ab3c759a 6445 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6446 reg_val &= 0xffffff00;
6447 reg_val |= 0x00000030;
ab3c759a 6448 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6449
ab3c759a 6450 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
6451 reg_val &= 0x00ffffff;
6452 reg_val |= 0x8c000000;
ab3c759a 6453 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6454
ab3c759a 6455 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6456 reg_val &= 0xffffff00;
ab3c759a 6457 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6458
ab3c759a 6459 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6460 reg_val &= 0x00ffffff;
6461 reg_val |= 0xb0000000;
ab3c759a 6462 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6463}
6464
b551842d
DV
6465static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6466 struct intel_link_m_n *m_n)
6467{
6468 struct drm_device *dev = crtc->base.dev;
fac5e23e 6469 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
DV
6470 int pipe = crtc->pipe;
6471
e3b95f1e
DV
6472 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6473 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6474 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6475 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6476}
6477
6478static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6479 struct intel_link_m_n *m_n,
6480 struct intel_link_m_n *m2_n2)
b551842d 6481{
6315b5d3 6482 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6483 int pipe = crtc->pipe;
6e3c9717 6484 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6485
6315b5d3 6486 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
DV
6487 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6488 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6489 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6490 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6491 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6492 * for gen < 8) and if DRRS is supported (to make sure the
6493 * registers are not unnecessarily accessed).
6494 */
920a14b2
TU
6495 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6496 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6497 I915_WRITE(PIPE_DATA_M2(transcoder),
6498 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6499 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6500 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6501 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6502 }
b551842d 6503 } else {
e3b95f1e
DV
6504 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6505 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6506 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6507 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6508 }
6509}
6510
fe3cd48d 6511void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6512{
fe3cd48d
R
6513 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6514
6515 if (m_n == M1_N1) {
6516 dp_m_n = &crtc->config->dp_m_n;
6517 dp_m2_n2 = &crtc->config->dp_m2_n2;
6518 } else if (m_n == M2_N2) {
6519
6520 /*
6521 * M2_N2 registers are not supported. Hence m2_n2 divider value
6522 * needs to be programmed into M1_N1.
6523 */
6524 dp_m_n = &crtc->config->dp_m2_n2;
6525 } else {
6526 DRM_ERROR("Unsupported divider value\n");
6527 return;
6528 }
6529
6e3c9717
ACO
6530 if (crtc->config->has_pch_encoder)
6531 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6532 else
fe3cd48d 6533 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6534}
6535
251ac862
DV
6536static void vlv_compute_dpll(struct intel_crtc *crtc,
6537 struct intel_crtc_state *pipe_config)
bdd4b6a6 6538{
03ed5cbf 6539 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 6540 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6541 if (crtc->pipe != PIPE_A)
6542 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 6543
cd2d34d9 6544 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6545 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6546 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
6547 DPLL_EXT_BUFFER_ENABLE_VLV;
6548
03ed5cbf
VS
6549 pipe_config->dpll_hw_state.dpll_md =
6550 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6551}
bdd4b6a6 6552
03ed5cbf
VS
6553static void chv_compute_dpll(struct intel_crtc *crtc,
6554 struct intel_crtc_state *pipe_config)
6555{
6556 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 6557 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
6558 if (crtc->pipe != PIPE_A)
6559 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6560
cd2d34d9 6561 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 6562 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
6563 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
6564
03ed5cbf
VS
6565 pipe_config->dpll_hw_state.dpll_md =
6566 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
6567}
6568
d288f65f 6569static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6570 const struct intel_crtc_state *pipe_config)
a0c4da24 6571{
f47709a9 6572 struct drm_device *dev = crtc->base.dev;
fac5e23e 6573 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6574 enum pipe pipe = crtc->pipe;
bdd4b6a6 6575 u32 mdiv;
a0c4da24 6576 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6577 u32 coreclk, reg_val;
a0c4da24 6578
cd2d34d9
VS
6579 /* Enable Refclk */
6580 I915_WRITE(DPLL(pipe),
6581 pipe_config->dpll_hw_state.dpll &
6582 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
6583
6584 /* No need to actually set up the DPLL with DSI */
6585 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6586 return;
6587
a580516d 6588 mutex_lock(&dev_priv->sb_lock);
09153000 6589
d288f65f
VS
6590 bestn = pipe_config->dpll.n;
6591 bestm1 = pipe_config->dpll.m1;
6592 bestm2 = pipe_config->dpll.m2;
6593 bestp1 = pipe_config->dpll.p1;
6594 bestp2 = pipe_config->dpll.p2;
a0c4da24 6595
89b667f8
JB
6596 /* See eDP HDMI DPIO driver vbios notes doc */
6597
6598 /* PLL B needs special handling */
bdd4b6a6 6599 if (pipe == PIPE_B)
5e69f97f 6600 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6601
6602 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6603 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6604
6605 /* Disable target IRef on PLL */
ab3c759a 6606 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6607 reg_val &= 0x00ffffff;
ab3c759a 6608 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6609
6610 /* Disable fast lock */
ab3c759a 6611 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6612
6613 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6614 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6615 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6616 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6617 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6618
6619 /*
6620 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6621 * but we don't support that).
6622 * Note: don't use the DAC post divider as it seems unstable.
6623 */
6624 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6625 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6626
a0c4da24 6627 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6628 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6629
89b667f8 6630 /* Set HBR and RBR LPF coefficients */
d288f65f 6631 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
6632 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
6633 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 6634 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6635 0x009f0003);
89b667f8 6636 else
ab3c759a 6637 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6638 0x00d0000f);
6639
37a5650b 6640 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 6641 /* Use SSC source */
bdd4b6a6 6642 if (pipe == PIPE_A)
ab3c759a 6643 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6644 0x0df40000);
6645 else
ab3c759a 6646 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6647 0x0df70000);
6648 } else { /* HDMI or VGA */
6649 /* Use bend source */
bdd4b6a6 6650 if (pipe == PIPE_A)
ab3c759a 6651 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6652 0x0df70000);
6653 else
ab3c759a 6654 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6655 0x0df40000);
6656 }
a0c4da24 6657
ab3c759a 6658 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6659 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 6660 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 6661 coreclk |= 0x01000000;
ab3c759a 6662 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6663
ab3c759a 6664 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 6665 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
6666}
6667
d288f65f 6668static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6669 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6670{
6671 struct drm_device *dev = crtc->base.dev;
fac5e23e 6672 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 6673 enum pipe pipe = crtc->pipe;
9d556c99 6674 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6675 u32 loopfilter, tribuf_calcntr;
9d556c99 6676 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6677 u32 dpio_val;
9cbe40c1 6678 int vco;
9d556c99 6679
cd2d34d9
VS
6680 /* Enable Refclk and SSC */
6681 I915_WRITE(DPLL(pipe),
6682 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6683
6684 /* No need to actually set up the DPLL with DSI */
6685 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
6686 return;
6687
d288f65f
VS
6688 bestn = pipe_config->dpll.n;
6689 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6690 bestm1 = pipe_config->dpll.m1;
6691 bestm2 = pipe_config->dpll.m2 >> 22;
6692 bestp1 = pipe_config->dpll.p1;
6693 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6694 vco = pipe_config->dpll.vco;
a945ce7e 6695 dpio_val = 0;
9cbe40c1 6696 loopfilter = 0;
9d556c99 6697
a580516d 6698 mutex_lock(&dev_priv->sb_lock);
9d556c99 6699
9d556c99
CML
6700 /* p1 and p2 divider */
6701 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6702 5 << DPIO_CHV_S1_DIV_SHIFT |
6703 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6704 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6705 1 << DPIO_CHV_K_DIV_SHIFT);
6706
6707 /* Feedback post-divider - m2 */
6708 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6709
6710 /* Feedback refclk divider - n and m1 */
6711 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6712 DPIO_CHV_M1_DIV_BY_2 |
6713 1 << DPIO_CHV_N_DIV_SHIFT);
6714
6715 /* M2 fraction division */
25a25dfc 6716 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
6717
6718 /* M2 fraction division enable */
a945ce7e
VP
6719 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6720 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6721 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6722 if (bestm2_frac)
6723 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6724 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 6725
de3a0fde
VP
6726 /* Program digital lock detect threshold */
6727 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6728 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6729 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6730 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6731 if (!bestm2_frac)
6732 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6733 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6734
9d556c99 6735 /* Loop filter */
9cbe40c1
VP
6736 if (vco == 5400000) {
6737 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6738 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6739 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6740 tribuf_calcntr = 0x9;
6741 } else if (vco <= 6200000) {
6742 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6743 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6744 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6745 tribuf_calcntr = 0x9;
6746 } else if (vco <= 6480000) {
6747 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6748 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6749 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6750 tribuf_calcntr = 0x8;
6751 } else {
6752 /* Not supported. Apply the same limits as in the max case */
6753 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6754 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6755 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6756 tribuf_calcntr = 0;
6757 }
9d556c99
CML
6758 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6759
968040b2 6760 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
6761 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6762 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6763 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6764
9d556c99
CML
6765 /* AFC Recal */
6766 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6767 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6768 DPIO_AFC_RECAL);
6769
a580516d 6770 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
6771}
6772
d288f65f
VS
6773/**
6774 * vlv_force_pll_on - forcibly enable just the PLL
6775 * @dev_priv: i915 private structure
6776 * @pipe: pipe PLL to enable
6777 * @dpll: PLL configuration
6778 *
6779 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6780 * in cases where we need the PLL enabled even when @pipe is not going to
6781 * be enabled.
6782 */
30ad9814 6783int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 6784 const struct dpll *dpll)
d288f65f 6785{
b91eb5cc 6786 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
6787 struct intel_crtc_state *pipe_config;
6788
6789 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
6790 if (!pipe_config)
6791 return -ENOMEM;
6792
6793 pipe_config->base.crtc = &crtc->base;
6794 pipe_config->pixel_multiplier = 1;
6795 pipe_config->dpll = *dpll;
d288f65f 6796
30ad9814 6797 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
6798 chv_compute_dpll(crtc, pipe_config);
6799 chv_prepare_pll(crtc, pipe_config);
6800 chv_enable_pll(crtc, pipe_config);
d288f65f 6801 } else {
3f36b937
TU
6802 vlv_compute_dpll(crtc, pipe_config);
6803 vlv_prepare_pll(crtc, pipe_config);
6804 vlv_enable_pll(crtc, pipe_config);
d288f65f 6805 }
3f36b937
TU
6806
6807 kfree(pipe_config);
6808
6809 return 0;
d288f65f
VS
6810}
6811
6812/**
6813 * vlv_force_pll_off - forcibly disable just the PLL
6814 * @dev_priv: i915 private structure
6815 * @pipe: pipe PLL to disable
6816 *
6817 * Disable the PLL for @pipe. To be used in cases where we need
6818 * the PLL enabled even when @pipe is not going to be enabled.
6819 */
30ad9814 6820void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 6821{
30ad9814
VS
6822 if (IS_CHERRYVIEW(dev_priv))
6823 chv_disable_pll(dev_priv, pipe);
d288f65f 6824 else
30ad9814 6825 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
6826}
6827
251ac862
DV
6828static void i9xx_compute_dpll(struct intel_crtc *crtc,
6829 struct intel_crtc_state *crtc_state,
9e2c8475 6830 struct dpll *reduced_clock)
eb1cbe48 6831{
9b1e14f4 6832 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 6833 u32 dpll;
190f68c5 6834 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6835
190f68c5 6836 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6837
eb1cbe48
DV
6838 dpll = DPLL_VGA_MODE_DIS;
6839
2d84d2b3 6840 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
6841 dpll |= DPLLB_MODE_LVDS;
6842 else
6843 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 6844
73f67aa8
JN
6845 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
6846 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 6847 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 6848 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 6849 }
198a037f 6850
3d6e9ee0
VS
6851 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6852 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 6853 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 6854
37a5650b 6855 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 6856 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
6857
6858 /* compute bitmask from p1 value */
9b1e14f4 6859 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
6860 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6861 else {
6862 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 6863 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
6864 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6865 }
6866 switch (clock->p2) {
6867 case 5:
6868 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6869 break;
6870 case 7:
6871 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6872 break;
6873 case 10:
6874 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6875 break;
6876 case 14:
6877 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6878 break;
6879 }
9b1e14f4 6880 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
6881 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6882
190f68c5 6883 if (crtc_state->sdvo_tv_clock)
eb1cbe48 6884 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 6885 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6886 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6887 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6888 else
6889 dpll |= PLL_REF_INPUT_DREFCLK;
6890
6891 dpll |= DPLL_VCO_ENABLE;
190f68c5 6892 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 6893
9b1e14f4 6894 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 6895 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 6896 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 6897 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6898 }
6899}
6900
251ac862
DV
6901static void i8xx_compute_dpll(struct intel_crtc *crtc,
6902 struct intel_crtc_state *crtc_state,
9e2c8475 6903 struct dpll *reduced_clock)
eb1cbe48 6904{
f47709a9 6905 struct drm_device *dev = crtc->base.dev;
fac5e23e 6906 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 6907 u32 dpll;
190f68c5 6908 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 6909
190f68c5 6910 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 6911
eb1cbe48
DV
6912 dpll = DPLL_VGA_MODE_DIS;
6913
2d84d2b3 6914 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6915 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6916 } else {
6917 if (clock->p1 == 2)
6918 dpll |= PLL_P1_DIVIDE_BY_TWO;
6919 else
6920 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6921 if (clock->p2 == 4)
6922 dpll |= PLL_P2_DIVIDE_BY_4;
6923 }
6924
50a0bc90
TU
6925 if (!IS_I830(dev_priv) &&
6926 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
6927 dpll |= DPLL_DVO_2X_MODE;
6928
2d84d2b3 6929 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 6930 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
6931 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6932 else
6933 dpll |= PLL_REF_INPUT_DREFCLK;
6934
6935 dpll |= DPLL_VCO_ENABLE;
190f68c5 6936 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6937}
6938
8a654f3b 6939static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 6940{
6315b5d3 6941 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 6942 enum pipe pipe = intel_crtc->pipe;
6e3c9717 6943 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 6944 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
6945 uint32_t crtc_vtotal, crtc_vblank_end;
6946 int vsyncshift = 0;
4d8a62ea
DV
6947
6948 /* We need to be careful not to changed the adjusted mode, for otherwise
6949 * the hw state checker will get angry at the mismatch. */
6950 crtc_vtotal = adjusted_mode->crtc_vtotal;
6951 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6952
609aeaca 6953 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6954 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6955 crtc_vtotal -= 1;
6956 crtc_vblank_end -= 1;
609aeaca 6957
2d84d2b3 6958 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
6959 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6960 else
6961 vsyncshift = adjusted_mode->crtc_hsync_start -
6962 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6963 if (vsyncshift < 0)
6964 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6965 }
6966
6315b5d3 6967 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 6968 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6969
fe2b8f9d 6970 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6971 (adjusted_mode->crtc_hdisplay - 1) |
6972 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6973 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6974 (adjusted_mode->crtc_hblank_start - 1) |
6975 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6976 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6977 (adjusted_mode->crtc_hsync_start - 1) |
6978 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6979
fe2b8f9d 6980 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6981 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6982 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6983 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6984 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6985 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6986 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6987 (adjusted_mode->crtc_vsync_start - 1) |
6988 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6989
b5e508d4
PZ
6990 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6991 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6992 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6993 * bits. */
772c2a51 6994 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
6995 (pipe == PIPE_B || pipe == PIPE_C))
6996 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6997
bc58be60
JN
6998}
6999
7000static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7001{
7002 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7003 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7004 enum pipe pipe = intel_crtc->pipe;
7005
b0e77b9c
PZ
7006 /* pipesrc controls the size that is scaled from, which should
7007 * always be the user's requested size.
7008 */
7009 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7010 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7011 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7012}
7013
1bd1bd80 7014static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7015 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7016{
7017 struct drm_device *dev = crtc->base.dev;
fac5e23e 7018 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
DV
7019 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7020 uint32_t tmp;
7021
7022 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7023 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7024 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7025 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7026 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7027 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7028 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7029 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7030 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7031
7032 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7033 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7034 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7035 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7036 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7037 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7038 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7039 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7040 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7041
7042 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7043 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7044 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7045 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7046 }
bc58be60
JN
7047}
7048
7049static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7050 struct intel_crtc_state *pipe_config)
7051{
7052 struct drm_device *dev = crtc->base.dev;
fac5e23e 7053 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7054 u32 tmp;
1bd1bd80
DV
7055
7056 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7057 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7058 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7059
2d112de7
ACO
7060 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7061 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7062}
7063
f6a83288 7064void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7065 struct intel_crtc_state *pipe_config)
babea61d 7066{
2d112de7
ACO
7067 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7068 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7069 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7070 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7071
2d112de7
ACO
7072 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7073 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7074 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7075 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7076
2d112de7 7077 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7078 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7079
2d112de7 7080 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7081
7082 mode->hsync = drm_mode_hsync(mode);
7083 mode->vrefresh = drm_mode_vrefresh(mode);
7084 drm_mode_set_name(mode);
babea61d
JB
7085}
7086
84b046f3
DV
7087static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7088{
6315b5d3 7089 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
DV
7090 uint32_t pipeconf;
7091
9f11a9e4 7092 pipeconf = 0;
84b046f3 7093
e56134bc
VS
7094 /* we keep both pipes enabled on 830 */
7095 if (IS_I830(dev_priv))
b6b5d049 7096 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7097
6e3c9717 7098 if (intel_crtc->config->double_wide)
cf532bb2 7099 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7100
ff9ce46e 7101 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7102 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7103 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7104 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7105 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7106 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7107 PIPECONF_DITHER_TYPE_SP;
84b046f3 7108
6e3c9717 7109 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7110 case 18:
7111 pipeconf |= PIPECONF_6BPC;
7112 break;
7113 case 24:
7114 pipeconf |= PIPECONF_8BPC;
7115 break;
7116 case 30:
7117 pipeconf |= PIPECONF_10BPC;
7118 break;
7119 default:
7120 /* Case prevented by intel_choose_pipe_bpp_dither. */
7121 BUG();
84b046f3
DV
7122 }
7123 }
7124
6e3c9717 7125 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7126 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7127 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7128 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7129 else
7130 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7131 } else
84b046f3
DV
7132 pipeconf |= PIPECONF_PROGRESSIVE;
7133
920a14b2 7134 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7135 intel_crtc->config->limited_color_range)
9f11a9e4 7136 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7137
84b046f3
DV
7138 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7139 POSTING_READ(PIPECONF(intel_crtc->pipe));
7140}
7141
81c97f52
ACO
7142static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7143 struct intel_crtc_state *crtc_state)
7144{
7145 struct drm_device *dev = crtc->base.dev;
fac5e23e 7146 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7147 const struct intel_limit *limit;
81c97f52
ACO
7148 int refclk = 48000;
7149
7150 memset(&crtc_state->dpll_hw_state, 0,
7151 sizeof(crtc_state->dpll_hw_state));
7152
2d84d2b3 7153 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7154 if (intel_panel_use_ssc(dev_priv)) {
7155 refclk = dev_priv->vbt.lvds_ssc_freq;
7156 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7157 }
7158
7159 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7160 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7161 limit = &intel_limits_i8xx_dvo;
7162 } else {
7163 limit = &intel_limits_i8xx_dac;
7164 }
7165
7166 if (!crtc_state->clock_set &&
7167 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7168 refclk, NULL, &crtc_state->dpll)) {
7169 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7170 return -EINVAL;
7171 }
7172
7173 i8xx_compute_dpll(crtc, crtc_state, NULL);
7174
7175 return 0;
7176}
7177
19ec6693
ACO
7178static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7179 struct intel_crtc_state *crtc_state)
7180{
7181 struct drm_device *dev = crtc->base.dev;
fac5e23e 7182 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7183 const struct intel_limit *limit;
19ec6693
ACO
7184 int refclk = 96000;
7185
7186 memset(&crtc_state->dpll_hw_state, 0,
7187 sizeof(crtc_state->dpll_hw_state));
7188
2d84d2b3 7189 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7190 if (intel_panel_use_ssc(dev_priv)) {
7191 refclk = dev_priv->vbt.lvds_ssc_freq;
7192 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7193 }
7194
7195 if (intel_is_dual_link_lvds(dev))
7196 limit = &intel_limits_g4x_dual_channel_lvds;
7197 else
7198 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7199 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7200 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7201 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7202 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7203 limit = &intel_limits_g4x_sdvo;
7204 } else {
7205 /* The option is for other outputs */
7206 limit = &intel_limits_i9xx_sdvo;
7207 }
7208
7209 if (!crtc_state->clock_set &&
7210 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7211 refclk, NULL, &crtc_state->dpll)) {
7212 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7213 return -EINVAL;
7214 }
7215
7216 i9xx_compute_dpll(crtc, crtc_state, NULL);
7217
7218 return 0;
7219}
7220
70e8aa21
ACO
7221static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7222 struct intel_crtc_state *crtc_state)
7223{
7224 struct drm_device *dev = crtc->base.dev;
fac5e23e 7225 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7226 const struct intel_limit *limit;
70e8aa21
ACO
7227 int refclk = 96000;
7228
7229 memset(&crtc_state->dpll_hw_state, 0,
7230 sizeof(crtc_state->dpll_hw_state));
7231
2d84d2b3 7232 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7233 if (intel_panel_use_ssc(dev_priv)) {
7234 refclk = dev_priv->vbt.lvds_ssc_freq;
7235 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7236 }
7237
7238 limit = &intel_limits_pineview_lvds;
7239 } else {
7240 limit = &intel_limits_pineview_sdvo;
7241 }
7242
7243 if (!crtc_state->clock_set &&
7244 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7245 refclk, NULL, &crtc_state->dpll)) {
7246 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7247 return -EINVAL;
7248 }
7249
7250 i9xx_compute_dpll(crtc, crtc_state, NULL);
7251
7252 return 0;
7253}
7254
190f68c5
ACO
7255static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7256 struct intel_crtc_state *crtc_state)
79e53945 7257{
c7653199 7258 struct drm_device *dev = crtc->base.dev;
fac5e23e 7259 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7260 const struct intel_limit *limit;
81c97f52 7261 int refclk = 96000;
79e53945 7262
dd3cd74a
ACO
7263 memset(&crtc_state->dpll_hw_state, 0,
7264 sizeof(crtc_state->dpll_hw_state));
7265
2d84d2b3 7266 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7267 if (intel_panel_use_ssc(dev_priv)) {
7268 refclk = dev_priv->vbt.lvds_ssc_freq;
7269 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7270 }
43565a06 7271
70e8aa21
ACO
7272 limit = &intel_limits_i9xx_lvds;
7273 } else {
7274 limit = &intel_limits_i9xx_sdvo;
81c97f52 7275 }
79e53945 7276
70e8aa21
ACO
7277 if (!crtc_state->clock_set &&
7278 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7279 refclk, NULL, &crtc_state->dpll)) {
7280 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7281 return -EINVAL;
f47709a9 7282 }
7026d4ac 7283
81c97f52 7284 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7285
c8f7a0db 7286 return 0;
f564048e
EA
7287}
7288
65b3d6a9
ACO
7289static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7290 struct intel_crtc_state *crtc_state)
7291{
7292 int refclk = 100000;
1b6f4958 7293 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7294
7295 memset(&crtc_state->dpll_hw_state, 0,
7296 sizeof(crtc_state->dpll_hw_state));
7297
65b3d6a9
ACO
7298 if (!crtc_state->clock_set &&
7299 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7300 refclk, NULL, &crtc_state->dpll)) {
7301 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7302 return -EINVAL;
7303 }
7304
7305 chv_compute_dpll(crtc, crtc_state);
7306
7307 return 0;
7308}
7309
7310static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7311 struct intel_crtc_state *crtc_state)
7312{
7313 int refclk = 100000;
1b6f4958 7314 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7315
7316 memset(&crtc_state->dpll_hw_state, 0,
7317 sizeof(crtc_state->dpll_hw_state));
7318
65b3d6a9
ACO
7319 if (!crtc_state->clock_set &&
7320 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7321 refclk, NULL, &crtc_state->dpll)) {
7322 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7323 return -EINVAL;
7324 }
7325
7326 vlv_compute_dpll(crtc, crtc_state);
7327
7328 return 0;
7329}
7330
2fa2fe9a 7331static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7332 struct intel_crtc_state *pipe_config)
2fa2fe9a 7333{
6315b5d3 7334 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
DV
7335 uint32_t tmp;
7336
50a0bc90
TU
7337 if (INTEL_GEN(dev_priv) <= 3 &&
7338 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7339 return;
7340
2fa2fe9a 7341 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7342 if (!(tmp & PFIT_ENABLE))
7343 return;
2fa2fe9a 7344
06922821 7345 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7346 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
7347 if (crtc->pipe != PIPE_B)
7348 return;
2fa2fe9a
DV
7349 } else {
7350 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7351 return;
7352 }
7353
06922821 7354 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7355 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
DV
7356}
7357
acbec814 7358static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7359 struct intel_crtc_state *pipe_config)
acbec814
JB
7360{
7361 struct drm_device *dev = crtc->base.dev;
fac5e23e 7362 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7363 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7364 struct dpll clock;
acbec814 7365 u32 mdiv;
662c6ecb 7366 int refclk = 100000;
acbec814 7367
b521973b
VS
7368 /* In case of DSI, DPLL will not be used */
7369 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7370 return;
7371
a580516d 7372 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7373 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7374 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7375
7376 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7377 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7378 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7379 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7380 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7381
dccbea3b 7382 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7383}
7384
5724dbd1
DL
7385static void
7386i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7387 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7388{
7389 struct drm_device *dev = crtc->base.dev;
fac5e23e 7390 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
7391 u32 val, base, offset;
7392 int pipe = crtc->pipe, plane = crtc->plane;
7393 int fourcc, pixel_format;
6761dd31 7394 unsigned int aligned_height;
b113d5ee 7395 struct drm_framebuffer *fb;
1b842c89 7396 struct intel_framebuffer *intel_fb;
1ad292b5 7397
42a7b088
DL
7398 val = I915_READ(DSPCNTR(plane));
7399 if (!(val & DISPLAY_PLANE_ENABLE))
7400 return;
7401
d9806c9f 7402 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7403 if (!intel_fb) {
1ad292b5
JB
7404 DRM_DEBUG_KMS("failed to alloc fb\n");
7405 return;
7406 }
7407
1b842c89
DL
7408 fb = &intel_fb->base;
7409
d2e9f5fc
VS
7410 fb->dev = dev;
7411
6315b5d3 7412 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7413 if (val & DISPPLANE_TILED) {
49af449b 7414 plane_config->tiling = I915_TILING_X;
bae781b2 7415 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
7416 }
7417 }
1ad292b5
JB
7418
7419 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7420 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7421 fb->format = drm_format_info(fourcc);
1ad292b5 7422
6315b5d3 7423 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7424 if (plane_config->tiling)
1ad292b5
JB
7425 offset = I915_READ(DSPTILEOFF(plane));
7426 else
7427 offset = I915_READ(DSPLINOFF(plane));
7428 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7429 } else {
7430 base = I915_READ(DSPADDR(plane));
7431 }
7432 plane_config->base = base;
7433
7434 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7435 fb->width = ((val >> 16) & 0xfff) + 1;
7436 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7437
7438 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7439 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7440
d88c4afd 7441 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7442
f37b5c2b 7443 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7444
2844a921
DL
7445 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7446 pipe_name(pipe), plane, fb->width, fb->height,
272725c7 7447 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7448 plane_config->size);
1ad292b5 7449
2d14030b 7450 plane_config->fb = intel_fb;
1ad292b5
JB
7451}
7452
70b23a98 7453static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7454 struct intel_crtc_state *pipe_config)
70b23a98
VS
7455{
7456 struct drm_device *dev = crtc->base.dev;
fac5e23e 7457 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7458 int pipe = pipe_config->cpu_transcoder;
7459 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7460 struct dpll clock;
0d7b6b11 7461 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7462 int refclk = 100000;
7463
b521973b
VS
7464 /* In case of DSI, DPLL will not be used */
7465 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7466 return;
7467
a580516d 7468 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7469 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7470 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7471 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7472 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7473 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7474 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7475
7476 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7477 clock.m2 = (pll_dw0 & 0xff) << 22;
7478 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7479 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7480 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7481 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7482 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7483
dccbea3b 7484 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7485}
7486
0e8ffe1b 7487static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7488 struct intel_crtc_state *pipe_config)
0e8ffe1b 7489{
6315b5d3 7490 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7491 enum intel_display_power_domain power_domain;
0e8ffe1b 7492 uint32_t tmp;
1729050e 7493 bool ret;
0e8ffe1b 7494
1729050e
ID
7495 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7496 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7497 return false;
7498
e143a21c 7499 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7500 pipe_config->shared_dpll = NULL;
eccb140b 7501
1729050e
ID
7502 ret = false;
7503
0e8ffe1b
DV
7504 tmp = I915_READ(PIPECONF(crtc->pipe));
7505 if (!(tmp & PIPECONF_ENABLE))
1729050e 7506 goto out;
0e8ffe1b 7507
9beb5fea
TU
7508 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7509 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7510 switch (tmp & PIPECONF_BPC_MASK) {
7511 case PIPECONF_6BPC:
7512 pipe_config->pipe_bpp = 18;
7513 break;
7514 case PIPECONF_8BPC:
7515 pipe_config->pipe_bpp = 24;
7516 break;
7517 case PIPECONF_10BPC:
7518 pipe_config->pipe_bpp = 30;
7519 break;
7520 default:
7521 break;
7522 }
7523 }
7524
920a14b2 7525 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7526 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
7527 pipe_config->limited_color_range = true;
7528
6315b5d3 7529 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
7530 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7531
1bd1bd80 7532 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 7533 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 7534
2fa2fe9a
DV
7535 i9xx_get_pfit_config(crtc, pipe_config);
7536
6315b5d3 7537 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 7538 /* No way to read it out on pipes B and C */
920a14b2 7539 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
7540 tmp = dev_priv->chv_dpll_md[crtc->pipe];
7541 else
7542 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
DV
7543 pipe_config->pixel_multiplier =
7544 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7545 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7546 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 7547 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 7548 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
DV
7549 tmp = I915_READ(DPLL(crtc->pipe));
7550 pipe_config->pixel_multiplier =
7551 ((tmp & SDVO_MULTIPLIER_MASK)
7552 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7553 } else {
7554 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7555 * port and will be fixed up in the encoder->get_config
7556 * function. */
7557 pipe_config->pixel_multiplier = 1;
7558 }
8bcc2795 7559 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 7560 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
7561 /*
7562 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7563 * on 830. Filter it out here so that we don't
7564 * report errors due to that.
7565 */
50a0bc90 7566 if (IS_I830(dev_priv))
1c4e0274
VS
7567 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7568
8bcc2795
DV
7569 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7570 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7571 } else {
7572 /* Mask out read-only status bits. */
7573 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7574 DPLL_PORTC_READY_MASK |
7575 DPLL_PORTB_READY_MASK);
8bcc2795 7576 }
6c49f241 7577
920a14b2 7578 if (IS_CHERRYVIEW(dev_priv))
70b23a98 7579 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 7580 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
7581 vlv_crtc_clock_get(crtc, pipe_config);
7582 else
7583 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7584
0f64614d
VS
7585 /*
7586 * Normally the dotclock is filled in by the encoder .get_config()
7587 * but in case the pipe is enabled w/o any ports we need a sane
7588 * default.
7589 */
7590 pipe_config->base.adjusted_mode.crtc_clock =
7591 pipe_config->port_clock / pipe_config->pixel_multiplier;
7592
1729050e
ID
7593 ret = true;
7594
7595out:
7596 intel_display_power_put(dev_priv, power_domain);
7597
7598 return ret;
0e8ffe1b
DV
7599}
7600
c39055b0 7601static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 7602{
13d83a67 7603 struct intel_encoder *encoder;
1c1a24d2 7604 int i;
74cfd7ac 7605 u32 val, final;
13d83a67 7606 bool has_lvds = false;
199e5d79 7607 bool has_cpu_edp = false;
199e5d79 7608 bool has_panel = false;
99eb6a01
KP
7609 bool has_ck505 = false;
7610 bool can_ssc = false;
1c1a24d2 7611 bool using_ssc_source = false;
13d83a67
JB
7612
7613 /* We need to take the global config into account */
c39055b0 7614 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
7615 switch (encoder->type) {
7616 case INTEL_OUTPUT_LVDS:
7617 has_panel = true;
7618 has_lvds = true;
7619 break;
7620 case INTEL_OUTPUT_EDP:
7621 has_panel = true;
2de6905f 7622 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7623 has_cpu_edp = true;
7624 break;
6847d71b
PZ
7625 default:
7626 break;
13d83a67
JB
7627 }
7628 }
7629
6e266956 7630 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 7631 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7632 can_ssc = has_ck505;
7633 } else {
7634 has_ck505 = false;
7635 can_ssc = true;
7636 }
7637
1c1a24d2
L
7638 /* Check if any DPLLs are using the SSC source */
7639 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
7640 u32 temp = I915_READ(PCH_DPLL(i));
7641
7642 if (!(temp & DPLL_VCO_ENABLE))
7643 continue;
7644
7645 if ((temp & PLL_REF_INPUT_MASK) ==
7646 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
7647 using_ssc_source = true;
7648 break;
7649 }
7650 }
7651
7652 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
7653 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
7654
7655 /* Ironlake: try to setup display ref clock before DPLL
7656 * enabling. This is only under driver's control after
7657 * PCH B stepping, previous chipset stepping should be
7658 * ignoring this setting.
7659 */
74cfd7ac
CW
7660 val = I915_READ(PCH_DREF_CONTROL);
7661
7662 /* As we must carefully and slowly disable/enable each source in turn,
7663 * compute the final state we want first and check if we need to
7664 * make any changes at all.
7665 */
7666 final = val;
7667 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7668 if (has_ck505)
7669 final |= DREF_NONSPREAD_CK505_ENABLE;
7670 else
7671 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7672
8c07eb68 7673 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 7674 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 7675 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
7676
7677 if (has_panel) {
7678 final |= DREF_SSC_SOURCE_ENABLE;
7679
7680 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7681 final |= DREF_SSC1_ENABLE;
7682
7683 if (has_cpu_edp) {
7684 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7685 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7686 else
7687 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7688 } else
7689 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
7690 } else if (using_ssc_source) {
7691 final |= DREF_SSC_SOURCE_ENABLE;
7692 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
7693 }
7694
7695 if (final == val)
7696 return;
7697
13d83a67 7698 /* Always enable nonspread source */
74cfd7ac 7699 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7700
99eb6a01 7701 if (has_ck505)
74cfd7ac 7702 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7703 else
74cfd7ac 7704 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7705
199e5d79 7706 if (has_panel) {
74cfd7ac
CW
7707 val &= ~DREF_SSC_SOURCE_MASK;
7708 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7709
199e5d79 7710 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7711 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7712 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7713 val |= DREF_SSC1_ENABLE;
e77166b5 7714 } else
74cfd7ac 7715 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7716
7717 /* Get SSC going before enabling the outputs */
74cfd7ac 7718 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7719 POSTING_READ(PCH_DREF_CONTROL);
7720 udelay(200);
7721
74cfd7ac 7722 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7723
7724 /* Enable CPU source on CPU attached eDP */
199e5d79 7725 if (has_cpu_edp) {
99eb6a01 7726 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7727 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7728 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7729 } else
74cfd7ac 7730 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7731 } else
74cfd7ac 7732 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7733
74cfd7ac 7734 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7735 POSTING_READ(PCH_DREF_CONTROL);
7736 udelay(200);
7737 } else {
1c1a24d2 7738 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 7739
74cfd7ac 7740 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7741
7742 /* Turn off CPU output */
74cfd7ac 7743 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7744
74cfd7ac 7745 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7746 POSTING_READ(PCH_DREF_CONTROL);
7747 udelay(200);
7748
1c1a24d2
L
7749 if (!using_ssc_source) {
7750 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 7751
1c1a24d2
L
7752 /* Turn off the SSC source */
7753 val &= ~DREF_SSC_SOURCE_MASK;
7754 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 7755
1c1a24d2
L
7756 /* Turn off SSC1 */
7757 val &= ~DREF_SSC1_ENABLE;
7758
7759 I915_WRITE(PCH_DREF_CONTROL, val);
7760 POSTING_READ(PCH_DREF_CONTROL);
7761 udelay(200);
7762 }
13d83a67 7763 }
74cfd7ac
CW
7764
7765 BUG_ON(val != final);
13d83a67
JB
7766}
7767
f31f2d55 7768static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7769{
f31f2d55 7770 uint32_t tmp;
dde86e2d 7771
0ff066a9
PZ
7772 tmp = I915_READ(SOUTH_CHICKEN2);
7773 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7774 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7775
cf3598c2
ID
7776 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
7777 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 7778 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7779
0ff066a9
PZ
7780 tmp = I915_READ(SOUTH_CHICKEN2);
7781 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7782 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7783
cf3598c2
ID
7784 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
7785 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 7786 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7787}
7788
7789/* WaMPhyProgramming:hsw */
7790static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7791{
7792 uint32_t tmp;
dde86e2d
PZ
7793
7794 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7795 tmp &= ~(0xFF << 24);
7796 tmp |= (0x12 << 24);
7797 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7798
dde86e2d
PZ
7799 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7800 tmp |= (1 << 11);
7801 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7802
7803 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7804 tmp |= (1 << 11);
7805 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7806
dde86e2d
PZ
7807 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7808 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7809 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7810
7811 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7812 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7813 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7814
0ff066a9
PZ
7815 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7816 tmp &= ~(7 << 13);
7817 tmp |= (5 << 13);
7818 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7819
0ff066a9
PZ
7820 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7821 tmp &= ~(7 << 13);
7822 tmp |= (5 << 13);
7823 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7824
7825 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7826 tmp &= ~0xFF;
7827 tmp |= 0x1C;
7828 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7829
7830 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7831 tmp &= ~0xFF;
7832 tmp |= 0x1C;
7833 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7834
7835 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7836 tmp &= ~(0xFF << 16);
7837 tmp |= (0x1C << 16);
7838 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7839
7840 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7841 tmp &= ~(0xFF << 16);
7842 tmp |= (0x1C << 16);
7843 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7844
0ff066a9
PZ
7845 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7846 tmp |= (1 << 27);
7847 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 7848
0ff066a9
PZ
7849 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7850 tmp |= (1 << 27);
7851 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 7852
0ff066a9
PZ
7853 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7854 tmp &= ~(0xF << 28);
7855 tmp |= (4 << 28);
7856 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 7857
0ff066a9
PZ
7858 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7859 tmp &= ~(0xF << 28);
7860 tmp |= (4 << 28);
7861 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
7862}
7863
2fa86a1f
PZ
7864/* Implements 3 different sequences from BSpec chapter "Display iCLK
7865 * Programming" based on the parameters passed:
7866 * - Sequence to enable CLKOUT_DP
7867 * - Sequence to enable CLKOUT_DP without spread
7868 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7869 */
c39055b0
ACO
7870static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
7871 bool with_spread, bool with_fdi)
f31f2d55 7872{
2fa86a1f
PZ
7873 uint32_t reg, tmp;
7874
7875 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7876 with_spread = true;
4f8036a2
TU
7877 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
7878 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 7879 with_fdi = false;
f31f2d55 7880
a580516d 7881 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
7882
7883 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7884 tmp &= ~SBI_SSCCTL_DISABLE;
7885 tmp |= SBI_SSCCTL_PATHALT;
7886 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7887
7888 udelay(24);
7889
2fa86a1f
PZ
7890 if (with_spread) {
7891 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7892 tmp &= ~SBI_SSCCTL_PATHALT;
7893 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 7894
2fa86a1f
PZ
7895 if (with_fdi) {
7896 lpt_reset_fdi_mphy(dev_priv);
7897 lpt_program_fdi_mphy(dev_priv);
7898 }
7899 }
dde86e2d 7900
4f8036a2 7901 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
7902 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7903 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7904 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 7905
a580516d 7906 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
7907}
7908
47701c3b 7909/* Sequence to disable CLKOUT_DP */
c39055b0 7910static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 7911{
47701c3b
PZ
7912 uint32_t reg, tmp;
7913
a580516d 7914 mutex_lock(&dev_priv->sb_lock);
47701c3b 7915
4f8036a2 7916 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
7917 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7918 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7919 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7920
7921 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7922 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7923 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7924 tmp |= SBI_SSCCTL_PATHALT;
7925 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7926 udelay(32);
7927 }
7928 tmp |= SBI_SSCCTL_DISABLE;
7929 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7930 }
7931
a580516d 7932 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
7933}
7934
f7be2c21
VS
7935#define BEND_IDX(steps) ((50 + (steps)) / 5)
7936
7937static const uint16_t sscdivintphase[] = {
7938 [BEND_IDX( 50)] = 0x3B23,
7939 [BEND_IDX( 45)] = 0x3B23,
7940 [BEND_IDX( 40)] = 0x3C23,
7941 [BEND_IDX( 35)] = 0x3C23,
7942 [BEND_IDX( 30)] = 0x3D23,
7943 [BEND_IDX( 25)] = 0x3D23,
7944 [BEND_IDX( 20)] = 0x3E23,
7945 [BEND_IDX( 15)] = 0x3E23,
7946 [BEND_IDX( 10)] = 0x3F23,
7947 [BEND_IDX( 5)] = 0x3F23,
7948 [BEND_IDX( 0)] = 0x0025,
7949 [BEND_IDX( -5)] = 0x0025,
7950 [BEND_IDX(-10)] = 0x0125,
7951 [BEND_IDX(-15)] = 0x0125,
7952 [BEND_IDX(-20)] = 0x0225,
7953 [BEND_IDX(-25)] = 0x0225,
7954 [BEND_IDX(-30)] = 0x0325,
7955 [BEND_IDX(-35)] = 0x0325,
7956 [BEND_IDX(-40)] = 0x0425,
7957 [BEND_IDX(-45)] = 0x0425,
7958 [BEND_IDX(-50)] = 0x0525,
7959};
7960
7961/*
7962 * Bend CLKOUT_DP
7963 * steps -50 to 50 inclusive, in steps of 5
7964 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
7965 * change in clock period = -(steps / 10) * 5.787 ps
7966 */
7967static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
7968{
7969 uint32_t tmp;
7970 int idx = BEND_IDX(steps);
7971
7972 if (WARN_ON(steps % 5 != 0))
7973 return;
7974
7975 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
7976 return;
7977
7978 mutex_lock(&dev_priv->sb_lock);
7979
7980 if (steps % 10 != 0)
7981 tmp = 0xAAAAAAAB;
7982 else
7983 tmp = 0x00000000;
7984 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
7985
7986 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
7987 tmp &= 0xffff0000;
7988 tmp |= sscdivintphase[idx];
7989 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
7990
7991 mutex_unlock(&dev_priv->sb_lock);
7992}
7993
7994#undef BEND_IDX
7995
c39055b0 7996static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 7997{
bf8fa3d3
PZ
7998 struct intel_encoder *encoder;
7999 bool has_vga = false;
8000
c39055b0 8001 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
8002 switch (encoder->type) {
8003 case INTEL_OUTPUT_ANALOG:
8004 has_vga = true;
8005 break;
6847d71b
PZ
8006 default:
8007 break;
bf8fa3d3
PZ
8008 }
8009 }
8010
f7be2c21 8011 if (has_vga) {
c39055b0
ACO
8012 lpt_bend_clkout_dp(dev_priv, 0);
8013 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 8014 } else {
c39055b0 8015 lpt_disable_clkout_dp(dev_priv);
f7be2c21 8016 }
bf8fa3d3
PZ
8017}
8018
dde86e2d
PZ
8019/*
8020 * Initialize reference clocks when the driver loads
8021 */
c39055b0 8022void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8023{
6e266956 8024 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8025 ironlake_init_pch_refclk(dev_priv);
6e266956 8026 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8027 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8028}
8029
6ff93609 8030static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8031{
fac5e23e 8032 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8034 int pipe = intel_crtc->pipe;
c8203565
PZ
8035 uint32_t val;
8036
78114071 8037 val = 0;
c8203565 8038
6e3c9717 8039 switch (intel_crtc->config->pipe_bpp) {
c8203565 8040 case 18:
dfd07d72 8041 val |= PIPECONF_6BPC;
c8203565
PZ
8042 break;
8043 case 24:
dfd07d72 8044 val |= PIPECONF_8BPC;
c8203565
PZ
8045 break;
8046 case 30:
dfd07d72 8047 val |= PIPECONF_10BPC;
c8203565
PZ
8048 break;
8049 case 36:
dfd07d72 8050 val |= PIPECONF_12BPC;
c8203565
PZ
8051 break;
8052 default:
cc769b62
PZ
8053 /* Case prevented by intel_choose_pipe_bpp_dither. */
8054 BUG();
c8203565
PZ
8055 }
8056
6e3c9717 8057 if (intel_crtc->config->dither)
c8203565
PZ
8058 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8059
6e3c9717 8060 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8061 val |= PIPECONF_INTERLACED_ILK;
8062 else
8063 val |= PIPECONF_PROGRESSIVE;
8064
6e3c9717 8065 if (intel_crtc->config->limited_color_range)
3685a8f3 8066 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8067
c8203565
PZ
8068 I915_WRITE(PIPECONF(pipe), val);
8069 POSTING_READ(PIPECONF(pipe));
8070}
8071
6ff93609 8072static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8073{
fac5e23e 8074 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8076 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8077 u32 val = 0;
ee2b0b38 8078
391bf048 8079 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8080 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8081
6e3c9717 8082 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8083 val |= PIPECONF_INTERLACED_ILK;
8084 else
8085 val |= PIPECONF_PROGRESSIVE;
8086
702e7a56
PZ
8087 I915_WRITE(PIPECONF(cpu_transcoder), val);
8088 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8089}
8090
391bf048
JN
8091static void haswell_set_pipemisc(struct drm_crtc *crtc)
8092{
fac5e23e 8093 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b22ca995 8095 struct intel_crtc_state *config = intel_crtc->config;
756f85cf 8096
391bf048
JN
8097 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8098 u32 val = 0;
756f85cf 8099
6e3c9717 8100 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8101 case 18:
8102 val |= PIPEMISC_DITHER_6_BPC;
8103 break;
8104 case 24:
8105 val |= PIPEMISC_DITHER_8_BPC;
8106 break;
8107 case 30:
8108 val |= PIPEMISC_DITHER_10_BPC;
8109 break;
8110 case 36:
8111 val |= PIPEMISC_DITHER_12_BPC;
8112 break;
8113 default:
8114 /* Case prevented by pipe_config_set_bpp. */
8115 BUG();
8116 }
8117
6e3c9717 8118 if (intel_crtc->config->dither)
756f85cf
PZ
8119 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8120
b22ca995
SS
8121 if (config->ycbcr420) {
8122 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
8123 PIPEMISC_YUV420_ENABLE |
8124 PIPEMISC_YUV420_MODE_FULL_BLEND;
8125 }
8126
391bf048 8127 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8128 }
ee2b0b38
PZ
8129}
8130
d4b1931c
PZ
8131int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8132{
8133 /*
8134 * Account for spread spectrum to avoid
8135 * oversubscribing the link. Max center spread
8136 * is 2.5%; use 5% for safety's sake.
8137 */
8138 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8139 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8140}
8141
7429e9d4 8142static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8143{
7429e9d4 8144 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8145}
8146
b75ca6f6
ACO
8147static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8148 struct intel_crtc_state *crtc_state,
9e2c8475 8149 struct dpll *reduced_clock)
79e53945 8150{
de13a2e3 8151 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8152 struct drm_device *dev = crtc->dev;
fac5e23e 8153 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8154 u32 dpll, fp, fp2;
3d6e9ee0 8155 int factor;
79e53945 8156
c1858123 8157 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8158 factor = 21;
3d6e9ee0 8159 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8160 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8161 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8162 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8163 factor = 25;
190f68c5 8164 } else if (crtc_state->sdvo_tv_clock)
8febb297 8165 factor = 20;
c1858123 8166
b75ca6f6
ACO
8167 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8168
190f68c5 8169 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8170 fp |= FP_CB_TUNE;
8171
8172 if (reduced_clock) {
8173 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8174
b75ca6f6
ACO
8175 if (reduced_clock->m < factor * reduced_clock->n)
8176 fp2 |= FP_CB_TUNE;
8177 } else {
8178 fp2 = fp;
8179 }
9a7c7890 8180
5eddb70b 8181 dpll = 0;
2c07245f 8182
3d6e9ee0 8183 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8184 dpll |= DPLLB_MODE_LVDS;
8185 else
8186 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8187
190f68c5 8188 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8189 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8190
3d6e9ee0
VS
8191 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8192 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8193 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8194
37a5650b 8195 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8196 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8197
7d7f8633
VS
8198 /*
8199 * The high speed IO clock is only really required for
8200 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8201 * possible to share the DPLL between CRT and HDMI. Enabling
8202 * the clock needlessly does no real harm, except use up a
8203 * bit of power potentially.
8204 *
8205 * We'll limit this to IVB with 3 pipes, since it has only two
8206 * DPLLs and so DPLL sharing is the only way to get three pipes
8207 * driving PCH ports at the same time. On SNB we could do this,
8208 * and potentially avoid enabling the second DPLL, but it's not
8209 * clear if it''s a win or loss power wise. No point in doing
8210 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8211 */
8212 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8213 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8214 dpll |= DPLL_SDVO_HIGH_SPEED;
8215
a07d6787 8216 /* compute bitmask from p1 value */
190f68c5 8217 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8218 /* also FPA1 */
190f68c5 8219 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8220
190f68c5 8221 switch (crtc_state->dpll.p2) {
a07d6787
EA
8222 case 5:
8223 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8224 break;
8225 case 7:
8226 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8227 break;
8228 case 10:
8229 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8230 break;
8231 case 14:
8232 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8233 break;
79e53945
JB
8234 }
8235
3d6e9ee0
VS
8236 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8237 intel_panel_use_ssc(dev_priv))
43565a06 8238 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8239 else
8240 dpll |= PLL_REF_INPUT_DREFCLK;
8241
b75ca6f6
ACO
8242 dpll |= DPLL_VCO_ENABLE;
8243
8244 crtc_state->dpll_hw_state.dpll = dpll;
8245 crtc_state->dpll_hw_state.fp0 = fp;
8246 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8247}
8248
190f68c5
ACO
8249static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8250 struct intel_crtc_state *crtc_state)
de13a2e3 8251{
997c030c 8252 struct drm_device *dev = crtc->base.dev;
fac5e23e 8253 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8254 const struct intel_limit *limit;
997c030c 8255 int refclk = 120000;
de13a2e3 8256
dd3cd74a
ACO
8257 memset(&crtc_state->dpll_hw_state, 0,
8258 sizeof(crtc_state->dpll_hw_state));
8259
ded220e2
ACO
8260 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8261 if (!crtc_state->has_pch_encoder)
8262 return 0;
79e53945 8263
2d84d2b3 8264 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8265 if (intel_panel_use_ssc(dev_priv)) {
8266 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8267 dev_priv->vbt.lvds_ssc_freq);
8268 refclk = dev_priv->vbt.lvds_ssc_freq;
8269 }
8270
8271 if (intel_is_dual_link_lvds(dev)) {
8272 if (refclk == 100000)
8273 limit = &intel_limits_ironlake_dual_lvds_100m;
8274 else
8275 limit = &intel_limits_ironlake_dual_lvds;
8276 } else {
8277 if (refclk == 100000)
8278 limit = &intel_limits_ironlake_single_lvds_100m;
8279 else
8280 limit = &intel_limits_ironlake_single_lvds;
8281 }
8282 } else {
8283 limit = &intel_limits_ironlake_dac;
8284 }
8285
364ee29d 8286 if (!crtc_state->clock_set &&
997c030c
ACO
8287 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8288 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8289 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8290 return -EINVAL;
f47709a9 8291 }
79e53945 8292
cbaa3315 8293 ironlake_compute_dpll(crtc, crtc_state, NULL);
66e985c0 8294
efd38b68 8295 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
ded220e2
ACO
8296 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8297 pipe_name(crtc->pipe));
8298 return -EINVAL;
3fb37703 8299 }
79e53945 8300
c8f7a0db 8301 return 0;
79e53945
JB
8302}
8303
eb14cb74
VS
8304static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8305 struct intel_link_m_n *m_n)
8306{
8307 struct drm_device *dev = crtc->base.dev;
fac5e23e 8308 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8309 enum pipe pipe = crtc->pipe;
8310
8311 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8312 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8313 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8314 & ~TU_SIZE_MASK;
8315 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8316 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8317 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8318}
8319
8320static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8321 enum transcoder transcoder,
b95af8be
VK
8322 struct intel_link_m_n *m_n,
8323 struct intel_link_m_n *m2_n2)
72419203 8324{
6315b5d3 8325 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8326 enum pipe pipe = crtc->pipe;
72419203 8327
6315b5d3 8328 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8329 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8330 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8331 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8332 & ~TU_SIZE_MASK;
8333 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8334 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8335 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8336 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8337 * gen < 8) and if DRRS is supported (to make sure the
8338 * registers are not unnecessarily read).
8339 */
6315b5d3 8340 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8341 crtc->config->has_drrs) {
b95af8be
VK
8342 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8343 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8344 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8345 & ~TU_SIZE_MASK;
8346 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8347 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8348 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8349 }
eb14cb74
VS
8350 } else {
8351 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8352 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8353 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8354 & ~TU_SIZE_MASK;
8355 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8356 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8357 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8358 }
8359}
8360
8361void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8362 struct intel_crtc_state *pipe_config)
eb14cb74 8363{
681a8504 8364 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8365 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8366 else
8367 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8368 &pipe_config->dp_m_n,
8369 &pipe_config->dp_m2_n2);
eb14cb74 8370}
72419203 8371
eb14cb74 8372static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8373 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8374{
8375 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8376 &pipe_config->fdi_m_n, NULL);
72419203
DV
8377}
8378
bd2e244f 8379static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8380 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8381{
8382 struct drm_device *dev = crtc->base.dev;
fac5e23e 8383 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8384 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8385 uint32_t ps_ctrl = 0;
8386 int id = -1;
8387 int i;
bd2e244f 8388
a1b2278e
CK
8389 /* find scaler attached to this pipe */
8390 for (i = 0; i < crtc->num_scalers; i++) {
8391 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8392 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8393 id = i;
8394 pipe_config->pch_pfit.enabled = true;
8395 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8396 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8397 break;
8398 }
8399 }
bd2e244f 8400
a1b2278e
CK
8401 scaler_state->scaler_id = id;
8402 if (id >= 0) {
8403 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8404 } else {
8405 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8406 }
8407}
8408
5724dbd1
DL
8409static void
8410skylake_get_initial_plane_config(struct intel_crtc *crtc,
8411 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8412{
8413 struct drm_device *dev = crtc->base.dev;
fac5e23e 8414 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 8415 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8416 int pipe = crtc->pipe;
8417 int fourcc, pixel_format;
6761dd31 8418 unsigned int aligned_height;
bc8d7dff 8419 struct drm_framebuffer *fb;
1b842c89 8420 struct intel_framebuffer *intel_fb;
bc8d7dff 8421
d9806c9f 8422 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8423 if (!intel_fb) {
bc8d7dff
DL
8424 DRM_DEBUG_KMS("failed to alloc fb\n");
8425 return;
8426 }
8427
1b842c89
DL
8428 fb = &intel_fb->base;
8429
d2e9f5fc
VS
8430 fb->dev = dev;
8431
bc8d7dff 8432 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8433 if (!(val & PLANE_CTL_ENABLE))
8434 goto error;
8435
bc8d7dff
DL
8436 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8437 fourcc = skl_format_to_fourcc(pixel_format,
8438 val & PLANE_CTL_ORDER_RGBX,
8439 val & PLANE_CTL_ALPHA_MASK);
2f3f4763 8440 fb->format = drm_format_info(fourcc);
bc8d7dff 8441
40f46283
DL
8442 tiling = val & PLANE_CTL_TILED_MASK;
8443 switch (tiling) {
8444 case PLANE_CTL_TILED_LINEAR:
2f075565 8445 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
8446 break;
8447 case PLANE_CTL_TILED_X:
8448 plane_config->tiling = I915_TILING_X;
bae781b2 8449 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8450 break;
8451 case PLANE_CTL_TILED_Y:
2e2adb05
VS
8452 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8453 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8454 else
8455 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8456 break;
8457 case PLANE_CTL_TILED_YF:
2e2adb05
VS
8458 if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
8459 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8460 else
8461 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8462 break;
8463 default:
8464 MISSING_CASE(tiling);
8465 goto error;
8466 }
8467
bc8d7dff
DL
8468 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8469 plane_config->base = base;
8470
8471 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8472
8473 val = I915_READ(PLANE_SIZE(pipe, 0));
8474 fb->height = ((val >> 16) & 0xfff) + 1;
8475 fb->width = ((val >> 0) & 0x1fff) + 1;
8476
8477 val = I915_READ(PLANE_STRIDE(pipe, 0));
d88c4afd 8478 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8479 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8480
d88c4afd 8481 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 8482
f37b5c2b 8483 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8484
8485 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8486 pipe_name(pipe), fb->width, fb->height,
272725c7 8487 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
8488 plane_config->size);
8489
2d14030b 8490 plane_config->fb = intel_fb;
bc8d7dff
DL
8491 return;
8492
8493error:
d1a3a036 8494 kfree(intel_fb);
bc8d7dff
DL
8495}
8496
2fa2fe9a 8497static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8498 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8499{
8500 struct drm_device *dev = crtc->base.dev;
fac5e23e 8501 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
DV
8502 uint32_t tmp;
8503
8504 tmp = I915_READ(PF_CTL(crtc->pipe));
8505
8506 if (tmp & PF_ENABLE) {
fd4daa9c 8507 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8508 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8509 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8510
8511 /* We currently do not free assignements of panel fitters on
8512 * ivb/hsw (since we don't use the higher upscaling modes which
8513 * differentiates them) so just WARN about this case for now. */
5db94019 8514 if (IS_GEN7(dev_priv)) {
cb8b2a30
DV
8515 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8516 PF_PIPE_SEL_IVB(crtc->pipe));
8517 }
2fa2fe9a 8518 }
79e53945
JB
8519}
8520
5724dbd1
DL
8521static void
8522ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8523 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8524{
8525 struct drm_device *dev = crtc->base.dev;
fac5e23e 8526 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 8527 u32 val, base, offset;
aeee5a49 8528 int pipe = crtc->pipe;
4c6baa59 8529 int fourcc, pixel_format;
6761dd31 8530 unsigned int aligned_height;
b113d5ee 8531 struct drm_framebuffer *fb;
1b842c89 8532 struct intel_framebuffer *intel_fb;
4c6baa59 8533
42a7b088
DL
8534 val = I915_READ(DSPCNTR(pipe));
8535 if (!(val & DISPLAY_PLANE_ENABLE))
8536 return;
8537
d9806c9f 8538 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8539 if (!intel_fb) {
4c6baa59
JB
8540 DRM_DEBUG_KMS("failed to alloc fb\n");
8541 return;
8542 }
8543
1b842c89
DL
8544 fb = &intel_fb->base;
8545
d2e9f5fc
VS
8546 fb->dev = dev;
8547
6315b5d3 8548 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8549 if (val & DISPPLANE_TILED) {
49af449b 8550 plane_config->tiling = I915_TILING_X;
bae781b2 8551 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
DV
8552 }
8553 }
4c6baa59
JB
8554
8555 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8556 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 8557 fb->format = drm_format_info(fourcc);
4c6baa59 8558
aeee5a49 8559 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 8560 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 8561 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8562 } else {
49af449b 8563 if (plane_config->tiling)
aeee5a49 8564 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8565 else
aeee5a49 8566 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8567 }
8568 plane_config->base = base;
8569
8570 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8571 fb->width = ((val >> 16) & 0xfff) + 1;
8572 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8573
8574 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8575 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8576
d88c4afd 8577 aligned_height = intel_fb_align_height(fb, 0, fb->height);
4c6baa59 8578
f37b5c2b 8579 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8580
2844a921
DL
8581 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8582 pipe_name(pipe), fb->width, fb->height,
272725c7 8583 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 8584 plane_config->size);
b113d5ee 8585
2d14030b 8586 plane_config->fb = intel_fb;
4c6baa59
JB
8587}
8588
0e8ffe1b 8589static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8590 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8591{
8592 struct drm_device *dev = crtc->base.dev;
fac5e23e 8593 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 8594 enum intel_display_power_domain power_domain;
0e8ffe1b 8595 uint32_t tmp;
1729050e 8596 bool ret;
0e8ffe1b 8597
1729050e
ID
8598 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8599 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
8600 return false;
8601
e143a21c 8602 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8603 pipe_config->shared_dpll = NULL;
eccb140b 8604
1729050e 8605 ret = false;
0e8ffe1b
DV
8606 tmp = I915_READ(PIPECONF(crtc->pipe));
8607 if (!(tmp & PIPECONF_ENABLE))
1729050e 8608 goto out;
0e8ffe1b 8609
42571aef
VS
8610 switch (tmp & PIPECONF_BPC_MASK) {
8611 case PIPECONF_6BPC:
8612 pipe_config->pipe_bpp = 18;
8613 break;
8614 case PIPECONF_8BPC:
8615 pipe_config->pipe_bpp = 24;
8616 break;
8617 case PIPECONF_10BPC:
8618 pipe_config->pipe_bpp = 30;
8619 break;
8620 case PIPECONF_12BPC:
8621 pipe_config->pipe_bpp = 36;
8622 break;
8623 default:
8624 break;
8625 }
8626
b5a9fa09
DV
8627 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8628 pipe_config->limited_color_range = true;
8629
ab9412ba 8630 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 8631 struct intel_shared_dpll *pll;
8106ddbd 8632 enum intel_dpll_id pll_id;
66e985c0 8633
88adfff1
DV
8634 pipe_config->has_pch_encoder = true;
8635
627eb5a3
DV
8636 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8637 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8638 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8639
8640 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8641
2d1fe073 8642 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
8643 /*
8644 * The pipe->pch transcoder and pch transcoder->pll
8645 * mapping is fixed.
8646 */
8106ddbd 8647 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8648 } else {
8649 tmp = I915_READ(PCH_DPLL_SEL);
8650 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 8651 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 8652 else
8106ddbd 8653 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 8654 }
66e985c0 8655
8106ddbd
ACO
8656 pipe_config->shared_dpll =
8657 intel_get_shared_dpll_by_id(dev_priv, pll_id);
8658 pll = pipe_config->shared_dpll;
66e985c0 8659
2edd6443
ACO
8660 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
8661 &pipe_config->dpll_hw_state));
c93f54cf
DV
8662
8663 tmp = pipe_config->dpll_hw_state.dpll;
8664 pipe_config->pixel_multiplier =
8665 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8666 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8667
8668 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8669 } else {
8670 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8671 }
8672
1bd1bd80 8673 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8674 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8675
2fa2fe9a
DV
8676 ironlake_get_pfit_config(crtc, pipe_config);
8677
1729050e
ID
8678 ret = true;
8679
8680out:
8681 intel_display_power_put(dev_priv, power_domain);
8682
8683 return ret;
0e8ffe1b
DV
8684}
8685
be256dc7
PZ
8686static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8687{
91c8a326 8688 struct drm_device *dev = &dev_priv->drm;
be256dc7 8689 struct intel_crtc *crtc;
be256dc7 8690
d3fcc808 8691 for_each_intel_crtc(dev, crtc)
e2c719b7 8692 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8693 pipe_name(crtc->pipe));
8694
9c3a16c8
ID
8695 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
8696 "Display power well on\n");
e2c719b7 8697 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
8698 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8699 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 8700 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 8701 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8702 "CPU PWM1 enabled\n");
772c2a51 8703 if (IS_HASWELL(dev_priv))
e2c719b7 8704 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8705 "CPU PWM2 enabled\n");
e2c719b7 8706 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8707 "PCH PWM1 enabled\n");
e2c719b7 8708 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8709 "Utility pin enabled\n");
e2c719b7 8710 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8711
9926ada1
PZ
8712 /*
8713 * In theory we can still leave IRQs enabled, as long as only the HPD
8714 * interrupts remain enabled. We used to check for that, but since it's
8715 * gen-specific and since we only disable LCPLL after we fully disable
8716 * the interrupts, the check below should be enough.
8717 */
e2c719b7 8718 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8719}
8720
9ccd5aeb
PZ
8721static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8722{
772c2a51 8723 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
8724 return I915_READ(D_COMP_HSW);
8725 else
8726 return I915_READ(D_COMP_BDW);
8727}
8728
3c4c9b81
PZ
8729static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8730{
772c2a51 8731 if (IS_HASWELL(dev_priv)) {
9f817501 8732 mutex_lock(&dev_priv->pcu_lock);
3c4c9b81
PZ
8733 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8734 val))
79cf219a 8735 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
9f817501 8736 mutex_unlock(&dev_priv->pcu_lock);
3c4c9b81 8737 } else {
9ccd5aeb
PZ
8738 I915_WRITE(D_COMP_BDW, val);
8739 POSTING_READ(D_COMP_BDW);
3c4c9b81 8740 }
be256dc7
PZ
8741}
8742
8743/*
8744 * This function implements pieces of two sequences from BSpec:
8745 * - Sequence for display software to disable LCPLL
8746 * - Sequence for display software to allow package C8+
8747 * The steps implemented here are just the steps that actually touch the LCPLL
8748 * register. Callers should take care of disabling all the display engine
8749 * functions, doing the mode unset, fixing interrupts, etc.
8750 */
6ff58d53
PZ
8751static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8752 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8753{
8754 uint32_t val;
8755
8756 assert_can_disable_lcpll(dev_priv);
8757
8758 val = I915_READ(LCPLL_CTL);
8759
8760 if (switch_to_fclk) {
8761 val |= LCPLL_CD_SOURCE_FCLK;
8762 I915_WRITE(LCPLL_CTL, val);
8763
f53dd63f
ID
8764 if (wait_for_us(I915_READ(LCPLL_CTL) &
8765 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
8766 DRM_ERROR("Switching to FCLK failed\n");
8767
8768 val = I915_READ(LCPLL_CTL);
8769 }
8770
8771 val |= LCPLL_PLL_DISABLE;
8772 I915_WRITE(LCPLL_CTL, val);
8773 POSTING_READ(LCPLL_CTL);
8774
24d8441d 8775 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
8776 DRM_ERROR("LCPLL still locked\n");
8777
9ccd5aeb 8778 val = hsw_read_dcomp(dev_priv);
be256dc7 8779 val |= D_COMP_COMP_DISABLE;
3c4c9b81 8780 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8781 ndelay(100);
8782
9ccd5aeb
PZ
8783 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8784 1))
be256dc7
PZ
8785 DRM_ERROR("D_COMP RCOMP still in progress\n");
8786
8787 if (allow_power_down) {
8788 val = I915_READ(LCPLL_CTL);
8789 val |= LCPLL_POWER_DOWN_ALLOW;
8790 I915_WRITE(LCPLL_CTL, val);
8791 POSTING_READ(LCPLL_CTL);
8792 }
8793}
8794
8795/*
8796 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8797 * source.
8798 */
6ff58d53 8799static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
8800{
8801 uint32_t val;
8802
8803 val = I915_READ(LCPLL_CTL);
8804
8805 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8806 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8807 return;
8808
a8a8bd54
PZ
8809 /*
8810 * Make sure we're not on PC8 state before disabling PC8, otherwise
8811 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 8812 */
59bad947 8813 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 8814
be256dc7
PZ
8815 if (val & LCPLL_POWER_DOWN_ALLOW) {
8816 val &= ~LCPLL_POWER_DOWN_ALLOW;
8817 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 8818 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
8819 }
8820
9ccd5aeb 8821 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
8822 val |= D_COMP_COMP_FORCE;
8823 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 8824 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
8825
8826 val = I915_READ(LCPLL_CTL);
8827 val &= ~LCPLL_PLL_DISABLE;
8828 I915_WRITE(LCPLL_CTL, val);
8829
93220c08
CW
8830 if (intel_wait_for_register(dev_priv,
8831 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
8832 5))
be256dc7
PZ
8833 DRM_ERROR("LCPLL not locked yet\n");
8834
8835 if (val & LCPLL_CD_SOURCE_FCLK) {
8836 val = I915_READ(LCPLL_CTL);
8837 val &= ~LCPLL_CD_SOURCE_FCLK;
8838 I915_WRITE(LCPLL_CTL, val);
8839
f53dd63f
ID
8840 if (wait_for_us((I915_READ(LCPLL_CTL) &
8841 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
8842 DRM_ERROR("Switching back to LCPLL failed\n");
8843 }
215733fa 8844
59bad947 8845 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 8846 intel_update_cdclk(dev_priv);
be256dc7
PZ
8847}
8848
765dab67
PZ
8849/*
8850 * Package states C8 and deeper are really deep PC states that can only be
8851 * reached when all the devices on the system allow it, so even if the graphics
8852 * device allows PC8+, it doesn't mean the system will actually get to these
8853 * states. Our driver only allows PC8+ when going into runtime PM.
8854 *
8855 * The requirements for PC8+ are that all the outputs are disabled, the power
8856 * well is disabled and most interrupts are disabled, and these are also
8857 * requirements for runtime PM. When these conditions are met, we manually do
8858 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8859 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8860 * hang the machine.
8861 *
8862 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8863 * the state of some registers, so when we come back from PC8+ we need to
8864 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8865 * need to take care of the registers kept by RC6. Notice that this happens even
8866 * if we don't put the device in PCI D3 state (which is what currently happens
8867 * because of the runtime PM support).
8868 *
8869 * For more, read "Display Sequences for Package C8" on the hardware
8870 * documentation.
8871 */
a14cb6fc 8872void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8873{
c67a470b
PZ
8874 uint32_t val;
8875
c67a470b
PZ
8876 DRM_DEBUG_KMS("Enabling package C8+\n");
8877
4f8036a2 8878 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8879 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8880 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8881 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8882 }
8883
c39055b0 8884 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
8885 hsw_disable_lcpll(dev_priv, true, true);
8886}
8887
a14cb6fc 8888void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 8889{
c67a470b
PZ
8890 uint32_t val;
8891
c67a470b
PZ
8892 DRM_DEBUG_KMS("Disabling package C8+\n");
8893
8894 hsw_restore_lcpll(dev_priv);
c39055b0 8895 lpt_init_pch_refclk(dev_priv);
c67a470b 8896
4f8036a2 8897 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
8898 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8899 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8900 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8901 }
c67a470b
PZ
8902}
8903
190f68c5
ACO
8904static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8905 struct intel_crtc_state *crtc_state)
09b4ddf9 8906{
d7edc4e5 8907 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
44a126ba
PZ
8908 struct intel_encoder *encoder =
8909 intel_ddi_get_crtc_new_encoder(crtc_state);
8910
8911 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
8912 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8913 pipe_name(crtc->pipe));
af3997b5 8914 return -EINVAL;
44a126ba 8915 }
af3997b5 8916 }
716c2e55 8917
c8f7a0db 8918 return 0;
79e53945
JB
8919}
8920
8b0f7e06
KM
8921static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
8922 enum port port,
8923 struct intel_crtc_state *pipe_config)
8924{
8925 enum intel_dpll_id id;
8926 u32 temp;
8927
8928 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
dfbd4508 8929 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
8b0f7e06
KM
8930
8931 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
8932 return;
8933
8934 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
8935}
8936
3760b59c
S
8937static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
8938 enum port port,
8939 struct intel_crtc_state *pipe_config)
8940{
8106ddbd
ACO
8941 enum intel_dpll_id id;
8942
3760b59c
S
8943 switch (port) {
8944 case PORT_A:
08250c4b 8945 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
8946 break;
8947 case PORT_B:
08250c4b 8948 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
8949 break;
8950 case PORT_C:
08250c4b 8951 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
8952 break;
8953 default:
8954 DRM_ERROR("Incorrect port type\n");
8106ddbd 8955 return;
3760b59c 8956 }
8106ddbd
ACO
8957
8958 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
8959}
8960
96b7dfb7
S
8961static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8962 enum port port,
5cec258b 8963 struct intel_crtc_state *pipe_config)
96b7dfb7 8964{
8106ddbd 8965 enum intel_dpll_id id;
a3c988ea 8966 u32 temp;
96b7dfb7
S
8967
8968 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 8969 id = temp >> (port * 3 + 1);
96b7dfb7 8970
c856052a 8971 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 8972 return;
8106ddbd
ACO
8973
8974 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
8975}
8976
7d2c8175
DL
8977static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8978 enum port port,
5cec258b 8979 struct intel_crtc_state *pipe_config)
7d2c8175 8980{
8106ddbd 8981 enum intel_dpll_id id;
c856052a 8982 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 8983
c856052a 8984 switch (ddi_pll_sel) {
7d2c8175 8985 case PORT_CLK_SEL_WRPLL1:
8106ddbd 8986 id = DPLL_ID_WRPLL1;
7d2c8175
DL
8987 break;
8988 case PORT_CLK_SEL_WRPLL2:
8106ddbd 8989 id = DPLL_ID_WRPLL2;
7d2c8175 8990 break;
00490c22 8991 case PORT_CLK_SEL_SPLL:
8106ddbd 8992 id = DPLL_ID_SPLL;
79bd23da 8993 break;
9d16da65
ACO
8994 case PORT_CLK_SEL_LCPLL_810:
8995 id = DPLL_ID_LCPLL_810;
8996 break;
8997 case PORT_CLK_SEL_LCPLL_1350:
8998 id = DPLL_ID_LCPLL_1350;
8999 break;
9000 case PORT_CLK_SEL_LCPLL_2700:
9001 id = DPLL_ID_LCPLL_2700;
9002 break;
8106ddbd 9003 default:
c856052a 9004 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
9005 /* fall through */
9006 case PORT_CLK_SEL_NONE:
8106ddbd 9007 return;
7d2c8175 9008 }
8106ddbd
ACO
9009
9010 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9011}
9012
cf30429e
JN
9013static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9014 struct intel_crtc_state *pipe_config,
d8fc70b7 9015 u64 *power_domain_mask)
cf30429e
JN
9016{
9017 struct drm_device *dev = crtc->base.dev;
fac5e23e 9018 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9019 enum intel_display_power_domain power_domain;
9020 u32 tmp;
9021
d9a7bc67
ID
9022 /*
9023 * The pipe->transcoder mapping is fixed with the exception of the eDP
9024 * transcoder handled below.
9025 */
cf30429e
JN
9026 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9027
9028 /*
9029 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9030 * consistency and less surprising code; it's in always on power).
9031 */
9032 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9033 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9034 enum pipe trans_edp_pipe;
9035 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9036 default:
9037 WARN(1, "unknown pipe linked to edp transcoder\n");
9038 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9039 case TRANS_DDI_EDP_INPUT_A_ON:
9040 trans_edp_pipe = PIPE_A;
9041 break;
9042 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9043 trans_edp_pipe = PIPE_B;
9044 break;
9045 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9046 trans_edp_pipe = PIPE_C;
9047 break;
9048 }
9049
9050 if (trans_edp_pipe == crtc->pipe)
9051 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9052 }
9053
9054 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9055 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9056 return false;
d8fc70b7 9057 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9058
9059 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9060
9061 return tmp & PIPECONF_ENABLE;
9062}
9063
4d1de975
JN
9064static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9065 struct intel_crtc_state *pipe_config,
d8fc70b7 9066 u64 *power_domain_mask)
4d1de975
JN
9067{
9068 struct drm_device *dev = crtc->base.dev;
fac5e23e 9069 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9070 enum intel_display_power_domain power_domain;
9071 enum port port;
9072 enum transcoder cpu_transcoder;
9073 u32 tmp;
9074
4d1de975
JN
9075 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9076 if (port == PORT_A)
9077 cpu_transcoder = TRANSCODER_DSI_A;
9078 else
9079 cpu_transcoder = TRANSCODER_DSI_C;
9080
9081 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9082 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9083 continue;
d8fc70b7 9084 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9085
db18b6a6
ID
9086 /*
9087 * The PLL needs to be enabled with a valid divider
9088 * configuration, otherwise accessing DSI registers will hang
9089 * the machine. See BSpec North Display Engine
9090 * registers/MIPI[BXT]. We can break out here early, since we
9091 * need the same DSI PLL to be enabled for both DSI ports.
9092 */
9093 if (!intel_dsi_pll_is_enabled(dev_priv))
9094 break;
9095
4d1de975
JN
9096 /* XXX: this works for video mode only */
9097 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9098 if (!(tmp & DPI_ENABLE))
9099 continue;
9100
9101 tmp = I915_READ(MIPI_CTRL(port));
9102 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9103 continue;
9104
9105 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9106 break;
9107 }
9108
d7edc4e5 9109 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9110}
9111
26804afd 9112static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9113 struct intel_crtc_state *pipe_config)
26804afd 9114{
6315b5d3 9115 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9116 struct intel_shared_dpll *pll;
26804afd
DV
9117 enum port port;
9118 uint32_t tmp;
9119
9120 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9121
9122 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9123
8b0f7e06
KM
9124 if (IS_CANNONLAKE(dev_priv))
9125 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9126 else if (IS_GEN9_BC(dev_priv))
96b7dfb7 9127 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9128 else if (IS_GEN9_LP(dev_priv))
3760b59c 9129 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9130 else
9131 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9132
8106ddbd
ACO
9133 pll = pipe_config->shared_dpll;
9134 if (pll) {
2edd6443
ACO
9135 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9136 &pipe_config->dpll_hw_state));
d452c5b6
DV
9137 }
9138
26804afd
DV
9139 /*
9140 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9141 * DDI E. So just check whether this pipe is wired to DDI E and whether
9142 * the PCH transcoder is on.
9143 */
6315b5d3 9144 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9145 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9146 pipe_config->has_pch_encoder = true;
9147
9148 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9149 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9150 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9151
9152 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9153 }
9154}
9155
0e8ffe1b 9156static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9157 struct intel_crtc_state *pipe_config)
0e8ffe1b 9158{
6315b5d3 9159 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9160 enum intel_display_power_domain power_domain;
d8fc70b7 9161 u64 power_domain_mask;
cf30429e 9162 bool active;
0e8ffe1b 9163
e79dfb51 9164 intel_crtc_init_scalers(crtc, pipe_config);
5fb9dadf 9165
1729050e
ID
9166 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9167 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9168 return false;
d8fc70b7 9169 power_domain_mask = BIT_ULL(power_domain);
1729050e 9170
8106ddbd 9171 pipe_config->shared_dpll = NULL;
c0d43d62 9172
cf30429e 9173 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9174
cc3f90f0 9175 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9176 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9177 WARN_ON(active);
9178 active = true;
4d1de975
JN
9179 }
9180
cf30429e 9181 if (!active)
1729050e 9182 goto out;
0e8ffe1b 9183
d7edc4e5 9184 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9185 haswell_get_ddi_port_state(crtc, pipe_config);
9186 intel_get_pipe_timings(crtc, pipe_config);
9187 }
627eb5a3 9188
bc58be60 9189 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9190
05dc698c
LL
9191 pipe_config->gamma_mode =
9192 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9193
bd30ca2d 9194 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
b22ca995
SS
9195 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
9196 bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
9197
bd30ca2d 9198 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
b22ca995
SS
9199 bool blend_mode_420 = tmp &
9200 PIPEMISC_YUV420_MODE_FULL_BLEND;
9201
9202 pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
9203 if (pipe_config->ycbcr420 != clrspace_yuv ||
9204 pipe_config->ycbcr420 != blend_mode_420)
9205 DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
9206 } else if (clrspace_yuv) {
9207 DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
9208 }
9209 }
9210
1729050e
ID
9211 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9212 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9213 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9214 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9215 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9216 else
1c132b44 9217 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9218 }
88adfff1 9219
772c2a51 9220 if (IS_HASWELL(dev_priv))
e59150dc
JB
9221 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9222 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9223
4d1de975
JN
9224 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9225 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9226 pipe_config->pixel_multiplier =
9227 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9228 } else {
9229 pipe_config->pixel_multiplier = 1;
9230 }
6c49f241 9231
1729050e
ID
9232out:
9233 for_each_power_domain(power_domain, power_domain_mask)
9234 intel_display_power_put(dev_priv, power_domain);
9235
cf30429e 9236 return active;
0e8ffe1b
DV
9237}
9238
cd5dcbf1 9239static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
9240{
9241 struct drm_i915_private *dev_priv =
9242 to_i915(plane_state->base.plane->dev);
9243 const struct drm_framebuffer *fb = plane_state->base.fb;
9244 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9245 u32 base;
9246
9247 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9248 base = obj->phys_handle->busaddr;
9249 else
9250 base = intel_plane_ggtt_offset(plane_state);
9251
1e7b4fd8
VS
9252 base += plane_state->main.offset;
9253
1cecc830
VS
9254 /* ILK+ do this automagically */
9255 if (HAS_GMCH_DISPLAY(dev_priv) &&
a82256bc 9256 plane_state->base.rotation & DRM_MODE_ROTATE_180)
1cecc830
VS
9257 base += (plane_state->base.crtc_h *
9258 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9259
9260 return base;
9261}
9262
ed270223
VS
9263static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9264{
9265 int x = plane_state->base.crtc_x;
9266 int y = plane_state->base.crtc_y;
9267 u32 pos = 0;
9268
9269 if (x < 0) {
9270 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9271 x = -x;
9272 }
9273 pos |= x << CURSOR_X_SHIFT;
9274
9275 if (y < 0) {
9276 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9277 y = -y;
9278 }
9279 pos |= y << CURSOR_Y_SHIFT;
9280
9281 return pos;
9282}
9283
3637ecf0
VS
9284static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9285{
9286 const struct drm_mode_config *config =
9287 &plane_state->base.plane->dev->mode_config;
9288 int width = plane_state->base.crtc_w;
9289 int height = plane_state->base.crtc_h;
9290
9291 return width > 0 && width <= config->cursor_width &&
9292 height > 0 && height <= config->cursor_height;
9293}
9294
659056f2
VS
9295static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9296 struct intel_plane_state *plane_state)
9297{
9298 const struct drm_framebuffer *fb = plane_state->base.fb;
1e7b4fd8
VS
9299 int src_x, src_y;
9300 u32 offset;
659056f2
VS
9301 int ret;
9302
9303 ret = drm_plane_helper_check_state(&plane_state->base,
9304 &plane_state->clip,
9305 DRM_PLANE_HELPER_NO_SCALING,
9306 DRM_PLANE_HELPER_NO_SCALING,
9307 true, true);
9308 if (ret)
9309 return ret;
9310
9311 if (!fb)
9312 return 0;
9313
9314 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9315 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9316 return -EINVAL;
9317 }
9318
1e7b4fd8
VS
9319 src_x = plane_state->base.src_x >> 16;
9320 src_y = plane_state->base.src_y >> 16;
9321
9322 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9323 offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
9324
9325 if (src_x != 0 || src_y != 0) {
9326 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9327 return -EINVAL;
9328 }
9329
9330 plane_state->main.offset = offset;
9331
659056f2
VS
9332 return 0;
9333}
9334
292889e1
VS
9335static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9336 const struct intel_plane_state *plane_state)
9337{
1e1bb871 9338 const struct drm_framebuffer *fb = plane_state->base.fb;
292889e1 9339
292889e1
VS
9340 return CURSOR_ENABLE |
9341 CURSOR_GAMMA_ENABLE |
9342 CURSOR_FORMAT_ARGB |
1e1bb871 9343 CURSOR_STRIDE(fb->pitches[0]);
292889e1
VS
9344}
9345
659056f2
VS
9346static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9347{
659056f2 9348 int width = plane_state->base.crtc_w;
659056f2
VS
9349
9350 /*
9351 * 845g/865g are only limited by the width of their cursors,
9352 * the height is arbitrary up to the precision of the register.
9353 */
3637ecf0 9354 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
9355}
9356
9357static int i845_check_cursor(struct intel_plane *plane,
9358 struct intel_crtc_state *crtc_state,
9359 struct intel_plane_state *plane_state)
9360{
9361 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9362 int ret;
9363
9364 ret = intel_check_cursor(crtc_state, plane_state);
9365 if (ret)
9366 return ret;
9367
9368 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9369 if (!fb)
659056f2
VS
9370 return 0;
9371
9372 /* Check for which cursor types we support */
9373 if (!i845_cursor_size_ok(plane_state)) {
9374 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9375 plane_state->base.crtc_w,
9376 plane_state->base.crtc_h);
9377 return -EINVAL;
9378 }
9379
1e1bb871 9380 switch (fb->pitches[0]) {
292889e1
VS
9381 case 256:
9382 case 512:
9383 case 1024:
9384 case 2048:
9385 break;
1e1bb871
VS
9386 default:
9387 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9388 fb->pitches[0]);
9389 return -EINVAL;
292889e1
VS
9390 }
9391
659056f2
VS
9392 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9393
9394 return 0;
292889e1
VS
9395}
9396
b2d03b0d
VS
9397static void i845_update_cursor(struct intel_plane *plane,
9398 const struct intel_crtc_state *crtc_state,
55a08b3f 9399 const struct intel_plane_state *plane_state)
560b85bb 9400{
cd5dcbf1 9401 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
9402 u32 cntl = 0, base = 0, pos = 0, size = 0;
9403 unsigned long irqflags;
560b85bb 9404
936e71e3 9405 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9406 unsigned int width = plane_state->base.crtc_w;
9407 unsigned int height = plane_state->base.crtc_h;
dc41c154 9408
a0864d59 9409 cntl = plane_state->ctl;
dc41c154 9410 size = (height << 12) | width;
560b85bb 9411
b2d03b0d
VS
9412 base = intel_cursor_base(plane_state);
9413 pos = intel_cursor_position(plane_state);
4b0e333e 9414 }
560b85bb 9415
b2d03b0d 9416 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 9417
e11ffddb
VS
9418 /* On these chipsets we can only modify the base/size/stride
9419 * whilst the cursor is disabled.
9420 */
9421 if (plane->cursor.base != base ||
9422 plane->cursor.size != size ||
9423 plane->cursor.cntl != cntl) {
dd584fc0 9424 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
dd584fc0 9425 I915_WRITE_FW(CURBASE(PIPE_A), base);
dd584fc0 9426 I915_WRITE_FW(CURSIZE, size);
b2d03b0d 9427 I915_WRITE_FW(CURPOS(PIPE_A), pos);
dd584fc0 9428 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
75343a44 9429
e11ffddb
VS
9430 plane->cursor.base = base;
9431 plane->cursor.size = size;
9432 plane->cursor.cntl = cntl;
9433 } else {
9434 I915_WRITE_FW(CURPOS(PIPE_A), pos);
560b85bb 9435 }
e11ffddb 9436
75343a44 9437 POSTING_READ_FW(CURCNTR(PIPE_A));
b2d03b0d
VS
9438
9439 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9440}
9441
9442static void i845_disable_cursor(struct intel_plane *plane,
9443 struct intel_crtc *crtc)
9444{
9445 i845_update_cursor(plane, NULL, NULL);
560b85bb
CW
9446}
9447
d87ce764
VS
9448static bool i845_cursor_get_hw_state(struct intel_plane *plane)
9449{
9450 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9451 enum intel_display_power_domain power_domain;
9452 bool ret;
9453
9454 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9455 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9456 return false;
9457
9458 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9459
9460 intel_display_power_put(dev_priv, power_domain);
9461
9462 return ret;
9463}
9464
292889e1
VS
9465static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
9466 const struct intel_plane_state *plane_state)
9467{
9468 struct drm_i915_private *dev_priv =
9469 to_i915(plane_state->base.plane->dev);
9470 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
292889e1
VS
9471 u32 cntl;
9472
9473 cntl = MCURSOR_GAMMA_ENABLE;
9474
9475 if (HAS_DDI(dev_priv))
9476 cntl |= CURSOR_PIPE_CSC_ENABLE;
9477
d509e28b 9478 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1
VS
9479
9480 switch (plane_state->base.crtc_w) {
9481 case 64:
9482 cntl |= CURSOR_MODE_64_ARGB_AX;
9483 break;
9484 case 128:
9485 cntl |= CURSOR_MODE_128_ARGB_AX;
9486 break;
9487 case 256:
9488 cntl |= CURSOR_MODE_256_ARGB_AX;
9489 break;
9490 default:
9491 MISSING_CASE(plane_state->base.crtc_w);
9492 return 0;
9493 }
9494
c2c446ad 9495 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
292889e1
VS
9496 cntl |= CURSOR_ROTATE_180;
9497
9498 return cntl;
9499}
9500
659056f2 9501static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 9502{
024faac7
VS
9503 struct drm_i915_private *dev_priv =
9504 to_i915(plane_state->base.plane->dev);
659056f2
VS
9505 int width = plane_state->base.crtc_w;
9506 int height = plane_state->base.crtc_h;
4b0e333e 9507
3637ecf0 9508 if (!intel_cursor_size_ok(plane_state))
659056f2 9509 return false;
4398ad45 9510
024faac7
VS
9511 /* Cursor width is limited to a few power-of-two sizes */
9512 switch (width) {
659056f2
VS
9513 case 256:
9514 case 128:
659056f2
VS
9515 case 64:
9516 break;
9517 default:
9518 return false;
65a21cd6 9519 }
4b0e333e 9520
024faac7
VS
9521 /*
9522 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
9523 * height from 8 lines up to the cursor width, when the
9524 * cursor is not rotated. Everything else requires square
9525 * cursors.
9526 */
9527 if (HAS_CUR_FBC(dev_priv) &&
a82256bc 9528 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
9529 if (height < 8 || height > width)
9530 return false;
9531 } else {
9532 if (height != width)
9533 return false;
9534 }
99d1f387 9535
659056f2 9536 return true;
65a21cd6
JB
9537}
9538
659056f2
VS
9539static int i9xx_check_cursor(struct intel_plane *plane,
9540 struct intel_crtc_state *crtc_state,
9541 struct intel_plane_state *plane_state)
cda4b7d3 9542{
659056f2
VS
9543 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9544 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2 9545 enum pipe pipe = plane->pipe;
659056f2 9546 int ret;
cda4b7d3 9547
659056f2
VS
9548 ret = intel_check_cursor(crtc_state, plane_state);
9549 if (ret)
9550 return ret;
cda4b7d3 9551
659056f2 9552 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9553 if (!fb)
659056f2 9554 return 0;
55a08b3f 9555
659056f2
VS
9556 /* Check for which cursor types we support */
9557 if (!i9xx_cursor_size_ok(plane_state)) {
9558 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9559 plane_state->base.crtc_w,
9560 plane_state->base.crtc_h);
9561 return -EINVAL;
cda4b7d3 9562 }
cda4b7d3 9563
1e1bb871
VS
9564 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
9565 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
9566 fb->pitches[0], plane_state->base.crtc_w);
9567 return -EINVAL;
659056f2 9568 }
dd584fc0 9569
659056f2
VS
9570 /*
9571 * There's something wrong with the cursor on CHV pipe C.
9572 * If it straddles the left edge of the screen then
9573 * moving it away from the edge or disabling it often
9574 * results in a pipe underrun, and often that can lead to
9575 * dead pipe (constant underrun reported, and it scans
9576 * out just a solid color). To recover from that, the
9577 * display power well must be turned off and on again.
9578 * Refuse the put the cursor into that compromised position.
9579 */
9580 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
9581 plane_state->base.visible && plane_state->base.crtc_x < 0) {
9582 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
9583 return -EINVAL;
9584 }
5efb3e28 9585
659056f2 9586 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 9587
659056f2 9588 return 0;
cda4b7d3
CW
9589}
9590
b2d03b0d
VS
9591static void i9xx_update_cursor(struct intel_plane *plane,
9592 const struct intel_crtc_state *crtc_state,
55a08b3f 9593 const struct intel_plane_state *plane_state)
dc41c154 9594{
cd5dcbf1
VS
9595 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9596 enum pipe pipe = plane->pipe;
024faac7 9597 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 9598 unsigned long irqflags;
dc41c154 9599
b2d03b0d 9600 if (plane_state && plane_state->base.visible) {
a0864d59 9601 cntl = plane_state->ctl;
dc41c154 9602
024faac7
VS
9603 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
9604 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
dc41c154 9605
b2d03b0d
VS
9606 base = intel_cursor_base(plane_state);
9607 pos = intel_cursor_position(plane_state);
9608 }
9609
9610 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
9611
e11ffddb
VS
9612 /*
9613 * On some platforms writing CURCNTR first will also
9614 * cause CURPOS to be armed by the CURBASE write.
9615 * Without the CURCNTR write the CURPOS write would
8753d2bc
VS
9616 * arm itself. Thus we always start the full update
9617 * with a CURCNTR write.
9618 *
9619 * On other platforms CURPOS always requires the
9620 * CURBASE write to arm the update. Additonally
9621 * a write to any of the cursor register will cancel
9622 * an already armed cursor update. Thus leaving out
9623 * the CURBASE write after CURPOS could lead to a
9624 * cursor that doesn't appear to move, or even change
9625 * shape. Thus we always write CURBASE.
e11ffddb
VS
9626 *
9627 * CURCNTR and CUR_FBC_CTL are always
9628 * armed by the CURBASE write only.
9629 */
9630 if (plane->cursor.base != base ||
9631 plane->cursor.size != fbc_ctl ||
9632 plane->cursor.cntl != cntl) {
dd584fc0 9633 I915_WRITE_FW(CURCNTR(pipe), cntl);
e11ffddb
VS
9634 if (HAS_CUR_FBC(dev_priv))
9635 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
b2d03b0d 9636 I915_WRITE_FW(CURPOS(pipe), pos);
75343a44
VS
9637 I915_WRITE_FW(CURBASE(pipe), base);
9638
e11ffddb
VS
9639 plane->cursor.base = base;
9640 plane->cursor.size = fbc_ctl;
9641 plane->cursor.cntl = cntl;
dc41c154 9642 } else {
e11ffddb 9643 I915_WRITE_FW(CURPOS(pipe), pos);
8753d2bc 9644 I915_WRITE_FW(CURBASE(pipe), base);
dc41c154
VS
9645 }
9646
dd584fc0 9647 POSTING_READ_FW(CURBASE(pipe));
99d1f387 9648
b2d03b0d 9649 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
9650}
9651
b2d03b0d
VS
9652static void i9xx_disable_cursor(struct intel_plane *plane,
9653 struct intel_crtc *crtc)
cda4b7d3 9654{
b2d03b0d 9655 i9xx_update_cursor(plane, NULL, NULL);
dc41c154
VS
9656}
9657
d87ce764
VS
9658static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
9659{
9660 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9661 enum intel_display_power_domain power_domain;
9662 enum pipe pipe = plane->pipe;
9663 bool ret;
9664
9665 /*
9666 * Not 100% correct for planes that can move between pipes,
9667 * but that's only the case for gen2-3 which don't have any
9668 * display power wells.
9669 */
9670 power_domain = POWER_DOMAIN_PIPE(pipe);
9671 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9672 return false;
9673
9674 ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
9675
9676 intel_display_power_put(dev_priv, power_domain);
9677
9678 return ret;
9679}
dc41c154 9680
79e53945 9681/* VESA 640x480x72Hz mode to set on the pipe */
bacdcd55 9682static const struct drm_display_mode load_detect_mode = {
79e53945
JB
9683 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9684 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9685};
9686
a8bb6818 9687struct drm_framebuffer *
24dbf51a
CW
9688intel_framebuffer_create(struct drm_i915_gem_object *obj,
9689 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
9690{
9691 struct intel_framebuffer *intel_fb;
9692 int ret;
9693
9694 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 9695 if (!intel_fb)
d2dff872 9696 return ERR_PTR(-ENOMEM);
d2dff872 9697
24dbf51a 9698 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
9699 if (ret)
9700 goto err;
d2dff872
CW
9701
9702 return &intel_fb->base;
dcb1394e 9703
dd4916c5 9704err:
dd4916c5 9705 kfree(intel_fb);
dd4916c5 9706 return ERR_PTR(ret);
d2dff872
CW
9707}
9708
9709static u32
9710intel_framebuffer_pitch_for_width(int width, int bpp)
9711{
9712 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9713 return ALIGN(pitch, 64);
9714}
9715
9716static u32
bacdcd55 9717intel_framebuffer_size_for_mode(const struct drm_display_mode *mode, int bpp)
d2dff872
CW
9718{
9719 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9720 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9721}
9722
9723static struct drm_framebuffer *
9724intel_framebuffer_create_for_mode(struct drm_device *dev,
bacdcd55 9725 const struct drm_display_mode *mode,
d2dff872
CW
9726 int depth, int bpp)
9727{
dcb1394e 9728 struct drm_framebuffer *fb;
d2dff872 9729 struct drm_i915_gem_object *obj;
0fed39bd 9730 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 9731
12d79d78 9732 obj = i915_gem_object_create(to_i915(dev),
d2dff872 9733 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
9734 if (IS_ERR(obj))
9735 return ERR_CAST(obj);
d2dff872
CW
9736
9737 mode_cmd.width = mode->hdisplay;
9738 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9739 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9740 bpp);
5ca0c34a 9741 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 9742
24dbf51a 9743 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 9744 if (IS_ERR(fb))
f0cd5182 9745 i915_gem_object_put(obj);
dcb1394e
LW
9746
9747 return fb;
d2dff872
CW
9748}
9749
9750static struct drm_framebuffer *
9751mode_fits_in_fbdev(struct drm_device *dev,
bacdcd55 9752 const struct drm_display_mode *mode)
d2dff872 9753{
0695726e 9754#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 9755 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
9756 struct drm_i915_gem_object *obj;
9757 struct drm_framebuffer *fb;
9758
4c0e5528 9759 if (!dev_priv->fbdev)
d2dff872
CW
9760 return NULL;
9761
4c0e5528 9762 if (!dev_priv->fbdev->fb)
d2dff872
CW
9763 return NULL;
9764
4c0e5528
DV
9765 obj = dev_priv->fbdev->fb->obj;
9766 BUG_ON(!obj);
9767
8bcd4553 9768 fb = &dev_priv->fbdev->fb->base;
01f2c773 9769 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
272725c7 9770 fb->format->cpp[0] * 8))
d2dff872
CW
9771 return NULL;
9772
01f2c773 9773 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9774 return NULL;
9775
c3ed1103 9776 drm_framebuffer_get(fb);
d2dff872 9777 return fb;
4520f53a
DV
9778#else
9779 return NULL;
9780#endif
d2dff872
CW
9781}
9782
d3a40d1b
ACO
9783static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9784 struct drm_crtc *crtc,
bacdcd55 9785 const struct drm_display_mode *mode,
d3a40d1b
ACO
9786 struct drm_framebuffer *fb,
9787 int x, int y)
9788{
9789 struct drm_plane_state *plane_state;
9790 int hdisplay, vdisplay;
9791 int ret;
9792
9793 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9794 if (IS_ERR(plane_state))
9795 return PTR_ERR(plane_state);
9796
9797 if (mode)
196cd5d3 9798 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
d3a40d1b
ACO
9799 else
9800 hdisplay = vdisplay = 0;
9801
9802 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9803 if (ret)
9804 return ret;
9805 drm_atomic_set_fb_for_plane(plane_state, fb);
9806 plane_state->crtc_x = 0;
9807 plane_state->crtc_y = 0;
9808 plane_state->crtc_w = hdisplay;
9809 plane_state->crtc_h = vdisplay;
9810 plane_state->src_x = x << 16;
9811 plane_state->src_y = y << 16;
9812 plane_state->src_w = hdisplay << 16;
9813 plane_state->src_h = vdisplay << 16;
9814
9815 return 0;
9816}
9817
6c5ed5ae 9818int intel_get_load_detect_pipe(struct drm_connector *connector,
bacdcd55 9819 const struct drm_display_mode *mode,
6c5ed5ae
ML
9820 struct intel_load_detect_pipe *old,
9821 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9822{
9823 struct intel_crtc *intel_crtc;
d2434ab7
DV
9824 struct intel_encoder *intel_encoder =
9825 intel_attached_encoder(connector);
79e53945 9826 struct drm_crtc *possible_crtc;
4ef69c7a 9827 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9828 struct drm_crtc *crtc = NULL;
9829 struct drm_device *dev = encoder->dev;
0f0f74bc 9830 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 9831 struct drm_framebuffer *fb;
51fd371b 9832 struct drm_mode_config *config = &dev->mode_config;
edde3617 9833 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 9834 struct drm_connector_state *connector_state;
4be07317 9835 struct intel_crtc_state *crtc_state;
51fd371b 9836 int ret, i = -1;
79e53945 9837
d2dff872 9838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9839 connector->base.id, connector->name,
8e329a03 9840 encoder->base.id, encoder->name);
d2dff872 9841
edde3617
ML
9842 old->restore_state = NULL;
9843
6c5ed5ae 9844 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 9845
79e53945
JB
9846 /*
9847 * Algorithm gets a little messy:
7a5e4805 9848 *
79e53945
JB
9849 * - if the connector already has an assigned crtc, use it (but make
9850 * sure it's on first)
7a5e4805 9851 *
79e53945
JB
9852 * - try to find the first unused crtc that can drive this connector,
9853 * and use that if we find one
79e53945
JB
9854 */
9855
9856 /* See if we already have a CRTC for this connector */
edde3617
ML
9857 if (connector->state->crtc) {
9858 crtc = connector->state->crtc;
8261b191 9859
51fd371b 9860 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 9861 if (ret)
ad3c558f 9862 goto fail;
8261b191
CW
9863
9864 /* Make sure the crtc and connector are running */
edde3617 9865 goto found;
79e53945
JB
9866 }
9867
9868 /* Find an unused one (if possible) */
70e1e0ec 9869 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9870 i++;
9871 if (!(encoder->possible_crtcs & (1 << i)))
9872 continue;
edde3617
ML
9873
9874 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
9875 if (ret)
9876 goto fail;
9877
9878 if (possible_crtc->state->enable) {
9879 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 9880 continue;
edde3617 9881 }
a459249c
VS
9882
9883 crtc = possible_crtc;
9884 break;
79e53945
JB
9885 }
9886
9887 /*
9888 * If we didn't find an unused CRTC, don't use any.
9889 */
9890 if (!crtc) {
7173188d 9891 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f4bf77b4 9892 ret = -ENODEV;
ad3c558f 9893 goto fail;
79e53945
JB
9894 }
9895
edde3617
ML
9896found:
9897 intel_crtc = to_intel_crtc(crtc);
9898
4d02e2de
DV
9899 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9900 if (ret)
ad3c558f 9901 goto fail;
79e53945 9902
83a57153 9903 state = drm_atomic_state_alloc(dev);
edde3617
ML
9904 restore_state = drm_atomic_state_alloc(dev);
9905 if (!state || !restore_state) {
9906 ret = -ENOMEM;
9907 goto fail;
9908 }
83a57153
ACO
9909
9910 state->acquire_ctx = ctx;
edde3617 9911 restore_state->acquire_ctx = ctx;
83a57153 9912
944b0c76
ACO
9913 connector_state = drm_atomic_get_connector_state(state, connector);
9914 if (IS_ERR(connector_state)) {
9915 ret = PTR_ERR(connector_state);
9916 goto fail;
9917 }
9918
edde3617
ML
9919 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
9920 if (ret)
9921 goto fail;
944b0c76 9922
4be07317
ACO
9923 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9924 if (IS_ERR(crtc_state)) {
9925 ret = PTR_ERR(crtc_state);
9926 goto fail;
9927 }
9928
49d6fa21 9929 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9930
6492711d
CW
9931 if (!mode)
9932 mode = &load_detect_mode;
79e53945 9933
d2dff872
CW
9934 /* We need a framebuffer large enough to accommodate all accesses
9935 * that the plane may generate whilst we perform load detection.
9936 * We can not rely on the fbcon either being present (we get called
9937 * during its initialisation to detect all boot displays, or it may
9938 * not even exist) or that it is large enough to satisfy the
9939 * requested mode.
9940 */
94352cf9
DV
9941 fb = mode_fits_in_fbdev(dev, mode);
9942 if (fb == NULL) {
d2dff872 9943 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 9944 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
9945 } else
9946 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9947 if (IS_ERR(fb)) {
d2dff872 9948 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
f4bf77b4 9949 ret = PTR_ERR(fb);
412b61d8 9950 goto fail;
79e53945 9951 }
79e53945 9952
d3a40d1b 9953 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
2b3a2e9f 9954 drm_framebuffer_put(fb);
d3a40d1b
ACO
9955 if (ret)
9956 goto fail;
9957
edde3617
ML
9958 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
9959 if (ret)
9960 goto fail;
9961
9962 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
9963 if (!ret)
9964 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
9965 if (!ret)
9966 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
9967 if (ret) {
9968 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
9969 goto fail;
9970 }
8c7b5ccb 9971
3ba86073
ML
9972 ret = drm_atomic_commit(state);
9973 if (ret) {
6492711d 9974 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 9975 goto fail;
79e53945 9976 }
edde3617
ML
9977
9978 old->restore_state = restore_state;
7abbd11f 9979 drm_atomic_state_put(state);
7173188d 9980
79e53945 9981 /* let the connector get through one full cycle before testing */
0f0f74bc 9982 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 9983 return true;
412b61d8 9984
ad3c558f 9985fail:
7fb71c8f
CW
9986 if (state) {
9987 drm_atomic_state_put(state);
9988 state = NULL;
9989 }
9990 if (restore_state) {
9991 drm_atomic_state_put(restore_state);
9992 restore_state = NULL;
9993 }
83a57153 9994
6c5ed5ae
ML
9995 if (ret == -EDEADLK)
9996 return ret;
51fd371b 9997
412b61d8 9998 return false;
79e53945
JB
9999}
10000
d2434ab7 10001void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10002 struct intel_load_detect_pipe *old,
10003 struct drm_modeset_acquire_ctx *ctx)
79e53945 10004{
d2434ab7
DV
10005 struct intel_encoder *intel_encoder =
10006 intel_attached_encoder(connector);
4ef69c7a 10007 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10008 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10009 int ret;
79e53945 10010
d2dff872 10011 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10012 connector->base.id, connector->name,
8e329a03 10013 encoder->base.id, encoder->name);
d2dff872 10014
edde3617 10015 if (!state)
0622a53c 10016 return;
79e53945 10017
581e49fe 10018 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 10019 if (ret)
edde3617 10020 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 10021 drm_atomic_state_put(state);
79e53945
JB
10022}
10023
da4a1efa 10024static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10025 const struct intel_crtc_state *pipe_config)
da4a1efa 10026{
fac5e23e 10027 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10028 u32 dpll = pipe_config->dpll_hw_state.dpll;
10029
10030 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10031 return dev_priv->vbt.lvds_ssc_freq;
6e266956 10032 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 10033 return 120000;
5db94019 10034 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
10035 return 96000;
10036 else
10037 return 48000;
10038}
10039
79e53945 10040/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10041static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10042 struct intel_crtc_state *pipe_config)
79e53945 10043{
f1f644dc 10044 struct drm_device *dev = crtc->base.dev;
fac5e23e 10045 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10046 int pipe = pipe_config->cpu_transcoder;
293623f7 10047 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10048 u32 fp;
9e2c8475 10049 struct dpll clock;
dccbea3b 10050 int port_clock;
da4a1efa 10051 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10052
10053 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10054 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10055 else
293623f7 10056 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10057
10058 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 10059 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
10060 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10061 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10062 } else {
10063 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10064 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10065 }
10066
5db94019 10067 if (!IS_GEN2(dev_priv)) {
9b1e14f4 10068 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
10069 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10070 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10071 else
10072 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10073 DPLL_FPA01_P1_POST_DIV_SHIFT);
10074
10075 switch (dpll & DPLL_MODE_MASK) {
10076 case DPLLB_MODE_DAC_SERIAL:
10077 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10078 5 : 10;
10079 break;
10080 case DPLLB_MODE_LVDS:
10081 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10082 7 : 14;
10083 break;
10084 default:
28c97730 10085 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10086 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10087 return;
79e53945
JB
10088 }
10089
9b1e14f4 10090 if (IS_PINEVIEW(dev_priv))
dccbea3b 10091 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10092 else
dccbea3b 10093 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10094 } else {
50a0bc90 10095 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 10096 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10097
10098 if (is_lvds) {
10099 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10100 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10101
10102 if (lvds & LVDS_CLKB_POWER_UP)
10103 clock.p2 = 7;
10104 else
10105 clock.p2 = 14;
79e53945
JB
10106 } else {
10107 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10108 clock.p1 = 2;
10109 else {
10110 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10111 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10112 }
10113 if (dpll & PLL_P2_DIVIDE_BY_4)
10114 clock.p2 = 4;
10115 else
10116 clock.p2 = 2;
79e53945 10117 }
da4a1efa 10118
dccbea3b 10119 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10120 }
10121
18442d08
VS
10122 /*
10123 * This value includes pixel_multiplier. We will use
241bfc38 10124 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10125 * encoder's get_config() function.
10126 */
dccbea3b 10127 pipe_config->port_clock = port_clock;
f1f644dc
JB
10128}
10129
6878da05
VS
10130int intel_dotclock_calculate(int link_freq,
10131 const struct intel_link_m_n *m_n)
f1f644dc 10132{
f1f644dc
JB
10133 /*
10134 * The calculation for the data clock is:
1041a02f 10135 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10136 * But we want to avoid losing precison if possible, so:
1041a02f 10137 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10138 *
10139 * and the link clock is simpler:
1041a02f 10140 * link_clock = (m * link_clock) / n
f1f644dc
JB
10141 */
10142
6878da05
VS
10143 if (!m_n->link_n)
10144 return 0;
f1f644dc 10145
3123698f 10146 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6878da05 10147}
f1f644dc 10148
18442d08 10149static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10150 struct intel_crtc_state *pipe_config)
6878da05 10151{
e3b247da 10152 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10153
18442d08
VS
10154 /* read out port_clock from the DPLL */
10155 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10156
f1f644dc 10157 /*
e3b247da
VS
10158 * In case there is an active pipe without active ports,
10159 * we may need some idea for the dotclock anyway.
10160 * Calculate one based on the FDI configuration.
79e53945 10161 */
2d112de7 10162 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10163 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10164 &pipe_config->fdi_m_n);
79e53945
JB
10165}
10166
de330815
VS
10167/* Returns the currently programmed mode of the given encoder. */
10168struct drm_display_mode *
10169intel_encoder_current_mode(struct intel_encoder *encoder)
79e53945 10170{
de330815
VS
10171 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10172 struct intel_crtc_state *crtc_state;
79e53945 10173 struct drm_display_mode *mode;
de330815
VS
10174 struct intel_crtc *crtc;
10175 enum pipe pipe;
10176
10177 if (!encoder->get_hw_state(encoder, &pipe))
10178 return NULL;
10179
10180 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
79e53945
JB
10181
10182 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10183 if (!mode)
10184 return NULL;
10185
de330815
VS
10186 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10187 if (!crtc_state) {
3f36b937
TU
10188 kfree(mode);
10189 return NULL;
10190 }
10191
de330815 10192 crtc_state->base.crtc = &crtc->base;
79e53945 10193
de330815
VS
10194 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10195 kfree(crtc_state);
10196 kfree(mode);
10197 return NULL;
10198 }
79e53945 10199
de330815 10200 encoder->get_config(encoder, crtc_state);
79e53945 10201
de330815 10202 intel_mode_from_pipe_config(mode, crtc_state);
79e53945 10203
de330815 10204 kfree(crtc_state);
3f36b937 10205
79e53945
JB
10206 return mode;
10207}
10208
10209static void intel_crtc_destroy(struct drm_crtc *crtc)
10210{
10211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10212
10213 drm_crtc_cleanup(crtc);
10214 kfree(intel_crtc);
10215}
10216
5a21b665
DV
10217/**
10218 * intel_wm_need_update - Check whether watermarks need updating
10219 * @plane: drm plane
10220 * @state: new plane state
10221 *
10222 * Check current plane state versus the new one to determine whether
10223 * watermarks need to be recalculated.
10224 *
10225 * Returns true or false.
10226 */
10227static bool intel_wm_need_update(struct drm_plane *plane,
10228 struct drm_plane_state *state)
10229{
10230 struct intel_plane_state *new = to_intel_plane_state(state);
10231 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10232
10233 /* Update watermarks on tiling or size changes. */
936e71e3 10234 if (new->base.visible != cur->base.visible)
5a21b665
DV
10235 return true;
10236
10237 if (!cur->base.fb || !new->base.fb)
10238 return false;
10239
bae781b2 10240 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10241 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10242 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10243 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10244 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10245 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
DV
10246 return true;
10247
10248 return false;
10249}
10250
b2b55502 10251static bool needs_scaling(const struct intel_plane_state *state)
5a21b665 10252{
936e71e3
VS
10253 int src_w = drm_rect_width(&state->base.src) >> 16;
10254 int src_h = drm_rect_height(&state->base.src) >> 16;
10255 int dst_w = drm_rect_width(&state->base.dst);
10256 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
DV
10257
10258 return (src_w != dst_w || src_h != dst_h);
10259}
d21fbe87 10260
b2b55502
VS
10261int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10262 struct drm_crtc_state *crtc_state,
10263 const struct intel_plane_state *old_plane_state,
da20eabd
ML
10264 struct drm_plane_state *plane_state)
10265{
ab1d3a0e 10266 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10267 struct drm_crtc *crtc = crtc_state->crtc;
10268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 10269 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 10270 struct drm_device *dev = crtc->dev;
ed4a6a7c 10271 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 10272 bool mode_changed = needs_modeset(crtc_state);
b2b55502 10273 bool was_crtc_enabled = old_crtc_state->base.active;
da20eabd 10274 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10275 bool turn_off, turn_on, visible, was_visible;
10276 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10277 int ret;
da20eabd 10278
e9728bd8 10279 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
10280 ret = skl_update_scaler_plane(
10281 to_intel_crtc_state(crtc_state),
10282 to_intel_plane_state(plane_state));
10283 if (ret)
10284 return ret;
10285 }
10286
936e71e3 10287 was_visible = old_plane_state->base.visible;
1d4258db 10288 visible = plane_state->visible;
da20eabd
ML
10289
10290 if (!was_crtc_enabled && WARN_ON(was_visible))
10291 was_visible = false;
10292
35c08f43
ML
10293 /*
10294 * Visibility is calculated as if the crtc was on, but
10295 * after scaler setup everything depends on it being off
10296 * when the crtc isn't active.
f818ffea
VS
10297 *
10298 * FIXME this is wrong for watermarks. Watermarks should also
10299 * be computed as if the pipe would be active. Perhaps move
10300 * per-plane wm computation to the .check_plane() hook, and
10301 * only combine the results from all planes in the current place?
35c08f43 10302 */
e9728bd8 10303 if (!is_crtc_enabled) {
1d4258db 10304 plane_state->visible = visible = false;
e9728bd8
VS
10305 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10306 }
da20eabd
ML
10307
10308 if (!was_visible && !visible)
10309 return 0;
10310
e8861675
ML
10311 if (fb != old_plane_state->base.fb)
10312 pipe_config->fb_changed = true;
10313
da20eabd
ML
10314 turn_off = was_visible && (!visible || mode_changed);
10315 turn_on = visible && (!was_visible || mode_changed);
10316
72660ce0 10317 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
10318 intel_crtc->base.base.id, intel_crtc->base.name,
10319 plane->base.base.id, plane->base.name,
72660ce0 10320 fb ? fb->base.id : -1);
da20eabd 10321
72660ce0 10322 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 10323 plane->base.base.id, plane->base.name,
72660ce0 10324 was_visible, visible,
da20eabd
ML
10325 turn_off, turn_on, mode_changed);
10326
caed361d 10327 if (turn_on) {
04548cba 10328 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10329 pipe_config->update_wm_pre = true;
caed361d
VS
10330
10331 /* must disable cxsr around plane enable/disable */
e9728bd8 10332 if (plane->id != PLANE_CURSOR)
caed361d
VS
10333 pipe_config->disable_cxsr = true;
10334 } else if (turn_off) {
04548cba 10335 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10336 pipe_config->update_wm_post = true;
92826fcd 10337
852eb00d 10338 /* must disable cxsr around plane enable/disable */
e9728bd8 10339 if (plane->id != PLANE_CURSOR)
ab1d3a0e 10340 pipe_config->disable_cxsr = true;
e9728bd8 10341 } else if (intel_wm_need_update(&plane->base, plane_state)) {
04548cba 10342 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df
VS
10343 /* FIXME bollocks */
10344 pipe_config->update_wm_pre = true;
10345 pipe_config->update_wm_post = true;
10346 }
852eb00d 10347 }
da20eabd 10348
8be6ca85 10349 if (visible || was_visible)
e9728bd8 10350 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 10351
31ae71fc
ML
10352 /*
10353 * WaCxSRDisabledForSpriteScaling:ivb
10354 *
10355 * cstate->update_wm was already set above, so this flag will
10356 * take effect when we commit and program watermarks.
10357 */
e9728bd8 10358 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10359 needs_scaling(to_intel_plane_state(plane_state)) &&
10360 !needs_scaling(old_plane_state))
10361 pipe_config->disable_lp_wm = true;
d21fbe87 10362
da20eabd
ML
10363 return 0;
10364}
10365
6d3a1ce7
ML
10366static bool encoders_cloneable(const struct intel_encoder *a,
10367 const struct intel_encoder *b)
10368{
10369 /* masks could be asymmetric, so check both ways */
10370 return a == b || (a->cloneable & (1 << b->type) &&
10371 b->cloneable & (1 << a->type));
10372}
10373
10374static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10375 struct intel_crtc *crtc,
10376 struct intel_encoder *encoder)
10377{
10378 struct intel_encoder *source_encoder;
10379 struct drm_connector *connector;
10380 struct drm_connector_state *connector_state;
10381 int i;
10382
aa5e9b47 10383 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
10384 if (connector_state->crtc != &crtc->base)
10385 continue;
10386
10387 source_encoder =
10388 to_intel_encoder(connector_state->best_encoder);
10389 if (!encoders_cloneable(encoder, source_encoder))
10390 return false;
10391 }
10392
10393 return true;
10394}
10395
6d3a1ce7
ML
10396static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10397 struct drm_crtc_state *crtc_state)
10398{
cf5a15be 10399 struct drm_device *dev = crtc->dev;
fac5e23e 10400 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10402 struct intel_crtc_state *pipe_config =
10403 to_intel_crtc_state(crtc_state);
6d3a1ce7 10404 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10405 int ret;
6d3a1ce7
ML
10406 bool mode_changed = needs_modeset(crtc_state);
10407
852eb00d 10408 if (mode_changed && !crtc_state->active)
caed361d 10409 pipe_config->update_wm_post = true;
eddfcbcd 10410
ad421372
ML
10411 if (mode_changed && crtc_state->enable &&
10412 dev_priv->display.crtc_compute_clock &&
8106ddbd 10413 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10414 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10415 pipe_config);
10416 if (ret)
10417 return ret;
10418 }
10419
82cf435b
LL
10420 if (crtc_state->color_mgmt_changed) {
10421 ret = intel_color_check(crtc, crtc_state);
10422 if (ret)
10423 return ret;
e7852a4b
LL
10424
10425 /*
10426 * Changing color management on Intel hardware is
10427 * handled as part of planes update.
10428 */
10429 crtc_state->planes_changed = true;
82cf435b
LL
10430 }
10431
e435d6e5 10432 ret = 0;
86c8bbbe 10433 if (dev_priv->display.compute_pipe_wm) {
e3bddded 10434 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
10435 if (ret) {
10436 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
10437 return ret;
10438 }
10439 }
10440
10441 if (dev_priv->display.compute_intermediate_wm &&
10442 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10443 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10444 return 0;
10445
10446 /*
10447 * Calculate 'intermediate' watermarks that satisfy both the
10448 * old state and the new state. We can program these
10449 * immediately.
10450 */
6315b5d3 10451 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
10452 intel_crtc,
10453 pipe_config);
10454 if (ret) {
10455 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 10456 return ret;
ed4a6a7c 10457 }
e3d5457c
VS
10458 } else if (dev_priv->display.compute_intermediate_wm) {
10459 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10460 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
10461 }
10462
6315b5d3 10463 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
10464 if (mode_changed)
10465 ret = skl_update_scaler_crtc(pipe_config);
10466
73b0ca8e
MK
10467 if (!ret)
10468 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10469 pipe_config);
e435d6e5 10470 if (!ret)
6ebc6923 10471 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
10472 pipe_config);
10473 }
10474
10475 return ret;
6d3a1ce7
ML
10476}
10477
65b38e0d 10478static const struct drm_crtc_helper_funcs intel_helper_funcs = {
5a21b665
DV
10479 .atomic_begin = intel_begin_crtc_commit,
10480 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 10481 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
10482};
10483
d29b2f9d
ACO
10484static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10485{
10486 struct intel_connector *connector;
f9e905ca 10487 struct drm_connector_list_iter conn_iter;
d29b2f9d 10488
f9e905ca
DV
10489 drm_connector_list_iter_begin(dev, &conn_iter);
10490 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f
DV
10491 if (connector->base.state->crtc)
10492 drm_connector_unreference(&connector->base);
10493
d29b2f9d
ACO
10494 if (connector->base.encoder) {
10495 connector->base.state->best_encoder =
10496 connector->base.encoder;
10497 connector->base.state->crtc =
10498 connector->base.encoder->crtc;
8863dc7f
DV
10499
10500 drm_connector_reference(&connector->base);
d29b2f9d
ACO
10501 } else {
10502 connector->base.state->best_encoder = NULL;
10503 connector->base.state->crtc = NULL;
10504 }
10505 }
f9e905ca 10506 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
10507}
10508
050f7aeb 10509static void
eba905b2 10510connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 10511 struct intel_crtc_state *pipe_config)
050f7aeb 10512{
6a2a5c5d 10513 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
DV
10514 int bpp = pipe_config->pipe_bpp;
10515
10516 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
10517 connector->base.base.id,
10518 connector->base.name);
050f7aeb
DV
10519
10520 /* Don't use an invalid EDID bpc value */
6a2a5c5d 10521 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 10522 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
10523 bpp, info->bpc * 3);
10524 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
DV
10525 }
10526
196f954e 10527 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 10528 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
10529 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10530 bpp);
10531 pipe_config->pipe_bpp = 24;
050f7aeb
DV
10532 }
10533}
10534
4e53c2e0 10535static int
050f7aeb 10536compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 10537 struct intel_crtc_state *pipe_config)
4e53c2e0 10538{
9beb5fea 10539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 10540 struct drm_atomic_state *state;
da3ced29
ACO
10541 struct drm_connector *connector;
10542 struct drm_connector_state *connector_state;
1486017f 10543 int bpp, i;
4e53c2e0 10544
9beb5fea
TU
10545 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
10546 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 10547 bpp = 10*3;
9beb5fea 10548 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
10549 bpp = 12*3;
10550 else
10551 bpp = 8*3;
10552
4e53c2e0 10553
4e53c2e0
DV
10554 pipe_config->pipe_bpp = bpp;
10555
1486017f
ACO
10556 state = pipe_config->base.state;
10557
4e53c2e0 10558 /* Clamp display bpp to EDID value */
aa5e9b47 10559 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 10560 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
10561 continue;
10562
da3ced29
ACO
10563 connected_sink_compute_bpp(to_intel_connector(connector),
10564 pipe_config);
4e53c2e0
DV
10565 }
10566
10567 return bpp;
10568}
10569
644db711
DV
10570static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10571{
10572 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10573 "type: 0x%x flags: 0x%x\n",
1342830c 10574 mode->crtc_clock,
644db711
DV
10575 mode->crtc_hdisplay, mode->crtc_hsync_start,
10576 mode->crtc_hsync_end, mode->crtc_htotal,
10577 mode->crtc_vdisplay, mode->crtc_vsync_start,
10578 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10579}
10580
f6982332
TU
10581static inline void
10582intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 10583 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 10584{
a4309657
TU
10585 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10586 id, lane_count,
f6982332
TU
10587 m_n->gmch_m, m_n->gmch_n,
10588 m_n->link_m, m_n->link_n, m_n->tu);
10589}
10590
40b2be41
VS
10591#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
10592
10593static const char * const output_type_str[] = {
10594 OUTPUT_TYPE(UNUSED),
10595 OUTPUT_TYPE(ANALOG),
10596 OUTPUT_TYPE(DVO),
10597 OUTPUT_TYPE(SDVO),
10598 OUTPUT_TYPE(LVDS),
10599 OUTPUT_TYPE(TVOUT),
10600 OUTPUT_TYPE(HDMI),
10601 OUTPUT_TYPE(DP),
10602 OUTPUT_TYPE(EDP),
10603 OUTPUT_TYPE(DSI),
10604 OUTPUT_TYPE(UNKNOWN),
10605 OUTPUT_TYPE(DP_MST),
10606};
10607
10608#undef OUTPUT_TYPE
10609
10610static void snprintf_output_types(char *buf, size_t len,
10611 unsigned int output_types)
10612{
10613 char *str = buf;
10614 int i;
10615
10616 str[0] = '\0';
10617
10618 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
10619 int r;
10620
10621 if ((output_types & BIT(i)) == 0)
10622 continue;
10623
10624 r = snprintf(str, len, "%s%s",
10625 str != buf ? "," : "", output_type_str[i]);
10626 if (r >= len)
10627 break;
10628 str += r;
10629 len -= r;
10630
10631 output_types &= ~BIT(i);
10632 }
10633
10634 WARN_ON_ONCE(output_types != 0);
10635}
10636
c0b03411 10637static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 10638 struct intel_crtc_state *pipe_config,
c0b03411
DV
10639 const char *context)
10640{
6a60cd87 10641 struct drm_device *dev = crtc->base.dev;
4f8036a2 10642 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
10643 struct drm_plane *plane;
10644 struct intel_plane *intel_plane;
10645 struct intel_plane_state *state;
10646 struct drm_framebuffer *fb;
40b2be41 10647 char buf[64];
6a60cd87 10648
66766e4f
TU
10649 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
10650 crtc->base.base.id, crtc->base.name, context);
c0b03411 10651
40b2be41
VS
10652 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
10653 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
10654 buf, pipe_config->output_types);
10655
2c89429e
TU
10656 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
10657 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 10658 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
10659
10660 if (pipe_config->has_pch_encoder)
10661 intel_dump_m_n_config(pipe_config, "fdi",
10662 pipe_config->fdi_lanes,
10663 &pipe_config->fdi_m_n);
f6982332 10664
b22ca995
SS
10665 if (pipe_config->ycbcr420)
10666 DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
10667
f6982332 10668 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
10669 intel_dump_m_n_config(pipe_config, "dp m_n",
10670 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
10671 if (pipe_config->has_drrs)
10672 intel_dump_m_n_config(pipe_config, "dp m2_n2",
10673 pipe_config->lane_count,
10674 &pipe_config->dp_m2_n2);
f6982332 10675 }
b95af8be 10676
55072d19 10677 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 10678 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 10679
c0b03411 10680 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 10681 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 10682 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
10683 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10684 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 10685 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 10686 pipe_config->port_clock,
a7d1b3f4
VS
10687 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
10688 pipe_config->pixel_rate);
dd2f616d
TU
10689
10690 if (INTEL_GEN(dev_priv) >= 9)
10691 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
10692 crtc->num_scalers,
10693 pipe_config->scaler_state.scaler_users,
10694 pipe_config->scaler_state.scaler_id);
a74f8375
TU
10695
10696 if (HAS_GMCH_DISPLAY(dev_priv))
10697 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10698 pipe_config->gmch_pfit.control,
10699 pipe_config->gmch_pfit.pgm_ratios,
10700 pipe_config->gmch_pfit.lvds_border_bits);
10701 else
10702 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
10703 pipe_config->pch_pfit.pos,
10704 pipe_config->pch_pfit.size,
08c4d7fc 10705 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 10706
2c89429e
TU
10707 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
10708 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 10709
f50b79f0 10710 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 10711
6a60cd87
CK
10712 DRM_DEBUG_KMS("planes on this crtc\n");
10713 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 10714 struct drm_format_name_buf format_name;
6a60cd87
CK
10715 intel_plane = to_intel_plane(plane);
10716 if (intel_plane->pipe != crtc->pipe)
10717 continue;
10718
10719 state = to_intel_plane_state(plane->state);
10720 fb = state->base.fb;
10721 if (!fb) {
1d577e02
VS
10722 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
10723 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
10724 continue;
10725 }
10726
dd2f616d
TU
10727 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
10728 plane->base.id, plane->name,
b3c11ac2 10729 fb->base.id, fb->width, fb->height,
438b74a5 10730 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
10731 if (INTEL_GEN(dev_priv) >= 9)
10732 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
10733 state->scaler_id,
10734 state->base.src.x1 >> 16,
10735 state->base.src.y1 >> 16,
10736 drm_rect_width(&state->base.src) >> 16,
10737 drm_rect_height(&state->base.src) >> 16,
10738 state->base.dst.x1, state->base.dst.y1,
10739 drm_rect_width(&state->base.dst),
10740 drm_rect_height(&state->base.dst));
6a60cd87 10741 }
c0b03411
DV
10742}
10743
5448a00d 10744static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 10745{
5448a00d 10746 struct drm_device *dev = state->dev;
da3ced29 10747 struct drm_connector *connector;
2fd96b41 10748 struct drm_connector_list_iter conn_iter;
00f0b378 10749 unsigned int used_ports = 0;
477321e0 10750 unsigned int used_mst_ports = 0;
00f0b378
VS
10751
10752 /*
10753 * Walk the connector list instead of the encoder
10754 * list to detect the problem on ddi platforms
10755 * where there's just one encoder per digital port.
10756 */
2fd96b41
GP
10757 drm_connector_list_iter_begin(dev, &conn_iter);
10758 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
10759 struct drm_connector_state *connector_state;
10760 struct intel_encoder *encoder;
10761
10762 connector_state = drm_atomic_get_existing_connector_state(state, connector);
10763 if (!connector_state)
10764 connector_state = connector->state;
10765
5448a00d 10766 if (!connector_state->best_encoder)
00f0b378
VS
10767 continue;
10768
5448a00d
ACO
10769 encoder = to_intel_encoder(connector_state->best_encoder);
10770
10771 WARN_ON(!connector_state->crtc);
00f0b378
VS
10772
10773 switch (encoder->type) {
10774 unsigned int port_mask;
10775 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 10776 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 10777 break;
cca0502b 10778 case INTEL_OUTPUT_DP:
00f0b378
VS
10779 case INTEL_OUTPUT_HDMI:
10780 case INTEL_OUTPUT_EDP:
10781 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10782
10783 /* the same port mustn't appear more than once */
10784 if (used_ports & port_mask)
10785 return false;
10786
10787 used_ports |= port_mask;
477321e0
VS
10788 break;
10789 case INTEL_OUTPUT_DP_MST:
10790 used_mst_ports |=
10791 1 << enc_to_mst(&encoder->base)->primary->port;
10792 break;
00f0b378
VS
10793 default:
10794 break;
10795 }
10796 }
2fd96b41 10797 drm_connector_list_iter_end(&conn_iter);
00f0b378 10798
477321e0
VS
10799 /* can't mix MST and SST/HDMI on the same port */
10800 if (used_ports & used_mst_ports)
10801 return false;
10802
00f0b378
VS
10803 return true;
10804}
10805
83a57153
ACO
10806static void
10807clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10808{
ff32c54e
VS
10809 struct drm_i915_private *dev_priv =
10810 to_i915(crtc_state->base.crtc->dev);
663a3640 10811 struct intel_crtc_scaler_state scaler_state;
4978cc93 10812 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 10813 struct intel_shared_dpll *shared_dpll;
ff32c54e 10814 struct intel_crtc_wm_state wm_state;
6e644626 10815 bool force_thru, ips_force_disable;
83a57153 10816
7546a384
ACO
10817 /* FIXME: before the switch to atomic started, a new pipe_config was
10818 * kzalloc'd. Code that depends on any field being zero should be
10819 * fixed, so that the crtc_state can be safely duplicated. For now,
10820 * only fields that are know to not cause problems are preserved. */
10821
663a3640 10822 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
10823 shared_dpll = crtc_state->shared_dpll;
10824 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 10825 force_thru = crtc_state->pch_pfit.force_thru;
6e644626 10826 ips_force_disable = crtc_state->ips_force_disable;
04548cba
VS
10827 if (IS_G4X(dev_priv) ||
10828 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 10829 wm_state = crtc_state->wm;
4978cc93 10830
d2fa80a5
CW
10831 /* Keep base drm_crtc_state intact, only clear our extended struct */
10832 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
10833 memset(&crtc_state->base + 1, 0,
10834 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 10835
663a3640 10836 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
10837 crtc_state->shared_dpll = shared_dpll;
10838 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 10839 crtc_state->pch_pfit.force_thru = force_thru;
6e644626 10840 crtc_state->ips_force_disable = ips_force_disable;
04548cba
VS
10841 if (IS_G4X(dev_priv) ||
10842 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 10843 crtc_state->wm = wm_state;
83a57153
ACO
10844}
10845
548ee15b 10846static int
b8cecdf5 10847intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 10848 struct intel_crtc_state *pipe_config)
ee7b9f93 10849{
b359283a 10850 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 10851 struct intel_encoder *encoder;
da3ced29 10852 struct drm_connector *connector;
0b901879 10853 struct drm_connector_state *connector_state;
d328c9d7 10854 int base_bpp, ret = -EINVAL;
0b901879 10855 int i;
e29c22c0 10856 bool retry = true;
ee7b9f93 10857
83a57153 10858 clear_intel_crtc_state(pipe_config);
7758a113 10859
e143a21c
DV
10860 pipe_config->cpu_transcoder =
10861 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 10862
2960bc9c
ID
10863 /*
10864 * Sanitize sync polarity flags based on requested ones. If neither
10865 * positive or negative polarity is requested, treat this as meaning
10866 * negative polarity.
10867 */
2d112de7 10868 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10869 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 10870 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 10871
2d112de7 10872 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 10873 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 10874 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 10875
d328c9d7
DV
10876 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10877 pipe_config);
10878 if (base_bpp < 0)
4e53c2e0
DV
10879 goto fail;
10880
e41a56be
VS
10881 /*
10882 * Determine the real pipe dimensions. Note that stereo modes can
10883 * increase the actual pipe size due to the frame doubling and
10884 * insertion of additional space for blanks between the frame. This
10885 * is stored in the crtc timings. We use the requested mode to do this
10886 * computation to clearly distinguish it from the adjusted mode, which
10887 * can be changed by the connectors in the below retry loop.
10888 */
196cd5d3 10889 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
10890 &pipe_config->pipe_src_w,
10891 &pipe_config->pipe_src_h);
e41a56be 10892
aa5e9b47 10893 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
10894 if (connector_state->crtc != crtc)
10895 continue;
10896
10897 encoder = to_intel_encoder(connector_state->best_encoder);
10898
e25148d0
VS
10899 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
10900 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10901 goto fail;
10902 }
10903
253c84c8
VS
10904 /*
10905 * Determine output_types before calling the .compute_config()
10906 * hooks so that the hooks can use this information safely.
10907 */
10908 pipe_config->output_types |= 1 << encoder->type;
10909 }
10910
e29c22c0 10911encoder_retry:
ef1b460d 10912 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10913 pipe_config->port_clock = 0;
ef1b460d 10914 pipe_config->pixel_multiplier = 1;
ff9a6750 10915
135c81b8 10916 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
10917 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10918 CRTC_STEREO_DOUBLE);
135c81b8 10919
7758a113
DV
10920 /* Pass our mode to the connectors and the CRTC to give them a chance to
10921 * adjust it according to limitations or connector properties, and also
10922 * a chance to reject the mode entirely.
47f1c6c9 10923 */
aa5e9b47 10924 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 10925 if (connector_state->crtc != crtc)
7758a113 10926 continue;
7ae89233 10927
0b901879
ACO
10928 encoder = to_intel_encoder(connector_state->best_encoder);
10929
0a478c27 10930 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 10931 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10932 goto fail;
10933 }
ee7b9f93 10934 }
47f1c6c9 10935
ff9a6750
DV
10936 /* Set default port clock if not overwritten by the encoder. Needs to be
10937 * done afterwards in case the encoder adjusts the mode. */
10938 if (!pipe_config->port_clock)
2d112de7 10939 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 10940 * pipe_config->pixel_multiplier;
ff9a6750 10941
a43f6e0f 10942 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10943 if (ret < 0) {
7758a113
DV
10944 DRM_DEBUG_KMS("CRTC fixup failed\n");
10945 goto fail;
ee7b9f93 10946 }
e29c22c0
DV
10947
10948 if (ret == RETRY) {
10949 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10950 ret = -EINVAL;
10951 goto fail;
10952 }
10953
10954 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10955 retry = false;
10956 goto encoder_retry;
10957 }
10958
e8fa4270 10959 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
10960 * only enable it on 6bpc panels and when its not a compliance
10961 * test requesting 6bpc video pattern.
10962 */
10963 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
10964 !pipe_config->dither_force_disable;
62f0ace5 10965 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 10966 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 10967
7758a113 10968fail:
548ee15b 10969 return ret;
ee7b9f93 10970}
47f1c6c9 10971
ea9d758d 10972static void
4740b0f2 10973intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 10974{
0a9ab303 10975 struct drm_crtc *crtc;
aa5e9b47 10976 struct drm_crtc_state *new_crtc_state;
8a75d157 10977 int i;
ea9d758d 10978
7668851f 10979 /* Double check state. */
aa5e9b47
ML
10980 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
10981 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
fc467a22 10982
61067a5e
ML
10983 /*
10984 * Update legacy state to satisfy fbc code. This can
10985 * be removed when fbc uses the atomic state.
10986 */
10987 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
10988 struct drm_plane_state *plane_state = crtc->primary->state;
10989
10990 crtc->primary->fb = plane_state->fb;
10991 crtc->x = plane_state->src_x >> 16;
10992 crtc->y = plane_state->src_y >> 16;
10993 }
ea9d758d 10994 }
ea9d758d
DV
10995}
10996
3bd26263 10997static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10998{
3bd26263 10999 int diff;
f1f644dc
JB
11000
11001 if (clock1 == clock2)
11002 return true;
11003
11004 if (!clock1 || !clock2)
11005 return false;
11006
11007 diff = abs(clock1 - clock2);
11008
11009 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11010 return true;
11011
11012 return false;
11013}
11014
cfb23ed6
ML
11015static bool
11016intel_compare_m_n(unsigned int m, unsigned int n,
11017 unsigned int m2, unsigned int n2,
11018 bool exact)
11019{
11020 if (m == m2 && n == n2)
11021 return true;
11022
11023 if (exact || !m || !n || !m2 || !n2)
11024 return false;
11025
11026 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11027
31d10b57
ML
11028 if (n > n2) {
11029 while (n > n2) {
cfb23ed6
ML
11030 m2 <<= 1;
11031 n2 <<= 1;
11032 }
31d10b57
ML
11033 } else if (n < n2) {
11034 while (n < n2) {
cfb23ed6
ML
11035 m <<= 1;
11036 n <<= 1;
11037 }
11038 }
11039
31d10b57
ML
11040 if (n != n2)
11041 return false;
11042
11043 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11044}
11045
11046static bool
11047intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11048 struct intel_link_m_n *m2_n2,
11049 bool adjust)
11050{
11051 if (m_n->tu == m2_n2->tu &&
11052 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11053 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11054 intel_compare_m_n(m_n->link_m, m_n->link_n,
11055 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11056 if (adjust)
11057 *m2_n2 = *m_n;
11058
11059 return true;
11060 }
11061
11062 return false;
11063}
11064
4e8048f8
TU
11065static void __printf(3, 4)
11066pipe_config_err(bool adjust, const char *name, const char *format, ...)
11067{
11068 char *level;
11069 unsigned int category;
11070 struct va_format vaf;
11071 va_list args;
11072
11073 if (adjust) {
11074 level = KERN_DEBUG;
11075 category = DRM_UT_KMS;
11076 } else {
11077 level = KERN_ERR;
11078 category = DRM_UT_NONE;
11079 }
11080
11081 va_start(args, format);
11082 vaf.fmt = format;
11083 vaf.va = &args;
11084
11085 drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
11086
11087 va_end(args);
11088}
11089
0e8ffe1b 11090static bool
6315b5d3 11091intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11092 struct intel_crtc_state *current_config,
cfb23ed6
ML
11093 struct intel_crtc_state *pipe_config,
11094 bool adjust)
0e8ffe1b 11095{
cfb23ed6
ML
11096 bool ret = true;
11097
66e985c0
DV
11098#define PIPE_CONF_CHECK_X(name) \
11099 if (current_config->name != pipe_config->name) { \
4e8048f8 11100 pipe_config_err(adjust, __stringify(name), \
66e985c0
DV
11101 "(expected 0x%08x, found 0x%08x)\n", \
11102 current_config->name, \
11103 pipe_config->name); \
cfb23ed6 11104 ret = false; \
66e985c0
DV
11105 }
11106
08a24034
DV
11107#define PIPE_CONF_CHECK_I(name) \
11108 if (current_config->name != pipe_config->name) { \
4e8048f8 11109 pipe_config_err(adjust, __stringify(name), \
08a24034
DV
11110 "(expected %i, found %i)\n", \
11111 current_config->name, \
11112 pipe_config->name); \
cfb23ed6
ML
11113 ret = false; \
11114 }
11115
8106ddbd
ACO
11116#define PIPE_CONF_CHECK_P(name) \
11117 if (current_config->name != pipe_config->name) { \
4e8048f8 11118 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11119 "(expected %p, found %p)\n", \
11120 current_config->name, \
11121 pipe_config->name); \
11122 ret = false; \
11123 }
11124
cfb23ed6
ML
11125#define PIPE_CONF_CHECK_M_N(name) \
11126 if (!intel_compare_link_m_n(&current_config->name, \
11127 &pipe_config->name,\
11128 adjust)) { \
4e8048f8 11129 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11130 "(expected tu %i gmch %i/%i link %i/%i, " \
11131 "found tu %i, gmch %i/%i link %i/%i)\n", \
11132 current_config->name.tu, \
11133 current_config->name.gmch_m, \
11134 current_config->name.gmch_n, \
11135 current_config->name.link_m, \
11136 current_config->name.link_n, \
11137 pipe_config->name.tu, \
11138 pipe_config->name.gmch_m, \
11139 pipe_config->name.gmch_n, \
11140 pipe_config->name.link_m, \
11141 pipe_config->name.link_n); \
11142 ret = false; \
11143 }
11144
55c561a7
DV
11145/* This is required for BDW+ where there is only one set of registers for
11146 * switching between high and low RR.
11147 * This macro can be used whenever a comparison has to be made between one
11148 * hw state and multiple sw state variables.
11149 */
cfb23ed6
ML
11150#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
11151 if (!intel_compare_link_m_n(&current_config->name, \
11152 &pipe_config->name, adjust) && \
11153 !intel_compare_link_m_n(&current_config->alt_name, \
11154 &pipe_config->name, adjust)) { \
4e8048f8 11155 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11156 "(expected tu %i gmch %i/%i link %i/%i, " \
11157 "or tu %i gmch %i/%i link %i/%i, " \
11158 "found tu %i, gmch %i/%i link %i/%i)\n", \
11159 current_config->name.tu, \
11160 current_config->name.gmch_m, \
11161 current_config->name.gmch_n, \
11162 current_config->name.link_m, \
11163 current_config->name.link_n, \
11164 current_config->alt_name.tu, \
11165 current_config->alt_name.gmch_m, \
11166 current_config->alt_name.gmch_n, \
11167 current_config->alt_name.link_m, \
11168 current_config->alt_name.link_n, \
11169 pipe_config->name.tu, \
11170 pipe_config->name.gmch_m, \
11171 pipe_config->name.gmch_n, \
11172 pipe_config->name.link_m, \
11173 pipe_config->name.link_n); \
11174 ret = false; \
88adfff1
DV
11175 }
11176
1bd1bd80
DV
11177#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11178 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11179 pipe_config_err(adjust, __stringify(name), \
11180 "(%x) (expected %i, found %i)\n", \
11181 (mask), \
1bd1bd80
DV
11182 current_config->name & (mask), \
11183 pipe_config->name & (mask)); \
cfb23ed6 11184 ret = false; \
1bd1bd80
DV
11185 }
11186
5e550656
VS
11187#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11188 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11189 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11190 "(expected %i, found %i)\n", \
11191 current_config->name, \
11192 pipe_config->name); \
cfb23ed6 11193 ret = false; \
5e550656
VS
11194 }
11195
bb760063
DV
11196#define PIPE_CONF_QUIRK(quirk) \
11197 ((current_config->quirks | pipe_config->quirks) & (quirk))
11198
eccb140b
DV
11199 PIPE_CONF_CHECK_I(cpu_transcoder);
11200
08a24034
DV
11201 PIPE_CONF_CHECK_I(has_pch_encoder);
11202 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11203 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11204
90a6b7b0 11205 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11206 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11207
6315b5d3 11208 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11209 PIPE_CONF_CHECK_M_N(dp_m_n);
11210
cfb23ed6
ML
11211 if (current_config->has_drrs)
11212 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11213 } else
11214 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11215
253c84c8 11216 PIPE_CONF_CHECK_X(output_types);
a65347ba 11217
2d112de7
ACO
11218 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11219 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11220 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11221 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11222 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11223 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11224
2d112de7
ACO
11225 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11226 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11227 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11228 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11229 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11230 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11231
c93f54cf 11232 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11233 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 11234 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11235 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 11236 PIPE_CONF_CHECK_I(limited_color_range);
15953637
SS
11237
11238 PIPE_CONF_CHECK_I(hdmi_scrambling);
11239 PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
e43823ec 11240 PIPE_CONF_CHECK_I(has_infoframe);
60436fd4 11241 PIPE_CONF_CHECK_I(ycbcr420);
6c49f241 11242
9ed109a7
DV
11243 PIPE_CONF_CHECK_I(has_audio);
11244
2d112de7 11245 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11246 DRM_MODE_FLAG_INTERLACE);
11247
bb760063 11248 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11249 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11250 DRM_MODE_FLAG_PHSYNC);
2d112de7 11251 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11252 DRM_MODE_FLAG_NHSYNC);
2d112de7 11253 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11254 DRM_MODE_FLAG_PVSYNC);
2d112de7 11255 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11256 DRM_MODE_FLAG_NVSYNC);
11257 }
045ac3b5 11258
333b8ca8 11259 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11260 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11261 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11262 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11263 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11264
bfd16b2a
ML
11265 if (!adjust) {
11266 PIPE_CONF_CHECK_I(pipe_src_w);
11267 PIPE_CONF_CHECK_I(pipe_src_h);
11268
11269 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11270 if (current_config->pch_pfit.enabled) {
11271 PIPE_CONF_CHECK_X(pch_pfit.pos);
11272 PIPE_CONF_CHECK_X(pch_pfit.size);
11273 }
2fa2fe9a 11274
7aefe2b5 11275 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11276 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11277 }
a1b2278e 11278
e59150dc 11279 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 11280 if (IS_HASWELL(dev_priv))
e59150dc 11281 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11282
282740f7
VS
11283 PIPE_CONF_CHECK_I(double_wide);
11284
8106ddbd 11285 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11286 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11287 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11288 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11289 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11290 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11291 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11292 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11293 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11294 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
2de38138
PZ
11295 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11296 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11297 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11298 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11299 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11300 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11301 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11302 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11303 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11304 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11305 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11306 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
c0d43d62 11307
47eacbab
VS
11308 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11309 PIPE_CONF_CHECK_X(dsi_pll.div);
11310
9beb5fea 11311 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11312 PIPE_CONF_CHECK_I(pipe_bpp);
11313
2d112de7 11314 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11315 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11316
66e985c0 11317#undef PIPE_CONF_CHECK_X
08a24034 11318#undef PIPE_CONF_CHECK_I
8106ddbd 11319#undef PIPE_CONF_CHECK_P
1bd1bd80 11320#undef PIPE_CONF_CHECK_FLAGS
5e550656 11321#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11322#undef PIPE_CONF_QUIRK
88adfff1 11323
cfb23ed6 11324 return ret;
0e8ffe1b
DV
11325}
11326
e3b247da
VS
11327static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11328 const struct intel_crtc_state *pipe_config)
11329{
11330 if (pipe_config->has_pch_encoder) {
21a727b3 11331 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11332 &pipe_config->fdi_m_n);
11333 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11334
11335 /*
11336 * FDI already provided one idea for the dotclock.
11337 * Yell if the encoder disagrees.
11338 */
11339 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11340 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11341 fdi_dotclock, dotclock);
11342 }
11343}
11344
c0ead703
ML
11345static void verify_wm_state(struct drm_crtc *crtc,
11346 struct drm_crtc_state *new_state)
08db6652 11347{
6315b5d3 11348 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11349 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11350 struct skl_pipe_wm hw_wm, *sw_wm;
11351 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11352 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11354 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11355 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11356
6315b5d3 11357 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11358 return;
11359
3de8a14c 11360 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11361 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11362
08db6652
DL
11363 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11364 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11365
e7c84544 11366 /* planes */
8b364b41 11367 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11368 hw_plane_wm = &hw_wm.planes[plane];
11369 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11370
3de8a14c 11371 /* Watermarks */
11372 for (level = 0; level <= max_level; level++) {
11373 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11374 &sw_plane_wm->wm[level]))
11375 continue;
11376
11377 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",
11378 pipe_name(pipe), plane + 1, level,
11379 sw_plane_wm->wm[level].plane_en,
11380 sw_plane_wm->wm[level].plane_res_b,
11381 sw_plane_wm->wm[level].plane_res_l,
11382 hw_plane_wm->wm[level].plane_en,
11383 hw_plane_wm->wm[level].plane_res_b,
11384 hw_plane_wm->wm[level].plane_res_l);
11385 }
08db6652 11386
3de8a14c 11387 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11388 &sw_plane_wm->trans_wm)) {
11389 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",
11390 pipe_name(pipe), plane + 1,
11391 sw_plane_wm->trans_wm.plane_en,
11392 sw_plane_wm->trans_wm.plane_res_b,
11393 sw_plane_wm->trans_wm.plane_res_l,
11394 hw_plane_wm->trans_wm.plane_en,
11395 hw_plane_wm->trans_wm.plane_res_b,
11396 hw_plane_wm->trans_wm.plane_res_l);
11397 }
11398
11399 /* DDB */
11400 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11401 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11402
11403 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11404 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11405 pipe_name(pipe), plane + 1,
11406 sw_ddb_entry->start, sw_ddb_entry->end,
11407 hw_ddb_entry->start, hw_ddb_entry->end);
11408 }
e7c84544 11409 }
08db6652 11410
27082493
L
11411 /*
11412 * cursor
11413 * If the cursor plane isn't active, we may not have updated it's ddb
11414 * allocation. In that case since the ddb allocation will be updated
11415 * once the plane becomes visible, we can skip this check
11416 */
cd5dcbf1 11417 if (1) {
3de8a14c 11418 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11419 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11420
11421 /* Watermarks */
11422 for (level = 0; level <= max_level; level++) {
11423 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11424 &sw_plane_wm->wm[level]))
11425 continue;
11426
11427 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",
11428 pipe_name(pipe), level,
11429 sw_plane_wm->wm[level].plane_en,
11430 sw_plane_wm->wm[level].plane_res_b,
11431 sw_plane_wm->wm[level].plane_res_l,
11432 hw_plane_wm->wm[level].plane_en,
11433 hw_plane_wm->wm[level].plane_res_b,
11434 hw_plane_wm->wm[level].plane_res_l);
11435 }
11436
11437 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11438 &sw_plane_wm->trans_wm)) {
11439 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11440 pipe_name(pipe),
11441 sw_plane_wm->trans_wm.plane_en,
11442 sw_plane_wm->trans_wm.plane_res_b,
11443 sw_plane_wm->trans_wm.plane_res_l,
11444 hw_plane_wm->trans_wm.plane_en,
11445 hw_plane_wm->trans_wm.plane_res_b,
11446 hw_plane_wm->trans_wm.plane_res_l);
11447 }
11448
11449 /* DDB */
11450 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11451 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11452
3de8a14c 11453 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11454 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11455 pipe_name(pipe),
3de8a14c 11456 sw_ddb_entry->start, sw_ddb_entry->end,
11457 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11458 }
08db6652
DL
11459 }
11460}
11461
91d1b4bd 11462static void
677100ce
ML
11463verify_connector_state(struct drm_device *dev,
11464 struct drm_atomic_state *state,
11465 struct drm_crtc *crtc)
8af6cf88 11466{
35dd3c64 11467 struct drm_connector *connector;
aa5e9b47 11468 struct drm_connector_state *new_conn_state;
677100ce 11469 int i;
8af6cf88 11470
aa5e9b47 11471 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 11472 struct drm_encoder *encoder = connector->encoder;
749d98b8 11473 struct drm_crtc_state *crtc_state = NULL;
ad3c558f 11474
aa5e9b47 11475 if (new_conn_state->crtc != crtc)
e7c84544
ML
11476 continue;
11477
749d98b8
ML
11478 if (crtc)
11479 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11480
11481 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 11482
aa5e9b47 11483 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 11484 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11485 }
91d1b4bd
DV
11486}
11487
11488static void
86b04268 11489verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
DV
11490{
11491 struct intel_encoder *encoder;
86b04268
DV
11492 struct drm_connector *connector;
11493 struct drm_connector_state *old_conn_state, *new_conn_state;
11494 int i;
8af6cf88 11495
b2784e15 11496 for_each_intel_encoder(dev, encoder) {
86b04268 11497 bool enabled = false, found = false;
4d20cd86 11498 enum pipe pipe;
8af6cf88
DV
11499
11500 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11501 encoder->base.base.id,
8e329a03 11502 encoder->base.name);
8af6cf88 11503
86b04268
DV
11504 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11505 new_conn_state, i) {
11506 if (old_conn_state->best_encoder == &encoder->base)
11507 found = true;
11508
11509 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 11510 continue;
86b04268 11511 found = enabled = true;
ad3c558f 11512
86b04268 11513 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
11514 encoder->base.crtc,
11515 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 11516 }
86b04268
DV
11517
11518 if (!found)
11519 continue;
0e32b39c 11520
e2c719b7 11521 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11522 "encoder's enabled state mismatch "
11523 "(expected %i, found %i)\n",
11524 !!encoder->base.crtc, enabled);
7c60d198
ML
11525
11526 if (!encoder->base.crtc) {
4d20cd86 11527 bool active;
7c60d198 11528
4d20cd86
ML
11529 active = encoder->get_hw_state(encoder, &pipe);
11530 I915_STATE_WARN(active,
11531 "encoder detached but still enabled on pipe %c.\n",
11532 pipe_name(pipe));
7c60d198 11533 }
8af6cf88 11534 }
91d1b4bd
DV
11535}
11536
11537static void
c0ead703
ML
11538verify_crtc_state(struct drm_crtc *crtc,
11539 struct drm_crtc_state *old_crtc_state,
11540 struct drm_crtc_state *new_crtc_state)
91d1b4bd 11541{
e7c84544 11542 struct drm_device *dev = crtc->dev;
fac5e23e 11543 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 11544 struct intel_encoder *encoder;
e7c84544
ML
11545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11546 struct intel_crtc_state *pipe_config, *sw_config;
11547 struct drm_atomic_state *old_state;
11548 bool active;
045ac3b5 11549
e7c84544 11550 old_state = old_crtc_state->state;
ec2dc6a0 11551 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
11552 pipe_config = to_intel_crtc_state(old_crtc_state);
11553 memset(pipe_config, 0, sizeof(*pipe_config));
11554 pipe_config->base.crtc = crtc;
11555 pipe_config->base.state = old_state;
8af6cf88 11556
78108b7c 11557 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 11558
e7c84544 11559 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 11560
e56134bc
VS
11561 /* we keep both pipes enabled on 830 */
11562 if (IS_I830(dev_priv))
e7c84544 11563 active = new_crtc_state->active;
6c49f241 11564
e7c84544
ML
11565 I915_STATE_WARN(new_crtc_state->active != active,
11566 "crtc active state doesn't match with hw state "
11567 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 11568
e7c84544
ML
11569 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
11570 "transitional active state does not match atomic hw state "
11571 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 11572
e7c84544
ML
11573 for_each_encoder_on_crtc(dev, crtc, encoder) {
11574 enum pipe pipe;
4d20cd86 11575
e7c84544
ML
11576 active = encoder->get_hw_state(encoder, &pipe);
11577 I915_STATE_WARN(active != new_crtc_state->active,
11578 "[ENCODER:%i] active %i with crtc active %i\n",
11579 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 11580
e7c84544
ML
11581 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
11582 "Encoder connected to wrong pipe %c\n",
11583 pipe_name(pipe));
4d20cd86 11584
253c84c8
VS
11585 if (active) {
11586 pipe_config->output_types |= 1 << encoder->type;
e7c84544 11587 encoder->get_config(encoder, pipe_config);
253c84c8 11588 }
e7c84544 11589 }
53d9f4e9 11590
a7d1b3f4
VS
11591 intel_crtc_compute_pixel_rate(pipe_config);
11592
e7c84544
ML
11593 if (!new_crtc_state->active)
11594 return;
cfb23ed6 11595
e7c84544 11596 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 11597
749d98b8 11598 sw_config = to_intel_crtc_state(new_crtc_state);
6315b5d3 11599 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
11600 pipe_config, false)) {
11601 I915_STATE_WARN(1, "pipe state doesn't match!\n");
11602 intel_dump_pipe_config(intel_crtc, pipe_config,
11603 "[hw state]");
11604 intel_dump_pipe_config(intel_crtc, sw_config,
11605 "[sw state]");
8af6cf88
DV
11606 }
11607}
11608
91d1b4bd 11609static void
c0ead703
ML
11610verify_single_dpll_state(struct drm_i915_private *dev_priv,
11611 struct intel_shared_dpll *pll,
11612 struct drm_crtc *crtc,
11613 struct drm_crtc_state *new_state)
91d1b4bd 11614{
91d1b4bd 11615 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
11616 unsigned crtc_mask;
11617 bool active;
5358901f 11618
e7c84544 11619 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 11620
e7c84544 11621 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 11622
e7c84544 11623 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 11624
e7c84544
ML
11625 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
11626 I915_STATE_WARN(!pll->on && pll->active_mask,
11627 "pll in active use but not on in sw tracking\n");
11628 I915_STATE_WARN(pll->on && !pll->active_mask,
11629 "pll is on but not used by any active crtc\n");
11630 I915_STATE_WARN(pll->on != active,
11631 "pll on state mismatch (expected %i, found %i)\n",
11632 pll->on, active);
11633 }
5358901f 11634
e7c84544 11635 if (!crtc) {
2c42e535 11636 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 11637 "more active pll users than references: %x vs %x\n",
2c42e535 11638 pll->active_mask, pll->state.crtc_mask);
5358901f 11639
e7c84544
ML
11640 return;
11641 }
11642
11643 crtc_mask = 1 << drm_crtc_index(crtc);
11644
11645 if (new_state->active)
11646 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
11647 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
11648 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
11649 else
11650 I915_STATE_WARN(pll->active_mask & crtc_mask,
11651 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
11652 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 11653
2c42e535 11654 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 11655 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 11656 crtc_mask, pll->state.crtc_mask);
66e985c0 11657
2c42e535 11658 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
11659 &dpll_hw_state,
11660 sizeof(dpll_hw_state)),
11661 "pll hw state mismatch\n");
11662}
11663
11664static void
c0ead703
ML
11665verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
11666 struct drm_crtc_state *old_crtc_state,
11667 struct drm_crtc_state *new_crtc_state)
e7c84544 11668{
fac5e23e 11669 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
11670 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
11671 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
11672
11673 if (new_state->shared_dpll)
c0ead703 11674 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
11675
11676 if (old_state->shared_dpll &&
11677 old_state->shared_dpll != new_state->shared_dpll) {
11678 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
11679 struct intel_shared_dpll *pll = old_state->shared_dpll;
11680
11681 I915_STATE_WARN(pll->active_mask & crtc_mask,
11682 "pll active mismatch (didn't expect pipe %c in active mask)\n",
11683 pipe_name(drm_crtc_index(crtc)));
2c42e535 11684 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
11685 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
11686 pipe_name(drm_crtc_index(crtc)));
5358901f 11687 }
8af6cf88
DV
11688}
11689
e7c84544 11690static void
c0ead703 11691intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
11692 struct drm_atomic_state *state,
11693 struct drm_crtc_state *old_state,
11694 struct drm_crtc_state *new_state)
e7c84544 11695{
5a21b665
DV
11696 if (!needs_modeset(new_state) &&
11697 !to_intel_crtc_state(new_state)->update_pipe)
11698 return;
11699
c0ead703 11700 verify_wm_state(crtc, new_state);
677100ce 11701 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
11702 verify_crtc_state(crtc, old_state, new_state);
11703 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
11704}
11705
11706static void
c0ead703 11707verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 11708{
fac5e23e 11709 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
11710 int i;
11711
11712 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 11713 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
11714}
11715
11716static void
677100ce
ML
11717intel_modeset_verify_disabled(struct drm_device *dev,
11718 struct drm_atomic_state *state)
e7c84544 11719{
86b04268 11720 verify_encoder_state(dev, state);
677100ce 11721 verify_connector_state(dev, state, NULL);
c0ead703 11722 verify_disabled_dpll_state(dev);
e7c84544
ML
11723}
11724
80715b2f
VS
11725static void update_scanline_offset(struct intel_crtc *crtc)
11726{
4f8036a2 11727 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
11728
11729 /*
11730 * The scanline counter increments at the leading edge of hsync.
11731 *
11732 * On most platforms it starts counting from vtotal-1 on the
11733 * first active line. That means the scanline counter value is
11734 * always one less than what we would expect. Ie. just after
11735 * start of vblank, which also occurs at start of hsync (on the
11736 * last active line), the scanline counter will read vblank_start-1.
11737 *
11738 * On gen2 the scanline counter starts counting from 1 instead
11739 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11740 * to keep the value positive), instead of adding one.
11741 *
11742 * On HSW+ the behaviour of the scanline counter depends on the output
11743 * type. For DP ports it behaves like most other platforms, but on HDMI
11744 * there's an extra 1 line difference. So we need to add two instead of
11745 * one to the value.
ec1b4ee2
VS
11746 *
11747 * On VLV/CHV DSI the scanline counter would appear to increment
11748 * approx. 1/3 of a scanline before start of vblank. Unfortunately
11749 * that means we can't tell whether we're in vblank or not while
11750 * we're on that particular line. We must still set scanline_offset
11751 * to 1 so that the vblank timestamps come out correct when we query
11752 * the scanline counter from within the vblank interrupt handler.
11753 * However if queried just before the start of vblank we'll get an
11754 * answer that's slightly in the future.
80715b2f 11755 */
4f8036a2 11756 if (IS_GEN2(dev_priv)) {
124abe07 11757 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
11758 int vtotal;
11759
124abe07
VS
11760 vtotal = adjusted_mode->crtc_vtotal;
11761 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
11762 vtotal /= 2;
11763
11764 crtc->scanline_offset = vtotal - 1;
4f8036a2 11765 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 11766 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
11767 crtc->scanline_offset = 2;
11768 } else
11769 crtc->scanline_offset = 1;
11770}
11771
ad421372 11772static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 11773{
225da59b 11774 struct drm_device *dev = state->dev;
ed6739ef 11775 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 11776 struct drm_crtc *crtc;
aa5e9b47 11777 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 11778 int i;
ed6739ef
ACO
11779
11780 if (!dev_priv->display.crtc_compute_clock)
ad421372 11781 return;
ed6739ef 11782
aa5e9b47 11783 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 11784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 11785 struct intel_shared_dpll *old_dpll =
aa5e9b47 11786 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 11787
aa5e9b47 11788 if (!needs_modeset(new_crtc_state))
225da59b
ACO
11789 continue;
11790
aa5e9b47 11791 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 11792
8106ddbd 11793 if (!old_dpll)
fb1a38a9 11794 continue;
0a9ab303 11795
a1c414ee 11796 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 11797 }
ed6739ef
ACO
11798}
11799
99d736a2
ML
11800/*
11801 * This implements the workaround described in the "notes" section of the mode
11802 * set sequence documentation. When going from no pipes or single pipe to
11803 * multiple pipes, and planes are enabled after the pipe, we need to wait at
11804 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
11805 */
11806static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
11807{
11808 struct drm_crtc_state *crtc_state;
11809 struct intel_crtc *intel_crtc;
11810 struct drm_crtc *crtc;
11811 struct intel_crtc_state *first_crtc_state = NULL;
11812 struct intel_crtc_state *other_crtc_state = NULL;
11813 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
11814 int i;
11815
11816 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 11817 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
11818 intel_crtc = to_intel_crtc(crtc);
11819
11820 if (!crtc_state->active || !needs_modeset(crtc_state))
11821 continue;
11822
11823 if (first_crtc_state) {
11824 other_crtc_state = to_intel_crtc_state(crtc_state);
11825 break;
11826 } else {
11827 first_crtc_state = to_intel_crtc_state(crtc_state);
11828 first_pipe = intel_crtc->pipe;
11829 }
11830 }
11831
11832 /* No workaround needed? */
11833 if (!first_crtc_state)
11834 return 0;
11835
11836 /* w/a possibly needed, check how many crtc's are already enabled. */
11837 for_each_intel_crtc(state->dev, intel_crtc) {
11838 struct intel_crtc_state *pipe_config;
11839
11840 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11841 if (IS_ERR(pipe_config))
11842 return PTR_ERR(pipe_config);
11843
11844 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
11845
11846 if (!pipe_config->base.active ||
11847 needs_modeset(&pipe_config->base))
11848 continue;
11849
11850 /* 2 or more enabled crtcs means no need for w/a */
11851 if (enabled_pipe != INVALID_PIPE)
11852 return 0;
11853
11854 enabled_pipe = intel_crtc->pipe;
11855 }
11856
11857 if (enabled_pipe != INVALID_PIPE)
11858 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
11859 else if (other_crtc_state)
11860 other_crtc_state->hsw_workaround_pipe = first_pipe;
11861
11862 return 0;
11863}
11864
8d96561a
VS
11865static int intel_lock_all_pipes(struct drm_atomic_state *state)
11866{
11867 struct drm_crtc *crtc;
11868
11869 /* Add all pipes to the state */
11870 for_each_crtc(state->dev, crtc) {
11871 struct drm_crtc_state *crtc_state;
11872
11873 crtc_state = drm_atomic_get_crtc_state(state, crtc);
11874 if (IS_ERR(crtc_state))
11875 return PTR_ERR(crtc_state);
11876 }
11877
11878 return 0;
11879}
11880
27c329ed
ML
11881static int intel_modeset_all_pipes(struct drm_atomic_state *state)
11882{
11883 struct drm_crtc *crtc;
27c329ed 11884
8d96561a
VS
11885 /*
11886 * Add all pipes to the state, and force
11887 * a modeset on all the active ones.
11888 */
27c329ed 11889 for_each_crtc(state->dev, crtc) {
9780aad5
VS
11890 struct drm_crtc_state *crtc_state;
11891 int ret;
11892
27c329ed
ML
11893 crtc_state = drm_atomic_get_crtc_state(state, crtc);
11894 if (IS_ERR(crtc_state))
11895 return PTR_ERR(crtc_state);
11896
11897 if (!crtc_state->active || needs_modeset(crtc_state))
11898 continue;
11899
11900 crtc_state->mode_changed = true;
11901
11902 ret = drm_atomic_add_affected_connectors(state, crtc);
11903 if (ret)
9780aad5 11904 return ret;
27c329ed
ML
11905
11906 ret = drm_atomic_add_affected_planes(state, crtc);
11907 if (ret)
9780aad5 11908 return ret;
27c329ed
ML
11909 }
11910
9780aad5 11911 return 0;
27c329ed
ML
11912}
11913
c347a676 11914static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 11915{
565602d7 11916 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 11917 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 11918 struct drm_crtc *crtc;
aa5e9b47 11919 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 11920 int ret = 0, i;
054518dd 11921
b359283a
ML
11922 if (!check_digital_port_conflicts(state)) {
11923 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11924 return -EINVAL;
11925 }
11926
68b342c9
VS
11927 /* keep the current setting */
11928 if (!intel_state->cdclk.force_min_cdclk_changed)
11929 intel_state->cdclk.force_min_cdclk =
11930 dev_priv->cdclk.force_min_cdclk;
11931
11932
565602d7
ML
11933 intel_state->modeset = true;
11934 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
11935 intel_state->cdclk.logical = dev_priv->cdclk.logical;
11936 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 11937
aa5e9b47
ML
11938 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11939 if (new_crtc_state->active)
565602d7
ML
11940 intel_state->active_crtcs |= 1 << i;
11941 else
11942 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 11943
aa5e9b47 11944 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 11945 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
11946 }
11947
054518dd
ACO
11948 /*
11949 * See if the config requires any additional preparation, e.g.
11950 * to adjust global state with pipes off. We need to do this
11951 * here so we can get the modeset_pipe updated config for the new
11952 * mode set on this crtc. For other crtcs we need to use the
11953 * adjusted_mode bits in the crtc directly.
11954 */
27c329ed 11955 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 11956 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
11957 if (ret < 0)
11958 return ret;
27c329ed 11959
8d96561a 11960 /*
bb0f4aab 11961 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
11962 * holding all the crtc locks, even if we don't end up
11963 * touching the hardware
11964 */
bb0f4aab
VS
11965 if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
11966 &intel_state->cdclk.logical)) {
8d96561a
VS
11967 ret = intel_lock_all_pipes(state);
11968 if (ret < 0)
11969 return ret;
11970 }
11971
11972 /* All pipes must be switched off while we change the cdclk. */
bb0f4aab
VS
11973 if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
11974 &intel_state->cdclk.actual)) {
27c329ed 11975 ret = intel_modeset_all_pipes(state);
8d96561a
VS
11976 if (ret < 0)
11977 return ret;
11978 }
e8788cbc 11979
bb0f4aab
VS
11980 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
11981 intel_state->cdclk.logical.cdclk,
11982 intel_state->cdclk.actual.cdclk);
e0ca7a6b 11983 } else {
bb0f4aab 11984 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 11985 }
054518dd 11986
ad421372 11987 intel_modeset_clear_plls(state);
054518dd 11988
565602d7 11989 if (IS_HASWELL(dev_priv))
ad421372 11990 return haswell_mode_set_planes_workaround(state);
99d736a2 11991
ad421372 11992 return 0;
c347a676
ACO
11993}
11994
aa363136
MR
11995/*
11996 * Handle calculation of various watermark data at the end of the atomic check
11997 * phase. The code here should be run after the per-crtc and per-plane 'check'
11998 * handlers to ensure that all derived state has been updated.
11999 */
55994c2c 12000static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12001{
12002 struct drm_device *dev = state->dev;
98d39494 12003 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12004
12005 /* Is there platform-specific watermark information to calculate? */
12006 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12007 return dev_priv->display.compute_global_watermarks(state);
12008
12009 return 0;
aa363136
MR
12010}
12011
74c090b1
ML
12012/**
12013 * intel_atomic_check - validate state object
12014 * @dev: drm device
12015 * @state: state to validate
12016 */
12017static int intel_atomic_check(struct drm_device *dev,
12018 struct drm_atomic_state *state)
c347a676 12019{
dd8b3bdb 12020 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12021 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12022 struct drm_crtc *crtc;
aa5e9b47 12023 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12024 int ret, i;
68b342c9 12025 bool any_ms = intel_state->cdclk.force_min_cdclk_changed;
c347a676 12026
74c090b1 12027 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12028 if (ret)
12029 return ret;
12030
aa5e9b47 12031 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12032 struct intel_crtc_state *pipe_config =
12033 to_intel_crtc_state(crtc_state);
1ed51de9
DV
12034
12035 /* Catch I915_MODE_FLAG_INHERITED */
aa5e9b47 12036 if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
1ed51de9 12037 crtc_state->mode_changed = true;
cfb23ed6 12038
af4a879e 12039 if (!needs_modeset(crtc_state))
c347a676
ACO
12040 continue;
12041
af4a879e
DV
12042 if (!crtc_state->enable) {
12043 any_ms = true;
cfb23ed6 12044 continue;
af4a879e 12045 }
cfb23ed6 12046
26495481
DV
12047 /* FIXME: For only active_changed we shouldn't need to do any
12048 * state recomputation at all. */
12049
1ed51de9
DV
12050 ret = drm_atomic_add_affected_connectors(state, crtc);
12051 if (ret)
12052 return ret;
b359283a 12053
cfb23ed6 12054 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12055 if (ret) {
12056 intel_dump_pipe_config(to_intel_crtc(crtc),
12057 pipe_config, "[failed]");
c347a676 12058 return ret;
25aa1c39 12059 }
c347a676 12060
4f044a88 12061 if (i915_modparams.fastboot &&
6315b5d3 12062 intel_pipe_config_compare(dev_priv,
aa5e9b47 12063 to_intel_crtc_state(old_crtc_state),
1ed51de9 12064 pipe_config, true)) {
26495481 12065 crtc_state->mode_changed = false;
aa5e9b47 12066 pipe_config->update_pipe = true;
26495481
DV
12067 }
12068
af4a879e 12069 if (needs_modeset(crtc_state))
26495481 12070 any_ms = true;
cfb23ed6 12071
af4a879e
DV
12072 ret = drm_atomic_add_affected_planes(state, crtc);
12073 if (ret)
12074 return ret;
61333b60 12075
26495481
DV
12076 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12077 needs_modeset(crtc_state) ?
12078 "[modeset]" : "[fastset]");
c347a676
ACO
12079 }
12080
61333b60
ML
12081 if (any_ms) {
12082 ret = intel_modeset_checks(state);
12083
12084 if (ret)
12085 return ret;
e0ca7a6b 12086 } else {
bb0f4aab 12087 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12088 }
76305b1a 12089
dd8b3bdb 12090 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12091 if (ret)
12092 return ret;
12093
f51be2e0 12094 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 12095 return calc_watermark_data(state);
054518dd
ACO
12096}
12097
5008e874 12098static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12099 struct drm_atomic_state *state)
5008e874 12100{
fd70075f 12101 return drm_atomic_helper_prepare_planes(dev, state);
5008e874
ML
12102}
12103
a2991414
ML
12104u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12105{
12106 struct drm_device *dev = crtc->base.dev;
12107
12108 if (!dev->max_vblank_count)
ca814b25 12109 return drm_crtc_accurate_vblank_count(&crtc->base);
a2991414
ML
12110
12111 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12112}
12113
896e5bb0
L
12114static void intel_update_crtc(struct drm_crtc *crtc,
12115 struct drm_atomic_state *state,
12116 struct drm_crtc_state *old_crtc_state,
b44d5c0c 12117 struct drm_crtc_state *new_crtc_state)
896e5bb0
L
12118{
12119 struct drm_device *dev = crtc->dev;
12120 struct drm_i915_private *dev_priv = to_i915(dev);
12121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12122 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12123 bool modeset = needs_modeset(new_crtc_state);
896e5bb0
L
12124
12125 if (modeset) {
12126 update_scanline_offset(intel_crtc);
12127 dev_priv->display.crtc_enable(pipe_config, state);
12128 } else {
aa5e9b47
ML
12129 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12130 pipe_config);
896e5bb0
L
12131 }
12132
12133 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12134 intel_fbc_enable(
12135 intel_crtc, pipe_config,
12136 to_intel_plane_state(crtc->primary->state));
12137 }
12138
12139 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
896e5bb0
L
12140}
12141
b44d5c0c 12142static void intel_update_crtcs(struct drm_atomic_state *state)
896e5bb0
L
12143{
12144 struct drm_crtc *crtc;
aa5e9b47 12145 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12146 int i;
12147
aa5e9b47
ML
12148 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12149 if (!new_crtc_state->active)
896e5bb0
L
12150 continue;
12151
12152 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12153 new_crtc_state);
896e5bb0
L
12154 }
12155}
12156
b44d5c0c 12157static void skl_update_crtcs(struct drm_atomic_state *state)
27082493 12158{
0f0f74bc 12159 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12160 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12161 struct drm_crtc *crtc;
ce0ba283 12162 struct intel_crtc *intel_crtc;
aa5e9b47 12163 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12164 struct intel_crtc_state *cstate;
27082493
L
12165 unsigned int updated = 0;
12166 bool progress;
12167 enum pipe pipe;
5eff503b
ML
12168 int i;
12169
12170 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12171
aa5e9b47 12172 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12173 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12174 if (new_crtc_state->active)
5eff503b 12175 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
12176
12177 /*
12178 * Whenever the number of active pipes changes, we need to make sure we
12179 * update the pipes in the right order so that their ddb allocations
12180 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12181 * cause pipe underruns and other bad stuff.
12182 */
12183 do {
27082493
L
12184 progress = false;
12185
aa5e9b47 12186 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12187 bool vbl_wait = false;
12188 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12189
12190 intel_crtc = to_intel_crtc(crtc);
21794813 12191 cstate = to_intel_crtc_state(new_crtc_state);
ce0ba283 12192 pipe = intel_crtc->pipe;
27082493 12193
5eff503b 12194 if (updated & cmask || !cstate->base.active)
27082493 12195 continue;
5eff503b 12196
2b68504b
MK
12197 if (skl_ddb_allocation_overlaps(dev_priv,
12198 entries,
12199 &cstate->wm.skl.ddb,
12200 i))
27082493
L
12201 continue;
12202
12203 updated |= cmask;
5eff503b 12204 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12205
12206 /*
12207 * If this is an already active pipe, it's DDB changed,
12208 * and this isn't the last pipe that needs updating
12209 * then we need to wait for a vblank to pass for the
12210 * new ddb allocation to take effect.
12211 */
ce0ba283 12212 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12213 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12214 !new_crtc_state->active_changed &&
27082493
L
12215 intel_state->wm_results.dirty_pipes != updated)
12216 vbl_wait = true;
12217
12218 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12219 new_crtc_state);
27082493
L
12220
12221 if (vbl_wait)
0f0f74bc 12222 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12223
12224 progress = true;
12225 }
12226 } while (progress);
12227}
12228
ba318c61
CW
12229static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12230{
12231 struct intel_atomic_state *state, *next;
12232 struct llist_node *freed;
12233
12234 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12235 llist_for_each_entry_safe(state, next, freed, freed)
12236 drm_atomic_state_put(&state->base);
12237}
12238
12239static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12240{
12241 struct drm_i915_private *dev_priv =
12242 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12243
12244 intel_atomic_helper_free_state(dev_priv);
12245}
12246
9db529aa
DV
12247static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12248{
12249 struct wait_queue_entry wait_fence, wait_reset;
12250 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12251
12252 init_wait_entry(&wait_fence, 0);
12253 init_wait_entry(&wait_reset, 0);
12254 for (;;) {
12255 prepare_to_wait(&intel_state->commit_ready.wait,
12256 &wait_fence, TASK_UNINTERRUPTIBLE);
12257 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12258 &wait_reset, TASK_UNINTERRUPTIBLE);
12259
12260
12261 if (i915_sw_fence_done(&intel_state->commit_ready)
12262 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12263 break;
12264
12265 schedule();
12266 }
12267 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12268 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12269}
12270
94f05024 12271static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12272{
94f05024 12273 struct drm_device *dev = state->dev;
565602d7 12274 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12275 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 12276 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 12277 struct drm_crtc *crtc;
5a21b665 12278 struct intel_crtc_state *intel_cstate;
d8fc70b7 12279 u64 put_domains[I915_MAX_PIPES] = {};
e95433c7 12280 int i;
a6778b3c 12281
9db529aa 12282 intel_atomic_commit_fence_wait(intel_state);
42b062b0 12283
ea0000f0
DV
12284 drm_atomic_helper_wait_for_dependencies(state);
12285
c3b32658 12286 if (intel_state->modeset)
5a21b665 12287 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12288
aa5e9b47 12289 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
12290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12291
aa5e9b47
ML
12292 if (needs_modeset(new_crtc_state) ||
12293 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
DV
12294
12295 put_domains[to_intel_crtc(crtc)->pipe] =
12296 modeset_get_crtc_power_domains(crtc,
aa5e9b47 12297 to_intel_crtc_state(new_crtc_state));
5a21b665
DV
12298 }
12299
aa5e9b47 12300 if (!needs_modeset(new_crtc_state))
61333b60
ML
12301 continue;
12302
aa5e9b47
ML
12303 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12304 to_intel_crtc_state(new_crtc_state));
460da916 12305
29ceb0e6
VS
12306 if (old_crtc_state->active) {
12307 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 12308 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12309 intel_crtc->active = false;
58f9c0bc 12310 intel_fbc_disable(intel_crtc);
eddfcbcd 12311 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12312
12313 /*
12314 * Underruns don't always raise
12315 * interrupts, so check manually.
12316 */
12317 intel_check_cpu_fifo_underruns(dev_priv);
12318 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12319
21794813 12320 if (!new_crtc_state->active) {
e62929b3
ML
12321 /*
12322 * Make sure we don't call initial_watermarks
12323 * for ILK-style watermark updates.
ff32c54e
VS
12324 *
12325 * No clue what this is supposed to achieve.
e62929b3 12326 */
ff32c54e 12327 if (INTEL_GEN(dev_priv) >= 9)
e62929b3 12328 dev_priv->display.initial_watermarks(intel_state,
21794813 12329 to_intel_crtc_state(new_crtc_state));
e62929b3 12330 }
a539205a 12331 }
b8cecdf5 12332 }
7758a113 12333
ea9d758d
DV
12334 /* Only after disabling all output pipelines that will be changed can we
12335 * update the the output configuration. */
4740b0f2 12336 intel_modeset_update_crtc_state(state);
f6e5b160 12337
565602d7 12338 if (intel_state->modeset) {
4740b0f2 12339 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12340
b0587e4d 12341 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12342
656d1b89
L
12343 /*
12344 * SKL workaround: bspec recommends we disable the SAGV when we
12345 * have more then one pipe enabled
12346 */
56feca91 12347 if (!intel_can_enable_sagv(state))
16dcdc4e 12348 intel_disable_sagv(dev_priv);
656d1b89 12349
677100ce 12350 intel_modeset_verify_disabled(dev, state);
4740b0f2 12351 }
47fab737 12352
896e5bb0 12353 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
12354 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12355 bool modeset = needs_modeset(new_crtc_state);
80715b2f 12356
1f7528c4 12357 /* Complete events for now disable pipes here. */
aa5e9b47 12358 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 12359 spin_lock_irq(&dev->event_lock);
aa5e9b47 12360 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
DV
12361 spin_unlock_irq(&dev->event_lock);
12362
aa5e9b47 12363 new_crtc_state->event = NULL;
1f7528c4 12364 }
177246a8
MR
12365 }
12366
896e5bb0 12367 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
b44d5c0c 12368 dev_priv->display.update_crtcs(state);
896e5bb0 12369
94f05024
DV
12370 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12371 * already, but still need the state for the delayed optimization. To
12372 * fix this:
12373 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12374 * - schedule that vblank worker _before_ calling hw_done
12375 * - at the start of commit_tail, cancel it _synchrously
12376 * - switch over to the vblank wait helper in the core after that since
12377 * we don't need out special handling any more.
12378 */
b44d5c0c 12379 drm_atomic_helper_wait_for_flip_done(dev, state);
5a21b665
DV
12380
12381 /*
12382 * Now that the vblank has passed, we can go ahead and program the
12383 * optimal watermarks on platforms that need two-step watermark
12384 * programming.
12385 *
12386 * TODO: Move this (and other cleanup) to an async worker eventually.
12387 */
aa5e9b47
ML
12388 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12389 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
DV
12390
12391 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12392 dev_priv->display.optimize_watermarks(intel_state,
12393 intel_cstate);
5a21b665
DV
12394 }
12395
aa5e9b47 12396 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
DV
12397 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12398
12399 if (put_domains[i])
12400 modeset_put_power_domains(dev_priv, put_domains[i]);
12401
aa5e9b47 12402 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
DV
12403 }
12404
56feca91 12405 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12406 intel_enable_sagv(dev_priv);
656d1b89 12407
94f05024
DV
12408 drm_atomic_helper_commit_hw_done(state);
12409
d5553c09
CW
12410 if (intel_state->modeset) {
12411 /* As one of the primary mmio accessors, KMS has a high
12412 * likelihood of triggering bugs in unclaimed access. After we
12413 * finish modesetting, see if an error has been flagged, and if
12414 * so enable debugging for the next modeset - and hope we catch
12415 * the culprit.
12416 */
12417 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
5a21b665 12418 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
d5553c09 12419 }
5a21b665 12420
5a21b665 12421 drm_atomic_helper_cleanup_planes(dev, state);
5a21b665 12422
ea0000f0
DV
12423 drm_atomic_helper_commit_cleanup_done(state);
12424
0853695c 12425 drm_atomic_state_put(state);
f30da187 12426
ba318c61 12427 intel_atomic_helper_free_state(dev_priv);
94f05024
DV
12428}
12429
12430static void intel_atomic_commit_work(struct work_struct *work)
12431{
c004a90b
CW
12432 struct drm_atomic_state *state =
12433 container_of(work, struct drm_atomic_state, commit_work);
12434
94f05024
DV
12435 intel_atomic_commit_tail(state);
12436}
12437
c004a90b
CW
12438static int __i915_sw_fence_call
12439intel_atomic_commit_ready(struct i915_sw_fence *fence,
12440 enum i915_sw_fence_notify notify)
12441{
12442 struct intel_atomic_state *state =
12443 container_of(fence, struct intel_atomic_state, commit_ready);
12444
12445 switch (notify) {
12446 case FENCE_COMPLETE:
42b062b0 12447 /* we do blocking waits in the worker, nothing to do here */
c004a90b 12448 break;
c004a90b 12449 case FENCE_FREE:
eb955eee
CW
12450 {
12451 struct intel_atomic_helper *helper =
12452 &to_i915(state->base.dev)->atomic_helper;
12453
12454 if (llist_add(&state->freed, &helper->free_list))
12455 schedule_work(&helper->free_work);
12456 break;
12457 }
c004a90b
CW
12458 }
12459
12460 return NOTIFY_DONE;
12461}
12462
6c9c1b38
DV
12463static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12464{
aa5e9b47 12465 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 12466 struct drm_plane *plane;
6c9c1b38
DV
12467 int i;
12468
aa5e9b47 12469 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 12470 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 12471 intel_fb_obj(new_plane_state->fb),
faf5bf0a 12472 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
DV
12473}
12474
94f05024
DV
12475/**
12476 * intel_atomic_commit - commit validated state object
12477 * @dev: DRM device
12478 * @state: the top-level driver state object
12479 * @nonblock: nonblocking commit
12480 *
12481 * This function commits a top-level state object that has been validated
12482 * with drm_atomic_helper_check().
12483 *
94f05024
DV
12484 * RETURNS
12485 * Zero for success or -errno.
12486 */
12487static int intel_atomic_commit(struct drm_device *dev,
12488 struct drm_atomic_state *state,
12489 bool nonblock)
12490{
12491 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12492 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
12493 int ret = 0;
12494
c004a90b
CW
12495 drm_atomic_state_get(state);
12496 i915_sw_fence_init(&intel_state->commit_ready,
12497 intel_atomic_commit_ready);
94f05024 12498
440df938
VS
12499 /*
12500 * The intel_legacy_cursor_update() fast path takes care
12501 * of avoiding the vblank waits for simple cursor
12502 * movement and flips. For cursor on/off and size changes,
12503 * we want to perform the vblank waits so that watermark
12504 * updates happen during the correct frames. Gen9+ have
12505 * double buffered watermarks and so shouldn't need this.
12506 *
3cf50c63
ML
12507 * Unset state->legacy_cursor_update before the call to
12508 * drm_atomic_helper_setup_commit() because otherwise
12509 * drm_atomic_helper_wait_for_flip_done() is a noop and
12510 * we get FIFO underruns because we didn't wait
12511 * for vblank.
440df938
VS
12512 *
12513 * FIXME doing watermarks and fb cleanup from a vblank worker
12514 * (assuming we had any) would solve these problems.
12515 */
213f1bd0
ML
12516 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
12517 struct intel_crtc_state *new_crtc_state;
12518 struct intel_crtc *crtc;
12519 int i;
12520
12521 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
12522 if (new_crtc_state->wm.need_postvbl_update ||
12523 new_crtc_state->update_wm_post)
12524 state->legacy_cursor_update = false;
12525 }
440df938 12526
3cf50c63
ML
12527 ret = intel_atomic_prepare_commit(dev, state);
12528 if (ret) {
12529 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
12530 i915_sw_fence_commit(&intel_state->commit_ready);
12531 return ret;
12532 }
12533
12534 ret = drm_atomic_helper_setup_commit(state, nonblock);
12535 if (!ret)
12536 ret = drm_atomic_helper_swap_state(state, true);
440df938 12537
0806f4ee
ML
12538 if (ret) {
12539 i915_sw_fence_commit(&intel_state->commit_ready);
12540
0806f4ee 12541 drm_atomic_helper_cleanup_planes(dev, state);
0806f4ee
ML
12542 return ret;
12543 }
94f05024 12544 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 12545 intel_shared_dpll_swap_state(state);
6c9c1b38 12546 intel_atomic_track_fbs(state);
94f05024 12547
c3b32658 12548 if (intel_state->modeset) {
d305e061
VS
12549 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
12550 sizeof(intel_state->min_cdclk));
c3b32658 12551 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
12552 dev_priv->cdclk.logical = intel_state->cdclk.logical;
12553 dev_priv->cdclk.actual = intel_state->cdclk.actual;
68b342c9
VS
12554 dev_priv->cdclk.force_min_cdclk =
12555 intel_state->cdclk.force_min_cdclk;
c3b32658
ML
12556 }
12557
0853695c 12558 drm_atomic_state_get(state);
42b062b0 12559 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
c004a90b
CW
12560
12561 i915_sw_fence_commit(&intel_state->commit_ready);
eda41bdc
VS
12562 if (nonblock && intel_state->modeset) {
12563 queue_work(dev_priv->modeset_wq, &state->commit_work);
12564 } else if (nonblock) {
42b062b0 12565 queue_work(system_unbound_wq, &state->commit_work);
eda41bdc
VS
12566 } else {
12567 if (intel_state->modeset)
12568 flush_workqueue(dev_priv->modeset_wq);
94f05024 12569 intel_atomic_commit_tail(state);
eda41bdc 12570 }
75714940 12571
74c090b1 12572 return 0;
7f27126e
JB
12573}
12574
f6e5b160 12575static const struct drm_crtc_funcs intel_crtc_funcs = {
3fab2f09 12576 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 12577 .set_config = drm_atomic_helper_set_config,
f6e5b160 12578 .destroy = intel_crtc_destroy,
4c01ded5 12579 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
12580 .atomic_duplicate_state = intel_crtc_duplicate_state,
12581 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 12582 .set_crc_source = intel_crtc_set_crc_source,
f6e5b160
CW
12583};
12584
74d290f8
CW
12585struct wait_rps_boost {
12586 struct wait_queue_entry wait;
12587
12588 struct drm_crtc *crtc;
12589 struct drm_i915_gem_request *request;
12590};
12591
12592static int do_rps_boost(struct wait_queue_entry *_wait,
12593 unsigned mode, int sync, void *key)
12594{
12595 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
12596 struct drm_i915_gem_request *rq = wait->request;
12597
12598 gen6_rps_boost(rq, NULL);
12599 i915_gem_request_put(rq);
12600
12601 drm_crtc_vblank_put(wait->crtc);
12602
12603 list_del(&wait->wait.entry);
12604 kfree(wait);
12605 return 1;
12606}
12607
12608static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
12609 struct dma_fence *fence)
12610{
12611 struct wait_rps_boost *wait;
12612
12613 if (!dma_fence_is_i915(fence))
12614 return;
12615
12616 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
12617 return;
12618
12619 if (drm_crtc_vblank_get(crtc))
12620 return;
12621
12622 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
12623 if (!wait) {
12624 drm_crtc_vblank_put(crtc);
12625 return;
12626 }
12627
12628 wait->request = to_request(dma_fence_get(fence));
12629 wait->crtc = crtc;
12630
12631 wait->wait.func = do_rps_boost;
12632 wait->wait.flags = 0;
12633
12634 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
12635}
12636
6beb8c23
MR
12637/**
12638 * intel_prepare_plane_fb - Prepare fb for usage on plane
12639 * @plane: drm plane to prepare for
12640 * @fb: framebuffer to prepare for presentation
12641 *
12642 * Prepares a framebuffer for usage on a display plane. Generally this
12643 * involves pinning the underlying object and updating the frontbuffer tracking
12644 * bits. Some older platforms need special physical address handling for
12645 * cursor planes.
12646 *
f935675f
ML
12647 * Must be called with struct_mutex held.
12648 *
6beb8c23
MR
12649 * Returns 0 on success, negative error code on failure.
12650 */
12651int
12652intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 12653 struct drm_plane_state *new_state)
465c120c 12654{
c004a90b
CW
12655 struct intel_atomic_state *intel_state =
12656 to_intel_atomic_state(new_state->state);
b7f05d4a 12657 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 12658 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 12659 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 12660 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 12661 int ret;
465c120c 12662
5008e874
ML
12663 if (old_obj) {
12664 struct drm_crtc_state *crtc_state =
c004a90b
CW
12665 drm_atomic_get_existing_crtc_state(new_state->state,
12666 plane->state->crtc);
5008e874
ML
12667
12668 /* Big Hammer, we also need to ensure that any pending
12669 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
12670 * current scanout is retired before unpinning the old
12671 * framebuffer. Note that we rely on userspace rendering
12672 * into the buffer attached to the pipe they are waiting
12673 * on. If not, userspace generates a GPU hang with IPEHR
12674 * point to the MI_WAIT_FOR_EVENT.
12675 *
12676 * This should only fail upon a hung GPU, in which case we
12677 * can safely continue.
12678 */
c004a90b
CW
12679 if (needs_modeset(crtc_state)) {
12680 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12681 old_obj->resv, NULL,
12682 false, 0,
12683 GFP_KERNEL);
12684 if (ret < 0)
12685 return ret;
f4457ae7 12686 }
5008e874
ML
12687 }
12688
c004a90b
CW
12689 if (new_state->fence) { /* explicit fencing */
12690 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
12691 new_state->fence,
12692 I915_FENCE_TIMEOUT,
12693 GFP_KERNEL);
12694 if (ret < 0)
12695 return ret;
12696 }
12697
c37efb99
CW
12698 if (!obj)
12699 return 0;
12700
4d3088c7 12701 ret = i915_gem_object_pin_pages(obj);
fd70075f
CW
12702 if (ret)
12703 return ret;
12704
4d3088c7
CW
12705 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
12706 if (ret) {
12707 i915_gem_object_unpin_pages(obj);
12708 return ret;
12709 }
12710
fd70075f
CW
12711 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12712 INTEL_INFO(dev_priv)->cursor_needs_physical) {
12713 const int align = intel_cursor_alignment(dev_priv);
12714
12715 ret = i915_gem_object_attach_phys(obj, align);
12716 } else {
12717 struct i915_vma *vma;
12718
12719 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
12720 if (!IS_ERR(vma))
12721 to_intel_plane_state(new_state)->vma = vma;
12722 else
12723 ret = PTR_ERR(vma);
12724 }
12725
12726 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
12727
12728 mutex_unlock(&dev_priv->drm.struct_mutex);
4d3088c7 12729 i915_gem_object_unpin_pages(obj);
fd70075f
CW
12730 if (ret)
12731 return ret;
12732
c004a90b 12733 if (!new_state->fence) { /* implicit fencing */
74d290f8
CW
12734 struct dma_fence *fence;
12735
c004a90b
CW
12736 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
12737 obj->resv, NULL,
12738 false, I915_FENCE_TIMEOUT,
12739 GFP_KERNEL);
12740 if (ret < 0)
12741 return ret;
74d290f8
CW
12742
12743 fence = reservation_object_get_excl_rcu(obj->resv);
12744 if (fence) {
12745 add_rps_boost_after_vblank(new_state->crtc, fence);
12746 dma_fence_put(fence);
12747 }
12748 } else {
12749 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
c004a90b 12750 }
5a21b665 12751
d07f0e59 12752 return 0;
6beb8c23
MR
12753}
12754
38f3ce3a
MR
12755/**
12756 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12757 * @plane: drm plane to clean up for
12758 * @fb: old framebuffer that was on plane
12759 *
12760 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
12761 *
12762 * Must be called with struct_mutex held.
38f3ce3a
MR
12763 */
12764void
12765intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 12766 struct drm_plane_state *old_state)
38f3ce3a 12767{
be1e3415 12768 struct i915_vma *vma;
38f3ce3a 12769
be1e3415
CW
12770 /* Should only be called after a successful intel_prepare_plane_fb()! */
12771 vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
fd70075f
CW
12772 if (vma) {
12773 mutex_lock(&plane->dev->struct_mutex);
be1e3415 12774 intel_unpin_fb_vma(vma);
fd70075f
CW
12775 mutex_unlock(&plane->dev->struct_mutex);
12776 }
465c120c
MR
12777}
12778
6156a456
CK
12779int
12780skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
12781{
5b7280f0 12782 struct drm_i915_private *dev_priv;
6156a456 12783 int max_scale;
5b7280f0 12784 int crtc_clock, max_dotclk;
6156a456 12785
bf8a0af0 12786 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
12787 return DRM_PLANE_HELPER_NO_SCALING;
12788
5b7280f0
ACO
12789 dev_priv = to_i915(intel_crtc->base.dev);
12790
6156a456 12791 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
12792 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
12793
12794 if (IS_GEMINILAKE(dev_priv))
12795 max_dotclk *= 2;
6156a456 12796
5b7280f0 12797 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
12798 return DRM_PLANE_HELPER_NO_SCALING;
12799
12800 /*
12801 * skl max scale is lower of:
12802 * close to 3 but not 3, -1 is for that purpose
12803 * or
12804 * cdclk/crtc_clock
12805 */
5b7280f0
ACO
12806 max_scale = min((1 << 16) * 3 - 1,
12807 (1 << 8) * ((max_dotclk << 8) / crtc_clock));
6156a456
CK
12808
12809 return max_scale;
12810}
12811
465c120c 12812static int
282dbf9b 12813intel_check_primary_plane(struct intel_plane *plane,
061e4b8d 12814 struct intel_crtc_state *crtc_state,
3c692a41
GP
12815 struct intel_plane_state *state)
12816{
282dbf9b 12817 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2b875c22 12818 struct drm_crtc *crtc = state->base.crtc;
6156a456 12819 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
12820 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
12821 bool can_position = false;
b63a16f6 12822 int ret;
465c120c 12823
b63a16f6 12824 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
12825 /* use scaler when colorkey is not required */
12826 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
12827 min_scale = 1;
12828 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
12829 }
d8106366 12830 can_position = true;
6156a456 12831 }
d8106366 12832
cc926387
DV
12833 ret = drm_plane_helper_check_state(&state->base,
12834 &state->clip,
12835 min_scale, max_scale,
12836 can_position, true);
b63a16f6
VS
12837 if (ret)
12838 return ret;
12839
cc926387 12840 if (!state->base.fb)
b63a16f6
VS
12841 return 0;
12842
12843 if (INTEL_GEN(dev_priv) >= 9) {
12844 ret = skl_check_plane_surface(state);
12845 if (ret)
12846 return ret;
a0864d59
VS
12847
12848 state->ctl = skl_plane_ctl(crtc_state, state);
12849 } else {
5b7fcc44
VS
12850 ret = i9xx_check_plane_surface(state);
12851 if (ret)
12852 return ret;
12853
a0864d59 12854 state->ctl = i9xx_plane_ctl(crtc_state, state);
b63a16f6
VS
12855 }
12856
12857 return 0;
14af293f
GP
12858}
12859
5a21b665
DV
12860static void intel_begin_crtc_commit(struct drm_crtc *crtc,
12861 struct drm_crtc_state *old_crtc_state)
12862{
12863 struct drm_device *dev = crtc->dev;
62e0fb88 12864 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 12865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ccf010fb 12866 struct intel_crtc_state *old_intel_cstate =
5a21b665 12867 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
12868 struct intel_atomic_state *old_intel_state =
12869 to_intel_atomic_state(old_crtc_state->state);
d3a8fb32
VS
12870 struct intel_crtc_state *intel_cstate =
12871 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
12872 bool modeset = needs_modeset(&intel_cstate->base);
5a21b665 12873
567f0792
ML
12874 if (!modeset &&
12875 (intel_cstate->base.color_mgmt_changed ||
12876 intel_cstate->update_pipe)) {
5c857e60
VS
12877 intel_color_set_csc(&intel_cstate->base);
12878 intel_color_load_luts(&intel_cstate->base);
567f0792
ML
12879 }
12880
5a21b665 12881 /* Perform vblank evasion around commit operation */
d3a8fb32 12882 intel_pipe_update_start(intel_cstate);
5a21b665
DV
12883
12884 if (modeset)
e62929b3 12885 goto out;
5a21b665 12886
ccf010fb 12887 if (intel_cstate->update_pipe)
1a15b77b 12888 intel_update_pipe_config(old_intel_cstate, intel_cstate);
ccf010fb 12889 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 12890 skl_detach_scalers(intel_crtc);
62e0fb88 12891
e62929b3 12892out:
ccf010fb
ML
12893 if (dev_priv->display.atomic_update_watermarks)
12894 dev_priv->display.atomic_update_watermarks(old_intel_state,
12895 intel_cstate);
5a21b665
DV
12896}
12897
12898static void intel_finish_crtc_commit(struct drm_crtc *crtc,
12899 struct drm_crtc_state *old_crtc_state)
12900{
12901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d3a8fb32
VS
12902 struct intel_atomic_state *old_intel_state =
12903 to_intel_atomic_state(old_crtc_state->state);
12904 struct intel_crtc_state *new_crtc_state =
12905 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
5a21b665 12906
d3a8fb32 12907 intel_pipe_update_end(new_crtc_state);
5a21b665
DV
12908}
12909
cf4c7c12 12910/**
4a3b8769
MR
12911 * intel_plane_destroy - destroy a plane
12912 * @plane: plane to destroy
cf4c7c12 12913 *
4a3b8769
MR
12914 * Common destruction function for all types of planes (primary, cursor,
12915 * sprite).
cf4c7c12 12916 */
4a3b8769 12917void intel_plane_destroy(struct drm_plane *plane)
465c120c 12918{
465c120c 12919 drm_plane_cleanup(plane);
69ae561f 12920 kfree(to_intel_plane(plane));
465c120c
MR
12921}
12922
714244e2
BW
12923static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
12924{
12925 switch (format) {
12926 case DRM_FORMAT_C8:
12927 case DRM_FORMAT_RGB565:
12928 case DRM_FORMAT_XRGB1555:
12929 case DRM_FORMAT_XRGB8888:
12930 return modifier == DRM_FORMAT_MOD_LINEAR ||
12931 modifier == I915_FORMAT_MOD_X_TILED;
12932 default:
12933 return false;
12934 }
12935}
12936
12937static bool i965_mod_supported(uint32_t format, uint64_t modifier)
12938{
12939 switch (format) {
12940 case DRM_FORMAT_C8:
12941 case DRM_FORMAT_RGB565:
12942 case DRM_FORMAT_XRGB8888:
12943 case DRM_FORMAT_XBGR8888:
12944 case DRM_FORMAT_XRGB2101010:
12945 case DRM_FORMAT_XBGR2101010:
12946 return modifier == DRM_FORMAT_MOD_LINEAR ||
12947 modifier == I915_FORMAT_MOD_X_TILED;
12948 default:
12949 return false;
12950 }
12951}
12952
12953static bool skl_mod_supported(uint32_t format, uint64_t modifier)
12954{
12955 switch (format) {
12956 case DRM_FORMAT_XRGB8888:
12957 case DRM_FORMAT_XBGR8888:
12958 case DRM_FORMAT_ARGB8888:
12959 case DRM_FORMAT_ABGR8888:
12960 if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
12961 modifier == I915_FORMAT_MOD_Y_TILED_CCS)
12962 return true;
12963 /* fall through */
12964 case DRM_FORMAT_RGB565:
12965 case DRM_FORMAT_XRGB2101010:
12966 case DRM_FORMAT_XBGR2101010:
12967 case DRM_FORMAT_YUYV:
12968 case DRM_FORMAT_YVYU:
12969 case DRM_FORMAT_UYVY:
12970 case DRM_FORMAT_VYUY:
12971 if (modifier == I915_FORMAT_MOD_Yf_TILED)
12972 return true;
12973 /* fall through */
12974 case DRM_FORMAT_C8:
12975 if (modifier == DRM_FORMAT_MOD_LINEAR ||
12976 modifier == I915_FORMAT_MOD_X_TILED ||
12977 modifier == I915_FORMAT_MOD_Y_TILED)
12978 return true;
12979 /* fall through */
12980 default:
12981 return false;
12982 }
12983}
12984
12985static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
12986 uint32_t format,
12987 uint64_t modifier)
12988{
12989 struct drm_i915_private *dev_priv = to_i915(plane->dev);
12990
12991 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
12992 return false;
12993
12994 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
12995 modifier != DRM_FORMAT_MOD_LINEAR)
12996 return false;
12997
12998 if (INTEL_GEN(dev_priv) >= 9)
12999 return skl_mod_supported(format, modifier);
13000 else if (INTEL_GEN(dev_priv) >= 4)
13001 return i965_mod_supported(format, modifier);
13002 else
13003 return i8xx_mod_supported(format, modifier);
13004
13005 unreachable();
13006}
13007
13008static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
13009 uint32_t format,
13010 uint64_t modifier)
13011{
13012 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
13013 return false;
13014
13015 return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
13016}
13017
13018static struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13019 .update_plane = drm_atomic_helper_update_plane,
13020 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13021 .destroy = intel_plane_destroy,
a98b3431
MR
13022 .atomic_get_property = intel_plane_atomic_get_property,
13023 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13024 .atomic_duplicate_state = intel_plane_duplicate_state,
13025 .atomic_destroy_state = intel_plane_destroy_state,
714244e2 13026 .format_mod_supported = intel_primary_plane_format_mod_supported,
465c120c
MR
13027};
13028
f79f2692
ML
13029static int
13030intel_legacy_cursor_update(struct drm_plane *plane,
13031 struct drm_crtc *crtc,
13032 struct drm_framebuffer *fb,
13033 int crtc_x, int crtc_y,
13034 unsigned int crtc_w, unsigned int crtc_h,
13035 uint32_t src_x, uint32_t src_y,
34a2ab5e
DV
13036 uint32_t src_w, uint32_t src_h,
13037 struct drm_modeset_acquire_ctx *ctx)
f79f2692
ML
13038{
13039 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13040 int ret;
13041 struct drm_plane_state *old_plane_state, *new_plane_state;
13042 struct intel_plane *intel_plane = to_intel_plane(plane);
13043 struct drm_framebuffer *old_fb;
13044 struct drm_crtc_state *crtc_state = crtc->state;
fd70075f 13045 struct i915_vma *old_vma, *vma;
f79f2692
ML
13046
13047 /*
13048 * When crtc is inactive or there is a modeset pending,
13049 * wait for it to complete in the slowpath
13050 */
13051 if (!crtc_state->active || needs_modeset(crtc_state) ||
13052 to_intel_crtc_state(crtc_state)->update_pipe)
13053 goto slow;
13054
13055 old_plane_state = plane->state;
669c9215
ML
13056 /*
13057 * Don't do an async update if there is an outstanding commit modifying
13058 * the plane. This prevents our async update's changes from getting
13059 * overridden by a previous synchronous update's state.
13060 */
13061 if (old_plane_state->commit &&
13062 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13063 goto slow;
f79f2692
ML
13064
13065 /*
13066 * If any parameters change that may affect watermarks,
13067 * take the slowpath. Only changing fb or position should be
13068 * in the fastpath.
13069 */
13070 if (old_plane_state->crtc != crtc ||
13071 old_plane_state->src_w != src_w ||
13072 old_plane_state->src_h != src_h ||
13073 old_plane_state->crtc_w != crtc_w ||
13074 old_plane_state->crtc_h != crtc_h ||
a5509abd 13075 !old_plane_state->fb != !fb)
f79f2692
ML
13076 goto slow;
13077
13078 new_plane_state = intel_plane_duplicate_state(plane);
13079 if (!new_plane_state)
13080 return -ENOMEM;
13081
13082 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13083
13084 new_plane_state->src_x = src_x;
13085 new_plane_state->src_y = src_y;
13086 new_plane_state->src_w = src_w;
13087 new_plane_state->src_h = src_h;
13088 new_plane_state->crtc_x = crtc_x;
13089 new_plane_state->crtc_y = crtc_y;
13090 new_plane_state->crtc_w = crtc_w;
13091 new_plane_state->crtc_h = crtc_h;
13092
13093 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
b2b55502
VS
13094 to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
13095 to_intel_plane_state(plane->state),
f79f2692
ML
13096 to_intel_plane_state(new_plane_state));
13097 if (ret)
13098 goto out_free;
13099
f79f2692
ML
13100 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13101 if (ret)
13102 goto out_free;
13103
13104 if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
fabac484 13105 int align = intel_cursor_alignment(dev_priv);
f79f2692
ML
13106
13107 ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
13108 if (ret) {
13109 DRM_DEBUG_KMS("failed to attach phys object\n");
13110 goto out_unlock;
13111 }
13112 } else {
f79f2692
ML
13113 vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
13114 if (IS_ERR(vma)) {
13115 DRM_DEBUG_KMS("failed to pin object\n");
13116
13117 ret = PTR_ERR(vma);
13118 goto out_unlock;
13119 }
be1e3415
CW
13120
13121 to_intel_plane_state(new_plane_state)->vma = vma;
f79f2692
ML
13122 }
13123
13124 old_fb = old_plane_state->fb;
13125
13126 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13127 intel_plane->frontbuffer_bit);
13128
13129 /* Swap plane state */
669c9215 13130 plane->state = new_plane_state;
f79f2692 13131
72259536
VS
13132 if (plane->state->visible) {
13133 trace_intel_update_plane(plane, to_intel_crtc(crtc));
282dbf9b 13134 intel_plane->update_plane(intel_plane,
a5509abd
VS
13135 to_intel_crtc_state(crtc->state),
13136 to_intel_plane_state(plane->state));
72259536
VS
13137 } else {
13138 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
282dbf9b 13139 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
72259536 13140 }
f79f2692 13141
669c9215 13142 old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
fd70075f
CW
13143 if (old_vma)
13144 intel_unpin_fb_vma(old_vma);
f79f2692
ML
13145
13146out_unlock:
13147 mutex_unlock(&dev_priv->drm.struct_mutex);
13148out_free:
669c9215
ML
13149 if (ret)
13150 intel_plane_destroy_state(plane, new_plane_state);
13151 else
13152 intel_plane_destroy_state(plane, old_plane_state);
f79f2692
ML
13153 return ret;
13154
f79f2692
ML
13155slow:
13156 return drm_atomic_helper_update_plane(plane, crtc, fb,
13157 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 13158 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
13159}
13160
13161static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13162 .update_plane = intel_legacy_cursor_update,
13163 .disable_plane = drm_atomic_helper_disable_plane,
13164 .destroy = intel_plane_destroy,
f79f2692
ML
13165 .atomic_get_property = intel_plane_atomic_get_property,
13166 .atomic_set_property = intel_plane_atomic_set_property,
13167 .atomic_duplicate_state = intel_plane_duplicate_state,
13168 .atomic_destroy_state = intel_plane_destroy_state,
714244e2 13169 .format_mod_supported = intel_cursor_plane_format_mod_supported,
f79f2692
ML
13170};
13171
b079bd17 13172static struct intel_plane *
580503c7 13173intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13174{
fca0ce2a
VS
13175 struct intel_plane *primary = NULL;
13176 struct intel_plane_state *state = NULL;
465c120c 13177 const uint32_t *intel_primary_formats;
93ca7e00 13178 unsigned int supported_rotations;
45e3743a 13179 unsigned int num_formats;
714244e2 13180 const uint64_t *modifiers;
fca0ce2a 13181 int ret;
465c120c
MR
13182
13183 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13184 if (!primary) {
13185 ret = -ENOMEM;
fca0ce2a 13186 goto fail;
b079bd17 13187 }
465c120c 13188
8e7d688b 13189 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13190 if (!state) {
13191 ret = -ENOMEM;
fca0ce2a 13192 goto fail;
b079bd17
VS
13193 }
13194
8e7d688b 13195 primary->base.state = &state->base;
ea2c67bb 13196
465c120c
MR
13197 primary->can_scale = false;
13198 primary->max_downscale = 1;
580503c7 13199 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 13200 primary->can_scale = true;
af99ceda 13201 state->scaler_id = -1;
6156a456 13202 }
465c120c 13203 primary->pipe = pipe;
e3c566df
VS
13204 /*
13205 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13206 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13207 */
13208 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
13209 primary->plane = (enum plane) !pipe;
13210 else
13211 primary->plane = (enum plane) pipe;
b14e5848 13212 primary->id = PLANE_PRIMARY;
a9ff8714 13213 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 13214 primary->check_plane = intel_check_primary_plane;
465c120c 13215
8bc0d7ac 13216 if (INTEL_GEN(dev_priv) >= 10) {
6c0fd451
DL
13217 intel_primary_formats = skl_primary_formats;
13218 num_formats = ARRAY_SIZE(skl_primary_formats);
714244e2
BW
13219 modifiers = skl_format_modifiers_ccs;
13220
9a8cc576 13221 primary->update_plane = skl_update_plane;
779d4d8f 13222 primary->disable_plane = skl_disable_plane;
d87ce764 13223 primary->get_hw_state = skl_plane_get_hw_state;
714244e2
BW
13224 } else if (INTEL_GEN(dev_priv) >= 9) {
13225 intel_primary_formats = skl_primary_formats;
13226 num_formats = ARRAY_SIZE(skl_primary_formats);
13227 if (pipe < PIPE_C)
13228 modifiers = skl_format_modifiers_ccs;
13229 else
13230 modifiers = skl_format_modifiers_noccs;
a8d201af 13231
9a8cc576 13232 primary->update_plane = skl_update_plane;
779d4d8f 13233 primary->disable_plane = skl_disable_plane;
d87ce764 13234 primary->get_hw_state = skl_plane_get_hw_state;
580503c7 13235 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13236 intel_primary_formats = i965_primary_formats;
13237 num_formats = ARRAY_SIZE(i965_primary_formats);
714244e2 13238 modifiers = i9xx_format_modifiers;
a8d201af
ML
13239
13240 primary->update_plane = i9xx_update_primary_plane;
13241 primary->disable_plane = i9xx_disable_primary_plane;
d87ce764 13242 primary->get_hw_state = i9xx_plane_get_hw_state;
6c0fd451
DL
13243 } else {
13244 intel_primary_formats = i8xx_primary_formats;
13245 num_formats = ARRAY_SIZE(i8xx_primary_formats);
714244e2 13246 modifiers = i9xx_format_modifiers;
a8d201af
ML
13247
13248 primary->update_plane = i9xx_update_primary_plane;
13249 primary->disable_plane = i9xx_disable_primary_plane;
d87ce764 13250 primary->get_hw_state = i9xx_plane_get_hw_state;
465c120c
MR
13251 }
13252
580503c7
VS
13253 if (INTEL_GEN(dev_priv) >= 9)
13254 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13255 0, &intel_plane_funcs,
38573dc1 13256 intel_primary_formats, num_formats,
714244e2 13257 modifiers,
38573dc1
VS
13258 DRM_PLANE_TYPE_PRIMARY,
13259 "plane 1%c", pipe_name(pipe));
9beb5fea 13260 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
13261 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13262 0, &intel_plane_funcs,
38573dc1 13263 intel_primary_formats, num_formats,
714244e2 13264 modifiers,
38573dc1
VS
13265 DRM_PLANE_TYPE_PRIMARY,
13266 "primary %c", pipe_name(pipe));
13267 else
580503c7
VS
13268 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
13269 0, &intel_plane_funcs,
38573dc1 13270 intel_primary_formats, num_formats,
714244e2 13271 modifiers,
38573dc1
VS
13272 DRM_PLANE_TYPE_PRIMARY,
13273 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
13274 if (ret)
13275 goto fail;
48404c1e 13276
5481e27f 13277 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 13278 supported_rotations =
c2c446ad
RF
13279 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13280 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
13281 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13282 supported_rotations =
c2c446ad
RF
13283 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13284 DRM_MODE_REFLECT_X;
5481e27f 13285 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 13286 supported_rotations =
c2c446ad 13287 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 13288 } else {
c2c446ad 13289 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
13290 }
13291
5481e27f 13292 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13293 drm_plane_create_rotation_property(&primary->base,
c2c446ad 13294 DRM_MODE_ROTATE_0,
93ca7e00 13295 supported_rotations);
48404c1e 13296
ea2c67bb
MR
13297 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13298
b079bd17 13299 return primary;
fca0ce2a
VS
13300
13301fail:
13302 kfree(state);
13303 kfree(primary);
13304
b079bd17 13305 return ERR_PTR(ret);
465c120c
MR
13306}
13307
b079bd17 13308static struct intel_plane *
b2d03b0d
VS
13309intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13310 enum pipe pipe)
3d7d6510 13311{
fca0ce2a
VS
13312 struct intel_plane *cursor = NULL;
13313 struct intel_plane_state *state = NULL;
13314 int ret;
3d7d6510
MR
13315
13316 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13317 if (!cursor) {
13318 ret = -ENOMEM;
fca0ce2a 13319 goto fail;
b079bd17 13320 }
3d7d6510 13321
8e7d688b 13322 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13323 if (!state) {
13324 ret = -ENOMEM;
fca0ce2a 13325 goto fail;
b079bd17
VS
13326 }
13327
8e7d688b 13328 cursor->base.state = &state->base;
ea2c67bb 13329
3d7d6510
MR
13330 cursor->can_scale = false;
13331 cursor->max_downscale = 1;
13332 cursor->pipe = pipe;
13333 cursor->plane = pipe;
b14e5848 13334 cursor->id = PLANE_CURSOR;
a9ff8714 13335 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
b2d03b0d
VS
13336
13337 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
13338 cursor->update_plane = i845_update_cursor;
13339 cursor->disable_plane = i845_disable_cursor;
d87ce764 13340 cursor->get_hw_state = i845_cursor_get_hw_state;
659056f2 13341 cursor->check_plane = i845_check_cursor;
b2d03b0d
VS
13342 } else {
13343 cursor->update_plane = i9xx_update_cursor;
13344 cursor->disable_plane = i9xx_disable_cursor;
d87ce764 13345 cursor->get_hw_state = i9xx_cursor_get_hw_state;
659056f2 13346 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 13347 }
3d7d6510 13348
cd5dcbf1
VS
13349 cursor->cursor.base = ~0;
13350 cursor->cursor.cntl = ~0;
024faac7
VS
13351
13352 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
13353 cursor->cursor.size = ~0;
3d7d6510 13354
580503c7 13355 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 13356 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
13357 intel_cursor_formats,
13358 ARRAY_SIZE(intel_cursor_formats),
714244e2
BW
13359 cursor_format_modifiers,
13360 DRM_PLANE_TYPE_CURSOR,
38573dc1 13361 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
13362 if (ret)
13363 goto fail;
4398ad45 13364
5481e27f 13365 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13366 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
13367 DRM_MODE_ROTATE_0,
13368 DRM_MODE_ROTATE_0 |
13369 DRM_MODE_ROTATE_180);
4398ad45 13370
580503c7 13371 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
13372 state->scaler_id = -1;
13373
ea2c67bb
MR
13374 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13375
b079bd17 13376 return cursor;
fca0ce2a
VS
13377
13378fail:
13379 kfree(state);
13380 kfree(cursor);
13381
b079bd17 13382 return ERR_PTR(ret);
3d7d6510
MR
13383}
13384
1c74eeaf
NM
13385static void intel_crtc_init_scalers(struct intel_crtc *crtc,
13386 struct intel_crtc_state *crtc_state)
549e2bfb 13387{
65edccce
VS
13388 struct intel_crtc_scaler_state *scaler_state =
13389 &crtc_state->scaler_state;
1c74eeaf 13390 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 13391 int i;
549e2bfb 13392
1c74eeaf
NM
13393 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
13394 if (!crtc->num_scalers)
13395 return;
13396
65edccce
VS
13397 for (i = 0; i < crtc->num_scalers; i++) {
13398 struct intel_scaler *scaler = &scaler_state->scalers[i];
13399
13400 scaler->in_use = 0;
13401 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
13402 }
13403
13404 scaler_state->scaler_id = -1;
13405}
13406
5ab0d85b 13407static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
13408{
13409 struct intel_crtc *intel_crtc;
f5de6e07 13410 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
13411 struct intel_plane *primary = NULL;
13412 struct intel_plane *cursor = NULL;
a81d6fa0 13413 int sprite, ret;
79e53945 13414
955382f3 13415 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
13416 if (!intel_crtc)
13417 return -ENOMEM;
79e53945 13418
f5de6e07 13419 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
13420 if (!crtc_state) {
13421 ret = -ENOMEM;
f5de6e07 13422 goto fail;
b079bd17 13423 }
550acefd
ACO
13424 intel_crtc->config = crtc_state;
13425 intel_crtc->base.state = &crtc_state->base;
07878248 13426 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13427
580503c7 13428 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
13429 if (IS_ERR(primary)) {
13430 ret = PTR_ERR(primary);
3d7d6510 13431 goto fail;
b079bd17 13432 }
d97d7b48 13433 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 13434
a81d6fa0 13435 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
13436 struct intel_plane *plane;
13437
580503c7 13438 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 13439 if (IS_ERR(plane)) {
b079bd17
VS
13440 ret = PTR_ERR(plane);
13441 goto fail;
13442 }
d97d7b48 13443 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
13444 }
13445
580503c7 13446 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 13447 if (IS_ERR(cursor)) {
b079bd17 13448 ret = PTR_ERR(cursor);
3d7d6510 13449 goto fail;
b079bd17 13450 }
d97d7b48 13451 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 13452
5ab0d85b 13453 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
13454 &primary->base, &cursor->base,
13455 &intel_crtc_funcs,
4d5d72b7 13456 "pipe %c", pipe_name(pipe));
3d7d6510
MR
13457 if (ret)
13458 goto fail;
79e53945 13459
80824003 13460 intel_crtc->pipe = pipe;
e3c566df 13461 intel_crtc->plane = primary->plane;
80824003 13462
1c74eeaf
NM
13463 /* initialize shared scalers */
13464 intel_crtc_init_scalers(intel_crtc, crtc_state);
13465
22fd0fab
JB
13466 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13467 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
13468 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
13469 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 13470
79e53945 13471 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 13472
8563b1e8
LL
13473 intel_color_init(&intel_crtc->base);
13474
87b6b101 13475 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
13476
13477 return 0;
3d7d6510
MR
13478
13479fail:
b079bd17
VS
13480 /*
13481 * drm_mode_config_cleanup() will free up any
13482 * crtcs/planes already initialized.
13483 */
f5de6e07 13484 kfree(crtc_state);
3d7d6510 13485 kfree(intel_crtc);
b079bd17
VS
13486
13487 return ret;
79e53945
JB
13488}
13489
752aa88a
JB
13490enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13491{
6e9f798d 13492 struct drm_device *dev = connector->base.dev;
752aa88a 13493
51fd371b 13494 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13495
51ec53da 13496 if (!connector->base.state->crtc)
752aa88a
JB
13497 return INVALID_PIPE;
13498
51ec53da 13499 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
13500}
13501
08d7b3d1 13502int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13503 struct drm_file *file)
08d7b3d1 13504{
08d7b3d1 13505 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13506 struct drm_crtc *drmmode_crtc;
c05422d5 13507 struct intel_crtc *crtc;
08d7b3d1 13508
418da172 13509 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
71240ed2 13510 if (!drmmode_crtc)
3f2c2057 13511 return -ENOENT;
08d7b3d1 13512
7707e653 13513 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13514 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13515
c05422d5 13516 return 0;
08d7b3d1
CW
13517}
13518
66a9278e 13519static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13520{
66a9278e
DV
13521 struct drm_device *dev = encoder->base.dev;
13522 struct intel_encoder *source_encoder;
79e53945 13523 int index_mask = 0;
79e53945
JB
13524 int entry = 0;
13525
b2784e15 13526 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13527 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13528 index_mask |= (1 << entry);
13529
79e53945
JB
13530 entry++;
13531 }
4ef69c7a 13532
79e53945
JB
13533 return index_mask;
13534}
13535
646d5772 13536static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 13537{
646d5772 13538 if (!IS_MOBILE(dev_priv))
4d302442
CW
13539 return false;
13540
13541 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13542 return false;
13543
5db94019 13544 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13545 return false;
13546
13547 return true;
13548}
13549
6315b5d3 13550static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 13551{
6315b5d3 13552 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
13553 return false;
13554
50a0bc90 13555 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
13556 return false;
13557
920a14b2 13558 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
13559 return false;
13560
4f8036a2
TU
13561 if (HAS_PCH_LPT_H(dev_priv) &&
13562 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
13563 return false;
13564
70ac54d0 13565 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 13566 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
13567 return false;
13568
e4abb733 13569 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
13570 return false;
13571
13572 return true;
13573}
13574
8090ba8c
ID
13575void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
13576{
13577 int pps_num;
13578 int pps_idx;
13579
13580 if (HAS_DDI(dev_priv))
13581 return;
13582 /*
13583 * This w/a is needed at least on CPT/PPT, but to be sure apply it
13584 * everywhere where registers can be write protected.
13585 */
13586 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13587 pps_num = 2;
13588 else
13589 pps_num = 1;
13590
13591 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
13592 u32 val = I915_READ(PP_CONTROL(pps_idx));
13593
13594 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
13595 I915_WRITE(PP_CONTROL(pps_idx), val);
13596 }
13597}
13598
44cb734c
ID
13599static void intel_pps_init(struct drm_i915_private *dev_priv)
13600{
cc3f90f0 13601 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
13602 dev_priv->pps_mmio_base = PCH_PPS_BASE;
13603 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13604 dev_priv->pps_mmio_base = VLV_PPS_BASE;
13605 else
13606 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
13607
13608 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
13609}
13610
c39055b0 13611static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 13612{
4ef69c7a 13613 struct intel_encoder *encoder;
cb0953d7 13614 bool dpd_is_edp = false;
79e53945 13615
44cb734c
ID
13616 intel_pps_init(dev_priv);
13617
97a824e1
ID
13618 /*
13619 * intel_edp_init_connector() depends on this completing first, to
13620 * prevent the registeration of both eDP and LVDS and the incorrect
13621 * sharing of the PPS.
13622 */
c39055b0 13623 intel_lvds_init(dev_priv);
79e53945 13624
6315b5d3 13625 if (intel_crt_present(dev_priv))
c39055b0 13626 intel_crt_init(dev_priv);
cb0953d7 13627
cc3f90f0 13628 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
13629 /*
13630 * FIXME: Broxton doesn't support port detection via the
13631 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13632 * detect the ports.
13633 */
c39055b0
ACO
13634 intel_ddi_init(dev_priv, PORT_A);
13635 intel_ddi_init(dev_priv, PORT_B);
13636 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 13637
c39055b0 13638 intel_dsi_init(dev_priv);
4f8036a2 13639 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
13640 int found;
13641
de31facd
JB
13642 /*
13643 * Haswell uses DDI functions to detect digital outputs.
13644 * On SKL pre-D0 the strap isn't connected, so we assume
13645 * it's there.
13646 */
77179400 13647 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 13648 /* WaIgnoreDDIAStrap: skl */
b976dc53 13649 if (found || IS_GEN9_BC(dev_priv))
c39055b0 13650 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
13651
13652 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13653 * register */
13654 found = I915_READ(SFUSE_STRAP);
13655
13656 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 13657 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 13658 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 13659 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 13660 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 13661 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
13662 /*
13663 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13664 */
b976dc53 13665 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
13666 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
13667 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
13668 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 13669 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 13670
6e266956 13671 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 13672 int found;
7b91bf7f 13673 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
270b3042 13674
646d5772 13675 if (has_edp_a(dev_priv))
c39055b0 13676 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 13677
dc0fa718 13678 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13679 /* PCH SDVOB multiplex with HDMIB */
c39055b0 13680 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 13681 if (!found)
c39055b0 13682 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 13683 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 13684 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
13685 }
13686
dc0fa718 13687 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 13688 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 13689
dc0fa718 13690 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 13691 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 13692
5eb08b69 13693 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 13694 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 13695
270b3042 13696 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 13697 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 13698 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 13699 bool has_edp, has_port;
457c52d8 13700
e17ac6db
VS
13701 /*
13702 * The DP_DETECTED bit is the latched state of the DDC
13703 * SDA pin at boot. However since eDP doesn't require DDC
13704 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13705 * eDP ports may have been muxed to an alternate function.
13706 * Thus we can't rely on the DP_DETECTED bit alone to detect
13707 * eDP ports. Consult the VBT as well as DP_DETECTED to
13708 * detect eDP ports.
22f35042
VS
13709 *
13710 * Sadly the straps seem to be missing sometimes even for HDMI
13711 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
13712 * and VBT for the presence of the port. Additionally we can't
13713 * trust the port type the VBT declares as we've seen at least
13714 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 13715 */
7b91bf7f 13716 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
22f35042
VS
13717 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
13718 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 13719 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 13720 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 13721 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 13722
7b91bf7f 13723 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
22f35042
VS
13724 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
13725 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 13726 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 13727 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 13728 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 13729
920a14b2 13730 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
13731 /*
13732 * eDP not supported on port D,
13733 * so no need to worry about it
13734 */
13735 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
13736 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 13737 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 13738 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 13739 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
13740 }
13741
c39055b0 13742 intel_dsi_init(dev_priv);
5db94019 13743 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 13744 bool found = false;
7d57382e 13745
e2debe91 13746 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13747 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 13748 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 13749 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 13750 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 13751 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 13752 }
27185ae1 13753
9beb5fea 13754 if (!found && IS_G4X(dev_priv))
c39055b0 13755 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 13756 }
13520b05
KH
13757
13758 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 13759
e2debe91 13760 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13761 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 13762 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 13763 }
27185ae1 13764
e2debe91 13765 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 13766
9beb5fea 13767 if (IS_G4X(dev_priv)) {
b01f2c3a 13768 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 13769 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 13770 }
9beb5fea 13771 if (IS_G4X(dev_priv))
c39055b0 13772 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 13773 }
27185ae1 13774
9beb5fea 13775 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 13776 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 13777 } else if (IS_GEN2(dev_priv))
c39055b0 13778 intel_dvo_init(dev_priv);
79e53945 13779
56b857a5 13780 if (SUPPORTS_TV(dev_priv))
c39055b0 13781 intel_tv_init(dev_priv);
79e53945 13782
c39055b0 13783 intel_psr_init(dev_priv);
7c8f8a70 13784
c39055b0 13785 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
13786 encoder->base.possible_crtcs = encoder->crtc_mask;
13787 encoder->base.possible_clones =
66a9278e 13788 intel_encoder_clones(encoder);
79e53945 13789 }
47356eb6 13790
c39055b0 13791 intel_init_pch_refclk(dev_priv);
270b3042 13792
c39055b0 13793 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
13794}
13795
13796static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13797{
13798 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 13799
ef2d633e 13800 drm_framebuffer_cleanup(fb);
70001cd2 13801
dd689287
CW
13802 i915_gem_object_lock(intel_fb->obj);
13803 WARN_ON(!intel_fb->obj->framebuffer_references--);
13804 i915_gem_object_unlock(intel_fb->obj);
13805
f8c417cd 13806 i915_gem_object_put(intel_fb->obj);
70001cd2 13807
79e53945
JB
13808 kfree(intel_fb);
13809}
13810
13811static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 13812 struct drm_file *file,
79e53945
JB
13813 unsigned int *handle)
13814{
13815 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 13816 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 13817
cc917ab4
CW
13818 if (obj->userptr.mm) {
13819 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
13820 return -EINVAL;
13821 }
13822
05394f39 13823 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
13824}
13825
86c98588
RV
13826static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
13827 struct drm_file *file,
13828 unsigned flags, unsigned color,
13829 struct drm_clip_rect *clips,
13830 unsigned num_clips)
13831{
5a97bcc6 13832 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 13833
5a97bcc6 13834 i915_gem_object_flush_if_display(obj);
d59b21ec 13835 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
13836
13837 return 0;
13838}
13839
79e53945
JB
13840static const struct drm_framebuffer_funcs intel_fb_funcs = {
13841 .destroy = intel_user_framebuffer_destroy,
13842 .create_handle = intel_user_framebuffer_create_handle,
86c98588 13843 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
13844};
13845
b321803d 13846static
920a14b2
TU
13847u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
13848 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 13849{
24dbf51a 13850 u32 gen = INTEL_GEN(dev_priv);
b321803d
DL
13851
13852 if (gen >= 9) {
ac484963
VS
13853 int cpp = drm_format_plane_cpp(pixel_format, 0);
13854
b321803d
DL
13855 /* "The stride in bytes must not exceed the of the size of 8K
13856 * pixels and 32K bytes."
13857 */
ac484963 13858 return min(8192 * cpp, 32768);
6401c37d 13859 } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
b321803d
DL
13860 return 32*1024;
13861 } else if (gen >= 4) {
13862 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13863 return 16*1024;
13864 else
13865 return 32*1024;
13866 } else if (gen >= 3) {
13867 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13868 return 8*1024;
13869 else
13870 return 16*1024;
13871 } else {
13872 /* XXX DSPC is limited to 4k tiled */
13873 return 8*1024;
13874 }
13875}
13876
24dbf51a
CW
13877static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
13878 struct drm_i915_gem_object *obj,
13879 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 13880{
24dbf51a 13881 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2e2adb05 13882 struct drm_framebuffer *fb = &intel_fb->base;
b3c11ac2 13883 struct drm_format_name_buf format_name;
2e2adb05 13884 u32 pitch_limit;
dd689287 13885 unsigned int tiling, stride;
24dbf51a 13886 int ret = -EINVAL;
2e2adb05 13887 int i;
79e53945 13888
dd689287
CW
13889 i915_gem_object_lock(obj);
13890 obj->framebuffer_references++;
13891 tiling = i915_gem_object_get_tiling(obj);
13892 stride = i915_gem_object_get_stride(obj);
13893 i915_gem_object_unlock(obj);
dd4916c5 13894
2a80eada 13895 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
13896 /*
13897 * If there's a fence, enforce that
13898 * the fb modifier and tiling mode match.
13899 */
13900 if (tiling != I915_TILING_NONE &&
13901 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 13902 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 13903 goto err;
2a80eada
DV
13904 }
13905 } else {
c2ff7370 13906 if (tiling == I915_TILING_X) {
2a80eada 13907 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 13908 } else if (tiling == I915_TILING_Y) {
144cc143 13909 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 13910 goto err;
2a80eada
DV
13911 }
13912 }
13913
9a8f0a12
TU
13914 /* Passed in modifier sanity checking. */
13915 switch (mode_cmd->modifier[0]) {
2e2adb05
VS
13916 case I915_FORMAT_MOD_Y_TILED_CCS:
13917 case I915_FORMAT_MOD_Yf_TILED_CCS:
13918 switch (mode_cmd->pixel_format) {
13919 case DRM_FORMAT_XBGR8888:
13920 case DRM_FORMAT_ABGR8888:
13921 case DRM_FORMAT_XRGB8888:
13922 case DRM_FORMAT_ARGB8888:
13923 break;
13924 default:
13925 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
13926 goto err;
13927 }
13928 /* fall through */
9a8f0a12
TU
13929 case I915_FORMAT_MOD_Y_TILED:
13930 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 13931 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
13932 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
13933 mode_cmd->modifier[0]);
24dbf51a 13934 goto err;
9a8f0a12 13935 }
2f075565 13936 case DRM_FORMAT_MOD_LINEAR:
9a8f0a12
TU
13937 case I915_FORMAT_MOD_X_TILED:
13938 break;
13939 default:
144cc143
VS
13940 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
13941 mode_cmd->modifier[0]);
24dbf51a 13942 goto err;
c16ed4be 13943 }
57cd6508 13944
c2ff7370
VS
13945 /*
13946 * gen2/3 display engine uses the fence if present,
13947 * so the tiling mode must match the fb modifier exactly.
13948 */
13949 if (INTEL_INFO(dev_priv)->gen < 4 &&
13950 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 13951 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 13952 goto err;
c2ff7370
VS
13953 }
13954
920a14b2 13955 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 13956 mode_cmd->pixel_format);
a35cdaa0 13957 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143 13958 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
2f075565 13959 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
144cc143
VS
13960 "tiled" : "linear",
13961 mode_cmd->pitches[0], pitch_limit);
24dbf51a 13962 goto err;
c16ed4be 13963 }
5d7bd705 13964
c2ff7370
VS
13965 /*
13966 * If there's a fence, enforce that
13967 * the fb pitch and fence stride match.
13968 */
144cc143
VS
13969 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
13970 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
13971 mode_cmd->pitches[0], stride);
24dbf51a 13972 goto err;
c16ed4be 13973 }
5d7bd705 13974
57779d06 13975 /* Reject formats not supported by any plane early. */
308e5bcb 13976 switch (mode_cmd->pixel_format) {
57779d06 13977 case DRM_FORMAT_C8:
04b3924d
VS
13978 case DRM_FORMAT_RGB565:
13979 case DRM_FORMAT_XRGB8888:
13980 case DRM_FORMAT_ARGB8888:
57779d06
VS
13981 break;
13982 case DRM_FORMAT_XRGB1555:
6315b5d3 13983 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
13984 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13985 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13986 goto err;
c16ed4be 13987 }
57779d06 13988 break;
57779d06 13989 case DRM_FORMAT_ABGR8888:
920a14b2 13990 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 13991 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
13992 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
13993 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 13994 goto err;
6c0fd451
DL
13995 }
13996 break;
13997 case DRM_FORMAT_XBGR8888:
04b3924d 13998 case DRM_FORMAT_XRGB2101010:
57779d06 13999 case DRM_FORMAT_XBGR2101010:
6315b5d3 14000 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
14001 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14002 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14003 goto err;
c16ed4be 14004 }
b5626747 14005 break;
7531208b 14006 case DRM_FORMAT_ABGR2101010:
920a14b2 14007 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
14008 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14009 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14010 goto err;
7531208b
DL
14011 }
14012 break;
04b3924d
VS
14013 case DRM_FORMAT_YUYV:
14014 case DRM_FORMAT_UYVY:
14015 case DRM_FORMAT_YVYU:
14016 case DRM_FORMAT_VYUY:
ab33081a 14017 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
144cc143
VS
14018 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14019 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14020 goto err;
c16ed4be 14021 }
57cd6508
CW
14022 break;
14023 default:
144cc143
VS
14024 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14025 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14026 goto err;
57cd6508
CW
14027 }
14028
90f9a336
VS
14029 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14030 if (mode_cmd->offsets[0] != 0)
24dbf51a 14031 goto err;
90f9a336 14032
2e2adb05 14033 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
d88c4afd 14034
2e2adb05
VS
14035 for (i = 0; i < fb->format->num_planes; i++) {
14036 u32 stride_alignment;
14037
14038 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14039 DRM_DEBUG_KMS("bad plane %d handle\n", i);
814feed3 14040 goto err;
2e2adb05
VS
14041 }
14042
14043 stride_alignment = intel_fb_stride_alignment(fb, i);
14044
14045 /*
14046 * Display WA #0531: skl,bxt,kbl,glk
14047 *
14048 * Render decompression and plane width > 3840
14049 * combined with horizontal panning requires the
14050 * plane stride to be a multiple of 4. We'll just
14051 * require the entire fb to accommodate that to avoid
14052 * potential runtime errors at plane configuration time.
14053 */
14054 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
14055 (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
14056 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
14057 stride_alignment *= 4;
14058
14059 if (fb->pitches[i] & (stride_alignment - 1)) {
14060 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14061 i, fb->pitches[i], stride_alignment);
14062 goto err;
14063 }
d88c4afd
VS
14064 }
14065
c7d73f6a
DV
14066 intel_fb->obj = obj;
14067
2e2adb05 14068 ret = intel_fill_fb_info(dev_priv, fb);
6687c906 14069 if (ret)
9aceb5c1 14070 goto err;
2d7a215f 14071
2e2adb05 14072 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
79e53945
JB
14073 if (ret) {
14074 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14075 goto err;
79e53945
JB
14076 }
14077
79e53945 14078 return 0;
24dbf51a
CW
14079
14080err:
dd689287
CW
14081 i915_gem_object_lock(obj);
14082 obj->framebuffer_references--;
14083 i915_gem_object_unlock(obj);
24dbf51a 14084 return ret;
79e53945
JB
14085}
14086
79e53945
JB
14087static struct drm_framebuffer *
14088intel_user_framebuffer_create(struct drm_device *dev,
14089 struct drm_file *filp,
1eb83451 14090 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14091{
dcb1394e 14092 struct drm_framebuffer *fb;
05394f39 14093 struct drm_i915_gem_object *obj;
76dc3769 14094 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14095
03ac0642
CW
14096 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14097 if (!obj)
cce13ff7 14098 return ERR_PTR(-ENOENT);
79e53945 14099
24dbf51a 14100 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14101 if (IS_ERR(fb))
f0cd5182 14102 i915_gem_object_put(obj);
dcb1394e
LW
14103
14104 return fb;
79e53945
JB
14105}
14106
778e23a9
CW
14107static void intel_atomic_state_free(struct drm_atomic_state *state)
14108{
14109 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14110
14111 drm_atomic_state_default_release(state);
14112
14113 i915_sw_fence_fini(&intel_state->commit_ready);
14114
14115 kfree(state);
14116}
14117
79e53945 14118static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14119 .fb_create = intel_user_framebuffer_create,
bbfb6ce8 14120 .get_format_info = intel_get_format_info,
0632fef6 14121 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14122 .atomic_check = intel_atomic_check,
14123 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14124 .atomic_state_alloc = intel_atomic_state_alloc,
14125 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14126 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14127};
14128
88212941
ID
14129/**
14130 * intel_init_display_hooks - initialize the display modesetting hooks
14131 * @dev_priv: device private
14132 */
14133void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14134{
7ff89ca2
VS
14135 intel_init_cdclk_hooks(dev_priv);
14136
88212941 14137 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 14138 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14139 dev_priv->display.get_initial_plane_config =
14140 skylake_get_initial_plane_config;
bc8d7dff
DL
14141 dev_priv->display.crtc_compute_clock =
14142 haswell_crtc_compute_clock;
14143 dev_priv->display.crtc_enable = haswell_crtc_enable;
14144 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14145 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14146 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14147 dev_priv->display.get_initial_plane_config =
14148 ironlake_get_initial_plane_config;
797d0259
ACO
14149 dev_priv->display.crtc_compute_clock =
14150 haswell_crtc_compute_clock;
4f771f10
PZ
14151 dev_priv->display.crtc_enable = haswell_crtc_enable;
14152 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14153 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14154 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14155 dev_priv->display.get_initial_plane_config =
14156 ironlake_get_initial_plane_config;
3fb37703
ACO
14157 dev_priv->display.crtc_compute_clock =
14158 ironlake_crtc_compute_clock;
76e5a89c
DV
14159 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14160 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14161 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14162 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14163 dev_priv->display.get_initial_plane_config =
14164 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14165 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14166 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14167 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14168 } else if (IS_VALLEYVIEW(dev_priv)) {
14169 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14170 dev_priv->display.get_initial_plane_config =
14171 i9xx_get_initial_plane_config;
14172 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14173 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14174 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14175 } else if (IS_G4X(dev_priv)) {
14176 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14177 dev_priv->display.get_initial_plane_config =
14178 i9xx_get_initial_plane_config;
14179 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14180 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14181 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14182 } else if (IS_PINEVIEW(dev_priv)) {
14183 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14184 dev_priv->display.get_initial_plane_config =
14185 i9xx_get_initial_plane_config;
14186 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14187 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14188 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14189 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14190 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14191 dev_priv->display.get_initial_plane_config =
14192 i9xx_get_initial_plane_config;
d6dfee7a 14193 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14194 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14195 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14196 } else {
14197 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14198 dev_priv->display.get_initial_plane_config =
14199 i9xx_get_initial_plane_config;
14200 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14201 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14202 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14203 }
e70236a8 14204
88212941 14205 if (IS_GEN5(dev_priv)) {
3bb11b53 14206 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14207 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14208 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14209 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14210 /* FIXME: detect B0+ stepping and use auto training */
14211 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14212 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14213 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14214 }
14215
bd30ca2d 14216 if (INTEL_GEN(dev_priv) >= 9)
27082493
L
14217 dev_priv->display.update_crtcs = skl_update_crtcs;
14218 else
14219 dev_priv->display.update_crtcs = intel_update_crtcs;
e70236a8
JB
14220}
14221
435793df
KP
14222/*
14223 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14224 */
14225static void quirk_ssc_force_disable(struct drm_device *dev)
14226{
fac5e23e 14227 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14228 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14229 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14230}
14231
4dca20ef 14232/*
5a15ab5b
CE
14233 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14234 * brightness value
4dca20ef
CE
14235 */
14236static void quirk_invert_brightness(struct drm_device *dev)
14237{
fac5e23e 14238 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14239 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14240 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14241}
14242
9c72cc6f
SD
14243/* Some VBT's incorrectly indicate no backlight is present */
14244static void quirk_backlight_present(struct drm_device *dev)
14245{
fac5e23e 14246 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14247 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14248 DRM_INFO("applying backlight present quirk\n");
14249}
14250
c99a259b
MN
14251/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
14252 * which is 300 ms greater than eDP spec T12 min.
14253 */
14254static void quirk_increase_t12_delay(struct drm_device *dev)
14255{
14256 struct drm_i915_private *dev_priv = to_i915(dev);
14257
14258 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
14259 DRM_INFO("Applying T12 delay quirk\n");
14260}
14261
af25065b
CT
14262/*
14263 * GeminiLake NUC HDMI outputs require additional off time
14264 * this allows the onboard retimer to correctly sync to signal
14265 */
14266static void quirk_increase_ddi_disabled_time(struct drm_device *dev)
14267{
14268 struct drm_i915_private *dev_priv = to_i915(dev);
14269
14270 dev_priv->quirks |= QUIRK_INCREASE_DDI_DISABLED_TIME;
14271 DRM_INFO("Applying Increase DDI Disabled quirk\n");
14272}
14273
b690e96c
JB
14274struct intel_quirk {
14275 int device;
14276 int subsystem_vendor;
14277 int subsystem_device;
14278 void (*hook)(struct drm_device *dev);
14279};
14280
5f85f176
EE
14281/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14282struct intel_dmi_quirk {
14283 void (*hook)(struct drm_device *dev);
14284 const struct dmi_system_id (*dmi_id_list)[];
14285};
14286
14287static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14288{
14289 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14290 return 1;
14291}
14292
14293static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14294 {
14295 .dmi_id_list = &(const struct dmi_system_id[]) {
14296 {
14297 .callback = intel_dmi_reverse_brightness,
14298 .ident = "NCR Corporation",
14299 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14300 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14301 },
14302 },
14303 { } /* terminating entry */
14304 },
14305 .hook = quirk_invert_brightness,
14306 },
14307};
14308
c43b5634 14309static struct intel_quirk intel_quirks[] = {
435793df
KP
14310 /* Lenovo U160 cannot use SSC on LVDS */
14311 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14312
14313 /* Sony Vaio Y cannot use SSC on LVDS */
14314 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14315
be505f64
AH
14316 /* Acer Aspire 5734Z must invert backlight brightness */
14317 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14318
14319 /* Acer/eMachines G725 */
14320 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14321
14322 /* Acer/eMachines e725 */
14323 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14324
14325 /* Acer/Packard Bell NCL20 */
14326 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14327
14328 /* Acer Aspire 4736Z */
14329 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14330
14331 /* Acer Aspire 5336 */
14332 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14333
14334 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14335 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14336
dfb3d47b
SD
14337 /* Acer C720 Chromebook (Core i3 4005U) */
14338 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14339
b2a9601c 14340 /* Apple Macbook 2,1 (Core 2 T7400) */
14341 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14342
1b9448b0
JN
14343 /* Apple Macbook 4,1 */
14344 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14345
d4967d8c
SD
14346 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14347 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14348
14349 /* HP Chromebook 14 (Celeron 2955U) */
14350 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14351
14352 /* Dell Chromebook 11 */
14353 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
14354
14355 /* Dell Chromebook 11 (2015 version) */
14356 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
c99a259b
MN
14357
14358 /* Toshiba Satellite P50-C-18C */
14359 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
af25065b
CT
14360
14361 /* GeminiLake NUC */
14362 { 0x3185, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
14363 { 0x3184, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
14364 /* ASRock ITX*/
14365 { 0x3185, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
14366 { 0x3184, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
b690e96c
JB
14367};
14368
14369static void intel_init_quirks(struct drm_device *dev)
14370{
14371 struct pci_dev *d = dev->pdev;
14372 int i;
14373
14374 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14375 struct intel_quirk *q = &intel_quirks[i];
14376
14377 if (d->device == q->device &&
14378 (d->subsystem_vendor == q->subsystem_vendor ||
14379 q->subsystem_vendor == PCI_ANY_ID) &&
14380 (d->subsystem_device == q->subsystem_device ||
14381 q->subsystem_device == PCI_ANY_ID))
14382 q->hook(dev);
14383 }
5f85f176
EE
14384 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14385 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14386 intel_dmi_quirks[i].hook(dev);
14387 }
b690e96c
JB
14388}
14389
9cce37f4 14390/* Disable the VGA plane that we never use */
29b74b7f 14391static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 14392{
52a05c30 14393 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 14394 u8 sr1;
920a14b2 14395 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 14396
2b37c616 14397 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 14398 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14399 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14400 sr1 = inb(VGA_SR_DATA);
14401 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 14402 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
14403 udelay(300);
14404
01f5a626 14405 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14406 POSTING_READ(vga_reg);
14407}
14408
f817586c
DV
14409void intel_modeset_init_hw(struct drm_device *dev)
14410{
fac5e23e 14411 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 14412
4c75b940 14413 intel_update_cdclk(dev_priv);
bb0f4aab 14414 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
f817586c
DV
14415}
14416
d93c0372
MR
14417/*
14418 * Calculate what we think the watermarks should be for the state we've read
14419 * out of the hardware and then immediately program those watermarks so that
14420 * we ensure the hardware settings match our internal state.
14421 *
14422 * We can calculate what we think WM's should be by creating a duplicate of the
14423 * current state (which was constructed during hardware readout) and running it
14424 * through the atomic check code to calculate new watermark values in the
14425 * state object.
14426 */
14427static void sanitize_watermarks(struct drm_device *dev)
14428{
14429 struct drm_i915_private *dev_priv = to_i915(dev);
14430 struct drm_atomic_state *state;
ccf010fb 14431 struct intel_atomic_state *intel_state;
d93c0372
MR
14432 struct drm_crtc *crtc;
14433 struct drm_crtc_state *cstate;
14434 struct drm_modeset_acquire_ctx ctx;
14435 int ret;
14436 int i;
14437
14438 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 14439 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
14440 return;
14441
14442 /*
14443 * We need to hold connection_mutex before calling duplicate_state so
14444 * that the connector loop is protected.
14445 */
14446 drm_modeset_acquire_init(&ctx, 0);
14447retry:
0cd1262d 14448 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
14449 if (ret == -EDEADLK) {
14450 drm_modeset_backoff(&ctx);
14451 goto retry;
14452 } else if (WARN_ON(ret)) {
0cd1262d 14453 goto fail;
d93c0372
MR
14454 }
14455
14456 state = drm_atomic_helper_duplicate_state(dev, &ctx);
14457 if (WARN_ON(IS_ERR(state)))
0cd1262d 14458 goto fail;
d93c0372 14459
ccf010fb
ML
14460 intel_state = to_intel_atomic_state(state);
14461
ed4a6a7c
MR
14462 /*
14463 * Hardware readout is the only time we don't want to calculate
14464 * intermediate watermarks (since we don't trust the current
14465 * watermarks).
14466 */
602ae835
VS
14467 if (!HAS_GMCH_DISPLAY(dev_priv))
14468 intel_state->skip_intermediate_wm = true;
ed4a6a7c 14469
d93c0372
MR
14470 ret = intel_atomic_check(dev, state);
14471 if (ret) {
14472 /*
14473 * If we fail here, it means that the hardware appears to be
14474 * programmed in a way that shouldn't be possible, given our
14475 * understanding of watermark requirements. This might mean a
14476 * mistake in the hardware readout code or a mistake in the
14477 * watermark calculations for a given platform. Raise a WARN
14478 * so that this is noticeable.
14479 *
14480 * If this actually happens, we'll have to just leave the
14481 * BIOS-programmed watermarks untouched and hope for the best.
14482 */
14483 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 14484 goto put_state;
d93c0372
MR
14485 }
14486
14487 /* Write calculated watermark values back */
aa5e9b47 14488 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
14489 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
14490
ed4a6a7c 14491 cs->wm.need_postvbl_update = true;
ccf010fb 14492 dev_priv->display.optimize_watermarks(intel_state, cs);
5d25aa47
ML
14493
14494 to_intel_crtc_state(crtc->state)->wm = cs->wm;
d93c0372
MR
14495 }
14496
b9a1b717 14497put_state:
0853695c 14498 drm_atomic_state_put(state);
0cd1262d 14499fail:
d93c0372
MR
14500 drm_modeset_drop_locks(&ctx);
14501 drm_modeset_acquire_fini(&ctx);
14502}
14503
b079bd17 14504int intel_modeset_init(struct drm_device *dev)
79e53945 14505{
72e96d64
JL
14506 struct drm_i915_private *dev_priv = to_i915(dev);
14507 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 14508 enum pipe pipe;
46f297fb 14509 struct intel_crtc *crtc;
79e53945 14510
eda41bdc
VS
14511 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
14512
79e53945
JB
14513 drm_mode_config_init(dev);
14514
14515 dev->mode_config.min_width = 0;
14516 dev->mode_config.min_height = 0;
14517
019d96cb
DA
14518 dev->mode_config.preferred_depth = 24;
14519 dev->mode_config.prefer_shadow = 1;
14520
25bab385
TU
14521 dev->mode_config.allow_fb_modifiers = true;
14522
e6ecefaa 14523 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14524
400c19d9 14525 init_llist_head(&dev_priv->atomic_helper.free_list);
eb955eee 14526 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 14527 intel_atomic_helper_free_state_worker);
eb955eee 14528
b690e96c
JB
14529 intel_init_quirks(dev);
14530
62d75df7 14531 intel_init_pm(dev_priv);
1fa61106 14532
b7f05d4a 14533 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 14534 return 0;
e3c74757 14535
69f92f67
LW
14536 /*
14537 * There may be no VBT; and if the BIOS enabled SSC we can
14538 * just keep using it to avoid unnecessary flicker. Whereas if the
14539 * BIOS isn't using it, don't assume it will work even if the VBT
14540 * indicates as much.
14541 */
6e266956 14542 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
14543 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14544 DREF_SSC1_ENABLE);
14545
14546 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14547 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14548 bios_lvds_use_ssc ? "en" : "dis",
14549 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14550 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14551 }
14552 }
14553
5db94019 14554 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
14555 dev->mode_config.max_width = 2048;
14556 dev->mode_config.max_height = 2048;
5db94019 14557 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
14558 dev->mode_config.max_width = 4096;
14559 dev->mode_config.max_height = 4096;
79e53945 14560 } else {
a6c45cf0
CW
14561 dev->mode_config.max_width = 8192;
14562 dev->mode_config.max_height = 8192;
79e53945 14563 }
068be561 14564
2a307c2e
JN
14565 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14566 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 14567 dev->mode_config.cursor_height = 1023;
5db94019 14568 } else if (IS_GEN2(dev_priv)) {
068be561
DL
14569 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14570 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14571 } else {
14572 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14573 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14574 }
14575
b06f4c80 14576 dev->mode_config.fb_base = ggtt->gmadr.start;
79e53945 14577
28c97730 14578 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
14579 INTEL_INFO(dev_priv)->num_pipes,
14580 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 14581
055e393f 14582 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
14583 int ret;
14584
5ab0d85b 14585 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
14586 if (ret) {
14587 drm_mode_config_cleanup(dev);
14588 return ret;
14589 }
79e53945
JB
14590 }
14591
e72f9fbf 14592 intel_shared_dpll_init(dev);
ee7b9f93 14593
5be6e334
VS
14594 intel_update_czclk(dev_priv);
14595 intel_modeset_init_hw(dev);
14596
b2045352 14597 if (dev_priv->max_cdclk_freq == 0)
4c75b940 14598 intel_update_max_cdclk(dev_priv);
b2045352 14599
9cce37f4 14600 /* Just disable it once at startup */
29b74b7f 14601 i915_disable_vga(dev_priv);
c39055b0 14602 intel_setup_outputs(dev_priv);
11be49eb 14603
6e9f798d 14604 drm_modeset_lock_all(dev);
aecd36b8 14605 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 14606 drm_modeset_unlock_all(dev);
46f297fb 14607
d3fcc808 14608 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
14609 struct intel_initial_plane_config plane_config = {};
14610
46f297fb
JB
14611 if (!crtc->active)
14612 continue;
14613
46f297fb 14614 /*
46f297fb
JB
14615 * Note that reserving the BIOS fb up front prevents us
14616 * from stuffing other stolen allocations like the ring
14617 * on top. This prevents some ugliness at boot time, and
14618 * can even allow for smooth boot transitions if the BIOS
14619 * fb is large enough for the active pipe configuration.
14620 */
eeebeac5
ML
14621 dev_priv->display.get_initial_plane_config(crtc,
14622 &plane_config);
14623
14624 /*
14625 * If the fb is shared between multiple heads, we'll
14626 * just get the first one.
14627 */
14628 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 14629 }
d93c0372
MR
14630
14631 /*
14632 * Make sure hardware watermarks really match the state we read out.
14633 * Note that we need to do this after reconstructing the BIOS fb's
14634 * since the watermark calculation done here will use pstate->fb.
14635 */
602ae835
VS
14636 if (!HAS_GMCH_DISPLAY(dev_priv))
14637 sanitize_watermarks(dev);
b079bd17
VS
14638
14639 return 0;
2c7111db
CW
14640}
14641
2ee0da16
VS
14642void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
14643{
14644 /* 640x480@60Hz, ~25175 kHz */
14645 struct dpll clock = {
14646 .m1 = 18,
14647 .m2 = 7,
14648 .p1 = 13,
14649 .p2 = 4,
14650 .n = 2,
14651 };
14652 u32 dpll, fp;
14653 int i;
14654
14655 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
14656
14657 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
14658 pipe_name(pipe), clock.vco, clock.dot);
14659
14660 fp = i9xx_dpll_compute_fp(&clock);
14661 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
14662 DPLL_VGA_MODE_DIS |
14663 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
14664 PLL_P2_DIVIDE_BY_4 |
14665 PLL_REF_INPUT_DREFCLK |
14666 DPLL_VCO_ENABLE;
14667
14668 I915_WRITE(FP0(pipe), fp);
14669 I915_WRITE(FP1(pipe), fp);
14670
14671 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
14672 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
14673 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
14674 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
14675 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
14676 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
14677 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
14678
14679 /*
14680 * Apparently we need to have VGA mode enabled prior to changing
14681 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
14682 * dividers, even though the register value does change.
14683 */
14684 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
14685 I915_WRITE(DPLL(pipe), dpll);
14686
14687 /* Wait for the clocks to stabilize. */
14688 POSTING_READ(DPLL(pipe));
14689 udelay(150);
14690
14691 /* The pixel multiplier can only be updated once the
14692 * DPLL is enabled and the clocks are stable.
14693 *
14694 * So write it again.
14695 */
14696 I915_WRITE(DPLL(pipe), dpll);
14697
14698 /* We do this three times for luck */
14699 for (i = 0; i < 3 ; i++) {
14700 I915_WRITE(DPLL(pipe), dpll);
14701 POSTING_READ(DPLL(pipe));
14702 udelay(150); /* wait for warmup */
14703 }
14704
14705 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
14706 POSTING_READ(PIPECONF(pipe));
14707}
14708
14709void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
14710{
a87e55f8
VS
14711 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
14712
2ee0da16
VS
14713 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
14714 pipe_name(pipe));
14715
4488496d
VS
14716 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
14717 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
14718 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
14719 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
14720 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
2ee0da16
VS
14721
14722 I915_WRITE(PIPECONF(pipe), 0);
14723 POSTING_READ(PIPECONF(pipe));
14724
a87e55f8 14725 intel_wait_for_pipe_scanline_stopped(crtc);
2ee0da16
VS
14726
14727 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
14728 POSTING_READ(DPLL(pipe));
14729}
14730
23ac1273
VS
14731static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
14732 struct intel_plane *primary)
fa555837 14733{
b7f05d4a 14734 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
23ac1273
VS
14735 enum plane plane = primary->plane;
14736 u32 val = I915_READ(DSPCNTR(plane));
fa555837 14737
23ac1273
VS
14738 return (val & DISPLAY_PLANE_ENABLE) == 0 ||
14739 (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
14740}
fa555837 14741
23ac1273
VS
14742static void
14743intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
14744{
14745 struct intel_crtc *crtc;
fa555837 14746
23ac1273
VS
14747 if (INTEL_GEN(dev_priv) >= 4)
14748 return;
fa555837 14749
23ac1273
VS
14750 for_each_intel_crtc(&dev_priv->drm, crtc) {
14751 struct intel_plane *plane =
14752 to_intel_plane(crtc->base.primary);
14753
14754 if (intel_plane_mapping_ok(crtc, plane))
14755 continue;
14756
14757 DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
14758 plane->base.name);
14759 intel_plane_disable_noatomic(crtc, plane);
14760 }
fa555837
DV
14761}
14762
02e93c35
VS
14763static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14764{
14765 struct drm_device *dev = crtc->base.dev;
14766 struct intel_encoder *encoder;
14767
14768 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14769 return true;
14770
14771 return false;
14772}
14773
496b0fc3
ML
14774static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
14775{
14776 struct drm_device *dev = encoder->base.dev;
14777 struct intel_connector *connector;
14778
14779 for_each_connector_on_encoder(dev, &encoder->base, connector)
14780 return connector;
14781
14782 return NULL;
14783}
14784
a168f5b3 14785static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
ecf837d9 14786 enum pipe pch_transcoder)
a168f5b3
VS
14787{
14788 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
ecf837d9 14789 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
a168f5b3
VS
14790}
14791
aecd36b8
VS
14792static void intel_sanitize_crtc(struct intel_crtc *crtc,
14793 struct drm_modeset_acquire_ctx *ctx)
24929352
DV
14794{
14795 struct drm_device *dev = crtc->base.dev;
fac5e23e 14796 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 14797 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 14798
24929352 14799 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
14800 if (!transcoder_is_dsi(cpu_transcoder)) {
14801 i915_reg_t reg = PIPECONF(cpu_transcoder);
14802
14803 I915_WRITE(reg,
14804 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14805 }
24929352 14806
d3eaf884 14807 /* restore vblank interrupts to correct state */
9625604c 14808 drm_crtc_vblank_reset(&crtc->base);
d297e103 14809 if (crtc->active) {
f9cd7b88
VS
14810 struct intel_plane *plane;
14811
9625604c 14812 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
14813
14814 /* Disable everything but the primary plane */
14815 for_each_intel_plane_on_crtc(dev, crtc, plane) {
23ac1273
VS
14816 const struct intel_plane_state *plane_state =
14817 to_intel_plane_state(plane->base.state);
f9cd7b88 14818
23ac1273
VS
14819 if (plane_state->base.visible &&
14820 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
14821 intel_plane_disable_noatomic(crtc, plane);
f9cd7b88 14822 }
9625604c 14823 }
d3eaf884 14824
24929352
DV
14825 /* Adjust the state of the output pipe according to whether we
14826 * have active connectors/encoders. */
842e0307 14827 if (crtc->active && !intel_crtc_has_encoders(crtc))
da1d0e26 14828 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 14829
49cff963 14830 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
DV
14831 /*
14832 * We start out with underrun reporting disabled to avoid races.
14833 * For correct bookkeeping mark this on active crtcs.
14834 *
c5ab3bc0
DV
14835 * Also on gmch platforms we dont have any hardware bits to
14836 * disable the underrun reporting. Which means we need to start
14837 * out with underrun reporting disabled also on inactive pipes,
14838 * since otherwise we'll complain about the garbage we read when
14839 * e.g. coming up after runtime pm.
14840 *
4cc31489
DV
14841 * No protection against concurrent access is required - at
14842 * worst a fifo underrun happens which also sets this to false.
14843 */
14844 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
14845 /*
14846 * We track the PCH trancoder underrun reporting state
14847 * within the crtc. With crtc for pipe A housing the underrun
14848 * reporting state for PCH transcoder A, crtc for pipe B housing
14849 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
14850 * and marking underrun reporting as disabled for the non-existing
14851 * PCH transcoders B and C would prevent enabling the south
14852 * error interrupt (see cpt_can_enable_serr_int()).
14853 */
ecf837d9 14854 if (has_pch_trancoder(dev_priv, crtc->pipe))
a168f5b3 14855 crtc->pch_fifo_underrun_disabled = true;
4cc31489 14856 }
24929352
DV
14857}
14858
14859static void intel_sanitize_encoder(struct intel_encoder *encoder)
14860{
14861 struct intel_connector *connector;
24929352
DV
14862
14863 /* We need to check both for a crtc link (meaning that the
14864 * encoder is active and trying to read from a pipe) and the
14865 * pipe itself being active. */
14866 bool has_active_crtc = encoder->base.crtc &&
14867 to_intel_crtc(encoder->base.crtc)->active;
14868
496b0fc3
ML
14869 connector = intel_encoder_find_connector(encoder);
14870 if (connector && !has_active_crtc) {
24929352
DV
14871 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14872 encoder->base.base.id,
8e329a03 14873 encoder->base.name);
24929352
DV
14874
14875 /* Connector is active, but has no active pipe. This is
14876 * fallout from our resume register restoring. Disable
14877 * the encoder manually again. */
14878 if (encoder->base.crtc) {
fd6bbda9
ML
14879 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
14880
24929352
DV
14881 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14882 encoder->base.base.id,
8e329a03 14883 encoder->base.name);
fd6bbda9 14884 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 14885 if (encoder->post_disable)
fd6bbda9 14886 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 14887 }
7f1950fb 14888 encoder->base.crtc = NULL;
24929352
DV
14889
14890 /* Inconsistent output/port/pipe state happens presumably due to
14891 * a bug in one of the get_hw_state functions. Or someplace else
14892 * in our code, like the register restore mess on resume. Clamp
14893 * things to off as a safer default. */
fd6bbda9
ML
14894
14895 connector->base.dpms = DRM_MODE_DPMS_OFF;
14896 connector->base.encoder = NULL;
24929352
DV
14897 }
14898 /* Enabled encoders without active connectors will be fixed in
14899 * the crtc fixup. */
14900}
14901
29b74b7f 14902void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 14903{
920a14b2 14904 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 14905
04098753
ID
14906 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14907 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 14908 i915_disable_vga(dev_priv);
04098753
ID
14909 }
14910}
14911
29b74b7f 14912void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 14913{
8dc8a27c
PZ
14914 /* This function can be called both from intel_modeset_setup_hw_state or
14915 * at a very early point in our resume sequence, where the power well
14916 * structures are not yet restored. Since this function is at a very
14917 * paranoid "someone might have enabled VGA while we were not looking"
14918 * level, just check if the power well is enabled instead of trying to
14919 * follow the "don't touch the power well if we don't need it" policy
14920 * the rest of the driver uses. */
6392f847 14921 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14922 return;
14923
29b74b7f 14924 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
14925
14926 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
14927}
14928
f9cd7b88
VS
14929/* FIXME read out full plane state for all planes */
14930static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 14931{
23ac1273
VS
14932 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14933 struct intel_crtc_state *crtc_state =
14934 to_intel_crtc_state(crtc->base.state);
14935 struct intel_plane *plane;
d032ffa0 14936
23ac1273
VS
14937 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14938 struct intel_plane_state *plane_state =
14939 to_intel_plane_state(plane->base.state);
14940 bool visible = plane->get_hw_state(plane);
b26d3ea3 14941
23ac1273
VS
14942 intel_set_plane_visible(crtc_state, plane_state, visible);
14943 }
98ec7739
VS
14944}
14945
30e984df 14946static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 14947{
fac5e23e 14948 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 14949 enum pipe pipe;
24929352
DV
14950 struct intel_crtc *crtc;
14951 struct intel_encoder *encoder;
14952 struct intel_connector *connector;
f9e905ca 14953 struct drm_connector_list_iter conn_iter;
5358901f 14954 int i;
24929352 14955
565602d7
ML
14956 dev_priv->active_crtcs = 0;
14957
d3fcc808 14958 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
14959 struct intel_crtc_state *crtc_state =
14960 to_intel_crtc_state(crtc->base.state);
3b117c8f 14961
ec2dc6a0 14962 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
14963 memset(crtc_state, 0, sizeof(*crtc_state));
14964 crtc_state->base.crtc = &crtc->base;
24929352 14965
565602d7
ML
14966 crtc_state->base.active = crtc_state->base.enable =
14967 dev_priv->display.get_pipe_config(crtc, crtc_state);
14968
14969 crtc->base.enabled = crtc_state->base.enable;
14970 crtc->active = crtc_state->base.active;
14971
aca1ebf4 14972 if (crtc_state->base.active)
565602d7
ML
14973 dev_priv->active_crtcs |= 1 << crtc->pipe;
14974
f9cd7b88 14975 readout_plane_state(crtc);
24929352 14976
78108b7c
VS
14977 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
14978 crtc->base.base.id, crtc->base.name,
a8cd6da0 14979 enableddisabled(crtc_state->base.active));
24929352
DV
14980 }
14981
5358901f
DV
14982 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14983 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14984
2edd6443 14985 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
2c42e535
ACO
14986 &pll->state.hw_state);
14987 pll->state.crtc_mask = 0;
d3fcc808 14988 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
14989 struct intel_crtc_state *crtc_state =
14990 to_intel_crtc_state(crtc->base.state);
14991
14992 if (crtc_state->base.active &&
14993 crtc_state->shared_dpll == pll)
2c42e535 14994 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 14995 }
2c42e535 14996 pll->active_mask = pll->state.crtc_mask;
5358901f 14997
1e6f2ddc 14998 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
2c42e535 14999 pll->name, pll->state.crtc_mask, pll->on);
5358901f
DV
15000 }
15001
b2784e15 15002 for_each_intel_encoder(dev, encoder) {
24929352
DV
15003 pipe = 0;
15004
15005 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15006 struct intel_crtc_state *crtc_state;
15007
98187836 15008 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15009 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15010
045ac3b5 15011 encoder->base.crtc = &crtc->base;
a8cd6da0
VS
15012 crtc_state->output_types |= 1 << encoder->type;
15013 encoder->get_config(encoder, crtc_state);
24929352
DV
15014 } else {
15015 encoder->base.crtc = NULL;
15016 }
15017
6f2bcceb 15018 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15019 encoder->base.base.id, encoder->base.name,
15020 enableddisabled(encoder->base.crtc),
6f2bcceb 15021 pipe_name(pipe));
24929352
DV
15022 }
15023
f9e905ca
DV
15024 drm_connector_list_iter_begin(dev, &conn_iter);
15025 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
DV
15026 if (connector->get_hw_state(connector)) {
15027 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15028
15029 encoder = connector->encoder;
15030 connector->base.encoder = &encoder->base;
15031
15032 if (encoder->base.crtc &&
15033 encoder->base.crtc->state->active) {
15034 /*
15035 * This has to be done during hardware readout
15036 * because anything calling .crtc_disable may
15037 * rely on the connector_mask being accurate.
15038 */
15039 encoder->base.crtc->state->connector_mask |=
15040 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15041 encoder->base.crtc->state->encoder_mask |=
15042 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15043 }
15044
24929352
DV
15045 } else {
15046 connector->base.dpms = DRM_MODE_DPMS_OFF;
15047 connector->base.encoder = NULL;
15048 }
15049 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15050 connector->base.base.id, connector->base.name,
15051 enableddisabled(connector->base.encoder));
24929352 15052 }
f9e905ca 15053 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15054
15055 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15056 struct intel_crtc_state *crtc_state =
15057 to_intel_crtc_state(crtc->base.state);
d305e061 15058 int min_cdclk = 0;
aca1ebf4 15059
7f4c6284 15060 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15061 if (crtc_state->base.active) {
15062 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
15063 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15064 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15065
15066 /*
15067 * The initial mode needs to be set in order to keep
15068 * the atomic core happy. It wants a valid mode if the
15069 * crtc's enabled, so we do the above call.
15070 *
7800fb69
DV
15071 * But we don't set all the derived state fully, hence
15072 * set a flag to indicate that a full recalculation is
15073 * needed on the next commit.
7f4c6284 15074 */
a8cd6da0 15075 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15076
a7d1b3f4
VS
15077 intel_crtc_compute_pixel_rate(crtc_state);
15078
9c61de4c 15079 if (dev_priv->display.modeset_calc_cdclk) {
d305e061 15080 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
9c61de4c
VS
15081 if (WARN_ON(min_cdclk < 0))
15082 min_cdclk = 0;
15083 }
aca1ebf4 15084
5caa0fea
DV
15085 drm_calc_timestamping_constants(&crtc->base,
15086 &crtc_state->base.adjusted_mode);
9eca6832 15087 update_scanline_offset(crtc);
7f4c6284 15088 }
e3b247da 15089
d305e061 15090 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
aca1ebf4 15091
a8cd6da0 15092 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15093 }
30e984df
DV
15094}
15095
62b69566
ACO
15096static void
15097get_encoder_power_domains(struct drm_i915_private *dev_priv)
15098{
15099 struct intel_encoder *encoder;
15100
15101 for_each_intel_encoder(&dev_priv->drm, encoder) {
15102 u64 get_domains;
15103 enum intel_display_power_domain domain;
15104
15105 if (!encoder->get_power_domains)
15106 continue;
15107
15108 get_domains = encoder->get_power_domains(encoder);
15109 for_each_power_domain(domain, get_domains)
15110 intel_display_power_get(dev_priv, domain);
15111 }
15112}
15113
043e9bda
ML
15114/* Scan out the current hw modeset state,
15115 * and sanitizes it to the current state
15116 */
15117static void
aecd36b8
VS
15118intel_modeset_setup_hw_state(struct drm_device *dev,
15119 struct drm_modeset_acquire_ctx *ctx)
30e984df 15120{
fac5e23e 15121 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 15122 enum pipe pipe;
30e984df
DV
15123 struct intel_crtc *crtc;
15124 struct intel_encoder *encoder;
35c95375 15125 int i;
30e984df 15126
6ac43272
VS
15127 if (IS_HASWELL(dev_priv)) {
15128 /*
15129 * WaRsPkgCStateDisplayPMReq:hsw
15130 * System hang if this isn't done before disabling all planes!
15131 */
15132 I915_WRITE(CHICKEN_PAR1_1,
15133 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15134 }
15135
30e984df 15136 intel_modeset_readout_hw_state(dev);
24929352
DV
15137
15138 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
15139 get_encoder_power_domains(dev_priv);
15140
23ac1273
VS
15141 intel_sanitize_plane_mapping(dev_priv);
15142
b2784e15 15143 for_each_intel_encoder(dev, encoder) {
24929352
DV
15144 intel_sanitize_encoder(encoder);
15145 }
15146
055e393f 15147 for_each_pipe(dev_priv, pipe) {
98187836 15148 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 15149
aecd36b8 15150 intel_sanitize_crtc(crtc, ctx);
6e3c9717
ACO
15151 intel_dump_pipe_config(crtc, crtc->config,
15152 "[setup_hw_state]");
24929352 15153 }
9a935856 15154
d29b2f9d
ACO
15155 intel_modeset_update_connector_atomic_state(dev);
15156
35c95375
DV
15157 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15158 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15159
2dd66ebd 15160 if (!pll->on || pll->active_mask)
35c95375
DV
15161 continue;
15162
15163 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15164
2edd6443 15165 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15166 pll->on = false;
15167 }
15168
04548cba
VS
15169 if (IS_G4X(dev_priv)) {
15170 g4x_wm_get_hw_state(dev);
15171 g4x_wm_sanitize(dev_priv);
15172 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 15173 vlv_wm_get_hw_state(dev);
602ae835 15174 vlv_wm_sanitize(dev_priv);
a029fa4d 15175 } else if (INTEL_GEN(dev_priv) >= 9) {
3078999f 15176 skl_wm_get_hw_state(dev);
602ae835 15177 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 15178 ilk_wm_get_hw_state(dev);
602ae835 15179 }
292b990e
ML
15180
15181 for_each_intel_crtc(dev, crtc) {
d8fc70b7 15182 u64 put_domains;
292b990e 15183
74bff5f9 15184 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15185 if (WARN_ON(put_domains))
15186 modeset_put_power_domains(dev_priv, put_domains);
15187 }
15188 intel_display_set_init_power(dev_priv, false);
010cf73d 15189
8d8c386c
ID
15190 intel_power_domains_verify_state(dev_priv);
15191
010cf73d 15192 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15193}
7d0bc1ea 15194
043e9bda
ML
15195void intel_display_resume(struct drm_device *dev)
15196{
e2c8b870
ML
15197 struct drm_i915_private *dev_priv = to_i915(dev);
15198 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15199 struct drm_modeset_acquire_ctx ctx;
043e9bda 15200 int ret;
f30da187 15201
e2c8b870 15202 dev_priv->modeset_restore_state = NULL;
73974893
ML
15203 if (state)
15204 state->acquire_ctx = &ctx;
043e9bda 15205
e2c8b870 15206 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15207
73974893
ML
15208 while (1) {
15209 ret = drm_modeset_lock_all_ctx(dev, &ctx);
15210 if (ret != -EDEADLK)
15211 break;
043e9bda 15212
e2c8b870 15213 drm_modeset_backoff(&ctx);
e2c8b870 15214 }
043e9bda 15215
73974893 15216 if (!ret)
581e49fe 15217 ret = __intel_display_resume(dev, state, &ctx);
73974893 15218
2503a0fe 15219 intel_enable_ipc(dev_priv);
e2c8b870
ML
15220 drm_modeset_drop_locks(&ctx);
15221 drm_modeset_acquire_fini(&ctx);
043e9bda 15222
0853695c 15223 if (ret)
e2c8b870 15224 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
15225 if (state)
15226 drm_atomic_state_put(state);
2c7111db
CW
15227}
15228
15229void intel_modeset_gem_init(struct drm_device *dev)
15230{
dc97997a 15231 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 15232
dc97997a 15233 intel_init_gt_powersave(dev_priv);
ae48434c 15234
6ac43272
VS
15235 intel_init_clock_gating(dev_priv);
15236
1ee8da6d 15237 intel_setup_overlay(dev_priv);
1ebaa0b9
CW
15238}
15239
15240int intel_connector_register(struct drm_connector *connector)
15241{
15242 struct intel_connector *intel_connector = to_intel_connector(connector);
15243 int ret;
15244
15245 ret = intel_backlight_device_register(intel_connector);
15246 if (ret)
15247 goto err;
15248
15249 return 0;
0962c3c9 15250
1ebaa0b9
CW
15251err:
15252 return ret;
79e53945
JB
15253}
15254
c191eca1 15255void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 15256{
e63d87c0 15257 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 15258
e63d87c0 15259 intel_backlight_device_unregister(intel_connector);
4932e2c3 15260 intel_panel_destroy_backlight(connector);
4932e2c3
ID
15261}
15262
713946d1
MN
15263static void intel_hpd_poll_fini(struct drm_device *dev)
15264{
15265 struct intel_connector *connector;
15266 struct drm_connector_list_iter conn_iter;
15267
15268 /* First disable polling... */
15269 drm_kms_helper_poll_fini(dev);
15270
15271 /* Then kill the work that may have been queued by hpd. */
15272 drm_connector_list_iter_begin(dev, &conn_iter);
15273 for_each_intel_connector_iter(connector, &conn_iter) {
15274 if (connector->modeset_retry_work.func)
15275 cancel_work_sync(&connector->modeset_retry_work);
15276 }
15277 drm_connector_list_iter_end(&conn_iter);
15278}
15279
79e53945
JB
15280void intel_modeset_cleanup(struct drm_device *dev)
15281{
fac5e23e 15282 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 15283
eb955eee
CW
15284 flush_work(&dev_priv->atomic_helper.free_work);
15285 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
15286
dc97997a 15287 intel_disable_gt_powersave(dev_priv);
2eb5252e 15288
fd0c0642
DV
15289 /*
15290 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15291 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15292 * experience fancy races otherwise.
15293 */
2aeb7d3a 15294 intel_irq_uninstall(dev_priv);
eb21b92b 15295
fd0c0642
DV
15296 /*
15297 * Due to the hpd irq storm handling the hotplug work can re-arm the
15298 * poll handlers. Hence disable polling after hpd handling is shut down.
15299 */
713946d1 15300 intel_hpd_poll_fini(dev);
fd0c0642 15301
4f256d82
DV
15302 /* poll work can call into fbdev, hence clean that up afterwards */
15303 intel_fbdev_fini(dev_priv);
15304
723bfd70
JB
15305 intel_unregister_dsm_handler();
15306
c937ab3e 15307 intel_fbc_global_disable(dev_priv);
69341a5e 15308
1630fe75
CW
15309 /* flush any delayed tasks or pending work */
15310 flush_scheduled_work();
15311
79e53945 15312 drm_mode_config_cleanup(dev);
4d7bb011 15313
1ee8da6d 15314 intel_cleanup_overlay(dev_priv);
ae48434c 15315
dc97997a 15316 intel_cleanup_gt_powersave(dev_priv);
f5949141 15317
40196446 15318 intel_teardown_gmbus(dev_priv);
eda41bdc
VS
15319
15320 destroy_workqueue(dev_priv->modeset_wq);
79e53945
JB
15321}
15322
df0e9248
CW
15323void intel_connector_attach_encoder(struct intel_connector *connector,
15324 struct intel_encoder *encoder)
15325{
15326 connector->encoder = encoder;
15327 drm_mode_connector_attach_encoder(&connector->base,
15328 &encoder->base);
79e53945 15329}
28d52043
DA
15330
15331/*
15332 * set vga decode state - true == enable VGA decode
15333 */
6315b5d3 15334int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 15335{
6315b5d3 15336 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15337 u16 gmch_ctrl;
15338
75fa041d
CW
15339 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15340 DRM_ERROR("failed to read control word\n");
15341 return -EIO;
15342 }
15343
c0cc8a55
CW
15344 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15345 return 0;
15346
28d52043
DA
15347 if (state)
15348 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15349 else
15350 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15351
15352 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15353 DRM_ERROR("failed to write control word\n");
15354 return -EIO;
15355 }
15356
28d52043
DA
15357 return 0;
15358}
c4a1d9e4 15359
98a2f411
CW
15360#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15361
c4a1d9e4 15362struct intel_display_error_state {
ff57f1b0
PZ
15363
15364 u32 power_well_driver;
15365
63b66e5b
CW
15366 int num_transcoders;
15367
c4a1d9e4
CW
15368 struct intel_cursor_error_state {
15369 u32 control;
15370 u32 position;
15371 u32 base;
15372 u32 size;
52331309 15373 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15374
15375 struct intel_pipe_error_state {
ddf9c536 15376 bool power_domain_on;
c4a1d9e4 15377 u32 source;
f301b1e1 15378 u32 stat;
52331309 15379 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15380
15381 struct intel_plane_error_state {
15382 u32 control;
15383 u32 stride;
15384 u32 size;
15385 u32 pos;
15386 u32 addr;
15387 u32 surface;
15388 u32 tile_offset;
52331309 15389 } plane[I915_MAX_PIPES];
63b66e5b
CW
15390
15391 struct intel_transcoder_error_state {
ddf9c536 15392 bool power_domain_on;
63b66e5b
CW
15393 enum transcoder cpu_transcoder;
15394
15395 u32 conf;
15396
15397 u32 htotal;
15398 u32 hblank;
15399 u32 hsync;
15400 u32 vtotal;
15401 u32 vblank;
15402 u32 vsync;
15403 } transcoder[4];
c4a1d9e4
CW
15404};
15405
15406struct intel_display_error_state *
c033666a 15407intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 15408{
c4a1d9e4 15409 struct intel_display_error_state *error;
63b66e5b
CW
15410 int transcoders[] = {
15411 TRANSCODER_A,
15412 TRANSCODER_B,
15413 TRANSCODER_C,
15414 TRANSCODER_EDP,
15415 };
c4a1d9e4
CW
15416 int i;
15417
c033666a 15418 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
15419 return NULL;
15420
9d1cb914 15421 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15422 if (error == NULL)
15423 return NULL;
15424
c033666a 15425 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9c3a16c8
ID
15426 error->power_well_driver =
15427 I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
ff57f1b0 15428
055e393f 15429 for_each_pipe(dev_priv, i) {
ddf9c536 15430 error->pipe[i].power_domain_on =
f458ebbc
DV
15431 __intel_display_power_is_enabled(dev_priv,
15432 POWER_DOMAIN_PIPE(i));
ddf9c536 15433 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15434 continue;
15435
5efb3e28
VS
15436 error->cursor[i].control = I915_READ(CURCNTR(i));
15437 error->cursor[i].position = I915_READ(CURPOS(i));
15438 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15439
15440 error->plane[i].control = I915_READ(DSPCNTR(i));
15441 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 15442 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 15443 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15444 error->plane[i].pos = I915_READ(DSPPOS(i));
15445 }
c033666a 15446 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 15447 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 15448 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
15449 error->plane[i].surface = I915_READ(DSPSURF(i));
15450 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15451 }
15452
c4a1d9e4 15453 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15454
c033666a 15455 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 15456 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15457 }
15458
4d1de975 15459 /* Note: this does not include DSI transcoders. */
c033666a 15460 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 15461 if (HAS_DDI(dev_priv))
63b66e5b
CW
15462 error->num_transcoders++; /* Account for eDP. */
15463
15464 for (i = 0; i < error->num_transcoders; i++) {
15465 enum transcoder cpu_transcoder = transcoders[i];
15466
ddf9c536 15467 error->transcoder[i].power_domain_on =
f458ebbc 15468 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15469 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15470 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15471 continue;
15472
63b66e5b
CW
15473 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15474
15475 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15476 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15477 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15478 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15479 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15480 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15481 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15482 }
15483
15484 return error;
15485}
15486
edc3d884
MK
15487#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15488
c4a1d9e4 15489void
edc3d884 15490intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15491 struct intel_display_error_state *error)
15492{
5a4c6f1b 15493 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
15494 int i;
15495
63b66e5b
CW
15496 if (!error)
15497 return;
15498
b7f05d4a 15499 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 15500 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 15501 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15502 error->power_well_driver);
055e393f 15503 for_each_pipe(dev_priv, i) {
edc3d884 15504 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 15505 err_printf(m, " Power: %s\n",
87ad3212 15506 onoff(error->pipe[i].power_domain_on));
edc3d884 15507 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15508 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15509
15510 err_printf(m, "Plane [%d]:\n", i);
15511 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15512 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 15513 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
15514 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15515 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15516 }
772c2a51 15517 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 15518 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 15519 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
15520 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15521 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15522 }
15523
edc3d884
MK
15524 err_printf(m, "Cursor [%d]:\n", i);
15525 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15526 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15527 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15528 }
63b66e5b
CW
15529
15530 for (i = 0; i < error->num_transcoders; i++) {
da205630 15531 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 15532 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 15533 err_printf(m, " Power: %s\n",
87ad3212 15534 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
15535 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15536 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15537 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15538 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15539 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15540 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15541 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15542 }
c4a1d9e4 15543}
98a2f411
CW
15544
15545#endif