]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Remove some post-commit members from intel_crtc->atomic, v3.
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
fd8e058a
AG
47#include <linux/reservation.h>
48#include <linux/dma-buf.h>
79e53945 49
465c120c 50/* Primary plane formats for gen <= 3 */
568db4f2 51static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
52 DRM_FORMAT_C8,
53 DRM_FORMAT_RGB565,
465c120c 54 DRM_FORMAT_XRGB1555,
67fe7dc5 55 DRM_FORMAT_XRGB8888,
465c120c
MR
56};
57
58/* Primary plane formats for gen >= 4 */
568db4f2 59static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
60 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_XRGB2101010,
65 DRM_FORMAT_XBGR2101010,
66};
67
68static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
69 DRM_FORMAT_C8,
70 DRM_FORMAT_RGB565,
71 DRM_FORMAT_XRGB8888,
465c120c 72 DRM_FORMAT_XBGR8888,
67fe7dc5 73 DRM_FORMAT_ARGB8888,
465c120c
MR
74 DRM_FORMAT_ABGR8888,
75 DRM_FORMAT_XRGB2101010,
465c120c 76 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
77 DRM_FORMAT_YUYV,
78 DRM_FORMAT_YVYU,
79 DRM_FORMAT_UYVY,
80 DRM_FORMAT_VYUY,
465c120c
MR
81};
82
3d7d6510
MR
83/* Cursor formats */
84static const uint32_t intel_cursor_formats[] = {
85 DRM_FORMAT_ARGB8888,
86};
87
f1f644dc 88static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 89 struct intel_crtc_state *pipe_config);
18442d08 90static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 91 struct intel_crtc_state *pipe_config);
f1f644dc 92
eb1bfe80
JB
93static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
5b18e57c
DV
97static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 99static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
100 struct intel_link_m_n *m_n,
101 struct intel_link_m_n *m2_n2);
29407aab 102static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
103static void haswell_set_pipeconf(struct drm_crtc *crtc);
104static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 105static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 106 const struct intel_crtc_state *pipe_config);
d288f65f 107static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 108 const struct intel_crtc_state *pipe_config);
613d2b27
ML
109static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
110static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
549e2bfb
CK
111static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
112 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
113static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
114 int num_connectors);
bfd16b2a
ML
115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 118static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
e7457a9a 120
79e53945 121typedef struct {
0206e353 122 int min, max;
79e53945
JB
123} intel_range_t;
124
125typedef struct {
0206e353
AJ
126 int dot_limit;
127 int p2_slow, p2_fast;
79e53945
JB
128} intel_p2_t;
129
d4906093
ML
130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
0206e353
AJ
132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
d4906093 134};
79e53945 135
bfa7df01
VS
136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
e7dc33f3
VS
172static int
173intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 174{
e7dc33f3
VS
175 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
176}
d2acd215 177
e7dc33f3
VS
178static int
179intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
180{
35d38d1f
VS
181 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
182 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
DV
183}
184
e7dc33f3
VS
185static int
186intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 187{
79e50a4f
JN
188 uint32_t clkcfg;
189
e7dc33f3 190 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
e7dc33f3 194 return 100000;
79e50a4f 195 case CLKCFG_FSB_533:
e7dc33f3 196 return 133333;
79e50a4f 197 case CLKCFG_FSB_667:
e7dc33f3 198 return 166667;
79e50a4f 199 case CLKCFG_FSB_800:
e7dc33f3 200 return 200000;
79e50a4f 201 case CLKCFG_FSB_1067:
e7dc33f3 202 return 266667;
79e50a4f 203 case CLKCFG_FSB_1333:
e7dc33f3 204 return 333333;
79e50a4f
JN
205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
e7dc33f3 208 return 400000;
79e50a4f 209 default:
e7dc33f3 210 return 133333;
79e50a4f
JN
211 }
212}
213
e7dc33f3
VS
214static void intel_update_rawclk(struct drm_i915_private *dev_priv)
215{
216 if (HAS_PCH_SPLIT(dev_priv))
217 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
218 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
219 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
220 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
221 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
222 else
223 return; /* no rawclk on other platforms, or no need to know it */
224
225 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
226}
227
bfa7df01
VS
228static void intel_update_czclk(struct drm_i915_private *dev_priv)
229{
666a4537 230 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
231 return;
232
233 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
234 CCK_CZ_CLOCK_CONTROL);
235
236 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
237}
238
021357ac 239static inline u32 /* units of 100MHz */
21a727b3
VS
240intel_fdi_link_freq(struct drm_i915_private *dev_priv,
241 const struct intel_crtc_state *pipe_config)
021357ac 242{
21a727b3
VS
243 if (HAS_DDI(dev_priv))
244 return pipe_config->port_clock; /* SPLL */
245 else if (IS_GEN5(dev_priv))
246 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 247 else
21a727b3 248 return 270000;
021357ac
CW
249}
250
5d536e28 251static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 252 .dot = { .min = 25000, .max = 350000 },
9c333719 253 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 254 .n = { .min = 2, .max = 16 },
0206e353
AJ
255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
262};
263
5d536e28
DV
264static const intel_limit_t intel_limits_i8xx_dvo = {
265 .dot = { .min = 25000, .max = 350000 },
9c333719 266 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 267 .n = { .min = 2, .max = 16 },
5d536e28
DV
268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 4 },
275};
276
e4b36699 277static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 278 .dot = { .min = 25000, .max = 350000 },
9c333719 279 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 280 .n = { .min = 2, .max = 16 },
0206e353
AJ
281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 14, .p2_fast = 7 },
e4b36699 288};
273e27ca 289
e4b36699 290static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
299 .p2 = { .dot_limit = 200000,
300 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
301};
302
303static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
310 .p = { .min = 7, .max = 98 },
311 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
312 .p2 = { .dot_limit = 112000,
313 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
314};
315
273e27ca 316
e4b36699 317static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
318 .dot = { .min = 25000, .max = 270000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 17, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 10, .max = 30 },
325 .p1 = { .min = 1, .max = 3},
326 .p2 = { .dot_limit = 270000,
327 .p2_slow = 10,
328 .p2_fast = 10
044c7c41 329 },
e4b36699
KP
330};
331
332static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
333 .dot = { .min = 22000, .max = 400000 },
334 .vco = { .min = 1750000, .max = 3500000},
335 .n = { .min = 1, .max = 4 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 16, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 5, .max = 80 },
340 .p1 = { .min = 1, .max = 8},
341 .p2 = { .dot_limit = 165000,
342 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
343};
344
345static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
346 .dot = { .min = 20000, .max = 115000 },
347 .vco = { .min = 1750000, .max = 3500000 },
348 .n = { .min = 1, .max = 3 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 17, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 0,
355 .p2_slow = 14, .p2_fast = 14
044c7c41 356 },
e4b36699
KP
357};
358
359static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
360 .dot = { .min = 80000, .max = 224000 },
361 .vco = { .min = 1750000, .max = 3500000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 104, .max = 138 },
364 .m1 = { .min = 17, .max = 23 },
365 .m2 = { .min = 5, .max = 11 },
366 .p = { .min = 14, .max = 42 },
367 .p1 = { .min = 2, .max = 6 },
368 .p2 = { .dot_limit = 0,
369 .p2_slow = 7, .p2_fast = 7
044c7c41 370 },
e4b36699
KP
371};
372
f2b115e6 373static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
374 .dot = { .min = 20000, .max = 400000},
375 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 376 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
377 .n = { .min = 3, .max = 6 },
378 .m = { .min = 2, .max = 256 },
273e27ca 379 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 5, .max = 80 },
383 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
384 .p2 = { .dot_limit = 200000,
385 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
386};
387
f2b115e6 388static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
389 .dot = { .min = 20000, .max = 400000 },
390 .vco = { .min = 1700000, .max = 3500000 },
391 .n = { .min = 3, .max = 6 },
392 .m = { .min = 2, .max = 256 },
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 7, .max = 112 },
396 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
397 .p2 = { .dot_limit = 112000,
398 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
399};
400
273e27ca
EA
401/* Ironlake / Sandybridge
402 *
403 * We calculate clock using (register_value + 2) for N/M1/M2, so here
404 * the range value for them is (actual_value - 2).
405 */
b91ad0ec 406static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 5 },
410 .m = { .min = 79, .max = 127 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 5, .max = 80 },
414 .p1 = { .min = 1, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
417};
418
b91ad0ec 419static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 118 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 28, .max = 112 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
430};
431
432static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 127 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 14, .max = 56 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
443};
444
273e27ca 445/* LVDS 100mhz refclk limits. */
b91ad0ec 446static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 2 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 28, .max = 112 },
0206e353 454 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
457};
458
459static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 3 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 14, .max = 42 },
0206e353 467 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
470};
471
dc730512 472static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 480 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 481 .n = { .min = 1, .max = 7 },
a0c4da24
JB
482 .m1 = { .min = 2, .max = 3 },
483 .m2 = { .min = 11, .max = 156 },
b99ab663 484 .p1 = { .min = 2, .max = 3 },
5fdc9c49 485 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
486};
487
ef9348c8
CML
488static const intel_limit_t intel_limits_chv = {
489 /*
490 * These are the data rate limits (measured in fast clocks)
491 * since those are the strictest limits we have. The fast
492 * clock and actual rate limits are more relaxed, so checking
493 * them would make no difference.
494 */
495 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 496 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
497 .n = { .min = 1, .max = 1 },
498 .m1 = { .min = 2, .max = 2 },
499 .m2 = { .min = 24 << 22, .max = 175 << 22 },
500 .p1 = { .min = 2, .max = 4 },
501 .p2 = { .p2_slow = 1, .p2_fast = 14 },
502};
503
5ab7b0b7
ID
504static const intel_limit_t intel_limits_bxt = {
505 /* FIXME: find real dot limits */
506 .dot = { .min = 0, .max = INT_MAX },
e6292556 507 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 /* FIXME: find real m2 limits */
511 .m2 = { .min = 2 << 22, .max = 255 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 20 },
514};
515
cdba954e
ACO
516static bool
517needs_modeset(struct drm_crtc_state *state)
518{
fc596660 519 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
520}
521
e0638cdf
PZ
522/**
523 * Returns whether any output on the specified pipe is of the specified type
524 */
4093561b 525bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 526{
409ee761 527 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
528 struct intel_encoder *encoder;
529
409ee761 530 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
531 if (encoder->type == type)
532 return true;
533
534 return false;
535}
536
d0737e1d
ACO
537/**
538 * Returns whether any output on the specified pipe will have the specified
539 * type after a staged modeset is complete, i.e., the same as
540 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
541 * encoder->crtc.
542 */
a93e255f
ACO
543static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
544 int type)
d0737e1d 545{
a93e255f 546 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 547 struct drm_connector *connector;
a93e255f 548 struct drm_connector_state *connector_state;
d0737e1d 549 struct intel_encoder *encoder;
a93e255f
ACO
550 int i, num_connectors = 0;
551
da3ced29 552 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
553 if (connector_state->crtc != crtc_state->base.crtc)
554 continue;
555
556 num_connectors++;
d0737e1d 557
a93e255f
ACO
558 encoder = to_intel_encoder(connector_state->best_encoder);
559 if (encoder->type == type)
d0737e1d 560 return true;
a93e255f
ACO
561 }
562
563 WARN_ON(num_connectors == 0);
d0737e1d
ACO
564
565 return false;
566}
567
a93e255f
ACO
568static const intel_limit_t *
569intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 570{
a93e255f 571 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 572 const intel_limit_t *limit;
b91ad0ec 573
a93e255f 574 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 575 if (intel_is_dual_link_lvds(dev)) {
1b894b59 576 if (refclk == 100000)
b91ad0ec
ZW
577 limit = &intel_limits_ironlake_dual_lvds_100m;
578 else
579 limit = &intel_limits_ironlake_dual_lvds;
580 } else {
1b894b59 581 if (refclk == 100000)
b91ad0ec
ZW
582 limit = &intel_limits_ironlake_single_lvds_100m;
583 else
584 limit = &intel_limits_ironlake_single_lvds;
585 }
c6bb3538 586 } else
b91ad0ec 587 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
588
589 return limit;
590}
591
a93e255f
ACO
592static const intel_limit_t *
593intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 594{
a93e255f 595 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
596 const intel_limit_t *limit;
597
a93e255f 598 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 599 if (intel_is_dual_link_lvds(dev))
e4b36699 600 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 601 else
e4b36699 602 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
603 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
604 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 605 limit = &intel_limits_g4x_hdmi;
a93e255f 606 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 607 limit = &intel_limits_g4x_sdvo;
044c7c41 608 } else /* The option is for other outputs */
e4b36699 609 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
610
611 return limit;
612}
613
a93e255f
ACO
614static const intel_limit_t *
615intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 616{
a93e255f 617 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
618 const intel_limit_t *limit;
619
5ab7b0b7
ID
620 if (IS_BROXTON(dev))
621 limit = &intel_limits_bxt;
622 else if (HAS_PCH_SPLIT(dev))
a93e255f 623 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 624 else if (IS_G4X(dev)) {
a93e255f 625 limit = intel_g4x_limit(crtc_state);
f2b115e6 626 } else if (IS_PINEVIEW(dev)) {
a93e255f 627 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 628 limit = &intel_limits_pineview_lvds;
2177832f 629 else
f2b115e6 630 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
631 } else if (IS_CHERRYVIEW(dev)) {
632 limit = &intel_limits_chv;
a0c4da24 633 } else if (IS_VALLEYVIEW(dev)) {
dc730512 634 limit = &intel_limits_vlv;
a6c45cf0 635 } else if (!IS_GEN2(dev)) {
a93e255f 636 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
637 limit = &intel_limits_i9xx_lvds;
638 else
639 limit = &intel_limits_i9xx_sdvo;
79e53945 640 } else {
a93e255f 641 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 642 limit = &intel_limits_i8xx_lvds;
a93e255f 643 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 644 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
645 else
646 limit = &intel_limits_i8xx_dac;
79e53945
JB
647 }
648 return limit;
649}
650
dccbea3b
ID
651/*
652 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
653 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
654 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
655 * The helpers' return value is the rate of the clock that is fed to the
656 * display engine's pipe which can be the above fast dot clock rate or a
657 * divided-down version of it.
658 */
f2b115e6 659/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 660static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 661{
2177832f
SL
662 clock->m = clock->m2 + 2;
663 clock->p = clock->p1 * clock->p2;
ed5ca77e 664 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 665 return 0;
fb03ac01
VS
666 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
667 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
668
669 return clock->dot;
2177832f
SL
670}
671
7429e9d4
DV
672static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
673{
674 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
675}
676
dccbea3b 677static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 678{
7429e9d4 679 clock->m = i9xx_dpll_compute_m(clock);
79e53945 680 clock->p = clock->p1 * clock->p2;
ed5ca77e 681 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 682 return 0;
fb03ac01
VS
683 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
684 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
685
686 return clock->dot;
79e53945
JB
687}
688
dccbea3b 689static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
690{
691 clock->m = clock->m1 * clock->m2;
692 clock->p = clock->p1 * clock->p2;
693 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 694 return 0;
589eca67
ID
695 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
696 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
697
698 return clock->dot / 5;
589eca67
ID
699}
700
dccbea3b 701int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
702{
703 clock->m = clock->m1 * clock->m2;
704 clock->p = clock->p1 * clock->p2;
705 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 706 return 0;
ef9348c8
CML
707 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
708 clock->n << 22);
709 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
710
711 return clock->dot / 5;
ef9348c8
CML
712}
713
7c04d1d9 714#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
715/**
716 * Returns whether the given set of divisors are valid for a given refclk with
717 * the given connectors.
718 */
719
1b894b59
CW
720static bool intel_PLL_is_valid(struct drm_device *dev,
721 const intel_limit_t *limit,
722 const intel_clock_t *clock)
79e53945 723{
f01b7962
VS
724 if (clock->n < limit->n.min || limit->n.max < clock->n)
725 INTELPllInvalid("n out of range\n");
79e53945 726 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 727 INTELPllInvalid("p1 out of range\n");
79e53945 728 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 729 INTELPllInvalid("m2 out of range\n");
79e53945 730 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 731 INTELPllInvalid("m1 out of range\n");
f01b7962 732
666a4537
WB
733 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
734 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
735 if (clock->m1 <= clock->m2)
736 INTELPllInvalid("m1 <= m2\n");
737
666a4537 738 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
739 if (clock->p < limit->p.min || limit->p.max < clock->p)
740 INTELPllInvalid("p out of range\n");
741 if (clock->m < limit->m.min || limit->m.max < clock->m)
742 INTELPllInvalid("m out of range\n");
743 }
744
79e53945 745 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 746 INTELPllInvalid("vco out of range\n");
79e53945
JB
747 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
748 * connector, etc., rather than just a single range.
749 */
750 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 751 INTELPllInvalid("dot out of range\n");
79e53945
JB
752
753 return true;
754}
755
3b1429d9
VS
756static int
757i9xx_select_p2_div(const intel_limit_t *limit,
758 const struct intel_crtc_state *crtc_state,
759 int target)
79e53945 760{
3b1429d9 761 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 762
a93e255f 763 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 764 /*
a210b028
DV
765 * For LVDS just rely on its current settings for dual-channel.
766 * We haven't figured out how to reliably set up different
767 * single/dual channel state, if we even can.
79e53945 768 */
1974cad0 769 if (intel_is_dual_link_lvds(dev))
3b1429d9 770 return limit->p2.p2_fast;
79e53945 771 else
3b1429d9 772 return limit->p2.p2_slow;
79e53945
JB
773 } else {
774 if (target < limit->p2.dot_limit)
3b1429d9 775 return limit->p2.p2_slow;
79e53945 776 else
3b1429d9 777 return limit->p2.p2_fast;
79e53945 778 }
3b1429d9
VS
779}
780
781static bool
782i9xx_find_best_dpll(const intel_limit_t *limit,
783 struct intel_crtc_state *crtc_state,
784 int target, int refclk, intel_clock_t *match_clock,
785 intel_clock_t *best_clock)
786{
787 struct drm_device *dev = crtc_state->base.crtc->dev;
788 intel_clock_t clock;
789 int err = target;
79e53945 790
0206e353 791 memset(best_clock, 0, sizeof(*best_clock));
79e53945 792
3b1429d9
VS
793 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
794
42158660
ZY
795 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
796 clock.m1++) {
797 for (clock.m2 = limit->m2.min;
798 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 799 if (clock.m2 >= clock.m1)
42158660
ZY
800 break;
801 for (clock.n = limit->n.min;
802 clock.n <= limit->n.max; clock.n++) {
803 for (clock.p1 = limit->p1.min;
804 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
805 int this_err;
806
dccbea3b 807 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
808 if (!intel_PLL_is_valid(dev, limit,
809 &clock))
810 continue;
811 if (match_clock &&
812 clock.p != match_clock->p)
813 continue;
814
815 this_err = abs(clock.dot - target);
816 if (this_err < err) {
817 *best_clock = clock;
818 err = this_err;
819 }
820 }
821 }
822 }
823 }
824
825 return (err != target);
826}
827
828static bool
a93e255f
ACO
829pnv_find_best_dpll(const intel_limit_t *limit,
830 struct intel_crtc_state *crtc_state,
ee9300bb
DV
831 int target, int refclk, intel_clock_t *match_clock,
832 intel_clock_t *best_clock)
79e53945 833{
3b1429d9 834 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 835 intel_clock_t clock;
79e53945
JB
836 int err = target;
837
0206e353 838 memset(best_clock, 0, sizeof(*best_clock));
79e53945 839
3b1429d9
VS
840 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
841
42158660
ZY
842 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
843 clock.m1++) {
844 for (clock.m2 = limit->m2.min;
845 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
846 for (clock.n = limit->n.min;
847 clock.n <= limit->n.max; clock.n++) {
848 for (clock.p1 = limit->p1.min;
849 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
850 int this_err;
851
dccbea3b 852 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
853 if (!intel_PLL_is_valid(dev, limit,
854 &clock))
79e53945 855 continue;
cec2f356
SP
856 if (match_clock &&
857 clock.p != match_clock->p)
858 continue;
79e53945
JB
859
860 this_err = abs(clock.dot - target);
861 if (this_err < err) {
862 *best_clock = clock;
863 err = this_err;
864 }
865 }
866 }
867 }
868 }
869
870 return (err != target);
871}
872
d4906093 873static bool
a93e255f
ACO
874g4x_find_best_dpll(const intel_limit_t *limit,
875 struct intel_crtc_state *crtc_state,
ee9300bb
DV
876 int target, int refclk, intel_clock_t *match_clock,
877 intel_clock_t *best_clock)
d4906093 878{
3b1429d9 879 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
880 intel_clock_t clock;
881 int max_n;
3b1429d9 882 bool found = false;
6ba770dc
AJ
883 /* approximately equals target * 0.00585 */
884 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
885
886 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
887
888 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
889
d4906093 890 max_n = limit->n.max;
f77f13e2 891 /* based on hardware requirement, prefer smaller n to precision */
d4906093 892 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 893 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
894 for (clock.m1 = limit->m1.max;
895 clock.m1 >= limit->m1.min; clock.m1--) {
896 for (clock.m2 = limit->m2.max;
897 clock.m2 >= limit->m2.min; clock.m2--) {
898 for (clock.p1 = limit->p1.max;
899 clock.p1 >= limit->p1.min; clock.p1--) {
900 int this_err;
901
dccbea3b 902 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
903 if (!intel_PLL_is_valid(dev, limit,
904 &clock))
d4906093 905 continue;
1b894b59
CW
906
907 this_err = abs(clock.dot - target);
d4906093
ML
908 if (this_err < err_most) {
909 *best_clock = clock;
910 err_most = this_err;
911 max_n = clock.n;
912 found = true;
913 }
914 }
915 }
916 }
917 }
2c07245f
ZW
918 return found;
919}
920
d5dd62bd
ID
921/*
922 * Check if the calculated PLL configuration is more optimal compared to the
923 * best configuration and error found so far. Return the calculated error.
924 */
925static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
926 const intel_clock_t *calculated_clock,
927 const intel_clock_t *best_clock,
928 unsigned int best_error_ppm,
929 unsigned int *error_ppm)
930{
9ca3ba01
ID
931 /*
932 * For CHV ignore the error and consider only the P value.
933 * Prefer a bigger P value based on HW requirements.
934 */
935 if (IS_CHERRYVIEW(dev)) {
936 *error_ppm = 0;
937
938 return calculated_clock->p > best_clock->p;
939 }
940
24be4e46
ID
941 if (WARN_ON_ONCE(!target_freq))
942 return false;
943
d5dd62bd
ID
944 *error_ppm = div_u64(1000000ULL *
945 abs(target_freq - calculated_clock->dot),
946 target_freq);
947 /*
948 * Prefer a better P value over a better (smaller) error if the error
949 * is small. Ensure this preference for future configurations too by
950 * setting the error to 0.
951 */
952 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
953 *error_ppm = 0;
954
955 return true;
956 }
957
958 return *error_ppm + 10 < best_error_ppm;
959}
960
a0c4da24 961static bool
a93e255f
ACO
962vlv_find_best_dpll(const intel_limit_t *limit,
963 struct intel_crtc_state *crtc_state,
ee9300bb
DV
964 int target, int refclk, intel_clock_t *match_clock,
965 intel_clock_t *best_clock)
a0c4da24 966{
a93e255f 967 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 968 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 969 intel_clock_t clock;
69e4f900 970 unsigned int bestppm = 1000000;
27e639bf
VS
971 /* min update 19.2 MHz */
972 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 973 bool found = false;
a0c4da24 974
6b4bf1c4
VS
975 target *= 5; /* fast clock */
976
977 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
978
979 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 980 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 981 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 982 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 983 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 984 clock.p = clock.p1 * clock.p2;
a0c4da24 985 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 986 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 987 unsigned int ppm;
69e4f900 988
6b4bf1c4
VS
989 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
990 refclk * clock.m1);
991
dccbea3b 992 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 993
f01b7962
VS
994 if (!intel_PLL_is_valid(dev, limit,
995 &clock))
43b0ac53
VS
996 continue;
997
d5dd62bd
ID
998 if (!vlv_PLL_is_optimal(dev, target,
999 &clock,
1000 best_clock,
1001 bestppm, &ppm))
1002 continue;
6b4bf1c4 1003
d5dd62bd
ID
1004 *best_clock = clock;
1005 bestppm = ppm;
1006 found = true;
a0c4da24
JB
1007 }
1008 }
1009 }
1010 }
a0c4da24 1011
49e497ef 1012 return found;
a0c4da24 1013}
a4fc5ed6 1014
ef9348c8 1015static bool
a93e255f
ACO
1016chv_find_best_dpll(const intel_limit_t *limit,
1017 struct intel_crtc_state *crtc_state,
ef9348c8
CML
1018 int target, int refclk, intel_clock_t *match_clock,
1019 intel_clock_t *best_clock)
1020{
a93e255f 1021 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 1022 struct drm_device *dev = crtc->base.dev;
9ca3ba01 1023 unsigned int best_error_ppm;
ef9348c8
CML
1024 intel_clock_t clock;
1025 uint64_t m2;
1026 int found = false;
1027
1028 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 1029 best_error_ppm = 1000000;
ef9348c8
CML
1030
1031 /*
1032 * Based on hardware doc, the n always set to 1, and m1 always
1033 * set to 2. If requires to support 200Mhz refclk, we need to
1034 * revisit this because n may not 1 anymore.
1035 */
1036 clock.n = 1, clock.m1 = 2;
1037 target *= 5; /* fast clock */
1038
1039 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1040 for (clock.p2 = limit->p2.p2_fast;
1041 clock.p2 >= limit->p2.p2_slow;
1042 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1043 unsigned int error_ppm;
ef9348c8
CML
1044
1045 clock.p = clock.p1 * clock.p2;
1046
1047 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1048 clock.n) << 22, refclk * clock.m1);
1049
1050 if (m2 > INT_MAX/clock.m1)
1051 continue;
1052
1053 clock.m2 = m2;
1054
dccbea3b 1055 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1056
1057 if (!intel_PLL_is_valid(dev, limit, &clock))
1058 continue;
1059
9ca3ba01
ID
1060 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1061 best_error_ppm, &error_ppm))
1062 continue;
1063
1064 *best_clock = clock;
1065 best_error_ppm = error_ppm;
1066 found = true;
ef9348c8
CML
1067 }
1068 }
1069
1070 return found;
1071}
1072
5ab7b0b7
ID
1073bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1074 intel_clock_t *best_clock)
1075{
1076 int refclk = i9xx_get_refclk(crtc_state, 0);
1077
1078 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1079 target_clock, refclk, NULL, best_clock);
1080}
1081
20ddf665
VS
1082bool intel_crtc_active(struct drm_crtc *crtc)
1083{
1084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1085
1086 /* Be paranoid as we can arrive here with only partial
1087 * state retrieved from the hardware during setup.
1088 *
241bfc38 1089 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1090 * as Haswell has gained clock readout/fastboot support.
1091 *
66e514c1 1092 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1093 * properly reconstruct framebuffers.
c3d1f436
MR
1094 *
1095 * FIXME: The intel_crtc->active here should be switched to
1096 * crtc->state->active once we have proper CRTC states wired up
1097 * for atomic.
20ddf665 1098 */
c3d1f436 1099 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1100 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1101}
1102
a5c961d1
PZ
1103enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1104 enum pipe pipe)
1105{
1106 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1108
6e3c9717 1109 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1110}
1111
fbf49ea2
VS
1112static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1113{
1114 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1115 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1116 u32 line1, line2;
1117 u32 line_mask;
1118
1119 if (IS_GEN2(dev))
1120 line_mask = DSL_LINEMASK_GEN2;
1121 else
1122 line_mask = DSL_LINEMASK_GEN3;
1123
1124 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1125 msleep(5);
fbf49ea2
VS
1126 line2 = I915_READ(reg) & line_mask;
1127
1128 return line1 == line2;
1129}
1130
ab7ad7f6
KP
1131/*
1132 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1133 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1134 *
1135 * After disabling a pipe, we can't wait for vblank in the usual way,
1136 * spinning on the vblank interrupt status bit, since we won't actually
1137 * see an interrupt when the pipe is disabled.
1138 *
ab7ad7f6
KP
1139 * On Gen4 and above:
1140 * wait for the pipe register state bit to turn off
1141 *
1142 * Otherwise:
1143 * wait for the display line value to settle (it usually
1144 * ends up stopping at the start of the next frame).
58e10eb9 1145 *
9d0498a2 1146 */
575f7ab7 1147static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1148{
575f7ab7 1149 struct drm_device *dev = crtc->base.dev;
9d0498a2 1150 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1151 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1152 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1153
1154 if (INTEL_INFO(dev)->gen >= 4) {
f0f59a00 1155 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1156
1157 /* Wait for the Pipe State to go off */
58e10eb9
CW
1158 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1159 100))
284637d9 1160 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1161 } else {
ab7ad7f6 1162 /* Wait for the display line to settle */
fbf49ea2 1163 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1164 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1165 }
79e53945
JB
1166}
1167
b24e7179 1168/* Only for pre-ILK configs */
55607e8a
DV
1169void assert_pll(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
b24e7179 1171{
b24e7179
JB
1172 u32 val;
1173 bool cur_state;
1174
649636ef 1175 val = I915_READ(DPLL(pipe));
b24e7179 1176 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1177 I915_STATE_WARN(cur_state != state,
b24e7179 1178 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1179 onoff(state), onoff(cur_state));
b24e7179 1180}
b24e7179 1181
23538ef1
JN
1182/* XXX: the dsi pll is shared between MIPI DSI ports */
1183static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1184{
1185 u32 val;
1186 bool cur_state;
1187
a580516d 1188 mutex_lock(&dev_priv->sb_lock);
23538ef1 1189 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1190 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1191
1192 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1193 I915_STATE_WARN(cur_state != state,
23538ef1 1194 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1195 onoff(state), onoff(cur_state));
23538ef1
JN
1196}
1197#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1198#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1199
040484af
JB
1200static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1201 enum pipe pipe, bool state)
1202{
040484af 1203 bool cur_state;
ad80a810
PZ
1204 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1205 pipe);
040484af 1206
affa9354
PZ
1207 if (HAS_DDI(dev_priv->dev)) {
1208 /* DDI does not have a specific FDI_TX register */
649636ef 1209 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1210 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1211 } else {
649636ef 1212 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1213 cur_state = !!(val & FDI_TX_ENABLE);
1214 }
e2c719b7 1215 I915_STATE_WARN(cur_state != state,
040484af 1216 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1217 onoff(state), onoff(cur_state));
040484af
JB
1218}
1219#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1220#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1221
1222static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1223 enum pipe pipe, bool state)
1224{
040484af
JB
1225 u32 val;
1226 bool cur_state;
1227
649636ef 1228 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1229 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1230 I915_STATE_WARN(cur_state != state,
040484af 1231 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1232 onoff(state), onoff(cur_state));
040484af
JB
1233}
1234#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1235#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1236
1237static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1238 enum pipe pipe)
1239{
040484af
JB
1240 u32 val;
1241
1242 /* ILK FDI PLL is always enabled */
3d13ef2e 1243 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1244 return;
1245
bf507ef7 1246 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1247 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1248 return;
1249
649636ef 1250 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1251 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1252}
1253
55607e8a
DV
1254void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1255 enum pipe pipe, bool state)
040484af 1256{
040484af 1257 u32 val;
55607e8a 1258 bool cur_state;
040484af 1259
649636ef 1260 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1261 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1262 I915_STATE_WARN(cur_state != state,
55607e8a 1263 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1264 onoff(state), onoff(cur_state));
040484af
JB
1265}
1266
b680c37a
DV
1267void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1268 enum pipe pipe)
ea0760cf 1269{
bedd4dba 1270 struct drm_device *dev = dev_priv->dev;
f0f59a00 1271 i915_reg_t pp_reg;
ea0760cf
JB
1272 u32 val;
1273 enum pipe panel_pipe = PIPE_A;
0de3b485 1274 bool locked = true;
ea0760cf 1275
bedd4dba
JN
1276 if (WARN_ON(HAS_DDI(dev)))
1277 return;
1278
1279 if (HAS_PCH_SPLIT(dev)) {
1280 u32 port_sel;
1281
ea0760cf 1282 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1283 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1284
1285 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1286 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1287 panel_pipe = PIPE_B;
1288 /* XXX: else fix for eDP */
666a4537 1289 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
bedd4dba
JN
1290 /* presumably write lock depends on pipe, not port select */
1291 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1292 panel_pipe = pipe;
ea0760cf
JB
1293 } else {
1294 pp_reg = PP_CONTROL;
bedd4dba
JN
1295 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1296 panel_pipe = PIPE_B;
ea0760cf
JB
1297 }
1298
1299 val = I915_READ(pp_reg);
1300 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1301 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1302 locked = false;
1303
e2c719b7 1304 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1305 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1306 pipe_name(pipe));
ea0760cf
JB
1307}
1308
93ce0ba6
JN
1309static void assert_cursor(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, bool state)
1311{
1312 struct drm_device *dev = dev_priv->dev;
1313 bool cur_state;
1314
d9d82081 1315 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1316 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1317 else
5efb3e28 1318 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1319
e2c719b7 1320 I915_STATE_WARN(cur_state != state,
93ce0ba6 1321 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1322 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1323}
1324#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1325#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1326
b840d907
JB
1327void assert_pipe(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, bool state)
b24e7179 1329{
63d7bbe9 1330 bool cur_state;
702e7a56
PZ
1331 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1332 pipe);
4feed0eb 1333 enum intel_display_power_domain power_domain;
b24e7179 1334
b6b5d049
VS
1335 /* if we need the pipe quirk it must be always on */
1336 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1337 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1338 state = true;
1339
4feed0eb
ID
1340 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1341 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1342 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1343 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1344
1345 intel_display_power_put(dev_priv, power_domain);
1346 } else {
1347 cur_state = false;
69310161
PZ
1348 }
1349
e2c719b7 1350 I915_STATE_WARN(cur_state != state,
63d7bbe9 1351 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1352 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1353}
1354
931872fc
CW
1355static void assert_plane(struct drm_i915_private *dev_priv,
1356 enum plane plane, bool state)
b24e7179 1357{
b24e7179 1358 u32 val;
931872fc 1359 bool cur_state;
b24e7179 1360
649636ef 1361 val = I915_READ(DSPCNTR(plane));
931872fc 1362 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1363 I915_STATE_WARN(cur_state != state,
931872fc 1364 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1365 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1366}
1367
931872fc
CW
1368#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1369#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1370
b24e7179
JB
1371static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe)
1373{
653e1026 1374 struct drm_device *dev = dev_priv->dev;
649636ef 1375 int i;
b24e7179 1376
653e1026
VS
1377 /* Primary planes are fixed to pipes on gen4+ */
1378 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1379 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1380 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1381 "plane %c assertion failure, should be disabled but not\n",
1382 plane_name(pipe));
19ec1358 1383 return;
28c05794 1384 }
19ec1358 1385
b24e7179 1386 /* Need to check both planes against the pipe */
055e393f 1387 for_each_pipe(dev_priv, i) {
649636ef
VS
1388 u32 val = I915_READ(DSPCNTR(i));
1389 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1390 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1391 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1392 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(i), pipe_name(pipe));
b24e7179
JB
1394 }
1395}
1396
19332d7a
JB
1397static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
1399{
20674eef 1400 struct drm_device *dev = dev_priv->dev;
649636ef 1401 int sprite;
19332d7a 1402
7feb8b88 1403 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1404 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1405 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1406 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1407 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1408 sprite, pipe_name(pipe));
1409 }
666a4537 1410 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
3bdcfc0c 1411 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1412 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1413 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1415 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1416 }
1417 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1418 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1419 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1420 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1421 plane_name(pipe), pipe_name(pipe));
1422 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1423 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1424 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1425 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1426 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1427 }
1428}
1429
08c71e5e
VS
1430static void assert_vblank_disabled(struct drm_crtc *crtc)
1431{
e2c719b7 1432 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1433 drm_crtc_vblank_put(crtc);
1434}
1435
7abd4b35
ACO
1436void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1437 enum pipe pipe)
92f2584a 1438{
92f2584a
JB
1439 u32 val;
1440 bool enabled;
1441
649636ef 1442 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1443 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1444 I915_STATE_WARN(enabled,
9db4a9c7
JB
1445 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1446 pipe_name(pipe));
92f2584a
JB
1447}
1448
4e634389
KP
1449static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1450 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1451{
1452 if ((val & DP_PORT_EN) == 0)
1453 return false;
1454
1455 if (HAS_PCH_CPT(dev_priv->dev)) {
f0f59a00 1456 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1457 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1458 return false;
44f37d1f
CML
1459 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1460 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1461 return false;
f0575e92
KP
1462 } else {
1463 if ((val & DP_PIPE_MASK) != (pipe << 30))
1464 return false;
1465 }
1466 return true;
1467}
1468
1519b995
KP
1469static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe, u32 val)
1471{
dc0fa718 1472 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1473 return false;
1474
1475 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1476 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1477 return false;
44f37d1f
CML
1478 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1479 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1480 return false;
1519b995 1481 } else {
dc0fa718 1482 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1483 return false;
1484 }
1485 return true;
1486}
1487
1488static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1489 enum pipe pipe, u32 val)
1490{
1491 if ((val & LVDS_PORT_EN) == 0)
1492 return false;
1493
1494 if (HAS_PCH_CPT(dev_priv->dev)) {
1495 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1496 return false;
1497 } else {
1498 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1499 return false;
1500 }
1501 return true;
1502}
1503
1504static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1505 enum pipe pipe, u32 val)
1506{
1507 if ((val & ADPA_DAC_ENABLE) == 0)
1508 return false;
1509 if (HAS_PCH_CPT(dev_priv->dev)) {
1510 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1511 return false;
1512 } else {
1513 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1514 return false;
1515 }
1516 return true;
1517}
1518
291906f1 1519static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1520 enum pipe pipe, i915_reg_t reg,
1521 u32 port_sel)
291906f1 1522{
47a05eca 1523 u32 val = I915_READ(reg);
e2c719b7 1524 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1525 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1526 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1527
e2c719b7 1528 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1529 && (val & DP_PIPEB_SELECT),
de9a35ab 1530 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1531}
1532
1533static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1534 enum pipe pipe, i915_reg_t reg)
291906f1 1535{
47a05eca 1536 u32 val = I915_READ(reg);
e2c719b7 1537 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1538 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1539 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1540
e2c719b7 1541 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1542 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1543 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1544}
1545
1546static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1547 enum pipe pipe)
1548{
291906f1 1549 u32 val;
291906f1 1550
f0575e92
KP
1551 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1552 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1553 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1554
649636ef 1555 val = I915_READ(PCH_ADPA);
e2c719b7 1556 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1557 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1558 pipe_name(pipe));
291906f1 1559
649636ef 1560 val = I915_READ(PCH_LVDS);
e2c719b7 1561 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1562 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1563 pipe_name(pipe));
291906f1 1564
e2debe91
PZ
1565 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1566 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1567 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1568}
1569
d288f65f 1570static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1571 const struct intel_crtc_state *pipe_config)
87442f73 1572{
426115cf
DV
1573 struct drm_device *dev = crtc->base.dev;
1574 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1575 i915_reg_t reg = DPLL(crtc->pipe);
d288f65f 1576 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1577
426115cf 1578 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73 1579
87442f73 1580 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1581 if (IS_MOBILE(dev_priv->dev))
426115cf 1582 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1583
426115cf
DV
1584 I915_WRITE(reg, dpll);
1585 POSTING_READ(reg);
1586 udelay(150);
1587
1588 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1589 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1590
d288f65f 1591 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1592 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1593
1594 /* We do this three times for luck */
426115cf 1595 I915_WRITE(reg, dpll);
87442f73
DV
1596 POSTING_READ(reg);
1597 udelay(150); /* wait for warmup */
426115cf 1598 I915_WRITE(reg, dpll);
87442f73
DV
1599 POSTING_READ(reg);
1600 udelay(150); /* wait for warmup */
426115cf 1601 I915_WRITE(reg, dpll);
87442f73
DV
1602 POSTING_READ(reg);
1603 udelay(150); /* wait for warmup */
1604}
1605
d288f65f 1606static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1607 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1608{
1609 struct drm_device *dev = crtc->base.dev;
1610 struct drm_i915_private *dev_priv = dev->dev_private;
1611 int pipe = crtc->pipe;
1612 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1613 u32 tmp;
1614
1615 assert_pipe_disabled(dev_priv, crtc->pipe);
1616
a580516d 1617 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1618
1619 /* Enable back the 10bit clock to display controller */
1620 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1621 tmp |= DPIO_DCLKP_EN;
1622 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1623
54433e91
VS
1624 mutex_unlock(&dev_priv->sb_lock);
1625
9d556c99
CML
1626 /*
1627 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1628 */
1629 udelay(1);
1630
1631 /* Enable PLL */
d288f65f 1632 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1633
1634 /* Check PLL is locked */
a11b0703 1635 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1636 DRM_ERROR("PLL %d failed to lock\n", pipe);
1637
a11b0703 1638 /* not sure when this should be written */
d288f65f 1639 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1640 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1641}
1642
1c4e0274
VS
1643static int intel_num_dvo_pipes(struct drm_device *dev)
1644{
1645 struct intel_crtc *crtc;
1646 int count = 0;
1647
1648 for_each_intel_crtc(dev, crtc)
3538b9df 1649 count += crtc->base.state->active &&
409ee761 1650 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1651
1652 return count;
1653}
1654
66e3d5c0 1655static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1656{
66e3d5c0
DV
1657 struct drm_device *dev = crtc->base.dev;
1658 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 1659 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1660 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1661
66e3d5c0 1662 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1663
63d7bbe9 1664 /* No really, not for ILK+ */
3d13ef2e 1665 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1666
1667 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1668 if (IS_MOBILE(dev) && !IS_I830(dev))
1669 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1670
1c4e0274
VS
1671 /* Enable DVO 2x clock on both PLLs if necessary */
1672 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1673 /*
1674 * It appears to be important that we don't enable this
1675 * for the current pipe before otherwise configuring the
1676 * PLL. No idea how this should be handled if multiple
1677 * DVO outputs are enabled simultaneosly.
1678 */
1679 dpll |= DPLL_DVO_2X_MODE;
1680 I915_WRITE(DPLL(!crtc->pipe),
1681 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1682 }
66e3d5c0 1683
c2b63374
VS
1684 /*
1685 * Apparently we need to have VGA mode enabled prior to changing
1686 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1687 * dividers, even though the register value does change.
1688 */
1689 I915_WRITE(reg, 0);
1690
8e7a65aa
VS
1691 I915_WRITE(reg, dpll);
1692
66e3d5c0
DV
1693 /* Wait for the clocks to stabilize. */
1694 POSTING_READ(reg);
1695 udelay(150);
1696
1697 if (INTEL_INFO(dev)->gen >= 4) {
1698 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1699 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1700 } else {
1701 /* The pixel multiplier can only be updated once the
1702 * DPLL is enabled and the clocks are stable.
1703 *
1704 * So write it again.
1705 */
1706 I915_WRITE(reg, dpll);
1707 }
63d7bbe9
JB
1708
1709 /* We do this three times for luck */
66e3d5c0 1710 I915_WRITE(reg, dpll);
63d7bbe9
JB
1711 POSTING_READ(reg);
1712 udelay(150); /* wait for warmup */
66e3d5c0 1713 I915_WRITE(reg, dpll);
63d7bbe9
JB
1714 POSTING_READ(reg);
1715 udelay(150); /* wait for warmup */
66e3d5c0 1716 I915_WRITE(reg, dpll);
63d7bbe9
JB
1717 POSTING_READ(reg);
1718 udelay(150); /* wait for warmup */
1719}
1720
1721/**
50b44a44 1722 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1723 * @dev_priv: i915 private structure
1724 * @pipe: pipe PLL to disable
1725 *
1726 * Disable the PLL for @pipe, making sure the pipe is off first.
1727 *
1728 * Note! This is for pre-ILK only.
1729 */
1c4e0274 1730static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1731{
1c4e0274
VS
1732 struct drm_device *dev = crtc->base.dev;
1733 struct drm_i915_private *dev_priv = dev->dev_private;
1734 enum pipe pipe = crtc->pipe;
1735
1736 /* Disable DVO 2x clock on both PLLs if necessary */
1737 if (IS_I830(dev) &&
409ee761 1738 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1739 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1740 I915_WRITE(DPLL(PIPE_B),
1741 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1742 I915_WRITE(DPLL(PIPE_A),
1743 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1744 }
1745
b6b5d049
VS
1746 /* Don't disable pipe or pipe PLLs if needed */
1747 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1748 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1749 return;
1750
1751 /* Make sure the pipe isn't still relying on us */
1752 assert_pipe_disabled(dev_priv, pipe);
1753
b8afb911 1754 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1755 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1756}
1757
f6071166
JB
1758static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1759{
b8afb911 1760 u32 val;
f6071166
JB
1761
1762 /* Make sure the pipe isn't still relying on us */
1763 assert_pipe_disabled(dev_priv, pipe);
1764
e5cbfbfb
ID
1765 /*
1766 * Leave integrated clock source and reference clock enabled for pipe B.
1767 * The latter is needed for VGA hotplug / manual detection.
1768 */
b8afb911 1769 val = DPLL_VGA_MODE_DIS;
f6071166 1770 if (pipe == PIPE_B)
60bfe44f 1771 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1772 I915_WRITE(DPLL(pipe), val);
1773 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1774
1775}
1776
1777static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1778{
d752048d 1779 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1780 u32 val;
1781
a11b0703
VS
1782 /* Make sure the pipe isn't still relying on us */
1783 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1784
a11b0703 1785 /* Set PLL en = 0 */
60bfe44f
VS
1786 val = DPLL_SSC_REF_CLK_CHV |
1787 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1788 if (pipe != PIPE_A)
1789 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1790 I915_WRITE(DPLL(pipe), val);
1791 POSTING_READ(DPLL(pipe));
d752048d 1792
a580516d 1793 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1794
1795 /* Disable 10bit clock to display controller */
1796 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1797 val &= ~DPIO_DCLKP_EN;
1798 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1799
a580516d 1800 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1801}
1802
e4607fcf 1803void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1804 struct intel_digital_port *dport,
1805 unsigned int expected_mask)
89b667f8
JB
1806{
1807 u32 port_mask;
f0f59a00 1808 i915_reg_t dpll_reg;
89b667f8 1809
e4607fcf
CML
1810 switch (dport->port) {
1811 case PORT_B:
89b667f8 1812 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1813 dpll_reg = DPLL(0);
e4607fcf
CML
1814 break;
1815 case PORT_C:
89b667f8 1816 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1817 dpll_reg = DPLL(0);
9b6de0a1 1818 expected_mask <<= 4;
00fc31b7
CML
1819 break;
1820 case PORT_D:
1821 port_mask = DPLL_PORTD_READY_MASK;
1822 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1823 break;
1824 default:
1825 BUG();
1826 }
89b667f8 1827
9b6de0a1
VS
1828 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1829 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1830 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1831}
1832
b8a4f404
PZ
1833static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1834 enum pipe pipe)
040484af 1835{
23670b32 1836 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1837 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
f0f59a00
VS
1839 i915_reg_t reg;
1840 uint32_t val, pipeconf_val;
040484af
JB
1841
1842 /* PCH only available on ILK+ */
55522f37 1843 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1844
1845 /* Make sure PCH DPLL is enabled */
8106ddbd 1846 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1847
1848 /* FDI must be feeding us bits for PCH ports */
1849 assert_fdi_tx_enabled(dev_priv, pipe);
1850 assert_fdi_rx_enabled(dev_priv, pipe);
1851
23670b32
DV
1852 if (HAS_PCH_CPT(dev)) {
1853 /* Workaround: Set the timing override bit before enabling the
1854 * pch transcoder. */
1855 reg = TRANS_CHICKEN2(pipe);
1856 val = I915_READ(reg);
1857 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1858 I915_WRITE(reg, val);
59c859d6 1859 }
23670b32 1860
ab9412ba 1861 reg = PCH_TRANSCONF(pipe);
040484af 1862 val = I915_READ(reg);
5f7f726d 1863 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1864
1865 if (HAS_PCH_IBX(dev_priv->dev)) {
1866 /*
c5de7c6f
VS
1867 * Make the BPC in transcoder be consistent with
1868 * that in pipeconf reg. For HDMI we must use 8bpc
1869 * here for both 8bpc and 12bpc.
e9bcff5c 1870 */
dfd07d72 1871 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1872 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1873 val |= PIPECONF_8BPC;
1874 else
1875 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1876 }
5f7f726d
PZ
1877
1878 val &= ~TRANS_INTERLACE_MASK;
1879 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1880 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1881 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1882 val |= TRANS_LEGACY_INTERLACED_ILK;
1883 else
1884 val |= TRANS_INTERLACED;
5f7f726d
PZ
1885 else
1886 val |= TRANS_PROGRESSIVE;
1887
040484af
JB
1888 I915_WRITE(reg, val | TRANS_ENABLE);
1889 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1890 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1891}
1892
8fb033d7 1893static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1894 enum transcoder cpu_transcoder)
040484af 1895{
8fb033d7 1896 u32 val, pipeconf_val;
8fb033d7
PZ
1897
1898 /* PCH only available on ILK+ */
55522f37 1899 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1900
8fb033d7 1901 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1902 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1903 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1904
223a6fdf 1905 /* Workaround: set timing override bit. */
36c0d0cf 1906 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1907 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1908 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1909
25f3ef11 1910 val = TRANS_ENABLE;
937bb610 1911 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1912
9a76b1c6
PZ
1913 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1914 PIPECONF_INTERLACED_ILK)
a35f2679 1915 val |= TRANS_INTERLACED;
8fb033d7
PZ
1916 else
1917 val |= TRANS_PROGRESSIVE;
1918
ab9412ba
DV
1919 I915_WRITE(LPT_TRANSCONF, val);
1920 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1921 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1922}
1923
b8a4f404
PZ
1924static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1925 enum pipe pipe)
040484af 1926{
23670b32 1927 struct drm_device *dev = dev_priv->dev;
f0f59a00
VS
1928 i915_reg_t reg;
1929 uint32_t val;
040484af
JB
1930
1931 /* FDI relies on the transcoder */
1932 assert_fdi_tx_disabled(dev_priv, pipe);
1933 assert_fdi_rx_disabled(dev_priv, pipe);
1934
291906f1
JB
1935 /* Ports must be off as well */
1936 assert_pch_ports_disabled(dev_priv, pipe);
1937
ab9412ba 1938 reg = PCH_TRANSCONF(pipe);
040484af
JB
1939 val = I915_READ(reg);
1940 val &= ~TRANS_ENABLE;
1941 I915_WRITE(reg, val);
1942 /* wait for PCH transcoder off, transcoder state */
1943 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1944 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1945
c465613b 1946 if (HAS_PCH_CPT(dev)) {
23670b32
DV
1947 /* Workaround: Clear the timing override chicken bit again. */
1948 reg = TRANS_CHICKEN2(pipe);
1949 val = I915_READ(reg);
1950 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1951 I915_WRITE(reg, val);
1952 }
040484af
JB
1953}
1954
ab4d966c 1955static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1956{
8fb033d7
PZ
1957 u32 val;
1958
ab9412ba 1959 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1960 val &= ~TRANS_ENABLE;
ab9412ba 1961 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1962 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1963 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1964 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1965
1966 /* Workaround: clear timing override bit. */
36c0d0cf 1967 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1968 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1969 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1970}
1971
b24e7179 1972/**
309cfea8 1973 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1974 * @crtc: crtc responsible for the pipe
b24e7179 1975 *
0372264a 1976 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1977 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1978 */
e1fdc473 1979static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1980{
0372264a
PZ
1981 struct drm_device *dev = crtc->base.dev;
1982 struct drm_i915_private *dev_priv = dev->dev_private;
1983 enum pipe pipe = crtc->pipe;
1a70a728 1984 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 1985 enum pipe pch_transcoder;
f0f59a00 1986 i915_reg_t reg;
b24e7179
JB
1987 u32 val;
1988
9e2ee2dd
VS
1989 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1990
58c6eaa2 1991 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1992 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1993 assert_sprites_disabled(dev_priv, pipe);
1994
681e5811 1995 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1996 pch_transcoder = TRANSCODER_A;
1997 else
1998 pch_transcoder = pipe;
1999
b24e7179
JB
2000 /*
2001 * A pipe without a PLL won't actually be able to drive bits from
2002 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2003 * need the check.
2004 */
50360403 2005 if (HAS_GMCH_DISPLAY(dev_priv->dev))
a65347ba 2006 if (crtc->config->has_dsi_encoder)
23538ef1
JN
2007 assert_dsi_pll_enabled(dev_priv);
2008 else
2009 assert_pll_enabled(dev_priv, pipe);
040484af 2010 else {
6e3c9717 2011 if (crtc->config->has_pch_encoder) {
040484af 2012 /* if driving the PCH, we need FDI enabled */
cc391bbb 2013 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2014 assert_fdi_tx_pll_enabled(dev_priv,
2015 (enum pipe) cpu_transcoder);
040484af
JB
2016 }
2017 /* FIXME: assert CPU port conditions for SNB+ */
2018 }
b24e7179 2019
702e7a56 2020 reg = PIPECONF(cpu_transcoder);
b24e7179 2021 val = I915_READ(reg);
7ad25d48 2022 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2023 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2024 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2025 return;
7ad25d48 2026 }
00d70b15
CW
2027
2028 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2029 POSTING_READ(reg);
b7792d8b
VS
2030
2031 /*
2032 * Until the pipe starts DSL will read as 0, which would cause
2033 * an apparent vblank timestamp jump, which messes up also the
2034 * frame count when it's derived from the timestamps. So let's
2035 * wait for the pipe to start properly before we call
2036 * drm_crtc_vblank_on()
2037 */
2038 if (dev->max_vblank_count == 0 &&
2039 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2040 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
2041}
2042
2043/**
309cfea8 2044 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2045 * @crtc: crtc whose pipes is to be disabled
b24e7179 2046 *
575f7ab7
VS
2047 * Disable the pipe of @crtc, making sure that various hardware
2048 * specific requirements are met, if applicable, e.g. plane
2049 * disabled, panel fitter off, etc.
b24e7179
JB
2050 *
2051 * Will wait until the pipe has shut down before returning.
2052 */
575f7ab7 2053static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2054{
575f7ab7 2055 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2056 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2057 enum pipe pipe = crtc->pipe;
f0f59a00 2058 i915_reg_t reg;
b24e7179
JB
2059 u32 val;
2060
9e2ee2dd
VS
2061 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2062
b24e7179
JB
2063 /*
2064 * Make sure planes won't keep trying to pump pixels to us,
2065 * or we might hang the display.
2066 */
2067 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2068 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2069 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2070
702e7a56 2071 reg = PIPECONF(cpu_transcoder);
b24e7179 2072 val = I915_READ(reg);
00d70b15
CW
2073 if ((val & PIPECONF_ENABLE) == 0)
2074 return;
2075
67adc644
VS
2076 /*
2077 * Double wide has implications for planes
2078 * so best keep it disabled when not needed.
2079 */
6e3c9717 2080 if (crtc->config->double_wide)
67adc644
VS
2081 val &= ~PIPECONF_DOUBLE_WIDE;
2082
2083 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2084 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2085 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2086 val &= ~PIPECONF_ENABLE;
2087
2088 I915_WRITE(reg, val);
2089 if ((val & PIPECONF_ENABLE) == 0)
2090 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2091}
2092
693db184
CW
2093static bool need_vtd_wa(struct drm_device *dev)
2094{
2095#ifdef CONFIG_INTEL_IOMMU
2096 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2097 return true;
2098#endif
2099 return false;
2100}
2101
832be82f
VS
2102static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2103{
2104 return IS_GEN2(dev_priv) ? 2048 : 4096;
2105}
2106
27ba3910
VS
2107static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2108 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2109{
2110 switch (fb_modifier) {
2111 case DRM_FORMAT_MOD_NONE:
2112 return cpp;
2113 case I915_FORMAT_MOD_X_TILED:
2114 if (IS_GEN2(dev_priv))
2115 return 128;
2116 else
2117 return 512;
2118 case I915_FORMAT_MOD_Y_TILED:
2119 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2120 return 128;
2121 else
2122 return 512;
2123 case I915_FORMAT_MOD_Yf_TILED:
2124 switch (cpp) {
2125 case 1:
2126 return 64;
2127 case 2:
2128 case 4:
2129 return 128;
2130 case 8:
2131 case 16:
2132 return 256;
2133 default:
2134 MISSING_CASE(cpp);
2135 return cpp;
2136 }
2137 break;
2138 default:
2139 MISSING_CASE(fb_modifier);
2140 return cpp;
2141 }
2142}
2143
832be82f
VS
2144unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2145 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2146{
832be82f
VS
2147 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2148 return 1;
2149 else
2150 return intel_tile_size(dev_priv) /
27ba3910 2151 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2152}
2153
8d0deca8
VS
2154/* Return the tile dimensions in pixel units */
2155static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2156 unsigned int *tile_width,
2157 unsigned int *tile_height,
2158 uint64_t fb_modifier,
2159 unsigned int cpp)
2160{
2161 unsigned int tile_width_bytes =
2162 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2163
2164 *tile_width = tile_width_bytes / cpp;
2165 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2166}
2167
6761dd31
TU
2168unsigned int
2169intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2170 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2171{
832be82f
VS
2172 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2173 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2174
2175 return ALIGN(height, tile_height);
a57ce0b2
JB
2176}
2177
1663b9d6
VS
2178unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2179{
2180 unsigned int size = 0;
2181 int i;
2182
2183 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2184 size += rot_info->plane[i].width * rot_info->plane[i].height;
2185
2186 return size;
2187}
2188
75c82a53 2189static void
3465c580
VS
2190intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2191 const struct drm_framebuffer *fb,
2192 unsigned int rotation)
f64b98cd 2193{
2d7a215f
VS
2194 if (intel_rotation_90_or_270(rotation)) {
2195 *view = i915_ggtt_view_rotated;
2196 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2197 } else {
2198 *view = i915_ggtt_view_normal;
2199 }
2200}
50470bb0 2201
2d7a215f
VS
2202static void
2203intel_fill_fb_info(struct drm_i915_private *dev_priv,
2204 struct drm_framebuffer *fb)
2205{
2206 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
2207 unsigned int tile_size, tile_width, tile_height, cpp;
50470bb0 2208
d9b3288e
VS
2209 tile_size = intel_tile_size(dev_priv);
2210
2211 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8d0deca8
VS
2212 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2213 fb->modifier[0], cpp);
d9b3288e 2214
1663b9d6
VS
2215 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2216 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
84fe03f7 2217
89e3e142 2218 if (info->pixel_format == DRM_FORMAT_NV12) {
832be82f 2219 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
8d0deca8
VS
2220 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2221 fb->modifier[1], cpp);
d9b3288e 2222
2d7a215f 2223 info->uv_offset = fb->offsets[1];
1663b9d6
VS
2224 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2225 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
89e3e142 2226 }
f64b98cd
TU
2227}
2228
603525d7 2229static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2230{
2231 if (INTEL_INFO(dev_priv)->gen >= 9)
2232 return 256 * 1024;
985b8bb4 2233 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2234 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2235 return 128 * 1024;
2236 else if (INTEL_INFO(dev_priv)->gen >= 4)
2237 return 4 * 1024;
2238 else
44c5905e 2239 return 0;
4e9a86b6
VS
2240}
2241
603525d7
VS
2242static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2243 uint64_t fb_modifier)
2244{
2245 switch (fb_modifier) {
2246 case DRM_FORMAT_MOD_NONE:
2247 return intel_linear_alignment(dev_priv);
2248 case I915_FORMAT_MOD_X_TILED:
2249 if (INTEL_INFO(dev_priv)->gen >= 9)
2250 return 256 * 1024;
2251 return 0;
2252 case I915_FORMAT_MOD_Y_TILED:
2253 case I915_FORMAT_MOD_Yf_TILED:
2254 return 1 * 1024 * 1024;
2255 default:
2256 MISSING_CASE(fb_modifier);
2257 return 0;
2258 }
2259}
2260
127bd2ac 2261int
3465c580
VS
2262intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2263 unsigned int rotation)
6b95a207 2264{
850c4cdc 2265 struct drm_device *dev = fb->dev;
ce453d81 2266 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2267 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2268 struct i915_ggtt_view view;
6b95a207
KH
2269 u32 alignment;
2270 int ret;
2271
ebcdd39e
MR
2272 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2273
603525d7 2274 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
6b95a207 2275
3465c580 2276 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2277
693db184
CW
2278 /* Note that the w/a also requires 64 PTE of padding following the
2279 * bo. We currently fill all unused PTE with the shadow page and so
2280 * we should always have valid PTE following the scanout preventing
2281 * the VT-d warning.
2282 */
2283 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2284 alignment = 256 * 1024;
2285
d6dd6843
PZ
2286 /*
2287 * Global gtt pte registers are special registers which actually forward
2288 * writes to a chunk of system memory. Which means that there is no risk
2289 * that the register values disappear as soon as we call
2290 * intel_runtime_pm_put(), so it is correct to wrap only the
2291 * pin/unpin/fence and not more.
2292 */
2293 intel_runtime_pm_get(dev_priv);
2294
7580d774
ML
2295 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2296 &view);
48b956c5 2297 if (ret)
b26a6b35 2298 goto err_pm;
6b95a207
KH
2299
2300 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2301 * fence, whereas 965+ only requires a fence if using
2302 * framebuffer compression. For simplicity, we always install
2303 * a fence as the cost is not that onerous.
2304 */
9807216f
VK
2305 if (view.type == I915_GGTT_VIEW_NORMAL) {
2306 ret = i915_gem_object_get_fence(obj);
2307 if (ret == -EDEADLK) {
2308 /*
2309 * -EDEADLK means there are no free fences
2310 * no pending flips.
2311 *
2312 * This is propagated to atomic, but it uses
2313 * -EDEADLK to force a locking recovery, so
2314 * change the returned error to -EBUSY.
2315 */
2316 ret = -EBUSY;
2317 goto err_unpin;
2318 } else if (ret)
2319 goto err_unpin;
1690e1eb 2320
9807216f
VK
2321 i915_gem_object_pin_fence(obj);
2322 }
6b95a207 2323
d6dd6843 2324 intel_runtime_pm_put(dev_priv);
6b95a207 2325 return 0;
48b956c5
CW
2326
2327err_unpin:
f64b98cd 2328 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2329err_pm:
d6dd6843 2330 intel_runtime_pm_put(dev_priv);
48b956c5 2331 return ret;
6b95a207
KH
2332}
2333
3465c580 2334static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2335{
82bc3b2d 2336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2337 struct i915_ggtt_view view;
82bc3b2d 2338
ebcdd39e
MR
2339 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2340
3465c580 2341 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2342
9807216f
VK
2343 if (view.type == I915_GGTT_VIEW_NORMAL)
2344 i915_gem_object_unpin_fence(obj);
2345
f64b98cd 2346 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2347}
2348
29cf9491
VS
2349/*
2350 * Adjust the tile offset by moving the difference into
2351 * the x/y offsets.
2352 *
2353 * Input tile dimensions and pitch must already be
2354 * rotated to match x and y, and in pixel units.
2355 */
2356static u32 intel_adjust_tile_offset(int *x, int *y,
2357 unsigned int tile_width,
2358 unsigned int tile_height,
2359 unsigned int tile_size,
2360 unsigned int pitch_tiles,
2361 u32 old_offset,
2362 u32 new_offset)
2363{
2364 unsigned int tiles;
2365
2366 WARN_ON(old_offset & (tile_size - 1));
2367 WARN_ON(new_offset & (tile_size - 1));
2368 WARN_ON(new_offset > old_offset);
2369
2370 tiles = (old_offset - new_offset) / tile_size;
2371
2372 *y += tiles / pitch_tiles * tile_height;
2373 *x += tiles % pitch_tiles * tile_width;
2374
2375 return new_offset;
2376}
2377
8d0deca8
VS
2378/*
2379 * Computes the linear offset to the base tile and adjusts
2380 * x, y. bytes per pixel is assumed to be a power-of-two.
2381 *
2382 * In the 90/270 rotated case, x and y are assumed
2383 * to be already rotated to match the rotated GTT view, and
2384 * pitch is the tile_height aligned framebuffer height.
2385 */
4f2d9934
VS
2386u32 intel_compute_tile_offset(int *x, int *y,
2387 const struct drm_framebuffer *fb, int plane,
8d0deca8
VS
2388 unsigned int pitch,
2389 unsigned int rotation)
c2c75131 2390{
4f2d9934
VS
2391 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2392 uint64_t fb_modifier = fb->modifier[plane];
2393 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
29cf9491
VS
2394 u32 offset, offset_aligned, alignment;
2395
2396 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2397 if (alignment)
2398 alignment--;
2399
b5c65338 2400 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2401 unsigned int tile_size, tile_width, tile_height;
2402 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2403
d843310d 2404 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2405 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2406 fb_modifier, cpp);
2407
2408 if (intel_rotation_90_or_270(rotation)) {
2409 pitch_tiles = pitch / tile_height;
2410 swap(tile_width, tile_height);
2411 } else {
2412 pitch_tiles = pitch / (tile_width * cpp);
2413 }
d843310d
VS
2414
2415 tile_rows = *y / tile_height;
2416 *y %= tile_height;
c2c75131 2417
8d0deca8
VS
2418 tiles = *x / tile_width;
2419 *x %= tile_width;
bc752862 2420
29cf9491
VS
2421 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2422 offset_aligned = offset & ~alignment;
bc752862 2423
29cf9491
VS
2424 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2425 tile_size, pitch_tiles,
2426 offset, offset_aligned);
2427 } else {
bc752862 2428 offset = *y * pitch + *x * cpp;
29cf9491
VS
2429 offset_aligned = offset & ~alignment;
2430
4e9a86b6
VS
2431 *y = (offset & alignment) / pitch;
2432 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2433 }
29cf9491
VS
2434
2435 return offset_aligned;
c2c75131
DV
2436}
2437
b35d63fa 2438static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2439{
2440 switch (format) {
2441 case DISPPLANE_8BPP:
2442 return DRM_FORMAT_C8;
2443 case DISPPLANE_BGRX555:
2444 return DRM_FORMAT_XRGB1555;
2445 case DISPPLANE_BGRX565:
2446 return DRM_FORMAT_RGB565;
2447 default:
2448 case DISPPLANE_BGRX888:
2449 return DRM_FORMAT_XRGB8888;
2450 case DISPPLANE_RGBX888:
2451 return DRM_FORMAT_XBGR8888;
2452 case DISPPLANE_BGRX101010:
2453 return DRM_FORMAT_XRGB2101010;
2454 case DISPPLANE_RGBX101010:
2455 return DRM_FORMAT_XBGR2101010;
2456 }
2457}
2458
bc8d7dff
DL
2459static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2460{
2461 switch (format) {
2462 case PLANE_CTL_FORMAT_RGB_565:
2463 return DRM_FORMAT_RGB565;
2464 default:
2465 case PLANE_CTL_FORMAT_XRGB_8888:
2466 if (rgb_order) {
2467 if (alpha)
2468 return DRM_FORMAT_ABGR8888;
2469 else
2470 return DRM_FORMAT_XBGR8888;
2471 } else {
2472 if (alpha)
2473 return DRM_FORMAT_ARGB8888;
2474 else
2475 return DRM_FORMAT_XRGB8888;
2476 }
2477 case PLANE_CTL_FORMAT_XRGB_2101010:
2478 if (rgb_order)
2479 return DRM_FORMAT_XBGR2101010;
2480 else
2481 return DRM_FORMAT_XRGB2101010;
2482 }
2483}
2484
5724dbd1 2485static bool
f6936e29
DV
2486intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2487 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2488{
2489 struct drm_device *dev = crtc->base.dev;
3badb49f 2490 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2491 struct drm_i915_gem_object *obj = NULL;
2492 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2493 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2494 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2495 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2496 PAGE_SIZE);
2497
2498 size_aligned -= base_aligned;
46f297fb 2499
ff2652ea
CW
2500 if (plane_config->size == 0)
2501 return false;
2502
3badb49f
PZ
2503 /* If the FB is too big, just don't use it since fbdev is not very
2504 * important and we should probably use that space with FBC or other
2505 * features. */
2506 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2507 return false;
2508
12c83d99
TU
2509 mutex_lock(&dev->struct_mutex);
2510
f37b5c2b
DV
2511 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2512 base_aligned,
2513 base_aligned,
2514 size_aligned);
12c83d99
TU
2515 if (!obj) {
2516 mutex_unlock(&dev->struct_mutex);
484b41dd 2517 return false;
12c83d99 2518 }
46f297fb 2519
49af449b
DL
2520 obj->tiling_mode = plane_config->tiling;
2521 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2522 obj->stride = fb->pitches[0];
46f297fb 2523
6bf129df
DL
2524 mode_cmd.pixel_format = fb->pixel_format;
2525 mode_cmd.width = fb->width;
2526 mode_cmd.height = fb->height;
2527 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2528 mode_cmd.modifier[0] = fb->modifier[0];
2529 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2530
6bf129df 2531 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2532 &mode_cmd, obj)) {
46f297fb
JB
2533 DRM_DEBUG_KMS("intel fb init failed\n");
2534 goto out_unref_obj;
2535 }
12c83d99 2536
46f297fb 2537 mutex_unlock(&dev->struct_mutex);
484b41dd 2538
f6936e29 2539 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2540 return true;
46f297fb
JB
2541
2542out_unref_obj:
2543 drm_gem_object_unreference(&obj->base);
2544 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2545 return false;
2546}
2547
afd65eb4
MR
2548/* Update plane->state->fb to match plane->fb after driver-internal updates */
2549static void
2550update_state_fb(struct drm_plane *plane)
2551{
2552 if (plane->fb == plane->state->fb)
2553 return;
2554
2555 if (plane->state->fb)
2556 drm_framebuffer_unreference(plane->state->fb);
2557 plane->state->fb = plane->fb;
2558 if (plane->state->fb)
2559 drm_framebuffer_reference(plane->state->fb);
2560}
2561
5724dbd1 2562static void
f6936e29
DV
2563intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2564 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2565{
2566 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2567 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2568 struct drm_crtc *c;
2569 struct intel_crtc *i;
2ff8fde1 2570 struct drm_i915_gem_object *obj;
88595ac9 2571 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2572 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2573 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2574 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2575 struct intel_plane_state *intel_state =
2576 to_intel_plane_state(plane_state);
88595ac9 2577 struct drm_framebuffer *fb;
484b41dd 2578
2d14030b 2579 if (!plane_config->fb)
484b41dd
JB
2580 return;
2581
f6936e29 2582 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2583 fb = &plane_config->fb->base;
2584 goto valid_fb;
f55548b5 2585 }
484b41dd 2586
2d14030b 2587 kfree(plane_config->fb);
484b41dd
JB
2588
2589 /*
2590 * Failed to alloc the obj, check to see if we should share
2591 * an fb with another CRTC instead
2592 */
70e1e0ec 2593 for_each_crtc(dev, c) {
484b41dd
JB
2594 i = to_intel_crtc(c);
2595
2596 if (c == &intel_crtc->base)
2597 continue;
2598
2ff8fde1
MR
2599 if (!i->active)
2600 continue;
2601
88595ac9
DV
2602 fb = c->primary->fb;
2603 if (!fb)
484b41dd
JB
2604 continue;
2605
88595ac9 2606 obj = intel_fb_obj(fb);
2ff8fde1 2607 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2608 drm_framebuffer_reference(fb);
2609 goto valid_fb;
484b41dd
JB
2610 }
2611 }
88595ac9 2612
200757f5
MR
2613 /*
2614 * We've failed to reconstruct the BIOS FB. Current display state
2615 * indicates that the primary plane is visible, but has a NULL FB,
2616 * which will lead to problems later if we don't fix it up. The
2617 * simplest solution is to just disable the primary plane now and
2618 * pretend the BIOS never had it enabled.
2619 */
2620 to_intel_plane_state(plane_state)->visible = false;
2621 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2622 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2623 intel_plane->disable_plane(primary, &intel_crtc->base);
2624
88595ac9
DV
2625 return;
2626
2627valid_fb:
f44e2659
VS
2628 plane_state->src_x = 0;
2629 plane_state->src_y = 0;
be5651f2
ML
2630 plane_state->src_w = fb->width << 16;
2631 plane_state->src_h = fb->height << 16;
2632
f44e2659
VS
2633 plane_state->crtc_x = 0;
2634 plane_state->crtc_y = 0;
be5651f2
ML
2635 plane_state->crtc_w = fb->width;
2636 plane_state->crtc_h = fb->height;
2637
0a8d8a86
MR
2638 intel_state->src.x1 = plane_state->src_x;
2639 intel_state->src.y1 = plane_state->src_y;
2640 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2641 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2642 intel_state->dst.x1 = plane_state->crtc_x;
2643 intel_state->dst.y1 = plane_state->crtc_y;
2644 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2645 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2646
88595ac9
DV
2647 obj = intel_fb_obj(fb);
2648 if (obj->tiling_mode != I915_TILING_NONE)
2649 dev_priv->preserve_bios_swizzle = true;
2650
be5651f2
ML
2651 drm_framebuffer_reference(fb);
2652 primary->fb = primary->state->fb = fb;
36750f28 2653 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2654 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2655 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2656}
2657
a8d201af
ML
2658static void i9xx_update_primary_plane(struct drm_plane *primary,
2659 const struct intel_crtc_state *crtc_state,
2660 const struct intel_plane_state *plane_state)
81255565 2661{
a8d201af 2662 struct drm_device *dev = primary->dev;
81255565 2663 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
2664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2665 struct drm_framebuffer *fb = plane_state->base.fb;
2666 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
81255565 2667 int plane = intel_crtc->plane;
54ea9da8 2668 u32 linear_offset;
81255565 2669 u32 dspcntr;
f0f59a00 2670 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2671 unsigned int rotation = plane_state->base.rotation;
ac484963 2672 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
54ea9da8
VS
2673 int x = plane_state->src.x1 >> 16;
2674 int y = plane_state->src.y1 >> 16;
c9ba6fad 2675
f45651ba
VS
2676 dspcntr = DISPPLANE_GAMMA_ENABLE;
2677
fdd508a6 2678 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2679
2680 if (INTEL_INFO(dev)->gen < 4) {
2681 if (intel_crtc->pipe == PIPE_B)
2682 dspcntr |= DISPPLANE_SEL_PIPE_B;
2683
2684 /* pipesrc and dspsize control the size that is scaled from,
2685 * which should always be the user's requested size.
2686 */
2687 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
2688 ((crtc_state->pipe_src_h - 1) << 16) |
2689 (crtc_state->pipe_src_w - 1));
f45651ba 2690 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2691 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2692 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
2693 ((crtc_state->pipe_src_h - 1) << 16) |
2694 (crtc_state->pipe_src_w - 1));
c14b0485
VS
2695 I915_WRITE(PRIMPOS(plane), 0);
2696 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2697 }
81255565 2698
57779d06
VS
2699 switch (fb->pixel_format) {
2700 case DRM_FORMAT_C8:
81255565
JB
2701 dspcntr |= DISPPLANE_8BPP;
2702 break;
57779d06 2703 case DRM_FORMAT_XRGB1555:
57779d06 2704 dspcntr |= DISPPLANE_BGRX555;
81255565 2705 break;
57779d06
VS
2706 case DRM_FORMAT_RGB565:
2707 dspcntr |= DISPPLANE_BGRX565;
2708 break;
2709 case DRM_FORMAT_XRGB8888:
57779d06
VS
2710 dspcntr |= DISPPLANE_BGRX888;
2711 break;
2712 case DRM_FORMAT_XBGR8888:
57779d06
VS
2713 dspcntr |= DISPPLANE_RGBX888;
2714 break;
2715 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2716 dspcntr |= DISPPLANE_BGRX101010;
2717 break;
2718 case DRM_FORMAT_XBGR2101010:
57779d06 2719 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2720 break;
2721 default:
baba133a 2722 BUG();
81255565 2723 }
57779d06 2724
f45651ba
VS
2725 if (INTEL_INFO(dev)->gen >= 4 &&
2726 obj->tiling_mode != I915_TILING_NONE)
2727 dspcntr |= DISPPLANE_TILED;
81255565 2728
de1aa629
VS
2729 if (IS_G4X(dev))
2730 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2731
ac484963 2732 linear_offset = y * fb->pitches[0] + x * cpp;
81255565 2733
c2c75131
DV
2734 if (INTEL_INFO(dev)->gen >= 4) {
2735 intel_crtc->dspaddr_offset =
4f2d9934 2736 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2737 fb->pitches[0], rotation);
c2c75131
DV
2738 linear_offset -= intel_crtc->dspaddr_offset;
2739 } else {
e506a0c6 2740 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2741 }
e506a0c6 2742
8d0deca8 2743 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2744 dspcntr |= DISPPLANE_ROTATE_180;
2745
a8d201af
ML
2746 x += (crtc_state->pipe_src_w - 1);
2747 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2748
2749 /* Finding the last pixel of the last line of the display
2750 data and adding to linear_offset*/
2751 linear_offset +=
a8d201af 2752 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2753 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2754 }
2755
2db3366b
PZ
2756 intel_crtc->adjusted_x = x;
2757 intel_crtc->adjusted_y = y;
2758
48404c1e
SJ
2759 I915_WRITE(reg, dspcntr);
2760
01f2c773 2761 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2762 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2763 I915_WRITE(DSPSURF(plane),
2764 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2765 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2766 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2767 } else
f343c5f6 2768 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2769 POSTING_READ(reg);
17638cd6
JB
2770}
2771
a8d201af
ML
2772static void i9xx_disable_primary_plane(struct drm_plane *primary,
2773 struct drm_crtc *crtc)
17638cd6
JB
2774{
2775 struct drm_device *dev = crtc->dev;
2776 struct drm_i915_private *dev_priv = dev->dev_private;
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 2778 int plane = intel_crtc->plane;
f45651ba 2779
a8d201af
ML
2780 I915_WRITE(DSPCNTR(plane), 0);
2781 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 2782 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
2783 else
2784 I915_WRITE(DSPADDR(plane), 0);
2785 POSTING_READ(DSPCNTR(plane));
2786}
c9ba6fad 2787
a8d201af
ML
2788static void ironlake_update_primary_plane(struct drm_plane *primary,
2789 const struct intel_crtc_state *crtc_state,
2790 const struct intel_plane_state *plane_state)
2791{
2792 struct drm_device *dev = primary->dev;
2793 struct drm_i915_private *dev_priv = dev->dev_private;
2794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2795 struct drm_framebuffer *fb = plane_state->base.fb;
2796 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2797 int plane = intel_crtc->plane;
54ea9da8 2798 u32 linear_offset;
a8d201af
ML
2799 u32 dspcntr;
2800 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 2801 unsigned int rotation = plane_state->base.rotation;
ac484963 2802 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
a8d201af
ML
2803 int x = plane_state->src.x1 >> 16;
2804 int y = plane_state->src.y1 >> 16;
c9ba6fad 2805
f45651ba 2806 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 2807 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2808
2809 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2810 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2811
57779d06
VS
2812 switch (fb->pixel_format) {
2813 case DRM_FORMAT_C8:
17638cd6
JB
2814 dspcntr |= DISPPLANE_8BPP;
2815 break;
57779d06
VS
2816 case DRM_FORMAT_RGB565:
2817 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2818 break;
57779d06 2819 case DRM_FORMAT_XRGB8888:
57779d06
VS
2820 dspcntr |= DISPPLANE_BGRX888;
2821 break;
2822 case DRM_FORMAT_XBGR8888:
57779d06
VS
2823 dspcntr |= DISPPLANE_RGBX888;
2824 break;
2825 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2826 dspcntr |= DISPPLANE_BGRX101010;
2827 break;
2828 case DRM_FORMAT_XBGR2101010:
57779d06 2829 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2830 break;
2831 default:
baba133a 2832 BUG();
17638cd6
JB
2833 }
2834
2835 if (obj->tiling_mode != I915_TILING_NONE)
2836 dspcntr |= DISPPLANE_TILED;
17638cd6 2837
f45651ba 2838 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2839 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2840
ac484963 2841 linear_offset = y * fb->pitches[0] + x * cpp;
c2c75131 2842 intel_crtc->dspaddr_offset =
4f2d9934 2843 intel_compute_tile_offset(&x, &y, fb, 0,
8d0deca8 2844 fb->pitches[0], rotation);
c2c75131 2845 linear_offset -= intel_crtc->dspaddr_offset;
8d0deca8 2846 if (rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2847 dspcntr |= DISPPLANE_ROTATE_180;
2848
2849 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
a8d201af
ML
2850 x += (crtc_state->pipe_src_w - 1);
2851 y += (crtc_state->pipe_src_h - 1);
48404c1e
SJ
2852
2853 /* Finding the last pixel of the last line of the display
2854 data and adding to linear_offset*/
2855 linear_offset +=
a8d201af 2856 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
ac484963 2857 (crtc_state->pipe_src_w - 1) * cpp;
48404c1e
SJ
2858 }
2859 }
2860
2db3366b
PZ
2861 intel_crtc->adjusted_x = x;
2862 intel_crtc->adjusted_y = y;
2863
48404c1e 2864 I915_WRITE(reg, dspcntr);
17638cd6 2865
01f2c773 2866 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2867 I915_WRITE(DSPSURF(plane),
2868 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2869 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2870 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2871 } else {
2872 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2873 I915_WRITE(DSPLINOFF(plane), linear_offset);
2874 }
17638cd6 2875 POSTING_READ(reg);
17638cd6
JB
2876}
2877
7b49f948
VS
2878u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2879 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 2880{
7b49f948 2881 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 2882 return 64;
7b49f948
VS
2883 } else {
2884 int cpp = drm_format_plane_cpp(pixel_format, 0);
2885
27ba3910 2886 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
2887 }
2888}
2889
44eb0cb9
MK
2890u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2891 struct drm_i915_gem_object *obj,
2892 unsigned int plane)
121920fa 2893{
ce7f1728 2894 struct i915_ggtt_view view;
dedf278c 2895 struct i915_vma *vma;
44eb0cb9 2896 u64 offset;
121920fa 2897
e7941294 2898 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
3465c580 2899 intel_plane->base.state->rotation);
121920fa 2900
ce7f1728 2901 vma = i915_gem_obj_to_ggtt_view(obj, &view);
dedf278c 2902 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
ce7f1728 2903 view.type))
dedf278c
TU
2904 return -1;
2905
44eb0cb9 2906 offset = vma->node.start;
dedf278c
TU
2907
2908 if (plane == 1) {
7723f47d 2909 offset += vma->ggtt_view.params.rotated.uv_start_page *
dedf278c
TU
2910 PAGE_SIZE;
2911 }
2912
44eb0cb9
MK
2913 WARN_ON(upper_32_bits(offset));
2914
2915 return lower_32_bits(offset);
121920fa
TU
2916}
2917
e435d6e5
ML
2918static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2919{
2920 struct drm_device *dev = intel_crtc->base.dev;
2921 struct drm_i915_private *dev_priv = dev->dev_private;
2922
2923 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2924 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2925 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2926}
2927
a1b2278e
CK
2928/*
2929 * This function detaches (aka. unbinds) unused scalers in hardware
2930 */
0583236e 2931static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2932{
a1b2278e
CK
2933 struct intel_crtc_scaler_state *scaler_state;
2934 int i;
2935
a1b2278e
CK
2936 scaler_state = &intel_crtc->config->scaler_state;
2937
2938 /* loop through and disable scalers that aren't in use */
2939 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2940 if (!scaler_state->scalers[i].in_use)
2941 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2942 }
2943}
2944
6156a456 2945u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2946{
6156a456 2947 switch (pixel_format) {
d161cf7a 2948 case DRM_FORMAT_C8:
c34ce3d1 2949 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2950 case DRM_FORMAT_RGB565:
c34ce3d1 2951 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2952 case DRM_FORMAT_XBGR8888:
c34ce3d1 2953 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2954 case DRM_FORMAT_XRGB8888:
c34ce3d1 2955 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2956 /*
2957 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2958 * to be already pre-multiplied. We need to add a knob (or a different
2959 * DRM_FORMAT) for user-space to configure that.
2960 */
f75fb42a 2961 case DRM_FORMAT_ABGR8888:
c34ce3d1 2962 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2963 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2964 case DRM_FORMAT_ARGB8888:
c34ce3d1 2965 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2966 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2967 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2968 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2969 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2970 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2971 case DRM_FORMAT_YUYV:
c34ce3d1 2972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2973 case DRM_FORMAT_YVYU:
c34ce3d1 2974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2975 case DRM_FORMAT_UYVY:
c34ce3d1 2976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2977 case DRM_FORMAT_VYUY:
c34ce3d1 2978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2979 default:
4249eeef 2980 MISSING_CASE(pixel_format);
70d21f0e 2981 }
8cfcba41 2982
c34ce3d1 2983 return 0;
6156a456 2984}
70d21f0e 2985
6156a456
CK
2986u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2987{
6156a456 2988 switch (fb_modifier) {
30af77c4 2989 case DRM_FORMAT_MOD_NONE:
70d21f0e 2990 break;
30af77c4 2991 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2992 return PLANE_CTL_TILED_X;
b321803d 2993 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2994 return PLANE_CTL_TILED_Y;
b321803d 2995 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2996 return PLANE_CTL_TILED_YF;
70d21f0e 2997 default:
6156a456 2998 MISSING_CASE(fb_modifier);
70d21f0e 2999 }
8cfcba41 3000
c34ce3d1 3001 return 0;
6156a456 3002}
70d21f0e 3003
6156a456
CK
3004u32 skl_plane_ctl_rotation(unsigned int rotation)
3005{
3b7a5119 3006 switch (rotation) {
6156a456
CK
3007 case BIT(DRM_ROTATE_0):
3008 break;
1e8df167
SJ
3009 /*
3010 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3011 * while i915 HW rotation is clockwise, thats why this swapping.
3012 */
3b7a5119 3013 case BIT(DRM_ROTATE_90):
1e8df167 3014 return PLANE_CTL_ROTATE_270;
3b7a5119 3015 case BIT(DRM_ROTATE_180):
c34ce3d1 3016 return PLANE_CTL_ROTATE_180;
3b7a5119 3017 case BIT(DRM_ROTATE_270):
1e8df167 3018 return PLANE_CTL_ROTATE_90;
6156a456
CK
3019 default:
3020 MISSING_CASE(rotation);
3021 }
3022
c34ce3d1 3023 return 0;
6156a456
CK
3024}
3025
a8d201af
ML
3026static void skylake_update_primary_plane(struct drm_plane *plane,
3027 const struct intel_crtc_state *crtc_state,
3028 const struct intel_plane_state *plane_state)
6156a456 3029{
a8d201af 3030 struct drm_device *dev = plane->dev;
6156a456 3031 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af
ML
3032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3033 struct drm_framebuffer *fb = plane_state->base.fb;
3034 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6156a456
CK
3035 int pipe = intel_crtc->pipe;
3036 u32 plane_ctl, stride_div, stride;
3037 u32 tile_height, plane_offset, plane_size;
a8d201af 3038 unsigned int rotation = plane_state->base.rotation;
6156a456 3039 int x_offset, y_offset;
44eb0cb9 3040 u32 surf_addr;
a8d201af
ML
3041 int scaler_id = plane_state->scaler_id;
3042 int src_x = plane_state->src.x1 >> 16;
3043 int src_y = plane_state->src.y1 >> 16;
3044 int src_w = drm_rect_width(&plane_state->src) >> 16;
3045 int src_h = drm_rect_height(&plane_state->src) >> 16;
3046 int dst_x = plane_state->dst.x1;
3047 int dst_y = plane_state->dst.y1;
3048 int dst_w = drm_rect_width(&plane_state->dst);
3049 int dst_h = drm_rect_height(&plane_state->dst);
70d21f0e 3050
6156a456
CK
3051 plane_ctl = PLANE_CTL_ENABLE |
3052 PLANE_CTL_PIPE_GAMMA_ENABLE |
3053 PLANE_CTL_PIPE_CSC_ENABLE;
3054
3055 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3056 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3057 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3058 plane_ctl |= skl_plane_ctl_rotation(rotation);
3059
7b49f948 3060 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
b321803d 3061 fb->pixel_format);
dedf278c 3062 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3063
a42e5a23
PZ
3064 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3065
3b7a5119 3066 if (intel_rotation_90_or_270(rotation)) {
832be82f
VS
3067 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3068
3b7a5119 3069 /* stride = Surface height in tiles */
832be82f 3070 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
3b7a5119 3071 stride = DIV_ROUND_UP(fb->height, tile_height);
a8d201af
ML
3072 x_offset = stride * tile_height - src_y - src_h;
3073 y_offset = src_x;
6156a456 3074 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3075 } else {
3076 stride = fb->pitches[0] / stride_div;
a8d201af
ML
3077 x_offset = src_x;
3078 y_offset = src_y;
6156a456 3079 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3080 }
3081 plane_offset = y_offset << 16 | x_offset;
b321803d 3082
2db3366b
PZ
3083 intel_crtc->adjusted_x = x_offset;
3084 intel_crtc->adjusted_y = y_offset;
3085
70d21f0e 3086 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3087 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3088 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3089 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3090
3091 if (scaler_id >= 0) {
3092 uint32_t ps_ctrl = 0;
3093
3094 WARN_ON(!dst_w || !dst_h);
3095 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3096 crtc_state->scaler_state.scalers[scaler_id].mode;
3097 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3098 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3099 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3100 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3101 I915_WRITE(PLANE_POS(pipe, 0), 0);
3102 } else {
3103 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3104 }
3105
121920fa 3106 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3107
3108 POSTING_READ(PLANE_SURF(pipe, 0));
3109}
3110
a8d201af
ML
3111static void skylake_disable_primary_plane(struct drm_plane *primary,
3112 struct drm_crtc *crtc)
17638cd6
JB
3113{
3114 struct drm_device *dev = crtc->dev;
3115 struct drm_i915_private *dev_priv = dev->dev_private;
a8d201af 3116 int pipe = to_intel_crtc(crtc)->pipe;
17638cd6 3117
a8d201af
ML
3118 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3119 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3120 POSTING_READ(PLANE_SURF(pipe, 0));
3121}
29b9bde6 3122
a8d201af
ML
3123/* Assume fb object is pinned & idle & fenced and just update base pointers */
3124static int
3125intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3126 int x, int y, enum mode_set_atomic state)
3127{
3128 /* Support for kgdboc is disabled, this needs a major rework. */
3129 DRM_ERROR("legacy panic handler not supported any more.\n");
3130
3131 return -ENODEV;
81255565
JB
3132}
3133
7514747d 3134static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3135{
96a02917
VS
3136 struct drm_crtc *crtc;
3137
70e1e0ec 3138 for_each_crtc(dev, crtc) {
96a02917
VS
3139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3140 enum plane plane = intel_crtc->plane;
3141
3142 intel_prepare_page_flip(dev, plane);
3143 intel_finish_page_flip_plane(dev, plane);
3144 }
7514747d
VS
3145}
3146
3147static void intel_update_primary_planes(struct drm_device *dev)
3148{
7514747d 3149 struct drm_crtc *crtc;
96a02917 3150
70e1e0ec 3151 for_each_crtc(dev, crtc) {
11c22da6
ML
3152 struct intel_plane *plane = to_intel_plane(crtc->primary);
3153 struct intel_plane_state *plane_state;
96a02917 3154
11c22da6 3155 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3156 plane_state = to_intel_plane_state(plane->base.state);
3157
a8d201af
ML
3158 if (plane_state->visible)
3159 plane->update_plane(&plane->base,
3160 to_intel_crtc_state(crtc->state),
3161 plane_state);
11c22da6
ML
3162
3163 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3164 }
3165}
3166
7514747d
VS
3167void intel_prepare_reset(struct drm_device *dev)
3168{
3169 /* no reset support for gen2 */
3170 if (IS_GEN2(dev))
3171 return;
3172
3173 /* reset doesn't touch the display */
3174 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3175 return;
3176
3177 drm_modeset_lock_all(dev);
f98ce92f
VS
3178 /*
3179 * Disabling the crtcs gracefully seems nicer. Also the
3180 * g33 docs say we should at least disable all the planes.
3181 */
6b72d486 3182 intel_display_suspend(dev);
7514747d
VS
3183}
3184
3185void intel_finish_reset(struct drm_device *dev)
3186{
3187 struct drm_i915_private *dev_priv = to_i915(dev);
3188
3189 /*
3190 * Flips in the rings will be nuked by the reset,
3191 * so complete all pending flips so that user space
3192 * will get its events and not get stuck.
3193 */
3194 intel_complete_page_flips(dev);
3195
3196 /* no reset support for gen2 */
3197 if (IS_GEN2(dev))
3198 return;
3199
3200 /* reset doesn't touch the display */
3201 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3202 /*
3203 * Flips in the rings have been nuked by the reset,
3204 * so update the base address of all primary
3205 * planes to the the last fb to make sure we're
3206 * showing the correct fb after a reset.
11c22da6
ML
3207 *
3208 * FIXME: Atomic will make this obsolete since we won't schedule
3209 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3210 */
3211 intel_update_primary_planes(dev);
3212 return;
3213 }
3214
3215 /*
3216 * The display has been reset as well,
3217 * so need a full re-initialization.
3218 */
3219 intel_runtime_pm_disable_interrupts(dev_priv);
3220 intel_runtime_pm_enable_interrupts(dev_priv);
3221
3222 intel_modeset_init_hw(dev);
3223
3224 spin_lock_irq(&dev_priv->irq_lock);
3225 if (dev_priv->display.hpd_irq_setup)
3226 dev_priv->display.hpd_irq_setup(dev);
3227 spin_unlock_irq(&dev_priv->irq_lock);
3228
043e9bda 3229 intel_display_resume(dev);
7514747d
VS
3230
3231 intel_hpd_init(dev_priv);
3232
3233 drm_modeset_unlock_all(dev);
3234}
3235
7d5e3799
CW
3236static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3237{
3238 struct drm_device *dev = crtc->dev;
3239 struct drm_i915_private *dev_priv = dev->dev_private;
3240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3241 bool pending;
3242
3243 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3244 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3245 return false;
3246
5e2d7afc 3247 spin_lock_irq(&dev->event_lock);
7d5e3799 3248 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3249 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3250
3251 return pending;
3252}
3253
bfd16b2a
ML
3254static void intel_update_pipe_config(struct intel_crtc *crtc,
3255 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3256{
3257 struct drm_device *dev = crtc->base.dev;
3258 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3259 struct intel_crtc_state *pipe_config =
3260 to_intel_crtc_state(crtc->base.state);
e30e8f75 3261
bfd16b2a
ML
3262 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3263 crtc->base.mode = crtc->base.state->mode;
3264
3265 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3266 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3267 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3268
44522d85
ML
3269 if (HAS_DDI(dev))
3270 intel_set_pipe_csc(&crtc->base);
3271
e30e8f75
GP
3272 /*
3273 * Update pipe size and adjust fitter if needed: the reason for this is
3274 * that in compute_mode_changes we check the native mode (not the pfit
3275 * mode) to see if we can flip rather than do a full mode set. In the
3276 * fastboot case, we'll flip, but if we don't update the pipesrc and
3277 * pfit state, we'll end up with a big fb scanned out into the wrong
3278 * sized surface.
e30e8f75
GP
3279 */
3280
e30e8f75 3281 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3282 ((pipe_config->pipe_src_w - 1) << 16) |
3283 (pipe_config->pipe_src_h - 1));
3284
3285 /* on skylake this is done by detaching scalers */
3286 if (INTEL_INFO(dev)->gen >= 9) {
3287 skl_detach_scalers(crtc);
3288
3289 if (pipe_config->pch_pfit.enabled)
3290 skylake_pfit_enable(crtc);
3291 } else if (HAS_PCH_SPLIT(dev)) {
3292 if (pipe_config->pch_pfit.enabled)
3293 ironlake_pfit_enable(crtc);
3294 else if (old_crtc_state->pch_pfit.enabled)
3295 ironlake_pfit_disable(crtc, true);
e30e8f75 3296 }
e30e8f75
GP
3297}
3298
5e84e1a4
ZW
3299static void intel_fdi_normal_train(struct drm_crtc *crtc)
3300{
3301 struct drm_device *dev = crtc->dev;
3302 struct drm_i915_private *dev_priv = dev->dev_private;
3303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3304 int pipe = intel_crtc->pipe;
f0f59a00
VS
3305 i915_reg_t reg;
3306 u32 temp;
5e84e1a4
ZW
3307
3308 /* enable normal train */
3309 reg = FDI_TX_CTL(pipe);
3310 temp = I915_READ(reg);
61e499bf 3311 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3312 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3313 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3314 } else {
3315 temp &= ~FDI_LINK_TRAIN_NONE;
3316 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3317 }
5e84e1a4
ZW
3318 I915_WRITE(reg, temp);
3319
3320 reg = FDI_RX_CTL(pipe);
3321 temp = I915_READ(reg);
3322 if (HAS_PCH_CPT(dev)) {
3323 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3324 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3325 } else {
3326 temp &= ~FDI_LINK_TRAIN_NONE;
3327 temp |= FDI_LINK_TRAIN_NONE;
3328 }
3329 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3330
3331 /* wait one idle pattern time */
3332 POSTING_READ(reg);
3333 udelay(1000);
357555c0
JB
3334
3335 /* IVB wants error correction enabled */
3336 if (IS_IVYBRIDGE(dev))
3337 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3338 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3339}
3340
8db9d77b
ZW
3341/* The FDI link training functions for ILK/Ibexpeak. */
3342static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3343{
3344 struct drm_device *dev = crtc->dev;
3345 struct drm_i915_private *dev_priv = dev->dev_private;
3346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3347 int pipe = intel_crtc->pipe;
f0f59a00
VS
3348 i915_reg_t reg;
3349 u32 temp, tries;
8db9d77b 3350
1c8562f6 3351 /* FDI needs bits from pipe first */
0fc932b8 3352 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3353
e1a44743
AJ
3354 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3355 for train result */
5eddb70b
CW
3356 reg = FDI_RX_IMR(pipe);
3357 temp = I915_READ(reg);
e1a44743
AJ
3358 temp &= ~FDI_RX_SYMBOL_LOCK;
3359 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3360 I915_WRITE(reg, temp);
3361 I915_READ(reg);
e1a44743
AJ
3362 udelay(150);
3363
8db9d77b 3364 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3365 reg = FDI_TX_CTL(pipe);
3366 temp = I915_READ(reg);
627eb5a3 3367 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3368 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3371 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3372
5eddb70b
CW
3373 reg = FDI_RX_CTL(pipe);
3374 temp = I915_READ(reg);
8db9d77b
ZW
3375 temp &= ~FDI_LINK_TRAIN_NONE;
3376 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3377 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3378
3379 POSTING_READ(reg);
8db9d77b
ZW
3380 udelay(150);
3381
5b2adf89 3382 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3383 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3384 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3385 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3386
5eddb70b 3387 reg = FDI_RX_IIR(pipe);
e1a44743 3388 for (tries = 0; tries < 5; tries++) {
5eddb70b 3389 temp = I915_READ(reg);
8db9d77b
ZW
3390 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3391
3392 if ((temp & FDI_RX_BIT_LOCK)) {
3393 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3394 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3395 break;
3396 }
8db9d77b 3397 }
e1a44743 3398 if (tries == 5)
5eddb70b 3399 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3400
3401 /* Train 2 */
5eddb70b
CW
3402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
8db9d77b
ZW
3404 temp &= ~FDI_LINK_TRAIN_NONE;
3405 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3406 I915_WRITE(reg, temp);
8db9d77b 3407
5eddb70b
CW
3408 reg = FDI_RX_CTL(pipe);
3409 temp = I915_READ(reg);
8db9d77b
ZW
3410 temp &= ~FDI_LINK_TRAIN_NONE;
3411 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3412 I915_WRITE(reg, temp);
8db9d77b 3413
5eddb70b
CW
3414 POSTING_READ(reg);
3415 udelay(150);
8db9d77b 3416
5eddb70b 3417 reg = FDI_RX_IIR(pipe);
e1a44743 3418 for (tries = 0; tries < 5; tries++) {
5eddb70b 3419 temp = I915_READ(reg);
8db9d77b
ZW
3420 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3421
3422 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3423 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3424 DRM_DEBUG_KMS("FDI train 2 done.\n");
3425 break;
3426 }
8db9d77b 3427 }
e1a44743 3428 if (tries == 5)
5eddb70b 3429 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3430
3431 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3432
8db9d77b
ZW
3433}
3434
0206e353 3435static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3436 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3437 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3438 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3439 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3440};
3441
3442/* The FDI link training functions for SNB/Cougarpoint. */
3443static void gen6_fdi_link_train(struct drm_crtc *crtc)
3444{
3445 struct drm_device *dev = crtc->dev;
3446 struct drm_i915_private *dev_priv = dev->dev_private;
3447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3448 int pipe = intel_crtc->pipe;
f0f59a00
VS
3449 i915_reg_t reg;
3450 u32 temp, i, retry;
8db9d77b 3451
e1a44743
AJ
3452 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3453 for train result */
5eddb70b
CW
3454 reg = FDI_RX_IMR(pipe);
3455 temp = I915_READ(reg);
e1a44743
AJ
3456 temp &= ~FDI_RX_SYMBOL_LOCK;
3457 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3458 I915_WRITE(reg, temp);
3459
3460 POSTING_READ(reg);
e1a44743
AJ
3461 udelay(150);
3462
8db9d77b 3463 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3464 reg = FDI_TX_CTL(pipe);
3465 temp = I915_READ(reg);
627eb5a3 3466 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3467 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3468 temp &= ~FDI_LINK_TRAIN_NONE;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3471 /* SNB-B */
3472 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3473 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3474
d74cf324
DV
3475 I915_WRITE(FDI_RX_MISC(pipe),
3476 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3477
5eddb70b
CW
3478 reg = FDI_RX_CTL(pipe);
3479 temp = I915_READ(reg);
8db9d77b
ZW
3480 if (HAS_PCH_CPT(dev)) {
3481 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3482 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3483 } else {
3484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1;
3486 }
5eddb70b
CW
3487 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3488
3489 POSTING_READ(reg);
8db9d77b
ZW
3490 udelay(150);
3491
0206e353 3492 for (i = 0; i < 4; i++) {
5eddb70b
CW
3493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
8db9d77b
ZW
3495 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3496 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
8db9d77b
ZW
3500 udelay(500);
3501
fa37d39e
SP
3502 for (retry = 0; retry < 5; retry++) {
3503 reg = FDI_RX_IIR(pipe);
3504 temp = I915_READ(reg);
3505 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3506 if (temp & FDI_RX_BIT_LOCK) {
3507 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3508 DRM_DEBUG_KMS("FDI train 1 done.\n");
3509 break;
3510 }
3511 udelay(50);
8db9d77b 3512 }
fa37d39e
SP
3513 if (retry < 5)
3514 break;
8db9d77b
ZW
3515 }
3516 if (i == 4)
5eddb70b 3517 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3518
3519 /* Train 2 */
5eddb70b
CW
3520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
8db9d77b
ZW
3522 temp &= ~FDI_LINK_TRAIN_NONE;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2;
3524 if (IS_GEN6(dev)) {
3525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 /* SNB-B */
3527 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3528 }
5eddb70b 3529 I915_WRITE(reg, temp);
8db9d77b 3530
5eddb70b
CW
3531 reg = FDI_RX_CTL(pipe);
3532 temp = I915_READ(reg);
8db9d77b
ZW
3533 if (HAS_PCH_CPT(dev)) {
3534 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3535 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3536 } else {
3537 temp &= ~FDI_LINK_TRAIN_NONE;
3538 temp |= FDI_LINK_TRAIN_PATTERN_2;
3539 }
5eddb70b
CW
3540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
8db9d77b
ZW
3543 udelay(150);
3544
0206e353 3545 for (i = 0; i < 4; i++) {
5eddb70b
CW
3546 reg = FDI_TX_CTL(pipe);
3547 temp = I915_READ(reg);
8db9d77b
ZW
3548 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3549 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
8db9d77b
ZW
3553 udelay(500);
3554
fa37d39e
SP
3555 for (retry = 0; retry < 5; retry++) {
3556 reg = FDI_RX_IIR(pipe);
3557 temp = I915_READ(reg);
3558 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3559 if (temp & FDI_RX_SYMBOL_LOCK) {
3560 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3561 DRM_DEBUG_KMS("FDI train 2 done.\n");
3562 break;
3563 }
3564 udelay(50);
8db9d77b 3565 }
fa37d39e
SP
3566 if (retry < 5)
3567 break;
8db9d77b
ZW
3568 }
3569 if (i == 4)
5eddb70b 3570 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3571
3572 DRM_DEBUG_KMS("FDI train done.\n");
3573}
3574
357555c0
JB
3575/* Manual link training for Ivy Bridge A0 parts */
3576static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3577{
3578 struct drm_device *dev = crtc->dev;
3579 struct drm_i915_private *dev_priv = dev->dev_private;
3580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3581 int pipe = intel_crtc->pipe;
f0f59a00
VS
3582 i915_reg_t reg;
3583 u32 temp, i, j;
357555c0
JB
3584
3585 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3586 for train result */
3587 reg = FDI_RX_IMR(pipe);
3588 temp = I915_READ(reg);
3589 temp &= ~FDI_RX_SYMBOL_LOCK;
3590 temp &= ~FDI_RX_BIT_LOCK;
3591 I915_WRITE(reg, temp);
3592
3593 POSTING_READ(reg);
3594 udelay(150);
3595
01a415fd
DV
3596 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3597 I915_READ(FDI_RX_IIR(pipe)));
3598
139ccd3f
JB
3599 /* Try each vswing and preemphasis setting twice before moving on */
3600 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3601 /* disable first in case we need to retry */
3602 reg = FDI_TX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3605 temp &= ~FDI_TX_ENABLE;
3606 I915_WRITE(reg, temp);
357555c0 3607
139ccd3f
JB
3608 reg = FDI_RX_CTL(pipe);
3609 temp = I915_READ(reg);
3610 temp &= ~FDI_LINK_TRAIN_AUTO;
3611 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3612 temp &= ~FDI_RX_ENABLE;
3613 I915_WRITE(reg, temp);
357555c0 3614
139ccd3f 3615 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3616 reg = FDI_TX_CTL(pipe);
3617 temp = I915_READ(reg);
139ccd3f 3618 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3619 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3620 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3621 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3622 temp |= snb_b_fdi_train_param[j/2];
3623 temp |= FDI_COMPOSITE_SYNC;
3624 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3625
139ccd3f
JB
3626 I915_WRITE(FDI_RX_MISC(pipe),
3627 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3628
139ccd3f 3629 reg = FDI_RX_CTL(pipe);
357555c0 3630 temp = I915_READ(reg);
139ccd3f
JB
3631 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3632 temp |= FDI_COMPOSITE_SYNC;
3633 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3634
139ccd3f
JB
3635 POSTING_READ(reg);
3636 udelay(1); /* should be 0.5us */
357555c0 3637
139ccd3f
JB
3638 for (i = 0; i < 4; i++) {
3639 reg = FDI_RX_IIR(pipe);
3640 temp = I915_READ(reg);
3641 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3642
139ccd3f
JB
3643 if (temp & FDI_RX_BIT_LOCK ||
3644 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3645 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3646 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3647 i);
3648 break;
3649 }
3650 udelay(1); /* should be 0.5us */
3651 }
3652 if (i == 4) {
3653 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3654 continue;
3655 }
357555c0 3656
139ccd3f 3657 /* Train 2 */
357555c0
JB
3658 reg = FDI_TX_CTL(pipe);
3659 temp = I915_READ(reg);
139ccd3f
JB
3660 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3661 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3662 I915_WRITE(reg, temp);
3663
3664 reg = FDI_RX_CTL(pipe);
3665 temp = I915_READ(reg);
3666 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3667 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3668 I915_WRITE(reg, temp);
3669
3670 POSTING_READ(reg);
139ccd3f 3671 udelay(2); /* should be 1.5us */
357555c0 3672
139ccd3f
JB
3673 for (i = 0; i < 4; i++) {
3674 reg = FDI_RX_IIR(pipe);
3675 temp = I915_READ(reg);
3676 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3677
139ccd3f
JB
3678 if (temp & FDI_RX_SYMBOL_LOCK ||
3679 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3680 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3681 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3682 i);
3683 goto train_done;
3684 }
3685 udelay(2); /* should be 1.5us */
357555c0 3686 }
139ccd3f
JB
3687 if (i == 4)
3688 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3689 }
357555c0 3690
139ccd3f 3691train_done:
357555c0
JB
3692 DRM_DEBUG_KMS("FDI train done.\n");
3693}
3694
88cefb6c 3695static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3696{
88cefb6c 3697 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3698 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3699 int pipe = intel_crtc->pipe;
f0f59a00
VS
3700 i915_reg_t reg;
3701 u32 temp;
c64e311e 3702
c98e9dcf 3703 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3704 reg = FDI_RX_CTL(pipe);
3705 temp = I915_READ(reg);
627eb5a3 3706 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3707 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3708 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3709 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3710
3711 POSTING_READ(reg);
c98e9dcf
JB
3712 udelay(200);
3713
3714 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp | FDI_PCDCLK);
3717
3718 POSTING_READ(reg);
c98e9dcf
JB
3719 udelay(200);
3720
20749730
PZ
3721 /* Enable CPU FDI TX PLL, always on for Ironlake */
3722 reg = FDI_TX_CTL(pipe);
3723 temp = I915_READ(reg);
3724 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3725 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3726
20749730
PZ
3727 POSTING_READ(reg);
3728 udelay(100);
6be4a607 3729 }
0e23b99d
JB
3730}
3731
88cefb6c
DV
3732static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3733{
3734 struct drm_device *dev = intel_crtc->base.dev;
3735 struct drm_i915_private *dev_priv = dev->dev_private;
3736 int pipe = intel_crtc->pipe;
f0f59a00
VS
3737 i915_reg_t reg;
3738 u32 temp;
88cefb6c
DV
3739
3740 /* Switch from PCDclk to Rawclk */
3741 reg = FDI_RX_CTL(pipe);
3742 temp = I915_READ(reg);
3743 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3744
3745 /* Disable CPU FDI TX PLL */
3746 reg = FDI_TX_CTL(pipe);
3747 temp = I915_READ(reg);
3748 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3749
3750 POSTING_READ(reg);
3751 udelay(100);
3752
3753 reg = FDI_RX_CTL(pipe);
3754 temp = I915_READ(reg);
3755 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3756
3757 /* Wait for the clocks to turn off. */
3758 POSTING_READ(reg);
3759 udelay(100);
3760}
3761
0fc932b8
JB
3762static void ironlake_fdi_disable(struct drm_crtc *crtc)
3763{
3764 struct drm_device *dev = crtc->dev;
3765 struct drm_i915_private *dev_priv = dev->dev_private;
3766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3767 int pipe = intel_crtc->pipe;
f0f59a00
VS
3768 i915_reg_t reg;
3769 u32 temp;
0fc932b8
JB
3770
3771 /* disable CPU FDI tx and PCH FDI rx */
3772 reg = FDI_TX_CTL(pipe);
3773 temp = I915_READ(reg);
3774 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3775 POSTING_READ(reg);
3776
3777 reg = FDI_RX_CTL(pipe);
3778 temp = I915_READ(reg);
3779 temp &= ~(0x7 << 16);
dfd07d72 3780 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3781 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3782
3783 POSTING_READ(reg);
3784 udelay(100);
3785
3786 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3787 if (HAS_PCH_IBX(dev))
6f06ce18 3788 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3789
3790 /* still set train pattern 1 */
3791 reg = FDI_TX_CTL(pipe);
3792 temp = I915_READ(reg);
3793 temp &= ~FDI_LINK_TRAIN_NONE;
3794 temp |= FDI_LINK_TRAIN_PATTERN_1;
3795 I915_WRITE(reg, temp);
3796
3797 reg = FDI_RX_CTL(pipe);
3798 temp = I915_READ(reg);
3799 if (HAS_PCH_CPT(dev)) {
3800 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3801 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3802 } else {
3803 temp &= ~FDI_LINK_TRAIN_NONE;
3804 temp |= FDI_LINK_TRAIN_PATTERN_1;
3805 }
3806 /* BPC in FDI rx is consistent with that in PIPECONF */
3807 temp &= ~(0x07 << 16);
dfd07d72 3808 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3809 I915_WRITE(reg, temp);
3810
3811 POSTING_READ(reg);
3812 udelay(100);
3813}
3814
5dce5b93
CW
3815bool intel_has_pending_fb_unpin(struct drm_device *dev)
3816{
3817 struct intel_crtc *crtc;
3818
3819 /* Note that we don't need to be called with mode_config.lock here
3820 * as our list of CRTC objects is static for the lifetime of the
3821 * device and so cannot disappear as we iterate. Similarly, we can
3822 * happily treat the predicates as racy, atomic checks as userspace
3823 * cannot claim and pin a new fb without at least acquring the
3824 * struct_mutex and so serialising with us.
3825 */
d3fcc808 3826 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3827 if (atomic_read(&crtc->unpin_work_count) == 0)
3828 continue;
3829
3830 if (crtc->unpin_work)
3831 intel_wait_for_vblank(dev, crtc->pipe);
3832
3833 return true;
3834 }
3835
3836 return false;
3837}
3838
d6bbafa1
CW
3839static void page_flip_completed(struct intel_crtc *intel_crtc)
3840{
3841 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3842 struct intel_unpin_work *work = intel_crtc->unpin_work;
3843
3844 /* ensure that the unpin work is consistent wrt ->pending. */
3845 smp_rmb();
3846 intel_crtc->unpin_work = NULL;
3847
3848 if (work->event)
3849 drm_send_vblank_event(intel_crtc->base.dev,
3850 intel_crtc->pipe,
3851 work->event);
3852
3853 drm_crtc_vblank_put(&intel_crtc->base);
3854
3855 wake_up_all(&dev_priv->pending_flip_queue);
3856 queue_work(dev_priv->wq, &work->work);
3857
3858 trace_i915_flip_complete(intel_crtc->plane,
3859 work->pending_flip_obj);
3860}
3861
5008e874 3862static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3863{
0f91128d 3864 struct drm_device *dev = crtc->dev;
5bb61643 3865 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3866 long ret;
e6c3a2a6 3867
2c10d571 3868 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3869
3870 ret = wait_event_interruptible_timeout(
3871 dev_priv->pending_flip_queue,
3872 !intel_crtc_has_pending_flip(crtc),
3873 60*HZ);
3874
3875 if (ret < 0)
3876 return ret;
3877
3878 if (ret == 0) {
9c787942 3879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3880
5e2d7afc 3881 spin_lock_irq(&dev->event_lock);
9c787942
CW
3882 if (intel_crtc->unpin_work) {
3883 WARN_ONCE(1, "Removing stuck page flip\n");
3884 page_flip_completed(intel_crtc);
3885 }
5e2d7afc 3886 spin_unlock_irq(&dev->event_lock);
9c787942 3887 }
5bb61643 3888
5008e874 3889 return 0;
e6c3a2a6
CW
3890}
3891
060f02d8
VS
3892static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3893{
3894 u32 temp;
3895
3896 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3897
3898 mutex_lock(&dev_priv->sb_lock);
3899
3900 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3901 temp |= SBI_SSCCTL_DISABLE;
3902 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3903
3904 mutex_unlock(&dev_priv->sb_lock);
3905}
3906
e615efe4
ED
3907/* Program iCLKIP clock to the desired frequency */
3908static void lpt_program_iclkip(struct drm_crtc *crtc)
3909{
64b46a06 3910 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 3911 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3912 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3913 u32 temp;
3914
060f02d8 3915 lpt_disable_iclkip(dev_priv);
e615efe4 3916
64b46a06
VS
3917 /* The iCLK virtual clock root frequency is in MHz,
3918 * but the adjusted_mode->crtc_clock in in KHz. To get the
3919 * divisors, it is necessary to divide one by another, so we
3920 * convert the virtual clock precision to KHz here for higher
3921 * precision.
3922 */
3923 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
64b46a06 3926 u32 desired_divisor;
e615efe4 3927
64b46a06
VS
3928 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3929 clock << auxdiv);
3930 divsel = (desired_divisor / iclk_pi_range) - 2;
3931 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 3932
64b46a06
VS
3933 /*
3934 * Near 20MHz is a corner case which is
3935 * out of range for the 7-bit divisor
3936 */
3937 if (divsel <= 0x7f)
3938 break;
e615efe4
ED
3939 }
3940
3941 /* This should not happen with any sane values */
3942 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3943 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3944 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3945 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3946
3947 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3948 clock,
e615efe4
ED
3949 auxdiv,
3950 divsel,
3951 phasedir,
3952 phaseinc);
3953
060f02d8
VS
3954 mutex_lock(&dev_priv->sb_lock);
3955
e615efe4 3956 /* Program SSCDIVINTPHASE6 */
988d6ee8 3957 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3958 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3959 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3960 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3961 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3962 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3963 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3964 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3965
3966 /* Program SSCAUXDIV */
988d6ee8 3967 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3968 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3969 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3970 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3971
3972 /* Enable modulator and associated divider */
988d6ee8 3973 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3974 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3975 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 3976
060f02d8
VS
3977 mutex_unlock(&dev_priv->sb_lock);
3978
e615efe4
ED
3979 /* Wait for initialization time */
3980 udelay(24);
3981
3982 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3983}
3984
8802e5b6
VS
3985int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3986{
3987 u32 divsel, phaseinc, auxdiv;
3988 u32 iclk_virtual_root_freq = 172800 * 1000;
3989 u32 iclk_pi_range = 64;
3990 u32 desired_divisor;
3991 u32 temp;
3992
3993 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3994 return 0;
3995
3996 mutex_lock(&dev_priv->sb_lock);
3997
3998 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3999 if (temp & SBI_SSCCTL_DISABLE) {
4000 mutex_unlock(&dev_priv->sb_lock);
4001 return 0;
4002 }
4003
4004 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4005 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4006 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4007 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4008 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4009
4010 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4011 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4012 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4013
4014 mutex_unlock(&dev_priv->sb_lock);
4015
4016 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4017
4018 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4019 desired_divisor << auxdiv);
4020}
4021
275f01b2
DV
4022static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4023 enum pipe pch_transcoder)
4024{
4025 struct drm_device *dev = crtc->base.dev;
4026 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4027 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4028
4029 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4030 I915_READ(HTOTAL(cpu_transcoder)));
4031 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4032 I915_READ(HBLANK(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4034 I915_READ(HSYNC(cpu_transcoder)));
4035
4036 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4037 I915_READ(VTOTAL(cpu_transcoder)));
4038 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4039 I915_READ(VBLANK(cpu_transcoder)));
4040 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4041 I915_READ(VSYNC(cpu_transcoder)));
4042 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4043 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4044}
4045
003632d9 4046static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4047{
4048 struct drm_i915_private *dev_priv = dev->dev_private;
4049 uint32_t temp;
4050
4051 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4052 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4053 return;
4054
4055 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4056 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4057
003632d9
ACO
4058 temp &= ~FDI_BC_BIFURCATION_SELECT;
4059 if (enable)
4060 temp |= FDI_BC_BIFURCATION_SELECT;
4061
4062 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4063 I915_WRITE(SOUTH_CHICKEN1, temp);
4064 POSTING_READ(SOUTH_CHICKEN1);
4065}
4066
4067static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4068{
4069 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4070
4071 switch (intel_crtc->pipe) {
4072 case PIPE_A:
4073 break;
4074 case PIPE_B:
6e3c9717 4075 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4076 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4077 else
003632d9 4078 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4079
4080 break;
4081 case PIPE_C:
003632d9 4082 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4083
4084 break;
4085 default:
4086 BUG();
4087 }
4088}
4089
c48b5305
VS
4090/* Return which DP Port should be selected for Transcoder DP control */
4091static enum port
4092intel_trans_dp_port_sel(struct drm_crtc *crtc)
4093{
4094 struct drm_device *dev = crtc->dev;
4095 struct intel_encoder *encoder;
4096
4097 for_each_encoder_on_crtc(dev, crtc, encoder) {
4098 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4099 encoder->type == INTEL_OUTPUT_EDP)
4100 return enc_to_dig_port(&encoder->base)->port;
4101 }
4102
4103 return -1;
4104}
4105
f67a559d
JB
4106/*
4107 * Enable PCH resources required for PCH ports:
4108 * - PCH PLLs
4109 * - FDI training & RX/TX
4110 * - update transcoder timings
4111 * - DP transcoding bits
4112 * - transcoder
4113 */
4114static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4115{
4116 struct drm_device *dev = crtc->dev;
4117 struct drm_i915_private *dev_priv = dev->dev_private;
4118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4119 int pipe = intel_crtc->pipe;
f0f59a00 4120 u32 temp;
2c07245f 4121
ab9412ba 4122 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4123
1fbc0d78
DV
4124 if (IS_IVYBRIDGE(dev))
4125 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4126
cd986abb
DV
4127 /* Write the TU size bits before fdi link training, so that error
4128 * detection works. */
4129 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4130 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4131
3860b2ec
VS
4132 /*
4133 * Sometimes spurious CPU pipe underruns happen during FDI
4134 * training, at least with VGA+HDMI cloning. Suppress them.
4135 */
4136 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4137
c98e9dcf 4138 /* For PCH output, training FDI link */
674cf967 4139 dev_priv->display.fdi_link_train(crtc);
2c07245f 4140
3ad8a208
DV
4141 /* We need to program the right clock selection before writing the pixel
4142 * mutliplier into the DPLL. */
303b81e0 4143 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4144 u32 sel;
4b645f14 4145
c98e9dcf 4146 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4147 temp |= TRANS_DPLL_ENABLE(pipe);
4148 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4149 if (intel_crtc->config->shared_dpll ==
4150 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4151 temp |= sel;
4152 else
4153 temp &= ~sel;
c98e9dcf 4154 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4155 }
5eddb70b 4156
3ad8a208
DV
4157 /* XXX: pch pll's can be enabled any time before we enable the PCH
4158 * transcoder, and we actually should do this to not upset any PCH
4159 * transcoder that already use the clock when we share it.
4160 *
4161 * Note that enable_shared_dpll tries to do the right thing, but
4162 * get_shared_dpll unconditionally resets the pll - we need that to have
4163 * the right LVDS enable sequence. */
85b3894f 4164 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4165
d9b6cb56
JB
4166 /* set transcoder timing, panel must allow it */
4167 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4168 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4169
303b81e0 4170 intel_fdi_normal_train(crtc);
5e84e1a4 4171
3860b2ec
VS
4172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4173
c98e9dcf 4174 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4175 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4176 const struct drm_display_mode *adjusted_mode =
4177 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4178 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4179 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4180 temp = I915_READ(reg);
4181 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4182 TRANS_DP_SYNC_MASK |
4183 TRANS_DP_BPC_MASK);
e3ef4479 4184 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4185 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4186
9c4edaee 4187 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4188 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4189 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4190 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4191
4192 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4193 case PORT_B:
5eddb70b 4194 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4195 break;
c48b5305 4196 case PORT_C:
5eddb70b 4197 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4198 break;
c48b5305 4199 case PORT_D:
5eddb70b 4200 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4201 break;
4202 default:
e95d41e1 4203 BUG();
32f9d658 4204 }
2c07245f 4205
5eddb70b 4206 I915_WRITE(reg, temp);
6be4a607 4207 }
b52eb4dc 4208
b8a4f404 4209 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4210}
4211
1507e5bd
PZ
4212static void lpt_pch_enable(struct drm_crtc *crtc)
4213{
4214 struct drm_device *dev = crtc->dev;
4215 struct drm_i915_private *dev_priv = dev->dev_private;
4216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4217 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4218
ab9412ba 4219 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4220
8c52b5e8 4221 lpt_program_iclkip(crtc);
1507e5bd 4222
0540e488 4223 /* Set transcoder timing. */
275f01b2 4224 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4225
937bb610 4226 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4227}
4228
a1520318 4229static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4230{
4231 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 4232 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4233 u32 temp;
4234
4235 temp = I915_READ(dslreg);
4236 udelay(500);
4237 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4238 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4239 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4240 }
4241}
4242
86adf9d7
ML
4243static int
4244skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4245 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4246 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4247{
86adf9d7
ML
4248 struct intel_crtc_scaler_state *scaler_state =
4249 &crtc_state->scaler_state;
4250 struct intel_crtc *intel_crtc =
4251 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4252 int need_scaling;
6156a456
CK
4253
4254 need_scaling = intel_rotation_90_or_270(rotation) ?
4255 (src_h != dst_w || src_w != dst_h):
4256 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4257
4258 /*
4259 * if plane is being disabled or scaler is no more required or force detach
4260 * - free scaler binded to this plane/crtc
4261 * - in order to do this, update crtc->scaler_usage
4262 *
4263 * Here scaler state in crtc_state is set free so that
4264 * scaler can be assigned to other user. Actual register
4265 * update to free the scaler is done in plane/panel-fit programming.
4266 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4267 */
86adf9d7 4268 if (force_detach || !need_scaling) {
a1b2278e 4269 if (*scaler_id >= 0) {
86adf9d7 4270 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4271 scaler_state->scalers[*scaler_id].in_use = 0;
4272
86adf9d7
ML
4273 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4274 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4275 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4276 scaler_state->scaler_users);
4277 *scaler_id = -1;
4278 }
4279 return 0;
4280 }
4281
4282 /* range checks */
4283 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4284 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4285
4286 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4287 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4288 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4289 "size is out of scaler range\n",
86adf9d7 4290 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4291 return -EINVAL;
4292 }
4293
86adf9d7
ML
4294 /* mark this plane as a scaler user in crtc_state */
4295 scaler_state->scaler_users |= (1 << scaler_user);
4296 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4297 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4298 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4299 scaler_state->scaler_users);
4300
4301 return 0;
4302}
4303
4304/**
4305 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4306 *
4307 * @state: crtc's scaler state
86adf9d7
ML
4308 *
4309 * Return
4310 * 0 - scaler_usage updated successfully
4311 * error - requested scaling cannot be supported or other error condition
4312 */
e435d6e5 4313int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4314{
4315 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4316 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4317
4318 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4319 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4320
e435d6e5 4321 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
fa5a7970 4322 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
86adf9d7 4323 state->pipe_src_w, state->pipe_src_h,
aad941d5 4324 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4325}
4326
4327/**
4328 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4329 *
4330 * @state: crtc's scaler state
86adf9d7
ML
4331 * @plane_state: atomic plane state to update
4332 *
4333 * Return
4334 * 0 - scaler_usage updated successfully
4335 * error - requested scaling cannot be supported or other error condition
4336 */
da20eabd
ML
4337static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4338 struct intel_plane_state *plane_state)
86adf9d7
ML
4339{
4340
4341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4342 struct intel_plane *intel_plane =
4343 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4344 struct drm_framebuffer *fb = plane_state->base.fb;
4345 int ret;
4346
4347 bool force_detach = !fb || !plane_state->visible;
4348
4349 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4350 intel_plane->base.base.id, intel_crtc->pipe,
4351 drm_plane_index(&intel_plane->base));
4352
4353 ret = skl_update_scaler(crtc_state, force_detach,
4354 drm_plane_index(&intel_plane->base),
4355 &plane_state->scaler_id,
4356 plane_state->base.rotation,
4357 drm_rect_width(&plane_state->src) >> 16,
4358 drm_rect_height(&plane_state->src) >> 16,
4359 drm_rect_width(&plane_state->dst),
4360 drm_rect_height(&plane_state->dst));
4361
4362 if (ret || plane_state->scaler_id < 0)
4363 return ret;
4364
a1b2278e 4365 /* check colorkey */
818ed961 4366 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4367 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4368 intel_plane->base.base.id);
a1b2278e
CK
4369 return -EINVAL;
4370 }
4371
4372 /* Check src format */
86adf9d7
ML
4373 switch (fb->pixel_format) {
4374 case DRM_FORMAT_RGB565:
4375 case DRM_FORMAT_XBGR8888:
4376 case DRM_FORMAT_XRGB8888:
4377 case DRM_FORMAT_ABGR8888:
4378 case DRM_FORMAT_ARGB8888:
4379 case DRM_FORMAT_XRGB2101010:
4380 case DRM_FORMAT_XBGR2101010:
4381 case DRM_FORMAT_YUYV:
4382 case DRM_FORMAT_YVYU:
4383 case DRM_FORMAT_UYVY:
4384 case DRM_FORMAT_VYUY:
4385 break;
4386 default:
4387 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4388 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4389 return -EINVAL;
a1b2278e
CK
4390 }
4391
a1b2278e
CK
4392 return 0;
4393}
4394
e435d6e5
ML
4395static void skylake_scaler_disable(struct intel_crtc *crtc)
4396{
4397 int i;
4398
4399 for (i = 0; i < crtc->num_scalers; i++)
4400 skl_detach_scaler(crtc, i);
4401}
4402
4403static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4404{
4405 struct drm_device *dev = crtc->base.dev;
4406 struct drm_i915_private *dev_priv = dev->dev_private;
4407 int pipe = crtc->pipe;
a1b2278e
CK
4408 struct intel_crtc_scaler_state *scaler_state =
4409 &crtc->config->scaler_state;
4410
4411 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4412
6e3c9717 4413 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4414 int id;
4415
4416 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4417 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4418 return;
4419 }
4420
4421 id = scaler_state->scaler_id;
4422 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4423 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4424 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4425 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4426
4427 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4428 }
4429}
4430
b074cec8
JB
4431static void ironlake_pfit_enable(struct intel_crtc *crtc)
4432{
4433 struct drm_device *dev = crtc->base.dev;
4434 struct drm_i915_private *dev_priv = dev->dev_private;
4435 int pipe = crtc->pipe;
4436
6e3c9717 4437 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4438 /* Force use of hard-coded filter coefficients
4439 * as some pre-programmed values are broken,
4440 * e.g. x201.
4441 */
4442 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4443 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4444 PF_PIPE_SEL_IVB(pipe));
4445 else
4446 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4447 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4448 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4449 }
4450}
4451
20bc8673 4452void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4453{
cea165c3
VS
4454 struct drm_device *dev = crtc->base.dev;
4455 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4456
6e3c9717 4457 if (!crtc->config->ips_enabled)
d77e4531
PZ
4458 return;
4459
cea165c3
VS
4460 /* We can only enable IPS after we enable a plane and wait for a vblank */
4461 intel_wait_for_vblank(dev, crtc->pipe);
4462
d77e4531 4463 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4464 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4465 mutex_lock(&dev_priv->rps.hw_lock);
4466 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4467 mutex_unlock(&dev_priv->rps.hw_lock);
4468 /* Quoting Art Runyan: "its not safe to expect any particular
4469 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4470 * mailbox." Moreover, the mailbox may return a bogus state,
4471 * so we need to just enable it and continue on.
2a114cc1
BW
4472 */
4473 } else {
4474 I915_WRITE(IPS_CTL, IPS_ENABLE);
4475 /* The bit only becomes 1 in the next vblank, so this wait here
4476 * is essentially intel_wait_for_vblank. If we don't have this
4477 * and don't wait for vblanks until the end of crtc_enable, then
4478 * the HW state readout code will complain that the expected
4479 * IPS_CTL value is not the one we read. */
4480 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4481 DRM_ERROR("Timed out waiting for IPS enable\n");
4482 }
d77e4531
PZ
4483}
4484
20bc8673 4485void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4486{
4487 struct drm_device *dev = crtc->base.dev;
4488 struct drm_i915_private *dev_priv = dev->dev_private;
4489
6e3c9717 4490 if (!crtc->config->ips_enabled)
d77e4531
PZ
4491 return;
4492
4493 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4494 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4495 mutex_lock(&dev_priv->rps.hw_lock);
4496 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4497 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4498 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4499 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4500 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4501 } else {
2a114cc1 4502 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4503 POSTING_READ(IPS_CTL);
4504 }
d77e4531
PZ
4505
4506 /* We need to wait for a vblank before we can disable the plane. */
4507 intel_wait_for_vblank(dev, crtc->pipe);
4508}
4509
4510/** Loads the palette/gamma unit for the CRTC with the prepared values */
4511static void intel_crtc_load_lut(struct drm_crtc *crtc)
4512{
4513 struct drm_device *dev = crtc->dev;
4514 struct drm_i915_private *dev_priv = dev->dev_private;
4515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4516 enum pipe pipe = intel_crtc->pipe;
d77e4531
PZ
4517 int i;
4518 bool reenable_ips = false;
4519
4520 /* The clocks have to be on to load the palette. */
53d9f4e9 4521 if (!crtc->state->active)
d77e4531
PZ
4522 return;
4523
50360403 4524 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
a65347ba 4525 if (intel_crtc->config->has_dsi_encoder)
d77e4531
PZ
4526 assert_dsi_pll_enabled(dev_priv);
4527 else
4528 assert_pll_enabled(dev_priv, pipe);
4529 }
4530
d77e4531
PZ
4531 /* Workaround : Do not read or write the pipe palette/gamma data while
4532 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4533 */
6e3c9717 4534 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4535 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4536 GAMMA_MODE_MODE_SPLIT)) {
4537 hsw_disable_ips(intel_crtc);
4538 reenable_ips = true;
4539 }
4540
4541 for (i = 0; i < 256; i++) {
f0f59a00 4542 i915_reg_t palreg;
f65a9c5b
VS
4543
4544 if (HAS_GMCH_DISPLAY(dev))
4545 palreg = PALETTE(pipe, i);
4546 else
4547 palreg = LGC_PALETTE(pipe, i);
4548
4549 I915_WRITE(palreg,
d77e4531
PZ
4550 (intel_crtc->lut_r[i] << 16) |
4551 (intel_crtc->lut_g[i] << 8) |
4552 intel_crtc->lut_b[i]);
4553 }
4554
4555 if (reenable_ips)
4556 hsw_enable_ips(intel_crtc);
4557}
4558
7cac945f 4559static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4560{
7cac945f 4561 if (intel_crtc->overlay) {
d3eedb1a
VS
4562 struct drm_device *dev = intel_crtc->base.dev;
4563 struct drm_i915_private *dev_priv = dev->dev_private;
4564
4565 mutex_lock(&dev->struct_mutex);
4566 dev_priv->mm.interruptible = false;
4567 (void) intel_overlay_switch_off(intel_crtc->overlay);
4568 dev_priv->mm.interruptible = true;
4569 mutex_unlock(&dev->struct_mutex);
4570 }
4571
4572 /* Let userspace switch the overlay on again. In most cases userspace
4573 * has to recompute where to put it anyway.
4574 */
4575}
4576
87d4300a
ML
4577/**
4578 * intel_post_enable_primary - Perform operations after enabling primary plane
4579 * @crtc: the CRTC whose primary plane was just enabled
4580 *
4581 * Performs potentially sleeping operations that must be done after the primary
4582 * plane is enabled, such as updating FBC and IPS. Note that this may be
4583 * called due to an explicit primary plane update, or due to an implicit
4584 * re-enable that is caused when a sprite plane is updated to no longer
4585 * completely hide the primary plane.
4586 */
4587static void
4588intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4589{
4590 struct drm_device *dev = crtc->dev;
87d4300a 4591 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4593 int pipe = intel_crtc->pipe;
a5c4d7bc 4594
87d4300a
ML
4595 /*
4596 * FIXME IPS should be fine as long as one plane is
4597 * enabled, but in practice it seems to have problems
4598 * when going from primary only to sprite only and vice
4599 * versa.
4600 */
a5c4d7bc
VS
4601 hsw_enable_ips(intel_crtc);
4602
f99d7069 4603 /*
87d4300a
ML
4604 * Gen2 reports pipe underruns whenever all planes are disabled.
4605 * So don't enable underrun reporting before at least some planes
4606 * are enabled.
4607 * FIXME: Need to fix the logic to work when we turn off all planes
4608 * but leave the pipe running.
f99d7069 4609 */
87d4300a
ML
4610 if (IS_GEN2(dev))
4611 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4612
aca7b684
VS
4613 /* Underruns don't always raise interrupts, so check manually. */
4614 intel_check_cpu_fifo_underruns(dev_priv);
4615 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4616}
4617
2622a081 4618/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4619static void
4620intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4621{
4622 struct drm_device *dev = crtc->dev;
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4625 int pipe = intel_crtc->pipe;
a5c4d7bc 4626
87d4300a
ML
4627 /*
4628 * Gen2 reports pipe underruns whenever all planes are disabled.
4629 * So diasble underrun reporting before all the planes get disabled.
4630 * FIXME: Need to fix the logic to work when we turn off all planes
4631 * but leave the pipe running.
4632 */
4633 if (IS_GEN2(dev))
4634 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4635
2622a081
VS
4636 /*
4637 * FIXME IPS should be fine as long as one plane is
4638 * enabled, but in practice it seems to have problems
4639 * when going from primary only to sprite only and vice
4640 * versa.
4641 */
4642 hsw_disable_ips(intel_crtc);
4643}
4644
4645/* FIXME get rid of this and use pre_plane_update */
4646static void
4647intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4648{
4649 struct drm_device *dev = crtc->dev;
4650 struct drm_i915_private *dev_priv = dev->dev_private;
4651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4652 int pipe = intel_crtc->pipe;
4653
4654 intel_pre_disable_primary(crtc);
4655
87d4300a
ML
4656 /*
4657 * Vblank time updates from the shadow to live plane control register
4658 * are blocked if the memory self-refresh mode is active at that
4659 * moment. So to make sure the plane gets truly disabled, disable
4660 * first the self-refresh mode. The self-refresh enable bit in turn
4661 * will be checked/applied by the HW only at the next frame start
4662 * event which is after the vblank start event, so we need to have a
4663 * wait-for-vblank between disabling the plane and the pipe.
4664 */
262cd2e1 4665 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4666 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4667 dev_priv->wm.vlv.cxsr = false;
4668 intel_wait_for_vblank(dev, pipe);
4669 }
87d4300a
ML
4670}
4671
cd202f69 4672static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4673{
cd202f69
ML
4674 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4675 struct drm_atomic_state *old_state = old_crtc_state->base.state;
ac21b225 4676 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
92826fcd
ML
4677 struct intel_crtc_state *pipe_config =
4678 to_intel_crtc_state(crtc->base.state);
ac21b225 4679 struct drm_device *dev = crtc->base.dev;
cd202f69
ML
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681 struct drm_plane *primary = crtc->base.primary;
4682 struct drm_plane_state *old_pri_state =
4683 drm_atomic_get_existing_plane_state(old_state, primary);
ac21b225 4684
cd202f69 4685 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
ac21b225 4686
ab1d3a0e 4687 crtc->wm.cxsr_allowed = true;
852eb00d 4688
caed361d 4689 if (pipe_config->update_wm_post && pipe_config->base.active)
f015c551
VS
4690 intel_update_watermarks(&crtc->base);
4691
c80ac854 4692 if (atomic->update_fbc)
1eb52238 4693 intel_fbc_post_update(crtc);
ac21b225 4694
cd202f69
ML
4695 if (old_pri_state) {
4696 struct intel_plane_state *primary_state =
4697 to_intel_plane_state(primary->state);
4698 struct intel_plane_state *old_primary_state =
4699 to_intel_plane_state(old_pri_state);
4700
4701 if (primary_state->visible &&
4702 (needs_modeset(&pipe_config->base) ||
4703 !old_primary_state->visible))
4704 intel_post_enable_primary(&crtc->base);
4705 }
ac21b225 4706
ac21b225
ML
4707 memset(atomic, 0, sizeof(*atomic));
4708}
4709
5c74cd73 4710static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 4711{
5c74cd73 4712 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 4713 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4714 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225 4715 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
ab1d3a0e
ML
4716 struct intel_crtc_state *pipe_config =
4717 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
4718 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4719 struct drm_plane *primary = crtc->base.primary;
4720 struct drm_plane_state *old_pri_state =
4721 drm_atomic_get_existing_plane_state(old_state, primary);
4722 bool modeset = needs_modeset(&pipe_config->base);
ac21b225 4723
1eb52238
PZ
4724 if (atomic->update_fbc)
4725 intel_fbc_pre_update(crtc);
ac21b225 4726
5c74cd73
ML
4727 if (old_pri_state) {
4728 struct intel_plane_state *primary_state =
4729 to_intel_plane_state(primary->state);
4730 struct intel_plane_state *old_primary_state =
4731 to_intel_plane_state(old_pri_state);
4732
4733 if (old_primary_state->visible &&
4734 (modeset || !primary_state->visible))
4735 intel_pre_disable_primary(&crtc->base);
4736 }
852eb00d 4737
ab1d3a0e 4738 if (pipe_config->disable_cxsr) {
852eb00d 4739 crtc->wm.cxsr_allowed = false;
2dfd178d 4740
2622a081
VS
4741 /*
4742 * Vblank time updates from the shadow to live plane control register
4743 * are blocked if the memory self-refresh mode is active at that
4744 * moment. So to make sure the plane gets truly disabled, disable
4745 * first the self-refresh mode. The self-refresh enable bit in turn
4746 * will be checked/applied by the HW only at the next frame start
4747 * event which is after the vblank start event, so we need to have a
4748 * wait-for-vblank between disabling the plane and the pipe.
4749 */
4750 if (old_crtc_state->base.active) {
2dfd178d 4751 intel_set_memory_cxsr(dev_priv, false);
2622a081
VS
4752 dev_priv->wm.vlv.cxsr = false;
4753 intel_wait_for_vblank(dev, crtc->pipe);
4754 }
852eb00d 4755 }
92826fcd 4756
ed4a6a7c
MR
4757 /*
4758 * IVB workaround: must disable low power watermarks for at least
4759 * one frame before enabling scaling. LP watermarks can be re-enabled
4760 * when scaling is disabled.
4761 *
4762 * WaCxSRDisabledForSpriteScaling:ivb
4763 */
4764 if (pipe_config->disable_lp_wm) {
4765 ilk_disable_lp_wm(dev);
4766 intel_wait_for_vblank(dev, crtc->pipe);
4767 }
4768
4769 /*
4770 * If we're doing a modeset, we're done. No need to do any pre-vblank
4771 * watermark programming here.
4772 */
4773 if (needs_modeset(&pipe_config->base))
4774 return;
4775
4776 /*
4777 * For platforms that support atomic watermarks, program the
4778 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4779 * will be the intermediate values that are safe for both pre- and
4780 * post- vblank; when vblank happens, the 'active' values will be set
4781 * to the final 'target' values and we'll do this again to get the
4782 * optimal watermarks. For gen9+ platforms, the values we program here
4783 * will be the final target values which will get automatically latched
4784 * at vblank time; no further programming will be necessary.
4785 *
4786 * If a platform hasn't been transitioned to atomic watermarks yet,
4787 * we'll continue to update watermarks the old way, if flags tell
4788 * us to.
4789 */
4790 if (dev_priv->display.initial_watermarks != NULL)
4791 dev_priv->display.initial_watermarks(pipe_config);
caed361d 4792 else if (pipe_config->update_wm_pre)
92826fcd 4793 intel_update_watermarks(&crtc->base);
ac21b225
ML
4794}
4795
d032ffa0 4796static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4797{
4798 struct drm_device *dev = crtc->dev;
4799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4800 struct drm_plane *p;
87d4300a
ML
4801 int pipe = intel_crtc->pipe;
4802
7cac945f 4803 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4804
d032ffa0
ML
4805 drm_for_each_plane_mask(p, dev, plane_mask)
4806 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4807
f99d7069
DV
4808 /*
4809 * FIXME: Once we grow proper nuclear flip support out of this we need
4810 * to compute the mask of flip planes precisely. For the time being
4811 * consider this a flip to a NULL plane.
4812 */
4813 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4814}
4815
f67a559d
JB
4816static void ironlake_crtc_enable(struct drm_crtc *crtc)
4817{
4818 struct drm_device *dev = crtc->dev;
4819 struct drm_i915_private *dev_priv = dev->dev_private;
4820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4821 struct intel_encoder *encoder;
f67a559d 4822 int pipe = intel_crtc->pipe;
f67a559d 4823
53d9f4e9 4824 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4825 return;
4826
81b088ca
VS
4827 if (intel_crtc->config->has_pch_encoder)
4828 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4829
6e3c9717 4830 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4831 intel_prepare_shared_dpll(intel_crtc);
4832
6e3c9717 4833 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4834 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4835
4836 intel_set_pipe_timings(intel_crtc);
4837
6e3c9717 4838 if (intel_crtc->config->has_pch_encoder) {
29407aab 4839 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4840 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4841 }
4842
4843 ironlake_set_pipeconf(crtc);
4844
f67a559d 4845 intel_crtc->active = true;
8664281b 4846
a72e4c9f 4847 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4848
f6736a1a 4849 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4850 if (encoder->pre_enable)
4851 encoder->pre_enable(encoder);
f67a559d 4852
6e3c9717 4853 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4854 /* Note: FDI PLL enabling _must_ be done before we enable the
4855 * cpu pipes, hence this is separate from all the other fdi/pch
4856 * enabling. */
88cefb6c 4857 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4858 } else {
4859 assert_fdi_tx_disabled(dev_priv, pipe);
4860 assert_fdi_rx_disabled(dev_priv, pipe);
4861 }
f67a559d 4862
b074cec8 4863 ironlake_pfit_enable(intel_crtc);
f67a559d 4864
9c54c0dd
JB
4865 /*
4866 * On ILK+ LUT must be loaded before the pipe is running but with
4867 * clocks enabled
4868 */
4869 intel_crtc_load_lut(crtc);
4870
1d5bf5d9
ID
4871 if (dev_priv->display.initial_watermarks != NULL)
4872 dev_priv->display.initial_watermarks(intel_crtc->config);
e1fdc473 4873 intel_enable_pipe(intel_crtc);
f67a559d 4874
6e3c9717 4875 if (intel_crtc->config->has_pch_encoder)
f67a559d 4876 ironlake_pch_enable(crtc);
c98e9dcf 4877
f9b61ff6
DV
4878 assert_vblank_disabled(crtc);
4879 drm_crtc_vblank_on(crtc);
4880
fa5c73b1
DV
4881 for_each_encoder_on_crtc(dev, crtc, encoder)
4882 encoder->enable(encoder);
61b77ddd
DV
4883
4884 if (HAS_PCH_CPT(dev))
a1520318 4885 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4886
4887 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4888 if (intel_crtc->config->has_pch_encoder)
4889 intel_wait_for_vblank(dev, pipe);
4890 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4891}
4892
42db64ef
PZ
4893/* IPS only exists on ULT machines and is tied to pipe A. */
4894static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4895{
f5adf94e 4896 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4897}
4898
4f771f10
PZ
4899static void haswell_crtc_enable(struct drm_crtc *crtc)
4900{
4901 struct drm_device *dev = crtc->dev;
4902 struct drm_i915_private *dev_priv = dev->dev_private;
4903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4904 struct intel_encoder *encoder;
99d736a2
ML
4905 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4906 struct intel_crtc_state *pipe_config =
4907 to_intel_crtc_state(crtc->state);
4f771f10 4908
53d9f4e9 4909 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4910 return;
4911
81b088ca
VS
4912 if (intel_crtc->config->has_pch_encoder)
4913 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4914 false);
4915
8106ddbd 4916 if (intel_crtc->config->shared_dpll)
df8ad70c
DV
4917 intel_enable_shared_dpll(intel_crtc);
4918
6e3c9717 4919 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4920 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4921
4922 intel_set_pipe_timings(intel_crtc);
4923
6e3c9717
ACO
4924 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4925 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4926 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4927 }
4928
6e3c9717 4929 if (intel_crtc->config->has_pch_encoder) {
229fca97 4930 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4931 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4932 }
4933
4934 haswell_set_pipeconf(crtc);
4935
4936 intel_set_pipe_csc(crtc);
4937
4f771f10 4938 intel_crtc->active = true;
8664281b 4939
6b698516
DV
4940 if (intel_crtc->config->has_pch_encoder)
4941 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4942 else
4943 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4944
7d4aefd0 4945 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10
PZ
4946 if (encoder->pre_enable)
4947 encoder->pre_enable(encoder);
7d4aefd0 4948 }
4f771f10 4949
d2d65408 4950 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4951 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4952
a65347ba 4953 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4954 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4955
1c132b44 4956 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4957 skylake_pfit_enable(intel_crtc);
ff6d9f55 4958 else
1c132b44 4959 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4960
4961 /*
4962 * On ILK+ LUT must be loaded before the pipe is running but with
4963 * clocks enabled
4964 */
4965 intel_crtc_load_lut(crtc);
4966
1f544388 4967 intel_ddi_set_pipe_settings(crtc);
a65347ba 4968 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 4969 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4970
1d5bf5d9
ID
4971 if (dev_priv->display.initial_watermarks != NULL)
4972 dev_priv->display.initial_watermarks(pipe_config);
4973 else
4974 intel_update_watermarks(crtc);
e1fdc473 4975 intel_enable_pipe(intel_crtc);
42db64ef 4976
6e3c9717 4977 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4978 lpt_pch_enable(crtc);
4f771f10 4979
a65347ba 4980 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4981 intel_ddi_set_vc_payload_alloc(crtc, true);
4982
f9b61ff6
DV
4983 assert_vblank_disabled(crtc);
4984 drm_crtc_vblank_on(crtc);
4985
8807e55b 4986 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4987 encoder->enable(encoder);
8807e55b
JN
4988 intel_opregion_notify_encoder(encoder, true);
4989 }
4f771f10 4990
6b698516
DV
4991 if (intel_crtc->config->has_pch_encoder) {
4992 intel_wait_for_vblank(dev, pipe);
4993 intel_wait_for_vblank(dev, pipe);
4994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
4995 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4996 true);
6b698516 4997 }
d2d65408 4998
e4916946
PZ
4999 /* If we change the relative order between pipe/planes enabling, we need
5000 * to change the workaround. */
99d736a2
ML
5001 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5002 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5003 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5004 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5005 }
4f771f10
PZ
5006}
5007
bfd16b2a 5008static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5009{
5010 struct drm_device *dev = crtc->base.dev;
5011 struct drm_i915_private *dev_priv = dev->dev_private;
5012 int pipe = crtc->pipe;
5013
5014 /* To avoid upsetting the power well on haswell only disable the pfit if
5015 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5016 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5017 I915_WRITE(PF_CTL(pipe), 0);
5018 I915_WRITE(PF_WIN_POS(pipe), 0);
5019 I915_WRITE(PF_WIN_SZ(pipe), 0);
5020 }
5021}
5022
6be4a607
JB
5023static void ironlake_crtc_disable(struct drm_crtc *crtc)
5024{
5025 struct drm_device *dev = crtc->dev;
5026 struct drm_i915_private *dev_priv = dev->dev_private;
5027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5028 struct intel_encoder *encoder;
6be4a607 5029 int pipe = intel_crtc->pipe;
b52eb4dc 5030
37ca8d4c
VS
5031 if (intel_crtc->config->has_pch_encoder)
5032 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5033
ea9d758d
DV
5034 for_each_encoder_on_crtc(dev, crtc, encoder)
5035 encoder->disable(encoder);
5036
f9b61ff6
DV
5037 drm_crtc_vblank_off(crtc);
5038 assert_vblank_disabled(crtc);
5039
3860b2ec
VS
5040 /*
5041 * Sometimes spurious CPU pipe underruns happen when the
5042 * pipe is already disabled, but FDI RX/TX is still enabled.
5043 * Happens at least with VGA+HDMI cloning. Suppress them.
5044 */
5045 if (intel_crtc->config->has_pch_encoder)
5046 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5047
575f7ab7 5048 intel_disable_pipe(intel_crtc);
32f9d658 5049
bfd16b2a 5050 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5051
3860b2ec 5052 if (intel_crtc->config->has_pch_encoder) {
5a74f70a 5053 ironlake_fdi_disable(crtc);
3860b2ec
VS
5054 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5055 }
5a74f70a 5056
bf49ec8c
DV
5057 for_each_encoder_on_crtc(dev, crtc, encoder)
5058 if (encoder->post_disable)
5059 encoder->post_disable(encoder);
2c07245f 5060
6e3c9717 5061 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5062 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5063
d925c59a 5064 if (HAS_PCH_CPT(dev)) {
f0f59a00
VS
5065 i915_reg_t reg;
5066 u32 temp;
5067
d925c59a
DV
5068 /* disable TRANS_DP_CTL */
5069 reg = TRANS_DP_CTL(pipe);
5070 temp = I915_READ(reg);
5071 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5072 TRANS_DP_PORT_SEL_MASK);
5073 temp |= TRANS_DP_PORT_SEL_NONE;
5074 I915_WRITE(reg, temp);
5075
5076 /* disable DPLL_SEL */
5077 temp = I915_READ(PCH_DPLL_SEL);
11887397 5078 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5079 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5080 }
e3421a18 5081
d925c59a
DV
5082 ironlake_fdi_pll_disable(intel_crtc);
5083 }
81b088ca
VS
5084
5085 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5086}
1b3c7a47 5087
4f771f10 5088static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5089{
4f771f10
PZ
5090 struct drm_device *dev = crtc->dev;
5091 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5093 struct intel_encoder *encoder;
6e3c9717 5094 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5095
d2d65408
VS
5096 if (intel_crtc->config->has_pch_encoder)
5097 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5098 false);
5099
8807e55b
JN
5100 for_each_encoder_on_crtc(dev, crtc, encoder) {
5101 intel_opregion_notify_encoder(encoder, false);
4f771f10 5102 encoder->disable(encoder);
8807e55b 5103 }
4f771f10 5104
f9b61ff6
DV
5105 drm_crtc_vblank_off(crtc);
5106 assert_vblank_disabled(crtc);
5107
575f7ab7 5108 intel_disable_pipe(intel_crtc);
4f771f10 5109
6e3c9717 5110 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5111 intel_ddi_set_vc_payload_alloc(crtc, false);
5112
a65347ba 5113 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5114 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5115
1c132b44 5116 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5117 skylake_scaler_disable(intel_crtc);
ff6d9f55 5118 else
bfd16b2a 5119 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5120
a65347ba 5121 if (!intel_crtc->config->has_dsi_encoder)
7d4aefd0 5122 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5123
97b040aa
ID
5124 for_each_encoder_on_crtc(dev, crtc, encoder)
5125 if (encoder->post_disable)
5126 encoder->post_disable(encoder);
81b088ca 5127
92966a37
VS
5128 if (intel_crtc->config->has_pch_encoder) {
5129 lpt_disable_pch_transcoder(dev_priv);
503a74e9 5130 lpt_disable_iclkip(dev_priv);
92966a37
VS
5131 intel_ddi_fdi_disable(crtc);
5132
81b088ca
VS
5133 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5134 true);
92966a37 5135 }
4f771f10
PZ
5136}
5137
2dd24552
JB
5138static void i9xx_pfit_enable(struct intel_crtc *crtc)
5139{
5140 struct drm_device *dev = crtc->base.dev;
5141 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5142 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5143
681a8504 5144 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5145 return;
5146
2dd24552 5147 /*
c0b03411
DV
5148 * The panel fitter should only be adjusted whilst the pipe is disabled,
5149 * according to register description and PRM.
2dd24552 5150 */
c0b03411
DV
5151 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5152 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5153
b074cec8
JB
5154 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5155 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5156
5157 /* Border color in case we don't scale up to the full screen. Black by
5158 * default, change to something else for debugging. */
5159 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5160}
5161
d05410f9
DA
5162static enum intel_display_power_domain port_to_power_domain(enum port port)
5163{
5164 switch (port) {
5165 case PORT_A:
6331a704 5166 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5167 case PORT_B:
6331a704 5168 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5169 case PORT_C:
6331a704 5170 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5171 case PORT_D:
6331a704 5172 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5173 case PORT_E:
6331a704 5174 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5175 default:
b9fec167 5176 MISSING_CASE(port);
d05410f9
DA
5177 return POWER_DOMAIN_PORT_OTHER;
5178 }
5179}
5180
25f78f58
VS
5181static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5182{
5183 switch (port) {
5184 case PORT_A:
5185 return POWER_DOMAIN_AUX_A;
5186 case PORT_B:
5187 return POWER_DOMAIN_AUX_B;
5188 case PORT_C:
5189 return POWER_DOMAIN_AUX_C;
5190 case PORT_D:
5191 return POWER_DOMAIN_AUX_D;
5192 case PORT_E:
5193 /* FIXME: Check VBT for actual wiring of PORT E */
5194 return POWER_DOMAIN_AUX_D;
5195 default:
b9fec167 5196 MISSING_CASE(port);
25f78f58
VS
5197 return POWER_DOMAIN_AUX_A;
5198 }
5199}
5200
319be8ae
ID
5201enum intel_display_power_domain
5202intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5203{
5204 struct drm_device *dev = intel_encoder->base.dev;
5205 struct intel_digital_port *intel_dig_port;
5206
5207 switch (intel_encoder->type) {
5208 case INTEL_OUTPUT_UNKNOWN:
5209 /* Only DDI platforms should ever use this output type */
5210 WARN_ON_ONCE(!HAS_DDI(dev));
5211 case INTEL_OUTPUT_DISPLAYPORT:
5212 case INTEL_OUTPUT_HDMI:
5213 case INTEL_OUTPUT_EDP:
5214 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5215 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5216 case INTEL_OUTPUT_DP_MST:
5217 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5218 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5219 case INTEL_OUTPUT_ANALOG:
5220 return POWER_DOMAIN_PORT_CRT;
5221 case INTEL_OUTPUT_DSI:
5222 return POWER_DOMAIN_PORT_DSI;
5223 default:
5224 return POWER_DOMAIN_PORT_OTHER;
5225 }
5226}
5227
25f78f58
VS
5228enum intel_display_power_domain
5229intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5230{
5231 struct drm_device *dev = intel_encoder->base.dev;
5232 struct intel_digital_port *intel_dig_port;
5233
5234 switch (intel_encoder->type) {
5235 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5236 case INTEL_OUTPUT_HDMI:
5237 /*
5238 * Only DDI platforms should ever use these output types.
5239 * We can get here after the HDMI detect code has already set
5240 * the type of the shared encoder. Since we can't be sure
5241 * what's the status of the given connectors, play safe and
5242 * run the DP detection too.
5243 */
25f78f58
VS
5244 WARN_ON_ONCE(!HAS_DDI(dev));
5245 case INTEL_OUTPUT_DISPLAYPORT:
5246 case INTEL_OUTPUT_EDP:
5247 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5248 return port_to_aux_power_domain(intel_dig_port->port);
5249 case INTEL_OUTPUT_DP_MST:
5250 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5251 return port_to_aux_power_domain(intel_dig_port->port);
5252 default:
b9fec167 5253 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5254 return POWER_DOMAIN_AUX_A;
5255 }
5256}
5257
74bff5f9
ML
5258static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5259 struct intel_crtc_state *crtc_state)
77d22dca 5260{
319be8ae 5261 struct drm_device *dev = crtc->dev;
74bff5f9 5262 struct drm_encoder *encoder;
319be8ae
ID
5263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5264 enum pipe pipe = intel_crtc->pipe;
77d22dca 5265 unsigned long mask;
74bff5f9 5266 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5267
74bff5f9 5268 if (!crtc_state->base.active)
292b990e
ML
5269 return 0;
5270
77d22dca
ID
5271 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5272 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5273 if (crtc_state->pch_pfit.enabled ||
5274 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5275 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5276
74bff5f9
ML
5277 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5278 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5279
319be8ae 5280 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5281 }
319be8ae 5282
77d22dca
ID
5283 return mask;
5284}
5285
74bff5f9
ML
5286static unsigned long
5287modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5288 struct intel_crtc_state *crtc_state)
77d22dca 5289{
292b990e
ML
5290 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5292 enum intel_display_power_domain domain;
5293 unsigned long domains, new_domains, old_domains;
77d22dca 5294
292b990e 5295 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5296 intel_crtc->enabled_power_domains = new_domains =
5297 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5298
292b990e
ML
5299 domains = new_domains & ~old_domains;
5300
5301 for_each_power_domain(domain, domains)
5302 intel_display_power_get(dev_priv, domain);
5303
5304 return old_domains & ~new_domains;
5305}
5306
5307static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5308 unsigned long domains)
5309{
5310 enum intel_display_power_domain domain;
5311
5312 for_each_power_domain(domain, domains)
5313 intel_display_power_put(dev_priv, domain);
5314}
77d22dca 5315
adafdc6f
MK
5316static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5317{
5318 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5319
5320 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5321 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5322 return max_cdclk_freq;
5323 else if (IS_CHERRYVIEW(dev_priv))
5324 return max_cdclk_freq*95/100;
5325 else if (INTEL_INFO(dev_priv)->gen < 4)
5326 return 2*max_cdclk_freq*90/100;
5327 else
5328 return max_cdclk_freq*90/100;
5329}
5330
560a7ae4
DL
5331static void intel_update_max_cdclk(struct drm_device *dev)
5332{
5333 struct drm_i915_private *dev_priv = dev->dev_private;
5334
ef11bdb3 5335 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5336 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5337
5338 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5339 dev_priv->max_cdclk_freq = 675000;
5340 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5341 dev_priv->max_cdclk_freq = 540000;
5342 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5343 dev_priv->max_cdclk_freq = 450000;
5344 else
5345 dev_priv->max_cdclk_freq = 337500;
5346 } else if (IS_BROADWELL(dev)) {
5347 /*
5348 * FIXME with extra cooling we can allow
5349 * 540 MHz for ULX and 675 Mhz for ULT.
5350 * How can we know if extra cooling is
5351 * available? PCI ID, VTB, something else?
5352 */
5353 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5354 dev_priv->max_cdclk_freq = 450000;
5355 else if (IS_BDW_ULX(dev))
5356 dev_priv->max_cdclk_freq = 450000;
5357 else if (IS_BDW_ULT(dev))
5358 dev_priv->max_cdclk_freq = 540000;
5359 else
5360 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5361 } else if (IS_CHERRYVIEW(dev)) {
5362 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5363 } else if (IS_VALLEYVIEW(dev)) {
5364 dev_priv->max_cdclk_freq = 400000;
5365 } else {
5366 /* otherwise assume cdclk is fixed */
5367 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5368 }
5369
adafdc6f
MK
5370 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5371
560a7ae4
DL
5372 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5373 dev_priv->max_cdclk_freq);
adafdc6f
MK
5374
5375 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5376 dev_priv->max_dotclk_freq);
560a7ae4
DL
5377}
5378
5379static void intel_update_cdclk(struct drm_device *dev)
5380{
5381 struct drm_i915_private *dev_priv = dev->dev_private;
5382
5383 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5384 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5385 dev_priv->cdclk_freq);
5386
5387 /*
5388 * Program the gmbus_freq based on the cdclk frequency.
5389 * BSpec erroneously claims we should aim for 4MHz, but
5390 * in fact 1MHz is the correct frequency.
5391 */
666a4537 5392 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
560a7ae4
DL
5393 /*
5394 * Program the gmbus_freq based on the cdclk frequency.
5395 * BSpec erroneously claims we should aim for 4MHz, but
5396 * in fact 1MHz is the correct frequency.
5397 */
5398 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5399 }
5400
5401 if (dev_priv->max_cdclk_freq == 0)
5402 intel_update_max_cdclk(dev);
5403}
5404
70d0c574 5405static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5406{
5407 struct drm_i915_private *dev_priv = dev->dev_private;
5408 uint32_t divider;
5409 uint32_t ratio;
5410 uint32_t current_freq;
5411 int ret;
5412
5413 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5414 switch (frequency) {
5415 case 144000:
5416 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5417 ratio = BXT_DE_PLL_RATIO(60);
5418 break;
5419 case 288000:
5420 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5421 ratio = BXT_DE_PLL_RATIO(60);
5422 break;
5423 case 384000:
5424 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5425 ratio = BXT_DE_PLL_RATIO(60);
5426 break;
5427 case 576000:
5428 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5429 ratio = BXT_DE_PLL_RATIO(60);
5430 break;
5431 case 624000:
5432 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5433 ratio = BXT_DE_PLL_RATIO(65);
5434 break;
5435 case 19200:
5436 /*
5437 * Bypass frequency with DE PLL disabled. Init ratio, divider
5438 * to suppress GCC warning.
5439 */
5440 ratio = 0;
5441 divider = 0;
5442 break;
5443 default:
5444 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5445
5446 return;
5447 }
5448
5449 mutex_lock(&dev_priv->rps.hw_lock);
5450 /* Inform power controller of upcoming frequency change */
5451 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5452 0x80000000);
5453 mutex_unlock(&dev_priv->rps.hw_lock);
5454
5455 if (ret) {
5456 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5457 ret, frequency);
5458 return;
5459 }
5460
5461 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5462 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5463 current_freq = current_freq * 500 + 1000;
5464
5465 /*
5466 * DE PLL has to be disabled when
5467 * - setting to 19.2MHz (bypass, PLL isn't used)
5468 * - before setting to 624MHz (PLL needs toggling)
5469 * - before setting to any frequency from 624MHz (PLL needs toggling)
5470 */
5471 if (frequency == 19200 || frequency == 624000 ||
5472 current_freq == 624000) {
5473 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5474 /* Timeout 200us */
5475 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5476 1))
5477 DRM_ERROR("timout waiting for DE PLL unlock\n");
5478 }
5479
5480 if (frequency != 19200) {
5481 uint32_t val;
5482
5483 val = I915_READ(BXT_DE_PLL_CTL);
5484 val &= ~BXT_DE_PLL_RATIO_MASK;
5485 val |= ratio;
5486 I915_WRITE(BXT_DE_PLL_CTL, val);
5487
5488 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5489 /* Timeout 200us */
5490 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5491 DRM_ERROR("timeout waiting for DE PLL lock\n");
5492
5493 val = I915_READ(CDCLK_CTL);
5494 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5495 val |= divider;
5496 /*
5497 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5498 * enable otherwise.
5499 */
5500 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5501 if (frequency >= 500000)
5502 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5503
5504 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5505 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5506 val |= (frequency - 1000) / 500;
5507 I915_WRITE(CDCLK_CTL, val);
5508 }
5509
5510 mutex_lock(&dev_priv->rps.hw_lock);
5511 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5512 DIV_ROUND_UP(frequency, 25000));
5513 mutex_unlock(&dev_priv->rps.hw_lock);
5514
5515 if (ret) {
5516 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5517 ret, frequency);
5518 return;
5519 }
5520
a47871bd 5521 intel_update_cdclk(dev);
f8437dd1
VK
5522}
5523
5524void broxton_init_cdclk(struct drm_device *dev)
5525{
5526 struct drm_i915_private *dev_priv = dev->dev_private;
5527 uint32_t val;
5528
5529 /*
5530 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5531 * or else the reset will hang because there is no PCH to respond.
5532 * Move the handshake programming to initialization sequence.
5533 * Previously was left up to BIOS.
5534 */
5535 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5536 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5537 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5538
5539 /* Enable PG1 for cdclk */
5540 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5541
5542 /* check if cd clock is enabled */
5543 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5544 DRM_DEBUG_KMS("Display already initialized\n");
5545 return;
5546 }
5547
5548 /*
5549 * FIXME:
5550 * - The initial CDCLK needs to be read from VBT.
5551 * Need to make this change after VBT has changes for BXT.
5552 * - check if setting the max (or any) cdclk freq is really necessary
5553 * here, it belongs to modeset time
5554 */
5555 broxton_set_cdclk(dev, 624000);
5556
5557 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5558 POSTING_READ(DBUF_CTL);
5559
f8437dd1
VK
5560 udelay(10);
5561
5562 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5563 DRM_ERROR("DBuf power enable timeout!\n");
5564}
5565
5566void broxton_uninit_cdclk(struct drm_device *dev)
5567{
5568 struct drm_i915_private *dev_priv = dev->dev_private;
5569
5570 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5571 POSTING_READ(DBUF_CTL);
5572
f8437dd1
VK
5573 udelay(10);
5574
5575 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5576 DRM_ERROR("DBuf power disable timeout!\n");
5577
5578 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5579 broxton_set_cdclk(dev, 19200);
5580
5581 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5582}
5583
5d96d8af
DL
5584static const struct skl_cdclk_entry {
5585 unsigned int freq;
5586 unsigned int vco;
5587} skl_cdclk_frequencies[] = {
5588 { .freq = 308570, .vco = 8640 },
5589 { .freq = 337500, .vco = 8100 },
5590 { .freq = 432000, .vco = 8640 },
5591 { .freq = 450000, .vco = 8100 },
5592 { .freq = 540000, .vco = 8100 },
5593 { .freq = 617140, .vco = 8640 },
5594 { .freq = 675000, .vco = 8100 },
5595};
5596
5597static unsigned int skl_cdclk_decimal(unsigned int freq)
5598{
5599 return (freq - 1000) / 500;
5600}
5601
5602static unsigned int skl_cdclk_get_vco(unsigned int freq)
5603{
5604 unsigned int i;
5605
5606 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5607 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5608
5609 if (e->freq == freq)
5610 return e->vco;
5611 }
5612
5613 return 8100;
5614}
5615
5616static void
5617skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5618{
5619 unsigned int min_freq;
5620 u32 val;
5621
5622 /* select the minimum CDCLK before enabling DPLL 0 */
5623 val = I915_READ(CDCLK_CTL);
5624 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5625 val |= CDCLK_FREQ_337_308;
5626
5627 if (required_vco == 8640)
5628 min_freq = 308570;
5629 else
5630 min_freq = 337500;
5631
5632 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5633
5634 I915_WRITE(CDCLK_CTL, val);
5635 POSTING_READ(CDCLK_CTL);
5636
5637 /*
5638 * We always enable DPLL0 with the lowest link rate possible, but still
5639 * taking into account the VCO required to operate the eDP panel at the
5640 * desired frequency. The usual DP link rates operate with a VCO of
5641 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5642 * The modeset code is responsible for the selection of the exact link
5643 * rate later on, with the constraint of choosing a frequency that
5644 * works with required_vco.
5645 */
5646 val = I915_READ(DPLL_CTRL1);
5647
5648 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5649 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5650 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5651 if (required_vco == 8640)
5652 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5653 SKL_DPLL0);
5654 else
5655 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5656 SKL_DPLL0);
5657
5658 I915_WRITE(DPLL_CTRL1, val);
5659 POSTING_READ(DPLL_CTRL1);
5660
5661 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5662
5663 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5664 DRM_ERROR("DPLL0 not locked\n");
5665}
5666
5667static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5668{
5669 int ret;
5670 u32 val;
5671
5672 /* inform PCU we want to change CDCLK */
5673 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5674 mutex_lock(&dev_priv->rps.hw_lock);
5675 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5676 mutex_unlock(&dev_priv->rps.hw_lock);
5677
5678 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5679}
5680
5681static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5682{
5683 unsigned int i;
5684
5685 for (i = 0; i < 15; i++) {
5686 if (skl_cdclk_pcu_ready(dev_priv))
5687 return true;
5688 udelay(10);
5689 }
5690
5691 return false;
5692}
5693
5694static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5695{
560a7ae4 5696 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5697 u32 freq_select, pcu_ack;
5698
5699 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5700
5701 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5702 DRM_ERROR("failed to inform PCU about cdclk change\n");
5703 return;
5704 }
5705
5706 /* set CDCLK_CTL */
5707 switch(freq) {
5708 case 450000:
5709 case 432000:
5710 freq_select = CDCLK_FREQ_450_432;
5711 pcu_ack = 1;
5712 break;
5713 case 540000:
5714 freq_select = CDCLK_FREQ_540;
5715 pcu_ack = 2;
5716 break;
5717 case 308570:
5718 case 337500:
5719 default:
5720 freq_select = CDCLK_FREQ_337_308;
5721 pcu_ack = 0;
5722 break;
5723 case 617140:
5724 case 675000:
5725 freq_select = CDCLK_FREQ_675_617;
5726 pcu_ack = 3;
5727 break;
5728 }
5729
5730 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5731 POSTING_READ(CDCLK_CTL);
5732
5733 /* inform PCU of the change */
5734 mutex_lock(&dev_priv->rps.hw_lock);
5735 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5736 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5737
5738 intel_update_cdclk(dev);
5d96d8af
DL
5739}
5740
5741void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5742{
5743 /* disable DBUF power */
5744 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5745 POSTING_READ(DBUF_CTL);
5746
5747 udelay(10);
5748
5749 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5750 DRM_ERROR("DBuf power disable timeout\n");
5751
ab96c1ee
ID
5752 /* disable DPLL0 */
5753 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5754 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5755 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5756}
5757
5758void skl_init_cdclk(struct drm_i915_private *dev_priv)
5759{
5d96d8af
DL
5760 unsigned int required_vco;
5761
39d9b85a
GW
5762 /* DPLL0 not enabled (happens on early BIOS versions) */
5763 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5764 /* enable DPLL0 */
5765 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5766 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5767 }
5768
5d96d8af
DL
5769 /* set CDCLK to the frequency the BIOS chose */
5770 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5771
5772 /* enable DBUF power */
5773 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5774 POSTING_READ(DBUF_CTL);
5775
5776 udelay(10);
5777
5778 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5779 DRM_ERROR("DBuf power enable timeout\n");
5780}
5781
c73666f3
SK
5782int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5783{
5784 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5785 uint32_t cdctl = I915_READ(CDCLK_CTL);
5786 int freq = dev_priv->skl_boot_cdclk;
5787
f1b391a5
SK
5788 /*
5789 * check if the pre-os intialized the display
5790 * There is SWF18 scratchpad register defined which is set by the
5791 * pre-os which can be used by the OS drivers to check the status
5792 */
5793 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5794 goto sanitize;
5795
c73666f3
SK
5796 /* Is PLL enabled and locked ? */
5797 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5798 goto sanitize;
5799
5800 /* DPLL okay; verify the cdclock
5801 *
5802 * Noticed in some instances that the freq selection is correct but
5803 * decimal part is programmed wrong from BIOS where pre-os does not
5804 * enable display. Verify the same as well.
5805 */
5806 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5807 /* All well; nothing to sanitize */
5808 return false;
5809sanitize:
5810 /*
5811 * As of now initialize with max cdclk till
5812 * we get dynamic cdclk support
5813 * */
5814 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5815 skl_init_cdclk(dev_priv);
5816
5817 /* we did have to sanitize */
5818 return true;
5819}
5820
30a970c6
JB
5821/* Adjust CDclk dividers to allow high res or save power if possible */
5822static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5823{
5824 struct drm_i915_private *dev_priv = dev->dev_private;
5825 u32 val, cmd;
5826
164dfd28
VK
5827 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5828 != dev_priv->cdclk_freq);
d60c4473 5829
dfcab17e 5830 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5831 cmd = 2;
dfcab17e 5832 else if (cdclk == 266667)
30a970c6
JB
5833 cmd = 1;
5834 else
5835 cmd = 0;
5836
5837 mutex_lock(&dev_priv->rps.hw_lock);
5838 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5839 val &= ~DSPFREQGUAR_MASK;
5840 val |= (cmd << DSPFREQGUAR_SHIFT);
5841 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5842 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5843 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5844 50)) {
5845 DRM_ERROR("timed out waiting for CDclk change\n");
5846 }
5847 mutex_unlock(&dev_priv->rps.hw_lock);
5848
54433e91
VS
5849 mutex_lock(&dev_priv->sb_lock);
5850
dfcab17e 5851 if (cdclk == 400000) {
6bcda4f0 5852 u32 divider;
30a970c6 5853
6bcda4f0 5854 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5855
30a970c6
JB
5856 /* adjust cdclk divider */
5857 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5858 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5859 val |= divider;
5860 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5861
5862 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5863 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5864 50))
5865 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5866 }
5867
30a970c6
JB
5868 /* adjust self-refresh exit latency value */
5869 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5870 val &= ~0x7f;
5871
5872 /*
5873 * For high bandwidth configs, we set a higher latency in the bunit
5874 * so that the core display fetch happens in time to avoid underruns.
5875 */
dfcab17e 5876 if (cdclk == 400000)
30a970c6
JB
5877 val |= 4500 / 250; /* 4.5 usec */
5878 else
5879 val |= 3000 / 250; /* 3.0 usec */
5880 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5881
a580516d 5882 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5883
b6283055 5884 intel_update_cdclk(dev);
30a970c6
JB
5885}
5886
383c5a6a
VS
5887static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5888{
5889 struct drm_i915_private *dev_priv = dev->dev_private;
5890 u32 val, cmd;
5891
164dfd28
VK
5892 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5893 != dev_priv->cdclk_freq);
383c5a6a
VS
5894
5895 switch (cdclk) {
383c5a6a
VS
5896 case 333333:
5897 case 320000:
383c5a6a 5898 case 266667:
383c5a6a 5899 case 200000:
383c5a6a
VS
5900 break;
5901 default:
5f77eeb0 5902 MISSING_CASE(cdclk);
383c5a6a
VS
5903 return;
5904 }
5905
9d0d3fda
VS
5906 /*
5907 * Specs are full of misinformation, but testing on actual
5908 * hardware has shown that we just need to write the desired
5909 * CCK divider into the Punit register.
5910 */
5911 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5912
383c5a6a
VS
5913 mutex_lock(&dev_priv->rps.hw_lock);
5914 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5915 val &= ~DSPFREQGUAR_MASK_CHV;
5916 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5917 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5918 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5919 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5920 50)) {
5921 DRM_ERROR("timed out waiting for CDclk change\n");
5922 }
5923 mutex_unlock(&dev_priv->rps.hw_lock);
5924
b6283055 5925 intel_update_cdclk(dev);
383c5a6a
VS
5926}
5927
30a970c6
JB
5928static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5929 int max_pixclk)
5930{
6bcda4f0 5931 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5932 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5933
30a970c6
JB
5934 /*
5935 * Really only a few cases to deal with, as only 4 CDclks are supported:
5936 * 200MHz
5937 * 267MHz
29dc7ef3 5938 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5939 * 400MHz (VLV only)
5940 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5941 * of the lower bin and adjust if needed.
e37c67a1
VS
5942 *
5943 * We seem to get an unstable or solid color picture at 200MHz.
5944 * Not sure what's wrong. For now use 200MHz only when all pipes
5945 * are off.
30a970c6 5946 */
6cca3195
VS
5947 if (!IS_CHERRYVIEW(dev_priv) &&
5948 max_pixclk > freq_320*limit/100)
dfcab17e 5949 return 400000;
6cca3195 5950 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5951 return freq_320;
e37c67a1 5952 else if (max_pixclk > 0)
dfcab17e 5953 return 266667;
e37c67a1
VS
5954 else
5955 return 200000;
30a970c6
JB
5956}
5957
f8437dd1
VK
5958static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5959 int max_pixclk)
5960{
5961 /*
5962 * FIXME:
5963 * - remove the guardband, it's not needed on BXT
5964 * - set 19.2MHz bypass frequency if there are no active pipes
5965 */
5966 if (max_pixclk > 576000*9/10)
5967 return 624000;
5968 else if (max_pixclk > 384000*9/10)
5969 return 576000;
5970 else if (max_pixclk > 288000*9/10)
5971 return 384000;
5972 else if (max_pixclk > 144000*9/10)
5973 return 288000;
5974 else
5975 return 144000;
5976}
5977
e8788cbc 5978/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
5979static int intel_mode_max_pixclk(struct drm_device *dev,
5980 struct drm_atomic_state *state)
30a970c6 5981{
565602d7
ML
5982 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5983 struct drm_i915_private *dev_priv = dev->dev_private;
5984 struct drm_crtc *crtc;
5985 struct drm_crtc_state *crtc_state;
5986 unsigned max_pixclk = 0, i;
5987 enum pipe pipe;
30a970c6 5988
565602d7
ML
5989 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5990 sizeof(intel_state->min_pixclk));
304603f4 5991
565602d7
ML
5992 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5993 int pixclk = 0;
5994
5995 if (crtc_state->enable)
5996 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 5997
565602d7 5998 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
5999 }
6000
565602d7
ML
6001 for_each_pipe(dev_priv, pipe)
6002 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6003
30a970c6
JB
6004 return max_pixclk;
6005}
6006
27c329ed 6007static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6008{
27c329ed
ML
6009 struct drm_device *dev = state->dev;
6010 struct drm_i915_private *dev_priv = dev->dev_private;
6011 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6012 struct intel_atomic_state *intel_state =
6013 to_intel_atomic_state(state);
30a970c6 6014
304603f4
ACO
6015 if (max_pixclk < 0)
6016 return max_pixclk;
30a970c6 6017
1a617b77 6018 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6019 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6020
1a617b77
ML
6021 if (!intel_state->active_crtcs)
6022 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6023
27c329ed
ML
6024 return 0;
6025}
304603f4 6026
27c329ed
ML
6027static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6028{
6029 struct drm_device *dev = state->dev;
6030 struct drm_i915_private *dev_priv = dev->dev_private;
6031 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6032 struct intel_atomic_state *intel_state =
6033 to_intel_atomic_state(state);
85a96e7a 6034
27c329ed
ML
6035 if (max_pixclk < 0)
6036 return max_pixclk;
85a96e7a 6037
1a617b77 6038 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6039 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 6040
1a617b77
ML
6041 if (!intel_state->active_crtcs)
6042 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
6043
27c329ed 6044 return 0;
30a970c6
JB
6045}
6046
1e69cd74
VS
6047static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6048{
6049 unsigned int credits, default_credits;
6050
6051 if (IS_CHERRYVIEW(dev_priv))
6052 default_credits = PFI_CREDIT(12);
6053 else
6054 default_credits = PFI_CREDIT(8);
6055
bfa7df01 6056 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6057 /* CHV suggested value is 31 or 63 */
6058 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6059 credits = PFI_CREDIT_63;
1e69cd74
VS
6060 else
6061 credits = PFI_CREDIT(15);
6062 } else {
6063 credits = default_credits;
6064 }
6065
6066 /*
6067 * WA - write default credits before re-programming
6068 * FIXME: should we also set the resend bit here?
6069 */
6070 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6071 default_credits);
6072
6073 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6074 credits | PFI_CREDIT_RESEND);
6075
6076 /*
6077 * FIXME is this guaranteed to clear
6078 * immediately or should we poll for it?
6079 */
6080 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6081}
6082
27c329ed 6083static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6084{
a821fc46 6085 struct drm_device *dev = old_state->dev;
30a970c6 6086 struct drm_i915_private *dev_priv = dev->dev_private;
1a617b77
ML
6087 struct intel_atomic_state *old_intel_state =
6088 to_intel_atomic_state(old_state);
6089 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6090
27c329ed
ML
6091 /*
6092 * FIXME: We can end up here with all power domains off, yet
6093 * with a CDCLK frequency other than the minimum. To account
6094 * for this take the PIPE-A power domain, which covers the HW
6095 * blocks needed for the following programming. This can be
6096 * removed once it's guaranteed that we get here either with
6097 * the minimum CDCLK set, or the required power domains
6098 * enabled.
6099 */
6100 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6101
27c329ed
ML
6102 if (IS_CHERRYVIEW(dev))
6103 cherryview_set_cdclk(dev, req_cdclk);
6104 else
6105 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6106
27c329ed 6107 vlv_program_pfi_credits(dev_priv);
1e69cd74 6108
27c329ed 6109 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6110}
6111
89b667f8
JB
6112static void valleyview_crtc_enable(struct drm_crtc *crtc)
6113{
6114 struct drm_device *dev = crtc->dev;
a72e4c9f 6115 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6117 struct intel_encoder *encoder;
6118 int pipe = intel_crtc->pipe;
89b667f8 6119
53d9f4e9 6120 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6121 return;
6122
6e3c9717 6123 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6124 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6125
6126 intel_set_pipe_timings(intel_crtc);
6127
c14b0485
VS
6128 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6129 struct drm_i915_private *dev_priv = dev->dev_private;
6130
6131 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6132 I915_WRITE(CHV_CANVAS(pipe), 0);
6133 }
6134
5b18e57c
DV
6135 i9xx_set_pipeconf(intel_crtc);
6136
89b667f8 6137 intel_crtc->active = true;
89b667f8 6138
a72e4c9f 6139 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6140
89b667f8
JB
6141 for_each_encoder_on_crtc(dev, crtc, encoder)
6142 if (encoder->pre_pll_enable)
6143 encoder->pre_pll_enable(encoder);
6144
a65347ba 6145 if (!intel_crtc->config->has_dsi_encoder) {
c0b4c660
VS
6146 if (IS_CHERRYVIEW(dev)) {
6147 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6148 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6149 } else {
6150 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6151 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6152 }
9d556c99 6153 }
89b667f8
JB
6154
6155 for_each_encoder_on_crtc(dev, crtc, encoder)
6156 if (encoder->pre_enable)
6157 encoder->pre_enable(encoder);
6158
2dd24552
JB
6159 i9xx_pfit_enable(intel_crtc);
6160
63cbb074
VS
6161 intel_crtc_load_lut(crtc);
6162
caed361d 6163 intel_update_watermarks(crtc);
e1fdc473 6164 intel_enable_pipe(intel_crtc);
be6a6f8e 6165
4b3a9526
VS
6166 assert_vblank_disabled(crtc);
6167 drm_crtc_vblank_on(crtc);
6168
f9b61ff6
DV
6169 for_each_encoder_on_crtc(dev, crtc, encoder)
6170 encoder->enable(encoder);
89b667f8
JB
6171}
6172
f13c2ef3
DV
6173static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6174{
6175 struct drm_device *dev = crtc->base.dev;
6176 struct drm_i915_private *dev_priv = dev->dev_private;
6177
6e3c9717
ACO
6178 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6179 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6180}
6181
0b8765c6 6182static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6183{
6184 struct drm_device *dev = crtc->dev;
a72e4c9f 6185 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6187 struct intel_encoder *encoder;
79e53945 6188 int pipe = intel_crtc->pipe;
79e53945 6189
53d9f4e9 6190 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6191 return;
6192
f13c2ef3
DV
6193 i9xx_set_pll_dividers(intel_crtc);
6194
6e3c9717 6195 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6196 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6197
6198 intel_set_pipe_timings(intel_crtc);
6199
5b18e57c
DV
6200 i9xx_set_pipeconf(intel_crtc);
6201
f7abfe8b 6202 intel_crtc->active = true;
6b383a7f 6203
4a3436e8 6204 if (!IS_GEN2(dev))
a72e4c9f 6205 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6206
9d6d9f19
MK
6207 for_each_encoder_on_crtc(dev, crtc, encoder)
6208 if (encoder->pre_enable)
6209 encoder->pre_enable(encoder);
6210
f6736a1a
DV
6211 i9xx_enable_pll(intel_crtc);
6212
2dd24552
JB
6213 i9xx_pfit_enable(intel_crtc);
6214
63cbb074
VS
6215 intel_crtc_load_lut(crtc);
6216
f37fcc2a 6217 intel_update_watermarks(crtc);
e1fdc473 6218 intel_enable_pipe(intel_crtc);
be6a6f8e 6219
4b3a9526
VS
6220 assert_vblank_disabled(crtc);
6221 drm_crtc_vblank_on(crtc);
6222
f9b61ff6
DV
6223 for_each_encoder_on_crtc(dev, crtc, encoder)
6224 encoder->enable(encoder);
0b8765c6 6225}
79e53945 6226
87476d63
DV
6227static void i9xx_pfit_disable(struct intel_crtc *crtc)
6228{
6229 struct drm_device *dev = crtc->base.dev;
6230 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6231
6e3c9717 6232 if (!crtc->config->gmch_pfit.control)
328d8e82 6233 return;
87476d63 6234
328d8e82 6235 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6236
328d8e82
DV
6237 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6238 I915_READ(PFIT_CONTROL));
6239 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6240}
6241
0b8765c6
JB
6242static void i9xx_crtc_disable(struct drm_crtc *crtc)
6243{
6244 struct drm_device *dev = crtc->dev;
6245 struct drm_i915_private *dev_priv = dev->dev_private;
6246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6247 struct intel_encoder *encoder;
0b8765c6 6248 int pipe = intel_crtc->pipe;
ef9c3aee 6249
6304cd91
VS
6250 /*
6251 * On gen2 planes are double buffered but the pipe isn't, so we must
6252 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6253 * We also need to wait on all gmch platforms because of the
6254 * self-refresh mode constraint explained above.
6304cd91 6255 */
564ed191 6256 intel_wait_for_vblank(dev, pipe);
6304cd91 6257
4b3a9526
VS
6258 for_each_encoder_on_crtc(dev, crtc, encoder)
6259 encoder->disable(encoder);
6260
f9b61ff6
DV
6261 drm_crtc_vblank_off(crtc);
6262 assert_vblank_disabled(crtc);
6263
575f7ab7 6264 intel_disable_pipe(intel_crtc);
24a1f16d 6265
87476d63 6266 i9xx_pfit_disable(intel_crtc);
24a1f16d 6267
89b667f8
JB
6268 for_each_encoder_on_crtc(dev, crtc, encoder)
6269 if (encoder->post_disable)
6270 encoder->post_disable(encoder);
6271
a65347ba 6272 if (!intel_crtc->config->has_dsi_encoder) {
076ed3b2
CML
6273 if (IS_CHERRYVIEW(dev))
6274 chv_disable_pll(dev_priv, pipe);
6275 else if (IS_VALLEYVIEW(dev))
6276 vlv_disable_pll(dev_priv, pipe);
6277 else
1c4e0274 6278 i9xx_disable_pll(intel_crtc);
076ed3b2 6279 }
0b8765c6 6280
d6db995f
VS
6281 for_each_encoder_on_crtc(dev, crtc, encoder)
6282 if (encoder->post_pll_disable)
6283 encoder->post_pll_disable(encoder);
6284
4a3436e8 6285 if (!IS_GEN2(dev))
a72e4c9f 6286 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6287}
6288
b17d48e2
ML
6289static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6290{
842e0307 6291 struct intel_encoder *encoder;
b17d48e2
ML
6292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6293 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6294 enum intel_display_power_domain domain;
6295 unsigned long domains;
6296
6297 if (!intel_crtc->active)
6298 return;
6299
a539205a 6300 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6301 WARN_ON(intel_crtc->unpin_work);
6302
2622a081 6303 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6304
6305 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6306 to_intel_plane_state(crtc->primary->state)->visible = false;
a539205a
ML
6307 }
6308
b17d48e2 6309 dev_priv->display.crtc_disable(crtc);
842e0307
ML
6310
6311 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6312 crtc->base.id);
6313
6314 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6315 crtc->state->active = false;
37d9078b 6316 intel_crtc->active = false;
842e0307
ML
6317 crtc->enabled = false;
6318 crtc->state->connector_mask = 0;
6319 crtc->state->encoder_mask = 0;
6320
6321 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6322 encoder->base.crtc = NULL;
6323
58f9c0bc 6324 intel_fbc_disable(intel_crtc);
37d9078b 6325 intel_update_watermarks(crtc);
1f7457b1 6326 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6327
6328 domains = intel_crtc->enabled_power_domains;
6329 for_each_power_domain(domain, domains)
6330 intel_display_power_put(dev_priv, domain);
6331 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6332
6333 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6334 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6335}
6336
6b72d486
ML
6337/*
6338 * turn all crtc's off, but do not adjust state
6339 * This has to be paired with a call to intel_modeset_setup_hw_state.
6340 */
70e0bd74 6341int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6342{
e2c8b870 6343 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6344 struct drm_atomic_state *state;
e2c8b870 6345 int ret;
70e0bd74 6346
e2c8b870
ML
6347 state = drm_atomic_helper_suspend(dev);
6348 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6349 if (ret)
6350 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6351 else
6352 dev_priv->modeset_restore_state = state;
70e0bd74 6353 return ret;
ee7b9f93
JB
6354}
6355
ea5b213a 6356void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6357{
4ef69c7a 6358 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6359
ea5b213a
CW
6360 drm_encoder_cleanup(encoder);
6361 kfree(intel_encoder);
7e7d76c3
JB
6362}
6363
0a91ca29
DV
6364/* Cross check the actual hw state with our own modeset state tracking (and it's
6365 * internal consistency). */
b980514c 6366static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6367{
35dd3c64
ML
6368 struct drm_crtc *crtc = connector->base.state->crtc;
6369
6370 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6371 connector->base.base.id,
6372 connector->base.name);
6373
0a91ca29 6374 if (connector->get_hw_state(connector)) {
e85376cb 6375 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6376 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6377
35dd3c64
ML
6378 I915_STATE_WARN(!crtc,
6379 "connector enabled without attached crtc\n");
0a91ca29 6380
35dd3c64
ML
6381 if (!crtc)
6382 return;
6383
6384 I915_STATE_WARN(!crtc->state->active,
6385 "connector is active, but attached crtc isn't\n");
6386
e85376cb 6387 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6388 return;
6389
e85376cb 6390 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6391 "atomic encoder doesn't match attached encoder\n");
6392
e85376cb 6393 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6394 "attached encoder crtc differs from connector crtc\n");
6395 } else {
4d688a2a
ML
6396 I915_STATE_WARN(crtc && crtc->state->active,
6397 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6398 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6399 "best encoder set without crtc!\n");
0a91ca29 6400 }
79e53945
JB
6401}
6402
08d9bc92
ACO
6403int intel_connector_init(struct intel_connector *connector)
6404{
5350a031 6405 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 6406
5350a031 6407 if (!connector->base.state)
08d9bc92
ACO
6408 return -ENOMEM;
6409
08d9bc92
ACO
6410 return 0;
6411}
6412
6413struct intel_connector *intel_connector_alloc(void)
6414{
6415 struct intel_connector *connector;
6416
6417 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6418 if (!connector)
6419 return NULL;
6420
6421 if (intel_connector_init(connector) < 0) {
6422 kfree(connector);
6423 return NULL;
6424 }
6425
6426 return connector;
6427}
6428
f0947c37
DV
6429/* Simple connector->get_hw_state implementation for encoders that support only
6430 * one connector and no cloning and hence the encoder state determines the state
6431 * of the connector. */
6432bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6433{
24929352 6434 enum pipe pipe = 0;
f0947c37 6435 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6436
f0947c37 6437 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6438}
6439
6d293983 6440static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6441{
6d293983
ACO
6442 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6443 return crtc_state->fdi_lanes;
d272ddfa
VS
6444
6445 return 0;
6446}
6447
6d293983 6448static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6449 struct intel_crtc_state *pipe_config)
1857e1da 6450{
6d293983
ACO
6451 struct drm_atomic_state *state = pipe_config->base.state;
6452 struct intel_crtc *other_crtc;
6453 struct intel_crtc_state *other_crtc_state;
6454
1857e1da
DV
6455 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6456 pipe_name(pipe), pipe_config->fdi_lanes);
6457 if (pipe_config->fdi_lanes > 4) {
6458 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6459 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6460 return -EINVAL;
1857e1da
DV
6461 }
6462
bafb6553 6463 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6464 if (pipe_config->fdi_lanes > 2) {
6465 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6466 pipe_config->fdi_lanes);
6d293983 6467 return -EINVAL;
1857e1da 6468 } else {
6d293983 6469 return 0;
1857e1da
DV
6470 }
6471 }
6472
6473 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6474 return 0;
1857e1da
DV
6475
6476 /* Ivybridge 3 pipe is really complicated */
6477 switch (pipe) {
6478 case PIPE_A:
6d293983 6479 return 0;
1857e1da 6480 case PIPE_B:
6d293983
ACO
6481 if (pipe_config->fdi_lanes <= 2)
6482 return 0;
6483
6484 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6485 other_crtc_state =
6486 intel_atomic_get_crtc_state(state, other_crtc);
6487 if (IS_ERR(other_crtc_state))
6488 return PTR_ERR(other_crtc_state);
6489
6490 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6491 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6492 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6493 return -EINVAL;
1857e1da 6494 }
6d293983 6495 return 0;
1857e1da 6496 case PIPE_C:
251cc67c
VS
6497 if (pipe_config->fdi_lanes > 2) {
6498 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6499 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6500 return -EINVAL;
251cc67c 6501 }
6d293983
ACO
6502
6503 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6504 other_crtc_state =
6505 intel_atomic_get_crtc_state(state, other_crtc);
6506 if (IS_ERR(other_crtc_state))
6507 return PTR_ERR(other_crtc_state);
6508
6509 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6510 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6511 return -EINVAL;
1857e1da 6512 }
6d293983 6513 return 0;
1857e1da
DV
6514 default:
6515 BUG();
6516 }
6517}
6518
e29c22c0
DV
6519#define RETRY 1
6520static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6521 struct intel_crtc_state *pipe_config)
877d48d5 6522{
1857e1da 6523 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6524 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6525 int lane, link_bw, fdi_dotclock, ret;
6526 bool needs_recompute = false;
877d48d5 6527
e29c22c0 6528retry:
877d48d5
DV
6529 /* FDI is a binary signal running at ~2.7GHz, encoding
6530 * each output octet as 10 bits. The actual frequency
6531 * is stored as a divider into a 100MHz clock, and the
6532 * mode pixel clock is stored in units of 1KHz.
6533 * Hence the bw of each lane in terms of the mode signal
6534 * is:
6535 */
21a727b3 6536 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 6537
241bfc38 6538 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6539
2bd89a07 6540 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6541 pipe_config->pipe_bpp);
6542
6543 pipe_config->fdi_lanes = lane;
6544
2bd89a07 6545 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6546 link_bw, &pipe_config->fdi_m_n);
1857e1da 6547
e3b247da 6548 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 6549 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6550 pipe_config->pipe_bpp -= 2*3;
6551 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6552 pipe_config->pipe_bpp);
6553 needs_recompute = true;
6554 pipe_config->bw_constrained = true;
6555
6556 goto retry;
6557 }
6558
6559 if (needs_recompute)
6560 return RETRY;
6561
6d293983 6562 return ret;
877d48d5
DV
6563}
6564
8cfb3407
VS
6565static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6566 struct intel_crtc_state *pipe_config)
6567{
6568 if (pipe_config->pipe_bpp > 24)
6569 return false;
6570
6571 /* HSW can handle pixel rate up to cdclk? */
6572 if (IS_HASWELL(dev_priv->dev))
6573 return true;
6574
6575 /*
b432e5cf
VS
6576 * We compare against max which means we must take
6577 * the increased cdclk requirement into account when
6578 * calculating the new cdclk.
6579 *
6580 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6581 */
6582 return ilk_pipe_pixel_rate(pipe_config) <=
6583 dev_priv->max_cdclk_freq * 95 / 100;
6584}
6585
42db64ef 6586static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6587 struct intel_crtc_state *pipe_config)
42db64ef 6588{
8cfb3407
VS
6589 struct drm_device *dev = crtc->base.dev;
6590 struct drm_i915_private *dev_priv = dev->dev_private;
6591
d330a953 6592 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6593 hsw_crtc_supports_ips(crtc) &&
6594 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6595}
6596
39acb4aa
VS
6597static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6598{
6599 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6600
6601 /* GDG double wide on either pipe, otherwise pipe A only */
6602 return INTEL_INFO(dev_priv)->gen < 4 &&
6603 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6604}
6605
a43f6e0f 6606static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6607 struct intel_crtc_state *pipe_config)
79e53945 6608{
a43f6e0f 6609 struct drm_device *dev = crtc->base.dev;
8bd31e67 6610 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6611 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6612
ad3a4479 6613 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6614 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6615 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6616
6617 /*
39acb4aa 6618 * Enable double wide mode when the dot clock
cf532bb2 6619 * is > 90% of the (display) core speed.
cf532bb2 6620 */
39acb4aa
VS
6621 if (intel_crtc_supports_double_wide(crtc) &&
6622 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6623 clock_limit *= 2;
cf532bb2 6624 pipe_config->double_wide = true;
ad3a4479
VS
6625 }
6626
39acb4aa
VS
6627 if (adjusted_mode->crtc_clock > clock_limit) {
6628 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6629 adjusted_mode->crtc_clock, clock_limit,
6630 yesno(pipe_config->double_wide));
e29c22c0 6631 return -EINVAL;
39acb4aa 6632 }
2c07245f 6633 }
89749350 6634
1d1d0e27
VS
6635 /*
6636 * Pipe horizontal size must be even in:
6637 * - DVO ganged mode
6638 * - LVDS dual channel mode
6639 * - Double wide pipe
6640 */
a93e255f 6641 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6642 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6643 pipe_config->pipe_src_w &= ~1;
6644
8693a824
DL
6645 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6646 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6647 */
6648 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6649 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6650 return -EINVAL;
44f46b42 6651
f5adf94e 6652 if (HAS_IPS(dev))
a43f6e0f
DV
6653 hsw_compute_ips_config(crtc, pipe_config);
6654
877d48d5 6655 if (pipe_config->has_pch_encoder)
a43f6e0f 6656 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6657
cf5a15be 6658 return 0;
79e53945
JB
6659}
6660
1652d19e
VS
6661static int skylake_get_display_clock_speed(struct drm_device *dev)
6662{
6663 struct drm_i915_private *dev_priv = to_i915(dev);
6664 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6665 uint32_t cdctl = I915_READ(CDCLK_CTL);
6666 uint32_t linkrate;
6667
414355a7 6668 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6669 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6670
6671 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6672 return 540000;
6673
6674 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6675 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6676
71cd8423
DL
6677 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6678 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6679 /* vco 8640 */
6680 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6681 case CDCLK_FREQ_450_432:
6682 return 432000;
6683 case CDCLK_FREQ_337_308:
6684 return 308570;
6685 case CDCLK_FREQ_675_617:
6686 return 617140;
6687 default:
6688 WARN(1, "Unknown cd freq selection\n");
6689 }
6690 } else {
6691 /* vco 8100 */
6692 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6693 case CDCLK_FREQ_450_432:
6694 return 450000;
6695 case CDCLK_FREQ_337_308:
6696 return 337500;
6697 case CDCLK_FREQ_675_617:
6698 return 675000;
6699 default:
6700 WARN(1, "Unknown cd freq selection\n");
6701 }
6702 }
6703
6704 /* error case, do as if DPLL0 isn't enabled */
6705 return 24000;
6706}
6707
acd3f3d3
BP
6708static int broxton_get_display_clock_speed(struct drm_device *dev)
6709{
6710 struct drm_i915_private *dev_priv = to_i915(dev);
6711 uint32_t cdctl = I915_READ(CDCLK_CTL);
6712 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6713 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6714 int cdclk;
6715
6716 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6717 return 19200;
6718
6719 cdclk = 19200 * pll_ratio / 2;
6720
6721 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6722 case BXT_CDCLK_CD2X_DIV_SEL_1:
6723 return cdclk; /* 576MHz or 624MHz */
6724 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6725 return cdclk * 2 / 3; /* 384MHz */
6726 case BXT_CDCLK_CD2X_DIV_SEL_2:
6727 return cdclk / 2; /* 288MHz */
6728 case BXT_CDCLK_CD2X_DIV_SEL_4:
6729 return cdclk / 4; /* 144MHz */
6730 }
6731
6732 /* error case, do as if DE PLL isn't enabled */
6733 return 19200;
6734}
6735
1652d19e
VS
6736static int broadwell_get_display_clock_speed(struct drm_device *dev)
6737{
6738 struct drm_i915_private *dev_priv = dev->dev_private;
6739 uint32_t lcpll = I915_READ(LCPLL_CTL);
6740 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6741
6742 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6743 return 800000;
6744 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6745 return 450000;
6746 else if (freq == LCPLL_CLK_FREQ_450)
6747 return 450000;
6748 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6749 return 540000;
6750 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6751 return 337500;
6752 else
6753 return 675000;
6754}
6755
6756static int haswell_get_display_clock_speed(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = dev->dev_private;
6759 uint32_t lcpll = I915_READ(LCPLL_CTL);
6760 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6761
6762 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6763 return 800000;
6764 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6765 return 450000;
6766 else if (freq == LCPLL_CLK_FREQ_450)
6767 return 450000;
6768 else if (IS_HSW_ULT(dev))
6769 return 337500;
6770 else
6771 return 540000;
79e53945
JB
6772}
6773
25eb05fc
JB
6774static int valleyview_get_display_clock_speed(struct drm_device *dev)
6775{
bfa7df01
VS
6776 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6777 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6778}
6779
b37a6434
VS
6780static int ilk_get_display_clock_speed(struct drm_device *dev)
6781{
6782 return 450000;
6783}
6784
e70236a8
JB
6785static int i945_get_display_clock_speed(struct drm_device *dev)
6786{
6787 return 400000;
6788}
79e53945 6789
e70236a8 6790static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6791{
e907f170 6792 return 333333;
e70236a8 6793}
79e53945 6794
e70236a8
JB
6795static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6796{
6797 return 200000;
6798}
79e53945 6799
257a7ffc
DV
6800static int pnv_get_display_clock_speed(struct drm_device *dev)
6801{
6802 u16 gcfgc = 0;
6803
6804 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6805
6806 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6807 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6808 return 266667;
257a7ffc 6809 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6810 return 333333;
257a7ffc 6811 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6812 return 444444;
257a7ffc
DV
6813 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6814 return 200000;
6815 default:
6816 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6817 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6818 return 133333;
257a7ffc 6819 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6820 return 166667;
257a7ffc
DV
6821 }
6822}
6823
e70236a8
JB
6824static int i915gm_get_display_clock_speed(struct drm_device *dev)
6825{
6826 u16 gcfgc = 0;
79e53945 6827
e70236a8
JB
6828 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6829
6830 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6831 return 133333;
e70236a8
JB
6832 else {
6833 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6834 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6835 return 333333;
e70236a8
JB
6836 default:
6837 case GC_DISPLAY_CLOCK_190_200_MHZ:
6838 return 190000;
79e53945 6839 }
e70236a8
JB
6840 }
6841}
6842
6843static int i865_get_display_clock_speed(struct drm_device *dev)
6844{
e907f170 6845 return 266667;
e70236a8
JB
6846}
6847
1b1d2716 6848static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6849{
6850 u16 hpllcc = 0;
1b1d2716 6851
65cd2b3f
VS
6852 /*
6853 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6854 * encoding is different :(
6855 * FIXME is this the right way to detect 852GM/852GMV?
6856 */
6857 if (dev->pdev->revision == 0x1)
6858 return 133333;
6859
1b1d2716
VS
6860 pci_bus_read_config_word(dev->pdev->bus,
6861 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6862
e70236a8
JB
6863 /* Assume that the hardware is in the high speed state. This
6864 * should be the default.
6865 */
6866 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6867 case GC_CLOCK_133_200:
1b1d2716 6868 case GC_CLOCK_133_200_2:
e70236a8
JB
6869 case GC_CLOCK_100_200:
6870 return 200000;
6871 case GC_CLOCK_166_250:
6872 return 250000;
6873 case GC_CLOCK_100_133:
e907f170 6874 return 133333;
1b1d2716
VS
6875 case GC_CLOCK_133_266:
6876 case GC_CLOCK_133_266_2:
6877 case GC_CLOCK_166_266:
6878 return 266667;
e70236a8 6879 }
79e53945 6880
e70236a8
JB
6881 /* Shouldn't happen */
6882 return 0;
6883}
79e53945 6884
e70236a8
JB
6885static int i830_get_display_clock_speed(struct drm_device *dev)
6886{
e907f170 6887 return 133333;
79e53945
JB
6888}
6889
34edce2f
VS
6890static unsigned int intel_hpll_vco(struct drm_device *dev)
6891{
6892 struct drm_i915_private *dev_priv = dev->dev_private;
6893 static const unsigned int blb_vco[8] = {
6894 [0] = 3200000,
6895 [1] = 4000000,
6896 [2] = 5333333,
6897 [3] = 4800000,
6898 [4] = 6400000,
6899 };
6900 static const unsigned int pnv_vco[8] = {
6901 [0] = 3200000,
6902 [1] = 4000000,
6903 [2] = 5333333,
6904 [3] = 4800000,
6905 [4] = 2666667,
6906 };
6907 static const unsigned int cl_vco[8] = {
6908 [0] = 3200000,
6909 [1] = 4000000,
6910 [2] = 5333333,
6911 [3] = 6400000,
6912 [4] = 3333333,
6913 [5] = 3566667,
6914 [6] = 4266667,
6915 };
6916 static const unsigned int elk_vco[8] = {
6917 [0] = 3200000,
6918 [1] = 4000000,
6919 [2] = 5333333,
6920 [3] = 4800000,
6921 };
6922 static const unsigned int ctg_vco[8] = {
6923 [0] = 3200000,
6924 [1] = 4000000,
6925 [2] = 5333333,
6926 [3] = 6400000,
6927 [4] = 2666667,
6928 [5] = 4266667,
6929 };
6930 const unsigned int *vco_table;
6931 unsigned int vco;
6932 uint8_t tmp = 0;
6933
6934 /* FIXME other chipsets? */
6935 if (IS_GM45(dev))
6936 vco_table = ctg_vco;
6937 else if (IS_G4X(dev))
6938 vco_table = elk_vco;
6939 else if (IS_CRESTLINE(dev))
6940 vco_table = cl_vco;
6941 else if (IS_PINEVIEW(dev))
6942 vco_table = pnv_vco;
6943 else if (IS_G33(dev))
6944 vco_table = blb_vco;
6945 else
6946 return 0;
6947
6948 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6949
6950 vco = vco_table[tmp & 0x7];
6951 if (vco == 0)
6952 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6953 else
6954 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6955
6956 return vco;
6957}
6958
6959static int gm45_get_display_clock_speed(struct drm_device *dev)
6960{
6961 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6962 uint16_t tmp = 0;
6963
6964 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6965
6966 cdclk_sel = (tmp >> 12) & 0x1;
6967
6968 switch (vco) {
6969 case 2666667:
6970 case 4000000:
6971 case 5333333:
6972 return cdclk_sel ? 333333 : 222222;
6973 case 3200000:
6974 return cdclk_sel ? 320000 : 228571;
6975 default:
6976 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6977 return 222222;
6978 }
6979}
6980
6981static int i965gm_get_display_clock_speed(struct drm_device *dev)
6982{
6983 static const uint8_t div_3200[] = { 16, 10, 8 };
6984 static const uint8_t div_4000[] = { 20, 12, 10 };
6985 static const uint8_t div_5333[] = { 24, 16, 14 };
6986 const uint8_t *div_table;
6987 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6988 uint16_t tmp = 0;
6989
6990 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6991
6992 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6993
6994 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6995 goto fail;
6996
6997 switch (vco) {
6998 case 3200000:
6999 div_table = div_3200;
7000 break;
7001 case 4000000:
7002 div_table = div_4000;
7003 break;
7004 case 5333333:
7005 div_table = div_5333;
7006 break;
7007 default:
7008 goto fail;
7009 }
7010
7011 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7012
caf4e252 7013fail:
34edce2f
VS
7014 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7015 return 200000;
7016}
7017
7018static int g33_get_display_clock_speed(struct drm_device *dev)
7019{
7020 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7021 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7022 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7023 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7024 const uint8_t *div_table;
7025 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7026 uint16_t tmp = 0;
7027
7028 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7029
7030 cdclk_sel = (tmp >> 4) & 0x7;
7031
7032 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7033 goto fail;
7034
7035 switch (vco) {
7036 case 3200000:
7037 div_table = div_3200;
7038 break;
7039 case 4000000:
7040 div_table = div_4000;
7041 break;
7042 case 4800000:
7043 div_table = div_4800;
7044 break;
7045 case 5333333:
7046 div_table = div_5333;
7047 break;
7048 default:
7049 goto fail;
7050 }
7051
7052 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7053
caf4e252 7054fail:
34edce2f
VS
7055 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7056 return 190476;
7057}
7058
2c07245f 7059static void
a65851af 7060intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7061{
a65851af
VS
7062 while (*num > DATA_LINK_M_N_MASK ||
7063 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7064 *num >>= 1;
7065 *den >>= 1;
7066 }
7067}
7068
a65851af
VS
7069static void compute_m_n(unsigned int m, unsigned int n,
7070 uint32_t *ret_m, uint32_t *ret_n)
7071{
7072 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7073 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7074 intel_reduce_m_n_ratio(ret_m, ret_n);
7075}
7076
e69d0bc1
DV
7077void
7078intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7079 int pixel_clock, int link_clock,
7080 struct intel_link_m_n *m_n)
2c07245f 7081{
e69d0bc1 7082 m_n->tu = 64;
a65851af
VS
7083
7084 compute_m_n(bits_per_pixel * pixel_clock,
7085 link_clock * nlanes * 8,
7086 &m_n->gmch_m, &m_n->gmch_n);
7087
7088 compute_m_n(pixel_clock, link_clock,
7089 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7090}
7091
a7615030
CW
7092static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7093{
d330a953
JN
7094 if (i915.panel_use_ssc >= 0)
7095 return i915.panel_use_ssc != 0;
41aa3448 7096 return dev_priv->vbt.lvds_use_ssc
435793df 7097 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7098}
7099
a93e255f
ACO
7100static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7101 int num_connectors)
c65d77d8 7102{
a93e255f 7103 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7104 struct drm_i915_private *dev_priv = dev->dev_private;
7105 int refclk;
7106
a93e255f
ACO
7107 WARN_ON(!crtc_state->base.state);
7108
666a4537 7109 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7110 refclk = 100000;
a93e255f 7111 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7112 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7113 refclk = dev_priv->vbt.lvds_ssc_freq;
7114 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7115 } else if (!IS_GEN2(dev)) {
7116 refclk = 96000;
7117 } else {
7118 refclk = 48000;
7119 }
7120
7121 return refclk;
7122}
7123
7429e9d4 7124static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7125{
7df00d7a 7126 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7127}
f47709a9 7128
7429e9d4
DV
7129static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7130{
7131 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7132}
7133
f47709a9 7134static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7135 struct intel_crtc_state *crtc_state,
a7516a05
JB
7136 intel_clock_t *reduced_clock)
7137{
f47709a9 7138 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7139 u32 fp, fp2 = 0;
7140
7141 if (IS_PINEVIEW(dev)) {
190f68c5 7142 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7143 if (reduced_clock)
7429e9d4 7144 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7145 } else {
190f68c5 7146 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7147 if (reduced_clock)
7429e9d4 7148 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7149 }
7150
190f68c5 7151 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7152
f47709a9 7153 crtc->lowfreq_avail = false;
a93e255f 7154 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7155 reduced_clock) {
190f68c5 7156 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7157 crtc->lowfreq_avail = true;
a7516a05 7158 } else {
190f68c5 7159 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7160 }
7161}
7162
5e69f97f
CML
7163static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7164 pipe)
89b667f8
JB
7165{
7166 u32 reg_val;
7167
7168 /*
7169 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7170 * and set it to a reasonable value instead.
7171 */
ab3c759a 7172 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7173 reg_val &= 0xffffff00;
7174 reg_val |= 0x00000030;
ab3c759a 7175 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7176
ab3c759a 7177 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7178 reg_val &= 0x8cffffff;
7179 reg_val = 0x8c000000;
ab3c759a 7180 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7181
ab3c759a 7182 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7183 reg_val &= 0xffffff00;
ab3c759a 7184 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7185
ab3c759a 7186 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7187 reg_val &= 0x00ffffff;
7188 reg_val |= 0xb0000000;
ab3c759a 7189 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7190}
7191
b551842d
DV
7192static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7193 struct intel_link_m_n *m_n)
7194{
7195 struct drm_device *dev = crtc->base.dev;
7196 struct drm_i915_private *dev_priv = dev->dev_private;
7197 int pipe = crtc->pipe;
7198
e3b95f1e
DV
7199 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7200 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7201 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7202 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7203}
7204
7205static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7206 struct intel_link_m_n *m_n,
7207 struct intel_link_m_n *m2_n2)
b551842d
DV
7208{
7209 struct drm_device *dev = crtc->base.dev;
7210 struct drm_i915_private *dev_priv = dev->dev_private;
7211 int pipe = crtc->pipe;
6e3c9717 7212 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7213
7214 if (INTEL_INFO(dev)->gen >= 5) {
7215 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7216 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7217 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7218 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7219 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7220 * for gen < 8) and if DRRS is supported (to make sure the
7221 * registers are not unnecessarily accessed).
7222 */
44395bfe 7223 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7224 crtc->config->has_drrs) {
f769cd24
VK
7225 I915_WRITE(PIPE_DATA_M2(transcoder),
7226 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7227 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7228 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7229 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7230 }
b551842d 7231 } else {
e3b95f1e
DV
7232 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7233 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7234 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7235 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7236 }
7237}
7238
fe3cd48d 7239void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7240{
fe3cd48d
R
7241 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7242
7243 if (m_n == M1_N1) {
7244 dp_m_n = &crtc->config->dp_m_n;
7245 dp_m2_n2 = &crtc->config->dp_m2_n2;
7246 } else if (m_n == M2_N2) {
7247
7248 /*
7249 * M2_N2 registers are not supported. Hence m2_n2 divider value
7250 * needs to be programmed into M1_N1.
7251 */
7252 dp_m_n = &crtc->config->dp_m2_n2;
7253 } else {
7254 DRM_ERROR("Unsupported divider value\n");
7255 return;
7256 }
7257
6e3c9717
ACO
7258 if (crtc->config->has_pch_encoder)
7259 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7260 else
fe3cd48d 7261 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7262}
7263
251ac862
DV
7264static void vlv_compute_dpll(struct intel_crtc *crtc,
7265 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7266{
7267 u32 dpll, dpll_md;
7268
7269 /*
7270 * Enable DPIO clock input. We should never disable the reference
7271 * clock for pipe B, since VGA hotplug / manual detection depends
7272 * on it.
7273 */
60bfe44f
VS
7274 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7275 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7276 /* We should never disable this, set it here for state tracking */
7277 if (crtc->pipe == PIPE_B)
7278 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7279 dpll |= DPLL_VCO_ENABLE;
d288f65f 7280 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7281
d288f65f 7282 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7283 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7284 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7285}
7286
d288f65f 7287static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7288 const struct intel_crtc_state *pipe_config)
a0c4da24 7289{
f47709a9 7290 struct drm_device *dev = crtc->base.dev;
a0c4da24 7291 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7292 int pipe = crtc->pipe;
bdd4b6a6 7293 u32 mdiv;
a0c4da24 7294 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7295 u32 coreclk, reg_val;
a0c4da24 7296
a580516d 7297 mutex_lock(&dev_priv->sb_lock);
09153000 7298
d288f65f
VS
7299 bestn = pipe_config->dpll.n;
7300 bestm1 = pipe_config->dpll.m1;
7301 bestm2 = pipe_config->dpll.m2;
7302 bestp1 = pipe_config->dpll.p1;
7303 bestp2 = pipe_config->dpll.p2;
a0c4da24 7304
89b667f8
JB
7305 /* See eDP HDMI DPIO driver vbios notes doc */
7306
7307 /* PLL B needs special handling */
bdd4b6a6 7308 if (pipe == PIPE_B)
5e69f97f 7309 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7310
7311 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7313
7314 /* Disable target IRef on PLL */
ab3c759a 7315 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7316 reg_val &= 0x00ffffff;
ab3c759a 7317 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7318
7319 /* Disable fast lock */
ab3c759a 7320 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7321
7322 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7323 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7324 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7325 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7326 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7327
7328 /*
7329 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7330 * but we don't support that).
7331 * Note: don't use the DAC post divider as it seems unstable.
7332 */
7333 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7334 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7335
a0c4da24 7336 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7337 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7338
89b667f8 7339 /* Set HBR and RBR LPF coefficients */
d288f65f 7340 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7341 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7342 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7343 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7344 0x009f0003);
89b667f8 7345 else
ab3c759a 7346 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7347 0x00d0000f);
7348
681a8504 7349 if (pipe_config->has_dp_encoder) {
89b667f8 7350 /* Use SSC source */
bdd4b6a6 7351 if (pipe == PIPE_A)
ab3c759a 7352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7353 0x0df40000);
7354 else
ab3c759a 7355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7356 0x0df70000);
7357 } else { /* HDMI or VGA */
7358 /* Use bend source */
bdd4b6a6 7359 if (pipe == PIPE_A)
ab3c759a 7360 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7361 0x0df70000);
7362 else
ab3c759a 7363 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7364 0x0df40000);
7365 }
a0c4da24 7366
ab3c759a 7367 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7368 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7369 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7370 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7371 coreclk |= 0x01000000;
ab3c759a 7372 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7373
ab3c759a 7374 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7375 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7376}
7377
251ac862
DV
7378static void chv_compute_dpll(struct intel_crtc *crtc,
7379 struct intel_crtc_state *pipe_config)
1ae0d137 7380{
60bfe44f
VS
7381 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7382 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7383 DPLL_VCO_ENABLE;
7384 if (crtc->pipe != PIPE_A)
d288f65f 7385 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7386
d288f65f
VS
7387 pipe_config->dpll_hw_state.dpll_md =
7388 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7389}
7390
d288f65f 7391static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7392 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7393{
7394 struct drm_device *dev = crtc->base.dev;
7395 struct drm_i915_private *dev_priv = dev->dev_private;
7396 int pipe = crtc->pipe;
f0f59a00 7397 i915_reg_t dpll_reg = DPLL(crtc->pipe);
9d556c99 7398 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7399 u32 loopfilter, tribuf_calcntr;
9d556c99 7400 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7401 u32 dpio_val;
9cbe40c1 7402 int vco;
9d556c99 7403
d288f65f
VS
7404 bestn = pipe_config->dpll.n;
7405 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7406 bestm1 = pipe_config->dpll.m1;
7407 bestm2 = pipe_config->dpll.m2 >> 22;
7408 bestp1 = pipe_config->dpll.p1;
7409 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7410 vco = pipe_config->dpll.vco;
a945ce7e 7411 dpio_val = 0;
9cbe40c1 7412 loopfilter = 0;
9d556c99
CML
7413
7414 /*
7415 * Enable Refclk and SSC
7416 */
a11b0703 7417 I915_WRITE(dpll_reg,
d288f65f 7418 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7419
a580516d 7420 mutex_lock(&dev_priv->sb_lock);
9d556c99 7421
9d556c99
CML
7422 /* p1 and p2 divider */
7423 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7424 5 << DPIO_CHV_S1_DIV_SHIFT |
7425 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7426 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7427 1 << DPIO_CHV_K_DIV_SHIFT);
7428
7429 /* Feedback post-divider - m2 */
7430 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7431
7432 /* Feedback refclk divider - n and m1 */
7433 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7434 DPIO_CHV_M1_DIV_BY_2 |
7435 1 << DPIO_CHV_N_DIV_SHIFT);
7436
7437 /* M2 fraction division */
25a25dfc 7438 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7439
7440 /* M2 fraction division enable */
a945ce7e
VP
7441 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7442 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7443 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7444 if (bestm2_frac)
7445 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7446 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7447
de3a0fde
VP
7448 /* Program digital lock detect threshold */
7449 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7450 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7451 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7452 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7453 if (!bestm2_frac)
7454 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7455 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7456
9d556c99 7457 /* Loop filter */
9cbe40c1
VP
7458 if (vco == 5400000) {
7459 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7460 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7461 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7462 tribuf_calcntr = 0x9;
7463 } else if (vco <= 6200000) {
7464 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7465 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7466 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7467 tribuf_calcntr = 0x9;
7468 } else if (vco <= 6480000) {
7469 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7470 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7471 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7472 tribuf_calcntr = 0x8;
7473 } else {
7474 /* Not supported. Apply the same limits as in the max case */
7475 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7476 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7477 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7478 tribuf_calcntr = 0;
7479 }
9d556c99
CML
7480 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7481
968040b2 7482 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7483 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7484 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7485 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7486
9d556c99
CML
7487 /* AFC Recal */
7488 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7489 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7490 DPIO_AFC_RECAL);
7491
a580516d 7492 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7493}
7494
d288f65f
VS
7495/**
7496 * vlv_force_pll_on - forcibly enable just the PLL
7497 * @dev_priv: i915 private structure
7498 * @pipe: pipe PLL to enable
7499 * @dpll: PLL configuration
7500 *
7501 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7502 * in cases where we need the PLL enabled even when @pipe is not going to
7503 * be enabled.
7504 */
3f36b937
TU
7505int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7506 const struct dpll *dpll)
d288f65f
VS
7507{
7508 struct intel_crtc *crtc =
7509 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
3f36b937
TU
7510 struct intel_crtc_state *pipe_config;
7511
7512 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7513 if (!pipe_config)
7514 return -ENOMEM;
7515
7516 pipe_config->base.crtc = &crtc->base;
7517 pipe_config->pixel_multiplier = 1;
7518 pipe_config->dpll = *dpll;
d288f65f
VS
7519
7520 if (IS_CHERRYVIEW(dev)) {
3f36b937
TU
7521 chv_compute_dpll(crtc, pipe_config);
7522 chv_prepare_pll(crtc, pipe_config);
7523 chv_enable_pll(crtc, pipe_config);
d288f65f 7524 } else {
3f36b937
TU
7525 vlv_compute_dpll(crtc, pipe_config);
7526 vlv_prepare_pll(crtc, pipe_config);
7527 vlv_enable_pll(crtc, pipe_config);
d288f65f 7528 }
3f36b937
TU
7529
7530 kfree(pipe_config);
7531
7532 return 0;
d288f65f
VS
7533}
7534
7535/**
7536 * vlv_force_pll_off - forcibly disable just the PLL
7537 * @dev_priv: i915 private structure
7538 * @pipe: pipe PLL to disable
7539 *
7540 * Disable the PLL for @pipe. To be used in cases where we need
7541 * the PLL enabled even when @pipe is not going to be enabled.
7542 */
7543void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7544{
7545 if (IS_CHERRYVIEW(dev))
7546 chv_disable_pll(to_i915(dev), pipe);
7547 else
7548 vlv_disable_pll(to_i915(dev), pipe);
7549}
7550
251ac862
DV
7551static void i9xx_compute_dpll(struct intel_crtc *crtc,
7552 struct intel_crtc_state *crtc_state,
7553 intel_clock_t *reduced_clock,
7554 int num_connectors)
eb1cbe48 7555{
f47709a9 7556 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7557 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7558 u32 dpll;
7559 bool is_sdvo;
190f68c5 7560 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7561
190f68c5 7562 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7563
a93e255f
ACO
7564 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7565 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7566
7567 dpll = DPLL_VGA_MODE_DIS;
7568
a93e255f 7569 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7570 dpll |= DPLLB_MODE_LVDS;
7571 else
7572 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7573
ef1b460d 7574 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7575 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7576 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7577 }
198a037f
DV
7578
7579 if (is_sdvo)
4a33e48d 7580 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7581
190f68c5 7582 if (crtc_state->has_dp_encoder)
4a33e48d 7583 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7584
7585 /* compute bitmask from p1 value */
7586 if (IS_PINEVIEW(dev))
7587 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7588 else {
7589 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7590 if (IS_G4X(dev) && reduced_clock)
7591 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7592 }
7593 switch (clock->p2) {
7594 case 5:
7595 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7596 break;
7597 case 7:
7598 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7599 break;
7600 case 10:
7601 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7602 break;
7603 case 14:
7604 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7605 break;
7606 }
7607 if (INTEL_INFO(dev)->gen >= 4)
7608 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7609
190f68c5 7610 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7611 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7612 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7613 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7614 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7615 else
7616 dpll |= PLL_REF_INPUT_DREFCLK;
7617
7618 dpll |= DPLL_VCO_ENABLE;
190f68c5 7619 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7620
eb1cbe48 7621 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7622 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7623 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7624 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7625 }
7626}
7627
251ac862
DV
7628static void i8xx_compute_dpll(struct intel_crtc *crtc,
7629 struct intel_crtc_state *crtc_state,
7630 intel_clock_t *reduced_clock,
7631 int num_connectors)
eb1cbe48 7632{
f47709a9 7633 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7634 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7635 u32 dpll;
190f68c5 7636 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7637
190f68c5 7638 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7639
eb1cbe48
DV
7640 dpll = DPLL_VGA_MODE_DIS;
7641
a93e255f 7642 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7643 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7644 } else {
7645 if (clock->p1 == 2)
7646 dpll |= PLL_P1_DIVIDE_BY_TWO;
7647 else
7648 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7649 if (clock->p2 == 4)
7650 dpll |= PLL_P2_DIVIDE_BY_4;
7651 }
7652
a93e255f 7653 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7654 dpll |= DPLL_DVO_2X_MODE;
7655
a93e255f 7656 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7657 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7658 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7659 else
7660 dpll |= PLL_REF_INPUT_DREFCLK;
7661
7662 dpll |= DPLL_VCO_ENABLE;
190f68c5 7663 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7664}
7665
8a654f3b 7666static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7667{
7668 struct drm_device *dev = intel_crtc->base.dev;
7669 struct drm_i915_private *dev_priv = dev->dev_private;
7670 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7671 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7672 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7673 uint32_t crtc_vtotal, crtc_vblank_end;
7674 int vsyncshift = 0;
4d8a62ea
DV
7675
7676 /* We need to be careful not to changed the adjusted mode, for otherwise
7677 * the hw state checker will get angry at the mismatch. */
7678 crtc_vtotal = adjusted_mode->crtc_vtotal;
7679 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7680
609aeaca 7681 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7682 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7683 crtc_vtotal -= 1;
7684 crtc_vblank_end -= 1;
609aeaca 7685
409ee761 7686 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7687 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7688 else
7689 vsyncshift = adjusted_mode->crtc_hsync_start -
7690 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7691 if (vsyncshift < 0)
7692 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7693 }
7694
7695 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7696 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7697
fe2b8f9d 7698 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7699 (adjusted_mode->crtc_hdisplay - 1) |
7700 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7701 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7702 (adjusted_mode->crtc_hblank_start - 1) |
7703 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7704 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7705 (adjusted_mode->crtc_hsync_start - 1) |
7706 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7707
fe2b8f9d 7708 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7709 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7710 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7711 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7712 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7713 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7714 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7715 (adjusted_mode->crtc_vsync_start - 1) |
7716 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7717
b5e508d4
PZ
7718 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7719 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7720 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7721 * bits. */
7722 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7723 (pipe == PIPE_B || pipe == PIPE_C))
7724 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7725
b0e77b9c
PZ
7726 /* pipesrc controls the size that is scaled from, which should
7727 * always be the user's requested size.
7728 */
7729 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7730 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7731 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7732}
7733
1bd1bd80 7734static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7735 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7736{
7737 struct drm_device *dev = crtc->base.dev;
7738 struct drm_i915_private *dev_priv = dev->dev_private;
7739 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7740 uint32_t tmp;
7741
7742 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7743 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7744 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7745 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7746 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7747 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7748 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7749 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7750 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7751
7752 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7753 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7754 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7755 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7756 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7757 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7758 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7759 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7760 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7761
7762 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7763 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7764 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7765 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7766 }
7767
7768 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7769 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7770 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7771
2d112de7
ACO
7772 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7773 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7774}
7775
f6a83288 7776void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7777 struct intel_crtc_state *pipe_config)
babea61d 7778{
2d112de7
ACO
7779 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7780 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7781 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7782 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7783
2d112de7
ACO
7784 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7785 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7786 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7787 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7788
2d112de7 7789 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7790 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7791
2d112de7
ACO
7792 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7793 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7794
7795 mode->hsync = drm_mode_hsync(mode);
7796 mode->vrefresh = drm_mode_vrefresh(mode);
7797 drm_mode_set_name(mode);
babea61d
JB
7798}
7799
84b046f3
DV
7800static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7801{
7802 struct drm_device *dev = intel_crtc->base.dev;
7803 struct drm_i915_private *dev_priv = dev->dev_private;
7804 uint32_t pipeconf;
7805
9f11a9e4 7806 pipeconf = 0;
84b046f3 7807
b6b5d049
VS
7808 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7809 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7810 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7811
6e3c9717 7812 if (intel_crtc->config->double_wide)
cf532bb2 7813 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7814
ff9ce46e 7815 /* only g4x and later have fancy bpc/dither controls */
666a4537 7816 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
ff9ce46e 7817 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7818 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7819 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7820 PIPECONF_DITHER_TYPE_SP;
84b046f3 7821
6e3c9717 7822 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7823 case 18:
7824 pipeconf |= PIPECONF_6BPC;
7825 break;
7826 case 24:
7827 pipeconf |= PIPECONF_8BPC;
7828 break;
7829 case 30:
7830 pipeconf |= PIPECONF_10BPC;
7831 break;
7832 default:
7833 /* Case prevented by intel_choose_pipe_bpp_dither. */
7834 BUG();
84b046f3
DV
7835 }
7836 }
7837
7838 if (HAS_PIPE_CXSR(dev)) {
7839 if (intel_crtc->lowfreq_avail) {
7840 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7841 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7842 } else {
7843 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7844 }
7845 }
7846
6e3c9717 7847 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7848 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7849 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7850 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7851 else
7852 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7853 } else
84b046f3
DV
7854 pipeconf |= PIPECONF_PROGRESSIVE;
7855
666a4537
WB
7856 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7857 intel_crtc->config->limited_color_range)
9f11a9e4 7858 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7859
84b046f3
DV
7860 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7861 POSTING_READ(PIPECONF(intel_crtc->pipe));
7862}
7863
190f68c5
ACO
7864static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7865 struct intel_crtc_state *crtc_state)
79e53945 7866{
c7653199 7867 struct drm_device *dev = crtc->base.dev;
79e53945 7868 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7869 int refclk, num_connectors = 0;
c329a4ec
DV
7870 intel_clock_t clock;
7871 bool ok;
d4906093 7872 const intel_limit_t *limit;
55bb9992 7873 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7874 struct drm_connector *connector;
55bb9992
ACO
7875 struct drm_connector_state *connector_state;
7876 int i;
79e53945 7877
dd3cd74a
ACO
7878 memset(&crtc_state->dpll_hw_state, 0,
7879 sizeof(crtc_state->dpll_hw_state));
7880
a65347ba
JN
7881 if (crtc_state->has_dsi_encoder)
7882 return 0;
43565a06 7883
a65347ba
JN
7884 for_each_connector_in_state(state, connector, connector_state, i) {
7885 if (connector_state->crtc == &crtc->base)
7886 num_connectors++;
79e53945
JB
7887 }
7888
190f68c5 7889 if (!crtc_state->clock_set) {
a93e255f 7890 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7891
e9fd1c02
JN
7892 /*
7893 * Returns a set of divisors for the desired target clock with
7894 * the given refclk, or FALSE. The returned values represent
7895 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7896 * 2) / p1 / p2.
7897 */
a93e255f
ACO
7898 limit = intel_limit(crtc_state, refclk);
7899 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7900 crtc_state->port_clock,
e9fd1c02 7901 refclk, NULL, &clock);
f2335330 7902 if (!ok) {
e9fd1c02
JN
7903 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7904 return -EINVAL;
7905 }
79e53945 7906
f2335330 7907 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7908 crtc_state->dpll.n = clock.n;
7909 crtc_state->dpll.m1 = clock.m1;
7910 crtc_state->dpll.m2 = clock.m2;
7911 crtc_state->dpll.p1 = clock.p1;
7912 crtc_state->dpll.p2 = clock.p2;
f47709a9 7913 }
7026d4ac 7914
e9fd1c02 7915 if (IS_GEN2(dev)) {
c329a4ec 7916 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7917 num_connectors);
9d556c99 7918 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7919 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7920 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7921 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7922 } else {
c329a4ec 7923 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7924 num_connectors);
e9fd1c02 7925 }
79e53945 7926
c8f7a0db 7927 return 0;
f564048e
EA
7928}
7929
2fa2fe9a 7930static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7931 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7932{
7933 struct drm_device *dev = crtc->base.dev;
7934 struct drm_i915_private *dev_priv = dev->dev_private;
7935 uint32_t tmp;
7936
dc9e7dec
VS
7937 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7938 return;
7939
2fa2fe9a 7940 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7941 if (!(tmp & PFIT_ENABLE))
7942 return;
2fa2fe9a 7943
06922821 7944 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7945 if (INTEL_INFO(dev)->gen < 4) {
7946 if (crtc->pipe != PIPE_B)
7947 return;
2fa2fe9a
DV
7948 } else {
7949 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7950 return;
7951 }
7952
06922821 7953 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7954 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7955 if (INTEL_INFO(dev)->gen < 5)
7956 pipe_config->gmch_pfit.lvds_border_bits =
7957 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7958}
7959
acbec814 7960static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7961 struct intel_crtc_state *pipe_config)
acbec814
JB
7962{
7963 struct drm_device *dev = crtc->base.dev;
7964 struct drm_i915_private *dev_priv = dev->dev_private;
7965 int pipe = pipe_config->cpu_transcoder;
7966 intel_clock_t clock;
7967 u32 mdiv;
662c6ecb 7968 int refclk = 100000;
acbec814 7969
f573de5a
SK
7970 /* In case of MIPI DPLL will not even be used */
7971 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7972 return;
7973
a580516d 7974 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7975 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7976 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7977
7978 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7979 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7980 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7981 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7982 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7983
dccbea3b 7984 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
7985}
7986
5724dbd1
DL
7987static void
7988i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7989 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7990{
7991 struct drm_device *dev = crtc->base.dev;
7992 struct drm_i915_private *dev_priv = dev->dev_private;
7993 u32 val, base, offset;
7994 int pipe = crtc->pipe, plane = crtc->plane;
7995 int fourcc, pixel_format;
6761dd31 7996 unsigned int aligned_height;
b113d5ee 7997 struct drm_framebuffer *fb;
1b842c89 7998 struct intel_framebuffer *intel_fb;
1ad292b5 7999
42a7b088
DL
8000 val = I915_READ(DSPCNTR(plane));
8001 if (!(val & DISPLAY_PLANE_ENABLE))
8002 return;
8003
d9806c9f 8004 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8005 if (!intel_fb) {
1ad292b5
JB
8006 DRM_DEBUG_KMS("failed to alloc fb\n");
8007 return;
8008 }
8009
1b842c89
DL
8010 fb = &intel_fb->base;
8011
18c5247e
DV
8012 if (INTEL_INFO(dev)->gen >= 4) {
8013 if (val & DISPPLANE_TILED) {
49af449b 8014 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8015 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8016 }
8017 }
1ad292b5
JB
8018
8019 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8020 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8021 fb->pixel_format = fourcc;
8022 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8023
8024 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8025 if (plane_config->tiling)
1ad292b5
JB
8026 offset = I915_READ(DSPTILEOFF(plane));
8027 else
8028 offset = I915_READ(DSPLINOFF(plane));
8029 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8030 } else {
8031 base = I915_READ(DSPADDR(plane));
8032 }
8033 plane_config->base = base;
8034
8035 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8036 fb->width = ((val >> 16) & 0xfff) + 1;
8037 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8038
8039 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8040 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8041
b113d5ee 8042 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8043 fb->pixel_format,
8044 fb->modifier[0]);
1ad292b5 8045
f37b5c2b 8046 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8047
2844a921
DL
8048 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8049 pipe_name(pipe), plane, fb->width, fb->height,
8050 fb->bits_per_pixel, base, fb->pitches[0],
8051 plane_config->size);
1ad292b5 8052
2d14030b 8053 plane_config->fb = intel_fb;
1ad292b5
JB
8054}
8055
70b23a98 8056static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8057 struct intel_crtc_state *pipe_config)
70b23a98
VS
8058{
8059 struct drm_device *dev = crtc->base.dev;
8060 struct drm_i915_private *dev_priv = dev->dev_private;
8061 int pipe = pipe_config->cpu_transcoder;
8062 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8063 intel_clock_t clock;
0d7b6b11 8064 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8065 int refclk = 100000;
8066
a580516d 8067 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8068 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8069 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8070 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8071 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8072 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8073 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8074
8075 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8076 clock.m2 = (pll_dw0 & 0xff) << 22;
8077 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8078 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8079 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8080 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8081 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8082
dccbea3b 8083 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8084}
8085
0e8ffe1b 8086static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8087 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8088{
8089 struct drm_device *dev = crtc->base.dev;
8090 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 8091 enum intel_display_power_domain power_domain;
0e8ffe1b 8092 uint32_t tmp;
1729050e 8093 bool ret;
0e8ffe1b 8094
1729050e
ID
8095 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8096 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8097 return false;
8098
e143a21c 8099 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8100 pipe_config->shared_dpll = NULL;
eccb140b 8101
1729050e
ID
8102 ret = false;
8103
0e8ffe1b
DV
8104 tmp = I915_READ(PIPECONF(crtc->pipe));
8105 if (!(tmp & PIPECONF_ENABLE))
1729050e 8106 goto out;
0e8ffe1b 8107
666a4537 8108 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
42571aef
VS
8109 switch (tmp & PIPECONF_BPC_MASK) {
8110 case PIPECONF_6BPC:
8111 pipe_config->pipe_bpp = 18;
8112 break;
8113 case PIPECONF_8BPC:
8114 pipe_config->pipe_bpp = 24;
8115 break;
8116 case PIPECONF_10BPC:
8117 pipe_config->pipe_bpp = 30;
8118 break;
8119 default:
8120 break;
8121 }
8122 }
8123
666a4537
WB
8124 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8125 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
DV
8126 pipe_config->limited_color_range = true;
8127
282740f7
VS
8128 if (INTEL_INFO(dev)->gen < 4)
8129 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8130
1bd1bd80
DV
8131 intel_get_pipe_timings(crtc, pipe_config);
8132
2fa2fe9a
DV
8133 i9xx_get_pfit_config(crtc, pipe_config);
8134
6c49f241
DV
8135 if (INTEL_INFO(dev)->gen >= 4) {
8136 tmp = I915_READ(DPLL_MD(crtc->pipe));
8137 pipe_config->pixel_multiplier =
8138 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8139 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8140 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8141 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8142 tmp = I915_READ(DPLL(crtc->pipe));
8143 pipe_config->pixel_multiplier =
8144 ((tmp & SDVO_MULTIPLIER_MASK)
8145 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8146 } else {
8147 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8148 * port and will be fixed up in the encoder->get_config
8149 * function. */
8150 pipe_config->pixel_multiplier = 1;
8151 }
8bcc2795 8152 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
666a4537 8153 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
1c4e0274
VS
8154 /*
8155 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8156 * on 830. Filter it out here so that we don't
8157 * report errors due to that.
8158 */
8159 if (IS_I830(dev))
8160 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8161
8bcc2795
DV
8162 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8163 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8164 } else {
8165 /* Mask out read-only status bits. */
8166 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8167 DPLL_PORTC_READY_MASK |
8168 DPLL_PORTB_READY_MASK);
8bcc2795 8169 }
6c49f241 8170
70b23a98
VS
8171 if (IS_CHERRYVIEW(dev))
8172 chv_crtc_clock_get(crtc, pipe_config);
8173 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8174 vlv_crtc_clock_get(crtc, pipe_config);
8175 else
8176 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8177
0f64614d
VS
8178 /*
8179 * Normally the dotclock is filled in by the encoder .get_config()
8180 * but in case the pipe is enabled w/o any ports we need a sane
8181 * default.
8182 */
8183 pipe_config->base.adjusted_mode.crtc_clock =
8184 pipe_config->port_clock / pipe_config->pixel_multiplier;
8185
1729050e
ID
8186 ret = true;
8187
8188out:
8189 intel_display_power_put(dev_priv, power_domain);
8190
8191 return ret;
0e8ffe1b
DV
8192}
8193
dde86e2d 8194static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8195{
8196 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8197 struct intel_encoder *encoder;
74cfd7ac 8198 u32 val, final;
13d83a67 8199 bool has_lvds = false;
199e5d79 8200 bool has_cpu_edp = false;
199e5d79 8201 bool has_panel = false;
99eb6a01
KP
8202 bool has_ck505 = false;
8203 bool can_ssc = false;
13d83a67
JB
8204
8205 /* We need to take the global config into account */
b2784e15 8206 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8207 switch (encoder->type) {
8208 case INTEL_OUTPUT_LVDS:
8209 has_panel = true;
8210 has_lvds = true;
8211 break;
8212 case INTEL_OUTPUT_EDP:
8213 has_panel = true;
2de6905f 8214 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8215 has_cpu_edp = true;
8216 break;
6847d71b
PZ
8217 default:
8218 break;
13d83a67
JB
8219 }
8220 }
8221
99eb6a01 8222 if (HAS_PCH_IBX(dev)) {
41aa3448 8223 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8224 can_ssc = has_ck505;
8225 } else {
8226 has_ck505 = false;
8227 can_ssc = true;
8228 }
8229
2de6905f
ID
8230 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8231 has_panel, has_lvds, has_ck505);
13d83a67
JB
8232
8233 /* Ironlake: try to setup display ref clock before DPLL
8234 * enabling. This is only under driver's control after
8235 * PCH B stepping, previous chipset stepping should be
8236 * ignoring this setting.
8237 */
74cfd7ac
CW
8238 val = I915_READ(PCH_DREF_CONTROL);
8239
8240 /* As we must carefully and slowly disable/enable each source in turn,
8241 * compute the final state we want first and check if we need to
8242 * make any changes at all.
8243 */
8244 final = val;
8245 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8246 if (has_ck505)
8247 final |= DREF_NONSPREAD_CK505_ENABLE;
8248 else
8249 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8250
8251 final &= ~DREF_SSC_SOURCE_MASK;
8252 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8253 final &= ~DREF_SSC1_ENABLE;
8254
8255 if (has_panel) {
8256 final |= DREF_SSC_SOURCE_ENABLE;
8257
8258 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8259 final |= DREF_SSC1_ENABLE;
8260
8261 if (has_cpu_edp) {
8262 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8263 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8264 else
8265 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8266 } else
8267 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8268 } else {
8269 final |= DREF_SSC_SOURCE_DISABLE;
8270 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8271 }
8272
8273 if (final == val)
8274 return;
8275
13d83a67 8276 /* Always enable nonspread source */
74cfd7ac 8277 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8278
99eb6a01 8279 if (has_ck505)
74cfd7ac 8280 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8281 else
74cfd7ac 8282 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8283
199e5d79 8284 if (has_panel) {
74cfd7ac
CW
8285 val &= ~DREF_SSC_SOURCE_MASK;
8286 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8287
199e5d79 8288 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8289 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8290 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8291 val |= DREF_SSC1_ENABLE;
e77166b5 8292 } else
74cfd7ac 8293 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8294
8295 /* Get SSC going before enabling the outputs */
74cfd7ac 8296 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8297 POSTING_READ(PCH_DREF_CONTROL);
8298 udelay(200);
8299
74cfd7ac 8300 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8301
8302 /* Enable CPU source on CPU attached eDP */
199e5d79 8303 if (has_cpu_edp) {
99eb6a01 8304 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8305 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8306 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8307 } else
74cfd7ac 8308 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8309 } else
74cfd7ac 8310 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8311
74cfd7ac 8312 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8313 POSTING_READ(PCH_DREF_CONTROL);
8314 udelay(200);
8315 } else {
8316 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8317
74cfd7ac 8318 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8319
8320 /* Turn off CPU output */
74cfd7ac 8321 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8322
74cfd7ac 8323 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8324 POSTING_READ(PCH_DREF_CONTROL);
8325 udelay(200);
8326
8327 /* Turn off the SSC source */
74cfd7ac
CW
8328 val &= ~DREF_SSC_SOURCE_MASK;
8329 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8330
8331 /* Turn off SSC1 */
74cfd7ac 8332 val &= ~DREF_SSC1_ENABLE;
199e5d79 8333
74cfd7ac 8334 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8335 POSTING_READ(PCH_DREF_CONTROL);
8336 udelay(200);
8337 }
74cfd7ac
CW
8338
8339 BUG_ON(val != final);
13d83a67
JB
8340}
8341
f31f2d55 8342static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8343{
f31f2d55 8344 uint32_t tmp;
dde86e2d 8345
0ff066a9
PZ
8346 tmp = I915_READ(SOUTH_CHICKEN2);
8347 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8348 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8349
0ff066a9
PZ
8350 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8351 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8352 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8353
0ff066a9
PZ
8354 tmp = I915_READ(SOUTH_CHICKEN2);
8355 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8356 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8357
0ff066a9
PZ
8358 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8359 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8360 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8361}
8362
8363/* WaMPhyProgramming:hsw */
8364static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8365{
8366 uint32_t tmp;
dde86e2d
PZ
8367
8368 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8369 tmp &= ~(0xFF << 24);
8370 tmp |= (0x12 << 24);
8371 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8372
dde86e2d
PZ
8373 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8374 tmp |= (1 << 11);
8375 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8376
8377 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8378 tmp |= (1 << 11);
8379 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8380
dde86e2d
PZ
8381 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8382 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8383 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8384
8385 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8386 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8387 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8388
0ff066a9
PZ
8389 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8390 tmp &= ~(7 << 13);
8391 tmp |= (5 << 13);
8392 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8393
0ff066a9
PZ
8394 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8395 tmp &= ~(7 << 13);
8396 tmp |= (5 << 13);
8397 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8398
8399 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8400 tmp &= ~0xFF;
8401 tmp |= 0x1C;
8402 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8403
8404 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8405 tmp &= ~0xFF;
8406 tmp |= 0x1C;
8407 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8408
8409 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8410 tmp &= ~(0xFF << 16);
8411 tmp |= (0x1C << 16);
8412 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8413
8414 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8415 tmp &= ~(0xFF << 16);
8416 tmp |= (0x1C << 16);
8417 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8418
0ff066a9
PZ
8419 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8420 tmp |= (1 << 27);
8421 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8422
0ff066a9
PZ
8423 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8424 tmp |= (1 << 27);
8425 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8426
0ff066a9
PZ
8427 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8428 tmp &= ~(0xF << 28);
8429 tmp |= (4 << 28);
8430 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8431
0ff066a9
PZ
8432 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8433 tmp &= ~(0xF << 28);
8434 tmp |= (4 << 28);
8435 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8436}
8437
2fa86a1f
PZ
8438/* Implements 3 different sequences from BSpec chapter "Display iCLK
8439 * Programming" based on the parameters passed:
8440 * - Sequence to enable CLKOUT_DP
8441 * - Sequence to enable CLKOUT_DP without spread
8442 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8443 */
8444static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8445 bool with_fdi)
f31f2d55
PZ
8446{
8447 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8448 uint32_t reg, tmp;
8449
8450 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8451 with_spread = true;
c2699524 8452 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8453 with_fdi = false;
f31f2d55 8454
a580516d 8455 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8456
8457 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8458 tmp &= ~SBI_SSCCTL_DISABLE;
8459 tmp |= SBI_SSCCTL_PATHALT;
8460 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8461
8462 udelay(24);
8463
2fa86a1f
PZ
8464 if (with_spread) {
8465 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8466 tmp &= ~SBI_SSCCTL_PATHALT;
8467 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8468
2fa86a1f
PZ
8469 if (with_fdi) {
8470 lpt_reset_fdi_mphy(dev_priv);
8471 lpt_program_fdi_mphy(dev_priv);
8472 }
8473 }
dde86e2d 8474
c2699524 8475 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8476 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8477 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8478 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8479
a580516d 8480 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8481}
8482
47701c3b
PZ
8483/* Sequence to disable CLKOUT_DP */
8484static void lpt_disable_clkout_dp(struct drm_device *dev)
8485{
8486 struct drm_i915_private *dev_priv = dev->dev_private;
8487 uint32_t reg, tmp;
8488
a580516d 8489 mutex_lock(&dev_priv->sb_lock);
47701c3b 8490
c2699524 8491 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8492 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8493 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8494 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8495
8496 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8497 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8498 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8499 tmp |= SBI_SSCCTL_PATHALT;
8500 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8501 udelay(32);
8502 }
8503 tmp |= SBI_SSCCTL_DISABLE;
8504 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8505 }
8506
a580516d 8507 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8508}
8509
f7be2c21
VS
8510#define BEND_IDX(steps) ((50 + (steps)) / 5)
8511
8512static const uint16_t sscdivintphase[] = {
8513 [BEND_IDX( 50)] = 0x3B23,
8514 [BEND_IDX( 45)] = 0x3B23,
8515 [BEND_IDX( 40)] = 0x3C23,
8516 [BEND_IDX( 35)] = 0x3C23,
8517 [BEND_IDX( 30)] = 0x3D23,
8518 [BEND_IDX( 25)] = 0x3D23,
8519 [BEND_IDX( 20)] = 0x3E23,
8520 [BEND_IDX( 15)] = 0x3E23,
8521 [BEND_IDX( 10)] = 0x3F23,
8522 [BEND_IDX( 5)] = 0x3F23,
8523 [BEND_IDX( 0)] = 0x0025,
8524 [BEND_IDX( -5)] = 0x0025,
8525 [BEND_IDX(-10)] = 0x0125,
8526 [BEND_IDX(-15)] = 0x0125,
8527 [BEND_IDX(-20)] = 0x0225,
8528 [BEND_IDX(-25)] = 0x0225,
8529 [BEND_IDX(-30)] = 0x0325,
8530 [BEND_IDX(-35)] = 0x0325,
8531 [BEND_IDX(-40)] = 0x0425,
8532 [BEND_IDX(-45)] = 0x0425,
8533 [BEND_IDX(-50)] = 0x0525,
8534};
8535
8536/*
8537 * Bend CLKOUT_DP
8538 * steps -50 to 50 inclusive, in steps of 5
8539 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8540 * change in clock period = -(steps / 10) * 5.787 ps
8541 */
8542static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8543{
8544 uint32_t tmp;
8545 int idx = BEND_IDX(steps);
8546
8547 if (WARN_ON(steps % 5 != 0))
8548 return;
8549
8550 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8551 return;
8552
8553 mutex_lock(&dev_priv->sb_lock);
8554
8555 if (steps % 10 != 0)
8556 tmp = 0xAAAAAAAB;
8557 else
8558 tmp = 0x00000000;
8559 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8560
8561 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8562 tmp &= 0xffff0000;
8563 tmp |= sscdivintphase[idx];
8564 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8565
8566 mutex_unlock(&dev_priv->sb_lock);
8567}
8568
8569#undef BEND_IDX
8570
bf8fa3d3
PZ
8571static void lpt_init_pch_refclk(struct drm_device *dev)
8572{
bf8fa3d3
PZ
8573 struct intel_encoder *encoder;
8574 bool has_vga = false;
8575
b2784e15 8576 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8577 switch (encoder->type) {
8578 case INTEL_OUTPUT_ANALOG:
8579 has_vga = true;
8580 break;
6847d71b
PZ
8581 default:
8582 break;
bf8fa3d3
PZ
8583 }
8584 }
8585
f7be2c21
VS
8586 if (has_vga) {
8587 lpt_bend_clkout_dp(to_i915(dev), 0);
47701c3b 8588 lpt_enable_clkout_dp(dev, true, true);
f7be2c21 8589 } else {
47701c3b 8590 lpt_disable_clkout_dp(dev);
f7be2c21 8591 }
bf8fa3d3
PZ
8592}
8593
dde86e2d
PZ
8594/*
8595 * Initialize reference clocks when the driver loads
8596 */
8597void intel_init_pch_refclk(struct drm_device *dev)
8598{
8599 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8600 ironlake_init_pch_refclk(dev);
8601 else if (HAS_PCH_LPT(dev))
8602 lpt_init_pch_refclk(dev);
8603}
8604
55bb9992 8605static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8606{
55bb9992 8607 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8608 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8609 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8610 struct drm_connector *connector;
55bb9992 8611 struct drm_connector_state *connector_state;
d9d444cb 8612 struct intel_encoder *encoder;
55bb9992 8613 int num_connectors = 0, i;
d9d444cb
JB
8614 bool is_lvds = false;
8615
da3ced29 8616 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8617 if (connector_state->crtc != crtc_state->base.crtc)
8618 continue;
8619
8620 encoder = to_intel_encoder(connector_state->best_encoder);
8621
d9d444cb
JB
8622 switch (encoder->type) {
8623 case INTEL_OUTPUT_LVDS:
8624 is_lvds = true;
8625 break;
6847d71b
PZ
8626 default:
8627 break;
d9d444cb
JB
8628 }
8629 num_connectors++;
8630 }
8631
8632 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8633 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8634 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8635 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8636 }
8637
8638 return 120000;
8639}
8640
6ff93609 8641static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8642{
c8203565 8643 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8645 int pipe = intel_crtc->pipe;
c8203565
PZ
8646 uint32_t val;
8647
78114071 8648 val = 0;
c8203565 8649
6e3c9717 8650 switch (intel_crtc->config->pipe_bpp) {
c8203565 8651 case 18:
dfd07d72 8652 val |= PIPECONF_6BPC;
c8203565
PZ
8653 break;
8654 case 24:
dfd07d72 8655 val |= PIPECONF_8BPC;
c8203565
PZ
8656 break;
8657 case 30:
dfd07d72 8658 val |= PIPECONF_10BPC;
c8203565
PZ
8659 break;
8660 case 36:
dfd07d72 8661 val |= PIPECONF_12BPC;
c8203565
PZ
8662 break;
8663 default:
cc769b62
PZ
8664 /* Case prevented by intel_choose_pipe_bpp_dither. */
8665 BUG();
c8203565
PZ
8666 }
8667
6e3c9717 8668 if (intel_crtc->config->dither)
c8203565
PZ
8669 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8670
6e3c9717 8671 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8672 val |= PIPECONF_INTERLACED_ILK;
8673 else
8674 val |= PIPECONF_PROGRESSIVE;
8675
6e3c9717 8676 if (intel_crtc->config->limited_color_range)
3685a8f3 8677 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8678
c8203565
PZ
8679 I915_WRITE(PIPECONF(pipe), val);
8680 POSTING_READ(PIPECONF(pipe));
8681}
8682
86d3efce
VS
8683/*
8684 * Set up the pipe CSC unit.
8685 *
8686 * Currently only full range RGB to limited range RGB conversion
8687 * is supported, but eventually this should handle various
8688 * RGB<->YCbCr scenarios as well.
8689 */
50f3b016 8690static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8691{
8692 struct drm_device *dev = crtc->dev;
8693 struct drm_i915_private *dev_priv = dev->dev_private;
8694 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8695 int pipe = intel_crtc->pipe;
8696 uint16_t coeff = 0x7800; /* 1.0 */
8697
8698 /*
8699 * TODO: Check what kind of values actually come out of the pipe
8700 * with these coeff/postoff values and adjust to get the best
8701 * accuracy. Perhaps we even need to take the bpc value into
8702 * consideration.
8703 */
8704
6e3c9717 8705 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8706 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8707
8708 /*
8709 * GY/GU and RY/RU should be the other way around according
8710 * to BSpec, but reality doesn't agree. Just set them up in
8711 * a way that results in the correct picture.
8712 */
8713 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8714 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8715
8716 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8717 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8718
8719 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8720 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8721
8722 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8723 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8724 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8725
8726 if (INTEL_INFO(dev)->gen > 6) {
8727 uint16_t postoff = 0;
8728
6e3c9717 8729 if (intel_crtc->config->limited_color_range)
32cf0cb0 8730 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8731
8732 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8733 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8734 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8735
8736 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8737 } else {
8738 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8739
6e3c9717 8740 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8741 mode |= CSC_BLACK_SCREEN_OFFSET;
8742
8743 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8744 }
8745}
8746
6ff93609 8747static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8748{
756f85cf
PZ
8749 struct drm_device *dev = crtc->dev;
8750 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8752 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8753 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8754 uint32_t val;
8755
3eff4faa 8756 val = 0;
ee2b0b38 8757
6e3c9717 8758 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8759 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8760
6e3c9717 8761 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8762 val |= PIPECONF_INTERLACED_ILK;
8763 else
8764 val |= PIPECONF_PROGRESSIVE;
8765
702e7a56
PZ
8766 I915_WRITE(PIPECONF(cpu_transcoder), val);
8767 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8768
8769 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8770 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8771
3cdf122c 8772 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8773 val = 0;
8774
6e3c9717 8775 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8776 case 18:
8777 val |= PIPEMISC_DITHER_6_BPC;
8778 break;
8779 case 24:
8780 val |= PIPEMISC_DITHER_8_BPC;
8781 break;
8782 case 30:
8783 val |= PIPEMISC_DITHER_10_BPC;
8784 break;
8785 case 36:
8786 val |= PIPEMISC_DITHER_12_BPC;
8787 break;
8788 default:
8789 /* Case prevented by pipe_config_set_bpp. */
8790 BUG();
8791 }
8792
6e3c9717 8793 if (intel_crtc->config->dither)
756f85cf
PZ
8794 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8795
8796 I915_WRITE(PIPEMISC(pipe), val);
8797 }
ee2b0b38
PZ
8798}
8799
6591c6e4 8800static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8801 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8802 intel_clock_t *clock,
8803 bool *has_reduced_clock,
8804 intel_clock_t *reduced_clock)
8805{
8806 struct drm_device *dev = crtc->dev;
8807 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8808 int refclk;
d4906093 8809 const intel_limit_t *limit;
c329a4ec 8810 bool ret;
79e53945 8811
55bb9992 8812 refclk = ironlake_get_refclk(crtc_state);
79e53945 8813
d4906093
ML
8814 /*
8815 * Returns a set of divisors for the desired target clock with the given
8816 * refclk, or FALSE. The returned values represent the clock equation:
8817 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8818 */
a93e255f
ACO
8819 limit = intel_limit(crtc_state, refclk);
8820 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8821 crtc_state->port_clock,
ee9300bb 8822 refclk, NULL, clock);
6591c6e4
PZ
8823 if (!ret)
8824 return false;
cda4b7d3 8825
6591c6e4
PZ
8826 return true;
8827}
8828
d4b1931c
PZ
8829int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8830{
8831 /*
8832 * Account for spread spectrum to avoid
8833 * oversubscribing the link. Max center spread
8834 * is 2.5%; use 5% for safety's sake.
8835 */
8836 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8837 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8838}
8839
7429e9d4 8840static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8841{
7429e9d4 8842 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8843}
8844
de13a2e3 8845static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8846 struct intel_crtc_state *crtc_state,
7429e9d4 8847 u32 *fp,
9a7c7890 8848 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8849{
de13a2e3 8850 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8851 struct drm_device *dev = crtc->dev;
8852 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8853 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8854 struct drm_connector *connector;
55bb9992
ACO
8855 struct drm_connector_state *connector_state;
8856 struct intel_encoder *encoder;
de13a2e3 8857 uint32_t dpll;
55bb9992 8858 int factor, num_connectors = 0, i;
09ede541 8859 bool is_lvds = false, is_sdvo = false;
79e53945 8860
da3ced29 8861 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8862 if (connector_state->crtc != crtc_state->base.crtc)
8863 continue;
8864
8865 encoder = to_intel_encoder(connector_state->best_encoder);
8866
8867 switch (encoder->type) {
79e53945
JB
8868 case INTEL_OUTPUT_LVDS:
8869 is_lvds = true;
8870 break;
8871 case INTEL_OUTPUT_SDVO:
7d57382e 8872 case INTEL_OUTPUT_HDMI:
79e53945 8873 is_sdvo = true;
79e53945 8874 break;
6847d71b
PZ
8875 default:
8876 break;
79e53945 8877 }
43565a06 8878
c751ce4f 8879 num_connectors++;
79e53945 8880 }
79e53945 8881
c1858123 8882 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8883 factor = 21;
8884 if (is_lvds) {
8885 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8886 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8887 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8888 factor = 25;
190f68c5 8889 } else if (crtc_state->sdvo_tv_clock)
8febb297 8890 factor = 20;
c1858123 8891
190f68c5 8892 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8893 *fp |= FP_CB_TUNE;
2c07245f 8894
9a7c7890
DV
8895 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8896 *fp2 |= FP_CB_TUNE;
8897
5eddb70b 8898 dpll = 0;
2c07245f 8899
a07d6787
EA
8900 if (is_lvds)
8901 dpll |= DPLLB_MODE_LVDS;
8902 else
8903 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8904
190f68c5 8905 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8906 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8907
8908 if (is_sdvo)
4a33e48d 8909 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8910 if (crtc_state->has_dp_encoder)
4a33e48d 8911 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8912
a07d6787 8913 /* compute bitmask from p1 value */
190f68c5 8914 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8915 /* also FPA1 */
190f68c5 8916 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8917
190f68c5 8918 switch (crtc_state->dpll.p2) {
a07d6787
EA
8919 case 5:
8920 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8921 break;
8922 case 7:
8923 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8924 break;
8925 case 10:
8926 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8927 break;
8928 case 14:
8929 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8930 break;
79e53945
JB
8931 }
8932
b4c09f3b 8933 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8934 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8935 else
8936 dpll |= PLL_REF_INPUT_DREFCLK;
8937
959e16d6 8938 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8939}
8940
190f68c5
ACO
8941static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8942 struct intel_crtc_state *crtc_state)
de13a2e3 8943{
c7653199 8944 struct drm_device *dev = crtc->base.dev;
de13a2e3 8945 intel_clock_t clock, reduced_clock;
cbbab5bd 8946 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8947 bool ok, has_reduced_clock = false;
8b47047b 8948 bool is_lvds = false;
e2b78267 8949 struct intel_shared_dpll *pll;
de13a2e3 8950
dd3cd74a
ACO
8951 memset(&crtc_state->dpll_hw_state, 0,
8952 sizeof(crtc_state->dpll_hw_state));
8953
7905df29 8954 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8955
5dc5298b
PZ
8956 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8957 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8958
190f68c5 8959 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8960 &has_reduced_clock, &reduced_clock);
190f68c5 8961 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8962 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8963 return -EINVAL;
79e53945 8964 }
f47709a9 8965 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8966 if (!crtc_state->clock_set) {
8967 crtc_state->dpll.n = clock.n;
8968 crtc_state->dpll.m1 = clock.m1;
8969 crtc_state->dpll.m2 = clock.m2;
8970 crtc_state->dpll.p1 = clock.p1;
8971 crtc_state->dpll.p2 = clock.p2;
f47709a9 8972 }
79e53945 8973
5dc5298b 8974 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8975 if (crtc_state->has_pch_encoder) {
8976 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8977 if (has_reduced_clock)
7429e9d4 8978 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8979
190f68c5 8980 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8981 &fp, &reduced_clock,
8982 has_reduced_clock ? &fp2 : NULL);
8983
190f68c5
ACO
8984 crtc_state->dpll_hw_state.dpll = dpll;
8985 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8986 if (has_reduced_clock)
190f68c5 8987 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8988 else
190f68c5 8989 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8990
daedf20a 8991 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
ee7b9f93 8992 if (pll == NULL) {
84f44ce7 8993 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8994 pipe_name(crtc->pipe));
4b645f14
JB
8995 return -EINVAL;
8996 }
3fb37703 8997 }
79e53945 8998
ab585dea 8999 if (is_lvds && has_reduced_clock)
c7653199 9000 crtc->lowfreq_avail = true;
bcd644e0 9001 else
c7653199 9002 crtc->lowfreq_avail = false;
e2b78267 9003
c8f7a0db 9004 return 0;
79e53945
JB
9005}
9006
eb14cb74
VS
9007static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9008 struct intel_link_m_n *m_n)
9009{
9010 struct drm_device *dev = crtc->base.dev;
9011 struct drm_i915_private *dev_priv = dev->dev_private;
9012 enum pipe pipe = crtc->pipe;
9013
9014 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9015 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9016 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9017 & ~TU_SIZE_MASK;
9018 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9019 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9020 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9021}
9022
9023static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9024 enum transcoder transcoder,
b95af8be
VK
9025 struct intel_link_m_n *m_n,
9026 struct intel_link_m_n *m2_n2)
72419203
DV
9027{
9028 struct drm_device *dev = crtc->base.dev;
9029 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 9030 enum pipe pipe = crtc->pipe;
72419203 9031
eb14cb74
VS
9032 if (INTEL_INFO(dev)->gen >= 5) {
9033 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9034 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9035 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9036 & ~TU_SIZE_MASK;
9037 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9038 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9039 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9040 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9041 * gen < 8) and if DRRS is supported (to make sure the
9042 * registers are not unnecessarily read).
9043 */
9044 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9045 crtc->config->has_drrs) {
b95af8be
VK
9046 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9047 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9048 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9049 & ~TU_SIZE_MASK;
9050 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9051 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9052 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9053 }
eb14cb74
VS
9054 } else {
9055 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9056 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9057 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9058 & ~TU_SIZE_MASK;
9059 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9060 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9061 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9062 }
9063}
9064
9065void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9066 struct intel_crtc_state *pipe_config)
eb14cb74 9067{
681a8504 9068 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9069 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9070 else
9071 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9072 &pipe_config->dp_m_n,
9073 &pipe_config->dp_m2_n2);
eb14cb74 9074}
72419203 9075
eb14cb74 9076static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9077 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9078{
9079 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9080 &pipe_config->fdi_m_n, NULL);
72419203
DV
9081}
9082
bd2e244f 9083static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9084 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9085{
9086 struct drm_device *dev = crtc->base.dev;
9087 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9088 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9089 uint32_t ps_ctrl = 0;
9090 int id = -1;
9091 int i;
bd2e244f 9092
a1b2278e
CK
9093 /* find scaler attached to this pipe */
9094 for (i = 0; i < crtc->num_scalers; i++) {
9095 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9096 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9097 id = i;
9098 pipe_config->pch_pfit.enabled = true;
9099 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9100 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9101 break;
9102 }
9103 }
bd2e244f 9104
a1b2278e
CK
9105 scaler_state->scaler_id = id;
9106 if (id >= 0) {
9107 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9108 } else {
9109 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9110 }
9111}
9112
5724dbd1
DL
9113static void
9114skylake_get_initial_plane_config(struct intel_crtc *crtc,
9115 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9116{
9117 struct drm_device *dev = crtc->base.dev;
9118 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9119 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9120 int pipe = crtc->pipe;
9121 int fourcc, pixel_format;
6761dd31 9122 unsigned int aligned_height;
bc8d7dff 9123 struct drm_framebuffer *fb;
1b842c89 9124 struct intel_framebuffer *intel_fb;
bc8d7dff 9125
d9806c9f 9126 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9127 if (!intel_fb) {
bc8d7dff
DL
9128 DRM_DEBUG_KMS("failed to alloc fb\n");
9129 return;
9130 }
9131
1b842c89
DL
9132 fb = &intel_fb->base;
9133
bc8d7dff 9134 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9135 if (!(val & PLANE_CTL_ENABLE))
9136 goto error;
9137
bc8d7dff
DL
9138 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9139 fourcc = skl_format_to_fourcc(pixel_format,
9140 val & PLANE_CTL_ORDER_RGBX,
9141 val & PLANE_CTL_ALPHA_MASK);
9142 fb->pixel_format = fourcc;
9143 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9144
40f46283
DL
9145 tiling = val & PLANE_CTL_TILED_MASK;
9146 switch (tiling) {
9147 case PLANE_CTL_TILED_LINEAR:
9148 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9149 break;
9150 case PLANE_CTL_TILED_X:
9151 plane_config->tiling = I915_TILING_X;
9152 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9153 break;
9154 case PLANE_CTL_TILED_Y:
9155 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9156 break;
9157 case PLANE_CTL_TILED_YF:
9158 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9159 break;
9160 default:
9161 MISSING_CASE(tiling);
9162 goto error;
9163 }
9164
bc8d7dff
DL
9165 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9166 plane_config->base = base;
9167
9168 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9169
9170 val = I915_READ(PLANE_SIZE(pipe, 0));
9171 fb->height = ((val >> 16) & 0xfff) + 1;
9172 fb->width = ((val >> 0) & 0x1fff) + 1;
9173
9174 val = I915_READ(PLANE_STRIDE(pipe, 0));
7b49f948 9175 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
40f46283 9176 fb->pixel_format);
bc8d7dff
DL
9177 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9178
9179 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9180 fb->pixel_format,
9181 fb->modifier[0]);
bc8d7dff 9182
f37b5c2b 9183 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9184
9185 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9186 pipe_name(pipe), fb->width, fb->height,
9187 fb->bits_per_pixel, base, fb->pitches[0],
9188 plane_config->size);
9189
2d14030b 9190 plane_config->fb = intel_fb;
bc8d7dff
DL
9191 return;
9192
9193error:
9194 kfree(fb);
9195}
9196
2fa2fe9a 9197static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9198 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9199{
9200 struct drm_device *dev = crtc->base.dev;
9201 struct drm_i915_private *dev_priv = dev->dev_private;
9202 uint32_t tmp;
9203
9204 tmp = I915_READ(PF_CTL(crtc->pipe));
9205
9206 if (tmp & PF_ENABLE) {
fd4daa9c 9207 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9208 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9209 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9210
9211 /* We currently do not free assignements of panel fitters on
9212 * ivb/hsw (since we don't use the higher upscaling modes which
9213 * differentiates them) so just WARN about this case for now. */
9214 if (IS_GEN7(dev)) {
9215 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9216 PF_PIPE_SEL_IVB(crtc->pipe));
9217 }
2fa2fe9a 9218 }
79e53945
JB
9219}
9220
5724dbd1
DL
9221static void
9222ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9223 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9224{
9225 struct drm_device *dev = crtc->base.dev;
9226 struct drm_i915_private *dev_priv = dev->dev_private;
9227 u32 val, base, offset;
aeee5a49 9228 int pipe = crtc->pipe;
4c6baa59 9229 int fourcc, pixel_format;
6761dd31 9230 unsigned int aligned_height;
b113d5ee 9231 struct drm_framebuffer *fb;
1b842c89 9232 struct intel_framebuffer *intel_fb;
4c6baa59 9233
42a7b088
DL
9234 val = I915_READ(DSPCNTR(pipe));
9235 if (!(val & DISPLAY_PLANE_ENABLE))
9236 return;
9237
d9806c9f 9238 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9239 if (!intel_fb) {
4c6baa59
JB
9240 DRM_DEBUG_KMS("failed to alloc fb\n");
9241 return;
9242 }
9243
1b842c89
DL
9244 fb = &intel_fb->base;
9245
18c5247e
DV
9246 if (INTEL_INFO(dev)->gen >= 4) {
9247 if (val & DISPPLANE_TILED) {
49af449b 9248 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9249 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9250 }
9251 }
4c6baa59
JB
9252
9253 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9254 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9255 fb->pixel_format = fourcc;
9256 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9257
aeee5a49 9258 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9259 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9260 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9261 } else {
49af449b 9262 if (plane_config->tiling)
aeee5a49 9263 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9264 else
aeee5a49 9265 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9266 }
9267 plane_config->base = base;
9268
9269 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9270 fb->width = ((val >> 16) & 0xfff) + 1;
9271 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9272
9273 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9274 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9275
b113d5ee 9276 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9277 fb->pixel_format,
9278 fb->modifier[0]);
4c6baa59 9279
f37b5c2b 9280 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9281
2844a921
DL
9282 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9283 pipe_name(pipe), fb->width, fb->height,
9284 fb->bits_per_pixel, base, fb->pitches[0],
9285 plane_config->size);
b113d5ee 9286
2d14030b 9287 plane_config->fb = intel_fb;
4c6baa59
JB
9288}
9289
0e8ffe1b 9290static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9291 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9292{
9293 struct drm_device *dev = crtc->base.dev;
9294 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e 9295 enum intel_display_power_domain power_domain;
0e8ffe1b 9296 uint32_t tmp;
1729050e 9297 bool ret;
0e8ffe1b 9298
1729050e
ID
9299 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9300 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9301 return false;
9302
e143a21c 9303 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9304 pipe_config->shared_dpll = NULL;
eccb140b 9305
1729050e 9306 ret = false;
0e8ffe1b
DV
9307 tmp = I915_READ(PIPECONF(crtc->pipe));
9308 if (!(tmp & PIPECONF_ENABLE))
1729050e 9309 goto out;
0e8ffe1b 9310
42571aef
VS
9311 switch (tmp & PIPECONF_BPC_MASK) {
9312 case PIPECONF_6BPC:
9313 pipe_config->pipe_bpp = 18;
9314 break;
9315 case PIPECONF_8BPC:
9316 pipe_config->pipe_bpp = 24;
9317 break;
9318 case PIPECONF_10BPC:
9319 pipe_config->pipe_bpp = 30;
9320 break;
9321 case PIPECONF_12BPC:
9322 pipe_config->pipe_bpp = 36;
9323 break;
9324 default:
9325 break;
9326 }
9327
b5a9fa09
DV
9328 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9329 pipe_config->limited_color_range = true;
9330
ab9412ba 9331 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9332 struct intel_shared_dpll *pll;
8106ddbd 9333 enum intel_dpll_id pll_id;
66e985c0 9334
88adfff1
DV
9335 pipe_config->has_pch_encoder = true;
9336
627eb5a3
DV
9337 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9338 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9339 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9340
9341 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9342
c0d43d62 9343 if (HAS_PCH_IBX(dev_priv->dev)) {
8106ddbd 9344 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9345 } else {
9346 tmp = I915_READ(PCH_DPLL_SEL);
9347 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 9348 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 9349 else
8106ddbd 9350 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 9351 }
66e985c0 9352
8106ddbd
ACO
9353 pipe_config->shared_dpll =
9354 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9355 pll = pipe_config->shared_dpll;
66e985c0 9356
2edd6443
ACO
9357 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9358 &pipe_config->dpll_hw_state));
c93f54cf
DV
9359
9360 tmp = pipe_config->dpll_hw_state.dpll;
9361 pipe_config->pixel_multiplier =
9362 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9363 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9364
9365 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9366 } else {
9367 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9368 }
9369
1bd1bd80
DV
9370 intel_get_pipe_timings(crtc, pipe_config);
9371
2fa2fe9a
DV
9372 ironlake_get_pfit_config(crtc, pipe_config);
9373
1729050e
ID
9374 ret = true;
9375
9376out:
9377 intel_display_power_put(dev_priv, power_domain);
9378
9379 return ret;
0e8ffe1b
DV
9380}
9381
be256dc7
PZ
9382static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9383{
9384 struct drm_device *dev = dev_priv->dev;
be256dc7 9385 struct intel_crtc *crtc;
be256dc7 9386
d3fcc808 9387 for_each_intel_crtc(dev, crtc)
e2c719b7 9388 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9389 pipe_name(crtc->pipe));
9390
e2c719b7
RC
9391 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9392 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9393 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9394 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9395 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9396 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9397 "CPU PWM1 enabled\n");
c5107b87 9398 if (IS_HASWELL(dev))
e2c719b7 9399 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9400 "CPU PWM2 enabled\n");
e2c719b7 9401 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9402 "PCH PWM1 enabled\n");
e2c719b7 9403 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9404 "Utility pin enabled\n");
e2c719b7 9405 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9406
9926ada1
PZ
9407 /*
9408 * In theory we can still leave IRQs enabled, as long as only the HPD
9409 * interrupts remain enabled. We used to check for that, but since it's
9410 * gen-specific and since we only disable LCPLL after we fully disable
9411 * the interrupts, the check below should be enough.
9412 */
e2c719b7 9413 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9414}
9415
9ccd5aeb
PZ
9416static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9417{
9418 struct drm_device *dev = dev_priv->dev;
9419
9420 if (IS_HASWELL(dev))
9421 return I915_READ(D_COMP_HSW);
9422 else
9423 return I915_READ(D_COMP_BDW);
9424}
9425
3c4c9b81
PZ
9426static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9427{
9428 struct drm_device *dev = dev_priv->dev;
9429
9430 if (IS_HASWELL(dev)) {
9431 mutex_lock(&dev_priv->rps.hw_lock);
9432 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9433 val))
f475dadf 9434 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9435 mutex_unlock(&dev_priv->rps.hw_lock);
9436 } else {
9ccd5aeb
PZ
9437 I915_WRITE(D_COMP_BDW, val);
9438 POSTING_READ(D_COMP_BDW);
3c4c9b81 9439 }
be256dc7
PZ
9440}
9441
9442/*
9443 * This function implements pieces of two sequences from BSpec:
9444 * - Sequence for display software to disable LCPLL
9445 * - Sequence for display software to allow package C8+
9446 * The steps implemented here are just the steps that actually touch the LCPLL
9447 * register. Callers should take care of disabling all the display engine
9448 * functions, doing the mode unset, fixing interrupts, etc.
9449 */
6ff58d53
PZ
9450static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9451 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9452{
9453 uint32_t val;
9454
9455 assert_can_disable_lcpll(dev_priv);
9456
9457 val = I915_READ(LCPLL_CTL);
9458
9459 if (switch_to_fclk) {
9460 val |= LCPLL_CD_SOURCE_FCLK;
9461 I915_WRITE(LCPLL_CTL, val);
9462
9463 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9464 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9465 DRM_ERROR("Switching to FCLK failed\n");
9466
9467 val = I915_READ(LCPLL_CTL);
9468 }
9469
9470 val |= LCPLL_PLL_DISABLE;
9471 I915_WRITE(LCPLL_CTL, val);
9472 POSTING_READ(LCPLL_CTL);
9473
9474 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9475 DRM_ERROR("LCPLL still locked\n");
9476
9ccd5aeb 9477 val = hsw_read_dcomp(dev_priv);
be256dc7 9478 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9479 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9480 ndelay(100);
9481
9ccd5aeb
PZ
9482 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9483 1))
be256dc7
PZ
9484 DRM_ERROR("D_COMP RCOMP still in progress\n");
9485
9486 if (allow_power_down) {
9487 val = I915_READ(LCPLL_CTL);
9488 val |= LCPLL_POWER_DOWN_ALLOW;
9489 I915_WRITE(LCPLL_CTL, val);
9490 POSTING_READ(LCPLL_CTL);
9491 }
9492}
9493
9494/*
9495 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9496 * source.
9497 */
6ff58d53 9498static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9499{
9500 uint32_t val;
9501
9502 val = I915_READ(LCPLL_CTL);
9503
9504 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9505 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9506 return;
9507
a8a8bd54
PZ
9508 /*
9509 * Make sure we're not on PC8 state before disabling PC8, otherwise
9510 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9511 */
59bad947 9512 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9513
be256dc7
PZ
9514 if (val & LCPLL_POWER_DOWN_ALLOW) {
9515 val &= ~LCPLL_POWER_DOWN_ALLOW;
9516 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9517 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9518 }
9519
9ccd5aeb 9520 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9521 val |= D_COMP_COMP_FORCE;
9522 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9523 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9524
9525 val = I915_READ(LCPLL_CTL);
9526 val &= ~LCPLL_PLL_DISABLE;
9527 I915_WRITE(LCPLL_CTL, val);
9528
9529 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9530 DRM_ERROR("LCPLL not locked yet\n");
9531
9532 if (val & LCPLL_CD_SOURCE_FCLK) {
9533 val = I915_READ(LCPLL_CTL);
9534 val &= ~LCPLL_CD_SOURCE_FCLK;
9535 I915_WRITE(LCPLL_CTL, val);
9536
9537 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9538 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9539 DRM_ERROR("Switching back to LCPLL failed\n");
9540 }
215733fa 9541
59bad947 9542 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9543 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9544}
9545
765dab67
PZ
9546/*
9547 * Package states C8 and deeper are really deep PC states that can only be
9548 * reached when all the devices on the system allow it, so even if the graphics
9549 * device allows PC8+, it doesn't mean the system will actually get to these
9550 * states. Our driver only allows PC8+ when going into runtime PM.
9551 *
9552 * The requirements for PC8+ are that all the outputs are disabled, the power
9553 * well is disabled and most interrupts are disabled, and these are also
9554 * requirements for runtime PM. When these conditions are met, we manually do
9555 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9556 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9557 * hang the machine.
9558 *
9559 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9560 * the state of some registers, so when we come back from PC8+ we need to
9561 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9562 * need to take care of the registers kept by RC6. Notice that this happens even
9563 * if we don't put the device in PCI D3 state (which is what currently happens
9564 * because of the runtime PM support).
9565 *
9566 * For more, read "Display Sequences for Package C8" on the hardware
9567 * documentation.
9568 */
a14cb6fc 9569void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9570{
c67a470b
PZ
9571 struct drm_device *dev = dev_priv->dev;
9572 uint32_t val;
9573
c67a470b
PZ
9574 DRM_DEBUG_KMS("Enabling package C8+\n");
9575
c2699524 9576 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9577 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9578 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9579 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9580 }
9581
9582 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9583 hsw_disable_lcpll(dev_priv, true, true);
9584}
9585
a14cb6fc 9586void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9587{
9588 struct drm_device *dev = dev_priv->dev;
9589 uint32_t val;
9590
c67a470b
PZ
9591 DRM_DEBUG_KMS("Disabling package C8+\n");
9592
9593 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9594 lpt_init_pch_refclk(dev);
9595
c2699524 9596 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9597 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9598 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9599 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9600 }
c67a470b
PZ
9601}
9602
27c329ed 9603static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9604{
a821fc46 9605 struct drm_device *dev = old_state->dev;
1a617b77
ML
9606 struct intel_atomic_state *old_intel_state =
9607 to_intel_atomic_state(old_state);
9608 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 9609
27c329ed 9610 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9611}
9612
b432e5cf 9613/* compute the max rate for new configuration */
27c329ed 9614static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9615{
565602d7
ML
9616 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9617 struct drm_i915_private *dev_priv = state->dev->dev_private;
9618 struct drm_crtc *crtc;
9619 struct drm_crtc_state *cstate;
27c329ed 9620 struct intel_crtc_state *crtc_state;
565602d7
ML
9621 unsigned max_pixel_rate = 0, i;
9622 enum pipe pipe;
b432e5cf 9623
565602d7
ML
9624 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9625 sizeof(intel_state->min_pixclk));
27c329ed 9626
565602d7
ML
9627 for_each_crtc_in_state(state, crtc, cstate, i) {
9628 int pixel_rate;
27c329ed 9629
565602d7
ML
9630 crtc_state = to_intel_crtc_state(cstate);
9631 if (!crtc_state->base.enable) {
9632 intel_state->min_pixclk[i] = 0;
b432e5cf 9633 continue;
565602d7 9634 }
b432e5cf 9635
27c329ed 9636 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9637
9638 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
565602d7 9639 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b432e5cf
VS
9640 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9641
565602d7 9642 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
9643 }
9644
565602d7
ML
9645 for_each_pipe(dev_priv, pipe)
9646 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9647
b432e5cf
VS
9648 return max_pixel_rate;
9649}
9650
9651static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9652{
9653 struct drm_i915_private *dev_priv = dev->dev_private;
9654 uint32_t val, data;
9655 int ret;
9656
9657 if (WARN((I915_READ(LCPLL_CTL) &
9658 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9659 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9660 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9661 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9662 "trying to change cdclk frequency with cdclk not enabled\n"))
9663 return;
9664
9665 mutex_lock(&dev_priv->rps.hw_lock);
9666 ret = sandybridge_pcode_write(dev_priv,
9667 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9668 mutex_unlock(&dev_priv->rps.hw_lock);
9669 if (ret) {
9670 DRM_ERROR("failed to inform pcode about cdclk change\n");
9671 return;
9672 }
9673
9674 val = I915_READ(LCPLL_CTL);
9675 val |= LCPLL_CD_SOURCE_FCLK;
9676 I915_WRITE(LCPLL_CTL, val);
9677
5ba00178
TU
9678 if (wait_for_us(I915_READ(LCPLL_CTL) &
9679 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
9680 DRM_ERROR("Switching to FCLK failed\n");
9681
9682 val = I915_READ(LCPLL_CTL);
9683 val &= ~LCPLL_CLK_FREQ_MASK;
9684
9685 switch (cdclk) {
9686 case 450000:
9687 val |= LCPLL_CLK_FREQ_450;
9688 data = 0;
9689 break;
9690 case 540000:
9691 val |= LCPLL_CLK_FREQ_54O_BDW;
9692 data = 1;
9693 break;
9694 case 337500:
9695 val |= LCPLL_CLK_FREQ_337_5_BDW;
9696 data = 2;
9697 break;
9698 case 675000:
9699 val |= LCPLL_CLK_FREQ_675_BDW;
9700 data = 3;
9701 break;
9702 default:
9703 WARN(1, "invalid cdclk frequency\n");
9704 return;
9705 }
9706
9707 I915_WRITE(LCPLL_CTL, val);
9708
9709 val = I915_READ(LCPLL_CTL);
9710 val &= ~LCPLL_CD_SOURCE_FCLK;
9711 I915_WRITE(LCPLL_CTL, val);
9712
5ba00178
TU
9713 if (wait_for_us((I915_READ(LCPLL_CTL) &
9714 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
9715 DRM_ERROR("Switching back to LCPLL failed\n");
9716
9717 mutex_lock(&dev_priv->rps.hw_lock);
9718 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9719 mutex_unlock(&dev_priv->rps.hw_lock);
9720
9721 intel_update_cdclk(dev);
9722
9723 WARN(cdclk != dev_priv->cdclk_freq,
9724 "cdclk requested %d kHz but got %d kHz\n",
9725 cdclk, dev_priv->cdclk_freq);
9726}
9727
27c329ed 9728static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9729{
27c329ed 9730 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 9731 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 9732 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9733 int cdclk;
9734
9735 /*
9736 * FIXME should also account for plane ratio
9737 * once 64bpp pixel formats are supported.
9738 */
27c329ed 9739 if (max_pixclk > 540000)
b432e5cf 9740 cdclk = 675000;
27c329ed 9741 else if (max_pixclk > 450000)
b432e5cf 9742 cdclk = 540000;
27c329ed 9743 else if (max_pixclk > 337500)
b432e5cf
VS
9744 cdclk = 450000;
9745 else
9746 cdclk = 337500;
9747
b432e5cf 9748 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
9749 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9750 cdclk, dev_priv->max_cdclk_freq);
9751 return -EINVAL;
b432e5cf
VS
9752 }
9753
1a617b77
ML
9754 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9755 if (!intel_state->active_crtcs)
9756 intel_state->dev_cdclk = 337500;
b432e5cf
VS
9757
9758 return 0;
9759}
9760
27c329ed 9761static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9762{
27c329ed 9763 struct drm_device *dev = old_state->dev;
1a617b77
ML
9764 struct intel_atomic_state *old_intel_state =
9765 to_intel_atomic_state(old_state);
9766 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 9767
27c329ed 9768 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9769}
9770
190f68c5
ACO
9771static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9772 struct intel_crtc_state *crtc_state)
09b4ddf9 9773{
af3997b5
MK
9774 struct intel_encoder *intel_encoder =
9775 intel_ddi_get_crtc_new_encoder(crtc_state);
9776
9777 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9778 if (!intel_ddi_pll_select(crtc, crtc_state))
9779 return -EINVAL;
9780 }
716c2e55 9781
c7653199 9782 crtc->lowfreq_avail = false;
644cef34 9783
c8f7a0db 9784 return 0;
79e53945
JB
9785}
9786
3760b59c
S
9787static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9788 enum port port,
9789 struct intel_crtc_state *pipe_config)
9790{
8106ddbd
ACO
9791 enum intel_dpll_id id;
9792
3760b59c
S
9793 switch (port) {
9794 case PORT_A:
9795 pipe_config->ddi_pll_sel = SKL_DPLL0;
8106ddbd 9796 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
9797 break;
9798 case PORT_B:
9799 pipe_config->ddi_pll_sel = SKL_DPLL1;
8106ddbd 9800 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
9801 break;
9802 case PORT_C:
9803 pipe_config->ddi_pll_sel = SKL_DPLL2;
8106ddbd 9804 id = DPLL_ID_SKL_DPLL3;
3760b59c
S
9805 break;
9806 default:
9807 DRM_ERROR("Incorrect port type\n");
8106ddbd 9808 return;
3760b59c 9809 }
8106ddbd
ACO
9810
9811 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
9812}
9813
96b7dfb7
S
9814static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9815 enum port port,
5cec258b 9816 struct intel_crtc_state *pipe_config)
96b7dfb7 9817{
8106ddbd 9818 enum intel_dpll_id id;
a3c988ea 9819 u32 temp;
96b7dfb7
S
9820
9821 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9822 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9823
9824 switch (pipe_config->ddi_pll_sel) {
3148ade7 9825 case SKL_DPLL0:
a3c988ea
ACO
9826 id = DPLL_ID_SKL_DPLL0;
9827 break;
96b7dfb7 9828 case SKL_DPLL1:
8106ddbd 9829 id = DPLL_ID_SKL_DPLL1;
96b7dfb7
S
9830 break;
9831 case SKL_DPLL2:
8106ddbd 9832 id = DPLL_ID_SKL_DPLL2;
96b7dfb7
S
9833 break;
9834 case SKL_DPLL3:
8106ddbd 9835 id = DPLL_ID_SKL_DPLL3;
96b7dfb7 9836 break;
8106ddbd
ACO
9837 default:
9838 MISSING_CASE(pipe_config->ddi_pll_sel);
9839 return;
96b7dfb7 9840 }
8106ddbd
ACO
9841
9842 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
9843}
9844
7d2c8175
DL
9845static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9846 enum port port,
5cec258b 9847 struct intel_crtc_state *pipe_config)
7d2c8175 9848{
8106ddbd
ACO
9849 enum intel_dpll_id id;
9850
7d2c8175
DL
9851 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9852
9853 switch (pipe_config->ddi_pll_sel) {
9854 case PORT_CLK_SEL_WRPLL1:
8106ddbd 9855 id = DPLL_ID_WRPLL1;
7d2c8175
DL
9856 break;
9857 case PORT_CLK_SEL_WRPLL2:
8106ddbd 9858 id = DPLL_ID_WRPLL2;
7d2c8175 9859 break;
00490c22 9860 case PORT_CLK_SEL_SPLL:
8106ddbd 9861 id = DPLL_ID_SPLL;
79bd23da 9862 break;
9d16da65
ACO
9863 case PORT_CLK_SEL_LCPLL_810:
9864 id = DPLL_ID_LCPLL_810;
9865 break;
9866 case PORT_CLK_SEL_LCPLL_1350:
9867 id = DPLL_ID_LCPLL_1350;
9868 break;
9869 case PORT_CLK_SEL_LCPLL_2700:
9870 id = DPLL_ID_LCPLL_2700;
9871 break;
8106ddbd
ACO
9872 default:
9873 MISSING_CASE(pipe_config->ddi_pll_sel);
9874 /* fall through */
9875 case PORT_CLK_SEL_NONE:
8106ddbd 9876 return;
7d2c8175 9877 }
8106ddbd
ACO
9878
9879 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
9880}
9881
26804afd 9882static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9883 struct intel_crtc_state *pipe_config)
26804afd
DV
9884{
9885 struct drm_device *dev = crtc->base.dev;
9886 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9887 struct intel_shared_dpll *pll;
26804afd
DV
9888 enum port port;
9889 uint32_t tmp;
9890
9891 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9892
9893 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9894
ef11bdb3 9895 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9896 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9897 else if (IS_BROXTON(dev))
9898 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9899 else
9900 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9901
8106ddbd
ACO
9902 pll = pipe_config->shared_dpll;
9903 if (pll) {
2edd6443
ACO
9904 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9905 &pipe_config->dpll_hw_state));
d452c5b6
DV
9906 }
9907
26804afd
DV
9908 /*
9909 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9910 * DDI E. So just check whether this pipe is wired to DDI E and whether
9911 * the PCH transcoder is on.
9912 */
ca370455
DL
9913 if (INTEL_INFO(dev)->gen < 9 &&
9914 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9915 pipe_config->has_pch_encoder = true;
9916
9917 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9918 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9919 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9920
9921 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9922 }
9923}
9924
0e8ffe1b 9925static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9926 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9927{
9928 struct drm_device *dev = crtc->base.dev;
9929 struct drm_i915_private *dev_priv = dev->dev_private;
1729050e
ID
9930 enum intel_display_power_domain power_domain;
9931 unsigned long power_domain_mask;
0e8ffe1b 9932 uint32_t tmp;
1729050e 9933 bool ret;
0e8ffe1b 9934
1729050e
ID
9935 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9936 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 9937 return false;
1729050e
ID
9938 power_domain_mask = BIT(power_domain);
9939
9940 ret = false;
b5482bd0 9941
e143a21c 9942 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9943 pipe_config->shared_dpll = NULL;
c0d43d62 9944
eccb140b
DV
9945 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9946 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9947 enum pipe trans_edp_pipe;
9948 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9949 default:
9950 WARN(1, "unknown pipe linked to edp transcoder\n");
9951 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9952 case TRANS_DDI_EDP_INPUT_A_ON:
9953 trans_edp_pipe = PIPE_A;
9954 break;
9955 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9956 trans_edp_pipe = PIPE_B;
9957 break;
9958 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9959 trans_edp_pipe = PIPE_C;
9960 break;
9961 }
9962
9963 if (trans_edp_pipe == crtc->pipe)
9964 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9965 }
9966
1729050e
ID
9967 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9968 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9969 goto out;
9970 power_domain_mask |= BIT(power_domain);
2bfce950 9971
eccb140b 9972 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b 9973 if (!(tmp & PIPECONF_ENABLE))
1729050e 9974 goto out;
0e8ffe1b 9975
26804afd 9976 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9977
1bd1bd80
DV
9978 intel_get_pipe_timings(crtc, pipe_config);
9979
a1b2278e
CK
9980 if (INTEL_INFO(dev)->gen >= 9) {
9981 skl_init_scalers(dev, crtc, pipe_config);
9982 }
9983
af99ceda
CK
9984 if (INTEL_INFO(dev)->gen >= 9) {
9985 pipe_config->scaler_state.scaler_id = -1;
9986 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9987 }
9988
1729050e
ID
9989 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9990 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9991 power_domain_mask |= BIT(power_domain);
1c132b44 9992 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 9993 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9994 else
1c132b44 9995 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9996 }
88adfff1 9997
e59150dc
JB
9998 if (IS_HASWELL(dev))
9999 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10000 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10001
ebb69c95
CT
10002 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10003 pipe_config->pixel_multiplier =
10004 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10005 } else {
10006 pipe_config->pixel_multiplier = 1;
10007 }
6c49f241 10008
1729050e
ID
10009 ret = true;
10010
10011out:
10012 for_each_power_domain(power_domain, power_domain_mask)
10013 intel_display_power_put(dev_priv, power_domain);
10014
10015 return ret;
0e8ffe1b
DV
10016}
10017
55a08b3f
ML
10018static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10019 const struct intel_plane_state *plane_state)
560b85bb
CW
10020{
10021 struct drm_device *dev = crtc->dev;
10022 struct drm_i915_private *dev_priv = dev->dev_private;
10023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10024 uint32_t cntl = 0, size = 0;
560b85bb 10025
55a08b3f
ML
10026 if (plane_state && plane_state->visible) {
10027 unsigned int width = plane_state->base.crtc_w;
10028 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10029 unsigned int stride = roundup_pow_of_two(width) * 4;
10030
10031 switch (stride) {
10032 default:
10033 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10034 width, stride);
10035 stride = 256;
10036 /* fallthrough */
10037 case 256:
10038 case 512:
10039 case 1024:
10040 case 2048:
10041 break;
4b0e333e
CW
10042 }
10043
dc41c154
VS
10044 cntl |= CURSOR_ENABLE |
10045 CURSOR_GAMMA_ENABLE |
10046 CURSOR_FORMAT_ARGB |
10047 CURSOR_STRIDE(stride);
10048
10049 size = (height << 12) | width;
4b0e333e 10050 }
560b85bb 10051
dc41c154
VS
10052 if (intel_crtc->cursor_cntl != 0 &&
10053 (intel_crtc->cursor_base != base ||
10054 intel_crtc->cursor_size != size ||
10055 intel_crtc->cursor_cntl != cntl)) {
10056 /* On these chipsets we can only modify the base/size/stride
10057 * whilst the cursor is disabled.
10058 */
0b87c24e
VS
10059 I915_WRITE(CURCNTR(PIPE_A), 0);
10060 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10061 intel_crtc->cursor_cntl = 0;
4b0e333e 10062 }
560b85bb 10063
99d1f387 10064 if (intel_crtc->cursor_base != base) {
0b87c24e 10065 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10066 intel_crtc->cursor_base = base;
10067 }
4726e0b0 10068
dc41c154
VS
10069 if (intel_crtc->cursor_size != size) {
10070 I915_WRITE(CURSIZE, size);
10071 intel_crtc->cursor_size = size;
4b0e333e 10072 }
560b85bb 10073
4b0e333e 10074 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10075 I915_WRITE(CURCNTR(PIPE_A), cntl);
10076 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10077 intel_crtc->cursor_cntl = cntl;
560b85bb 10078 }
560b85bb
CW
10079}
10080
55a08b3f
ML
10081static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10082 const struct intel_plane_state *plane_state)
65a21cd6
JB
10083{
10084 struct drm_device *dev = crtc->dev;
10085 struct drm_i915_private *dev_priv = dev->dev_private;
10086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10087 int pipe = intel_crtc->pipe;
663f3122 10088 uint32_t cntl = 0;
4b0e333e 10089
55a08b3f 10090 if (plane_state && plane_state->visible) {
4b0e333e 10091 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10092 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10093 case 64:
10094 cntl |= CURSOR_MODE_64_ARGB_AX;
10095 break;
10096 case 128:
10097 cntl |= CURSOR_MODE_128_ARGB_AX;
10098 break;
10099 case 256:
10100 cntl |= CURSOR_MODE_256_ARGB_AX;
10101 break;
10102 default:
55a08b3f 10103 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10104 return;
65a21cd6 10105 }
4b0e333e 10106 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10107
fc6f93bc 10108 if (HAS_DDI(dev))
47bf17a7 10109 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10110
55a08b3f
ML
10111 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10112 cntl |= CURSOR_ROTATE_180;
10113 }
4398ad45 10114
4b0e333e
CW
10115 if (intel_crtc->cursor_cntl != cntl) {
10116 I915_WRITE(CURCNTR(pipe), cntl);
10117 POSTING_READ(CURCNTR(pipe));
10118 intel_crtc->cursor_cntl = cntl;
65a21cd6 10119 }
4b0e333e 10120
65a21cd6 10121 /* and commit changes on next vblank */
5efb3e28
VS
10122 I915_WRITE(CURBASE(pipe), base);
10123 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10124
10125 intel_crtc->cursor_base = base;
65a21cd6
JB
10126}
10127
cda4b7d3 10128/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10129static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10130 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10131{
10132 struct drm_device *dev = crtc->dev;
10133 struct drm_i915_private *dev_priv = dev->dev_private;
10134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10135 int pipe = intel_crtc->pipe;
55a08b3f
ML
10136 u32 base = intel_crtc->cursor_addr;
10137 u32 pos = 0;
cda4b7d3 10138
55a08b3f
ML
10139 if (plane_state) {
10140 int x = plane_state->base.crtc_x;
10141 int y = plane_state->base.crtc_y;
cda4b7d3 10142
55a08b3f
ML
10143 if (x < 0) {
10144 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10145 x = -x;
10146 }
10147 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10148
55a08b3f
ML
10149 if (y < 0) {
10150 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10151 y = -y;
10152 }
10153 pos |= y << CURSOR_Y_SHIFT;
10154
10155 /* ILK+ do this automagically */
10156 if (HAS_GMCH_DISPLAY(dev) &&
10157 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10158 base += (plane_state->base.crtc_h *
10159 plane_state->base.crtc_w - 1) * 4;
10160 }
cda4b7d3 10161 }
cda4b7d3 10162
5efb3e28
VS
10163 I915_WRITE(CURPOS(pipe), pos);
10164
8ac54669 10165 if (IS_845G(dev) || IS_I865G(dev))
55a08b3f 10166 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10167 else
55a08b3f 10168 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10169}
10170
dc41c154
VS
10171static bool cursor_size_ok(struct drm_device *dev,
10172 uint32_t width, uint32_t height)
10173{
10174 if (width == 0 || height == 0)
10175 return false;
10176
10177 /*
10178 * 845g/865g are special in that they are only limited by
10179 * the width of their cursors, the height is arbitrary up to
10180 * the precision of the register. Everything else requires
10181 * square cursors, limited to a few power-of-two sizes.
10182 */
10183 if (IS_845G(dev) || IS_I865G(dev)) {
10184 if ((width & 63) != 0)
10185 return false;
10186
10187 if (width > (IS_845G(dev) ? 64 : 512))
10188 return false;
10189
10190 if (height > 1023)
10191 return false;
10192 } else {
10193 switch (width | height) {
10194 case 256:
10195 case 128:
10196 if (IS_GEN2(dev))
10197 return false;
10198 case 64:
10199 break;
10200 default:
10201 return false;
10202 }
10203 }
10204
10205 return true;
10206}
10207
79e53945 10208static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10209 u16 *blue, uint32_t start, uint32_t size)
79e53945 10210{
7203425a 10211 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10213
7203425a 10214 for (i = start; i < end; i++) {
79e53945
JB
10215 intel_crtc->lut_r[i] = red[i] >> 8;
10216 intel_crtc->lut_g[i] = green[i] >> 8;
10217 intel_crtc->lut_b[i] = blue[i] >> 8;
10218 }
10219
10220 intel_crtc_load_lut(crtc);
10221}
10222
79e53945
JB
10223/* VESA 640x480x72Hz mode to set on the pipe */
10224static struct drm_display_mode load_detect_mode = {
10225 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10226 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10227};
10228
a8bb6818
DV
10229struct drm_framebuffer *
10230__intel_framebuffer_create(struct drm_device *dev,
10231 struct drm_mode_fb_cmd2 *mode_cmd,
10232 struct drm_i915_gem_object *obj)
d2dff872
CW
10233{
10234 struct intel_framebuffer *intel_fb;
10235 int ret;
10236
10237 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10238 if (!intel_fb)
d2dff872 10239 return ERR_PTR(-ENOMEM);
d2dff872
CW
10240
10241 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10242 if (ret)
10243 goto err;
d2dff872
CW
10244
10245 return &intel_fb->base;
dcb1394e 10246
dd4916c5 10247err:
dd4916c5 10248 kfree(intel_fb);
dd4916c5 10249 return ERR_PTR(ret);
d2dff872
CW
10250}
10251
b5ea642a 10252static struct drm_framebuffer *
a8bb6818
DV
10253intel_framebuffer_create(struct drm_device *dev,
10254 struct drm_mode_fb_cmd2 *mode_cmd,
10255 struct drm_i915_gem_object *obj)
10256{
10257 struct drm_framebuffer *fb;
10258 int ret;
10259
10260 ret = i915_mutex_lock_interruptible(dev);
10261 if (ret)
10262 return ERR_PTR(ret);
10263 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10264 mutex_unlock(&dev->struct_mutex);
10265
10266 return fb;
10267}
10268
d2dff872
CW
10269static u32
10270intel_framebuffer_pitch_for_width(int width, int bpp)
10271{
10272 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10273 return ALIGN(pitch, 64);
10274}
10275
10276static u32
10277intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10278{
10279 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10280 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10281}
10282
10283static struct drm_framebuffer *
10284intel_framebuffer_create_for_mode(struct drm_device *dev,
10285 struct drm_display_mode *mode,
10286 int depth, int bpp)
10287{
dcb1394e 10288 struct drm_framebuffer *fb;
d2dff872 10289 struct drm_i915_gem_object *obj;
0fed39bd 10290 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10291
10292 obj = i915_gem_alloc_object(dev,
10293 intel_framebuffer_size_for_mode(mode, bpp));
10294 if (obj == NULL)
10295 return ERR_PTR(-ENOMEM);
10296
10297 mode_cmd.width = mode->hdisplay;
10298 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10299 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10300 bpp);
5ca0c34a 10301 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10302
dcb1394e
LW
10303 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10304 if (IS_ERR(fb))
10305 drm_gem_object_unreference_unlocked(&obj->base);
10306
10307 return fb;
d2dff872
CW
10308}
10309
10310static struct drm_framebuffer *
10311mode_fits_in_fbdev(struct drm_device *dev,
10312 struct drm_display_mode *mode)
10313{
0695726e 10314#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10315 struct drm_i915_private *dev_priv = dev->dev_private;
10316 struct drm_i915_gem_object *obj;
10317 struct drm_framebuffer *fb;
10318
4c0e5528 10319 if (!dev_priv->fbdev)
d2dff872
CW
10320 return NULL;
10321
4c0e5528 10322 if (!dev_priv->fbdev->fb)
d2dff872
CW
10323 return NULL;
10324
4c0e5528
DV
10325 obj = dev_priv->fbdev->fb->obj;
10326 BUG_ON(!obj);
10327
8bcd4553 10328 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10329 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10330 fb->bits_per_pixel))
d2dff872
CW
10331 return NULL;
10332
01f2c773 10333 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10334 return NULL;
10335
edde3617 10336 drm_framebuffer_reference(fb);
d2dff872 10337 return fb;
4520f53a
DV
10338#else
10339 return NULL;
10340#endif
d2dff872
CW
10341}
10342
d3a40d1b
ACO
10343static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10344 struct drm_crtc *crtc,
10345 struct drm_display_mode *mode,
10346 struct drm_framebuffer *fb,
10347 int x, int y)
10348{
10349 struct drm_plane_state *plane_state;
10350 int hdisplay, vdisplay;
10351 int ret;
10352
10353 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10354 if (IS_ERR(plane_state))
10355 return PTR_ERR(plane_state);
10356
10357 if (mode)
10358 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10359 else
10360 hdisplay = vdisplay = 0;
10361
10362 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10363 if (ret)
10364 return ret;
10365 drm_atomic_set_fb_for_plane(plane_state, fb);
10366 plane_state->crtc_x = 0;
10367 plane_state->crtc_y = 0;
10368 plane_state->crtc_w = hdisplay;
10369 plane_state->crtc_h = vdisplay;
10370 plane_state->src_x = x << 16;
10371 plane_state->src_y = y << 16;
10372 plane_state->src_w = hdisplay << 16;
10373 plane_state->src_h = vdisplay << 16;
10374
10375 return 0;
10376}
10377
d2434ab7 10378bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10379 struct drm_display_mode *mode,
51fd371b
RC
10380 struct intel_load_detect_pipe *old,
10381 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10382{
10383 struct intel_crtc *intel_crtc;
d2434ab7
DV
10384 struct intel_encoder *intel_encoder =
10385 intel_attached_encoder(connector);
79e53945 10386 struct drm_crtc *possible_crtc;
4ef69c7a 10387 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10388 struct drm_crtc *crtc = NULL;
10389 struct drm_device *dev = encoder->dev;
94352cf9 10390 struct drm_framebuffer *fb;
51fd371b 10391 struct drm_mode_config *config = &dev->mode_config;
edde3617 10392 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 10393 struct drm_connector_state *connector_state;
4be07317 10394 struct intel_crtc_state *crtc_state;
51fd371b 10395 int ret, i = -1;
79e53945 10396
d2dff872 10397 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10398 connector->base.id, connector->name,
8e329a03 10399 encoder->base.id, encoder->name);
d2dff872 10400
edde3617
ML
10401 old->restore_state = NULL;
10402
51fd371b
RC
10403retry:
10404 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10405 if (ret)
ad3c558f 10406 goto fail;
6e9f798d 10407
79e53945
JB
10408 /*
10409 * Algorithm gets a little messy:
7a5e4805 10410 *
79e53945
JB
10411 * - if the connector already has an assigned crtc, use it (but make
10412 * sure it's on first)
7a5e4805 10413 *
79e53945
JB
10414 * - try to find the first unused crtc that can drive this connector,
10415 * and use that if we find one
79e53945
JB
10416 */
10417
10418 /* See if we already have a CRTC for this connector */
edde3617
ML
10419 if (connector->state->crtc) {
10420 crtc = connector->state->crtc;
8261b191 10421
51fd371b 10422 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10423 if (ret)
ad3c558f 10424 goto fail;
8261b191
CW
10425
10426 /* Make sure the crtc and connector are running */
edde3617 10427 goto found;
79e53945
JB
10428 }
10429
10430 /* Find an unused one (if possible) */
70e1e0ec 10431 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10432 i++;
10433 if (!(encoder->possible_crtcs & (1 << i)))
10434 continue;
edde3617
ML
10435
10436 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10437 if (ret)
10438 goto fail;
10439
10440 if (possible_crtc->state->enable) {
10441 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 10442 continue;
edde3617 10443 }
a459249c
VS
10444
10445 crtc = possible_crtc;
10446 break;
79e53945
JB
10447 }
10448
10449 /*
10450 * If we didn't find an unused CRTC, don't use any.
10451 */
10452 if (!crtc) {
7173188d 10453 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10454 goto fail;
79e53945
JB
10455 }
10456
edde3617
ML
10457found:
10458 intel_crtc = to_intel_crtc(crtc);
10459
4d02e2de
DV
10460 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10461 if (ret)
ad3c558f 10462 goto fail;
79e53945 10463
83a57153 10464 state = drm_atomic_state_alloc(dev);
edde3617
ML
10465 restore_state = drm_atomic_state_alloc(dev);
10466 if (!state || !restore_state) {
10467 ret = -ENOMEM;
10468 goto fail;
10469 }
83a57153
ACO
10470
10471 state->acquire_ctx = ctx;
edde3617 10472 restore_state->acquire_ctx = ctx;
83a57153 10473
944b0c76
ACO
10474 connector_state = drm_atomic_get_connector_state(state, connector);
10475 if (IS_ERR(connector_state)) {
10476 ret = PTR_ERR(connector_state);
10477 goto fail;
10478 }
10479
edde3617
ML
10480 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10481 if (ret)
10482 goto fail;
944b0c76 10483
4be07317
ACO
10484 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10485 if (IS_ERR(crtc_state)) {
10486 ret = PTR_ERR(crtc_state);
10487 goto fail;
10488 }
10489
49d6fa21 10490 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10491
6492711d
CW
10492 if (!mode)
10493 mode = &load_detect_mode;
79e53945 10494
d2dff872
CW
10495 /* We need a framebuffer large enough to accommodate all accesses
10496 * that the plane may generate whilst we perform load detection.
10497 * We can not rely on the fbcon either being present (we get called
10498 * during its initialisation to detect all boot displays, or it may
10499 * not even exist) or that it is large enough to satisfy the
10500 * requested mode.
10501 */
94352cf9
DV
10502 fb = mode_fits_in_fbdev(dev, mode);
10503 if (fb == NULL) {
d2dff872 10504 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 10505 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
10506 } else
10507 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10508 if (IS_ERR(fb)) {
d2dff872 10509 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10510 goto fail;
79e53945 10511 }
79e53945 10512
d3a40d1b
ACO
10513 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10514 if (ret)
10515 goto fail;
10516
edde3617
ML
10517 drm_framebuffer_unreference(fb);
10518
10519 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10520 if (ret)
10521 goto fail;
10522
10523 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10524 if (!ret)
10525 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10526 if (!ret)
10527 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10528 if (ret) {
10529 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10530 goto fail;
10531 }
8c7b5ccb 10532
3ba86073
ML
10533 ret = drm_atomic_commit(state);
10534 if (ret) {
6492711d 10535 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 10536 goto fail;
79e53945 10537 }
edde3617
ML
10538
10539 old->restore_state = restore_state;
7173188d 10540
79e53945 10541 /* let the connector get through one full cycle before testing */
9d0498a2 10542 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10543 return true;
412b61d8 10544
ad3c558f 10545fail:
e5d958ef 10546 drm_atomic_state_free(state);
edde3617
ML
10547 drm_atomic_state_free(restore_state);
10548 restore_state = state = NULL;
83a57153 10549
51fd371b
RC
10550 if (ret == -EDEADLK) {
10551 drm_modeset_backoff(ctx);
10552 goto retry;
10553 }
10554
412b61d8 10555 return false;
79e53945
JB
10556}
10557
d2434ab7 10558void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10559 struct intel_load_detect_pipe *old,
10560 struct drm_modeset_acquire_ctx *ctx)
79e53945 10561{
d2434ab7
DV
10562 struct intel_encoder *intel_encoder =
10563 intel_attached_encoder(connector);
4ef69c7a 10564 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 10565 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 10566 int ret;
79e53945 10567
d2dff872 10568 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10569 connector->base.id, connector->name,
8e329a03 10570 encoder->base.id, encoder->name);
d2dff872 10571
edde3617 10572 if (!state)
0622a53c 10573 return;
79e53945 10574
edde3617
ML
10575 ret = drm_atomic_commit(state);
10576 if (ret) {
10577 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10578 drm_atomic_state_free(state);
10579 }
79e53945
JB
10580}
10581
da4a1efa 10582static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10583 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10584{
10585 struct drm_i915_private *dev_priv = dev->dev_private;
10586 u32 dpll = pipe_config->dpll_hw_state.dpll;
10587
10588 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10589 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10590 else if (HAS_PCH_SPLIT(dev))
10591 return 120000;
10592 else if (!IS_GEN2(dev))
10593 return 96000;
10594 else
10595 return 48000;
10596}
10597
79e53945 10598/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10599static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10600 struct intel_crtc_state *pipe_config)
79e53945 10601{
f1f644dc 10602 struct drm_device *dev = crtc->base.dev;
79e53945 10603 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10604 int pipe = pipe_config->cpu_transcoder;
293623f7 10605 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10606 u32 fp;
10607 intel_clock_t clock;
dccbea3b 10608 int port_clock;
da4a1efa 10609 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10610
10611 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10612 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10613 else
293623f7 10614 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10615
10616 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10617 if (IS_PINEVIEW(dev)) {
10618 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10619 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10620 } else {
10621 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10622 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10623 }
10624
a6c45cf0 10625 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10626 if (IS_PINEVIEW(dev))
10627 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10628 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10629 else
10630 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10631 DPLL_FPA01_P1_POST_DIV_SHIFT);
10632
10633 switch (dpll & DPLL_MODE_MASK) {
10634 case DPLLB_MODE_DAC_SERIAL:
10635 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10636 5 : 10;
10637 break;
10638 case DPLLB_MODE_LVDS:
10639 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10640 7 : 14;
10641 break;
10642 default:
28c97730 10643 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10644 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10645 return;
79e53945
JB
10646 }
10647
ac58c3f0 10648 if (IS_PINEVIEW(dev))
dccbea3b 10649 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10650 else
dccbea3b 10651 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10652 } else {
0fb58223 10653 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10654 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10655
10656 if (is_lvds) {
10657 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10658 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10659
10660 if (lvds & LVDS_CLKB_POWER_UP)
10661 clock.p2 = 7;
10662 else
10663 clock.p2 = 14;
79e53945
JB
10664 } else {
10665 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10666 clock.p1 = 2;
10667 else {
10668 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10669 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10670 }
10671 if (dpll & PLL_P2_DIVIDE_BY_4)
10672 clock.p2 = 4;
10673 else
10674 clock.p2 = 2;
79e53945 10675 }
da4a1efa 10676
dccbea3b 10677 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10678 }
10679
18442d08
VS
10680 /*
10681 * This value includes pixel_multiplier. We will use
241bfc38 10682 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10683 * encoder's get_config() function.
10684 */
dccbea3b 10685 pipe_config->port_clock = port_clock;
f1f644dc
JB
10686}
10687
6878da05
VS
10688int intel_dotclock_calculate(int link_freq,
10689 const struct intel_link_m_n *m_n)
f1f644dc 10690{
f1f644dc
JB
10691 /*
10692 * The calculation for the data clock is:
1041a02f 10693 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10694 * But we want to avoid losing precison if possible, so:
1041a02f 10695 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10696 *
10697 * and the link clock is simpler:
1041a02f 10698 * link_clock = (m * link_clock) / n
f1f644dc
JB
10699 */
10700
6878da05
VS
10701 if (!m_n->link_n)
10702 return 0;
f1f644dc 10703
6878da05
VS
10704 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10705}
f1f644dc 10706
18442d08 10707static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10708 struct intel_crtc_state *pipe_config)
6878da05 10709{
e3b247da 10710 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 10711
18442d08
VS
10712 /* read out port_clock from the DPLL */
10713 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10714
f1f644dc 10715 /*
e3b247da
VS
10716 * In case there is an active pipe without active ports,
10717 * we may need some idea for the dotclock anyway.
10718 * Calculate one based on the FDI configuration.
79e53945 10719 */
2d112de7 10720 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 10721 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 10722 &pipe_config->fdi_m_n);
79e53945
JB
10723}
10724
10725/** Returns the currently programmed mode of the given pipe. */
10726struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10727 struct drm_crtc *crtc)
10728{
548f245b 10729 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10731 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10732 struct drm_display_mode *mode;
3f36b937 10733 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
10734 int htot = I915_READ(HTOTAL(cpu_transcoder));
10735 int hsync = I915_READ(HSYNC(cpu_transcoder));
10736 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10737 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10738 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10739
10740 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10741 if (!mode)
10742 return NULL;
10743
3f36b937
TU
10744 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10745 if (!pipe_config) {
10746 kfree(mode);
10747 return NULL;
10748 }
10749
f1f644dc
JB
10750 /*
10751 * Construct a pipe_config sufficient for getting the clock info
10752 * back out of crtc_clock_get.
10753 *
10754 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10755 * to use a real value here instead.
10756 */
3f36b937
TU
10757 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10758 pipe_config->pixel_multiplier = 1;
10759 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10760 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10761 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10762 i9xx_crtc_clock_get(intel_crtc, pipe_config);
10763
10764 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
10765 mode->hdisplay = (htot & 0xffff) + 1;
10766 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10767 mode->hsync_start = (hsync & 0xffff) + 1;
10768 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10769 mode->vdisplay = (vtot & 0xffff) + 1;
10770 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10771 mode->vsync_start = (vsync & 0xffff) + 1;
10772 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10773
10774 drm_mode_set_name(mode);
79e53945 10775
3f36b937
TU
10776 kfree(pipe_config);
10777
79e53945
JB
10778 return mode;
10779}
10780
f047e395
CW
10781void intel_mark_busy(struct drm_device *dev)
10782{
c67a470b
PZ
10783 struct drm_i915_private *dev_priv = dev->dev_private;
10784
f62a0076
CW
10785 if (dev_priv->mm.busy)
10786 return;
10787
43694d69 10788 intel_runtime_pm_get(dev_priv);
c67a470b 10789 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10790 if (INTEL_INFO(dev)->gen >= 6)
10791 gen6_rps_busy(dev_priv);
f62a0076 10792 dev_priv->mm.busy = true;
f047e395
CW
10793}
10794
10795void intel_mark_idle(struct drm_device *dev)
652c393a 10796{
c67a470b 10797 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10798
f62a0076
CW
10799 if (!dev_priv->mm.busy)
10800 return;
10801
10802 dev_priv->mm.busy = false;
10803
3d13ef2e 10804 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10805 gen6_rps_idle(dev->dev_private);
bb4cdd53 10806
43694d69 10807 intel_runtime_pm_put(dev_priv);
652c393a
JB
10808}
10809
79e53945
JB
10810static void intel_crtc_destroy(struct drm_crtc *crtc)
10811{
10812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10813 struct drm_device *dev = crtc->dev;
10814 struct intel_unpin_work *work;
67e77c5a 10815
5e2d7afc 10816 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10817 work = intel_crtc->unpin_work;
10818 intel_crtc->unpin_work = NULL;
5e2d7afc 10819 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10820
10821 if (work) {
10822 cancel_work_sync(&work->work);
10823 kfree(work);
10824 }
79e53945
JB
10825
10826 drm_crtc_cleanup(crtc);
67e77c5a 10827
79e53945
JB
10828 kfree(intel_crtc);
10829}
10830
6b95a207
KH
10831static void intel_unpin_work_fn(struct work_struct *__work)
10832{
10833 struct intel_unpin_work *work =
10834 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10835 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10836 struct drm_device *dev = crtc->base.dev;
10837 struct drm_plane *primary = crtc->base.primary;
6b95a207 10838
b4a98e57 10839 mutex_lock(&dev->struct_mutex);
3465c580 10840 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
05394f39 10841 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10842
f06cc1b9 10843 if (work->flip_queued_req)
146d84f0 10844 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10845 mutex_unlock(&dev->struct_mutex);
10846
a9ff8714 10847 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
1eb52238 10848 intel_fbc_post_update(crtc);
89ed88ba 10849 drm_framebuffer_unreference(work->old_fb);
f99d7069 10850
a9ff8714
VS
10851 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10852 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10853
6b95a207
KH
10854 kfree(work);
10855}
10856
1afe3e9d 10857static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10858 struct drm_crtc *crtc)
6b95a207 10859{
6b95a207
KH
10860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10861 struct intel_unpin_work *work;
6b95a207
KH
10862 unsigned long flags;
10863
10864 /* Ignore early vblank irqs */
10865 if (intel_crtc == NULL)
10866 return;
10867
f326038a
DV
10868 /*
10869 * This is called both by irq handlers and the reset code (to complete
10870 * lost pageflips) so needs the full irqsave spinlocks.
10871 */
6b95a207
KH
10872 spin_lock_irqsave(&dev->event_lock, flags);
10873 work = intel_crtc->unpin_work;
e7d841ca
CW
10874
10875 /* Ensure we don't miss a work->pending update ... */
10876 smp_rmb();
10877
10878 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10879 spin_unlock_irqrestore(&dev->event_lock, flags);
10880 return;
10881 }
10882
d6bbafa1 10883 page_flip_completed(intel_crtc);
0af7e4df 10884
6b95a207 10885 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10886}
10887
1afe3e9d
JB
10888void intel_finish_page_flip(struct drm_device *dev, int pipe)
10889{
fbee40df 10890 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10891 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10892
49b14a5c 10893 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10894}
10895
10896void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10897{
fbee40df 10898 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10899 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10900
49b14a5c 10901 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10902}
10903
75f7f3ec
VS
10904/* Is 'a' after or equal to 'b'? */
10905static bool g4x_flip_count_after_eq(u32 a, u32 b)
10906{
10907 return !((a - b) & 0x80000000);
10908}
10909
10910static bool page_flip_finished(struct intel_crtc *crtc)
10911{
10912 struct drm_device *dev = crtc->base.dev;
10913 struct drm_i915_private *dev_priv = dev->dev_private;
10914
bdfa7542
VS
10915 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10916 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10917 return true;
10918
75f7f3ec
VS
10919 /*
10920 * The relevant registers doen't exist on pre-ctg.
10921 * As the flip done interrupt doesn't trigger for mmio
10922 * flips on gmch platforms, a flip count check isn't
10923 * really needed there. But since ctg has the registers,
10924 * include it in the check anyway.
10925 */
10926 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10927 return true;
10928
e8861675
ML
10929 /*
10930 * BDW signals flip done immediately if the plane
10931 * is disabled, even if the plane enable is already
10932 * armed to occur at the next vblank :(
10933 */
10934
75f7f3ec
VS
10935 /*
10936 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10937 * used the same base address. In that case the mmio flip might
10938 * have completed, but the CS hasn't even executed the flip yet.
10939 *
10940 * A flip count check isn't enough as the CS might have updated
10941 * the base address just after start of vblank, but before we
10942 * managed to process the interrupt. This means we'd complete the
10943 * CS flip too soon.
10944 *
10945 * Combining both checks should get us a good enough result. It may
10946 * still happen that the CS flip has been executed, but has not
10947 * yet actually completed. But in case the base address is the same
10948 * anyway, we don't really care.
10949 */
10950 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10951 crtc->unpin_work->gtt_offset &&
fd8f507c 10952 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10953 crtc->unpin_work->flip_count);
10954}
10955
6b95a207
KH
10956void intel_prepare_page_flip(struct drm_device *dev, int plane)
10957{
fbee40df 10958 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10959 struct intel_crtc *intel_crtc =
10960 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10961 unsigned long flags;
10962
f326038a
DV
10963
10964 /*
10965 * This is called both by irq handlers and the reset code (to complete
10966 * lost pageflips) so needs the full irqsave spinlocks.
10967 *
10968 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10969 * generate a page-flip completion irq, i.e. every modeset
10970 * is also accompanied by a spurious intel_prepare_page_flip().
10971 */
6b95a207 10972 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10973 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10974 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10975 spin_unlock_irqrestore(&dev->event_lock, flags);
10976}
10977
6042639c 10978static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10979{
10980 /* Ensure that the work item is consistent when activating it ... */
10981 smp_wmb();
6042639c 10982 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10983 /* and that it is marked active as soon as the irq could fire. */
10984 smp_wmb();
10985}
10986
8c9f3aaf
JB
10987static int intel_gen2_queue_flip(struct drm_device *dev,
10988 struct drm_crtc *crtc,
10989 struct drm_framebuffer *fb,
ed8d1975 10990 struct drm_i915_gem_object *obj,
6258fbe2 10991 struct drm_i915_gem_request *req,
ed8d1975 10992 uint32_t flags)
8c9f3aaf 10993{
6258fbe2 10994 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10996 u32 flip_mask;
10997 int ret;
10998
5fb9de1a 10999 ret = intel_ring_begin(req, 6);
8c9f3aaf 11000 if (ret)
4fa62c89 11001 return ret;
8c9f3aaf
JB
11002
11003 /* Can't queue multiple flips, so wait for the previous
11004 * one to finish before executing the next.
11005 */
11006 if (intel_crtc->plane)
11007 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11008 else
11009 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11010 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11011 intel_ring_emit(ring, MI_NOOP);
11012 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11013 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11014 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11015 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 11016 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca 11017
6042639c 11018 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11019 return 0;
8c9f3aaf
JB
11020}
11021
11022static int intel_gen3_queue_flip(struct drm_device *dev,
11023 struct drm_crtc *crtc,
11024 struct drm_framebuffer *fb,
ed8d1975 11025 struct drm_i915_gem_object *obj,
6258fbe2 11026 struct drm_i915_gem_request *req,
ed8d1975 11027 uint32_t flags)
8c9f3aaf 11028{
6258fbe2 11029 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 11030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11031 u32 flip_mask;
11032 int ret;
11033
5fb9de1a 11034 ret = intel_ring_begin(req, 6);
8c9f3aaf 11035 if (ret)
4fa62c89 11036 return ret;
8c9f3aaf
JB
11037
11038 if (intel_crtc->plane)
11039 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11040 else
11041 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11042 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11043 intel_ring_emit(ring, MI_NOOP);
11044 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11045 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11046 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11047 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
11048 intel_ring_emit(ring, MI_NOOP);
11049
6042639c 11050 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11051 return 0;
8c9f3aaf
JB
11052}
11053
11054static int intel_gen4_queue_flip(struct drm_device *dev,
11055 struct drm_crtc *crtc,
11056 struct drm_framebuffer *fb,
ed8d1975 11057 struct drm_i915_gem_object *obj,
6258fbe2 11058 struct drm_i915_gem_request *req,
ed8d1975 11059 uint32_t flags)
8c9f3aaf 11060{
6258fbe2 11061 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
11062 struct drm_i915_private *dev_priv = dev->dev_private;
11063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11064 uint32_t pf, pipesrc;
11065 int ret;
11066
5fb9de1a 11067 ret = intel_ring_begin(req, 4);
8c9f3aaf 11068 if (ret)
4fa62c89 11069 return ret;
8c9f3aaf
JB
11070
11071 /* i965+ uses the linear or tiled offsets from the
11072 * Display Registers (which do not change across a page-flip)
11073 * so we need only reprogram the base address.
11074 */
6d90c952
DV
11075 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11076 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11077 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11078 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 11079 obj->tiling_mode);
8c9f3aaf
JB
11080
11081 /* XXX Enabling the panel-fitter across page-flip is so far
11082 * untested on non-native modes, so ignore it for now.
11083 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11084 */
11085 pf = 0;
11086 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11087 intel_ring_emit(ring, pf | pipesrc);
e7d841ca 11088
6042639c 11089 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11090 return 0;
8c9f3aaf
JB
11091}
11092
11093static int intel_gen6_queue_flip(struct drm_device *dev,
11094 struct drm_crtc *crtc,
11095 struct drm_framebuffer *fb,
ed8d1975 11096 struct drm_i915_gem_object *obj,
6258fbe2 11097 struct drm_i915_gem_request *req,
ed8d1975 11098 uint32_t flags)
8c9f3aaf 11099{
6258fbe2 11100 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
11101 struct drm_i915_private *dev_priv = dev->dev_private;
11102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11103 uint32_t pf, pipesrc;
11104 int ret;
11105
5fb9de1a 11106 ret = intel_ring_begin(req, 4);
8c9f3aaf 11107 if (ret)
4fa62c89 11108 return ret;
8c9f3aaf 11109
6d90c952
DV
11110 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11111 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11112 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11113 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11114
dc257cf1
DV
11115 /* Contrary to the suggestions in the documentation,
11116 * "Enable Panel Fitter" does not seem to be required when page
11117 * flipping with a non-native mode, and worse causes a normal
11118 * modeset to fail.
11119 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11120 */
11121 pf = 0;
8c9f3aaf 11122 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11123 intel_ring_emit(ring, pf | pipesrc);
e7d841ca 11124
6042639c 11125 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11126 return 0;
8c9f3aaf
JB
11127}
11128
7c9017e5
JB
11129static int intel_gen7_queue_flip(struct drm_device *dev,
11130 struct drm_crtc *crtc,
11131 struct drm_framebuffer *fb,
ed8d1975 11132 struct drm_i915_gem_object *obj,
6258fbe2 11133 struct drm_i915_gem_request *req,
ed8d1975 11134 uint32_t flags)
7c9017e5 11135{
6258fbe2 11136 struct intel_engine_cs *ring = req->ring;
7c9017e5 11137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11138 uint32_t plane_bit = 0;
ffe74d75
CW
11139 int len, ret;
11140
eba905b2 11141 switch (intel_crtc->plane) {
cb05d8de
DV
11142 case PLANE_A:
11143 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11144 break;
11145 case PLANE_B:
11146 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11147 break;
11148 case PLANE_C:
11149 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11150 break;
11151 default:
11152 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11153 return -ENODEV;
cb05d8de
DV
11154 }
11155
ffe74d75 11156 len = 4;
f476828a 11157 if (ring->id == RCS) {
ffe74d75 11158 len += 6;
f476828a
DL
11159 /*
11160 * On Gen 8, SRM is now taking an extra dword to accommodate
11161 * 48bits addresses, and we need a NOOP for the batch size to
11162 * stay even.
11163 */
11164 if (IS_GEN8(dev))
11165 len += 2;
11166 }
ffe74d75 11167
f66fab8e
VS
11168 /*
11169 * BSpec MI_DISPLAY_FLIP for IVB:
11170 * "The full packet must be contained within the same cache line."
11171 *
11172 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11173 * cacheline, if we ever start emitting more commands before
11174 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11175 * then do the cacheline alignment, and finally emit the
11176 * MI_DISPLAY_FLIP.
11177 */
bba09b12 11178 ret = intel_ring_cacheline_align(req);
f66fab8e 11179 if (ret)
4fa62c89 11180 return ret;
f66fab8e 11181
5fb9de1a 11182 ret = intel_ring_begin(req, len);
7c9017e5 11183 if (ret)
4fa62c89 11184 return ret;
7c9017e5 11185
ffe74d75
CW
11186 /* Unmask the flip-done completion message. Note that the bspec says that
11187 * we should do this for both the BCS and RCS, and that we must not unmask
11188 * more than one flip event at any time (or ensure that one flip message
11189 * can be sent by waiting for flip-done prior to queueing new flips).
11190 * Experimentation says that BCS works despite DERRMR masking all
11191 * flip-done completion events and that unmasking all planes at once
11192 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11193 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11194 */
11195 if (ring->id == RCS) {
11196 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
f92a9162 11197 intel_ring_emit_reg(ring, DERRMR);
ffe74d75
CW
11198 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11199 DERRMR_PIPEB_PRI_FLIP_DONE |
11200 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11201 if (IS_GEN8(dev))
f1afe24f 11202 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11203 MI_SRM_LRM_GLOBAL_GTT);
11204 else
f1afe24f 11205 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
f476828a 11206 MI_SRM_LRM_GLOBAL_GTT);
f92a9162 11207 intel_ring_emit_reg(ring, DERRMR);
ffe74d75 11208 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11209 if (IS_GEN8(dev)) {
11210 intel_ring_emit(ring, 0);
11211 intel_ring_emit(ring, MI_NOOP);
11212 }
ffe74d75
CW
11213 }
11214
cb05d8de 11215 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11216 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11217 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11218 intel_ring_emit(ring, (MI_NOOP));
e7d841ca 11219
6042639c 11220 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11221 return 0;
7c9017e5
JB
11222}
11223
84c33a64
SG
11224static bool use_mmio_flip(struct intel_engine_cs *ring,
11225 struct drm_i915_gem_object *obj)
11226{
11227 /*
11228 * This is not being used for older platforms, because
11229 * non-availability of flip done interrupt forces us to use
11230 * CS flips. Older platforms derive flip done using some clever
11231 * tricks involving the flip_pending status bits and vblank irqs.
11232 * So using MMIO flips there would disrupt this mechanism.
11233 */
11234
8e09bf83
CW
11235 if (ring == NULL)
11236 return true;
11237
84c33a64
SG
11238 if (INTEL_INFO(ring->dev)->gen < 5)
11239 return false;
11240
11241 if (i915.use_mmio_flip < 0)
11242 return false;
11243 else if (i915.use_mmio_flip > 0)
11244 return true;
14bf993e
OM
11245 else if (i915.enable_execlists)
11246 return true;
fd8e058a
AG
11247 else if (obj->base.dma_buf &&
11248 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11249 false))
11250 return true;
84c33a64 11251 else
b4716185 11252 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11253}
11254
6042639c 11255static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11256 unsigned int rotation,
6042639c 11257 struct intel_unpin_work *work)
ff944564
DL
11258{
11259 struct drm_device *dev = intel_crtc->base.dev;
11260 struct drm_i915_private *dev_priv = dev->dev_private;
11261 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11262 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11263 u32 ctl, stride, tile_height;
ff944564
DL
11264
11265 ctl = I915_READ(PLANE_CTL(pipe, 0));
11266 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11267 switch (fb->modifier[0]) {
11268 case DRM_FORMAT_MOD_NONE:
11269 break;
11270 case I915_FORMAT_MOD_X_TILED:
ff944564 11271 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11272 break;
11273 case I915_FORMAT_MOD_Y_TILED:
11274 ctl |= PLANE_CTL_TILED_Y;
11275 break;
11276 case I915_FORMAT_MOD_Yf_TILED:
11277 ctl |= PLANE_CTL_TILED_YF;
11278 break;
11279 default:
11280 MISSING_CASE(fb->modifier[0]);
11281 }
ff944564
DL
11282
11283 /*
11284 * The stride is either expressed as a multiple of 64 bytes chunks for
11285 * linear buffers or in number of tiles for tiled buffers.
11286 */
86efe24a
TU
11287 if (intel_rotation_90_or_270(rotation)) {
11288 /* stride = Surface height in tiles */
832be82f 11289 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
86efe24a
TU
11290 stride = DIV_ROUND_UP(fb->height, tile_height);
11291 } else {
11292 stride = fb->pitches[0] /
7b49f948
VS
11293 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11294 fb->pixel_format);
86efe24a 11295 }
ff944564
DL
11296
11297 /*
11298 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11299 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11300 */
11301 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11302 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11303
6042639c 11304 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11305 POSTING_READ(PLANE_SURF(pipe, 0));
11306}
11307
6042639c
CW
11308static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11309 struct intel_unpin_work *work)
84c33a64
SG
11310{
11311 struct drm_device *dev = intel_crtc->base.dev;
11312 struct drm_i915_private *dev_priv = dev->dev_private;
11313 struct intel_framebuffer *intel_fb =
11314 to_intel_framebuffer(intel_crtc->base.primary->fb);
11315 struct drm_i915_gem_object *obj = intel_fb->obj;
f0f59a00 11316 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
84c33a64 11317 u32 dspcntr;
84c33a64 11318
84c33a64
SG
11319 dspcntr = I915_READ(reg);
11320
c5d97472
DL
11321 if (obj->tiling_mode != I915_TILING_NONE)
11322 dspcntr |= DISPPLANE_TILED;
11323 else
11324 dspcntr &= ~DISPPLANE_TILED;
11325
84c33a64
SG
11326 I915_WRITE(reg, dspcntr);
11327
6042639c 11328 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11329 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11330}
11331
11332/*
11333 * XXX: This is the temporary way to update the plane registers until we get
11334 * around to using the usual plane update functions for MMIO flips
11335 */
6042639c 11336static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11337{
6042639c
CW
11338 struct intel_crtc *crtc = mmio_flip->crtc;
11339 struct intel_unpin_work *work;
11340
11341 spin_lock_irq(&crtc->base.dev->event_lock);
11342 work = crtc->unpin_work;
11343 spin_unlock_irq(&crtc->base.dev->event_lock);
11344 if (work == NULL)
11345 return;
ff944564 11346
6042639c 11347 intel_mark_page_flip_active(work);
ff944564 11348
6042639c 11349 intel_pipe_update_start(crtc);
ff944564 11350
6042639c 11351 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11352 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11353 else
11354 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11355 ilk_do_mmio_flip(crtc, work);
ff944564 11356
6042639c 11357 intel_pipe_update_end(crtc);
84c33a64
SG
11358}
11359
9362c7c5 11360static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11361{
b2cfe0ab
CW
11362 struct intel_mmio_flip *mmio_flip =
11363 container_of(work, struct intel_mmio_flip, work);
fd8e058a
AG
11364 struct intel_framebuffer *intel_fb =
11365 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11366 struct drm_i915_gem_object *obj = intel_fb->obj;
84c33a64 11367
6042639c 11368 if (mmio_flip->req) {
eed29a5b 11369 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11370 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11371 false, NULL,
11372 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11373 i915_gem_request_unreference__unlocked(mmio_flip->req);
11374 }
84c33a64 11375
fd8e058a
AG
11376 /* For framebuffer backed by dmabuf, wait for fence */
11377 if (obj->base.dma_buf)
11378 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11379 false, false,
11380 MAX_SCHEDULE_TIMEOUT) < 0);
11381
6042639c 11382 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11383 kfree(mmio_flip);
84c33a64
SG
11384}
11385
11386static int intel_queue_mmio_flip(struct drm_device *dev,
11387 struct drm_crtc *crtc,
86efe24a 11388 struct drm_i915_gem_object *obj)
84c33a64 11389{
b2cfe0ab
CW
11390 struct intel_mmio_flip *mmio_flip;
11391
11392 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11393 if (mmio_flip == NULL)
11394 return -ENOMEM;
84c33a64 11395
bcafc4e3 11396 mmio_flip->i915 = to_i915(dev);
eed29a5b 11397 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11398 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11399 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11400
b2cfe0ab
CW
11401 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11402 schedule_work(&mmio_flip->work);
84c33a64 11403
84c33a64
SG
11404 return 0;
11405}
11406
8c9f3aaf
JB
11407static int intel_default_queue_flip(struct drm_device *dev,
11408 struct drm_crtc *crtc,
11409 struct drm_framebuffer *fb,
ed8d1975 11410 struct drm_i915_gem_object *obj,
6258fbe2 11411 struct drm_i915_gem_request *req,
ed8d1975 11412 uint32_t flags)
8c9f3aaf
JB
11413{
11414 return -ENODEV;
11415}
11416
d6bbafa1
CW
11417static bool __intel_pageflip_stall_check(struct drm_device *dev,
11418 struct drm_crtc *crtc)
11419{
11420 struct drm_i915_private *dev_priv = dev->dev_private;
11421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11422 struct intel_unpin_work *work = intel_crtc->unpin_work;
11423 u32 addr;
11424
11425 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11426 return true;
11427
908565c2
CW
11428 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11429 return false;
11430
d6bbafa1
CW
11431 if (!work->enable_stall_check)
11432 return false;
11433
11434 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11435 if (work->flip_queued_req &&
11436 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11437 return false;
11438
1e3feefd 11439 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11440 }
11441
1e3feefd 11442 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11443 return false;
11444
11445 /* Potential stall - if we see that the flip has happened,
11446 * assume a missed interrupt. */
11447 if (INTEL_INFO(dev)->gen >= 4)
11448 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11449 else
11450 addr = I915_READ(DSPADDR(intel_crtc->plane));
11451
11452 /* There is a potential issue here with a false positive after a flip
11453 * to the same address. We could address this by checking for a
11454 * non-incrementing frame counter.
11455 */
11456 return addr == work->gtt_offset;
11457}
11458
11459void intel_check_page_flip(struct drm_device *dev, int pipe)
11460{
11461 struct drm_i915_private *dev_priv = dev->dev_private;
11462 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11464 struct intel_unpin_work *work;
f326038a 11465
6c51d46f 11466 WARN_ON(!in_interrupt());
d6bbafa1
CW
11467
11468 if (crtc == NULL)
11469 return;
11470
f326038a 11471 spin_lock(&dev->event_lock);
6ad790c0
CW
11472 work = intel_crtc->unpin_work;
11473 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11474 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11475 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11476 page_flip_completed(intel_crtc);
6ad790c0 11477 work = NULL;
d6bbafa1 11478 }
6ad790c0
CW
11479 if (work != NULL &&
11480 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11481 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11482 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11483}
11484
6b95a207
KH
11485static int intel_crtc_page_flip(struct drm_crtc *crtc,
11486 struct drm_framebuffer *fb,
ed8d1975
KP
11487 struct drm_pending_vblank_event *event,
11488 uint32_t page_flip_flags)
6b95a207
KH
11489{
11490 struct drm_device *dev = crtc->dev;
11491 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11492 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11493 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11495 struct drm_plane *primary = crtc->primary;
a071fa00 11496 enum pipe pipe = intel_crtc->pipe;
6b95a207 11497 struct intel_unpin_work *work;
a4872ba6 11498 struct intel_engine_cs *ring;
cf5d8a46 11499 bool mmio_flip;
91af127f 11500 struct drm_i915_gem_request *request = NULL;
52e68630 11501 int ret;
6b95a207 11502
2ff8fde1
MR
11503 /*
11504 * drm_mode_page_flip_ioctl() should already catch this, but double
11505 * check to be safe. In the future we may enable pageflipping from
11506 * a disabled primary plane.
11507 */
11508 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11509 return -EBUSY;
11510
e6a595d2 11511 /* Can't change pixel format via MI display flips. */
f4510a27 11512 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11513 return -EINVAL;
11514
11515 /*
11516 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11517 * Note that pitch changes could also affect these register.
11518 */
11519 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11520 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11521 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11522 return -EINVAL;
11523
f900db47
CW
11524 if (i915_terminally_wedged(&dev_priv->gpu_error))
11525 goto out_hang;
11526
b14c5679 11527 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11528 if (work == NULL)
11529 return -ENOMEM;
11530
6b95a207 11531 work->event = event;
b4a98e57 11532 work->crtc = crtc;
ab8d6675 11533 work->old_fb = old_fb;
6b95a207
KH
11534 INIT_WORK(&work->work, intel_unpin_work_fn);
11535
87b6b101 11536 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11537 if (ret)
11538 goto free_work;
11539
6b95a207 11540 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11541 spin_lock_irq(&dev->event_lock);
6b95a207 11542 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11543 /* Before declaring the flip queue wedged, check if
11544 * the hardware completed the operation behind our backs.
11545 */
11546 if (__intel_pageflip_stall_check(dev, crtc)) {
11547 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11548 page_flip_completed(intel_crtc);
11549 } else {
11550 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11551 spin_unlock_irq(&dev->event_lock);
468f0b44 11552
d6bbafa1
CW
11553 drm_crtc_vblank_put(crtc);
11554 kfree(work);
11555 return -EBUSY;
11556 }
6b95a207
KH
11557 }
11558 intel_crtc->unpin_work = work;
5e2d7afc 11559 spin_unlock_irq(&dev->event_lock);
6b95a207 11560
b4a98e57
CW
11561 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11562 flush_workqueue(dev_priv->wq);
11563
75dfca80 11564 /* Reference the objects for the scheduled work. */
ab8d6675 11565 drm_framebuffer_reference(work->old_fb);
05394f39 11566 drm_gem_object_reference(&obj->base);
6b95a207 11567
f4510a27 11568 crtc->primary->fb = fb;
afd65eb4 11569 update_state_fb(crtc->primary);
e8216e50 11570 intel_fbc_pre_update(intel_crtc);
1ed1f968 11571
e1f99ce6 11572 work->pending_flip_obj = obj;
e1f99ce6 11573
89ed88ba
CW
11574 ret = i915_mutex_lock_interruptible(dev);
11575 if (ret)
11576 goto cleanup;
11577
b4a98e57 11578 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11579 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11580
75f7f3ec 11581 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11582 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11583
666a4537 11584 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4fa62c89 11585 ring = &dev_priv->ring[BCS];
ab8d6675 11586 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11587 /* vlv: DISPLAY_FLIP fails to change tiling */
11588 ring = NULL;
48bf5b2d 11589 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11590 ring = &dev_priv->ring[BCS];
4fa62c89 11591 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11592 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11593 if (ring == NULL || ring->id != RCS)
11594 ring = &dev_priv->ring[BCS];
11595 } else {
11596 ring = &dev_priv->ring[RCS];
11597 }
11598
cf5d8a46
CW
11599 mmio_flip = use_mmio_flip(ring, obj);
11600
11601 /* When using CS flips, we want to emit semaphores between rings.
11602 * However, when using mmio flips we will create a task to do the
11603 * synchronisation, so all we want here is to pin the framebuffer
11604 * into the display plane and skip any waits.
11605 */
7580d774
ML
11606 if (!mmio_flip) {
11607 ret = i915_gem_object_sync(obj, ring, &request);
11608 if (ret)
11609 goto cleanup_pending;
11610 }
11611
3465c580 11612 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
8c9f3aaf
JB
11613 if (ret)
11614 goto cleanup_pending;
6b95a207 11615
dedf278c
TU
11616 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11617 obj, 0);
11618 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11619
cf5d8a46 11620 if (mmio_flip) {
86efe24a 11621 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11622 if (ret)
11623 goto cleanup_unpin;
11624
f06cc1b9
JH
11625 i915_gem_request_assign(&work->flip_queued_req,
11626 obj->last_write_req);
d6bbafa1 11627 } else {
6258fbe2 11628 if (!request) {
26827088
DG
11629 request = i915_gem_request_alloc(ring, NULL);
11630 if (IS_ERR(request)) {
11631 ret = PTR_ERR(request);
6258fbe2 11632 goto cleanup_unpin;
26827088 11633 }
6258fbe2
JH
11634 }
11635
11636 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11637 page_flip_flags);
11638 if (ret)
11639 goto cleanup_unpin;
11640
6258fbe2 11641 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11642 }
11643
91af127f 11644 if (request)
75289874 11645 i915_add_request_no_flush(request);
91af127f 11646
1e3feefd 11647 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11648 work->enable_stall_check = true;
4fa62c89 11649
ab8d6675 11650 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11651 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11652 mutex_unlock(&dev->struct_mutex);
a071fa00 11653
a9ff8714
VS
11654 intel_frontbuffer_flip_prepare(dev,
11655 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11656
e5510fac
JB
11657 trace_i915_flip_request(intel_crtc->plane, obj);
11658
6b95a207 11659 return 0;
96b099fd 11660
4fa62c89 11661cleanup_unpin:
3465c580 11662 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
8c9f3aaf 11663cleanup_pending:
0aa498d5 11664 if (!IS_ERR_OR_NULL(request))
91af127f 11665 i915_gem_request_cancel(request);
b4a98e57 11666 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11667 mutex_unlock(&dev->struct_mutex);
11668cleanup:
f4510a27 11669 crtc->primary->fb = old_fb;
afd65eb4 11670 update_state_fb(crtc->primary);
89ed88ba
CW
11671
11672 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11673 drm_framebuffer_unreference(work->old_fb);
96b099fd 11674
5e2d7afc 11675 spin_lock_irq(&dev->event_lock);
96b099fd 11676 intel_crtc->unpin_work = NULL;
5e2d7afc 11677 spin_unlock_irq(&dev->event_lock);
96b099fd 11678
87b6b101 11679 drm_crtc_vblank_put(crtc);
7317c75e 11680free_work:
96b099fd
CW
11681 kfree(work);
11682
f900db47 11683 if (ret == -EIO) {
02e0efb5
ML
11684 struct drm_atomic_state *state;
11685 struct drm_plane_state *plane_state;
11686
f900db47 11687out_hang:
02e0efb5
ML
11688 state = drm_atomic_state_alloc(dev);
11689 if (!state)
11690 return -ENOMEM;
11691 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11692
11693retry:
11694 plane_state = drm_atomic_get_plane_state(state, primary);
11695 ret = PTR_ERR_OR_ZERO(plane_state);
11696 if (!ret) {
11697 drm_atomic_set_fb_for_plane(plane_state, fb);
11698
11699 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11700 if (!ret)
11701 ret = drm_atomic_commit(state);
11702 }
11703
11704 if (ret == -EDEADLK) {
11705 drm_modeset_backoff(state->acquire_ctx);
11706 drm_atomic_state_clear(state);
11707 goto retry;
11708 }
11709
11710 if (ret)
11711 drm_atomic_state_free(state);
11712
f0d3dad3 11713 if (ret == 0 && event) {
5e2d7afc 11714 spin_lock_irq(&dev->event_lock);
a071fa00 11715 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11716 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11717 }
f900db47 11718 }
96b099fd 11719 return ret;
6b95a207
KH
11720}
11721
da20eabd
ML
11722
11723/**
11724 * intel_wm_need_update - Check whether watermarks need updating
11725 * @plane: drm plane
11726 * @state: new plane state
11727 *
11728 * Check current plane state versus the new one to determine whether
11729 * watermarks need to be recalculated.
11730 *
11731 * Returns true or false.
11732 */
11733static bool intel_wm_need_update(struct drm_plane *plane,
11734 struct drm_plane_state *state)
11735{
d21fbe87
MR
11736 struct intel_plane_state *new = to_intel_plane_state(state);
11737 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11738
11739 /* Update watermarks on tiling or size changes. */
92826fcd
ML
11740 if (new->visible != cur->visible)
11741 return true;
11742
11743 if (!cur->base.fb || !new->base.fb)
11744 return false;
11745
11746 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11747 cur->base.rotation != new->base.rotation ||
d21fbe87
MR
11748 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11749 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11750 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11751 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11752 return true;
7809e5ae 11753
2791a16c 11754 return false;
7809e5ae
MR
11755}
11756
d21fbe87
MR
11757static bool needs_scaling(struct intel_plane_state *state)
11758{
11759 int src_w = drm_rect_width(&state->src) >> 16;
11760 int src_h = drm_rect_height(&state->src) >> 16;
11761 int dst_w = drm_rect_width(&state->dst);
11762 int dst_h = drm_rect_height(&state->dst);
11763
11764 return (src_w != dst_w || src_h != dst_h);
11765}
11766
da20eabd
ML
11767int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11768 struct drm_plane_state *plane_state)
11769{
ab1d3a0e 11770 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
11771 struct drm_crtc *crtc = crtc_state->crtc;
11772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11773 struct drm_plane *plane = plane_state->plane;
11774 struct drm_device *dev = crtc->dev;
ed4a6a7c 11775 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
11776 struct intel_plane_state *old_plane_state =
11777 to_intel_plane_state(plane->state);
11778 int idx = intel_crtc->base.base.id, ret;
da20eabd
ML
11779 bool mode_changed = needs_modeset(crtc_state);
11780 bool was_crtc_enabled = crtc->state->active;
11781 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11782 bool turn_off, turn_on, visible, was_visible;
11783 struct drm_framebuffer *fb = plane_state->fb;
11784
11785 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11786 plane->type != DRM_PLANE_TYPE_CURSOR) {
11787 ret = skl_update_scaler_plane(
11788 to_intel_crtc_state(crtc_state),
11789 to_intel_plane_state(plane_state));
11790 if (ret)
11791 return ret;
11792 }
11793
da20eabd
ML
11794 was_visible = old_plane_state->visible;
11795 visible = to_intel_plane_state(plane_state)->visible;
11796
11797 if (!was_crtc_enabled && WARN_ON(was_visible))
11798 was_visible = false;
11799
35c08f43
ML
11800 /*
11801 * Visibility is calculated as if the crtc was on, but
11802 * after scaler setup everything depends on it being off
11803 * when the crtc isn't active.
11804 */
11805 if (!is_crtc_enabled)
11806 to_intel_plane_state(plane_state)->visible = visible = false;
da20eabd
ML
11807
11808 if (!was_visible && !visible)
11809 return 0;
11810
e8861675
ML
11811 if (fb != old_plane_state->base.fb)
11812 pipe_config->fb_changed = true;
11813
da20eabd
ML
11814 turn_off = was_visible && (!visible || mode_changed);
11815 turn_on = visible && (!was_visible || mode_changed);
11816
11817 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11818 plane->base.id, fb ? fb->base.id : -1);
11819
11820 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11821 plane->base.id, was_visible, visible,
11822 turn_off, turn_on, mode_changed);
11823
caed361d
VS
11824 if (turn_on) {
11825 pipe_config->update_wm_pre = true;
11826
11827 /* must disable cxsr around plane enable/disable */
11828 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11829 pipe_config->disable_cxsr = true;
11830 } else if (turn_off) {
11831 pipe_config->update_wm_post = true;
92826fcd 11832
852eb00d 11833 /* must disable cxsr around plane enable/disable */
e8861675 11834 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 11835 pipe_config->disable_cxsr = true;
852eb00d 11836 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
11837 /* FIXME bollocks */
11838 pipe_config->update_wm_pre = true;
11839 pipe_config->update_wm_post = true;
852eb00d 11840 }
da20eabd 11841
ed4a6a7c 11842 /* Pre-gen9 platforms need two-step watermark updates */
caed361d
VS
11843 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11844 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
11845 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11846
8be6ca85 11847 if (visible || was_visible)
cd202f69 11848 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 11849
da20eabd
ML
11850 switch (plane->type) {
11851 case DRM_PLANE_TYPE_PRIMARY:
fcf38d13 11852 intel_crtc->atomic.update_fbc = true;
da20eabd 11853
da20eabd
ML
11854 break;
11855 case DRM_PLANE_TYPE_CURSOR:
da20eabd
ML
11856 break;
11857 case DRM_PLANE_TYPE_OVERLAY:
d21fbe87
MR
11858 /*
11859 * WaCxSRDisabledForSpriteScaling:ivb
11860 *
11861 * cstate->update_wm was already set above, so this flag will
11862 * take effect when we commit and program watermarks.
11863 */
11864 if (IS_IVYBRIDGE(dev) &&
11865 needs_scaling(to_intel_plane_state(plane_state)) &&
e8861675
ML
11866 !needs_scaling(old_plane_state))
11867 pipe_config->disable_lp_wm = true;
d21fbe87
MR
11868
11869 break;
da20eabd
ML
11870 }
11871 return 0;
11872}
11873
6d3a1ce7
ML
11874static bool encoders_cloneable(const struct intel_encoder *a,
11875 const struct intel_encoder *b)
11876{
11877 /* masks could be asymmetric, so check both ways */
11878 return a == b || (a->cloneable & (1 << b->type) &&
11879 b->cloneable & (1 << a->type));
11880}
11881
11882static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11883 struct intel_crtc *crtc,
11884 struct intel_encoder *encoder)
11885{
11886 struct intel_encoder *source_encoder;
11887 struct drm_connector *connector;
11888 struct drm_connector_state *connector_state;
11889 int i;
11890
11891 for_each_connector_in_state(state, connector, connector_state, i) {
11892 if (connector_state->crtc != &crtc->base)
11893 continue;
11894
11895 source_encoder =
11896 to_intel_encoder(connector_state->best_encoder);
11897 if (!encoders_cloneable(encoder, source_encoder))
11898 return false;
11899 }
11900
11901 return true;
11902}
11903
11904static bool check_encoder_cloning(struct drm_atomic_state *state,
11905 struct intel_crtc *crtc)
11906{
11907 struct intel_encoder *encoder;
11908 struct drm_connector *connector;
11909 struct drm_connector_state *connector_state;
11910 int i;
11911
11912 for_each_connector_in_state(state, connector, connector_state, i) {
11913 if (connector_state->crtc != &crtc->base)
11914 continue;
11915
11916 encoder = to_intel_encoder(connector_state->best_encoder);
11917 if (!check_single_encoder_cloning(state, crtc, encoder))
11918 return false;
11919 }
11920
11921 return true;
11922}
11923
11924static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11925 struct drm_crtc_state *crtc_state)
11926{
cf5a15be 11927 struct drm_device *dev = crtc->dev;
ad421372 11928 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11930 struct intel_crtc_state *pipe_config =
11931 to_intel_crtc_state(crtc_state);
6d3a1ce7 11932 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11933 int ret;
6d3a1ce7
ML
11934 bool mode_changed = needs_modeset(crtc_state);
11935
11936 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11937 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11938 return -EINVAL;
11939 }
11940
852eb00d 11941 if (mode_changed && !crtc_state->active)
caed361d 11942 pipe_config->update_wm_post = true;
eddfcbcd 11943
ad421372
ML
11944 if (mode_changed && crtc_state->enable &&
11945 dev_priv->display.crtc_compute_clock &&
8106ddbd 11946 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
11947 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11948 pipe_config);
11949 if (ret)
11950 return ret;
11951 }
11952
e435d6e5 11953 ret = 0;
86c8bbbe 11954 if (dev_priv->display.compute_pipe_wm) {
e3bddded 11955 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
11956 if (ret) {
11957 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11958 return ret;
11959 }
11960 }
11961
11962 if (dev_priv->display.compute_intermediate_wm &&
11963 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11964 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11965 return 0;
11966
11967 /*
11968 * Calculate 'intermediate' watermarks that satisfy both the
11969 * old state and the new state. We can program these
11970 * immediately.
11971 */
11972 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11973 intel_crtc,
11974 pipe_config);
11975 if (ret) {
11976 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 11977 return ret;
ed4a6a7c 11978 }
86c8bbbe
MR
11979 }
11980
e435d6e5
ML
11981 if (INTEL_INFO(dev)->gen >= 9) {
11982 if (mode_changed)
11983 ret = skl_update_scaler_crtc(pipe_config);
11984
11985 if (!ret)
11986 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11987 pipe_config);
11988 }
11989
11990 return ret;
6d3a1ce7
ML
11991}
11992
65b38e0d 11993static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11994 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11995 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11996 .atomic_begin = intel_begin_crtc_commit,
11997 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11998 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11999};
12000
d29b2f9d
ACO
12001static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12002{
12003 struct intel_connector *connector;
12004
12005 for_each_intel_connector(dev, connector) {
12006 if (connector->base.encoder) {
12007 connector->base.state->best_encoder =
12008 connector->base.encoder;
12009 connector->base.state->crtc =
12010 connector->base.encoder->crtc;
12011 } else {
12012 connector->base.state->best_encoder = NULL;
12013 connector->base.state->crtc = NULL;
12014 }
12015 }
12016}
12017
050f7aeb 12018static void
eba905b2 12019connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12020 struct intel_crtc_state *pipe_config)
050f7aeb
DV
12021{
12022 int bpp = pipe_config->pipe_bpp;
12023
12024 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12025 connector->base.base.id,
c23cc417 12026 connector->base.name);
050f7aeb
DV
12027
12028 /* Don't use an invalid EDID bpc value */
12029 if (connector->base.display_info.bpc &&
12030 connector->base.display_info.bpc * 3 < bpp) {
12031 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12032 bpp, connector->base.display_info.bpc*3);
12033 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12034 }
12035
013dd9e0
JN
12036 /* Clamp bpp to default limit on screens without EDID 1.4 */
12037 if (connector->base.display_info.bpc == 0) {
12038 int type = connector->base.connector_type;
12039 int clamp_bpp = 24;
12040
12041 /* Fall back to 18 bpp when DP sink capability is unknown. */
12042 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12043 type == DRM_MODE_CONNECTOR_eDP)
12044 clamp_bpp = 18;
12045
12046 if (bpp > clamp_bpp) {
12047 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12048 bpp, clamp_bpp);
12049 pipe_config->pipe_bpp = clamp_bpp;
12050 }
050f7aeb
DV
12051 }
12052}
12053
4e53c2e0 12054static int
050f7aeb 12055compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12056 struct intel_crtc_state *pipe_config)
4e53c2e0 12057{
050f7aeb 12058 struct drm_device *dev = crtc->base.dev;
1486017f 12059 struct drm_atomic_state *state;
da3ced29
ACO
12060 struct drm_connector *connector;
12061 struct drm_connector_state *connector_state;
1486017f 12062 int bpp, i;
4e53c2e0 12063
666a4537 12064 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
4e53c2e0 12065 bpp = 10*3;
d328c9d7
DV
12066 else if (INTEL_INFO(dev)->gen >= 5)
12067 bpp = 12*3;
12068 else
12069 bpp = 8*3;
12070
4e53c2e0 12071
4e53c2e0
DV
12072 pipe_config->pipe_bpp = bpp;
12073
1486017f
ACO
12074 state = pipe_config->base.state;
12075
4e53c2e0 12076 /* Clamp display bpp to EDID value */
da3ced29
ACO
12077 for_each_connector_in_state(state, connector, connector_state, i) {
12078 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
12079 continue;
12080
da3ced29
ACO
12081 connected_sink_compute_bpp(to_intel_connector(connector),
12082 pipe_config);
4e53c2e0
DV
12083 }
12084
12085 return bpp;
12086}
12087
644db711
DV
12088static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12089{
12090 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12091 "type: 0x%x flags: 0x%x\n",
1342830c 12092 mode->crtc_clock,
644db711
DV
12093 mode->crtc_hdisplay, mode->crtc_hsync_start,
12094 mode->crtc_hsync_end, mode->crtc_htotal,
12095 mode->crtc_vdisplay, mode->crtc_vsync_start,
12096 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12097}
12098
c0b03411 12099static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12100 struct intel_crtc_state *pipe_config,
c0b03411
DV
12101 const char *context)
12102{
6a60cd87
CK
12103 struct drm_device *dev = crtc->base.dev;
12104 struct drm_plane *plane;
12105 struct intel_plane *intel_plane;
12106 struct intel_plane_state *state;
12107 struct drm_framebuffer *fb;
12108
12109 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12110 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
12111
12112 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12113 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12114 pipe_config->pipe_bpp, pipe_config->dither);
12115 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12116 pipe_config->has_pch_encoder,
12117 pipe_config->fdi_lanes,
12118 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12119 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12120 pipe_config->fdi_m_n.tu);
90a6b7b0 12121 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12122 pipe_config->has_dp_encoder,
90a6b7b0 12123 pipe_config->lane_count,
eb14cb74
VS
12124 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12125 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12126 pipe_config->dp_m_n.tu);
b95af8be 12127
90a6b7b0 12128 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12129 pipe_config->has_dp_encoder,
90a6b7b0 12130 pipe_config->lane_count,
b95af8be
VK
12131 pipe_config->dp_m2_n2.gmch_m,
12132 pipe_config->dp_m2_n2.gmch_n,
12133 pipe_config->dp_m2_n2.link_m,
12134 pipe_config->dp_m2_n2.link_n,
12135 pipe_config->dp_m2_n2.tu);
12136
55072d19
DV
12137 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12138 pipe_config->has_audio,
12139 pipe_config->has_infoframe);
12140
c0b03411 12141 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12142 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12143 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12144 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12145 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12146 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12147 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12148 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12149 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12150 crtc->num_scalers,
12151 pipe_config->scaler_state.scaler_users,
12152 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12153 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12154 pipe_config->gmch_pfit.control,
12155 pipe_config->gmch_pfit.pgm_ratios,
12156 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12157 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12158 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12159 pipe_config->pch_pfit.size,
12160 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12161 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12162 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12163
415ff0f6 12164 if (IS_BROXTON(dev)) {
05712c15 12165 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12166 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12167 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12168 pipe_config->ddi_pll_sel,
12169 pipe_config->dpll_hw_state.ebb0,
05712c15 12170 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12171 pipe_config->dpll_hw_state.pll0,
12172 pipe_config->dpll_hw_state.pll1,
12173 pipe_config->dpll_hw_state.pll2,
12174 pipe_config->dpll_hw_state.pll3,
12175 pipe_config->dpll_hw_state.pll6,
12176 pipe_config->dpll_hw_state.pll8,
05712c15 12177 pipe_config->dpll_hw_state.pll9,
c8453338 12178 pipe_config->dpll_hw_state.pll10,
415ff0f6 12179 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12180 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12181 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12182 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12183 pipe_config->ddi_pll_sel,
12184 pipe_config->dpll_hw_state.ctrl1,
12185 pipe_config->dpll_hw_state.cfgcr1,
12186 pipe_config->dpll_hw_state.cfgcr2);
12187 } else if (HAS_DDI(dev)) {
1260f07e 12188 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
415ff0f6 12189 pipe_config->ddi_pll_sel,
00490c22
ML
12190 pipe_config->dpll_hw_state.wrpll,
12191 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12192 } else {
12193 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12194 "fp0: 0x%x, fp1: 0x%x\n",
12195 pipe_config->dpll_hw_state.dpll,
12196 pipe_config->dpll_hw_state.dpll_md,
12197 pipe_config->dpll_hw_state.fp0,
12198 pipe_config->dpll_hw_state.fp1);
12199 }
12200
6a60cd87
CK
12201 DRM_DEBUG_KMS("planes on this crtc\n");
12202 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12203 intel_plane = to_intel_plane(plane);
12204 if (intel_plane->pipe != crtc->pipe)
12205 continue;
12206
12207 state = to_intel_plane_state(plane->state);
12208 fb = state->base.fb;
12209 if (!fb) {
12210 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12211 "disabled, scaler_id = %d\n",
12212 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12213 plane->base.id, intel_plane->pipe,
12214 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12215 drm_plane_index(plane), state->scaler_id);
12216 continue;
12217 }
12218
12219 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12220 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12221 plane->base.id, intel_plane->pipe,
12222 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12223 drm_plane_index(plane));
12224 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12225 fb->base.id, fb->width, fb->height, fb->pixel_format);
12226 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12227 state->scaler_id,
12228 state->src.x1 >> 16, state->src.y1 >> 16,
12229 drm_rect_width(&state->src) >> 16,
12230 drm_rect_height(&state->src) >> 16,
12231 state->dst.x1, state->dst.y1,
12232 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12233 }
c0b03411
DV
12234}
12235
5448a00d 12236static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12237{
5448a00d 12238 struct drm_device *dev = state->dev;
da3ced29 12239 struct drm_connector *connector;
00f0b378
VS
12240 unsigned int used_ports = 0;
12241
12242 /*
12243 * Walk the connector list instead of the encoder
12244 * list to detect the problem on ddi platforms
12245 * where there's just one encoder per digital port.
12246 */
0bff4858
VS
12247 drm_for_each_connector(connector, dev) {
12248 struct drm_connector_state *connector_state;
12249 struct intel_encoder *encoder;
12250
12251 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12252 if (!connector_state)
12253 connector_state = connector->state;
12254
5448a00d 12255 if (!connector_state->best_encoder)
00f0b378
VS
12256 continue;
12257
5448a00d
ACO
12258 encoder = to_intel_encoder(connector_state->best_encoder);
12259
12260 WARN_ON(!connector_state->crtc);
00f0b378
VS
12261
12262 switch (encoder->type) {
12263 unsigned int port_mask;
12264 case INTEL_OUTPUT_UNKNOWN:
12265 if (WARN_ON(!HAS_DDI(dev)))
12266 break;
12267 case INTEL_OUTPUT_DISPLAYPORT:
12268 case INTEL_OUTPUT_HDMI:
12269 case INTEL_OUTPUT_EDP:
12270 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12271
12272 /* the same port mustn't appear more than once */
12273 if (used_ports & port_mask)
12274 return false;
12275
12276 used_ports |= port_mask;
12277 default:
12278 break;
12279 }
12280 }
12281
12282 return true;
12283}
12284
83a57153
ACO
12285static void
12286clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12287{
12288 struct drm_crtc_state tmp_state;
663a3640 12289 struct intel_crtc_scaler_state scaler_state;
4978cc93 12290 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12291 struct intel_shared_dpll *shared_dpll;
8504c74c 12292 uint32_t ddi_pll_sel;
c4e2d043 12293 bool force_thru;
83a57153 12294
7546a384
ACO
12295 /* FIXME: before the switch to atomic started, a new pipe_config was
12296 * kzalloc'd. Code that depends on any field being zero should be
12297 * fixed, so that the crtc_state can be safely duplicated. For now,
12298 * only fields that are know to not cause problems are preserved. */
12299
83a57153 12300 tmp_state = crtc_state->base;
663a3640 12301 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12302 shared_dpll = crtc_state->shared_dpll;
12303 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12304 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12305 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12306
83a57153 12307 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12308
83a57153 12309 crtc_state->base = tmp_state;
663a3640 12310 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12311 crtc_state->shared_dpll = shared_dpll;
12312 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12313 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12314 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12315}
12316
548ee15b 12317static int
b8cecdf5 12318intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12319 struct intel_crtc_state *pipe_config)
ee7b9f93 12320{
b359283a 12321 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12322 struct intel_encoder *encoder;
da3ced29 12323 struct drm_connector *connector;
0b901879 12324 struct drm_connector_state *connector_state;
d328c9d7 12325 int base_bpp, ret = -EINVAL;
0b901879 12326 int i;
e29c22c0 12327 bool retry = true;
ee7b9f93 12328
83a57153 12329 clear_intel_crtc_state(pipe_config);
7758a113 12330
e143a21c
DV
12331 pipe_config->cpu_transcoder =
12332 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12333
2960bc9c
ID
12334 /*
12335 * Sanitize sync polarity flags based on requested ones. If neither
12336 * positive or negative polarity is requested, treat this as meaning
12337 * negative polarity.
12338 */
2d112de7 12339 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12340 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12341 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12342
2d112de7 12343 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12344 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12345 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12346
d328c9d7
DV
12347 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12348 pipe_config);
12349 if (base_bpp < 0)
4e53c2e0
DV
12350 goto fail;
12351
e41a56be
VS
12352 /*
12353 * Determine the real pipe dimensions. Note that stereo modes can
12354 * increase the actual pipe size due to the frame doubling and
12355 * insertion of additional space for blanks between the frame. This
12356 * is stored in the crtc timings. We use the requested mode to do this
12357 * computation to clearly distinguish it from the adjusted mode, which
12358 * can be changed by the connectors in the below retry loop.
12359 */
2d112de7 12360 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12361 &pipe_config->pipe_src_w,
12362 &pipe_config->pipe_src_h);
e41a56be 12363
e29c22c0 12364encoder_retry:
ef1b460d 12365 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12366 pipe_config->port_clock = 0;
ef1b460d 12367 pipe_config->pixel_multiplier = 1;
ff9a6750 12368
135c81b8 12369 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12370 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12371 CRTC_STEREO_DOUBLE);
135c81b8 12372
7758a113
DV
12373 /* Pass our mode to the connectors and the CRTC to give them a chance to
12374 * adjust it according to limitations or connector properties, and also
12375 * a chance to reject the mode entirely.
47f1c6c9 12376 */
da3ced29 12377 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12378 if (connector_state->crtc != crtc)
7758a113 12379 continue;
7ae89233 12380
0b901879
ACO
12381 encoder = to_intel_encoder(connector_state->best_encoder);
12382
efea6e8e
DV
12383 if (!(encoder->compute_config(encoder, pipe_config))) {
12384 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12385 goto fail;
12386 }
ee7b9f93 12387 }
47f1c6c9 12388
ff9a6750
DV
12389 /* Set default port clock if not overwritten by the encoder. Needs to be
12390 * done afterwards in case the encoder adjusts the mode. */
12391 if (!pipe_config->port_clock)
2d112de7 12392 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12393 * pipe_config->pixel_multiplier;
ff9a6750 12394
a43f6e0f 12395 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12396 if (ret < 0) {
7758a113
DV
12397 DRM_DEBUG_KMS("CRTC fixup failed\n");
12398 goto fail;
ee7b9f93 12399 }
e29c22c0
DV
12400
12401 if (ret == RETRY) {
12402 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12403 ret = -EINVAL;
12404 goto fail;
12405 }
12406
12407 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12408 retry = false;
12409 goto encoder_retry;
12410 }
12411
e8fa4270
DV
12412 /* Dithering seems to not pass-through bits correctly when it should, so
12413 * only enable it on 6bpc panels. */
12414 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12415 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12416 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12417
7758a113 12418fail:
548ee15b 12419 return ret;
ee7b9f93 12420}
47f1c6c9 12421
ea9d758d 12422static void
4740b0f2 12423intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12424{
0a9ab303
ACO
12425 struct drm_crtc *crtc;
12426 struct drm_crtc_state *crtc_state;
8a75d157 12427 int i;
ea9d758d 12428
7668851f 12429 /* Double check state. */
8a75d157 12430 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12431 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12432
12433 /* Update hwmode for vblank functions */
12434 if (crtc->state->active)
12435 crtc->hwmode = crtc->state->adjusted_mode;
12436 else
12437 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12438
12439 /*
12440 * Update legacy state to satisfy fbc code. This can
12441 * be removed when fbc uses the atomic state.
12442 */
12443 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12444 struct drm_plane_state *plane_state = crtc->primary->state;
12445
12446 crtc->primary->fb = plane_state->fb;
12447 crtc->x = plane_state->src_x >> 16;
12448 crtc->y = plane_state->src_y >> 16;
12449 }
ea9d758d 12450 }
ea9d758d
DV
12451}
12452
3bd26263 12453static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12454{
3bd26263 12455 int diff;
f1f644dc
JB
12456
12457 if (clock1 == clock2)
12458 return true;
12459
12460 if (!clock1 || !clock2)
12461 return false;
12462
12463 diff = abs(clock1 - clock2);
12464
12465 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12466 return true;
12467
12468 return false;
12469}
12470
25c5b266
DV
12471#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12472 list_for_each_entry((intel_crtc), \
12473 &(dev)->mode_config.crtc_list, \
12474 base.head) \
95150bdf 12475 for_each_if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12476
cfb23ed6
ML
12477static bool
12478intel_compare_m_n(unsigned int m, unsigned int n,
12479 unsigned int m2, unsigned int n2,
12480 bool exact)
12481{
12482 if (m == m2 && n == n2)
12483 return true;
12484
12485 if (exact || !m || !n || !m2 || !n2)
12486 return false;
12487
12488 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12489
31d10b57
ML
12490 if (n > n2) {
12491 while (n > n2) {
cfb23ed6
ML
12492 m2 <<= 1;
12493 n2 <<= 1;
12494 }
31d10b57
ML
12495 } else if (n < n2) {
12496 while (n < n2) {
cfb23ed6
ML
12497 m <<= 1;
12498 n <<= 1;
12499 }
12500 }
12501
31d10b57
ML
12502 if (n != n2)
12503 return false;
12504
12505 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
12506}
12507
12508static bool
12509intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12510 struct intel_link_m_n *m2_n2,
12511 bool adjust)
12512{
12513 if (m_n->tu == m2_n2->tu &&
12514 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12515 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12516 intel_compare_m_n(m_n->link_m, m_n->link_n,
12517 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12518 if (adjust)
12519 *m2_n2 = *m_n;
12520
12521 return true;
12522 }
12523
12524 return false;
12525}
12526
0e8ffe1b 12527static bool
2fa2fe9a 12528intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12529 struct intel_crtc_state *current_config,
cfb23ed6
ML
12530 struct intel_crtc_state *pipe_config,
12531 bool adjust)
0e8ffe1b 12532{
cfb23ed6
ML
12533 bool ret = true;
12534
12535#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12536 do { \
12537 if (!adjust) \
12538 DRM_ERROR(fmt, ##__VA_ARGS__); \
12539 else \
12540 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12541 } while (0)
12542
66e985c0
DV
12543#define PIPE_CONF_CHECK_X(name) \
12544 if (current_config->name != pipe_config->name) { \
cfb23ed6 12545 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12546 "(expected 0x%08x, found 0x%08x)\n", \
12547 current_config->name, \
12548 pipe_config->name); \
cfb23ed6 12549 ret = false; \
66e985c0
DV
12550 }
12551
08a24034
DV
12552#define PIPE_CONF_CHECK_I(name) \
12553 if (current_config->name != pipe_config->name) { \
cfb23ed6 12554 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12555 "(expected %i, found %i)\n", \
12556 current_config->name, \
12557 pipe_config->name); \
cfb23ed6
ML
12558 ret = false; \
12559 }
12560
8106ddbd
ACO
12561#define PIPE_CONF_CHECK_P(name) \
12562 if (current_config->name != pipe_config->name) { \
12563 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12564 "(expected %p, found %p)\n", \
12565 current_config->name, \
12566 pipe_config->name); \
12567 ret = false; \
12568 }
12569
cfb23ed6
ML
12570#define PIPE_CONF_CHECK_M_N(name) \
12571 if (!intel_compare_link_m_n(&current_config->name, \
12572 &pipe_config->name,\
12573 adjust)) { \
12574 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12575 "(expected tu %i gmch %i/%i link %i/%i, " \
12576 "found tu %i, gmch %i/%i link %i/%i)\n", \
12577 current_config->name.tu, \
12578 current_config->name.gmch_m, \
12579 current_config->name.gmch_n, \
12580 current_config->name.link_m, \
12581 current_config->name.link_n, \
12582 pipe_config->name.tu, \
12583 pipe_config->name.gmch_m, \
12584 pipe_config->name.gmch_n, \
12585 pipe_config->name.link_m, \
12586 pipe_config->name.link_n); \
12587 ret = false; \
12588 }
12589
12590#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12591 if (!intel_compare_link_m_n(&current_config->name, \
12592 &pipe_config->name, adjust) && \
12593 !intel_compare_link_m_n(&current_config->alt_name, \
12594 &pipe_config->name, adjust)) { \
12595 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12596 "(expected tu %i gmch %i/%i link %i/%i, " \
12597 "or tu %i gmch %i/%i link %i/%i, " \
12598 "found tu %i, gmch %i/%i link %i/%i)\n", \
12599 current_config->name.tu, \
12600 current_config->name.gmch_m, \
12601 current_config->name.gmch_n, \
12602 current_config->name.link_m, \
12603 current_config->name.link_n, \
12604 current_config->alt_name.tu, \
12605 current_config->alt_name.gmch_m, \
12606 current_config->alt_name.gmch_n, \
12607 current_config->alt_name.link_m, \
12608 current_config->alt_name.link_n, \
12609 pipe_config->name.tu, \
12610 pipe_config->name.gmch_m, \
12611 pipe_config->name.gmch_n, \
12612 pipe_config->name.link_m, \
12613 pipe_config->name.link_n); \
12614 ret = false; \
88adfff1
DV
12615 }
12616
b95af8be
VK
12617/* This is required for BDW+ where there is only one set of registers for
12618 * switching between high and low RR.
12619 * This macro can be used whenever a comparison has to be made between one
12620 * hw state and multiple sw state variables.
12621 */
12622#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12623 if ((current_config->name != pipe_config->name) && \
12624 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12625 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12626 "(expected %i or %i, found %i)\n", \
12627 current_config->name, \
12628 current_config->alt_name, \
12629 pipe_config->name); \
cfb23ed6 12630 ret = false; \
b95af8be
VK
12631 }
12632
1bd1bd80
DV
12633#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12634 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12635 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12636 "(expected %i, found %i)\n", \
12637 current_config->name & (mask), \
12638 pipe_config->name & (mask)); \
cfb23ed6 12639 ret = false; \
1bd1bd80
DV
12640 }
12641
5e550656
VS
12642#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12643 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12644 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12645 "(expected %i, found %i)\n", \
12646 current_config->name, \
12647 pipe_config->name); \
cfb23ed6 12648 ret = false; \
5e550656
VS
12649 }
12650
bb760063
DV
12651#define PIPE_CONF_QUIRK(quirk) \
12652 ((current_config->quirks | pipe_config->quirks) & (quirk))
12653
eccb140b
DV
12654 PIPE_CONF_CHECK_I(cpu_transcoder);
12655
08a24034
DV
12656 PIPE_CONF_CHECK_I(has_pch_encoder);
12657 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12658 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12659
eb14cb74 12660 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12661 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12662
12663 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12664 PIPE_CONF_CHECK_M_N(dp_m_n);
12665
cfb23ed6
ML
12666 if (current_config->has_drrs)
12667 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12668 } else
12669 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12670
a65347ba
JN
12671 PIPE_CONF_CHECK_I(has_dsi_encoder);
12672
2d112de7
ACO
12673 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12674 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12675 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12676 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12677 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12678 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12679
2d112de7
ACO
12680 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12681 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12682 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12683 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12684 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12685 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12686
c93f54cf 12687 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12688 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09 12689 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
666a4537 12690 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
b5a9fa09 12691 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12692 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12693
9ed109a7
DV
12694 PIPE_CONF_CHECK_I(has_audio);
12695
2d112de7 12696 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12697 DRM_MODE_FLAG_INTERLACE);
12698
bb760063 12699 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12700 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12701 DRM_MODE_FLAG_PHSYNC);
2d112de7 12702 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12703 DRM_MODE_FLAG_NHSYNC);
2d112de7 12704 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12705 DRM_MODE_FLAG_PVSYNC);
2d112de7 12706 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12707 DRM_MODE_FLAG_NVSYNC);
12708 }
045ac3b5 12709
333b8ca8 12710 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12711 /* pfit ratios are autocomputed by the hw on gen4+ */
12712 if (INTEL_INFO(dev)->gen < 4)
12713 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12714 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12715
bfd16b2a
ML
12716 if (!adjust) {
12717 PIPE_CONF_CHECK_I(pipe_src_w);
12718 PIPE_CONF_CHECK_I(pipe_src_h);
12719
12720 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12721 if (current_config->pch_pfit.enabled) {
12722 PIPE_CONF_CHECK_X(pch_pfit.pos);
12723 PIPE_CONF_CHECK_X(pch_pfit.size);
12724 }
2fa2fe9a 12725
7aefe2b5
ML
12726 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12727 }
a1b2278e 12728
e59150dc
JB
12729 /* BDW+ don't expose a synchronous way to read the state */
12730 if (IS_HASWELL(dev))
12731 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12732
282740f7
VS
12733 PIPE_CONF_CHECK_I(double_wide);
12734
26804afd
DV
12735 PIPE_CONF_CHECK_X(ddi_pll_sel);
12736
8106ddbd 12737 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 12738 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12739 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12740 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12741 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12742 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 12743 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
12744 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12745 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12746 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12747
42571aef
VS
12748 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12749 PIPE_CONF_CHECK_I(pipe_bpp);
12750
2d112de7 12751 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12752 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12753
66e985c0 12754#undef PIPE_CONF_CHECK_X
08a24034 12755#undef PIPE_CONF_CHECK_I
8106ddbd 12756#undef PIPE_CONF_CHECK_P
b95af8be 12757#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12758#undef PIPE_CONF_CHECK_FLAGS
5e550656 12759#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12760#undef PIPE_CONF_QUIRK
cfb23ed6 12761#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12762
cfb23ed6 12763 return ret;
0e8ffe1b
DV
12764}
12765
e3b247da
VS
12766static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12767 const struct intel_crtc_state *pipe_config)
12768{
12769 if (pipe_config->has_pch_encoder) {
21a727b3 12770 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
12771 &pipe_config->fdi_m_n);
12772 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12773
12774 /*
12775 * FDI already provided one idea for the dotclock.
12776 * Yell if the encoder disagrees.
12777 */
12778 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12779 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12780 fdi_dotclock, dotclock);
12781 }
12782}
12783
08db6652
DL
12784static void check_wm_state(struct drm_device *dev)
12785{
12786 struct drm_i915_private *dev_priv = dev->dev_private;
12787 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12788 struct intel_crtc *intel_crtc;
12789 int plane;
12790
12791 if (INTEL_INFO(dev)->gen < 9)
12792 return;
12793
12794 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12795 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12796
12797 for_each_intel_crtc(dev, intel_crtc) {
12798 struct skl_ddb_entry *hw_entry, *sw_entry;
12799 const enum pipe pipe = intel_crtc->pipe;
12800
12801 if (!intel_crtc->active)
12802 continue;
12803
12804 /* planes */
dd740780 12805 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12806 hw_entry = &hw_ddb.plane[pipe][plane];
12807 sw_entry = &sw_ddb->plane[pipe][plane];
12808
12809 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12810 continue;
12811
12812 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12813 "(expected (%u,%u), found (%u,%u))\n",
12814 pipe_name(pipe), plane + 1,
12815 sw_entry->start, sw_entry->end,
12816 hw_entry->start, hw_entry->end);
12817 }
12818
12819 /* cursor */
4969d33e
MR
12820 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12821 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12822
12823 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12824 continue;
12825
12826 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12827 "(expected (%u,%u), found (%u,%u))\n",
12828 pipe_name(pipe),
12829 sw_entry->start, sw_entry->end,
12830 hw_entry->start, hw_entry->end);
12831 }
12832}
12833
91d1b4bd 12834static void
35dd3c64
ML
12835check_connector_state(struct drm_device *dev,
12836 struct drm_atomic_state *old_state)
8af6cf88 12837{
35dd3c64
ML
12838 struct drm_connector_state *old_conn_state;
12839 struct drm_connector *connector;
12840 int i;
8af6cf88 12841
35dd3c64
ML
12842 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12843 struct drm_encoder *encoder = connector->encoder;
12844 struct drm_connector_state *state = connector->state;
ad3c558f 12845
8af6cf88
DV
12846 /* This also checks the encoder/connector hw state with the
12847 * ->get_hw_state callbacks. */
35dd3c64 12848 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12849
ad3c558f 12850 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12851 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12852 }
91d1b4bd
DV
12853}
12854
12855static void
12856check_encoder_state(struct drm_device *dev)
12857{
12858 struct intel_encoder *encoder;
12859 struct intel_connector *connector;
8af6cf88 12860
b2784e15 12861 for_each_intel_encoder(dev, encoder) {
8af6cf88 12862 bool enabled = false;
4d20cd86 12863 enum pipe pipe;
8af6cf88
DV
12864
12865 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12866 encoder->base.base.id,
8e329a03 12867 encoder->base.name);
8af6cf88 12868
3a3371ff 12869 for_each_intel_connector(dev, connector) {
4d20cd86 12870 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12871 continue;
12872 enabled = true;
ad3c558f
ML
12873
12874 I915_STATE_WARN(connector->base.state->crtc !=
12875 encoder->base.crtc,
12876 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12877 }
0e32b39c 12878
e2c719b7 12879 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12880 "encoder's enabled state mismatch "
12881 "(expected %i, found %i)\n",
12882 !!encoder->base.crtc, enabled);
7c60d198
ML
12883
12884 if (!encoder->base.crtc) {
4d20cd86 12885 bool active;
7c60d198 12886
4d20cd86
ML
12887 active = encoder->get_hw_state(encoder, &pipe);
12888 I915_STATE_WARN(active,
12889 "encoder detached but still enabled on pipe %c.\n",
12890 pipe_name(pipe));
7c60d198 12891 }
8af6cf88 12892 }
91d1b4bd
DV
12893}
12894
12895static void
4d20cd86 12896check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12897{
fbee40df 12898 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12899 struct intel_encoder *encoder;
4d20cd86
ML
12900 struct drm_crtc_state *old_crtc_state;
12901 struct drm_crtc *crtc;
12902 int i;
8af6cf88 12903
4d20cd86
ML
12904 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12906 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12907 bool active;
8af6cf88 12908
bfd16b2a
ML
12909 if (!needs_modeset(crtc->state) &&
12910 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12911 continue;
045ac3b5 12912
4d20cd86
ML
12913 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12914 pipe_config = to_intel_crtc_state(old_crtc_state);
12915 memset(pipe_config, 0, sizeof(*pipe_config));
12916 pipe_config->base.crtc = crtc;
12917 pipe_config->base.state = old_state;
8af6cf88 12918
4d20cd86
ML
12919 DRM_DEBUG_KMS("[CRTC:%d]\n",
12920 crtc->base.id);
8af6cf88 12921
4d20cd86
ML
12922 active = dev_priv->display.get_pipe_config(intel_crtc,
12923 pipe_config);
d62cf62a 12924
b6b5d049 12925 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12926 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12927 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12928 active = crtc->state->active;
6c49f241 12929
4d20cd86 12930 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12931 "crtc active state doesn't match with hw state "
4d20cd86 12932 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12933
4d20cd86 12934 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12935 "transitional active state does not match atomic hw state "
4d20cd86
ML
12936 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12937
12938 for_each_encoder_on_crtc(dev, crtc, encoder) {
12939 enum pipe pipe;
12940
12941 active = encoder->get_hw_state(encoder, &pipe);
12942 I915_STATE_WARN(active != crtc->state->active,
12943 "[ENCODER:%i] active %i with crtc active %i\n",
12944 encoder->base.base.id, active, crtc->state->active);
12945
12946 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12947 "Encoder connected to wrong pipe %c\n",
12948 pipe_name(pipe));
12949
12950 if (active)
12951 encoder->get_config(encoder, pipe_config);
12952 }
53d9f4e9 12953
4d20cd86 12954 if (!crtc->state->active)
cfb23ed6
ML
12955 continue;
12956
e3b247da
VS
12957 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12958
4d20cd86
ML
12959 sw_config = to_intel_crtc_state(crtc->state);
12960 if (!intel_pipe_config_compare(dev, sw_config,
12961 pipe_config, false)) {
e2c719b7 12962 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 12963 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 12964 "[hw state]");
4d20cd86 12965 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
12966 "[sw state]");
12967 }
8af6cf88
DV
12968 }
12969}
12970
91d1b4bd
DV
12971static void
12972check_shared_dpll_state(struct drm_device *dev)
12973{
fbee40df 12974 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12975 struct intel_crtc *crtc;
12976 struct intel_dpll_hw_state dpll_hw_state;
12977 int i;
5358901f
DV
12978
12979 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8106ddbd
ACO
12980 struct intel_shared_dpll *pll =
12981 intel_get_shared_dpll_by_id(dev_priv, i);
5358901f
DV
12982 int enabled_crtcs = 0, active_crtcs = 0;
12983 bool active;
12984
12985 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12986
12987 DRM_DEBUG_KMS("%s\n", pll->name);
12988
2edd6443 12989 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 12990
e2c719b7 12991 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12992 "more active pll users than references: %i vs %i\n",
3e369b76 12993 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12994 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12995 "pll in active use but not on in sw tracking\n");
9d16da65
ACO
12996
12997 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12998 I915_STATE_WARN(pll->on && !pll->active,
12999 "pll in on but not on in use in sw tracking\n");
13000 I915_STATE_WARN(pll->on != active,
13001 "pll on state mismatch (expected %i, found %i)\n",
13002 pll->on, active);
13003 }
5358901f 13004
d3fcc808 13005 for_each_intel_crtc(dev, crtc) {
8106ddbd 13006 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
5358901f 13007 enabled_crtcs++;
8106ddbd 13008 if (crtc->active && crtc->config->shared_dpll == pll)
5358901f
DV
13009 active_crtcs++;
13010 }
e2c719b7 13011 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
13012 "pll active crtcs mismatch (expected %i, found %i)\n",
13013 pll->active, active_crtcs);
e2c719b7 13014 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 13015 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 13016 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 13017
e2c719b7 13018 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
13019 sizeof(dpll_hw_state)),
13020 "pll hw state mismatch\n");
5358901f 13021 }
8af6cf88
DV
13022}
13023
ee165b1a
ML
13024static void
13025intel_modeset_check_state(struct drm_device *dev,
13026 struct drm_atomic_state *old_state)
91d1b4bd 13027{
08db6652 13028 check_wm_state(dev);
35dd3c64 13029 check_connector_state(dev, old_state);
91d1b4bd 13030 check_encoder_state(dev);
4d20cd86 13031 check_crtc_state(dev, old_state);
91d1b4bd
DV
13032 check_shared_dpll_state(dev);
13033}
13034
80715b2f
VS
13035static void update_scanline_offset(struct intel_crtc *crtc)
13036{
13037 struct drm_device *dev = crtc->base.dev;
13038
13039 /*
13040 * The scanline counter increments at the leading edge of hsync.
13041 *
13042 * On most platforms it starts counting from vtotal-1 on the
13043 * first active line. That means the scanline counter value is
13044 * always one less than what we would expect. Ie. just after
13045 * start of vblank, which also occurs at start of hsync (on the
13046 * last active line), the scanline counter will read vblank_start-1.
13047 *
13048 * On gen2 the scanline counter starts counting from 1 instead
13049 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13050 * to keep the value positive), instead of adding one.
13051 *
13052 * On HSW+ the behaviour of the scanline counter depends on the output
13053 * type. For DP ports it behaves like most other platforms, but on HDMI
13054 * there's an extra 1 line difference. So we need to add two instead of
13055 * one to the value.
13056 */
13057 if (IS_GEN2(dev)) {
124abe07 13058 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13059 int vtotal;
13060
124abe07
VS
13061 vtotal = adjusted_mode->crtc_vtotal;
13062 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13063 vtotal /= 2;
13064
13065 crtc->scanline_offset = vtotal - 1;
13066 } else if (HAS_DDI(dev) &&
409ee761 13067 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13068 crtc->scanline_offset = 2;
13069 } else
13070 crtc->scanline_offset = 1;
13071}
13072
ad421372 13073static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13074{
225da59b 13075 struct drm_device *dev = state->dev;
ed6739ef 13076 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13077 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13078 struct drm_crtc *crtc;
13079 struct drm_crtc_state *crtc_state;
0a9ab303 13080 int i;
ed6739ef
ACO
13081
13082 if (!dev_priv->display.crtc_compute_clock)
ad421372 13083 return;
ed6739ef 13084
0a9ab303 13085 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13087 struct intel_shared_dpll *old_dpll =
13088 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13089
fb1a38a9 13090 if (!needs_modeset(crtc_state))
225da59b
ACO
13091 continue;
13092
8106ddbd 13093 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13094
8106ddbd 13095 if (!old_dpll)
fb1a38a9 13096 continue;
0a9ab303 13097
ad421372
ML
13098 if (!shared_dpll)
13099 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13100
8106ddbd 13101 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13102 }
ed6739ef
ACO
13103}
13104
99d736a2
ML
13105/*
13106 * This implements the workaround described in the "notes" section of the mode
13107 * set sequence documentation. When going from no pipes or single pipe to
13108 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13109 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13110 */
13111static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13112{
13113 struct drm_crtc_state *crtc_state;
13114 struct intel_crtc *intel_crtc;
13115 struct drm_crtc *crtc;
13116 struct intel_crtc_state *first_crtc_state = NULL;
13117 struct intel_crtc_state *other_crtc_state = NULL;
13118 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13119 int i;
13120
13121 /* look at all crtc's that are going to be enabled in during modeset */
13122 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13123 intel_crtc = to_intel_crtc(crtc);
13124
13125 if (!crtc_state->active || !needs_modeset(crtc_state))
13126 continue;
13127
13128 if (first_crtc_state) {
13129 other_crtc_state = to_intel_crtc_state(crtc_state);
13130 break;
13131 } else {
13132 first_crtc_state = to_intel_crtc_state(crtc_state);
13133 first_pipe = intel_crtc->pipe;
13134 }
13135 }
13136
13137 /* No workaround needed? */
13138 if (!first_crtc_state)
13139 return 0;
13140
13141 /* w/a possibly needed, check how many crtc's are already enabled. */
13142 for_each_intel_crtc(state->dev, intel_crtc) {
13143 struct intel_crtc_state *pipe_config;
13144
13145 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13146 if (IS_ERR(pipe_config))
13147 return PTR_ERR(pipe_config);
13148
13149 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13150
13151 if (!pipe_config->base.active ||
13152 needs_modeset(&pipe_config->base))
13153 continue;
13154
13155 /* 2 or more enabled crtcs means no need for w/a */
13156 if (enabled_pipe != INVALID_PIPE)
13157 return 0;
13158
13159 enabled_pipe = intel_crtc->pipe;
13160 }
13161
13162 if (enabled_pipe != INVALID_PIPE)
13163 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13164 else if (other_crtc_state)
13165 other_crtc_state->hsw_workaround_pipe = first_pipe;
13166
13167 return 0;
13168}
13169
27c329ed
ML
13170static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13171{
13172 struct drm_crtc *crtc;
13173 struct drm_crtc_state *crtc_state;
13174 int ret = 0;
13175
13176 /* add all active pipes to the state */
13177 for_each_crtc(state->dev, crtc) {
13178 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13179 if (IS_ERR(crtc_state))
13180 return PTR_ERR(crtc_state);
13181
13182 if (!crtc_state->active || needs_modeset(crtc_state))
13183 continue;
13184
13185 crtc_state->mode_changed = true;
13186
13187 ret = drm_atomic_add_affected_connectors(state, crtc);
13188 if (ret)
13189 break;
13190
13191 ret = drm_atomic_add_affected_planes(state, crtc);
13192 if (ret)
13193 break;
13194 }
13195
13196 return ret;
13197}
13198
c347a676 13199static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 13200{
565602d7
ML
13201 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13202 struct drm_i915_private *dev_priv = state->dev->dev_private;
13203 struct drm_crtc *crtc;
13204 struct drm_crtc_state *crtc_state;
13205 int ret = 0, i;
054518dd 13206
b359283a
ML
13207 if (!check_digital_port_conflicts(state)) {
13208 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13209 return -EINVAL;
13210 }
13211
565602d7
ML
13212 intel_state->modeset = true;
13213 intel_state->active_crtcs = dev_priv->active_crtcs;
13214
13215 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13216 if (crtc_state->active)
13217 intel_state->active_crtcs |= 1 << i;
13218 else
13219 intel_state->active_crtcs &= ~(1 << i);
13220 }
13221
054518dd
ACO
13222 /*
13223 * See if the config requires any additional preparation, e.g.
13224 * to adjust global state with pipes off. We need to do this
13225 * here so we can get the modeset_pipe updated config for the new
13226 * mode set on this crtc. For other crtcs we need to use the
13227 * adjusted_mode bits in the crtc directly.
13228 */
27c329ed 13229 if (dev_priv->display.modeset_calc_cdclk) {
27c329ed
ML
13230 ret = dev_priv->display.modeset_calc_cdclk(state);
13231
1a617b77 13232 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
27c329ed
ML
13233 ret = intel_modeset_all_pipes(state);
13234
13235 if (ret < 0)
054518dd 13236 return ret;
e8788cbc
ML
13237
13238 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13239 intel_state->cdclk, intel_state->dev_cdclk);
27c329ed 13240 } else
1a617b77 13241 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
054518dd 13242
ad421372 13243 intel_modeset_clear_plls(state);
054518dd 13244
565602d7 13245 if (IS_HASWELL(dev_priv))
ad421372 13246 return haswell_mode_set_planes_workaround(state);
99d736a2 13247
ad421372 13248 return 0;
c347a676
ACO
13249}
13250
aa363136
MR
13251/*
13252 * Handle calculation of various watermark data at the end of the atomic check
13253 * phase. The code here should be run after the per-crtc and per-plane 'check'
13254 * handlers to ensure that all derived state has been updated.
13255 */
13256static void calc_watermark_data(struct drm_atomic_state *state)
13257{
13258 struct drm_device *dev = state->dev;
13259 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13260 struct drm_crtc *crtc;
13261 struct drm_crtc_state *cstate;
13262 struct drm_plane *plane;
13263 struct drm_plane_state *pstate;
13264
13265 /*
13266 * Calculate watermark configuration details now that derived
13267 * plane/crtc state is all properly updated.
13268 */
13269 drm_for_each_crtc(crtc, dev) {
13270 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13271 crtc->state;
13272
13273 if (cstate->active)
13274 intel_state->wm_config.num_pipes_active++;
13275 }
13276 drm_for_each_legacy_plane(plane, dev) {
13277 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13278 plane->state;
13279
13280 if (!to_intel_plane_state(pstate)->visible)
13281 continue;
13282
13283 intel_state->wm_config.sprites_enabled = true;
13284 if (pstate->crtc_w != pstate->src_w >> 16 ||
13285 pstate->crtc_h != pstate->src_h >> 16)
13286 intel_state->wm_config.sprites_scaled = true;
13287 }
13288}
13289
74c090b1
ML
13290/**
13291 * intel_atomic_check - validate state object
13292 * @dev: drm device
13293 * @state: state to validate
13294 */
13295static int intel_atomic_check(struct drm_device *dev,
13296 struct drm_atomic_state *state)
c347a676 13297{
dd8b3bdb 13298 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 13299 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13300 struct drm_crtc *crtc;
13301 struct drm_crtc_state *crtc_state;
13302 int ret, i;
61333b60 13303 bool any_ms = false;
c347a676 13304
74c090b1 13305 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13306 if (ret)
13307 return ret;
13308
c347a676 13309 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13310 struct intel_crtc_state *pipe_config =
13311 to_intel_crtc_state(crtc_state);
1ed51de9 13312
ba8af3e5
ML
13313 memset(&to_intel_crtc(crtc)->atomic, 0,
13314 sizeof(struct intel_crtc_atomic_commit));
13315
1ed51de9
DV
13316 /* Catch I915_MODE_FLAG_INHERITED */
13317 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13318 crtc_state->mode_changed = true;
cfb23ed6 13319
61333b60
ML
13320 if (!crtc_state->enable) {
13321 if (needs_modeset(crtc_state))
13322 any_ms = true;
c347a676 13323 continue;
61333b60 13324 }
c347a676 13325
26495481 13326 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13327 continue;
13328
26495481
DV
13329 /* FIXME: For only active_changed we shouldn't need to do any
13330 * state recomputation at all. */
13331
1ed51de9
DV
13332 ret = drm_atomic_add_affected_connectors(state, crtc);
13333 if (ret)
13334 return ret;
b359283a 13335
cfb23ed6 13336 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13337 if (ret)
13338 return ret;
13339
73831236 13340 if (i915.fastboot &&
dd8b3bdb 13341 intel_pipe_config_compare(dev,
cfb23ed6 13342 to_intel_crtc_state(crtc->state),
1ed51de9 13343 pipe_config, true)) {
26495481 13344 crtc_state->mode_changed = false;
bfd16b2a 13345 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13346 }
13347
13348 if (needs_modeset(crtc_state)) {
13349 any_ms = true;
cfb23ed6
ML
13350
13351 ret = drm_atomic_add_affected_planes(state, crtc);
13352 if (ret)
13353 return ret;
13354 }
61333b60 13355
26495481
DV
13356 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13357 needs_modeset(crtc_state) ?
13358 "[modeset]" : "[fastset]");
c347a676
ACO
13359 }
13360
61333b60
ML
13361 if (any_ms) {
13362 ret = intel_modeset_checks(state);
13363
13364 if (ret)
13365 return ret;
27c329ed 13366 } else
dd8b3bdb 13367 intel_state->cdclk = dev_priv->cdclk_freq;
76305b1a 13368
dd8b3bdb 13369 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
13370 if (ret)
13371 return ret;
13372
f51be2e0 13373 intel_fbc_choose_crtc(dev_priv, state);
aa363136
MR
13374 calc_watermark_data(state);
13375
13376 return 0;
054518dd
ACO
13377}
13378
5008e874
ML
13379static int intel_atomic_prepare_commit(struct drm_device *dev,
13380 struct drm_atomic_state *state,
13381 bool async)
13382{
7580d774
ML
13383 struct drm_i915_private *dev_priv = dev->dev_private;
13384 struct drm_plane_state *plane_state;
5008e874 13385 struct drm_crtc_state *crtc_state;
7580d774 13386 struct drm_plane *plane;
5008e874
ML
13387 struct drm_crtc *crtc;
13388 int i, ret;
13389
13390 if (async) {
13391 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13392 return -EINVAL;
13393 }
13394
13395 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13396 ret = intel_crtc_wait_for_pending_flips(crtc);
13397 if (ret)
13398 return ret;
7580d774
ML
13399
13400 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13401 flush_workqueue(dev_priv->wq);
5008e874
ML
13402 }
13403
f935675f
ML
13404 ret = mutex_lock_interruptible(&dev->struct_mutex);
13405 if (ret)
13406 return ret;
13407
5008e874 13408 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13409 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13410 u32 reset_counter;
13411
13412 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13413 mutex_unlock(&dev->struct_mutex);
13414
13415 for_each_plane_in_state(state, plane, plane_state, i) {
13416 struct intel_plane_state *intel_plane_state =
13417 to_intel_plane_state(plane_state);
13418
13419 if (!intel_plane_state->wait_req)
13420 continue;
13421
13422 ret = __i915_wait_request(intel_plane_state->wait_req,
13423 reset_counter, true,
13424 NULL, NULL);
13425
13426 /* Swallow -EIO errors to allow updates during hw lockup. */
13427 if (ret == -EIO)
13428 ret = 0;
13429
13430 if (ret)
13431 break;
13432 }
13433
13434 if (!ret)
13435 return 0;
13436
13437 mutex_lock(&dev->struct_mutex);
13438 drm_atomic_helper_cleanup_planes(dev, state);
13439 }
5008e874 13440
f935675f 13441 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13442 return ret;
13443}
13444
e8861675
ML
13445static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13446 struct drm_i915_private *dev_priv,
13447 unsigned crtc_mask)
13448{
13449 unsigned last_vblank_count[I915_MAX_PIPES];
13450 enum pipe pipe;
13451 int ret;
13452
13453 if (!crtc_mask)
13454 return;
13455
13456 for_each_pipe(dev_priv, pipe) {
13457 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13458
13459 if (!((1 << pipe) & crtc_mask))
13460 continue;
13461
13462 ret = drm_crtc_vblank_get(crtc);
13463 if (WARN_ON(ret != 0)) {
13464 crtc_mask &= ~(1 << pipe);
13465 continue;
13466 }
13467
13468 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13469 }
13470
13471 for_each_pipe(dev_priv, pipe) {
13472 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13473 long lret;
13474
13475 if (!((1 << pipe) & crtc_mask))
13476 continue;
13477
13478 lret = wait_event_timeout(dev->vblank[pipe].queue,
13479 last_vblank_count[pipe] !=
13480 drm_crtc_vblank_count(crtc),
13481 msecs_to_jiffies(50));
13482
13483 WARN_ON(!lret);
13484
13485 drm_crtc_vblank_put(crtc);
13486 }
13487}
13488
13489static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13490{
13491 /* fb updated, need to unpin old fb */
13492 if (crtc_state->fb_changed)
13493 return true;
13494
13495 /* wm changes, need vblank before final wm's */
caed361d 13496 if (crtc_state->update_wm_post)
e8861675
ML
13497 return true;
13498
13499 /*
13500 * cxsr is re-enabled after vblank.
caed361d 13501 * This is already handled by crtc_state->update_wm_post,
e8861675
ML
13502 * but added for clarity.
13503 */
13504 if (crtc_state->disable_cxsr)
13505 return true;
13506
13507 return false;
13508}
13509
74c090b1
ML
13510/**
13511 * intel_atomic_commit - commit validated state object
13512 * @dev: DRM device
13513 * @state: the top-level driver state object
13514 * @async: asynchronous commit
13515 *
13516 * This function commits a top-level state object that has been validated
13517 * with drm_atomic_helper_check().
13518 *
13519 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13520 * we can only handle plane-related operations and do not yet support
13521 * asynchronous commit.
13522 *
13523 * RETURNS
13524 * Zero for success or -errno.
13525 */
13526static int intel_atomic_commit(struct drm_device *dev,
13527 struct drm_atomic_state *state,
13528 bool async)
a6778b3c 13529{
565602d7 13530 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fbee40df 13531 struct drm_i915_private *dev_priv = dev->dev_private;
29ceb0e6 13532 struct drm_crtc_state *old_crtc_state;
7580d774 13533 struct drm_crtc *crtc;
ed4a6a7c 13534 struct intel_crtc_state *intel_cstate;
565602d7
ML
13535 int ret = 0, i;
13536 bool hw_check = intel_state->modeset;
33c8df89 13537 unsigned long put_domains[I915_MAX_PIPES] = {};
e8861675 13538 unsigned crtc_vblank_mask = 0;
a6778b3c 13539
5008e874 13540 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13541 if (ret) {
13542 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13543 return ret;
7580d774 13544 }
d4afb8cc 13545
1c5e19f8 13546 drm_atomic_helper_swap_state(dev, state);
aa363136 13547 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
1c5e19f8 13548
565602d7
ML
13549 if (intel_state->modeset) {
13550 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13551 sizeof(intel_state->min_pixclk));
13552 dev_priv->active_crtcs = intel_state->active_crtcs;
1a617b77 13553 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
33c8df89
ML
13554
13555 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7
ML
13556 }
13557
29ceb0e6 13558 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
13559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13560
33c8df89
ML
13561 if (needs_modeset(crtc->state) ||
13562 to_intel_crtc_state(crtc->state)->update_pipe) {
13563 hw_check = true;
13564
13565 put_domains[to_intel_crtc(crtc)->pipe] =
13566 modeset_get_crtc_power_domains(crtc,
13567 to_intel_crtc_state(crtc->state));
13568 }
13569
61333b60
ML
13570 if (!needs_modeset(crtc->state))
13571 continue;
13572
29ceb0e6 13573 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 13574
29ceb0e6
VS
13575 if (old_crtc_state->active) {
13576 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
a539205a 13577 dev_priv->display.crtc_disable(crtc);
eddfcbcd 13578 intel_crtc->active = false;
58f9c0bc 13579 intel_fbc_disable(intel_crtc);
eddfcbcd 13580 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
13581
13582 /*
13583 * Underruns don't always raise
13584 * interrupts, so check manually.
13585 */
13586 intel_check_cpu_fifo_underruns(dev_priv);
13587 intel_check_pch_fifo_underruns(dev_priv);
b9001114
ML
13588
13589 if (!crtc->state->active)
13590 intel_update_watermarks(crtc);
a539205a 13591 }
b8cecdf5 13592 }
7758a113 13593
ea9d758d
DV
13594 /* Only after disabling all output pipelines that will be changed can we
13595 * update the the output configuration. */
4740b0f2 13596 intel_modeset_update_crtc_state(state);
f6e5b160 13597
565602d7 13598 if (intel_state->modeset) {
4740b0f2
ML
13599 intel_shared_dpll_commit(state);
13600
13601 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
13602
13603 if (dev_priv->display.modeset_commit_cdclk &&
13604 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13605 dev_priv->display.modeset_commit_cdclk(state);
4740b0f2 13606 }
47fab737 13607
a6778b3c 13608 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
29ceb0e6 13609 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a
ML
13610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13611 bool modeset = needs_modeset(crtc->state);
e8861675
ML
13612 struct intel_crtc_state *pipe_config =
13613 to_intel_crtc_state(crtc->state);
13614 bool update_pipe = !modeset && pipe_config->update_pipe;
9f836f90 13615
f6ac4b2a 13616 if (modeset && crtc->state->active) {
a539205a
ML
13617 update_scanline_offset(to_intel_crtc(crtc));
13618 dev_priv->display.crtc_enable(crtc);
13619 }
80715b2f 13620
f6ac4b2a 13621 if (!modeset)
29ceb0e6 13622 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
f6ac4b2a 13623
49227c4a
PZ
13624 if (crtc->state->active && intel_crtc->atomic.update_fbc)
13625 intel_fbc_enable(intel_crtc);
13626
6173ee28
ML
13627 if (crtc->state->active &&
13628 (crtc->state->planes_changed || update_pipe))
29ceb0e6 13629 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
bfd16b2a 13630
e8861675
ML
13631 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13632 crtc_vblank_mask |= 1 << i;
80715b2f 13633 }
a6778b3c 13634
a6778b3c 13635 /* FIXME: add subpixel order */
83a57153 13636
e8861675
ML
13637 if (!state->legacy_cursor_update)
13638 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
f935675f 13639
29ceb0e6 13640 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
cd202f69 13641 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
e8861675 13642
33c8df89
ML
13643 if (put_domains[i])
13644 modeset_put_power_domains(dev_priv, put_domains[i]);
13645 }
13646
13647 if (intel_state->modeset)
13648 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13649
ed4a6a7c
MR
13650 /*
13651 * Now that the vblank has passed, we can go ahead and program the
13652 * optimal watermarks on platforms that need two-step watermark
13653 * programming.
13654 *
13655 * TODO: Move this (and other cleanup) to an async worker eventually.
13656 */
29ceb0e6 13657 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
ed4a6a7c
MR
13658 intel_cstate = to_intel_crtc_state(crtc->state);
13659
13660 if (dev_priv->display.optimize_watermarks)
13661 dev_priv->display.optimize_watermarks(intel_cstate);
13662 }
13663
f935675f 13664 mutex_lock(&dev->struct_mutex);
d4afb8cc 13665 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13666 mutex_unlock(&dev->struct_mutex);
2bfb4627 13667
565602d7 13668 if (hw_check)
ee165b1a
ML
13669 intel_modeset_check_state(dev, state);
13670
13671 drm_atomic_state_free(state);
f30da187 13672
75714940
MK
13673 /* As one of the primary mmio accessors, KMS has a high likelihood
13674 * of triggering bugs in unclaimed access. After we finish
13675 * modesetting, see if an error has been flagged, and if so
13676 * enable debugging for the next modeset - and hope we catch
13677 * the culprit.
13678 *
13679 * XXX note that we assume display power is on at this point.
13680 * This might hold true now but we need to add pm helper to check
13681 * unclaimed only when the hardware is on, as atomic commits
13682 * can happen also when the device is completely off.
13683 */
13684 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13685
74c090b1 13686 return 0;
7f27126e
JB
13687}
13688
c0c36b94
CW
13689void intel_crtc_restore_mode(struct drm_crtc *crtc)
13690{
83a57153
ACO
13691 struct drm_device *dev = crtc->dev;
13692 struct drm_atomic_state *state;
e694eb02 13693 struct drm_crtc_state *crtc_state;
2bfb4627 13694 int ret;
83a57153
ACO
13695
13696 state = drm_atomic_state_alloc(dev);
13697 if (!state) {
e694eb02 13698 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13699 crtc->base.id);
13700 return;
13701 }
13702
e694eb02 13703 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13704
e694eb02
ML
13705retry:
13706 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13707 ret = PTR_ERR_OR_ZERO(crtc_state);
13708 if (!ret) {
13709 if (!crtc_state->active)
13710 goto out;
83a57153 13711
e694eb02 13712 crtc_state->mode_changed = true;
74c090b1 13713 ret = drm_atomic_commit(state);
83a57153
ACO
13714 }
13715
e694eb02
ML
13716 if (ret == -EDEADLK) {
13717 drm_atomic_state_clear(state);
13718 drm_modeset_backoff(state->acquire_ctx);
13719 goto retry;
4ed9fb37 13720 }
4be07317 13721
2bfb4627 13722 if (ret)
e694eb02 13723out:
2bfb4627 13724 drm_atomic_state_free(state);
c0c36b94
CW
13725}
13726
25c5b266
DV
13727#undef for_each_intel_crtc_masked
13728
f6e5b160 13729static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13730 .gamma_set = intel_crtc_gamma_set,
74c090b1 13731 .set_config = drm_atomic_helper_set_config,
f6e5b160
CW
13732 .destroy = intel_crtc_destroy,
13733 .page_flip = intel_crtc_page_flip,
1356837e
MR
13734 .atomic_duplicate_state = intel_crtc_duplicate_state,
13735 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13736};
13737
6beb8c23
MR
13738/**
13739 * intel_prepare_plane_fb - Prepare fb for usage on plane
13740 * @plane: drm plane to prepare for
13741 * @fb: framebuffer to prepare for presentation
13742 *
13743 * Prepares a framebuffer for usage on a display plane. Generally this
13744 * involves pinning the underlying object and updating the frontbuffer tracking
13745 * bits. Some older platforms need special physical address handling for
13746 * cursor planes.
13747 *
f935675f
ML
13748 * Must be called with struct_mutex held.
13749 *
6beb8c23
MR
13750 * Returns 0 on success, negative error code on failure.
13751 */
13752int
13753intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13754 const struct drm_plane_state *new_state)
465c120c
MR
13755{
13756 struct drm_device *dev = plane->dev;
844f9111 13757 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13758 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13759 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13760 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13761 int ret = 0;
465c120c 13762
1ee49399 13763 if (!obj && !old_obj)
465c120c
MR
13764 return 0;
13765
5008e874
ML
13766 if (old_obj) {
13767 struct drm_crtc_state *crtc_state =
13768 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13769
13770 /* Big Hammer, we also need to ensure that any pending
13771 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13772 * current scanout is retired before unpinning the old
13773 * framebuffer. Note that we rely on userspace rendering
13774 * into the buffer attached to the pipe they are waiting
13775 * on. If not, userspace generates a GPU hang with IPEHR
13776 * point to the MI_WAIT_FOR_EVENT.
13777 *
13778 * This should only fail upon a hung GPU, in which case we
13779 * can safely continue.
13780 */
13781 if (needs_modeset(crtc_state))
13782 ret = i915_gem_object_wait_rendering(old_obj, true);
13783
13784 /* Swallow -EIO errors to allow updates during hw lockup. */
13785 if (ret && ret != -EIO)
f935675f 13786 return ret;
5008e874
ML
13787 }
13788
3c28ff22
AG
13789 /* For framebuffer backed by dmabuf, wait for fence */
13790 if (obj && obj->base.dma_buf) {
bcf8be27
ML
13791 long lret;
13792
13793 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13794 false, true,
13795 MAX_SCHEDULE_TIMEOUT);
13796 if (lret == -ERESTARTSYS)
13797 return lret;
3c28ff22 13798
bcf8be27 13799 WARN(lret < 0, "waiting returns %li\n", lret);
3c28ff22
AG
13800 }
13801
1ee49399
ML
13802 if (!obj) {
13803 ret = 0;
13804 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13805 INTEL_INFO(dev)->cursor_needs_physical) {
13806 int align = IS_I830(dev) ? 16 * 1024 : 256;
13807 ret = i915_gem_object_attach_phys(obj, align);
13808 if (ret)
13809 DRM_DEBUG_KMS("failed to attach phys object\n");
13810 } else {
3465c580 13811 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
6beb8c23 13812 }
465c120c 13813
7580d774
ML
13814 if (ret == 0) {
13815 if (obj) {
13816 struct intel_plane_state *plane_state =
13817 to_intel_plane_state(new_state);
13818
13819 i915_gem_request_assign(&plane_state->wait_req,
13820 obj->last_write_req);
13821 }
13822
a9ff8714 13823 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13824 }
fdd508a6 13825
6beb8c23
MR
13826 return ret;
13827}
13828
38f3ce3a
MR
13829/**
13830 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13831 * @plane: drm plane to clean up for
13832 * @fb: old framebuffer that was on plane
13833 *
13834 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13835 *
13836 * Must be called with struct_mutex held.
38f3ce3a
MR
13837 */
13838void
13839intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13840 const struct drm_plane_state *old_state)
38f3ce3a
MR
13841{
13842 struct drm_device *dev = plane->dev;
1ee49399 13843 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13844 struct intel_plane_state *old_intel_state;
1ee49399
ML
13845 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13846 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13847
7580d774
ML
13848 old_intel_state = to_intel_plane_state(old_state);
13849
1ee49399 13850 if (!obj && !old_obj)
38f3ce3a
MR
13851 return;
13852
1ee49399
ML
13853 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13854 !INTEL_INFO(dev)->cursor_needs_physical))
3465c580 13855 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
1ee49399
ML
13856
13857 /* prepare_fb aborted? */
13858 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13859 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13860 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13861
13862 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
465c120c
MR
13863}
13864
6156a456
CK
13865int
13866skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13867{
13868 int max_scale;
13869 struct drm_device *dev;
13870 struct drm_i915_private *dev_priv;
13871 int crtc_clock, cdclk;
13872
bf8a0af0 13873 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
13874 return DRM_PLANE_HELPER_NO_SCALING;
13875
13876 dev = intel_crtc->base.dev;
13877 dev_priv = dev->dev_private;
13878 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13879 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13880
54bf1ce6 13881 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13882 return DRM_PLANE_HELPER_NO_SCALING;
13883
13884 /*
13885 * skl max scale is lower of:
13886 * close to 3 but not 3, -1 is for that purpose
13887 * or
13888 * cdclk/crtc_clock
13889 */
13890 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13891
13892 return max_scale;
13893}
13894
465c120c 13895static int
3c692a41 13896intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13897 struct intel_crtc_state *crtc_state,
3c692a41
GP
13898 struct intel_plane_state *state)
13899{
2b875c22
MR
13900 struct drm_crtc *crtc = state->base.crtc;
13901 struct drm_framebuffer *fb = state->base.fb;
6156a456 13902 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13903 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13904 bool can_position = false;
465c120c 13905
693bdc28
VS
13906 if (INTEL_INFO(plane->dev)->gen >= 9) {
13907 /* use scaler when colorkey is not required */
13908 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13909 min_scale = 1;
13910 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13911 }
d8106366 13912 can_position = true;
6156a456 13913 }
d8106366 13914
061e4b8d
ML
13915 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13916 &state->dst, &state->clip,
da20eabd
ML
13917 min_scale, max_scale,
13918 can_position, true,
13919 &state->visible);
14af293f
GP
13920}
13921
613d2b27
ML
13922static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13923 struct drm_crtc_state *old_crtc_state)
3c692a41 13924{
32b7eeec 13925 struct drm_device *dev = crtc->dev;
3c692a41 13926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13927 struct intel_crtc_state *old_intel_state =
13928 to_intel_crtc_state(old_crtc_state);
13929 bool modeset = needs_modeset(crtc->state);
3c692a41 13930
c34c9ee4 13931 /* Perform vblank evasion around commit operation */
62852622 13932 intel_pipe_update_start(intel_crtc);
0583236e 13933
bfd16b2a
ML
13934 if (modeset)
13935 return;
13936
13937 if (to_intel_crtc_state(crtc->state)->update_pipe)
13938 intel_update_pipe_config(intel_crtc, old_intel_state);
13939 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13940 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13941}
13942
613d2b27
ML
13943static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13944 struct drm_crtc_state *old_crtc_state)
32b7eeec 13945{
32b7eeec 13946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13947
62852622 13948 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13949}
13950
cf4c7c12 13951/**
4a3b8769
MR
13952 * intel_plane_destroy - destroy a plane
13953 * @plane: plane to destroy
cf4c7c12 13954 *
4a3b8769
MR
13955 * Common destruction function for all types of planes (primary, cursor,
13956 * sprite).
cf4c7c12 13957 */
4a3b8769 13958void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13959{
13960 struct intel_plane *intel_plane = to_intel_plane(plane);
13961 drm_plane_cleanup(plane);
13962 kfree(intel_plane);
13963}
13964
65a3fea0 13965const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13966 .update_plane = drm_atomic_helper_update_plane,
13967 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13968 .destroy = intel_plane_destroy,
c196e1d6 13969 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13970 .atomic_get_property = intel_plane_atomic_get_property,
13971 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13972 .atomic_duplicate_state = intel_plane_duplicate_state,
13973 .atomic_destroy_state = intel_plane_destroy_state,
13974
465c120c
MR
13975};
13976
13977static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13978 int pipe)
13979{
13980 struct intel_plane *primary;
8e7d688b 13981 struct intel_plane_state *state;
465c120c 13982 const uint32_t *intel_primary_formats;
45e3743a 13983 unsigned int num_formats;
465c120c
MR
13984
13985 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13986 if (primary == NULL)
13987 return NULL;
13988
8e7d688b
MR
13989 state = intel_create_plane_state(&primary->base);
13990 if (!state) {
ea2c67bb
MR
13991 kfree(primary);
13992 return NULL;
13993 }
8e7d688b 13994 primary->base.state = &state->base;
ea2c67bb 13995
465c120c
MR
13996 primary->can_scale = false;
13997 primary->max_downscale = 1;
6156a456
CK
13998 if (INTEL_INFO(dev)->gen >= 9) {
13999 primary->can_scale = true;
af99ceda 14000 state->scaler_id = -1;
6156a456 14001 }
465c120c
MR
14002 primary->pipe = pipe;
14003 primary->plane = pipe;
a9ff8714 14004 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 14005 primary->check_plane = intel_check_primary_plane;
465c120c
MR
14006 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14007 primary->plane = !pipe;
14008
6c0fd451
DL
14009 if (INTEL_INFO(dev)->gen >= 9) {
14010 intel_primary_formats = skl_primary_formats;
14011 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
14012
14013 primary->update_plane = skylake_update_primary_plane;
14014 primary->disable_plane = skylake_disable_primary_plane;
14015 } else if (HAS_PCH_SPLIT(dev)) {
14016 intel_primary_formats = i965_primary_formats;
14017 num_formats = ARRAY_SIZE(i965_primary_formats);
14018
14019 primary->update_plane = ironlake_update_primary_plane;
14020 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451 14021 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
14022 intel_primary_formats = i965_primary_formats;
14023 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
14024
14025 primary->update_plane = i9xx_update_primary_plane;
14026 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
14027 } else {
14028 intel_primary_formats = i8xx_primary_formats;
14029 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
14030
14031 primary->update_plane = i9xx_update_primary_plane;
14032 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
14033 }
14034
14035 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 14036 &intel_plane_funcs,
465c120c 14037 intel_primary_formats, num_formats,
b0b3b795 14038 DRM_PLANE_TYPE_PRIMARY, NULL);
48404c1e 14039
3b7a5119
SJ
14040 if (INTEL_INFO(dev)->gen >= 4)
14041 intel_create_rotation_property(dev, primary);
48404c1e 14042
ea2c67bb
MR
14043 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14044
465c120c
MR
14045 return &primary->base;
14046}
14047
3b7a5119
SJ
14048void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14049{
14050 if (!dev->mode_config.rotation_property) {
14051 unsigned long flags = BIT(DRM_ROTATE_0) |
14052 BIT(DRM_ROTATE_180);
14053
14054 if (INTEL_INFO(dev)->gen >= 9)
14055 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14056
14057 dev->mode_config.rotation_property =
14058 drm_mode_create_rotation_property(dev, flags);
14059 }
14060 if (dev->mode_config.rotation_property)
14061 drm_object_attach_property(&plane->base.base,
14062 dev->mode_config.rotation_property,
14063 plane->base.state->rotation);
14064}
14065
3d7d6510 14066static int
852e787c 14067intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 14068 struct intel_crtc_state *crtc_state,
852e787c 14069 struct intel_plane_state *state)
3d7d6510 14070{
061e4b8d 14071 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 14072 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 14073 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 14074 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
14075 unsigned stride;
14076 int ret;
3d7d6510 14077
061e4b8d
ML
14078 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14079 &state->dst, &state->clip,
3d7d6510
MR
14080 DRM_PLANE_HELPER_NO_SCALING,
14081 DRM_PLANE_HELPER_NO_SCALING,
852e787c 14082 true, true, &state->visible);
757f9a3e
GP
14083 if (ret)
14084 return ret;
14085
757f9a3e
GP
14086 /* if we want to turn off the cursor ignore width and height */
14087 if (!obj)
da20eabd 14088 return 0;
757f9a3e 14089
757f9a3e 14090 /* Check for which cursor types we support */
061e4b8d 14091 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
14092 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14093 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
14094 return -EINVAL;
14095 }
14096
ea2c67bb
MR
14097 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14098 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
14099 DRM_DEBUG_KMS("buffer is too small\n");
14100 return -ENOMEM;
14101 }
14102
3a656b54 14103 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 14104 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 14105 return -EINVAL;
32b7eeec
MR
14106 }
14107
b29ec92c
VS
14108 /*
14109 * There's something wrong with the cursor on CHV pipe C.
14110 * If it straddles the left edge of the screen then
14111 * moving it away from the edge or disabling it often
14112 * results in a pipe underrun, and often that can lead to
14113 * dead pipe (constant underrun reported, and it scans
14114 * out just a solid color). To recover from that, the
14115 * display power well must be turned off and on again.
14116 * Refuse the put the cursor into that compromised position.
14117 */
14118 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14119 state->visible && state->base.crtc_x < 0) {
14120 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14121 return -EINVAL;
14122 }
14123
da20eabd 14124 return 0;
852e787c 14125}
3d7d6510 14126
a8ad0d8e
ML
14127static void
14128intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 14129 struct drm_crtc *crtc)
a8ad0d8e 14130{
f2858021
ML
14131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14132
14133 intel_crtc->cursor_addr = 0;
55a08b3f 14134 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
14135}
14136
f4a2cf29 14137static void
55a08b3f
ML
14138intel_update_cursor_plane(struct drm_plane *plane,
14139 const struct intel_crtc_state *crtc_state,
14140 const struct intel_plane_state *state)
852e787c 14141{
55a08b3f
ML
14142 struct drm_crtc *crtc = crtc_state->base.crtc;
14143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb 14144 struct drm_device *dev = plane->dev;
2b875c22 14145 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 14146 uint32_t addr;
852e787c 14147
f4a2cf29 14148 if (!obj)
a912f12f 14149 addr = 0;
f4a2cf29 14150 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14151 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14152 else
a912f12f 14153 addr = obj->phys_handle->busaddr;
852e787c 14154
a912f12f 14155 intel_crtc->cursor_addr = addr;
55a08b3f 14156 intel_crtc_update_cursor(crtc, state);
852e787c
GP
14157}
14158
3d7d6510
MR
14159static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14160 int pipe)
14161{
14162 struct intel_plane *cursor;
8e7d688b 14163 struct intel_plane_state *state;
3d7d6510
MR
14164
14165 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14166 if (cursor == NULL)
14167 return NULL;
14168
8e7d688b
MR
14169 state = intel_create_plane_state(&cursor->base);
14170 if (!state) {
ea2c67bb
MR
14171 kfree(cursor);
14172 return NULL;
14173 }
8e7d688b 14174 cursor->base.state = &state->base;
ea2c67bb 14175
3d7d6510
MR
14176 cursor->can_scale = false;
14177 cursor->max_downscale = 1;
14178 cursor->pipe = pipe;
14179 cursor->plane = pipe;
a9ff8714 14180 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 14181 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 14182 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 14183 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14184
14185 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14186 &intel_plane_funcs,
3d7d6510
MR
14187 intel_cursor_formats,
14188 ARRAY_SIZE(intel_cursor_formats),
b0b3b795 14189 DRM_PLANE_TYPE_CURSOR, NULL);
4398ad45
VS
14190
14191 if (INTEL_INFO(dev)->gen >= 4) {
14192 if (!dev->mode_config.rotation_property)
14193 dev->mode_config.rotation_property =
14194 drm_mode_create_rotation_property(dev,
14195 BIT(DRM_ROTATE_0) |
14196 BIT(DRM_ROTATE_180));
14197 if (dev->mode_config.rotation_property)
14198 drm_object_attach_property(&cursor->base.base,
14199 dev->mode_config.rotation_property,
8e7d688b 14200 state->base.rotation);
4398ad45
VS
14201 }
14202
af99ceda
CK
14203 if (INTEL_INFO(dev)->gen >=9)
14204 state->scaler_id = -1;
14205
ea2c67bb
MR
14206 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14207
3d7d6510
MR
14208 return &cursor->base;
14209}
14210
549e2bfb
CK
14211static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14212 struct intel_crtc_state *crtc_state)
14213{
14214 int i;
14215 struct intel_scaler *intel_scaler;
14216 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14217
14218 for (i = 0; i < intel_crtc->num_scalers; i++) {
14219 intel_scaler = &scaler_state->scalers[i];
14220 intel_scaler->in_use = 0;
549e2bfb
CK
14221 intel_scaler->mode = PS_SCALER_MODE_DYN;
14222 }
14223
14224 scaler_state->scaler_id = -1;
14225}
14226
b358d0a6 14227static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14228{
fbee40df 14229 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14230 struct intel_crtc *intel_crtc;
f5de6e07 14231 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14232 struct drm_plane *primary = NULL;
14233 struct drm_plane *cursor = NULL;
465c120c 14234 int i, ret;
79e53945 14235
955382f3 14236 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14237 if (intel_crtc == NULL)
14238 return;
14239
f5de6e07
ACO
14240 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14241 if (!crtc_state)
14242 goto fail;
550acefd
ACO
14243 intel_crtc->config = crtc_state;
14244 intel_crtc->base.state = &crtc_state->base;
07878248 14245 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14246
549e2bfb
CK
14247 /* initialize shared scalers */
14248 if (INTEL_INFO(dev)->gen >= 9) {
14249 if (pipe == PIPE_C)
14250 intel_crtc->num_scalers = 1;
14251 else
14252 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14253
14254 skl_init_scalers(dev, intel_crtc, crtc_state);
14255 }
14256
465c120c 14257 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14258 if (!primary)
14259 goto fail;
14260
14261 cursor = intel_cursor_plane_create(dev, pipe);
14262 if (!cursor)
14263 goto fail;
14264
465c120c 14265 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
f9882876 14266 cursor, &intel_crtc_funcs, NULL);
3d7d6510
MR
14267 if (ret)
14268 goto fail;
79e53945
JB
14269
14270 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14271 for (i = 0; i < 256; i++) {
14272 intel_crtc->lut_r[i] = i;
14273 intel_crtc->lut_g[i] = i;
14274 intel_crtc->lut_b[i] = i;
14275 }
14276
1f1c2e24
VS
14277 /*
14278 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14279 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14280 */
80824003
JB
14281 intel_crtc->pipe = pipe;
14282 intel_crtc->plane = pipe;
3a77c4c4 14283 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14284 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14285 intel_crtc->plane = !pipe;
80824003
JB
14286 }
14287
4b0e333e
CW
14288 intel_crtc->cursor_base = ~0;
14289 intel_crtc->cursor_cntl = ~0;
dc41c154 14290 intel_crtc->cursor_size = ~0;
8d7849db 14291
852eb00d
VS
14292 intel_crtc->wm.cxsr_allowed = true;
14293
22fd0fab
JB
14294 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14295 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14296 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14297 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14298
79e53945 14299 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14300
14301 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14302 return;
14303
14304fail:
14305 if (primary)
14306 drm_plane_cleanup(primary);
14307 if (cursor)
14308 drm_plane_cleanup(cursor);
f5de6e07 14309 kfree(crtc_state);
3d7d6510 14310 kfree(intel_crtc);
79e53945
JB
14311}
14312
752aa88a
JB
14313enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14314{
14315 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14316 struct drm_device *dev = connector->base.dev;
752aa88a 14317
51fd371b 14318 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14319
d3babd3f 14320 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14321 return INVALID_PIPE;
14322
14323 return to_intel_crtc(encoder->crtc)->pipe;
14324}
14325
08d7b3d1 14326int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14327 struct drm_file *file)
08d7b3d1 14328{
08d7b3d1 14329 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14330 struct drm_crtc *drmmode_crtc;
c05422d5 14331 struct intel_crtc *crtc;
08d7b3d1 14332
7707e653 14333 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14334
7707e653 14335 if (!drmmode_crtc) {
08d7b3d1 14336 DRM_ERROR("no such CRTC id\n");
3f2c2057 14337 return -ENOENT;
08d7b3d1
CW
14338 }
14339
7707e653 14340 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14341 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14342
c05422d5 14343 return 0;
08d7b3d1
CW
14344}
14345
66a9278e 14346static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14347{
66a9278e
DV
14348 struct drm_device *dev = encoder->base.dev;
14349 struct intel_encoder *source_encoder;
79e53945 14350 int index_mask = 0;
79e53945
JB
14351 int entry = 0;
14352
b2784e15 14353 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14354 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14355 index_mask |= (1 << entry);
14356
79e53945
JB
14357 entry++;
14358 }
4ef69c7a 14359
79e53945
JB
14360 return index_mask;
14361}
14362
4d302442
CW
14363static bool has_edp_a(struct drm_device *dev)
14364{
14365 struct drm_i915_private *dev_priv = dev->dev_private;
14366
14367 if (!IS_MOBILE(dev))
14368 return false;
14369
14370 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14371 return false;
14372
e3589908 14373 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14374 return false;
14375
14376 return true;
14377}
14378
84b4e042
JB
14379static bool intel_crt_present(struct drm_device *dev)
14380{
14381 struct drm_i915_private *dev_priv = dev->dev_private;
14382
884497ed
DL
14383 if (INTEL_INFO(dev)->gen >= 9)
14384 return false;
14385
cf404ce4 14386 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14387 return false;
14388
14389 if (IS_CHERRYVIEW(dev))
14390 return false;
14391
65e472e4
VS
14392 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14393 return false;
14394
70ac54d0
VS
14395 /* DDI E can't be used if DDI A requires 4 lanes */
14396 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14397 return false;
14398
e4abb733 14399 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
14400 return false;
14401
14402 return true;
14403}
14404
79e53945
JB
14405static void intel_setup_outputs(struct drm_device *dev)
14406{
725e30ad 14407 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14408 struct intel_encoder *encoder;
cb0953d7 14409 bool dpd_is_edp = false;
79e53945 14410
c9093354 14411 intel_lvds_init(dev);
79e53945 14412
84b4e042 14413 if (intel_crt_present(dev))
79935fca 14414 intel_crt_init(dev);
cb0953d7 14415
c776eb2e
VK
14416 if (IS_BROXTON(dev)) {
14417 /*
14418 * FIXME: Broxton doesn't support port detection via the
14419 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14420 * detect the ports.
14421 */
14422 intel_ddi_init(dev, PORT_A);
14423 intel_ddi_init(dev, PORT_B);
14424 intel_ddi_init(dev, PORT_C);
14425 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14426 int found;
14427
de31facd
JB
14428 /*
14429 * Haswell uses DDI functions to detect digital outputs.
14430 * On SKL pre-D0 the strap isn't connected, so we assume
14431 * it's there.
14432 */
77179400 14433 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14434 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14435 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14436 intel_ddi_init(dev, PORT_A);
14437
14438 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14439 * register */
14440 found = I915_READ(SFUSE_STRAP);
14441
14442 if (found & SFUSE_STRAP_DDIB_DETECTED)
14443 intel_ddi_init(dev, PORT_B);
14444 if (found & SFUSE_STRAP_DDIC_DETECTED)
14445 intel_ddi_init(dev, PORT_C);
14446 if (found & SFUSE_STRAP_DDID_DETECTED)
14447 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14448 /*
14449 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14450 */
ef11bdb3 14451 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14452 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14453 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14454 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14455 intel_ddi_init(dev, PORT_E);
14456
0e72a5b5 14457 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14458 int found;
5d8a7752 14459 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14460
14461 if (has_edp_a(dev))
14462 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14463
dc0fa718 14464 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14465 /* PCH SDVOB multiplex with HDMIB */
2a5c0832 14466 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
30ad48b7 14467 if (!found)
e2debe91 14468 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14469 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14470 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14471 }
14472
dc0fa718 14473 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14474 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14475
dc0fa718 14476 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14477 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14478
5eb08b69 14479 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14480 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14481
270b3042 14482 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14483 intel_dp_init(dev, PCH_DP_D, PORT_D);
666a4537 14484 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
e17ac6db
VS
14485 /*
14486 * The DP_DETECTED bit is the latched state of the DDC
14487 * SDA pin at boot. However since eDP doesn't require DDC
14488 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14489 * eDP ports may have been muxed to an alternate function.
14490 * Thus we can't rely on the DP_DETECTED bit alone to detect
14491 * eDP ports. Consult the VBT as well as DP_DETECTED to
14492 * detect eDP ports.
14493 */
e66eb81d 14494 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14495 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14496 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14497 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14498 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14499 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14500
e66eb81d 14501 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14502 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14503 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14504 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14505 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14506 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14507
9418c1f1 14508 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14509 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14510 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14511 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14512 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14513 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14514 }
14515
3cfca973 14516 intel_dsi_init(dev);
09da55dc 14517 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14518 bool found = false;
7d57382e 14519
e2debe91 14520 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14521 DRM_DEBUG_KMS("probing SDVOB\n");
2a5c0832 14522 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
3fec3d2f 14523 if (!found && IS_G4X(dev)) {
b01f2c3a 14524 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14525 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14526 }
27185ae1 14527
3fec3d2f 14528 if (!found && IS_G4X(dev))
ab9d7c30 14529 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14530 }
13520b05
KH
14531
14532 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14533
e2debe91 14534 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14535 DRM_DEBUG_KMS("probing SDVOC\n");
2a5c0832 14536 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
b01f2c3a 14537 }
27185ae1 14538
e2debe91 14539 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14540
3fec3d2f 14541 if (IS_G4X(dev)) {
b01f2c3a 14542 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14543 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14544 }
3fec3d2f 14545 if (IS_G4X(dev))
ab9d7c30 14546 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14547 }
27185ae1 14548
3fec3d2f 14549 if (IS_G4X(dev) &&
e7281eab 14550 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14551 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14552 } else if (IS_GEN2(dev))
79e53945
JB
14553 intel_dvo_init(dev);
14554
103a196f 14555 if (SUPPORTS_TV(dev))
79e53945
JB
14556 intel_tv_init(dev);
14557
0bc12bcb 14558 intel_psr_init(dev);
7c8f8a70 14559
b2784e15 14560 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14561 encoder->base.possible_crtcs = encoder->crtc_mask;
14562 encoder->base.possible_clones =
66a9278e 14563 intel_encoder_clones(encoder);
79e53945 14564 }
47356eb6 14565
dde86e2d 14566 intel_init_pch_refclk(dev);
270b3042
DV
14567
14568 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14569}
14570
14571static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14572{
60a5ca01 14573 struct drm_device *dev = fb->dev;
79e53945 14574 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14575
ef2d633e 14576 drm_framebuffer_cleanup(fb);
60a5ca01 14577 mutex_lock(&dev->struct_mutex);
ef2d633e 14578 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14579 drm_gem_object_unreference(&intel_fb->obj->base);
14580 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14581 kfree(intel_fb);
14582}
14583
14584static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14585 struct drm_file *file,
79e53945
JB
14586 unsigned int *handle)
14587{
14588 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14589 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14590
cc917ab4
CW
14591 if (obj->userptr.mm) {
14592 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14593 return -EINVAL;
14594 }
14595
05394f39 14596 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14597}
14598
86c98588
RV
14599static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14600 struct drm_file *file,
14601 unsigned flags, unsigned color,
14602 struct drm_clip_rect *clips,
14603 unsigned num_clips)
14604{
14605 struct drm_device *dev = fb->dev;
14606 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14607 struct drm_i915_gem_object *obj = intel_fb->obj;
14608
14609 mutex_lock(&dev->struct_mutex);
74b4ea1e 14610 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14611 mutex_unlock(&dev->struct_mutex);
14612
14613 return 0;
14614}
14615
79e53945
JB
14616static const struct drm_framebuffer_funcs intel_fb_funcs = {
14617 .destroy = intel_user_framebuffer_destroy,
14618 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14619 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14620};
14621
b321803d
DL
14622static
14623u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14624 uint32_t pixel_format)
14625{
14626 u32 gen = INTEL_INFO(dev)->gen;
14627
14628 if (gen >= 9) {
ac484963
VS
14629 int cpp = drm_format_plane_cpp(pixel_format, 0);
14630
b321803d
DL
14631 /* "The stride in bytes must not exceed the of the size of 8K
14632 * pixels and 32K bytes."
14633 */
ac484963 14634 return min(8192 * cpp, 32768);
666a4537 14635 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
b321803d
DL
14636 return 32*1024;
14637 } else if (gen >= 4) {
14638 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14639 return 16*1024;
14640 else
14641 return 32*1024;
14642 } else if (gen >= 3) {
14643 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14644 return 8*1024;
14645 else
14646 return 16*1024;
14647 } else {
14648 /* XXX DSPC is limited to 4k tiled */
14649 return 8*1024;
14650 }
14651}
14652
b5ea642a
DV
14653static int intel_framebuffer_init(struct drm_device *dev,
14654 struct intel_framebuffer *intel_fb,
14655 struct drm_mode_fb_cmd2 *mode_cmd,
14656 struct drm_i915_gem_object *obj)
79e53945 14657{
7b49f948 14658 struct drm_i915_private *dev_priv = to_i915(dev);
6761dd31 14659 unsigned int aligned_height;
79e53945 14660 int ret;
b321803d 14661 u32 pitch_limit, stride_alignment;
79e53945 14662
dd4916c5
DV
14663 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14664
2a80eada
DV
14665 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14666 /* Enforce that fb modifier and tiling mode match, but only for
14667 * X-tiled. This is needed for FBC. */
14668 if (!!(obj->tiling_mode == I915_TILING_X) !=
14669 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14670 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14671 return -EINVAL;
14672 }
14673 } else {
14674 if (obj->tiling_mode == I915_TILING_X)
14675 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14676 else if (obj->tiling_mode == I915_TILING_Y) {
14677 DRM_DEBUG("No Y tiling for legacy addfb\n");
14678 return -EINVAL;
14679 }
14680 }
14681
9a8f0a12
TU
14682 /* Passed in modifier sanity checking. */
14683 switch (mode_cmd->modifier[0]) {
14684 case I915_FORMAT_MOD_Y_TILED:
14685 case I915_FORMAT_MOD_Yf_TILED:
14686 if (INTEL_INFO(dev)->gen < 9) {
14687 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14688 mode_cmd->modifier[0]);
14689 return -EINVAL;
14690 }
14691 case DRM_FORMAT_MOD_NONE:
14692 case I915_FORMAT_MOD_X_TILED:
14693 break;
14694 default:
c0f40428
JB
14695 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14696 mode_cmd->modifier[0]);
57cd6508 14697 return -EINVAL;
c16ed4be 14698 }
57cd6508 14699
7b49f948
VS
14700 stride_alignment = intel_fb_stride_alignment(dev_priv,
14701 mode_cmd->modifier[0],
b321803d
DL
14702 mode_cmd->pixel_format);
14703 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14704 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14705 mode_cmd->pitches[0], stride_alignment);
57cd6508 14706 return -EINVAL;
c16ed4be 14707 }
57cd6508 14708
b321803d
DL
14709 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14710 mode_cmd->pixel_format);
a35cdaa0 14711 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14712 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14713 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14714 "tiled" : "linear",
a35cdaa0 14715 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14716 return -EINVAL;
c16ed4be 14717 }
5d7bd705 14718
2a80eada 14719 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14720 mode_cmd->pitches[0] != obj->stride) {
14721 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14722 mode_cmd->pitches[0], obj->stride);
5d7bd705 14723 return -EINVAL;
c16ed4be 14724 }
5d7bd705 14725
57779d06 14726 /* Reject formats not supported by any plane early. */
308e5bcb 14727 switch (mode_cmd->pixel_format) {
57779d06 14728 case DRM_FORMAT_C8:
04b3924d
VS
14729 case DRM_FORMAT_RGB565:
14730 case DRM_FORMAT_XRGB8888:
14731 case DRM_FORMAT_ARGB8888:
57779d06
VS
14732 break;
14733 case DRM_FORMAT_XRGB1555:
c16ed4be 14734 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14735 DRM_DEBUG("unsupported pixel format: %s\n",
14736 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14737 return -EINVAL;
c16ed4be 14738 }
57779d06 14739 break;
57779d06 14740 case DRM_FORMAT_ABGR8888:
666a4537
WB
14741 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14742 INTEL_INFO(dev)->gen < 9) {
6c0fd451
DL
14743 DRM_DEBUG("unsupported pixel format: %s\n",
14744 drm_get_format_name(mode_cmd->pixel_format));
14745 return -EINVAL;
14746 }
14747 break;
14748 case DRM_FORMAT_XBGR8888:
04b3924d 14749 case DRM_FORMAT_XRGB2101010:
57779d06 14750 case DRM_FORMAT_XBGR2101010:
c16ed4be 14751 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14752 DRM_DEBUG("unsupported pixel format: %s\n",
14753 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14754 return -EINVAL;
c16ed4be 14755 }
b5626747 14756 break;
7531208b 14757 case DRM_FORMAT_ABGR2101010:
666a4537 14758 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7531208b
DL
14759 DRM_DEBUG("unsupported pixel format: %s\n",
14760 drm_get_format_name(mode_cmd->pixel_format));
14761 return -EINVAL;
14762 }
14763 break;
04b3924d
VS
14764 case DRM_FORMAT_YUYV:
14765 case DRM_FORMAT_UYVY:
14766 case DRM_FORMAT_YVYU:
14767 case DRM_FORMAT_VYUY:
c16ed4be 14768 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14769 DRM_DEBUG("unsupported pixel format: %s\n",
14770 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14771 return -EINVAL;
c16ed4be 14772 }
57cd6508
CW
14773 break;
14774 default:
4ee62c76
VS
14775 DRM_DEBUG("unsupported pixel format: %s\n",
14776 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14777 return -EINVAL;
14778 }
14779
90f9a336
VS
14780 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14781 if (mode_cmd->offsets[0] != 0)
14782 return -EINVAL;
14783
ec2c981e 14784 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14785 mode_cmd->pixel_format,
14786 mode_cmd->modifier[0]);
53155c0a
DV
14787 /* FIXME drm helper for size checks (especially planar formats)? */
14788 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14789 return -EINVAL;
14790
c7d73f6a
DV
14791 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14792 intel_fb->obj = obj;
14793
2d7a215f
VS
14794 intel_fill_fb_info(dev_priv, &intel_fb->base);
14795
79e53945
JB
14796 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14797 if (ret) {
14798 DRM_ERROR("framebuffer init failed %d\n", ret);
14799 return ret;
14800 }
14801
0b05e1e0
VS
14802 intel_fb->obj->framebuffer_references++;
14803
79e53945
JB
14804 return 0;
14805}
14806
79e53945
JB
14807static struct drm_framebuffer *
14808intel_user_framebuffer_create(struct drm_device *dev,
14809 struct drm_file *filp,
1eb83451 14810 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 14811{
dcb1394e 14812 struct drm_framebuffer *fb;
05394f39 14813 struct drm_i915_gem_object *obj;
76dc3769 14814 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 14815
308e5bcb 14816 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
76dc3769 14817 mode_cmd.handles[0]));
c8725226 14818 if (&obj->base == NULL)
cce13ff7 14819 return ERR_PTR(-ENOENT);
79e53945 14820
92907cbb 14821 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e
LW
14822 if (IS_ERR(fb))
14823 drm_gem_object_unreference_unlocked(&obj->base);
14824
14825 return fb;
79e53945
JB
14826}
14827
0695726e 14828#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14829static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14830{
14831}
14832#endif
14833
79e53945 14834static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14835 .fb_create = intel_user_framebuffer_create,
0632fef6 14836 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14837 .atomic_check = intel_atomic_check,
14838 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14839 .atomic_state_alloc = intel_atomic_state_alloc,
14840 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14841};
14842
e70236a8
JB
14843/* Set up chip specific display functions */
14844static void intel_init_display(struct drm_device *dev)
14845{
14846 struct drm_i915_private *dev_priv = dev->dev_private;
14847
ee9300bb
DV
14848 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14849 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14850 else if (IS_CHERRYVIEW(dev))
14851 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14852 else if (IS_VALLEYVIEW(dev))
14853 dev_priv->display.find_dpll = vlv_find_best_dpll;
14854 else if (IS_PINEVIEW(dev))
14855 dev_priv->display.find_dpll = pnv_find_best_dpll;
14856 else
14857 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14858
bc8d7dff
DL
14859 if (INTEL_INFO(dev)->gen >= 9) {
14860 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14861 dev_priv->display.get_initial_plane_config =
14862 skylake_get_initial_plane_config;
bc8d7dff
DL
14863 dev_priv->display.crtc_compute_clock =
14864 haswell_crtc_compute_clock;
14865 dev_priv->display.crtc_enable = haswell_crtc_enable;
14866 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff 14867 } else if (HAS_DDI(dev)) {
0e8ffe1b 14868 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14869 dev_priv->display.get_initial_plane_config =
14870 ironlake_get_initial_plane_config;
797d0259
ACO
14871 dev_priv->display.crtc_compute_clock =
14872 haswell_crtc_compute_clock;
4f771f10
PZ
14873 dev_priv->display.crtc_enable = haswell_crtc_enable;
14874 dev_priv->display.crtc_disable = haswell_crtc_disable;
09b4ddf9 14875 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14876 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14877 dev_priv->display.get_initial_plane_config =
14878 ironlake_get_initial_plane_config;
3fb37703
ACO
14879 dev_priv->display.crtc_compute_clock =
14880 ironlake_crtc_compute_clock;
76e5a89c
DV
14881 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14882 dev_priv->display.crtc_disable = ironlake_crtc_disable;
666a4537 14883 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
89b667f8 14884 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14885 dev_priv->display.get_initial_plane_config =
14886 i9xx_get_initial_plane_config;
d6dfee7a 14887 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14888 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14889 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14890 } else {
0e8ffe1b 14891 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14892 dev_priv->display.get_initial_plane_config =
14893 i9xx_get_initial_plane_config;
d6dfee7a 14894 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14895 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14896 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 14897 }
e70236a8 14898
e70236a8 14899 /* Returns the core display clock speed */
ef11bdb3 14900 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
1652d19e
VS
14901 dev_priv->display.get_display_clock_speed =
14902 skylake_get_display_clock_speed;
acd3f3d3
BP
14903 else if (IS_BROXTON(dev))
14904 dev_priv->display.get_display_clock_speed =
14905 broxton_get_display_clock_speed;
1652d19e
VS
14906 else if (IS_BROADWELL(dev))
14907 dev_priv->display.get_display_clock_speed =
14908 broadwell_get_display_clock_speed;
14909 else if (IS_HASWELL(dev))
14910 dev_priv->display.get_display_clock_speed =
14911 haswell_get_display_clock_speed;
666a4537 14912 else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
25eb05fc
JB
14913 dev_priv->display.get_display_clock_speed =
14914 valleyview_get_display_clock_speed;
b37a6434
VS
14915 else if (IS_GEN5(dev))
14916 dev_priv->display.get_display_clock_speed =
14917 ilk_get_display_clock_speed;
a7c66cd8 14918 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14919 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14920 dev_priv->display.get_display_clock_speed =
14921 i945_get_display_clock_speed;
34edce2f
VS
14922 else if (IS_GM45(dev))
14923 dev_priv->display.get_display_clock_speed =
14924 gm45_get_display_clock_speed;
14925 else if (IS_CRESTLINE(dev))
14926 dev_priv->display.get_display_clock_speed =
14927 i965gm_get_display_clock_speed;
14928 else if (IS_PINEVIEW(dev))
14929 dev_priv->display.get_display_clock_speed =
14930 pnv_get_display_clock_speed;
14931 else if (IS_G33(dev) || IS_G4X(dev))
14932 dev_priv->display.get_display_clock_speed =
14933 g33_get_display_clock_speed;
e70236a8
JB
14934 else if (IS_I915G(dev))
14935 dev_priv->display.get_display_clock_speed =
14936 i915_get_display_clock_speed;
257a7ffc 14937 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14938 dev_priv->display.get_display_clock_speed =
14939 i9xx_misc_get_display_clock_speed;
14940 else if (IS_I915GM(dev))
14941 dev_priv->display.get_display_clock_speed =
14942 i915gm_get_display_clock_speed;
14943 else if (IS_I865G(dev))
14944 dev_priv->display.get_display_clock_speed =
14945 i865_get_display_clock_speed;
f0f8a9ce 14946 else if (IS_I85X(dev))
e70236a8 14947 dev_priv->display.get_display_clock_speed =
1b1d2716 14948 i85x_get_display_clock_speed;
623e01e5
VS
14949 else { /* 830 */
14950 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14951 dev_priv->display.get_display_clock_speed =
14952 i830_get_display_clock_speed;
623e01e5 14953 }
e70236a8 14954
7c10a2b5 14955 if (IS_GEN5(dev)) {
3bb11b53 14956 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14957 } else if (IS_GEN6(dev)) {
14958 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14959 } else if (IS_IVYBRIDGE(dev)) {
14960 /* FIXME: detect B0+ stepping and use auto training */
14961 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14962 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14963 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14964 if (IS_BROADWELL(dev)) {
14965 dev_priv->display.modeset_commit_cdclk =
14966 broadwell_modeset_commit_cdclk;
14967 dev_priv->display.modeset_calc_cdclk =
14968 broadwell_modeset_calc_cdclk;
14969 }
666a4537 14970 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
27c329ed
ML
14971 dev_priv->display.modeset_commit_cdclk =
14972 valleyview_modeset_commit_cdclk;
14973 dev_priv->display.modeset_calc_cdclk =
14974 valleyview_modeset_calc_cdclk;
f8437dd1 14975 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14976 dev_priv->display.modeset_commit_cdclk =
14977 broxton_modeset_commit_cdclk;
14978 dev_priv->display.modeset_calc_cdclk =
14979 broxton_modeset_calc_cdclk;
e70236a8 14980 }
8c9f3aaf 14981
8c9f3aaf
JB
14982 switch (INTEL_INFO(dev)->gen) {
14983 case 2:
14984 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14985 break;
14986
14987 case 3:
14988 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14989 break;
14990
14991 case 4:
14992 case 5:
14993 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14994 break;
14995
14996 case 6:
14997 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14998 break;
7c9017e5 14999 case 7:
4e0bbc31 15000 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
15001 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15002 break;
830c81db 15003 case 9:
ba343e02
TU
15004 /* Drop through - unsupported since execlist only. */
15005 default:
15006 /* Default just returns -ENODEV to indicate unsupported */
15007 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 15008 }
7bd688cd 15009
e39b999a 15010 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
15011}
15012
b690e96c
JB
15013/*
15014 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15015 * resume, or other times. This quirk makes sure that's the case for
15016 * affected systems.
15017 */
0206e353 15018static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
15019{
15020 struct drm_i915_private *dev_priv = dev->dev_private;
15021
15022 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 15023 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
15024}
15025
b6b5d049
VS
15026static void quirk_pipeb_force(struct drm_device *dev)
15027{
15028 struct drm_i915_private *dev_priv = dev->dev_private;
15029
15030 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15031 DRM_INFO("applying pipe b force quirk\n");
15032}
15033
435793df
KP
15034/*
15035 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15036 */
15037static void quirk_ssc_force_disable(struct drm_device *dev)
15038{
15039 struct drm_i915_private *dev_priv = dev->dev_private;
15040 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 15041 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
15042}
15043
4dca20ef 15044/*
5a15ab5b
CE
15045 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15046 * brightness value
4dca20ef
CE
15047 */
15048static void quirk_invert_brightness(struct drm_device *dev)
15049{
15050 struct drm_i915_private *dev_priv = dev->dev_private;
15051 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 15052 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
15053}
15054
9c72cc6f
SD
15055/* Some VBT's incorrectly indicate no backlight is present */
15056static void quirk_backlight_present(struct drm_device *dev)
15057{
15058 struct drm_i915_private *dev_priv = dev->dev_private;
15059 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15060 DRM_INFO("applying backlight present quirk\n");
15061}
15062
b690e96c
JB
15063struct intel_quirk {
15064 int device;
15065 int subsystem_vendor;
15066 int subsystem_device;
15067 void (*hook)(struct drm_device *dev);
15068};
15069
5f85f176
EE
15070/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15071struct intel_dmi_quirk {
15072 void (*hook)(struct drm_device *dev);
15073 const struct dmi_system_id (*dmi_id_list)[];
15074};
15075
15076static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15077{
15078 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15079 return 1;
15080}
15081
15082static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15083 {
15084 .dmi_id_list = &(const struct dmi_system_id[]) {
15085 {
15086 .callback = intel_dmi_reverse_brightness,
15087 .ident = "NCR Corporation",
15088 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15089 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15090 },
15091 },
15092 { } /* terminating entry */
15093 },
15094 .hook = quirk_invert_brightness,
15095 },
15096};
15097
c43b5634 15098static struct intel_quirk intel_quirks[] = {
b690e96c
JB
15099 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15100 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15101
b690e96c
JB
15102 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15103 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15104
5f080c0f
VS
15105 /* 830 needs to leave pipe A & dpll A up */
15106 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15107
b6b5d049
VS
15108 /* 830 needs to leave pipe B & dpll B up */
15109 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15110
435793df
KP
15111 /* Lenovo U160 cannot use SSC on LVDS */
15112 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
15113
15114 /* Sony Vaio Y cannot use SSC on LVDS */
15115 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 15116
be505f64
AH
15117 /* Acer Aspire 5734Z must invert backlight brightness */
15118 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15119
15120 /* Acer/eMachines G725 */
15121 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15122
15123 /* Acer/eMachines e725 */
15124 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15125
15126 /* Acer/Packard Bell NCL20 */
15127 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15128
15129 /* Acer Aspire 4736Z */
15130 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
15131
15132 /* Acer Aspire 5336 */
15133 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
15134
15135 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15136 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 15137
dfb3d47b
SD
15138 /* Acer C720 Chromebook (Core i3 4005U) */
15139 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15140
b2a9601c 15141 /* Apple Macbook 2,1 (Core 2 T7400) */
15142 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15143
1b9448b0
JN
15144 /* Apple Macbook 4,1 */
15145 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15146
d4967d8c
SD
15147 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15148 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
15149
15150 /* HP Chromebook 14 (Celeron 2955U) */
15151 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
15152
15153 /* Dell Chromebook 11 */
15154 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
15155
15156 /* Dell Chromebook 11 (2015 version) */
15157 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
15158};
15159
15160static void intel_init_quirks(struct drm_device *dev)
15161{
15162 struct pci_dev *d = dev->pdev;
15163 int i;
15164
15165 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15166 struct intel_quirk *q = &intel_quirks[i];
15167
15168 if (d->device == q->device &&
15169 (d->subsystem_vendor == q->subsystem_vendor ||
15170 q->subsystem_vendor == PCI_ANY_ID) &&
15171 (d->subsystem_device == q->subsystem_device ||
15172 q->subsystem_device == PCI_ANY_ID))
15173 q->hook(dev);
15174 }
5f85f176
EE
15175 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15176 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15177 intel_dmi_quirks[i].hook(dev);
15178 }
b690e96c
JB
15179}
15180
9cce37f4
JB
15181/* Disable the VGA plane that we never use */
15182static void i915_disable_vga(struct drm_device *dev)
15183{
15184 struct drm_i915_private *dev_priv = dev->dev_private;
15185 u8 sr1;
f0f59a00 15186 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 15187
2b37c616 15188 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 15189 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 15190 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
15191 sr1 = inb(VGA_SR_DATA);
15192 outb(sr1 | 1<<5, VGA_SR_DATA);
15193 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15194 udelay(300);
15195
01f5a626 15196 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15197 POSTING_READ(vga_reg);
15198}
15199
f817586c
DV
15200void intel_modeset_init_hw(struct drm_device *dev)
15201{
1a617b77
ML
15202 struct drm_i915_private *dev_priv = dev->dev_private;
15203
b6283055 15204 intel_update_cdclk(dev);
1a617b77
ML
15205
15206 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15207
f817586c 15208 intel_init_clock_gating(dev);
8090c6b9 15209 intel_enable_gt_powersave(dev);
f817586c
DV
15210}
15211
d93c0372
MR
15212/*
15213 * Calculate what we think the watermarks should be for the state we've read
15214 * out of the hardware and then immediately program those watermarks so that
15215 * we ensure the hardware settings match our internal state.
15216 *
15217 * We can calculate what we think WM's should be by creating a duplicate of the
15218 * current state (which was constructed during hardware readout) and running it
15219 * through the atomic check code to calculate new watermark values in the
15220 * state object.
15221 */
15222static void sanitize_watermarks(struct drm_device *dev)
15223{
15224 struct drm_i915_private *dev_priv = to_i915(dev);
15225 struct drm_atomic_state *state;
15226 struct drm_crtc *crtc;
15227 struct drm_crtc_state *cstate;
15228 struct drm_modeset_acquire_ctx ctx;
15229 int ret;
15230 int i;
15231
15232 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 15233 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
15234 return;
15235
15236 /*
15237 * We need to hold connection_mutex before calling duplicate_state so
15238 * that the connector loop is protected.
15239 */
15240 drm_modeset_acquire_init(&ctx, 0);
15241retry:
0cd1262d 15242 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
15243 if (ret == -EDEADLK) {
15244 drm_modeset_backoff(&ctx);
15245 goto retry;
15246 } else if (WARN_ON(ret)) {
0cd1262d 15247 goto fail;
d93c0372
MR
15248 }
15249
15250 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15251 if (WARN_ON(IS_ERR(state)))
0cd1262d 15252 goto fail;
d93c0372 15253
ed4a6a7c
MR
15254 /*
15255 * Hardware readout is the only time we don't want to calculate
15256 * intermediate watermarks (since we don't trust the current
15257 * watermarks).
15258 */
15259 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15260
d93c0372
MR
15261 ret = intel_atomic_check(dev, state);
15262 if (ret) {
15263 /*
15264 * If we fail here, it means that the hardware appears to be
15265 * programmed in a way that shouldn't be possible, given our
15266 * understanding of watermark requirements. This might mean a
15267 * mistake in the hardware readout code or a mistake in the
15268 * watermark calculations for a given platform. Raise a WARN
15269 * so that this is noticeable.
15270 *
15271 * If this actually happens, we'll have to just leave the
15272 * BIOS-programmed watermarks untouched and hope for the best.
15273 */
15274 WARN(true, "Could not determine valid watermarks for inherited state\n");
0cd1262d 15275 goto fail;
d93c0372
MR
15276 }
15277
15278 /* Write calculated watermark values back */
15279 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15280 for_each_crtc_in_state(state, crtc, cstate, i) {
15281 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15282
ed4a6a7c
MR
15283 cs->wm.need_postvbl_update = true;
15284 dev_priv->display.optimize_watermarks(cs);
d93c0372
MR
15285 }
15286
15287 drm_atomic_state_free(state);
0cd1262d 15288fail:
d93c0372
MR
15289 drm_modeset_drop_locks(&ctx);
15290 drm_modeset_acquire_fini(&ctx);
15291}
15292
79e53945
JB
15293void intel_modeset_init(struct drm_device *dev)
15294{
652c393a 15295 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15296 int sprite, ret;
8cc87b75 15297 enum pipe pipe;
46f297fb 15298 struct intel_crtc *crtc;
79e53945
JB
15299
15300 drm_mode_config_init(dev);
15301
15302 dev->mode_config.min_width = 0;
15303 dev->mode_config.min_height = 0;
15304
019d96cb
DA
15305 dev->mode_config.preferred_depth = 24;
15306 dev->mode_config.prefer_shadow = 1;
15307
25bab385
TU
15308 dev->mode_config.allow_fb_modifiers = true;
15309
e6ecefaa 15310 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15311
b690e96c
JB
15312 intel_init_quirks(dev);
15313
1fa61106
ED
15314 intel_init_pm(dev);
15315
e3c74757
BW
15316 if (INTEL_INFO(dev)->num_pipes == 0)
15317 return;
15318
69f92f67
LW
15319 /*
15320 * There may be no VBT; and if the BIOS enabled SSC we can
15321 * just keep using it to avoid unnecessary flicker. Whereas if the
15322 * BIOS isn't using it, don't assume it will work even if the VBT
15323 * indicates as much.
15324 */
15325 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15326 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15327 DREF_SSC1_ENABLE);
15328
15329 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15330 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15331 bios_lvds_use_ssc ? "en" : "dis",
15332 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15333 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15334 }
15335 }
15336
e70236a8 15337 intel_init_display(dev);
7c10a2b5 15338 intel_init_audio(dev);
e70236a8 15339
a6c45cf0
CW
15340 if (IS_GEN2(dev)) {
15341 dev->mode_config.max_width = 2048;
15342 dev->mode_config.max_height = 2048;
15343 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15344 dev->mode_config.max_width = 4096;
15345 dev->mode_config.max_height = 4096;
79e53945 15346 } else {
a6c45cf0
CW
15347 dev->mode_config.max_width = 8192;
15348 dev->mode_config.max_height = 8192;
79e53945 15349 }
068be561 15350
dc41c154
VS
15351 if (IS_845G(dev) || IS_I865G(dev)) {
15352 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15353 dev->mode_config.cursor_height = 1023;
15354 } else if (IS_GEN2(dev)) {
068be561
DL
15355 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15356 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15357 } else {
15358 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15359 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15360 }
15361
5d4545ae 15362 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15363
28c97730 15364 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15365 INTEL_INFO(dev)->num_pipes,
15366 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15367
055e393f 15368 for_each_pipe(dev_priv, pipe) {
8cc87b75 15369 intel_crtc_init(dev, pipe);
3bdcfc0c 15370 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15371 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15372 if (ret)
06da8da2 15373 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15374 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15375 }
79e53945
JB
15376 }
15377
bfa7df01 15378 intel_update_czclk(dev_priv);
e7dc33f3 15379 intel_update_rawclk(dev_priv);
bfa7df01
VS
15380 intel_update_cdclk(dev);
15381
e72f9fbf 15382 intel_shared_dpll_init(dev);
ee7b9f93 15383
9cce37f4
JB
15384 /* Just disable it once at startup */
15385 i915_disable_vga(dev);
79e53945 15386 intel_setup_outputs(dev);
11be49eb 15387
6e9f798d 15388 drm_modeset_lock_all(dev);
043e9bda 15389 intel_modeset_setup_hw_state(dev);
6e9f798d 15390 drm_modeset_unlock_all(dev);
46f297fb 15391
d3fcc808 15392 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15393 struct intel_initial_plane_config plane_config = {};
15394
46f297fb
JB
15395 if (!crtc->active)
15396 continue;
15397
46f297fb 15398 /*
46f297fb
JB
15399 * Note that reserving the BIOS fb up front prevents us
15400 * from stuffing other stolen allocations like the ring
15401 * on top. This prevents some ugliness at boot time, and
15402 * can even allow for smooth boot transitions if the BIOS
15403 * fb is large enough for the active pipe configuration.
15404 */
eeebeac5
ML
15405 dev_priv->display.get_initial_plane_config(crtc,
15406 &plane_config);
15407
15408 /*
15409 * If the fb is shared between multiple heads, we'll
15410 * just get the first one.
15411 */
15412 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15413 }
d93c0372
MR
15414
15415 /*
15416 * Make sure hardware watermarks really match the state we read out.
15417 * Note that we need to do this after reconstructing the BIOS fb's
15418 * since the watermark calculation done here will use pstate->fb.
15419 */
15420 sanitize_watermarks(dev);
2c7111db
CW
15421}
15422
7fad798e
DV
15423static void intel_enable_pipe_a(struct drm_device *dev)
15424{
15425 struct intel_connector *connector;
15426 struct drm_connector *crt = NULL;
15427 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15428 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15429
15430 /* We can't just switch on the pipe A, we need to set things up with a
15431 * proper mode and output configuration. As a gross hack, enable pipe A
15432 * by enabling the load detect pipe once. */
3a3371ff 15433 for_each_intel_connector(dev, connector) {
7fad798e
DV
15434 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15435 crt = &connector->base;
15436 break;
15437 }
15438 }
15439
15440 if (!crt)
15441 return;
15442
208bf9fd 15443 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15444 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15445}
15446
fa555837
DV
15447static bool
15448intel_check_plane_mapping(struct intel_crtc *crtc)
15449{
7eb552ae
BW
15450 struct drm_device *dev = crtc->base.dev;
15451 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15452 u32 val;
fa555837 15453
7eb552ae 15454 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15455 return true;
15456
649636ef 15457 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15458
15459 if ((val & DISPLAY_PLANE_ENABLE) &&
15460 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15461 return false;
15462
15463 return true;
15464}
15465
02e93c35
VS
15466static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15467{
15468 struct drm_device *dev = crtc->base.dev;
15469 struct intel_encoder *encoder;
15470
15471 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15472 return true;
15473
15474 return false;
15475}
15476
dd756198
VS
15477static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15478{
15479 struct drm_device *dev = encoder->base.dev;
15480 struct intel_connector *connector;
15481
15482 for_each_connector_on_encoder(dev, &encoder->base, connector)
15483 return true;
15484
15485 return false;
15486}
15487
24929352
DV
15488static void intel_sanitize_crtc(struct intel_crtc *crtc)
15489{
15490 struct drm_device *dev = crtc->base.dev;
15491 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15492 i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
24929352 15493
24929352 15494 /* Clear any frame start delays used for debugging left by the BIOS */
24929352
DV
15495 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15496
d3eaf884 15497 /* restore vblank interrupts to correct state */
9625604c 15498 drm_crtc_vblank_reset(&crtc->base);
d297e103 15499 if (crtc->active) {
f9cd7b88
VS
15500 struct intel_plane *plane;
15501
9625604c 15502 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15503
15504 /* Disable everything but the primary plane */
15505 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15506 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15507 continue;
15508
15509 plane->disable_plane(&plane->base, &crtc->base);
15510 }
9625604c 15511 }
d3eaf884 15512
24929352 15513 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15514 * disable the crtc (and hence change the state) if it is wrong. Note
15515 * that gen4+ has a fixed plane -> pipe mapping. */
15516 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15517 bool plane;
15518
24929352
DV
15519 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15520 crtc->base.base.id);
15521
15522 /* Pipe has the wrong plane attached and the plane is active.
15523 * Temporarily change the plane mapping and disable everything
15524 * ... */
15525 plane = crtc->plane;
b70709a6 15526 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15527 crtc->plane = !plane;
b17d48e2 15528 intel_crtc_disable_noatomic(&crtc->base);
24929352 15529 crtc->plane = plane;
24929352 15530 }
24929352 15531
7fad798e
DV
15532 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15533 crtc->pipe == PIPE_A && !crtc->active) {
15534 /* BIOS forgot to enable pipe A, this mostly happens after
15535 * resume. Force-enable the pipe to fix this, the update_dpms
15536 * call below we restore the pipe to the right state, but leave
15537 * the required bits on. */
15538 intel_enable_pipe_a(dev);
15539 }
15540
24929352
DV
15541 /* Adjust the state of the output pipe according to whether we
15542 * have active connectors/encoders. */
842e0307 15543 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 15544 intel_crtc_disable_noatomic(&crtc->base);
24929352 15545
a3ed6aad 15546 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15547 /*
15548 * We start out with underrun reporting disabled to avoid races.
15549 * For correct bookkeeping mark this on active crtcs.
15550 *
c5ab3bc0
DV
15551 * Also on gmch platforms we dont have any hardware bits to
15552 * disable the underrun reporting. Which means we need to start
15553 * out with underrun reporting disabled also on inactive pipes,
15554 * since otherwise we'll complain about the garbage we read when
15555 * e.g. coming up after runtime pm.
15556 *
4cc31489
DV
15557 * No protection against concurrent access is required - at
15558 * worst a fifo underrun happens which also sets this to false.
15559 */
15560 crtc->cpu_fifo_underrun_disabled = true;
15561 crtc->pch_fifo_underrun_disabled = true;
15562 }
24929352
DV
15563}
15564
15565static void intel_sanitize_encoder(struct intel_encoder *encoder)
15566{
15567 struct intel_connector *connector;
15568 struct drm_device *dev = encoder->base.dev;
15569
15570 /* We need to check both for a crtc link (meaning that the
15571 * encoder is active and trying to read from a pipe) and the
15572 * pipe itself being active. */
15573 bool has_active_crtc = encoder->base.crtc &&
15574 to_intel_crtc(encoder->base.crtc)->active;
15575
dd756198 15576 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
24929352
DV
15577 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15578 encoder->base.base.id,
8e329a03 15579 encoder->base.name);
24929352
DV
15580
15581 /* Connector is active, but has no active pipe. This is
15582 * fallout from our resume register restoring. Disable
15583 * the encoder manually again. */
15584 if (encoder->base.crtc) {
15585 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15586 encoder->base.base.id,
8e329a03 15587 encoder->base.name);
24929352 15588 encoder->disable(encoder);
a62d1497
VS
15589 if (encoder->post_disable)
15590 encoder->post_disable(encoder);
24929352 15591 }
7f1950fb 15592 encoder->base.crtc = NULL;
24929352
DV
15593
15594 /* Inconsistent output/port/pipe state happens presumably due to
15595 * a bug in one of the get_hw_state functions. Or someplace else
15596 * in our code, like the register restore mess on resume. Clamp
15597 * things to off as a safer default. */
3a3371ff 15598 for_each_intel_connector(dev, connector) {
24929352
DV
15599 if (connector->encoder != encoder)
15600 continue;
7f1950fb
EE
15601 connector->base.dpms = DRM_MODE_DPMS_OFF;
15602 connector->base.encoder = NULL;
24929352
DV
15603 }
15604 }
15605 /* Enabled encoders without active connectors will be fixed in
15606 * the crtc fixup. */
15607}
15608
04098753 15609void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15610{
15611 struct drm_i915_private *dev_priv = dev->dev_private;
f0f59a00 15612 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15613
04098753
ID
15614 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15615 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15616 i915_disable_vga(dev);
15617 }
15618}
15619
15620void i915_redisable_vga(struct drm_device *dev)
15621{
15622 struct drm_i915_private *dev_priv = dev->dev_private;
15623
8dc8a27c
PZ
15624 /* This function can be called both from intel_modeset_setup_hw_state or
15625 * at a very early point in our resume sequence, where the power well
15626 * structures are not yet restored. Since this function is at a very
15627 * paranoid "someone might have enabled VGA while we were not looking"
15628 * level, just check if the power well is enabled instead of trying to
15629 * follow the "don't touch the power well if we don't need it" policy
15630 * the rest of the driver uses. */
6392f847 15631 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15632 return;
15633
04098753 15634 i915_redisable_vga_power_on(dev);
6392f847
ID
15635
15636 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
15637}
15638
f9cd7b88 15639static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15640{
f9cd7b88 15641 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15642
f9cd7b88 15643 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15644}
15645
f9cd7b88
VS
15646/* FIXME read out full plane state for all planes */
15647static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15648{
b26d3ea3 15649 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15650 struct intel_plane_state *plane_state =
b26d3ea3 15651 to_intel_plane_state(primary->state);
d032ffa0 15652
19b8d387 15653 plane_state->visible = crtc->active &&
b26d3ea3
ML
15654 primary_get_hw_state(to_intel_plane(primary));
15655
15656 if (plane_state->visible)
15657 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15658}
15659
30e984df 15660static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15661{
15662 struct drm_i915_private *dev_priv = dev->dev_private;
15663 enum pipe pipe;
24929352
DV
15664 struct intel_crtc *crtc;
15665 struct intel_encoder *encoder;
15666 struct intel_connector *connector;
5358901f 15667 int i;
24929352 15668
565602d7
ML
15669 dev_priv->active_crtcs = 0;
15670
d3fcc808 15671 for_each_intel_crtc(dev, crtc) {
565602d7
ML
15672 struct intel_crtc_state *crtc_state = crtc->config;
15673 int pixclk = 0;
3b117c8f 15674
565602d7
ML
15675 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15676 memset(crtc_state, 0, sizeof(*crtc_state));
15677 crtc_state->base.crtc = &crtc->base;
24929352 15678
565602d7
ML
15679 crtc_state->base.active = crtc_state->base.enable =
15680 dev_priv->display.get_pipe_config(crtc, crtc_state);
15681
15682 crtc->base.enabled = crtc_state->base.enable;
15683 crtc->active = crtc_state->base.active;
15684
15685 if (crtc_state->base.active) {
15686 dev_priv->active_crtcs |= 1 << crtc->pipe;
15687
15688 if (IS_BROADWELL(dev_priv)) {
15689 pixclk = ilk_pipe_pixel_rate(crtc_state);
15690
15691 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15692 if (crtc_state->ips_enabled)
15693 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15694 } else if (IS_VALLEYVIEW(dev_priv) ||
15695 IS_CHERRYVIEW(dev_priv) ||
15696 IS_BROXTON(dev_priv))
15697 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15698 else
15699 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15700 }
15701
15702 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 15703
f9cd7b88 15704 readout_plane_state(crtc);
24929352
DV
15705
15706 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15707 crtc->base.base.id,
15708 crtc->active ? "enabled" : "disabled");
15709 }
15710
5358901f
DV
15711 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15712 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15713
2edd6443
ACO
15714 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15715 &pll->config.hw_state);
5358901f 15716 pll->active = 0;
3e369b76 15717 pll->config.crtc_mask = 0;
d3fcc808 15718 for_each_intel_crtc(dev, crtc) {
8106ddbd 15719 if (crtc->active && crtc->config->shared_dpll == pll) {
5358901f 15720 pll->active++;
3e369b76 15721 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15722 }
5358901f 15723 }
5358901f 15724
1e6f2ddc 15725 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15726 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15727
3e369b76 15728 if (pll->config.crtc_mask)
bd2bb1b9 15729 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15730 }
15731
b2784e15 15732 for_each_intel_encoder(dev, encoder) {
24929352
DV
15733 pipe = 0;
15734
15735 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15736 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15737 encoder->base.crtc = &crtc->base;
6e3c9717 15738 encoder->get_config(encoder, crtc->config);
24929352
DV
15739 } else {
15740 encoder->base.crtc = NULL;
15741 }
15742
6f2bcceb 15743 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15744 encoder->base.base.id,
8e329a03 15745 encoder->base.name,
24929352 15746 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15747 pipe_name(pipe));
24929352
DV
15748 }
15749
3a3371ff 15750 for_each_intel_connector(dev, connector) {
24929352
DV
15751 if (connector->get_hw_state(connector)) {
15752 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
15753
15754 encoder = connector->encoder;
15755 connector->base.encoder = &encoder->base;
15756
15757 if (encoder->base.crtc &&
15758 encoder->base.crtc->state->active) {
15759 /*
15760 * This has to be done during hardware readout
15761 * because anything calling .crtc_disable may
15762 * rely on the connector_mask being accurate.
15763 */
15764 encoder->base.crtc->state->connector_mask |=
15765 1 << drm_connector_index(&connector->base);
e87a52b3
ML
15766 encoder->base.crtc->state->encoder_mask |=
15767 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
15768 }
15769
24929352
DV
15770 } else {
15771 connector->base.dpms = DRM_MODE_DPMS_OFF;
15772 connector->base.encoder = NULL;
15773 }
15774 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15775 connector->base.base.id,
c23cc417 15776 connector->base.name,
24929352
DV
15777 connector->base.encoder ? "enabled" : "disabled");
15778 }
7f4c6284
VS
15779
15780 for_each_intel_crtc(dev, crtc) {
15781 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15782
15783 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15784 if (crtc->base.state->active) {
15785 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15786 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15787 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15788
15789 /*
15790 * The initial mode needs to be set in order to keep
15791 * the atomic core happy. It wants a valid mode if the
15792 * crtc's enabled, so we do the above call.
15793 *
15794 * At this point some state updated by the connectors
15795 * in their ->detect() callback has not run yet, so
15796 * no recalculation can be done yet.
15797 *
15798 * Even if we could do a recalculation and modeset
15799 * right now it would cause a double modeset if
15800 * fbdev or userspace chooses a different initial mode.
15801 *
15802 * If that happens, someone indicated they wanted a
15803 * mode change, which means it's safe to do a full
15804 * recalculation.
15805 */
15806 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15807
15808 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15809 update_scanline_offset(crtc);
7f4c6284 15810 }
e3b247da
VS
15811
15812 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 15813 }
30e984df
DV
15814}
15815
043e9bda
ML
15816/* Scan out the current hw modeset state,
15817 * and sanitizes it to the current state
15818 */
15819static void
15820intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15821{
15822 struct drm_i915_private *dev_priv = dev->dev_private;
15823 enum pipe pipe;
30e984df
DV
15824 struct intel_crtc *crtc;
15825 struct intel_encoder *encoder;
35c95375 15826 int i;
30e984df
DV
15827
15828 intel_modeset_readout_hw_state(dev);
24929352
DV
15829
15830 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15831 for_each_intel_encoder(dev, encoder) {
24929352
DV
15832 intel_sanitize_encoder(encoder);
15833 }
15834
055e393f 15835 for_each_pipe(dev_priv, pipe) {
24929352
DV
15836 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15837 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15838 intel_dump_pipe_config(crtc, crtc->config,
15839 "[setup_hw_state]");
24929352 15840 }
9a935856 15841
d29b2f9d
ACO
15842 intel_modeset_update_connector_atomic_state(dev);
15843
35c95375
DV
15844 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15845 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15846
15847 if (!pll->on || pll->active)
15848 continue;
15849
15850 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15851
2edd6443 15852 pll->funcs.disable(dev_priv, pll);
35c95375
DV
15853 pll->on = false;
15854 }
15855
666a4537 15856 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
6eb1a681
VS
15857 vlv_wm_get_hw_state(dev);
15858 else if (IS_GEN9(dev))
3078999f
PB
15859 skl_wm_get_hw_state(dev);
15860 else if (HAS_PCH_SPLIT(dev))
243e6a44 15861 ilk_wm_get_hw_state(dev);
292b990e
ML
15862
15863 for_each_intel_crtc(dev, crtc) {
15864 unsigned long put_domains;
15865
74bff5f9 15866 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
15867 if (WARN_ON(put_domains))
15868 modeset_put_power_domains(dev_priv, put_domains);
15869 }
15870 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
15871
15872 intel_fbc_init_pipe_state(dev_priv);
043e9bda 15873}
7d0bc1ea 15874
043e9bda
ML
15875void intel_display_resume(struct drm_device *dev)
15876{
e2c8b870
ML
15877 struct drm_i915_private *dev_priv = to_i915(dev);
15878 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15879 struct drm_modeset_acquire_ctx ctx;
043e9bda 15880 int ret;
e2c8b870 15881 bool setup = false;
f30da187 15882
e2c8b870 15883 dev_priv->modeset_restore_state = NULL;
043e9bda 15884
ea49c9ac
ML
15885 /*
15886 * This is a cludge because with real atomic modeset mode_config.mutex
15887 * won't be taken. Unfortunately some probed state like
15888 * audio_codec_enable is still protected by mode_config.mutex, so lock
15889 * it here for now.
15890 */
15891 mutex_lock(&dev->mode_config.mutex);
e2c8b870 15892 drm_modeset_acquire_init(&ctx, 0);
043e9bda 15893
e2c8b870
ML
15894retry:
15895 ret = drm_modeset_lock_all_ctx(dev, &ctx);
043e9bda 15896
e2c8b870
ML
15897 if (ret == 0 && !setup) {
15898 setup = true;
043e9bda 15899
e2c8b870
ML
15900 intel_modeset_setup_hw_state(dev);
15901 i915_redisable_vga(dev);
45e2b5f6 15902 }
8af6cf88 15903
e2c8b870
ML
15904 if (ret == 0 && state) {
15905 struct drm_crtc_state *crtc_state;
15906 struct drm_crtc *crtc;
15907 int i;
043e9bda 15908
e2c8b870
ML
15909 state->acquire_ctx = &ctx;
15910
15911 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15912 /*
15913 * Force recalculation even if we restore
15914 * current state. With fast modeset this may not result
15915 * in a modeset when the state is compatible.
15916 */
15917 crtc_state->mode_changed = true;
15918 }
15919
15920 ret = drm_atomic_commit(state);
043e9bda
ML
15921 }
15922
e2c8b870
ML
15923 if (ret == -EDEADLK) {
15924 drm_modeset_backoff(&ctx);
15925 goto retry;
15926 }
043e9bda 15927
e2c8b870
ML
15928 drm_modeset_drop_locks(&ctx);
15929 drm_modeset_acquire_fini(&ctx);
ea49c9ac 15930 mutex_unlock(&dev->mode_config.mutex);
043e9bda 15931
e2c8b870
ML
15932 if (ret) {
15933 DRM_ERROR("Restoring old state failed with %i\n", ret);
15934 drm_atomic_state_free(state);
15935 }
2c7111db
CW
15936}
15937
15938void intel_modeset_gem_init(struct drm_device *dev)
15939{
484b41dd 15940 struct drm_crtc *c;
2ff8fde1 15941 struct drm_i915_gem_object *obj;
e0d6149b 15942 int ret;
484b41dd 15943
ae48434c 15944 intel_init_gt_powersave(dev);
ae48434c 15945
1833b134 15946 intel_modeset_init_hw(dev);
02e792fb
DV
15947
15948 intel_setup_overlay(dev);
484b41dd
JB
15949
15950 /*
15951 * Make sure any fbs we allocated at startup are properly
15952 * pinned & fenced. When we do the allocation it's too early
15953 * for this.
15954 */
70e1e0ec 15955 for_each_crtc(dev, c) {
2ff8fde1
MR
15956 obj = intel_fb_obj(c->primary->fb);
15957 if (obj == NULL)
484b41dd
JB
15958 continue;
15959
e0d6149b 15960 mutex_lock(&dev->struct_mutex);
3465c580
VS
15961 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15962 c->primary->state->rotation);
e0d6149b
TU
15963 mutex_unlock(&dev->struct_mutex);
15964 if (ret) {
484b41dd
JB
15965 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15966 to_intel_crtc(c)->pipe);
66e514c1
DA
15967 drm_framebuffer_unreference(c->primary->fb);
15968 c->primary->fb = NULL;
36750f28 15969 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15970 update_state_fb(c->primary);
36750f28 15971 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15972 }
15973 }
0962c3c9
VS
15974
15975 intel_backlight_register(dev);
79e53945
JB
15976}
15977
4932e2c3
ID
15978void intel_connector_unregister(struct intel_connector *intel_connector)
15979{
15980 struct drm_connector *connector = &intel_connector->base;
15981
15982 intel_panel_destroy_backlight(connector);
34ea3d38 15983 drm_connector_unregister(connector);
4932e2c3
ID
15984}
15985
79e53945
JB
15986void intel_modeset_cleanup(struct drm_device *dev)
15987{
652c393a 15988 struct drm_i915_private *dev_priv = dev->dev_private;
19c8054c 15989 struct intel_connector *connector;
652c393a 15990
2eb5252e
ID
15991 intel_disable_gt_powersave(dev);
15992
0962c3c9
VS
15993 intel_backlight_unregister(dev);
15994
fd0c0642
DV
15995 /*
15996 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15997 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15998 * experience fancy races otherwise.
15999 */
2aeb7d3a 16000 intel_irq_uninstall(dev_priv);
eb21b92b 16001
fd0c0642
DV
16002 /*
16003 * Due to the hpd irq storm handling the hotplug work can re-arm the
16004 * poll handlers. Hence disable polling after hpd handling is shut down.
16005 */
f87ea761 16006 drm_kms_helper_poll_fini(dev);
fd0c0642 16007
723bfd70
JB
16008 intel_unregister_dsm_handler();
16009
c937ab3e 16010 intel_fbc_global_disable(dev_priv);
69341a5e 16011
1630fe75
CW
16012 /* flush any delayed tasks or pending work */
16013 flush_scheduled_work();
16014
db31af1d 16015 /* destroy the backlight and sysfs files before encoders/connectors */
19c8054c
JN
16016 for_each_intel_connector(dev, connector)
16017 connector->unregister(connector);
d9255d57 16018
79e53945 16019 drm_mode_config_cleanup(dev);
4d7bb011
DV
16020
16021 intel_cleanup_overlay(dev);
ae48434c 16022
ae48434c 16023 intel_cleanup_gt_powersave(dev);
f5949141
DV
16024
16025 intel_teardown_gmbus(dev);
79e53945
JB
16026}
16027
f1c79df3
ZW
16028/*
16029 * Return which encoder is currently attached for connector.
16030 */
df0e9248 16031struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 16032{
df0e9248
CW
16033 return &intel_attached_encoder(connector)->base;
16034}
f1c79df3 16035
df0e9248
CW
16036void intel_connector_attach_encoder(struct intel_connector *connector,
16037 struct intel_encoder *encoder)
16038{
16039 connector->encoder = encoder;
16040 drm_mode_connector_attach_encoder(&connector->base,
16041 &encoder->base);
79e53945 16042}
28d52043
DA
16043
16044/*
16045 * set vga decode state - true == enable VGA decode
16046 */
16047int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16048{
16049 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 16050 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
16051 u16 gmch_ctrl;
16052
75fa041d
CW
16053 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16054 DRM_ERROR("failed to read control word\n");
16055 return -EIO;
16056 }
16057
c0cc8a55
CW
16058 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16059 return 0;
16060
28d52043
DA
16061 if (state)
16062 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16063 else
16064 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
16065
16066 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16067 DRM_ERROR("failed to write control word\n");
16068 return -EIO;
16069 }
16070
28d52043
DA
16071 return 0;
16072}
c4a1d9e4 16073
c4a1d9e4 16074struct intel_display_error_state {
ff57f1b0
PZ
16075
16076 u32 power_well_driver;
16077
63b66e5b
CW
16078 int num_transcoders;
16079
c4a1d9e4
CW
16080 struct intel_cursor_error_state {
16081 u32 control;
16082 u32 position;
16083 u32 base;
16084 u32 size;
52331309 16085 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
16086
16087 struct intel_pipe_error_state {
ddf9c536 16088 bool power_domain_on;
c4a1d9e4 16089 u32 source;
f301b1e1 16090 u32 stat;
52331309 16091 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
16092
16093 struct intel_plane_error_state {
16094 u32 control;
16095 u32 stride;
16096 u32 size;
16097 u32 pos;
16098 u32 addr;
16099 u32 surface;
16100 u32 tile_offset;
52331309 16101 } plane[I915_MAX_PIPES];
63b66e5b
CW
16102
16103 struct intel_transcoder_error_state {
ddf9c536 16104 bool power_domain_on;
63b66e5b
CW
16105 enum transcoder cpu_transcoder;
16106
16107 u32 conf;
16108
16109 u32 htotal;
16110 u32 hblank;
16111 u32 hsync;
16112 u32 vtotal;
16113 u32 vblank;
16114 u32 vsync;
16115 } transcoder[4];
c4a1d9e4
CW
16116};
16117
16118struct intel_display_error_state *
16119intel_display_capture_error_state(struct drm_device *dev)
16120{
fbee40df 16121 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 16122 struct intel_display_error_state *error;
63b66e5b
CW
16123 int transcoders[] = {
16124 TRANSCODER_A,
16125 TRANSCODER_B,
16126 TRANSCODER_C,
16127 TRANSCODER_EDP,
16128 };
c4a1d9e4
CW
16129 int i;
16130
63b66e5b
CW
16131 if (INTEL_INFO(dev)->num_pipes == 0)
16132 return NULL;
16133
9d1cb914 16134 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
16135 if (error == NULL)
16136 return NULL;
16137
190be112 16138 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
16139 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16140
055e393f 16141 for_each_pipe(dev_priv, i) {
ddf9c536 16142 error->pipe[i].power_domain_on =
f458ebbc
DV
16143 __intel_display_power_is_enabled(dev_priv,
16144 POWER_DOMAIN_PIPE(i));
ddf9c536 16145 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
16146 continue;
16147
5efb3e28
VS
16148 error->cursor[i].control = I915_READ(CURCNTR(i));
16149 error->cursor[i].position = I915_READ(CURPOS(i));
16150 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
16151
16152 error->plane[i].control = I915_READ(DSPCNTR(i));
16153 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 16154 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 16155 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
16156 error->plane[i].pos = I915_READ(DSPPOS(i));
16157 }
ca291363
PZ
16158 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16159 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
16160 if (INTEL_INFO(dev)->gen >= 4) {
16161 error->plane[i].surface = I915_READ(DSPSURF(i));
16162 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16163 }
16164
c4a1d9e4 16165 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 16166
3abfce77 16167 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 16168 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
16169 }
16170
16171 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16172 if (HAS_DDI(dev_priv->dev))
16173 error->num_transcoders++; /* Account for eDP. */
16174
16175 for (i = 0; i < error->num_transcoders; i++) {
16176 enum transcoder cpu_transcoder = transcoders[i];
16177
ddf9c536 16178 error->transcoder[i].power_domain_on =
f458ebbc 16179 __intel_display_power_is_enabled(dev_priv,
38cc1daf 16180 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 16181 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
16182 continue;
16183
63b66e5b
CW
16184 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16185
16186 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16187 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16188 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16189 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16190 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16191 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16192 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
16193 }
16194
16195 return error;
16196}
16197
edc3d884
MK
16198#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16199
c4a1d9e4 16200void
edc3d884 16201intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
16202 struct drm_device *dev,
16203 struct intel_display_error_state *error)
16204{
055e393f 16205 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
16206 int i;
16207
63b66e5b
CW
16208 if (!error)
16209 return;
16210
edc3d884 16211 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 16212 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 16213 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 16214 error->power_well_driver);
055e393f 16215 for_each_pipe(dev_priv, i) {
edc3d884 16216 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 16217 err_printf(m, " Power: %s\n",
87ad3212 16218 onoff(error->pipe[i].power_domain_on));
edc3d884 16219 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 16220 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
16221
16222 err_printf(m, "Plane [%d]:\n", i);
16223 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16224 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 16225 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
16226 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16227 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 16228 }
4b71a570 16229 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 16230 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 16231 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
16232 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16233 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
16234 }
16235
edc3d884
MK
16236 err_printf(m, "Cursor [%d]:\n", i);
16237 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16238 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16239 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 16240 }
63b66e5b
CW
16241
16242 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 16243 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 16244 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 16245 err_printf(m, " Power: %s\n",
87ad3212 16246 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
16247 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16248 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16249 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16250 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16251 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16252 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16253 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16254 }
c4a1d9e4 16255}