]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/i915/display/intel_display.c
drm/i915: Get the uapi state from the correct plane when bigjoiner is used
[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
f90a85e7 4318 ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
2225f3c6 4319 &crtc_state->uapi,
4e0b83a5
VS
4320 DRM_PLANE_HELPER_NO_SCALING,
4321 DRM_PLANE_HELPER_NO_SCALING,
26443a4b
VS
4322 i9xx_plane_has_windowing(plane),
4323 true);
4e0b83a5
VS
4324 if (ret)
4325 return ret;
4326
54d4d719
VS
4327 ret = i9xx_check_plane_surface(plane_state);
4328 if (ret)
4329 return ret;
4330
f90a85e7 4331 if (!plane_state->uapi.visible)
4e0b83a5
VS
4332 return 0;
4333
4334 ret = intel_plane_check_src_coordinates(plane_state);
4335 if (ret)
4336 return ret;
4337
4e0b83a5
VS
4338 plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4339
4340 return 0;
4341}
4342
ed15030d
VS
4343static void i9xx_update_plane(struct intel_plane *plane,
4344 const struct intel_crtc_state *crtc_state,
4345 const struct intel_plane_state *plane_state)
7145f60a 4346{
ed15030d 4347 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
ed15030d 4348 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
7145f60a 4349 u32 linear_offset;
c11ada07
VS
4350 int x = plane_state->color_plane[0].x;
4351 int y = plane_state->color_plane[0].y;
f90a85e7
ML
4352 int crtc_x = plane_state->uapi.dst.x1;
4353 int crtc_y = plane_state->uapi.dst.y1;
4354 int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4355 int crtc_h = drm_rect_height(&plane_state->uapi.dst);
7145f60a 4356 unsigned long irqflags;
e288881b 4357 u32 dspaddr_offset;
7eb31a0b
VS
4358 u32 dspcntr;
4359
4360 dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
7145f60a 4361
2949056c 4362 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 4363
5b7fcc44 4364 if (INTEL_GEN(dev_priv) >= 4)
c11ada07 4365 dspaddr_offset = plane_state->color_plane[0].offset;
5b7fcc44 4366 else
e288881b 4367 dspaddr_offset = linear_offset;
6687c906 4368
dd584fc0
VS
4369 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4370
dc008bf0
JN
4371 intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4372 plane_state->color_plane[0].stride);
83234d13 4373
78587de2 4374 if (INTEL_GEN(dev_priv) < 4) {
26443a4b
VS
4375 /*
4376 * PLANE_A doesn't actually have a full window
4377 * generator but let's assume we still need to
4378 * program whatever is there.
78587de2 4379 */
dc008bf0
JN
4380 intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4381 (crtc_y << 16) | crtc_x);
4382 intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4383 ((crtc_h - 1) << 16) | (crtc_w - 1));
ed15030d 4384 } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
dc008bf0
JN
4385 intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4386 (crtc_y << 16) | crtc_x);
4387 intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4388 ((crtc_h - 1) << 16) | (crtc_w - 1));
4389 intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
78587de2
VS
4390 }
4391
3ba35e53 4392 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
dc008bf0
JN
4393 intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4394 (y << 16) | x);
3ba35e53 4395 } else if (INTEL_GEN(dev_priv) >= 4) {
dc008bf0
JN
4396 intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4397 linear_offset);
4398 intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4399 (y << 16) | x);
83234d13
VS
4400 }
4401
4402 /*
4403 * The control register self-arms if the plane was previously
4404 * disabled. Try to make the plane enable atomic by writing
4405 * the control register just before the surface register.
4406 */
dc008bf0 4407 intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
83234d13 4408 if (INTEL_GEN(dev_priv) >= 4)
dc008bf0
JN
4409 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4410 intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
83234d13 4411 else
dc008bf0
JN
4412 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4413 intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
dd584fc0
VS
4414
4415 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
17638cd6
JB
4416}
4417
ed15030d 4418static void i9xx_disable_plane(struct intel_plane *plane,
0dd14be3 4419 const struct intel_crtc_state *crtc_state)
17638cd6 4420{
ed15030d
VS
4421 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4422 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
dd584fc0 4423 unsigned long irqflags;
7eb31a0b
VS
4424 u32 dspcntr;
4425
4426 /*
4427 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4428 * enable on ilk+ affect the pipe bottom color as
4429 * well, so we must configure them even if the plane
4430 * is disabled.
4431 *
4432 * On pre-g4x there is no way to gamma correct the
4433 * pipe bottom color but we'll keep on doing this
9d5441de 4434 * anyway so that the crtc state readout works correctly.
7eb31a0b
VS
4435 */
4436 dspcntr = i9xx_plane_ctl_crtc(crtc_state);
dd584fc0
VS
4437
4438 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f45651ba 4439
dc008bf0 4440 intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
ed15030d 4441 if (INTEL_GEN(dev_priv) >= 4)
dc008bf0 4442 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
a8d201af 4443 else
dc008bf0 4444 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
dd584fc0
VS
4445
4446 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
a8d201af 4447}
c9ba6fad 4448
eade6c89
VS
4449static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4450 enum pipe *pipe)
51f5a096 4451{
ed15030d 4452 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
51f5a096 4453 enum intel_display_power_domain power_domain;
ed15030d 4454 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
0e6e0be4 4455 intel_wakeref_t wakeref;
51f5a096 4456 bool ret;
eade6c89 4457 u32 val;
51f5a096
VS
4458
4459 /*
4460 * Not 100% correct for planes that can move between pipes,
4461 * but that's only the case for gen2-4 which don't have any
4462 * display power wells.
4463 */
eade6c89 4464 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
0e6e0be4
CW
4465 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4466 if (!wakeref)
51f5a096
VS
4467 return false;
4468
dc008bf0 4469 val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
eade6c89
VS
4470
4471 ret = val & DISPLAY_PLANE_ENABLE;
4472
4473 if (INTEL_GEN(dev_priv) >= 5)
4474 *pipe = plane->pipe;
4475 else
4476 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4477 DISPPLANE_SEL_PIPE_SHIFT;
51f5a096 4478
0e6e0be4 4479 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
4480
4481 return ret;
4482}
4483
e435d6e5
ML
4484static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4485{
4486 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4487 struct drm_i915_private *dev_priv = to_i915(dev);
f986ef2e
VS
4488 unsigned long irqflags;
4489
4490 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4491
4492 intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4493 intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4494 intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5 4495
f986ef2e 4496 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
e435d6e5
ML
4497}
4498
a1b2278e
CK
4499/*
4500 * This function detaches (aka. unbinds) unused scalers in hardware
4501 */
15cbe5d0 4502static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
a1b2278e 4503{
2225f3c6 4504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
15cbe5d0
ML
4505 const struct intel_crtc_scaler_state *scaler_state =
4506 &crtc_state->scaler_state;
a1b2278e
CK
4507 int i;
4508
a1b2278e
CK
4509 /* loop through and disable scalers that aren't in use */
4510 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
4511 if (!scaler_state->scalers[i].in_use)
4512 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
4513 }
4514}
4515
b3cf5c06
VS
4516static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4517 int color_plane, unsigned int rotation)
4518{
4519 /*
4520 * The stride is either expressed as a multiple of 64 bytes chunks for
4521 * linear buffers or in number of tiles for tiled buffers.
4522 */
b3e57bcc 4523 if (is_surface_linear(fb, color_plane))
b3cf5c06
VS
4524 return 64;
4525 else if (drm_rotation_90_or_270(rotation))
4526 return intel_tile_height(fb, color_plane);
4527 else
4528 return intel_tile_width_bytes(fb, color_plane);
4529}
4530
df79cf44 4531u32 skl_plane_stride(const struct intel_plane_state *plane_state,
5d2a1950 4532 int color_plane)
d2196774 4533{
7b3cb17a
ML
4534 const struct drm_framebuffer *fb = plane_state->hw.fb;
4535 unsigned int rotation = plane_state->hw.rotation;
5d2a1950 4536 u32 stride = plane_state->color_plane[color_plane].stride;
1b500535 4537
5d2a1950 4538 if (color_plane >= fb->format->num_planes)
1b500535
VS
4539 return 0;
4540
b3cf5c06 4541 return stride / skl_plane_stride_mult(fb, color_plane, rotation);
d2196774
VS
4542}
4543
ba3f4d0a 4544static u32 skl_plane_ctl_format(u32 pixel_format)
70d21f0e 4545{
6156a456 4546 switch (pixel_format) {
d161cf7a 4547 case DRM_FORMAT_C8:
c34ce3d1 4548 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 4549 case DRM_FORMAT_RGB565:
c34ce3d1 4550 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 4551 case DRM_FORMAT_XBGR8888:
4036c78c 4552 case DRM_FORMAT_ABGR8888:
c34ce3d1 4553 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 4554 case DRM_FORMAT_XRGB8888:
6156a456 4555 case DRM_FORMAT_ARGB8888:
4036c78c 4556 return PLANE_CTL_FORMAT_XRGB_8888;
94e35ce2 4557 case DRM_FORMAT_XBGR2101010:
f9c43a31 4558 case DRM_FORMAT_ABGR2101010:
94e35ce2 4559 return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
70d21f0e 4560 case DRM_FORMAT_XRGB2101010:
f9c43a31 4561 case DRM_FORMAT_ARGB2101010:
c34ce3d1 4562 return PLANE_CTL_FORMAT_XRGB_2101010;
a94bed60
KS
4563 case DRM_FORMAT_XBGR16161616F:
4564 case DRM_FORMAT_ABGR16161616F:
4565 return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4566 case DRM_FORMAT_XRGB16161616F:
4567 case DRM_FORMAT_ARGB16161616F:
4568 return PLANE_CTL_FORMAT_XRGB_16161616F;
da904174
SL
4569 case DRM_FORMAT_XYUV8888:
4570 return PLANE_CTL_FORMAT_XYUV;
6156a456 4571 case DRM_FORMAT_YUYV:
c34ce3d1 4572 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 4573 case DRM_FORMAT_YVYU:
c34ce3d1 4574 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 4575 case DRM_FORMAT_UYVY:
c34ce3d1 4576 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 4577 case DRM_FORMAT_VYUY:
c34ce3d1 4578 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
77224cd5
CK
4579 case DRM_FORMAT_NV12:
4580 return PLANE_CTL_FORMAT_NV12;
df7d4156
JPH
4581 case DRM_FORMAT_P010:
4582 return PLANE_CTL_FORMAT_P010;
4583 case DRM_FORMAT_P012:
4584 return PLANE_CTL_FORMAT_P012;
4585 case DRM_FORMAT_P016:
4586 return PLANE_CTL_FORMAT_P016;
296e9b19
SS
4587 case DRM_FORMAT_Y210:
4588 return PLANE_CTL_FORMAT_Y210;
4589 case DRM_FORMAT_Y212:
4590 return PLANE_CTL_FORMAT_Y212;
4591 case DRM_FORMAT_Y216:
4592 return PLANE_CTL_FORMAT_Y216;
ff01e697 4593 case DRM_FORMAT_XVYU2101010:
296e9b19 4594 return PLANE_CTL_FORMAT_Y410;
ff01e697 4595 case DRM_FORMAT_XVYU12_16161616:
296e9b19 4596 return PLANE_CTL_FORMAT_Y412;
ff01e697 4597 case DRM_FORMAT_XVYU16161616:
296e9b19 4598 return PLANE_CTL_FORMAT_Y416;
70d21f0e 4599 default:
4249eeef 4600 MISSING_CASE(pixel_format);
70d21f0e 4601 }
8cfcba41 4602
c34ce3d1 4603 return 0;
6156a456 4604}
70d21f0e 4605
b2081525 4606static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4036c78c 4607{
7b3cb17a 4608 if (!plane_state->hw.fb->format->has_alpha)
b2081525
ML
4609 return PLANE_CTL_ALPHA_DISABLE;
4610
7b3cb17a 4611 switch (plane_state->hw.pixel_blend_mode) {
b2081525
ML
4612 case DRM_MODE_BLEND_PIXEL_NONE:
4613 return PLANE_CTL_ALPHA_DISABLE;
4614 case DRM_MODE_BLEND_PREMULTI:
4036c78c 4615 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
b2081525
ML
4616 case DRM_MODE_BLEND_COVERAGE:
4617 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4036c78c 4618 default:
7b3cb17a 4619 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4036c78c
JA
4620 return PLANE_CTL_ALPHA_DISABLE;
4621 }
4622}
4623
b2081525 4624static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4036c78c 4625{
7b3cb17a 4626 if (!plane_state->hw.fb->format->has_alpha)
b2081525
ML
4627 return PLANE_COLOR_ALPHA_DISABLE;
4628
7b3cb17a 4629 switch (plane_state->hw.pixel_blend_mode) {
b2081525
ML
4630 case DRM_MODE_BLEND_PIXEL_NONE:
4631 return PLANE_COLOR_ALPHA_DISABLE;
4632 case DRM_MODE_BLEND_PREMULTI:
4036c78c 4633 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
b2081525
ML
4634 case DRM_MODE_BLEND_COVERAGE:
4635 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4036c78c 4636 default:
7b3cb17a 4637 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4036c78c
JA
4638 return PLANE_COLOR_ALPHA_DISABLE;
4639 }
4640}
4641
ba3f4d0a 4642static u32 skl_plane_ctl_tiling(u64 fb_modifier)
6156a456 4643{
6156a456 4644 switch (fb_modifier) {
2f075565 4645 case DRM_FORMAT_MOD_LINEAR:
70d21f0e 4646 break;
30af77c4 4647 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 4648 return PLANE_CTL_TILED_X;
b321803d 4649 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 4650 return PLANE_CTL_TILED_Y;
2e2adb05 4651 case I915_FORMAT_MOD_Y_TILED_CCS:
53867b46 4652 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
b3e57bcc
DP
4653 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4654 return PLANE_CTL_TILED_Y |
4655 PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4656 PLANE_CTL_CLEAR_COLOR_DISABLE;
2dfbf9d2
DP
4657 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4658 return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
b321803d 4659 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 4660 return PLANE_CTL_TILED_YF;
2e2adb05 4661 case I915_FORMAT_MOD_Yf_TILED_CCS:
53867b46 4662 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
70d21f0e 4663 default:
6156a456 4664 MISSING_CASE(fb_modifier);
70d21f0e 4665 }
8cfcba41 4666
c34ce3d1 4667 return 0;
6156a456 4668}
70d21f0e 4669
5f8e3f57 4670static u32 skl_plane_ctl_rotate(unsigned int rotate)
6156a456 4671{
5f8e3f57 4672 switch (rotate) {
c2c446ad 4673 case DRM_MODE_ROTATE_0:
6156a456 4674 break;
1e8df167 4675 /*
c2c446ad 4676 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
1e8df167
SJ
4677 * while i915 HW rotation is clockwise, thats why this swapping.
4678 */
c2c446ad 4679 case DRM_MODE_ROTATE_90:
1e8df167 4680 return PLANE_CTL_ROTATE_270;
c2c446ad 4681 case DRM_MODE_ROTATE_180:
c34ce3d1 4682 return PLANE_CTL_ROTATE_180;
c2c446ad 4683 case DRM_MODE_ROTATE_270:
1e8df167 4684 return PLANE_CTL_ROTATE_90;
6156a456 4685 default:
5f8e3f57
JL
4686 MISSING_CASE(rotate);
4687 }
4688
4689 return 0;
4690}
4691
4692static u32 cnl_plane_ctl_flip(unsigned int reflect)
4693{
4694 switch (reflect) {
4695 case 0:
4696 break;
4697 case DRM_MODE_REFLECT_X:
4698 return PLANE_CTL_FLIP_HORIZONTAL;
4699 case DRM_MODE_REFLECT_Y:
4700 default:
4701 MISSING_CASE(reflect);
6156a456
CK
4702 }
4703
c34ce3d1 4704 return 0;
6156a456
CK
4705}
4706
7eb31a0b
VS
4707u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4708{
2225f3c6 4709 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7eb31a0b
VS
4710 u32 plane_ctl = 0;
4711
c5e07e00
K
4712 if (crtc_state->uapi.async_flip)
4713 plane_ctl |= PLANE_CTL_ASYNC_FLIP;
4714
7eb31a0b
VS
4715 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4716 return plane_ctl;
4717
5f29ab23
VS
4718 if (crtc_state->gamma_enable)
4719 plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4720
8271b2ef
VS
4721 if (crtc_state->csc_enable)
4722 plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
7eb31a0b
VS
4723
4724 return plane_ctl;
4725}
4726
2e881264
VS
4727u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4728 const struct intel_plane_state *plane_state)
46f788ba
VS
4729{
4730 struct drm_i915_private *dev_priv =
f90a85e7 4731 to_i915(plane_state->uapi.plane->dev);
7b3cb17a
ML
4732 const struct drm_framebuffer *fb = plane_state->hw.fb;
4733 unsigned int rotation = plane_state->hw.rotation;
2e881264 4734 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
46f788ba
VS
4735 u32 plane_ctl;
4736
4737 plane_ctl = PLANE_CTL_ENABLE;
4738
4036c78c 4739 if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
b2081525 4740 plane_ctl |= skl_plane_ctl_alpha(plane_state);
7eb31a0b 4741 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
b0f5c0ba 4742
7b3cb17a 4743 if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
b0f5c0ba 4744 plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
c8624ede 4745
7b3cb17a 4746 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
c8624ede 4747 plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
46f788ba
VS
4748 }
4749
4750 plane_ctl |= skl_plane_ctl_format(fb->format->format);
4751 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
5f8e3f57
JL
4752 plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4753
4754 if (INTEL_GEN(dev_priv) >= 10)
4755 plane_ctl |= cnl_plane_ctl_flip(rotation &
4756 DRM_MODE_REFLECT_MASK);
46f788ba 4757
2e881264
VS
4758 if (key->flags & I915_SET_COLORKEY_DESTINATION)
4759 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4760 else if (key->flags & I915_SET_COLORKEY_SOURCE)
4761 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4762
46f788ba
VS
4763 return plane_ctl;
4764}
4765
7eb31a0b
VS
4766u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4767{
2225f3c6 4768 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7eb31a0b
VS
4769 u32 plane_color_ctl = 0;
4770
4771 if (INTEL_GEN(dev_priv) >= 11)
4772 return plane_color_ctl;
4773
5f29ab23
VS
4774 if (crtc_state->gamma_enable)
4775 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4776
8271b2ef
VS
4777 if (crtc_state->csc_enable)
4778 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
7eb31a0b
VS
4779
4780 return plane_color_ctl;
4781}
4782
4036c78c
JA
4783u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4784 const struct intel_plane_state *plane_state)
4785{
42fd20ed 4786 struct drm_i915_private *dev_priv =
f90a85e7 4787 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 4788 const struct drm_framebuffer *fb = plane_state->hw.fb;
f90a85e7 4789 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4036c78c
JA
4790 u32 plane_color_ctl = 0;
4791
4036c78c 4792 plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
b2081525 4793 plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4036c78c 4794
42fd20ed 4795 if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
a0196dd6
KK
4796 switch (plane_state->hw.color_encoding) {
4797 case DRM_COLOR_YCBCR_BT709:
b0f5c0ba 4798 plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
a0196dd6
KK
4799 break;
4800 case DRM_COLOR_YCBCR_BT2020:
4801 plane_color_ctl |=
4802 PLANE_COLOR_CSC_MODE_YUV2020_TO_RGB2020;
4803 break;
4804 default:
4805 plane_color_ctl |=
4806 PLANE_COLOR_CSC_MODE_YUV601_TO_RGB601;
4807 }
7b3cb17a 4808 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
c8624ede 4809 plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
bfe60a02
US
4810 } else if (fb->format->is_yuv) {
4811 plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
b0f5c0ba 4812 }
012d79e6 4813
4036c78c
JA
4814 return plane_color_ctl;
4815}
4816
73974893
ML
4817static int
4818__intel_display_resume(struct drm_device *dev,
581e49fe
ML
4819 struct drm_atomic_state *state,
4820 struct drm_modeset_acquire_ctx *ctx)
73974893
ML
4821{
4822 struct drm_crtc_state *crtc_state;
4823 struct drm_crtc *crtc;
4824 int i, ret;
11c22da6 4825
aecd36b8 4826 intel_modeset_setup_hw_state(dev, ctx);
4fb87831 4827 intel_vga_redisable(to_i915(dev));
73974893
ML
4828
4829 if (!state)
4830 return 0;
4831
aa5e9b47
ML
4832 /*
4833 * We've duplicated the state, pointers to the old state are invalid.
4834 *
4835 * Don't attempt to use the old state until we commit the duplicated state.
4836 */
4837 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
73974893
ML
4838 /*
4839 * Force recalculation even if we restore
4840 * current state. With fast modeset this may not result
4841 * in a modeset when the state is compatible.
4842 */
4843 crtc_state->mode_changed = true;
96a02917 4844 }
73974893
ML
4845
4846 /* ignore any reset values/BIOS leftovers in the WM registers */
b2ae318a 4847 if (!HAS_GMCH(to_i915(dev)))
602ae835 4848 to_intel_atomic_state(state)->skip_intermediate_wm = true;
73974893 4849
581e49fe 4850 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
73974893 4851
e57291c2 4852 drm_WARN_ON(dev, ret == -EDEADLK);
73974893 4853 return ret;
96a02917
VS
4854}
4855
4ac2ba2f
VS
4856static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4857{
55277e1f 4858 return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
260e6b71 4859 intel_has_gpu_reset(&dev_priv->gt));
4ac2ba2f
VS
4860}
4861
87ebfaab 4862void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 4863{
73974893
ML
4864 struct drm_device *dev = &dev_priv->drm;
4865 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4866 struct drm_atomic_state *state;
4867 int ret;
4868
2c568805
JRS
4869 if (!HAS_DISPLAY(dev_priv))
4870 return;
4871
ce87ea15 4872 /* reset doesn't touch the display */
8a25c4be 4873 if (!dev_priv->params.force_reset_modeset_test &&
ce87ea15
DV
4874 !gpu_reset_clobbers_display(dev_priv))
4875 return;
4876
9db529aa 4877 /* We have a modeset vs reset deadlock, defensively unbreak it. */
cb823ed9
CW
4878 set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4879 smp_mb__after_atomic();
4880 wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
9db529aa
DV
4881
4882 if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
cd49f818
WK
4883 drm_dbg_kms(&dev_priv->drm,
4884 "Modeset potentially stuck, unbreaking through wedging\n");
cb823ed9 4885 intel_gt_set_wedged(&dev_priv->gt);
9db529aa 4886 }
97154ec2 4887
73974893
ML
4888 /*
4889 * Need mode_config.mutex so that we don't
4890 * trample ongoing ->detect() and whatnot.
4891 */
4892 mutex_lock(&dev->mode_config.mutex);
4893 drm_modeset_acquire_init(ctx, 0);
4894 while (1) {
4895 ret = drm_modeset_lock_all_ctx(dev, ctx);
4896 if (ret != -EDEADLK)
4897 break;
4898
4899 drm_modeset_backoff(ctx);
4900 }
f98ce92f
VS
4901 /*
4902 * Disabling the crtcs gracefully seems nicer. Also the
4903 * g33 docs say we should at least disable all the planes.
4904 */
73974893
ML
4905 state = drm_atomic_helper_duplicate_state(dev, ctx);
4906 if (IS_ERR(state)) {
4907 ret = PTR_ERR(state);
cd49f818
WK
4908 drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4909 ret);
1e5a15d6 4910 return;
73974893
ML
4911 }
4912
4913 ret = drm_atomic_helper_disable_all(dev, ctx);
4914 if (ret) {
cd49f818
WK
4915 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4916 ret);
1e5a15d6
ACO
4917 drm_atomic_state_put(state);
4918 return;
73974893
ML
4919 }
4920
4921 dev_priv->modeset_restore_state = state;
4922 state->acquire_ctx = ctx;
7514747d
VS
4923}
4924
e669ad6f 4925void intel_display_finish_reset(struct drm_i915_private *dev_priv)
7514747d 4926{
73974893
ML
4927 struct drm_device *dev = &dev_priv->drm;
4928 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
40da1d31 4929 struct drm_atomic_state *state;
73974893
ML
4930 int ret;
4931
2c568805
JRS
4932 if (!HAS_DISPLAY(dev_priv))
4933 return;
4934
ce87ea15 4935 /* reset doesn't touch the display */
cb823ed9 4936 if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
ce87ea15
DV
4937 return;
4938
40da1d31 4939 state = fetch_and_zero(&dev_priv->modeset_restore_state);
ce87ea15
DV
4940 if (!state)
4941 goto unlock;
4942
7514747d 4943 /* reset doesn't touch the display */
4ac2ba2f 4944 if (!gpu_reset_clobbers_display(dev_priv)) {
ce87ea15
DV
4945 /* for testing only restore the display */
4946 ret = __intel_display_resume(dev, state, ctx);
942d5d0d 4947 if (ret)
cd49f818
WK
4948 drm_err(&dev_priv->drm,
4949 "Restoring old state failed with %i\n", ret);
73974893
ML
4950 } else {
4951 /*
4952 * The display has been reset as well,
4953 * so need a full re-initialization.
4954 */
51f59205 4955 intel_pps_unlock_regs_wa(dev_priv);
6cd02e77 4956 intel_modeset_init_hw(dev_priv);
f72b84c6 4957 intel_init_clock_gating(dev_priv);
4c8d4651 4958 intel_hpd_init(dev_priv);
7514747d 4959
581e49fe 4960 ret = __intel_display_resume(dev, state, ctx);
73974893 4961 if (ret)
cd49f818
WK
4962 drm_err(&dev_priv->drm,
4963 "Restoring old state failed with %i\n", ret);
7514747d 4964
4c8d4651 4965 intel_hpd_poll_disable(dev_priv);
73974893 4966 }
7514747d 4967
ce87ea15
DV
4968 drm_atomic_state_put(state);
4969unlock:
73974893
ML
4970 drm_modeset_drop_locks(ctx);
4971 drm_modeset_acquire_fini(ctx);
4972 mutex_unlock(&dev->mode_config.mutex);
9db529aa 4973
cb823ed9 4974 clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
7514747d
VS
4975}
4976
d1622119
VS
4977static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4978{
4979 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4980 enum pipe pipe = crtc->pipe;
4981 u32 tmp;
4982
dc008bf0 4983 tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
d1622119
VS
4984
4985 /*
4986 * Display WA #1153: icl
4987 * enable hardware to bypass the alpha math
4988 * and rounding for per-pixel values 00 and 0xff
4989 */
4990 tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
26eeea15
AS
4991 /*
4992 * Display WA # 1605353570: icl
4993 * Set the pixel rounding bit to 1 for allowing
4994 * passthrough of Frame buffer pixels unmodified
4995 * across pipe
4996 */
4997 tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
dc008bf0 4998 intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
d1622119
VS
4999}
5000
4cbe4b2b 5001static void intel_fdi_normal_train(struct intel_crtc *crtc)
5e84e1a4 5002{
4cbe4b2b 5003 struct drm_device *dev = crtc->base.dev;
fac5e23e 5004 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5005 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5006 i915_reg_t reg;
5007 u32 temp;
5e84e1a4
ZW
5008
5009 /* enable normal train */
5010 reg = FDI_TX_CTL(pipe);
dc008bf0 5011 temp = intel_de_read(dev_priv, reg);
fd6b8f43 5012 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
5013 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5014 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
5015 } else {
5016 temp &= ~FDI_LINK_TRAIN_NONE;
5017 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 5018 }
dc008bf0 5019 intel_de_write(dev_priv, reg, temp);
5e84e1a4
ZW
5020
5021 reg = FDI_RX_CTL(pipe);
dc008bf0 5022 temp = intel_de_read(dev_priv, reg);
6e266956 5023 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
5024 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5025 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5026 } else {
5027 temp &= ~FDI_LINK_TRAIN_NONE;
5028 temp |= FDI_LINK_TRAIN_NONE;
5029 }
dc008bf0 5030 intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5e84e1a4
ZW
5031
5032 /* wait one idle pattern time */
dc008bf0 5033 intel_de_posting_read(dev_priv, reg);
5e84e1a4 5034 udelay(1000);
357555c0
JB
5035
5036 /* IVB wants error correction enabled */
fd6b8f43 5037 if (IS_IVYBRIDGE(dev_priv))
dc008bf0
JN
5038 intel_de_write(dev_priv, reg,
5039 intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
5040}
5041
8db9d77b 5042/* The FDI link training functions for ILK/Ibexpeak. */
9eae5e27
LDM
5043static void ilk_fdi_link_train(struct intel_crtc *crtc,
5044 const struct intel_crtc_state *crtc_state)
8db9d77b 5045{
4cbe4b2b 5046 struct drm_device *dev = crtc->base.dev;
fac5e23e 5047 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5048 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5049 i915_reg_t reg;
5050 u32 temp, tries;
8db9d77b 5051
1c8562f6 5052 /* FDI needs bits from pipe first */
b104e8b2 5053 assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
0fc932b8 5054
e1a44743
AJ
5055 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5056 for train result */
5eddb70b 5057 reg = FDI_RX_IMR(pipe);
dc008bf0 5058 temp = intel_de_read(dev_priv, reg);
e1a44743
AJ
5059 temp &= ~FDI_RX_SYMBOL_LOCK;
5060 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0
JN
5061 intel_de_write(dev_priv, reg, temp);
5062 intel_de_read(dev_priv, reg);
e1a44743
AJ
5063 udelay(150);
5064
8db9d77b 5065 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b 5066 reg = FDI_TX_CTL(pipe);
dc008bf0 5067 temp = intel_de_read(dev_priv, reg);
627eb5a3 5068 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5069 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
5070 temp &= ~FDI_LINK_TRAIN_NONE;
5071 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5072 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
8db9d77b 5073
5eddb70b 5074 reg = FDI_RX_CTL(pipe);
dc008bf0 5075 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5076 temp &= ~FDI_LINK_TRAIN_NONE;
5077 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5078 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5eddb70b 5079
dc008bf0 5080 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5081 udelay(150);
5082
5b2adf89 5083 /* Ironlake workaround, enable clock pointer after FDI enable*/
dc008bf0
JN
5084 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5085 FDI_RX_PHASE_SYNC_POINTER_OVR);
5086 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5087 FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 5088
5eddb70b 5089 reg = FDI_RX_IIR(pipe);
e1a44743 5090 for (tries = 0; tries < 5; tries++) {
dc008bf0 5091 temp = intel_de_read(dev_priv, reg);
cd49f818 5092 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
8db9d77b
ZW
5093
5094 if ((temp & FDI_RX_BIT_LOCK)) {
cd49f818 5095 drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
dc008bf0 5096 intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
5097 break;
5098 }
8db9d77b 5099 }
e1a44743 5100 if (tries == 5)
cd49f818 5101 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
8db9d77b
ZW
5102
5103 /* Train 2 */
5eddb70b 5104 reg = FDI_TX_CTL(pipe);
dc008bf0 5105 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5106 temp &= ~FDI_LINK_TRAIN_NONE;
5107 temp |= FDI_LINK_TRAIN_PATTERN_2;
dc008bf0 5108 intel_de_write(dev_priv, reg, temp);
8db9d77b 5109
5eddb70b 5110 reg = FDI_RX_CTL(pipe);
dc008bf0 5111 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5112 temp &= ~FDI_LINK_TRAIN_NONE;
5113 temp |= FDI_LINK_TRAIN_PATTERN_2;
dc008bf0 5114 intel_de_write(dev_priv, reg, temp);
8db9d77b 5115
dc008bf0 5116 intel_de_posting_read(dev_priv, reg);
5eddb70b 5117 udelay(150);
8db9d77b 5118
5eddb70b 5119 reg = FDI_RX_IIR(pipe);
e1a44743 5120 for (tries = 0; tries < 5; tries++) {
dc008bf0 5121 temp = intel_de_read(dev_priv, reg);
cd49f818 5122 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
8db9d77b
ZW
5123
5124 if (temp & FDI_RX_SYMBOL_LOCK) {
dc008bf0
JN
5125 intel_de_write(dev_priv, reg,
5126 temp | FDI_RX_SYMBOL_LOCK);
cd49f818 5127 drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
8db9d77b
ZW
5128 break;
5129 }
8db9d77b 5130 }
e1a44743 5131 if (tries == 5)
cd49f818 5132 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
8db9d77b 5133
cd49f818 5134 drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5c5313c8 5135
8db9d77b
ZW
5136}
5137
0206e353 5138static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
5139 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5140 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5141 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5142 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5143};
5144
5145/* The FDI link training functions for SNB/Cougarpoint. */
dc4a1094
ACO
5146static void gen6_fdi_link_train(struct intel_crtc *crtc,
5147 const struct intel_crtc_state *crtc_state)
8db9d77b 5148{
4cbe4b2b 5149 struct drm_device *dev = crtc->base.dev;
fac5e23e 5150 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5151 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5152 i915_reg_t reg;
5153 u32 temp, i, retry;
8db9d77b 5154
e1a44743
AJ
5155 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5156 for train result */
5eddb70b 5157 reg = FDI_RX_IMR(pipe);
dc008bf0 5158 temp = intel_de_read(dev_priv, reg);
e1a44743
AJ
5159 temp &= ~FDI_RX_SYMBOL_LOCK;
5160 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0 5161 intel_de_write(dev_priv, reg, temp);
5eddb70b 5162
dc008bf0 5163 intel_de_posting_read(dev_priv, reg);
e1a44743
AJ
5164 udelay(150);
5165
8db9d77b 5166 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b 5167 reg = FDI_TX_CTL(pipe);
dc008bf0 5168 temp = intel_de_read(dev_priv, reg);
627eb5a3 5169 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5170 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
8db9d77b
ZW
5171 temp &= ~FDI_LINK_TRAIN_NONE;
5172 temp |= FDI_LINK_TRAIN_PATTERN_1;
5173 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5174 /* SNB-B */
5175 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
dc008bf0 5176 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
8db9d77b 5177
dc008bf0
JN
5178 intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5179 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
d74cf324 5180
5eddb70b 5181 reg = FDI_RX_CTL(pipe);
dc008bf0 5182 temp = intel_de_read(dev_priv, reg);
6e266956 5183 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
5184 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5185 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5186 } else {
5187 temp &= ~FDI_LINK_TRAIN_NONE;
5188 temp |= FDI_LINK_TRAIN_PATTERN_1;
5189 }
dc008bf0 5190 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5eddb70b 5191
dc008bf0 5192 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5193 udelay(150);
5194
0206e353 5195 for (i = 0; i < 4; i++) {
5eddb70b 5196 reg = FDI_TX_CTL(pipe);
dc008bf0 5197 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5198 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5199 temp |= snb_b_fdi_train_param[i];
dc008bf0 5200 intel_de_write(dev_priv, reg, temp);
5eddb70b 5201
dc008bf0 5202 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5203 udelay(500);
5204
fa37d39e
SP
5205 for (retry = 0; retry < 5; retry++) {
5206 reg = FDI_RX_IIR(pipe);
dc008bf0 5207 temp = intel_de_read(dev_priv, reg);
cd49f818 5208 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
fa37d39e 5209 if (temp & FDI_RX_BIT_LOCK) {
dc008bf0
JN
5210 intel_de_write(dev_priv, reg,
5211 temp | FDI_RX_BIT_LOCK);
cd49f818
WK
5212 drm_dbg_kms(&dev_priv->drm,
5213 "FDI train 1 done.\n");
fa37d39e
SP
5214 break;
5215 }
5216 udelay(50);
8db9d77b 5217 }
fa37d39e
SP
5218 if (retry < 5)
5219 break;
8db9d77b
ZW
5220 }
5221 if (i == 4)
cd49f818 5222 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
8db9d77b
ZW
5223
5224 /* Train 2 */
5eddb70b 5225 reg = FDI_TX_CTL(pipe);
dc008bf0 5226 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5227 temp &= ~FDI_LINK_TRAIN_NONE;
5228 temp |= FDI_LINK_TRAIN_PATTERN_2;
cf819eff 5229 if (IS_GEN(dev_priv, 6)) {
8db9d77b
ZW
5230 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5231 /* SNB-B */
5232 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5233 }
dc008bf0 5234 intel_de_write(dev_priv, reg, temp);
8db9d77b 5235
5eddb70b 5236 reg = FDI_RX_CTL(pipe);
dc008bf0 5237 temp = intel_de_read(dev_priv, reg);
6e266956 5238 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
5239 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5240 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5241 } else {
5242 temp &= ~FDI_LINK_TRAIN_NONE;
5243 temp |= FDI_LINK_TRAIN_PATTERN_2;
5244 }
dc008bf0 5245 intel_de_write(dev_priv, reg, temp);
5eddb70b 5246
dc008bf0 5247 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5248 udelay(150);
5249
0206e353 5250 for (i = 0; i < 4; i++) {
5eddb70b 5251 reg = FDI_TX_CTL(pipe);
dc008bf0 5252 temp = intel_de_read(dev_priv, reg);
8db9d77b
ZW
5253 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5254 temp |= snb_b_fdi_train_param[i];
dc008bf0 5255 intel_de_write(dev_priv, reg, temp);
5eddb70b 5256
dc008bf0 5257 intel_de_posting_read(dev_priv, reg);
8db9d77b
ZW
5258 udelay(500);
5259
fa37d39e
SP
5260 for (retry = 0; retry < 5; retry++) {
5261 reg = FDI_RX_IIR(pipe);
dc008bf0 5262 temp = intel_de_read(dev_priv, reg);
cd49f818 5263 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
fa37d39e 5264 if (temp & FDI_RX_SYMBOL_LOCK) {
dc008bf0
JN
5265 intel_de_write(dev_priv, reg,
5266 temp | FDI_RX_SYMBOL_LOCK);
cd49f818
WK
5267 drm_dbg_kms(&dev_priv->drm,
5268 "FDI train 2 done.\n");
fa37d39e
SP
5269 break;
5270 }
5271 udelay(50);
8db9d77b 5272 }
fa37d39e
SP
5273 if (retry < 5)
5274 break;
8db9d77b
ZW
5275 }
5276 if (i == 4)
cd49f818 5277 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
8db9d77b 5278
cd49f818 5279 drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
8db9d77b
ZW
5280}
5281
357555c0 5282/* Manual link training for Ivy Bridge A0 parts */
dc4a1094
ACO
5283static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5284 const struct intel_crtc_state *crtc_state)
357555c0 5285{
4cbe4b2b 5286 struct drm_device *dev = crtc->base.dev;
fac5e23e 5287 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5288 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5289 i915_reg_t reg;
5290 u32 temp, i, j;
357555c0
JB
5291
5292 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5293 for train result */
5294 reg = FDI_RX_IMR(pipe);
dc008bf0 5295 temp = intel_de_read(dev_priv, reg);
357555c0
JB
5296 temp &= ~FDI_RX_SYMBOL_LOCK;
5297 temp &= ~FDI_RX_BIT_LOCK;
dc008bf0 5298 intel_de_write(dev_priv, reg, temp);
357555c0 5299
dc008bf0 5300 intel_de_posting_read(dev_priv, reg);
357555c0
JB
5301 udelay(150);
5302
cd49f818 5303 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
dc008bf0 5304 intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
01a415fd 5305
139ccd3f
JB
5306 /* Try each vswing and preemphasis setting twice before moving on */
5307 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5308 /* disable first in case we need to retry */
5309 reg = FDI_TX_CTL(pipe);
dc008bf0 5310 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5311 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5312 temp &= ~FDI_TX_ENABLE;
dc008bf0 5313 intel_de_write(dev_priv, reg, temp);
357555c0 5314
139ccd3f 5315 reg = FDI_RX_CTL(pipe);
dc008bf0 5316 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5317 temp &= ~FDI_LINK_TRAIN_AUTO;
5318 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5319 temp &= ~FDI_RX_ENABLE;
dc008bf0 5320 intel_de_write(dev_priv, reg, temp);
357555c0 5321
139ccd3f 5322 /* enable CPU FDI TX and PCH FDI RX */
357555c0 5323 reg = FDI_TX_CTL(pipe);
dc008bf0 5324 temp = intel_de_read(dev_priv, reg);
139ccd3f 5325 temp &= ~FDI_DP_PORT_WIDTH_MASK;
dc4a1094 5326 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
139ccd3f 5327 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 5328 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
5329 temp |= snb_b_fdi_train_param[j/2];
5330 temp |= FDI_COMPOSITE_SYNC;
dc008bf0 5331 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
357555c0 5332
dc008bf0
JN
5333 intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5334 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 5335
139ccd3f 5336 reg = FDI_RX_CTL(pipe);
dc008bf0 5337 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5338 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5339 temp |= FDI_COMPOSITE_SYNC;
dc008bf0 5340 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
357555c0 5341
dc008bf0 5342 intel_de_posting_read(dev_priv, reg);
139ccd3f 5343 udelay(1); /* should be 0.5us */
357555c0 5344
139ccd3f
JB
5345 for (i = 0; i < 4; i++) {
5346 reg = FDI_RX_IIR(pipe);
dc008bf0 5347 temp = intel_de_read(dev_priv, reg);
cd49f818 5348 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
357555c0 5349
139ccd3f 5350 if (temp & FDI_RX_BIT_LOCK ||
dc008bf0
JN
5351 (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5352 intel_de_write(dev_priv, reg,
5353 temp | FDI_RX_BIT_LOCK);
cd49f818
WK
5354 drm_dbg_kms(&dev_priv->drm,
5355 "FDI train 1 done, level %i.\n",
5356 i);
139ccd3f
JB
5357 break;
5358 }
5359 udelay(1); /* should be 0.5us */
5360 }
5361 if (i == 4) {
cd49f818
WK
5362 drm_dbg_kms(&dev_priv->drm,
5363 "FDI train 1 fail on vswing %d\n", j / 2);
139ccd3f
JB
5364 continue;
5365 }
357555c0 5366
139ccd3f 5367 /* Train 2 */
357555c0 5368 reg = FDI_TX_CTL(pipe);
dc008bf0 5369 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5370 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5371 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
dc008bf0 5372 intel_de_write(dev_priv, reg, temp);
139ccd3f
JB
5373
5374 reg = FDI_RX_CTL(pipe);
dc008bf0 5375 temp = intel_de_read(dev_priv, reg);
139ccd3f
JB
5376 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5377 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
dc008bf0 5378 intel_de_write(dev_priv, reg, temp);
357555c0 5379
dc008bf0 5380 intel_de_posting_read(dev_priv, reg);
139ccd3f 5381 udelay(2); /* should be 1.5us */
357555c0 5382
139ccd3f
JB
5383 for (i = 0; i < 4; i++) {
5384 reg = FDI_RX_IIR(pipe);
dc008bf0 5385 temp = intel_de_read(dev_priv, reg);
cd49f818 5386 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
357555c0 5387
139ccd3f 5388 if (temp & FDI_RX_SYMBOL_LOCK ||
dc008bf0
JN
5389 (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5390 intel_de_write(dev_priv, reg,
5391 temp | FDI_RX_SYMBOL_LOCK);
cd49f818
WK
5392 drm_dbg_kms(&dev_priv->drm,
5393 "FDI train 2 done, level %i.\n",
5394 i);
139ccd3f
JB
5395 goto train_done;
5396 }
5397 udelay(2); /* should be 1.5us */
357555c0 5398 }
139ccd3f 5399 if (i == 4)
cd49f818
WK
5400 drm_dbg_kms(&dev_priv->drm,
5401 "FDI train 2 fail on vswing %d\n", j / 2);
357555c0 5402 }
357555c0 5403
139ccd3f 5404train_done:
cd49f818 5405 drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
357555c0
JB
5406}
5407
9eae5e27 5408static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
2c07245f 5409{
2225f3c6 5410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2354c78 5411 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
d048a268 5412 enum pipe pipe = intel_crtc->pipe;
f0f59a00
VS
5413 i915_reg_t reg;
5414 u32 temp;
c64e311e 5415
c98e9dcf 5416 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b 5417 reg = FDI_RX_CTL(pipe);
dc008bf0 5418 temp = intel_de_read(dev_priv, reg);
627eb5a3 5419 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
b2354c78 5420 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
dc008bf0
JN
5421 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5422 intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5eddb70b 5423
dc008bf0 5424 intel_de_posting_read(dev_priv, reg);
c98e9dcf
JB
5425 udelay(200);
5426
5427 /* Switch from Rawclk to PCDclk */
dc008bf0
JN
5428 temp = intel_de_read(dev_priv, reg);
5429 intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5eddb70b 5430
dc008bf0 5431 intel_de_posting_read(dev_priv, reg);
c98e9dcf
JB
5432 udelay(200);
5433
20749730
PZ
5434 /* Enable CPU FDI TX PLL, always on for Ironlake */
5435 reg = FDI_TX_CTL(pipe);
dc008bf0 5436 temp = intel_de_read(dev_priv, reg);
20749730 5437 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
dc008bf0 5438 intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 5439
dc008bf0 5440 intel_de_posting_read(dev_priv, reg);
20749730 5441 udelay(100);
6be4a607 5442 }
0e23b99d
JB
5443}
5444
9eae5e27 5445static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
88cefb6c
DV
5446{
5447 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 5448 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5449 enum pipe pipe = intel_crtc->pipe;
f0f59a00
VS
5450 i915_reg_t reg;
5451 u32 temp;
88cefb6c
DV
5452
5453 /* Switch from PCDclk to Rawclk */
5454 reg = FDI_RX_CTL(pipe);
dc008bf0
JN
5455 temp = intel_de_read(dev_priv, reg);
5456 intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
88cefb6c
DV
5457
5458 /* Disable CPU FDI TX PLL */
5459 reg = FDI_TX_CTL(pipe);
dc008bf0
JN
5460 temp = intel_de_read(dev_priv, reg);
5461 intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
88cefb6c 5462
dc008bf0 5463 intel_de_posting_read(dev_priv, reg);
88cefb6c
DV
5464 udelay(100);
5465
5466 reg = FDI_RX_CTL(pipe);
dc008bf0
JN
5467 temp = intel_de_read(dev_priv, reg);
5468 intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
88cefb6c
DV
5469
5470 /* Wait for the clocks to turn off. */
dc008bf0 5471 intel_de_posting_read(dev_priv, reg);
88cefb6c
DV
5472 udelay(100);
5473}
5474
9eae5e27 5475static void ilk_fdi_disable(struct intel_crtc *crtc)
0fc932b8 5476{
5b4f4e94
VS
5477 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5478 enum pipe pipe = crtc->pipe;
f0f59a00
VS
5479 i915_reg_t reg;
5480 u32 temp;
0fc932b8
JB
5481
5482 /* disable CPU FDI tx and PCH FDI rx */
5483 reg = FDI_TX_CTL(pipe);
dc008bf0
JN
5484 temp = intel_de_read(dev_priv, reg);
5485 intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5486 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5487
5488 reg = FDI_RX_CTL(pipe);
dc008bf0 5489 temp = intel_de_read(dev_priv, reg);
0fc932b8 5490 temp &= ~(0x7 << 16);
dc008bf0
JN
5491 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5492 intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
0fc932b8 5493
dc008bf0 5494 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5495 udelay(100);
5496
5497 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 5498 if (HAS_PCH_IBX(dev_priv))
dc008bf0
JN
5499 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5500 FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
5501
5502 /* still set train pattern 1 */
5503 reg = FDI_TX_CTL(pipe);
dc008bf0 5504 temp = intel_de_read(dev_priv, reg);
0fc932b8
JB
5505 temp &= ~FDI_LINK_TRAIN_NONE;
5506 temp |= FDI_LINK_TRAIN_PATTERN_1;
dc008bf0 5507 intel_de_write(dev_priv, reg, temp);
0fc932b8
JB
5508
5509 reg = FDI_RX_CTL(pipe);
dc008bf0 5510 temp = intel_de_read(dev_priv, reg);
6e266956 5511 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
5512 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5513 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5514 } else {
5515 temp &= ~FDI_LINK_TRAIN_NONE;
5516 temp |= FDI_LINK_TRAIN_PATTERN_1;
5517 }
5518 /* BPC in FDI rx is consistent with that in PIPECONF */
5519 temp &= ~(0x07 << 16);
dc008bf0
JN
5520 temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5521 intel_de_write(dev_priv, reg, temp);
0fc932b8 5522
dc008bf0 5523 intel_de_posting_read(dev_priv, reg);
0fc932b8
JB
5524 udelay(100);
5525}
5526
49d73912 5527bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93 5528{
fa05887a
DV
5529 struct drm_crtc *crtc;
5530 bool cleanup_done;
5531
5532 drm_for_each_crtc(crtc, &dev_priv->drm) {
5533 struct drm_crtc_commit *commit;
5534 spin_lock(&crtc->commit_lock);
5535 commit = list_first_entry_or_null(&crtc->commit_list,
5536 struct drm_crtc_commit, commit_entry);
5537 cleanup_done = commit ?
5538 try_wait_for_completion(&commit->cleanup_done) : true;
5539 spin_unlock(&crtc->commit_lock);
5540
5541 if (cleanup_done)
5dce5b93
CW
5542 continue;
5543
fa05887a 5544 drm_crtc_wait_one_vblank(crtc);
5dce5b93
CW
5545
5546 return true;
5547 }
5548
5549 return false;
5550}
5551
b7076546 5552void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
5553{
5554 u32 temp;
5555
dc008bf0 5556 intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
060f02d8
VS
5557
5558 mutex_lock(&dev_priv->sb_lock);
5559
5560 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5561 temp |= SBI_SSCCTL_DISABLE;
5562 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5563
5564 mutex_unlock(&dev_priv->sb_lock);
5565}
5566
e615efe4 5567/* Program iCLKIP clock to the desired frequency */
c5b36fac 5568static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
e615efe4 5569{
2225f3c6 5570 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0dcdc382 5571 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1326a92c 5572 int clock = crtc_state->hw.adjusted_mode.crtc_clock;
e615efe4
ED
5573 u32 divsel, phaseinc, auxdiv, phasedir = 0;
5574 u32 temp;
5575
060f02d8 5576 lpt_disable_iclkip(dev_priv);
e615efe4 5577
64b46a06
VS
5578 /* The iCLK virtual clock root frequency is in MHz,
5579 * but the adjusted_mode->crtc_clock in in KHz. To get the
5580 * divisors, it is necessary to divide one by another, so we
5581 * convert the virtual clock precision to KHz here for higher
5582 * precision.
5583 */
5584 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
5585 u32 iclk_virtual_root_freq = 172800 * 1000;
5586 u32 iclk_pi_range = 64;
64b46a06 5587 u32 desired_divisor;
e615efe4 5588
64b46a06
VS
5589 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5590 clock << auxdiv);
5591 divsel = (desired_divisor / iclk_pi_range) - 2;
5592 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 5593
64b46a06
VS
5594 /*
5595 * Near 20MHz is a corner case which is
5596 * out of range for the 7-bit divisor
5597 */
5598 if (divsel <= 0x7f)
5599 break;
e615efe4
ED
5600 }
5601
5602 /* This should not happen with any sane values */
e57291c2
PB
5603 drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5604 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5605 drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5606 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
e615efe4 5607
cd49f818
WK
5608 drm_dbg_kms(&dev_priv->drm,
5609 "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5610 clock, auxdiv, divsel, phasedir, phaseinc);
e615efe4 5611
060f02d8
VS
5612 mutex_lock(&dev_priv->sb_lock);
5613
e615efe4 5614 /* Program SSCDIVINTPHASE6 */
988d6ee8 5615 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
5616 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5617 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5618 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5619 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5620 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5621 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 5622 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
5623
5624 /* Program SSCAUXDIV */
988d6ee8 5625 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
5626 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5627 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 5628 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
5629
5630 /* Enable modulator and associated divider */
988d6ee8 5631 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 5632 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 5633 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 5634
060f02d8
VS
5635 mutex_unlock(&dev_priv->sb_lock);
5636
e615efe4
ED
5637 /* Wait for initialization time */
5638 udelay(24);
5639
dc008bf0 5640 intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
e615efe4
ED
5641}
5642
8802e5b6
VS
5643int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5644{
5645 u32 divsel, phaseinc, auxdiv;
5646 u32 iclk_virtual_root_freq = 172800 * 1000;
5647 u32 iclk_pi_range = 64;
5648 u32 desired_divisor;
5649 u32 temp;
5650
dc008bf0 5651 if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
8802e5b6
VS
5652 return 0;
5653
5654 mutex_lock(&dev_priv->sb_lock);
5655
5656 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5657 if (temp & SBI_SSCCTL_DISABLE) {
5658 mutex_unlock(&dev_priv->sb_lock);
5659 return 0;
5660 }
5661
5662 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5663 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5664 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5665 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5666 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5667
5668 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5669 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5670 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5671
5672 mutex_unlock(&dev_priv->sb_lock);
5673
5674 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5675
5676 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5677 desired_divisor << auxdiv);
5678}
5679
9eae5e27
LDM
5680static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5681 enum pipe pch_transcoder)
275f01b2 5682{
2225f3c6 5683 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5e1cdf54
ML
5684 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5685 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
275f01b2 5686
dc008bf0
JN
5687 intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5688 intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5689 intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5690 intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5691 intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5692 intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
275f01b2 5693
dc008bf0
JN
5694 intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5695 intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5696 intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5697 intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5698 intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5699 intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5700 intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5701 intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
275f01b2
DV
5702}
5703
b0b62d84 5704static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
1fbc0d78 5705{
ba3f4d0a 5706 u32 temp;
1fbc0d78 5707
dc008bf0 5708 temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
003632d9 5709 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
5710 return;
5711
e57291c2
PB
5712 drm_WARN_ON(&dev_priv->drm,
5713 intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5714 FDI_RX_ENABLE);
5715 drm_WARN_ON(&dev_priv->drm,
5716 intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5717 FDI_RX_ENABLE);
1fbc0d78 5718
003632d9
ACO
5719 temp &= ~FDI_BC_BIFURCATION_SELECT;
5720 if (enable)
5721 temp |= FDI_BC_BIFURCATION_SELECT;
5722
cd49f818
WK
5723 drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5724 enable ? "en" : "dis");
dc008bf0
JN
5725 intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5726 intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
1fbc0d78
DV
5727}
5728
74bb98ba 5729static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
1fbc0d78 5730{
2225f3c6 5731 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b0b62d84 5732 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1fbc0d78 5733
b0b62d84 5734 switch (crtc->pipe) {
1fbc0d78
DV
5735 case PIPE_A:
5736 break;
5737 case PIPE_B:
b0b62d84
ML
5738 if (crtc_state->fdi_lanes > 2)
5739 cpt_set_fdi_bc_bifurcation(dev_priv, false);
1fbc0d78 5740 else
b0b62d84 5741 cpt_set_fdi_bc_bifurcation(dev_priv, true);
1fbc0d78
DV
5742
5743 break;
5744 case PIPE_C:
b0b62d84 5745 cpt_set_fdi_bc_bifurcation(dev_priv, true);
1fbc0d78
DV
5746
5747 break;
5748 default:
5749 BUG();
5750 }
5751}
5752
f606bc6d
VS
5753/*
5754 * Finds the encoder associated with the given CRTC. This can only be
5755 * used when we know that the CRTC isn't feeding multiple encoders!
5756 */
5757static struct intel_encoder *
5a0b385e
VS
5758intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5759 const struct intel_crtc_state *crtc_state)
f606bc6d 5760{
2225f3c6 5761 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
f606bc6d
VS
5762 const struct drm_connector_state *connector_state;
5763 const struct drm_connector *connector;
5764 struct intel_encoder *encoder = NULL;
5765 int num_encoders = 0;
5766 int i;
5767
5a0b385e 5768 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
f606bc6d
VS
5769 if (connector_state->crtc != &crtc->base)
5770 continue;
5771
5772 encoder = to_intel_encoder(connector_state->best_encoder);
5773 num_encoders++;
5774 }
5775
3a47ae20
PB
5776 drm_WARN(encoder->base.dev, num_encoders != 1,
5777 "%d encoders for pipe %c\n",
5778 num_encoders, pipe_name(crtc->pipe));
f606bc6d
VS
5779
5780 return encoder;
5781}
5782
f67a559d
JB
5783/*
5784 * Enable PCH resources required for PCH ports:
5785 * - PCH PLLs
5786 * - FDI training & RX/TX
5787 * - update transcoder timings
5788 * - DP transcoding bits
5789 * - transcoder
5790 */
9eae5e27
LDM
5791static void ilk_pch_enable(const struct intel_atomic_state *state,
5792 const struct intel_crtc_state *crtc_state)
0e23b99d 5793{
2225f3c6 5794 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4cbe4b2b 5795 struct drm_device *dev = crtc->base.dev;
fac5e23e 5796 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 5797 enum pipe pipe = crtc->pipe;
f0f59a00 5798 u32 temp;
2c07245f 5799
ab9412ba 5800 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 5801
fd6b8f43 5802 if (IS_IVYBRIDGE(dev_priv))
74bb98ba 5803 ivb_update_fdi_bc_bifurcation(crtc_state);
1fbc0d78 5804
cd986abb
DV
5805 /* Write the TU size bits before fdi link training, so that error
5806 * detection works. */
dc008bf0
JN
5807 intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5808 intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
cd986abb 5809
c98e9dcf 5810 /* For PCH output, training FDI link */
dc4a1094 5811 dev_priv->display.fdi_link_train(crtc, crtc_state);
2c07245f 5812
3ad8a208
DV
5813 /* We need to program the right clock selection before writing the pixel
5814 * mutliplier into the DPLL. */
6e266956 5815 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 5816 u32 sel;
4b645f14 5817
dc008bf0 5818 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
11887397
DV
5819 temp |= TRANS_DPLL_ENABLE(pipe);
5820 sel = TRANS_DPLLB_SEL(pipe);
2ce42273 5821 if (crtc_state->shared_dpll ==
8106ddbd 5822 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
5823 temp |= sel;
5824 else
5825 temp &= ~sel;
dc008bf0 5826 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
c98e9dcf 5827 }
5eddb70b 5828
3ad8a208
DV
5829 /* XXX: pch pll's can be enabled any time before we enable the PCH
5830 * transcoder, and we actually should do this to not upset any PCH
5831 * transcoder that already use the clock when we share it.
5832 *
5833 * Note that enable_shared_dpll tries to do the right thing, but
5834 * get_shared_dpll unconditionally resets the pll - we need that to have
5835 * the right LVDS enable sequence. */
65c307fd 5836 intel_enable_shared_dpll(crtc_state);
3ad8a208 5837
d9b6cb56
JB
5838 /* set transcoder timing, panel must allow it */
5839 assert_panel_unlocked(dev_priv, pipe);
9eae5e27 5840 ilk_pch_transcoder_set_timings(crtc_state, pipe);
8db9d77b 5841
303b81e0 5842 intel_fdi_normal_train(crtc);
5e84e1a4 5843
c98e9dcf 5844 /* For PCH DP, enable TRANS_DP_CTL */
6e266956 5845 if (HAS_PCH_CPT(dev_priv) &&
2ce42273 5846 intel_crtc_has_dp_encoder(crtc_state)) {
9c4edaee 5847 const struct drm_display_mode *adjusted_mode =
1326a92c 5848 &crtc_state->hw.adjusted_mode;
dc008bf0 5849 u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 5850 i915_reg_t reg = TRANS_DP_CTL(pipe);
f67dc6d8
VS
5851 enum port port;
5852
dc008bf0 5853 temp = intel_de_read(dev_priv, reg);
5eddb70b 5854 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
5855 TRANS_DP_SYNC_MASK |
5856 TRANS_DP_BPC_MASK);
e3ef4479 5857 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 5858 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 5859
9c4edaee 5860 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 5861 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 5862 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 5863 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf 5864
5a0b385e 5865 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
e57291c2 5866 drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
f67dc6d8 5867 temp |= TRANS_DP_PORT_SEL(port);
2c07245f 5868
dc008bf0 5869 intel_de_write(dev_priv, reg, temp);
6be4a607 5870 }
b52eb4dc 5871
9eae5e27 5872 ilk_enable_pch_transcoder(crtc_state);
f67a559d
JB
5873}
5874
21fd23ac 5875void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1507e5bd 5876{
2225f3c6 5877 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0dcdc382 5878 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2ce42273 5879 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1507e5bd 5880
a2196033 5881 assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1507e5bd 5882
c5b36fac 5883 lpt_program_iclkip(crtc_state);
1507e5bd 5884
0540e488 5885 /* Set transcoder timing. */
9eae5e27 5886 ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
1507e5bd 5887
937bb610 5888 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
5889}
5890
c684fb44
VS
5891static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5892 enum pipe pipe)
d4270e57 5893{
f0f59a00 5894 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
5895 u32 temp;
5896
dc008bf0 5897 temp = intel_de_read(dev_priv, dslreg);
d4270e57 5898 udelay(500);
dc008bf0
JN
5899 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5900 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
cd49f818
WK
5901 drm_err(&dev_priv->drm,
5902 "mode set failed: pipe %c stuck\n",
5903 pipe_name(pipe));
d4270e57
JB
5904 }
5905}
5906
0a59952b
VS
5907/*
5908 * The hardware phase 0.0 refers to the center of the pixel.
5909 * We want to start from the top/left edge which is phase
5910 * -0.5. That matches how the hardware calculates the scaling
5911 * factors (from top-left of the first pixel to bottom-right
5912 * of the last pixel, as opposed to the pixel centers).
5913 *
5914 * For 4:2:0 subsampled chroma planes we obviously have to
5915 * adjust that so that the chroma sample position lands in
5916 * the right spot.
5917 *
5918 * Note that for packed YCbCr 4:2:2 formats there is no way to
5919 * control chroma siting. The hardware simply replicates the
5920 * chroma samples for both of the luma samples, and thus we don't
5921 * actually get the expected MPEG2 chroma siting convention :(
5922 * The same behaviour is observed on pre-SKL platforms as well.
e7a278a3
VS
5923 *
5924 * Theory behind the formula (note that we ignore sub-pixel
5925 * source coordinates):
5926 * s = source sample position
5927 * d = destination sample position
5928 *
5929 * Downscaling 4:1:
5930 * -0.5
5931 * | 0.0
5932 * | | 1.5 (initial phase)
5933 * | | |
5934 * v v v
5935 * | s | s | s | s |
5936 * | d |
5937 *
5938 * Upscaling 1:4:
5939 * -0.5
5940 * | -0.375 (initial phase)
5941 * | | 0.0
5942 * | | |
5943 * v v v
5944 * | s |
5945 * | d | d | d | d |
0a59952b 5946 */
e7a278a3 5947u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
0a59952b
VS
5948{
5949 int phase = -0x8000;
5950 u16 trip = 0;
5951
5952 if (chroma_cosited)
5953 phase += (sub - 1) * 0x8000 / sub;
5954
e7a278a3
VS
5955 phase += scale / (2 * sub);
5956
5957 /*
5958 * Hardware initial phase limited to [-0.5:1.5].
5959 * Since the max hardware scale factor is 3.0, we
5960 * should never actually excdeed 1.0 here.
5961 */
5962 WARN_ON(phase < -0x8000 || phase > 0x18000);
5963
0a59952b
VS
5964 if (phase < 0)
5965 phase = 0x10000 + phase;
5966 else
5967 trip = PS_PHASE_TRIP;
5968
5969 return ((phase >> 2) & PS_PHASE_MASK) | trip;
5970}
5971
69f44d3b
JN
5972#define SKL_MIN_SRC_W 8
5973#define SKL_MAX_SRC_W 4096
5974#define SKL_MIN_SRC_H 8
5975#define SKL_MAX_SRC_H 4096
5976#define SKL_MIN_DST_W 8
5977#define SKL_MAX_DST_W 4096
5978#define SKL_MIN_DST_H 8
5979#define SKL_MAX_DST_H 4096
5980#define ICL_MAX_SRC_W 5120
5981#define ICL_MAX_SRC_H 4096
5982#define ICL_MAX_DST_W 5120
5983#define ICL_MAX_DST_H 4096
5984#define SKL_MIN_YUV_420_SRC_W 16
5985#define SKL_MIN_YUV_420_SRC_H 16
5986
86adf9d7
ML
5987static int
5988skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
d96a7d2a 5989 unsigned int scaler_user, int *scaler_id,
77224cd5 5990 int src_w, int src_h, int dst_w, int dst_h,
4941f35b
ID
5991 const struct drm_format_info *format,
5992 u64 modifier, bool need_scaler)
a1b2278e 5993{
86adf9d7
ML
5994 struct intel_crtc_scaler_state *scaler_state =
5995 &crtc_state->scaler_state;
5996 struct intel_crtc *intel_crtc =
2225f3c6 5997 to_intel_crtc(crtc_state->uapi.crtc);
7f58cbb1
MK
5998 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5999 const struct drm_display_mode *adjusted_mode =
1326a92c 6000 &crtc_state->hw.adjusted_mode;
6156a456 6001
d96a7d2a
VS
6002 /*
6003 * Src coordinates are already rotated by 270 degrees for
6004 * the 90/270 degree plane rotation cases (to match the
6005 * GTT mapping), hence no need to account for rotation here.
6006 */
b1554e23
ML
6007 if (src_w != dst_w || src_h != dst_h)
6008 need_scaler = true;
e5c05931 6009
7f58cbb1
MK
6010 /*
6011 * Scaling/fitting not supported in IF-ID mode in GEN9+
6012 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6013 * Once NV12 is enabled, handle it here while allocating scaler
6014 * for NV12.
6015 */
1326a92c 6016 if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
b1554e23 6017 need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
cd49f818
WK
6018 drm_dbg_kms(&dev_priv->drm,
6019 "Pipe/Plane scaling not supported with IF-ID mode\n");
7f58cbb1
MK
6020 return -EINVAL;
6021 }
6022
a1b2278e
CK
6023 /*
6024 * if plane is being disabled or scaler is no more required or force detach
6025 * - free scaler binded to this plane/crtc
6026 * - in order to do this, update crtc->scaler_usage
6027 *
6028 * Here scaler state in crtc_state is set free so that
6029 * scaler can be assigned to other user. Actual register
6030 * update to free the scaler is done in plane/panel-fit programming.
6031 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6032 */
b1554e23 6033 if (force_detach || !need_scaler) {
a1b2278e 6034 if (*scaler_id >= 0) {
86adf9d7 6035 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
6036 scaler_state->scalers[*scaler_id].in_use = 0;
6037
cd49f818
WK
6038 drm_dbg_kms(&dev_priv->drm,
6039 "scaler_user index %u.%u: "
6040 "Staged freeing scaler id %d scaler_users = 0x%x\n",
6041 intel_crtc->pipe, scaler_user, *scaler_id,
6042 scaler_state->scaler_users);
a1b2278e
CK
6043 *scaler_id = -1;
6044 }
6045 return 0;
6046 }
6047
4941f35b 6048 if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
5d794288 6049 (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
cd49f818
WK
6050 drm_dbg_kms(&dev_priv->drm,
6051 "Planar YUV: src dimensions not met\n");
77224cd5
CK
6052 return -EINVAL;
6053 }
6054
a1b2278e
CK
6055 /* range checks */
6056 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
323301af 6057 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
2dd24a9c 6058 (INTEL_GEN(dev_priv) >= 11 &&
323301af
NM
6059 (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6060 dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
2dd24a9c 6061 (INTEL_GEN(dev_priv) < 11 &&
323301af
NM
6062 (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6063 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
cd49f818
WK
6064 drm_dbg_kms(&dev_priv->drm,
6065 "scaler_user index %u.%u: src %ux%u dst %ux%u "
6066 "size is out of scaler range\n",
6067 intel_crtc->pipe, scaler_user, src_w, src_h,
6068 dst_w, dst_h);
a1b2278e
CK
6069 return -EINVAL;
6070 }
6071
86adf9d7
ML
6072 /* mark this plane as a scaler user in crtc_state */
6073 scaler_state->scaler_users |= (1 << scaler_user);
cd49f818
WK
6074 drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6075 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6076 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6077 scaler_state->scaler_users);
86adf9d7
ML
6078
6079 return 0;
6080}
6081
c5a01ec7 6082static int skl_update_scaler_crtc(struct intel_crtc_state *crtc_state)
86adf9d7 6083{
bafcdad6 6084 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
c5a01ec7 6085 int width, height;
b1554e23 6086
c5a01ec7 6087 if (crtc_state->pch_pfit.enabled) {
35dd95b4
VS
6088 width = drm_rect_width(&crtc_state->pch_pfit.dst);
6089 height = drm_rect_height(&crtc_state->pch_pfit.dst);
c5a01ec7 6090 } else {
bafcdad6
ML
6091 width = pipe_mode->crtc_hdisplay;
6092 height = pipe_mode->crtc_vdisplay;
c5a01ec7 6093 }
c5a01ec7
VS
6094 return skl_update_scaler(crtc_state, !crtc_state->hw.active,
6095 SKL_CRTC_INDEX,
6096 &crtc_state->scaler_state.scaler_id,
6097 crtc_state->pipe_src_w, crtc_state->pipe_src_h,
6098 width, height, NULL, 0,
6099 crtc_state->pch_pfit.enabled);
86adf9d7
ML
6100}
6101
6102/**
6103 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
c38c1455 6104 * @crtc_state: crtc's scaler state
86adf9d7
ML
6105 * @plane_state: atomic plane state to update
6106 *
6107 * Return
6108 * 0 - scaler_usage updated successfully
6109 * error - requested scaling cannot be supported or other error condition
6110 */
da20eabd
ML
6111static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6112 struct intel_plane_state *plane_state)
86adf9d7 6113{
da20eabd 6114 struct intel_plane *intel_plane =
f90a85e7 6115 to_intel_plane(plane_state->uapi.plane);
42fd20ed 6116 struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
7b3cb17a 6117 struct drm_framebuffer *fb = plane_state->hw.fb;
86adf9d7 6118 int ret;
f90a85e7 6119 bool force_detach = !fb || !plane_state->uapi.visible;
b1554e23
ML
6120 bool need_scaler = false;
6121
6122 /* Pre-gen11 and SDR planes always need a scaler for planar formats. */
42fd20ed 6123 if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
4941f35b 6124 fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
b1554e23 6125 need_scaler = true;
86adf9d7 6126
86adf9d7
ML
6127 ret = skl_update_scaler(crtc_state, force_detach,
6128 drm_plane_index(&intel_plane->base),
6129 &plane_state->scaler_id,
f90a85e7
ML
6130 drm_rect_width(&plane_state->uapi.src) >> 16,
6131 drm_rect_height(&plane_state->uapi.src) >> 16,
6132 drm_rect_width(&plane_state->uapi.dst),
6133 drm_rect_height(&plane_state->uapi.dst),
4941f35b
ID
6134 fb ? fb->format : NULL,
6135 fb ? fb->modifier : 0,
6136 need_scaler);
86adf9d7
ML
6137
6138 if (ret || plane_state->scaler_id < 0)
6139 return ret;
6140
a1b2278e 6141 /* check colorkey */
6ec5bd34 6142 if (plane_state->ckey.flags) {
cd49f818
WK
6143 drm_dbg_kms(&dev_priv->drm,
6144 "[PLANE:%d:%s] scaling with color key not allowed",
6145 intel_plane->base.base.id,
6146 intel_plane->base.name);
a1b2278e
CK
6147 return -EINVAL;
6148 }
6149
6150 /* Check src format */
438b74a5 6151 switch (fb->format->format) {
86adf9d7
ML
6152 case DRM_FORMAT_RGB565:
6153 case DRM_FORMAT_XBGR8888:
6154 case DRM_FORMAT_XRGB8888:
6155 case DRM_FORMAT_ABGR8888:
6156 case DRM_FORMAT_ARGB8888:
6157 case DRM_FORMAT_XRGB2101010:
6158 case DRM_FORMAT_XBGR2101010:
f9c43a31
VS
6159 case DRM_FORMAT_ARGB2101010:
6160 case DRM_FORMAT_ABGR2101010:
86adf9d7
ML
6161 case DRM_FORMAT_YUYV:
6162 case DRM_FORMAT_YVYU:
6163 case DRM_FORMAT_UYVY:
6164 case DRM_FORMAT_VYUY:
77224cd5 6165 case DRM_FORMAT_NV12:
da904174 6166 case DRM_FORMAT_XYUV8888:
df7d4156
JPH
6167 case DRM_FORMAT_P010:
6168 case DRM_FORMAT_P012:
6169 case DRM_FORMAT_P016:
296e9b19
SS
6170 case DRM_FORMAT_Y210:
6171 case DRM_FORMAT_Y212:
6172 case DRM_FORMAT_Y216:
ff01e697
ML
6173 case DRM_FORMAT_XVYU2101010:
6174 case DRM_FORMAT_XVYU12_16161616:
6175 case DRM_FORMAT_XVYU16161616:
86adf9d7 6176 break;
6e6c155d
VS
6177 case DRM_FORMAT_XBGR16161616F:
6178 case DRM_FORMAT_ABGR16161616F:
6179 case DRM_FORMAT_XRGB16161616F:
6180 case DRM_FORMAT_ARGB16161616F:
6181 if (INTEL_GEN(dev_priv) >= 11)
6182 break;
df561f66 6183 fallthrough;
86adf9d7 6184 default:
cd49f818
WK
6185 drm_dbg_kms(&dev_priv->drm,
6186 "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6187 intel_plane->base.base.id, intel_plane->base.name,
6188 fb->base.id, fb->format->format);
86adf9d7 6189 return -EINVAL;
a1b2278e
CK
6190 }
6191
a1b2278e
CK
6192 return 0;
6193}
6194
f6df4d46 6195void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
e435d6e5 6196{
cfb627c4 6197 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
e435d6e5
ML
6198 int i;
6199
6200 for (i = 0; i < crtc->num_scalers; i++)
6201 skl_detach_scaler(crtc, i);
6202}
6203
cc2396ff
PB
6204static int cnl_coef_tap(int i)
6205{
6206 return i % 7;
6207}
6208
6209static u16 cnl_nearest_filter_coef(int t)
6210{
6211 return t == 3 ? 0x0800 : 0x3000;
6212}
6213
176fd228 6214/*
cc2396ff
PB
6215 * Theory behind setting nearest-neighbor integer scaling:
6216 *
6217 * 17 phase of 7 taps requires 119 coefficients in 60 dwords per set.
6218 * The letter represents the filter tap (D is the center tap) and the number
6219 * represents the coefficient set for a phase (0-16).
6220 *
6221 * +------------+------------------------+------------------------+
6222 * |Index value | Data value coeffient 1 | Data value coeffient 2 |
6223 * +------------+------------------------+------------------------+
6224 * | 00h | B0 | A0 |
6225 * +------------+------------------------+------------------------+
6226 * | 01h | D0 | C0 |
6227 * +------------+------------------------+------------------------+
6228 * | 02h | F0 | E0 |
6229 * +------------+------------------------+------------------------+
6230 * | 03h | A1 | G0 |
6231 * +------------+------------------------+------------------------+
6232 * | 04h | C1 | B1 |
6233 * +------------+------------------------+------------------------+
6234 * | ... | ... | ... |
6235 * +------------+------------------------+------------------------+
6236 * | 38h | B16 | A16 |
6237 * +------------+------------------------+------------------------+
6238 * | 39h | D16 | C16 |
6239 * +------------+------------------------+------------------------+
6240 * | 3Ah | F16 | C16 |
6241 * +------------+------------------------+------------------------+
6242 * | 3Bh | Reserved | G16 |
6243 * +------------+------------------------+------------------------+
6244 *
6245 * To enable nearest-neighbor scaling: program scaler coefficents with
6246 * the center tap (Dxx) values set to 1 and all other values set to 0 as per
6247 * SCALER_COEFFICIENT_FORMAT
6248 *
6249 */
6250
6251static void cnl_program_nearest_filter_coefs(struct drm_i915_private *dev_priv,
6252 enum pipe pipe, int id, int set)
6253{
6254 int i;
6255
6256 intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set),
6257 PS_COEE_INDEX_AUTO_INC);
6258
6259 for (i = 0; i < 17 * 7; i += 2) {
6260 u32 tmp;
6261 int t;
6262
6263 t = cnl_coef_tap(i);
6264 tmp = cnl_nearest_filter_coef(t);
6265
6266 t = cnl_coef_tap(i + 1);
6267 tmp |= cnl_nearest_filter_coef(t) << 16;
6268
6269 intel_de_write_fw(dev_priv, CNL_PS_COEF_DATA_SET(pipe, id, set),
6270 tmp);
6271 }
6272
6273 intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set), 0);
6274}
6275
6276inline u32 skl_scaler_get_filter_select(enum drm_scaling_filter filter, int set)
6277{
6278 if (filter == DRM_SCALING_FILTER_NEAREST_NEIGHBOR) {
6279 return (PS_FILTER_PROGRAMMED |
6280 PS_Y_VERT_FILTER_SELECT(set) |
6281 PS_Y_HORZ_FILTER_SELECT(set) |
6282 PS_UV_VERT_FILTER_SELECT(set) |
6283 PS_UV_HORZ_FILTER_SELECT(set));
6284 }
6285
6286 return PS_FILTER_MEDIUM;
6287}
6288
6289void skl_scaler_setup_filter(struct drm_i915_private *dev_priv, enum pipe pipe,
6290 int id, int set, enum drm_scaling_filter filter)
6291{
6292 switch (filter) {
6293 case DRM_SCALING_FILTER_DEFAULT:
6294 break;
6295 case DRM_SCALING_FILTER_NEAREST_NEIGHBOR:
6296 cnl_program_nearest_filter_coefs(dev_priv, pipe, id, set);
6297 break;
6298 default:
6299 MISSING_CASE(filter);
6300 }
6301}
6302
f6df4d46 6303static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
bd2e244f 6304{
2225f3c6 6305 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 6306 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b2562712
ML
6307 const struct intel_crtc_scaler_state *scaler_state =
6308 &crtc_state->scaler_state;
35dd95b4
VS
6309 struct drm_rect src = {
6310 .x2 = crtc_state->pipe_src_w << 16,
6311 .y2 = crtc_state->pipe_src_h << 16,
6312 };
6313 const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
eac9c585 6314 u16 uv_rgb_hphase, uv_rgb_vphase;
35dd95b4
VS
6315 enum pipe pipe = crtc->pipe;
6316 int width = drm_rect_width(dst);
6317 int height = drm_rect_height(dst);
6318 int x = dst->x1;
6319 int y = dst->y1;
6320 int hscale, vscale;
eac9c585
VS
6321 unsigned long irqflags;
6322 int id;
6d1a2fde 6323 u32 ps_ctrl;
a1b2278e 6324
eac9c585
VS
6325 if (!crtc_state->pch_pfit.enabled)
6326 return;
a1b2278e 6327
eac9c585
VS
6328 if (drm_WARN_ON(&dev_priv->drm,
6329 crtc_state->scaler_state.scaler_id < 0))
6330 return;
a1b2278e 6331
35dd95b4
VS
6332 hscale = drm_rect_calc_hscale(&src, dst, 0, INT_MAX);
6333 vscale = drm_rect_calc_vscale(&src, dst, 0, INT_MAX);
e7a278a3 6334
eac9c585
VS
6335 uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6336 uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
0a59952b 6337
eac9c585 6338 id = scaler_state->scaler_id;
f986ef2e 6339
6d1a2fde
PB
6340 ps_ctrl = skl_scaler_get_filter_select(crtc_state->hw.scaling_filter, 0);
6341 ps_ctrl |= PS_SCALER_EN | scaler_state->scalers[id].mode;
6342
eac9c585 6343 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
f986ef2e 6344
6d1a2fde
PB
6345 skl_scaler_setup_filter(dev_priv, pipe, id, 0,
6346 crtc_state->hw.scaling_filter);
6347
6348 intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), ps_ctrl);
6349
eac9c585
VS
6350 intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6351 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6352 intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6353 PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6354 intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
35dd95b4 6355 x << 16 | y);
eac9c585 6356 intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
35dd95b4 6357 width << 16 | height);
f986ef2e 6358
eac9c585 6359 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
bd2e244f
JB
6360}
6361
9eae5e27 6362static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
b074cec8 6363{
2225f3c6 6364 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 6365 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
35dd95b4 6366 const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
d048a268 6367 enum pipe pipe = crtc->pipe;
35dd95b4
VS
6368 int width = drm_rect_width(dst);
6369 int height = drm_rect_height(dst);
6370 int x = dst->x1;
6371 int y = dst->y1;
b074cec8 6372
eac9c585
VS
6373 if (!crtc_state->pch_pfit.enabled)
6374 return;
6375
6376 /* Force use of hard-coded filter coefficients
6377 * as some pre-programmed values are broken,
6378 * e.g. x201.
6379 */
6380 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6381 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6382 PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6383 else
6384 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6385 PF_FILTER_MED_3x3);
35dd95b4
VS
6386 intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
6387 intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
d4270e57
JB
6388}
6389
199ea381 6390void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 6391{
2225f3c6 6392 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
cea165c3 6393 struct drm_device *dev = crtc->base.dev;
fac5e23e 6394 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 6395
24f28450 6396 if (!crtc_state->ips_enabled)
d77e4531
PZ
6397 return;
6398
307e4498
ML
6399 /*
6400 * We can only enable IPS after we enable a plane and wait for a vblank
6401 * This function is called from post_plane_update, which is run after
6402 * a vblank wait.
6403 */
e57291c2 6404 drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
51f5a096 6405
8652744b 6406 if (IS_BROADWELL(dev_priv)) {
e57291c2
PB
6407 drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6408 IPS_ENABLE | IPS_PCODE_CONTROL));
2a114cc1
BW
6409 /* Quoting Art Runyan: "its not safe to expect any particular
6410 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
6411 * mailbox." Moreover, the mailbox may return a bogus state,
6412 * so we need to just enable it and continue on.
2a114cc1
BW
6413 */
6414 } else {
dc008bf0 6415 intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
2a114cc1
BW
6416 /* The bit only becomes 1 in the next vblank, so this wait here
6417 * is essentially intel_wait_for_vblank. If we don't have this
6418 * and don't wait for vblanks until the end of crtc_enable, then
6419 * the HW state readout code will complain that the expected
6420 * IPS_CTL value is not the one we read. */
4cb3b44d 6421 if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
cd49f818
WK
6422 drm_err(&dev_priv->drm,
6423 "Timed out waiting for IPS enable\n");
2a114cc1 6424 }
d77e4531
PZ
6425}
6426
199ea381 6427void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
d77e4531 6428{
2225f3c6 6429 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
d77e4531 6430 struct drm_device *dev = crtc->base.dev;
fac5e23e 6431 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 6432
199ea381 6433 if (!crtc_state->ips_enabled)
d77e4531
PZ
6434 return;
6435
8652744b 6436 if (IS_BROADWELL(dev_priv)) {
e57291c2
PB
6437 drm_WARN_ON(dev,
6438 sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
acb3ef0e
ID
6439 /*
6440 * Wait for PCODE to finish disabling IPS. The BSpec specified
6441 * 42ms timeout value leads to occasional timeouts so use 100ms
6442 * instead.
6443 */
4cb3b44d 6444 if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
cd49f818
WK
6445 drm_err(&dev_priv->drm,
6446 "Timed out waiting for IPS disable\n");
e59150dc 6447 } else {
dc008bf0
JN
6448 intel_de_write(dev_priv, IPS_CTL, 0);
6449 intel_de_posting_read(dev_priv, IPS_CTL);
e59150dc 6450 }
d77e4531
PZ
6451
6452 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 6453 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
6454}
6455
7cac945f 6456static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 6457{
cb5eb072 6458 if (intel_crtc->overlay)
d3eedb1a 6459 (void) intel_overlay_switch_off(intel_crtc->overlay);
d3eedb1a
VS
6460
6461 /* Let userspace switch the overlay on again. In most cases userspace
6462 * has to recompute where to put it anyway.
6463 */
6464}
6465
24f28450
ML
6466static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6467 const struct intel_crtc_state *new_crtc_state)
6468{
2225f3c6 6469 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
051a6d8d
VS
6470 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6471
24f28450
ML
6472 if (!old_crtc_state->ips_enabled)
6473 return false;
6474
69f786ae 6475 if (needs_modeset(new_crtc_state))
24f28450
ML
6476 return true;
6477
051a6d8d
VS
6478 /*
6479 * Workaround : Do not read or write the pipe palette/gamma data while
6480 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6481 *
6482 * Disable IPS before we program the LUT.
6483 */
6484 if (IS_HASWELL(dev_priv) &&
2225f3c6 6485 (new_crtc_state->uapi.color_mgmt_changed ||
051a6d8d
VS
6486 new_crtc_state->update_pipe) &&
6487 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6488 return true;
6489
24f28450
ML
6490 return !new_crtc_state->ips_enabled;
6491}
6492
6493static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6494 const struct intel_crtc_state *new_crtc_state)
6495{
2225f3c6 6496 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
051a6d8d
VS
6497 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6498
24f28450
ML
6499 if (!new_crtc_state->ips_enabled)
6500 return false;
6501
69f786ae 6502 if (needs_modeset(new_crtc_state))
24f28450
ML
6503 return true;
6504
051a6d8d
VS
6505 /*
6506 * Workaround : Do not read or write the pipe palette/gamma data while
6507 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6508 *
6509 * Re-enable IPS after the LUT has been programmed.
6510 */
6511 if (IS_HASWELL(dev_priv) &&
2225f3c6 6512 (new_crtc_state->uapi.color_mgmt_changed ||
051a6d8d
VS
6513 new_crtc_state->update_pipe) &&
6514 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6515 return true;
6516
24f28450
ML
6517 /*
6518 * We can't read out IPS on broadwell, assume the worst and
6519 * forcibly enable IPS on the first fastset.
6520 */
a227569d 6521 if (new_crtc_state->update_pipe && old_crtc_state->inherited)
24f28450
ML
6522 return true;
6523
6524 return !old_crtc_state->ips_enabled;
6525}
6526
d2432796 6527static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
8e021151 6528{
d2432796
VS
6529 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6530
8e021151
ML
6531 if (!crtc_state->nv12_planes)
6532 return false;
6533
1347d3ce 6534 /* WA Display #0827: Gen9:all */
cf819eff 6535 if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
8e021151
ML
6536 return true;
6537
6538 return false;
6539}
6540
d2432796 6541static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
51eb1a1d 6542{
d2432796
VS
6543 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6544
f96198ab
MA
6545 /* Wa_2006604312:icl,ehl */
6546 if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
51eb1a1d
RS
6547 return true;
6548
6549 return false;
6550}
6551
7181f5c5
VS
6552static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6553 const struct intel_crtc_state *new_crtc_state)
6554{
6555 return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6556 new_crtc_state->active_planes;
6557}
6558
6559static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6560 const struct intel_crtc_state *new_crtc_state)
6561{
6562 return old_crtc_state->active_planes &&
6563 (!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6564}
6565
bee43ca4
VS
6566static void intel_post_plane_update(struct intel_atomic_state *state,
6567 struct intel_crtc *crtc)
5a21b665 6568{
bee43ca4 6569 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
bee43ca4
VS
6570 const struct intel_crtc_state *old_crtc_state =
6571 intel_atomic_get_old_crtc_state(state, crtc);
6572 const struct intel_crtc_state *new_crtc_state =
6573 intel_atomic_get_new_crtc_state(state, crtc);
7181f5c5 6574 enum pipe pipe = crtc->pipe;
5a21b665 6575
bee43ca4 6576 intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
5a21b665 6577
0e75fb8c 6578 if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
432081bc 6579 intel_update_watermarks(crtc);
5a21b665 6580
0e75fb8c
VS
6581 if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6582 hsw_enable_ips(new_crtc_state);
24f28450 6583
9ecc6eab 6584 intel_fbc_post_update(state, crtc);
5a21b665 6585
d2432796 6586 if (needs_nv12_wa(old_crtc_state) &&
0e75fb8c 6587 !needs_nv12_wa(new_crtc_state))
7181f5c5 6588 skl_wa_827(dev_priv, pipe, false);
51eb1a1d 6589
d2432796 6590 if (needs_scalerclk_wa(old_crtc_state) &&
0e75fb8c 6591 !needs_scalerclk_wa(new_crtc_state))
7181f5c5 6592 icl_wa_scalerclkgating(dev_priv, pipe, false);
5a21b665
DV
6593}
6594
e5cb1afb
K
6595static void skl_disable_async_flip_wa(struct intel_atomic_state *state,
6596 struct intel_crtc *crtc,
6597 const struct intel_crtc_state *new_crtc_state)
6598{
6599 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6600 struct intel_plane *plane;
6601 struct intel_plane_state *new_plane_state;
6602 int i;
6603
6604 for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
6605 u32 update_mask = new_crtc_state->update_planes;
6606 u32 plane_ctl, surf_addr;
6607 enum plane_id plane_id;
6608 unsigned long irqflags;
6609 enum pipe pipe;
6610
6611 if (crtc->pipe != plane->pipe ||
6612 !(update_mask & BIT(plane->id)))
6613 continue;
6614
6615 plane_id = plane->id;
6616 pipe = plane->pipe;
6617
6618 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6619 plane_ctl = intel_de_read_fw(dev_priv, PLANE_CTL(pipe, plane_id));
6620 surf_addr = intel_de_read_fw(dev_priv, PLANE_SURF(pipe, plane_id));
6621
6622 plane_ctl &= ~PLANE_CTL_ASYNC_FLIP;
6623
6624 intel_de_write_fw(dev_priv, PLANE_CTL(pipe, plane_id), plane_ctl);
6625 intel_de_write_fw(dev_priv, PLANE_SURF(pipe, plane_id), surf_addr);
6626 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6627 }
6628
6629 intel_wait_for_vblank(dev_priv, crtc->pipe);
6630}
6631
bee43ca4
VS
6632static void intel_pre_plane_update(struct intel_atomic_state *state,
6633 struct intel_crtc *crtc)
ac21b225 6634{
bee43ca4 6635 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
bee43ca4
VS
6636 const struct intel_crtc_state *old_crtc_state =
6637 intel_atomic_get_old_crtc_state(state, crtc);
6638 const struct intel_crtc_state *new_crtc_state =
6639 intel_atomic_get_new_crtc_state(state, crtc);
7181f5c5 6640 enum pipe pipe = crtc->pipe;
ac21b225 6641
0e75fb8c 6642 if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
24f28450
ML
6643 hsw_disable_ips(old_crtc_state);
6644
9ecc6eab 6645 if (intel_fbc_pre_update(state, crtc))
07fd0df8
VS
6646 intel_wait_for_vblank(dev_priv, pipe);
6647
8e021151 6648 /* Display WA 827 */
d2432796 6649 if (!needs_nv12_wa(old_crtc_state) &&
0e75fb8c 6650 needs_nv12_wa(new_crtc_state))
7181f5c5 6651 skl_wa_827(dev_priv, pipe, true);
51eb1a1d 6652
f96198ab 6653 /* Wa_2006604312:icl,ehl */
d2432796 6654 if (!needs_scalerclk_wa(old_crtc_state) &&
0e75fb8c 6655 needs_scalerclk_wa(new_crtc_state))
7181f5c5 6656 icl_wa_scalerclkgating(dev_priv, pipe, true);
8e021151 6657
5eeb798b
VS
6658 /*
6659 * Vblank time updates from the shadow to live plane control register
6660 * are blocked if the memory self-refresh mode is active at that
6661 * moment. So to make sure the plane gets truly disabled, disable
6662 * first the self-refresh mode. The self-refresh enable bit in turn
6663 * will be checked/applied by the HW only at the next frame start
6664 * event which is after the vblank start event, so we need to have a
6665 * wait-for-vblank between disabling the plane and the pipe.
6666 */
1326a92c 6667 if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
0e75fb8c 6668 new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
7181f5c5 6669 intel_wait_for_vblank(dev_priv, pipe);
92826fcd 6670
ed4a6a7c
MR
6671 /*
6672 * IVB workaround: must disable low power watermarks for at least
6673 * one frame before enabling scaling. LP watermarks can be re-enabled
6674 * when scaling is disabled.
6675 *
6676 * WaCxSRDisabledForSpriteScaling:ivb
6677 */
0e75fb8c
VS
6678 if (old_crtc_state->hw.active &&
6679 new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
7181f5c5 6680 intel_wait_for_vblank(dev_priv, pipe);
ed4a6a7c
MR
6681
6682 /*
7181f5c5
VS
6683 * If we're doing a modeset we don't need to do any
6684 * pre-vblank watermark programming here.
ed4a6a7c 6685 */
7181f5c5
VS
6686 if (!needs_modeset(new_crtc_state)) {
6687 /*
6688 * For platforms that support atomic watermarks, program the
6689 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
6690 * will be the intermediate values that are safe for both pre- and
6691 * post- vblank; when vblank happens, the 'active' values will be set
6692 * to the final 'target' values and we'll do this again to get the
6693 * optimal watermarks. For gen9+ platforms, the values we program here
6694 * will be the final target values which will get automatically latched
6695 * at vblank time; no further programming will be necessary.
6696 *
6697 * If a platform hasn't been transitioned to atomic watermarks yet,
6698 * we'll continue to update watermarks the old way, if flags tell
6699 * us to.
6700 */
6701 if (dev_priv->display.initial_watermarks)
6702 dev_priv->display.initial_watermarks(state, crtc);
6703 else if (new_crtc_state->update_wm_pre)
6704 intel_update_watermarks(crtc);
6705 }
ed4a6a7c
MR
6706
6707 /*
7181f5c5
VS
6708 * Gen2 reports pipe underruns whenever all planes are disabled.
6709 * So disable underrun reporting before all the planes get disabled.
ed4a6a7c 6710 *
7181f5c5
VS
6711 * We do this after .initial_watermarks() so that we have a
6712 * chance of catching underruns with the intermediate watermarks
6713 * vs. the old plane configuration.
ed4a6a7c 6714 */
7181f5c5
VS
6715 if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6716 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
e5cb1afb
K
6717
6718 /*
6719 * WA for platforms where async address update enable bit
6720 * is double buffered and only latched at start of vblank.
6721 */
6722 if (old_crtc_state->uapi.async_flip &&
6723 !new_crtc_state->uapi.async_flip &&
6724 IS_GEN_RANGE(dev_priv, 9, 10))
6725 skl_disable_async_flip_wa(state, crtc, new_crtc_state);
ac21b225
ML
6726}
6727
0dd14be3
VS
6728static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6729 struct intel_crtc *crtc)
87d4300a 6730{
0dd14be3
VS
6731 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6732 const struct intel_crtc_state *new_crtc_state =
6733 intel_atomic_get_new_crtc_state(state, crtc);
6734 unsigned int update_mask = new_crtc_state->update_planes;
6735 const struct intel_plane_state *old_plane_state;
f59e9701
ML
6736 struct intel_plane *plane;
6737 unsigned fb_bits = 0;
0dd14be3 6738 int i;
87d4300a 6739
f59e9701 6740 intel_crtc_dpms_overlay_disable(crtc);
27321ae8 6741
0dd14be3
VS
6742 for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6743 if (crtc->pipe != plane->pipe ||
6744 !(update_mask & BIT(plane->id)))
6745 continue;
6746
c48b86f9 6747 intel_disable_plane(plane, new_crtc_state);
f98551ae 6748
f90a85e7 6749 if (old_plane_state->uapi.visible)
f59e9701 6750 fb_bits |= plane->frontbuffer_bit;
f59e9701
ML
6751 }
6752
0dd14be3 6753 intel_frontbuffer_flip(dev_priv, fb_bits);
a5c4d7bc
VS
6754}
6755
24a7bfe0
ID
6756/*
6757 * intel_connector_primary_encoder - get the primary encoder for a connector
6758 * @connector: connector for which to return the encoder
6759 *
6760 * Returns the primary encoder for a connector. There is a 1:1 mapping from
6761 * all connectors to their encoder, except for DP-MST connectors which have
6762 * both a virtual and a primary encoder. These DP-MST primary encoders can be
6763 * pointed to by as many DP-MST connectors as there are pipes.
6764 */
6765static struct intel_encoder *
6766intel_connector_primary_encoder(struct intel_connector *connector)
6767{
6768 struct intel_encoder *encoder;
6769
6770 if (connector->mst_port)
6771 return &dp_to_dig_port(connector->mst_port)->base;
6772
43a6d19c 6773 encoder = intel_attached_encoder(connector);
ce04ecd9 6774 drm_WARN_ON(connector->base.dev, !encoder);
24a7bfe0
ID
6775
6776 return encoder;
6777}
6778
24a7bfe0
ID
6779static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6780{
24a7bfe0 6781 struct drm_connector_state *new_conn_state;
ee36c7c0 6782 struct drm_connector *connector;
24a7bfe0
ID
6783 int i;
6784
ee36c7c0
JRS
6785 for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6786 i) {
6787 struct intel_connector *intel_connector;
24a7bfe0
ID
6788 struct intel_encoder *encoder;
6789 struct intel_crtc *crtc;
6790
ee36c7c0 6791 if (!intel_connector_needs_modeset(state, connector))
24a7bfe0
ID
6792 continue;
6793
ee36c7c0
JRS
6794 intel_connector = to_intel_connector(connector);
6795 encoder = intel_connector_primary_encoder(intel_connector);
24a7bfe0
ID
6796 if (!encoder->update_prepare)
6797 continue;
6798
6799 crtc = new_conn_state->crtc ?
6800 to_intel_crtc(new_conn_state->crtc) : NULL;
6801 encoder->update_prepare(state, encoder, crtc);
6802 }
6803}
6804
6805static void intel_encoders_update_complete(struct intel_atomic_state *state)
6806{
24a7bfe0 6807 struct drm_connector_state *new_conn_state;
ee36c7c0 6808 struct drm_connector *connector;
24a7bfe0
ID
6809 int i;
6810
ee36c7c0
JRS
6811 for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6812 i) {
6813 struct intel_connector *intel_connector;
24a7bfe0
ID
6814 struct intel_encoder *encoder;
6815 struct intel_crtc *crtc;
6816
ee36c7c0 6817 if (!intel_connector_needs_modeset(state, connector))
24a7bfe0
ID
6818 continue;
6819
ee36c7c0
JRS
6820 intel_connector = to_intel_connector(connector);
6821 encoder = intel_connector_primary_encoder(intel_connector);
24a7bfe0
ID
6822 if (!encoder->update_complete)
6823 continue;
6824
6825 crtc = new_conn_state->crtc ?
6826 to_intel_crtc(new_conn_state->crtc) : NULL;
6827 encoder->update_complete(state, encoder, crtc);
6828 }
6829}
6830
021ba100
VS
6831static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6832 struct intel_crtc *crtc)
fb1c98b1 6833{
021ba100
VS
6834 const struct intel_crtc_state *crtc_state =
6835 intel_atomic_get_new_crtc_state(state, crtc);
6836 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6837 struct drm_connector *conn;
6838 int i;
6839
855e0d68 6840 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6841 struct intel_encoder *encoder =
6842 to_intel_encoder(conn_state->best_encoder);
6843
855e0d68 6844 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6845 continue;
6846
6847 if (encoder->pre_pll_enable)
ede9771d
VS
6848 encoder->pre_pll_enable(state, encoder,
6849 crtc_state, conn_state);
fb1c98b1
ML
6850 }
6851}
6852
021ba100
VS
6853static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6854 struct intel_crtc *crtc)
fb1c98b1 6855{
021ba100
VS
6856 const struct intel_crtc_state *crtc_state =
6857 intel_atomic_get_new_crtc_state(state, crtc);
6858 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6859 struct drm_connector *conn;
6860 int i;
6861
855e0d68 6862 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6863 struct intel_encoder *encoder =
6864 to_intel_encoder(conn_state->best_encoder);
6865
855e0d68 6866 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6867 continue;
6868
6869 if (encoder->pre_enable)
ede9771d
VS
6870 encoder->pre_enable(state, encoder,
6871 crtc_state, conn_state);
fb1c98b1
ML
6872 }
6873}
6874
021ba100
VS
6875static void intel_encoders_enable(struct intel_atomic_state *state,
6876 struct intel_crtc *crtc)
fb1c98b1 6877{
021ba100
VS
6878 const struct intel_crtc_state *crtc_state =
6879 intel_atomic_get_new_crtc_state(state, crtc);
6880 const struct drm_connector_state *conn_state;
fb1c98b1
ML
6881 struct drm_connector *conn;
6882 int i;
6883
855e0d68 6884 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
fb1c98b1
ML
6885 struct intel_encoder *encoder =
6886 to_intel_encoder(conn_state->best_encoder);
6887
855e0d68 6888 if (conn_state->crtc != &crtc->base)
fb1c98b1
ML
6889 continue;
6890
c84c6fe3 6891 if (encoder->enable)
ede9771d
VS
6892 encoder->enable(state, encoder,
6893 crtc_state, conn_state);
fb1c98b1
ML
6894 intel_opregion_notify_encoder(encoder, true);
6895 }
6896}
6897
021ba100
VS
6898static void intel_encoders_disable(struct intel_atomic_state *state,
6899 struct intel_crtc *crtc)
fb1c98b1 6900{
021ba100
VS
6901 const struct intel_crtc_state *old_crtc_state =
6902 intel_atomic_get_old_crtc_state(state, crtc);
6903 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6904 struct drm_connector *conn;
6905 int i;
6906
855e0d68 6907 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6908 struct intel_encoder *encoder =
6909 to_intel_encoder(old_conn_state->best_encoder);
6910
855e0d68 6911 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6912 continue;
6913
6914 intel_opregion_notify_encoder(encoder, false);
c84c6fe3 6915 if (encoder->disable)
ede9771d
VS
6916 encoder->disable(state, encoder,
6917 old_crtc_state, old_conn_state);
fb1c98b1
ML
6918 }
6919}
6920
021ba100
VS
6921static void intel_encoders_post_disable(struct intel_atomic_state *state,
6922 struct intel_crtc *crtc)
fb1c98b1 6923{
021ba100
VS
6924 const struct intel_crtc_state *old_crtc_state =
6925 intel_atomic_get_old_crtc_state(state, crtc);
6926 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6927 struct drm_connector *conn;
6928 int i;
6929
855e0d68 6930 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6931 struct intel_encoder *encoder =
6932 to_intel_encoder(old_conn_state->best_encoder);
6933
855e0d68 6934 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6935 continue;
6936
6937 if (encoder->post_disable)
ede9771d
VS
6938 encoder->post_disable(state, encoder,
6939 old_crtc_state, old_conn_state);
fb1c98b1
ML
6940 }
6941}
6942
021ba100
VS
6943static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6944 struct intel_crtc *crtc)
fb1c98b1 6945{
021ba100
VS
6946 const struct intel_crtc_state *old_crtc_state =
6947 intel_atomic_get_old_crtc_state(state, crtc);
6948 const struct drm_connector_state *old_conn_state;
fb1c98b1
ML
6949 struct drm_connector *conn;
6950 int i;
6951
855e0d68 6952 for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
fb1c98b1
ML
6953 struct intel_encoder *encoder =
6954 to_intel_encoder(old_conn_state->best_encoder);
6955
855e0d68 6956 if (old_conn_state->crtc != &crtc->base)
fb1c98b1
ML
6957 continue;
6958
6959 if (encoder->post_pll_disable)
ede9771d
VS
6960 encoder->post_pll_disable(state, encoder,
6961 old_crtc_state, old_conn_state);
fb1c98b1
ML
6962 }
6963}
6964
021ba100
VS
6965static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6966 struct intel_crtc *crtc)
608ed4ab 6967{
021ba100
VS
6968 const struct intel_crtc_state *crtc_state =
6969 intel_atomic_get_new_crtc_state(state, crtc);
6970 const struct drm_connector_state *conn_state;
608ed4ab
HG
6971 struct drm_connector *conn;
6972 int i;
6973
855e0d68 6974 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
608ed4ab
HG
6975 struct intel_encoder *encoder =
6976 to_intel_encoder(conn_state->best_encoder);
6977
855e0d68 6978 if (conn_state->crtc != &crtc->base)
608ed4ab
HG
6979 continue;
6980
6981 if (encoder->update_pipe)
ede9771d
VS
6982 encoder->update_pipe(state, encoder,
6983 crtc_state, conn_state);
608ed4ab
HG
6984 }
6985}
6986
73a116be
VS
6987static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6988{
2225f3c6 6989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
73a116be
VS
6990 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6991
6992 plane->disable_plane(plane, crtc_state);
6993}
6994
9eae5e27
LDM
6995static void ilk_crtc_enable(struct intel_atomic_state *state,
6996 struct intel_crtc *crtc)
f67a559d 6997{
7451a074
VS
6998 const struct intel_crtc_state *new_crtc_state =
6999 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7000 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7001 enum pipe pipe = crtc->pipe;
f67a559d 7002
e57291c2 7003 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
f67a559d
JB
7004 return;
7005
b2c0593a
VS
7006 /*
7007 * Sometimes spurious CPU pipe underruns happen during FDI
7008 * training, at least with VGA+HDMI cloning. Suppress them.
7009 *
7010 * On ILK we get an occasional spurious CPU pipe underruns
7011 * between eDP port A enable and vdd enable. Also PCH port
7012 * enable seems to result in the occasional CPU pipe underrun.
7013 *
7014 * Spurious PCH underruns also occur during PCH enabling.
7015 */
2b5b6312
VS
7016 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7017 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca 7018
502d8714
VS
7019 if (new_crtc_state->has_pch_encoder)
7020 intel_prepare_shared_dpll(new_crtc_state);
b14b1055 7021
502d8714
VS
7022 if (intel_crtc_has_dp_encoder(new_crtc_state))
7023 intel_dp_set_m_n(new_crtc_state, M1_N1);
29407aab 7024
e7fc3f90 7025 intel_set_transcoder_timings(new_crtc_state);
502d8714 7026 intel_set_pipe_src_size(new_crtc_state);
29407aab 7027
502d8714
VS
7028 if (new_crtc_state->has_pch_encoder)
7029 intel_cpu_transcoder_set_m_n(new_crtc_state,
7030 &new_crtc_state->fdi_m_n, NULL);
29407aab 7031
9eae5e27 7032 ilk_set_pipeconf(new_crtc_state);
29407aab 7033
e44c84a1 7034 crtc->active = true;
8664281b 7035
e44c84a1 7036 intel_encoders_pre_enable(state, crtc);
f67a559d 7037
502d8714 7038 if (new_crtc_state->has_pch_encoder) {
fff367c7
DV
7039 /* Note: FDI PLL enabling _must_ be done before we enable the
7040 * cpu pipes, hence this is separate from all the other fdi/pch
7041 * enabling. */
9eae5e27 7042 ilk_fdi_pll_enable(new_crtc_state);
46b6f814
DV
7043 } else {
7044 assert_fdi_tx_disabled(dev_priv, pipe);
7045 assert_fdi_rx_disabled(dev_priv, pipe);
7046 }
f67a559d 7047
9eae5e27 7048 ilk_pfit_enable(new_crtc_state);
f67a559d 7049
9c54c0dd
JB
7050 /*
7051 * On ILK+ LUT must be loaded before the pipe is running but with
7052 * clocks enabled
7053 */
502d8714
VS
7054 intel_color_load_luts(new_crtc_state);
7055 intel_color_commit(new_crtc_state);
73a116be 7056 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7057 intel_disable_primary_plane(new_crtc_state);
9c54c0dd 7058
7a8fdb1f 7059 if (dev_priv->display.initial_watermarks)
e44c84a1 7060 dev_priv->display.initial_watermarks(state, crtc);
502d8714 7061 intel_enable_pipe(new_crtc_state);
f67a559d 7062
502d8714 7063 if (new_crtc_state->has_pch_encoder)
9eae5e27 7064 ilk_pch_enable(state, new_crtc_state);
c98e9dcf 7065
502d8714 7066 intel_crtc_vblank_on(new_crtc_state);
f9b61ff6 7067
e44c84a1 7068 intel_encoders_enable(state, crtc);
61b77ddd 7069
6e266956 7070 if (HAS_PCH_CPT(dev_priv))
c684fb44 7071 cpt_verify_modeset(dev_priv, pipe);
37ca8d4c 7072
ea80a661
VS
7073 /*
7074 * Must wait for vblank to avoid spurious PCH FIFO underruns.
7075 * And a second vblank wait is needed at least on ILK with
7076 * some interlaced HDMI modes. Let's do the double wait always
7077 * in case there are more corner cases we don't know about.
7078 */
502d8714 7079 if (new_crtc_state->has_pch_encoder) {
ea80a661 7080 intel_wait_for_vblank(dev_priv, pipe);
0f0f74bc 7081 intel_wait_for_vblank(dev_priv, pipe);
ea80a661 7082 }
b2c0593a 7083 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 7084 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
7085}
7086
42db64ef
PZ
7087/* IPS only exists on ULT machines and is tied to pipe A. */
7088static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
7089{
50a0bc90 7090 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
7091}
7092
ed69cd40
ID
7093static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
7094 enum pipe pipe, bool apply)
7095{
dc008bf0 7096 u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
ed69cd40
ID
7097 u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
7098
7099 if (apply)
7100 val |= mask;
7101 else
7102 val &= ~mask;
7103
dc008bf0 7104 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
ed69cd40
ID
7105}
7106
c3cc39c5
MK
7107static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
7108{
7109 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7110 enum pipe pipe = crtc->pipe;
ba3f4d0a 7111 u32 val;
c3cc39c5 7112
443d5e39 7113 val = MBUS_DBOX_A_CREDIT(2);
30fcc338
RV
7114
7115 if (INTEL_GEN(dev_priv) >= 12) {
7116 val |= MBUS_DBOX_BW_CREDIT(2);
7117 val |= MBUS_DBOX_B_CREDIT(12);
7118 } else {
7119 val |= MBUS_DBOX_BW_CREDIT(1);
7120 val |= MBUS_DBOX_B_CREDIT(8);
7121 }
c3cc39c5 7122
dc008bf0 7123 intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
c3cc39c5
MK
7124}
7125
6dcde047
VS
7126static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
7127{
7128 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7129 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7130
7131 intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7132 HSW_LINETIME(crtc_state->linetime) |
7133 HSW_IPS_LINETIME(crtc_state->ips_linetime));
7134}
7135
cc7a4cff
VS
7136static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7137{
7138 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7139 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7140 i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7141 u32 val;
7142
dc008bf0 7143 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
7144 val &= ~HSW_FRAME_START_DELAY_MASK;
7145 val |= HSW_FRAME_START_DELAY(0);
dc008bf0 7146 intel_de_write(dev_priv, reg, val);
cc7a4cff
VS
7147}
7148
4e3cdb45
MN
7149static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
7150 const struct intel_crtc_state *crtc_state)
7151{
7152 struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
7153 struct intel_crtc_state *master_crtc_state;
7154 struct drm_connector_state *conn_state;
7155 struct drm_connector *conn;
7156 struct intel_encoder *encoder = NULL;
7157 int i;
7158
7159 if (crtc_state->bigjoiner_slave)
7160 master = crtc_state->bigjoiner_linked_crtc;
7161
7162 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
7163
7164 for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
7165 if (conn_state->crtc != &master->base)
7166 continue;
7167
7168 encoder = to_intel_encoder(conn_state->best_encoder);
7169 break;
7170 }
7171
7172 if (!crtc_state->bigjoiner_slave) {
7173 /* need to enable VDSC, which we skipped in pre-enable */
7174 intel_dsc_enable(encoder, crtc_state);
7175 } else {
7176 /*
7177 * Enable sequence steps 1-7 on bigjoiner master
7178 */
7179 intel_encoders_pre_pll_enable(state, master);
7180 intel_enable_shared_dpll(master_crtc_state);
7181 intel_encoders_pre_enable(state, master);
7182
7183 /* and DSC on slave */
7184 intel_dsc_enable(NULL, crtc_state);
7185 }
7186}
7187
1e98f88c
LDM
7188static void hsw_crtc_enable(struct intel_atomic_state *state,
7189 struct intel_crtc *crtc)
4f771f10 7190{
7451a074
VS
7191 const struct intel_crtc_state *new_crtc_state =
7192 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7193 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7194 enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
502d8714 7195 enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
ed69cd40 7196 bool psl_clkgate_wa;
4f771f10 7197
e57291c2 7198 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4f771f10
PZ
7199 return;
7200
4e3cdb45
MN
7201 if (!new_crtc_state->bigjoiner) {
7202 intel_encoders_pre_pll_enable(state, crtc);
df8ad70c 7203
4e3cdb45
MN
7204 if (new_crtc_state->shared_dpll)
7205 intel_enable_shared_dpll(new_crtc_state);
c8af5274 7206
4e3cdb45
MN
7207 intel_encoders_pre_enable(state, crtc);
7208 } else {
7209 icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
7210 }
4d1de975 7211
502d8714 7212 intel_set_pipe_src_size(new_crtc_state);
4e3cdb45
MN
7213 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7214 bdw_set_pipemisc(new_crtc_state);
229fca97 7215
4e3cdb45
MN
7216 if (!new_crtc_state->bigjoiner_slave || !transcoder_is_dsi(cpu_transcoder)) {
7217 if (!transcoder_is_dsi(cpu_transcoder))
7218 intel_set_transcoder_timings(new_crtc_state);
ebb69c95 7219
4e3cdb45
MN
7220 if (cpu_transcoder != TRANSCODER_EDP &&
7221 !transcoder_is_dsi(cpu_transcoder))
7222 intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7223 new_crtc_state->pixel_multiplier - 1);
7224
7225 if (new_crtc_state->has_pch_encoder)
7226 intel_cpu_transcoder_set_m_n(new_crtc_state,
7227 &new_crtc_state->fdi_m_n, NULL);
229fca97 7228
502d8714 7229 hsw_set_frame_start_delay(new_crtc_state);
cc7a4cff 7230 }
4d1de975 7231
4e3cdb45
MN
7232 if (!transcoder_is_dsi(cpu_transcoder))
7233 hsw_set_pipeconf(new_crtc_state);
229fca97 7234
e44c84a1 7235 crtc->active = true;
8664281b 7236
ed69cd40
ID
7237 /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7238 psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
502d8714 7239 new_crtc_state->pch_pfit.enabled;
ed69cd40
ID
7240 if (psl_clkgate_wa)
7241 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7242
6315b5d3 7243 if (INTEL_GEN(dev_priv) >= 9)
f6df4d46 7244 skl_pfit_enable(new_crtc_state);
ff6d9f55 7245 else
9eae5e27 7246 ilk_pfit_enable(new_crtc_state);
4f771f10
PZ
7247
7248 /*
7249 * On ILK+ LUT must be loaded before the pipe is running but with
7250 * clocks enabled
7251 */
502d8714
VS
7252 intel_color_load_luts(new_crtc_state);
7253 intel_color_commit(new_crtc_state);
73a116be
VS
7254 /* update DSPCNTR to configure gamma/csc for pipe bottom color */
7255 if (INTEL_GEN(dev_priv) < 9)
502d8714 7256 intel_disable_primary_plane(new_crtc_state);
4f771f10 7257
6dcde047
VS
7258 hsw_set_linetime_wm(new_crtc_state);
7259
d1622119 7260 if (INTEL_GEN(dev_priv) >= 11)
e44c84a1 7261 icl_set_pipe_chicken(crtc);
e16a3750 7262
7a8fdb1f 7263 if (dev_priv->display.initial_watermarks)
e44c84a1 7264 dev_priv->display.initial_watermarks(state, crtc);
4d1de975 7265
c3cc39c5 7266 if (INTEL_GEN(dev_priv) >= 11)
e44c84a1 7267 icl_pipe_mbus_enable(crtc);
c3cc39c5 7268
4e3cdb45
MN
7269 if (new_crtc_state->bigjoiner_slave) {
7270 trace_intel_pipe_enable(crtc);
7271 intel_crtc_vblank_on(new_crtc_state);
7272 }
7273
e44c84a1 7274 intel_encoders_enable(state, crtc);
4f771f10 7275
ed69cd40
ID
7276 if (psl_clkgate_wa) {
7277 intel_wait_for_vblank(dev_priv, pipe);
7278 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7279 }
7280
e4916946
PZ
7281 /* If we change the relative order between pipe/planes enabling, we need
7282 * to change the workaround. */
502d8714 7283 hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
772c2a51 7284 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
7285 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7286 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 7287 }
4f771f10
PZ
7288}
7289
9eae5e27 7290void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
3f8dce3a 7291{
2225f3c6 7292 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
b2562712
ML
7293 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7294 enum pipe pipe = crtc->pipe;
3f8dce3a
DV
7295
7296 /* To avoid upsetting the power well on haswell only disable the pfit if
7297 * it's in use. The hw state code will make sure we get this right. */
eac9c585
VS
7298 if (!old_crtc_state->pch_pfit.enabled)
7299 return;
7300
7301 intel_de_write(dev_priv, PF_CTL(pipe), 0);
7302 intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7303 intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
3f8dce3a
DV
7304}
7305
9eae5e27
LDM
7306static void ilk_crtc_disable(struct intel_atomic_state *state,
7307 struct intel_crtc *crtc)
6be4a607 7308{
7451a074
VS
7309 const struct intel_crtc_state *old_crtc_state =
7310 intel_atomic_get_old_crtc_state(state, crtc);
e44c84a1
VS
7311 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7312 enum pipe pipe = crtc->pipe;
b52eb4dc 7313
b2c0593a
VS
7314 /*
7315 * Sometimes spurious CPU pipe underruns happen when the
7316 * pipe is already disabled, but FDI RX/TX is still enabled.
7317 * Happens at least with VGA+HDMI cloning. Suppress them.
7318 */
2b5b6312
VS
7319 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7320 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 7321
e44c84a1 7322 intel_encoders_disable(state, crtc);
ea9d758d 7323
f5271ee5 7324 intel_crtc_vblank_off(old_crtc_state);
f9b61ff6 7325
4972f70a 7326 intel_disable_pipe(old_crtc_state);
32f9d658 7327
9eae5e27 7328 ilk_pfit_disable(old_crtc_state);
2c07245f 7329
6f405638 7330 if (old_crtc_state->has_pch_encoder)
9eae5e27 7331 ilk_fdi_disable(crtc);
5a74f70a 7332
e44c84a1 7333 intel_encoders_post_disable(state, crtc);
2c07245f 7334
6f405638 7335 if (old_crtc_state->has_pch_encoder) {
9eae5e27 7336 ilk_disable_pch_transcoder(dev_priv, pipe);
6be4a607 7337
6e266956 7338 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
7339 i915_reg_t reg;
7340 u32 temp;
7341
d925c59a
DV
7342 /* disable TRANS_DP_CTL */
7343 reg = TRANS_DP_CTL(pipe);
dc008bf0 7344 temp = intel_de_read(dev_priv, reg);
d925c59a
DV
7345 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7346 TRANS_DP_PORT_SEL_MASK);
7347 temp |= TRANS_DP_PORT_SEL_NONE;
dc008bf0 7348 intel_de_write(dev_priv, reg, temp);
d925c59a
DV
7349
7350 /* disable DPLL_SEL */
dc008bf0 7351 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
11887397 7352 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
dc008bf0 7353 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
9db4a9c7 7354 }
e3421a18 7355
9eae5e27 7356 ilk_fdi_pll_disable(crtc);
d925c59a 7357 }
81b088ca 7358
b2c0593a 7359 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 7360 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 7361}
1b3c7a47 7362
1e98f88c
LDM
7363static void hsw_crtc_disable(struct intel_atomic_state *state,
7364 struct intel_crtc *crtc)
ee7b9f93 7365{
773b4b54
VS
7366 /*
7367 * FIXME collapse everything to one hook.
7368 * Need care with mst->ddi interactions.
7369 */
e44c84a1 7370 intel_encoders_disable(state, crtc);
e44c84a1 7371 intel_encoders_post_disable(state, crtc);
4f771f10
PZ
7372}
7373
b2562712 7374static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
2dd24552 7375{
2225f3c6 7376 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2562712 7377 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2dd24552 7378
b2562712 7379 if (!crtc_state->gmch_pfit.control)
2dd24552
JB
7380 return;
7381
2dd24552 7382 /*
c0b03411
DV
7383 * The panel fitter should only be adjusted whilst the pipe is disabled,
7384 * according to register description and PRM.
2dd24552 7385 */
e57291c2
PB
7386 drm_WARN_ON(&dev_priv->drm,
7387 intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
b104e8b2 7388 assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
2dd24552 7389
dc008bf0
JN
7390 intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7391 crtc_state->gmch_pfit.pgm_ratios);
7392 intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
5a80c45c
DV
7393
7394 /* Border color in case we don't scale up to the full screen. Black by
7395 * default, change to something else for debugging. */
dc008bf0 7396 intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
2dd24552
JB
7397}
7398
358633e7
MR
7399bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7400{
7401 if (phy == PHY_NONE)
7402 return false;
aefaa1f4
MR
7403 else if (IS_ROCKETLAKE(dev_priv))
7404 return phy <= PHY_D;
24ea098b 7405 else if (IS_JSL_EHL(dev_priv))
358633e7 7406 return phy <= PHY_C;
aefaa1f4 7407 else if (INTEL_GEN(dev_priv) >= 11)
358633e7 7408 return phy <= PHY_B;
aefaa1f4
MR
7409 else
7410 return false;
358633e7
MR
7411}
7412
358633e7
MR
7413bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7414{
aefaa1f4
MR
7415 if (IS_ROCKETLAKE(dev_priv))
7416 return false;
7417 else if (INTEL_GEN(dev_priv) >= 12)
5c719708 7418 return phy >= PHY_D && phy <= PHY_I;
24ea098b 7419 else if (INTEL_GEN(dev_priv) >= 11 && !IS_JSL_EHL(dev_priv))
358633e7 7420 return phy >= PHY_C && phy <= PHY_F;
aefaa1f4
MR
7421 else
7422 return false;
358633e7
MR
7423}
7424
7425enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7426{
1d8ca002
VS
7427 if (IS_ROCKETLAKE(i915) && port >= PORT_TC1)
7428 return PHY_C + port - PORT_TC1;
24ea098b 7429 else if (IS_JSL_EHL(i915) && port == PORT_D)
358633e7
MR
7430 return PHY_A;
7431
1d8ca002 7432 return PHY_A + port - PORT_A;
358633e7
MR
7433}
7434
ac213c1b
PZ
7435enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7436{
358633e7 7437 if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
320c670c 7438 return TC_PORT_NONE;
ac213c1b 7439
6c8337da 7440 if (INTEL_GEN(dev_priv) >= 12)
1d8ca002
VS
7441 return TC_PORT_1 + port - PORT_TC1;
7442 else
7443 return TC_PORT_1 + port - PORT_C;
ac213c1b
PZ
7444}
7445
79f255a0 7446enum intel_display_power_domain intel_port_to_power_domain(enum port port)
d05410f9
DA
7447{
7448 switch (port) {
7449 case PORT_A:
6331a704 7450 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 7451 case PORT_B:
6331a704 7452 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 7453 case PORT_C:
6331a704 7454 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 7455 case PORT_D:
6331a704 7456 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 7457 case PORT_E:
6331a704 7458 return POWER_DOMAIN_PORT_DDI_E_LANES;
9787e835
RV
7459 case PORT_F:
7460 return POWER_DOMAIN_PORT_DDI_F_LANES;
eb8de23c
KA
7461 case PORT_G:
7462 return POWER_DOMAIN_PORT_DDI_G_LANES;
07c9b088
VS
7463 case PORT_H:
7464 return POWER_DOMAIN_PORT_DDI_H_LANES;
7465 case PORT_I:
7466 return POWER_DOMAIN_PORT_DDI_I_LANES;
d05410f9 7467 default:
b9fec167 7468 MISSING_CASE(port);
d05410f9
DA
7469 return POWER_DOMAIN_PORT_OTHER;
7470 }
7471}
7472
337837ac
ID
7473enum intel_display_power_domain
7474intel_aux_power_domain(struct intel_digital_port *dig_port)
7475{
dd7239c5 7476 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
d8fe2ab6 7477 enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
dd7239c5 7478
d8fe2ab6 7479 if (intel_phy_is_tc(dev_priv, phy) &&
dd7239c5
ID
7480 dig_port->tc_mode == TC_PORT_TBT_ALT) {
7481 switch (dig_port->aux_ch) {
7482 case AUX_CH_C:
8a84bacb 7483 return POWER_DOMAIN_AUX_C_TBT;
dd7239c5 7484 case AUX_CH_D:
8a84bacb 7485 return POWER_DOMAIN_AUX_D_TBT;
dd7239c5 7486 case AUX_CH_E:
8a84bacb 7487 return POWER_DOMAIN_AUX_E_TBT;
dd7239c5 7488 case AUX_CH_F:
8a84bacb 7489 return POWER_DOMAIN_AUX_F_TBT;
eb8de23c
KA
7490 case AUX_CH_G:
7491 return POWER_DOMAIN_AUX_G_TBT;
244f2e9c
VS
7492 case AUX_CH_H:
7493 return POWER_DOMAIN_AUX_H_TBT;
7494 case AUX_CH_I:
7495 return POWER_DOMAIN_AUX_I_TBT;
dd7239c5
ID
7496 default:
7497 MISSING_CASE(dig_port->aux_ch);
8a84bacb 7498 return POWER_DOMAIN_AUX_C_TBT;
dd7239c5
ID
7499 }
7500 }
7501
dba6b0b4
JRS
7502 return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
7503}
7504
7505/*
7506 * Converts aux_ch to power_domain without caring about TBT ports for that use
7507 * intel_aux_power_domain()
7508 */
7509enum intel_display_power_domain
7510intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
7511{
7512 switch (aux_ch) {
337837ac
ID
7513 case AUX_CH_A:
7514 return POWER_DOMAIN_AUX_A;
7515 case AUX_CH_B:
7516 return POWER_DOMAIN_AUX_B;
7517 case AUX_CH_C:
7518 return POWER_DOMAIN_AUX_C;
7519 case AUX_CH_D:
7520 return POWER_DOMAIN_AUX_D;
7521 case AUX_CH_E:
7522 return POWER_DOMAIN_AUX_E;
7523 case AUX_CH_F:
7524 return POWER_DOMAIN_AUX_F;
eb8de23c
KA
7525 case AUX_CH_G:
7526 return POWER_DOMAIN_AUX_G;
244f2e9c
VS
7527 case AUX_CH_H:
7528 return POWER_DOMAIN_AUX_H;
7529 case AUX_CH_I:
7530 return POWER_DOMAIN_AUX_I;
337837ac 7531 default:
dba6b0b4 7532 MISSING_CASE(aux_ch);
337837ac
ID
7533 return POWER_DOMAIN_AUX_A;
7534 }
7535}
7536
afe0c21b 7537static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
77d22dca 7538{
2225f3c6 7539 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
afe0c21b 7540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
74bff5f9 7541 struct drm_encoder *encoder;
855e0d68 7542 enum pipe pipe = crtc->pipe;
d8fc70b7 7543 u64 mask;
74bff5f9 7544 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 7545
1326a92c 7546 if (!crtc_state->hw.active)
292b990e
ML
7547 return 0;
7548
17bd6e66
ID
7549 mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7550 mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
7551 if (crtc_state->pch_pfit.enabled ||
7552 crtc_state->pch_pfit.force_thru)
d8fc70b7 7553 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
77d22dca 7554
afe0c21b 7555 drm_for_each_encoder_mask(encoder, &dev_priv->drm,
2225f3c6 7556 crtc_state->uapi.encoder_mask) {
74bff5f9
ML
7557 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7558
79f255a0 7559 mask |= BIT_ULL(intel_encoder->power_domain);
74bff5f9 7560 }
319be8ae 7561
37255d8d 7562 if (HAS_DDI(dev_priv) && crtc_state->has_audio)
17bd6e66 7563 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
37255d8d 7564
15e7ec29 7565 if (crtc_state->shared_dpll)
08d8e170 7566 mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
15e7ec29 7567
8a029c11
MN
7568 if (crtc_state->dsc.compression_enable)
7569 mask |= BIT_ULL(intel_dsc_power_domain(crtc_state));
7570
77d22dca
ID
7571 return mask;
7572}
7573
d2d15016 7574static u64
afe0c21b 7575modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
77d22dca 7576{
2225f3c6 7577 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
855e0d68 7578 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
292b990e 7579 enum intel_display_power_domain domain;
d8fc70b7 7580 u64 domains, new_domains, old_domains;
77d22dca 7581
855e0d68
ML
7582 old_domains = crtc->enabled_power_domains;
7583 crtc->enabled_power_domains = new_domains =
afe0c21b 7584 get_crtc_power_domains(crtc_state);
77d22dca 7585
5a21b665 7586 domains = new_domains & ~old_domains;
292b990e
ML
7587
7588 for_each_power_domain(domain, domains)
7589 intel_display_power_get(dev_priv, domain);
7590
5a21b665 7591 return old_domains & ~new_domains;
292b990e
ML
7592}
7593
7594static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
d8fc70b7 7595 u64 domains)
292b990e
ML
7596{
7597 enum intel_display_power_domain domain;
7598
7599 for_each_power_domain(domain, domains)
0e6e0be4 7600 intel_display_power_put_unchecked(dev_priv, domain);
292b990e 7601}
77d22dca 7602
7451a074
VS
7603static void valleyview_crtc_enable(struct intel_atomic_state *state,
7604 struct intel_crtc *crtc)
adafdc6f 7605{
7451a074
VS
7606 const struct intel_crtc_state *new_crtc_state =
7607 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7608 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7609 enum pipe pipe = crtc->pipe;
adafdc6f 7610
e57291c2 7611 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7ff89ca2 7612 return;
adafdc6f 7613
502d8714
VS
7614 if (intel_crtc_has_dp_encoder(new_crtc_state))
7615 intel_dp_set_m_n(new_crtc_state, M1_N1);
b2045352 7616
e7fc3f90 7617 intel_set_transcoder_timings(new_crtc_state);
502d8714 7618 intel_set_pipe_src_size(new_crtc_state);
b2045352 7619
7ff89ca2 7620 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
dc008bf0
JN
7621 intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7622 intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
560a7ae4
DL
7623 }
7624
502d8714 7625 i9xx_set_pipeconf(new_crtc_state);
560a7ae4 7626
e44c84a1 7627 crtc->active = true;
92891e45 7628
7ff89ca2 7629 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 7630
e44c84a1 7631 intel_encoders_pre_pll_enable(state, crtc);
5f199dfa 7632
7ff89ca2 7633 if (IS_CHERRYVIEW(dev_priv)) {
502d8714
VS
7634 chv_prepare_pll(crtc, new_crtc_state);
7635 chv_enable_pll(crtc, new_crtc_state);
7ff89ca2 7636 } else {
502d8714
VS
7637 vlv_prepare_pll(crtc, new_crtc_state);
7638 vlv_enable_pll(crtc, new_crtc_state);
5f199dfa
VS
7639 }
7640
e44c84a1 7641 intel_encoders_pre_enable(state, crtc);
5f199dfa 7642
502d8714 7643 i9xx_pfit_enable(new_crtc_state);
89b3c3c7 7644
502d8714
VS
7645 intel_color_load_luts(new_crtc_state);
7646 intel_color_commit(new_crtc_state);
73a116be 7647 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7648 intel_disable_primary_plane(new_crtc_state);
89b3c3c7 7649
e44c84a1 7650 dev_priv->display.initial_watermarks(state, crtc);
502d8714 7651 intel_enable_pipe(new_crtc_state);
7ff89ca2 7652
502d8714 7653 intel_crtc_vblank_on(new_crtc_state);
89b3c3c7 7654
e44c84a1 7655 intel_encoders_enable(state, crtc);
89b3c3c7
ACO
7656}
7657
b2354c78 7658static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
2b73001e 7659{
2225f3c6 7660 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
b2354c78 7661 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
83d7c81f 7662
dc008bf0
JN
7663 intel_de_write(dev_priv, FP0(crtc->pipe),
7664 crtc_state->dpll_hw_state.fp0);
7665 intel_de_write(dev_priv, FP1(crtc->pipe),
7666 crtc_state->dpll_hw_state.fp1);
2b73001e
VS
7667}
7668
7451a074
VS
7669static void i9xx_crtc_enable(struct intel_atomic_state *state,
7670 struct intel_crtc *crtc)
2b73001e 7671{
7451a074
VS
7672 const struct intel_crtc_state *new_crtc_state =
7673 intel_atomic_get_new_crtc_state(state, crtc);
e44c84a1
VS
7674 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7675 enum pipe pipe = crtc->pipe;
2b73001e 7676
e57291c2 7677 if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7ff89ca2 7678 return;
2b73001e 7679
502d8714 7680 i9xx_set_pll_dividers(new_crtc_state);
2b73001e 7681
502d8714
VS
7682 if (intel_crtc_has_dp_encoder(new_crtc_state))
7683 intel_dp_set_m_n(new_crtc_state, M1_N1);
83d7c81f 7684
e7fc3f90 7685 intel_set_transcoder_timings(new_crtc_state);
502d8714 7686 intel_set_pipe_src_size(new_crtc_state);
2b73001e 7687
502d8714 7688 i9xx_set_pipeconf(new_crtc_state);
f8437dd1 7689
e44c84a1 7690 crtc->active = true;
5f199dfa 7691
cf819eff 7692 if (!IS_GEN(dev_priv, 2))
7ff89ca2 7693 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5f199dfa 7694
e44c84a1 7695 intel_encoders_pre_enable(state, crtc);
f8437dd1 7696
502d8714 7697 i9xx_enable_pll(crtc, new_crtc_state);
f8437dd1 7698
502d8714 7699 i9xx_pfit_enable(new_crtc_state);
f8437dd1 7700
502d8714
VS
7701 intel_color_load_luts(new_crtc_state);
7702 intel_color_commit(new_crtc_state);
73a116be 7703 /* update DSPCNTR to configure gamma for pipe bottom color */
502d8714 7704 intel_disable_primary_plane(new_crtc_state);
f8437dd1 7705
7a8fdb1f 7706 if (dev_priv->display.initial_watermarks)
e44c84a1 7707 dev_priv->display.initial_watermarks(state, crtc);
04548cba 7708 else
e44c84a1 7709 intel_update_watermarks(crtc);
502d8714 7710 intel_enable_pipe(new_crtc_state);
f8437dd1 7711
502d8714 7712 intel_crtc_vblank_on(new_crtc_state);
f8437dd1 7713
e44c84a1 7714 intel_encoders_enable(state, crtc);
f6a7d395
VS
7715
7716 /* prevents spurious underruns */
7717 if (IS_GEN(dev_priv, 2))
7718 intel_wait_for_vblank(dev_priv, pipe);
7ff89ca2 7719}
f8437dd1 7720
b2562712 7721static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7ff89ca2 7722{
2225f3c6 7723 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
b2562712 7724 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f8437dd1 7725
b2562712 7726 if (!old_crtc_state->gmch_pfit.control)
f8437dd1 7727 return;
f8437dd1 7728
b104e8b2 7729 assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7ff89ca2 7730
cd49f818 7731 drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
dc008bf0
JN
7732 intel_de_read(dev_priv, PFIT_CONTROL));
7733 intel_de_write(dev_priv, PFIT_CONTROL, 0);
f8437dd1
VK
7734}
7735
7451a074
VS
7736static void i9xx_crtc_disable(struct intel_atomic_state *state,
7737 struct intel_crtc *crtc)
f8437dd1 7738{
7451a074
VS
7739 struct intel_crtc_state *old_crtc_state =
7740 intel_atomic_get_old_crtc_state(state, crtc);
e44c84a1
VS
7741 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7742 enum pipe pipe = crtc->pipe;
d66a2194 7743
d66a2194 7744 /*
7ff89ca2
VS
7745 * On gen2 planes are double buffered but the pipe isn't, so we must
7746 * wait for planes to fully turn off before disabling the pipe.
d66a2194 7747 */
cf819eff 7748 if (IS_GEN(dev_priv, 2))
7ff89ca2 7749 intel_wait_for_vblank(dev_priv, pipe);
d66a2194 7750
e44c84a1 7751 intel_encoders_disable(state, crtc);
d66a2194 7752
f5271ee5 7753 intel_crtc_vblank_off(old_crtc_state);
d66a2194 7754
4972f70a 7755 intel_disable_pipe(old_crtc_state);
d66a2194 7756
b2562712 7757 i9xx_pfit_disable(old_crtc_state);
89b3c3c7 7758
e44c84a1 7759 intel_encoders_post_disable(state, crtc);
d66a2194 7760
6f405638 7761 if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7ff89ca2
VS
7762 if (IS_CHERRYVIEW(dev_priv))
7763 chv_disable_pll(dev_priv, pipe);
7764 else if (IS_VALLEYVIEW(dev_priv))
7765 vlv_disable_pll(dev_priv, pipe);
7766 else
b2354c78 7767 i9xx_disable_pll(old_crtc_state);
7ff89ca2 7768 }
c2e001ef 7769
e44c84a1 7770 intel_encoders_post_pll_disable(state, crtc);
89b3c3c7 7771
cf819eff 7772 if (!IS_GEN(dev_priv, 2))
7ff89ca2 7773 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
ff32c54e
VS
7774
7775 if (!dev_priv->display.initial_watermarks)
e44c84a1 7776 intel_update_watermarks(crtc);
2ee0da16
VS
7777
7778 /* clock the pipe down to 640x480@60 to potentially save power */
7779 if (IS_I830(dev_priv))
7780 i830_enable_pipe(dev_priv, pipe);
f8437dd1
VK
7781}
7782
56273062 7783static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
da1d0e26 7784 struct drm_modeset_acquire_ctx *ctx)
f8437dd1 7785{
7ff89ca2 7786 struct intel_encoder *encoder;
56273062 7787 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
c457d9cf
VS
7788 struct intel_bw_state *bw_state =
7789 to_intel_bw_state(dev_priv->bw_obj.state);
1965de63 7790 struct intel_cdclk_state *cdclk_state =
28a30b45 7791 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
3cf43cdc
VS
7792 struct intel_dbuf_state *dbuf_state =
7793 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
1e460bf9 7794 struct intel_crtc_state *crtc_state =
56273062 7795 to_intel_crtc_state(crtc->base.state);
7ff89ca2 7796 enum intel_display_power_domain domain;
b1e01595 7797 struct intel_plane *plane;
7ff89ca2 7798 struct drm_atomic_state *state;
1e460bf9 7799 struct intel_crtc_state *temp_crtc_state;
56273062
VS
7800 enum pipe pipe = crtc->pipe;
7801 u64 domains;
7ff89ca2 7802 int ret;
f8437dd1 7803
56273062 7804 if (!crtc_state->hw.active)
7ff89ca2 7805 return;
a8ca4934 7806
56273062 7807 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
b1e01595
VS
7808 const struct intel_plane_state *plane_state =
7809 to_intel_plane_state(plane->base.state);
709e05c3 7810
f90a85e7 7811 if (plane_state->uapi.visible)
56273062 7812 intel_plane_disable_noatomic(crtc, plane);
7ff89ca2 7813 }
5d96d8af 7814
56273062 7815 state = drm_atomic_state_alloc(&dev_priv->drm);
7ff89ca2 7816 if (!state) {
cd49f818
WK
7817 drm_dbg_kms(&dev_priv->drm,
7818 "failed to disable [CRTC:%d:%s], out of memory",
7819 crtc->base.base.id, crtc->base.name);
1c3f7700 7820 return;
7ff89ca2 7821 }
9f7eb31a 7822
da1d0e26 7823 state->acquire_ctx = ctx;
ea61791e 7824
7ff89ca2 7825 /* Everything's already locked, -EDEADLK can't happen. */
56273062
VS
7826 temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7827 ret = drm_atomic_add_affected_connectors(state, &crtc->base);
9f7eb31a 7828
e57291c2 7829 drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
5d96d8af 7830
56273062 7831 dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
4a806558 7832
0853695c 7833 drm_atomic_state_put(state);
842e0307 7834
cd49f818
WK
7835 drm_dbg_kms(&dev_priv->drm,
7836 "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7837 crtc->base.base.id, crtc->base.name);
56273062
VS
7838
7839 crtc->active = false;
7840 crtc->base.enabled = false;
7841
e57291c2
PB
7842 drm_WARN_ON(&dev_priv->drm,
7843 drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
56273062
VS
7844 crtc_state->uapi.active = false;
7845 crtc_state->uapi.connector_mask = 0;
7846 crtc_state->uapi.encoder_mask = 0;
58d124ea
ML
7847 intel_crtc_free_hw_state(crtc_state);
7848 memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
842e0307 7849
56273062 7850 for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
842e0307
ML
7851 encoder->base.crtc = NULL;
7852
56273062
VS
7853 intel_fbc_disable(crtc);
7854 intel_update_watermarks(crtc);
7855 intel_disable_shared_dpll(crtc_state);
b17d48e2 7856
56273062 7857 domains = crtc->enabled_power_domains;
b17d48e2 7858 for_each_power_domain(domain, domains)
0e6e0be4 7859 intel_display_power_put_unchecked(dev_priv, domain);
56273062 7860 crtc->enabled_power_domains = 0;
565602d7 7861
56273062 7862 dev_priv->active_pipes &= ~BIT(pipe);
1965de63
VS
7863 cdclk_state->min_cdclk[pipe] = 0;
7864 cdclk_state->min_voltage_level[pipe] = 0;
0c2d5512 7865 cdclk_state->active_pipes &= ~BIT(pipe);
c457d9cf 7866
3cf43cdc
VS
7867 dbuf_state->active_pipes &= ~BIT(pipe);
7868
56273062
VS
7869 bw_state->data_rate[pipe] = 0;
7870 bw_state->num_active_planes[pipe] = 0;
b17d48e2
ML
7871}
7872
6b72d486
ML
7873/*
7874 * turn all crtc's off, but do not adjust state
7875 * This has to be paired with a call to intel_modeset_setup_hw_state.
7876 */
70e0bd74 7877int intel_display_suspend(struct drm_device *dev)
ee7b9f93 7878{
e2c8b870 7879 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 7880 struct drm_atomic_state *state;
e2c8b870 7881 int ret;
70e0bd74 7882
e2c8b870
ML
7883 state = drm_atomic_helper_suspend(dev);
7884 ret = PTR_ERR_OR_ZERO(state);
70e0bd74 7885 if (ret)
cd49f818
WK
7886 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7887 ret);
e2c8b870
ML
7888 else
7889 dev_priv->modeset_restore_state = state;
70e0bd74 7890 return ret;
ee7b9f93
JB
7891}
7892
ea5b213a 7893void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 7894{
4ef69c7a 7895 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 7896
ea5b213a
CW
7897 drm_encoder_cleanup(encoder);
7898 kfree(intel_encoder);
7e7d76c3
JB
7899}
7900
0a91ca29
DV
7901/* Cross check the actual hw state with our own modeset state tracking (and it's
7902 * internal consistency). */
3b4bf24d 7903static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
749d98b8 7904 struct drm_connector_state *conn_state)
79e53945 7905{
749d98b8 7906 struct intel_connector *connector = to_intel_connector(conn_state->connector);
cd49f818 7907 struct drm_i915_private *i915 = to_i915(connector->base.dev);
35dd3c64 7908
cd49f818
WK
7909 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7910 connector->base.base.id, connector->base.name);
35dd3c64 7911
0a91ca29 7912 if (connector->get_hw_state(connector)) {
fa7edcd2 7913 struct intel_encoder *encoder = intel_attached_encoder(connector);
0a91ca29 7914
749d98b8 7915 I915_STATE_WARN(!crtc_state,
35dd3c64 7916 "connector enabled without attached crtc\n");
0a91ca29 7917
749d98b8 7918 if (!crtc_state)
35dd3c64
ML
7919 return;
7920
1326a92c
ML
7921 I915_STATE_WARN(!crtc_state->hw.active,
7922 "connector is active, but attached crtc isn't\n");
35dd3c64 7923
e85376cb 7924 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
7925 return;
7926
e85376cb 7927 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
7928 "atomic encoder doesn't match attached encoder\n");
7929
e85376cb 7930 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
7931 "attached encoder crtc differs from connector crtc\n");
7932 } else {
1326a92c
ML
7933 I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7934 "attached crtc is active, but connector isn't\n");
749d98b8 7935 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
35dd3c64 7936 "best encoder set without crtc!\n");
0a91ca29 7937 }
79e53945
JB
7938}
7939
6d293983 7940static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7941{
1326a92c 7942 if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
6d293983 7943 return crtc_state->fdi_lanes;
d272ddfa
VS
7944
7945 return 0;
7946}
7947
9eae5e27
LDM
7948static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7949 struct intel_crtc_state *pipe_config)
1857e1da 7950{
8652744b 7951 struct drm_i915_private *dev_priv = to_i915(dev);
2225f3c6 7952 struct drm_atomic_state *state = pipe_config->uapi.state;
6d293983
ACO
7953 struct intel_crtc *other_crtc;
7954 struct intel_crtc_state *other_crtc_state;
7955
cd49f818
WK
7956 drm_dbg_kms(&dev_priv->drm,
7957 "checking fdi config on pipe %c, lanes %i\n",
7958 pipe_name(pipe), pipe_config->fdi_lanes);
1857e1da 7959 if (pipe_config->fdi_lanes > 4) {
cd49f818
WK
7960 drm_dbg_kms(&dev_priv->drm,
7961 "invalid fdi lane config on pipe %c: %i lanes\n",
7962 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7963 return -EINVAL;
1857e1da
DV
7964 }
7965
8652744b 7966 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da 7967 if (pipe_config->fdi_lanes > 2) {
cd49f818
WK
7968 drm_dbg_kms(&dev_priv->drm,
7969 "only 2 lanes on haswell, required: %i lanes\n",
7970 pipe_config->fdi_lanes);
6d293983 7971 return -EINVAL;
1857e1da 7972 } else {
6d293983 7973 return 0;
1857e1da
DV
7974 }
7975 }
7976
24977870 7977 if (INTEL_NUM_PIPES(dev_priv) == 2)
6d293983 7978 return 0;
1857e1da
DV
7979
7980 /* Ivybridge 3 pipe is really complicated */
7981 switch (pipe) {
7982 case PIPE_A:
6d293983 7983 return 0;
1857e1da 7984 case PIPE_B:
6d293983
ACO
7985 if (pipe_config->fdi_lanes <= 2)
7986 return 0;
7987
b91eb5cc 7988 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7989 other_crtc_state =
7990 intel_atomic_get_crtc_state(state, other_crtc);
7991 if (IS_ERR(other_crtc_state))
7992 return PTR_ERR(other_crtc_state);
7993
7994 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
cd49f818
WK
7995 drm_dbg_kms(&dev_priv->drm,
7996 "invalid shared fdi lane config on pipe %c: %i lanes\n",
7997 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7998 return -EINVAL;
1857e1da 7999 }
6d293983 8000 return 0;
1857e1da 8001 case PIPE_C:
251cc67c 8002 if (pipe_config->fdi_lanes > 2) {
cd49f818
WK
8003 drm_dbg_kms(&dev_priv->drm,
8004 "only 2 lanes on pipe %c: required %i lanes\n",
8005 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 8006 return -EINVAL;
251cc67c 8007 }
6d293983 8008
b91eb5cc 8009 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
8010 other_crtc_state =
8011 intel_atomic_get_crtc_state(state, other_crtc);
8012 if (IS_ERR(other_crtc_state))
8013 return PTR_ERR(other_crtc_state);
8014
8015 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
cd49f818
WK
8016 drm_dbg_kms(&dev_priv->drm,
8017 "fdi link B uses too many lanes to enable link C\n");
6d293983 8018 return -EINVAL;
1857e1da 8019 }
6d293983 8020 return 0;
1857e1da
DV
8021 default:
8022 BUG();
8023 }
8024}
8025
e29c22c0 8026#define RETRY 1
9eae5e27
LDM
8027static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
8028 struct intel_crtc_state *pipe_config)
877d48d5 8029{
1857e1da 8030 struct drm_device *dev = intel_crtc->base.dev;
cd49f818 8031 struct drm_i915_private *i915 = to_i915(dev);
1326a92c 8032 const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
6d293983
ACO
8033 int lane, link_bw, fdi_dotclock, ret;
8034 bool needs_recompute = false;
877d48d5 8035
e29c22c0 8036retry:
877d48d5
DV
8037 /* FDI is a binary signal running at ~2.7GHz, encoding
8038 * each output octet as 10 bits. The actual frequency
8039 * is stored as a divider into a 100MHz clock, and the
8040 * mode pixel clock is stored in units of 1KHz.
8041 * Hence the bw of each lane in terms of the mode signal
8042 * is:
8043 */
cd49f818 8044 link_bw = intel_fdi_link_freq(i915, pipe_config);
877d48d5 8045
241bfc38 8046 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 8047
9eae5e27
LDM
8048 lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
8049 pipe_config->pipe_bpp);
877d48d5
DV
8050
8051 pipe_config->fdi_lanes = lane;
8052
2bd89a07 8053 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
ed06efb8 8054 link_bw, &pipe_config->fdi_m_n, false, false);
1857e1da 8055
9eae5e27 8056 ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
8e2b4dff
VS
8057 if (ret == -EDEADLK)
8058 return ret;
8059
6d293983 8060 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0 8061 pipe_config->pipe_bpp -= 2*3;
cd49f818
WK
8062 drm_dbg_kms(&i915->drm,
8063 "fdi link bw constraint, reducing pipe bpp to %i\n",
8064 pipe_config->pipe_bpp);
7ff89ca2
VS
8065 needs_recompute = true;
8066 pipe_config->bw_constrained = true;
257a7ffc 8067
7ff89ca2 8068 goto retry;
257a7ffc 8069 }
79e53945 8070
7ff89ca2
VS
8071 if (needs_recompute)
8072 return RETRY;
e70236a8 8073
7ff89ca2 8074 return ret;
e70236a8
JB
8075}
8076
24f28450 8077bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
e70236a8 8078{
2225f3c6 8079 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
24f28450
ML
8080 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8081
8082 /* IPS only exists on ULT machines and is tied to pipe A. */
8083 if (!hsw_crtc_supports_ips(crtc))
6e644626
VS
8084 return false;
8085
8a25c4be 8086 if (!dev_priv->params.enable_ips)
7ff89ca2 8087 return false;
e70236a8 8088
24f28450
ML
8089 if (crtc_state->pipe_bpp > 24)
8090 return false;
1b1d2716 8091
65cd2b3f 8092 /*
7ff89ca2
VS
8093 * We compare against max which means we must take
8094 * the increased cdclk requirement into account when
8095 * calculating the new cdclk.
8096 *
8097 * Should measure whether using a lower cdclk w/o IPS
e70236a8 8098 */
24f28450
ML
8099 if (IS_BROADWELL(dev_priv) &&
8100 crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
8101 return false;
8102
8103 return true;
e70236a8 8104}
79e53945 8105
28a30b45 8106static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7ff89ca2 8107{
24f28450 8108 struct drm_i915_private *dev_priv =
2225f3c6 8109 to_i915(crtc_state->uapi.crtc->dev);
28a30b45 8110 struct intel_atomic_state *state =
2225f3c6 8111 to_intel_atomic_state(crtc_state->uapi.state);
28a30b45
VS
8112
8113 crtc_state->ips_enabled = false;
24f28450
ML
8114
8115 if (!hsw_crtc_state_ips_capable(crtc_state))
28a30b45 8116 return 0;
24f28450 8117
a8ebf607
JRS
8118 /*
8119 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
8120 * enabled and disabled dynamically based on package C states,
8121 * user space can't make reliable use of the CRCs, so let's just
8122 * completely disable it.
8123 */
8124 if (crtc_state->crc_enabled)
28a30b45 8125 return 0;
24f28450 8126
adbe5c5c
ML
8127 /* IPS should be fine as long as at least one plane is enabled. */
8128 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
28a30b45 8129 return 0;
34edce2f 8130
28a30b45
VS
8131 if (IS_BROADWELL(dev_priv)) {
8132 const struct intel_cdclk_state *cdclk_state;
24f28450 8133
28a30b45
VS
8134 cdclk_state = intel_atomic_get_cdclk_state(state);
8135 if (IS_ERR(cdclk_state))
8136 return PTR_ERR(cdclk_state);
8137
8138 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
8139 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
8140 return 0;
8141 }
8142
8143 crtc_state->ips_enabled = true;
8144
8145 return 0;
34edce2f
VS
8146}
8147
7ff89ca2 8148static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
34edce2f 8149{
7ff89ca2 8150 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
34edce2f 8151
7ff89ca2 8152 /* GDG double wide on either pipe, otherwise pipe A only */
c56b89f1 8153 return INTEL_GEN(dev_priv) < 4 &&
7ff89ca2 8154 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
34edce2f
VS
8155}
8156
eac9c585 8157static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
ceb99320 8158{
bafcdad6 8159 u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
35dd95b4 8160 unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
ceb99320
VS
8161
8162 /*
8163 * We only use IF-ID interlacing. If we ever use
8164 * PF-ID we'll need to adjust the pixel_rate here.
8165 */
8166
eac9c585
VS
8167 if (!crtc_state->pch_pfit.enabled)
8168 return pixel_rate;
ceb99320 8169
eac9c585
VS
8170 pipe_w = crtc_state->pipe_src_w;
8171 pipe_h = crtc_state->pipe_src_h;
ceb99320 8172
35dd95b4
VS
8173 pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
8174 pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
8175
eac9c585
VS
8176 if (pipe_w < pfit_w)
8177 pipe_w = pfit_w;
8178 if (pipe_h < pfit_h)
8179 pipe_h = pfit_h;
ceb99320 8180
eac9c585
VS
8181 if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
8182 !pfit_w || !pfit_h))
8183 return pixel_rate;
ceb99320 8184
eac9c585
VS
8185 return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
8186 pfit_w * pfit_h);
ceb99320
VS
8187}
8188
33574ec9
VS
8189static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
8190 const struct drm_display_mode *timings)
8191{
8192 mode->hdisplay = timings->crtc_hdisplay;
8193 mode->htotal = timings->crtc_htotal;
8194 mode->hsync_start = timings->crtc_hsync_start;
8195 mode->hsync_end = timings->crtc_hsync_end;
8196
8197 mode->vdisplay = timings->crtc_vdisplay;
8198 mode->vtotal = timings->crtc_vtotal;
8199 mode->vsync_start = timings->crtc_vsync_start;
8200 mode->vsync_end = timings->crtc_vsync_end;
8201
8202 mode->flags = timings->flags;
8203 mode->type = DRM_MODE_TYPE_DRIVER;
8204
8205 mode->clock = timings->crtc_clock;
8206
8207 drm_mode_set_name(mode);
8208}
8209
7ff89ca2 8210static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
34edce2f 8211{
2225f3c6 8212 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
34edce2f 8213
b2ae318a 8214 if (HAS_GMCH(dev_priv))
7ff89ca2
VS
8215 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
8216 crtc_state->pixel_rate =
bafcdad6 8217 crtc_state->hw.pipe_mode.crtc_clock;
7ff89ca2
VS
8218 else
8219 crtc_state->pixel_rate =
8220 ilk_pipe_pixel_rate(crtc_state);
8221}
34edce2f 8222
c42773b6
VS
8223static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
8224{
8225 struct drm_display_mode *mode = &crtc_state->hw.mode;
bafcdad6 8226 struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
c42773b6
VS
8227 struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8228
bafcdad6
ML
8229 drm_mode_copy(pipe_mode, adjusted_mode);
8230
0385ecea
MN
8231 if (crtc_state->bigjoiner) {
8232 /*
8233 * transcoder is programmed to the full mode,
8234 * but pipe timings are half of the transcoder mode
8235 */
8236 pipe_mode->crtc_hdisplay /= 2;
8237 pipe_mode->crtc_hblank_start /= 2;
8238 pipe_mode->crtc_hblank_end /= 2;
8239 pipe_mode->crtc_hsync_start /= 2;
8240 pipe_mode->crtc_hsync_end /= 2;
8241 pipe_mode->crtc_htotal /= 2;
8242 pipe_mode->crtc_clock /= 2;
8243 }
8244
bafcdad6 8245 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
c42773b6
VS
8246 intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
8247
8248 intel_crtc_compute_pixel_rate(crtc_state);
8249
8250 drm_mode_copy(mode, adjusted_mode);
0385ecea 8251 mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
c42773b6
VS
8252 mode->vdisplay = crtc_state->pipe_src_h;
8253}
8254
65c1ed30
MN
8255static void intel_encoder_get_config(struct intel_encoder *encoder,
8256 struct intel_crtc_state *crtc_state)
8257{
8258 encoder->get_config(encoder, crtc_state);
c42773b6
VS
8259
8260 intel_crtc_readout_derived_state(crtc_state);
65c1ed30
MN
8261}
8262
7ff89ca2
VS
8263static int intel_crtc_compute_config(struct intel_crtc *crtc,
8264 struct intel_crtc_state *pipe_config)
8265{
d2daff2c 8266 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bafcdad6 8267 struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
7ff89ca2 8268 int clock_limit = dev_priv->max_dotclk_freq;
34edce2f 8269
bafcdad6 8270 drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
19f65a3d
ML
8271
8272 /* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
8273 if (pipe_config->bigjoiner) {
8274 pipe_mode->crtc_clock /= 2;
8275 pipe_mode->crtc_hdisplay /= 2;
8276 pipe_mode->crtc_hblank_start /= 2;
8277 pipe_mode->crtc_hblank_end /= 2;
8278 pipe_mode->crtc_hsync_start /= 2;
8279 pipe_mode->crtc_hsync_end /= 2;
8280 pipe_mode->crtc_htotal /= 2;
8281 pipe_config->pipe_src_w /= 2;
8282 }
8283
bafcdad6
ML
8284 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
8285
7ff89ca2
VS
8286 if (INTEL_GEN(dev_priv) < 4) {
8287 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
34edce2f 8288
7ff89ca2
VS
8289 /*
8290 * Enable double wide mode when the dot clock
8291 * is > 90% of the (display) core speed.
8292 */
8293 if (intel_crtc_supports_double_wide(crtc) &&
bafcdad6 8294 pipe_mode->crtc_clock > clock_limit) {
7ff89ca2
VS
8295 clock_limit = dev_priv->max_dotclk_freq;
8296 pipe_config->double_wide = true;
8297 }
34edce2f
VS
8298 }
8299
bafcdad6 8300 if (pipe_mode->crtc_clock > clock_limit) {
cd49f818
WK
8301 drm_dbg_kms(&dev_priv->drm,
8302 "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
bafcdad6 8303 pipe_mode->crtc_clock, clock_limit,
cd49f818 8304 yesno(pipe_config->double_wide));
7ff89ca2
VS
8305 return -EINVAL;
8306 }
34edce2f 8307
8c79f844
SS
8308 if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8309 pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
1326a92c 8310 pipe_config->hw.ctm) {
25edf915
SS
8311 /*
8312 * There is only one pipe CSC unit per pipe, and we need that
8313 * for output conversion from RGB->YCBCR. So if CTM is already
8314 * applied we can't support YCBCR420 output.
8315 */
cd49f818
WK
8316 drm_dbg_kms(&dev_priv->drm,
8317 "YCBCR420 and CTM together are not possible\n");
25edf915
SS
8318 return -EINVAL;
8319 }
8320
7ff89ca2
VS
8321 /*
8322 * Pipe horizontal size must be even in:
8323 * - DVO ganged mode
8324 * - LVDS dual channel mode
8325 * - Double wide pipe
8326 */
0574bd88
VS
8327 if (pipe_config->pipe_src_w & 1) {
8328 if (pipe_config->double_wide) {
cd49f818
WK
8329 drm_dbg_kms(&dev_priv->drm,
8330 "Odd pipe source width not supported with double wide pipe\n");
0574bd88
VS
8331 return -EINVAL;
8332 }
8333
8334 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
d2daff2c 8335 intel_is_dual_link_lvds(dev_priv)) {
cd49f818
WK
8336 drm_dbg_kms(&dev_priv->drm,
8337 "Odd pipe source width not supported with dual link LVDS\n");
0574bd88
VS
8338 return -EINVAL;
8339 }
8340 }
34edce2f 8341
7ff89ca2
VS
8342 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
8343 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8344 */
8345 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
bafcdad6 8346 pipe_mode->crtc_hsync_start == pipe_mode->crtc_hdisplay)
7ff89ca2 8347 return -EINVAL;
34edce2f 8348
7ff89ca2 8349 intel_crtc_compute_pixel_rate(pipe_config);
34edce2f 8350
7ff89ca2 8351 if (pipe_config->has_pch_encoder)
9eae5e27 8352 return ilk_fdi_compute_config(crtc, pipe_config);
34edce2f 8353
7ff89ca2 8354 return 0;
34edce2f
VS
8355}
8356
2c07245f 8357static void
ba3f4d0a 8358intel_reduce_m_n_ratio(u32 *num, u32 *den)
2c07245f 8359{
a65851af
VS
8360 while (*num > DATA_LINK_M_N_MASK ||
8361 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
8362 *num >>= 1;
8363 *den >>= 1;
8364 }
8365}
8366
a65851af 8367static void compute_m_n(unsigned int m, unsigned int n,
ba3f4d0a 8368 u32 *ret_m, u32 *ret_n,
53ca2edc 8369 bool constant_n)
a65851af 8370{
9a86cda0 8371 /*
53ca2edc
LS
8372 * Several DP dongles in particular seem to be fussy about
8373 * too large link M/N values. Give N value as 0x8000 that
8374 * should be acceptable by specific devices. 0x8000 is the
8375 * specified fixed N value for asynchronous clock mode,
8376 * which the devices expect also in synchronous clock mode.
9a86cda0 8377 */
53ca2edc 8378 if (constant_n)
b22960b8 8379 *ret_n = DP_LINK_CONSTANT_N_VALUE;
53ca2edc
LS
8380 else
8381 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
9a86cda0 8382
d492a29d 8383 *ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
a65851af
VS
8384 intel_reduce_m_n_ratio(ret_m, ret_n);
8385}
8386
e69d0bc1 8387void
a4a15777 8388intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
e69d0bc1 8389 int pixel_clock, int link_clock,
b31e85ed 8390 struct intel_link_m_n *m_n,
ed06efb8 8391 bool constant_n, bool fec_enable)
2c07245f 8392{
ed06efb8
ML
8393 u32 data_clock = bits_per_pixel * pixel_clock;
8394
8395 if (fec_enable)
8396 data_clock = intel_dp_mode_to_fec_clock(data_clock);
a65851af 8397
ed06efb8
ML
8398 m_n->tu = 64;
8399 compute_m_n(data_clock,
a65851af 8400 link_clock * nlanes * 8,
b31e85ed 8401 &m_n->gmch_m, &m_n->gmch_n,
53ca2edc 8402 constant_n);
a65851af
VS
8403
8404 compute_m_n(pixel_clock, link_clock,
b31e85ed 8405 &m_n->link_m, &m_n->link_n,
53ca2edc 8406 constant_n);
2c07245f
ZW
8407}
8408
064bd628
JN
8409static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8410{
8411 /*
8412 * There may be no VBT; and if the BIOS enabled SSC we can
8413 * just keep using it to avoid unnecessary flicker. Whereas if the
8414 * BIOS isn't using it, don't assume it will work even if the VBT
8415 * indicates as much.
8416 */
8417 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
dc008bf0
JN
8418 bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8419 PCH_DREF_CONTROL) &
064bd628
JN
8420 DREF_SSC1_ENABLE;
8421
8422 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
cd49f818
WK
8423 drm_dbg_kms(&dev_priv->drm,
8424 "SSC %s by BIOS, overriding VBT which says %s\n",
8425 enableddisabled(bios_lvds_use_ssc),
8426 enableddisabled(dev_priv->vbt.lvds_use_ssc));
064bd628
JN
8427 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8428 }
8429 }
8430}
8431
81b55ef1 8432static bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
a7615030 8433{
8a25c4be
JN
8434 if (dev_priv->params.panel_use_ssc >= 0)
8435 return dev_priv->params.panel_use_ssc != 0;
41aa3448 8436 return dev_priv->vbt.lvds_use_ssc
435793df 8437 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
8438}
8439
ba3f4d0a 8440static u32 pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 8441{
7df00d7a 8442 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 8443}
f47709a9 8444
ba3f4d0a 8445static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
7429e9d4
DV
8446{
8447 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
8448}
8449
f47709a9 8450static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 8451 struct intel_crtc_state *crtc_state,
9e2c8475 8452 struct dpll *reduced_clock)
a7516a05 8453{
9b1e14f4 8454 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
8455 u32 fp, fp2 = 0;
8456
9b1e14f4 8457 if (IS_PINEVIEW(dev_priv)) {
190f68c5 8458 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 8459 if (reduced_clock)
7429e9d4 8460 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 8461 } else {
190f68c5 8462 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 8463 if (reduced_clock)
7429e9d4 8464 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
8465 }
8466
190f68c5 8467 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 8468
2d84d2b3 8469 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 8470 reduced_clock) {
190f68c5 8471 crtc_state->dpll_hw_state.fp1 = fp2;
a7516a05 8472 } else {
190f68c5 8473 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
8474 }
8475}
8476
5e69f97f
CML
8477static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8478 pipe)
89b667f8
JB
8479{
8480 u32 reg_val;
8481
8482 /*
8483 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8484 * and set it to a reasonable value instead.
8485 */
ab3c759a 8486 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
8487 reg_val &= 0xffffff00;
8488 reg_val |= 0x00000030;
ab3c759a 8489 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 8490
ab3c759a 8491 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
ed58570f
ID
8492 reg_val &= 0x00ffffff;
8493 reg_val |= 0x8c000000;
ab3c759a 8494 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 8495
ab3c759a 8496 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 8497 reg_val &= 0xffffff00;
ab3c759a 8498 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 8499
ab3c759a 8500 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
8501 reg_val &= 0x00ffffff;
8502 reg_val |= 0xb0000000;
ab3c759a 8503 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
8504}
8505
4c354754
ML
8506static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8507 const struct intel_link_m_n *m_n)
b551842d 8508{
2225f3c6 8509 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4c354754
ML
8510 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8511 enum pipe pipe = crtc->pipe;
b551842d 8512
dc008bf0
JN
8513 intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8514 TU_SIZE(m_n->tu) | m_n->gmch_m);
8515 intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8516 intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8517 intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
8518}
8519
4207c8b9
ML
8520static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8521 enum transcoder transcoder)
8522{
8523 if (IS_HASWELL(dev_priv))
8524 return transcoder == TRANSCODER_EDP;
8525
8526 /*
8527 * Strictly speaking some registers are available before
8528 * gen7, but we only support DRRS on gen7+
8529 */
cf819eff 8530 return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
4207c8b9
ML
8531}
8532
4c354754
ML
8533static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8534 const struct intel_link_m_n *m_n,
8535 const struct intel_link_m_n *m2_n2)
b551842d 8536{
2225f3c6 8537 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6315b5d3 8538 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4c354754
ML
8539 enum pipe pipe = crtc->pipe;
8540 enum transcoder transcoder = crtc_state->cpu_transcoder;
b551842d 8541
6315b5d3 8542 if (INTEL_GEN(dev_priv) >= 5) {
dc008bf0
JN
8543 intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8544 TU_SIZE(m_n->tu) | m_n->gmch_m);
8545 intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8546 m_n->gmch_n);
8547 intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8548 m_n->link_m);
8549 intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8550 m_n->link_n);
4207c8b9
ML
8551 /*
8552 * M2_N2 registers are set only if DRRS is supported
8553 * (to make sure the registers are not unnecessarily accessed).
f769cd24 8554 */
4207c8b9
ML
8555 if (m2_n2 && crtc_state->has_drrs &&
8556 transcoder_has_m2_n2(dev_priv, transcoder)) {
dc008bf0
JN
8557 intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8558 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8559 intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8560 m2_n2->gmch_n);
8561 intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8562 m2_n2->link_m);
8563 intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8564 m2_n2->link_n);
f769cd24 8565 }
b551842d 8566 } else {
dc008bf0
JN
8567 intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8568 TU_SIZE(m_n->tu) | m_n->gmch_m);
8569 intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8570 intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8571 intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
8572 }
8573}
8574
4c354754 8575void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
03afc4a2 8576{
4c354754 8577 const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
cd49f818 8578 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
fe3cd48d
R
8579
8580 if (m_n == M1_N1) {
4c354754
ML
8581 dp_m_n = &crtc_state->dp_m_n;
8582 dp_m2_n2 = &crtc_state->dp_m2_n2;
fe3cd48d
R
8583 } else if (m_n == M2_N2) {
8584
8585 /*
8586 * M2_N2 registers are not supported. Hence m2_n2 divider value
8587 * needs to be programmed into M1_N1.
8588 */
4c354754 8589 dp_m_n = &crtc_state->dp_m2_n2;
fe3cd48d 8590 } else {
cd49f818 8591 drm_err(&i915->drm, "Unsupported divider value\n");
fe3cd48d
R
8592 return;
8593 }
8594
4c354754
ML
8595 if (crtc_state->has_pch_encoder)
8596 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
03afc4a2 8597 else
4c354754 8598 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
03afc4a2
DV
8599}
8600
251ac862
DV
8601static void vlv_compute_dpll(struct intel_crtc *crtc,
8602 struct intel_crtc_state *pipe_config)
bdd4b6a6 8603{
03ed5cbf 8604 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 8605 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
8606 if (crtc->pipe != PIPE_A)
8607 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 8608
cd2d34d9 8609 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 8610 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
8611 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8612 DPLL_EXT_BUFFER_ENABLE_VLV;
8613
03ed5cbf
VS
8614 pipe_config->dpll_hw_state.dpll_md =
8615 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8616}
bdd4b6a6 8617
03ed5cbf
VS
8618static void chv_compute_dpll(struct intel_crtc *crtc,
8619 struct intel_crtc_state *pipe_config)
8620{
8621 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 8622 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
8623 if (crtc->pipe != PIPE_A)
8624 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8625
cd2d34d9 8626 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 8627 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
8628 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8629
03ed5cbf
VS
8630 pipe_config->dpll_hw_state.dpll_md =
8631 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
DV
8632}
8633
d288f65f 8634static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8635 const struct intel_crtc_state *pipe_config)
a0c4da24 8636{
f47709a9 8637 struct drm_device *dev = crtc->base.dev;
fac5e23e 8638 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8639 enum pipe pipe = crtc->pipe;
bdd4b6a6 8640 u32 mdiv;
a0c4da24 8641 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 8642 u32 coreclk, reg_val;
a0c4da24 8643
cd2d34d9 8644 /* Enable Refclk */
dc008bf0
JN
8645 intel_de_write(dev_priv, DPLL(pipe),
8646 pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
cd2d34d9
VS
8647
8648 /* No need to actually set up the DPLL with DSI */
8649 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8650 return;
8651
221c7862 8652 vlv_dpio_get(dev_priv);
09153000 8653
d288f65f
VS
8654 bestn = pipe_config->dpll.n;
8655 bestm1 = pipe_config->dpll.m1;
8656 bestm2 = pipe_config->dpll.m2;
8657 bestp1 = pipe_config->dpll.p1;
8658 bestp2 = pipe_config->dpll.p2;
a0c4da24 8659
89b667f8
JB
8660 /* See eDP HDMI DPIO driver vbios notes doc */
8661
8662 /* PLL B needs special handling */
bdd4b6a6 8663 if (pipe == PIPE_B)
5e69f97f 8664 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
8665
8666 /* Set up Tx target for periodic Rcomp update */
ab3c759a 8667 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
8668
8669 /* Disable target IRef on PLL */
ab3c759a 8670 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 8671 reg_val &= 0x00ffffff;
ab3c759a 8672 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
8673
8674 /* Disable fast lock */
ab3c759a 8675 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
8676
8677 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
8678 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8679 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8680 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 8681 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
8682
8683 /*
8684 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8685 * but we don't support that).
8686 * Note: don't use the DAC post divider as it seems unstable.
8687 */
8688 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 8689 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 8690
a0c4da24 8691 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 8692 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 8693
89b667f8 8694 /* Set HBR and RBR LPF coefficients */
d288f65f 8695 if (pipe_config->port_clock == 162000 ||
92d54b07
ML
8696 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8697 intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
ab3c759a 8698 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 8699 0x009f0003);
89b667f8 8700 else
ab3c759a 8701 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
8702 0x00d0000f);
8703
37a5650b 8704 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 8705 /* Use SSC source */
bdd4b6a6 8706 if (pipe == PIPE_A)
ab3c759a 8707 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8708 0x0df40000);
8709 else
ab3c759a 8710 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8711 0x0df70000);
8712 } else { /* HDMI or VGA */
8713 /* Use bend source */
bdd4b6a6 8714 if (pipe == PIPE_A)
ab3c759a 8715 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8716 0x0df70000);
8717 else
ab3c759a 8718 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8719 0x0df40000);
8720 }
a0c4da24 8721
ab3c759a 8722 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 8723 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
92d54b07 8724 if (intel_crtc_has_dp_encoder(pipe_config))
89b667f8 8725 coreclk |= 0x01000000;
ab3c759a 8726 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 8727
ab3c759a 8728 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
221c7862
CW
8729
8730 vlv_dpio_put(dev_priv);
a0c4da24
JB
8731}
8732
d288f65f 8733static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8734 const struct intel_crtc_state *pipe_config)
9d556c99
CML
8735{
8736 struct drm_device *dev = crtc->base.dev;
fac5e23e 8737 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8738 enum pipe pipe = crtc->pipe;
9d556c99 8739 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 8740 u32 loopfilter, tribuf_calcntr;
9d556c99 8741 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 8742 u32 dpio_val;
9cbe40c1 8743 int vco;
9d556c99 8744
cd2d34d9 8745 /* Enable Refclk and SSC */
dc008bf0
JN
8746 intel_de_write(dev_priv, DPLL(pipe),
8747 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
cd2d34d9
VS
8748
8749 /* No need to actually set up the DPLL with DSI */
8750 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8751 return;
8752
d288f65f
VS
8753 bestn = pipe_config->dpll.n;
8754 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8755 bestm1 = pipe_config->dpll.m1;
8756 bestm2 = pipe_config->dpll.m2 >> 22;
8757 bestp1 = pipe_config->dpll.p1;
8758 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8759 vco = pipe_config->dpll.vco;
a945ce7e 8760 dpio_val = 0;
9cbe40c1 8761 loopfilter = 0;
9d556c99 8762
221c7862 8763 vlv_dpio_get(dev_priv);
9d556c99 8764
9d556c99
CML
8765 /* p1 and p2 divider */
8766 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8767 5 << DPIO_CHV_S1_DIV_SHIFT |
8768 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8769 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8770 1 << DPIO_CHV_K_DIV_SHIFT);
8771
8772 /* Feedback post-divider - m2 */
8773 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8774
8775 /* Feedback refclk divider - n and m1 */
8776 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8777 DPIO_CHV_M1_DIV_BY_2 |
8778 1 << DPIO_CHV_N_DIV_SHIFT);
8779
8780 /* M2 fraction division */
25a25dfc 8781 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8782
8783 /* M2 fraction division enable */
a945ce7e
VP
8784 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8785 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8786 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8787 if (bestm2_frac)
8788 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8789 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8790
de3a0fde
VP
8791 /* Program digital lock detect threshold */
8792 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8793 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8794 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8795 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8796 if (!bestm2_frac)
8797 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8798 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8799
9d556c99 8800 /* Loop filter */
9cbe40c1
VP
8801 if (vco == 5400000) {
8802 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8803 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8804 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8805 tribuf_calcntr = 0x9;
8806 } else if (vco <= 6200000) {
8807 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8808 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8809 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8810 tribuf_calcntr = 0x9;
8811 } else if (vco <= 6480000) {
8812 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8813 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8814 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8815 tribuf_calcntr = 0x8;
8816 } else {
8817 /* Not supported. Apply the same limits as in the max case */
8818 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8819 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8820 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8821 tribuf_calcntr = 0;
8822 }
9d556c99
CML
8823 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8824
968040b2 8825 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8826 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8827 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8828 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8829
9d556c99
CML
8830 /* AFC Recal */
8831 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8832 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8833 DPIO_AFC_RECAL);
8834
221c7862 8835 vlv_dpio_put(dev_priv);
9d556c99
CML
8836}
8837
d288f65f
VS
8838/**
8839 * vlv_force_pll_on - forcibly enable just the PLL
8840 * @dev_priv: i915 private structure
8841 * @pipe: pipe PLL to enable
8842 * @dpll: PLL configuration
8843 *
8844 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8845 * in cases where we need the PLL enabled even when @pipe is not going to
8846 * be enabled.
8847 */
30ad9814 8848int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8849 const struct dpll *dpll)
d288f65f 8850{
b91eb5cc 8851 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8852 struct intel_crtc_state *pipe_config;
8853
216383e9 8854 pipe_config = intel_crtc_state_alloc(crtc);
3f36b937
TU
8855 if (!pipe_config)
8856 return -ENOMEM;
8857
b104e8b2 8858 pipe_config->cpu_transcoder = (enum transcoder)pipe;
3f36b937
TU
8859 pipe_config->pixel_multiplier = 1;
8860 pipe_config->dpll = *dpll;
d288f65f 8861
30ad9814 8862 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8863 chv_compute_dpll(crtc, pipe_config);
8864 chv_prepare_pll(crtc, pipe_config);
8865 chv_enable_pll(crtc, pipe_config);
d288f65f 8866 } else {
3f36b937
TU
8867 vlv_compute_dpll(crtc, pipe_config);
8868 vlv_prepare_pll(crtc, pipe_config);
8869 vlv_enable_pll(crtc, pipe_config);
d288f65f 8870 }
3f36b937
TU
8871
8872 kfree(pipe_config);
8873
8874 return 0;
d288f65f
VS
8875}
8876
8877/**
8878 * vlv_force_pll_off - forcibly disable just the PLL
8879 * @dev_priv: i915 private structure
8880 * @pipe: pipe PLL to disable
8881 *
8882 * Disable the PLL for @pipe. To be used in cases where we need
8883 * the PLL enabled even when @pipe is not going to be enabled.
8884 */
30ad9814 8885void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8886{
30ad9814
VS
8887 if (IS_CHERRYVIEW(dev_priv))
8888 chv_disable_pll(dev_priv, pipe);
d288f65f 8889 else
30ad9814 8890 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8891}
8892
251ac862
DV
8893static void i9xx_compute_dpll(struct intel_crtc *crtc,
8894 struct intel_crtc_state *crtc_state,
9e2c8475 8895 struct dpll *reduced_clock)
eb1cbe48 8896{
9b1e14f4 8897 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8898 u32 dpll;
190f68c5 8899 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8900
190f68c5 8901 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8902
eb1cbe48
DV
8903 dpll = DPLL_VGA_MODE_DIS;
8904
2d84d2b3 8905 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
8906 dpll |= DPLLB_MODE_LVDS;
8907 else
8908 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8909
73f67aa8
JN
8910 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8911 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
190f68c5 8912 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8913 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8914 }
198a037f 8915
3d6e9ee0
VS
8916 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8917 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8918 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8919
37a5650b 8920 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8921 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
8922
8923 /* compute bitmask from p1 value */
9b1e14f4 8924 if (IS_PINEVIEW(dev_priv))
eb1cbe48
DV
8925 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8926 else {
8927 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8928 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
DV
8929 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8930 }
8931 switch (clock->p2) {
8932 case 5:
8933 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8934 break;
8935 case 7:
8936 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8937 break;
8938 case 10:
8939 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8940 break;
8941 case 14:
8942 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8943 break;
8944 }
9b1e14f4 8945 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
DV
8946 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8947
190f68c5 8948 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8949 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8950 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8951 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
8952 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8953 else
8954 dpll |= PLL_REF_INPUT_DREFCLK;
8955
8956 dpll |= DPLL_VCO_ENABLE;
190f68c5 8957 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8958
9b1e14f4 8959 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8960 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8961 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8962 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
8963 }
8964}
8965
251ac862
DV
8966static void i8xx_compute_dpll(struct intel_crtc *crtc,
8967 struct intel_crtc_state *crtc_state,
9e2c8475 8968 struct dpll *reduced_clock)
eb1cbe48 8969{
f47709a9 8970 struct drm_device *dev = crtc->base.dev;
fac5e23e 8971 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8972 u32 dpll;
190f68c5 8973 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8974
190f68c5 8975 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8976
eb1cbe48
DV
8977 dpll = DPLL_VGA_MODE_DIS;
8978
2d84d2b3 8979 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
8980 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8981 } else {
8982 if (clock->p1 == 2)
8983 dpll |= PLL_P1_DIVIDE_BY_TWO;
8984 else
8985 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8986 if (clock->p2 == 4)
8987 dpll |= PLL_P2_DIVIDE_BY_4;
8988 }
8989
171d1562
VS
8990 /*
8991 * Bspec:
8992 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8993 * (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8994 * GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8995 * Enable) must be set to “1” in both the DPLL A Control Register
8996 * (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8997 *
8998 * For simplicity We simply keep both bits always enabled in
8999 * both DPLLS. The spec says we should disable the DVO 2X clock
9000 * when not needed, but this seems to work fine in practice.
9001 */
9002 if (IS_I830(dev_priv) ||
50a0bc90 9003 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
9004 dpll |= DPLL_DVO_2X_MODE;
9005
2d84d2b3 9006 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 9007 intel_panel_use_ssc(dev_priv))
eb1cbe48
DV
9008 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
9009 else
9010 dpll |= PLL_REF_INPUT_DREFCLK;
9011
9012 dpll |= DPLL_VCO_ENABLE;
190f68c5 9013 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
9014}
9015
e7fc3f90 9016static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
b0e77b9c 9017{
2225f3c6 9018 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
44fe7f35
ML
9019 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9020 enum pipe pipe = crtc->pipe;
9021 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1326a92c 9022 const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
ba3f4d0a 9023 u32 crtc_vtotal, crtc_vblank_end;
1caea6e9 9024 int vsyncshift = 0;
4d8a62ea
DV
9025
9026 /* We need to be careful not to changed the adjusted mode, for otherwise
9027 * the hw state checker will get angry at the mismatch. */
9028 crtc_vtotal = adjusted_mode->crtc_vtotal;
9029 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 9030
609aeaca 9031 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 9032 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
9033 crtc_vtotal -= 1;
9034 crtc_vblank_end -= 1;
609aeaca 9035
44fe7f35 9036 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
609aeaca
VS
9037 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
9038 else
9039 vsyncshift = adjusted_mode->crtc_hsync_start -
9040 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
9041 if (vsyncshift < 0)
9042 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
9043 }
9044
6315b5d3 9045 if (INTEL_GEN(dev_priv) > 3)
dc008bf0
JN
9046 intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
9047 vsyncshift);
9048
9049 intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
9050 (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
9051 intel_de_write(dev_priv, HBLANK(cpu_transcoder),
9052 (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
9053 intel_de_write(dev_priv, HSYNC(cpu_transcoder),
9054 (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
9055
9056 intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
9057 (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
9058 intel_de_write(dev_priv, VBLANK(cpu_transcoder),
9059 (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
9060 intel_de_write(dev_priv, VSYNC(cpu_transcoder),
9061 (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
b0e77b9c 9062
b5e508d4
PZ
9063 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
9064 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
9065 * documented on the DDI_FUNC_CTL register description, EDP Input Select
9066 * bits. */
772c2a51 9067 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4 9068 (pipe == PIPE_B || pipe == PIPE_C))
dc008bf0
JN
9069 intel_de_write(dev_priv, VTOTAL(pipe),
9070 intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
b5e508d4 9071
bc58be60
JN
9072}
9073
44fe7f35 9074static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
bc58be60 9075{
2225f3c6 9076 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
44fe7f35
ML
9077 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9078 enum pipe pipe = crtc->pipe;
bc58be60 9079
b0e77b9c
PZ
9080 /* pipesrc controls the size that is scaled from, which should
9081 * always be the user's requested size.
9082 */
dc008bf0
JN
9083 intel_de_write(dev_priv, PIPESRC(pipe),
9084 ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
b0e77b9c
PZ
9085}
9086
60a02311
VS
9087static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
9088{
2225f3c6 9089 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
60a02311
VS
9090 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
9091
9092 if (IS_GEN(dev_priv, 2))
9093 return false;
9094
9095 if (INTEL_GEN(dev_priv) >= 9 ||
9096 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
dc008bf0 9097 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
60a02311 9098 else
dc008bf0 9099 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
60a02311
VS
9100}
9101
e7fc3f90
MN
9102static void intel_get_transcoder_timings(struct intel_crtc *crtc,
9103 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
9104{
9105 struct drm_device *dev = crtc->base.dev;
fac5e23e 9106 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80 9107 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
ba3f4d0a 9108 u32 tmp;
1bd1bd80 9109
dc008bf0 9110 tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
1326a92c
ML
9111 pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
9112 pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
3c23ed13
VK
9113
9114 if (!transcoder_is_dsi(cpu_transcoder)) {
dc008bf0 9115 tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
1326a92c 9116 pipe_config->hw.adjusted_mode.crtc_hblank_start =
3c23ed13 9117 (tmp & 0xffff) + 1;
1326a92c 9118 pipe_config->hw.adjusted_mode.crtc_hblank_end =
3c23ed13
VK
9119 ((tmp >> 16) & 0xffff) + 1;
9120 }
dc008bf0 9121 tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
1326a92c
ML
9122 pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
9123 pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 9124
dc008bf0 9125 tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
1326a92c
ML
9126 pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
9127 pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
3c23ed13
VK
9128
9129 if (!transcoder_is_dsi(cpu_transcoder)) {
dc008bf0 9130 tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
1326a92c 9131 pipe_config->hw.adjusted_mode.crtc_vblank_start =
3c23ed13 9132 (tmp & 0xffff) + 1;
1326a92c 9133 pipe_config->hw.adjusted_mode.crtc_vblank_end =
3c23ed13
VK
9134 ((tmp >> 16) & 0xffff) + 1;
9135 }
dc008bf0 9136 tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
1326a92c
ML
9137 pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
9138 pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 9139
60a02311 9140 if (intel_pipe_is_interlaced(pipe_config)) {
1326a92c
ML
9141 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
9142 pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
9143 pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 9144 }
bc58be60
JN
9145}
9146
9147static void intel_get_pipe_src_size(struct intel_crtc *crtc,
9148 struct intel_crtc_state *pipe_config)
9149{
9150 struct drm_device *dev = crtc->base.dev;
fac5e23e 9151 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 9152 u32 tmp;
1bd1bd80 9153
dc008bf0 9154 tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
37327abd
VS
9155 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
9156 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
9157}
9158
fdf73510 9159static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
84b046f3 9160{
2225f3c6 9161 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510 9162 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ba3f4d0a 9163 u32 pipeconf;
84b046f3 9164
9f11a9e4 9165 pipeconf = 0;
84b046f3 9166
e56134bc
VS
9167 /* we keep both pipes enabled on 830 */
9168 if (IS_I830(dev_priv))
dc008bf0 9169 pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 9170
fdf73510 9171 if (crtc_state->double_wide)
cf532bb2 9172 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 9173
ff9ce46e 9174 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
9175 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9176 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 9177 /* Bspec claims that we can't use dithering for 30bpp pipes. */
fdf73510 9178 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
ff9ce46e 9179 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 9180 PIPECONF_DITHER_TYPE_SP;
84b046f3 9181
fdf73510 9182 switch (crtc_state->pipe_bpp) {
ff9ce46e
DV
9183 case 18:
9184 pipeconf |= PIPECONF_6BPC;
9185 break;
9186 case 24:
9187 pipeconf |= PIPECONF_8BPC;
9188 break;
9189 case 30:
9190 pipeconf |= PIPECONF_10BPC;
9191 break;
9192 default:
9193 /* Case prevented by intel_choose_pipe_bpp_dither. */
9194 BUG();
84b046f3
DV
9195 }
9196 }
9197
1326a92c 9198 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 9199 if (INTEL_GEN(dev_priv) < 4 ||
fdf73510 9200 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
efc2cfff
VS
9201 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
9202 else
9203 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
27b680f9 9204 } else {
84b046f3 9205 pipeconf |= PIPECONF_PROGRESSIVE;
27b680f9 9206 }
84b046f3 9207
920a14b2 9208 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
fdf73510 9209 crtc_state->limited_color_range)
9f11a9e4 9210 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 9211
9d5441de
VS
9212 pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
9213
cc7a4cff
VS
9214 pipeconf |= PIPECONF_FRAME_START_DELAY(0);
9215
dc008bf0
JN
9216 intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
9217 intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
84b046f3
DV
9218}
9219
81c97f52
ACO
9220static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
9221 struct intel_crtc_state *crtc_state)
9222{
9223 struct drm_device *dev = crtc->base.dev;
fac5e23e 9224 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9225 const struct intel_limit *limit;
81c97f52
ACO
9226 int refclk = 48000;
9227
9228 memset(&crtc_state->dpll_hw_state, 0,
9229 sizeof(crtc_state->dpll_hw_state));
9230
2d84d2b3 9231 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
9232 if (intel_panel_use_ssc(dev_priv)) {
9233 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9234 drm_dbg_kms(&dev_priv->drm,
9235 "using SSC reference clock of %d kHz\n",
9236 refclk);
81c97f52
ACO
9237 }
9238
9239 limit = &intel_limits_i8xx_lvds;
2d84d2b3 9240 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
9241 limit = &intel_limits_i8xx_dvo;
9242 } else {
9243 limit = &intel_limits_i8xx_dac;
9244 }
9245
9246 if (!crtc_state->clock_set &&
9247 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9248 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9249 drm_err(&dev_priv->drm,
9250 "Couldn't find PLL settings for mode!\n");
81c97f52
ACO
9251 return -EINVAL;
9252 }
9253
9254 i8xx_compute_dpll(crtc, crtc_state, NULL);
9255
9256 return 0;
9257}
9258
19ec6693
ACO
9259static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
9260 struct intel_crtc_state *crtc_state)
9261{
d2daff2c 9262 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1b6f4958 9263 const struct intel_limit *limit;
19ec6693
ACO
9264 int refclk = 96000;
9265
9266 memset(&crtc_state->dpll_hw_state, 0,
9267 sizeof(crtc_state->dpll_hw_state));
9268
2d84d2b3 9269 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
9270 if (intel_panel_use_ssc(dev_priv)) {
9271 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9272 drm_dbg_kms(&dev_priv->drm,
9273 "using SSC reference clock of %d kHz\n",
9274 refclk);
19ec6693
ACO
9275 }
9276
d2daff2c 9277 if (intel_is_dual_link_lvds(dev_priv))
19ec6693
ACO
9278 limit = &intel_limits_g4x_dual_channel_lvds;
9279 else
9280 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
9281 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9282 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 9283 limit = &intel_limits_g4x_hdmi;
2d84d2b3 9284 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
9285 limit = &intel_limits_g4x_sdvo;
9286 } else {
9287 /* The option is for other outputs */
9288 limit = &intel_limits_i9xx_sdvo;
9289 }
9290
9291 if (!crtc_state->clock_set &&
9292 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9293 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9294 drm_err(&dev_priv->drm,
9295 "Couldn't find PLL settings for mode!\n");
19ec6693
ACO
9296 return -EINVAL;
9297 }
9298
9299 i9xx_compute_dpll(crtc, crtc_state, NULL);
9300
9301 return 0;
9302}
9303
70e8aa21
ACO
9304static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9305 struct intel_crtc_state *crtc_state)
9306{
9307 struct drm_device *dev = crtc->base.dev;
fac5e23e 9308 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9309 const struct intel_limit *limit;
70e8aa21
ACO
9310 int refclk = 96000;
9311
9312 memset(&crtc_state->dpll_hw_state, 0,
9313 sizeof(crtc_state->dpll_hw_state));
9314
2d84d2b3 9315 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
9316 if (intel_panel_use_ssc(dev_priv)) {
9317 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9318 drm_dbg_kms(&dev_priv->drm,
9319 "using SSC reference clock of %d kHz\n",
9320 refclk);
70e8aa21
ACO
9321 }
9322
1d218220 9323 limit = &pnv_limits_lvds;
70e8aa21 9324 } else {
1d218220 9325 limit = &pnv_limits_sdvo;
70e8aa21
ACO
9326 }
9327
9328 if (!crtc_state->clock_set &&
9329 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9330 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9331 drm_err(&dev_priv->drm,
9332 "Couldn't find PLL settings for mode!\n");
70e8aa21
ACO
9333 return -EINVAL;
9334 }
9335
9336 i9xx_compute_dpll(crtc, crtc_state, NULL);
9337
9338 return 0;
9339}
9340
190f68c5
ACO
9341static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9342 struct intel_crtc_state *crtc_state)
79e53945 9343{
c7653199 9344 struct drm_device *dev = crtc->base.dev;
fac5e23e 9345 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 9346 const struct intel_limit *limit;
81c97f52 9347 int refclk = 96000;
79e53945 9348
dd3cd74a
ACO
9349 memset(&crtc_state->dpll_hw_state, 0,
9350 sizeof(crtc_state->dpll_hw_state));
9351
2d84d2b3 9352 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
9353 if (intel_panel_use_ssc(dev_priv)) {
9354 refclk = dev_priv->vbt.lvds_ssc_freq;
cd49f818
WK
9355 drm_dbg_kms(&dev_priv->drm,
9356 "using SSC reference clock of %d kHz\n",
9357 refclk);
70e8aa21 9358 }
43565a06 9359
70e8aa21
ACO
9360 limit = &intel_limits_i9xx_lvds;
9361 } else {
9362 limit = &intel_limits_i9xx_sdvo;
81c97f52 9363 }
79e53945 9364
70e8aa21
ACO
9365 if (!crtc_state->clock_set &&
9366 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9367 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
9368 drm_err(&dev_priv->drm,
9369 "Couldn't find PLL settings for mode!\n");
70e8aa21 9370 return -EINVAL;
f47709a9 9371 }
7026d4ac 9372
81c97f52 9373 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 9374
c8f7a0db 9375 return 0;
f564048e
EA
9376}
9377
65b3d6a9
ACO
9378static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9379 struct intel_crtc_state *crtc_state)
9380{
9381 int refclk = 100000;
1b6f4958 9382 const struct intel_limit *limit = &intel_limits_chv;
cd49f818 9383 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
65b3d6a9
ACO
9384
9385 memset(&crtc_state->dpll_hw_state, 0,
9386 sizeof(crtc_state->dpll_hw_state));
9387
65b3d6a9
ACO
9388 if (!crtc_state->clock_set &&
9389 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9390 refclk, NULL, &crtc_state->dpll)) {
cd49f818 9391 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
65b3d6a9
ACO
9392 return -EINVAL;
9393 }
9394
9395 chv_compute_dpll(crtc, crtc_state);
9396
9397 return 0;
9398}
9399
9400static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9401 struct intel_crtc_state *crtc_state)
9402{
9403 int refclk = 100000;
1b6f4958 9404 const struct intel_limit *limit = &intel_limits_vlv;
cd49f818 9405 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
65b3d6a9
ACO
9406
9407 memset(&crtc_state->dpll_hw_state, 0,
9408 sizeof(crtc_state->dpll_hw_state));
9409
65b3d6a9
ACO
9410 if (!crtc_state->clock_set &&
9411 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9412 refclk, NULL, &crtc_state->dpll)) {
cd49f818 9413 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
65b3d6a9
ACO
9414 return -EINVAL;
9415 }
9416
9417 vlv_compute_dpll(crtc, crtc_state);
9418
9419 return 0;
9420}
9421
b7c8093f
VS
9422static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9423{
9424 if (IS_I830(dev_priv))
9425 return false;
9426
9427 return INTEL_GEN(dev_priv) >= 4 ||
9428 IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9429}
9430
eac9c585 9431static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
2fa2fe9a 9432{
eac9c585 9433 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6315b5d3 9434 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
ba3f4d0a 9435 u32 tmp;
2fa2fe9a 9436
b7c8093f 9437 if (!i9xx_has_pfit(dev_priv))
dc9e7dec
VS
9438 return;
9439
dc008bf0 9440 tmp = intel_de_read(dev_priv, PFIT_CONTROL);
06922821
DV
9441 if (!(tmp & PFIT_ENABLE))
9442 return;
2fa2fe9a 9443
06922821 9444 /* Check whether the pfit is attached to our pipe. */
6315b5d3 9445 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
DV
9446 if (crtc->pipe != PIPE_B)
9447 return;
2fa2fe9a
DV
9448 } else {
9449 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9450 return;
9451 }
9452
eac9c585
VS
9453 crtc_state->gmch_pfit.control = tmp;
9454 crtc_state->gmch_pfit.pgm_ratios =
9455 intel_de_read(dev_priv, PFIT_PGM_RATIOS);
2fa2fe9a
DV
9456}
9457
acbec814 9458static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9459 struct intel_crtc_state *pipe_config)
acbec814
JB
9460{
9461 struct drm_device *dev = crtc->base.dev;
fac5e23e 9462 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 9463 enum pipe pipe = crtc->pipe;
9e2c8475 9464 struct dpll clock;
acbec814 9465 u32 mdiv;
662c6ecb 9466 int refclk = 100000;
acbec814 9467
b521973b
VS
9468 /* In case of DSI, DPLL will not be used */
9469 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
9470 return;
9471
221c7862 9472 vlv_dpio_get(dev_priv);
ab3c759a 9473 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
221c7862 9474 vlv_dpio_put(dev_priv);
acbec814
JB
9475
9476 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9477 clock.m2 = mdiv & DPIO_M2DIV_MASK;
9478 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9479 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9480 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9481
dccbea3b 9482 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
9483}
9484
5724dbd1
DL
9485static void
9486i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9487 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
9488{
9489 struct drm_device *dev = crtc->base.dev;
fac5e23e 9490 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
9491 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9492 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
eade6c89 9493 enum pipe pipe;
1ad292b5 9494 u32 val, base, offset;
1ad292b5 9495 int fourcc, pixel_format;
6761dd31 9496 unsigned int aligned_height;
b113d5ee 9497 struct drm_framebuffer *fb;
1b842c89 9498 struct intel_framebuffer *intel_fb;
1ad292b5 9499
eade6c89 9500 if (!plane->get_hw_state(plane, &pipe))
42a7b088
DL
9501 return;
9502
e57291c2 9503 drm_WARN_ON(dev, pipe != crtc->pipe);
eade6c89 9504
d9806c9f 9505 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9506 if (!intel_fb) {
cd49f818 9507 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
1ad292b5
JB
9508 return;
9509 }
9510
1b842c89
DL
9511 fb = &intel_fb->base;
9512
d2e9f5fc
VS
9513 fb->dev = dev;
9514
dc008bf0 9515 val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
2924b8cc 9516
6315b5d3 9517 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 9518 if (val & DISPPLANE_TILED) {
49af449b 9519 plane_config->tiling = I915_TILING_X;
bae781b2 9520 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e 9521 }
f43348a3
VS
9522
9523 if (val & DISPPLANE_ROTATE_180)
9524 plane_config->rotation = DRM_MODE_ROTATE_180;
18c5247e 9525 }
1ad292b5 9526
f43348a3
VS
9527 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9528 val & DISPPLANE_MIRROR)
9529 plane_config->rotation |= DRM_MODE_REFLECT_X;
9530
1ad292b5 9531 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9532 fourcc = i9xx_format_to_fourcc(pixel_format);
2f3f4763 9533 fb->format = drm_format_info(fourcc);
1ad292b5 9534
81894b2f 9535 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
dc008bf0
JN
9536 offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9537 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
81894b2f 9538 } else if (INTEL_GEN(dev_priv) >= 4) {
49af449b 9539 if (plane_config->tiling)
dc008bf0
JN
9540 offset = intel_de_read(dev_priv,
9541 DSPTILEOFF(i9xx_plane));
1ad292b5 9542 else
dc008bf0
JN
9543 offset = intel_de_read(dev_priv,
9544 DSPLINOFF(i9xx_plane));
9545 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
1ad292b5 9546 } else {
dc008bf0 9547 base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
1ad292b5
JB
9548 }
9549 plane_config->base = base;
9550
dc008bf0 9551 val = intel_de_read(dev_priv, PIPESRC(pipe));
b113d5ee
DL
9552 fb->width = ((val >> 16) & 0xfff) + 1;
9553 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5 9554
dc008bf0 9555 val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
b113d5ee 9556 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 9557
d88c4afd 9558 aligned_height = intel_fb_align_height(fb, 0, fb->height);
1ad292b5 9559
f37b5c2b 9560 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 9561
cd49f818
WK
9562 drm_dbg_kms(&dev_priv->drm,
9563 "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9564 crtc->base.name, plane->base.name, fb->width, fb->height,
9565 fb->format->cpp[0] * 8, base, fb->pitches[0],
9566 plane_config->size);
1ad292b5 9567
2d14030b 9568 plane_config->fb = intel_fb;
1ad292b5
JB
9569}
9570
70b23a98 9571static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9572 struct intel_crtc_state *pipe_config)
70b23a98
VS
9573{
9574 struct drm_device *dev = crtc->base.dev;
fac5e23e 9575 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 9576 enum pipe pipe = crtc->pipe;
70b23a98 9577 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 9578 struct dpll clock;
0d7b6b11 9579 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
9580 int refclk = 100000;
9581
b521973b
VS
9582 /* In case of DSI, DPLL will not be used */
9583 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9584 return;
9585
221c7862 9586 vlv_dpio_get(dev_priv);
70b23a98
VS
9587 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9588 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9589 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9590 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 9591 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
221c7862 9592 vlv_dpio_put(dev_priv);
70b23a98
VS
9593
9594 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
9595 clock.m2 = (pll_dw0 & 0xff) << 22;
9596 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9597 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
9598 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9599 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9600 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9601
dccbea3b 9602 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
9603}
9604
b10d1173
VS
9605static enum intel_output_format
9606bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
33b7f3ee
SS
9607{
9608 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b10d1173
VS
9609 u32 tmp;
9610
dc008bf0 9611 tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
b10d1173
VS
9612
9613 if (tmp & PIPEMISC_YUV420_ENABLE) {
9614 /* We support 4:2:0 in full blend mode only */
e57291c2
PB
9615 drm_WARN_ON(&dev_priv->drm,
9616 (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
33b7f3ee 9617
b10d1173
VS
9618 return INTEL_OUTPUT_FORMAT_YCBCR420;
9619 } else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9620 return INTEL_OUTPUT_FORMAT_YCBCR444;
9621 } else {
9622 return INTEL_OUTPUT_FORMAT_RGB;
9623 }
33b7f3ee
SS
9624}
9625
5f29ab23
VS
9626static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9627{
2225f3c6 9628 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5f29ab23
VS
9629 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9630 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9631 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9632 u32 tmp;
9633
dc008bf0 9634 tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
5f29ab23
VS
9635
9636 if (tmp & DISPPLANE_GAMMA_ENABLE)
9637 crtc_state->gamma_enable = true;
8271b2ef
VS
9638
9639 if (!HAS_GMCH(dev_priv) &&
9640 tmp & DISPPLANE_PIPE_CSC_ENABLE)
9641 crtc_state->csc_enable = true;
5f29ab23
VS
9642}
9643
0e8ffe1b 9644static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9645 struct intel_crtc_state *pipe_config)
0e8ffe1b 9646{
6315b5d3 9647 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 9648 enum intel_display_power_domain power_domain;
0e6e0be4 9649 intel_wakeref_t wakeref;
ba3f4d0a 9650 u32 tmp;
1729050e 9651 bool ret;
0e8ffe1b 9652
1729050e 9653 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
0e6e0be4
CW
9654 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9655 if (!wakeref)
b5482bd0
ID
9656 return false;
9657
d9facae6 9658 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
e143a21c 9659 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9660 pipe_config->shared_dpll = NULL;
eccb140b 9661
1729050e
ID
9662 ret = false;
9663
dc008bf0 9664 tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
0e8ffe1b 9665 if (!(tmp & PIPECONF_ENABLE))
1729050e 9666 goto out;
0e8ffe1b 9667
9beb5fea
TU
9668 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9669 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
9670 switch (tmp & PIPECONF_BPC_MASK) {
9671 case PIPECONF_6BPC:
9672 pipe_config->pipe_bpp = 18;
9673 break;
9674 case PIPECONF_8BPC:
9675 pipe_config->pipe_bpp = 24;
9676 break;
9677 case PIPECONF_10BPC:
9678 pipe_config->pipe_bpp = 30;
9679 break;
9680 default:
9681 break;
9682 }
9683 }
9684
920a14b2 9685 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 9686 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
9687 pipe_config->limited_color_range = true;
9688
9d5441de
VS
9689 pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9690 PIPECONF_GAMMA_MODE_SHIFT;
9691
9fdfb8e7 9692 if (IS_CHERRYVIEW(dev_priv))
dc008bf0
JN
9693 pipe_config->cgm_mode = intel_de_read(dev_priv,
9694 CGM_PIPE_MODE(crtc->pipe));
9fdfb8e7 9695
5f29ab23 9696 i9xx_get_pipe_color_config(pipe_config);
3633e511 9697 intel_color_get_config(pipe_config);
5f29ab23 9698
6315b5d3 9699 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
9700 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9701
e7fc3f90 9702 intel_get_transcoder_timings(crtc, pipe_config);
bc58be60 9703 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 9704
eac9c585 9705 i9xx_get_pfit_config(pipe_config);
2fa2fe9a 9706
6315b5d3 9707 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 9708 /* No way to read it out on pipes B and C */
920a14b2 9709 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
9710 tmp = dev_priv->chv_dpll_md[crtc->pipe];
9711 else
dc008bf0 9712 tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
6c49f241
DV
9713 pipe_config->pixel_multiplier =
9714 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9715 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 9716 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90 9717 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
73f67aa8 9718 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
dc008bf0 9719 tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
6c49f241
DV
9720 pipe_config->pixel_multiplier =
9721 ((tmp & SDVO_MULTIPLIER_MASK)
9722 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9723 } else {
9724 /* Note that on i915G/GM the pixel multiplier is in the sdvo
9725 * port and will be fixed up in the encoder->get_config
9726 * function. */
9727 pipe_config->pixel_multiplier = 1;
9728 }
dc008bf0
JN
9729 pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9730 DPLL(crtc->pipe));
920a14b2 9731 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
dc008bf0
JN
9732 pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9733 FP0(crtc->pipe));
9734 pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9735 FP1(crtc->pipe));
165e901c
VS
9736 } else {
9737 /* Mask out read-only status bits. */
9738 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9739 DPLL_PORTC_READY_MASK |
9740 DPLL_PORTB_READY_MASK);
8bcc2795 9741 }
6c49f241 9742
920a14b2 9743 if (IS_CHERRYVIEW(dev_priv))
70b23a98 9744 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 9745 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
9746 vlv_crtc_clock_get(crtc, pipe_config);
9747 else
9748 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 9749
0f64614d
VS
9750 /*
9751 * Normally the dotclock is filled in by the encoder .get_config()
9752 * but in case the pipe is enabled w/o any ports we need a sane
9753 * default.
9754 */
1326a92c 9755 pipe_config->hw.adjusted_mode.crtc_clock =
0f64614d
VS
9756 pipe_config->port_clock / pipe_config->pixel_multiplier;
9757
1729050e
ID
9758 ret = true;
9759
9760out:
0e6e0be4 9761 intel_display_power_put(dev_priv, power_domain, wakeref);
1729050e
ID
9762
9763 return ret;
0e8ffe1b
DV
9764}
9765
9eae5e27 9766static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 9767{
13d83a67 9768 struct intel_encoder *encoder;
1c1a24d2 9769 int i;
74cfd7ac 9770 u32 val, final;
13d83a67 9771 bool has_lvds = false;
199e5d79 9772 bool has_cpu_edp = false;
199e5d79 9773 bool has_panel = false;
99eb6a01
KP
9774 bool has_ck505 = false;
9775 bool can_ssc = false;
1c1a24d2 9776 bool using_ssc_source = false;
13d83a67
JB
9777
9778 /* We need to take the global config into account */
c39055b0 9779 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
9780 switch (encoder->type) {
9781 case INTEL_OUTPUT_LVDS:
9782 has_panel = true;
9783 has_lvds = true;
9784 break;
9785 case INTEL_OUTPUT_EDP:
9786 has_panel = true;
8f4f2797 9787 if (encoder->port == PORT_A)
199e5d79
KP
9788 has_cpu_edp = true;
9789 break;
6847d71b
PZ
9790 default:
9791 break;
13d83a67
JB
9792 }
9793 }
9794
6e266956 9795 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 9796 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
9797 can_ssc = has_ck505;
9798 } else {
9799 has_ck505 = false;
9800 can_ssc = true;
9801 }
9802
1c1a24d2 9803 /* Check if any DPLLs are using the SSC source */
353ad959 9804 for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
dc008bf0 9805 u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
1c1a24d2
L
9806
9807 if (!(temp & DPLL_VCO_ENABLE))
9808 continue;
9809
9810 if ((temp & PLL_REF_INPUT_MASK) ==
9811 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9812 using_ssc_source = true;
9813 break;
9814 }
9815 }
9816
cd49f818
WK
9817 drm_dbg_kms(&dev_priv->drm,
9818 "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9819 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
9820
9821 /* Ironlake: try to setup display ref clock before DPLL
9822 * enabling. This is only under driver's control after
9823 * PCH B stepping, previous chipset stepping should be
9824 * ignoring this setting.
9825 */
dc008bf0 9826 val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
74cfd7ac
CW
9827
9828 /* As we must carefully and slowly disable/enable each source in turn,
9829 * compute the final state we want first and check if we need to
9830 * make any changes at all.
9831 */
9832 final = val;
9833 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9834 if (has_ck505)
9835 final |= DREF_NONSPREAD_CK505_ENABLE;
9836 else
9837 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9838
8c07eb68 9839 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9840 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9841 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9842
9843 if (has_panel) {
9844 final |= DREF_SSC_SOURCE_ENABLE;
9845
9846 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9847 final |= DREF_SSC1_ENABLE;
9848
9849 if (has_cpu_edp) {
9850 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9851 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9852 else
9853 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9854 } else
9855 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9856 } else if (using_ssc_source) {
9857 final |= DREF_SSC_SOURCE_ENABLE;
9858 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9859 }
9860
9861 if (final == val)
9862 return;
9863
13d83a67 9864 /* Always enable nonspread source */
74cfd7ac 9865 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9866
99eb6a01 9867 if (has_ck505)
74cfd7ac 9868 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9869 else
74cfd7ac 9870 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9871
199e5d79 9872 if (has_panel) {
74cfd7ac
CW
9873 val &= ~DREF_SSC_SOURCE_MASK;
9874 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9875
199e5d79 9876 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9877 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
cd49f818 9878 drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
74cfd7ac 9879 val |= DREF_SSC1_ENABLE;
e77166b5 9880 } else
74cfd7ac 9881 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9882
9883 /* Get SSC going before enabling the outputs */
dc008bf0
JN
9884 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9885 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9886 udelay(200);
9887
74cfd7ac 9888 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9889
9890 /* Enable CPU source on CPU attached eDP */
199e5d79 9891 if (has_cpu_edp) {
99eb6a01 9892 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
cd49f818
WK
9893 drm_dbg_kms(&dev_priv->drm,
9894 "Using SSC on eDP\n");
74cfd7ac 9895 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9896 } else
74cfd7ac 9897 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9898 } else
74cfd7ac 9899 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9900
dc008bf0
JN
9901 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9902 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9903 udelay(200);
9904 } else {
cd49f818 9905 drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
199e5d79 9906
74cfd7ac 9907 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9908
9909 /* Turn off CPU output */
74cfd7ac 9910 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9911
dc008bf0
JN
9912 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9913 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
199e5d79
KP
9914 udelay(200);
9915
1c1a24d2 9916 if (!using_ssc_source) {
cd49f818 9917 drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
199e5d79 9918
1c1a24d2
L
9919 /* Turn off the SSC source */
9920 val &= ~DREF_SSC_SOURCE_MASK;
9921 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9922
1c1a24d2
L
9923 /* Turn off SSC1 */
9924 val &= ~DREF_SSC1_ENABLE;
9925
dc008bf0
JN
9926 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9927 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
1c1a24d2
L
9928 udelay(200);
9929 }
13d83a67 9930 }
74cfd7ac
CW
9931
9932 BUG_ON(val != final);
13d83a67
JB
9933}
9934
f31f2d55 9935static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9936{
ba3f4d0a 9937 u32 tmp;
dde86e2d 9938
dc008bf0 9939 tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
0ff066a9 9940 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
dc008bf0 9941 intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
dde86e2d 9942
dc008bf0 9943 if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
cf3598c2 9944 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
cd49f818 9945 drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
dde86e2d 9946
dc008bf0 9947 tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
0ff066a9 9948 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
dc008bf0 9949 intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
dde86e2d 9950
dc008bf0 9951 if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
cf3598c2 9952 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
cd49f818 9953 drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9954}
9955
9956/* WaMPhyProgramming:hsw */
9957static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9958{
ba3f4d0a 9959 u32 tmp;
dde86e2d
PZ
9960
9961 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9962 tmp &= ~(0xFF << 24);
9963 tmp |= (0x12 << 24);
9964 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9965
dde86e2d
PZ
9966 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9967 tmp |= (1 << 11);
9968 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9969
9970 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9971 tmp |= (1 << 11);
9972 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9973
dde86e2d
PZ
9974 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9975 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9976 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9977
9978 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9979 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9980 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9981
0ff066a9
PZ
9982 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9983 tmp &= ~(7 << 13);
9984 tmp |= (5 << 13);
9985 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9986
0ff066a9
PZ
9987 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9988 tmp &= ~(7 << 13);
9989 tmp |= (5 << 13);
9990 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9991
9992 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9993 tmp &= ~0xFF;
9994 tmp |= 0x1C;
9995 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9996
9997 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9998 tmp &= ~0xFF;
9999 tmp |= 0x1C;
10000 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
10001
10002 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
10003 tmp &= ~(0xFF << 16);
10004 tmp |= (0x1C << 16);
10005 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
10006
10007 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
10008 tmp &= ~(0xFF << 16);
10009 tmp |= (0x1C << 16);
10010 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
10011
0ff066a9
PZ
10012 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
10013 tmp |= (1 << 27);
10014 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 10015
0ff066a9
PZ
10016 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
10017 tmp |= (1 << 27);
10018 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 10019
0ff066a9
PZ
10020 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
10021 tmp &= ~(0xF << 28);
10022 tmp |= (4 << 28);
10023 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 10024
0ff066a9
PZ
10025 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
10026 tmp &= ~(0xF << 28);
10027 tmp |= (4 << 28);
10028 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
10029}
10030
2fa86a1f
PZ
10031/* Implements 3 different sequences from BSpec chapter "Display iCLK
10032 * Programming" based on the parameters passed:
10033 * - Sequence to enable CLKOUT_DP
10034 * - Sequence to enable CLKOUT_DP without spread
10035 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
10036 */
c39055b0
ACO
10037static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
10038 bool with_spread, bool with_fdi)
f31f2d55 10039{
ba3f4d0a 10040 u32 reg, tmp;
2fa86a1f 10041
e57291c2
PB
10042 if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
10043 "FDI requires downspread\n"))
2fa86a1f 10044 with_spread = true;
e57291c2
PB
10045 if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
10046 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 10047 with_fdi = false;
f31f2d55 10048
a580516d 10049 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
10050
10051 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10052 tmp &= ~SBI_SSCCTL_DISABLE;
10053 tmp |= SBI_SSCCTL_PATHALT;
10054 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10055
10056 udelay(24);
10057
2fa86a1f
PZ
10058 if (with_spread) {
10059 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10060 tmp &= ~SBI_SSCCTL_PATHALT;
10061 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 10062
2fa86a1f
PZ
10063 if (with_fdi) {
10064 lpt_reset_fdi_mphy(dev_priv);
10065 lpt_program_fdi_mphy(dev_priv);
10066 }
10067 }
dde86e2d 10068
4f8036a2 10069 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
10070 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10071 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10072 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 10073
a580516d 10074 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
10075}
10076
47701c3b 10077/* Sequence to disable CLKOUT_DP */
46034d2b 10078void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 10079{
ba3f4d0a 10080 u32 reg, tmp;
47701c3b 10081
a580516d 10082 mutex_lock(&dev_priv->sb_lock);
47701c3b 10083
4f8036a2 10084 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
10085 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10086 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10087 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
10088
10089 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10090 if (!(tmp & SBI_SSCCTL_DISABLE)) {
10091 if (!(tmp & SBI_SSCCTL_PATHALT)) {
10092 tmp |= SBI_SSCCTL_PATHALT;
10093 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10094 udelay(32);
10095 }
10096 tmp |= SBI_SSCCTL_DISABLE;
10097 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10098 }
10099
a580516d 10100 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
10101}
10102
f7be2c21
VS
10103#define BEND_IDX(steps) ((50 + (steps)) / 5)
10104
ba3f4d0a 10105static const u16 sscdivintphase[] = {
f7be2c21
VS
10106 [BEND_IDX( 50)] = 0x3B23,
10107 [BEND_IDX( 45)] = 0x3B23,
10108 [BEND_IDX( 40)] = 0x3C23,
10109 [BEND_IDX( 35)] = 0x3C23,
10110 [BEND_IDX( 30)] = 0x3D23,
10111 [BEND_IDX( 25)] = 0x3D23,
10112 [BEND_IDX( 20)] = 0x3E23,
10113 [BEND_IDX( 15)] = 0x3E23,
10114 [BEND_IDX( 10)] = 0x3F23,
10115 [BEND_IDX( 5)] = 0x3F23,
10116 [BEND_IDX( 0)] = 0x0025,
10117 [BEND_IDX( -5)] = 0x0025,
10118 [BEND_IDX(-10)] = 0x0125,
10119 [BEND_IDX(-15)] = 0x0125,
10120 [BEND_IDX(-20)] = 0x0225,
10121 [BEND_IDX(-25)] = 0x0225,
10122 [BEND_IDX(-30)] = 0x0325,
10123 [BEND_IDX(-35)] = 0x0325,
10124 [BEND_IDX(-40)] = 0x0425,
10125 [BEND_IDX(-45)] = 0x0425,
10126 [BEND_IDX(-50)] = 0x0525,
10127};
10128
10129/*
10130 * Bend CLKOUT_DP
10131 * steps -50 to 50 inclusive, in steps of 5
10132 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
10133 * change in clock period = -(steps / 10) * 5.787 ps
10134 */
10135static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
10136{
ba3f4d0a 10137 u32 tmp;
f7be2c21
VS
10138 int idx = BEND_IDX(steps);
10139
e57291c2 10140 if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
f7be2c21
VS
10141 return;
10142
e57291c2 10143 if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
f7be2c21
VS
10144 return;
10145
10146 mutex_lock(&dev_priv->sb_lock);
10147
10148 if (steps % 10 != 0)
10149 tmp = 0xAAAAAAAB;
10150 else
10151 tmp = 0x00000000;
10152 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
10153
10154 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
10155 tmp &= 0xffff0000;
10156 tmp |= sscdivintphase[idx];
10157 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
10158
10159 mutex_unlock(&dev_priv->sb_lock);
10160}
10161
10162#undef BEND_IDX
10163
b16c7ed9
VS
10164static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
10165{
dc008bf0
JN
10166 u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10167 u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
b16c7ed9
VS
10168
10169 if ((ctl & SPLL_PLL_ENABLE) == 0)
10170 return false;
10171
4a95e36f 10172 if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
b16c7ed9
VS
10173 (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10174 return true;
10175
10176 if (IS_BROADWELL(dev_priv) &&
4a95e36f 10177 (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
b16c7ed9
VS
10178 return true;
10179
10180 return false;
10181}
10182
10183static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
10184 enum intel_dpll_id id)
10185{
dc008bf0
JN
10186 u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10187 u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
b16c7ed9
VS
10188
10189 if ((ctl & WRPLL_PLL_ENABLE) == 0)
10190 return false;
10191
4a95e36f 10192 if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
b16c7ed9
VS
10193 return true;
10194
10195 if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
4a95e36f 10196 (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
b16c7ed9
VS
10197 (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10198 return true;
10199
10200 return false;
10201}
10202
c39055b0 10203static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 10204{
bf8fa3d3 10205 struct intel_encoder *encoder;
b16c7ed9 10206 bool has_fdi = false;
bf8fa3d3 10207
c39055b0 10208 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
10209 switch (encoder->type) {
10210 case INTEL_OUTPUT_ANALOG:
b16c7ed9 10211 has_fdi = true;
bf8fa3d3 10212 break;
6847d71b
PZ
10213 default:
10214 break;
bf8fa3d3
PZ
10215 }
10216 }
10217
b16c7ed9
VS
10218 /*
10219 * The BIOS may have decided to use the PCH SSC
10220 * reference so we must not disable it until the
10221 * relevant PLLs have stopped relying on it. We'll
10222 * just leave the PCH SSC reference enabled in case
10223 * any active PLL is using it. It will get disabled
10224 * after runtime suspend if we don't have FDI.
10225 *
10226 * TODO: Move the whole reference clock handling
10227 * to the modeset sequence proper so that we can
10228 * actually enable/disable/reconfigure these things
10229 * safely. To do that we need to introduce a real
10230 * clock hierarchy. That would also allow us to do
10231 * clock bending finally.
10232 */
dd5279c7
VS
10233 dev_priv->pch_ssc_use = 0;
10234
b16c7ed9 10235 if (spll_uses_pch_ssc(dev_priv)) {
cd49f818 10236 drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
dd5279c7 10237 dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
b16c7ed9
VS
10238 }
10239
10240 if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
cd49f818 10241 drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
dd5279c7 10242 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
b16c7ed9
VS
10243 }
10244
10245 if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
cd49f818 10246 drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
dd5279c7 10247 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
b16c7ed9
VS
10248 }
10249
dd5279c7 10250 if (dev_priv->pch_ssc_use)
b16c7ed9
VS
10251 return;
10252
10253 if (has_fdi) {
c39055b0
ACO
10254 lpt_bend_clkout_dp(dev_priv, 0);
10255 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 10256 } else {
c39055b0 10257 lpt_disable_clkout_dp(dev_priv);
f7be2c21 10258 }
bf8fa3d3
PZ
10259}
10260
dde86e2d
PZ
10261/*
10262 * Initialize reference clocks when the driver loads
10263 */
c39055b0 10264void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 10265{
6e266956 10266 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
9eae5e27 10267 ilk_init_pch_refclk(dev_priv);
6e266956 10268 else if (HAS_PCH_LPT(dev_priv))
c39055b0 10269 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
10270}
10271
9eae5e27 10272static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
79e53945 10273{
2225f3c6 10274 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510
ML
10275 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10276 enum pipe pipe = crtc->pipe;
ba3f4d0a 10277 u32 val;
c8203565 10278
78114071 10279 val = 0;
c8203565 10280
fdf73510 10281 switch (crtc_state->pipe_bpp) {
c8203565 10282 case 18:
dfd07d72 10283 val |= PIPECONF_6BPC;
c8203565
PZ
10284 break;
10285 case 24:
dfd07d72 10286 val |= PIPECONF_8BPC;
c8203565
PZ
10287 break;
10288 case 30:
dfd07d72 10289 val |= PIPECONF_10BPC;
c8203565
PZ
10290 break;
10291 case 36:
dfd07d72 10292 val |= PIPECONF_12BPC;
c8203565
PZ
10293 break;
10294 default:
cc769b62
PZ
10295 /* Case prevented by intel_choose_pipe_bpp_dither. */
10296 BUG();
c8203565
PZ
10297 }
10298
fdf73510 10299 if (crtc_state->dither)
c8203565
PZ
10300 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10301
1326a92c 10302 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
10303 val |= PIPECONF_INTERLACED_ILK;
10304 else
10305 val |= PIPECONF_PROGRESSIVE;
10306
d1844606
VS
10307 /*
10308 * This would end up with an odd purple hue over
10309 * the entire display. Make sure we don't do it.
10310 */
e57291c2
PB
10311 drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10312 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
d1844606 10313
90f8ed85
VS
10314 if (crtc_state->limited_color_range &&
10315 !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3685a8f3 10316 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 10317
d1844606
VS
10318 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10319 val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10320
9d5441de
VS
10321 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10322
cc7a4cff
VS
10323 val |= PIPECONF_FRAME_START_DELAY(0);
10324
dc008bf0
JN
10325 intel_de_write(dev_priv, PIPECONF(pipe), val);
10326 intel_de_posting_read(dev_priv, PIPECONF(pipe));
c8203565
PZ
10327}
10328
1e98f88c 10329static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
ee2b0b38 10330{
2225f3c6 10331 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
fdf73510
ML
10332 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10333 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
391bf048 10334 u32 val = 0;
ee2b0b38 10335
fdf73510 10336 if (IS_HASWELL(dev_priv) && crtc_state->dither)
ee2b0b38
PZ
10337 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10338
1326a92c 10339 if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
10340 val |= PIPECONF_INTERLACED_ILK;
10341 else
10342 val |= PIPECONF_PROGRESSIVE;
10343
ac0f01ce
VS
10344 if (IS_HASWELL(dev_priv) &&
10345 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10346 val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10347
dc008bf0
JN
10348 intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10349 intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
391bf048
JN
10350}
10351
9b11215e 10352static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
391bf048 10353{
2225f3c6 10354 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9b11215e
VS
10355 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10356 u32 val = 0;
756f85cf 10357
9b11215e
VS
10358 switch (crtc_state->pipe_bpp) {
10359 case 18:
10360 val |= PIPEMISC_DITHER_6_BPC;
10361 break;
10362 case 24:
10363 val |= PIPEMISC_DITHER_8_BPC;
10364 break;
10365 case 30:
10366 val |= PIPEMISC_DITHER_10_BPC;
10367 break;
10368 case 36:
10369 val |= PIPEMISC_DITHER_12_BPC;
10370 break;
10371 default:
10372 MISSING_CASE(crtc_state->pipe_bpp);
10373 break;
10374 }
756f85cf 10375
9b11215e
VS
10376 if (crtc_state->dither)
10377 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
756f85cf 10378
9b11215e
VS
10379 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10380 crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10381 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
8c79f844 10382
9b11215e
VS
10383 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10384 val |= PIPEMISC_YUV420_ENABLE |
10385 PIPEMISC_YUV420_MODE_FULL_BLEND;
b22ca995 10386
09b25812 10387 if (INTEL_GEN(dev_priv) >= 11 &&
b7ffc4a8
VS
10388 (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10389 BIT(PLANE_CURSOR))) == 0)
09b25812
VS
10390 val |= PIPEMISC_HDR_MODE_PRECISION;
10391
041be481
VS
10392 if (INTEL_GEN(dev_priv) >= 12)
10393 val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10394
dc008bf0 10395 intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
ee2b0b38
PZ
10396}
10397
8ae89743
VK
10398int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10399{
10400 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10401 u32 tmp;
10402
dc008bf0 10403 tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
8ae89743
VK
10404
10405 switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10406 case PIPEMISC_DITHER_6_BPC:
10407 return 18;
10408 case PIPEMISC_DITHER_8_BPC:
10409 return 24;
10410 case PIPEMISC_DITHER_10_BPC:
10411 return 30;
10412 case PIPEMISC_DITHER_12_BPC:
10413 return 36;
10414 default:
10415 MISSING_CASE(tmp);
10416 return 0;
10417 }
10418}
10419
9eae5e27 10420int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
d4b1931c
PZ
10421{
10422 /*
10423 * Account for spread spectrum to avoid
10424 * oversubscribing the link. Max center spread
10425 * is 2.5%; use 5% for safety's sake.
10426 */
10427 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 10428 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
10429}
10430
9eae5e27 10431static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 10432{
7429e9d4 10433 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
10434}
10435
9eae5e27
LDM
10436static void ilk_compute_dpll(struct intel_crtc *crtc,
10437 struct intel_crtc_state *crtc_state,
10438 struct dpll *reduced_clock)
79e53945 10439{
d2daff2c 10440 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b75ca6f6 10441 u32 dpll, fp, fp2;
3d6e9ee0 10442 int factor;
79e53945 10443
c1858123 10444 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 10445 factor = 21;
3d6e9ee0 10446 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 10447 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 10448 dev_priv->vbt.lvds_ssc_freq == 100000) ||
d2daff2c
VS
10449 (HAS_PCH_IBX(dev_priv) &&
10450 intel_is_dual_link_lvds(dev_priv)))
8febb297 10451 factor = 25;
27b680f9 10452 } else if (crtc_state->sdvo_tv_clock) {
8febb297 10453 factor = 20;
27b680f9 10454 }
c1858123 10455
b75ca6f6
ACO
10456 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10457
9eae5e27 10458 if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
10459 fp |= FP_CB_TUNE;
10460
10461 if (reduced_clock) {
10462 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 10463
b75ca6f6
ACO
10464 if (reduced_clock->m < factor * reduced_clock->n)
10465 fp2 |= FP_CB_TUNE;
10466 } else {
10467 fp2 = fp;
10468 }
9a7c7890 10469
5eddb70b 10470 dpll = 0;
2c07245f 10471
3d6e9ee0 10472 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
10473 dpll |= DPLLB_MODE_LVDS;
10474 else
10475 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 10476
190f68c5 10477 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 10478 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 10479
3d6e9ee0
VS
10480 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10481 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 10482 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 10483
37a5650b 10484 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 10485 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 10486
7d7f8633
VS
10487 /*
10488 * The high speed IO clock is only really required for
10489 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10490 * possible to share the DPLL between CRT and HDMI. Enabling
10491 * the clock needlessly does no real harm, except use up a
10492 * bit of power potentially.
10493 *
10494 * We'll limit this to IVB with 3 pipes, since it has only two
10495 * DPLLs and so DPLL sharing is the only way to get three pipes
10496 * driving PCH ports at the same time. On SNB we could do this,
10497 * and potentially avoid enabling the second DPLL, but it's not
10498 * clear if it''s a win or loss power wise. No point in doing
10499 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10500 */
24977870 10501 if (INTEL_NUM_PIPES(dev_priv) == 3 &&
7d7f8633
VS
10502 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10503 dpll |= DPLL_SDVO_HIGH_SPEED;
10504
a07d6787 10505 /* compute bitmask from p1 value */
190f68c5 10506 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 10507 /* also FPA1 */
190f68c5 10508 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 10509
190f68c5 10510 switch (crtc_state->dpll.p2) {
a07d6787
EA
10511 case 5:
10512 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10513 break;
10514 case 7:
10515 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10516 break;
10517 case 10:
10518 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10519 break;
10520 case 14:
10521 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10522 break;
79e53945
JB
10523 }
10524
3d6e9ee0
VS
10525 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10526 intel_panel_use_ssc(dev_priv))
43565a06 10527 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
10528 else
10529 dpll |= PLL_REF_INPUT_DREFCLK;
10530
b75ca6f6
ACO
10531 dpll |= DPLL_VCO_ENABLE;
10532
10533 crtc_state->dpll_hw_state.dpll = dpll;
10534 crtc_state->dpll_hw_state.fp0 = fp;
10535 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
10536}
10537
9eae5e27
LDM
10538static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10539 struct intel_crtc_state *crtc_state)
de13a2e3 10540{
d2daff2c 10541 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
866955fa 10542 struct intel_atomic_state *state =
2225f3c6 10543 to_intel_atomic_state(crtc_state->uapi.state);
1b6f4958 10544 const struct intel_limit *limit;
997c030c 10545 int refclk = 120000;
de13a2e3 10546
dd3cd74a
ACO
10547 memset(&crtc_state->dpll_hw_state, 0,
10548 sizeof(crtc_state->dpll_hw_state));
10549
ded220e2
ACO
10550 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10551 if (!crtc_state->has_pch_encoder)
10552 return 0;
79e53945 10553
2d84d2b3 10554 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c 10555 if (intel_panel_use_ssc(dev_priv)) {
cd49f818
WK
10556 drm_dbg_kms(&dev_priv->drm,
10557 "using SSC reference clock of %d kHz\n",
10558 dev_priv->vbt.lvds_ssc_freq);
997c030c
ACO
10559 refclk = dev_priv->vbt.lvds_ssc_freq;
10560 }
10561
d2daff2c 10562 if (intel_is_dual_link_lvds(dev_priv)) {
997c030c 10563 if (refclk == 100000)
9eae5e27 10564 limit = &ilk_limits_dual_lvds_100m;
997c030c 10565 else
9eae5e27 10566 limit = &ilk_limits_dual_lvds;
997c030c
ACO
10567 } else {
10568 if (refclk == 100000)
9eae5e27 10569 limit = &ilk_limits_single_lvds_100m;
997c030c 10570 else
9eae5e27 10571 limit = &ilk_limits_single_lvds;
997c030c
ACO
10572 }
10573 } else {
9eae5e27 10574 limit = &ilk_limits_dac;
997c030c
ACO
10575 }
10576
364ee29d 10577 if (!crtc_state->clock_set &&
997c030c
ACO
10578 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10579 refclk, NULL, &crtc_state->dpll)) {
cd49f818
WK
10580 drm_err(&dev_priv->drm,
10581 "Couldn't find PLL settings for mode!\n");
364ee29d 10582 return -EINVAL;
f47709a9 10583 }
79e53945 10584
9eae5e27 10585 ilk_compute_dpll(crtc, crtc_state, NULL);
66e985c0 10586
866955fa 10587 if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
cd49f818
WK
10588 drm_dbg_kms(&dev_priv->drm,
10589 "failed to find PLL for pipe %c\n",
10590 pipe_name(crtc->pipe));
ded220e2 10591 return -EINVAL;
3fb37703 10592 }
79e53945 10593
c8f7a0db 10594 return 0;
79e53945
JB
10595}
10596
eb14cb74
VS
10597static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10598 struct intel_link_m_n *m_n)
10599{
10600 struct drm_device *dev = crtc->base.dev;
fac5e23e 10601 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
10602 enum pipe pipe = crtc->pipe;
10603
dc008bf0
JN
10604 m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10605 m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10606 m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
eb14cb74 10607 & ~TU_SIZE_MASK;
dc008bf0
JN
10608 m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10609 m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
eb14cb74
VS
10610 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10611}
10612
10613static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10614 enum transcoder transcoder,
b95af8be
VK
10615 struct intel_link_m_n *m_n,
10616 struct intel_link_m_n *m2_n2)
72419203 10617{
6315b5d3 10618 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 10619 enum pipe pipe = crtc->pipe;
72419203 10620
6315b5d3 10621 if (INTEL_GEN(dev_priv) >= 5) {
dc008bf0
JN
10622 m_n->link_m = intel_de_read(dev_priv,
10623 PIPE_LINK_M1(transcoder));
10624 m_n->link_n = intel_de_read(dev_priv,
10625 PIPE_LINK_N1(transcoder));
10626 m_n->gmch_m = intel_de_read(dev_priv,
10627 PIPE_DATA_M1(transcoder))
eb14cb74 10628 & ~TU_SIZE_MASK;
dc008bf0
JN
10629 m_n->gmch_n = intel_de_read(dev_priv,
10630 PIPE_DATA_N1(transcoder));
10631 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
eb14cb74 10632 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4207c8b9
ML
10633
10634 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
dc008bf0
JN
10635 m2_n2->link_m = intel_de_read(dev_priv,
10636 PIPE_LINK_M2(transcoder));
10637 m2_n2->link_n = intel_de_read(dev_priv,
10638 PIPE_LINK_N2(transcoder));
10639 m2_n2->gmch_m = intel_de_read(dev_priv,
10640 PIPE_DATA_M2(transcoder))
b95af8be 10641 & ~TU_SIZE_MASK;
dc008bf0
JN
10642 m2_n2->gmch_n = intel_de_read(dev_priv,
10643 PIPE_DATA_N2(transcoder));
10644 m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
b95af8be
VK
10645 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10646 }
eb14cb74 10647 } else {
dc008bf0
JN
10648 m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10649 m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10650 m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
eb14cb74 10651 & ~TU_SIZE_MASK;
dc008bf0
JN
10652 m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10653 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
eb14cb74
VS
10654 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10655 }
10656}
10657
10658void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 10659 struct intel_crtc_state *pipe_config)
eb14cb74 10660{
681a8504 10661 if (pipe_config->has_pch_encoder)
eb14cb74
VS
10662 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10663 else
10664 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
10665 &pipe_config->dp_m_n,
10666 &pipe_config->dp_m2_n2);
eb14cb74 10667}
72419203 10668
9eae5e27
LDM
10669static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10670 struct intel_crtc_state *pipe_config)
eb14cb74
VS
10671{
10672 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 10673 &pipe_config->fdi_m_n, NULL);
72419203
DV
10674}
10675
35dd95b4
VS
10676static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
10677 u32 pos, u32 size)
10678{
10679 drm_rect_init(&crtc_state->pch_pfit.dst,
10680 pos >> 16, pos & 0xffff,
10681 size >> 16, size & 0xffff);
10682}
10683
eac9c585 10684static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
bd2e244f 10685{
eac9c585
VS
10686 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10687 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10688 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
a1b2278e
CK
10689 int id = -1;
10690 int i;
bd2e244f 10691
a1b2278e
CK
10692 /* find scaler attached to this pipe */
10693 for (i = 0; i < crtc->num_scalers; i++) {
35dd95b4 10694 u32 ctl, pos, size;
eac9c585 10695
35dd95b4
VS
10696 ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10697 if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
eac9c585
VS
10698 continue;
10699
10700 id = i;
10701 crtc_state->pch_pfit.enabled = true;
35dd95b4
VS
10702
10703 pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
10704 size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
10705
10706 ilk_get_pfit_pos_size(crtc_state, pos, size);
10707
eac9c585
VS
10708 scaler_state->scalers[i].in_use = true;
10709 break;
a1b2278e 10710 }
bd2e244f 10711
a1b2278e 10712 scaler_state->scaler_id = id;
eac9c585 10713 if (id >= 0)
a1b2278e 10714 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
eac9c585 10715 else
a1b2278e 10716 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
10717}
10718
5724dbd1 10719static void
f6df4d46
LDM
10720skl_get_initial_plane_config(struct intel_crtc *crtc,
10721 struct intel_initial_plane_config *plane_config)
bc8d7dff 10722{
0385ecea 10723 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
bc8d7dff 10724 struct drm_device *dev = crtc->base.dev;
fac5e23e 10725 struct drm_i915_private *dev_priv = to_i915(dev);
282e83ef
VS
10726 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10727 enum plane_id plane_id = plane->id;
eade6c89 10728 enum pipe pipe;
4036c78c 10729 u32 val, base, offset, stride_mult, tiling, alpha;
bc8d7dff 10730 int fourcc, pixel_format;
6761dd31 10731 unsigned int aligned_height;
bc8d7dff 10732 struct drm_framebuffer *fb;
1b842c89 10733 struct intel_framebuffer *intel_fb;
bc8d7dff 10734
eade6c89 10735 if (!plane->get_hw_state(plane, &pipe))
2924b8cc
VS
10736 return;
10737
e57291c2 10738 drm_WARN_ON(dev, pipe != crtc->pipe);
eade6c89 10739
0385ecea
MN
10740 if (crtc_state->bigjoiner) {
10741 drm_dbg_kms(&dev_priv->drm,
10742 "Unsupported bigjoiner configuration for initial FB\n");
10743 return;
10744 }
10745
d9806c9f 10746 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 10747 if (!intel_fb) {
cd49f818 10748 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
bc8d7dff
DL
10749 return;
10750 }
10751
1b842c89
DL
10752 fb = &intel_fb->base;
10753
d2e9f5fc
VS
10754 fb->dev = dev;
10755
dc008bf0 10756 val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
42a7b088 10757
b5972776
JA
10758 if (INTEL_GEN(dev_priv) >= 11)
10759 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10760 else
10761 pixel_format = val & PLANE_CTL_FORMAT_MASK;
4036c78c
JA
10762
10763 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
dc008bf0
JN
10764 alpha = intel_de_read(dev_priv,
10765 PLANE_COLOR_CTL(pipe, plane_id));
4036c78c
JA
10766 alpha &= PLANE_COLOR_ALPHA_MASK;
10767 } else {
10768 alpha = val & PLANE_CTL_ALPHA_MASK;
10769 }
10770
bc8d7dff 10771 fourcc = skl_format_to_fourcc(pixel_format,
4036c78c 10772 val & PLANE_CTL_ORDER_RGBX, alpha);
2f3f4763 10773 fb->format = drm_format_info(fourcc);
bc8d7dff 10774
40f46283
DL
10775 tiling = val & PLANE_CTL_TILED_MASK;
10776 switch (tiling) {
10777 case PLANE_CTL_TILED_LINEAR:
2f075565 10778 fb->modifier = DRM_FORMAT_MOD_LINEAR;
40f46283
DL
10779 break;
10780 case PLANE_CTL_TILED_X:
10781 plane_config->tiling = I915_TILING_X;
bae781b2 10782 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
10783 break;
10784 case PLANE_CTL_TILED_Y:
914a4fd8 10785 plane_config->tiling = I915_TILING_Y;
53867b46 10786 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
b3e57bcc
DP
10787 fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10788 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10789 I915_FORMAT_MOD_Y_TILED_CCS;
2dfbf9d2
DP
10790 else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10791 fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
2e2adb05
VS
10792 else
10793 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
10794 break;
10795 case PLANE_CTL_TILED_YF:
53867b46 10796 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
2e2adb05
VS
10797 fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10798 else
10799 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
10800 break;
10801 default:
10802 MISSING_CASE(tiling);
10803 goto error;
10804 }
10805
f43348a3
VS
10806 /*
10807 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10808 * while i915 HW rotation is clockwise, thats why this swapping.
10809 */
10810 switch (val & PLANE_CTL_ROTATE_MASK) {
10811 case PLANE_CTL_ROTATE_0:
10812 plane_config->rotation = DRM_MODE_ROTATE_0;
10813 break;
10814 case PLANE_CTL_ROTATE_90:
10815 plane_config->rotation = DRM_MODE_ROTATE_270;
10816 break;
10817 case PLANE_CTL_ROTATE_180:
10818 plane_config->rotation = DRM_MODE_ROTATE_180;
10819 break;
10820 case PLANE_CTL_ROTATE_270:
10821 plane_config->rotation = DRM_MODE_ROTATE_90;
10822 break;
10823 }
10824
10825 if (INTEL_GEN(dev_priv) >= 10 &&
10826 val & PLANE_CTL_FLIP_HORIZONTAL)
10827 plane_config->rotation |= DRM_MODE_REFLECT_X;
10828
a40a8305
VS
10829 /* 90/270 degree rotation would require extra work */
10830 if (drm_rotation_90_or_270(plane_config->rotation))
10831 goto error;
10832
dc008bf0 10833 base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
bc8d7dff
DL
10834 plane_config->base = base;
10835
dc008bf0 10836 offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
bc8d7dff 10837
dc008bf0 10838 val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
e91c8a29
MN
10839 fb->height = ((val >> 16) & 0xffff) + 1;
10840 fb->width = ((val >> 0) & 0xffff) + 1;
bc8d7dff 10841
dc008bf0 10842 val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
b3cf5c06 10843 stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
bc8d7dff
DL
10844 fb->pitches[0] = (val & 0x3ff) * stride_mult;
10845
d88c4afd 10846 aligned_height = intel_fb_align_height(fb, 0, fb->height);
bc8d7dff 10847
f37b5c2b 10848 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff 10849
cd49f818
WK
10850 drm_dbg_kms(&dev_priv->drm,
10851 "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10852 crtc->base.name, plane->base.name, fb->width, fb->height,
10853 fb->format->cpp[0] * 8, base, fb->pitches[0],
10854 plane_config->size);
bc8d7dff 10855
2d14030b 10856 plane_config->fb = intel_fb;
bc8d7dff
DL
10857 return;
10858
10859error:
d1a3a036 10860 kfree(intel_fb);
bc8d7dff
DL
10861}
10862
eac9c585 10863static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
2fa2fe9a 10864{
eac9c585
VS
10865 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10866 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
35dd95b4 10867 u32 ctl, pos, size;
2fa2fe9a 10868
35dd95b4
VS
10869 ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10870 if ((ctl & PF_ENABLE) == 0)
eac9c585 10871 return;
2fa2fe9a 10872
eac9c585 10873 crtc_state->pch_pfit.enabled = true;
35dd95b4
VS
10874
10875 pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
10876 size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
10877
10878 ilk_get_pfit_pos_size(crtc_state, pos, size);
eac9c585
VS
10879
10880 /*
10881 * We currently do not free assignements of panel fitters on
10882 * ivb/hsw (since we don't use the higher upscaling modes which
10883 * differentiates them) so just WARN about this case for now.
10884 */
10885 drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
35dd95b4 10886 (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
79e53945
JB
10887}
10888
9eae5e27
LDM
10889static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10890 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
10891{
10892 struct drm_device *dev = crtc->base.dev;
fac5e23e 10893 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 10894 enum intel_display_power_domain power_domain;
0e6e0be4 10895 intel_wakeref_t wakeref;
ba3f4d0a 10896 u32 tmp;
1729050e 10897 bool ret;
0e8ffe1b 10898
1729050e 10899 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
0e6e0be4
CW
10900 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10901 if (!wakeref)
930e8c9e
PZ
10902 return false;
10903
e143a21c 10904 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 10905 pipe_config->shared_dpll = NULL;
eccb140b 10906
1729050e 10907 ret = false;
dc008bf0 10908 tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
0e8ffe1b 10909 if (!(tmp & PIPECONF_ENABLE))
1729050e 10910 goto out;
0e8ffe1b 10911
42571aef
VS
10912 switch (tmp & PIPECONF_BPC_MASK) {
10913 case PIPECONF_6BPC:
10914 pipe_config->pipe_bpp = 18;
10915 break;
10916 case PIPECONF_8BPC:
10917 pipe_config->pipe_bpp = 24;
10918 break;
10919 case PIPECONF_10BPC:
10920 pipe_config->pipe_bpp = 30;
10921 break;
10922 case PIPECONF_12BPC:
10923 pipe_config->pipe_bpp = 36;
10924 break;
10925 default:
10926 break;
10927 }
10928
b5a9fa09
DV
10929 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10930 pipe_config->limited_color_range = true;
10931
d1844606
VS
10932 switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10933 case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10934 case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10935 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10936 break;
10937 default:
10938 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10939 break;
10940 }
10941
9d5441de
VS
10942 pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10943 PIPECONF_GAMMA_MODE_SHIFT;
10944
dc008bf0
JN
10945 pipe_config->csc_mode = intel_de_read(dev_priv,
10946 PIPE_CSC_MODE(crtc->pipe));
a1f1e61b 10947
5f29ab23 10948 i9xx_get_pipe_color_config(pipe_config);
3633e511 10949 intel_color_get_config(pipe_config);
5f29ab23 10950
dc008bf0 10951 if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 10952 struct intel_shared_dpll *pll;
8106ddbd 10953 enum intel_dpll_id pll_id;
fdbc5d68 10954 bool pll_active;
66e985c0 10955
88adfff1
DV
10956 pipe_config->has_pch_encoder = true;
10957
dc008bf0 10958 tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
627eb5a3
DV
10959 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10960 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203 10961
9eae5e27 10962 ilk_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 10963
2d1fe073 10964 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
10965 /*
10966 * The pipe->pch transcoder and pch transcoder->pll
10967 * mapping is fixed.
10968 */
8106ddbd 10969 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62 10970 } else {
dc008bf0 10971 tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
c0d43d62 10972 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 10973 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 10974 else
8106ddbd 10975 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 10976 }
66e985c0 10977
8106ddbd
ACO
10978 pipe_config->shared_dpll =
10979 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10980 pll = pipe_config->shared_dpll;
66e985c0 10981
fdbc5d68
VS
10982 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
10983 &pipe_config->dpll_hw_state);
10984 drm_WARN_ON(dev, !pll_active);
c93f54cf
DV
10985
10986 tmp = pipe_config->dpll_hw_state.dpll;
10987 pipe_config->pixel_multiplier =
10988 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10989 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08 10990
9eae5e27 10991 ilk_pch_clock_get(crtc, pipe_config);
6c49f241
DV
10992 } else {
10993 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
10994 }
10995
e7fc3f90 10996 intel_get_transcoder_timings(crtc, pipe_config);
bc58be60 10997 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10998
eac9c585 10999 ilk_get_pfit_config(pipe_config);
2fa2fe9a 11000
1729050e
ID
11001 ret = true;
11002
11003out:
0e6e0be4 11004 intel_display_power_put(dev_priv, power_domain, wakeref);
1729050e
ID
11005
11006 return ret;
0e8ffe1b 11007}
1e98f88c
LDM
11008
11009static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
11010 struct intel_crtc_state *crtc_state)
09b4ddf9 11011{
70a057b7 11012 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5a0b385e 11013 struct intel_atomic_state *state =
2225f3c6 11014 to_intel_atomic_state(crtc_state->uapi.state);
5a0b385e 11015
70a057b7 11016 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
2dd24a9c 11017 INTEL_GEN(dev_priv) >= 11) {
44a126ba 11018 struct intel_encoder *encoder =
5a0b385e 11019 intel_get_crtc_new_encoder(state, crtc_state);
44a126ba 11020
866955fa 11021 if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
cd49f818
WK
11022 drm_dbg_kms(&dev_priv->drm,
11023 "failed to find PLL for pipe %c\n",
11024 pipe_name(crtc->pipe));
af3997b5 11025 return -EINVAL;
44a126ba 11026 }
af3997b5 11027 }
716c2e55 11028
c8f7a0db 11029 return 0;
79e53945
JB
11030}
11031
11ffe972
LDM
11032static void dg1_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11033 struct intel_crtc_state *pipe_config)
11034{
11035 enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
11036 enum phy phy = intel_port_to_phy(dev_priv, port);
b97fcaee
VS
11037 struct icl_port_dpll *port_dpll;
11038 struct intel_shared_dpll *pll;
11ffe972 11039 enum intel_dpll_id id;
b97fcaee 11040 bool pll_active;
11ffe972
LDM
11041 u32 clk_sel;
11042
11043 clk_sel = intel_de_read(dev_priv, DG1_DPCLKA_CFGCR0(phy)) & DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11044 id = DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_DPLL_MAP(clk_sel, phy);
11045
11046 if (WARN_ON(id > DPLL_ID_DG1_DPLL3))
11047 return;
11048
b97fcaee
VS
11049 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11050 port_dpll = &pipe_config->icl_port_dplls[port_dpll_id];
11051
11052 port_dpll->pll = pll;
11053 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11054 &port_dpll->hw_state);
11055 drm_WARN_ON(&dev_priv->drm, !pll_active);
11ffe972
LDM
11056
11057 icl_set_active_port_dpll(pipe_config, port_dpll_id);
11058}
11059
95be3484
LDM
11060static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11061 struct intel_crtc_state *pipe_config)
970888e7 11062{
d8fe2ab6 11063 enum phy phy = intel_port_to_phy(dev_priv, port);
eea72c4c 11064 enum icl_port_dpll_id port_dpll_id;
b97fcaee
VS
11065 struct icl_port_dpll *port_dpll;
11066 struct intel_shared_dpll *pll;
970888e7 11067 enum intel_dpll_id id;
b97fcaee 11068 bool pll_active;
970888e7
PZ
11069 u32 temp;
11070
d8fe2ab6 11071 if (intel_phy_is_combo(dev_priv, phy)) {
cd803bb4
MR
11072 u32 mask, shift;
11073
11074 if (IS_ROCKETLAKE(dev_priv)) {
11075 mask = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11076 shift = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11077 } else {
11078 mask = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11079 shift = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11080 }
11081
11082 temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) & mask;
11083 id = temp >> shift;
eea72c4c 11084 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
d8fe2ab6 11085 } else if (intel_phy_is_tc(dev_priv, phy)) {
dc008bf0 11086 u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
39a5883f
ID
11087
11088 if (clk_sel == DDI_CLK_SEL_MG) {
11089 id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
11090 port));
eea72c4c 11091 port_dpll_id = ICL_PORT_DPLL_MG_PHY;
39a5883f 11092 } else {
e57291c2
PB
11093 drm_WARN_ON(&dev_priv->drm,
11094 clk_sel < DDI_CLK_SEL_TBT_162);
39a5883f 11095 id = DPLL_ID_ICL_TBTPLL;
eea72c4c 11096 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
39a5883f 11097 }
8ea59e67 11098 } else {
e57291c2 11099 drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
970888e7
PZ
11100 return;
11101 }
11102
b97fcaee
VS
11103 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11104 port_dpll = &pipe_config->icl_port_dplls[port_dpll_id];
11105
11106 port_dpll->pll = pll;
11107 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11108 &port_dpll->hw_state);
11109 drm_WARN_ON(&dev_priv->drm, !pll_active);
eea72c4c
ID
11110
11111 icl_set_active_port_dpll(pipe_config, port_dpll_id);
970888e7
PZ
11112}
11113
c9afbf58
VS
11114static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11115 struct intel_crtc_state *pipe_config)
11116{
11117 struct intel_shared_dpll *pll;
11118 enum intel_dpll_id id;
11119 bool pll_active;
11120 u32 temp;
11121
11122 temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
11123 id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
11124
11125 if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
11126 return;
11127
11128 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11129
11130 pipe_config->shared_dpll = pll;
11131 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11132 &pipe_config->dpll_hw_state);
11133 drm_WARN_ON(&dev_priv->drm, !pll_active);
11134}
11135
3760b59c
S
11136static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
11137 enum port port,
11138 struct intel_crtc_state *pipe_config)
11139{
b97fcaee 11140 struct intel_shared_dpll *pll;
8106ddbd 11141 enum intel_dpll_id id;
b97fcaee 11142 bool pll_active;
8106ddbd 11143
3760b59c
S
11144 switch (port) {
11145 case PORT_A:
08250c4b 11146 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
11147 break;
11148 case PORT_B:
08250c4b 11149 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
11150 break;
11151 case PORT_C:
08250c4b 11152 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
11153 break;
11154 default:
cd49f818 11155 drm_err(&dev_priv->drm, "Incorrect port type\n");
8106ddbd 11156 return;
3760b59c 11157 }
8106ddbd 11158
b97fcaee
VS
11159 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11160
11161 pipe_config->shared_dpll = pll;
11162 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11163 &pipe_config->dpll_hw_state);
11164 drm_WARN_ON(&dev_priv->drm, !pll_active);
3760b59c
S
11165}
11166
f6df4d46
LDM
11167static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11168 struct intel_crtc_state *pipe_config)
96b7dfb7 11169{
b97fcaee 11170 struct intel_shared_dpll *pll;
8106ddbd 11171 enum intel_dpll_id id;
b97fcaee 11172 bool pll_active;
a3c988ea 11173 u32 temp;
96b7dfb7 11174
dc008bf0 11175 temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 11176 id = temp >> (port * 3 + 1);
96b7dfb7 11177
e57291c2 11178 if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 11179 return;
8106ddbd 11180
b97fcaee
VS
11181 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11182
11183 pipe_config->shared_dpll = pll;
11184 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11185 &pipe_config->dpll_hw_state);
11186 drm_WARN_ON(&dev_priv->drm, !pll_active);
96b7dfb7
S
11187}
11188
1e98f88c
LDM
11189static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11190 struct intel_crtc_state *pipe_config)
7d2c8175 11191{
b97fcaee 11192 struct intel_shared_dpll *pll;
8106ddbd 11193 enum intel_dpll_id id;
dc008bf0 11194 u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
b97fcaee 11195 bool pll_active;
8106ddbd 11196
c856052a 11197 switch (ddi_pll_sel) {
7d2c8175 11198 case PORT_CLK_SEL_WRPLL1:
8106ddbd 11199 id = DPLL_ID_WRPLL1;
7d2c8175
DL
11200 break;
11201 case PORT_CLK_SEL_WRPLL2:
8106ddbd 11202 id = DPLL_ID_WRPLL2;
7d2c8175 11203 break;
00490c22 11204 case PORT_CLK_SEL_SPLL:
8106ddbd 11205 id = DPLL_ID_SPLL;
79bd23da 11206 break;
9d16da65
ACO
11207 case PORT_CLK_SEL_LCPLL_810:
11208 id = DPLL_ID_LCPLL_810;
11209 break;
11210 case PORT_CLK_SEL_LCPLL_1350:
11211 id = DPLL_ID_LCPLL_1350;
11212 break;
11213 case PORT_CLK_SEL_LCPLL_2700:
11214 id = DPLL_ID_LCPLL_2700;
11215 break;
8106ddbd 11216 default:
c856052a 11217 MISSING_CASE(ddi_pll_sel);
df561f66 11218 fallthrough;
8106ddbd 11219 case PORT_CLK_SEL_NONE:
8106ddbd 11220 return;
7d2c8175 11221 }
8106ddbd 11222
b97fcaee
VS
11223 pll = intel_get_shared_dpll_by_id(dev_priv, id);
11224
11225 pipe_config->shared_dpll = pll;
11226 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
11227 &pipe_config->dpll_hw_state);
11228 drm_WARN_ON(&dev_priv->drm, !pll_active);
7d2c8175
DL
11229}
11230
cf30429e
JN
11231static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
11232 struct intel_crtc_state *pipe_config,
513a4c55
CW
11233 u64 *power_domain_mask,
11234 intel_wakeref_t *wakerefs)
cf30429e
JN
11235{
11236 struct drm_device *dev = crtc->base.dev;
fac5e23e 11237 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e 11238 enum intel_display_power_domain power_domain;
617458cd 11239 unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
0716931a
JN
11240 unsigned long enabled_panel_transcoders = 0;
11241 enum transcoder panel_transcoder;
513a4c55 11242 intel_wakeref_t wf;
cf30429e 11243 u32 tmp;
0716931a 11244
2dd24a9c 11245 if (INTEL_GEN(dev_priv) >= 11)
0716931a
JN
11246 panel_transcoder_mask |=
11247 BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
cf30429e 11248
d9a7bc67
ID
11249 /*
11250 * The pipe->transcoder mapping is fixed with the exception of the eDP
0716931a 11251 * and DSI transcoders handled below.
d9a7bc67 11252 */
cf30429e
JN
11253 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
11254
11255 /*
11256 * XXX: Do intel_display_power_get_if_enabled before reading this (for
11257 * consistency and less surprising code; it's in always on power).
11258 */
617458cd
AS
11259 for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
11260 panel_transcoder_mask) {
dc0c0bfe 11261 bool force_thru = false;
0716931a 11262 enum pipe trans_pipe;
2ca711ca 11263
dc008bf0
JN
11264 tmp = intel_de_read(dev_priv,
11265 TRANS_DDI_FUNC_CTL(panel_transcoder));
0716931a
JN
11266 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11267 continue;
2ca711ca 11268
0716931a
JN
11269 /*
11270 * Log all enabled ones, only use the first one.
11271 *
11272 * FIXME: This won't work for two separate DSI displays.
11273 */
11274 enabled_panel_transcoders |= BIT(panel_transcoder);
11275 if (enabled_panel_transcoders != BIT(panel_transcoder))
11276 continue;
2ca711ca 11277
cf30429e
JN
11278 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
11279 default:
e57291c2
PB
11280 drm_WARN(dev, 1,
11281 "unknown pipe linked to transcoder %s\n",
11282 transcoder_name(panel_transcoder));
df561f66 11283 fallthrough;
cf30429e 11284 case TRANS_DDI_EDP_INPUT_A_ONOFF:
dc0c0bfe 11285 force_thru = true;
df561f66 11286 fallthrough;
cf30429e 11287 case TRANS_DDI_EDP_INPUT_A_ON:
2ca711ca 11288 trans_pipe = PIPE_A;
cf30429e
JN
11289 break;
11290 case TRANS_DDI_EDP_INPUT_B_ONOFF:
2ca711ca 11291 trans_pipe = PIPE_B;
cf30429e
JN
11292 break;
11293 case TRANS_DDI_EDP_INPUT_C_ONOFF:
2ca711ca 11294 trans_pipe = PIPE_C;
cf30429e 11295 break;
9c569784
JN
11296 case TRANS_DDI_EDP_INPUT_D_ONOFF:
11297 trans_pipe = PIPE_D;
11298 break;
cf30429e
JN
11299 }
11300
dc0c0bfe 11301 if (trans_pipe == crtc->pipe) {
0716931a 11302 pipe_config->cpu_transcoder = panel_transcoder;
dc0c0bfe
VS
11303 pipe_config->pch_pfit.force_thru = force_thru;
11304 }
cf30429e
JN
11305 }
11306
0716931a
JN
11307 /*
11308 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
11309 */
e57291c2
PB
11310 drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
11311 enabled_panel_transcoders != BIT(TRANSCODER_EDP));
0716931a 11312
cf30429e 11313 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
e57291c2 11314 drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11315
11316 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11317 if (!wf)
cf30429e 11318 return false;
04161d64 11319
513a4c55 11320 wakerefs[power_domain] = wf;
d8fc70b7 11321 *power_domain_mask |= BIT_ULL(power_domain);
cf30429e 11322
dc008bf0 11323 tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
cf30429e
JN
11324
11325 return tmp & PIPECONF_ENABLE;
11326}
11327
4d1de975
JN
11328static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
11329 struct intel_crtc_state *pipe_config,
513a4c55
CW
11330 u64 *power_domain_mask,
11331 intel_wakeref_t *wakerefs)
4d1de975
JN
11332{
11333 struct drm_device *dev = crtc->base.dev;
fac5e23e 11334 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 11335 enum intel_display_power_domain power_domain;
4d1de975 11336 enum transcoder cpu_transcoder;
513a4c55
CW
11337 intel_wakeref_t wf;
11338 enum port port;
4d1de975
JN
11339 u32 tmp;
11340
4d1de975
JN
11341 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
11342 if (port == PORT_A)
11343 cpu_transcoder = TRANSCODER_DSI_A;
11344 else
11345 cpu_transcoder = TRANSCODER_DSI_C;
11346
11347 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
e57291c2 11348 drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11349
11350 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11351 if (!wf)
4d1de975 11352 continue;
04161d64 11353
513a4c55 11354 wakerefs[power_domain] = wf;
d8fc70b7 11355 *power_domain_mask |= BIT_ULL(power_domain);
4d1de975 11356
db18b6a6
ID
11357 /*
11358 * The PLL needs to be enabled with a valid divider
11359 * configuration, otherwise accessing DSI registers will hang
11360 * the machine. See BSpec North Display Engine
11361 * registers/MIPI[BXT]. We can break out here early, since we
11362 * need the same DSI PLL to be enabled for both DSI ports.
11363 */
e518634b 11364 if (!bxt_dsi_pll_is_enabled(dev_priv))
db18b6a6
ID
11365 break;
11366
4d1de975 11367 /* XXX: this works for video mode only */
dc008bf0 11368 tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
4d1de975
JN
11369 if (!(tmp & DPI_ENABLE))
11370 continue;
11371
dc008bf0 11372 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
4d1de975
JN
11373 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11374 continue;
11375
11376 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
11377 break;
11378 }
11379
d7edc4e5 11380 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
11381}
11382
1e98f88c
LDM
11383static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11384 struct intel_crtc_state *pipe_config)
26804afd 11385{
6315b5d3 11386 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6d73af27 11387 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
26804afd 11388 enum port port;
ba3f4d0a 11389 u32 tmp;
26804afd 11390
6d73af27
VK
11391 if (transcoder_is_dsi(cpu_transcoder)) {
11392 port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11393 PORT_A : PORT_B;
11394 } else {
dc008bf0
JN
11395 tmp = intel_de_read(dev_priv,
11396 TRANS_DDI_FUNC_CTL(cpu_transcoder));
0385ecea
MN
11397 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11398 return;
6d73af27
VK
11399 if (INTEL_GEN(dev_priv) >= 12)
11400 port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11401 else
11402 port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11403 }
26804afd 11404
11ffe972
LDM
11405 if (IS_DG1(dev_priv))
11406 dg1_get_ddi_pll(dev_priv, port, pipe_config);
11407 else if (INTEL_GEN(dev_priv) >= 11)
95be3484 11408 icl_get_ddi_pll(dev_priv, port, pipe_config);
970888e7 11409 else if (IS_CANNONLAKE(dev_priv))
8e619820 11410 cnl_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 11411 else if (IS_GEN9_LP(dev_priv))
3760b59c 11412 bxt_get_ddi_pll(dev_priv, port, pipe_config);
c9afbf58
VS
11413 else if (IS_GEN9_BC(dev_priv))
11414 skl_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7 11415 else
1e98f88c 11416 hsw_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 11417
26804afd
DV
11418 /*
11419 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11420 * DDI E. So just check whether this pipe is wired to DDI E and whether
11421 * the PCH transcoder is on.
11422 */
6315b5d3 11423 if (INTEL_GEN(dev_priv) < 9 &&
dc008bf0 11424 (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
11425 pipe_config->has_pch_encoder = true;
11426
dc008bf0 11427 tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
26804afd
DV
11428 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11429 FDI_DP_PORT_WIDTH_SHIFT) + 1;
11430
9eae5e27 11431 ilk_get_fdi_m_n_config(crtc, pipe_config);
26804afd
DV
11432 }
11433}
11434
1e98f88c
LDM
11435static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11436 struct intel_crtc_state *pipe_config)
0e8ffe1b 11437{
6315b5d3 11438 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
513a4c55 11439 intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
1729050e 11440 enum intel_display_power_domain power_domain;
d8fc70b7 11441 u64 power_domain_mask;
cf30429e 11442 bool active;
6dcde047 11443 u32 tmp;
0e8ffe1b 11444
ba5f1ae9
MN
11445 pipe_config->master_transcoder = INVALID_TRANSCODER;
11446
1729050e 11447 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
513a4c55
CW
11448 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11449 if (!wf)
b5482bd0 11450 return false;
513a4c55
CW
11451
11452 wakerefs[power_domain] = wf;
d8fc70b7 11453 power_domain_mask = BIT_ULL(power_domain);
1729050e 11454
8106ddbd 11455 pipe_config->shared_dpll = NULL;
c0d43d62 11456
513a4c55
CW
11457 active = hsw_get_transcoder_state(crtc, pipe_config,
11458 &power_domain_mask, wakerefs);
eccb140b 11459
cc3f90f0 11460 if (IS_GEN9_LP(dev_priv) &&
513a4c55
CW
11461 bxt_get_dsi_transcoder_state(crtc, pipe_config,
11462 &power_domain_mask, wakerefs)) {
e57291c2 11463 drm_WARN_ON(&dev_priv->drm, active);
d7edc4e5 11464 active = true;
4d1de975
JN
11465 }
11466
0385ecea
MN
11467 intel_dsc_get_config(pipe_config);
11468
11469 if (!active) {
11470 /* bigjoiner slave doesn't enable transcoder */
11471 if (!pipe_config->bigjoiner_slave)
11472 goto out;
0e8ffe1b 11473
0385ecea
MN
11474 active = true;
11475 pipe_config->pixel_multiplier = 1;
11476
11477 /* we cannot read out most state, so don't bother.. */
11478 pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
11479 } else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
2dd24a9c 11480 INTEL_GEN(dev_priv) >= 11) {
1e98f88c 11481 hsw_get_ddi_port_state(crtc, pipe_config);
e7fc3f90 11482 intel_get_transcoder_timings(crtc, pipe_config);
4d1de975 11483 }
627eb5a3 11484
bc58be60 11485 intel_get_pipe_src_size(crtc, pipe_config);
b10d1173 11486
ac0f01ce 11487 if (IS_HASWELL(dev_priv)) {
dc008bf0
JN
11488 u32 tmp = intel_de_read(dev_priv,
11489 PIPECONF(pipe_config->cpu_transcoder));
ac0f01ce
VS
11490
11491 if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11492 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11493 else
11494 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11495 } else {
b10d1173
VS
11496 pipe_config->output_format =
11497 bdw_get_pipemisc_output_format(crtc);
b10d1173 11498 }
1bd1bd80 11499
dc008bf0
JN
11500 pipe_config->gamma_mode = intel_de_read(dev_priv,
11501 GAMMA_MODE(crtc->pipe));
05dc698c 11502
dc008bf0
JN
11503 pipe_config->csc_mode = intel_de_read(dev_priv,
11504 PIPE_CSC_MODE(crtc->pipe));
a1f1e61b 11505
5f29ab23 11506 if (INTEL_GEN(dev_priv) >= 9) {
6dcde047 11507 tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
5f29ab23
VS
11508
11509 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11510 pipe_config->gamma_enable = true;
8271b2ef
VS
11511
11512 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11513 pipe_config->csc_enable = true;
5f29ab23
VS
11514 } else {
11515 i9xx_get_pipe_color_config(pipe_config);
11516 }
11517
3633e511
SS
11518 intel_color_get_config(pipe_config);
11519
6dcde047
VS
11520 tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11521 pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11522 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11523 pipe_config->ips_linetime =
11524 REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11525
1729050e 11526 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
e57291c2 11527 drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
513a4c55
CW
11528
11529 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11530 if (wf) {
11531 wakerefs[power_domain] = wf;
d8fc70b7 11532 power_domain_mask |= BIT_ULL(power_domain);
04161d64 11533
6315b5d3 11534 if (INTEL_GEN(dev_priv) >= 9)
eac9c585 11535 skl_get_pfit_config(pipe_config);
ff6d9f55 11536 else
eac9c585 11537 ilk_get_pfit_config(pipe_config);
bd2e244f 11538 }
88adfff1 11539
24f28450
ML
11540 if (hsw_crtc_supports_ips(crtc)) {
11541 if (IS_HASWELL(dev_priv))
dc008bf0
JN
11542 pipe_config->ips_enabled = intel_de_read(dev_priv,
11543 IPS_CTL) & IPS_ENABLE;
24f28450
ML
11544 else {
11545 /*
11546 * We cannot readout IPS state on broadwell, set to
11547 * true so we can set it to a defined state on first
11548 * commit.
11549 */
11550 pipe_config->ips_enabled = true;
11551 }
11552 }
11553
0385ecea
MN
11554 if (pipe_config->bigjoiner_slave) {
11555 /* Cannot be read out as a slave, set to 0. */
11556 pipe_config->pixel_multiplier = 0;
11557 } else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
4d1de975 11558 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95 11559 pipe_config->pixel_multiplier =
dc008bf0
JN
11560 intel_de_read(dev_priv,
11561 PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
ebb69c95
CT
11562 } else {
11563 pipe_config->pixel_multiplier = 1;
11564 }
6c49f241 11565
1729050e
ID
11566out:
11567 for_each_power_domain(power_domain, power_domain_mask)
513a4c55
CW
11568 intel_display_power_put(dev_priv,
11569 power_domain, wakerefs[power_domain]);
1729050e 11570
cf30429e 11571 return active;
0e8ffe1b
DV
11572}
11573
11f9af16
MN
11574static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
11575{
11576 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11577 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
11578
291106cb
VS
11579 if (!i915->display.get_pipe_config(crtc, crtc_state))
11580 return false;
11581
11582 crtc_state->hw.active = true;
11583
c42773b6
VS
11584 intel_crtc_readout_derived_state(crtc_state);
11585
291106cb 11586 return true;
11f9af16
MN
11587}
11588
cd5dcbf1 11589static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
1cecc830
VS
11590{
11591 struct drm_i915_private *dev_priv =
f90a85e7 11592 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 11593 const struct drm_framebuffer *fb = plane_state->hw.fb;
1cecc830
VS
11594 const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11595 u32 base;
11596
d53db442 11597 if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
c6790dc2 11598 base = sg_dma_address(obj->mm.pages->sgl);
1cecc830
VS
11599 else
11600 base = intel_plane_ggtt_offset(plane_state);
11601
30a027dc 11602 return base + plane_state->color_plane[0].offset;
1cecc830
VS
11603}
11604
ed270223
VS
11605static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11606{
f90a85e7
ML
11607 int x = plane_state->uapi.dst.x1;
11608 int y = plane_state->uapi.dst.y1;
ed270223
VS
11609 u32 pos = 0;
11610
11611 if (x < 0) {
11612 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11613 x = -x;
11614 }
11615 pos |= x << CURSOR_X_SHIFT;
11616
11617 if (y < 0) {
11618 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11619 y = -y;
11620 }
11621 pos |= y << CURSOR_Y_SHIFT;
11622
11623 return pos;
11624}
11625
3637ecf0
VS
11626static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11627{
11628 const struct drm_mode_config *config =
f90a85e7
ML
11629 &plane_state->uapi.plane->dev->mode_config;
11630 int width = drm_rect_width(&plane_state->uapi.dst);
11631 int height = drm_rect_height(&plane_state->uapi.dst);
3637ecf0
VS
11632
11633 return width > 0 && width <= config->cursor_width &&
11634 height > 0 && height <= config->cursor_height;
11635}
11636
fce8d235 11637static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
659056f2 11638{
30a027dc 11639 struct drm_i915_private *dev_priv =
f90a85e7 11640 to_i915(plane_state->uapi.plane->dev);
7b3cb17a 11641 unsigned int rotation = plane_state->hw.rotation;
1e7b4fd8
VS
11642 int src_x, src_y;
11643 u32 offset;
fc3fed5d 11644 int ret;
fce8d235 11645
54d4d719 11646 ret = intel_plane_compute_gtt(plane_state);
fc3fed5d
VS
11647 if (ret)
11648 return ret;
11649
f90a85e7 11650 if (!plane_state->uapi.visible)
54d4d719
VS
11651 return 0;
11652
f90a85e7
ML
11653 src_x = plane_state->uapi.src.x1 >> 16;
11654 src_y = plane_state->uapi.src.y1 >> 16;
fce8d235
VS
11655
11656 intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11657 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11658 plane_state, 0);
11659
11660 if (src_x != 0 || src_y != 0) {
cd49f818
WK
11661 drm_dbg_kms(&dev_priv->drm,
11662 "Arbitrary cursor panning not supported\n");
fce8d235
VS
11663 return -EINVAL;
11664 }
11665
30a027dc
VS
11666 /*
11667 * Put the final coordinates back so that the src
11668 * coordinate checks will see the right values.
11669 */
f90a85e7 11670 drm_rect_translate_to(&plane_state->uapi.src,
30a027dc
VS
11671 src_x << 16, src_y << 16);
11672
11673 /* ILK+ do this automagically in hardware */
11674 if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
7b3cb17a 11675 const struct drm_framebuffer *fb = plane_state->hw.fb;
f90a85e7
ML
11676 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11677 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
30a027dc
VS
11678
11679 offset += (src_h * src_w - 1) * fb->format->cpp[0];
11680 }
11681
fce8d235 11682 plane_state->color_plane[0].offset = offset;
30a027dc
VS
11683 plane_state->color_plane[0].x = src_x;
11684 plane_state->color_plane[0].y = src_y;
fce8d235
VS
11685
11686 return 0;
11687}
11688
11689static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11690 struct intel_plane_state *plane_state)
11691{
7b3cb17a 11692 const struct drm_framebuffer *fb = plane_state->hw.fb;
cd49f818 11693 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
659056f2
VS
11694 int ret;
11695
4e0b83a5 11696 if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
cd49f818 11697 drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
4e0b83a5
VS
11698 return -EINVAL;
11699 }
11700
f90a85e7 11701 ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
2225f3c6 11702 &crtc_state->uapi,
a01cb8ba
VS
11703 DRM_PLANE_HELPER_NO_SCALING,
11704 DRM_PLANE_HELPER_NO_SCALING,
11705 true, true);
659056f2
VS
11706 if (ret)
11707 return ret;
11708
3a612765 11709 /* Use the unclipped src/dst rectangles, which we program to hw */
f90a85e7
ML
11710 plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11711 plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
3a612765 11712
54d4d719
VS
11713 ret = intel_cursor_check_surface(plane_state);
11714 if (ret)
11715 return ret;
11716
f90a85e7 11717 if (!plane_state->uapi.visible)
659056f2
VS
11718 return 0;
11719
4e0b83a5
VS
11720 ret = intel_plane_check_src_coordinates(plane_state);
11721 if (ret)
11722 return ret;
659056f2
VS
11723
11724 return 0;
11725}
11726
ddd5713d
VS
11727static unsigned int
11728i845_cursor_max_stride(struct intel_plane *plane,
11729 u32 pixel_format, u64 modifier,
11730 unsigned int rotation)
11731{
11732 return 2048;
11733}
11734
7eb31a0b
VS
11735static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11736{
5f29ab23
VS
11737 u32 cntl = 0;
11738
11739 if (crtc_state->gamma_enable)
11740 cntl |= CURSOR_GAMMA_ENABLE;
11741
11742 return cntl;
7eb31a0b
VS
11743}
11744
292889e1
VS
11745static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11746 const struct intel_plane_state *plane_state)
11747{
292889e1 11748 return CURSOR_ENABLE |
292889e1 11749 CURSOR_FORMAT_ARGB |
df79cf44 11750 CURSOR_STRIDE(plane_state->color_plane[0].stride);
292889e1
VS
11751}
11752
659056f2
VS
11753static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11754{
f90a85e7 11755 int width = drm_rect_width(&plane_state->uapi.dst);
659056f2
VS
11756
11757 /*
11758 * 845g/865g are only limited by the width of their cursors,
11759 * the height is arbitrary up to the precision of the register.
11760 */
3637ecf0 11761 return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
659056f2
VS
11762}
11763
eb0f5044 11764static int i845_check_cursor(struct intel_crtc_state *crtc_state,
659056f2
VS
11765 struct intel_plane_state *plane_state)
11766{
7b3cb17a 11767 const struct drm_framebuffer *fb = plane_state->hw.fb;
cd49f818 11768 struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
659056f2
VS
11769 int ret;
11770
11771 ret = intel_check_cursor(crtc_state, plane_state);
11772 if (ret)
11773 return ret;
11774
11775 /* if we want to turn off the cursor ignore width and height */
1e1bb871 11776 if (!fb)
659056f2
VS
11777 return 0;
11778
11779 /* Check for which cursor types we support */
11780 if (!i845_cursor_size_ok(plane_state)) {
cd49f818
WK
11781 drm_dbg_kms(&i915->drm,
11782 "Cursor dimension %dx%d not supported\n",
11783 drm_rect_width(&plane_state->uapi.dst),
11784 drm_rect_height(&plane_state->uapi.dst));
659056f2
VS
11785 return -EINVAL;
11786 }
11787
e57291c2
PB
11788 drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11789 plane_state->color_plane[0].stride != fb->pitches[0]);
df79cf44 11790
1e1bb871 11791 switch (fb->pitches[0]) {
292889e1
VS
11792 case 256:
11793 case 512:
11794 case 1024:
11795 case 2048:
11796 break;
1e1bb871 11797 default:
cd49f818
WK
11798 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11799 fb->pitches[0]);
1e1bb871 11800 return -EINVAL;
292889e1
VS
11801 }
11802
659056f2
VS
11803 plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11804
11805 return 0;
292889e1
VS
11806}
11807
b2d03b0d
VS
11808static void i845_update_cursor(struct intel_plane *plane,
11809 const struct intel_crtc_state *crtc_state,
55a08b3f 11810 const struct intel_plane_state *plane_state)
560b85bb 11811{
cd5dcbf1 11812 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
b2d03b0d
VS
11813 u32 cntl = 0, base = 0, pos = 0, size = 0;
11814 unsigned long irqflags;
560b85bb 11815
f90a85e7
ML
11816 if (plane_state && plane_state->uapi.visible) {
11817 unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11818 unsigned int height = drm_rect_height(&plane_state->uapi.dst);
dc41c154 11819
7eb31a0b
VS
11820 cntl = plane_state->ctl |
11821 i845_cursor_ctl_crtc(crtc_state);
11822
dc41c154 11823 size = (height << 12) | width;
560b85bb 11824
b2d03b0d
VS
11825 base = intel_cursor_base(plane_state);
11826 pos = intel_cursor_position(plane_state);
4b0e333e 11827 }
560b85bb 11828
b2d03b0d 11829 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4726e0b0 11830
e11ffddb
VS
11831 /* On these chipsets we can only modify the base/size/stride
11832 * whilst the cursor is disabled.
11833 */
11834 if (plane->cursor.base != base ||
11835 plane->cursor.size != size ||
11836 plane->cursor.cntl != cntl) {
dc008bf0
JN
11837 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11838 intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11839 intel_de_write_fw(dev_priv, CURSIZE, size);
11840 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11841 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
75343a44 11842
e11ffddb
VS
11843 plane->cursor.base = base;
11844 plane->cursor.size = size;
11845 plane->cursor.cntl = cntl;
11846 } else {
dc008bf0 11847 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
560b85bb 11848 }
e11ffddb 11849
b2d03b0d
VS
11850 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11851}
11852
11853static void i845_disable_cursor(struct intel_plane *plane,
0dd14be3 11854 const struct intel_crtc_state *crtc_state)
b2d03b0d 11855{
0dd14be3 11856 i845_update_cursor(plane, crtc_state, NULL);
560b85bb
CW
11857}
11858
eade6c89
VS
11859static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11860 enum pipe *pipe)
51f5a096
VS
11861{
11862 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11863 enum intel_display_power_domain power_domain;
0e6e0be4 11864 intel_wakeref_t wakeref;
51f5a096
VS
11865 bool ret;
11866
11867 power_domain = POWER_DOMAIN_PIPE(PIPE_A);
0e6e0be4
CW
11868 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11869 if (!wakeref)
51f5a096
VS
11870 return false;
11871
dc008bf0 11872 ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
51f5a096 11873
eade6c89
VS
11874 *pipe = PIPE_A;
11875
0e6e0be4 11876 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
11877
11878 return ret;
11879}
11880
ddd5713d
VS
11881static unsigned int
11882i9xx_cursor_max_stride(struct intel_plane *plane,
11883 u32 pixel_format, u64 modifier,
11884 unsigned int rotation)
11885{
11886 return plane->base.dev->mode_config.cursor_width * 4;
11887}
11888
7eb31a0b 11889static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
292889e1 11890{
2225f3c6 11891 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7eb31a0b 11892 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
c894d63c 11893 u32 cntl = 0;
292889e1 11894
7eb31a0b
VS
11895 if (INTEL_GEN(dev_priv) >= 11)
11896 return cntl;
e876b78c 11897
5f29ab23
VS
11898 if (crtc_state->gamma_enable)
11899 cntl = MCURSOR_GAMMA_ENABLE;
292889e1 11900
8271b2ef 11901 if (crtc_state->csc_enable)
7eb31a0b 11902 cntl |= MCURSOR_PIPE_CSC_ENABLE;
292889e1 11903
32ea06b6
VS
11904 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11905 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
292889e1 11906
7eb31a0b
VS
11907 return cntl;
11908}
11909
11910static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11911 const struct intel_plane_state *plane_state)
11912{
11913 struct drm_i915_private *dev_priv =
f90a85e7 11914 to_i915(plane_state->uapi.plane->dev);
7eb31a0b
VS
11915 u32 cntl = 0;
11916
11917 if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11918 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11919
f90a85e7 11920 switch (drm_rect_width(&plane_state->uapi.dst)) {
292889e1 11921 case 64:
b99b9ec1 11922 cntl |= MCURSOR_MODE_64_ARGB_AX;
292889e1
VS
11923 break;
11924 case 128:
b99b9ec1 11925 cntl |= MCURSOR_MODE_128_ARGB_AX;
292889e1
VS
11926 break;
11927 case 256:
b99b9ec1 11928 cntl |= MCURSOR_MODE_256_ARGB_AX;
292889e1
VS
11929 break;
11930 default:
f90a85e7 11931 MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
292889e1
VS
11932 return 0;
11933 }
11934
7b3cb17a 11935 if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
b99b9ec1 11936 cntl |= MCURSOR_ROTATE_180;
292889e1
VS
11937
11938 return cntl;
11939}
11940
659056f2 11941static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
65a21cd6 11942{
024faac7 11943 struct drm_i915_private *dev_priv =
f90a85e7
ML
11944 to_i915(plane_state->uapi.plane->dev);
11945 int width = drm_rect_width(&plane_state->uapi.dst);
11946 int height = drm_rect_height(&plane_state->uapi.dst);
4b0e333e 11947
3637ecf0 11948 if (!intel_cursor_size_ok(plane_state))
659056f2 11949 return false;
4398ad45 11950
024faac7
VS
11951 /* Cursor width is limited to a few power-of-two sizes */
11952 switch (width) {
659056f2
VS
11953 case 256:
11954 case 128:
659056f2
VS
11955 case 64:
11956 break;
11957 default:
11958 return false;
65a21cd6 11959 }
4b0e333e 11960
024faac7
VS
11961 /*
11962 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11963 * height from 8 lines up to the cursor width, when the
11964 * cursor is not rotated. Everything else requires square
11965 * cursors.
11966 */
11967 if (HAS_CUR_FBC(dev_priv) &&
7b3cb17a 11968 plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
024faac7
VS
11969 if (height < 8 || height > width)
11970 return false;
11971 } else {
11972 if (height != width)
11973 return false;
11974 }
99d1f387 11975
659056f2 11976 return true;
65a21cd6
JB
11977}
11978
eb0f5044 11979static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
659056f2 11980 struct intel_plane_state *plane_state)
cda4b7d3 11981{
f90a85e7 11982 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
659056f2 11983 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a 11984 const struct drm_framebuffer *fb = plane_state->hw.fb;
659056f2 11985 enum pipe pipe = plane->pipe;
659056f2 11986 int ret;
cda4b7d3 11987
659056f2
VS
11988 ret = intel_check_cursor(crtc_state, plane_state);
11989 if (ret)
11990 return ret;
cda4b7d3 11991
659056f2 11992 /* if we want to turn off the cursor ignore width and height */
1e1bb871 11993 if (!fb)
659056f2 11994 return 0;
55a08b3f 11995
659056f2
VS
11996 /* Check for which cursor types we support */
11997 if (!i9xx_cursor_size_ok(plane_state)) {
cd49f818
WK
11998 drm_dbg(&dev_priv->drm,
11999 "Cursor dimension %dx%d not supported\n",
12000 drm_rect_width(&plane_state->uapi.dst),
12001 drm_rect_height(&plane_state->uapi.dst));
659056f2 12002 return -EINVAL;
cda4b7d3 12003 }
cda4b7d3 12004
e57291c2
PB
12005 drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
12006 plane_state->color_plane[0].stride != fb->pitches[0]);
df79cf44 12007
3a612765 12008 if (fb->pitches[0] !=
f90a85e7 12009 drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
cd49f818
WK
12010 drm_dbg_kms(&dev_priv->drm,
12011 "Invalid cursor stride (%u) (cursor width %d)\n",
12012 fb->pitches[0],
12013 drm_rect_width(&plane_state->uapi.dst));
1e1bb871 12014 return -EINVAL;
659056f2 12015 }
dd584fc0 12016
659056f2
VS
12017 /*
12018 * There's something wrong with the cursor on CHV pipe C.
12019 * If it straddles the left edge of the screen then
12020 * moving it away from the edge or disabling it often
12021 * results in a pipe underrun, and often that can lead to
12022 * dead pipe (constant underrun reported, and it scans
12023 * out just a solid color). To recover from that, the
12024 * display power well must be turned off and on again.
12025 * Refuse the put the cursor into that compromised position.
12026 */
12027 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
f90a85e7 12028 plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
cd49f818
WK
12029 drm_dbg_kms(&dev_priv->drm,
12030 "CHV cursor C not allowed to straddle the left screen edge\n");
659056f2
VS
12031 return -EINVAL;
12032 }
5efb3e28 12033
659056f2 12034 plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
dd584fc0 12035
659056f2 12036 return 0;
cda4b7d3
CW
12037}
12038
b2d03b0d
VS
12039static void i9xx_update_cursor(struct intel_plane *plane,
12040 const struct intel_crtc_state *crtc_state,
55a08b3f 12041 const struct intel_plane_state *plane_state)
dc41c154 12042{
cd5dcbf1
VS
12043 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12044 enum pipe pipe = plane->pipe;
024faac7 12045 u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
b2d03b0d 12046 unsigned long irqflags;
dc41c154 12047
f90a85e7
ML
12048 if (plane_state && plane_state->uapi.visible) {
12049 unsigned width = drm_rect_width(&plane_state->uapi.dst);
12050 unsigned height = drm_rect_height(&plane_state->uapi.dst);
3a612765 12051
7eb31a0b
VS
12052 cntl = plane_state->ctl |
12053 i9xx_cursor_ctl_crtc(crtc_state);
dc41c154 12054
3a612765
ML
12055 if (width != height)
12056 fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
dc41c154 12057
b2d03b0d
VS
12058 base = intel_cursor_base(plane_state);
12059 pos = intel_cursor_position(plane_state);
12060 }
12061
12062 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
12063
e11ffddb
VS
12064 /*
12065 * On some platforms writing CURCNTR first will also
12066 * cause CURPOS to be armed by the CURBASE write.
12067 * Without the CURCNTR write the CURPOS write would
83234d13
VS
12068 * arm itself. Thus we always update CURCNTR before
12069 * CURPOS.
8753d2bc
VS
12070 *
12071 * On other platforms CURPOS always requires the
12072 * CURBASE write to arm the update. Additonally
12073 * a write to any of the cursor register will cancel
12074 * an already armed cursor update. Thus leaving out
12075 * the CURBASE write after CURPOS could lead to a
12076 * cursor that doesn't appear to move, or even change
12077 * shape. Thus we always write CURBASE.
e11ffddb 12078 *
83234d13
VS
12079 * The other registers are armed by by the CURBASE write
12080 * except when the plane is getting enabled at which time
12081 * the CURCNTR write arms the update.
e11ffddb 12082 */
ff43bc37
VS
12083
12084 if (INTEL_GEN(dev_priv) >= 9)
12085 skl_write_cursor_wm(plane, crtc_state);
12086
0bcbcba7
JRS
12087 if (!needs_modeset(crtc_state))
12088 intel_psr2_program_plane_sel_fetch(plane, crtc_state, plane_state, 0);
12089
e11ffddb
VS
12090 if (plane->cursor.base != base ||
12091 plane->cursor.size != fbc_ctl ||
12092 plane->cursor.cntl != cntl) {
e11ffddb 12093 if (HAS_CUR_FBC(dev_priv))
dc008bf0
JN
12094 intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
12095 fbc_ctl);
12096 intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
12097 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
12098 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
75343a44 12099
e11ffddb
VS
12100 plane->cursor.base = base;
12101 plane->cursor.size = fbc_ctl;
12102 plane->cursor.cntl = cntl;
dc41c154 12103 } else {
dc008bf0
JN
12104 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
12105 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
dc41c154
VS
12106 }
12107
b2d03b0d 12108 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
65a21cd6
JB
12109}
12110
b2d03b0d 12111static void i9xx_disable_cursor(struct intel_plane *plane,
0dd14be3 12112 const struct intel_crtc_state *crtc_state)
cda4b7d3 12113{
0dd14be3 12114 i9xx_update_cursor(plane, crtc_state, NULL);
dc41c154
VS
12115}
12116
eade6c89
VS
12117static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
12118 enum pipe *pipe)
51f5a096
VS
12119{
12120 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12121 enum intel_display_power_domain power_domain;
0e6e0be4 12122 intel_wakeref_t wakeref;
51f5a096 12123 bool ret;
eade6c89 12124 u32 val;
51f5a096
VS
12125
12126 /*
12127 * Not 100% correct for planes that can move between pipes,
12128 * but that's only the case for gen2-3 which don't have any
12129 * display power wells.
12130 */
eade6c89 12131 power_domain = POWER_DOMAIN_PIPE(plane->pipe);
0e6e0be4
CW
12132 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
12133 if (!wakeref)
51f5a096
VS
12134 return false;
12135
dc008bf0 12136 val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
eade6c89 12137
b99b9ec1 12138 ret = val & MCURSOR_MODE;
eade6c89
VS
12139
12140 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
12141 *pipe = plane->pipe;
12142 else
12143 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
12144 MCURSOR_PIPE_SELECT_SHIFT;
51f5a096 12145
0e6e0be4 12146 intel_display_power_put(dev_priv, power_domain, wakeref);
51f5a096
VS
12147
12148 return ret;
12149}
dc41c154 12150
79e53945 12151/* VESA 640x480x72Hz mode to set on the pipe */
bacdcd55 12152static const struct drm_display_mode load_detect_mode = {
79e53945
JB
12153 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
12154 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
12155};
12156
a8bb6818 12157struct drm_framebuffer *
24dbf51a
CW
12158intel_framebuffer_create(struct drm_i915_gem_object *obj,
12159 struct drm_mode_fb_cmd2 *mode_cmd)
d2dff872
CW
12160{
12161 struct intel_framebuffer *intel_fb;
12162 int ret;
12163
12164 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 12165 if (!intel_fb)
d2dff872 12166 return ERR_PTR(-ENOMEM);
d2dff872 12167
24dbf51a 12168 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
dd4916c5
DV
12169 if (ret)
12170 goto err;
d2dff872
CW
12171
12172 return &intel_fb->base;
dcb1394e 12173
dd4916c5 12174err:
dd4916c5 12175 kfree(intel_fb);
dd4916c5 12176 return ERR_PTR(ret);
d2dff872
CW
12177}
12178
20bdc112
VS
12179static int intel_modeset_disable_planes(struct drm_atomic_state *state,
12180 struct drm_crtc *crtc)
d3a40d1b 12181{
20bdc112 12182 struct drm_plane *plane;
d3a40d1b 12183 struct drm_plane_state *plane_state;
20bdc112 12184 int ret, i;
d3a40d1b 12185
20bdc112 12186 ret = drm_atomic_add_affected_planes(state, crtc);
d3a40d1b
ACO
12187 if (ret)
12188 return ret;
20bdc112
VS
12189
12190 for_each_new_plane_in_state(state, plane, plane_state, i) {
12191 if (plane_state->crtc != crtc)
12192 continue;
12193
12194 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
12195 if (ret)
12196 return ret;
12197
12198 drm_atomic_set_fb_for_plane(plane_state, NULL);
12199 }
d3a40d1b
ACO
12200
12201 return 0;
12202}
12203
6c5ed5ae 12204int intel_get_load_detect_pipe(struct drm_connector *connector,
6c5ed5ae
ML
12205 struct intel_load_detect_pipe *old,
12206 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
12207{
12208 struct intel_crtc *intel_crtc;
d2434ab7 12209 struct intel_encoder *intel_encoder =
43a6d19c 12210 intel_attached_encoder(to_intel_connector(connector));
79e53945 12211 struct drm_crtc *possible_crtc;
4ef69c7a 12212 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
12213 struct drm_crtc *crtc = NULL;
12214 struct drm_device *dev = encoder->dev;
0f0f74bc 12215 struct drm_i915_private *dev_priv = to_i915(dev);
51fd371b 12216 struct drm_mode_config *config = &dev->mode_config;
edde3617 12217 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 12218 struct drm_connector_state *connector_state;
4be07317 12219 struct intel_crtc_state *crtc_state;
51fd371b 12220 int ret, i = -1;
79e53945 12221
cd49f818
WK
12222 drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12223 connector->base.id, connector->name,
12224 encoder->base.id, encoder->name);
d2dff872 12225
edde3617
ML
12226 old->restore_state = NULL;
12227
e57291c2 12228 drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
6e9f798d 12229
79e53945
JB
12230 /*
12231 * Algorithm gets a little messy:
7a5e4805 12232 *
79e53945
JB
12233 * - if the connector already has an assigned crtc, use it (but make
12234 * sure it's on first)
7a5e4805 12235 *
79e53945
JB
12236 * - try to find the first unused crtc that can drive this connector,
12237 * and use that if we find one
79e53945
JB
12238 */
12239
12240 /* See if we already have a CRTC for this connector */
edde3617
ML
12241 if (connector->state->crtc) {
12242 crtc = connector->state->crtc;
8261b191 12243
51fd371b 12244 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 12245 if (ret)
ad3c558f 12246 goto fail;
8261b191
CW
12247
12248 /* Make sure the crtc and connector are running */
edde3617 12249 goto found;
79e53945
JB
12250 }
12251
12252 /* Find an unused one (if possible) */
70e1e0ec 12253 for_each_crtc(dev, possible_crtc) {
79e53945
JB
12254 i++;
12255 if (!(encoder->possible_crtcs & (1 << i)))
12256 continue;
edde3617
ML
12257
12258 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
12259 if (ret)
12260 goto fail;
12261
12262 if (possible_crtc->state->enable) {
12263 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 12264 continue;
edde3617 12265 }
a459249c
VS
12266
12267 crtc = possible_crtc;
12268 break;
79e53945
JB
12269 }
12270
12271 /*
12272 * If we didn't find an unused CRTC, don't use any.
12273 */
12274 if (!crtc) {
cd49f818
WK
12275 drm_dbg_kms(&dev_priv->drm,
12276 "no pipe available for load-detect\n");
f4bf77b4 12277 ret = -ENODEV;
ad3c558f 12278 goto fail;
79e53945
JB
12279 }
12280
edde3617
ML
12281found:
12282 intel_crtc = to_intel_crtc(crtc);
12283
83a57153 12284 state = drm_atomic_state_alloc(dev);
edde3617
ML
12285 restore_state = drm_atomic_state_alloc(dev);
12286 if (!state || !restore_state) {
12287 ret = -ENOMEM;
12288 goto fail;
12289 }
83a57153
ACO
12290
12291 state->acquire_ctx = ctx;
edde3617 12292 restore_state->acquire_ctx = ctx;
83a57153 12293
944b0c76
ACO
12294 connector_state = drm_atomic_get_connector_state(state, connector);
12295 if (IS_ERR(connector_state)) {
12296 ret = PTR_ERR(connector_state);
12297 goto fail;
12298 }
12299
edde3617
ML
12300 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
12301 if (ret)
12302 goto fail;
944b0c76 12303
4be07317
ACO
12304 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12305 if (IS_ERR(crtc_state)) {
12306 ret = PTR_ERR(crtc_state);
12307 goto fail;
12308 }
12309
aa42a50a 12310 crtc_state->uapi.active = true;
4be07317 12311
2225f3c6 12312 ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
25f89954 12313 &load_detect_mode);
d3a40d1b
ACO
12314 if (ret)
12315 goto fail;
12316
20bdc112 12317 ret = intel_modeset_disable_planes(state, crtc);
edde3617
ML
12318 if (ret)
12319 goto fail;
12320
12321 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12322 if (!ret)
12323 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
be90cc31
VS
12324 if (!ret)
12325 ret = drm_atomic_add_affected_planes(restore_state, crtc);
edde3617 12326 if (ret) {
cd49f818
WK
12327 drm_dbg_kms(&dev_priv->drm,
12328 "Failed to create a copy of old state to restore: %i\n",
12329 ret);
edde3617
ML
12330 goto fail;
12331 }
8c7b5ccb 12332
3ba86073
ML
12333 ret = drm_atomic_commit(state);
12334 if (ret) {
cd49f818
WK
12335 drm_dbg_kms(&dev_priv->drm,
12336 "failed to set mode on load-detect pipe\n");
412b61d8 12337 goto fail;
79e53945 12338 }
edde3617
ML
12339
12340 old->restore_state = restore_state;
7abbd11f 12341 drm_atomic_state_put(state);
7173188d 12342
79e53945 12343 /* let the connector get through one full cycle before testing */
0f0f74bc 12344 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 12345 return true;
412b61d8 12346
ad3c558f 12347fail:
7fb71c8f
CW
12348 if (state) {
12349 drm_atomic_state_put(state);
12350 state = NULL;
12351 }
12352 if (restore_state) {
12353 drm_atomic_state_put(restore_state);
12354 restore_state = NULL;
12355 }
83a57153 12356
6c5ed5ae
ML
12357 if (ret == -EDEADLK)
12358 return ret;
51fd371b 12359
412b61d8 12360 return false;
79e53945
JB
12361}
12362
d2434ab7 12363void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
12364 struct intel_load_detect_pipe *old,
12365 struct drm_modeset_acquire_ctx *ctx)
79e53945 12366{
d2434ab7 12367 struct intel_encoder *intel_encoder =
43a6d19c 12368 intel_attached_encoder(to_intel_connector(connector));
cd49f818 12369 struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
4ef69c7a 12370 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 12371 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 12372 int ret;
79e53945 12373
cd49f818
WK
12374 drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12375 connector->base.id, connector->name,
12376 encoder->base.id, encoder->name);
d2dff872 12377
edde3617 12378 if (!state)
0622a53c 12379 return;
79e53945 12380
581e49fe 12381 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
0853695c 12382 if (ret)
cd49f818
WK
12383 drm_dbg_kms(&i915->drm,
12384 "Couldn't release load detect pipe: %i\n", ret);
0853695c 12385 drm_atomic_state_put(state);
79e53945
JB
12386}
12387
da4a1efa 12388static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 12389 const struct intel_crtc_state *pipe_config)
da4a1efa 12390{
fac5e23e 12391 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
12392 u32 dpll = pipe_config->dpll_hw_state.dpll;
12393
12394 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 12395 return dev_priv->vbt.lvds_ssc_freq;
6e266956 12396 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 12397 return 120000;
cf819eff 12398 else if (!IS_GEN(dev_priv, 2))
da4a1efa
VS
12399 return 96000;
12400 else
12401 return 48000;
12402}
12403
79e53945 12404/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 12405static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 12406 struct intel_crtc_state *pipe_config)
79e53945 12407{
f1f644dc 12408 struct drm_device *dev = crtc->base.dev;
fac5e23e 12409 struct drm_i915_private *dev_priv = to_i915(dev);
d048a268 12410 enum pipe pipe = crtc->pipe;
293623f7 12411 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 12412 u32 fp;
9e2c8475 12413 struct dpll clock;
dccbea3b 12414 int port_clock;
da4a1efa 12415 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
12416
12417 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 12418 fp = pipe_config->dpll_hw_state.fp0;
79e53945 12419 else
293623f7 12420 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
12421
12422 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 12423 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
12424 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12425 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
12426 } else {
12427 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12428 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12429 }
12430
cf819eff 12431 if (!IS_GEN(dev_priv, 2)) {
9b1e14f4 12432 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
12433 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12434 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
12435 else
12436 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
12437 DPLL_FPA01_P1_POST_DIV_SHIFT);
12438
12439 switch (dpll & DPLL_MODE_MASK) {
12440 case DPLLB_MODE_DAC_SERIAL:
12441 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12442 5 : 10;
12443 break;
12444 case DPLLB_MODE_LVDS:
12445 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12446 7 : 14;
12447 break;
12448 default:
cd49f818
WK
12449 drm_dbg_kms(&dev_priv->drm,
12450 "Unknown DPLL mode %08x in programmed "
12451 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 12452 return;
79e53945
JB
12453 }
12454
9b1e14f4 12455 if (IS_PINEVIEW(dev_priv))
dccbea3b 12456 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 12457 else
dccbea3b 12458 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 12459 } else {
dc008bf0
JN
12460 u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12461 LVDS);
b1c560d1 12462 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
12463
12464 if (is_lvds) {
12465 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12466 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
12467
12468 if (lvds & LVDS_CLKB_POWER_UP)
12469 clock.p2 = 7;
12470 else
12471 clock.p2 = 14;
79e53945
JB
12472 } else {
12473 if (dpll & PLL_P1_DIVIDE_BY_TWO)
12474 clock.p1 = 2;
12475 else {
12476 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12477 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12478 }
12479 if (dpll & PLL_P2_DIVIDE_BY_4)
12480 clock.p2 = 4;
12481 else
12482 clock.p2 = 2;
79e53945 12483 }
da4a1efa 12484
dccbea3b 12485 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
12486 }
12487
18442d08
VS
12488 /*
12489 * This value includes pixel_multiplier. We will use
241bfc38 12490 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
12491 * encoder's get_config() function.
12492 */
dccbea3b 12493 pipe_config->port_clock = port_clock;
f1f644dc
JB
12494}
12495
6878da05
VS
12496int intel_dotclock_calculate(int link_freq,
12497 const struct intel_link_m_n *m_n)
f1f644dc 12498{
f1f644dc
JB
12499 /*
12500 * The calculation for the data clock is:
1041a02f 12501 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 12502 * But we want to avoid losing precison if possible, so:
1041a02f 12503 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
12504 *
12505 * and the link clock is simpler:
1041a02f 12506 * link_clock = (m * link_clock) / n
f1f644dc
JB
12507 */
12508
6878da05
VS
12509 if (!m_n->link_n)
12510 return 0;
f1f644dc 12511
3123698f 12512 return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6878da05 12513}
f1f644dc 12514
9eae5e27
LDM
12515static void ilk_pch_clock_get(struct intel_crtc *crtc,
12516 struct intel_crtc_state *pipe_config)
6878da05 12517{
e3b247da 12518 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 12519
18442d08
VS
12520 /* read out port_clock from the DPLL */
12521 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 12522
f1f644dc 12523 /*
e3b247da
VS
12524 * In case there is an active pipe without active ports,
12525 * we may need some idea for the dotclock anyway.
12526 * Calculate one based on the FDI configuration.
79e53945 12527 */
1326a92c 12528 pipe_config->hw.adjusted_mode.crtc_clock =
21a727b3 12529 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 12530 &pipe_config->fdi_m_n);
79e53945
JB
12531}
12532
979e94c1
VS
12533static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12534 struct intel_crtc *crtc)
12535{
12536 memset(crtc_state, 0, sizeof(*crtc_state));
12537
12538 __drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12539
12540 crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12541 crtc_state->master_transcoder = INVALID_TRANSCODER;
12542 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12543 crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12544 crtc_state->scaler_state.scaler_id = -1;
6671c367 12545 crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
979e94c1
VS
12546}
12547
216383e9
VS
12548static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12549{
12550 struct intel_crtc_state *crtc_state;
12551
12552 crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12553
12554 if (crtc_state)
12555 intel_crtc_state_reset(crtc_state, crtc);
12556
12557 return crtc_state;
12558}
12559
de330815
VS
12560/* Returns the currently programmed mode of the given encoder. */
12561struct drm_display_mode *
12562intel_encoder_current_mode(struct intel_encoder *encoder)
79e53945 12563{
de330815
VS
12564 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12565 struct intel_crtc_state *crtc_state;
79e53945 12566 struct drm_display_mode *mode;
de330815
VS
12567 struct intel_crtc *crtc;
12568 enum pipe pipe;
12569
12570 if (!encoder->get_hw_state(encoder, &pipe))
12571 return NULL;
12572
12573 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
79e53945
JB
12574
12575 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12576 if (!mode)
12577 return NULL;
12578
216383e9 12579 crtc_state = intel_crtc_state_alloc(crtc);
de330815 12580 if (!crtc_state) {
3f36b937
TU
12581 kfree(mode);
12582 return NULL;
12583 }
12584
11f9af16 12585 if (!intel_crtc_get_pipe_config(crtc_state)) {
de330815
VS
12586 kfree(crtc_state);
12587 kfree(mode);
12588 return NULL;
12589 }
e30a154b 12590
65c1ed30 12591 intel_encoder_get_config(encoder, crtc_state);
79e53945 12592
33574ec9 12593 intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
79e53945 12594
de330815 12595 kfree(crtc_state);
3f36b937 12596
79e53945
JB
12597 return mode;
12598}
12599
12600static void intel_crtc_destroy(struct drm_crtc *crtc)
12601{
12602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12603
12604 drm_crtc_cleanup(crtc);
12605 kfree(intel_crtc);
12606}
12607
5a21b665
DV
12608/**
12609 * intel_wm_need_update - Check whether watermarks need updating
6bf19817
CW
12610 * @cur: current plane state
12611 * @new: new plane state
5a21b665
DV
12612 *
12613 * Check current plane state versus the new one to determine whether
12614 * watermarks need to be recalculated.
12615 *
12616 * Returns true or false.
12617 */
4f25720b 12618static bool intel_wm_need_update(const struct intel_plane_state *cur,
cd1d3ee9 12619 struct intel_plane_state *new)
5a21b665 12620{
5a21b665 12621 /* Update watermarks on tiling or size changes. */
f90a85e7 12622 if (new->uapi.visible != cur->uapi.visible)
5a21b665
DV
12623 return true;
12624
7b3cb17a 12625 if (!cur->hw.fb || !new->hw.fb)
5a21b665
DV
12626 return false;
12627
7b3cb17a
ML
12628 if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12629 cur->hw.rotation != new->hw.rotation ||
f90a85e7
ML
12630 drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12631 drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12632 drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12633 drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
5a21b665
DV
12634 return true;
12635
12636 return false;
12637}
12638
b2b55502 12639static bool needs_scaling(const struct intel_plane_state *state)
5a21b665 12640{
f90a85e7
ML
12641 int src_w = drm_rect_width(&state->uapi.src) >> 16;
12642 int src_h = drm_rect_height(&state->uapi.src) >> 16;
12643 int dst_w = drm_rect_width(&state->uapi.dst);
12644 int dst_h = drm_rect_height(&state->uapi.dst);
5a21b665
DV
12645
12646 return (src_w != dst_w || src_h != dst_h);
12647}
d21fbe87 12648
b2b55502 12649int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
4f25720b 12650 struct intel_crtc_state *crtc_state,
b2b55502 12651 const struct intel_plane_state *old_plane_state,
4f25720b 12652 struct intel_plane_state *plane_state)
da20eabd 12653{
2225f3c6 12654 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
f90a85e7 12655 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4f25720b
ML
12656 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12657 bool mode_changed = needs_modeset(crtc_state);
1326a92c
ML
12658 bool was_crtc_enabled = old_crtc_state->hw.active;
12659 bool is_crtc_enabled = crtc_state->hw.active;
da20eabd 12660 bool turn_off, turn_on, visible, was_visible;
78108b7c 12661 int ret;
da20eabd 12662
e9728bd8 12663 if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
4f25720b 12664 ret = skl_update_scaler_plane(crtc_state, plane_state);
da20eabd
ML
12665 if (ret)
12666 return ret;
12667 }
12668
f90a85e7
ML
12669 was_visible = old_plane_state->uapi.visible;
12670 visible = plane_state->uapi.visible;
da20eabd 12671
e57291c2 12672 if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
da20eabd
ML
12673 was_visible = false;
12674
35c08f43
ML
12675 /*
12676 * Visibility is calculated as if the crtc was on, but
12677 * after scaler setup everything depends on it being off
12678 * when the crtc isn't active.
f818ffea
VS
12679 *
12680 * FIXME this is wrong for watermarks. Watermarks should also
12681 * be computed as if the pipe would be active. Perhaps move
12682 * per-plane wm computation to the .check_plane() hook, and
12683 * only combine the results from all planes in the current place?
35c08f43 12684 */
e9728bd8 12685 if (!is_crtc_enabled) {
cb1824bb
VS
12686 intel_plane_set_invisible(crtc_state, plane_state);
12687 visible = false;
e9728bd8 12688 }
da20eabd
ML
12689
12690 if (!was_visible && !visible)
12691 return 0;
12692
12693 turn_off = was_visible && (!visible || mode_changed);
12694 turn_on = visible && (!was_visible || mode_changed);
12695
cd49f818
WK
12696 drm_dbg_atomic(&dev_priv->drm,
12697 "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12698 crtc->base.base.id, crtc->base.name,
12699 plane->base.base.id, plane->base.name,
12700 was_visible, visible,
12701 turn_off, turn_on, mode_changed);
da20eabd 12702
caed361d 12703 if (turn_on) {
04548cba 12704 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
4f25720b 12705 crtc_state->update_wm_pre = true;
caed361d
VS
12706
12707 /* must disable cxsr around plane enable/disable */
e9728bd8 12708 if (plane->id != PLANE_CURSOR)
4f25720b 12709 crtc_state->disable_cxsr = true;
caed361d 12710 } else if (turn_off) {
04548cba 12711 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
4f25720b 12712 crtc_state->update_wm_post = true;
92826fcd 12713
852eb00d 12714 /* must disable cxsr around plane enable/disable */
e9728bd8 12715 if (plane->id != PLANE_CURSOR)
4f25720b
ML
12716 crtc_state->disable_cxsr = true;
12717 } else if (intel_wm_need_update(old_plane_state, plane_state)) {
04548cba 12718 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
b4ede6df 12719 /* FIXME bollocks */
4f25720b
ML
12720 crtc_state->update_wm_pre = true;
12721 crtc_state->update_wm_post = true;
b4ede6df 12722 }
852eb00d 12723 }
da20eabd 12724
8be6ca85 12725 if (visible || was_visible)
4f25720b 12726 crtc_state->fb_bits |= plane->frontbuffer_bit;
a9ff8714 12727
31ae71fc 12728 /*
8e7a4424
VS
12729 * ILK/SNB DVSACNTR/Sprite Enable
12730 * IVB SPR_CTL/Sprite Enable
12731 * "When in Self Refresh Big FIFO mode, a write to enable the
12732 * plane will be internally buffered and delayed while Big FIFO
12733 * mode is exiting."
12734 *
12735 * Which means that enabling the sprite can take an extra frame
12736 * when we start in big FIFO mode (LP1+). Thus we need to drop
12737 * down to LP0 and wait for vblank in order to make sure the
12738 * sprite gets enabled on the next vblank after the register write.
12739 * Doing otherwise would risk enabling the sprite one frame after
12740 * we've already signalled flip completion. We can resume LP1+
12741 * once the sprite has been enabled.
12742 *
12743 *
31ae71fc 12744 * WaCxSRDisabledForSpriteScaling:ivb
8e7a4424
VS
12745 * IVB SPR_SCALE/Scaling Enable
12746 * "Low Power watermarks must be disabled for at least one
12747 * frame before enabling sprite scaling, and kept disabled
12748 * until sprite scaling is disabled."
12749 *
12750 * ILK/SNB DVSASCALE/Scaling Enable
12751 * "When in Self Refresh Big FIFO mode, scaling enable will be
12752 * masked off while Big FIFO mode is exiting."
31ae71fc 12753 *
8e7a4424
VS
12754 * Despite the w/a only being listed for IVB we assume that
12755 * the ILK/SNB note has similar ramifications, hence we apply
12756 * the w/a on all three platforms.
d8af3270
JPH
12757 *
12758 * With experimental results seems this is needed also for primary
12759 * plane, not only sprite plane.
31ae71fc 12760 */
d8af3270 12761 if (plane->id != PLANE_CURSOR &&
f3ce44a0 12762 (IS_GEN_RANGE(dev_priv, 5, 6) ||
8e7a4424
VS
12763 IS_IVYBRIDGE(dev_priv)) &&
12764 (turn_on || (!needs_scaling(old_plane_state) &&
4f25720b
ML
12765 needs_scaling(plane_state))))
12766 crtc_state->disable_lp_wm = true;
d21fbe87 12767
da20eabd
ML
12768 return 0;
12769}
12770
6d3a1ce7
ML
12771static bool encoders_cloneable(const struct intel_encoder *a,
12772 const struct intel_encoder *b)
12773{
12774 /* masks could be asymmetric, so check both ways */
12775 return a == b || (a->cloneable & (1 << b->type) &&
12776 b->cloneable & (1 << a->type));
12777}
12778
aa07c1d3 12779static bool check_single_encoder_cloning(struct intel_atomic_state *state,
6d3a1ce7
ML
12780 struct intel_crtc *crtc,
12781 struct intel_encoder *encoder)
12782{
12783 struct intel_encoder *source_encoder;
12784 struct drm_connector *connector;
12785 struct drm_connector_state *connector_state;
12786 int i;
12787
aa07c1d3 12788 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
6d3a1ce7
ML
12789 if (connector_state->crtc != &crtc->base)
12790 continue;
12791
12792 source_encoder =
12793 to_intel_encoder(connector_state->best_encoder);
12794 if (!encoders_cloneable(encoder, source_encoder))
12795 return false;
12796 }
12797
12798 return true;
12799}
12800
1ab554b0
ML
12801static int icl_add_linked_planes(struct intel_atomic_state *state)
12802{
12803 struct intel_plane *plane, *linked;
12804 struct intel_plane_state *plane_state, *linked_plane_state;
12805 int i;
12806
12807 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
c47b7ddb 12808 linked = plane_state->planar_linked_plane;
1ab554b0
ML
12809
12810 if (!linked)
12811 continue;
12812
12813 linked_plane_state = intel_atomic_get_plane_state(state, linked);
12814 if (IS_ERR(linked_plane_state))
12815 return PTR_ERR(linked_plane_state);
12816
ce04ecd9
PB
12817 drm_WARN_ON(state->base.dev,
12818 linked_plane_state->planar_linked_plane != plane);
12819 drm_WARN_ON(state->base.dev,
12820 linked_plane_state->planar_slave == plane_state->planar_slave);
1ab554b0
ML
12821 }
12822
12823 return 0;
12824}
12825
12826static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12827{
2225f3c6 12828 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1ab554b0 12829 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2225f3c6 12830 struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
1ab554b0
ML
12831 struct intel_plane *plane, *linked;
12832 struct intel_plane_state *plane_state;
12833 int i;
12834
12835 if (INTEL_GEN(dev_priv) < 11)
12836 return 0;
12837
12838 /*
12839 * Destroy all old plane links and make the slave plane invisible
12840 * in the crtc_state->active_planes mask.
12841 */
12842 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
c47b7ddb 12843 if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
1ab554b0
ML
12844 continue;
12845
c47b7ddb 12846 plane_state->planar_linked_plane = NULL;
f90a85e7 12847 if (plane_state->planar_slave && !plane_state->uapi.visible) {
1ab554b0 12848 crtc_state->active_planes &= ~BIT(plane->id);
afbd8a72
VS
12849 crtc_state->update_planes |= BIT(plane->id);
12850 }
1ab554b0 12851
c47b7ddb 12852 plane_state->planar_slave = false;
1ab554b0
ML
12853 }
12854
12855 if (!crtc_state->nv12_planes)
12856 return 0;
12857
12858 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12859 struct intel_plane_state *linked_state = NULL;
12860
12861 if (plane->pipe != crtc->pipe ||
12862 !(crtc_state->nv12_planes & BIT(plane->id)))
12863 continue;
12864
12865 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
99e2d8bc 12866 if (!icl_is_nv12_y_plane(dev_priv, linked->id))
1ab554b0
ML
12867 continue;
12868
12869 if (crtc_state->active_planes & BIT(linked->id))
12870 continue;
12871
12872 linked_state = intel_atomic_get_plane_state(state, linked);
12873 if (IS_ERR(linked_state))
12874 return PTR_ERR(linked_state);
12875
12876 break;
12877 }
12878
12879 if (!linked_state) {
cd49f818
WK
12880 drm_dbg_kms(&dev_priv->drm,
12881 "Need %d free Y planes for planar YUV\n",
12882 hweight8(crtc_state->nv12_planes));
1ab554b0
ML
12883
12884 return -EINVAL;
12885 }
12886
c47b7ddb 12887 plane_state->planar_linked_plane = linked;
1ab554b0 12888
c47b7ddb
ML
12889 linked_state->planar_slave = true;
12890 linked_state->planar_linked_plane = plane;
1ab554b0 12891 crtc_state->active_planes |= BIT(linked->id);
afbd8a72 12892 crtc_state->update_planes |= BIT(linked->id);
cd49f818
WK
12893 drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12894 linked->base.name, plane->base.name);
1f594b20
ML
12895
12896 /* Copy parameters to slave plane */
12897 linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12898 linked_state->color_ctl = plane_state->color_ctl;
103605e0 12899 linked_state->view = plane_state->view;
320625aa
ID
12900 memcpy(linked_state->color_plane, plane_state->color_plane,
12901 sizeof(linked_state->color_plane));
1f594b20 12902
e85e7458 12903 intel_plane_copy_hw_state(linked_state, plane_state);
1f594b20
ML
12904 linked_state->uapi.src = plane_state->uapi.src;
12905 linked_state->uapi.dst = plane_state->uapi.dst;
12906
12907 if (icl_is_hdr_plane(dev_priv, plane->id)) {
12908 if (linked->id == PLANE_SPRITE5)
12909 plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12910 else if (linked->id == PLANE_SPRITE4)
12911 plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
99e2d8bc
MR
12912 else if (linked->id == PLANE_SPRITE3)
12913 plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
12914 else if (linked->id == PLANE_SPRITE2)
12915 plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
1f594b20
ML
12916 else
12917 MISSING_CASE(linked->id);
12918 }
1ab554b0
ML
12919 }
12920
12921 return 0;
12922}
12923
638d87c4
VS
12924static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12925{
2225f3c6 12926 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
638d87c4 12927 struct intel_atomic_state *state =
2225f3c6 12928 to_intel_atomic_state(new_crtc_state->uapi.state);
638d87c4
VS
12929 const struct intel_crtc_state *old_crtc_state =
12930 intel_atomic_get_old_crtc_state(state, crtc);
12931
12932 return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12933}
12934
6dcde047
VS
12935static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12936{
bafcdad6
ML
12937 const struct drm_display_mode *pipe_mode =
12938 &crtc_state->hw.pipe_mode;
4003dac1 12939 int linetime_wm;
6dcde047
VS
12940
12941 if (!crtc_state->hw.enable)
12942 return 0;
12943
bafcdad6
ML
12944 linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
12945 pipe_mode->crtc_clock);
4003dac1
VS
12946
12947 return min(linetime_wm, 0x1ff);
6dcde047
VS
12948}
12949
28a30b45
VS
12950static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12951 const struct intel_cdclk_state *cdclk_state)
6dcde047 12952{
bafcdad6
ML
12953 const struct drm_display_mode *pipe_mode =
12954 &crtc_state->hw.pipe_mode;
4003dac1 12955 int linetime_wm;
6dcde047
VS
12956
12957 if (!crtc_state->hw.enable)
12958 return 0;
12959
bafcdad6 12960 linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4003dac1
VS
12961 cdclk_state->logical.cdclk);
12962
12963 return min(linetime_wm, 0x1ff);
6dcde047
VS
12964}
12965
12966static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12967{
12968 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12969 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bafcdad6
ML
12970 const struct drm_display_mode *pipe_mode =
12971 &crtc_state->hw.pipe_mode;
4003dac1 12972 int linetime_wm;
6dcde047
VS
12973
12974 if (!crtc_state->hw.enable)
12975 return 0;
12976
bafcdad6 12977 linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
6dcde047
VS
12978 crtc_state->pixel_rate);
12979
12980 /* Display WA #1135: BXT:ALL GLK:ALL */
12981 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12982 linetime_wm /= 2;
12983
4003dac1 12984 return min(linetime_wm, 0x1ff);
6dcde047
VS
12985}
12986
28a30b45
VS
12987static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12988 struct intel_crtc *crtc)
12989{
12990 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12991 struct intel_crtc_state *crtc_state =
12992 intel_atomic_get_new_crtc_state(state, crtc);
12993 const struct intel_cdclk_state *cdclk_state;
12994
12995 if (INTEL_GEN(dev_priv) >= 9)
12996 crtc_state->linetime = skl_linetime_wm(crtc_state);
12997 else
12998 crtc_state->linetime = hsw_linetime_wm(crtc_state);
12999
13000 if (!hsw_crtc_supports_ips(crtc))
13001 return 0;
13002
13003 cdclk_state = intel_atomic_get_cdclk_state(state);
13004 if (IS_ERR(cdclk_state))
13005 return PTR_ERR(cdclk_state);
13006
13007 crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
13008 cdclk_state);
13009
13010 return 0;
13011}
13012
131d3b1a
VS
13013static int intel_crtc_atomic_check(struct intel_atomic_state *state,
13014 struct intel_crtc *crtc)
6d3a1ce7 13015{
2e7f76c1
VS
13016 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13017 struct intel_crtc_state *crtc_state =
131d3b1a 13018 intel_atomic_get_new_crtc_state(state, crtc);
2e7f76c1 13019 bool mode_changed = needs_modeset(crtc_state);
131d3b1a 13020 int ret;
6d3a1ce7 13021
440e84a5 13022 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
1326a92c 13023 mode_changed && !crtc_state->hw.active)
2e7f76c1 13024 crtc_state->update_wm_post = true;
eddfcbcd 13025
1326a92c 13026 if (mode_changed && crtc_state->hw.enable &&
ad421372 13027 dev_priv->display.crtc_compute_clock &&
19f65a3d 13028 !crtc_state->bigjoiner_slave &&
e57291c2 13029 !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
2e7f76c1 13030 ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
ad421372
ML
13031 if (ret)
13032 return ret;
13033 }
13034
638d87c4
VS
13035 /*
13036 * May need to update pipe gamma enable bits
13037 * when C8 planes are getting enabled/disabled.
13038 */
2e7f76c1 13039 if (c8_planes_changed(crtc_state))
2225f3c6 13040 crtc_state->uapi.color_mgmt_changed = true;
638d87c4 13041
2e7f76c1 13042 if (mode_changed || crtc_state->update_pipe ||
2225f3c6 13043 crtc_state->uapi.color_mgmt_changed) {
2e7f76c1 13044 ret = intel_color_check(crtc_state);
82cf435b
LL
13045 if (ret)
13046 return ret;
13047 }
13048
86c8bbbe 13049 if (dev_priv->display.compute_pipe_wm) {
2e7f76c1 13050 ret = dev_priv->display.compute_pipe_wm(crtc_state);
ed4a6a7c 13051 if (ret) {
cd49f818
WK
13052 drm_dbg_kms(&dev_priv->drm,
13053 "Target pipe watermarks are invalid\n");
ed4a6a7c
MR
13054 return ret;
13055 }
13056 }
13057
f255c624 13058 if (dev_priv->display.compute_intermediate_wm) {
e57291c2
PB
13059 if (drm_WARN_ON(&dev_priv->drm,
13060 !dev_priv->display.compute_pipe_wm))
ed4a6a7c
MR
13061 return 0;
13062
13063 /*
13064 * Calculate 'intermediate' watermarks that satisfy both the
13065 * old state and the new state. We can program these
13066 * immediately.
13067 */
2e7f76c1 13068 ret = dev_priv->display.compute_intermediate_wm(crtc_state);
ed4a6a7c 13069 if (ret) {
cd49f818
WK
13070 drm_dbg_kms(&dev_priv->drm,
13071 "No valid intermediate pipe watermarks are possible\n");
86c8bbbe 13072 return ret;
ed4a6a7c 13073 }
86c8bbbe
MR
13074 }
13075
6315b5d3 13076 if (INTEL_GEN(dev_priv) >= 9) {
40d42793 13077 if (mode_changed || crtc_state->update_pipe) {
2e7f76c1 13078 ret = skl_update_scaler_crtc(crtc_state);
40d42793
VS
13079 if (ret)
13080 return ret;
13081 }
13082
13083 ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
28a30b45
VS
13084 if (ret)
13085 return ret;
e435d6e5
ML
13086 }
13087
28a30b45
VS
13088 if (HAS_IPS(dev_priv)) {
13089 ret = hsw_compute_ips_config(crtc_state);
13090 if (ret)
13091 return ret;
13092 }
13093
13094 if (INTEL_GEN(dev_priv) >= 9 ||
13095 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
13096 ret = hsw_compute_linetime_wm(state, crtc);
13097 if (ret)
13098 return ret;
24f28450 13099
6dcde047
VS
13100 }
13101
0bcbcba7
JRS
13102 if (!mode_changed) {
13103 ret = intel_psr2_sel_fetch_update(state, crtc);
13104 if (ret)
13105 return ret;
13106 }
6e43e276 13107
28a30b45 13108 return 0;
6d3a1ce7
ML
13109}
13110
d29b2f9d
ACO
13111static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
13112{
13113 struct intel_connector *connector;
f9e905ca 13114 struct drm_connector_list_iter conn_iter;
d29b2f9d 13115
f9e905ca
DV
13116 drm_connector_list_iter_begin(dev, &conn_iter);
13117 for_each_intel_connector_iter(connector, &conn_iter) {
8863dc7f 13118 if (connector->base.state->crtc)
ef196b5c 13119 drm_connector_put(&connector->base);
8863dc7f 13120
d29b2f9d
ACO
13121 if (connector->base.encoder) {
13122 connector->base.state->best_encoder =
13123 connector->base.encoder;
13124 connector->base.state->crtc =
13125 connector->base.encoder->crtc;
8863dc7f 13126
ef196b5c 13127 drm_connector_get(&connector->base);
d29b2f9d
ACO
13128 } else {
13129 connector->base.state->best_encoder = NULL;
13130 connector->base.state->crtc = NULL;
13131 }
13132 }
f9e905ca 13133 drm_connector_list_iter_end(&conn_iter);
d29b2f9d
ACO
13134}
13135
f1a12172 13136static int
bcce8d86
VS
13137compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
13138 struct intel_crtc_state *pipe_config)
050f7aeb 13139{
bcce8d86 13140 struct drm_connector *connector = conn_state->connector;
cd49f818 13141 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
bcce8d86 13142 const struct drm_display_info *info = &connector->display_info;
f1a12172 13143 int bpp;
050f7aeb 13144
f1a12172
RS
13145 switch (conn_state->max_bpc) {
13146 case 6 ... 7:
13147 bpp = 6 * 3;
13148 break;
13149 case 8 ... 9:
13150 bpp = 8 * 3;
13151 break;
13152 case 10 ... 11:
13153 bpp = 10 * 3;
13154 break;
2ca5a7b8 13155 case 12 ... 16:
f1a12172
RS
13156 bpp = 12 * 3;
13157 break;
13158 default:
2ca5a7b8 13159 MISSING_CASE(conn_state->max_bpc);
f1a12172 13160 return -EINVAL;
050f7aeb
DV
13161 }
13162
f1a12172 13163 if (bpp < pipe_config->pipe_bpp) {
cd49f818
WK
13164 drm_dbg_kms(&i915->drm,
13165 "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
13166 "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
13167 connector->base.id, connector->name,
13168 bpp, 3 * info->bpc,
13169 3 * conn_state->max_requested_bpc,
13170 pipe_config->pipe_bpp);
bcce8d86 13171
f1a12172 13172 pipe_config->pipe_bpp = bpp;
050f7aeb 13173 }
bcce8d86 13174
f1a12172 13175 return 0;
050f7aeb
DV
13176}
13177
4e53c2e0 13178static int
050f7aeb 13179compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 13180 struct intel_crtc_state *pipe_config)
4e53c2e0 13181{
9beb5fea 13182 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2225f3c6 13183 struct drm_atomic_state *state = pipe_config->uapi.state;
da3ced29
ACO
13184 struct drm_connector *connector;
13185 struct drm_connector_state *connector_state;
1486017f 13186 int bpp, i;
4e53c2e0 13187
9beb5fea
TU
13188 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
13189 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 13190 bpp = 10*3;
9beb5fea 13191 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
DV
13192 bpp = 12*3;
13193 else
13194 bpp = 8*3;
13195
4e53c2e0
DV
13196 pipe_config->pipe_bpp = bpp;
13197
bcce8d86 13198 /* Clamp display bpp to connector max bpp */
aa5e9b47 13199 for_each_new_connector_in_state(state, connector, connector_state, i) {
bcce8d86
VS
13200 int ret;
13201
da3ced29 13202 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
13203 continue;
13204
bcce8d86
VS
13205 ret = compute_sink_pipe_bpp(connector_state, pipe_config);
13206 if (ret)
13207 return ret;
4e53c2e0
DV
13208 }
13209
bcce8d86 13210 return 0;
4e53c2e0
DV
13211}
13212
3dfd8d71
JN
13213static void intel_dump_crtc_timings(struct drm_i915_private *i915,
13214 const struct drm_display_mode *mode)
644db711 13215{
3dfd8d71
JN
13216 drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
13217 "type: 0x%x flags: 0x%x\n",
13218 mode->crtc_clock,
13219 mode->crtc_hdisplay, mode->crtc_hsync_start,
13220 mode->crtc_hsync_end, mode->crtc_htotal,
13221 mode->crtc_vdisplay, mode->crtc_vsync_start,
13222 mode->crtc_vsync_end, mode->crtc_vtotal,
13223 mode->type, mode->flags);
644db711
DV
13224}
13225
81b55ef1 13226static void
926878fb
VS
13227intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
13228 const char *id, unsigned int lane_count,
13229 const struct intel_link_m_n *m_n)
f6982332 13230{
cd49f818
WK
13231 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13232
13233 drm_dbg_kms(&i915->drm,
13234 "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
13235 id, lane_count,
13236 m_n->gmch_m, m_n->gmch_n,
13237 m_n->link_m, m_n->link_n, m_n->tu);
f6982332
TU
13238}
13239
69e89032
VS
13240static void
13241intel_dump_infoframe(struct drm_i915_private *dev_priv,
13242 const union hdmi_infoframe *frame)
13243{
bdbf43d7 13244 if (!drm_debug_enabled(DRM_UT_KMS))
69e89032
VS
13245 return;
13246
13247 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
13248}
13249
42890250
GM
13250static void
13251intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
13252 const struct drm_dp_vsc_sdp *vsc)
13253{
13254 if (!drm_debug_enabled(DRM_UT_KMS))
13255 return;
13256
13257 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
13258}
13259
40b2be41
VS
13260#define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
13261
13262static const char * const output_type_str[] = {
13263 OUTPUT_TYPE(UNUSED),
13264 OUTPUT_TYPE(ANALOG),
13265 OUTPUT_TYPE(DVO),
13266 OUTPUT_TYPE(SDVO),
13267 OUTPUT_TYPE(LVDS),
13268 OUTPUT_TYPE(TVOUT),
13269 OUTPUT_TYPE(HDMI),
13270 OUTPUT_TYPE(DP),
13271 OUTPUT_TYPE(EDP),
13272 OUTPUT_TYPE(DSI),
7e732cac 13273 OUTPUT_TYPE(DDI),
40b2be41
VS
13274 OUTPUT_TYPE(DP_MST),
13275};
13276
13277#undef OUTPUT_TYPE
13278
13279static void snprintf_output_types(char *buf, size_t len,
13280 unsigned int output_types)
13281{
13282 char *str = buf;
13283 int i;
13284
13285 str[0] = '\0';
13286
13287 for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
13288 int r;
13289
13290 if ((output_types & BIT(i)) == 0)
13291 continue;
13292
13293 r = snprintf(str, len, "%s%s",
13294 str != buf ? "," : "", output_type_str[i]);
13295 if (r >= len)
13296 break;
13297 str += r;
13298 len -= r;
13299
13300 output_types &= ~BIT(i);
13301 }
13302
13303 WARN_ON_ONCE(output_types != 0);
13304}
13305
d9facae6
SS
13306static const char * const output_format_str[] = {
13307 [INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
13308 [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
33b7f3ee 13309 [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
8c79f844 13310 [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
d9facae6
SS
13311};
13312
13313static const char *output_formats(enum intel_output_format format)
13314{
33b7f3ee 13315 if (format >= ARRAY_SIZE(output_format_str))
d9facae6
SS
13316 format = INTEL_OUTPUT_FORMAT_INVALID;
13317 return output_format_str[format];
13318}
13319
10d75f54
VS
13320static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
13321{
f90a85e7 13322 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
cd49f818 13323 struct drm_i915_private *i915 = to_i915(plane->base.dev);
7b3cb17a 13324 const struct drm_framebuffer *fb = plane_state->hw.fb;
10d75f54
VS
13325 struct drm_format_name_buf format_name;
13326
13327 if (!fb) {
cd49f818
WK
13328 drm_dbg_kms(&i915->drm,
13329 "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
13330 plane->base.base.id, plane->base.name,
13331 yesno(plane_state->uapi.visible));
10d75f54
VS
13332 return;
13333 }
13334
cd49f818 13335 drm_dbg_kms(&i915->drm,
54defc10 13336 "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s modifier = 0x%llx, visible: %s\n",
cd49f818
WK
13337 plane->base.base.id, plane->base.name,
13338 fb->base.id, fb->width, fb->height,
13339 drm_get_format_name(fb->format->format, &format_name),
54defc10 13340 fb->modifier, yesno(plane_state->uapi.visible));
cd49f818
WK
13341 drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13342 plane_state->hw.rotation, plane_state->scaler_id);
f90a85e7 13343 if (plane_state->uapi.visible)
cd49f818
WK
13344 drm_dbg_kms(&i915->drm,
13345 "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13346 DRM_RECT_FP_ARG(&plane_state->uapi.src),
13347 DRM_RECT_ARG(&plane_state->uapi.dst));
10d75f54
VS
13348}
13349
926878fb 13350static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
10d75f54 13351 struct intel_atomic_state *state,
c0b03411
DV
13352 const char *context)
13353{
2225f3c6 13354 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
10d75f54
VS
13355 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13356 const struct intel_plane_state *plane_state;
13357 struct intel_plane *plane;
40b2be41 13358 char buf[64];
10d75f54 13359 int i;
6a60cd87 13360
cd49f818
WK
13361 drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13362 crtc->base.base.id, crtc->base.name,
13363 yesno(pipe_config->hw.enable), context);
c0b03411 13364
1326a92c 13365 if (!pipe_config->hw.enable)
10d75f54
VS
13366 goto dump_planes;
13367
40b2be41 13368 snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
cd49f818
WK
13369 drm_dbg_kms(&dev_priv->drm,
13370 "active: %s, output_types: %s (0x%x), output format: %s\n",
13371 yesno(pipe_config->hw.active),
13372 buf, pipe_config->output_types,
13373 output_formats(pipe_config->output_format));
d9facae6 13374
cd49f818
WK
13375 drm_dbg_kms(&dev_priv->drm,
13376 "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13377 transcoder_name(pipe_config->cpu_transcoder),
13378 pipe_config->pipe_bpp, pipe_config->dither);
a4309657 13379
cf52acde
VS
13380 drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13381 transcoder_name(pipe_config->mst_master_transcoder));
13382
05d756b0
VS
13383 drm_dbg_kms(&dev_priv->drm,
13384 "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
13385 transcoder_name(pipe_config->master_transcoder),
13386 pipe_config->sync_mode_slaves_mask);
13387
a4309657
TU
13388 if (pipe_config->has_pch_encoder)
13389 intel_dump_m_n_config(pipe_config, "fdi",
13390 pipe_config->fdi_lanes,
13391 &pipe_config->fdi_m_n);
f6982332
TU
13392
13393 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
13394 intel_dump_m_n_config(pipe_config, "dp m_n",
13395 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
13396 if (pipe_config->has_drrs)
13397 intel_dump_m_n_config(pipe_config, "dp m2_n2",
13398 pipe_config->lane_count,
13399 &pipe_config->dp_m2_n2);
f6982332 13400 }
b95af8be 13401
cd49f818
WK
13402 drm_dbg_kms(&dev_priv->drm,
13403 "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13404 pipe_config->has_audio, pipe_config->has_infoframe,
13405 pipe_config->infoframes.enable);
69e89032
VS
13406
13407 if (pipe_config->infoframes.enable &
13408 intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
cd49f818
WK
13409 drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13410 pipe_config->infoframes.gcp);
69e89032
VS
13411 if (pipe_config->infoframes.enable &
13412 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13413 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13414 if (pipe_config->infoframes.enable &
13415 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13416 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13417 if (pipe_config->infoframes.enable &
13418 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13419 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
bfbeba29
GM
13420 if (pipe_config->infoframes.enable &
13421 intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
13422 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
e274fb32
GM
13423 if (pipe_config->infoframes.enable &
13424 intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
13425 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
42890250
GM
13426 if (pipe_config->infoframes.enable &
13427 intel_hdmi_infoframe_enable(DP_SDP_VSC))
13428 intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
69e89032 13429
cd49f818 13430 drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
1326a92c 13431 drm_mode_debug_printmodeline(&pipe_config->hw.mode);
cd49f818 13432 drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
1326a92c 13433 drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
3dfd8d71 13434 intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
bafcdad6
ML
13435 drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
13436 drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
13437 intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
cd49f818
WK
13438 drm_dbg_kms(&dev_priv->drm,
13439 "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13440 pipe_config->port_clock,
13441 pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13442 pipe_config->pixel_rate);
dd2f616d 13443
6dcde047
VS
13444 drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13445 pipe_config->linetime, pipe_config->ips_linetime);
13446
dd2f616d 13447 if (INTEL_GEN(dev_priv) >= 9)
cd49f818
WK
13448 drm_dbg_kms(&dev_priv->drm,
13449 "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13450 crtc->num_scalers,
13451 pipe_config->scaler_state.scaler_users,
13452 pipe_config->scaler_state.scaler_id);
a74f8375 13453
b2ae318a 13454 if (HAS_GMCH(dev_priv))
cd49f818
WK
13455 drm_dbg_kms(&dev_priv->drm,
13456 "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13457 pipe_config->gmch_pfit.control,
13458 pipe_config->gmch_pfit.pgm_ratios,
13459 pipe_config->gmch_pfit.lvds_border_bits);
a74f8375 13460 else
cd49f818 13461 drm_dbg_kms(&dev_priv->drm,
35dd95b4
VS
13462 "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
13463 DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
cd49f818
WK
13464 enableddisabled(pipe_config->pch_pfit.enabled),
13465 yesno(pipe_config->pch_pfit.force_thru));
a74f8375 13466
cd49f818
WK
13467 drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13468 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 13469
f50b79f0 13470 intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
415ff0f6 13471
b1a4383d 13472 if (IS_CHERRYVIEW(dev_priv))
cd49f818
WK
13473 drm_dbg_kms(&dev_priv->drm,
13474 "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13475 pipe_config->cgm_mode, pipe_config->gamma_mode,
13476 pipe_config->gamma_enable, pipe_config->csc_enable);
b1a4383d 13477 else
cd49f818
WK
13478 drm_dbg_kms(&dev_priv->drm,
13479 "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13480 pipe_config->csc_mode, pipe_config->gamma_mode,
13481 pipe_config->gamma_enable, pipe_config->csc_enable);
b1a4383d 13482
cfcd558c
VS
13483 drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
13484 pipe_config->hw.degamma_lut ?
13485 drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
13486 pipe_config->hw.gamma_lut ?
13487 drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
13488
10d75f54
VS
13489dump_planes:
13490 if (!state)
13491 return;
6a60cd87 13492
10d75f54
VS
13493 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13494 if (plane->pipe == crtc->pipe)
13495 intel_dump_plane_state(plane_state);
6a60cd87 13496 }
c0b03411
DV
13497}
13498
85829eb5 13499static bool check_digital_port_conflicts(struct intel_atomic_state *state)
00f0b378 13500{
85829eb5 13501 struct drm_device *dev = state->base.dev;
da3ced29 13502 struct drm_connector *connector;
2fd96b41 13503 struct drm_connector_list_iter conn_iter;
00f0b378 13504 unsigned int used_ports = 0;
477321e0 13505 unsigned int used_mst_ports = 0;
bd67a8c1 13506 bool ret = true;
00f0b378 13507
1d5a95b5
VS
13508 /*
13509 * We're going to peek into connector->state,
13510 * hence connection_mutex must be held.
13511 */
13512 drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13513
00f0b378
VS
13514 /*
13515 * Walk the connector list instead of the encoder
13516 * list to detect the problem on ddi platforms
13517 * where there's just one encoder per digital port.
13518 */
2fd96b41
GP
13519 drm_connector_list_iter_begin(dev, &conn_iter);
13520 drm_for_each_connector_iter(connector, &conn_iter) {
0bff4858
VS
13521 struct drm_connector_state *connector_state;
13522 struct intel_encoder *encoder;
13523
85829eb5
VS
13524 connector_state =
13525 drm_atomic_get_new_connector_state(&state->base,
13526 connector);
0bff4858
VS
13527 if (!connector_state)
13528 connector_state = connector->state;
13529
5448a00d 13530 if (!connector_state->best_encoder)
00f0b378
VS
13531 continue;
13532
5448a00d
ACO
13533 encoder = to_intel_encoder(connector_state->best_encoder);
13534
e57291c2 13535 drm_WARN_ON(dev, !connector_state->crtc);
00f0b378
VS
13536
13537 switch (encoder->type) {
7e732cac 13538 case INTEL_OUTPUT_DDI:
e57291c2 13539 if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
00f0b378 13540 break;
df561f66 13541 fallthrough;
cca0502b 13542 case INTEL_OUTPUT_DP:
00f0b378
VS
13543 case INTEL_OUTPUT_HDMI:
13544 case INTEL_OUTPUT_EDP:
00f0b378 13545 /* the same port mustn't appear more than once */
2713eb41 13546 if (used_ports & BIT(encoder->port))
bd67a8c1 13547 ret = false;
00f0b378 13548
2713eb41 13549 used_ports |= BIT(encoder->port);
477321e0
VS
13550 break;
13551 case INTEL_OUTPUT_DP_MST:
13552 used_mst_ports |=
8f4f2797 13553 1 << encoder->port;
477321e0 13554 break;
00f0b378
VS
13555 default:
13556 break;
13557 }
13558 }
2fd96b41 13559 drm_connector_list_iter_end(&conn_iter);
00f0b378 13560
477321e0
VS
13561 /* can't mix MST and SST/HDMI on the same port */
13562 if (used_ports & used_mst_ports)
13563 return false;
13564
bd67a8c1 13565 return ret;
00f0b378
VS
13566}
13567
58d124ea 13568static void
aa07c1d3
MN
13569intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
13570 struct intel_crtc_state *crtc_state)
58d124ea 13571{
ee230706
MN
13572 const struct intel_crtc_state *from_crtc_state = crtc_state;
13573
19f65a3d
ML
13574 if (crtc_state->bigjoiner_slave) {
13575 from_crtc_state = intel_atomic_get_new_crtc_state(state,
13576 crtc_state->bigjoiner_linked_crtc);
13577
13578 /* No need to copy state if the master state is unchanged */
13579 if (!from_crtc_state)
13580 return;
13581 }
13582
ee230706 13583 intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
58d124ea
ML
13584}
13585
13586static void
aa07c1d3
MN
13587intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
13588 struct intel_crtc_state *crtc_state)
58d124ea
ML
13589{
13590 crtc_state->hw.enable = crtc_state->uapi.enable;
13591 crtc_state->hw.active = crtc_state->uapi.active;
13592 crtc_state->hw.mode = crtc_state->uapi.mode;
13593 crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
6d1a2fde 13594 crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
aa07c1d3
MN
13595
13596 intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
58d124ea
ML
13597}
13598
13599static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13600{
0385ecea
MN
13601 if (crtc_state->bigjoiner_slave)
13602 return;
13603
58d124ea
ML
13604 crtc_state->uapi.enable = crtc_state->hw.enable;
13605 crtc_state->uapi.active = crtc_state->hw.active;
ce04ecd9
PB
13606 drm_WARN_ON(crtc_state->uapi.crtc->dev,
13607 drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
58d124ea
ML
13608
13609 crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
6d1a2fde 13610 crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
58d124ea
ML
13611
13612 /* copy color blobs to uapi */
13613 drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13614 crtc_state->hw.degamma_lut);
13615 drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13616 crtc_state->hw.gamma_lut);
13617 drm_property_replace_blob(&crtc_state->uapi.ctm,
13618 crtc_state->hw.ctm);
13619}
13620
19f65a3d
ML
13621static int
13622copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
13623 const struct intel_crtc_state *from_crtc_state)
13624{
13625 struct intel_crtc_state *saved_state;
13626 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13627
13628 saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
13629 if (!saved_state)
13630 return -ENOMEM;
13631
13632 saved_state->uapi = crtc_state->uapi;
13633 saved_state->scaler_state = crtc_state->scaler_state;
13634 saved_state->shared_dpll = crtc_state->shared_dpll;
13635 saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13636 saved_state->crc_enabled = crtc_state->crc_enabled;
13637
13638 intel_crtc_free_hw_state(crtc_state);
13639 memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13640 kfree(saved_state);
13641
13642 /* Re-init hw state */
13643 memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
13644 crtc_state->hw.enable = from_crtc_state->hw.enable;
13645 crtc_state->hw.active = from_crtc_state->hw.active;
13646 crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
13647 crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
13648
13649 /* Some fixups */
13650 crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
13651 crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
13652 crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
13653 crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
13654 crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
13655 crtc_state->bigjoiner_slave = true;
13656 crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
13657 crtc_state->has_audio = false;
13658
13659 return 0;
13660}
13661
f81b845f 13662static int
aa07c1d3
MN
13663intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
13664 struct intel_crtc_state *crtc_state)
83a57153 13665{
216383e9
VS
13666 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13667 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f81b845f
CW
13668 struct intel_crtc_state *saved_state;
13669
216383e9 13670 saved_state = intel_crtc_state_alloc(crtc);
f81b845f
CW
13671 if (!saved_state)
13672 return -ENOMEM;
83a57153 13673
58d124ea
ML
13674 /* free the old crtc_state->hw members */
13675 intel_crtc_free_hw_state(crtc_state);
13676
7546a384
ACO
13677 /* FIXME: before the switch to atomic started, a new pipe_config was
13678 * kzalloc'd. Code that depends on any field being zero should be
13679 * fixed, so that the crtc_state can be safely duplicated. For now,
13680 * only fields that are know to not cause problems are preserved. */
13681
58d124ea 13682 saved_state->uapi = crtc_state->uapi;
f81b845f
CW
13683 saved_state->scaler_state = crtc_state->scaler_state;
13684 saved_state->shared_dpll = crtc_state->shared_dpll;
13685 saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
eea72c4c
ID
13686 memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13687 sizeof(saved_state->icl_port_dplls));
a8ebf607 13688 saved_state->crc_enabled = crtc_state->crc_enabled;
04548cba
VS
13689 if (IS_G4X(dev_priv) ||
13690 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
f81b845f 13691 saved_state->wm = crtc_state->wm;
4978cc93 13692
58d124ea 13693 memcpy(crtc_state, saved_state, sizeof(*crtc_state));
f81b845f 13694 kfree(saved_state);
58d124ea 13695
aa07c1d3 13696 intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
58d124ea 13697
f81b845f 13698 return 0;
83a57153
ACO
13699}
13700
548ee15b 13701static int
aa07c1d3
MN
13702intel_modeset_pipe_config(struct intel_atomic_state *state,
13703 struct intel_crtc_state *pipe_config)
ee7b9f93 13704{
2225f3c6 13705 struct drm_crtc *crtc = pipe_config->uapi.crtc;
cd49f818 13706 struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
da3ced29 13707 struct drm_connector *connector;
0b901879 13708 struct drm_connector_state *connector_state;
b50a1aa6 13709 int base_bpp, ret, i;
e29c22c0 13710 bool retry = true;
ee7b9f93 13711
e143a21c
DV
13712 pipe_config->cpu_transcoder =
13713 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 13714
2960bc9c
ID
13715 /*
13716 * Sanitize sync polarity flags based on requested ones. If neither
13717 * positive or negative polarity is requested, treat this as meaning
13718 * negative polarity.
13719 */
1326a92c 13720 if (!(pipe_config->hw.adjusted_mode.flags &
2960bc9c 13721 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
1326a92c 13722 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13723
1326a92c 13724 if (!(pipe_config->hw.adjusted_mode.flags &
2960bc9c 13725 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
1326a92c 13726 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13727
bcce8d86
VS
13728 ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13729 pipe_config);
13730 if (ret)
13731 return ret;
13732
13733 base_bpp = pipe_config->pipe_bpp;
4e53c2e0 13734
e41a56be
VS
13735 /*
13736 * Determine the real pipe dimensions. Note that stereo modes can
13737 * increase the actual pipe size due to the frame doubling and
13738 * insertion of additional space for blanks between the frame. This
13739 * is stored in the crtc timings. We use the requested mode to do this
13740 * computation to clearly distinguish it from the adjusted mode, which
13741 * can be changed by the connectors in the below retry loop.
13742 */
1326a92c 13743 drm_mode_get_hv_timing(&pipe_config->hw.mode,
ecb7e16b
GP
13744 &pipe_config->pipe_src_w,
13745 &pipe_config->pipe_src_h);
e41a56be 13746
aa07c1d3 13747 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
691313ea
VS
13748 struct intel_encoder *encoder =
13749 to_intel_encoder(connector_state->best_encoder);
13750
253c84c8
VS
13751 if (connector_state->crtc != crtc)
13752 continue;
13753
e25148d0 13754 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
cd49f818
WK
13755 drm_dbg_kms(&i915->drm,
13756 "rejecting invalid cloning configuration\n");
d26592c6 13757 return -EINVAL;
e25148d0
VS
13758 }
13759
253c84c8
VS
13760 /*
13761 * Determine output_types before calling the .compute_config()
13762 * hooks so that the hooks can use this information safely.
13763 */
7e732cac
VS
13764 if (encoder->compute_output_type)
13765 pipe_config->output_types |=
13766 BIT(encoder->compute_output_type(encoder, pipe_config,
13767 connector_state));
13768 else
13769 pipe_config->output_types |= BIT(encoder->type);
253c84c8
VS
13770 }
13771
e29c22c0 13772encoder_retry:
ef1b460d 13773 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13774 pipe_config->port_clock = 0;
ef1b460d 13775 pipe_config->pixel_multiplier = 1;
ff9a6750 13776
135c81b8 13777 /* Fill in default crtc timings, allow encoders to overwrite them. */
1326a92c 13778 drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
2d112de7 13779 CRTC_STEREO_DOUBLE);
135c81b8 13780
7758a113
DV
13781 /* Pass our mode to the connectors and the CRTC to give them a chance to
13782 * adjust it according to limitations or connector properties, and also
13783 * a chance to reject the mode entirely.
47f1c6c9 13784 */
aa07c1d3 13785 for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
691313ea
VS
13786 struct intel_encoder *encoder =
13787 to_intel_encoder(connector_state->best_encoder);
13788
0b901879 13789 if (connector_state->crtc != crtc)
7758a113 13790 continue;
7ae89233 13791
204474a6
LP
13792 ret = encoder->compute_config(encoder, pipe_config,
13793 connector_state);
13794 if (ret < 0) {
13795 if (ret != -EDEADLK)
cd49f818
WK
13796 drm_dbg_kms(&i915->drm,
13797 "Encoder config failure: %d\n",
13798 ret);
204474a6 13799 return ret;
7758a113 13800 }
ee7b9f93 13801 }
47f1c6c9 13802
ff9a6750
DV
13803 /* Set default port clock if not overwritten by the encoder. Needs to be
13804 * done afterwards in case the encoder adjusts the mode. */
13805 if (!pipe_config->port_clock)
1326a92c 13806 pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
241bfc38 13807 * pipe_config->pixel_multiplier;
ff9a6750 13808
a43f6e0f 13809 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8e2b4dff 13810 if (ret == -EDEADLK)
d26592c6 13811 return ret;
e29c22c0 13812 if (ret < 0) {
cd49f818 13813 drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
d26592c6 13814 return ret;
ee7b9f93 13815 }
e29c22c0
DV
13816
13817 if (ret == RETRY) {
e57291c2
PB
13818 if (drm_WARN(&i915->drm, !retry,
13819 "loop in pipe configuration computation\n"))
d26592c6 13820 return -EINVAL;
e29c22c0 13821
cd49f818 13822 drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
e29c22c0
DV
13823 retry = false;
13824 goto encoder_retry;
13825 }
13826
e8fa4270 13827 /* Dithering seems to not pass-through bits correctly when it should, so
611032bf
MN
13828 * only enable it on 6bpc panels and when its not a compliance
13829 * test requesting 6bpc video pattern.
13830 */
13831 pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13832 !pipe_config->dither_force_disable;
cd49f818
WK
13833 drm_dbg_kms(&i915->drm,
13834 "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13835 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13836
d26592c6 13837 return 0;
ee7b9f93 13838}
47f1c6c9 13839
f2801424
VS
13840static int
13841intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13842{
13843 struct intel_atomic_state *state =
13844 to_intel_atomic_state(crtc_state->uapi.state);
13845 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13846 struct drm_connector_state *conn_state;
13847 struct drm_connector *connector;
13848 int i;
13849
13850 for_each_new_connector_in_state(&state->base, connector,
13851 conn_state, i) {
13852 struct intel_encoder *encoder =
13853 to_intel_encoder(conn_state->best_encoder);
13854 int ret;
13855
13856 if (conn_state->crtc != &crtc->base ||
13857 !encoder->compute_config_late)
13858 continue;
13859
13860 ret = encoder->compute_config_late(encoder, crtc_state,
13861 conn_state);
13862 if (ret)
13863 return ret;
13864 }
13865
13866 return 0;
13867}
13868
2c1c5525 13869bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13870{
3bd26263 13871 int diff;
f1f644dc
JB
13872
13873 if (clock1 == clock2)
13874 return true;
13875
13876 if (!clock1 || !clock2)
13877 return false;
13878
13879 diff = abs(clock1 - clock2);
13880
13881 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13882 return true;
13883
13884 return false;
13885}
13886
cfb23ed6
ML
13887static bool
13888intel_compare_m_n(unsigned int m, unsigned int n,
13889 unsigned int m2, unsigned int n2,
13890 bool exact)
13891{
13892 if (m == m2 && n == n2)
13893 return true;
13894
13895 if (exact || !m || !n || !m2 || !n2)
13896 return false;
13897
13898 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13899
31d10b57
ML
13900 if (n > n2) {
13901 while (n > n2) {
cfb23ed6
ML
13902 m2 <<= 1;
13903 n2 <<= 1;
13904 }
31d10b57
ML
13905 } else if (n < n2) {
13906 while (n < n2) {
cfb23ed6
ML
13907 m <<= 1;
13908 n <<= 1;
13909 }
13910 }
13911
31d10b57
ML
13912 if (n != n2)
13913 return false;
13914
13915 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13916}
13917
13918static bool
13919intel_compare_link_m_n(const struct intel_link_m_n *m_n,
b124ea43
VS
13920 const struct intel_link_m_n *m2_n2,
13921 bool exact)
13922{
13923 return m_n->tu == m2_n2->tu &&
13924 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13925 m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13926 intel_compare_m_n(m_n->link_m, m_n->link_n,
13927 m2_n2->link_m, m2_n2->link_n, exact);
cfb23ed6
ML
13928}
13929
6454cb9f
VS
13930static bool
13931intel_compare_infoframe(const union hdmi_infoframe *a,
13932 const union hdmi_infoframe *b)
13933{
13934 return memcmp(a, b, sizeof(*a)) == 0;
13935}
13936
2c3928e4
GM
13937static bool
13938intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
13939 const struct drm_dp_vsc_sdp *b)
13940{
13941 return memcmp(a, b, sizeof(*a)) == 0;
13942}
13943
6454cb9f 13944static void
dde84833
VS
13945pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13946 bool fastset, const char *name,
13947 const union hdmi_infoframe *a,
13948 const union hdmi_infoframe *b)
6454cb9f 13949{
dde84833 13950 if (fastset) {
bdbf43d7 13951 if (!drm_debug_enabled(DRM_UT_KMS))
6454cb9f
VS
13952 return;
13953
cd49f818
WK
13954 drm_dbg_kms(&dev_priv->drm,
13955 "fastset mismatch in %s infoframe\n", name);
13956 drm_dbg_kms(&dev_priv->drm, "expected:\n");
6454cb9f 13957 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
cd49f818 13958 drm_dbg_kms(&dev_priv->drm, "found:\n");
6454cb9f
VS
13959 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13960 } else {
cd49f818
WK
13961 drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13962 drm_err(&dev_priv->drm, "expected:\n");
6454cb9f 13963 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
cd49f818 13964 drm_err(&dev_priv->drm, "found:\n");
6454cb9f
VS
13965 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13966 }
13967}
13968
2c3928e4
GM
13969static void
13970pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
13971 bool fastset, const char *name,
13972 const struct drm_dp_vsc_sdp *a,
13973 const struct drm_dp_vsc_sdp *b)
13974{
13975 if (fastset) {
13976 if (!drm_debug_enabled(DRM_UT_KMS))
13977 return;
13978
13979 drm_dbg_kms(&dev_priv->drm,
13980 "fastset mismatch in %s dp sdp\n", name);
13981 drm_dbg_kms(&dev_priv->drm, "expected:\n");
13982 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
13983 drm_dbg_kms(&dev_priv->drm, "found:\n");
13984 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
13985 } else {
13986 drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
13987 drm_err(&dev_priv->drm, "expected:\n");
13988 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
13989 drm_err(&dev_priv->drm, "found:\n");
13990 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
13991 }
13992}
13993
73cefd90
LDM
13994static void __printf(4, 5)
13995pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13996 const char *name, const char *format, ...)
4e8048f8 13997{
cd49f818 13998 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
4e8048f8
TU
13999 struct va_format vaf;
14000 va_list args;
14001
4e8048f8
TU
14002 va_start(args, format);
14003 vaf.fmt = format;
14004 vaf.va = &args;
14005
dde84833 14006 if (fastset)
cd49f818
WK
14007 drm_dbg_kms(&i915->drm,
14008 "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
14009 crtc->base.base.id, crtc->base.name, name, &vaf);
99a95487 14010 else
cd49f818
WK
14011 drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
14012 crtc->base.base.id, crtc->base.name, name, &vaf);
4e8048f8
TU
14013
14014 va_end(args);
14015}
14016
3d6535cb
HG
14017static bool fastboot_enabled(struct drm_i915_private *dev_priv)
14018{
8a25c4be
JN
14019 if (dev_priv->params.fastboot != -1)
14020 return dev_priv->params.fastboot;
3d6535cb
HG
14021
14022 /* Enable fastboot by default on Skylake and newer */
7360c9f6
HG
14023 if (INTEL_GEN(dev_priv) >= 9)
14024 return true;
14025
14026 /* Enable fastboot by default on VLV and CHV */
14027 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14028 return true;
14029
14030 /* Disabled by default on all others */
14031 return false;
3d6535cb
HG
14032}
14033
0e8ffe1b 14034static bool
b124ea43
VS
14035intel_pipe_config_compare(const struct intel_crtc_state *current_config,
14036 const struct intel_crtc_state *pipe_config,
dde84833 14037 bool fastset)
0e8ffe1b 14038{
2225f3c6
ML
14039 struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
14040 struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
cfb23ed6 14041 bool ret = true;
7e764059 14042 u32 bp_gamma = 0;
dde84833 14043 bool fixup_inherited = fastset &&
a227569d 14044 current_config->inherited && !pipe_config->inherited;
cfb23ed6 14045
3d6535cb 14046 if (fixup_inherited && !fastboot_enabled(dev_priv)) {
cd49f818
WK
14047 drm_dbg_kms(&dev_priv->drm,
14048 "initial modeset and fastboot not set\n");
d19f958d
ML
14049 ret = false;
14050 }
14051
eadd2721 14052#define PIPE_CONF_CHECK_X(name) do { \
66e985c0 14053 if (current_config->name != pipe_config->name) { \
73cefd90 14054 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14055 "(expected 0x%08x, found 0x%08x)", \
dde84833
VS
14056 current_config->name, \
14057 pipe_config->name); \
cfb23ed6 14058 ret = false; \
eadd2721
VS
14059 } \
14060} while (0)
66e985c0 14061
eadd2721 14062#define PIPE_CONF_CHECK_I(name) do { \
08a24034 14063 if (current_config->name != pipe_config->name) { \
73cefd90 14064 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14065 "(expected %i, found %i)", \
dde84833
VS
14066 current_config->name, \
14067 pipe_config->name); \
cfb23ed6 14068 ret = false; \
eadd2721
VS
14069 } \
14070} while (0)
cfb23ed6 14071
eadd2721 14072#define PIPE_CONF_CHECK_BOOL(name) do { \
d640bf79 14073 if (current_config->name != pipe_config->name) { \
73cefd90 14074 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14075 "(expected %s, found %s)", \
dde84833
VS
14076 yesno(current_config->name), \
14077 yesno(pipe_config->name)); \
d640bf79 14078 ret = false; \
eadd2721
VS
14079 } \
14080} while (0)
d640bf79 14081
4493e098
ML
14082/*
14083 * Checks state where we only read out the enabling, but not the entire
14084 * state itself (like full infoframes or ELD for audio). These states
14085 * require a full modeset on bootup to fix up.
14086 */
eadd2721 14087#define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
4493e098
ML
14088 if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
14089 PIPE_CONF_CHECK_BOOL(name); \
14090 } else { \
73cefd90 14091 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14092 "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
dde84833
VS
14093 yesno(current_config->name), \
14094 yesno(pipe_config->name)); \
4493e098 14095 ret = false; \
eadd2721
VS
14096 } \
14097} while (0)
4493e098 14098
eadd2721 14099#define PIPE_CONF_CHECK_P(name) do { \
8106ddbd 14100 if (current_config->name != pipe_config->name) { \
73cefd90 14101 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14102 "(expected %p, found %p)", \
dde84833
VS
14103 current_config->name, \
14104 pipe_config->name); \
8106ddbd 14105 ret = false; \
eadd2721
VS
14106 } \
14107} while (0)
8106ddbd 14108
eadd2721 14109#define PIPE_CONF_CHECK_M_N(name) do { \
cfb23ed6
ML
14110 if (!intel_compare_link_m_n(&current_config->name, \
14111 &pipe_config->name,\
dde84833 14112 !fastset)) { \
73cefd90 14113 pipe_config_mismatch(fastset, crtc, __stringify(name), \
dde84833 14114 "(expected tu %i gmch %i/%i link %i/%i, " \
cbd9b9f2 14115 "found tu %i, gmch %i/%i link %i/%i)", \
dde84833
VS
14116 current_config->name.tu, \
14117 current_config->name.gmch_m, \
14118 current_config->name.gmch_n, \
14119 current_config->name.link_m, \
14120 current_config->name.link_n, \
14121 pipe_config->name.tu, \
14122 pipe_config->name.gmch_m, \
14123 pipe_config->name.gmch_n, \
14124 pipe_config->name.link_m, \
14125 pipe_config->name.link_n); \
cfb23ed6 14126 ret = false; \
eadd2721
VS
14127 } \
14128} while (0)
cfb23ed6 14129
55c561a7
DV
14130/* This is required for BDW+ where there is only one set of registers for
14131 * switching between high and low RR.
14132 * This macro can be used whenever a comparison has to be made between one
14133 * hw state and multiple sw state variables.
14134 */
eadd2721 14135#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
cfb23ed6 14136 if (!intel_compare_link_m_n(&current_config->name, \
dde84833 14137 &pipe_config->name, !fastset) && \
cfb23ed6 14138 !intel_compare_link_m_n(&current_config->alt_name, \
dde84833 14139 &pipe_config->name, !fastset)) { \
73cefd90 14140 pipe_config_mismatch(fastset, crtc, __stringify(name), \
dde84833
VS
14141 "(expected tu %i gmch %i/%i link %i/%i, " \
14142 "or tu %i gmch %i/%i link %i/%i, " \
cbd9b9f2 14143 "found tu %i, gmch %i/%i link %i/%i)", \
dde84833
VS
14144 current_config->name.tu, \
14145 current_config->name.gmch_m, \
14146 current_config->name.gmch_n, \
14147 current_config->name.link_m, \
14148 current_config->name.link_n, \
14149 current_config->alt_name.tu, \
14150 current_config->alt_name.gmch_m, \
14151 current_config->alt_name.gmch_n, \
14152 current_config->alt_name.link_m, \
14153 current_config->alt_name.link_n, \
14154 pipe_config->name.tu, \
14155 pipe_config->name.gmch_m, \
14156 pipe_config->name.gmch_n, \
14157 pipe_config->name.link_m, \
14158 pipe_config->name.link_n); \
cfb23ed6 14159 ret = false; \
eadd2721
VS
14160 } \
14161} while (0)
88adfff1 14162
eadd2721 14163#define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
1bd1bd80 14164 if ((current_config->name ^ pipe_config->name) & (mask)) { \
73cefd90 14165 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14166 "(%x) (expected %i, found %i)", \
dde84833
VS
14167 (mask), \
14168 current_config->name & (mask), \
14169 pipe_config->name & (mask)); \
cfb23ed6 14170 ret = false; \
eadd2721
VS
14171 } \
14172} while (0)
1bd1bd80 14173
eadd2721 14174#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
5e550656 14175 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
73cefd90 14176 pipe_config_mismatch(fastset, crtc, __stringify(name), \
cbd9b9f2 14177 "(expected %i, found %i)", \
dde84833
VS
14178 current_config->name, \
14179 pipe_config->name); \
cfb23ed6 14180 ret = false; \
eadd2721
VS
14181 } \
14182} while (0)
5e550656 14183
6454cb9f
VS
14184#define PIPE_CONF_CHECK_INFOFRAME(name) do { \
14185 if (!intel_compare_infoframe(&current_config->infoframes.name, \
14186 &pipe_config->infoframes.name)) { \
dde84833
VS
14187 pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
14188 &current_config->infoframes.name, \
14189 &pipe_config->infoframes.name); \
6454cb9f
VS
14190 ret = false; \
14191 } \
14192} while (0)
14193
2c3928e4
GM
14194#define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
14195 if (!current_config->has_psr && !pipe_config->has_psr && \
14196 !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
14197 &pipe_config->infoframes.name)) { \
14198 pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
14199 &current_config->infoframes.name, \
14200 &pipe_config->infoframes.name); \
14201 ret = false; \
14202 } \
14203} while (0)
14204
7e764059
SS
14205#define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
14206 if (current_config->name1 != pipe_config->name1) { \
73cefd90 14207 pipe_config_mismatch(fastset, crtc, __stringify(name1), \
cbd9b9f2 14208 "(expected %i, found %i, won't compare lut values)", \
7e764059
SS
14209 current_config->name1, \
14210 pipe_config->name1); \
14211 ret = false;\
14212 } else { \
14213 if (!intel_color_lut_equal(current_config->name2, \
14214 pipe_config->name2, pipe_config->name1, \
14215 bit_precision)) { \
73cefd90 14216 pipe_config_mismatch(fastset, crtc, __stringify(name2), \
cbd9b9f2 14217 "hw_state doesn't match sw_state"); \
7e764059
SS
14218 ret = false; \
14219 } \
14220 } \
14221} while (0)
14222
6454cb9f 14223#define PIPE_CONF_QUIRK(quirk) \
bb760063
DV
14224 ((current_config->quirks | pipe_config->quirks) & (quirk))
14225
eccb140b
DV
14226 PIPE_CONF_CHECK_I(cpu_transcoder);
14227
d640bf79 14228 PIPE_CONF_CHECK_BOOL(has_pch_encoder);
08a24034 14229 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 14230 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 14231
90a6b7b0 14232 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 14233 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 14234
6315b5d3 14235 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
14236 PIPE_CONF_CHECK_M_N(dp_m_n);
14237
cfb23ed6
ML
14238 if (current_config->has_drrs)
14239 PIPE_CONF_CHECK_M_N(dp_m2_n2);
14240 } else
14241 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 14242
253c84c8 14243 PIPE_CONF_CHECK_X(output_types);
a65347ba 14244
0385ecea
MN
14245 /* FIXME do the readout properly and get rid of this quirk */
14246 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
14247 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
14248 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
14249 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
14250 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
14251 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
14252 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
14253
14254 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
14255 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
14256 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
14257 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
14258 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
14259 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
14260
14261 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
14262 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
14263 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
14264 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
14265 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
14266 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
14267
14268 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
14269 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
14270 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
14271 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
14272 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
14273 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
14274
14275 PIPE_CONF_CHECK_I(pixel_multiplier);
14276
14277 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14278 DRM_MODE_FLAG_INTERLACE);
14279
14280 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
14281 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14282 DRM_MODE_FLAG_PHSYNC);
14283 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14284 DRM_MODE_FLAG_NHSYNC);
14285 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14286 DRM_MODE_FLAG_PVSYNC);
14287 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14288 DRM_MODE_FLAG_NVSYNC);
14289 }
14290 }
14291
d9facae6 14292 PIPE_CONF_CHECK_I(output_format);
d640bf79 14293 PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
772c2a51 14294 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 14295 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
d640bf79 14296 PIPE_CONF_CHECK_BOOL(limited_color_range);
15953637 14297
d640bf79
ML
14298 PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
14299 PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
7afc7f81 14300 PIPE_CONF_CHECK_BOOL(has_infoframe);
0385ecea
MN
14301 /* FIXME do the readout properly and get rid of this quirk */
14302 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
14303 PIPE_CONF_CHECK_BOOL(fec_enable);
6c49f241 14304
4493e098 14305 PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
9ed109a7 14306
333b8ca8 14307 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 14308 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 14309 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 14310 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 14311 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 14312
13b7648b
VS
14313 /*
14314 * Changing the EDP transcoder input mux
14315 * (A_ONOFF vs. A_ON) requires a full modeset.
14316 */
dc0c0bfe 14317 PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
13b7648b 14318
dde84833 14319 if (!fastset) {
bfd16b2a
ML
14320 PIPE_CONF_CHECK_I(pipe_src_w);
14321 PIPE_CONF_CHECK_I(pipe_src_h);
14322
d640bf79 14323 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
bfd16b2a 14324 if (current_config->pch_pfit.enabled) {
35dd95b4
VS
14325 PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
14326 PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
14327 PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
14328 PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
bfd16b2a 14329 }
2fa2fe9a 14330
7aefe2b5 14331 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
0385ecea
MN
14332 /* FIXME do the readout properly and get rid of this quirk */
14333 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
14334 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
9d5441de
VS
14335
14336 PIPE_CONF_CHECK_X(gamma_mode);
9fdfb8e7
VS
14337 if (IS_CHERRYVIEW(dev_priv))
14338 PIPE_CONF_CHECK_X(cgm_mode);
14339 else
14340 PIPE_CONF_CHECK_X(csc_mode);
5f29ab23 14341 PIPE_CONF_CHECK_BOOL(gamma_enable);
8271b2ef 14342 PIPE_CONF_CHECK_BOOL(csc_enable);
7e764059 14343
6dcde047
VS
14344 PIPE_CONF_CHECK_I(linetime);
14345 PIPE_CONF_CHECK_I(ips_linetime);
14346
7e764059
SS
14347 bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
14348 if (bp_gamma)
aa42a50a 14349 PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
7aefe2b5 14350 }
a1b2278e 14351
d640bf79 14352 PIPE_CONF_CHECK_BOOL(double_wide);
282740f7 14353
8106ddbd 14354 PIPE_CONF_CHECK_P(shared_dpll);
0385ecea
MN
14355
14356 /* FIXME do the readout properly and get rid of this quirk */
14357 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
14358 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
14359 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
14360 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
14361 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
14362 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
14363 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
14364 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
14365 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
14366 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
14367 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
14368 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
14369 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
14370 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
14371 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
14372 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
14373 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
14374 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
14375 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
14376 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
14377 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
14378 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
14379 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
14380 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
14381 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
14382 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
14383 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
14384 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
14385 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
14386 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
14387 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
14388 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
14389
14390 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
14391 PIPE_CONF_CHECK_X(dsi_pll.div);
14392
14393 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
14394 PIPE_CONF_CHECK_I(pipe_bpp);
14395
14396 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
14397 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
14398 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
14399
14400 PIPE_CONF_CHECK_I(min_voltage_level);
14401 }
53e9bf5e 14402
6454cb9f
VS
14403 PIPE_CONF_CHECK_X(infoframes.enable);
14404 PIPE_CONF_CHECK_X(infoframes.gcp);
14405 PIPE_CONF_CHECK_INFOFRAME(avi);
14406 PIPE_CONF_CHECK_INFOFRAME(spd);
14407 PIPE_CONF_CHECK_INFOFRAME(hdmi);
b37f588e 14408 PIPE_CONF_CHECK_INFOFRAME(drm);
2c3928e4 14409 PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
6454cb9f 14410
b33950dd 14411 PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
bfb926e3 14412 PIPE_CONF_CHECK_I(master_transcoder);
0385ecea
MN
14413 PIPE_CONF_CHECK_BOOL(bigjoiner);
14414 PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
14415 PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
bfb926e3 14416
fbacb15e
JN
14417 PIPE_CONF_CHECK_I(dsc.compression_enable);
14418 PIPE_CONF_CHECK_I(dsc.dsc_split);
14419 PIPE_CONF_CHECK_I(dsc.compressed_bpp);
14420
6671c367
JRS
14421 PIPE_CONF_CHECK_I(mst_master_transcoder);
14422
66e985c0 14423#undef PIPE_CONF_CHECK_X
08a24034 14424#undef PIPE_CONF_CHECK_I
d640bf79 14425#undef PIPE_CONF_CHECK_BOOL
4493e098 14426#undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
8106ddbd 14427#undef PIPE_CONF_CHECK_P
1bd1bd80 14428#undef PIPE_CONF_CHECK_FLAGS
5e550656 14429#undef PIPE_CONF_CHECK_CLOCK_FUZZY
7e764059 14430#undef PIPE_CONF_CHECK_COLOR_LUT
bb760063 14431#undef PIPE_CONF_QUIRK
88adfff1 14432
cfb23ed6 14433 return ret;
0e8ffe1b
DV
14434}
14435
e3b247da
VS
14436static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
14437 const struct intel_crtc_state *pipe_config)
14438{
14439 if (pipe_config->has_pch_encoder) {
21a727b3 14440 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da 14441 &pipe_config->fdi_m_n);
1326a92c 14442 int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
e3b247da
VS
14443
14444 /*
14445 * FDI already provided one idea for the dotclock.
14446 * Yell if the encoder disagrees.
14447 */
e57291c2
PB
14448 drm_WARN(&dev_priv->drm,
14449 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
14450 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
14451 fdi_dotclock, dotclock);
e3b247da
VS
14452 }
14453}
14454
3b4bf24d
ML
14455static void verify_wm_state(struct intel_crtc *crtc,
14456 struct intel_crtc_state *new_crtc_state)
08db6652 14457{
3b4bf24d 14458 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
370d757d
CW
14459 struct skl_hw_state {
14460 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
14461 struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
370d757d
CW
14462 struct skl_pipe_wm wm;
14463 } *hw;
370d757d 14464 struct skl_pipe_wm *sw_wm;
3de8a14c 14465 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
072fcc30 14466 u8 hw_enabled_slices;
3b4bf24d 14467 const enum pipe pipe = crtc->pipe;
3de8a14c 14468 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 14469
1326a92c 14470 if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
08db6652
DL
14471 return;
14472
370d757d
CW
14473 hw = kzalloc(sizeof(*hw), GFP_KERNEL);
14474 if (!hw)
14475 return;
14476
3b4bf24d
ML
14477 skl_pipe_wm_get_hw_state(crtc, &hw->wm);
14478 sw_wm = &new_crtc_state->wm.skl.optimal;
3de8a14c 14479
3b4bf24d 14480 skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
ff43bc37 14481
0f0f9aee 14482 hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
08db6652 14483
370d757d 14484 if (INTEL_GEN(dev_priv) >= 11 &&
3cf43cdc 14485 hw_enabled_slices != dev_priv->dbuf.enabled_slices)
cd49f818 14486 drm_err(&dev_priv->drm,
0f0f9aee 14487 "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
3cf43cdc 14488 dev_priv->dbuf.enabled_slices,
072fcc30 14489 hw_enabled_slices);
370d757d 14490
e7c84544 14491 /* planes */
8b364b41 14492 for_each_universal_plane(dev_priv, pipe, plane) {
370d757d
CW
14493 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14494
14495 hw_plane_wm = &hw->wm.planes[plane];
3de8a14c 14496 sw_plane_wm = &sw_wm->planes[plane];
08db6652 14497
3de8a14c 14498 /* Watermarks */
14499 for (level = 0; level <= max_level; level++) {
14500 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
7241c57d
SL
14501 &sw_plane_wm->wm[level]) ||
14502 (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14503 &sw_plane_wm->sagv_wm0)))
3de8a14c 14504 continue;
14505
cd49f818
WK
14506 drm_err(&dev_priv->drm,
14507 "mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14508 pipe_name(pipe), plane + 1, level,
14509 sw_plane_wm->wm[level].plane_en,
14510 sw_plane_wm->wm[level].plane_res_b,
14511 sw_plane_wm->wm[level].plane_res_l,
14512 hw_plane_wm->wm[level].plane_en,
14513 hw_plane_wm->wm[level].plane_res_b,
14514 hw_plane_wm->wm[level].plane_res_l);
3de8a14c 14515 }
08db6652 14516
3de8a14c 14517 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14518 &sw_plane_wm->trans_wm)) {
cd49f818
WK
14519 drm_err(&dev_priv->drm,
14520 "mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14521 pipe_name(pipe), plane + 1,
14522 sw_plane_wm->trans_wm.plane_en,
14523 sw_plane_wm->trans_wm.plane_res_b,
14524 sw_plane_wm->trans_wm.plane_res_l,
14525 hw_plane_wm->trans_wm.plane_en,
14526 hw_plane_wm->trans_wm.plane_res_b,
14527 hw_plane_wm->trans_wm.plane_res_l);
3de8a14c 14528 }
14529
14530 /* DDB */
370d757d 14531 hw_ddb_entry = &hw->ddb_y[plane];
3b4bf24d 14532 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
3de8a14c 14533
14534 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cd49f818
WK
14535 drm_err(&dev_priv->drm,
14536 "mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14537 pipe_name(pipe), plane + 1,
14538 sw_ddb_entry->start, sw_ddb_entry->end,
14539 hw_ddb_entry->start, hw_ddb_entry->end);
3de8a14c 14540 }
e7c84544 14541 }
08db6652 14542
27082493
L
14543 /*
14544 * cursor
14545 * If the cursor plane isn't active, we may not have updated it's ddb
14546 * allocation. In that case since the ddb allocation will be updated
14547 * once the plane becomes visible, we can skip this check
14548 */
cd5dcbf1 14549 if (1) {
370d757d
CW
14550 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14551
14552 hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
3de8a14c 14553 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14554
14555 /* Watermarks */
14556 for (level = 0; level <= max_level; level++) {
14557 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
7241c57d
SL
14558 &sw_plane_wm->wm[level]) ||
14559 (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14560 &sw_plane_wm->sagv_wm0)))
3de8a14c 14561 continue;
14562
cd49f818
WK
14563 drm_err(&dev_priv->drm,
14564 "mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14565 pipe_name(pipe), level,
14566 sw_plane_wm->wm[level].plane_en,
14567 sw_plane_wm->wm[level].plane_res_b,
14568 sw_plane_wm->wm[level].plane_res_l,
14569 hw_plane_wm->wm[level].plane_en,
14570 hw_plane_wm->wm[level].plane_res_b,
14571 hw_plane_wm->wm[level].plane_res_l);
3de8a14c 14572 }
14573
14574 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14575 &sw_plane_wm->trans_wm)) {
cd49f818
WK
14576 drm_err(&dev_priv->drm,
14577 "mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14578 pipe_name(pipe),
14579 sw_plane_wm->trans_wm.plane_en,
14580 sw_plane_wm->trans_wm.plane_res_b,
14581 sw_plane_wm->trans_wm.plane_res_l,
14582 hw_plane_wm->trans_wm.plane_en,
14583 hw_plane_wm->trans_wm.plane_res_b,
14584 hw_plane_wm->trans_wm.plane_res_l);
3de8a14c 14585 }
14586
14587 /* DDB */
370d757d 14588 hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
3b4bf24d 14589 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
27082493 14590
3de8a14c 14591 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cd49f818
WK
14592 drm_err(&dev_priv->drm,
14593 "mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14594 pipe_name(pipe),
14595 sw_ddb_entry->start, sw_ddb_entry->end,
14596 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 14597 }
08db6652 14598 }
370d757d
CW
14599
14600 kfree(hw);
08db6652
DL
14601}
14602
91d1b4bd 14603static void
3b4bf24d
ML
14604verify_connector_state(struct intel_atomic_state *state,
14605 struct intel_crtc *crtc)
8af6cf88 14606{
35dd3c64 14607 struct drm_connector *connector;
aa5e9b47 14608 struct drm_connector_state *new_conn_state;
677100ce 14609 int i;
8af6cf88 14610
3b4bf24d 14611 for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
35dd3c64 14612 struct drm_encoder *encoder = connector->encoder;
3b4bf24d 14613 struct intel_crtc_state *crtc_state = NULL;
ad3c558f 14614
3b4bf24d 14615 if (new_conn_state->crtc != &crtc->base)
e7c84544
ML
14616 continue;
14617
749d98b8 14618 if (crtc)
3b4bf24d 14619 crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
749d98b8
ML
14620
14621 intel_connector_verify_state(crtc_state, new_conn_state);
8af6cf88 14622
aa5e9b47 14623 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
35dd3c64 14624 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 14625 }
91d1b4bd
DV
14626}
14627
14628static void
3b4bf24d 14629verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
91d1b4bd
DV
14630{
14631 struct intel_encoder *encoder;
86b04268
DV
14632 struct drm_connector *connector;
14633 struct drm_connector_state *old_conn_state, *new_conn_state;
14634 int i;
8af6cf88 14635
3b4bf24d 14636 for_each_intel_encoder(&dev_priv->drm, encoder) {
86b04268 14637 bool enabled = false, found = false;
4d20cd86 14638 enum pipe pipe;
8af6cf88 14639
cd49f818
WK
14640 drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14641 encoder->base.base.id,
14642 encoder->base.name);
8af6cf88 14643
3b4bf24d 14644 for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
86b04268
DV
14645 new_conn_state, i) {
14646 if (old_conn_state->best_encoder == &encoder->base)
14647 found = true;
14648
14649 if (new_conn_state->best_encoder != &encoder->base)
8af6cf88 14650 continue;
86b04268 14651 found = enabled = true;
ad3c558f 14652
86b04268 14653 I915_STATE_WARN(new_conn_state->crtc !=
ad3c558f
ML
14654 encoder->base.crtc,
14655 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 14656 }
86b04268
DV
14657
14658 if (!found)
14659 continue;
0e32b39c 14660
e2c719b7 14661 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
14662 "encoder's enabled state mismatch "
14663 "(expected %i, found %i)\n",
14664 !!encoder->base.crtc, enabled);
7c60d198
ML
14665
14666 if (!encoder->base.crtc) {
4d20cd86 14667 bool active;
7c60d198 14668
4d20cd86
ML
14669 active = encoder->get_hw_state(encoder, &pipe);
14670 I915_STATE_WARN(active,
14671 "encoder detached but still enabled on pipe %c.\n",
14672 pipe_name(pipe));
7c60d198 14673 }
8af6cf88 14674 }
91d1b4bd
DV
14675}
14676
14677static void
3b4bf24d
ML
14678verify_crtc_state(struct intel_crtc *crtc,
14679 struct intel_crtc_state *old_crtc_state,
14680 struct intel_crtc_state *new_crtc_state)
91d1b4bd 14681{
3b4bf24d 14682 struct drm_device *dev = crtc->base.dev;
fac5e23e 14683 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 14684 struct intel_encoder *encoder;
979e94c1
VS
14685 struct intel_crtc_state *pipe_config = old_crtc_state;
14686 struct drm_atomic_state *state = old_crtc_state->uapi.state;
0385ecea 14687 struct intel_crtc *master = crtc;
045ac3b5 14688
2225f3c6 14689 __drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
58d124ea 14690 intel_crtc_free_hw_state(old_crtc_state);
979e94c1
VS
14691 intel_crtc_state_reset(old_crtc_state, crtc);
14692 old_crtc_state->uapi.state = state;
8af6cf88 14693
cd49f818
WK
14694 drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14695 crtc->base.name);
8af6cf88 14696
504c7bd8
VS
14697 pipe_config->hw.enable = new_crtc_state->hw.enable;
14698
291106cb 14699 intel_crtc_get_pipe_config(pipe_config);
d62cf62a 14700
e56134bc 14701 /* we keep both pipes enabled on 830 */
504c7bd8
VS
14702 if (IS_I830(dev_priv) && pipe_config->hw.active)
14703 pipe_config->hw.active = new_crtc_state->hw.active;
6c49f241 14704
504c7bd8 14705 I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
1326a92c
ML
14706 "crtc active state doesn't match with hw state "
14707 "(expected %i, found %i)\n",
504c7bd8 14708 new_crtc_state->hw.active, pipe_config->hw.active);
0e8ffe1b 14709
1326a92c
ML
14710 I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14711 "transitional active state does not match atomic hw state "
14712 "(expected %i, found %i)\n",
14713 new_crtc_state->hw.active, crtc->active);
4d20cd86 14714
0385ecea
MN
14715 if (new_crtc_state->bigjoiner_slave)
14716 master = new_crtc_state->bigjoiner_linked_crtc;
14717
14718 for_each_encoder_on_crtc(dev, &master->base, encoder) {
e7c84544 14719 enum pipe pipe;
504c7bd8 14720 bool active;
4d20cd86 14721
e7c84544 14722 active = encoder->get_hw_state(encoder, &pipe);
1326a92c
ML
14723 I915_STATE_WARN(active != new_crtc_state->hw.active,
14724 "[ENCODER:%i] active %i with crtc active %i\n",
14725 encoder->base.base.id, active,
14726 new_crtc_state->hw.active);
4d20cd86 14727
0385ecea 14728 I915_STATE_WARN(active && master->pipe != pipe,
e7c84544
ML
14729 "Encoder connected to wrong pipe %c\n",
14730 pipe_name(pipe));
4d20cd86 14731
e1214b95 14732 if (active)
65c1ed30 14733 intel_encoder_get_config(encoder, pipe_config);
e7c84544 14734 }
53d9f4e9 14735
1326a92c 14736 if (!new_crtc_state->hw.active)
e7c84544 14737 return;
cfb23ed6 14738
e7c84544 14739 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 14740
3b4bf24d
ML
14741 if (!intel_pipe_config_compare(new_crtc_state,
14742 pipe_config, false)) {
e7c84544 14743 I915_STATE_WARN(1, "pipe state doesn't match!\n");
10d75f54 14744 intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
3b4bf24d 14745 intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
8af6cf88
DV
14746 }
14747}
14748
cff109f0
VS
14749static void
14750intel_verify_planes(struct intel_atomic_state *state)
14751{
14752 struct intel_plane *plane;
14753 const struct intel_plane_state *plane_state;
14754 int i;
14755
14756 for_each_new_intel_plane_in_state(state, plane,
14757 plane_state, i)
c47b7ddb 14758 assert_plane(plane, plane_state->planar_slave ||
f90a85e7 14759 plane_state->uapi.visible);
cff109f0
VS
14760}
14761
91d1b4bd 14762static void
c0ead703
ML
14763verify_single_dpll_state(struct drm_i915_private *dev_priv,
14764 struct intel_shared_dpll *pll,
3b4bf24d
ML
14765 struct intel_crtc *crtc,
14766 struct intel_crtc_state *new_crtc_state)
91d1b4bd 14767{
91d1b4bd 14768 struct intel_dpll_hw_state dpll_hw_state;
40560e26 14769 unsigned int crtc_mask;
e7c84544 14770 bool active;
5358901f 14771
e7c84544 14772 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 14773
cd49f818 14774 drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
5358901f 14775
fdbc5d68 14776 active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 14777
5cd281f6 14778 if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
e7c84544
ML
14779 I915_STATE_WARN(!pll->on && pll->active_mask,
14780 "pll in active use but not on in sw tracking\n");
14781 I915_STATE_WARN(pll->on && !pll->active_mask,
14782 "pll is on but not used by any active crtc\n");
14783 I915_STATE_WARN(pll->on != active,
14784 "pll on state mismatch (expected %i, found %i)\n",
14785 pll->on, active);
14786 }
5358901f 14787
e7c84544 14788 if (!crtc) {
2c42e535 14789 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
e7c84544 14790 "more active pll users than references: %x vs %x\n",
2c42e535 14791 pll->active_mask, pll->state.crtc_mask);
5358901f 14792
e7c84544
ML
14793 return;
14794 }
14795
3b4bf24d 14796 crtc_mask = drm_crtc_mask(&crtc->base);
e7c84544 14797
1326a92c 14798 if (new_crtc_state->hw.active)
e7c84544
ML
14799 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14800 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
aca9310a 14801 pipe_name(crtc->pipe), pll->active_mask);
e7c84544
ML
14802 else
14803 I915_STATE_WARN(pll->active_mask & crtc_mask,
14804 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
aca9310a 14805 pipe_name(crtc->pipe), pll->active_mask);
2dd66ebd 14806
2c42e535 14807 I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
e7c84544 14808 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
2c42e535 14809 crtc_mask, pll->state.crtc_mask);
66e985c0 14810
2c42e535 14811 I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
e7c84544
ML
14812 &dpll_hw_state,
14813 sizeof(dpll_hw_state)),
14814 "pll hw state mismatch\n");
14815}
14816
14817static void
3b4bf24d
ML
14818verify_shared_dpll_state(struct intel_crtc *crtc,
14819 struct intel_crtc_state *old_crtc_state,
14820 struct intel_crtc_state *new_crtc_state)
e7c84544 14821{
3b4bf24d 14822 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
e7c84544 14823
3b4bf24d
ML
14824 if (new_crtc_state->shared_dpll)
14825 verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
e7c84544 14826
3b4bf24d
ML
14827 if (old_crtc_state->shared_dpll &&
14828 old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14829 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14830 struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
e7c84544
ML
14831
14832 I915_STATE_WARN(pll->active_mask & crtc_mask,
14833 "pll active mismatch (didn't expect pipe %c in active mask)\n",
aca9310a 14834 pipe_name(crtc->pipe));
2c42e535 14835 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
e7c84544 14836 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
aca9310a 14837 pipe_name(crtc->pipe));
5358901f 14838 }
8af6cf88
DV
14839}
14840
e7c84544 14841static void
855e0d68
ML
14842intel_modeset_verify_crtc(struct intel_crtc *crtc,
14843 struct intel_atomic_state *state,
3b4bf24d
ML
14844 struct intel_crtc_state *old_crtc_state,
14845 struct intel_crtc_state *new_crtc_state)
e7c84544 14846{
3b4bf24d 14847 if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
5a21b665
DV
14848 return;
14849
3b4bf24d
ML
14850 verify_wm_state(crtc, new_crtc_state);
14851 verify_connector_state(state, crtc);
14852 verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14853 verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
e7c84544
ML
14854}
14855
14856static void
3b4bf24d 14857verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
e7c84544 14858{
e7c84544
ML
14859 int i;
14860
353ad959
ID
14861 for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14862 verify_single_dpll_state(dev_priv,
14863 &dev_priv->dpll.shared_dplls[i],
14864 NULL, NULL);
e7c84544
ML
14865}
14866
14867static void
3b4bf24d 14868intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
855e0d68 14869 struct intel_atomic_state *state)
e7c84544 14870{
3b4bf24d
ML
14871 verify_encoder_state(dev_priv, state);
14872 verify_connector_state(state, NULL);
14873 verify_disabled_dpll_state(dev_priv);
e7c84544
ML
14874}
14875
99325429
VS
14876static void
14877intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
80715b2f 14878{
2225f3c6 14879 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4f8036a2 14880 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
99325429 14881 const struct drm_display_mode *adjusted_mode =
1326a92c 14882 &crtc_state->hw.adjusted_mode;
99325429
VS
14883
14884 drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
80715b2f 14885
af157b76
VS
14886 crtc->mode_flags = crtc_state->mode_flags;
14887
80715b2f
VS
14888 /*
14889 * The scanline counter increments at the leading edge of hsync.
14890 *
14891 * On most platforms it starts counting from vtotal-1 on the
14892 * first active line. That means the scanline counter value is
14893 * always one less than what we would expect. Ie. just after
14894 * start of vblank, which also occurs at start of hsync (on the
14895 * last active line), the scanline counter will read vblank_start-1.
14896 *
14897 * On gen2 the scanline counter starts counting from 1 instead
14898 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14899 * to keep the value positive), instead of adding one.
14900 *
14901 * On HSW+ the behaviour of the scanline counter depends on the output
14902 * type. For DP ports it behaves like most other platforms, but on HDMI
14903 * there's an extra 1 line difference. So we need to add two instead of
14904 * one to the value.
ec1b4ee2
VS
14905 *
14906 * On VLV/CHV DSI the scanline counter would appear to increment
14907 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14908 * that means we can't tell whether we're in vblank or not while
14909 * we're on that particular line. We must still set scanline_offset
14910 * to 1 so that the vblank timestamps come out correct when we query
14911 * the scanline counter from within the vblank interrupt handler.
14912 * However if queried just before the start of vblank we'll get an
14913 * answer that's slightly in the future.
80715b2f 14914 */
cf819eff 14915 if (IS_GEN(dev_priv, 2)) {
80715b2f
VS
14916 int vtotal;
14917
124abe07
VS
14918 vtotal = adjusted_mode->crtc_vtotal;
14919 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
14920 vtotal /= 2;
14921
14922 crtc->scanline_offset = vtotal - 1;
4f8036a2 14923 } else if (HAS_DDI(dev_priv) &&
f2bdd112 14924 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
80715b2f 14925 crtc->scanline_offset = 2;
99325429 14926 } else {
80715b2f 14927 crtc->scanline_offset = 1;
99325429 14928 }
80715b2f
VS
14929}
14930
c3b1e6c6 14931static void intel_modeset_clear_plls(struct intel_atomic_state *state)
ed6739ef 14932{
c3b1e6c6 14933 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
866955fa 14934 struct intel_crtc_state *new_crtc_state;
c3b1e6c6 14935 struct intel_crtc *crtc;
0a9ab303 14936 int i;
ed6739ef
ACO
14937
14938 if (!dev_priv->display.crtc_compute_clock)
ad421372 14939 return;
ed6739ef 14940
866955fa 14941 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
69f786ae 14942 if (!needs_modeset(new_crtc_state))
225da59b
ACO
14943 continue;
14944
866955fa 14945 intel_release_shared_dplls(state, crtc);
ad421372 14946 }
ed6739ef
ACO
14947}
14948
99d736a2
ML
14949/*
14950 * This implements the workaround described in the "notes" section of the mode
14951 * set sequence documentation. When going from no pipes or single pipe to
14952 * multiple pipes, and planes are enabled after the pipe, we need to wait at
14953 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14954 */
1e98f88c 14955static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
99d736a2 14956{
bca0bfa3
VS
14957 struct intel_crtc_state *crtc_state;
14958 struct intel_crtc *crtc;
99d736a2
ML
14959 struct intel_crtc_state *first_crtc_state = NULL;
14960 struct intel_crtc_state *other_crtc_state = NULL;
14961 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14962 int i;
14963
14964 /* look at all crtc's that are going to be enabled in during modeset */
bca0bfa3 14965 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1326a92c 14966 if (!crtc_state->hw.active ||
69f786ae 14967 !needs_modeset(crtc_state))
99d736a2
ML
14968 continue;
14969
14970 if (first_crtc_state) {
bca0bfa3 14971 other_crtc_state = crtc_state;
99d736a2
ML
14972 break;
14973 } else {
bca0bfa3
VS
14974 first_crtc_state = crtc_state;
14975 first_pipe = crtc->pipe;
99d736a2
ML
14976 }
14977 }
14978
14979 /* No workaround needed? */
14980 if (!first_crtc_state)
14981 return 0;
14982
14983 /* w/a possibly needed, check how many crtc's are already enabled. */
bca0bfa3
VS
14984 for_each_intel_crtc(state->base.dev, crtc) {
14985 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14986 if (IS_ERR(crtc_state))
14987 return PTR_ERR(crtc_state);
99d736a2 14988
bca0bfa3 14989 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
99d736a2 14990
1326a92c 14991 if (!crtc_state->hw.active ||
69f786ae 14992 needs_modeset(crtc_state))
99d736a2
ML
14993 continue;
14994
14995 /* 2 or more enabled crtcs means no need for w/a */
14996 if (enabled_pipe != INVALID_PIPE)
14997 return 0;
14998
bca0bfa3 14999 enabled_pipe = crtc->pipe;
99d736a2
ML
15000 }
15001
15002 if (enabled_pipe != INVALID_PIPE)
15003 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
15004 else if (other_crtc_state)
15005 other_crtc_state->hsw_workaround_pipe = first_pipe;
15006
15007 return 0;
15008}
15009
aac97871
VS
15010u8 intel_calc_active_pipes(struct intel_atomic_state *state,
15011 u8 active_pipes)
15012{
15013 const struct intel_crtc_state *crtc_state;
15014 struct intel_crtc *crtc;
15015 int i;
15016
15017 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15018 if (crtc_state->hw.active)
15019 active_pipes |= BIT(crtc->pipe);
15020 else
15021 active_pipes &= ~BIT(crtc->pipe);
15022 }
15023
15024 return active_pipes;
15025}
15026
5643dd9c 15027static int intel_modeset_checks(struct intel_atomic_state *state)
054518dd 15028{
5643dd9c 15029 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
054518dd 15030
5643dd9c 15031 state->modeset = true;
1d5a95b5 15032
565602d7 15033 if (IS_HASWELL(dev_priv))
1e98f88c 15034 return hsw_mode_set_planes_workaround(state);
99d736a2 15035
ad421372 15036 return 0;
c347a676
ACO
15037}
15038
aa363136
MR
15039/*
15040 * Handle calculation of various watermark data at the end of the atomic check
15041 * phase. The code here should be run after the per-crtc and per-plane 'check'
15042 * handlers to ensure that all derived state has been updated.
15043 */
cd1d3ee9 15044static int calc_watermark_data(struct intel_atomic_state *state)
aa363136 15045{
cd1d3ee9 15046 struct drm_device *dev = state->base.dev;
98d39494 15047 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
15048
15049 /* Is there platform-specific watermark information to calculate? */
15050 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
15051 return dev_priv->display.compute_global_watermarks(state);
15052
15053 return 0;
aa363136
MR
15054}
15055
b124ea43 15056static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
f0521558
VS
15057 struct intel_crtc_state *new_crtc_state)
15058{
b124ea43 15059 if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
f0521558
VS
15060 return;
15061
2225f3c6 15062 new_crtc_state->uapi.mode_changed = false;
f0521558 15063 new_crtc_state->update_pipe = true;
5cb5b370 15064}
f0521558 15065
5cb5b370
JRS
15066static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
15067 struct intel_crtc_state *new_crtc_state)
15068{
f0521558
VS
15069 /*
15070 * If we're not doing the full modeset we want to
15071 * keep the current M/N values as they may be
15072 * sufficiently different to the computed values
15073 * to cause problems.
15074 *
15075 * FIXME: should really copy more fuzzy state here
15076 */
15077 new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
15078 new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
15079 new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
15080 new_crtc_state->has_drrs = old_crtc_state->has_drrs;
15081}
15082
bb6ae9e6
VS
15083static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
15084 struct intel_crtc *crtc,
15085 u8 plane_ids_mask)
131d3b1a 15086{
bb6ae9e6
VS
15087 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15088 struct intel_plane *plane;
15089
15090 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
15091 struct intel_plane_state *plane_state;
15092
15093 if ((plane_ids_mask & BIT(plane->id)) == 0)
15094 continue;
15095
15096 plane_state = intel_atomic_get_plane_state(state, plane);
15097 if (IS_ERR(plane_state))
15098 return PTR_ERR(plane_state);
15099 }
15100
15101 return 0;
15102}
15103
15104static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
15105{
15106 /* See {hsw,vlv,ivb}_plane_ratio() */
15107 return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
15108 IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
cf129762 15109 IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
bb6ae9e6
VS
15110}
15111
1cf6adb7
VS
15112static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
15113 struct intel_crtc *crtc,
15114 struct intel_crtc *other)
15115{
15116 const struct intel_plane_state *plane_state;
15117 struct intel_plane *plane;
15118 u8 plane_ids = 0;
15119 int i;
15120
15121 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15122 if (plane->pipe == crtc->pipe)
15123 plane_ids |= BIT(plane->id);
15124 }
15125
15126 return intel_crtc_add_planes_to_state(state, other, plane_ids);
15127}
15128
15129static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
15130{
15131 const struct intel_crtc_state *crtc_state;
15132 struct intel_crtc *crtc;
15133 int i;
15134
15135 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15136 int ret;
15137
15138 if (!crtc_state->bigjoiner)
15139 continue;
15140
15141 ret = intel_crtc_add_bigjoiner_planes(state, crtc,
15142 crtc_state->bigjoiner_linked_crtc);
15143 if (ret)
15144 return ret;
15145 }
15146
15147 return 0;
15148}
15149
4f0b4352 15150static int intel_atomic_check_planes(struct intel_atomic_state *state)
bb6ae9e6
VS
15151{
15152 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15153 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
131d3b1a
VS
15154 struct intel_plane_state *plane_state;
15155 struct intel_plane *plane;
bb6ae9e6 15156 struct intel_crtc *crtc;
131d3b1a
VS
15157 int i, ret;
15158
bb6ae9e6
VS
15159 ret = icl_add_linked_planes(state);
15160 if (ret)
15161 return ret;
15162
1cf6adb7
VS
15163 ret = intel_bigjoiner_add_affected_planes(state);
15164 if (ret)
15165 return ret;
15166
131d3b1a
VS
15167 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15168 ret = intel_plane_atomic_check(state, plane);
15169 if (ret) {
cd49f818
WK
15170 drm_dbg_atomic(&dev_priv->drm,
15171 "[PLANE:%d:%s] atomic driver check failed\n",
15172 plane->base.base.id, plane->base.name);
131d3b1a
VS
15173 return ret;
15174 }
15175 }
15176
bb6ae9e6
VS
15177 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15178 new_crtc_state, i) {
15179 u8 old_active_planes, new_active_planes;
15180
15181 ret = icl_check_nv12_planes(new_crtc_state);
15182 if (ret)
15183 return ret;
15184
15185 /*
15186 * On some platforms the number of active planes affects
15187 * the planes' minimum cdclk calculation. Add such planes
15188 * to the state before we compute the minimum cdclk.
15189 */
15190 if (!active_planes_affects_min_cdclk(dev_priv))
15191 continue;
15192
15193 old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
15194 new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
15195
9877c37e
SL
15196 /*
15197 * Not only the number of planes, but if the plane configuration had
15198 * changed might already mean we need to recompute min CDCLK,
15199 * because different planes might consume different amount of Dbuf bandwidth
15200 * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
15201 */
15202 if (old_active_planes == new_active_planes)
bb6ae9e6
VS
15203 continue;
15204
15205 ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
15206 if (ret)
15207 return ret;
15208 }
15209
4f0b4352
SL
15210 return 0;
15211}
15212
15213static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
15214 bool *need_cdclk_calc)
15215{
cd191546 15216 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
dc98f50f
VS
15217 const struct intel_cdclk_state *old_cdclk_state;
15218 const struct intel_cdclk_state *new_cdclk_state;
4f0b4352 15219 struct intel_plane_state *plane_state;
19aefbc7 15220 struct intel_bw_state *new_bw_state;
4f0b4352 15221 struct intel_plane *plane;
19aefbc7
SL
15222 int min_cdclk = 0;
15223 enum pipe pipe;
4f0b4352 15224 int ret;
19aefbc7 15225 int i;
bb6ae9e6
VS
15226 /*
15227 * active_planes bitmask has been updated, and potentially
15228 * affected planes are part of the state. We can now
15229 * compute the minimum cdclk for each plane.
15230 */
28a30b45
VS
15231 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
15232 ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
15233 if (ret)
15234 return ret;
15235 }
bb6ae9e6 15236
dc98f50f 15237 old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
cd191546
SL
15238 new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
15239
dc98f50f
VS
15240 if (new_cdclk_state &&
15241 old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
cd191546
SL
15242 *need_cdclk_calc = true;
15243
15244 ret = dev_priv->display.bw_calc_min_cdclk(state);
15245 if (ret)
15246 return ret;
15247
19aefbc7 15248 new_bw_state = intel_atomic_get_new_bw_state(state);
cd191546 15249
19aefbc7
SL
15250 if (!new_cdclk_state || !new_bw_state)
15251 return 0;
cd191546 15252
19aefbc7
SL
15253 for_each_pipe(dev_priv, pipe) {
15254 min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
cd191546
SL
15255
15256 /*
15257 * Currently do this change only if we need to increase
15258 */
19aefbc7 15259 if (new_bw_state->min_cdclk > min_cdclk)
cd191546
SL
15260 *need_cdclk_calc = true;
15261 }
15262
131d3b1a
VS
15263 return 0;
15264}
15265
15266static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
15267{
15268 struct intel_crtc_state *crtc_state;
15269 struct intel_crtc *crtc;
15270 int i;
15271
15272 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
cd49f818 15273 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
eba10ec8
VS
15274 int ret;
15275
15276 ret = intel_crtc_atomic_check(state, crtc);
131d3b1a 15277 if (ret) {
cd49f818
WK
15278 drm_dbg_atomic(&i915->drm,
15279 "[CRTC:%d:%s] atomic driver check failed\n",
15280 crtc->base.base.id, crtc->base.name);
131d3b1a
VS
15281 return ret;
15282 }
15283 }
15284
15285 return 0;
15286}
15287
d0eed154
VS
15288static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
15289 u8 transcoders)
080d47bf 15290{
d0eed154 15291 const struct intel_crtc_state *new_crtc_state;
080d47bf
JRS
15292 struct intel_crtc *crtc;
15293 int i;
15294
d0eed154
VS
15295 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15296 if (new_crtc_state->hw.enable &&
15297 transcoders & BIT(new_crtc_state->cpu_transcoder) &&
15298 needs_modeset(new_crtc_state))
15299 return true;
15300 }
080d47bf
JRS
15301
15302 return false;
15303}
15304
19f65a3d
ML
15305static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
15306 struct intel_crtc *crtc,
15307 struct intel_crtc_state *old_crtc_state,
15308 struct intel_crtc_state *new_crtc_state)
15309{
15310 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15311 struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
15312 struct intel_crtc *slave, *master;
15313
15314 /* slave being enabled, is master is still claiming this crtc? */
15315 if (old_crtc_state->bigjoiner_slave) {
15316 slave = crtc;
15317 master = old_crtc_state->bigjoiner_linked_crtc;
15318 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
15319 if (!master_crtc_state || !needs_modeset(master_crtc_state))
15320 goto claimed;
15321 }
15322
15323 if (!new_crtc_state->bigjoiner)
15324 return 0;
15325
15326 if (1 + crtc->pipe >= INTEL_NUM_PIPES(dev_priv)) {
15327 DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
15328 "CRTC + 1 to be used, doesn't exist\n",
15329 crtc->base.base.id, crtc->base.name);
15330 return -EINVAL;
15331 }
15332
15333 slave = new_crtc_state->bigjoiner_linked_crtc =
15334 intel_get_crtc_for_pipe(dev_priv, crtc->pipe + 1);
15335 slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
15336 master = crtc;
15337 if (IS_ERR(slave_crtc_state))
15338 return PTR_ERR(slave_crtc_state);
15339
15340 /* master being enabled, slave was already configured? */
15341 if (slave_crtc_state->uapi.enable)
15342 goto claimed;
15343
15344 DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
15345 slave->base.base.id, slave->base.name);
15346
15347 return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
15348
15349claimed:
15350 DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
15351 "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
15352 slave->base.base.id, slave->base.name,
15353 master->base.base.id, master->base.name);
15354 return -EINVAL;
15355}
15356
15357static int kill_bigjoiner_slave(struct intel_atomic_state *state,
15358 struct intel_crtc_state *master_crtc_state)
15359{
15360 struct intel_crtc_state *slave_crtc_state =
15361 intel_atomic_get_crtc_state(&state->base,
15362 master_crtc_state->bigjoiner_linked_crtc);
15363
15364 if (IS_ERR(slave_crtc_state))
15365 return PTR_ERR(slave_crtc_state);
15366
15367 slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
15368 slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
15369 slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
15370 intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
15371 return 0;
15372}
15373
6914c968
K
15374/**
15375 * DOC: asynchronous flip implementation
15376 *
15377 * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
15378 * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
15379 * Correspondingly, support is currently added for primary plane only.
15380 *
15381 * Async flip can only change the plane surface address, so anything else
15382 * changing is rejected from the intel_atomic_check_async() function.
15383 * Once this check is cleared, flip done interrupt is enabled using
15384 * the skl_enable_flip_done() function.
15385 *
15386 * As soon as the surface address register is written, flip done interrupt is
15387 * generated and the requested events are sent to the usersapce in the interrupt
15388 * handler itself. The timestamp and sequence sent during the flip done event
15389 * correspond to the last vblank and have no relation to the actual time when
15390 * the flip done event was sent.
15391 */
30ff93af
K
15392static int intel_atomic_check_async(struct intel_atomic_state *state)
15393{
15394 struct drm_i915_private *i915 = to_i915(state->base.dev);
15395 const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15396 const struct intel_plane_state *new_plane_state, *old_plane_state;
15397 struct intel_crtc *crtc;
15398 struct intel_plane *plane;
15399 int i;
15400
15401 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15402 new_crtc_state, i) {
15403 if (needs_modeset(new_crtc_state)) {
15404 drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
15405 return -EINVAL;
15406 }
15407
15408 if (!new_crtc_state->hw.active) {
15409 drm_dbg_kms(&i915->drm, "CRTC inactive\n");
15410 return -EINVAL;
15411 }
15412 if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
15413 drm_dbg_kms(&i915->drm,
15414 "Active planes cannot be changed during async flip\n");
15415 return -EINVAL;
15416 }
15417 }
15418
15419 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15420 new_plane_state, i) {
15421 /*
15422 * TODO: Async flip is only supported through the page flip IOCTL
15423 * as of now. So support currently added for primary plane only.
15424 * Support for other planes on platforms on which supports
15425 * this(vlv/chv and icl+) should be added when async flip is
15426 * enabled in the atomic IOCTL path.
15427 */
15428 if (plane->id != PLANE_PRIMARY)
15429 return -EINVAL;
15430
15431 /*
15432 * FIXME: This check is kept generic for all platforms.
15433 * Need to verify this for all gen9 and gen10 platforms to enable
15434 * this selectively if required.
15435 */
15436 switch (new_plane_state->hw.fb->modifier) {
15437 case I915_FORMAT_MOD_X_TILED:
15438 case I915_FORMAT_MOD_Y_TILED:
15439 case I915_FORMAT_MOD_Yf_TILED:
15440 break;
15441 default:
15442 drm_dbg_kms(&i915->drm,
15443 "Linear memory/CCS does not support async flips\n");
15444 return -EINVAL;
15445 }
15446
15447 if (old_plane_state->color_plane[0].stride !=
15448 new_plane_state->color_plane[0].stride) {
15449 drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
15450 return -EINVAL;
15451 }
15452
15453 if (old_plane_state->hw.fb->modifier !=
15454 new_plane_state->hw.fb->modifier) {
15455 drm_dbg_kms(&i915->drm,
15456 "Framebuffer modifiers cannot be changed in async flip\n");
15457 return -EINVAL;
15458 }
15459
15460 if (old_plane_state->hw.fb->format !=
15461 new_plane_state->hw.fb->format) {
15462 drm_dbg_kms(&i915->drm,
15463 "Framebuffer format cannot be changed in async flip\n");
15464 return -EINVAL;
15465 }
15466
15467 if (old_plane_state->hw.rotation !=
15468 new_plane_state->hw.rotation) {
15469 drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
15470 return -EINVAL;
15471 }
15472
15473 if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
15474 !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
15475 drm_dbg_kms(&i915->drm,
15476 "Plane size/co-ordinates cannot be changed in async flip\n");
15477 return -EINVAL;
15478 }
15479
15480 if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
15481 drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
15482 return -EINVAL;
15483 }
15484
15485 if (old_plane_state->hw.pixel_blend_mode !=
15486 new_plane_state->hw.pixel_blend_mode) {
15487 drm_dbg_kms(&i915->drm,
15488 "Pixel blend mode cannot be changed in async flip\n");
15489 return -EINVAL;
15490 }
15491
15492 if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
15493 drm_dbg_kms(&i915->drm,
15494 "Color encoding cannot be changed in async flip\n");
15495 return -EINVAL;
15496 }
15497
15498 if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
15499 drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
15500 return -EINVAL;
15501 }
15502 }
15503
15504 return 0;
15505}
15506
d321634b
VS
15507static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
15508{
15509 const struct intel_crtc_state *crtc_state;
15510 struct intel_crtc *crtc;
15511 int i;
15512
15513 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15514 struct intel_crtc_state *linked_crtc_state;
15515
15516 if (!crtc_state->bigjoiner)
15517 continue;
15518
15519 linked_crtc_state = intel_atomic_get_crtc_state(&state->base,
15520 crtc_state->bigjoiner_linked_crtc);
15521 if (IS_ERR(linked_crtc_state))
15522 return PTR_ERR(linked_crtc_state);
15523 }
15524
15525 return 0;
15526}
15527
74c090b1
ML
15528/**
15529 * intel_atomic_check - validate state object
15530 * @dev: drm device
70972f51 15531 * @_state: state to validate
74c090b1
ML
15532 */
15533static int intel_atomic_check(struct drm_device *dev,
9a86a07c 15534 struct drm_atomic_state *_state)
c347a676 15535{
dd8b3bdb 15536 struct drm_i915_private *dev_priv = to_i915(dev);
9a86a07c
VS
15537 struct intel_atomic_state *state = to_intel_atomic_state(_state);
15538 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15539 struct intel_crtc *crtc;
c347a676 15540 int ret, i;
1d5a95b5 15541 bool any_ms = false;
c347a676 15542
9a86a07c
VS
15543 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15544 new_crtc_state, i) {
a227569d 15545 if (new_crtc_state->inherited != old_crtc_state->inherited)
2225f3c6 15546 new_crtc_state->uapi.mode_changed = true;
8c58f73c
ML
15547 }
15548
9a86a07c 15549 ret = drm_atomic_helper_check_modeset(dev, &state->base);
054518dd 15550 if (ret)
2833920d 15551 goto fail;
054518dd 15552
d321634b
VS
15553 ret = intel_bigjoiner_add_affected_crtcs(state);
15554 if (ret)
15555 goto fail;
15556
9a86a07c
VS
15557 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15558 new_crtc_state, i) {
58d124ea
ML
15559 if (!needs_modeset(new_crtc_state)) {
15560 /* Light copy */
aa07c1d3 15561 intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
58d124ea 15562
c347a676 15563 continue;
58d124ea 15564 }
c347a676 15565
19f65a3d
ML
15566 /* Kill old bigjoiner link, we may re-establish afterwards */
15567 if (old_crtc_state->bigjoiner && !old_crtc_state->bigjoiner_slave) {
15568 ret = kill_bigjoiner_slave(state, new_crtc_state);
15569 if (ret)
15570 goto fail;
15571 }
15572
15573 if (!new_crtc_state->uapi.enable) {
15574 if (!new_crtc_state->bigjoiner_slave) {
15575 intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
15576 any_ms = true;
15577 }
15578 continue;
15579 }
15580
aa07c1d3 15581 ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
58d124ea
ML
15582 if (ret)
15583 goto fail;
15584
aa07c1d3 15585 ret = intel_modeset_pipe_config(state, new_crtc_state);
2833920d
VS
15586 if (ret)
15587 goto fail;
19f65a3d
ML
15588
15589 ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
15590 new_crtc_state);
15591 if (ret)
15592 goto fail;
f2801424
VS
15593 }
15594
15595 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15596 new_crtc_state, i) {
15597 if (!needs_modeset(new_crtc_state))
15598 continue;
15599
15600 ret = intel_modeset_pipe_config_late(new_crtc_state);
15601 if (ret)
15602 goto fail;
c347a676 15603
f0521558 15604 intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
5cb5b370 15605 }
26495481 15606
080d47bf
JRS
15607 /**
15608 * Check if fastset is allowed by external dependencies like other
15609 * pipes and transcoders.
15610 *
15611 * Right now it only forces a fullmodeset when the MST master
15612 * transcoder did not changed but the pipe of the master transcoder
05a8e451
JRS
15613 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
15614 * in case of port synced crtcs, if one of the synced crtcs
15615 * needs a full modeset, all other synced crtcs should be
15616 * forced a full modeset.
080d47bf
JRS
15617 */
15618 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
05a8e451 15619 if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
080d47bf
JRS
15620 continue;
15621
05a8e451
JRS
15622 if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
15623 enum transcoder master = new_crtc_state->mst_master_transcoder;
15624
d0eed154 15625 if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
05a8e451
JRS
15626 new_crtc_state->uapi.mode_changed = true;
15627 new_crtc_state->update_pipe = false;
15628 }
d0eed154
VS
15629 }
15630
15631 if (is_trans_port_sync_mode(new_crtc_state)) {
8c47eb86
MN
15632 u8 trans = new_crtc_state->sync_mode_slaves_mask;
15633
15634 if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
15635 trans |= BIT(new_crtc_state->master_transcoder);
05a8e451 15636
d0eed154
VS
15637 if (intel_cpu_transcoders_need_modeset(state, trans)) {
15638 new_crtc_state->uapi.mode_changed = true;
15639 new_crtc_state->update_pipe = false;
15640 }
080d47bf
JRS
15641 }
15642 }
15643
5cb5b370
JRS
15644 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15645 new_crtc_state, i) {
15646 if (needs_modeset(new_crtc_state)) {
26495481 15647 any_ms = true;
5cb5b370
JRS
15648 continue;
15649 }
15650
15651 if (!new_crtc_state->update_pipe)
15652 continue;
15653
15654 intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
c347a676
ACO
15655 }
15656
bf5da83e 15657 if (any_ms && !check_digital_port_conflicts(state)) {
cd49f818
WK
15658 drm_dbg_kms(&dev_priv->drm,
15659 "rejecting conflicting digital port configuration\n");
66b51b80 15660 ret = -EINVAL;
bf5da83e
VS
15661 goto fail;
15662 }
15663
9a86a07c 15664 ret = drm_dp_mst_atomic_check(&state->base);
eceae147 15665 if (ret)
2833920d 15666 goto fail;
eceae147 15667
4f0b4352 15668 ret = intel_atomic_check_planes(state);
bb6ae9e6
VS
15669 if (ret)
15670 goto fail;
15671
0f8839f5
VS
15672 /*
15673 * distrust_bios_wm will force a full dbuf recomputation
15674 * but the hardware state will only get updated accordingly
15675 * if state->modeset==true. Hence distrust_bios_wm==true &&
15676 * state->modeset==false is an invalid combination which
15677 * would cause the hardware and software dbuf state to get
15678 * out of sync. We must prevent that.
15679 *
15680 * FIXME clean up this mess and introduce better
15681 * state tracking for dbuf.
15682 */
15683 if (dev_priv->wm.distrust_bios_wm)
15684 any_ms = true;
15685
9a86a07c
VS
15686 intel_fbc_choose_crtc(dev_priv, state);
15687 ret = calc_watermark_data(state);
c457d9cf 15688 if (ret)
2833920d 15689 goto fail;
c457d9cf 15690
9a86a07c 15691 ret = intel_bw_atomic_check(state);
c457d9cf 15692 if (ret)
2833920d 15693 goto fail;
c457d9cf 15694
4f0b4352
SL
15695 ret = intel_atomic_check_cdclk(state, &any_ms);
15696 if (ret)
15697 goto fail;
15698
c93b9b2c 15699 if (any_ms) {
cf696856
KV
15700 ret = intel_modeset_checks(state);
15701 if (ret)
15702 goto fail;
15703
c93b9b2c
SL
15704 ret = intel_modeset_calc_cdclk(state);
15705 if (ret)
15706 return ret;
15707
15708 intel_modeset_clear_plls(state);
15709 }
15710
15711 ret = intel_atomic_check_crtcs(state);
15712 if (ret)
15713 goto fail;
15714
a0e70104
VS
15715 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15716 new_crtc_state, i) {
30ff93af
K
15717 if (new_crtc_state->uapi.async_flip) {
15718 ret = intel_atomic_check_async(state);
15719 if (ret)
15720 goto fail;
15721 }
15722
69f786ae 15723 if (!needs_modeset(new_crtc_state) &&
a0e70104
VS
15724 !new_crtc_state->update_pipe)
15725 continue;
15726
10d75f54 15727 intel_dump_pipe_config(new_crtc_state, state,
69f786ae 15728 needs_modeset(new_crtc_state) ?
a0e70104
VS
15729 "[modeset]" : "[fastset]");
15730 }
15731
c457d9cf 15732 return 0;
2833920d
VS
15733
15734 fail:
15735 if (ret == -EDEADLK)
15736 return ret;
15737
15738 /*
15739 * FIXME would probably be nice to know which crtc specifically
15740 * caused the failure, in cases where we can pinpoint it.
15741 */
15742 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15743 new_crtc_state, i)
10d75f54 15744 intel_dump_pipe_config(new_crtc_state, state, "[failed]");
2833920d
VS
15745
15746 return ret;
054518dd
ACO
15747}
15748
a85fb467 15749static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
5008e874 15750{
afeda4f3
AM
15751 struct intel_crtc_state *crtc_state;
15752 struct intel_crtc *crtc;
15753 int i, ret;
15754
15755 ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
15756 if (ret < 0)
15757 return ret;
15758
15759 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15760 bool mode_changed = needs_modeset(crtc_state);
15761
15762 if (mode_changed || crtc_state->update_pipe ||
15763 crtc_state->uapi.color_mgmt_changed) {
15764 intel_dsb_prepare(crtc_state);
15765 }
15766 }
15767
15768 return 0;
5008e874
ML
15769}
15770
a2991414
ML
15771u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
15772{
15773 struct drm_device *dev = crtc->base.dev;
32db0b65 15774 struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
a2991414 15775
32db0b65 15776 if (!vblank->max_vblank_count)
734cbbf3 15777 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
a2991414 15778
08fa8fd0 15779 return crtc->base.funcs->get_vblank_counter(&crtc->base);
a2991414
ML
15780}
15781
d8bd3e15
ML
15782void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
15783 struct intel_crtc_state *crtc_state)
15784{
15785 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15786
cfdd1747 15787 if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
d8bd3e15
ML
15788 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15789
15790 if (crtc_state->has_pch_encoder) {
15791 enum pipe pch_transcoder =
15792 intel_crtc_pch_transcoder(crtc);
15793
15794 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
15795 }
15796}
15797
15798static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
15799 const struct intel_crtc_state *new_crtc_state)
15800{
2225f3c6 15801 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
d8bd3e15
ML
15802 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15803
d8bd3e15
ML
15804 /*
15805 * Update pipe size and adjust fitter if needed: the reason for this is
15806 * that in compute_mode_changes we check the native mode (not the pfit
15807 * mode) to see if we can flip rather than do a full mode set. In the
15808 * fastboot case, we'll flip, but if we don't update the pipesrc and
15809 * pfit state, we'll end up with a big fb scanned out into the wrong
15810 * sized surface.
15811 */
15812 intel_set_pipe_src_size(new_crtc_state);
15813
15814 /* on skylake this is done by detaching scalers */
15815 if (INTEL_GEN(dev_priv) >= 9) {
15816 skl_detach_scalers(new_crtc_state);
15817
15818 if (new_crtc_state->pch_pfit.enabled)
f6df4d46 15819 skl_pfit_enable(new_crtc_state);
d8bd3e15
ML
15820 } else if (HAS_PCH_SPLIT(dev_priv)) {
15821 if (new_crtc_state->pch_pfit.enabled)
9eae5e27 15822 ilk_pfit_enable(new_crtc_state);
d8bd3e15 15823 else if (old_crtc_state->pch_pfit.enabled)
9eae5e27 15824 ilk_pfit_disable(old_crtc_state);
d8bd3e15
ML
15825 }
15826
6dcde047
VS
15827 /*
15828 * The register is supposedly single buffered so perhaps
15829 * not 100% correct to do this here. But SKL+ calculate
15830 * this based on the adjust pixel rate so pfit changes do
15831 * affect it and so it must be updated for fastsets.
15832 * HSW/BDW only really need this here for fastboot, after
15833 * that the value should not change without a full modeset.
15834 */
15835 if (INTEL_GEN(dev_priv) >= 9 ||
15836 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
15837 hsw_set_linetime_wm(new_crtc_state);
15838
d8bd3e15
ML
15839 if (INTEL_GEN(dev_priv) >= 11)
15840 icl_set_pipe_chicken(crtc);
15841}
15842
15843static void commit_pipe_config(struct intel_atomic_state *state,
b932da3c 15844 struct intel_crtc *crtc)
d8bd3e15
ML
15845{
15846 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
b932da3c
VS
15847 const struct intel_crtc_state *old_crtc_state =
15848 intel_atomic_get_old_crtc_state(state, crtc);
15849 const struct intel_crtc_state *new_crtc_state =
15850 intel_atomic_get_new_crtc_state(state, crtc);
d8bd3e15
ML
15851 bool modeset = needs_modeset(new_crtc_state);
15852
15853 /*
15854 * During modesets pipe configuration was programmed as the
15855 * CRTC was enabled.
15856 */
15857 if (!modeset) {
2225f3c6 15858 if (new_crtc_state->uapi.color_mgmt_changed ||
d8bd3e15
ML
15859 new_crtc_state->update_pipe)
15860 intel_color_commit(new_crtc_state);
15861
15862 if (INTEL_GEN(dev_priv) >= 9)
15863 skl_detach_scalers(new_crtc_state);
15864
15865 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15866 bdw_set_pipemisc(new_crtc_state);
15867
15868 if (new_crtc_state->update_pipe)
15869 intel_pipe_fastset(old_crtc_state, new_crtc_state);
6e43e276
JRS
15870
15871 intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
d8bd3e15
ML
15872 }
15873
15874 if (dev_priv->display.atomic_update_watermarks)
7a8fdb1f 15875 dev_priv->display.atomic_update_watermarks(state, crtc);
d8bd3e15
ML
15876}
15877
b932da3c
VS
15878static void intel_enable_crtc(struct intel_atomic_state *state,
15879 struct intel_crtc *crtc)
896e5bb0 15880{
d8bd3e15 15881 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
b932da3c
VS
15882 const struct intel_crtc_state *new_crtc_state =
15883 intel_atomic_get_new_crtc_state(state, crtc);
896e5bb0 15884
b932da3c
VS
15885 if (!needs_modeset(new_crtc_state))
15886 return;
99325429 15887
b932da3c 15888 intel_crtc_update_active_timings(new_crtc_state);
033b7a23 15889
b932da3c
VS
15890 dev_priv->display.crtc_enable(state, crtc);
15891
4e3cdb45
MN
15892 if (new_crtc_state->bigjoiner_slave)
15893 return;
15894
b932da3c
VS
15895 /* vblanks work again, re-enable pipe CRC. */
15896 intel_crtc_enable_pipe_crc(crtc);
15897}
15898
15899static void intel_update_crtc(struct intel_atomic_state *state,
15900 struct intel_crtc *crtc)
15901{
15902 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15903 const struct intel_crtc_state *old_crtc_state =
15904 intel_atomic_get_old_crtc_state(state, crtc);
15905 struct intel_crtc_state *new_crtc_state =
15906 intel_atomic_get_new_crtc_state(state, crtc);
15907 bool modeset = needs_modeset(new_crtc_state);
15908
15909 if (!modeset) {
0ccc42a2
VS
15910 if (new_crtc_state->preload_luts &&
15911 (new_crtc_state->uapi.color_mgmt_changed ||
15912 new_crtc_state->update_pipe))
15913 intel_color_load_luts(new_crtc_state);
15914
bee43ca4 15915 intel_pre_plane_update(state, crtc);
608ed4ab 15916
855e0d68 15917 if (new_crtc_state->update_pipe)
021ba100 15918 intel_encoders_update_pipe(state, crtc);
896e5bb0
L
15919 }
15920
855e0d68
ML
15921 if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15922 intel_fbc_disable(crtc);
9ecc6eab
VS
15923 else
15924 intel_fbc_enable(state, crtc);
896e5bb0 15925
d8bd3e15
ML
15926 /* Perform vblank evasion around commit operation */
15927 intel_pipe_update_start(new_crtc_state);
15928
b932da3c 15929 commit_pipe_config(state, crtc);
6c246b81 15930
5f2e5112 15931 if (INTEL_GEN(dev_priv) >= 9)
855e0d68 15932 skl_update_planes_on_crtc(state, crtc);
5f2e5112 15933 else
855e0d68 15934 i9xx_update_planes_on_crtc(state, crtc);
6c246b81 15935
d8bd3e15
ML
15936 intel_pipe_update_end(new_crtc_state);
15937
15938 /*
15939 * We usually enable FIFO underrun interrupts as part of the
15940 * CRTC enable sequence during modesets. But when we inherit a
15941 * valid pipe configuration from the BIOS we need to take care
15942 * of enabling them on the CRTC's first fastset.
15943 */
15944 if (new_crtc_state->update_pipe && !modeset &&
a227569d 15945 old_crtc_state->inherited)
d8bd3e15 15946 intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
896e5bb0
L
15947}
15948
eadf6f91 15949
66d9cec8
MN
15950static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15951 struct intel_crtc_state *old_crtc_state,
15952 struct intel_crtc_state *new_crtc_state,
15953 struct intel_crtc *crtc)
15954{
15955 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15956
4e3cdb45
MN
15957 drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
15958
66d9cec8
MN
15959 intel_crtc_disable_planes(state, crtc);
15960
4e3cdb45
MN
15961 /*
15962 * We still need special handling for disabling bigjoiner master
15963 * and slaves since for slave we do not have encoder or plls
15964 * so we dont need to disable those.
15965 */
15966 if (old_crtc_state->bigjoiner) {
15967 intel_crtc_disable_planes(state,
15968 old_crtc_state->bigjoiner_linked_crtc);
15969 old_crtc_state->bigjoiner_linked_crtc->active = false;
15970 }
15971
66d9cec8
MN
15972 /*
15973 * We need to disable pipe CRC before disabling the pipe,
15974 * or we race against vblank off.
15975 */
15976 intel_crtc_disable_pipe_crc(crtc);
15977
7451a074 15978 dev_priv->display.crtc_disable(state, crtc);
66d9cec8
MN
15979 crtc->active = false;
15980 intel_fbc_disable(crtc);
15981 intel_disable_shared_dpll(old_crtc_state);
15982
66d9cec8 15983 /* FIXME unify this for all platforms */
1326a92c 15984 if (!new_crtc_state->hw.active &&
66d9cec8
MN
15985 !HAS_GMCH(dev_priv) &&
15986 dev_priv->display.initial_watermarks)
7a8fdb1f 15987 dev_priv->display.initial_watermarks(state, crtc);
66d9cec8
MN
15988}
15989
15990static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15991{
15992 struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15993 struct intel_crtc *crtc;
ad457191 15994 u32 handled = 0;
66d9cec8
MN
15995 int i;
15996
6671c367 15997 /* Only disable port sync and MST slaves */
ad457191
JRS
15998 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15999 new_crtc_state, i) {
4e3cdb45 16000 if (!needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
66d9cec8
MN
16001 continue;
16002
ad457191
JRS
16003 if (!old_crtc_state->hw.active)
16004 continue;
16005
a6c948f9
MN
16006 /* In case of Transcoder port Sync master slave CRTCs can be
16007 * assigned in any order and we need to make sure that
16008 * slave CRTCs are disabled first and then master CRTC since
16009 * Slave vblanks are masked till Master Vblanks.
16010 */
6671c367
JRS
16011 if (!is_trans_port_sync_slave(old_crtc_state) &&
16012 !intel_dp_mst_is_slave_trans(old_crtc_state))
ad457191 16013 continue;
66d9cec8 16014
ad457191
JRS
16015 intel_pre_plane_update(state, crtc);
16016 intel_old_crtc_state_disables(state, old_crtc_state,
16017 new_crtc_state, crtc);
16018 handled |= BIT(crtc->pipe);
16019 }
16020
16021 /* Disable everything else left on */
16022 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16023 new_crtc_state, i) {
16024 if (!needs_modeset(new_crtc_state) ||
4e3cdb45
MN
16025 (handled & BIT(crtc->pipe)) ||
16026 old_crtc_state->bigjoiner_slave)
ad457191
JRS
16027 continue;
16028
16029 intel_pre_plane_update(state, crtc);
4e3cdb45
MN
16030 if (old_crtc_state->bigjoiner) {
16031 struct intel_crtc *slave =
16032 old_crtc_state->bigjoiner_linked_crtc;
16033
16034 intel_pre_plane_update(state, slave);
16035 }
16036
ad457191
JRS
16037 if (old_crtc_state->hw.active)
16038 intel_old_crtc_state_disables(state, old_crtc_state,
16039 new_crtc_state, crtc);
66d9cec8
MN
16040 }
16041}
16042
0c841271 16043static void intel_commit_modeset_enables(struct intel_atomic_state *state)
896e5bb0 16044{
b932da3c 16045 struct intel_crtc_state *new_crtc_state;
855e0d68 16046 struct intel_crtc *crtc;
896e5bb0
L
16047 int i;
16048
b932da3c 16049 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
1326a92c 16050 if (!new_crtc_state->hw.active)
896e5bb0
L
16051 continue;
16052
b932da3c
VS
16053 intel_enable_crtc(state, crtc);
16054 intel_update_crtc(state, crtc);
896e5bb0
L
16055 }
16056}
16057
0c841271 16058static void skl_commit_modeset_enables(struct intel_atomic_state *state)
27082493 16059{
855e0d68
ML
16060 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
16061 struct intel_crtc *crtc;
16062 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
53cc6880 16063 struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
659f1415 16064 u8 update_pipes = 0, modeset_pipes = 0;
601a9ee0 16065 int i;
5eff503b 16066
601a9ee0 16067 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
ee34801c
VS
16068 enum pipe pipe = crtc->pipe;
16069
659f1415
JRS
16070 if (!new_crtc_state->hw.active)
16071 continue;
16072
5eff503b 16073 /* ignore allocations for crtc's that have been turned off. */
659f1415 16074 if (!needs_modeset(new_crtc_state)) {
ee34801c
VS
16075 entries[pipe] = old_crtc_state->wm.skl.ddb;
16076 update_pipes |= BIT(pipe);
659f1415 16077 } else {
ee34801c 16078 modeset_pipes |= BIT(pipe);
659f1415 16079 }
601a9ee0 16080 }
27082493
L
16081
16082 /*
16083 * Whenever the number of active pipes changes, we need to make sure we
16084 * update the pipes in the right order so that their ddb allocations
659f1415 16085 * never overlap with each other between CRTC updates. Otherwise we'll
27082493 16086 * cause pipe underruns and other bad stuff.
659f1415
JRS
16087 *
16088 * So first lets enable all pipes that do not need a fullmodeset as
16089 * those don't have any external dependency.
27082493 16090 */
659f1415 16091 while (update_pipes) {
601a9ee0
VS
16092 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16093 new_crtc_state, i) {
36b53a29 16094 enum pipe pipe = crtc->pipe;
27082493 16095
659f1415 16096 if ((update_pipes & BIT(pipe)) == 0)
27082493 16097 continue;
5eff503b 16098
855e0d68 16099 if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7a0a6ee7 16100 entries, I915_MAX_PIPES, pipe))
27082493
L
16101 continue;
16102
ee34801c 16103 entries[pipe] = new_crtc_state->wm.skl.ddb;
659f1415
JRS
16104 update_pipes &= ~BIT(pipe);
16105
b932da3c 16106 intel_update_crtc(state, crtc);
27082493 16107
601a9ee0
VS
16108 /*
16109 * If this is an already active pipe, it's DDB changed,
16110 * and this isn't the last pipe that needs updating
16111 * then we need to wait for a vblank to pass for the
16112 * new ddb allocation to take effect.
16113 */
16114 if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
16115 &old_crtc_state->wm.skl.ddb) &&
659f1415 16116 (update_pipes | modeset_pipes))
0f0f74bc 16117 intel_wait_for_vblank(dev_priv, pipe);
27082493 16118 }
601a9ee0 16119 }
aa9664ff 16120
1ff241ea
VS
16121 update_pipes = modeset_pipes;
16122
659f1415
JRS
16123 /*
16124 * Enable all pipes that needs a modeset and do not depends on other
16125 * pipes
16126 */
4f05d7ae 16127 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
659f1415
JRS
16128 enum pipe pipe = crtc->pipe;
16129
16130 if ((modeset_pipes & BIT(pipe)) == 0)
16131 continue;
16132
16133 if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
4e3cdb45
MN
16134 is_trans_port_sync_master(new_crtc_state) ||
16135 (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
659f1415
JRS
16136 continue;
16137
659f1415
JRS
16138 modeset_pipes &= ~BIT(pipe);
16139
d82a855a 16140 intel_enable_crtc(state, crtc);
659f1415
JRS
16141 }
16142
16143 /*
1ff241ea 16144 * Then we enable all remaining pipes that depend on other
4e3cdb45 16145 * pipes: MST slaves and port sync masters, big joiner master
659f1415 16146 */
b932da3c 16147 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
659f1415
JRS
16148 enum pipe pipe = crtc->pipe;
16149
16150 if ((modeset_pipes & BIT(pipe)) == 0)
16151 continue;
16152
1ff241ea
VS
16153 modeset_pipes &= ~BIT(pipe);
16154
16155 intel_enable_crtc(state, crtc);
16156 }
16157
16158 /*
16159 * Finally we do the plane updates/etc. for all pipes that got enabled.
16160 */
16161 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16162 enum pipe pipe = crtc->pipe;
16163
16164 if ((update_pipes & BIT(pipe)) == 0)
16165 continue;
16166
e57291c2 16167 drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7a0a6ee7 16168 entries, I915_MAX_PIPES, pipe));
659f1415 16169
ee34801c 16170 entries[pipe] = new_crtc_state->wm.skl.ddb;
1ff241ea 16171 update_pipes &= ~BIT(pipe);
659f1415 16172
b932da3c 16173 intel_update_crtc(state, crtc);
659f1415
JRS
16174 }
16175
e57291c2 16176 drm_WARN_ON(&dev_priv->drm, modeset_pipes);
1ff241ea 16177 drm_WARN_ON(&dev_priv->drm, update_pipes);
27082493
L
16178}
16179
ba318c61
CW
16180static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
16181{
16182 struct intel_atomic_state *state, *next;
16183 struct llist_node *freed;
16184
16185 freed = llist_del_all(&dev_priv->atomic_helper.free_list);
16186 llist_for_each_entry_safe(state, next, freed, freed)
16187 drm_atomic_state_put(&state->base);
16188}
16189
16190static void intel_atomic_helper_free_state_worker(struct work_struct *work)
16191{
16192 struct drm_i915_private *dev_priv =
16193 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
16194
16195 intel_atomic_helper_free_state(dev_priv);
16196}
16197
9db529aa
DV
16198static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
16199{
16200 struct wait_queue_entry wait_fence, wait_reset;
16201 struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
16202
16203 init_wait_entry(&wait_fence, 0);
16204 init_wait_entry(&wait_reset, 0);
16205 for (;;) {
16206 prepare_to_wait(&intel_state->commit_ready.wait,
16207 &wait_fence, TASK_UNINTERRUPTIBLE);
cb823ed9
CW
16208 prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
16209 I915_RESET_MODESET),
9db529aa
DV
16210 &wait_reset, TASK_UNINTERRUPTIBLE);
16211
16212
cb823ed9
CW
16213 if (i915_sw_fence_done(&intel_state->commit_ready) ||
16214 test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
9db529aa
DV
16215 break;
16216
16217 schedule();
16218 }
16219 finish_wait(&intel_state->commit_ready.wait, &wait_fence);
cb823ed9
CW
16220 finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
16221 I915_RESET_MODESET),
16222 &wait_reset);
9db529aa
DV
16223}
16224
afeda4f3
AM
16225static void intel_cleanup_dsbs(struct intel_atomic_state *state)
16226{
16227 struct intel_crtc_state *old_crtc_state, *new_crtc_state;
16228 struct intel_crtc *crtc;
16229 int i;
16230
16231 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16232 new_crtc_state, i)
16233 intel_dsb_cleanup(old_crtc_state);
16234}
16235
8d52e447
CW
16236static void intel_atomic_cleanup_work(struct work_struct *work)
16237{
afeda4f3
AM
16238 struct intel_atomic_state *state =
16239 container_of(work, struct intel_atomic_state, base.commit_work);
16240 struct drm_i915_private *i915 = to_i915(state->base.dev);
8d52e447 16241
afeda4f3
AM
16242 intel_cleanup_dsbs(state);
16243 drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
16244 drm_atomic_helper_commit_cleanup_done(&state->base);
16245 drm_atomic_state_put(&state->base);
8d52e447
CW
16246
16247 intel_atomic_helper_free_state(i915);
16248}
16249
855e0d68 16250static void intel_atomic_commit_tail(struct intel_atomic_state *state)
a6778b3c 16251{
855e0d68 16252 struct drm_device *dev = state->base.dev;
fac5e23e 16253 struct drm_i915_private *dev_priv = to_i915(dev);
855e0d68
ML
16254 struct intel_crtc_state *new_crtc_state, *old_crtc_state;
16255 struct intel_crtc *crtc;
d8fc70b7 16256 u64 put_domains[I915_MAX_PIPES] = {};
0e6e0be4 16257 intel_wakeref_t wakeref = 0;
e95433c7 16258 int i;
a6778b3c 16259
855e0d68 16260 intel_atomic_commit_fence_wait(state);
42b062b0 16261
855e0d68 16262 drm_atomic_helper_wait_for_dependencies(&state->base);
ea0000f0 16263
855e0d68 16264 if (state->modeset)
0e6e0be4 16265 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 16266
7bff9779
ML
16267 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16268 new_crtc_state, i) {
855e0d68
ML
16269 if (needs_modeset(new_crtc_state) ||
16270 new_crtc_state->update_pipe) {
a539205a 16271
855e0d68 16272 put_domains[crtc->pipe] =
afe0c21b 16273 modeset_get_crtc_power_domains(new_crtc_state);
5a21b665 16274 }
b8cecdf5 16275 }
7758a113 16276
66d9cec8
MN
16277 intel_commit_modeset_disables(state);
16278
855e0d68
ML
16279 /* FIXME: Eventually get rid of our crtc->config pointer */
16280 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16281 crtc->config = new_crtc_state;
f6e5b160 16282
855e0d68
ML
16283 if (state->modeset) {
16284 drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
33c8df89 16285
5604e9ce 16286 intel_set_cdclk_pre_plane_update(state);
f6d1973d 16287
3b4bf24d 16288 intel_modeset_verify_disabled(dev_priv, state);
4740b0f2 16289 }
47fab737 16290
ecab0f3d
SL
16291 intel_sagv_pre_plane_update(state);
16292
896e5bb0 16293 /* Complete the events for pipes that have now been disabled */
855e0d68
ML
16294 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16295 bool modeset = needs_modeset(new_crtc_state);
80715b2f 16296
1f7528c4 16297 /* Complete events for now disable pipes here. */
2225f3c6 16298 if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
1f7528c4 16299 spin_lock_irq(&dev->event_lock);
2225f3c6
ML
16300 drm_crtc_send_vblank_event(&crtc->base,
16301 new_crtc_state->uapi.event);
1f7528c4
DV
16302 spin_unlock_irq(&dev->event_lock);
16303
2225f3c6 16304 new_crtc_state->uapi.event = NULL;
1f7528c4 16305 }
177246a8
MR
16306 }
16307
24a7bfe0
ID
16308 if (state->modeset)
16309 intel_encoders_update_prepare(state);
16310
c7c0e7eb 16311 intel_dbuf_pre_plane_update(state);
b06cf595 16312
1288f9b0
K
16313 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
16314 if (new_crtc_state->uapi.async_flip)
16315 skl_enable_flip_done(crtc);
16316 }
16317
896e5bb0 16318 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0c841271 16319 dev_priv->display.commit_modeset_enables(state);
896e5bb0 16320
24a7bfe0
ID
16321 if (state->modeset) {
16322 intel_encoders_update_complete(state);
16323
5604e9ce 16324 intel_set_cdclk_post_plane_update(state);
24a7bfe0 16325 }
59f9e9ca 16326
94f05024
DV
16327 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
16328 * already, but still need the state for the delayed optimization. To
16329 * fix this:
16330 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
16331 * - schedule that vblank worker _before_ calling hw_done
16332 * - at the start of commit_tail, cancel it _synchrously
16333 * - switch over to the vblank wait helper in the core after that since
16334 * we don't need out special handling any more.
16335 */
855e0d68 16336 drm_atomic_helper_wait_for_flip_done(dev, &state->base);
5a21b665 16337
855e0d68 16338 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
1288f9b0
K
16339 if (new_crtc_state->uapi.async_flip)
16340 skl_disable_flip_done(crtc);
16341
1326a92c 16342 if (new_crtc_state->hw.active &&
855e0d68 16343 !needs_modeset(new_crtc_state) &&
0ccc42a2 16344 !new_crtc_state->preload_luts &&
2225f3c6 16345 (new_crtc_state->uapi.color_mgmt_changed ||
855e0d68
ML
16346 new_crtc_state->update_pipe))
16347 intel_color_load_luts(new_crtc_state);
051a6d8d
VS
16348 }
16349
5a21b665
DV
16350 /*
16351 * Now that the vblank has passed, we can go ahead and program the
16352 * optimal watermarks on platforms that need two-step watermark
16353 * programming.
16354 *
16355 * TODO: Move this (and other cleanup) to an async worker eventually.
16356 */
7181f5c5
VS
16357 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
16358 new_crtc_state, i) {
16359 /*
16360 * Gen2 reports pipe underruns whenever all planes are disabled.
16361 * So re-enable underrun reporting after some planes get enabled.
16362 *
16363 * We do this before .optimize_watermarks() so that we have a
16364 * chance of catching underruns with the intermediate watermarks
16365 * vs. the new plane configuration.
16366 */
16367 if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
16368 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
16369
5a21b665 16370 if (dev_priv->display.optimize_watermarks)
7a8fdb1f 16371 dev_priv->display.optimize_watermarks(state, crtc);
5a21b665
DV
16372 }
16373
c7c0e7eb 16374 intel_dbuf_post_plane_update(state);
b06cf595 16375
855e0d68 16376 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
bee43ca4 16377 intel_post_plane_update(state, crtc);
5a21b665
DV
16378
16379 if (put_domains[i])
16380 modeset_put_power_domains(dev_priv, put_domains[i]);
16381
aa5e9b47 16382 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
afeda4f3
AM
16383
16384 /*
16385 * DSB cleanup is done in cleanup_work aligning with framebuffer
16386 * cleanup. So copy and reset the dsb structure to sync with
16387 * commit_done and later do dsb cleanup in cleanup_work.
16388 */
16389 old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
5a21b665
DV
16390 }
16391
7181f5c5
VS
16392 /* Underruns don't always raise interrupts, so check manually */
16393 intel_check_cpu_fifo_underruns(dev_priv);
16394 intel_check_pch_fifo_underruns(dev_priv);
16395
ecab0f3d 16396 if (state->modeset)
855e0d68 16397 intel_verify_planes(state);
cff109f0 16398
ecab0f3d 16399 intel_sagv_post_plane_update(state);
656d1b89 16400
855e0d68 16401 drm_atomic_helper_commit_hw_done(&state->base);
94f05024 16402
855e0d68 16403 if (state->modeset) {
d5553c09
CW
16404 /* As one of the primary mmio accessors, KMS has a high
16405 * likelihood of triggering bugs in unclaimed access. After we
16406 * finish modesetting, see if an error has been flagged, and if
16407 * so enable debugging for the next modeset - and hope we catch
16408 * the culprit.
16409 */
2cf7bf6f 16410 intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
0e6e0be4 16411 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
d5553c09 16412 }
855e0d68 16413 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
5a21b665 16414
8d52e447
CW
16415 /*
16416 * Defer the cleanup of the old state to a separate worker to not
16417 * impede the current task (userspace for blocking modesets) that
16418 * are executed inline. For out-of-line asynchronous modesets/flips,
16419 * deferring to a new worker seems overkill, but we would place a
16420 * schedule point (cond_resched()) here anyway to keep latencies
16421 * down.
16422 */
855e0d68
ML
16423 INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
16424 queue_work(system_highpri_wq, &state->base.commit_work);
94f05024
DV
16425}
16426
16427static void intel_atomic_commit_work(struct work_struct *work)
16428{
855e0d68
ML
16429 struct intel_atomic_state *state =
16430 container_of(work, struct intel_atomic_state, base.commit_work);
c004a90b 16431
94f05024
DV
16432 intel_atomic_commit_tail(state);
16433}
16434
c004a90b
CW
16435static int __i915_sw_fence_call
16436intel_atomic_commit_ready(struct i915_sw_fence *fence,
16437 enum i915_sw_fence_notify notify)
16438{
16439 struct intel_atomic_state *state =
16440 container_of(fence, struct intel_atomic_state, commit_ready);
16441
16442 switch (notify) {
16443 case FENCE_COMPLETE:
42b062b0 16444 /* we do blocking waits in the worker, nothing to do here */
c004a90b 16445 break;
c004a90b 16446 case FENCE_FREE:
eb955eee
CW
16447 {
16448 struct intel_atomic_helper *helper =
16449 &to_i915(state->base.dev)->atomic_helper;
16450
16451 if (llist_add(&state->freed, &helper->free_list))
16452 schedule_work(&helper->free_work);
16453 break;
16454 }
c004a90b
CW
16455 }
16456
16457 return NOTIFY_DONE;
16458}
16459
e3b4089c 16460static void intel_atomic_track_fbs(struct intel_atomic_state *state)
6c9c1b38 16461{
e3b4089c
VS
16462 struct intel_plane_state *old_plane_state, *new_plane_state;
16463 struct intel_plane *plane;
6c9c1b38
DV
16464 int i;
16465
e3b4089c
VS
16466 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
16467 new_plane_state, i)
7b3cb17a
ML
16468 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16469 to_intel_frontbuffer(new_plane_state->hw.fb),
8e7cb179 16470 plane->frontbuffer_bit);
6c9c1b38
DV
16471}
16472
94f05024 16473static int intel_atomic_commit(struct drm_device *dev,
a85fb467 16474 struct drm_atomic_state *_state,
94f05024
DV
16475 bool nonblock)
16476{
a85fb467 16477 struct intel_atomic_state *state = to_intel_atomic_state(_state);
fac5e23e 16478 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
DV
16479 int ret = 0;
16480
a85fb467 16481 state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2e2f08d0 16482
a85fb467
VS
16483 drm_atomic_state_get(&state->base);
16484 i915_sw_fence_init(&state->commit_ready,
c004a90b 16485 intel_atomic_commit_ready);
94f05024 16486
440df938
VS
16487 /*
16488 * The intel_legacy_cursor_update() fast path takes care
16489 * of avoiding the vblank waits for simple cursor
16490 * movement and flips. For cursor on/off and size changes,
16491 * we want to perform the vblank waits so that watermark
16492 * updates happen during the correct frames. Gen9+ have
16493 * double buffered watermarks and so shouldn't need this.
16494 *
3cf50c63
ML
16495 * Unset state->legacy_cursor_update before the call to
16496 * drm_atomic_helper_setup_commit() because otherwise
16497 * drm_atomic_helper_wait_for_flip_done() is a noop and
16498 * we get FIFO underruns because we didn't wait
16499 * for vblank.
440df938
VS
16500 *
16501 * FIXME doing watermarks and fb cleanup from a vblank worker
16502 * (assuming we had any) would solve these problems.
16503 */
a85fb467 16504 if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
213f1bd0
ML
16505 struct intel_crtc_state *new_crtc_state;
16506 struct intel_crtc *crtc;
16507 int i;
16508
a85fb467 16509 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
213f1bd0
ML
16510 if (new_crtc_state->wm.need_postvbl_update ||
16511 new_crtc_state->update_wm_post)
a85fb467 16512 state->base.legacy_cursor_update = false;
213f1bd0 16513 }
440df938 16514
a85fb467 16515 ret = intel_atomic_prepare_commit(state);
3cf50c63 16516 if (ret) {
cd49f818
WK
16517 drm_dbg_atomic(&dev_priv->drm,
16518 "Preparing state failed with %i\n", ret);
a85fb467
VS
16519 i915_sw_fence_commit(&state->commit_ready);
16520 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
3cf50c63
ML
16521 return ret;
16522 }
16523
a85fb467 16524 ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
3cf50c63 16525 if (!ret)
a85fb467 16526 ret = drm_atomic_helper_swap_state(&state->base, true);
0ef1905e
VS
16527 if (!ret)
16528 intel_atomic_swap_global_state(state);
3cf50c63 16529
0806f4ee 16530 if (ret) {
afeda4f3
AM
16531 struct intel_crtc_state *new_crtc_state;
16532 struct intel_crtc *crtc;
16533 int i;
16534
a85fb467 16535 i915_sw_fence_commit(&state->commit_ready);
0806f4ee 16536
afeda4f3
AM
16537 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16538 intel_dsb_cleanup(new_crtc_state);
16539
a85fb467
VS
16540 drm_atomic_helper_cleanup_planes(dev, &state->base);
16541 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
0806f4ee
ML
16542 return ret;
16543 }
94f05024 16544 dev_priv->wm.distrust_bios_wm = false;
a85fb467
VS
16545 intel_shared_dpll_swap_state(state);
16546 intel_atomic_track_fbs(state);
94f05024 16547
a85fb467
VS
16548 drm_atomic_state_get(&state->base);
16549 INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
c004a90b 16550
a85fb467
VS
16551 i915_sw_fence_commit(&state->commit_ready);
16552 if (nonblock && state->modeset) {
16553 queue_work(dev_priv->modeset_wq, &state->base.commit_work);
757fffcf 16554 } else if (nonblock) {
c26a0586 16555 queue_work(dev_priv->flip_wq, &state->base.commit_work);
757fffcf 16556 } else {
a85fb467 16557 if (state->modeset)
757fffcf 16558 flush_workqueue(dev_priv->modeset_wq);
a85fb467 16559 intel_atomic_commit_tail(state);
757fffcf 16560 }
75714940 16561
74c090b1 16562 return 0;
7f27126e
JB
16563}
16564
74d290f8
CW
16565struct wait_rps_boost {
16566 struct wait_queue_entry wait;
16567
16568 struct drm_crtc *crtc;
e61e0f51 16569 struct i915_request *request;
74d290f8
CW
16570};
16571
16572static int do_rps_boost(struct wait_queue_entry *_wait,
16573 unsigned mode, int sync, void *key)
16574{
16575 struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
e61e0f51 16576 struct i915_request *rq = wait->request;
74d290f8 16577
e9af4ea2
CW
16578 /*
16579 * If we missed the vblank, but the request is already running it
16580 * is reasonable to assume that it will complete before the next
16581 * vblank without our intervention, so leave RPS alone.
16582 */
e61e0f51 16583 if (!i915_request_started(rq))
3e7abf81 16584 intel_rps_boost(rq);
e61e0f51 16585 i915_request_put(rq);
74d290f8
CW
16586
16587 drm_crtc_vblank_put(wait->crtc);
16588
16589 list_del(&wait->wait.entry);
16590 kfree(wait);
16591 return 1;
16592}
16593
16594static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
16595 struct dma_fence *fence)
16596{
16597 struct wait_rps_boost *wait;
16598
16599 if (!dma_fence_is_i915(fence))
16600 return;
16601
16602 if (INTEL_GEN(to_i915(crtc->dev)) < 6)
16603 return;
16604
16605 if (drm_crtc_vblank_get(crtc))
16606 return;
16607
16608 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
16609 if (!wait) {
16610 drm_crtc_vblank_put(crtc);
16611 return;
16612 }
16613
16614 wait->request = to_request(dma_fence_get(fence));
16615 wait->crtc = crtc;
16616
16617 wait->wait.func = do_rps_boost;
16618 wait->wait.flags = 0;
16619
16620 add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
16621}
16622
ef1a1914
VS
16623static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
16624{
f90a85e7 16625 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
ef1a1914 16626 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
7b3cb17a 16627 struct drm_framebuffer *fb = plane_state->hw.fb;
ef1a1914
VS
16628 struct i915_vma *vma;
16629
16630 if (plane->id == PLANE_CURSOR &&
d53db442 16631 INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
ef1a1914
VS
16632 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
16633 const int align = intel_cursor_alignment(dev_priv);
4a477651 16634 int err;
ef1a1914 16635
4a477651
CW
16636 err = i915_gem_object_attach_phys(obj, align);
16637 if (err)
16638 return err;
ef1a1914
VS
16639 }
16640
16641 vma = intel_pin_and_fence_fb_obj(fb,
f5929c53 16642 &plane_state->view,
ef1a1914
VS
16643 intel_plane_uses_fence(plane_state),
16644 &plane_state->flags);
16645 if (IS_ERR(vma))
16646 return PTR_ERR(vma);
16647
16648 plane_state->vma = vma;
16649
16650 return 0;
16651}
16652
16653static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
16654{
16655 struct i915_vma *vma;
16656
16657 vma = fetch_and_zero(&old_plane_state->vma);
16658 if (vma)
16659 intel_unpin_fb_vma(vma, old_plane_state->flags);
16660}
16661
b7268c5e
CW
16662static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
16663{
16664 struct i915_sched_attr attr = {
058179e7 16665 .priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
b7268c5e
CW
16666 };
16667
16668 i915_gem_object_wait_priority(obj, 0, &attr);
16669}
16670
6beb8c23
MR
16671/**
16672 * intel_prepare_plane_fb - Prepare fb for usage on plane
723196f4 16673 * @_plane: drm plane to prepare for
1fd37669 16674 * @_new_plane_state: the plane state being prepared
6beb8c23
MR
16675 *
16676 * Prepares a framebuffer for usage on a display plane. Generally this
16677 * involves pinning the underlying object and updating the frontbuffer tracking
16678 * bits. Some older platforms need special physical address handling for
16679 * cursor planes.
16680 *
16681 * Returns 0 on success, negative error code on failure.
16682 */
16683int
b2faf669 16684intel_prepare_plane_fb(struct drm_plane *_plane,
04c8b0bf 16685 struct drm_plane_state *_new_plane_state)
465c120c 16686{
b2faf669 16687 struct intel_plane *plane = to_intel_plane(_plane);
04c8b0bf
ML
16688 struct intel_plane_state *new_plane_state =
16689 to_intel_plane_state(_new_plane_state);
fa528334 16690 struct intel_atomic_state *state =
f90a85e7 16691 to_intel_atomic_state(new_plane_state->uapi.state);
b2faf669
VS
16692 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16693 const struct intel_plane_state *old_plane_state =
fa528334 16694 intel_atomic_get_old_plane_state(state, plane);
b2faf669
VS
16695 struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
16696 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
c004a90b 16697 int ret;
465c120c 16698
5008e874 16699 if (old_obj) {
b2faf669 16700 const struct intel_crtc_state *crtc_state =
fa528334 16701 intel_atomic_get_new_crtc_state(state,
b2faf669 16702 to_intel_crtc(old_plane_state->hw.crtc));
5008e874
ML
16703
16704 /* Big Hammer, we also need to ensure that any pending
16705 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
16706 * current scanout is retired before unpinning the old
16707 * framebuffer. Note that we rely on userspace rendering
16708 * into the buffer attached to the pipe they are waiting
16709 * on. If not, userspace generates a GPU hang with IPEHR
16710 * point to the MI_WAIT_FOR_EVENT.
16711 *
16712 * This should only fail upon a hung GPU, in which case we
16713 * can safely continue.
16714 */
c004a90b 16715 if (needs_modeset(crtc_state)) {
fa528334 16716 ret = i915_sw_fence_await_reservation(&state->commit_ready,
ef78f7b1 16717 old_obj->base.resv, NULL,
c004a90b
CW
16718 false, 0,
16719 GFP_KERNEL);
16720 if (ret < 0)
16721 return ret;
f4457ae7 16722 }
5008e874
ML
16723 }
16724
f90a85e7 16725 if (new_plane_state->uapi.fence) { /* explicit fencing */
fa528334 16726 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
f90a85e7 16727 new_plane_state->uapi.fence,
16dc224f 16728 i915_fence_timeout(dev_priv),
c004a90b
CW
16729 GFP_KERNEL);
16730 if (ret < 0)
16731 return ret;
16732 }
16733
c37efb99
CW
16734 if (!obj)
16735 return 0;
16736
4d3088c7 16737 ret = i915_gem_object_pin_pages(obj);
fd70075f
CW
16738 if (ret)
16739 return ret;
16740
04c8b0bf 16741 ret = intel_plane_pin_fb(new_plane_state);
fd70075f 16742
4d3088c7 16743 i915_gem_object_unpin_pages(obj);
fd70075f
CW
16744 if (ret)
16745 return ret;
16746
e2f3496e 16747 fb_obj_bump_render_priority(obj);
da42104f 16748 i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
07bcd99b 16749
f90a85e7 16750 if (!new_plane_state->uapi.fence) { /* implicit fencing */
74d290f8
CW
16751 struct dma_fence *fence;
16752
fa528334 16753 ret = i915_sw_fence_await_reservation(&state->commit_ready,
ef78f7b1 16754 obj->base.resv, NULL,
16dc224f
CW
16755 false,
16756 i915_fence_timeout(dev_priv),
c004a90b
CW
16757 GFP_KERNEL);
16758 if (ret < 0)
6fef8510 16759 goto unpin_fb;
74d290f8 16760
52791eee 16761 fence = dma_resv_get_excl_rcu(obj->base.resv);
74d290f8 16762 if (fence) {
7b3cb17a 16763 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
04c8b0bf 16764 fence);
74d290f8
CW
16765 dma_fence_put(fence);
16766 }
16767 } else {
7b3cb17a 16768 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
f90a85e7 16769 new_plane_state->uapi.fence);
c004a90b 16770 }
5a21b665 16771
60548c55
CW
16772 /*
16773 * We declare pageflips to be interactive and so merit a small bias
16774 * towards upclocking to deliver the frame on time. By only changing
16775 * the RPS thresholds to sample more regularly and aim for higher
16776 * clocks we can hopefully deliver low power workloads (like kodi)
16777 * that are not quite steady state without resorting to forcing
16778 * maximum clocks following a vblank miss (see do_rps_boost()).
16779 */
fa528334 16780 if (!state->rps_interactive) {
3e7abf81 16781 intel_rps_mark_interactive(&dev_priv->gt.rps, true);
fa528334 16782 state->rps_interactive = true;
60548c55
CW
16783 }
16784
d07f0e59 16785 return 0;
6fef8510
VS
16786
16787unpin_fb:
16788 intel_plane_unpin_fb(new_plane_state);
16789
16790 return ret;
6beb8c23
MR
16791}
16792
38f3ce3a
MR
16793/**
16794 * intel_cleanup_plane_fb - Cleans up an fb after plane use
16795 * @plane: drm plane to clean up for
1fd37669 16796 * @_old_plane_state: the state from the previous modeset
38f3ce3a
MR
16797 *
16798 * Cleans up a framebuffer that has just been removed from a plane.
16799 */
16800void
16801intel_cleanup_plane_fb(struct drm_plane *plane,
04c8b0bf 16802 struct drm_plane_state *_old_plane_state)
38f3ce3a 16803{
04c8b0bf
ML
16804 struct intel_plane_state *old_plane_state =
16805 to_intel_plane_state(_old_plane_state);
fa528334 16806 struct intel_atomic_state *state =
f90a85e7 16807 to_intel_atomic_state(old_plane_state->uapi.state);
ef1a1914 16808 struct drm_i915_private *dev_priv = to_i915(plane->dev);
58fa1760
VS
16809 struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16810
16811 if (!obj)
16812 return;
38f3ce3a 16813
fa528334 16814 if (state->rps_interactive) {
3e7abf81 16815 intel_rps_mark_interactive(&dev_priv->gt.rps, false);
fa528334 16816 state->rps_interactive = false;
60548c55
CW
16817 }
16818
be1e3415 16819 /* Should only be called after a successful intel_prepare_plane_fb()! */
04c8b0bf 16820 intel_plane_unpin_fb(old_plane_state);
465c120c
MR
16821}
16822
cf4c7c12 16823/**
4a3b8769
MR
16824 * intel_plane_destroy - destroy a plane
16825 * @plane: plane to destroy
cf4c7c12 16826 *
4a3b8769
MR
16827 * Common destruction function for all types of planes (primary, cursor,
16828 * sprite).
cf4c7c12 16829 */
4a3b8769 16830void intel_plane_destroy(struct drm_plane *plane)
465c120c 16831{
465c120c 16832 drm_plane_cleanup(plane);
69ae561f 16833 kfree(to_intel_plane(plane));
465c120c
MR
16834}
16835
a38189c5
VS
16836static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16837 u32 format, u64 modifier)
714244e2 16838{
a38189c5
VS
16839 switch (modifier) {
16840 case DRM_FORMAT_MOD_LINEAR:
16841 case I915_FORMAT_MOD_X_TILED:
16842 break;
16843 default:
16844 return false;
16845 }
16846
714244e2
BW
16847 switch (format) {
16848 case DRM_FORMAT_C8:
16849 case DRM_FORMAT_RGB565:
16850 case DRM_FORMAT_XRGB1555:
16851 case DRM_FORMAT_XRGB8888:
16852 return modifier == DRM_FORMAT_MOD_LINEAR ||
16853 modifier == I915_FORMAT_MOD_X_TILED;
16854 default:
16855 return false;
16856 }
16857}
16858
a38189c5
VS
16859static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16860 u32 format, u64 modifier)
714244e2 16861{
a38189c5
VS
16862 switch (modifier) {
16863 case DRM_FORMAT_MOD_LINEAR:
16864 case I915_FORMAT_MOD_X_TILED:
16865 break;
16866 default:
16867 return false;
16868 }
16869
714244e2
BW
16870 switch (format) {
16871 case DRM_FORMAT_C8:
16872 case DRM_FORMAT_RGB565:
16873 case DRM_FORMAT_XRGB8888:
16874 case DRM_FORMAT_XBGR8888:
73263cb6
VS
16875 case DRM_FORMAT_ARGB8888:
16876 case DRM_FORMAT_ABGR8888:
714244e2
BW
16877 case DRM_FORMAT_XRGB2101010:
16878 case DRM_FORMAT_XBGR2101010:
73263cb6
VS
16879 case DRM_FORMAT_ARGB2101010:
16880 case DRM_FORMAT_ABGR2101010:
03b0ce95 16881 case DRM_FORMAT_XBGR16161616F:
714244e2
BW
16882 return modifier == DRM_FORMAT_MOD_LINEAR ||
16883 modifier == I915_FORMAT_MOD_X_TILED;
16884 default:
16885 return false;
16886 }
16887}
16888
a38189c5
VS
16889static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16890 u32 format, u64 modifier)
714244e2 16891{
a38189c5
VS
16892 return modifier == DRM_FORMAT_MOD_LINEAR &&
16893 format == DRM_FORMAT_ARGB8888;
714244e2
BW
16894}
16895
679bfe84 16896static const struct drm_plane_funcs i965_plane_funcs = {
a38189c5
VS
16897 .update_plane = drm_atomic_helper_update_plane,
16898 .disable_plane = drm_atomic_helper_disable_plane,
16899 .destroy = intel_plane_destroy,
a38189c5
VS
16900 .atomic_duplicate_state = intel_plane_duplicate_state,
16901 .atomic_destroy_state = intel_plane_destroy_state,
16902 .format_mod_supported = i965_plane_format_mod_supported,
16903};
714244e2 16904
679bfe84 16905static const struct drm_plane_funcs i8xx_plane_funcs = {
70a101f8
MR
16906 .update_plane = drm_atomic_helper_update_plane,
16907 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 16908 .destroy = intel_plane_destroy,
ea2c67bb
MR
16909 .atomic_duplicate_state = intel_plane_duplicate_state,
16910 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 16911 .format_mod_supported = i8xx_plane_format_mod_supported,
465c120c
MR
16912};
16913
f79f2692 16914static int
4078c983
VS
16915intel_legacy_cursor_update(struct drm_plane *_plane,
16916 struct drm_crtc *_crtc,
f79f2692
ML
16917 struct drm_framebuffer *fb,
16918 int crtc_x, int crtc_y,
16919 unsigned int crtc_w, unsigned int crtc_h,
ba3f4d0a
JN
16920 u32 src_x, u32 src_y,
16921 u32 src_w, u32 src_h,
34a2ab5e 16922 struct drm_modeset_acquire_ctx *ctx)
f79f2692 16923{
4078c983
VS
16924 struct intel_plane *plane = to_intel_plane(_plane);
16925 struct intel_crtc *crtc = to_intel_crtc(_crtc);
16926 struct intel_plane_state *old_plane_state =
16927 to_intel_plane_state(plane->base.state);
16928 struct intel_plane_state *new_plane_state;
c249c5f6 16929 struct intel_crtc_state *crtc_state =
4078c983 16930 to_intel_crtc_state(crtc->base.state);
c249c5f6 16931 struct intel_crtc_state *new_crtc_state;
8e7cb179 16932 int ret;
f79f2692
ML
16933
16934 /*
16935 * When crtc is inactive or there is a modeset pending,
16936 * wait for it to complete in the slowpath
16937 */
1326a92c 16938 if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
c249c5f6 16939 crtc_state->update_pipe)
f79f2692
ML
16940 goto slow;
16941
669c9215
ML
16942 /*
16943 * Don't do an async update if there is an outstanding commit modifying
16944 * the plane. This prevents our async update's changes from getting
16945 * overridden by a previous synchronous update's state.
16946 */
f90a85e7
ML
16947 if (old_plane_state->uapi.commit &&
16948 !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
669c9215 16949 goto slow;
f79f2692
ML
16950
16951 /*
16952 * If any parameters change that may affect watermarks,
16953 * take the slowpath. Only changing fb or position should be
16954 * in the fastpath.
16955 */
a456f65f
ML
16956 if (old_plane_state->uapi.crtc != &crtc->base ||
16957 old_plane_state->uapi.src_w != src_w ||
16958 old_plane_state->uapi.src_h != src_h ||
16959 old_plane_state->uapi.crtc_w != crtc_w ||
16960 old_plane_state->uapi.crtc_h != crtc_h ||
16961 !old_plane_state->uapi.fb != !fb)
f79f2692
ML
16962 goto slow;
16963
4078c983 16964 new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
f79f2692
ML
16965 if (!new_plane_state)
16966 return -ENOMEM;
16967
4078c983 16968 new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
c249c5f6
ML
16969 if (!new_crtc_state) {
16970 ret = -ENOMEM;
16971 goto out_free;
16972 }
16973
a456f65f 16974 drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
f79f2692 16975
a456f65f
ML
16976 new_plane_state->uapi.src_x = src_x;
16977 new_plane_state->uapi.src_y = src_y;
16978 new_plane_state->uapi.src_w = src_w;
16979 new_plane_state->uapi.src_h = src_h;
16980 new_plane_state->uapi.crtc_x = crtc_x;
16981 new_plane_state->uapi.crtc_y = crtc_y;
16982 new_plane_state->uapi.crtc_w = crtc_w;
16983 new_plane_state->uapi.crtc_h = crtc_h;
f79f2692 16984
8246d9c7 16985 intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state, crtc);
7d8d2cbc 16986
c249c5f6 16987 ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
4078c983 16988 old_plane_state, new_plane_state);
f79f2692
ML
16989 if (ret)
16990 goto out_free;
16991
4078c983 16992 ret = intel_plane_pin_fb(new_plane_state);
ef1a1914 16993 if (ret)
2850748e 16994 goto out_free;
f79f2692 16995
a456f65f
ML
16996 intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16997 ORIGIN_FLIP);
16998 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16999 to_intel_frontbuffer(new_plane_state->hw.fb),
4078c983 17000 plane->frontbuffer_bit);
f79f2692
ML
17001
17002 /* Swap plane state */
a456f65f 17003 plane->base.state = &new_plane_state->uapi;
f79f2692 17004
c249c5f6
ML
17005 /*
17006 * We cannot swap crtc_state as it may be in use by an atomic commit or
17007 * page flip that's running simultaneously. If we swap crtc_state and
17008 * destroy the old state, we will cause a use-after-free there.
17009 *
17010 * Only update active_planes, which is needed for our internal
17011 * bookkeeping. Either value will do the right thing when updating
17012 * planes atomically. If the cursor was part of the atomic update then
17013 * we would have taken the slowpath.
17014 */
17015 crtc_state->active_planes = new_crtc_state->active_planes;
17016
a456f65f 17017 if (new_plane_state->uapi.visible)
4078c983 17018 intel_update_plane(plane, crtc_state, new_plane_state);
c48b86f9 17019 else
4078c983 17020 intel_disable_plane(plane, crtc_state);
f79f2692 17021
4078c983 17022 intel_plane_unpin_fb(old_plane_state);
f79f2692 17023
f79f2692 17024out_free:
c249c5f6 17025 if (new_crtc_state)
2225f3c6 17026 intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
669c9215 17027 if (ret)
a456f65f 17028 intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
669c9215 17029 else
a456f65f 17030 intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
f79f2692
ML
17031 return ret;
17032
f79f2692 17033slow:
4078c983 17034 return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
f79f2692 17035 crtc_x, crtc_y, crtc_w, crtc_h,
34a2ab5e 17036 src_x, src_y, src_w, src_h, ctx);
f79f2692
ML
17037}
17038
17039static const struct drm_plane_funcs intel_cursor_plane_funcs = {
17040 .update_plane = intel_legacy_cursor_update,
17041 .disable_plane = drm_atomic_helper_disable_plane,
17042 .destroy = intel_plane_destroy,
f79f2692
ML
17043 .atomic_duplicate_state = intel_plane_duplicate_state,
17044 .atomic_destroy_state = intel_plane_destroy_state,
a38189c5 17045 .format_mod_supported = intel_cursor_format_mod_supported,
f79f2692
ML
17046};
17047
cf1805e6
VS
17048static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
17049 enum i9xx_plane_id i9xx_plane)
17050{
17051 if (!HAS_FBC(dev_priv))
17052 return false;
17053
17054 if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
17055 return i9xx_plane == PLANE_A; /* tied to pipe A */
17056 else if (IS_IVYBRIDGE(dev_priv))
17057 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
17058 i9xx_plane == PLANE_C;
17059 else if (INTEL_GEN(dev_priv) >= 4)
17060 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
17061 else
17062 return i9xx_plane == PLANE_A;
17063}
17064
b079bd17 17065static struct intel_plane *
580503c7 17066intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 17067{
881440a8 17068 struct intel_plane *plane;
a38189c5 17069 const struct drm_plane_funcs *plane_funcs;
93ca7e00 17070 unsigned int supported_rotations;
881440a8
VS
17071 const u32 *formats;
17072 int num_formats;
ca9cab18 17073 int ret, zpos;
465c120c 17074
b7c80600
VS
17075 if (INTEL_GEN(dev_priv) >= 9)
17076 return skl_universal_plane_create(dev_priv, pipe,
17077 PLANE_PRIMARY);
17078
881440a8
VS
17079 plane = intel_plane_alloc();
17080 if (IS_ERR(plane))
17081 return plane;
ea2c67bb 17082
881440a8 17083 plane->pipe = pipe;
e3c566df
VS
17084 /*
17085 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
17086 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
17087 */
ddf08d32
VS
17088 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4 &&
17089 INTEL_NUM_PIPES(dev_priv) == 2)
881440a8 17090 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
e3c566df 17091 else
881440a8
VS
17092 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
17093 plane->id = PLANE_PRIMARY;
17094 plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
cf1805e6 17095
881440a8
VS
17096 plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
17097 if (plane->has_fbc) {
cf1805e6
VS
17098 struct intel_fbc *fbc = &dev_priv->fbc;
17099
881440a8 17100 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
cf1805e6
VS
17101 }
17102
73263cb6
VS
17103 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
17104 formats = vlv_primary_formats;
17105 num_formats = ARRAY_SIZE(vlv_primary_formats);
73263cb6 17106 } else if (INTEL_GEN(dev_priv) >= 4) {
03b0ce95
VS
17107 /*
17108 * WaFP16GammaEnabling:ivb
17109 * "Workaround : When using the 64-bit format, the plane
17110 * output on each color channel has one quarter amplitude.
17111 * It can be brought up to full amplitude by using pipe
17112 * gamma correction or pipe color space conversion to
17113 * multiply the plane output by four."
17114 *
17115 * There is no dedicated plane gamma for the primary plane,
17116 * and using the pipe gamma/csc could conflict with other
17117 * planes, so we choose not to expose fp16 on IVB primary
17118 * planes. HSW primary planes no longer have this problem.
17119 */
17120 if (IS_IVYBRIDGE(dev_priv)) {
17121 formats = ivb_primary_formats;
17122 num_formats = ARRAY_SIZE(ivb_primary_formats);
17123 } else {
17124 formats = i965_primary_formats;
17125 num_formats = ARRAY_SIZE(i965_primary_formats);
17126 }
6c0fd451 17127 } else {
881440a8 17128 formats = i8xx_primary_formats;
6c0fd451 17129 num_formats = ARRAY_SIZE(i8xx_primary_formats);
dbb1a6fb 17130 }
a8d201af 17131
dbb1a6fb
VS
17132 if (INTEL_GEN(dev_priv) >= 4)
17133 plane_funcs = &i965_plane_funcs;
17134 else
17135 plane_funcs = &i8xx_plane_funcs;
17136
17137 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17138 plane->min_cdclk = vlv_plane_min_cdclk;
17139 else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
17140 plane->min_cdclk = hsw_plane_min_cdclk;
17141 else if (IS_IVYBRIDGE(dev_priv))
17142 plane->min_cdclk = ivb_plane_min_cdclk;
17143 else
bb6ae9e6 17144 plane->min_cdclk = i9xx_plane_min_cdclk;
a38189c5 17145
dbb1a6fb
VS
17146 plane->max_stride = i9xx_plane_max_stride;
17147 plane->update_plane = i9xx_update_plane;
17148 plane->disable_plane = i9xx_disable_plane;
17149 plane->get_hw_state = i9xx_plane_get_hw_state;
17150 plane->check_plane = i9xx_plane_check;
465c120c 17151
b7c80600 17152 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
881440a8 17153 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
6875eb3f 17154 0, plane_funcs,
dbb1a6fb
VS
17155 formats, num_formats,
17156 i9xx_format_modifiers,
38573dc1
VS
17157 DRM_PLANE_TYPE_PRIMARY,
17158 "primary %c", pipe_name(pipe));
17159 else
881440a8 17160 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
6875eb3f 17161 0, plane_funcs,
dbb1a6fb
VS
17162 formats, num_formats,
17163 i9xx_format_modifiers,
38573dc1 17164 DRM_PLANE_TYPE_PRIMARY,
ed15030d 17165 "plane %c",
881440a8 17166 plane_name(plane->i9xx_plane));
fca0ce2a
VS
17167 if (ret)
17168 goto fail;
48404c1e 17169
b7c80600 17170 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
4ea7be2b 17171 supported_rotations =
c2c446ad
RF
17172 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
17173 DRM_MODE_REFLECT_X;
5481e27f 17174 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00 17175 supported_rotations =
c2c446ad 17176 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00 17177 } else {
c2c446ad 17178 supported_rotations = DRM_MODE_ROTATE_0;
93ca7e00
VS
17179 }
17180
5481e27f 17181 if (INTEL_GEN(dev_priv) >= 4)
881440a8 17182 drm_plane_create_rotation_property(&plane->base,
c2c446ad 17183 DRM_MODE_ROTATE_0,
93ca7e00 17184 supported_rotations);
48404c1e 17185
ca9cab18
VS
17186 zpos = 0;
17187 drm_plane_create_zpos_immutable_property(&plane->base, zpos);
17188
881440a8 17189 drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
ea2c67bb 17190
881440a8 17191 return plane;
fca0ce2a
VS
17192
17193fail:
881440a8 17194 intel_plane_free(plane);
fca0ce2a 17195
b079bd17 17196 return ERR_PTR(ret);
465c120c
MR
17197}
17198
b079bd17 17199static struct intel_plane *
b2d03b0d
VS
17200intel_cursor_plane_create(struct drm_i915_private *dev_priv,
17201 enum pipe pipe)
3d7d6510 17202{
c539b579 17203 struct intel_plane *cursor;
ca9cab18 17204 int ret, zpos;
3d7d6510 17205
c539b579
VS
17206 cursor = intel_plane_alloc();
17207 if (IS_ERR(cursor))
17208 return cursor;
ea2c67bb 17209
3d7d6510 17210 cursor->pipe = pipe;
ed15030d 17211 cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
b14e5848 17212 cursor->id = PLANE_CURSOR;
c19e1124 17213 cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
b2d03b0d
VS
17214
17215 if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
ddd5713d 17216 cursor->max_stride = i845_cursor_max_stride;
b2d03b0d
VS
17217 cursor->update_plane = i845_update_cursor;
17218 cursor->disable_plane = i845_disable_cursor;
51f5a096 17219 cursor->get_hw_state = i845_cursor_get_hw_state;
659056f2 17220 cursor->check_plane = i845_check_cursor;
b2d03b0d 17221 } else {
ddd5713d 17222 cursor->max_stride = i9xx_cursor_max_stride;
b2d03b0d
VS
17223 cursor->update_plane = i9xx_update_cursor;
17224 cursor->disable_plane = i9xx_disable_cursor;
51f5a096 17225 cursor->get_hw_state = i9xx_cursor_get_hw_state;
659056f2 17226 cursor->check_plane = i9xx_check_cursor;
b2d03b0d 17227 }
3d7d6510 17228
cd5dcbf1
VS
17229 cursor->cursor.base = ~0;
17230 cursor->cursor.cntl = ~0;
024faac7
VS
17231
17232 if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
17233 cursor->cursor.size = ~0;
3d7d6510 17234
580503c7 17235 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
6875eb3f 17236 0, &intel_cursor_plane_funcs,
fca0ce2a
VS
17237 intel_cursor_formats,
17238 ARRAY_SIZE(intel_cursor_formats),
714244e2
BW
17239 cursor_format_modifiers,
17240 DRM_PLANE_TYPE_CURSOR,
38573dc1 17241 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
17242 if (ret)
17243 goto fail;
4398ad45 17244
5481e27f 17245 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00 17246 drm_plane_create_rotation_property(&cursor->base,
c2c446ad
RF
17247 DRM_MODE_ROTATE_0,
17248 DRM_MODE_ROTATE_0 |
17249 DRM_MODE_ROTATE_180);
4398ad45 17250
ca9cab18
VS
17251 zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
17252 drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
17253
093a3a30
JRS
17254 if (INTEL_GEN(dev_priv) >= 12)
17255 drm_plane_enable_fb_damage_clips(&cursor->base);
17256
ea2c67bb
MR
17257 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
17258
b079bd17 17259 return cursor;
fca0ce2a
VS
17260
17261fail:
c539b579 17262 intel_plane_free(cursor);
fca0ce2a 17263
b079bd17 17264 return ERR_PTR(ret);
3d7d6510
MR
17265}
17266
08fa8fd0
VS
17267#define INTEL_CRTC_FUNCS \
17268 .gamma_set = drm_atomic_helper_legacy_gamma_set, \
17269 .set_config = drm_atomic_helper_set_config, \
17270 .destroy = intel_crtc_destroy, \
17271 .page_flip = drm_atomic_helper_page_flip, \
17272 .atomic_duplicate_state = intel_crtc_duplicate_state, \
17273 .atomic_destroy_state = intel_crtc_destroy_state, \
17274 .set_crc_source = intel_crtc_set_crc_source, \
17275 .verify_crc_source = intel_crtc_verify_crc_source, \
17276 .get_crc_sources = intel_crtc_get_crc_sources
17277
17278static const struct drm_crtc_funcs bdw_crtc_funcs = {
17279 INTEL_CRTC_FUNCS,
17280
17281 .get_vblank_counter = g4x_get_vblank_counter,
17282 .enable_vblank = bdw_enable_vblank,
17283 .disable_vblank = bdw_disable_vblank,
4bbffbf3 17284 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17285};
17286
17287static const struct drm_crtc_funcs ilk_crtc_funcs = {
17288 INTEL_CRTC_FUNCS,
17289
17290 .get_vblank_counter = g4x_get_vblank_counter,
17291 .enable_vblank = ilk_enable_vblank,
17292 .disable_vblank = ilk_disable_vblank,
4bbffbf3 17293 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17294};
17295
17296static const struct drm_crtc_funcs g4x_crtc_funcs = {
17297 INTEL_CRTC_FUNCS,
17298
17299 .get_vblank_counter = g4x_get_vblank_counter,
17300 .enable_vblank = i965_enable_vblank,
17301 .disable_vblank = i965_disable_vblank,
4bbffbf3 17302 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17303};
17304
17305static const struct drm_crtc_funcs i965_crtc_funcs = {
17306 INTEL_CRTC_FUNCS,
17307
17308 .get_vblank_counter = i915_get_vblank_counter,
17309 .enable_vblank = i965_enable_vblank,
17310 .disable_vblank = i965_disable_vblank,
4bbffbf3 17311 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17312};
17313
7d423af9 17314static const struct drm_crtc_funcs i915gm_crtc_funcs = {
08fa8fd0
VS
17315 INTEL_CRTC_FUNCS,
17316
17317 .get_vblank_counter = i915_get_vblank_counter,
7d423af9
VS
17318 .enable_vblank = i915gm_enable_vblank,
17319 .disable_vblank = i915gm_disable_vblank,
4bbffbf3 17320 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17321};
17322
17323static const struct drm_crtc_funcs i915_crtc_funcs = {
17324 INTEL_CRTC_FUNCS,
17325
17326 .get_vblank_counter = i915_get_vblank_counter,
17327 .enable_vblank = i8xx_enable_vblank,
17328 .disable_vblank = i8xx_disable_vblank,
4bbffbf3 17329 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17330};
17331
17332static const struct drm_crtc_funcs i8xx_crtc_funcs = {
17333 INTEL_CRTC_FUNCS,
17334
17335 /* no hw vblank counter */
17336 .enable_vblank = i8xx_enable_vblank,
17337 .disable_vblank = i8xx_disable_vblank,
4bbffbf3 17338 .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
08fa8fd0
VS
17339};
17340
66434539 17341static struct intel_crtc *intel_crtc_alloc(void)
79e53945 17342{
66434539 17343 struct intel_crtc_state *crtc_state;
f44bfa7f 17344 struct intel_crtc *crtc;
79e53945 17345
f44bfa7f
VS
17346 crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
17347 if (!crtc)
66434539 17348 return ERR_PTR(-ENOMEM);
79e53945 17349
216383e9 17350 crtc_state = intel_crtc_state_alloc(crtc);
b079bd17 17351 if (!crtc_state) {
66434539
VS
17352 kfree(crtc);
17353 return ERR_PTR(-ENOMEM);
b079bd17 17354 }
66434539 17355
979e94c1 17356 crtc->base.state = &crtc_state->uapi;
f44bfa7f 17357 crtc->config = crtc_state;
f5de6e07 17358
66434539
VS
17359 return crtc;
17360}
17361
17362static void intel_crtc_free(struct intel_crtc *crtc)
17363{
17364 intel_crtc_destroy_state(&crtc->base, crtc->base.state);
17365 kfree(crtc);
17366}
17367
6875eb3f
AG
17368static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
17369{
17370 struct intel_plane *plane;
17371
17372 for_each_intel_plane(&dev_priv->drm, plane) {
17373 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
17374 plane->pipe);
17375
17376 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
17377 }
17378}
17379
66434539
VS
17380static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
17381{
17382 struct intel_plane *primary, *cursor;
17383 const struct drm_crtc_funcs *funcs;
17384 struct intel_crtc *crtc;
17385 int sprite, ret;
17386
17387 crtc = intel_crtc_alloc();
17388 if (IS_ERR(crtc))
17389 return PTR_ERR(crtc);
17390
17391 crtc->pipe = pipe;
17392 crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
17393
580503c7 17394 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
17395 if (IS_ERR(primary)) {
17396 ret = PTR_ERR(primary);
3d7d6510 17397 goto fail;
b079bd17 17398 }
f44bfa7f 17399 crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 17400
a81d6fa0 17401 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
17402 struct intel_plane *plane;
17403
580503c7 17404 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 17405 if (IS_ERR(plane)) {
b079bd17
VS
17406 ret = PTR_ERR(plane);
17407 goto fail;
17408 }
f44bfa7f 17409 crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
17410 }
17411
580503c7 17412 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 17413 if (IS_ERR(cursor)) {
b079bd17 17414 ret = PTR_ERR(cursor);
3d7d6510 17415 goto fail;
b079bd17 17416 }
f44bfa7f 17417 crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 17418
08fa8fd0
VS
17419 if (HAS_GMCH(dev_priv)) {
17420 if (IS_CHERRYVIEW(dev_priv) ||
17421 IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
17422 funcs = &g4x_crtc_funcs;
17423 else if (IS_GEN(dev_priv, 4))
17424 funcs = &i965_crtc_funcs;
7d423af9
VS
17425 else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
17426 funcs = &i915gm_crtc_funcs;
08fa8fd0
VS
17427 else if (IS_GEN(dev_priv, 3))
17428 funcs = &i915_crtc_funcs;
17429 else
17430 funcs = &i8xx_crtc_funcs;
17431 } else {
17432 if (INTEL_GEN(dev_priv) >= 8)
17433 funcs = &bdw_crtc_funcs;
17434 else
17435 funcs = &ilk_crtc_funcs;
17436 }
17437
f44bfa7f 17438 ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
b079bd17 17439 &primary->base, &cursor->base,
08fa8fd0 17440 funcs, "pipe %c", pipe_name(pipe));
3d7d6510
MR
17441 if (ret)
17442 goto fail;
79e53945 17443
1947fd13
VS
17444 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
17445 dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
f44bfa7f 17446 dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
1947fd13
VS
17447
17448 if (INTEL_GEN(dev_priv) < 9) {
17449 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
17450
17451 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
17452 dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
f44bfa7f 17453 dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
1947fd13 17454 }
22fd0fab 17455
6d1a2fde
PB
17456 if (INTEL_GEN(dev_priv) >= 10)
17457 drm_crtc_create_scaling_filter_property(&crtc->base,
17458 BIT(DRM_SCALING_FILTER_DEFAULT) |
17459 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
17460
f44bfa7f 17461 intel_color_init(crtc);
8563b1e8 17462
00535527
JN
17463 intel_crtc_crc_init(crtc);
17464
e57291c2 17465 drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
b079bd17
VS
17466
17467 return 0;
3d7d6510
MR
17468
17469fail:
66434539 17470 intel_crtc_free(crtc);
b079bd17
VS
17471
17472 return ret;
79e53945
JB
17473}
17474
6a20fe7b
VS
17475int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
17476 struct drm_file *file)
08d7b3d1 17477{
08d7b3d1 17478 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 17479 struct drm_crtc *drmmode_crtc;
c05422d5 17480 struct intel_crtc *crtc;
08d7b3d1 17481
418da172 17482 drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
71240ed2 17483 if (!drmmode_crtc)
3f2c2057 17484 return -ENOENT;
08d7b3d1 17485
7707e653 17486 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 17487 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 17488
c05422d5 17489 return 0;
08d7b3d1
CW
17490}
17491
c08f995a 17492static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
79e53945 17493{
66a9278e
DV
17494 struct drm_device *dev = encoder->base.dev;
17495 struct intel_encoder *source_encoder;
c08f995a 17496 u32 possible_clones = 0;
79e53945 17497
b2784e15 17498 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 17499 if (encoders_cloneable(encoder, source_encoder))
c08f995a 17500 possible_clones |= drm_encoder_mask(&source_encoder->base);
79e53945 17501 }
4ef69c7a 17502
c08f995a 17503 return possible_clones;
79e53945
JB
17504}
17505
ed500bf6
VS
17506static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
17507{
17508 struct drm_device *dev = encoder->base.dev;
17509 struct intel_crtc *crtc;
17510 u32 possible_crtcs = 0;
17511
17512 for_each_intel_crtc(dev, crtc) {
981329ce 17513 if (encoder->pipe_mask & BIT(crtc->pipe))
ed500bf6
VS
17514 possible_crtcs |= drm_crtc_mask(&crtc->base);
17515 }
17516
17517 return possible_crtcs;
17518}
17519
a5916fd7 17520static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
4d302442 17521{
646d5772 17522 if (!IS_MOBILE(dev_priv))
4d302442
CW
17523 return false;
17524
dc008bf0 17525 if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
4d302442
CW
17526 return false;
17527
dc008bf0 17528 if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
17529 return false;
17530
17531 return true;
17532}
17533
63cb4e64 17534static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
84b4e042 17535{
6315b5d3 17536 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
17537 return false;
17538
50a0bc90 17539 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
17540 return false;
17541
4f8036a2 17542 if (HAS_PCH_LPT_H(dev_priv) &&
dc008bf0 17543 intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
17544 return false;
17545
70ac54d0 17546 /* DDI E can't be used if DDI A requires 4 lanes */
dc008bf0 17547 if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
17548 return false;
17549
e4abb733 17550 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
17551 return false;
17552
17553 return true;
17554}
17555
8090ba8c
ID
17556void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
17557{
17558 int pps_num;
17559 int pps_idx;
17560
17561 if (HAS_DDI(dev_priv))
17562 return;
17563 /*
17564 * This w/a is needed at least on CPT/PPT, but to be sure apply it
17565 * everywhere where registers can be write protected.
17566 */
17567 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17568 pps_num = 2;
17569 else
17570 pps_num = 1;
17571
17572 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
dc008bf0 17573 u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
8090ba8c
ID
17574
17575 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
dc008bf0 17576 intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
8090ba8c
ID
17577 }
17578}
17579
44cb734c
ID
17580static void intel_pps_init(struct drm_i915_private *dev_priv)
17581{
cc3f90f0 17582 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
17583 dev_priv->pps_mmio_base = PCH_PPS_BASE;
17584 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17585 dev_priv->pps_mmio_base = VLV_PPS_BASE;
17586 else
17587 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
17588
17589 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
17590}
17591
c39055b0 17592static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 17593{
4ef69c7a 17594 struct intel_encoder *encoder;
cb0953d7 17595 bool dpd_is_edp = false;
79e53945 17596
44cb734c
ID
17597 intel_pps_init(dev_priv);
17598
b81dddb9 17599 if (!HAS_DISPLAY(dev_priv))
fc0c5a9d
CW
17600 return;
17601
aefaa1f4
MR
17602 if (IS_ROCKETLAKE(dev_priv)) {
17603 intel_ddi_init(dev_priv, PORT_A);
17604 intel_ddi_init(dev_priv, PORT_B);
1d8ca002
VS
17605 intel_ddi_init(dev_priv, PORT_TC1);
17606 intel_ddi_init(dev_priv, PORT_TC2);
aefaa1f4 17607 } else if (INTEL_GEN(dev_priv) >= 12) {
55cd5048
MK
17608 intel_ddi_init(dev_priv, PORT_A);
17609 intel_ddi_init(dev_priv, PORT_B);
1d8ca002
VS
17610 intel_ddi_init(dev_priv, PORT_TC1);
17611 intel_ddi_init(dev_priv, PORT_TC2);
ba8a5cb2 17612 intel_ddi_init(dev_priv, PORT_TC3);
1d8ca002
VS
17613 intel_ddi_init(dev_priv, PORT_TC4);
17614 intel_ddi_init(dev_priv, PORT_TC5);
17615 intel_ddi_init(dev_priv, PORT_TC6);
33365fec 17616 icl_dsi_init(dev_priv);
24ea098b 17617 } else if (IS_JSL_EHL(dev_priv)) {
759c9ab5
BP
17618 intel_ddi_init(dev_priv, PORT_A);
17619 intel_ddi_init(dev_priv, PORT_B);
17620 intel_ddi_init(dev_priv, PORT_C);
719d2400 17621 intel_ddi_init(dev_priv, PORT_D);
759c9ab5 17622 icl_dsi_init(dev_priv);
55cd5048 17623 } else if (IS_GEN(dev_priv, 11)) {
00c92d92
PZ
17624 intel_ddi_init(dev_priv, PORT_A);
17625 intel_ddi_init(dev_priv, PORT_B);
17626 intel_ddi_init(dev_priv, PORT_C);
17627 intel_ddi_init(dev_priv, PORT_D);
17628 intel_ddi_init(dev_priv, PORT_E);
3f2e9ed0
ID
17629 /*
17630 * On some ICL SKUs port F is not present. No strap bits for
17631 * this, so rely on VBT.
2b34e562 17632 * Work around broken VBTs on SKUs known to have no port F.
3f2e9ed0 17633 */
2b34e562
ID
17634 if (IS_ICL_WITH_PORT_F(dev_priv) &&
17635 intel_bios_is_port_present(dev_priv, PORT_F))
3f2e9ed0
ID
17636 intel_ddi_init(dev_priv, PORT_F);
17637
bf4d57ff 17638 icl_dsi_init(dev_priv);
00c92d92 17639 } else if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
17640 /*
17641 * FIXME: Broxton doesn't support port detection via the
17642 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
17643 * detect the ports.
17644 */
c39055b0
ACO
17645 intel_ddi_init(dev_priv, PORT_A);
17646 intel_ddi_init(dev_priv, PORT_B);
17647 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 17648
e518634b 17649 vlv_dsi_init(dev_priv);
4f8036a2 17650 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
17651 int found;
17652
63cb4e64
JN
17653 if (intel_ddi_crt_present(dev_priv))
17654 intel_crt_init(dev_priv);
17655
de31facd
JB
17656 /*
17657 * Haswell uses DDI functions to detect digital outputs.
17658 * On SKL pre-D0 the strap isn't connected, so we assume
17659 * it's there.
17660 */
dc008bf0 17661 found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 17662 /* WaIgnoreDDIAStrap: skl */
b976dc53 17663 if (found || IS_GEN9_BC(dev_priv))
c39055b0 17664 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5 17665
9787e835 17666 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
0e72a5b5 17667 * register */
dc008bf0 17668 found = intel_de_read(dev_priv, SFUSE_STRAP);
0e72a5b5
ED
17669
17670 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 17671 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 17672 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 17673 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 17674 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 17675 intel_ddi_init(dev_priv, PORT_D);
9787e835
RV
17676 if (found & SFUSE_STRAP_DDIF_DETECTED)
17677 intel_ddi_init(dev_priv, PORT_F);
2800e4c2
RV
17678 /*
17679 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
17680 */
b976dc53 17681 if (IS_GEN9_BC(dev_priv) &&
e9d49bb7 17682 intel_bios_is_port_present(dev_priv, PORT_E))
c39055b0 17683 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 17684
6e266956 17685 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 17686 int found;
63cb4e64 17687
0fafa226
JN
17688 /*
17689 * intel_edp_init_connector() depends on this completing first,
17690 * to prevent the registration of both eDP and LVDS and the
17691 * incorrect sharing of the PPS.
17692 */
17693 intel_lvds_init(dev_priv);
74d021ea 17694 intel_crt_init(dev_priv);
63cb4e64 17695
7b91bf7f 17696 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
270b3042 17697
a5916fd7 17698 if (ilk_has_edp_a(dev_priv))
c39055b0 17699 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 17700
dc008bf0 17701 if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 17702 /* PCH SDVOB multiplex with HDMIB */
c39055b0 17703 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 17704 if (!found)
c39055b0 17705 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
dc008bf0 17706 if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
c39055b0 17707 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
17708 }
17709
dc008bf0 17710 if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
c39055b0 17711 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 17712
dc008bf0 17713 if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
c39055b0 17714 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 17715
dc008bf0 17716 if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
c39055b0 17717 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 17718
dc008bf0 17719 if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
c39055b0 17720 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 17721 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 17722 bool has_edp, has_port;
457c52d8 17723
63cb4e64
JN
17724 if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
17725 intel_crt_init(dev_priv);
17726
e17ac6db
VS
17727 /*
17728 * The DP_DETECTED bit is the latched state of the DDC
17729 * SDA pin at boot. However since eDP doesn't require DDC
17730 * (no way to plug in a DP->HDMI dongle) the DDC pins for
17731 * eDP ports may have been muxed to an alternate function.
17732 * Thus we can't rely on the DP_DETECTED bit alone to detect
17733 * eDP ports. Consult the VBT as well as DP_DETECTED to
17734 * detect eDP ports.
22f35042
VS
17735 *
17736 * Sadly the straps seem to be missing sometimes even for HDMI
17737 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
17738 * and VBT for the presence of the port. Additionally we can't
17739 * trust the port type the VBT declares as we've seen at least
17740 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 17741 */
7b91bf7f 17742 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
22f35042 17743 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
dc008bf0 17744 if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 17745 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
dc008bf0 17746 if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 17747 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 17748
7b91bf7f 17749 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
22f35042 17750 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
dc008bf0 17751 if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 17752 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
dc008bf0 17753 if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 17754 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 17755
920a14b2 17756 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
17757 /*
17758 * eDP not supported on port D,
17759 * so no need to worry about it
17760 */
17761 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
dc008bf0 17762 if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 17763 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
dc008bf0 17764 if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 17765 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
17766 }
17767
e518634b 17768 vlv_dsi_init(dev_priv);
63cb4e64 17769 } else if (IS_PINEVIEW(dev_priv)) {
0fafa226 17770 intel_lvds_init(dev_priv);
74d021ea 17771 intel_crt_init(dev_priv);
63cb4e64 17772 } else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
27185ae1 17773 bool found = false;
7d57382e 17774
9bedc7ed
JN
17775 if (IS_MOBILE(dev_priv))
17776 intel_lvds_init(dev_priv);
0fafa226 17777
74d021ea 17778 intel_crt_init(dev_priv);
63cb4e64 17779
dc008bf0 17780 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
cd49f818 17781 drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
c39055b0 17782 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 17783 if (!found && IS_G4X(dev_priv)) {
cd49f818
WK
17784 drm_dbg_kms(&dev_priv->drm,
17785 "probing HDMI on SDVOB\n");
c39055b0 17786 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 17787 }
27185ae1 17788
9beb5fea 17789 if (!found && IS_G4X(dev_priv))
c39055b0 17790 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 17791 }
13520b05
KH
17792
17793 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 17794
dc008bf0 17795 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
cd49f818 17796 drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
c39055b0 17797 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 17798 }
27185ae1 17799
dc008bf0 17800 if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 17801
9beb5fea 17802 if (IS_G4X(dev_priv)) {
cd49f818
WK
17803 drm_dbg_kms(&dev_priv->drm,
17804 "probing HDMI on SDVOC\n");
c39055b0 17805 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 17806 }
9beb5fea 17807 if (IS_G4X(dev_priv))
c39055b0 17808 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 17809 }
27185ae1 17810
dc008bf0 17811 if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
c39055b0 17812 intel_dp_init(dev_priv, DP_D, PORT_D);
d6521463
JN
17813
17814 if (SUPPORTS_TV(dev_priv))
17815 intel_tv_init(dev_priv);
63cb4e64 17816 } else if (IS_GEN(dev_priv, 2)) {
346073ce 17817 if (IS_I85X(dev_priv))
9bedc7ed 17818 intel_lvds_init(dev_priv);
0fafa226 17819
74d021ea 17820 intel_crt_init(dev_priv);
c39055b0 17821 intel_dvo_init(dev_priv);
63cb4e64 17822 }
79e53945 17823
c39055b0 17824 intel_psr_init(dev_priv);
7c8f8a70 17825
c39055b0 17826 for_each_intel_encoder(&dev_priv->drm, encoder) {
ed500bf6
VS
17827 encoder->base.possible_crtcs =
17828 intel_encoder_possible_crtcs(encoder);
4ef69c7a 17829 encoder->base.possible_clones =
c08f995a 17830 intel_encoder_possible_clones(encoder);
79e53945 17831 }
47356eb6 17832
c39055b0 17833 intel_init_pch_refclk(dev_priv);
270b3042 17834
c39055b0 17835 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
17836}
17837
17838static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17839{
17840 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 17841
ef2d633e 17842 drm_framebuffer_cleanup(fb);
8e7cb179 17843 intel_frontbuffer_put(intel_fb->frontbuffer);
70001cd2 17844
79e53945
JB
17845 kfree(intel_fb);
17846}
17847
17848static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 17849 struct drm_file *file,
79e53945
JB
17850 unsigned int *handle)
17851{
a5ff7a45 17852 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
cd49f818 17853 struct drm_i915_private *i915 = to_i915(obj->base.dev);
79e53945 17854
cc917ab4 17855 if (obj->userptr.mm) {
cd49f818
WK
17856 drm_dbg(&i915->drm,
17857 "attempting to use a userptr for a framebuffer, denied\n");
cc917ab4
CW
17858 return -EINVAL;
17859 }
17860
05394f39 17861 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
17862}
17863
86c98588
RV
17864static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17865 struct drm_file *file,
17866 unsigned flags, unsigned color,
17867 struct drm_clip_rect *clips,
17868 unsigned num_clips)
17869{
5a97bcc6 17870 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
86c98588 17871
5a97bcc6 17872 i915_gem_object_flush_if_display(obj);
8e7cb179 17873 intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
86c98588
RV
17874
17875 return 0;
17876}
17877
79e53945
JB
17878static const struct drm_framebuffer_funcs intel_fb_funcs = {
17879 .destroy = intel_user_framebuffer_destroy,
17880 .create_handle = intel_user_framebuffer_create_handle,
86c98588 17881 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
17882};
17883
24dbf51a
CW
17884static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17885 struct drm_i915_gem_object *obj,
17886 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 17887{
24dbf51a 17888 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2e2adb05 17889 struct drm_framebuffer *fb = &intel_fb->base;
a88c40eb 17890 u32 max_stride;
dd689287 17891 unsigned int tiling, stride;
24dbf51a 17892 int ret = -EINVAL;
2e2adb05 17893 int i;
79e53945 17894
8e7cb179
CW
17895 intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17896 if (!intel_fb->frontbuffer)
17897 return -ENOMEM;
17898
80f0b679 17899 i915_gem_object_lock(obj, NULL);
dd689287
CW
17900 tiling = i915_gem_object_get_tiling(obj);
17901 stride = i915_gem_object_get_stride(obj);
17902 i915_gem_object_unlock(obj);
dd4916c5 17903
2a80eada 17904 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
17905 /*
17906 * If there's a fence, enforce that
17907 * the fb modifier and tiling mode match.
17908 */
17909 if (tiling != I915_TILING_NONE &&
17910 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
cd49f818
WK
17911 drm_dbg_kms(&dev_priv->drm,
17912 "tiling_mode doesn't match fb modifier\n");
24dbf51a 17913 goto err;
2a80eada
DV
17914 }
17915 } else {
c2ff7370 17916 if (tiling == I915_TILING_X) {
2a80eada 17917 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 17918 } else if (tiling == I915_TILING_Y) {
cd49f818
WK
17919 drm_dbg_kms(&dev_priv->drm,
17920 "No Y tiling for legacy addfb\n");
24dbf51a 17921 goto err;
2a80eada
DV
17922 }
17923 }
17924
17e8fd11
VS
17925 if (!drm_any_plane_has_format(&dev_priv->drm,
17926 mode_cmd->pixel_format,
17927 mode_cmd->modifier[0])) {
17928 struct drm_format_name_buf format_name;
17929
cd49f818
WK
17930 drm_dbg_kms(&dev_priv->drm,
17931 "unsupported pixel format %s / modifier 0x%llx\n",
17932 drm_get_format_name(mode_cmd->pixel_format,
17933 &format_name),
17934 mode_cmd->modifier[0]);
24dbf51a 17935 goto err;
c16ed4be 17936 }
57cd6508 17937
c2ff7370
VS
17938 /*
17939 * gen2/3 display engine uses the fence if present,
17940 * so the tiling mode must match the fb modifier exactly.
17941 */
c56b89f1 17942 if (INTEL_GEN(dev_priv) < 4 &&
c2ff7370 17943 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
cd49f818
WK
17944 drm_dbg_kms(&dev_priv->drm,
17945 "tiling_mode must match fb modifier exactly on gen2/3\n");
9aceb5c1 17946 goto err;
c2ff7370
VS
17947 }
17948
a88c40eb
VS
17949 max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17950 mode_cmd->modifier[0]);
17951 if (mode_cmd->pitches[0] > max_stride) {
cd49f818
WK
17952 drm_dbg_kms(&dev_priv->drm,
17953 "%s pitch (%u) must be at most %d\n",
17954 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17955 "tiled" : "linear",
17956 mode_cmd->pitches[0], max_stride);
24dbf51a 17957 goto err;
c16ed4be 17958 }
5d7bd705 17959
c2ff7370
VS
17960 /*
17961 * If there's a fence, enforce that
17962 * the fb pitch and fence stride match.
17963 */
144cc143 17964 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
cd49f818
WK
17965 drm_dbg_kms(&dev_priv->drm,
17966 "pitch (%d) must match tiling stride (%d)\n",
17967 mode_cmd->pitches[0], stride);
24dbf51a 17968 goto err;
c16ed4be 17969 }
5d7bd705 17970
90f9a336 17971 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
5cf15dfc 17972 if (mode_cmd->offsets[0] != 0) {
cd49f818
WK
17973 drm_dbg_kms(&dev_priv->drm,
17974 "plane 0 offset (0x%08x) must be 0\n",
17975 mode_cmd->offsets[0]);
24dbf51a 17976 goto err;
5cf15dfc 17977 }
90f9a336 17978
2e2adb05 17979 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
d88c4afd 17980
2e2adb05
VS
17981 for (i = 0; i < fb->format->num_planes; i++) {
17982 u32 stride_alignment;
17983
17984 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
cd49f818
WK
17985 drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17986 i);
37875d6b 17987 goto err;
2e2adb05
VS
17988 }
17989
17990 stride_alignment = intel_fb_stride_alignment(fb, i);
2e2adb05 17991 if (fb->pitches[i] & (stride_alignment - 1)) {
cd49f818
WK
17992 drm_dbg_kms(&dev_priv->drm,
17993 "plane %d pitch (%d) must be at least %u byte aligned\n",
17994 i, fb->pitches[i], stride_alignment);
2e2adb05
VS
17995 goto err;
17996 }
d88c4afd 17997
71df86f0
ID
17998 if (is_gen12_ccs_plane(fb, i)) {
17999 int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
18000
18001 if (fb->pitches[i] != ccs_aux_stride) {
cd49f818
WK
18002 drm_dbg_kms(&dev_priv->drm,
18003 "ccs aux plane %d pitch (%d) must be %d\n",
18004 i,
18005 fb->pitches[i], ccs_aux_stride);
71df86f0
ID
18006 goto err;
18007 }
18008 }
18009
a268bcd7
DS
18010 fb->obj[i] = &obj->base;
18011 }
c7d73f6a 18012
2e2adb05 18013 ret = intel_fill_fb_info(dev_priv, fb);
6687c906 18014 if (ret)
9aceb5c1 18015 goto err;
2d7a215f 18016
2e2adb05 18017 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
79e53945 18018 if (ret) {
cd49f818 18019 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
24dbf51a 18020 goto err;
79e53945
JB
18021 }
18022
79e53945 18023 return 0;
24dbf51a
CW
18024
18025err:
8e7cb179 18026 intel_frontbuffer_put(intel_fb->frontbuffer);
24dbf51a 18027 return ret;
79e53945
JB
18028}
18029
79e53945
JB
18030static struct drm_framebuffer *
18031intel_user_framebuffer_create(struct drm_device *dev,
18032 struct drm_file *filp,
1eb83451 18033 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 18034{
dcb1394e 18035 struct drm_framebuffer *fb;
05394f39 18036 struct drm_i915_gem_object *obj;
76dc3769 18037 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 18038
03ac0642
CW
18039 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
18040 if (!obj)
cce13ff7 18041 return ERR_PTR(-ENOENT);
79e53945 18042
24dbf51a 18043 fb = intel_framebuffer_create(obj, &mode_cmd);
8e7cb179 18044 i915_gem_object_put(obj);
dcb1394e
LW
18045
18046 return fb;
79e53945
JB
18047}
18048
e995ca0b
VS
18049static enum drm_mode_status
18050intel_mode_valid(struct drm_device *dev,
18051 const struct drm_display_mode *mode)
18052{
ad77c537
VS
18053 struct drm_i915_private *dev_priv = to_i915(dev);
18054 int hdisplay_max, htotal_max;
18055 int vdisplay_max, vtotal_max;
18056
e4dd27aa
VS
18057 /*
18058 * Can't reject DBLSCAN here because Xorg ddxen can add piles
18059 * of DBLSCAN modes to the output's mode list when they detect
18060 * the scaling mode property on the connector. And they don't
18061 * ask the kernel to validate those modes in any way until
18062 * modeset time at which point the client gets a protocol error.
18063 * So in order to not upset those clients we silently ignore the
18064 * DBLSCAN flag on such connectors. For other connectors we will
18065 * reject modes with the DBLSCAN flag in encoder->compute_config().
18066 * And we always reject DBLSCAN modes in connector->mode_valid()
18067 * as we never want such modes on the connector's mode list.
18068 */
18069
e995ca0b
VS
18070 if (mode->vscan > 1)
18071 return MODE_NO_VSCAN;
18072
e995ca0b
VS
18073 if (mode->flags & DRM_MODE_FLAG_HSKEW)
18074 return MODE_H_ILLEGAL;
18075
18076 if (mode->flags & (DRM_MODE_FLAG_CSYNC |
18077 DRM_MODE_FLAG_NCSYNC |
18078 DRM_MODE_FLAG_PCSYNC))
18079 return MODE_HSYNC;
18080
18081 if (mode->flags & (DRM_MODE_FLAG_BCAST |
18082 DRM_MODE_FLAG_PIXMUX |
18083 DRM_MODE_FLAG_CLKDIV2))
18084 return MODE_BAD;
18085
2d20411e 18086 /* Transcoder timing limits */
12a97df0
MN
18087 if (INTEL_GEN(dev_priv) >= 11) {
18088 hdisplay_max = 16384;
18089 vdisplay_max = 8192;
18090 htotal_max = 16384;
18091 vtotal_max = 8192;
18092 } else if (INTEL_GEN(dev_priv) >= 9 ||
18093 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
ad77c537
VS
18094 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
18095 vdisplay_max = 4096;
18096 htotal_max = 8192;
18097 vtotal_max = 8192;
18098 } else if (INTEL_GEN(dev_priv) >= 3) {
18099 hdisplay_max = 4096;
18100 vdisplay_max = 4096;
18101 htotal_max = 8192;
18102 vtotal_max = 8192;
18103 } else {
18104 hdisplay_max = 2048;
18105 vdisplay_max = 2048;
18106 htotal_max = 4096;
18107 vtotal_max = 4096;
18108 }
18109
18110 if (mode->hdisplay > hdisplay_max ||
18111 mode->hsync_start > htotal_max ||
18112 mode->hsync_end > htotal_max ||
18113 mode->htotal > htotal_max)
18114 return MODE_H_ILLEGAL;
18115
18116 if (mode->vdisplay > vdisplay_max ||
18117 mode->vsync_start > vtotal_max ||
18118 mode->vsync_end > vtotal_max ||
18119 mode->vtotal > vtotal_max)
18120 return MODE_V_ILLEGAL;
18121
8f4b1068
VS
18122 if (INTEL_GEN(dev_priv) >= 5) {
18123 if (mode->hdisplay < 64 ||
18124 mode->htotal - mode->hdisplay < 32)
18125 return MODE_H_ILLEGAL;
18126
18127 if (mode->vtotal - mode->vdisplay < 5)
18128 return MODE_V_ILLEGAL;
18129 } else {
18130 if (mode->htotal - mode->hdisplay < 32)
18131 return MODE_H_ILLEGAL;
18132
18133 if (mode->vtotal - mode->vdisplay < 3)
18134 return MODE_V_ILLEGAL;
18135 }
18136
e995ca0b
VS
18137 return MODE_OK;
18138}
18139
2d20411e
VS
18140enum drm_mode_status
18141intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
63dc014e
ML
18142 const struct drm_display_mode *mode,
18143 bool bigjoiner)
2d20411e
VS
18144{
18145 int plane_width_max, plane_height_max;
18146
18147 /*
18148 * intel_mode_valid() should be
18149 * sufficient on older platforms.
18150 */
18151 if (INTEL_GEN(dev_priv) < 9)
18152 return MODE_OK;
18153
18154 /*
18155 * Most people will probably want a fullscreen
18156 * plane so let's not advertize modes that are
18157 * too big for that.
18158 */
18159 if (INTEL_GEN(dev_priv) >= 11) {
63dc014e 18160 plane_width_max = 5120 << bigjoiner;
2d20411e
VS
18161 plane_height_max = 4320;
18162 } else {
18163 plane_width_max = 5120;
18164 plane_height_max = 4096;
18165 }
18166
18167 if (mode->hdisplay > plane_width_max)
18168 return MODE_H_ILLEGAL;
18169
18170 if (mode->vdisplay > plane_height_max)
18171 return MODE_V_ILLEGAL;
18172
18173 return MODE_OK;
18174}
18175
79e53945 18176static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 18177 .fb_create = intel_user_framebuffer_create,
bbfb6ce8 18178 .get_format_info = intel_get_format_info,
0632fef6 18179 .output_poll_changed = intel_fbdev_output_poll_changed,
e995ca0b 18180 .mode_valid = intel_mode_valid,
5ee67f1c
MR
18181 .atomic_check = intel_atomic_check,
18182 .atomic_commit = intel_atomic_commit,
de419ab6
ML
18183 .atomic_state_alloc = intel_atomic_state_alloc,
18184 .atomic_state_clear = intel_atomic_state_clear,
778e23a9 18185 .atomic_state_free = intel_atomic_state_free,
79e53945
JB
18186};
18187
88212941
ID
18188/**
18189 * intel_init_display_hooks - initialize the display modesetting hooks
18190 * @dev_priv: device private
18191 */
18192void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 18193{
7ff89ca2
VS
18194 intel_init_cdclk_hooks(dev_priv);
18195
c56b89f1 18196 if (INTEL_GEN(dev_priv) >= 9) {
1e98f88c 18197 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
5724dbd1 18198 dev_priv->display.get_initial_plane_config =
f6df4d46 18199 skl_get_initial_plane_config;
1e98f88c
LDM
18200 dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
18201 dev_priv->display.crtc_enable = hsw_crtc_enable;
18202 dev_priv->display.crtc_disable = hsw_crtc_disable;
88212941 18203 } else if (HAS_DDI(dev_priv)) {
1e98f88c 18204 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
5724dbd1 18205 dev_priv->display.get_initial_plane_config =
81894b2f 18206 i9xx_get_initial_plane_config;
797d0259 18207 dev_priv->display.crtc_compute_clock =
1e98f88c
LDM
18208 hsw_crtc_compute_clock;
18209 dev_priv->display.crtc_enable = hsw_crtc_enable;
18210 dev_priv->display.crtc_disable = hsw_crtc_disable;
88212941 18211 } else if (HAS_PCH_SPLIT(dev_priv)) {
9eae5e27 18212 dev_priv->display.get_pipe_config = ilk_get_pipe_config;
5724dbd1 18213 dev_priv->display.get_initial_plane_config =
81894b2f 18214 i9xx_get_initial_plane_config;
3fb37703 18215 dev_priv->display.crtc_compute_clock =
9eae5e27
LDM
18216 ilk_crtc_compute_clock;
18217 dev_priv->display.crtc_enable = ilk_crtc_enable;
18218 dev_priv->display.crtc_disable = ilk_crtc_disable;
65b3d6a9 18219 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 18220 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
18221 dev_priv->display.get_initial_plane_config =
18222 i9xx_get_initial_plane_config;
65b3d6a9
ACO
18223 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
18224 dev_priv->display.crtc_enable = valleyview_crtc_enable;
18225 dev_priv->display.crtc_disable = i9xx_crtc_disable;
18226 } else if (IS_VALLEYVIEW(dev_priv)) {
18227 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18228 dev_priv->display.get_initial_plane_config =
18229 i9xx_get_initial_plane_config;
18230 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
18231 dev_priv->display.crtc_enable = valleyview_crtc_enable;
18232 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
18233 } else if (IS_G4X(dev_priv)) {
18234 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18235 dev_priv->display.get_initial_plane_config =
18236 i9xx_get_initial_plane_config;
18237 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
18238 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18239 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
18240 } else if (IS_PINEVIEW(dev_priv)) {
18241 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18242 dev_priv->display.get_initial_plane_config =
18243 i9xx_get_initial_plane_config;
18244 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
18245 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18246 dev_priv->display.crtc_disable = i9xx_crtc_disable;
cf819eff 18247 } else if (!IS_GEN(dev_priv, 2)) {
0e8ffe1b 18248 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
18249 dev_priv->display.get_initial_plane_config =
18250 i9xx_get_initial_plane_config;
d6dfee7a 18251 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
18252 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18253 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
18254 } else {
18255 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
18256 dev_priv->display.get_initial_plane_config =
18257 i9xx_get_initial_plane_config;
18258 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
18259 dev_priv->display.crtc_enable = i9xx_crtc_enable;
18260 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 18261 }
e70236a8 18262
cf819eff 18263 if (IS_GEN(dev_priv, 5)) {
9eae5e27 18264 dev_priv->display.fdi_link_train = ilk_fdi_link_train;
cf819eff 18265 } else if (IS_GEN(dev_priv, 6)) {
3bb11b53 18266 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 18267 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
18268 /* FIXME: detect B0+ stepping and use auto training */
18269 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
445e780b
VS
18270 }
18271
bd30ca2d 18272 if (INTEL_GEN(dev_priv) >= 9)
0c841271 18273 dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
27082493 18274 else
0c841271 18275 dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
66d9cec8 18276
e70236a8
JB
18277}
18278
6cd02e77 18279void intel_modeset_init_hw(struct drm_i915_private *i915)
f817586c 18280{
1965de63 18281 struct intel_cdclk_state *cdclk_state =
28a30b45 18282 to_intel_cdclk_state(i915->cdclk.obj.state);
3cf43cdc
VS
18283 struct intel_dbuf_state *dbuf_state =
18284 to_intel_dbuf_state(i915->dbuf.obj.state);
1965de63 18285
6cd02e77 18286 intel_update_cdclk(i915);
0bb94e03 18287 intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
1965de63 18288 cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
3cf43cdc
VS
18289
18290 dbuf_state->enabled_slices = i915->dbuf.enabled_slices;
f817586c
DV
18291}
18292
d1b2828a
VS
18293static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
18294{
18295 struct drm_plane *plane;
af157b76 18296 struct intel_crtc *crtc;
d1b2828a 18297
af157b76
VS
18298 for_each_intel_crtc(state->dev, crtc) {
18299 struct intel_crtc_state *crtc_state;
d1b2828a 18300
af157b76 18301 crtc_state = intel_atomic_get_crtc_state(state, crtc);
d1b2828a
VS
18302 if (IS_ERR(crtc_state))
18303 return PTR_ERR(crtc_state);
af157b76
VS
18304
18305 if (crtc_state->hw.active) {
18306 /*
18307 * Preserve the inherited flag to avoid
18308 * taking the full modeset path.
18309 */
a227569d 18310 crtc_state->inherited = true;
af157b76 18311 }
d1b2828a
VS
18312 }
18313
18314 drm_for_each_plane(plane, state->dev) {
18315 struct drm_plane_state *plane_state;
18316
18317 plane_state = drm_atomic_get_plane_state(state, plane);
18318 if (IS_ERR(plane_state))
18319 return PTR_ERR(plane_state);
18320 }
18321
18322 return 0;
18323}
18324
d93c0372
MR
18325/*
18326 * Calculate what we think the watermarks should be for the state we've read
18327 * out of the hardware and then immediately program those watermarks so that
18328 * we ensure the hardware settings match our internal state.
18329 *
18330 * We can calculate what we think WM's should be by creating a duplicate of the
18331 * current state (which was constructed during hardware readout) and running it
18332 * through the atomic check code to calculate new watermark values in the
18333 * state object.
18334 */
d1b2828a 18335static void sanitize_watermarks(struct drm_i915_private *dev_priv)
d93c0372 18336{
d93c0372 18337 struct drm_atomic_state *state;
ccf010fb 18338 struct intel_atomic_state *intel_state;
49743e1d
ML
18339 struct intel_crtc *crtc;
18340 struct intel_crtc_state *crtc_state;
d93c0372
MR
18341 struct drm_modeset_acquire_ctx ctx;
18342 int ret;
18343 int i;
18344
18345 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 18346 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
18347 return;
18348
d1b2828a 18349 state = drm_atomic_state_alloc(&dev_priv->drm);
e57291c2 18350 if (drm_WARN_ON(&dev_priv->drm, !state))
d1b2828a 18351 return;
d93c0372 18352
ccf010fb
ML
18353 intel_state = to_intel_atomic_state(state);
18354
d1b2828a
VS
18355 drm_modeset_acquire_init(&ctx, 0);
18356
18357retry:
18358 state->acquire_ctx = &ctx;
18359
ed4a6a7c
MR
18360 /*
18361 * Hardware readout is the only time we don't want to calculate
18362 * intermediate watermarks (since we don't trust the current
18363 * watermarks).
18364 */
b2ae318a 18365 if (!HAS_GMCH(dev_priv))
602ae835 18366 intel_state->skip_intermediate_wm = true;
ed4a6a7c 18367
d1b2828a
VS
18368 ret = sanitize_watermarks_add_affected(state);
18369 if (ret)
18370 goto fail;
18371
18372 ret = intel_atomic_check(&dev_priv->drm, state);
18373 if (ret)
18374 goto fail;
d93c0372
MR
18375
18376 /* Write calculated watermark values back */
49743e1d
ML
18377 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
18378 crtc_state->wm.need_postvbl_update = true;
7a8fdb1f 18379 dev_priv->display.optimize_watermarks(intel_state, crtc);
556fe36d 18380
49743e1d 18381 to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
d93c0372
MR
18382 }
18383
0cd1262d 18384fail:
d1b2828a
VS
18385 if (ret == -EDEADLK) {
18386 drm_atomic_state_clear(state);
18387 drm_modeset_backoff(&ctx);
18388 goto retry;
18389 }
18390
18391 /*
18392 * If we fail here, it means that the hardware appears to be
18393 * programmed in a way that shouldn't be possible, given our
18394 * understanding of watermark requirements. This might mean a
18395 * mistake in the hardware readout code or a mistake in the
18396 * watermark calculations for a given platform. Raise a WARN
18397 * so that this is noticeable.
18398 *
18399 * If this actually happens, we'll have to just leave the
18400 * BIOS-programmed watermarks untouched and hope for the best.
18401 */
e57291c2
PB
18402 drm_WARN(&dev_priv->drm, ret,
18403 "Could not determine valid watermarks for inherited state\n");
d1b2828a
VS
18404
18405 drm_atomic_state_put(state);
18406
d93c0372
MR
18407 drm_modeset_drop_locks(&ctx);
18408 drm_modeset_acquire_fini(&ctx);
18409}
18410
58ecd9d5
CW
18411static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
18412{
cf819eff 18413 if (IS_GEN(dev_priv, 5)) {
58ecd9d5 18414 u32 fdi_pll_clk =
dc008bf0 18415 intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
58ecd9d5
CW
18416
18417 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
cf819eff 18418 } else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
58ecd9d5
CW
18419 dev_priv->fdi_pll_freq = 270000;
18420 } else {
18421 return;
18422 }
18423
cd49f818 18424 drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
58ecd9d5
CW
18425}
18426
516a49cc
AS
18427static int intel_initial_commit(struct drm_device *dev)
18428{
18429 struct drm_atomic_state *state = NULL;
18430 struct drm_modeset_acquire_ctx ctx;
3558cafc 18431 struct intel_crtc *crtc;
516a49cc
AS
18432 int ret = 0;
18433
18434 state = drm_atomic_state_alloc(dev);
18435 if (!state)
18436 return -ENOMEM;
18437
18438 drm_modeset_acquire_init(&ctx, 0);
18439
18440retry:
18441 state->acquire_ctx = &ctx;
18442
3558cafc
ML
18443 for_each_intel_crtc(dev, crtc) {
18444 struct intel_crtc_state *crtc_state =
18445 intel_atomic_get_crtc_state(state, crtc);
18446
516a49cc
AS
18447 if (IS_ERR(crtc_state)) {
18448 ret = PTR_ERR(crtc_state);
18449 goto out;
18450 }
18451
1326a92c 18452 if (crtc_state->hw.active) {
b671d6ef
ID
18453 struct intel_encoder *encoder;
18454
af157b76
VS
18455 /*
18456 * We've not yet detected sink capabilities
18457 * (audio,infoframes,etc.) and thus we don't want to
18458 * force a full state recomputation yet. We want that to
18459 * happen only for the first real commit from userspace.
18460 * So preserve the inherited flag for the time being.
18461 */
a227569d 18462 crtc_state->inherited = true;
af157b76 18463
3558cafc 18464 ret = drm_atomic_add_affected_planes(state, &crtc->base);
516a49cc
AS
18465 if (ret)
18466 goto out;
fa6af514
VS
18467
18468 /*
18469 * FIXME hack to force a LUT update to avoid the
18470 * plane update forcing the pipe gamma on without
18471 * having a proper LUT loaded. Remove once we
18472 * have readout for pipe gamma enable.
18473 */
2225f3c6 18474 crtc_state->uapi.color_mgmt_changed = true;
a4277aa3 18475
b671d6ef
ID
18476 for_each_intel_encoder_mask(dev, encoder,
18477 crtc_state->uapi.encoder_mask) {
18478 if (encoder->initial_fastset_check &&
18479 !encoder->initial_fastset_check(encoder, crtc_state)) {
18480 ret = drm_atomic_add_affected_connectors(state,
18481 &crtc->base);
18482 if (ret)
18483 goto out;
18484 }
a4277aa3 18485 }
516a49cc
AS
18486 }
18487 }
18488
18489 ret = drm_atomic_commit(state);
18490
18491out:
18492 if (ret == -EDEADLK) {
18493 drm_atomic_state_clear(state);
18494 drm_modeset_backoff(&ctx);
18495 goto retry;
18496 }
18497
18498 drm_atomic_state_put(state);
18499
18500 drm_modeset_drop_locks(&ctx);
18501 drm_modeset_acquire_fini(&ctx);
18502
18503 return ret;
18504}
18505
e1a3d989
JN
18506static void intel_mode_config_init(struct drm_i915_private *i915)
18507{
18508 struct drm_mode_config *mode_config = &i915->drm.mode_config;
18509
18510 drm_mode_config_init(&i915->drm);
0ef1905e 18511 INIT_LIST_HEAD(&i915->global_obj_list);
e1a3d989
JN
18512
18513 mode_config->min_width = 0;
18514 mode_config->min_height = 0;
18515
18516 mode_config->preferred_depth = 24;
18517 mode_config->prefer_shadow = 1;
18518
18519 mode_config->allow_fb_modifiers = true;
18520
18521 mode_config->funcs = &intel_mode_funcs;
18522
55ea1cb1
K
18523 if (INTEL_GEN(i915) >= 9)
18524 mode_config->async_page_flip = true;
18525
e1a3d989
JN
18526 /*
18527 * Maximum framebuffer dimensions, chosen to match
18528 * the maximum render engine surface size on gen4+.
18529 */
18530 if (INTEL_GEN(i915) >= 7) {
18531 mode_config->max_width = 16384;
18532 mode_config->max_height = 16384;
18533 } else if (INTEL_GEN(i915) >= 4) {
18534 mode_config->max_width = 8192;
18535 mode_config->max_height = 8192;
18536 } else if (IS_GEN(i915, 3)) {
18537 mode_config->max_width = 4096;
18538 mode_config->max_height = 4096;
18539 } else {
18540 mode_config->max_width = 2048;
18541 mode_config->max_height = 2048;
18542 }
18543
18544 if (IS_I845G(i915) || IS_I865G(i915)) {
18545 mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
18546 mode_config->cursor_height = 1023;
5e9e0a3a
VS
18547 } else if (IS_I830(i915) || IS_I85X(i915) ||
18548 IS_I915G(i915) || IS_I915GM(i915)) {
e1a3d989
JN
18549 mode_config->cursor_width = 64;
18550 mode_config->cursor_height = 64;
18551 } else {
18552 mode_config->cursor_width = 256;
18553 mode_config->cursor_height = 256;
18554 }
18555}
18556
0ef1905e
VS
18557static void intel_mode_config_cleanup(struct drm_i915_private *i915)
18558{
18559 intel_atomic_global_obj_cleanup(i915);
18560 drm_mode_config_cleanup(&i915->drm);
18561}
18562
1586f620
CW
18563static void plane_config_fini(struct intel_initial_plane_config *plane_config)
18564{
18565 if (plane_config->fb) {
18566 struct drm_framebuffer *fb = &plane_config->fb->base;
18567
18568 /* We may only have the stub and not a full framebuffer */
18569 if (drm_framebuffer_read_refcount(fb))
18570 drm_framebuffer_put(fb);
18571 else
18572 kfree(fb);
18573 }
9c4ce97d
CW
18574
18575 if (plane_config->vma)
18576 i915_vma_put(plane_config->vma);
1586f620
CW
18577}
18578
80f286a5
JN
18579/* part #1: call before irq install */
18580int intel_modeset_init_noirq(struct drm_i915_private *i915)
79e53945 18581{
516a49cc 18582 int ret;
79e53945 18583
24d98a54
JN
18584 if (i915_inject_probe_failure(i915))
18585 return -ENODEV;
18586
da27bd41 18587 if (HAS_DISPLAY(i915)) {
24d98a54
JN
18588 ret = drm_vblank_init(&i915->drm,
18589 INTEL_NUM_PIPES(i915));
18590 if (ret)
18591 return ret;
18592 }
18593
18594 intel_bios_init(i915);
18595
18596 ret = intel_vga_register(i915);
18597 if (ret)
18598 goto cleanup_bios;
18599
18600 /* FIXME: completely on the wrong abstraction layer */
18601 intel_power_domains_init_hw(i915, false);
18602
18603 intel_csr_ucode_init(i915);
18604
6cd02e77
JN
18605 i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
18606 i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
18607 WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
757fffcf 18608
6cd02e77 18609 intel_mode_config_init(i915);
79e53945 18610
28a30b45
VS
18611 ret = intel_cdclk_init(i915);
18612 if (ret)
24d98a54 18613 goto cleanup_vga_client_pw_domain_csr;
28a30b45 18614
3cf43cdc
VS
18615 ret = intel_dbuf_init(i915);
18616 if (ret)
24d98a54 18617 goto cleanup_vga_client_pw_domain_csr;
3cf43cdc 18618
6cd02e77 18619 ret = intel_bw_init(i915);
c457d9cf 18620 if (ret)
24d98a54 18621 goto cleanup_vga_client_pw_domain_csr;
c457d9cf 18622
6cd02e77
JN
18623 init_llist_head(&i915->atomic_helper.free_list);
18624 INIT_WORK(&i915->atomic_helper.free_work,
ba318c61 18625 intel_atomic_helper_free_state_worker);
eb955eee 18626
6cd02e77 18627 intel_init_quirks(i915);
b690e96c 18628
6cd02e77 18629 intel_fbc_init(i915);
acde44b5 18630
80f286a5 18631 return 0;
24d98a54
JN
18632
18633cleanup_vga_client_pw_domain_csr:
18634 intel_csr_ucode_fini(i915);
18635 intel_power_domains_driver_remove(i915);
18636 intel_vga_unregister(i915);
18637cleanup_bios:
18638 intel_bios_driver_remove(i915);
18639
18640 return ret;
80f286a5
JN
18641}
18642
a5f2488f
JN
18643/* part #2: call after irq install, but before gem init */
18644int intel_modeset_init_nogem(struct drm_i915_private *i915)
80f286a5
JN
18645{
18646 struct drm_device *dev = &i915->drm;
18647 enum pipe pipe;
18648 struct intel_crtc *crtc;
18649 int ret;
18650
6cd02e77 18651 intel_init_pm(i915);
1fa61106 18652
6cd02e77 18653 intel_panel_sanitize_ssc(i915);
69f92f67 18654
9bfcf194
JN
18655 intel_gmbus_setup(i915);
18656
cd49f818
WK
18657 drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
18658 INTEL_NUM_PIPES(i915),
18659 INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
79e53945 18660
da27bd41 18661 if (HAS_DISPLAY(i915)) {
6cd02e77
JN
18662 for_each_pipe(i915, pipe) {
18663 ret = intel_crtc_init(i915, pipe);
ef404bc6 18664 if (ret) {
0ef1905e 18665 intel_mode_config_cleanup(i915);
ef404bc6
JN
18666 return ret;
18667 }
b079bd17 18668 }
79e53945
JB
18669 }
18670
6875eb3f 18671 intel_plane_possible_crtcs_init(i915);
e72f9fbf 18672 intel_shared_dpll_init(dev);
6cd02e77 18673 intel_update_fdi_pll_freq(i915);
ee7b9f93 18674
6cd02e77
JN
18675 intel_update_czclk(i915);
18676 intel_modeset_init_hw(i915);
5be6e334 18677
6cd02e77 18678 intel_hdcp_component_init(i915);
9055aac7 18679
6cd02e77
JN
18680 if (i915->max_cdclk_freq == 0)
18681 intel_update_max_cdclk(i915);
b2045352 18682
ddff9a60
MR
18683 /*
18684 * If the platform has HTI, we need to find out whether it has reserved
18685 * any display resources before we create our display outputs.
18686 */
18687 if (INTEL_INFO(i915)->display.has_hti)
18688 i915->hti_state = intel_de_read(i915, HDPORT_STATE);
18689
9cce37f4 18690 /* Just disable it once at startup */
4fb87831 18691 intel_vga_disable(i915);
6cd02e77 18692 intel_setup_outputs(i915);
11be49eb 18693
6e9f798d 18694 drm_modeset_lock_all(dev);
aecd36b8 18695 intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
6e9f798d 18696 drm_modeset_unlock_all(dev);
46f297fb 18697
d3fcc808 18698 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
18699 struct intel_initial_plane_config plane_config = {};
18700
0385ecea 18701 if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
46f297fb
JB
18702 continue;
18703
46f297fb 18704 /*
46f297fb
JB
18705 * Note that reserving the BIOS fb up front prevents us
18706 * from stuffing other stolen allocations like the ring
18707 * on top. This prevents some ugliness at boot time, and
18708 * can even allow for smooth boot transitions if the BIOS
18709 * fb is large enough for the active pipe configuration.
18710 */
6cd02e77 18711 i915->display.get_initial_plane_config(crtc, &plane_config);
eeebeac5
ML
18712
18713 /*
18714 * If the fb is shared between multiple heads, we'll
18715 * just get the first one.
18716 */
18717 intel_find_initial_plane_obj(crtc, &plane_config);
1586f620
CW
18718
18719 plane_config_fini(&plane_config);
46f297fb 18720 }
d93c0372
MR
18721
18722 /*
18723 * Make sure hardware watermarks really match the state we read out.
18724 * Note that we need to do this after reconstructing the BIOS fb's
18725 * since the watermark calculation done here will use pstate->fb.
18726 */
6cd02e77 18727 if (!HAS_GMCH(i915))
d1b2828a 18728 sanitize_watermarks(i915);
b079bd17 18729
516a49cc
AS
18730 /*
18731 * Force all active planes to recompute their states. So that on
18732 * mode_setcrtc after probe, all the intel_plane_state variables
18733 * are already calculated and there is no assert_plane warnings
18734 * during bootup.
18735 */
18736 ret = intel_initial_commit(dev);
18737 if (ret)
cd49f818 18738 drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
516a49cc 18739
b079bd17 18740 return 0;
2c7111db
CW
18741}
18742
a5f2488f
JN
18743/* part #3: call after gem init */
18744int intel_modeset_init(struct drm_i915_private *i915)
18745{
18746 int ret;
18747
da27bd41 18748 if (!HAS_DISPLAY(i915))
a5f2488f
JN
18749 return 0;
18750
71c8415d
JN
18751 intel_overlay_setup(i915);
18752
a5f2488f
JN
18753 ret = intel_fbdev_init(&i915->drm);
18754 if (ret)
18755 return ret;
18756
18757 /* Only enable hotplug handling once the fbdev is fully set up. */
18758 intel_hpd_init(i915);
4c8d4651 18759 intel_hpd_poll_disable(i915);
a5f2488f
JN
18760
18761 intel_init_ipc(i915);
18762
a5f2488f
JN
18763 return 0;
18764}
18765
2ee0da16
VS
18766void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18767{
d5fb43cb 18768 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
2ee0da16
VS
18769 /* 640x480@60Hz, ~25175 kHz */
18770 struct dpll clock = {
18771 .m1 = 18,
18772 .m2 = 7,
18773 .p1 = 13,
18774 .p2 = 4,
18775 .n = 2,
18776 };
18777 u32 dpll, fp;
18778 int i;
18779
e57291c2
PB
18780 drm_WARN_ON(&dev_priv->drm,
18781 i9xx_calc_dpll_params(48000, &clock) != 25154);
2ee0da16 18782
cd49f818
WK
18783 drm_dbg_kms(&dev_priv->drm,
18784 "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
18785 pipe_name(pipe), clock.vco, clock.dot);
2ee0da16
VS
18786
18787 fp = i9xx_dpll_compute_fp(&clock);
171d1562 18788 dpll = DPLL_DVO_2X_MODE |
2ee0da16
VS
18789 DPLL_VGA_MODE_DIS |
18790 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
18791 PLL_P2_DIVIDE_BY_4 |
18792 PLL_REF_INPUT_DREFCLK |
18793 DPLL_VCO_ENABLE;
18794
dc008bf0
JN
18795 intel_de_write(dev_priv, FP0(pipe), fp);
18796 intel_de_write(dev_priv, FP1(pipe), fp);
2ee0da16 18797
dc008bf0
JN
18798 intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
18799 intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
18800 intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
18801 intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
18802 intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
18803 intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
18804 intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
2ee0da16
VS
18805
18806 /*
18807 * Apparently we need to have VGA mode enabled prior to changing
18808 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
18809 * dividers, even though the register value does change.
18810 */
dc008bf0
JN
18811 intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
18812 intel_de_write(dev_priv, DPLL(pipe), dpll);
2ee0da16
VS
18813
18814 /* Wait for the clocks to stabilize. */
dc008bf0 18815 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18816 udelay(150);
18817
18818 /* The pixel multiplier can only be updated once the
18819 * DPLL is enabled and the clocks are stable.
18820 *
18821 * So write it again.
18822 */
dc008bf0 18823 intel_de_write(dev_priv, DPLL(pipe), dpll);
2ee0da16
VS
18824
18825 /* We do this three times for luck */
18826 for (i = 0; i < 3 ; i++) {
dc008bf0
JN
18827 intel_de_write(dev_priv, DPLL(pipe), dpll);
18828 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18829 udelay(150); /* wait for warmup */
18830 }
18831
dc008bf0
JN
18832 intel_de_write(dev_priv, PIPECONF(pipe),
18833 PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
18834 intel_de_posting_read(dev_priv, PIPECONF(pipe));
d5fb43cb
VS
18835
18836 intel_wait_for_pipe_scanline_moving(crtc);
2ee0da16
VS
18837}
18838
18839void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18840{
8fedd64d
VS
18841 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18842
cd49f818
WK
18843 drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
18844 pipe_name(pipe));
2ee0da16 18845
e57291c2
PB
18846 drm_WARN_ON(&dev_priv->drm,
18847 intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
18848 DISPLAY_PLANE_ENABLE);
18849 drm_WARN_ON(&dev_priv->drm,
18850 intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
18851 DISPLAY_PLANE_ENABLE);
18852 drm_WARN_ON(&dev_priv->drm,
18853 intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
18854 DISPLAY_PLANE_ENABLE);
18855 drm_WARN_ON(&dev_priv->drm,
18856 intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
18857 drm_WARN_ON(&dev_priv->drm,
18858 intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
2ee0da16 18859
dc008bf0
JN
18860 intel_de_write(dev_priv, PIPECONF(pipe), 0);
18861 intel_de_posting_read(dev_priv, PIPECONF(pipe));
2ee0da16 18862
8fedd64d 18863 intel_wait_for_pipe_scanline_stopped(crtc);
2ee0da16 18864
dc008bf0
JN
18865 intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18866 intel_de_posting_read(dev_priv, DPLL(pipe));
2ee0da16
VS
18867}
18868
b1e01595
VS
18869static void
18870intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18871{
18872 struct intel_crtc *crtc;
fa555837 18873
b1e01595
VS
18874 if (INTEL_GEN(dev_priv) >= 4)
18875 return;
fa555837 18876
b1e01595
VS
18877 for_each_intel_crtc(&dev_priv->drm, crtc) {
18878 struct intel_plane *plane =
18879 to_intel_plane(crtc->base.primary);
62358aa4
VS
18880 struct intel_crtc *plane_crtc;
18881 enum pipe pipe;
b1e01595 18882
62358aa4
VS
18883 if (!plane->get_hw_state(plane, &pipe))
18884 continue;
18885
18886 if (pipe == crtc->pipe)
b1e01595
VS
18887 continue;
18888
cd49f818
WK
18889 drm_dbg_kms(&dev_priv->drm,
18890 "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18891 plane->base.base.id, plane->base.name);
62358aa4
VS
18892
18893 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18894 intel_plane_disable_noatomic(plane_crtc, plane);
b1e01595 18895 }
fa555837
DV
18896}
18897
02e93c35
VS
18898static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18899{
18900 struct drm_device *dev = crtc->base.dev;
18901 struct intel_encoder *encoder;
18902
18903 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18904 return true;
18905
18906 return false;
18907}
18908
496b0fc3
ML
18909static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18910{
18911 struct drm_device *dev = encoder->base.dev;
18912 struct intel_connector *connector;
18913
18914 for_each_connector_on_encoder(dev, &encoder->base, connector)
18915 return connector;
18916
18917 return NULL;
18918}
18919
a168f5b3 18920static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
ecf837d9 18921 enum pipe pch_transcoder)
a168f5b3
VS
18922{
18923 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
ecf837d9 18924 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
a168f5b3
VS
18925}
18926
cc7a4cff 18927static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
24929352 18928{
cc7a4cff
VS
18929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18930 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1b52ad46 18931 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
24929352 18932
cc7a4cff
VS
18933 if (INTEL_GEN(dev_priv) >= 9 ||
18934 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18935 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18936 u32 val;
18937
18938 if (transcoder_is_dsi(cpu_transcoder))
18939 return;
18940
dc008bf0 18941 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18942 val &= ~HSW_FRAME_START_DELAY_MASK;
18943 val |= HSW_FRAME_START_DELAY(0);
dc008bf0 18944 intel_de_write(dev_priv, reg, val);
cc7a4cff 18945 } else {
4d1de975 18946 i915_reg_t reg = PIPECONF(cpu_transcoder);
cc7a4cff
VS
18947 u32 val;
18948
dc008bf0 18949 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18950 val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18951 val |= PIPECONF_FRAME_START_DELAY(0);
dc008bf0 18952 intel_de_write(dev_priv, reg, val);
cc7a4cff
VS
18953 }
18954
18955 if (!crtc_state->has_pch_encoder)
18956 return;
18957
18958 if (HAS_PCH_IBX(dev_priv)) {
18959 i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18960 u32 val;
18961
dc008bf0 18962 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18963 val &= ~TRANS_FRAME_START_DELAY_MASK;
18964 val |= TRANS_FRAME_START_DELAY(0);
dc008bf0 18965 intel_de_write(dev_priv, reg, val);
cc7a4cff 18966 } else {
7df49149
VS
18967 enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18968 i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
cc7a4cff 18969 u32 val;
4d1de975 18970
dc008bf0 18971 val = intel_de_read(dev_priv, reg);
cc7a4cff
VS
18972 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18973 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
dc008bf0 18974 intel_de_write(dev_priv, reg, val);
4d1de975 18975 }
cc7a4cff
VS
18976}
18977
18978static void intel_sanitize_crtc(struct intel_crtc *crtc,
18979 struct drm_modeset_acquire_ctx *ctx)
18980{
18981 struct drm_device *dev = crtc->base.dev;
18982 struct drm_i915_private *dev_priv = to_i915(dev);
18983 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
24929352 18984
1326a92c 18985 if (crtc_state->hw.active) {
f9cd7b88
VS
18986 struct intel_plane *plane;
18987
cc7a4cff
VS
18988 /* Clear any frame start delays used for debugging left by the BIOS */
18989 intel_sanitize_frame_start_delay(crtc_state);
18990
f9cd7b88
VS
18991 /* Disable everything but the primary plane */
18992 for_each_intel_plane_on_crtc(dev, crtc, plane) {
b1e01595
VS
18993 const struct intel_plane_state *plane_state =
18994 to_intel_plane_state(plane->base.state);
f9cd7b88 18995
f90a85e7 18996 if (plane_state->uapi.visible &&
b1e01595
VS
18997 plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18998 intel_plane_disable_noatomic(crtc, plane);
f9cd7b88 18999 }
c0550305
MR
19000
19001 /*
19002 * Disable any background color set by the BIOS, but enable the
19003 * gamma and CSC to match how we program our planes.
19004 */
19005 if (INTEL_GEN(dev_priv) >= 9)
dc008bf0
JN
19006 intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
19007 SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
9625604c 19008 }
d3eaf884 19009
24929352
DV
19010 /* Adjust the state of the output pipe according to whether we
19011 * have active connectors/encoders. */
0385ecea
MN
19012 if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
19013 !crtc_state->bigjoiner_slave)
56273062 19014 intel_crtc_disable_noatomic(crtc, ctx);
24929352 19015
1326a92c 19016 if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
4cc31489
DV
19017 /*
19018 * We start out with underrun reporting disabled to avoid races.
19019 * For correct bookkeeping mark this on active crtcs.
19020 *
c5ab3bc0
DV
19021 * Also on gmch platforms we dont have any hardware bits to
19022 * disable the underrun reporting. Which means we need to start
19023 * out with underrun reporting disabled also on inactive pipes,
19024 * since otherwise we'll complain about the garbage we read when
19025 * e.g. coming up after runtime pm.
19026 *
4cc31489
DV
19027 * No protection against concurrent access is required - at
19028 * worst a fifo underrun happens which also sets this to false.
19029 */
19030 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
19031 /*
19032 * We track the PCH trancoder underrun reporting state
19033 * within the crtc. With crtc for pipe A housing the underrun
19034 * reporting state for PCH transcoder A, crtc for pipe B housing
19035 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
19036 * and marking underrun reporting as disabled for the non-existing
19037 * PCH transcoders B and C would prevent enabling the south
19038 * error interrupt (see cpt_can_enable_serr_int()).
19039 */
ecf837d9 19040 if (has_pch_trancoder(dev_priv, crtc->pipe))
a168f5b3 19041 crtc->pch_fifo_underrun_disabled = true;
4cc31489 19042 }
24929352
DV
19043}
19044
7bed8adc
VS
19045static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
19046{
2225f3c6 19047 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7bed8adc
VS
19048
19049 /*
19050 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
19051 * the hardware when a high res displays plugged in. DPLL P
19052 * divider is zero, and the pipe timings are bonkers. We'll
19053 * try to disable everything in that case.
19054 *
19055 * FIXME would be nice to be able to sanitize this state
19056 * without several WARNs, but for now let's take the easy
19057 * road.
19058 */
19059 return IS_GEN(dev_priv, 6) &&
1326a92c 19060 crtc_state->hw.active &&
7bed8adc
VS
19061 crtc_state->shared_dpll &&
19062 crtc_state->port_clock == 0;
19063}
19064
24929352
DV
19065static void intel_sanitize_encoder(struct intel_encoder *encoder)
19066{
70332ac5 19067 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
24929352 19068 struct intel_connector *connector;
7bed8adc
VS
19069 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
19070 struct intel_crtc_state *crtc_state = crtc ?
19071 to_intel_crtc_state(crtc->base.state) : NULL;
24929352
DV
19072
19073 /* We need to check both for a crtc link (meaning that the
19074 * encoder is active and trying to read from a pipe) and the
19075 * pipe itself being active. */
7bed8adc 19076 bool has_active_crtc = crtc_state &&
1326a92c 19077 crtc_state->hw.active;
7bed8adc
VS
19078
19079 if (crtc_state && has_bogus_dpll_config(crtc_state)) {
cd49f818
WK
19080 drm_dbg_kms(&dev_priv->drm,
19081 "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
19082 pipe_name(crtc->pipe));
7bed8adc
VS
19083 has_active_crtc = false;
19084 }
24929352 19085
496b0fc3
ML
19086 connector = intel_encoder_find_connector(encoder);
19087 if (connector && !has_active_crtc) {
cd49f818
WK
19088 drm_dbg_kms(&dev_priv->drm,
19089 "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
19090 encoder->base.base.id,
19091 encoder->base.name);
24929352
DV
19092
19093 /* Connector is active, but has no active pipe. This is
19094 * fallout from our resume register restoring. Disable
19095 * the encoder manually again. */
7bed8adc
VS
19096 if (crtc_state) {
19097 struct drm_encoder *best_encoder;
fd6bbda9 19098
cd49f818
WK
19099 drm_dbg_kms(&dev_priv->drm,
19100 "[ENCODER:%d:%s] manually disabled\n",
19101 encoder->base.base.id,
19102 encoder->base.name);
7bed8adc
VS
19103
19104 /* avoid oopsing in case the hooks consult best_encoder */
19105 best_encoder = connector->base.state->best_encoder;
19106 connector->base.state->best_encoder = &encoder->base;
19107
ede9771d 19108 /* FIXME NULL atomic state passed! */
c84c6fe3 19109 if (encoder->disable)
ede9771d 19110 encoder->disable(NULL, encoder, crtc_state,
7bed8adc 19111 connector->base.state);
a62d1497 19112 if (encoder->post_disable)
ede9771d 19113 encoder->post_disable(NULL, encoder, crtc_state,
7bed8adc
VS
19114 connector->base.state);
19115
19116 connector->base.state->best_encoder = best_encoder;
24929352 19117 }
7f1950fb 19118 encoder->base.crtc = NULL;
24929352
DV
19119
19120 /* Inconsistent output/port/pipe state happens presumably due to
19121 * a bug in one of the get_hw_state functions. Or someplace else
19122 * in our code, like the register restore mess on resume. Clamp
19123 * things to off as a safer default. */
fd6bbda9
ML
19124
19125 connector->base.dpms = DRM_MODE_DPMS_OFF;
19126 connector->base.encoder = NULL;
24929352 19127 }
d6cae4aa
ML
19128
19129 /* notify opregion of the sanitized encoder state */
19130 intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
70332ac5
ID
19131
19132 if (INTEL_GEN(dev_priv) >= 11)
19133 icl_sanitize_encoder_pll_mapping(encoder);
24929352
DV
19134}
19135
f9cd7b88 19136/* FIXME read out full plane state for all planes */
62358aa4 19137static void readout_plane_state(struct drm_i915_private *dev_priv)
d032ffa0 19138{
b1e01595 19139 struct intel_plane *plane;
62358aa4 19140 struct intel_crtc *crtc;
d032ffa0 19141
62358aa4 19142 for_each_intel_plane(&dev_priv->drm, plane) {
b1e01595
VS
19143 struct intel_plane_state *plane_state =
19144 to_intel_plane_state(plane->base.state);
62358aa4
VS
19145 struct intel_crtc_state *crtc_state;
19146 enum pipe pipe = PIPE_A;
eade6c89
VS
19147 bool visible;
19148
19149 visible = plane->get_hw_state(plane, &pipe);
b26d3ea3 19150
62358aa4
VS
19151 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
19152 crtc_state = to_intel_crtc_state(crtc->base.state);
19153
b1e01595 19154 intel_set_plane_visible(crtc_state, plane_state, visible);
7a4a2a46 19155
cd49f818
WK
19156 drm_dbg_kms(&dev_priv->drm,
19157 "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
19158 plane->base.base.id, plane->base.name,
19159 enableddisabled(visible), pipe_name(pipe));
b1e01595 19160 }
62358aa4
VS
19161
19162 for_each_intel_crtc(&dev_priv->drm, crtc) {
19163 struct intel_crtc_state *crtc_state =
19164 to_intel_crtc_state(crtc->base.state);
19165
19166 fixup_active_planes(crtc_state);
19167 }
98ec7739
VS
19168}
19169
30e984df 19170static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 19171{
fac5e23e 19172 struct drm_i915_private *dev_priv = to_i915(dev);
28a30b45
VS
19173 struct intel_cdclk_state *cdclk_state =
19174 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
3cf43cdc
VS
19175 struct intel_dbuf_state *dbuf_state =
19176 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
24929352 19177 enum pipe pipe;
24929352
DV
19178 struct intel_crtc *crtc;
19179 struct intel_encoder *encoder;
19180 struct intel_connector *connector;
f9e905ca 19181 struct drm_connector_list_iter conn_iter;
0c2d5512 19182 u8 active_pipes = 0;
24929352 19183
d3fcc808 19184 for_each_intel_crtc(dev, crtc) {
a8cd6da0
VS
19185 struct intel_crtc_state *crtc_state =
19186 to_intel_crtc_state(crtc->base.state);
3b117c8f 19187
2225f3c6 19188 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
58d124ea 19189 intel_crtc_free_hw_state(crtc_state);
979e94c1 19190 intel_crtc_state_reset(crtc_state, crtc);
24929352 19191
291106cb
VS
19192 intel_crtc_get_pipe_config(crtc_state);
19193
19194 crtc_state->hw.enable = crtc_state->hw.active;
565602d7 19195
1326a92c
ML
19196 crtc->base.enabled = crtc_state->hw.enable;
19197 crtc->active = crtc_state->hw.active;
565602d7 19198
1326a92c 19199 if (crtc_state->hw.active)
0c2d5512 19200 active_pipes |= BIT(crtc->pipe);
565602d7 19201
cd49f818
WK
19202 drm_dbg_kms(&dev_priv->drm,
19203 "[CRTC:%d:%s] hw state readout: %s\n",
19204 crtc->base.base.id, crtc->base.name,
19205 enableddisabled(crtc_state->hw.active));
24929352
DV
19206 }
19207
3cf43cdc
VS
19208 dev_priv->active_pipes = cdclk_state->active_pipes =
19209 dbuf_state->active_pipes = active_pipes;
0c2d5512 19210
62358aa4
VS
19211 readout_plane_state(dev_priv);
19212
830b2cdc 19213 intel_dpll_readout_hw_state(dev_priv);
5358901f 19214
b2784e15 19215 for_each_intel_encoder(dev, encoder) {
24929352
DV
19216 pipe = 0;
19217
19218 if (encoder->get_hw_state(encoder, &pipe)) {
a8cd6da0
VS
19219 struct intel_crtc_state *crtc_state;
19220
98187836 19221 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a8cd6da0 19222 crtc_state = to_intel_crtc_state(crtc->base.state);
e2af48c6 19223
045ac3b5 19224 encoder->base.crtc = &crtc->base;
65c1ed30 19225 intel_encoder_get_config(encoder, crtc_state);
f9e76a6e
ID
19226 if (encoder->sync_state)
19227 encoder->sync_state(encoder, crtc_state);
0385ecea
MN
19228
19229 /* read out to slave crtc as well for bigjoiner */
19230 if (crtc_state->bigjoiner) {
19231 /* encoder should read be linked to bigjoiner master */
19232 WARN_ON(crtc_state->bigjoiner_slave);
19233
19234 crtc = crtc_state->bigjoiner_linked_crtc;
19235 crtc_state = to_intel_crtc_state(crtc->base.state);
19236 intel_encoder_get_config(encoder, crtc_state);
19237 }
24929352
DV
19238 } else {
19239 encoder->base.crtc = NULL;
19240 }
19241
cd49f818
WK
19242 drm_dbg_kms(&dev_priv->drm,
19243 "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
19244 encoder->base.base.id, encoder->base.name,
19245 enableddisabled(encoder->base.crtc),
19246 pipe_name(pipe));
24929352
DV
19247 }
19248
f9e905ca
DV
19249 drm_connector_list_iter_begin(dev, &conn_iter);
19250 for_each_intel_connector_iter(connector, &conn_iter) {
24929352 19251 if (connector->get_hw_state(connector)) {
de3b67af
VS
19252 struct intel_crtc_state *crtc_state;
19253 struct intel_crtc *crtc;
19254
24929352 19255 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9 19256
fa7edcd2 19257 encoder = intel_attached_encoder(connector);
2aa974c9
ML
19258 connector->base.encoder = &encoder->base;
19259
de3b67af
VS
19260 crtc = to_intel_crtc(encoder->base.crtc);
19261 crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
19262
1326a92c 19263 if (crtc_state && crtc_state->hw.active) {
2aa974c9
ML
19264 /*
19265 * This has to be done during hardware readout
19266 * because anything calling .crtc_disable may
19267 * rely on the connector_mask being accurate.
19268 */
2225f3c6 19269 crtc_state->uapi.connector_mask |=
40560e26 19270 drm_connector_mask(&connector->base);
2225f3c6 19271 crtc_state->uapi.encoder_mask |=
40560e26 19272 drm_encoder_mask(&encoder->base);
2aa974c9 19273 }
24929352
DV
19274 } else {
19275 connector->base.dpms = DRM_MODE_DPMS_OFF;
19276 connector->base.encoder = NULL;
19277 }
cd49f818
WK
19278 drm_dbg_kms(&dev_priv->drm,
19279 "[CONNECTOR:%d:%s] hw state readout: %s\n",
19280 connector->base.base.id, connector->base.name,
19281 enableddisabled(connector->base.encoder));
24929352 19282 }
f9e905ca 19283 drm_connector_list_iter_end(&conn_iter);
7f4c6284
VS
19284
19285 for_each_intel_crtc(dev, crtc) {
c457d9cf
VS
19286 struct intel_bw_state *bw_state =
19287 to_intel_bw_state(dev_priv->bw_obj.state);
a8cd6da0
VS
19288 struct intel_crtc_state *crtc_state =
19289 to_intel_crtc_state(crtc->base.state);
c457d9cf 19290 struct intel_plane *plane;
d305e061 19291 int min_cdclk = 0;
aca1ebf4 19292
0385ecea
MN
19293 if (crtc_state->bigjoiner_slave)
19294 continue;
19295
1326a92c 19296 if (crtc_state->hw.active) {
7f4c6284
VS
19297 /*
19298 * The initial mode needs to be set in order to keep
19299 * the atomic core happy. It wants a valid mode if the
19300 * crtc's enabled, so we do the above call.
19301 *
7800fb69
DV
19302 * But we don't set all the derived state fully, hence
19303 * set a flag to indicate that a full recalculation is
19304 * needed on the next commit.
7f4c6284 19305 */
a227569d 19306 crtc_state->inherited = true;
9eca6832 19307
99325429 19308 intel_crtc_update_active_timings(crtc_state);
58d124ea
ML
19309
19310 intel_crtc_copy_hw_to_uapi_state(crtc_state);
7f4c6284 19311 }
e3b247da 19312
c457d9cf
VS
19313 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
19314 const struct intel_plane_state *plane_state =
19315 to_intel_plane_state(plane->base.state);
19316
19317 /*
19318 * FIXME don't have the fb yet, so can't
19319 * use intel_plane_data_rate() :(
19320 */
f90a85e7 19321 if (plane_state->uapi.visible)
c457d9cf
VS
19322 crtc_state->data_rate[plane->id] =
19323 4 * crtc_state->pixel_rate;
bb6ae9e6
VS
19324 /*
19325 * FIXME don't have the fb yet, so can't
19326 * use plane->min_cdclk() :(
19327 */
f90a85e7 19328 if (plane_state->uapi.visible && plane->min_cdclk) {
bb6ae9e6
VS
19329 if (crtc_state->double_wide ||
19330 INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
19331 crtc_state->min_cdclk[plane->id] =
19332 DIV_ROUND_UP(crtc_state->pixel_rate, 2);
19333 else
19334 crtc_state->min_cdclk[plane->id] =
19335 crtc_state->pixel_rate;
19336 }
cd49f818
WK
19337 drm_dbg_kms(&dev_priv->drm,
19338 "[PLANE:%d:%s] min_cdclk %d kHz\n",
19339 plane->base.base.id, plane->base.name,
19340 crtc_state->min_cdclk[plane->id]);
bb6ae9e6
VS
19341 }
19342
1326a92c 19343 if (crtc_state->hw.active) {
bb6ae9e6 19344 min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
e57291c2 19345 if (drm_WARN_ON(dev, min_cdclk < 0))
bb6ae9e6 19346 min_cdclk = 0;
c457d9cf
VS
19347 }
19348
1965de63
VS
19349 cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
19350 cdclk_state->min_voltage_level[crtc->pipe] =
bb6ae9e6
VS
19351 crtc_state->min_voltage_level;
19352
c457d9cf
VS
19353 intel_bw_crtc_update(bw_state, crtc_state);
19354
a8cd6da0 19355 intel_pipe_config_sanity_check(dev_priv, crtc_state);
0385ecea
MN
19356
19357 /* discard our incomplete slave state, copy it from master */
19358 if (crtc_state->bigjoiner && crtc_state->hw.active) {
19359 struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
19360 struct intel_crtc_state *slave_crtc_state =
19361 to_intel_crtc_state(slave->base.state);
19362
19363 copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
19364 slave->base.mode = crtc->base.mode;
19365
19366 cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
19367 cdclk_state->min_voltage_level[slave->pipe] =
19368 crtc_state->min_voltage_level;
19369
19370 for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
19371 const struct intel_plane_state *plane_state =
19372 to_intel_plane_state(plane->base.state);
19373
19374 /*
19375 * FIXME don't have the fb yet, so can't
19376 * use intel_plane_data_rate() :(
19377 */
19378 if (plane_state->uapi.visible)
19379 crtc_state->data_rate[plane->id] =
19380 4 * crtc_state->pixel_rate;
19381 else
19382 crtc_state->data_rate[plane->id] = 0;
19383 }
19384
19385 intel_bw_crtc_update(bw_state, slave_crtc_state);
19386 drm_calc_timestamping_constants(&slave->base,
19387 &slave_crtc_state->hw.adjusted_mode);
19388 }
7f4c6284 19389 }
30e984df
DV
19390}
19391
62b69566
ACO
19392static void
19393get_encoder_power_domains(struct drm_i915_private *dev_priv)
19394{
19395 struct intel_encoder *encoder;
19396
19397 for_each_intel_encoder(&dev_priv->drm, encoder) {
52528055 19398 struct intel_crtc_state *crtc_state;
62b69566
ACO
19399
19400 if (!encoder->get_power_domains)
19401 continue;
19402
52528055 19403 /*
b79ebe74
ID
19404 * MST-primary and inactive encoders don't have a crtc state
19405 * and neither of these require any power domain references.
52528055 19406 */
b79ebe74
ID
19407 if (!encoder->base.crtc)
19408 continue;
52528055 19409
b79ebe74 19410 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
3a52fb7e 19411 encoder->get_power_domains(encoder, crtc_state);
62b69566
ACO
19412 }
19413}
19414
df49ec82
RV
19415static void intel_early_display_was(struct drm_i915_private *dev_priv)
19416{
1e1a139d
MR
19417 /*
19418 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
19419 * Also known as Wa_14010480278.
19420 */
19421 if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
dc008bf0
JN
19422 intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
19423 intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
df49ec82
RV
19424
19425 if (IS_HASWELL(dev_priv)) {
19426 /*
19427 * WaRsPkgCStateDisplayPMReq:hsw
19428 * System hang if this isn't done before disabling all planes!
19429 */
dc008bf0
JN
19430 intel_de_write(dev_priv, CHICKEN_PAR1_1,
19431 intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
df49ec82 19432 }
562ad8ad
VS
19433
19434 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
19435 /* Display WA #1142:kbl,cfl,cml */
19436 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
19437 KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
19438 intel_de_rmw(dev_priv, CHICKEN_MISC_2,
19439 KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
19440 KBL_ARB_FILL_SPARE_14);
19441 }
df49ec82
RV
19442}
19443
3aefb67f
VS
19444static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
19445 enum port port, i915_reg_t hdmi_reg)
19446{
dc008bf0 19447 u32 val = intel_de_read(dev_priv, hdmi_reg);
3aefb67f
VS
19448
19449 if (val & SDVO_ENABLE ||
19450 (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
19451 return;
19452
cd49f818
WK
19453 drm_dbg_kms(&dev_priv->drm,
19454 "Sanitizing transcoder select for HDMI %c\n",
19455 port_name(port));
3aefb67f
VS
19456
19457 val &= ~SDVO_PIPE_SEL_MASK;
19458 val |= SDVO_PIPE_SEL(PIPE_A);
19459
dc008bf0 19460 intel_de_write(dev_priv, hdmi_reg, val);
3aefb67f
VS
19461}
19462
19463static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
19464 enum port port, i915_reg_t dp_reg)
19465{
dc008bf0 19466 u32 val = intel_de_read(dev_priv, dp_reg);
3aefb67f
VS
19467
19468 if (val & DP_PORT_EN ||
19469 (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
19470 return;
19471
cd49f818
WK
19472 drm_dbg_kms(&dev_priv->drm,
19473 "Sanitizing transcoder select for DP %c\n",
19474 port_name(port));
3aefb67f
VS
19475
19476 val &= ~DP_PIPE_SEL_MASK;
19477 val |= DP_PIPE_SEL(PIPE_A);
19478
dc008bf0 19479 intel_de_write(dev_priv, dp_reg, val);
3aefb67f
VS
19480}
19481
19482static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
19483{
19484 /*
19485 * The BIOS may select transcoder B on some of the PCH
19486 * ports even it doesn't enable the port. This would trip
19487 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
19488 * Sanitize the transcoder select bits to prevent that. We
19489 * assume that the BIOS never actually enabled the port,
19490 * because if it did we'd actually have to toggle the port
19491 * on and back off to make the transcoder A select stick
19492 * (see. intel_dp_link_down(), intel_disable_hdmi(),
19493 * intel_disable_sdvo()).
19494 */
19495 ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
19496 ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
19497 ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
19498
19499 /* PCH SDVOB multiplex with HDMIB */
19500 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
19501 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
19502 ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
19503}
19504
043e9bda
ML
19505/* Scan out the current hw modeset state,
19506 * and sanitizes it to the current state
19507 */
19508static void
aecd36b8
VS
19509intel_modeset_setup_hw_state(struct drm_device *dev,
19510 struct drm_modeset_acquire_ctx *ctx)
30e984df 19511{
fac5e23e 19512 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 19513 struct intel_encoder *encoder;
0e6e0be4
CW
19514 struct intel_crtc *crtc;
19515 intel_wakeref_t wakeref;
30e984df 19516
0e6e0be4 19517 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
2cd9a689 19518
df49ec82 19519 intel_early_display_was(dev_priv);
30e984df 19520 intel_modeset_readout_hw_state(dev);
24929352
DV
19521
19522 /* HW state is read out, now we need to sanitize this mess. */
32691b58
ID
19523
19524 /* Sanitize the TypeC port mode upfront, encoders depend on this */
19525 for_each_intel_encoder(dev, encoder) {
d8fe2ab6
MR
19526 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
19527
32691b58
ID
19528 /* We need to sanitize only the MST primary port. */
19529 if (encoder->type != INTEL_OUTPUT_DP_MST &&
d8fe2ab6 19530 intel_phy_is_tc(dev_priv, phy))
b7d02c3a 19531 intel_tc_port_sanitize(enc_to_dig_port(encoder));
32691b58
ID
19532 }
19533
62b69566 19534 get_encoder_power_domains(dev_priv);
3aefb67f
VS
19535
19536 if (HAS_PCH_IBX(dev_priv))
19537 ibx_sanitize_pch_ports(dev_priv);
62b69566 19538
68bc30de
VS
19539 /*
19540 * intel_sanitize_plane_mapping() may need to do vblank
19541 * waits, so we need vblank interrupts restored beforehand.
19542 */
19543 for_each_intel_crtc(&dev_priv->drm, crtc) {
777bf6d7
VS
19544 struct intel_crtc_state *crtc_state =
19545 to_intel_crtc_state(crtc->base.state);
32db0b65 19546
68bc30de 19547 drm_crtc_vblank_reset(&crtc->base);
b1e01595 19548
1326a92c 19549 if (crtc_state->hw.active)
32db0b65 19550 intel_crtc_vblank_on(crtc_state);
24929352
DV
19551 }
19552
68bc30de 19553 intel_sanitize_plane_mapping(dev_priv);
e2af48c6 19554
68bc30de
VS
19555 for_each_intel_encoder(dev, encoder)
19556 intel_sanitize_encoder(encoder);
19557
19558 for_each_intel_crtc(&dev_priv->drm, crtc) {
777bf6d7 19559 struct intel_crtc_state *crtc_state =
7b0bcead 19560 to_intel_crtc_state(crtc->base.state);
777bf6d7 19561
aecd36b8 19562 intel_sanitize_crtc(crtc, ctx);
10d75f54 19563 intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
24929352 19564 }
9a935856 19565
d29b2f9d
ACO
19566 intel_modeset_update_connector_atomic_state(dev);
19567
830b2cdc 19568 intel_dpll_sanitize_state(dev_priv);
35c95375 19569
04548cba 19570 if (IS_G4X(dev_priv)) {
cd1d3ee9 19571 g4x_wm_get_hw_state(dev_priv);
04548cba
VS
19572 g4x_wm_sanitize(dev_priv);
19573 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
cd1d3ee9 19574 vlv_wm_get_hw_state(dev_priv);
602ae835 19575 vlv_wm_sanitize(dev_priv);
a029fa4d 19576 } else if (INTEL_GEN(dev_priv) >= 9) {
cd1d3ee9 19577 skl_wm_get_hw_state(dev_priv);
602ae835 19578 } else if (HAS_PCH_SPLIT(dev_priv)) {
cd1d3ee9 19579 ilk_wm_get_hw_state(dev_priv);
602ae835 19580 }
292b990e
ML
19581
19582 for_each_intel_crtc(dev, crtc) {
777bf6d7
VS
19583 struct intel_crtc_state *crtc_state =
19584 to_intel_crtc_state(crtc->base.state);
d8fc70b7 19585 u64 put_domains;
292b990e 19586
afe0c21b 19587 put_domains = modeset_get_crtc_power_domains(crtc_state);
e57291c2 19588 if (drm_WARN_ON(dev, put_domains))
292b990e
ML
19589 modeset_put_power_domains(dev_priv, put_domains);
19590 }
2cd9a689 19591
0e6e0be4 19592 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
043e9bda 19593}
7d0bc1ea 19594
043e9bda
ML
19595void intel_display_resume(struct drm_device *dev)
19596{
e2c8b870
ML
19597 struct drm_i915_private *dev_priv = to_i915(dev);
19598 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
19599 struct drm_modeset_acquire_ctx ctx;
043e9bda 19600 int ret;
f30da187 19601
e2c8b870 19602 dev_priv->modeset_restore_state = NULL;
73974893
ML
19603 if (state)
19604 state->acquire_ctx = &ctx;
043e9bda 19605
e2c8b870 19606 drm_modeset_acquire_init(&ctx, 0);
043e9bda 19607
73974893
ML
19608 while (1) {
19609 ret = drm_modeset_lock_all_ctx(dev, &ctx);
19610 if (ret != -EDEADLK)
19611 break;
043e9bda 19612
e2c8b870 19613 drm_modeset_backoff(&ctx);
e2c8b870 19614 }
043e9bda 19615
73974893 19616 if (!ret)
581e49fe 19617 ret = __intel_display_resume(dev, state, &ctx);
73974893 19618
2503a0fe 19619 intel_enable_ipc(dev_priv);
e2c8b870
ML
19620 drm_modeset_drop_locks(&ctx);
19621 drm_modeset_acquire_fini(&ctx);
043e9bda 19622
0853695c 19623 if (ret)
cd49f818
WK
19624 drm_err(&dev_priv->drm,
19625 "Restoring old state failed with %i\n", ret);
3c5e37f1
CW
19626 if (state)
19627 drm_atomic_state_put(state);
2c7111db
CW
19628}
19629
9980c3c1 19630static void intel_hpd_poll_fini(struct drm_i915_private *i915)
886c6b86
MN
19631{
19632 struct intel_connector *connector;
19633 struct drm_connector_list_iter conn_iter;
19634
448aa911 19635 /* Kill all the work that may have been queued by hpd. */
9980c3c1 19636 drm_connector_list_iter_begin(&i915->drm, &conn_iter);
886c6b86
MN
19637 for_each_intel_connector_iter(connector, &conn_iter) {
19638 if (connector->modeset_retry_work.func)
19639 cancel_work_sync(&connector->modeset_retry_work);
d3dacc70
R
19640 if (connector->hdcp.shim) {
19641 cancel_delayed_work_sync(&connector->hdcp.check_work);
19642 cancel_work_sync(&connector->hdcp.prop_work);
ee5e5e7a 19643 }
886c6b86
MN
19644 }
19645 drm_connector_list_iter_end(&conn_iter);
19646}
19647
93a0ed6c 19648/* part #1: call before irq uninstall */
9980c3c1 19649void intel_modeset_driver_remove(struct drm_i915_private *i915)
79e53945 19650{
9980c3c1
JN
19651 flush_workqueue(i915->flip_wq);
19652 flush_workqueue(i915->modeset_wq);
8bcf9f70 19653
9980c3c1 19654 flush_work(&i915->atomic_helper.free_work);
e57291c2 19655 drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
93a0ed6c 19656}
eb955eee 19657
93a0ed6c
JN
19658/* part #2: call after irq uninstall */
19659void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
19660{
fd0c0642
DV
19661 /*
19662 * Due to the hpd irq storm handling the hotplug work can re-arm the
19663 * poll handlers. Hence disable polling after hpd handling is shut down.
19664 */
9980c3c1 19665 intel_hpd_poll_fini(i915);
fd0c0642 19666
d5746bf2
JRS
19667 /*
19668 * MST topology needs to be suspended so we don't have any calls to
19669 * fbdev after it's finalized. MST will be destroyed later as part of
19670 * drm_mode_config_cleanup()
19671 */
19672 intel_dp_mst_suspend(i915);
19673
4f256d82 19674 /* poll work can call into fbdev, hence clean that up afterwards */
9980c3c1 19675 intel_fbdev_fini(i915);
4f256d82 19676
723bfd70
JB
19677 intel_unregister_dsm_handler();
19678
9980c3c1 19679 intel_fbc_global_disable(i915);
69341a5e 19680
1630fe75
CW
19681 /* flush any delayed tasks or pending work */
19682 flush_scheduled_work();
19683
9980c3c1 19684 intel_hdcp_component_fini(i915);
9055aac7 19685
0ef1905e 19686 intel_mode_config_cleanup(i915);
4d7bb011 19687
9980c3c1 19688 intel_overlay_cleanup(i915);
ae48434c 19689
9980c3c1 19690 intel_gmbus_teardown(i915);
757fffcf 19691
9980c3c1
JN
19692 destroy_workqueue(i915->flip_wq);
19693 destroy_workqueue(i915->modeset_wq);
acde44b5 19694
9980c3c1 19695 intel_fbc_cleanup_cfb(i915);
79e53945
JB
19696}
19697
eb4612d8
JN
19698/* part #3: call after gem init */
19699void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
19700{
19701 intel_csr_ucode_fini(i915);
19702
19703 intel_power_domains_driver_remove(i915);
19704
19705 intel_vga_unregister(i915);
19706
19707 intel_bios_driver_remove(i915);
19708}
19709
98a2f411
CW
19710#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
19711
c4a1d9e4 19712struct intel_display_error_state {
ff57f1b0
PZ
19713
19714 u32 power_well_driver;
19715
c4a1d9e4
CW
19716 struct intel_cursor_error_state {
19717 u32 control;
19718 u32 position;
19719 u32 base;
19720 u32 size;
52331309 19721 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
19722
19723 struct intel_pipe_error_state {
ddf9c536 19724 bool power_domain_on;
c4a1d9e4 19725 u32 source;
f301b1e1 19726 u32 stat;
52331309 19727 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
19728
19729 struct intel_plane_error_state {
19730 u32 control;
19731 u32 stride;
19732 u32 size;
19733 u32 pos;
19734 u32 addr;
19735 u32 surface;
19736 u32 tile_offset;
52331309 19737 } plane[I915_MAX_PIPES];
63b66e5b
CW
19738
19739 struct intel_transcoder_error_state {
062de72b 19740 bool available;
ddf9c536 19741 bool power_domain_on;
63b66e5b
CW
19742 enum transcoder cpu_transcoder;
19743
19744 u32 conf;
19745
19746 u32 htotal;
19747 u32 hblank;
19748 u32 hsync;
19749 u32 vtotal;
19750 u32 vblank;
19751 u32 vsync;
f1f1d4fa 19752 } transcoder[5];
c4a1d9e4
CW
19753};
19754
19755struct intel_display_error_state *
c033666a 19756intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 19757{
c4a1d9e4 19758 struct intel_display_error_state *error;
63b66e5b
CW
19759 int transcoders[] = {
19760 TRANSCODER_A,
19761 TRANSCODER_B,
19762 TRANSCODER_C,
f1f1d4fa 19763 TRANSCODER_D,
63b66e5b
CW
19764 TRANSCODER_EDP,
19765 };
c4a1d9e4
CW
19766 int i;
19767
062de72b
LDM
19768 BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
19769
da27bd41 19770 if (!HAS_DISPLAY(dev_priv))
63b66e5b
CW
19771 return NULL;
19772
9d1cb914 19773 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
19774 if (error == NULL)
19775 return NULL;
19776
c033666a 19777 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
dc008bf0
JN
19778 error->power_well_driver = intel_de_read(dev_priv,
19779 HSW_PWR_WELL_CTL2);
ff57f1b0 19780
055e393f 19781 for_each_pipe(dev_priv, i) {
ddf9c536 19782 error->pipe[i].power_domain_on =
f458ebbc
DV
19783 __intel_display_power_is_enabled(dev_priv,
19784 POWER_DOMAIN_PIPE(i));
ddf9c536 19785 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
19786 continue;
19787
dc008bf0
JN
19788 error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
19789 error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
19790 error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
c4a1d9e4 19791
dc008bf0
JN
19792 error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
19793 error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
c033666a 19794 if (INTEL_GEN(dev_priv) <= 3) {
dc008bf0
JN
19795 error->plane[i].size = intel_de_read(dev_priv,
19796 DSPSIZE(i));
19797 error->plane[i].pos = intel_de_read(dev_priv,
19798 DSPPOS(i));
80ca378b 19799 }
c033666a 19800 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
dc008bf0
JN
19801 error->plane[i].addr = intel_de_read(dev_priv,
19802 DSPADDR(i));
c033666a 19803 if (INTEL_GEN(dev_priv) >= 4) {
dc008bf0
JN
19804 error->plane[i].surface = intel_de_read(dev_priv,
19805 DSPSURF(i));
19806 error->plane[i].tile_offset = intel_de_read(dev_priv,
19807 DSPTILEOFF(i));
c4a1d9e4
CW
19808 }
19809
dc008bf0 19810 error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
f301b1e1 19811
b2ae318a 19812 if (HAS_GMCH(dev_priv))
dc008bf0
JN
19813 error->pipe[i].stat = intel_de_read(dev_priv,
19814 PIPESTAT(i));
63b66e5b
CW
19815 }
19816
062de72b 19817 for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
63b66e5b
CW
19818 enum transcoder cpu_transcoder = transcoders[i];
19819
10cf8e75 19820 if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
062de72b
LDM
19821 continue;
19822
19823 error->transcoder[i].available = true;
ddf9c536 19824 error->transcoder[i].power_domain_on =
f458ebbc 19825 __intel_display_power_is_enabled(dev_priv,
38cc1daf 19826 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 19827 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
19828 continue;
19829
63b66e5b
CW
19830 error->transcoder[i].cpu_transcoder = cpu_transcoder;
19831
dc008bf0
JN
19832 error->transcoder[i].conf = intel_de_read(dev_priv,
19833 PIPECONF(cpu_transcoder));
19834 error->transcoder[i].htotal = intel_de_read(dev_priv,
19835 HTOTAL(cpu_transcoder));
19836 error->transcoder[i].hblank = intel_de_read(dev_priv,
19837 HBLANK(cpu_transcoder));
19838 error->transcoder[i].hsync = intel_de_read(dev_priv,
19839 HSYNC(cpu_transcoder));
19840 error->transcoder[i].vtotal = intel_de_read(dev_priv,
19841 VTOTAL(cpu_transcoder));
19842 error->transcoder[i].vblank = intel_de_read(dev_priv,
19843 VBLANK(cpu_transcoder));
19844 error->transcoder[i].vsync = intel_de_read(dev_priv,
19845 VSYNC(cpu_transcoder));
c4a1d9e4
CW
19846 }
19847
19848 return error;
19849}
19850
edc3d884
MK
19851#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
19852
c4a1d9e4 19853void
edc3d884 19854intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
19855 struct intel_display_error_state *error)
19856{
5a4c6f1b 19857 struct drm_i915_private *dev_priv = m->i915;
c4a1d9e4
CW
19858 int i;
19859
63b66e5b
CW
19860 if (!error)
19861 return;
19862
24977870 19863 err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
8652744b 19864 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 19865 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 19866 error->power_well_driver);
055e393f 19867 for_each_pipe(dev_priv, i) {
edc3d884 19868 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 19869 err_printf(m, " Power: %s\n",
87ad3212 19870 onoff(error->pipe[i].power_domain_on));
edc3d884 19871 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 19872 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
19873
19874 err_printf(m, "Plane [%d]:\n", i);
19875 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
19876 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 19877 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
19878 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
19879 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 19880 }
772c2a51 19881 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 19882 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 19883 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
19884 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
19885 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
19886 }
19887
edc3d884
MK
19888 err_printf(m, "Cursor [%d]:\n", i);
19889 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
19890 err_printf(m, " POS: %08x\n", error->cursor[i].position);
19891 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 19892 }
63b66e5b 19893
062de72b
LDM
19894 for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19895 if (!error->transcoder[i].available)
19896 continue;
19897
da205630 19898 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 19899 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 19900 err_printf(m, " Power: %s\n",
87ad3212 19901 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
19902 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
19903 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
19904 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
19905 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
19906 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
19907 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
19908 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
19909 }
c4a1d9e4 19910}
98a2f411
CW
19911
19912#endif