]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_display.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.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 <[email protected]>
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>
72fdb40c 49#include <drm/drm_atomic_uapi.h>
c0f372b3 50#include <linux/dma_remapping.h>
fd8e058a 51#include <linux/reservation.h>
79e53945 52
465c120c 53/* Primary plane formats for gen <= 3 */
568db4f2 54static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
55 DRM_FORMAT_C8,
56 DRM_FORMAT_RGB565,
465c120c 57 DRM_FORMAT_XRGB1555,
67fe7dc5 58 DRM_FORMAT_XRGB8888,
465c120c
MR
59};
60
61/* Primary plane formats for gen >= 4 */
568db4f2 62static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
63 DRM_FORMAT_C8,
64 DRM_FORMAT_RGB565,
65 DRM_FORMAT_XRGB8888,
66 DRM_FORMAT_XBGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_XBGR2101010,
69};
70
714244e2
BW
71static const uint64_t i9xx_format_modifiers[] = {
72 I915_FORMAT_MOD_X_TILED,
73 DRM_FORMAT_MOD_LINEAR,
74 DRM_FORMAT_MOD_INVALID
75};
76
6c0fd451 77static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
78 DRM_FORMAT_C8,
79 DRM_FORMAT_RGB565,
80 DRM_FORMAT_XRGB8888,
465c120c 81 DRM_FORMAT_XBGR8888,
67fe7dc5 82 DRM_FORMAT_ARGB8888,
465c120c
MR
83 DRM_FORMAT_ABGR8888,
84 DRM_FORMAT_XRGB2101010,
465c120c 85 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
86 DRM_FORMAT_YUYV,
87 DRM_FORMAT_YVYU,
88 DRM_FORMAT_UYVY,
89 DRM_FORMAT_VYUY,
465c120c
MR
90};
91
c0b56ab5
CK
92static const uint32_t skl_pri_planar_formats[] = {
93 DRM_FORMAT_C8,
94 DRM_FORMAT_RGB565,
95 DRM_FORMAT_XRGB8888,
96 DRM_FORMAT_XBGR8888,
97 DRM_FORMAT_ARGB8888,
98 DRM_FORMAT_ABGR8888,
99 DRM_FORMAT_XRGB2101010,
100 DRM_FORMAT_XBGR2101010,
101 DRM_FORMAT_YUYV,
102 DRM_FORMAT_YVYU,
103 DRM_FORMAT_UYVY,
104 DRM_FORMAT_VYUY,
105 DRM_FORMAT_NV12,
106};
107
714244e2
BW
108static const uint64_t skl_format_modifiers_noccs[] = {
109 I915_FORMAT_MOD_Yf_TILED,
110 I915_FORMAT_MOD_Y_TILED,
111 I915_FORMAT_MOD_X_TILED,
112 DRM_FORMAT_MOD_LINEAR,
113 DRM_FORMAT_MOD_INVALID
114};
115
116static const uint64_t skl_format_modifiers_ccs[] = {
117 I915_FORMAT_MOD_Yf_TILED_CCS,
118 I915_FORMAT_MOD_Y_TILED_CCS,
119 I915_FORMAT_MOD_Yf_TILED,
120 I915_FORMAT_MOD_Y_TILED,
121 I915_FORMAT_MOD_X_TILED,
122 DRM_FORMAT_MOD_LINEAR,
123 DRM_FORMAT_MOD_INVALID
124};
125
3d7d6510
MR
126/* Cursor formats */
127static const uint32_t intel_cursor_formats[] = {
128 DRM_FORMAT_ARGB8888,
129};
130
714244e2
BW
131static const uint64_t cursor_format_modifiers[] = {
132 DRM_FORMAT_MOD_LINEAR,
133 DRM_FORMAT_MOD_INVALID
134};
135
f1f644dc 136static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 137 struct intel_crtc_state *pipe_config);
18442d08 138static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 139 struct intel_crtc_state *pipe_config);
f1f644dc 140
24dbf51a
CW
141static int intel_framebuffer_init(struct intel_framebuffer *ifb,
142 struct drm_i915_gem_object *obj,
143 struct drm_mode_fb_cmd2 *mode_cmd);
5b18e57c
SV
144static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
145static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 146static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 147static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
148 struct intel_link_m_n *m_n,
149 struct intel_link_m_n *m2_n2);
29407aab 150static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 151static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 152static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 153static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 154 const struct intel_crtc_state *pipe_config);
d288f65f 155static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 156 const struct intel_crtc_state *pipe_config);
5a21b665
SV
157static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
158static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
1c74eeaf
NM
159static void intel_crtc_init_scalers(struct intel_crtc *crtc,
160 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
161static void skylake_pfit_enable(struct intel_crtc *crtc);
162static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
163static void ironlake_pfit_enable(struct intel_crtc *crtc);
aecd36b8
VS
164static void intel_modeset_setup_hw_state(struct drm_device *dev,
165 struct drm_modeset_acquire_ctx *ctx);
2622a081 166static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 167
d4906093 168struct intel_limit {
4c5def93
ACO
169 struct {
170 int min, max;
171 } dot, vco, n, m, m1, m2, p, p1;
172
173 struct {
174 int dot_limit;
175 int p2_slow, p2_fast;
176 } p2;
d4906093 177};
79e53945 178
bfa7df01 179/* returns HPLL frequency in kHz */
49cd97a3 180int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
181{
182 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
183
184 /* Obtain SKU information */
185 mutex_lock(&dev_priv->sb_lock);
186 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
187 CCK_FUSE_HPLL_FREQ_MASK;
188 mutex_unlock(&dev_priv->sb_lock);
189
190 return vco_freq[hpll_freq] * 1000;
191}
192
c30fec65
VS
193int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
194 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
195{
196 u32 val;
197 int divider;
198
bfa7df01
VS
199 mutex_lock(&dev_priv->sb_lock);
200 val = vlv_cck_read(dev_priv, reg);
201 mutex_unlock(&dev_priv->sb_lock);
202
203 divider = val & CCK_FREQUENCY_VALUES;
204
205 WARN((val & CCK_FREQUENCY_STATUS) !=
206 (divider << CCK_FREQUENCY_STATUS_SHIFT),
207 "%s change in progress\n", name);
208
c30fec65
VS
209 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
210}
211
7ff89ca2
VS
212int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
213 const char *name, u32 reg)
c30fec65
VS
214{
215 if (dev_priv->hpll_freq == 0)
49cd97a3 216 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65
VS
217
218 return vlv_get_cck_clock(dev_priv, name, reg,
219 dev_priv->hpll_freq);
bfa7df01
VS
220}
221
bfa7df01
VS
222static void intel_update_czclk(struct drm_i915_private *dev_priv)
223{
666a4537 224 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
225 return;
226
227 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
228 CCK_CZ_CLOCK_CONTROL);
229
230 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
231}
232
021357ac 233static inline u32 /* units of 100MHz */
21a727b3
VS
234intel_fdi_link_freq(struct drm_i915_private *dev_priv,
235 const struct intel_crtc_state *pipe_config)
021357ac 236{
21a727b3
VS
237 if (HAS_DDI(dev_priv))
238 return pipe_config->port_clock; /* SPLL */
e3b247da 239 else
58ecd9d5 240 return dev_priv->fdi_pll_freq;
021357ac
CW
241}
242
1b6f4958 243static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 244 .dot = { .min = 25000, .max = 350000 },
9c333719 245 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 246 .n = { .min = 2, .max = 16 },
0206e353
AJ
247 .m = { .min = 96, .max = 140 },
248 .m1 = { .min = 18, .max = 26 },
249 .m2 = { .min = 6, .max = 16 },
250 .p = { .min = 4, .max = 128 },
251 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
252 .p2 = { .dot_limit = 165000,
253 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
254};
255
1b6f4958 256static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 257 .dot = { .min = 25000, .max = 350000 },
9c333719 258 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 259 .n = { .min = 2, .max = 16 },
5d536e28
SV
260 .m = { .min = 96, .max = 140 },
261 .m1 = { .min = 18, .max = 26 },
262 .m2 = { .min = 6, .max = 16 },
263 .p = { .min = 4, .max = 128 },
264 .p1 = { .min = 2, .max = 33 },
265 .p2 = { .dot_limit = 165000,
266 .p2_slow = 4, .p2_fast = 4 },
267};
268
1b6f4958 269static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 270 .dot = { .min = 25000, .max = 350000 },
9c333719 271 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 272 .n = { .min = 2, .max = 16 },
0206e353
AJ
273 .m = { .min = 96, .max = 140 },
274 .m1 = { .min = 18, .max = 26 },
275 .m2 = { .min = 6, .max = 16 },
276 .p = { .min = 4, .max = 128 },
277 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
278 .p2 = { .dot_limit = 165000,
279 .p2_slow = 14, .p2_fast = 7 },
e4b36699 280};
273e27ca 281
1b6f4958 282static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
283 .dot = { .min = 20000, .max = 400000 },
284 .vco = { .min = 1400000, .max = 2800000 },
285 .n = { .min = 1, .max = 6 },
286 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
287 .m1 = { .min = 8, .max = 18 },
288 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
289 .p = { .min = 5, .max = 80 },
290 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
291 .p2 = { .dot_limit = 200000,
292 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
293};
294
1b6f4958 295static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
296 .dot = { .min = 20000, .max = 400000 },
297 .vco = { .min = 1400000, .max = 2800000 },
298 .n = { .min = 1, .max = 6 },
299 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
300 .m1 = { .min = 8, .max = 18 },
301 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
302 .p = { .min = 7, .max = 98 },
303 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
304 .p2 = { .dot_limit = 112000,
305 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
306};
307
273e27ca 308
1b6f4958 309static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
310 .dot = { .min = 25000, .max = 270000 },
311 .vco = { .min = 1750000, .max = 3500000},
312 .n = { .min = 1, .max = 4 },
313 .m = { .min = 104, .max = 138 },
314 .m1 = { .min = 17, .max = 23 },
315 .m2 = { .min = 5, .max = 11 },
316 .p = { .min = 10, .max = 30 },
317 .p1 = { .min = 1, .max = 3},
318 .p2 = { .dot_limit = 270000,
319 .p2_slow = 10,
320 .p2_fast = 10
044c7c41 321 },
e4b36699
KP
322};
323
1b6f4958 324static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
325 .dot = { .min = 22000, .max = 400000 },
326 .vco = { .min = 1750000, .max = 3500000},
327 .n = { .min = 1, .max = 4 },
328 .m = { .min = 104, .max = 138 },
329 .m1 = { .min = 16, .max = 23 },
330 .m2 = { .min = 5, .max = 11 },
331 .p = { .min = 5, .max = 80 },
332 .p1 = { .min = 1, .max = 8},
333 .p2 = { .dot_limit = 165000,
334 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
335};
336
1b6f4958 337static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
338 .dot = { .min = 20000, .max = 115000 },
339 .vco = { .min = 1750000, .max = 3500000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 104, .max = 138 },
342 .m1 = { .min = 17, .max = 23 },
343 .m2 = { .min = 5, .max = 11 },
344 .p = { .min = 28, .max = 112 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 0,
347 .p2_slow = 14, .p2_fast = 14
044c7c41 348 },
e4b36699
KP
349};
350
1b6f4958 351static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
352 .dot = { .min = 80000, .max = 224000 },
353 .vco = { .min = 1750000, .max = 3500000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 104, .max = 138 },
356 .m1 = { .min = 17, .max = 23 },
357 .m2 = { .min = 5, .max = 11 },
358 .p = { .min = 14, .max = 42 },
359 .p1 = { .min = 2, .max = 6 },
360 .p2 = { .dot_limit = 0,
361 .p2_slow = 7, .p2_fast = 7
044c7c41 362 },
e4b36699
KP
363};
364
1b6f4958 365static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
366 .dot = { .min = 20000, .max = 400000},
367 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 368 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
369 .n = { .min = 3, .max = 6 },
370 .m = { .min = 2, .max = 256 },
273e27ca 371 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
372 .m1 = { .min = 0, .max = 0 },
373 .m2 = { .min = 0, .max = 254 },
374 .p = { .min = 5, .max = 80 },
375 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
376 .p2 = { .dot_limit = 200000,
377 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
378};
379
1b6f4958 380static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
381 .dot = { .min = 20000, .max = 400000 },
382 .vco = { .min = 1700000, .max = 3500000 },
383 .n = { .min = 3, .max = 6 },
384 .m = { .min = 2, .max = 256 },
385 .m1 = { .min = 0, .max = 0 },
386 .m2 = { .min = 0, .max = 254 },
387 .p = { .min = 7, .max = 112 },
388 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
389 .p2 = { .dot_limit = 112000,
390 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
391};
392
273e27ca
EA
393/* Ironlake / Sandybridge
394 *
395 * We calculate clock using (register_value + 2) for N/M1/M2, so here
396 * the range value for them is (actual_value - 2).
397 */
1b6f4958 398static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
399 .dot = { .min = 25000, .max = 350000 },
400 .vco = { .min = 1760000, .max = 3510000 },
401 .n = { .min = 1, .max = 5 },
402 .m = { .min = 79, .max = 127 },
403 .m1 = { .min = 12, .max = 22 },
404 .m2 = { .min = 5, .max = 9 },
405 .p = { .min = 5, .max = 80 },
406 .p1 = { .min = 1, .max = 8 },
407 .p2 = { .dot_limit = 225000,
408 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
409};
410
1b6f4958 411static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 3 },
415 .m = { .min = 79, .max = 118 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 28, .max = 112 },
419 .p1 = { .min = 2, .max = 8 },
420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
422};
423
1b6f4958 424static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
425 .dot = { .min = 25000, .max = 350000 },
426 .vco = { .min = 1760000, .max = 3510000 },
427 .n = { .min = 1, .max = 3 },
428 .m = { .min = 79, .max = 127 },
429 .m1 = { .min = 12, .max = 22 },
430 .m2 = { .min = 5, .max = 9 },
431 .p = { .min = 14, .max = 56 },
432 .p1 = { .min = 2, .max = 8 },
433 .p2 = { .dot_limit = 225000,
434 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
435};
436
273e27ca 437/* LVDS 100mhz refclk limits. */
1b6f4958 438static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
439 .dot = { .min = 25000, .max = 350000 },
440 .vco = { .min = 1760000, .max = 3510000 },
441 .n = { .min = 1, .max = 2 },
442 .m = { .min = 79, .max = 126 },
443 .m1 = { .min = 12, .max = 22 },
444 .m2 = { .min = 5, .max = 9 },
445 .p = { .min = 28, .max = 112 },
0206e353 446 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
447 .p2 = { .dot_limit = 225000,
448 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
449};
450
1b6f4958 451static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
452 .dot = { .min = 25000, .max = 350000 },
453 .vco = { .min = 1760000, .max = 3510000 },
454 .n = { .min = 1, .max = 3 },
455 .m = { .min = 79, .max = 126 },
456 .m1 = { .min = 12, .max = 22 },
457 .m2 = { .min = 5, .max = 9 },
458 .p = { .min = 14, .max = 42 },
0206e353 459 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
460 .p2 = { .dot_limit = 225000,
461 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
462};
463
1b6f4958 464static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
465 /*
466 * These are the data rate limits (measured in fast clocks)
467 * since those are the strictest limits we have. The fast
468 * clock and actual rate limits are more relaxed, so checking
469 * them would make no difference.
470 */
471 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 472 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 473 .n = { .min = 1, .max = 7 },
a0c4da24
JB
474 .m1 = { .min = 2, .max = 3 },
475 .m2 = { .min = 11, .max = 156 },
b99ab663 476 .p1 = { .min = 2, .max = 3 },
5fdc9c49 477 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
478};
479
1b6f4958 480static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
481 /*
482 * These are the data rate limits (measured in fast clocks)
483 * since those are the strictest limits we have. The fast
484 * clock and actual rate limits are more relaxed, so checking
485 * them would make no difference.
486 */
487 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 488 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
489 .n = { .min = 1, .max = 1 },
490 .m1 = { .min = 2, .max = 2 },
491 .m2 = { .min = 24 << 22, .max = 175 << 22 },
492 .p1 = { .min = 2, .max = 4 },
493 .p2 = { .p2_slow = 1, .p2_fast = 14 },
494};
495
1b6f4958 496static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
497 /* FIXME: find real dot limits */
498 .dot = { .min = 0, .max = INT_MAX },
e6292556 499 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
500 .n = { .min = 1, .max = 1 },
501 .m1 = { .min = 2, .max = 2 },
502 /* FIXME: find real m2 limits */
503 .m2 = { .min = 2 << 22, .max = 255 << 22 },
504 .p1 = { .min = 2, .max = 4 },
505 .p2 = { .p2_slow = 1, .p2_fast = 20 },
506};
507
6deef9b6
VS
508static void
509skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
510{
511 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
512 return;
513
514 if (enable)
515 I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
516 else
517 I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
518}
519
c4a4efa9
VS
520static void
521skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
522{
6deef9b6 523 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
c4a4efa9
VS
524 return;
525
526 if (enable)
527 I915_WRITE(CLKGATE_DIS_PSL(pipe),
528 DUPS1_GATING_DIS | DUPS2_GATING_DIS);
529 else
530 I915_WRITE(CLKGATE_DIS_PSL(pipe),
531 I915_READ(CLKGATE_DIS_PSL(pipe)) &
532 ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
533}
534
cdba954e 535static bool
24f28450 536needs_modeset(const struct drm_crtc_state *state)
cdba954e 537{
fc596660 538 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
539}
540
dccbea3b
ID
541/*
542 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
543 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
544 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
545 * The helpers' return value is the rate of the clock that is fed to the
546 * display engine's pipe which can be the above fast dot clock rate or a
547 * divided-down version of it.
548 */
f2b115e6 549/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 550static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 551{
2177832f
SL
552 clock->m = clock->m2 + 2;
553 clock->p = clock->p1 * clock->p2;
ed5ca77e 554 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 555 return 0;
fb03ac01
VS
556 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
557 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
558
559 return clock->dot;
2177832f
SL
560}
561
7429e9d4
SV
562static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
563{
564 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
565}
566
9e2c8475 567static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 568{
7429e9d4 569 clock->m = i9xx_dpll_compute_m(clock);
79e53945 570 clock->p = clock->p1 * clock->p2;
ed5ca77e 571 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 572 return 0;
fb03ac01
VS
573 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
574 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
575
576 return clock->dot;
79e53945
JB
577}
578
9e2c8475 579static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
580{
581 clock->m = clock->m1 * clock->m2;
582 clock->p = clock->p1 * clock->p2;
583 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 584 return 0;
589eca67
ID
585 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
586 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
587
588 return clock->dot / 5;
589eca67
ID
589}
590
9e2c8475 591int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
592{
593 clock->m = clock->m1 * clock->m2;
594 clock->p = clock->p1 * clock->p2;
595 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 596 return 0;
ef9348c8
CML
597 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
598 clock->n << 22);
599 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
600
601 return clock->dot / 5;
ef9348c8
CML
602}
603
7c04d1d9 604#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
c38c1455
CW
605
606/*
79e53945
JB
607 * Returns whether the given set of divisors are valid for a given refclk with
608 * the given connectors.
609 */
e2d214ae 610static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 611 const struct intel_limit *limit,
9e2c8475 612 const struct dpll *clock)
79e53945 613{
f01b7962
VS
614 if (clock->n < limit->n.min || limit->n.max < clock->n)
615 INTELPllInvalid("n out of range\n");
79e53945 616 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 617 INTELPllInvalid("p1 out of range\n");
79e53945 618 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 619 INTELPllInvalid("m2 out of range\n");
79e53945 620 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 621 INTELPllInvalid("m1 out of range\n");
f01b7962 622
e2d214ae 623 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 624 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
625 if (clock->m1 <= clock->m2)
626 INTELPllInvalid("m1 <= m2\n");
627
e2d214ae 628 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 629 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
630 if (clock->p < limit->p.min || limit->p.max < clock->p)
631 INTELPllInvalid("p out of range\n");
632 if (clock->m < limit->m.min || limit->m.max < clock->m)
633 INTELPllInvalid("m out of range\n");
634 }
635
79e53945 636 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 637 INTELPllInvalid("vco out of range\n");
79e53945
JB
638 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
639 * connector, etc., rather than just a single range.
640 */
641 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 642 INTELPllInvalid("dot out of range\n");
79e53945
JB
643
644 return true;
645}
646
3b1429d9 647static int
1b6f4958 648i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
649 const struct intel_crtc_state *crtc_state,
650 int target)
79e53945 651{
3b1429d9 652 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 653
2d84d2b3 654 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 655 /*
a210b028
SV
656 * For LVDS just rely on its current settings for dual-channel.
657 * We haven't figured out how to reliably set up different
658 * single/dual channel state, if we even can.
79e53945 659 */
1974cad0 660 if (intel_is_dual_link_lvds(dev))
3b1429d9 661 return limit->p2.p2_fast;
79e53945 662 else
3b1429d9 663 return limit->p2.p2_slow;
79e53945
JB
664 } else {
665 if (target < limit->p2.dot_limit)
3b1429d9 666 return limit->p2.p2_slow;
79e53945 667 else
3b1429d9 668 return limit->p2.p2_fast;
79e53945 669 }
3b1429d9
VS
670}
671
70e8aa21
ACO
672/*
673 * Returns a set of divisors for the desired target clock with the given
674 * refclk, or FALSE. The returned values represent the clock equation:
675 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
676 *
677 * Target and reference clocks are specified in kHz.
678 *
679 * If match_clock is provided, then best_clock P divider must match the P
680 * divider from @match_clock used for LVDS downclocking.
681 */
3b1429d9 682static bool
1b6f4958 683i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 684 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
685 int target, int refclk, struct dpll *match_clock,
686 struct dpll *best_clock)
3b1429d9
VS
687{
688 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 689 struct dpll clock;
3b1429d9 690 int err = target;
79e53945 691
0206e353 692 memset(best_clock, 0, sizeof(*best_clock));
79e53945 693
3b1429d9
VS
694 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
695
42158660
ZY
696 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
697 clock.m1++) {
698 for (clock.m2 = limit->m2.min;
699 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 700 if (clock.m2 >= clock.m1)
42158660
ZY
701 break;
702 for (clock.n = limit->n.min;
703 clock.n <= limit->n.max; clock.n++) {
704 for (clock.p1 = limit->p1.min;
705 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
706 int this_err;
707
dccbea3b 708 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
709 if (!intel_PLL_is_valid(to_i915(dev),
710 limit,
ac58c3f0
SV
711 &clock))
712 continue;
713 if (match_clock &&
714 clock.p != match_clock->p)
715 continue;
716
717 this_err = abs(clock.dot - target);
718 if (this_err < err) {
719 *best_clock = clock;
720 err = this_err;
721 }
722 }
723 }
724 }
725 }
726
727 return (err != target);
728}
729
70e8aa21
ACO
730/*
731 * Returns a set of divisors for the desired target clock with the given
732 * refclk, or FALSE. The returned values represent the clock equation:
733 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
734 *
735 * Target and reference clocks are specified in kHz.
736 *
737 * If match_clock is provided, then best_clock P divider must match the P
738 * divider from @match_clock used for LVDS downclocking.
739 */
ac58c3f0 740static bool
1b6f4958 741pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 742 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
743 int target, int refclk, struct dpll *match_clock,
744 struct dpll *best_clock)
79e53945 745{
3b1429d9 746 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 747 struct dpll clock;
79e53945
JB
748 int err = target;
749
0206e353 750 memset(best_clock, 0, sizeof(*best_clock));
79e53945 751
3b1429d9
VS
752 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
753
42158660
ZY
754 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
755 clock.m1++) {
756 for (clock.m2 = limit->m2.min;
757 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
758 for (clock.n = limit->n.min;
759 clock.n <= limit->n.max; clock.n++) {
760 for (clock.p1 = limit->p1.min;
761 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
762 int this_err;
763
dccbea3b 764 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
765 if (!intel_PLL_is_valid(to_i915(dev),
766 limit,
1b894b59 767 &clock))
79e53945 768 continue;
cec2f356
SP
769 if (match_clock &&
770 clock.p != match_clock->p)
771 continue;
79e53945
JB
772
773 this_err = abs(clock.dot - target);
774 if (this_err < err) {
775 *best_clock = clock;
776 err = this_err;
777 }
778 }
779 }
780 }
781 }
782
783 return (err != target);
784}
785
997c030c
ACO
786/*
787 * Returns a set of divisors for the desired target clock with the given
788 * refclk, or FALSE. The returned values represent the clock equation:
789 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
790 *
791 * Target and reference clocks are specified in kHz.
792 *
793 * If match_clock is provided, then best_clock P divider must match the P
794 * divider from @match_clock used for LVDS downclocking.
997c030c 795 */
d4906093 796static bool
1b6f4958 797g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 798 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
799 int target, int refclk, struct dpll *match_clock,
800 struct dpll *best_clock)
d4906093 801{
3b1429d9 802 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 803 struct dpll clock;
d4906093 804 int max_n;
3b1429d9 805 bool found = false;
6ba770dc
AJ
806 /* approximately equals target * 0.00585 */
807 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
808
809 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
810
811 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
812
d4906093 813 max_n = limit->n.max;
f77f13e2 814 /* based on hardware requirement, prefer smaller n to precision */
d4906093 815 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 816 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
817 for (clock.m1 = limit->m1.max;
818 clock.m1 >= limit->m1.min; clock.m1--) {
819 for (clock.m2 = limit->m2.max;
820 clock.m2 >= limit->m2.min; clock.m2--) {
821 for (clock.p1 = limit->p1.max;
822 clock.p1 >= limit->p1.min; clock.p1--) {
823 int this_err;
824
dccbea3b 825 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
826 if (!intel_PLL_is_valid(to_i915(dev),
827 limit,
1b894b59 828 &clock))
d4906093 829 continue;
1b894b59
CW
830
831 this_err = abs(clock.dot - target);
d4906093
ML
832 if (this_err < err_most) {
833 *best_clock = clock;
834 err_most = this_err;
835 max_n = clock.n;
836 found = true;
837 }
838 }
839 }
840 }
841 }
2c07245f
ZW
842 return found;
843}
844
d5dd62bd
ID
845/*
846 * Check if the calculated PLL configuration is more optimal compared to the
847 * best configuration and error found so far. Return the calculated error.
848 */
849static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
850 const struct dpll *calculated_clock,
851 const struct dpll *best_clock,
d5dd62bd
ID
852 unsigned int best_error_ppm,
853 unsigned int *error_ppm)
854{
9ca3ba01
ID
855 /*
856 * For CHV ignore the error and consider only the P value.
857 * Prefer a bigger P value based on HW requirements.
858 */
920a14b2 859 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
860 *error_ppm = 0;
861
862 return calculated_clock->p > best_clock->p;
863 }
864
24be4e46
ID
865 if (WARN_ON_ONCE(!target_freq))
866 return false;
867
d5dd62bd
ID
868 *error_ppm = div_u64(1000000ULL *
869 abs(target_freq - calculated_clock->dot),
870 target_freq);
871 /*
872 * Prefer a better P value over a better (smaller) error if the error
873 * is small. Ensure this preference for future configurations too by
874 * setting the error to 0.
875 */
876 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
877 *error_ppm = 0;
878
879 return true;
880 }
881
882 return *error_ppm + 10 < best_error_ppm;
883}
884
65b3d6a9
ACO
885/*
886 * Returns a set of divisors for the desired target clock with the given
887 * refclk, or FALSE. The returned values represent the clock equation:
888 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
889 */
a0c4da24 890static bool
1b6f4958 891vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 892 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
893 int target, int refclk, struct dpll *match_clock,
894 struct dpll *best_clock)
a0c4da24 895{
a93e255f 896 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 897 struct drm_device *dev = crtc->base.dev;
9e2c8475 898 struct dpll clock;
69e4f900 899 unsigned int bestppm = 1000000;
27e639bf
VS
900 /* min update 19.2 MHz */
901 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 902 bool found = false;
a0c4da24 903
6b4bf1c4
VS
904 target *= 5; /* fast clock */
905
906 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
907
908 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 909 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 910 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 911 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 912 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 913 clock.p = clock.p1 * clock.p2;
a0c4da24 914 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 915 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 916 unsigned int ppm;
69e4f900 917
6b4bf1c4
VS
918 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
919 refclk * clock.m1);
920
dccbea3b 921 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 922
e2d214ae
TU
923 if (!intel_PLL_is_valid(to_i915(dev),
924 limit,
f01b7962 925 &clock))
43b0ac53
VS
926 continue;
927
d5dd62bd
ID
928 if (!vlv_PLL_is_optimal(dev, target,
929 &clock,
930 best_clock,
931 bestppm, &ppm))
932 continue;
6b4bf1c4 933
d5dd62bd
ID
934 *best_clock = clock;
935 bestppm = ppm;
936 found = true;
a0c4da24
JB
937 }
938 }
939 }
940 }
a0c4da24 941
49e497ef 942 return found;
a0c4da24 943}
a4fc5ed6 944
65b3d6a9
ACO
945/*
946 * Returns a set of divisors for the desired target clock with the given
947 * refclk, or FALSE. The returned values represent the clock equation:
948 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
949 */
ef9348c8 950static bool
1b6f4958 951chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 952 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
953 int target, int refclk, struct dpll *match_clock,
954 struct dpll *best_clock)
ef9348c8 955{
a93e255f 956 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 957 struct drm_device *dev = crtc->base.dev;
9ca3ba01 958 unsigned int best_error_ppm;
9e2c8475 959 struct dpll clock;
ef9348c8
CML
960 uint64_t m2;
961 int found = false;
962
963 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 964 best_error_ppm = 1000000;
ef9348c8
CML
965
966 /*
967 * Based on hardware doc, the n always set to 1, and m1 always
968 * set to 2. If requires to support 200Mhz refclk, we need to
969 * revisit this because n may not 1 anymore.
970 */
971 clock.n = 1, clock.m1 = 2;
972 target *= 5; /* fast clock */
973
974 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
975 for (clock.p2 = limit->p2.p2_fast;
976 clock.p2 >= limit->p2.p2_slow;
977 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 978 unsigned int error_ppm;
ef9348c8
CML
979
980 clock.p = clock.p1 * clock.p2;
981
982 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
983 clock.n) << 22, refclk * clock.m1);
984
985 if (m2 > INT_MAX/clock.m1)
986 continue;
987
988 clock.m2 = m2;
989
dccbea3b 990 chv_calc_dpll_params(refclk, &clock);
ef9348c8 991
e2d214ae 992 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
993 continue;
994
9ca3ba01
ID
995 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
996 best_error_ppm, &error_ppm))
997 continue;
998
999 *best_clock = clock;
1000 best_error_ppm = error_ppm;
1001 found = true;
ef9348c8
CML
1002 }
1003 }
1004
1005 return found;
1006}
1007
5ab7b0b7 1008bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1009 struct dpll *best_clock)
5ab7b0b7 1010{
65b3d6a9 1011 int refclk = 100000;
1b6f4958 1012 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1013
65b3d6a9 1014 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1015 target_clock, refclk, NULL, best_clock);
1016}
1017
525b9311 1018bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 1019{
20ddf665
VS
1020 /* Be paranoid as we can arrive here with only partial
1021 * state retrieved from the hardware during setup.
1022 *
241bfc38 1023 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1024 * as Haswell has gained clock readout/fastboot support.
1025 *
cd30fbca 1026 * We can ditch the crtc->primary->state->fb check as soon as we can
20ddf665 1027 * properly reconstruct framebuffers.
c3d1f436
MR
1028 *
1029 * FIXME: The intel_crtc->active here should be switched to
1030 * crtc->state->active once we have proper CRTC states wired up
1031 * for atomic.
20ddf665 1032 */
525b9311
VS
1033 return crtc->active && crtc->base.primary->state->fb &&
1034 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1035}
1036
a5c961d1
PZ
1037enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1038 enum pipe pipe)
1039{
98187836 1040 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 1041
e2af48c6 1042 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1043}
1044
8fedd64d
VS
1045static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1046 enum pipe pipe)
fbf49ea2 1047{
f0f59a00 1048 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1049 u32 line1, line2;
1050 u32 line_mask;
1051
5db94019 1052 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1053 line_mask = DSL_LINEMASK_GEN2;
1054 else
1055 line_mask = DSL_LINEMASK_GEN3;
1056
1057 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1058 msleep(5);
fbf49ea2
VS
1059 line2 = I915_READ(reg) & line_mask;
1060
8fedd64d
VS
1061 return line1 != line2;
1062}
1063
1064static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1065{
1066 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1067 enum pipe pipe = crtc->pipe;
1068
1069 /* Wait for the display line to settle/start moving */
1070 if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1071 DRM_ERROR("pipe %c scanline %s wait timed out\n",
1072 pipe_name(pipe), onoff(state));
1073}
1074
1075static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1076{
1077 wait_for_pipe_scanline_moving(crtc, false);
1078}
1079
1080static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1081{
1082 wait_for_pipe_scanline_moving(crtc, true);
fbf49ea2
VS
1083}
1084
4972f70a
VS
1085static void
1086intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
9d0498a2 1087{
4972f70a 1088 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6315b5d3 1089 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ab7ad7f6 1090
6315b5d3 1091 if (INTEL_GEN(dev_priv) >= 4) {
4972f70a 1092 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
f0f59a00 1093 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1094
1095 /* Wait for the Pipe State to go off */
b8511f53
CW
1096 if (intel_wait_for_register(dev_priv,
1097 reg, I965_PIPECONF_ACTIVE, 0,
1098 100))
284637d9 1099 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1100 } else {
8fedd64d 1101 intel_wait_for_pipe_scanline_stopped(crtc);
ab7ad7f6 1102 }
79e53945
JB
1103}
1104
b24e7179 1105/* Only for pre-ILK configs */
55607e8a
SV
1106void assert_pll(struct drm_i915_private *dev_priv,
1107 enum pipe pipe, bool state)
b24e7179 1108{
b24e7179
JB
1109 u32 val;
1110 bool cur_state;
1111
649636ef 1112 val = I915_READ(DPLL(pipe));
b24e7179 1113 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1114 I915_STATE_WARN(cur_state != state,
b24e7179 1115 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1116 onoff(state), onoff(cur_state));
b24e7179 1117}
b24e7179 1118
23538ef1 1119/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1120void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1121{
1122 u32 val;
1123 bool cur_state;
1124
a580516d 1125 mutex_lock(&dev_priv->sb_lock);
23538ef1 1126 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1127 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1128
1129 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1130 I915_STATE_WARN(cur_state != state,
23538ef1 1131 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1132 onoff(state), onoff(cur_state));
23538ef1 1133}
23538ef1 1134
040484af
JB
1135static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1136 enum pipe pipe, bool state)
1137{
040484af 1138 bool cur_state;
ad80a810
PZ
1139 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1140 pipe);
040484af 1141
2d1fe073 1142 if (HAS_DDI(dev_priv)) {
affa9354 1143 /* DDI does not have a specific FDI_TX register */
649636ef 1144 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1145 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1146 } else {
649636ef 1147 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1148 cur_state = !!(val & FDI_TX_ENABLE);
1149 }
e2c719b7 1150 I915_STATE_WARN(cur_state != state,
040484af 1151 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1152 onoff(state), onoff(cur_state));
040484af
JB
1153}
1154#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1155#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1156
1157static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1158 enum pipe pipe, bool state)
1159{
040484af
JB
1160 u32 val;
1161 bool cur_state;
1162
649636ef 1163 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1164 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1165 I915_STATE_WARN(cur_state != state,
040484af 1166 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1167 onoff(state), onoff(cur_state));
040484af
JB
1168}
1169#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1170#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1171
1172static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1173 enum pipe pipe)
1174{
040484af
JB
1175 u32 val;
1176
1177 /* ILK FDI PLL is always enabled */
7e22dbbb 1178 if (IS_GEN5(dev_priv))
040484af
JB
1179 return;
1180
bf507ef7 1181 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1182 if (HAS_DDI(dev_priv))
bf507ef7
ED
1183 return;
1184
649636ef 1185 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1186 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1187}
1188
55607e8a
SV
1189void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
040484af 1191{
040484af 1192 u32 val;
55607e8a 1193 bool cur_state;
040484af 1194
649636ef 1195 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1196 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1197 I915_STATE_WARN(cur_state != state,
55607e8a 1198 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1199 onoff(state), onoff(cur_state));
040484af
JB
1200}
1201
4f8036a2 1202void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1203{
f0f59a00 1204 i915_reg_t pp_reg;
ea0760cf 1205 u32 val;
10ed55e4 1206 enum pipe panel_pipe = INVALID_PIPE;
0de3b485 1207 bool locked = true;
ea0760cf 1208
4f8036a2 1209 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1210 return;
1211
4f8036a2 1212 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1213 u32 port_sel;
1214
44cb734c
ID
1215 pp_reg = PP_CONTROL(0);
1216 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba 1217
4c23dea4
VS
1218 switch (port_sel) {
1219 case PANEL_PORT_SELECT_LVDS:
a44628b9 1220 intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
4c23dea4
VS
1221 break;
1222 case PANEL_PORT_SELECT_DPA:
1223 intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1224 break;
1225 case PANEL_PORT_SELECT_DPC:
1226 intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1227 break;
1228 case PANEL_PORT_SELECT_DPD:
1229 intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1230 break;
1231 default:
1232 MISSING_CASE(port_sel);
1233 break;
1234 }
4f8036a2 1235 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1236 /* presumably write lock depends on pipe, not port select */
44cb734c 1237 pp_reg = PP_CONTROL(pipe);
bedd4dba 1238 panel_pipe = pipe;
ea0760cf 1239 } else {
f0d2b758
VS
1240 u32 port_sel;
1241
44cb734c 1242 pp_reg = PP_CONTROL(0);
f0d2b758
VS
1243 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1244
1245 WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
a44628b9 1246 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
ea0760cf
JB
1247 }
1248
1249 val = I915_READ(pp_reg);
1250 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1251 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1252 locked = false;
1253
e2c719b7 1254 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1255 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1256 pipe_name(pipe));
ea0760cf
JB
1257}
1258
b840d907
JB
1259void assert_pipe(struct drm_i915_private *dev_priv,
1260 enum pipe pipe, bool state)
b24e7179 1261{
63d7bbe9 1262 bool cur_state;
702e7a56
PZ
1263 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1264 pipe);
4feed0eb 1265 enum intel_display_power_domain power_domain;
b24e7179 1266
e56134bc
VS
1267 /* we keep both pipes enabled on 830 */
1268 if (IS_I830(dev_priv))
8e636784
SV
1269 state = true;
1270
4feed0eb
ID
1271 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1272 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1273 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1274 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1275
1276 intel_display_power_put(dev_priv, power_domain);
1277 } else {
1278 cur_state = false;
69310161
PZ
1279 }
1280
e2c719b7 1281 I915_STATE_WARN(cur_state != state,
63d7bbe9 1282 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1283 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1284}
1285
51f5a096 1286static void assert_plane(struct intel_plane *plane, bool state)
b24e7179 1287{
eade6c89
VS
1288 enum pipe pipe;
1289 bool cur_state;
1290
1291 cur_state = plane->get_hw_state(plane, &pipe);
b24e7179 1292
e2c719b7 1293 I915_STATE_WARN(cur_state != state,
51f5a096
VS
1294 "%s assertion failure (expected %s, current %s)\n",
1295 plane->base.name, onoff(state), onoff(cur_state));
b24e7179
JB
1296}
1297
51f5a096
VS
1298#define assert_plane_enabled(p) assert_plane(p, true)
1299#define assert_plane_disabled(p) assert_plane(p, false)
931872fc 1300
51f5a096 1301static void assert_planes_disabled(struct intel_crtc *crtc)
b24e7179 1302{
51f5a096
VS
1303 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1304 struct intel_plane *plane;
19332d7a 1305
51f5a096
VS
1306 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1307 assert_plane_disabled(plane);
19332d7a
JB
1308}
1309
08c71e5e
VS
1310static void assert_vblank_disabled(struct drm_crtc *crtc)
1311{
e2c719b7 1312 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1313 drm_crtc_vblank_put(crtc);
1314}
1315
7abd4b35
ACO
1316void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1317 enum pipe pipe)
92f2584a 1318{
92f2584a
JB
1319 u32 val;
1320 bool enabled;
1321
649636ef 1322 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1323 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1324 I915_STATE_WARN(enabled,
9db4a9c7
JB
1325 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1326 pipe_name(pipe));
92f2584a
JB
1327}
1328
59b74c49
VS
1329static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1330 enum pipe pipe, enum port port,
1331 i915_reg_t dp_reg)
f0575e92 1332{
59b74c49
VS
1333 enum pipe port_pipe;
1334 bool state;
f0575e92 1335
59b74c49 1336 state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
f0575e92 1337
59b74c49
VS
1338 I915_STATE_WARN(state && port_pipe == pipe,
1339 "PCH DP %c enabled on transcoder %c, should be disabled\n",
1340 port_name(port), pipe_name(pipe));
1341
1342 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1343 "IBX PCH DP %c still using transcoder B\n",
1344 port_name(port));
291906f1
JB
1345}
1346
1347static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
76203467
VS
1348 enum pipe pipe, enum port port,
1349 i915_reg_t hdmi_reg)
291906f1 1350{
76203467
VS
1351 enum pipe port_pipe;
1352 bool state;
1353
1354 state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1355
1356 I915_STATE_WARN(state && port_pipe == pipe,
1357 "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1358 port_name(port), pipe_name(pipe));
de9a35ab 1359
76203467
VS
1360 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1361 "IBX PCH HDMI %c still using transcoder B\n",
1362 port_name(port));
291906f1
JB
1363}
1364
1365static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
6102a8ee 1368 enum pipe port_pipe;
291906f1 1369
59b74c49
VS
1370 assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1371 assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1372 assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
291906f1 1373
6102a8ee
VS
1374 I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1375 port_pipe == pipe,
1376 "PCH VGA enabled on transcoder %c, should be disabled\n",
1377 pipe_name(pipe));
291906f1 1378
a44628b9
VS
1379 I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1380 port_pipe == pipe,
1381 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1382 pipe_name(pipe));
291906f1 1383
76203467
VS
1384 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1385 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1386 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
291906f1
JB
1387}
1388
cd2d34d9
VS
1389static void _vlv_enable_pll(struct intel_crtc *crtc,
1390 const struct intel_crtc_state *pipe_config)
1391{
1392 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1393 enum pipe pipe = crtc->pipe;
1394
1395 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1396 POSTING_READ(DPLL(pipe));
1397 udelay(150);
1398
2c30b43b
CW
1399 if (intel_wait_for_register(dev_priv,
1400 DPLL(pipe),
1401 DPLL_LOCK_VLV,
1402 DPLL_LOCK_VLV,
1403 1))
cd2d34d9
VS
1404 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1405}
1406
d288f65f 1407static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1408 const struct intel_crtc_state *pipe_config)
87442f73 1409{
cd2d34d9 1410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1411 enum pipe pipe = crtc->pipe;
87442f73 1412
8bd3f301 1413 assert_pipe_disabled(dev_priv, pipe);
87442f73 1414
87442f73 1415 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1416 assert_panel_unlocked(dev_priv, pipe);
87442f73 1417
cd2d34d9
VS
1418 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1419 _vlv_enable_pll(crtc, pipe_config);
426115cf 1420
8bd3f301
VS
1421 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1422 POSTING_READ(DPLL_MD(pipe));
87442f73
SV
1423}
1424
cd2d34d9
VS
1425
1426static void _chv_enable_pll(struct intel_crtc *crtc,
1427 const struct intel_crtc_state *pipe_config)
9d556c99 1428{
cd2d34d9 1429 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1430 enum pipe pipe = crtc->pipe;
9d556c99 1431 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1432 u32 tmp;
1433
a580516d 1434 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1435
1436 /* Enable back the 10bit clock to display controller */
1437 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1438 tmp |= DPIO_DCLKP_EN;
1439 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1440
54433e91
VS
1441 mutex_unlock(&dev_priv->sb_lock);
1442
9d556c99
CML
1443 /*
1444 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1445 */
1446 udelay(1);
1447
1448 /* Enable PLL */
d288f65f 1449 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1450
1451 /* Check PLL is locked */
6b18826a
CW
1452 if (intel_wait_for_register(dev_priv,
1453 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1454 1))
9d556c99 1455 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1456}
1457
1458static void chv_enable_pll(struct intel_crtc *crtc,
1459 const struct intel_crtc_state *pipe_config)
1460{
1461 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1462 enum pipe pipe = crtc->pipe;
1463
1464 assert_pipe_disabled(dev_priv, pipe);
1465
1466 /* PLL is protected by panel, make sure we can write it */
1467 assert_panel_unlocked(dev_priv, pipe);
1468
1469 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1470 _chv_enable_pll(crtc, pipe_config);
9d556c99 1471
c231775c
VS
1472 if (pipe != PIPE_A) {
1473 /*
1474 * WaPixelRepeatModeFixForC0:chv
1475 *
1476 * DPLLCMD is AWOL. Use chicken bits to propagate
1477 * the value from DPLLBMD to either pipe B or C.
1478 */
dfa311f0 1479 I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
c231775c
VS
1480 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1481 I915_WRITE(CBR4_VLV, 0);
1482 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1483
1484 /*
1485 * DPLLB VGA mode also seems to cause problems.
1486 * We should always have it disabled.
1487 */
1488 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1489 } else {
1490 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1491 POSTING_READ(DPLL_MD(pipe));
1492 }
9d556c99
CML
1493}
1494
6315b5d3 1495static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1496{
1497 struct intel_crtc *crtc;
1498 int count = 0;
1499
6315b5d3 1500 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1501 count += crtc->base.state->active &&
2d84d2b3
VS
1502 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1503 }
1c4e0274
VS
1504
1505 return count;
1506}
1507
939994da
VS
1508static void i9xx_enable_pll(struct intel_crtc *crtc,
1509 const struct intel_crtc_state *crtc_state)
63d7bbe9 1510{
6315b5d3 1511 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1512 i915_reg_t reg = DPLL(crtc->pipe);
939994da 1513 u32 dpll = crtc_state->dpll_hw_state.dpll;
bb408dd2 1514 int i;
63d7bbe9 1515
66e3d5c0 1516 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1517
63d7bbe9 1518 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1519 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1520 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1521
1c4e0274 1522 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1523 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1524 /*
1525 * It appears to be important that we don't enable this
1526 * for the current pipe before otherwise configuring the
1527 * PLL. No idea how this should be handled if multiple
1528 * DVO outputs are enabled simultaneosly.
1529 */
1530 dpll |= DPLL_DVO_2X_MODE;
1531 I915_WRITE(DPLL(!crtc->pipe),
1532 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1533 }
66e3d5c0 1534
c2b63374
VS
1535 /*
1536 * Apparently we need to have VGA mode enabled prior to changing
1537 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1538 * dividers, even though the register value does change.
1539 */
1540 I915_WRITE(reg, 0);
1541
8e7a65aa
VS
1542 I915_WRITE(reg, dpll);
1543
66e3d5c0
SV
1544 /* Wait for the clocks to stabilize. */
1545 POSTING_READ(reg);
1546 udelay(150);
1547
6315b5d3 1548 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1549 I915_WRITE(DPLL_MD(crtc->pipe),
939994da 1550 crtc_state->dpll_hw_state.dpll_md);
66e3d5c0
SV
1551 } else {
1552 /* The pixel multiplier can only be updated once the
1553 * DPLL is enabled and the clocks are stable.
1554 *
1555 * So write it again.
1556 */
1557 I915_WRITE(reg, dpll);
1558 }
63d7bbe9
JB
1559
1560 /* We do this three times for luck */
bb408dd2
VS
1561 for (i = 0; i < 3; i++) {
1562 I915_WRITE(reg, dpll);
1563 POSTING_READ(reg);
1564 udelay(150); /* wait for warmup */
1565 }
63d7bbe9
JB
1566}
1567
1c4e0274 1568static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1569{
6315b5d3 1570 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1571 enum pipe pipe = crtc->pipe;
1572
1573 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1574 if (IS_I830(dev_priv) &&
2d84d2b3 1575 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1576 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1577 I915_WRITE(DPLL(PIPE_B),
1578 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1579 I915_WRITE(DPLL(PIPE_A),
1580 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1581 }
1582
b6b5d049 1583 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1584 if (IS_I830(dev_priv))
63d7bbe9
JB
1585 return;
1586
1587 /* Make sure the pipe isn't still relying on us */
1588 assert_pipe_disabled(dev_priv, pipe);
1589
b8afb911 1590 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1591 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1592}
1593
f6071166
JB
1594static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1595{
b8afb911 1596 u32 val;
f6071166
JB
1597
1598 /* Make sure the pipe isn't still relying on us */
1599 assert_pipe_disabled(dev_priv, pipe);
1600
03ed5cbf
VS
1601 val = DPLL_INTEGRATED_REF_CLK_VLV |
1602 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1603 if (pipe != PIPE_A)
1604 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1605
f6071166
JB
1606 I915_WRITE(DPLL(pipe), val);
1607 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1608}
1609
1610static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1611{
d752048d 1612 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1613 u32 val;
1614
a11b0703
VS
1615 /* Make sure the pipe isn't still relying on us */
1616 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1617
60bfe44f
VS
1618 val = DPLL_SSC_REF_CLK_CHV |
1619 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1620 if (pipe != PIPE_A)
1621 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1622
a11b0703
VS
1623 I915_WRITE(DPLL(pipe), val);
1624 POSTING_READ(DPLL(pipe));
d752048d 1625
a580516d 1626 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1627
1628 /* Disable 10bit clock to display controller */
1629 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1630 val &= ~DPIO_DCLKP_EN;
1631 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1632
a580516d 1633 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1634}
1635
e4607fcf 1636void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1637 struct intel_digital_port *dport,
1638 unsigned int expected_mask)
89b667f8
JB
1639{
1640 u32 port_mask;
f0f59a00 1641 i915_reg_t dpll_reg;
89b667f8 1642
8f4f2797 1643 switch (dport->base.port) {
e4607fcf 1644 case PORT_B:
89b667f8 1645 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1646 dpll_reg = DPLL(0);
e4607fcf
CML
1647 break;
1648 case PORT_C:
89b667f8 1649 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1650 dpll_reg = DPLL(0);
9b6de0a1 1651 expected_mask <<= 4;
00fc31b7
CML
1652 break;
1653 case PORT_D:
1654 port_mask = DPLL_PORTD_READY_MASK;
1655 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1656 break;
1657 default:
1658 BUG();
1659 }
89b667f8 1660
370004d3
CW
1661 if (intel_wait_for_register(dev_priv,
1662 dpll_reg, port_mask, expected_mask,
1663 1000))
9b6de0a1 1664 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
8f4f2797
VS
1665 port_name(dport->base.port),
1666 I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1667}
1668
b8a4f404
PZ
1669static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1670 enum pipe pipe)
040484af 1671{
98187836
VS
1672 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1673 pipe);
f0f59a00
VS
1674 i915_reg_t reg;
1675 uint32_t val, pipeconf_val;
040484af 1676
040484af 1677 /* Make sure PCH DPLL is enabled */
8106ddbd 1678 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1679
1680 /* FDI must be feeding us bits for PCH ports */
1681 assert_fdi_tx_enabled(dev_priv, pipe);
1682 assert_fdi_rx_enabled(dev_priv, pipe);
1683
6e266956 1684 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1685 /* Workaround: Set the timing override bit before enabling the
1686 * pch transcoder. */
1687 reg = TRANS_CHICKEN2(pipe);
1688 val = I915_READ(reg);
1689 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1690 I915_WRITE(reg, val);
59c859d6 1691 }
23670b32 1692
ab9412ba 1693 reg = PCH_TRANSCONF(pipe);
040484af 1694 val = I915_READ(reg);
5f7f726d 1695 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1696
2d1fe073 1697 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1698 /*
c5de7c6f
VS
1699 * Make the BPC in transcoder be consistent with
1700 * that in pipeconf reg. For HDMI we must use 8bpc
1701 * here for both 8bpc and 12bpc.
e9bcff5c 1702 */
dfd07d72 1703 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1704 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1705 val |= PIPECONF_8BPC;
1706 else
1707 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1708 }
5f7f726d
PZ
1709
1710 val &= ~TRANS_INTERLACE_MASK;
1711 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1712 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1713 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1714 val |= TRANS_LEGACY_INTERLACED_ILK;
1715 else
1716 val |= TRANS_INTERLACED;
5f7f726d
PZ
1717 else
1718 val |= TRANS_PROGRESSIVE;
1719
040484af 1720 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1721 if (intel_wait_for_register(dev_priv,
1722 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1723 100))
4bb6f1f3 1724 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1725}
1726
8fb033d7 1727static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1728 enum transcoder cpu_transcoder)
040484af 1729{
8fb033d7 1730 u32 val, pipeconf_val;
8fb033d7 1731
8fb033d7 1732 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1733 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
a2196033 1734 assert_fdi_rx_enabled(dev_priv, PIPE_A);
8fb033d7 1735
223a6fdf 1736 /* Workaround: set timing override bit. */
36c0d0cf 1737 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1738 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1739 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1740
25f3ef11 1741 val = TRANS_ENABLE;
937bb610 1742 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1743
9a76b1c6
PZ
1744 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1745 PIPECONF_INTERLACED_ILK)
a35f2679 1746 val |= TRANS_INTERLACED;
8fb033d7
PZ
1747 else
1748 val |= TRANS_PROGRESSIVE;
1749
ab9412ba 1750 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1751 if (intel_wait_for_register(dev_priv,
1752 LPT_TRANSCONF,
1753 TRANS_STATE_ENABLE,
1754 TRANS_STATE_ENABLE,
1755 100))
937bb610 1756 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1757}
1758
b8a4f404
PZ
1759static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1760 enum pipe pipe)
040484af 1761{
f0f59a00
VS
1762 i915_reg_t reg;
1763 uint32_t val;
040484af
JB
1764
1765 /* FDI relies on the transcoder */
1766 assert_fdi_tx_disabled(dev_priv, pipe);
1767 assert_fdi_rx_disabled(dev_priv, pipe);
1768
291906f1
JB
1769 /* Ports must be off as well */
1770 assert_pch_ports_disabled(dev_priv, pipe);
1771
ab9412ba 1772 reg = PCH_TRANSCONF(pipe);
040484af
JB
1773 val = I915_READ(reg);
1774 val &= ~TRANS_ENABLE;
1775 I915_WRITE(reg, val);
1776 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1777 if (intel_wait_for_register(dev_priv,
1778 reg, TRANS_STATE_ENABLE, 0,
1779 50))
4bb6f1f3 1780 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1781
6e266956 1782 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1783 /* Workaround: Clear the timing override chicken bit again. */
1784 reg = TRANS_CHICKEN2(pipe);
1785 val = I915_READ(reg);
1786 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1787 I915_WRITE(reg, val);
1788 }
040484af
JB
1789}
1790
b7076546 1791void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1792{
8fb033d7
PZ
1793 u32 val;
1794
ab9412ba 1795 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1796 val &= ~TRANS_ENABLE;
ab9412ba 1797 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1798 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1799 if (intel_wait_for_register(dev_priv,
1800 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1801 50))
8a52fd9f 1802 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1803
1804 /* Workaround: clear timing override bit. */
36c0d0cf 1805 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1806 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1807 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1808}
1809
a2196033 1810enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
65f2130c
VS
1811{
1812 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1813
65f2130c 1814 if (HAS_PCH_LPT(dev_priv))
a2196033 1815 return PIPE_A;
65f2130c 1816 else
a2196033 1817 return crtc->pipe;
65f2130c
VS
1818}
1819
4972f70a 1820static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
b24e7179 1821{
4972f70a
VS
1822 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1823 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1824 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
0372264a 1825 enum pipe pipe = crtc->pipe;
f0f59a00 1826 i915_reg_t reg;
b24e7179
JB
1827 u32 val;
1828
9e2ee2dd
VS
1829 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1830
51f5a096 1831 assert_planes_disabled(crtc);
58c6eaa2 1832
b24e7179
JB
1833 /*
1834 * A pipe without a PLL won't actually be able to drive bits from
1835 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1836 * need the check.
1837 */
09fa8bb9 1838 if (HAS_GMCH_DISPLAY(dev_priv)) {
4972f70a 1839 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
23538ef1
JN
1840 assert_dsi_pll_enabled(dev_priv);
1841 else
1842 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1843 } else {
4972f70a 1844 if (new_crtc_state->has_pch_encoder) {
040484af 1845 /* if driving the PCH, we need FDI enabled */
65f2130c 1846 assert_fdi_rx_pll_enabled(dev_priv,
a2196033 1847 intel_crtc_pch_transcoder(crtc));
1a240d4d
SV
1848 assert_fdi_tx_pll_enabled(dev_priv,
1849 (enum pipe) cpu_transcoder);
040484af
JB
1850 }
1851 /* FIXME: assert CPU port conditions for SNB+ */
1852 }
b24e7179 1853
702e7a56 1854 reg = PIPECONF(cpu_transcoder);
b24e7179 1855 val = I915_READ(reg);
7ad25d48 1856 if (val & PIPECONF_ENABLE) {
e56134bc
VS
1857 /* we keep both pipes enabled on 830 */
1858 WARN_ON(!IS_I830(dev_priv));
00d70b15 1859 return;
7ad25d48 1860 }
00d70b15
CW
1861
1862 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1863 POSTING_READ(reg);
b7792d8b
VS
1864
1865 /*
8fedd64d
VS
1866 * Until the pipe starts PIPEDSL reads will return a stale value,
1867 * which causes an apparent vblank timestamp jump when PIPEDSL
1868 * resets to its proper value. That also messes up the frame count
1869 * when it's derived from the timestamps. So let's wait for the
1870 * pipe to start properly before we call drm_crtc_vblank_on()
b7792d8b 1871 */
4972f70a 1872 if (dev_priv->drm.max_vblank_count == 0)
8fedd64d 1873 intel_wait_for_pipe_scanline_moving(crtc);
b24e7179
JB
1874}
1875
4972f70a 1876static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
b24e7179 1877{
4972f70a 1878 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
fac5e23e 1879 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4972f70a 1880 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
575f7ab7 1881 enum pipe pipe = crtc->pipe;
f0f59a00 1882 i915_reg_t reg;
b24e7179
JB
1883 u32 val;
1884
9e2ee2dd
VS
1885 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1886
b24e7179
JB
1887 /*
1888 * Make sure planes won't keep trying to pump pixels to us,
1889 * or we might hang the display.
1890 */
51f5a096 1891 assert_planes_disabled(crtc);
b24e7179 1892
702e7a56 1893 reg = PIPECONF(cpu_transcoder);
b24e7179 1894 val = I915_READ(reg);
00d70b15
CW
1895 if ((val & PIPECONF_ENABLE) == 0)
1896 return;
1897
67adc644
VS
1898 /*
1899 * Double wide has implications for planes
1900 * so best keep it disabled when not needed.
1901 */
4972f70a 1902 if (old_crtc_state->double_wide)
67adc644
VS
1903 val &= ~PIPECONF_DOUBLE_WIDE;
1904
1905 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1906 if (!IS_I830(dev_priv))
67adc644
VS
1907 val &= ~PIPECONF_ENABLE;
1908
1909 I915_WRITE(reg, val);
1910 if ((val & PIPECONF_ENABLE) == 0)
4972f70a 1911 intel_wait_for_pipe_off(old_crtc_state);
b24e7179
JB
1912}
1913
832be82f
VS
1914static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1915{
1916 return IS_GEN2(dev_priv) ? 2048 : 4096;
1917}
1918
d88c4afd 1919static unsigned int
5d2a1950 1920intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
7b49f948 1921{
d88c4afd 1922 struct drm_i915_private *dev_priv = to_i915(fb->dev);
5d2a1950 1923 unsigned int cpp = fb->format->cpp[color_plane];
d88c4afd
VS
1924
1925 switch (fb->modifier) {
2f075565 1926 case DRM_FORMAT_MOD_LINEAR:
7b49f948
VS
1927 return cpp;
1928 case I915_FORMAT_MOD_X_TILED:
1929 if (IS_GEN2(dev_priv))
1930 return 128;
1931 else
1932 return 512;
2e2adb05 1933 case I915_FORMAT_MOD_Y_TILED_CCS:
5d2a1950 1934 if (color_plane == 1)
2e2adb05
VS
1935 return 128;
1936 /* fall through */
7b49f948
VS
1937 case I915_FORMAT_MOD_Y_TILED:
1938 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
1939 return 128;
1940 else
1941 return 512;
2e2adb05 1942 case I915_FORMAT_MOD_Yf_TILED_CCS:
5d2a1950 1943 if (color_plane == 1)
2e2adb05
VS
1944 return 128;
1945 /* fall through */
7b49f948
VS
1946 case I915_FORMAT_MOD_Yf_TILED:
1947 switch (cpp) {
1948 case 1:
1949 return 64;
1950 case 2:
1951 case 4:
1952 return 128;
1953 case 8:
1954 case 16:
1955 return 256;
1956 default:
1957 MISSING_CASE(cpp);
1958 return cpp;
1959 }
1960 break;
1961 default:
d88c4afd 1962 MISSING_CASE(fb->modifier);
7b49f948
VS
1963 return cpp;
1964 }
1965}
1966
d88c4afd 1967static unsigned int
5d2a1950 1968intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
a57ce0b2 1969{
2f075565 1970 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
832be82f
VS
1971 return 1;
1972 else
d88c4afd 1973 return intel_tile_size(to_i915(fb->dev)) /
5d2a1950 1974 intel_tile_width_bytes(fb, color_plane);
6761dd31
TU
1975}
1976
8d0deca8 1977/* Return the tile dimensions in pixel units */
5d2a1950 1978static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
8d0deca8 1979 unsigned int *tile_width,
d88c4afd 1980 unsigned int *tile_height)
8d0deca8 1981{
5d2a1950
VS
1982 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
1983 unsigned int cpp = fb->format->cpp[color_plane];
8d0deca8
VS
1984
1985 *tile_width = tile_width_bytes / cpp;
d88c4afd 1986 *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
8d0deca8
VS
1987}
1988
6761dd31 1989unsigned int
d88c4afd 1990intel_fb_align_height(const struct drm_framebuffer *fb,
5d2a1950 1991 int color_plane, unsigned int height)
6761dd31 1992{
5d2a1950 1993 unsigned int tile_height = intel_tile_height(fb, color_plane);
832be82f
VS
1994
1995 return ALIGN(height, tile_height);
a57ce0b2
JB
1996}
1997
1663b9d6
VS
1998unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1999{
2000 unsigned int size = 0;
2001 int i;
2002
2003 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2004 size += rot_info->plane[i].width * rot_info->plane[i].height;
2005
2006 return size;
2007}
2008
75c82a53 2009static void
3465c580
VS
2010intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2011 const struct drm_framebuffer *fb,
2012 unsigned int rotation)
f64b98cd 2013{
7b92c047 2014 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2015 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2016 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2017 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2018 }
2019}
50470bb0 2020
fabac484
VS
2021static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2022{
2023 if (IS_I830(dev_priv))
2024 return 16 * 1024;
2025 else if (IS_I85X(dev_priv))
2026 return 256;
d9e1551e
VS
2027 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2028 return 32;
fabac484
VS
2029 else
2030 return 4 * 1024;
2031}
2032
603525d7 2033static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6 2034{
c56b89f1 2035 if (INTEL_GEN(dev_priv) >= 9)
4e9a86b6 2036 return 256 * 1024;
c0f86832 2037 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2038 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6 2039 return 128 * 1024;
c56b89f1 2040 else if (INTEL_GEN(dev_priv) >= 4)
4e9a86b6
VS
2041 return 4 * 1024;
2042 else
44c5905e 2043 return 0;
4e9a86b6
VS
2044}
2045
d88c4afd 2046static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
5d2a1950 2047 int color_plane)
603525d7 2048{
d88c4afd
VS
2049 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2050
b90c1ee1 2051 /* AUX_DIST needs only 4K alignment */
5d2a1950 2052 if (color_plane == 1)
b90c1ee1
VS
2053 return 4096;
2054
d88c4afd 2055 switch (fb->modifier) {
2f075565 2056 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2057 return intel_linear_alignment(dev_priv);
2058 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2059 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2060 return 256 * 1024;
2061 return 0;
2e2adb05
VS
2062 case I915_FORMAT_MOD_Y_TILED_CCS:
2063 case I915_FORMAT_MOD_Yf_TILED_CCS:
603525d7
VS
2064 case I915_FORMAT_MOD_Y_TILED:
2065 case I915_FORMAT_MOD_Yf_TILED:
2066 return 1 * 1024 * 1024;
2067 default:
d88c4afd 2068 MISSING_CASE(fb->modifier);
603525d7
VS
2069 return 0;
2070 }
2071}
2072
f7a02ad7
VS
2073static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2074{
2075 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2076 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2077
32febd91 2078 return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
f7a02ad7
VS
2079}
2080
058d88c4 2081struct i915_vma *
5935485f 2082intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
f5929c53 2083 const struct i915_ggtt_view *view,
f7a02ad7 2084 bool uses_fence,
5935485f 2085 unsigned long *out_flags)
6b95a207 2086{
850c4cdc 2087 struct drm_device *dev = fb->dev;
fac5e23e 2088 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2089 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
058d88c4 2090 struct i915_vma *vma;
5935485f 2091 unsigned int pinctl;
6b95a207 2092 u32 alignment;
6b95a207 2093
ebcdd39e
MR
2094 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2095
d88c4afd 2096 alignment = intel_surf_alignment(fb, 0);
6b95a207 2097
693db184
CW
2098 /* Note that the w/a also requires 64 PTE of padding following the
2099 * bo. We currently fill all unused PTE with the shadow page and so
2100 * we should always have valid PTE following the scanout preventing
2101 * the VT-d warning.
2102 */
48f112fe 2103 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2104 alignment = 256 * 1024;
2105
d6dd6843
PZ
2106 /*
2107 * Global gtt pte registers are special registers which actually forward
2108 * writes to a chunk of system memory. Which means that there is no risk
2109 * that the register values disappear as soon as we call
2110 * intel_runtime_pm_put(), so it is correct to wrap only the
2111 * pin/unpin/fence and not more.
2112 */
2113 intel_runtime_pm_get(dev_priv);
2114
9db529aa
SV
2115 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2116
5935485f
CW
2117 pinctl = 0;
2118
2119 /* Valleyview is definitely limited to scanning out the first
2120 * 512MiB. Lets presume this behaviour was inherited from the
2121 * g4x display engine and that all earlier gen are similarly
2122 * limited. Testing suggests that it is a little more
2123 * complicated than this. For example, Cherryview appears quite
2124 * happy to scanout from anywhere within its global aperture.
2125 */
2126 if (HAS_GMCH_DISPLAY(dev_priv))
2127 pinctl |= PIN_MAPPABLE;
2128
2129 vma = i915_gem_object_pin_to_display_plane(obj,
f5929c53 2130 alignment, view, pinctl);
49ef5294
CW
2131 if (IS_ERR(vma))
2132 goto err;
6b95a207 2133
f7a02ad7 2134 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
85798ac9
VS
2135 int ret;
2136
49ef5294
CW
2137 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2138 * fence, whereas 965+ only requires a fence if using
2139 * framebuffer compression. For simplicity, we always, when
2140 * possible, install a fence as the cost is not that onerous.
2141 *
2142 * If we fail to fence the tiled scanout, then either the
2143 * modeset will reject the change (which is highly unlikely as
2144 * the affected systems, all but one, do not have unmappable
2145 * space) or we will not be able to enable full powersaving
2146 * techniques (also likely not to apply due to various limits
2147 * FBC and the like impose on the size of the buffer, which
2148 * presumably we violated anyway with this unmappable buffer).
2149 * Anyway, it is presumably better to stumble onwards with
2150 * something and try to run the system in a "less than optimal"
2151 * mode that matches the user configuration.
2152 */
85798ac9
VS
2153 ret = i915_vma_pin_fence(vma);
2154 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
7509702b 2155 i915_gem_object_unpin_from_display_plane(vma);
85798ac9
VS
2156 vma = ERR_PTR(ret);
2157 goto err;
2158 }
2159
2160 if (ret == 0 && vma->fence)
5935485f 2161 *out_flags |= PLANE_HAS_FENCE;
9807216f 2162 }
6b95a207 2163
be1e3415 2164 i915_vma_get(vma);
49ef5294 2165err:
9db529aa
SV
2166 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2167
d6dd6843 2168 intel_runtime_pm_put(dev_priv);
058d88c4 2169 return vma;
6b95a207
KH
2170}
2171
5935485f 2172void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
1690e1eb 2173{
be1e3415 2174 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
f64b98cd 2175
5935485f
CW
2176 if (flags & PLANE_HAS_FENCE)
2177 i915_vma_unpin_fence(vma);
058d88c4 2178 i915_gem_object_unpin_from_display_plane(vma);
be1e3415 2179 i915_vma_put(vma);
1690e1eb
CW
2180}
2181
5d2a1950 2182static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
ef78ec94
VS
2183 unsigned int rotation)
2184{
bd2ef25d 2185 if (drm_rotation_90_or_270(rotation))
5d2a1950 2186 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
ef78ec94 2187 else
5d2a1950 2188 return fb->pitches[color_plane];
ef78ec94
VS
2189}
2190
6687c906
VS
2191/*
2192 * Convert the x/y offsets into a linear offset.
2193 * Only valid with 0/180 degree rotation, which is fine since linear
2194 * offset is only used with linear buffers on pre-hsw and tiled buffers
2195 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2196 */
2197u32 intel_fb_xy_to_linear(int x, int y,
2949056c 2198 const struct intel_plane_state *state,
5d2a1950 2199 int color_plane)
6687c906 2200{
2949056c 2201 const struct drm_framebuffer *fb = state->base.fb;
5d2a1950
VS
2202 unsigned int cpp = fb->format->cpp[color_plane];
2203 unsigned int pitch = state->color_plane[color_plane].stride;
6687c906
VS
2204
2205 return y * pitch + x * cpp;
2206}
2207
2208/*
2209 * Add the x/y offsets derived from fb->offsets[] to the user
2210 * specified plane src x/y offsets. The resulting x/y offsets
2211 * specify the start of scanout from the beginning of the gtt mapping.
2212 */
2213void intel_add_fb_offsets(int *x, int *y,
2949056c 2214 const struct intel_plane_state *state,
5d2a1950 2215 int color_plane)
6687c906
VS
2216
2217{
2949056c
VS
2218 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2219 unsigned int rotation = state->base.rotation;
6687c906 2220
bd2ef25d 2221 if (drm_rotation_90_or_270(rotation)) {
5d2a1950
VS
2222 *x += intel_fb->rotated[color_plane].x;
2223 *y += intel_fb->rotated[color_plane].y;
6687c906 2224 } else {
5d2a1950
VS
2225 *x += intel_fb->normal[color_plane].x;
2226 *y += intel_fb->normal[color_plane].y;
6687c906
VS
2227 }
2228}
2229
6d19a44c
VS
2230static u32 intel_adjust_tile_offset(int *x, int *y,
2231 unsigned int tile_width,
2232 unsigned int tile_height,
2233 unsigned int tile_size,
2234 unsigned int pitch_tiles,
2235 u32 old_offset,
2236 u32 new_offset)
29cf9491 2237{
b9b24038 2238 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2239 unsigned int tiles;
2240
2241 WARN_ON(old_offset & (tile_size - 1));
2242 WARN_ON(new_offset & (tile_size - 1));
2243 WARN_ON(new_offset > old_offset);
2244
2245 tiles = (old_offset - new_offset) / tile_size;
2246
2247 *y += tiles / pitch_tiles * tile_height;
2248 *x += tiles % pitch_tiles * tile_width;
2249
b9b24038
VS
2250 /* minimize x in case it got needlessly big */
2251 *y += *x / pitch_pixels * tile_height;
2252 *x %= pitch_pixels;
2253
29cf9491
VS
2254 return new_offset;
2255}
2256
6d19a44c 2257static u32 intel_adjust_aligned_offset(int *x, int *y,
5d2a1950
VS
2258 const struct drm_framebuffer *fb,
2259 int color_plane,
6d19a44c 2260 unsigned int rotation,
df79cf44 2261 unsigned int pitch,
6d19a44c 2262 u32 old_offset, u32 new_offset)
66a2d927 2263{
6d19a44c 2264 struct drm_i915_private *dev_priv = to_i915(fb->dev);
5d2a1950 2265 unsigned int cpp = fb->format->cpp[color_plane];
66a2d927
VS
2266
2267 WARN_ON(new_offset > old_offset);
2268
2f075565 2269 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
66a2d927
VS
2270 unsigned int tile_size, tile_width, tile_height;
2271 unsigned int pitch_tiles;
2272
2273 tile_size = intel_tile_size(dev_priv);
5d2a1950 2274 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
66a2d927 2275
bd2ef25d 2276 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2277 pitch_tiles = pitch / tile_height;
2278 swap(tile_width, tile_height);
2279 } else {
2280 pitch_tiles = pitch / (tile_width * cpp);
2281 }
2282
6d19a44c
VS
2283 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2284 tile_size, pitch_tiles,
2285 old_offset, new_offset);
66a2d927
VS
2286 } else {
2287 old_offset += *y * pitch + *x * cpp;
2288
2289 *y = (old_offset - new_offset) / pitch;
2290 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2291 }
2292
2293 return new_offset;
2294}
2295
303ba695
VS
2296/*
2297 * Adjust the tile offset by moving the difference into
2298 * the x/y offsets.
2299 */
6d19a44c
VS
2300static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2301 const struct intel_plane_state *state,
5d2a1950 2302 int color_plane,
6d19a44c 2303 u32 old_offset, u32 new_offset)
303ba695 2304{
5d2a1950 2305 return intel_adjust_aligned_offset(x, y, state->base.fb, color_plane,
6d19a44c 2306 state->base.rotation,
5d2a1950 2307 state->color_plane[color_plane].stride,
6d19a44c 2308 old_offset, new_offset);
303ba695
VS
2309}
2310
8d0deca8 2311/*
6d19a44c 2312 * Computes the aligned offset to the base tile and adjusts
8d0deca8
VS
2313 * x, y. bytes per pixel is assumed to be a power-of-two.
2314 *
2315 * In the 90/270 rotated case, x and y are assumed
2316 * to be already rotated to match the rotated GTT view, and
2317 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2318 *
2319 * This function is used when computing the derived information
2320 * under intel_framebuffer, so using any of that information
2321 * here is not allowed. Anything under drm_framebuffer can be
2322 * used. This is why the user has to pass in the pitch since it
2323 * is specified in the rotated orientation.
8d0deca8 2324 */
6d19a44c
VS
2325static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2326 int *x, int *y,
5d2a1950
VS
2327 const struct drm_framebuffer *fb,
2328 int color_plane,
6d19a44c
VS
2329 unsigned int pitch,
2330 unsigned int rotation,
2331 u32 alignment)
c2c75131 2332{
bae781b2 2333 uint64_t fb_modifier = fb->modifier;
5d2a1950 2334 unsigned int cpp = fb->format->cpp[color_plane];
6687c906 2335 u32 offset, offset_aligned;
29cf9491 2336
29cf9491
VS
2337 if (alignment)
2338 alignment--;
2339
2f075565 2340 if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
8d0deca8
VS
2341 unsigned int tile_size, tile_width, tile_height;
2342 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2343
d843310d 2344 tile_size = intel_tile_size(dev_priv);
5d2a1950 2345 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
8d0deca8 2346
bd2ef25d 2347 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2348 pitch_tiles = pitch / tile_height;
2349 swap(tile_width, tile_height);
2350 } else {
2351 pitch_tiles = pitch / (tile_width * cpp);
2352 }
d843310d
VS
2353
2354 tile_rows = *y / tile_height;
2355 *y %= tile_height;
c2c75131 2356
8d0deca8
VS
2357 tiles = *x / tile_width;
2358 *x %= tile_width;
bc752862 2359
29cf9491
VS
2360 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2361 offset_aligned = offset & ~alignment;
bc752862 2362
6d19a44c
VS
2363 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2364 tile_size, pitch_tiles,
2365 offset, offset_aligned);
29cf9491 2366 } else {
bc752862 2367 offset = *y * pitch + *x * cpp;
29cf9491
VS
2368 offset_aligned = offset & ~alignment;
2369
4e9a86b6
VS
2370 *y = (offset & alignment) / pitch;
2371 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2372 }
29cf9491
VS
2373
2374 return offset_aligned;
c2c75131
SV
2375}
2376
6d19a44c
VS
2377static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2378 const struct intel_plane_state *state,
5d2a1950 2379 int color_plane)
6687c906 2380{
1e7b4fd8
VS
2381 struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2382 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2949056c
VS
2383 const struct drm_framebuffer *fb = state->base.fb;
2384 unsigned int rotation = state->base.rotation;
5d2a1950 2385 int pitch = state->color_plane[color_plane].stride;
1e7b4fd8
VS
2386 u32 alignment;
2387
2388 if (intel_plane->id == PLANE_CURSOR)
2389 alignment = intel_cursor_alignment(dev_priv);
2390 else
5d2a1950 2391 alignment = intel_surf_alignment(fb, color_plane);
6687c906 2392
5d2a1950 2393 return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
6d19a44c 2394 pitch, rotation, alignment);
6687c906
VS
2395}
2396
303ba695
VS
2397/* Convert the fb->offset[] into x/y offsets */
2398static int intel_fb_offset_to_xy(int *x, int *y,
5d2a1950
VS
2399 const struct drm_framebuffer *fb,
2400 int color_plane)
6687c906 2401{
303ba695 2402 struct drm_i915_private *dev_priv = to_i915(fb->dev);
6687c906 2403
303ba695 2404 if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
5d2a1950 2405 fb->offsets[color_plane] % intel_tile_size(dev_priv))
303ba695
VS
2406 return -EINVAL;
2407
2408 *x = 0;
2409 *y = 0;
2410
6d19a44c 2411 intel_adjust_aligned_offset(x, y,
5d2a1950
VS
2412 fb, color_plane, DRM_MODE_ROTATE_0,
2413 fb->pitches[color_plane],
2414 fb->offsets[color_plane], 0);
303ba695
VS
2415
2416 return 0;
6687c906
VS
2417}
2418
72618ebf
VS
2419static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2420{
2421 switch (fb_modifier) {
2422 case I915_FORMAT_MOD_X_TILED:
2423 return I915_TILING_X;
2424 case I915_FORMAT_MOD_Y_TILED:
2e2adb05 2425 case I915_FORMAT_MOD_Y_TILED_CCS:
72618ebf
VS
2426 return I915_TILING_Y;
2427 default:
2428 return I915_TILING_NONE;
2429 }
2430}
2431
16af25fa
VS
2432/*
2433 * From the Sky Lake PRM:
2434 * "The Color Control Surface (CCS) contains the compression status of
2435 * the cache-line pairs. The compression state of the cache-line pair
2436 * is specified by 2 bits in the CCS. Each CCS cache-line represents
2437 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2438 * cache-line-pairs. CCS is always Y tiled."
2439 *
2440 * Since cache line pairs refers to horizontally adjacent cache lines,
2441 * each cache line in the CCS corresponds to an area of 32x16 cache
2442 * lines on the main surface. Since each pixel is 4 bytes, this gives
2443 * us a ratio of one byte in the CCS for each 8x16 pixels in the
2444 * main surface.
2445 */
bbfb6ce8
VS
2446static const struct drm_format_info ccs_formats[] = {
2447 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2448 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2449 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2450 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2451};
2452
2453static const struct drm_format_info *
2454lookup_format_info(const struct drm_format_info formats[],
2455 int num_formats, u32 format)
2456{
2457 int i;
2458
2459 for (i = 0; i < num_formats; i++) {
2460 if (formats[i].format == format)
2461 return &formats[i];
2462 }
2463
2464 return NULL;
2465}
2466
2467static const struct drm_format_info *
2468intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2469{
2470 switch (cmd->modifier[0]) {
2471 case I915_FORMAT_MOD_Y_TILED_CCS:
2472 case I915_FORMAT_MOD_Yf_TILED_CCS:
2473 return lookup_format_info(ccs_formats,
2474 ARRAY_SIZE(ccs_formats),
2475 cmd->pixel_format);
2476 default:
2477 return NULL;
2478 }
2479}
2480
63eaf9ac
DP
2481bool is_ccs_modifier(u64 modifier)
2482{
2483 return modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2484 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2485}
2486
6687c906
VS
2487static int
2488intel_fill_fb_info(struct drm_i915_private *dev_priv,
2489 struct drm_framebuffer *fb)
2490{
2491 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2492 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
a5ff7a45 2493 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6687c906
VS
2494 u32 gtt_offset_rotated = 0;
2495 unsigned int max_size = 0;
bcb0b461 2496 int i, num_planes = fb->format->num_planes;
6687c906
VS
2497 unsigned int tile_size = intel_tile_size(dev_priv);
2498
2499 for (i = 0; i < num_planes; i++) {
2500 unsigned int width, height;
2501 unsigned int cpp, size;
2502 u32 offset;
2503 int x, y;
303ba695 2504 int ret;
6687c906 2505
353c8598 2506 cpp = fb->format->cpp[i];
145fcb11
VS
2507 width = drm_framebuffer_plane_width(fb->width, fb, i);
2508 height = drm_framebuffer_plane_height(fb->height, fb, i);
6687c906 2509
303ba695
VS
2510 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2511 if (ret) {
2512 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2513 i, fb->offsets[i]);
2514 return ret;
2515 }
6687c906 2516
63eaf9ac 2517 if (is_ccs_modifier(fb->modifier) && i == 1) {
2e2adb05
VS
2518 int hsub = fb->format->hsub;
2519 int vsub = fb->format->vsub;
2520 int tile_width, tile_height;
2521 int main_x, main_y;
2522 int ccs_x, ccs_y;
2523
2524 intel_tile_dims(fb, i, &tile_width, &tile_height);
303ba695
VS
2525 tile_width *= hsub;
2526 tile_height *= vsub;
2e2adb05 2527
303ba695
VS
2528 ccs_x = (x * hsub) % tile_width;
2529 ccs_y = (y * vsub) % tile_height;
2530 main_x = intel_fb->normal[0].x % tile_width;
2531 main_y = intel_fb->normal[0].y % tile_height;
2e2adb05
VS
2532
2533 /*
2534 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2535 * x/y offsets must match between CCS and the main surface.
2536 */
2537 if (main_x != ccs_x || main_y != ccs_y) {
2538 DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2539 main_x, main_y,
2540 ccs_x, ccs_y,
2541 intel_fb->normal[0].x,
2542 intel_fb->normal[0].y,
2543 x, y);
2544 return -EINVAL;
2545 }
2546 }
2547
60d5f2a4
VS
2548 /*
2549 * The fence (if used) is aligned to the start of the object
2550 * so having the framebuffer wrap around across the edge of the
2551 * fenced region doesn't really work. We have no API to configure
2552 * the fence start offset within the object (nor could we probably
2553 * on gen2/3). So it's just easier if we just require that the
2554 * fb layout agrees with the fence layout. We already check that the
2555 * fb stride matches the fence stride elsewhere.
2556 */
a5ff7a45 2557 if (i == 0 && i915_gem_object_is_tiled(obj) &&
60d5f2a4 2558 (x + width) * cpp > fb->pitches[i]) {
144cc143
VS
2559 DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2560 i, fb->offsets[i]);
60d5f2a4
VS
2561 return -EINVAL;
2562 }
2563
6687c906
VS
2564 /*
2565 * First pixel of the framebuffer from
2566 * the start of the normal gtt mapping.
2567 */
2568 intel_fb->normal[i].x = x;
2569 intel_fb->normal[i].y = y;
2570
6d19a44c
VS
2571 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
2572 fb->pitches[i],
2573 DRM_MODE_ROTATE_0,
2574 tile_size);
6687c906
VS
2575 offset /= tile_size;
2576
2f075565 2577 if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
6687c906
VS
2578 unsigned int tile_width, tile_height;
2579 unsigned int pitch_tiles;
2580 struct drm_rect r;
2581
d88c4afd 2582 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906
VS
2583
2584 rot_info->plane[i].offset = offset;
2585 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2586 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2587 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2588
2589 intel_fb->rotated[i].pitch =
2590 rot_info->plane[i].height * tile_height;
2591
2592 /* how many tiles does this plane need */
2593 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2594 /*
2595 * If the plane isn't horizontally tile aligned,
2596 * we need one more tile.
2597 */
2598 if (x != 0)
2599 size++;
2600
2601 /* rotate the x/y offsets to match the GTT view */
2602 r.x1 = x;
2603 r.y1 = y;
2604 r.x2 = x + width;
2605 r.y2 = y + height;
2606 drm_rect_rotate(&r,
2607 rot_info->plane[i].width * tile_width,
2608 rot_info->plane[i].height * tile_height,
c2c446ad 2609 DRM_MODE_ROTATE_270);
6687c906
VS
2610 x = r.x1;
2611 y = r.y1;
2612
2613 /* rotate the tile dimensions to match the GTT view */
2614 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2615 swap(tile_width, tile_height);
2616
2617 /*
2618 * We only keep the x/y offsets, so push all of the
2619 * gtt offset into the x/y offsets.
2620 */
6d19a44c
VS
2621 intel_adjust_tile_offset(&x, &y,
2622 tile_width, tile_height,
2623 tile_size, pitch_tiles,
2624 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2625
2626 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2627
2628 /*
2629 * First pixel of the framebuffer from
2630 * the start of the rotated gtt mapping.
2631 */
2632 intel_fb->rotated[i].x = x;
2633 intel_fb->rotated[i].y = y;
2634 } else {
2635 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2636 x * cpp, tile_size);
2637 }
2638
2639 /* how many tiles in total needed in the bo */
2640 max_size = max(max_size, offset + size);
2641 }
2642
4e05047d
VS
2643 if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2644 DRM_DEBUG_KMS("fb too big for bo (need %llu bytes, have %zu bytes)\n",
2645 mul_u32_u32(max_size, tile_size), obj->base.size);
6687c906
VS
2646 return -EINVAL;
2647 }
2648
2649 return 0;
2650}
2651
b35d63fa 2652static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2653{
2654 switch (format) {
2655 case DISPPLANE_8BPP:
2656 return DRM_FORMAT_C8;
2657 case DISPPLANE_BGRX555:
2658 return DRM_FORMAT_XRGB1555;
2659 case DISPPLANE_BGRX565:
2660 return DRM_FORMAT_RGB565;
2661 default:
2662 case DISPPLANE_BGRX888:
2663 return DRM_FORMAT_XRGB8888;
2664 case DISPPLANE_RGBX888:
2665 return DRM_FORMAT_XBGR8888;
2666 case DISPPLANE_BGRX101010:
2667 return DRM_FORMAT_XRGB2101010;
2668 case DISPPLANE_RGBX101010:
2669 return DRM_FORMAT_XBGR2101010;
2670 }
2671}
2672
ddf34319 2673int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
bc8d7dff
DL
2674{
2675 switch (format) {
2676 case PLANE_CTL_FORMAT_RGB_565:
2677 return DRM_FORMAT_RGB565;
f34a291c
MK
2678 case PLANE_CTL_FORMAT_NV12:
2679 return DRM_FORMAT_NV12;
bc8d7dff
DL
2680 default:
2681 case PLANE_CTL_FORMAT_XRGB_8888:
2682 if (rgb_order) {
2683 if (alpha)
2684 return DRM_FORMAT_ABGR8888;
2685 else
2686 return DRM_FORMAT_XBGR8888;
2687 } else {
2688 if (alpha)
2689 return DRM_FORMAT_ARGB8888;
2690 else
2691 return DRM_FORMAT_XRGB8888;
2692 }
2693 case PLANE_CTL_FORMAT_XRGB_2101010:
2694 if (rgb_order)
2695 return DRM_FORMAT_XBGR2101010;
2696 else
2697 return DRM_FORMAT_XRGB2101010;
2698 }
2699}
2700
5724dbd1 2701static bool
f6936e29
SV
2702intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2703 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2704{
2705 struct drm_device *dev = crtc->base.dev;
3badb49f 2706 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2707 struct drm_i915_gem_object *obj = NULL;
2708 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2709 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
SV
2710 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2711 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2712 PAGE_SIZE);
2713
2714 size_aligned -= base_aligned;
46f297fb 2715
ff2652ea
CW
2716 if (plane_config->size == 0)
2717 return false;
2718
3badb49f
PZ
2719 /* If the FB is too big, just don't use it since fbdev is not very
2720 * important and we should probably use that space with FBC or other
2721 * features. */
b1ace601 2722 if (size_aligned * 2 > dev_priv->stolen_usable_size)
3badb49f
PZ
2723 return false;
2724
d9a51586
ID
2725 switch (fb->modifier) {
2726 case DRM_FORMAT_MOD_LINEAR:
2727 case I915_FORMAT_MOD_X_TILED:
2728 case I915_FORMAT_MOD_Y_TILED:
2729 break;
2730 default:
2731 DRM_DEBUG_DRIVER("Unsupported modifier for initial FB: 0x%llx\n",
2732 fb->modifier);
2733 return false;
2734 }
2735
12c83d99 2736 mutex_lock(&dev->struct_mutex);
187685cb 2737 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
SV
2738 base_aligned,
2739 base_aligned,
2740 size_aligned);
24dbf51a
CW
2741 mutex_unlock(&dev->struct_mutex);
2742 if (!obj)
484b41dd 2743 return false;
46f297fb 2744
d9a51586
ID
2745 switch (plane_config->tiling) {
2746 case I915_TILING_NONE:
2747 break;
2748 case I915_TILING_X:
2749 case I915_TILING_Y:
2750 obj->tiling_and_stride = fb->pitches[0] | plane_config->tiling;
2751 break;
2752 default:
2753 MISSING_CASE(plane_config->tiling);
2754 return false;
2755 }
46f297fb 2756
438b74a5 2757 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
2758 mode_cmd.width = fb->width;
2759 mode_cmd.height = fb->height;
2760 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2761 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2762 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2763
24dbf51a 2764 if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
46f297fb
JB
2765 DRM_DEBUG_KMS("intel fb init failed\n");
2766 goto out_unref_obj;
2767 }
12c83d99 2768
484b41dd 2769
f6936e29 2770 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2771 return true;
46f297fb
JB
2772
2773out_unref_obj:
f8c417cd 2774 i915_gem_object_put(obj);
484b41dd
JB
2775 return false;
2776}
2777
e9728bd8
VS
2778static void
2779intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2780 struct intel_plane_state *plane_state,
2781 bool visible)
2782{
2783 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2784
2785 plane_state->base.visible = visible;
2786
9b273901 2787 if (visible)
40560e26 2788 crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
9b273901 2789 else
40560e26 2790 crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
e9728bd8
VS
2791
2792 DRM_DEBUG_KMS("%s active planes 0x%x\n",
2793 crtc_state->base.crtc->name,
2794 crtc_state->active_planes);
2795}
2796
9b273901
VS
2797static void fixup_active_planes(struct intel_crtc_state *crtc_state)
2798{
2799 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2800 struct drm_plane *plane;
2801
2802 /*
2803 * Active_planes aliases if multiple "primary" or cursor planes
2804 * have been used on the same (or wrong) pipe. plane_mask uses
2805 * unique ids, hence we can use that to reconstruct active_planes.
2806 */
2807 crtc_state->active_planes = 0;
2808
2809 drm_for_each_plane_mask(plane, &dev_priv->drm,
2810 crtc_state->base.plane_mask)
2811 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
2812}
2813
b1e01595
VS
2814static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2815 struct intel_plane *plane)
2816{
2817 struct intel_crtc_state *crtc_state =
2818 to_intel_crtc_state(crtc->base.state);
2819 struct intel_plane_state *plane_state =
2820 to_intel_plane_state(plane->base.state);
2821
2822 intel_set_plane_visible(crtc_state, plane_state, false);
9b273901 2823 fixup_active_planes(crtc_state);
b1e01595
VS
2824
2825 if (plane->id == PLANE_PRIMARY)
2826 intel_pre_disable_primary_noatomic(&crtc->base);
2827
2828 trace_intel_disable_plane(&plane->base, crtc);
2829 plane->disable_plane(plane, crtc);
2830}
2831
5724dbd1 2832static void
f6936e29
SV
2833intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2834 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2835{
2836 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2837 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 2838 struct drm_crtc *c;
2ff8fde1 2839 struct drm_i915_gem_object *obj;
88595ac9 2840 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2841 struct drm_plane_state *plane_state = primary->state;
200757f5 2842 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2843 struct intel_plane_state *intel_state =
2844 to_intel_plane_state(plane_state);
88595ac9 2845 struct drm_framebuffer *fb;
484b41dd 2846
2d14030b 2847 if (!plane_config->fb)
484b41dd
JB
2848 return;
2849
f6936e29 2850 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
SV
2851 fb = &plane_config->fb->base;
2852 goto valid_fb;
f55548b5 2853 }
484b41dd 2854
2d14030b 2855 kfree(plane_config->fb);
484b41dd
JB
2856
2857 /*
2858 * Failed to alloc the obj, check to see if we should share
2859 * an fb with another CRTC instead
2860 */
70e1e0ec 2861 for_each_crtc(dev, c) {
be1e3415 2862 struct intel_plane_state *state;
484b41dd
JB
2863
2864 if (c == &intel_crtc->base)
2865 continue;
2866
be1e3415 2867 if (!to_intel_crtc(c)->active)
2ff8fde1
MR
2868 continue;
2869
be1e3415
CW
2870 state = to_intel_plane_state(c->primary->state);
2871 if (!state->vma)
484b41dd
JB
2872 continue;
2873
be1e3415 2874 if (intel_plane_ggtt_offset(state) == plane_config->base) {
8bc20f65 2875 fb = state->base.fb;
c3ed1103 2876 drm_framebuffer_get(fb);
88595ac9 2877 goto valid_fb;
484b41dd
JB
2878 }
2879 }
88595ac9 2880
200757f5
MR
2881 /*
2882 * We've failed to reconstruct the BIOS FB. Current display state
2883 * indicates that the primary plane is visible, but has a NULL FB,
2884 * which will lead to problems later if we don't fix it up. The
2885 * simplest solution is to just disable the primary plane now and
2886 * pretend the BIOS never had it enabled.
2887 */
b1e01595 2888 intel_plane_disable_noatomic(intel_crtc, intel_plane);
200757f5 2889
88595ac9
SV
2890 return;
2891
2892valid_fb:
f559156c 2893 intel_state->base.rotation = plane_config->rotation;
f5929c53
VS
2894 intel_fill_fb_ggtt_view(&intel_state->view, fb,
2895 intel_state->base.rotation);
df79cf44
VS
2896 intel_state->color_plane[0].stride =
2897 intel_fb_pitch(fb, 0, intel_state->base.rotation);
2898
be1e3415
CW
2899 mutex_lock(&dev->struct_mutex);
2900 intel_state->vma =
5935485f 2901 intel_pin_and_fence_fb_obj(fb,
f5929c53 2902 &intel_state->view,
f7a02ad7 2903 intel_plane_uses_fence(intel_state),
5935485f 2904 &intel_state->flags);
be1e3415
CW
2905 mutex_unlock(&dev->struct_mutex);
2906 if (IS_ERR(intel_state->vma)) {
2907 DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
2908 intel_crtc->pipe, PTR_ERR(intel_state->vma));
2909
2910 intel_state->vma = NULL;
c3ed1103 2911 drm_framebuffer_put(fb);
be1e3415
CW
2912 return;
2913 }
2914
07bcd99b
DP
2915 obj = intel_fb_obj(fb);
2916 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
2917
f44e2659
VS
2918 plane_state->src_x = 0;
2919 plane_state->src_y = 0;
be5651f2
ML
2920 plane_state->src_w = fb->width << 16;
2921 plane_state->src_h = fb->height << 16;
2922
f44e2659
VS
2923 plane_state->crtc_x = 0;
2924 plane_state->crtc_y = 0;
be5651f2
ML
2925 plane_state->crtc_w = fb->width;
2926 plane_state->crtc_h = fb->height;
2927
1638d30c
RC
2928 intel_state->base.src = drm_plane_state_src(plane_state);
2929 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2930
3e510a8e 2931 if (i915_gem_object_is_tiled(obj))
88595ac9
SV
2932 dev_priv->preserve_bios_swizzle = true;
2933
cd30fbca
VS
2934 plane_state->fb = fb;
2935 plane_state->crtc = &intel_crtc->base;
e9728bd8 2936
faf5bf0a
CW
2937 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2938 &obj->frontbuffer_bits);
46f297fb
JB
2939}
2940
5d2a1950
VS
2941static int skl_max_plane_width(const struct drm_framebuffer *fb,
2942 int color_plane,
b63a16f6
VS
2943 unsigned int rotation)
2944{
5d2a1950 2945 int cpp = fb->format->cpp[color_plane];
b63a16f6 2946
bae781b2 2947 switch (fb->modifier) {
2f075565 2948 case DRM_FORMAT_MOD_LINEAR:
b63a16f6
VS
2949 case I915_FORMAT_MOD_X_TILED:
2950 switch (cpp) {
2951 case 8:
2952 return 4096;
2953 case 4:
2954 case 2:
2955 case 1:
2956 return 8192;
2957 default:
2958 MISSING_CASE(cpp);
2959 break;
2960 }
2961 break;
2e2adb05
VS
2962 case I915_FORMAT_MOD_Y_TILED_CCS:
2963 case I915_FORMAT_MOD_Yf_TILED_CCS:
2964 /* FIXME AUX plane? */
b63a16f6
VS
2965 case I915_FORMAT_MOD_Y_TILED:
2966 case I915_FORMAT_MOD_Yf_TILED:
2967 switch (cpp) {
2968 case 8:
2969 return 2048;
2970 case 4:
2971 return 4096;
2972 case 2:
2973 case 1:
2974 return 8192;
2975 default:
2976 MISSING_CASE(cpp);
2977 break;
2978 }
2979 break;
2980 default:
bae781b2 2981 MISSING_CASE(fb->modifier);
b63a16f6
VS
2982 }
2983
2984 return 2048;
2985}
2986
2e2adb05
VS
2987static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
2988 int main_x, int main_y, u32 main_offset)
2989{
2990 const struct drm_framebuffer *fb = plane_state->base.fb;
2991 int hsub = fb->format->hsub;
2992 int vsub = fb->format->vsub;
c11ada07
VS
2993 int aux_x = plane_state->color_plane[1].x;
2994 int aux_y = plane_state->color_plane[1].y;
2995 u32 aux_offset = plane_state->color_plane[1].offset;
2e2adb05
VS
2996 u32 alignment = intel_surf_alignment(fb, 1);
2997
2998 while (aux_offset >= main_offset && aux_y <= main_y) {
2999 int x, y;
3000
3001 if (aux_x == main_x && aux_y == main_y)
3002 break;
3003
3004 if (aux_offset == 0)
3005 break;
3006
3007 x = aux_x / hsub;
3008 y = aux_y / vsub;
6d19a44c
VS
3009 aux_offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 1,
3010 aux_offset, aux_offset - alignment);
2e2adb05
VS
3011 aux_x = x * hsub + aux_x % hsub;
3012 aux_y = y * vsub + aux_y % vsub;
3013 }
3014
3015 if (aux_x != main_x || aux_y != main_y)
3016 return false;
3017
c11ada07
VS
3018 plane_state->color_plane[1].offset = aux_offset;
3019 plane_state->color_plane[1].x = aux_x;
3020 plane_state->color_plane[1].y = aux_y;
2e2adb05
VS
3021
3022 return true;
3023}
3024
73266595 3025static int skl_check_main_surface(struct intel_plane_state *plane_state)
b63a16f6 3026{
b63a16f6
VS
3027 const struct drm_framebuffer *fb = plane_state->base.fb;
3028 unsigned int rotation = plane_state->base.rotation;
cc926387
SV
3029 int x = plane_state->base.src.x1 >> 16;
3030 int y = plane_state->base.src.y1 >> 16;
3031 int w = drm_rect_width(&plane_state->base.src) >> 16;
3032 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
3033 int max_width = skl_max_plane_width(fb, 0, rotation);
3034 int max_height = 4096;
c11ada07 3035 u32 alignment, offset, aux_offset = plane_state->color_plane[1].offset;
b63a16f6
VS
3036
3037 if (w > max_width || h > max_height) {
3038 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3039 w, h, max_width, max_height);
3040 return -EINVAL;
3041 }
3042
3043 intel_add_fb_offsets(&x, &y, plane_state, 0);
6d19a44c 3044 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
d88c4afd 3045 alignment = intel_surf_alignment(fb, 0);
b63a16f6 3046
8d970654
VS
3047 /*
3048 * AUX surface offset is specified as the distance from the
3049 * main surface offset, and it must be non-negative. Make
3050 * sure that is what we will get.
3051 */
3052 if (offset > aux_offset)
6d19a44c
VS
3053 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3054 offset, aux_offset & ~(alignment - 1));
8d970654 3055
b63a16f6
VS
3056 /*
3057 * When using an X-tiled surface, the plane blows up
3058 * if the x offset + width exceed the stride.
3059 *
3060 * TODO: linear and Y-tiled seem fine, Yf untested,
3061 */
bae781b2 3062 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 3063 int cpp = fb->format->cpp[0];
b63a16f6 3064
df79cf44 3065 while ((x + w) * cpp > plane_state->color_plane[0].stride) {
b63a16f6 3066 if (offset == 0) {
2e2adb05 3067 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
b63a16f6
VS
3068 return -EINVAL;
3069 }
3070
6d19a44c
VS
3071 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3072 offset, offset - alignment);
b63a16f6
VS
3073 }
3074 }
3075
2e2adb05
VS
3076 /*
3077 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3078 * they match with the main surface x/y offsets.
3079 */
63eaf9ac 3080 if (is_ccs_modifier(fb->modifier)) {
2e2adb05
VS
3081 while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3082 if (offset == 0)
3083 break;
3084
6d19a44c
VS
3085 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3086 offset, offset - alignment);
2e2adb05
VS
3087 }
3088
c11ada07 3089 if (x != plane_state->color_plane[1].x || y != plane_state->color_plane[1].y) {
2e2adb05
VS
3090 DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3091 return -EINVAL;
3092 }
3093 }
3094
c11ada07
VS
3095 plane_state->color_plane[0].offset = offset;
3096 plane_state->color_plane[0].x = x;
3097 plane_state->color_plane[0].y = y;
b63a16f6
VS
3098
3099 return 0;
3100}
3101
5d794288 3102static int
73266595 3103skl_check_nv12_surface(struct intel_plane_state *plane_state)
5d794288
ML
3104{
3105 /* Display WA #1106 */
3106 if (plane_state->base.rotation !=
3107 (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
3108 plane_state->base.rotation != DRM_MODE_ROTATE_270)
3109 return 0;
3110
3111 /*
3112 * src coordinates are rotated here.
3113 * We check height but report it as width
3114 */
3115 if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
3116 DRM_DEBUG_KMS("src width must be multiple "
3117 "of 4 for rotated NV12\n");
3118 return -EINVAL;
3119 }
3120
3121 return 0;
3122}
3123
8d970654
VS
3124static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3125{
3126 const struct drm_framebuffer *fb = plane_state->base.fb;
3127 unsigned int rotation = plane_state->base.rotation;
3128 int max_width = skl_max_plane_width(fb, 1, rotation);
3129 int max_height = 4096;
cc926387
SV
3130 int x = plane_state->base.src.x1 >> 17;
3131 int y = plane_state->base.src.y1 >> 17;
3132 int w = drm_rect_width(&plane_state->base.src) >> 17;
3133 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
3134 u32 offset;
3135
3136 intel_add_fb_offsets(&x, &y, plane_state, 1);
6d19a44c 3137 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
8d970654
VS
3138
3139 /* FIXME not quite sure how/if these apply to the chroma plane */
3140 if (w > max_width || h > max_height) {
3141 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3142 w, h, max_width, max_height);
3143 return -EINVAL;
3144 }
3145
c11ada07
VS
3146 plane_state->color_plane[1].offset = offset;
3147 plane_state->color_plane[1].x = x;
3148 plane_state->color_plane[1].y = y;
8d970654
VS
3149
3150 return 0;
3151}
3152
2e2adb05
VS
3153static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3154{
2e2adb05
VS
3155 const struct drm_framebuffer *fb = plane_state->base.fb;
3156 int src_x = plane_state->base.src.x1 >> 16;
3157 int src_y = plane_state->base.src.y1 >> 16;
3158 int hsub = fb->format->hsub;
3159 int vsub = fb->format->vsub;
3160 int x = src_x / hsub;
3161 int y = src_y / vsub;
3162 u32 offset;
3163
2e2adb05 3164 intel_add_fb_offsets(&x, &y, plane_state, 1);
6d19a44c 3165 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
2e2adb05 3166
c11ada07
VS
3167 plane_state->color_plane[1].offset = offset;
3168 plane_state->color_plane[1].x = x * hsub + src_x % hsub;
3169 plane_state->color_plane[1].y = y * vsub + src_y % vsub;
2e2adb05
VS
3170
3171 return 0;
3172}
3173
73266595 3174int skl_check_plane_surface(struct intel_plane_state *plane_state)
b63a16f6
VS
3175{
3176 const struct drm_framebuffer *fb = plane_state->base.fb;
3177 unsigned int rotation = plane_state->base.rotation;
3178 int ret;
3179
f5929c53 3180 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
df79cf44
VS
3181 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3182 plane_state->color_plane[1].stride = intel_fb_pitch(fb, 1, rotation);
5f8e3f57 3183
3f6d5ba1
VS
3184 ret = intel_plane_check_stride(plane_state);
3185 if (ret)
3186 return ret;
3187
a5e4c7d0
VS
3188 if (!plane_state->base.visible)
3189 return 0;
3190
b63a16f6 3191 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 3192 if (drm_rotation_90_or_270(rotation))
cc926387 3193 drm_rect_rotate(&plane_state->base.src,
da064b47 3194 fb->width << 16, fb->height << 16,
c2c446ad 3195 DRM_MODE_ROTATE_270);
b63a16f6 3196
8d970654
VS
3197 /*
3198 * Handle the AUX surface first since
3199 * the main surface setup depends on it.
3200 */
438b74a5 3201 if (fb->format->format == DRM_FORMAT_NV12) {
73266595 3202 ret = skl_check_nv12_surface(plane_state);
5d794288
ML
3203 if (ret)
3204 return ret;
8d970654
VS
3205 ret = skl_check_nv12_aux_surface(plane_state);
3206 if (ret)
3207 return ret;
63eaf9ac 3208 } else if (is_ccs_modifier(fb->modifier)) {
2e2adb05
VS
3209 ret = skl_check_ccs_aux_surface(plane_state);
3210 if (ret)
3211 return ret;
8d970654 3212 } else {
c11ada07
VS
3213 plane_state->color_plane[1].offset = ~0xfff;
3214 plane_state->color_plane[1].x = 0;
3215 plane_state->color_plane[1].y = 0;
8d970654
VS
3216 }
3217
73266595 3218 ret = skl_check_main_surface(plane_state);
b63a16f6
VS
3219 if (ret)
3220 return ret;
3221
3222 return 0;
3223}
3224
ddd5713d
VS
3225unsigned int
3226i9xx_plane_max_stride(struct intel_plane *plane,
3227 u32 pixel_format, u64 modifier,
3228 unsigned int rotation)
3229{
3230 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3231
3232 if (!HAS_GMCH_DISPLAY(dev_priv)) {
3233 return 32*1024;
3234 } else if (INTEL_GEN(dev_priv) >= 4) {
3235 if (modifier == I915_FORMAT_MOD_X_TILED)
3236 return 16*1024;
3237 else
3238 return 32*1024;
3239 } else if (INTEL_GEN(dev_priv) >= 3) {
3240 if (modifier == I915_FORMAT_MOD_X_TILED)
3241 return 8*1024;
3242 else
3243 return 16*1024;
3244 } else {
3245 if (plane->i9xx_plane == PLANE_C)
3246 return 4*1024;
3247 else
3248 return 8*1024;
3249 }
3250}
3251
7145f60a
VS
3252static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3253 const struct intel_plane_state *plane_state)
81255565 3254{
7145f60a
VS
3255 struct drm_i915_private *dev_priv =
3256 to_i915(plane_state->base.plane->dev);
3257 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3258 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 3259 unsigned int rotation = plane_state->base.rotation;
7145f60a 3260 u32 dspcntr;
c9ba6fad 3261
7145f60a 3262 dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
f45651ba 3263
6a4407a6
VS
3264 if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
3265 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
7145f60a 3266 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 3267
6a4407a6
VS
3268 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3269 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
f45651ba 3270
c154d1e0 3271 if (INTEL_GEN(dev_priv) < 5)
d509e28b 3272 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
81255565 3273
438b74a5 3274 switch (fb->format->format) {
57779d06 3275 case DRM_FORMAT_C8:
81255565
JB
3276 dspcntr |= DISPPLANE_8BPP;
3277 break;
57779d06 3278 case DRM_FORMAT_XRGB1555:
57779d06 3279 dspcntr |= DISPPLANE_BGRX555;
81255565 3280 break;
57779d06
VS
3281 case DRM_FORMAT_RGB565:
3282 dspcntr |= DISPPLANE_BGRX565;
3283 break;
3284 case DRM_FORMAT_XRGB8888:
57779d06
VS
3285 dspcntr |= DISPPLANE_BGRX888;
3286 break;
3287 case DRM_FORMAT_XBGR8888:
57779d06
VS
3288 dspcntr |= DISPPLANE_RGBX888;
3289 break;
3290 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3291 dspcntr |= DISPPLANE_BGRX101010;
3292 break;
3293 case DRM_FORMAT_XBGR2101010:
57779d06 3294 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3295 break;
3296 default:
7145f60a
VS
3297 MISSING_CASE(fb->format->format);
3298 return 0;
81255565 3299 }
57779d06 3300
72618ebf 3301 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3302 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3303 dspcntr |= DISPPLANE_TILED;
81255565 3304
c2c446ad 3305 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
3306 dspcntr |= DISPPLANE_ROTATE_180;
3307
c2c446ad 3308 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
3309 dspcntr |= DISPPLANE_MIRROR;
3310
7145f60a
VS
3311 return dspcntr;
3312}
de1aa629 3313
f9407ae1 3314int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
3315{
3316 struct drm_i915_private *dev_priv =
3317 to_i915(plane_state->base.plane->dev);
df79cf44
VS
3318 const struct drm_framebuffer *fb = plane_state->base.fb;
3319 unsigned int rotation = plane_state->base.rotation;
5b7fcc44
VS
3320 int src_x = plane_state->base.src.x1 >> 16;
3321 int src_y = plane_state->base.src.y1 >> 16;
3322 u32 offset;
3f6d5ba1 3323 int ret;
81255565 3324
f5929c53 3325 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
df79cf44
VS
3326 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
3327
3f6d5ba1
VS
3328 ret = intel_plane_check_stride(plane_state);
3329 if (ret)
3330 return ret;
3331
5b7fcc44 3332 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 3333
5b7fcc44 3334 if (INTEL_GEN(dev_priv) >= 4)
6d19a44c
VS
3335 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
3336 plane_state, 0);
5b7fcc44
VS
3337 else
3338 offset = 0;
3339
3340 /* HSW/BDW do this automagically in hardware */
3341 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
5b7fcc44
VS
3342 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3343 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3344
c2c446ad 3345 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
3346 src_x += src_w - 1;
3347 src_y += src_h - 1;
c2c446ad 3348 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
3349 src_x += src_w - 1;
3350 }
48404c1e
SJ
3351 }
3352
c11ada07
VS
3353 plane_state->color_plane[0].offset = offset;
3354 plane_state->color_plane[0].x = src_x;
3355 plane_state->color_plane[0].y = src_y;
5b7fcc44
VS
3356
3357 return 0;
3358}
3359
4e0b83a5
VS
3360static int
3361i9xx_plane_check(struct intel_crtc_state *crtc_state,
3362 struct intel_plane_state *plane_state)
3363{
3364 int ret;
3365
25721f82
VS
3366 ret = chv_plane_check_rotation(plane_state);
3367 if (ret)
3368 return ret;
3369
4e0b83a5
VS
3370 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
3371 &crtc_state->base,
3372 DRM_PLANE_HELPER_NO_SCALING,
3373 DRM_PLANE_HELPER_NO_SCALING,
3374 false, true);
3375 if (ret)
3376 return ret;
3377
3378 if (!plane_state->base.visible)
3379 return 0;
3380
3381 ret = intel_plane_check_src_coordinates(plane_state);
3382 if (ret)
3383 return ret;
3384
3385 ret = i9xx_check_plane_surface(plane_state);
3386 if (ret)
3387 return ret;
3388
3389 plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
5b7fcc44
VS
3390
3391 return 0;
3392}
3393
ed15030d
VS
3394static void i9xx_update_plane(struct intel_plane *plane,
3395 const struct intel_crtc_state *crtc_state,
3396 const struct intel_plane_state *plane_state)
7145f60a 3397{
ed15030d 3398 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
ed15030d 3399 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
7145f60a 3400 u32 linear_offset;
a0864d59 3401 u32 dspcntr = plane_state->ctl;
ed15030d 3402 i915_reg_t reg = DSPCNTR(i9xx_plane);
c11ada07
VS
3403 int x = plane_state->color_plane[0].x;
3404 int y = plane_state->color_plane[0].y;
7145f60a 3405 unsigned long irqflags;
e288881b 3406 u32 dspaddr_offset;
7145f60a 3407
2949056c 3408 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3409
5b7fcc44 3410 if (INTEL_GEN(dev_priv) >= 4)
c11ada07 3411 dspaddr_offset = plane_state->color_plane[0].offset;
5b7fcc44 3412 else
e288881b 3413 dspaddr_offset = linear_offset;
6687c906 3414
dd584fc0
VS
3415 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3416
78587de2
VS
3417 if (INTEL_GEN(dev_priv) < 4) {
3418 /* pipesrc and dspsize control the size that is scaled from,
3419 * which should always be the user's requested size.
3420 */
ed15030d 3421 I915_WRITE_FW(DSPSIZE(i9xx_plane),
dd584fc0
VS
3422 ((crtc_state->pipe_src_h - 1) << 16) |
3423 (crtc_state->pipe_src_w - 1));
ed15030d
VS
3424 I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
3425 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
3426 I915_WRITE_FW(PRIMSIZE(i9xx_plane),
dd584fc0
VS
3427 ((crtc_state->pipe_src_h - 1) << 16) |
3428 (crtc_state->pipe_src_w - 1));
ed15030d
VS
3429 I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
3430 I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
78587de2
VS
3431 }
3432
dd584fc0 3433 I915_WRITE_FW(reg, dspcntr);
48404c1e 3434
df79cf44 3435 I915_WRITE_FW(DSPSTRIDE(i9xx_plane), plane_state->color_plane[0].stride);
3ba35e53 3436 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
ed15030d 3437 I915_WRITE_FW(DSPSURF(i9xx_plane),
3ba35e53 3438 intel_plane_ggtt_offset(plane_state) +
e288881b 3439 dspaddr_offset);
ed15030d 3440 I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
3ba35e53 3441 } else if (INTEL_GEN(dev_priv) >= 4) {
ed15030d 3442 I915_WRITE_FW(DSPSURF(i9xx_plane),
dd584fc0 3443 intel_plane_ggtt_offset(plane_state) +
e288881b 3444 dspaddr_offset);
ed15030d
VS
3445 I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
3446 I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
bfb81049 3447 } else {
ed15030d 3448 I915_WRITE_FW(DSPADDR(i9xx_plane),
dd584fc0 3449 intel_plane_ggtt_offset(plane_state) +
e288881b 3450 dspaddr_offset);
bfb81049 3451 }
dd584fc0
VS
3452 POSTING_READ_FW(reg);
3453
3454 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
3455}
3456
ed15030d
VS
3457static void i9xx_disable_plane(struct intel_plane *plane,
3458 struct intel_crtc *crtc)
17638cd6 3459{
ed15030d
VS
3460 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3461 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
dd584fc0
VS
3462 unsigned long irqflags;
3463
3464 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 3465
ed15030d
VS
3466 I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
3467 if (INTEL_GEN(dev_priv) >= 4)
3468 I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
a8d201af 3469 else
ed15030d
VS
3470 I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
3471 POSTING_READ_FW(DSPCNTR(i9xx_plane));
dd584fc0
VS
3472
3473 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 3474}
c9ba6fad 3475
eade6c89
VS
3476static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
3477 enum pipe *pipe)
51f5a096 3478{
ed15030d 3479 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
51f5a096 3480 enum intel_display_power_domain power_domain;
ed15030d 3481 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
51f5a096 3482 bool ret;
eade6c89 3483 u32 val;
51f5a096
VS
3484
3485 /*
3486 * Not 100% correct for planes that can move between pipes,
3487 * but that's only the case for gen2-4 which don't have any
3488 * display power wells.
3489 */
eade6c89 3490 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
51f5a096
VS
3491 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
3492 return false;
3493
eade6c89
VS
3494 val = I915_READ(DSPCNTR(i9xx_plane));
3495
3496 ret = val & DISPLAY_PLANE_ENABLE;
3497
3498 if (INTEL_GEN(dev_priv) >= 5)
3499 *pipe = plane->pipe;
3500 else
3501 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
3502 DISPPLANE_SEL_PIPE_SHIFT;
51f5a096
VS
3503
3504 intel_display_power_put(dev_priv, power_domain);
3505
3506 return ret;
3507}
3508
d88c4afd 3509static u32
5d2a1950 3510intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
b321803d 3511{
2f075565 3512 if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
b321803d 3513 return 64;
d88c4afd 3514 else
5d2a1950 3515 return intel_tile_width_bytes(fb, color_plane);
b321803d
DL
3516}
3517
e435d6e5
ML
3518static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3519{
3520 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3521 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3522
3523 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3524 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3525 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3526}
3527
a1b2278e
CK
3528/*
3529 * This function detaches (aka. unbinds) unused scalers in hardware
3530 */
0583236e 3531static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3532{
a1b2278e
CK
3533 struct intel_crtc_scaler_state *scaler_state;
3534 int i;
3535
a1b2278e
CK
3536 scaler_state = &intel_crtc->config->scaler_state;
3537
3538 /* loop through and disable scalers that aren't in use */
3539 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3540 if (!scaler_state->scalers[i].in_use)
3541 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3542 }
3543}
3544
df79cf44 3545u32 skl_plane_stride(const struct intel_plane_state *plane_state,
5d2a1950 3546 int color_plane)
d2196774 3547{
df79cf44
VS
3548 const struct drm_framebuffer *fb = plane_state->base.fb;
3549 unsigned int rotation = plane_state->base.rotation;
5d2a1950 3550 u32 stride = plane_state->color_plane[color_plane].stride;
1b500535 3551
5d2a1950 3552 if (color_plane >= fb->format->num_planes)
1b500535
VS
3553 return 0;
3554
d2196774
VS
3555 /*
3556 * The stride is either expressed as a multiple of 64 bytes chunks for
3557 * linear buffers or in number of tiles for tiled buffers.
3558 */
d88c4afd 3559 if (drm_rotation_90_or_270(rotation))
5d2a1950 3560 stride /= intel_tile_height(fb, color_plane);
d88c4afd 3561 else
5d2a1950 3562 stride /= intel_fb_stride_alignment(fb, color_plane);
d2196774
VS
3563
3564 return stride;
3565}
3566
2e881264 3567static u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3568{
6156a456 3569 switch (pixel_format) {
d161cf7a 3570 case DRM_FORMAT_C8:
c34ce3d1 3571 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3572 case DRM_FORMAT_RGB565:
c34ce3d1 3573 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3574 case DRM_FORMAT_XBGR8888:
4036c78c 3575 case DRM_FORMAT_ABGR8888:
c34ce3d1 3576 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3577 case DRM_FORMAT_XRGB8888:
6156a456 3578 case DRM_FORMAT_ARGB8888:
4036c78c 3579 return PLANE_CTL_FORMAT_XRGB_8888;
70d21f0e 3580 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3581 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3582 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3583 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3584 case DRM_FORMAT_YUYV:
c34ce3d1 3585 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3586 case DRM_FORMAT_YVYU:
c34ce3d1 3587 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3588 case DRM_FORMAT_UYVY:
c34ce3d1 3589 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3590 case DRM_FORMAT_VYUY:
c34ce3d1 3591 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
77224cd5
CK
3592 case DRM_FORMAT_NV12:
3593 return PLANE_CTL_FORMAT_NV12;
70d21f0e 3594 default:
4249eeef 3595 MISSING_CASE(pixel_format);
70d21f0e 3596 }
8cfcba41 3597
c34ce3d1 3598 return 0;
6156a456 3599}
70d21f0e 3600
4036c78c
JA
3601/*
3602 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3603 * to be already pre-multiplied. We need to add a knob (or a different
3604 * DRM_FORMAT) for user-space to configure that.
3605 */
3606static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
3607{
3608 switch (pixel_format) {
3609 case DRM_FORMAT_ABGR8888:
3610 case DRM_FORMAT_ARGB8888:
3611 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3612 default:
3613 return PLANE_CTL_ALPHA_DISABLE;
3614 }
3615}
3616
3617static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
3618{
3619 switch (pixel_format) {
3620 case DRM_FORMAT_ABGR8888:
3621 case DRM_FORMAT_ARGB8888:
3622 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
3623 default:
3624 return PLANE_COLOR_ALPHA_DISABLE;
3625 }
3626}
3627
2e881264 3628static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
6156a456 3629{
6156a456 3630 switch (fb_modifier) {
2f075565 3631 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 3632 break;
30af77c4 3633 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3634 return PLANE_CTL_TILED_X;
b321803d 3635 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3636 return PLANE_CTL_TILED_Y;
2e2adb05 3637 case I915_FORMAT_MOD_Y_TILED_CCS:
53867b46 3638 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
b321803d 3639 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3640 return PLANE_CTL_TILED_YF;
2e2adb05 3641 case I915_FORMAT_MOD_Yf_TILED_CCS:
53867b46 3642 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
70d21f0e 3643 default:
6156a456 3644 MISSING_CASE(fb_modifier);
70d21f0e 3645 }
8cfcba41 3646
c34ce3d1 3647 return 0;
6156a456 3648}
70d21f0e 3649
5f8e3f57 3650static u32 skl_plane_ctl_rotate(unsigned int rotate)
6156a456 3651{
5f8e3f57 3652 switch (rotate) {
c2c446ad 3653 case DRM_MODE_ROTATE_0:
6156a456 3654 break;
1e8df167 3655 /*
c2c446ad 3656 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
3657 * while i915 HW rotation is clockwise, thats why this swapping.
3658 */
c2c446ad 3659 case DRM_MODE_ROTATE_90:
1e8df167 3660 return PLANE_CTL_ROTATE_270;
c2c446ad 3661 case DRM_MODE_ROTATE_180:
c34ce3d1 3662 return PLANE_CTL_ROTATE_180;
c2c446ad 3663 case DRM_MODE_ROTATE_270:
1e8df167 3664 return PLANE_CTL_ROTATE_90;
6156a456 3665 default:
5f8e3f57
JL
3666 MISSING_CASE(rotate);
3667 }
3668
3669 return 0;
3670}
3671
3672static u32 cnl_plane_ctl_flip(unsigned int reflect)
3673{
3674 switch (reflect) {
3675 case 0:
3676 break;
3677 case DRM_MODE_REFLECT_X:
3678 return PLANE_CTL_FLIP_HORIZONTAL;
3679 case DRM_MODE_REFLECT_Y:
3680 default:
3681 MISSING_CASE(reflect);
6156a456
CK
3682 }
3683
c34ce3d1 3684 return 0;
6156a456
CK
3685}
3686
2e881264
VS
3687u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
3688 const struct intel_plane_state *plane_state)
46f788ba
VS
3689{
3690 struct drm_i915_private *dev_priv =
3691 to_i915(plane_state->base.plane->dev);
3692 const struct drm_framebuffer *fb = plane_state->base.fb;
3693 unsigned int rotation = plane_state->base.rotation;
2e881264 3694 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
3695 u32 plane_ctl;
3696
3697 plane_ctl = PLANE_CTL_ENABLE;
3698
4036c78c
JA
3699 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
3700 plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
46f788ba
VS
3701 plane_ctl |=
3702 PLANE_CTL_PIPE_GAMMA_ENABLE |
3703 PLANE_CTL_PIPE_CSC_ENABLE |
3704 PLANE_CTL_PLANE_GAMMA_DISABLE;
b0f5c0ba
VS
3705
3706 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3707 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
c8624ede
VS
3708
3709 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3710 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
46f788ba
VS
3711 }
3712
3713 plane_ctl |= skl_plane_ctl_format(fb->format->format);
3714 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
5f8e3f57
JL
3715 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
3716
3717 if (INTEL_GEN(dev_priv) >= 10)
3718 plane_ctl |= cnl_plane_ctl_flip(rotation &
3719 DRM_MODE_REFLECT_MASK);
46f788ba 3720
2e881264
VS
3721 if (key->flags & I915_SET_COLORKEY_DESTINATION)
3722 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
3723 else if (key->flags & I915_SET_COLORKEY_SOURCE)
3724 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
3725
46f788ba
VS
3726 return plane_ctl;
3727}
3728
4036c78c
JA
3729u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
3730 const struct intel_plane_state *plane_state)
3731{
077ef1f0
JA
3732 struct drm_i915_private *dev_priv =
3733 to_i915(plane_state->base.plane->dev);
4036c78c
JA
3734 const struct drm_framebuffer *fb = plane_state->base.fb;
3735 u32 plane_color_ctl = 0;
3736
077ef1f0
JA
3737 if (INTEL_GEN(dev_priv) < 11) {
3738 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
3739 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
3740 }
4036c78c
JA
3741 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
3742 plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
3743
9bace659 3744 if (fb->format->is_yuv) {
b0f5c0ba
VS
3745 if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
3746 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
3747 else
3748 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
c8624ede
VS
3749
3750 if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
3751 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
b0f5c0ba 3752 }
012d79e6 3753
4036c78c
JA
3754 return plane_color_ctl;
3755}
3756
73974893
ML
3757static int
3758__intel_display_resume(struct drm_device *dev,
581e49fe
ML
3759 struct drm_atomic_state *state,
3760 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
3761{
3762 struct drm_crtc_state *crtc_state;
3763 struct drm_crtc *crtc;
3764 int i, ret;
11c22da6 3765
aecd36b8 3766 intel_modeset_setup_hw_state(dev, ctx);
29b74b7f 3767 i915_redisable_vga(to_i915(dev));
73974893
ML
3768
3769 if (!state)
3770 return 0;
3771
aa5e9b47
ML
3772 /*
3773 * We've duplicated the state, pointers to the old state are invalid.
3774 *
3775 * Don't attempt to use the old state until we commit the duplicated state.
3776 */
3777 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
3778 /*
3779 * Force recalculation even if we restore
3780 * current state. With fast modeset this may not result
3781 * in a modeset when the state is compatible.
3782 */
3783 crtc_state->mode_changed = true;
96a02917 3784 }
73974893
ML
3785
3786 /* ignore any reset values/BIOS leftovers in the WM registers */
602ae835
VS
3787 if (!HAS_GMCH_DISPLAY(to_i915(dev)))
3788 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 3789
581e49fe 3790 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893
ML
3791
3792 WARN_ON(ret == -EDEADLK);
3793 return ret;
96a02917
VS
3794}
3795
4ac2ba2f
VS
3796static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3797{
ae98104b
VS
3798 return intel_has_gpu_reset(dev_priv) &&
3799 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3800}
3801
c033666a 3802void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3803{
73974893
ML
3804 struct drm_device *dev = &dev_priv->drm;
3805 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3806 struct drm_atomic_state *state;
3807 int ret;
3808
ce87ea15 3809 /* reset doesn't touch the display */
4f044a88 3810 if (!i915_modparams.force_reset_modeset_test &&
ce87ea15
SV
3811 !gpu_reset_clobbers_display(dev_priv))
3812 return;
3813
9db529aa
SV
3814 /* We have a modeset vs reset deadlock, defensively unbreak it. */
3815 set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
3816 wake_up_all(&dev_priv->gpu_error.wait_queue);
3817
3818 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
3819 DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
3820 i915_gem_set_wedged(dev_priv);
3821 }
97154ec2 3822
73974893
ML
3823 /*
3824 * Need mode_config.mutex so that we don't
3825 * trample ongoing ->detect() and whatnot.
3826 */
3827 mutex_lock(&dev->mode_config.mutex);
3828 drm_modeset_acquire_init(ctx, 0);
3829 while (1) {
3830 ret = drm_modeset_lock_all_ctx(dev, ctx);
3831 if (ret != -EDEADLK)
3832 break;
3833
3834 drm_modeset_backoff(ctx);
3835 }
f98ce92f
VS
3836 /*
3837 * Disabling the crtcs gracefully seems nicer. Also the
3838 * g33 docs say we should at least disable all the planes.
3839 */
73974893
ML
3840 state = drm_atomic_helper_duplicate_state(dev, ctx);
3841 if (IS_ERR(state)) {
3842 ret = PTR_ERR(state);
73974893 3843 DRM_ERROR("Duplicating state failed with %i\n", ret);
1e5a15d6 3844 return;
73974893
ML
3845 }
3846
3847 ret = drm_atomic_helper_disable_all(dev, ctx);
3848 if (ret) {
3849 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
1e5a15d6
ACO
3850 drm_atomic_state_put(state);
3851 return;
73974893
ML
3852 }
3853
3854 dev_priv->modeset_restore_state = state;
3855 state->acquire_ctx = ctx;
7514747d
VS
3856}
3857
c033666a 3858void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3859{
73974893
ML
3860 struct drm_device *dev = &dev_priv->drm;
3861 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
40da1d31 3862 struct drm_atomic_state *state;
73974893
ML
3863 int ret;
3864
ce87ea15 3865 /* reset doesn't touch the display */
40da1d31 3866 if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
ce87ea15
SV
3867 return;
3868
40da1d31 3869 state = fetch_and_zero(&dev_priv->modeset_restore_state);
ce87ea15
SV
3870 if (!state)
3871 goto unlock;
3872
7514747d 3873 /* reset doesn't touch the display */
4ac2ba2f 3874 if (!gpu_reset_clobbers_display(dev_priv)) {
ce87ea15
SV
3875 /* for testing only restore the display */
3876 ret = __intel_display_resume(dev, state, ctx);
942d5d0d
CW
3877 if (ret)
3878 DRM_ERROR("Restoring old state failed with %i\n", ret);
73974893
ML
3879 } else {
3880 /*
3881 * The display has been reset as well,
3882 * so need a full re-initialization.
3883 */
3884 intel_runtime_pm_disable_interrupts(dev_priv);
3885 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3886
51f59205 3887 intel_pps_unlock_regs_wa(dev_priv);
73974893 3888 intel_modeset_init_hw(dev);
f72b84c6 3889 intel_init_clock_gating(dev_priv);
7514747d 3890
73974893
ML
3891 spin_lock_irq(&dev_priv->irq_lock);
3892 if (dev_priv->display.hpd_irq_setup)
3893 dev_priv->display.hpd_irq_setup(dev_priv);
3894 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3895
581e49fe 3896 ret = __intel_display_resume(dev, state, ctx);
73974893
ML
3897 if (ret)
3898 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3899
73974893
ML
3900 intel_hpd_init(dev_priv);
3901 }
7514747d 3902
ce87ea15
SV
3903 drm_atomic_state_put(state);
3904unlock:
73974893
ML
3905 drm_modeset_drop_locks(ctx);
3906 drm_modeset_acquire_fini(ctx);
3907 mutex_unlock(&dev->mode_config.mutex);
9db529aa
SV
3908
3909 clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
7514747d
VS
3910}
3911
1a15b77b
VS
3912static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
3913 const struct intel_crtc_state *new_crtc_state)
e30e8f75 3914{
1a15b77b 3915 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
6315b5d3 3916 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
e30e8f75 3917
bfd16b2a 3918 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
1a15b77b 3919 crtc->base.mode = new_crtc_state->base.mode;
bfd16b2a 3920
e30e8f75
GP
3921 /*
3922 * Update pipe size and adjust fitter if needed: the reason for this is
3923 * that in compute_mode_changes we check the native mode (not the pfit
3924 * mode) to see if we can flip rather than do a full mode set. In the
3925 * fastboot case, we'll flip, but if we don't update the pipesrc and
3926 * pfit state, we'll end up with a big fb scanned out into the wrong
3927 * sized surface.
e30e8f75
GP
3928 */
3929
e30e8f75 3930 I915_WRITE(PIPESRC(crtc->pipe),
1a15b77b
VS
3931 ((new_crtc_state->pipe_src_w - 1) << 16) |
3932 (new_crtc_state->pipe_src_h - 1));
bfd16b2a
ML
3933
3934 /* on skylake this is done by detaching scalers */
6315b5d3 3935 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3936 skl_detach_scalers(crtc);
3937
1a15b77b 3938 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a 3939 skylake_pfit_enable(crtc);
6e266956 3940 } else if (HAS_PCH_SPLIT(dev_priv)) {
1a15b77b 3941 if (new_crtc_state->pch_pfit.enabled)
bfd16b2a
ML
3942 ironlake_pfit_enable(crtc);
3943 else if (old_crtc_state->pch_pfit.enabled)
3944 ironlake_pfit_disable(crtc, true);
e30e8f75 3945 }
e30e8f75
GP
3946}
3947
4cbe4b2b 3948static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 3949{
4cbe4b2b 3950 struct drm_device *dev = crtc->base.dev;
fac5e23e 3951 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3952 int pipe = crtc->pipe;
f0f59a00
VS
3953 i915_reg_t reg;
3954 u32 temp;
5e84e1a4
ZW
3955
3956 /* enable normal train */
3957 reg = FDI_TX_CTL(pipe);
3958 temp = I915_READ(reg);
fd6b8f43 3959 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3960 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3961 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3962 } else {
3963 temp &= ~FDI_LINK_TRAIN_NONE;
3964 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3965 }
5e84e1a4
ZW
3966 I915_WRITE(reg, temp);
3967
3968 reg = FDI_RX_CTL(pipe);
3969 temp = I915_READ(reg);
6e266956 3970 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3971 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3972 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3973 } else {
3974 temp &= ~FDI_LINK_TRAIN_NONE;
3975 temp |= FDI_LINK_TRAIN_NONE;
3976 }
3977 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3978
3979 /* wait one idle pattern time */
3980 POSTING_READ(reg);
3981 udelay(1000);
357555c0
JB
3982
3983 /* IVB wants error correction enabled */
fd6b8f43 3984 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3985 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3986 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3987}
3988
8db9d77b 3989/* The FDI link training functions for ILK/Ibexpeak. */
dc4a1094
ACO
3990static void ironlake_fdi_link_train(struct intel_crtc *crtc,
3991 const struct intel_crtc_state *crtc_state)
8db9d77b 3992{
4cbe4b2b 3993 struct drm_device *dev = crtc->base.dev;
fac5e23e 3994 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 3995 int pipe = crtc->pipe;
f0f59a00
VS
3996 i915_reg_t reg;
3997 u32 temp, tries;
8db9d77b 3998
1c8562f6 3999 /* FDI needs bits from pipe first */
0fc932b8 4000 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 4001
e1a44743
AJ
4002 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4003 for train result */
5eddb70b
CW
4004 reg = FDI_RX_IMR(pipe);
4005 temp = I915_READ(reg);
e1a44743
AJ
4006 temp &= ~FDI_RX_SYMBOL_LOCK;
4007 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
4008 I915_WRITE(reg, temp);
4009 I915_READ(reg);
e1a44743
AJ
4010 udelay(150);
4011
8db9d77b 4012 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
4013 reg = FDI_TX_CTL(pipe);
4014 temp = I915_READ(reg);
627eb5a3 4015 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4016 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
4017 temp &= ~FDI_LINK_TRAIN_NONE;
4018 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 4019 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 4020
5eddb70b
CW
4021 reg = FDI_RX_CTL(pipe);
4022 temp = I915_READ(reg);
8db9d77b
ZW
4023 temp &= ~FDI_LINK_TRAIN_NONE;
4024 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
4025 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4026
4027 POSTING_READ(reg);
8db9d77b
ZW
4028 udelay(150);
4029
5b2adf89 4030 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
SV
4031 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
4032 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
4033 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 4034
5eddb70b 4035 reg = FDI_RX_IIR(pipe);
e1a44743 4036 for (tries = 0; tries < 5; tries++) {
5eddb70b 4037 temp = I915_READ(reg);
8db9d77b
ZW
4038 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4039
4040 if ((temp & FDI_RX_BIT_LOCK)) {
4041 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 4042 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
4043 break;
4044 }
8db9d77b 4045 }
e1a44743 4046 if (tries == 5)
5eddb70b 4047 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
4048
4049 /* Train 2 */
5eddb70b
CW
4050 reg = FDI_TX_CTL(pipe);
4051 temp = I915_READ(reg);
8db9d77b
ZW
4052 temp &= ~FDI_LINK_TRAIN_NONE;
4053 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 4054 I915_WRITE(reg, temp);
8db9d77b 4055
5eddb70b
CW
4056 reg = FDI_RX_CTL(pipe);
4057 temp = I915_READ(reg);
8db9d77b
ZW
4058 temp &= ~FDI_LINK_TRAIN_NONE;
4059 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 4060 I915_WRITE(reg, temp);
8db9d77b 4061
5eddb70b
CW
4062 POSTING_READ(reg);
4063 udelay(150);
8db9d77b 4064
5eddb70b 4065 reg = FDI_RX_IIR(pipe);
e1a44743 4066 for (tries = 0; tries < 5; tries++) {
5eddb70b 4067 temp = I915_READ(reg);
8db9d77b
ZW
4068 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4069
4070 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 4071 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
4072 DRM_DEBUG_KMS("FDI train 2 done.\n");
4073 break;
4074 }
8db9d77b 4075 }
e1a44743 4076 if (tries == 5)
5eddb70b 4077 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4078
4079 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 4080
8db9d77b
ZW
4081}
4082
0206e353 4083static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
4084 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
4085 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
4086 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
4087 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
4088};
4089
4090/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
4091static void gen6_fdi_link_train(struct intel_crtc *crtc,
4092 const struct intel_crtc_state *crtc_state)
8db9d77b 4093{
4cbe4b2b 4094 struct drm_device *dev = crtc->base.dev;
fac5e23e 4095 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4096 int pipe = crtc->pipe;
f0f59a00
VS
4097 i915_reg_t reg;
4098 u32 temp, i, retry;
8db9d77b 4099
e1a44743
AJ
4100 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4101 for train result */
5eddb70b
CW
4102 reg = FDI_RX_IMR(pipe);
4103 temp = I915_READ(reg);
e1a44743
AJ
4104 temp &= ~FDI_RX_SYMBOL_LOCK;
4105 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
4106 I915_WRITE(reg, temp);
4107
4108 POSTING_READ(reg);
e1a44743
AJ
4109 udelay(150);
4110
8db9d77b 4111 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
4112 reg = FDI_TX_CTL(pipe);
4113 temp = I915_READ(reg);
627eb5a3 4114 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4115 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
4116 temp &= ~FDI_LINK_TRAIN_NONE;
4117 temp |= FDI_LINK_TRAIN_PATTERN_1;
4118 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4119 /* SNB-B */
4120 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 4121 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 4122
d74cf324
SV
4123 I915_WRITE(FDI_RX_MISC(pipe),
4124 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4125
5eddb70b
CW
4126 reg = FDI_RX_CTL(pipe);
4127 temp = I915_READ(reg);
6e266956 4128 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
4129 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4130 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4131 } else {
4132 temp &= ~FDI_LINK_TRAIN_NONE;
4133 temp |= FDI_LINK_TRAIN_PATTERN_1;
4134 }
5eddb70b
CW
4135 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4136
4137 POSTING_READ(reg);
8db9d77b
ZW
4138 udelay(150);
4139
0206e353 4140 for (i = 0; i < 4; i++) {
5eddb70b
CW
4141 reg = FDI_TX_CTL(pipe);
4142 temp = I915_READ(reg);
8db9d77b
ZW
4143 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4144 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
4145 I915_WRITE(reg, temp);
4146
4147 POSTING_READ(reg);
8db9d77b
ZW
4148 udelay(500);
4149
fa37d39e
SP
4150 for (retry = 0; retry < 5; retry++) {
4151 reg = FDI_RX_IIR(pipe);
4152 temp = I915_READ(reg);
4153 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4154 if (temp & FDI_RX_BIT_LOCK) {
4155 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4156 DRM_DEBUG_KMS("FDI train 1 done.\n");
4157 break;
4158 }
4159 udelay(50);
8db9d77b 4160 }
fa37d39e
SP
4161 if (retry < 5)
4162 break;
8db9d77b
ZW
4163 }
4164 if (i == 4)
5eddb70b 4165 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
4166
4167 /* Train 2 */
5eddb70b
CW
4168 reg = FDI_TX_CTL(pipe);
4169 temp = I915_READ(reg);
8db9d77b
ZW
4170 temp &= ~FDI_LINK_TRAIN_NONE;
4171 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 4172 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
4173 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4174 /* SNB-B */
4175 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4176 }
5eddb70b 4177 I915_WRITE(reg, temp);
8db9d77b 4178
5eddb70b
CW
4179 reg = FDI_RX_CTL(pipe);
4180 temp = I915_READ(reg);
6e266956 4181 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
4182 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4183 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4184 } else {
4185 temp &= ~FDI_LINK_TRAIN_NONE;
4186 temp |= FDI_LINK_TRAIN_PATTERN_2;
4187 }
5eddb70b
CW
4188 I915_WRITE(reg, temp);
4189
4190 POSTING_READ(reg);
8db9d77b
ZW
4191 udelay(150);
4192
0206e353 4193 for (i = 0; i < 4; i++) {
5eddb70b
CW
4194 reg = FDI_TX_CTL(pipe);
4195 temp = I915_READ(reg);
8db9d77b
ZW
4196 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4197 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
4198 I915_WRITE(reg, temp);
4199
4200 POSTING_READ(reg);
8db9d77b
ZW
4201 udelay(500);
4202
fa37d39e
SP
4203 for (retry = 0; retry < 5; retry++) {
4204 reg = FDI_RX_IIR(pipe);
4205 temp = I915_READ(reg);
4206 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4207 if (temp & FDI_RX_SYMBOL_LOCK) {
4208 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4209 DRM_DEBUG_KMS("FDI train 2 done.\n");
4210 break;
4211 }
4212 udelay(50);
8db9d77b 4213 }
fa37d39e
SP
4214 if (retry < 5)
4215 break;
8db9d77b
ZW
4216 }
4217 if (i == 4)
5eddb70b 4218 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
4219
4220 DRM_DEBUG_KMS("FDI train done.\n");
4221}
4222
357555c0 4223/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
4224static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4225 const struct intel_crtc_state *crtc_state)
357555c0 4226{
4cbe4b2b 4227 struct drm_device *dev = crtc->base.dev;
fac5e23e 4228 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4229 int pipe = crtc->pipe;
f0f59a00
VS
4230 i915_reg_t reg;
4231 u32 temp, i, j;
357555c0
JB
4232
4233 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4234 for train result */
4235 reg = FDI_RX_IMR(pipe);
4236 temp = I915_READ(reg);
4237 temp &= ~FDI_RX_SYMBOL_LOCK;
4238 temp &= ~FDI_RX_BIT_LOCK;
4239 I915_WRITE(reg, temp);
4240
4241 POSTING_READ(reg);
4242 udelay(150);
4243
01a415fd
SV
4244 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4245 I915_READ(FDI_RX_IIR(pipe)));
4246
139ccd3f
JB
4247 /* Try each vswing and preemphasis setting twice before moving on */
4248 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4249 /* disable first in case we need to retry */
4250 reg = FDI_TX_CTL(pipe);
4251 temp = I915_READ(reg);
4252 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4253 temp &= ~FDI_TX_ENABLE;
4254 I915_WRITE(reg, temp);
357555c0 4255
139ccd3f
JB
4256 reg = FDI_RX_CTL(pipe);
4257 temp = I915_READ(reg);
4258 temp &= ~FDI_LINK_TRAIN_AUTO;
4259 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4260 temp &= ~FDI_RX_ENABLE;
4261 I915_WRITE(reg, temp);
357555c0 4262
139ccd3f 4263 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4264 reg = FDI_TX_CTL(pipe);
4265 temp = I915_READ(reg);
139ccd3f 4266 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 4267 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 4268 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4269 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4270 temp |= snb_b_fdi_train_param[j/2];
4271 temp |= FDI_COMPOSITE_SYNC;
4272 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4273
139ccd3f
JB
4274 I915_WRITE(FDI_RX_MISC(pipe),
4275 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4276
139ccd3f 4277 reg = FDI_RX_CTL(pipe);
357555c0 4278 temp = I915_READ(reg);
139ccd3f
JB
4279 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4280 temp |= FDI_COMPOSITE_SYNC;
4281 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4282
139ccd3f
JB
4283 POSTING_READ(reg);
4284 udelay(1); /* should be 0.5us */
357555c0 4285
139ccd3f
JB
4286 for (i = 0; i < 4; i++) {
4287 reg = FDI_RX_IIR(pipe);
4288 temp = I915_READ(reg);
4289 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4290
139ccd3f
JB
4291 if (temp & FDI_RX_BIT_LOCK ||
4292 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4293 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4294 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4295 i);
4296 break;
4297 }
4298 udelay(1); /* should be 0.5us */
4299 }
4300 if (i == 4) {
4301 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4302 continue;
4303 }
357555c0 4304
139ccd3f 4305 /* Train 2 */
357555c0
JB
4306 reg = FDI_TX_CTL(pipe);
4307 temp = I915_READ(reg);
139ccd3f
JB
4308 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4309 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4310 I915_WRITE(reg, temp);
4311
4312 reg = FDI_RX_CTL(pipe);
4313 temp = I915_READ(reg);
4314 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4315 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4316 I915_WRITE(reg, temp);
4317
4318 POSTING_READ(reg);
139ccd3f 4319 udelay(2); /* should be 1.5us */
357555c0 4320
139ccd3f
JB
4321 for (i = 0; i < 4; i++) {
4322 reg = FDI_RX_IIR(pipe);
4323 temp = I915_READ(reg);
4324 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4325
139ccd3f
JB
4326 if (temp & FDI_RX_SYMBOL_LOCK ||
4327 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4328 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4329 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4330 i);
4331 goto train_done;
4332 }
4333 udelay(2); /* should be 1.5us */
357555c0 4334 }
139ccd3f
JB
4335 if (i == 4)
4336 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4337 }
357555c0 4338
139ccd3f 4339train_done:
357555c0
JB
4340 DRM_DEBUG_KMS("FDI train done.\n");
4341}
4342
88cefb6c 4343static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4344{
88cefb6c 4345 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4346 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4347 int pipe = intel_crtc->pipe;
f0f59a00
VS
4348 i915_reg_t reg;
4349 u32 temp;
c64e311e 4350
c98e9dcf 4351 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4352 reg = FDI_RX_CTL(pipe);
4353 temp = I915_READ(reg);
627eb5a3 4354 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4355 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4356 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4357 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4358
4359 POSTING_READ(reg);
c98e9dcf
JB
4360 udelay(200);
4361
4362 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4363 temp = I915_READ(reg);
4364 I915_WRITE(reg, temp | FDI_PCDCLK);
4365
4366 POSTING_READ(reg);
c98e9dcf
JB
4367 udelay(200);
4368
20749730
PZ
4369 /* Enable CPU FDI TX PLL, always on for Ironlake */
4370 reg = FDI_TX_CTL(pipe);
4371 temp = I915_READ(reg);
4372 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4373 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4374
20749730
PZ
4375 POSTING_READ(reg);
4376 udelay(100);
6be4a607 4377 }
0e23b99d
JB
4378}
4379
88cefb6c
SV
4380static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4381{
4382 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4383 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4384 int pipe = intel_crtc->pipe;
f0f59a00
VS
4385 i915_reg_t reg;
4386 u32 temp;
88cefb6c
SV
4387
4388 /* Switch from PCDclk to Rawclk */
4389 reg = FDI_RX_CTL(pipe);
4390 temp = I915_READ(reg);
4391 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4392
4393 /* Disable CPU FDI TX PLL */
4394 reg = FDI_TX_CTL(pipe);
4395 temp = I915_READ(reg);
4396 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4397
4398 POSTING_READ(reg);
4399 udelay(100);
4400
4401 reg = FDI_RX_CTL(pipe);
4402 temp = I915_READ(reg);
4403 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4404
4405 /* Wait for the clocks to turn off. */
4406 POSTING_READ(reg);
4407 udelay(100);
4408}
4409
0fc932b8
JB
4410static void ironlake_fdi_disable(struct drm_crtc *crtc)
4411{
4412 struct drm_device *dev = crtc->dev;
fac5e23e 4413 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4415 int pipe = intel_crtc->pipe;
f0f59a00
VS
4416 i915_reg_t reg;
4417 u32 temp;
0fc932b8
JB
4418
4419 /* disable CPU FDI tx and PCH FDI rx */
4420 reg = FDI_TX_CTL(pipe);
4421 temp = I915_READ(reg);
4422 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4423 POSTING_READ(reg);
4424
4425 reg = FDI_RX_CTL(pipe);
4426 temp = I915_READ(reg);
4427 temp &= ~(0x7 << 16);
dfd07d72 4428 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4429 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4430
4431 POSTING_READ(reg);
4432 udelay(100);
4433
4434 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4435 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4436 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4437
4438 /* still set train pattern 1 */
4439 reg = FDI_TX_CTL(pipe);
4440 temp = I915_READ(reg);
4441 temp &= ~FDI_LINK_TRAIN_NONE;
4442 temp |= FDI_LINK_TRAIN_PATTERN_1;
4443 I915_WRITE(reg, temp);
4444
4445 reg = FDI_RX_CTL(pipe);
4446 temp = I915_READ(reg);
6e266956 4447 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4448 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4449 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4450 } else {
4451 temp &= ~FDI_LINK_TRAIN_NONE;
4452 temp |= FDI_LINK_TRAIN_PATTERN_1;
4453 }
4454 /* BPC in FDI rx is consistent with that in PIPECONF */
4455 temp &= ~(0x07 << 16);
dfd07d72 4456 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4457 I915_WRITE(reg, temp);
4458
4459 POSTING_READ(reg);
4460 udelay(100);
4461}
4462
49d73912 4463bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93 4464{
fa05887a
SV
4465 struct drm_crtc *crtc;
4466 bool cleanup_done;
4467
4468 drm_for_each_crtc(crtc, &dev_priv->drm) {
4469 struct drm_crtc_commit *commit;
4470 spin_lock(&crtc->commit_lock);
4471 commit = list_first_entry_or_null(&crtc->commit_list,
4472 struct drm_crtc_commit, commit_entry);
4473 cleanup_done = commit ?
4474 try_wait_for_completion(&commit->cleanup_done) : true;
4475 spin_unlock(&crtc->commit_lock);
4476
4477 if (cleanup_done)
5dce5b93
CW
4478 continue;
4479
fa05887a 4480 drm_crtc_wait_one_vblank(crtc);
5dce5b93
CW
4481
4482 return true;
4483 }
4484
4485 return false;
4486}
4487
b7076546 4488void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4489{
4490 u32 temp;
4491
4492 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4493
4494 mutex_lock(&dev_priv->sb_lock);
4495
4496 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4497 temp |= SBI_SSCCTL_DISABLE;
4498 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4499
4500 mutex_unlock(&dev_priv->sb_lock);
4501}
4502
e615efe4 4503/* Program iCLKIP clock to the desired frequency */
0dcdc382 4504static void lpt_program_iclkip(struct intel_crtc *crtc)
e615efe4 4505{
0dcdc382
ACO
4506 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4507 int clock = crtc->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4508 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4509 u32 temp;
4510
060f02d8 4511 lpt_disable_iclkip(dev_priv);
e615efe4 4512
64b46a06
VS
4513 /* The iCLK virtual clock root frequency is in MHz,
4514 * but the adjusted_mode->crtc_clock in in KHz. To get the
4515 * divisors, it is necessary to divide one by another, so we
4516 * convert the virtual clock precision to KHz here for higher
4517 * precision.
4518 */
4519 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4520 u32 iclk_virtual_root_freq = 172800 * 1000;
4521 u32 iclk_pi_range = 64;
64b46a06 4522 u32 desired_divisor;
e615efe4 4523
64b46a06
VS
4524 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4525 clock << auxdiv);
4526 divsel = (desired_divisor / iclk_pi_range) - 2;
4527 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4528
64b46a06
VS
4529 /*
4530 * Near 20MHz is a corner case which is
4531 * out of range for the 7-bit divisor
4532 */
4533 if (divsel <= 0x7f)
4534 break;
e615efe4
ED
4535 }
4536
4537 /* This should not happen with any sane values */
4538 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4539 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4540 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4541 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4542
4543 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4544 clock,
e615efe4
ED
4545 auxdiv,
4546 divsel,
4547 phasedir,
4548 phaseinc);
4549
060f02d8
VS
4550 mutex_lock(&dev_priv->sb_lock);
4551
e615efe4 4552 /* Program SSCDIVINTPHASE6 */
988d6ee8 4553 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4554 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4555 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4556 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4557 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4558 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4559 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4560 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4561
4562 /* Program SSCAUXDIV */
988d6ee8 4563 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4564 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4565 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4566 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4567
4568 /* Enable modulator and associated divider */
988d6ee8 4569 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4570 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4571 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4572
060f02d8
VS
4573 mutex_unlock(&dev_priv->sb_lock);
4574
e615efe4
ED
4575 /* Wait for initialization time */
4576 udelay(24);
4577
4578 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4579}
4580
8802e5b6
VS
4581int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4582{
4583 u32 divsel, phaseinc, auxdiv;
4584 u32 iclk_virtual_root_freq = 172800 * 1000;
4585 u32 iclk_pi_range = 64;
4586 u32 desired_divisor;
4587 u32 temp;
4588
4589 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4590 return 0;
4591
4592 mutex_lock(&dev_priv->sb_lock);
4593
4594 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4595 if (temp & SBI_SSCCTL_DISABLE) {
4596 mutex_unlock(&dev_priv->sb_lock);
4597 return 0;
4598 }
4599
4600 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4601 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4602 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4603 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4604 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4605
4606 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4607 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4608 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4609
4610 mutex_unlock(&dev_priv->sb_lock);
4611
4612 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4613
4614 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4615 desired_divisor << auxdiv);
4616}
4617
275f01b2
SV
4618static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4619 enum pipe pch_transcoder)
4620{
4621 struct drm_device *dev = crtc->base.dev;
fac5e23e 4622 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4623 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
SV
4624
4625 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4626 I915_READ(HTOTAL(cpu_transcoder)));
4627 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4628 I915_READ(HBLANK(cpu_transcoder)));
4629 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4630 I915_READ(HSYNC(cpu_transcoder)));
4631
4632 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4633 I915_READ(VTOTAL(cpu_transcoder)));
4634 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4635 I915_READ(VBLANK(cpu_transcoder)));
4636 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4637 I915_READ(VSYNC(cpu_transcoder)));
4638 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4639 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4640}
4641
003632d9 4642static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4643{
fac5e23e 4644 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
SV
4645 uint32_t temp;
4646
4647 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4648 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
SV
4649 return;
4650
4651 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4652 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4653
003632d9
ACO
4654 temp &= ~FDI_BC_BIFURCATION_SELECT;
4655 if (enable)
4656 temp |= FDI_BC_BIFURCATION_SELECT;
4657
4658 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
SV
4659 I915_WRITE(SOUTH_CHICKEN1, temp);
4660 POSTING_READ(SOUTH_CHICKEN1);
4661}
4662
4663static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4664{
4665 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
SV
4666
4667 switch (intel_crtc->pipe) {
4668 case PIPE_A:
4669 break;
4670 case PIPE_B:
6e3c9717 4671 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4672 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4673 else
003632d9 4674 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4675
4676 break;
4677 case PIPE_C:
003632d9 4678 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4679
4680 break;
4681 default:
4682 BUG();
4683 }
4684}
4685
f606bc6d
VS
4686/*
4687 * Finds the encoder associated with the given CRTC. This can only be
4688 * used when we know that the CRTC isn't feeding multiple encoders!
4689 */
4690static struct intel_encoder *
5a0b385e
VS
4691intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
4692 const struct intel_crtc_state *crtc_state)
f606bc6d
VS
4693{
4694 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
f606bc6d
VS
4695 const struct drm_connector_state *connector_state;
4696 const struct drm_connector *connector;
4697 struct intel_encoder *encoder = NULL;
4698 int num_encoders = 0;
4699 int i;
4700
5a0b385e 4701 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
f606bc6d
VS
4702 if (connector_state->crtc != &crtc->base)
4703 continue;
4704
4705 encoder = to_intel_encoder(connector_state->best_encoder);
4706 num_encoders++;
4707 }
4708
4709 WARN(num_encoders != 1, "%d encoders for pipe %c\n",
4710 num_encoders, pipe_name(crtc->pipe));
4711
4712 return encoder;
4713}
4714
f67a559d
JB
4715/*
4716 * Enable PCH resources required for PCH ports:
4717 * - PCH PLLs
4718 * - FDI training & RX/TX
4719 * - update transcoder timings
4720 * - DP transcoding bits
4721 * - transcoder
4722 */
5a0b385e
VS
4723static void ironlake_pch_enable(const struct intel_atomic_state *state,
4724 const struct intel_crtc_state *crtc_state)
0e23b99d 4725{
2ce42273 4726 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4cbe4b2b 4727 struct drm_device *dev = crtc->base.dev;
fac5e23e 4728 struct drm_i915_private *dev_priv = to_i915(dev);
4cbe4b2b 4729 int pipe = crtc->pipe;
f0f59a00 4730 u32 temp;
2c07245f 4731
ab9412ba 4732 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4733
fd6b8f43 4734 if (IS_IVYBRIDGE(dev_priv))
4cbe4b2b 4735 ivybridge_update_fdi_bc_bifurcation(crtc);
1fbc0d78 4736
cd986abb
SV
4737 /* Write the TU size bits before fdi link training, so that error
4738 * detection works. */
4739 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4740 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4741
c98e9dcf 4742 /* For PCH output, training FDI link */
dc4a1094 4743 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 4744
3ad8a208
SV
4745 /* We need to program the right clock selection before writing the pixel
4746 * mutliplier into the DPLL. */
6e266956 4747 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4748 u32 sel;
4b645f14 4749
c98e9dcf 4750 temp = I915_READ(PCH_DPLL_SEL);
11887397
SV
4751 temp |= TRANS_DPLL_ENABLE(pipe);
4752 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 4753 if (crtc_state->shared_dpll ==
8106ddbd 4754 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4755 temp |= sel;
4756 else
4757 temp &= ~sel;
c98e9dcf 4758 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4759 }
5eddb70b 4760
3ad8a208
SV
4761 /* XXX: pch pll's can be enabled any time before we enable the PCH
4762 * transcoder, and we actually should do this to not upset any PCH
4763 * transcoder that already use the clock when we share it.
4764 *
4765 * Note that enable_shared_dpll tries to do the right thing, but
4766 * get_shared_dpll unconditionally resets the pll - we need that to have
4767 * the right LVDS enable sequence. */
4cbe4b2b 4768 intel_enable_shared_dpll(crtc);
3ad8a208 4769
d9b6cb56
JB
4770 /* set transcoder timing, panel must allow it */
4771 assert_panel_unlocked(dev_priv, pipe);
4cbe4b2b 4772 ironlake_pch_transcoder_set_timings(crtc, pipe);
8db9d77b 4773
303b81e0 4774 intel_fdi_normal_train(crtc);
5e84e1a4 4775
c98e9dcf 4776 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 4777 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 4778 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 4779 const struct drm_display_mode *adjusted_mode =
2ce42273 4780 &crtc_state->base.adjusted_mode;
dfd07d72 4781 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4782 i915_reg_t reg = TRANS_DP_CTL(pipe);
f67dc6d8
VS
4783 enum port port;
4784
5eddb70b
CW
4785 temp = I915_READ(reg);
4786 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4787 TRANS_DP_SYNC_MASK |
4788 TRANS_DP_BPC_MASK);
e3ef4479 4789 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4790 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4791
9c4edaee 4792 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4793 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4794 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4795 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf 4796
5a0b385e 4797 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
f67dc6d8
VS
4798 WARN_ON(port < PORT_B || port > PORT_D);
4799 temp |= TRANS_DP_PORT_SEL(port);
2c07245f 4800
5eddb70b 4801 I915_WRITE(reg, temp);
6be4a607 4802 }
b52eb4dc 4803
b8a4f404 4804 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4805}
4806
5a0b385e
VS
4807static void lpt_pch_enable(const struct intel_atomic_state *state,
4808 const struct intel_crtc_state *crtc_state)
1507e5bd 4809{
2ce42273 4810 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
0dcdc382 4811 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 4812 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 4813
a2196033 4814 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1507e5bd 4815
8c52b5e8 4816 lpt_program_iclkip(crtc);
1507e5bd 4817
0540e488 4818 /* Set transcoder timing. */
0dcdc382 4819 ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
1507e5bd 4820
937bb610 4821 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4822}
4823
a1520318 4824static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4825{
fac5e23e 4826 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4827 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4828 u32 temp;
4829
4830 temp = I915_READ(dslreg);
4831 udelay(500);
4832 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4833 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4834 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4835 }
4836}
4837
0a59952b
VS
4838/*
4839 * The hardware phase 0.0 refers to the center of the pixel.
4840 * We want to start from the top/left edge which is phase
4841 * -0.5. That matches how the hardware calculates the scaling
4842 * factors (from top-left of the first pixel to bottom-right
4843 * of the last pixel, as opposed to the pixel centers).
4844 *
4845 * For 4:2:0 subsampled chroma planes we obviously have to
4846 * adjust that so that the chroma sample position lands in
4847 * the right spot.
4848 *
4849 * Note that for packed YCbCr 4:2:2 formats there is no way to
4850 * control chroma siting. The hardware simply replicates the
4851 * chroma samples for both of the luma samples, and thus we don't
4852 * actually get the expected MPEG2 chroma siting convention :(
4853 * The same behaviour is observed on pre-SKL platforms as well.
6e8adf6f
VS
4854 *
4855 * Theory behind the formula (note that we ignore sub-pixel
4856 * source coordinates):
4857 * s = source sample position
4858 * d = destination sample position
4859 *
4860 * Downscaling 4:1:
4861 * -0.5
4862 * | 0.0
4863 * | | 1.5 (initial phase)
4864 * | | |
4865 * v v v
4866 * | s | s | s | s |
4867 * | d |
4868 *
4869 * Upscaling 1:4:
4870 * -0.5
4871 * | -0.375 (initial phase)
4872 * | | 0.0
4873 * | | |
4874 * v v v
4875 * | s |
4876 * | d | d | d | d |
0a59952b 4877 */
6e8adf6f 4878u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
0a59952b
VS
4879{
4880 int phase = -0x8000;
4881 u16 trip = 0;
4882
4883 if (chroma_cosited)
4884 phase += (sub - 1) * 0x8000 / sub;
4885
6e8adf6f
VS
4886 phase += scale / (2 * sub);
4887
4888 /*
4889 * Hardware initial phase limited to [-0.5:1.5].
4890 * Since the max hardware scale factor is 3.0, we
4891 * should never actually excdeed 1.0 here.
4892 */
4893 WARN_ON(phase < -0x8000 || phase > 0x18000);
4894
0a59952b
VS
4895 if (phase < 0)
4896 phase = 0x10000 + phase;
4897 else
4898 trip = PS_PHASE_TRIP;
4899
4900 return ((phase >> 2) & PS_PHASE_MASK) | trip;
4901}
4902
86adf9d7
ML
4903static int
4904skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 4905 unsigned int scaler_user, int *scaler_id,
77224cd5
CK
4906 int src_w, int src_h, int dst_w, int dst_h,
4907 bool plane_scaler_check,
4908 uint32_t pixel_format)
a1b2278e 4909{
86adf9d7
ML
4910 struct intel_crtc_scaler_state *scaler_state =
4911 &crtc_state->scaler_state;
4912 struct intel_crtc *intel_crtc =
4913 to_intel_crtc(crtc_state->base.crtc);
7f58cbb1
MK
4914 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4915 const struct drm_display_mode *adjusted_mode =
4916 &crtc_state->base.adjusted_mode;
a1b2278e 4917 int need_scaling;
6156a456 4918
d96a7d2a
VS
4919 /*
4920 * Src coordinates are already rotated by 270 degrees for
4921 * the 90/270 degree plane rotation cases (to match the
4922 * GTT mapping), hence no need to account for rotation here.
4923 */
4924 need_scaling = src_w != dst_w || src_h != dst_h;
a1b2278e 4925
77224cd5
CK
4926 if (plane_scaler_check)
4927 if (pixel_format == DRM_FORMAT_NV12)
4928 need_scaling = true;
4929
e5c05931
SS
4930 if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
4931 need_scaling = true;
4932
7f58cbb1
MK
4933 /*
4934 * Scaling/fitting not supported in IF-ID mode in GEN9+
4935 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
4936 * Once NV12 is enabled, handle it here while allocating scaler
4937 * for NV12.
4938 */
4939 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
4940 need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4941 DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
4942 return -EINVAL;
4943 }
4944
a1b2278e
CK
4945 /*
4946 * if plane is being disabled or scaler is no more required or force detach
4947 * - free scaler binded to this plane/crtc
4948 * - in order to do this, update crtc->scaler_usage
4949 *
4950 * Here scaler state in crtc_state is set free so that
4951 * scaler can be assigned to other user. Actual register
4952 * update to free the scaler is done in plane/panel-fit programming.
4953 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4954 */
86adf9d7 4955 if (force_detach || !need_scaling) {
a1b2278e 4956 if (*scaler_id >= 0) {
86adf9d7 4957 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4958 scaler_state->scalers[*scaler_id].in_use = 0;
4959
86adf9d7
ML
4960 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4961 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4962 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4963 scaler_state->scaler_users);
4964 *scaler_id = -1;
4965 }
4966 return 0;
4967 }
4968
77224cd5 4969 if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
5d794288 4970 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
77224cd5
CK
4971 DRM_DEBUG_KMS("NV12: src dimensions not met\n");
4972 return -EINVAL;
4973 }
4974
a1b2278e
CK
4975 /* range checks */
4976 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
323301af
NM
4977 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4978 (IS_GEN11(dev_priv) &&
4979 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
4980 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
4981 (!IS_GEN11(dev_priv) &&
4982 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4983 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
86adf9d7 4984 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4985 "size is out of scaler range\n",
86adf9d7 4986 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4987 return -EINVAL;
4988 }
4989
86adf9d7
ML
4990 /* mark this plane as a scaler user in crtc_state */
4991 scaler_state->scaler_users |= (1 << scaler_user);
4992 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4993 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4994 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4995 scaler_state->scaler_users);
4996
4997 return 0;
4998}
4999
5000/**
5001 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
5002 *
5003 * @state: crtc's scaler state
86adf9d7
ML
5004 *
5005 * Return
5006 * 0 - scaler_usage updated successfully
5007 * error - requested scaling cannot be supported or other error condition
5008 */
e435d6e5 5009int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 5010{
7c5f93b0 5011 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 5012
e435d6e5 5013 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
77224cd5
CK
5014 &state->scaler_state.scaler_id,
5015 state->pipe_src_w, state->pipe_src_h,
5016 adjusted_mode->crtc_hdisplay,
5017 adjusted_mode->crtc_vdisplay, false, 0);
86adf9d7
ML
5018}
5019
5020/**
5021 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
c38c1455 5022 * @crtc_state: crtc's scaler state
86adf9d7
ML
5023 * @plane_state: atomic plane state to update
5024 *
5025 * Return
5026 * 0 - scaler_usage updated successfully
5027 * error - requested scaling cannot be supported or other error condition
5028 */
da20eabd
ML
5029static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
5030 struct intel_plane_state *plane_state)
86adf9d7
ML
5031{
5032
da20eabd
ML
5033 struct intel_plane *intel_plane =
5034 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
5035 struct drm_framebuffer *fb = plane_state->base.fb;
5036 int ret;
5037
936e71e3 5038 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 5039
86adf9d7
ML
5040 ret = skl_update_scaler(crtc_state, force_detach,
5041 drm_plane_index(&intel_plane->base),
5042 &plane_state->scaler_id,
936e71e3
VS
5043 drm_rect_width(&plane_state->base.src) >> 16,
5044 drm_rect_height(&plane_state->base.src) >> 16,
5045 drm_rect_width(&plane_state->base.dst),
77224cd5
CK
5046 drm_rect_height(&plane_state->base.dst),
5047 fb ? true : false, fb ? fb->format->format : 0);
86adf9d7
ML
5048
5049 if (ret || plane_state->scaler_id < 0)
5050 return ret;
5051
a1b2278e 5052 /* check colorkey */
6ec5bd34 5053 if (plane_state->ckey.flags) {
72660ce0
VS
5054 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
5055 intel_plane->base.base.id,
5056 intel_plane->base.name);
a1b2278e
CK
5057 return -EINVAL;
5058 }
5059
5060 /* Check src format */
438b74a5 5061 switch (fb->format->format) {
86adf9d7
ML
5062 case DRM_FORMAT_RGB565:
5063 case DRM_FORMAT_XBGR8888:
5064 case DRM_FORMAT_XRGB8888:
5065 case DRM_FORMAT_ABGR8888:
5066 case DRM_FORMAT_ARGB8888:
5067 case DRM_FORMAT_XRGB2101010:
5068 case DRM_FORMAT_XBGR2101010:
5069 case DRM_FORMAT_YUYV:
5070 case DRM_FORMAT_YVYU:
5071 case DRM_FORMAT_UYVY:
5072 case DRM_FORMAT_VYUY:
77224cd5 5073 case DRM_FORMAT_NV12:
86adf9d7
ML
5074 break;
5075 default:
72660ce0
VS
5076 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
5077 intel_plane->base.base.id, intel_plane->base.name,
438b74a5 5078 fb->base.id, fb->format->format);
86adf9d7 5079 return -EINVAL;
a1b2278e
CK
5080 }
5081
a1b2278e
CK
5082 return 0;
5083}
5084
e435d6e5
ML
5085static void skylake_scaler_disable(struct intel_crtc *crtc)
5086{
5087 int i;
5088
5089 for (i = 0; i < crtc->num_scalers; i++)
5090 skl_detach_scaler(crtc, i);
5091}
5092
5093static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
5094{
5095 struct drm_device *dev = crtc->base.dev;
fac5e23e 5096 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 5097 int pipe = crtc->pipe;
a1b2278e
CK
5098 struct intel_crtc_scaler_state *scaler_state =
5099 &crtc->config->scaler_state;
5100
6e3c9717 5101 if (crtc->config->pch_pfit.enabled) {
0a59952b 5102 u16 uv_rgb_hphase, uv_rgb_vphase;
6e8adf6f 5103 int pfit_w, pfit_h, hscale, vscale;
a1b2278e
CK
5104 int id;
5105
c3f8ad57 5106 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
a1b2278e 5107 return;
a1b2278e 5108
6e8adf6f
VS
5109 pfit_w = (crtc->config->pch_pfit.size >> 16) & 0xFFFF;
5110 pfit_h = crtc->config->pch_pfit.size & 0xFFFF;
5111
5112 hscale = (crtc->config->pipe_src_w << 16) / pfit_w;
5113 vscale = (crtc->config->pipe_src_h << 16) / pfit_h;
5114
5115 uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
5116 uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
0a59952b 5117
a1b2278e
CK
5118 id = scaler_state->scaler_id;
5119 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
5120 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
0a59952b
VS
5121 I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
5122 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
5123 I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
5124 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
a1b2278e
CK
5125 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
5126 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
bd2e244f
JB
5127 }
5128}
5129
b074cec8
JB
5130static void ironlake_pfit_enable(struct intel_crtc *crtc)
5131{
5132 struct drm_device *dev = crtc->base.dev;
fac5e23e 5133 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
5134 int pipe = crtc->pipe;
5135
6e3c9717 5136 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
5137 /* Force use of hard-coded filter coefficients
5138 * as some pre-programmed values are broken,
5139 * e.g. x201.
5140 */
fd6b8f43 5141 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
5142 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
5143 PF_PIPE_SEL_IVB(pipe));
5144 else
5145 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
5146 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
5147 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
5148 }
5149}
5150
199ea381 5151void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 5152{
199ea381 5153 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
cea165c3 5154 struct drm_device *dev = crtc->base.dev;
fac5e23e 5155 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 5156
24f28450 5157 if (!crtc_state->ips_enabled)
d77e4531
PZ
5158 return;
5159
307e4498
ML
5160 /*
5161 * We can only enable IPS after we enable a plane and wait for a vblank
5162 * This function is called from post_plane_update, which is run after
5163 * a vblank wait.
5164 */
24f28450 5165 WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
51f5a096 5166
8652744b 5167 if (IS_BROADWELL(dev_priv)) {
9f817501 5168 mutex_lock(&dev_priv->pcu_lock);
61843f0e
VS
5169 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
5170 IPS_ENABLE | IPS_PCODE_CONTROL));
9f817501 5171 mutex_unlock(&dev_priv->pcu_lock);
2a114cc1
BW
5172 /* Quoting Art Runyan: "its not safe to expect any particular
5173 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
5174 * mailbox." Moreover, the mailbox may return a bogus state,
5175 * so we need to just enable it and continue on.
2a114cc1
BW
5176 */
5177 } else {
5178 I915_WRITE(IPS_CTL, IPS_ENABLE);
5179 /* The bit only becomes 1 in the next vblank, so this wait here
5180 * is essentially intel_wait_for_vblank. If we don't have this
5181 * and don't wait for vblanks until the end of crtc_enable, then
5182 * the HW state readout code will complain that the expected
5183 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
5184 if (intel_wait_for_register(dev_priv,
5185 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
5186 50))
2a114cc1
BW
5187 DRM_ERROR("Timed out waiting for IPS enable\n");
5188 }
d77e4531
PZ
5189}
5190
199ea381 5191void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 5192{
199ea381 5193 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
d77e4531 5194 struct drm_device *dev = crtc->base.dev;
fac5e23e 5195 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 5196
199ea381 5197 if (!crtc_state->ips_enabled)
d77e4531
PZ
5198 return;
5199
8652744b 5200 if (IS_BROADWELL(dev_priv)) {
9f817501 5201 mutex_lock(&dev_priv->pcu_lock);
2a114cc1 5202 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
9f817501 5203 mutex_unlock(&dev_priv->pcu_lock);
acb3ef0e
ID
5204 /*
5205 * Wait for PCODE to finish disabling IPS. The BSpec specified
5206 * 42ms timeout value leads to occasional timeouts so use 100ms
5207 * instead.
5208 */
b85c1ecf
CW
5209 if (intel_wait_for_register(dev_priv,
5210 IPS_CTL, IPS_ENABLE, 0,
acb3ef0e 5211 100))
23d0b130 5212 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 5213 } else {
2a114cc1 5214 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
5215 POSTING_READ(IPS_CTL);
5216 }
d77e4531
PZ
5217
5218 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 5219 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
5220}
5221
7cac945f 5222static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 5223{
7cac945f 5224 if (intel_crtc->overlay) {
d3eedb1a 5225 struct drm_device *dev = intel_crtc->base.dev;
d3eedb1a
VS
5226
5227 mutex_lock(&dev->struct_mutex);
d3eedb1a 5228 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
5229 mutex_unlock(&dev->struct_mutex);
5230 }
5231
5232 /* Let userspace switch the overlay on again. In most cases userspace
5233 * has to recompute where to put it anyway.
5234 */
5235}
5236
87d4300a
ML
5237/**
5238 * intel_post_enable_primary - Perform operations after enabling primary plane
5239 * @crtc: the CRTC whose primary plane was just enabled
c38c1455 5240 * @new_crtc_state: the enabling state
87d4300a
ML
5241 *
5242 * Performs potentially sleeping operations that must be done after the primary
5243 * plane is enabled, such as updating FBC and IPS. Note that this may be
5244 * called due to an explicit primary plane update, or due to an implicit
5245 * re-enable that is caused when a sprite plane is updated to no longer
5246 * completely hide the primary plane.
5247 */
5248static void
199ea381
ML
5249intel_post_enable_primary(struct drm_crtc *crtc,
5250 const struct intel_crtc_state *new_crtc_state)
a5c4d7bc
VS
5251{
5252 struct drm_device *dev = crtc->dev;
fac5e23e 5253 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
5254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5255 int pipe = intel_crtc->pipe;
a5c4d7bc 5256
f99d7069 5257 /*
87d4300a
ML
5258 * Gen2 reports pipe underruns whenever all planes are disabled.
5259 * So don't enable underrun reporting before at least some planes
5260 * are enabled.
5261 * FIXME: Need to fix the logic to work when we turn off all planes
5262 * but leave the pipe running.
f99d7069 5263 */
5db94019 5264 if (IS_GEN2(dev_priv))
87d4300a
ML
5265 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5266
aca7b684
VS
5267 /* Underruns don't always raise interrupts, so check manually. */
5268 intel_check_cpu_fifo_underruns(dev_priv);
5269 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
5270}
5271
24f28450 5272/* FIXME get rid of this and use pre_plane_update */
87d4300a 5273static void
24f28450 5274intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
a5c4d7bc
VS
5275{
5276 struct drm_device *dev = crtc->dev;
fac5e23e 5277 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
5278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5279 int pipe = intel_crtc->pipe;
a5c4d7bc 5280
87d4300a
ML
5281 /*
5282 * Gen2 reports pipe underruns whenever all planes are disabled.
24f28450 5283 * So disable underrun reporting before all the planes get disabled.
87d4300a 5284 */
5db94019 5285 if (IS_GEN2(dev_priv))
87d4300a 5286 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 5287
24f28450 5288 hsw_disable_ips(to_intel_crtc_state(crtc->state));
2622a081 5289
87d4300a
ML
5290 /*
5291 * Vblank time updates from the shadow to live plane control register
5292 * are blocked if the memory self-refresh mode is active at that
5293 * moment. So to make sure the plane gets truly disabled, disable
5294 * first the self-refresh mode. The self-refresh enable bit in turn
5295 * will be checked/applied by the HW only at the next frame start
5296 * event which is after the vblank start event, so we need to have a
5297 * wait-for-vblank between disabling the plane and the pipe.
5298 */
11a85d6a
VS
5299 if (HAS_GMCH_DISPLAY(dev_priv) &&
5300 intel_set_memory_cxsr(dev_priv, false))
0f0f74bc 5301 intel_wait_for_vblank(dev_priv, pipe);
87d4300a
ML
5302}
5303
24f28450
ML
5304static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5305 const struct intel_crtc_state *new_crtc_state)
5306{
5307 if (!old_crtc_state->ips_enabled)
5308 return false;
5309
5310 if (needs_modeset(&new_crtc_state->base))
5311 return true;
5312
5313 return !new_crtc_state->ips_enabled;
5314}
5315
5316static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5317 const struct intel_crtc_state *new_crtc_state)
5318{
5319 if (!new_crtc_state->ips_enabled)
5320 return false;
5321
5322 if (needs_modeset(&new_crtc_state->base))
5323 return true;
5324
5325 /*
5326 * We can't read out IPS on broadwell, assume the worst and
5327 * forcibly enable IPS on the first fastset.
5328 */
5329 if (new_crtc_state->update_pipe &&
5330 old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5331 return true;
5332
5333 return !old_crtc_state->ips_enabled;
5334}
5335
8e021151
ML
5336static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5337 const struct intel_crtc_state *crtc_state)
5338{
5339 if (!crtc_state->nv12_planes)
5340 return false;
5341
5342 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
5343 return false;
5344
5345 if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
5346 IS_CANNONLAKE(dev_priv))
5347 return true;
5348
5349 return false;
5350}
5351
5a21b665
SV
5352static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5353{
5354 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
c4a4efa9
VS
5355 struct drm_device *dev = crtc->base.dev;
5356 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
5357 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5358 struct intel_crtc_state *pipe_config =
f9a8c149
VS
5359 intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
5360 crtc);
5a21b665 5361 struct drm_plane *primary = crtc->base.primary;
8b69449d
ML
5362 struct drm_plane_state *old_primary_state =
5363 drm_atomic_get_old_plane_state(old_state, primary);
5a21b665 5364
5748b6a1 5365 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665 5366
5a21b665 5367 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5368 intel_update_watermarks(crtc);
5a21b665 5369
24f28450
ML
5370 if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5371 hsw_enable_ips(pipe_config);
5372
8b69449d
ML
5373 if (old_primary_state) {
5374 struct drm_plane_state *new_primary_state =
5375 drm_atomic_get_new_plane_state(old_state, primary);
5a21b665
SV
5376
5377 intel_fbc_post_update(crtc);
5378
8b69449d 5379 if (new_primary_state->visible &&
5a21b665 5380 (needs_modeset(&pipe_config->base) ||
8b69449d 5381 !old_primary_state->visible))
199ea381 5382 intel_post_enable_primary(&crtc->base, pipe_config);
5a21b665 5383 }
8e021151
ML
5384
5385 /* Display WA 827 */
5386 if (needs_nv12_wa(dev_priv, old_crtc_state) &&
6deef9b6 5387 !needs_nv12_wa(dev_priv, pipe_config)) {
8e021151 5388 skl_wa_clkgate(dev_priv, crtc->pipe, false);
6deef9b6
VS
5389 skl_wa_528(dev_priv, crtc->pipe, false);
5390 }
5a21b665
SV
5391}
5392
aa5e9b47
ML
5393static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5394 struct intel_crtc_state *pipe_config)
ac21b225 5395{
5c74cd73 5396 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5397 struct drm_device *dev = crtc->base.dev;
fac5e23e 5398 struct drm_i915_private *dev_priv = to_i915(dev);
5c74cd73
ML
5399 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5400 struct drm_plane *primary = crtc->base.primary;
8b69449d
ML
5401 struct drm_plane_state *old_primary_state =
5402 drm_atomic_get_old_plane_state(old_state, primary);
5c74cd73 5403 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5404 struct intel_atomic_state *old_intel_state =
5405 to_intel_atomic_state(old_state);
ac21b225 5406
24f28450
ML
5407 if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5408 hsw_disable_ips(old_crtc_state);
5409
8b69449d
ML
5410 if (old_primary_state) {
5411 struct intel_plane_state *new_primary_state =
f9a8c149
VS
5412 intel_atomic_get_new_plane_state(old_intel_state,
5413 to_intel_plane(primary));
5c74cd73 5414
8b69449d 5415 intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
24f28450
ML
5416 /*
5417 * Gen2 reports pipe underruns whenever all planes are disabled.
5418 * So disable underrun reporting before all the planes get disabled.
5419 */
8b69449d
ML
5420 if (IS_GEN2(dev_priv) && old_primary_state->visible &&
5421 (modeset || !new_primary_state->base.visible))
24f28450 5422 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
5c74cd73 5423 }
852eb00d 5424
8e021151
ML
5425 /* Display WA 827 */
5426 if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
6deef9b6 5427 needs_nv12_wa(dev_priv, pipe_config)) {
8e021151 5428 skl_wa_clkgate(dev_priv, crtc->pipe, true);
6deef9b6
VS
5429 skl_wa_528(dev_priv, crtc->pipe, true);
5430 }
8e021151 5431
5eeb798b
VS
5432 /*
5433 * Vblank time updates from the shadow to live plane control register
5434 * are blocked if the memory self-refresh mode is active at that
5435 * moment. So to make sure the plane gets truly disabled, disable
5436 * first the self-refresh mode. The self-refresh enable bit in turn
5437 * will be checked/applied by the HW only at the next frame start
5438 * event which is after the vblank start event, so we need to have a
5439 * wait-for-vblank between disabling the plane and the pipe.
5440 */
5441 if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
5442 pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5443 intel_wait_for_vblank(dev_priv, crtc->pipe);
92826fcd 5444
ed4a6a7c
MR
5445 /*
5446 * IVB workaround: must disable low power watermarks for at least
5447 * one frame before enabling scaling. LP watermarks can be re-enabled
5448 * when scaling is disabled.
5449 *
5450 * WaCxSRDisabledForSpriteScaling:ivb
5451 */
ddd2b792 5452 if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
0f0f74bc 5453 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5454
5455 /*
5456 * If we're doing a modeset, we're done. No need to do any pre-vblank
5457 * watermark programming here.
5458 */
5459 if (needs_modeset(&pipe_config->base))
5460 return;
5461
5462 /*
5463 * For platforms that support atomic watermarks, program the
5464 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5465 * will be the intermediate values that are safe for both pre- and
5466 * post- vblank; when vblank happens, the 'active' values will be set
5467 * to the final 'target' values and we'll do this again to get the
5468 * optimal watermarks. For gen9+ platforms, the values we program here
5469 * will be the final target values which will get automatically latched
5470 * at vblank time; no further programming will be necessary.
5471 *
5472 * If a platform hasn't been transitioned to atomic watermarks yet,
5473 * we'll continue to update watermarks the old way, if flags tell
5474 * us to.
5475 */
5476 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5477 dev_priv->display.initial_watermarks(old_intel_state,
5478 pipe_config);
caed361d 5479 else if (pipe_config->update_wm_pre)
432081bc 5480 intel_update_watermarks(crtc);
ac21b225
ML
5481}
5482
d032ffa0 5483static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5484{
5485 struct drm_device *dev = crtc->dev;
5486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5487 struct drm_plane *p;
87d4300a
ML
5488 int pipe = intel_crtc->pipe;
5489
7cac945f 5490 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5491
d032ffa0 5492 drm_for_each_plane_mask(p, dev, plane_mask)
282dbf9b 5493 to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
f98551ae 5494
f99d7069
SV
5495 /*
5496 * FIXME: Once we grow proper nuclear flip support out of this we need
5497 * to compute the mask of flip planes precisely. For the time being
5498 * consider this a flip to a NULL plane.
5499 */
5748b6a1 5500 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5501}
5502
fb1c98b1 5503static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5504 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5505 struct drm_atomic_state *old_state)
5506{
aa5e9b47 5507 struct drm_connector_state *conn_state;
fb1c98b1
ML
5508 struct drm_connector *conn;
5509 int i;
5510
aa5e9b47 5511 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5512 struct intel_encoder *encoder =
5513 to_intel_encoder(conn_state->best_encoder);
5514
5515 if (conn_state->crtc != crtc)
5516 continue;
5517
5518 if (encoder->pre_pll_enable)
fd6bbda9 5519 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5520 }
5521}
5522
5523static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5524 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5525 struct drm_atomic_state *old_state)
5526{
aa5e9b47 5527 struct drm_connector_state *conn_state;
fb1c98b1
ML
5528 struct drm_connector *conn;
5529 int i;
5530
aa5e9b47 5531 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5532 struct intel_encoder *encoder =
5533 to_intel_encoder(conn_state->best_encoder);
5534
5535 if (conn_state->crtc != crtc)
5536 continue;
5537
5538 if (encoder->pre_enable)
fd6bbda9 5539 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5540 }
5541}
5542
5543static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5544 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5545 struct drm_atomic_state *old_state)
5546{
aa5e9b47 5547 struct drm_connector_state *conn_state;
fb1c98b1
ML
5548 struct drm_connector *conn;
5549 int i;
5550
aa5e9b47 5551 for_each_new_connector_in_state(old_state, conn, conn_state, i) {
fb1c98b1
ML
5552 struct intel_encoder *encoder =
5553 to_intel_encoder(conn_state->best_encoder);
5554
5555 if (conn_state->crtc != crtc)
5556 continue;
5557
fd6bbda9 5558 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5559 intel_opregion_notify_encoder(encoder, true);
5560 }
5561}
5562
5563static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5564 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5565 struct drm_atomic_state *old_state)
5566{
5567 struct drm_connector_state *old_conn_state;
5568 struct drm_connector *conn;
5569 int i;
5570
aa5e9b47 5571 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5572 struct intel_encoder *encoder =
5573 to_intel_encoder(old_conn_state->best_encoder);
5574
5575 if (old_conn_state->crtc != crtc)
5576 continue;
5577
5578 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5579 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5580 }
5581}
5582
5583static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5584 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5585 struct drm_atomic_state *old_state)
5586{
5587 struct drm_connector_state *old_conn_state;
5588 struct drm_connector *conn;
5589 int i;
5590
aa5e9b47 5591 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5592 struct intel_encoder *encoder =
5593 to_intel_encoder(old_conn_state->best_encoder);
5594
5595 if (old_conn_state->crtc != crtc)
5596 continue;
5597
5598 if (encoder->post_disable)
fd6bbda9 5599 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5600 }
5601}
5602
5603static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5604 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5605 struct drm_atomic_state *old_state)
5606{
5607 struct drm_connector_state *old_conn_state;
5608 struct drm_connector *conn;
5609 int i;
5610
aa5e9b47 5611 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
fb1c98b1
ML
5612 struct intel_encoder *encoder =
5613 to_intel_encoder(old_conn_state->best_encoder);
5614
5615 if (old_conn_state->crtc != crtc)
5616 continue;
5617
5618 if (encoder->post_pll_disable)
fd6bbda9 5619 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5620 }
5621}
5622
4a806558
ML
5623static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5624 struct drm_atomic_state *old_state)
f67a559d 5625{
4a806558 5626 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5627 struct drm_device *dev = crtc->dev;
fac5e23e 5628 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5630 int pipe = intel_crtc->pipe;
ccf010fb
ML
5631 struct intel_atomic_state *old_intel_state =
5632 to_intel_atomic_state(old_state);
f67a559d 5633
53d9f4e9 5634 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5635 return;
5636
b2c0593a
VS
5637 /*
5638 * Sometimes spurious CPU pipe underruns happen during FDI
5639 * training, at least with VGA+HDMI cloning. Suppress them.
5640 *
5641 * On ILK we get an occasional spurious CPU pipe underruns
5642 * between eDP port A enable and vdd enable. Also PCH port
5643 * enable seems to result in the occasional CPU pipe underrun.
5644 *
5645 * Spurious PCH underruns also occur during PCH enabling.
5646 */
2b5b6312
VS
5647 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5648 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca 5649
6e3c9717 5650 if (intel_crtc->config->has_pch_encoder)
b14b1055
SV
5651 intel_prepare_shared_dpll(intel_crtc);
5652
37a5650b 5653 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5654 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
SV
5655
5656 intel_set_pipe_timings(intel_crtc);
bc58be60 5657 intel_set_pipe_src_size(intel_crtc);
29407aab 5658
6e3c9717 5659 if (intel_crtc->config->has_pch_encoder) {
29407aab 5660 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5661 &intel_crtc->config->fdi_m_n, NULL);
29407aab
SV
5662 }
5663
5664 ironlake_set_pipeconf(crtc);
5665
f67a559d 5666 intel_crtc->active = true;
8664281b 5667
fd6bbda9 5668 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5669
6e3c9717 5670 if (intel_crtc->config->has_pch_encoder) {
fff367c7
SV
5671 /* Note: FDI PLL enabling _must_ be done before we enable the
5672 * cpu pipes, hence this is separate from all the other fdi/pch
5673 * enabling. */
88cefb6c 5674 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
SV
5675 } else {
5676 assert_fdi_tx_disabled(dev_priv, pipe);
5677 assert_fdi_rx_disabled(dev_priv, pipe);
5678 }
f67a559d 5679
b074cec8 5680 ironlake_pfit_enable(intel_crtc);
f67a559d 5681
9c54c0dd
JB
5682 /*
5683 * On ILK+ LUT must be loaded before the pipe is running but with
5684 * clocks enabled
5685 */
b95c5321 5686 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5687
1d5bf5d9 5688 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5689 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
4972f70a 5690 intel_enable_pipe(pipe_config);
f67a559d 5691
6e3c9717 5692 if (intel_crtc->config->has_pch_encoder)
5a0b385e 5693 ironlake_pch_enable(old_intel_state, pipe_config);
c98e9dcf 5694
f9b61ff6
SV
5695 assert_vblank_disabled(crtc);
5696 drm_crtc_vblank_on(crtc);
5697
fd6bbda9 5698 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5699
6e266956 5700 if (HAS_PCH_CPT(dev_priv))
a1520318 5701 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c 5702
ea80a661
VS
5703 /*
5704 * Must wait for vblank to avoid spurious PCH FIFO underruns.
5705 * And a second vblank wait is needed at least on ILK with
5706 * some interlaced HDMI modes. Let's do the double wait always
5707 * in case there are more corner cases we don't know about.
5708 */
5709 if (intel_crtc->config->has_pch_encoder) {
5710 intel_wait_for_vblank(dev_priv, pipe);
0f0f74bc 5711 intel_wait_for_vblank(dev_priv, pipe);
ea80a661 5712 }
b2c0593a 5713 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5714 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5715}
5716
42db64ef
PZ
5717/* IPS only exists on ULT machines and is tied to pipe A. */
5718static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5719{
50a0bc90 5720 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5721}
5722
ed69cd40
ID
5723static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
5724 enum pipe pipe, bool apply)
5725{
5726 u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
5727 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
5728
5729 if (apply)
5730 val |= mask;
5731 else
5732 val &= ~mask;
5733
5734 I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
5735}
5736
c3cc39c5
MK
5737static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
5738{
5739 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5740 enum pipe pipe = crtc->pipe;
5741 uint32_t val;
5742
5743 val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
5744
5745 /* Program B credit equally to all pipes */
5746 val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
5747
5748 I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
5749}
5750
4a806558
ML
5751static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5752 struct drm_atomic_state *old_state)
4f771f10 5753{
4a806558 5754 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5755 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5757 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5758 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5759 struct intel_atomic_state *old_intel_state =
5760 to_intel_atomic_state(old_state);
ed69cd40 5761 bool psl_clkgate_wa;
e16a3750 5762 u32 pipe_chicken;
4f771f10 5763
53d9f4e9 5764 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5765 return;
5766
fd6bbda9 5767 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5768
8106ddbd 5769 if (intel_crtc->config->shared_dpll)
df8ad70c
SV
5770 intel_enable_shared_dpll(intel_crtc);
5771
c27e917e
PZ
5772 if (INTEL_GEN(dev_priv) >= 11)
5773 icl_map_plls_to_ports(crtc, pipe_config, old_state);
5774
c8af5274
PZ
5775 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5776
37a5650b 5777 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5778 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5779
d7edc4e5 5780 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5781 intel_set_pipe_timings(intel_crtc);
5782
bc58be60 5783 intel_set_pipe_src_size(intel_crtc);
229fca97 5784
4d1de975
JN
5785 if (cpu_transcoder != TRANSCODER_EDP &&
5786 !transcoder_is_dsi(cpu_transcoder)) {
5787 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5788 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5789 }
5790
6e3c9717 5791 if (intel_crtc->config->has_pch_encoder) {
229fca97 5792 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5793 &intel_crtc->config->fdi_m_n, NULL);
229fca97
SV
5794 }
5795
d7edc4e5 5796 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5797 haswell_set_pipeconf(crtc);
5798
391bf048 5799 haswell_set_pipemisc(crtc);
229fca97 5800
b95c5321 5801 intel_color_set_csc(&pipe_config->base);
229fca97 5802
4f771f10 5803 intel_crtc->active = true;
8664281b 5804
ed69cd40
ID
5805 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
5806 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
5807 intel_crtc->config->pch_pfit.enabled;
5808 if (psl_clkgate_wa)
5809 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
5810
6315b5d3 5811 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5812 skylake_pfit_enable(intel_crtc);
ff6d9f55 5813 else
1c132b44 5814 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5815
5816 /*
5817 * On ILK+ LUT must be loaded before the pipe is running but with
5818 * clocks enabled
5819 */
b95c5321 5820 intel_color_load_luts(&pipe_config->base);
4f771f10 5821
e16a3750
VK
5822 /*
5823 * Display WA #1153: enable hardware to bypass the alpha math
5824 * and rounding for per-pixel values 00 and 0xff
5825 */
5826 if (INTEL_GEN(dev_priv) >= 11) {
5827 pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
5828 if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
5829 I915_WRITE_FW(PIPE_CHICKEN(pipe),
5830 pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
5831 }
5832
3dc38eea 5833 intel_ddi_set_pipe_settings(pipe_config);
d7edc4e5 5834 if (!transcoder_is_dsi(cpu_transcoder))
3dc38eea 5835 intel_ddi_enable_transcoder_func(pipe_config);
4f771f10 5836
1d5bf5d9 5837 if (dev_priv->display.initial_watermarks != NULL)
3125d39f 5838 dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
4d1de975 5839
c3cc39c5
MK
5840 if (INTEL_GEN(dev_priv) >= 11)
5841 icl_pipe_mbus_enable(intel_crtc);
5842
4d1de975 5843 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5844 if (!transcoder_is_dsi(cpu_transcoder))
4972f70a 5845 intel_enable_pipe(pipe_config);
42db64ef 5846
6e3c9717 5847 if (intel_crtc->config->has_pch_encoder)
5a0b385e 5848 lpt_pch_enable(old_intel_state, pipe_config);
4f771f10 5849
0037071d 5850 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
3dc38eea 5851 intel_ddi_set_vc_payload_alloc(pipe_config, true);
0e32b39c 5852
f9b61ff6
SV
5853 assert_vblank_disabled(crtc);
5854 drm_crtc_vblank_on(crtc);
5855
fd6bbda9 5856 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5857
ed69cd40
ID
5858 if (psl_clkgate_wa) {
5859 intel_wait_for_vblank(dev_priv, pipe);
5860 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
5861 }
5862
e4916946
PZ
5863 /* If we change the relative order between pipe/planes enabling, we need
5864 * to change the workaround. */
99d736a2 5865 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5866 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5867 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5868 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5869 }
4f771f10
PZ
5870}
5871
bfd16b2a 5872static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
SV
5873{
5874 struct drm_device *dev = crtc->base.dev;
fac5e23e 5875 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
SV
5876 int pipe = crtc->pipe;
5877
5878 /* To avoid upsetting the power well on haswell only disable the pfit if
5879 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5880 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
SV
5881 I915_WRITE(PF_CTL(pipe), 0);
5882 I915_WRITE(PF_WIN_POS(pipe), 0);
5883 I915_WRITE(PF_WIN_SZ(pipe), 0);
5884 }
5885}
5886
4a806558
ML
5887static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5888 struct drm_atomic_state *old_state)
6be4a607 5889{
4a806558 5890 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5891 struct drm_device *dev = crtc->dev;
fac5e23e 5892 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5894 int pipe = intel_crtc->pipe;
b52eb4dc 5895
b2c0593a
VS
5896 /*
5897 * Sometimes spurious CPU pipe underruns happen when the
5898 * pipe is already disabled, but FDI RX/TX is still enabled.
5899 * Happens at least with VGA+HDMI cloning. Suppress them.
5900 */
2b5b6312
VS
5901 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5902 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5903
fd6bbda9 5904 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5905
f9b61ff6
SV
5906 drm_crtc_vblank_off(crtc);
5907 assert_vblank_disabled(crtc);
5908
4972f70a 5909 intel_disable_pipe(old_crtc_state);
32f9d658 5910
bfd16b2a 5911 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5912
b2c0593a 5913 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5914 ironlake_fdi_disable(crtc);
5915
fd6bbda9 5916 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5917
6e3c9717 5918 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5919 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5920
6e266956 5921 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5922 i915_reg_t reg;
5923 u32 temp;
5924
d925c59a
SV
5925 /* disable TRANS_DP_CTL */
5926 reg = TRANS_DP_CTL(pipe);
5927 temp = I915_READ(reg);
5928 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5929 TRANS_DP_PORT_SEL_MASK);
5930 temp |= TRANS_DP_PORT_SEL_NONE;
5931 I915_WRITE(reg, temp);
5932
5933 /* disable DPLL_SEL */
5934 temp = I915_READ(PCH_DPLL_SEL);
11887397 5935 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5936 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5937 }
e3421a18 5938
d925c59a
SV
5939 ironlake_fdi_pll_disable(intel_crtc);
5940 }
81b088ca 5941
b2c0593a 5942 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5943 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5944}
1b3c7a47 5945
4a806558
ML
5946static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5947 struct drm_atomic_state *old_state)
ee7b9f93 5948{
4a806558 5949 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5950 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
24a28179 5952 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
ee7b9f93 5953
fd6bbda9 5954 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5955
f9b61ff6
SV
5956 drm_crtc_vblank_off(crtc);
5957 assert_vblank_disabled(crtc);
5958
4d1de975 5959 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5960 if (!transcoder_is_dsi(cpu_transcoder))
4972f70a 5961 intel_disable_pipe(old_crtc_state);
4f771f10 5962
24a28179
ID
5963 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
5964 intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
a4bf214f 5965
d7edc4e5 5966 if (!transcoder_is_dsi(cpu_transcoder))
90c3e219 5967 intel_ddi_disable_transcoder_func(old_crtc_state);
4f771f10 5968
6315b5d3 5969 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5970 skylake_scaler_disable(intel_crtc);
ff6d9f55 5971 else
bfd16b2a 5972 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5973
fd6bbda9 5974 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
c27e917e
PZ
5975
5976 if (INTEL_GEN(dev_priv) >= 11)
5977 icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
4f771f10
PZ
5978}
5979
2dd24552
JB
5980static void i9xx_pfit_enable(struct intel_crtc *crtc)
5981{
5982 struct drm_device *dev = crtc->base.dev;
fac5e23e 5983 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5984 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5985
681a8504 5986 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5987 return;
5988
2dd24552 5989 /*
c0b03411
SV
5990 * The panel fitter should only be adjusted whilst the pipe is disabled,
5991 * according to register description and PRM.
2dd24552 5992 */
c0b03411
SV
5993 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5994 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5995
b074cec8
JB
5996 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5997 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
SV
5998
5999 /* Border color in case we don't scale up to the full screen. Black by
6000 * default, change to something else for debugging. */
6001 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
6002}
6003
a9b84b44
MK
6004bool intel_port_is_combophy(struct drm_i915_private *dev_priv, enum port port)
6005{
6006 if (port == PORT_NONE)
6007 return false;
6008
6009 if (IS_ICELAKE(dev_priv))
6010 return port <= PORT_B;
6011
6012 return false;
6013}
6014
ac213c1b
PZ
6015bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
6016{
6017 if (IS_ICELAKE(dev_priv))
6018 return port >= PORT_C && port <= PORT_F;
6019
6020 return false;
6021}
6022
6023enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
6024{
6025 if (!intel_port_is_tc(dev_priv, port))
6026 return PORT_TC_NONE;
6027
6028 return port - PORT_C;
6029}
6030
79f255a0 6031enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
6032{
6033 switch (port) {
6034 case PORT_A:
6331a704 6035 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 6036 case PORT_B:
6331a704 6037 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 6038 case PORT_C:
6331a704 6039 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 6040 case PORT_D:
6331a704 6041 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 6042 case PORT_E:
6331a704 6043 return POWER_DOMAIN_PORT_DDI_E_LANES;
9787e835
RV
6044 case PORT_F:
6045 return POWER_DOMAIN_PORT_DDI_F_LANES;
d05410f9 6046 default:
b9fec167 6047 MISSING_CASE(port);
d05410f9
DA
6048 return POWER_DOMAIN_PORT_OTHER;
6049 }
6050}
6051
d8fc70b7
ACO
6052static u64 get_crtc_power_domains(struct drm_crtc *crtc,
6053 struct intel_crtc_state *crtc_state)
77d22dca 6054{
319be8ae 6055 struct drm_device *dev = crtc->dev;
37255d8d 6056 struct drm_i915_private *dev_priv = to_i915(dev);
74bff5f9 6057 struct drm_encoder *encoder;
319be8ae
ID
6058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6059 enum pipe pipe = intel_crtc->pipe;
d8fc70b7 6060 u64 mask;
74bff5f9 6061 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 6062
74bff5f9 6063 if (!crtc_state->base.active)
292b990e
ML
6064 return 0;
6065
17bd6e66
ID
6066 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
6067 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
6068 if (crtc_state->pch_pfit.enabled ||
6069 crtc_state->pch_pfit.force_thru)
d8fc70b7 6070 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 6071
74bff5f9
ML
6072 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
6073 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
6074
79f255a0 6075 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 6076 }
319be8ae 6077
37255d8d 6078 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
17bd6e66 6079 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
37255d8d 6080
15e7ec29 6081 if (crtc_state->shared_dpll)
d8fc70b7 6082 mask |= BIT_ULL(POWER_DOMAIN_PLLS);
15e7ec29 6083
77d22dca
ID
6084 return mask;
6085}
6086
d2d15016 6087static u64
74bff5f9
ML
6088modeset_get_crtc_power_domains(struct drm_crtc *crtc,
6089 struct intel_crtc_state *crtc_state)
77d22dca 6090{
fac5e23e 6091 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
6092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6093 enum intel_display_power_domain domain;
d8fc70b7 6094 u64 domains, new_domains, old_domains;
77d22dca 6095
292b990e 6096 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
6097 intel_crtc->enabled_power_domains = new_domains =
6098 get_crtc_power_domains(crtc, crtc_state);
77d22dca 6099
5a21b665 6100 domains = new_domains & ~old_domains;
292b990e
ML
6101
6102 for_each_power_domain(domain, domains)
6103 intel_display_power_get(dev_priv, domain);
6104
5a21b665 6105 return old_domains & ~new_domains;
292b990e
ML
6106}
6107
6108static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 6109 u64 domains)
292b990e
ML
6110{
6111 enum intel_display_power_domain domain;
6112
6113 for_each_power_domain(domain, domains)
6114 intel_display_power_put(dev_priv, domain);
6115}
77d22dca 6116
7ff89ca2
VS
6117static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6118 struct drm_atomic_state *old_state)
adafdc6f 6119{
ff32c54e
VS
6120 struct intel_atomic_state *old_intel_state =
6121 to_intel_atomic_state(old_state);
7ff89ca2
VS
6122 struct drm_crtc *crtc = pipe_config->base.crtc;
6123 struct drm_device *dev = crtc->dev;
6124 struct drm_i915_private *dev_priv = to_i915(dev);
6125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6126 int pipe = intel_crtc->pipe;
adafdc6f 6127
7ff89ca2
VS
6128 if (WARN_ON(intel_crtc->active))
6129 return;
adafdc6f 6130
7ff89ca2
VS
6131 if (intel_crtc_has_dp_encoder(intel_crtc->config))
6132 intel_dp_set_m_n(intel_crtc, M1_N1);
b2045352 6133
7ff89ca2
VS
6134 intel_set_pipe_timings(intel_crtc);
6135 intel_set_pipe_src_size(intel_crtc);
b2045352 6136
7ff89ca2
VS
6137 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
6138 struct drm_i915_private *dev_priv = to_i915(dev);
560a7ae4 6139
7ff89ca2
VS
6140 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6141 I915_WRITE(CHV_CANVAS(pipe), 0);
560a7ae4
DL
6142 }
6143
7ff89ca2 6144 i9xx_set_pipeconf(intel_crtc);
560a7ae4 6145
c59d2da8
RS
6146 intel_color_set_csc(&pipe_config->base);
6147
7ff89ca2 6148 intel_crtc->active = true;
92891e45 6149
7ff89ca2 6150 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 6151
7ff89ca2 6152 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
5f199dfa 6153
7ff89ca2
VS
6154 if (IS_CHERRYVIEW(dev_priv)) {
6155 chv_prepare_pll(intel_crtc, intel_crtc->config);
6156 chv_enable_pll(intel_crtc, intel_crtc->config);
6157 } else {
6158 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6159 vlv_enable_pll(intel_crtc, intel_crtc->config);
5f199dfa
VS
6160 }
6161
7ff89ca2 6162 intel_encoders_pre_enable(crtc, pipe_config, old_state);
5f199dfa 6163
7ff89ca2 6164 i9xx_pfit_enable(intel_crtc);
89b3c3c7 6165
7ff89ca2 6166 intel_color_load_luts(&pipe_config->base);
89b3c3c7 6167
ff32c54e
VS
6168 dev_priv->display.initial_watermarks(old_intel_state,
6169 pipe_config);
4972f70a 6170 intel_enable_pipe(pipe_config);
7ff89ca2
VS
6171
6172 assert_vblank_disabled(crtc);
6173 drm_crtc_vblank_on(crtc);
89b3c3c7 6174
7ff89ca2 6175 intel_encoders_enable(crtc, pipe_config, old_state);
89b3c3c7
ACO
6176}
6177
7ff89ca2 6178static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
2b73001e 6179{
7ff89ca2
VS
6180 struct drm_device *dev = crtc->base.dev;
6181 struct drm_i915_private *dev_priv = to_i915(dev);
83d7c81f 6182
7ff89ca2
VS
6183 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6184 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
2b73001e
VS
6185}
6186
7ff89ca2
VS
6187static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6188 struct drm_atomic_state *old_state)
2b73001e 6189{
04548cba
VS
6190 struct intel_atomic_state *old_intel_state =
6191 to_intel_atomic_state(old_state);
7ff89ca2
VS
6192 struct drm_crtc *crtc = pipe_config->base.crtc;
6193 struct drm_device *dev = crtc->dev;
6194 struct drm_i915_private *dev_priv = to_i915(dev);
6195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6196 enum pipe pipe = intel_crtc->pipe;
2b73001e 6197
7ff89ca2
VS
6198 if (WARN_ON(intel_crtc->active))
6199 return;
2b73001e 6200
7ff89ca2 6201 i9xx_set_pll_dividers(intel_crtc);
2b73001e 6202
7ff89ca2
VS
6203 if (intel_crtc_has_dp_encoder(intel_crtc->config))
6204 intel_dp_set_m_n(intel_crtc, M1_N1);
83d7c81f 6205
7ff89ca2
VS
6206 intel_set_pipe_timings(intel_crtc);
6207 intel_set_pipe_src_size(intel_crtc);
2b73001e 6208
7ff89ca2 6209 i9xx_set_pipeconf(intel_crtc);
f8437dd1 6210
7ff89ca2 6211 intel_crtc->active = true;
5f199dfa 6212
7ff89ca2
VS
6213 if (!IS_GEN2(dev_priv))
6214 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 6215
7ff89ca2 6216 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f8437dd1 6217
939994da 6218 i9xx_enable_pll(intel_crtc, pipe_config);
f8437dd1 6219
7ff89ca2 6220 i9xx_pfit_enable(intel_crtc);
f8437dd1 6221
7ff89ca2 6222 intel_color_load_luts(&pipe_config->base);
f8437dd1 6223
04548cba
VS
6224 if (dev_priv->display.initial_watermarks != NULL)
6225 dev_priv->display.initial_watermarks(old_intel_state,
6226 intel_crtc->config);
6227 else
6228 intel_update_watermarks(intel_crtc);
4972f70a 6229 intel_enable_pipe(pipe_config);
f8437dd1 6230
7ff89ca2
VS
6231 assert_vblank_disabled(crtc);
6232 drm_crtc_vblank_on(crtc);
f8437dd1 6233
7ff89ca2
VS
6234 intel_encoders_enable(crtc, pipe_config, old_state);
6235}
f8437dd1 6236
7ff89ca2
VS
6237static void i9xx_pfit_disable(struct intel_crtc *crtc)
6238{
6239 struct drm_device *dev = crtc->base.dev;
6240 struct drm_i915_private *dev_priv = to_i915(dev);
f8437dd1 6241
7ff89ca2 6242 if (!crtc->config->gmch_pfit.control)
f8437dd1 6243 return;
f8437dd1 6244
7ff89ca2
VS
6245 assert_pipe_disabled(dev_priv, crtc->pipe);
6246
43031788
CW
6247 DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
6248 I915_READ(PFIT_CONTROL));
7ff89ca2 6249 I915_WRITE(PFIT_CONTROL, 0);
f8437dd1
VK
6250}
6251
7ff89ca2
VS
6252static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6253 struct drm_atomic_state *old_state)
f8437dd1 6254{
7ff89ca2
VS
6255 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6256 struct drm_device *dev = crtc->dev;
6257 struct drm_i915_private *dev_priv = to_i915(dev);
6258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6259 int pipe = intel_crtc->pipe;
d66a2194 6260
d66a2194 6261 /*
7ff89ca2
VS
6262 * On gen2 planes are double buffered but the pipe isn't, so we must
6263 * wait for planes to fully turn off before disabling the pipe.
d66a2194 6264 */
7ff89ca2
VS
6265 if (IS_GEN2(dev_priv))
6266 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 6267
7ff89ca2 6268 intel_encoders_disable(crtc, old_crtc_state, old_state);
d66a2194 6269
7ff89ca2
VS
6270 drm_crtc_vblank_off(crtc);
6271 assert_vblank_disabled(crtc);
d66a2194 6272
4972f70a 6273 intel_disable_pipe(old_crtc_state);
d66a2194 6274
7ff89ca2 6275 i9xx_pfit_disable(intel_crtc);
89b3c3c7 6276
7ff89ca2 6277 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
d66a2194 6278
7ff89ca2
VS
6279 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
6280 if (IS_CHERRYVIEW(dev_priv))
6281 chv_disable_pll(dev_priv, pipe);
6282 else if (IS_VALLEYVIEW(dev_priv))
6283 vlv_disable_pll(dev_priv, pipe);
6284 else
6285 i9xx_disable_pll(intel_crtc);
6286 }
c2e001ef 6287
7ff89ca2 6288 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
89b3c3c7 6289
7ff89ca2
VS
6290 if (!IS_GEN2(dev_priv))
6291 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
6292
6293 if (!dev_priv->display.initial_watermarks)
6294 intel_update_watermarks(intel_crtc);
2ee0da16
VS
6295
6296 /* clock the pipe down to 640x480@60 to potentially save power */
6297 if (IS_I830(dev_priv))
6298 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
6299}
6300
da1d0e26
VS
6301static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
6302 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 6303{
7ff89ca2
VS
6304 struct intel_encoder *encoder;
6305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6306 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6307 enum intel_display_power_domain domain;
b1e01595 6308 struct intel_plane *plane;
d2d15016 6309 u64 domains;
7ff89ca2
VS
6310 struct drm_atomic_state *state;
6311 struct intel_crtc_state *crtc_state;
6312 int ret;
f8437dd1 6313
7ff89ca2
VS
6314 if (!intel_crtc->active)
6315 return;
a8ca4934 6316
b1e01595
VS
6317 for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
6318 const struct intel_plane_state *plane_state =
6319 to_intel_plane_state(plane->base.state);
709e05c3 6320
b1e01595
VS
6321 if (plane_state->base.visible)
6322 intel_plane_disable_noatomic(intel_crtc, plane);
7ff89ca2 6323 }
5d96d8af 6324
7ff89ca2
VS
6325 state = drm_atomic_state_alloc(crtc->dev);
6326 if (!state) {
6327 DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
6328 crtc->base.id, crtc->name);
1c3f7700 6329 return;
7ff89ca2 6330 }
9f7eb31a 6331
da1d0e26 6332 state->acquire_ctx = ctx;
ea61791e 6333
7ff89ca2
VS
6334 /* Everything's already locked, -EDEADLK can't happen. */
6335 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6336 ret = drm_atomic_add_affected_connectors(state, crtc);
9f7eb31a 6337
7ff89ca2 6338 WARN_ON(IS_ERR(crtc_state) || ret);
5d96d8af 6339
7ff89ca2 6340 dev_priv->display.crtc_disable(crtc_state, state);
4a806558 6341
0853695c 6342 drm_atomic_state_put(state);
842e0307 6343
78108b7c
VS
6344 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6345 crtc->base.id, crtc->name);
842e0307
ML
6346
6347 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6348 crtc->state->active = false;
37d9078b 6349 intel_crtc->active = false;
842e0307
ML
6350 crtc->enabled = false;
6351 crtc->state->connector_mask = 0;
6352 crtc->state->encoder_mask = 0;
6353
6354 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6355 encoder->base.crtc = NULL;
6356
58f9c0bc 6357 intel_fbc_disable(intel_crtc);
432081bc 6358 intel_update_watermarks(intel_crtc);
1f7457b1 6359 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6360
6361 domains = intel_crtc->enabled_power_domains;
6362 for_each_power_domain(domain, domains)
6363 intel_display_power_put(dev_priv, domain);
6364 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6365
6366 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
d305e061 6367 dev_priv->min_cdclk[intel_crtc->pipe] = 0;
53e9bf5e 6368 dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
b17d48e2
ML
6369}
6370
6b72d486
ML
6371/*
6372 * turn all crtc's off, but do not adjust state
6373 * This has to be paired with a call to intel_modeset_setup_hw_state.
6374 */
70e0bd74 6375int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6376{
e2c8b870 6377 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6378 struct drm_atomic_state *state;
e2c8b870 6379 int ret;
70e0bd74 6380
e2c8b870
ML
6381 state = drm_atomic_helper_suspend(dev);
6382 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6383 if (ret)
6384 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6385 else
6386 dev_priv->modeset_restore_state = state;
70e0bd74 6387 return ret;
ee7b9f93
JB
6388}
6389
ea5b213a 6390void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6391{
4ef69c7a 6392 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6393
ea5b213a
CW
6394 drm_encoder_cleanup(encoder);
6395 kfree(intel_encoder);
7e7d76c3
JB
6396}
6397
0a91ca29
SV
6398/* Cross check the actual hw state with our own modeset state tracking (and it's
6399 * internal consistency). */
749d98b8
ML
6400static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
6401 struct drm_connector_state *conn_state)
79e53945 6402{
749d98b8 6403 struct intel_connector *connector = to_intel_connector(conn_state->connector);
35dd3c64
ML
6404
6405 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6406 connector->base.base.id,
6407 connector->base.name);
6408
0a91ca29 6409 if (connector->get_hw_state(connector)) {
e85376cb 6410 struct intel_encoder *encoder = connector->encoder;
0a91ca29 6411
749d98b8 6412 I915_STATE_WARN(!crtc_state,
35dd3c64 6413 "connector enabled without attached crtc\n");
0a91ca29 6414
749d98b8 6415 if (!crtc_state)
35dd3c64
ML
6416 return;
6417
749d98b8 6418 I915_STATE_WARN(!crtc_state->active,
35dd3c64
ML
6419 "connector is active, but attached crtc isn't\n");
6420
e85376cb 6421 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6422 return;
6423
e85376cb 6424 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6425 "atomic encoder doesn't match attached encoder\n");
6426
e85376cb 6427 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6428 "attached encoder crtc differs from connector crtc\n");
6429 } else {
749d98b8 6430 I915_STATE_WARN(crtc_state && crtc_state->active,
4d688a2a 6431 "attached crtc is active, but connector isn't\n");
749d98b8 6432 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 6433 "best encoder set without crtc!\n");
0a91ca29 6434 }
79e53945
JB
6435}
6436
08d9bc92
ACO
6437int intel_connector_init(struct intel_connector *connector)
6438{
11c1a9ec 6439 struct intel_digital_connector_state *conn_state;
08d9bc92 6440
11c1a9ec
ML
6441 /*
6442 * Allocate enough memory to hold intel_digital_connector_state,
6443 * This might be a few bytes too many, but for connectors that don't
6444 * need it we'll free the state and allocate a smaller one on the first
6445 * succesful commit anyway.
6446 */
6447 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
6448 if (!conn_state)
08d9bc92
ACO
6449 return -ENOMEM;
6450
11c1a9ec
ML
6451 __drm_atomic_helper_connector_reset(&connector->base,
6452 &conn_state->base);
6453
08d9bc92
ACO
6454 return 0;
6455}
6456
6457struct intel_connector *intel_connector_alloc(void)
6458{
6459 struct intel_connector *connector;
6460
6461 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6462 if (!connector)
6463 return NULL;
6464
6465 if (intel_connector_init(connector) < 0) {
6466 kfree(connector);
6467 return NULL;
6468 }
6469
6470 return connector;
6471}
6472
091a4f91
JA
6473/*
6474 * Free the bits allocated by intel_connector_alloc.
6475 * This should only be used after intel_connector_alloc has returned
6476 * successfully, and before drm_connector_init returns successfully.
6477 * Otherwise the destroy callbacks for the connector and the state should
6478 * take care of proper cleanup/free
6479 */
6480void intel_connector_free(struct intel_connector *connector)
6481{
6482 kfree(to_intel_digital_connector_state(connector->base.state));
6483 kfree(connector);
6484}
6485
f0947c37
SV
6486/* Simple connector->get_hw_state implementation for encoders that support only
6487 * one connector and no cloning and hence the encoder state determines the state
6488 * of the connector. */
6489bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6490{
24929352 6491 enum pipe pipe = 0;
f0947c37 6492 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6493
f0947c37 6494 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6495}
6496
6d293983 6497static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6498{
6d293983
ACO
6499 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6500 return crtc_state->fdi_lanes;
d272ddfa
VS
6501
6502 return 0;
6503}
6504
6d293983 6505static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6506 struct intel_crtc_state *pipe_config)
1857e1da 6507{
8652744b 6508 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
6509 struct drm_atomic_state *state = pipe_config->base.state;
6510 struct intel_crtc *other_crtc;
6511 struct intel_crtc_state *other_crtc_state;
6512
1857e1da
SV
6513 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6514 pipe_name(pipe), pipe_config->fdi_lanes);
6515 if (pipe_config->fdi_lanes > 4) {
6516 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6517 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6518 return -EINVAL;
1857e1da
SV
6519 }
6520
8652744b 6521 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
SV
6522 if (pipe_config->fdi_lanes > 2) {
6523 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6524 pipe_config->fdi_lanes);
6d293983 6525 return -EINVAL;
1857e1da 6526 } else {
6d293983 6527 return 0;
1857e1da
SV
6528 }
6529 }
6530
b7f05d4a 6531 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 6532 return 0;
1857e1da
SV
6533
6534 /* Ivybridge 3 pipe is really complicated */
6535 switch (pipe) {
6536 case PIPE_A:
6d293983 6537 return 0;
1857e1da 6538 case PIPE_B:
6d293983
ACO
6539 if (pipe_config->fdi_lanes <= 2)
6540 return 0;
6541
b91eb5cc 6542 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
6543 other_crtc_state =
6544 intel_atomic_get_crtc_state(state, other_crtc);
6545 if (IS_ERR(other_crtc_state))
6546 return PTR_ERR(other_crtc_state);
6547
6548 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
SV
6549 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6550 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6551 return -EINVAL;
1857e1da 6552 }
6d293983 6553 return 0;
1857e1da 6554 case PIPE_C:
251cc67c
VS
6555 if (pipe_config->fdi_lanes > 2) {
6556 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6557 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6558 return -EINVAL;
251cc67c 6559 }
6d293983 6560
b91eb5cc 6561 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
6562 other_crtc_state =
6563 intel_atomic_get_crtc_state(state, other_crtc);
6564 if (IS_ERR(other_crtc_state))
6565 return PTR_ERR(other_crtc_state);
6566
6567 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6568 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6569 return -EINVAL;
1857e1da 6570 }
6d293983 6571 return 0;
1857e1da
SV
6572 default:
6573 BUG();
6574 }
6575}
6576
e29c22c0
SV
6577#define RETRY 1
6578static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6579 struct intel_crtc_state *pipe_config)
877d48d5 6580{
1857e1da 6581 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6582 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6583 int lane, link_bw, fdi_dotclock, ret;
6584 bool needs_recompute = false;
877d48d5 6585
e29c22c0 6586retry:
877d48d5
SV
6587 /* FDI is a binary signal running at ~2.7GHz, encoding
6588 * each output octet as 10 bits. The actual frequency
6589 * is stored as a divider into a 100MHz clock, and the
6590 * mode pixel clock is stored in units of 1KHz.
6591 * Hence the bw of each lane in terms of the mode signal
6592 * is:
6593 */
21a727b3 6594 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6595
241bfc38 6596 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6597
2bd89a07 6598 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
SV
6599 pipe_config->pipe_bpp);
6600
6601 pipe_config->fdi_lanes = lane;
6602
2bd89a07 6603 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
b31e85ed 6604 link_bw, &pipe_config->fdi_m_n, false);
1857e1da 6605
e3b247da 6606 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6607 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 6608 pipe_config->pipe_bpp -= 2*3;
7ff89ca2
VS
6609 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6610 pipe_config->pipe_bpp);
6611 needs_recompute = true;
6612 pipe_config->bw_constrained = true;
257a7ffc 6613
7ff89ca2 6614 goto retry;
257a7ffc 6615 }
79e53945 6616
7ff89ca2
VS
6617 if (needs_recompute)
6618 return RETRY;
e70236a8 6619
7ff89ca2 6620 return ret;
e70236a8
JB
6621}
6622
24f28450 6623bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
e70236a8 6624{
24f28450
ML
6625 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6626 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6627
6628 /* IPS only exists on ULT machines and is tied to pipe A. */
6629 if (!hsw_crtc_supports_ips(crtc))
6e644626
VS
6630 return false;
6631
24f28450 6632 if (!i915_modparams.enable_ips)
7ff89ca2 6633 return false;
e70236a8 6634
24f28450
ML
6635 if (crtc_state->pipe_bpp > 24)
6636 return false;
1b1d2716 6637
65cd2b3f 6638 /*
7ff89ca2
VS
6639 * We compare against max which means we must take
6640 * the increased cdclk requirement into account when
6641 * calculating the new cdclk.
6642 *
6643 * Should measure whether using a lower cdclk w/o IPS
e70236a8 6644 */
24f28450
ML
6645 if (IS_BROADWELL(dev_priv) &&
6646 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
6647 return false;
6648
6649 return true;
e70236a8 6650}
79e53945 6651
24f28450 6652static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7ff89ca2 6653{
24f28450
ML
6654 struct drm_i915_private *dev_priv =
6655 to_i915(crtc_state->base.crtc->dev);
6656 struct intel_atomic_state *intel_state =
6657 to_intel_atomic_state(crtc_state->base.state);
6658
6659 if (!hsw_crtc_state_ips_capable(crtc_state))
6660 return false;
6661
6662 if (crtc_state->ips_force_disable)
6663 return false;
6664
adbe5c5c
ML
6665 /* IPS should be fine as long as at least one plane is enabled. */
6666 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
24f28450 6667 return false;
34edce2f 6668
24f28450
ML
6669 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
6670 if (IS_BROADWELL(dev_priv) &&
6671 crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
6672 return false;
6673
6674 return true;
34edce2f
VS
6675}
6676
7ff89ca2 6677static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 6678{
7ff89ca2 6679 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 6680
7ff89ca2 6681 /* GDG double wide on either pipe, otherwise pipe A only */
c56b89f1 6682 return INTEL_GEN(dev_priv) < 4 &&
7ff89ca2 6683 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
6684}
6685
ceb99320
VS
6686static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
6687{
6688 uint32_t pixel_rate;
6689
6690 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
6691
6692 /*
6693 * We only use IF-ID interlacing. If we ever use
6694 * PF-ID we'll need to adjust the pixel_rate here.
6695 */
6696
6697 if (pipe_config->pch_pfit.enabled) {
6698 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
6699 uint32_t pfit_size = pipe_config->pch_pfit.size;
6700
6701 pipe_w = pipe_config->pipe_src_w;
6702 pipe_h = pipe_config->pipe_src_h;
6703
6704 pfit_w = (pfit_size >> 16) & 0xFFFF;
6705 pfit_h = pfit_size & 0xFFFF;
6706 if (pipe_w < pfit_w)
6707 pipe_w = pfit_w;
6708 if (pipe_h < pfit_h)
6709 pipe_h = pfit_h;
6710
6711 if (WARN_ON(!pfit_w || !pfit_h))
6712 return pixel_rate;
6713
6714 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
6715 pfit_w * pfit_h);
6716 }
6717
6718 return pixel_rate;
6719}
6720
7ff89ca2 6721static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 6722{
7ff89ca2 6723 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
34edce2f 6724
7ff89ca2
VS
6725 if (HAS_GMCH_DISPLAY(dev_priv))
6726 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
6727 crtc_state->pixel_rate =
6728 crtc_state->base.adjusted_mode.crtc_clock;
6729 else
6730 crtc_state->pixel_rate =
6731 ilk_pipe_pixel_rate(crtc_state);
6732}
34edce2f 6733
7ff89ca2
VS
6734static int intel_crtc_compute_config(struct intel_crtc *crtc,
6735 struct intel_crtc_state *pipe_config)
6736{
6737 struct drm_device *dev = crtc->base.dev;
6738 struct drm_i915_private *dev_priv = to_i915(dev);
6739 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6740 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 6741
7ff89ca2
VS
6742 if (INTEL_GEN(dev_priv) < 4) {
6743 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 6744
7ff89ca2
VS
6745 /*
6746 * Enable double wide mode when the dot clock
6747 * is > 90% of the (display) core speed.
6748 */
6749 if (intel_crtc_supports_double_wide(crtc) &&
6750 adjusted_mode->crtc_clock > clock_limit) {
6751 clock_limit = dev_priv->max_dotclk_freq;
6752 pipe_config->double_wide = true;
6753 }
34edce2f
VS
6754 }
6755
7ff89ca2
VS
6756 if (adjusted_mode->crtc_clock > clock_limit) {
6757 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6758 adjusted_mode->crtc_clock, clock_limit,
6759 yesno(pipe_config->double_wide));
6760 return -EINVAL;
6761 }
34edce2f 6762
25edf915
SS
6763 if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
6764 /*
6765 * There is only one pipe CSC unit per pipe, and we need that
6766 * for output conversion from RGB->YCBCR. So if CTM is already
6767 * applied we can't support YCBCR420 output.
6768 */
6769 DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
6770 return -EINVAL;
6771 }
6772
7ff89ca2
VS
6773 /*
6774 * Pipe horizontal size must be even in:
6775 * - DVO ganged mode
6776 * - LVDS dual channel mode
6777 * - Double wide pipe
6778 */
0574bd88
VS
6779 if (pipe_config->pipe_src_w & 1) {
6780 if (pipe_config->double_wide) {
6781 DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
6782 return -EINVAL;
6783 }
6784
6785 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6786 intel_is_dual_link_lvds(dev)) {
6787 DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
6788 return -EINVAL;
6789 }
6790 }
34edce2f 6791
7ff89ca2
VS
6792 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6793 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6794 */
6795 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
6796 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6797 return -EINVAL;
34edce2f 6798
7ff89ca2 6799 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 6800
7ff89ca2
VS
6801 if (pipe_config->has_pch_encoder)
6802 return ironlake_fdi_compute_config(crtc, pipe_config);
34edce2f 6803
7ff89ca2 6804 return 0;
34edce2f
VS
6805}
6806
2c07245f 6807static void
a65851af 6808intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6809{
a65851af
VS
6810 while (*num > DATA_LINK_M_N_MASK ||
6811 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6812 *num >>= 1;
6813 *den >>= 1;
6814 }
6815}
6816
a65851af 6817static void compute_m_n(unsigned int m, unsigned int n,
b31e85ed 6818 uint32_t *ret_m, uint32_t *ret_n,
53ca2edc 6819 bool constant_n)
a65851af 6820{
9a86cda0 6821 /*
53ca2edc
LS
6822 * Several DP dongles in particular seem to be fussy about
6823 * too large link M/N values. Give N value as 0x8000 that
6824 * should be acceptable by specific devices. 0x8000 is the
6825 * specified fixed N value for asynchronous clock mode,
6826 * which the devices expect also in synchronous clock mode.
9a86cda0 6827 */
53ca2edc
LS
6828 if (constant_n)
6829 *ret_n = 0x8000;
6830 else
6831 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
9a86cda0 6832
a65851af
VS
6833 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6834 intel_reduce_m_n_ratio(ret_m, ret_n);
6835}
6836
e69d0bc1
SV
6837void
6838intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6839 int pixel_clock, int link_clock,
b31e85ed 6840 struct intel_link_m_n *m_n,
53ca2edc 6841 bool constant_n)
2c07245f 6842{
e69d0bc1 6843 m_n->tu = 64;
a65851af
VS
6844
6845 compute_m_n(bits_per_pixel * pixel_clock,
6846 link_clock * nlanes * 8,
b31e85ed 6847 &m_n->gmch_m, &m_n->gmch_n,
53ca2edc 6848 constant_n);
a65851af
VS
6849
6850 compute_m_n(pixel_clock, link_clock,
b31e85ed 6851 &m_n->link_m, &m_n->link_n,
53ca2edc 6852 constant_n);
2c07245f
ZW
6853}
6854
a7615030
CW
6855static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6856{
4f044a88
MW
6857 if (i915_modparams.panel_use_ssc >= 0)
6858 return i915_modparams.panel_use_ssc != 0;
41aa3448 6859 return dev_priv->vbt.lvds_use_ssc
435793df 6860 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6861}
6862
7429e9d4 6863static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6864{
7df00d7a 6865 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6866}
f47709a9 6867
7429e9d4
SV
6868static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6869{
6870 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6871}
6872
f47709a9 6873static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6874 struct intel_crtc_state *crtc_state,
9e2c8475 6875 struct dpll *reduced_clock)
a7516a05 6876{
9b1e14f4 6877 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
6878 u32 fp, fp2 = 0;
6879
9b1e14f4 6880 if (IS_PINEVIEW(dev_priv)) {
190f68c5 6881 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6882 if (reduced_clock)
7429e9d4 6883 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6884 } else {
190f68c5 6885 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6886 if (reduced_clock)
7429e9d4 6887 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6888 }
6889
190f68c5 6890 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6891
2d84d2b3 6892 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6893 reduced_clock) {
190f68c5 6894 crtc_state->dpll_hw_state.fp1 = fp2;
a7516a05 6895 } else {
190f68c5 6896 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6897 }
6898}
6899
5e69f97f
CML
6900static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6901 pipe)
89b667f8
JB
6902{
6903 u32 reg_val;
6904
6905 /*
6906 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6907 * and set it to a reasonable value instead.
6908 */
ab3c759a 6909 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6910 reg_val &= 0xffffff00;
6911 reg_val |= 0x00000030;
ab3c759a 6912 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6913
ab3c759a 6914 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
6915 reg_val &= 0x00ffffff;
6916 reg_val |= 0x8c000000;
ab3c759a 6917 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6918
ab3c759a 6919 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6920 reg_val &= 0xffffff00;
ab3c759a 6921 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6922
ab3c759a 6923 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6924 reg_val &= 0x00ffffff;
6925 reg_val |= 0xb0000000;
ab3c759a 6926 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6927}
6928
b551842d
SV
6929static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6930 struct intel_link_m_n *m_n)
6931{
6932 struct drm_device *dev = crtc->base.dev;
fac5e23e 6933 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
SV
6934 int pipe = crtc->pipe;
6935
e3b95f1e
SV
6936 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6937 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6938 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6939 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
SV
6940}
6941
6942static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6943 struct intel_link_m_n *m_n,
6944 struct intel_link_m_n *m2_n2)
b551842d 6945{
6315b5d3 6946 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 6947 int pipe = crtc->pipe;
6e3c9717 6948 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 6949
6315b5d3 6950 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
SV
6951 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6952 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6953 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6954 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6955 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6956 * for gen < 8) and if DRRS is supported (to make sure the
6957 * registers are not unnecessarily accessed).
6958 */
920a14b2
TU
6959 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
6960 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
6961 I915_WRITE(PIPE_DATA_M2(transcoder),
6962 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6963 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6964 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6965 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6966 }
b551842d 6967 } else {
e3b95f1e
SV
6968 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6969 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6970 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6971 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
SV
6972 }
6973}
6974
fe3cd48d 6975void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6976{
fe3cd48d
R
6977 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6978
6979 if (m_n == M1_N1) {
6980 dp_m_n = &crtc->config->dp_m_n;
6981 dp_m2_n2 = &crtc->config->dp_m2_n2;
6982 } else if (m_n == M2_N2) {
6983
6984 /*
6985 * M2_N2 registers are not supported. Hence m2_n2 divider value
6986 * needs to be programmed into M1_N1.
6987 */
6988 dp_m_n = &crtc->config->dp_m2_n2;
6989 } else {
6990 DRM_ERROR("Unsupported divider value\n");
6991 return;
6992 }
6993
6e3c9717
ACO
6994 if (crtc->config->has_pch_encoder)
6995 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6996 else
fe3cd48d 6997 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
SV
6998}
6999
251ac862
SV
7000static void vlv_compute_dpll(struct intel_crtc *crtc,
7001 struct intel_crtc_state *pipe_config)
bdd4b6a6 7002{
03ed5cbf 7003 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7004 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7005 if (crtc->pipe != PIPE_A)
7006 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7007
cd2d34d9 7008 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7009 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7010 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7011 DPLL_EXT_BUFFER_ENABLE_VLV;
7012
03ed5cbf
VS
7013 pipe_config->dpll_hw_state.dpll_md =
7014 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7015}
bdd4b6a6 7016
03ed5cbf
VS
7017static void chv_compute_dpll(struct intel_crtc *crtc,
7018 struct intel_crtc_state *pipe_config)
7019{
7020 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7021 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7022 if (crtc->pipe != PIPE_A)
7023 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7024
cd2d34d9 7025 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7026 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7027 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7028
03ed5cbf
VS
7029 pipe_config->dpll_hw_state.dpll_md =
7030 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
SV
7031}
7032
d288f65f 7033static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7034 const struct intel_crtc_state *pipe_config)
a0c4da24 7035{
f47709a9 7036 struct drm_device *dev = crtc->base.dev;
fac5e23e 7037 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7038 enum pipe pipe = crtc->pipe;
bdd4b6a6 7039 u32 mdiv;
a0c4da24 7040 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7041 u32 coreclk, reg_val;
a0c4da24 7042
cd2d34d9
VS
7043 /* Enable Refclk */
7044 I915_WRITE(DPLL(pipe),
7045 pipe_config->dpll_hw_state.dpll &
7046 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7047
7048 /* No need to actually set up the DPLL with DSI */
7049 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7050 return;
7051
a580516d 7052 mutex_lock(&dev_priv->sb_lock);
09153000 7053
d288f65f
VS
7054 bestn = pipe_config->dpll.n;
7055 bestm1 = pipe_config->dpll.m1;
7056 bestm2 = pipe_config->dpll.m2;
7057 bestp1 = pipe_config->dpll.p1;
7058 bestp2 = pipe_config->dpll.p2;
a0c4da24 7059
89b667f8
JB
7060 /* See eDP HDMI DPIO driver vbios notes doc */
7061
7062 /* PLL B needs special handling */
bdd4b6a6 7063 if (pipe == PIPE_B)
5e69f97f 7064 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7065
7066 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7067 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7068
7069 /* Disable target IRef on PLL */
ab3c759a 7070 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7071 reg_val &= 0x00ffffff;
ab3c759a 7072 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7073
7074 /* Disable fast lock */
ab3c759a 7075 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7076
7077 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7078 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7079 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7080 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7081 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7082
7083 /*
7084 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7085 * but we don't support that).
7086 * Note: don't use the DAC post divider as it seems unstable.
7087 */
7088 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7089 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7090
a0c4da24 7091 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7092 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7093
89b667f8 7094 /* Set HBR and RBR LPF coefficients */
d288f65f 7095 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7096 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7097 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7098 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7099 0x009f0003);
89b667f8 7100 else
ab3c759a 7101 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7102 0x00d0000f);
7103
37a5650b 7104 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7105 /* Use SSC source */
bdd4b6a6 7106 if (pipe == PIPE_A)
ab3c759a 7107 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7108 0x0df40000);
7109 else
ab3c759a 7110 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7111 0x0df70000);
7112 } else { /* HDMI or VGA */
7113 /* Use bend source */
bdd4b6a6 7114 if (pipe == PIPE_A)
ab3c759a 7115 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7116 0x0df70000);
7117 else
ab3c759a 7118 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7119 0x0df40000);
7120 }
a0c4da24 7121
ab3c759a 7122 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7123 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 7124 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 7125 coreclk |= 0x01000000;
ab3c759a 7126 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7127
ab3c759a 7128 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7129 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7130}
7131
d288f65f 7132static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7133 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7134{
7135 struct drm_device *dev = crtc->base.dev;
fac5e23e 7136 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7137 enum pipe pipe = crtc->pipe;
9d556c99 7138 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7139 u32 loopfilter, tribuf_calcntr;
9d556c99 7140 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7141 u32 dpio_val;
9cbe40c1 7142 int vco;
9d556c99 7143
cd2d34d9
VS
7144 /* Enable Refclk and SSC */
7145 I915_WRITE(DPLL(pipe),
7146 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7147
7148 /* No need to actually set up the DPLL with DSI */
7149 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7150 return;
7151
d288f65f
VS
7152 bestn = pipe_config->dpll.n;
7153 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7154 bestm1 = pipe_config->dpll.m1;
7155 bestm2 = pipe_config->dpll.m2 >> 22;
7156 bestp1 = pipe_config->dpll.p1;
7157 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7158 vco = pipe_config->dpll.vco;
a945ce7e 7159 dpio_val = 0;
9cbe40c1 7160 loopfilter = 0;
9d556c99 7161
a580516d 7162 mutex_lock(&dev_priv->sb_lock);
9d556c99 7163
9d556c99
CML
7164 /* p1 and p2 divider */
7165 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7166 5 << DPIO_CHV_S1_DIV_SHIFT |
7167 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7168 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7169 1 << DPIO_CHV_K_DIV_SHIFT);
7170
7171 /* Feedback post-divider - m2 */
7172 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7173
7174 /* Feedback refclk divider - n and m1 */
7175 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7176 DPIO_CHV_M1_DIV_BY_2 |
7177 1 << DPIO_CHV_N_DIV_SHIFT);
7178
7179 /* M2 fraction division */
25a25dfc 7180 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7181
7182 /* M2 fraction division enable */
a945ce7e
VP
7183 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7184 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7185 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7186 if (bestm2_frac)
7187 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7188 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7189
de3a0fde
VP
7190 /* Program digital lock detect threshold */
7191 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7192 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7193 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7194 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7195 if (!bestm2_frac)
7196 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7197 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7198
9d556c99 7199 /* Loop filter */
9cbe40c1
VP
7200 if (vco == 5400000) {
7201 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7202 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7203 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7204 tribuf_calcntr = 0x9;
7205 } else if (vco <= 6200000) {
7206 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7207 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7208 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7209 tribuf_calcntr = 0x9;
7210 } else if (vco <= 6480000) {
7211 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7212 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7213 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7214 tribuf_calcntr = 0x8;
7215 } else {
7216 /* Not supported. Apply the same limits as in the max case */
7217 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7218 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7219 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7220 tribuf_calcntr = 0;
7221 }
9d556c99
CML
7222 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7223
968040b2 7224 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7225 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7226 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7227 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7228
9d556c99
CML
7229 /* AFC Recal */
7230 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7231 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7232 DPIO_AFC_RECAL);
7233
a580516d 7234 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7235}
7236
d288f65f
VS
7237/**
7238 * vlv_force_pll_on - forcibly enable just the PLL
7239 * @dev_priv: i915 private structure
7240 * @pipe: pipe PLL to enable
7241 * @dpll: PLL configuration
7242 *
7243 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7244 * in cases where we need the PLL enabled even when @pipe is not going to
7245 * be enabled.
7246 */
30ad9814 7247int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 7248 const struct dpll *dpll)
d288f65f 7249{
b91eb5cc 7250 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
7251 struct intel_crtc_state *pipe_config;
7252
7253 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7254 if (!pipe_config)
7255 return -ENOMEM;
7256
7257 pipe_config->base.crtc = &crtc->base;
7258 pipe_config->pixel_multiplier = 1;
7259 pipe_config->dpll = *dpll;
d288f65f 7260
30ad9814 7261 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
7262 chv_compute_dpll(crtc, pipe_config);
7263 chv_prepare_pll(crtc, pipe_config);
7264 chv_enable_pll(crtc, pipe_config);
d288f65f 7265 } else {
3f36b937
TU
7266 vlv_compute_dpll(crtc, pipe_config);
7267 vlv_prepare_pll(crtc, pipe_config);
7268 vlv_enable_pll(crtc, pipe_config);
d288f65f 7269 }
3f36b937
TU
7270
7271 kfree(pipe_config);
7272
7273 return 0;
d288f65f
VS
7274}
7275
7276/**
7277 * vlv_force_pll_off - forcibly disable just the PLL
7278 * @dev_priv: i915 private structure
7279 * @pipe: pipe PLL to disable
7280 *
7281 * Disable the PLL for @pipe. To be used in cases where we need
7282 * the PLL enabled even when @pipe is not going to be enabled.
7283 */
30ad9814 7284void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 7285{
30ad9814
VS
7286 if (IS_CHERRYVIEW(dev_priv))
7287 chv_disable_pll(dev_priv, pipe);
d288f65f 7288 else
30ad9814 7289 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
7290}
7291
251ac862
SV
7292static void i9xx_compute_dpll(struct intel_crtc *crtc,
7293 struct intel_crtc_state *crtc_state,
9e2c8475 7294 struct dpll *reduced_clock)
eb1cbe48 7295{
9b1e14f4 7296 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 7297 u32 dpll;
190f68c5 7298 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7299
190f68c5 7300 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7301
eb1cbe48
SV
7302 dpll = DPLL_VGA_MODE_DIS;
7303
2d84d2b3 7304 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
SV
7305 dpll |= DPLLB_MODE_LVDS;
7306 else
7307 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7308
73f67aa8
JN
7309 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7310 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 7311 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7312 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7313 }
198a037f 7314
3d6e9ee0
VS
7315 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7316 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 7317 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7318
37a5650b 7319 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 7320 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
SV
7321
7322 /* compute bitmask from p1 value */
9b1e14f4 7323 if (IS_PINEVIEW(dev_priv))
eb1cbe48
SV
7324 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7325 else {
7326 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 7327 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
SV
7328 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7329 }
7330 switch (clock->p2) {
7331 case 5:
7332 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7333 break;
7334 case 7:
7335 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7336 break;
7337 case 10:
7338 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7339 break;
7340 case 14:
7341 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7342 break;
7343 }
9b1e14f4 7344 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
SV
7345 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7346
190f68c5 7347 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7348 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 7349 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7350 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
7351 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7352 else
7353 dpll |= PLL_REF_INPUT_DREFCLK;
7354
7355 dpll |= DPLL_VCO_ENABLE;
190f68c5 7356 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7357
9b1e14f4 7358 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 7359 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7360 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7361 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
SV
7362 }
7363}
7364
251ac862
SV
7365static void i8xx_compute_dpll(struct intel_crtc *crtc,
7366 struct intel_crtc_state *crtc_state,
9e2c8475 7367 struct dpll *reduced_clock)
eb1cbe48 7368{
f47709a9 7369 struct drm_device *dev = crtc->base.dev;
fac5e23e 7370 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 7371 u32 dpll;
190f68c5 7372 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7373
190f68c5 7374 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7375
eb1cbe48
SV
7376 dpll = DPLL_VGA_MODE_DIS;
7377
2d84d2b3 7378 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
SV
7379 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7380 } else {
7381 if (clock->p1 == 2)
7382 dpll |= PLL_P1_DIVIDE_BY_TWO;
7383 else
7384 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7385 if (clock->p2 == 4)
7386 dpll |= PLL_P2_DIVIDE_BY_4;
7387 }
7388
50a0bc90
TU
7389 if (!IS_I830(dev_priv) &&
7390 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
SV
7391 dpll |= DPLL_DVO_2X_MODE;
7392
2d84d2b3 7393 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 7394 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
7395 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7396 else
7397 dpll |= PLL_REF_INPUT_DREFCLK;
7398
7399 dpll |= DPLL_VCO_ENABLE;
190f68c5 7400 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
SV
7401}
7402
8a654f3b 7403static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 7404{
6315b5d3 7405 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 7406 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7407 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7408 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7409 uint32_t crtc_vtotal, crtc_vblank_end;
7410 int vsyncshift = 0;
4d8a62ea
SV
7411
7412 /* We need to be careful not to changed the adjusted mode, for otherwise
7413 * the hw state checker will get angry at the mismatch. */
7414 crtc_vtotal = adjusted_mode->crtc_vtotal;
7415 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7416
609aeaca 7417 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7418 /* the chip adds 2 halflines automatically */
4d8a62ea
SV
7419 crtc_vtotal -= 1;
7420 crtc_vblank_end -= 1;
609aeaca 7421
2d84d2b3 7422 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
7423 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7424 else
7425 vsyncshift = adjusted_mode->crtc_hsync_start -
7426 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7427 if (vsyncshift < 0)
7428 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7429 }
7430
6315b5d3 7431 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 7432 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7433
fe2b8f9d 7434 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7435 (adjusted_mode->crtc_hdisplay - 1) |
7436 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7437 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7438 (adjusted_mode->crtc_hblank_start - 1) |
7439 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7440 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7441 (adjusted_mode->crtc_hsync_start - 1) |
7442 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7443
fe2b8f9d 7444 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7445 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7446 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7447 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7448 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7449 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7450 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7451 (adjusted_mode->crtc_vsync_start - 1) |
7452 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7453
b5e508d4
PZ
7454 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7455 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7456 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7457 * bits. */
772c2a51 7458 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
7459 (pipe == PIPE_B || pipe == PIPE_C))
7460 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7461
bc58be60
JN
7462}
7463
7464static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7465{
7466 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 7467 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
7468 enum pipe pipe = intel_crtc->pipe;
7469
b0e77b9c
PZ
7470 /* pipesrc controls the size that is scaled from, which should
7471 * always be the user's requested size.
7472 */
7473 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7474 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7475 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7476}
7477
1bd1bd80 7478static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7479 struct intel_crtc_state *pipe_config)
1bd1bd80
SV
7480{
7481 struct drm_device *dev = crtc->base.dev;
fac5e23e 7482 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
SV
7483 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7484 uint32_t tmp;
7485
7486 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7487 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7488 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7489 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7490 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7491 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7492 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7493 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7494 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
7495
7496 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7497 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7498 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7499 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7500 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7501 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7502 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7503 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7504 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
7505
7506 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7507 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7508 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7509 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 7510 }
bc58be60
JN
7511}
7512
7513static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7514 struct intel_crtc_state *pipe_config)
7515{
7516 struct drm_device *dev = crtc->base.dev;
fac5e23e 7517 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 7518 u32 tmp;
1bd1bd80
SV
7519
7520 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7521 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7522 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7523
2d112de7
ACO
7524 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7525 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
SV
7526}
7527
f6a83288 7528void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7529 struct intel_crtc_state *pipe_config)
babea61d 7530{
2d112de7
ACO
7531 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7532 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7533 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7534 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7535
2d112de7
ACO
7536 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7537 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7538 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7539 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7540
2d112de7 7541 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7542 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7543
2d112de7 7544 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
cd13f5ab
ML
7545
7546 mode->hsync = drm_mode_hsync(mode);
7547 mode->vrefresh = drm_mode_vrefresh(mode);
7548 drm_mode_set_name(mode);
babea61d
JB
7549}
7550
84b046f3
SV
7551static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7552{
6315b5d3 7553 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
SV
7554 uint32_t pipeconf;
7555
9f11a9e4 7556 pipeconf = 0;
84b046f3 7557
e56134bc
VS
7558 /* we keep both pipes enabled on 830 */
7559 if (IS_I830(dev_priv))
b6b5d049 7560 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7561
6e3c9717 7562 if (intel_crtc->config->double_wide)
cf532bb2 7563 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7564
ff9ce46e 7565 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
7566 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7567 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 7568 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7569 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7570 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7571 PIPECONF_DITHER_TYPE_SP;
84b046f3 7572
6e3c9717 7573 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
SV
7574 case 18:
7575 pipeconf |= PIPECONF_6BPC;
7576 break;
7577 case 24:
7578 pipeconf |= PIPECONF_8BPC;
7579 break;
7580 case 30:
7581 pipeconf |= PIPECONF_10BPC;
7582 break;
7583 default:
7584 /* Case prevented by intel_choose_pipe_bpp_dither. */
7585 BUG();
84b046f3
SV
7586 }
7587 }
7588
6e3c9717 7589 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 7590 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 7591 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7592 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7593 else
7594 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7595 } else
84b046f3
SV
7596 pipeconf |= PIPECONF_PROGRESSIVE;
7597
920a14b2 7598 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 7599 intel_crtc->config->limited_color_range)
9f11a9e4 7600 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7601
84b046f3
SV
7602 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7603 POSTING_READ(PIPECONF(intel_crtc->pipe));
7604}
7605
81c97f52
ACO
7606static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7607 struct intel_crtc_state *crtc_state)
7608{
7609 struct drm_device *dev = crtc->base.dev;
fac5e23e 7610 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7611 const struct intel_limit *limit;
81c97f52
ACO
7612 int refclk = 48000;
7613
7614 memset(&crtc_state->dpll_hw_state, 0,
7615 sizeof(crtc_state->dpll_hw_state));
7616
2d84d2b3 7617 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
7618 if (intel_panel_use_ssc(dev_priv)) {
7619 refclk = dev_priv->vbt.lvds_ssc_freq;
7620 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7621 }
7622
7623 limit = &intel_limits_i8xx_lvds;
2d84d2b3 7624 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
7625 limit = &intel_limits_i8xx_dvo;
7626 } else {
7627 limit = &intel_limits_i8xx_dac;
7628 }
7629
7630 if (!crtc_state->clock_set &&
7631 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7632 refclk, NULL, &crtc_state->dpll)) {
7633 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7634 return -EINVAL;
7635 }
7636
7637 i8xx_compute_dpll(crtc, crtc_state, NULL);
7638
7639 return 0;
7640}
7641
19ec6693
ACO
7642static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7643 struct intel_crtc_state *crtc_state)
7644{
7645 struct drm_device *dev = crtc->base.dev;
fac5e23e 7646 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7647 const struct intel_limit *limit;
19ec6693
ACO
7648 int refclk = 96000;
7649
7650 memset(&crtc_state->dpll_hw_state, 0,
7651 sizeof(crtc_state->dpll_hw_state));
7652
2d84d2b3 7653 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
7654 if (intel_panel_use_ssc(dev_priv)) {
7655 refclk = dev_priv->vbt.lvds_ssc_freq;
7656 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7657 }
7658
7659 if (intel_is_dual_link_lvds(dev))
7660 limit = &intel_limits_g4x_dual_channel_lvds;
7661 else
7662 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
7663 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7664 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 7665 limit = &intel_limits_g4x_hdmi;
2d84d2b3 7666 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
7667 limit = &intel_limits_g4x_sdvo;
7668 } else {
7669 /* The option is for other outputs */
7670 limit = &intel_limits_i9xx_sdvo;
7671 }
7672
7673 if (!crtc_state->clock_set &&
7674 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7675 refclk, NULL, &crtc_state->dpll)) {
7676 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7677 return -EINVAL;
7678 }
7679
7680 i9xx_compute_dpll(crtc, crtc_state, NULL);
7681
7682 return 0;
7683}
7684
70e8aa21
ACO
7685static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7686 struct intel_crtc_state *crtc_state)
7687{
7688 struct drm_device *dev = crtc->base.dev;
fac5e23e 7689 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7690 const struct intel_limit *limit;
70e8aa21
ACO
7691 int refclk = 96000;
7692
7693 memset(&crtc_state->dpll_hw_state, 0,
7694 sizeof(crtc_state->dpll_hw_state));
7695
2d84d2b3 7696 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7697 if (intel_panel_use_ssc(dev_priv)) {
7698 refclk = dev_priv->vbt.lvds_ssc_freq;
7699 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7700 }
7701
7702 limit = &intel_limits_pineview_lvds;
7703 } else {
7704 limit = &intel_limits_pineview_sdvo;
7705 }
7706
7707 if (!crtc_state->clock_set &&
7708 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7709 refclk, NULL, &crtc_state->dpll)) {
7710 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7711 return -EINVAL;
7712 }
7713
7714 i9xx_compute_dpll(crtc, crtc_state, NULL);
7715
7716 return 0;
7717}
7718
190f68c5
ACO
7719static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7720 struct intel_crtc_state *crtc_state)
79e53945 7721{
c7653199 7722 struct drm_device *dev = crtc->base.dev;
fac5e23e 7723 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 7724 const struct intel_limit *limit;
81c97f52 7725 int refclk = 96000;
79e53945 7726
dd3cd74a
ACO
7727 memset(&crtc_state->dpll_hw_state, 0,
7728 sizeof(crtc_state->dpll_hw_state));
7729
2d84d2b3 7730 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
7731 if (intel_panel_use_ssc(dev_priv)) {
7732 refclk = dev_priv->vbt.lvds_ssc_freq;
7733 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7734 }
43565a06 7735
70e8aa21
ACO
7736 limit = &intel_limits_i9xx_lvds;
7737 } else {
7738 limit = &intel_limits_i9xx_sdvo;
81c97f52 7739 }
79e53945 7740
70e8aa21
ACO
7741 if (!crtc_state->clock_set &&
7742 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7743 refclk, NULL, &crtc_state->dpll)) {
7744 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7745 return -EINVAL;
f47709a9 7746 }
7026d4ac 7747
81c97f52 7748 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 7749
c8f7a0db 7750 return 0;
f564048e
EA
7751}
7752
65b3d6a9
ACO
7753static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7754 struct intel_crtc_state *crtc_state)
7755{
7756 int refclk = 100000;
1b6f4958 7757 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
7758
7759 memset(&crtc_state->dpll_hw_state, 0,
7760 sizeof(crtc_state->dpll_hw_state));
7761
65b3d6a9
ACO
7762 if (!crtc_state->clock_set &&
7763 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7764 refclk, NULL, &crtc_state->dpll)) {
7765 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7766 return -EINVAL;
7767 }
7768
7769 chv_compute_dpll(crtc, crtc_state);
7770
7771 return 0;
7772}
7773
7774static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7775 struct intel_crtc_state *crtc_state)
7776{
7777 int refclk = 100000;
1b6f4958 7778 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
7779
7780 memset(&crtc_state->dpll_hw_state, 0,
7781 sizeof(crtc_state->dpll_hw_state));
7782
65b3d6a9
ACO
7783 if (!crtc_state->clock_set &&
7784 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7785 refclk, NULL, &crtc_state->dpll)) {
7786 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7787 return -EINVAL;
7788 }
7789
7790 vlv_compute_dpll(crtc, crtc_state);
7791
7792 return 0;
7793}
7794
2fa2fe9a 7795static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7796 struct intel_crtc_state *pipe_config)
2fa2fe9a 7797{
6315b5d3 7798 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
SV
7799 uint32_t tmp;
7800
50a0bc90
TU
7801 if (INTEL_GEN(dev_priv) <= 3 &&
7802 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
7803 return;
7804
2fa2fe9a 7805 tmp = I915_READ(PFIT_CONTROL);
06922821
SV
7806 if (!(tmp & PFIT_ENABLE))
7807 return;
2fa2fe9a 7808
06922821 7809 /* Check whether the pfit is attached to our pipe. */
6315b5d3 7810 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
SV
7811 if (crtc->pipe != PIPE_B)
7812 return;
2fa2fe9a
SV
7813 } else {
7814 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7815 return;
7816 }
7817
06922821 7818 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 7819 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
SV
7820}
7821
acbec814 7822static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7823 struct intel_crtc_state *pipe_config)
acbec814
JB
7824{
7825 struct drm_device *dev = crtc->base.dev;
fac5e23e 7826 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 7827 int pipe = pipe_config->cpu_transcoder;
9e2c8475 7828 struct dpll clock;
acbec814 7829 u32 mdiv;
662c6ecb 7830 int refclk = 100000;
acbec814 7831
b521973b
VS
7832 /* In case of DSI, DPLL will not be used */
7833 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
7834 return;
7835
a580516d 7836 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7837 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7838 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7839
7840 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7841 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7842 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7843 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7844 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7845
dccbea3b 7846 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7847}
7848
5724dbd1
DL
7849static void
7850i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7851 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7852{
7853 struct drm_device *dev = crtc->base.dev;
fac5e23e 7854 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
7855 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7856 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
eade6c89 7857 enum pipe pipe;
1ad292b5 7858 u32 val, base, offset;
1ad292b5 7859 int fourcc, pixel_format;
6761dd31 7860 unsigned int aligned_height;
b113d5ee 7861 struct drm_framebuffer *fb;
1b842c89 7862 struct intel_framebuffer *intel_fb;
1ad292b5 7863
eade6c89 7864 if (!plane->get_hw_state(plane, &pipe))
42a7b088
DL
7865 return;
7866
eade6c89
VS
7867 WARN_ON(pipe != crtc->pipe);
7868
d9806c9f 7869 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7870 if (!intel_fb) {
1ad292b5
JB
7871 DRM_DEBUG_KMS("failed to alloc fb\n");
7872 return;
7873 }
7874
1b842c89
DL
7875 fb = &intel_fb->base;
7876
d2e9f5fc
VS
7877 fb->dev = dev;
7878
2924b8cc
VS
7879 val = I915_READ(DSPCNTR(i9xx_plane));
7880
6315b5d3 7881 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 7882 if (val & DISPPLANE_TILED) {
49af449b 7883 plane_config->tiling = I915_TILING_X;
bae781b2 7884 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e 7885 }
f559156c
VS
7886
7887 if (val & DISPPLANE_ROTATE_180)
7888 plane_config->rotation = DRM_MODE_ROTATE_180;
18c5247e 7889 }
1ad292b5 7890
f559156c
VS
7891 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
7892 val & DISPPLANE_MIRROR)
7893 plane_config->rotation |= DRM_MODE_REFLECT_X;
7894
1ad292b5 7895 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7896 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 7897 fb->format = drm_format_info(fourcc);
1ad292b5 7898
81894b2f
VS
7899 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7900 offset = I915_READ(DSPOFFSET(i9xx_plane));
7901 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
7902 } else if (INTEL_GEN(dev_priv) >= 4) {
49af449b 7903 if (plane_config->tiling)
282e83ef 7904 offset = I915_READ(DSPTILEOFF(i9xx_plane));
1ad292b5 7905 else
282e83ef
VS
7906 offset = I915_READ(DSPLINOFF(i9xx_plane));
7907 base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
1ad292b5 7908 } else {
282e83ef 7909 base = I915_READ(DSPADDR(i9xx_plane));
1ad292b5
JB
7910 }
7911 plane_config->base = base;
7912
7913 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7914 fb->width = ((val >> 16) & 0xfff) + 1;
7915 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5 7916
282e83ef 7917 val = I915_READ(DSPSTRIDE(i9xx_plane));
b113d5ee 7918 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7919
d88c4afd 7920 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 7921
f37b5c2b 7922 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7923
282e83ef
VS
7924 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7925 crtc->base.name, plane->base.name, fb->width, fb->height,
272725c7 7926 fb->format->cpp[0] * 8, base, fb->pitches[0],
2844a921 7927 plane_config->size);
1ad292b5 7928
2d14030b 7929 plane_config->fb = intel_fb;
1ad292b5
JB
7930}
7931
70b23a98 7932static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7933 struct intel_crtc_state *pipe_config)
70b23a98
VS
7934{
7935 struct drm_device *dev = crtc->base.dev;
fac5e23e 7936 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
7937 int pipe = pipe_config->cpu_transcoder;
7938 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 7939 struct dpll clock;
0d7b6b11 7940 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
7941 int refclk = 100000;
7942
b521973b
VS
7943 /* In case of DSI, DPLL will not be used */
7944 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7945 return;
7946
a580516d 7947 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7948 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7949 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7950 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7951 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 7952 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 7953 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7954
7955 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
7956 clock.m2 = (pll_dw0 & 0xff) << 22;
7957 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7958 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
7959 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7960 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7961 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7962
dccbea3b 7963 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
7964}
7965
0e8ffe1b 7966static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7967 struct intel_crtc_state *pipe_config)
0e8ffe1b 7968{
6315b5d3 7969 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 7970 enum intel_display_power_domain power_domain;
0e8ffe1b 7971 uint32_t tmp;
1729050e 7972 bool ret;
0e8ffe1b 7973
1729050e
ID
7974 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
7975 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
7976 return false;
7977
e143a21c 7978 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 7979 pipe_config->shared_dpll = NULL;
eccb140b 7980
1729050e
ID
7981 ret = false;
7982
0e8ffe1b
SV
7983 tmp = I915_READ(PIPECONF(crtc->pipe));
7984 if (!(tmp & PIPECONF_ENABLE))
1729050e 7985 goto out;
0e8ffe1b 7986
9beb5fea
TU
7987 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7988 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
7989 switch (tmp & PIPECONF_BPC_MASK) {
7990 case PIPECONF_6BPC:
7991 pipe_config->pipe_bpp = 18;
7992 break;
7993 case PIPECONF_8BPC:
7994 pipe_config->pipe_bpp = 24;
7995 break;
7996 case PIPECONF_10BPC:
7997 pipe_config->pipe_bpp = 30;
7998 break;
7999 default:
8000 break;
8001 }
8002 }
8003
920a14b2 8004 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8005 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
SV
8006 pipe_config->limited_color_range = true;
8007
6315b5d3 8008 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
8009 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8010
1bd1bd80 8011 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8012 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8013
2fa2fe9a
SV
8014 i9xx_get_pfit_config(crtc, pipe_config);
8015
6315b5d3 8016 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 8017 /* No way to read it out on pipes B and C */
920a14b2 8018 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8019 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8020 else
8021 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
SV
8022 pipe_config->pixel_multiplier =
8023 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8024 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8025 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 8026 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 8027 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
6c49f241
SV
8028 tmp = I915_READ(DPLL(crtc->pipe));
8029 pipe_config->pixel_multiplier =
8030 ((tmp & SDVO_MULTIPLIER_MASK)
8031 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8032 } else {
8033 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8034 * port and will be fixed up in the encoder->get_config
8035 * function. */
8036 pipe_config->pixel_multiplier = 1;
8037 }
8bcc2795 8038 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8039 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8040 /*
8041 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8042 * on 830. Filter it out here so that we don't
8043 * report errors due to that.
8044 */
50a0bc90 8045 if (IS_I830(dev_priv))
1c4e0274
VS
8046 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8047
8bcc2795
SV
8048 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8049 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8050 } else {
8051 /* Mask out read-only status bits. */
8052 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8053 DPLL_PORTC_READY_MASK |
8054 DPLL_PORTB_READY_MASK);
8bcc2795 8055 }
6c49f241 8056
920a14b2 8057 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8058 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8059 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8060 vlv_crtc_clock_get(crtc, pipe_config);
8061 else
8062 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8063
0f64614d
VS
8064 /*
8065 * Normally the dotclock is filled in by the encoder .get_config()
8066 * but in case the pipe is enabled w/o any ports we need a sane
8067 * default.
8068 */
8069 pipe_config->base.adjusted_mode.crtc_clock =
8070 pipe_config->port_clock / pipe_config->pixel_multiplier;
8071
1729050e
ID
8072 ret = true;
8073
8074out:
8075 intel_display_power_put(dev_priv, power_domain);
8076
8077 return ret;
0e8ffe1b
SV
8078}
8079
c39055b0 8080static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 8081{
13d83a67 8082 struct intel_encoder *encoder;
1c1a24d2 8083 int i;
74cfd7ac 8084 u32 val, final;
13d83a67 8085 bool has_lvds = false;
199e5d79 8086 bool has_cpu_edp = false;
199e5d79 8087 bool has_panel = false;
99eb6a01
KP
8088 bool has_ck505 = false;
8089 bool can_ssc = false;
1c1a24d2 8090 bool using_ssc_source = false;
13d83a67
JB
8091
8092 /* We need to take the global config into account */
c39055b0 8093 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
8094 switch (encoder->type) {
8095 case INTEL_OUTPUT_LVDS:
8096 has_panel = true;
8097 has_lvds = true;
8098 break;
8099 case INTEL_OUTPUT_EDP:
8100 has_panel = true;
8f4f2797 8101 if (encoder->port == PORT_A)
199e5d79
KP
8102 has_cpu_edp = true;
8103 break;
6847d71b
PZ
8104 default:
8105 break;
13d83a67
JB
8106 }
8107 }
8108
6e266956 8109 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8110 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8111 can_ssc = has_ck505;
8112 } else {
8113 has_ck505 = false;
8114 can_ssc = true;
8115 }
8116
1c1a24d2
L
8117 /* Check if any DPLLs are using the SSC source */
8118 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8119 u32 temp = I915_READ(PCH_DPLL(i));
8120
8121 if (!(temp & DPLL_VCO_ENABLE))
8122 continue;
8123
8124 if ((temp & PLL_REF_INPUT_MASK) ==
8125 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8126 using_ssc_source = true;
8127 break;
8128 }
8129 }
8130
8131 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8132 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
8133
8134 /* Ironlake: try to setup display ref clock before DPLL
8135 * enabling. This is only under driver's control after
8136 * PCH B stepping, previous chipset stepping should be
8137 * ignoring this setting.
8138 */
74cfd7ac
CW
8139 val = I915_READ(PCH_DREF_CONTROL);
8140
8141 /* As we must carefully and slowly disable/enable each source in turn,
8142 * compute the final state we want first and check if we need to
8143 * make any changes at all.
8144 */
8145 final = val;
8146 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8147 if (has_ck505)
8148 final |= DREF_NONSPREAD_CK505_ENABLE;
8149 else
8150 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8151
8c07eb68 8152 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 8153 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 8154 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
8155
8156 if (has_panel) {
8157 final |= DREF_SSC_SOURCE_ENABLE;
8158
8159 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8160 final |= DREF_SSC1_ENABLE;
8161
8162 if (has_cpu_edp) {
8163 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8164 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8165 else
8166 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8167 } else
8168 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
8169 } else if (using_ssc_source) {
8170 final |= DREF_SSC_SOURCE_ENABLE;
8171 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
8172 }
8173
8174 if (final == val)
8175 return;
8176
13d83a67 8177 /* Always enable nonspread source */
74cfd7ac 8178 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8179
99eb6a01 8180 if (has_ck505)
74cfd7ac 8181 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8182 else
74cfd7ac 8183 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8184
199e5d79 8185 if (has_panel) {
74cfd7ac
CW
8186 val &= ~DREF_SSC_SOURCE_MASK;
8187 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8188
199e5d79 8189 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8190 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8191 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8192 val |= DREF_SSC1_ENABLE;
e77166b5 8193 } else
74cfd7ac 8194 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8195
8196 /* Get SSC going before enabling the outputs */
74cfd7ac 8197 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8198 POSTING_READ(PCH_DREF_CONTROL);
8199 udelay(200);
8200
74cfd7ac 8201 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8202
8203 /* Enable CPU source on CPU attached eDP */
199e5d79 8204 if (has_cpu_edp) {
99eb6a01 8205 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8206 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8207 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8208 } else
74cfd7ac 8209 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8210 } else
74cfd7ac 8211 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8212
74cfd7ac 8213 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8214 POSTING_READ(PCH_DREF_CONTROL);
8215 udelay(200);
8216 } else {
1c1a24d2 8217 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 8218
74cfd7ac 8219 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8220
8221 /* Turn off CPU output */
74cfd7ac 8222 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8223
74cfd7ac 8224 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8225 POSTING_READ(PCH_DREF_CONTROL);
8226 udelay(200);
8227
1c1a24d2
L
8228 if (!using_ssc_source) {
8229 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 8230
1c1a24d2
L
8231 /* Turn off the SSC source */
8232 val &= ~DREF_SSC_SOURCE_MASK;
8233 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 8234
1c1a24d2
L
8235 /* Turn off SSC1 */
8236 val &= ~DREF_SSC1_ENABLE;
8237
8238 I915_WRITE(PCH_DREF_CONTROL, val);
8239 POSTING_READ(PCH_DREF_CONTROL);
8240 udelay(200);
8241 }
13d83a67 8242 }
74cfd7ac
CW
8243
8244 BUG_ON(val != final);
13d83a67
JB
8245}
8246
f31f2d55 8247static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8248{
f31f2d55 8249 uint32_t tmp;
dde86e2d 8250
0ff066a9
PZ
8251 tmp = I915_READ(SOUTH_CHICKEN2);
8252 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8253 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8254
cf3598c2
ID
8255 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8256 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 8257 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8258
0ff066a9
PZ
8259 tmp = I915_READ(SOUTH_CHICKEN2);
8260 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8261 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8262
cf3598c2
ID
8263 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8264 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 8265 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8266}
8267
8268/* WaMPhyProgramming:hsw */
8269static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8270{
8271 uint32_t tmp;
dde86e2d
PZ
8272
8273 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8274 tmp &= ~(0xFF << 24);
8275 tmp |= (0x12 << 24);
8276 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8277
dde86e2d
PZ
8278 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8279 tmp |= (1 << 11);
8280 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8281
8282 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8283 tmp |= (1 << 11);
8284 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8285
dde86e2d
PZ
8286 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8287 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8288 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8289
8290 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8291 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8292 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8293
0ff066a9
PZ
8294 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8295 tmp &= ~(7 << 13);
8296 tmp |= (5 << 13);
8297 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8298
0ff066a9
PZ
8299 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8300 tmp &= ~(7 << 13);
8301 tmp |= (5 << 13);
8302 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8303
8304 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8305 tmp &= ~0xFF;
8306 tmp |= 0x1C;
8307 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8308
8309 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8310 tmp &= ~0xFF;
8311 tmp |= 0x1C;
8312 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8313
8314 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8315 tmp &= ~(0xFF << 16);
8316 tmp |= (0x1C << 16);
8317 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8318
8319 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8320 tmp &= ~(0xFF << 16);
8321 tmp |= (0x1C << 16);
8322 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8323
0ff066a9
PZ
8324 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8325 tmp |= (1 << 27);
8326 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8327
0ff066a9
PZ
8328 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8329 tmp |= (1 << 27);
8330 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8331
0ff066a9
PZ
8332 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8333 tmp &= ~(0xF << 28);
8334 tmp |= (4 << 28);
8335 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8336
0ff066a9
PZ
8337 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8338 tmp &= ~(0xF << 28);
8339 tmp |= (4 << 28);
8340 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8341}
8342
2fa86a1f
PZ
8343/* Implements 3 different sequences from BSpec chapter "Display iCLK
8344 * Programming" based on the parameters passed:
8345 * - Sequence to enable CLKOUT_DP
8346 * - Sequence to enable CLKOUT_DP without spread
8347 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8348 */
c39055b0
ACO
8349static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
8350 bool with_spread, bool with_fdi)
f31f2d55 8351{
2fa86a1f
PZ
8352 uint32_t reg, tmp;
8353
8354 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8355 with_spread = true;
4f8036a2
TU
8356 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
8357 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8358 with_fdi = false;
f31f2d55 8359
a580516d 8360 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8361
8362 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8363 tmp &= ~SBI_SSCCTL_DISABLE;
8364 tmp |= SBI_SSCCTL_PATHALT;
8365 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8366
8367 udelay(24);
8368
2fa86a1f
PZ
8369 if (with_spread) {
8370 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8371 tmp &= ~SBI_SSCCTL_PATHALT;
8372 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8373
2fa86a1f
PZ
8374 if (with_fdi) {
8375 lpt_reset_fdi_mphy(dev_priv);
8376 lpt_program_fdi_mphy(dev_priv);
8377 }
8378 }
dde86e2d 8379
4f8036a2 8380 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8381 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8382 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8383 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8384
a580516d 8385 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8386}
8387
47701c3b 8388/* Sequence to disable CLKOUT_DP */
c39055b0 8389static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 8390{
47701c3b
PZ
8391 uint32_t reg, tmp;
8392
a580516d 8393 mutex_lock(&dev_priv->sb_lock);
47701c3b 8394
4f8036a2 8395 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8396 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8397 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8398 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8399
8400 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8401 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8402 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8403 tmp |= SBI_SSCCTL_PATHALT;
8404 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8405 udelay(32);
8406 }
8407 tmp |= SBI_SSCCTL_DISABLE;
8408 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8409 }
8410
a580516d 8411 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8412}
8413
f7be2c21
VS
8414#define BEND_IDX(steps) ((50 + (steps)) / 5)
8415
8416static const uint16_t sscdivintphase[] = {
8417 [BEND_IDX( 50)] = 0x3B23,
8418 [BEND_IDX( 45)] = 0x3B23,
8419 [BEND_IDX( 40)] = 0x3C23,
8420 [BEND_IDX( 35)] = 0x3C23,
8421 [BEND_IDX( 30)] = 0x3D23,
8422 [BEND_IDX( 25)] = 0x3D23,
8423 [BEND_IDX( 20)] = 0x3E23,
8424 [BEND_IDX( 15)] = 0x3E23,
8425 [BEND_IDX( 10)] = 0x3F23,
8426 [BEND_IDX( 5)] = 0x3F23,
8427 [BEND_IDX( 0)] = 0x0025,
8428 [BEND_IDX( -5)] = 0x0025,
8429 [BEND_IDX(-10)] = 0x0125,
8430 [BEND_IDX(-15)] = 0x0125,
8431 [BEND_IDX(-20)] = 0x0225,
8432 [BEND_IDX(-25)] = 0x0225,
8433 [BEND_IDX(-30)] = 0x0325,
8434 [BEND_IDX(-35)] = 0x0325,
8435 [BEND_IDX(-40)] = 0x0425,
8436 [BEND_IDX(-45)] = 0x0425,
8437 [BEND_IDX(-50)] = 0x0525,
8438};
8439
8440/*
8441 * Bend CLKOUT_DP
8442 * steps -50 to 50 inclusive, in steps of 5
8443 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8444 * change in clock period = -(steps / 10) * 5.787 ps
8445 */
8446static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8447{
8448 uint32_t tmp;
8449 int idx = BEND_IDX(steps);
8450
8451 if (WARN_ON(steps % 5 != 0))
8452 return;
8453
8454 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8455 return;
8456
8457 mutex_lock(&dev_priv->sb_lock);
8458
8459 if (steps % 10 != 0)
8460 tmp = 0xAAAAAAAB;
8461 else
8462 tmp = 0x00000000;
8463 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8464
8465 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8466 tmp &= 0xffff0000;
8467 tmp |= sscdivintphase[idx];
8468 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8469
8470 mutex_unlock(&dev_priv->sb_lock);
8471}
8472
8473#undef BEND_IDX
8474
c39055b0 8475static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 8476{
bf8fa3d3
PZ
8477 struct intel_encoder *encoder;
8478 bool has_vga = false;
8479
c39055b0 8480 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
8481 switch (encoder->type) {
8482 case INTEL_OUTPUT_ANALOG:
8483 has_vga = true;
8484 break;
6847d71b
PZ
8485 default:
8486 break;
bf8fa3d3
PZ
8487 }
8488 }
8489
f7be2c21 8490 if (has_vga) {
c39055b0
ACO
8491 lpt_bend_clkout_dp(dev_priv, 0);
8492 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 8493 } else {
c39055b0 8494 lpt_disable_clkout_dp(dev_priv);
f7be2c21 8495 }
bf8fa3d3
PZ
8496}
8497
dde86e2d
PZ
8498/*
8499 * Initialize reference clocks when the driver loads
8500 */
c39055b0 8501void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 8502{
6e266956 8503 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 8504 ironlake_init_pch_refclk(dev_priv);
6e266956 8505 else if (HAS_PCH_LPT(dev_priv))
c39055b0 8506 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
8507}
8508
6ff93609 8509static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8510{
fac5e23e 8511 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
8512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8513 int pipe = intel_crtc->pipe;
c8203565
PZ
8514 uint32_t val;
8515
78114071 8516 val = 0;
c8203565 8517
6e3c9717 8518 switch (intel_crtc->config->pipe_bpp) {
c8203565 8519 case 18:
dfd07d72 8520 val |= PIPECONF_6BPC;
c8203565
PZ
8521 break;
8522 case 24:
dfd07d72 8523 val |= PIPECONF_8BPC;
c8203565
PZ
8524 break;
8525 case 30:
dfd07d72 8526 val |= PIPECONF_10BPC;
c8203565
PZ
8527 break;
8528 case 36:
dfd07d72 8529 val |= PIPECONF_12BPC;
c8203565
PZ
8530 break;
8531 default:
cc769b62
PZ
8532 /* Case prevented by intel_choose_pipe_bpp_dither. */
8533 BUG();
c8203565
PZ
8534 }
8535
6e3c9717 8536 if (intel_crtc->config->dither)
c8203565
PZ
8537 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8538
6e3c9717 8539 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8540 val |= PIPECONF_INTERLACED_ILK;
8541 else
8542 val |= PIPECONF_PROGRESSIVE;
8543
6e3c9717 8544 if (intel_crtc->config->limited_color_range)
3685a8f3 8545 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8546
c8203565
PZ
8547 I915_WRITE(PIPECONF(pipe), val);
8548 POSTING_READ(PIPECONF(pipe));
8549}
8550
6ff93609 8551static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8552{
fac5e23e 8553 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 8554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 8555 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 8556 u32 val = 0;
ee2b0b38 8557
391bf048 8558 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
8559 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8560
6e3c9717 8561 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8562 val |= PIPECONF_INTERLACED_ILK;
8563 else
8564 val |= PIPECONF_PROGRESSIVE;
8565
702e7a56
PZ
8566 I915_WRITE(PIPECONF(cpu_transcoder), val);
8567 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
8568}
8569
391bf048
JN
8570static void haswell_set_pipemisc(struct drm_crtc *crtc)
8571{
fac5e23e 8572 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 8573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b22ca995 8574 struct intel_crtc_state *config = intel_crtc->config;
756f85cf 8575
c56b89f1 8576 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
391bf048 8577 u32 val = 0;
756f85cf 8578
6e3c9717 8579 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8580 case 18:
8581 val |= PIPEMISC_DITHER_6_BPC;
8582 break;
8583 case 24:
8584 val |= PIPEMISC_DITHER_8_BPC;
8585 break;
8586 case 30:
8587 val |= PIPEMISC_DITHER_10_BPC;
8588 break;
8589 case 36:
8590 val |= PIPEMISC_DITHER_12_BPC;
8591 break;
8592 default:
8593 /* Case prevented by pipe_config_set_bpp. */
8594 BUG();
8595 }
8596
6e3c9717 8597 if (intel_crtc->config->dither)
756f85cf
PZ
8598 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8599
b22ca995
SS
8600 if (config->ycbcr420) {
8601 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
8602 PIPEMISC_YUV420_ENABLE |
8603 PIPEMISC_YUV420_MODE_FULL_BLEND;
8604 }
8605
391bf048 8606 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 8607 }
ee2b0b38
PZ
8608}
8609
d4b1931c
PZ
8610int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8611{
8612 /*
8613 * Account for spread spectrum to avoid
8614 * oversubscribing the link. Max center spread
8615 * is 2.5%; use 5% for safety's sake.
8616 */
8617 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8618 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8619}
8620
7429e9d4 8621static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8622{
7429e9d4 8623 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8624}
8625
b75ca6f6
ACO
8626static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8627 struct intel_crtc_state *crtc_state,
9e2c8475 8628 struct dpll *reduced_clock)
79e53945 8629{
de13a2e3 8630 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 8631 struct drm_device *dev = crtc->dev;
fac5e23e 8632 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 8633 u32 dpll, fp, fp2;
3d6e9ee0 8634 int factor;
79e53945 8635
c1858123 8636 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 8637 factor = 21;
3d6e9ee0 8638 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 8639 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8640 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 8641 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 8642 factor = 25;
190f68c5 8643 } else if (crtc_state->sdvo_tv_clock)
8febb297 8644 factor = 20;
c1858123 8645
b75ca6f6
ACO
8646 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8647
190f68c5 8648 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
8649 fp |= FP_CB_TUNE;
8650
8651 if (reduced_clock) {
8652 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 8653
b75ca6f6
ACO
8654 if (reduced_clock->m < factor * reduced_clock->n)
8655 fp2 |= FP_CB_TUNE;
8656 } else {
8657 fp2 = fp;
8658 }
9a7c7890 8659
5eddb70b 8660 dpll = 0;
2c07245f 8661
3d6e9ee0 8662 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
8663 dpll |= DPLLB_MODE_LVDS;
8664 else
8665 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8666
190f68c5 8667 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8668 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 8669
3d6e9ee0
VS
8670 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8671 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8672 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 8673
37a5650b 8674 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8675 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8676
7d7f8633
VS
8677 /*
8678 * The high speed IO clock is only really required for
8679 * SDVO/HDMI/DP, but we also enable it for CRT to make it
8680 * possible to share the DPLL between CRT and HDMI. Enabling
8681 * the clock needlessly does no real harm, except use up a
8682 * bit of power potentially.
8683 *
8684 * We'll limit this to IVB with 3 pipes, since it has only two
8685 * DPLLs and so DPLL sharing is the only way to get three pipes
8686 * driving PCH ports at the same time. On SNB we could do this,
8687 * and potentially avoid enabling the second DPLL, but it's not
8688 * clear if it''s a win or loss power wise. No point in doing
8689 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
8690 */
8691 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
8692 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
8693 dpll |= DPLL_SDVO_HIGH_SPEED;
8694
a07d6787 8695 /* compute bitmask from p1 value */
190f68c5 8696 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8697 /* also FPA1 */
190f68c5 8698 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8699
190f68c5 8700 switch (crtc_state->dpll.p2) {
a07d6787
EA
8701 case 5:
8702 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8703 break;
8704 case 7:
8705 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8706 break;
8707 case 10:
8708 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8709 break;
8710 case 14:
8711 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8712 break;
79e53945
JB
8713 }
8714
3d6e9ee0
VS
8715 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8716 intel_panel_use_ssc(dev_priv))
43565a06 8717 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8718 else
8719 dpll |= PLL_REF_INPUT_DREFCLK;
8720
b75ca6f6
ACO
8721 dpll |= DPLL_VCO_ENABLE;
8722
8723 crtc_state->dpll_hw_state.dpll = dpll;
8724 crtc_state->dpll_hw_state.fp0 = fp;
8725 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
8726}
8727
190f68c5
ACO
8728static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8729 struct intel_crtc_state *crtc_state)
de13a2e3 8730{
997c030c 8731 struct drm_device *dev = crtc->base.dev;
fac5e23e 8732 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8733 const struct intel_limit *limit;
997c030c 8734 int refclk = 120000;
de13a2e3 8735
dd3cd74a
ACO
8736 memset(&crtc_state->dpll_hw_state, 0,
8737 sizeof(crtc_state->dpll_hw_state));
8738
ded220e2
ACO
8739 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8740 if (!crtc_state->has_pch_encoder)
8741 return 0;
79e53945 8742
2d84d2b3 8743 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
8744 if (intel_panel_use_ssc(dev_priv)) {
8745 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8746 dev_priv->vbt.lvds_ssc_freq);
8747 refclk = dev_priv->vbt.lvds_ssc_freq;
8748 }
8749
8750 if (intel_is_dual_link_lvds(dev)) {
8751 if (refclk == 100000)
8752 limit = &intel_limits_ironlake_dual_lvds_100m;
8753 else
8754 limit = &intel_limits_ironlake_dual_lvds;
8755 } else {
8756 if (refclk == 100000)
8757 limit = &intel_limits_ironlake_single_lvds_100m;
8758 else
8759 limit = &intel_limits_ironlake_single_lvds;
8760 }
8761 } else {
8762 limit = &intel_limits_ironlake_dac;
8763 }
8764
364ee29d 8765 if (!crtc_state->clock_set &&
997c030c
ACO
8766 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8767 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
8768 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8769 return -EINVAL;
f47709a9 8770 }
79e53945 8771
cbaa3315 8772 ironlake_compute_dpll(crtc, crtc_state, NULL);
66e985c0 8773
efd38b68 8774 if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
43031788
CW
8775 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
8776 pipe_name(crtc->pipe));
ded220e2 8777 return -EINVAL;
3fb37703 8778 }
79e53945 8779
c8f7a0db 8780 return 0;
79e53945
JB
8781}
8782
eb14cb74
VS
8783static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8784 struct intel_link_m_n *m_n)
8785{
8786 struct drm_device *dev = crtc->base.dev;
fac5e23e 8787 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
8788 enum pipe pipe = crtc->pipe;
8789
8790 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8791 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8792 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8793 & ~TU_SIZE_MASK;
8794 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8795 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8796 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8797}
8798
8799static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8800 enum transcoder transcoder,
b95af8be
VK
8801 struct intel_link_m_n *m_n,
8802 struct intel_link_m_n *m2_n2)
72419203 8803{
6315b5d3 8804 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 8805 enum pipe pipe = crtc->pipe;
72419203 8806
6315b5d3 8807 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
8808 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8809 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8810 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8811 & ~TU_SIZE_MASK;
8812 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8813 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8814 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8815 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8816 * gen < 8) and if DRRS is supported (to make sure the
8817 * registers are not unnecessarily read).
8818 */
6315b5d3 8819 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 8820 crtc->config->has_drrs) {
b95af8be
VK
8821 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8822 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8823 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8824 & ~TU_SIZE_MASK;
8825 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8826 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8827 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8828 }
eb14cb74
VS
8829 } else {
8830 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8831 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8832 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8833 & ~TU_SIZE_MASK;
8834 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8835 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8836 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8837 }
8838}
8839
8840void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8841 struct intel_crtc_state *pipe_config)
eb14cb74 8842{
681a8504 8843 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8844 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8845 else
8846 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8847 &pipe_config->dp_m_n,
8848 &pipe_config->dp_m2_n2);
eb14cb74 8849}
72419203 8850
eb14cb74 8851static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8852 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8853{
8854 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8855 &pipe_config->fdi_m_n, NULL);
72419203
SV
8856}
8857
bd2e244f 8858static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8859 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8860{
8861 struct drm_device *dev = crtc->base.dev;
fac5e23e 8862 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
8863 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8864 uint32_t ps_ctrl = 0;
8865 int id = -1;
8866 int i;
bd2e244f 8867
a1b2278e
CK
8868 /* find scaler attached to this pipe */
8869 for (i = 0; i < crtc->num_scalers; i++) {
8870 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8871 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8872 id = i;
8873 pipe_config->pch_pfit.enabled = true;
8874 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8875 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8876 break;
8877 }
8878 }
bd2e244f 8879
a1b2278e
CK
8880 scaler_state->scaler_id = id;
8881 if (id >= 0) {
8882 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8883 } else {
8884 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8885 }
8886}
8887
5724dbd1
DL
8888static void
8889skylake_get_initial_plane_config(struct intel_crtc *crtc,
8890 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8891{
8892 struct drm_device *dev = crtc->base.dev;
fac5e23e 8893 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
8894 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8895 enum plane_id plane_id = plane->id;
eade6c89 8896 enum pipe pipe;
4036c78c 8897 u32 val, base, offset, stride_mult, tiling, alpha;
bc8d7dff 8898 int fourcc, pixel_format;
6761dd31 8899 unsigned int aligned_height;
bc8d7dff 8900 struct drm_framebuffer *fb;
1b842c89 8901 struct intel_framebuffer *intel_fb;
bc8d7dff 8902
eade6c89 8903 if (!plane->get_hw_state(plane, &pipe))
2924b8cc
VS
8904 return;
8905
eade6c89
VS
8906 WARN_ON(pipe != crtc->pipe);
8907
d9806c9f 8908 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8909 if (!intel_fb) {
bc8d7dff
DL
8910 DRM_DEBUG_KMS("failed to alloc fb\n");
8911 return;
8912 }
8913
1b842c89
DL
8914 fb = &intel_fb->base;
8915
d2e9f5fc
VS
8916 fb->dev = dev;
8917
282e83ef 8918 val = I915_READ(PLANE_CTL(pipe, plane_id));
42a7b088 8919
b5972776
JA
8920 if (INTEL_GEN(dev_priv) >= 11)
8921 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
8922 else
8923 pixel_format = val & PLANE_CTL_FORMAT_MASK;
4036c78c
JA
8924
8925 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
282e83ef 8926 alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
4036c78c
JA
8927 alpha &= PLANE_COLOR_ALPHA_MASK;
8928 } else {
8929 alpha = val & PLANE_CTL_ALPHA_MASK;
8930 }
8931
bc8d7dff 8932 fourcc = skl_format_to_fourcc(pixel_format,
4036c78c 8933 val & PLANE_CTL_ORDER_RGBX, alpha);
2f3f4763 8934 fb->format = drm_format_info(fourcc);
bc8d7dff 8935
40f46283
DL
8936 tiling = val & PLANE_CTL_TILED_MASK;
8937 switch (tiling) {
8938 case PLANE_CTL_TILED_LINEAR:
2f075565 8939 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
8940 break;
8941 case PLANE_CTL_TILED_X:
8942 plane_config->tiling = I915_TILING_X;
bae781b2 8943 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
8944 break;
8945 case PLANE_CTL_TILED_Y:
d9a51586 8946 plane_config->tiling = I915_TILING_Y;
53867b46 8947 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
2e2adb05
VS
8948 fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
8949 else
8950 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
8951 break;
8952 case PLANE_CTL_TILED_YF:
53867b46 8953 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
2e2adb05
VS
8954 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
8955 else
8956 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
8957 break;
8958 default:
8959 MISSING_CASE(tiling);
8960 goto error;
8961 }
8962
f559156c
VS
8963 /*
8964 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
8965 * while i915 HW rotation is clockwise, thats why this swapping.
8966 */
8967 switch (val & PLANE_CTL_ROTATE_MASK) {
8968 case PLANE_CTL_ROTATE_0:
8969 plane_config->rotation = DRM_MODE_ROTATE_0;
8970 break;
8971 case PLANE_CTL_ROTATE_90:
8972 plane_config->rotation = DRM_MODE_ROTATE_270;
8973 break;
8974 case PLANE_CTL_ROTATE_180:
8975 plane_config->rotation = DRM_MODE_ROTATE_180;
8976 break;
8977 case PLANE_CTL_ROTATE_270:
8978 plane_config->rotation = DRM_MODE_ROTATE_90;
8979 break;
8980 }
8981
8982 if (INTEL_GEN(dev_priv) >= 10 &&
8983 val & PLANE_CTL_FLIP_HORIZONTAL)
8984 plane_config->rotation |= DRM_MODE_REFLECT_X;
8985
282e83ef 8986 base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
bc8d7dff
DL
8987 plane_config->base = base;
8988
282e83ef 8989 offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
bc8d7dff 8990
282e83ef 8991 val = I915_READ(PLANE_SIZE(pipe, plane_id));
bc8d7dff
DL
8992 fb->height = ((val >> 16) & 0xfff) + 1;
8993 fb->width = ((val >> 0) & 0x1fff) + 1;
8994
282e83ef 8995 val = I915_READ(PLANE_STRIDE(pipe, plane_id));
d88c4afd 8996 stride_mult = intel_fb_stride_alignment(fb, 0);
bc8d7dff
DL
8997 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8998
d88c4afd 8999 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 9000
f37b5c2b 9001 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff 9002
282e83ef
VS
9003 DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9004 crtc->base.name, plane->base.name, fb->width, fb->height,
272725c7 9005 fb->format->cpp[0] * 8, base, fb->pitches[0],
bc8d7dff
DL
9006 plane_config->size);
9007
2d14030b 9008 plane_config->fb = intel_fb;
bc8d7dff
DL
9009 return;
9010
9011error:
d1a3a036 9012 kfree(intel_fb);
bc8d7dff
DL
9013}
9014
2fa2fe9a 9015static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9016 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
9017{
9018 struct drm_device *dev = crtc->base.dev;
fac5e23e 9019 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
9020 uint32_t tmp;
9021
9022 tmp = I915_READ(PF_CTL(crtc->pipe));
9023
9024 if (tmp & PF_ENABLE) {
fd4daa9c 9025 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
SV
9026 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9027 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
SV
9028
9029 /* We currently do not free assignements of panel fitters on
9030 * ivb/hsw (since we don't use the higher upscaling modes which
9031 * differentiates them) so just WARN about this case for now. */
5db94019 9032 if (IS_GEN7(dev_priv)) {
cb8b2a30
SV
9033 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9034 PF_PIPE_SEL_IVB(crtc->pipe));
9035 }
2fa2fe9a 9036 }
79e53945
JB
9037}
9038
0e8ffe1b 9039static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9040 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
9041{
9042 struct drm_device *dev = crtc->base.dev;
fac5e23e 9043 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9044 enum intel_display_power_domain power_domain;
0e8ffe1b 9045 uint32_t tmp;
1729050e 9046 bool ret;
0e8ffe1b 9047
1729050e
ID
9048 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9049 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9050 return false;
9051
e143a21c 9052 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9053 pipe_config->shared_dpll = NULL;
eccb140b 9054
1729050e 9055 ret = false;
0e8ffe1b
SV
9056 tmp = I915_READ(PIPECONF(crtc->pipe));
9057 if (!(tmp & PIPECONF_ENABLE))
1729050e 9058 goto out;
0e8ffe1b 9059
42571aef
VS
9060 switch (tmp & PIPECONF_BPC_MASK) {
9061 case PIPECONF_6BPC:
9062 pipe_config->pipe_bpp = 18;
9063 break;
9064 case PIPECONF_8BPC:
9065 pipe_config->pipe_bpp = 24;
9066 break;
9067 case PIPECONF_10BPC:
9068 pipe_config->pipe_bpp = 30;
9069 break;
9070 case PIPECONF_12BPC:
9071 pipe_config->pipe_bpp = 36;
9072 break;
9073 default:
9074 break;
9075 }
9076
b5a9fa09
SV
9077 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9078 pipe_config->limited_color_range = true;
9079
ab9412ba 9080 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9081 struct intel_shared_dpll *pll;
8106ddbd 9082 enum intel_dpll_id pll_id;
66e985c0 9083
88adfff1
SV
9084 pipe_config->has_pch_encoder = true;
9085
627eb5a3
SV
9086 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9087 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9088 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
SV
9089
9090 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9091
2d1fe073 9092 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
9093 /*
9094 * The pipe->pch transcoder and pch transcoder->pll
9095 * mapping is fixed.
9096 */
8106ddbd 9097 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
SV
9098 } else {
9099 tmp = I915_READ(PCH_DPLL_SEL);
9100 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9101 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9102 else
8106ddbd 9103 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9104 }
66e985c0 9105
8106ddbd
ACO
9106 pipe_config->shared_dpll =
9107 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9108 pll = pipe_config->shared_dpll;
66e985c0 9109
ee1398ba
LDM
9110 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
9111 &pipe_config->dpll_hw_state));
c93f54cf
SV
9112
9113 tmp = pipe_config->dpll_hw_state.dpll;
9114 pipe_config->pixel_multiplier =
9115 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9116 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9117
9118 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
SV
9119 } else {
9120 pipe_config->pixel_multiplier = 1;
627eb5a3
SV
9121 }
9122
1bd1bd80 9123 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 9124 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9125
2fa2fe9a
SV
9126 ironlake_get_pfit_config(crtc, pipe_config);
9127
1729050e
ID
9128 ret = true;
9129
9130out:
9131 intel_display_power_put(dev_priv, power_domain);
9132
9133 return ret;
0e8ffe1b
SV
9134}
9135
be256dc7
PZ
9136static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9137{
91c8a326 9138 struct drm_device *dev = &dev_priv->drm;
be256dc7 9139 struct intel_crtc *crtc;
be256dc7 9140
d3fcc808 9141 for_each_intel_crtc(dev, crtc)
e2c719b7 9142 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9143 pipe_name(crtc->pipe));
9144
75e39688 9145 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL2),
9c3a16c8 9146 "Display power well on\n");
e2c719b7 9147 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9148 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9149 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 9150 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 9151 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9152 "CPU PWM1 enabled\n");
772c2a51 9153 if (IS_HASWELL(dev_priv))
e2c719b7 9154 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9155 "CPU PWM2 enabled\n");
e2c719b7 9156 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9157 "PCH PWM1 enabled\n");
e2c719b7 9158 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9159 "Utility pin enabled\n");
e2c719b7 9160 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9161
9926ada1
PZ
9162 /*
9163 * In theory we can still leave IRQs enabled, as long as only the HPD
9164 * interrupts remain enabled. We used to check for that, but since it's
9165 * gen-specific and since we only disable LCPLL after we fully disable
9166 * the interrupts, the check below should be enough.
9167 */
e2c719b7 9168 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9169}
9170
9ccd5aeb
PZ
9171static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9172{
772c2a51 9173 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
9174 return I915_READ(D_COMP_HSW);
9175 else
9176 return I915_READ(D_COMP_BDW);
9177}
9178
3c4c9b81
PZ
9179static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9180{
772c2a51 9181 if (IS_HASWELL(dev_priv)) {
9f817501 9182 mutex_lock(&dev_priv->pcu_lock);
3c4c9b81
PZ
9183 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9184 val))
79cf219a 9185 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
9f817501 9186 mutex_unlock(&dev_priv->pcu_lock);
3c4c9b81 9187 } else {
9ccd5aeb
PZ
9188 I915_WRITE(D_COMP_BDW, val);
9189 POSTING_READ(D_COMP_BDW);
3c4c9b81 9190 }
be256dc7
PZ
9191}
9192
9193/*
9194 * This function implements pieces of two sequences from BSpec:
9195 * - Sequence for display software to disable LCPLL
9196 * - Sequence for display software to allow package C8+
9197 * The steps implemented here are just the steps that actually touch the LCPLL
9198 * register. Callers should take care of disabling all the display engine
9199 * functions, doing the mode unset, fixing interrupts, etc.
9200 */
6ff58d53
PZ
9201static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9202 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9203{
9204 uint32_t val;
9205
9206 assert_can_disable_lcpll(dev_priv);
9207
9208 val = I915_READ(LCPLL_CTL);
9209
9210 if (switch_to_fclk) {
9211 val |= LCPLL_CD_SOURCE_FCLK;
9212 I915_WRITE(LCPLL_CTL, val);
9213
f53dd63f
ID
9214 if (wait_for_us(I915_READ(LCPLL_CTL) &
9215 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
9216 DRM_ERROR("Switching to FCLK failed\n");
9217
9218 val = I915_READ(LCPLL_CTL);
9219 }
9220
9221 val |= LCPLL_PLL_DISABLE;
9222 I915_WRITE(LCPLL_CTL, val);
9223 POSTING_READ(LCPLL_CTL);
9224
24d8441d 9225 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
9226 DRM_ERROR("LCPLL still locked\n");
9227
9ccd5aeb 9228 val = hsw_read_dcomp(dev_priv);
be256dc7 9229 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9230 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9231 ndelay(100);
9232
9ccd5aeb
PZ
9233 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9234 1))
be256dc7
PZ
9235 DRM_ERROR("D_COMP RCOMP still in progress\n");
9236
9237 if (allow_power_down) {
9238 val = I915_READ(LCPLL_CTL);
9239 val |= LCPLL_POWER_DOWN_ALLOW;
9240 I915_WRITE(LCPLL_CTL, val);
9241 POSTING_READ(LCPLL_CTL);
9242 }
9243}
9244
9245/*
9246 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9247 * source.
9248 */
6ff58d53 9249static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9250{
9251 uint32_t val;
9252
9253 val = I915_READ(LCPLL_CTL);
9254
9255 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9256 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9257 return;
9258
a8a8bd54
PZ
9259 /*
9260 * Make sure we're not on PC8 state before disabling PC8, otherwise
9261 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9262 */
59bad947 9263 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9264
be256dc7
PZ
9265 if (val & LCPLL_POWER_DOWN_ALLOW) {
9266 val &= ~LCPLL_POWER_DOWN_ALLOW;
9267 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9268 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9269 }
9270
9ccd5aeb 9271 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9272 val |= D_COMP_COMP_FORCE;
9273 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9274 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9275
9276 val = I915_READ(LCPLL_CTL);
9277 val &= ~LCPLL_PLL_DISABLE;
9278 I915_WRITE(LCPLL_CTL, val);
9279
93220c08
CW
9280 if (intel_wait_for_register(dev_priv,
9281 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9282 5))
be256dc7
PZ
9283 DRM_ERROR("LCPLL not locked yet\n");
9284
9285 if (val & LCPLL_CD_SOURCE_FCLK) {
9286 val = I915_READ(LCPLL_CTL);
9287 val &= ~LCPLL_CD_SOURCE_FCLK;
9288 I915_WRITE(LCPLL_CTL, val);
9289
f53dd63f
ID
9290 if (wait_for_us((I915_READ(LCPLL_CTL) &
9291 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
9292 DRM_ERROR("Switching back to LCPLL failed\n");
9293 }
215733fa 9294
59bad947 9295 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
cfddadc9 9296
4c75b940 9297 intel_update_cdclk(dev_priv);
cfddadc9 9298 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
be256dc7
PZ
9299}
9300
765dab67
PZ
9301/*
9302 * Package states C8 and deeper are really deep PC states that can only be
9303 * reached when all the devices on the system allow it, so even if the graphics
9304 * device allows PC8+, it doesn't mean the system will actually get to these
9305 * states. Our driver only allows PC8+ when going into runtime PM.
9306 *
9307 * The requirements for PC8+ are that all the outputs are disabled, the power
9308 * well is disabled and most interrupts are disabled, and these are also
9309 * requirements for runtime PM. When these conditions are met, we manually do
9310 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9311 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9312 * hang the machine.
9313 *
9314 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9315 * the state of some registers, so when we come back from PC8+ we need to
9316 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9317 * need to take care of the registers kept by RC6. Notice that this happens even
9318 * if we don't put the device in PCI D3 state (which is what currently happens
9319 * because of the runtime PM support).
9320 *
9321 * For more, read "Display Sequences for Package C8" on the hardware
9322 * documentation.
9323 */
a14cb6fc 9324void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9325{
c67a470b
PZ
9326 uint32_t val;
9327
c67a470b
PZ
9328 DRM_DEBUG_KMS("Enabling package C8+\n");
9329
4f8036a2 9330 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
9331 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9332 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9333 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9334 }
9335
c39055b0 9336 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
9337 hsw_disable_lcpll(dev_priv, true, true);
9338}
9339
a14cb6fc 9340void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9341{
c67a470b
PZ
9342 uint32_t val;
9343
c67a470b
PZ
9344 DRM_DEBUG_KMS("Disabling package C8+\n");
9345
9346 hsw_restore_lcpll(dev_priv);
c39055b0 9347 lpt_init_pch_refclk(dev_priv);
c67a470b 9348
4f8036a2 9349 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
9350 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9351 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9352 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9353 }
c67a470b
PZ
9354}
9355
190f68c5
ACO
9356static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9357 struct intel_crtc_state *crtc_state)
09b4ddf9 9358{
5a0b385e
VS
9359 struct intel_atomic_state *state =
9360 to_intel_atomic_state(crtc_state->base.state);
9361
d7edc4e5 9362 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
44a126ba 9363 struct intel_encoder *encoder =
5a0b385e 9364 intel_get_crtc_new_encoder(state, crtc_state);
44a126ba
PZ
9365
9366 if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
43031788
CW
9367 DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
9368 pipe_name(crtc->pipe));
af3997b5 9369 return -EINVAL;
44a126ba 9370 }
af3997b5 9371 }
716c2e55 9372
c8f7a0db 9373 return 0;
79e53945
JB
9374}
9375
8b0f7e06
KM
9376static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
9377 enum port port,
9378 struct intel_crtc_state *pipe_config)
9379{
9380 enum intel_dpll_id id;
9381 u32 temp;
9382
9383 temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
dfbd4508 9384 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
8b0f7e06
KM
9385
9386 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
9387 return;
9388
9389 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9390}
9391
970888e7
PZ
9392static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
9393 enum port port,
9394 struct intel_crtc_state *pipe_config)
9395{
9396 enum intel_dpll_id id;
9397 u32 temp;
9398
9399 /* TODO: TBT pll not implemented. */
9400 switch (port) {
9401 case PORT_A:
9402 case PORT_B:
9403 temp = I915_READ(DPCLKA_CFGCR0_ICL) &
9404 DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
9405 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
9406
9407 if (WARN_ON(id != DPLL_ID_ICL_DPLL0 && id != DPLL_ID_ICL_DPLL1))
9408 return;
9409 break;
9410 case PORT_C:
9411 id = DPLL_ID_ICL_MGPLL1;
9412 break;
9413 case PORT_D:
9414 id = DPLL_ID_ICL_MGPLL2;
9415 break;
9416 case PORT_E:
9417 id = DPLL_ID_ICL_MGPLL3;
9418 break;
9419 case PORT_F:
9420 id = DPLL_ID_ICL_MGPLL4;
9421 break;
9422 default:
9423 MISSING_CASE(port);
9424 return;
9425 }
9426
9427 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
9428}
9429
3760b59c
S
9430static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9431 enum port port,
9432 struct intel_crtc_state *pipe_config)
9433{
8106ddbd
ACO
9434 enum intel_dpll_id id;
9435
3760b59c
S
9436 switch (port) {
9437 case PORT_A:
08250c4b 9438 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
9439 break;
9440 case PORT_B:
08250c4b 9441 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9442 break;
9443 case PORT_C:
08250c4b 9444 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9445 break;
9446 default:
9447 DRM_ERROR("Incorrect port type\n");
8106ddbd 9448 return;
3760b59c 9449 }
8106ddbd
ACO
9450
9451 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9452}
9453
96b7dfb7
S
9454static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9455 enum port port,
5cec258b 9456 struct intel_crtc_state *pipe_config)
96b7dfb7 9457{
8106ddbd 9458 enum intel_dpll_id id;
a3c988ea 9459 u32 temp;
96b7dfb7
S
9460
9461 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 9462 id = temp >> (port * 3 + 1);
96b7dfb7 9463
c856052a 9464 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 9465 return;
8106ddbd
ACO
9466
9467 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9468}
9469
7d2c8175
DL
9470static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9471 enum port port,
5cec258b 9472 struct intel_crtc_state *pipe_config)
7d2c8175 9473{
8106ddbd 9474 enum intel_dpll_id id;
c856052a 9475 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 9476
c856052a 9477 switch (ddi_pll_sel) {
7d2c8175 9478 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9479 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9480 break;
9481 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9482 id = DPLL_ID_WRPLL2;
7d2c8175 9483 break;
00490c22 9484 case PORT_CLK_SEL_SPLL:
8106ddbd 9485 id = DPLL_ID_SPLL;
79bd23da 9486 break;
9d16da65
ACO
9487 case PORT_CLK_SEL_LCPLL_810:
9488 id = DPLL_ID_LCPLL_810;
9489 break;
9490 case PORT_CLK_SEL_LCPLL_1350:
9491 id = DPLL_ID_LCPLL_1350;
9492 break;
9493 case PORT_CLK_SEL_LCPLL_2700:
9494 id = DPLL_ID_LCPLL_2700;
9495 break;
8106ddbd 9496 default:
c856052a 9497 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
9498 /* fall through */
9499 case PORT_CLK_SEL_NONE:
8106ddbd 9500 return;
7d2c8175 9501 }
8106ddbd
ACO
9502
9503 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9504}
9505
cf30429e
JN
9506static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9507 struct intel_crtc_state *pipe_config,
d8fc70b7 9508 u64 *power_domain_mask)
cf30429e
JN
9509{
9510 struct drm_device *dev = crtc->base.dev;
fac5e23e 9511 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
9512 enum intel_display_power_domain power_domain;
9513 u32 tmp;
9514
d9a7bc67
ID
9515 /*
9516 * The pipe->transcoder mapping is fixed with the exception of the eDP
9517 * transcoder handled below.
9518 */
cf30429e
JN
9519 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9520
9521 /*
9522 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9523 * consistency and less surprising code; it's in always on power).
9524 */
9525 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9526 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9527 enum pipe trans_edp_pipe;
9528 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9529 default:
9530 WARN(1, "unknown pipe linked to edp transcoder\n");
f0d759f0 9531 /* fall through */
cf30429e
JN
9532 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9533 case TRANS_DDI_EDP_INPUT_A_ON:
9534 trans_edp_pipe = PIPE_A;
9535 break;
9536 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9537 trans_edp_pipe = PIPE_B;
9538 break;
9539 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9540 trans_edp_pipe = PIPE_C;
9541 break;
9542 }
9543
9544 if (trans_edp_pipe == crtc->pipe)
9545 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9546 }
9547
9548 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9549 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9550 return false;
d8fc70b7 9551 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e
JN
9552
9553 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9554
9555 return tmp & PIPECONF_ENABLE;
9556}
9557
4d1de975
JN
9558static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9559 struct intel_crtc_state *pipe_config,
d8fc70b7 9560 u64 *power_domain_mask)
4d1de975
JN
9561{
9562 struct drm_device *dev = crtc->base.dev;
fac5e23e 9563 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
9564 enum intel_display_power_domain power_domain;
9565 enum port port;
9566 enum transcoder cpu_transcoder;
9567 u32 tmp;
9568
4d1de975
JN
9569 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9570 if (port == PORT_A)
9571 cpu_transcoder = TRANSCODER_DSI_A;
9572 else
9573 cpu_transcoder = TRANSCODER_DSI_C;
9574
9575 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9576 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9577 continue;
d8fc70b7 9578 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 9579
db18b6a6
ID
9580 /*
9581 * The PLL needs to be enabled with a valid divider
9582 * configuration, otherwise accessing DSI registers will hang
9583 * the machine. See BSpec North Display Engine
9584 * registers/MIPI[BXT]. We can break out here early, since we
9585 * need the same DSI PLL to be enabled for both DSI ports.
9586 */
e518634b 9587 if (!bxt_dsi_pll_is_enabled(dev_priv))
db18b6a6
ID
9588 break;
9589
4d1de975
JN
9590 /* XXX: this works for video mode only */
9591 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9592 if (!(tmp & DPI_ENABLE))
9593 continue;
9594
9595 tmp = I915_READ(MIPI_CTRL(port));
9596 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9597 continue;
9598
9599 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
9600 break;
9601 }
9602
d7edc4e5 9603 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
9604}
9605
26804afd 9606static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9607 struct intel_crtc_state *pipe_config)
26804afd 9608{
6315b5d3 9609 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 9610 struct intel_shared_dpll *pll;
26804afd
SV
9611 enum port port;
9612 uint32_t tmp;
9613
9614 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9615
9616 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9617
970888e7
PZ
9618 if (IS_ICELAKE(dev_priv))
9619 icelake_get_ddi_pll(dev_priv, port, pipe_config);
9620 else if (IS_CANNONLAKE(dev_priv))
8b0f7e06
KM
9621 cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
9622 else if (IS_GEN9_BC(dev_priv))
96b7dfb7 9623 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 9624 else if (IS_GEN9_LP(dev_priv))
3760b59c 9625 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9626 else
9627 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9628
8106ddbd
ACO
9629 pll = pipe_config->shared_dpll;
9630 if (pll) {
ee1398ba
LDM
9631 WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
9632 &pipe_config->dpll_hw_state));
d452c5b6
SV
9633 }
9634
26804afd
SV
9635 /*
9636 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9637 * DDI E. So just check whether this pipe is wired to DDI E and whether
9638 * the PCH transcoder is on.
9639 */
6315b5d3 9640 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 9641 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
SV
9642 pipe_config->has_pch_encoder = true;
9643
9644 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9645 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9646 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9647
9648 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9649 }
9650}
9651
0e8ffe1b 9652static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9653 struct intel_crtc_state *pipe_config)
0e8ffe1b 9654{
6315b5d3 9655 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9656 enum intel_display_power_domain power_domain;
d8fc70b7 9657 u64 power_domain_mask;
cf30429e 9658 bool active;
0e8ffe1b 9659
e79dfb51 9660 intel_crtc_init_scalers(crtc, pipe_config);
5fb9dadf 9661
1729050e
ID
9662 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9663 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9664 return false;
d8fc70b7 9665 power_domain_mask = BIT_ULL(power_domain);
1729050e 9666
8106ddbd 9667 pipe_config->shared_dpll = NULL;
c0d43d62 9668
cf30429e 9669 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 9670
cc3f90f0 9671 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
9672 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
9673 WARN_ON(active);
9674 active = true;
4d1de975
JN
9675 }
9676
cf30429e 9677 if (!active)
1729050e 9678 goto out;
0e8ffe1b 9679
d7edc4e5 9680 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
9681 haswell_get_ddi_port_state(crtc, pipe_config);
9682 intel_get_pipe_timings(crtc, pipe_config);
9683 }
627eb5a3 9684
bc58be60 9685 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9686
05dc698c
LL
9687 pipe_config->gamma_mode =
9688 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9689
bd30ca2d 9690 if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
b22ca995
SS
9691 u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
9692 bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
9693
bd30ca2d 9694 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
b22ca995
SS
9695 bool blend_mode_420 = tmp &
9696 PIPEMISC_YUV420_MODE_FULL_BLEND;
9697
9698 pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
9699 if (pipe_config->ycbcr420 != clrspace_yuv ||
9700 pipe_config->ycbcr420 != blend_mode_420)
9701 DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
9702 } else if (clrspace_yuv) {
9703 DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
9704 }
9705 }
9706
1729050e
ID
9707 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9708 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
d8fc70b7 9709 power_domain_mask |= BIT_ULL(power_domain);
6315b5d3 9710 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 9711 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9712 else
1c132b44 9713 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9714 }
88adfff1 9715
24f28450
ML
9716 if (hsw_crtc_supports_ips(crtc)) {
9717 if (IS_HASWELL(dev_priv))
9718 pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
9719 else {
9720 /*
9721 * We cannot readout IPS state on broadwell, set to
9722 * true so we can set it to a defined state on first
9723 * commit.
9724 */
9725 pipe_config->ips_enabled = true;
9726 }
9727 }
9728
4d1de975
JN
9729 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
9730 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
9731 pipe_config->pixel_multiplier =
9732 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9733 } else {
9734 pipe_config->pixel_multiplier = 1;
9735 }
6c49f241 9736
1729050e
ID
9737out:
9738 for_each_power_domain(power_domain, power_domain_mask)
9739 intel_display_power_put(dev_priv, power_domain);
9740
cf30429e 9741 return active;
0e8ffe1b
SV
9742}
9743
cd5dcbf1 9744static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
9745{
9746 struct drm_i915_private *dev_priv =
9747 to_i915(plane_state->base.plane->dev);
9748 const struct drm_framebuffer *fb = plane_state->base.fb;
9749 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9750 u32 base;
9751
9752 if (INTEL_INFO(dev_priv)->cursor_needs_physical)
9753 base = obj->phys_handle->busaddr;
9754 else
9755 base = intel_plane_ggtt_offset(plane_state);
9756
c11ada07 9757 base += plane_state->color_plane[0].offset;
1e7b4fd8 9758
1cecc830
VS
9759 /* ILK+ do this automagically */
9760 if (HAS_GMCH_DISPLAY(dev_priv) &&
a82256bc 9761 plane_state->base.rotation & DRM_MODE_ROTATE_180)
1cecc830
VS
9762 base += (plane_state->base.crtc_h *
9763 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
9764
9765 return base;
9766}
9767
ed270223
VS
9768static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
9769{
9770 int x = plane_state->base.crtc_x;
9771 int y = plane_state->base.crtc_y;
9772 u32 pos = 0;
9773
9774 if (x < 0) {
9775 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9776 x = -x;
9777 }
9778 pos |= x << CURSOR_X_SHIFT;
9779
9780 if (y < 0) {
9781 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9782 y = -y;
9783 }
9784 pos |= y << CURSOR_Y_SHIFT;
9785
9786 return pos;
9787}
9788
3637ecf0
VS
9789static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
9790{
9791 const struct drm_mode_config *config =
9792 &plane_state->base.plane->dev->mode_config;
9793 int width = plane_state->base.crtc_w;
9794 int height = plane_state->base.crtc_h;
9795
9796 return width > 0 && width <= config->cursor_width &&
9797 height > 0 && height <= config->cursor_height;
9798}
9799
fce8d235 9800static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
659056f2
VS
9801{
9802 const struct drm_framebuffer *fb = plane_state->base.fb;
df79cf44 9803 unsigned int rotation = plane_state->base.rotation;
1e7b4fd8
VS
9804 int src_x, src_y;
9805 u32 offset;
3f6d5ba1 9806 int ret;
fce8d235
VS
9807
9808 intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
9809 plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
9810
3f6d5ba1
VS
9811 ret = intel_plane_check_stride(plane_state);
9812 if (ret)
9813 return ret;
9814
fce8d235
VS
9815 src_x = plane_state->base.src_x >> 16;
9816 src_y = plane_state->base.src_y >> 16;
9817
9818 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
9819 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
9820 plane_state, 0);
9821
9822 if (src_x != 0 || src_y != 0) {
9823 DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
9824 return -EINVAL;
9825 }
9826
9827 plane_state->color_plane[0].offset = offset;
9828
9829 return 0;
9830}
9831
9832static int intel_check_cursor(struct intel_crtc_state *crtc_state,
9833 struct intel_plane_state *plane_state)
9834{
9835 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9836 int ret;
9837
4e0b83a5
VS
9838 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
9839 DRM_DEBUG_KMS("cursor cannot be tiled\n");
9840 return -EINVAL;
9841 }
9842
a01cb8ba
VS
9843 ret = drm_atomic_helper_check_plane_state(&plane_state->base,
9844 &crtc_state->base,
a01cb8ba
VS
9845 DRM_PLANE_HELPER_NO_SCALING,
9846 DRM_PLANE_HELPER_NO_SCALING,
9847 true, true);
659056f2
VS
9848 if (ret)
9849 return ret;
9850
4e0b83a5 9851 if (!plane_state->base.visible)
659056f2
VS
9852 return 0;
9853
4e0b83a5
VS
9854 ret = intel_plane_check_src_coordinates(plane_state);
9855 if (ret)
9856 return ret;
1e7b4fd8 9857
fce8d235
VS
9858 ret = intel_cursor_check_surface(plane_state);
9859 if (ret)
9860 return ret;
1e7b4fd8 9861
659056f2
VS
9862 return 0;
9863}
9864
ddd5713d
VS
9865static unsigned int
9866i845_cursor_max_stride(struct intel_plane *plane,
9867 u32 pixel_format, u64 modifier,
9868 unsigned int rotation)
9869{
9870 return 2048;
9871}
9872
292889e1
VS
9873static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
9874 const struct intel_plane_state *plane_state)
9875{
292889e1
VS
9876 return CURSOR_ENABLE |
9877 CURSOR_GAMMA_ENABLE |
9878 CURSOR_FORMAT_ARGB |
df79cf44 9879 CURSOR_STRIDE(plane_state->color_plane[0].stride);
292889e1
VS
9880}
9881
659056f2
VS
9882static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
9883{
659056f2 9884 int width = plane_state->base.crtc_w;
659056f2
VS
9885
9886 /*
9887 * 845g/865g are only limited by the width of their cursors,
9888 * the height is arbitrary up to the precision of the register.
9889 */
3637ecf0 9890 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
9891}
9892
eb0f5044 9893static int i845_check_cursor(struct intel_crtc_state *crtc_state,
659056f2
VS
9894 struct intel_plane_state *plane_state)
9895{
9896 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2
VS
9897 int ret;
9898
9899 ret = intel_check_cursor(crtc_state, plane_state);
9900 if (ret)
9901 return ret;
9902
9903 /* if we want to turn off the cursor ignore width and height */
1e1bb871 9904 if (!fb)
659056f2
VS
9905 return 0;
9906
9907 /* Check for which cursor types we support */
9908 if (!i845_cursor_size_ok(plane_state)) {
9909 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
9910 plane_state->base.crtc_w,
9911 plane_state->base.crtc_h);
9912 return -EINVAL;
9913 }
9914
df79cf44
VS
9915 WARN_ON(plane_state->base.visible &&
9916 plane_state->color_plane[0].stride != fb->pitches[0]);
9917
1e1bb871 9918 switch (fb->pitches[0]) {
292889e1
VS
9919 case 256:
9920 case 512:
9921 case 1024:
9922 case 2048:
9923 break;
1e1bb871
VS
9924 default:
9925 DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
9926 fb->pitches[0]);
9927 return -EINVAL;
292889e1
VS
9928 }
9929
659056f2
VS
9930 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
9931
9932 return 0;
292889e1
VS
9933}
9934
b2d03b0d
VS
9935static void i845_update_cursor(struct intel_plane *plane,
9936 const struct intel_crtc_state *crtc_state,
55a08b3f 9937 const struct intel_plane_state *plane_state)
560b85bb 9938{
cd5dcbf1 9939 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
9940 u32 cntl = 0, base = 0, pos = 0, size = 0;
9941 unsigned long irqflags;
560b85bb 9942
936e71e3 9943 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
9944 unsigned int width = plane_state->base.crtc_w;
9945 unsigned int height = plane_state->base.crtc_h;
dc41c154 9946
a0864d59 9947 cntl = plane_state->ctl;
dc41c154 9948 size = (height << 12) | width;
560b85bb 9949
b2d03b0d
VS
9950 base = intel_cursor_base(plane_state);
9951 pos = intel_cursor_position(plane_state);
4b0e333e 9952 }
560b85bb 9953
b2d03b0d 9954 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 9955
e11ffddb
VS
9956 /* On these chipsets we can only modify the base/size/stride
9957 * whilst the cursor is disabled.
9958 */
9959 if (plane->cursor.base != base ||
9960 plane->cursor.size != size ||
9961 plane->cursor.cntl != cntl) {
dd584fc0 9962 I915_WRITE_FW(CURCNTR(PIPE_A), 0);
dd584fc0 9963 I915_WRITE_FW(CURBASE(PIPE_A), base);
dd584fc0 9964 I915_WRITE_FW(CURSIZE, size);
b2d03b0d 9965 I915_WRITE_FW(CURPOS(PIPE_A), pos);
dd584fc0 9966 I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
75343a44 9967
e11ffddb
VS
9968 plane->cursor.base = base;
9969 plane->cursor.size = size;
9970 plane->cursor.cntl = cntl;
9971 } else {
9972 I915_WRITE_FW(CURPOS(PIPE_A), pos);
560b85bb 9973 }
e11ffddb 9974
75343a44 9975 POSTING_READ_FW(CURCNTR(PIPE_A));
b2d03b0d
VS
9976
9977 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
9978}
9979
9980static void i845_disable_cursor(struct intel_plane *plane,
9981 struct intel_crtc *crtc)
9982{
9983 i845_update_cursor(plane, NULL, NULL);
560b85bb
CW
9984}
9985
eade6c89
VS
9986static bool i845_cursor_get_hw_state(struct intel_plane *plane,
9987 enum pipe *pipe)
51f5a096
VS
9988{
9989 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
9990 enum intel_display_power_domain power_domain;
9991 bool ret;
9992
9993 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
9994 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9995 return false;
9996
9997 ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
9998
eade6c89
VS
9999 *pipe = PIPE_A;
10000
51f5a096
VS
10001 intel_display_power_put(dev_priv, power_domain);
10002
10003 return ret;
10004}
10005
ddd5713d
VS
10006static unsigned int
10007i9xx_cursor_max_stride(struct intel_plane *plane,
10008 u32 pixel_format, u64 modifier,
10009 unsigned int rotation)
10010{
10011 return plane->base.dev->mode_config.cursor_width * 4;
10012}
10013
292889e1
VS
10014static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
10015 const struct intel_plane_state *plane_state)
10016{
10017 struct drm_i915_private *dev_priv =
10018 to_i915(plane_state->base.plane->dev);
10019 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
c894d63c 10020 u32 cntl = 0;
292889e1 10021
e876b78c
VS
10022 if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
10023 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
10024
c894d63c
JRS
10025 if (INTEL_GEN(dev_priv) <= 10) {
10026 cntl |= MCURSOR_GAMMA_ENABLE;
292889e1 10027
c894d63c 10028 if (HAS_DDI(dev_priv))
b99b9ec1 10029 cntl |= MCURSOR_PIPE_CSC_ENABLE;
c894d63c 10030 }
292889e1 10031
32ea06b6
VS
10032 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
10033 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1
VS
10034
10035 switch (plane_state->base.crtc_w) {
10036 case 64:
b99b9ec1 10037 cntl |= MCURSOR_MODE_64_ARGB_AX;
292889e1
VS
10038 break;
10039 case 128:
b99b9ec1 10040 cntl |= MCURSOR_MODE_128_ARGB_AX;
292889e1
VS
10041 break;
10042 case 256:
b99b9ec1 10043 cntl |= MCURSOR_MODE_256_ARGB_AX;
292889e1
VS
10044 break;
10045 default:
10046 MISSING_CASE(plane_state->base.crtc_w);
10047 return 0;
10048 }
10049
c2c446ad 10050 if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
b99b9ec1 10051 cntl |= MCURSOR_ROTATE_180;
292889e1
VS
10052
10053 return cntl;
10054}
10055
659056f2 10056static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 10057{
024faac7
VS
10058 struct drm_i915_private *dev_priv =
10059 to_i915(plane_state->base.plane->dev);
659056f2
VS
10060 int width = plane_state->base.crtc_w;
10061 int height = plane_state->base.crtc_h;
4b0e333e 10062
3637ecf0 10063 if (!intel_cursor_size_ok(plane_state))
659056f2 10064 return false;
4398ad45 10065
024faac7
VS
10066 /* Cursor width is limited to a few power-of-two sizes */
10067 switch (width) {
659056f2
VS
10068 case 256:
10069 case 128:
659056f2
VS
10070 case 64:
10071 break;
10072 default:
10073 return false;
65a21cd6 10074 }
4b0e333e 10075
024faac7
VS
10076 /*
10077 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
10078 * height from 8 lines up to the cursor width, when the
10079 * cursor is not rotated. Everything else requires square
10080 * cursors.
10081 */
10082 if (HAS_CUR_FBC(dev_priv) &&
a82256bc 10083 plane_state->base.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
10084 if (height < 8 || height > width)
10085 return false;
10086 } else {
10087 if (height != width)
10088 return false;
10089 }
99d1f387 10090
659056f2 10091 return true;
65a21cd6
JB
10092}
10093
eb0f5044 10094static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
659056f2 10095 struct intel_plane_state *plane_state)
cda4b7d3 10096{
eb0f5044 10097 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
659056f2
VS
10098 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10099 const struct drm_framebuffer *fb = plane_state->base.fb;
659056f2 10100 enum pipe pipe = plane->pipe;
659056f2 10101 int ret;
cda4b7d3 10102
659056f2
VS
10103 ret = intel_check_cursor(crtc_state, plane_state);
10104 if (ret)
10105 return ret;
cda4b7d3 10106
659056f2 10107 /* if we want to turn off the cursor ignore width and height */
1e1bb871 10108 if (!fb)
659056f2 10109 return 0;
55a08b3f 10110
659056f2
VS
10111 /* Check for which cursor types we support */
10112 if (!i9xx_cursor_size_ok(plane_state)) {
10113 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
10114 plane_state->base.crtc_w,
10115 plane_state->base.crtc_h);
10116 return -EINVAL;
cda4b7d3 10117 }
cda4b7d3 10118
df79cf44
VS
10119 WARN_ON(plane_state->base.visible &&
10120 plane_state->color_plane[0].stride != fb->pitches[0]);
10121
1e1bb871
VS
10122 if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
10123 DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
10124 fb->pitches[0], plane_state->base.crtc_w);
10125 return -EINVAL;
659056f2 10126 }
dd584fc0 10127
659056f2
VS
10128 /*
10129 * There's something wrong with the cursor on CHV pipe C.
10130 * If it straddles the left edge of the screen then
10131 * moving it away from the edge or disabling it often
10132 * results in a pipe underrun, and often that can lead to
10133 * dead pipe (constant underrun reported, and it scans
10134 * out just a solid color). To recover from that, the
10135 * display power well must be turned off and on again.
10136 * Refuse the put the cursor into that compromised position.
10137 */
10138 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
10139 plane_state->base.visible && plane_state->base.crtc_x < 0) {
10140 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
10141 return -EINVAL;
10142 }
5efb3e28 10143
659056f2 10144 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 10145
659056f2 10146 return 0;
cda4b7d3
CW
10147}
10148
b2d03b0d
VS
10149static void i9xx_update_cursor(struct intel_plane *plane,
10150 const struct intel_crtc_state *crtc_state,
55a08b3f 10151 const struct intel_plane_state *plane_state)
dc41c154 10152{
cd5dcbf1
VS
10153 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10154 enum pipe pipe = plane->pipe;
024faac7 10155 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 10156 unsigned long irqflags;
dc41c154 10157
b2d03b0d 10158 if (plane_state && plane_state->base.visible) {
a0864d59 10159 cntl = plane_state->ctl;
dc41c154 10160
024faac7
VS
10161 if (plane_state->base.crtc_h != plane_state->base.crtc_w)
10162 fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
dc41c154 10163
b2d03b0d
VS
10164 base = intel_cursor_base(plane_state);
10165 pos = intel_cursor_position(plane_state);
10166 }
10167
10168 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10169
e11ffddb
VS
10170 /*
10171 * On some platforms writing CURCNTR first will also
10172 * cause CURPOS to be armed by the CURBASE write.
10173 * Without the CURCNTR write the CURPOS write would
8753d2bc
VS
10174 * arm itself. Thus we always start the full update
10175 * with a CURCNTR write.
10176 *
10177 * On other platforms CURPOS always requires the
10178 * CURBASE write to arm the update. Additonally
10179 * a write to any of the cursor register will cancel
10180 * an already armed cursor update. Thus leaving out
10181 * the CURBASE write after CURPOS could lead to a
10182 * cursor that doesn't appear to move, or even change
10183 * shape. Thus we always write CURBASE.
e11ffddb
VS
10184 *
10185 * CURCNTR and CUR_FBC_CTL are always
10186 * armed by the CURBASE write only.
10187 */
10188 if (plane->cursor.base != base ||
10189 plane->cursor.size != fbc_ctl ||
10190 plane->cursor.cntl != cntl) {
dd584fc0 10191 I915_WRITE_FW(CURCNTR(pipe), cntl);
e11ffddb
VS
10192 if (HAS_CUR_FBC(dev_priv))
10193 I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
b2d03b0d 10194 I915_WRITE_FW(CURPOS(pipe), pos);
75343a44
VS
10195 I915_WRITE_FW(CURBASE(pipe), base);
10196
e11ffddb
VS
10197 plane->cursor.base = base;
10198 plane->cursor.size = fbc_ctl;
10199 plane->cursor.cntl = cntl;
dc41c154 10200 } else {
e11ffddb 10201 I915_WRITE_FW(CURPOS(pipe), pos);
8753d2bc 10202 I915_WRITE_FW(CURBASE(pipe), base);
dc41c154
VS
10203 }
10204
dd584fc0 10205 POSTING_READ_FW(CURBASE(pipe));
99d1f387 10206
b2d03b0d 10207 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
10208}
10209
b2d03b0d
VS
10210static void i9xx_disable_cursor(struct intel_plane *plane,
10211 struct intel_crtc *crtc)
cda4b7d3 10212{
b2d03b0d 10213 i9xx_update_cursor(plane, NULL, NULL);
dc41c154
VS
10214}
10215
eade6c89
VS
10216static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
10217 enum pipe *pipe)
51f5a096
VS
10218{
10219 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10220 enum intel_display_power_domain power_domain;
51f5a096 10221 bool ret;
eade6c89 10222 u32 val;
51f5a096
VS
10223
10224 /*
10225 * Not 100% correct for planes that can move between pipes,
10226 * but that's only the case for gen2-3 which don't have any
10227 * display power wells.
10228 */
eade6c89 10229 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
51f5a096
VS
10230 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10231 return false;
10232
eade6c89
VS
10233 val = I915_READ(CURCNTR(plane->pipe));
10234
b99b9ec1 10235 ret = val & MCURSOR_MODE;
eade6c89
VS
10236
10237 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
10238 *pipe = plane->pipe;
10239 else
10240 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
10241 MCURSOR_PIPE_SELECT_SHIFT;
51f5a096
VS
10242
10243 intel_display_power_put(dev_priv, power_domain);
10244
10245 return ret;
10246}
dc41c154 10247
79e53945 10248/* VESA 640x480x72Hz mode to set on the pipe */
bacdcd55 10249static const struct drm_display_mode load_detect_mode = {
79e53945
JB
10250 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10251 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10252};
10253
a8bb6818 10254struct drm_framebuffer *
24dbf51a
CW
10255intel_framebuffer_create(struct drm_i915_gem_object *obj,
10256 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
10257{
10258 struct intel_framebuffer *intel_fb;
10259 int ret;
10260
10261 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10262 if (!intel_fb)
d2dff872 10263 return ERR_PTR(-ENOMEM);
d2dff872 10264
24dbf51a 10265 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
SV
10266 if (ret)
10267 goto err;
d2dff872
CW
10268
10269 return &intel_fb->base;
dcb1394e 10270
dd4916c5 10271err:
dd4916c5 10272 kfree(intel_fb);
dd4916c5 10273 return ERR_PTR(ret);
d2dff872
CW
10274}
10275
20bdc112
VS
10276static int intel_modeset_disable_planes(struct drm_atomic_state *state,
10277 struct drm_crtc *crtc)
d3a40d1b 10278{
20bdc112 10279 struct drm_plane *plane;
d3a40d1b 10280 struct drm_plane_state *plane_state;
20bdc112 10281 int ret, i;
d3a40d1b 10282
20bdc112 10283 ret = drm_atomic_add_affected_planes(state, crtc);
d3a40d1b
ACO
10284 if (ret)
10285 return ret;
20bdc112
VS
10286
10287 for_each_new_plane_in_state(state, plane, plane_state, i) {
10288 if (plane_state->crtc != crtc)
10289 continue;
10290
10291 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
10292 if (ret)
10293 return ret;
10294
10295 drm_atomic_set_fb_for_plane(plane_state, NULL);
10296 }
d3a40d1b
ACO
10297
10298 return 0;
10299}
10300
6c5ed5ae 10301int intel_get_load_detect_pipe(struct drm_connector *connector,
bacdcd55 10302 const struct drm_display_mode *mode,
6c5ed5ae
ML
10303 struct intel_load_detect_pipe *old,
10304 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10305{
10306 struct intel_crtc *intel_crtc;
d2434ab7
SV
10307 struct intel_encoder *intel_encoder =
10308 intel_attached_encoder(connector);
79e53945 10309 struct drm_crtc *possible_crtc;
4ef69c7a 10310 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10311 struct drm_crtc *crtc = NULL;
10312 struct drm_device *dev = encoder->dev;
0f0f74bc 10313 struct drm_i915_private *dev_priv = to_i915(dev);
51fd371b 10314 struct drm_mode_config *config = &dev->mode_config;
edde3617 10315 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10316 struct drm_connector_state *connector_state;
4be07317 10317 struct intel_crtc_state *crtc_state;
51fd371b 10318 int ret, i = -1;
79e53945 10319
d2dff872 10320 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10321 connector->base.id, connector->name,
8e329a03 10322 encoder->base.id, encoder->name);
d2dff872 10323
edde3617
ML
10324 old->restore_state = NULL;
10325
6c5ed5ae 10326 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 10327
79e53945
JB
10328 /*
10329 * Algorithm gets a little messy:
7a5e4805 10330 *
79e53945
JB
10331 * - if the connector already has an assigned crtc, use it (but make
10332 * sure it's on first)
7a5e4805 10333 *
79e53945
JB
10334 * - try to find the first unused crtc that can drive this connector,
10335 * and use that if we find one
79e53945
JB
10336 */
10337
10338 /* See if we already have a CRTC for this connector */
edde3617
ML
10339 if (connector->state->crtc) {
10340 crtc = connector->state->crtc;
8261b191 10341
51fd371b 10342 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10343 if (ret)
ad3c558f 10344 goto fail;
8261b191
CW
10345
10346 /* Make sure the crtc and connector are running */
edde3617 10347 goto found;
79e53945
JB
10348 }
10349
10350 /* Find an unused one (if possible) */
70e1e0ec 10351 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10352 i++;
10353 if (!(encoder->possible_crtcs & (1 << i)))
10354 continue;
edde3617
ML
10355
10356 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10357 if (ret)
10358 goto fail;
10359
10360 if (possible_crtc->state->enable) {
10361 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10362 continue;
edde3617 10363 }
a459249c
VS
10364
10365 crtc = possible_crtc;
10366 break;
79e53945
JB
10367 }
10368
10369 /*
10370 * If we didn't find an unused CRTC, don't use any.
10371 */
10372 if (!crtc) {
7173188d 10373 DRM_DEBUG_KMS("no pipe available for load-detect\n");
f4bf77b4 10374 ret = -ENODEV;
ad3c558f 10375 goto fail;
79e53945
JB
10376 }
10377
edde3617
ML
10378found:
10379 intel_crtc = to_intel_crtc(crtc);
10380
83a57153 10381 state = drm_atomic_state_alloc(dev);
edde3617
ML
10382 restore_state = drm_atomic_state_alloc(dev);
10383 if (!state || !restore_state) {
10384 ret = -ENOMEM;
10385 goto fail;
10386 }
83a57153
ACO
10387
10388 state->acquire_ctx = ctx;
edde3617 10389 restore_state->acquire_ctx = ctx;
83a57153 10390
944b0c76
ACO
10391 connector_state = drm_atomic_get_connector_state(state, connector);
10392 if (IS_ERR(connector_state)) {
10393 ret = PTR_ERR(connector_state);
10394 goto fail;
10395 }
10396
edde3617
ML
10397 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10398 if (ret)
10399 goto fail;
944b0c76 10400
4be07317
ACO
10401 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10402 if (IS_ERR(crtc_state)) {
10403 ret = PTR_ERR(crtc_state);
10404 goto fail;
10405 }
10406
49d6fa21 10407 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10408
6492711d
CW
10409 if (!mode)
10410 mode = &load_detect_mode;
79e53945 10411
20bdc112 10412 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
d3a40d1b
ACO
10413 if (ret)
10414 goto fail;
10415
20bdc112 10416 ret = intel_modeset_disable_planes(state, crtc);
edde3617
ML
10417 if (ret)
10418 goto fail;
10419
10420 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10421 if (!ret)
10422 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
be90cc31
VS
10423 if (!ret)
10424 ret = drm_atomic_add_affected_planes(restore_state, crtc);
edde3617
ML
10425 if (ret) {
10426 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10427 goto fail;
10428 }
8c7b5ccb 10429
3ba86073
ML
10430 ret = drm_atomic_commit(state);
10431 if (ret) {
6492711d 10432 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10433 goto fail;
79e53945 10434 }
edde3617
ML
10435
10436 old->restore_state = restore_state;
7abbd11f 10437 drm_atomic_state_put(state);
7173188d 10438
79e53945 10439 /* let the connector get through one full cycle before testing */
0f0f74bc 10440 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 10441 return true;
412b61d8 10442
ad3c558f 10443fail:
7fb71c8f
CW
10444 if (state) {
10445 drm_atomic_state_put(state);
10446 state = NULL;
10447 }
10448 if (restore_state) {
10449 drm_atomic_state_put(restore_state);
10450 restore_state = NULL;
10451 }
83a57153 10452
6c5ed5ae
ML
10453 if (ret == -EDEADLK)
10454 return ret;
51fd371b 10455
412b61d8 10456 return false;
79e53945
JB
10457}
10458
d2434ab7 10459void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10460 struct intel_load_detect_pipe *old,
10461 struct drm_modeset_acquire_ctx *ctx)
79e53945 10462{
d2434ab7
SV
10463 struct intel_encoder *intel_encoder =
10464 intel_attached_encoder(connector);
4ef69c7a 10465 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10466 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10467 int ret;
79e53945 10468
d2dff872 10469 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10470 connector->base.id, connector->name,
8e329a03 10471 encoder->base.id, encoder->name);
d2dff872 10472
edde3617 10473 if (!state)
0622a53c 10474 return;
79e53945 10475
581e49fe 10476 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 10477 if (ret)
edde3617 10478 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 10479 drm_atomic_state_put(state);
79e53945
JB
10480}
10481
da4a1efa 10482static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10483 const struct intel_crtc_state *pipe_config)
da4a1efa 10484{
fac5e23e 10485 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
10486 u32 dpll = pipe_config->dpll_hw_state.dpll;
10487
10488 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10489 return dev_priv->vbt.lvds_ssc_freq;
6e266956 10490 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 10491 return 120000;
5db94019 10492 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
10493 return 96000;
10494 else
10495 return 48000;
10496}
10497
79e53945 10498/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10499static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10500 struct intel_crtc_state *pipe_config)
79e53945 10501{
f1f644dc 10502 struct drm_device *dev = crtc->base.dev;
fac5e23e 10503 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 10504 int pipe = pipe_config->cpu_transcoder;
293623f7 10505 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 10506 u32 fp;
9e2c8475 10507 struct dpll clock;
dccbea3b 10508 int port_clock;
da4a1efa 10509 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10510
10511 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10512 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10513 else
293623f7 10514 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10515
10516 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 10517 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
10518 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10519 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10520 } else {
10521 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10522 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10523 }
10524
5db94019 10525 if (!IS_GEN2(dev_priv)) {
9b1e14f4 10526 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
10527 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10528 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10529 else
10530 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10531 DPLL_FPA01_P1_POST_DIV_SHIFT);
10532
10533 switch (dpll & DPLL_MODE_MASK) {
10534 case DPLLB_MODE_DAC_SERIAL:
10535 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10536 5 : 10;
10537 break;
10538 case DPLLB_MODE_LVDS:
10539 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10540 7 : 14;
10541 break;
10542 default:
28c97730 10543 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10544 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10545 return;
79e53945
JB
10546 }
10547
9b1e14f4 10548 if (IS_PINEVIEW(dev_priv))
dccbea3b 10549 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10550 else
dccbea3b 10551 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10552 } else {
50a0bc90 10553 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 10554 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10555
10556 if (is_lvds) {
10557 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10558 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10559
10560 if (lvds & LVDS_CLKB_POWER_UP)
10561 clock.p2 = 7;
10562 else
10563 clock.p2 = 14;
79e53945
JB
10564 } else {
10565 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10566 clock.p1 = 2;
10567 else {
10568 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10569 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10570 }
10571 if (dpll & PLL_P2_DIVIDE_BY_4)
10572 clock.p2 = 4;
10573 else
10574 clock.p2 = 2;
79e53945 10575 }
da4a1efa 10576
dccbea3b 10577 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10578 }
10579
18442d08
VS
10580 /*
10581 * This value includes pixel_multiplier. We will use
241bfc38 10582 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10583 * encoder's get_config() function.
10584 */
dccbea3b 10585 pipe_config->port_clock = port_clock;
f1f644dc
JB
10586}
10587
6878da05
VS
10588int intel_dotclock_calculate(int link_freq,
10589 const struct intel_link_m_n *m_n)
f1f644dc 10590{
f1f644dc
JB
10591 /*
10592 * The calculation for the data clock is:
1041a02f 10593 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10594 * But we want to avoid losing precison if possible, so:
1041a02f 10595 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10596 *
10597 * and the link clock is simpler:
1041a02f 10598 * link_clock = (m * link_clock) / n
f1f644dc
JB
10599 */
10600
6878da05
VS
10601 if (!m_n->link_n)
10602 return 0;
f1f644dc 10603
3123698f 10604 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6878da05 10605}
f1f644dc 10606
18442d08 10607static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10608 struct intel_crtc_state *pipe_config)
6878da05 10609{
e3b247da 10610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10611
18442d08
VS
10612 /* read out port_clock from the DPLL */
10613 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10614
f1f644dc 10615 /*
e3b247da
VS
10616 * In case there is an active pipe without active ports,
10617 * we may need some idea for the dotclock anyway.
10618 * Calculate one based on the FDI configuration.
79e53945 10619 */
2d112de7 10620 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10621 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10622 &pipe_config->fdi_m_n);
79e53945
JB
10623}
10624
de330815
VS
10625/* Returns the currently programmed mode of the given encoder. */
10626struct drm_display_mode *
10627intel_encoder_current_mode(struct intel_encoder *encoder)
79e53945 10628{
de330815
VS
10629 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
10630 struct intel_crtc_state *crtc_state;
79e53945 10631 struct drm_display_mode *mode;
de330815
VS
10632 struct intel_crtc *crtc;
10633 enum pipe pipe;
10634
10635 if (!encoder->get_hw_state(encoder, &pipe))
10636 return NULL;
10637
10638 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
79e53945
JB
10639
10640 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10641 if (!mode)
10642 return NULL;
10643
de330815
VS
10644 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
10645 if (!crtc_state) {
3f36b937
TU
10646 kfree(mode);
10647 return NULL;
10648 }
10649
de330815 10650 crtc_state->base.crtc = &crtc->base;
3f36b937 10651
de330815
VS
10652 if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
10653 kfree(crtc_state);
10654 kfree(mode);
10655 return NULL;
10656 }
e30a154b 10657
de330815 10658 encoder->get_config(encoder, crtc_state);
79e53945 10659
de330815 10660 intel_mode_from_pipe_config(mode, crtc_state);
79e53945 10661
de330815 10662 kfree(crtc_state);
3f36b937 10663
79e53945
JB
10664 return mode;
10665}
10666
10667static void intel_crtc_destroy(struct drm_crtc *crtc)
10668{
10669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10670
10671 drm_crtc_cleanup(crtc);
10672 kfree(intel_crtc);
10673}
10674
5a21b665
SV
10675/**
10676 * intel_wm_need_update - Check whether watermarks need updating
10677 * @plane: drm plane
10678 * @state: new plane state
10679 *
10680 * Check current plane state versus the new one to determine whether
10681 * watermarks need to be recalculated.
10682 *
10683 * Returns true or false.
10684 */
10685static bool intel_wm_need_update(struct drm_plane *plane,
10686 struct drm_plane_state *state)
10687{
10688 struct intel_plane_state *new = to_intel_plane_state(state);
10689 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
10690
10691 /* Update watermarks on tiling or size changes. */
936e71e3 10692 if (new->base.visible != cur->base.visible)
5a21b665
SV
10693 return true;
10694
10695 if (!cur->base.fb || !new->base.fb)
10696 return false;
10697
bae781b2 10698 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 10699 cur->base.rotation != new->base.rotation ||
936e71e3
VS
10700 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
10701 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
10702 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
10703 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
SV
10704 return true;
10705
10706 return false;
10707}
10708
b2b55502 10709static bool needs_scaling(const struct intel_plane_state *state)
5a21b665 10710{
936e71e3
VS
10711 int src_w = drm_rect_width(&state->base.src) >> 16;
10712 int src_h = drm_rect_height(&state->base.src) >> 16;
10713 int dst_w = drm_rect_width(&state->base.dst);
10714 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
SV
10715
10716 return (src_w != dst_w || src_h != dst_h);
10717}
d21fbe87 10718
b2b55502
VS
10719int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
10720 struct drm_crtc_state *crtc_state,
10721 const struct intel_plane_state *old_plane_state,
da20eabd
ML
10722 struct drm_plane_state *plane_state)
10723{
ab1d3a0e 10724 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
10725 struct drm_crtc *crtc = crtc_state->crtc;
10726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e9728bd8 10727 struct intel_plane *plane = to_intel_plane(plane_state->plane);
da20eabd 10728 struct drm_device *dev = crtc->dev;
ed4a6a7c 10729 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd 10730 bool mode_changed = needs_modeset(crtc_state);
b2b55502 10731 bool was_crtc_enabled = old_crtc_state->base.active;
da20eabd 10732 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
10733 bool turn_off, turn_on, visible, was_visible;
10734 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 10735 int ret;
da20eabd 10736
e9728bd8 10737 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
da20eabd
ML
10738 ret = skl_update_scaler_plane(
10739 to_intel_crtc_state(crtc_state),
10740 to_intel_plane_state(plane_state));
10741 if (ret)
10742 return ret;
10743 }
10744
936e71e3 10745 was_visible = old_plane_state->base.visible;
1d4258db 10746 visible = plane_state->visible;
da20eabd
ML
10747
10748 if (!was_crtc_enabled && WARN_ON(was_visible))
10749 was_visible = false;
10750
35c08f43
ML
10751 /*
10752 * Visibility is calculated as if the crtc was on, but
10753 * after scaler setup everything depends on it being off
10754 * when the crtc isn't active.
f818ffea
VS
10755 *
10756 * FIXME this is wrong for watermarks. Watermarks should also
10757 * be computed as if the pipe would be active. Perhaps move
10758 * per-plane wm computation to the .check_plane() hook, and
10759 * only combine the results from all planes in the current place?
35c08f43 10760 */
e9728bd8 10761 if (!is_crtc_enabled) {
1d4258db 10762 plane_state->visible = visible = false;
e9728bd8
VS
10763 to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
10764 }
da20eabd
ML
10765
10766 if (!was_visible && !visible)
10767 return 0;
10768
e8861675
ML
10769 if (fb != old_plane_state->base.fb)
10770 pipe_config->fb_changed = true;
10771
da20eabd
ML
10772 turn_off = was_visible && (!visible || mode_changed);
10773 turn_on = visible && (!was_visible || mode_changed);
10774
72660ce0 10775 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
e9728bd8
VS
10776 intel_crtc->base.base.id, intel_crtc->base.name,
10777 plane->base.base.id, plane->base.name,
72660ce0 10778 fb ? fb->base.id : -1);
da20eabd 10779
72660ce0 10780 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
e9728bd8 10781 plane->base.base.id, plane->base.name,
72660ce0 10782 was_visible, visible,
da20eabd
ML
10783 turn_off, turn_on, mode_changed);
10784
caed361d 10785 if (turn_on) {
04548cba 10786 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10787 pipe_config->update_wm_pre = true;
caed361d
VS
10788
10789 /* must disable cxsr around plane enable/disable */
e9728bd8 10790 if (plane->id != PLANE_CURSOR)
caed361d
VS
10791 pipe_config->disable_cxsr = true;
10792 } else if (turn_off) {
04548cba 10793 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
b4ede6df 10794 pipe_config->update_wm_post = true;
92826fcd 10795
852eb00d 10796 /* must disable cxsr around plane enable/disable */
e9728bd8 10797 if (plane->id != PLANE_CURSOR)
ab1d3a0e 10798 pipe_config->disable_cxsr = true;
e9728bd8 10799 } else if (intel_wm_need_update(&plane->base, plane_state)) {
04548cba 10800 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df
VS
10801 /* FIXME bollocks */
10802 pipe_config->update_wm_pre = true;
10803 pipe_config->update_wm_post = true;
10804 }
852eb00d 10805 }
da20eabd 10806
8be6ca85 10807 if (visible || was_visible)
e9728bd8 10808 pipe_config->fb_bits |= plane->frontbuffer_bit;
a9ff8714 10809
31ae71fc
ML
10810 /*
10811 * WaCxSRDisabledForSpriteScaling:ivb
10812 *
10813 * cstate->update_wm was already set above, so this flag will
10814 * take effect when we commit and program watermarks.
10815 */
e9728bd8 10816 if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
10817 needs_scaling(to_intel_plane_state(plane_state)) &&
10818 !needs_scaling(old_plane_state))
10819 pipe_config->disable_lp_wm = true;
d21fbe87 10820
da20eabd
ML
10821 return 0;
10822}
10823
6d3a1ce7
ML
10824static bool encoders_cloneable(const struct intel_encoder *a,
10825 const struct intel_encoder *b)
10826{
10827 /* masks could be asymmetric, so check both ways */
10828 return a == b || (a->cloneable & (1 << b->type) &&
10829 b->cloneable & (1 << a->type));
10830}
10831
10832static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10833 struct intel_crtc *crtc,
10834 struct intel_encoder *encoder)
10835{
10836 struct intel_encoder *source_encoder;
10837 struct drm_connector *connector;
10838 struct drm_connector_state *connector_state;
10839 int i;
10840
aa5e9b47 10841 for_each_new_connector_in_state(state, connector, connector_state, i) {
6d3a1ce7
ML
10842 if (connector_state->crtc != &crtc->base)
10843 continue;
10844
10845 source_encoder =
10846 to_intel_encoder(connector_state->best_encoder);
10847 if (!encoders_cloneable(encoder, source_encoder))
10848 return false;
10849 }
10850
10851 return true;
10852}
10853
6d3a1ce7
ML
10854static int intel_crtc_atomic_check(struct drm_crtc *crtc,
10855 struct drm_crtc_state *crtc_state)
10856{
cf5a15be 10857 struct drm_device *dev = crtc->dev;
fac5e23e 10858 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 10859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
10860 struct intel_crtc_state *pipe_config =
10861 to_intel_crtc_state(crtc_state);
6d3a1ce7 10862 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 10863 int ret;
6d3a1ce7
ML
10864 bool mode_changed = needs_modeset(crtc_state);
10865
852eb00d 10866 if (mode_changed && !crtc_state->active)
caed361d 10867 pipe_config->update_wm_post = true;
eddfcbcd 10868
ad421372
ML
10869 if (mode_changed && crtc_state->enable &&
10870 dev_priv->display.crtc_compute_clock &&
8106ddbd 10871 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
10872 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
10873 pipe_config);
10874 if (ret)
10875 return ret;
10876 }
10877
82cf435b
LL
10878 if (crtc_state->color_mgmt_changed) {
10879 ret = intel_color_check(crtc, crtc_state);
10880 if (ret)
10881 return ret;
e7852a4b
LL
10882
10883 /*
10884 * Changing color management on Intel hardware is
10885 * handled as part of planes update.
10886 */
10887 crtc_state->planes_changed = true;
82cf435b
LL
10888 }
10889
e435d6e5 10890 ret = 0;
86c8bbbe 10891 if (dev_priv->display.compute_pipe_wm) {
e3bddded 10892 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
10893 if (ret) {
10894 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
10895 return ret;
10896 }
10897 }
10898
10899 if (dev_priv->display.compute_intermediate_wm &&
10900 !to_intel_atomic_state(state)->skip_intermediate_wm) {
10901 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
10902 return 0;
10903
10904 /*
10905 * Calculate 'intermediate' watermarks that satisfy both the
10906 * old state and the new state. We can program these
10907 * immediately.
10908 */
6315b5d3 10909 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
10910 intel_crtc,
10911 pipe_config);
10912 if (ret) {
10913 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 10914 return ret;
ed4a6a7c 10915 }
e3d5457c
VS
10916 } else if (dev_priv->display.compute_intermediate_wm) {
10917 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
10918 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
10919 }
10920
6315b5d3 10921 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
10922 if (mode_changed)
10923 ret = skl_update_scaler_crtc(pipe_config);
10924
73b0ca8e
MK
10925 if (!ret)
10926 ret = skl_check_pipe_max_pixel_rate(intel_crtc,
10927 pipe_config);
e435d6e5 10928 if (!ret)
6ebc6923 10929 ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
e435d6e5
ML
10930 pipe_config);
10931 }
10932
24f28450
ML
10933 if (HAS_IPS(dev_priv))
10934 pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
10935
e435d6e5 10936 return ret;
6d3a1ce7
ML
10937}
10938
65b38e0d 10939static const struct drm_crtc_helper_funcs intel_helper_funcs = {
5a21b665
SV
10940 .atomic_begin = intel_begin_crtc_commit,
10941 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 10942 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
10943};
10944
d29b2f9d
ACO
10945static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10946{
10947 struct intel_connector *connector;
f9e905ca 10948 struct drm_connector_list_iter conn_iter;
d29b2f9d 10949
f9e905ca
SV
10950 drm_connector_list_iter_begin(dev, &conn_iter);
10951 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f 10952 if (connector->base.state->crtc)
ef196b5c 10953 drm_connector_put(&connector->base);
8863dc7f 10954
d29b2f9d
ACO
10955 if (connector->base.encoder) {
10956 connector->base.state->best_encoder =
10957 connector->base.encoder;
10958 connector->base.state->crtc =
10959 connector->base.encoder->crtc;
8863dc7f 10960
ef196b5c 10961 drm_connector_get(&connector->base);
d29b2f9d
ACO
10962 } else {
10963 connector->base.state->best_encoder = NULL;
10964 connector->base.state->crtc = NULL;
10965 }
10966 }
f9e905ca 10967 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
10968}
10969
050f7aeb 10970static void
eba905b2 10971connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 10972 struct intel_crtc_state *pipe_config)
050f7aeb 10973{
6a2a5c5d 10974 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
SV
10975 int bpp = pipe_config->pipe_bpp;
10976
10977 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
10978 connector->base.base.id,
10979 connector->base.name);
050f7aeb
SV
10980
10981 /* Don't use an invalid EDID bpc value */
6a2a5c5d 10982 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 10983 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
10984 bpp, info->bpc * 3);
10985 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
SV
10986 }
10987
196f954e 10988 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 10989 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
10990 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10991 bpp);
10992 pipe_config->pipe_bpp = 24;
050f7aeb
SV
10993 }
10994}
10995
4e53c2e0 10996static int
050f7aeb 10997compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 10998 struct intel_crtc_state *pipe_config)
4e53c2e0 10999{
9beb5fea 11000 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 11001 struct drm_atomic_state *state;
da3ced29
ACO
11002 struct drm_connector *connector;
11003 struct drm_connector_state *connector_state;
1486017f 11004 int bpp, i;
4e53c2e0 11005
9beb5fea
TU
11006 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11007 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 11008 bpp = 10*3;
9beb5fea 11009 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
SV
11010 bpp = 12*3;
11011 else
11012 bpp = 8*3;
11013
4e53c2e0 11014
4e53c2e0
SV
11015 pipe_config->pipe_bpp = bpp;
11016
1486017f
ACO
11017 state = pipe_config->base.state;
11018
4e53c2e0 11019 /* Clamp display bpp to EDID value */
aa5e9b47 11020 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 11021 if (connector_state->crtc != &crtc->base)
4e53c2e0
SV
11022 continue;
11023
da3ced29
ACO
11024 connected_sink_compute_bpp(to_intel_connector(connector),
11025 pipe_config);
4e53c2e0
SV
11026 }
11027
11028 return bpp;
11029}
11030
644db711
SV
11031static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11032{
11033 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11034 "type: 0x%x flags: 0x%x\n",
1342830c 11035 mode->crtc_clock,
644db711
SV
11036 mode->crtc_hdisplay, mode->crtc_hsync_start,
11037 mode->crtc_hsync_end, mode->crtc_htotal,
11038 mode->crtc_vdisplay, mode->crtc_vsync_start,
11039 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11040}
11041
f6982332
TU
11042static inline void
11043intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 11044 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 11045{
a4309657
TU
11046 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11047 id, lane_count,
f6982332
TU
11048 m_n->gmch_m, m_n->gmch_n,
11049 m_n->link_m, m_n->link_n, m_n->tu);
11050}
11051
40b2be41
VS
11052#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
11053
11054static const char * const output_type_str[] = {
11055 OUTPUT_TYPE(UNUSED),
11056 OUTPUT_TYPE(ANALOG),
11057 OUTPUT_TYPE(DVO),
11058 OUTPUT_TYPE(SDVO),
11059 OUTPUT_TYPE(LVDS),
11060 OUTPUT_TYPE(TVOUT),
11061 OUTPUT_TYPE(HDMI),
11062 OUTPUT_TYPE(DP),
11063 OUTPUT_TYPE(EDP),
11064 OUTPUT_TYPE(DSI),
7e732cac 11065 OUTPUT_TYPE(DDI),
40b2be41
VS
11066 OUTPUT_TYPE(DP_MST),
11067};
11068
11069#undef OUTPUT_TYPE
11070
11071static void snprintf_output_types(char *buf, size_t len,
11072 unsigned int output_types)
11073{
11074 char *str = buf;
11075 int i;
11076
11077 str[0] = '\0';
11078
11079 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
11080 int r;
11081
11082 if ((output_types & BIT(i)) == 0)
11083 continue;
11084
11085 r = snprintf(str, len, "%s%s",
11086 str != buf ? "," : "", output_type_str[i]);
11087 if (r >= len)
11088 break;
11089 str += r;
11090 len -= r;
11091
11092 output_types &= ~BIT(i);
11093 }
11094
11095 WARN_ON_ONCE(output_types != 0);
11096}
11097
c0b03411 11098static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11099 struct intel_crtc_state *pipe_config,
c0b03411
SV
11100 const char *context)
11101{
6a60cd87 11102 struct drm_device *dev = crtc->base.dev;
4f8036a2 11103 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
11104 struct drm_plane *plane;
11105 struct intel_plane *intel_plane;
11106 struct intel_plane_state *state;
11107 struct drm_framebuffer *fb;
40b2be41 11108 char buf[64];
6a60cd87 11109
66766e4f
TU
11110 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
11111 crtc->base.base.id, crtc->base.name, context);
c0b03411 11112
40b2be41
VS
11113 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
11114 DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
11115 buf, pipe_config->output_types);
11116
2c89429e
TU
11117 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
11118 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 11119 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
11120
11121 if (pipe_config->has_pch_encoder)
11122 intel_dump_m_n_config(pipe_config, "fdi",
11123 pipe_config->fdi_lanes,
11124 &pipe_config->fdi_m_n);
f6982332 11125
b22ca995
SS
11126 if (pipe_config->ycbcr420)
11127 DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
11128
f6982332 11129 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
11130 intel_dump_m_n_config(pipe_config, "dp m_n",
11131 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
11132 if (pipe_config->has_drrs)
11133 intel_dump_m_n_config(pipe_config, "dp m2_n2",
11134 pipe_config->lane_count,
11135 &pipe_config->dp_m2_n2);
f6982332 11136 }
b95af8be 11137
55072d19 11138 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 11139 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 11140
c0b03411 11141 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11142 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11143 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11144 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11145 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
a7d1b3f4 11146 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
2c89429e 11147 pipe_config->port_clock,
a7d1b3f4
VS
11148 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
11149 pipe_config->pixel_rate);
dd2f616d
TU
11150
11151 if (INTEL_GEN(dev_priv) >= 9)
11152 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11153 crtc->num_scalers,
11154 pipe_config->scaler_state.scaler_users,
11155 pipe_config->scaler_state.scaler_id);
a74f8375
TU
11156
11157 if (HAS_GMCH_DISPLAY(dev_priv))
11158 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11159 pipe_config->gmch_pfit.control,
11160 pipe_config->gmch_pfit.pgm_ratios,
11161 pipe_config->gmch_pfit.lvds_border_bits);
11162 else
11163 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11164 pipe_config->pch_pfit.pos,
11165 pipe_config->pch_pfit.size,
08c4d7fc 11166 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 11167
2c89429e
TU
11168 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
11169 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 11170
f50b79f0 11171 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 11172
6a60cd87
CK
11173 DRM_DEBUG_KMS("planes on this crtc\n");
11174 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 11175 struct drm_format_name_buf format_name;
6a60cd87
CK
11176 intel_plane = to_intel_plane(plane);
11177 if (intel_plane->pipe != crtc->pipe)
11178 continue;
11179
11180 state = to_intel_plane_state(plane->state);
11181 fb = state->base.fb;
11182 if (!fb) {
1d577e02
VS
11183 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
11184 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
11185 continue;
11186 }
11187
dd2f616d
TU
11188 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
11189 plane->base.id, plane->name,
b3c11ac2 11190 fb->base.id, fb->width, fb->height,
438b74a5 11191 drm_get_format_name(fb->format->format, &format_name));
dd2f616d
TU
11192 if (INTEL_GEN(dev_priv) >= 9)
11193 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
11194 state->scaler_id,
11195 state->base.src.x1 >> 16,
11196 state->base.src.y1 >> 16,
11197 drm_rect_width(&state->base.src) >> 16,
11198 drm_rect_height(&state->base.src) >> 16,
11199 state->base.dst.x1, state->base.dst.y1,
11200 drm_rect_width(&state->base.dst),
11201 drm_rect_height(&state->base.dst));
6a60cd87 11202 }
c0b03411
SV
11203}
11204
5448a00d 11205static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11206{
5448a00d 11207 struct drm_device *dev = state->dev;
da3ced29 11208 struct drm_connector *connector;
2fd96b41 11209 struct drm_connector_list_iter conn_iter;
00f0b378 11210 unsigned int used_ports = 0;
477321e0 11211 unsigned int used_mst_ports = 0;
bd67a8c1 11212 bool ret = true;
00f0b378
VS
11213
11214 /*
11215 * Walk the connector list instead of the encoder
11216 * list to detect the problem on ddi platforms
11217 * where there's just one encoder per digital port.
11218 */
2fd96b41
GP
11219 drm_connector_list_iter_begin(dev, &conn_iter);
11220 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
11221 struct drm_connector_state *connector_state;
11222 struct intel_encoder *encoder;
11223
8b69449d 11224 connector_state = drm_atomic_get_new_connector_state(state, connector);
0bff4858
VS
11225 if (!connector_state)
11226 connector_state = connector->state;
11227
5448a00d 11228 if (!connector_state->best_encoder)
00f0b378
VS
11229 continue;
11230
5448a00d
ACO
11231 encoder = to_intel_encoder(connector_state->best_encoder);
11232
11233 WARN_ON(!connector_state->crtc);
00f0b378
VS
11234
11235 switch (encoder->type) {
11236 unsigned int port_mask;
7e732cac 11237 case INTEL_OUTPUT_DDI:
4f8036a2 11238 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 11239 break;
f0d759f0 11240 /* else: fall through */
cca0502b 11241 case INTEL_OUTPUT_DP:
00f0b378
VS
11242 case INTEL_OUTPUT_HDMI:
11243 case INTEL_OUTPUT_EDP:
8f4f2797 11244 port_mask = 1 << encoder->port;
00f0b378
VS
11245
11246 /* the same port mustn't appear more than once */
11247 if (used_ports & port_mask)
bd67a8c1 11248 ret = false;
00f0b378
VS
11249
11250 used_ports |= port_mask;
477321e0
VS
11251 break;
11252 case INTEL_OUTPUT_DP_MST:
11253 used_mst_ports |=
8f4f2797 11254 1 << encoder->port;
477321e0 11255 break;
00f0b378
VS
11256 default:
11257 break;
11258 }
11259 }
2fd96b41 11260 drm_connector_list_iter_end(&conn_iter);
00f0b378 11261
477321e0
VS
11262 /* can't mix MST and SST/HDMI on the same port */
11263 if (used_ports & used_mst_ports)
11264 return false;
11265
bd67a8c1 11266 return ret;
00f0b378
VS
11267}
11268
83a57153
ACO
11269static void
11270clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11271{
ff32c54e
VS
11272 struct drm_i915_private *dev_priv =
11273 to_i915(crtc_state->base.crtc->dev);
663a3640 11274 struct intel_crtc_scaler_state scaler_state;
4978cc93 11275 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 11276 struct intel_shared_dpll *shared_dpll;
ff32c54e 11277 struct intel_crtc_wm_state wm_state;
6e644626 11278 bool force_thru, ips_force_disable;
83a57153 11279
7546a384
ACO
11280 /* FIXME: before the switch to atomic started, a new pipe_config was
11281 * kzalloc'd. Code that depends on any field being zero should be
11282 * fixed, so that the crtc_state can be safely duplicated. For now,
11283 * only fields that are know to not cause problems are preserved. */
11284
663a3640 11285 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11286 shared_dpll = crtc_state->shared_dpll;
11287 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 11288 force_thru = crtc_state->pch_pfit.force_thru;
6e644626 11289 ips_force_disable = crtc_state->ips_force_disable;
04548cba
VS
11290 if (IS_G4X(dev_priv) ||
11291 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11292 wm_state = crtc_state->wm;
4978cc93 11293
d2fa80a5
CW
11294 /* Keep base drm_crtc_state intact, only clear our extended struct */
11295 BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
11296 memset(&crtc_state->base + 1, 0,
11297 sizeof(*crtc_state) - sizeof(crtc_state->base));
4978cc93 11298
663a3640 11299 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11300 crtc_state->shared_dpll = shared_dpll;
11301 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 11302 crtc_state->pch_pfit.force_thru = force_thru;
6e644626 11303 crtc_state->ips_force_disable = ips_force_disable;
04548cba
VS
11304 if (IS_G4X(dev_priv) ||
11305 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
ff32c54e 11306 crtc_state->wm = wm_state;
83a57153
ACO
11307}
11308
548ee15b 11309static int
b8cecdf5 11310intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 11311 struct intel_crtc_state *pipe_config)
ee7b9f93 11312{
b359283a 11313 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 11314 struct intel_encoder *encoder;
da3ced29 11315 struct drm_connector *connector;
0b901879 11316 struct drm_connector_state *connector_state;
d328c9d7 11317 int base_bpp, ret = -EINVAL;
0b901879 11318 int i;
e29c22c0 11319 bool retry = true;
ee7b9f93 11320
83a57153 11321 clear_intel_crtc_state(pipe_config);
7758a113 11322
e143a21c
SV
11323 pipe_config->cpu_transcoder =
11324 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11325
2960bc9c
ID
11326 /*
11327 * Sanitize sync polarity flags based on requested ones. If neither
11328 * positive or negative polarity is requested, treat this as meaning
11329 * negative polarity.
11330 */
2d112de7 11331 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11332 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11333 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11334
2d112de7 11335 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11336 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11337 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11338
d328c9d7
SV
11339 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11340 pipe_config);
11341 if (base_bpp < 0)
4e53c2e0
SV
11342 goto fail;
11343
e41a56be
VS
11344 /*
11345 * Determine the real pipe dimensions. Note that stereo modes can
11346 * increase the actual pipe size due to the frame doubling and
11347 * insertion of additional space for blanks between the frame. This
11348 * is stored in the crtc timings. We use the requested mode to do this
11349 * computation to clearly distinguish it from the adjusted mode, which
11350 * can be changed by the connectors in the below retry loop.
11351 */
196cd5d3 11352 drm_mode_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11353 &pipe_config->pipe_src_w,
11354 &pipe_config->pipe_src_h);
e41a56be 11355
aa5e9b47 11356 for_each_new_connector_in_state(state, connector, connector_state, i) {
253c84c8
VS
11357 if (connector_state->crtc != crtc)
11358 continue;
11359
11360 encoder = to_intel_encoder(connector_state->best_encoder);
11361
e25148d0
VS
11362 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
11363 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11364 goto fail;
11365 }
11366
253c84c8
VS
11367 /*
11368 * Determine output_types before calling the .compute_config()
11369 * hooks so that the hooks can use this information safely.
11370 */
7e732cac
VS
11371 if (encoder->compute_output_type)
11372 pipe_config->output_types |=
11373 BIT(encoder->compute_output_type(encoder, pipe_config,
11374 connector_state));
11375 else
11376 pipe_config->output_types |= BIT(encoder->type);
253c84c8
VS
11377 }
11378
e29c22c0 11379encoder_retry:
ef1b460d 11380 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11381 pipe_config->port_clock = 0;
ef1b460d 11382 pipe_config->pixel_multiplier = 1;
ff9a6750 11383
135c81b8 11384 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11385 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11386 CRTC_STEREO_DOUBLE);
135c81b8 11387
7758a113
SV
11388 /* Pass our mode to the connectors and the CRTC to give them a chance to
11389 * adjust it according to limitations or connector properties, and also
11390 * a chance to reject the mode entirely.
47f1c6c9 11391 */
aa5e9b47 11392 for_each_new_connector_in_state(state, connector, connector_state, i) {
0b901879 11393 if (connector_state->crtc != crtc)
7758a113 11394 continue;
7ae89233 11395
0b901879
ACO
11396 encoder = to_intel_encoder(connector_state->best_encoder);
11397
0a478c27 11398 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 11399 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
SV
11400 goto fail;
11401 }
ee7b9f93 11402 }
47f1c6c9 11403
ff9a6750
SV
11404 /* Set default port clock if not overwritten by the encoder. Needs to be
11405 * done afterwards in case the encoder adjusts the mode. */
11406 if (!pipe_config->port_clock)
2d112de7 11407 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11408 * pipe_config->pixel_multiplier;
ff9a6750 11409
a43f6e0f 11410 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11411 if (ret < 0) {
7758a113
SV
11412 DRM_DEBUG_KMS("CRTC fixup failed\n");
11413 goto fail;
ee7b9f93 11414 }
e29c22c0
SV
11415
11416 if (ret == RETRY) {
11417 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11418 ret = -EINVAL;
11419 goto fail;
11420 }
11421
11422 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11423 retry = false;
11424 goto encoder_retry;
11425 }
11426
e8fa4270 11427 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
11428 * only enable it on 6bpc panels and when its not a compliance
11429 * test requesting 6bpc video pattern.
11430 */
11431 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
11432 !pipe_config->dither_force_disable;
62f0ace5 11433 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11434 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11435
7758a113 11436fail:
548ee15b 11437 return ret;
ee7b9f93 11438}
47f1c6c9 11439
3bd26263 11440static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11441{
3bd26263 11442 int diff;
f1f644dc
JB
11443
11444 if (clock1 == clock2)
11445 return true;
11446
11447 if (!clock1 || !clock2)
11448 return false;
11449
11450 diff = abs(clock1 - clock2);
11451
11452 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11453 return true;
11454
11455 return false;
11456}
11457
cfb23ed6
ML
11458static bool
11459intel_compare_m_n(unsigned int m, unsigned int n,
11460 unsigned int m2, unsigned int n2,
11461 bool exact)
11462{
11463 if (m == m2 && n == n2)
11464 return true;
11465
11466 if (exact || !m || !n || !m2 || !n2)
11467 return false;
11468
11469 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
11470
31d10b57
ML
11471 if (n > n2) {
11472 while (n > n2) {
cfb23ed6
ML
11473 m2 <<= 1;
11474 n2 <<= 1;
11475 }
31d10b57
ML
11476 } else if (n < n2) {
11477 while (n < n2) {
cfb23ed6
ML
11478 m <<= 1;
11479 n <<= 1;
11480 }
11481 }
11482
31d10b57
ML
11483 if (n != n2)
11484 return false;
11485
11486 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
11487}
11488
11489static bool
11490intel_compare_link_m_n(const struct intel_link_m_n *m_n,
11491 struct intel_link_m_n *m2_n2,
11492 bool adjust)
11493{
11494 if (m_n->tu == m2_n2->tu &&
11495 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
11496 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
11497 intel_compare_m_n(m_n->link_m, m_n->link_n,
11498 m2_n2->link_m, m2_n2->link_n, !adjust)) {
11499 if (adjust)
11500 *m2_n2 = *m_n;
11501
11502 return true;
11503 }
11504
11505 return false;
11506}
11507
4e8048f8
TU
11508static void __printf(3, 4)
11509pipe_config_err(bool adjust, const char *name, const char *format, ...)
11510{
4e8048f8
TU
11511 struct va_format vaf;
11512 va_list args;
11513
4e8048f8
TU
11514 va_start(args, format);
11515 vaf.fmt = format;
11516 vaf.va = &args;
11517
99a95487
JP
11518 if (adjust)
11519 drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
11520 else
11521 drm_err("mismatch in %s %pV", name, &vaf);
4e8048f8
TU
11522
11523 va_end(args);
11524}
11525
0e8ffe1b 11526static bool
6315b5d3 11527intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 11528 struct intel_crtc_state *current_config,
cfb23ed6
ML
11529 struct intel_crtc_state *pipe_config,
11530 bool adjust)
0e8ffe1b 11531{
cfb23ed6 11532 bool ret = true;
4493e098
ML
11533 bool fixup_inherited = adjust &&
11534 (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
11535 !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
cfb23ed6 11536
eadd2721 11537#define PIPE_CONF_CHECK_X(name) do { \
66e985c0 11538 if (current_config->name != pipe_config->name) { \
4e8048f8 11539 pipe_config_err(adjust, __stringify(name), \
66e985c0
SV
11540 "(expected 0x%08x, found 0x%08x)\n", \
11541 current_config->name, \
11542 pipe_config->name); \
cfb23ed6 11543 ret = false; \
eadd2721
VS
11544 } \
11545} while (0)
66e985c0 11546
eadd2721 11547#define PIPE_CONF_CHECK_I(name) do { \
08a24034 11548 if (current_config->name != pipe_config->name) { \
4e8048f8 11549 pipe_config_err(adjust, __stringify(name), \
08a24034
SV
11550 "(expected %i, found %i)\n", \
11551 current_config->name, \
11552 pipe_config->name); \
cfb23ed6 11553 ret = false; \
eadd2721
VS
11554 } \
11555} while (0)
cfb23ed6 11556
eadd2721 11557#define PIPE_CONF_CHECK_BOOL(name) do { \
d640bf79
ML
11558 if (current_config->name != pipe_config->name) { \
11559 pipe_config_err(adjust, __stringify(name), \
11560 "(expected %s, found %s)\n", \
11561 yesno(current_config->name), \
11562 yesno(pipe_config->name)); \
11563 ret = false; \
eadd2721
VS
11564 } \
11565} while (0)
d640bf79 11566
4493e098
ML
11567/*
11568 * Checks state where we only read out the enabling, but not the entire
11569 * state itself (like full infoframes or ELD for audio). These states
11570 * require a full modeset on bootup to fix up.
11571 */
eadd2721 11572#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
4493e098
ML
11573 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
11574 PIPE_CONF_CHECK_BOOL(name); \
11575 } else { \
11576 pipe_config_err(adjust, __stringify(name), \
11577 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
11578 yesno(current_config->name), \
11579 yesno(pipe_config->name)); \
11580 ret = false; \
eadd2721
VS
11581 } \
11582} while (0)
4493e098 11583
eadd2721 11584#define PIPE_CONF_CHECK_P(name) do { \
8106ddbd 11585 if (current_config->name != pipe_config->name) { \
4e8048f8 11586 pipe_config_err(adjust, __stringify(name), \
8106ddbd
ACO
11587 "(expected %p, found %p)\n", \
11588 current_config->name, \
11589 pipe_config->name); \
11590 ret = false; \
eadd2721
VS
11591 } \
11592} while (0)
8106ddbd 11593
eadd2721 11594#define PIPE_CONF_CHECK_M_N(name) do { \
cfb23ed6
ML
11595 if (!intel_compare_link_m_n(&current_config->name, \
11596 &pipe_config->name,\
11597 adjust)) { \
4e8048f8 11598 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11599 "(expected tu %i gmch %i/%i link %i/%i, " \
11600 "found tu %i, gmch %i/%i link %i/%i)\n", \
11601 current_config->name.tu, \
11602 current_config->name.gmch_m, \
11603 current_config->name.gmch_n, \
11604 current_config->name.link_m, \
11605 current_config->name.link_n, \
11606 pipe_config->name.tu, \
11607 pipe_config->name.gmch_m, \
11608 pipe_config->name.gmch_n, \
11609 pipe_config->name.link_m, \
11610 pipe_config->name.link_n); \
11611 ret = false; \
eadd2721
VS
11612 } \
11613} while (0)
cfb23ed6 11614
55c561a7
SV
11615/* This is required for BDW+ where there is only one set of registers for
11616 * switching between high and low RR.
11617 * This macro can be used whenever a comparison has to be made between one
11618 * hw state and multiple sw state variables.
11619 */
eadd2721 11620#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
cfb23ed6
ML
11621 if (!intel_compare_link_m_n(&current_config->name, \
11622 &pipe_config->name, adjust) && \
11623 !intel_compare_link_m_n(&current_config->alt_name, \
11624 &pipe_config->name, adjust)) { \
4e8048f8 11625 pipe_config_err(adjust, __stringify(name), \
cfb23ed6
ML
11626 "(expected tu %i gmch %i/%i link %i/%i, " \
11627 "or tu %i gmch %i/%i link %i/%i, " \
11628 "found tu %i, gmch %i/%i link %i/%i)\n", \
11629 current_config->name.tu, \
11630 current_config->name.gmch_m, \
11631 current_config->name.gmch_n, \
11632 current_config->name.link_m, \
11633 current_config->name.link_n, \
11634 current_config->alt_name.tu, \
11635 current_config->alt_name.gmch_m, \
11636 current_config->alt_name.gmch_n, \
11637 current_config->alt_name.link_m, \
11638 current_config->alt_name.link_n, \
11639 pipe_config->name.tu, \
11640 pipe_config->name.gmch_m, \
11641 pipe_config->name.gmch_n, \
11642 pipe_config->name.link_m, \
11643 pipe_config->name.link_n); \
11644 ret = false; \
eadd2721
VS
11645 } \
11646} while (0)
88adfff1 11647
eadd2721 11648#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
1bd1bd80 11649 if ((current_config->name ^ pipe_config->name) & (mask)) { \
4e8048f8
TU
11650 pipe_config_err(adjust, __stringify(name), \
11651 "(%x) (expected %i, found %i)\n", \
11652 (mask), \
1bd1bd80
SV
11653 current_config->name & (mask), \
11654 pipe_config->name & (mask)); \
cfb23ed6 11655 ret = false; \
eadd2721
VS
11656 } \
11657} while (0)
1bd1bd80 11658
eadd2721 11659#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
5e550656 11660 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
4e8048f8 11661 pipe_config_err(adjust, __stringify(name), \
5e550656
VS
11662 "(expected %i, found %i)\n", \
11663 current_config->name, \
11664 pipe_config->name); \
cfb23ed6 11665 ret = false; \
eadd2721
VS
11666 } \
11667} while (0)
5e550656 11668
bb760063
SV
11669#define PIPE_CONF_QUIRK(quirk) \
11670 ((current_config->quirks | pipe_config->quirks) & (quirk))
11671
eccb140b
SV
11672 PIPE_CONF_CHECK_I(cpu_transcoder);
11673
d640bf79 11674 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
08a24034 11675 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 11676 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 11677
90a6b7b0 11678 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 11679 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 11680
6315b5d3 11681 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
11682 PIPE_CONF_CHECK_M_N(dp_m_n);
11683
cfb23ed6
ML
11684 if (current_config->has_drrs)
11685 PIPE_CONF_CHECK_M_N(dp_m2_n2);
11686 } else
11687 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 11688
253c84c8 11689 PIPE_CONF_CHECK_X(output_types);
a65347ba 11690
2d112de7
ACO
11691 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11692 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11693 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11694 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11695 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11696 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11697
2d112de7
ACO
11698 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11699 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11700 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11701 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11702 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11703 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11704
c93f54cf 11705 PIPE_CONF_CHECK_I(pixel_multiplier);
d640bf79 11706 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
772c2a51 11707 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 11708 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
d640bf79 11709 PIPE_CONF_CHECK_BOOL(limited_color_range);
15953637 11710
d640bf79
ML
11711 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
11712 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
4493e098 11713 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
d640bf79 11714 PIPE_CONF_CHECK_BOOL(ycbcr420);
6c49f241 11715
4493e098 11716 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
9ed109a7 11717
2d112de7 11718 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
SV
11719 DRM_MODE_FLAG_INTERLACE);
11720
bb760063 11721 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11722 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11723 DRM_MODE_FLAG_PHSYNC);
2d112de7 11724 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11725 DRM_MODE_FLAG_NHSYNC);
2d112de7 11726 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11727 DRM_MODE_FLAG_PVSYNC);
2d112de7 11728 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
SV
11729 DRM_MODE_FLAG_NVSYNC);
11730 }
045ac3b5 11731
333b8ca8 11732 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 11733 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 11734 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 11735 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 11736 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 11737
bfd16b2a
ML
11738 if (!adjust) {
11739 PIPE_CONF_CHECK_I(pipe_src_w);
11740 PIPE_CONF_CHECK_I(pipe_src_h);
11741
d640bf79 11742 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
bfd16b2a
ML
11743 if (current_config->pch_pfit.enabled) {
11744 PIPE_CONF_CHECK_X(pch_pfit.pos);
11745 PIPE_CONF_CHECK_X(pch_pfit.size);
11746 }
2fa2fe9a 11747
7aefe2b5 11748 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
a7d1b3f4 11749 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7aefe2b5 11750 }
a1b2278e 11751
d640bf79 11752 PIPE_CONF_CHECK_BOOL(double_wide);
282740f7 11753
8106ddbd 11754 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 11755 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11756 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
SV
11757 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11758 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11759 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 11760 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
11761 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11762 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11763 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
2de38138
PZ
11764 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
11765 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
11766 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
11767 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
11768 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
11769 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
11770 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
11771 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
11772 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
11773 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
11774 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
11775 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
c27e917e
PZ
11776 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
11777 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
11778 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
11779 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
11780 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
11781 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
11782 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
11783 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
11784 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
11785 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
c0d43d62 11786
47eacbab
VS
11787 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
11788 PIPE_CONF_CHECK_X(dsi_pll.div);
11789
9beb5fea 11790 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
11791 PIPE_CONF_CHECK_I(pipe_bpp);
11792
2d112de7 11793 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11794 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11795
53e9bf5e
VS
11796 PIPE_CONF_CHECK_I(min_voltage_level);
11797
66e985c0 11798#undef PIPE_CONF_CHECK_X
08a24034 11799#undef PIPE_CONF_CHECK_I
d640bf79 11800#undef PIPE_CONF_CHECK_BOOL
4493e098 11801#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
8106ddbd 11802#undef PIPE_CONF_CHECK_P
1bd1bd80 11803#undef PIPE_CONF_CHECK_FLAGS
5e550656 11804#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11805#undef PIPE_CONF_QUIRK
88adfff1 11806
cfb23ed6 11807 return ret;
0e8ffe1b
SV
11808}
11809
e3b247da
VS
11810static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
11811 const struct intel_crtc_state *pipe_config)
11812{
11813 if (pipe_config->has_pch_encoder) {
21a727b3 11814 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
11815 &pipe_config->fdi_m_n);
11816 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
11817
11818 /*
11819 * FDI already provided one idea for the dotclock.
11820 * Yell if the encoder disagrees.
11821 */
11822 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
11823 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11824 fdi_dotclock, dotclock);
11825 }
11826}
11827
c0ead703
ML
11828static void verify_wm_state(struct drm_crtc *crtc,
11829 struct drm_crtc_state *new_state)
08db6652 11830{
6315b5d3 11831 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 11832 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 11833 struct skl_pipe_wm hw_wm, *sw_wm;
11834 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
11835 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
11836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11837 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 11838 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 11839
6315b5d3 11840 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
11841 return;
11842
3de8a14c 11843 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 11844 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 11845
08db6652
DL
11846 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11847 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11848
74bd8004
MK
11849 if (INTEL_GEN(dev_priv) >= 11)
11850 if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
11851 DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
11852 sw_ddb->enabled_slices,
11853 hw_ddb.enabled_slices);
e7c84544 11854 /* planes */
8b364b41 11855 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 11856 hw_plane_wm = &hw_wm.planes[plane];
11857 sw_plane_wm = &sw_wm->planes[plane];
08db6652 11858
3de8a14c 11859 /* Watermarks */
11860 for (level = 0; level <= max_level; level++) {
11861 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11862 &sw_plane_wm->wm[level]))
11863 continue;
11864
11865 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",
11866 pipe_name(pipe), plane + 1, level,
11867 sw_plane_wm->wm[level].plane_en,
11868 sw_plane_wm->wm[level].plane_res_b,
11869 sw_plane_wm->wm[level].plane_res_l,
11870 hw_plane_wm->wm[level].plane_en,
11871 hw_plane_wm->wm[level].plane_res_b,
11872 hw_plane_wm->wm[level].plane_res_l);
11873 }
08db6652 11874
3de8a14c 11875 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11876 &sw_plane_wm->trans_wm)) {
11877 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",
11878 pipe_name(pipe), plane + 1,
11879 sw_plane_wm->trans_wm.plane_en,
11880 sw_plane_wm->trans_wm.plane_res_b,
11881 sw_plane_wm->trans_wm.plane_res_l,
11882 hw_plane_wm->trans_wm.plane_en,
11883 hw_plane_wm->trans_wm.plane_res_b,
11884 hw_plane_wm->trans_wm.plane_res_l);
11885 }
11886
11887 /* DDB */
11888 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
11889 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
11890
11891 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11892 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 11893 pipe_name(pipe), plane + 1,
11894 sw_ddb_entry->start, sw_ddb_entry->end,
11895 hw_ddb_entry->start, hw_ddb_entry->end);
11896 }
e7c84544 11897 }
08db6652 11898
27082493
L
11899 /*
11900 * cursor
11901 * If the cursor plane isn't active, we may not have updated it's ddb
11902 * allocation. In that case since the ddb allocation will be updated
11903 * once the plane becomes visible, we can skip this check
11904 */
cd5dcbf1 11905 if (1) {
3de8a14c 11906 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
11907 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
11908
11909 /* Watermarks */
11910 for (level = 0; level <= max_level; level++) {
11911 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
11912 &sw_plane_wm->wm[level]))
11913 continue;
11914
11915 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",
11916 pipe_name(pipe), level,
11917 sw_plane_wm->wm[level].plane_en,
11918 sw_plane_wm->wm[level].plane_res_b,
11919 sw_plane_wm->wm[level].plane_res_l,
11920 hw_plane_wm->wm[level].plane_en,
11921 hw_plane_wm->wm[level].plane_res_b,
11922 hw_plane_wm->wm[level].plane_res_l);
11923 }
11924
11925 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
11926 &sw_plane_wm->trans_wm)) {
11927 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
11928 pipe_name(pipe),
11929 sw_plane_wm->trans_wm.plane_en,
11930 sw_plane_wm->trans_wm.plane_res_b,
11931 sw_plane_wm->trans_wm.plane_res_l,
11932 hw_plane_wm->trans_wm.plane_en,
11933 hw_plane_wm->trans_wm.plane_res_b,
11934 hw_plane_wm->trans_wm.plane_res_l);
11935 }
11936
11937 /* DDB */
11938 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
11939 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 11940
3de8a14c 11941 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 11942 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 11943 pipe_name(pipe),
3de8a14c 11944 sw_ddb_entry->start, sw_ddb_entry->end,
11945 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 11946 }
08db6652
DL
11947 }
11948}
11949
91d1b4bd 11950static void
677100ce
ML
11951verify_connector_state(struct drm_device *dev,
11952 struct drm_atomic_state *state,
11953 struct drm_crtc *crtc)
8af6cf88 11954{
35dd3c64 11955 struct drm_connector *connector;
aa5e9b47 11956 struct drm_connector_state *new_conn_state;
677100ce 11957 int i;
8af6cf88 11958
aa5e9b47 11959 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
35dd3c64 11960 struct drm_encoder *encoder = connector->encoder;
749d98b8 11961 struct drm_crtc_state *crtc_state = NULL;
ad3c558f 11962
aa5e9b47 11963 if (new_conn_state->crtc != crtc)
e7c84544
ML
11964 continue;
11965
749d98b8
ML
11966 if (crtc)
11967 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
11968
11969 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 11970
aa5e9b47 11971 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 11972 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 11973 }
91d1b4bd
SV
11974}
11975
11976static void
86b04268 11977verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
91d1b4bd
SV
11978{
11979 struct intel_encoder *encoder;
86b04268
SV
11980 struct drm_connector *connector;
11981 struct drm_connector_state *old_conn_state, *new_conn_state;
11982 int i;
8af6cf88 11983
b2784e15 11984 for_each_intel_encoder(dev, encoder) {
86b04268 11985 bool enabled = false, found = false;
4d20cd86 11986 enum pipe pipe;
8af6cf88
SV
11987
11988 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11989 encoder->base.base.id,
8e329a03 11990 encoder->base.name);
8af6cf88 11991
86b04268
SV
11992 for_each_oldnew_connector_in_state(state, connector, old_conn_state,
11993 new_conn_state, i) {
11994 if (old_conn_state->best_encoder == &encoder->base)
11995 found = true;
11996
11997 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 11998 continue;
86b04268 11999 found = enabled = true;
ad3c558f 12000
86b04268 12001 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
12002 encoder->base.crtc,
12003 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12004 }
86b04268
SV
12005
12006 if (!found)
12007 continue;
0e32b39c 12008
e2c719b7 12009 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
SV
12010 "encoder's enabled state mismatch "
12011 "(expected %i, found %i)\n",
12012 !!encoder->base.crtc, enabled);
7c60d198
ML
12013
12014 if (!encoder->base.crtc) {
4d20cd86 12015 bool active;
7c60d198 12016
4d20cd86
ML
12017 active = encoder->get_hw_state(encoder, &pipe);
12018 I915_STATE_WARN(active,
12019 "encoder detached but still enabled on pipe %c.\n",
12020 pipe_name(pipe));
7c60d198 12021 }
8af6cf88 12022 }
91d1b4bd
SV
12023}
12024
12025static void
c0ead703
ML
12026verify_crtc_state(struct drm_crtc *crtc,
12027 struct drm_crtc_state *old_crtc_state,
12028 struct drm_crtc_state *new_crtc_state)
91d1b4bd 12029{
e7c84544 12030 struct drm_device *dev = crtc->dev;
fac5e23e 12031 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 12032 struct intel_encoder *encoder;
e7c84544
ML
12033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12034 struct intel_crtc_state *pipe_config, *sw_config;
12035 struct drm_atomic_state *old_state;
12036 bool active;
045ac3b5 12037
e7c84544 12038 old_state = old_crtc_state->state;
ec2dc6a0 12039 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
12040 pipe_config = to_intel_crtc_state(old_crtc_state);
12041 memset(pipe_config, 0, sizeof(*pipe_config));
12042 pipe_config->base.crtc = crtc;
12043 pipe_config->base.state = old_state;
8af6cf88 12044
78108b7c 12045 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 12046
e7c84544 12047 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 12048
e56134bc
VS
12049 /* we keep both pipes enabled on 830 */
12050 if (IS_I830(dev_priv))
e7c84544 12051 active = new_crtc_state->active;
6c49f241 12052
e7c84544
ML
12053 I915_STATE_WARN(new_crtc_state->active != active,
12054 "crtc active state doesn't match with hw state "
12055 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 12056
e7c84544
ML
12057 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12058 "transitional active state does not match atomic hw state "
12059 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 12060
e7c84544
ML
12061 for_each_encoder_on_crtc(dev, crtc, encoder) {
12062 enum pipe pipe;
4d20cd86 12063
e7c84544
ML
12064 active = encoder->get_hw_state(encoder, &pipe);
12065 I915_STATE_WARN(active != new_crtc_state->active,
12066 "[ENCODER:%i] active %i with crtc active %i\n",
12067 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 12068
e7c84544
ML
12069 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12070 "Encoder connected to wrong pipe %c\n",
12071 pipe_name(pipe));
4d20cd86 12072
e1214b95 12073 if (active)
e7c84544
ML
12074 encoder->get_config(encoder, pipe_config);
12075 }
53d9f4e9 12076
a7d1b3f4
VS
12077 intel_crtc_compute_pixel_rate(pipe_config);
12078
e7c84544
ML
12079 if (!new_crtc_state->active)
12080 return;
cfb23ed6 12081
e7c84544 12082 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 12083
749d98b8 12084 sw_config = to_intel_crtc_state(new_crtc_state);
6315b5d3 12085 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
12086 pipe_config, false)) {
12087 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12088 intel_dump_pipe_config(intel_crtc, pipe_config,
12089 "[hw state]");
12090 intel_dump_pipe_config(intel_crtc, sw_config,
12091 "[sw state]");
8af6cf88
SV
12092 }
12093}
12094
cff109f0
VS
12095static void
12096intel_verify_planes(struct intel_atomic_state *state)
12097{
12098 struct intel_plane *plane;
12099 const struct intel_plane_state *plane_state;
12100 int i;
12101
12102 for_each_new_intel_plane_in_state(state, plane,
12103 plane_state, i)
12104 assert_plane(plane, plane_state->base.visible);
12105}
12106
91d1b4bd 12107static void
c0ead703
ML
12108verify_single_dpll_state(struct drm_i915_private *dev_priv,
12109 struct intel_shared_dpll *pll,
12110 struct drm_crtc *crtc,
12111 struct drm_crtc_state *new_state)
91d1b4bd 12112{
91d1b4bd 12113 struct intel_dpll_hw_state dpll_hw_state;
40560e26 12114 unsigned int crtc_mask;
e7c84544 12115 bool active;
5358901f 12116
e7c84544 12117 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 12118
72f775fa 12119 DRM_DEBUG_KMS("%s\n", pll->info->name);
5358901f 12120
ee1398ba 12121 active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12122
5cd281f6 12123 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
e7c84544
ML
12124 I915_STATE_WARN(!pll->on && pll->active_mask,
12125 "pll in active use but not on in sw tracking\n");
12126 I915_STATE_WARN(pll->on && !pll->active_mask,
12127 "pll is on but not used by any active crtc\n");
12128 I915_STATE_WARN(pll->on != active,
12129 "pll on state mismatch (expected %i, found %i)\n",
12130 pll->on, active);
12131 }
5358901f 12132
e7c84544 12133 if (!crtc) {
2c42e535 12134 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 12135 "more active pll users than references: %x vs %x\n",
2c42e535 12136 pll->active_mask, pll->state.crtc_mask);
5358901f 12137
e7c84544
ML
12138 return;
12139 }
12140
40560e26 12141 crtc_mask = drm_crtc_mask(crtc);
e7c84544
ML
12142
12143 if (new_state->active)
12144 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12145 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12146 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12147 else
12148 I915_STATE_WARN(pll->active_mask & crtc_mask,
12149 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12150 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 12151
2c42e535 12152 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 12153 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 12154 crtc_mask, pll->state.crtc_mask);
66e985c0 12155
2c42e535 12156 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
12157 &dpll_hw_state,
12158 sizeof(dpll_hw_state)),
12159 "pll hw state mismatch\n");
12160}
12161
12162static void
c0ead703
ML
12163verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12164 struct drm_crtc_state *old_crtc_state,
12165 struct drm_crtc_state *new_crtc_state)
e7c84544 12166{
fac5e23e 12167 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12168 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12169 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12170
12171 if (new_state->shared_dpll)
c0ead703 12172 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
12173
12174 if (old_state->shared_dpll &&
12175 old_state->shared_dpll != new_state->shared_dpll) {
40560e26 12176 unsigned int crtc_mask = drm_crtc_mask(crtc);
e7c84544
ML
12177 struct intel_shared_dpll *pll = old_state->shared_dpll;
12178
12179 I915_STATE_WARN(pll->active_mask & crtc_mask,
12180 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12181 pipe_name(drm_crtc_index(crtc)));
2c42e535 12182 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544
ML
12183 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12184 pipe_name(drm_crtc_index(crtc)));
5358901f 12185 }
8af6cf88
SV
12186}
12187
e7c84544 12188static void
c0ead703 12189intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
12190 struct drm_atomic_state *state,
12191 struct drm_crtc_state *old_state,
12192 struct drm_crtc_state *new_state)
e7c84544 12193{
5a21b665
SV
12194 if (!needs_modeset(new_state) &&
12195 !to_intel_crtc_state(new_state)->update_pipe)
12196 return;
12197
c0ead703 12198 verify_wm_state(crtc, new_state);
677100ce 12199 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
12200 verify_crtc_state(crtc, old_state, new_state);
12201 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
12202}
12203
12204static void
c0ead703 12205verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 12206{
fac5e23e 12207 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
12208 int i;
12209
12210 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 12211 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
12212}
12213
12214static void
677100ce
ML
12215intel_modeset_verify_disabled(struct drm_device *dev,
12216 struct drm_atomic_state *state)
e7c84544 12217{
86b04268 12218 verify_encoder_state(dev, state);
677100ce 12219 verify_connector_state(dev, state, NULL);
c0ead703 12220 verify_disabled_dpll_state(dev);
e7c84544
ML
12221}
12222
80715b2f
VS
12223static void update_scanline_offset(struct intel_crtc *crtc)
12224{
4f8036a2 12225 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
12226
12227 /*
12228 * The scanline counter increments at the leading edge of hsync.
12229 *
12230 * On most platforms it starts counting from vtotal-1 on the
12231 * first active line. That means the scanline counter value is
12232 * always one less than what we would expect. Ie. just after
12233 * start of vblank, which also occurs at start of hsync (on the
12234 * last active line), the scanline counter will read vblank_start-1.
12235 *
12236 * On gen2 the scanline counter starts counting from 1 instead
12237 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12238 * to keep the value positive), instead of adding one.
12239 *
12240 * On HSW+ the behaviour of the scanline counter depends on the output
12241 * type. For DP ports it behaves like most other platforms, but on HDMI
12242 * there's an extra 1 line difference. So we need to add two instead of
12243 * one to the value.
ec1b4ee2
VS
12244 *
12245 * On VLV/CHV DSI the scanline counter would appear to increment
12246 * approx. 1/3 of a scanline before start of vblank. Unfortunately
12247 * that means we can't tell whether we're in vblank or not while
12248 * we're on that particular line. We must still set scanline_offset
12249 * to 1 so that the vblank timestamps come out correct when we query
12250 * the scanline counter from within the vblank interrupt handler.
12251 * However if queried just before the start of vblank we'll get an
12252 * answer that's slightly in the future.
80715b2f 12253 */
4f8036a2 12254 if (IS_GEN2(dev_priv)) {
124abe07 12255 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12256 int vtotal;
12257
124abe07
VS
12258 vtotal = adjusted_mode->crtc_vtotal;
12259 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12260 vtotal /= 2;
12261
12262 crtc->scanline_offset = vtotal - 1;
4f8036a2 12263 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 12264 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12265 crtc->scanline_offset = 2;
12266 } else
12267 crtc->scanline_offset = 1;
12268}
12269
ad421372 12270static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12271{
225da59b 12272 struct drm_device *dev = state->dev;
ed6739ef 12273 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12274 struct drm_crtc *crtc;
aa5e9b47 12275 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
0a9ab303 12276 int i;
ed6739ef
ACO
12277
12278 if (!dev_priv->display.crtc_compute_clock)
ad421372 12279 return;
ed6739ef 12280
aa5e9b47 12281 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
fb1a38a9 12282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd 12283 struct intel_shared_dpll *old_dpll =
aa5e9b47 12284 to_intel_crtc_state(old_crtc_state)->shared_dpll;
0a9ab303 12285
aa5e9b47 12286 if (!needs_modeset(new_crtc_state))
225da59b
ACO
12287 continue;
12288
aa5e9b47 12289 to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
fb1a38a9 12290
8106ddbd 12291 if (!old_dpll)
fb1a38a9 12292 continue;
0a9ab303 12293
a1c414ee 12294 intel_release_shared_dpll(old_dpll, intel_crtc, state);
ad421372 12295 }
ed6739ef
ACO
12296}
12297
99d736a2
ML
12298/*
12299 * This implements the workaround described in the "notes" section of the mode
12300 * set sequence documentation. When going from no pipes or single pipe to
12301 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12302 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12303 */
12304static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12305{
12306 struct drm_crtc_state *crtc_state;
12307 struct intel_crtc *intel_crtc;
12308 struct drm_crtc *crtc;
12309 struct intel_crtc_state *first_crtc_state = NULL;
12310 struct intel_crtc_state *other_crtc_state = NULL;
12311 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12312 int i;
12313
12314 /* look at all crtc's that are going to be enabled in during modeset */
aa5e9b47 12315 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
99d736a2
ML
12316 intel_crtc = to_intel_crtc(crtc);
12317
12318 if (!crtc_state->active || !needs_modeset(crtc_state))
12319 continue;
12320
12321 if (first_crtc_state) {
12322 other_crtc_state = to_intel_crtc_state(crtc_state);
12323 break;
12324 } else {
12325 first_crtc_state = to_intel_crtc_state(crtc_state);
12326 first_pipe = intel_crtc->pipe;
12327 }
12328 }
12329
12330 /* No workaround needed? */
12331 if (!first_crtc_state)
12332 return 0;
12333
12334 /* w/a possibly needed, check how many crtc's are already enabled. */
12335 for_each_intel_crtc(state->dev, intel_crtc) {
12336 struct intel_crtc_state *pipe_config;
12337
12338 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12339 if (IS_ERR(pipe_config))
12340 return PTR_ERR(pipe_config);
12341
12342 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12343
12344 if (!pipe_config->base.active ||
12345 needs_modeset(&pipe_config->base))
12346 continue;
12347
12348 /* 2 or more enabled crtcs means no need for w/a */
12349 if (enabled_pipe != INVALID_PIPE)
12350 return 0;
12351
12352 enabled_pipe = intel_crtc->pipe;
12353 }
12354
12355 if (enabled_pipe != INVALID_PIPE)
12356 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12357 else if (other_crtc_state)
12358 other_crtc_state->hsw_workaround_pipe = first_pipe;
12359
12360 return 0;
12361}
12362
8d96561a
VS
12363static int intel_lock_all_pipes(struct drm_atomic_state *state)
12364{
12365 struct drm_crtc *crtc;
12366
12367 /* Add all pipes to the state */
12368 for_each_crtc(state->dev, crtc) {
12369 struct drm_crtc_state *crtc_state;
12370
12371 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12372 if (IS_ERR(crtc_state))
12373 return PTR_ERR(crtc_state);
12374 }
12375
12376 return 0;
12377}
12378
27c329ed
ML
12379static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12380{
12381 struct drm_crtc *crtc;
27c329ed 12382
8d96561a
VS
12383 /*
12384 * Add all pipes to the state, and force
12385 * a modeset on all the active ones.
12386 */
27c329ed 12387 for_each_crtc(state->dev, crtc) {
9780aad5
VS
12388 struct drm_crtc_state *crtc_state;
12389 int ret;
12390
27c329ed
ML
12391 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12392 if (IS_ERR(crtc_state))
12393 return PTR_ERR(crtc_state);
12394
12395 if (!crtc_state->active || needs_modeset(crtc_state))
12396 continue;
12397
12398 crtc_state->mode_changed = true;
12399
12400 ret = drm_atomic_add_affected_connectors(state, crtc);
12401 if (ret)
9780aad5 12402 return ret;
27c329ed
ML
12403
12404 ret = drm_atomic_add_affected_planes(state, crtc);
12405 if (ret)
9780aad5 12406 return ret;
27c329ed
ML
12407 }
12408
9780aad5 12409 return 0;
27c329ed
ML
12410}
12411
c347a676 12412static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 12413{
565602d7 12414 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12415 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7 12416 struct drm_crtc *crtc;
aa5e9b47 12417 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
565602d7 12418 int ret = 0, i;
054518dd 12419
b359283a
ML
12420 if (!check_digital_port_conflicts(state)) {
12421 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12422 return -EINVAL;
12423 }
12424
565602d7
ML
12425 intel_state->modeset = true;
12426 intel_state->active_crtcs = dev_priv->active_crtcs;
bb0f4aab
VS
12427 intel_state->cdclk.logical = dev_priv->cdclk.logical;
12428 intel_state->cdclk.actual = dev_priv->cdclk.actual;
565602d7 12429
aa5e9b47
ML
12430 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12431 if (new_crtc_state->active)
565602d7
ML
12432 intel_state->active_crtcs |= 1 << i;
12433 else
12434 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05 12435
aa5e9b47 12436 if (old_crtc_state->active != new_crtc_state->active)
8b4a7d05 12437 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
12438 }
12439
054518dd
ACO
12440 /*
12441 * See if the config requires any additional preparation, e.g.
12442 * to adjust global state with pipes off. We need to do this
12443 * here so we can get the modeset_pipe updated config for the new
12444 * mode set on this crtc. For other crtcs we need to use the
12445 * adjusted_mode bits in the crtc directly.
12446 */
27c329ed 12447 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed 12448 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
12449 if (ret < 0)
12450 return ret;
27c329ed 12451
8d96561a 12452 /*
bb0f4aab 12453 * Writes to dev_priv->cdclk.logical must protected by
8d96561a
VS
12454 * holding all the crtc locks, even if we don't end up
12455 * touching the hardware
12456 */
64600bd5
VS
12457 if (intel_cdclk_changed(&dev_priv->cdclk.logical,
12458 &intel_state->cdclk.logical)) {
8d96561a
VS
12459 ret = intel_lock_all_pipes(state);
12460 if (ret < 0)
12461 return ret;
12462 }
12463
12464 /* All pipes must be switched off while we change the cdclk. */
64600bd5
VS
12465 if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
12466 &intel_state->cdclk.actual)) {
27c329ed 12467 ret = intel_modeset_all_pipes(state);
8d96561a
VS
12468 if (ret < 0)
12469 return ret;
12470 }
e8788cbc 12471
bb0f4aab
VS
12472 DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
12473 intel_state->cdclk.logical.cdclk,
12474 intel_state->cdclk.actual.cdclk);
53e9bf5e
VS
12475 DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
12476 intel_state->cdclk.logical.voltage_level,
12477 intel_state->cdclk.actual.voltage_level);
e0ca7a6b 12478 } else {
bb0f4aab 12479 to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12480 }
054518dd 12481
ad421372 12482 intel_modeset_clear_plls(state);
054518dd 12483
565602d7 12484 if (IS_HASWELL(dev_priv))
ad421372 12485 return haswell_mode_set_planes_workaround(state);
99d736a2 12486
ad421372 12487 return 0;
c347a676
ACO
12488}
12489
aa363136
MR
12490/*
12491 * Handle calculation of various watermark data at the end of the atomic check
12492 * phase. The code here should be run after the per-crtc and per-plane 'check'
12493 * handlers to ensure that all derived state has been updated.
12494 */
55994c2c 12495static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
12496{
12497 struct drm_device *dev = state->dev;
98d39494 12498 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
12499
12500 /* Is there platform-specific watermark information to calculate? */
12501 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
12502 return dev_priv->display.compute_global_watermarks(state);
12503
12504 return 0;
aa363136
MR
12505}
12506
74c090b1
ML
12507/**
12508 * intel_atomic_check - validate state object
12509 * @dev: drm device
12510 * @state: state to validate
12511 */
12512static int intel_atomic_check(struct drm_device *dev,
12513 struct drm_atomic_state *state)
c347a676 12514{
dd8b3bdb 12515 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 12516 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676 12517 struct drm_crtc *crtc;
aa5e9b47 12518 struct drm_crtc_state *old_crtc_state, *crtc_state;
c347a676 12519 int ret, i;
61333b60 12520 bool any_ms = false;
c347a676 12521
8c58f73c
ML
12522 /* Catch I915_MODE_FLAG_INHERITED */
12523 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
12524 crtc_state, i) {
12525 if (crtc_state->mode.private_flags !=
12526 old_crtc_state->mode.private_flags)
12527 crtc_state->mode_changed = true;
12528 }
12529
74c090b1 12530 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
12531 if (ret)
12532 return ret;
12533
aa5e9b47 12534 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
cfb23ed6
ML
12535 struct intel_crtc_state *pipe_config =
12536 to_intel_crtc_state(crtc_state);
1ed51de9 12537
af4a879e 12538 if (!needs_modeset(crtc_state))
c347a676
ACO
12539 continue;
12540
af4a879e
SV
12541 if (!crtc_state->enable) {
12542 any_ms = true;
cfb23ed6 12543 continue;
af4a879e 12544 }
cfb23ed6 12545
cfb23ed6 12546 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
12547 if (ret) {
12548 intel_dump_pipe_config(to_intel_crtc(crtc),
12549 pipe_config, "[failed]");
c347a676 12550 return ret;
25aa1c39 12551 }
c347a676 12552
4f044a88 12553 if (i915_modparams.fastboot &&
6315b5d3 12554 intel_pipe_config_compare(dev_priv,
aa5e9b47 12555 to_intel_crtc_state(old_crtc_state),
1ed51de9 12556 pipe_config, true)) {
26495481 12557 crtc_state->mode_changed = false;
aa5e9b47 12558 pipe_config->update_pipe = true;
26495481
SV
12559 }
12560
af4a879e 12561 if (needs_modeset(crtc_state))
26495481 12562 any_ms = true;
cfb23ed6 12563
26495481
SV
12564 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12565 needs_modeset(crtc_state) ?
12566 "[modeset]" : "[fastset]");
c347a676
ACO
12567 }
12568
61333b60
ML
12569 if (any_ms) {
12570 ret = intel_modeset_checks(state);
12571
12572 if (ret)
12573 return ret;
e0ca7a6b 12574 } else {
bb0f4aab 12575 intel_state->cdclk.logical = dev_priv->cdclk.logical;
e0ca7a6b 12576 }
76305b1a 12577
dd8b3bdb 12578 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
12579 if (ret)
12580 return ret;
12581
dd57602e 12582 intel_fbc_choose_crtc(dev_priv, intel_state);
55994c2c 12583 return calc_watermark_data(state);
054518dd
ACO
12584}
12585
5008e874 12586static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 12587 struct drm_atomic_state *state)
5008e874 12588{
fd70075f 12589 return drm_atomic_helper_prepare_planes(dev, state);
5008e874
ML
12590}
12591
a2991414
ML
12592u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
12593{
12594 struct drm_device *dev = crtc->base.dev;
12595
12596 if (!dev->max_vblank_count)
734cbbf3 12597 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
a2991414
ML
12598
12599 return dev->driver->get_vblank_counter(dev, crtc->pipe);
12600}
12601
896e5bb0
L
12602static void intel_update_crtc(struct drm_crtc *crtc,
12603 struct drm_atomic_state *state,
12604 struct drm_crtc_state *old_crtc_state,
b44d5c0c 12605 struct drm_crtc_state *new_crtc_state)
896e5bb0
L
12606{
12607 struct drm_device *dev = crtc->dev;
12608 struct drm_i915_private *dev_priv = to_i915(dev);
12609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
aa5e9b47
ML
12610 struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
12611 bool modeset = needs_modeset(new_crtc_state);
8b69449d
ML
12612 struct intel_plane_state *new_plane_state =
12613 intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
12614 to_intel_plane(crtc->primary));
896e5bb0
L
12615
12616 if (modeset) {
12617 update_scanline_offset(intel_crtc);
12618 dev_priv->display.crtc_enable(pipe_config, state);
033b7a23
ML
12619
12620 /* vblanks work again, re-enable pipe CRC. */
12621 intel_crtc_enable_pipe_crc(intel_crtc);
896e5bb0 12622 } else {
aa5e9b47
ML
12623 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12624 pipe_config);
896e5bb0
L
12625 }
12626
8b69449d
ML
12627 if (new_plane_state)
12628 intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
896e5bb0
L
12629
12630 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
896e5bb0
L
12631}
12632
b44d5c0c 12633static void intel_update_crtcs(struct drm_atomic_state *state)
896e5bb0
L
12634{
12635 struct drm_crtc *crtc;
aa5e9b47 12636 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
896e5bb0
L
12637 int i;
12638
aa5e9b47
ML
12639 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
12640 if (!new_crtc_state->active)
896e5bb0
L
12641 continue;
12642
12643 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12644 new_crtc_state);
896e5bb0
L
12645 }
12646}
12647
b44d5c0c 12648static void skl_update_crtcs(struct drm_atomic_state *state)
27082493 12649{
0f0f74bc 12650 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
12651 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
12652 struct drm_crtc *crtc;
ce0ba283 12653 struct intel_crtc *intel_crtc;
aa5e9b47 12654 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
ce0ba283 12655 struct intel_crtc_state *cstate;
27082493
L
12656 unsigned int updated = 0;
12657 bool progress;
12658 enum pipe pipe;
5eff503b 12659 int i;
aa9664ff
MK
12660 u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
12661 u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
5eff503b
ML
12662
12663 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
12664
aa5e9b47 12665 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
5eff503b 12666 /* ignore allocations for crtc's that have been turned off. */
aa5e9b47 12667 if (new_crtc_state->active)
5eff503b 12668 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493 12669
aa9664ff
MK
12670 /* If 2nd DBuf slice required, enable it here */
12671 if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
12672 icl_dbuf_slices_update(dev_priv, required_slices);
12673
27082493
L
12674 /*
12675 * Whenever the number of active pipes changes, we need to make sure we
12676 * update the pipes in the right order so that their ddb allocations
12677 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
12678 * cause pipe underruns and other bad stuff.
12679 */
12680 do {
27082493
L
12681 progress = false;
12682
aa5e9b47 12683 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
27082493
L
12684 bool vbl_wait = false;
12685 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
12686
12687 intel_crtc = to_intel_crtc(crtc);
21794813 12688 cstate = to_intel_crtc_state(new_crtc_state);
ce0ba283 12689 pipe = intel_crtc->pipe;
27082493 12690
5eff503b 12691 if (updated & cmask || !cstate->base.active)
27082493 12692 continue;
5eff503b 12693
2b68504b
MK
12694 if (skl_ddb_allocation_overlaps(dev_priv,
12695 entries,
12696 &cstate->wm.skl.ddb,
12697 i))
27082493
L
12698 continue;
12699
12700 updated |= cmask;
5eff503b 12701 entries[i] = &cstate->wm.skl.ddb;
27082493
L
12702
12703 /*
12704 * If this is an already active pipe, it's DDB changed,
12705 * and this isn't the last pipe that needs updating
12706 * then we need to wait for a vblank to pass for the
12707 * new ddb allocation to take effect.
12708 */
ce0ba283 12709 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 12710 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
aa5e9b47 12711 !new_crtc_state->active_changed &&
27082493
L
12712 intel_state->wm_results.dirty_pipes != updated)
12713 vbl_wait = true;
12714
12715 intel_update_crtc(crtc, state, old_crtc_state,
b44d5c0c 12716 new_crtc_state);
27082493
L
12717
12718 if (vbl_wait)
0f0f74bc 12719 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
12720
12721 progress = true;
12722 }
12723 } while (progress);
aa9664ff
MK
12724
12725 /* If 2nd DBuf slice is no more required disable it */
12726 if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
12727 icl_dbuf_slices_update(dev_priv, required_slices);
27082493
L
12728}
12729
ba318c61
CW
12730static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
12731{
12732 struct intel_atomic_state *state, *next;
12733 struct llist_node *freed;
12734
12735 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
12736 llist_for_each_entry_safe(state, next, freed, freed)
12737 drm_atomic_state_put(&state->base);
12738}
12739
12740static void intel_atomic_helper_free_state_worker(struct work_struct *work)
12741{
12742 struct drm_i915_private *dev_priv =
12743 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
12744
12745 intel_atomic_helper_free_state(dev_priv);
12746}
12747
9db529aa
SV
12748static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
12749{
12750 struct wait_queue_entry wait_fence, wait_reset;
12751 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
12752
12753 init_wait_entry(&wait_fence, 0);
12754 init_wait_entry(&wait_reset, 0);
12755 for (;;) {
12756 prepare_to_wait(&intel_state->commit_ready.wait,
12757 &wait_fence, TASK_UNINTERRUPTIBLE);
12758 prepare_to_wait(&dev_priv->gpu_error.wait_queue,
12759 &wait_reset, TASK_UNINTERRUPTIBLE);
12760
12761
12762 if (i915_sw_fence_done(&intel_state->commit_ready)
12763 || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
12764 break;
12765
12766 schedule();
12767 }
12768 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
12769 finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
12770}
12771
8d52e447
CW
12772static void intel_atomic_cleanup_work(struct work_struct *work)
12773{
12774 struct drm_atomic_state *state =
12775 container_of(work, struct drm_atomic_state, commit_work);
12776 struct drm_i915_private *i915 = to_i915(state->dev);
12777
12778 drm_atomic_helper_cleanup_planes(&i915->drm, state);
12779 drm_atomic_helper_commit_cleanup_done(state);
12780 drm_atomic_state_put(state);
12781
12782 intel_atomic_helper_free_state(i915);
12783}
12784
94f05024 12785static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 12786{
94f05024 12787 struct drm_device *dev = state->dev;
565602d7 12788 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 12789 struct drm_i915_private *dev_priv = to_i915(dev);
aa5e9b47 12790 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7580d774 12791 struct drm_crtc *crtc;
5a21b665 12792 struct intel_crtc_state *intel_cstate;
d8fc70b7 12793 u64 put_domains[I915_MAX_PIPES] = {};
e95433c7 12794 int i;
a6778b3c 12795
9db529aa 12796 intel_atomic_commit_fence_wait(intel_state);
42b062b0 12797
ea0000f0
SV
12798 drm_atomic_helper_wait_for_dependencies(state);
12799
c3b32658 12800 if (intel_state->modeset)
5a21b665 12801 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 12802
aa5e9b47 12803 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a539205a
ML
12804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12805
aa5e9b47
ML
12806 if (needs_modeset(new_crtc_state) ||
12807 to_intel_crtc_state(new_crtc_state)->update_pipe) {
5a21b665
SV
12808
12809 put_domains[to_intel_crtc(crtc)->pipe] =
12810 modeset_get_crtc_power_domains(crtc,
aa5e9b47 12811 to_intel_crtc_state(new_crtc_state));
5a21b665
SV
12812 }
12813
aa5e9b47 12814 if (!needs_modeset(new_crtc_state))
61333b60
ML
12815 continue;
12816
aa5e9b47
ML
12817 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
12818 to_intel_crtc_state(new_crtc_state));
460da916 12819
29ceb0e6
VS
12820 if (old_crtc_state->active) {
12821 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
033b7a23
ML
12822
12823 /*
12824 * We need to disable pipe CRC before disabling the pipe,
12825 * or we race against vblank off.
12826 */
12827 intel_crtc_disable_pipe_crc(intel_crtc);
12828
4a806558 12829 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 12830 intel_crtc->active = false;
58f9c0bc 12831 intel_fbc_disable(intel_crtc);
eddfcbcd 12832 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
12833
12834 /*
12835 * Underruns don't always raise
12836 * interrupts, so check manually.
12837 */
12838 intel_check_cpu_fifo_underruns(dev_priv);
12839 intel_check_pch_fifo_underruns(dev_priv);
b9001114 12840
df5e31c2
VS
12841 /* FIXME unify this for all platforms */
12842 if (!new_crtc_state->active &&
12843 !HAS_GMCH_DISPLAY(dev_priv) &&
12844 dev_priv->display.initial_watermarks)
12845 dev_priv->display.initial_watermarks(intel_state,
12846 to_intel_crtc_state(new_crtc_state));
a539205a 12847 }
b8cecdf5 12848 }
7758a113 12849
7a1530d7
SV
12850 /* FIXME: Eventually get rid of our intel_crtc->config pointer */
12851 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
12852 to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
f6e5b160 12853
565602d7 12854 if (intel_state->modeset) {
4740b0f2 12855 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89 12856
b0587e4d 12857 intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
f6d1973d 12858
656d1b89
L
12859 /*
12860 * SKL workaround: bspec recommends we disable the SAGV when we
12861 * have more then one pipe enabled
12862 */
56feca91 12863 if (!intel_can_enable_sagv(state))
16dcdc4e 12864 intel_disable_sagv(dev_priv);
656d1b89 12865
677100ce 12866 intel_modeset_verify_disabled(dev, state);
4740b0f2 12867 }
47fab737 12868
896e5bb0 12869 /* Complete the events for pipes that have now been disabled */
aa5e9b47
ML
12870 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12871 bool modeset = needs_modeset(new_crtc_state);
80715b2f 12872
1f7528c4 12873 /* Complete events for now disable pipes here. */
aa5e9b47 12874 if (modeset && !new_crtc_state->active && new_crtc_state->event) {
1f7528c4 12875 spin_lock_irq(&dev->event_lock);
aa5e9b47 12876 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
1f7528c4
SV
12877 spin_unlock_irq(&dev->event_lock);
12878
aa5e9b47 12879 new_crtc_state->event = NULL;
1f7528c4 12880 }
177246a8
MR
12881 }
12882
896e5bb0 12883 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
b44d5c0c 12884 dev_priv->display.update_crtcs(state);
896e5bb0 12885
94f05024
SV
12886 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
12887 * already, but still need the state for the delayed optimization. To
12888 * fix this:
12889 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
12890 * - schedule that vblank worker _before_ calling hw_done
12891 * - at the start of commit_tail, cancel it _synchrously
12892 * - switch over to the vblank wait helper in the core after that since
12893 * we don't need out special handling any more.
12894 */
b44d5c0c 12895 drm_atomic_helper_wait_for_flip_done(dev, state);
5a21b665
SV
12896
12897 /*
12898 * Now that the vblank has passed, we can go ahead and program the
12899 * optimal watermarks on platforms that need two-step watermark
12900 * programming.
12901 *
12902 * TODO: Move this (and other cleanup) to an async worker eventually.
12903 */
aa5e9b47
ML
12904 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
12905 intel_cstate = to_intel_crtc_state(new_crtc_state);
5a21b665
SV
12906
12907 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
12908 dev_priv->display.optimize_watermarks(intel_state,
12909 intel_cstate);
5a21b665
SV
12910 }
12911
aa5e9b47 12912 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5a21b665
SV
12913 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
12914
12915 if (put_domains[i])
12916 modeset_put_power_domains(dev_priv, put_domains[i]);
12917
aa5e9b47 12918 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
5a21b665
SV
12919 }
12920
cff109f0
VS
12921 if (intel_state->modeset)
12922 intel_verify_planes(intel_state);
12923
56feca91 12924 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 12925 intel_enable_sagv(dev_priv);
656d1b89 12926
94f05024
SV
12927 drm_atomic_helper_commit_hw_done(state);
12928
d5553c09
CW
12929 if (intel_state->modeset) {
12930 /* As one of the primary mmio accessors, KMS has a high
12931 * likelihood of triggering bugs in unclaimed access. After we
12932 * finish modesetting, see if an error has been flagged, and if
12933 * so enable debugging for the next modeset - and hope we catch
12934 * the culprit.
12935 */
12936 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
5a21b665 12937 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
d5553c09 12938 }
5a21b665 12939
8d52e447
CW
12940 /*
12941 * Defer the cleanup of the old state to a separate worker to not
12942 * impede the current task (userspace for blocking modesets) that
12943 * are executed inline. For out-of-line asynchronous modesets/flips,
12944 * deferring to a new worker seems overkill, but we would place a
12945 * schedule point (cond_resched()) here anyway to keep latencies
12946 * down.
12947 */
12948 INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
41db645a 12949 queue_work(system_highpri_wq, &state->commit_work);
94f05024
SV
12950}
12951
12952static void intel_atomic_commit_work(struct work_struct *work)
12953{
c004a90b
CW
12954 struct drm_atomic_state *state =
12955 container_of(work, struct drm_atomic_state, commit_work);
12956
94f05024
SV
12957 intel_atomic_commit_tail(state);
12958}
12959
c004a90b
CW
12960static int __i915_sw_fence_call
12961intel_atomic_commit_ready(struct i915_sw_fence *fence,
12962 enum i915_sw_fence_notify notify)
12963{
12964 struct intel_atomic_state *state =
12965 container_of(fence, struct intel_atomic_state, commit_ready);
12966
12967 switch (notify) {
12968 case FENCE_COMPLETE:
42b062b0 12969 /* we do blocking waits in the worker, nothing to do here */
c004a90b 12970 break;
c004a90b 12971 case FENCE_FREE:
eb955eee
CW
12972 {
12973 struct intel_atomic_helper *helper =
12974 &to_i915(state->base.dev)->atomic_helper;
12975
12976 if (llist_add(&state->freed, &helper->free_list))
12977 schedule_work(&helper->free_work);
12978 break;
12979 }
c004a90b
CW
12980 }
12981
12982 return NOTIFY_DONE;
12983}
12984
6c9c1b38
SV
12985static void intel_atomic_track_fbs(struct drm_atomic_state *state)
12986{
aa5e9b47 12987 struct drm_plane_state *old_plane_state, *new_plane_state;
6c9c1b38 12988 struct drm_plane *plane;
6c9c1b38
SV
12989 int i;
12990
aa5e9b47 12991 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
faf5bf0a 12992 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
aa5e9b47 12993 intel_fb_obj(new_plane_state->fb),
faf5bf0a 12994 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
SV
12995}
12996
94f05024
SV
12997/**
12998 * intel_atomic_commit - commit validated state object
12999 * @dev: DRM device
13000 * @state: the top-level driver state object
13001 * @nonblock: nonblocking commit
13002 *
13003 * This function commits a top-level state object that has been validated
13004 * with drm_atomic_helper_check().
13005 *
94f05024
SV
13006 * RETURNS
13007 * Zero for success or -errno.
13008 */
13009static int intel_atomic_commit(struct drm_device *dev,
13010 struct drm_atomic_state *state,
13011 bool nonblock)
13012{
13013 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 13014 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
SV
13015 int ret = 0;
13016
c004a90b
CW
13017 drm_atomic_state_get(state);
13018 i915_sw_fence_init(&intel_state->commit_ready,
13019 intel_atomic_commit_ready);
94f05024 13020
440df938
VS
13021 /*
13022 * The intel_legacy_cursor_update() fast path takes care
13023 * of avoiding the vblank waits for simple cursor
13024 * movement and flips. For cursor on/off and size changes,
13025 * we want to perform the vblank waits so that watermark
13026 * updates happen during the correct frames. Gen9+ have
13027 * double buffered watermarks and so shouldn't need this.
13028 *
3cf50c63
ML
13029 * Unset state->legacy_cursor_update before the call to
13030 * drm_atomic_helper_setup_commit() because otherwise
13031 * drm_atomic_helper_wait_for_flip_done() is a noop and
13032 * we get FIFO underruns because we didn't wait
13033 * for vblank.
440df938
VS
13034 *
13035 * FIXME doing watermarks and fb cleanup from a vblank worker
13036 * (assuming we had any) would solve these problems.
13037 */
213f1bd0
ML
13038 if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
13039 struct intel_crtc_state *new_crtc_state;
13040 struct intel_crtc *crtc;
13041 int i;
13042
13043 for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
13044 if (new_crtc_state->wm.need_postvbl_update ||
13045 new_crtc_state->update_wm_post)
13046 state->legacy_cursor_update = false;
13047 }
440df938 13048
3cf50c63
ML
13049 ret = intel_atomic_prepare_commit(dev, state);
13050 if (ret) {
13051 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13052 i915_sw_fence_commit(&intel_state->commit_ready);
13053 return ret;
13054 }
13055
13056 ret = drm_atomic_helper_setup_commit(state, nonblock);
13057 if (!ret)
13058 ret = drm_atomic_helper_swap_state(state, true);
13059
0806f4ee
ML
13060 if (ret) {
13061 i915_sw_fence_commit(&intel_state->commit_ready);
13062
0806f4ee 13063 drm_atomic_helper_cleanup_planes(dev, state);
0806f4ee
ML
13064 return ret;
13065 }
94f05024 13066 dev_priv->wm.distrust_bios_wm = false;
3c0fb588 13067 intel_shared_dpll_swap_state(state);
6c9c1b38 13068 intel_atomic_track_fbs(state);
94f05024 13069
c3b32658 13070 if (intel_state->modeset) {
d305e061
VS
13071 memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
13072 sizeof(intel_state->min_cdclk));
53e9bf5e
VS
13073 memcpy(dev_priv->min_voltage_level,
13074 intel_state->min_voltage_level,
13075 sizeof(intel_state->min_voltage_level));
c3b32658 13076 dev_priv->active_crtcs = intel_state->active_crtcs;
bb0f4aab
VS
13077 dev_priv->cdclk.logical = intel_state->cdclk.logical;
13078 dev_priv->cdclk.actual = intel_state->cdclk.actual;
c3b32658
ML
13079 }
13080
0853695c 13081 drm_atomic_state_get(state);
42b062b0 13082 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
c004a90b
CW
13083
13084 i915_sw_fence_commit(&intel_state->commit_ready);
757fffcf
VS
13085 if (nonblock && intel_state->modeset) {
13086 queue_work(dev_priv->modeset_wq, &state->commit_work);
13087 } else if (nonblock) {
42b062b0 13088 queue_work(system_unbound_wq, &state->commit_work);
757fffcf
VS
13089 } else {
13090 if (intel_state->modeset)
13091 flush_workqueue(dev_priv->modeset_wq);
94f05024 13092 intel_atomic_commit_tail(state);
757fffcf 13093 }
75714940 13094
74c090b1 13095 return 0;
7f27126e
JB
13096}
13097
f6e5b160 13098static const struct drm_crtc_funcs intel_crtc_funcs = {
3fab2f09 13099 .gamma_set = drm_atomic_helper_legacy_gamma_set,
74c090b1 13100 .set_config = drm_atomic_helper_set_config,
f6e5b160 13101 .destroy = intel_crtc_destroy,
4c01ded5 13102 .page_flip = drm_atomic_helper_page_flip,
1356837e
MR
13103 .atomic_duplicate_state = intel_crtc_duplicate_state,
13104 .atomic_destroy_state = intel_crtc_destroy_state,
8c6b709d 13105 .set_crc_source = intel_crtc_set_crc_source,
a8c20833 13106 .verify_crc_source = intel_crtc_verify_crc_source,
260bc551 13107 .get_crc_sources = intel_crtc_get_crc_sources,
f6e5b160
CW
13108};
13109
74d290f8
CW
13110struct wait_rps_boost {
13111 struct wait_queue_entry wait;
13112
13113 struct drm_crtc *crtc;
e61e0f51 13114 struct i915_request *request;
74d290f8
CW
13115};
13116
13117static int do_rps_boost(struct wait_queue_entry *_wait,
13118 unsigned mode, int sync, void *key)
13119{
13120 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
e61e0f51 13121 struct i915_request *rq = wait->request;
74d290f8 13122
e9af4ea2
CW
13123 /*
13124 * If we missed the vblank, but the request is already running it
13125 * is reasonable to assume that it will complete before the next
13126 * vblank without our intervention, so leave RPS alone.
13127 */
e61e0f51 13128 if (!i915_request_started(rq))
e9af4ea2 13129 gen6_rps_boost(rq, NULL);
e61e0f51 13130 i915_request_put(rq);
74d290f8
CW
13131
13132 drm_crtc_vblank_put(wait->crtc);
13133
13134 list_del(&wait->wait.entry);
13135 kfree(wait);
13136 return 1;
13137}
13138
13139static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
13140 struct dma_fence *fence)
13141{
13142 struct wait_rps_boost *wait;
13143
13144 if (!dma_fence_is_i915(fence))
13145 return;
13146
13147 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
13148 return;
13149
13150 if (drm_crtc_vblank_get(crtc))
13151 return;
13152
13153 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
13154 if (!wait) {
13155 drm_crtc_vblank_put(crtc);
13156 return;
13157 }
13158
13159 wait->request = to_request(dma_fence_get(fence));
13160 wait->crtc = crtc;
13161
13162 wait->wait.func = do_rps_boost;
13163 wait->wait.flags = 0;
13164
13165 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
13166}
13167
ef1a1914
VS
13168static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
13169{
13170 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
13171 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
13172 struct drm_framebuffer *fb = plane_state->base.fb;
13173 struct i915_vma *vma;
13174
13175 if (plane->id == PLANE_CURSOR &&
13176 INTEL_INFO(dev_priv)->cursor_needs_physical) {
13177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13178 const int align = intel_cursor_alignment(dev_priv);
4a477651 13179 int err;
ef1a1914 13180
4a477651
CW
13181 err = i915_gem_object_attach_phys(obj, align);
13182 if (err)
13183 return err;
ef1a1914
VS
13184 }
13185
13186 vma = intel_pin_and_fence_fb_obj(fb,
f5929c53 13187 &plane_state->view,
ef1a1914
VS
13188 intel_plane_uses_fence(plane_state),
13189 &plane_state->flags);
13190 if (IS_ERR(vma))
13191 return PTR_ERR(vma);
13192
13193 plane_state->vma = vma;
13194
13195 return 0;
13196}
13197
13198static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
13199{
13200 struct i915_vma *vma;
13201
13202 vma = fetch_and_zero(&old_plane_state->vma);
13203 if (vma)
13204 intel_unpin_fb_vma(vma, old_plane_state->flags);
13205}
13206
b7268c5e
CW
13207static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
13208{
13209 struct i915_sched_attr attr = {
13210 .priority = I915_PRIORITY_DISPLAY,
13211 };
13212
13213 i915_gem_object_wait_priority(obj, 0, &attr);
13214}
13215
6beb8c23
MR
13216/**
13217 * intel_prepare_plane_fb - Prepare fb for usage on plane
13218 * @plane: drm plane to prepare for
c38c1455 13219 * @new_state: the plane state being prepared
6beb8c23
MR
13220 *
13221 * Prepares a framebuffer for usage on a display plane. Generally this
13222 * involves pinning the underlying object and updating the frontbuffer tracking
13223 * bits. Some older platforms need special physical address handling for
13224 * cursor planes.
13225 *
f935675f
ML
13226 * Must be called with struct_mutex held.
13227 *
6beb8c23
MR
13228 * Returns 0 on success, negative error code on failure.
13229 */
13230int
13231intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 13232 struct drm_plane_state *new_state)
465c120c 13233{
c004a90b
CW
13234 struct intel_atomic_state *intel_state =
13235 to_intel_atomic_state(new_state->state);
b7f05d4a 13236 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 13237 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13238 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13239 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 13240 int ret;
465c120c 13241
5008e874
ML
13242 if (old_obj) {
13243 struct drm_crtc_state *crtc_state =
8b69449d
ML
13244 drm_atomic_get_new_crtc_state(new_state->state,
13245 plane->state->crtc);
5008e874
ML
13246
13247 /* Big Hammer, we also need to ensure that any pending
13248 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13249 * current scanout is retired before unpinning the old
13250 * framebuffer. Note that we rely on userspace rendering
13251 * into the buffer attached to the pipe they are waiting
13252 * on. If not, userspace generates a GPU hang with IPEHR
13253 * point to the MI_WAIT_FOR_EVENT.
13254 *
13255 * This should only fail upon a hung GPU, in which case we
13256 * can safely continue.
13257 */
c004a90b
CW
13258 if (needs_modeset(crtc_state)) {
13259 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13260 old_obj->resv, NULL,
13261 false, 0,
13262 GFP_KERNEL);
13263 if (ret < 0)
13264 return ret;
f4457ae7 13265 }
5008e874
ML
13266 }
13267
c004a90b
CW
13268 if (new_state->fence) { /* explicit fencing */
13269 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
13270 new_state->fence,
13271 I915_FENCE_TIMEOUT,
13272 GFP_KERNEL);
13273 if (ret < 0)
13274 return ret;
13275 }
13276
c37efb99
CW
13277 if (!obj)
13278 return 0;
13279
4d3088c7 13280 ret = i915_gem_object_pin_pages(obj);
fd70075f
CW
13281 if (ret)
13282 return ret;
13283
4d3088c7
CW
13284 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13285 if (ret) {
13286 i915_gem_object_unpin_pages(obj);
13287 return ret;
13288 }
13289
ef1a1914 13290 ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
fd70075f 13291
b7268c5e 13292 fb_obj_bump_render_priority(obj);
fd70075f
CW
13293
13294 mutex_unlock(&dev_priv->drm.struct_mutex);
4d3088c7 13295 i915_gem_object_unpin_pages(obj);
fd70075f
CW
13296 if (ret)
13297 return ret;
13298
07bcd99b
DP
13299 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
13300
c004a90b 13301 if (!new_state->fence) { /* implicit fencing */
74d290f8
CW
13302 struct dma_fence *fence;
13303
c004a90b
CW
13304 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
13305 obj->resv, NULL,
13306 false, I915_FENCE_TIMEOUT,
13307 GFP_KERNEL);
13308 if (ret < 0)
13309 return ret;
74d290f8
CW
13310
13311 fence = reservation_object_get_excl_rcu(obj->resv);
13312 if (fence) {
13313 add_rps_boost_after_vblank(new_state->crtc, fence);
13314 dma_fence_put(fence);
13315 }
13316 } else {
13317 add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
c004a90b 13318 }
5a21b665 13319
027063b1
CW
13320 /*
13321 * We declare pageflips to be interactive and so merit a small bias
13322 * towards upclocking to deliver the frame on time. By only changing
13323 * the RPS thresholds to sample more regularly and aim for higher
13324 * clocks we can hopefully deliver low power workloads (like kodi)
13325 * that are not quite steady state without resorting to forcing
13326 * maximum clocks following a vblank miss (see do_rps_boost()).
13327 */
13328 if (!intel_state->rps_interactive) {
13329 intel_rps_mark_interactive(dev_priv, true);
13330 intel_state->rps_interactive = true;
13331 }
13332
d07f0e59 13333 return 0;
6beb8c23
MR
13334}
13335
38f3ce3a
MR
13336/**
13337 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13338 * @plane: drm plane to clean up for
c38c1455 13339 * @old_state: the state from the previous modeset
38f3ce3a
MR
13340 *
13341 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13342 *
13343 * Must be called with struct_mutex held.
38f3ce3a
MR
13344 */
13345void
13346intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 13347 struct drm_plane_state *old_state)
38f3ce3a 13348{
027063b1
CW
13349 struct intel_atomic_state *intel_state =
13350 to_intel_atomic_state(old_state->state);
ef1a1914 13351 struct drm_i915_private *dev_priv = to_i915(plane->dev);
38f3ce3a 13352
027063b1
CW
13353 if (intel_state->rps_interactive) {
13354 intel_rps_mark_interactive(dev_priv, false);
13355 intel_state->rps_interactive = false;
13356 }
13357
be1e3415 13358 /* Should only be called after a successful intel_prepare_plane_fb()! */
ef1a1914
VS
13359 mutex_lock(&dev_priv->drm.struct_mutex);
13360 intel_plane_unpin_fb(to_intel_plane_state(old_state));
13361 mutex_unlock(&dev_priv->drm.struct_mutex);
465c120c
MR
13362}
13363
6156a456 13364int
4e0b83a5
VS
13365skl_max_scale(const struct intel_crtc_state *crtc_state,
13366 u32 pixel_format)
6156a456 13367{
4e0b83a5
VS
13368 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
13369 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
77224cd5
CK
13370 int max_scale, mult;
13371 int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
6156a456 13372
4e0b83a5 13373 if (!crtc_state->base.enable)
6156a456
CK
13374 return DRM_PLANE_HELPER_NO_SCALING;
13375
6156a456 13376 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
5b7280f0
ACO
13377 max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
13378
43037c86 13379 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
5b7280f0 13380 max_dotclk *= 2;
6156a456 13381
5b7280f0 13382 if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
6156a456
CK
13383 return DRM_PLANE_HELPER_NO_SCALING;
13384
13385 /*
13386 * skl max scale is lower of:
13387 * close to 3 but not 3, -1 is for that purpose
13388 * or
13389 * cdclk/crtc_clock
13390 */
77224cd5
CK
13391 mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
13392 tmpclk1 = (1 << 16) * mult - 1;
13393 tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
13394 max_scale = min(tmpclk1, tmpclk2);
6156a456
CK
13395
13396 return max_scale;
13397}
13398
5a21b665
SV
13399static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13400 struct drm_crtc_state *old_crtc_state)
13401{
13402 struct drm_device *dev = crtc->dev;
62e0fb88 13403 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 13404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ccf010fb 13405 struct intel_crtc_state *old_intel_cstate =
5a21b665 13406 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
13407 struct intel_atomic_state *old_intel_state =
13408 to_intel_atomic_state(old_crtc_state->state);
d3a8fb32
VS
13409 struct intel_crtc_state *intel_cstate =
13410 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
13411 bool modeset = needs_modeset(&intel_cstate->base);
5a21b665 13412
567f0792
ML
13413 if (!modeset &&
13414 (intel_cstate->base.color_mgmt_changed ||
13415 intel_cstate->update_pipe)) {
5c857e60
VS
13416 intel_color_set_csc(&intel_cstate->base);
13417 intel_color_load_luts(&intel_cstate->base);
567f0792
ML
13418 }
13419
5a21b665 13420 /* Perform vblank evasion around commit operation */
d3a8fb32 13421 intel_pipe_update_start(intel_cstate);
5a21b665
SV
13422
13423 if (modeset)
e62929b3 13424 goto out;
5a21b665 13425
ccf010fb 13426 if (intel_cstate->update_pipe)
1a15b77b 13427 intel_update_pipe_config(old_intel_cstate, intel_cstate);
ccf010fb 13428 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 13429 skl_detach_scalers(intel_crtc);
62e0fb88 13430
e62929b3 13431out:
ccf010fb
ML
13432 if (dev_priv->display.atomic_update_watermarks)
13433 dev_priv->display.atomic_update_watermarks(old_intel_state,
13434 intel_cstate);
5a21b665
SV
13435}
13436
d52ad9cb
ML
13437void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
13438 struct intel_crtc_state *crtc_state)
13439{
13440 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13441
13442 if (!IS_GEN2(dev_priv))
13443 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
13444
13445 if (crtc_state->has_pch_encoder) {
13446 enum pipe pch_transcoder =
13447 intel_crtc_pch_transcoder(crtc);
13448
13449 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
13450 }
13451}
13452
5a21b665
SV
13453static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13454 struct drm_crtc_state *old_crtc_state)
13455{
13456 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d3a8fb32
VS
13457 struct intel_atomic_state *old_intel_state =
13458 to_intel_atomic_state(old_crtc_state->state);
13459 struct intel_crtc_state *new_crtc_state =
13460 intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
5a21b665 13461
d3a8fb32 13462 intel_pipe_update_end(new_crtc_state);
33a49868
ML
13463
13464 if (new_crtc_state->update_pipe &&
13465 !needs_modeset(&new_crtc_state->base) &&
d52ad9cb
ML
13466 old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
13467 intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
5a21b665
SV
13468}
13469
cf4c7c12 13470/**
4a3b8769
MR
13471 * intel_plane_destroy - destroy a plane
13472 * @plane: plane to destroy
cf4c7c12 13473 *
4a3b8769
MR
13474 * Common destruction function for all types of planes (primary, cursor,
13475 * sprite).
cf4c7c12 13476 */
4a3b8769 13477void intel_plane_destroy(struct drm_plane *plane)
465c120c 13478{
465c120c 13479 drm_plane_cleanup(plane);
69ae561f 13480 kfree(to_intel_plane(plane));
465c120c
MR
13481}
13482
a38189c5
VS
13483static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
13484 u32 format, u64 modifier)
714244e2 13485{
a38189c5
VS
13486 switch (modifier) {
13487 case DRM_FORMAT_MOD_LINEAR:
13488 case I915_FORMAT_MOD_X_TILED:
13489 break;
13490 default:
13491 return false;
13492 }
13493
714244e2
BW
13494 switch (format) {
13495 case DRM_FORMAT_C8:
13496 case DRM_FORMAT_RGB565:
13497 case DRM_FORMAT_XRGB1555:
13498 case DRM_FORMAT_XRGB8888:
13499 return modifier == DRM_FORMAT_MOD_LINEAR ||
13500 modifier == I915_FORMAT_MOD_X_TILED;
13501 default:
13502 return false;
13503 }
13504}
13505
a38189c5
VS
13506static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
13507 u32 format, u64 modifier)
714244e2 13508{
a38189c5
VS
13509 switch (modifier) {
13510 case DRM_FORMAT_MOD_LINEAR:
13511 case I915_FORMAT_MOD_X_TILED:
13512 break;
13513 default:
13514 return false;
13515 }
13516
714244e2
BW
13517 switch (format) {
13518 case DRM_FORMAT_C8:
13519 case DRM_FORMAT_RGB565:
13520 case DRM_FORMAT_XRGB8888:
13521 case DRM_FORMAT_XBGR8888:
13522 case DRM_FORMAT_XRGB2101010:
13523 case DRM_FORMAT_XBGR2101010:
13524 return modifier == DRM_FORMAT_MOD_LINEAR ||
13525 modifier == I915_FORMAT_MOD_X_TILED;
13526 default:
13527 return false;
13528 }
13529}
13530
a38189c5
VS
13531static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
13532 u32 format, u64 modifier)
714244e2 13533{
a38189c5
VS
13534 struct intel_plane *plane = to_intel_plane(_plane);
13535
13536 switch (modifier) {
13537 case DRM_FORMAT_MOD_LINEAR:
13538 case I915_FORMAT_MOD_X_TILED:
13539 case I915_FORMAT_MOD_Y_TILED:
13540 case I915_FORMAT_MOD_Yf_TILED:
13541 break;
13542 case I915_FORMAT_MOD_Y_TILED_CCS:
13543 case I915_FORMAT_MOD_Yf_TILED_CCS:
13544 if (!plane->has_ccs)
13545 return false;
13546 break;
13547 default:
13548 return false;
13549 }
13550
714244e2
BW
13551 switch (format) {
13552 case DRM_FORMAT_XRGB8888:
13553 case DRM_FORMAT_XBGR8888:
13554 case DRM_FORMAT_ARGB8888:
13555 case DRM_FORMAT_ABGR8888:
63eaf9ac 13556 if (is_ccs_modifier(modifier))
714244e2
BW
13557 return true;
13558 /* fall through */
13559 case DRM_FORMAT_RGB565:
13560 case DRM_FORMAT_XRGB2101010:
13561 case DRM_FORMAT_XBGR2101010:
13562 case DRM_FORMAT_YUYV:
13563 case DRM_FORMAT_YVYU:
13564 case DRM_FORMAT_UYVY:
13565 case DRM_FORMAT_VYUY:
c0b56ab5 13566 case DRM_FORMAT_NV12:
714244e2
BW
13567 if (modifier == I915_FORMAT_MOD_Yf_TILED)
13568 return true;
13569 /* fall through */
13570 case DRM_FORMAT_C8:
13571 if (modifier == DRM_FORMAT_MOD_LINEAR ||
13572 modifier == I915_FORMAT_MOD_X_TILED ||
13573 modifier == I915_FORMAT_MOD_Y_TILED)
13574 return true;
13575 /* fall through */
13576 default:
13577 return false;
13578 }
13579}
13580
a38189c5
VS
13581static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
13582 u32 format, u64 modifier)
714244e2 13583{
a38189c5
VS
13584 return modifier == DRM_FORMAT_MOD_LINEAR &&
13585 format == DRM_FORMAT_ARGB8888;
714244e2
BW
13586}
13587
a38189c5
VS
13588static struct drm_plane_funcs skl_plane_funcs = {
13589 .update_plane = drm_atomic_helper_update_plane,
13590 .disable_plane = drm_atomic_helper_disable_plane,
13591 .destroy = intel_plane_destroy,
13592 .atomic_get_property = intel_plane_atomic_get_property,
13593 .atomic_set_property = intel_plane_atomic_set_property,
13594 .atomic_duplicate_state = intel_plane_duplicate_state,
13595 .atomic_destroy_state = intel_plane_destroy_state,
13596 .format_mod_supported = skl_plane_format_mod_supported,
13597};
714244e2 13598
a38189c5
VS
13599static struct drm_plane_funcs i965_plane_funcs = {
13600 .update_plane = drm_atomic_helper_update_plane,
13601 .disable_plane = drm_atomic_helper_disable_plane,
13602 .destroy = intel_plane_destroy,
13603 .atomic_get_property = intel_plane_atomic_get_property,
13604 .atomic_set_property = intel_plane_atomic_set_property,
13605 .atomic_duplicate_state = intel_plane_duplicate_state,
13606 .atomic_destroy_state = intel_plane_destroy_state,
13607 .format_mod_supported = i965_plane_format_mod_supported,
13608};
714244e2 13609
a38189c5 13610static struct drm_plane_funcs i8xx_plane_funcs = {
70a101f8
MR
13611 .update_plane = drm_atomic_helper_update_plane,
13612 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13613 .destroy = intel_plane_destroy,
a98b3431
MR
13614 .atomic_get_property = intel_plane_atomic_get_property,
13615 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13616 .atomic_duplicate_state = intel_plane_duplicate_state,
13617 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 13618 .format_mod_supported = i8xx_plane_format_mod_supported,
465c120c
MR
13619};
13620
f79f2692
ML
13621static int
13622intel_legacy_cursor_update(struct drm_plane *plane,
13623 struct drm_crtc *crtc,
13624 struct drm_framebuffer *fb,
13625 int crtc_x, int crtc_y,
13626 unsigned int crtc_w, unsigned int crtc_h,
13627 uint32_t src_x, uint32_t src_y,
34a2ab5e
SV
13628 uint32_t src_w, uint32_t src_h,
13629 struct drm_modeset_acquire_ctx *ctx)
f79f2692
ML
13630{
13631 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
13632 int ret;
13633 struct drm_plane_state *old_plane_state, *new_plane_state;
13634 struct intel_plane *intel_plane = to_intel_plane(plane);
13635 struct drm_framebuffer *old_fb;
13636 struct drm_crtc_state *crtc_state = crtc->state;
13637
13638 /*
13639 * When crtc is inactive or there is a modeset pending,
13640 * wait for it to complete in the slowpath
13641 */
13642 if (!crtc_state->active || needs_modeset(crtc_state) ||
13643 to_intel_crtc_state(crtc_state)->update_pipe)
13644 goto slow;
13645
13646 old_plane_state = plane->state;
669c9215
ML
13647 /*
13648 * Don't do an async update if there is an outstanding commit modifying
13649 * the plane. This prevents our async update's changes from getting
13650 * overridden by a previous synchronous update's state.
13651 */
13652 if (old_plane_state->commit &&
13653 !try_wait_for_completion(&old_plane_state->commit->hw_done))
13654 goto slow;
f79f2692
ML
13655
13656 /*
13657 * If any parameters change that may affect watermarks,
13658 * take the slowpath. Only changing fb or position should be
13659 * in the fastpath.
13660 */
13661 if (old_plane_state->crtc != crtc ||
13662 old_plane_state->src_w != src_w ||
13663 old_plane_state->src_h != src_h ||
13664 old_plane_state->crtc_w != crtc_w ||
13665 old_plane_state->crtc_h != crtc_h ||
a5509abd 13666 !old_plane_state->fb != !fb)
f79f2692
ML
13667 goto slow;
13668
13669 new_plane_state = intel_plane_duplicate_state(plane);
13670 if (!new_plane_state)
13671 return -ENOMEM;
13672
13673 drm_atomic_set_fb_for_plane(new_plane_state, fb);
13674
13675 new_plane_state->src_x = src_x;
13676 new_plane_state->src_y = src_y;
13677 new_plane_state->src_w = src_w;
13678 new_plane_state->src_h = src_h;
13679 new_plane_state->crtc_x = crtc_x;
13680 new_plane_state->crtc_y = crtc_y;
13681 new_plane_state->crtc_w = crtc_w;
13682 new_plane_state->crtc_h = crtc_h;
13683
13684 ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
b2b55502
VS
13685 to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
13686 to_intel_plane_state(plane->state),
f79f2692
ML
13687 to_intel_plane_state(new_plane_state));
13688 if (ret)
13689 goto out_free;
13690
f79f2692
ML
13691 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
13692 if (ret)
13693 goto out_free;
13694
ef1a1914
VS
13695 ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
13696 if (ret)
13697 goto out_unlock;
f79f2692 13698
a694e226 13699 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
f79f2692 13700
07bcd99b 13701 old_fb = old_plane_state->fb;
f79f2692
ML
13702 i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
13703 intel_plane->frontbuffer_bit);
13704
13705 /* Swap plane state */
669c9215 13706 plane->state = new_plane_state;
f79f2692 13707
72259536
VS
13708 if (plane->state->visible) {
13709 trace_intel_update_plane(plane, to_intel_crtc(crtc));
282dbf9b 13710 intel_plane->update_plane(intel_plane,
a5509abd
VS
13711 to_intel_crtc_state(crtc->state),
13712 to_intel_plane_state(plane->state));
72259536
VS
13713 } else {
13714 trace_intel_disable_plane(plane, to_intel_crtc(crtc));
282dbf9b 13715 intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
72259536 13716 }
f79f2692 13717
ef1a1914 13718 intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
f79f2692
ML
13719
13720out_unlock:
13721 mutex_unlock(&dev_priv->drm.struct_mutex);
13722out_free:
669c9215
ML
13723 if (ret)
13724 intel_plane_destroy_state(plane, new_plane_state);
13725 else
13726 intel_plane_destroy_state(plane, old_plane_state);
f79f2692
ML
13727 return ret;
13728
f79f2692
ML
13729slow:
13730 return drm_atomic_helper_update_plane(plane, crtc, fb,
13731 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 13732 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
13733}
13734
13735static const struct drm_plane_funcs intel_cursor_plane_funcs = {
13736 .update_plane = intel_legacy_cursor_update,
13737 .disable_plane = drm_atomic_helper_disable_plane,
13738 .destroy = intel_plane_destroy,
f79f2692
ML
13739 .atomic_get_property = intel_plane_atomic_get_property,
13740 .atomic_set_property = intel_plane_atomic_set_property,
13741 .atomic_duplicate_state = intel_plane_duplicate_state,
13742 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 13743 .format_mod_supported = intel_cursor_format_mod_supported,
f79f2692
ML
13744};
13745
cf1805e6
VS
13746static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
13747 enum i9xx_plane_id i9xx_plane)
13748{
13749 if (!HAS_FBC(dev_priv))
13750 return false;
13751
13752 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
13753 return i9xx_plane == PLANE_A; /* tied to pipe A */
13754 else if (IS_IVYBRIDGE(dev_priv))
13755 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
13756 i9xx_plane == PLANE_C;
13757 else if (INTEL_GEN(dev_priv) >= 4)
13758 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
13759 else
13760 return i9xx_plane == PLANE_A;
13761}
13762
13763static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
13764 enum pipe pipe, enum plane_id plane_id)
13765{
13766 if (!HAS_FBC(dev_priv))
13767 return false;
13768
13769 return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
13770}
13771
c0b56ab5
CK
13772bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
13773 enum pipe pipe, enum plane_id plane_id)
13774{
b45649fb
DP
13775 /*
13776 * FIXME: ICL requires two hardware planes for scanning out NV12
13777 * framebuffers. Do not advertize support until this is implemented.
13778 */
13779 if (INTEL_GEN(dev_priv) >= 11)
13780 return false;
13781
18563409
DP
13782 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
13783 return false;
13784
13785 if (INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv) && pipe == PIPE_C)
13786 return false;
13787
13788 if (plane_id != PLANE_PRIMARY && plane_id != PLANE_SPRITE0)
13789 return false;
13790
c0b56ab5
CK
13791 return true;
13792}
13793
b079bd17 13794static struct intel_plane *
580503c7 13795intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 13796{
fca0ce2a
VS
13797 struct intel_plane *primary = NULL;
13798 struct intel_plane_state *state = NULL;
a38189c5 13799 const struct drm_plane_funcs *plane_funcs;
465c120c 13800 const uint32_t *intel_primary_formats;
93ca7e00 13801 unsigned int supported_rotations;
45e3743a 13802 unsigned int num_formats;
714244e2 13803 const uint64_t *modifiers;
fca0ce2a 13804 int ret;
465c120c
MR
13805
13806 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
13807 if (!primary) {
13808 ret = -ENOMEM;
fca0ce2a 13809 goto fail;
b079bd17 13810 }
465c120c 13811
8e7d688b 13812 state = intel_create_plane_state(&primary->base);
b079bd17
VS
13813 if (!state) {
13814 ret = -ENOMEM;
fca0ce2a 13815 goto fail;
b079bd17
VS
13816 }
13817
8e7d688b 13818 primary->base.state = &state->base;
ea2c67bb 13819
fd6e3c6c 13820 if (INTEL_GEN(dev_priv) >= 9)
af99ceda 13821 state->scaler_id = -1;
465c120c 13822 primary->pipe = pipe;
e3c566df
VS
13823 /*
13824 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
13825 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
13826 */
13827 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
ed15030d 13828 primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
e3c566df 13829 else
ed15030d 13830 primary->i9xx_plane = (enum i9xx_plane_id) pipe;
b14e5848 13831 primary->id = PLANE_PRIMARY;
c19e1124 13832 primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
cf1805e6
VS
13833
13834 if (INTEL_GEN(dev_priv) >= 9)
13835 primary->has_fbc = skl_plane_has_fbc(dev_priv,
13836 primary->pipe,
13837 primary->id);
13838 else
13839 primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
13840 primary->i9xx_plane);
13841
13842 if (primary->has_fbc) {
13843 struct intel_fbc *fbc = &dev_priv->fbc;
13844
13845 fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
13846 }
13847
77064e2e 13848 if (INTEL_GEN(dev_priv) >= 9) {
a38189c5
VS
13849 primary->has_ccs = skl_plane_has_ccs(dev_priv, pipe,
13850 PLANE_PRIMARY);
13851
c0b56ab5
CK
13852 if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
13853 intel_primary_formats = skl_pri_planar_formats;
13854 num_formats = ARRAY_SIZE(skl_pri_planar_formats);
13855 } else {
13856 intel_primary_formats = skl_primary_formats;
13857 num_formats = ARRAY_SIZE(skl_primary_formats);
13858 }
714244e2 13859
a38189c5 13860 if (primary->has_ccs)
714244e2
BW
13861 modifiers = skl_format_modifiers_ccs;
13862 else
13863 modifiers = skl_format_modifiers_noccs;
a8d201af 13864
ddd5713d 13865 primary->max_stride = skl_plane_max_stride;
9a8cc576 13866 primary->update_plane = skl_update_plane;
779d4d8f 13867 primary->disable_plane = skl_disable_plane;
51f5a096 13868 primary->get_hw_state = skl_plane_get_hw_state;
4e0b83a5 13869 primary->check_plane = skl_plane_check;
a38189c5
VS
13870
13871 plane_funcs = &skl_plane_funcs;
580503c7 13872 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
13873 intel_primary_formats = i965_primary_formats;
13874 num_formats = ARRAY_SIZE(i965_primary_formats);
714244e2 13875 modifiers = i9xx_format_modifiers;
a8d201af 13876
ddd5713d 13877 primary->max_stride = i9xx_plane_max_stride;
ed15030d
VS
13878 primary->update_plane = i9xx_update_plane;
13879 primary->disable_plane = i9xx_disable_plane;
51f5a096 13880 primary->get_hw_state = i9xx_plane_get_hw_state;
4e0b83a5 13881 primary->check_plane = i9xx_plane_check;
a38189c5
VS
13882
13883 plane_funcs = &i965_plane_funcs;
6c0fd451
DL
13884 } else {
13885 intel_primary_formats = i8xx_primary_formats;
13886 num_formats = ARRAY_SIZE(i8xx_primary_formats);
714244e2 13887 modifiers = i9xx_format_modifiers;
a8d201af 13888
ddd5713d 13889 primary->max_stride = i9xx_plane_max_stride;
ed15030d
VS
13890 primary->update_plane = i9xx_update_plane;
13891 primary->disable_plane = i9xx_disable_plane;
51f5a096 13892 primary->get_hw_state = i9xx_plane_get_hw_state;
4e0b83a5 13893 primary->check_plane = i9xx_plane_check;
a38189c5
VS
13894
13895 plane_funcs = &i8xx_plane_funcs;
465c120c
MR
13896 }
13897
580503c7
VS
13898 if (INTEL_GEN(dev_priv) >= 9)
13899 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
a38189c5 13900 0, plane_funcs,
38573dc1 13901 intel_primary_formats, num_formats,
714244e2 13902 modifiers,
38573dc1
VS
13903 DRM_PLANE_TYPE_PRIMARY,
13904 "plane 1%c", pipe_name(pipe));
9beb5fea 13905 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7 13906 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
a38189c5 13907 0, plane_funcs,
38573dc1 13908 intel_primary_formats, num_formats,
714244e2 13909 modifiers,
38573dc1
VS
13910 DRM_PLANE_TYPE_PRIMARY,
13911 "primary %c", pipe_name(pipe));
13912 else
580503c7 13913 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
a38189c5 13914 0, plane_funcs,
38573dc1 13915 intel_primary_formats, num_formats,
714244e2 13916 modifiers,
38573dc1 13917 DRM_PLANE_TYPE_PRIMARY,
ed15030d
VS
13918 "plane %c",
13919 plane_name(primary->i9xx_plane));
fca0ce2a
VS
13920 if (ret)
13921 goto fail;
48404c1e 13922
5f8e3f57
JL
13923 if (INTEL_GEN(dev_priv) >= 10) {
13924 supported_rotations =
13925 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13926 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
13927 DRM_MODE_REFLECT_X;
13928 } else if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 13929 supported_rotations =
c2c446ad
RF
13930 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
13931 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
13932 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
13933 supported_rotations =
c2c446ad
RF
13934 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
13935 DRM_MODE_REFLECT_X;
5481e27f 13936 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 13937 supported_rotations =
c2c446ad 13938 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 13939 } else {
c2c446ad 13940 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
13941 }
13942
5481e27f 13943 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 13944 drm_plane_create_rotation_property(&primary->base,
c2c446ad 13945 DRM_MODE_ROTATE_0,
93ca7e00 13946 supported_rotations);
48404c1e 13947
b0f5c0ba
VS
13948 if (INTEL_GEN(dev_priv) >= 9)
13949 drm_plane_create_color_properties(&primary->base,
13950 BIT(DRM_COLOR_YCBCR_BT601) |
13951 BIT(DRM_COLOR_YCBCR_BT709),
c8624ede
VS
13952 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
13953 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
23b28089 13954 DRM_COLOR_YCBCR_BT709,
b0f5c0ba
VS
13955 DRM_COLOR_YCBCR_LIMITED_RANGE);
13956
ea2c67bb
MR
13957 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13958
b079bd17 13959 return primary;
fca0ce2a
VS
13960
13961fail:
13962 kfree(state);
13963 kfree(primary);
13964
b079bd17 13965 return ERR_PTR(ret);
465c120c
MR
13966}
13967
b079bd17 13968static struct intel_plane *
b2d03b0d
VS
13969intel_cursor_plane_create(struct drm_i915_private *dev_priv,
13970 enum pipe pipe)
3d7d6510 13971{
fca0ce2a
VS
13972 struct intel_plane *cursor = NULL;
13973 struct intel_plane_state *state = NULL;
13974 int ret;
3d7d6510
MR
13975
13976 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
13977 if (!cursor) {
13978 ret = -ENOMEM;
fca0ce2a 13979 goto fail;
b079bd17 13980 }
3d7d6510 13981
8e7d688b 13982 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
13983 if (!state) {
13984 ret = -ENOMEM;
fca0ce2a 13985 goto fail;
b079bd17
VS
13986 }
13987
8e7d688b 13988 cursor->base.state = &state->base;
ea2c67bb 13989
3d7d6510 13990 cursor->pipe = pipe;
ed15030d 13991 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
b14e5848 13992 cursor->id = PLANE_CURSOR;
c19e1124 13993 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
b2d03b0d
VS
13994
13995 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
ddd5713d 13996 cursor->max_stride = i845_cursor_max_stride;
b2d03b0d
VS
13997 cursor->update_plane = i845_update_cursor;
13998 cursor->disable_plane = i845_disable_cursor;
51f5a096 13999 cursor->get_hw_state = i845_cursor_get_hw_state;
659056f2 14000 cursor->check_plane = i845_check_cursor;
b2d03b0d 14001 } else {
ddd5713d 14002 cursor->max_stride = i9xx_cursor_max_stride;
b2d03b0d
VS
14003 cursor->update_plane = i9xx_update_cursor;
14004 cursor->disable_plane = i9xx_disable_cursor;
51f5a096 14005 cursor->get_hw_state = i9xx_cursor_get_hw_state;
659056f2 14006 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 14007 }
3d7d6510 14008
cd5dcbf1
VS
14009 cursor->cursor.base = ~0;
14010 cursor->cursor.cntl = ~0;
024faac7
VS
14011
14012 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
14013 cursor->cursor.size = ~0;
3d7d6510 14014
580503c7 14015 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
f79f2692 14016 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
14017 intel_cursor_formats,
14018 ARRAY_SIZE(intel_cursor_formats),
714244e2
BW
14019 cursor_format_modifiers,
14020 DRM_PLANE_TYPE_CURSOR,
38573dc1 14021 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
14022 if (ret)
14023 goto fail;
4398ad45 14024
5481e27f 14025 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 14026 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
14027 DRM_MODE_ROTATE_0,
14028 DRM_MODE_ROTATE_0 |
14029 DRM_MODE_ROTATE_180);
4398ad45 14030
580503c7 14031 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
14032 state->scaler_id = -1;
14033
ea2c67bb
MR
14034 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14035
b079bd17 14036 return cursor;
fca0ce2a
VS
14037
14038fail:
14039 kfree(state);
14040 kfree(cursor);
14041
b079bd17 14042 return ERR_PTR(ret);
3d7d6510
MR
14043}
14044
1c74eeaf
NM
14045static void intel_crtc_init_scalers(struct intel_crtc *crtc,
14046 struct intel_crtc_state *crtc_state)
549e2bfb 14047{
65edccce
VS
14048 struct intel_crtc_scaler_state *scaler_state =
14049 &crtc_state->scaler_state;
1c74eeaf 14050 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
549e2bfb 14051 int i;
549e2bfb 14052
1c74eeaf
NM
14053 crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
14054 if (!crtc->num_scalers)
14055 return;
14056
65edccce
VS
14057 for (i = 0; i < crtc->num_scalers; i++) {
14058 struct intel_scaler *scaler = &scaler_state->scalers[i];
14059
14060 scaler->in_use = 0;
14061 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
14062 }
14063
14064 scaler_state->scaler_id = -1;
14065}
14066
5ab0d85b 14067static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
14068{
14069 struct intel_crtc *intel_crtc;
f5de6e07 14070 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
14071 struct intel_plane *primary = NULL;
14072 struct intel_plane *cursor = NULL;
a81d6fa0 14073 int sprite, ret;
79e53945 14074
955382f3 14075 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
14076 if (!intel_crtc)
14077 return -ENOMEM;
79e53945 14078
f5de6e07 14079 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
14080 if (!crtc_state) {
14081 ret = -ENOMEM;
f5de6e07 14082 goto fail;
b079bd17 14083 }
550acefd
ACO
14084 intel_crtc->config = crtc_state;
14085 intel_crtc->base.state = &crtc_state->base;
07878248 14086 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14087
580503c7 14088 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
14089 if (IS_ERR(primary)) {
14090 ret = PTR_ERR(primary);
3d7d6510 14091 goto fail;
b079bd17 14092 }
d97d7b48 14093 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 14094
a81d6fa0 14095 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
14096 struct intel_plane *plane;
14097
580503c7 14098 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 14099 if (IS_ERR(plane)) {
b079bd17
VS
14100 ret = PTR_ERR(plane);
14101 goto fail;
14102 }
d97d7b48 14103 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
14104 }
14105
580503c7 14106 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 14107 if (IS_ERR(cursor)) {
b079bd17 14108 ret = PTR_ERR(cursor);
3d7d6510 14109 goto fail;
b079bd17 14110 }
d97d7b48 14111 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 14112
5ab0d85b 14113 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
14114 &primary->base, &cursor->base,
14115 &intel_crtc_funcs,
4d5d72b7 14116 "pipe %c", pipe_name(pipe));
3d7d6510
MR
14117 if (ret)
14118 goto fail;
79e53945 14119
80824003 14120 intel_crtc->pipe = pipe;
80824003 14121
1c74eeaf
NM
14122 /* initialize shared scalers */
14123 intel_crtc_init_scalers(intel_crtc, crtc_state);
14124
1947fd13
VS
14125 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
14126 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
14127 dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
14128
14129 if (INTEL_GEN(dev_priv) < 9) {
14130 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
14131
14132 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14133 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
14134 dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
14135 }
22fd0fab 14136
79e53945 14137 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 14138
8563b1e8
LL
14139 intel_color_init(&intel_crtc->base);
14140
87b6b101 14141 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
14142
14143 return 0;
3d7d6510
MR
14144
14145fail:
b079bd17
VS
14146 /*
14147 * drm_mode_config_cleanup() will free up any
14148 * crtcs/planes already initialized.
14149 */
f5de6e07 14150 kfree(crtc_state);
3d7d6510 14151 kfree(intel_crtc);
b079bd17
VS
14152
14153 return ret;
79e53945
JB
14154}
14155
752aa88a
JB
14156enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14157{
6e9f798d 14158 struct drm_device *dev = connector->base.dev;
752aa88a 14159
51fd371b 14160 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14161
51ec53da 14162 if (!connector->base.state->crtc)
752aa88a
JB
14163 return INVALID_PIPE;
14164
51ec53da 14165 return to_intel_crtc(connector->base.state->crtc)->pipe;
752aa88a
JB
14166}
14167
6a20fe7b
VS
14168int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
14169 struct drm_file *file)
08d7b3d1 14170{
08d7b3d1 14171 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14172 struct drm_crtc *drmmode_crtc;
c05422d5 14173 struct intel_crtc *crtc;
08d7b3d1 14174
418da172 14175 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
71240ed2 14176 if (!drmmode_crtc)
3f2c2057 14177 return -ENOENT;
08d7b3d1 14178
7707e653 14179 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14180 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14181
c05422d5 14182 return 0;
08d7b3d1
CW
14183}
14184
66a9278e 14185static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14186{
66a9278e
SV
14187 struct drm_device *dev = encoder->base.dev;
14188 struct intel_encoder *source_encoder;
79e53945 14189 int index_mask = 0;
79e53945
JB
14190 int entry = 0;
14191
b2784e15 14192 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14193 if (encoders_cloneable(encoder, source_encoder))
66a9278e
SV
14194 index_mask |= (1 << entry);
14195
79e53945
JB
14196 entry++;
14197 }
4ef69c7a 14198
79e53945
JB
14199 return index_mask;
14200}
14201
646d5772 14202static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 14203{
646d5772 14204 if (!IS_MOBILE(dev_priv))
4d302442
CW
14205 return false;
14206
14207 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14208 return false;
14209
5db94019 14210 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14211 return false;
14212
14213 return true;
14214}
14215
6315b5d3 14216static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 14217{
6315b5d3 14218 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
14219 return false;
14220
50a0bc90 14221 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
14222 return false;
14223
920a14b2 14224 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
14225 return false;
14226
4f8036a2
TU
14227 if (HAS_PCH_LPT_H(dev_priv) &&
14228 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
14229 return false;
14230
70ac54d0 14231 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 14232 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
14233 return false;
14234
e4abb733 14235 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14236 return false;
14237
14238 return true;
14239}
14240
8090ba8c
ID
14241void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
14242{
14243 int pps_num;
14244 int pps_idx;
14245
14246 if (HAS_DDI(dev_priv))
14247 return;
14248 /*
14249 * This w/a is needed at least on CPT/PPT, but to be sure apply it
14250 * everywhere where registers can be write protected.
14251 */
14252 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14253 pps_num = 2;
14254 else
14255 pps_num = 1;
14256
14257 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
14258 u32 val = I915_READ(PP_CONTROL(pps_idx));
14259
14260 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
14261 I915_WRITE(PP_CONTROL(pps_idx), val);
14262 }
14263}
14264
44cb734c
ID
14265static void intel_pps_init(struct drm_i915_private *dev_priv)
14266{
cc3f90f0 14267 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
14268 dev_priv->pps_mmio_base = PCH_PPS_BASE;
14269 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14270 dev_priv->pps_mmio_base = VLV_PPS_BASE;
14271 else
14272 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
14273
14274 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
14275}
14276
c39055b0 14277static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 14278{
4ef69c7a 14279 struct intel_encoder *encoder;
cb0953d7 14280 bool dpd_is_edp = false;
79e53945 14281
44cb734c
ID
14282 intel_pps_init(dev_priv);
14283
fc0c5a9d
CW
14284 if (INTEL_INFO(dev_priv)->num_pipes == 0)
14285 return;
14286
97a824e1
ID
14287 /*
14288 * intel_edp_init_connector() depends on this completing first, to
14289 * prevent the registeration of both eDP and LVDS and the incorrect
14290 * sharing of the PPS.
14291 */
c39055b0 14292 intel_lvds_init(dev_priv);
79e53945 14293
6315b5d3 14294 if (intel_crt_present(dev_priv))
c39055b0 14295 intel_crt_init(dev_priv);
cb0953d7 14296
00c92d92
PZ
14297 if (IS_ICELAKE(dev_priv)) {
14298 intel_ddi_init(dev_priv, PORT_A);
14299 intel_ddi_init(dev_priv, PORT_B);
14300 intel_ddi_init(dev_priv, PORT_C);
14301 intel_ddi_init(dev_priv, PORT_D);
14302 intel_ddi_init(dev_priv, PORT_E);
14303 intel_ddi_init(dev_priv, PORT_F);
14304 } else if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
14305 /*
14306 * FIXME: Broxton doesn't support port detection via the
14307 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14308 * detect the ports.
14309 */
c39055b0
ACO
14310 intel_ddi_init(dev_priv, PORT_A);
14311 intel_ddi_init(dev_priv, PORT_B);
14312 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 14313
e518634b 14314 vlv_dsi_init(dev_priv);
4f8036a2 14315 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
14316 int found;
14317
de31facd
JB
14318 /*
14319 * Haswell uses DDI functions to detect digital outputs.
14320 * On SKL pre-D0 the strap isn't connected, so we assume
14321 * it's there.
14322 */
77179400 14323 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14324 /* WaIgnoreDDIAStrap: skl */
b976dc53 14325 if (found || IS_GEN9_BC(dev_priv))
c39055b0 14326 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5 14327
9787e835 14328 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
0e72a5b5
ED
14329 * register */
14330 found = I915_READ(SFUSE_STRAP);
14331
14332 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 14333 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 14334 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 14335 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 14336 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 14337 intel_ddi_init(dev_priv, PORT_D);
9787e835
RV
14338 if (found & SFUSE_STRAP_DDIF_DETECTED)
14339 intel_ddi_init(dev_priv, PORT_F);
2800e4c2
RV
14340 /*
14341 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14342 */
b976dc53 14343 if (IS_GEN9_BC(dev_priv) &&
2800e4c2
RV
14344 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14345 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14346 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 14347 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 14348
6e266956 14349 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 14350 int found;
7b91bf7f 14351 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
270b3042 14352
646d5772 14353 if (has_edp_a(dev_priv))
c39055b0 14354 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 14355
dc0fa718 14356 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14357 /* PCH SDVOB multiplex with HDMIB */
c39055b0 14358 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 14359 if (!found)
c39055b0 14360 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 14361 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 14362 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
14363 }
14364
dc0fa718 14365 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 14366 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 14367
dc0fa718 14368 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 14369 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 14370
5eb08b69 14371 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 14372 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 14373
270b3042 14374 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 14375 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 14376 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 14377 bool has_edp, has_port;
457c52d8 14378
e17ac6db
VS
14379 /*
14380 * The DP_DETECTED bit is the latched state of the DDC
14381 * SDA pin at boot. However since eDP doesn't require DDC
14382 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14383 * eDP ports may have been muxed to an alternate function.
14384 * Thus we can't rely on the DP_DETECTED bit alone to detect
14385 * eDP ports. Consult the VBT as well as DP_DETECTED to
14386 * detect eDP ports.
22f35042
VS
14387 *
14388 * Sadly the straps seem to be missing sometimes even for HDMI
14389 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14390 * and VBT for the presence of the port. Additionally we can't
14391 * trust the port type the VBT declares as we've seen at least
14392 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 14393 */
7b91bf7f 14394 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
22f35042
VS
14395 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14396 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 14397 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 14398 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14399 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 14400
7b91bf7f 14401 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
22f35042
VS
14402 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14403 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 14404 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 14405 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 14406 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 14407
920a14b2 14408 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
14409 /*
14410 * eDP not supported on port D,
14411 * so no need to worry about it
14412 */
14413 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14414 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 14415 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 14416 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 14417 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
14418 }
14419
e518634b 14420 vlv_dsi_init(dev_priv);
5db94019 14421 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 14422 bool found = false;
7d57382e 14423
e2debe91 14424 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14425 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 14426 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 14427 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 14428 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 14429 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 14430 }
27185ae1 14431
9beb5fea 14432 if (!found && IS_G4X(dev_priv))
c39055b0 14433 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 14434 }
13520b05
KH
14435
14436 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14437
e2debe91 14438 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14439 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 14440 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 14441 }
27185ae1 14442
e2debe91 14443 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14444
9beb5fea 14445 if (IS_G4X(dev_priv)) {
b01f2c3a 14446 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 14447 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 14448 }
9beb5fea 14449 if (IS_G4X(dev_priv))
c39055b0 14450 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 14451 }
27185ae1 14452
9beb5fea 14453 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 14454 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 14455 } else if (IS_GEN2(dev_priv))
c39055b0 14456 intel_dvo_init(dev_priv);
79e53945 14457
56b857a5 14458 if (SUPPORTS_TV(dev_priv))
c39055b0 14459 intel_tv_init(dev_priv);
79e53945 14460
c39055b0 14461 intel_psr_init(dev_priv);
7c8f8a70 14462
c39055b0 14463 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
14464 encoder->base.possible_crtcs = encoder->crtc_mask;
14465 encoder->base.possible_clones =
66a9278e 14466 intel_encoder_clones(encoder);
79e53945 14467 }
47356eb6 14468
c39055b0 14469 intel_init_pch_refclk(dev_priv);
270b3042 14470
c39055b0 14471 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
14472}
14473
14474static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14475{
14476 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
a5ff7a45 14477 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
79e53945 14478
ef2d633e 14479 drm_framebuffer_cleanup(fb);
70001cd2 14480
a5ff7a45
DS
14481 i915_gem_object_lock(obj);
14482 WARN_ON(!obj->framebuffer_references--);
14483 i915_gem_object_unlock(obj);
dd689287 14484
a5ff7a45 14485 i915_gem_object_put(obj);
70001cd2 14486
79e53945
JB
14487 kfree(intel_fb);
14488}
14489
14490static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14491 struct drm_file *file,
79e53945
JB
14492 unsigned int *handle)
14493{
a5ff7a45 14494 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
79e53945 14495
cc917ab4
CW
14496 if (obj->userptr.mm) {
14497 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14498 return -EINVAL;
14499 }
14500
05394f39 14501 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14502}
14503
86c98588
RV
14504static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14505 struct drm_file *file,
14506 unsigned flags, unsigned color,
14507 struct drm_clip_rect *clips,
14508 unsigned num_clips)
14509{
5a97bcc6 14510 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 14511
5a97bcc6 14512 i915_gem_object_flush_if_display(obj);
d59b21ec 14513 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
86c98588
RV
14514
14515 return 0;
14516}
14517
79e53945
JB
14518static const struct drm_framebuffer_funcs intel_fb_funcs = {
14519 .destroy = intel_user_framebuffer_destroy,
14520 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14521 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14522};
14523
b321803d 14524static
920a14b2
TU
14525u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
14526 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 14527{
645d91f6
VS
14528 struct intel_crtc *crtc;
14529 struct intel_plane *plane;
b321803d 14530
645d91f6
VS
14531 /*
14532 * We assume the primary plane for pipe A has
14533 * the highest stride limits of them all.
14534 */
14535 crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
14536 plane = to_intel_plane(crtc->base.primary);
ac484963 14537
645d91f6
VS
14538 return plane->max_stride(plane, pixel_format, fb_modifier,
14539 DRM_MODE_ROTATE_0);
b321803d
DL
14540}
14541
24dbf51a
CW
14542static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
14543 struct drm_i915_gem_object *obj,
14544 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14545{
24dbf51a 14546 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2e2adb05 14547 struct drm_framebuffer *fb = &intel_fb->base;
b3c11ac2 14548 struct drm_format_name_buf format_name;
2e2adb05 14549 u32 pitch_limit;
dd689287 14550 unsigned int tiling, stride;
24dbf51a 14551 int ret = -EINVAL;
2e2adb05 14552 int i;
79e53945 14553
dd689287
CW
14554 i915_gem_object_lock(obj);
14555 obj->framebuffer_references++;
14556 tiling = i915_gem_object_get_tiling(obj);
14557 stride = i915_gem_object_get_stride(obj);
14558 i915_gem_object_unlock(obj);
dd4916c5 14559
2a80eada 14560 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
14561 /*
14562 * If there's a fence, enforce that
14563 * the fb modifier and tiling mode match.
14564 */
14565 if (tiling != I915_TILING_NONE &&
14566 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14567 DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
24dbf51a 14568 goto err;
2a80eada
SV
14569 }
14570 } else {
c2ff7370 14571 if (tiling == I915_TILING_X) {
2a80eada 14572 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 14573 } else if (tiling == I915_TILING_Y) {
144cc143 14574 DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
24dbf51a 14575 goto err;
2a80eada
SV
14576 }
14577 }
14578
9a8f0a12
TU
14579 /* Passed in modifier sanity checking. */
14580 switch (mode_cmd->modifier[0]) {
2e2adb05
VS
14581 case I915_FORMAT_MOD_Y_TILED_CCS:
14582 case I915_FORMAT_MOD_Yf_TILED_CCS:
14583 switch (mode_cmd->pixel_format) {
14584 case DRM_FORMAT_XBGR8888:
14585 case DRM_FORMAT_ABGR8888:
14586 case DRM_FORMAT_XRGB8888:
14587 case DRM_FORMAT_ARGB8888:
14588 break;
14589 default:
14590 DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
14591 goto err;
14592 }
14593 /* fall through */
9a8f0a12
TU
14594 case I915_FORMAT_MOD_Y_TILED:
14595 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 14596 if (INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14597 DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
14598 mode_cmd->modifier[0]);
24dbf51a 14599 goto err;
9a8f0a12 14600 }
2f075565 14601 case DRM_FORMAT_MOD_LINEAR:
9a8f0a12
TU
14602 case I915_FORMAT_MOD_X_TILED:
14603 break;
14604 default:
144cc143
VS
14605 DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
14606 mode_cmd->modifier[0]);
24dbf51a 14607 goto err;
c16ed4be 14608 }
57cd6508 14609
c2ff7370
VS
14610 /*
14611 * gen2/3 display engine uses the fence if present,
14612 * so the tiling mode must match the fb modifier exactly.
14613 */
c56b89f1 14614 if (INTEL_GEN(dev_priv) < 4 &&
c2ff7370 14615 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
144cc143 14616 DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 14617 goto err;
c2ff7370
VS
14618 }
14619
920a14b2 14620 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 14621 mode_cmd->pixel_format);
a35cdaa0 14622 if (mode_cmd->pitches[0] > pitch_limit) {
144cc143 14623 DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
2f075565 14624 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
144cc143
VS
14625 "tiled" : "linear",
14626 mode_cmd->pitches[0], pitch_limit);
24dbf51a 14627 goto err;
c16ed4be 14628 }
5d7bd705 14629
c2ff7370
VS
14630 /*
14631 * If there's a fence, enforce that
14632 * the fb pitch and fence stride match.
14633 */
144cc143
VS
14634 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
14635 DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
14636 mode_cmd->pitches[0], stride);
24dbf51a 14637 goto err;
c16ed4be 14638 }
5d7bd705 14639
57779d06 14640 /* Reject formats not supported by any plane early. */
308e5bcb 14641 switch (mode_cmd->pixel_format) {
57779d06 14642 case DRM_FORMAT_C8:
04b3924d
VS
14643 case DRM_FORMAT_RGB565:
14644 case DRM_FORMAT_XRGB8888:
14645 case DRM_FORMAT_ARGB8888:
57779d06
VS
14646 break;
14647 case DRM_FORMAT_XRGB1555:
6315b5d3 14648 if (INTEL_GEN(dev_priv) > 3) {
144cc143
VS
14649 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14650 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14651 goto err;
c16ed4be 14652 }
57779d06 14653 break;
57779d06 14654 case DRM_FORMAT_ABGR8888:
920a14b2 14655 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 14656 INTEL_GEN(dev_priv) < 9) {
144cc143
VS
14657 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14658 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14659 goto err;
6c0fd451
DL
14660 }
14661 break;
14662 case DRM_FORMAT_XBGR8888:
04b3924d 14663 case DRM_FORMAT_XRGB2101010:
57779d06 14664 case DRM_FORMAT_XBGR2101010:
6315b5d3 14665 if (INTEL_GEN(dev_priv) < 4) {
144cc143
VS
14666 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14667 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14668 goto err;
c16ed4be 14669 }
b5626747 14670 break;
7531208b 14671 case DRM_FORMAT_ABGR2101010:
920a14b2 14672 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
144cc143
VS
14673 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14674 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14675 goto err;
7531208b
DL
14676 }
14677 break;
04b3924d
VS
14678 case DRM_FORMAT_YUYV:
14679 case DRM_FORMAT_UYVY:
14680 case DRM_FORMAT_YVYU:
14681 case DRM_FORMAT_VYUY:
ab33081a 14682 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
144cc143
VS
14683 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14684 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14685 goto err;
c16ed4be 14686 }
57cd6508 14687 break;
e44134f2 14688 case DRM_FORMAT_NV12:
e44134f2 14689 if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
b45649fb 14690 IS_BROXTON(dev_priv) || INTEL_GEN(dev_priv) >= 11) {
e44134f2
CK
14691 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14692 drm_get_format_name(mode_cmd->pixel_format,
14693 &format_name));
14694 goto err;
14695 }
14696 break;
57cd6508 14697 default:
144cc143
VS
14698 DRM_DEBUG_KMS("unsupported pixel format: %s\n",
14699 drm_get_format_name(mode_cmd->pixel_format, &format_name));
9aceb5c1 14700 goto err;
57cd6508
CW
14701 }
14702
90f9a336
VS
14703 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14704 if (mode_cmd->offsets[0] != 0)
24dbf51a 14705 goto err;
90f9a336 14706
2e2adb05 14707 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
d88c4afd 14708
e44134f2
CK
14709 if (fb->format->format == DRM_FORMAT_NV12 &&
14710 (fb->width < SKL_MIN_YUV_420_SRC_W ||
14711 fb->height < SKL_MIN_YUV_420_SRC_H ||
14712 (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
14713 DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
f42f3438 14714 goto err;
e44134f2
CK
14715 }
14716
2e2adb05
VS
14717 for (i = 0; i < fb->format->num_planes; i++) {
14718 u32 stride_alignment;
14719
14720 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
14721 DRM_DEBUG_KMS("bad plane %d handle\n", i);
37875d6b 14722 goto err;
2e2adb05
VS
14723 }
14724
14725 stride_alignment = intel_fb_stride_alignment(fb, i);
14726
14727 /*
14728 * Display WA #0531: skl,bxt,kbl,glk
14729 *
14730 * Render decompression and plane width > 3840
14731 * combined with horizontal panning requires the
14732 * plane stride to be a multiple of 4. We'll just
14733 * require the entire fb to accommodate that to avoid
14734 * potential runtime errors at plane configuration time.
14735 */
14736 if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
63eaf9ac 14737 is_ccs_modifier(fb->modifier))
2e2adb05
VS
14738 stride_alignment *= 4;
14739
14740 if (fb->pitches[i] & (stride_alignment - 1)) {
14741 DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
14742 i, fb->pitches[i], stride_alignment);
14743 goto err;
14744 }
d88c4afd 14745
a268bcd7
DS
14746 fb->obj[i] = &obj->base;
14747 }
c7d73f6a 14748
2e2adb05 14749 ret = intel_fill_fb_info(dev_priv, fb);
6687c906 14750 if (ret)
9aceb5c1 14751 goto err;
2d7a215f 14752
2e2adb05 14753 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
79e53945
JB
14754 if (ret) {
14755 DRM_ERROR("framebuffer init failed %d\n", ret);
24dbf51a 14756 goto err;
79e53945
JB
14757 }
14758
79e53945 14759 return 0;
24dbf51a
CW
14760
14761err:
dd689287
CW
14762 i915_gem_object_lock(obj);
14763 obj->framebuffer_references--;
14764 i915_gem_object_unlock(obj);
24dbf51a 14765 return ret;
79e53945
JB
14766}
14767
79e53945
JB
14768static struct drm_framebuffer *
14769intel_user_framebuffer_create(struct drm_device *dev,
14770 struct drm_file *filp,
1eb83451 14771 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14772{
dcb1394e 14773 struct drm_framebuffer *fb;
05394f39 14774 struct drm_i915_gem_object *obj;
76dc3769 14775 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14776
03ac0642
CW
14777 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
14778 if (!obj)
cce13ff7 14779 return ERR_PTR(-ENOENT);
79e53945 14780
24dbf51a 14781 fb = intel_framebuffer_create(obj, &mode_cmd);
dcb1394e 14782 if (IS_ERR(fb))
f0cd5182 14783 i915_gem_object_put(obj);
dcb1394e
LW
14784
14785 return fb;
79e53945
JB
14786}
14787
778e23a9
CW
14788static void intel_atomic_state_free(struct drm_atomic_state *state)
14789{
14790 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14791
14792 drm_atomic_state_default_release(state);
14793
14794 i915_sw_fence_fini(&intel_state->commit_ready);
14795
14796 kfree(state);
14797}
14798
e995ca0b
VS
14799static enum drm_mode_status
14800intel_mode_valid(struct drm_device *dev,
14801 const struct drm_display_mode *mode)
14802{
ad77c537
VS
14803 struct drm_i915_private *dev_priv = to_i915(dev);
14804 int hdisplay_max, htotal_max;
14805 int vdisplay_max, vtotal_max;
14806
e4dd27aa
VS
14807 /*
14808 * Can't reject DBLSCAN here because Xorg ddxen can add piles
14809 * of DBLSCAN modes to the output's mode list when they detect
14810 * the scaling mode property on the connector. And they don't
14811 * ask the kernel to validate those modes in any way until
14812 * modeset time at which point the client gets a protocol error.
14813 * So in order to not upset those clients we silently ignore the
14814 * DBLSCAN flag on such connectors. For other connectors we will
14815 * reject modes with the DBLSCAN flag in encoder->compute_config().
14816 * And we always reject DBLSCAN modes in connector->mode_valid()
14817 * as we never want such modes on the connector's mode list.
14818 */
14819
e995ca0b
VS
14820 if (mode->vscan > 1)
14821 return MODE_NO_VSCAN;
14822
e995ca0b
VS
14823 if (mode->flags & DRM_MODE_FLAG_HSKEW)
14824 return MODE_H_ILLEGAL;
14825
14826 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
14827 DRM_MODE_FLAG_NCSYNC |
14828 DRM_MODE_FLAG_PCSYNC))
14829 return MODE_HSYNC;
14830
14831 if (mode->flags & (DRM_MODE_FLAG_BCAST |
14832 DRM_MODE_FLAG_PIXMUX |
14833 DRM_MODE_FLAG_CLKDIV2))
14834 return MODE_BAD;
14835
ad77c537
VS
14836 if (INTEL_GEN(dev_priv) >= 9 ||
14837 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
14838 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
14839 vdisplay_max = 4096;
14840 htotal_max = 8192;
14841 vtotal_max = 8192;
14842 } else if (INTEL_GEN(dev_priv) >= 3) {
14843 hdisplay_max = 4096;
14844 vdisplay_max = 4096;
14845 htotal_max = 8192;
14846 vtotal_max = 8192;
14847 } else {
14848 hdisplay_max = 2048;
14849 vdisplay_max = 2048;
14850 htotal_max = 4096;
14851 vtotal_max = 4096;
14852 }
14853
14854 if (mode->hdisplay > hdisplay_max ||
14855 mode->hsync_start > htotal_max ||
14856 mode->hsync_end > htotal_max ||
14857 mode->htotal > htotal_max)
14858 return MODE_H_ILLEGAL;
14859
14860 if (mode->vdisplay > vdisplay_max ||
14861 mode->vsync_start > vtotal_max ||
14862 mode->vsync_end > vtotal_max ||
14863 mode->vtotal > vtotal_max)
14864 return MODE_V_ILLEGAL;
14865
e995ca0b
VS
14866 return MODE_OK;
14867}
14868
79e53945 14869static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14870 .fb_create = intel_user_framebuffer_create,
bbfb6ce8 14871 .get_format_info = intel_get_format_info,
0632fef6 14872 .output_poll_changed = intel_fbdev_output_poll_changed,
e995ca0b 14873 .mode_valid = intel_mode_valid,
5ee67f1c
MR
14874 .atomic_check = intel_atomic_check,
14875 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14876 .atomic_state_alloc = intel_atomic_state_alloc,
14877 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 14878 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
14879};
14880
88212941
ID
14881/**
14882 * intel_init_display_hooks - initialize the display modesetting hooks
14883 * @dev_priv: device private
14884 */
14885void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 14886{
7ff89ca2
VS
14887 intel_init_cdclk_hooks(dev_priv);
14888
c56b89f1 14889 if (INTEL_GEN(dev_priv) >= 9) {
bc8d7dff 14890 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14891 dev_priv->display.get_initial_plane_config =
14892 skylake_get_initial_plane_config;
bc8d7dff
DL
14893 dev_priv->display.crtc_compute_clock =
14894 haswell_crtc_compute_clock;
14895 dev_priv->display.crtc_enable = haswell_crtc_enable;
14896 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14897 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 14898 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1 14899 dev_priv->display.get_initial_plane_config =
81894b2f 14900 i9xx_get_initial_plane_config;
797d0259
ACO
14901 dev_priv->display.crtc_compute_clock =
14902 haswell_crtc_compute_clock;
4f771f10
PZ
14903 dev_priv->display.crtc_enable = haswell_crtc_enable;
14904 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 14905 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 14906 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1 14907 dev_priv->display.get_initial_plane_config =
81894b2f 14908 i9xx_get_initial_plane_config;
3fb37703
ACO
14909 dev_priv->display.crtc_compute_clock =
14910 ironlake_crtc_compute_clock;
76e5a89c
SV
14911 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14912 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 14913 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 14914 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14915 dev_priv->display.get_initial_plane_config =
14916 i9xx_get_initial_plane_config;
65b3d6a9
ACO
14917 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14918 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14919 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14920 } else if (IS_VALLEYVIEW(dev_priv)) {
14921 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14922 dev_priv->display.get_initial_plane_config =
14923 i9xx_get_initial_plane_config;
14924 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
14925 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14926 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
14927 } else if (IS_G4X(dev_priv)) {
14928 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14929 dev_priv->display.get_initial_plane_config =
14930 i9xx_get_initial_plane_config;
14931 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14932 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14933 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
14934 } else if (IS_PINEVIEW(dev_priv)) {
14935 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14936 dev_priv->display.get_initial_plane_config =
14937 i9xx_get_initial_plane_config;
14938 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14939 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14940 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 14941 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 14942 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14943 dev_priv->display.get_initial_plane_config =
14944 i9xx_get_initial_plane_config;
d6dfee7a 14945 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
SV
14946 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14947 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
14948 } else {
14949 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14950 dev_priv->display.get_initial_plane_config =
14951 i9xx_get_initial_plane_config;
14952 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14953 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14954 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14955 }
e70236a8 14956
88212941 14957 if (IS_GEN5(dev_priv)) {
3bb11b53 14958 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 14959 } else if (IS_GEN6(dev_priv)) {
3bb11b53 14960 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 14961 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
14962 /* FIXME: detect B0+ stepping and use auto training */
14963 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 14964 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 14965 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
14966 }
14967
bd30ca2d 14968 if (INTEL_GEN(dev_priv) >= 9)
27082493
L
14969 dev_priv->display.update_crtcs = skl_update_crtcs;
14970 else
14971 dev_priv->display.update_crtcs = intel_update_crtcs;
e70236a8
JB
14972}
14973
435793df
KP
14974/*
14975 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14976 */
14977static void quirk_ssc_force_disable(struct drm_device *dev)
14978{
fac5e23e 14979 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 14980 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14981 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14982}
14983
4dca20ef 14984/*
5a15ab5b
CE
14985 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14986 * brightness value
4dca20ef
CE
14987 */
14988static void quirk_invert_brightness(struct drm_device *dev)
14989{
fac5e23e 14990 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 14991 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14992 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14993}
14994
9c72cc6f
SD
14995/* Some VBT's incorrectly indicate no backlight is present */
14996static void quirk_backlight_present(struct drm_device *dev)
14997{
fac5e23e 14998 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
14999 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15000 DRM_INFO("applying backlight present quirk\n");
15001}
15002
c99a259b
MN
15003/* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
15004 * which is 300 ms greater than eDP spec T12 min.
15005 */
15006static void quirk_increase_t12_delay(struct drm_device *dev)
15007{
15008 struct drm_i915_private *dev_priv = to_i915(dev);
15009
15010 dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
15011 DRM_INFO("Applying T12 delay quirk\n");
15012}
15013
90c3e219
CT
15014/*
15015 * GeminiLake NUC HDMI outputs require additional off time
15016 * this allows the onboard retimer to correctly sync to signal
15017 */
15018static void quirk_increase_ddi_disabled_time(struct drm_device *dev)
15019{
15020 struct drm_i915_private *dev_priv = to_i915(dev);
15021
15022 dev_priv->quirks |= QUIRK_INCREASE_DDI_DISABLED_TIME;
15023 DRM_INFO("Applying Increase DDI Disabled quirk\n");
15024}
15025
b690e96c
JB
15026struct intel_quirk {
15027 int device;
15028 int subsystem_vendor;
15029 int subsystem_device;
15030 void (*hook)(struct drm_device *dev);
15031};
15032
5f85f176
EE
15033/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15034struct intel_dmi_quirk {
15035 void (*hook)(struct drm_device *dev);
15036 const struct dmi_system_id (*dmi_id_list)[];
15037};
15038
15039static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15040{
15041 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15042 return 1;
15043}
15044
15045static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15046 {
15047 .dmi_id_list = &(const struct dmi_system_id[]) {
15048 {
15049 .callback = intel_dmi_reverse_brightness,
15050 .ident = "NCR Corporation",
15051 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15052 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15053 },
15054 },
15055 { } /* terminating entry */
15056 },
15057 .hook = quirk_invert_brightness,
15058 },
15059};
15060
c43b5634 15061static struct intel_quirk intel_quirks[] = {
435793df
KP
15062 /* Lenovo U160 cannot use SSC on LVDS */
15063 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15064
15065 /* Sony Vaio Y cannot use SSC on LVDS */
15066 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15067
be505f64
AH
15068 /* Acer Aspire 5734Z must invert backlight brightness */
15069 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15070
15071 /* Acer/eMachines G725 */
15072 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15073
15074 /* Acer/eMachines e725 */
15075 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15076
15077 /* Acer/Packard Bell NCL20 */
15078 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15079
15080 /* Acer Aspire 4736Z */
15081 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15082
15083 /* Acer Aspire 5336 */
15084 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15085
15086 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15087 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15088
dfb3d47b
SD
15089 /* Acer C720 Chromebook (Core i3 4005U) */
15090 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15091
b2a9601c 15092 /* Apple Macbook 2,1 (Core 2 T7400) */
15093 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15094
1b9448b0
JN
15095 /* Apple Macbook 4,1 */
15096 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15097
d4967d8c
SD
15098 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15099 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15100
15101 /* HP Chromebook 14 (Celeron 2955U) */
15102 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15103
15104 /* Dell Chromebook 11 */
15105 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15106
15107 /* Dell Chromebook 11 (2015 version) */
15108 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
c99a259b
MN
15109
15110 /* Toshiba Satellite P50-C-18C */
15111 { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
90c3e219
CT
15112
15113 /* GeminiLake NUC */
15114 { 0x3185, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
15115 { 0x3184, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
15116 /* ASRock ITX*/
15117 { 0x3185, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
15118 { 0x3184, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
b690e96c
JB
15119};
15120
15121static void intel_init_quirks(struct drm_device *dev)
15122{
15123 struct pci_dev *d = dev->pdev;
15124 int i;
15125
15126 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15127 struct intel_quirk *q = &intel_quirks[i];
15128
15129 if (d->device == q->device &&
15130 (d->subsystem_vendor == q->subsystem_vendor ||
15131 q->subsystem_vendor == PCI_ANY_ID) &&
15132 (d->subsystem_device == q->subsystem_device ||
15133 q->subsystem_device == PCI_ANY_ID))
15134 q->hook(dev);
15135 }
5f85f176
EE
15136 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15137 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15138 intel_dmi_quirks[i].hook(dev);
15139 }
b690e96c
JB
15140}
15141
9cce37f4 15142/* Disable the VGA plane that we never use */
29b74b7f 15143static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 15144{
52a05c30 15145 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 15146 u8 sr1;
920a14b2 15147 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 15148
2b37c616 15149 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 15150 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15151 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15152 sr1 = inb(VGA_SR_DATA);
15153 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 15154 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
15155 udelay(300);
15156
01f5a626 15157 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15158 POSTING_READ(vga_reg);
15159}
15160
f817586c
SV
15161void intel_modeset_init_hw(struct drm_device *dev)
15162{
fac5e23e 15163 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 15164
4c75b940 15165 intel_update_cdclk(dev_priv);
cfddadc9 15166 intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
bb0f4aab 15167 dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
f817586c
SV
15168}
15169
d93c0372
MR
15170/*
15171 * Calculate what we think the watermarks should be for the state we've read
15172 * out of the hardware and then immediately program those watermarks so that
15173 * we ensure the hardware settings match our internal state.
15174 *
15175 * We can calculate what we think WM's should be by creating a duplicate of the
15176 * current state (which was constructed during hardware readout) and running it
15177 * through the atomic check code to calculate new watermark values in the
15178 * state object.
15179 */
15180static void sanitize_watermarks(struct drm_device *dev)
15181{
15182 struct drm_i915_private *dev_priv = to_i915(dev);
15183 struct drm_atomic_state *state;
ccf010fb 15184 struct intel_atomic_state *intel_state;
d93c0372
MR
15185 struct drm_crtc *crtc;
15186 struct drm_crtc_state *cstate;
15187 struct drm_modeset_acquire_ctx ctx;
15188 int ret;
15189 int i;
15190
15191 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15192 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15193 return;
15194
15195 /*
15196 * We need to hold connection_mutex before calling duplicate_state so
15197 * that the connector loop is protected.
15198 */
15199 drm_modeset_acquire_init(&ctx, 0);
15200retry:
0cd1262d 15201 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15202 if (ret == -EDEADLK) {
15203 drm_modeset_backoff(&ctx);
15204 goto retry;
15205 } else if (WARN_ON(ret)) {
0cd1262d 15206 goto fail;
d93c0372
MR
15207 }
15208
15209 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15210 if (WARN_ON(IS_ERR(state)))
0cd1262d 15211 goto fail;
d93c0372 15212
ccf010fb
ML
15213 intel_state = to_intel_atomic_state(state);
15214
ed4a6a7c
MR
15215 /*
15216 * Hardware readout is the only time we don't want to calculate
15217 * intermediate watermarks (since we don't trust the current
15218 * watermarks).
15219 */
602ae835
VS
15220 if (!HAS_GMCH_DISPLAY(dev_priv))
15221 intel_state->skip_intermediate_wm = true;
ed4a6a7c 15222
d93c0372
MR
15223 ret = intel_atomic_check(dev, state);
15224 if (ret) {
15225 /*
15226 * If we fail here, it means that the hardware appears to be
15227 * programmed in a way that shouldn't be possible, given our
15228 * understanding of watermark requirements. This might mean a
15229 * mistake in the hardware readout code or a mistake in the
15230 * watermark calculations for a given platform. Raise a WARN
15231 * so that this is noticeable.
15232 *
15233 * If this actually happens, we'll have to just leave the
15234 * BIOS-programmed watermarks untouched and hope for the best.
15235 */
15236 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 15237 goto put_state;
d93c0372
MR
15238 }
15239
15240 /* Write calculated watermark values back */
aa5e9b47 15241 for_each_new_crtc_in_state(state, crtc, cstate, i) {
d93c0372
MR
15242 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15243
ed4a6a7c 15244 cs->wm.need_postvbl_update = true;
ccf010fb 15245 dev_priv->display.optimize_watermarks(intel_state, cs);
556fe36d
ML
15246
15247 to_intel_crtc_state(crtc->state)->wm = cs->wm;
d93c0372
MR
15248 }
15249
b9a1b717 15250put_state:
0853695c 15251 drm_atomic_state_put(state);
0cd1262d 15252fail:
d93c0372
MR
15253 drm_modeset_drop_locks(&ctx);
15254 drm_modeset_acquire_fini(&ctx);
15255}
15256
58ecd9d5
CW
15257static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
15258{
15259 if (IS_GEN5(dev_priv)) {
15260 u32 fdi_pll_clk =
15261 I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
15262
15263 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
15264 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
15265 dev_priv->fdi_pll_freq = 270000;
15266 } else {
15267 return;
15268 }
15269
15270 DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
15271}
15272
516a49cc
AS
15273static int intel_initial_commit(struct drm_device *dev)
15274{
15275 struct drm_atomic_state *state = NULL;
15276 struct drm_modeset_acquire_ctx ctx;
15277 struct drm_crtc *crtc;
15278 struct drm_crtc_state *crtc_state;
15279 int ret = 0;
15280
15281 state = drm_atomic_state_alloc(dev);
15282 if (!state)
15283 return -ENOMEM;
15284
15285 drm_modeset_acquire_init(&ctx, 0);
15286
15287retry:
15288 state->acquire_ctx = &ctx;
15289
15290 drm_for_each_crtc(crtc, dev) {
15291 crtc_state = drm_atomic_get_crtc_state(state, crtc);
15292 if (IS_ERR(crtc_state)) {
15293 ret = PTR_ERR(crtc_state);
15294 goto out;
15295 }
15296
15297 if (crtc_state->active) {
15298 ret = drm_atomic_add_affected_planes(state, crtc);
15299 if (ret)
15300 goto out;
c773058d
VS
15301
15302 /*
15303 * FIXME hack to force a LUT update to avoid the
15304 * plane update forcing the pipe gamma on without
15305 * having a proper LUT loaded. Remove once we
15306 * have readout for pipe gamma enable.
15307 */
15308 crtc_state->color_mgmt_changed = true;
516a49cc
AS
15309 }
15310 }
15311
15312 ret = drm_atomic_commit(state);
15313
15314out:
15315 if (ret == -EDEADLK) {
15316 drm_atomic_state_clear(state);
15317 drm_modeset_backoff(&ctx);
15318 goto retry;
15319 }
15320
15321 drm_atomic_state_put(state);
15322
15323 drm_modeset_drop_locks(&ctx);
15324 drm_modeset_acquire_fini(&ctx);
15325
15326 return ret;
15327}
15328
b079bd17 15329int intel_modeset_init(struct drm_device *dev)
79e53945 15330{
72e96d64
JL
15331 struct drm_i915_private *dev_priv = to_i915(dev);
15332 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 15333 enum pipe pipe;
46f297fb 15334 struct intel_crtc *crtc;
516a49cc 15335 int ret;
79e53945 15336
757fffcf
VS
15337 dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
15338
79e53945
JB
15339 drm_mode_config_init(dev);
15340
15341 dev->mode_config.min_width = 0;
15342 dev->mode_config.min_height = 0;
15343
019d96cb
DA
15344 dev->mode_config.preferred_depth = 24;
15345 dev->mode_config.prefer_shadow = 1;
15346
25bab385
TU
15347 dev->mode_config.allow_fb_modifiers = true;
15348
e6ecefaa 15349 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15350
400c19d9 15351 init_llist_head(&dev_priv->atomic_helper.free_list);
eb955eee 15352 INIT_WORK(&dev_priv->atomic_helper.free_work,
ba318c61 15353 intel_atomic_helper_free_state_worker);
eb955eee 15354
b690e96c
JB
15355 intel_init_quirks(dev);
15356
62d75df7 15357 intel_init_pm(dev_priv);
1fa61106 15358
69f92f67
LW
15359 /*
15360 * There may be no VBT; and if the BIOS enabled SSC we can
15361 * just keep using it to avoid unnecessary flicker. Whereas if the
15362 * BIOS isn't using it, don't assume it will work even if the VBT
15363 * indicates as much.
15364 */
6e266956 15365 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
15366 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15367 DREF_SSC1_ENABLE);
15368
15369 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15370 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15371 bios_lvds_use_ssc ? "en" : "dis",
15372 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15373 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15374 }
15375 }
15376
ad77c537 15377 /* maximum framebuffer dimensions */
5db94019 15378 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
15379 dev->mode_config.max_width = 2048;
15380 dev->mode_config.max_height = 2048;
5db94019 15381 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
15382 dev->mode_config.max_width = 4096;
15383 dev->mode_config.max_height = 4096;
79e53945 15384 } else {
a6c45cf0
CW
15385 dev->mode_config.max_width = 8192;
15386 dev->mode_config.max_height = 8192;
79e53945 15387 }
068be561 15388
2a307c2e
JN
15389 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
15390 dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
dc41c154 15391 dev->mode_config.cursor_height = 1023;
5db94019 15392 } else if (IS_GEN2(dev_priv)) {
98fac1d5
VS
15393 dev->mode_config.cursor_width = 64;
15394 dev->mode_config.cursor_height = 64;
068be561 15395 } else {
98fac1d5
VS
15396 dev->mode_config.cursor_width = 256;
15397 dev->mode_config.cursor_height = 256;
068be561
DL
15398 }
15399
73ebd503 15400 dev->mode_config.fb_base = ggtt->gmadr.start;
79e53945 15401
28c97730 15402 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
15403 INTEL_INFO(dev_priv)->num_pipes,
15404 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 15405
055e393f 15406 for_each_pipe(dev_priv, pipe) {
5ab0d85b 15407 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
15408 if (ret) {
15409 drm_mode_config_cleanup(dev);
15410 return ret;
15411 }
79e53945
JB
15412 }
15413
e72f9fbf 15414 intel_shared_dpll_init(dev);
58ecd9d5 15415 intel_update_fdi_pll_freq(dev_priv);
ee7b9f93 15416
5be6e334
VS
15417 intel_update_czclk(dev_priv);
15418 intel_modeset_init_hw(dev);
15419
b2045352 15420 if (dev_priv->max_cdclk_freq == 0)
4c75b940 15421 intel_update_max_cdclk(dev_priv);
b2045352 15422
9cce37f4 15423 /* Just disable it once at startup */
29b74b7f 15424 i915_disable_vga(dev_priv);
c39055b0 15425 intel_setup_outputs(dev_priv);
11be49eb 15426
6e9f798d 15427 drm_modeset_lock_all(dev);
aecd36b8 15428 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 15429 drm_modeset_unlock_all(dev);
46f297fb 15430
d3fcc808 15431 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15432 struct intel_initial_plane_config plane_config = {};
15433
46f297fb
JB
15434 if (!crtc->active)
15435 continue;
15436
46f297fb 15437 /*
46f297fb
JB
15438 * Note that reserving the BIOS fb up front prevents us
15439 * from stuffing other stolen allocations like the ring
15440 * on top. This prevents some ugliness at boot time, and
15441 * can even allow for smooth boot transitions if the BIOS
15442 * fb is large enough for the active pipe configuration.
15443 */
eeebeac5
ML
15444 dev_priv->display.get_initial_plane_config(crtc,
15445 &plane_config);
15446
15447 /*
15448 * If the fb is shared between multiple heads, we'll
15449 * just get the first one.
15450 */
15451 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15452 }
d93c0372
MR
15453
15454 /*
15455 * Make sure hardware watermarks really match the state we read out.
15456 * Note that we need to do this after reconstructing the BIOS fb's
15457 * since the watermark calculation done here will use pstate->fb.
15458 */
602ae835
VS
15459 if (!HAS_GMCH_DISPLAY(dev_priv))
15460 sanitize_watermarks(dev);
b079bd17 15461
516a49cc
AS
15462 /*
15463 * Force all active planes to recompute their states. So that on
15464 * mode_setcrtc after probe, all the intel_plane_state variables
15465 * are already calculated and there is no assert_plane warnings
15466 * during bootup.
15467 */
15468 ret = intel_initial_commit(dev);
15469 if (ret)
15470 DRM_DEBUG_KMS("Initial commit in probe failed.\n");
15471
b079bd17 15472 return 0;
2c7111db
CW
15473}
15474
2ee0da16
VS
15475void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15476{
d5fb43cb 15477 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
2ee0da16
VS
15478 /* 640x480@60Hz, ~25175 kHz */
15479 struct dpll clock = {
15480 .m1 = 18,
15481 .m2 = 7,
15482 .p1 = 13,
15483 .p2 = 4,
15484 .n = 2,
15485 };
15486 u32 dpll, fp;
15487 int i;
15488
15489 WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
15490
15491 DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
15492 pipe_name(pipe), clock.vco, clock.dot);
15493
15494 fp = i9xx_dpll_compute_fp(&clock);
15495 dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
15496 DPLL_VGA_MODE_DIS |
15497 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
15498 PLL_P2_DIVIDE_BY_4 |
15499 PLL_REF_INPUT_DREFCLK |
15500 DPLL_VCO_ENABLE;
15501
15502 I915_WRITE(FP0(pipe), fp);
15503 I915_WRITE(FP1(pipe), fp);
15504
15505 I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
15506 I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
15507 I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
15508 I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
15509 I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
15510 I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
15511 I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
15512
15513 /*
15514 * Apparently we need to have VGA mode enabled prior to changing
15515 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
15516 * dividers, even though the register value does change.
15517 */
15518 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
15519 I915_WRITE(DPLL(pipe), dpll);
15520
15521 /* Wait for the clocks to stabilize. */
15522 POSTING_READ(DPLL(pipe));
15523 udelay(150);
15524
15525 /* The pixel multiplier can only be updated once the
15526 * DPLL is enabled and the clocks are stable.
15527 *
15528 * So write it again.
15529 */
15530 I915_WRITE(DPLL(pipe), dpll);
15531
15532 /* We do this three times for luck */
15533 for (i = 0; i < 3 ; i++) {
15534 I915_WRITE(DPLL(pipe), dpll);
15535 POSTING_READ(DPLL(pipe));
15536 udelay(150); /* wait for warmup */
15537 }
15538
15539 I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
15540 POSTING_READ(PIPECONF(pipe));
d5fb43cb
VS
15541
15542 intel_wait_for_pipe_scanline_moving(crtc);
2ee0da16
VS
15543}
15544
15545void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
15546{
8fedd64d
VS
15547 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15548
2ee0da16
VS
15549 DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
15550 pipe_name(pipe));
15551
5816d9cb
VS
15552 WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
15553 WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
15554 WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
b99b9ec1
VS
15555 WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
15556 WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
2ee0da16
VS
15557
15558 I915_WRITE(PIPECONF(pipe), 0);
15559 POSTING_READ(PIPECONF(pipe));
15560
8fedd64d 15561 intel_wait_for_pipe_scanline_stopped(crtc);
2ee0da16
VS
15562
15563 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
15564 POSTING_READ(DPLL(pipe));
15565}
15566
b1e01595
VS
15567static void
15568intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
15569{
15570 struct intel_crtc *crtc;
fa555837 15571
b1e01595
VS
15572 if (INTEL_GEN(dev_priv) >= 4)
15573 return;
fa555837 15574
b1e01595
VS
15575 for_each_intel_crtc(&dev_priv->drm, crtc) {
15576 struct intel_plane *plane =
15577 to_intel_plane(crtc->base.primary);
9b273901
VS
15578 struct intel_crtc *plane_crtc;
15579 enum pipe pipe;
b1e01595 15580
9b273901
VS
15581 if (!plane->get_hw_state(plane, &pipe))
15582 continue;
15583
15584 if (pipe == crtc->pipe)
b1e01595
VS
15585 continue;
15586
15587 DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
15588 plane->base.name);
9b273901
VS
15589
15590 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15591 intel_plane_disable_noatomic(plane_crtc, plane);
b1e01595 15592 }
fa555837
SV
15593}
15594
02e93c35
VS
15595static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15596{
15597 struct drm_device *dev = crtc->base.dev;
15598 struct intel_encoder *encoder;
15599
15600 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15601 return true;
15602
15603 return false;
15604}
15605
496b0fc3
ML
15606static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
15607{
15608 struct drm_device *dev = encoder->base.dev;
15609 struct intel_connector *connector;
15610
15611 for_each_connector_on_encoder(dev, &encoder->base, connector)
15612 return connector;
15613
15614 return NULL;
15615}
15616
a168f5b3 15617static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
ecf837d9 15618 enum pipe pch_transcoder)
a168f5b3
VS
15619{
15620 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
ecf837d9 15621 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
a168f5b3
VS
15622}
15623
aecd36b8
VS
15624static void intel_sanitize_crtc(struct intel_crtc *crtc,
15625 struct drm_modeset_acquire_ctx *ctx)
24929352
SV
15626{
15627 struct drm_device *dev = crtc->base.dev;
fac5e23e 15628 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 15629 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 15630
24929352 15631 /* Clear any frame start delays used for debugging left by the BIOS */
738a8143 15632 if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
4d1de975
JN
15633 i915_reg_t reg = PIPECONF(cpu_transcoder);
15634
15635 I915_WRITE(reg,
15636 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15637 }
24929352 15638
d297e103 15639 if (crtc->active) {
f9cd7b88
VS
15640 struct intel_plane *plane;
15641
f9cd7b88
VS
15642 /* Disable everything but the primary plane */
15643 for_each_intel_plane_on_crtc(dev, crtc, plane) {
b1e01595
VS
15644 const struct intel_plane_state *plane_state =
15645 to_intel_plane_state(plane->base.state);
f9cd7b88 15646
b1e01595
VS
15647 if (plane_state->base.visible &&
15648 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
15649 intel_plane_disable_noatomic(crtc, plane);
f9cd7b88 15650 }
9625604c 15651 }
d3eaf884 15652
24929352
SV
15653 /* Adjust the state of the output pipe according to whether we
15654 * have active connectors/encoders. */
842e0307 15655 if (crtc->active && !intel_crtc_has_encoders(crtc))
da1d0e26 15656 intel_crtc_disable_noatomic(&crtc->base, ctx);
24929352 15657
49cff963 15658 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
SV
15659 /*
15660 * We start out with underrun reporting disabled to avoid races.
15661 * For correct bookkeeping mark this on active crtcs.
15662 *
c5ab3bc0
SV
15663 * Also on gmch platforms we dont have any hardware bits to
15664 * disable the underrun reporting. Which means we need to start
15665 * out with underrun reporting disabled also on inactive pipes,
15666 * since otherwise we'll complain about the garbage we read when
15667 * e.g. coming up after runtime pm.
15668 *
4cc31489
SV
15669 * No protection against concurrent access is required - at
15670 * worst a fifo underrun happens which also sets this to false.
15671 */
15672 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
15673 /*
15674 * We track the PCH trancoder underrun reporting state
15675 * within the crtc. With crtc for pipe A housing the underrun
15676 * reporting state for PCH transcoder A, crtc for pipe B housing
15677 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
15678 * and marking underrun reporting as disabled for the non-existing
15679 * PCH transcoders B and C would prevent enabling the south
15680 * error interrupt (see cpt_can_enable_serr_int()).
15681 */
ecf837d9 15682 if (has_pch_trancoder(dev_priv, crtc->pipe))
a168f5b3 15683 crtc->pch_fifo_underrun_disabled = true;
4cc31489 15684 }
24929352
SV
15685}
15686
15687static void intel_sanitize_encoder(struct intel_encoder *encoder)
15688{
15689 struct intel_connector *connector;
24929352
SV
15690
15691 /* We need to check both for a crtc link (meaning that the
15692 * encoder is active and trying to read from a pipe) and the
15693 * pipe itself being active. */
15694 bool has_active_crtc = encoder->base.crtc &&
15695 to_intel_crtc(encoder->base.crtc)->active;
15696
496b0fc3
ML
15697 connector = intel_encoder_find_connector(encoder);
15698 if (connector && !has_active_crtc) {
24929352
SV
15699 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15700 encoder->base.base.id,
8e329a03 15701 encoder->base.name);
24929352
SV
15702
15703 /* Connector is active, but has no active pipe. This is
15704 * fallout from our resume register restoring. Disable
15705 * the encoder manually again. */
15706 if (encoder->base.crtc) {
fd6bbda9
ML
15707 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
15708
24929352
SV
15709 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15710 encoder->base.base.id,
8e329a03 15711 encoder->base.name);
fd6bbda9 15712 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 15713 if (encoder->post_disable)
fd6bbda9 15714 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 15715 }
7f1950fb 15716 encoder->base.crtc = NULL;
24929352
SV
15717
15718 /* Inconsistent output/port/pipe state happens presumably due to
15719 * a bug in one of the get_hw_state functions. Or someplace else
15720 * in our code, like the register restore mess on resume. Clamp
15721 * things to off as a safer default. */
fd6bbda9
ML
15722
15723 connector->base.dpms = DRM_MODE_DPMS_OFF;
15724 connector->base.encoder = NULL;
24929352 15725 }
d6cae4aa
ML
15726
15727 /* notify opregion of the sanitized encoder state */
15728 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
24929352
SV
15729}
15730
29b74b7f 15731void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 15732{
920a14b2 15733 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 15734
04098753
ID
15735 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15736 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 15737 i915_disable_vga(dev_priv);
04098753
ID
15738 }
15739}
15740
29b74b7f 15741void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 15742{
8dc8a27c
PZ
15743 /* This function can be called both from intel_modeset_setup_hw_state or
15744 * at a very early point in our resume sequence, where the power well
15745 * structures are not yet restored. Since this function is at a very
15746 * paranoid "someone might have enabled VGA while we were not looking"
15747 * level, just check if the power well is enabled instead of trying to
15748 * follow the "don't touch the power well if we don't need it" policy
15749 * the rest of the driver uses. */
6392f847 15750 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15751 return;
15752
29b74b7f 15753 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
15754
15755 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15756}
15757
f9cd7b88 15758/* FIXME read out full plane state for all planes */
9b273901 15759static void readout_plane_state(struct drm_i915_private *dev_priv)
d032ffa0 15760{
b1e01595 15761 struct intel_plane *plane;
9b273901 15762 struct intel_crtc *crtc;
d032ffa0 15763
9b273901 15764 for_each_intel_plane(&dev_priv->drm, plane) {
b1e01595
VS
15765 struct intel_plane_state *plane_state =
15766 to_intel_plane_state(plane->base.state);
9b273901
VS
15767 struct intel_crtc_state *crtc_state;
15768 enum pipe pipe = PIPE_A;
eade6c89
VS
15769 bool visible;
15770
15771 visible = plane->get_hw_state(plane, &pipe);
b26d3ea3 15772
9b273901
VS
15773 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15774 crtc_state = to_intel_crtc_state(crtc->base.state);
15775
b1e01595
VS
15776 intel_set_plane_visible(crtc_state, plane_state, visible);
15777 }
9b273901
VS
15778
15779 for_each_intel_crtc(&dev_priv->drm, crtc) {
15780 struct intel_crtc_state *crtc_state =
15781 to_intel_crtc_state(crtc->base.state);
15782
15783 fixup_active_planes(crtc_state);
15784 }
98ec7739
VS
15785}
15786
30e984df 15787static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 15788{
fac5e23e 15789 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 15790 enum pipe pipe;
24929352
SV
15791 struct intel_crtc *crtc;
15792 struct intel_encoder *encoder;
15793 struct intel_connector *connector;
f9e905ca 15794 struct drm_connector_list_iter conn_iter;
5358901f 15795 int i;
24929352 15796
565602d7
ML
15797 dev_priv->active_crtcs = 0;
15798
d3fcc808 15799 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15800 struct intel_crtc_state *crtc_state =
15801 to_intel_crtc_state(crtc->base.state);
3b117c8f 15802
ec2dc6a0 15803 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
15804 memset(crtc_state, 0, sizeof(*crtc_state));
15805 crtc_state->base.crtc = &crtc->base;
24929352 15806
565602d7
ML
15807 crtc_state->base.active = crtc_state->base.enable =
15808 dev_priv->display.get_pipe_config(crtc, crtc_state);
15809
15810 crtc->base.enabled = crtc_state->base.enable;
15811 crtc->active = crtc_state->base.active;
15812
aca1ebf4 15813 if (crtc_state->base.active)
565602d7
ML
15814 dev_priv->active_crtcs |= 1 << crtc->pipe;
15815
78108b7c
VS
15816 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15817 crtc->base.base.id, crtc->base.name,
a8cd6da0 15818 enableddisabled(crtc_state->base.active));
24929352
SV
15819 }
15820
9b273901
VS
15821 readout_plane_state(dev_priv);
15822
5358901f
SV
15823 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15824 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15825
ee1398ba
LDM
15826 pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
15827 &pll->state.hw_state);
2c42e535 15828 pll->state.crtc_mask = 0;
d3fcc808 15829 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15830 struct intel_crtc_state *crtc_state =
15831 to_intel_crtc_state(crtc->base.state);
15832
15833 if (crtc_state->base.active &&
15834 crtc_state->shared_dpll == pll)
2c42e535 15835 pll->state.crtc_mask |= 1 << crtc->pipe;
5358901f 15836 }
2c42e535 15837 pll->active_mask = pll->state.crtc_mask;
5358901f 15838
1e6f2ddc 15839 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
72f775fa 15840 pll->info->name, pll->state.crtc_mask, pll->on);
5358901f
SV
15841 }
15842
b2784e15 15843 for_each_intel_encoder(dev, encoder) {
24929352
SV
15844 pipe = 0;
15845
15846 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
15847 struct intel_crtc_state *crtc_state;
15848
98187836 15849 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 15850 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 15851
045ac3b5 15852 encoder->base.crtc = &crtc->base;
a8cd6da0 15853 encoder->get_config(encoder, crtc_state);
24929352
SV
15854 } else {
15855 encoder->base.crtc = NULL;
15856 }
15857
6f2bcceb 15858 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
15859 encoder->base.base.id, encoder->base.name,
15860 enableddisabled(encoder->base.crtc),
6f2bcceb 15861 pipe_name(pipe));
24929352
SV
15862 }
15863
f9e905ca
SV
15864 drm_connector_list_iter_begin(dev, &conn_iter);
15865 for_each_intel_connector_iter(connector, &conn_iter) {
24929352
SV
15866 if (connector->get_hw_state(connector)) {
15867 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15868
15869 encoder = connector->encoder;
15870 connector->base.encoder = &encoder->base;
15871
15872 if (encoder->base.crtc &&
15873 encoder->base.crtc->state->active) {
15874 /*
15875 * This has to be done during hardware readout
15876 * because anything calling .crtc_disable may
15877 * rely on the connector_mask being accurate.
15878 */
15879 encoder->base.crtc->state->connector_mask |=
40560e26 15880 drm_connector_mask(&connector->base);
e87a52b3 15881 encoder->base.crtc->state->encoder_mask |=
40560e26 15882 drm_encoder_mask(&encoder->base);
2aa974c9
ML
15883 }
15884
24929352
SV
15885 } else {
15886 connector->base.dpms = DRM_MODE_DPMS_OFF;
15887 connector->base.encoder = NULL;
15888 }
15889 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
15890 connector->base.base.id, connector->base.name,
15891 enableddisabled(connector->base.encoder));
24929352 15892 }
f9e905ca 15893 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
15894
15895 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
15896 struct intel_crtc_state *crtc_state =
15897 to_intel_crtc_state(crtc->base.state);
d305e061 15898 int min_cdclk = 0;
aca1ebf4 15899
7f4c6284 15900 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
a8cd6da0
VS
15901 if (crtc_state->base.active) {
15902 intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
bd4cd03c
VS
15903 crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
15904 crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
a8cd6da0 15905 intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
7f4c6284
VS
15906 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15907
15908 /*
15909 * The initial mode needs to be set in order to keep
15910 * the atomic core happy. It wants a valid mode if the
15911 * crtc's enabled, so we do the above call.
15912 *
7800fb69
SV
15913 * But we don't set all the derived state fully, hence
15914 * set a flag to indicate that a full recalculation is
15915 * needed on the next commit.
7f4c6284 15916 */
a8cd6da0 15917 crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832 15918
a7d1b3f4
VS
15919 intel_crtc_compute_pixel_rate(crtc_state);
15920
9c61de4c 15921 if (dev_priv->display.modeset_calc_cdclk) {
d305e061 15922 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
9c61de4c
VS
15923 if (WARN_ON(min_cdclk < 0))
15924 min_cdclk = 0;
15925 }
aca1ebf4 15926
5caa0fea
SV
15927 drm_calc_timestamping_constants(&crtc->base,
15928 &crtc_state->base.adjusted_mode);
9eca6832 15929 update_scanline_offset(crtc);
7f4c6284 15930 }
e3b247da 15931
d305e061 15932 dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
53e9bf5e
VS
15933 dev_priv->min_voltage_level[crtc->pipe] =
15934 crtc_state->min_voltage_level;
aca1ebf4 15935
a8cd6da0 15936 intel_pipe_config_sanity_check(dev_priv, crtc_state);
7f4c6284 15937 }
30e984df
SV
15938}
15939
62b69566
ACO
15940static void
15941get_encoder_power_domains(struct drm_i915_private *dev_priv)
15942{
15943 struct intel_encoder *encoder;
15944
15945 for_each_intel_encoder(&dev_priv->drm, encoder) {
15946 u64 get_domains;
15947 enum intel_display_power_domain domain;
52528055 15948 struct intel_crtc_state *crtc_state;
62b69566
ACO
15949
15950 if (!encoder->get_power_domains)
15951 continue;
15952
52528055 15953 /*
b79ebe74
ID
15954 * MST-primary and inactive encoders don't have a crtc state
15955 * and neither of these require any power domain references.
52528055 15956 */
b79ebe74
ID
15957 if (!encoder->base.crtc)
15958 continue;
52528055 15959
b79ebe74 15960 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
52528055 15961 get_domains = encoder->get_power_domains(encoder, crtc_state);
62b69566
ACO
15962 for_each_power_domain(domain, get_domains)
15963 intel_display_power_get(dev_priv, domain);
15964 }
15965}
15966
df49ec82
RV
15967static void intel_early_display_was(struct drm_i915_private *dev_priv)
15968{
15969 /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
15970 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
15971 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
15972 DARBF_GATING_DIS);
15973
15974 if (IS_HASWELL(dev_priv)) {
15975 /*
15976 * WaRsPkgCStateDisplayPMReq:hsw
15977 * System hang if this isn't done before disabling all planes!
15978 */
15979 I915_WRITE(CHICKEN_PAR1_1,
15980 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
15981 }
15982}
15983
043e9bda
ML
15984/* Scan out the current hw modeset state,
15985 * and sanitizes it to the current state
15986 */
15987static void
aecd36b8
VS
15988intel_modeset_setup_hw_state(struct drm_device *dev,
15989 struct drm_modeset_acquire_ctx *ctx)
30e984df 15990{
fac5e23e 15991 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df
SV
15992 struct intel_crtc *crtc;
15993 struct intel_encoder *encoder;
35c95375 15994 int i;
30e984df 15995
2cd9a689
ID
15996 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
15997
df49ec82 15998 intel_early_display_was(dev_priv);
30e984df 15999 intel_modeset_readout_hw_state(dev);
24929352
SV
16000
16001 /* HW state is read out, now we need to sanitize this mess. */
62b69566
ACO
16002 get_encoder_power_domains(dev_priv);
16003
7cada4d0
VS
16004 /*
16005 * intel_sanitize_plane_mapping() may need to do vblank
16006 * waits, so we need vblank interrupts restored beforehand.
16007 */
16008 for_each_intel_crtc(&dev_priv->drm, crtc) {
16009 drm_crtc_vblank_reset(&crtc->base);
b1e01595 16010
7cada4d0
VS
16011 if (crtc->active)
16012 drm_crtc_vblank_on(&crtc->base);
24929352
SV
16013 }
16014
7cada4d0 16015 intel_sanitize_plane_mapping(dev_priv);
e2af48c6 16016
7cada4d0
VS
16017 for_each_intel_encoder(dev, encoder)
16018 intel_sanitize_encoder(encoder);
16019
16020 for_each_intel_crtc(&dev_priv->drm, crtc) {
aecd36b8 16021 intel_sanitize_crtc(crtc, ctx);
6e3c9717
ACO
16022 intel_dump_pipe_config(crtc, crtc->config,
16023 "[setup_hw_state]");
24929352 16024 }
9a935856 16025
d29b2f9d
ACO
16026 intel_modeset_update_connector_atomic_state(dev);
16027
35c95375
SV
16028 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16029 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16030
2dd66ebd 16031 if (!pll->on || pll->active_mask)
35c95375
SV
16032 continue;
16033
72f775fa
LDM
16034 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
16035 pll->info->name);
35c95375 16036
ee1398ba 16037 pll->info->funcs->disable(dev_priv, pll);
35c95375
SV
16038 pll->on = false;
16039 }
16040
04548cba
VS
16041 if (IS_G4X(dev_priv)) {
16042 g4x_wm_get_hw_state(dev);
16043 g4x_wm_sanitize(dev_priv);
16044 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
6eb1a681 16045 vlv_wm_get_hw_state(dev);
602ae835 16046 vlv_wm_sanitize(dev_priv);
a029fa4d 16047 } else if (INTEL_GEN(dev_priv) >= 9) {
3078999f 16048 skl_wm_get_hw_state(dev);
602ae835 16049 } else if (HAS_PCH_SPLIT(dev_priv)) {
243e6a44 16050 ilk_wm_get_hw_state(dev);
602ae835 16051 }
292b990e
ML
16052
16053 for_each_intel_crtc(dev, crtc) {
d8fc70b7 16054 u64 put_domains;
292b990e 16055
74bff5f9 16056 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
16057 if (WARN_ON(put_domains))
16058 modeset_put_power_domains(dev_priv, put_domains);
16059 }
010cf73d 16060
2cd9a689 16061 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
8d8c386c 16062
010cf73d 16063 intel_fbc_init_pipe_state(dev_priv);
043e9bda 16064}
7d0bc1ea 16065
043e9bda
ML
16066void intel_display_resume(struct drm_device *dev)
16067{
e2c8b870
ML
16068 struct drm_i915_private *dev_priv = to_i915(dev);
16069 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16070 struct drm_modeset_acquire_ctx ctx;
043e9bda 16071 int ret;
f30da187 16072
e2c8b870 16073 dev_priv->modeset_restore_state = NULL;
73974893
ML
16074 if (state)
16075 state->acquire_ctx = &ctx;
043e9bda 16076
e2c8b870 16077 drm_modeset_acquire_init(&ctx, 0);
043e9bda 16078
73974893
ML
16079 while (1) {
16080 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16081 if (ret != -EDEADLK)
16082 break;
043e9bda 16083
e2c8b870 16084 drm_modeset_backoff(&ctx);
e2c8b870 16085 }
043e9bda 16086
73974893 16087 if (!ret)
581e49fe 16088 ret = __intel_display_resume(dev, state, &ctx);
73974893 16089
2503a0fe 16090 intel_enable_ipc(dev_priv);
e2c8b870
ML
16091 drm_modeset_drop_locks(&ctx);
16092 drm_modeset_acquire_fini(&ctx);
043e9bda 16093
0853695c 16094 if (ret)
e2c8b870 16095 DRM_ERROR("Restoring old state failed with %i\n", ret);
3c5e37f1
CW
16096 if (state)
16097 drm_atomic_state_put(state);
2c7111db
CW
16098}
16099
1ebaa0b9
CW
16100int intel_connector_register(struct drm_connector *connector)
16101{
16102 struct intel_connector *intel_connector = to_intel_connector(connector);
16103 int ret;
16104
16105 ret = intel_backlight_device_register(intel_connector);
16106 if (ret)
16107 goto err;
16108
16109 return 0;
0962c3c9 16110
1ebaa0b9
CW
16111err:
16112 return ret;
79e53945
JB
16113}
16114
c191eca1 16115void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 16116{
e63d87c0 16117 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 16118
e63d87c0 16119 intel_backlight_device_unregister(intel_connector);
4932e2c3 16120 intel_panel_destroy_backlight(connector);
4932e2c3
ID
16121}
16122
886c6b86
MN
16123static void intel_hpd_poll_fini(struct drm_device *dev)
16124{
16125 struct intel_connector *connector;
16126 struct drm_connector_list_iter conn_iter;
16127
448aa911 16128 /* Kill all the work that may have been queued by hpd. */
886c6b86
MN
16129 drm_connector_list_iter_begin(dev, &conn_iter);
16130 for_each_intel_connector_iter(connector, &conn_iter) {
16131 if (connector->modeset_retry_work.func)
16132 cancel_work_sync(&connector->modeset_retry_work);
ee5e5e7a
SP
16133 if (connector->hdcp_shim) {
16134 cancel_delayed_work_sync(&connector->hdcp_check_work);
16135 cancel_work_sync(&connector->hdcp_prop_work);
16136 }
886c6b86
MN
16137 }
16138 drm_connector_list_iter_end(&conn_iter);
16139}
16140
79e53945
JB
16141void intel_modeset_cleanup(struct drm_device *dev)
16142{
fac5e23e 16143 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 16144
8bcf9f70
CW
16145 flush_workqueue(dev_priv->modeset_wq);
16146
eb955eee
CW
16147 flush_work(&dev_priv->atomic_helper.free_work);
16148 WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
16149
fd0c0642
SV
16150 /*
16151 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 16152 * Too much stuff here (turning of connectors, ...) would
fd0c0642
SV
16153 * experience fancy races otherwise.
16154 */
2aeb7d3a 16155 intel_irq_uninstall(dev_priv);
eb21b92b 16156
fd0c0642
SV
16157 /*
16158 * Due to the hpd irq storm handling the hotplug work can re-arm the
16159 * poll handlers. Hence disable polling after hpd handling is shut down.
16160 */
886c6b86 16161 intel_hpd_poll_fini(dev);
fd0c0642 16162
4f256d82
SV
16163 /* poll work can call into fbdev, hence clean that up afterwards */
16164 intel_fbdev_fini(dev_priv);
16165
723bfd70
JB
16166 intel_unregister_dsm_handler();
16167
c937ab3e 16168 intel_fbc_global_disable(dev_priv);
69341a5e 16169
1630fe75
CW
16170 /* flush any delayed tasks or pending work */
16171 flush_scheduled_work();
16172
79e53945 16173 drm_mode_config_cleanup(dev);
4d7bb011 16174
1ee8da6d 16175 intel_cleanup_overlay(dev_priv);
ae48434c 16176
40196446 16177 intel_teardown_gmbus(dev_priv);
757fffcf
VS
16178
16179 destroy_workqueue(dev_priv->modeset_wq);
79e53945
JB
16180}
16181
df0e9248
CW
16182void intel_connector_attach_encoder(struct intel_connector *connector,
16183 struct intel_encoder *encoder)
16184{
16185 connector->encoder = encoder;
cde4c44d 16186 drm_connector_attach_encoder(&connector->base, &encoder->base);
79e53945 16187}
28d52043
DA
16188
16189/*
16190 * set vga decode state - true == enable VGA decode
16191 */
6315b5d3 16192int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 16193{
6315b5d3 16194 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16195 u16 gmch_ctrl;
16196
75fa041d
CW
16197 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16198 DRM_ERROR("failed to read control word\n");
16199 return -EIO;
16200 }
16201
c0cc8a55
CW
16202 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16203 return 0;
16204
28d52043
DA
16205 if (state)
16206 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16207 else
16208 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16209
16210 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16211 DRM_ERROR("failed to write control word\n");
16212 return -EIO;
16213 }
16214
28d52043
DA
16215 return 0;
16216}
c4a1d9e4 16217
98a2f411
CW
16218#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
16219
c4a1d9e4 16220struct intel_display_error_state {
ff57f1b0
PZ
16221
16222 u32 power_well_driver;
16223
63b66e5b
CW
16224 int num_transcoders;
16225
c4a1d9e4
CW
16226 struct intel_cursor_error_state {
16227 u32 control;
16228 u32 position;
16229 u32 base;
16230 u32 size;
52331309 16231 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16232
16233 struct intel_pipe_error_state {
ddf9c536 16234 bool power_domain_on;
c4a1d9e4 16235 u32 source;
f301b1e1 16236 u32 stat;
52331309 16237 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16238
16239 struct intel_plane_error_state {
16240 u32 control;
16241 u32 stride;
16242 u32 size;
16243 u32 pos;
16244 u32 addr;
16245 u32 surface;
16246 u32 tile_offset;
52331309 16247 } plane[I915_MAX_PIPES];
63b66e5b
CW
16248
16249 struct intel_transcoder_error_state {
ddf9c536 16250 bool power_domain_on;
63b66e5b
CW
16251 enum transcoder cpu_transcoder;
16252
16253 u32 conf;
16254
16255 u32 htotal;
16256 u32 hblank;
16257 u32 hsync;
16258 u32 vtotal;
16259 u32 vblank;
16260 u32 vsync;
16261 } transcoder[4];
c4a1d9e4
CW
16262};
16263
16264struct intel_display_error_state *
c033666a 16265intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 16266{
c4a1d9e4 16267 struct intel_display_error_state *error;
63b66e5b
CW
16268 int transcoders[] = {
16269 TRANSCODER_A,
16270 TRANSCODER_B,
16271 TRANSCODER_C,
16272 TRANSCODER_EDP,
16273 };
c4a1d9e4
CW
16274 int i;
16275
c033666a 16276 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
16277 return NULL;
16278
9d1cb914 16279 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16280 if (error == NULL)
16281 return NULL;
16282
c033666a 16283 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
75e39688 16284 error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
ff57f1b0 16285
055e393f 16286 for_each_pipe(dev_priv, i) {
ddf9c536 16287 error->pipe[i].power_domain_on =
f458ebbc
SV
16288 __intel_display_power_is_enabled(dev_priv,
16289 POWER_DOMAIN_PIPE(i));
ddf9c536 16290 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16291 continue;
16292
5efb3e28
VS
16293 error->cursor[i].control = I915_READ(CURCNTR(i));
16294 error->cursor[i].position = I915_READ(CURPOS(i));
16295 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16296
16297 error->plane[i].control = I915_READ(DSPCNTR(i));
16298 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 16299 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 16300 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16301 error->plane[i].pos = I915_READ(DSPPOS(i));
16302 }
c033666a 16303 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 16304 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 16305 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
16306 error->plane[i].surface = I915_READ(DSPSURF(i));
16307 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16308 }
16309
c4a1d9e4 16310 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16311
c033666a 16312 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 16313 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16314 }
16315
4d1de975 16316 /* Note: this does not include DSI transcoders. */
c033666a 16317 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 16318 if (HAS_DDI(dev_priv))
63b66e5b
CW
16319 error->num_transcoders++; /* Account for eDP. */
16320
16321 for (i = 0; i < error->num_transcoders; i++) {
16322 enum transcoder cpu_transcoder = transcoders[i];
16323
ddf9c536 16324 error->transcoder[i].power_domain_on =
f458ebbc 16325 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16326 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16327 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16328 continue;
16329
63b66e5b
CW
16330 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16331
16332 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16333 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16334 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16335 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16336 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16337 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16338 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16339 }
16340
16341 return error;
16342}
16343
edc3d884
MK
16344#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16345
c4a1d9e4 16346void
edc3d884 16347intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16348 struct intel_display_error_state *error)
16349{
5a4c6f1b 16350 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
16351 int i;
16352
63b66e5b
CW
16353 if (!error)
16354 return;
16355
b7f05d4a 16356 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 16357 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 16358 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16359 error->power_well_driver);
055e393f 16360 for_each_pipe(dev_priv, i) {
edc3d884 16361 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16362 err_printf(m, " Power: %s\n",
87ad3212 16363 onoff(error->pipe[i].power_domain_on));
edc3d884 16364 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16365 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16366
16367 err_printf(m, "Plane [%d]:\n", i);
16368 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16369 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 16370 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
16371 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16372 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16373 }
772c2a51 16374 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 16375 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 16376 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
16377 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16378 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16379 }
16380
edc3d884
MK
16381 err_printf(m, "Cursor [%d]:\n", i);
16382 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16383 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16384 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16385 }
63b66e5b
CW
16386
16387 for (i = 0; i < error->num_transcoders; i++) {
da205630 16388 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 16389 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16390 err_printf(m, " Power: %s\n",
87ad3212 16391 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16392 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16393 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16394 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16395 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16396 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16397 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16398 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16399 }
c4a1d9e4 16400}
98a2f411
CW
16401
16402#endif
This page took 9.242338 seconds and 4 git commands to generate.