]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/i915/display/intel_display.c
drm/i915: Disable legacy cursor fastpath for bigjoiner
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / i915 / display / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
27#include <linux/i2c.h>
d0e93599
SR
28#include <linux/input.h>
29#include <linux/intel-iommu.h>
7662c8bd 30#include <linux/kernel.h>
d0e93599 31#include <linux/module.h>
52791eee 32#include <linux/dma-resv.h>
5a0e3ad6 33#include <linux/slab.h>
d0e93599 34
319c1d42 35#include <drm/drm_atomic.h>
c196e1d6 36#include <drm/drm_atomic_helper.h>
d0e93599 37#include <drm/drm_atomic_uapi.h>
093a3a30 38#include <drm/drm_damage_helper.h>
760285e7 39#include <drm/drm_dp_helper.h>
d0e93599
SR
40#include <drm/drm_edid.h>
41#include <drm/drm_fourcc.h>
465c120c 42#include <drm/drm_plane_helper.h>
fcd70cd3 43#include <drm/drm_probe_helper.h>
465c120c 44#include <drm/drm_rect.h>
d0e93599 45
379bc100
JN
46#include "display/intel_crt.h"
47#include "display/intel_ddi.h"
48#include "display/intel_dp.h"
6671c367 49#include "display/intel_dp_mst.h"
ddff9a60 50#include "display/intel_dpll_mgr.h"
379bc100
JN
51#include "display/intel_dsi.h"
52#include "display/intel_dvo.h"
53#include "display/intel_gmbus.h"
54#include "display/intel_hdmi.h"
55#include "display/intel_lvds.h"
56#include "display/intel_sdvo.h"
57#include "display/intel_tv.h"
58#include "display/intel_vdsc.h"
59
3e7abf81
AS
60#include "gt/intel_rps.h"
61
d0e93599 62#include "i915_drv.h"
d0e93599 63#include "i915_trace.h"
4e49d35c 64#include "intel_acpi.h"
12392a74 65#include "intel_atomic.h"
56dabc93 66#include "intel_atomic_plane.h"
c457d9cf 67#include "intel_bw.h"
e7674ef6 68#include "intel_cdclk.h"
1d455f8d 69#include "intel_color.h"
24d98a54 70#include "intel_csr.h"
1d455f8d 71#include "intel_display_types.h"
3c954c41 72#include "intel_dp_link_training.h"
98afa316 73#include "intel_fbc.h"
6dfccb95 74#include "intel_fbdev.h"
8834e365 75#include "intel_fifo_underrun.h"
d0e93599 76#include "intel_frontbuffer.h"
408bd917 77#include "intel_hdcp.h"
dbeb38d9 78#include "intel_hotplug.h"
05ca9306 79#include "intel_overlay.h"
c6a35b9c 80#include "intel_pipe_crc.h"
696173b0 81#include "intel_pm.h"
55367a27 82#include "intel_psr.h"
220b92bf 83#include "intel_quirks.h"
56c5098f 84#include "intel_sideband.h"
f9a79f9a 85#include "intel_sprite.h"
32691b58 86#include "intel_tc.h"
4fb87831 87#include "intel_vga.h"
79e53945 88
465c120c 89/* Primary plane formats for gen <= 3 */
ba3f4d0a 90static const u32 i8xx_primary_formats[] = {
67fe7dc5 91 DRM_FORMAT_C8,
465c120c 92 DRM_FORMAT_XRGB1555,
12fef149 93 DRM_FORMAT_RGB565,
67fe7dc5 94 DRM_FORMAT_XRGB8888,
465c120c
MR
95};
96
03b0ce95
VS
97/* Primary plane formats for ivb (no fp16 due to hw issue) */
98static const u32 ivb_primary_formats[] = {
99 DRM_FORMAT_C8,
100 DRM_FORMAT_RGB565,
101 DRM_FORMAT_XRGB8888,
102 DRM_FORMAT_XBGR8888,
103 DRM_FORMAT_XRGB2101010,
104 DRM_FORMAT_XBGR2101010,
105};
106
107/* Primary plane formats for gen >= 4, except ivb */
ba3f4d0a 108static const u32 i965_primary_formats[] = {
6c0fd451
DL
109 DRM_FORMAT_C8,
110 DRM_FORMAT_RGB565,
111 DRM_FORMAT_XRGB8888,
112 DRM_FORMAT_XBGR8888,
113 DRM_FORMAT_XRGB2101010,
114 DRM_FORMAT_XBGR2101010,
03b0ce95 115 DRM_FORMAT_XBGR16161616F,
6c0fd451
DL
116};
117
73263cb6
VS
118/* Primary plane formats for vlv/chv */
119static const u32 vlv_primary_formats[] = {
120 DRM_FORMAT_C8,
121 DRM_FORMAT_RGB565,
122 DRM_FORMAT_XRGB8888,
123 DRM_FORMAT_XBGR8888,
124 DRM_FORMAT_ARGB8888,
125 DRM_FORMAT_ABGR8888,
126 DRM_FORMAT_XRGB2101010,
127 DRM_FORMAT_XBGR2101010,
128 DRM_FORMAT_ARGB2101010,
129 DRM_FORMAT_ABGR2101010,
130 DRM_FORMAT_XBGR16161616F,
131};
132
ba3f4d0a 133static const u64 i9xx_format_modifiers[] = {
714244e2
BW
134 I915_FORMAT_MOD_X_TILED,
135 DRM_FORMAT_MOD_LINEAR,
136 DRM_FORMAT_MOD_INVALID
137};
138
3d7d6510 139/* Cursor formats */
ba3f4d0a 140static const u32 intel_cursor_formats[] = {
3d7d6510
MR
141 DRM_FORMAT_ARGB8888,
142};
143
ba3f4d0a 144static const u64 cursor_format_modifiers[] = {
714244e2
BW
145 DRM_FORMAT_MOD_LINEAR,
146 DRM_FORMAT_MOD_INVALID
147};
148
f1f644dc 149static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 150 struct intel_crtc_state *pipe_config);
9eae5e27
LDM
151static void ilk_pch_clock_get(struct intel_crtc *crtc,
152 struct intel_crtc_state *pipe_config);
f1f644dc 153
24dbf51a
CW
154static int intel_framebuffer_init(struct intel_framebuffer *ifb,
155 struct drm_i915_gem_object *obj,
156 struct drm_mode_fb_cmd2 *mode_cmd);
e7fc3f90 157static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
44fe7f35 158static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
4c354754
ML
159static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
160 const struct intel_link_m_n *m_n,
161 const struct intel_link_m_n *m2_n2);
fdf73510 162static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
9eae5e27 163static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
1e98f88c 164static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
9b11215e 165static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
d288f65f 166static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 167 const struct intel_crtc_state *pipe_config);
d288f65f 168static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 169 const struct intel_crtc_state *pipe_config);
f6df4d46 170static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
9eae5e27 171static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
aecd36b8
VS
172static void intel_modeset_setup_hw_state(struct drm_device *dev,
173 struct drm_modeset_acquire_ctx *ctx);
216383e9 174static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
e7457a9a 175
d4906093 176struct intel_limit {
4c5def93
ACO
177 struct {
178 int min, max;
179 } dot, vco, n, m, m1, m2, p, p1;
180
181 struct {
182 int dot_limit;
183 int p2_slow, p2_fast;
184 } p2;
d4906093 185};
79e53945 186
bfa7df01 187/* returns HPLL frequency in kHz */
49cd97a3 188int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
bfa7df01
VS
189{
190 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
191
192 /* Obtain SKU information */
bfa7df01
VS
193 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
194 CCK_FUSE_HPLL_FREQ_MASK;
bfa7df01
VS
195
196 return vco_freq[hpll_freq] * 1000;
197}
198
c30fec65
VS
199int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
200 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
201{
202 u32 val;
203 int divider;
204
bfa7df01 205 val = vlv_cck_read(dev_priv, reg);
bfa7df01
VS
206 divider = val & CCK_FREQUENCY_VALUES;
207
e57291c2
PB
208 drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
209 (divider << CCK_FREQUENCY_STATUS_SHIFT),
210 "%s change in progress\n", name);
bfa7df01 211
c30fec65
VS
212 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
213}
214
7ff89ca2
VS
215int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
216 const char *name, u32 reg)
c30fec65 217{
337fa6e0
CW
218 int hpll;
219
220 vlv_cck_get(dev_priv);
221
c30fec65 222 if (dev_priv->hpll_freq == 0)
49cd97a3 223 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
c30fec65 224
337fa6e0
CW
225 hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
226
227 vlv_cck_put(dev_priv);
228
229 return hpll;
bfa7df01
VS
230}
231
bfa7df01
VS
232static void intel_update_czclk(struct drm_i915_private *dev_priv)
233{
666a4537 234 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
235 return;
236
237 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
238 CCK_CZ_CLOCK_CONTROL);
239
cd49f818
WK
240 drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
241 dev_priv->czclk_freq);
bfa7df01
VS
242}
243
81b55ef1
JN
244/* units of 100MHz */
245static u32 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
246 const struct intel_crtc_state *pipe_config)
021357ac 247{
21a727b3
VS
248 if (HAS_DDI(dev_priv))
249 return pipe_config->port_clock; /* SPLL */
e3b247da 250 else
58ecd9d5 251 return dev_priv->fdi_pll_freq;
021357ac
CW
252}
253
1b6f4958 254static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 255 .dot = { .min = 25000, .max = 350000 },
9c333719 256 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 257 .n = { .min = 2, .max = 16 },
0206e353
AJ
258 .m = { .min = 96, .max = 140 },
259 .m1 = { .min = 18, .max = 26 },
260 .m2 = { .min = 6, .max = 16 },
261 .p = { .min = 4, .max = 128 },
262 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
263 .p2 = { .dot_limit = 165000,
264 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
265};
266
1b6f4958 267static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 268 .dot = { .min = 25000, .max = 350000 },
9c333719 269 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 270 .n = { .min = 2, .max = 16 },
5d536e28
DV
271 .m = { .min = 96, .max = 140 },
272 .m1 = { .min = 18, .max = 26 },
273 .m2 = { .min = 6, .max = 16 },
274 .p = { .min = 4, .max = 128 },
275 .p1 = { .min = 2, .max = 33 },
276 .p2 = { .dot_limit = 165000,
277 .p2_slow = 4, .p2_fast = 4 },
278};
279
1b6f4958 280static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 281 .dot = { .min = 25000, .max = 350000 },
9c333719 282 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 283 .n = { .min = 2, .max = 16 },
0206e353
AJ
284 .m = { .min = 96, .max = 140 },
285 .m1 = { .min = 18, .max = 26 },
286 .m2 = { .min = 6, .max = 16 },
287 .p = { .min = 4, .max = 128 },
288 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
289 .p2 = { .dot_limit = 165000,
290 .p2_slow = 14, .p2_fast = 7 },
e4b36699 291};
273e27ca 292
1b6f4958 293static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1400000, .max = 2800000 },
296 .n = { .min = 1, .max = 6 },
297 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
298 .m1 = { .min = 8, .max = 18 },
299 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
300 .p = { .min = 5, .max = 80 },
301 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
302 .p2 = { .dot_limit = 200000,
303 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
304};
305
1b6f4958 306static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
307 .dot = { .min = 20000, .max = 400000 },
308 .vco = { .min = 1400000, .max = 2800000 },
309 .n = { .min = 1, .max = 6 },
310 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
311 .m1 = { .min = 8, .max = 18 },
312 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
313 .p = { .min = 7, .max = 98 },
314 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
315 .p2 = { .dot_limit = 112000,
316 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
317};
318
273e27ca 319
1b6f4958 320static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
321 .dot = { .min = 25000, .max = 270000 },
322 .vco = { .min = 1750000, .max = 3500000},
323 .n = { .min = 1, .max = 4 },
324 .m = { .min = 104, .max = 138 },
325 .m1 = { .min = 17, .max = 23 },
326 .m2 = { .min = 5, .max = 11 },
327 .p = { .min = 10, .max = 30 },
328 .p1 = { .min = 1, .max = 3},
329 .p2 = { .dot_limit = 270000,
330 .p2_slow = 10,
331 .p2_fast = 10
044c7c41 332 },
e4b36699
KP
333};
334
1b6f4958 335static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
336 .dot = { .min = 22000, .max = 400000 },
337 .vco = { .min = 1750000, .max = 3500000},
338 .n = { .min = 1, .max = 4 },
339 .m = { .min = 104, .max = 138 },
340 .m1 = { .min = 16, .max = 23 },
341 .m2 = { .min = 5, .max = 11 },
342 .p = { .min = 5, .max = 80 },
343 .p1 = { .min = 1, .max = 8},
344 .p2 = { .dot_limit = 165000,
345 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
346};
347
1b6f4958 348static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
349 .dot = { .min = 20000, .max = 115000 },
350 .vco = { .min = 1750000, .max = 3500000 },
351 .n = { .min = 1, .max = 3 },
352 .m = { .min = 104, .max = 138 },
353 .m1 = { .min = 17, .max = 23 },
354 .m2 = { .min = 5, .max = 11 },
355 .p = { .min = 28, .max = 112 },
356 .p1 = { .min = 2, .max = 8 },
357 .p2 = { .dot_limit = 0,
358 .p2_slow = 14, .p2_fast = 14
044c7c41 359 },
e4b36699
KP
360};
361
1b6f4958 362static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
363 .dot = { .min = 80000, .max = 224000 },
364 .vco = { .min = 1750000, .max = 3500000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 104, .max = 138 },
367 .m1 = { .min = 17, .max = 23 },
368 .m2 = { .min = 5, .max = 11 },
369 .p = { .min = 14, .max = 42 },
370 .p1 = { .min = 2, .max = 6 },
371 .p2 = { .dot_limit = 0,
372 .p2_slow = 7, .p2_fast = 7
044c7c41 373 },
e4b36699
KP
374};
375
1d218220 376static const struct intel_limit pnv_limits_sdvo = {
0206e353
AJ
377 .dot = { .min = 20000, .max = 400000},
378 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 379 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
380 .n = { .min = 3, .max = 6 },
381 .m = { .min = 2, .max = 256 },
273e27ca 382 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
383 .m1 = { .min = 0, .max = 0 },
384 .m2 = { .min = 0, .max = 254 },
385 .p = { .min = 5, .max = 80 },
386 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
387 .p2 = { .dot_limit = 200000,
388 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
389};
390
1d218220 391static const struct intel_limit pnv_limits_lvds = {
0206e353
AJ
392 .dot = { .min = 20000, .max = 400000 },
393 .vco = { .min = 1700000, .max = 3500000 },
394 .n = { .min = 3, .max = 6 },
395 .m = { .min = 2, .max = 256 },
396 .m1 = { .min = 0, .max = 0 },
397 .m2 = { .min = 0, .max = 254 },
398 .p = { .min = 7, .max = 112 },
399 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
400 .p2 = { .dot_limit = 112000,
401 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
402};
403
273e27ca
EA
404/* Ironlake / Sandybridge
405 *
406 * We calculate clock using (register_value + 2) for N/M1/M2, so here
407 * the range value for them is (actual_value - 2).
408 */
9eae5e27 409static const struct intel_limit ilk_limits_dac = {
273e27ca
EA
410 .dot = { .min = 25000, .max = 350000 },
411 .vco = { .min = 1760000, .max = 3510000 },
412 .n = { .min = 1, .max = 5 },
413 .m = { .min = 79, .max = 127 },
414 .m1 = { .min = 12, .max = 22 },
415 .m2 = { .min = 5, .max = 9 },
416 .p = { .min = 5, .max = 80 },
417 .p1 = { .min = 1, .max = 8 },
418 .p2 = { .dot_limit = 225000,
419 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
420};
421
9eae5e27 422static const struct intel_limit ilk_limits_single_lvds = {
273e27ca
EA
423 .dot = { .min = 25000, .max = 350000 },
424 .vco = { .min = 1760000, .max = 3510000 },
425 .n = { .min = 1, .max = 3 },
426 .m = { .min = 79, .max = 118 },
427 .m1 = { .min = 12, .max = 22 },
428 .m2 = { .min = 5, .max = 9 },
429 .p = { .min = 28, .max = 112 },
430 .p1 = { .min = 2, .max = 8 },
431 .p2 = { .dot_limit = 225000,
432 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
433};
434
9eae5e27 435static const struct intel_limit ilk_limits_dual_lvds = {
273e27ca
EA
436 .dot = { .min = 25000, .max = 350000 },
437 .vco = { .min = 1760000, .max = 3510000 },
438 .n = { .min = 1, .max = 3 },
439 .m = { .min = 79, .max = 127 },
440 .m1 = { .min = 12, .max = 22 },
441 .m2 = { .min = 5, .max = 9 },
442 .p = { .min = 14, .max = 56 },
443 .p1 = { .min = 2, .max = 8 },
444 .p2 = { .dot_limit = 225000,
445 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
446};
447
273e27ca 448/* LVDS 100mhz refclk limits. */
9eae5e27 449static const struct intel_limit ilk_limits_single_lvds_100m = {
273e27ca
EA
450 .dot = { .min = 25000, .max = 350000 },
451 .vco = { .min = 1760000, .max = 3510000 },
452 .n = { .min = 1, .max = 2 },
453 .m = { .min = 79, .max = 126 },
454 .m1 = { .min = 12, .max = 22 },
455 .m2 = { .min = 5, .max = 9 },
456 .p = { .min = 28, .max = 112 },
0206e353 457 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
458 .p2 = { .dot_limit = 225000,
459 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
460};
461
9eae5e27 462static const struct intel_limit ilk_limits_dual_lvds_100m = {
273e27ca
EA
463 .dot = { .min = 25000, .max = 350000 },
464 .vco = { .min = 1760000, .max = 3510000 },
465 .n = { .min = 1, .max = 3 },
466 .m = { .min = 79, .max = 126 },
467 .m1 = { .min = 12, .max = 22 },
468 .m2 = { .min = 5, .max = 9 },
469 .p = { .min = 14, .max = 42 },
0206e353 470 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
471 .p2 = { .dot_limit = 225000,
472 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
473};
474
1b6f4958 475static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
476 /*
477 * These are the data rate limits (measured in fast clocks)
478 * since those are the strictest limits we have. The fast
479 * clock and actual rate limits are more relaxed, so checking
480 * them would make no difference.
481 */
482 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 483 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 484 .n = { .min = 1, .max = 7 },
a0c4da24
JB
485 .m1 = { .min = 2, .max = 3 },
486 .m2 = { .min = 11, .max = 156 },
b99ab663 487 .p1 = { .min = 2, .max = 3 },
5fdc9c49 488 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
489};
490
1b6f4958 491static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
492 /*
493 * These are the data rate limits (measured in fast clocks)
494 * since those are the strictest limits we have. The fast
495 * clock and actual rate limits are more relaxed, so checking
496 * them would make no difference.
497 */
498 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 499 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
500 .n = { .min = 1, .max = 1 },
501 .m1 = { .min = 2, .max = 2 },
502 .m2 = { .min = 24 << 22, .max = 175 << 22 },
503 .p1 = { .min = 2, .max = 4 },
504 .p2 = { .p2_slow = 1, .p2_fast = 14 },
505};
506
1b6f4958 507static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
508 /* FIXME: find real dot limits */
509 .dot = { .min = 0, .max = INT_MAX },
e6292556 510 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
511 .n = { .min = 1, .max = 1 },
512 .m1 = { .min = 2, .max = 2 },
513 /* FIXME: find real m2 limits */
514 .m2 = { .min = 2 << 22, .max = 255 << 22 },
515 .p1 = { .min = 2, .max = 4 },
516 .p2 = { .p2_slow = 1, .p2_fast = 20 },
517};
518
51eb1a1d 519/* WA Display #0827: Gen9:all */
c4a4efa9 520static void
d048a268 521skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
c4a4efa9 522{
c4a4efa9 523 if (enable)
dc008bf0
JN
524 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
525 intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
c4a4efa9 526 else
dc008bf0
JN
527 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
528 intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
c4a4efa9
VS
529}
530
f96198ab 531/* Wa_2006604312:icl,ehl */
51eb1a1d
RS
532static void
533icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
534 bool enable)
535{
536 if (enable)
dc008bf0
JN
537 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
538 intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
51eb1a1d 539 else
dc008bf0
JN
540 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
541 intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
51eb1a1d
RS
542}
543
cdba954e 544static bool
69f786ae 545needs_modeset(const struct intel_crtc_state *state)
cdba954e 546{
2225f3c6 547 return drm_atomic_crtc_needs_modeset(&state->uapi);
cdba954e
ACO
548}
549
d82a855a
VS
550static bool
551is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
bfb926e3 552{
d82a855a 553 return crtc_state->master_transcoder != INVALID_TRANSCODER;
bfb926e3
MN
554}
555
ad457191 556static bool
d82a855a 557is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
ad457191 558{
d82a855a
VS
559 return crtc_state->sync_mode_slaves_mask != 0;
560}
561
562bool
563is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
564{
565 return is_trans_port_sync_master(crtc_state) ||
566 is_trans_port_sync_slave(crtc_state);
ad457191
JRS
567}
568
dccbea3b
ID
569/*
570 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
571 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
572 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
573 * The helpers' return value is the rate of the clock that is fed to the
574 * display engine's pipe which can be the above fast dot clock rate or a
575 * divided-down version of it.
576 */
f2b115e6 577/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 578static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 579{
2177832f
SL
580 clock->m = clock->m2 + 2;
581 clock->p = clock->p1 * clock->p2;
ed5ca77e 582 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 583 return 0;
fb03ac01
VS
584 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
585 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
586
587 return clock->dot;
2177832f
SL
588}
589
ba3f4d0a 590static u32 i9xx_dpll_compute_m(struct dpll *dpll)
7429e9d4
DV
591{
592 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
593}
594
9e2c8475 595static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 596{
7429e9d4 597 clock->m = i9xx_dpll_compute_m(clock);
79e53945 598 clock->p = clock->p1 * clock->p2;
ed5ca77e 599 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 600 return 0;
fb03ac01
VS
601 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
603
604 return clock->dot;
79e53945
JB
605}
606
9e2c8475 607static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
608{
609 clock->m = clock->m1 * clock->m2;
610 clock->p = clock->p1 * clock->p2;
611 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 612 return 0;
589eca67
ID
613 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
614 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
615
616 return clock->dot / 5;
589eca67
ID
617}
618
9e2c8475 619int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
620{
621 clock->m = clock->m1 * clock->m2;
622 clock->p = clock->p1 * clock->p2;
623 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 624 return 0;
d492a29d 625 clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
ba3f4d0a 626 clock->n << 22);
ef9348c8 627 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
628
629 return clock->dot / 5;
ef9348c8
CML
630}
631
c38c1455 632/*
79e53945
JB
633 * Returns whether the given set of divisors are valid for a given refclk with
634 * the given connectors.
635 */
4fb5eec9 636static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 637 const struct intel_limit *limit,
9e2c8475 638 const struct dpll *clock)
79e53945 639{
4fb5eec9
JN
640 if (clock->n < limit->n.min || limit->n.max < clock->n)
641 return false;
642 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
643 return false;
644 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
645 return false;
646 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
647 return false;
f01b7962 648
e2d214ae 649 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 650 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962 651 if (clock->m1 <= clock->m2)
4fb5eec9 652 return false;
f01b7962 653
e2d214ae 654 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 655 !IS_GEN9_LP(dev_priv)) {
f01b7962 656 if (clock->p < limit->p.min || limit->p.max < clock->p)
4fb5eec9 657 return false;
f01b7962 658 if (clock->m < limit->m.min || limit->m.max < clock->m)
4fb5eec9 659 return false;
f01b7962
VS
660 }
661
79e53945 662 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
4fb5eec9 663 return false;
79e53945
JB
664 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
665 * connector, etc., rather than just a single range.
666 */
667 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
4fb5eec9 668 return false;
79e53945
JB
669
670 return true;
671}
672
3b1429d9 673static int
1b6f4958 674i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
675 const struct intel_crtc_state *crtc_state,
676 int target)
79e53945 677{
2225f3c6 678 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
79e53945 679
2d84d2b3 680 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 681 /*
a210b028
DV
682 * For LVDS just rely on its current settings for dual-channel.
683 * We haven't figured out how to reliably set up different
684 * single/dual channel state, if we even can.
79e53945 685 */
d2daff2c 686 if (intel_is_dual_link_lvds(dev_priv))
3b1429d9 687 return limit->p2.p2_fast;
79e53945 688 else
3b1429d9 689 return limit->p2.p2_slow;
79e53945
JB
690 } else {
691 if (target < limit->p2.dot_limit)
3b1429d9 692 return limit->p2.p2_slow;
79e53945 693 else
3b1429d9 694 return limit->p2.p2_fast;
79e53945 695 }
3b1429d9
VS
696}
697
70e8aa21
ACO
698/*
699 * Returns a set of divisors for the desired target clock with the given
700 * refclk, or FALSE. The returned values represent the clock equation:
701 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
702 *
703 * Target and reference clocks are specified in kHz.
704 *
705 * If match_clock is provided, then best_clock P divider must match the P
706 * divider from @match_clock used for LVDS downclocking.
707 */
3b1429d9 708static bool
1b6f4958 709i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 710 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
711 int target, int refclk, struct dpll *match_clock,
712 struct dpll *best_clock)
3b1429d9 713{
2225f3c6 714 struct drm_device *dev = crtc_state->uapi.crtc->dev;
9e2c8475 715 struct dpll clock;
3b1429d9 716 int err = target;
79e53945 717
0206e353 718 memset(best_clock, 0, sizeof(*best_clock));
79e53945 719
3b1429d9
VS
720 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
721
42158660
ZY
722 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
723 clock.m1++) {
724 for (clock.m2 = limit->m2.min;
725 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 726 if (clock.m2 >= clock.m1)
42158660
ZY
727 break;
728 for (clock.n = limit->n.min;
729 clock.n <= limit->n.max; clock.n++) {
730 for (clock.p1 = limit->p1.min;
731 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
732 int this_err;
733
dccbea3b 734 i9xx_calc_dpll_params(refclk, &clock);
4fb5eec9 735 if (!intel_pll_is_valid(to_i915(dev),
e2d214ae 736 limit,
ac58c3f0
DV
737 &clock))
738 continue;
739 if (match_clock &&
740 clock.p != match_clock->p)
741 continue;
742
743 this_err = abs(clock.dot - target);
744 if (this_err < err) {
745 *best_clock = clock;
746 err = this_err;
747 }
748 }
749 }
750 }
751 }
752
753 return (err != target);
754}
755
70e8aa21
ACO
756/*
757 * Returns a set of divisors for the desired target clock with the given
758 * refclk, or FALSE. The returned values represent the clock equation:
759 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
760 *
761 * Target and reference clocks are specified in kHz.
762 *
763 * If match_clock is provided, then best_clock P divider must match the P
764 * divider from @match_clock used for LVDS downclocking.
765 */
ac58c3f0 766static bool
1b6f4958 767pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 768 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
769 int target, int refclk, struct dpll *match_clock,
770 struct dpll *best_clock)
79e53945 771{
2225f3c6 772 struct drm_device *dev = crtc_state->uapi.crtc->dev;
9e2c8475 773 struct dpll clock;
79e53945
JB
774 int err = target;
775
0206e353 776 memset(best_clock, 0, sizeof(*best_clock));
79e53945 777
3b1429d9
VS
778 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
779
42158660
ZY
780 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
781 clock.m1++) {
782 for (clock.m2 = limit->m2.min;
783 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
784 for (clock.n = limit->n.min;
785 clock.n <= limit->n.max; clock.n++) {
786 for (clock.p1 = limit->p1.min;
787 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
788 int this_err;
789
dccbea3b 790 pnv_calc_dpll_params(refclk, &clock);
4fb5eec9 791 if (!intel_pll_is_valid(to_i915(dev),
e2d214ae 792 limit,
1b894b59 793 &clock))
79e53945 794 continue;
cec2f356
SP
795 if (match_clock &&
796 clock.p != match_clock->p)
797 continue;
79e53945
JB
798
799 this_err = abs(clock.dot - target);
800 if (this_err < err) {
801 *best_clock = clock;
802 err = this_err;
803 }
804 }
805 }
806 }
807 }
808
809 return (err != target);
810}
811
997c030c
ACO
812/*
813 * Returns a set of divisors for the desired target clock with the given
814 * refclk, or FALSE. The returned values represent the clock equation:
815 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
816 *
817 * Target and reference clocks are specified in kHz.
818 *
819 * If match_clock is provided, then best_clock P divider must match the P
820 * divider from @match_clock used for LVDS downclocking.
997c030c 821 */
d4906093 822static bool
1b6f4958 823g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 824 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
825 int target, int refclk, struct dpll *match_clock,
826 struct dpll *best_clock)
d4906093 827{
2225f3c6 828 struct drm_device *dev = crtc_state->uapi.crtc->dev;
9e2c8475 829 struct dpll clock;
d4906093 830 int max_n;
3b1429d9 831 bool found = false;
6ba770dc
AJ
832 /* approximately equals target * 0.00585 */
833 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
834
835 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
836
837 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
838
d4906093 839 max_n = limit->n.max;
f77f13e2 840 /* based on hardware requirement, prefer smaller n to precision */
d4906093 841 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 842 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
843 for (clock.m1 = limit->m1.max;
844 clock.m1 >= limit->m1.min; clock.m1--) {
845 for (clock.m2 = limit->m2.max;
846 clock.m2 >= limit->m2.min; clock.m2--) {
847 for (clock.p1 = limit->p1.max;
848 clock.p1 >= limit->p1.min; clock.p1--) {
849 int this_err;
850
dccbea3b 851 i9xx_calc_dpll_params(refclk, &clock);
4fb5eec9 852 if (!intel_pll_is_valid(to_i915(dev),
e2d214ae 853 limit,
1b894b59 854 &clock))
d4906093 855 continue;
1b894b59
CW
856
857 this_err = abs(clock.dot - target);
d4906093
ML
858 if (this_err < err_most) {
859 *best_clock = clock;
860 err_most = this_err;
861 max_n = clock.n;
862 found = true;
863 }
864 }
865 }
866 }
867 }
2c07245f
ZW
868 return found;
869}
870
d5dd62bd
ID
871/*
872 * Check if the calculated PLL configuration is more optimal compared to the
873 * best configuration and error found so far. Return the calculated error.
874 */
875static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
876 const struct dpll *calculated_clock,
877 const struct dpll *best_clock,
d5dd62bd
ID
878 unsigned int best_error_ppm,
879 unsigned int *error_ppm)
880{
9ca3ba01
ID
881 /*
882 * For CHV ignore the error and consider only the P value.
883 * Prefer a bigger P value based on HW requirements.
884 */
920a14b2 885 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
886 *error_ppm = 0;
887
888 return calculated_clock->p > best_clock->p;
889 }
890
e57291c2 891 if (drm_WARN_ON_ONCE(dev, !target_freq))
24be4e46
ID
892 return false;
893
d5dd62bd
ID
894 *error_ppm = div_u64(1000000ULL *
895 abs(target_freq - calculated_clock->dot),
896 target_freq);
897 /*
898 * Prefer a better P value over a better (smaller) error if the error
899 * is small. Ensure this preference for future configurations too by
900 * setting the error to 0.
901 */
902 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
903 *error_ppm = 0;
904
905 return true;
906 }
907
908 return *error_ppm + 10 < best_error_ppm;
909}
910
65b3d6a9
ACO
911/*
912 * Returns a set of divisors for the desired target clock with the given
913 * refclk, or FALSE. The returned values represent the clock equation:
914 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
915 */
a0c4da24 916static bool
1b6f4958 917vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 918 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
919 int target, int refclk, struct dpll *match_clock,
920 struct dpll *best_clock)
a0c4da24 921{
2225f3c6 922 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
a919ff14 923 struct drm_device *dev = crtc->base.dev;
9e2c8475 924 struct dpll clock;
69e4f900 925 unsigned int bestppm = 1000000;
27e639bf
VS
926 /* min update 19.2 MHz */
927 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 928 bool found = false;
a0c4da24 929
6b4bf1c4
VS
930 target *= 5; /* fast clock */
931
932 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
933
934 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 935 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 936 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 937 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 938 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 939 clock.p = clock.p1 * clock.p2;
a0c4da24 940 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 941 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 942 unsigned int ppm;
69e4f900 943
6b4bf1c4
VS
944 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
945 refclk * clock.m1);
946
dccbea3b 947 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 948
4fb5eec9 949 if (!intel_pll_is_valid(to_i915(dev),
e2d214ae 950 limit,
f01b7962 951 &clock))
43b0ac53
VS
952 continue;
953
d5dd62bd
ID
954 if (!vlv_PLL_is_optimal(dev, target,
955 &clock,
956 best_clock,
957 bestppm, &ppm))
958 continue;
6b4bf1c4 959
d5dd62bd
ID
960 *best_clock = clock;
961 bestppm = ppm;
962 found = true;
a0c4da24
JB
963 }
964 }
965 }
966 }
a0c4da24 967
49e497ef 968 return found;
a0c4da24 969}
a4fc5ed6 970
65b3d6a9
ACO
971/*
972 * Returns a set of divisors for the desired target clock with the given
973 * refclk, or FALSE. The returned values represent the clock equation:
974 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
975 */
ef9348c8 976static bool
1b6f4958 977chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 978 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
979 int target, int refclk, struct dpll *match_clock,
980 struct dpll *best_clock)
ef9348c8 981{
2225f3c6 982 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
a919ff14 983 struct drm_device *dev = crtc->base.dev;
9ca3ba01 984 unsigned int best_error_ppm;
9e2c8475 985 struct dpll clock;
ba3f4d0a 986 u64 m2;
ef9348c8
CML
987 int found = false;
988
989 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 990 best_error_ppm = 1000000;
ef9348c8
CML
991
992 /*
993 * Based on hardware doc, the n always set to 1, and m1 always
994 * set to 2. If requires to support 200Mhz refclk, we need to
995 * revisit this because n may not 1 anymore.
996 */
997 clock.n = 1, clock.m1 = 2;
998 target *= 5; /* fast clock */
999
1000 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1001 for (clock.p2 = limit->p2.p2_fast;
1002 clock.p2 >= limit->p2.p2_slow;
1003 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1004 unsigned int error_ppm;
ef9348c8
CML
1005
1006 clock.p = clock.p1 * clock.p2;
1007
d492a29d
VS
1008 m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1009 refclk * clock.m1);
ef9348c8
CML
1010
1011 if (m2 > INT_MAX/clock.m1)
1012 continue;
1013
1014 clock.m2 = m2;
1015
dccbea3b 1016 chv_calc_dpll_params(refclk, &clock);
ef9348c8 1017
4fb5eec9 1018 if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
1019 continue;
1020
9ca3ba01
ID
1021 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1022 best_error_ppm, &error_ppm))
1023 continue;
1024
1025 *best_clock = clock;
1026 best_error_ppm = error_ppm;
1027 found = true;
ef9348c8
CML
1028 }
1029 }
1030
1031 return found;
1032}
1033
e40396d0 1034bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
9e2c8475 1035 struct dpll *best_clock)
5ab7b0b7 1036{
65b3d6a9 1037 int refclk = 100000;
1b6f4958 1038 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1039
65b3d6a9 1040 return chv_find_best_dpll(limit, crtc_state,
e40396d0
VS
1041 crtc_state->port_clock, refclk,
1042 NULL, best_clock);
5ab7b0b7
ID
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
cf819eff 1052 if (IS_GEN(dev_priv, 2))
fbf49ea2
VS
1053 line_mask = DSL_LINEMASK_GEN2;
1054 else
1055 line_mask = DSL_LINEMASK_GEN3;
1056
dc008bf0 1057 line1 = intel_de_read(dev_priv, reg) & line_mask;
6adfb1ef 1058 msleep(5);
dc008bf0 1059 line2 = intel_de_read(dev_priv, reg) & line_mask;
fbf49ea2 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))
cd49f818
WK
1071 drm_err(&dev_priv->drm,
1072 "pipe %c scanline %s wait timed out\n",
1073 pipe_name(pipe), onoff(state));
8fedd64d
VS
1074}
1075
1076static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1077{
1078 wait_for_pipe_scanline_moving(crtc, false);
1079}
1080
1081static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1082{
1083 wait_for_pipe_scanline_moving(crtc, true);
fbf49ea2
VS
1084}
1085
4972f70a
VS
1086static void
1087intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
9d0498a2 1088{
2225f3c6 1089 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6315b5d3 1090 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ab7ad7f6 1091
6315b5d3 1092 if (INTEL_GEN(dev_priv) >= 4) {
4972f70a 1093 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
f0f59a00 1094 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1095
1096 /* Wait for the Pipe State to go off */
4cb3b44d
DCS
1097 if (intel_de_wait_for_clear(dev_priv, reg,
1098 I965_PIPECONF_ACTIVE, 100))
e57291c2
PB
1099 drm_WARN(&dev_priv->drm, 1,
1100 "pipe_off wait timed out\n");
ab7ad7f6 1101 } else {
8fedd64d 1102 intel_wait_for_pipe_scanline_stopped(crtc);
ab7ad7f6 1103 }
79e53945
JB
1104}
1105
b24e7179 1106/* Only for pre-ILK configs */
55607e8a
DV
1107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
b24e7179 1109{
b24e7179
JB
1110 u32 val;
1111 bool cur_state;
1112
dc008bf0 1113 val = intel_de_read(dev_priv, DPLL(pipe));
b24e7179 1114 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1115 I915_STATE_WARN(cur_state != state,
b24e7179 1116 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1117 onoff(state), onoff(cur_state));
b24e7179 1118}
b24e7179 1119
23538ef1 1120/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1121void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1122{
1123 u32 val;
1124 bool cur_state;
1125
221c7862 1126 vlv_cck_get(dev_priv);
23538ef1 1127 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
221c7862 1128 vlv_cck_put(dev_priv);
23538ef1
JN
1129
1130 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1131 I915_STATE_WARN(cur_state != state,
23538ef1 1132 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1133 onoff(state), onoff(cur_state));
23538ef1 1134}
23538ef1 1135
040484af
JB
1136static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1137 enum pipe pipe, bool state)
1138{
040484af
JB
1139 bool cur_state;
1140
2d1fe073 1141 if (HAS_DDI(dev_priv)) {
a722146b
VS
1142 /*
1143 * DDI does not have a specific FDI_TX register.
1144 *
1145 * FDI is never fed from EDP transcoder
1146 * so pipe->transcoder cast is fine here.
1147 */
1148 enum transcoder cpu_transcoder = (enum transcoder)pipe;
dc008bf0
JN
1149 u32 val = intel_de_read(dev_priv,
1150 TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1151 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1152 } else {
dc008bf0 1153 u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
bf507ef7
ED
1154 cur_state = !!(val & FDI_TX_ENABLE);
1155 }
e2c719b7 1156 I915_STATE_WARN(cur_state != state,
040484af 1157 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1158 onoff(state), onoff(cur_state));
040484af
JB
1159}
1160#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1161#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1162
1163static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1164 enum pipe pipe, bool state)
1165{
040484af
JB
1166 u32 val;
1167 bool cur_state;
1168
dc008bf0 1169 val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
d63fa0dc 1170 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1171 I915_STATE_WARN(cur_state != state,
040484af 1172 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1173 onoff(state), onoff(cur_state));
040484af
JB
1174}
1175#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1176#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1177
1178static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1179 enum pipe pipe)
1180{
040484af
JB
1181 u32 val;
1182
1183 /* ILK FDI PLL is always enabled */
cf819eff 1184 if (IS_GEN(dev_priv, 5))
040484af
JB
1185 return;
1186
bf507ef7 1187 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1188 if (HAS_DDI(dev_priv))
bf507ef7
ED
1189 return;
1190
dc008bf0 1191 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
e2c719b7 1192 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1193}
1194
55607e8a
DV
1195void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
040484af 1197{
040484af 1198 u32 val;
55607e8a 1199 bool cur_state;
040484af 1200
dc008bf0 1201 val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
55607e8a 1202 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1203 I915_STATE_WARN(cur_state != state,
55607e8a 1204 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1205 onoff(state), onoff(cur_state));
040484af
JB
1206}
1207
4f8036a2 1208void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1209{
f0f59a00 1210 i915_reg_t pp_reg;
ea0760cf 1211 u32 val;
10ed55e4 1212 enum pipe panel_pipe = INVALID_PIPE;
0de3b485 1213 bool locked = true;
ea0760cf 1214
e57291c2 1215 if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
bedd4dba
JN
1216 return;
1217
4f8036a2 1218 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1219 u32 port_sel;
1220
44cb734c 1221 pp_reg = PP_CONTROL(0);
dc008bf0 1222 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba 1223
4c23dea4
VS
1224 switch (port_sel) {
1225 case PANEL_PORT_SELECT_LVDS:
a44628b9 1226 intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
4c23dea4
VS
1227 break;
1228 case PANEL_PORT_SELECT_DPA:
1229 intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1230 break;
1231 case PANEL_PORT_SELECT_DPC:
1232 intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1233 break;
1234 case PANEL_PORT_SELECT_DPD:
1235 intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1236 break;
1237 default:
1238 MISSING_CASE(port_sel);
1239 break;
1240 }
4f8036a2 1241 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1242 /* presumably write lock depends on pipe, not port select */
44cb734c 1243 pp_reg = PP_CONTROL(pipe);
bedd4dba 1244 panel_pipe = pipe;
ea0760cf 1245 } else {
f0d2b758
VS
1246 u32 port_sel;
1247
44cb734c 1248 pp_reg = PP_CONTROL(0);
dc008bf0 1249 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
f0d2b758 1250
e57291c2
PB
1251 drm_WARN_ON(&dev_priv->drm,
1252 port_sel != PANEL_PORT_SELECT_LVDS);
a44628b9 1253 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
ea0760cf
JB
1254 }
1255
dc008bf0 1256 val = intel_de_read(dev_priv, pp_reg);
ea0760cf 1257 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1258 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1259 locked = false;
1260
e2c719b7 1261 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1262 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1263 pipe_name(pipe));
ea0760cf
JB
1264}
1265
b840d907 1266void assert_pipe(struct drm_i915_private *dev_priv,
b104e8b2 1267 enum transcoder cpu_transcoder, bool state)
b24e7179 1268{
63d7bbe9 1269 bool cur_state;
4feed0eb 1270 enum intel_display_power_domain power_domain;
0e6e0be4 1271 intel_wakeref_t wakeref;
b24e7179 1272
e56134bc
VS
1273 /* we keep both pipes enabled on 830 */
1274 if (IS_I830(dev_priv))
8e636784
DV
1275 state = true;
1276
4feed0eb 1277 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
0e6e0be4
CW
1278 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1279 if (wakeref) {
dc008bf0 1280 u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
69310161 1281 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb 1282
0e6e0be4 1283 intel_display_power_put(dev_priv, power_domain, wakeref);
4feed0eb
ID
1284 } else {
1285 cur_state = false;
69310161
PZ
1286 }
1287
e2c719b7 1288 I915_STATE_WARN(cur_state != state,
b104e8b2
VS
1289 "transcoder %s assertion failure (expected %s, current %s)\n",
1290 transcoder_name(cpu_transcoder),
1291 onoff(state), onoff(cur_state));
b24e7179
JB
1292}
1293
51f5a096 1294static void assert_plane(struct intel_plane *plane, bool state)
b24e7179 1295{
eade6c89
VS
1296 enum pipe pipe;
1297 bool cur_state;
1298
1299 cur_state = plane->get_hw_state(plane, &pipe);
b24e7179 1300
e2c719b7 1301 I915_STATE_WARN(cur_state != state,
51f5a096
VS
1302 "%s assertion failure (expected %s, current %s)\n",
1303 plane->base.name, onoff(state), onoff(cur_state));
b24e7179
JB
1304}
1305
51f5a096
VS
1306#define assert_plane_enabled(p) assert_plane(p, true)
1307#define assert_plane_disabled(p) assert_plane(p, false)
931872fc 1308
51f5a096 1309static void assert_planes_disabled(struct intel_crtc *crtc)
b24e7179 1310{
51f5a096
VS
1311 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1312 struct intel_plane *plane;
19332d7a 1313
51f5a096
VS
1314 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1315 assert_plane_disabled(plane);
19332d7a
JB
1316}
1317
08c71e5e
VS
1318static void assert_vblank_disabled(struct drm_crtc *crtc)
1319{
e2c719b7 1320 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1321 drm_crtc_vblank_put(crtc);
1322}
1323
7abd4b35
ACO
1324void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1325 enum pipe pipe)
92f2584a 1326{
92f2584a
JB
1327 u32 val;
1328 bool enabled;
1329
dc008bf0 1330 val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
92f2584a 1331 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1332 I915_STATE_WARN(enabled,
9db4a9c7
JB
1333 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1334 pipe_name(pipe));
92f2584a
JB
1335}
1336
59b74c49
VS
1337static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1338 enum pipe pipe, enum port port,
1339 i915_reg_t dp_reg)
f0575e92 1340{
59b74c49
VS
1341 enum pipe port_pipe;
1342 bool state;
f0575e92 1343
59b74c49 1344 state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
f0575e92 1345
59b74c49
VS
1346 I915_STATE_WARN(state && port_pipe == pipe,
1347 "PCH DP %c enabled on transcoder %c, should be disabled\n",
1348 port_name(port), pipe_name(pipe));
1349
1350 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1351 "IBX PCH DP %c still using transcoder B\n",
1352 port_name(port));
291906f1
JB
1353}
1354
1355static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
76203467
VS
1356 enum pipe pipe, enum port port,
1357 i915_reg_t hdmi_reg)
291906f1 1358{
76203467
VS
1359 enum pipe port_pipe;
1360 bool state;
1361
1362 state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1363
1364 I915_STATE_WARN(state && port_pipe == pipe,
1365 "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1366 port_name(port), pipe_name(pipe));
de9a35ab 1367
76203467
VS
1368 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1369 "IBX PCH HDMI %c still using transcoder B\n",
1370 port_name(port));
291906f1
JB
1371}
1372
1373static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1374 enum pipe pipe)
1375{
6102a8ee 1376 enum pipe port_pipe;
291906f1 1377
59b74c49
VS
1378 assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1379 assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1380 assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
291906f1 1381
6102a8ee
VS
1382 I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1383 port_pipe == pipe,
1384 "PCH VGA enabled on transcoder %c, should be disabled\n",
1385 pipe_name(pipe));
291906f1 1386
a44628b9
VS
1387 I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1388 port_pipe == pipe,
1389 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1390 pipe_name(pipe));
291906f1 1391
3aefb67f 1392 /* PCH SDVOB multiplex with HDMIB */
76203467
VS
1393 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1394 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1395 assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
291906f1
JB
1396}
1397
cd2d34d9
VS
1398static void _vlv_enable_pll(struct intel_crtc *crtc,
1399 const struct intel_crtc_state *pipe_config)
1400{
1401 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1402 enum pipe pipe = crtc->pipe;
1403
dc008bf0
JN
1404 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1405 intel_de_posting_read(dev_priv, DPLL(pipe));
cd2d34d9
VS
1406 udelay(150);
1407
4cb3b44d 1408 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
cd49f818 1409 drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
cd2d34d9
VS
1410}
1411
d288f65f 1412static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1413 const struct intel_crtc_state *pipe_config)
87442f73 1414{
cd2d34d9 1415 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1416 enum pipe pipe = crtc->pipe;
87442f73 1417
b104e8b2 1418 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
87442f73 1419
87442f73 1420 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1421 assert_panel_unlocked(dev_priv, pipe);
87442f73 1422
cd2d34d9
VS
1423 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1424 _vlv_enable_pll(crtc, pipe_config);
426115cf 1425
dc008bf0
JN
1426 intel_de_write(dev_priv, DPLL_MD(pipe),
1427 pipe_config->dpll_hw_state.dpll_md);
1428 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
87442f73
DV
1429}
1430
cd2d34d9
VS
1431
1432static void _chv_enable_pll(struct intel_crtc *crtc,
1433 const struct intel_crtc_state *pipe_config)
9d556c99 1434{
cd2d34d9 1435 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1436 enum pipe pipe = crtc->pipe;
9d556c99 1437 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1438 u32 tmp;
1439
221c7862 1440 vlv_dpio_get(dev_priv);
9d556c99
CML
1441
1442 /* Enable back the 10bit clock to display controller */
1443 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1444 tmp |= DPIO_DCLKP_EN;
1445 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1446
221c7862 1447 vlv_dpio_put(dev_priv);
54433e91 1448
9d556c99
CML
1449 /*
1450 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1451 */
1452 udelay(1);
1453
1454 /* Enable PLL */
dc008bf0 1455 intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1456
1457 /* Check PLL is locked */
4cb3b44d 1458 if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
cd49f818 1459 drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1460}
1461
1462static void chv_enable_pll(struct intel_crtc *crtc,
1463 const struct intel_crtc_state *pipe_config)
1464{
1465 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1466 enum pipe pipe = crtc->pipe;
1467
b104e8b2 1468 assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
cd2d34d9
VS
1469
1470 /* PLL is protected by panel, make sure we can write it */
1471 assert_panel_unlocked(dev_priv, pipe);
1472
1473 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1474 _chv_enable_pll(crtc, pipe_config);
9d556c99 1475
c231775c
VS
1476 if (pipe != PIPE_A) {
1477 /*
1478 * WaPixelRepeatModeFixForC0:chv
1479 *
1480 * DPLLCMD is AWOL. Use chicken bits to propagate
1481 * the value from DPLLBMD to either pipe B or C.
1482 */
dc008bf0
JN
1483 intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1484 intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1485 pipe_config->dpll_hw_state.dpll_md);
1486 intel_de_write(dev_priv, CBR4_VLV, 0);
c231775c
VS
1487 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1488
1489 /*
1490 * DPLLB VGA mode also seems to cause problems.
1491 * We should always have it disabled.
1492 */
e57291c2
PB
1493 drm_WARN_ON(&dev_priv->drm,
1494 (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1495 DPLL_VGA_MODE_DIS) == 0);
c231775c 1496 } else {
dc008bf0
JN
1497 intel_de_write(dev_priv, DPLL_MD(pipe),
1498 pipe_config->dpll_hw_state.dpll_md);
1499 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
c231775c 1500 }
9d556c99
CML
1501}
1502
9e7d5699
VS
1503static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1504{
1505 if (IS_I830(dev_priv))
1506 return false;
1507
1508 return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1509}
1510
939994da
VS
1511static void i9xx_enable_pll(struct intel_crtc *crtc,
1512 const struct intel_crtc_state *crtc_state)
63d7bbe9 1513{
6315b5d3 1514 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1515 i915_reg_t reg = DPLL(crtc->pipe);
939994da 1516 u32 dpll = crtc_state->dpll_hw_state.dpll;
bb408dd2 1517 int i;
63d7bbe9 1518
b104e8b2 1519 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
58c6eaa2 1520
63d7bbe9 1521 /* PLL is protected by panel, make sure we can write it */
9e7d5699 1522 if (i9xx_has_pps(dev_priv))
66e3d5c0 1523 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1524
c2b63374
VS
1525 /*
1526 * Apparently we need to have VGA mode enabled prior to changing
1527 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1528 * dividers, even though the register value does change.
1529 */
dc008bf0
JN
1530 intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1531 intel_de_write(dev_priv, reg, dpll);
8e7a65aa 1532
66e3d5c0 1533 /* Wait for the clocks to stabilize. */
dc008bf0 1534 intel_de_posting_read(dev_priv, reg);
66e3d5c0
DV
1535 udelay(150);
1536
6315b5d3 1537 if (INTEL_GEN(dev_priv) >= 4) {
dc008bf0
JN
1538 intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1539 crtc_state->dpll_hw_state.dpll_md);
66e3d5c0
DV
1540 } else {
1541 /* The pixel multiplier can only be updated once the
1542 * DPLL is enabled and the clocks are stable.
1543 *
1544 * So write it again.
1545 */
dc008bf0 1546 intel_de_write(dev_priv, reg, dpll);
66e3d5c0 1547 }
63d7bbe9
JB
1548
1549 /* We do this three times for luck */
bb408dd2 1550 for (i = 0; i < 3; i++) {
dc008bf0
JN
1551 intel_de_write(dev_priv, reg, dpll);
1552 intel_de_posting_read(dev_priv, reg);
bb408dd2
VS
1553 udelay(150); /* wait for warmup */
1554 }
63d7bbe9
JB
1555}
1556
b2354c78 1557static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
63d7bbe9 1558{
2225f3c6 1559 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6315b5d3 1560 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1561 enum pipe pipe = crtc->pipe;
1562
b6b5d049 1563 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1564 if (IS_I830(dev_priv))
63d7bbe9
JB
1565 return;
1566
1567 /* Make sure the pipe isn't still relying on us */
b104e8b2 1568 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
63d7bbe9 1569
dc008bf0
JN
1570 intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1571 intel_de_posting_read(dev_priv, DPLL(pipe));
63d7bbe9
JB
1572}
1573
f6071166
JB
1574static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1575{
b8afb911 1576 u32 val;
f6071166
JB
1577
1578 /* Make sure the pipe isn't still relying on us */
b104e8b2 1579 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
f6071166 1580
03ed5cbf
VS
1581 val = DPLL_INTEGRATED_REF_CLK_VLV |
1582 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1583 if (pipe != PIPE_A)
1584 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1585
dc008bf0
JN
1586 intel_de_write(dev_priv, DPLL(pipe), val);
1587 intel_de_posting_read(dev_priv, DPLL(pipe));
076ed3b2
CML
1588}
1589
1590static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1591{
d752048d 1592 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1593 u32 val;
1594
a11b0703 1595 /* Make sure the pipe isn't still relying on us */
b104e8b2 1596 assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
076ed3b2 1597
60bfe44f
VS
1598 val = DPLL_SSC_REF_CLK_CHV |
1599 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1600 if (pipe != PIPE_A)
1601 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1602
dc008bf0
JN
1603 intel_de_write(dev_priv, DPLL(pipe), val);
1604 intel_de_posting_read(dev_priv, DPLL(pipe));
d752048d 1605
221c7862 1606 vlv_dpio_get(dev_priv);
d752048d
VS
1607
1608 /* Disable 10bit clock to display controller */
1609 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1610 val &= ~DPIO_DCLKP_EN;
1611 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1612
221c7862 1613 vlv_dpio_put(dev_priv);
f6071166
JB
1614}
1615
e4607fcf 1616void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
7801f3b7 1617 struct intel_digital_port *dig_port,
9b6de0a1 1618 unsigned int expected_mask)
89b667f8
JB
1619{
1620 u32 port_mask;
f0f59a00 1621 i915_reg_t dpll_reg;
89b667f8 1622
7801f3b7 1623 switch (dig_port->base.port) {
e4607fcf 1624 case PORT_B:
89b667f8 1625 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1626 dpll_reg = DPLL(0);
e4607fcf
CML
1627 break;
1628 case PORT_C:
89b667f8 1629 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1630 dpll_reg = DPLL(0);
9b6de0a1 1631 expected_mask <<= 4;
00fc31b7
CML
1632 break;
1633 case PORT_D:
1634 port_mask = DPLL_PORTD_READY_MASK;
1635 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1636 break;
1637 default:
1638 BUG();
1639 }
89b667f8 1640
4cb3b44d
DCS
1641 if (intel_de_wait_for_register(dev_priv, dpll_reg,
1642 port_mask, expected_mask, 1000))
e57291c2
PB
1643 drm_WARN(&dev_priv->drm, 1,
1644 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
7801f3b7 1645 dig_port->base.base.base.id, dig_port->base.base.name,
e57291c2
PB
1646 intel_de_read(dev_priv, dpll_reg) & port_mask,
1647 expected_mask);
89b667f8
JB
1648}
1649
9eae5e27 1650static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
040484af 1651{
2225f3c6 1652 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7efd90fb
ML
1653 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1654 enum pipe pipe = crtc->pipe;
f0f59a00 1655 i915_reg_t reg;
ba3f4d0a 1656 u32 val, pipeconf_val;
040484af 1657
040484af 1658 /* Make sure PCH DPLL is enabled */
7efd90fb 1659 assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
040484af
JB
1660
1661 /* FDI must be feeding us bits for PCH ports */
1662 assert_fdi_tx_enabled(dev_priv, pipe);
1663 assert_fdi_rx_enabled(dev_priv, pipe);
1664
6e266956 1665 if (HAS_PCH_CPT(dev_priv)) {
23670b32 1666 reg = TRANS_CHICKEN2(pipe);
dc008bf0 1667 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
1668 /*
1669 * Workaround: Set the timing override bit
1670 * before enabling the pch transcoder.
1671 */
23670b32 1672 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
cc7a4cff
VS
1673 /* Configure frame start delay to match the CPU */
1674 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1675 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
dc008bf0 1676 intel_de_write(dev_priv, reg, val);
59c859d6 1677 }
23670b32 1678
ab9412ba 1679 reg = PCH_TRANSCONF(pipe);
dc008bf0
JN
1680 val = intel_de_read(dev_priv, reg);
1681 pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
e9bcff5c 1682
2d1fe073 1683 if (HAS_PCH_IBX(dev_priv)) {
cc7a4cff
VS
1684 /* Configure frame start delay to match the CPU */
1685 val &= ~TRANS_FRAME_START_DELAY_MASK;
1686 val |= TRANS_FRAME_START_DELAY(0);
1687
e9bcff5c 1688 /*
c5de7c6f
VS
1689 * Make the BPC in transcoder be consistent with
1690 * that in pipeconf reg. For HDMI we must use 8bpc
1691 * here for both 8bpc and 12bpc.
e9bcff5c 1692 */
dfd07d72 1693 val &= ~PIPECONF_BPC_MASK;
7efd90fb 1694 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1695 val |= PIPECONF_8BPC;
1696 else
1697 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1698 }
5f7f726d
PZ
1699
1700 val &= ~TRANS_INTERLACE_MASK;
27b680f9 1701 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
2d1fe073 1702 if (HAS_PCH_IBX(dev_priv) &&
7efd90fb 1703 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1704 val |= TRANS_LEGACY_INTERLACED_ILK;
1705 else
1706 val |= TRANS_INTERLACED;
27b680f9 1707 } else {
5f7f726d 1708 val |= TRANS_PROGRESSIVE;
27b680f9 1709 }
5f7f726d 1710
dc008bf0 1711 intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
4cb3b44d 1712 if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
cd49f818
WK
1713 drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1714 pipe_name(pipe));
040484af
JB
1715}
1716
8fb033d7 1717static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1718 enum transcoder cpu_transcoder)
040484af 1719{
8fb033d7 1720 u32 val, pipeconf_val;
8fb033d7 1721
8fb033d7 1722 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1723 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
a2196033 1724 assert_fdi_rx_enabled(dev_priv, PIPE_A);
8fb033d7 1725
dc008bf0 1726 val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
cc7a4cff 1727 /* Workaround: set timing override bit. */
23670b32 1728 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
cc7a4cff
VS
1729 /* Configure frame start delay to match the CPU */
1730 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1731 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
dc008bf0 1732 intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1733
25f3ef11 1734 val = TRANS_ENABLE;
dc008bf0 1735 pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
8fb033d7 1736
9a76b1c6
PZ
1737 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1738 PIPECONF_INTERLACED_ILK)
a35f2679 1739 val |= TRANS_INTERLACED;
8fb033d7
PZ
1740 else
1741 val |= TRANS_PROGRESSIVE;
1742
dc008bf0 1743 intel_de_write(dev_priv, LPT_TRANSCONF, val);
4cb3b44d
DCS
1744 if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1745 TRANS_STATE_ENABLE, 100))
cd49f818 1746 drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
8fb033d7
PZ
1747}
1748
9eae5e27
LDM
1749static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1750 enum pipe pipe)
040484af 1751{
f0f59a00 1752 i915_reg_t reg;
ba3f4d0a 1753 u32 val;
040484af
JB
1754
1755 /* FDI relies on the transcoder */
1756 assert_fdi_tx_disabled(dev_priv, pipe);
1757 assert_fdi_rx_disabled(dev_priv, pipe);
1758
291906f1
JB
1759 /* Ports must be off as well */
1760 assert_pch_ports_disabled(dev_priv, pipe);
1761
ab9412ba 1762 reg = PCH_TRANSCONF(pipe);
dc008bf0 1763 val = intel_de_read(dev_priv, reg);
040484af 1764 val &= ~TRANS_ENABLE;
dc008bf0 1765 intel_de_write(dev_priv, reg, val);
040484af 1766 /* wait for PCH transcoder off, transcoder state */
4cb3b44d 1767 if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
cd49f818
WK
1768 drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1769 pipe_name(pipe));
23670b32 1770
6e266956 1771 if (HAS_PCH_CPT(dev_priv)) {
23670b32
DV
1772 /* Workaround: Clear the timing override chicken bit again. */
1773 reg = TRANS_CHICKEN2(pipe);
dc008bf0 1774 val = intel_de_read(dev_priv, reg);
23670b32 1775 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
dc008bf0 1776 intel_de_write(dev_priv, reg, val);
23670b32 1777 }
040484af
JB
1778}
1779
b7076546 1780void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1781{
8fb033d7
PZ
1782 u32 val;
1783
dc008bf0 1784 val = intel_de_read(dev_priv, LPT_TRANSCONF);
8fb033d7 1785 val &= ~TRANS_ENABLE;
dc008bf0 1786 intel_de_write(dev_priv, LPT_TRANSCONF, val);
8fb033d7 1787 /* wait for PCH transcoder off, transcoder state */
4cb3b44d
DCS
1788 if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1789 TRANS_STATE_ENABLE, 50))
cd49f818 1790 drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
223a6fdf
PZ
1791
1792 /* Workaround: clear timing override bit. */
dc008bf0 1793 val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
23670b32 1794 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
dc008bf0 1795 intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1796}
1797
a2196033 1798enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
65f2130c
VS
1799{
1800 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1801
65f2130c 1802 if (HAS_PCH_LPT(dev_priv))
a2196033 1803 return PIPE_A;
65f2130c 1804 else
a2196033 1805 return crtc->pipe;
65f2130c
VS
1806}
1807
32db0b65
VS
1808static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1809{
2225f3c6 1810 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
33267703
VK
1811 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1812 u32 mode_flags = crtc->mode_flags;
1813
1814 /*
1815 * From Gen 11, In case of dsi cmd mode, frame counter wouldnt
1816 * have updated at the beginning of TE, if we want to use
1817 * the hw counter, then we would find it updated in only
1818 * the next TE, hence switching to sw counter.
1819 */
1820 if (mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 | I915_MODE_FLAG_DSI_USE_TE1))
1821 return 0;
32db0b65
VS
1822
1823 /*
1824 * On i965gm the hardware frame counter reads
1825 * zero when the TV encoder is enabled :(
1826 */
1827 if (IS_I965GM(dev_priv) &&
1828 (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1829 return 0;
1830
1831 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1832 return 0xffffffff; /* full 32 bit counter */
1833 else if (INTEL_GEN(dev_priv) >= 3)
1834 return 0xffffff; /* only 24 bits of frame count */
1835 else
1836 return 0; /* Gen2 doesn't have a hardware frame counter */
1837}
1838
21fd23ac 1839void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
32db0b65 1840{
2225f3c6 1841 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
32db0b65 1842
407b9405 1843 assert_vblank_disabled(&crtc->base);
32db0b65
VS
1844 drm_crtc_set_max_vblank_count(&crtc->base,
1845 intel_crtc_max_vblank_count(crtc_state));
1846 drm_crtc_vblank_on(&crtc->base);
1847}
1848
773b4b54 1849void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
d18b6bb8 1850{
f5271ee5
VS
1851 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1852
d18b6bb8
VS
1853 drm_crtc_vblank_off(&crtc->base);
1854 assert_vblank_disabled(&crtc->base);
1855}
1856
21fd23ac 1857void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
b24e7179 1858{
2225f3c6 1859 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
4972f70a
VS
1860 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1861 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
0372264a 1862 enum pipe pipe = crtc->pipe;
f0f59a00 1863 i915_reg_t reg;
b24e7179
JB
1864 u32 val;
1865
cd49f818 1866 drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
9e2ee2dd 1867
51f5a096 1868 assert_planes_disabled(crtc);
58c6eaa2 1869
b24e7179
JB
1870 /*
1871 * A pipe without a PLL won't actually be able to drive bits from
1872 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1873 * need the check.
1874 */
b2ae318a 1875 if (HAS_GMCH(dev_priv)) {
4972f70a 1876 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
23538ef1
JN
1877 assert_dsi_pll_enabled(dev_priv);
1878 else
1879 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1880 } else {
4972f70a 1881 if (new_crtc_state->has_pch_encoder) {
040484af 1882 /* if driving the PCH, we need FDI enabled */
65f2130c 1883 assert_fdi_rx_pll_enabled(dev_priv,
a2196033 1884 intel_crtc_pch_transcoder(crtc));
1a240d4d
DV
1885 assert_fdi_tx_pll_enabled(dev_priv,
1886 (enum pipe) cpu_transcoder);
040484af
JB
1887 }
1888 /* FIXME: assert CPU port conditions for SNB+ */
1889 }
b24e7179 1890
4c888e7b 1891 trace_intel_pipe_enable(crtc);
0b2599a4 1892
702e7a56 1893 reg = PIPECONF(cpu_transcoder);
dc008bf0 1894 val = intel_de_read(dev_priv, reg);
7ad25d48 1895 if (val & PIPECONF_ENABLE) {
e56134bc 1896 /* we keep both pipes enabled on 830 */
e57291c2 1897 drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
00d70b15 1898 return;
7ad25d48 1899 }
00d70b15 1900
dc008bf0
JN
1901 intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1902 intel_de_posting_read(dev_priv, reg);
b7792d8b
VS
1903
1904 /*
8fedd64d
VS
1905 * Until the pipe starts PIPEDSL reads will return a stale value,
1906 * which causes an apparent vblank timestamp jump when PIPEDSL
1907 * resets to its proper value. That also messes up the frame count
1908 * when it's derived from the timestamps. So let's wait for the
1909 * pipe to start properly before we call drm_crtc_vblank_on()
b7792d8b 1910 */
32db0b65 1911 if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
8fedd64d 1912 intel_wait_for_pipe_scanline_moving(crtc);
b24e7179
JB
1913}
1914
773b4b54 1915void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
b24e7179 1916{
2225f3c6 1917 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
fac5e23e 1918 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4972f70a 1919 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
575f7ab7 1920 enum pipe pipe = crtc->pipe;
f0f59a00 1921 i915_reg_t reg;
b24e7179
JB
1922 u32 val;
1923
cd49f818 1924 drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
9e2ee2dd 1925
b24e7179
JB
1926 /*
1927 * Make sure planes won't keep trying to pump pixels to us,
1928 * or we might hang the display.
1929 */
51f5a096 1930 assert_planes_disabled(crtc);
b24e7179 1931
4c888e7b 1932 trace_intel_pipe_disable(crtc);
0b2599a4 1933
702e7a56 1934 reg = PIPECONF(cpu_transcoder);
dc008bf0 1935 val = intel_de_read(dev_priv, reg);
00d70b15
CW
1936 if ((val & PIPECONF_ENABLE) == 0)
1937 return;
1938
67adc644
VS
1939 /*
1940 * Double wide has implications for planes
1941 * so best keep it disabled when not needed.
1942 */
4972f70a 1943 if (old_crtc_state->double_wide)
67adc644
VS
1944 val &= ~PIPECONF_DOUBLE_WIDE;
1945
1946 /* Don't disable pipe or pipe PLLs if needed */
e56134bc 1947 if (!IS_I830(dev_priv))
67adc644
VS
1948 val &= ~PIPECONF_ENABLE;
1949
dc008bf0 1950 intel_de_write(dev_priv, reg, val);
67adc644 1951 if ((val & PIPECONF_ENABLE) == 0)
4972f70a 1952 intel_wait_for_pipe_off(old_crtc_state);
b24e7179
JB
1953}
1954
832be82f
VS
1955static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1956{
cf819eff 1957 return IS_GEN(dev_priv, 2) ? 2048 : 4096;
832be82f
VS
1958}
1959
e7af9094
ID
1960static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1961{
1962 if (!is_ccs_modifier(fb->modifier))
1963 return false;
1964
1965 return plane >= fb->format->num_planes / 2;
1966}
1967
b3e57bcc
DP
1968static bool is_gen12_ccs_modifier(u64 modifier)
1969{
2dfbf9d2
DP
1970 return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1971 modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1972
b3e57bcc
DP
1973}
1974
1975static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1976{
1977 return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1978}
1979
e7af9094
ID
1980static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1981{
1982 if (is_ccs_modifier(fb->modifier))
1983 return is_ccs_plane(fb, plane);
1984
1985 return plane == 1;
1986}
1987
1988static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1989{
ce04ecd9
PB
1990 drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1991 (main_plane && main_plane >= fb->format->num_planes / 2));
e7af9094
ID
1992
1993 return fb->format->num_planes / 2 + main_plane;
1994}
1995
b3e57bcc
DP
1996static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1997{
ce04ecd9
PB
1998 drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1999 ccs_plane < fb->format->num_planes / 2);
b3e57bcc
DP
2000
2001 return ccs_plane - fb->format->num_planes / 2;
2002}
2003
2dfbf9d2 2004int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
e7af9094 2005{
a007138e
VS
2006 struct drm_i915_private *i915 = to_i915(fb->dev);
2007
e7af9094
ID
2008 if (is_ccs_modifier(fb->modifier))
2009 return main_to_ccs_plane(fb, main_plane);
a007138e
VS
2010 else if (INTEL_GEN(i915) < 11 &&
2011 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
2012 return 1;
2013 else
2014 return 0;
e7af9094
ID
2015}
2016
4941f35b
ID
2017bool
2018intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
2019 uint64_t modifier)
2020{
2021 return info->is_yuv &&
2022 info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2023}
2024
d156135e
ID
2025static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2026 int color_plane)
2027{
2028 return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2029 color_plane == 1;
2030}
2031
d88c4afd 2032static unsigned int
5d2a1950 2033intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
7b49f948 2034{
d88c4afd 2035 struct drm_i915_private *dev_priv = to_i915(fb->dev);
5d2a1950 2036 unsigned int cpp = fb->format->cpp[color_plane];
d88c4afd
VS
2037
2038 switch (fb->modifier) {
2f075565 2039 case DRM_FORMAT_MOD_LINEAR:
54d4d719 2040 return intel_tile_size(dev_priv);
7b49f948 2041 case I915_FORMAT_MOD_X_TILED:
cf819eff 2042 if (IS_GEN(dev_priv, 2))
7b49f948
VS
2043 return 128;
2044 else
2045 return 512;
2e2adb05 2046 case I915_FORMAT_MOD_Y_TILED_CCS:
e7af9094 2047 if (is_ccs_plane(fb, color_plane))
2e2adb05 2048 return 128;
df561f66 2049 fallthrough;
b3e57bcc 2050 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2dfbf9d2 2051 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
b3e57bcc
DP
2052 if (is_ccs_plane(fb, color_plane))
2053 return 64;
df561f66 2054 fallthrough;
7b49f948 2055 case I915_FORMAT_MOD_Y_TILED:
cf819eff 2056 if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
7b49f948
VS
2057 return 128;
2058 else
2059 return 512;
2e2adb05 2060 case I915_FORMAT_MOD_Yf_TILED_CCS:
e7af9094 2061 if (is_ccs_plane(fb, color_plane))
2e2adb05 2062 return 128;
df561f66 2063 fallthrough;
7b49f948
VS
2064 case I915_FORMAT_MOD_Yf_TILED:
2065 switch (cpp) {
2066 case 1:
2067 return 64;
2068 case 2:
2069 case 4:
2070 return 128;
2071 case 8:
2072 case 16:
2073 return 256;
2074 default:
2075 MISSING_CASE(cpp);
2076 return cpp;
2077 }
2078 break;
2079 default:
d88c4afd 2080 MISSING_CASE(fb->modifier);
7b49f948
VS
2081 return cpp;
2082 }
2083}
2084
d88c4afd 2085static unsigned int
5d2a1950 2086intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
a57ce0b2 2087{
b3e57bcc
DP
2088 if (is_gen12_ccs_plane(fb, color_plane))
2089 return 1;
2090
54d4d719
VS
2091 return intel_tile_size(to_i915(fb->dev)) /
2092 intel_tile_width_bytes(fb, color_plane);
6761dd31
TU
2093}
2094
8d0deca8 2095/* Return the tile dimensions in pixel units */
5d2a1950 2096static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
8d0deca8 2097 unsigned int *tile_width,
d88c4afd 2098 unsigned int *tile_height)
8d0deca8 2099{
5d2a1950
VS
2100 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2101 unsigned int cpp = fb->format->cpp[color_plane];
8d0deca8
VS
2102
2103 *tile_width = tile_width_bytes / cpp;
021a4116 2104 *tile_height = intel_tile_height(fb, color_plane);
8d0deca8
VS
2105}
2106
d156135e
ID
2107static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2108 int color_plane)
2109{
2110 unsigned int tile_width, tile_height;
2111
2112 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2113
2114 return fb->pitches[color_plane] * tile_height;
2115}
2116
6761dd31 2117unsigned int
d88c4afd 2118intel_fb_align_height(const struct drm_framebuffer *fb,
5d2a1950 2119 int color_plane, unsigned int height)
6761dd31 2120{
5d2a1950 2121 unsigned int tile_height = intel_tile_height(fb, color_plane);
832be82f
VS
2122
2123 return ALIGN(height, tile_height);
a57ce0b2
JB
2124}
2125
1663b9d6
VS
2126unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127{
2128 unsigned int size = 0;
2129 int i;
2130
2131 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134 return size;
2135}
2136
1a74fc0b
VS
2137unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2138{
2139 unsigned int size = 0;
2140 int i;
2141
2142 for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2143 size += rem_info->plane[i].width * rem_info->plane[i].height;
2144
2145 return size;
2146}
2147
75c82a53 2148static void
3465c580
VS
2149intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2150 const struct drm_framebuffer *fb,
2151 unsigned int rotation)
f64b98cd 2152{
7b92c047 2153 view->type = I915_GGTT_VIEW_NORMAL;
bd2ef25d 2154 if (drm_rotation_90_or_270(rotation)) {
7b92c047 2155 view->type = I915_GGTT_VIEW_ROTATED;
8bab1193 2156 view->rotated = to_intel_framebuffer(fb)->rot_info;
2d7a215f
VS
2157 }
2158}
50470bb0 2159
fabac484
VS
2160static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2161{
2162 if (IS_I830(dev_priv))
2163 return 16 * 1024;
2164 else if (IS_I85X(dev_priv))
2165 return 256;
d9e1551e
VS
2166 else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2167 return 32;
fabac484
VS
2168 else
2169 return 4 * 1024;
2170}
2171
603525d7 2172static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6 2173{
c56b89f1 2174 if (INTEL_GEN(dev_priv) >= 9)
4e9a86b6 2175 return 256 * 1024;
c0f86832 2176 else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
666a4537 2177 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6 2178 return 128 * 1024;
c56b89f1 2179 else if (INTEL_GEN(dev_priv) >= 4)
4e9a86b6
VS
2180 return 4 * 1024;
2181 else
44c5905e 2182 return 0;
4e9a86b6
VS
2183}
2184
d88c4afd 2185static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
5d2a1950 2186 int color_plane)
603525d7 2187{
d88c4afd
VS
2188 struct drm_i915_private *dev_priv = to_i915(fb->dev);
2189
b90c1ee1 2190 /* AUX_DIST needs only 4K alignment */
d156135e
ID
2191 if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2192 is_ccs_plane(fb, color_plane))
b90c1ee1
VS
2193 return 4096;
2194
d88c4afd 2195 switch (fb->modifier) {
2f075565 2196 case DRM_FORMAT_MOD_LINEAR:
603525d7
VS
2197 return intel_linear_alignment(dev_priv);
2198 case I915_FORMAT_MOD_X_TILED:
d88c4afd 2199 if (INTEL_GEN(dev_priv) >= 9)
603525d7
VS
2200 return 256 * 1024;
2201 return 0;
2dfbf9d2
DP
2202 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2203 if (is_semiplanar_uv_plane(fb, color_plane))
2204 return intel_tile_row_size(fb, color_plane);
df561f66 2205 fallthrough;
b3e57bcc
DP
2206 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2207 return 16 * 1024;
2e2adb05
VS
2208 case I915_FORMAT_MOD_Y_TILED_CCS:
2209 case I915_FORMAT_MOD_Yf_TILED_CCS:
603525d7 2210 case I915_FORMAT_MOD_Y_TILED:
d156135e
ID
2211 if (INTEL_GEN(dev_priv) >= 12 &&
2212 is_semiplanar_uv_plane(fb, color_plane))
2213 return intel_tile_row_size(fb, color_plane);
df561f66 2214 fallthrough;
603525d7
VS
2215 case I915_FORMAT_MOD_Yf_TILED:
2216 return 1 * 1024 * 1024;
2217 default:
d88c4afd 2218 MISSING_CASE(fb->modifier);
603525d7
VS
2219 return 0;
2220 }
2221}
2222
f7a02ad7
VS
2223static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2224{
f90a85e7 2225 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
f7a02ad7
VS
2226 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2227
bb211c3d
VS
2228 return INTEL_GEN(dev_priv) < 4 ||
2229 (plane->has_fbc &&
2230 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
f7a02ad7
VS
2231}
2232
058d88c4 2233struct i915_vma *
5935485f 2234intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
f5929c53 2235 const struct i915_ggtt_view *view,
f7a02ad7 2236 bool uses_fence,
5935485f 2237 unsigned long *out_flags)
6b95a207 2238{
850c4cdc 2239 struct drm_device *dev = fb->dev;
fac5e23e 2240 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2241 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1d264d91 2242 intel_wakeref_t wakeref;
058d88c4 2243 struct i915_vma *vma;
5935485f 2244 unsigned int pinctl;
6b95a207 2245 u32 alignment;
6b95a207 2246
e57291c2 2247 if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
5a90606d 2248 return ERR_PTR(-EINVAL);
ebcdd39e 2249
d88c4afd 2250 alignment = intel_surf_alignment(fb, 0);
e57291c2 2251 if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
7361bdb2 2252 return ERR_PTR(-EINVAL);
6b95a207 2253
693db184
CW
2254 /* Note that the w/a also requires 64 PTE of padding following the
2255 * bo. We currently fill all unused PTE with the shadow page and so
2256 * we should always have valid PTE following the scanout preventing
2257 * the VT-d warning.
2258 */
48f112fe 2259 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2260 alignment = 256 * 1024;
2261
d6dd6843
PZ
2262 /*
2263 * Global gtt pte registers are special registers which actually forward
2264 * writes to a chunk of system memory. Which means that there is no risk
2265 * that the register values disappear as soon as we call
2266 * intel_runtime_pm_put(), so it is correct to wrap only the
2267 * pin/unpin/fence and not more.
2268 */
d858d569 2269 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
d6dd6843 2270
9db529aa
DV
2271 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2272
8b1c78e0
CW
2273 /*
2274 * Valleyview is definitely limited to scanning out the first
5935485f
CW
2275 * 512MiB. Lets presume this behaviour was inherited from the
2276 * g4x display engine and that all earlier gen are similarly
2277 * limited. Testing suggests that it is a little more
2278 * complicated than this. For example, Cherryview appears quite
2279 * happy to scanout from anywhere within its global aperture.
2280 */
8b1c78e0 2281 pinctl = 0;
b2ae318a 2282 if (HAS_GMCH(dev_priv))
5935485f
CW
2283 pinctl |= PIN_MAPPABLE;
2284
2285 vma = i915_gem_object_pin_to_display_plane(obj,
f5929c53 2286 alignment, view, pinctl);
49ef5294
CW
2287 if (IS_ERR(vma))
2288 goto err;
6b95a207 2289
f7a02ad7 2290 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
85798ac9
VS
2291 int ret;
2292
8b1c78e0
CW
2293 /*
2294 * Install a fence for tiled scan-out. Pre-i965 always needs a
49ef5294
CW
2295 * fence, whereas 965+ only requires a fence if using
2296 * framebuffer compression. For simplicity, we always, when
2297 * possible, install a fence as the cost is not that onerous.
2298 *
2299 * If we fail to fence the tiled scanout, then either the
2300 * modeset will reject the change (which is highly unlikely as
2301 * the affected systems, all but one, do not have unmappable
2302 * space) or we will not be able to enable full powersaving
2303 * techniques (also likely not to apply due to various limits
2304 * FBC and the like impose on the size of the buffer, which
2305 * presumably we violated anyway with this unmappable buffer).
2306 * Anyway, it is presumably better to stumble onwards with
2307 * something and try to run the system in a "less than optimal"
2308 * mode that matches the user configuration.
2309 */
85798ac9
VS
2310 ret = i915_vma_pin_fence(vma);
2311 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
7509702b 2312 i915_gem_object_unpin_from_display_plane(vma);
85798ac9
VS
2313 vma = ERR_PTR(ret);
2314 goto err;
2315 }
2316
2317 if (ret == 0 && vma->fence)
5935485f 2318 *out_flags |= PLANE_HAS_FENCE;
9807216f 2319 }
6b95a207 2320
be1e3415 2321 i915_vma_get(vma);
49ef5294 2322err:
9db529aa 2323 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
d858d569 2324 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
058d88c4 2325 return vma;
6b95a207
KH
2326}
2327
5935485f 2328void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
1690e1eb 2329{
80f0b679 2330 i915_gem_object_lock(vma->obj, NULL);
5935485f
CW
2331 if (flags & PLANE_HAS_FENCE)
2332 i915_vma_unpin_fence(vma);
058d88c4 2333 i915_gem_object_unpin_from_display_plane(vma);
6951e589
CW
2334 i915_gem_object_unlock(vma->obj);
2335
be1e3415 2336 i915_vma_put(vma);
1690e1eb
CW
2337}
2338
5d2a1950 2339static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
ef78ec94
VS
2340 unsigned int rotation)
2341{
bd2ef25d 2342 if (drm_rotation_90_or_270(rotation))
5d2a1950 2343 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
ef78ec94 2344 else
5d2a1950 2345 return fb->pitches[color_plane];
ef78ec94
VS
2346}
2347
6687c906
VS
2348/*
2349 * Convert the x/y offsets into a linear offset.
2350 * Only valid with 0/180 degree rotation, which is fine since linear
2351 * offset is only used with linear buffers on pre-hsw and tiled buffers
2352 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2353 */
2354u32 intel_fb_xy_to_linear(int x, int y,
2949056c 2355 const struct intel_plane_state *state,
5d2a1950 2356 int color_plane)
6687c906 2357{
7b3cb17a 2358 const struct drm_framebuffer *fb = state->hw.fb;
5d2a1950
VS
2359 unsigned int cpp = fb->format->cpp[color_plane];
2360 unsigned int pitch = state->color_plane[color_plane].stride;
6687c906
VS
2361
2362 return y * pitch + x * cpp;
2363}
2364
2365/*
2366 * Add the x/y offsets derived from fb->offsets[] to the user
2367 * specified plane src x/y offsets. The resulting x/y offsets
2368 * specify the start of scanout from the beginning of the gtt mapping.
2369 */
2370void intel_add_fb_offsets(int *x, int *y,
2949056c 2371 const struct intel_plane_state *state,
5d2a1950 2372 int color_plane)
6687c906
VS
2373
2374{
54d4d719
VS
2375 *x += state->color_plane[color_plane].x;
2376 *y += state->color_plane[color_plane].y;
6687c906
VS
2377}
2378
6d19a44c
VS
2379static u32 intel_adjust_tile_offset(int *x, int *y,
2380 unsigned int tile_width,
2381 unsigned int tile_height,
2382 unsigned int tile_size,
2383 unsigned int pitch_tiles,
2384 u32 old_offset,
2385 u32 new_offset)
29cf9491 2386{
b9b24038 2387 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2388 unsigned int tiles;
2389
2390 WARN_ON(old_offset & (tile_size - 1));
2391 WARN_ON(new_offset & (tile_size - 1));
2392 WARN_ON(new_offset > old_offset);
2393
2394 tiles = (old_offset - new_offset) / tile_size;
2395
2396 *y += tiles / pitch_tiles * tile_height;
2397 *x += tiles % pitch_tiles * tile_width;
2398
b9b24038
VS
2399 /* minimize x in case it got needlessly big */
2400 *y += *x / pitch_pixels * tile_height;
2401 *x %= pitch_pixels;
2402
29cf9491
VS
2403 return new_offset;
2404}
2405
b3e57bcc 2406static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2a11b1b4 2407{
b3e57bcc
DP
2408 return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2409 is_gen12_ccs_plane(fb, color_plane);
2a11b1b4
DP
2410}
2411
6d19a44c 2412static u32 intel_adjust_aligned_offset(int *x, int *y,
5d2a1950
VS
2413 const struct drm_framebuffer *fb,
2414 int color_plane,
6d19a44c 2415 unsigned int rotation,
df79cf44 2416 unsigned int pitch,
6d19a44c 2417 u32 old_offset, u32 new_offset)
66a2d927 2418{
6d19a44c 2419 struct drm_i915_private *dev_priv = to_i915(fb->dev);
5d2a1950 2420 unsigned int cpp = fb->format->cpp[color_plane];
66a2d927 2421
e57291c2 2422 drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
66a2d927 2423
b3e57bcc 2424 if (!is_surface_linear(fb, color_plane)) {
66a2d927
VS
2425 unsigned int tile_size, tile_width, tile_height;
2426 unsigned int pitch_tiles;
2427
2428 tile_size = intel_tile_size(dev_priv);
5d2a1950 2429 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
66a2d927 2430
bd2ef25d 2431 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2432 pitch_tiles = pitch / tile_height;
2433 swap(tile_width, tile_height);
2434 } else {
2435 pitch_tiles = pitch / (tile_width * cpp);
2436 }
2437
6d19a44c
VS
2438 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2439 tile_size, pitch_tiles,
2440 old_offset, new_offset);
66a2d927
VS
2441 } else {
2442 old_offset += *y * pitch + *x * cpp;
2443
2444 *y = (old_offset - new_offset) / pitch;
2445 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2446 }
2447
2448 return new_offset;
2449}
2450
303ba695
VS
2451/*
2452 * Adjust the tile offset by moving the difference into
2453 * the x/y offsets.
2454 */
6d19a44c
VS
2455static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2456 const struct intel_plane_state *state,
5d2a1950 2457 int color_plane,
6d19a44c 2458 u32 old_offset, u32 new_offset)
303ba695 2459{
7b3cb17a
ML
2460 return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2461 state->hw.rotation,
5d2a1950 2462 state->color_plane[color_plane].stride,
6d19a44c 2463 old_offset, new_offset);
303ba695
VS
2464}
2465
8d0deca8 2466/*
6d19a44c 2467 * Computes the aligned offset to the base tile and adjusts
8d0deca8
VS
2468 * x, y. bytes per pixel is assumed to be a power-of-two.
2469 *
2470 * In the 90/270 rotated case, x and y are assumed
2471 * to be already rotated to match the rotated GTT view, and
2472 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2473 *
2474 * This function is used when computing the derived information
2475 * under intel_framebuffer, so using any of that information
2476 * here is not allowed. Anything under drm_framebuffer can be
2477 * used. This is why the user has to pass in the pitch since it
2478 * is specified in the rotated orientation.
8d0deca8 2479 */
6d19a44c
VS
2480static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2481 int *x, int *y,
5d2a1950
VS
2482 const struct drm_framebuffer *fb,
2483 int color_plane,
6d19a44c
VS
2484 unsigned int pitch,
2485 unsigned int rotation,
2486 u32 alignment)
c2c75131 2487{
5d2a1950 2488 unsigned int cpp = fb->format->cpp[color_plane];
6687c906 2489 u32 offset, offset_aligned;
29cf9491 2490
b3e57bcc 2491 if (!is_surface_linear(fb, color_plane)) {
8d0deca8
VS
2492 unsigned int tile_size, tile_width, tile_height;
2493 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2494
d843310d 2495 tile_size = intel_tile_size(dev_priv);
5d2a1950 2496 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
8d0deca8 2497
bd2ef25d 2498 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2499 pitch_tiles = pitch / tile_height;
2500 swap(tile_width, tile_height);
2501 } else {
2502 pitch_tiles = pitch / (tile_width * cpp);
2503 }
d843310d
VS
2504
2505 tile_rows = *y / tile_height;
2506 *y %= tile_height;
c2c75131 2507
8d0deca8
VS
2508 tiles = *x / tile_width;
2509 *x %= tile_width;
bc752862 2510
29cf9491 2511 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
7361bdb2
ID
2512
2513 offset_aligned = offset;
2514 if (alignment)
2515 offset_aligned = rounddown(offset_aligned, alignment);
bc752862 2516
6d19a44c
VS
2517 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2518 tile_size, pitch_tiles,
2519 offset, offset_aligned);
29cf9491 2520 } else {
bc752862 2521 offset = *y * pitch + *x * cpp;
7361bdb2
ID
2522 offset_aligned = offset;
2523 if (alignment) {
2524 offset_aligned = rounddown(offset_aligned, alignment);
2525 *y = (offset % alignment) / pitch;
2526 *x = ((offset % alignment) - *y * pitch) / cpp;
2527 } else {
2528 *y = *x = 0;
2529 }
bc752862 2530 }
29cf9491
VS
2531
2532 return offset_aligned;
c2c75131
DV
2533}
2534
6d19a44c
VS
2535static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2536 const struct intel_plane_state *state,
5d2a1950 2537 int color_plane)
6687c906 2538{
f90a85e7 2539 struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
1e7b4fd8 2540 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
7b3cb17a
ML
2541 const struct drm_framebuffer *fb = state->hw.fb;
2542 unsigned int rotation = state->hw.rotation;
5d2a1950 2543 int pitch = state->color_plane[color_plane].stride;
1e7b4fd8
VS
2544 u32 alignment;
2545
2546 if (intel_plane->id == PLANE_CURSOR)
2547 alignment = intel_cursor_alignment(dev_priv);
2548 else
5d2a1950 2549 alignment = intel_surf_alignment(fb, color_plane);
6687c906 2550
5d2a1950 2551 return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
6d19a44c 2552 pitch, rotation, alignment);
6687c906
VS
2553}
2554
303ba695
VS
2555/* Convert the fb->offset[] into x/y offsets */
2556static int intel_fb_offset_to_xy(int *x, int *y,
5d2a1950
VS
2557 const struct drm_framebuffer *fb,
2558 int color_plane)
6687c906 2559{
303ba695 2560 struct drm_i915_private *dev_priv = to_i915(fb->dev);
70bbe53c 2561 unsigned int height;
d156135e
ID
2562 u32 alignment;
2563
2564 if (INTEL_GEN(dev_priv) >= 12 &&
2565 is_semiplanar_uv_plane(fb, color_plane))
2566 alignment = intel_tile_row_size(fb, color_plane);
2567 else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2568 alignment = intel_tile_size(dev_priv);
2569 else
2570 alignment = 0;
6687c906 2571
d156135e 2572 if (alignment != 0 && fb->offsets[color_plane] % alignment) {
cd49f818
WK
2573 drm_dbg_kms(&dev_priv->drm,
2574 "Misaligned offset 0x%08x for color plane %d\n",
2575 fb->offsets[color_plane], color_plane);
303ba695 2576 return -EINVAL;
70bbe53c
VS
2577 }
2578
2579 height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2580 height = ALIGN(height, intel_tile_height(fb, color_plane));
2581
2582 /* Catch potential overflows early */
2583 if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2584 fb->offsets[color_plane])) {
cd49f818
WK
2585 drm_dbg_kms(&dev_priv->drm,
2586 "Bad offset 0x%08x or pitch %d for color plane %d\n",
2587 fb->offsets[color_plane], fb->pitches[color_plane],
2588 color_plane);
70bbe53c
VS
2589 return -ERANGE;
2590 }
303ba695
VS
2591
2592 *x = 0;
2593 *y = 0;
2594
6d19a44c 2595 intel_adjust_aligned_offset(x, y,
5d2a1950
VS
2596 fb, color_plane, DRM_MODE_ROTATE_0,
2597 fb->pitches[color_plane],
2598 fb->offsets[color_plane], 0);
303ba695
VS
2599
2600 return 0;
6687c906
VS
2601}
2602
ba3f4d0a 2603static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
72618ebf
VS
2604{
2605 switch (fb_modifier) {
2606 case I915_FORMAT_MOD_X_TILED:
2607 return I915_TILING_X;
2608 case I915_FORMAT_MOD_Y_TILED:
2e2adb05 2609 case I915_FORMAT_MOD_Y_TILED_CCS:
b3e57bcc 2610 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2dfbf9d2 2611 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
72618ebf
VS
2612 return I915_TILING_Y;
2613 default:
2614 return I915_TILING_NONE;
2615 }
2616}
2617
16af25fa
VS
2618/*
2619 * From the Sky Lake PRM:
2620 * "The Color Control Surface (CCS) contains the compression status of
2621 * the cache-line pairs. The compression state of the cache-line pair
2622 * is specified by 2 bits in the CCS. Each CCS cache-line represents
2623 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2624 * cache-line-pairs. CCS is always Y tiled."
2625 *
2626 * Since cache line pairs refers to horizontally adjacent cache lines,
2627 * each cache line in the CCS corresponds to an area of 32x16 cache
2628 * lines on the main surface. Since each pixel is 4 bytes, this gives
2629 * us a ratio of one byte in the CCS for each 8x16 pixels in the
2630 * main surface.
2631 */
b3e57bcc 2632static const struct drm_format_info skl_ccs_formats[] = {
38f30041
VS
2633 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2634 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2635 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2636 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2637 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2638 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2639 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2640 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
bbfb6ce8
VS
2641};
2642
b3e57bcc
DP
2643/*
2644 * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2645 * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2646 * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2647 * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2648 * the main surface.
2649 */
2650static const struct drm_format_info gen12_ccs_formats[] = {
2651 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2652 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2653 .hsub = 1, .vsub = 1, },
2654 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2655 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2656 .hsub = 1, .vsub = 1, },
2657 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2658 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2659 .hsub = 1, .vsub = 1, .has_alpha = true },
2660 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2661 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2662 .hsub = 1, .vsub = 1, .has_alpha = true },
2dfbf9d2
DP
2663 { .format = DRM_FORMAT_YUYV, .num_planes = 2,
2664 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2665 .hsub = 2, .vsub = 1, .is_yuv = true },
2666 { .format = DRM_FORMAT_YVYU, .num_planes = 2,
2667 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2668 .hsub = 2, .vsub = 1, .is_yuv = true },
2669 { .format = DRM_FORMAT_UYVY, .num_planes = 2,
2670 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2671 .hsub = 2, .vsub = 1, .is_yuv = true },
2672 { .format = DRM_FORMAT_VYUY, .num_planes = 2,
2673 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2674 .hsub = 2, .vsub = 1, .is_yuv = true },
2675 { .format = DRM_FORMAT_NV12, .num_planes = 4,
2676 .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2677 .hsub = 2, .vsub = 2, .is_yuv = true },
2678 { .format = DRM_FORMAT_P010, .num_planes = 4,
2679 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2680 .hsub = 2, .vsub = 2, .is_yuv = true },
2681 { .format = DRM_FORMAT_P012, .num_planes = 4,
2682 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2683 .hsub = 2, .vsub = 2, .is_yuv = true },
2684 { .format = DRM_FORMAT_P016, .num_planes = 4,
2685 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2686 .hsub = 2, .vsub = 2, .is_yuv = true },
b3e57bcc
DP
2687};
2688
bbfb6ce8
VS
2689static const struct drm_format_info *
2690lookup_format_info(const struct drm_format_info formats[],
2691 int num_formats, u32 format)
2692{
2693 int i;
2694
2695 for (i = 0; i < num_formats; i++) {
2696 if (formats[i].format == format)
2697 return &formats[i];
2698 }
2699
2700 return NULL;
2701}
2702
2703static const struct drm_format_info *
2704intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2705{
2706 switch (cmd->modifier[0]) {
2707 case I915_FORMAT_MOD_Y_TILED_CCS:
2708 case I915_FORMAT_MOD_Yf_TILED_CCS:
b3e57bcc
DP
2709 return lookup_format_info(skl_ccs_formats,
2710 ARRAY_SIZE(skl_ccs_formats),
2711 cmd->pixel_format);
2712 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2dfbf9d2 2713 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
b3e57bcc
DP
2714 return lookup_format_info(gen12_ccs_formats,
2715 ARRAY_SIZE(gen12_ccs_formats),
bbfb6ce8
VS
2716 cmd->pixel_format);
2717 default:
2718 return NULL;
2719 }
2720}
2721
63eaf9ac
DP
2722bool is_ccs_modifier(u64 modifier)
2723{
b3e57bcc 2724 return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2dfbf9d2 2725 modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
b3e57bcc 2726 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
63eaf9ac
DP
2727 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2728}
2729
71df86f0
ID
2730static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2731{
2732 return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2733 512) * 64;
2734}
2735
54d4d719
VS
2736u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2737 u32 pixel_format, u64 modifier)
a88c40eb
VS
2738{
2739 struct intel_crtc *crtc;
2740 struct intel_plane *plane;
2741
2742 /*
2743 * We assume the primary plane for pipe A has
eae3da27
AG
2744 * the highest stride limits of them all,
2745 * if in case pipe A is disabled, use the first pipe from pipe_mask.
a88c40eb 2746 */
eae3da27 2747 crtc = intel_get_first_crtc(dev_priv);
baea9ffe
VS
2748 if (!crtc)
2749 return 0;
2750
a88c40eb
VS
2751 plane = to_intel_plane(crtc->base.primary);
2752
2753 return plane->max_stride(plane, pixel_format, modifier,
2754 DRM_MODE_ROTATE_0);
2755}
2756
54d4d719
VS
2757static
2758u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2759 u32 pixel_format, u64 modifier)
2760{
20330129
VS
2761 /*
2762 * Arbitrary limit for gen4+ chosen to match the
2763 * render engine max stride.
2764 *
2765 * The new CCS hash mode makes remapping impossible
2766 */
2767 if (!is_ccs_modifier(modifier)) {
2768 if (INTEL_GEN(dev_priv) >= 7)
2769 return 256*1024;
2770 else if (INTEL_GEN(dev_priv) >= 4)
2771 return 128*1024;
2772 }
2773
54d4d719
VS
2774 return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2775}
2776
a88c40eb
VS
2777static u32
2778intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2779{
54d4d719 2780 struct drm_i915_private *dev_priv = to_i915(fb->dev);
b3e57bcc 2781 u32 tile_width;
54d4d719 2782
b3e57bcc 2783 if (is_surface_linear(fb, color_plane)) {
54d4d719
VS
2784 u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2785 fb->format->format,
2786 fb->modifier);
2787
2788 /*
2789 * To make remapping with linear generally feasible
2790 * we need the stride to be page aligned.
2791 */
b3e57bcc
DP
2792 if (fb->pitches[color_plane] > max_stride &&
2793 !is_ccs_modifier(fb->modifier))
54d4d719
VS
2794 return intel_tile_size(dev_priv);
2795 else
2796 return 64;
b3e57bcc 2797 }
86f236bb 2798
b3e57bcc 2799 tile_width = intel_tile_width_bytes(fb, color_plane);
2dfbf9d2 2800 if (is_ccs_modifier(fb->modifier)) {
86f236bb
DP
2801 /*
2802 * Display WA #0531: skl,bxt,kbl,glk
2803 *
2804 * Render decompression and plane width > 3840
2805 * combined with horizontal panning requires the
2806 * plane stride to be a multiple of 4. We'll just
2807 * require the entire fb to accommodate that to avoid
2808 * potential runtime errors at plane configuration time.
2809 */
2dfbf9d2 2810 if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
b3e57bcc
DP
2811 tile_width *= 4;
2812 /*
2813 * The main surface pitch must be padded to a multiple of four
2814 * tile widths.
2815 */
2816 else if (INTEL_GEN(dev_priv) >= 12)
86f236bb 2817 tile_width *= 4;
54d4d719 2818 }
b3e57bcc 2819 return tile_width;
54d4d719
VS
2820}
2821
2822bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2823{
f90a85e7 2824 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
54d4d719 2825 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a 2826 const struct drm_framebuffer *fb = plane_state->hw.fb;
54d4d719
VS
2827 int i;
2828
2829 /* We don't want to deal with remapping with cursors */
2830 if (plane->id == PLANE_CURSOR)
2831 return false;
2832
2833 /*
2834 * The display engine limits already match/exceed the
2835 * render engine limits, so not much point in remapping.
2836 * Would also need to deal with the fence POT alignment
2837 * and gen2 2KiB GTT tile size.
2838 */
2839 if (INTEL_GEN(dev_priv) < 4)
2840 return false;
2841
2842 /*
2843 * The new CCS hash mode isn't compatible with remapping as
2844 * the virtual address of the pages affects the compressed data.
2845 */
2846 if (is_ccs_modifier(fb->modifier))
2847 return false;
2848
2849 /* Linear needs a page aligned stride for remapping */
2850 if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2851 unsigned int alignment = intel_tile_size(dev_priv) - 1;
2852
2853 for (i = 0; i < fb->format->num_planes; i++) {
2854 if (fb->pitches[i] & alignment)
2855 return false;
2856 }
2857 }
2858
2859 return true;
2860}
2861
2862static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2863{
f90a85e7 2864 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
7b3cb17a
ML
2865 const struct drm_framebuffer *fb = plane_state->hw.fb;
2866 unsigned int rotation = plane_state->hw.rotation;
54d4d719
VS
2867 u32 stride, max_stride;
2868
2869 /*
2870 * No remapping for invisible planes since we don't have
2871 * an actual source viewport to remap.
2872 */
f90a85e7 2873 if (!plane_state->uapi.visible)
54d4d719
VS
2874 return false;
2875
2876 if (!intel_plane_can_remap(plane_state))
2877 return false;
2878
2879 /*
2880 * FIXME: aux plane limits on gen9+ are
2881 * unclear in Bspec, for now no checking.
2882 */
2883 stride = intel_fb_pitch(fb, 0, rotation);
2884 max_stride = plane->max_stride(plane, fb->format->format,
2885 fb->modifier, rotation);
2886
2887 return stride > max_stride;
a88c40eb
VS
2888}
2889
b3e57bcc
DP
2890static void
2891intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2892 const struct drm_framebuffer *fb,
2893 int color_plane)
2894{
2895 int main_plane;
2896
2897 if (color_plane == 0) {
2898 *hsub = 1;
2899 *vsub = 1;
2900
2901 return;
2902 }
2903
2904 /*
2905 * TODO: Deduct the subsampling from the char block for all CCS
2906 * formats and planes.
2907 */
2908 if (!is_gen12_ccs_plane(fb, color_plane)) {
2909 *hsub = fb->format->hsub;
2910 *vsub = fb->format->vsub;
2911
2912 return;
2913 }
2914
2915 main_plane = ccs_to_main_plane(fb, color_plane);
2916 *hsub = drm_format_info_block_width(fb->format, color_plane) /
2917 drm_format_info_block_width(fb->format, main_plane);
2918
2919 /*
2920 * The min stride check in the core framebuffer_check() function
2921 * assumes that format->hsub applies to every plane except for the
2922 * first plane. That's incorrect for the CCS AUX plane of the first
2923 * plane, but for the above check to pass we must define the block
2924 * width with that subsampling applied to it. Adjust the width here
2925 * accordingly, so we can calculate the actual subsampling factor.
2926 */
2927 if (main_plane == 0)
2928 *hsub *= fb->format->hsub;
2929
2930 *vsub = 32;
2931}
13f2cb9a
DP
2932static int
2933intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2934{
3dfd8d71 2935 struct drm_i915_private *i915 = to_i915(fb->dev);
13f2cb9a 2936 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
b3e57bcc
DP
2937 int main_plane;
2938 int hsub, vsub;
13f2cb9a
DP
2939 int tile_width, tile_height;
2940 int ccs_x, ccs_y;
2941 int main_x, main_y;
2942
b3e57bcc 2943 if (!is_ccs_plane(fb, ccs_plane))
13f2cb9a
DP
2944 return 0;
2945
b3e57bcc
DP
2946 intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2947 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
13f2cb9a
DP
2948
2949 tile_width *= hsub;
2950 tile_height *= vsub;
2951
2952 ccs_x = (x * hsub) % tile_width;
2953 ccs_y = (y * vsub) % tile_height;
b3e57bcc
DP
2954
2955 main_plane = ccs_to_main_plane(fb, ccs_plane);
2956 main_x = intel_fb->normal[main_plane].x % tile_width;
2957 main_y = intel_fb->normal[main_plane].y % tile_height;
13f2cb9a
DP
2958
2959 /*
2960 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2961 * x/y offsets must match between CCS and the main surface.
2962 */
2963 if (main_x != ccs_x || main_y != ccs_y) {
3dfd8d71
JN
2964 drm_dbg_kms(&i915->drm,
2965 "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
13f2cb9a
DP
2966 main_x, main_y,
2967 ccs_x, ccs_y,
b3e57bcc
DP
2968 intel_fb->normal[main_plane].x,
2969 intel_fb->normal[main_plane].y,
13f2cb9a
DP
2970 x, y);
2971 return -EINVAL;
2972 }
2973
2974 return 0;
2975}
2976
b3e57bcc
DP
2977static void
2978intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2979{
577687bf
ID
2980 int main_plane = is_ccs_plane(fb, color_plane) ?
2981 ccs_to_main_plane(fb, color_plane) : 0;
2982 int main_hsub, main_vsub;
b3e57bcc
DP
2983 int hsub, vsub;
2984
577687bf 2985 intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
b3e57bcc 2986 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
577687bf
ID
2987 *w = fb->width / main_hsub / hsub;
2988 *h = fb->height / main_vsub / vsub;
b3e57bcc
DP
2989}
2990
931cd348
DP
2991/*
2992 * Setup the rotated view for an FB plane and return the size the GTT mapping
2993 * requires for this view.
2994 */
2995static u32
2996setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2997 u32 gtt_offset_rotated, int x, int y,
2998 unsigned int width, unsigned int height,
2999 unsigned int tile_size,
3000 unsigned int tile_width, unsigned int tile_height,
3001 struct drm_framebuffer *fb)
3002{
3003 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3004 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
3005 unsigned int pitch_tiles;
3006 struct drm_rect r;
3007
3008 /* Y or Yf modifiers required for 90/270 rotation */
3009 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3010 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
3011 return 0;
3012
ce04ecd9 3013 if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
931cd348
DP
3014 return 0;
3015
3016 rot_info->plane[plane] = *plane_info;
3017
3018 intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
3019
3020 /* rotate the x/y offsets to match the GTT view */
3021 drm_rect_init(&r, x, y, width, height);
3022 drm_rect_rotate(&r,
3023 plane_info->width * tile_width,
3024 plane_info->height * tile_height,
3025 DRM_MODE_ROTATE_270);
3026 x = r.x1;
3027 y = r.y1;
3028
3029 /* rotate the tile dimensions to match the GTT view */
3030 pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3031 swap(tile_width, tile_height);
3032
3033 /*
3034 * We only keep the x/y offsets, so push all of the
3035 * gtt offset into the x/y offsets.
3036 */
3037 intel_adjust_tile_offset(&x, &y,
3038 tile_width, tile_height,
3039 tile_size, pitch_tiles,
3040 gtt_offset_rotated * tile_size, 0);
3041
3042 /*
3043 * First pixel of the framebuffer from
3044 * the start of the rotated gtt mapping.
3045 */
3046 intel_fb->rotated[plane].x = x;
3047 intel_fb->rotated[plane].y = y;
3048
3049 return plane_info->width * plane_info->height;
3050}
3051
6687c906
VS
3052static int
3053intel_fill_fb_info(struct drm_i915_private *dev_priv,
3054 struct drm_framebuffer *fb)
3055{
3056 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
a5ff7a45 3057 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6687c906
VS
3058 u32 gtt_offset_rotated = 0;
3059 unsigned int max_size = 0;
bcb0b461 3060 int i, num_planes = fb->format->num_planes;
6687c906
VS
3061 unsigned int tile_size = intel_tile_size(dev_priv);
3062
3063 for (i = 0; i < num_planes; i++) {
3064 unsigned int width, height;
3065 unsigned int cpp, size;
3066 u32 offset;
3067 int x, y;
303ba695 3068 int ret;
6687c906 3069
353c8598 3070 cpp = fb->format->cpp[i];
b3e57bcc 3071 intel_fb_plane_dims(&width, &height, fb, i);
6687c906 3072
303ba695
VS
3073 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3074 if (ret) {
cd49f818
WK
3075 drm_dbg_kms(&dev_priv->drm,
3076 "bad fb plane %d offset: 0x%x\n",
3077 i, fb->offsets[i]);
303ba695
VS
3078 return ret;
3079 }
6687c906 3080
13f2cb9a
DP
3081 ret = intel_fb_check_ccs_xy(fb, i, x, y);
3082 if (ret)
3083 return ret;
2e2adb05 3084
60d5f2a4
VS
3085 /*
3086 * The fence (if used) is aligned to the start of the object
3087 * so having the framebuffer wrap around across the edge of the
3088 * fenced region doesn't really work. We have no API to configure
3089 * the fence start offset within the object (nor could we probably
3090 * on gen2/3). So it's just easier if we just require that the
3091 * fb layout agrees with the fence layout. We already check that the
3092 * fb stride matches the fence stride elsewhere.
3093 */
a5ff7a45 3094 if (i == 0 && i915_gem_object_is_tiled(obj) &&
60d5f2a4 3095 (x + width) * cpp > fb->pitches[i]) {
cd49f818
WK
3096 drm_dbg_kms(&dev_priv->drm,
3097 "bad fb plane %d offset: 0x%x\n",
3098 i, fb->offsets[i]);
60d5f2a4
VS
3099 return -EINVAL;
3100 }
3101
6687c906
VS
3102 /*
3103 * First pixel of the framebuffer from
3104 * the start of the normal gtt mapping.
3105 */
3106 intel_fb->normal[i].x = x;
3107 intel_fb->normal[i].y = y;
3108
6d19a44c
VS
3109 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3110 fb->pitches[i],
3111 DRM_MODE_ROTATE_0,
3112 tile_size);
6687c906
VS
3113 offset /= tile_size;
3114
b3e57bcc 3115 if (!is_surface_linear(fb, i)) {
931cd348 3116 struct intel_remapped_plane_info plane_info;
6687c906 3117 unsigned int tile_width, tile_height;
6687c906 3118
d88c4afd 3119 intel_tile_dims(fb, i, &tile_width, &tile_height);
6687c906 3120
931cd348
DP
3121 plane_info.offset = offset;
3122 plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3123 tile_width * cpp);
3124 plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3125 plane_info.height = DIV_ROUND_UP(y + height,
3126 tile_height);
6687c906
VS
3127
3128 /* how many tiles does this plane need */
931cd348 3129 size = plane_info.stride * plane_info.height;
6687c906
VS
3130 /*
3131 * If the plane isn't horizontally tile aligned,
3132 * we need one more tile.
3133 */
3134 if (x != 0)
3135 size++;
3136
931cd348
DP
3137 gtt_offset_rotated +=
3138 setup_fb_rotation(i, &plane_info,
3139 gtt_offset_rotated,
3140 x, y, width, height,
3141 tile_size,
3142 tile_width, tile_height,
3143 fb);
6687c906
VS
3144 } else {
3145 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3146 x * cpp, tile_size);
3147 }
3148
3149 /* how many tiles in total needed in the bo */
3150 max_size = max(max_size, offset + size);
3151 }
3152
4e05047d 3153 if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
cd49f818
WK
3154 drm_dbg_kms(&dev_priv->drm,
3155 "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3156 mul_u32_u32(max_size, tile_size), obj->base.size);
6687c906
VS
3157 return -EINVAL;
3158 }
3159
3160 return 0;
3161}
3162
54d4d719
VS
3163static void
3164intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3165{
3166 struct drm_i915_private *dev_priv =
f90a85e7 3167 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 3168 struct drm_framebuffer *fb = plane_state->hw.fb;
54d4d719
VS
3169 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3170 struct intel_rotation_info *info = &plane_state->view.rotated;
7b3cb17a 3171 unsigned int rotation = plane_state->hw.rotation;
54d4d719
VS
3172 int i, num_planes = fb->format->num_planes;
3173 unsigned int tile_size = intel_tile_size(dev_priv);
3174 unsigned int src_x, src_y;
3175 unsigned int src_w, src_h;
3176 u32 gtt_offset = 0;
3177
3178 memset(&plane_state->view, 0, sizeof(plane_state->view));
3179 plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3180 I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3181
f90a85e7
ML
3182 src_x = plane_state->uapi.src.x1 >> 16;
3183 src_y = plane_state->uapi.src.y1 >> 16;
3184 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3185 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
54d4d719 3186
e57291c2 3187 drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
54d4d719
VS
3188
3189 /* Make src coordinates relative to the viewport */
f90a85e7 3190 drm_rect_translate(&plane_state->uapi.src,
54d4d719
VS
3191 -(src_x << 16), -(src_y << 16));
3192
3193 /* Rotate src coordinates to match rotated GTT view */
3194 if (drm_rotation_90_or_270(rotation))
f90a85e7 3195 drm_rect_rotate(&plane_state->uapi.src,
54d4d719
VS
3196 src_w << 16, src_h << 16,
3197 DRM_MODE_ROTATE_270);
3198
3199 for (i = 0; i < num_planes; i++) {
3200 unsigned int hsub = i ? fb->format->hsub : 1;
3201 unsigned int vsub = i ? fb->format->vsub : 1;
3202 unsigned int cpp = fb->format->cpp[i];
3203 unsigned int tile_width, tile_height;
3204 unsigned int width, height;
3205 unsigned int pitch_tiles;
3206 unsigned int x, y;
3207 u32 offset;
3208
3209 intel_tile_dims(fb, i, &tile_width, &tile_height);
3210
3211 x = src_x / hsub;
3212 y = src_y / vsub;
3213 width = src_w / hsub;
3214 height = src_h / vsub;
3215
3216 /*
3217 * First pixel of the src viewport from the
3218 * start of the normal gtt mapping.
3219 */
3220 x += intel_fb->normal[i].x;
3221 y += intel_fb->normal[i].y;
3222
3223 offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3224 fb, i, fb->pitches[i],
3225 DRM_MODE_ROTATE_0, tile_size);
3226 offset /= tile_size;
3227
e57291c2 3228 drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
54d4d719
VS
3229 info->plane[i].offset = offset;
3230 info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3231 tile_width * cpp);
3232 info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3233 info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3234
3235 if (drm_rotation_90_or_270(rotation)) {
3236 struct drm_rect r;
3237
3238 /* rotate the x/y offsets to match the GTT view */
fc1a0fb5 3239 drm_rect_init(&r, x, y, width, height);
54d4d719
VS
3240 drm_rect_rotate(&r,
3241 info->plane[i].width * tile_width,
3242 info->plane[i].height * tile_height,
3243 DRM_MODE_ROTATE_270);
3244 x = r.x1;
3245 y = r.y1;
3246
3247 pitch_tiles = info->plane[i].height;
3248 plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3249
3250 /* rotate the tile dimensions to match the GTT view */
3251 swap(tile_width, tile_height);
3252 } else {
3253 pitch_tiles = info->plane[i].width;
3254 plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3255 }
3256
3257 /*
3258 * We only keep the x/y offsets, so push all of the
3259 * gtt offset into the x/y offsets.
3260 */
3261 intel_adjust_tile_offset(&x, &y,
3262 tile_width, tile_height,
3263 tile_size, pitch_tiles,
3264 gtt_offset * tile_size, 0);
3265
3266 gtt_offset += info->plane[i].width * info->plane[i].height;
3267
3268 plane_state->color_plane[i].offset = 0;
3269 plane_state->color_plane[i].x = x;
3270 plane_state->color_plane[i].y = y;
3271 }
3272}
3273
3274static int
3275intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3276{
3277 const struct intel_framebuffer *fb =
7b3cb17a
ML
3278 to_intel_framebuffer(plane_state->hw.fb);
3279 unsigned int rotation = plane_state->hw.rotation;
54d4d719
VS
3280 int i, num_planes;
3281
3282 if (!fb)
3283 return 0;
3284
3285 num_planes = fb->base.format->num_planes;
3286
3287 if (intel_plane_needs_remap(plane_state)) {
3288 intel_plane_remap_gtt(plane_state);
3289
3290 /*
3291 * Sometimes even remapping can't overcome
3292 * the stride limitations :( Can happen with
3293 * big plane sizes and suitably misaligned
3294 * offsets.
3295 */
3296 return intel_plane_check_stride(plane_state);
3297 }
3298
3299 intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3300
3301 for (i = 0; i < num_planes; i++) {
3302 plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3303 plane_state->color_plane[i].offset = 0;
3304
3305 if (drm_rotation_90_or_270(rotation)) {
3306 plane_state->color_plane[i].x = fb->rotated[i].x;
3307 plane_state->color_plane[i].y = fb->rotated[i].y;
3308 } else {
3309 plane_state->color_plane[i].x = fb->normal[i].x;
3310 plane_state->color_plane[i].y = fb->normal[i].y;
3311 }
3312 }
3313
3314 /* Rotate src coordinates to match rotated GTT view */
3315 if (drm_rotation_90_or_270(rotation))
f90a85e7 3316 drm_rect_rotate(&plane_state->uapi.src,
54d4d719
VS
3317 fb->base.width << 16, fb->base.height << 16,
3318 DRM_MODE_ROTATE_270);
3319
3320 return intel_plane_check_stride(plane_state);
3321}
3322
b35d63fa 3323static int i9xx_format_to_fourcc(int format)
46f297fb
JB
3324{
3325 switch (format) {
3326 case DISPPLANE_8BPP:
3327 return DRM_FORMAT_C8;
73263cb6
VS
3328 case DISPPLANE_BGRA555:
3329 return DRM_FORMAT_ARGB1555;
46f297fb
JB
3330 case DISPPLANE_BGRX555:
3331 return DRM_FORMAT_XRGB1555;
3332 case DISPPLANE_BGRX565:
3333 return DRM_FORMAT_RGB565;
3334 default:
3335 case DISPPLANE_BGRX888:
3336 return DRM_FORMAT_XRGB8888;
3337 case DISPPLANE_RGBX888:
3338 return DRM_FORMAT_XBGR8888;
73263cb6
VS
3339 case DISPPLANE_BGRA888:
3340 return DRM_FORMAT_ARGB8888;
3341 case DISPPLANE_RGBA888:
3342 return DRM_FORMAT_ABGR8888;
46f297fb
JB
3343 case DISPPLANE_BGRX101010:
3344 return DRM_FORMAT_XRGB2101010;
3345 case DISPPLANE_RGBX101010:
3346 return DRM_FORMAT_XBGR2101010;
73263cb6
VS
3347 case DISPPLANE_BGRA101010:
3348 return DRM_FORMAT_ARGB2101010;
3349 case DISPPLANE_RGBA101010:
3350 return DRM_FORMAT_ABGR2101010;
03b0ce95
VS
3351 case DISPPLANE_RGBX161616:
3352 return DRM_FORMAT_XBGR16161616F;
46f297fb
JB
3353 }
3354}
3355
ddf34319 3356int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
bc8d7dff
DL
3357{
3358 switch (format) {
3359 case PLANE_CTL_FORMAT_RGB_565:
3360 return DRM_FORMAT_RGB565;
f34a291c
MK
3361 case PLANE_CTL_FORMAT_NV12:
3362 return DRM_FORMAT_NV12;
da904174
SL
3363 case PLANE_CTL_FORMAT_XYUV:
3364 return DRM_FORMAT_XYUV8888;
df7d4156
JPH
3365 case PLANE_CTL_FORMAT_P010:
3366 return DRM_FORMAT_P010;
3367 case PLANE_CTL_FORMAT_P012:
3368 return DRM_FORMAT_P012;
3369 case PLANE_CTL_FORMAT_P016:
3370 return DRM_FORMAT_P016;
296e9b19
SS
3371 case PLANE_CTL_FORMAT_Y210:
3372 return DRM_FORMAT_Y210;
3373 case PLANE_CTL_FORMAT_Y212:
3374 return DRM_FORMAT_Y212;
3375 case PLANE_CTL_FORMAT_Y216:
3376 return DRM_FORMAT_Y216;
3377 case PLANE_CTL_FORMAT_Y410:
ff01e697 3378 return DRM_FORMAT_XVYU2101010;
296e9b19 3379 case PLANE_CTL_FORMAT_Y412:
ff01e697 3380 return DRM_FORMAT_XVYU12_16161616;
296e9b19 3381 case PLANE_CTL_FORMAT_Y416:
ff01e697 3382 return DRM_FORMAT_XVYU16161616;
bc8d7dff
DL
3383 default:
3384 case PLANE_CTL_FORMAT_XRGB_8888:
3385 if (rgb_order) {
3386 if (alpha)
3387 return DRM_FORMAT_ABGR8888;
3388 else
3389 return DRM_FORMAT_XBGR8888;
3390 } else {
3391 if (alpha)
3392 return DRM_FORMAT_ARGB8888;
3393 else
3394 return DRM_FORMAT_XRGB8888;
3395 }
3396 case PLANE_CTL_FORMAT_XRGB_2101010:
f9c43a31
VS
3397 if (rgb_order) {
3398 if (alpha)
3399 return DRM_FORMAT_ABGR2101010;
3400 else
3401 return DRM_FORMAT_XBGR2101010;
3402 } else {
3403 if (alpha)
3404 return DRM_FORMAT_ARGB2101010;
3405 else
3406 return DRM_FORMAT_XRGB2101010;
3407 }
a94bed60
KS
3408 case PLANE_CTL_FORMAT_XRGB_16161616F:
3409 if (rgb_order) {
3410 if (alpha)
3411 return DRM_FORMAT_ABGR16161616F;
3412 else
3413 return DRM_FORMAT_XBGR16161616F;
3414 } else {
3415 if (alpha)
3416 return DRM_FORMAT_ARGB16161616F;
3417 else
3418 return DRM_FORMAT_XRGB16161616F;
3419 }
bc8d7dff
DL
3420 }
3421}
3422
9c4ce97d
CW
3423static struct i915_vma *
3424initial_plane_vma(struct drm_i915_private *i915,
3425 struct intel_initial_plane_config *plane_config)
3426{
3427 struct drm_i915_gem_object *obj;
3428 struct i915_vma *vma;
3429 u32 base, size;
3430
3431 if (plane_config->size == 0)
3432 return NULL;
3433
3434 base = round_down(plane_config->base,
3435 I915_GTT_MIN_ALIGNMENT);
3436 size = round_up(plane_config->base + plane_config->size,
3437 I915_GTT_MIN_ALIGNMENT);
3438 size -= base;
3439
3440 /*
3441 * If the FB is too big, just don't use it since fbdev is not very
3442 * important and we should probably use that space with FBC or other
3443 * features.
3444 */
3445 if (size * 2 > i915->stolen_usable_size)
3446 return NULL;
3447
3448 obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3449 if (IS_ERR(obj))
3450 return NULL;
3451
2c1e63ba
VS
3452 /*
3453 * Mark it WT ahead of time to avoid changing the
3454 * cache_level during fbdev initialization. The
3455 * unbind there would get stuck waiting for rcu.
3456 */
3457 i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
3458 I915_CACHE_WT : I915_CACHE_NONE);
3459
9c4ce97d
CW
3460 switch (plane_config->tiling) {
3461 case I915_TILING_NONE:
3462 break;
3463 case I915_TILING_X:
3464 case I915_TILING_Y:
3465 obj->tiling_and_stride =
3466 plane_config->fb->base.pitches[0] |
3467 plane_config->tiling;
3468 break;
3469 default:
3470 MISSING_CASE(plane_config->tiling);
3471 goto err_obj;
3472 }
3473
3474 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3475 if (IS_ERR(vma))
3476 goto err_obj;
3477
47b08693 3478 if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
9c4ce97d
CW
3479 goto err_obj;
3480
3481 if (i915_gem_object_is_tiled(obj) &&
3482 !i915_vma_is_map_and_fenceable(vma))
3483 goto err_obj;
3484
3485 return vma;
3486
3487err_obj:
3488 i915_gem_object_put(obj);
3489 return NULL;
3490}
3491
5724dbd1 3492static bool
f6936e29
DV
3493intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3494 struct intel_initial_plane_config *plane_config)
46f297fb
JB
3495{
3496 struct drm_device *dev = crtc->base.dev;
3badb49f 3497 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb 3498 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 3499 struct drm_framebuffer *fb = &plane_config->fb->base;
9c4ce97d 3500 struct i915_vma *vma;
3badb49f 3501
914a4fd8
ID
3502 switch (fb->modifier) {
3503 case DRM_FORMAT_MOD_LINEAR:
3504 case I915_FORMAT_MOD_X_TILED:
3505 case I915_FORMAT_MOD_Y_TILED:
3506 break;
3507 default:
cd49f818
WK
3508 drm_dbg(&dev_priv->drm,
3509 "Unsupported modifier for initial FB: 0x%llx\n",
3510 fb->modifier);
914a4fd8
ID
3511 return false;
3512 }
3513
9c4ce97d
CW
3514 vma = initial_plane_vma(dev_priv, plane_config);
3515 if (!vma)
484b41dd 3516 return false;
46f297fb 3517
438b74a5 3518 mode_cmd.pixel_format = fb->format->format;
6bf129df
DL
3519 mode_cmd.width = fb->width;
3520 mode_cmd.height = fb->height;
3521 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 3522 mode_cmd.modifier[0] = fb->modifier;
18c5247e 3523 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 3524
9c4ce97d
CW
3525 if (intel_framebuffer_init(to_intel_framebuffer(fb),
3526 vma->obj, &mode_cmd)) {
cd49f818 3527 drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
9c4ce97d 3528 goto err_vma;
46f297fb 3529 }
12c83d99 3530
9c4ce97d
CW
3531 plane_config->vma = vma;
3532 return true;
484b41dd 3533
9c4ce97d
CW
3534err_vma:
3535 i915_vma_put(vma);
3536 return false;
484b41dd
JB
3537}
3538
e9728bd8
VS
3539static void
3540intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3541 struct intel_plane_state *plane_state,
3542 bool visible)
3543{
f90a85e7 3544 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
e9728bd8 3545
f90a85e7 3546 plane_state->uapi.visible = visible;
e9728bd8 3547
62358aa4 3548 if (visible)
2225f3c6 3549 crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
62358aa4 3550 else
2225f3c6 3551 crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
e9728bd8
VS
3552}
3553
62358aa4
VS
3554static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3555{
2225f3c6 3556 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
62358aa4
VS
3557 struct drm_plane *plane;
3558
3559 /*
3560 * Active_planes aliases if multiple "primary" or cursor planes
3561 * have been used on the same (or wrong) pipe. plane_mask uses
3562 * unique ids, hence we can use that to reconstruct active_planes.
3563 */
3564 crtc_state->active_planes = 0;
3565
3566 drm_for_each_plane_mask(plane, &dev_priv->drm,
2225f3c6 3567 crtc_state->uapi.plane_mask)
62358aa4
VS
3568 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3569}
3570
b1e01595
VS
3571static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3572 struct intel_plane *plane)
3573{
23526249 3574 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b1e01595
VS
3575 struct intel_crtc_state *crtc_state =
3576 to_intel_crtc_state(crtc->base.state);
3577 struct intel_plane_state *plane_state =
3578 to_intel_plane_state(plane->base.state);
3579
cd49f818
WK
3580 drm_dbg_kms(&dev_priv->drm,
3581 "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3582 plane->base.base.id, plane->base.name,
3583 crtc->base.base.id, crtc->base.name);
7a4a2a46 3584
b1e01595 3585 intel_set_plane_visible(crtc_state, plane_state, false);
62358aa4 3586 fixup_active_planes(crtc_state);
c457d9cf 3587 crtc_state->data_rate[plane->id] = 0;
bb6ae9e6 3588 crtc_state->min_cdclk[plane->id] = 0;
b1e01595
VS
3589
3590 if (plane->id == PLANE_PRIMARY)
23526249
VS
3591 hsw_disable_ips(crtc_state);
3592
3593 /*
3594 * Vblank time updates from the shadow to live plane control register
3595 * are blocked if the memory self-refresh mode is active at that
3596 * moment. So to make sure the plane gets truly disabled, disable
3597 * first the self-refresh mode. The self-refresh enable bit in turn
3598 * will be checked/applied by the HW only at the next frame start
3599 * event which is after the vblank start event, so we need to have a
3600 * wait-for-vblank between disabling the plane and the pipe.
3601 */
3602 if (HAS_GMCH(dev_priv) &&
3603 intel_set_memory_cxsr(dev_priv, false))
3604 intel_wait_for_vblank(dev_priv, crtc->pipe);
3605
3606 /*
3607 * Gen2 reports pipe underruns whenever all planes are disabled.
3608 * So disable underrun reporting before all the planes get disabled.
3609 */
3610 if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3611 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
b1e01595 3612
c48b86f9 3613 intel_disable_plane(plane, crtc_state);
b1e01595
VS
3614}
3615
8e7cb179
CW
3616static struct intel_frontbuffer *
3617to_intel_frontbuffer(struct drm_framebuffer *fb)
3618{
3619 return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3620}
3621
5724dbd1 3622static void
f6936e29
DV
3623intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3624 struct intel_initial_plane_config *plane_config)
484b41dd
JB
3625{
3626 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3627 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 3628 struct drm_crtc *c;
88595ac9 3629 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 3630 struct drm_plane_state *plane_state = primary->state;
200757f5 3631 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
3632 struct intel_plane_state *intel_state =
3633 to_intel_plane_state(plane_state);
0385ecea
MN
3634 struct intel_crtc_state *crtc_state =
3635 to_intel_crtc_state(intel_crtc->base.state);
88595ac9 3636 struct drm_framebuffer *fb;
9c4ce97d 3637 struct i915_vma *vma;
484b41dd 3638
2d14030b 3639 if (!plane_config->fb)
484b41dd
JB
3640 return;
3641
f6936e29 3642 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9 3643 fb = &plane_config->fb->base;
9c4ce97d 3644 vma = plane_config->vma;
88595ac9 3645 goto valid_fb;
f55548b5 3646 }
484b41dd 3647
484b41dd
JB
3648 /*
3649 * Failed to alloc the obj, check to see if we should share
3650 * an fb with another CRTC instead
3651 */
70e1e0ec 3652 for_each_crtc(dev, c) {
be1e3415 3653 struct intel_plane_state *state;
484b41dd
JB
3654
3655 if (c == &intel_crtc->base)
3656 continue;
3657
0385ecea 3658 if (!to_intel_crtc_state(c->state)->uapi.active)
2ff8fde1
MR
3659 continue;
3660
be1e3415
CW
3661 state = to_intel_plane_state(c->primary->state);
3662 if (!state->vma)
484b41dd
JB
3663 continue;
3664
be1e3415 3665 if (intel_plane_ggtt_offset(state) == plane_config->base) {
7b3cb17a 3666 fb = state->hw.fb;
9c4ce97d 3667 vma = state->vma;
88595ac9 3668 goto valid_fb;
484b41dd
JB
3669 }
3670 }
88595ac9 3671
200757f5
MR
3672 /*
3673 * We've failed to reconstruct the BIOS FB. Current display state
3674 * indicates that the primary plane is visible, but has a NULL FB,
3675 * which will lead to problems later if we don't fix it up. The
3676 * simplest solution is to just disable the primary plane now and
3677 * pretend the BIOS never had it enabled.
3678 */
b1e01595 3679 intel_plane_disable_noatomic(intel_crtc, intel_plane);
0385ecea
MN
3680 if (crtc_state->bigjoiner) {
3681 struct intel_crtc *slave =
3682 crtc_state->bigjoiner_linked_crtc;
3683 intel_plane_disable_noatomic(slave, to_intel_plane(slave->base.primary));
3684 }
200757f5 3685
88595ac9
DV
3686 return;
3687
3688valid_fb:
7b3cb17a 3689 intel_state->hw.rotation = plane_config->rotation;
f5929c53 3690 intel_fill_fb_ggtt_view(&intel_state->view, fb,
7b3cb17a 3691 intel_state->hw.rotation);
df79cf44 3692 intel_state->color_plane[0].stride =
7b3cb17a 3693 intel_fb_pitch(fb, 0, intel_state->hw.rotation);
df79cf44 3694
9c4ce97d
CW
3695 __i915_vma_pin(vma);
3696 intel_state->vma = i915_vma_get(vma);
3697 if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3698 if (vma->fence)
3699 intel_state->flags |= PLANE_HAS_FENCE;
07bcd99b 3700
f44e2659
VS
3701 plane_state->src_x = 0;
3702 plane_state->src_y = 0;
be5651f2
ML
3703 plane_state->src_w = fb->width << 16;
3704 plane_state->src_h = fb->height << 16;
3705
f44e2659
VS
3706 plane_state->crtc_x = 0;
3707 plane_state->crtc_y = 0;
be5651f2
ML
3708 plane_state->crtc_w = fb->width;
3709 plane_state->crtc_h = fb->height;
3710
f90a85e7
ML
3711 intel_state->uapi.src = drm_plane_state_src(plane_state);
3712 intel_state->uapi.dst = drm_plane_state_dest(plane_state);
0a8d8a86 3713
8e7cb179 3714 if (plane_config->tiling)
88595ac9
DV
3715 dev_priv->preserve_bios_swizzle = true;
3716
cd30fbca 3717 plane_state->fb = fb;
1586f620
CW
3718 drm_framebuffer_get(fb);
3719
cd30fbca 3720 plane_state->crtc = &intel_crtc->base;
8246d9c7
VS
3721 intel_plane_copy_uapi_to_hw_state(intel_state, intel_state,
3722 intel_crtc);
e9728bd8 3723
9c4ce97d
CW
3724 intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3725
faf5bf0a 3726 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
8e7cb179 3727 &to_intel_frontbuffer(fb)->bits);
46f297fb
JB
3728}
3729
e91c8a29 3730
2dfbf9d2
DP
3731static bool
3732skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3733 int main_x, int main_y, u32 main_offset,
3734 int ccs_plane)
2e2adb05 3735{
7b3cb17a 3736 const struct drm_framebuffer *fb = plane_state->hw.fb;
e7af9094
ID
3737 int aux_x = plane_state->color_plane[ccs_plane].x;
3738 int aux_y = plane_state->color_plane[ccs_plane].y;
3739 u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3740 u32 alignment = intel_surf_alignment(fb, ccs_plane);
b3e57bcc
DP
3741 int hsub;
3742 int vsub;
2e2adb05 3743
b3e57bcc 3744 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2e2adb05
VS
3745 while (aux_offset >= main_offset && aux_y <= main_y) {
3746 int x, y;
3747
3748 if (aux_x == main_x && aux_y == main_y)
3749 break;
3750
3751 if (aux_offset == 0)
3752 break;
3753
3754 x = aux_x / hsub;
3755 y = aux_y / vsub;
e7af9094
ID
3756 aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3757 plane_state,
3758 ccs_plane,
3759 aux_offset,
3760 aux_offset -
3761 alignment);
2e2adb05
VS
3762 aux_x = x * hsub + aux_x % hsub;
3763 aux_y = y * vsub + aux_y % vsub;
3764 }
3765
3766 if (aux_x != main_x || aux_y != main_y)
3767 return false;
3768
e7af9094
ID
3769 plane_state->color_plane[ccs_plane].offset = aux_offset;
3770 plane_state->color_plane[ccs_plane].x = aux_x;
3771 plane_state->color_plane[ccs_plane].y = aux_y;
2e2adb05
VS
3772
3773 return true;
3774}
3775
5331889b
VS
3776unsigned int
3777intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
3778{
3779 int x = 0, y = 0;
3780
3781 intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3782 plane_state->color_plane[0].offset, 0);
3783
3784 return y;
3785}
3786
230edf78
VS
3787static int intel_plane_min_width(struct intel_plane *plane,
3788 const struct drm_framebuffer *fb,
3789 int color_plane,
3790 unsigned int rotation)
3791{
3792 if (plane->min_width)
3793 return plane->min_width(fb, color_plane, rotation);
3794 else
3795 return 1;
3796}
3797
3798static int intel_plane_max_width(struct intel_plane *plane,
3799 const struct drm_framebuffer *fb,
3800 int color_plane,
3801 unsigned int rotation)
3802{
3803 if (plane->max_width)
3804 return plane->max_width(fb, color_plane, rotation);
3805 else
3806 return INT_MAX;
3807}
3808
3809static int intel_plane_max_height(struct intel_plane *plane,
3810 const struct drm_framebuffer *fb,
3811 int color_plane,
3812 unsigned int rotation)
3813{
3814 if (plane->max_height)
3815 return plane->max_height(fb, color_plane, rotation);
3816 else
3817 return INT_MAX;
3818}
3819
73266595 3820static int skl_check_main_surface(struct intel_plane_state *plane_state)
b63a16f6 3821{
230edf78
VS
3822 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3823 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a
ML
3824 const struct drm_framebuffer *fb = plane_state->hw.fb;
3825 unsigned int rotation = plane_state->hw.rotation;
f90a85e7
ML
3826 int x = plane_state->uapi.src.x1 >> 16;
3827 int y = plane_state->uapi.src.y1 >> 16;
3828 int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3829 int h = drm_rect_height(&plane_state->uapi.src) >> 16;
230edf78
VS
3830 int min_width = intel_plane_min_width(plane, fb, 0, rotation);
3831 int max_width = intel_plane_max_width(plane, fb, 0, rotation);
3832 int max_height = intel_plane_max_height(plane, fb, 0, rotation);
e7af9094
ID
3833 int aux_plane = intel_main_to_aux_plane(fb, 0);
3834 u32 aux_offset = plane_state->color_plane[aux_plane].offset;
230edf78 3835 u32 alignment, offset;
e91c8a29 3836
d24f1341 3837 if (w > max_width || w < min_width || h > max_height) {
cd49f818 3838 drm_dbg_kms(&dev_priv->drm,
d24f1341
MA
3839 "requested Y/RGB source size %dx%d outside limits (min: %dx1 max: %dx%d)\n",
3840 w, h, min_width, max_width, max_height);
b63a16f6
VS
3841 return -EINVAL;
3842 }
3843
3844 intel_add_fb_offsets(&x, &y, plane_state, 0);
6d19a44c 3845 offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
d88c4afd 3846 alignment = intel_surf_alignment(fb, 0);
e57291c2 3847 if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
7361bdb2 3848 return -EINVAL;
b63a16f6 3849
8d970654
VS
3850 /*
3851 * AUX surface offset is specified as the distance from the
3852 * main surface offset, and it must be non-negative. Make
3853 * sure that is what we will get.
3854 */
a007138e 3855 if (aux_plane && offset > aux_offset)
6d19a44c
VS
3856 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3857 offset, aux_offset & ~(alignment - 1));
8d970654 3858
b63a16f6
VS
3859 /*
3860 * When using an X-tiled surface, the plane blows up
3861 * if the x offset + width exceed the stride.
3862 *
3863 * TODO: linear and Y-tiled seem fine, Yf untested,
3864 */
bae781b2 3865 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
353c8598 3866 int cpp = fb->format->cpp[0];
b63a16f6 3867
df79cf44 3868 while ((x + w) * cpp > plane_state->color_plane[0].stride) {
b63a16f6 3869 if (offset == 0) {
cd49f818
WK
3870 drm_dbg_kms(&dev_priv->drm,
3871 "Unable to find suitable display surface offset due to X-tiling\n");
b63a16f6
VS
3872 return -EINVAL;
3873 }
3874
6d19a44c
VS
3875 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3876 offset, offset - alignment);
b63a16f6
VS
3877 }
3878 }
3879
2e2adb05
VS
3880 /*
3881 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3882 * they match with the main surface x/y offsets.
3883 */
63eaf9ac 3884 if (is_ccs_modifier(fb->modifier)) {
2dfbf9d2
DP
3885 while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3886 offset, aux_plane)) {
2e2adb05
VS
3887 if (offset == 0)
3888 break;
3889
6d19a44c
VS
3890 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3891 offset, offset - alignment);
2e2adb05
VS
3892 }
3893
e7af9094
ID
3894 if (x != plane_state->color_plane[aux_plane].x ||
3895 y != plane_state->color_plane[aux_plane].y) {
cd49f818
WK
3896 drm_dbg_kms(&dev_priv->drm,
3897 "Unable to find suitable display surface offset due to CCS\n");
2e2adb05
VS
3898 return -EINVAL;
3899 }
3900 }
3901
c11ada07
VS
3902 plane_state->color_plane[0].offset = offset;
3903 plane_state->color_plane[0].x = x;
3904 plane_state->color_plane[0].y = y;
b63a16f6 3905
54d4d719
VS
3906 /*
3907 * Put the final coordinates back so that the src
3908 * coordinate checks will see the right values.
3909 */
f90a85e7 3910 drm_rect_translate_to(&plane_state->uapi.src,
dcdef1ab 3911 x << 16, y << 16);
54d4d719 3912
b63a16f6
VS
3913 return 0;
3914}
3915
8d970654
VS
3916static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3917{
230edf78
VS
3918 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3919 struct drm_i915_private *i915 = to_i915(plane->base.dev);
7b3cb17a
ML
3920 const struct drm_framebuffer *fb = plane_state->hw.fb;
3921 unsigned int rotation = plane_state->hw.rotation;
2dfbf9d2 3922 int uv_plane = 1;
230edf78
VS
3923 int max_width = intel_plane_max_width(plane, fb, uv_plane, rotation);
3924 int max_height = intel_plane_max_height(plane, fb, uv_plane, rotation);
f90a85e7
ML
3925 int x = plane_state->uapi.src.x1 >> 17;
3926 int y = plane_state->uapi.src.y1 >> 17;
3927 int w = drm_rect_width(&plane_state->uapi.src) >> 17;
3928 int h = drm_rect_height(&plane_state->uapi.src) >> 17;
8d970654
VS
3929 u32 offset;
3930
8d970654
VS
3931 /* FIXME not quite sure how/if these apply to the chroma plane */
3932 if (w > max_width || h > max_height) {
cd49f818
WK
3933 drm_dbg_kms(&i915->drm,
3934 "CbCr source size %dx%d too big (limit %dx%d)\n",
3935 w, h, max_width, max_height);
8d970654
VS
3936 return -EINVAL;
3937 }
3938
230edf78
VS
3939 intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
3940 offset = intel_plane_compute_aligned_offset(&x, &y,
3941 plane_state, uv_plane);
3942
2dfbf9d2
DP
3943 if (is_ccs_modifier(fb->modifier)) {
3944 int ccs_plane = main_to_ccs_plane(fb, uv_plane);
63b9d9aa
VS
3945 u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3946 u32 alignment = intel_surf_alignment(fb, uv_plane);
2dfbf9d2
DP
3947
3948 if (offset > aux_offset)
3949 offset = intel_plane_adjust_aligned_offset(&x, &y,
3950 plane_state,
3951 uv_plane,
3952 offset,
3953 aux_offset & ~(alignment - 1));
3954
3955 while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3956 offset, ccs_plane)) {
3957 if (offset == 0)
3958 break;
3959
3960 offset = intel_plane_adjust_aligned_offset(&x, &y,
3961 plane_state,
3962 uv_plane,
3963 offset, offset - alignment);
3964 }
3965
3966 if (x != plane_state->color_plane[ccs_plane].x ||
3967 y != plane_state->color_plane[ccs_plane].y) {
cd49f818
WK
3968 drm_dbg_kms(&i915->drm,
3969 "Unable to find suitable display surface offset due to CCS\n");
2dfbf9d2
DP
3970 return -EINVAL;
3971 }
3972 }
3973
3974 plane_state->color_plane[uv_plane].offset = offset;
3975 plane_state->color_plane[uv_plane].x = x;
3976 plane_state->color_plane[uv_plane].y = y;
8d970654
VS
3977
3978 return 0;
3979}
3980
2e2adb05
VS
3981static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3982{
7b3cb17a 3983 const struct drm_framebuffer *fb = plane_state->hw.fb;
f90a85e7
ML
3984 int src_x = plane_state->uapi.src.x1 >> 16;
3985 int src_y = plane_state->uapi.src.y1 >> 16;
2e2adb05 3986 u32 offset;
2dfbf9d2 3987 int ccs_plane;
2e2adb05 3988
2dfbf9d2
DP
3989 for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
3990 int main_hsub, main_vsub;
3991 int hsub, vsub;
3992 int x, y;
2e2adb05 3993
2dfbf9d2
DP
3994 if (!is_ccs_plane(fb, ccs_plane))
3995 continue;
3996
3997 intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
3998 ccs_to_main_plane(fb, ccs_plane));
3999 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
4000
4001 hsub *= main_hsub;
4002 vsub *= main_vsub;
4003 x = src_x / hsub;
4004 y = src_y / vsub;
4005
4006 intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
4007
4008 offset = intel_plane_compute_aligned_offset(&x, &y,
4009 plane_state,
4010 ccs_plane);
4011
4012 plane_state->color_plane[ccs_plane].offset = offset;
4013 plane_state->color_plane[ccs_plane].x = (x * hsub +
4014 src_x % hsub) /
4015 main_hsub;
4016 plane_state->color_plane[ccs_plane].y = (y * vsub +
4017 src_y % vsub) /
4018 main_vsub;
4019 }
2e2adb05
VS
4020
4021 return 0;
4022}
4023
73266595 4024int skl_check_plane_surface(struct intel_plane_state *plane_state)
b63a16f6 4025{
7b3cb17a 4026 const struct drm_framebuffer *fb = plane_state->hw.fb;
79148ce4 4027 int ret, i;
b63a16f6 4028
54d4d719 4029 ret = intel_plane_compute_gtt(plane_state);
fc3fed5d
VS
4030 if (ret)
4031 return ret;
4032
f90a85e7 4033 if (!plane_state->uapi.visible)
a5e4c7d0
VS
4034 return 0;
4035
8d970654 4036 /*
2dfbf9d2
DP
4037 * Handle the AUX surface first since the main surface setup depends on
4038 * it.
8d970654 4039 */
2dfbf9d2 4040 if (is_ccs_modifier(fb->modifier)) {
2dfbf9d2
DP
4041 ret = skl_check_ccs_aux_surface(plane_state);
4042 if (ret)
4043 return ret;
4044 }
4045
4941f35b
ID
4046 if (intel_format_info_is_yuv_semiplanar(fb->format,
4047 fb->modifier)) {
8d970654
VS
4048 ret = skl_check_nv12_aux_surface(plane_state);
4049 if (ret)
4050 return ret;
2dfbf9d2
DP
4051 }
4052
79148ce4 4053 for (i = fb->format->num_planes; i < ARRAY_SIZE(plane_state->color_plane); i++) {
a007138e 4054 plane_state->color_plane[i].offset = 0;
79148ce4
VS
4055 plane_state->color_plane[i].x = 0;
4056 plane_state->color_plane[i].y = 0;
8d970654
VS
4057 }
4058
73266595 4059 ret = skl_check_main_surface(plane_state);
b63a16f6
VS
4060 if (ret)
4061 return ret;
4062
4063 return 0;
4064}
4065
bb6ae9e6
VS
4066static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4067 const struct intel_plane_state *plane_state,
4068 unsigned int *num, unsigned int *den)
4069{
7b3cb17a 4070 const struct drm_framebuffer *fb = plane_state->hw.fb;
bb6ae9e6
VS
4071 unsigned int cpp = fb->format->cpp[0];
4072
4073 /*
4074 * g4x bspec says 64bpp pixel rate can't exceed 80%
4075 * of cdclk when the sprite plane is enabled on the
4076 * same pipe. ilk/snb bspec says 64bpp pixel rate is
4077 * never allowed to exceed 80% of cdclk. Let's just go
4078 * with the ilk/snb limit always.
4079 */
4080 if (cpp == 8) {
4081 *num = 10;
4082 *den = 8;
4083 } else {
4084 *num = 1;
4085 *den = 1;
4086 }
4087}
4088
4089static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4090 const struct intel_plane_state *plane_state)
4091{
4092 unsigned int pixel_rate;
4093 unsigned int num, den;
4094
4095 /*
4096 * Note that crtc_state->pixel_rate accounts for both
4097 * horizontal and vertical panel fitter downscaling factors.
4098 * Pre-HSW bspec tells us to only consider the horizontal
4099 * downscaling factor here. We ignore that and just consider
4100 * both for simplicity.
4101 */
4102 pixel_rate = crtc_state->pixel_rate;
4103
4104 i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4105
4106 /* two pixels per clock with double wide pipe */
4107 if (crtc_state->double_wide)
4108 den *= 2;
4109
4110 return DIV_ROUND_UP(pixel_rate * num, den);
4111}
4112
ddd5713d
VS
4113unsigned int
4114i9xx_plane_max_stride(struct intel_plane *plane,
4115 u32 pixel_format, u64 modifier,
4116 unsigned int rotation)
4117{
4118 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4119
b2ae318a 4120 if (!HAS_GMCH(dev_priv)) {
ddd5713d
VS
4121 return 32*1024;
4122 } else if (INTEL_GEN(dev_priv) >= 4) {
4123 if (modifier == I915_FORMAT_MOD_X_TILED)
4124 return 16*1024;
4125 else
4126 return 32*1024;
4127 } else if (INTEL_GEN(dev_priv) >= 3) {
4128 if (modifier == I915_FORMAT_MOD_X_TILED)
4129 return 8*1024;
4130 else
4131 return 16*1024;
4132 } else {
4133 if (plane->i9xx_plane == PLANE_C)
4134 return 4*1024;
4135 else
4136 return 8*1024;
4137 }
4138}
4139
7eb31a0b
VS
4140static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4141{
2225f3c6 4142 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7eb31a0b
VS
4143 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4144 u32 dspcntr = 0;
4145
5f29ab23
VS
4146 if (crtc_state->gamma_enable)
4147 dspcntr |= DISPPLANE_GAMMA_ENABLE;
7eb31a0b 4148
8271b2ef 4149 if (crtc_state->csc_enable)
7eb31a0b
VS
4150 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4151
4152 if (INTEL_GEN(dev_priv) < 5)
4153 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4154
4155 return dspcntr;
4156}
4157
7145f60a
VS
4158static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4159 const struct intel_plane_state *plane_state)
81255565 4160{
7145f60a 4161 struct drm_i915_private *dev_priv =
f90a85e7 4162 to_i915(plane_state->uapi.plane->dev);
7b3cb17a
ML
4163 const struct drm_framebuffer *fb = plane_state->hw.fb;
4164 unsigned int rotation = plane_state->hw.rotation;
7145f60a 4165 u32 dspcntr;
c9ba6fad 4166
7eb31a0b 4167 dspcntr = DISPLAY_PLANE_ENABLE;
f45651ba 4168
cf819eff
LDM
4169 if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4170 IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
7145f60a 4171 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
f45651ba 4172
438b74a5 4173 switch (fb->format->format) {
57779d06 4174 case DRM_FORMAT_C8:
81255565
JB
4175 dspcntr |= DISPPLANE_8BPP;
4176 break;
57779d06 4177 case DRM_FORMAT_XRGB1555:
57779d06 4178 dspcntr |= DISPPLANE_BGRX555;
81255565 4179 break;
73263cb6
VS
4180 case DRM_FORMAT_ARGB1555:
4181 dspcntr |= DISPPLANE_BGRA555;
4182 break;
57779d06
VS
4183 case DRM_FORMAT_RGB565:
4184 dspcntr |= DISPPLANE_BGRX565;
4185 break;
4186 case DRM_FORMAT_XRGB8888:
57779d06
VS
4187 dspcntr |= DISPPLANE_BGRX888;
4188 break;
4189 case DRM_FORMAT_XBGR8888:
57779d06
VS
4190 dspcntr |= DISPPLANE_RGBX888;
4191 break;
73263cb6
VS
4192 case DRM_FORMAT_ARGB8888:
4193 dspcntr |= DISPPLANE_BGRA888;
4194 break;
4195 case DRM_FORMAT_ABGR8888:
4196 dspcntr |= DISPPLANE_RGBA888;
4197 break;
57779d06 4198 case DRM_FORMAT_XRGB2101010:
57779d06
VS
4199 dspcntr |= DISPPLANE_BGRX101010;
4200 break;
4201 case DRM_FORMAT_XBGR2101010:
57779d06 4202 dspcntr |= DISPPLANE_RGBX101010;
81255565 4203 break;
73263cb6
VS
4204 case DRM_FORMAT_ARGB2101010:
4205 dspcntr |= DISPPLANE_BGRA101010;
4206 break;
4207 case DRM_FORMAT_ABGR2101010:
4208 dspcntr |= DISPPLANE_RGBA101010;
4209 break;
03b0ce95
VS
4210 case DRM_FORMAT_XBGR16161616F:
4211 dspcntr |= DISPPLANE_RGBX161616;
4212 break;
81255565 4213 default:
7145f60a
VS
4214 MISSING_CASE(fb->format->format);
4215 return 0;
81255565 4216 }
57779d06 4217
72618ebf 4218 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 4219 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 4220 dspcntr |= DISPPLANE_TILED;
81255565 4221
c2c446ad 4222 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
4223 dspcntr |= DISPPLANE_ROTATE_180;
4224
c2c446ad 4225 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
4226 dspcntr |= DISPPLANE_MIRROR;
4227
7145f60a
VS
4228 return dspcntr;
4229}
de1aa629 4230
f9407ae1 4231int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
5b7fcc44
VS
4232{
4233 struct drm_i915_private *dev_priv =
f90a85e7 4234 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 4235 const struct drm_framebuffer *fb = plane_state->hw.fb;
03b0ce95 4236 int src_x, src_y, src_w;
5b7fcc44 4237 u32 offset;
fc3fed5d 4238 int ret;
81255565 4239
54d4d719 4240 ret = intel_plane_compute_gtt(plane_state);
fc3fed5d
VS
4241 if (ret)
4242 return ret;
4243
f90a85e7 4244 if (!plane_state->uapi.visible)
54d4d719
VS
4245 return 0;
4246
f90a85e7
ML
4247 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4248 src_x = plane_state->uapi.src.x1 >> 16;
4249 src_y = plane_state->uapi.src.y1 >> 16;
54d4d719 4250
03b0ce95
VS
4251 /* Undocumented hardware limit on i965/g4x/vlv/chv */
4252 if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4253 return -EINVAL;
4254
5b7fcc44 4255 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
e506a0c6 4256
5b7fcc44 4257 if (INTEL_GEN(dev_priv) >= 4)
6d19a44c
VS
4258 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4259 plane_state, 0);
5b7fcc44
VS
4260 else
4261 offset = 0;
4262
54d4d719
VS
4263 /*
4264 * Put the final coordinates back so that the src
4265 * coordinate checks will see the right values.
4266 */
f90a85e7 4267 drm_rect_translate_to(&plane_state->uapi.src,
dcdef1ab 4268 src_x << 16, src_y << 16);
54d4d719 4269
5b7fcc44
VS
4270 /* HSW/BDW do this automagically in hardware */
4271 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
7b3cb17a 4272 unsigned int rotation = plane_state->hw.rotation;
f90a85e7
ML
4273 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4274 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
5b7fcc44 4275
c2c446ad 4276 if (rotation & DRM_MODE_ROTATE_180) {
5b7fcc44
VS
4277 src_x += src_w - 1;
4278 src_y += src_h - 1;
c2c446ad 4279 } else if (rotation & DRM_MODE_REFLECT_X) {
5b7fcc44
VS
4280 src_x += src_w - 1;
4281 }
48404c1e
SJ
4282 }
4283
c11ada07
VS
4284 plane_state->color_plane[0].offset = offset;
4285 plane_state->color_plane[0].x = src_x;
4286 plane_state->color_plane[0].y = src_y;
5b7fcc44
VS
4287
4288 return 0;
4289}
4290
26443a4b
VS
4291static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4292{
4293 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4294 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4295
4296 if (IS_CHERRYVIEW(dev_priv))
4297 return i9xx_plane == PLANE_B;
4298 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4299 return false;
4300 else if (IS_GEN(dev_priv, 4))
4301 return i9xx_plane == PLANE_C;
4302 else
4303 return i9xx_plane == PLANE_B ||
4304 i9xx_plane == PLANE_C;
4305}
4306
4e0b83a5
VS
4307static int
4308i9xx_plane_check(struct intel_crtc_state *crtc_state,
4309 struct intel_plane_state *plane_state)
4310{
f90a85e7 4311 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4e0b83a5
VS
4312 int ret;
4313
25721f82
VS
4314 ret = chv_plane_check_rotation(plane_state);
4315 if (ret)
4316 return ret;
4317
9f05a7c0
ML
4318 ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
4319 DRM_PLANE_HELPER_NO_SCALING,
4320 DRM_PLANE_HELPER_NO_SCALING,
4321 i9xx_plane_has_windowing(plane));
4e0b83a5
VS
4322 if (ret)
4323 return ret;
4324
54d4d719
VS
4325 ret = i9xx_check_plane_surface(plane_state);
4326 if (ret)
4327 return ret;
4328
f90a85e7 4329 if (!plane_state->uapi.visible)
4e0b83a5
VS
4330 return 0;
4331
4332 ret = intel_plane_check_src_coordinates(plane_state);
4333 if (ret)
4334 return ret;
4335
4e0b83a5
VS
4336 plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4337
4338 return 0;
4339}
4340
ed15030d
VS
4341static void i9xx_update_plane(struct intel_plane *plane,
4342 const struct intel_crtc_state *crtc_state,
4343 const struct intel_plane_state *plane_state)
7145f60a 4344{
ed15030d 4345 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
ed15030d 4346 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
7145f60a 4347 u32 linear_offset;
c11ada07
VS
4348 int x = plane_state->color_plane[0].x;
4349 int y = plane_state->color_plane[0].y;
f90a85e7
ML
4350 int crtc_x = plane_state->uapi.dst.x1;
4351 int crtc_y = plane_state->uapi.dst.y1;
4352 int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4353 int crtc_h = drm_rect_height(&plane_state->uapi.dst);
7145f60a 4354 unsigned long irqflags;
e288881b 4355 u32 dspaddr_offset;
7eb31a0b
VS
4356 u32 dspcntr;
4357
4358 dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
7145f60a 4359
2949056c 4360 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 4361
5b7fcc44 4362 if (INTEL_GEN(dev_priv) >= 4)
c11ada07 4363 dspaddr_offset = plane_state->color_plane[0].offset;
5b7fcc44 4364 else
e288881b 4365 dspaddr_offset = linear_offset;
6687c906 4366
dd584fc0
VS
4367 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4368
dc008bf0
JN
4369 intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4370 plane_state->color_plane[0].stride);
83234d13 4371
78587de2 4372 if (INTEL_GEN(dev_priv) < 4) {
26443a4b
VS
4373 /*
4374 * PLANE_A doesn't actually have a full window
4375 * generator but let's assume we still need to
4376 * program whatever is there.
78587de2 4377 */
dc008bf0
JN
4378 intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4379 (crtc_y << 16) | crtc_x);
4380 intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4381 ((crtc_h - 1) << 16) | (crtc_w - 1));
ed15030d 4382 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
dc008bf0
JN
4383 intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4384 (crtc_y << 16) | crtc_x);
4385 intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4386 ((crtc_h - 1) << 16) | (crtc_w - 1));
4387 intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
78587de2
VS
4388 }
4389
3ba35e53 4390 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
dc008bf0
JN
4391 intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4392 (y << 16) | x);
3ba35e53 4393 } else if (INTEL_GEN(dev_priv) >= 4) {
dc008bf0
JN
4394 intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4395 linear_offset);
4396 intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4397 (y << 16) | x);
83234d13
VS
4398 }
4399
4400 /*
4401 * The control register self-arms if the plane was previously
4402 * disabled. Try to make the plane enable atomic by writing
4403 * the control register just before the surface register.
4404 */
dc008bf0 4405 intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
83234d13 4406 if (INTEL_GEN(dev_priv) >= 4)
dc008bf0
JN
4407 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4408 intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
83234d13 4409 else
dc008bf0
JN
4410 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4411 intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
dd584fc0
VS
4412
4413 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
4414}
4415
ed15030d 4416static void i9xx_disable_plane(struct intel_plane *plane,
0dd14be3 4417 const struct intel_crtc_state *crtc_state)
17638cd6 4418{
ed15030d
VS
4419 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4420 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
dd584fc0 4421 unsigned long irqflags;
7eb31a0b
VS
4422 u32 dspcntr;
4423
4424 /*
4425 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4426 * enable on ilk+ affect the pipe bottom color as
4427 * well, so we must configure them even if the plane
4428 * is disabled.
4429 *
4430 * On pre-g4x there is no way to gamma correct the
4431 * pipe bottom color but we'll keep on doing this
9d5441de 4432 * anyway so that the crtc state readout works correctly.
7eb31a0b
VS
4433 */
4434 dspcntr = i9xx_plane_ctl_crtc(crtc_state);
dd584fc0
VS
4435
4436 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 4437
dc008bf0 4438 intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
ed15030d 4439 if (INTEL_GEN(dev_priv) >= 4)
dc008bf0 4440 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
a8d201af 4441 else
dc008bf0 4442 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
dd584fc0
VS
4443
4444 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 4445}
c9ba6fad 4446
eade6c89
VS
4447static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4448 enum pipe *pipe)
51f5a096 4449{
ed15030d 4450 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
51f5a096 4451 enum intel_display_power_domain power_domain;
ed15030d 4452 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
0e6e0be4 4453 intel_wakeref_t wakeref;
51f5a096 4454 bool ret;
eade6c89 4455 u32 val;
51f5a096
VS
4456
4457 /*
4458 * Not 100% correct for planes that can move between pipes,
4459 * but that's only the case for gen2-4 which don't have any
4460 * display power wells.
4461 */
eade6c89 4462 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
0e6e0be4
CW
4463 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4464 if (!wakeref)
51f5a096
VS
4465 return false;
4466
dc008bf0 4467 val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
eade6c89
VS
4468
4469 ret = val & DISPLAY_PLANE_ENABLE;
4470
4471 if (INTEL_GEN(dev_priv) >= 5)
4472 *pipe = plane->pipe;
4473 else
4474 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4475 DISPPLANE_SEL_PIPE_SHIFT;
51f5a096 4476
0e6e0be4 4477 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
4478
4479 return ret;
4480}
4481
e435d6e5
ML
4482static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4483{
4484 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4485 struct drm_i915_private *dev_priv = to_i915(dev);
f986ef2e
VS
4486 unsigned long irqflags;
4487
4488 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4489
4490 intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4491 intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4492 intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5 4493
f986ef2e 4494 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
e435d6e5
ML
4495}
4496
a1b2278e
CK
4497/*
4498 * This function detaches (aka. unbinds) unused scalers in hardware
4499 */
15cbe5d0 4500static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
a1b2278e 4501{
2225f3c6 4502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
15cbe5d0
ML
4503 const struct intel_crtc_scaler_state *scaler_state =
4504 &crtc_state->scaler_state;
a1b2278e
CK
4505 int i;
4506
a1b2278e
CK
4507 /* loop through and disable scalers that aren't in use */
4508 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
4509 if (!scaler_state->scalers[i].in_use)
4510 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
4511 }
4512}
4513
b3cf5c06
VS
4514static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4515 int color_plane, unsigned int rotation)
4516{
4517 /*
4518 * The stride is either expressed as a multiple of 64 bytes chunks for
4519 * linear buffers or in number of tiles for tiled buffers.
4520 */
b3e57bcc 4521 if (is_surface_linear(fb, color_plane))
b3cf5c06
VS
4522 return 64;
4523 else if (drm_rotation_90_or_270(rotation))
4524 return intel_tile_height(fb, color_plane);
4525 else
4526 return intel_tile_width_bytes(fb, color_plane);
4527}
4528
df79cf44 4529u32 skl_plane_stride(const struct intel_plane_state *plane_state,
5d2a1950 4530 int color_plane)
d2196774 4531{
7b3cb17a
ML
4532 const struct drm_framebuffer *fb = plane_state->hw.fb;
4533 unsigned int rotation = plane_state->hw.rotation;
5d2a1950 4534 u32 stride = plane_state->color_plane[color_plane].stride;
1b500535 4535
5d2a1950 4536 if (color_plane >= fb->format->num_planes)
1b500535
VS
4537 return 0;
4538
b3cf5c06 4539 return stride / skl_plane_stride_mult(fb, color_plane, rotation);
d2196774
VS
4540}
4541
ba3f4d0a 4542static u32 skl_plane_ctl_format(u32 pixel_format)
70d21f0e 4543{
6156a456 4544 switch (pixel_format) {
d161cf7a 4545 case DRM_FORMAT_C8:
c34ce3d1 4546 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 4547 case DRM_FORMAT_RGB565:
c34ce3d1 4548 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 4549 case DRM_FORMAT_XBGR8888:
4036c78c 4550 case DRM_FORMAT_ABGR8888:
c34ce3d1 4551 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 4552 case DRM_FORMAT_XRGB8888:
6156a456 4553 case DRM_FORMAT_ARGB8888:
4036c78c 4554 return PLANE_CTL_FORMAT_XRGB_8888;
94e35ce2 4555 case DRM_FORMAT_XBGR2101010:
f9c43a31 4556 case DRM_FORMAT_ABGR2101010:
94e35ce2 4557 return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
70d21f0e 4558 case DRM_FORMAT_XRGB2101010:
f9c43a31 4559 case DRM_FORMAT_ARGB2101010:
c34ce3d1 4560 return PLANE_CTL_FORMAT_XRGB_2101010;
a94bed60
KS
4561 case DRM_FORMAT_XBGR16161616F:
4562 case DRM_FORMAT_ABGR16161616F:
4563 return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4564 case DRM_FORMAT_XRGB16161616F:
4565 case DRM_FORMAT_ARGB16161616F:
4566 return PLANE_CTL_FORMAT_XRGB_16161616F;
da904174
SL
4567 case DRM_FORMAT_XYUV8888:
4568 return PLANE_CTL_FORMAT_XYUV;
6156a456 4569 case DRM_FORMAT_YUYV:
c34ce3d1 4570 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 4571 case DRM_FORMAT_YVYU:
c34ce3d1 4572 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 4573 case DRM_FORMAT_UYVY:
c34ce3d1 4574 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 4575 case DRM_FORMAT_VYUY:
c34ce3d1 4576 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
77224cd5
CK
4577 case DRM_FORMAT_NV12:
4578 return PLANE_CTL_FORMAT_NV12;
df7d4156
JPH
4579 case DRM_FORMAT_P010:
4580 return PLANE_CTL_FORMAT_P010;
4581 case DRM_FORMAT_P012:
4582 return PLANE_CTL_FORMAT_P012;
4583 case DRM_FORMAT_P016:
4584 return PLANE_CTL_FORMAT_P016;
296e9b19
SS
4585 case DRM_FORMAT_Y210:
4586 return PLANE_CTL_FORMAT_Y210;
4587 case DRM_FORMAT_Y212:
4588 return PLANE_CTL_FORMAT_Y212;
4589 case DRM_FORMAT_Y216:
4590 return PLANE_CTL_FORMAT_Y216;
ff01e697 4591 case DRM_FORMAT_XVYU2101010:
296e9b19 4592 return PLANE_CTL_FORMAT_Y410;
ff01e697 4593 case DRM_FORMAT_XVYU12_16161616:
296e9b19 4594 return PLANE_CTL_FORMAT_Y412;
ff01e697 4595 case DRM_FORMAT_XVYU16161616:
296e9b19 4596 return PLANE_CTL_FORMAT_Y416;
70d21f0e 4597 default:
4249eeef 4598 MISSING_CASE(pixel_format);
70d21f0e 4599 }
8cfcba41 4600
c34ce3d1 4601 return 0;
6156a456 4602}
70d21f0e 4603
b2081525 4604static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4036c78c 4605{
7b3cb17a 4606 if (!plane_state->hw.fb->format->has_alpha)
b2081525
ML
4607 return PLANE_CTL_ALPHA_DISABLE;
4608
7b3cb17a 4609 switch (plane_state->hw.pixel_blend_mode) {
b2081525
ML
4610 case DRM_MODE_BLEND_PIXEL_NONE:
4611 return PLANE_CTL_ALPHA_DISABLE;
4612 case DRM_MODE_BLEND_PREMULTI:
4036c78c 4613 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
b2081525
ML
4614 case DRM_MODE_BLEND_COVERAGE:
4615 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4036c78c 4616 default:
7b3cb17a 4617 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4036c78c
JA
4618 return PLANE_CTL_ALPHA_DISABLE;
4619 }
4620}
4621
b2081525 4622static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4036c78c 4623{
7b3cb17a 4624 if (!plane_state->hw.fb->format->has_alpha)
b2081525
ML
4625 return PLANE_COLOR_ALPHA_DISABLE;
4626
7b3cb17a 4627 switch (plane_state->hw.pixel_blend_mode) {
b2081525
ML
4628 case DRM_MODE_BLEND_PIXEL_NONE:
4629 return PLANE_COLOR_ALPHA_DISABLE;
4630 case DRM_MODE_BLEND_PREMULTI:
4036c78c 4631 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
b2081525
ML
4632 case DRM_MODE_BLEND_COVERAGE:
4633 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4036c78c 4634 default:
7b3cb17a 4635 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4036c78c
JA
4636 return PLANE_COLOR_ALPHA_DISABLE;
4637 }
4638}
4639
ba3f4d0a 4640static u32 skl_plane_ctl_tiling(u64 fb_modifier)
6156a456 4641{
6156a456 4642 switch (fb_modifier) {
2f075565 4643 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 4644 break;
30af77c4 4645 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 4646 return PLANE_CTL_TILED_X;
b321803d 4647 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 4648 return PLANE_CTL_TILED_Y;
2e2adb05 4649 case I915_FORMAT_MOD_Y_TILED_CCS:
53867b46 4650 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
b3e57bcc
DP
4651 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4652 return PLANE_CTL_TILED_Y |
4653 PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4654 PLANE_CTL_CLEAR_COLOR_DISABLE;
2dfbf9d2
DP
4655 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4656 return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
b321803d 4657 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 4658 return PLANE_CTL_TILED_YF;
2e2adb05 4659 case I915_FORMAT_MOD_Yf_TILED_CCS:
53867b46 4660 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
70d21f0e 4661 default:
6156a456 4662 MISSING_CASE(fb_modifier);
70d21f0e 4663 }
8cfcba41 4664
c34ce3d1 4665 return 0;
6156a456 4666}
70d21f0e 4667
5f8e3f57 4668static u32 skl_plane_ctl_rotate(unsigned int rotate)
6156a456 4669{
5f8e3f57 4670 switch (rotate) {
c2c446ad 4671 case DRM_MODE_ROTATE_0:
6156a456 4672 break;
1e8df167 4673 /*
c2c446ad 4674 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
4675 * while i915 HW rotation is clockwise, thats why this swapping.
4676 */
c2c446ad 4677 case DRM_MODE_ROTATE_90:
1e8df167 4678 return PLANE_CTL_ROTATE_270;
c2c446ad 4679 case DRM_MODE_ROTATE_180:
c34ce3d1 4680 return PLANE_CTL_ROTATE_180;
c2c446ad 4681 case DRM_MODE_ROTATE_270:
1e8df167 4682 return PLANE_CTL_ROTATE_90;
6156a456 4683 default:
5f8e3f57
JL
4684 MISSING_CASE(rotate);
4685 }
4686
4687 return 0;
4688}
4689
4690static u32 cnl_plane_ctl_flip(unsigned int reflect)
4691{
4692 switch (reflect) {
4693 case 0:
4694 break;
4695 case DRM_MODE_REFLECT_X:
4696 return PLANE_CTL_FLIP_HORIZONTAL;
4697 case DRM_MODE_REFLECT_Y:
4698 default:
4699 MISSING_CASE(reflect);
6156a456
CK
4700 }
4701
c34ce3d1 4702 return 0;
6156a456
CK
4703}
4704
7eb31a0b
VS
4705u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4706{
2225f3c6 4707 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7eb31a0b
VS
4708 u32 plane_ctl = 0;
4709
c5e07e00
K
4710 if (crtc_state->uapi.async_flip)
4711 plane_ctl |= PLANE_CTL_ASYNC_FLIP;
4712
7eb31a0b
VS
4713 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4714 return plane_ctl;
4715
5f29ab23
VS
4716 if (crtc_state->gamma_enable)
4717 plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4718
8271b2ef
VS
4719 if (crtc_state->csc_enable)
4720 plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
7eb31a0b
VS
4721
4722 return plane_ctl;
4723}
4724
2e881264
VS
4725u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4726 const struct intel_plane_state *plane_state)
46f788ba
VS
4727{
4728 struct drm_i915_private *dev_priv =
f90a85e7 4729 to_i915(plane_state->uapi.plane->dev);
7b3cb17a
ML
4730 const struct drm_framebuffer *fb = plane_state->hw.fb;
4731 unsigned int rotation = plane_state->hw.rotation;
2e881264 4732 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
4733 u32 plane_ctl;
4734
4735 plane_ctl = PLANE_CTL_ENABLE;
4736
4036c78c 4737 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
b2081525 4738 plane_ctl |= skl_plane_ctl_alpha(plane_state);
7eb31a0b 4739 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
b0f5c0ba 4740
7b3cb17a 4741 if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
b0f5c0ba 4742 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
c8624ede 4743
7b3cb17a 4744 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
c8624ede 4745 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
46f788ba
VS
4746 }
4747
4748 plane_ctl |= skl_plane_ctl_format(fb->format->format);
4749 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
5f8e3f57
JL
4750 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4751
4752 if (INTEL_GEN(dev_priv) >= 10)
4753 plane_ctl |= cnl_plane_ctl_flip(rotation &
4754 DRM_MODE_REFLECT_MASK);
46f788ba 4755
2e881264
VS
4756 if (key->flags & I915_SET_COLORKEY_DESTINATION)
4757 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4758 else if (key->flags & I915_SET_COLORKEY_SOURCE)
4759 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4760
46f788ba
VS
4761 return plane_ctl;
4762}
4763
7eb31a0b
VS
4764u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4765{
2225f3c6 4766 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7eb31a0b
VS
4767 u32 plane_color_ctl = 0;
4768
4769 if (INTEL_GEN(dev_priv) >= 11)
4770 return plane_color_ctl;
4771
5f29ab23
VS
4772 if (crtc_state->gamma_enable)
4773 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4774
8271b2ef
VS
4775 if (crtc_state->csc_enable)
4776 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
7eb31a0b
VS
4777
4778 return plane_color_ctl;
4779}
4780
4036c78c
JA
4781u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4782 const struct intel_plane_state *plane_state)
4783{
42fd20ed 4784 struct drm_i915_private *dev_priv =
f90a85e7 4785 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 4786 const struct drm_framebuffer *fb = plane_state->hw.fb;
f90a85e7 4787 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4036c78c
JA
4788 u32 plane_color_ctl = 0;
4789
4036c78c 4790 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
b2081525 4791 plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4036c78c 4792
42fd20ed 4793 if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
a0196dd6
KK
4794 switch (plane_state->hw.color_encoding) {
4795 case DRM_COLOR_YCBCR_BT709:
b0f5c0ba 4796 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
a0196dd6
KK
4797 break;
4798 case DRM_COLOR_YCBCR_BT2020:
4799 plane_color_ctl |=
4800 PLANE_COLOR_CSC_MODE_YUV2020_TO_RGB2020;
4801 break;
4802 default:
4803 plane_color_ctl |=
4804 PLANE_COLOR_CSC_MODE_YUV601_TO_RGB601;
4805 }
7b3cb17a 4806 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
c8624ede 4807 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
bfe60a02
US
4808 } else if (fb->format->is_yuv) {
4809 plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
b0f5c0ba 4810 }
012d79e6 4811
4036c78c
JA
4812 return plane_color_ctl;
4813}
4814
73974893
ML
4815static int
4816__intel_display_resume(struct drm_device *dev,
581e49fe
ML
4817 struct drm_atomic_state *state,
4818 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
4819{
4820 struct drm_crtc_state *crtc_state;
4821 struct drm_crtc *crtc;
4822 int i, ret;
11c22da6 4823
aecd36b8 4824 intel_modeset_setup_hw_state(dev, ctx);
4fb87831 4825 intel_vga_redisable(to_i915(dev));
73974893
ML
4826
4827 if (!state)
4828 return 0;
4829
aa5e9b47
ML
4830 /*
4831 * We've duplicated the state, pointers to the old state are invalid.
4832 *
4833 * Don't attempt to use the old state until we commit the duplicated state.
4834 */
4835 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
4836 /*
4837 * Force recalculation even if we restore
4838 * current state. With fast modeset this may not result
4839 * in a modeset when the state is compatible.
4840 */
4841 crtc_state->mode_changed = true;
96a02917 4842 }
73974893
ML
4843
4844 /* ignore any reset values/BIOS leftovers in the WM registers */
b2ae318a 4845 if (!HAS_GMCH(to_i915(dev)))
602ae835 4846 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 4847
581e49fe 4848 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893 4849
e57291c2 4850 drm_WARN_ON(dev, ret == -EDEADLK);
73974893 4851 return ret;
96a02917
VS
4852}
4853
4ac2ba2f
VS
4854static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4855{
55277e1f 4856 return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
260e6b71 4857 intel_has_gpu_reset(&dev_priv->gt));
4ac2ba2f
VS
4858}
4859
87ebfaab 4860void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 4861{
73974893
ML
4862 struct drm_device *dev = &dev_priv->drm;
4863 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4864 struct drm_atomic_state *state;
4865 int ret;
4866
2c568805
JRS
4867 if (!HAS_DISPLAY(dev_priv))
4868 return;
4869
ce87ea15 4870 /* reset doesn't touch the display */
8a25c4be 4871 if (!dev_priv->params.force_reset_modeset_test &&
ce87ea15
DV
4872 !gpu_reset_clobbers_display(dev_priv))
4873 return;
4874
9db529aa 4875 /* We have a modeset vs reset deadlock, defensively unbreak it. */
cb823ed9
CW
4876 set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4877 smp_mb__after_atomic();
4878 wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
9db529aa
DV
4879
4880 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
cd49f818
WK
4881 drm_dbg_kms(&dev_priv->drm,
4882 "Modeset potentially stuck, unbreaking through wedging\n");
cb823ed9 4883 intel_gt_set_wedged(&dev_priv->gt);
9db529aa 4884 }
97154ec2 4885
73974893
ML
4886 /*
4887 * Need mode_config.mutex so that we don't
4888 * trample ongoing ->detect() and whatnot.
4889 */
4890 mutex_lock(&dev->mode_config.mutex);
4891 drm_modeset_acquire_init(ctx, 0);
4892 while (1) {
4893 ret = drm_modeset_lock_all_ctx(dev, ctx);
4894 if (ret != -EDEADLK)
4895 break;
4896
4897 drm_modeset_backoff(ctx);
4898 }
f98ce92f
VS
4899 /*
4900 * Disabling the crtcs gracefully seems nicer. Also the
4901 * g33 docs say we should at least disable all the planes.
4902 */
73974893
ML
4903 state = drm_atomic_helper_duplicate_state(dev, ctx);
4904 if (IS_ERR(state)) {
4905 ret = PTR_ERR(state);
cd49f818
WK
4906 drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4907 ret);
1e5a15d6 4908 return;
73974893
ML
4909 }
4910
4911 ret = drm_atomic_helper_disable_all(dev, ctx);
4912 if (ret) {
cd49f818
WK
4913 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4914 ret);
1e5a15d6
ACO
4915 drm_atomic_state_put(state);
4916 return;
73974893
ML
4917 }
4918
4919 dev_priv->modeset_restore_state = state;
4920 state->acquire_ctx = ctx;
7514747d
VS
4921}
4922
e669ad6f 4923void intel_display_finish_reset(struct drm_i915_private *dev_priv)
7514747d 4924{
73974893
ML
4925 struct drm_device *dev = &dev_priv->drm;
4926 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
40da1d31 4927 struct drm_atomic_state *state;
73974893
ML
4928 int ret;
4929
2c568805
JRS
4930 if (!HAS_DISPLAY(dev_priv))
4931 return;
4932
ce87ea15 4933 /* reset doesn't touch the display */
cb823ed9 4934 if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
ce87ea15
DV
4935 return;
4936
40da1d31 4937 state = fetch_and_zero(&dev_priv->modeset_restore_state);
ce87ea15
DV
4938 if (!state)
4939 goto unlock;
4940
7514747d 4941 /* reset doesn't touch the display */
4ac2ba2f 4942 if (!gpu_reset_clobbers_display(dev_priv)) {
ce87ea15
DV
4943 /* for testing only restore the display */
4944 ret = __intel_display_resume(dev, state, ctx);
942d5d0d 4945 if (ret)
cd49f818
WK
4946 drm_err(&dev_priv->drm,
4947 "Restoring old state failed with %i\n", ret);
73974893
ML
4948 } else {
4949 /*
4950 * The display has been reset as well,
4951 * so need a full re-initialization.
4952 */
51f59205 4953 intel_pps_unlock_regs_wa(dev_priv);
6cd02e77 4954 intel_modeset_init_hw(dev_priv);
f72b84c6 4955 intel_init_clock_gating(dev_priv);
4c8d4651 4956 intel_hpd_init(dev_priv);
7514747d 4957
581e49fe 4958 ret = __intel_display_resume(dev, state, ctx);
73974893 4959 if (ret)
cd49f818
WK
4960 drm_err(&dev_priv->drm,
4961 "Restoring old state failed with %i\n", ret);
7514747d 4962
4c8d4651 4963 intel_hpd_poll_disable(dev_priv);
73974893 4964 }
7514747d 4965
ce87ea15
DV
4966 drm_atomic_state_put(state);
4967unlock:
73974893
ML
4968 drm_modeset_drop_locks(ctx);
4969 drm_modeset_acquire_fini(ctx);
4970 mutex_unlock(&dev->mode_config.mutex);
9db529aa 4971
cb823ed9 4972 clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
7514747d
VS
4973}
4974
d1622119
VS
4975static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4976{
4977 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4978 enum pipe pipe = crtc->pipe;
4979 u32 tmp;
4980
dc008bf0 4981 tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
d1622119
VS
4982
4983 /*
4984 * Display WA #1153: icl
4985 * enable hardware to bypass the alpha math
4986 * and rounding for per-pixel values 00 and 0xff
4987 */
4988 tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
26eeea15
AS
4989 /*
4990 * Display WA # 1605353570: icl
4991 * Set the pixel rounding bit to 1 for allowing
4992 * passthrough of Frame buffer pixels unmodified
4993 * across pipe
4994 */
4995 tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
dc008bf0 4996 intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
d1622119
VS
4997}
4998
4cbe4b2b 4999static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 5000{
4cbe4b2b 5001 struct drm_device *dev = crtc->base.dev;
fac5e23e 5002 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5003 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5004 i915_reg_t reg;
5005 u32 temp;
5e84e1a4
ZW
5006
5007 /* enable normal train */
5008 reg = FDI_TX_CTL(pipe);
dc008bf0 5009 temp = intel_de_read(dev_priv, reg);
fd6b8f43 5010 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
5011 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5012 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
5013 } else {
5014 temp &= ~FDI_LINK_TRAIN_NONE;
5015 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 5016 }
dc008bf0 5017 intel_de_write(dev_priv, reg, temp);
5e84e1a4
ZW
5018
5019 reg = FDI_RX_CTL(pipe);
dc008bf0 5020 temp = intel_de_read(dev_priv, reg);
6e266956 5021 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
5022 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5023 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5024 } else {
5025 temp &= ~FDI_LINK_TRAIN_NONE;
5026 temp |= FDI_LINK_TRAIN_NONE;
5027 }
dc008bf0 5028 intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5e84e1a4
ZW
5029
5030 /* wait one idle pattern time */
dc008bf0 5031 intel_de_posting_read(dev_priv, reg);
5e84e1a4 5032 udelay(1000);
357555c0
JB
5033
5034 /* IVB wants error correction enabled */
fd6b8f43 5035 if (IS_IVYBRIDGE(dev_priv))
dc008bf0
JN
5036 intel_de_write(dev_priv, reg,
5037 intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
5038}
5039
8db9d77b 5040/* The FDI link training functions for ILK/Ibexpeak. */
9eae5e27
LDM
5041static void ilk_fdi_link_train(struct intel_crtc *crtc,
5042 const struct intel_crtc_state *crtc_state)
8db9d77b 5043{
4cbe4b2b 5044 struct drm_device *dev = crtc->base.dev;
fac5e23e 5045 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5046 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5047 i915_reg_t reg;
5048 u32 temp, tries;
8db9d77b 5049
1c8562f6 5050 /* FDI needs bits from pipe first */
b104e8b2 5051 assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
0fc932b8 5052
e1a44743
AJ
5053 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5054 for train result */
5eddb70b 5055 reg = FDI_RX_IMR(pipe);
dc008bf0 5056 temp = intel_de_read(dev_priv, reg);
e1a44743
AJ
5057 temp &= ~FDI_RX_SYMBOL_LOCK;
5058 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0
JN
5059 intel_de_write(dev_priv, reg, temp);
5060 intel_de_read(dev_priv, reg);
e1a44743
AJ
5061 udelay(150);
5062
8db9d77b 5063 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b 5064 reg = FDI_TX_CTL(pipe);
dc008bf0 5065 temp = intel_de_read(dev_priv, reg);
627eb5a3 5066 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5067 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
5068 temp &= ~FDI_LINK_TRAIN_NONE;
5069 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5070 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
8db9d77b 5071
5eddb70b 5072 reg = FDI_RX_CTL(pipe);
dc008bf0 5073 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5074 temp &= ~FDI_LINK_TRAIN_NONE;
5075 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5076 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5eddb70b 5077
dc008bf0 5078 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5079 udelay(150);
5080
5b2adf89 5081 /* Ironlake workaround, enable clock pointer after FDI enable*/
dc008bf0
JN
5082 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5083 FDI_RX_PHASE_SYNC_POINTER_OVR);
5084 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5085 FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 5086
5eddb70b 5087 reg = FDI_RX_IIR(pipe);
e1a44743 5088 for (tries = 0; tries < 5; tries++) {
dc008bf0 5089 temp = intel_de_read(dev_priv, reg);
cd49f818 5090 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
8db9d77b
ZW
5091
5092 if ((temp & FDI_RX_BIT_LOCK)) {
cd49f818 5093 drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
dc008bf0 5094 intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
5095 break;
5096 }
8db9d77b 5097 }
e1a44743 5098 if (tries == 5)
cd49f818 5099 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
8db9d77b
ZW
5100
5101 /* Train 2 */
5eddb70b 5102 reg = FDI_TX_CTL(pipe);
dc008bf0 5103 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5104 temp &= ~FDI_LINK_TRAIN_NONE;
5105 temp |= FDI_LINK_TRAIN_PATTERN_2;
dc008bf0 5106 intel_de_write(dev_priv, reg, temp);
8db9d77b 5107
5eddb70b 5108 reg = FDI_RX_CTL(pipe);
dc008bf0 5109 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5110 temp &= ~FDI_LINK_TRAIN_NONE;
5111 temp |= FDI_LINK_TRAIN_PATTERN_2;
dc008bf0 5112 intel_de_write(dev_priv, reg, temp);
8db9d77b 5113
dc008bf0 5114 intel_de_posting_read(dev_priv, reg);
5eddb70b 5115 udelay(150);
8db9d77b 5116
5eddb70b 5117 reg = FDI_RX_IIR(pipe);
e1a44743 5118 for (tries = 0; tries < 5; tries++) {
dc008bf0 5119 temp = intel_de_read(dev_priv, reg);
cd49f818 5120 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
8db9d77b
ZW
5121
5122 if (temp & FDI_RX_SYMBOL_LOCK) {
dc008bf0
JN
5123 intel_de_write(dev_priv, reg,
5124 temp | FDI_RX_SYMBOL_LOCK);
cd49f818 5125 drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
8db9d77b
ZW
5126 break;
5127 }
8db9d77b 5128 }
e1a44743 5129 if (tries == 5)
cd49f818 5130 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
8db9d77b 5131
cd49f818 5132 drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5c5313c8 5133
8db9d77b
ZW
5134}
5135
0206e353 5136static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
5137 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5138 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5139 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5140 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5141};
5142
5143/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
5144static void gen6_fdi_link_train(struct intel_crtc *crtc,
5145 const struct intel_crtc_state *crtc_state)
8db9d77b 5146{
4cbe4b2b 5147 struct drm_device *dev = crtc->base.dev;
fac5e23e 5148 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5149 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5150 i915_reg_t reg;
5151 u32 temp, i, retry;
8db9d77b 5152
e1a44743
AJ
5153 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5154 for train result */
5eddb70b 5155 reg = FDI_RX_IMR(pipe);
dc008bf0 5156 temp = intel_de_read(dev_priv, reg);
e1a44743
AJ
5157 temp &= ~FDI_RX_SYMBOL_LOCK;
5158 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0 5159 intel_de_write(dev_priv, reg, temp);
5eddb70b 5160
dc008bf0 5161 intel_de_posting_read(dev_priv, reg);
e1a44743
AJ
5162 udelay(150);
5163
8db9d77b 5164 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b 5165 reg = FDI_TX_CTL(pipe);
dc008bf0 5166 temp = intel_de_read(dev_priv, reg);
627eb5a3 5167 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5168 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
5169 temp &= ~FDI_LINK_TRAIN_NONE;
5170 temp |= FDI_LINK_TRAIN_PATTERN_1;
5171 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5172 /* SNB-B */
5173 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
dc008bf0 5174 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
8db9d77b 5175
dc008bf0
JN
5176 intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5177 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
d74cf324 5178
5eddb70b 5179 reg = FDI_RX_CTL(pipe);
dc008bf0 5180 temp = intel_de_read(dev_priv, reg);
6e266956 5181 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
5182 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5183 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5184 } else {
5185 temp &= ~FDI_LINK_TRAIN_NONE;
5186 temp |= FDI_LINK_TRAIN_PATTERN_1;
5187 }
dc008bf0 5188 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5eddb70b 5189
dc008bf0 5190 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5191 udelay(150);
5192
0206e353 5193 for (i = 0; i < 4; i++) {
5eddb70b 5194 reg = FDI_TX_CTL(pipe);
dc008bf0 5195 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5196 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5197 temp |= snb_b_fdi_train_param[i];
dc008bf0 5198 intel_de_write(dev_priv, reg, temp);
5eddb70b 5199
dc008bf0 5200 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5201 udelay(500);
5202
fa37d39e
SP
5203 for (retry = 0; retry < 5; retry++) {
5204 reg = FDI_RX_IIR(pipe);
dc008bf0 5205 temp = intel_de_read(dev_priv, reg);
cd49f818 5206 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
fa37d39e 5207 if (temp & FDI_RX_BIT_LOCK) {
dc008bf0
JN
5208 intel_de_write(dev_priv, reg,
5209 temp | FDI_RX_BIT_LOCK);
cd49f818
WK
5210 drm_dbg_kms(&dev_priv->drm,
5211 "FDI train 1 done.\n");
fa37d39e
SP
5212 break;
5213 }
5214 udelay(50);
8db9d77b 5215 }
fa37d39e
SP
5216 if (retry < 5)
5217 break;
8db9d77b
ZW
5218 }
5219 if (i == 4)
cd49f818 5220 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
8db9d77b
ZW
5221
5222 /* Train 2 */
5eddb70b 5223 reg = FDI_TX_CTL(pipe);
dc008bf0 5224 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5225 temp &= ~FDI_LINK_TRAIN_NONE;
5226 temp |= FDI_LINK_TRAIN_PATTERN_2;
cf819eff 5227 if (IS_GEN(dev_priv, 6)) {
8db9d77b
ZW
5228 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5229 /* SNB-B */
5230 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5231 }
dc008bf0 5232 intel_de_write(dev_priv, reg, temp);
8db9d77b 5233
5eddb70b 5234 reg = FDI_RX_CTL(pipe);
dc008bf0 5235 temp = intel_de_read(dev_priv, reg);
6e266956 5236 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
5237 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5238 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5239 } else {
5240 temp &= ~FDI_LINK_TRAIN_NONE;
5241 temp |= FDI_LINK_TRAIN_PATTERN_2;
5242 }
dc008bf0 5243 intel_de_write(dev_priv, reg, temp);
5eddb70b 5244
dc008bf0 5245 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5246 udelay(150);
5247
0206e353 5248 for (i = 0; i < 4; i++) {
5eddb70b 5249 reg = FDI_TX_CTL(pipe);
dc008bf0 5250 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5251 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5252 temp |= snb_b_fdi_train_param[i];
dc008bf0 5253 intel_de_write(dev_priv, reg, temp);
5eddb70b 5254
dc008bf0 5255 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5256 udelay(500);
5257
fa37d39e
SP
5258 for (retry = 0; retry < 5; retry++) {
5259 reg = FDI_RX_IIR(pipe);
dc008bf0 5260 temp = intel_de_read(dev_priv, reg);
cd49f818 5261 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
fa37d39e 5262 if (temp & FDI_RX_SYMBOL_LOCK) {
dc008bf0
JN
5263 intel_de_write(dev_priv, reg,
5264 temp | FDI_RX_SYMBOL_LOCK);
cd49f818
WK
5265 drm_dbg_kms(&dev_priv->drm,
5266 "FDI train 2 done.\n");
fa37d39e
SP
5267 break;
5268 }
5269 udelay(50);
8db9d77b 5270 }
fa37d39e
SP
5271 if (retry < 5)
5272 break;
8db9d77b
ZW
5273 }
5274 if (i == 4)
cd49f818 5275 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
8db9d77b 5276
cd49f818 5277 drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
8db9d77b
ZW
5278}
5279
357555c0 5280/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
5281static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5282 const struct intel_crtc_state *crtc_state)
357555c0 5283{
4cbe4b2b 5284 struct drm_device *dev = crtc->base.dev;
fac5e23e 5285 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5286 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5287 i915_reg_t reg;
5288 u32 temp, i, j;
357555c0
JB
5289
5290 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5291 for train result */
5292 reg = FDI_RX_IMR(pipe);
dc008bf0 5293 temp = intel_de_read(dev_priv, reg);
357555c0
JB
5294 temp &= ~FDI_RX_SYMBOL_LOCK;
5295 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0 5296 intel_de_write(dev_priv, reg, temp);
357555c0 5297
dc008bf0 5298 intel_de_posting_read(dev_priv, reg);
357555c0
JB
5299 udelay(150);
5300
cd49f818 5301 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
dc008bf0 5302 intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
01a415fd 5303
139ccd3f
JB
5304 /* Try each vswing and preemphasis setting twice before moving on */
5305 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5306 /* disable first in case we need to retry */
5307 reg = FDI_TX_CTL(pipe);
dc008bf0 5308 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5309 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5310 temp &= ~FDI_TX_ENABLE;
dc008bf0 5311 intel_de_write(dev_priv, reg, temp);
357555c0 5312
139ccd3f 5313 reg = FDI_RX_CTL(pipe);
dc008bf0 5314 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5315 temp &= ~FDI_LINK_TRAIN_AUTO;
5316 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5317 temp &= ~FDI_RX_ENABLE;
dc008bf0 5318 intel_de_write(dev_priv, reg, temp);
357555c0 5319
139ccd3f 5320 /* enable CPU FDI TX and PCH FDI RX */
357555c0 5321 reg = FDI_TX_CTL(pipe);
dc008bf0 5322 temp = intel_de_read(dev_priv, reg);
139ccd3f 5323 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5324 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 5325 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 5326 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
5327 temp |= snb_b_fdi_train_param[j/2];
5328 temp |= FDI_COMPOSITE_SYNC;
dc008bf0 5329 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
357555c0 5330
dc008bf0
JN
5331 intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5332 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 5333
139ccd3f 5334 reg = FDI_RX_CTL(pipe);
dc008bf0 5335 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5336 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5337 temp |= FDI_COMPOSITE_SYNC;
dc008bf0 5338 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
357555c0 5339
dc008bf0 5340 intel_de_posting_read(dev_priv, reg);
139ccd3f 5341 udelay(1); /* should be 0.5us */
357555c0 5342
139ccd3f
JB
5343 for (i = 0; i < 4; i++) {
5344 reg = FDI_RX_IIR(pipe);
dc008bf0 5345 temp = intel_de_read(dev_priv, reg);
cd49f818 5346 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
357555c0 5347
139ccd3f 5348 if (temp & FDI_RX_BIT_LOCK ||
dc008bf0
JN
5349 (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5350 intel_de_write(dev_priv, reg,
5351 temp | FDI_RX_BIT_LOCK);
cd49f818
WK
5352 drm_dbg_kms(&dev_priv->drm,
5353 "FDI train 1 done, level %i.\n",
5354 i);
139ccd3f
JB
5355 break;
5356 }
5357 udelay(1); /* should be 0.5us */
5358 }
5359 if (i == 4) {
cd49f818
WK
5360 drm_dbg_kms(&dev_priv->drm,
5361 "FDI train 1 fail on vswing %d\n", j / 2);
139ccd3f
JB
5362 continue;
5363 }
357555c0 5364
139ccd3f 5365 /* Train 2 */
357555c0 5366 reg = FDI_TX_CTL(pipe);
dc008bf0 5367 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5368 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5369 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
dc008bf0 5370 intel_de_write(dev_priv, reg, temp);
139ccd3f
JB
5371
5372 reg = FDI_RX_CTL(pipe);
dc008bf0 5373 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5374 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5375 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
dc008bf0 5376 intel_de_write(dev_priv, reg, temp);
357555c0 5377
dc008bf0 5378 intel_de_posting_read(dev_priv, reg);
139ccd3f 5379 udelay(2); /* should be 1.5us */
357555c0 5380
139ccd3f
JB
5381 for (i = 0; i < 4; i++) {
5382 reg = FDI_RX_IIR(pipe);
dc008bf0 5383 temp = intel_de_read(dev_priv, reg);
cd49f818 5384 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
357555c0 5385
139ccd3f 5386 if (temp & FDI_RX_SYMBOL_LOCK ||
dc008bf0
JN
5387 (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5388 intel_de_write(dev_priv, reg,
5389 temp | FDI_RX_SYMBOL_LOCK);
cd49f818
WK
5390 drm_dbg_kms(&dev_priv->drm,
5391 "FDI train 2 done, level %i.\n",
5392 i);
139ccd3f
JB
5393 goto train_done;
5394 }
5395 udelay(2); /* should be 1.5us */
357555c0 5396 }
139ccd3f 5397 if (i == 4)
cd49f818
WK
5398 drm_dbg_kms(&dev_priv->drm,
5399 "FDI train 2 fail on vswing %d\n", j / 2);
357555c0 5400 }
357555c0 5401
139ccd3f 5402train_done:
cd49f818 5403 drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
357555c0
JB
5404}
5405
9eae5e27 5406static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
2c07245f 5407{
2225f3c6 5408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2354c78 5409 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
d048a268 5410 enum pipe pipe = intel_crtc->pipe;
f0f59a00
VS
5411 i915_reg_t reg;
5412 u32 temp;
c64e311e 5413
c98e9dcf 5414 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b 5415 reg = FDI_RX_CTL(pipe);
dc008bf0 5416 temp = intel_de_read(dev_priv, reg);
627eb5a3 5417 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
b2354c78 5418 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
dc008bf0
JN
5419 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5420 intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5eddb70b 5421
dc008bf0 5422 intel_de_posting_read(dev_priv, reg);
c98e9dcf
JB
5423 udelay(200);
5424
5425 /* Switch from Rawclk to PCDclk */
dc008bf0
JN
5426 temp = intel_de_read(dev_priv, reg);
5427 intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5eddb70b 5428
dc008bf0 5429 intel_de_posting_read(dev_priv, reg);
c98e9dcf
JB
5430 udelay(200);
5431
20749730
PZ
5432 /* Enable CPU FDI TX PLL, always on for Ironlake */
5433 reg = FDI_TX_CTL(pipe);
dc008bf0 5434 temp = intel_de_read(dev_priv, reg);
20749730 5435 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
dc008bf0 5436 intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 5437
dc008bf0 5438 intel_de_posting_read(dev_priv, reg);
20749730 5439 udelay(100);
6be4a607 5440 }
0e23b99d
JB
5441}
5442
9eae5e27 5443static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
88cefb6c
DV
5444{
5445 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 5446 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5447 enum pipe pipe = intel_crtc->pipe;
f0f59a00
VS
5448 i915_reg_t reg;
5449 u32 temp;
88cefb6c
DV
5450
5451 /* Switch from PCDclk to Rawclk */
5452 reg = FDI_RX_CTL(pipe);
dc008bf0
JN
5453 temp = intel_de_read(dev_priv, reg);
5454 intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
88cefb6c
DV
5455
5456 /* Disable CPU FDI TX PLL */
5457 reg = FDI_TX_CTL(pipe);
dc008bf0
JN
5458 temp = intel_de_read(dev_priv, reg);
5459 intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
88cefb6c 5460
dc008bf0 5461 intel_de_posting_read(dev_priv, reg);
88cefb6c
DV
5462 udelay(100);
5463
5464 reg = FDI_RX_CTL(pipe);
dc008bf0
JN
5465 temp = intel_de_read(dev_priv, reg);
5466 intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
88cefb6c
DV
5467
5468 /* Wait for the clocks to turn off. */
dc008bf0 5469 intel_de_posting_read(dev_priv, reg);
88cefb6c
DV
5470 udelay(100);
5471}
5472
9eae5e27 5473static void ilk_fdi_disable(struct intel_crtc *crtc)
0fc932b8 5474{
5b4f4e94
VS
5475 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5476 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5477 i915_reg_t reg;
5478 u32 temp;
0fc932b8
JB
5479
5480 /* disable CPU FDI tx and PCH FDI rx */
5481 reg = FDI_TX_CTL(pipe);
dc008bf0
JN
5482 temp = intel_de_read(dev_priv, reg);
5483 intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5484 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5485
5486 reg = FDI_RX_CTL(pipe);
dc008bf0 5487 temp = intel_de_read(dev_priv, reg);
0fc932b8 5488 temp &= ~(0x7 << 16);
dc008bf0
JN
5489 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5490 intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
0fc932b8 5491
dc008bf0 5492 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5493 udelay(100);
5494
5495 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 5496 if (HAS_PCH_IBX(dev_priv))
dc008bf0
JN
5497 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5498 FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
5499
5500 /* still set train pattern 1 */
5501 reg = FDI_TX_CTL(pipe);
dc008bf0 5502 temp = intel_de_read(dev_priv, reg);
0fc932b8
JB
5503 temp &= ~FDI_LINK_TRAIN_NONE;
5504 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5505 intel_de_write(dev_priv, reg, temp);
0fc932b8
JB
5506
5507 reg = FDI_RX_CTL(pipe);
dc008bf0 5508 temp = intel_de_read(dev_priv, reg);
6e266956 5509 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
5510 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5511 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5512 } else {
5513 temp &= ~FDI_LINK_TRAIN_NONE;
5514 temp |= FDI_LINK_TRAIN_PATTERN_1;
5515 }
5516 /* BPC in FDI rx is consistent with that in PIPECONF */
5517 temp &= ~(0x07 << 16);
dc008bf0
JN
5518 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5519 intel_de_write(dev_priv, reg, temp);
0fc932b8 5520
dc008bf0 5521 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5522 udelay(100);
5523}
5524
49d73912 5525bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93 5526{
fa05887a
DV
5527 struct drm_crtc *crtc;
5528 bool cleanup_done;
5529
5530 drm_for_each_crtc(crtc, &dev_priv->drm) {
5531 struct drm_crtc_commit *commit;
5532 spin_lock(&crtc->commit_lock);
5533 commit = list_first_entry_or_null(&crtc->commit_list,
5534 struct drm_crtc_commit, commit_entry);
5535 cleanup_done = commit ?
5536 try_wait_for_completion(&commit->cleanup_done) : true;
5537 spin_unlock(&crtc->commit_lock);
5538
5539 if (cleanup_done)
5dce5b93
CW
5540 continue;
5541
fa05887a 5542 drm_crtc_wait_one_vblank(crtc);
5dce5b93
CW
5543
5544 return true;
5545 }
5546
5547 return false;
5548}
5549
b7076546 5550void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
5551{
5552 u32 temp;
5553
dc008bf0 5554 intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
060f02d8
VS
5555
5556 mutex_lock(&dev_priv->sb_lock);
5557
5558 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5559 temp |= SBI_SSCCTL_DISABLE;
5560 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5561
5562 mutex_unlock(&dev_priv->sb_lock);
5563}
5564
e615efe4 5565/* Program iCLKIP clock to the desired frequency */
c5b36fac 5566static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
e615efe4 5567{
2225f3c6 5568 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0dcdc382 5569 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1326a92c 5570 int clock = crtc_state->hw.adjusted_mode.crtc_clock;
e615efe4
ED
5571 u32 divsel, phaseinc, auxdiv, phasedir = 0;
5572 u32 temp;
5573
060f02d8 5574 lpt_disable_iclkip(dev_priv);
e615efe4 5575
64b46a06
VS
5576 /* The iCLK virtual clock root frequency is in MHz,
5577 * but the adjusted_mode->crtc_clock in in KHz. To get the
5578 * divisors, it is necessary to divide one by another, so we
5579 * convert the virtual clock precision to KHz here for higher
5580 * precision.
5581 */
5582 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
5583 u32 iclk_virtual_root_freq = 172800 * 1000;
5584 u32 iclk_pi_range = 64;
64b46a06 5585 u32 desired_divisor;
e615efe4 5586
64b46a06
VS
5587 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5588 clock << auxdiv);
5589 divsel = (desired_divisor / iclk_pi_range) - 2;
5590 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 5591
64b46a06
VS
5592 /*
5593 * Near 20MHz is a corner case which is
5594 * out of range for the 7-bit divisor
5595 */
5596 if (divsel <= 0x7f)
5597 break;
e615efe4
ED
5598 }
5599
5600 /* This should not happen with any sane values */
e57291c2
PB
5601 drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5602 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5603 drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5604 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
e615efe4 5605
cd49f818
WK
5606 drm_dbg_kms(&dev_priv->drm,
5607 "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5608 clock, auxdiv, divsel, phasedir, phaseinc);
e615efe4 5609
060f02d8
VS
5610 mutex_lock(&dev_priv->sb_lock);
5611
e615efe4 5612 /* Program SSCDIVINTPHASE6 */
988d6ee8 5613 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
5614 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5615 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5616 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5617 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5618 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5619 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 5620 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
5621
5622 /* Program SSCAUXDIV */
988d6ee8 5623 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
5624 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5625 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 5626 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
5627
5628 /* Enable modulator and associated divider */
988d6ee8 5629 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 5630 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 5631 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 5632
060f02d8
VS
5633 mutex_unlock(&dev_priv->sb_lock);
5634
e615efe4
ED
5635 /* Wait for initialization time */
5636 udelay(24);
5637
dc008bf0 5638 intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
e615efe4
ED
5639}
5640
8802e5b6
VS
5641int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5642{
5643 u32 divsel, phaseinc, auxdiv;
5644 u32 iclk_virtual_root_freq = 172800 * 1000;
5645 u32 iclk_pi_range = 64;
5646 u32 desired_divisor;
5647 u32 temp;
5648
dc008bf0 5649 if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
8802e5b6
VS
5650 return 0;
5651
5652 mutex_lock(&dev_priv->sb_lock);
5653
5654 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5655 if (temp & SBI_SSCCTL_DISABLE) {
5656 mutex_unlock(&dev_priv->sb_lock);
5657 return 0;
5658 }
5659
5660 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5661 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5662 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5663 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5664 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5665
5666 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5667 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5668 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5669
5670 mutex_unlock(&dev_priv->sb_lock);
5671
5672 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5673
5674 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5675 desired_divisor << auxdiv);
5676}
5677
9eae5e27
LDM
5678static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5679 enum pipe pch_transcoder)
275f01b2 5680{
2225f3c6 5681 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5e1cdf54
ML
5682 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5683 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
275f01b2 5684
dc008bf0
JN
5685 intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5686 intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5687 intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5688 intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5689 intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5690 intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
275f01b2 5691
dc008bf0
JN
5692 intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5693 intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5694 intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5695 intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5696 intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5697 intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5698 intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5699 intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
275f01b2
DV
5700}
5701
b0b62d84 5702static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
1fbc0d78 5703{
ba3f4d0a 5704 u32 temp;
1fbc0d78 5705
dc008bf0 5706 temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
003632d9 5707 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
5708 return;
5709
e57291c2
PB
5710 drm_WARN_ON(&dev_priv->drm,
5711 intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5712 FDI_RX_ENABLE);
5713 drm_WARN_ON(&dev_priv->drm,
5714 intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5715 FDI_RX_ENABLE);
1fbc0d78 5716
003632d9
ACO
5717 temp &= ~FDI_BC_BIFURCATION_SELECT;
5718 if (enable)
5719 temp |= FDI_BC_BIFURCATION_SELECT;
5720
cd49f818
WK
5721 drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5722 enable ? "en" : "dis");
dc008bf0
JN
5723 intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5724 intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
1fbc0d78
DV
5725}
5726
74bb98ba 5727static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
1fbc0d78 5728{
2225f3c6 5729 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b0b62d84 5730 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1fbc0d78 5731
b0b62d84 5732 switch (crtc->pipe) {
1fbc0d78
DV
5733 case PIPE_A:
5734 break;
5735 case PIPE_B:
b0b62d84
ML
5736 if (crtc_state->fdi_lanes > 2)
5737 cpt_set_fdi_bc_bifurcation(dev_priv, false);
1fbc0d78 5738 else
b0b62d84 5739 cpt_set_fdi_bc_bifurcation(dev_priv, true);
1fbc0d78
DV
5740
5741 break;
5742 case PIPE_C:
b0b62d84 5743 cpt_set_fdi_bc_bifurcation(dev_priv, true);
1fbc0d78
DV
5744
5745 break;
5746 default:
5747 BUG();
5748 }
5749}
5750
f606bc6d
VS
5751/*
5752 * Finds the encoder associated with the given CRTC. This can only be
5753 * used when we know that the CRTC isn't feeding multiple encoders!
5754 */
5755static struct intel_encoder *
5a0b385e
VS
5756intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5757 const struct intel_crtc_state *crtc_state)
f606bc6d 5758{
2225f3c6 5759 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
f606bc6d
VS
5760 const struct drm_connector_state *connector_state;
5761 const struct drm_connector *connector;
5762 struct intel_encoder *encoder = NULL;
5763 int num_encoders = 0;
5764 int i;
5765
5a0b385e 5766 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
f606bc6d
VS
5767 if (connector_state->crtc != &crtc->base)
5768 continue;
5769
5770 encoder = to_intel_encoder(connector_state->best_encoder);
5771 num_encoders++;
5772 }
5773
3a47ae20
PB
5774 drm_WARN(encoder->base.dev, num_encoders != 1,
5775 "%d encoders for pipe %c\n",
5776 num_encoders, pipe_name(crtc->pipe));
f606bc6d
VS
5777
5778 return encoder;
5779}
5780
f67a559d
JB
5781/*
5782 * Enable PCH resources required for PCH ports:
5783 * - PCH PLLs
5784 * - FDI training & RX/TX
5785 * - update transcoder timings
5786 * - DP transcoding bits
5787 * - transcoder
5788 */
9eae5e27
LDM
5789static void ilk_pch_enable(const struct intel_atomic_state *state,
5790 const struct intel_crtc_state *crtc_state)
0e23b99d 5791{
2225f3c6 5792 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4cbe4b2b 5793 struct drm_device *dev = crtc->base.dev;
fac5e23e 5794 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5795 enum pipe pipe = crtc->pipe;
f0f59a00 5796 u32 temp;
2c07245f 5797
ab9412ba 5798 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 5799
fd6b8f43 5800 if (IS_IVYBRIDGE(dev_priv))
74bb98ba 5801 ivb_update_fdi_bc_bifurcation(crtc_state);
1fbc0d78 5802
cd986abb
DV
5803 /* Write the TU size bits before fdi link training, so that error
5804 * detection works. */
dc008bf0
JN
5805 intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5806 intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
cd986abb 5807
c98e9dcf 5808 /* For PCH output, training FDI link */
dc4a1094 5809 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 5810
3ad8a208
DV
5811 /* We need to program the right clock selection before writing the pixel
5812 * mutliplier into the DPLL. */
6e266956 5813 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 5814 u32 sel;
4b645f14 5815
dc008bf0 5816 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
11887397
DV
5817 temp |= TRANS_DPLL_ENABLE(pipe);
5818 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 5819 if (crtc_state->shared_dpll ==
8106ddbd 5820 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
5821 temp |= sel;
5822 else
5823 temp &= ~sel;
dc008bf0 5824 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
c98e9dcf 5825 }
5eddb70b 5826
3ad8a208
DV
5827 /* XXX: pch pll's can be enabled any time before we enable the PCH
5828 * transcoder, and we actually should do this to not upset any PCH
5829 * transcoder that already use the clock when we share it.
5830 *
5831 * Note that enable_shared_dpll tries to do the right thing, but
5832 * get_shared_dpll unconditionally resets the pll - we need that to have
5833 * the right LVDS enable sequence. */
65c307fd 5834 intel_enable_shared_dpll(crtc_state);
3ad8a208 5835
d9b6cb56
JB
5836 /* set transcoder timing, panel must allow it */
5837 assert_panel_unlocked(dev_priv, pipe);
9eae5e27 5838 ilk_pch_transcoder_set_timings(crtc_state, pipe);
8db9d77b 5839
303b81e0 5840 intel_fdi_normal_train(crtc);
5e84e1a4 5841
c98e9dcf 5842 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 5843 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 5844 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 5845 const struct drm_display_mode *adjusted_mode =
1326a92c 5846 &crtc_state->hw.adjusted_mode;
dc008bf0 5847 u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 5848 i915_reg_t reg = TRANS_DP_CTL(pipe);
f67dc6d8
VS
5849 enum port port;
5850
dc008bf0 5851 temp = intel_de_read(dev_priv, reg);
5eddb70b 5852 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
5853 TRANS_DP_SYNC_MASK |
5854 TRANS_DP_BPC_MASK);
e3ef4479 5855 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 5856 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 5857
9c4edaee 5858 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 5859 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 5860 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 5861 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf 5862
5a0b385e 5863 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
e57291c2 5864 drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
f67dc6d8 5865 temp |= TRANS_DP_PORT_SEL(port);
2c07245f 5866
dc008bf0 5867 intel_de_write(dev_priv, reg, temp);
6be4a607 5868 }
b52eb4dc 5869
9eae5e27 5870 ilk_enable_pch_transcoder(crtc_state);
f67a559d
JB
5871}
5872
21fd23ac 5873void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 5874{
2225f3c6 5875 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0dcdc382 5876 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 5877 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 5878
a2196033 5879 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1507e5bd 5880
c5b36fac 5881 lpt_program_iclkip(crtc_state);
1507e5bd 5882
0540e488 5883 /* Set transcoder timing. */
9eae5e27 5884 ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
1507e5bd 5885
937bb610 5886 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
5887}
5888
c684fb44
VS
5889static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5890 enum pipe pipe)
d4270e57 5891{
f0f59a00 5892 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
5893 u32 temp;
5894
dc008bf0 5895 temp = intel_de_read(dev_priv, dslreg);
d4270e57 5896 udelay(500);
dc008bf0
JN
5897 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5898 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
cd49f818
WK
5899 drm_err(&dev_priv->drm,
5900 "mode set failed: pipe %c stuck\n",
5901 pipe_name(pipe));
d4270e57
JB
5902 }
5903}
5904
0a59952b
VS
5905/*
5906 * The hardware phase 0.0 refers to the center of the pixel.
5907 * We want to start from the top/left edge which is phase
5908 * -0.5. That matches how the hardware calculates the scaling
5909 * factors (from top-left of the first pixel to bottom-right
5910 * of the last pixel, as opposed to the pixel centers).
5911 *
5912 * For 4:2:0 subsampled chroma planes we obviously have to
5913 * adjust that so that the chroma sample position lands in
5914 * the right spot.
5915 *
5916 * Note that for packed YCbCr 4:2:2 formats there is no way to
5917 * control chroma siting. The hardware simply replicates the
5918 * chroma samples for both of the luma samples, and thus we don't
5919 * actually get the expected MPEG2 chroma siting convention :(
5920 * The same behaviour is observed on pre-SKL platforms as well.
e7a278a3
VS
5921 *
5922 * Theory behind the formula (note that we ignore sub-pixel
5923 * source coordinates):
5924 * s = source sample position
5925 * d = destination sample position
5926 *
5927 * Downscaling 4:1:
5928 * -0.5
5929 * | 0.0
5930 * | | 1.5 (initial phase)
5931 * | | |
5932 * v v v
5933 * | s | s | s | s |
5934 * | d |
5935 *
5936 * Upscaling 1:4:
5937 * -0.5
5938 * | -0.375 (initial phase)
5939 * | | 0.0
5940 * | | |
5941 * v v v
5942 * | s |
5943 * | d | d | d | d |
0a59952b 5944 */
e7a278a3 5945u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
0a59952b
VS
5946{
5947 int phase = -0x8000;
5948 u16 trip = 0;
5949
5950 if (chroma_cosited)
5951 phase += (sub - 1) * 0x8000 / sub;
5952
e7a278a3
VS
5953 phase += scale / (2 * sub);
5954
5955 /*
5956 * Hardware initial phase limited to [-0.5:1.5].
5957 * Since the max hardware scale factor is 3.0, we
5958 * should never actually excdeed 1.0 here.
5959 */
5960 WARN_ON(phase < -0x8000 || phase > 0x18000);
5961
0a59952b
VS
5962 if (phase < 0)
5963 phase = 0x10000 + phase;
5964 else
5965 trip = PS_PHASE_TRIP;
5966
5967 return ((phase >> 2) & PS_PHASE_MASK) | trip;
5968}
5969
69f44d3b
JN
5970#define SKL_MIN_SRC_W 8
5971#define SKL_MAX_SRC_W 4096
5972#define SKL_MIN_SRC_H 8
5973#define SKL_MAX_SRC_H 4096
5974#define SKL_MIN_DST_W 8
5975#define SKL_MAX_DST_W 4096
5976#define SKL_MIN_DST_H 8
5977#define SKL_MAX_DST_H 4096
5978#define ICL_MAX_SRC_W 5120
5979#define ICL_MAX_SRC_H 4096
5980#define ICL_MAX_DST_W 5120
5981#define ICL_MAX_DST_H 4096
5982#define SKL_MIN_YUV_420_SRC_W 16
5983#define SKL_MIN_YUV_420_SRC_H 16
5984
86adf9d7
ML
5985static int
5986skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 5987 unsigned int scaler_user, int *scaler_id,
77224cd5 5988 int src_w, int src_h, int dst_w, int dst_h,
4941f35b
ID
5989 const struct drm_format_info *format,
5990 u64 modifier, bool need_scaler)
a1b2278e 5991{
86adf9d7
ML
5992 struct intel_crtc_scaler_state *scaler_state =
5993 &crtc_state->scaler_state;
5994 struct intel_crtc *intel_crtc =
2225f3c6 5995 to_intel_crtc(crtc_state->uapi.crtc);
7f58cbb1
MK
5996 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5997 const struct drm_display_mode *adjusted_mode =
1326a92c 5998 &crtc_state->hw.adjusted_mode;
6156a456 5999
d96a7d2a
VS
6000 /*
6001 * Src coordinates are already rotated by 270 degrees for
6002 * the 90/270 degree plane rotation cases (to match the
6003 * GTT mapping), hence no need to account for rotation here.
6004 */
b1554e23
ML
6005 if (src_w != dst_w || src_h != dst_h)
6006 need_scaler = true;
e5c05931 6007
7f58cbb1
MK
6008 /*
6009 * Scaling/fitting not supported in IF-ID mode in GEN9+
6010 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6011 * Once NV12 is enabled, handle it here while allocating scaler
6012 * for NV12.
6013 */
1326a92c 6014 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
b1554e23 6015 need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
cd49f818
WK
6016 drm_dbg_kms(&dev_priv->drm,
6017 "Pipe/Plane scaling not supported with IF-ID mode\n");
7f58cbb1
MK
6018 return -EINVAL;
6019 }
6020
a1b2278e
CK
6021 /*
6022 * if plane is being disabled or scaler is no more required or force detach
6023 * - free scaler binded to this plane/crtc
6024 * - in order to do this, update crtc->scaler_usage
6025 *
6026 * Here scaler state in crtc_state is set free so that
6027 * scaler can be assigned to other user. Actual register
6028 * update to free the scaler is done in plane/panel-fit programming.
6029 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6030 */
b1554e23 6031 if (force_detach || !need_scaler) {
a1b2278e 6032 if (*scaler_id >= 0) {
86adf9d7 6033 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
6034 scaler_state->scalers[*scaler_id].in_use = 0;
6035
cd49f818
WK
6036 drm_dbg_kms(&dev_priv->drm,
6037 "scaler_user index %u.%u: "
6038 "Staged freeing scaler id %d scaler_users = 0x%x\n",
6039 intel_crtc->pipe, scaler_user, *scaler_id,
6040 scaler_state->scaler_users);
a1b2278e
CK
6041 *scaler_id = -1;
6042 }
6043 return 0;
6044 }
6045
4941f35b 6046 if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
5d794288 6047 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
cd49f818
WK
6048 drm_dbg_kms(&dev_priv->drm,
6049 "Planar YUV: src dimensions not met\n");
77224cd5
CK
6050 return -EINVAL;
6051 }
6052
a1b2278e
CK
6053 /* range checks */
6054 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
323301af 6055 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
2dd24a9c 6056 (INTEL_GEN(dev_priv) >= 11 &&
323301af
NM
6057 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6058 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
2dd24a9c 6059 (INTEL_GEN(dev_priv) < 11 &&
323301af
NM
6060 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6061 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
cd49f818
WK
6062 drm_dbg_kms(&dev_priv->drm,
6063 "scaler_user index %u.%u: src %ux%u dst %ux%u "
6064 "size is out of scaler range\n",
6065 intel_crtc->pipe, scaler_user, src_w, src_h,
6066 dst_w, dst_h);
a1b2278e
CK
6067 return -EINVAL;
6068 }
6069
86adf9d7
ML
6070 /* mark this plane as a scaler user in crtc_state */
6071 scaler_state->scaler_users |= (1 << scaler_user);
cd49f818
WK
6072 drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6073 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6074 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6075 scaler_state->scaler_users);
86adf9d7
ML
6076
6077 return 0;
6078}
6079
c5a01ec7 6080static int skl_update_scaler_crtc(struct intel_crtc_state *crtc_state)
86adf9d7 6081{
bafcdad6 6082 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
c5a01ec7 6083 int width, height;
b1554e23 6084
c5a01ec7 6085 if (crtc_state->pch_pfit.enabled) {
35dd95b4
VS
6086 width = drm_rect_width(&crtc_state->pch_pfit.dst);
6087 height = drm_rect_height(&crtc_state->pch_pfit.dst);
c5a01ec7 6088 } else {
bafcdad6
ML
6089 width = pipe_mode->crtc_hdisplay;
6090 height = pipe_mode->crtc_vdisplay;
c5a01ec7 6091 }
c5a01ec7
VS
6092 return skl_update_scaler(crtc_state, !crtc_state->hw.active,
6093 SKL_CRTC_INDEX,
6094 &crtc_state->scaler_state.scaler_id,
6095 crtc_state->pipe_src_w, crtc_state->pipe_src_h,
6096 width, height, NULL, 0,
6097 crtc_state->pch_pfit.enabled);
86adf9d7
ML
6098}
6099
6100/**
6101 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
c38c1455 6102 * @crtc_state: crtc's scaler state
86adf9d7
ML
6103 * @plane_state: atomic plane state to update
6104 *
6105 * Return
6106 * 0 - scaler_usage updated successfully
6107 * error - requested scaling cannot be supported or other error condition
6108 */
da20eabd
ML
6109static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6110 struct intel_plane_state *plane_state)
86adf9d7 6111{
da20eabd 6112 struct intel_plane *intel_plane =
f90a85e7 6113 to_intel_plane(plane_state->uapi.plane);
42fd20ed 6114 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
7b3cb17a 6115 struct drm_framebuffer *fb = plane_state->hw.fb;
86adf9d7 6116 int ret;
f90a85e7 6117 bool force_detach = !fb || !plane_state->uapi.visible;
b1554e23
ML
6118 bool need_scaler = false;
6119
6120 /* Pre-gen11 and SDR planes always need a scaler for planar formats. */
42fd20ed 6121 if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
4941f35b 6122 fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
b1554e23 6123 need_scaler = true;
86adf9d7 6124
86adf9d7
ML
6125 ret = skl_update_scaler(crtc_state, force_detach,
6126 drm_plane_index(&intel_plane->base),
6127 &plane_state->scaler_id,
f90a85e7
ML
6128 drm_rect_width(&plane_state->uapi.src) >> 16,
6129 drm_rect_height(&plane_state->uapi.src) >> 16,
6130 drm_rect_width(&plane_state->uapi.dst),
6131 drm_rect_height(&plane_state->uapi.dst),
4941f35b
ID
6132 fb ? fb->format : NULL,
6133 fb ? fb->modifier : 0,
6134 need_scaler);
86adf9d7
ML
6135
6136 if (ret || plane_state->scaler_id < 0)
6137 return ret;
6138
a1b2278e 6139 /* check colorkey */
6ec5bd34 6140 if (plane_state->ckey.flags) {
cd49f818
WK
6141 drm_dbg_kms(&dev_priv->drm,
6142 "[PLANE:%d:%s] scaling with color key not allowed",
6143 intel_plane->base.base.id,
6144 intel_plane->base.name);
a1b2278e
CK
6145 return -EINVAL;
6146 }
6147
6148 /* Check src format */
438b74a5 6149 switch (fb->format->format) {
86adf9d7
ML
6150 case DRM_FORMAT_RGB565:
6151 case DRM_FORMAT_XBGR8888:
6152 case DRM_FORMAT_XRGB8888:
6153 case DRM_FORMAT_ABGR8888:
6154 case DRM_FORMAT_ARGB8888:
6155 case DRM_FORMAT_XRGB2101010:
6156 case DRM_FORMAT_XBGR2101010:
f9c43a31
VS
6157 case DRM_FORMAT_ARGB2101010:
6158 case DRM_FORMAT_ABGR2101010:
86adf9d7
ML
6159 case DRM_FORMAT_YUYV:
6160 case DRM_FORMAT_YVYU:
6161 case DRM_FORMAT_UYVY:
6162 case DRM_FORMAT_VYUY:
77224cd5 6163 case DRM_FORMAT_NV12:
da904174 6164 case DRM_FORMAT_XYUV8888:
df7d4156
JPH
6165 case DRM_FORMAT_P010:
6166 case DRM_FORMAT_P012:
6167 case DRM_FORMAT_P016:
296e9b19
SS
6168 case DRM_FORMAT_Y210:
6169 case DRM_FORMAT_Y212:
6170 case DRM_FORMAT_Y216:
ff01e697
ML
6171 case DRM_FORMAT_XVYU2101010:
6172 case DRM_FORMAT_XVYU12_16161616:
6173 case DRM_FORMAT_XVYU16161616:
86adf9d7 6174 break;
6e6c155d
VS
6175 case DRM_FORMAT_XBGR16161616F:
6176 case DRM_FORMAT_ABGR16161616F:
6177 case DRM_FORMAT_XRGB16161616F:
6178 case DRM_FORMAT_ARGB16161616F:
6179 if (INTEL_GEN(dev_priv) >= 11)
6180 break;
df561f66 6181 fallthrough;
86adf9d7 6182 default:
cd49f818
WK
6183 drm_dbg_kms(&dev_priv->drm,
6184 "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6185 intel_plane->base.base.id, intel_plane->base.name,
6186 fb->base.id, fb->format->format);
86adf9d7 6187 return -EINVAL;
a1b2278e
CK
6188 }
6189
a1b2278e
CK
6190 return 0;
6191}
6192
f6df4d46 6193void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
e435d6e5 6194{
cfb627c4 6195 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
e435d6e5
ML
6196 int i;
6197
6198 for (i = 0; i < crtc->num_scalers; i++)
6199 skl_detach_scaler(crtc, i);
6200}
6201
cc2396ff
PB
6202static int cnl_coef_tap(int i)
6203{
6204 return i % 7;
6205}
6206
6207static u16 cnl_nearest_filter_coef(int t)
6208{
6209 return t == 3 ? 0x0800 : 0x3000;
6210}
6211
176fd228 6212/*
cc2396ff
PB
6213 * Theory behind setting nearest-neighbor integer scaling:
6214 *
6215 * 17 phase of 7 taps requires 119 coefficients in 60 dwords per set.
6216 * The letter represents the filter tap (D is the center tap) and the number
6217 * represents the coefficient set for a phase (0-16).
6218 *
6219 * +------------+------------------------+------------------------+
6220 * |Index value | Data value coeffient 1 | Data value coeffient 2 |
6221 * +------------+------------------------+------------------------+
6222 * | 00h | B0 | A0 |
6223 * +------------+------------------------+------------------------+
6224 * | 01h | D0 | C0 |
6225 * +------------+------------------------+------------------------+
6226 * | 02h | F0 | E0 |
6227 * +------------+------------------------+------------------------+
6228 * | 03h | A1 | G0 |
6229 * +------------+------------------------+------------------------+
6230 * | 04h | C1 | B1 |
6231 * +------------+------------------------+------------------------+
6232 * | ... | ... | ... |
6233 * +------------+------------------------+------------------------+
6234 * | 38h | B16 | A16 |
6235 * +------------+------------------------+------------------------+
6236 * | 39h | D16 | C16 |
6237 * +------------+------------------------+------------------------+
6238 * | 3Ah | F16 | C16 |
6239 * +------------+------------------------+------------------------+
6240 * | 3Bh | Reserved | G16 |
6241 * +------------+------------------------+------------------------+
6242 *
6243 * To enable nearest-neighbor scaling: program scaler coefficents with
6244 * the center tap (Dxx) values set to 1 and all other values set to 0 as per
6245 * SCALER_COEFFICIENT_FORMAT
6246 *
6247 */
6248
6249static void cnl_program_nearest_filter_coefs(struct drm_i915_private *dev_priv,
6250 enum pipe pipe, int id, int set)
6251{
6252 int i;
6253
6254 intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set),
6255 PS_COEE_INDEX_AUTO_INC);
6256
6257 for (i = 0; i < 17 * 7; i += 2) {
6258 u32 tmp;
6259 int t;
6260
6261 t = cnl_coef_tap(i);
6262 tmp = cnl_nearest_filter_coef(t);
6263
6264 t = cnl_coef_tap(i + 1);
6265 tmp |= cnl_nearest_filter_coef(t) << 16;
6266
6267 intel_de_write_fw(dev_priv, CNL_PS_COEF_DATA_SET(pipe, id, set),
6268 tmp);
6269 }
6270
6271 intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set), 0);
6272}
6273
6274inline u32 skl_scaler_get_filter_select(enum drm_scaling_filter filter, int set)
6275{
6276 if (filter == DRM_SCALING_FILTER_NEAREST_NEIGHBOR) {
6277 return (PS_FILTER_PROGRAMMED |
6278 PS_Y_VERT_FILTER_SELECT(set) |
6279 PS_Y_HORZ_FILTER_SELECT(set) |
6280 PS_UV_VERT_FILTER_SELECT(set) |
6281 PS_UV_HORZ_FILTER_SELECT(set));
6282 }
6283
6284 return PS_FILTER_MEDIUM;
6285}
6286
6287void skl_scaler_setup_filter(struct drm_i915_private *dev_priv, enum pipe pipe,
6288 int id, int set, enum drm_scaling_filter filter)
6289{
6290 switch (filter) {
6291 case DRM_SCALING_FILTER_DEFAULT:
6292 break;
6293 case DRM_SCALING_FILTER_NEAREST_NEIGHBOR:
6294 cnl_program_nearest_filter_coefs(dev_priv, pipe, id, set);
6295 break;
6296 default:
6297 MISSING_CASE(filter);
6298 }
6299}
6300
f6df4d46 6301static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
bd2e244f 6302{
2225f3c6 6303 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 6304 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b2562712
ML
6305 const struct intel_crtc_scaler_state *scaler_state =
6306 &crtc_state->scaler_state;
35dd95b4
VS
6307 struct drm_rect src = {
6308 .x2 = crtc_state->pipe_src_w << 16,
6309 .y2 = crtc_state->pipe_src_h << 16,
6310 };
6311 const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
eac9c585 6312 u16 uv_rgb_hphase, uv_rgb_vphase;
35dd95b4
VS
6313 enum pipe pipe = crtc->pipe;
6314 int width = drm_rect_width(dst);
6315 int height = drm_rect_height(dst);
6316 int x = dst->x1;
6317 int y = dst->y1;
6318 int hscale, vscale;
eac9c585
VS
6319 unsigned long irqflags;
6320 int id;
6d1a2fde 6321 u32 ps_ctrl;
a1b2278e 6322
eac9c585
VS
6323 if (!crtc_state->pch_pfit.enabled)
6324 return;
a1b2278e 6325
eac9c585
VS
6326 if (drm_WARN_ON(&dev_priv->drm,
6327 crtc_state->scaler_state.scaler_id < 0))
6328 return;
a1b2278e 6329
35dd95b4
VS
6330 hscale = drm_rect_calc_hscale(&src, dst, 0, INT_MAX);
6331 vscale = drm_rect_calc_vscale(&src, dst, 0, INT_MAX);
e7a278a3 6332
eac9c585
VS
6333 uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6334 uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
0a59952b 6335
eac9c585 6336 id = scaler_state->scaler_id;
f986ef2e 6337
6d1a2fde
PB
6338 ps_ctrl = skl_scaler_get_filter_select(crtc_state->hw.scaling_filter, 0);
6339 ps_ctrl |= PS_SCALER_EN | scaler_state->scalers[id].mode;
6340
eac9c585 6341 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f986ef2e 6342
6d1a2fde
PB
6343 skl_scaler_setup_filter(dev_priv, pipe, id, 0,
6344 crtc_state->hw.scaling_filter);
6345
6346 intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), ps_ctrl);
6347
eac9c585
VS
6348 intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6349 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6350 intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6351 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6352 intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
35dd95b4 6353 x << 16 | y);
eac9c585 6354 intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
35dd95b4 6355 width << 16 | height);
f986ef2e 6356
eac9c585 6357 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
bd2e244f
JB
6358}
6359
9eae5e27 6360static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
b074cec8 6361{
2225f3c6 6362 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 6363 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
35dd95b4 6364 const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
d048a268 6365 enum pipe pipe = crtc->pipe;
35dd95b4
VS
6366 int width = drm_rect_width(dst);
6367 int height = drm_rect_height(dst);
6368 int x = dst->x1;
6369 int y = dst->y1;
b074cec8 6370
eac9c585
VS
6371 if (!crtc_state->pch_pfit.enabled)
6372 return;
6373
6374 /* Force use of hard-coded filter coefficients
6375 * as some pre-programmed values are broken,
6376 * e.g. x201.
6377 */
6378 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6379 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6380 PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6381 else
6382 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6383 PF_FILTER_MED_3x3);
35dd95b4
VS
6384 intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
6385 intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
d4270e57
JB
6386}
6387
199ea381 6388void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 6389{
2225f3c6 6390 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
cea165c3 6391 struct drm_device *dev = crtc->base.dev;
fac5e23e 6392 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 6393
24f28450 6394 if (!crtc_state->ips_enabled)
d77e4531
PZ
6395 return;
6396
307e4498
ML
6397 /*
6398 * We can only enable IPS after we enable a plane and wait for a vblank
6399 * This function is called from post_plane_update, which is run after
6400 * a vblank wait.
6401 */
e57291c2 6402 drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
51f5a096 6403
8652744b 6404 if (IS_BROADWELL(dev_priv)) {
e57291c2
PB
6405 drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6406 IPS_ENABLE | IPS_PCODE_CONTROL));
2a114cc1
BW
6407 /* Quoting Art Runyan: "its not safe to expect any particular
6408 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
6409 * mailbox." Moreover, the mailbox may return a bogus state,
6410 * so we need to just enable it and continue on.
2a114cc1
BW
6411 */
6412 } else {
dc008bf0 6413 intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
2a114cc1
BW
6414 /* The bit only becomes 1 in the next vblank, so this wait here
6415 * is essentially intel_wait_for_vblank. If we don't have this
6416 * and don't wait for vblanks until the end of crtc_enable, then
6417 * the HW state readout code will complain that the expected
6418 * IPS_CTL value is not the one we read. */
4cb3b44d 6419 if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
cd49f818
WK
6420 drm_err(&dev_priv->drm,
6421 "Timed out waiting for IPS enable\n");
2a114cc1 6422 }
d77e4531
PZ
6423}
6424
199ea381 6425void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 6426{
2225f3c6 6427 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
d77e4531 6428 struct drm_device *dev = crtc->base.dev;
fac5e23e 6429 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 6430
199ea381 6431 if (!crtc_state->ips_enabled)
d77e4531
PZ
6432 return;
6433
8652744b 6434 if (IS_BROADWELL(dev_priv)) {
e57291c2
PB
6435 drm_WARN_ON(dev,
6436 sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
acb3ef0e
ID
6437 /*
6438 * Wait for PCODE to finish disabling IPS. The BSpec specified
6439 * 42ms timeout value leads to occasional timeouts so use 100ms
6440 * instead.
6441 */
4cb3b44d 6442 if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
cd49f818
WK
6443 drm_err(&dev_priv->drm,
6444 "Timed out waiting for IPS disable\n");
e59150dc 6445 } else {
dc008bf0
JN
6446 intel_de_write(dev_priv, IPS_CTL, 0);
6447 intel_de_posting_read(dev_priv, IPS_CTL);
e59150dc 6448 }
d77e4531
PZ
6449
6450 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 6451 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
6452}
6453
7cac945f 6454static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 6455{
cb5eb072 6456 if (intel_crtc->overlay)
d3eedb1a 6457 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
6458
6459 /* Let userspace switch the overlay on again. In most cases userspace
6460 * has to recompute where to put it anyway.
6461 */
6462}
6463
24f28450
ML
6464static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6465 const struct intel_crtc_state *new_crtc_state)
6466{
2225f3c6 6467 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
051a6d8d
VS
6468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6469
24f28450
ML
6470 if (!old_crtc_state->ips_enabled)
6471 return false;
6472
69f786ae 6473 if (needs_modeset(new_crtc_state))
24f28450
ML
6474 return true;
6475
051a6d8d
VS
6476 /*
6477 * Workaround : Do not read or write the pipe palette/gamma data while
6478 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6479 *
6480 * Disable IPS before we program the LUT.
6481 */
6482 if (IS_HASWELL(dev_priv) &&
2225f3c6 6483 (new_crtc_state->uapi.color_mgmt_changed ||
051a6d8d
VS
6484 new_crtc_state->update_pipe) &&
6485 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6486 return true;
6487
24f28450
ML
6488 return !new_crtc_state->ips_enabled;
6489}
6490
6491static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6492 const struct intel_crtc_state *new_crtc_state)
6493{
2225f3c6 6494 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
051a6d8d
VS
6495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6496
24f28450
ML
6497 if (!new_crtc_state->ips_enabled)
6498 return false;
6499
69f786ae 6500 if (needs_modeset(new_crtc_state))
24f28450
ML
6501 return true;
6502
051a6d8d
VS
6503 /*
6504 * Workaround : Do not read or write the pipe palette/gamma data while
6505 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6506 *
6507 * Re-enable IPS after the LUT has been programmed.
6508 */
6509 if (IS_HASWELL(dev_priv) &&
2225f3c6 6510 (new_crtc_state->uapi.color_mgmt_changed ||
051a6d8d
VS
6511 new_crtc_state->update_pipe) &&
6512 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6513 return true;
6514
24f28450
ML
6515 /*
6516 * We can't read out IPS on broadwell, assume the worst and
6517 * forcibly enable IPS on the first fastset.
6518 */
a227569d 6519 if (new_crtc_state->update_pipe && old_crtc_state->inherited)
24f28450
ML
6520 return true;
6521
6522 return !old_crtc_state->ips_enabled;
6523}
6524
d2432796 6525static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
8e021151 6526{
d2432796
VS
6527 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6528
8e021151
ML
6529 if (!crtc_state->nv12_planes)
6530 return false;
6531
1347d3ce 6532 /* WA Display #0827: Gen9:all */
cf819eff 6533 if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
8e021151
ML
6534 return true;
6535
6536 return false;
6537}
6538
d2432796 6539static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
51eb1a1d 6540{
d2432796
VS
6541 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6542
f96198ab
MA
6543 /* Wa_2006604312:icl,ehl */
6544 if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
51eb1a1d
RS
6545 return true;
6546
6547 return false;
6548}
6549
7181f5c5
VS
6550static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6551 const struct intel_crtc_state *new_crtc_state)
6552{
6553 return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6554 new_crtc_state->active_planes;
6555}
6556
6557static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6558 const struct intel_crtc_state *new_crtc_state)
6559{
6560 return old_crtc_state->active_planes &&
6561 (!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6562}
6563
bee43ca4
VS
6564static void intel_post_plane_update(struct intel_atomic_state *state,
6565 struct intel_crtc *crtc)
5a21b665 6566{
bee43ca4 6567 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
bee43ca4
VS
6568 const struct intel_crtc_state *old_crtc_state =
6569 intel_atomic_get_old_crtc_state(state, crtc);
6570 const struct intel_crtc_state *new_crtc_state =
6571 intel_atomic_get_new_crtc_state(state, crtc);
7181f5c5 6572 enum pipe pipe = crtc->pipe;
5a21b665 6573
bee43ca4 6574 intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
5a21b665 6575
0e75fb8c 6576 if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
432081bc 6577 intel_update_watermarks(crtc);
5a21b665 6578
0e75fb8c
VS
6579 if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6580 hsw_enable_ips(new_crtc_state);
24f28450 6581
9ecc6eab 6582 intel_fbc_post_update(state, crtc);
5a21b665 6583
d2432796 6584 if (needs_nv12_wa(old_crtc_state) &&
0e75fb8c 6585 !needs_nv12_wa(new_crtc_state))
7181f5c5 6586 skl_wa_827(dev_priv, pipe, false);
51eb1a1d 6587
d2432796 6588 if (needs_scalerclk_wa(old_crtc_state) &&
0e75fb8c 6589 !needs_scalerclk_wa(new_crtc_state))
7181f5c5 6590 icl_wa_scalerclkgating(dev_priv, pipe, false);
5a21b665
DV
6591}
6592
e5cb1afb
K
6593static void skl_disable_async_flip_wa(struct intel_atomic_state *state,
6594 struct intel_crtc *crtc,
6595 const struct intel_crtc_state *new_crtc_state)
6596{
6597 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6598 struct intel_plane *plane;
6599 struct intel_plane_state *new_plane_state;
6600 int i;
6601
6602 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
6603 u32 update_mask = new_crtc_state->update_planes;
6604 u32 plane_ctl, surf_addr;
6605 enum plane_id plane_id;
6606 unsigned long irqflags;
6607 enum pipe pipe;
6608
6609 if (crtc->pipe != plane->pipe ||
6610 !(update_mask & BIT(plane->id)))
6611 continue;
6612
6613 plane_id = plane->id;
6614 pipe = plane->pipe;
6615
6616 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6617 plane_ctl = intel_de_read_fw(dev_priv, PLANE_CTL(pipe, plane_id));
6618 surf_addr = intel_de_read_fw(dev_priv, PLANE_SURF(pipe, plane_id));
6619
6620 plane_ctl &= ~PLANE_CTL_ASYNC_FLIP;
6621
6622 intel_de_write_fw(dev_priv, PLANE_CTL(pipe, plane_id), plane_ctl);
6623 intel_de_write_fw(dev_priv, PLANE_SURF(pipe, plane_id), surf_addr);
6624 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6625 }
6626
6627 intel_wait_for_vblank(dev_priv, crtc->pipe);
6628}
6629
bee43ca4
VS
6630static void intel_pre_plane_update(struct intel_atomic_state *state,
6631 struct intel_crtc *crtc)
ac21b225 6632{
bee43ca4 6633 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
bee43ca4
VS
6634 const struct intel_crtc_state *old_crtc_state =
6635 intel_atomic_get_old_crtc_state(state, crtc);
6636 const struct intel_crtc_state *new_crtc_state =
6637 intel_atomic_get_new_crtc_state(state, crtc);
7181f5c5 6638 enum pipe pipe = crtc->pipe;
ac21b225 6639
0e75fb8c 6640 if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
24f28450
ML
6641 hsw_disable_ips(old_crtc_state);
6642
9ecc6eab 6643 if (intel_fbc_pre_update(state, crtc))
07fd0df8
VS
6644 intel_wait_for_vblank(dev_priv, pipe);
6645
8e021151 6646 /* Display WA 827 */
d2432796 6647 if (!needs_nv12_wa(old_crtc_state) &&
0e75fb8c 6648 needs_nv12_wa(new_crtc_state))
7181f5c5 6649 skl_wa_827(dev_priv, pipe, true);
51eb1a1d 6650
f96198ab 6651 /* Wa_2006604312:icl,ehl */
d2432796 6652 if (!needs_scalerclk_wa(old_crtc_state) &&
0e75fb8c 6653 needs_scalerclk_wa(new_crtc_state))
7181f5c5 6654 icl_wa_scalerclkgating(dev_priv, pipe, true);
8e021151 6655
5eeb798b
VS
6656 /*
6657 * Vblank time updates from the shadow to live plane control register
6658 * are blocked if the memory self-refresh mode is active at that
6659 * moment. So to make sure the plane gets truly disabled, disable
6660 * first the self-refresh mode. The self-refresh enable bit in turn
6661 * will be checked/applied by the HW only at the next frame start
6662 * event which is after the vblank start event, so we need to have a
6663 * wait-for-vblank between disabling the plane and the pipe.
6664 */
1326a92c 6665 if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
0e75fb8c 6666 new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
7181f5c5 6667 intel_wait_for_vblank(dev_priv, pipe);
92826fcd 6668
ed4a6a7c
MR
6669 /*
6670 * IVB workaround: must disable low power watermarks for at least
6671 * one frame before enabling scaling. LP watermarks can be re-enabled
6672 * when scaling is disabled.
6673 *
6674 * WaCxSRDisabledForSpriteScaling:ivb
6675 */
0e75fb8c
VS
6676 if (old_crtc_state->hw.active &&
6677 new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
7181f5c5 6678 intel_wait_for_vblank(dev_priv, pipe);
ed4a6a7c
MR
6679
6680 /*
7181f5c5
VS
6681 * If we're doing a modeset we don't need to do any
6682 * pre-vblank watermark programming here.
ed4a6a7c 6683 */
7181f5c5
VS
6684 if (!needs_modeset(new_crtc_state)) {
6685 /*
6686 * For platforms that support atomic watermarks, program the
6687 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
6688 * will be the intermediate values that are safe for both pre- and
6689 * post- vblank; when vblank happens, the 'active' values will be set
6690 * to the final 'target' values and we'll do this again to get the
6691 * optimal watermarks. For gen9+ platforms, the values we program here
6692 * will be the final target values which will get automatically latched
6693 * at vblank time; no further programming will be necessary.
6694 *
6695 * If a platform hasn't been transitioned to atomic watermarks yet,
6696 * we'll continue to update watermarks the old way, if flags tell
6697 * us to.
6698 */
6699 if (dev_priv->display.initial_watermarks)
6700 dev_priv->display.initial_watermarks(state, crtc);
6701 else if (new_crtc_state->update_wm_pre)
6702 intel_update_watermarks(crtc);
6703 }
ed4a6a7c
MR
6704
6705 /*
7181f5c5
VS
6706 * Gen2 reports pipe underruns whenever all planes are disabled.
6707 * So disable underrun reporting before all the planes get disabled.
ed4a6a7c 6708 *
7181f5c5
VS
6709 * We do this after .initial_watermarks() so that we have a
6710 * chance of catching underruns with the intermediate watermarks
6711 * vs. the old plane configuration.
ed4a6a7c 6712 */
7181f5c5
VS
6713 if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6714 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
e5cb1afb
K
6715
6716 /*
6717 * WA for platforms where async address update enable bit
6718 * is double buffered and only latched at start of vblank.
6719 */
6720 if (old_crtc_state->uapi.async_flip &&
6721 !new_crtc_state->uapi.async_flip &&
6722 IS_GEN_RANGE(dev_priv, 9, 10))
6723 skl_disable_async_flip_wa(state, crtc, new_crtc_state);
ac21b225
ML
6724}
6725
0dd14be3
VS
6726static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6727 struct intel_crtc *crtc)
87d4300a 6728{
0dd14be3
VS
6729 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6730 const struct intel_crtc_state *new_crtc_state =
6731 intel_atomic_get_new_crtc_state(state, crtc);
6732 unsigned int update_mask = new_crtc_state->update_planes;
6733 const struct intel_plane_state *old_plane_state;
f59e9701
ML
6734 struct intel_plane *plane;
6735 unsigned fb_bits = 0;
0dd14be3 6736 int i;
87d4300a 6737
f59e9701 6738 intel_crtc_dpms_overlay_disable(crtc);
27321ae8 6739
0dd14be3
VS
6740 for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6741 if (crtc->pipe != plane->pipe ||
6742 !(update_mask & BIT(plane->id)))
6743 continue;
6744
c48b86f9 6745 intel_disable_plane(plane, new_crtc_state);
f98551ae 6746
f90a85e7 6747 if (old_plane_state->uapi.visible)
f59e9701 6748 fb_bits |= plane->frontbuffer_bit;
f59e9701
ML
6749 }
6750
0dd14be3 6751 intel_frontbuffer_flip(dev_priv, fb_bits);
a5c4d7bc
VS
6752}
6753
24a7bfe0
ID
6754/*
6755 * intel_connector_primary_encoder - get the primary encoder for a connector
6756 * @connector: connector for which to return the encoder
6757 *
6758 * Returns the primary encoder for a connector. There is a 1:1 mapping from
6759 * all connectors to their encoder, except for DP-MST connectors which have
6760 * both a virtual and a primary encoder. These DP-MST primary encoders can be
6761 * pointed to by as many DP-MST connectors as there are pipes.
6762 */
6763static struct intel_encoder *
6764intel_connector_primary_encoder(struct intel_connector *connector)
6765{
6766 struct intel_encoder *encoder;
6767
6768 if (connector->mst_port)
6769 return &dp_to_dig_port(connector->mst_port)->base;
6770
43a6d19c 6771 encoder = intel_attached_encoder(connector);
ce04ecd9 6772 drm_WARN_ON(connector->base.dev, !encoder);
24a7bfe0
ID
6773
6774 return encoder;
6775}
6776
24a7bfe0
ID
6777static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6778{
24a7bfe0 6779 struct drm_connector_state *new_conn_state;
ee36c7c0 6780 struct drm_connector *connector;
24a7bfe0
ID
6781 int i;
6782
ee36c7c0
JRS
6783 for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6784 i) {
6785 struct intel_connector *intel_connector;
24a7bfe0
ID
6786 struct intel_encoder *encoder;
6787 struct intel_crtc *crtc;
6788
ee36c7c0 6789 if (!intel_connector_needs_modeset(state, connector))
24a7bfe0
ID
6790 continue;
6791
ee36c7c0
JRS
6792 intel_connector = to_intel_connector(connector);
6793 encoder = intel_connector_primary_encoder(intel_connector);
24a7bfe0
ID
6794 if (!encoder->update_prepare)
6795 continue;
6796
6797 crtc = new_conn_state->crtc ?
6798 to_intel_crtc(new_conn_state->crtc) : NULL;
6799 encoder->update_prepare(state, encoder, crtc);
6800 }
6801}
6802
6803static void intel_encoders_update_complete(struct intel_atomic_state *state)
6804{
24a7bfe0 6805 struct drm_connector_state *new_conn_state;
ee36c7c0 6806 struct drm_connector *connector;
24a7bfe0
ID
6807 int i;
6808
ee36c7c0
JRS
6809 for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6810 i) {
6811 struct intel_connector *intel_connector;
24a7bfe0
ID
6812 struct intel_encoder *encoder;
6813 struct intel_crtc *crtc;
6814
ee36c7c0 6815 if (!intel_connector_needs_modeset(state, connector))
24a7bfe0
ID
6816 continue;
6817
ee36c7c0
JRS
6818 intel_connector = to_intel_connector(connector);
6819 encoder = intel_connector_primary_encoder(intel_connector);
24a7bfe0
ID
6820 if (!encoder->update_complete)
6821 continue;
6822
6823 crtc = new_conn_state->crtc ?
6824 to_intel_crtc(new_conn_state->crtc) : NULL;
6825 encoder->update_complete(state, encoder, crtc);
6826 }
6827}
6828
021ba100
VS
6829static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6830 struct intel_crtc *crtc)
fb1c98b1 6831{
021ba100
VS
6832 const struct intel_crtc_state *crtc_state =
6833 intel_atomic_get_new_crtc_state(state, crtc);
6834 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6835 struct drm_connector *conn;
6836 int i;
6837
855e0d68 6838 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6839 struct intel_encoder *encoder =
6840 to_intel_encoder(conn_state->best_encoder);
6841
855e0d68 6842 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6843 continue;
6844
6845 if (encoder->pre_pll_enable)
ede9771d
VS
6846 encoder->pre_pll_enable(state, encoder,
6847 crtc_state, conn_state);
fb1c98b1
ML
6848 }
6849}
6850
021ba100
VS
6851static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6852 struct intel_crtc *crtc)
fb1c98b1 6853{
021ba100
VS
6854 const struct intel_crtc_state *crtc_state =
6855 intel_atomic_get_new_crtc_state(state, crtc);
6856 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6857 struct drm_connector *conn;
6858 int i;
6859
855e0d68 6860 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6861 struct intel_encoder *encoder =
6862 to_intel_encoder(conn_state->best_encoder);
6863
855e0d68 6864 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6865 continue;
6866
6867 if (encoder->pre_enable)
ede9771d
VS
6868 encoder->pre_enable(state, encoder,
6869 crtc_state, conn_state);
fb1c98b1
ML
6870 }
6871}
6872
021ba100
VS
6873static void intel_encoders_enable(struct intel_atomic_state *state,
6874 struct intel_crtc *crtc)
fb1c98b1 6875{
021ba100
VS
6876 const struct intel_crtc_state *crtc_state =
6877 intel_atomic_get_new_crtc_state(state, crtc);
6878 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6879 struct drm_connector *conn;
6880 int i;
6881
855e0d68 6882 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6883 struct intel_encoder *encoder =
6884 to_intel_encoder(conn_state->best_encoder);
6885
855e0d68 6886 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6887 continue;
6888
c84c6fe3 6889 if (encoder->enable)
ede9771d
VS
6890 encoder->enable(state, encoder,
6891 crtc_state, conn_state);
fb1c98b1
ML
6892 intel_opregion_notify_encoder(encoder, true);
6893 }
6894}
6895
021ba100
VS
6896static void intel_encoders_disable(struct intel_atomic_state *state,
6897 struct intel_crtc *crtc)
fb1c98b1 6898{
021ba100
VS
6899 const struct intel_crtc_state *old_crtc_state =
6900 intel_atomic_get_old_crtc_state(state, crtc);
6901 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6902 struct drm_connector *conn;
6903 int i;
6904
855e0d68 6905 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6906 struct intel_encoder *encoder =
6907 to_intel_encoder(old_conn_state->best_encoder);
6908
855e0d68 6909 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6910 continue;
6911
6912 intel_opregion_notify_encoder(encoder, false);
c84c6fe3 6913 if (encoder->disable)
ede9771d
VS
6914 encoder->disable(state, encoder,
6915 old_crtc_state, old_conn_state);
fb1c98b1
ML
6916 }
6917}
6918
021ba100
VS
6919static void intel_encoders_post_disable(struct intel_atomic_state *state,
6920 struct intel_crtc *crtc)
fb1c98b1 6921{
021ba100
VS
6922 const struct intel_crtc_state *old_crtc_state =
6923 intel_atomic_get_old_crtc_state(state, crtc);
6924 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6925 struct drm_connector *conn;
6926 int i;
6927
855e0d68 6928 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6929 struct intel_encoder *encoder =
6930 to_intel_encoder(old_conn_state->best_encoder);
6931
855e0d68 6932 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6933 continue;
6934
6935 if (encoder->post_disable)
ede9771d
VS
6936 encoder->post_disable(state, encoder,
6937 old_crtc_state, old_conn_state);
fb1c98b1
ML
6938 }
6939}
6940
021ba100
VS
6941static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6942 struct intel_crtc *crtc)
fb1c98b1 6943{
021ba100
VS
6944 const struct intel_crtc_state *old_crtc_state =
6945 intel_atomic_get_old_crtc_state(state, crtc);
6946 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6947 struct drm_connector *conn;
6948 int i;
6949
855e0d68 6950 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6951 struct intel_encoder *encoder =
6952 to_intel_encoder(old_conn_state->best_encoder);
6953
855e0d68 6954 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6955 continue;
6956
6957 if (encoder->post_pll_disable)
ede9771d
VS
6958 encoder->post_pll_disable(state, encoder,
6959 old_crtc_state, old_conn_state);
fb1c98b1
ML
6960 }
6961}
6962
021ba100
VS
6963static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6964 struct intel_crtc *crtc)
608ed4ab 6965{
021ba100
VS
6966 const struct intel_crtc_state *crtc_state =
6967 intel_atomic_get_new_crtc_state(state, crtc);
6968 const struct drm_connector_state *conn_state;
608ed4ab
HG
6969 struct drm_connector *conn;
6970 int i;
6971
855e0d68 6972 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
608ed4ab
HG
6973 struct intel_encoder *encoder =
6974 to_intel_encoder(conn_state->best_encoder);
6975
855e0d68 6976 if (conn_state->crtc != &crtc->base)
608ed4ab
HG
6977 continue;
6978
6979 if (encoder->update_pipe)
ede9771d
VS
6980 encoder->update_pipe(state, encoder,
6981 crtc_state, conn_state);
608ed4ab
HG
6982 }
6983}
6984
73a116be
VS
6985static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6986{
2225f3c6 6987 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
73a116be
VS
6988 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6989
6990 plane->disable_plane(plane, crtc_state);
6991}
6992
9eae5e27
LDM
6993static void ilk_crtc_enable(struct intel_atomic_state *state,
6994 struct intel_crtc *crtc)
f67a559d 6995{
7451a074
VS
6996 const struct intel_crtc_state *new_crtc_state =
6997 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
6998 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6999 enum pipe pipe = crtc->pipe;
f67a559d 7000
e57291c2 7001 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
f67a559d
JB
7002 return;
7003
b2c0593a
VS
7004 /*
7005 * Sometimes spurious CPU pipe underruns happen during FDI
7006 * training, at least with VGA+HDMI cloning. Suppress them.
7007 *
7008 * On ILK we get an occasional spurious CPU pipe underruns
7009 * between eDP port A enable and vdd enable. Also PCH port
7010 * enable seems to result in the occasional CPU pipe underrun.
7011 *
7012 * Spurious PCH underruns also occur during PCH enabling.
7013 */
2b5b6312
VS
7014 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7015 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca 7016
502d8714
VS
7017 if (new_crtc_state->has_pch_encoder)
7018 intel_prepare_shared_dpll(new_crtc_state);
b14b1055 7019
502d8714
VS
7020 if (intel_crtc_has_dp_encoder(new_crtc_state))
7021 intel_dp_set_m_n(new_crtc_state, M1_N1);
29407aab 7022
e7fc3f90 7023 intel_set_transcoder_timings(new_crtc_state);
502d8714 7024 intel_set_pipe_src_size(new_crtc_state);
29407aab 7025
502d8714
VS
7026 if (new_crtc_state->has_pch_encoder)
7027 intel_cpu_transcoder_set_m_n(new_crtc_state,
7028 &new_crtc_state->fdi_m_n, NULL);
29407aab 7029
9eae5e27 7030 ilk_set_pipeconf(new_crtc_state);
29407aab 7031
e44c84a1 7032 crtc->active = true;
8664281b 7033
e44c84a1 7034 intel_encoders_pre_enable(state, crtc);
f67a559d 7035
502d8714 7036 if (new_crtc_state->has_pch_encoder) {
fff367c7
DV
7037 /* Note: FDI PLL enabling _must_ be done before we enable the
7038 * cpu pipes, hence this is separate from all the other fdi/pch
7039 * enabling. */
9eae5e27 7040 ilk_fdi_pll_enable(new_crtc_state);
46b6f814
DV
7041 } else {
7042 assert_fdi_tx_disabled(dev_priv, pipe);
7043 assert_fdi_rx_disabled(dev_priv, pipe);
7044 }
f67a559d 7045
9eae5e27 7046 ilk_pfit_enable(new_crtc_state);
f67a559d 7047
9c54c0dd
JB
7048 /*
7049 * On ILK+ LUT must be loaded before the pipe is running but with
7050 * clocks enabled
7051 */
502d8714
VS
7052 intel_color_load_luts(new_crtc_state);
7053 intel_color_commit(new_crtc_state);
73a116be 7054 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7055 intel_disable_primary_plane(new_crtc_state);
9c54c0dd 7056
7a8fdb1f 7057 if (dev_priv->display.initial_watermarks)
e44c84a1 7058 dev_priv->display.initial_watermarks(state, crtc);
502d8714 7059 intel_enable_pipe(new_crtc_state);
f67a559d 7060
502d8714 7061 if (new_crtc_state->has_pch_encoder)
9eae5e27 7062 ilk_pch_enable(state, new_crtc_state);
c98e9dcf 7063
502d8714 7064 intel_crtc_vblank_on(new_crtc_state);
f9b61ff6 7065
e44c84a1 7066 intel_encoders_enable(state, crtc);
61b77ddd 7067
6e266956 7068 if (HAS_PCH_CPT(dev_priv))
c684fb44 7069 cpt_verify_modeset(dev_priv, pipe);
37ca8d4c 7070
ea80a661
VS
7071 /*
7072 * Must wait for vblank to avoid spurious PCH FIFO underruns.
7073 * And a second vblank wait is needed at least on ILK with
7074 * some interlaced HDMI modes. Let's do the double wait always
7075 * in case there are more corner cases we don't know about.
7076 */
502d8714 7077 if (new_crtc_state->has_pch_encoder) {
ea80a661 7078 intel_wait_for_vblank(dev_priv, pipe);
0f0f74bc 7079 intel_wait_for_vblank(dev_priv, pipe);
ea80a661 7080 }
b2c0593a 7081 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 7082 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
7083}
7084
42db64ef
PZ
7085/* IPS only exists on ULT machines and is tied to pipe A. */
7086static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
7087{
50a0bc90 7088 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
7089}
7090
ed69cd40
ID
7091static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
7092 enum pipe pipe, bool apply)
7093{
dc008bf0 7094 u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
ed69cd40
ID
7095 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
7096
7097 if (apply)
7098 val |= mask;
7099 else
7100 val &= ~mask;
7101
dc008bf0 7102 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
ed69cd40
ID
7103}
7104
c3cc39c5
MK
7105static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
7106{
7107 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7108 enum pipe pipe = crtc->pipe;
ba3f4d0a 7109 u32 val;
c3cc39c5 7110
443d5e39 7111 val = MBUS_DBOX_A_CREDIT(2);
30fcc338
RV
7112
7113 if (INTEL_GEN(dev_priv) >= 12) {
7114 val |= MBUS_DBOX_BW_CREDIT(2);
7115 val |= MBUS_DBOX_B_CREDIT(12);
7116 } else {
7117 val |= MBUS_DBOX_BW_CREDIT(1);
7118 val |= MBUS_DBOX_B_CREDIT(8);
7119 }
c3cc39c5 7120
dc008bf0 7121 intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
c3cc39c5
MK
7122}
7123
6dcde047
VS
7124static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
7125{
7126 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7127 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7128
7129 intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7130 HSW_LINETIME(crtc_state->linetime) |
7131 HSW_IPS_LINETIME(crtc_state->ips_linetime));
7132}
7133
cc7a4cff
VS
7134static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7135{
7136 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7137 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7138 i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7139 u32 val;
7140
dc008bf0 7141 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
7142 val &= ~HSW_FRAME_START_DELAY_MASK;
7143 val |= HSW_FRAME_START_DELAY(0);
dc008bf0 7144 intel_de_write(dev_priv, reg, val);
cc7a4cff
VS
7145}
7146
4e3cdb45
MN
7147static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
7148 const struct intel_crtc_state *crtc_state)
7149{
7150 struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
7151 struct intel_crtc_state *master_crtc_state;
7152 struct drm_connector_state *conn_state;
7153 struct drm_connector *conn;
7154 struct intel_encoder *encoder = NULL;
7155 int i;
7156
7157 if (crtc_state->bigjoiner_slave)
7158 master = crtc_state->bigjoiner_linked_crtc;
7159
7160 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
7161
7162 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
7163 if (conn_state->crtc != &master->base)
7164 continue;
7165
7166 encoder = to_intel_encoder(conn_state->best_encoder);
7167 break;
7168 }
7169
7170 if (!crtc_state->bigjoiner_slave) {
7171 /* need to enable VDSC, which we skipped in pre-enable */
7172 intel_dsc_enable(encoder, crtc_state);
7173 } else {
7174 /*
7175 * Enable sequence steps 1-7 on bigjoiner master
7176 */
7177 intel_encoders_pre_pll_enable(state, master);
7178 intel_enable_shared_dpll(master_crtc_state);
7179 intel_encoders_pre_enable(state, master);
7180
7181 /* and DSC on slave */
7182 intel_dsc_enable(NULL, crtc_state);
7183 }
7184}
7185
1e98f88c
LDM
7186static void hsw_crtc_enable(struct intel_atomic_state *state,
7187 struct intel_crtc *crtc)
4f771f10 7188{
7451a074
VS
7189 const struct intel_crtc_state *new_crtc_state =
7190 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7192 enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
502d8714 7193 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
ed69cd40 7194 bool psl_clkgate_wa;
4f771f10 7195
e57291c2 7196 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4f771f10
PZ
7197 return;
7198
4e3cdb45
MN
7199 if (!new_crtc_state->bigjoiner) {
7200 intel_encoders_pre_pll_enable(state, crtc);
df8ad70c 7201
4e3cdb45
MN
7202 if (new_crtc_state->shared_dpll)
7203 intel_enable_shared_dpll(new_crtc_state);
c8af5274 7204
4e3cdb45
MN
7205 intel_encoders_pre_enable(state, crtc);
7206 } else {
7207 icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
7208 }
4d1de975 7209
502d8714 7210 intel_set_pipe_src_size(new_crtc_state);
4e3cdb45
MN
7211 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7212 bdw_set_pipemisc(new_crtc_state);
229fca97 7213
4e3cdb45
MN
7214 if (!new_crtc_state->bigjoiner_slave || !transcoder_is_dsi(cpu_transcoder)) {
7215 if (!transcoder_is_dsi(cpu_transcoder))
7216 intel_set_transcoder_timings(new_crtc_state);
ebb69c95 7217
4e3cdb45
MN
7218 if (cpu_transcoder != TRANSCODER_EDP &&
7219 !transcoder_is_dsi(cpu_transcoder))
7220 intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7221 new_crtc_state->pixel_multiplier - 1);
7222
7223 if (new_crtc_state->has_pch_encoder)
7224 intel_cpu_transcoder_set_m_n(new_crtc_state,
7225 &new_crtc_state->fdi_m_n, NULL);
229fca97 7226
502d8714 7227 hsw_set_frame_start_delay(new_crtc_state);
cc7a4cff 7228 }
4d1de975 7229
4e3cdb45
MN
7230 if (!transcoder_is_dsi(cpu_transcoder))
7231 hsw_set_pipeconf(new_crtc_state);
229fca97 7232
e44c84a1 7233 crtc->active = true;
8664281b 7234
ed69cd40
ID
7235 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7236 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
502d8714 7237 new_crtc_state->pch_pfit.enabled;
ed69cd40
ID
7238 if (psl_clkgate_wa)
7239 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7240
6315b5d3 7241 if (INTEL_GEN(dev_priv) >= 9)
f6df4d46 7242 skl_pfit_enable(new_crtc_state);
ff6d9f55 7243 else
9eae5e27 7244 ilk_pfit_enable(new_crtc_state);
4f771f10
PZ
7245
7246 /*
7247 * On ILK+ LUT must be loaded before the pipe is running but with
7248 * clocks enabled
7249 */
502d8714
VS
7250 intel_color_load_luts(new_crtc_state);
7251 intel_color_commit(new_crtc_state);
73a116be
VS
7252 /* update DSPCNTR to configure gamma/csc for pipe bottom color */
7253 if (INTEL_GEN(dev_priv) < 9)
502d8714 7254 intel_disable_primary_plane(new_crtc_state);
4f771f10 7255
6dcde047
VS
7256 hsw_set_linetime_wm(new_crtc_state);
7257
d1622119 7258 if (INTEL_GEN(dev_priv) >= 11)
e44c84a1 7259 icl_set_pipe_chicken(crtc);
e16a3750 7260
7a8fdb1f 7261 if (dev_priv->display.initial_watermarks)
e44c84a1 7262 dev_priv->display.initial_watermarks(state, crtc);
4d1de975 7263
c3cc39c5 7264 if (INTEL_GEN(dev_priv) >= 11)
e44c84a1 7265 icl_pipe_mbus_enable(crtc);
c3cc39c5 7266
4e3cdb45
MN
7267 if (new_crtc_state->bigjoiner_slave) {
7268 trace_intel_pipe_enable(crtc);
7269 intel_crtc_vblank_on(new_crtc_state);
7270 }
7271
e44c84a1 7272 intel_encoders_enable(state, crtc);
4f771f10 7273
ed69cd40
ID
7274 if (psl_clkgate_wa) {
7275 intel_wait_for_vblank(dev_priv, pipe);
7276 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7277 }
7278
e4916946
PZ
7279 /* If we change the relative order between pipe/planes enabling, we need
7280 * to change the workaround. */
502d8714 7281 hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
772c2a51 7282 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
7283 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7284 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 7285 }
4f771f10
PZ
7286}
7287
9eae5e27 7288void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
3f8dce3a 7289{
2225f3c6 7290 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
b2562712
ML
7291 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7292 enum pipe pipe = crtc->pipe;
3f8dce3a
DV
7293
7294 /* To avoid upsetting the power well on haswell only disable the pfit if
7295 * it's in use. The hw state code will make sure we get this right. */
eac9c585
VS
7296 if (!old_crtc_state->pch_pfit.enabled)
7297 return;
7298
7299 intel_de_write(dev_priv, PF_CTL(pipe), 0);
7300 intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7301 intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
3f8dce3a
DV
7302}
7303
9eae5e27
LDM
7304static void ilk_crtc_disable(struct intel_atomic_state *state,
7305 struct intel_crtc *crtc)
6be4a607 7306{
7451a074
VS
7307 const struct intel_crtc_state *old_crtc_state =
7308 intel_atomic_get_old_crtc_state(state, crtc);
e44c84a1
VS
7309 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7310 enum pipe pipe = crtc->pipe;
b52eb4dc 7311
b2c0593a
VS
7312 /*
7313 * Sometimes spurious CPU pipe underruns happen when the
7314 * pipe is already disabled, but FDI RX/TX is still enabled.
7315 * Happens at least with VGA+HDMI cloning. Suppress them.
7316 */
2b5b6312
VS
7317 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7318 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 7319
e44c84a1 7320 intel_encoders_disable(state, crtc);
ea9d758d 7321
f5271ee5 7322 intel_crtc_vblank_off(old_crtc_state);
f9b61ff6 7323
4972f70a 7324 intel_disable_pipe(old_crtc_state);
32f9d658 7325
9eae5e27 7326 ilk_pfit_disable(old_crtc_state);
2c07245f 7327
6f405638 7328 if (old_crtc_state->has_pch_encoder)
9eae5e27 7329 ilk_fdi_disable(crtc);
5a74f70a 7330
e44c84a1 7331 intel_encoders_post_disable(state, crtc);
2c07245f 7332
6f405638 7333 if (old_crtc_state->has_pch_encoder) {
9eae5e27 7334 ilk_disable_pch_transcoder(dev_priv, pipe);
6be4a607 7335
6e266956 7336 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
7337 i915_reg_t reg;
7338 u32 temp;
7339
d925c59a
DV
7340 /* disable TRANS_DP_CTL */
7341 reg = TRANS_DP_CTL(pipe);
dc008bf0 7342 temp = intel_de_read(dev_priv, reg);
d925c59a
DV
7343 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7344 TRANS_DP_PORT_SEL_MASK);
7345 temp |= TRANS_DP_PORT_SEL_NONE;
dc008bf0 7346 intel_de_write(dev_priv, reg, temp);
d925c59a
DV
7347
7348 /* disable DPLL_SEL */
dc008bf0 7349 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
11887397 7350 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
dc008bf0 7351 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
9db4a9c7 7352 }
e3421a18 7353
9eae5e27 7354 ilk_fdi_pll_disable(crtc);
d925c59a 7355 }
81b088ca 7356
b2c0593a 7357 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 7358 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 7359}
1b3c7a47 7360
1e98f88c
LDM
7361static void hsw_crtc_disable(struct intel_atomic_state *state,
7362 struct intel_crtc *crtc)
ee7b9f93 7363{
773b4b54
VS
7364 /*
7365 * FIXME collapse everything to one hook.
7366 * Need care with mst->ddi interactions.
7367 */
e44c84a1 7368 intel_encoders_disable(state, crtc);
e44c84a1 7369 intel_encoders_post_disable(state, crtc);
4f771f10
PZ
7370}
7371
b2562712 7372static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
2dd24552 7373{
2225f3c6 7374 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 7375 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2dd24552 7376
b2562712 7377 if (!crtc_state->gmch_pfit.control)
2dd24552
JB
7378 return;
7379
2dd24552 7380 /*
c0b03411
DV
7381 * The panel fitter should only be adjusted whilst the pipe is disabled,
7382 * according to register description and PRM.
2dd24552 7383 */
e57291c2
PB
7384 drm_WARN_ON(&dev_priv->drm,
7385 intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
b104e8b2 7386 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
2dd24552 7387
dc008bf0
JN
7388 intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7389 crtc_state->gmch_pfit.pgm_ratios);
7390 intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
5a80c45c
DV
7391
7392 /* Border color in case we don't scale up to the full screen. Black by
7393 * default, change to something else for debugging. */
dc008bf0 7394 intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
2dd24552
JB
7395}
7396
358633e7
MR
7397bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7398{
7399 if (phy == PHY_NONE)
7400 return false;
aefaa1f4
MR
7401 else if (IS_ROCKETLAKE(dev_priv))
7402 return phy <= PHY_D;
24ea098b 7403 else if (IS_JSL_EHL(dev_priv))
358633e7 7404 return phy <= PHY_C;
aefaa1f4 7405 else if (INTEL_GEN(dev_priv) >= 11)
358633e7 7406 return phy <= PHY_B;
aefaa1f4
MR
7407 else
7408 return false;
358633e7
MR
7409}
7410
358633e7
MR
7411bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7412{
aefaa1f4
MR
7413 if (IS_ROCKETLAKE(dev_priv))
7414 return false;
7415 else if (INTEL_GEN(dev_priv) >= 12)
5c719708 7416 return phy >= PHY_D && phy <= PHY_I;
24ea098b 7417 else if (INTEL_GEN(dev_priv) >= 11 && !IS_JSL_EHL(dev_priv))
358633e7 7418 return phy >= PHY_C && phy <= PHY_F;
aefaa1f4
MR
7419 else
7420 return false;
358633e7
MR
7421}
7422
7423enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7424{
1d8ca002
VS
7425 if (IS_ROCKETLAKE(i915) && port >= PORT_TC1)
7426 return PHY_C + port - PORT_TC1;
24ea098b 7427 else if (IS_JSL_EHL(i915) && port == PORT_D)
358633e7
MR
7428 return PHY_A;
7429
1d8ca002 7430 return PHY_A + port - PORT_A;
358633e7
MR
7431}
7432
ac213c1b
PZ
7433enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7434{
358633e7 7435 if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
320c670c 7436 return TC_PORT_NONE;
ac213c1b 7437
6c8337da 7438 if (INTEL_GEN(dev_priv) >= 12)
1d8ca002
VS
7439 return TC_PORT_1 + port - PORT_TC1;
7440 else
7441 return TC_PORT_1 + port - PORT_C;
ac213c1b
PZ
7442}
7443
79f255a0 7444enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
7445{
7446 switch (port) {
7447 case PORT_A:
6331a704 7448 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 7449 case PORT_B:
6331a704 7450 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 7451 case PORT_C:
6331a704 7452 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 7453 case PORT_D:
6331a704 7454 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 7455 case PORT_E:
6331a704 7456 return POWER_DOMAIN_PORT_DDI_E_LANES;
9787e835
RV
7457 case PORT_F:
7458 return POWER_DOMAIN_PORT_DDI_F_LANES;
eb8de23c
KA
7459 case PORT_G:
7460 return POWER_DOMAIN_PORT_DDI_G_LANES;
07c9b088
VS
7461 case PORT_H:
7462 return POWER_DOMAIN_PORT_DDI_H_LANES;
7463 case PORT_I:
7464 return POWER_DOMAIN_PORT_DDI_I_LANES;
d05410f9 7465 default:
b9fec167 7466 MISSING_CASE(port);
d05410f9
DA
7467 return POWER_DOMAIN_PORT_OTHER;
7468 }
7469}
7470
337837ac
ID
7471enum intel_display_power_domain
7472intel_aux_power_domain(struct intel_digital_port *dig_port)
7473{
dd7239c5 7474 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
d8fe2ab6 7475 enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
dd7239c5 7476
d8fe2ab6 7477 if (intel_phy_is_tc(dev_priv, phy) &&
dd7239c5
ID
7478 dig_port->tc_mode == TC_PORT_TBT_ALT) {
7479 switch (dig_port->aux_ch) {
7480 case AUX_CH_C:
8a84bacb 7481 return POWER_DOMAIN_AUX_C_TBT;
dd7239c5 7482 case AUX_CH_D:
8a84bacb 7483 return POWER_DOMAIN_AUX_D_TBT;
dd7239c5 7484 case AUX_CH_E:
8a84bacb 7485 return POWER_DOMAIN_AUX_E_TBT;
dd7239c5 7486 case AUX_CH_F:
8a84bacb 7487 return POWER_DOMAIN_AUX_F_TBT;
eb8de23c
KA
7488 case AUX_CH_G:
7489 return POWER_DOMAIN_AUX_G_TBT;
244f2e9c
VS
7490 case AUX_CH_H:
7491 return POWER_DOMAIN_AUX_H_TBT;
7492 case AUX_CH_I:
7493 return POWER_DOMAIN_AUX_I_TBT;
dd7239c5
ID
7494 default:
7495 MISSING_CASE(dig_port->aux_ch);
8a84bacb 7496 return POWER_DOMAIN_AUX_C_TBT;
dd7239c5
ID
7497 }
7498 }
7499
dba6b0b4
JRS
7500 return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
7501}
7502
7503/*
7504 * Converts aux_ch to power_domain without caring about TBT ports for that use
7505 * intel_aux_power_domain()
7506 */
7507enum intel_display_power_domain
7508intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
7509{
7510 switch (aux_ch) {
337837ac
ID
7511 case AUX_CH_A:
7512 return POWER_DOMAIN_AUX_A;
7513 case AUX_CH_B:
7514 return POWER_DOMAIN_AUX_B;
7515 case AUX_CH_C:
7516 return POWER_DOMAIN_AUX_C;
7517 case AUX_CH_D:
7518 return POWER_DOMAIN_AUX_D;
7519 case AUX_CH_E:
7520 return POWER_DOMAIN_AUX_E;
7521 case AUX_CH_F:
7522 return POWER_DOMAIN_AUX_F;
eb8de23c
KA
7523 case AUX_CH_G:
7524 return POWER_DOMAIN_AUX_G;
244f2e9c
VS
7525 case AUX_CH_H:
7526 return POWER_DOMAIN_AUX_H;
7527 case AUX_CH_I:
7528 return POWER_DOMAIN_AUX_I;
337837ac 7529 default:
dba6b0b4 7530 MISSING_CASE(aux_ch);
337837ac
ID
7531 return POWER_DOMAIN_AUX_A;
7532 }
7533}
7534
afe0c21b 7535static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
77d22dca 7536{
2225f3c6 7537 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
afe0c21b 7538 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
74bff5f9 7539 struct drm_encoder *encoder;
855e0d68 7540 enum pipe pipe = crtc->pipe;
d8fc70b7 7541 u64 mask;
74bff5f9 7542 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 7543
1326a92c 7544 if (!crtc_state->hw.active)
292b990e
ML
7545 return 0;
7546
17bd6e66
ID
7547 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7548 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
7549 if (crtc_state->pch_pfit.enabled ||
7550 crtc_state->pch_pfit.force_thru)
d8fc70b7 7551 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 7552
afe0c21b 7553 drm_for_each_encoder_mask(encoder, &dev_priv->drm,
2225f3c6 7554 crtc_state->uapi.encoder_mask) {
74bff5f9
ML
7555 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7556
79f255a0 7557 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 7558 }
319be8ae 7559
37255d8d 7560 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
17bd6e66 7561 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
37255d8d 7562
15e7ec29 7563 if (crtc_state->shared_dpll)
08d8e170 7564 mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
15e7ec29 7565
8a029c11
MN
7566 if (crtc_state->dsc.compression_enable)
7567 mask |= BIT_ULL(intel_dsc_power_domain(crtc_state));
7568
77d22dca
ID
7569 return mask;
7570}
7571
d2d15016 7572static u64
afe0c21b 7573modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
77d22dca 7574{
2225f3c6 7575 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
855e0d68 7576 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
292b990e 7577 enum intel_display_power_domain domain;
d8fc70b7 7578 u64 domains, new_domains, old_domains;
77d22dca 7579
855e0d68
ML
7580 old_domains = crtc->enabled_power_domains;
7581 crtc->enabled_power_domains = new_domains =
afe0c21b 7582 get_crtc_power_domains(crtc_state);
77d22dca 7583
5a21b665 7584 domains = new_domains & ~old_domains;
292b990e
ML
7585
7586 for_each_power_domain(domain, domains)
7587 intel_display_power_get(dev_priv, domain);
7588
5a21b665 7589 return old_domains & ~new_domains;
292b990e
ML
7590}
7591
7592static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 7593 u64 domains)
292b990e
ML
7594{
7595 enum intel_display_power_domain domain;
7596
7597 for_each_power_domain(domain, domains)
0e6e0be4 7598 intel_display_power_put_unchecked(dev_priv, domain);
292b990e 7599}
77d22dca 7600
7451a074
VS
7601static void valleyview_crtc_enable(struct intel_atomic_state *state,
7602 struct intel_crtc *crtc)
adafdc6f 7603{
7451a074
VS
7604 const struct intel_crtc_state *new_crtc_state =
7605 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7606 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7607 enum pipe pipe = crtc->pipe;
adafdc6f 7608
e57291c2 7609 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7ff89ca2 7610 return;
adafdc6f 7611
502d8714
VS
7612 if (intel_crtc_has_dp_encoder(new_crtc_state))
7613 intel_dp_set_m_n(new_crtc_state, M1_N1);
b2045352 7614
e7fc3f90 7615 intel_set_transcoder_timings(new_crtc_state);
502d8714 7616 intel_set_pipe_src_size(new_crtc_state);
b2045352 7617
7ff89ca2 7618 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
dc008bf0
JN
7619 intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7620 intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
560a7ae4
DL
7621 }
7622
502d8714 7623 i9xx_set_pipeconf(new_crtc_state);
560a7ae4 7624
e44c84a1 7625 crtc->active = true;
92891e45 7626
7ff89ca2 7627 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 7628
e44c84a1 7629 intel_encoders_pre_pll_enable(state, crtc);
5f199dfa 7630
7ff89ca2 7631 if (IS_CHERRYVIEW(dev_priv)) {
502d8714
VS
7632 chv_prepare_pll(crtc, new_crtc_state);
7633 chv_enable_pll(crtc, new_crtc_state);
7ff89ca2 7634 } else {
502d8714
VS
7635 vlv_prepare_pll(crtc, new_crtc_state);
7636 vlv_enable_pll(crtc, new_crtc_state);
5f199dfa
VS
7637 }
7638
e44c84a1 7639 intel_encoders_pre_enable(state, crtc);
5f199dfa 7640
502d8714 7641 i9xx_pfit_enable(new_crtc_state);
89b3c3c7 7642
502d8714
VS
7643 intel_color_load_luts(new_crtc_state);
7644 intel_color_commit(new_crtc_state);
73a116be 7645 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7646 intel_disable_primary_plane(new_crtc_state);
89b3c3c7 7647
e44c84a1 7648 dev_priv->display.initial_watermarks(state, crtc);
502d8714 7649 intel_enable_pipe(new_crtc_state);
7ff89ca2 7650
502d8714 7651 intel_crtc_vblank_on(new_crtc_state);
89b3c3c7 7652
e44c84a1 7653 intel_encoders_enable(state, crtc);
89b3c3c7
ACO
7654}
7655
b2354c78 7656static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
2b73001e 7657{
2225f3c6 7658 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2354c78 7659 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
83d7c81f 7660
dc008bf0
JN
7661 intel_de_write(dev_priv, FP0(crtc->pipe),
7662 crtc_state->dpll_hw_state.fp0);
7663 intel_de_write(dev_priv, FP1(crtc->pipe),
7664 crtc_state->dpll_hw_state.fp1);
2b73001e
VS
7665}
7666
7451a074
VS
7667static void i9xx_crtc_enable(struct intel_atomic_state *state,
7668 struct intel_crtc *crtc)
2b73001e 7669{
7451a074
VS
7670 const struct intel_crtc_state *new_crtc_state =
7671 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7672 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7673 enum pipe pipe = crtc->pipe;
2b73001e 7674
e57291c2 7675 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7ff89ca2 7676 return;
2b73001e 7677
502d8714 7678 i9xx_set_pll_dividers(new_crtc_state);
2b73001e 7679
502d8714
VS
7680 if (intel_crtc_has_dp_encoder(new_crtc_state))
7681 intel_dp_set_m_n(new_crtc_state, M1_N1);
83d7c81f 7682
e7fc3f90 7683 intel_set_transcoder_timings(new_crtc_state);
502d8714 7684 intel_set_pipe_src_size(new_crtc_state);
2b73001e 7685
502d8714 7686 i9xx_set_pipeconf(new_crtc_state);
f8437dd1 7687
e44c84a1 7688 crtc->active = true;
5f199dfa 7689
cf819eff 7690 if (!IS_GEN(dev_priv, 2))
7ff89ca2 7691 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 7692
e44c84a1 7693 intel_encoders_pre_enable(state, crtc);
f8437dd1 7694
502d8714 7695 i9xx_enable_pll(crtc, new_crtc_state);
f8437dd1 7696
502d8714 7697 i9xx_pfit_enable(new_crtc_state);
f8437dd1 7698
502d8714
VS
7699 intel_color_load_luts(new_crtc_state);
7700 intel_color_commit(new_crtc_state);
73a116be 7701 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7702 intel_disable_primary_plane(new_crtc_state);
f8437dd1 7703
7a8fdb1f 7704 if (dev_priv->display.initial_watermarks)
e44c84a1 7705 dev_priv->display.initial_watermarks(state, crtc);
04548cba 7706 else
e44c84a1 7707 intel_update_watermarks(crtc);
502d8714 7708 intel_enable_pipe(new_crtc_state);
f8437dd1 7709
502d8714 7710 intel_crtc_vblank_on(new_crtc_state);
f8437dd1 7711
e44c84a1 7712 intel_encoders_enable(state, crtc);
f6a7d395
VS
7713
7714 /* prevents spurious underruns */
7715 if (IS_GEN(dev_priv, 2))
7716 intel_wait_for_vblank(dev_priv, pipe);
7ff89ca2 7717}
f8437dd1 7718
b2562712 7719static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7ff89ca2 7720{
2225f3c6 7721 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
b2562712 7722 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f8437dd1 7723
b2562712 7724 if (!old_crtc_state->gmch_pfit.control)
f8437dd1 7725 return;
f8437dd1 7726
b104e8b2 7727 assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7ff89ca2 7728
cd49f818 7729 drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
dc008bf0
JN
7730 intel_de_read(dev_priv, PFIT_CONTROL));
7731 intel_de_write(dev_priv, PFIT_CONTROL, 0);
f8437dd1
VK
7732}
7733
7451a074
VS
7734static void i9xx_crtc_disable(struct intel_atomic_state *state,
7735 struct intel_crtc *crtc)
f8437dd1 7736{
7451a074
VS
7737 struct intel_crtc_state *old_crtc_state =
7738 intel_atomic_get_old_crtc_state(state, crtc);
e44c84a1
VS
7739 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7740 enum pipe pipe = crtc->pipe;
d66a2194 7741
d66a2194 7742 /*
7ff89ca2
VS
7743 * On gen2 planes are double buffered but the pipe isn't, so we must
7744 * wait for planes to fully turn off before disabling the pipe.
d66a2194 7745 */
cf819eff 7746 if (IS_GEN(dev_priv, 2))
7ff89ca2 7747 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 7748
e44c84a1 7749 intel_encoders_disable(state, crtc);
d66a2194 7750
f5271ee5 7751 intel_crtc_vblank_off(old_crtc_state);
d66a2194 7752
4972f70a 7753 intel_disable_pipe(old_crtc_state);
d66a2194 7754
b2562712 7755 i9xx_pfit_disable(old_crtc_state);
89b3c3c7 7756
e44c84a1 7757 intel_encoders_post_disable(state, crtc);
d66a2194 7758
6f405638 7759 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7ff89ca2
VS
7760 if (IS_CHERRYVIEW(dev_priv))
7761 chv_disable_pll(dev_priv, pipe);
7762 else if (IS_VALLEYVIEW(dev_priv))
7763 vlv_disable_pll(dev_priv, pipe);
7764 else
b2354c78 7765 i9xx_disable_pll(old_crtc_state);
7ff89ca2 7766 }
c2e001ef 7767
e44c84a1 7768 intel_encoders_post_pll_disable(state, crtc);
89b3c3c7 7769
cf819eff 7770 if (!IS_GEN(dev_priv, 2))
7ff89ca2 7771 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
7772
7773 if (!dev_priv->display.initial_watermarks)
e44c84a1 7774 intel_update_watermarks(crtc);
2ee0da16
VS
7775
7776 /* clock the pipe down to 640x480@60 to potentially save power */
7777 if (IS_I830(dev_priv))
7778 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
7779}
7780
56273062 7781static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
da1d0e26 7782 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 7783{
7ff89ca2 7784 struct intel_encoder *encoder;
56273062 7785 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
c457d9cf
VS
7786 struct intel_bw_state *bw_state =
7787 to_intel_bw_state(dev_priv->bw_obj.state);
1965de63 7788 struct intel_cdclk_state *cdclk_state =
28a30b45 7789 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
3cf43cdc
VS
7790 struct intel_dbuf_state *dbuf_state =
7791 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
1e460bf9 7792 struct intel_crtc_state *crtc_state =
56273062 7793 to_intel_crtc_state(crtc->base.state);
7ff89ca2 7794 enum intel_display_power_domain domain;
b1e01595 7795 struct intel_plane *plane;
7ff89ca2 7796 struct drm_atomic_state *state;
1e460bf9 7797 struct intel_crtc_state *temp_crtc_state;
56273062
VS
7798 enum pipe pipe = crtc->pipe;
7799 u64 domains;
7ff89ca2 7800 int ret;
f8437dd1 7801
56273062 7802 if (!crtc_state->hw.active)
7ff89ca2 7803 return;
a8ca4934 7804
56273062 7805 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
b1e01595
VS
7806 const struct intel_plane_state *plane_state =
7807 to_intel_plane_state(plane->base.state);
709e05c3 7808
f90a85e7 7809 if (plane_state->uapi.visible)
56273062 7810 intel_plane_disable_noatomic(crtc, plane);
7ff89ca2 7811 }
5d96d8af 7812
56273062 7813 state = drm_atomic_state_alloc(&dev_priv->drm);
7ff89ca2 7814 if (!state) {
cd49f818
WK
7815 drm_dbg_kms(&dev_priv->drm,
7816 "failed to disable [CRTC:%d:%s], out of memory",
7817 crtc->base.base.id, crtc->base.name);
1c3f7700 7818 return;
7ff89ca2 7819 }
9f7eb31a 7820
da1d0e26 7821 state->acquire_ctx = ctx;
ea61791e 7822
7ff89ca2 7823 /* Everything's already locked, -EDEADLK can't happen. */
56273062
VS
7824 temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7825 ret = drm_atomic_add_affected_connectors(state, &crtc->base);
9f7eb31a 7826
e57291c2 7827 drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
5d96d8af 7828
56273062 7829 dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
4a806558 7830
0853695c 7831 drm_atomic_state_put(state);
842e0307 7832
cd49f818
WK
7833 drm_dbg_kms(&dev_priv->drm,
7834 "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7835 crtc->base.base.id, crtc->base.name);
56273062
VS
7836
7837 crtc->active = false;
7838 crtc->base.enabled = false;
7839
e57291c2
PB
7840 drm_WARN_ON(&dev_priv->drm,
7841 drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
56273062
VS
7842 crtc_state->uapi.active = false;
7843 crtc_state->uapi.connector_mask = 0;
7844 crtc_state->uapi.encoder_mask = 0;
58d124ea
ML
7845 intel_crtc_free_hw_state(crtc_state);
7846 memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
842e0307 7847
56273062 7848 for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
842e0307
ML
7849 encoder->base.crtc = NULL;
7850
56273062
VS
7851 intel_fbc_disable(crtc);
7852 intel_update_watermarks(crtc);
7853 intel_disable_shared_dpll(crtc_state);
b17d48e2 7854
56273062 7855 domains = crtc->enabled_power_domains;
b17d48e2 7856 for_each_power_domain(domain, domains)
0e6e0be4 7857 intel_display_power_put_unchecked(dev_priv, domain);
56273062 7858 crtc->enabled_power_domains = 0;
565602d7 7859
56273062 7860 dev_priv->active_pipes &= ~BIT(pipe);
1965de63
VS
7861 cdclk_state->min_cdclk[pipe] = 0;
7862 cdclk_state->min_voltage_level[pipe] = 0;
0c2d5512 7863 cdclk_state->active_pipes &= ~BIT(pipe);
c457d9cf 7864
3cf43cdc
VS
7865 dbuf_state->active_pipes &= ~BIT(pipe);
7866
56273062
VS
7867 bw_state->data_rate[pipe] = 0;
7868 bw_state->num_active_planes[pipe] = 0;
b17d48e2
ML
7869}
7870
6b72d486
ML
7871/*
7872 * turn all crtc's off, but do not adjust state
7873 * This has to be paired with a call to intel_modeset_setup_hw_state.
7874 */
70e0bd74 7875int intel_display_suspend(struct drm_device *dev)
ee7b9f93 7876{
e2c8b870 7877 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 7878 struct drm_atomic_state *state;
e2c8b870 7879 int ret;
70e0bd74 7880
e2c8b870
ML
7881 state = drm_atomic_helper_suspend(dev);
7882 ret = PTR_ERR_OR_ZERO(state);
70e0bd74 7883 if (ret)
cd49f818
WK
7884 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7885 ret);
e2c8b870
ML
7886 else
7887 dev_priv->modeset_restore_state = state;
70e0bd74 7888 return ret;
ee7b9f93
JB
7889}
7890
ea5b213a 7891void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 7892{
4ef69c7a 7893 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 7894
ea5b213a
CW
7895 drm_encoder_cleanup(encoder);
7896 kfree(intel_encoder);
7e7d76c3
JB
7897}
7898
0a91ca29
DV
7899/* Cross check the actual hw state with our own modeset state tracking (and it's
7900 * internal consistency). */
3b4bf24d 7901static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
749d98b8 7902 struct drm_connector_state *conn_state)
79e53945 7903{
749d98b8 7904 struct intel_connector *connector = to_intel_connector(conn_state->connector);
cd49f818 7905 struct drm_i915_private *i915 = to_i915(connector->base.dev);
35dd3c64 7906
cd49f818
WK
7907 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7908 connector->base.base.id, connector->base.name);
35dd3c64 7909
0a91ca29 7910 if (connector->get_hw_state(connector)) {
fa7edcd2 7911 struct intel_encoder *encoder = intel_attached_encoder(connector);
0a91ca29 7912
749d98b8 7913 I915_STATE_WARN(!crtc_state,
35dd3c64 7914 "connector enabled without attached crtc\n");
0a91ca29 7915
749d98b8 7916 if (!crtc_state)
35dd3c64
ML
7917 return;
7918
1326a92c
ML
7919 I915_STATE_WARN(!crtc_state->hw.active,
7920 "connector is active, but attached crtc isn't\n");
35dd3c64 7921
e85376cb 7922 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
7923 return;
7924
e85376cb 7925 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
7926 "atomic encoder doesn't match attached encoder\n");
7927
e85376cb 7928 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
7929 "attached encoder crtc differs from connector crtc\n");
7930 } else {
1326a92c
ML
7931 I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7932 "attached crtc is active, but connector isn't\n");
749d98b8 7933 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 7934 "best encoder set without crtc!\n");
0a91ca29 7935 }
79e53945
JB
7936}
7937
6d293983 7938static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7939{
1326a92c 7940 if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
6d293983 7941 return crtc_state->fdi_lanes;
d272ddfa
VS
7942
7943 return 0;
7944}
7945
9eae5e27
LDM
7946static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7947 struct intel_crtc_state *pipe_config)
1857e1da 7948{
8652744b 7949 struct drm_i915_private *dev_priv = to_i915(dev);
2225f3c6 7950 struct drm_atomic_state *state = pipe_config->uapi.state;
6d293983
ACO
7951 struct intel_crtc *other_crtc;
7952 struct intel_crtc_state *other_crtc_state;
7953
cd49f818
WK
7954 drm_dbg_kms(&dev_priv->drm,
7955 "checking fdi config on pipe %c, lanes %i\n",
7956 pipe_name(pipe), pipe_config->fdi_lanes);
1857e1da 7957 if (pipe_config->fdi_lanes > 4) {
cd49f818
WK
7958 drm_dbg_kms(&dev_priv->drm,
7959 "invalid fdi lane config on pipe %c: %i lanes\n",
7960 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7961 return -EINVAL;
1857e1da
DV
7962 }
7963
8652744b 7964 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da 7965 if (pipe_config->fdi_lanes > 2) {
cd49f818
WK
7966 drm_dbg_kms(&dev_priv->drm,
7967 "only 2 lanes on haswell, required: %i lanes\n",
7968 pipe_config->fdi_lanes);
6d293983 7969 return -EINVAL;
1857e1da 7970 } else {
6d293983 7971 return 0;
1857e1da
DV
7972 }
7973 }
7974
24977870 7975 if (INTEL_NUM_PIPES(dev_priv) == 2)
6d293983 7976 return 0;
1857e1da
DV
7977
7978 /* Ivybridge 3 pipe is really complicated */
7979 switch (pipe) {
7980 case PIPE_A:
6d293983 7981 return 0;
1857e1da 7982 case PIPE_B:
6d293983
ACO
7983 if (pipe_config->fdi_lanes <= 2)
7984 return 0;
7985
b91eb5cc 7986 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7987 other_crtc_state =
7988 intel_atomic_get_crtc_state(state, other_crtc);
7989 if (IS_ERR(other_crtc_state))
7990 return PTR_ERR(other_crtc_state);
7991
7992 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
cd49f818
WK
7993 drm_dbg_kms(&dev_priv->drm,
7994 "invalid shared fdi lane config on pipe %c: %i lanes\n",
7995 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7996 return -EINVAL;
1857e1da 7997 }
6d293983 7998 return 0;
1857e1da 7999 case PIPE_C:
251cc67c 8000 if (pipe_config->fdi_lanes > 2) {
cd49f818
WK
8001 drm_dbg_kms(&dev_priv->drm,
8002 "only 2 lanes on pipe %c: required %i lanes\n",
8003 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 8004 return -EINVAL;
251cc67c 8005 }
6d293983 8006
b91eb5cc 8007 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
8008 other_crtc_state =
8009 intel_atomic_get_crtc_state(state, other_crtc);
8010 if (IS_ERR(other_crtc_state))
8011 return PTR_ERR(other_crtc_state);
8012
8013 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
cd49f818
WK
8014 drm_dbg_kms(&dev_priv->drm,
8015 "fdi link B uses too many lanes to enable link C\n");
6d293983 8016 return -EINVAL;
1857e1da 8017 }
6d293983 8018 return 0;
1857e1da
DV
8019 default:
8020 BUG();
8021 }
8022}
8023
e29c22c0 8024#define RETRY 1
9eae5e27
LDM
8025static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
8026 struct intel_crtc_state *pipe_config)
877d48d5 8027{
1857e1da 8028 struct drm_device *dev = intel_crtc->base.dev;
cd49f818 8029 struct drm_i915_private *i915 = to_i915(dev);
1326a92c 8030 const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
6d293983
ACO
8031 int lane, link_bw, fdi_dotclock, ret;
8032 bool needs_recompute = false;
877d48d5 8033
e29c22c0 8034retry:
877d48d5
DV
8035 /* FDI is a binary signal running at ~2.7GHz, encoding
8036 * each output octet as 10 bits. The actual frequency
8037 * is stored as a divider into a 100MHz clock, and the
8038 * mode pixel clock is stored in units of 1KHz.
8039 * Hence the bw of each lane in terms of the mode signal
8040 * is:
8041 */
cd49f818 8042 link_bw = intel_fdi_link_freq(i915, pipe_config);
877d48d5 8043
241bfc38 8044 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 8045
9eae5e27
LDM
8046 lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
8047 pipe_config->pipe_bpp);
877d48d5
DV
8048
8049 pipe_config->fdi_lanes = lane;
8050
2bd89a07 8051 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
ed06efb8 8052 link_bw, &pipe_config->fdi_m_n, false, false);
1857e1da 8053
9eae5e27 8054 ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
8e2b4dff
VS
8055 if (ret == -EDEADLK)
8056 return ret;
8057
6d293983 8058 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 8059 pipe_config->pipe_bpp -= 2*3;
cd49f818
WK
8060 drm_dbg_kms(&i915->drm,
8061 "fdi link bw constraint, reducing pipe bpp to %i\n",
8062 pipe_config->pipe_bpp);
7ff89ca2
VS
8063 needs_recompute = true;
8064 pipe_config->bw_constrained = true;
257a7ffc 8065
7ff89ca2 8066 goto retry;
257a7ffc 8067 }
79e53945 8068
7ff89ca2
VS
8069 if (needs_recompute)
8070 return RETRY;
e70236a8 8071
7ff89ca2 8072 return ret;
e70236a8
JB
8073}
8074
24f28450 8075bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
e70236a8 8076{
2225f3c6 8077 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
24f28450
ML
8078 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8079
8080 /* IPS only exists on ULT machines and is tied to pipe A. */
8081 if (!hsw_crtc_supports_ips(crtc))
6e644626
VS
8082 return false;
8083
8a25c4be 8084 if (!dev_priv->params.enable_ips)
7ff89ca2 8085 return false;
e70236a8 8086
24f28450
ML
8087 if (crtc_state->pipe_bpp > 24)
8088 return false;
1b1d2716 8089
65cd2b3f 8090 /*
7ff89ca2
VS
8091 * We compare against max which means we must take
8092 * the increased cdclk requirement into account when
8093 * calculating the new cdclk.
8094 *
8095 * Should measure whether using a lower cdclk w/o IPS
e70236a8 8096 */
24f28450
ML
8097 if (IS_BROADWELL(dev_priv) &&
8098 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
8099 return false;
8100
8101 return true;
e70236a8 8102}
79e53945 8103
28a30b45 8104static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7ff89ca2 8105{
24f28450 8106 struct drm_i915_private *dev_priv =
2225f3c6 8107 to_i915(crtc_state->uapi.crtc->dev);
28a30b45 8108 struct intel_atomic_state *state =
2225f3c6 8109 to_intel_atomic_state(crtc_state->uapi.state);
28a30b45
VS
8110
8111 crtc_state->ips_enabled = false;
24f28450
ML
8112
8113 if (!hsw_crtc_state_ips_capable(crtc_state))
28a30b45 8114 return 0;
24f28450 8115
a8ebf607
JRS
8116 /*
8117 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
8118 * enabled and disabled dynamically based on package C states,
8119 * user space can't make reliable use of the CRCs, so let's just
8120 * completely disable it.
8121 */
8122 if (crtc_state->crc_enabled)
28a30b45 8123 return 0;
24f28450 8124
adbe5c5c
ML
8125 /* IPS should be fine as long as at least one plane is enabled. */
8126 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
28a30b45 8127 return 0;
34edce2f 8128
28a30b45
VS
8129 if (IS_BROADWELL(dev_priv)) {
8130 const struct intel_cdclk_state *cdclk_state;
24f28450 8131
28a30b45
VS
8132 cdclk_state = intel_atomic_get_cdclk_state(state);
8133 if (IS_ERR(cdclk_state))
8134 return PTR_ERR(cdclk_state);
8135
8136 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
8137 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
8138 return 0;
8139 }
8140
8141 crtc_state->ips_enabled = true;
8142
8143 return 0;
34edce2f
VS
8144}
8145
7ff89ca2 8146static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 8147{
7ff89ca2 8148 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 8149
7ff89ca2 8150 /* GDG double wide on either pipe, otherwise pipe A only */
c56b89f1 8151 return INTEL_GEN(dev_priv) < 4 &&
7ff89ca2 8152 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
8153}
8154
eac9c585 8155static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
ceb99320 8156{
bafcdad6 8157 u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
35dd95b4 8158 unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
ceb99320
VS
8159
8160 /*
8161 * We only use IF-ID interlacing. If we ever use
8162 * PF-ID we'll need to adjust the pixel_rate here.
8163 */
8164
eac9c585
VS
8165 if (!crtc_state->pch_pfit.enabled)
8166 return pixel_rate;
ceb99320 8167
eac9c585
VS
8168 pipe_w = crtc_state->pipe_src_w;
8169 pipe_h = crtc_state->pipe_src_h;
ceb99320 8170
35dd95b4
VS
8171 pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
8172 pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
8173
eac9c585
VS
8174 if (pipe_w < pfit_w)
8175 pipe_w = pfit_w;
8176 if (pipe_h < pfit_h)
8177 pipe_h = pfit_h;
ceb99320 8178
eac9c585
VS
8179 if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
8180 !pfit_w || !pfit_h))
8181 return pixel_rate;
ceb99320 8182
eac9c585
VS
8183 return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
8184 pfit_w * pfit_h);
ceb99320
VS
8185}
8186
33574ec9
VS
8187static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
8188 const struct drm_display_mode *timings)
8189{
8190 mode->hdisplay = timings->crtc_hdisplay;
8191 mode->htotal = timings->crtc_htotal;
8192 mode->hsync_start = timings->crtc_hsync_start;
8193 mode->hsync_end = timings->crtc_hsync_end;
8194
8195 mode->vdisplay = timings->crtc_vdisplay;
8196 mode->vtotal = timings->crtc_vtotal;
8197 mode->vsync_start = timings->crtc_vsync_start;
8198 mode->vsync_end = timings->crtc_vsync_end;
8199
8200 mode->flags = timings->flags;
8201 mode->type = DRM_MODE_TYPE_DRIVER;
8202
8203 mode->clock = timings->crtc_clock;
8204
8205 drm_mode_set_name(mode);
8206}
8207
7ff89ca2 8208static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 8209{
2225f3c6 8210 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
34edce2f 8211
b2ae318a 8212 if (HAS_GMCH(dev_priv))
7ff89ca2
VS
8213 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
8214 crtc_state->pixel_rate =
bafcdad6 8215 crtc_state->hw.pipe_mode.crtc_clock;
7ff89ca2
VS
8216 else
8217 crtc_state->pixel_rate =
8218 ilk_pipe_pixel_rate(crtc_state);
8219}
34edce2f 8220
c42773b6
VS
8221static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
8222{
8223 struct drm_display_mode *mode = &crtc_state->hw.mode;
bafcdad6 8224 struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
c42773b6
VS
8225 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8226
bafcdad6
ML
8227 drm_mode_copy(pipe_mode, adjusted_mode);
8228
0385ecea
MN
8229 if (crtc_state->bigjoiner) {
8230 /*
8231 * transcoder is programmed to the full mode,
8232 * but pipe timings are half of the transcoder mode
8233 */
8234 pipe_mode->crtc_hdisplay /= 2;
8235 pipe_mode->crtc_hblank_start /= 2;
8236 pipe_mode->crtc_hblank_end /= 2;
8237 pipe_mode->crtc_hsync_start /= 2;
8238 pipe_mode->crtc_hsync_end /= 2;
8239 pipe_mode->crtc_htotal /= 2;
8240 pipe_mode->crtc_clock /= 2;
8241 }
8242
bafcdad6 8243 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
c42773b6
VS
8244 intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
8245
8246 intel_crtc_compute_pixel_rate(crtc_state);
8247
8248 drm_mode_copy(mode, adjusted_mode);
0385ecea 8249 mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
c42773b6
VS
8250 mode->vdisplay = crtc_state->pipe_src_h;
8251}
8252
65c1ed30
MN
8253static void intel_encoder_get_config(struct intel_encoder *encoder,
8254 struct intel_crtc_state *crtc_state)
8255{
8256 encoder->get_config(encoder, crtc_state);
c42773b6
VS
8257
8258 intel_crtc_readout_derived_state(crtc_state);
65c1ed30
MN
8259}
8260
7ff89ca2
VS
8261static int intel_crtc_compute_config(struct intel_crtc *crtc,
8262 struct intel_crtc_state *pipe_config)
8263{
d2daff2c 8264 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bafcdad6 8265 struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
7ff89ca2 8266 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 8267
bafcdad6 8268 drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
19f65a3d
ML
8269
8270 /* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
8271 if (pipe_config->bigjoiner) {
8272 pipe_mode->crtc_clock /= 2;
8273 pipe_mode->crtc_hdisplay /= 2;
8274 pipe_mode->crtc_hblank_start /= 2;
8275 pipe_mode->crtc_hblank_end /= 2;
8276 pipe_mode->crtc_hsync_start /= 2;
8277 pipe_mode->crtc_hsync_end /= 2;
8278 pipe_mode->crtc_htotal /= 2;
8279 pipe_config->pipe_src_w /= 2;
8280 }
8281
bafcdad6
ML
8282 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
8283
7ff89ca2
VS
8284 if (INTEL_GEN(dev_priv) < 4) {
8285 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 8286
7ff89ca2
VS
8287 /*
8288 * Enable double wide mode when the dot clock
8289 * is > 90% of the (display) core speed.
8290 */
8291 if (intel_crtc_supports_double_wide(crtc) &&
bafcdad6 8292 pipe_mode->crtc_clock > clock_limit) {
7ff89ca2
VS
8293 clock_limit = dev_priv->max_dotclk_freq;
8294 pipe_config->double_wide = true;
8295 }
34edce2f
VS
8296 }
8297
bafcdad6 8298 if (pipe_mode->crtc_clock > clock_limit) {
cd49f818
WK
8299 drm_dbg_kms(&dev_priv->drm,
8300 "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
bafcdad6 8301 pipe_mode->crtc_clock, clock_limit,
cd49f818 8302 yesno(pipe_config->double_wide));
7ff89ca2
VS
8303 return -EINVAL;
8304 }
34edce2f 8305
8c79f844
SS
8306 if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8307 pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
1326a92c 8308 pipe_config->hw.ctm) {
25edf915
SS
8309 /*
8310 * There is only one pipe CSC unit per pipe, and we need that
8311 * for output conversion from RGB->YCBCR. So if CTM is already
8312 * applied we can't support YCBCR420 output.
8313 */
cd49f818
WK
8314 drm_dbg_kms(&dev_priv->drm,
8315 "YCBCR420 and CTM together are not possible\n");
25edf915
SS
8316 return -EINVAL;
8317 }
8318
7ff89ca2
VS
8319 /*
8320 * Pipe horizontal size must be even in:
8321 * - DVO ganged mode
8322 * - LVDS dual channel mode
8323 * - Double wide pipe
8324 */
0574bd88
VS
8325 if (pipe_config->pipe_src_w & 1) {
8326 if (pipe_config->double_wide) {
cd49f818
WK
8327 drm_dbg_kms(&dev_priv->drm,
8328 "Odd pipe source width not supported with double wide pipe\n");
0574bd88
VS
8329 return -EINVAL;
8330 }
8331
8332 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
d2daff2c 8333 intel_is_dual_link_lvds(dev_priv)) {
cd49f818
WK
8334 drm_dbg_kms(&dev_priv->drm,
8335 "Odd pipe source width not supported with dual link LVDS\n");
0574bd88
VS
8336 return -EINVAL;
8337 }
8338 }
34edce2f 8339
7ff89ca2
VS
8340 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
8341 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8342 */
8343 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
bafcdad6 8344 pipe_mode->crtc_hsync_start == pipe_mode->crtc_hdisplay)
7ff89ca2 8345 return -EINVAL;
34edce2f 8346
7ff89ca2 8347 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 8348
7ff89ca2 8349 if (pipe_config->has_pch_encoder)
9eae5e27 8350 return ilk_fdi_compute_config(crtc, pipe_config);
34edce2f 8351
7ff89ca2 8352 return 0;
34edce2f
VS
8353}
8354
2c07245f 8355static void
ba3f4d0a 8356intel_reduce_m_n_ratio(u32 *num, u32 *den)
2c07245f 8357{
a65851af
VS
8358 while (*num > DATA_LINK_M_N_MASK ||
8359 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
8360 *num >>= 1;
8361 *den >>= 1;
8362 }
8363}
8364
a65851af 8365static void compute_m_n(unsigned int m, unsigned int n,
ba3f4d0a 8366 u32 *ret_m, u32 *ret_n,
53ca2edc 8367 bool constant_n)
a65851af 8368{
9a86cda0 8369 /*
53ca2edc
LS
8370 * Several DP dongles in particular seem to be fussy about
8371 * too large link M/N values. Give N value as 0x8000 that
8372 * should be acceptable by specific devices. 0x8000 is the
8373 * specified fixed N value for asynchronous clock mode,
8374 * which the devices expect also in synchronous clock mode.
9a86cda0 8375 */
53ca2edc 8376 if (constant_n)
b22960b8 8377 *ret_n = DP_LINK_CONSTANT_N_VALUE;
53ca2edc
LS
8378 else
8379 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
9a86cda0 8380
d492a29d 8381 *ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
a65851af
VS
8382 intel_reduce_m_n_ratio(ret_m, ret_n);
8383}
8384
e69d0bc1 8385void
a4a15777 8386intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
e69d0bc1 8387 int pixel_clock, int link_clock,
b31e85ed 8388 struct intel_link_m_n *m_n,
ed06efb8 8389 bool constant_n, bool fec_enable)
2c07245f 8390{
ed06efb8
ML
8391 u32 data_clock = bits_per_pixel * pixel_clock;
8392
8393 if (fec_enable)
8394 data_clock = intel_dp_mode_to_fec_clock(data_clock);
a65851af 8395
ed06efb8
ML
8396 m_n->tu = 64;
8397 compute_m_n(data_clock,
a65851af 8398 link_clock * nlanes * 8,
b31e85ed 8399 &m_n->gmch_m, &m_n->gmch_n,
53ca2edc 8400 constant_n);
a65851af
VS
8401
8402 compute_m_n(pixel_clock, link_clock,
b31e85ed 8403 &m_n->link_m, &m_n->link_n,
53ca2edc 8404 constant_n);
2c07245f
ZW
8405}
8406
064bd628
JN
8407static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8408{
8409 /*
8410 * There may be no VBT; and if the BIOS enabled SSC we can
8411 * just keep using it to avoid unnecessary flicker. Whereas if the
8412 * BIOS isn't using it, don't assume it will work even if the VBT
8413 * indicates as much.
8414 */
8415 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
dc008bf0
JN
8416 bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8417 PCH_DREF_CONTROL) &
064bd628
JN
8418 DREF_SSC1_ENABLE;
8419
8420 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
cd49f818
WK
8421 drm_dbg_kms(&dev_priv->drm,
8422 "SSC %s by BIOS, overriding VBT which says %s\n",
8423 enableddisabled(bios_lvds_use_ssc),
8424 enableddisabled(dev_priv->vbt.lvds_use_ssc));
064bd628
JN
8425 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8426 }
8427 }
8428}
8429
81b55ef1 8430static bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
a7615030 8431{
8a25c4be
JN
8432 if (dev_priv->params.panel_use_ssc >= 0)
8433 return dev_priv->params.panel_use_ssc != 0;
41aa3448 8434 return dev_priv->vbt.lvds_use_ssc
435793df 8435 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
8436}
8437
ba3f4d0a 8438static u32 pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 8439{
7df00d7a 8440 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 8441}
f47709a9 8442
ba3f4d0a 8443static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
7429e9d4
DV
8444{
8445 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
8446}
8447
f47709a9 8448static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 8449 struct intel_crtc_state *crtc_state,
9e2c8475 8450 struct dpll *reduced_clock)
a7516a05 8451{
9b1e14f4 8452 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
8453 u32 fp, fp2 = 0;
8454
9b1e14f4 8455 if (IS_PINEVIEW(dev_priv)) {
190f68c5 8456 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 8457 if (reduced_clock)
7429e9d4 8458 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 8459 } else {
190f68c5 8460 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 8461 if (reduced_clock)
7429e9d4 8462 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
8463 }
8464
190f68c5 8465 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 8466
2d84d2b3 8467 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 8468 reduced_clock) {
190f68c5 8469 crtc_state->dpll_hw_state.fp1 = fp2;
a7516a05 8470 } else {
190f68c5 8471 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
8472 }
8473}
8474
5e69f97f
CML
8475static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8476 pipe)
89b667f8
JB
8477{
8478 u32 reg_val;
8479
8480 /*
8481 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8482 * and set it to a reasonable value instead.
8483 */
ab3c759a 8484 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
8485 reg_val &= 0xffffff00;
8486 reg_val |= 0x00000030;
ab3c759a 8487 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 8488
ab3c759a 8489 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
8490 reg_val &= 0x00ffffff;
8491 reg_val |= 0x8c000000;
ab3c759a 8492 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 8493
ab3c759a 8494 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 8495 reg_val &= 0xffffff00;
ab3c759a 8496 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 8497
ab3c759a 8498 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
8499 reg_val &= 0x00ffffff;
8500 reg_val |= 0xb0000000;
ab3c759a 8501 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
8502}
8503
4c354754
ML
8504static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8505 const struct intel_link_m_n *m_n)
b551842d 8506{
2225f3c6 8507 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4c354754
ML
8508 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8509 enum pipe pipe = crtc->pipe;
b551842d 8510
dc008bf0
JN
8511 intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8512 TU_SIZE(m_n->tu) | m_n->gmch_m);
8513 intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8514 intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8515 intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
8516}
8517
4207c8b9
ML
8518static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8519 enum transcoder transcoder)
8520{
8521 if (IS_HASWELL(dev_priv))
8522 return transcoder == TRANSCODER_EDP;
8523
8524 /*
8525 * Strictly speaking some registers are available before
8526 * gen7, but we only support DRRS on gen7+
8527 */
cf819eff 8528 return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
4207c8b9
ML
8529}
8530
4c354754
ML
8531static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8532 const struct intel_link_m_n *m_n,
8533 const struct intel_link_m_n *m2_n2)
b551842d 8534{
2225f3c6 8535 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6315b5d3 8536 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4c354754
ML
8537 enum pipe pipe = crtc->pipe;
8538 enum transcoder transcoder = crtc_state->cpu_transcoder;
b551842d 8539
6315b5d3 8540 if (INTEL_GEN(dev_priv) >= 5) {
dc008bf0
JN
8541 intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8542 TU_SIZE(m_n->tu) | m_n->gmch_m);
8543 intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8544 m_n->gmch_n);
8545 intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8546 m_n->link_m);
8547 intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8548 m_n->link_n);
4207c8b9
ML
8549 /*
8550 * M2_N2 registers are set only if DRRS is supported
8551 * (to make sure the registers are not unnecessarily accessed).
f769cd24 8552 */
4207c8b9
ML
8553 if (m2_n2 && crtc_state->has_drrs &&
8554 transcoder_has_m2_n2(dev_priv, transcoder)) {
dc008bf0
JN
8555 intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8556 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8557 intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8558 m2_n2->gmch_n);
8559 intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8560 m2_n2->link_m);
8561 intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8562 m2_n2->link_n);
f769cd24 8563 }
b551842d 8564 } else {
dc008bf0
JN
8565 intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8566 TU_SIZE(m_n->tu) | m_n->gmch_m);
8567 intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8568 intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8569 intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
8570 }
8571}
8572
4c354754 8573void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
03afc4a2 8574{
4c354754 8575 const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
cd49f818 8576 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
fe3cd48d
R
8577
8578 if (m_n == M1_N1) {
4c354754
ML
8579 dp_m_n = &crtc_state->dp_m_n;
8580 dp_m2_n2 = &crtc_state->dp_m2_n2;
fe3cd48d
R
8581 } else if (m_n == M2_N2) {
8582
8583 /*
8584 * M2_N2 registers are not supported. Hence m2_n2 divider value
8585 * needs to be programmed into M1_N1.
8586 */
4c354754 8587 dp_m_n = &crtc_state->dp_m2_n2;
fe3cd48d 8588 } else {
cd49f818 8589 drm_err(&i915->drm, "Unsupported divider value\n");
fe3cd48d
R
8590 return;
8591 }
8592
4c354754
ML
8593 if (crtc_state->has_pch_encoder)
8594 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
03afc4a2 8595 else
4c354754 8596 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
03afc4a2
DV
8597}
8598
251ac862
DV
8599static void vlv_compute_dpll(struct intel_crtc *crtc,
8600 struct intel_crtc_state *pipe_config)
bdd4b6a6 8601{
03ed5cbf 8602 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 8603 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
8604 if (crtc->pipe != PIPE_A)
8605 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 8606
cd2d34d9 8607 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 8608 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
8609 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8610 DPLL_EXT_BUFFER_ENABLE_VLV;
8611
03ed5cbf
VS
8612 pipe_config->dpll_hw_state.dpll_md =
8613 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8614}
bdd4b6a6 8615
03ed5cbf
VS
8616static void chv_compute_dpll(struct intel_crtc *crtc,
8617 struct intel_crtc_state *pipe_config)
8618{
8619 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 8620 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
8621 if (crtc->pipe != PIPE_A)
8622 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8623
cd2d34d9 8624 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 8625 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
8626 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8627
03ed5cbf
VS
8628 pipe_config->dpll_hw_state.dpll_md =
8629 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
8630}
8631
d288f65f 8632static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8633 const struct intel_crtc_state *pipe_config)
a0c4da24 8634{
f47709a9 8635 struct drm_device *dev = crtc->base.dev;
fac5e23e 8636 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8637 enum pipe pipe = crtc->pipe;
bdd4b6a6 8638 u32 mdiv;
a0c4da24 8639 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 8640 u32 coreclk, reg_val;
a0c4da24 8641
cd2d34d9 8642 /* Enable Refclk */
dc008bf0
JN
8643 intel_de_write(dev_priv, DPLL(pipe),
8644 pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
cd2d34d9
VS
8645
8646 /* No need to actually set up the DPLL with DSI */
8647 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8648 return;
8649
221c7862 8650 vlv_dpio_get(dev_priv);
09153000 8651
d288f65f
VS
8652 bestn = pipe_config->dpll.n;
8653 bestm1 = pipe_config->dpll.m1;
8654 bestm2 = pipe_config->dpll.m2;
8655 bestp1 = pipe_config->dpll.p1;
8656 bestp2 = pipe_config->dpll.p2;
a0c4da24 8657
89b667f8
JB
8658 /* See eDP HDMI DPIO driver vbios notes doc */
8659
8660 /* PLL B needs special handling */
bdd4b6a6 8661 if (pipe == PIPE_B)
5e69f97f 8662 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
8663
8664 /* Set up Tx target for periodic Rcomp update */
ab3c759a 8665 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
8666
8667 /* Disable target IRef on PLL */
ab3c759a 8668 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 8669 reg_val &= 0x00ffffff;
ab3c759a 8670 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
8671
8672 /* Disable fast lock */
ab3c759a 8673 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
8674
8675 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
8676 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8677 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8678 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 8679 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
8680
8681 /*
8682 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8683 * but we don't support that).
8684 * Note: don't use the DAC post divider as it seems unstable.
8685 */
8686 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 8687 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 8688
a0c4da24 8689 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 8690 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 8691
89b667f8 8692 /* Set HBR and RBR LPF coefficients */
d288f65f 8693 if (pipe_config->port_clock == 162000 ||
92d54b07
ML
8694 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8695 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
ab3c759a 8696 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 8697 0x009f0003);
89b667f8 8698 else
ab3c759a 8699 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
8700 0x00d0000f);
8701
37a5650b 8702 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 8703 /* Use SSC source */
bdd4b6a6 8704 if (pipe == PIPE_A)
ab3c759a 8705 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8706 0x0df40000);
8707 else
ab3c759a 8708 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8709 0x0df70000);
8710 } else { /* HDMI or VGA */
8711 /* Use bend source */
bdd4b6a6 8712 if (pipe == PIPE_A)
ab3c759a 8713 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8714 0x0df70000);
8715 else
ab3c759a 8716 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8717 0x0df40000);
8718 }
a0c4da24 8719
ab3c759a 8720 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 8721 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
92d54b07 8722 if (intel_crtc_has_dp_encoder(pipe_config))
89b667f8 8723 coreclk |= 0x01000000;
ab3c759a 8724 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 8725
ab3c759a 8726 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
221c7862
CW
8727
8728 vlv_dpio_put(dev_priv);
a0c4da24
JB
8729}
8730
d288f65f 8731static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8732 const struct intel_crtc_state *pipe_config)
9d556c99
CML
8733{
8734 struct drm_device *dev = crtc->base.dev;
fac5e23e 8735 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8736 enum pipe pipe = crtc->pipe;
9d556c99 8737 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 8738 u32 loopfilter, tribuf_calcntr;
9d556c99 8739 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 8740 u32 dpio_val;
9cbe40c1 8741 int vco;
9d556c99 8742
cd2d34d9 8743 /* Enable Refclk and SSC */
dc008bf0
JN
8744 intel_de_write(dev_priv, DPLL(pipe),
8745 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
cd2d34d9
VS
8746
8747 /* No need to actually set up the DPLL with DSI */
8748 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8749 return;
8750
d288f65f
VS
8751 bestn = pipe_config->dpll.n;
8752 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8753 bestm1 = pipe_config->dpll.m1;
8754 bestm2 = pipe_config->dpll.m2 >> 22;
8755 bestp1 = pipe_config->dpll.p1;
8756 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8757 vco = pipe_config->dpll.vco;
a945ce7e 8758 dpio_val = 0;
9cbe40c1 8759 loopfilter = 0;
9d556c99 8760
221c7862 8761 vlv_dpio_get(dev_priv);
9d556c99 8762
9d556c99
CML
8763 /* p1 and p2 divider */
8764 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8765 5 << DPIO_CHV_S1_DIV_SHIFT |
8766 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8767 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8768 1 << DPIO_CHV_K_DIV_SHIFT);
8769
8770 /* Feedback post-divider - m2 */
8771 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8772
8773 /* Feedback refclk divider - n and m1 */
8774 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8775 DPIO_CHV_M1_DIV_BY_2 |
8776 1 << DPIO_CHV_N_DIV_SHIFT);
8777
8778 /* M2 fraction division */
25a25dfc 8779 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8780
8781 /* M2 fraction division enable */
a945ce7e
VP
8782 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8783 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8784 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8785 if (bestm2_frac)
8786 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8787 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8788
de3a0fde
VP
8789 /* Program digital lock detect threshold */
8790 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8791 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8792 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8793 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8794 if (!bestm2_frac)
8795 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8796 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8797
9d556c99 8798 /* Loop filter */
9cbe40c1
VP
8799 if (vco == 5400000) {
8800 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8801 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8802 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8803 tribuf_calcntr = 0x9;
8804 } else if (vco <= 6200000) {
8805 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8806 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8807 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8808 tribuf_calcntr = 0x9;
8809 } else if (vco <= 6480000) {
8810 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8811 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8812 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8813 tribuf_calcntr = 0x8;
8814 } else {
8815 /* Not supported. Apply the same limits as in the max case */
8816 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8817 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8818 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8819 tribuf_calcntr = 0;
8820 }
9d556c99
CML
8821 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8822
968040b2 8823 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8824 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8825 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8826 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8827
9d556c99
CML
8828 /* AFC Recal */
8829 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8830 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8831 DPIO_AFC_RECAL);
8832
221c7862 8833 vlv_dpio_put(dev_priv);
9d556c99
CML
8834}
8835
d288f65f
VS
8836/**
8837 * vlv_force_pll_on - forcibly enable just the PLL
8838 * @dev_priv: i915 private structure
8839 * @pipe: pipe PLL to enable
8840 * @dpll: PLL configuration
8841 *
8842 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8843 * in cases where we need the PLL enabled even when @pipe is not going to
8844 * be enabled.
8845 */
30ad9814 8846int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8847 const struct dpll *dpll)
d288f65f 8848{
b91eb5cc 8849 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8850 struct intel_crtc_state *pipe_config;
8851
216383e9 8852 pipe_config = intel_crtc_state_alloc(crtc);
3f36b937
TU
8853 if (!pipe_config)
8854 return -ENOMEM;
8855
b104e8b2 8856 pipe_config->cpu_transcoder = (enum transcoder)pipe;
3f36b937
TU
8857 pipe_config->pixel_multiplier = 1;
8858 pipe_config->dpll = *dpll;
d288f65f 8859
30ad9814 8860 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8861 chv_compute_dpll(crtc, pipe_config);
8862 chv_prepare_pll(crtc, pipe_config);
8863 chv_enable_pll(crtc, pipe_config);
d288f65f 8864 } else {
3f36b937
TU
8865 vlv_compute_dpll(crtc, pipe_config);
8866 vlv_prepare_pll(crtc, pipe_config);
8867 vlv_enable_pll(crtc, pipe_config);
d288f65f 8868 }
3f36b937
TU
8869
8870 kfree(pipe_config);
8871
8872 return 0;
d288f65f
VS
8873}
8874
8875/**
8876 * vlv_force_pll_off - forcibly disable just the PLL
8877 * @dev_priv: i915 private structure
8878 * @pipe: pipe PLL to disable
8879 *
8880 * Disable the PLL for @pipe. To be used in cases where we need
8881 * the PLL enabled even when @pipe is not going to be enabled.
8882 */
30ad9814 8883void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8884{
30ad9814
VS
8885 if (IS_CHERRYVIEW(dev_priv))
8886 chv_disable_pll(dev_priv, pipe);
d288f65f 8887 else
30ad9814 8888 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8889}
8890
251ac862
DV
8891static void i9xx_compute_dpll(struct intel_crtc *crtc,
8892 struct intel_crtc_state *crtc_state,
9e2c8475 8893 struct dpll *reduced_clock)
eb1cbe48 8894{
9b1e14f4 8895 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8896 u32 dpll;
190f68c5 8897 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8898
190f68c5 8899 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8900
eb1cbe48
DV
8901 dpll = DPLL_VGA_MODE_DIS;
8902
2d84d2b3 8903 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
8904 dpll |= DPLLB_MODE_LVDS;
8905 else
8906 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8907
73f67aa8
JN
8908 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8909 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 8910 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8911 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8912 }
198a037f 8913
3d6e9ee0
VS
8914 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8915 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8916 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8917
37a5650b 8918 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8919 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
8920
8921 /* compute bitmask from p1 value */
9b1e14f4 8922 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
8923 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8924 else {
8925 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8926 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
8927 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8928 }
8929 switch (clock->p2) {
8930 case 5:
8931 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8932 break;
8933 case 7:
8934 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8935 break;
8936 case 10:
8937 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8938 break;
8939 case 14:
8940 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8941 break;
8942 }
9b1e14f4 8943 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
8944 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8945
190f68c5 8946 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8947 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8948 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8949 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8950 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8951 else
8952 dpll |= PLL_REF_INPUT_DREFCLK;
8953
8954 dpll |= DPLL_VCO_ENABLE;
190f68c5 8955 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8956
9b1e14f4 8957 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8958 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8959 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8960 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
8961 }
8962}
8963
251ac862
DV
8964static void i8xx_compute_dpll(struct intel_crtc *crtc,
8965 struct intel_crtc_state *crtc_state,
9e2c8475 8966 struct dpll *reduced_clock)
eb1cbe48 8967{
f47709a9 8968 struct drm_device *dev = crtc->base.dev;
fac5e23e 8969 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8970 u32 dpll;
190f68c5 8971 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8972
190f68c5 8973 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8974
eb1cbe48
DV
8975 dpll = DPLL_VGA_MODE_DIS;
8976
2d84d2b3 8977 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
8978 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8979 } else {
8980 if (clock->p1 == 2)
8981 dpll |= PLL_P1_DIVIDE_BY_TWO;
8982 else
8983 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8984 if (clock->p2 == 4)
8985 dpll |= PLL_P2_DIVIDE_BY_4;
8986 }
8987
171d1562
VS
8988 /*
8989 * Bspec:
8990 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8991 * (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8992 * GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8993 * Enable) must be set to “1” in both the DPLL A Control Register
8994 * (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8995 *
8996 * For simplicity We simply keep both bits always enabled in
8997 * both DPLLS. The spec says we should disable the DVO 2X clock
8998 * when not needed, but this seems to work fine in practice.
8999 */
9000 if (IS_I830(dev_priv) ||
50a0bc90 9001 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
9002 dpll |= DPLL_DVO_2X_MODE;
9003
2d84d2b3 9004 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 9005 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
9006 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
9007 else
9008 dpll |= PLL_REF_INPUT_DREFCLK;
9009
9010 dpll |= DPLL_VCO_ENABLE;
190f68c5 9011 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
9012}
9013
e7fc3f90 9014static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
b0e77b9c 9015{
2225f3c6 9016 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
44fe7f35
ML
9017 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9018 enum pipe pipe = crtc->pipe;
9019 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1326a92c 9020 const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
ba3f4d0a 9021 u32 crtc_vtotal, crtc_vblank_end;
1caea6e9 9022 int vsyncshift = 0;
4d8a62ea
DV
9023
9024 /* We need to be careful not to changed the adjusted mode, for otherwise
9025 * the hw state checker will get angry at the mismatch. */
9026 crtc_vtotal = adjusted_mode->crtc_vtotal;
9027 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 9028
609aeaca 9029 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 9030 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
9031 crtc_vtotal -= 1;
9032 crtc_vblank_end -= 1;
609aeaca 9033
44fe7f35 9034 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
609aeaca
VS
9035 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
9036 else
9037 vsyncshift = adjusted_mode->crtc_hsync_start -
9038 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
9039 if (vsyncshift < 0)
9040 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
9041 }
9042
6315b5d3 9043 if (INTEL_GEN(dev_priv) > 3)
dc008bf0
JN
9044 intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
9045 vsyncshift);
9046
9047 intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
9048 (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
9049 intel_de_write(dev_priv, HBLANK(cpu_transcoder),
9050 (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
9051 intel_de_write(dev_priv, HSYNC(cpu_transcoder),
9052 (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
9053
9054 intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
9055 (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
9056 intel_de_write(dev_priv, VBLANK(cpu_transcoder),
9057 (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
9058 intel_de_write(dev_priv, VSYNC(cpu_transcoder),
9059 (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
b0e77b9c 9060
b5e508d4
PZ
9061 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
9062 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
9063 * documented on the DDI_FUNC_CTL register description, EDP Input Select
9064 * bits. */
772c2a51 9065 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4 9066 (pipe == PIPE_B || pipe == PIPE_C))
dc008bf0
JN
9067 intel_de_write(dev_priv, VTOTAL(pipe),
9068 intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
b5e508d4 9069
bc58be60
JN
9070}
9071
44fe7f35 9072static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
bc58be60 9073{
2225f3c6 9074 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
44fe7f35
ML
9075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9076 enum pipe pipe = crtc->pipe;
bc58be60 9077
b0e77b9c
PZ
9078 /* pipesrc controls the size that is scaled from, which should
9079 * always be the user's requested size.
9080 */
dc008bf0
JN
9081 intel_de_write(dev_priv, PIPESRC(pipe),
9082 ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
b0e77b9c
PZ
9083}
9084
60a02311
VS
9085static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
9086{
2225f3c6 9087 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
60a02311
VS
9088 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
9089
9090 if (IS_GEN(dev_priv, 2))
9091 return false;
9092
9093 if (INTEL_GEN(dev_priv) >= 9 ||
9094 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
dc008bf0 9095 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
60a02311 9096 else
dc008bf0 9097 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
60a02311
VS
9098}
9099
e7fc3f90
MN
9100static void intel_get_transcoder_timings(struct intel_crtc *crtc,
9101 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
9102{
9103 struct drm_device *dev = crtc->base.dev;
fac5e23e 9104 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80 9105 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
ba3f4d0a 9106 u32 tmp;
1bd1bd80 9107
dc008bf0 9108 tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
1326a92c
ML
9109 pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
9110 pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
3c23ed13
VK
9111
9112 if (!transcoder_is_dsi(cpu_transcoder)) {
dc008bf0 9113 tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
1326a92c 9114 pipe_config->hw.adjusted_mode.crtc_hblank_start =
3c23ed13 9115 (tmp & 0xffff) + 1;
1326a92c 9116 pipe_config->hw.adjusted_mode.crtc_hblank_end =
3c23ed13
VK
9117 ((tmp >> 16) & 0xffff) + 1;
9118 }
dc008bf0 9119 tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
1326a92c
ML
9120 pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
9121 pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 9122
dc008bf0 9123 tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
1326a92c
ML
9124 pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
9125 pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
3c23ed13
VK
9126
9127 if (!transcoder_is_dsi(cpu_transcoder)) {
dc008bf0 9128 tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
1326a92c 9129 pipe_config->hw.adjusted_mode.crtc_vblank_start =
3c23ed13 9130 (tmp & 0xffff) + 1;
1326a92c 9131 pipe_config->hw.adjusted_mode.crtc_vblank_end =
3c23ed13
VK
9132 ((tmp >> 16) & 0xffff) + 1;
9133 }
dc008bf0 9134 tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
1326a92c
ML
9135 pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
9136 pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 9137
60a02311 9138 if (intel_pipe_is_interlaced(pipe_config)) {
1326a92c
ML
9139 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
9140 pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
9141 pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 9142 }
bc58be60
JN
9143}
9144
9145static void intel_get_pipe_src_size(struct intel_crtc *crtc,
9146 struct intel_crtc_state *pipe_config)
9147{
9148 struct drm_device *dev = crtc->base.dev;
fac5e23e 9149 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 9150 u32 tmp;
1bd1bd80 9151
dc008bf0 9152 tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
37327abd
VS
9153 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
9154 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
9155}
9156
fdf73510 9157static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
84b046f3 9158{
2225f3c6 9159 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510 9160 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ba3f4d0a 9161 u32 pipeconf;
84b046f3 9162
9f11a9e4 9163 pipeconf = 0;
84b046f3 9164
e56134bc
VS
9165 /* we keep both pipes enabled on 830 */
9166 if (IS_I830(dev_priv))
dc008bf0 9167 pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 9168
fdf73510 9169 if (crtc_state->double_wide)
cf532bb2 9170 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 9171
ff9ce46e 9172 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
9173 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9174 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 9175 /* Bspec claims that we can't use dithering for 30bpp pipes. */
fdf73510 9176 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
ff9ce46e 9177 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 9178 PIPECONF_DITHER_TYPE_SP;
84b046f3 9179
fdf73510 9180 switch (crtc_state->pipe_bpp) {
ff9ce46e
DV
9181 case 18:
9182 pipeconf |= PIPECONF_6BPC;
9183 break;
9184 case 24:
9185 pipeconf |= PIPECONF_8BPC;
9186 break;
9187 case 30:
9188 pipeconf |= PIPECONF_10BPC;
9189 break;
9190 default:
9191 /* Case prevented by intel_choose_pipe_bpp_dither. */
9192 BUG();
84b046f3
DV
9193 }
9194 }
9195
1326a92c 9196 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 9197 if (INTEL_GEN(dev_priv) < 4 ||
fdf73510 9198 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
efc2cfff
VS
9199 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
9200 else
9201 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
27b680f9 9202 } else {
84b046f3 9203 pipeconf |= PIPECONF_PROGRESSIVE;
27b680f9 9204 }
84b046f3 9205
920a14b2 9206 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
fdf73510 9207 crtc_state->limited_color_range)
9f11a9e4 9208 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 9209
9d5441de
VS
9210 pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
9211
cc7a4cff
VS
9212 pipeconf |= PIPECONF_FRAME_START_DELAY(0);
9213
dc008bf0
JN
9214 intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
9215 intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
84b046f3
DV
9216}
9217
81c97f52
ACO
9218static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
9219 struct intel_crtc_state *crtc_state)
9220{
9221 struct drm_device *dev = crtc->base.dev;
fac5e23e 9222 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9223 const struct intel_limit *limit;
81c97f52
ACO
9224 int refclk = 48000;
9225
9226 memset(&crtc_state->dpll_hw_state, 0,
9227 sizeof(crtc_state->dpll_hw_state));
9228
2d84d2b3 9229 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
9230 if (intel_panel_use_ssc(dev_priv)) {
9231 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9232 drm_dbg_kms(&dev_priv->drm,
9233 "using SSC reference clock of %d kHz\n",
9234 refclk);
81c97f52
ACO
9235 }
9236
9237 limit = &intel_limits_i8xx_lvds;
2d84d2b3 9238 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
9239 limit = &intel_limits_i8xx_dvo;
9240 } else {
9241 limit = &intel_limits_i8xx_dac;
9242 }
9243
9244 if (!crtc_state->clock_set &&
9245 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9246 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9247 drm_err(&dev_priv->drm,
9248 "Couldn't find PLL settings for mode!\n");
81c97f52
ACO
9249 return -EINVAL;
9250 }
9251
9252 i8xx_compute_dpll(crtc, crtc_state, NULL);
9253
9254 return 0;
9255}
9256
19ec6693
ACO
9257static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
9258 struct intel_crtc_state *crtc_state)
9259{
d2daff2c 9260 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1b6f4958 9261 const struct intel_limit *limit;
19ec6693
ACO
9262 int refclk = 96000;
9263
9264 memset(&crtc_state->dpll_hw_state, 0,
9265 sizeof(crtc_state->dpll_hw_state));
9266
2d84d2b3 9267 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
9268 if (intel_panel_use_ssc(dev_priv)) {
9269 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9270 drm_dbg_kms(&dev_priv->drm,
9271 "using SSC reference clock of %d kHz\n",
9272 refclk);
19ec6693
ACO
9273 }
9274
d2daff2c 9275 if (intel_is_dual_link_lvds(dev_priv))
19ec6693
ACO
9276 limit = &intel_limits_g4x_dual_channel_lvds;
9277 else
9278 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
9279 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9280 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 9281 limit = &intel_limits_g4x_hdmi;
2d84d2b3 9282 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
9283 limit = &intel_limits_g4x_sdvo;
9284 } else {
9285 /* The option is for other outputs */
9286 limit = &intel_limits_i9xx_sdvo;
9287 }
9288
9289 if (!crtc_state->clock_set &&
9290 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9291 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9292 drm_err(&dev_priv->drm,
9293 "Couldn't find PLL settings for mode!\n");
19ec6693
ACO
9294 return -EINVAL;
9295 }
9296
9297 i9xx_compute_dpll(crtc, crtc_state, NULL);
9298
9299 return 0;
9300}
9301
70e8aa21
ACO
9302static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9303 struct intel_crtc_state *crtc_state)
9304{
9305 struct drm_device *dev = crtc->base.dev;
fac5e23e 9306 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9307 const struct intel_limit *limit;
70e8aa21
ACO
9308 int refclk = 96000;
9309
9310 memset(&crtc_state->dpll_hw_state, 0,
9311 sizeof(crtc_state->dpll_hw_state));
9312
2d84d2b3 9313 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
9314 if (intel_panel_use_ssc(dev_priv)) {
9315 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9316 drm_dbg_kms(&dev_priv->drm,
9317 "using SSC reference clock of %d kHz\n",
9318 refclk);
70e8aa21
ACO
9319 }
9320
1d218220 9321 limit = &pnv_limits_lvds;
70e8aa21 9322 } else {
1d218220 9323 limit = &pnv_limits_sdvo;
70e8aa21
ACO
9324 }
9325
9326 if (!crtc_state->clock_set &&
9327 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9328 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9329 drm_err(&dev_priv->drm,
9330 "Couldn't find PLL settings for mode!\n");
70e8aa21
ACO
9331 return -EINVAL;
9332 }
9333
9334 i9xx_compute_dpll(crtc, crtc_state, NULL);
9335
9336 return 0;
9337}
9338
190f68c5
ACO
9339static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9340 struct intel_crtc_state *crtc_state)
79e53945 9341{
c7653199 9342 struct drm_device *dev = crtc->base.dev;
fac5e23e 9343 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9344 const struct intel_limit *limit;
81c97f52 9345 int refclk = 96000;
79e53945 9346
dd3cd74a
ACO
9347 memset(&crtc_state->dpll_hw_state, 0,
9348 sizeof(crtc_state->dpll_hw_state));
9349
2d84d2b3 9350 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
9351 if (intel_panel_use_ssc(dev_priv)) {
9352 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9353 drm_dbg_kms(&dev_priv->drm,
9354 "using SSC reference clock of %d kHz\n",
9355 refclk);
70e8aa21 9356 }
43565a06 9357
70e8aa21
ACO
9358 limit = &intel_limits_i9xx_lvds;
9359 } else {
9360 limit = &intel_limits_i9xx_sdvo;
81c97f52 9361 }
79e53945 9362
70e8aa21
ACO
9363 if (!crtc_state->clock_set &&
9364 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9365 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9366 drm_err(&dev_priv->drm,
9367 "Couldn't find PLL settings for mode!\n");
70e8aa21 9368 return -EINVAL;
f47709a9 9369 }
7026d4ac 9370
81c97f52 9371 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 9372
c8f7a0db 9373 return 0;
f564048e
EA
9374}
9375
65b3d6a9
ACO
9376static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9377 struct intel_crtc_state *crtc_state)
9378{
9379 int refclk = 100000;
1b6f4958 9380 const struct intel_limit *limit = &intel_limits_chv;
cd49f818 9381 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
65b3d6a9
ACO
9382
9383 memset(&crtc_state->dpll_hw_state, 0,
9384 sizeof(crtc_state->dpll_hw_state));
9385
65b3d6a9
ACO
9386 if (!crtc_state->clock_set &&
9387 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9388 refclk, NULL, &crtc_state->dpll)) {
cd49f818 9389 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
65b3d6a9
ACO
9390 return -EINVAL;
9391 }
9392
9393 chv_compute_dpll(crtc, crtc_state);
9394
9395 return 0;
9396}
9397
9398static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9399 struct intel_crtc_state *crtc_state)
9400{
9401 int refclk = 100000;
1b6f4958 9402 const struct intel_limit *limit = &intel_limits_vlv;
cd49f818 9403 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
65b3d6a9
ACO
9404
9405 memset(&crtc_state->dpll_hw_state, 0,
9406 sizeof(crtc_state->dpll_hw_state));
9407
65b3d6a9
ACO
9408 if (!crtc_state->clock_set &&
9409 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9410 refclk, NULL, &crtc_state->dpll)) {
cd49f818 9411 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
65b3d6a9
ACO
9412 return -EINVAL;
9413 }
9414
9415 vlv_compute_dpll(crtc, crtc_state);
9416
9417 return 0;
9418}
9419
b7c8093f
VS
9420static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9421{
9422 if (IS_I830(dev_priv))
9423 return false;
9424
9425 return INTEL_GEN(dev_priv) >= 4 ||
9426 IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9427}
9428
eac9c585 9429static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
2fa2fe9a 9430{
eac9c585 9431 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6315b5d3 9432 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ba3f4d0a 9433 u32 tmp;
2fa2fe9a 9434
b7c8093f 9435 if (!i9xx_has_pfit(dev_priv))
dc9e7dec
VS
9436 return;
9437
dc008bf0 9438 tmp = intel_de_read(dev_priv, PFIT_CONTROL);
06922821
DV
9439 if (!(tmp & PFIT_ENABLE))
9440 return;
2fa2fe9a 9441
06922821 9442 /* Check whether the pfit is attached to our pipe. */
6315b5d3 9443 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
9444 if (crtc->pipe != PIPE_B)
9445 return;
2fa2fe9a
DV
9446 } else {
9447 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9448 return;
9449 }
9450
eac9c585
VS
9451 crtc_state->gmch_pfit.control = tmp;
9452 crtc_state->gmch_pfit.pgm_ratios =
9453 intel_de_read(dev_priv, PFIT_PGM_RATIOS);
2fa2fe9a
DV
9454}
9455
acbec814 9456static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9457 struct intel_crtc_state *pipe_config)
acbec814
JB
9458{
9459 struct drm_device *dev = crtc->base.dev;
fac5e23e 9460 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 9461 enum pipe pipe = crtc->pipe;
9e2c8475 9462 struct dpll clock;
acbec814 9463 u32 mdiv;
662c6ecb 9464 int refclk = 100000;
acbec814 9465
b521973b
VS
9466 /* In case of DSI, DPLL will not be used */
9467 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
9468 return;
9469
221c7862 9470 vlv_dpio_get(dev_priv);
ab3c759a 9471 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
221c7862 9472 vlv_dpio_put(dev_priv);
acbec814
JB
9473
9474 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9475 clock.m2 = mdiv & DPIO_M2DIV_MASK;
9476 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9477 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9478 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9479
dccbea3b 9480 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
9481}
9482
5724dbd1
DL
9483static void
9484i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9485 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
9486{
9487 struct drm_device *dev = crtc->base.dev;
fac5e23e 9488 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
9489 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9490 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
eade6c89 9491 enum pipe pipe;
1ad292b5 9492 u32 val, base, offset;
1ad292b5 9493 int fourcc, pixel_format;
6761dd31 9494 unsigned int aligned_height;
b113d5ee 9495 struct drm_framebuffer *fb;
1b842c89 9496 struct intel_framebuffer *intel_fb;
1ad292b5 9497
eade6c89 9498 if (!plane->get_hw_state(plane, &pipe))
42a7b088
DL
9499 return;
9500
e57291c2 9501 drm_WARN_ON(dev, pipe != crtc->pipe);
eade6c89 9502
d9806c9f 9503 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9504 if (!intel_fb) {
cd49f818 9505 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
1ad292b5
JB
9506 return;
9507 }
9508
1b842c89
DL
9509 fb = &intel_fb->base;
9510
d2e9f5fc
VS
9511 fb->dev = dev;
9512
dc008bf0 9513 val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
2924b8cc 9514
6315b5d3 9515 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 9516 if (val & DISPPLANE_TILED) {
49af449b 9517 plane_config->tiling = I915_TILING_X;
bae781b2 9518 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e 9519 }
f43348a3
VS
9520
9521 if (val & DISPPLANE_ROTATE_180)
9522 plane_config->rotation = DRM_MODE_ROTATE_180;
18c5247e 9523 }
1ad292b5 9524
f43348a3
VS
9525 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9526 val & DISPPLANE_MIRROR)
9527 plane_config->rotation |= DRM_MODE_REFLECT_X;
9528
1ad292b5 9529 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9530 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 9531 fb->format = drm_format_info(fourcc);
1ad292b5 9532
81894b2f 9533 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
dc008bf0
JN
9534 offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9535 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
81894b2f 9536 } else if (INTEL_GEN(dev_priv) >= 4) {
49af449b 9537 if (plane_config->tiling)
dc008bf0
JN
9538 offset = intel_de_read(dev_priv,
9539 DSPTILEOFF(i9xx_plane));
1ad292b5 9540 else
dc008bf0
JN
9541 offset = intel_de_read(dev_priv,
9542 DSPLINOFF(i9xx_plane));
9543 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
1ad292b5 9544 } else {
dc008bf0 9545 base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
1ad292b5
JB
9546 }
9547 plane_config->base = base;
9548
dc008bf0 9549 val = intel_de_read(dev_priv, PIPESRC(pipe));
b113d5ee
DL
9550 fb->width = ((val >> 16) & 0xfff) + 1;
9551 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5 9552
dc008bf0 9553 val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
b113d5ee 9554 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 9555
d88c4afd 9556 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 9557
f37b5c2b 9558 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 9559
cd49f818
WK
9560 drm_dbg_kms(&dev_priv->drm,
9561 "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9562 crtc->base.name, plane->base.name, fb->width, fb->height,
9563 fb->format->cpp[0] * 8, base, fb->pitches[0],
9564 plane_config->size);
1ad292b5 9565
2d14030b 9566 plane_config->fb = intel_fb;
1ad292b5
JB
9567}
9568
70b23a98 9569static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9570 struct intel_crtc_state *pipe_config)
70b23a98
VS
9571{
9572 struct drm_device *dev = crtc->base.dev;
fac5e23e 9573 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 9574 enum pipe pipe = crtc->pipe;
70b23a98 9575 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 9576 struct dpll clock;
0d7b6b11 9577 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
9578 int refclk = 100000;
9579
b521973b
VS
9580 /* In case of DSI, DPLL will not be used */
9581 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9582 return;
9583
221c7862 9584 vlv_dpio_get(dev_priv);
70b23a98
VS
9585 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9586 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9587 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9588 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 9589 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
221c7862 9590 vlv_dpio_put(dev_priv);
70b23a98
VS
9591
9592 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
9593 clock.m2 = (pll_dw0 & 0xff) << 22;
9594 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9595 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
9596 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9597 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9598 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9599
dccbea3b 9600 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
9601}
9602
b10d1173
VS
9603static enum intel_output_format
9604bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
33b7f3ee
SS
9605{
9606 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b10d1173
VS
9607 u32 tmp;
9608
dc008bf0 9609 tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
b10d1173
VS
9610
9611 if (tmp & PIPEMISC_YUV420_ENABLE) {
9612 /* We support 4:2:0 in full blend mode only */
e57291c2
PB
9613 drm_WARN_ON(&dev_priv->drm,
9614 (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
33b7f3ee 9615
b10d1173
VS
9616 return INTEL_OUTPUT_FORMAT_YCBCR420;
9617 } else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9618 return INTEL_OUTPUT_FORMAT_YCBCR444;
9619 } else {
9620 return INTEL_OUTPUT_FORMAT_RGB;
9621 }
33b7f3ee
SS
9622}
9623
5f29ab23
VS
9624static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9625{
2225f3c6 9626 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5f29ab23
VS
9627 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9628 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9629 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9630 u32 tmp;
9631
dc008bf0 9632 tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
5f29ab23
VS
9633
9634 if (tmp & DISPPLANE_GAMMA_ENABLE)
9635 crtc_state->gamma_enable = true;
8271b2ef
VS
9636
9637 if (!HAS_GMCH(dev_priv) &&
9638 tmp & DISPPLANE_PIPE_CSC_ENABLE)
9639 crtc_state->csc_enable = true;
5f29ab23
VS
9640}
9641
0e8ffe1b 9642static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9643 struct intel_crtc_state *pipe_config)
0e8ffe1b 9644{
6315b5d3 9645 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9646 enum intel_display_power_domain power_domain;
0e6e0be4 9647 intel_wakeref_t wakeref;
ba3f4d0a 9648 u32 tmp;
1729050e 9649 bool ret;
0e8ffe1b 9650
1729050e 9651 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
0e6e0be4
CW
9652 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9653 if (!wakeref)
b5482bd0
ID
9654 return false;
9655
d9facae6 9656 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
e143a21c 9657 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9658 pipe_config->shared_dpll = NULL;
eccb140b 9659
1729050e
ID
9660 ret = false;
9661
dc008bf0 9662 tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
0e8ffe1b 9663 if (!(tmp & PIPECONF_ENABLE))
1729050e 9664 goto out;
0e8ffe1b 9665
9beb5fea
TU
9666 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9667 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
9668 switch (tmp & PIPECONF_BPC_MASK) {
9669 case PIPECONF_6BPC:
9670 pipe_config->pipe_bpp = 18;
9671 break;
9672 case PIPECONF_8BPC:
9673 pipe_config->pipe_bpp = 24;
9674 break;
9675 case PIPECONF_10BPC:
9676 pipe_config->pipe_bpp = 30;
9677 break;
9678 default:
9679 break;
9680 }
9681 }
9682
920a14b2 9683 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 9684 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
9685 pipe_config->limited_color_range = true;
9686
9d5441de
VS
9687 pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9688 PIPECONF_GAMMA_MODE_SHIFT;
9689
9fdfb8e7 9690 if (IS_CHERRYVIEW(dev_priv))
dc008bf0
JN
9691 pipe_config->cgm_mode = intel_de_read(dev_priv,
9692 CGM_PIPE_MODE(crtc->pipe));
9fdfb8e7 9693
5f29ab23 9694 i9xx_get_pipe_color_config(pipe_config);
3633e511 9695 intel_color_get_config(pipe_config);
5f29ab23 9696
6315b5d3 9697 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
9698 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9699
e7fc3f90 9700 intel_get_transcoder_timings(crtc, pipe_config);
bc58be60 9701 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9702
eac9c585 9703 i9xx_get_pfit_config(pipe_config);
2fa2fe9a 9704
6315b5d3 9705 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 9706 /* No way to read it out on pipes B and C */
920a14b2 9707 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
9708 tmp = dev_priv->chv_dpll_md[crtc->pipe];
9709 else
dc008bf0 9710 tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
6c49f241
DV
9711 pipe_config->pixel_multiplier =
9712 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9713 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 9714 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 9715 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 9716 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
dc008bf0 9717 tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
6c49f241
DV
9718 pipe_config->pixel_multiplier =
9719 ((tmp & SDVO_MULTIPLIER_MASK)
9720 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9721 } else {
9722 /* Note that on i915G/GM the pixel multiplier is in the sdvo
9723 * port and will be fixed up in the encoder->get_config
9724 * function. */
9725 pipe_config->pixel_multiplier = 1;
9726 }
dc008bf0
JN
9727 pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9728 DPLL(crtc->pipe));
920a14b2 9729 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
dc008bf0
JN
9730 pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9731 FP0(crtc->pipe));
9732 pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9733 FP1(crtc->pipe));
165e901c
VS
9734 } else {
9735 /* Mask out read-only status bits. */
9736 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9737 DPLL_PORTC_READY_MASK |
9738 DPLL_PORTB_READY_MASK);
8bcc2795 9739 }
6c49f241 9740
920a14b2 9741 if (IS_CHERRYVIEW(dev_priv))
70b23a98 9742 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 9743 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
9744 vlv_crtc_clock_get(crtc, pipe_config);
9745 else
9746 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 9747
0f64614d
VS
9748 /*
9749 * Normally the dotclock is filled in by the encoder .get_config()
9750 * but in case the pipe is enabled w/o any ports we need a sane
9751 * default.
9752 */
1326a92c 9753 pipe_config->hw.adjusted_mode.crtc_clock =
0f64614d
VS
9754 pipe_config->port_clock / pipe_config->pixel_multiplier;
9755
1729050e
ID
9756 ret = true;
9757
9758out:
0e6e0be4 9759 intel_display_power_put(dev_priv, power_domain, wakeref);
1729050e
ID
9760
9761 return ret;
0e8ffe1b
DV
9762}
9763
9eae5e27 9764static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 9765{
13d83a67 9766 struct intel_encoder *encoder;
1c1a24d2 9767 int i;
74cfd7ac 9768 u32 val, final;
13d83a67 9769 bool has_lvds = false;
199e5d79 9770 bool has_cpu_edp = false;
199e5d79 9771 bool has_panel = false;
99eb6a01
KP
9772 bool has_ck505 = false;
9773 bool can_ssc = false;
1c1a24d2 9774 bool using_ssc_source = false;
13d83a67
JB
9775
9776 /* We need to take the global config into account */
c39055b0 9777 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
9778 switch (encoder->type) {
9779 case INTEL_OUTPUT_LVDS:
9780 has_panel = true;
9781 has_lvds = true;
9782 break;
9783 case INTEL_OUTPUT_EDP:
9784 has_panel = true;
8f4f2797 9785 if (encoder->port == PORT_A)
199e5d79
KP
9786 has_cpu_edp = true;
9787 break;
6847d71b
PZ
9788 default:
9789 break;
13d83a67
JB
9790 }
9791 }
9792
6e266956 9793 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 9794 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
9795 can_ssc = has_ck505;
9796 } else {
9797 has_ck505 = false;
9798 can_ssc = true;
9799 }
9800
1c1a24d2 9801 /* Check if any DPLLs are using the SSC source */
353ad959 9802 for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
dc008bf0 9803 u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
1c1a24d2
L
9804
9805 if (!(temp & DPLL_VCO_ENABLE))
9806 continue;
9807
9808 if ((temp & PLL_REF_INPUT_MASK) ==
9809 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9810 using_ssc_source = true;
9811 break;
9812 }
9813 }
9814
cd49f818
WK
9815 drm_dbg_kms(&dev_priv->drm,
9816 "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9817 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
9818
9819 /* Ironlake: try to setup display ref clock before DPLL
9820 * enabling. This is only under driver's control after
9821 * PCH B stepping, previous chipset stepping should be
9822 * ignoring this setting.
9823 */
dc008bf0 9824 val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
74cfd7ac
CW
9825
9826 /* As we must carefully and slowly disable/enable each source in turn,
9827 * compute the final state we want first and check if we need to
9828 * make any changes at all.
9829 */
9830 final = val;
9831 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9832 if (has_ck505)
9833 final |= DREF_NONSPREAD_CK505_ENABLE;
9834 else
9835 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9836
8c07eb68 9837 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9838 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9839 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9840
9841 if (has_panel) {
9842 final |= DREF_SSC_SOURCE_ENABLE;
9843
9844 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9845 final |= DREF_SSC1_ENABLE;
9846
9847 if (has_cpu_edp) {
9848 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9849 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9850 else
9851 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9852 } else
9853 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9854 } else if (using_ssc_source) {
9855 final |= DREF_SSC_SOURCE_ENABLE;
9856 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9857 }
9858
9859 if (final == val)
9860 return;
9861
13d83a67 9862 /* Always enable nonspread source */
74cfd7ac 9863 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9864
99eb6a01 9865 if (has_ck505)
74cfd7ac 9866 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9867 else
74cfd7ac 9868 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9869
199e5d79 9870 if (has_panel) {
74cfd7ac
CW
9871 val &= ~DREF_SSC_SOURCE_MASK;
9872 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9873
199e5d79 9874 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9875 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
cd49f818 9876 drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
74cfd7ac 9877 val |= DREF_SSC1_ENABLE;
e77166b5 9878 } else
74cfd7ac 9879 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9880
9881 /* Get SSC going before enabling the outputs */
dc008bf0
JN
9882 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9883 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9884 udelay(200);
9885
74cfd7ac 9886 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9887
9888 /* Enable CPU source on CPU attached eDP */
199e5d79 9889 if (has_cpu_edp) {
99eb6a01 9890 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
cd49f818
WK
9891 drm_dbg_kms(&dev_priv->drm,
9892 "Using SSC on eDP\n");
74cfd7ac 9893 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9894 } else
74cfd7ac 9895 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9896 } else
74cfd7ac 9897 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9898
dc008bf0
JN
9899 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9900 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9901 udelay(200);
9902 } else {
cd49f818 9903 drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
199e5d79 9904
74cfd7ac 9905 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9906
9907 /* Turn off CPU output */
74cfd7ac 9908 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9909
dc008bf0
JN
9910 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9911 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9912 udelay(200);
9913
1c1a24d2 9914 if (!using_ssc_source) {
cd49f818 9915 drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
199e5d79 9916
1c1a24d2
L
9917 /* Turn off the SSC source */
9918 val &= ~DREF_SSC_SOURCE_MASK;
9919 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9920
1c1a24d2
L
9921 /* Turn off SSC1 */
9922 val &= ~DREF_SSC1_ENABLE;
9923
dc008bf0
JN
9924 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9925 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
1c1a24d2
L
9926 udelay(200);
9927 }
13d83a67 9928 }
74cfd7ac
CW
9929
9930 BUG_ON(val != final);
13d83a67
JB
9931}
9932
f31f2d55 9933static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9934{
ba3f4d0a 9935 u32 tmp;
dde86e2d 9936
dc008bf0 9937 tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
0ff066a9 9938 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
dc008bf0 9939 intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
dde86e2d 9940
dc008bf0 9941 if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
cf3598c2 9942 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
cd49f818 9943 drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
dde86e2d 9944
dc008bf0 9945 tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
0ff066a9 9946 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
dc008bf0 9947 intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
dde86e2d 9948
dc008bf0 9949 if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
cf3598c2 9950 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
cd49f818 9951 drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9952}
9953
9954/* WaMPhyProgramming:hsw */
9955static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9956{
ba3f4d0a 9957 u32 tmp;
dde86e2d
PZ
9958
9959 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9960 tmp &= ~(0xFF << 24);
9961 tmp |= (0x12 << 24);
9962 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9963
dde86e2d
PZ
9964 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9965 tmp |= (1 << 11);
9966 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9967
9968 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9969 tmp |= (1 << 11);
9970 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9971
dde86e2d
PZ
9972 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9973 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9974 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9975
9976 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9977 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9978 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9979
0ff066a9
PZ
9980 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9981 tmp &= ~(7 << 13);
9982 tmp |= (5 << 13);
9983 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9984
0ff066a9
PZ
9985 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9986 tmp &= ~(7 << 13);
9987 tmp |= (5 << 13);
9988 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9989
9990 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9991 tmp &= ~0xFF;
9992 tmp |= 0x1C;
9993 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9994
9995 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9996 tmp &= ~0xFF;
9997 tmp |= 0x1C;
9998 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9999
10000 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
10001 tmp &= ~(0xFF << 16);
10002 tmp |= (0x1C << 16);
10003 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
10004
10005 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
10006 tmp &= ~(0xFF << 16);
10007 tmp |= (0x1C << 16);
10008 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
10009
0ff066a9
PZ
10010 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
10011 tmp |= (1 << 27);
10012 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 10013
0ff066a9
PZ
10014 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
10015 tmp |= (1 << 27);
10016 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 10017
0ff066a9
PZ
10018 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
10019 tmp &= ~(0xF << 28);
10020 tmp |= (4 << 28);
10021 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 10022
0ff066a9
PZ
10023 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
10024 tmp &= ~(0xF << 28);
10025 tmp |= (4 << 28);
10026 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
10027}
10028
2fa86a1f
PZ
10029/* Implements 3 different sequences from BSpec chapter "Display iCLK
10030 * Programming" based on the parameters passed:
10031 * - Sequence to enable CLKOUT_DP
10032 * - Sequence to enable CLKOUT_DP without spread
10033 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
10034 */
c39055b0
ACO
10035static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
10036 bool with_spread, bool with_fdi)
f31f2d55 10037{
ba3f4d0a 10038 u32 reg, tmp;
2fa86a1f 10039
e57291c2
PB
10040 if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
10041 "FDI requires downspread\n"))
2fa86a1f 10042 with_spread = true;
e57291c2
PB
10043 if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
10044 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 10045 with_fdi = false;
f31f2d55 10046
a580516d 10047 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
10048
10049 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10050 tmp &= ~SBI_SSCCTL_DISABLE;
10051 tmp |= SBI_SSCCTL_PATHALT;
10052 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10053
10054 udelay(24);
10055
2fa86a1f
PZ
10056 if (with_spread) {
10057 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10058 tmp &= ~SBI_SSCCTL_PATHALT;
10059 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 10060
2fa86a1f
PZ
10061 if (with_fdi) {
10062 lpt_reset_fdi_mphy(dev_priv);
10063 lpt_program_fdi_mphy(dev_priv);
10064 }
10065 }
dde86e2d 10066
4f8036a2 10067 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
10068 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10069 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10070 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 10071
a580516d 10072 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
10073}
10074
47701c3b 10075/* Sequence to disable CLKOUT_DP */
46034d2b 10076void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 10077{
ba3f4d0a 10078 u32 reg, tmp;
47701c3b 10079
a580516d 10080 mutex_lock(&dev_priv->sb_lock);
47701c3b 10081
4f8036a2 10082 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
10083 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10084 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10085 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
10086
10087 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10088 if (!(tmp & SBI_SSCCTL_DISABLE)) {
10089 if (!(tmp & SBI_SSCCTL_PATHALT)) {
10090 tmp |= SBI_SSCCTL_PATHALT;
10091 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10092 udelay(32);
10093 }
10094 tmp |= SBI_SSCCTL_DISABLE;
10095 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10096 }
10097
a580516d 10098 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
10099}
10100
f7be2c21
VS
10101#define BEND_IDX(steps) ((50 + (steps)) / 5)
10102
ba3f4d0a 10103static const u16 sscdivintphase[] = {
f7be2c21
VS
10104 [BEND_IDX( 50)] = 0x3B23,
10105 [BEND_IDX( 45)] = 0x3B23,
10106 [BEND_IDX( 40)] = 0x3C23,
10107 [BEND_IDX( 35)] = 0x3C23,
10108 [BEND_IDX( 30)] = 0x3D23,
10109 [BEND_IDX( 25)] = 0x3D23,
10110 [BEND_IDX( 20)] = 0x3E23,
10111 [BEND_IDX( 15)] = 0x3E23,
10112 [BEND_IDX( 10)] = 0x3F23,
10113 [BEND_IDX( 5)] = 0x3F23,
10114 [BEND_IDX( 0)] = 0x0025,
10115 [BEND_IDX( -5)] = 0x0025,
10116 [BEND_IDX(-10)] = 0x0125,
10117 [BEND_IDX(-15)] = 0x0125,
10118 [BEND_IDX(-20)] = 0x0225,
10119 [BEND_IDX(-25)] = 0x0225,
10120 [BEND_IDX(-30)] = 0x0325,
10121 [BEND_IDX(-35)] = 0x0325,
10122 [BEND_IDX(-40)] = 0x0425,
10123 [BEND_IDX(-45)] = 0x0425,
10124 [BEND_IDX(-50)] = 0x0525,
10125};
10126
10127/*
10128 * Bend CLKOUT_DP
10129 * steps -50 to 50 inclusive, in steps of 5
10130 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
10131 * change in clock period = -(steps / 10) * 5.787 ps
10132 */
10133static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
10134{
ba3f4d0a 10135 u32 tmp;
f7be2c21
VS
10136 int idx = BEND_IDX(steps);
10137
e57291c2 10138 if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
f7be2c21
VS
10139 return;
10140
e57291c2 10141 if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
f7be2c21
VS
10142 return;
10143
10144 mutex_lock(&dev_priv->sb_lock);
10145
10146 if (steps % 10 != 0)
10147 tmp = 0xAAAAAAAB;
10148 else
10149 tmp = 0x00000000;
10150 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
10151
10152 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
10153 tmp &= 0xffff0000;
10154 tmp |= sscdivintphase[idx];
10155 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
10156
10157 mutex_unlock(&dev_priv->sb_lock);
10158}
10159
10160#undef BEND_IDX
10161
b16c7ed9
VS
10162static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
10163{
dc008bf0
JN
10164 u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10165 u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
b16c7ed9
VS
10166
10167 if ((ctl & SPLL_PLL_ENABLE) == 0)
10168 return false;
10169
4a95e36f 10170 if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
b16c7ed9
VS
10171 (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10172 return true;
10173
10174 if (IS_BROADWELL(dev_priv) &&
4a95e36f 10175 (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
b16c7ed9
VS
10176 return true;
10177
10178 return false;
10179}
10180
10181static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
10182 enum intel_dpll_id id)
10183{
dc008bf0
JN
10184 u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10185 u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
b16c7ed9
VS
10186
10187 if ((ctl & WRPLL_PLL_ENABLE) == 0)
10188 return false;
10189
4a95e36f 10190 if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
b16c7ed9
VS
10191 return true;
10192
10193 if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
4a95e36f 10194 (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
b16c7ed9
VS
10195 (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10196 return true;
10197
10198 return false;
10199}
10200
c39055b0 10201static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 10202{
bf8fa3d3 10203 struct intel_encoder *encoder;
b16c7ed9 10204 bool has_fdi = false;
bf8fa3d3 10205
c39055b0 10206 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
10207 switch (encoder->type) {
10208 case INTEL_OUTPUT_ANALOG:
b16c7ed9 10209 has_fdi = true;
bf8fa3d3 10210 break;
6847d71b
PZ
10211 default:
10212 break;
bf8fa3d3
PZ
10213 }
10214 }
10215
b16c7ed9
VS
10216 /*
10217 * The BIOS may have decided to use the PCH SSC
10218 * reference so we must not disable it until the
10219 * relevant PLLs have stopped relying on it. We'll
10220 * just leave the PCH SSC reference enabled in case
10221 * any active PLL is using it. It will get disabled
10222 * after runtime suspend if we don't have FDI.
10223 *
10224 * TODO: Move the whole reference clock handling
10225 * to the modeset sequence proper so that we can
10226 * actually enable/disable/reconfigure these things
10227 * safely. To do that we need to introduce a real
10228 * clock hierarchy. That would also allow us to do
10229 * clock bending finally.
10230 */
dd5279c7
VS
10231 dev_priv->pch_ssc_use = 0;
10232
b16c7ed9 10233 if (spll_uses_pch_ssc(dev_priv)) {
cd49f818 10234 drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
dd5279c7 10235 dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
b16c7ed9
VS
10236 }
10237
10238 if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
cd49f818 10239 drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
dd5279c7 10240 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
b16c7ed9
VS
10241 }
10242
10243 if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
cd49f818 10244 drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
dd5279c7 10245 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
b16c7ed9
VS
10246 }
10247
dd5279c7 10248 if (dev_priv->pch_ssc_use)
b16c7ed9
VS
10249 return;
10250
10251 if (has_fdi) {
c39055b0
ACO
10252 lpt_bend_clkout_dp(dev_priv, 0);
10253 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 10254 } else {
c39055b0 10255 lpt_disable_clkout_dp(dev_priv);
f7be2c21 10256 }
bf8fa3d3
PZ
10257}
10258
dde86e2d
PZ
10259/*
10260 * Initialize reference clocks when the driver loads
10261 */
c39055b0 10262void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 10263{
6e266956 10264 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
9eae5e27 10265 ilk_init_pch_refclk(dev_priv);
6e266956 10266 else if (HAS_PCH_LPT(dev_priv))
c39055b0 10267 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
10268}
10269
9eae5e27 10270static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
79e53945 10271{
2225f3c6 10272 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510
ML
10273 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10274 enum pipe pipe = crtc->pipe;
ba3f4d0a 10275 u32 val;
c8203565 10276
78114071 10277 val = 0;
c8203565 10278
fdf73510 10279 switch (crtc_state->pipe_bpp) {
c8203565 10280 case 18:
dfd07d72 10281 val |= PIPECONF_6BPC;
c8203565
PZ
10282 break;
10283 case 24:
dfd07d72 10284 val |= PIPECONF_8BPC;
c8203565
PZ
10285 break;
10286 case 30:
dfd07d72 10287 val |= PIPECONF_10BPC;
c8203565
PZ
10288 break;
10289 case 36:
dfd07d72 10290 val |= PIPECONF_12BPC;
c8203565
PZ
10291 break;
10292 default:
cc769b62
PZ
10293 /* Case prevented by intel_choose_pipe_bpp_dither. */
10294 BUG();
c8203565
PZ
10295 }
10296
fdf73510 10297 if (crtc_state->dither)
c8203565
PZ
10298 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10299
1326a92c 10300 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
10301 val |= PIPECONF_INTERLACED_ILK;
10302 else
10303 val |= PIPECONF_PROGRESSIVE;
10304
d1844606
VS
10305 /*
10306 * This would end up with an odd purple hue over
10307 * the entire display. Make sure we don't do it.
10308 */
e57291c2
PB
10309 drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10310 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
d1844606 10311
90f8ed85
VS
10312 if (crtc_state->limited_color_range &&
10313 !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3685a8f3 10314 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 10315
d1844606
VS
10316 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10317 val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10318
9d5441de
VS
10319 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10320
cc7a4cff
VS
10321 val |= PIPECONF_FRAME_START_DELAY(0);
10322
dc008bf0
JN
10323 intel_de_write(dev_priv, PIPECONF(pipe), val);
10324 intel_de_posting_read(dev_priv, PIPECONF(pipe));
c8203565
PZ
10325}
10326
1e98f88c 10327static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
ee2b0b38 10328{
2225f3c6 10329 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510
ML
10330 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10331 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
391bf048 10332 u32 val = 0;
ee2b0b38 10333
fdf73510 10334 if (IS_HASWELL(dev_priv) && crtc_state->dither)
ee2b0b38
PZ
10335 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10336
1326a92c 10337 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
10338 val |= PIPECONF_INTERLACED_ILK;
10339 else
10340 val |= PIPECONF_PROGRESSIVE;
10341
ac0f01ce
VS
10342 if (IS_HASWELL(dev_priv) &&
10343 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10344 val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10345
dc008bf0
JN
10346 intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10347 intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
391bf048
JN
10348}
10349
9b11215e 10350static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
391bf048 10351{
2225f3c6 10352 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9b11215e
VS
10353 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10354 u32 val = 0;
756f85cf 10355
9b11215e
VS
10356 switch (crtc_state->pipe_bpp) {
10357 case 18:
10358 val |= PIPEMISC_DITHER_6_BPC;
10359 break;
10360 case 24:
10361 val |= PIPEMISC_DITHER_8_BPC;
10362 break;
10363 case 30:
10364 val |= PIPEMISC_DITHER_10_BPC;
10365 break;
10366 case 36:
10367 val |= PIPEMISC_DITHER_12_BPC;
10368 break;
10369 default:
10370 MISSING_CASE(crtc_state->pipe_bpp);
10371 break;
10372 }
756f85cf 10373
9b11215e
VS
10374 if (crtc_state->dither)
10375 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
756f85cf 10376
9b11215e
VS
10377 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10378 crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10379 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
8c79f844 10380
9b11215e
VS
10381 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10382 val |= PIPEMISC_YUV420_ENABLE |
10383 PIPEMISC_YUV420_MODE_FULL_BLEND;
b22ca995 10384
09b25812 10385 if (INTEL_GEN(dev_priv) >= 11 &&
b7ffc4a8
VS
10386 (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10387 BIT(PLANE_CURSOR))) == 0)
09b25812
VS
10388 val |= PIPEMISC_HDR_MODE_PRECISION;
10389
041be481
VS
10390 if (INTEL_GEN(dev_priv) >= 12)
10391 val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10392
dc008bf0 10393 intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
ee2b0b38
PZ
10394}
10395
8ae89743
VK
10396int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10397{
10398 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10399 u32 tmp;
10400
dc008bf0 10401 tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
8ae89743
VK
10402
10403 switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10404 case PIPEMISC_DITHER_6_BPC:
10405 return 18;
10406 case PIPEMISC_DITHER_8_BPC:
10407 return 24;
10408 case PIPEMISC_DITHER_10_BPC:
10409 return 30;
10410 case PIPEMISC_DITHER_12_BPC:
10411 return 36;
10412 default:
10413 MISSING_CASE(tmp);
10414 return 0;
10415 }
10416}
10417
9eae5e27 10418int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
d4b1931c
PZ
10419{
10420 /*
10421 * Account for spread spectrum to avoid
10422 * oversubscribing the link. Max center spread
10423 * is 2.5%; use 5% for safety's sake.
10424 */
10425 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 10426 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
10427}
10428
9eae5e27 10429static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 10430{
7429e9d4 10431 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
10432}
10433
9eae5e27
LDM
10434static void ilk_compute_dpll(struct intel_crtc *crtc,
10435 struct intel_crtc_state *crtc_state,
10436 struct dpll *reduced_clock)
79e53945 10437{
d2daff2c 10438 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b75ca6f6 10439 u32 dpll, fp, fp2;
3d6e9ee0 10440 int factor;
79e53945 10441
c1858123 10442 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 10443 factor = 21;
3d6e9ee0 10444 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 10445 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 10446 dev_priv->vbt.lvds_ssc_freq == 100000) ||
d2daff2c
VS
10447 (HAS_PCH_IBX(dev_priv) &&
10448 intel_is_dual_link_lvds(dev_priv)))
8febb297 10449 factor = 25;
27b680f9 10450 } else if (crtc_state->sdvo_tv_clock) {
8febb297 10451 factor = 20;
27b680f9 10452 }
c1858123 10453
b75ca6f6
ACO
10454 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10455
9eae5e27 10456 if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
10457 fp |= FP_CB_TUNE;
10458
10459 if (reduced_clock) {
10460 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 10461
b75ca6f6
ACO
10462 if (reduced_clock->m < factor * reduced_clock->n)
10463 fp2 |= FP_CB_TUNE;
10464 } else {
10465 fp2 = fp;
10466 }
9a7c7890 10467
5eddb70b 10468 dpll = 0;
2c07245f 10469
3d6e9ee0 10470 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
10471 dpll |= DPLLB_MODE_LVDS;
10472 else
10473 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 10474
190f68c5 10475 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 10476 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 10477
3d6e9ee0
VS
10478 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10479 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 10480 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 10481
37a5650b 10482 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 10483 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 10484
7d7f8633
VS
10485 /*
10486 * The high speed IO clock is only really required for
10487 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10488 * possible to share the DPLL between CRT and HDMI. Enabling
10489 * the clock needlessly does no real harm, except use up a
10490 * bit of power potentially.
10491 *
10492 * We'll limit this to IVB with 3 pipes, since it has only two
10493 * DPLLs and so DPLL sharing is the only way to get three pipes
10494 * driving PCH ports at the same time. On SNB we could do this,
10495 * and potentially avoid enabling the second DPLL, but it's not
10496 * clear if it''s a win or loss power wise. No point in doing
10497 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10498 */
24977870 10499 if (INTEL_NUM_PIPES(dev_priv) == 3 &&
7d7f8633
VS
10500 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10501 dpll |= DPLL_SDVO_HIGH_SPEED;
10502
a07d6787 10503 /* compute bitmask from p1 value */
190f68c5 10504 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 10505 /* also FPA1 */
190f68c5 10506 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 10507
190f68c5 10508 switch (crtc_state->dpll.p2) {
a07d6787
EA
10509 case 5:
10510 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10511 break;
10512 case 7:
10513 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10514 break;
10515 case 10:
10516 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10517 break;
10518 case 14:
10519 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10520 break;
79e53945
JB
10521 }
10522
3d6e9ee0
VS
10523 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10524 intel_panel_use_ssc(dev_priv))
43565a06 10525 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
10526 else
10527 dpll |= PLL_REF_INPUT_DREFCLK;
10528
b75ca6f6
ACO
10529 dpll |= DPLL_VCO_ENABLE;
10530
10531 crtc_state->dpll_hw_state.dpll = dpll;
10532 crtc_state->dpll_hw_state.fp0 = fp;
10533 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
10534}
10535
9eae5e27
LDM
10536static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10537 struct intel_crtc_state *crtc_state)
de13a2e3 10538{
d2daff2c 10539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
866955fa 10540 struct intel_atomic_state *state =
2225f3c6 10541 to_intel_atomic_state(crtc_state->uapi.state);
1b6f4958 10542 const struct intel_limit *limit;
997c030c 10543 int refclk = 120000;
de13a2e3 10544
dd3cd74a
ACO
10545 memset(&crtc_state->dpll_hw_state, 0,
10546 sizeof(crtc_state->dpll_hw_state));
10547
ded220e2
ACO
10548 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10549 if (!crtc_state->has_pch_encoder)
10550 return 0;
79e53945 10551
2d84d2b3 10552 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c 10553 if (intel_panel_use_ssc(dev_priv)) {
cd49f818
WK
10554 drm_dbg_kms(&dev_priv->drm,
10555 "using SSC reference clock of %d kHz\n",
10556 dev_priv->vbt.lvds_ssc_freq);
997c030c
ACO
10557 refclk = dev_priv->vbt.lvds_ssc_freq;
10558 }
10559
d2daff2c 10560 if (intel_is_dual_link_lvds(dev_priv)) {
997c030c 10561 if (refclk == 100000)
9eae5e27 10562 limit = &ilk_limits_dual_lvds_100m;
997c030c 10563 else
9eae5e27 10564 limit = &ilk_limits_dual_lvds;
997c030c
ACO
10565 } else {
10566 if (refclk == 100000)
9eae5e27 10567 limit = &ilk_limits_single_lvds_100m;
997c030c 10568 else
9eae5e27 10569 limit = &ilk_limits_single_lvds;
997c030c
ACO
10570 }
10571 } else {
9eae5e27 10572 limit = &ilk_limits_dac;
997c030c
ACO
10573 }
10574
364ee29d 10575 if (!crtc_state->clock_set &&
997c030c
ACO
10576 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10577 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
10578 drm_err(&dev_priv->drm,
10579 "Couldn't find PLL settings for mode!\n");
364ee29d 10580 return -EINVAL;
f47709a9 10581 }
79e53945 10582
9eae5e27 10583 ilk_compute_dpll(crtc, crtc_state, NULL);
66e985c0 10584
866955fa 10585 if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
cd49f818
WK
10586 drm_dbg_kms(&dev_priv->drm,
10587 "failed to find PLL for pipe %c\n",
10588 pipe_name(crtc->pipe));
ded220e2 10589 return -EINVAL;
3fb37703 10590 }
79e53945 10591
c8f7a0db 10592 return 0;
79e53945
JB
10593}
10594
eb14cb74
VS
10595static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10596 struct intel_link_m_n *m_n)
10597{
10598 struct drm_device *dev = crtc->base.dev;
fac5e23e 10599 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
10600 enum pipe pipe = crtc->pipe;
10601
dc008bf0
JN
10602 m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10603 m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10604 m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
eb14cb74 10605 & ~TU_SIZE_MASK;
dc008bf0
JN
10606 m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10607 m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
eb14cb74
VS
10608 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10609}
10610
10611static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10612 enum transcoder transcoder,
b95af8be
VK
10613 struct intel_link_m_n *m_n,
10614 struct intel_link_m_n *m2_n2)
72419203 10615{
6315b5d3 10616 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 10617 enum pipe pipe = crtc->pipe;
72419203 10618
6315b5d3 10619 if (INTEL_GEN(dev_priv) >= 5) {
dc008bf0
JN
10620 m_n->link_m = intel_de_read(dev_priv,
10621 PIPE_LINK_M1(transcoder));
10622 m_n->link_n = intel_de_read(dev_priv,
10623 PIPE_LINK_N1(transcoder));
10624 m_n->gmch_m = intel_de_read(dev_priv,
10625 PIPE_DATA_M1(transcoder))
eb14cb74 10626 & ~TU_SIZE_MASK;
dc008bf0
JN
10627 m_n->gmch_n = intel_de_read(dev_priv,
10628 PIPE_DATA_N1(transcoder));
10629 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
eb14cb74 10630 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4207c8b9
ML
10631
10632 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
dc008bf0
JN
10633 m2_n2->link_m = intel_de_read(dev_priv,
10634 PIPE_LINK_M2(transcoder));
10635 m2_n2->link_n = intel_de_read(dev_priv,
10636 PIPE_LINK_N2(transcoder));
10637 m2_n2->gmch_m = intel_de_read(dev_priv,
10638 PIPE_DATA_M2(transcoder))
b95af8be 10639 & ~TU_SIZE_MASK;
dc008bf0
JN
10640 m2_n2->gmch_n = intel_de_read(dev_priv,
10641 PIPE_DATA_N2(transcoder));
10642 m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
b95af8be
VK
10643 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10644 }
eb14cb74 10645 } else {
dc008bf0
JN
10646 m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10647 m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10648 m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
eb14cb74 10649 & ~TU_SIZE_MASK;
dc008bf0
JN
10650 m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10651 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
eb14cb74
VS
10652 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10653 }
10654}
10655
10656void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 10657 struct intel_crtc_state *pipe_config)
eb14cb74 10658{
681a8504 10659 if (pipe_config->has_pch_encoder)
eb14cb74
VS
10660 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10661 else
10662 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
10663 &pipe_config->dp_m_n,
10664 &pipe_config->dp_m2_n2);
eb14cb74 10665}
72419203 10666
9eae5e27
LDM
10667static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10668 struct intel_crtc_state *pipe_config)
eb14cb74
VS
10669{
10670 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 10671 &pipe_config->fdi_m_n, NULL);
72419203
DV
10672}
10673
35dd95b4
VS
10674static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
10675 u32 pos, u32 size)
10676{
10677 drm_rect_init(&crtc_state->pch_pfit.dst,
10678 pos >> 16, pos & 0xffff,
10679 size >> 16, size & 0xffff);
10680}
10681
eac9c585 10682static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
bd2e244f 10683{
eac9c585
VS
10684 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10685 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10686 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
a1b2278e
CK
10687 int id = -1;
10688 int i;
bd2e244f 10689
a1b2278e
CK
10690 /* find scaler attached to this pipe */
10691 for (i = 0; i < crtc->num_scalers; i++) {
35dd95b4 10692 u32 ctl, pos, size;
eac9c585 10693
35dd95b4
VS
10694 ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10695 if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
eac9c585
VS
10696 continue;
10697
10698 id = i;
10699 crtc_state->pch_pfit.enabled = true;
35dd95b4
VS
10700
10701 pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
10702 size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
10703
10704 ilk_get_pfit_pos_size(crtc_state, pos, size);
10705
eac9c585
VS
10706 scaler_state->scalers[i].in_use = true;
10707 break;
a1b2278e 10708 }
bd2e244f 10709
a1b2278e 10710 scaler_state->scaler_id = id;
eac9c585 10711 if (id >= 0)
a1b2278e 10712 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
eac9c585 10713 else
a1b2278e 10714 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
10715}
10716
5724dbd1 10717static void
f6df4d46
LDM
10718skl_get_initial_plane_config(struct intel_crtc *crtc,
10719 struct intel_initial_plane_config *plane_config)
bc8d7dff 10720{
0385ecea 10721 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
bc8d7dff 10722 struct drm_device *dev = crtc->base.dev;
fac5e23e 10723 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
10724 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10725 enum plane_id plane_id = plane->id;
eade6c89 10726 enum pipe pipe;
4036c78c 10727 u32 val, base, offset, stride_mult, tiling, alpha;
bc8d7dff 10728 int fourcc, pixel_format;
6761dd31 10729 unsigned int aligned_height;
bc8d7dff 10730 struct drm_framebuffer *fb;
1b842c89 10731 struct intel_framebuffer *intel_fb;
bc8d7dff 10732
eade6c89 10733 if (!plane->get_hw_state(plane, &pipe))
2924b8cc
VS
10734 return;
10735
e57291c2 10736 drm_WARN_ON(dev, pipe != crtc->pipe);
eade6c89 10737
0385ecea
MN
10738 if (crtc_state->bigjoiner) {
10739 drm_dbg_kms(&dev_priv->drm,
10740 "Unsupported bigjoiner configuration for initial FB\n");
10741 return;
10742 }
10743
d9806c9f 10744 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 10745 if (!intel_fb) {
cd49f818 10746 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
bc8d7dff
DL
10747 return;
10748 }
10749
1b842c89
DL
10750 fb = &intel_fb->base;
10751
d2e9f5fc
VS
10752 fb->dev = dev;
10753
dc008bf0 10754 val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
42a7b088 10755
b5972776
JA
10756 if (INTEL_GEN(dev_priv) >= 11)
10757 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10758 else
10759 pixel_format = val & PLANE_CTL_FORMAT_MASK;
4036c78c
JA
10760
10761 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
dc008bf0
JN
10762 alpha = intel_de_read(dev_priv,
10763 PLANE_COLOR_CTL(pipe, plane_id));
4036c78c
JA
10764 alpha &= PLANE_COLOR_ALPHA_MASK;
10765 } else {
10766 alpha = val & PLANE_CTL_ALPHA_MASK;
10767 }
10768
bc8d7dff 10769 fourcc = skl_format_to_fourcc(pixel_format,
4036c78c 10770 val & PLANE_CTL_ORDER_RGBX, alpha);
2f3f4763 10771 fb->format = drm_format_info(fourcc);
bc8d7dff 10772
40f46283
DL
10773 tiling = val & PLANE_CTL_TILED_MASK;
10774 switch (tiling) {
10775 case PLANE_CTL_TILED_LINEAR:
2f075565 10776 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
10777 break;
10778 case PLANE_CTL_TILED_X:
10779 plane_config->tiling = I915_TILING_X;
bae781b2 10780 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
10781 break;
10782 case PLANE_CTL_TILED_Y:
914a4fd8 10783 plane_config->tiling = I915_TILING_Y;
53867b46 10784 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
b3e57bcc
DP
10785 fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10786 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10787 I915_FORMAT_MOD_Y_TILED_CCS;
2dfbf9d2
DP
10788 else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10789 fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
2e2adb05
VS
10790 else
10791 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
10792 break;
10793 case PLANE_CTL_TILED_YF:
53867b46 10794 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
2e2adb05
VS
10795 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10796 else
10797 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
10798 break;
10799 default:
10800 MISSING_CASE(tiling);
10801 goto error;
10802 }
10803
f43348a3
VS
10804 /*
10805 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10806 * while i915 HW rotation is clockwise, thats why this swapping.
10807 */
10808 switch (val & PLANE_CTL_ROTATE_MASK) {
10809 case PLANE_CTL_ROTATE_0:
10810 plane_config->rotation = DRM_MODE_ROTATE_0;
10811 break;
10812 case PLANE_CTL_ROTATE_90:
10813 plane_config->rotation = DRM_MODE_ROTATE_270;
10814 break;
10815 case PLANE_CTL_ROTATE_180:
10816 plane_config->rotation = DRM_MODE_ROTATE_180;
10817 break;
10818 case PLANE_CTL_ROTATE_270:
10819 plane_config->rotation = DRM_MODE_ROTATE_90;
10820 break;
10821 }
10822
10823 if (INTEL_GEN(dev_priv) >= 10 &&
10824 val & PLANE_CTL_FLIP_HORIZONTAL)
10825 plane_config->rotation |= DRM_MODE_REFLECT_X;
10826
a40a8305
VS
10827 /* 90/270 degree rotation would require extra work */
10828 if (drm_rotation_90_or_270(plane_config->rotation))
10829 goto error;
10830
dc008bf0 10831 base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
bc8d7dff
DL
10832 plane_config->base = base;
10833
dc008bf0 10834 offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
bc8d7dff 10835
dc008bf0 10836 val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
e91c8a29
MN
10837 fb->height = ((val >> 16) & 0xffff) + 1;
10838 fb->width = ((val >> 0) & 0xffff) + 1;
bc8d7dff 10839
dc008bf0 10840 val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
b3cf5c06 10841 stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
bc8d7dff
DL
10842 fb->pitches[0] = (val & 0x3ff) * stride_mult;
10843
d88c4afd 10844 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 10845
f37b5c2b 10846 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff 10847
cd49f818
WK
10848 drm_dbg_kms(&dev_priv->drm,
10849 "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10850 crtc->base.name, plane->base.name, fb->width, fb->height,
10851 fb->format->cpp[0] * 8, base, fb->pitches[0],
10852 plane_config->size);
bc8d7dff 10853
2d14030b 10854 plane_config->fb = intel_fb;
bc8d7dff
DL
10855 return;
10856
10857error:
d1a3a036 10858 kfree(intel_fb);
bc8d7dff
DL
10859}
10860
eac9c585 10861static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
2fa2fe9a 10862{
eac9c585
VS
10863 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10864 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
35dd95b4 10865 u32 ctl, pos, size;
2fa2fe9a 10866
35dd95b4
VS
10867 ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10868 if ((ctl & PF_ENABLE) == 0)
eac9c585 10869 return;
2fa2fe9a 10870
eac9c585 10871 crtc_state->pch_pfit.enabled = true;
35dd95b4
VS
10872
10873 pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
10874 size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
10875
10876 ilk_get_pfit_pos_size(crtc_state, pos, size);
eac9c585
VS
10877
10878 /*
10879 * We currently do not free assignements of panel fitters on
10880 * ivb/hsw (since we don't use the higher upscaling modes which
10881 * differentiates them) so just WARN about this case for now.
10882 */
10883 drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
35dd95b4 10884 (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
79e53945
JB
10885}
10886
9eae5e27
LDM
10887static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10888 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10889{
10890 struct drm_device *dev = crtc->base.dev;
fac5e23e 10891 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 10892 enum intel_display_power_domain power_domain;
0e6e0be4 10893 intel_wakeref_t wakeref;
ba3f4d0a 10894 u32 tmp;
1729050e 10895 bool ret;
0e8ffe1b 10896
1729050e 10897 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
0e6e0be4
CW
10898 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10899 if (!wakeref)
930e8c9e
PZ
10900 return false;
10901
e143a21c 10902 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 10903 pipe_config->shared_dpll = NULL;
eccb140b 10904
1729050e 10905 ret = false;
dc008bf0 10906 tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
0e8ffe1b 10907 if (!(tmp & PIPECONF_ENABLE))
1729050e 10908 goto out;
0e8ffe1b 10909
42571aef
VS
10910 switch (tmp & PIPECONF_BPC_MASK) {
10911 case PIPECONF_6BPC:
10912 pipe_config->pipe_bpp = 18;
10913 break;
10914 case PIPECONF_8BPC:
10915 pipe_config->pipe_bpp = 24;
10916 break;
10917 case PIPECONF_10BPC:
10918 pipe_config->pipe_bpp = 30;
10919 break;
10920 case PIPECONF_12BPC:
10921 pipe_config->pipe_bpp = 36;
10922 break;
10923 default:
10924 break;
10925 }
10926
b5a9fa09
DV
10927 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10928 pipe_config->limited_color_range = true;
10929
d1844606
VS
10930 switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10931 case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10932 case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10933 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10934 break;
10935 default:
10936 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10937 break;
10938 }
10939
9d5441de
VS
10940 pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10941 PIPECONF_GAMMA_MODE_SHIFT;
10942
dc008bf0
JN
10943 pipe_config->csc_mode = intel_de_read(dev_priv,
10944 PIPE_CSC_MODE(crtc->pipe));
a1f1e61b 10945
5f29ab23 10946 i9xx_get_pipe_color_config(pipe_config);
3633e511 10947 intel_color_get_config(pipe_config);
5f29ab23 10948
dc008bf0 10949 if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 10950 struct intel_shared_dpll *pll;
8106ddbd 10951 enum intel_dpll_id pll_id;
fdbc5d68 10952 bool pll_active;
66e985c0 10953
88adfff1
DV
10954 pipe_config->has_pch_encoder = true;
10955
dc008bf0 10956 tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
627eb5a3
DV
10957 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10958 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203 10959
9eae5e27 10960 ilk_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 10961
2d1fe073 10962 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
10963 /*
10964 * The pipe->pch transcoder and pch transcoder->pll
10965 * mapping is fixed.
10966 */
8106ddbd 10967 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62 10968 } else {
dc008bf0 10969 tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
c0d43d62 10970 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 10971 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 10972 else
8106ddbd 10973 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 10974 }
66e985c0 10975
8106ddbd
ACO
10976 pipe_config->shared_dpll =
10977 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10978 pll = pipe_config->shared_dpll;
66e985c0 10979
fdbc5d68
VS
10980 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
10981 &pipe_config->dpll_hw_state);
10982 drm_WARN_ON(dev, !pll_active);
c93f54cf
DV
10983
10984 tmp = pipe_config->dpll_hw_state.dpll;
10985 pipe_config->pixel_multiplier =
10986 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10987 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08 10988
9eae5e27 10989 ilk_pch_clock_get(crtc, pipe_config);
6c49f241
DV
10990 } else {
10991 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
10992 }
10993
e7fc3f90 10994 intel_get_transcoder_timings(crtc, pipe_config);
bc58be60 10995 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10996
eac9c585 10997 ilk_get_pfit_config(pipe_config);
2fa2fe9a 10998
1729050e
ID
10999 ret = true;
11000
11001out:
0e6e0be4 11002 intel_display_power_put(dev_priv, power_domain, wakeref);
1729050e
ID
11003
11004 return ret;
0e8ffe1b 11005}
1e98f88c
LDM
11006
11007static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
11008 struct intel_crtc_state *crtc_state)
09b4ddf9 11009{
70a057b7 11010 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5a0b385e 11011 struct intel_atomic_state *state =
2225f3c6 11012 to_intel_atomic_state(crtc_state->uapi.state);
5a0b385e 11013
70a057b7 11014 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
2dd24a9c 11015 INTEL_GEN(dev_priv) >= 11) {
44a126ba 11016 struct intel_encoder *encoder =
5a0b385e 11017 intel_get_crtc_new_encoder(state, crtc_state);
44a126ba 11018
866955fa 11019 if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
cd49f818
WK
11020 drm_dbg_kms(&dev_priv->drm,
11021 "failed to find PLL for pipe %c\n",
11022 pipe_name(crtc->pipe));
af3997b5 11023 return -EINVAL;
44a126ba 11024 }
af3997b5 11025 }
716c2e55 11026
c8f7a0db 11027 return 0;
79e53945
JB
11028}
11029
11ffe972
LDM
11030static void dg1_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11031 struct intel_crtc_state *pipe_config)
11032{
11033 enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
11034 enum phy phy = intel_port_to_phy(dev_priv, port);
b97fcaee
VS
11035 struct icl_port_dpll *port_dpll;
11036 struct intel_shared_dpll *pll;
11ffe972 11037 enum intel_dpll_id id;
b97fcaee 11038 bool pll_active;
11ffe972
LDM
11039 u32 clk_sel;
11040
11041 clk_sel = intel_de_read(dev_priv, DG1_DPCLKA_CFGCR0(phy)) & DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11042 id = DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_DPLL_MAP(clk_sel, phy);
11043
11044 if (WARN_ON(id > DPLL_ID_DG1_DPLL3))
11045 return;
11046
b97fcaee
VS
11047 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11048 port_dpll = &pipe_config->icl_port_dplls[port_dpll_id];
11049
11050 port_dpll->pll = pll;
11051 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11052 &port_dpll->hw_state);
11053 drm_WARN_ON(&dev_priv->drm, !pll_active);
11ffe972
LDM
11054
11055 icl_set_active_port_dpll(pipe_config, port_dpll_id);
11056}
11057
95be3484
LDM
11058static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11059 struct intel_crtc_state *pipe_config)
970888e7 11060{
d8fe2ab6 11061 enum phy phy = intel_port_to_phy(dev_priv, port);
eea72c4c 11062 enum icl_port_dpll_id port_dpll_id;
b97fcaee
VS
11063 struct icl_port_dpll *port_dpll;
11064 struct intel_shared_dpll *pll;
970888e7 11065 enum intel_dpll_id id;
b97fcaee 11066 bool pll_active;
970888e7
PZ
11067 u32 temp;
11068
d8fe2ab6 11069 if (intel_phy_is_combo(dev_priv, phy)) {
cd803bb4
MR
11070 u32 mask, shift;
11071
11072 if (IS_ROCKETLAKE(dev_priv)) {
11073 mask = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11074 shift = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11075 } else {
11076 mask = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11077 shift = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11078 }
11079
11080 temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) & mask;
11081 id = temp >> shift;
eea72c4c 11082 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
d8fe2ab6 11083 } else if (intel_phy_is_tc(dev_priv, phy)) {
dc008bf0 11084 u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
39a5883f
ID
11085
11086 if (clk_sel == DDI_CLK_SEL_MG) {
11087 id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
11088 port));
eea72c4c 11089 port_dpll_id = ICL_PORT_DPLL_MG_PHY;
39a5883f 11090 } else {
e57291c2
PB
11091 drm_WARN_ON(&dev_priv->drm,
11092 clk_sel < DDI_CLK_SEL_TBT_162);
39a5883f 11093 id = DPLL_ID_ICL_TBTPLL;
eea72c4c 11094 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
39a5883f 11095 }
8ea59e67 11096 } else {
e57291c2 11097 drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
970888e7
PZ
11098 return;
11099 }
11100
b97fcaee
VS
11101 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11102 port_dpll = &pipe_config->icl_port_dplls[port_dpll_id];
11103
11104 port_dpll->pll = pll;
11105 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11106 &port_dpll->hw_state);
11107 drm_WARN_ON(&dev_priv->drm, !pll_active);
eea72c4c
ID
11108
11109 icl_set_active_port_dpll(pipe_config, port_dpll_id);
970888e7
PZ
11110}
11111
c9afbf58
VS
11112static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11113 struct intel_crtc_state *pipe_config)
11114{
11115 struct intel_shared_dpll *pll;
11116 enum intel_dpll_id id;
11117 bool pll_active;
11118 u32 temp;
11119
11120 temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
11121 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
11122
11123 if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
11124 return;
11125
11126 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11127
11128 pipe_config->shared_dpll = pll;
11129 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11130 &pipe_config->dpll_hw_state);
11131 drm_WARN_ON(&dev_priv->drm, !pll_active);
11132}
11133
3760b59c
S
11134static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
11135 enum port port,
11136 struct intel_crtc_state *pipe_config)
11137{
b97fcaee 11138 struct intel_shared_dpll *pll;
8106ddbd 11139 enum intel_dpll_id id;
b97fcaee 11140 bool pll_active;
8106ddbd 11141
3760b59c
S
11142 switch (port) {
11143 case PORT_A:
08250c4b 11144 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
11145 break;
11146 case PORT_B:
08250c4b 11147 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
11148 break;
11149 case PORT_C:
08250c4b 11150 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
11151 break;
11152 default:
cd49f818 11153 drm_err(&dev_priv->drm, "Incorrect port type\n");
8106ddbd 11154 return;
3760b59c 11155 }
8106ddbd 11156
b97fcaee
VS
11157 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11158
11159 pipe_config->shared_dpll = pll;
11160 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11161 &pipe_config->dpll_hw_state);
11162 drm_WARN_ON(&dev_priv->drm, !pll_active);
3760b59c
S
11163}
11164
f6df4d46
LDM
11165static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11166 struct intel_crtc_state *pipe_config)
96b7dfb7 11167{
b97fcaee 11168 struct intel_shared_dpll *pll;
8106ddbd 11169 enum intel_dpll_id id;
b97fcaee 11170 bool pll_active;
a3c988ea 11171 u32 temp;
96b7dfb7 11172
dc008bf0 11173 temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 11174 id = temp >> (port * 3 + 1);
96b7dfb7 11175
e57291c2 11176 if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 11177 return;
8106ddbd 11178
b97fcaee
VS
11179 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11180
11181 pipe_config->shared_dpll = pll;
11182 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11183 &pipe_config->dpll_hw_state);
11184 drm_WARN_ON(&dev_priv->drm, !pll_active);
96b7dfb7
S
11185}
11186
1e98f88c
LDM
11187static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11188 struct intel_crtc_state *pipe_config)
7d2c8175 11189{
b97fcaee 11190 struct intel_shared_dpll *pll;
8106ddbd 11191 enum intel_dpll_id id;
dc008bf0 11192 u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
b97fcaee 11193 bool pll_active;
8106ddbd 11194
c856052a 11195 switch (ddi_pll_sel) {
7d2c8175 11196 case PORT_CLK_SEL_WRPLL1:
8106ddbd 11197 id = DPLL_ID_WRPLL1;
7d2c8175
DL
11198 break;
11199 case PORT_CLK_SEL_WRPLL2:
8106ddbd 11200 id = DPLL_ID_WRPLL2;
7d2c8175 11201 break;
00490c22 11202 case PORT_CLK_SEL_SPLL:
8106ddbd 11203 id = DPLL_ID_SPLL;
79bd23da 11204 break;
9d16da65
ACO
11205 case PORT_CLK_SEL_LCPLL_810:
11206 id = DPLL_ID_LCPLL_810;
11207 break;
11208 case PORT_CLK_SEL_LCPLL_1350:
11209 id = DPLL_ID_LCPLL_1350;
11210 break;
11211 case PORT_CLK_SEL_LCPLL_2700:
11212 id = DPLL_ID_LCPLL_2700;
11213 break;
8106ddbd 11214 default:
c856052a 11215 MISSING_CASE(ddi_pll_sel);
df561f66 11216 fallthrough;
8106ddbd 11217 case PORT_CLK_SEL_NONE:
8106ddbd 11218 return;
7d2c8175 11219 }
8106ddbd 11220
b97fcaee
VS
11221 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11222
11223 pipe_config->shared_dpll = pll;
11224 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11225 &pipe_config->dpll_hw_state);
11226 drm_WARN_ON(&dev_priv->drm, !pll_active);
7d2c8175
DL
11227}
11228
cf30429e
JN
11229static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
11230 struct intel_crtc_state *pipe_config,
513a4c55
CW
11231 u64 *power_domain_mask,
11232 intel_wakeref_t *wakerefs)
cf30429e
JN
11233{
11234 struct drm_device *dev = crtc->base.dev;
fac5e23e 11235 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e 11236 enum intel_display_power_domain power_domain;
617458cd 11237 unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
0716931a
JN
11238 unsigned long enabled_panel_transcoders = 0;
11239 enum transcoder panel_transcoder;
513a4c55 11240 intel_wakeref_t wf;
cf30429e 11241 u32 tmp;
0716931a 11242
2dd24a9c 11243 if (INTEL_GEN(dev_priv) >= 11)
0716931a
JN
11244 panel_transcoder_mask |=
11245 BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
cf30429e 11246
d9a7bc67
ID
11247 /*
11248 * The pipe->transcoder mapping is fixed with the exception of the eDP
0716931a 11249 * and DSI transcoders handled below.
d9a7bc67 11250 */
cf30429e
JN
11251 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
11252
11253 /*
11254 * XXX: Do intel_display_power_get_if_enabled before reading this (for
11255 * consistency and less surprising code; it's in always on power).
11256 */
617458cd
AS
11257 for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
11258 panel_transcoder_mask) {
dc0c0bfe 11259 bool force_thru = false;
0716931a 11260 enum pipe trans_pipe;
2ca711ca 11261
dc008bf0
JN
11262 tmp = intel_de_read(dev_priv,
11263 TRANS_DDI_FUNC_CTL(panel_transcoder));
0716931a
JN
11264 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11265 continue;
2ca711ca 11266
0716931a
JN
11267 /*
11268 * Log all enabled ones, only use the first one.
11269 *
11270 * FIXME: This won't work for two separate DSI displays.
11271 */
11272 enabled_panel_transcoders |= BIT(panel_transcoder);
11273 if (enabled_panel_transcoders != BIT(panel_transcoder))
11274 continue;
2ca711ca 11275
cf30429e
JN
11276 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
11277 default:
e57291c2
PB
11278 drm_WARN(dev, 1,
11279 "unknown pipe linked to transcoder %s\n",
11280 transcoder_name(panel_transcoder));
df561f66 11281 fallthrough;
cf30429e 11282 case TRANS_DDI_EDP_INPUT_A_ONOFF:
dc0c0bfe 11283 force_thru = true;
df561f66 11284 fallthrough;
cf30429e 11285 case TRANS_DDI_EDP_INPUT_A_ON:
2ca711ca 11286 trans_pipe = PIPE_A;
cf30429e
JN
11287 break;
11288 case TRANS_DDI_EDP_INPUT_B_ONOFF:
2ca711ca 11289 trans_pipe = PIPE_B;
cf30429e
JN
11290 break;
11291 case TRANS_DDI_EDP_INPUT_C_ONOFF:
2ca711ca 11292 trans_pipe = PIPE_C;
cf30429e 11293 break;
9c569784
JN
11294 case TRANS_DDI_EDP_INPUT_D_ONOFF:
11295 trans_pipe = PIPE_D;
11296 break;
cf30429e
JN
11297 }
11298
dc0c0bfe 11299 if (trans_pipe == crtc->pipe) {
0716931a 11300 pipe_config->cpu_transcoder = panel_transcoder;
dc0c0bfe
VS
11301 pipe_config->pch_pfit.force_thru = force_thru;
11302 }
cf30429e
JN
11303 }
11304
0716931a
JN
11305 /*
11306 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
11307 */
e57291c2
PB
11308 drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
11309 enabled_panel_transcoders != BIT(TRANSCODER_EDP));
0716931a 11310
cf30429e 11311 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
e57291c2 11312 drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11313
11314 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11315 if (!wf)
cf30429e 11316 return false;
04161d64 11317
513a4c55 11318 wakerefs[power_domain] = wf;
d8fc70b7 11319 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e 11320
dc008bf0 11321 tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
cf30429e
JN
11322
11323 return tmp & PIPECONF_ENABLE;
11324}
11325
4d1de975
JN
11326static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
11327 struct intel_crtc_state *pipe_config,
513a4c55
CW
11328 u64 *power_domain_mask,
11329 intel_wakeref_t *wakerefs)
4d1de975
JN
11330{
11331 struct drm_device *dev = crtc->base.dev;
fac5e23e 11332 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 11333 enum intel_display_power_domain power_domain;
4d1de975 11334 enum transcoder cpu_transcoder;
513a4c55
CW
11335 intel_wakeref_t wf;
11336 enum port port;
4d1de975
JN
11337 u32 tmp;
11338
4d1de975
JN
11339 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
11340 if (port == PORT_A)
11341 cpu_transcoder = TRANSCODER_DSI_A;
11342 else
11343 cpu_transcoder = TRANSCODER_DSI_C;
11344
11345 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
e57291c2 11346 drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11347
11348 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11349 if (!wf)
4d1de975 11350 continue;
04161d64 11351
513a4c55 11352 wakerefs[power_domain] = wf;
d8fc70b7 11353 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 11354
db18b6a6
ID
11355 /*
11356 * The PLL needs to be enabled with a valid divider
11357 * configuration, otherwise accessing DSI registers will hang
11358 * the machine. See BSpec North Display Engine
11359 * registers/MIPI[BXT]. We can break out here early, since we
11360 * need the same DSI PLL to be enabled for both DSI ports.
11361 */
e518634b 11362 if (!bxt_dsi_pll_is_enabled(dev_priv))
db18b6a6
ID
11363 break;
11364
4d1de975 11365 /* XXX: this works for video mode only */
dc008bf0 11366 tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
4d1de975
JN
11367 if (!(tmp & DPI_ENABLE))
11368 continue;
11369
dc008bf0 11370 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
4d1de975
JN
11371 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11372 continue;
11373
11374 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
11375 break;
11376 }
11377
d7edc4e5 11378 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
11379}
11380
1e98f88c
LDM
11381static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11382 struct intel_crtc_state *pipe_config)
26804afd 11383{
6315b5d3 11384 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6d73af27 11385 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
26804afd 11386 enum port port;
ba3f4d0a 11387 u32 tmp;
26804afd 11388
6d73af27
VK
11389 if (transcoder_is_dsi(cpu_transcoder)) {
11390 port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11391 PORT_A : PORT_B;
11392 } else {
dc008bf0
JN
11393 tmp = intel_de_read(dev_priv,
11394 TRANS_DDI_FUNC_CTL(cpu_transcoder));
0385ecea
MN
11395 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11396 return;
6d73af27
VK
11397 if (INTEL_GEN(dev_priv) >= 12)
11398 port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11399 else
11400 port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11401 }
26804afd 11402
11ffe972
LDM
11403 if (IS_DG1(dev_priv))
11404 dg1_get_ddi_pll(dev_priv, port, pipe_config);
11405 else if (INTEL_GEN(dev_priv) >= 11)
95be3484 11406 icl_get_ddi_pll(dev_priv, port, pipe_config);
970888e7 11407 else if (IS_CANNONLAKE(dev_priv))
8e619820 11408 cnl_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 11409 else if (IS_GEN9_LP(dev_priv))
3760b59c 11410 bxt_get_ddi_pll(dev_priv, port, pipe_config);
c9afbf58
VS
11411 else if (IS_GEN9_BC(dev_priv))
11412 skl_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7 11413 else
1e98f88c 11414 hsw_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 11415
26804afd
DV
11416 /*
11417 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11418 * DDI E. So just check whether this pipe is wired to DDI E and whether
11419 * the PCH transcoder is on.
11420 */
6315b5d3 11421 if (INTEL_GEN(dev_priv) < 9 &&
dc008bf0 11422 (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
11423 pipe_config->has_pch_encoder = true;
11424
dc008bf0 11425 tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
26804afd
DV
11426 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11427 FDI_DP_PORT_WIDTH_SHIFT) + 1;
11428
9eae5e27 11429 ilk_get_fdi_m_n_config(crtc, pipe_config);
26804afd
DV
11430 }
11431}
11432
1e98f88c
LDM
11433static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11434 struct intel_crtc_state *pipe_config)
0e8ffe1b 11435{
6315b5d3 11436 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
513a4c55 11437 intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
1729050e 11438 enum intel_display_power_domain power_domain;
d8fc70b7 11439 u64 power_domain_mask;
cf30429e 11440 bool active;
6dcde047 11441 u32 tmp;
0e8ffe1b 11442
ba5f1ae9
MN
11443 pipe_config->master_transcoder = INVALID_TRANSCODER;
11444
1729050e 11445 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
513a4c55
CW
11446 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11447 if (!wf)
b5482bd0 11448 return false;
513a4c55
CW
11449
11450 wakerefs[power_domain] = wf;
d8fc70b7 11451 power_domain_mask = BIT_ULL(power_domain);
1729050e 11452
8106ddbd 11453 pipe_config->shared_dpll = NULL;
c0d43d62 11454
513a4c55
CW
11455 active = hsw_get_transcoder_state(crtc, pipe_config,
11456 &power_domain_mask, wakerefs);
eccb140b 11457
cc3f90f0 11458 if (IS_GEN9_LP(dev_priv) &&
513a4c55
CW
11459 bxt_get_dsi_transcoder_state(crtc, pipe_config,
11460 &power_domain_mask, wakerefs)) {
e57291c2 11461 drm_WARN_ON(&dev_priv->drm, active);
d7edc4e5 11462 active = true;
4d1de975
JN
11463 }
11464
0385ecea
MN
11465 intel_dsc_get_config(pipe_config);
11466
11467 if (!active) {
11468 /* bigjoiner slave doesn't enable transcoder */
11469 if (!pipe_config->bigjoiner_slave)
11470 goto out;
0e8ffe1b 11471
0385ecea
MN
11472 active = true;
11473 pipe_config->pixel_multiplier = 1;
11474
11475 /* we cannot read out most state, so don't bother.. */
11476 pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
11477 } else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
2dd24a9c 11478 INTEL_GEN(dev_priv) >= 11) {
1e98f88c 11479 hsw_get_ddi_port_state(crtc, pipe_config);
e7fc3f90 11480 intel_get_transcoder_timings(crtc, pipe_config);
4d1de975 11481 }
627eb5a3 11482
bc58be60 11483 intel_get_pipe_src_size(crtc, pipe_config);
b10d1173 11484
ac0f01ce 11485 if (IS_HASWELL(dev_priv)) {
dc008bf0
JN
11486 u32 tmp = intel_de_read(dev_priv,
11487 PIPECONF(pipe_config->cpu_transcoder));
ac0f01ce
VS
11488
11489 if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11490 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11491 else
11492 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11493 } else {
b10d1173
VS
11494 pipe_config->output_format =
11495 bdw_get_pipemisc_output_format(crtc);
b10d1173 11496 }
1bd1bd80 11497
dc008bf0
JN
11498 pipe_config->gamma_mode = intel_de_read(dev_priv,
11499 GAMMA_MODE(crtc->pipe));
05dc698c 11500
dc008bf0
JN
11501 pipe_config->csc_mode = intel_de_read(dev_priv,
11502 PIPE_CSC_MODE(crtc->pipe));
a1f1e61b 11503
5f29ab23 11504 if (INTEL_GEN(dev_priv) >= 9) {
6dcde047 11505 tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
5f29ab23
VS
11506
11507 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11508 pipe_config->gamma_enable = true;
8271b2ef
VS
11509
11510 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11511 pipe_config->csc_enable = true;
5f29ab23
VS
11512 } else {
11513 i9xx_get_pipe_color_config(pipe_config);
11514 }
11515
3633e511
SS
11516 intel_color_get_config(pipe_config);
11517
6dcde047
VS
11518 tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11519 pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11520 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11521 pipe_config->ips_linetime =
11522 REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11523
1729050e 11524 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
e57291c2 11525 drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11526
11527 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11528 if (wf) {
11529 wakerefs[power_domain] = wf;
d8fc70b7 11530 power_domain_mask |= BIT_ULL(power_domain);
04161d64 11531
6315b5d3 11532 if (INTEL_GEN(dev_priv) >= 9)
eac9c585 11533 skl_get_pfit_config(pipe_config);
ff6d9f55 11534 else
eac9c585 11535 ilk_get_pfit_config(pipe_config);
bd2e244f 11536 }
88adfff1 11537
24f28450
ML
11538 if (hsw_crtc_supports_ips(crtc)) {
11539 if (IS_HASWELL(dev_priv))
dc008bf0
JN
11540 pipe_config->ips_enabled = intel_de_read(dev_priv,
11541 IPS_CTL) & IPS_ENABLE;
24f28450
ML
11542 else {
11543 /*
11544 * We cannot readout IPS state on broadwell, set to
11545 * true so we can set it to a defined state on first
11546 * commit.
11547 */
11548 pipe_config->ips_enabled = true;
11549 }
11550 }
11551
0385ecea
MN
11552 if (pipe_config->bigjoiner_slave) {
11553 /* Cannot be read out as a slave, set to 0. */
11554 pipe_config->pixel_multiplier = 0;
11555 } else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
4d1de975 11556 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95 11557 pipe_config->pixel_multiplier =
dc008bf0
JN
11558 intel_de_read(dev_priv,
11559 PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
ebb69c95
CT
11560 } else {
11561 pipe_config->pixel_multiplier = 1;
11562 }
6c49f241 11563
1729050e
ID
11564out:
11565 for_each_power_domain(power_domain, power_domain_mask)
513a4c55
CW
11566 intel_display_power_put(dev_priv,
11567 power_domain, wakerefs[power_domain]);
1729050e 11568
cf30429e 11569 return active;
0e8ffe1b
DV
11570}
11571
11f9af16
MN
11572static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
11573{
11574 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11575 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
11576
291106cb
VS
11577 if (!i915->display.get_pipe_config(crtc, crtc_state))
11578 return false;
11579
11580 crtc_state->hw.active = true;
11581
c42773b6
VS
11582 intel_crtc_readout_derived_state(crtc_state);
11583
291106cb 11584 return true;
11f9af16
MN
11585}
11586
cd5dcbf1 11587static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
11588{
11589 struct drm_i915_private *dev_priv =
f90a85e7 11590 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 11591 const struct drm_framebuffer *fb = plane_state->hw.fb;
1cecc830
VS
11592 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11593 u32 base;
11594
d53db442 11595 if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
c6790dc2 11596 base = sg_dma_address(obj->mm.pages->sgl);
1cecc830
VS
11597 else
11598 base = intel_plane_ggtt_offset(plane_state);
11599
30a027dc 11600 return base + plane_state->color_plane[0].offset;
1cecc830
VS
11601}
11602
ed270223
VS
11603static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11604{
f90a85e7
ML
11605 int x = plane_state->uapi.dst.x1;
11606 int y = plane_state->uapi.dst.y1;
ed270223
VS
11607 u32 pos = 0;
11608
11609 if (x < 0) {
11610 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11611 x = -x;
11612 }
11613 pos |= x << CURSOR_X_SHIFT;
11614
11615 if (y < 0) {
11616 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11617 y = -y;
11618 }
11619 pos |= y << CURSOR_Y_SHIFT;
11620
11621 return pos;
11622}
11623
3637ecf0
VS
11624static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11625{
11626 const struct drm_mode_config *config =
f90a85e7
ML
11627 &plane_state->uapi.plane->dev->mode_config;
11628 int width = drm_rect_width(&plane_state->uapi.dst);
11629 int height = drm_rect_height(&plane_state->uapi.dst);
3637ecf0
VS
11630
11631 return width > 0 && width <= config->cursor_width &&
11632 height > 0 && height <= config->cursor_height;
11633}
11634
fce8d235 11635static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
659056f2 11636{
30a027dc 11637 struct drm_i915_private *dev_priv =
f90a85e7 11638 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 11639 unsigned int rotation = plane_state->hw.rotation;
1e7b4fd8
VS
11640 int src_x, src_y;
11641 u32 offset;
fc3fed5d 11642 int ret;
fce8d235 11643
54d4d719 11644 ret = intel_plane_compute_gtt(plane_state);
fc3fed5d
VS
11645 if (ret)
11646 return ret;
11647
f90a85e7 11648 if (!plane_state->uapi.visible)
54d4d719
VS
11649 return 0;
11650
f90a85e7
ML
11651 src_x = plane_state->uapi.src.x1 >> 16;
11652 src_y = plane_state->uapi.src.y1 >> 16;
fce8d235
VS
11653
11654 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11655 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11656 plane_state, 0);
11657
11658 if (src_x != 0 || src_y != 0) {
cd49f818
WK
11659 drm_dbg_kms(&dev_priv->drm,
11660 "Arbitrary cursor panning not supported\n");
fce8d235
VS
11661 return -EINVAL;
11662 }
11663
30a027dc
VS
11664 /*
11665 * Put the final coordinates back so that the src
11666 * coordinate checks will see the right values.
11667 */
f90a85e7 11668 drm_rect_translate_to(&plane_state->uapi.src,
30a027dc
VS
11669 src_x << 16, src_y << 16);
11670
11671 /* ILK+ do this automagically in hardware */
11672 if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
7b3cb17a 11673 const struct drm_framebuffer *fb = plane_state->hw.fb;
f90a85e7
ML
11674 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11675 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
30a027dc
VS
11676
11677 offset += (src_h * src_w - 1) * fb->format->cpp[0];
11678 }
11679
fce8d235 11680 plane_state->color_plane[0].offset = offset;
30a027dc
VS
11681 plane_state->color_plane[0].x = src_x;
11682 plane_state->color_plane[0].y = src_y;
fce8d235
VS
11683
11684 return 0;
11685}
11686
11687static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11688 struct intel_plane_state *plane_state)
11689{
7b3cb17a 11690 const struct drm_framebuffer *fb = plane_state->hw.fb;
cd49f818 11691 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
659056f2
VS
11692 int ret;
11693
4e0b83a5 11694 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
cd49f818 11695 drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
4e0b83a5
VS
11696 return -EINVAL;
11697 }
11698
9f05a7c0
ML
11699 ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
11700 DRM_PLANE_HELPER_NO_SCALING,
11701 DRM_PLANE_HELPER_NO_SCALING,
11702 true);
659056f2
VS
11703 if (ret)
11704 return ret;
11705
3a612765 11706 /* Use the unclipped src/dst rectangles, which we program to hw */
f90a85e7
ML
11707 plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11708 plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
3a612765 11709
54d4d719
VS
11710 ret = intel_cursor_check_surface(plane_state);
11711 if (ret)
11712 return ret;
11713
f90a85e7 11714 if (!plane_state->uapi.visible)
659056f2
VS
11715 return 0;
11716
4e0b83a5
VS
11717 ret = intel_plane_check_src_coordinates(plane_state);
11718 if (ret)
11719 return ret;
659056f2
VS
11720
11721 return 0;
11722}
11723
ddd5713d
VS
11724static unsigned int
11725i845_cursor_max_stride(struct intel_plane *plane,
11726 u32 pixel_format, u64 modifier,
11727 unsigned int rotation)
11728{
11729 return 2048;
11730}
11731
7eb31a0b
VS
11732static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11733{
5f29ab23
VS
11734 u32 cntl = 0;
11735
11736 if (crtc_state->gamma_enable)
11737 cntl |= CURSOR_GAMMA_ENABLE;
11738
11739 return cntl;
7eb31a0b
VS
11740}
11741
292889e1
VS
11742static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11743 const struct intel_plane_state *plane_state)
11744{
292889e1 11745 return CURSOR_ENABLE |
292889e1 11746 CURSOR_FORMAT_ARGB |
df79cf44 11747 CURSOR_STRIDE(plane_state->color_plane[0].stride);
292889e1
VS
11748}
11749
659056f2
VS
11750static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11751{
f90a85e7 11752 int width = drm_rect_width(&plane_state->uapi.dst);
659056f2
VS
11753
11754 /*
11755 * 845g/865g are only limited by the width of their cursors,
11756 * the height is arbitrary up to the precision of the register.
11757 */
3637ecf0 11758 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
11759}
11760
eb0f5044 11761static int i845_check_cursor(struct intel_crtc_state *crtc_state,
659056f2
VS
11762 struct intel_plane_state *plane_state)
11763{
7b3cb17a 11764 const struct drm_framebuffer *fb = plane_state->hw.fb;
cd49f818 11765 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
659056f2
VS
11766 int ret;
11767
11768 ret = intel_check_cursor(crtc_state, plane_state);
11769 if (ret)
11770 return ret;
11771
11772 /* if we want to turn off the cursor ignore width and height */
1e1bb871 11773 if (!fb)
659056f2
VS
11774 return 0;
11775
11776 /* Check for which cursor types we support */
11777 if (!i845_cursor_size_ok(plane_state)) {
cd49f818
WK
11778 drm_dbg_kms(&i915->drm,
11779 "Cursor dimension %dx%d not supported\n",
11780 drm_rect_width(&plane_state->uapi.dst),
11781 drm_rect_height(&plane_state->uapi.dst));
659056f2
VS
11782 return -EINVAL;
11783 }
11784
e57291c2
PB
11785 drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11786 plane_state->color_plane[0].stride != fb->pitches[0]);
df79cf44 11787
1e1bb871 11788 switch (fb->pitches[0]) {
292889e1
VS
11789 case 256:
11790 case 512:
11791 case 1024:
11792 case 2048:
11793 break;
1e1bb871 11794 default:
cd49f818
WK
11795 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11796 fb->pitches[0]);
1e1bb871 11797 return -EINVAL;
292889e1
VS
11798 }
11799
659056f2
VS
11800 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11801
11802 return 0;
292889e1
VS
11803}
11804
b2d03b0d
VS
11805static void i845_update_cursor(struct intel_plane *plane,
11806 const struct intel_crtc_state *crtc_state,
55a08b3f 11807 const struct intel_plane_state *plane_state)
560b85bb 11808{
cd5dcbf1 11809 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
11810 u32 cntl = 0, base = 0, pos = 0, size = 0;
11811 unsigned long irqflags;
560b85bb 11812
f90a85e7
ML
11813 if (plane_state && plane_state->uapi.visible) {
11814 unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11815 unsigned int height = drm_rect_height(&plane_state->uapi.dst);
dc41c154 11816
7eb31a0b
VS
11817 cntl = plane_state->ctl |
11818 i845_cursor_ctl_crtc(crtc_state);
11819
dc41c154 11820 size = (height << 12) | width;
560b85bb 11821
b2d03b0d
VS
11822 base = intel_cursor_base(plane_state);
11823 pos = intel_cursor_position(plane_state);
4b0e333e 11824 }
560b85bb 11825
b2d03b0d 11826 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 11827
e11ffddb
VS
11828 /* On these chipsets we can only modify the base/size/stride
11829 * whilst the cursor is disabled.
11830 */
11831 if (plane->cursor.base != base ||
11832 plane->cursor.size != size ||
11833 plane->cursor.cntl != cntl) {
dc008bf0
JN
11834 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11835 intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11836 intel_de_write_fw(dev_priv, CURSIZE, size);
11837 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11838 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
75343a44 11839
e11ffddb
VS
11840 plane->cursor.base = base;
11841 plane->cursor.size = size;
11842 plane->cursor.cntl = cntl;
11843 } else {
dc008bf0 11844 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
560b85bb 11845 }
e11ffddb 11846
b2d03b0d
VS
11847 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11848}
11849
11850static void i845_disable_cursor(struct intel_plane *plane,
0dd14be3 11851 const struct intel_crtc_state *crtc_state)
b2d03b0d 11852{
0dd14be3 11853 i845_update_cursor(plane, crtc_state, NULL);
560b85bb
CW
11854}
11855
eade6c89
VS
11856static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11857 enum pipe *pipe)
51f5a096
VS
11858{
11859 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11860 enum intel_display_power_domain power_domain;
0e6e0be4 11861 intel_wakeref_t wakeref;
51f5a096
VS
11862 bool ret;
11863
11864 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
0e6e0be4
CW
11865 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11866 if (!wakeref)
51f5a096
VS
11867 return false;
11868
dc008bf0 11869 ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
51f5a096 11870
eade6c89
VS
11871 *pipe = PIPE_A;
11872
0e6e0be4 11873 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
11874
11875 return ret;
11876}
11877
ddd5713d
VS
11878static unsigned int
11879i9xx_cursor_max_stride(struct intel_plane *plane,
11880 u32 pixel_format, u64 modifier,
11881 unsigned int rotation)
11882{
11883 return plane->base.dev->mode_config.cursor_width * 4;
11884}
11885
7eb31a0b 11886static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
292889e1 11887{
2225f3c6 11888 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7eb31a0b 11889 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
c894d63c 11890 u32 cntl = 0;
292889e1 11891
7eb31a0b
VS
11892 if (INTEL_GEN(dev_priv) >= 11)
11893 return cntl;
e876b78c 11894
5f29ab23
VS
11895 if (crtc_state->gamma_enable)
11896 cntl = MCURSOR_GAMMA_ENABLE;
292889e1 11897
8271b2ef 11898 if (crtc_state->csc_enable)
7eb31a0b 11899 cntl |= MCURSOR_PIPE_CSC_ENABLE;
292889e1 11900
32ea06b6
VS
11901 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11902 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1 11903
7eb31a0b
VS
11904 return cntl;
11905}
11906
11907static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11908 const struct intel_plane_state *plane_state)
11909{
11910 struct drm_i915_private *dev_priv =
f90a85e7 11911 to_i915(plane_state->uapi.plane->dev);
7eb31a0b
VS
11912 u32 cntl = 0;
11913
11914 if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11915 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11916
f90a85e7 11917 switch (drm_rect_width(&plane_state->uapi.dst)) {
292889e1 11918 case 64:
b99b9ec1 11919 cntl |= MCURSOR_MODE_64_ARGB_AX;
292889e1
VS
11920 break;
11921 case 128:
b99b9ec1 11922 cntl |= MCURSOR_MODE_128_ARGB_AX;
292889e1
VS
11923 break;
11924 case 256:
b99b9ec1 11925 cntl |= MCURSOR_MODE_256_ARGB_AX;
292889e1
VS
11926 break;
11927 default:
f90a85e7 11928 MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
292889e1
VS
11929 return 0;
11930 }
11931
7b3cb17a 11932 if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
b99b9ec1 11933 cntl |= MCURSOR_ROTATE_180;
292889e1
VS
11934
11935 return cntl;
11936}
11937
659056f2 11938static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 11939{
024faac7 11940 struct drm_i915_private *dev_priv =
f90a85e7
ML
11941 to_i915(plane_state->uapi.plane->dev);
11942 int width = drm_rect_width(&plane_state->uapi.dst);
11943 int height = drm_rect_height(&plane_state->uapi.dst);
4b0e333e 11944
3637ecf0 11945 if (!intel_cursor_size_ok(plane_state))
659056f2 11946 return false;
4398ad45 11947
024faac7
VS
11948 /* Cursor width is limited to a few power-of-two sizes */
11949 switch (width) {
659056f2
VS
11950 case 256:
11951 case 128:
659056f2
VS
11952 case 64:
11953 break;
11954 default:
11955 return false;
65a21cd6 11956 }
4b0e333e 11957
024faac7
VS
11958 /*
11959 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11960 * height from 8 lines up to the cursor width, when the
11961 * cursor is not rotated. Everything else requires square
11962 * cursors.
11963 */
11964 if (HAS_CUR_FBC(dev_priv) &&
7b3cb17a 11965 plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
11966 if (height < 8 || height > width)
11967 return false;
11968 } else {
11969 if (height != width)
11970 return false;
11971 }
99d1f387 11972
659056f2 11973 return true;
65a21cd6
JB
11974}
11975
eb0f5044 11976static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
659056f2 11977 struct intel_plane_state *plane_state)
cda4b7d3 11978{
f90a85e7 11979 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
659056f2 11980 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a 11981 const struct drm_framebuffer *fb = plane_state->hw.fb;
659056f2 11982 enum pipe pipe = plane->pipe;
659056f2 11983 int ret;
cda4b7d3 11984
659056f2
VS
11985 ret = intel_check_cursor(crtc_state, plane_state);
11986 if (ret)
11987 return ret;
cda4b7d3 11988
659056f2 11989 /* if we want to turn off the cursor ignore width and height */
1e1bb871 11990 if (!fb)
659056f2 11991 return 0;
55a08b3f 11992
659056f2
VS
11993 /* Check for which cursor types we support */
11994 if (!i9xx_cursor_size_ok(plane_state)) {
cd49f818
WK
11995 drm_dbg(&dev_priv->drm,
11996 "Cursor dimension %dx%d not supported\n",
11997 drm_rect_width(&plane_state->uapi.dst),
11998 drm_rect_height(&plane_state->uapi.dst));
659056f2 11999 return -EINVAL;
cda4b7d3 12000 }
cda4b7d3 12001
e57291c2
PB
12002 drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
12003 plane_state->color_plane[0].stride != fb->pitches[0]);
df79cf44 12004
3a612765 12005 if (fb->pitches[0] !=
f90a85e7 12006 drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
cd49f818
WK
12007 drm_dbg_kms(&dev_priv->drm,
12008 "Invalid cursor stride (%u) (cursor width %d)\n",
12009 fb->pitches[0],
12010 drm_rect_width(&plane_state->uapi.dst));
1e1bb871 12011 return -EINVAL;
659056f2 12012 }
dd584fc0 12013
659056f2
VS
12014 /*
12015 * There's something wrong with the cursor on CHV pipe C.
12016 * If it straddles the left edge of the screen then
12017 * moving it away from the edge or disabling it often
12018 * results in a pipe underrun, and often that can lead to
12019 * dead pipe (constant underrun reported, and it scans
12020 * out just a solid color). To recover from that, the
12021 * display power well must be turned off and on again.
12022 * Refuse the put the cursor into that compromised position.
12023 */
12024 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
f90a85e7 12025 plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
cd49f818
WK
12026 drm_dbg_kms(&dev_priv->drm,
12027 "CHV cursor C not allowed to straddle the left screen edge\n");
659056f2
VS
12028 return -EINVAL;
12029 }
5efb3e28 12030
659056f2 12031 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 12032
659056f2 12033 return 0;
cda4b7d3
CW
12034}
12035
b2d03b0d
VS
12036static void i9xx_update_cursor(struct intel_plane *plane,
12037 const struct intel_crtc_state *crtc_state,
55a08b3f 12038 const struct intel_plane_state *plane_state)
dc41c154 12039{
cd5dcbf1
VS
12040 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12041 enum pipe pipe = plane->pipe;
024faac7 12042 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 12043 unsigned long irqflags;
dc41c154 12044
f90a85e7
ML
12045 if (plane_state && plane_state->uapi.visible) {
12046 unsigned width = drm_rect_width(&plane_state->uapi.dst);
12047 unsigned height = drm_rect_height(&plane_state->uapi.dst);
3a612765 12048
7eb31a0b
VS
12049 cntl = plane_state->ctl |
12050 i9xx_cursor_ctl_crtc(crtc_state);
dc41c154 12051
3a612765
ML
12052 if (width != height)
12053 fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
dc41c154 12054
b2d03b0d
VS
12055 base = intel_cursor_base(plane_state);
12056 pos = intel_cursor_position(plane_state);
12057 }
12058
12059 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
12060
e11ffddb
VS
12061 /*
12062 * On some platforms writing CURCNTR first will also
12063 * cause CURPOS to be armed by the CURBASE write.
12064 * Without the CURCNTR write the CURPOS write would
83234d13
VS
12065 * arm itself. Thus we always update CURCNTR before
12066 * CURPOS.
8753d2bc
VS
12067 *
12068 * On other platforms CURPOS always requires the
12069 * CURBASE write to arm the update. Additonally
12070 * a write to any of the cursor register will cancel
12071 * an already armed cursor update. Thus leaving out
12072 * the CURBASE write after CURPOS could lead to a
12073 * cursor that doesn't appear to move, or even change
12074 * shape. Thus we always write CURBASE.
e11ffddb 12075 *
83234d13
VS
12076 * The other registers are armed by by the CURBASE write
12077 * except when the plane is getting enabled at which time
12078 * the CURCNTR write arms the update.
e11ffddb 12079 */
ff43bc37
VS
12080
12081 if (INTEL_GEN(dev_priv) >= 9)
12082 skl_write_cursor_wm(plane, crtc_state);
12083
0bcbcba7
JRS
12084 if (!needs_modeset(crtc_state))
12085 intel_psr2_program_plane_sel_fetch(plane, crtc_state, plane_state, 0);
12086
e11ffddb
VS
12087 if (plane->cursor.base != base ||
12088 plane->cursor.size != fbc_ctl ||
12089 plane->cursor.cntl != cntl) {
e11ffddb 12090 if (HAS_CUR_FBC(dev_priv))
dc008bf0
JN
12091 intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
12092 fbc_ctl);
12093 intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
12094 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
12095 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
75343a44 12096
e11ffddb
VS
12097 plane->cursor.base = base;
12098 plane->cursor.size = fbc_ctl;
12099 plane->cursor.cntl = cntl;
dc41c154 12100 } else {
dc008bf0
JN
12101 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
12102 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
dc41c154
VS
12103 }
12104
b2d03b0d 12105 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
12106}
12107
b2d03b0d 12108static void i9xx_disable_cursor(struct intel_plane *plane,
0dd14be3 12109 const struct intel_crtc_state *crtc_state)
cda4b7d3 12110{
0dd14be3 12111 i9xx_update_cursor(plane, crtc_state, NULL);
dc41c154
VS
12112}
12113
eade6c89
VS
12114static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
12115 enum pipe *pipe)
51f5a096
VS
12116{
12117 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12118 enum intel_display_power_domain power_domain;
0e6e0be4 12119 intel_wakeref_t wakeref;
51f5a096 12120 bool ret;
eade6c89 12121 u32 val;
51f5a096
VS
12122
12123 /*
12124 * Not 100% correct for planes that can move between pipes,
12125 * but that's only the case for gen2-3 which don't have any
12126 * display power wells.
12127 */
eade6c89 12128 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
0e6e0be4
CW
12129 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
12130 if (!wakeref)
51f5a096
VS
12131 return false;
12132
dc008bf0 12133 val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
eade6c89 12134
b99b9ec1 12135 ret = val & MCURSOR_MODE;
eade6c89
VS
12136
12137 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
12138 *pipe = plane->pipe;
12139 else
12140 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
12141 MCURSOR_PIPE_SELECT_SHIFT;
51f5a096 12142
0e6e0be4 12143 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
12144
12145 return ret;
12146}
dc41c154 12147
79e53945 12148/* VESA 640x480x72Hz mode to set on the pipe */
bacdcd55 12149static const struct drm_display_mode load_detect_mode = {
79e53945
JB
12150 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
12151 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
12152};
12153
a8bb6818 12154struct drm_framebuffer *
24dbf51a
CW
12155intel_framebuffer_create(struct drm_i915_gem_object *obj,
12156 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
12157{
12158 struct intel_framebuffer *intel_fb;
12159 int ret;
12160
12161 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 12162 if (!intel_fb)
d2dff872 12163 return ERR_PTR(-ENOMEM);
d2dff872 12164
24dbf51a 12165 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
12166 if (ret)
12167 goto err;
d2dff872
CW
12168
12169 return &intel_fb->base;
dcb1394e 12170
dd4916c5 12171err:
dd4916c5 12172 kfree(intel_fb);
dd4916c5 12173 return ERR_PTR(ret);
d2dff872
CW
12174}
12175
20bdc112
VS
12176static int intel_modeset_disable_planes(struct drm_atomic_state *state,
12177 struct drm_crtc *crtc)
d3a40d1b 12178{
20bdc112 12179 struct drm_plane *plane;
d3a40d1b 12180 struct drm_plane_state *plane_state;
20bdc112 12181 int ret, i;
d3a40d1b 12182
20bdc112 12183 ret = drm_atomic_add_affected_planes(state, crtc);
d3a40d1b
ACO
12184 if (ret)
12185 return ret;
20bdc112
VS
12186
12187 for_each_new_plane_in_state(state, plane, plane_state, i) {
12188 if (plane_state->crtc != crtc)
12189 continue;
12190
12191 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
12192 if (ret)
12193 return ret;
12194
12195 drm_atomic_set_fb_for_plane(plane_state, NULL);
12196 }
d3a40d1b
ACO
12197
12198 return 0;
12199}
12200
6c5ed5ae 12201int intel_get_load_detect_pipe(struct drm_connector *connector,
6c5ed5ae
ML
12202 struct intel_load_detect_pipe *old,
12203 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
12204{
12205 struct intel_crtc *intel_crtc;
d2434ab7 12206 struct intel_encoder *intel_encoder =
43a6d19c 12207 intel_attached_encoder(to_intel_connector(connector));
79e53945 12208 struct drm_crtc *possible_crtc;
4ef69c7a 12209 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
12210 struct drm_crtc *crtc = NULL;
12211 struct drm_device *dev = encoder->dev;
0f0f74bc 12212 struct drm_i915_private *dev_priv = to_i915(dev);
51fd371b 12213 struct drm_mode_config *config = &dev->mode_config;
edde3617 12214 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 12215 struct drm_connector_state *connector_state;
4be07317 12216 struct intel_crtc_state *crtc_state;
51fd371b 12217 int ret, i = -1;
79e53945 12218
cd49f818
WK
12219 drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12220 connector->base.id, connector->name,
12221 encoder->base.id, encoder->name);
d2dff872 12222
edde3617
ML
12223 old->restore_state = NULL;
12224
e57291c2 12225 drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 12226
79e53945
JB
12227 /*
12228 * Algorithm gets a little messy:
7a5e4805 12229 *
79e53945
JB
12230 * - if the connector already has an assigned crtc, use it (but make
12231 * sure it's on first)
7a5e4805 12232 *
79e53945
JB
12233 * - try to find the first unused crtc that can drive this connector,
12234 * and use that if we find one
79e53945
JB
12235 */
12236
12237 /* See if we already have a CRTC for this connector */
edde3617
ML
12238 if (connector->state->crtc) {
12239 crtc = connector->state->crtc;
8261b191 12240
51fd371b 12241 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 12242 if (ret)
ad3c558f 12243 goto fail;
8261b191
CW
12244
12245 /* Make sure the crtc and connector are running */
edde3617 12246 goto found;
79e53945
JB
12247 }
12248
12249 /* Find an unused one (if possible) */
70e1e0ec 12250 for_each_crtc(dev, possible_crtc) {
79e53945
JB
12251 i++;
12252 if (!(encoder->possible_crtcs & (1 << i)))
12253 continue;
edde3617
ML
12254
12255 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
12256 if (ret)
12257 goto fail;
12258
12259 if (possible_crtc->state->enable) {
12260 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 12261 continue;
edde3617 12262 }
a459249c
VS
12263
12264 crtc = possible_crtc;
12265 break;
79e53945
JB
12266 }
12267
12268 /*
12269 * If we didn't find an unused CRTC, don't use any.
12270 */
12271 if (!crtc) {
cd49f818
WK
12272 drm_dbg_kms(&dev_priv->drm,
12273 "no pipe available for load-detect\n");
f4bf77b4 12274 ret = -ENODEV;
ad3c558f 12275 goto fail;
79e53945
JB
12276 }
12277
edde3617
ML
12278found:
12279 intel_crtc = to_intel_crtc(crtc);
12280
83a57153 12281 state = drm_atomic_state_alloc(dev);
edde3617
ML
12282 restore_state = drm_atomic_state_alloc(dev);
12283 if (!state || !restore_state) {
12284 ret = -ENOMEM;
12285 goto fail;
12286 }
83a57153
ACO
12287
12288 state->acquire_ctx = ctx;
edde3617 12289 restore_state->acquire_ctx = ctx;
83a57153 12290
944b0c76
ACO
12291 connector_state = drm_atomic_get_connector_state(state, connector);
12292 if (IS_ERR(connector_state)) {
12293 ret = PTR_ERR(connector_state);
12294 goto fail;
12295 }
12296
edde3617
ML
12297 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
12298 if (ret)
12299 goto fail;
944b0c76 12300
4be07317
ACO
12301 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12302 if (IS_ERR(crtc_state)) {
12303 ret = PTR_ERR(crtc_state);
12304 goto fail;
12305 }
12306
aa42a50a 12307 crtc_state->uapi.active = true;
4be07317 12308
2225f3c6 12309 ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
25f89954 12310 &load_detect_mode);
d3a40d1b
ACO
12311 if (ret)
12312 goto fail;
12313
20bdc112 12314 ret = intel_modeset_disable_planes(state, crtc);
edde3617
ML
12315 if (ret)
12316 goto fail;
12317
12318 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12319 if (!ret)
12320 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
be90cc31
VS
12321 if (!ret)
12322 ret = drm_atomic_add_affected_planes(restore_state, crtc);
edde3617 12323 if (ret) {
cd49f818
WK
12324 drm_dbg_kms(&dev_priv->drm,
12325 "Failed to create a copy of old state to restore: %i\n",
12326 ret);
edde3617
ML
12327 goto fail;
12328 }
8c7b5ccb 12329
3ba86073
ML
12330 ret = drm_atomic_commit(state);
12331 if (ret) {
cd49f818
WK
12332 drm_dbg_kms(&dev_priv->drm,
12333 "failed to set mode on load-detect pipe\n");
412b61d8 12334 goto fail;
79e53945 12335 }
edde3617
ML
12336
12337 old->restore_state = restore_state;
7abbd11f 12338 drm_atomic_state_put(state);
7173188d 12339
79e53945 12340 /* let the connector get through one full cycle before testing */
0f0f74bc 12341 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 12342 return true;
412b61d8 12343
ad3c558f 12344fail:
7fb71c8f
CW
12345 if (state) {
12346 drm_atomic_state_put(state);
12347 state = NULL;
12348 }
12349 if (restore_state) {
12350 drm_atomic_state_put(restore_state);
12351 restore_state = NULL;
12352 }
83a57153 12353
6c5ed5ae
ML
12354 if (ret == -EDEADLK)
12355 return ret;
51fd371b 12356
412b61d8 12357 return false;
79e53945
JB
12358}
12359
d2434ab7 12360void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
12361 struct intel_load_detect_pipe *old,
12362 struct drm_modeset_acquire_ctx *ctx)
79e53945 12363{
d2434ab7 12364 struct intel_encoder *intel_encoder =
43a6d19c 12365 intel_attached_encoder(to_intel_connector(connector));
cd49f818 12366 struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
4ef69c7a 12367 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 12368 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 12369 int ret;
79e53945 12370
cd49f818
WK
12371 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12372 connector->base.id, connector->name,
12373 encoder->base.id, encoder->name);
d2dff872 12374
edde3617 12375 if (!state)
0622a53c 12376 return;
79e53945 12377
581e49fe 12378 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 12379 if (ret)
cd49f818
WK
12380 drm_dbg_kms(&i915->drm,
12381 "Couldn't release load detect pipe: %i\n", ret);
0853695c 12382 drm_atomic_state_put(state);
79e53945
JB
12383}
12384
da4a1efa 12385static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 12386 const struct intel_crtc_state *pipe_config)
da4a1efa 12387{
fac5e23e 12388 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
12389 u32 dpll = pipe_config->dpll_hw_state.dpll;
12390
12391 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 12392 return dev_priv->vbt.lvds_ssc_freq;
6e266956 12393 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 12394 return 120000;
cf819eff 12395 else if (!IS_GEN(dev_priv, 2))
da4a1efa
VS
12396 return 96000;
12397 else
12398 return 48000;
12399}
12400
79e53945 12401/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 12402static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 12403 struct intel_crtc_state *pipe_config)
79e53945 12404{
f1f644dc 12405 struct drm_device *dev = crtc->base.dev;
fac5e23e 12406 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 12407 enum pipe pipe = crtc->pipe;
293623f7 12408 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 12409 u32 fp;
9e2c8475 12410 struct dpll clock;
dccbea3b 12411 int port_clock;
da4a1efa 12412 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
12413
12414 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 12415 fp = pipe_config->dpll_hw_state.fp0;
79e53945 12416 else
293623f7 12417 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
12418
12419 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 12420 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
12421 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12422 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
12423 } else {
12424 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12425 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12426 }
12427
cf819eff 12428 if (!IS_GEN(dev_priv, 2)) {
9b1e14f4 12429 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
12430 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12431 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
12432 else
12433 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
12434 DPLL_FPA01_P1_POST_DIV_SHIFT);
12435
12436 switch (dpll & DPLL_MODE_MASK) {
12437 case DPLLB_MODE_DAC_SERIAL:
12438 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12439 5 : 10;
12440 break;
12441 case DPLLB_MODE_LVDS:
12442 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12443 7 : 14;
12444 break;
12445 default:
cd49f818
WK
12446 drm_dbg_kms(&dev_priv->drm,
12447 "Unknown DPLL mode %08x in programmed "
12448 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 12449 return;
79e53945
JB
12450 }
12451
9b1e14f4 12452 if (IS_PINEVIEW(dev_priv))
dccbea3b 12453 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 12454 else
dccbea3b 12455 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 12456 } else {
dc008bf0
JN
12457 u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12458 LVDS);
b1c560d1 12459 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
12460
12461 if (is_lvds) {
12462 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12463 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
12464
12465 if (lvds & LVDS_CLKB_POWER_UP)
12466 clock.p2 = 7;
12467 else
12468 clock.p2 = 14;
79e53945
JB
12469 } else {
12470 if (dpll & PLL_P1_DIVIDE_BY_TWO)
12471 clock.p1 = 2;
12472 else {
12473 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12474 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12475 }
12476 if (dpll & PLL_P2_DIVIDE_BY_4)
12477 clock.p2 = 4;
12478 else
12479 clock.p2 = 2;
79e53945 12480 }
da4a1efa 12481
dccbea3b 12482 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
12483 }
12484
18442d08
VS
12485 /*
12486 * This value includes pixel_multiplier. We will use
241bfc38 12487 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
12488 * encoder's get_config() function.
12489 */
dccbea3b 12490 pipe_config->port_clock = port_clock;
f1f644dc
JB
12491}
12492
6878da05
VS
12493int intel_dotclock_calculate(int link_freq,
12494 const struct intel_link_m_n *m_n)
f1f644dc 12495{
f1f644dc
JB
12496 /*
12497 * The calculation for the data clock is:
1041a02f 12498 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 12499 * But we want to avoid losing precison if possible, so:
1041a02f 12500 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
12501 *
12502 * and the link clock is simpler:
1041a02f 12503 * link_clock = (m * link_clock) / n
f1f644dc
JB
12504 */
12505
6878da05
VS
12506 if (!m_n->link_n)
12507 return 0;
f1f644dc 12508
3123698f 12509 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6878da05 12510}
f1f644dc 12511
9eae5e27
LDM
12512static void ilk_pch_clock_get(struct intel_crtc *crtc,
12513 struct intel_crtc_state *pipe_config)
6878da05 12514{
e3b247da 12515 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 12516
18442d08
VS
12517 /* read out port_clock from the DPLL */
12518 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 12519
f1f644dc 12520 /*
e3b247da
VS
12521 * In case there is an active pipe without active ports,
12522 * we may need some idea for the dotclock anyway.
12523 * Calculate one based on the FDI configuration.
79e53945 12524 */
1326a92c 12525 pipe_config->hw.adjusted_mode.crtc_clock =
21a727b3 12526 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 12527 &pipe_config->fdi_m_n);
79e53945
JB
12528}
12529
979e94c1
VS
12530static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12531 struct intel_crtc *crtc)
12532{
12533 memset(crtc_state, 0, sizeof(*crtc_state));
12534
12535 __drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12536
12537 crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12538 crtc_state->master_transcoder = INVALID_TRANSCODER;
12539 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12540 crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12541 crtc_state->scaler_state.scaler_id = -1;
6671c367 12542 crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
979e94c1
VS
12543}
12544
216383e9
VS
12545static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12546{
12547 struct intel_crtc_state *crtc_state;
12548
12549 crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12550
12551 if (crtc_state)
12552 intel_crtc_state_reset(crtc_state, crtc);
12553
12554 return crtc_state;
12555}
12556
de330815
VS
12557/* Returns the currently programmed mode of the given encoder. */
12558struct drm_display_mode *
12559intel_encoder_current_mode(struct intel_encoder *encoder)
79e53945 12560{
de330815
VS
12561 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12562 struct intel_crtc_state *crtc_state;
79e53945 12563 struct drm_display_mode *mode;
de330815
VS
12564 struct intel_crtc *crtc;
12565 enum pipe pipe;
12566
12567 if (!encoder->get_hw_state(encoder, &pipe))
12568 return NULL;
12569
12570 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
79e53945
JB
12571
12572 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12573 if (!mode)
12574 return NULL;
12575
216383e9 12576 crtc_state = intel_crtc_state_alloc(crtc);
de330815 12577 if (!crtc_state) {
3f36b937
TU
12578 kfree(mode);
12579 return NULL;
12580 }
12581
11f9af16 12582 if (!intel_crtc_get_pipe_config(crtc_state)) {
de330815
VS
12583 kfree(crtc_state);
12584 kfree(mode);
12585 return NULL;
12586 }
e30a154b 12587
65c1ed30 12588 intel_encoder_get_config(encoder, crtc_state);
79e53945 12589
33574ec9 12590 intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
79e53945 12591
de330815 12592 kfree(crtc_state);
3f36b937 12593
79e53945
JB
12594 return mode;
12595}
12596
12597static void intel_crtc_destroy(struct drm_crtc *crtc)
12598{
12599 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12600
12601 drm_crtc_cleanup(crtc);
12602 kfree(intel_crtc);
12603}
12604
5a21b665
DV
12605/**
12606 * intel_wm_need_update - Check whether watermarks need updating
6bf19817
CW
12607 * @cur: current plane state
12608 * @new: new plane state
5a21b665
DV
12609 *
12610 * Check current plane state versus the new one to determine whether
12611 * watermarks need to be recalculated.
12612 *
12613 * Returns true or false.
12614 */
4f25720b 12615static bool intel_wm_need_update(const struct intel_plane_state *cur,
cd1d3ee9 12616 struct intel_plane_state *new)
5a21b665 12617{
5a21b665 12618 /* Update watermarks on tiling or size changes. */
f90a85e7 12619 if (new->uapi.visible != cur->uapi.visible)
5a21b665
DV
12620 return true;
12621
7b3cb17a 12622 if (!cur->hw.fb || !new->hw.fb)
5a21b665
DV
12623 return false;
12624
7b3cb17a
ML
12625 if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12626 cur->hw.rotation != new->hw.rotation ||
f90a85e7
ML
12627 drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12628 drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12629 drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12630 drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
5a21b665
DV
12631 return true;
12632
12633 return false;
12634}
12635
b2b55502 12636static bool needs_scaling(const struct intel_plane_state *state)
5a21b665 12637{
f90a85e7
ML
12638 int src_w = drm_rect_width(&state->uapi.src) >> 16;
12639 int src_h = drm_rect_height(&state->uapi.src) >> 16;
12640 int dst_w = drm_rect_width(&state->uapi.dst);
12641 int dst_h = drm_rect_height(&state->uapi.dst);
5a21b665
DV
12642
12643 return (src_w != dst_w || src_h != dst_h);
12644}
d21fbe87 12645
b2b55502 12646int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
4f25720b 12647 struct intel_crtc_state *crtc_state,
b2b55502 12648 const struct intel_plane_state *old_plane_state,
4f25720b 12649 struct intel_plane_state *plane_state)
da20eabd 12650{
2225f3c6 12651 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
f90a85e7 12652 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4f25720b
ML
12653 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12654 bool mode_changed = needs_modeset(crtc_state);
1326a92c
ML
12655 bool was_crtc_enabled = old_crtc_state->hw.active;
12656 bool is_crtc_enabled = crtc_state->hw.active;
da20eabd 12657 bool turn_off, turn_on, visible, was_visible;
78108b7c 12658 int ret;
da20eabd 12659
e9728bd8 12660 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
4f25720b 12661 ret = skl_update_scaler_plane(crtc_state, plane_state);
da20eabd
ML
12662 if (ret)
12663 return ret;
12664 }
12665
f90a85e7
ML
12666 was_visible = old_plane_state->uapi.visible;
12667 visible = plane_state->uapi.visible;
da20eabd 12668
e57291c2 12669 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
da20eabd
ML
12670 was_visible = false;
12671
35c08f43
ML
12672 /*
12673 * Visibility is calculated as if the crtc was on, but
12674 * after scaler setup everything depends on it being off
12675 * when the crtc isn't active.
f818ffea
VS
12676 *
12677 * FIXME this is wrong for watermarks. Watermarks should also
12678 * be computed as if the pipe would be active. Perhaps move
12679 * per-plane wm computation to the .check_plane() hook, and
12680 * only combine the results from all planes in the current place?
35c08f43 12681 */
e9728bd8 12682 if (!is_crtc_enabled) {
cb1824bb
VS
12683 intel_plane_set_invisible(crtc_state, plane_state);
12684 visible = false;
e9728bd8 12685 }
da20eabd
ML
12686
12687 if (!was_visible && !visible)
12688 return 0;
12689
12690 turn_off = was_visible && (!visible || mode_changed);
12691 turn_on = visible && (!was_visible || mode_changed);
12692
cd49f818
WK
12693 drm_dbg_atomic(&dev_priv->drm,
12694 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12695 crtc->base.base.id, crtc->base.name,
12696 plane->base.base.id, plane->base.name,
12697 was_visible, visible,
12698 turn_off, turn_on, mode_changed);
da20eabd 12699
caed361d 12700 if (turn_on) {
04548cba 12701 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
4f25720b 12702 crtc_state->update_wm_pre = true;
caed361d
VS
12703
12704 /* must disable cxsr around plane enable/disable */
e9728bd8 12705 if (plane->id != PLANE_CURSOR)
4f25720b 12706 crtc_state->disable_cxsr = true;
caed361d 12707 } else if (turn_off) {
04548cba 12708 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
4f25720b 12709 crtc_state->update_wm_post = true;
92826fcd 12710
852eb00d 12711 /* must disable cxsr around plane enable/disable */
e9728bd8 12712 if (plane->id != PLANE_CURSOR)
4f25720b
ML
12713 crtc_state->disable_cxsr = true;
12714 } else if (intel_wm_need_update(old_plane_state, plane_state)) {
04548cba 12715 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df 12716 /* FIXME bollocks */
4f25720b
ML
12717 crtc_state->update_wm_pre = true;
12718 crtc_state->update_wm_post = true;
b4ede6df 12719 }
852eb00d 12720 }
da20eabd 12721
8be6ca85 12722 if (visible || was_visible)
4f25720b 12723 crtc_state->fb_bits |= plane->frontbuffer_bit;
a9ff8714 12724
31ae71fc 12725 /*
8e7a4424
VS
12726 * ILK/SNB DVSACNTR/Sprite Enable
12727 * IVB SPR_CTL/Sprite Enable
12728 * "When in Self Refresh Big FIFO mode, a write to enable the
12729 * plane will be internally buffered and delayed while Big FIFO
12730 * mode is exiting."
12731 *
12732 * Which means that enabling the sprite can take an extra frame
12733 * when we start in big FIFO mode (LP1+). Thus we need to drop
12734 * down to LP0 and wait for vblank in order to make sure the
12735 * sprite gets enabled on the next vblank after the register write.
12736 * Doing otherwise would risk enabling the sprite one frame after
12737 * we've already signalled flip completion. We can resume LP1+
12738 * once the sprite has been enabled.
12739 *
12740 *
31ae71fc 12741 * WaCxSRDisabledForSpriteScaling:ivb
8e7a4424
VS
12742 * IVB SPR_SCALE/Scaling Enable
12743 * "Low Power watermarks must be disabled for at least one
12744 * frame before enabling sprite scaling, and kept disabled
12745 * until sprite scaling is disabled."
12746 *
12747 * ILK/SNB DVSASCALE/Scaling Enable
12748 * "When in Self Refresh Big FIFO mode, scaling enable will be
12749 * masked off while Big FIFO mode is exiting."
31ae71fc 12750 *
8e7a4424
VS
12751 * Despite the w/a only being listed for IVB we assume that
12752 * the ILK/SNB note has similar ramifications, hence we apply
12753 * the w/a on all three platforms.
d8af3270
JPH
12754 *
12755 * With experimental results seems this is needed also for primary
12756 * plane, not only sprite plane.
31ae71fc 12757 */
d8af3270 12758 if (plane->id != PLANE_CURSOR &&
f3ce44a0 12759 (IS_GEN_RANGE(dev_priv, 5, 6) ||
8e7a4424
VS
12760 IS_IVYBRIDGE(dev_priv)) &&
12761 (turn_on || (!needs_scaling(old_plane_state) &&
4f25720b
ML
12762 needs_scaling(plane_state))))
12763 crtc_state->disable_lp_wm = true;
d21fbe87 12764
da20eabd
ML
12765 return 0;
12766}
12767
6d3a1ce7
ML
12768static bool encoders_cloneable(const struct intel_encoder *a,
12769 const struct intel_encoder *b)
12770{
12771 /* masks could be asymmetric, so check both ways */
12772 return a == b || (a->cloneable & (1 << b->type) &&
12773 b->cloneable & (1 << a->type));
12774}
12775
aa07c1d3 12776static bool check_single_encoder_cloning(struct intel_atomic_state *state,
6d3a1ce7
ML
12777 struct intel_crtc *crtc,
12778 struct intel_encoder *encoder)
12779{
12780 struct intel_encoder *source_encoder;
12781 struct drm_connector *connector;
12782 struct drm_connector_state *connector_state;
12783 int i;
12784
aa07c1d3 12785 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
6d3a1ce7
ML
12786 if (connector_state->crtc != &crtc->base)
12787 continue;
12788
12789 source_encoder =
12790 to_intel_encoder(connector_state->best_encoder);
12791 if (!encoders_cloneable(encoder, source_encoder))
12792 return false;
12793 }
12794
12795 return true;
12796}
12797
1ab554b0
ML
12798static int icl_add_linked_planes(struct intel_atomic_state *state)
12799{
12800 struct intel_plane *plane, *linked;
12801 struct intel_plane_state *plane_state, *linked_plane_state;
12802 int i;
12803
12804 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
c47b7ddb 12805 linked = plane_state->planar_linked_plane;
1ab554b0
ML
12806
12807 if (!linked)
12808 continue;
12809
12810 linked_plane_state = intel_atomic_get_plane_state(state, linked);
12811 if (IS_ERR(linked_plane_state))
12812 return PTR_ERR(linked_plane_state);
12813
ce04ecd9
PB
12814 drm_WARN_ON(state->base.dev,
12815 linked_plane_state->planar_linked_plane != plane);
12816 drm_WARN_ON(state->base.dev,
12817 linked_plane_state->planar_slave == plane_state->planar_slave);
1ab554b0
ML
12818 }
12819
12820 return 0;
12821}
12822
12823static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12824{
2225f3c6 12825 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1ab554b0 12826 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2225f3c6 12827 struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
1ab554b0
ML
12828 struct intel_plane *plane, *linked;
12829 struct intel_plane_state *plane_state;
12830 int i;
12831
12832 if (INTEL_GEN(dev_priv) < 11)
12833 return 0;
12834
12835 /*
12836 * Destroy all old plane links and make the slave plane invisible
12837 * in the crtc_state->active_planes mask.
12838 */
12839 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
c47b7ddb 12840 if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
1ab554b0
ML
12841 continue;
12842
c47b7ddb 12843 plane_state->planar_linked_plane = NULL;
f90a85e7 12844 if (plane_state->planar_slave && !plane_state->uapi.visible) {
1ab554b0 12845 crtc_state->active_planes &= ~BIT(plane->id);
afbd8a72
VS
12846 crtc_state->update_planes |= BIT(plane->id);
12847 }
1ab554b0 12848
c47b7ddb 12849 plane_state->planar_slave = false;
1ab554b0
ML
12850 }
12851
12852 if (!crtc_state->nv12_planes)
12853 return 0;
12854
12855 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12856 struct intel_plane_state *linked_state = NULL;
12857
12858 if (plane->pipe != crtc->pipe ||
12859 !(crtc_state->nv12_planes & BIT(plane->id)))
12860 continue;
12861
12862 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
99e2d8bc 12863 if (!icl_is_nv12_y_plane(dev_priv, linked->id))
1ab554b0
ML
12864 continue;
12865
12866 if (crtc_state->active_planes & BIT(linked->id))
12867 continue;
12868
12869 linked_state = intel_atomic_get_plane_state(state, linked);
12870 if (IS_ERR(linked_state))
12871 return PTR_ERR(linked_state);
12872
12873 break;
12874 }
12875
12876 if (!linked_state) {
cd49f818
WK
12877 drm_dbg_kms(&dev_priv->drm,
12878 "Need %d free Y planes for planar YUV\n",
12879 hweight8(crtc_state->nv12_planes));
1ab554b0
ML
12880
12881 return -EINVAL;
12882 }
12883
c47b7ddb 12884 plane_state->planar_linked_plane = linked;
1ab554b0 12885
c47b7ddb
ML
12886 linked_state->planar_slave = true;
12887 linked_state->planar_linked_plane = plane;
1ab554b0 12888 crtc_state->active_planes |= BIT(linked->id);
afbd8a72 12889 crtc_state->update_planes |= BIT(linked->id);
cd49f818
WK
12890 drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12891 linked->base.name, plane->base.name);
1f594b20
ML
12892
12893 /* Copy parameters to slave plane */
12894 linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12895 linked_state->color_ctl = plane_state->color_ctl;
103605e0 12896 linked_state->view = plane_state->view;
320625aa
ID
12897 memcpy(linked_state->color_plane, plane_state->color_plane,
12898 sizeof(linked_state->color_plane));
1f594b20 12899
e85e7458 12900 intel_plane_copy_hw_state(linked_state, plane_state);
1f594b20
ML
12901 linked_state->uapi.src = plane_state->uapi.src;
12902 linked_state->uapi.dst = plane_state->uapi.dst;
12903
12904 if (icl_is_hdr_plane(dev_priv, plane->id)) {
12905 if (linked->id == PLANE_SPRITE5)
12906 plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12907 else if (linked->id == PLANE_SPRITE4)
12908 plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
99e2d8bc
MR
12909 else if (linked->id == PLANE_SPRITE3)
12910 plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
12911 else if (linked->id == PLANE_SPRITE2)
12912 plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
1f594b20
ML
12913 else
12914 MISSING_CASE(linked->id);
12915 }
1ab554b0
ML
12916 }
12917
12918 return 0;
12919}
12920
638d87c4
VS
12921static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12922{
2225f3c6 12923 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
638d87c4 12924 struct intel_atomic_state *state =
2225f3c6 12925 to_intel_atomic_state(new_crtc_state->uapi.state);
638d87c4
VS
12926 const struct intel_crtc_state *old_crtc_state =
12927 intel_atomic_get_old_crtc_state(state, crtc);
12928
12929 return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12930}
12931
6dcde047
VS
12932static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12933{
bafcdad6
ML
12934 const struct drm_display_mode *pipe_mode =
12935 &crtc_state->hw.pipe_mode;
4003dac1 12936 int linetime_wm;
6dcde047
VS
12937
12938 if (!crtc_state->hw.enable)
12939 return 0;
12940
bafcdad6
ML
12941 linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
12942 pipe_mode->crtc_clock);
4003dac1
VS
12943
12944 return min(linetime_wm, 0x1ff);
6dcde047
VS
12945}
12946
28a30b45
VS
12947static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12948 const struct intel_cdclk_state *cdclk_state)
6dcde047 12949{
bafcdad6
ML
12950 const struct drm_display_mode *pipe_mode =
12951 &crtc_state->hw.pipe_mode;
4003dac1 12952 int linetime_wm;
6dcde047
VS
12953
12954 if (!crtc_state->hw.enable)
12955 return 0;
12956
bafcdad6 12957 linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4003dac1
VS
12958 cdclk_state->logical.cdclk);
12959
12960 return min(linetime_wm, 0x1ff);
6dcde047
VS
12961}
12962
12963static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12964{
12965 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12966 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bafcdad6
ML
12967 const struct drm_display_mode *pipe_mode =
12968 &crtc_state->hw.pipe_mode;
4003dac1 12969 int linetime_wm;
6dcde047
VS
12970
12971 if (!crtc_state->hw.enable)
12972 return 0;
12973
bafcdad6 12974 linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
6dcde047
VS
12975 crtc_state->pixel_rate);
12976
12977 /* Display WA #1135: BXT:ALL GLK:ALL */
12978 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12979 linetime_wm /= 2;
12980
4003dac1 12981 return min(linetime_wm, 0x1ff);
6dcde047
VS
12982}
12983
28a30b45
VS
12984static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12985 struct intel_crtc *crtc)
12986{
12987 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12988 struct intel_crtc_state *crtc_state =
12989 intel_atomic_get_new_crtc_state(state, crtc);
12990 const struct intel_cdclk_state *cdclk_state;
12991
12992 if (INTEL_GEN(dev_priv) >= 9)
12993 crtc_state->linetime = skl_linetime_wm(crtc_state);
12994 else
12995 crtc_state->linetime = hsw_linetime_wm(crtc_state);
12996
12997 if (!hsw_crtc_supports_ips(crtc))
12998 return 0;
12999
13000 cdclk_state = intel_atomic_get_cdclk_state(state);
13001 if (IS_ERR(cdclk_state))
13002 return PTR_ERR(cdclk_state);
13003
13004 crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
13005 cdclk_state);
13006
13007 return 0;
13008}
13009
131d3b1a
VS
13010static int intel_crtc_atomic_check(struct intel_atomic_state *state,
13011 struct intel_crtc *crtc)
6d3a1ce7 13012{
2e7f76c1
VS
13013 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13014 struct intel_crtc_state *crtc_state =
131d3b1a 13015 intel_atomic_get_new_crtc_state(state, crtc);
2e7f76c1 13016 bool mode_changed = needs_modeset(crtc_state);
131d3b1a 13017 int ret;
6d3a1ce7 13018
440e84a5 13019 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
1326a92c 13020 mode_changed && !crtc_state->hw.active)
2e7f76c1 13021 crtc_state->update_wm_post = true;
eddfcbcd 13022
1326a92c 13023 if (mode_changed && crtc_state->hw.enable &&
ad421372 13024 dev_priv->display.crtc_compute_clock &&
19f65a3d 13025 !crtc_state->bigjoiner_slave &&
e57291c2 13026 !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
2e7f76c1 13027 ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
ad421372
ML
13028 if (ret)
13029 return ret;
13030 }
13031
638d87c4
VS
13032 /*
13033 * May need to update pipe gamma enable bits
13034 * when C8 planes are getting enabled/disabled.
13035 */
2e7f76c1 13036 if (c8_planes_changed(crtc_state))
2225f3c6 13037 crtc_state->uapi.color_mgmt_changed = true;
638d87c4 13038
2e7f76c1 13039 if (mode_changed || crtc_state->update_pipe ||
2225f3c6 13040 crtc_state->uapi.color_mgmt_changed) {
2e7f76c1 13041 ret = intel_color_check(crtc_state);
82cf435b
LL
13042 if (ret)
13043 return ret;
13044 }
13045
86c8bbbe 13046 if (dev_priv->display.compute_pipe_wm) {
2e7f76c1 13047 ret = dev_priv->display.compute_pipe_wm(crtc_state);
ed4a6a7c 13048 if (ret) {
cd49f818
WK
13049 drm_dbg_kms(&dev_priv->drm,
13050 "Target pipe watermarks are invalid\n");
ed4a6a7c
MR
13051 return ret;
13052 }
13053 }
13054
f255c624 13055 if (dev_priv->display.compute_intermediate_wm) {
e57291c2
PB
13056 if (drm_WARN_ON(&dev_priv->drm,
13057 !dev_priv->display.compute_pipe_wm))
ed4a6a7c
MR
13058 return 0;
13059
13060 /*
13061 * Calculate 'intermediate' watermarks that satisfy both the
13062 * old state and the new state. We can program these
13063 * immediately.
13064 */
2e7f76c1 13065 ret = dev_priv->display.compute_intermediate_wm(crtc_state);
ed4a6a7c 13066 if (ret) {
cd49f818
WK
13067 drm_dbg_kms(&dev_priv->drm,
13068 "No valid intermediate pipe watermarks are possible\n");
86c8bbbe 13069 return ret;
ed4a6a7c 13070 }
86c8bbbe
MR
13071 }
13072
6315b5d3 13073 if (INTEL_GEN(dev_priv) >= 9) {
40d42793 13074 if (mode_changed || crtc_state->update_pipe) {
2e7f76c1 13075 ret = skl_update_scaler_crtc(crtc_state);
40d42793
VS
13076 if (ret)
13077 return ret;
13078 }
13079
13080 ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
28a30b45
VS
13081 if (ret)
13082 return ret;
e435d6e5
ML
13083 }
13084
28a30b45
VS
13085 if (HAS_IPS(dev_priv)) {
13086 ret = hsw_compute_ips_config(crtc_state);
13087 if (ret)
13088 return ret;
13089 }
13090
13091 if (INTEL_GEN(dev_priv) >= 9 ||
13092 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
13093 ret = hsw_compute_linetime_wm(state, crtc);
13094 if (ret)
13095 return ret;
24f28450 13096
6dcde047
VS
13097 }
13098
0bcbcba7
JRS
13099 if (!mode_changed) {
13100 ret = intel_psr2_sel_fetch_update(state, crtc);
13101 if (ret)
13102 return ret;
13103 }
6e43e276 13104
28a30b45 13105 return 0;
6d3a1ce7
ML
13106}
13107
d29b2f9d
ACO
13108static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
13109{
13110 struct intel_connector *connector;
f9e905ca 13111 struct drm_connector_list_iter conn_iter;
d29b2f9d 13112
f9e905ca
DV
13113 drm_connector_list_iter_begin(dev, &conn_iter);
13114 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f 13115 if (connector->base.state->crtc)
ef196b5c 13116 drm_connector_put(&connector->base);
8863dc7f 13117
d29b2f9d
ACO
13118 if (connector->base.encoder) {
13119 connector->base.state->best_encoder =
13120 connector->base.encoder;
13121 connector->base.state->crtc =
13122 connector->base.encoder->crtc;
8863dc7f 13123
ef196b5c 13124 drm_connector_get(&connector->base);
d29b2f9d
ACO
13125 } else {
13126 connector->base.state->best_encoder = NULL;
13127 connector->base.state->crtc = NULL;
13128 }
13129 }
f9e905ca 13130 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
13131}
13132
f1a12172 13133static int
bcce8d86
VS
13134compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
13135 struct intel_crtc_state *pipe_config)
050f7aeb 13136{
bcce8d86 13137 struct drm_connector *connector = conn_state->connector;
cd49f818 13138 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
bcce8d86 13139 const struct drm_display_info *info = &connector->display_info;
f1a12172 13140 int bpp;
050f7aeb 13141
f1a12172
RS
13142 switch (conn_state->max_bpc) {
13143 case 6 ... 7:
13144 bpp = 6 * 3;
13145 break;
13146 case 8 ... 9:
13147 bpp = 8 * 3;
13148 break;
13149 case 10 ... 11:
13150 bpp = 10 * 3;
13151 break;
2ca5a7b8 13152 case 12 ... 16:
f1a12172
RS
13153 bpp = 12 * 3;
13154 break;
13155 default:
2ca5a7b8 13156 MISSING_CASE(conn_state->max_bpc);
f1a12172 13157 return -EINVAL;
050f7aeb
DV
13158 }
13159
f1a12172 13160 if (bpp < pipe_config->pipe_bpp) {
cd49f818
WK
13161 drm_dbg_kms(&i915->drm,
13162 "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
13163 "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
13164 connector->base.id, connector->name,
13165 bpp, 3 * info->bpc,
13166 3 * conn_state->max_requested_bpc,
13167 pipe_config->pipe_bpp);
bcce8d86 13168
f1a12172 13169 pipe_config->pipe_bpp = bpp;
050f7aeb 13170 }
bcce8d86 13171
f1a12172 13172 return 0;
050f7aeb
DV
13173}
13174
4e53c2e0 13175static int
050f7aeb 13176compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 13177 struct intel_crtc_state *pipe_config)
4e53c2e0 13178{
9beb5fea 13179 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2225f3c6 13180 struct drm_atomic_state *state = pipe_config->uapi.state;
da3ced29
ACO
13181 struct drm_connector *connector;
13182 struct drm_connector_state *connector_state;
1486017f 13183 int bpp, i;
4e53c2e0 13184
9beb5fea
TU
13185 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
13186 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 13187 bpp = 10*3;
9beb5fea 13188 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
13189 bpp = 12*3;
13190 else
13191 bpp = 8*3;
13192
4e53c2e0
DV
13193 pipe_config->pipe_bpp = bpp;
13194
bcce8d86 13195 /* Clamp display bpp to connector max bpp */
aa5e9b47 13196 for_each_new_connector_in_state(state, connector, connector_state, i) {
bcce8d86
VS
13197 int ret;
13198
da3ced29 13199 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
13200 continue;
13201
bcce8d86
VS
13202 ret = compute_sink_pipe_bpp(connector_state, pipe_config);
13203 if (ret)
13204 return ret;
4e53c2e0
DV
13205 }
13206
bcce8d86 13207 return 0;
4e53c2e0
DV
13208}
13209
3dfd8d71
JN
13210static void intel_dump_crtc_timings(struct drm_i915_private *i915,
13211 const struct drm_display_mode *mode)
644db711 13212{
3dfd8d71
JN
13213 drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
13214 "type: 0x%x flags: 0x%x\n",
13215 mode->crtc_clock,
13216 mode->crtc_hdisplay, mode->crtc_hsync_start,
13217 mode->crtc_hsync_end, mode->crtc_htotal,
13218 mode->crtc_vdisplay, mode->crtc_vsync_start,
13219 mode->crtc_vsync_end, mode->crtc_vtotal,
13220 mode->type, mode->flags);
644db711
DV
13221}
13222
81b55ef1 13223static void
926878fb
VS
13224intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
13225 const char *id, unsigned int lane_count,
13226 const struct intel_link_m_n *m_n)
f6982332 13227{
cd49f818
WK
13228 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13229
13230 drm_dbg_kms(&i915->drm,
13231 "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
13232 id, lane_count,
13233 m_n->gmch_m, m_n->gmch_n,
13234 m_n->link_m, m_n->link_n, m_n->tu);
f6982332
TU
13235}
13236
69e89032
VS
13237static void
13238intel_dump_infoframe(struct drm_i915_private *dev_priv,
13239 const union hdmi_infoframe *frame)
13240{
bdbf43d7 13241 if (!drm_debug_enabled(DRM_UT_KMS))
69e89032
VS
13242 return;
13243
13244 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
13245}
13246
42890250
GM
13247static void
13248intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
13249 const struct drm_dp_vsc_sdp *vsc)
13250{
13251 if (!drm_debug_enabled(DRM_UT_KMS))
13252 return;
13253
13254 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
13255}
13256
40b2be41
VS
13257#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
13258
13259static const char * const output_type_str[] = {
13260 OUTPUT_TYPE(UNUSED),
13261 OUTPUT_TYPE(ANALOG),
13262 OUTPUT_TYPE(DVO),
13263 OUTPUT_TYPE(SDVO),
13264 OUTPUT_TYPE(LVDS),
13265 OUTPUT_TYPE(TVOUT),
13266 OUTPUT_TYPE(HDMI),
13267 OUTPUT_TYPE(DP),
13268 OUTPUT_TYPE(EDP),
13269 OUTPUT_TYPE(DSI),
7e732cac 13270 OUTPUT_TYPE(DDI),
40b2be41
VS
13271 OUTPUT_TYPE(DP_MST),
13272};
13273
13274#undef OUTPUT_TYPE
13275
13276static void snprintf_output_types(char *buf, size_t len,
13277 unsigned int output_types)
13278{
13279 char *str = buf;
13280 int i;
13281
13282 str[0] = '\0';
13283
13284 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
13285 int r;
13286
13287 if ((output_types & BIT(i)) == 0)
13288 continue;
13289
13290 r = snprintf(str, len, "%s%s",
13291 str != buf ? "," : "", output_type_str[i]);
13292 if (r >= len)
13293 break;
13294 str += r;
13295 len -= r;
13296
13297 output_types &= ~BIT(i);
13298 }
13299
13300 WARN_ON_ONCE(output_types != 0);
13301}
13302
d9facae6
SS
13303static const char * const output_format_str[] = {
13304 [INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
13305 [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
33b7f3ee 13306 [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
8c79f844 13307 [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
d9facae6
SS
13308};
13309
13310static const char *output_formats(enum intel_output_format format)
13311{
33b7f3ee 13312 if (format >= ARRAY_SIZE(output_format_str))
d9facae6
SS
13313 format = INTEL_OUTPUT_FORMAT_INVALID;
13314 return output_format_str[format];
13315}
13316
10d75f54
VS
13317static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
13318{
f90a85e7 13319 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
cd49f818 13320 struct drm_i915_private *i915 = to_i915(plane->base.dev);
7b3cb17a 13321 const struct drm_framebuffer *fb = plane_state->hw.fb;
10d75f54
VS
13322 struct drm_format_name_buf format_name;
13323
13324 if (!fb) {
cd49f818
WK
13325 drm_dbg_kms(&i915->drm,
13326 "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
13327 plane->base.base.id, plane->base.name,
13328 yesno(plane_state->uapi.visible));
10d75f54
VS
13329 return;
13330 }
13331
cd49f818 13332 drm_dbg_kms(&i915->drm,
54defc10 13333 "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s modifier = 0x%llx, visible: %s\n",
cd49f818
WK
13334 plane->base.base.id, plane->base.name,
13335 fb->base.id, fb->width, fb->height,
13336 drm_get_format_name(fb->format->format, &format_name),
54defc10 13337 fb->modifier, yesno(plane_state->uapi.visible));
cd49f818
WK
13338 drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13339 plane_state->hw.rotation, plane_state->scaler_id);
f90a85e7 13340 if (plane_state->uapi.visible)
cd49f818
WK
13341 drm_dbg_kms(&i915->drm,
13342 "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13343 DRM_RECT_FP_ARG(&plane_state->uapi.src),
13344 DRM_RECT_ARG(&plane_state->uapi.dst));
10d75f54
VS
13345}
13346
926878fb 13347static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
10d75f54 13348 struct intel_atomic_state *state,
c0b03411
DV
13349 const char *context)
13350{
2225f3c6 13351 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
10d75f54
VS
13352 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13353 const struct intel_plane_state *plane_state;
13354 struct intel_plane *plane;
40b2be41 13355 char buf[64];
10d75f54 13356 int i;
6a60cd87 13357
cd49f818
WK
13358 drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13359 crtc->base.base.id, crtc->base.name,
13360 yesno(pipe_config->hw.enable), context);
c0b03411 13361
1326a92c 13362 if (!pipe_config->hw.enable)
10d75f54
VS
13363 goto dump_planes;
13364
40b2be41 13365 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
cd49f818
WK
13366 drm_dbg_kms(&dev_priv->drm,
13367 "active: %s, output_types: %s (0x%x), output format: %s\n",
13368 yesno(pipe_config->hw.active),
13369 buf, pipe_config->output_types,
13370 output_formats(pipe_config->output_format));
d9facae6 13371
cd49f818
WK
13372 drm_dbg_kms(&dev_priv->drm,
13373 "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13374 transcoder_name(pipe_config->cpu_transcoder),
13375 pipe_config->pipe_bpp, pipe_config->dither);
a4309657 13376
cf52acde
VS
13377 drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13378 transcoder_name(pipe_config->mst_master_transcoder));
13379
05d756b0
VS
13380 drm_dbg_kms(&dev_priv->drm,
13381 "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
13382 transcoder_name(pipe_config->master_transcoder),
13383 pipe_config->sync_mode_slaves_mask);
13384
a4309657
TU
13385 if (pipe_config->has_pch_encoder)
13386 intel_dump_m_n_config(pipe_config, "fdi",
13387 pipe_config->fdi_lanes,
13388 &pipe_config->fdi_m_n);
f6982332
TU
13389
13390 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
13391 intel_dump_m_n_config(pipe_config, "dp m_n",
13392 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
13393 if (pipe_config->has_drrs)
13394 intel_dump_m_n_config(pipe_config, "dp m2_n2",
13395 pipe_config->lane_count,
13396 &pipe_config->dp_m2_n2);
f6982332 13397 }
b95af8be 13398
cd49f818
WK
13399 drm_dbg_kms(&dev_priv->drm,
13400 "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13401 pipe_config->has_audio, pipe_config->has_infoframe,
13402 pipe_config->infoframes.enable);
69e89032
VS
13403
13404 if (pipe_config->infoframes.enable &
13405 intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
cd49f818
WK
13406 drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13407 pipe_config->infoframes.gcp);
69e89032
VS
13408 if (pipe_config->infoframes.enable &
13409 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13410 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13411 if (pipe_config->infoframes.enable &
13412 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13413 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13414 if (pipe_config->infoframes.enable &
13415 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13416 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
bfbeba29
GM
13417 if (pipe_config->infoframes.enable &
13418 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
13419 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
e274fb32
GM
13420 if (pipe_config->infoframes.enable &
13421 intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
13422 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
42890250
GM
13423 if (pipe_config->infoframes.enable &
13424 intel_hdmi_infoframe_enable(DP_SDP_VSC))
13425 intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
69e89032 13426
cd49f818 13427 drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
1326a92c 13428 drm_mode_debug_printmodeline(&pipe_config->hw.mode);
cd49f818 13429 drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
1326a92c 13430 drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
3dfd8d71 13431 intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
bafcdad6
ML
13432 drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
13433 drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
13434 intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
cd49f818
WK
13435 drm_dbg_kms(&dev_priv->drm,
13436 "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13437 pipe_config->port_clock,
13438 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13439 pipe_config->pixel_rate);
dd2f616d 13440
6dcde047
VS
13441 drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13442 pipe_config->linetime, pipe_config->ips_linetime);
13443
dd2f616d 13444 if (INTEL_GEN(dev_priv) >= 9)
cd49f818
WK
13445 drm_dbg_kms(&dev_priv->drm,
13446 "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13447 crtc->num_scalers,
13448 pipe_config->scaler_state.scaler_users,
13449 pipe_config->scaler_state.scaler_id);
a74f8375 13450
b2ae318a 13451 if (HAS_GMCH(dev_priv))
cd49f818
WK
13452 drm_dbg_kms(&dev_priv->drm,
13453 "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13454 pipe_config->gmch_pfit.control,
13455 pipe_config->gmch_pfit.pgm_ratios,
13456 pipe_config->gmch_pfit.lvds_border_bits);
a74f8375 13457 else
cd49f818 13458 drm_dbg_kms(&dev_priv->drm,
35dd95b4
VS
13459 "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
13460 DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
cd49f818
WK
13461 enableddisabled(pipe_config->pch_pfit.enabled),
13462 yesno(pipe_config->pch_pfit.force_thru));
a74f8375 13463
cd49f818
WK
13464 drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13465 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 13466
f50b79f0 13467 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 13468
b1a4383d 13469 if (IS_CHERRYVIEW(dev_priv))
cd49f818
WK
13470 drm_dbg_kms(&dev_priv->drm,
13471 "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13472 pipe_config->cgm_mode, pipe_config->gamma_mode,
13473 pipe_config->gamma_enable, pipe_config->csc_enable);
b1a4383d 13474 else
cd49f818
WK
13475 drm_dbg_kms(&dev_priv->drm,
13476 "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13477 pipe_config->csc_mode, pipe_config->gamma_mode,
13478 pipe_config->gamma_enable, pipe_config->csc_enable);
b1a4383d 13479
cfcd558c
VS
13480 drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
13481 pipe_config->hw.degamma_lut ?
13482 drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
13483 pipe_config->hw.gamma_lut ?
13484 drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
13485
10d75f54
VS
13486dump_planes:
13487 if (!state)
13488 return;
6a60cd87 13489
10d75f54
VS
13490 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13491 if (plane->pipe == crtc->pipe)
13492 intel_dump_plane_state(plane_state);
6a60cd87 13493 }
c0b03411
DV
13494}
13495
85829eb5 13496static bool check_digital_port_conflicts(struct intel_atomic_state *state)
00f0b378 13497{
85829eb5 13498 struct drm_device *dev = state->base.dev;
da3ced29 13499 struct drm_connector *connector;
2fd96b41 13500 struct drm_connector_list_iter conn_iter;
00f0b378 13501 unsigned int used_ports = 0;
477321e0 13502 unsigned int used_mst_ports = 0;
bd67a8c1 13503 bool ret = true;
00f0b378 13504
1d5a95b5
VS
13505 /*
13506 * We're going to peek into connector->state,
13507 * hence connection_mutex must be held.
13508 */
13509 drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13510
00f0b378
VS
13511 /*
13512 * Walk the connector list instead of the encoder
13513 * list to detect the problem on ddi platforms
13514 * where there's just one encoder per digital port.
13515 */
2fd96b41
GP
13516 drm_connector_list_iter_begin(dev, &conn_iter);
13517 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
13518 struct drm_connector_state *connector_state;
13519 struct intel_encoder *encoder;
13520
85829eb5
VS
13521 connector_state =
13522 drm_atomic_get_new_connector_state(&state->base,
13523 connector);
0bff4858
VS
13524 if (!connector_state)
13525 connector_state = connector->state;
13526
5448a00d 13527 if (!connector_state->best_encoder)
00f0b378
VS
13528 continue;
13529
5448a00d
ACO
13530 encoder = to_intel_encoder(connector_state->best_encoder);
13531
e57291c2 13532 drm_WARN_ON(dev, !connector_state->crtc);
00f0b378
VS
13533
13534 switch (encoder->type) {
7e732cac 13535 case INTEL_OUTPUT_DDI:
e57291c2 13536 if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
00f0b378 13537 break;
df561f66 13538 fallthrough;
cca0502b 13539 case INTEL_OUTPUT_DP:
00f0b378
VS
13540 case INTEL_OUTPUT_HDMI:
13541 case INTEL_OUTPUT_EDP:
00f0b378 13542 /* the same port mustn't appear more than once */
2713eb41 13543 if (used_ports & BIT(encoder->port))
bd67a8c1 13544 ret = false;
00f0b378 13545
2713eb41 13546 used_ports |= BIT(encoder->port);
477321e0
VS
13547 break;
13548 case INTEL_OUTPUT_DP_MST:
13549 used_mst_ports |=
8f4f2797 13550 1 << encoder->port;
477321e0 13551 break;
00f0b378
VS
13552 default:
13553 break;
13554 }
13555 }
2fd96b41 13556 drm_connector_list_iter_end(&conn_iter);
00f0b378 13557
477321e0
VS
13558 /* can't mix MST and SST/HDMI on the same port */
13559 if (used_ports & used_mst_ports)
13560 return false;
13561
bd67a8c1 13562 return ret;
00f0b378
VS
13563}
13564
58d124ea 13565static void
aa07c1d3
MN
13566intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
13567 struct intel_crtc_state *crtc_state)
58d124ea 13568{
ee230706
MN
13569 const struct intel_crtc_state *from_crtc_state = crtc_state;
13570
19f65a3d
ML
13571 if (crtc_state->bigjoiner_slave) {
13572 from_crtc_state = intel_atomic_get_new_crtc_state(state,
13573 crtc_state->bigjoiner_linked_crtc);
13574
13575 /* No need to copy state if the master state is unchanged */
13576 if (!from_crtc_state)
13577 return;
13578 }
13579
ee230706 13580 intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
58d124ea
ML
13581}
13582
13583static void
aa07c1d3
MN
13584intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
13585 struct intel_crtc_state *crtc_state)
58d124ea
ML
13586{
13587 crtc_state->hw.enable = crtc_state->uapi.enable;
13588 crtc_state->hw.active = crtc_state->uapi.active;
13589 crtc_state->hw.mode = crtc_state->uapi.mode;
13590 crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
6d1a2fde 13591 crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
aa07c1d3
MN
13592
13593 intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
58d124ea
ML
13594}
13595
13596static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13597{
0385ecea
MN
13598 if (crtc_state->bigjoiner_slave)
13599 return;
13600
58d124ea
ML
13601 crtc_state->uapi.enable = crtc_state->hw.enable;
13602 crtc_state->uapi.active = crtc_state->hw.active;
ce04ecd9
PB
13603 drm_WARN_ON(crtc_state->uapi.crtc->dev,
13604 drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
58d124ea
ML
13605
13606 crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
6d1a2fde 13607 crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
58d124ea
ML
13608
13609 /* copy color blobs to uapi */
13610 drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13611 crtc_state->hw.degamma_lut);
13612 drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13613 crtc_state->hw.gamma_lut);
13614 drm_property_replace_blob(&crtc_state->uapi.ctm,
13615 crtc_state->hw.ctm);
13616}
13617
19f65a3d
ML
13618static int
13619copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
13620 const struct intel_crtc_state *from_crtc_state)
13621{
13622 struct intel_crtc_state *saved_state;
13623 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13624
13625 saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
13626 if (!saved_state)
13627 return -ENOMEM;
13628
13629 saved_state->uapi = crtc_state->uapi;
13630 saved_state->scaler_state = crtc_state->scaler_state;
13631 saved_state->shared_dpll = crtc_state->shared_dpll;
13632 saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13633 saved_state->crc_enabled = crtc_state->crc_enabled;
13634
13635 intel_crtc_free_hw_state(crtc_state);
13636 memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13637 kfree(saved_state);
13638
13639 /* Re-init hw state */
13640 memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
13641 crtc_state->hw.enable = from_crtc_state->hw.enable;
13642 crtc_state->hw.active = from_crtc_state->hw.active;
13643 crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
13644 crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
13645
13646 /* Some fixups */
13647 crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
13648 crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
13649 crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
13650 crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
13651 crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
13652 crtc_state->bigjoiner_slave = true;
13653 crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
13654 crtc_state->has_audio = false;
13655
13656 return 0;
13657}
13658
f81b845f 13659static int
aa07c1d3
MN
13660intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
13661 struct intel_crtc_state *crtc_state)
83a57153 13662{
216383e9
VS
13663 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13664 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f81b845f
CW
13665 struct intel_crtc_state *saved_state;
13666
216383e9 13667 saved_state = intel_crtc_state_alloc(crtc);
f81b845f
CW
13668 if (!saved_state)
13669 return -ENOMEM;
83a57153 13670
58d124ea
ML
13671 /* free the old crtc_state->hw members */
13672 intel_crtc_free_hw_state(crtc_state);
13673
7546a384
ACO
13674 /* FIXME: before the switch to atomic started, a new pipe_config was
13675 * kzalloc'd. Code that depends on any field being zero should be
13676 * fixed, so that the crtc_state can be safely duplicated. For now,
13677 * only fields that are know to not cause problems are preserved. */
13678
58d124ea 13679 saved_state->uapi = crtc_state->uapi;
f81b845f
CW
13680 saved_state->scaler_state = crtc_state->scaler_state;
13681 saved_state->shared_dpll = crtc_state->shared_dpll;
13682 saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
eea72c4c
ID
13683 memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13684 sizeof(saved_state->icl_port_dplls));
a8ebf607 13685 saved_state->crc_enabled = crtc_state->crc_enabled;
04548cba
VS
13686 if (IS_G4X(dev_priv) ||
13687 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
f81b845f 13688 saved_state->wm = crtc_state->wm;
4978cc93 13689
58d124ea 13690 memcpy(crtc_state, saved_state, sizeof(*crtc_state));
f81b845f 13691 kfree(saved_state);
58d124ea 13692
aa07c1d3 13693 intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
58d124ea 13694
f81b845f 13695 return 0;
83a57153
ACO
13696}
13697
548ee15b 13698static int
aa07c1d3
MN
13699intel_modeset_pipe_config(struct intel_atomic_state *state,
13700 struct intel_crtc_state *pipe_config)
ee7b9f93 13701{
2225f3c6 13702 struct drm_crtc *crtc = pipe_config->uapi.crtc;
cd49f818 13703 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
da3ced29 13704 struct drm_connector *connector;
0b901879 13705 struct drm_connector_state *connector_state;
b50a1aa6 13706 int base_bpp, ret, i;
e29c22c0 13707 bool retry = true;
ee7b9f93 13708
e143a21c
DV
13709 pipe_config->cpu_transcoder =
13710 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 13711
2960bc9c
ID
13712 /*
13713 * Sanitize sync polarity flags based on requested ones. If neither
13714 * positive or negative polarity is requested, treat this as meaning
13715 * negative polarity.
13716 */
1326a92c 13717 if (!(pipe_config->hw.adjusted_mode.flags &
2960bc9c 13718 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
1326a92c 13719 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13720
1326a92c 13721 if (!(pipe_config->hw.adjusted_mode.flags &
2960bc9c 13722 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
1326a92c 13723 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13724
bcce8d86
VS
13725 ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13726 pipe_config);
13727 if (ret)
13728 return ret;
13729
13730 base_bpp = pipe_config->pipe_bpp;
4e53c2e0 13731
e41a56be
VS
13732 /*
13733 * Determine the real pipe dimensions. Note that stereo modes can
13734 * increase the actual pipe size due to the frame doubling and
13735 * insertion of additional space for blanks between the frame. This
13736 * is stored in the crtc timings. We use the requested mode to do this
13737 * computation to clearly distinguish it from the adjusted mode, which
13738 * can be changed by the connectors in the below retry loop.
13739 */
1326a92c 13740 drm_mode_get_hv_timing(&pipe_config->hw.mode,
ecb7e16b
GP
13741 &pipe_config->pipe_src_w,
13742 &pipe_config->pipe_src_h);
e41a56be 13743
aa07c1d3 13744 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
691313ea
VS
13745 struct intel_encoder *encoder =
13746 to_intel_encoder(connector_state->best_encoder);
13747
253c84c8
VS
13748 if (connector_state->crtc != crtc)
13749 continue;
13750
e25148d0 13751 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
cd49f818
WK
13752 drm_dbg_kms(&i915->drm,
13753 "rejecting invalid cloning configuration\n");
d26592c6 13754 return -EINVAL;
e25148d0
VS
13755 }
13756
253c84c8
VS
13757 /*
13758 * Determine output_types before calling the .compute_config()
13759 * hooks so that the hooks can use this information safely.
13760 */
7e732cac
VS
13761 if (encoder->compute_output_type)
13762 pipe_config->output_types |=
13763 BIT(encoder->compute_output_type(encoder, pipe_config,
13764 connector_state));
13765 else
13766 pipe_config->output_types |= BIT(encoder->type);
253c84c8
VS
13767 }
13768
e29c22c0 13769encoder_retry:
ef1b460d 13770 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13771 pipe_config->port_clock = 0;
ef1b460d 13772 pipe_config->pixel_multiplier = 1;
ff9a6750 13773
135c81b8 13774 /* Fill in default crtc timings, allow encoders to overwrite them. */
1326a92c 13775 drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
2d112de7 13776 CRTC_STEREO_DOUBLE);
135c81b8 13777
7758a113
DV
13778 /* Pass our mode to the connectors and the CRTC to give them a chance to
13779 * adjust it according to limitations or connector properties, and also
13780 * a chance to reject the mode entirely.
47f1c6c9 13781 */
aa07c1d3 13782 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
691313ea
VS
13783 struct intel_encoder *encoder =
13784 to_intel_encoder(connector_state->best_encoder);
13785
0b901879 13786 if (connector_state->crtc != crtc)
7758a113 13787 continue;
7ae89233 13788
204474a6
LP
13789 ret = encoder->compute_config(encoder, pipe_config,
13790 connector_state);
13791 if (ret < 0) {
13792 if (ret != -EDEADLK)
cd49f818
WK
13793 drm_dbg_kms(&i915->drm,
13794 "Encoder config failure: %d\n",
13795 ret);
204474a6 13796 return ret;
7758a113 13797 }
ee7b9f93 13798 }
47f1c6c9 13799
ff9a6750
DV
13800 /* Set default port clock if not overwritten by the encoder. Needs to be
13801 * done afterwards in case the encoder adjusts the mode. */
13802 if (!pipe_config->port_clock)
1326a92c 13803 pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
241bfc38 13804 * pipe_config->pixel_multiplier;
ff9a6750 13805
a43f6e0f 13806 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8e2b4dff 13807 if (ret == -EDEADLK)
d26592c6 13808 return ret;
e29c22c0 13809 if (ret < 0) {
cd49f818 13810 drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
d26592c6 13811 return ret;
ee7b9f93 13812 }
e29c22c0
DV
13813
13814 if (ret == RETRY) {
e57291c2
PB
13815 if (drm_WARN(&i915->drm, !retry,
13816 "loop in pipe configuration computation\n"))
d26592c6 13817 return -EINVAL;
e29c22c0 13818
cd49f818 13819 drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
e29c22c0
DV
13820 retry = false;
13821 goto encoder_retry;
13822 }
13823
e8fa4270 13824 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
13825 * only enable it on 6bpc panels and when its not a compliance
13826 * test requesting 6bpc video pattern.
13827 */
13828 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13829 !pipe_config->dither_force_disable;
cd49f818
WK
13830 drm_dbg_kms(&i915->drm,
13831 "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13832 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13833
d26592c6 13834 return 0;
ee7b9f93 13835}
47f1c6c9 13836
f2801424
VS
13837static int
13838intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13839{
13840 struct intel_atomic_state *state =
13841 to_intel_atomic_state(crtc_state->uapi.state);
13842 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13843 struct drm_connector_state *conn_state;
13844 struct drm_connector *connector;
13845 int i;
13846
13847 for_each_new_connector_in_state(&state->base, connector,
13848 conn_state, i) {
13849 struct intel_encoder *encoder =
13850 to_intel_encoder(conn_state->best_encoder);
13851 int ret;
13852
13853 if (conn_state->crtc != &crtc->base ||
13854 !encoder->compute_config_late)
13855 continue;
13856
13857 ret = encoder->compute_config_late(encoder, crtc_state,
13858 conn_state);
13859 if (ret)
13860 return ret;
13861 }
13862
13863 return 0;
13864}
13865
2c1c5525 13866bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13867{
3bd26263 13868 int diff;
f1f644dc
JB
13869
13870 if (clock1 == clock2)
13871 return true;
13872
13873 if (!clock1 || !clock2)
13874 return false;
13875
13876 diff = abs(clock1 - clock2);
13877
13878 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13879 return true;
13880
13881 return false;
13882}
13883
cfb23ed6
ML
13884static bool
13885intel_compare_m_n(unsigned int m, unsigned int n,
13886 unsigned int m2, unsigned int n2,
13887 bool exact)
13888{
13889 if (m == m2 && n == n2)
13890 return true;
13891
13892 if (exact || !m || !n || !m2 || !n2)
13893 return false;
13894
13895 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13896
31d10b57
ML
13897 if (n > n2) {
13898 while (n > n2) {
cfb23ed6
ML
13899 m2 <<= 1;
13900 n2 <<= 1;
13901 }
31d10b57
ML
13902 } else if (n < n2) {
13903 while (n < n2) {
cfb23ed6
ML
13904 m <<= 1;
13905 n <<= 1;
13906 }
13907 }
13908
31d10b57
ML
13909 if (n != n2)
13910 return false;
13911
13912 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13913}
13914
13915static bool
13916intel_compare_link_m_n(const struct intel_link_m_n *m_n,
b124ea43
VS
13917 const struct intel_link_m_n *m2_n2,
13918 bool exact)
13919{
13920 return m_n->tu == m2_n2->tu &&
13921 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13922 m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13923 intel_compare_m_n(m_n->link_m, m_n->link_n,
13924 m2_n2->link_m, m2_n2->link_n, exact);
cfb23ed6
ML
13925}
13926
6454cb9f
VS
13927static bool
13928intel_compare_infoframe(const union hdmi_infoframe *a,
13929 const union hdmi_infoframe *b)
13930{
13931 return memcmp(a, b, sizeof(*a)) == 0;
13932}
13933
2c3928e4
GM
13934static bool
13935intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
13936 const struct drm_dp_vsc_sdp *b)
13937{
13938 return memcmp(a, b, sizeof(*a)) == 0;
13939}
13940
6454cb9f 13941static void
dde84833
VS
13942pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13943 bool fastset, const char *name,
13944 const union hdmi_infoframe *a,
13945 const union hdmi_infoframe *b)
6454cb9f 13946{
dde84833 13947 if (fastset) {
bdbf43d7 13948 if (!drm_debug_enabled(DRM_UT_KMS))
6454cb9f
VS
13949 return;
13950
cd49f818
WK
13951 drm_dbg_kms(&dev_priv->drm,
13952 "fastset mismatch in %s infoframe\n", name);
13953 drm_dbg_kms(&dev_priv->drm, "expected:\n");
6454cb9f 13954 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
cd49f818 13955 drm_dbg_kms(&dev_priv->drm, "found:\n");
6454cb9f
VS
13956 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13957 } else {
cd49f818
WK
13958 drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13959 drm_err(&dev_priv->drm, "expected:\n");
6454cb9f 13960 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
cd49f818 13961 drm_err(&dev_priv->drm, "found:\n");
6454cb9f
VS
13962 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13963 }
13964}
13965
2c3928e4
GM
13966static void
13967pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
13968 bool fastset, const char *name,
13969 const struct drm_dp_vsc_sdp *a,
13970 const struct drm_dp_vsc_sdp *b)
13971{
13972 if (fastset) {
13973 if (!drm_debug_enabled(DRM_UT_KMS))
13974 return;
13975
13976 drm_dbg_kms(&dev_priv->drm,
13977 "fastset mismatch in %s dp sdp\n", name);
13978 drm_dbg_kms(&dev_priv->drm, "expected:\n");
13979 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
13980 drm_dbg_kms(&dev_priv->drm, "found:\n");
13981 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
13982 } else {
13983 drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
13984 drm_err(&dev_priv->drm, "expected:\n");
13985 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
13986 drm_err(&dev_priv->drm, "found:\n");
13987 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
13988 }
13989}
13990
73cefd90
LDM
13991static void __printf(4, 5)
13992pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13993 const char *name, const char *format, ...)
4e8048f8 13994{
cd49f818 13995 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
4e8048f8
TU
13996 struct va_format vaf;
13997 va_list args;
13998
4e8048f8
TU
13999 va_start(args, format);
14000 vaf.fmt = format;
14001 vaf.va = &args;
14002
dde84833 14003 if (fastset)
cd49f818
WK
14004 drm_dbg_kms(&i915->drm,
14005 "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
14006 crtc->base.base.id, crtc->base.name, name, &vaf);
99a95487 14007 else
cd49f818
WK
14008 drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
14009 crtc->base.base.id, crtc->base.name, name, &vaf);
4e8048f8
TU
14010
14011 va_end(args);
14012}
14013
3d6535cb
HG
14014static bool fastboot_enabled(struct drm_i915_private *dev_priv)
14015{
8a25c4be
JN
14016 if (dev_priv->params.fastboot != -1)
14017 return dev_priv->params.fastboot;
3d6535cb
HG
14018
14019 /* Enable fastboot by default on Skylake and newer */
7360c9f6
HG
14020 if (INTEL_GEN(dev_priv) >= 9)
14021 return true;
14022
14023 /* Enable fastboot by default on VLV and CHV */
14024 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14025 return true;
14026
14027 /* Disabled by default on all others */
14028 return false;
3d6535cb
HG
14029}
14030
0e8ffe1b 14031static bool
b124ea43
VS
14032intel_pipe_config_compare(const struct intel_crtc_state *current_config,
14033 const struct intel_crtc_state *pipe_config,
dde84833 14034 bool fastset)
0e8ffe1b 14035{
2225f3c6
ML
14036 struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
14037 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
cfb23ed6 14038 bool ret = true;
7e764059 14039 u32 bp_gamma = 0;
dde84833 14040 bool fixup_inherited = fastset &&
a227569d 14041 current_config->inherited && !pipe_config->inherited;
cfb23ed6 14042
3d6535cb 14043 if (fixup_inherited && !fastboot_enabled(dev_priv)) {
cd49f818
WK
14044 drm_dbg_kms(&dev_priv->drm,
14045 "initial modeset and fastboot not set\n");
d19f958d
ML
14046 ret = false;
14047 }
14048
eadd2721 14049#define PIPE_CONF_CHECK_X(name) do { \
66e985c0 14050 if (current_config->name != pipe_config->name) { \
73cefd90 14051 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14052 "(expected 0x%08x, found 0x%08x)", \
dde84833
VS
14053 current_config->name, \
14054 pipe_config->name); \
cfb23ed6 14055 ret = false; \
eadd2721
VS
14056 } \
14057} while (0)
66e985c0 14058
eadd2721 14059#define PIPE_CONF_CHECK_I(name) do { \
08a24034 14060 if (current_config->name != pipe_config->name) { \
73cefd90 14061 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14062 "(expected %i, found %i)", \
dde84833
VS
14063 current_config->name, \
14064 pipe_config->name); \
cfb23ed6 14065 ret = false; \
eadd2721
VS
14066 } \
14067} while (0)
cfb23ed6 14068
eadd2721 14069#define PIPE_CONF_CHECK_BOOL(name) do { \
d640bf79 14070 if (current_config->name != pipe_config->name) { \
73cefd90 14071 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14072 "(expected %s, found %s)", \
dde84833
VS
14073 yesno(current_config->name), \
14074 yesno(pipe_config->name)); \
d640bf79 14075 ret = false; \
eadd2721
VS
14076 } \
14077} while (0)
d640bf79 14078
4493e098
ML
14079/*
14080 * Checks state where we only read out the enabling, but not the entire
14081 * state itself (like full infoframes or ELD for audio). These states
14082 * require a full modeset on bootup to fix up.
14083 */
eadd2721 14084#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
4493e098
ML
14085 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
14086 PIPE_CONF_CHECK_BOOL(name); \
14087 } else { \
73cefd90 14088 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14089 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
dde84833
VS
14090 yesno(current_config->name), \
14091 yesno(pipe_config->name)); \
4493e098 14092 ret = false; \
eadd2721
VS
14093 } \
14094} while (0)
4493e098 14095
eadd2721 14096#define PIPE_CONF_CHECK_P(name) do { \
8106ddbd 14097 if (current_config->name != pipe_config->name) { \
73cefd90 14098 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14099 "(expected %p, found %p)", \
dde84833
VS
14100 current_config->name, \
14101 pipe_config->name); \
8106ddbd 14102 ret = false; \
eadd2721
VS
14103 } \
14104} while (0)
8106ddbd 14105
eadd2721 14106#define PIPE_CONF_CHECK_M_N(name) do { \
cfb23ed6
ML
14107 if (!intel_compare_link_m_n(&current_config->name, \
14108 &pipe_config->name,\
dde84833 14109 !fastset)) { \
73cefd90 14110 pipe_config_mismatch(fastset, crtc, __stringify(name), \
dde84833 14111 "(expected tu %i gmch %i/%i link %i/%i, " \
cbd9b9f2 14112 "found tu %i, gmch %i/%i link %i/%i)", \
dde84833
VS
14113 current_config->name.tu, \
14114 current_config->name.gmch_m, \
14115 current_config->name.gmch_n, \
14116 current_config->name.link_m, \
14117 current_config->name.link_n, \
14118 pipe_config->name.tu, \
14119 pipe_config->name.gmch_m, \
14120 pipe_config->name.gmch_n, \
14121 pipe_config->name.link_m, \
14122 pipe_config->name.link_n); \
cfb23ed6 14123 ret = false; \
eadd2721
VS
14124 } \
14125} while (0)
cfb23ed6 14126
55c561a7
DV
14127/* This is required for BDW+ where there is only one set of registers for
14128 * switching between high and low RR.
14129 * This macro can be used whenever a comparison has to be made between one
14130 * hw state and multiple sw state variables.
14131 */
eadd2721 14132#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
cfb23ed6 14133 if (!intel_compare_link_m_n(&current_config->name, \
dde84833 14134 &pipe_config->name, !fastset) && \
cfb23ed6 14135 !intel_compare_link_m_n(&current_config->alt_name, \
dde84833 14136 &pipe_config->name, !fastset)) { \
73cefd90 14137 pipe_config_mismatch(fastset, crtc, __stringify(name), \
dde84833
VS
14138 "(expected tu %i gmch %i/%i link %i/%i, " \
14139 "or tu %i gmch %i/%i link %i/%i, " \
cbd9b9f2 14140 "found tu %i, gmch %i/%i link %i/%i)", \
dde84833
VS
14141 current_config->name.tu, \
14142 current_config->name.gmch_m, \
14143 current_config->name.gmch_n, \
14144 current_config->name.link_m, \
14145 current_config->name.link_n, \
14146 current_config->alt_name.tu, \
14147 current_config->alt_name.gmch_m, \
14148 current_config->alt_name.gmch_n, \
14149 current_config->alt_name.link_m, \
14150 current_config->alt_name.link_n, \
14151 pipe_config->name.tu, \
14152 pipe_config->name.gmch_m, \
14153 pipe_config->name.gmch_n, \
14154 pipe_config->name.link_m, \
14155 pipe_config->name.link_n); \
cfb23ed6 14156 ret = false; \
eadd2721
VS
14157 } \
14158} while (0)
88adfff1 14159
eadd2721 14160#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
1bd1bd80 14161 if ((current_config->name ^ pipe_config->name) & (mask)) { \
73cefd90 14162 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14163 "(%x) (expected %i, found %i)", \
dde84833
VS
14164 (mask), \
14165 current_config->name & (mask), \
14166 pipe_config->name & (mask)); \
cfb23ed6 14167 ret = false; \
eadd2721
VS
14168 } \
14169} while (0)
1bd1bd80 14170
eadd2721 14171#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
5e550656 14172 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
73cefd90 14173 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14174 "(expected %i, found %i)", \
dde84833
VS
14175 current_config->name, \
14176 pipe_config->name); \
cfb23ed6 14177 ret = false; \
eadd2721
VS
14178 } \
14179} while (0)
5e550656 14180
6454cb9f
VS
14181#define PIPE_CONF_CHECK_INFOFRAME(name) do { \
14182 if (!intel_compare_infoframe(&current_config->infoframes.name, \
14183 &pipe_config->infoframes.name)) { \
dde84833
VS
14184 pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
14185 &current_config->infoframes.name, \
14186 &pipe_config->infoframes.name); \
6454cb9f
VS
14187 ret = false; \
14188 } \
14189} while (0)
14190
2c3928e4
GM
14191#define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
14192 if (!current_config->has_psr && !pipe_config->has_psr && \
14193 !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
14194 &pipe_config->infoframes.name)) { \
14195 pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
14196 &current_config->infoframes.name, \
14197 &pipe_config->infoframes.name); \
14198 ret = false; \
14199 } \
14200} while (0)
14201
7e764059
SS
14202#define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
14203 if (current_config->name1 != pipe_config->name1) { \
73cefd90 14204 pipe_config_mismatch(fastset, crtc, __stringify(name1), \
cbd9b9f2 14205 "(expected %i, found %i, won't compare lut values)", \
7e764059
SS
14206 current_config->name1, \
14207 pipe_config->name1); \
14208 ret = false;\
14209 } else { \
14210 if (!intel_color_lut_equal(current_config->name2, \
14211 pipe_config->name2, pipe_config->name1, \
14212 bit_precision)) { \
73cefd90 14213 pipe_config_mismatch(fastset, crtc, __stringify(name2), \
cbd9b9f2 14214 "hw_state doesn't match sw_state"); \
7e764059
SS
14215 ret = false; \
14216 } \
14217 } \
14218} while (0)
14219
6454cb9f 14220#define PIPE_CONF_QUIRK(quirk) \
bb760063
DV
14221 ((current_config->quirks | pipe_config->quirks) & (quirk))
14222
eccb140b
DV
14223 PIPE_CONF_CHECK_I(cpu_transcoder);
14224
d640bf79 14225 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
08a24034 14226 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 14227 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 14228
90a6b7b0 14229 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 14230 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 14231
6315b5d3 14232 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
14233 PIPE_CONF_CHECK_M_N(dp_m_n);
14234
cfb23ed6
ML
14235 if (current_config->has_drrs)
14236 PIPE_CONF_CHECK_M_N(dp_m2_n2);
14237 } else
14238 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 14239
253c84c8 14240 PIPE_CONF_CHECK_X(output_types);
a65347ba 14241
0385ecea
MN
14242 /* FIXME do the readout properly and get rid of this quirk */
14243 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
14244 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
14245 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
14246 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
14247 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
14248 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
14249 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
14250
14251 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
14252 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
14253 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
14254 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
14255 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
14256 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
14257
14258 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
14259 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
14260 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
14261 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
14262 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
14263 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
14264
14265 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
14266 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
14267 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
14268 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
14269 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
14270 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
14271
14272 PIPE_CONF_CHECK_I(pixel_multiplier);
14273
14274 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14275 DRM_MODE_FLAG_INTERLACE);
14276
14277 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
14278 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14279 DRM_MODE_FLAG_PHSYNC);
14280 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14281 DRM_MODE_FLAG_NHSYNC);
14282 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14283 DRM_MODE_FLAG_PVSYNC);
14284 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14285 DRM_MODE_FLAG_NVSYNC);
14286 }
14287 }
14288
d9facae6 14289 PIPE_CONF_CHECK_I(output_format);
d640bf79 14290 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
772c2a51 14291 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 14292 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
d640bf79 14293 PIPE_CONF_CHECK_BOOL(limited_color_range);
15953637 14294
d640bf79
ML
14295 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
14296 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
7afc7f81 14297 PIPE_CONF_CHECK_BOOL(has_infoframe);
0385ecea
MN
14298 /* FIXME do the readout properly and get rid of this quirk */
14299 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
14300 PIPE_CONF_CHECK_BOOL(fec_enable);
6c49f241 14301
4493e098 14302 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
9ed109a7 14303
333b8ca8 14304 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 14305 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 14306 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 14307 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 14308 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 14309
13b7648b
VS
14310 /*
14311 * Changing the EDP transcoder input mux
14312 * (A_ONOFF vs. A_ON) requires a full modeset.
14313 */
dc0c0bfe 14314 PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
13b7648b 14315
dde84833 14316 if (!fastset) {
bfd16b2a
ML
14317 PIPE_CONF_CHECK_I(pipe_src_w);
14318 PIPE_CONF_CHECK_I(pipe_src_h);
14319
d640bf79 14320 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
bfd16b2a 14321 if (current_config->pch_pfit.enabled) {
35dd95b4
VS
14322 PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
14323 PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
14324 PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
14325 PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
bfd16b2a 14326 }
2fa2fe9a 14327
7aefe2b5 14328 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
0385ecea
MN
14329 /* FIXME do the readout properly and get rid of this quirk */
14330 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
14331 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
9d5441de
VS
14332
14333 PIPE_CONF_CHECK_X(gamma_mode);
9fdfb8e7
VS
14334 if (IS_CHERRYVIEW(dev_priv))
14335 PIPE_CONF_CHECK_X(cgm_mode);
14336 else
14337 PIPE_CONF_CHECK_X(csc_mode);
5f29ab23 14338 PIPE_CONF_CHECK_BOOL(gamma_enable);
8271b2ef 14339 PIPE_CONF_CHECK_BOOL(csc_enable);
7e764059 14340
6dcde047
VS
14341 PIPE_CONF_CHECK_I(linetime);
14342 PIPE_CONF_CHECK_I(ips_linetime);
14343
7e764059
SS
14344 bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
14345 if (bp_gamma)
aa42a50a 14346 PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
7aefe2b5 14347 }
a1b2278e 14348
d640bf79 14349 PIPE_CONF_CHECK_BOOL(double_wide);
282740f7 14350
8106ddbd 14351 PIPE_CONF_CHECK_P(shared_dpll);
0385ecea
MN
14352
14353 /* FIXME do the readout properly and get rid of this quirk */
14354 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
14355 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
14356 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
14357 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
14358 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
14359 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
14360 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
14361 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
14362 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
14363 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
14364 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
14365 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
14366 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
14367 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
14368 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
14369 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
14370 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
14371 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
14372 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
14373 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
14374 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
14375 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
14376 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
14377 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
14378 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
14379 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
14380 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
14381 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
14382 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
14383 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
14384 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
14385 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
14386
14387 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
14388 PIPE_CONF_CHECK_X(dsi_pll.div);
14389
14390 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
14391 PIPE_CONF_CHECK_I(pipe_bpp);
14392
14393 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
14394 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
14395 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
14396
14397 PIPE_CONF_CHECK_I(min_voltage_level);
14398 }
53e9bf5e 14399
6454cb9f
VS
14400 PIPE_CONF_CHECK_X(infoframes.enable);
14401 PIPE_CONF_CHECK_X(infoframes.gcp);
14402 PIPE_CONF_CHECK_INFOFRAME(avi);
14403 PIPE_CONF_CHECK_INFOFRAME(spd);
14404 PIPE_CONF_CHECK_INFOFRAME(hdmi);
b37f588e 14405 PIPE_CONF_CHECK_INFOFRAME(drm);
2c3928e4 14406 PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
6454cb9f 14407
b33950dd 14408 PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
bfb926e3 14409 PIPE_CONF_CHECK_I(master_transcoder);
0385ecea
MN
14410 PIPE_CONF_CHECK_BOOL(bigjoiner);
14411 PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
14412 PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
bfb926e3 14413
fbacb15e
JN
14414 PIPE_CONF_CHECK_I(dsc.compression_enable);
14415 PIPE_CONF_CHECK_I(dsc.dsc_split);
14416 PIPE_CONF_CHECK_I(dsc.compressed_bpp);
14417
6671c367
JRS
14418 PIPE_CONF_CHECK_I(mst_master_transcoder);
14419
66e985c0 14420#undef PIPE_CONF_CHECK_X
08a24034 14421#undef PIPE_CONF_CHECK_I
d640bf79 14422#undef PIPE_CONF_CHECK_BOOL
4493e098 14423#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
8106ddbd 14424#undef PIPE_CONF_CHECK_P
1bd1bd80 14425#undef PIPE_CONF_CHECK_FLAGS
5e550656 14426#undef PIPE_CONF_CHECK_CLOCK_FUZZY
7e764059 14427#undef PIPE_CONF_CHECK_COLOR_LUT
bb760063 14428#undef PIPE_CONF_QUIRK
88adfff1 14429
cfb23ed6 14430 return ret;
0e8ffe1b
DV
14431}
14432
e3b247da
VS
14433static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
14434 const struct intel_crtc_state *pipe_config)
14435{
14436 if (pipe_config->has_pch_encoder) {
21a727b3 14437 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da 14438 &pipe_config->fdi_m_n);
1326a92c 14439 int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
e3b247da
VS
14440
14441 /*
14442 * FDI already provided one idea for the dotclock.
14443 * Yell if the encoder disagrees.
14444 */
e57291c2
PB
14445 drm_WARN(&dev_priv->drm,
14446 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
14447 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
14448 fdi_dotclock, dotclock);
e3b247da
VS
14449 }
14450}
14451
3b4bf24d
ML
14452static void verify_wm_state(struct intel_crtc *crtc,
14453 struct intel_crtc_state *new_crtc_state)
08db6652 14454{
3b4bf24d 14455 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
370d757d
CW
14456 struct skl_hw_state {
14457 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
14458 struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
370d757d
CW
14459 struct skl_pipe_wm wm;
14460 } *hw;
370d757d 14461 struct skl_pipe_wm *sw_wm;
3de8a14c 14462 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
072fcc30 14463 u8 hw_enabled_slices;
3b4bf24d 14464 const enum pipe pipe = crtc->pipe;
3de8a14c 14465 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 14466
1326a92c 14467 if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
08db6652
DL
14468 return;
14469
370d757d
CW
14470 hw = kzalloc(sizeof(*hw), GFP_KERNEL);
14471 if (!hw)
14472 return;
14473
3b4bf24d
ML
14474 skl_pipe_wm_get_hw_state(crtc, &hw->wm);
14475 sw_wm = &new_crtc_state->wm.skl.optimal;
3de8a14c 14476
3b4bf24d 14477 skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
ff43bc37 14478
0f0f9aee 14479 hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
08db6652 14480
370d757d 14481 if (INTEL_GEN(dev_priv) >= 11 &&
3cf43cdc 14482 hw_enabled_slices != dev_priv->dbuf.enabled_slices)
cd49f818 14483 drm_err(&dev_priv->drm,
0f0f9aee 14484 "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
3cf43cdc 14485 dev_priv->dbuf.enabled_slices,
072fcc30 14486 hw_enabled_slices);
370d757d 14487
e7c84544 14488 /* planes */
8b364b41 14489 for_each_universal_plane(dev_priv, pipe, plane) {
370d757d
CW
14490 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14491
14492 hw_plane_wm = &hw->wm.planes[plane];
3de8a14c 14493 sw_plane_wm = &sw_wm->planes[plane];
08db6652 14494
3de8a14c 14495 /* Watermarks */
14496 for (level = 0; level <= max_level; level++) {
14497 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
7241c57d
SL
14498 &sw_plane_wm->wm[level]) ||
14499 (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14500 &sw_plane_wm->sagv_wm0)))
3de8a14c 14501 continue;
14502
cd49f818
WK
14503 drm_err(&dev_priv->drm,
14504 "mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14505 pipe_name(pipe), plane + 1, level,
14506 sw_plane_wm->wm[level].plane_en,
14507 sw_plane_wm->wm[level].plane_res_b,
14508 sw_plane_wm->wm[level].plane_res_l,
14509 hw_plane_wm->wm[level].plane_en,
14510 hw_plane_wm->wm[level].plane_res_b,
14511 hw_plane_wm->wm[level].plane_res_l);
3de8a14c 14512 }
08db6652 14513
3de8a14c 14514 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14515 &sw_plane_wm->trans_wm)) {
cd49f818
WK
14516 drm_err(&dev_priv->drm,
14517 "mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14518 pipe_name(pipe), plane + 1,
14519 sw_plane_wm->trans_wm.plane_en,
14520 sw_plane_wm->trans_wm.plane_res_b,
14521 sw_plane_wm->trans_wm.plane_res_l,
14522 hw_plane_wm->trans_wm.plane_en,
14523 hw_plane_wm->trans_wm.plane_res_b,
14524 hw_plane_wm->trans_wm.plane_res_l);
3de8a14c 14525 }
14526
14527 /* DDB */
370d757d 14528 hw_ddb_entry = &hw->ddb_y[plane];
3b4bf24d 14529 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
3de8a14c 14530
14531 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cd49f818
WK
14532 drm_err(&dev_priv->drm,
14533 "mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14534 pipe_name(pipe), plane + 1,
14535 sw_ddb_entry->start, sw_ddb_entry->end,
14536 hw_ddb_entry->start, hw_ddb_entry->end);
3de8a14c 14537 }
e7c84544 14538 }
08db6652 14539
27082493
L
14540 /*
14541 * cursor
14542 * If the cursor plane isn't active, we may not have updated it's ddb
14543 * allocation. In that case since the ddb allocation will be updated
14544 * once the plane becomes visible, we can skip this check
14545 */
cd5dcbf1 14546 if (1) {
370d757d
CW
14547 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14548
14549 hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
3de8a14c 14550 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14551
14552 /* Watermarks */
14553 for (level = 0; level <= max_level; level++) {
14554 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
7241c57d
SL
14555 &sw_plane_wm->wm[level]) ||
14556 (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14557 &sw_plane_wm->sagv_wm0)))
3de8a14c 14558 continue;
14559
cd49f818
WK
14560 drm_err(&dev_priv->drm,
14561 "mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14562 pipe_name(pipe), level,
14563 sw_plane_wm->wm[level].plane_en,
14564 sw_plane_wm->wm[level].plane_res_b,
14565 sw_plane_wm->wm[level].plane_res_l,
14566 hw_plane_wm->wm[level].plane_en,
14567 hw_plane_wm->wm[level].plane_res_b,
14568 hw_plane_wm->wm[level].plane_res_l);
3de8a14c 14569 }
14570
14571 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14572 &sw_plane_wm->trans_wm)) {
cd49f818
WK
14573 drm_err(&dev_priv->drm,
14574 "mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14575 pipe_name(pipe),
14576 sw_plane_wm->trans_wm.plane_en,
14577 sw_plane_wm->trans_wm.plane_res_b,
14578 sw_plane_wm->trans_wm.plane_res_l,
14579 hw_plane_wm->trans_wm.plane_en,
14580 hw_plane_wm->trans_wm.plane_res_b,
14581 hw_plane_wm->trans_wm.plane_res_l);
3de8a14c 14582 }
14583
14584 /* DDB */
370d757d 14585 hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
3b4bf24d 14586 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
27082493 14587
3de8a14c 14588 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cd49f818
WK
14589 drm_err(&dev_priv->drm,
14590 "mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14591 pipe_name(pipe),
14592 sw_ddb_entry->start, sw_ddb_entry->end,
14593 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 14594 }
08db6652 14595 }
370d757d
CW
14596
14597 kfree(hw);
08db6652
DL
14598}
14599
91d1b4bd 14600static void
3b4bf24d
ML
14601verify_connector_state(struct intel_atomic_state *state,
14602 struct intel_crtc *crtc)
8af6cf88 14603{
35dd3c64 14604 struct drm_connector *connector;
aa5e9b47 14605 struct drm_connector_state *new_conn_state;
677100ce 14606 int i;
8af6cf88 14607
3b4bf24d 14608 for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
35dd3c64 14609 struct drm_encoder *encoder = connector->encoder;
3b4bf24d 14610 struct intel_crtc_state *crtc_state = NULL;
ad3c558f 14611
3b4bf24d 14612 if (new_conn_state->crtc != &crtc->base)
e7c84544
ML
14613 continue;
14614
749d98b8 14615 if (crtc)
3b4bf24d 14616 crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
749d98b8
ML
14617
14618 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 14619
aa5e9b47 14620 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 14621 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 14622 }
91d1b4bd
DV
14623}
14624
14625static void
3b4bf24d 14626verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
91d1b4bd
DV
14627{
14628 struct intel_encoder *encoder;
86b04268
DV
14629 struct drm_connector *connector;
14630 struct drm_connector_state *old_conn_state, *new_conn_state;
14631 int i;
8af6cf88 14632
3b4bf24d 14633 for_each_intel_encoder(&dev_priv->drm, encoder) {
86b04268 14634 bool enabled = false, found = false;
4d20cd86 14635 enum pipe pipe;
8af6cf88 14636
cd49f818
WK
14637 drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14638 encoder->base.base.id,
14639 encoder->base.name);
8af6cf88 14640
3b4bf24d 14641 for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
86b04268
DV
14642 new_conn_state, i) {
14643 if (old_conn_state->best_encoder == &encoder->base)
14644 found = true;
14645
14646 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 14647 continue;
86b04268 14648 found = enabled = true;
ad3c558f 14649
86b04268 14650 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
14651 encoder->base.crtc,
14652 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 14653 }
86b04268
DV
14654
14655 if (!found)
14656 continue;
0e32b39c 14657
e2c719b7 14658 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
14659 "encoder's enabled state mismatch "
14660 "(expected %i, found %i)\n",
14661 !!encoder->base.crtc, enabled);
7c60d198
ML
14662
14663 if (!encoder->base.crtc) {
4d20cd86 14664 bool active;
7c60d198 14665
4d20cd86
ML
14666 active = encoder->get_hw_state(encoder, &pipe);
14667 I915_STATE_WARN(active,
14668 "encoder detached but still enabled on pipe %c.\n",
14669 pipe_name(pipe));
7c60d198 14670 }
8af6cf88 14671 }
91d1b4bd
DV
14672}
14673
14674static void
3b4bf24d
ML
14675verify_crtc_state(struct intel_crtc *crtc,
14676 struct intel_crtc_state *old_crtc_state,
14677 struct intel_crtc_state *new_crtc_state)
91d1b4bd 14678{
3b4bf24d 14679 struct drm_device *dev = crtc->base.dev;
fac5e23e 14680 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 14681 struct intel_encoder *encoder;
979e94c1
VS
14682 struct intel_crtc_state *pipe_config = old_crtc_state;
14683 struct drm_atomic_state *state = old_crtc_state->uapi.state;
0385ecea 14684 struct intel_crtc *master = crtc;
045ac3b5 14685
2225f3c6 14686 __drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
58d124ea 14687 intel_crtc_free_hw_state(old_crtc_state);
979e94c1
VS
14688 intel_crtc_state_reset(old_crtc_state, crtc);
14689 old_crtc_state->uapi.state = state;
8af6cf88 14690
cd49f818
WK
14691 drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14692 crtc->base.name);
8af6cf88 14693
504c7bd8
VS
14694 pipe_config->hw.enable = new_crtc_state->hw.enable;
14695
291106cb 14696 intel_crtc_get_pipe_config(pipe_config);
d62cf62a 14697
e56134bc 14698 /* we keep both pipes enabled on 830 */
504c7bd8
VS
14699 if (IS_I830(dev_priv) && pipe_config->hw.active)
14700 pipe_config->hw.active = new_crtc_state->hw.active;
6c49f241 14701
504c7bd8 14702 I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
1326a92c
ML
14703 "crtc active state doesn't match with hw state "
14704 "(expected %i, found %i)\n",
504c7bd8 14705 new_crtc_state->hw.active, pipe_config->hw.active);
0e8ffe1b 14706
1326a92c
ML
14707 I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14708 "transitional active state does not match atomic hw state "
14709 "(expected %i, found %i)\n",
14710 new_crtc_state->hw.active, crtc->active);
4d20cd86 14711
0385ecea
MN
14712 if (new_crtc_state->bigjoiner_slave)
14713 master = new_crtc_state->bigjoiner_linked_crtc;
14714
14715 for_each_encoder_on_crtc(dev, &master->base, encoder) {
e7c84544 14716 enum pipe pipe;
504c7bd8 14717 bool active;
4d20cd86 14718
e7c84544 14719 active = encoder->get_hw_state(encoder, &pipe);
1326a92c
ML
14720 I915_STATE_WARN(active != new_crtc_state->hw.active,
14721 "[ENCODER:%i] active %i with crtc active %i\n",
14722 encoder->base.base.id, active,
14723 new_crtc_state->hw.active);
4d20cd86 14724
0385ecea 14725 I915_STATE_WARN(active && master->pipe != pipe,
e7c84544
ML
14726 "Encoder connected to wrong pipe %c\n",
14727 pipe_name(pipe));
4d20cd86 14728
e1214b95 14729 if (active)
65c1ed30 14730 intel_encoder_get_config(encoder, pipe_config);
e7c84544 14731 }
53d9f4e9 14732
1326a92c 14733 if (!new_crtc_state->hw.active)
e7c84544 14734 return;
cfb23ed6 14735
e7c84544 14736 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 14737
3b4bf24d
ML
14738 if (!intel_pipe_config_compare(new_crtc_state,
14739 pipe_config, false)) {
e7c84544 14740 I915_STATE_WARN(1, "pipe state doesn't match!\n");
10d75f54 14741 intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
3b4bf24d 14742 intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
8af6cf88
DV
14743 }
14744}
14745
cff109f0
VS
14746static void
14747intel_verify_planes(struct intel_atomic_state *state)
14748{
14749 struct intel_plane *plane;
14750 const struct intel_plane_state *plane_state;
14751 int i;
14752
14753 for_each_new_intel_plane_in_state(state, plane,
14754 plane_state, i)
c47b7ddb 14755 assert_plane(plane, plane_state->planar_slave ||
f90a85e7 14756 plane_state->uapi.visible);
cff109f0
VS
14757}
14758
91d1b4bd 14759static void
c0ead703
ML
14760verify_single_dpll_state(struct drm_i915_private *dev_priv,
14761 struct intel_shared_dpll *pll,
3b4bf24d
ML
14762 struct intel_crtc *crtc,
14763 struct intel_crtc_state *new_crtc_state)
91d1b4bd 14764{
91d1b4bd 14765 struct intel_dpll_hw_state dpll_hw_state;
40560e26 14766 unsigned int crtc_mask;
e7c84544 14767 bool active;
5358901f 14768
e7c84544 14769 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 14770
cd49f818 14771 drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
5358901f 14772
fdbc5d68 14773 active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 14774
5cd281f6 14775 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
e7c84544
ML
14776 I915_STATE_WARN(!pll->on && pll->active_mask,
14777 "pll in active use but not on in sw tracking\n");
14778 I915_STATE_WARN(pll->on && !pll->active_mask,
14779 "pll is on but not used by any active crtc\n");
14780 I915_STATE_WARN(pll->on != active,
14781 "pll on state mismatch (expected %i, found %i)\n",
14782 pll->on, active);
14783 }
5358901f 14784
e7c84544 14785 if (!crtc) {
2c42e535 14786 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 14787 "more active pll users than references: %x vs %x\n",
2c42e535 14788 pll->active_mask, pll->state.crtc_mask);
5358901f 14789
e7c84544
ML
14790 return;
14791 }
14792
3b4bf24d 14793 crtc_mask = drm_crtc_mask(&crtc->base);
e7c84544 14794
1326a92c 14795 if (new_crtc_state->hw.active)
e7c84544
ML
14796 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14797 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
aca9310a 14798 pipe_name(crtc->pipe), pll->active_mask);
e7c84544
ML
14799 else
14800 I915_STATE_WARN(pll->active_mask & crtc_mask,
14801 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
aca9310a 14802 pipe_name(crtc->pipe), pll->active_mask);
2dd66ebd 14803
2c42e535 14804 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 14805 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 14806 crtc_mask, pll->state.crtc_mask);
66e985c0 14807
2c42e535 14808 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
14809 &dpll_hw_state,
14810 sizeof(dpll_hw_state)),
14811 "pll hw state mismatch\n");
14812}
14813
14814static void
3b4bf24d
ML
14815verify_shared_dpll_state(struct intel_crtc *crtc,
14816 struct intel_crtc_state *old_crtc_state,
14817 struct intel_crtc_state *new_crtc_state)
e7c84544 14818{
3b4bf24d 14819 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
e7c84544 14820
3b4bf24d
ML
14821 if (new_crtc_state->shared_dpll)
14822 verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
e7c84544 14823
3b4bf24d
ML
14824 if (old_crtc_state->shared_dpll &&
14825 old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14826 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14827 struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
e7c84544
ML
14828
14829 I915_STATE_WARN(pll->active_mask & crtc_mask,
14830 "pll active mismatch (didn't expect pipe %c in active mask)\n",
aca9310a 14831 pipe_name(crtc->pipe));
2c42e535 14832 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544 14833 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
aca9310a 14834 pipe_name(crtc->pipe));
5358901f 14835 }
8af6cf88
DV
14836}
14837
e7c84544 14838static void
855e0d68
ML
14839intel_modeset_verify_crtc(struct intel_crtc *crtc,
14840 struct intel_atomic_state *state,
3b4bf24d
ML
14841 struct intel_crtc_state *old_crtc_state,
14842 struct intel_crtc_state *new_crtc_state)
e7c84544 14843{
3b4bf24d 14844 if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
5a21b665
DV
14845 return;
14846
3b4bf24d
ML
14847 verify_wm_state(crtc, new_crtc_state);
14848 verify_connector_state(state, crtc);
14849 verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14850 verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
e7c84544
ML
14851}
14852
14853static void
3b4bf24d 14854verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
e7c84544 14855{
e7c84544
ML
14856 int i;
14857
353ad959
ID
14858 for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14859 verify_single_dpll_state(dev_priv,
14860 &dev_priv->dpll.shared_dplls[i],
14861 NULL, NULL);
e7c84544
ML
14862}
14863
14864static void
3b4bf24d 14865intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
855e0d68 14866 struct intel_atomic_state *state)
e7c84544 14867{
3b4bf24d
ML
14868 verify_encoder_state(dev_priv, state);
14869 verify_connector_state(state, NULL);
14870 verify_disabled_dpll_state(dev_priv);
e7c84544
ML
14871}
14872
99325429
VS
14873static void
14874intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
80715b2f 14875{
2225f3c6 14876 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4f8036a2 14877 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
99325429 14878 const struct drm_display_mode *adjusted_mode =
1326a92c 14879 &crtc_state->hw.adjusted_mode;
99325429
VS
14880
14881 drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
80715b2f 14882
af157b76
VS
14883 crtc->mode_flags = crtc_state->mode_flags;
14884
80715b2f
VS
14885 /*
14886 * The scanline counter increments at the leading edge of hsync.
14887 *
14888 * On most platforms it starts counting from vtotal-1 on the
14889 * first active line. That means the scanline counter value is
14890 * always one less than what we would expect. Ie. just after
14891 * start of vblank, which also occurs at start of hsync (on the
14892 * last active line), the scanline counter will read vblank_start-1.
14893 *
14894 * On gen2 the scanline counter starts counting from 1 instead
14895 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14896 * to keep the value positive), instead of adding one.
14897 *
14898 * On HSW+ the behaviour of the scanline counter depends on the output
14899 * type. For DP ports it behaves like most other platforms, but on HDMI
14900 * there's an extra 1 line difference. So we need to add two instead of
14901 * one to the value.
ec1b4ee2
VS
14902 *
14903 * On VLV/CHV DSI the scanline counter would appear to increment
14904 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14905 * that means we can't tell whether we're in vblank or not while
14906 * we're on that particular line. We must still set scanline_offset
14907 * to 1 so that the vblank timestamps come out correct when we query
14908 * the scanline counter from within the vblank interrupt handler.
14909 * However if queried just before the start of vblank we'll get an
14910 * answer that's slightly in the future.
80715b2f 14911 */
cf819eff 14912 if (IS_GEN(dev_priv, 2)) {
80715b2f
VS
14913 int vtotal;
14914
124abe07
VS
14915 vtotal = adjusted_mode->crtc_vtotal;
14916 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
14917 vtotal /= 2;
14918
14919 crtc->scanline_offset = vtotal - 1;
4f8036a2 14920 } else if (HAS_DDI(dev_priv) &&
f2bdd112 14921 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
80715b2f 14922 crtc->scanline_offset = 2;
99325429 14923 } else {
80715b2f 14924 crtc->scanline_offset = 1;
99325429 14925 }
80715b2f
VS
14926}
14927
c3b1e6c6 14928static void intel_modeset_clear_plls(struct intel_atomic_state *state)
ed6739ef 14929{
c3b1e6c6 14930 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
866955fa 14931 struct intel_crtc_state *new_crtc_state;
c3b1e6c6 14932 struct intel_crtc *crtc;
0a9ab303 14933 int i;
ed6739ef
ACO
14934
14935 if (!dev_priv->display.crtc_compute_clock)
ad421372 14936 return;
ed6739ef 14937
866955fa 14938 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
69f786ae 14939 if (!needs_modeset(new_crtc_state))
225da59b
ACO
14940 continue;
14941
866955fa 14942 intel_release_shared_dplls(state, crtc);
ad421372 14943 }
ed6739ef
ACO
14944}
14945
99d736a2
ML
14946/*
14947 * This implements the workaround described in the "notes" section of the mode
14948 * set sequence documentation. When going from no pipes or single pipe to
14949 * multiple pipes, and planes are enabled after the pipe, we need to wait at
14950 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14951 */
1e98f88c 14952static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
99d736a2 14953{
bca0bfa3
VS
14954 struct intel_crtc_state *crtc_state;
14955 struct intel_crtc *crtc;
99d736a2
ML
14956 struct intel_crtc_state *first_crtc_state = NULL;
14957 struct intel_crtc_state *other_crtc_state = NULL;
14958 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14959 int i;
14960
14961 /* look at all crtc's that are going to be enabled in during modeset */
bca0bfa3 14962 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1326a92c 14963 if (!crtc_state->hw.active ||
69f786ae 14964 !needs_modeset(crtc_state))
99d736a2
ML
14965 continue;
14966
14967 if (first_crtc_state) {
bca0bfa3 14968 other_crtc_state = crtc_state;
99d736a2
ML
14969 break;
14970 } else {
bca0bfa3
VS
14971 first_crtc_state = crtc_state;
14972 first_pipe = crtc->pipe;
99d736a2
ML
14973 }
14974 }
14975
14976 /* No workaround needed? */
14977 if (!first_crtc_state)
14978 return 0;
14979
14980 /* w/a possibly needed, check how many crtc's are already enabled. */
bca0bfa3
VS
14981 for_each_intel_crtc(state->base.dev, crtc) {
14982 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14983 if (IS_ERR(crtc_state))
14984 return PTR_ERR(crtc_state);
99d736a2 14985
bca0bfa3 14986 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
99d736a2 14987
1326a92c 14988 if (!crtc_state->hw.active ||
69f786ae 14989 needs_modeset(crtc_state))
99d736a2
ML
14990 continue;
14991
14992 /* 2 or more enabled crtcs means no need for w/a */
14993 if (enabled_pipe != INVALID_PIPE)
14994 return 0;
14995
bca0bfa3 14996 enabled_pipe = crtc->pipe;
99d736a2
ML
14997 }
14998
14999 if (enabled_pipe != INVALID_PIPE)
15000 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
15001 else if (other_crtc_state)
15002 other_crtc_state->hsw_workaround_pipe = first_pipe;
15003
15004 return 0;
15005}
15006
aac97871
VS
15007u8 intel_calc_active_pipes(struct intel_atomic_state *state,
15008 u8 active_pipes)
15009{
15010 const struct intel_crtc_state *crtc_state;
15011 struct intel_crtc *crtc;
15012 int i;
15013
15014 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15015 if (crtc_state->hw.active)
15016 active_pipes |= BIT(crtc->pipe);
15017 else
15018 active_pipes &= ~BIT(crtc->pipe);
15019 }
15020
15021 return active_pipes;
15022}
15023
5643dd9c 15024static int intel_modeset_checks(struct intel_atomic_state *state)
054518dd 15025{
5643dd9c 15026 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
054518dd 15027
5643dd9c 15028 state->modeset = true;
1d5a95b5 15029
565602d7 15030 if (IS_HASWELL(dev_priv))
1e98f88c 15031 return hsw_mode_set_planes_workaround(state);
99d736a2 15032
ad421372 15033 return 0;
c347a676
ACO
15034}
15035
aa363136
MR
15036/*
15037 * Handle calculation of various watermark data at the end of the atomic check
15038 * phase. The code here should be run after the per-crtc and per-plane 'check'
15039 * handlers to ensure that all derived state has been updated.
15040 */
cd1d3ee9 15041static int calc_watermark_data(struct intel_atomic_state *state)
aa363136 15042{
cd1d3ee9 15043 struct drm_device *dev = state->base.dev;
98d39494 15044 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
15045
15046 /* Is there platform-specific watermark information to calculate? */
15047 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
15048 return dev_priv->display.compute_global_watermarks(state);
15049
15050 return 0;
aa363136
MR
15051}
15052
b124ea43 15053static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
f0521558
VS
15054 struct intel_crtc_state *new_crtc_state)
15055{
b124ea43 15056 if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
f0521558
VS
15057 return;
15058
2225f3c6 15059 new_crtc_state->uapi.mode_changed = false;
f0521558 15060 new_crtc_state->update_pipe = true;
5cb5b370 15061}
f0521558 15062
5cb5b370
JRS
15063static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
15064 struct intel_crtc_state *new_crtc_state)
15065{
f0521558
VS
15066 /*
15067 * If we're not doing the full modeset we want to
15068 * keep the current M/N values as they may be
15069 * sufficiently different to the computed values
15070 * to cause problems.
15071 *
15072 * FIXME: should really copy more fuzzy state here
15073 */
15074 new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
15075 new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
15076 new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
15077 new_crtc_state->has_drrs = old_crtc_state->has_drrs;
15078}
15079
bb6ae9e6
VS
15080static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
15081 struct intel_crtc *crtc,
15082 u8 plane_ids_mask)
131d3b1a 15083{
bb6ae9e6
VS
15084 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15085 struct intel_plane *plane;
15086
15087 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
15088 struct intel_plane_state *plane_state;
15089
15090 if ((plane_ids_mask & BIT(plane->id)) == 0)
15091 continue;
15092
15093 plane_state = intel_atomic_get_plane_state(state, plane);
15094 if (IS_ERR(plane_state))
15095 return PTR_ERR(plane_state);
15096 }
15097
15098 return 0;
15099}
15100
15101static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
15102{
15103 /* See {hsw,vlv,ivb}_plane_ratio() */
15104 return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
15105 IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
cf129762 15106 IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
bb6ae9e6
VS
15107}
15108
1cf6adb7
VS
15109static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
15110 struct intel_crtc *crtc,
15111 struct intel_crtc *other)
15112{
15113 const struct intel_plane_state *plane_state;
15114 struct intel_plane *plane;
15115 u8 plane_ids = 0;
15116 int i;
15117
15118 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15119 if (plane->pipe == crtc->pipe)
15120 plane_ids |= BIT(plane->id);
15121 }
15122
15123 return intel_crtc_add_planes_to_state(state, other, plane_ids);
15124}
15125
15126static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
15127{
15128 const struct intel_crtc_state *crtc_state;
15129 struct intel_crtc *crtc;
15130 int i;
15131
15132 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15133 int ret;
15134
15135 if (!crtc_state->bigjoiner)
15136 continue;
15137
15138 ret = intel_crtc_add_bigjoiner_planes(state, crtc,
15139 crtc_state->bigjoiner_linked_crtc);
15140 if (ret)
15141 return ret;
15142 }
15143
15144 return 0;
15145}
15146
4f0b4352 15147static int intel_atomic_check_planes(struct intel_atomic_state *state)
bb6ae9e6
VS
15148{
15149 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15150 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
131d3b1a
VS
15151 struct intel_plane_state *plane_state;
15152 struct intel_plane *plane;
bb6ae9e6 15153 struct intel_crtc *crtc;
131d3b1a
VS
15154 int i, ret;
15155
bb6ae9e6
VS
15156 ret = icl_add_linked_planes(state);
15157 if (ret)
15158 return ret;
15159
1cf6adb7
VS
15160 ret = intel_bigjoiner_add_affected_planes(state);
15161 if (ret)
15162 return ret;
15163
131d3b1a
VS
15164 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15165 ret = intel_plane_atomic_check(state, plane);
15166 if (ret) {
cd49f818
WK
15167 drm_dbg_atomic(&dev_priv->drm,
15168 "[PLANE:%d:%s] atomic driver check failed\n",
15169 plane->base.base.id, plane->base.name);
131d3b1a
VS
15170 return ret;
15171 }
15172 }
15173
bb6ae9e6
VS
15174 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15175 new_crtc_state, i) {
15176 u8 old_active_planes, new_active_planes;
15177
15178 ret = icl_check_nv12_planes(new_crtc_state);
15179 if (ret)
15180 return ret;
15181
15182 /*
15183 * On some platforms the number of active planes affects
15184 * the planes' minimum cdclk calculation. Add such planes
15185 * to the state before we compute the minimum cdclk.
15186 */
15187 if (!active_planes_affects_min_cdclk(dev_priv))
15188 continue;
15189
15190 old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
15191 new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
15192
9877c37e
SL
15193 /*
15194 * Not only the number of planes, but if the plane configuration had
15195 * changed might already mean we need to recompute min CDCLK,
15196 * because different planes might consume different amount of Dbuf bandwidth
15197 * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
15198 */
15199 if (old_active_planes == new_active_planes)
bb6ae9e6
VS
15200 continue;
15201
15202 ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
15203 if (ret)
15204 return ret;
15205 }
15206
4f0b4352
SL
15207 return 0;
15208}
15209
15210static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
15211 bool *need_cdclk_calc)
15212{
cd191546 15213 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
dc98f50f
VS
15214 const struct intel_cdclk_state *old_cdclk_state;
15215 const struct intel_cdclk_state *new_cdclk_state;
4f0b4352 15216 struct intel_plane_state *plane_state;
19aefbc7 15217 struct intel_bw_state *new_bw_state;
4f0b4352 15218 struct intel_plane *plane;
19aefbc7
SL
15219 int min_cdclk = 0;
15220 enum pipe pipe;
4f0b4352 15221 int ret;
19aefbc7 15222 int i;
bb6ae9e6
VS
15223 /*
15224 * active_planes bitmask has been updated, and potentially
15225 * affected planes are part of the state. We can now
15226 * compute the minimum cdclk for each plane.
15227 */
28a30b45
VS
15228 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15229 ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
15230 if (ret)
15231 return ret;
15232 }
bb6ae9e6 15233
dc98f50f 15234 old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
cd191546
SL
15235 new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
15236
dc98f50f
VS
15237 if (new_cdclk_state &&
15238 old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
cd191546
SL
15239 *need_cdclk_calc = true;
15240
15241 ret = dev_priv->display.bw_calc_min_cdclk(state);
15242 if (ret)
15243 return ret;
15244
19aefbc7 15245 new_bw_state = intel_atomic_get_new_bw_state(state);
cd191546 15246
19aefbc7
SL
15247 if (!new_cdclk_state || !new_bw_state)
15248 return 0;
cd191546 15249
19aefbc7
SL
15250 for_each_pipe(dev_priv, pipe) {
15251 min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
cd191546
SL
15252
15253 /*
15254 * Currently do this change only if we need to increase
15255 */
19aefbc7 15256 if (new_bw_state->min_cdclk > min_cdclk)
cd191546
SL
15257 *need_cdclk_calc = true;
15258 }
15259
131d3b1a
VS
15260 return 0;
15261}
15262
15263static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
15264{
15265 struct intel_crtc_state *crtc_state;
15266 struct intel_crtc *crtc;
15267 int i;
15268
15269 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
cd49f818 15270 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
eba10ec8
VS
15271 int ret;
15272
15273 ret = intel_crtc_atomic_check(state, crtc);
131d3b1a 15274 if (ret) {
cd49f818
WK
15275 drm_dbg_atomic(&i915->drm,
15276 "[CRTC:%d:%s] atomic driver check failed\n",
15277 crtc->base.base.id, crtc->base.name);
131d3b1a
VS
15278 return ret;
15279 }
15280 }
15281
15282 return 0;
15283}
15284
d0eed154
VS
15285static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
15286 u8 transcoders)
080d47bf 15287{
d0eed154 15288 const struct intel_crtc_state *new_crtc_state;
080d47bf
JRS
15289 struct intel_crtc *crtc;
15290 int i;
15291
d0eed154
VS
15292 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15293 if (new_crtc_state->hw.enable &&
15294 transcoders & BIT(new_crtc_state->cpu_transcoder) &&
15295 needs_modeset(new_crtc_state))
15296 return true;
15297 }
080d47bf
JRS
15298
15299 return false;
15300}
15301
19f65a3d
ML
15302static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
15303 struct intel_crtc *crtc,
15304 struct intel_crtc_state *old_crtc_state,
15305 struct intel_crtc_state *new_crtc_state)
15306{
15307 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15308 struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
15309 struct intel_crtc *slave, *master;
15310
15311 /* slave being enabled, is master is still claiming this crtc? */
15312 if (old_crtc_state->bigjoiner_slave) {
15313 slave = crtc;
15314 master = old_crtc_state->bigjoiner_linked_crtc;
15315 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
15316 if (!master_crtc_state || !needs_modeset(master_crtc_state))
15317 goto claimed;
15318 }
15319
15320 if (!new_crtc_state->bigjoiner)
15321 return 0;
15322
15323 if (1 + crtc->pipe >= INTEL_NUM_PIPES(dev_priv)) {
15324 DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
15325 "CRTC + 1 to be used, doesn't exist\n",
15326 crtc->base.base.id, crtc->base.name);
15327 return -EINVAL;
15328 }
15329
15330 slave = new_crtc_state->bigjoiner_linked_crtc =
15331 intel_get_crtc_for_pipe(dev_priv, crtc->pipe + 1);
15332 slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
15333 master = crtc;
15334 if (IS_ERR(slave_crtc_state))
15335 return PTR_ERR(slave_crtc_state);
15336
15337 /* master being enabled, slave was already configured? */
15338 if (slave_crtc_state->uapi.enable)
15339 goto claimed;
15340
15341 DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
15342 slave->base.base.id, slave->base.name);
15343
15344 return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
15345
15346claimed:
15347 DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
15348 "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
15349 slave->base.base.id, slave->base.name,
15350 master->base.base.id, master->base.name);
15351 return -EINVAL;
15352}
15353
15354static int kill_bigjoiner_slave(struct intel_atomic_state *state,
15355 struct intel_crtc_state *master_crtc_state)
15356{
15357 struct intel_crtc_state *slave_crtc_state =
15358 intel_atomic_get_crtc_state(&state->base,
15359 master_crtc_state->bigjoiner_linked_crtc);
15360
15361 if (IS_ERR(slave_crtc_state))
15362 return PTR_ERR(slave_crtc_state);
15363
15364 slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
15365 slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
15366 slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
15367 intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
15368 return 0;
15369}
15370
6914c968
K
15371/**
15372 * DOC: asynchronous flip implementation
15373 *
15374 * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
15375 * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
15376 * Correspondingly, support is currently added for primary plane only.
15377 *
15378 * Async flip can only change the plane surface address, so anything else
15379 * changing is rejected from the intel_atomic_check_async() function.
15380 * Once this check is cleared, flip done interrupt is enabled using
15381 * the skl_enable_flip_done() function.
15382 *
15383 * As soon as the surface address register is written, flip done interrupt is
15384 * generated and the requested events are sent to the usersapce in the interrupt
15385 * handler itself. The timestamp and sequence sent during the flip done event
15386 * correspond to the last vblank and have no relation to the actual time when
15387 * the flip done event was sent.
15388 */
30ff93af
K
15389static int intel_atomic_check_async(struct intel_atomic_state *state)
15390{
15391 struct drm_i915_private *i915 = to_i915(state->base.dev);
15392 const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15393 const struct intel_plane_state *new_plane_state, *old_plane_state;
15394 struct intel_crtc *crtc;
15395 struct intel_plane *plane;
15396 int i;
15397
15398 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15399 new_crtc_state, i) {
15400 if (needs_modeset(new_crtc_state)) {
15401 drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
15402 return -EINVAL;
15403 }
15404
15405 if (!new_crtc_state->hw.active) {
15406 drm_dbg_kms(&i915->drm, "CRTC inactive\n");
15407 return -EINVAL;
15408 }
15409 if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
15410 drm_dbg_kms(&i915->drm,
15411 "Active planes cannot be changed during async flip\n");
15412 return -EINVAL;
15413 }
15414 }
15415
15416 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15417 new_plane_state, i) {
15418 /*
15419 * TODO: Async flip is only supported through the page flip IOCTL
15420 * as of now. So support currently added for primary plane only.
15421 * Support for other planes on platforms on which supports
15422 * this(vlv/chv and icl+) should be added when async flip is
15423 * enabled in the atomic IOCTL path.
15424 */
15425 if (plane->id != PLANE_PRIMARY)
15426 return -EINVAL;
15427
15428 /*
15429 * FIXME: This check is kept generic for all platforms.
15430 * Need to verify this for all gen9 and gen10 platforms to enable
15431 * this selectively if required.
15432 */
15433 switch (new_plane_state->hw.fb->modifier) {
15434 case I915_FORMAT_MOD_X_TILED:
15435 case I915_FORMAT_MOD_Y_TILED:
15436 case I915_FORMAT_MOD_Yf_TILED:
15437 break;
15438 default:
15439 drm_dbg_kms(&i915->drm,
15440 "Linear memory/CCS does not support async flips\n");
15441 return -EINVAL;
15442 }
15443
15444 if (old_plane_state->color_plane[0].stride !=
15445 new_plane_state->color_plane[0].stride) {
15446 drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
15447 return -EINVAL;
15448 }
15449
15450 if (old_plane_state->hw.fb->modifier !=
15451 new_plane_state->hw.fb->modifier) {
15452 drm_dbg_kms(&i915->drm,
15453 "Framebuffer modifiers cannot be changed in async flip\n");
15454 return -EINVAL;
15455 }
15456
15457 if (old_plane_state->hw.fb->format !=
15458 new_plane_state->hw.fb->format) {
15459 drm_dbg_kms(&i915->drm,
15460 "Framebuffer format cannot be changed in async flip\n");
15461 return -EINVAL;
15462 }
15463
15464 if (old_plane_state->hw.rotation !=
15465 new_plane_state->hw.rotation) {
15466 drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
15467 return -EINVAL;
15468 }
15469
15470 if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
15471 !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
15472 drm_dbg_kms(&i915->drm,
15473 "Plane size/co-ordinates cannot be changed in async flip\n");
15474 return -EINVAL;
15475 }
15476
15477 if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
15478 drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
15479 return -EINVAL;
15480 }
15481
15482 if (old_plane_state->hw.pixel_blend_mode !=
15483 new_plane_state->hw.pixel_blend_mode) {
15484 drm_dbg_kms(&i915->drm,
15485 "Pixel blend mode cannot be changed in async flip\n");
15486 return -EINVAL;
15487 }
15488
15489 if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
15490 drm_dbg_kms(&i915->drm,
15491 "Color encoding cannot be changed in async flip\n");
15492 return -EINVAL;
15493 }
15494
15495 if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
15496 drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
15497 return -EINVAL;
15498 }
15499 }
15500
15501 return 0;
15502}
15503
d321634b
VS
15504static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
15505{
15506 const struct intel_crtc_state *crtc_state;
15507 struct intel_crtc *crtc;
15508 int i;
15509
15510 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15511 struct intel_crtc_state *linked_crtc_state;
15512
15513 if (!crtc_state->bigjoiner)
15514 continue;
15515
15516 linked_crtc_state = intel_atomic_get_crtc_state(&state->base,
15517 crtc_state->bigjoiner_linked_crtc);
15518 if (IS_ERR(linked_crtc_state))
15519 return PTR_ERR(linked_crtc_state);
15520 }
15521
15522 return 0;
15523}
15524
74c090b1
ML
15525/**
15526 * intel_atomic_check - validate state object
15527 * @dev: drm device
70972f51 15528 * @_state: state to validate
74c090b1
ML
15529 */
15530static int intel_atomic_check(struct drm_device *dev,
9a86a07c 15531 struct drm_atomic_state *_state)
c347a676 15532{
dd8b3bdb 15533 struct drm_i915_private *dev_priv = to_i915(dev);
9a86a07c
VS
15534 struct intel_atomic_state *state = to_intel_atomic_state(_state);
15535 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15536 struct intel_crtc *crtc;
c347a676 15537 int ret, i;
1d5a95b5 15538 bool any_ms = false;
c347a676 15539
9a86a07c
VS
15540 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15541 new_crtc_state, i) {
a227569d 15542 if (new_crtc_state->inherited != old_crtc_state->inherited)
2225f3c6 15543 new_crtc_state->uapi.mode_changed = true;
8c58f73c
ML
15544 }
15545
9a86a07c 15546 ret = drm_atomic_helper_check_modeset(dev, &state->base);
054518dd 15547 if (ret)
2833920d 15548 goto fail;
054518dd 15549
d321634b
VS
15550 ret = intel_bigjoiner_add_affected_crtcs(state);
15551 if (ret)
15552 goto fail;
15553
9a86a07c
VS
15554 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15555 new_crtc_state, i) {
58d124ea
ML
15556 if (!needs_modeset(new_crtc_state)) {
15557 /* Light copy */
aa07c1d3 15558 intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
58d124ea 15559
c347a676 15560 continue;
58d124ea 15561 }
c347a676 15562
19f65a3d
ML
15563 /* Kill old bigjoiner link, we may re-establish afterwards */
15564 if (old_crtc_state->bigjoiner && !old_crtc_state->bigjoiner_slave) {
15565 ret = kill_bigjoiner_slave(state, new_crtc_state);
15566 if (ret)
15567 goto fail;
15568 }
15569
15570 if (!new_crtc_state->uapi.enable) {
15571 if (!new_crtc_state->bigjoiner_slave) {
15572 intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
15573 any_ms = true;
15574 }
15575 continue;
15576 }
15577
aa07c1d3 15578 ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
58d124ea
ML
15579 if (ret)
15580 goto fail;
15581
aa07c1d3 15582 ret = intel_modeset_pipe_config(state, new_crtc_state);
2833920d
VS
15583 if (ret)
15584 goto fail;
19f65a3d
ML
15585
15586 ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
15587 new_crtc_state);
15588 if (ret)
15589 goto fail;
f2801424
VS
15590 }
15591
15592 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15593 new_crtc_state, i) {
15594 if (!needs_modeset(new_crtc_state))
15595 continue;
15596
15597 ret = intel_modeset_pipe_config_late(new_crtc_state);
15598 if (ret)
15599 goto fail;
c347a676 15600
f0521558 15601 intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
5cb5b370 15602 }
26495481 15603
080d47bf
JRS
15604 /**
15605 * Check if fastset is allowed by external dependencies like other
15606 * pipes and transcoders.
15607 *
15608 * Right now it only forces a fullmodeset when the MST master
15609 * transcoder did not changed but the pipe of the master transcoder
05a8e451
JRS
15610 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
15611 * in case of port synced crtcs, if one of the synced crtcs
15612 * needs a full modeset, all other synced crtcs should be
15613 * forced a full modeset.
080d47bf
JRS
15614 */
15615 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
05a8e451 15616 if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
080d47bf
JRS
15617 continue;
15618
05a8e451
JRS
15619 if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
15620 enum transcoder master = new_crtc_state->mst_master_transcoder;
15621
d0eed154 15622 if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
05a8e451
JRS
15623 new_crtc_state->uapi.mode_changed = true;
15624 new_crtc_state->update_pipe = false;
15625 }
d0eed154
VS
15626 }
15627
15628 if (is_trans_port_sync_mode(new_crtc_state)) {
8c47eb86
MN
15629 u8 trans = new_crtc_state->sync_mode_slaves_mask;
15630
15631 if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
15632 trans |= BIT(new_crtc_state->master_transcoder);
05a8e451 15633
d0eed154
VS
15634 if (intel_cpu_transcoders_need_modeset(state, trans)) {
15635 new_crtc_state->uapi.mode_changed = true;
15636 new_crtc_state->update_pipe = false;
15637 }
080d47bf
JRS
15638 }
15639 }
15640
5cb5b370
JRS
15641 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15642 new_crtc_state, i) {
15643 if (needs_modeset(new_crtc_state)) {
26495481 15644 any_ms = true;
5cb5b370
JRS
15645 continue;
15646 }
15647
15648 if (!new_crtc_state->update_pipe)
15649 continue;
15650
15651 intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
c347a676
ACO
15652 }
15653
bf5da83e 15654 if (any_ms && !check_digital_port_conflicts(state)) {
cd49f818
WK
15655 drm_dbg_kms(&dev_priv->drm,
15656 "rejecting conflicting digital port configuration\n");
66b51b80 15657 ret = -EINVAL;
bf5da83e
VS
15658 goto fail;
15659 }
15660
9a86a07c 15661 ret = drm_dp_mst_atomic_check(&state->base);
eceae147 15662 if (ret)
2833920d 15663 goto fail;
eceae147 15664
4f0b4352 15665 ret = intel_atomic_check_planes(state);
bb6ae9e6
VS
15666 if (ret)
15667 goto fail;
15668
0f8839f5
VS
15669 /*
15670 * distrust_bios_wm will force a full dbuf recomputation
15671 * but the hardware state will only get updated accordingly
15672 * if state->modeset==true. Hence distrust_bios_wm==true &&
15673 * state->modeset==false is an invalid combination which
15674 * would cause the hardware and software dbuf state to get
15675 * out of sync. We must prevent that.
15676 *
15677 * FIXME clean up this mess and introduce better
15678 * state tracking for dbuf.
15679 */
15680 if (dev_priv->wm.distrust_bios_wm)
15681 any_ms = true;
15682
9a86a07c
VS
15683 intel_fbc_choose_crtc(dev_priv, state);
15684 ret = calc_watermark_data(state);
c457d9cf 15685 if (ret)
2833920d 15686 goto fail;
c457d9cf 15687
9a86a07c 15688 ret = intel_bw_atomic_check(state);
c457d9cf 15689 if (ret)
2833920d 15690 goto fail;
c457d9cf 15691
4f0b4352
SL
15692 ret = intel_atomic_check_cdclk(state, &any_ms);
15693 if (ret)
15694 goto fail;
15695
c93b9b2c 15696 if (any_ms) {
cf696856
KV
15697 ret = intel_modeset_checks(state);
15698 if (ret)
15699 goto fail;
15700
c93b9b2c
SL
15701 ret = intel_modeset_calc_cdclk(state);
15702 if (ret)
15703 return ret;
15704
15705 intel_modeset_clear_plls(state);
15706 }
15707
15708 ret = intel_atomic_check_crtcs(state);
15709 if (ret)
15710 goto fail;
15711
a0e70104
VS
15712 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15713 new_crtc_state, i) {
30ff93af
K
15714 if (new_crtc_state->uapi.async_flip) {
15715 ret = intel_atomic_check_async(state);
15716 if (ret)
15717 goto fail;
15718 }
15719
69f786ae 15720 if (!needs_modeset(new_crtc_state) &&
a0e70104
VS
15721 !new_crtc_state->update_pipe)
15722 continue;
15723
10d75f54 15724 intel_dump_pipe_config(new_crtc_state, state,
69f786ae 15725 needs_modeset(new_crtc_state) ?
a0e70104
VS
15726 "[modeset]" : "[fastset]");
15727 }
15728
c457d9cf 15729 return 0;
2833920d
VS
15730
15731 fail:
15732 if (ret == -EDEADLK)
15733 return ret;
15734
15735 /*
15736 * FIXME would probably be nice to know which crtc specifically
15737 * caused the failure, in cases where we can pinpoint it.
15738 */
15739 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15740 new_crtc_state, i)
10d75f54 15741 intel_dump_pipe_config(new_crtc_state, state, "[failed]");
2833920d
VS
15742
15743 return ret;
054518dd
ACO
15744}
15745
a85fb467 15746static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
5008e874 15747{
afeda4f3
AM
15748 struct intel_crtc_state *crtc_state;
15749 struct intel_crtc *crtc;
15750 int i, ret;
15751
15752 ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
15753 if (ret < 0)
15754 return ret;
15755
15756 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15757 bool mode_changed = needs_modeset(crtc_state);
15758
15759 if (mode_changed || crtc_state->update_pipe ||
15760 crtc_state->uapi.color_mgmt_changed) {
15761 intel_dsb_prepare(crtc_state);
15762 }
15763 }
15764
15765 return 0;
5008e874
ML
15766}
15767
a2991414
ML
15768u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
15769{
15770 struct drm_device *dev = crtc->base.dev;
32db0b65 15771 struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
a2991414 15772
32db0b65 15773 if (!vblank->max_vblank_count)
734cbbf3 15774 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
a2991414 15775
08fa8fd0 15776 return crtc->base.funcs->get_vblank_counter(&crtc->base);
a2991414
ML
15777}
15778
d8bd3e15
ML
15779void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
15780 struct intel_crtc_state *crtc_state)
15781{
15782 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15783
cfdd1747 15784 if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
d8bd3e15
ML
15785 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15786
15787 if (crtc_state->has_pch_encoder) {
15788 enum pipe pch_transcoder =
15789 intel_crtc_pch_transcoder(crtc);
15790
15791 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
15792 }
15793}
15794
15795static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
15796 const struct intel_crtc_state *new_crtc_state)
15797{
2225f3c6 15798 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
d8bd3e15
ML
15799 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15800
d8bd3e15
ML
15801 /*
15802 * Update pipe size and adjust fitter if needed: the reason for this is
15803 * that in compute_mode_changes we check the native mode (not the pfit
15804 * mode) to see if we can flip rather than do a full mode set. In the
15805 * fastboot case, we'll flip, but if we don't update the pipesrc and
15806 * pfit state, we'll end up with a big fb scanned out into the wrong
15807 * sized surface.
15808 */
15809 intel_set_pipe_src_size(new_crtc_state);
15810
15811 /* on skylake this is done by detaching scalers */
15812 if (INTEL_GEN(dev_priv) >= 9) {
15813 skl_detach_scalers(new_crtc_state);
15814
15815 if (new_crtc_state->pch_pfit.enabled)
f6df4d46 15816 skl_pfit_enable(new_crtc_state);
d8bd3e15
ML
15817 } else if (HAS_PCH_SPLIT(dev_priv)) {
15818 if (new_crtc_state->pch_pfit.enabled)
9eae5e27 15819 ilk_pfit_enable(new_crtc_state);
d8bd3e15 15820 else if (old_crtc_state->pch_pfit.enabled)
9eae5e27 15821 ilk_pfit_disable(old_crtc_state);
d8bd3e15
ML
15822 }
15823
6dcde047
VS
15824 /*
15825 * The register is supposedly single buffered so perhaps
15826 * not 100% correct to do this here. But SKL+ calculate
15827 * this based on the adjust pixel rate so pfit changes do
15828 * affect it and so it must be updated for fastsets.
15829 * HSW/BDW only really need this here for fastboot, after
15830 * that the value should not change without a full modeset.
15831 */
15832 if (INTEL_GEN(dev_priv) >= 9 ||
15833 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
15834 hsw_set_linetime_wm(new_crtc_state);
15835
d8bd3e15
ML
15836 if (INTEL_GEN(dev_priv) >= 11)
15837 icl_set_pipe_chicken(crtc);
15838}
15839
15840static void commit_pipe_config(struct intel_atomic_state *state,
b932da3c 15841 struct intel_crtc *crtc)
d8bd3e15
ML
15842{
15843 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
b932da3c
VS
15844 const struct intel_crtc_state *old_crtc_state =
15845 intel_atomic_get_old_crtc_state(state, crtc);
15846 const struct intel_crtc_state *new_crtc_state =
15847 intel_atomic_get_new_crtc_state(state, crtc);
d8bd3e15
ML
15848 bool modeset = needs_modeset(new_crtc_state);
15849
15850 /*
15851 * During modesets pipe configuration was programmed as the
15852 * CRTC was enabled.
15853 */
15854 if (!modeset) {
2225f3c6 15855 if (new_crtc_state->uapi.color_mgmt_changed ||
d8bd3e15
ML
15856 new_crtc_state->update_pipe)
15857 intel_color_commit(new_crtc_state);
15858
15859 if (INTEL_GEN(dev_priv) >= 9)
15860 skl_detach_scalers(new_crtc_state);
15861
15862 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15863 bdw_set_pipemisc(new_crtc_state);
15864
15865 if (new_crtc_state->update_pipe)
15866 intel_pipe_fastset(old_crtc_state, new_crtc_state);
6e43e276
JRS
15867
15868 intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
d8bd3e15
ML
15869 }
15870
15871 if (dev_priv->display.atomic_update_watermarks)
7a8fdb1f 15872 dev_priv->display.atomic_update_watermarks(state, crtc);
d8bd3e15
ML
15873}
15874
b932da3c
VS
15875static void intel_enable_crtc(struct intel_atomic_state *state,
15876 struct intel_crtc *crtc)
896e5bb0 15877{
d8bd3e15 15878 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
b932da3c
VS
15879 const struct intel_crtc_state *new_crtc_state =
15880 intel_atomic_get_new_crtc_state(state, crtc);
896e5bb0 15881
b932da3c
VS
15882 if (!needs_modeset(new_crtc_state))
15883 return;
99325429 15884
b932da3c 15885 intel_crtc_update_active_timings(new_crtc_state);
033b7a23 15886
b932da3c
VS
15887 dev_priv->display.crtc_enable(state, crtc);
15888
4e3cdb45
MN
15889 if (new_crtc_state->bigjoiner_slave)
15890 return;
15891
b932da3c
VS
15892 /* vblanks work again, re-enable pipe CRC. */
15893 intel_crtc_enable_pipe_crc(crtc);
15894}
15895
15896static void intel_update_crtc(struct intel_atomic_state *state,
15897 struct intel_crtc *crtc)
15898{
15899 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15900 const struct intel_crtc_state *old_crtc_state =
15901 intel_atomic_get_old_crtc_state(state, crtc);
15902 struct intel_crtc_state *new_crtc_state =
15903 intel_atomic_get_new_crtc_state(state, crtc);
15904 bool modeset = needs_modeset(new_crtc_state);
15905
15906 if (!modeset) {
0ccc42a2
VS
15907 if (new_crtc_state->preload_luts &&
15908 (new_crtc_state->uapi.color_mgmt_changed ||
15909 new_crtc_state->update_pipe))
15910 intel_color_load_luts(new_crtc_state);
15911
bee43ca4 15912 intel_pre_plane_update(state, crtc);
608ed4ab 15913
855e0d68 15914 if (new_crtc_state->update_pipe)
021ba100 15915 intel_encoders_update_pipe(state, crtc);
896e5bb0
L
15916 }
15917
855e0d68
ML
15918 if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15919 intel_fbc_disable(crtc);
9ecc6eab
VS
15920 else
15921 intel_fbc_enable(state, crtc);
896e5bb0 15922
d8bd3e15
ML
15923 /* Perform vblank evasion around commit operation */
15924 intel_pipe_update_start(new_crtc_state);
15925
b932da3c 15926 commit_pipe_config(state, crtc);
6c246b81 15927
5f2e5112 15928 if (INTEL_GEN(dev_priv) >= 9)
855e0d68 15929 skl_update_planes_on_crtc(state, crtc);
5f2e5112 15930 else
855e0d68 15931 i9xx_update_planes_on_crtc(state, crtc);
6c246b81 15932
d8bd3e15
ML
15933 intel_pipe_update_end(new_crtc_state);
15934
15935 /*
15936 * We usually enable FIFO underrun interrupts as part of the
15937 * CRTC enable sequence during modesets. But when we inherit a
15938 * valid pipe configuration from the BIOS we need to take care
15939 * of enabling them on the CRTC's first fastset.
15940 */
15941 if (new_crtc_state->update_pipe && !modeset &&
a227569d 15942 old_crtc_state->inherited)
d8bd3e15 15943 intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
896e5bb0
L
15944}
15945
eadf6f91 15946
66d9cec8
MN
15947static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15948 struct intel_crtc_state *old_crtc_state,
15949 struct intel_crtc_state *new_crtc_state,
15950 struct intel_crtc *crtc)
15951{
15952 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15953
4e3cdb45
MN
15954 drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
15955
66d9cec8
MN
15956 intel_crtc_disable_planes(state, crtc);
15957
4e3cdb45
MN
15958 /*
15959 * We still need special handling for disabling bigjoiner master
15960 * and slaves since for slave we do not have encoder or plls
15961 * so we dont need to disable those.
15962 */
15963 if (old_crtc_state->bigjoiner) {
15964 intel_crtc_disable_planes(state,
15965 old_crtc_state->bigjoiner_linked_crtc);
15966 old_crtc_state->bigjoiner_linked_crtc->active = false;
15967 }
15968
66d9cec8
MN
15969 /*
15970 * We need to disable pipe CRC before disabling the pipe,
15971 * or we race against vblank off.
15972 */
15973 intel_crtc_disable_pipe_crc(crtc);
15974
7451a074 15975 dev_priv->display.crtc_disable(state, crtc);
66d9cec8
MN
15976 crtc->active = false;
15977 intel_fbc_disable(crtc);
15978 intel_disable_shared_dpll(old_crtc_state);
15979
66d9cec8 15980 /* FIXME unify this for all platforms */
1326a92c 15981 if (!new_crtc_state->hw.active &&
66d9cec8
MN
15982 !HAS_GMCH(dev_priv) &&
15983 dev_priv->display.initial_watermarks)
7a8fdb1f 15984 dev_priv->display.initial_watermarks(state, crtc);
66d9cec8
MN
15985}
15986
15987static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15988{
15989 struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15990 struct intel_crtc *crtc;
ad457191 15991 u32 handled = 0;
66d9cec8
MN
15992 int i;
15993
6671c367 15994 /* Only disable port sync and MST slaves */
ad457191
JRS
15995 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15996 new_crtc_state, i) {
4e3cdb45 15997 if (!needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
66d9cec8
MN
15998 continue;
15999
ad457191
JRS
16000 if (!old_crtc_state->hw.active)
16001 continue;
16002
a6c948f9
MN
16003 /* In case of Transcoder port Sync master slave CRTCs can be
16004 * assigned in any order and we need to make sure that
16005 * slave CRTCs are disabled first and then master CRTC since
16006 * Slave vblanks are masked till Master Vblanks.
16007 */
6671c367
JRS
16008 if (!is_trans_port_sync_slave(old_crtc_state) &&
16009 !intel_dp_mst_is_slave_trans(old_crtc_state))
ad457191 16010 continue;
66d9cec8 16011
ad457191
JRS
16012 intel_pre_plane_update(state, crtc);
16013 intel_old_crtc_state_disables(state, old_crtc_state,
16014 new_crtc_state, crtc);
16015 handled |= BIT(crtc->pipe);
16016 }
16017
16018 /* Disable everything else left on */
16019 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16020 new_crtc_state, i) {
16021 if (!needs_modeset(new_crtc_state) ||
4e3cdb45
MN
16022 (handled & BIT(crtc->pipe)) ||
16023 old_crtc_state->bigjoiner_slave)
ad457191
JRS
16024 continue;
16025
16026 intel_pre_plane_update(state, crtc);
4e3cdb45
MN
16027 if (old_crtc_state->bigjoiner) {
16028 struct intel_crtc *slave =
16029 old_crtc_state->bigjoiner_linked_crtc;
16030
16031 intel_pre_plane_update(state, slave);
16032 }
16033
ad457191
JRS
16034 if (old_crtc_state->hw.active)
16035 intel_old_crtc_state_disables(state, old_crtc_state,
16036 new_crtc_state, crtc);
66d9cec8
MN
16037 }
16038}
16039
0c841271 16040static void intel_commit_modeset_enables(struct intel_atomic_state *state)
896e5bb0 16041{
b932da3c 16042 struct intel_crtc_state *new_crtc_state;
855e0d68 16043 struct intel_crtc *crtc;
896e5bb0
L
16044 int i;
16045
b932da3c 16046 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
1326a92c 16047 if (!new_crtc_state->hw.active)
896e5bb0
L
16048 continue;
16049
b932da3c
VS
16050 intel_enable_crtc(state, crtc);
16051 intel_update_crtc(state, crtc);
896e5bb0
L
16052 }
16053}
16054
0c841271 16055static void skl_commit_modeset_enables(struct intel_atomic_state *state)
27082493 16056{
855e0d68
ML
16057 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
16058 struct intel_crtc *crtc;
16059 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
53cc6880 16060 struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
659f1415 16061 u8 update_pipes = 0, modeset_pipes = 0;
601a9ee0 16062 int i;
5eff503b 16063
601a9ee0 16064 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
ee34801c
VS
16065 enum pipe pipe = crtc->pipe;
16066
659f1415
JRS
16067 if (!new_crtc_state->hw.active)
16068 continue;
16069
5eff503b 16070 /* ignore allocations for crtc's that have been turned off. */
659f1415 16071 if (!needs_modeset(new_crtc_state)) {
ee34801c
VS
16072 entries[pipe] = old_crtc_state->wm.skl.ddb;
16073 update_pipes |= BIT(pipe);
659f1415 16074 } else {
ee34801c 16075 modeset_pipes |= BIT(pipe);
659f1415 16076 }
601a9ee0 16077 }
27082493
L
16078
16079 /*
16080 * Whenever the number of active pipes changes, we need to make sure we
16081 * update the pipes in the right order so that their ddb allocations
659f1415 16082 * never overlap with each other between CRTC updates. Otherwise we'll
27082493 16083 * cause pipe underruns and other bad stuff.
659f1415
JRS
16084 *
16085 * So first lets enable all pipes that do not need a fullmodeset as
16086 * those don't have any external dependency.
27082493 16087 */
659f1415 16088 while (update_pipes) {
601a9ee0
VS
16089 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16090 new_crtc_state, i) {
36b53a29 16091 enum pipe pipe = crtc->pipe;
27082493 16092
659f1415 16093 if ((update_pipes & BIT(pipe)) == 0)
27082493 16094 continue;
5eff503b 16095
855e0d68 16096 if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7a0a6ee7 16097 entries, I915_MAX_PIPES, pipe))
27082493
L
16098 continue;
16099
ee34801c 16100 entries[pipe] = new_crtc_state->wm.skl.ddb;
659f1415
JRS
16101 update_pipes &= ~BIT(pipe);
16102
b932da3c 16103 intel_update_crtc(state, crtc);
27082493 16104
601a9ee0
VS
16105 /*
16106 * If this is an already active pipe, it's DDB changed,
16107 * and this isn't the last pipe that needs updating
16108 * then we need to wait for a vblank to pass for the
16109 * new ddb allocation to take effect.
16110 */
16111 if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
16112 &old_crtc_state->wm.skl.ddb) &&
659f1415 16113 (update_pipes | modeset_pipes))
0f0f74bc 16114 intel_wait_for_vblank(dev_priv, pipe);
27082493 16115 }
601a9ee0 16116 }
aa9664ff 16117
1ff241ea
VS
16118 update_pipes = modeset_pipes;
16119
659f1415
JRS
16120 /*
16121 * Enable all pipes that needs a modeset and do not depends on other
16122 * pipes
16123 */
4f05d7ae 16124 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
659f1415
JRS
16125 enum pipe pipe = crtc->pipe;
16126
16127 if ((modeset_pipes & BIT(pipe)) == 0)
16128 continue;
16129
16130 if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
4e3cdb45
MN
16131 is_trans_port_sync_master(new_crtc_state) ||
16132 (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
659f1415
JRS
16133 continue;
16134
659f1415
JRS
16135 modeset_pipes &= ~BIT(pipe);
16136
d82a855a 16137 intel_enable_crtc(state, crtc);
659f1415
JRS
16138 }
16139
16140 /*
1ff241ea 16141 * Then we enable all remaining pipes that depend on other
4e3cdb45 16142 * pipes: MST slaves and port sync masters, big joiner master
659f1415 16143 */
b932da3c 16144 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
659f1415
JRS
16145 enum pipe pipe = crtc->pipe;
16146
16147 if ((modeset_pipes & BIT(pipe)) == 0)
16148 continue;
16149
1ff241ea
VS
16150 modeset_pipes &= ~BIT(pipe);
16151
16152 intel_enable_crtc(state, crtc);
16153 }
16154
16155 /*
16156 * Finally we do the plane updates/etc. for all pipes that got enabled.
16157 */
16158 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16159 enum pipe pipe = crtc->pipe;
16160
16161 if ((update_pipes & BIT(pipe)) == 0)
16162 continue;
16163
e57291c2 16164 drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7a0a6ee7 16165 entries, I915_MAX_PIPES, pipe));
659f1415 16166
ee34801c 16167 entries[pipe] = new_crtc_state->wm.skl.ddb;
1ff241ea 16168 update_pipes &= ~BIT(pipe);
659f1415 16169
b932da3c 16170 intel_update_crtc(state, crtc);
659f1415
JRS
16171 }
16172
e57291c2 16173 drm_WARN_ON(&dev_priv->drm, modeset_pipes);
1ff241ea 16174 drm_WARN_ON(&dev_priv->drm, update_pipes);
27082493
L
16175}
16176
ba318c61
CW
16177static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
16178{
16179 struct intel_atomic_state *state, *next;
16180 struct llist_node *freed;
16181
16182 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
16183 llist_for_each_entry_safe(state, next, freed, freed)
16184 drm_atomic_state_put(&state->base);
16185}
16186
16187static void intel_atomic_helper_free_state_worker(struct work_struct *work)
16188{
16189 struct drm_i915_private *dev_priv =
16190 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
16191
16192 intel_atomic_helper_free_state(dev_priv);
16193}
16194
9db529aa
DV
16195static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
16196{
16197 struct wait_queue_entry wait_fence, wait_reset;
16198 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
16199
16200 init_wait_entry(&wait_fence, 0);
16201 init_wait_entry(&wait_reset, 0);
16202 for (;;) {
16203 prepare_to_wait(&intel_state->commit_ready.wait,
16204 &wait_fence, TASK_UNINTERRUPTIBLE);
cb823ed9
CW
16205 prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
16206 I915_RESET_MODESET),
9db529aa
DV
16207 &wait_reset, TASK_UNINTERRUPTIBLE);
16208
16209
cb823ed9
CW
16210 if (i915_sw_fence_done(&intel_state->commit_ready) ||
16211 test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
9db529aa
DV
16212 break;
16213
16214 schedule();
16215 }
16216 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
cb823ed9
CW
16217 finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
16218 I915_RESET_MODESET),
16219 &wait_reset);
9db529aa
DV
16220}
16221
afeda4f3
AM
16222static void intel_cleanup_dsbs(struct intel_atomic_state *state)
16223{
16224 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
16225 struct intel_crtc *crtc;
16226 int i;
16227
16228 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16229 new_crtc_state, i)
16230 intel_dsb_cleanup(old_crtc_state);
16231}
16232
8d52e447
CW
16233static void intel_atomic_cleanup_work(struct work_struct *work)
16234{
afeda4f3
AM
16235 struct intel_atomic_state *state =
16236 container_of(work, struct intel_atomic_state, base.commit_work);
16237 struct drm_i915_private *i915 = to_i915(state->base.dev);
8d52e447 16238
afeda4f3
AM
16239 intel_cleanup_dsbs(state);
16240 drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
16241 drm_atomic_helper_commit_cleanup_done(&state->base);
16242 drm_atomic_state_put(&state->base);
8d52e447
CW
16243
16244 intel_atomic_helper_free_state(i915);
16245}
16246
855e0d68 16247static void intel_atomic_commit_tail(struct intel_atomic_state *state)
a6778b3c 16248{
855e0d68 16249 struct drm_device *dev = state->base.dev;
fac5e23e 16250 struct drm_i915_private *dev_priv = to_i915(dev);
855e0d68
ML
16251 struct intel_crtc_state *new_crtc_state, *old_crtc_state;
16252 struct intel_crtc *crtc;
d8fc70b7 16253 u64 put_domains[I915_MAX_PIPES] = {};
0e6e0be4 16254 intel_wakeref_t wakeref = 0;
e95433c7 16255 int i;
a6778b3c 16256
855e0d68 16257 intel_atomic_commit_fence_wait(state);
42b062b0 16258
855e0d68 16259 drm_atomic_helper_wait_for_dependencies(&state->base);
ea0000f0 16260
855e0d68 16261 if (state->modeset)
0e6e0be4 16262 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 16263
7bff9779
ML
16264 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16265 new_crtc_state, i) {
855e0d68
ML
16266 if (needs_modeset(new_crtc_state) ||
16267 new_crtc_state->update_pipe) {
a539205a 16268
855e0d68 16269 put_domains[crtc->pipe] =
afe0c21b 16270 modeset_get_crtc_power_domains(new_crtc_state);
5a21b665 16271 }
b8cecdf5 16272 }
7758a113 16273
66d9cec8
MN
16274 intel_commit_modeset_disables(state);
16275
855e0d68
ML
16276 /* FIXME: Eventually get rid of our crtc->config pointer */
16277 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16278 crtc->config = new_crtc_state;
f6e5b160 16279
855e0d68
ML
16280 if (state->modeset) {
16281 drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
33c8df89 16282
5604e9ce 16283 intel_set_cdclk_pre_plane_update(state);
f6d1973d 16284
3b4bf24d 16285 intel_modeset_verify_disabled(dev_priv, state);
4740b0f2 16286 }
47fab737 16287
ecab0f3d
SL
16288 intel_sagv_pre_plane_update(state);
16289
896e5bb0 16290 /* Complete the events for pipes that have now been disabled */
855e0d68
ML
16291 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16292 bool modeset = needs_modeset(new_crtc_state);
80715b2f 16293
1f7528c4 16294 /* Complete events for now disable pipes here. */
2225f3c6 16295 if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
1f7528c4 16296 spin_lock_irq(&dev->event_lock);
2225f3c6
ML
16297 drm_crtc_send_vblank_event(&crtc->base,
16298 new_crtc_state->uapi.event);
1f7528c4
DV
16299 spin_unlock_irq(&dev->event_lock);
16300
2225f3c6 16301 new_crtc_state->uapi.event = NULL;
1f7528c4 16302 }
177246a8
MR
16303 }
16304
24a7bfe0
ID
16305 if (state->modeset)
16306 intel_encoders_update_prepare(state);
16307
c7c0e7eb 16308 intel_dbuf_pre_plane_update(state);
b06cf595 16309
1288f9b0
K
16310 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16311 if (new_crtc_state->uapi.async_flip)
16312 skl_enable_flip_done(crtc);
16313 }
16314
896e5bb0 16315 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0c841271 16316 dev_priv->display.commit_modeset_enables(state);
896e5bb0 16317
24a7bfe0
ID
16318 if (state->modeset) {
16319 intel_encoders_update_complete(state);
16320
5604e9ce 16321 intel_set_cdclk_post_plane_update(state);
24a7bfe0 16322 }
59f9e9ca 16323
94f05024
DV
16324 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
16325 * already, but still need the state for the delayed optimization. To
16326 * fix this:
16327 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
16328 * - schedule that vblank worker _before_ calling hw_done
16329 * - at the start of commit_tail, cancel it _synchrously
16330 * - switch over to the vblank wait helper in the core after that since
16331 * we don't need out special handling any more.
16332 */
855e0d68 16333 drm_atomic_helper_wait_for_flip_done(dev, &state->base);
5a21b665 16334
855e0d68 16335 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
1288f9b0
K
16336 if (new_crtc_state->uapi.async_flip)
16337 skl_disable_flip_done(crtc);
16338
1326a92c 16339 if (new_crtc_state->hw.active &&
855e0d68 16340 !needs_modeset(new_crtc_state) &&
0ccc42a2 16341 !new_crtc_state->preload_luts &&
2225f3c6 16342 (new_crtc_state->uapi.color_mgmt_changed ||
855e0d68
ML
16343 new_crtc_state->update_pipe))
16344 intel_color_load_luts(new_crtc_state);
051a6d8d
VS
16345 }
16346
5a21b665
DV
16347 /*
16348 * Now that the vblank has passed, we can go ahead and program the
16349 * optimal watermarks on platforms that need two-step watermark
16350 * programming.
16351 *
16352 * TODO: Move this (and other cleanup) to an async worker eventually.
16353 */
7181f5c5
VS
16354 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16355 new_crtc_state, i) {
16356 /*
16357 * Gen2 reports pipe underruns whenever all planes are disabled.
16358 * So re-enable underrun reporting after some planes get enabled.
16359 *
16360 * We do this before .optimize_watermarks() so that we have a
16361 * chance of catching underruns with the intermediate watermarks
16362 * vs. the new plane configuration.
16363 */
16364 if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
16365 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
16366
5a21b665 16367 if (dev_priv->display.optimize_watermarks)
7a8fdb1f 16368 dev_priv->display.optimize_watermarks(state, crtc);
5a21b665
DV
16369 }
16370
c7c0e7eb 16371 intel_dbuf_post_plane_update(state);
b06cf595 16372
855e0d68 16373 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
bee43ca4 16374 intel_post_plane_update(state, crtc);
5a21b665
DV
16375
16376 if (put_domains[i])
16377 modeset_put_power_domains(dev_priv, put_domains[i]);
16378
aa5e9b47 16379 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
afeda4f3
AM
16380
16381 /*
16382 * DSB cleanup is done in cleanup_work aligning with framebuffer
16383 * cleanup. So copy and reset the dsb structure to sync with
16384 * commit_done and later do dsb cleanup in cleanup_work.
16385 */
16386 old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
5a21b665
DV
16387 }
16388
7181f5c5
VS
16389 /* Underruns don't always raise interrupts, so check manually */
16390 intel_check_cpu_fifo_underruns(dev_priv);
16391 intel_check_pch_fifo_underruns(dev_priv);
16392
ecab0f3d 16393 if (state->modeset)
855e0d68 16394 intel_verify_planes(state);
cff109f0 16395
ecab0f3d 16396 intel_sagv_post_plane_update(state);
656d1b89 16397
855e0d68 16398 drm_atomic_helper_commit_hw_done(&state->base);
94f05024 16399
855e0d68 16400 if (state->modeset) {
d5553c09
CW
16401 /* As one of the primary mmio accessors, KMS has a high
16402 * likelihood of triggering bugs in unclaimed access. After we
16403 * finish modesetting, see if an error has been flagged, and if
16404 * so enable debugging for the next modeset - and hope we catch
16405 * the culprit.
16406 */
2cf7bf6f 16407 intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
0e6e0be4 16408 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
d5553c09 16409 }
855e0d68 16410 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
5a21b665 16411
8d52e447
CW
16412 /*
16413 * Defer the cleanup of the old state to a separate worker to not
16414 * impede the current task (userspace for blocking modesets) that
16415 * are executed inline. For out-of-line asynchronous modesets/flips,
16416 * deferring to a new worker seems overkill, but we would place a
16417 * schedule point (cond_resched()) here anyway to keep latencies
16418 * down.
16419 */
855e0d68
ML
16420 INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
16421 queue_work(system_highpri_wq, &state->base.commit_work);
94f05024
DV
16422}
16423
16424static void intel_atomic_commit_work(struct work_struct *work)
16425{
855e0d68
ML
16426 struct intel_atomic_state *state =
16427 container_of(work, struct intel_atomic_state, base.commit_work);
c004a90b 16428
94f05024
DV
16429 intel_atomic_commit_tail(state);
16430}
16431
c004a90b
CW
16432static int __i915_sw_fence_call
16433intel_atomic_commit_ready(struct i915_sw_fence *fence,
16434 enum i915_sw_fence_notify notify)
16435{
16436 struct intel_atomic_state *state =
16437 container_of(fence, struct intel_atomic_state, commit_ready);
16438
16439 switch (notify) {
16440 case FENCE_COMPLETE:
42b062b0 16441 /* we do blocking waits in the worker, nothing to do here */
c004a90b 16442 break;
c004a90b 16443 case FENCE_FREE:
eb955eee
CW
16444 {
16445 struct intel_atomic_helper *helper =
16446 &to_i915(state->base.dev)->atomic_helper;
16447
16448 if (llist_add(&state->freed, &helper->free_list))
16449 schedule_work(&helper->free_work);
16450 break;
16451 }
c004a90b
CW
16452 }
16453
16454 return NOTIFY_DONE;
16455}
16456
e3b4089c 16457static void intel_atomic_track_fbs(struct intel_atomic_state *state)
6c9c1b38 16458{
e3b4089c
VS
16459 struct intel_plane_state *old_plane_state, *new_plane_state;
16460 struct intel_plane *plane;
6c9c1b38
DV
16461 int i;
16462
e3b4089c
VS
16463 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
16464 new_plane_state, i)
7b3cb17a
ML
16465 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16466 to_intel_frontbuffer(new_plane_state->hw.fb),
8e7cb179 16467 plane->frontbuffer_bit);
6c9c1b38
DV
16468}
16469
94f05024 16470static int intel_atomic_commit(struct drm_device *dev,
a85fb467 16471 struct drm_atomic_state *_state,
94f05024
DV
16472 bool nonblock)
16473{
a85fb467 16474 struct intel_atomic_state *state = to_intel_atomic_state(_state);
fac5e23e 16475 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
16476 int ret = 0;
16477
a85fb467 16478 state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2e2f08d0 16479
a85fb467
VS
16480 drm_atomic_state_get(&state->base);
16481 i915_sw_fence_init(&state->commit_ready,
c004a90b 16482 intel_atomic_commit_ready);
94f05024 16483
440df938
VS
16484 /*
16485 * The intel_legacy_cursor_update() fast path takes care
16486 * of avoiding the vblank waits for simple cursor
16487 * movement and flips. For cursor on/off and size changes,
16488 * we want to perform the vblank waits so that watermark
16489 * updates happen during the correct frames. Gen9+ have
16490 * double buffered watermarks and so shouldn't need this.
16491 *
3cf50c63
ML
16492 * Unset state->legacy_cursor_update before the call to
16493 * drm_atomic_helper_setup_commit() because otherwise
16494 * drm_atomic_helper_wait_for_flip_done() is a noop and
16495 * we get FIFO underruns because we didn't wait
16496 * for vblank.
440df938
VS
16497 *
16498 * FIXME doing watermarks and fb cleanup from a vblank worker
16499 * (assuming we had any) would solve these problems.
16500 */
a85fb467 16501 if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
213f1bd0
ML
16502 struct intel_crtc_state *new_crtc_state;
16503 struct intel_crtc *crtc;
16504 int i;
16505
a85fb467 16506 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
213f1bd0
ML
16507 if (new_crtc_state->wm.need_postvbl_update ||
16508 new_crtc_state->update_wm_post)
a85fb467 16509 state->base.legacy_cursor_update = false;
213f1bd0 16510 }
440df938 16511
a85fb467 16512 ret = intel_atomic_prepare_commit(state);
3cf50c63 16513 if (ret) {
cd49f818
WK
16514 drm_dbg_atomic(&dev_priv->drm,
16515 "Preparing state failed with %i\n", ret);
a85fb467
VS
16516 i915_sw_fence_commit(&state->commit_ready);
16517 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
3cf50c63
ML
16518 return ret;
16519 }
16520
a85fb467 16521 ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
3cf50c63 16522 if (!ret)
a85fb467 16523 ret = drm_atomic_helper_swap_state(&state->base, true);
0ef1905e
VS
16524 if (!ret)
16525 intel_atomic_swap_global_state(state);
3cf50c63 16526
0806f4ee 16527 if (ret) {
afeda4f3
AM
16528 struct intel_crtc_state *new_crtc_state;
16529 struct intel_crtc *crtc;
16530 int i;
16531
a85fb467 16532 i915_sw_fence_commit(&state->commit_ready);
0806f4ee 16533
afeda4f3
AM
16534 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16535 intel_dsb_cleanup(new_crtc_state);
16536
a85fb467
VS
16537 drm_atomic_helper_cleanup_planes(dev, &state->base);
16538 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
0806f4ee
ML
16539 return ret;
16540 }
94f05024 16541 dev_priv->wm.distrust_bios_wm = false;
a85fb467
VS
16542 intel_shared_dpll_swap_state(state);
16543 intel_atomic_track_fbs(state);
94f05024 16544
a85fb467
VS
16545 drm_atomic_state_get(&state->base);
16546 INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
c004a90b 16547
a85fb467
VS
16548 i915_sw_fence_commit(&state->commit_ready);
16549 if (nonblock && state->modeset) {
16550 queue_work(dev_priv->modeset_wq, &state->base.commit_work);
757fffcf 16551 } else if (nonblock) {
c26a0586 16552 queue_work(dev_priv->flip_wq, &state->base.commit_work);
757fffcf 16553 } else {
a85fb467 16554 if (state->modeset)
757fffcf 16555 flush_workqueue(dev_priv->modeset_wq);
a85fb467 16556 intel_atomic_commit_tail(state);
757fffcf 16557 }
75714940 16558
74c090b1 16559 return 0;
7f27126e
JB
16560}
16561
74d290f8
CW
16562struct wait_rps_boost {
16563 struct wait_queue_entry wait;
16564
16565 struct drm_crtc *crtc;
e61e0f51 16566 struct i915_request *request;
74d290f8
CW
16567};
16568
16569static int do_rps_boost(struct wait_queue_entry *_wait,
16570 unsigned mode, int sync, void *key)
16571{
16572 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
e61e0f51 16573 struct i915_request *rq = wait->request;
74d290f8 16574
e9af4ea2
CW
16575 /*
16576 * If we missed the vblank, but the request is already running it
16577 * is reasonable to assume that it will complete before the next
16578 * vblank without our intervention, so leave RPS alone.
16579 */
e61e0f51 16580 if (!i915_request_started(rq))
3e7abf81 16581 intel_rps_boost(rq);
e61e0f51 16582 i915_request_put(rq);
74d290f8
CW
16583
16584 drm_crtc_vblank_put(wait->crtc);
16585
16586 list_del(&wait->wait.entry);
16587 kfree(wait);
16588 return 1;
16589}
16590
16591static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
16592 struct dma_fence *fence)
16593{
16594 struct wait_rps_boost *wait;
16595
16596 if (!dma_fence_is_i915(fence))
16597 return;
16598
16599 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
16600 return;
16601
16602 if (drm_crtc_vblank_get(crtc))
16603 return;
16604
16605 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
16606 if (!wait) {
16607 drm_crtc_vblank_put(crtc);
16608 return;
16609 }
16610
16611 wait->request = to_request(dma_fence_get(fence));
16612 wait->crtc = crtc;
16613
16614 wait->wait.func = do_rps_boost;
16615 wait->wait.flags = 0;
16616
16617 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
16618}
16619
ef1a1914
VS
16620static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
16621{
f90a85e7 16622 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
ef1a1914 16623 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a 16624 struct drm_framebuffer *fb = plane_state->hw.fb;
ef1a1914
VS
16625 struct i915_vma *vma;
16626
16627 if (plane->id == PLANE_CURSOR &&
d53db442 16628 INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
ef1a1914
VS
16629 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
16630 const int align = intel_cursor_alignment(dev_priv);
4a477651 16631 int err;
ef1a1914 16632
4a477651
CW
16633 err = i915_gem_object_attach_phys(obj, align);
16634 if (err)
16635 return err;
ef1a1914
VS
16636 }
16637
16638 vma = intel_pin_and_fence_fb_obj(fb,
f5929c53 16639 &plane_state->view,
ef1a1914
VS
16640 intel_plane_uses_fence(plane_state),
16641 &plane_state->flags);
16642 if (IS_ERR(vma))
16643 return PTR_ERR(vma);
16644
16645 plane_state->vma = vma;
16646
16647 return 0;
16648}
16649
16650static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
16651{
16652 struct i915_vma *vma;
16653
16654 vma = fetch_and_zero(&old_plane_state->vma);
16655 if (vma)
16656 intel_unpin_fb_vma(vma, old_plane_state->flags);
16657}
16658
b7268c5e
CW
16659static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
16660{
16661 struct i915_sched_attr attr = {
058179e7 16662 .priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
b7268c5e
CW
16663 };
16664
16665 i915_gem_object_wait_priority(obj, 0, &attr);
16666}
16667
6beb8c23
MR
16668/**
16669 * intel_prepare_plane_fb - Prepare fb for usage on plane
723196f4 16670 * @_plane: drm plane to prepare for
1fd37669 16671 * @_new_plane_state: the plane state being prepared
6beb8c23
MR
16672 *
16673 * Prepares a framebuffer for usage on a display plane. Generally this
16674 * involves pinning the underlying object and updating the frontbuffer tracking
16675 * bits. Some older platforms need special physical address handling for
16676 * cursor planes.
16677 *
16678 * Returns 0 on success, negative error code on failure.
16679 */
16680int
b2faf669 16681intel_prepare_plane_fb(struct drm_plane *_plane,
04c8b0bf 16682 struct drm_plane_state *_new_plane_state)
465c120c 16683{
b2faf669 16684 struct intel_plane *plane = to_intel_plane(_plane);
04c8b0bf
ML
16685 struct intel_plane_state *new_plane_state =
16686 to_intel_plane_state(_new_plane_state);
fa528334 16687 struct intel_atomic_state *state =
f90a85e7 16688 to_intel_atomic_state(new_plane_state->uapi.state);
b2faf669
VS
16689 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16690 const struct intel_plane_state *old_plane_state =
fa528334 16691 intel_atomic_get_old_plane_state(state, plane);
b2faf669
VS
16692 struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
16693 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
c004a90b 16694 int ret;
465c120c 16695
5008e874 16696 if (old_obj) {
b2faf669 16697 const struct intel_crtc_state *crtc_state =
fa528334 16698 intel_atomic_get_new_crtc_state(state,
b2faf669 16699 to_intel_crtc(old_plane_state->hw.crtc));
5008e874
ML
16700
16701 /* Big Hammer, we also need to ensure that any pending
16702 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
16703 * current scanout is retired before unpinning the old
16704 * framebuffer. Note that we rely on userspace rendering
16705 * into the buffer attached to the pipe they are waiting
16706 * on. If not, userspace generates a GPU hang with IPEHR
16707 * point to the MI_WAIT_FOR_EVENT.
16708 *
16709 * This should only fail upon a hung GPU, in which case we
16710 * can safely continue.
16711 */
c004a90b 16712 if (needs_modeset(crtc_state)) {
fa528334 16713 ret = i915_sw_fence_await_reservation(&state->commit_ready,
ef78f7b1 16714 old_obj->base.resv, NULL,
c004a90b
CW
16715 false, 0,
16716 GFP_KERNEL);
16717 if (ret < 0)
16718 return ret;
f4457ae7 16719 }
5008e874
ML
16720 }
16721
f90a85e7 16722 if (new_plane_state->uapi.fence) { /* explicit fencing */
fa528334 16723 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
f90a85e7 16724 new_plane_state->uapi.fence,
16dc224f 16725 i915_fence_timeout(dev_priv),
c004a90b
CW
16726 GFP_KERNEL);
16727 if (ret < 0)
16728 return ret;
16729 }
16730
c37efb99
CW
16731 if (!obj)
16732 return 0;
16733
4d3088c7 16734 ret = i915_gem_object_pin_pages(obj);
fd70075f
CW
16735 if (ret)
16736 return ret;
16737
04c8b0bf 16738 ret = intel_plane_pin_fb(new_plane_state);
fd70075f 16739
4d3088c7 16740 i915_gem_object_unpin_pages(obj);
fd70075f
CW
16741 if (ret)
16742 return ret;
16743
e2f3496e 16744 fb_obj_bump_render_priority(obj);
da42104f 16745 i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
07bcd99b 16746
f90a85e7 16747 if (!new_plane_state->uapi.fence) { /* implicit fencing */
74d290f8
CW
16748 struct dma_fence *fence;
16749
fa528334 16750 ret = i915_sw_fence_await_reservation(&state->commit_ready,
ef78f7b1 16751 obj->base.resv, NULL,
16dc224f
CW
16752 false,
16753 i915_fence_timeout(dev_priv),
c004a90b
CW
16754 GFP_KERNEL);
16755 if (ret < 0)
6fef8510 16756 goto unpin_fb;
74d290f8 16757
52791eee 16758 fence = dma_resv_get_excl_rcu(obj->base.resv);
74d290f8 16759 if (fence) {
7b3cb17a 16760 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
04c8b0bf 16761 fence);
74d290f8
CW
16762 dma_fence_put(fence);
16763 }
16764 } else {
7b3cb17a 16765 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
f90a85e7 16766 new_plane_state->uapi.fence);
c004a90b 16767 }
5a21b665 16768
60548c55
CW
16769 /*
16770 * We declare pageflips to be interactive and so merit a small bias
16771 * towards upclocking to deliver the frame on time. By only changing
16772 * the RPS thresholds to sample more regularly and aim for higher
16773 * clocks we can hopefully deliver low power workloads (like kodi)
16774 * that are not quite steady state without resorting to forcing
16775 * maximum clocks following a vblank miss (see do_rps_boost()).
16776 */
fa528334 16777 if (!state->rps_interactive) {
3e7abf81 16778 intel_rps_mark_interactive(&dev_priv->gt.rps, true);
fa528334 16779 state->rps_interactive = true;
60548c55
CW
16780 }
16781
d07f0e59 16782 return 0;
6fef8510
VS
16783
16784unpin_fb:
16785 intel_plane_unpin_fb(new_plane_state);
16786
16787 return ret;
6beb8c23
MR
16788}
16789
38f3ce3a
MR
16790/**
16791 * intel_cleanup_plane_fb - Cleans up an fb after plane use
16792 * @plane: drm plane to clean up for
1fd37669 16793 * @_old_plane_state: the state from the previous modeset
38f3ce3a
MR
16794 *
16795 * Cleans up a framebuffer that has just been removed from a plane.
16796 */
16797void
16798intel_cleanup_plane_fb(struct drm_plane *plane,
04c8b0bf 16799 struct drm_plane_state *_old_plane_state)
38f3ce3a 16800{
04c8b0bf
ML
16801 struct intel_plane_state *old_plane_state =
16802 to_intel_plane_state(_old_plane_state);
fa528334 16803 struct intel_atomic_state *state =
f90a85e7 16804 to_intel_atomic_state(old_plane_state->uapi.state);
ef1a1914 16805 struct drm_i915_private *dev_priv = to_i915(plane->dev);
58fa1760
VS
16806 struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16807
16808 if (!obj)
16809 return;
38f3ce3a 16810
fa528334 16811 if (state->rps_interactive) {
3e7abf81 16812 intel_rps_mark_interactive(&dev_priv->gt.rps, false);
fa528334 16813 state->rps_interactive = false;
60548c55
CW
16814 }
16815
be1e3415 16816 /* Should only be called after a successful intel_prepare_plane_fb()! */
04c8b0bf 16817 intel_plane_unpin_fb(old_plane_state);
465c120c
MR
16818}
16819
cf4c7c12 16820/**
4a3b8769
MR
16821 * intel_plane_destroy - destroy a plane
16822 * @plane: plane to destroy
cf4c7c12 16823 *
4a3b8769
MR
16824 * Common destruction function for all types of planes (primary, cursor,
16825 * sprite).
cf4c7c12 16826 */
4a3b8769 16827void intel_plane_destroy(struct drm_plane *plane)
465c120c 16828{
465c120c 16829 drm_plane_cleanup(plane);
69ae561f 16830 kfree(to_intel_plane(plane));
465c120c
MR
16831}
16832
a38189c5
VS
16833static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16834 u32 format, u64 modifier)
714244e2 16835{
a38189c5
VS
16836 switch (modifier) {
16837 case DRM_FORMAT_MOD_LINEAR:
16838 case I915_FORMAT_MOD_X_TILED:
16839 break;
16840 default:
16841 return false;
16842 }
16843
714244e2
BW
16844 switch (format) {
16845 case DRM_FORMAT_C8:
16846 case DRM_FORMAT_RGB565:
16847 case DRM_FORMAT_XRGB1555:
16848 case DRM_FORMAT_XRGB8888:
16849 return modifier == DRM_FORMAT_MOD_LINEAR ||
16850 modifier == I915_FORMAT_MOD_X_TILED;
16851 default:
16852 return false;
16853 }
16854}
16855
a38189c5
VS
16856static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16857 u32 format, u64 modifier)
714244e2 16858{
a38189c5
VS
16859 switch (modifier) {
16860 case DRM_FORMAT_MOD_LINEAR:
16861 case I915_FORMAT_MOD_X_TILED:
16862 break;
16863 default:
16864 return false;
16865 }
16866
714244e2
BW
16867 switch (format) {
16868 case DRM_FORMAT_C8:
16869 case DRM_FORMAT_RGB565:
16870 case DRM_FORMAT_XRGB8888:
16871 case DRM_FORMAT_XBGR8888:
73263cb6
VS
16872 case DRM_FORMAT_ARGB8888:
16873 case DRM_FORMAT_ABGR8888:
714244e2
BW
16874 case DRM_FORMAT_XRGB2101010:
16875 case DRM_FORMAT_XBGR2101010:
73263cb6
VS
16876 case DRM_FORMAT_ARGB2101010:
16877 case DRM_FORMAT_ABGR2101010:
03b0ce95 16878 case DRM_FORMAT_XBGR16161616F:
714244e2
BW
16879 return modifier == DRM_FORMAT_MOD_LINEAR ||
16880 modifier == I915_FORMAT_MOD_X_TILED;
16881 default:
16882 return false;
16883 }
16884}
16885
a38189c5
VS
16886static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16887 u32 format, u64 modifier)
714244e2 16888{
a38189c5
VS
16889 return modifier == DRM_FORMAT_MOD_LINEAR &&
16890 format == DRM_FORMAT_ARGB8888;
714244e2
BW
16891}
16892
679bfe84 16893static const struct drm_plane_funcs i965_plane_funcs = {
a38189c5
VS
16894 .update_plane = drm_atomic_helper_update_plane,
16895 .disable_plane = drm_atomic_helper_disable_plane,
16896 .destroy = intel_plane_destroy,
a38189c5
VS
16897 .atomic_duplicate_state = intel_plane_duplicate_state,
16898 .atomic_destroy_state = intel_plane_destroy_state,
16899 .format_mod_supported = i965_plane_format_mod_supported,
16900};
714244e2 16901
679bfe84 16902static const struct drm_plane_funcs i8xx_plane_funcs = {
70a101f8
MR
16903 .update_plane = drm_atomic_helper_update_plane,
16904 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 16905 .destroy = intel_plane_destroy,
ea2c67bb
MR
16906 .atomic_duplicate_state = intel_plane_duplicate_state,
16907 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 16908 .format_mod_supported = i8xx_plane_format_mod_supported,
465c120c
MR
16909};
16910
f79f2692 16911static int
4078c983
VS
16912intel_legacy_cursor_update(struct drm_plane *_plane,
16913 struct drm_crtc *_crtc,
f79f2692
ML
16914 struct drm_framebuffer *fb,
16915 int crtc_x, int crtc_y,
16916 unsigned int crtc_w, unsigned int crtc_h,
ba3f4d0a
JN
16917 u32 src_x, u32 src_y,
16918 u32 src_w, u32 src_h,
34a2ab5e 16919 struct drm_modeset_acquire_ctx *ctx)
f79f2692 16920{
4078c983
VS
16921 struct intel_plane *plane = to_intel_plane(_plane);
16922 struct intel_crtc *crtc = to_intel_crtc(_crtc);
16923 struct intel_plane_state *old_plane_state =
16924 to_intel_plane_state(plane->base.state);
16925 struct intel_plane_state *new_plane_state;
c249c5f6 16926 struct intel_crtc_state *crtc_state =
4078c983 16927 to_intel_crtc_state(crtc->base.state);
c249c5f6 16928 struct intel_crtc_state *new_crtc_state;
8e7cb179 16929 int ret;
f79f2692
ML
16930
16931 /*
16932 * When crtc is inactive or there is a modeset pending,
16933 * wait for it to complete in the slowpath
756c1b87
VS
16934 *
16935 * FIXME bigjoiner fastpath would be good
f79f2692 16936 */
1326a92c 16937 if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
756c1b87 16938 crtc_state->update_pipe || crtc_state->bigjoiner)
f79f2692
ML
16939 goto slow;
16940
669c9215
ML
16941 /*
16942 * Don't do an async update if there is an outstanding commit modifying
16943 * the plane. This prevents our async update's changes from getting
16944 * overridden by a previous synchronous update's state.
16945 */
f90a85e7
ML
16946 if (old_plane_state->uapi.commit &&
16947 !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
669c9215 16948 goto slow;
f79f2692
ML
16949
16950 /*
16951 * If any parameters change that may affect watermarks,
16952 * take the slowpath. Only changing fb or position should be
16953 * in the fastpath.
16954 */
a456f65f
ML
16955 if (old_plane_state->uapi.crtc != &crtc->base ||
16956 old_plane_state->uapi.src_w != src_w ||
16957 old_plane_state->uapi.src_h != src_h ||
16958 old_plane_state->uapi.crtc_w != crtc_w ||
16959 old_plane_state->uapi.crtc_h != crtc_h ||
16960 !old_plane_state->uapi.fb != !fb)
f79f2692
ML
16961 goto slow;
16962
4078c983 16963 new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
f79f2692
ML
16964 if (!new_plane_state)
16965 return -ENOMEM;
16966
4078c983 16967 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
c249c5f6
ML
16968 if (!new_crtc_state) {
16969 ret = -ENOMEM;
16970 goto out_free;
16971 }
16972
a456f65f 16973 drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
f79f2692 16974
a456f65f
ML
16975 new_plane_state->uapi.src_x = src_x;
16976 new_plane_state->uapi.src_y = src_y;
16977 new_plane_state->uapi.src_w = src_w;
16978 new_plane_state->uapi.src_h = src_h;
16979 new_plane_state->uapi.crtc_x = crtc_x;
16980 new_plane_state->uapi.crtc_y = crtc_y;
16981 new_plane_state->uapi.crtc_w = crtc_w;
16982 new_plane_state->uapi.crtc_h = crtc_h;
f79f2692 16983
8246d9c7 16984 intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
7d8d2cbc 16985
c249c5f6 16986 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
4078c983 16987 old_plane_state, new_plane_state);
f79f2692
ML
16988 if (ret)
16989 goto out_free;
16990
4078c983 16991 ret = intel_plane_pin_fb(new_plane_state);
ef1a1914 16992 if (ret)
2850748e 16993 goto out_free;
f79f2692 16994
a456f65f
ML
16995 intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16996 ORIGIN_FLIP);
16997 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16998 to_intel_frontbuffer(new_plane_state->hw.fb),
4078c983 16999 plane->frontbuffer_bit);
f79f2692
ML
17000
17001 /* Swap plane state */
a456f65f 17002 plane->base.state = &new_plane_state->uapi;
f79f2692 17003
c249c5f6
ML
17004 /*
17005 * We cannot swap crtc_state as it may be in use by an atomic commit or
17006 * page flip that's running simultaneously. If we swap crtc_state and
17007 * destroy the old state, we will cause a use-after-free there.
17008 *
17009 * Only update active_planes, which is needed for our internal
17010 * bookkeeping. Either value will do the right thing when updating
17011 * planes atomically. If the cursor was part of the atomic update then
17012 * we would have taken the slowpath.
17013 */
17014 crtc_state->active_planes = new_crtc_state->active_planes;
17015
a456f65f 17016 if (new_plane_state->uapi.visible)
4078c983 17017 intel_update_plane(plane, crtc_state, new_plane_state);
c48b86f9 17018 else
4078c983 17019 intel_disable_plane(plane, crtc_state);
f79f2692 17020
4078c983 17021 intel_plane_unpin_fb(old_plane_state);
f79f2692 17022
f79f2692 17023out_free:
c249c5f6 17024 if (new_crtc_state)
2225f3c6 17025 intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
669c9215 17026 if (ret)
a456f65f 17027 intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
669c9215 17028 else
a456f65f 17029 intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
f79f2692
ML
17030 return ret;
17031
f79f2692 17032slow:
4078c983 17033 return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
f79f2692 17034 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 17035 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
17036}
17037
17038static const struct drm_plane_funcs intel_cursor_plane_funcs = {
17039 .update_plane = intel_legacy_cursor_update,
17040 .disable_plane = drm_atomic_helper_disable_plane,
17041 .destroy = intel_plane_destroy,
f79f2692
ML
17042 .atomic_duplicate_state = intel_plane_duplicate_state,
17043 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 17044 .format_mod_supported = intel_cursor_format_mod_supported,
f79f2692
ML
17045};
17046
cf1805e6
VS
17047static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
17048 enum i9xx_plane_id i9xx_plane)
17049{
17050 if (!HAS_FBC(dev_priv))
17051 return false;
17052
17053 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
17054 return i9xx_plane == PLANE_A; /* tied to pipe A */
17055 else if (IS_IVYBRIDGE(dev_priv))
17056 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
17057 i9xx_plane == PLANE_C;
17058 else if (INTEL_GEN(dev_priv) >= 4)
17059 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
17060 else
17061 return i9xx_plane == PLANE_A;
17062}
17063
b079bd17 17064static struct intel_plane *
580503c7 17065intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 17066{
881440a8 17067 struct intel_plane *plane;
a38189c5 17068 const struct drm_plane_funcs *plane_funcs;
93ca7e00 17069 unsigned int supported_rotations;
881440a8
VS
17070 const u32 *formats;
17071 int num_formats;
ca9cab18 17072 int ret, zpos;
465c120c 17073
b7c80600
VS
17074 if (INTEL_GEN(dev_priv) >= 9)
17075 return skl_universal_plane_create(dev_priv, pipe,
17076 PLANE_PRIMARY);
17077
881440a8
VS
17078 plane = intel_plane_alloc();
17079 if (IS_ERR(plane))
17080 return plane;
ea2c67bb 17081
881440a8 17082 plane->pipe = pipe;
e3c566df
VS
17083 /*
17084 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
17085 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
17086 */
ddf08d32
VS
17087 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4 &&
17088 INTEL_NUM_PIPES(dev_priv) == 2)
881440a8 17089 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
e3c566df 17090 else
881440a8
VS
17091 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
17092 plane->id = PLANE_PRIMARY;
17093 plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
cf1805e6 17094
881440a8
VS
17095 plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
17096 if (plane->has_fbc) {
cf1805e6
VS
17097 struct intel_fbc *fbc = &dev_priv->fbc;
17098
881440a8 17099 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
cf1805e6
VS
17100 }
17101
73263cb6
VS
17102 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
17103 formats = vlv_primary_formats;
17104 num_formats = ARRAY_SIZE(vlv_primary_formats);
73263cb6 17105 } else if (INTEL_GEN(dev_priv) >= 4) {
03b0ce95
VS
17106 /*
17107 * WaFP16GammaEnabling:ivb
17108 * "Workaround : When using the 64-bit format, the plane
17109 * output on each color channel has one quarter amplitude.
17110 * It can be brought up to full amplitude by using pipe
17111 * gamma correction or pipe color space conversion to
17112 * multiply the plane output by four."
17113 *
17114 * There is no dedicated plane gamma for the primary plane,
17115 * and using the pipe gamma/csc could conflict with other
17116 * planes, so we choose not to expose fp16 on IVB primary
17117 * planes. HSW primary planes no longer have this problem.
17118 */
17119 if (IS_IVYBRIDGE(dev_priv)) {
17120 formats = ivb_primary_formats;
17121 num_formats = ARRAY_SIZE(ivb_primary_formats);
17122 } else {
17123 formats = i965_primary_formats;
17124 num_formats = ARRAY_SIZE(i965_primary_formats);
17125 }
6c0fd451 17126 } else {
881440a8 17127 formats = i8xx_primary_formats;
6c0fd451 17128 num_formats = ARRAY_SIZE(i8xx_primary_formats);
dbb1a6fb 17129 }
a8d201af 17130
dbb1a6fb
VS
17131 if (INTEL_GEN(dev_priv) >= 4)
17132 plane_funcs = &i965_plane_funcs;
17133 else
17134 plane_funcs = &i8xx_plane_funcs;
17135
17136 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17137 plane->min_cdclk = vlv_plane_min_cdclk;
17138 else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
17139 plane->min_cdclk = hsw_plane_min_cdclk;
17140 else if (IS_IVYBRIDGE(dev_priv))
17141 plane->min_cdclk = ivb_plane_min_cdclk;
17142 else
bb6ae9e6 17143 plane->min_cdclk = i9xx_plane_min_cdclk;
a38189c5 17144
dbb1a6fb
VS
17145 plane->max_stride = i9xx_plane_max_stride;
17146 plane->update_plane = i9xx_update_plane;
17147 plane->disable_plane = i9xx_disable_plane;
17148 plane->get_hw_state = i9xx_plane_get_hw_state;
17149 plane->check_plane = i9xx_plane_check;
465c120c 17150
b7c80600 17151 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
881440a8 17152 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
6875eb3f 17153 0, plane_funcs,
dbb1a6fb
VS
17154 formats, num_formats,
17155 i9xx_format_modifiers,
38573dc1
VS
17156 DRM_PLANE_TYPE_PRIMARY,
17157 "primary %c", pipe_name(pipe));
17158 else
881440a8 17159 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
6875eb3f 17160 0, plane_funcs,
dbb1a6fb
VS
17161 formats, num_formats,
17162 i9xx_format_modifiers,
38573dc1 17163 DRM_PLANE_TYPE_PRIMARY,
ed15030d 17164 "plane %c",
881440a8 17165 plane_name(plane->i9xx_plane));
fca0ce2a
VS
17166 if (ret)
17167 goto fail;
48404c1e 17168
b7c80600 17169 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
4ea7be2b 17170 supported_rotations =
c2c446ad
RF
17171 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
17172 DRM_MODE_REFLECT_X;
5481e27f 17173 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 17174 supported_rotations =
c2c446ad 17175 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 17176 } else {
c2c446ad 17177 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
17178 }
17179
5481e27f 17180 if (INTEL_GEN(dev_priv) >= 4)
881440a8 17181 drm_plane_create_rotation_property(&plane->base,
c2c446ad 17182 DRM_MODE_ROTATE_0,
93ca7e00 17183 supported_rotations);
48404c1e 17184
ca9cab18
VS
17185 zpos = 0;
17186 drm_plane_create_zpos_immutable_property(&plane->base, zpos);
17187
881440a8 17188 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
ea2c67bb 17189
881440a8 17190 return plane;
fca0ce2a
VS
17191
17192fail:
881440a8 17193 intel_plane_free(plane);
fca0ce2a 17194
b079bd17 17195 return ERR_PTR(ret);
465c120c
MR
17196}
17197
b079bd17 17198static struct intel_plane *
b2d03b0d
VS
17199intel_cursor_plane_create(struct drm_i915_private *dev_priv,
17200 enum pipe pipe)
3d7d6510 17201{
c539b579 17202 struct intel_plane *cursor;
ca9cab18 17203 int ret, zpos;
3d7d6510 17204
c539b579
VS
17205 cursor = intel_plane_alloc();
17206 if (IS_ERR(cursor))
17207 return cursor;
ea2c67bb 17208
3d7d6510 17209 cursor->pipe = pipe;
ed15030d 17210 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
b14e5848 17211 cursor->id = PLANE_CURSOR;
c19e1124 17212 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
b2d03b0d
VS
17213
17214 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
ddd5713d 17215 cursor->max_stride = i845_cursor_max_stride;
b2d03b0d
VS
17216 cursor->update_plane = i845_update_cursor;
17217 cursor->disable_plane = i845_disable_cursor;
51f5a096 17218 cursor->get_hw_state = i845_cursor_get_hw_state;
659056f2 17219 cursor->check_plane = i845_check_cursor;
b2d03b0d 17220 } else {
ddd5713d 17221 cursor->max_stride = i9xx_cursor_max_stride;
b2d03b0d
VS
17222 cursor->update_plane = i9xx_update_cursor;
17223 cursor->disable_plane = i9xx_disable_cursor;
51f5a096 17224 cursor->get_hw_state = i9xx_cursor_get_hw_state;
659056f2 17225 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 17226 }
3d7d6510 17227
cd5dcbf1
VS
17228 cursor->cursor.base = ~0;
17229 cursor->cursor.cntl = ~0;
024faac7
VS
17230
17231 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
17232 cursor->cursor.size = ~0;
3d7d6510 17233
580503c7 17234 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
6875eb3f 17235 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
17236 intel_cursor_formats,
17237 ARRAY_SIZE(intel_cursor_formats),
714244e2
BW
17238 cursor_format_modifiers,
17239 DRM_PLANE_TYPE_CURSOR,
38573dc1 17240 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
17241 if (ret)
17242 goto fail;
4398ad45 17243
5481e27f 17244 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 17245 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
17246 DRM_MODE_ROTATE_0,
17247 DRM_MODE_ROTATE_0 |
17248 DRM_MODE_ROTATE_180);
4398ad45 17249
ca9cab18
VS
17250 zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
17251 drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
17252
093a3a30
JRS
17253 if (INTEL_GEN(dev_priv) >= 12)
17254 drm_plane_enable_fb_damage_clips(&cursor->base);
17255
ea2c67bb
MR
17256 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
17257
b079bd17 17258 return cursor;
fca0ce2a
VS
17259
17260fail:
c539b579 17261 intel_plane_free(cursor);
fca0ce2a 17262
b079bd17 17263 return ERR_PTR(ret);
3d7d6510
MR
17264}
17265
08fa8fd0
VS
17266#define INTEL_CRTC_FUNCS \
17267 .gamma_set = drm_atomic_helper_legacy_gamma_set, \
17268 .set_config = drm_atomic_helper_set_config, \
17269 .destroy = intel_crtc_destroy, \
17270 .page_flip = drm_atomic_helper_page_flip, \
17271 .atomic_duplicate_state = intel_crtc_duplicate_state, \
17272 .atomic_destroy_state = intel_crtc_destroy_state, \
17273 .set_crc_source = intel_crtc_set_crc_source, \
17274 .verify_crc_source = intel_crtc_verify_crc_source, \
17275 .get_crc_sources = intel_crtc_get_crc_sources
17276
17277static const struct drm_crtc_funcs bdw_crtc_funcs = {
17278 INTEL_CRTC_FUNCS,
17279
17280 .get_vblank_counter = g4x_get_vblank_counter,
17281 .enable_vblank = bdw_enable_vblank,
17282 .disable_vblank = bdw_disable_vblank,
4bbffbf3 17283 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17284};
17285
17286static const struct drm_crtc_funcs ilk_crtc_funcs = {
17287 INTEL_CRTC_FUNCS,
17288
17289 .get_vblank_counter = g4x_get_vblank_counter,
17290 .enable_vblank = ilk_enable_vblank,
17291 .disable_vblank = ilk_disable_vblank,
4bbffbf3 17292 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17293};
17294
17295static const struct drm_crtc_funcs g4x_crtc_funcs = {
17296 INTEL_CRTC_FUNCS,
17297
17298 .get_vblank_counter = g4x_get_vblank_counter,
17299 .enable_vblank = i965_enable_vblank,
17300 .disable_vblank = i965_disable_vblank,
4bbffbf3 17301 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17302};
17303
17304static const struct drm_crtc_funcs i965_crtc_funcs = {
17305 INTEL_CRTC_FUNCS,
17306
17307 .get_vblank_counter = i915_get_vblank_counter,
17308 .enable_vblank = i965_enable_vblank,
17309 .disable_vblank = i965_disable_vblank,
4bbffbf3 17310 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17311};
17312
7d423af9 17313static const struct drm_crtc_funcs i915gm_crtc_funcs = {
08fa8fd0
VS
17314 INTEL_CRTC_FUNCS,
17315
17316 .get_vblank_counter = i915_get_vblank_counter,
7d423af9
VS
17317 .enable_vblank = i915gm_enable_vblank,
17318 .disable_vblank = i915gm_disable_vblank,
4bbffbf3 17319 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17320};
17321
17322static const struct drm_crtc_funcs i915_crtc_funcs = {
17323 INTEL_CRTC_FUNCS,
17324
17325 .get_vblank_counter = i915_get_vblank_counter,
17326 .enable_vblank = i8xx_enable_vblank,
17327 .disable_vblank = i8xx_disable_vblank,
4bbffbf3 17328 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17329};
17330
17331static const struct drm_crtc_funcs i8xx_crtc_funcs = {
17332 INTEL_CRTC_FUNCS,
17333
17334 /* no hw vblank counter */
17335 .enable_vblank = i8xx_enable_vblank,
17336 .disable_vblank = i8xx_disable_vblank,
4bbffbf3 17337 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17338};
17339
66434539 17340static struct intel_crtc *intel_crtc_alloc(void)
79e53945 17341{
66434539 17342 struct intel_crtc_state *crtc_state;
f44bfa7f 17343 struct intel_crtc *crtc;
79e53945 17344
f44bfa7f
VS
17345 crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
17346 if (!crtc)
66434539 17347 return ERR_PTR(-ENOMEM);
79e53945 17348
216383e9 17349 crtc_state = intel_crtc_state_alloc(crtc);
b079bd17 17350 if (!crtc_state) {
66434539
VS
17351 kfree(crtc);
17352 return ERR_PTR(-ENOMEM);
b079bd17 17353 }
66434539 17354
979e94c1 17355 crtc->base.state = &crtc_state->uapi;
f44bfa7f 17356 crtc->config = crtc_state;
f5de6e07 17357
66434539
VS
17358 return crtc;
17359}
17360
17361static void intel_crtc_free(struct intel_crtc *crtc)
17362{
17363 intel_crtc_destroy_state(&crtc->base, crtc->base.state);
17364 kfree(crtc);
17365}
17366
6875eb3f
AG
17367static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
17368{
17369 struct intel_plane *plane;
17370
17371 for_each_intel_plane(&dev_priv->drm, plane) {
17372 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
17373 plane->pipe);
17374
17375 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
17376 }
17377}
17378
66434539
VS
17379static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
17380{
17381 struct intel_plane *primary, *cursor;
17382 const struct drm_crtc_funcs *funcs;
17383 struct intel_crtc *crtc;
17384 int sprite, ret;
17385
17386 crtc = intel_crtc_alloc();
17387 if (IS_ERR(crtc))
17388 return PTR_ERR(crtc);
17389
17390 crtc->pipe = pipe;
17391 crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
17392
580503c7 17393 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
17394 if (IS_ERR(primary)) {
17395 ret = PTR_ERR(primary);
3d7d6510 17396 goto fail;
b079bd17 17397 }
f44bfa7f 17398 crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 17399
a81d6fa0 17400 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
17401 struct intel_plane *plane;
17402
580503c7 17403 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 17404 if (IS_ERR(plane)) {
b079bd17
VS
17405 ret = PTR_ERR(plane);
17406 goto fail;
17407 }
f44bfa7f 17408 crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
17409 }
17410
580503c7 17411 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 17412 if (IS_ERR(cursor)) {
b079bd17 17413 ret = PTR_ERR(cursor);
3d7d6510 17414 goto fail;
b079bd17 17415 }
f44bfa7f 17416 crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 17417
08fa8fd0
VS
17418 if (HAS_GMCH(dev_priv)) {
17419 if (IS_CHERRYVIEW(dev_priv) ||
17420 IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
17421 funcs = &g4x_crtc_funcs;
17422 else if (IS_GEN(dev_priv, 4))
17423 funcs = &i965_crtc_funcs;
7d423af9
VS
17424 else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
17425 funcs = &i915gm_crtc_funcs;
08fa8fd0
VS
17426 else if (IS_GEN(dev_priv, 3))
17427 funcs = &i915_crtc_funcs;
17428 else
17429 funcs = &i8xx_crtc_funcs;
17430 } else {
17431 if (INTEL_GEN(dev_priv) >= 8)
17432 funcs = &bdw_crtc_funcs;
17433 else
17434 funcs = &ilk_crtc_funcs;
17435 }
17436
f44bfa7f 17437 ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
b079bd17 17438 &primary->base, &cursor->base,
08fa8fd0 17439 funcs, "pipe %c", pipe_name(pipe));
3d7d6510
MR
17440 if (ret)
17441 goto fail;
79e53945 17442
1947fd13
VS
17443 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
17444 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
f44bfa7f 17445 dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
1947fd13
VS
17446
17447 if (INTEL_GEN(dev_priv) < 9) {
17448 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
17449
17450 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
17451 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
f44bfa7f 17452 dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
1947fd13 17453 }
22fd0fab 17454
6d1a2fde
PB
17455 if (INTEL_GEN(dev_priv) >= 10)
17456 drm_crtc_create_scaling_filter_property(&crtc->base,
17457 BIT(DRM_SCALING_FILTER_DEFAULT) |
17458 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
17459
f44bfa7f 17460 intel_color_init(crtc);
8563b1e8 17461
00535527
JN
17462 intel_crtc_crc_init(crtc);
17463
e57291c2 17464 drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
b079bd17
VS
17465
17466 return 0;
3d7d6510
MR
17467
17468fail:
66434539 17469 intel_crtc_free(crtc);
b079bd17
VS
17470
17471 return ret;
79e53945
JB
17472}
17473
6a20fe7b
VS
17474int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
17475 struct drm_file *file)
08d7b3d1 17476{
08d7b3d1 17477 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 17478 struct drm_crtc *drmmode_crtc;
c05422d5 17479 struct intel_crtc *crtc;
08d7b3d1 17480
418da172 17481 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
71240ed2 17482 if (!drmmode_crtc)
3f2c2057 17483 return -ENOENT;
08d7b3d1 17484
7707e653 17485 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 17486 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 17487
c05422d5 17488 return 0;
08d7b3d1
CW
17489}
17490
c08f995a 17491static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
79e53945 17492{
66a9278e
DV
17493 struct drm_device *dev = encoder->base.dev;
17494 struct intel_encoder *source_encoder;
c08f995a 17495 u32 possible_clones = 0;
79e53945 17496
b2784e15 17497 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 17498 if (encoders_cloneable(encoder, source_encoder))
c08f995a 17499 possible_clones |= drm_encoder_mask(&source_encoder->base);
79e53945 17500 }
4ef69c7a 17501
c08f995a 17502 return possible_clones;
79e53945
JB
17503}
17504
ed500bf6
VS
17505static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
17506{
17507 struct drm_device *dev = encoder->base.dev;
17508 struct intel_crtc *crtc;
17509 u32 possible_crtcs = 0;
17510
17511 for_each_intel_crtc(dev, crtc) {
981329ce 17512 if (encoder->pipe_mask & BIT(crtc->pipe))
ed500bf6
VS
17513 possible_crtcs |= drm_crtc_mask(&crtc->base);
17514 }
17515
17516 return possible_crtcs;
17517}
17518
a5916fd7 17519static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
4d302442 17520{
646d5772 17521 if (!IS_MOBILE(dev_priv))
4d302442
CW
17522 return false;
17523
dc008bf0 17524 if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
4d302442
CW
17525 return false;
17526
dc008bf0 17527 if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
17528 return false;
17529
17530 return true;
17531}
17532
63cb4e64 17533static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
84b4e042 17534{
6315b5d3 17535 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
17536 return false;
17537
50a0bc90 17538 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
17539 return false;
17540
4f8036a2 17541 if (HAS_PCH_LPT_H(dev_priv) &&
dc008bf0 17542 intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
17543 return false;
17544
70ac54d0 17545 /* DDI E can't be used if DDI A requires 4 lanes */
dc008bf0 17546 if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
17547 return false;
17548
e4abb733 17549 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
17550 return false;
17551
17552 return true;
17553}
17554
8090ba8c
ID
17555void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
17556{
17557 int pps_num;
17558 int pps_idx;
17559
17560 if (HAS_DDI(dev_priv))
17561 return;
17562 /*
17563 * This w/a is needed at least on CPT/PPT, but to be sure apply it
17564 * everywhere where registers can be write protected.
17565 */
17566 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17567 pps_num = 2;
17568 else
17569 pps_num = 1;
17570
17571 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
dc008bf0 17572 u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
8090ba8c
ID
17573
17574 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
dc008bf0 17575 intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
8090ba8c
ID
17576 }
17577}
17578
44cb734c
ID
17579static void intel_pps_init(struct drm_i915_private *dev_priv)
17580{
cc3f90f0 17581 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
17582 dev_priv->pps_mmio_base = PCH_PPS_BASE;
17583 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17584 dev_priv->pps_mmio_base = VLV_PPS_BASE;
17585 else
17586 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
17587
17588 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
17589}
17590
c39055b0 17591static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 17592{
4ef69c7a 17593 struct intel_encoder *encoder;
cb0953d7 17594 bool dpd_is_edp = false;
79e53945 17595
44cb734c
ID
17596 intel_pps_init(dev_priv);
17597
b81dddb9 17598 if (!HAS_DISPLAY(dev_priv))
fc0c5a9d
CW
17599 return;
17600
aefaa1f4
MR
17601 if (IS_ROCKETLAKE(dev_priv)) {
17602 intel_ddi_init(dev_priv, PORT_A);
17603 intel_ddi_init(dev_priv, PORT_B);
1d8ca002
VS
17604 intel_ddi_init(dev_priv, PORT_TC1);
17605 intel_ddi_init(dev_priv, PORT_TC2);
aefaa1f4 17606 } else if (INTEL_GEN(dev_priv) >= 12) {
55cd5048
MK
17607 intel_ddi_init(dev_priv, PORT_A);
17608 intel_ddi_init(dev_priv, PORT_B);
1d8ca002
VS
17609 intel_ddi_init(dev_priv, PORT_TC1);
17610 intel_ddi_init(dev_priv, PORT_TC2);
ba8a5cb2 17611 intel_ddi_init(dev_priv, PORT_TC3);
1d8ca002
VS
17612 intel_ddi_init(dev_priv, PORT_TC4);
17613 intel_ddi_init(dev_priv, PORT_TC5);
17614 intel_ddi_init(dev_priv, PORT_TC6);
33365fec 17615 icl_dsi_init(dev_priv);
24ea098b 17616 } else if (IS_JSL_EHL(dev_priv)) {
759c9ab5
BP
17617 intel_ddi_init(dev_priv, PORT_A);
17618 intel_ddi_init(dev_priv, PORT_B);
17619 intel_ddi_init(dev_priv, PORT_C);
719d2400 17620 intel_ddi_init(dev_priv, PORT_D);
759c9ab5 17621 icl_dsi_init(dev_priv);
55cd5048 17622 } else if (IS_GEN(dev_priv, 11)) {
00c92d92
PZ
17623 intel_ddi_init(dev_priv, PORT_A);
17624 intel_ddi_init(dev_priv, PORT_B);
17625 intel_ddi_init(dev_priv, PORT_C);
17626 intel_ddi_init(dev_priv, PORT_D);
17627 intel_ddi_init(dev_priv, PORT_E);
3f2e9ed0
ID
17628 /*
17629 * On some ICL SKUs port F is not present. No strap bits for
17630 * this, so rely on VBT.
2b34e562 17631 * Work around broken VBTs on SKUs known to have no port F.
3f2e9ed0 17632 */
2b34e562
ID
17633 if (IS_ICL_WITH_PORT_F(dev_priv) &&
17634 intel_bios_is_port_present(dev_priv, PORT_F))
3f2e9ed0
ID
17635 intel_ddi_init(dev_priv, PORT_F);
17636
bf4d57ff 17637 icl_dsi_init(dev_priv);
00c92d92 17638 } else if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
17639 /*
17640 * FIXME: Broxton doesn't support port detection via the
17641 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
17642 * detect the ports.
17643 */
c39055b0
ACO
17644 intel_ddi_init(dev_priv, PORT_A);
17645 intel_ddi_init(dev_priv, PORT_B);
17646 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 17647
e518634b 17648 vlv_dsi_init(dev_priv);
4f8036a2 17649 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
17650 int found;
17651
63cb4e64
JN
17652 if (intel_ddi_crt_present(dev_priv))
17653 intel_crt_init(dev_priv);
17654
de31facd
JB
17655 /*
17656 * Haswell uses DDI functions to detect digital outputs.
17657 * On SKL pre-D0 the strap isn't connected, so we assume
17658 * it's there.
17659 */
dc008bf0 17660 found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 17661 /* WaIgnoreDDIAStrap: skl */
b976dc53 17662 if (found || IS_GEN9_BC(dev_priv))
c39055b0 17663 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5 17664
9787e835 17665 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
0e72a5b5 17666 * register */
dc008bf0 17667 found = intel_de_read(dev_priv, SFUSE_STRAP);
0e72a5b5
ED
17668
17669 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 17670 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 17671 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 17672 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 17673 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 17674 intel_ddi_init(dev_priv, PORT_D);
9787e835
RV
17675 if (found & SFUSE_STRAP_DDIF_DETECTED)
17676 intel_ddi_init(dev_priv, PORT_F);
2800e4c2
RV
17677 /*
17678 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
17679 */
b976dc53 17680 if (IS_GEN9_BC(dev_priv) &&
e9d49bb7 17681 intel_bios_is_port_present(dev_priv, PORT_E))
c39055b0 17682 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 17683
6e266956 17684 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 17685 int found;
63cb4e64 17686
0fafa226
JN
17687 /*
17688 * intel_edp_init_connector() depends on this completing first,
17689 * to prevent the registration of both eDP and LVDS and the
17690 * incorrect sharing of the PPS.
17691 */
17692 intel_lvds_init(dev_priv);
74d021ea 17693 intel_crt_init(dev_priv);
63cb4e64 17694
7b91bf7f 17695 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
270b3042 17696
a5916fd7 17697 if (ilk_has_edp_a(dev_priv))
c39055b0 17698 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 17699
dc008bf0 17700 if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 17701 /* PCH SDVOB multiplex with HDMIB */
c39055b0 17702 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 17703 if (!found)
c39055b0 17704 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
dc008bf0 17705 if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
c39055b0 17706 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
17707 }
17708
dc008bf0 17709 if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
c39055b0 17710 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 17711
dc008bf0 17712 if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
c39055b0 17713 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 17714
dc008bf0 17715 if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
c39055b0 17716 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 17717
dc008bf0 17718 if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
c39055b0 17719 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 17720 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 17721 bool has_edp, has_port;
457c52d8 17722
63cb4e64
JN
17723 if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
17724 intel_crt_init(dev_priv);
17725
e17ac6db
VS
17726 /*
17727 * The DP_DETECTED bit is the latched state of the DDC
17728 * SDA pin at boot. However since eDP doesn't require DDC
17729 * (no way to plug in a DP->HDMI dongle) the DDC pins for
17730 * eDP ports may have been muxed to an alternate function.
17731 * Thus we can't rely on the DP_DETECTED bit alone to detect
17732 * eDP ports. Consult the VBT as well as DP_DETECTED to
17733 * detect eDP ports.
22f35042
VS
17734 *
17735 * Sadly the straps seem to be missing sometimes even for HDMI
17736 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
17737 * and VBT for the presence of the port. Additionally we can't
17738 * trust the port type the VBT declares as we've seen at least
17739 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 17740 */
7b91bf7f 17741 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
22f35042 17742 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
dc008bf0 17743 if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 17744 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
dc008bf0 17745 if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 17746 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 17747
7b91bf7f 17748 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
22f35042 17749 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
dc008bf0 17750 if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 17751 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
dc008bf0 17752 if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 17753 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 17754
920a14b2 17755 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
17756 /*
17757 * eDP not supported on port D,
17758 * so no need to worry about it
17759 */
17760 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
dc008bf0 17761 if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 17762 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
dc008bf0 17763 if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 17764 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
17765 }
17766
e518634b 17767 vlv_dsi_init(dev_priv);
63cb4e64 17768 } else if (IS_PINEVIEW(dev_priv)) {
0fafa226 17769 intel_lvds_init(dev_priv);
74d021ea 17770 intel_crt_init(dev_priv);
63cb4e64 17771 } else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
27185ae1 17772 bool found = false;
7d57382e 17773
9bedc7ed
JN
17774 if (IS_MOBILE(dev_priv))
17775 intel_lvds_init(dev_priv);
0fafa226 17776
74d021ea 17777 intel_crt_init(dev_priv);
63cb4e64 17778
dc008bf0 17779 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
cd49f818 17780 drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
c39055b0 17781 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 17782 if (!found && IS_G4X(dev_priv)) {
cd49f818
WK
17783 drm_dbg_kms(&dev_priv->drm,
17784 "probing HDMI on SDVOB\n");
c39055b0 17785 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 17786 }
27185ae1 17787
9beb5fea 17788 if (!found && IS_G4X(dev_priv))
c39055b0 17789 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 17790 }
13520b05
KH
17791
17792 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 17793
dc008bf0 17794 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
cd49f818 17795 drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
c39055b0 17796 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 17797 }
27185ae1 17798
dc008bf0 17799 if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 17800
9beb5fea 17801 if (IS_G4X(dev_priv)) {
cd49f818
WK
17802 drm_dbg_kms(&dev_priv->drm,
17803 "probing HDMI on SDVOC\n");
c39055b0 17804 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 17805 }
9beb5fea 17806 if (IS_G4X(dev_priv))
c39055b0 17807 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 17808 }
27185ae1 17809
dc008bf0 17810 if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
c39055b0 17811 intel_dp_init(dev_priv, DP_D, PORT_D);
d6521463
JN
17812
17813 if (SUPPORTS_TV(dev_priv))
17814 intel_tv_init(dev_priv);
63cb4e64 17815 } else if (IS_GEN(dev_priv, 2)) {
346073ce 17816 if (IS_I85X(dev_priv))
9bedc7ed 17817 intel_lvds_init(dev_priv);
0fafa226 17818
74d021ea 17819 intel_crt_init(dev_priv);
c39055b0 17820 intel_dvo_init(dev_priv);
63cb4e64 17821 }
79e53945 17822
c39055b0 17823 intel_psr_init(dev_priv);
7c8f8a70 17824
c39055b0 17825 for_each_intel_encoder(&dev_priv->drm, encoder) {
ed500bf6
VS
17826 encoder->base.possible_crtcs =
17827 intel_encoder_possible_crtcs(encoder);
4ef69c7a 17828 encoder->base.possible_clones =
c08f995a 17829 intel_encoder_possible_clones(encoder);
79e53945 17830 }
47356eb6 17831
c39055b0 17832 intel_init_pch_refclk(dev_priv);
270b3042 17833
c39055b0 17834 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
17835}
17836
17837static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17838{
17839 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 17840
ef2d633e 17841 drm_framebuffer_cleanup(fb);
8e7cb179 17842 intel_frontbuffer_put(intel_fb->frontbuffer);
70001cd2 17843
79e53945
JB
17844 kfree(intel_fb);
17845}
17846
17847static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 17848 struct drm_file *file,
79e53945
JB
17849 unsigned int *handle)
17850{
a5ff7a45 17851 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
cd49f818 17852 struct drm_i915_private *i915 = to_i915(obj->base.dev);
79e53945 17853
cc917ab4 17854 if (obj->userptr.mm) {
cd49f818
WK
17855 drm_dbg(&i915->drm,
17856 "attempting to use a userptr for a framebuffer, denied\n");
cc917ab4
CW
17857 return -EINVAL;
17858 }
17859
05394f39 17860 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
17861}
17862
86c98588
RV
17863static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17864 struct drm_file *file,
17865 unsigned flags, unsigned color,
17866 struct drm_clip_rect *clips,
17867 unsigned num_clips)
17868{
5a97bcc6 17869 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 17870
5a97bcc6 17871 i915_gem_object_flush_if_display(obj);
8e7cb179 17872 intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
86c98588
RV
17873
17874 return 0;
17875}
17876
79e53945
JB
17877static const struct drm_framebuffer_funcs intel_fb_funcs = {
17878 .destroy = intel_user_framebuffer_destroy,
17879 .create_handle = intel_user_framebuffer_create_handle,
86c98588 17880 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
17881};
17882
24dbf51a
CW
17883static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17884 struct drm_i915_gem_object *obj,
17885 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 17886{
24dbf51a 17887 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2e2adb05 17888 struct drm_framebuffer *fb = &intel_fb->base;
a88c40eb 17889 u32 max_stride;
dd689287 17890 unsigned int tiling, stride;
24dbf51a 17891 int ret = -EINVAL;
2e2adb05 17892 int i;
79e53945 17893
8e7cb179
CW
17894 intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17895 if (!intel_fb->frontbuffer)
17896 return -ENOMEM;
17897
80f0b679 17898 i915_gem_object_lock(obj, NULL);
dd689287
CW
17899 tiling = i915_gem_object_get_tiling(obj);
17900 stride = i915_gem_object_get_stride(obj);
17901 i915_gem_object_unlock(obj);
dd4916c5 17902
2a80eada 17903 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
17904 /*
17905 * If there's a fence, enforce that
17906 * the fb modifier and tiling mode match.
17907 */
17908 if (tiling != I915_TILING_NONE &&
17909 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
cd49f818
WK
17910 drm_dbg_kms(&dev_priv->drm,
17911 "tiling_mode doesn't match fb modifier\n");
24dbf51a 17912 goto err;
2a80eada
DV
17913 }
17914 } else {
c2ff7370 17915 if (tiling == I915_TILING_X) {
2a80eada 17916 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 17917 } else if (tiling == I915_TILING_Y) {
cd49f818
WK
17918 drm_dbg_kms(&dev_priv->drm,
17919 "No Y tiling for legacy addfb\n");
24dbf51a 17920 goto err;
2a80eada
DV
17921 }
17922 }
17923
17e8fd11
VS
17924 if (!drm_any_plane_has_format(&dev_priv->drm,
17925 mode_cmd->pixel_format,
17926 mode_cmd->modifier[0])) {
17927 struct drm_format_name_buf format_name;
17928
cd49f818
WK
17929 drm_dbg_kms(&dev_priv->drm,
17930 "unsupported pixel format %s / modifier 0x%llx\n",
17931 drm_get_format_name(mode_cmd->pixel_format,
17932 &format_name),
17933 mode_cmd->modifier[0]);
24dbf51a 17934 goto err;
c16ed4be 17935 }
57cd6508 17936
c2ff7370
VS
17937 /*
17938 * gen2/3 display engine uses the fence if present,
17939 * so the tiling mode must match the fb modifier exactly.
17940 */
c56b89f1 17941 if (INTEL_GEN(dev_priv) < 4 &&
c2ff7370 17942 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
cd49f818
WK
17943 drm_dbg_kms(&dev_priv->drm,
17944 "tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 17945 goto err;
c2ff7370
VS
17946 }
17947
a88c40eb
VS
17948 max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17949 mode_cmd->modifier[0]);
17950 if (mode_cmd->pitches[0] > max_stride) {
cd49f818
WK
17951 drm_dbg_kms(&dev_priv->drm,
17952 "%s pitch (%u) must be at most %d\n",
17953 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17954 "tiled" : "linear",
17955 mode_cmd->pitches[0], max_stride);
24dbf51a 17956 goto err;
c16ed4be 17957 }
5d7bd705 17958
c2ff7370
VS
17959 /*
17960 * If there's a fence, enforce that
17961 * the fb pitch and fence stride match.
17962 */
144cc143 17963 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
cd49f818
WK
17964 drm_dbg_kms(&dev_priv->drm,
17965 "pitch (%d) must match tiling stride (%d)\n",
17966 mode_cmd->pitches[0], stride);
24dbf51a 17967 goto err;
c16ed4be 17968 }
5d7bd705 17969
90f9a336 17970 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
5cf15dfc 17971 if (mode_cmd->offsets[0] != 0) {
cd49f818
WK
17972 drm_dbg_kms(&dev_priv->drm,
17973 "plane 0 offset (0x%08x) must be 0\n",
17974 mode_cmd->offsets[0]);
24dbf51a 17975 goto err;
5cf15dfc 17976 }
90f9a336 17977
2e2adb05 17978 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
d88c4afd 17979
2e2adb05
VS
17980 for (i = 0; i < fb->format->num_planes; i++) {
17981 u32 stride_alignment;
17982
17983 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
cd49f818
WK
17984 drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17985 i);
37875d6b 17986 goto err;
2e2adb05
VS
17987 }
17988
17989 stride_alignment = intel_fb_stride_alignment(fb, i);
2e2adb05 17990 if (fb->pitches[i] & (stride_alignment - 1)) {
cd49f818
WK
17991 drm_dbg_kms(&dev_priv->drm,
17992 "plane %d pitch (%d) must be at least %u byte aligned\n",
17993 i, fb->pitches[i], stride_alignment);
2e2adb05
VS
17994 goto err;
17995 }
d88c4afd 17996
71df86f0
ID
17997 if (is_gen12_ccs_plane(fb, i)) {
17998 int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17999
18000 if (fb->pitches[i] != ccs_aux_stride) {
cd49f818
WK
18001 drm_dbg_kms(&dev_priv->drm,
18002 "ccs aux plane %d pitch (%d) must be %d\n",
18003 i,
18004 fb->pitches[i], ccs_aux_stride);
71df86f0
ID
18005 goto err;
18006 }
18007 }
18008
a268bcd7
DS
18009 fb->obj[i] = &obj->base;
18010 }
c7d73f6a 18011
2e2adb05 18012 ret = intel_fill_fb_info(dev_priv, fb);
6687c906 18013 if (ret)
9aceb5c1 18014 goto err;
2d7a215f 18015
2e2adb05 18016 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
79e53945 18017 if (ret) {
cd49f818 18018 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
24dbf51a 18019 goto err;
79e53945
JB
18020 }
18021
79e53945 18022 return 0;
24dbf51a
CW
18023
18024err:
8e7cb179 18025 intel_frontbuffer_put(intel_fb->frontbuffer);
24dbf51a 18026 return ret;
79e53945
JB
18027}
18028
79e53945
JB
18029static struct drm_framebuffer *
18030intel_user_framebuffer_create(struct drm_device *dev,
18031 struct drm_file *filp,
1eb83451 18032 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 18033{
dcb1394e 18034 struct drm_framebuffer *fb;
05394f39 18035 struct drm_i915_gem_object *obj;
76dc3769 18036 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 18037
03ac0642
CW
18038 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
18039 if (!obj)
cce13ff7 18040 return ERR_PTR(-ENOENT);
79e53945 18041
24dbf51a 18042 fb = intel_framebuffer_create(obj, &mode_cmd);
8e7cb179 18043 i915_gem_object_put(obj);
dcb1394e
LW
18044
18045 return fb;
79e53945
JB
18046}
18047
e995ca0b
VS
18048static enum drm_mode_status
18049intel_mode_valid(struct drm_device *dev,
18050 const struct drm_display_mode *mode)
18051{
ad77c537
VS
18052 struct drm_i915_private *dev_priv = to_i915(dev);
18053 int hdisplay_max, htotal_max;
18054 int vdisplay_max, vtotal_max;
18055
e4dd27aa
VS
18056 /*
18057 * Can't reject DBLSCAN here because Xorg ddxen can add piles
18058 * of DBLSCAN modes to the output's mode list when they detect
18059 * the scaling mode property on the connector. And they don't
18060 * ask the kernel to validate those modes in any way until
18061 * modeset time at which point the client gets a protocol error.
18062 * So in order to not upset those clients we silently ignore the
18063 * DBLSCAN flag on such connectors. For other connectors we will
18064 * reject modes with the DBLSCAN flag in encoder->compute_config().
18065 * And we always reject DBLSCAN modes in connector->mode_valid()
18066 * as we never want such modes on the connector's mode list.
18067 */
18068
e995ca0b
VS
18069 if (mode->vscan > 1)
18070 return MODE_NO_VSCAN;
18071
e995ca0b
VS
18072 if (mode->flags & DRM_MODE_FLAG_HSKEW)
18073 return MODE_H_ILLEGAL;
18074
18075 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
18076 DRM_MODE_FLAG_NCSYNC |
18077 DRM_MODE_FLAG_PCSYNC))
18078 return MODE_HSYNC;
18079
18080 if (mode->flags & (DRM_MODE_FLAG_BCAST |
18081 DRM_MODE_FLAG_PIXMUX |
18082 DRM_MODE_FLAG_CLKDIV2))
18083 return MODE_BAD;
18084
2d20411e 18085 /* Transcoder timing limits */
12a97df0
MN
18086 if (INTEL_GEN(dev_priv) >= 11) {
18087 hdisplay_max = 16384;
18088 vdisplay_max = 8192;
18089 htotal_max = 16384;
18090 vtotal_max = 8192;
18091 } else if (INTEL_GEN(dev_priv) >= 9 ||
18092 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
ad77c537
VS
18093 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
18094 vdisplay_max = 4096;
18095 htotal_max = 8192;
18096 vtotal_max = 8192;
18097 } else if (INTEL_GEN(dev_priv) >= 3) {
18098 hdisplay_max = 4096;
18099 vdisplay_max = 4096;
18100 htotal_max = 8192;
18101 vtotal_max = 8192;
18102 } else {
18103 hdisplay_max = 2048;
18104 vdisplay_max = 2048;
18105 htotal_max = 4096;
18106 vtotal_max = 4096;
18107 }
18108
18109 if (mode->hdisplay > hdisplay_max ||
18110 mode->hsync_start > htotal_max ||
18111 mode->hsync_end > htotal_max ||
18112 mode->htotal > htotal_max)
18113 return MODE_H_ILLEGAL;
18114
18115 if (mode->vdisplay > vdisplay_max ||
18116 mode->vsync_start > vtotal_max ||
18117 mode->vsync_end > vtotal_max ||
18118 mode->vtotal > vtotal_max)
18119 return MODE_V_ILLEGAL;
18120
8f4b1068
VS
18121 if (INTEL_GEN(dev_priv) >= 5) {
18122 if (mode->hdisplay < 64 ||
18123 mode->htotal - mode->hdisplay < 32)
18124 return MODE_H_ILLEGAL;
18125
18126 if (mode->vtotal - mode->vdisplay < 5)
18127 return MODE_V_ILLEGAL;
18128 } else {
18129 if (mode->htotal - mode->hdisplay < 32)
18130 return MODE_H_ILLEGAL;
18131
18132 if (mode->vtotal - mode->vdisplay < 3)
18133 return MODE_V_ILLEGAL;
18134 }
18135
e995ca0b
VS
18136 return MODE_OK;
18137}
18138
2d20411e
VS
18139enum drm_mode_status
18140intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
63dc014e
ML
18141 const struct drm_display_mode *mode,
18142 bool bigjoiner)
2d20411e
VS
18143{
18144 int plane_width_max, plane_height_max;
18145
18146 /*
18147 * intel_mode_valid() should be
18148 * sufficient on older platforms.
18149 */
18150 if (INTEL_GEN(dev_priv) < 9)
18151 return MODE_OK;
18152
18153 /*
18154 * Most people will probably want a fullscreen
18155 * plane so let's not advertize modes that are
18156 * too big for that.
18157 */
18158 if (INTEL_GEN(dev_priv) >= 11) {
63dc014e 18159 plane_width_max = 5120 << bigjoiner;
2d20411e
VS
18160 plane_height_max = 4320;
18161 } else {
18162 plane_width_max = 5120;
18163 plane_height_max = 4096;
18164 }
18165
18166 if (mode->hdisplay > plane_width_max)
18167 return MODE_H_ILLEGAL;
18168
18169 if (mode->vdisplay > plane_height_max)
18170 return MODE_V_ILLEGAL;
18171
18172 return MODE_OK;
18173}
18174
79e53945 18175static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 18176 .fb_create = intel_user_framebuffer_create,
bbfb6ce8 18177 .get_format_info = intel_get_format_info,
0632fef6 18178 .output_poll_changed = intel_fbdev_output_poll_changed,
e995ca0b 18179 .mode_valid = intel_mode_valid,
5ee67f1c
MR
18180 .atomic_check = intel_atomic_check,
18181 .atomic_commit = intel_atomic_commit,
de419ab6
ML
18182 .atomic_state_alloc = intel_atomic_state_alloc,
18183 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 18184 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
18185};
18186
88212941
ID
18187/**
18188 * intel_init_display_hooks - initialize the display modesetting hooks
18189 * @dev_priv: device private
18190 */
18191void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 18192{
7ff89ca2
VS
18193 intel_init_cdclk_hooks(dev_priv);
18194
c56b89f1 18195 if (INTEL_GEN(dev_priv) >= 9) {
1e98f88c 18196 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
5724dbd1 18197 dev_priv->display.get_initial_plane_config =
f6df4d46 18198 skl_get_initial_plane_config;
1e98f88c
LDM
18199 dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
18200 dev_priv->display.crtc_enable = hsw_crtc_enable;
18201 dev_priv->display.crtc_disable = hsw_crtc_disable;
88212941 18202 } else if (HAS_DDI(dev_priv)) {
1e98f88c 18203 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
5724dbd1 18204 dev_priv->display.get_initial_plane_config =
81894b2f 18205 i9xx_get_initial_plane_config;
797d0259 18206 dev_priv->display.crtc_compute_clock =
1e98f88c
LDM
18207 hsw_crtc_compute_clock;
18208 dev_priv->display.crtc_enable = hsw_crtc_enable;
18209 dev_priv->display.crtc_disable = hsw_crtc_disable;
88212941 18210 } else if (HAS_PCH_SPLIT(dev_priv)) {
9eae5e27 18211 dev_priv->display.get_pipe_config = ilk_get_pipe_config;
5724dbd1 18212 dev_priv->display.get_initial_plane_config =
81894b2f 18213 i9xx_get_initial_plane_config;
3fb37703 18214 dev_priv->display.crtc_compute_clock =
9eae5e27
LDM
18215 ilk_crtc_compute_clock;
18216 dev_priv->display.crtc_enable = ilk_crtc_enable;
18217 dev_priv->display.crtc_disable = ilk_crtc_disable;
65b3d6a9 18218 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 18219 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
18220 dev_priv->display.get_initial_plane_config =
18221 i9xx_get_initial_plane_config;
65b3d6a9
ACO
18222 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
18223 dev_priv->display.crtc_enable = valleyview_crtc_enable;
18224 dev_priv->display.crtc_disable = i9xx_crtc_disable;
18225 } else if (IS_VALLEYVIEW(dev_priv)) {
18226 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18227 dev_priv->display.get_initial_plane_config =
18228 i9xx_get_initial_plane_config;
18229 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
18230 dev_priv->display.crtc_enable = valleyview_crtc_enable;
18231 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
18232 } else if (IS_G4X(dev_priv)) {
18233 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18234 dev_priv->display.get_initial_plane_config =
18235 i9xx_get_initial_plane_config;
18236 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
18237 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18238 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
18239 } else if (IS_PINEVIEW(dev_priv)) {
18240 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18241 dev_priv->display.get_initial_plane_config =
18242 i9xx_get_initial_plane_config;
18243 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
18244 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18245 dev_priv->display.crtc_disable = i9xx_crtc_disable;
cf819eff 18246 } else if (!IS_GEN(dev_priv, 2)) {
0e8ffe1b 18247 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
18248 dev_priv->display.get_initial_plane_config =
18249 i9xx_get_initial_plane_config;
d6dfee7a 18250 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
18251 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18252 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
18253 } else {
18254 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18255 dev_priv->display.get_initial_plane_config =
18256 i9xx_get_initial_plane_config;
18257 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
18258 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18259 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 18260 }
e70236a8 18261
cf819eff 18262 if (IS_GEN(dev_priv, 5)) {
9eae5e27 18263 dev_priv->display.fdi_link_train = ilk_fdi_link_train;
cf819eff 18264 } else if (IS_GEN(dev_priv, 6)) {
3bb11b53 18265 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 18266 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
18267 /* FIXME: detect B0+ stepping and use auto training */
18268 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
445e780b
VS
18269 }
18270
bd30ca2d 18271 if (INTEL_GEN(dev_priv) >= 9)
0c841271 18272 dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
27082493 18273 else
0c841271 18274 dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
66d9cec8 18275
e70236a8
JB
18276}
18277
6cd02e77 18278void intel_modeset_init_hw(struct drm_i915_private *i915)
f817586c 18279{
1965de63 18280 struct intel_cdclk_state *cdclk_state =
28a30b45 18281 to_intel_cdclk_state(i915->cdclk.obj.state);
3cf43cdc
VS
18282 struct intel_dbuf_state *dbuf_state =
18283 to_intel_dbuf_state(i915->dbuf.obj.state);
1965de63 18284
6cd02e77 18285 intel_update_cdclk(i915);
0bb94e03 18286 intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
1965de63 18287 cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
3cf43cdc
VS
18288
18289 dbuf_state->enabled_slices = i915->dbuf.enabled_slices;
f817586c
DV
18290}
18291
d1b2828a
VS
18292static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
18293{
18294 struct drm_plane *plane;
af157b76 18295 struct intel_crtc *crtc;
d1b2828a 18296
af157b76
VS
18297 for_each_intel_crtc(state->dev, crtc) {
18298 struct intel_crtc_state *crtc_state;
d1b2828a 18299
af157b76 18300 crtc_state = intel_atomic_get_crtc_state(state, crtc);
d1b2828a
VS
18301 if (IS_ERR(crtc_state))
18302 return PTR_ERR(crtc_state);
af157b76
VS
18303
18304 if (crtc_state->hw.active) {
18305 /*
18306 * Preserve the inherited flag to avoid
18307 * taking the full modeset path.
18308 */
a227569d 18309 crtc_state->inherited = true;
af157b76 18310 }
d1b2828a
VS
18311 }
18312
18313 drm_for_each_plane(plane, state->dev) {
18314 struct drm_plane_state *plane_state;
18315
18316 plane_state = drm_atomic_get_plane_state(state, plane);
18317 if (IS_ERR(plane_state))
18318 return PTR_ERR(plane_state);
18319 }
18320
18321 return 0;
18322}
18323
d93c0372
MR
18324/*
18325 * Calculate what we think the watermarks should be for the state we've read
18326 * out of the hardware and then immediately program those watermarks so that
18327 * we ensure the hardware settings match our internal state.
18328 *
18329 * We can calculate what we think WM's should be by creating a duplicate of the
18330 * current state (which was constructed during hardware readout) and running it
18331 * through the atomic check code to calculate new watermark values in the
18332 * state object.
18333 */
d1b2828a 18334static void sanitize_watermarks(struct drm_i915_private *dev_priv)
d93c0372 18335{
d93c0372 18336 struct drm_atomic_state *state;
ccf010fb 18337 struct intel_atomic_state *intel_state;
49743e1d
ML
18338 struct intel_crtc *crtc;
18339 struct intel_crtc_state *crtc_state;
d93c0372
MR
18340 struct drm_modeset_acquire_ctx ctx;
18341 int ret;
18342 int i;
18343
18344 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 18345 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
18346 return;
18347
d1b2828a 18348 state = drm_atomic_state_alloc(&dev_priv->drm);
e57291c2 18349 if (drm_WARN_ON(&dev_priv->drm, !state))
d1b2828a 18350 return;
d93c0372 18351
ccf010fb
ML
18352 intel_state = to_intel_atomic_state(state);
18353
d1b2828a
VS
18354 drm_modeset_acquire_init(&ctx, 0);
18355
18356retry:
18357 state->acquire_ctx = &ctx;
18358
ed4a6a7c
MR
18359 /*
18360 * Hardware readout is the only time we don't want to calculate
18361 * intermediate watermarks (since we don't trust the current
18362 * watermarks).
18363 */
b2ae318a 18364 if (!HAS_GMCH(dev_priv))
602ae835 18365 intel_state->skip_intermediate_wm = true;
ed4a6a7c 18366
d1b2828a
VS
18367 ret = sanitize_watermarks_add_affected(state);
18368 if (ret)
18369 goto fail;
18370
18371 ret = intel_atomic_check(&dev_priv->drm, state);
18372 if (ret)
18373 goto fail;
d93c0372
MR
18374
18375 /* Write calculated watermark values back */
49743e1d
ML
18376 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
18377 crtc_state->wm.need_postvbl_update = true;
7a8fdb1f 18378 dev_priv->display.optimize_watermarks(intel_state, crtc);
556fe36d 18379
49743e1d 18380 to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
d93c0372
MR
18381 }
18382
0cd1262d 18383fail:
d1b2828a
VS
18384 if (ret == -EDEADLK) {
18385 drm_atomic_state_clear(state);
18386 drm_modeset_backoff(&ctx);
18387 goto retry;
18388 }
18389
18390 /*
18391 * If we fail here, it means that the hardware appears to be
18392 * programmed in a way that shouldn't be possible, given our
18393 * understanding of watermark requirements. This might mean a
18394 * mistake in the hardware readout code or a mistake in the
18395 * watermark calculations for a given platform. Raise a WARN
18396 * so that this is noticeable.
18397 *
18398 * If this actually happens, we'll have to just leave the
18399 * BIOS-programmed watermarks untouched and hope for the best.
18400 */
e57291c2
PB
18401 drm_WARN(&dev_priv->drm, ret,
18402 "Could not determine valid watermarks for inherited state\n");
d1b2828a
VS
18403
18404 drm_atomic_state_put(state);
18405
d93c0372
MR
18406 drm_modeset_drop_locks(&ctx);
18407 drm_modeset_acquire_fini(&ctx);
18408}
18409
58ecd9d5
CW
18410static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
18411{
cf819eff 18412 if (IS_GEN(dev_priv, 5)) {
58ecd9d5 18413 u32 fdi_pll_clk =
dc008bf0 18414 intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
58ecd9d5
CW
18415
18416 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
cf819eff 18417 } else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
58ecd9d5
CW
18418 dev_priv->fdi_pll_freq = 270000;
18419 } else {
18420 return;
18421 }
18422
cd49f818 18423 drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
58ecd9d5
CW
18424}
18425
516a49cc
AS
18426static int intel_initial_commit(struct drm_device *dev)
18427{
18428 struct drm_atomic_state *state = NULL;
18429 struct drm_modeset_acquire_ctx ctx;
3558cafc 18430 struct intel_crtc *crtc;
516a49cc
AS
18431 int ret = 0;
18432
18433 state = drm_atomic_state_alloc(dev);
18434 if (!state)
18435 return -ENOMEM;
18436
18437 drm_modeset_acquire_init(&ctx, 0);
18438
18439retry:
18440 state->acquire_ctx = &ctx;
18441
3558cafc
ML
18442 for_each_intel_crtc(dev, crtc) {
18443 struct intel_crtc_state *crtc_state =
18444 intel_atomic_get_crtc_state(state, crtc);
18445
516a49cc
AS
18446 if (IS_ERR(crtc_state)) {
18447 ret = PTR_ERR(crtc_state);
18448 goto out;
18449 }
18450
1326a92c 18451 if (crtc_state->hw.active) {
b671d6ef
ID
18452 struct intel_encoder *encoder;
18453
af157b76
VS
18454 /*
18455 * We've not yet detected sink capabilities
18456 * (audio,infoframes,etc.) and thus we don't want to
18457 * force a full state recomputation yet. We want that to
18458 * happen only for the first real commit from userspace.
18459 * So preserve the inherited flag for the time being.
18460 */
a227569d 18461 crtc_state->inherited = true;
af157b76 18462
3558cafc 18463 ret = drm_atomic_add_affected_planes(state, &crtc->base);
516a49cc
AS
18464 if (ret)
18465 goto out;
fa6af514
VS
18466
18467 /*
18468 * FIXME hack to force a LUT update to avoid the
18469 * plane update forcing the pipe gamma on without
18470 * having a proper LUT loaded. Remove once we
18471 * have readout for pipe gamma enable.
18472 */
2225f3c6 18473 crtc_state->uapi.color_mgmt_changed = true;
a4277aa3 18474
b671d6ef
ID
18475 for_each_intel_encoder_mask(dev, encoder,
18476 crtc_state->uapi.encoder_mask) {
18477 if (encoder->initial_fastset_check &&
18478 !encoder->initial_fastset_check(encoder, crtc_state)) {
18479 ret = drm_atomic_add_affected_connectors(state,
18480 &crtc->base);
18481 if (ret)
18482 goto out;
18483 }
a4277aa3 18484 }
516a49cc
AS
18485 }
18486 }
18487
18488 ret = drm_atomic_commit(state);
18489
18490out:
18491 if (ret == -EDEADLK) {
18492 drm_atomic_state_clear(state);
18493 drm_modeset_backoff(&ctx);
18494 goto retry;
18495 }
18496
18497 drm_atomic_state_put(state);
18498
18499 drm_modeset_drop_locks(&ctx);
18500 drm_modeset_acquire_fini(&ctx);
18501
18502 return ret;
18503}
18504
e1a3d989
JN
18505static void intel_mode_config_init(struct drm_i915_private *i915)
18506{
18507 struct drm_mode_config *mode_config = &i915->drm.mode_config;
18508
18509 drm_mode_config_init(&i915->drm);
0ef1905e 18510 INIT_LIST_HEAD(&i915->global_obj_list);
e1a3d989
JN
18511
18512 mode_config->min_width = 0;
18513 mode_config->min_height = 0;
18514
18515 mode_config->preferred_depth = 24;
18516 mode_config->prefer_shadow = 1;
18517
18518 mode_config->allow_fb_modifiers = true;
18519
18520 mode_config->funcs = &intel_mode_funcs;
18521
55ea1cb1
K
18522 if (INTEL_GEN(i915) >= 9)
18523 mode_config->async_page_flip = true;
18524
e1a3d989
JN
18525 /*
18526 * Maximum framebuffer dimensions, chosen to match
18527 * the maximum render engine surface size on gen4+.
18528 */
18529 if (INTEL_GEN(i915) >= 7) {
18530 mode_config->max_width = 16384;
18531 mode_config->max_height = 16384;
18532 } else if (INTEL_GEN(i915) >= 4) {
18533 mode_config->max_width = 8192;
18534 mode_config->max_height = 8192;
18535 } else if (IS_GEN(i915, 3)) {
18536 mode_config->max_width = 4096;
18537 mode_config->max_height = 4096;
18538 } else {
18539 mode_config->max_width = 2048;
18540 mode_config->max_height = 2048;
18541 }
18542
18543 if (IS_I845G(i915) || IS_I865G(i915)) {
18544 mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
18545 mode_config->cursor_height = 1023;
5e9e0a3a
VS
18546 } else if (IS_I830(i915) || IS_I85X(i915) ||
18547 IS_I915G(i915) || IS_I915GM(i915)) {
e1a3d989
JN
18548 mode_config->cursor_width = 64;
18549 mode_config->cursor_height = 64;
18550 } else {
18551 mode_config->cursor_width = 256;
18552 mode_config->cursor_height = 256;
18553 }
18554}
18555
0ef1905e
VS
18556static void intel_mode_config_cleanup(struct drm_i915_private *i915)
18557{
18558 intel_atomic_global_obj_cleanup(i915);
18559 drm_mode_config_cleanup(&i915->drm);
18560}
18561
1586f620
CW
18562static void plane_config_fini(struct intel_initial_plane_config *plane_config)
18563{
18564 if (plane_config->fb) {
18565 struct drm_framebuffer *fb = &plane_config->fb->base;
18566
18567 /* We may only have the stub and not a full framebuffer */
18568 if (drm_framebuffer_read_refcount(fb))
18569 drm_framebuffer_put(fb);
18570 else
18571 kfree(fb);
18572 }
9c4ce97d
CW
18573
18574 if (plane_config->vma)
18575 i915_vma_put(plane_config->vma);
1586f620
CW
18576}
18577
80f286a5
JN
18578/* part #1: call before irq install */
18579int intel_modeset_init_noirq(struct drm_i915_private *i915)
79e53945 18580{
516a49cc 18581 int ret;
79e53945 18582
24d98a54
JN
18583 if (i915_inject_probe_failure(i915))
18584 return -ENODEV;
18585
da27bd41 18586 if (HAS_DISPLAY(i915)) {
24d98a54
JN
18587 ret = drm_vblank_init(&i915->drm,
18588 INTEL_NUM_PIPES(i915));
18589 if (ret)
18590 return ret;
18591 }
18592
18593 intel_bios_init(i915);
18594
18595 ret = intel_vga_register(i915);
18596 if (ret)
18597 goto cleanup_bios;
18598
18599 /* FIXME: completely on the wrong abstraction layer */
18600 intel_power_domains_init_hw(i915, false);
18601
18602 intel_csr_ucode_init(i915);
18603
6cd02e77
JN
18604 i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
18605 i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
18606 WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
757fffcf 18607
6cd02e77 18608 intel_mode_config_init(i915);
79e53945 18609
28a30b45
VS
18610 ret = intel_cdclk_init(i915);
18611 if (ret)
24d98a54 18612 goto cleanup_vga_client_pw_domain_csr;
28a30b45 18613
3cf43cdc
VS
18614 ret = intel_dbuf_init(i915);
18615 if (ret)
24d98a54 18616 goto cleanup_vga_client_pw_domain_csr;
3cf43cdc 18617
6cd02e77 18618 ret = intel_bw_init(i915);
c457d9cf 18619 if (ret)
24d98a54 18620 goto cleanup_vga_client_pw_domain_csr;
c457d9cf 18621
6cd02e77
JN
18622 init_llist_head(&i915->atomic_helper.free_list);
18623 INIT_WORK(&i915->atomic_helper.free_work,
ba318c61 18624 intel_atomic_helper_free_state_worker);
eb955eee 18625
6cd02e77 18626 intel_init_quirks(i915);
b690e96c 18627
6cd02e77 18628 intel_fbc_init(i915);
acde44b5 18629
80f286a5 18630 return 0;
24d98a54
JN
18631
18632cleanup_vga_client_pw_domain_csr:
18633 intel_csr_ucode_fini(i915);
18634 intel_power_domains_driver_remove(i915);
18635 intel_vga_unregister(i915);
18636cleanup_bios:
18637 intel_bios_driver_remove(i915);
18638
18639 return ret;
80f286a5
JN
18640}
18641
a5f2488f
JN
18642/* part #2: call after irq install, but before gem init */
18643int intel_modeset_init_nogem(struct drm_i915_private *i915)
80f286a5
JN
18644{
18645 struct drm_device *dev = &i915->drm;
18646 enum pipe pipe;
18647 struct intel_crtc *crtc;
18648 int ret;
18649
6cd02e77 18650 intel_init_pm(i915);
1fa61106 18651
6cd02e77 18652 intel_panel_sanitize_ssc(i915);
69f92f67 18653
9bfcf194
JN
18654 intel_gmbus_setup(i915);
18655
cd49f818
WK
18656 drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
18657 INTEL_NUM_PIPES(i915),
18658 INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
79e53945 18659
da27bd41 18660 if (HAS_DISPLAY(i915)) {
6cd02e77
JN
18661 for_each_pipe(i915, pipe) {
18662 ret = intel_crtc_init(i915, pipe);
ef404bc6 18663 if (ret) {
0ef1905e 18664 intel_mode_config_cleanup(i915);
ef404bc6
JN
18665 return ret;
18666 }
b079bd17 18667 }
79e53945
JB
18668 }
18669
6875eb3f 18670 intel_plane_possible_crtcs_init(i915);
e72f9fbf 18671 intel_shared_dpll_init(dev);
6cd02e77 18672 intel_update_fdi_pll_freq(i915);
ee7b9f93 18673
6cd02e77
JN
18674 intel_update_czclk(i915);
18675 intel_modeset_init_hw(i915);
5be6e334 18676
6cd02e77 18677 intel_hdcp_component_init(i915);
9055aac7 18678
6cd02e77
JN
18679 if (i915->max_cdclk_freq == 0)
18680 intel_update_max_cdclk(i915);
b2045352 18681
ddff9a60
MR
18682 /*
18683 * If the platform has HTI, we need to find out whether it has reserved
18684 * any display resources before we create our display outputs.
18685 */
18686 if (INTEL_INFO(i915)->display.has_hti)
18687 i915->hti_state = intel_de_read(i915, HDPORT_STATE);
18688
9cce37f4 18689 /* Just disable it once at startup */
4fb87831 18690 intel_vga_disable(i915);
6cd02e77 18691 intel_setup_outputs(i915);
11be49eb 18692
6e9f798d 18693 drm_modeset_lock_all(dev);
aecd36b8 18694 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 18695 drm_modeset_unlock_all(dev);
46f297fb 18696
d3fcc808 18697 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
18698 struct intel_initial_plane_config plane_config = {};
18699
0385ecea 18700 if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
46f297fb
JB
18701 continue;
18702
46f297fb 18703 /*
46f297fb
JB
18704 * Note that reserving the BIOS fb up front prevents us
18705 * from stuffing other stolen allocations like the ring
18706 * on top. This prevents some ugliness at boot time, and
18707 * can even allow for smooth boot transitions if the BIOS
18708 * fb is large enough for the active pipe configuration.
18709 */
6cd02e77 18710 i915->display.get_initial_plane_config(crtc, &plane_config);
eeebeac5
ML
18711
18712 /*
18713 * If the fb is shared between multiple heads, we'll
18714 * just get the first one.
18715 */
18716 intel_find_initial_plane_obj(crtc, &plane_config);
1586f620
CW
18717
18718 plane_config_fini(&plane_config);
46f297fb 18719 }
d93c0372
MR
18720
18721 /*
18722 * Make sure hardware watermarks really match the state we read out.
18723 * Note that we need to do this after reconstructing the BIOS fb's
18724 * since the watermark calculation done here will use pstate->fb.
18725 */
6cd02e77 18726 if (!HAS_GMCH(i915))
d1b2828a 18727 sanitize_watermarks(i915);
b079bd17 18728
516a49cc
AS
18729 /*
18730 * Force all active planes to recompute their states. So that on
18731 * mode_setcrtc after probe, all the intel_plane_state variables
18732 * are already calculated and there is no assert_plane warnings
18733 * during bootup.
18734 */
18735 ret = intel_initial_commit(dev);
18736 if (ret)
cd49f818 18737 drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
516a49cc 18738
b079bd17 18739 return 0;
2c7111db
CW
18740}
18741
a5f2488f
JN
18742/* part #3: call after gem init */
18743int intel_modeset_init(struct drm_i915_private *i915)
18744{
18745 int ret;
18746
da27bd41 18747 if (!HAS_DISPLAY(i915))
a5f2488f
JN
18748 return 0;
18749
71c8415d
JN
18750 intel_overlay_setup(i915);
18751
a5f2488f
JN
18752 ret = intel_fbdev_init(&i915->drm);
18753 if (ret)
18754 return ret;
18755
18756 /* Only enable hotplug handling once the fbdev is fully set up. */
18757 intel_hpd_init(i915);
4c8d4651 18758 intel_hpd_poll_disable(i915);
a5f2488f
JN
18759
18760 intel_init_ipc(i915);
18761
a5f2488f
JN
18762 return 0;
18763}
18764
2ee0da16
VS
18765void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18766{
d5fb43cb 18767 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
2ee0da16
VS
18768 /* 640x480@60Hz, ~25175 kHz */
18769 struct dpll clock = {
18770 .m1 = 18,
18771 .m2 = 7,
18772 .p1 = 13,
18773 .p2 = 4,
18774 .n = 2,
18775 };
18776 u32 dpll, fp;
18777 int i;
18778
e57291c2
PB
18779 drm_WARN_ON(&dev_priv->drm,
18780 i9xx_calc_dpll_params(48000, &clock) != 25154);
2ee0da16 18781
cd49f818
WK
18782 drm_dbg_kms(&dev_priv->drm,
18783 "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
18784 pipe_name(pipe), clock.vco, clock.dot);
2ee0da16
VS
18785
18786 fp = i9xx_dpll_compute_fp(&clock);
171d1562 18787 dpll = DPLL_DVO_2X_MODE |
2ee0da16
VS
18788 DPLL_VGA_MODE_DIS |
18789 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
18790 PLL_P2_DIVIDE_BY_4 |
18791 PLL_REF_INPUT_DREFCLK |
18792 DPLL_VCO_ENABLE;
18793
dc008bf0
JN
18794 intel_de_write(dev_priv, FP0(pipe), fp);
18795 intel_de_write(dev_priv, FP1(pipe), fp);
2ee0da16 18796
dc008bf0
JN
18797 intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
18798 intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
18799 intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
18800 intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
18801 intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
18802 intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
18803 intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
2ee0da16
VS
18804
18805 /*
18806 * Apparently we need to have VGA mode enabled prior to changing
18807 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
18808 * dividers, even though the register value does change.
18809 */
dc008bf0
JN
18810 intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
18811 intel_de_write(dev_priv, DPLL(pipe), dpll);
2ee0da16
VS
18812
18813 /* Wait for the clocks to stabilize. */
dc008bf0 18814 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18815 udelay(150);
18816
18817 /* The pixel multiplier can only be updated once the
18818 * DPLL is enabled and the clocks are stable.
18819 *
18820 * So write it again.
18821 */
dc008bf0 18822 intel_de_write(dev_priv, DPLL(pipe), dpll);
2ee0da16
VS
18823
18824 /* We do this three times for luck */
18825 for (i = 0; i < 3 ; i++) {
dc008bf0
JN
18826 intel_de_write(dev_priv, DPLL(pipe), dpll);
18827 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18828 udelay(150); /* wait for warmup */
18829 }
18830
dc008bf0
JN
18831 intel_de_write(dev_priv, PIPECONF(pipe),
18832 PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
18833 intel_de_posting_read(dev_priv, PIPECONF(pipe));
d5fb43cb
VS
18834
18835 intel_wait_for_pipe_scanline_moving(crtc);
2ee0da16
VS
18836}
18837
18838void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18839{
8fedd64d
VS
18840 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18841
cd49f818
WK
18842 drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
18843 pipe_name(pipe));
2ee0da16 18844
e57291c2
PB
18845 drm_WARN_ON(&dev_priv->drm,
18846 intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
18847 DISPLAY_PLANE_ENABLE);
18848 drm_WARN_ON(&dev_priv->drm,
18849 intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
18850 DISPLAY_PLANE_ENABLE);
18851 drm_WARN_ON(&dev_priv->drm,
18852 intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
18853 DISPLAY_PLANE_ENABLE);
18854 drm_WARN_ON(&dev_priv->drm,
18855 intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
18856 drm_WARN_ON(&dev_priv->drm,
18857 intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
2ee0da16 18858
dc008bf0
JN
18859 intel_de_write(dev_priv, PIPECONF(pipe), 0);
18860 intel_de_posting_read(dev_priv, PIPECONF(pipe));
2ee0da16 18861
8fedd64d 18862 intel_wait_for_pipe_scanline_stopped(crtc);
2ee0da16 18863
dc008bf0
JN
18864 intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18865 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18866}
18867
b1e01595
VS
18868static void
18869intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18870{
18871 struct intel_crtc *crtc;
fa555837 18872
b1e01595
VS
18873 if (INTEL_GEN(dev_priv) >= 4)
18874 return;
fa555837 18875
b1e01595
VS
18876 for_each_intel_crtc(&dev_priv->drm, crtc) {
18877 struct intel_plane *plane =
18878 to_intel_plane(crtc->base.primary);
62358aa4
VS
18879 struct intel_crtc *plane_crtc;
18880 enum pipe pipe;
b1e01595 18881
62358aa4
VS
18882 if (!plane->get_hw_state(plane, &pipe))
18883 continue;
18884
18885 if (pipe == crtc->pipe)
b1e01595
VS
18886 continue;
18887
cd49f818
WK
18888 drm_dbg_kms(&dev_priv->drm,
18889 "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18890 plane->base.base.id, plane->base.name);
62358aa4
VS
18891
18892 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18893 intel_plane_disable_noatomic(plane_crtc, plane);
b1e01595 18894 }
fa555837
DV
18895}
18896
02e93c35
VS
18897static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18898{
18899 struct drm_device *dev = crtc->base.dev;
18900 struct intel_encoder *encoder;
18901
18902 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18903 return true;
18904
18905 return false;
18906}
18907
496b0fc3
ML
18908static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18909{
18910 struct drm_device *dev = encoder->base.dev;
18911 struct intel_connector *connector;
18912
18913 for_each_connector_on_encoder(dev, &encoder->base, connector)
18914 return connector;
18915
18916 return NULL;
18917}
18918
a168f5b3 18919static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
ecf837d9 18920 enum pipe pch_transcoder)
a168f5b3
VS
18921{
18922 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
ecf837d9 18923 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
a168f5b3
VS
18924}
18925
cc7a4cff 18926static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
24929352 18927{
cc7a4cff
VS
18928 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18929 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1b52ad46 18930 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
24929352 18931
cc7a4cff
VS
18932 if (INTEL_GEN(dev_priv) >= 9 ||
18933 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18934 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18935 u32 val;
18936
18937 if (transcoder_is_dsi(cpu_transcoder))
18938 return;
18939
dc008bf0 18940 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18941 val &= ~HSW_FRAME_START_DELAY_MASK;
18942 val |= HSW_FRAME_START_DELAY(0);
dc008bf0 18943 intel_de_write(dev_priv, reg, val);
cc7a4cff 18944 } else {
4d1de975 18945 i915_reg_t reg = PIPECONF(cpu_transcoder);
cc7a4cff
VS
18946 u32 val;
18947
dc008bf0 18948 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18949 val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18950 val |= PIPECONF_FRAME_START_DELAY(0);
dc008bf0 18951 intel_de_write(dev_priv, reg, val);
cc7a4cff
VS
18952 }
18953
18954 if (!crtc_state->has_pch_encoder)
18955 return;
18956
18957 if (HAS_PCH_IBX(dev_priv)) {
18958 i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18959 u32 val;
18960
dc008bf0 18961 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18962 val &= ~TRANS_FRAME_START_DELAY_MASK;
18963 val |= TRANS_FRAME_START_DELAY(0);
dc008bf0 18964 intel_de_write(dev_priv, reg, val);
cc7a4cff 18965 } else {
7df49149
VS
18966 enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18967 i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
cc7a4cff 18968 u32 val;
4d1de975 18969
dc008bf0 18970 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18971 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18972 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
dc008bf0 18973 intel_de_write(dev_priv, reg, val);
4d1de975 18974 }
cc7a4cff
VS
18975}
18976
18977static void intel_sanitize_crtc(struct intel_crtc *crtc,
18978 struct drm_modeset_acquire_ctx *ctx)
18979{
18980 struct drm_device *dev = crtc->base.dev;
18981 struct drm_i915_private *dev_priv = to_i915(dev);
18982 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
24929352 18983
1326a92c 18984 if (crtc_state->hw.active) {
f9cd7b88
VS
18985 struct intel_plane *plane;
18986
cc7a4cff
VS
18987 /* Clear any frame start delays used for debugging left by the BIOS */
18988 intel_sanitize_frame_start_delay(crtc_state);
18989
f9cd7b88
VS
18990 /* Disable everything but the primary plane */
18991 for_each_intel_plane_on_crtc(dev, crtc, plane) {
b1e01595
VS
18992 const struct intel_plane_state *plane_state =
18993 to_intel_plane_state(plane->base.state);
f9cd7b88 18994
f90a85e7 18995 if (plane_state->uapi.visible &&
b1e01595
VS
18996 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18997 intel_plane_disable_noatomic(crtc, plane);
f9cd7b88 18998 }
c0550305
MR
18999
19000 /*
19001 * Disable any background color set by the BIOS, but enable the
19002 * gamma and CSC to match how we program our planes.
19003 */
19004 if (INTEL_GEN(dev_priv) >= 9)
dc008bf0
JN
19005 intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
19006 SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
9625604c 19007 }
d3eaf884 19008
24929352
DV
19009 /* Adjust the state of the output pipe according to whether we
19010 * have active connectors/encoders. */
0385ecea
MN
19011 if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
19012 !crtc_state->bigjoiner_slave)
56273062 19013 intel_crtc_disable_noatomic(crtc, ctx);
24929352 19014
1326a92c 19015 if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
4cc31489
DV
19016 /*
19017 * We start out with underrun reporting disabled to avoid races.
19018 * For correct bookkeeping mark this on active crtcs.
19019 *
c5ab3bc0
DV
19020 * Also on gmch platforms we dont have any hardware bits to
19021 * disable the underrun reporting. Which means we need to start
19022 * out with underrun reporting disabled also on inactive pipes,
19023 * since otherwise we'll complain about the garbage we read when
19024 * e.g. coming up after runtime pm.
19025 *
4cc31489
DV
19026 * No protection against concurrent access is required - at
19027 * worst a fifo underrun happens which also sets this to false.
19028 */
19029 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
19030 /*
19031 * We track the PCH trancoder underrun reporting state
19032 * within the crtc. With crtc for pipe A housing the underrun
19033 * reporting state for PCH transcoder A, crtc for pipe B housing
19034 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
19035 * and marking underrun reporting as disabled for the non-existing
19036 * PCH transcoders B and C would prevent enabling the south
19037 * error interrupt (see cpt_can_enable_serr_int()).
19038 */
ecf837d9 19039 if (has_pch_trancoder(dev_priv, crtc->pipe))
a168f5b3 19040 crtc->pch_fifo_underrun_disabled = true;
4cc31489 19041 }
24929352
DV
19042}
19043
7bed8adc
VS
19044static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
19045{
2225f3c6 19046 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7bed8adc
VS
19047
19048 /*
19049 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
19050 * the hardware when a high res displays plugged in. DPLL P
19051 * divider is zero, and the pipe timings are bonkers. We'll
19052 * try to disable everything in that case.
19053 *
19054 * FIXME would be nice to be able to sanitize this state
19055 * without several WARNs, but for now let's take the easy
19056 * road.
19057 */
19058 return IS_GEN(dev_priv, 6) &&
1326a92c 19059 crtc_state->hw.active &&
7bed8adc
VS
19060 crtc_state->shared_dpll &&
19061 crtc_state->port_clock == 0;
19062}
19063
24929352
DV
19064static void intel_sanitize_encoder(struct intel_encoder *encoder)
19065{
70332ac5 19066 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
24929352 19067 struct intel_connector *connector;
7bed8adc
VS
19068 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
19069 struct intel_crtc_state *crtc_state = crtc ?
19070 to_intel_crtc_state(crtc->base.state) : NULL;
24929352
DV
19071
19072 /* We need to check both for a crtc link (meaning that the
19073 * encoder is active and trying to read from a pipe) and the
19074 * pipe itself being active. */
7bed8adc 19075 bool has_active_crtc = crtc_state &&
1326a92c 19076 crtc_state->hw.active;
7bed8adc
VS
19077
19078 if (crtc_state && has_bogus_dpll_config(crtc_state)) {
cd49f818
WK
19079 drm_dbg_kms(&dev_priv->drm,
19080 "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
19081 pipe_name(crtc->pipe));
7bed8adc
VS
19082 has_active_crtc = false;
19083 }
24929352 19084
496b0fc3
ML
19085 connector = intel_encoder_find_connector(encoder);
19086 if (connector && !has_active_crtc) {
cd49f818
WK
19087 drm_dbg_kms(&dev_priv->drm,
19088 "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
19089 encoder->base.base.id,
19090 encoder->base.name);
24929352
DV
19091
19092 /* Connector is active, but has no active pipe. This is
19093 * fallout from our resume register restoring. Disable
19094 * the encoder manually again. */
7bed8adc
VS
19095 if (crtc_state) {
19096 struct drm_encoder *best_encoder;
fd6bbda9 19097
cd49f818
WK
19098 drm_dbg_kms(&dev_priv->drm,
19099 "[ENCODER:%d:%s] manually disabled\n",
19100 encoder->base.base.id,
19101 encoder->base.name);
7bed8adc
VS
19102
19103 /* avoid oopsing in case the hooks consult best_encoder */
19104 best_encoder = connector->base.state->best_encoder;
19105 connector->base.state->best_encoder = &encoder->base;
19106
ede9771d 19107 /* FIXME NULL atomic state passed! */
c84c6fe3 19108 if (encoder->disable)
ede9771d 19109 encoder->disable(NULL, encoder, crtc_state,
7bed8adc 19110 connector->base.state);
a62d1497 19111 if (encoder->post_disable)
ede9771d 19112 encoder->post_disable(NULL, encoder, crtc_state,
7bed8adc
VS
19113 connector->base.state);
19114
19115 connector->base.state->best_encoder = best_encoder;
24929352 19116 }
7f1950fb 19117 encoder->base.crtc = NULL;
24929352
DV
19118
19119 /* Inconsistent output/port/pipe state happens presumably due to
19120 * a bug in one of the get_hw_state functions. Or someplace else
19121 * in our code, like the register restore mess on resume. Clamp
19122 * things to off as a safer default. */
fd6bbda9
ML
19123
19124 connector->base.dpms = DRM_MODE_DPMS_OFF;
19125 connector->base.encoder = NULL;
24929352 19126 }
d6cae4aa
ML
19127
19128 /* notify opregion of the sanitized encoder state */
19129 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
70332ac5
ID
19130
19131 if (INTEL_GEN(dev_priv) >= 11)
19132 icl_sanitize_encoder_pll_mapping(encoder);
24929352
DV
19133}
19134
f9cd7b88 19135/* FIXME read out full plane state for all planes */
62358aa4 19136static void readout_plane_state(struct drm_i915_private *dev_priv)
d032ffa0 19137{
b1e01595 19138 struct intel_plane *plane;
62358aa4 19139 struct intel_crtc *crtc;
d032ffa0 19140
62358aa4 19141 for_each_intel_plane(&dev_priv->drm, plane) {
b1e01595
VS
19142 struct intel_plane_state *plane_state =
19143 to_intel_plane_state(plane->base.state);
62358aa4
VS
19144 struct intel_crtc_state *crtc_state;
19145 enum pipe pipe = PIPE_A;
eade6c89
VS
19146 bool visible;
19147
19148 visible = plane->get_hw_state(plane, &pipe);
b26d3ea3 19149
62358aa4
VS
19150 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
19151 crtc_state = to_intel_crtc_state(crtc->base.state);
19152
b1e01595 19153 intel_set_plane_visible(crtc_state, plane_state, visible);
7a4a2a46 19154
cd49f818
WK
19155 drm_dbg_kms(&dev_priv->drm,
19156 "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
19157 plane->base.base.id, plane->base.name,
19158 enableddisabled(visible), pipe_name(pipe));
b1e01595 19159 }
62358aa4
VS
19160
19161 for_each_intel_crtc(&dev_priv->drm, crtc) {
19162 struct intel_crtc_state *crtc_state =
19163 to_intel_crtc_state(crtc->base.state);
19164
19165 fixup_active_planes(crtc_state);
19166 }
98ec7739
VS
19167}
19168
30e984df 19169static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 19170{
fac5e23e 19171 struct drm_i915_private *dev_priv = to_i915(dev);
28a30b45
VS
19172 struct intel_cdclk_state *cdclk_state =
19173 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
3cf43cdc
VS
19174 struct intel_dbuf_state *dbuf_state =
19175 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
24929352 19176 enum pipe pipe;
24929352
DV
19177 struct intel_crtc *crtc;
19178 struct intel_encoder *encoder;
19179 struct intel_connector *connector;
f9e905ca 19180 struct drm_connector_list_iter conn_iter;
0c2d5512 19181 u8 active_pipes = 0;
24929352 19182
d3fcc808 19183 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
19184 struct intel_crtc_state *crtc_state =
19185 to_intel_crtc_state(crtc->base.state);
3b117c8f 19186
2225f3c6 19187 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
58d124ea 19188 intel_crtc_free_hw_state(crtc_state);
979e94c1 19189 intel_crtc_state_reset(crtc_state, crtc);
24929352 19190
291106cb
VS
19191 intel_crtc_get_pipe_config(crtc_state);
19192
19193 crtc_state->hw.enable = crtc_state->hw.active;
565602d7 19194
1326a92c
ML
19195 crtc->base.enabled = crtc_state->hw.enable;
19196 crtc->active = crtc_state->hw.active;
565602d7 19197
1326a92c 19198 if (crtc_state->hw.active)
0c2d5512 19199 active_pipes |= BIT(crtc->pipe);
565602d7 19200
cd49f818
WK
19201 drm_dbg_kms(&dev_priv->drm,
19202 "[CRTC:%d:%s] hw state readout: %s\n",
19203 crtc->base.base.id, crtc->base.name,
19204 enableddisabled(crtc_state->hw.active));
24929352
DV
19205 }
19206
3cf43cdc
VS
19207 dev_priv->active_pipes = cdclk_state->active_pipes =
19208 dbuf_state->active_pipes = active_pipes;
0c2d5512 19209
62358aa4
VS
19210 readout_plane_state(dev_priv);
19211
830b2cdc 19212 intel_dpll_readout_hw_state(dev_priv);
5358901f 19213
b2784e15 19214 for_each_intel_encoder(dev, encoder) {
24929352
DV
19215 pipe = 0;
19216
19217 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
19218 struct intel_crtc_state *crtc_state;
19219
98187836 19220 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 19221 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 19222
045ac3b5 19223 encoder->base.crtc = &crtc->base;
65c1ed30 19224 intel_encoder_get_config(encoder, crtc_state);
f9e76a6e
ID
19225 if (encoder->sync_state)
19226 encoder->sync_state(encoder, crtc_state);
0385ecea
MN
19227
19228 /* read out to slave crtc as well for bigjoiner */
19229 if (crtc_state->bigjoiner) {
19230 /* encoder should read be linked to bigjoiner master */
19231 WARN_ON(crtc_state->bigjoiner_slave);
19232
19233 crtc = crtc_state->bigjoiner_linked_crtc;
19234 crtc_state = to_intel_crtc_state(crtc->base.state);
19235 intel_encoder_get_config(encoder, crtc_state);
19236 }
24929352
DV
19237 } else {
19238 encoder->base.crtc = NULL;
19239 }
19240
cd49f818
WK
19241 drm_dbg_kms(&dev_priv->drm,
19242 "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
19243 encoder->base.base.id, encoder->base.name,
19244 enableddisabled(encoder->base.crtc),
19245 pipe_name(pipe));
24929352
DV
19246 }
19247
f9e905ca
DV
19248 drm_connector_list_iter_begin(dev, &conn_iter);
19249 for_each_intel_connector_iter(connector, &conn_iter) {
24929352 19250 if (connector->get_hw_state(connector)) {
de3b67af
VS
19251 struct intel_crtc_state *crtc_state;
19252 struct intel_crtc *crtc;
19253
24929352 19254 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9 19255
fa7edcd2 19256 encoder = intel_attached_encoder(connector);
2aa974c9
ML
19257 connector->base.encoder = &encoder->base;
19258
de3b67af
VS
19259 crtc = to_intel_crtc(encoder->base.crtc);
19260 crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
19261
1326a92c 19262 if (crtc_state && crtc_state->hw.active) {
2aa974c9
ML
19263 /*
19264 * This has to be done during hardware readout
19265 * because anything calling .crtc_disable may
19266 * rely on the connector_mask being accurate.
19267 */
2225f3c6 19268 crtc_state->uapi.connector_mask |=
40560e26 19269 drm_connector_mask(&connector->base);
2225f3c6 19270 crtc_state->uapi.encoder_mask |=
40560e26 19271 drm_encoder_mask(&encoder->base);
2aa974c9 19272 }
24929352
DV
19273 } else {
19274 connector->base.dpms = DRM_MODE_DPMS_OFF;
19275 connector->base.encoder = NULL;
19276 }
cd49f818
WK
19277 drm_dbg_kms(&dev_priv->drm,
19278 "[CONNECTOR:%d:%s] hw state readout: %s\n",
19279 connector->base.base.id, connector->base.name,
19280 enableddisabled(connector->base.encoder));
24929352 19281 }
f9e905ca 19282 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
19283
19284 for_each_intel_crtc(dev, crtc) {
c457d9cf
VS
19285 struct intel_bw_state *bw_state =
19286 to_intel_bw_state(dev_priv->bw_obj.state);
a8cd6da0
VS
19287 struct intel_crtc_state *crtc_state =
19288 to_intel_crtc_state(crtc->base.state);
c457d9cf 19289 struct intel_plane *plane;
d305e061 19290 int min_cdclk = 0;
aca1ebf4 19291
0385ecea
MN
19292 if (crtc_state->bigjoiner_slave)
19293 continue;
19294
1326a92c 19295 if (crtc_state->hw.active) {
7f4c6284
VS
19296 /*
19297 * The initial mode needs to be set in order to keep
19298 * the atomic core happy. It wants a valid mode if the
19299 * crtc's enabled, so we do the above call.
19300 *
7800fb69
DV
19301 * But we don't set all the derived state fully, hence
19302 * set a flag to indicate that a full recalculation is
19303 * needed on the next commit.
7f4c6284 19304 */
a227569d 19305 crtc_state->inherited = true;
9eca6832 19306
99325429 19307 intel_crtc_update_active_timings(crtc_state);
58d124ea
ML
19308
19309 intel_crtc_copy_hw_to_uapi_state(crtc_state);
7f4c6284 19310 }
e3b247da 19311
c457d9cf
VS
19312 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
19313 const struct intel_plane_state *plane_state =
19314 to_intel_plane_state(plane->base.state);
19315
19316 /*
19317 * FIXME don't have the fb yet, so can't
19318 * use intel_plane_data_rate() :(
19319 */
f90a85e7 19320 if (plane_state->uapi.visible)
c457d9cf
VS
19321 crtc_state->data_rate[plane->id] =
19322 4 * crtc_state->pixel_rate;
bb6ae9e6
VS
19323 /*
19324 * FIXME don't have the fb yet, so can't
19325 * use plane->min_cdclk() :(
19326 */
f90a85e7 19327 if (plane_state->uapi.visible && plane->min_cdclk) {
bb6ae9e6
VS
19328 if (crtc_state->double_wide ||
19329 INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
19330 crtc_state->min_cdclk[plane->id] =
19331 DIV_ROUND_UP(crtc_state->pixel_rate, 2);
19332 else
19333 crtc_state->min_cdclk[plane->id] =
19334 crtc_state->pixel_rate;
19335 }
cd49f818
WK
19336 drm_dbg_kms(&dev_priv->drm,
19337 "[PLANE:%d:%s] min_cdclk %d kHz\n",
19338 plane->base.base.id, plane->base.name,
19339 crtc_state->min_cdclk[plane->id]);
bb6ae9e6
VS
19340 }
19341
1326a92c 19342 if (crtc_state->hw.active) {
bb6ae9e6 19343 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
e57291c2 19344 if (drm_WARN_ON(dev, min_cdclk < 0))
bb6ae9e6 19345 min_cdclk = 0;
c457d9cf
VS
19346 }
19347
1965de63
VS
19348 cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
19349 cdclk_state->min_voltage_level[crtc->pipe] =
bb6ae9e6
VS
19350 crtc_state->min_voltage_level;
19351
c457d9cf
VS
19352 intel_bw_crtc_update(bw_state, crtc_state);
19353
a8cd6da0 19354 intel_pipe_config_sanity_check(dev_priv, crtc_state);
0385ecea
MN
19355
19356 /* discard our incomplete slave state, copy it from master */
19357 if (crtc_state->bigjoiner && crtc_state->hw.active) {
19358 struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
19359 struct intel_crtc_state *slave_crtc_state =
19360 to_intel_crtc_state(slave->base.state);
19361
19362 copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
19363 slave->base.mode = crtc->base.mode;
19364
19365 cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
19366 cdclk_state->min_voltage_level[slave->pipe] =
19367 crtc_state->min_voltage_level;
19368
19369 for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
19370 const struct intel_plane_state *plane_state =
19371 to_intel_plane_state(plane->base.state);
19372
19373 /*
19374 * FIXME don't have the fb yet, so can't
19375 * use intel_plane_data_rate() :(
19376 */
19377 if (plane_state->uapi.visible)
19378 crtc_state->data_rate[plane->id] =
19379 4 * crtc_state->pixel_rate;
19380 else
19381 crtc_state->data_rate[plane->id] = 0;
19382 }
19383
19384 intel_bw_crtc_update(bw_state, slave_crtc_state);
19385 drm_calc_timestamping_constants(&slave->base,
19386 &slave_crtc_state->hw.adjusted_mode);
19387 }
7f4c6284 19388 }
30e984df
DV
19389}
19390
62b69566
ACO
19391static void
19392get_encoder_power_domains(struct drm_i915_private *dev_priv)
19393{
19394 struct intel_encoder *encoder;
19395
19396 for_each_intel_encoder(&dev_priv->drm, encoder) {
52528055 19397 struct intel_crtc_state *crtc_state;
62b69566
ACO
19398
19399 if (!encoder->get_power_domains)
19400 continue;
19401
52528055 19402 /*
b79ebe74
ID
19403 * MST-primary and inactive encoders don't have a crtc state
19404 * and neither of these require any power domain references.
52528055 19405 */
b79ebe74
ID
19406 if (!encoder->base.crtc)
19407 continue;
52528055 19408
b79ebe74 19409 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
3a52fb7e 19410 encoder->get_power_domains(encoder, crtc_state);
62b69566
ACO
19411 }
19412}
19413
df49ec82
RV
19414static void intel_early_display_was(struct drm_i915_private *dev_priv)
19415{
1e1a139d
MR
19416 /*
19417 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
19418 * Also known as Wa_14010480278.
19419 */
19420 if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
dc008bf0
JN
19421 intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
19422 intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
df49ec82
RV
19423
19424 if (IS_HASWELL(dev_priv)) {
19425 /*
19426 * WaRsPkgCStateDisplayPMReq:hsw
19427 * System hang if this isn't done before disabling all planes!
19428 */
dc008bf0
JN
19429 intel_de_write(dev_priv, CHICKEN_PAR1_1,
19430 intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
df49ec82 19431 }
562ad8ad
VS
19432
19433 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
19434 /* Display WA #1142:kbl,cfl,cml */
19435 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
19436 KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
19437 intel_de_rmw(dev_priv, CHICKEN_MISC_2,
19438 KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
19439 KBL_ARB_FILL_SPARE_14);
19440 }
df49ec82
RV
19441}
19442
3aefb67f
VS
19443static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
19444 enum port port, i915_reg_t hdmi_reg)
19445{
dc008bf0 19446 u32 val = intel_de_read(dev_priv, hdmi_reg);
3aefb67f
VS
19447
19448 if (val & SDVO_ENABLE ||
19449 (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
19450 return;
19451
cd49f818
WK
19452 drm_dbg_kms(&dev_priv->drm,
19453 "Sanitizing transcoder select for HDMI %c\n",
19454 port_name(port));
3aefb67f
VS
19455
19456 val &= ~SDVO_PIPE_SEL_MASK;
19457 val |= SDVO_PIPE_SEL(PIPE_A);
19458
dc008bf0 19459 intel_de_write(dev_priv, hdmi_reg, val);
3aefb67f
VS
19460}
19461
19462static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
19463 enum port port, i915_reg_t dp_reg)
19464{
dc008bf0 19465 u32 val = intel_de_read(dev_priv, dp_reg);
3aefb67f
VS
19466
19467 if (val & DP_PORT_EN ||
19468 (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
19469 return;
19470
cd49f818
WK
19471 drm_dbg_kms(&dev_priv->drm,
19472 "Sanitizing transcoder select for DP %c\n",
19473 port_name(port));
3aefb67f
VS
19474
19475 val &= ~DP_PIPE_SEL_MASK;
19476 val |= DP_PIPE_SEL(PIPE_A);
19477
dc008bf0 19478 intel_de_write(dev_priv, dp_reg, val);
3aefb67f
VS
19479}
19480
19481static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
19482{
19483 /*
19484 * The BIOS may select transcoder B on some of the PCH
19485 * ports even it doesn't enable the port. This would trip
19486 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
19487 * Sanitize the transcoder select bits to prevent that. We
19488 * assume that the BIOS never actually enabled the port,
19489 * because if it did we'd actually have to toggle the port
19490 * on and back off to make the transcoder A select stick
19491 * (see. intel_dp_link_down(), intel_disable_hdmi(),
19492 * intel_disable_sdvo()).
19493 */
19494 ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
19495 ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
19496 ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
19497
19498 /* PCH SDVOB multiplex with HDMIB */
19499 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
19500 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
19501 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
19502}
19503
043e9bda
ML
19504/* Scan out the current hw modeset state,
19505 * and sanitizes it to the current state
19506 */
19507static void
aecd36b8
VS
19508intel_modeset_setup_hw_state(struct drm_device *dev,
19509 struct drm_modeset_acquire_ctx *ctx)
30e984df 19510{
fac5e23e 19511 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 19512 struct intel_encoder *encoder;
0e6e0be4
CW
19513 struct intel_crtc *crtc;
19514 intel_wakeref_t wakeref;
30e984df 19515
0e6e0be4 19516 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
2cd9a689 19517
df49ec82 19518 intel_early_display_was(dev_priv);
30e984df 19519 intel_modeset_readout_hw_state(dev);
24929352
DV
19520
19521 /* HW state is read out, now we need to sanitize this mess. */
32691b58
ID
19522
19523 /* Sanitize the TypeC port mode upfront, encoders depend on this */
19524 for_each_intel_encoder(dev, encoder) {
d8fe2ab6
MR
19525 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
19526
32691b58
ID
19527 /* We need to sanitize only the MST primary port. */
19528 if (encoder->type != INTEL_OUTPUT_DP_MST &&
d8fe2ab6 19529 intel_phy_is_tc(dev_priv, phy))
b7d02c3a 19530 intel_tc_port_sanitize(enc_to_dig_port(encoder));
32691b58
ID
19531 }
19532
62b69566 19533 get_encoder_power_domains(dev_priv);
3aefb67f
VS
19534
19535 if (HAS_PCH_IBX(dev_priv))
19536 ibx_sanitize_pch_ports(dev_priv);
62b69566 19537
68bc30de
VS
19538 /*
19539 * intel_sanitize_plane_mapping() may need to do vblank
19540 * waits, so we need vblank interrupts restored beforehand.
19541 */
19542 for_each_intel_crtc(&dev_priv->drm, crtc) {
777bf6d7
VS
19543 struct intel_crtc_state *crtc_state =
19544 to_intel_crtc_state(crtc->base.state);
32db0b65 19545
68bc30de 19546 drm_crtc_vblank_reset(&crtc->base);
b1e01595 19547
1326a92c 19548 if (crtc_state->hw.active)
32db0b65 19549 intel_crtc_vblank_on(crtc_state);
24929352
DV
19550 }
19551
68bc30de 19552 intel_sanitize_plane_mapping(dev_priv);
e2af48c6 19553
68bc30de
VS
19554 for_each_intel_encoder(dev, encoder)
19555 intel_sanitize_encoder(encoder);
19556
19557 for_each_intel_crtc(&dev_priv->drm, crtc) {
777bf6d7 19558 struct intel_crtc_state *crtc_state =
7b0bcead 19559 to_intel_crtc_state(crtc->base.state);
777bf6d7 19560
aecd36b8 19561 intel_sanitize_crtc(crtc, ctx);
10d75f54 19562 intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
24929352 19563 }
9a935856 19564
d29b2f9d
ACO
19565 intel_modeset_update_connector_atomic_state(dev);
19566
830b2cdc 19567 intel_dpll_sanitize_state(dev_priv);
35c95375 19568
04548cba 19569 if (IS_G4X(dev_priv)) {
cd1d3ee9 19570 g4x_wm_get_hw_state(dev_priv);
04548cba
VS
19571 g4x_wm_sanitize(dev_priv);
19572 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
cd1d3ee9 19573 vlv_wm_get_hw_state(dev_priv);
602ae835 19574 vlv_wm_sanitize(dev_priv);
a029fa4d 19575 } else if (INTEL_GEN(dev_priv) >= 9) {
cd1d3ee9 19576 skl_wm_get_hw_state(dev_priv);
602ae835 19577 } else if (HAS_PCH_SPLIT(dev_priv)) {
cd1d3ee9 19578 ilk_wm_get_hw_state(dev_priv);
602ae835 19579 }
292b990e
ML
19580
19581 for_each_intel_crtc(dev, crtc) {
777bf6d7
VS
19582 struct intel_crtc_state *crtc_state =
19583 to_intel_crtc_state(crtc->base.state);
d8fc70b7 19584 u64 put_domains;
292b990e 19585
afe0c21b 19586 put_domains = modeset_get_crtc_power_domains(crtc_state);
e57291c2 19587 if (drm_WARN_ON(dev, put_domains))
292b990e
ML
19588 modeset_put_power_domains(dev_priv, put_domains);
19589 }
2cd9a689 19590
0e6e0be4 19591 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
043e9bda 19592}
7d0bc1ea 19593
043e9bda
ML
19594void intel_display_resume(struct drm_device *dev)
19595{
e2c8b870
ML
19596 struct drm_i915_private *dev_priv = to_i915(dev);
19597 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
19598 struct drm_modeset_acquire_ctx ctx;
043e9bda 19599 int ret;
f30da187 19600
e2c8b870 19601 dev_priv->modeset_restore_state = NULL;
73974893
ML
19602 if (state)
19603 state->acquire_ctx = &ctx;
043e9bda 19604
e2c8b870 19605 drm_modeset_acquire_init(&ctx, 0);
043e9bda 19606
73974893
ML
19607 while (1) {
19608 ret = drm_modeset_lock_all_ctx(dev, &ctx);
19609 if (ret != -EDEADLK)
19610 break;
043e9bda 19611
e2c8b870 19612 drm_modeset_backoff(&ctx);
e2c8b870 19613 }
043e9bda 19614
73974893 19615 if (!ret)
581e49fe 19616 ret = __intel_display_resume(dev, state, &ctx);
73974893 19617
2503a0fe 19618 intel_enable_ipc(dev_priv);
e2c8b870
ML
19619 drm_modeset_drop_locks(&ctx);
19620 drm_modeset_acquire_fini(&ctx);
043e9bda 19621
0853695c 19622 if (ret)
cd49f818
WK
19623 drm_err(&dev_priv->drm,
19624 "Restoring old state failed with %i\n", ret);
3c5e37f1
CW
19625 if (state)
19626 drm_atomic_state_put(state);
2c7111db
CW
19627}
19628
9980c3c1 19629static void intel_hpd_poll_fini(struct drm_i915_private *i915)
886c6b86
MN
19630{
19631 struct intel_connector *connector;
19632 struct drm_connector_list_iter conn_iter;
19633
448aa911 19634 /* Kill all the work that may have been queued by hpd. */
9980c3c1 19635 drm_connector_list_iter_begin(&i915->drm, &conn_iter);
886c6b86
MN
19636 for_each_intel_connector_iter(connector, &conn_iter) {
19637 if (connector->modeset_retry_work.func)
19638 cancel_work_sync(&connector->modeset_retry_work);
d3dacc70
R
19639 if (connector->hdcp.shim) {
19640 cancel_delayed_work_sync(&connector->hdcp.check_work);
19641 cancel_work_sync(&connector->hdcp.prop_work);
ee5e5e7a 19642 }
886c6b86
MN
19643 }
19644 drm_connector_list_iter_end(&conn_iter);
19645}
19646
93a0ed6c 19647/* part #1: call before irq uninstall */
9980c3c1 19648void intel_modeset_driver_remove(struct drm_i915_private *i915)
79e53945 19649{
9980c3c1
JN
19650 flush_workqueue(i915->flip_wq);
19651 flush_workqueue(i915->modeset_wq);
8bcf9f70 19652
9980c3c1 19653 flush_work(&i915->atomic_helper.free_work);
e57291c2 19654 drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
93a0ed6c 19655}
eb955eee 19656
93a0ed6c
JN
19657/* part #2: call after irq uninstall */
19658void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
19659{
fd0c0642
DV
19660 /*
19661 * Due to the hpd irq storm handling the hotplug work can re-arm the
19662 * poll handlers. Hence disable polling after hpd handling is shut down.
19663 */
9980c3c1 19664 intel_hpd_poll_fini(i915);
fd0c0642 19665
d5746bf2
JRS
19666 /*
19667 * MST topology needs to be suspended so we don't have any calls to
19668 * fbdev after it's finalized. MST will be destroyed later as part of
19669 * drm_mode_config_cleanup()
19670 */
19671 intel_dp_mst_suspend(i915);
19672
4f256d82 19673 /* poll work can call into fbdev, hence clean that up afterwards */
9980c3c1 19674 intel_fbdev_fini(i915);
4f256d82 19675
723bfd70
JB
19676 intel_unregister_dsm_handler();
19677
9980c3c1 19678 intel_fbc_global_disable(i915);
69341a5e 19679
1630fe75
CW
19680 /* flush any delayed tasks or pending work */
19681 flush_scheduled_work();
19682
9980c3c1 19683 intel_hdcp_component_fini(i915);
9055aac7 19684
0ef1905e 19685 intel_mode_config_cleanup(i915);
4d7bb011 19686
9980c3c1 19687 intel_overlay_cleanup(i915);
ae48434c 19688
9980c3c1 19689 intel_gmbus_teardown(i915);
757fffcf 19690
9980c3c1
JN
19691 destroy_workqueue(i915->flip_wq);
19692 destroy_workqueue(i915->modeset_wq);
acde44b5 19693
9980c3c1 19694 intel_fbc_cleanup_cfb(i915);
79e53945
JB
19695}
19696
eb4612d8
JN
19697/* part #3: call after gem init */
19698void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
19699{
19700 intel_csr_ucode_fini(i915);
19701
19702 intel_power_domains_driver_remove(i915);
19703
19704 intel_vga_unregister(i915);
19705
19706 intel_bios_driver_remove(i915);
19707}
19708
98a2f411
CW
19709#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
19710
c4a1d9e4 19711struct intel_display_error_state {
ff57f1b0
PZ
19712
19713 u32 power_well_driver;
19714
c4a1d9e4
CW
19715 struct intel_cursor_error_state {
19716 u32 control;
19717 u32 position;
19718 u32 base;
19719 u32 size;
52331309 19720 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
19721
19722 struct intel_pipe_error_state {
ddf9c536 19723 bool power_domain_on;
c4a1d9e4 19724 u32 source;
f301b1e1 19725 u32 stat;
52331309 19726 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
19727
19728 struct intel_plane_error_state {
19729 u32 control;
19730 u32 stride;
19731 u32 size;
19732 u32 pos;
19733 u32 addr;
19734 u32 surface;
19735 u32 tile_offset;
52331309 19736 } plane[I915_MAX_PIPES];
63b66e5b
CW
19737
19738 struct intel_transcoder_error_state {
062de72b 19739 bool available;
ddf9c536 19740 bool power_domain_on;
63b66e5b
CW
19741 enum transcoder cpu_transcoder;
19742
19743 u32 conf;
19744
19745 u32 htotal;
19746 u32 hblank;
19747 u32 hsync;
19748 u32 vtotal;
19749 u32 vblank;
19750 u32 vsync;
f1f1d4fa 19751 } transcoder[5];
c4a1d9e4
CW
19752};
19753
19754struct intel_display_error_state *
c033666a 19755intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 19756{
c4a1d9e4 19757 struct intel_display_error_state *error;
63b66e5b
CW
19758 int transcoders[] = {
19759 TRANSCODER_A,
19760 TRANSCODER_B,
19761 TRANSCODER_C,
f1f1d4fa 19762 TRANSCODER_D,
63b66e5b
CW
19763 TRANSCODER_EDP,
19764 };
c4a1d9e4
CW
19765 int i;
19766
062de72b
LDM
19767 BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
19768
da27bd41 19769 if (!HAS_DISPLAY(dev_priv))
63b66e5b
CW
19770 return NULL;
19771
9d1cb914 19772 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
19773 if (error == NULL)
19774 return NULL;
19775
c033666a 19776 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
dc008bf0
JN
19777 error->power_well_driver = intel_de_read(dev_priv,
19778 HSW_PWR_WELL_CTL2);
ff57f1b0 19779
055e393f 19780 for_each_pipe(dev_priv, i) {
ddf9c536 19781 error->pipe[i].power_domain_on =
f458ebbc
DV
19782 __intel_display_power_is_enabled(dev_priv,
19783 POWER_DOMAIN_PIPE(i));
ddf9c536 19784 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
19785 continue;
19786
dc008bf0
JN
19787 error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
19788 error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
19789 error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
c4a1d9e4 19790
dc008bf0
JN
19791 error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
19792 error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
c033666a 19793 if (INTEL_GEN(dev_priv) <= 3) {
dc008bf0
JN
19794 error->plane[i].size = intel_de_read(dev_priv,
19795 DSPSIZE(i));
19796 error->plane[i].pos = intel_de_read(dev_priv,
19797 DSPPOS(i));
80ca378b 19798 }
c033666a 19799 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
dc008bf0
JN
19800 error->plane[i].addr = intel_de_read(dev_priv,
19801 DSPADDR(i));
c033666a 19802 if (INTEL_GEN(dev_priv) >= 4) {
dc008bf0
JN
19803 error->plane[i].surface = intel_de_read(dev_priv,
19804 DSPSURF(i));
19805 error->plane[i].tile_offset = intel_de_read(dev_priv,
19806 DSPTILEOFF(i));
c4a1d9e4
CW
19807 }
19808
dc008bf0 19809 error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
f301b1e1 19810
b2ae318a 19811 if (HAS_GMCH(dev_priv))
dc008bf0
JN
19812 error->pipe[i].stat = intel_de_read(dev_priv,
19813 PIPESTAT(i));
63b66e5b
CW
19814 }
19815
062de72b 19816 for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
63b66e5b
CW
19817 enum transcoder cpu_transcoder = transcoders[i];
19818
10cf8e75 19819 if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
062de72b
LDM
19820 continue;
19821
19822 error->transcoder[i].available = true;
ddf9c536 19823 error->transcoder[i].power_domain_on =
f458ebbc 19824 __intel_display_power_is_enabled(dev_priv,
38cc1daf 19825 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 19826 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
19827 continue;
19828
63b66e5b
CW
19829 error->transcoder[i].cpu_transcoder = cpu_transcoder;
19830
dc008bf0
JN
19831 error->transcoder[i].conf = intel_de_read(dev_priv,
19832 PIPECONF(cpu_transcoder));
19833 error->transcoder[i].htotal = intel_de_read(dev_priv,
19834 HTOTAL(cpu_transcoder));
19835 error->transcoder[i].hblank = intel_de_read(dev_priv,
19836 HBLANK(cpu_transcoder));
19837 error->transcoder[i].hsync = intel_de_read(dev_priv,
19838 HSYNC(cpu_transcoder));
19839 error->transcoder[i].vtotal = intel_de_read(dev_priv,
19840 VTOTAL(cpu_transcoder));
19841 error->transcoder[i].vblank = intel_de_read(dev_priv,
19842 VBLANK(cpu_transcoder));
19843 error->transcoder[i].vsync = intel_de_read(dev_priv,
19844 VSYNC(cpu_transcoder));
c4a1d9e4
CW
19845 }
19846
19847 return error;
19848}
19849
edc3d884
MK
19850#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
19851
c4a1d9e4 19852void
edc3d884 19853intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
19854 struct intel_display_error_state *error)
19855{
5a4c6f1b 19856 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
19857 int i;
19858
63b66e5b
CW
19859 if (!error)
19860 return;
19861
24977870 19862 err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
8652744b 19863 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 19864 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 19865 error->power_well_driver);
055e393f 19866 for_each_pipe(dev_priv, i) {
edc3d884 19867 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 19868 err_printf(m, " Power: %s\n",
87ad3212 19869 onoff(error->pipe[i].power_domain_on));
edc3d884 19870 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 19871 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
19872
19873 err_printf(m, "Plane [%d]:\n", i);
19874 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
19875 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 19876 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
19877 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
19878 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 19879 }
772c2a51 19880 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 19881 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 19882 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
19883 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
19884 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
19885 }
19886
edc3d884
MK
19887 err_printf(m, "Cursor [%d]:\n", i);
19888 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
19889 err_printf(m, " POS: %08x\n", error->cursor[i].position);
19890 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 19891 }
63b66e5b 19892
062de72b
LDM
19893 for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19894 if (!error->transcoder[i].available)
19895 continue;
19896
da205630 19897 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 19898 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 19899 err_printf(m, " Power: %s\n",
87ad3212 19900 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
19901 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
19902 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
19903 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
19904 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
19905 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
19906 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
19907 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
19908 }
c4a1d9e4 19909}
98a2f411
CW
19910
19911#endif