drm/i915: s/PCH_DP_/PORT_/ in intel_trans_dp_port_sel() and move it next to its only...
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <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>
79e53945 47
465c120c 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5 53 DRM_FORMAT_XRGB8888,
465c120c
MR
54};
55
56/* Primary plane formats for gen >= 4 */
568db4f2 57static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
61 DRM_FORMAT_XBGR8888,
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
465c120c 70 DRM_FORMAT_XBGR8888,
67fe7dc5 71 DRM_FORMAT_ARGB8888,
465c120c
MR
72 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
465c120c 74 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
75 DRM_FORMAT_YUYV,
76 DRM_FORMAT_YVYU,
77 DRM_FORMAT_UYVY,
78 DRM_FORMAT_VYUY,
465c120c
MR
79};
80
3d7d6510
MR
81/* Cursor formats */
82static const uint32_t intel_cursor_formats[] = {
83 DRM_FORMAT_ARGB8888,
84};
85
6b383a7f 86static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 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);
e7457a9a 119
79e53945 120typedef struct {
0206e353 121 int min, max;
79e53945
JB
122} intel_range_t;
123
124typedef struct {
0206e353
AJ
125 int dot_limit;
126 int p2_slow, p2_fast;
79e53945
JB
127} intel_p2_t;
128
d4906093
ML
129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
0206e353
AJ
131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
d4906093 133};
79e53945 134
bfa7df01
VS
135/* returns HPLL frequency in kHz */
136static int valleyview_get_vco(struct drm_i915_private *dev_priv)
137{
138 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
139
140 /* Obtain SKU information */
141 mutex_lock(&dev_priv->sb_lock);
142 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
143 CCK_FUSE_HPLL_FREQ_MASK;
144 mutex_unlock(&dev_priv->sb_lock);
145
146 return vco_freq[hpll_freq] * 1000;
147}
148
149static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
150 const char *name, u32 reg)
151{
152 u32 val;
153 int divider;
154
155 if (dev_priv->hpll_freq == 0)
156 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
157
158 mutex_lock(&dev_priv->sb_lock);
159 val = vlv_cck_read(dev_priv, reg);
160 mutex_unlock(&dev_priv->sb_lock);
161
162 divider = val & CCK_FREQUENCY_VALUES;
163
164 WARN((val & CCK_FREQUENCY_STATUS) !=
165 (divider << CCK_FREQUENCY_STATUS_SHIFT),
166 "%s change in progress\n", name);
167
168 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
169}
170
d2acd215
DV
171int
172intel_pch_rawclk(struct drm_device *dev)
173{
174 struct drm_i915_private *dev_priv = dev->dev_private;
175
176 WARN_ON(!HAS_PCH_SPLIT(dev));
177
178 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
179}
180
79e50a4f
JN
181/* hrawclock is 1/4 the FSB frequency */
182int intel_hrawclk(struct drm_device *dev)
183{
184 struct drm_i915_private *dev_priv = dev->dev_private;
185 uint32_t clkcfg;
186
187 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
188 if (IS_VALLEYVIEW(dev))
189 return 200;
190
191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
194 return 100;
195 case CLKCFG_FSB_533:
196 return 133;
197 case CLKCFG_FSB_667:
198 return 166;
199 case CLKCFG_FSB_800:
200 return 200;
201 case CLKCFG_FSB_1067:
202 return 266;
203 case CLKCFG_FSB_1333:
204 return 333;
205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
208 return 400;
209 default:
210 return 133;
211 }
212}
213
bfa7df01
VS
214static void intel_update_czclk(struct drm_i915_private *dev_priv)
215{
216 if (!IS_VALLEYVIEW(dev_priv))
217 return;
218
219 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
220 CCK_CZ_CLOCK_CONTROL);
221
222 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
223}
224
021357ac
CW
225static inline u32 /* units of 100MHz */
226intel_fdi_link_freq(struct drm_device *dev)
227{
8b99e68c
CW
228 if (IS_GEN5(dev)) {
229 struct drm_i915_private *dev_priv = dev->dev_private;
230 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
231 } else
232 return 27;
021357ac
CW
233}
234
5d536e28 235static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 236 .dot = { .min = 25000, .max = 350000 },
9c333719 237 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 238 .n = { .min = 2, .max = 16 },
0206e353
AJ
239 .m = { .min = 96, .max = 140 },
240 .m1 = { .min = 18, .max = 26 },
241 .m2 = { .min = 6, .max = 16 },
242 .p = { .min = 4, .max = 128 },
243 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
246};
247
5d536e28
DV
248static const intel_limit_t intel_limits_i8xx_dvo = {
249 .dot = { .min = 25000, .max = 350000 },
9c333719 250 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 251 .n = { .min = 2, .max = 16 },
5d536e28
DV
252 .m = { .min = 96, .max = 140 },
253 .m1 = { .min = 18, .max = 26 },
254 .m2 = { .min = 6, .max = 16 },
255 .p = { .min = 4, .max = 128 },
256 .p1 = { .min = 2, .max = 33 },
257 .p2 = { .dot_limit = 165000,
258 .p2_slow = 4, .p2_fast = 4 },
259};
260
e4b36699 261static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 262 .dot = { .min = 25000, .max = 350000 },
9c333719 263 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 264 .n = { .min = 2, .max = 16 },
0206e353
AJ
265 .m = { .min = 96, .max = 140 },
266 .m1 = { .min = 18, .max = 26 },
267 .m2 = { .min = 6, .max = 16 },
268 .p = { .min = 4, .max = 128 },
269 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
270 .p2 = { .dot_limit = 165000,
271 .p2_slow = 14, .p2_fast = 7 },
e4b36699 272};
273e27ca 273
e4b36699 274static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
275 .dot = { .min = 20000, .max = 400000 },
276 .vco = { .min = 1400000, .max = 2800000 },
277 .n = { .min = 1, .max = 6 },
278 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
279 .m1 = { .min = 8, .max = 18 },
280 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
283 .p2 = { .dot_limit = 200000,
284 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
285};
286
287static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
288 .dot = { .min = 20000, .max = 400000 },
289 .vco = { .min = 1400000, .max = 2800000 },
290 .n = { .min = 1, .max = 6 },
291 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
292 .m1 = { .min = 8, .max = 18 },
293 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
294 .p = { .min = 7, .max = 98 },
295 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
296 .p2 = { .dot_limit = 112000,
297 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
298};
299
273e27ca 300
e4b36699 301static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
302 .dot = { .min = 25000, .max = 270000 },
303 .vco = { .min = 1750000, .max = 3500000},
304 .n = { .min = 1, .max = 4 },
305 .m = { .min = 104, .max = 138 },
306 .m1 = { .min = 17, .max = 23 },
307 .m2 = { .min = 5, .max = 11 },
308 .p = { .min = 10, .max = 30 },
309 .p1 = { .min = 1, .max = 3},
310 .p2 = { .dot_limit = 270000,
311 .p2_slow = 10,
312 .p2_fast = 10
044c7c41 313 },
e4b36699
KP
314};
315
316static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
317 .dot = { .min = 22000, .max = 400000 },
318 .vco = { .min = 1750000, .max = 3500000},
319 .n = { .min = 1, .max = 4 },
320 .m = { .min = 104, .max = 138 },
321 .m1 = { .min = 16, .max = 23 },
322 .m2 = { .min = 5, .max = 11 },
323 .p = { .min = 5, .max = 80 },
324 .p1 = { .min = 1, .max = 8},
325 .p2 = { .dot_limit = 165000,
326 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
327};
328
329static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
330 .dot = { .min = 20000, .max = 115000 },
331 .vco = { .min = 1750000, .max = 3500000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 28, .max = 112 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 0,
339 .p2_slow = 14, .p2_fast = 14
044c7c41 340 },
e4b36699
KP
341};
342
343static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
344 .dot = { .min = 80000, .max = 224000 },
345 .vco = { .min = 1750000, .max = 3500000 },
346 .n = { .min = 1, .max = 3 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 17, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 14, .max = 42 },
351 .p1 = { .min = 2, .max = 6 },
352 .p2 = { .dot_limit = 0,
353 .p2_slow = 7, .p2_fast = 7
044c7c41 354 },
e4b36699
KP
355};
356
f2b115e6 357static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
358 .dot = { .min = 20000, .max = 400000},
359 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 360 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
361 .n = { .min = 3, .max = 6 },
362 .m = { .min = 2, .max = 256 },
273e27ca 363 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
364 .m1 = { .min = 0, .max = 0 },
365 .m2 = { .min = 0, .max = 254 },
366 .p = { .min = 5, .max = 80 },
367 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
368 .p2 = { .dot_limit = 200000,
369 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
370};
371
f2b115e6 372static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
373 .dot = { .min = 20000, .max = 400000 },
374 .vco = { .min = 1700000, .max = 3500000 },
375 .n = { .min = 3, .max = 6 },
376 .m = { .min = 2, .max = 256 },
377 .m1 = { .min = 0, .max = 0 },
378 .m2 = { .min = 0, .max = 254 },
379 .p = { .min = 7, .max = 112 },
380 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
381 .p2 = { .dot_limit = 112000,
382 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
383};
384
273e27ca
EA
385/* Ironlake / Sandybridge
386 *
387 * We calculate clock using (register_value + 2) for N/M1/M2, so here
388 * the range value for them is (actual_value - 2).
389 */
b91ad0ec 390static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
391 .dot = { .min = 25000, .max = 350000 },
392 .vco = { .min = 1760000, .max = 3510000 },
393 .n = { .min = 1, .max = 5 },
394 .m = { .min = 79, .max = 127 },
395 .m1 = { .min = 12, .max = 22 },
396 .m2 = { .min = 5, .max = 9 },
397 .p = { .min = 5, .max = 80 },
398 .p1 = { .min = 1, .max = 8 },
399 .p2 = { .dot_limit = 225000,
400 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
401};
402
b91ad0ec 403static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
404 .dot = { .min = 25000, .max = 350000 },
405 .vco = { .min = 1760000, .max = 3510000 },
406 .n = { .min = 1, .max = 3 },
407 .m = { .min = 79, .max = 118 },
408 .m1 = { .min = 12, .max = 22 },
409 .m2 = { .min = 5, .max = 9 },
410 .p = { .min = 28, .max = 112 },
411 .p1 = { .min = 2, .max = 8 },
412 .p2 = { .dot_limit = 225000,
413 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
414};
415
416static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
417 .dot = { .min = 25000, .max = 350000 },
418 .vco = { .min = 1760000, .max = 3510000 },
419 .n = { .min = 1, .max = 3 },
420 .m = { .min = 79, .max = 127 },
421 .m1 = { .min = 12, .max = 22 },
422 .m2 = { .min = 5, .max = 9 },
423 .p = { .min = 14, .max = 56 },
424 .p1 = { .min = 2, .max = 8 },
425 .p2 = { .dot_limit = 225000,
426 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
427};
428
273e27ca 429/* LVDS 100mhz refclk limits. */
b91ad0ec 430static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 2 },
434 .m = { .min = 79, .max = 126 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
0206e353 438 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
441};
442
443static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 126 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 42 },
0206e353 451 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
454};
455
dc730512 456static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
457 /*
458 * These are the data rate limits (measured in fast clocks)
459 * since those are the strictest limits we have. The fast
460 * clock and actual rate limits are more relaxed, so checking
461 * them would make no difference.
462 */
463 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 464 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 465 .n = { .min = 1, .max = 7 },
a0c4da24
JB
466 .m1 = { .min = 2, .max = 3 },
467 .m2 = { .min = 11, .max = 156 },
b99ab663 468 .p1 = { .min = 2, .max = 3 },
5fdc9c49 469 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
470};
471
ef9348c8
CML
472static const intel_limit_t intel_limits_chv = {
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 = 540000 * 5},
17fe1021 480 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
481 .n = { .min = 1, .max = 1 },
482 .m1 = { .min = 2, .max = 2 },
483 .m2 = { .min = 24 << 22, .max = 175 << 22 },
484 .p1 = { .min = 2, .max = 4 },
485 .p2 = { .p2_slow = 1, .p2_fast = 14 },
486};
487
5ab7b0b7
ID
488static const intel_limit_t intel_limits_bxt = {
489 /* FIXME: find real dot limits */
490 .dot = { .min = 0, .max = INT_MAX },
e6292556 491 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
492 .n = { .min = 1, .max = 1 },
493 .m1 = { .min = 2, .max = 2 },
494 /* FIXME: find real m2 limits */
495 .m2 = { .min = 2 << 22, .max = 255 << 22 },
496 .p1 = { .min = 2, .max = 4 },
497 .p2 = { .p2_slow = 1, .p2_fast = 20 },
498};
499
cdba954e
ACO
500static bool
501needs_modeset(struct drm_crtc_state *state)
502{
fc596660 503 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
504}
505
e0638cdf
PZ
506/**
507 * Returns whether any output on the specified pipe is of the specified type
508 */
4093561b 509bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 510{
409ee761 511 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
512 struct intel_encoder *encoder;
513
409ee761 514 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
515 if (encoder->type == type)
516 return true;
517
518 return false;
519}
520
d0737e1d
ACO
521/**
522 * Returns whether any output on the specified pipe will have the specified
523 * type after a staged modeset is complete, i.e., the same as
524 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
525 * encoder->crtc.
526 */
a93e255f
ACO
527static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
528 int type)
d0737e1d 529{
a93e255f 530 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 531 struct drm_connector *connector;
a93e255f 532 struct drm_connector_state *connector_state;
d0737e1d 533 struct intel_encoder *encoder;
a93e255f
ACO
534 int i, num_connectors = 0;
535
da3ced29 536 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
537 if (connector_state->crtc != crtc_state->base.crtc)
538 continue;
539
540 num_connectors++;
d0737e1d 541
a93e255f
ACO
542 encoder = to_intel_encoder(connector_state->best_encoder);
543 if (encoder->type == type)
d0737e1d 544 return true;
a93e255f
ACO
545 }
546
547 WARN_ON(num_connectors == 0);
d0737e1d
ACO
548
549 return false;
550}
551
a93e255f
ACO
552static const intel_limit_t *
553intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 554{
a93e255f 555 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 556 const intel_limit_t *limit;
b91ad0ec 557
a93e255f 558 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 559 if (intel_is_dual_link_lvds(dev)) {
1b894b59 560 if (refclk == 100000)
b91ad0ec
ZW
561 limit = &intel_limits_ironlake_dual_lvds_100m;
562 else
563 limit = &intel_limits_ironlake_dual_lvds;
564 } else {
1b894b59 565 if (refclk == 100000)
b91ad0ec
ZW
566 limit = &intel_limits_ironlake_single_lvds_100m;
567 else
568 limit = &intel_limits_ironlake_single_lvds;
569 }
c6bb3538 570 } else
b91ad0ec 571 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
572
573 return limit;
574}
575
a93e255f
ACO
576static const intel_limit_t *
577intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 578{
a93e255f 579 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
580 const intel_limit_t *limit;
581
a93e255f 582 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 583 if (intel_is_dual_link_lvds(dev))
e4b36699 584 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 585 else
e4b36699 586 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
587 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
588 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 589 limit = &intel_limits_g4x_hdmi;
a93e255f 590 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 591 limit = &intel_limits_g4x_sdvo;
044c7c41 592 } else /* The option is for other outputs */
e4b36699 593 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
594
595 return limit;
596}
597
a93e255f
ACO
598static const intel_limit_t *
599intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 600{
a93e255f 601 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
602 const intel_limit_t *limit;
603
5ab7b0b7
ID
604 if (IS_BROXTON(dev))
605 limit = &intel_limits_bxt;
606 else if (HAS_PCH_SPLIT(dev))
a93e255f 607 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 608 else if (IS_G4X(dev)) {
a93e255f 609 limit = intel_g4x_limit(crtc_state);
f2b115e6 610 } else if (IS_PINEVIEW(dev)) {
a93e255f 611 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 612 limit = &intel_limits_pineview_lvds;
2177832f 613 else
f2b115e6 614 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
615 } else if (IS_CHERRYVIEW(dev)) {
616 limit = &intel_limits_chv;
a0c4da24 617 } else if (IS_VALLEYVIEW(dev)) {
dc730512 618 limit = &intel_limits_vlv;
a6c45cf0 619 } else if (!IS_GEN2(dev)) {
a93e255f 620 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
621 limit = &intel_limits_i9xx_lvds;
622 else
623 limit = &intel_limits_i9xx_sdvo;
79e53945 624 } else {
a93e255f 625 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 626 limit = &intel_limits_i8xx_lvds;
a93e255f 627 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 628 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
629 else
630 limit = &intel_limits_i8xx_dac;
79e53945
JB
631 }
632 return limit;
633}
634
dccbea3b
ID
635/*
636 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
637 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
638 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
639 * The helpers' return value is the rate of the clock that is fed to the
640 * display engine's pipe which can be the above fast dot clock rate or a
641 * divided-down version of it.
642 */
f2b115e6 643/* m1 is reserved as 0 in Pineview, n is a ring counter */
dccbea3b 644static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
79e53945 645{
2177832f
SL
646 clock->m = clock->m2 + 2;
647 clock->p = clock->p1 * clock->p2;
ed5ca77e 648 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 649 return 0;
fb03ac01
VS
650 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
651 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
652
653 return clock->dot;
2177832f
SL
654}
655
7429e9d4
DV
656static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
657{
658 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
659}
660
dccbea3b 661static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
2177832f 662{
7429e9d4 663 clock->m = i9xx_dpll_compute_m(clock);
79e53945 664 clock->p = clock->p1 * clock->p2;
ed5ca77e 665 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 666 return 0;
fb03ac01
VS
667 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
668 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
669
670 return clock->dot;
79e53945
JB
671}
672
dccbea3b 673static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
589eca67
ID
674{
675 clock->m = clock->m1 * clock->m2;
676 clock->p = clock->p1 * clock->p2;
677 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 678 return 0;
589eca67
ID
679 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
680 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
681
682 return clock->dot / 5;
589eca67
ID
683}
684
dccbea3b 685int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
ef9348c8
CML
686{
687 clock->m = clock->m1 * clock->m2;
688 clock->p = clock->p1 * clock->p2;
689 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 690 return 0;
ef9348c8
CML
691 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
692 clock->n << 22);
693 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
694
695 return clock->dot / 5;
ef9348c8
CML
696}
697
7c04d1d9 698#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
699/**
700 * Returns whether the given set of divisors are valid for a given refclk with
701 * the given connectors.
702 */
703
1b894b59
CW
704static bool intel_PLL_is_valid(struct drm_device *dev,
705 const intel_limit_t *limit,
706 const intel_clock_t *clock)
79e53945 707{
f01b7962
VS
708 if (clock->n < limit->n.min || limit->n.max < clock->n)
709 INTELPllInvalid("n out of range\n");
79e53945 710 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 711 INTELPllInvalid("p1 out of range\n");
79e53945 712 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 713 INTELPllInvalid("m2 out of range\n");
79e53945 714 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 715 INTELPllInvalid("m1 out of range\n");
f01b7962 716
5ab7b0b7 717 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
718 if (clock->m1 <= clock->m2)
719 INTELPllInvalid("m1 <= m2\n");
720
5ab7b0b7 721 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
722 if (clock->p < limit->p.min || limit->p.max < clock->p)
723 INTELPllInvalid("p out of range\n");
724 if (clock->m < limit->m.min || limit->m.max < clock->m)
725 INTELPllInvalid("m out of range\n");
726 }
727
79e53945 728 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 729 INTELPllInvalid("vco out of range\n");
79e53945
JB
730 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
731 * connector, etc., rather than just a single range.
732 */
733 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 734 INTELPllInvalid("dot out of range\n");
79e53945
JB
735
736 return true;
737}
738
3b1429d9
VS
739static int
740i9xx_select_p2_div(const intel_limit_t *limit,
741 const struct intel_crtc_state *crtc_state,
742 int target)
79e53945 743{
3b1429d9 744 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 745
a93e255f 746 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 747 /*
a210b028
DV
748 * For LVDS just rely on its current settings for dual-channel.
749 * We haven't figured out how to reliably set up different
750 * single/dual channel state, if we even can.
79e53945 751 */
1974cad0 752 if (intel_is_dual_link_lvds(dev))
3b1429d9 753 return limit->p2.p2_fast;
79e53945 754 else
3b1429d9 755 return limit->p2.p2_slow;
79e53945
JB
756 } else {
757 if (target < limit->p2.dot_limit)
3b1429d9 758 return limit->p2.p2_slow;
79e53945 759 else
3b1429d9 760 return limit->p2.p2_fast;
79e53945 761 }
3b1429d9
VS
762}
763
764static bool
765i9xx_find_best_dpll(const intel_limit_t *limit,
766 struct intel_crtc_state *crtc_state,
767 int target, int refclk, intel_clock_t *match_clock,
768 intel_clock_t *best_clock)
769{
770 struct drm_device *dev = crtc_state->base.crtc->dev;
771 intel_clock_t clock;
772 int err = target;
79e53945 773
0206e353 774 memset(best_clock, 0, sizeof(*best_clock));
79e53945 775
3b1429d9
VS
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
42158660
ZY
778 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
779 clock.m1++) {
780 for (clock.m2 = limit->m2.min;
781 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 782 if (clock.m2 >= clock.m1)
42158660
ZY
783 break;
784 for (clock.n = limit->n.min;
785 clock.n <= limit->n.max; clock.n++) {
786 for (clock.p1 = limit->p1.min;
787 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
788 int this_err;
789
dccbea3b 790 i9xx_calc_dpll_params(refclk, &clock);
ac58c3f0
DV
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
793 continue;
794 if (match_clock &&
795 clock.p != match_clock->p)
796 continue;
797
798 this_err = abs(clock.dot - target);
799 if (this_err < err) {
800 *best_clock = clock;
801 err = this_err;
802 }
803 }
804 }
805 }
806 }
807
808 return (err != target);
809}
810
811static bool
a93e255f
ACO
812pnv_find_best_dpll(const intel_limit_t *limit,
813 struct intel_crtc_state *crtc_state,
ee9300bb
DV
814 int target, int refclk, intel_clock_t *match_clock,
815 intel_clock_t *best_clock)
79e53945 816{
3b1429d9 817 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 818 intel_clock_t clock;
79e53945
JB
819 int err = target;
820
0206e353 821 memset(best_clock, 0, sizeof(*best_clock));
79e53945 822
3b1429d9
VS
823 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
824
42158660
ZY
825 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
826 clock.m1++) {
827 for (clock.m2 = limit->m2.min;
828 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
829 for (clock.n = limit->n.min;
830 clock.n <= limit->n.max; clock.n++) {
831 for (clock.p1 = limit->p1.min;
832 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
833 int this_err;
834
dccbea3b 835 pnv_calc_dpll_params(refclk, &clock);
1b894b59
CW
836 if (!intel_PLL_is_valid(dev, limit,
837 &clock))
79e53945 838 continue;
cec2f356
SP
839 if (match_clock &&
840 clock.p != match_clock->p)
841 continue;
79e53945
JB
842
843 this_err = abs(clock.dot - target);
844 if (this_err < err) {
845 *best_clock = clock;
846 err = this_err;
847 }
848 }
849 }
850 }
851 }
852
853 return (err != target);
854}
855
d4906093 856static bool
a93e255f
ACO
857g4x_find_best_dpll(const intel_limit_t *limit,
858 struct intel_crtc_state *crtc_state,
ee9300bb
DV
859 int target, int refclk, intel_clock_t *match_clock,
860 intel_clock_t *best_clock)
d4906093 861{
3b1429d9 862 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
863 intel_clock_t clock;
864 int max_n;
3b1429d9 865 bool found = false;
6ba770dc
AJ
866 /* approximately equals target * 0.00585 */
867 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
868
869 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
870
871 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
872
d4906093 873 max_n = limit->n.max;
f77f13e2 874 /* based on hardware requirement, prefer smaller n to precision */
d4906093 875 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 876 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
877 for (clock.m1 = limit->m1.max;
878 clock.m1 >= limit->m1.min; clock.m1--) {
879 for (clock.m2 = limit->m2.max;
880 clock.m2 >= limit->m2.min; clock.m2--) {
881 for (clock.p1 = limit->p1.max;
882 clock.p1 >= limit->p1.min; clock.p1--) {
883 int this_err;
884
dccbea3b 885 i9xx_calc_dpll_params(refclk, &clock);
1b894b59
CW
886 if (!intel_PLL_is_valid(dev, limit,
887 &clock))
d4906093 888 continue;
1b894b59
CW
889
890 this_err = abs(clock.dot - target);
d4906093
ML
891 if (this_err < err_most) {
892 *best_clock = clock;
893 err_most = this_err;
894 max_n = clock.n;
895 found = true;
896 }
897 }
898 }
899 }
900 }
2c07245f
ZW
901 return found;
902}
903
d5dd62bd
ID
904/*
905 * Check if the calculated PLL configuration is more optimal compared to the
906 * best configuration and error found so far. Return the calculated error.
907 */
908static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
909 const intel_clock_t *calculated_clock,
910 const intel_clock_t *best_clock,
911 unsigned int best_error_ppm,
912 unsigned int *error_ppm)
913{
9ca3ba01
ID
914 /*
915 * For CHV ignore the error and consider only the P value.
916 * Prefer a bigger P value based on HW requirements.
917 */
918 if (IS_CHERRYVIEW(dev)) {
919 *error_ppm = 0;
920
921 return calculated_clock->p > best_clock->p;
922 }
923
24be4e46
ID
924 if (WARN_ON_ONCE(!target_freq))
925 return false;
926
d5dd62bd
ID
927 *error_ppm = div_u64(1000000ULL *
928 abs(target_freq - calculated_clock->dot),
929 target_freq);
930 /*
931 * Prefer a better P value over a better (smaller) error if the error
932 * is small. Ensure this preference for future configurations too by
933 * setting the error to 0.
934 */
935 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
936 *error_ppm = 0;
937
938 return true;
939 }
940
941 return *error_ppm + 10 < best_error_ppm;
942}
943
a0c4da24 944static bool
a93e255f
ACO
945vlv_find_best_dpll(const intel_limit_t *limit,
946 struct intel_crtc_state *crtc_state,
ee9300bb
DV
947 int target, int refclk, intel_clock_t *match_clock,
948 intel_clock_t *best_clock)
a0c4da24 949{
a93e255f 950 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 951 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 952 intel_clock_t clock;
69e4f900 953 unsigned int bestppm = 1000000;
27e639bf
VS
954 /* min update 19.2 MHz */
955 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 956 bool found = false;
a0c4da24 957
6b4bf1c4
VS
958 target *= 5; /* fast clock */
959
960 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
961
962 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 963 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 964 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 965 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 966 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 967 clock.p = clock.p1 * clock.p2;
a0c4da24 968 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 969 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 970 unsigned int ppm;
69e4f900 971
6b4bf1c4
VS
972 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
973 refclk * clock.m1);
974
dccbea3b 975 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 976
f01b7962
VS
977 if (!intel_PLL_is_valid(dev, limit,
978 &clock))
43b0ac53
VS
979 continue;
980
d5dd62bd
ID
981 if (!vlv_PLL_is_optimal(dev, target,
982 &clock,
983 best_clock,
984 bestppm, &ppm))
985 continue;
6b4bf1c4 986
d5dd62bd
ID
987 *best_clock = clock;
988 bestppm = ppm;
989 found = true;
a0c4da24
JB
990 }
991 }
992 }
993 }
a0c4da24 994
49e497ef 995 return found;
a0c4da24 996}
a4fc5ed6 997
ef9348c8 998static bool
a93e255f
ACO
999chv_find_best_dpll(const intel_limit_t *limit,
1000 struct intel_crtc_state *crtc_state,
ef9348c8
CML
1001 int target, int refclk, intel_clock_t *match_clock,
1002 intel_clock_t *best_clock)
1003{
a93e255f 1004 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 1005 struct drm_device *dev = crtc->base.dev;
9ca3ba01 1006 unsigned int best_error_ppm;
ef9348c8
CML
1007 intel_clock_t clock;
1008 uint64_t m2;
1009 int found = false;
1010
1011 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 1012 best_error_ppm = 1000000;
ef9348c8
CML
1013
1014 /*
1015 * Based on hardware doc, the n always set to 1, and m1 always
1016 * set to 2. If requires to support 200Mhz refclk, we need to
1017 * revisit this because n may not 1 anymore.
1018 */
1019 clock.n = 1, clock.m1 = 2;
1020 target *= 5; /* fast clock */
1021
1022 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1023 for (clock.p2 = limit->p2.p2_fast;
1024 clock.p2 >= limit->p2.p2_slow;
1025 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 1026 unsigned int error_ppm;
ef9348c8
CML
1027
1028 clock.p = clock.p1 * clock.p2;
1029
1030 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1031 clock.n) << 22, refclk * clock.m1);
1032
1033 if (m2 > INT_MAX/clock.m1)
1034 continue;
1035
1036 clock.m2 = m2;
1037
dccbea3b 1038 chv_calc_dpll_params(refclk, &clock);
ef9348c8
CML
1039
1040 if (!intel_PLL_is_valid(dev, limit, &clock))
1041 continue;
1042
9ca3ba01
ID
1043 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1044 best_error_ppm, &error_ppm))
1045 continue;
1046
1047 *best_clock = clock;
1048 best_error_ppm = error_ppm;
1049 found = true;
ef9348c8
CML
1050 }
1051 }
1052
1053 return found;
1054}
1055
5ab7b0b7
ID
1056bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1057 intel_clock_t *best_clock)
1058{
1059 int refclk = i9xx_get_refclk(crtc_state, 0);
1060
1061 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1062 target_clock, refclk, NULL, best_clock);
1063}
1064
20ddf665
VS
1065bool intel_crtc_active(struct drm_crtc *crtc)
1066{
1067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1068
1069 /* Be paranoid as we can arrive here with only partial
1070 * state retrieved from the hardware during setup.
1071 *
241bfc38 1072 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1073 * as Haswell has gained clock readout/fastboot support.
1074 *
66e514c1 1075 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1076 * properly reconstruct framebuffers.
c3d1f436
MR
1077 *
1078 * FIXME: The intel_crtc->active here should be switched to
1079 * crtc->state->active once we have proper CRTC states wired up
1080 * for atomic.
20ddf665 1081 */
c3d1f436 1082 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1083 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1084}
1085
a5c961d1
PZ
1086enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1087 enum pipe pipe)
1088{
1089 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1091
6e3c9717 1092 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1093}
1094
fbf49ea2
VS
1095static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1096{
1097 struct drm_i915_private *dev_priv = dev->dev_private;
1098 u32 reg = PIPEDSL(pipe);
1099 u32 line1, line2;
1100 u32 line_mask;
1101
1102 if (IS_GEN2(dev))
1103 line_mask = DSL_LINEMASK_GEN2;
1104 else
1105 line_mask = DSL_LINEMASK_GEN3;
1106
1107 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1108 msleep(5);
fbf49ea2
VS
1109 line2 = I915_READ(reg) & line_mask;
1110
1111 return line1 == line2;
1112}
1113
ab7ad7f6
KP
1114/*
1115 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1116 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1117 *
1118 * After disabling a pipe, we can't wait for vblank in the usual way,
1119 * spinning on the vblank interrupt status bit, since we won't actually
1120 * see an interrupt when the pipe is disabled.
1121 *
ab7ad7f6
KP
1122 * On Gen4 and above:
1123 * wait for the pipe register state bit to turn off
1124 *
1125 * Otherwise:
1126 * wait for the display line value to settle (it usually
1127 * ends up stopping at the start of the next frame).
58e10eb9 1128 *
9d0498a2 1129 */
575f7ab7 1130static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1131{
575f7ab7 1132 struct drm_device *dev = crtc->base.dev;
9d0498a2 1133 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1134 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1135 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1136
1137 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1138 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1139
1140 /* Wait for the Pipe State to go off */
58e10eb9
CW
1141 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1142 100))
284637d9 1143 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1144 } else {
ab7ad7f6 1145 /* Wait for the display line to settle */
fbf49ea2 1146 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1147 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1148 }
79e53945
JB
1149}
1150
b24e7179
JB
1151static const char *state_string(bool enabled)
1152{
1153 return enabled ? "on" : "off";
1154}
1155
1156/* Only for pre-ILK configs */
55607e8a
DV
1157void assert_pll(struct drm_i915_private *dev_priv,
1158 enum pipe pipe, bool state)
b24e7179 1159{
b24e7179
JB
1160 u32 val;
1161 bool cur_state;
1162
649636ef 1163 val = I915_READ(DPLL(pipe));
b24e7179 1164 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1165 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1166 "PLL state assertion failure (expected %s, current %s)\n",
1167 state_string(state), state_string(cur_state));
1168}
b24e7179 1169
23538ef1
JN
1170/* XXX: the dsi pll is shared between MIPI DSI ports */
1171static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1172{
1173 u32 val;
1174 bool cur_state;
1175
a580516d 1176 mutex_lock(&dev_priv->sb_lock);
23538ef1 1177 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1178 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1179
1180 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1181 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1182 "DSI PLL state assertion failure (expected %s, current %s)\n",
1183 state_string(state), state_string(cur_state));
1184}
1185#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1186#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1187
55607e8a 1188struct intel_shared_dpll *
e2b78267
DV
1189intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1190{
1191 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1192
6e3c9717 1193 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1194 return NULL;
1195
6e3c9717 1196 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1197}
1198
040484af 1199/* For ILK+ */
55607e8a
DV
1200void assert_shared_dpll(struct drm_i915_private *dev_priv,
1201 struct intel_shared_dpll *pll,
1202 bool state)
040484af 1203{
040484af 1204 bool cur_state;
5358901f 1205 struct intel_dpll_hw_state hw_state;
040484af 1206
92b27b08 1207 if (WARN (!pll,
46edb027 1208 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1209 return;
ee7b9f93 1210
5358901f 1211 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1212 I915_STATE_WARN(cur_state != state,
5358901f
DV
1213 "%s assertion failure (expected %s, current %s)\n",
1214 pll->name, state_string(state), state_string(cur_state));
040484af 1215}
040484af
JB
1216
1217static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
1219{
040484af 1220 bool cur_state;
ad80a810
PZ
1221 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1222 pipe);
040484af 1223
affa9354
PZ
1224 if (HAS_DDI(dev_priv->dev)) {
1225 /* DDI does not have a specific FDI_TX register */
649636ef 1226 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1227 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1228 } else {
649636ef 1229 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1230 cur_state = !!(val & FDI_TX_ENABLE);
1231 }
e2c719b7 1232 I915_STATE_WARN(cur_state != state,
040484af
JB
1233 "FDI TX state assertion failure (expected %s, current %s)\n",
1234 state_string(state), state_string(cur_state));
1235}
1236#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1237#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1238
1239static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1240 enum pipe pipe, bool state)
1241{
040484af
JB
1242 u32 val;
1243 bool cur_state;
1244
649636ef 1245 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1246 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1247 I915_STATE_WARN(cur_state != state,
040484af
JB
1248 "FDI RX state assertion failure (expected %s, current %s)\n",
1249 state_string(state), state_string(cur_state));
1250}
1251#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1252#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1253
1254static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1255 enum pipe pipe)
1256{
040484af
JB
1257 u32 val;
1258
1259 /* ILK FDI PLL is always enabled */
3d13ef2e 1260 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1261 return;
1262
bf507ef7 1263 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1264 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1265 return;
1266
649636ef 1267 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1268 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1269}
1270
55607e8a
DV
1271void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1272 enum pipe pipe, bool state)
040484af 1273{
040484af 1274 u32 val;
55607e8a 1275 bool cur_state;
040484af 1276
649636ef 1277 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1278 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1279 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1280 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1281 state_string(state), state_string(cur_state));
040484af
JB
1282}
1283
b680c37a
DV
1284void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1285 enum pipe pipe)
ea0760cf 1286{
bedd4dba
JN
1287 struct drm_device *dev = dev_priv->dev;
1288 int pp_reg;
ea0760cf
JB
1289 u32 val;
1290 enum pipe panel_pipe = PIPE_A;
0de3b485 1291 bool locked = true;
ea0760cf 1292
bedd4dba
JN
1293 if (WARN_ON(HAS_DDI(dev)))
1294 return;
1295
1296 if (HAS_PCH_SPLIT(dev)) {
1297 u32 port_sel;
1298
ea0760cf 1299 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1300 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1301
1302 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1303 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1304 panel_pipe = PIPE_B;
1305 /* XXX: else fix for eDP */
1306 } else if (IS_VALLEYVIEW(dev)) {
1307 /* presumably write lock depends on pipe, not port select */
1308 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1309 panel_pipe = pipe;
ea0760cf
JB
1310 } else {
1311 pp_reg = PP_CONTROL;
bedd4dba
JN
1312 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1313 panel_pipe = PIPE_B;
ea0760cf
JB
1314 }
1315
1316 val = I915_READ(pp_reg);
1317 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1318 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1319 locked = false;
1320
e2c719b7 1321 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1322 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1323 pipe_name(pipe));
ea0760cf
JB
1324}
1325
93ce0ba6
JN
1326static void assert_cursor(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
1328{
1329 struct drm_device *dev = dev_priv->dev;
1330 bool cur_state;
1331
d9d82081 1332 if (IS_845G(dev) || IS_I865G(dev))
0b87c24e 1333 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1334 else
5efb3e28 1335 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1336
e2c719b7 1337 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1338 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1339 pipe_name(pipe), state_string(state), state_string(cur_state));
1340}
1341#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1342#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1343
b840d907
JB
1344void assert_pipe(struct drm_i915_private *dev_priv,
1345 enum pipe pipe, bool state)
b24e7179 1346{
63d7bbe9 1347 bool cur_state;
702e7a56
PZ
1348 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1349 pipe);
b24e7179 1350
b6b5d049
VS
1351 /* if we need the pipe quirk it must be always on */
1352 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1353 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1354 state = true;
1355
f458ebbc 1356 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1357 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1358 cur_state = false;
1359 } else {
649636ef 1360 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161
PZ
1361 cur_state = !!(val & PIPECONF_ENABLE);
1362 }
1363
e2c719b7 1364 I915_STATE_WARN(cur_state != state,
63d7bbe9 1365 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1366 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1367}
1368
931872fc
CW
1369static void assert_plane(struct drm_i915_private *dev_priv,
1370 enum plane plane, bool state)
b24e7179 1371{
b24e7179 1372 u32 val;
931872fc 1373 bool cur_state;
b24e7179 1374
649636ef 1375 val = I915_READ(DSPCNTR(plane));
931872fc 1376 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1377 I915_STATE_WARN(cur_state != state,
931872fc
CW
1378 "plane %c assertion failure (expected %s, current %s)\n",
1379 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1380}
1381
931872fc
CW
1382#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1383#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1384
b24e7179
JB
1385static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1386 enum pipe pipe)
1387{
653e1026 1388 struct drm_device *dev = dev_priv->dev;
649636ef 1389 int i;
b24e7179 1390
653e1026
VS
1391 /* Primary planes are fixed to pipes on gen4+ */
1392 if (INTEL_INFO(dev)->gen >= 4) {
649636ef 1393 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1394 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1395 "plane %c assertion failure, should be disabled but not\n",
1396 plane_name(pipe));
19ec1358 1397 return;
28c05794 1398 }
19ec1358 1399
b24e7179 1400 /* Need to check both planes against the pipe */
055e393f 1401 for_each_pipe(dev_priv, i) {
649636ef
VS
1402 u32 val = I915_READ(DSPCNTR(i));
1403 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1404 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1405 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1406 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1407 plane_name(i), pipe_name(pipe));
b24e7179
JB
1408 }
1409}
1410
19332d7a
JB
1411static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1412 enum pipe pipe)
1413{
20674eef 1414 struct drm_device *dev = dev_priv->dev;
649636ef 1415 int sprite;
19332d7a 1416
7feb8b88 1417 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1418 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1419 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1420 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1421 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1422 sprite, pipe_name(pipe));
1423 }
1424 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1425 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1426 u32 val = I915_READ(SPCNTR(pipe, sprite));
e2c719b7 1427 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1429 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1430 }
1431 } else if (INTEL_INFO(dev)->gen >= 7) {
649636ef 1432 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1433 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1434 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1435 plane_name(pipe), pipe_name(pipe));
1436 } else if (INTEL_INFO(dev)->gen >= 5) {
649636ef 1437 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1438 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1439 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1440 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1441 }
1442}
1443
08c71e5e
VS
1444static void assert_vblank_disabled(struct drm_crtc *crtc)
1445{
e2c719b7 1446 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1447 drm_crtc_vblank_put(crtc);
1448}
1449
89eff4be 1450static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1451{
1452 u32 val;
1453 bool enabled;
1454
e2c719b7 1455 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1456
92f2584a
JB
1457 val = I915_READ(PCH_DREF_CONTROL);
1458 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1459 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1460 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1461}
1462
ab9412ba
DV
1463static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1464 enum pipe pipe)
92f2584a 1465{
92f2584a
JB
1466 u32 val;
1467 bool enabled;
1468
649636ef 1469 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1470 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1471 I915_STATE_WARN(enabled,
9db4a9c7
JB
1472 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1473 pipe_name(pipe));
92f2584a
JB
1474}
1475
4e634389
KP
1476static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1477 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1478{
1479 if ((val & DP_PORT_EN) == 0)
1480 return false;
1481
1482 if (HAS_PCH_CPT(dev_priv->dev)) {
1483 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1484 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1485 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1486 return false;
44f37d1f
CML
1487 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1488 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1489 return false;
f0575e92
KP
1490 } else {
1491 if ((val & DP_PIPE_MASK) != (pipe << 30))
1492 return false;
1493 }
1494 return true;
1495}
1496
1519b995
KP
1497static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1498 enum pipe pipe, u32 val)
1499{
dc0fa718 1500 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1501 return false;
1502
1503 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1504 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1505 return false;
44f37d1f
CML
1506 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1507 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1508 return false;
1519b995 1509 } else {
dc0fa718 1510 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1511 return false;
1512 }
1513 return true;
1514}
1515
1516static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1517 enum pipe pipe, u32 val)
1518{
1519 if ((val & LVDS_PORT_EN) == 0)
1520 return false;
1521
1522 if (HAS_PCH_CPT(dev_priv->dev)) {
1523 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1524 return false;
1525 } else {
1526 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1527 return false;
1528 }
1529 return true;
1530}
1531
1532static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1533 enum pipe pipe, u32 val)
1534{
1535 if ((val & ADPA_DAC_ENABLE) == 0)
1536 return false;
1537 if (HAS_PCH_CPT(dev_priv->dev)) {
1538 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1539 return false;
1540 } else {
1541 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1542 return false;
1543 }
1544 return true;
1545}
1546
291906f1 1547static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1548 enum pipe pipe, int reg, u32 port_sel)
291906f1 1549{
47a05eca 1550 u32 val = I915_READ(reg);
e2c719b7 1551 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1552 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1553 reg, pipe_name(pipe));
de9a35ab 1554
e2c719b7 1555 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1556 && (val & DP_PIPEB_SELECT),
de9a35ab 1557 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1558}
1559
1560static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1561 enum pipe pipe, int reg)
1562{
47a05eca 1563 u32 val = I915_READ(reg);
e2c719b7 1564 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1565 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1566 reg, pipe_name(pipe));
de9a35ab 1567
e2c719b7 1568 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1569 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1570 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1571}
1572
1573static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1574 enum pipe pipe)
1575{
291906f1 1576 u32 val;
291906f1 1577
f0575e92
KP
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1580 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1581
649636ef 1582 val = I915_READ(PCH_ADPA);
e2c719b7 1583 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1584 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1585 pipe_name(pipe));
291906f1 1586
649636ef 1587 val = I915_READ(PCH_LVDS);
e2c719b7 1588 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1589 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1590 pipe_name(pipe));
291906f1 1591
e2debe91
PZ
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1595}
1596
d288f65f 1597static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1598 const struct intel_crtc_state *pipe_config)
87442f73 1599{
426115cf
DV
1600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
d288f65f 1603 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1604
426115cf 1605 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1606
1607 /* No really, not for ILK+ */
1608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1611 if (IS_MOBILE(dev_priv->dev))
426115cf 1612 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1613
426115cf
DV
1614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
d288f65f 1621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1622 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1623
1624 /* We do this three times for luck */
426115cf 1625 I915_WRITE(reg, dpll);
87442f73
DV
1626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
426115cf 1628 I915_WRITE(reg, dpll);
87442f73
DV
1629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
426115cf 1631 I915_WRITE(reg, dpll);
87442f73
DV
1632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
d288f65f 1636static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1637 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
a580516d 1649 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
54433e91
VS
1656 mutex_unlock(&dev_priv->sb_lock);
1657
9d556c99
CML
1658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
d288f65f 1664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1665
1666 /* Check PLL is locked */
a11b0703 1667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
a11b0703 1670 /* not sure when this should be written */
d288f65f 1671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1672 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1673}
1674
1c4e0274
VS
1675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
3538b9df 1681 count += crtc->base.state->active &&
409ee761 1682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1683
1684 return count;
1685}
1686
66e3d5c0 1687static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1688{
66e3d5c0
DV
1689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
6e3c9717 1692 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1693
66e3d5c0 1694 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1695
63d7bbe9 1696 /* No really, not for ILK+ */
3d13ef2e 1697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1698
1699 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1702
1c4e0274
VS
1703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
66e3d5c0
DV
1715
1716 /* Wait for the clocks to stabilize. */
1717 POSTING_READ(reg);
1718 udelay(150);
1719
1720 if (INTEL_INFO(dev)->gen >= 4) {
1721 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1722 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1723 } else {
1724 /* The pixel multiplier can only be updated once the
1725 * DPLL is enabled and the clocks are stable.
1726 *
1727 * So write it again.
1728 */
1729 I915_WRITE(reg, dpll);
1730 }
63d7bbe9
JB
1731
1732 /* We do this three times for luck */
66e3d5c0 1733 I915_WRITE(reg, dpll);
63d7bbe9
JB
1734 POSTING_READ(reg);
1735 udelay(150); /* wait for warmup */
66e3d5c0 1736 I915_WRITE(reg, dpll);
63d7bbe9
JB
1737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
66e3d5c0 1739 I915_WRITE(reg, dpll);
63d7bbe9
JB
1740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
1742}
1743
1744/**
50b44a44 1745 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1746 * @dev_priv: i915 private structure
1747 * @pipe: pipe PLL to disable
1748 *
1749 * Disable the PLL for @pipe, making sure the pipe is off first.
1750 *
1751 * Note! This is for pre-ILK only.
1752 */
1c4e0274 1753static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1754{
1c4e0274
VS
1755 struct drm_device *dev = crtc->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 enum pipe pipe = crtc->pipe;
1758
1759 /* Disable DVO 2x clock on both PLLs if necessary */
1760 if (IS_I830(dev) &&
409ee761 1761 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1762 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1763 I915_WRITE(DPLL(PIPE_B),
1764 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1765 I915_WRITE(DPLL(PIPE_A),
1766 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1767 }
1768
b6b5d049
VS
1769 /* Don't disable pipe or pipe PLLs if needed */
1770 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1771 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1772 return;
1773
1774 /* Make sure the pipe isn't still relying on us */
1775 assert_pipe_disabled(dev_priv, pipe);
1776
b8afb911 1777 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1778 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1779}
1780
f6071166
JB
1781static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1782{
b8afb911 1783 u32 val;
f6071166
JB
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
e5cbfbfb
ID
1788 /*
1789 * Leave integrated clock source and reference clock enabled for pipe B.
1790 * The latter is needed for VGA hotplug / manual detection.
1791 */
b8afb911 1792 val = DPLL_VGA_MODE_DIS;
f6071166 1793 if (pipe == PIPE_B)
60bfe44f 1794 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
f6071166
JB
1795 I915_WRITE(DPLL(pipe), val);
1796 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1797
1798}
1799
1800static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1801{
d752048d 1802 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1803 u32 val;
1804
a11b0703
VS
1805 /* Make sure the pipe isn't still relying on us */
1806 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1807
a11b0703 1808 /* Set PLL en = 0 */
60bfe44f
VS
1809 val = DPLL_SSC_REF_CLK_CHV |
1810 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1811 if (pipe != PIPE_A)
1812 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1813 I915_WRITE(DPLL(pipe), val);
1814 POSTING_READ(DPLL(pipe));
d752048d 1815
a580516d 1816 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1817
1818 /* Disable 10bit clock to display controller */
1819 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1820 val &= ~DPIO_DCLKP_EN;
1821 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1822
a580516d 1823 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1824}
1825
e4607fcf 1826void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1827 struct intel_digital_port *dport,
1828 unsigned int expected_mask)
89b667f8
JB
1829{
1830 u32 port_mask;
00fc31b7 1831 int dpll_reg;
89b667f8 1832
e4607fcf
CML
1833 switch (dport->port) {
1834 case PORT_B:
89b667f8 1835 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1836 dpll_reg = DPLL(0);
e4607fcf
CML
1837 break;
1838 case PORT_C:
89b667f8 1839 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1840 dpll_reg = DPLL(0);
9b6de0a1 1841 expected_mask <<= 4;
00fc31b7
CML
1842 break;
1843 case PORT_D:
1844 port_mask = DPLL_PORTD_READY_MASK;
1845 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1846 break;
1847 default:
1848 BUG();
1849 }
89b667f8 1850
9b6de0a1
VS
1851 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1852 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1853 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1854}
1855
b14b1055
DV
1856static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1857{
1858 struct drm_device *dev = crtc->base.dev;
1859 struct drm_i915_private *dev_priv = dev->dev_private;
1860 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1861
be19f0ff
CW
1862 if (WARN_ON(pll == NULL))
1863 return;
1864
3e369b76 1865 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1866 if (pll->active == 0) {
1867 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1868 WARN_ON(pll->on);
1869 assert_shared_dpll_disabled(dev_priv, pll);
1870
1871 pll->mode_set(dev_priv, pll);
1872 }
1873}
1874
92f2584a 1875/**
85b3894f 1876 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1877 * @dev_priv: i915 private structure
1878 * @pipe: pipe PLL to enable
1879 *
1880 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1881 * drives the transcoder clock.
1882 */
85b3894f 1883static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1884{
3d13ef2e
DL
1885 struct drm_device *dev = crtc->base.dev;
1886 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1887 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1888
87a875bb 1889 if (WARN_ON(pll == NULL))
48da64a8
CW
1890 return;
1891
3e369b76 1892 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1893 return;
ee7b9f93 1894
74dd6928 1895 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1896 pll->name, pll->active, pll->on,
e2b78267 1897 crtc->base.base.id);
92f2584a 1898
cdbd2316
DV
1899 if (pll->active++) {
1900 WARN_ON(!pll->on);
e9d6944e 1901 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1902 return;
1903 }
f4a091c7 1904 WARN_ON(pll->on);
ee7b9f93 1905
bd2bb1b9
PZ
1906 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1907
46edb027 1908 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1909 pll->enable(dev_priv, pll);
ee7b9f93 1910 pll->on = true;
92f2584a
JB
1911}
1912
f6daaec2 1913static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1914{
3d13ef2e
DL
1915 struct drm_device *dev = crtc->base.dev;
1916 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1917 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1918
92f2584a 1919 /* PCH only available on ILK+ */
80aa9312
JB
1920 if (INTEL_INFO(dev)->gen < 5)
1921 return;
1922
eddfcbcd
ML
1923 if (pll == NULL)
1924 return;
92f2584a 1925
eddfcbcd 1926 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
48da64a8 1927 return;
7a419866 1928
46edb027
DV
1929 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1930 pll->name, pll->active, pll->on,
e2b78267 1931 crtc->base.base.id);
7a419866 1932
48da64a8 1933 if (WARN_ON(pll->active == 0)) {
e9d6944e 1934 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1935 return;
1936 }
1937
e9d6944e 1938 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1939 WARN_ON(!pll->on);
cdbd2316 1940 if (--pll->active)
7a419866 1941 return;
ee7b9f93 1942
46edb027 1943 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1944 pll->disable(dev_priv, pll);
ee7b9f93 1945 pll->on = false;
bd2bb1b9
PZ
1946
1947 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1948}
1949
b8a4f404
PZ
1950static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1951 enum pipe pipe)
040484af 1952{
23670b32 1953 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1954 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1956 uint32_t reg, val, pipeconf_val;
040484af
JB
1957
1958 /* PCH only available on ILK+ */
55522f37 1959 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1960
1961 /* Make sure PCH DPLL is enabled */
e72f9fbf 1962 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1963 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1964
1965 /* FDI must be feeding us bits for PCH ports */
1966 assert_fdi_tx_enabled(dev_priv, pipe);
1967 assert_fdi_rx_enabled(dev_priv, pipe);
1968
23670b32
DV
1969 if (HAS_PCH_CPT(dev)) {
1970 /* Workaround: Set the timing override bit before enabling the
1971 * pch transcoder. */
1972 reg = TRANS_CHICKEN2(pipe);
1973 val = I915_READ(reg);
1974 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1975 I915_WRITE(reg, val);
59c859d6 1976 }
23670b32 1977
ab9412ba 1978 reg = PCH_TRANSCONF(pipe);
040484af 1979 val = I915_READ(reg);
5f7f726d 1980 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1981
1982 if (HAS_PCH_IBX(dev_priv->dev)) {
1983 /*
c5de7c6f
VS
1984 * Make the BPC in transcoder be consistent with
1985 * that in pipeconf reg. For HDMI we must use 8bpc
1986 * here for both 8bpc and 12bpc.
e9bcff5c 1987 */
dfd07d72 1988 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1989 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1990 val |= PIPECONF_8BPC;
1991 else
1992 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1993 }
5f7f726d
PZ
1994
1995 val &= ~TRANS_INTERLACE_MASK;
1996 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 1997 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 1998 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1999 val |= TRANS_LEGACY_INTERLACED_ILK;
2000 else
2001 val |= TRANS_INTERLACED;
5f7f726d
PZ
2002 else
2003 val |= TRANS_PROGRESSIVE;
2004
040484af
JB
2005 I915_WRITE(reg, val | TRANS_ENABLE);
2006 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2007 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2008}
2009
8fb033d7 2010static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2011 enum transcoder cpu_transcoder)
040484af 2012{
8fb033d7 2013 u32 val, pipeconf_val;
8fb033d7
PZ
2014
2015 /* PCH only available on ILK+ */
55522f37 2016 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2017
8fb033d7 2018 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2019 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2020 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2021
223a6fdf 2022 /* Workaround: set timing override bit. */
36c0d0cf 2023 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 2024 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 2025 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 2026
25f3ef11 2027 val = TRANS_ENABLE;
937bb610 2028 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2029
9a76b1c6
PZ
2030 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2031 PIPECONF_INTERLACED_ILK)
a35f2679 2032 val |= TRANS_INTERLACED;
8fb033d7
PZ
2033 else
2034 val |= TRANS_PROGRESSIVE;
2035
ab9412ba
DV
2036 I915_WRITE(LPT_TRANSCONF, val);
2037 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2038 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2039}
2040
b8a4f404
PZ
2041static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2042 enum pipe pipe)
040484af 2043{
23670b32
DV
2044 struct drm_device *dev = dev_priv->dev;
2045 uint32_t reg, val;
040484af
JB
2046
2047 /* FDI relies on the transcoder */
2048 assert_fdi_tx_disabled(dev_priv, pipe);
2049 assert_fdi_rx_disabled(dev_priv, pipe);
2050
291906f1
JB
2051 /* Ports must be off as well */
2052 assert_pch_ports_disabled(dev_priv, pipe);
2053
ab9412ba 2054 reg = PCH_TRANSCONF(pipe);
040484af
JB
2055 val = I915_READ(reg);
2056 val &= ~TRANS_ENABLE;
2057 I915_WRITE(reg, val);
2058 /* wait for PCH transcoder off, transcoder state */
2059 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2060 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 2061
c465613b 2062 if (HAS_PCH_CPT(dev)) {
23670b32
DV
2063 /* Workaround: Clear the timing override chicken bit again. */
2064 reg = TRANS_CHICKEN2(pipe);
2065 val = I915_READ(reg);
2066 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2067 I915_WRITE(reg, val);
2068 }
040484af
JB
2069}
2070
ab4d966c 2071static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2072{
8fb033d7
PZ
2073 u32 val;
2074
ab9412ba 2075 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2076 val &= ~TRANS_ENABLE;
ab9412ba 2077 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2078 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2079 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2080 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2081
2082 /* Workaround: clear timing override bit. */
36c0d0cf 2083 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 2084 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 2085 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
2086}
2087
b24e7179 2088/**
309cfea8 2089 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2090 * @crtc: crtc responsible for the pipe
b24e7179 2091 *
0372264a 2092 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2093 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2094 */
e1fdc473 2095static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2096{
0372264a
PZ
2097 struct drm_device *dev = crtc->base.dev;
2098 struct drm_i915_private *dev_priv = dev->dev_private;
2099 enum pipe pipe = crtc->pipe;
1a70a728 2100 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1a240d4d 2101 enum pipe pch_transcoder;
b24e7179
JB
2102 int reg;
2103 u32 val;
2104
9e2ee2dd
VS
2105 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2106
58c6eaa2 2107 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2108 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2109 assert_sprites_disabled(dev_priv, pipe);
2110
681e5811 2111 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2112 pch_transcoder = TRANSCODER_A;
2113 else
2114 pch_transcoder = pipe;
2115
b24e7179
JB
2116 /*
2117 * A pipe without a PLL won't actually be able to drive bits from
2118 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2119 * need the check.
2120 */
50360403 2121 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2122 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2123 assert_dsi_pll_enabled(dev_priv);
2124 else
2125 assert_pll_enabled(dev_priv, pipe);
040484af 2126 else {
6e3c9717 2127 if (crtc->config->has_pch_encoder) {
040484af 2128 /* if driving the PCH, we need FDI enabled */
cc391bbb 2129 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2130 assert_fdi_tx_pll_enabled(dev_priv,
2131 (enum pipe) cpu_transcoder);
040484af
JB
2132 }
2133 /* FIXME: assert CPU port conditions for SNB+ */
2134 }
b24e7179 2135
702e7a56 2136 reg = PIPECONF(cpu_transcoder);
b24e7179 2137 val = I915_READ(reg);
7ad25d48 2138 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2139 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2140 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2141 return;
7ad25d48 2142 }
00d70b15
CW
2143
2144 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2145 POSTING_READ(reg);
b24e7179
JB
2146}
2147
2148/**
309cfea8 2149 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2150 * @crtc: crtc whose pipes is to be disabled
b24e7179 2151 *
575f7ab7
VS
2152 * Disable the pipe of @crtc, making sure that various hardware
2153 * specific requirements are met, if applicable, e.g. plane
2154 * disabled, panel fitter off, etc.
b24e7179
JB
2155 *
2156 * Will wait until the pipe has shut down before returning.
2157 */
575f7ab7 2158static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2159{
575f7ab7 2160 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2161 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2162 enum pipe pipe = crtc->pipe;
b24e7179
JB
2163 int reg;
2164 u32 val;
2165
9e2ee2dd
VS
2166 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2167
b24e7179
JB
2168 /*
2169 * Make sure planes won't keep trying to pump pixels to us,
2170 * or we might hang the display.
2171 */
2172 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2173 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2174 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2175
702e7a56 2176 reg = PIPECONF(cpu_transcoder);
b24e7179 2177 val = I915_READ(reg);
00d70b15
CW
2178 if ((val & PIPECONF_ENABLE) == 0)
2179 return;
2180
67adc644
VS
2181 /*
2182 * Double wide has implications for planes
2183 * so best keep it disabled when not needed.
2184 */
6e3c9717 2185 if (crtc->config->double_wide)
67adc644
VS
2186 val &= ~PIPECONF_DOUBLE_WIDE;
2187
2188 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2189 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2190 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2191 val &= ~PIPECONF_ENABLE;
2192
2193 I915_WRITE(reg, val);
2194 if ((val & PIPECONF_ENABLE) == 0)
2195 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2196}
2197
693db184
CW
2198static bool need_vtd_wa(struct drm_device *dev)
2199{
2200#ifdef CONFIG_INTEL_IOMMU
2201 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2202 return true;
2203#endif
2204 return false;
2205}
2206
50470bb0 2207unsigned int
6761dd31 2208intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
fe47ea0c 2209 uint64_t fb_format_modifier, unsigned int plane)
a57ce0b2 2210{
6761dd31
TU
2211 unsigned int tile_height;
2212 uint32_t pixel_bytes;
a57ce0b2 2213
b5d0e9bf
DL
2214 switch (fb_format_modifier) {
2215 case DRM_FORMAT_MOD_NONE:
2216 tile_height = 1;
2217 break;
2218 case I915_FORMAT_MOD_X_TILED:
2219 tile_height = IS_GEN2(dev) ? 16 : 8;
2220 break;
2221 case I915_FORMAT_MOD_Y_TILED:
2222 tile_height = 32;
2223 break;
2224 case I915_FORMAT_MOD_Yf_TILED:
fe47ea0c 2225 pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
6761dd31 2226 switch (pixel_bytes) {
b5d0e9bf 2227 default:
6761dd31 2228 case 1:
b5d0e9bf
DL
2229 tile_height = 64;
2230 break;
6761dd31
TU
2231 case 2:
2232 case 4:
b5d0e9bf
DL
2233 tile_height = 32;
2234 break;
6761dd31 2235 case 8:
b5d0e9bf
DL
2236 tile_height = 16;
2237 break;
6761dd31 2238 case 16:
b5d0e9bf
DL
2239 WARN_ONCE(1,
2240 "128-bit pixels are not supported for display!");
2241 tile_height = 16;
2242 break;
2243 }
2244 break;
2245 default:
2246 MISSING_CASE(fb_format_modifier);
2247 tile_height = 1;
2248 break;
2249 }
091df6cb 2250
6761dd31
TU
2251 return tile_height;
2252}
2253
2254unsigned int
2255intel_fb_align_height(struct drm_device *dev, unsigned int height,
2256 uint32_t pixel_format, uint64_t fb_format_modifier)
2257{
2258 return ALIGN(height, intel_tile_height(dev, pixel_format,
fe47ea0c 2259 fb_format_modifier, 0));
a57ce0b2
JB
2260}
2261
f64b98cd
TU
2262static int
2263intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2264 const struct drm_plane_state *plane_state)
2265{
50470bb0 2266 struct intel_rotation_info *info = &view->rotation_info;
84fe03f7 2267 unsigned int tile_height, tile_pitch;
50470bb0 2268
f64b98cd
TU
2269 *view = i915_ggtt_view_normal;
2270
50470bb0
TU
2271 if (!plane_state)
2272 return 0;
2273
121920fa 2274 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2275 return 0;
2276
9abc4648 2277 *view = i915_ggtt_view_rotated;
50470bb0
TU
2278
2279 info->height = fb->height;
2280 info->pixel_format = fb->pixel_format;
2281 info->pitch = fb->pitches[0];
89e3e142 2282 info->uv_offset = fb->offsets[1];
50470bb0
TU
2283 info->fb_modifier = fb->modifier[0];
2284
84fe03f7 2285 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
fe47ea0c 2286 fb->modifier[0], 0);
84fe03f7
TU
2287 tile_pitch = PAGE_SIZE / tile_height;
2288 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2289 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2290 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2291
89e3e142
TU
2292 if (info->pixel_format == DRM_FORMAT_NV12) {
2293 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2294 fb->modifier[0], 1);
2295 tile_pitch = PAGE_SIZE / tile_height;
2296 info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2297 info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
2298 tile_height);
2299 info->size_uv = info->width_pages_uv * info->height_pages_uv *
2300 PAGE_SIZE;
2301 }
2302
f64b98cd
TU
2303 return 0;
2304}
2305
4e9a86b6
VS
2306static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2307{
2308 if (INTEL_INFO(dev_priv)->gen >= 9)
2309 return 256 * 1024;
985b8bb4
VS
2310 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2311 IS_VALLEYVIEW(dev_priv))
4e9a86b6
VS
2312 return 128 * 1024;
2313 else if (INTEL_INFO(dev_priv)->gen >= 4)
2314 return 4 * 1024;
2315 else
44c5905e 2316 return 0;
4e9a86b6
VS
2317}
2318
127bd2ac 2319int
850c4cdc
TU
2320intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2321 struct drm_framebuffer *fb,
7580d774 2322 const struct drm_plane_state *plane_state)
6b95a207 2323{
850c4cdc 2324 struct drm_device *dev = fb->dev;
ce453d81 2325 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2326 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2327 struct i915_ggtt_view view;
6b95a207
KH
2328 u32 alignment;
2329 int ret;
2330
ebcdd39e
MR
2331 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2332
7b911adc
TU
2333 switch (fb->modifier[0]) {
2334 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2335 alignment = intel_linear_alignment(dev_priv);
6b95a207 2336 break;
7b911adc 2337 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2338 if (INTEL_INFO(dev)->gen >= 9)
2339 alignment = 256 * 1024;
2340 else {
2341 /* pin() will align the object as required by fence */
2342 alignment = 0;
2343 }
6b95a207 2344 break;
7b911adc 2345 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2346 case I915_FORMAT_MOD_Yf_TILED:
2347 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2348 "Y tiling bo slipped through, driver bug!\n"))
2349 return -EINVAL;
2350 alignment = 1 * 1024 * 1024;
2351 break;
6b95a207 2352 default:
7b911adc
TU
2353 MISSING_CASE(fb->modifier[0]);
2354 return -EINVAL;
6b95a207
KH
2355 }
2356
f64b98cd
TU
2357 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2358 if (ret)
2359 return ret;
2360
693db184
CW
2361 /* Note that the w/a also requires 64 PTE of padding following the
2362 * bo. We currently fill all unused PTE with the shadow page and so
2363 * we should always have valid PTE following the scanout preventing
2364 * the VT-d warning.
2365 */
2366 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2367 alignment = 256 * 1024;
2368
d6dd6843
PZ
2369 /*
2370 * Global gtt pte registers are special registers which actually forward
2371 * writes to a chunk of system memory. Which means that there is no risk
2372 * that the register values disappear as soon as we call
2373 * intel_runtime_pm_put(), so it is correct to wrap only the
2374 * pin/unpin/fence and not more.
2375 */
2376 intel_runtime_pm_get(dev_priv);
2377
7580d774
ML
2378 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2379 &view);
48b956c5 2380 if (ret)
b26a6b35 2381 goto err_pm;
6b95a207
KH
2382
2383 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2384 * fence, whereas 965+ only requires a fence if using
2385 * framebuffer compression. For simplicity, we always install
2386 * a fence as the cost is not that onerous.
2387 */
06d98131 2388 ret = i915_gem_object_get_fence(obj);
842315ee
ML
2389 if (ret == -EDEADLK) {
2390 /*
2391 * -EDEADLK means there are no free fences
2392 * no pending flips.
2393 *
2394 * This is propagated to atomic, but it uses
2395 * -EDEADLK to force a locking recovery, so
2396 * change the returned error to -EBUSY.
2397 */
2398 ret = -EBUSY;
2399 goto err_unpin;
2400 } else if (ret)
9a5a53b3 2401 goto err_unpin;
1690e1eb 2402
9a5a53b3 2403 i915_gem_object_pin_fence(obj);
6b95a207 2404
d6dd6843 2405 intel_runtime_pm_put(dev_priv);
6b95a207 2406 return 0;
48b956c5
CW
2407
2408err_unpin:
f64b98cd 2409 i915_gem_object_unpin_from_display_plane(obj, &view);
b26a6b35 2410err_pm:
d6dd6843 2411 intel_runtime_pm_put(dev_priv);
48b956c5 2412 return ret;
6b95a207
KH
2413}
2414
82bc3b2d
TU
2415static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2416 const struct drm_plane_state *plane_state)
1690e1eb 2417{
82bc3b2d 2418 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2419 struct i915_ggtt_view view;
2420 int ret;
82bc3b2d 2421
ebcdd39e
MR
2422 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2423
f64b98cd
TU
2424 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2425 WARN_ONCE(ret, "Couldn't get view from plane state!");
2426
1690e1eb 2427 i915_gem_object_unpin_fence(obj);
f64b98cd 2428 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2429}
2430
c2c75131
DV
2431/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2432 * is assumed to be a power-of-two. */
4e9a86b6
VS
2433unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2434 int *x, int *y,
bc752862
CW
2435 unsigned int tiling_mode,
2436 unsigned int cpp,
2437 unsigned int pitch)
c2c75131 2438{
bc752862
CW
2439 if (tiling_mode != I915_TILING_NONE) {
2440 unsigned int tile_rows, tiles;
c2c75131 2441
bc752862
CW
2442 tile_rows = *y / 8;
2443 *y %= 8;
c2c75131 2444
bc752862
CW
2445 tiles = *x / (512/cpp);
2446 *x %= 512/cpp;
2447
2448 return tile_rows * pitch * 8 + tiles * 4096;
2449 } else {
4e9a86b6 2450 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2451 unsigned int offset;
2452
2453 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2454 *y = (offset & alignment) / pitch;
2455 *x = ((offset & alignment) - *y * pitch) / cpp;
2456 return offset & ~alignment;
bc752862 2457 }
c2c75131
DV
2458}
2459
b35d63fa 2460static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2461{
2462 switch (format) {
2463 case DISPPLANE_8BPP:
2464 return DRM_FORMAT_C8;
2465 case DISPPLANE_BGRX555:
2466 return DRM_FORMAT_XRGB1555;
2467 case DISPPLANE_BGRX565:
2468 return DRM_FORMAT_RGB565;
2469 default:
2470 case DISPPLANE_BGRX888:
2471 return DRM_FORMAT_XRGB8888;
2472 case DISPPLANE_RGBX888:
2473 return DRM_FORMAT_XBGR8888;
2474 case DISPPLANE_BGRX101010:
2475 return DRM_FORMAT_XRGB2101010;
2476 case DISPPLANE_RGBX101010:
2477 return DRM_FORMAT_XBGR2101010;
2478 }
2479}
2480
bc8d7dff
DL
2481static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2482{
2483 switch (format) {
2484 case PLANE_CTL_FORMAT_RGB_565:
2485 return DRM_FORMAT_RGB565;
2486 default:
2487 case PLANE_CTL_FORMAT_XRGB_8888:
2488 if (rgb_order) {
2489 if (alpha)
2490 return DRM_FORMAT_ABGR8888;
2491 else
2492 return DRM_FORMAT_XBGR8888;
2493 } else {
2494 if (alpha)
2495 return DRM_FORMAT_ARGB8888;
2496 else
2497 return DRM_FORMAT_XRGB8888;
2498 }
2499 case PLANE_CTL_FORMAT_XRGB_2101010:
2500 if (rgb_order)
2501 return DRM_FORMAT_XBGR2101010;
2502 else
2503 return DRM_FORMAT_XRGB2101010;
2504 }
2505}
2506
5724dbd1 2507static bool
f6936e29
DV
2508intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2509 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2510{
2511 struct drm_device *dev = crtc->base.dev;
3badb49f 2512 struct drm_i915_private *dev_priv = to_i915(dev);
46f297fb
JB
2513 struct drm_i915_gem_object *obj = NULL;
2514 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2515 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2516 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2517 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2518 PAGE_SIZE);
2519
2520 size_aligned -= base_aligned;
46f297fb 2521
ff2652ea
CW
2522 if (plane_config->size == 0)
2523 return false;
2524
3badb49f
PZ
2525 /* If the FB is too big, just don't use it since fbdev is not very
2526 * important and we should probably use that space with FBC or other
2527 * features. */
2528 if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2529 return false;
2530
f37b5c2b
DV
2531 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2532 base_aligned,
2533 base_aligned,
2534 size_aligned);
46f297fb 2535 if (!obj)
484b41dd 2536 return false;
46f297fb 2537
49af449b
DL
2538 obj->tiling_mode = plane_config->tiling;
2539 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2540 obj->stride = fb->pitches[0];
46f297fb 2541
6bf129df
DL
2542 mode_cmd.pixel_format = fb->pixel_format;
2543 mode_cmd.width = fb->width;
2544 mode_cmd.height = fb->height;
2545 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2546 mode_cmd.modifier[0] = fb->modifier[0];
2547 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2548
2549 mutex_lock(&dev->struct_mutex);
6bf129df 2550 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2551 &mode_cmd, obj)) {
46f297fb
JB
2552 DRM_DEBUG_KMS("intel fb init failed\n");
2553 goto out_unref_obj;
2554 }
46f297fb 2555 mutex_unlock(&dev->struct_mutex);
484b41dd 2556
f6936e29 2557 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2558 return true;
46f297fb
JB
2559
2560out_unref_obj:
2561 drm_gem_object_unreference(&obj->base);
2562 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2563 return false;
2564}
2565
afd65eb4
MR
2566/* Update plane->state->fb to match plane->fb after driver-internal updates */
2567static void
2568update_state_fb(struct drm_plane *plane)
2569{
2570 if (plane->fb == plane->state->fb)
2571 return;
2572
2573 if (plane->state->fb)
2574 drm_framebuffer_unreference(plane->state->fb);
2575 plane->state->fb = plane->fb;
2576 if (plane->state->fb)
2577 drm_framebuffer_reference(plane->state->fb);
2578}
2579
5724dbd1 2580static void
f6936e29
DV
2581intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2582 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2583{
2584 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2585 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2586 struct drm_crtc *c;
2587 struct intel_crtc *i;
2ff8fde1 2588 struct drm_i915_gem_object *obj;
88595ac9 2589 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2590 struct drm_plane_state *plane_state = primary->state;
88595ac9 2591 struct drm_framebuffer *fb;
484b41dd 2592
2d14030b 2593 if (!plane_config->fb)
484b41dd
JB
2594 return;
2595
f6936e29 2596 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2597 fb = &plane_config->fb->base;
2598 goto valid_fb;
f55548b5 2599 }
484b41dd 2600
2d14030b 2601 kfree(plane_config->fb);
484b41dd
JB
2602
2603 /*
2604 * Failed to alloc the obj, check to see if we should share
2605 * an fb with another CRTC instead
2606 */
70e1e0ec 2607 for_each_crtc(dev, c) {
484b41dd
JB
2608 i = to_intel_crtc(c);
2609
2610 if (c == &intel_crtc->base)
2611 continue;
2612
2ff8fde1
MR
2613 if (!i->active)
2614 continue;
2615
88595ac9
DV
2616 fb = c->primary->fb;
2617 if (!fb)
484b41dd
JB
2618 continue;
2619
88595ac9 2620 obj = intel_fb_obj(fb);
2ff8fde1 2621 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2622 drm_framebuffer_reference(fb);
2623 goto valid_fb;
484b41dd
JB
2624 }
2625 }
88595ac9
DV
2626
2627 return;
2628
2629valid_fb:
be5651f2
ML
2630 plane_state->src_x = plane_state->src_y = 0;
2631 plane_state->src_w = fb->width << 16;
2632 plane_state->src_h = fb->height << 16;
2633
2634 plane_state->crtc_x = plane_state->src_y = 0;
2635 plane_state->crtc_w = fb->width;
2636 plane_state->crtc_h = fb->height;
2637
88595ac9
DV
2638 obj = intel_fb_obj(fb);
2639 if (obj->tiling_mode != I915_TILING_NONE)
2640 dev_priv->preserve_bios_swizzle = true;
2641
be5651f2
ML
2642 drm_framebuffer_reference(fb);
2643 primary->fb = primary->state->fb = fb;
36750f28 2644 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2645 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2646 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2647}
2648
29b9bde6
DV
2649static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2650 struct drm_framebuffer *fb,
2651 int x, int y)
81255565
JB
2652{
2653 struct drm_device *dev = crtc->dev;
2654 struct drm_i915_private *dev_priv = dev->dev_private;
2655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2656 struct drm_plane *primary = crtc->primary;
2657 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2658 struct drm_i915_gem_object *obj;
81255565 2659 int plane = intel_crtc->plane;
e506a0c6 2660 unsigned long linear_offset;
81255565 2661 u32 dspcntr;
f45651ba 2662 u32 reg = DSPCNTR(plane);
48404c1e 2663 int pixel_size;
f45651ba 2664
b70709a6 2665 if (!visible || !fb) {
fdd508a6
VS
2666 I915_WRITE(reg, 0);
2667 if (INTEL_INFO(dev)->gen >= 4)
2668 I915_WRITE(DSPSURF(plane), 0);
2669 else
2670 I915_WRITE(DSPADDR(plane), 0);
2671 POSTING_READ(reg);
2672 return;
2673 }
2674
c9ba6fad
VS
2675 obj = intel_fb_obj(fb);
2676 if (WARN_ON(obj == NULL))
2677 return;
2678
2679 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2680
f45651ba
VS
2681 dspcntr = DISPPLANE_GAMMA_ENABLE;
2682
fdd508a6 2683 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2684
2685 if (INTEL_INFO(dev)->gen < 4) {
2686 if (intel_crtc->pipe == PIPE_B)
2687 dspcntr |= DISPPLANE_SEL_PIPE_B;
2688
2689 /* pipesrc and dspsize control the size that is scaled from,
2690 * which should always be the user's requested size.
2691 */
2692 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2693 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2694 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2695 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2696 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2697 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2698 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2699 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2700 I915_WRITE(PRIMPOS(plane), 0);
2701 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2702 }
81255565 2703
57779d06
VS
2704 switch (fb->pixel_format) {
2705 case DRM_FORMAT_C8:
81255565
JB
2706 dspcntr |= DISPPLANE_8BPP;
2707 break;
57779d06 2708 case DRM_FORMAT_XRGB1555:
57779d06 2709 dspcntr |= DISPPLANE_BGRX555;
81255565 2710 break;
57779d06
VS
2711 case DRM_FORMAT_RGB565:
2712 dspcntr |= DISPPLANE_BGRX565;
2713 break;
2714 case DRM_FORMAT_XRGB8888:
57779d06
VS
2715 dspcntr |= DISPPLANE_BGRX888;
2716 break;
2717 case DRM_FORMAT_XBGR8888:
57779d06
VS
2718 dspcntr |= DISPPLANE_RGBX888;
2719 break;
2720 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2721 dspcntr |= DISPPLANE_BGRX101010;
2722 break;
2723 case DRM_FORMAT_XBGR2101010:
57779d06 2724 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2725 break;
2726 default:
baba133a 2727 BUG();
81255565 2728 }
57779d06 2729
f45651ba
VS
2730 if (INTEL_INFO(dev)->gen >= 4 &&
2731 obj->tiling_mode != I915_TILING_NONE)
2732 dspcntr |= DISPPLANE_TILED;
81255565 2733
de1aa629
VS
2734 if (IS_G4X(dev))
2735 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2736
b9897127 2737 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2738
c2c75131
DV
2739 if (INTEL_INFO(dev)->gen >= 4) {
2740 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2741 intel_gen4_compute_page_offset(dev_priv,
2742 &x, &y, obj->tiling_mode,
b9897127 2743 pixel_size,
bc752862 2744 fb->pitches[0]);
c2c75131
DV
2745 linear_offset -= intel_crtc->dspaddr_offset;
2746 } else {
e506a0c6 2747 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2748 }
e506a0c6 2749
8e7d688b 2750 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2751 dspcntr |= DISPPLANE_ROTATE_180;
2752
6e3c9717
ACO
2753 x += (intel_crtc->config->pipe_src_w - 1);
2754 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2755
2756 /* Finding the last pixel of the last line of the display
2757 data and adding to linear_offset*/
2758 linear_offset +=
6e3c9717
ACO
2759 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2760 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2761 }
2762
2db3366b
PZ
2763 intel_crtc->adjusted_x = x;
2764 intel_crtc->adjusted_y = y;
2765
48404c1e
SJ
2766 I915_WRITE(reg, dspcntr);
2767
01f2c773 2768 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2769 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2770 I915_WRITE(DSPSURF(plane),
2771 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2772 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2773 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2774 } else
f343c5f6 2775 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2776 POSTING_READ(reg);
17638cd6
JB
2777}
2778
29b9bde6
DV
2779static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2780 struct drm_framebuffer *fb,
2781 int x, int y)
17638cd6
JB
2782{
2783 struct drm_device *dev = crtc->dev;
2784 struct drm_i915_private *dev_priv = dev->dev_private;
2785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2786 struct drm_plane *primary = crtc->primary;
2787 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2788 struct drm_i915_gem_object *obj;
17638cd6 2789 int plane = intel_crtc->plane;
e506a0c6 2790 unsigned long linear_offset;
17638cd6 2791 u32 dspcntr;
f45651ba 2792 u32 reg = DSPCNTR(plane);
48404c1e 2793 int pixel_size;
f45651ba 2794
b70709a6 2795 if (!visible || !fb) {
fdd508a6
VS
2796 I915_WRITE(reg, 0);
2797 I915_WRITE(DSPSURF(plane), 0);
2798 POSTING_READ(reg);
2799 return;
2800 }
2801
c9ba6fad
VS
2802 obj = intel_fb_obj(fb);
2803 if (WARN_ON(obj == NULL))
2804 return;
2805
2806 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2807
f45651ba
VS
2808 dspcntr = DISPPLANE_GAMMA_ENABLE;
2809
fdd508a6 2810 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2811
2812 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2813 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2814
57779d06
VS
2815 switch (fb->pixel_format) {
2816 case DRM_FORMAT_C8:
17638cd6
JB
2817 dspcntr |= DISPPLANE_8BPP;
2818 break;
57779d06
VS
2819 case DRM_FORMAT_RGB565:
2820 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2821 break;
57779d06 2822 case DRM_FORMAT_XRGB8888:
57779d06
VS
2823 dspcntr |= DISPPLANE_BGRX888;
2824 break;
2825 case DRM_FORMAT_XBGR8888:
57779d06
VS
2826 dspcntr |= DISPPLANE_RGBX888;
2827 break;
2828 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2829 dspcntr |= DISPPLANE_BGRX101010;
2830 break;
2831 case DRM_FORMAT_XBGR2101010:
57779d06 2832 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2833 break;
2834 default:
baba133a 2835 BUG();
17638cd6
JB
2836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
17638cd6 2840
f45651ba 2841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2843
b9897127 2844 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2845 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2846 intel_gen4_compute_page_offset(dev_priv,
2847 &x, &y, obj->tiling_mode,
b9897127 2848 pixel_size,
bc752862 2849 fb->pitches[0]);
c2c75131 2850 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2851 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2852 dspcntr |= DISPPLANE_ROTATE_180;
2853
2854 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2855 x += (intel_crtc->config->pipe_src_w - 1);
2856 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2857
2858 /* Finding the last pixel of the last line of the display
2859 data and adding to linear_offset*/
2860 linear_offset +=
6e3c9717
ACO
2861 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2862 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2863 }
2864 }
2865
2db3366b
PZ
2866 intel_crtc->adjusted_x = x;
2867 intel_crtc->adjusted_y = y;
2868
48404c1e 2869 I915_WRITE(reg, dspcntr);
17638cd6 2870
01f2c773 2871 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2872 I915_WRITE(DSPSURF(plane),
2873 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2874 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2875 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2876 } else {
2877 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2878 I915_WRITE(DSPLINOFF(plane), linear_offset);
2879 }
17638cd6 2880 POSTING_READ(reg);
17638cd6
JB
2881}
2882
b321803d
DL
2883u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2884 uint32_t pixel_format)
2885{
2886 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2887
2888 /*
2889 * The stride is either expressed as a multiple of 64 bytes
2890 * chunks for linear buffers or in number of tiles for tiled
2891 * buffers.
2892 */
2893 switch (fb_modifier) {
2894 case DRM_FORMAT_MOD_NONE:
2895 return 64;
2896 case I915_FORMAT_MOD_X_TILED:
2897 if (INTEL_INFO(dev)->gen == 2)
2898 return 128;
2899 return 512;
2900 case I915_FORMAT_MOD_Y_TILED:
2901 /* No need to check for old gens and Y tiling since this is
2902 * about the display engine and those will be blocked before
2903 * we get here.
2904 */
2905 return 128;
2906 case I915_FORMAT_MOD_Yf_TILED:
2907 if (bits_per_pixel == 8)
2908 return 64;
2909 else
2910 return 128;
2911 default:
2912 MISSING_CASE(fb_modifier);
2913 return 64;
2914 }
2915}
2916
44eb0cb9
MK
2917u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2918 struct drm_i915_gem_object *obj,
2919 unsigned int plane)
121920fa 2920{
9abc4648 2921 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
dedf278c 2922 struct i915_vma *vma;
44eb0cb9 2923 u64 offset;
121920fa
TU
2924
2925 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2926 view = &i915_ggtt_view_rotated;
121920fa 2927
dedf278c
TU
2928 vma = i915_gem_obj_to_ggtt_view(obj, view);
2929 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
2930 view->type))
2931 return -1;
2932
44eb0cb9 2933 offset = vma->node.start;
dedf278c
TU
2934
2935 if (plane == 1) {
2936 offset += vma->ggtt_view.rotation_info.uv_start_page *
2937 PAGE_SIZE;
2938 }
2939
44eb0cb9
MK
2940 WARN_ON(upper_32_bits(offset));
2941
2942 return lower_32_bits(offset);
121920fa
TU
2943}
2944
e435d6e5
ML
2945static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2946{
2947 struct drm_device *dev = intel_crtc->base.dev;
2948 struct drm_i915_private *dev_priv = dev->dev_private;
2949
2950 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2951 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2952 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
2953}
2954
a1b2278e
CK
2955/*
2956 * This function detaches (aka. unbinds) unused scalers in hardware
2957 */
0583236e 2958static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 2959{
a1b2278e
CK
2960 struct intel_crtc_scaler_state *scaler_state;
2961 int i;
2962
a1b2278e
CK
2963 scaler_state = &intel_crtc->config->scaler_state;
2964
2965 /* loop through and disable scalers that aren't in use */
2966 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
2967 if (!scaler_state->scalers[i].in_use)
2968 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
2969 }
2970}
2971
6156a456 2972u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2973{
6156a456 2974 switch (pixel_format) {
d161cf7a 2975 case DRM_FORMAT_C8:
c34ce3d1 2976 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2977 case DRM_FORMAT_RGB565:
c34ce3d1 2978 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2979 case DRM_FORMAT_XBGR8888:
c34ce3d1 2980 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2981 case DRM_FORMAT_XRGB8888:
c34ce3d1 2982 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2983 /*
2984 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2985 * to be already pre-multiplied. We need to add a knob (or a different
2986 * DRM_FORMAT) for user-space to configure that.
2987 */
f75fb42a 2988 case DRM_FORMAT_ABGR8888:
c34ce3d1 2989 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2990 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2991 case DRM_FORMAT_ARGB8888:
c34ce3d1 2992 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2993 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2994 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2995 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2996 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2997 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2998 case DRM_FORMAT_YUYV:
c34ce3d1 2999 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3000 case DRM_FORMAT_YVYU:
c34ce3d1 3001 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3002 case DRM_FORMAT_UYVY:
c34ce3d1 3003 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3004 case DRM_FORMAT_VYUY:
c34ce3d1 3005 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3006 default:
4249eeef 3007 MISSING_CASE(pixel_format);
70d21f0e 3008 }
8cfcba41 3009
c34ce3d1 3010 return 0;
6156a456 3011}
70d21f0e 3012
6156a456
CK
3013u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3014{
6156a456 3015 switch (fb_modifier) {
30af77c4 3016 case DRM_FORMAT_MOD_NONE:
70d21f0e 3017 break;
30af77c4 3018 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3019 return PLANE_CTL_TILED_X;
b321803d 3020 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3021 return PLANE_CTL_TILED_Y;
b321803d 3022 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3023 return PLANE_CTL_TILED_YF;
70d21f0e 3024 default:
6156a456 3025 MISSING_CASE(fb_modifier);
70d21f0e 3026 }
8cfcba41 3027
c34ce3d1 3028 return 0;
6156a456 3029}
70d21f0e 3030
6156a456
CK
3031u32 skl_plane_ctl_rotation(unsigned int rotation)
3032{
3b7a5119 3033 switch (rotation) {
6156a456
CK
3034 case BIT(DRM_ROTATE_0):
3035 break;
1e8df167
SJ
3036 /*
3037 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3038 * while i915 HW rotation is clockwise, thats why this swapping.
3039 */
3b7a5119 3040 case BIT(DRM_ROTATE_90):
1e8df167 3041 return PLANE_CTL_ROTATE_270;
3b7a5119 3042 case BIT(DRM_ROTATE_180):
c34ce3d1 3043 return PLANE_CTL_ROTATE_180;
3b7a5119 3044 case BIT(DRM_ROTATE_270):
1e8df167 3045 return PLANE_CTL_ROTATE_90;
6156a456
CK
3046 default:
3047 MISSING_CASE(rotation);
3048 }
3049
c34ce3d1 3050 return 0;
6156a456
CK
3051}
3052
3053static void skylake_update_primary_plane(struct drm_crtc *crtc,
3054 struct drm_framebuffer *fb,
3055 int x, int y)
3056{
3057 struct drm_device *dev = crtc->dev;
3058 struct drm_i915_private *dev_priv = dev->dev_private;
3059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3060 struct drm_plane *plane = crtc->primary;
3061 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3062 struct drm_i915_gem_object *obj;
3063 int pipe = intel_crtc->pipe;
3064 u32 plane_ctl, stride_div, stride;
3065 u32 tile_height, plane_offset, plane_size;
3066 unsigned int rotation;
3067 int x_offset, y_offset;
44eb0cb9 3068 u32 surf_addr;
6156a456
CK
3069 struct intel_crtc_state *crtc_state = intel_crtc->config;
3070 struct intel_plane_state *plane_state;
3071 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3072 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3073 int scaler_id = -1;
3074
6156a456
CK
3075 plane_state = to_intel_plane_state(plane->state);
3076
b70709a6 3077 if (!visible || !fb) {
6156a456
CK
3078 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3079 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3080 POSTING_READ(PLANE_CTL(pipe, 0));
3081 return;
3b7a5119 3082 }
70d21f0e 3083
6156a456
CK
3084 plane_ctl = PLANE_CTL_ENABLE |
3085 PLANE_CTL_PIPE_GAMMA_ENABLE |
3086 PLANE_CTL_PIPE_CSC_ENABLE;
3087
3088 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3089 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3090 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3091
3092 rotation = plane->state->rotation;
3093 plane_ctl |= skl_plane_ctl_rotation(rotation);
3094
b321803d
DL
3095 obj = intel_fb_obj(fb);
3096 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3097 fb->pixel_format);
dedf278c 3098 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3b7a5119 3099
a42e5a23
PZ
3100 WARN_ON(drm_rect_width(&plane_state->src) == 0);
3101
3102 scaler_id = plane_state->scaler_id;
3103 src_x = plane_state->src.x1 >> 16;
3104 src_y = plane_state->src.y1 >> 16;
3105 src_w = drm_rect_width(&plane_state->src) >> 16;
3106 src_h = drm_rect_height(&plane_state->src) >> 16;
3107 dst_x = plane_state->dst.x1;
3108 dst_y = plane_state->dst.y1;
3109 dst_w = drm_rect_width(&plane_state->dst);
3110 dst_h = drm_rect_height(&plane_state->dst);
3111
3112 WARN_ON(x != src_x || y != src_y);
6156a456 3113
3b7a5119
SJ
3114 if (intel_rotation_90_or_270(rotation)) {
3115 /* stride = Surface height in tiles */
2614f17d 3116 tile_height = intel_tile_height(dev, fb->pixel_format,
fe47ea0c 3117 fb->modifier[0], 0);
3b7a5119 3118 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3119 x_offset = stride * tile_height - y - src_h;
3b7a5119 3120 y_offset = x;
6156a456 3121 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3122 } else {
3123 stride = fb->pitches[0] / stride_div;
3124 x_offset = x;
3125 y_offset = y;
6156a456 3126 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3127 }
3128 plane_offset = y_offset << 16 | x_offset;
b321803d 3129
2db3366b
PZ
3130 intel_crtc->adjusted_x = x_offset;
3131 intel_crtc->adjusted_y = y_offset;
3132
70d21f0e 3133 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3134 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3135 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3136 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3137
3138 if (scaler_id >= 0) {
3139 uint32_t ps_ctrl = 0;
3140
3141 WARN_ON(!dst_w || !dst_h);
3142 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3143 crtc_state->scaler_state.scalers[scaler_id].mode;
3144 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3145 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3146 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3147 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3148 I915_WRITE(PLANE_POS(pipe, 0), 0);
3149 } else {
3150 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3151 }
3152
121920fa 3153 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3154
3155 POSTING_READ(PLANE_SURF(pipe, 0));
3156}
3157
17638cd6
JB
3158/* Assume fb object is pinned & idle & fenced and just update base pointers */
3159static int
3160intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3161 int x, int y, enum mode_set_atomic state)
3162{
3163 struct drm_device *dev = crtc->dev;
3164 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3165
ff2a3117 3166 if (dev_priv->fbc.disable_fbc)
7733b49b 3167 dev_priv->fbc.disable_fbc(dev_priv);
81255565 3168
29b9bde6
DV
3169 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3170
3171 return 0;
81255565
JB
3172}
3173
7514747d 3174static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3175{
96a02917
VS
3176 struct drm_crtc *crtc;
3177
70e1e0ec 3178 for_each_crtc(dev, crtc) {
96a02917
VS
3179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3180 enum plane plane = intel_crtc->plane;
3181
3182 intel_prepare_page_flip(dev, plane);
3183 intel_finish_page_flip_plane(dev, plane);
3184 }
7514747d
VS
3185}
3186
3187static void intel_update_primary_planes(struct drm_device *dev)
3188{
7514747d 3189 struct drm_crtc *crtc;
96a02917 3190
70e1e0ec 3191 for_each_crtc(dev, crtc) {
11c22da6
ML
3192 struct intel_plane *plane = to_intel_plane(crtc->primary);
3193 struct intel_plane_state *plane_state;
96a02917 3194
11c22da6 3195 drm_modeset_lock_crtc(crtc, &plane->base);
11c22da6
ML
3196 plane_state = to_intel_plane_state(plane->base.state);
3197
f029ee82 3198 if (crtc->state->active && plane_state->base.fb)
11c22da6
ML
3199 plane->commit_plane(&plane->base, plane_state);
3200
3201 drm_modeset_unlock_crtc(crtc);
96a02917
VS
3202 }
3203}
3204
7514747d
VS
3205void intel_prepare_reset(struct drm_device *dev)
3206{
3207 /* no reset support for gen2 */
3208 if (IS_GEN2(dev))
3209 return;
3210
3211 /* reset doesn't touch the display */
3212 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3213 return;
3214
3215 drm_modeset_lock_all(dev);
f98ce92f
VS
3216 /*
3217 * Disabling the crtcs gracefully seems nicer. Also the
3218 * g33 docs say we should at least disable all the planes.
3219 */
6b72d486 3220 intel_display_suspend(dev);
7514747d
VS
3221}
3222
3223void intel_finish_reset(struct drm_device *dev)
3224{
3225 struct drm_i915_private *dev_priv = to_i915(dev);
3226
3227 /*
3228 * Flips in the rings will be nuked by the reset,
3229 * so complete all pending flips so that user space
3230 * will get its events and not get stuck.
3231 */
3232 intel_complete_page_flips(dev);
3233
3234 /* no reset support for gen2 */
3235 if (IS_GEN2(dev))
3236 return;
3237
3238 /* reset doesn't touch the display */
3239 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3240 /*
3241 * Flips in the rings have been nuked by the reset,
3242 * so update the base address of all primary
3243 * planes to the the last fb to make sure we're
3244 * showing the correct fb after a reset.
11c22da6
ML
3245 *
3246 * FIXME: Atomic will make this obsolete since we won't schedule
3247 * CS-based flips (which might get lost in gpu resets) any more.
7514747d
VS
3248 */
3249 intel_update_primary_planes(dev);
3250 return;
3251 }
3252
3253 /*
3254 * The display has been reset as well,
3255 * so need a full re-initialization.
3256 */
3257 intel_runtime_pm_disable_interrupts(dev_priv);
3258 intel_runtime_pm_enable_interrupts(dev_priv);
3259
3260 intel_modeset_init_hw(dev);
3261
3262 spin_lock_irq(&dev_priv->irq_lock);
3263 if (dev_priv->display.hpd_irq_setup)
3264 dev_priv->display.hpd_irq_setup(dev);
3265 spin_unlock_irq(&dev_priv->irq_lock);
3266
043e9bda 3267 intel_display_resume(dev);
7514747d
VS
3268
3269 intel_hpd_init(dev_priv);
3270
3271 drm_modeset_unlock_all(dev);
3272}
3273
7d5e3799
CW
3274static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3275{
3276 struct drm_device *dev = crtc->dev;
3277 struct drm_i915_private *dev_priv = dev->dev_private;
3278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3279 bool pending;
3280
3281 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3282 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3283 return false;
3284
5e2d7afc 3285 spin_lock_irq(&dev->event_lock);
7d5e3799 3286 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3287 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3288
3289 return pending;
3290}
3291
bfd16b2a
ML
3292static void intel_update_pipe_config(struct intel_crtc *crtc,
3293 struct intel_crtc_state *old_crtc_state)
e30e8f75
GP
3294{
3295 struct drm_device *dev = crtc->base.dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
bfd16b2a
ML
3297 struct intel_crtc_state *pipe_config =
3298 to_intel_crtc_state(crtc->base.state);
e30e8f75 3299
bfd16b2a
ML
3300 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3301 crtc->base.mode = crtc->base.state->mode;
3302
3303 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3304 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3305 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75 3306
44522d85
ML
3307 if (HAS_DDI(dev))
3308 intel_set_pipe_csc(&crtc->base);
3309
e30e8f75
GP
3310 /*
3311 * Update pipe size and adjust fitter if needed: the reason for this is
3312 * that in compute_mode_changes we check the native mode (not the pfit
3313 * mode) to see if we can flip rather than do a full mode set. In the
3314 * fastboot case, we'll flip, but if we don't update the pipesrc and
3315 * pfit state, we'll end up with a big fb scanned out into the wrong
3316 * sized surface.
e30e8f75
GP
3317 */
3318
e30e8f75 3319 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3320 ((pipe_config->pipe_src_w - 1) << 16) |
3321 (pipe_config->pipe_src_h - 1));
3322
3323 /* on skylake this is done by detaching scalers */
3324 if (INTEL_INFO(dev)->gen >= 9) {
3325 skl_detach_scalers(crtc);
3326
3327 if (pipe_config->pch_pfit.enabled)
3328 skylake_pfit_enable(crtc);
3329 } else if (HAS_PCH_SPLIT(dev)) {
3330 if (pipe_config->pch_pfit.enabled)
3331 ironlake_pfit_enable(crtc);
3332 else if (old_crtc_state->pch_pfit.enabled)
3333 ironlake_pfit_disable(crtc, true);
e30e8f75 3334 }
e30e8f75
GP
3335}
3336
5e84e1a4
ZW
3337static void intel_fdi_normal_train(struct drm_crtc *crtc)
3338{
3339 struct drm_device *dev = crtc->dev;
3340 struct drm_i915_private *dev_priv = dev->dev_private;
3341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3342 int pipe = intel_crtc->pipe;
3343 u32 reg, temp;
3344
3345 /* enable normal train */
3346 reg = FDI_TX_CTL(pipe);
3347 temp = I915_READ(reg);
61e499bf 3348 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3349 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3350 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3351 } else {
3352 temp &= ~FDI_LINK_TRAIN_NONE;
3353 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3354 }
5e84e1a4
ZW
3355 I915_WRITE(reg, temp);
3356
3357 reg = FDI_RX_CTL(pipe);
3358 temp = I915_READ(reg);
3359 if (HAS_PCH_CPT(dev)) {
3360 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3361 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3362 } else {
3363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_NONE;
3365 }
3366 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3367
3368 /* wait one idle pattern time */
3369 POSTING_READ(reg);
3370 udelay(1000);
357555c0
JB
3371
3372 /* IVB wants error correction enabled */
3373 if (IS_IVYBRIDGE(dev))
3374 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3375 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3376}
3377
8db9d77b
ZW
3378/* The FDI link training functions for ILK/Ibexpeak. */
3379static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3380{
3381 struct drm_device *dev = crtc->dev;
3382 struct drm_i915_private *dev_priv = dev->dev_private;
3383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3384 int pipe = intel_crtc->pipe;
5eddb70b 3385 u32 reg, temp, tries;
8db9d77b 3386
1c8562f6 3387 /* FDI needs bits from pipe first */
0fc932b8 3388 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3389
e1a44743
AJ
3390 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3391 for train result */
5eddb70b
CW
3392 reg = FDI_RX_IMR(pipe);
3393 temp = I915_READ(reg);
e1a44743
AJ
3394 temp &= ~FDI_RX_SYMBOL_LOCK;
3395 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3396 I915_WRITE(reg, temp);
3397 I915_READ(reg);
e1a44743
AJ
3398 udelay(150);
3399
8db9d77b 3400 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3401 reg = FDI_TX_CTL(pipe);
3402 temp = I915_READ(reg);
627eb5a3 3403 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3404 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3405 temp &= ~FDI_LINK_TRAIN_NONE;
3406 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3407 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3408
5eddb70b
CW
3409 reg = FDI_RX_CTL(pipe);
3410 temp = I915_READ(reg);
8db9d77b
ZW
3411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3413 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3414
3415 POSTING_READ(reg);
8db9d77b
ZW
3416 udelay(150);
3417
5b2adf89 3418 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3419 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3420 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3421 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3422
5eddb70b 3423 reg = FDI_RX_IIR(pipe);
e1a44743 3424 for (tries = 0; tries < 5; tries++) {
5eddb70b 3425 temp = I915_READ(reg);
8db9d77b
ZW
3426 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3427
3428 if ((temp & FDI_RX_BIT_LOCK)) {
3429 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3430 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3431 break;
3432 }
8db9d77b 3433 }
e1a44743 3434 if (tries == 5)
5eddb70b 3435 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3436
3437 /* Train 2 */
5eddb70b
CW
3438 reg = FDI_TX_CTL(pipe);
3439 temp = I915_READ(reg);
8db9d77b
ZW
3440 temp &= ~FDI_LINK_TRAIN_NONE;
3441 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3442 I915_WRITE(reg, temp);
8db9d77b 3443
5eddb70b
CW
3444 reg = FDI_RX_CTL(pipe);
3445 temp = I915_READ(reg);
8db9d77b
ZW
3446 temp &= ~FDI_LINK_TRAIN_NONE;
3447 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3448 I915_WRITE(reg, temp);
8db9d77b 3449
5eddb70b
CW
3450 POSTING_READ(reg);
3451 udelay(150);
8db9d77b 3452
5eddb70b 3453 reg = FDI_RX_IIR(pipe);
e1a44743 3454 for (tries = 0; tries < 5; tries++) {
5eddb70b 3455 temp = I915_READ(reg);
8db9d77b
ZW
3456 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3457
3458 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3459 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3460 DRM_DEBUG_KMS("FDI train 2 done.\n");
3461 break;
3462 }
8db9d77b 3463 }
e1a44743 3464 if (tries == 5)
5eddb70b 3465 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3466
3467 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3468
8db9d77b
ZW
3469}
3470
0206e353 3471static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3472 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3473 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3474 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3475 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3476};
3477
3478/* The FDI link training functions for SNB/Cougarpoint. */
3479static void gen6_fdi_link_train(struct drm_crtc *crtc)
3480{
3481 struct drm_device *dev = crtc->dev;
3482 struct drm_i915_private *dev_priv = dev->dev_private;
3483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3484 int pipe = intel_crtc->pipe;
fa37d39e 3485 u32 reg, temp, i, retry;
8db9d77b 3486
e1a44743
AJ
3487 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3488 for train result */
5eddb70b
CW
3489 reg = FDI_RX_IMR(pipe);
3490 temp = I915_READ(reg);
e1a44743
AJ
3491 temp &= ~FDI_RX_SYMBOL_LOCK;
3492 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3493 I915_WRITE(reg, temp);
3494
3495 POSTING_READ(reg);
e1a44743
AJ
3496 udelay(150);
3497
8db9d77b 3498 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3499 reg = FDI_TX_CTL(pipe);
3500 temp = I915_READ(reg);
627eb5a3 3501 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3502 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3503 temp &= ~FDI_LINK_TRAIN_NONE;
3504 temp |= FDI_LINK_TRAIN_PATTERN_1;
3505 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3506 /* SNB-B */
3507 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3508 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3509
d74cf324
DV
3510 I915_WRITE(FDI_RX_MISC(pipe),
3511 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3512
5eddb70b
CW
3513 reg = FDI_RX_CTL(pipe);
3514 temp = I915_READ(reg);
8db9d77b
ZW
3515 if (HAS_PCH_CPT(dev)) {
3516 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3517 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3518 } else {
3519 temp &= ~FDI_LINK_TRAIN_NONE;
3520 temp |= FDI_LINK_TRAIN_PATTERN_1;
3521 }
5eddb70b
CW
3522 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3523
3524 POSTING_READ(reg);
8db9d77b
ZW
3525 udelay(150);
3526
0206e353 3527 for (i = 0; i < 4; i++) {
5eddb70b
CW
3528 reg = FDI_TX_CTL(pipe);
3529 temp = I915_READ(reg);
8db9d77b
ZW
3530 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3531 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3532 I915_WRITE(reg, temp);
3533
3534 POSTING_READ(reg);
8db9d77b
ZW
3535 udelay(500);
3536
fa37d39e
SP
3537 for (retry = 0; retry < 5; retry++) {
3538 reg = FDI_RX_IIR(pipe);
3539 temp = I915_READ(reg);
3540 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3541 if (temp & FDI_RX_BIT_LOCK) {
3542 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3543 DRM_DEBUG_KMS("FDI train 1 done.\n");
3544 break;
3545 }
3546 udelay(50);
8db9d77b 3547 }
fa37d39e
SP
3548 if (retry < 5)
3549 break;
8db9d77b
ZW
3550 }
3551 if (i == 4)
5eddb70b 3552 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3553
3554 /* Train 2 */
5eddb70b
CW
3555 reg = FDI_TX_CTL(pipe);
3556 temp = I915_READ(reg);
8db9d77b
ZW
3557 temp &= ~FDI_LINK_TRAIN_NONE;
3558 temp |= FDI_LINK_TRAIN_PATTERN_2;
3559 if (IS_GEN6(dev)) {
3560 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3561 /* SNB-B */
3562 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3563 }
5eddb70b 3564 I915_WRITE(reg, temp);
8db9d77b 3565
5eddb70b
CW
3566 reg = FDI_RX_CTL(pipe);
3567 temp = I915_READ(reg);
8db9d77b
ZW
3568 if (HAS_PCH_CPT(dev)) {
3569 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3570 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3571 } else {
3572 temp &= ~FDI_LINK_TRAIN_NONE;
3573 temp |= FDI_LINK_TRAIN_PATTERN_2;
3574 }
5eddb70b
CW
3575 I915_WRITE(reg, temp);
3576
3577 POSTING_READ(reg);
8db9d77b
ZW
3578 udelay(150);
3579
0206e353 3580 for (i = 0; i < 4; i++) {
5eddb70b
CW
3581 reg = FDI_TX_CTL(pipe);
3582 temp = I915_READ(reg);
8db9d77b
ZW
3583 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3584 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3585 I915_WRITE(reg, temp);
3586
3587 POSTING_READ(reg);
8db9d77b
ZW
3588 udelay(500);
3589
fa37d39e
SP
3590 for (retry = 0; retry < 5; retry++) {
3591 reg = FDI_RX_IIR(pipe);
3592 temp = I915_READ(reg);
3593 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3594 if (temp & FDI_RX_SYMBOL_LOCK) {
3595 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3596 DRM_DEBUG_KMS("FDI train 2 done.\n");
3597 break;
3598 }
3599 udelay(50);
8db9d77b 3600 }
fa37d39e
SP
3601 if (retry < 5)
3602 break;
8db9d77b
ZW
3603 }
3604 if (i == 4)
5eddb70b 3605 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3606
3607 DRM_DEBUG_KMS("FDI train done.\n");
3608}
3609
357555c0
JB
3610/* Manual link training for Ivy Bridge A0 parts */
3611static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3612{
3613 struct drm_device *dev = crtc->dev;
3614 struct drm_i915_private *dev_priv = dev->dev_private;
3615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3616 int pipe = intel_crtc->pipe;
139ccd3f 3617 u32 reg, temp, i, j;
357555c0
JB
3618
3619 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3620 for train result */
3621 reg = FDI_RX_IMR(pipe);
3622 temp = I915_READ(reg);
3623 temp &= ~FDI_RX_SYMBOL_LOCK;
3624 temp &= ~FDI_RX_BIT_LOCK;
3625 I915_WRITE(reg, temp);
3626
3627 POSTING_READ(reg);
3628 udelay(150);
3629
01a415fd
DV
3630 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3631 I915_READ(FDI_RX_IIR(pipe)));
3632
139ccd3f
JB
3633 /* Try each vswing and preemphasis setting twice before moving on */
3634 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3635 /* disable first in case we need to retry */
3636 reg = FDI_TX_CTL(pipe);
3637 temp = I915_READ(reg);
3638 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3639 temp &= ~FDI_TX_ENABLE;
3640 I915_WRITE(reg, temp);
357555c0 3641
139ccd3f
JB
3642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
3644 temp &= ~FDI_LINK_TRAIN_AUTO;
3645 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3646 temp &= ~FDI_RX_ENABLE;
3647 I915_WRITE(reg, temp);
357555c0 3648
139ccd3f 3649 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3650 reg = FDI_TX_CTL(pipe);
3651 temp = I915_READ(reg);
139ccd3f 3652 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3653 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3654 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3655 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3656 temp |= snb_b_fdi_train_param[j/2];
3657 temp |= FDI_COMPOSITE_SYNC;
3658 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3659
139ccd3f
JB
3660 I915_WRITE(FDI_RX_MISC(pipe),
3661 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3662
139ccd3f 3663 reg = FDI_RX_CTL(pipe);
357555c0 3664 temp = I915_READ(reg);
139ccd3f
JB
3665 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3666 temp |= FDI_COMPOSITE_SYNC;
3667 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3668
139ccd3f
JB
3669 POSTING_READ(reg);
3670 udelay(1); /* should be 0.5us */
357555c0 3671
139ccd3f
JB
3672 for (i = 0; i < 4; i++) {
3673 reg = FDI_RX_IIR(pipe);
3674 temp = I915_READ(reg);
3675 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3676
139ccd3f
JB
3677 if (temp & FDI_RX_BIT_LOCK ||
3678 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3679 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3680 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3681 i);
3682 break;
3683 }
3684 udelay(1); /* should be 0.5us */
3685 }
3686 if (i == 4) {
3687 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3688 continue;
3689 }
357555c0 3690
139ccd3f 3691 /* Train 2 */
357555c0
JB
3692 reg = FDI_TX_CTL(pipe);
3693 temp = I915_READ(reg);
139ccd3f
JB
3694 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3695 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3696 I915_WRITE(reg, temp);
3697
3698 reg = FDI_RX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3701 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3702 I915_WRITE(reg, temp);
3703
3704 POSTING_READ(reg);
139ccd3f 3705 udelay(2); /* should be 1.5us */
357555c0 3706
139ccd3f
JB
3707 for (i = 0; i < 4; i++) {
3708 reg = FDI_RX_IIR(pipe);
3709 temp = I915_READ(reg);
3710 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3711
139ccd3f
JB
3712 if (temp & FDI_RX_SYMBOL_LOCK ||
3713 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3714 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3715 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3716 i);
3717 goto train_done;
3718 }
3719 udelay(2); /* should be 1.5us */
357555c0 3720 }
139ccd3f
JB
3721 if (i == 4)
3722 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3723 }
357555c0 3724
139ccd3f 3725train_done:
357555c0
JB
3726 DRM_DEBUG_KMS("FDI train done.\n");
3727}
3728
88cefb6c 3729static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3730{
88cefb6c 3731 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3732 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3733 int pipe = intel_crtc->pipe;
5eddb70b 3734 u32 reg, temp;
79e53945 3735
c64e311e 3736
c98e9dcf 3737 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3738 reg = FDI_RX_CTL(pipe);
3739 temp = I915_READ(reg);
627eb5a3 3740 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3741 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3742 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3743 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3744
3745 POSTING_READ(reg);
c98e9dcf
JB
3746 udelay(200);
3747
3748 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3749 temp = I915_READ(reg);
3750 I915_WRITE(reg, temp | FDI_PCDCLK);
3751
3752 POSTING_READ(reg);
c98e9dcf
JB
3753 udelay(200);
3754
20749730
PZ
3755 /* Enable CPU FDI TX PLL, always on for Ironlake */
3756 reg = FDI_TX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3759 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3760
20749730
PZ
3761 POSTING_READ(reg);
3762 udelay(100);
6be4a607 3763 }
0e23b99d
JB
3764}
3765
88cefb6c
DV
3766static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3767{
3768 struct drm_device *dev = intel_crtc->base.dev;
3769 struct drm_i915_private *dev_priv = dev->dev_private;
3770 int pipe = intel_crtc->pipe;
3771 u32 reg, temp;
3772
3773 /* Switch from PCDclk to Rawclk */
3774 reg = FDI_RX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3777
3778 /* Disable CPU FDI TX PLL */
3779 reg = FDI_TX_CTL(pipe);
3780 temp = I915_READ(reg);
3781 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3782
3783 POSTING_READ(reg);
3784 udelay(100);
3785
3786 reg = FDI_RX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3789
3790 /* Wait for the clocks to turn off. */
3791 POSTING_READ(reg);
3792 udelay(100);
3793}
3794
0fc932b8
JB
3795static void ironlake_fdi_disable(struct drm_crtc *crtc)
3796{
3797 struct drm_device *dev = crtc->dev;
3798 struct drm_i915_private *dev_priv = dev->dev_private;
3799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3800 int pipe = intel_crtc->pipe;
3801 u32 reg, temp;
3802
3803 /* disable CPU FDI tx and PCH FDI rx */
3804 reg = FDI_TX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3807 POSTING_READ(reg);
3808
3809 reg = FDI_RX_CTL(pipe);
3810 temp = I915_READ(reg);
3811 temp &= ~(0x7 << 16);
dfd07d72 3812 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3813 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3814
3815 POSTING_READ(reg);
3816 udelay(100);
3817
3818 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3819 if (HAS_PCH_IBX(dev))
6f06ce18 3820 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3821
3822 /* still set train pattern 1 */
3823 reg = FDI_TX_CTL(pipe);
3824 temp = I915_READ(reg);
3825 temp &= ~FDI_LINK_TRAIN_NONE;
3826 temp |= FDI_LINK_TRAIN_PATTERN_1;
3827 I915_WRITE(reg, temp);
3828
3829 reg = FDI_RX_CTL(pipe);
3830 temp = I915_READ(reg);
3831 if (HAS_PCH_CPT(dev)) {
3832 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3833 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3834 } else {
3835 temp &= ~FDI_LINK_TRAIN_NONE;
3836 temp |= FDI_LINK_TRAIN_PATTERN_1;
3837 }
3838 /* BPC in FDI rx is consistent with that in PIPECONF */
3839 temp &= ~(0x07 << 16);
dfd07d72 3840 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3841 I915_WRITE(reg, temp);
3842
3843 POSTING_READ(reg);
3844 udelay(100);
3845}
3846
5dce5b93
CW
3847bool intel_has_pending_fb_unpin(struct drm_device *dev)
3848{
3849 struct intel_crtc *crtc;
3850
3851 /* Note that we don't need to be called with mode_config.lock here
3852 * as our list of CRTC objects is static for the lifetime of the
3853 * device and so cannot disappear as we iterate. Similarly, we can
3854 * happily treat the predicates as racy, atomic checks as userspace
3855 * cannot claim and pin a new fb without at least acquring the
3856 * struct_mutex and so serialising with us.
3857 */
d3fcc808 3858 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3859 if (atomic_read(&crtc->unpin_work_count) == 0)
3860 continue;
3861
3862 if (crtc->unpin_work)
3863 intel_wait_for_vblank(dev, crtc->pipe);
3864
3865 return true;
3866 }
3867
3868 return false;
3869}
3870
d6bbafa1
CW
3871static void page_flip_completed(struct intel_crtc *intel_crtc)
3872{
3873 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3874 struct intel_unpin_work *work = intel_crtc->unpin_work;
3875
3876 /* ensure that the unpin work is consistent wrt ->pending. */
3877 smp_rmb();
3878 intel_crtc->unpin_work = NULL;
3879
3880 if (work->event)
3881 drm_send_vblank_event(intel_crtc->base.dev,
3882 intel_crtc->pipe,
3883 work->event);
3884
3885 drm_crtc_vblank_put(&intel_crtc->base);
3886
3887 wake_up_all(&dev_priv->pending_flip_queue);
3888 queue_work(dev_priv->wq, &work->work);
3889
3890 trace_i915_flip_complete(intel_crtc->plane,
3891 work->pending_flip_obj);
3892}
3893
5008e874 3894static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3895{
0f91128d 3896 struct drm_device *dev = crtc->dev;
5bb61643 3897 struct drm_i915_private *dev_priv = dev->dev_private;
5008e874 3898 long ret;
e6c3a2a6 3899
2c10d571 3900 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
3901
3902 ret = wait_event_interruptible_timeout(
3903 dev_priv->pending_flip_queue,
3904 !intel_crtc_has_pending_flip(crtc),
3905 60*HZ);
3906
3907 if (ret < 0)
3908 return ret;
3909
3910 if (ret == 0) {
9c787942 3911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3912
5e2d7afc 3913 spin_lock_irq(&dev->event_lock);
9c787942
CW
3914 if (intel_crtc->unpin_work) {
3915 WARN_ONCE(1, "Removing stuck page flip\n");
3916 page_flip_completed(intel_crtc);
3917 }
5e2d7afc 3918 spin_unlock_irq(&dev->event_lock);
9c787942 3919 }
5bb61643 3920
5008e874 3921 return 0;
e6c3a2a6
CW
3922}
3923
e615efe4
ED
3924/* Program iCLKIP clock to the desired frequency */
3925static void lpt_program_iclkip(struct drm_crtc *crtc)
3926{
3927 struct drm_device *dev = crtc->dev;
3928 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3929 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3930 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3931 u32 temp;
3932
a580516d 3933 mutex_lock(&dev_priv->sb_lock);
09153000 3934
e615efe4
ED
3935 /* It is necessary to ungate the pixclk gate prior to programming
3936 * the divisors, and gate it back when it is done.
3937 */
3938 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3939
3940 /* Disable SSCCTL */
3941 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3942 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3943 SBI_SSCCTL_DISABLE,
3944 SBI_ICLK);
e615efe4
ED
3945
3946 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3947 if (clock == 20000) {
e615efe4
ED
3948 auxdiv = 1;
3949 divsel = 0x41;
3950 phaseinc = 0x20;
3951 } else {
3952 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3953 * but the adjusted_mode->crtc_clock in in KHz. To get the
3954 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3955 * convert the virtual clock precision to KHz here for higher
3956 * precision.
3957 */
3958 u32 iclk_virtual_root_freq = 172800 * 1000;
3959 u32 iclk_pi_range = 64;
3960 u32 desired_divisor, msb_divisor_value, pi_value;
3961
12d7ceed 3962 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3963 msb_divisor_value = desired_divisor / iclk_pi_range;
3964 pi_value = desired_divisor % iclk_pi_range;
3965
3966 auxdiv = 0;
3967 divsel = msb_divisor_value - 2;
3968 phaseinc = pi_value;
3969 }
3970
3971 /* This should not happen with any sane values */
3972 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3973 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3974 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3975 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3976
3977 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3978 clock,
e615efe4
ED
3979 auxdiv,
3980 divsel,
3981 phasedir,
3982 phaseinc);
3983
3984 /* Program SSCDIVINTPHASE6 */
988d6ee8 3985 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3986 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3987 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3988 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3989 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3990 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3991 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3992 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3993
3994 /* Program SSCAUXDIV */
988d6ee8 3995 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3996 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3997 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3998 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3999
4000 /* Enable modulator and associated divider */
988d6ee8 4001 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4002 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4003 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4004
4005 /* Wait for initialization time */
4006 udelay(24);
4007
4008 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 4009
a580516d 4010 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
4011}
4012
275f01b2
DV
4013static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4014 enum pipe pch_transcoder)
4015{
4016 struct drm_device *dev = crtc->base.dev;
4017 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4018 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4019
4020 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4021 I915_READ(HTOTAL(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4023 I915_READ(HBLANK(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4025 I915_READ(HSYNC(cpu_transcoder)));
4026
4027 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4028 I915_READ(VTOTAL(cpu_transcoder)));
4029 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4030 I915_READ(VBLANK(cpu_transcoder)));
4031 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4032 I915_READ(VSYNC(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4034 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4035}
4036
003632d9 4037static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4038{
4039 struct drm_i915_private *dev_priv = dev->dev_private;
4040 uint32_t temp;
4041
4042 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4043 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4044 return;
4045
4046 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4047 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4048
003632d9
ACO
4049 temp &= ~FDI_BC_BIFURCATION_SELECT;
4050 if (enable)
4051 temp |= FDI_BC_BIFURCATION_SELECT;
4052
4053 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4054 I915_WRITE(SOUTH_CHICKEN1, temp);
4055 POSTING_READ(SOUTH_CHICKEN1);
4056}
4057
4058static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4059{
4060 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4061
4062 switch (intel_crtc->pipe) {
4063 case PIPE_A:
4064 break;
4065 case PIPE_B:
6e3c9717 4066 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4067 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4068 else
003632d9 4069 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4070
4071 break;
4072 case PIPE_C:
003632d9 4073 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4074
4075 break;
4076 default:
4077 BUG();
4078 }
4079}
4080
c48b5305
VS
4081/* Return which DP Port should be selected for Transcoder DP control */
4082static enum port
4083intel_trans_dp_port_sel(struct drm_crtc *crtc)
4084{
4085 struct drm_device *dev = crtc->dev;
4086 struct intel_encoder *encoder;
4087
4088 for_each_encoder_on_crtc(dev, crtc, encoder) {
4089 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4090 encoder->type == INTEL_OUTPUT_EDP)
4091 return enc_to_dig_port(&encoder->base)->port;
4092 }
4093
4094 return -1;
4095}
4096
f67a559d
JB
4097/*
4098 * Enable PCH resources required for PCH ports:
4099 * - PCH PLLs
4100 * - FDI training & RX/TX
4101 * - update transcoder timings
4102 * - DP transcoding bits
4103 * - transcoder
4104 */
4105static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4106{
4107 struct drm_device *dev = crtc->dev;
4108 struct drm_i915_private *dev_priv = dev->dev_private;
4109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4110 int pipe = intel_crtc->pipe;
ee7b9f93 4111 u32 reg, temp;
2c07245f 4112
ab9412ba 4113 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4114
1fbc0d78
DV
4115 if (IS_IVYBRIDGE(dev))
4116 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4117
cd986abb
DV
4118 /* Write the TU size bits before fdi link training, so that error
4119 * detection works. */
4120 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4121 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4122
c98e9dcf 4123 /* For PCH output, training FDI link */
674cf967 4124 dev_priv->display.fdi_link_train(crtc);
2c07245f 4125
3ad8a208
DV
4126 /* We need to program the right clock selection before writing the pixel
4127 * mutliplier into the DPLL. */
303b81e0 4128 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4129 u32 sel;
4b645f14 4130
c98e9dcf 4131 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4132 temp |= TRANS_DPLL_ENABLE(pipe);
4133 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4134 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4135 temp |= sel;
4136 else
4137 temp &= ~sel;
c98e9dcf 4138 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4139 }
5eddb70b 4140
3ad8a208
DV
4141 /* XXX: pch pll's can be enabled any time before we enable the PCH
4142 * transcoder, and we actually should do this to not upset any PCH
4143 * transcoder that already use the clock when we share it.
4144 *
4145 * Note that enable_shared_dpll tries to do the right thing, but
4146 * get_shared_dpll unconditionally resets the pll - we need that to have
4147 * the right LVDS enable sequence. */
85b3894f 4148 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4149
d9b6cb56
JB
4150 /* set transcoder timing, panel must allow it */
4151 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4152 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4153
303b81e0 4154 intel_fdi_normal_train(crtc);
5e84e1a4 4155
c98e9dcf 4156 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4157 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
9c4edaee
VS
4158 const struct drm_display_mode *adjusted_mode =
4159 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4160 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4161 reg = TRANS_DP_CTL(pipe);
4162 temp = I915_READ(reg);
4163 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4164 TRANS_DP_SYNC_MASK |
4165 TRANS_DP_BPC_MASK);
e3ef4479 4166 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4167 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4168
9c4edaee 4169 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4170 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4171 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4172 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4173
4174 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4175 case PORT_B:
5eddb70b 4176 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4177 break;
c48b5305 4178 case PORT_C:
5eddb70b 4179 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4180 break;
c48b5305 4181 case PORT_D:
5eddb70b 4182 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4183 break;
4184 default:
e95d41e1 4185 BUG();
32f9d658 4186 }
2c07245f 4187
5eddb70b 4188 I915_WRITE(reg, temp);
6be4a607 4189 }
b52eb4dc 4190
b8a4f404 4191 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4192}
4193
1507e5bd
PZ
4194static void lpt_pch_enable(struct drm_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4199 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4200
ab9412ba 4201 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4202
8c52b5e8 4203 lpt_program_iclkip(crtc);
1507e5bd 4204
0540e488 4205 /* Set transcoder timing. */
275f01b2 4206 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4207
937bb610 4208 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4209}
4210
190f68c5
ACO
4211struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4212 struct intel_crtc_state *crtc_state)
ee7b9f93 4213{
e2b78267 4214 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4215 struct intel_shared_dpll *pll;
de419ab6 4216 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4217 enum intel_dpll_id i;
ee7b9f93 4218
de419ab6
ML
4219 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4220
98b6bd99
DV
4221 if (HAS_PCH_IBX(dev_priv->dev)) {
4222 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4223 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4224 pll = &dev_priv->shared_dplls[i];
98b6bd99 4225
46edb027
DV
4226 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4227 crtc->base.base.id, pll->name);
98b6bd99 4228
de419ab6 4229 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4230
98b6bd99
DV
4231 goto found;
4232 }
4233
bcddf610
S
4234 if (IS_BROXTON(dev_priv->dev)) {
4235 /* PLL is attached to port in bxt */
4236 struct intel_encoder *encoder;
4237 struct intel_digital_port *intel_dig_port;
4238
4239 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4240 if (WARN_ON(!encoder))
4241 return NULL;
4242
4243 intel_dig_port = enc_to_dig_port(&encoder->base);
4244 /* 1:1 mapping between ports and PLLs */
4245 i = (enum intel_dpll_id)intel_dig_port->port;
4246 pll = &dev_priv->shared_dplls[i];
4247 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4248 crtc->base.base.id, pll->name);
de419ab6 4249 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4250
4251 goto found;
4252 }
4253
e72f9fbf
DV
4254 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4255 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4256
4257 /* Only want to check enabled timings first */
de419ab6 4258 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4259 continue;
4260
190f68c5 4261 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4262 &shared_dpll[i].hw_state,
4263 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4264 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4265 crtc->base.base.id, pll->name,
de419ab6 4266 shared_dpll[i].crtc_mask,
8bd31e67 4267 pll->active);
ee7b9f93
JB
4268 goto found;
4269 }
4270 }
4271
4272 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4273 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4274 pll = &dev_priv->shared_dplls[i];
de419ab6 4275 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4276 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4277 crtc->base.base.id, pll->name);
ee7b9f93
JB
4278 goto found;
4279 }
4280 }
4281
4282 return NULL;
4283
4284found:
de419ab6
ML
4285 if (shared_dpll[i].crtc_mask == 0)
4286 shared_dpll[i].hw_state =
4287 crtc_state->dpll_hw_state;
f2a69f44 4288
190f68c5 4289 crtc_state->shared_dpll = i;
46edb027
DV
4290 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4291 pipe_name(crtc->pipe));
ee7b9f93 4292
de419ab6 4293 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4294
ee7b9f93
JB
4295 return pll;
4296}
4297
de419ab6 4298static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4299{
de419ab6
ML
4300 struct drm_i915_private *dev_priv = to_i915(state->dev);
4301 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4302 struct intel_shared_dpll *pll;
4303 enum intel_dpll_id i;
4304
de419ab6
ML
4305 if (!to_intel_atomic_state(state)->dpll_set)
4306 return;
8bd31e67 4307
de419ab6 4308 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4310 pll = &dev_priv->shared_dplls[i];
de419ab6 4311 pll->config = shared_dpll[i];
8bd31e67
ACO
4312 }
4313}
4314
a1520318 4315static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4316{
4317 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4318 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4319 u32 temp;
4320
4321 temp = I915_READ(dslreg);
4322 udelay(500);
4323 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4324 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4325 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4326 }
4327}
4328
86adf9d7
ML
4329static int
4330skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4331 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4332 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4333{
86adf9d7
ML
4334 struct intel_crtc_scaler_state *scaler_state =
4335 &crtc_state->scaler_state;
4336 struct intel_crtc *intel_crtc =
4337 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4338 int need_scaling;
6156a456
CK
4339
4340 need_scaling = intel_rotation_90_or_270(rotation) ?
4341 (src_h != dst_w || src_w != dst_h):
4342 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4343
4344 /*
4345 * if plane is being disabled or scaler is no more required or force detach
4346 * - free scaler binded to this plane/crtc
4347 * - in order to do this, update crtc->scaler_usage
4348 *
4349 * Here scaler state in crtc_state is set free so that
4350 * scaler can be assigned to other user. Actual register
4351 * update to free the scaler is done in plane/panel-fit programming.
4352 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4353 */
86adf9d7 4354 if (force_detach || !need_scaling) {
a1b2278e 4355 if (*scaler_id >= 0) {
86adf9d7 4356 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4357 scaler_state->scalers[*scaler_id].in_use = 0;
4358
86adf9d7
ML
4359 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4360 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4361 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4362 scaler_state->scaler_users);
4363 *scaler_id = -1;
4364 }
4365 return 0;
4366 }
4367
4368 /* range checks */
4369 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4370 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4371
4372 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4373 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4374 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4375 "size is out of scaler range\n",
86adf9d7 4376 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4377 return -EINVAL;
4378 }
4379
86adf9d7
ML
4380 /* mark this plane as a scaler user in crtc_state */
4381 scaler_state->scaler_users |= (1 << scaler_user);
4382 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4383 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4384 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4385 scaler_state->scaler_users);
4386
4387 return 0;
4388}
4389
4390/**
4391 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4392 *
4393 * @state: crtc's scaler state
86adf9d7
ML
4394 *
4395 * Return
4396 * 0 - scaler_usage updated successfully
4397 * error - requested scaling cannot be supported or other error condition
4398 */
e435d6e5 4399int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7
ML
4400{
4401 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
7c5f93b0 4402 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7
ML
4403
4404 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4405 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4406
e435d6e5 4407 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
86adf9d7
ML
4408 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4409 state->pipe_src_w, state->pipe_src_h,
aad941d5 4410 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4411}
4412
4413/**
4414 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4415 *
4416 * @state: crtc's scaler state
86adf9d7
ML
4417 * @plane_state: atomic plane state to update
4418 *
4419 * Return
4420 * 0 - scaler_usage updated successfully
4421 * error - requested scaling cannot be supported or other error condition
4422 */
da20eabd
ML
4423static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4424 struct intel_plane_state *plane_state)
86adf9d7
ML
4425{
4426
4427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4428 struct intel_plane *intel_plane =
4429 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4430 struct drm_framebuffer *fb = plane_state->base.fb;
4431 int ret;
4432
4433 bool force_detach = !fb || !plane_state->visible;
4434
4435 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4436 intel_plane->base.base.id, intel_crtc->pipe,
4437 drm_plane_index(&intel_plane->base));
4438
4439 ret = skl_update_scaler(crtc_state, force_detach,
4440 drm_plane_index(&intel_plane->base),
4441 &plane_state->scaler_id,
4442 plane_state->base.rotation,
4443 drm_rect_width(&plane_state->src) >> 16,
4444 drm_rect_height(&plane_state->src) >> 16,
4445 drm_rect_width(&plane_state->dst),
4446 drm_rect_height(&plane_state->dst));
4447
4448 if (ret || plane_state->scaler_id < 0)
4449 return ret;
4450
a1b2278e 4451 /* check colorkey */
818ed961 4452 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4453 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4454 intel_plane->base.base.id);
a1b2278e
CK
4455 return -EINVAL;
4456 }
4457
4458 /* Check src format */
86adf9d7
ML
4459 switch (fb->pixel_format) {
4460 case DRM_FORMAT_RGB565:
4461 case DRM_FORMAT_XBGR8888:
4462 case DRM_FORMAT_XRGB8888:
4463 case DRM_FORMAT_ABGR8888:
4464 case DRM_FORMAT_ARGB8888:
4465 case DRM_FORMAT_XRGB2101010:
4466 case DRM_FORMAT_XBGR2101010:
4467 case DRM_FORMAT_YUYV:
4468 case DRM_FORMAT_YVYU:
4469 case DRM_FORMAT_UYVY:
4470 case DRM_FORMAT_VYUY:
4471 break;
4472 default:
4473 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4474 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4475 return -EINVAL;
a1b2278e
CK
4476 }
4477
a1b2278e
CK
4478 return 0;
4479}
4480
e435d6e5
ML
4481static void skylake_scaler_disable(struct intel_crtc *crtc)
4482{
4483 int i;
4484
4485 for (i = 0; i < crtc->num_scalers; i++)
4486 skl_detach_scaler(crtc, i);
4487}
4488
4489static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4490{
4491 struct drm_device *dev = crtc->base.dev;
4492 struct drm_i915_private *dev_priv = dev->dev_private;
4493 int pipe = crtc->pipe;
a1b2278e
CK
4494 struct intel_crtc_scaler_state *scaler_state =
4495 &crtc->config->scaler_state;
4496
4497 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4498
6e3c9717 4499 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4500 int id;
4501
4502 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4503 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4504 return;
4505 }
4506
4507 id = scaler_state->scaler_id;
4508 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4509 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4510 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4511 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4512
4513 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4514 }
4515}
4516
b074cec8
JB
4517static void ironlake_pfit_enable(struct intel_crtc *crtc)
4518{
4519 struct drm_device *dev = crtc->base.dev;
4520 struct drm_i915_private *dev_priv = dev->dev_private;
4521 int pipe = crtc->pipe;
4522
6e3c9717 4523 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4524 /* Force use of hard-coded filter coefficients
4525 * as some pre-programmed values are broken,
4526 * e.g. x201.
4527 */
4528 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4529 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4530 PF_PIPE_SEL_IVB(pipe));
4531 else
4532 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4533 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4534 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4535 }
4536}
4537
20bc8673 4538void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4539{
cea165c3
VS
4540 struct drm_device *dev = crtc->base.dev;
4541 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4542
6e3c9717 4543 if (!crtc->config->ips_enabled)
d77e4531
PZ
4544 return;
4545
cea165c3
VS
4546 /* We can only enable IPS after we enable a plane and wait for a vblank */
4547 intel_wait_for_vblank(dev, crtc->pipe);
4548
d77e4531 4549 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4550 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4551 mutex_lock(&dev_priv->rps.hw_lock);
4552 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4553 mutex_unlock(&dev_priv->rps.hw_lock);
4554 /* Quoting Art Runyan: "its not safe to expect any particular
4555 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4556 * mailbox." Moreover, the mailbox may return a bogus state,
4557 * so we need to just enable it and continue on.
2a114cc1
BW
4558 */
4559 } else {
4560 I915_WRITE(IPS_CTL, IPS_ENABLE);
4561 /* The bit only becomes 1 in the next vblank, so this wait here
4562 * is essentially intel_wait_for_vblank. If we don't have this
4563 * and don't wait for vblanks until the end of crtc_enable, then
4564 * the HW state readout code will complain that the expected
4565 * IPS_CTL value is not the one we read. */
4566 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4567 DRM_ERROR("Timed out waiting for IPS enable\n");
4568 }
d77e4531
PZ
4569}
4570
20bc8673 4571void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4572{
4573 struct drm_device *dev = crtc->base.dev;
4574 struct drm_i915_private *dev_priv = dev->dev_private;
4575
6e3c9717 4576 if (!crtc->config->ips_enabled)
d77e4531
PZ
4577 return;
4578
4579 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4580 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4581 mutex_lock(&dev_priv->rps.hw_lock);
4582 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4583 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4584 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4585 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4586 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4587 } else {
2a114cc1 4588 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4589 POSTING_READ(IPS_CTL);
4590 }
d77e4531
PZ
4591
4592 /* We need to wait for a vblank before we can disable the plane. */
4593 intel_wait_for_vblank(dev, crtc->pipe);
4594}
4595
4596/** Loads the palette/gamma unit for the CRTC with the prepared values */
4597static void intel_crtc_load_lut(struct drm_crtc *crtc)
4598{
4599 struct drm_device *dev = crtc->dev;
4600 struct drm_i915_private *dev_priv = dev->dev_private;
4601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4602 enum pipe pipe = intel_crtc->pipe;
d77e4531
PZ
4603 int i;
4604 bool reenable_ips = false;
4605
4606 /* The clocks have to be on to load the palette. */
53d9f4e9 4607 if (!crtc->state->active)
d77e4531
PZ
4608 return;
4609
50360403 4610 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4611 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4612 assert_dsi_pll_enabled(dev_priv);
4613 else
4614 assert_pll_enabled(dev_priv, pipe);
4615 }
4616
d77e4531
PZ
4617 /* Workaround : Do not read or write the pipe palette/gamma data while
4618 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4619 */
6e3c9717 4620 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4621 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4622 GAMMA_MODE_MODE_SPLIT)) {
4623 hsw_disable_ips(intel_crtc);
4624 reenable_ips = true;
4625 }
4626
4627 for (i = 0; i < 256; i++) {
f65a9c5b
VS
4628 u32 palreg;
4629
4630 if (HAS_GMCH_DISPLAY(dev))
4631 palreg = PALETTE(pipe, i);
4632 else
4633 palreg = LGC_PALETTE(pipe, i);
4634
4635 I915_WRITE(palreg,
d77e4531
PZ
4636 (intel_crtc->lut_r[i] << 16) |
4637 (intel_crtc->lut_g[i] << 8) |
4638 intel_crtc->lut_b[i]);
4639 }
4640
4641 if (reenable_ips)
4642 hsw_enable_ips(intel_crtc);
4643}
4644
7cac945f 4645static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4646{
7cac945f 4647 if (intel_crtc->overlay) {
d3eedb1a
VS
4648 struct drm_device *dev = intel_crtc->base.dev;
4649 struct drm_i915_private *dev_priv = dev->dev_private;
4650
4651 mutex_lock(&dev->struct_mutex);
4652 dev_priv->mm.interruptible = false;
4653 (void) intel_overlay_switch_off(intel_crtc->overlay);
4654 dev_priv->mm.interruptible = true;
4655 mutex_unlock(&dev->struct_mutex);
4656 }
4657
4658 /* Let userspace switch the overlay on again. In most cases userspace
4659 * has to recompute where to put it anyway.
4660 */
4661}
4662
87d4300a
ML
4663/**
4664 * intel_post_enable_primary - Perform operations after enabling primary plane
4665 * @crtc: the CRTC whose primary plane was just enabled
4666 *
4667 * Performs potentially sleeping operations that must be done after the primary
4668 * plane is enabled, such as updating FBC and IPS. Note that this may be
4669 * called due to an explicit primary plane update, or due to an implicit
4670 * re-enable that is caused when a sprite plane is updated to no longer
4671 * completely hide the primary plane.
4672 */
4673static void
4674intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4675{
4676 struct drm_device *dev = crtc->dev;
87d4300a 4677 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4679 int pipe = intel_crtc->pipe;
a5c4d7bc 4680
87d4300a
ML
4681 /*
4682 * BDW signals flip done immediately if the plane
4683 * is disabled, even if the plane enable is already
4684 * armed to occur at the next vblank :(
4685 */
4686 if (IS_BROADWELL(dev))
4687 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4688
87d4300a
ML
4689 /*
4690 * FIXME IPS should be fine as long as one plane is
4691 * enabled, but in practice it seems to have problems
4692 * when going from primary only to sprite only and vice
4693 * versa.
4694 */
a5c4d7bc
VS
4695 hsw_enable_ips(intel_crtc);
4696
f99d7069 4697 /*
87d4300a
ML
4698 * Gen2 reports pipe underruns whenever all planes are disabled.
4699 * So don't enable underrun reporting before at least some planes
4700 * are enabled.
4701 * FIXME: Need to fix the logic to work when we turn off all planes
4702 * but leave the pipe running.
f99d7069 4703 */
87d4300a
ML
4704 if (IS_GEN2(dev))
4705 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4706
aca7b684
VS
4707 /* Underruns don't always raise interrupts, so check manually. */
4708 intel_check_cpu_fifo_underruns(dev_priv);
4709 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4710}
4711
87d4300a
ML
4712/**
4713 * intel_pre_disable_primary - Perform operations before disabling primary plane
4714 * @crtc: the CRTC whose primary plane is to be disabled
4715 *
4716 * Performs potentially sleeping operations that must be done before the
4717 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4718 * be called due to an explicit primary plane update, or due to an implicit
4719 * disable that is caused when a sprite plane completely hides the primary
4720 * plane.
4721 */
4722static void
4723intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4724{
4725 struct drm_device *dev = crtc->dev;
4726 struct drm_i915_private *dev_priv = dev->dev_private;
4727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4728 int pipe = intel_crtc->pipe;
a5c4d7bc 4729
87d4300a
ML
4730 /*
4731 * Gen2 reports pipe underruns whenever all planes are disabled.
4732 * So diasble underrun reporting before all the planes get disabled.
4733 * FIXME: Need to fix the logic to work when we turn off all planes
4734 * but leave the pipe running.
4735 */
4736 if (IS_GEN2(dev))
4737 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4738
87d4300a
ML
4739 /*
4740 * Vblank time updates from the shadow to live plane control register
4741 * are blocked if the memory self-refresh mode is active at that
4742 * moment. So to make sure the plane gets truly disabled, disable
4743 * first the self-refresh mode. The self-refresh enable bit in turn
4744 * will be checked/applied by the HW only at the next frame start
4745 * event which is after the vblank start event, so we need to have a
4746 * wait-for-vblank between disabling the plane and the pipe.
4747 */
262cd2e1 4748 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4749 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4750 dev_priv->wm.vlv.cxsr = false;
4751 intel_wait_for_vblank(dev, pipe);
4752 }
87d4300a 4753
87d4300a
ML
4754 /*
4755 * FIXME IPS should be fine as long as one plane is
4756 * enabled, but in practice it seems to have problems
4757 * when going from primary only to sprite only and vice
4758 * versa.
4759 */
a5c4d7bc 4760 hsw_disable_ips(intel_crtc);
87d4300a
ML
4761}
4762
ac21b225
ML
4763static void intel_post_plane_update(struct intel_crtc *crtc)
4764{
4765 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4766 struct drm_device *dev = crtc->base.dev;
7733b49b 4767 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4768
4769 if (atomic->wait_vblank)
4770 intel_wait_for_vblank(dev, crtc->pipe);
4771
4772 intel_frontbuffer_flip(dev, atomic->fb_bits);
4773
852eb00d
VS
4774 if (atomic->disable_cxsr)
4775 crtc->wm.cxsr_allowed = true;
4776
f015c551
VS
4777 if (crtc->atomic.update_wm_post)
4778 intel_update_watermarks(&crtc->base);
4779
c80ac854 4780 if (atomic->update_fbc)
7733b49b 4781 intel_fbc_update(dev_priv);
ac21b225
ML
4782
4783 if (atomic->post_enable_primary)
4784 intel_post_enable_primary(&crtc->base);
4785
ac21b225
ML
4786 memset(atomic, 0, sizeof(*atomic));
4787}
4788
4789static void intel_pre_plane_update(struct intel_crtc *crtc)
4790{
4791 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4792 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225 4793 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
ac21b225 4794
c80ac854 4795 if (atomic->disable_fbc)
25ad93fd 4796 intel_fbc_disable_crtc(crtc);
ac21b225 4797
066cf55b
RV
4798 if (crtc->atomic.disable_ips)
4799 hsw_disable_ips(crtc);
4800
ac21b225
ML
4801 if (atomic->pre_disable_primary)
4802 intel_pre_disable_primary(&crtc->base);
852eb00d
VS
4803
4804 if (atomic->disable_cxsr) {
4805 crtc->wm.cxsr_allowed = false;
4806 intel_set_memory_cxsr(dev_priv, false);
4807 }
ac21b225
ML
4808}
4809
d032ffa0 4810static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4811{
4812 struct drm_device *dev = crtc->dev;
4813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4814 struct drm_plane *p;
87d4300a
ML
4815 int pipe = intel_crtc->pipe;
4816
7cac945f 4817 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4818
d032ffa0
ML
4819 drm_for_each_plane_mask(p, dev, plane_mask)
4820 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4821
f99d7069
DV
4822 /*
4823 * FIXME: Once we grow proper nuclear flip support out of this we need
4824 * to compute the mask of flip planes precisely. For the time being
4825 * consider this a flip to a NULL plane.
4826 */
4827 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4828}
4829
f67a559d
JB
4830static void ironlake_crtc_enable(struct drm_crtc *crtc)
4831{
4832 struct drm_device *dev = crtc->dev;
4833 struct drm_i915_private *dev_priv = dev->dev_private;
4834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4835 struct intel_encoder *encoder;
f67a559d 4836 int pipe = intel_crtc->pipe;
f67a559d 4837
53d9f4e9 4838 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4839 return;
4840
81b088ca
VS
4841 if (intel_crtc->config->has_pch_encoder)
4842 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4843
6e3c9717 4844 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4845 intel_prepare_shared_dpll(intel_crtc);
4846
6e3c9717 4847 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4848 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4849
4850 intel_set_pipe_timings(intel_crtc);
4851
6e3c9717 4852 if (intel_crtc->config->has_pch_encoder) {
29407aab 4853 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4854 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4855 }
4856
4857 ironlake_set_pipeconf(crtc);
4858
f67a559d 4859 intel_crtc->active = true;
8664281b 4860
a72e4c9f 4861 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4862
f6736a1a 4863 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4864 if (encoder->pre_enable)
4865 encoder->pre_enable(encoder);
f67a559d 4866
6e3c9717 4867 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4868 /* Note: FDI PLL enabling _must_ be done before we enable the
4869 * cpu pipes, hence this is separate from all the other fdi/pch
4870 * enabling. */
88cefb6c 4871 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4872 } else {
4873 assert_fdi_tx_disabled(dev_priv, pipe);
4874 assert_fdi_rx_disabled(dev_priv, pipe);
4875 }
f67a559d 4876
b074cec8 4877 ironlake_pfit_enable(intel_crtc);
f67a559d 4878
9c54c0dd
JB
4879 /*
4880 * On ILK+ LUT must be loaded before the pipe is running but with
4881 * clocks enabled
4882 */
4883 intel_crtc_load_lut(crtc);
4884
f37fcc2a 4885 intel_update_watermarks(crtc);
e1fdc473 4886 intel_enable_pipe(intel_crtc);
f67a559d 4887
6e3c9717 4888 if (intel_crtc->config->has_pch_encoder)
f67a559d 4889 ironlake_pch_enable(crtc);
c98e9dcf 4890
f9b61ff6
DV
4891 assert_vblank_disabled(crtc);
4892 drm_crtc_vblank_on(crtc);
4893
fa5c73b1
DV
4894 for_each_encoder_on_crtc(dev, crtc, encoder)
4895 encoder->enable(encoder);
61b77ddd
DV
4896
4897 if (HAS_PCH_CPT(dev))
a1520318 4898 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
4899
4900 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4901 if (intel_crtc->config->has_pch_encoder)
4902 intel_wait_for_vblank(dev, pipe);
4903 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
4904}
4905
42db64ef
PZ
4906/* IPS only exists on ULT machines and is tied to pipe A. */
4907static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4908{
f5adf94e 4909 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4910}
4911
4f771f10
PZ
4912static void haswell_crtc_enable(struct drm_crtc *crtc)
4913{
4914 struct drm_device *dev = crtc->dev;
4915 struct drm_i915_private *dev_priv = dev->dev_private;
4916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4917 struct intel_encoder *encoder;
99d736a2
ML
4918 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4919 struct intel_crtc_state *pipe_config =
4920 to_intel_crtc_state(crtc->state);
7d4aefd0 4921 bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
4f771f10 4922
53d9f4e9 4923 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4924 return;
4925
81b088ca
VS
4926 if (intel_crtc->config->has_pch_encoder)
4927 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4928 false);
4929
df8ad70c
DV
4930 if (intel_crtc_to_shared_dpll(intel_crtc))
4931 intel_enable_shared_dpll(intel_crtc);
4932
6e3c9717 4933 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4934 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4935
4936 intel_set_pipe_timings(intel_crtc);
4937
6e3c9717
ACO
4938 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4939 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4940 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4941 }
4942
6e3c9717 4943 if (intel_crtc->config->has_pch_encoder) {
229fca97 4944 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4945 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4946 }
4947
4948 haswell_set_pipeconf(crtc);
4949
4950 intel_set_pipe_csc(crtc);
4951
4f771f10 4952 intel_crtc->active = true;
8664281b 4953
a72e4c9f 4954 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7d4aefd0
SS
4955 for_each_encoder_on_crtc(dev, crtc, encoder) {
4956 if (encoder->pre_pll_enable)
4957 encoder->pre_pll_enable(encoder);
4f771f10
PZ
4958 if (encoder->pre_enable)
4959 encoder->pre_enable(encoder);
7d4aefd0 4960 }
4f771f10 4961
d2d65408 4962 if (intel_crtc->config->has_pch_encoder)
4fe9467d 4963 dev_priv->display.fdi_link_train(crtc);
4fe9467d 4964
7d4aefd0
SS
4965 if (!is_dsi)
4966 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4967
1c132b44 4968 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 4969 skylake_pfit_enable(intel_crtc);
ff6d9f55 4970 else
1c132b44 4971 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4972
4973 /*
4974 * On ILK+ LUT must be loaded before the pipe is running but with
4975 * clocks enabled
4976 */
4977 intel_crtc_load_lut(crtc);
4978
1f544388 4979 intel_ddi_set_pipe_settings(crtc);
7d4aefd0
SS
4980 if (!is_dsi)
4981 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4982
f37fcc2a 4983 intel_update_watermarks(crtc);
e1fdc473 4984 intel_enable_pipe(intel_crtc);
42db64ef 4985
6e3c9717 4986 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4987 lpt_pch_enable(crtc);
4f771f10 4988
7d4aefd0 4989 if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
0e32b39c
DA
4990 intel_ddi_set_vc_payload_alloc(crtc, true);
4991
f9b61ff6
DV
4992 assert_vblank_disabled(crtc);
4993 drm_crtc_vblank_on(crtc);
4994
8807e55b 4995 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4996 encoder->enable(encoder);
8807e55b
JN
4997 intel_opregion_notify_encoder(encoder, true);
4998 }
4f771f10 4999
d2d65408
VS
5000 if (intel_crtc->config->has_pch_encoder)
5001 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5002 true);
5003
e4916946
PZ
5004 /* If we change the relative order between pipe/planes enabling, we need
5005 * to change the workaround. */
99d736a2
ML
5006 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5007 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5008 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5009 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5010 }
4f771f10
PZ
5011}
5012
bfd16b2a 5013static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
DV
5014{
5015 struct drm_device *dev = crtc->base.dev;
5016 struct drm_i915_private *dev_priv = dev->dev_private;
5017 int pipe = crtc->pipe;
5018
5019 /* To avoid upsetting the power well on haswell only disable the pfit if
5020 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5021 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5022 I915_WRITE(PF_CTL(pipe), 0);
5023 I915_WRITE(PF_WIN_POS(pipe), 0);
5024 I915_WRITE(PF_WIN_SZ(pipe), 0);
5025 }
5026}
5027
6be4a607
JB
5028static void ironlake_crtc_disable(struct drm_crtc *crtc)
5029{
5030 struct drm_device *dev = crtc->dev;
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5033 struct intel_encoder *encoder;
6be4a607 5034 int pipe = intel_crtc->pipe;
5eddb70b 5035 u32 reg, temp;
b52eb4dc 5036
37ca8d4c
VS
5037 if (intel_crtc->config->has_pch_encoder)
5038 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5039
ea9d758d
DV
5040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 encoder->disable(encoder);
5042
f9b61ff6
DV
5043 drm_crtc_vblank_off(crtc);
5044 assert_vblank_disabled(crtc);
5045
575f7ab7 5046 intel_disable_pipe(intel_crtc);
32f9d658 5047
bfd16b2a 5048 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5049
5a74f70a
VS
5050 if (intel_crtc->config->has_pch_encoder)
5051 ironlake_fdi_disable(crtc);
5052
bf49ec8c
DV
5053 for_each_encoder_on_crtc(dev, crtc, encoder)
5054 if (encoder->post_disable)
5055 encoder->post_disable(encoder);
2c07245f 5056
6e3c9717 5057 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5058 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5059
d925c59a
DV
5060 if (HAS_PCH_CPT(dev)) {
5061 /* disable TRANS_DP_CTL */
5062 reg = TRANS_DP_CTL(pipe);
5063 temp = I915_READ(reg);
5064 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5065 TRANS_DP_PORT_SEL_MASK);
5066 temp |= TRANS_DP_PORT_SEL_NONE;
5067 I915_WRITE(reg, temp);
5068
5069 /* disable DPLL_SEL */
5070 temp = I915_READ(PCH_DPLL_SEL);
11887397 5071 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5072 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5073 }
e3421a18 5074
d925c59a
DV
5075 ironlake_fdi_pll_disable(intel_crtc);
5076 }
81b088ca
VS
5077
5078 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5079}
1b3c7a47 5080
4f771f10 5081static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5082{
4f771f10
PZ
5083 struct drm_device *dev = crtc->dev;
5084 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5086 struct intel_encoder *encoder;
6e3c9717 5087 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7d4aefd0 5088 bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
ee7b9f93 5089
d2d65408
VS
5090 if (intel_crtc->config->has_pch_encoder)
5091 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5092 false);
5093
8807e55b
JN
5094 for_each_encoder_on_crtc(dev, crtc, encoder) {
5095 intel_opregion_notify_encoder(encoder, false);
4f771f10 5096 encoder->disable(encoder);
8807e55b 5097 }
4f771f10 5098
f9b61ff6
DV
5099 drm_crtc_vblank_off(crtc);
5100 assert_vblank_disabled(crtc);
5101
575f7ab7 5102 intel_disable_pipe(intel_crtc);
4f771f10 5103
6e3c9717 5104 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5105 intel_ddi_set_vc_payload_alloc(crtc, false);
5106
7d4aefd0
SS
5107 if (!is_dsi)
5108 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5109
1c132b44 5110 if (INTEL_INFO(dev)->gen >= 9)
e435d6e5 5111 skylake_scaler_disable(intel_crtc);
ff6d9f55 5112 else
bfd16b2a 5113 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5114
7d4aefd0
SS
5115 if (!is_dsi)
5116 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5117
6e3c9717 5118 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5119 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5120 intel_ddi_fdi_disable(crtc);
83616634 5121 }
4f771f10 5122
97b040aa
ID
5123 for_each_encoder_on_crtc(dev, crtc, encoder)
5124 if (encoder->post_disable)
5125 encoder->post_disable(encoder);
81b088ca
VS
5126
5127 if (intel_crtc->config->has_pch_encoder)
5128 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5129 true);
4f771f10
PZ
5130}
5131
2dd24552
JB
5132static void i9xx_pfit_enable(struct intel_crtc *crtc)
5133{
5134 struct drm_device *dev = crtc->base.dev;
5135 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5136 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5137
681a8504 5138 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5139 return;
5140
2dd24552 5141 /*
c0b03411
DV
5142 * The panel fitter should only be adjusted whilst the pipe is disabled,
5143 * according to register description and PRM.
2dd24552 5144 */
c0b03411
DV
5145 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5146 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5147
b074cec8
JB
5148 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5149 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5150
5151 /* Border color in case we don't scale up to the full screen. Black by
5152 * default, change to something else for debugging. */
5153 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5154}
5155
d05410f9
DA
5156static enum intel_display_power_domain port_to_power_domain(enum port port)
5157{
5158 switch (port) {
5159 case PORT_A:
6331a704 5160 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5161 case PORT_B:
6331a704 5162 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5163 case PORT_C:
6331a704 5164 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5165 case PORT_D:
6331a704 5166 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5167 case PORT_E:
6331a704 5168 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9
DA
5169 default:
5170 WARN_ON_ONCE(1);
5171 return POWER_DOMAIN_PORT_OTHER;
5172 }
5173}
5174
25f78f58
VS
5175static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5176{
5177 switch (port) {
5178 case PORT_A:
5179 return POWER_DOMAIN_AUX_A;
5180 case PORT_B:
5181 return POWER_DOMAIN_AUX_B;
5182 case PORT_C:
5183 return POWER_DOMAIN_AUX_C;
5184 case PORT_D:
5185 return POWER_DOMAIN_AUX_D;
5186 case PORT_E:
5187 /* FIXME: Check VBT for actual wiring of PORT E */
5188 return POWER_DOMAIN_AUX_D;
5189 default:
5190 WARN_ON_ONCE(1);
5191 return POWER_DOMAIN_AUX_A;
5192 }
5193}
5194
77d22dca
ID
5195#define for_each_power_domain(domain, mask) \
5196 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5197 if ((1 << (domain)) & (mask))
5198
319be8ae
ID
5199enum intel_display_power_domain
5200intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5201{
5202 struct drm_device *dev = intel_encoder->base.dev;
5203 struct intel_digital_port *intel_dig_port;
5204
5205 switch (intel_encoder->type) {
5206 case INTEL_OUTPUT_UNKNOWN:
5207 /* Only DDI platforms should ever use this output type */
5208 WARN_ON_ONCE(!HAS_DDI(dev));
5209 case INTEL_OUTPUT_DISPLAYPORT:
5210 case INTEL_OUTPUT_HDMI:
5211 case INTEL_OUTPUT_EDP:
5212 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5213 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5214 case INTEL_OUTPUT_DP_MST:
5215 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5216 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5217 case INTEL_OUTPUT_ANALOG:
5218 return POWER_DOMAIN_PORT_CRT;
5219 case INTEL_OUTPUT_DSI:
5220 return POWER_DOMAIN_PORT_DSI;
5221 default:
5222 return POWER_DOMAIN_PORT_OTHER;
5223 }
5224}
5225
25f78f58
VS
5226enum intel_display_power_domain
5227intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5228{
5229 struct drm_device *dev = intel_encoder->base.dev;
5230 struct intel_digital_port *intel_dig_port;
5231
5232 switch (intel_encoder->type) {
5233 case INTEL_OUTPUT_UNKNOWN:
5234 /* Only DDI platforms should ever use this output type */
5235 WARN_ON_ONCE(!HAS_DDI(dev));
5236 case INTEL_OUTPUT_DISPLAYPORT:
5237 case INTEL_OUTPUT_EDP:
5238 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5239 return port_to_aux_power_domain(intel_dig_port->port);
5240 case INTEL_OUTPUT_DP_MST:
5241 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5242 return port_to_aux_power_domain(intel_dig_port->port);
5243 default:
5244 WARN_ON_ONCE(1);
5245 return POWER_DOMAIN_AUX_A;
5246 }
5247}
5248
319be8ae 5249static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5250{
319be8ae
ID
5251 struct drm_device *dev = crtc->dev;
5252 struct intel_encoder *intel_encoder;
5253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5254 enum pipe pipe = intel_crtc->pipe;
77d22dca 5255 unsigned long mask;
1a70a728 5256 enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
77d22dca 5257
292b990e
ML
5258 if (!crtc->state->active)
5259 return 0;
5260
77d22dca
ID
5261 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5262 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5263 if (intel_crtc->config->pch_pfit.enabled ||
5264 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5265 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5266
319be8ae
ID
5267 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5268 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5269
77d22dca
ID
5270 return mask;
5271}
5272
292b990e 5273static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5274{
292b990e
ML
5275 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5277 enum intel_display_power_domain domain;
5278 unsigned long domains, new_domains, old_domains;
77d22dca 5279
292b990e
ML
5280 old_domains = intel_crtc->enabled_power_domains;
5281 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
77d22dca 5282
292b990e
ML
5283 domains = new_domains & ~old_domains;
5284
5285 for_each_power_domain(domain, domains)
5286 intel_display_power_get(dev_priv, domain);
5287
5288 return old_domains & ~new_domains;
5289}
5290
5291static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5292 unsigned long domains)
5293{
5294 enum intel_display_power_domain domain;
5295
5296 for_each_power_domain(domain, domains)
5297 intel_display_power_put(dev_priv, domain);
5298}
77d22dca 5299
292b990e
ML
5300static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
5301{
5302 struct drm_device *dev = state->dev;
5303 struct drm_i915_private *dev_priv = dev->dev_private;
5304 unsigned long put_domains[I915_MAX_PIPES] = {};
5305 struct drm_crtc_state *crtc_state;
5306 struct drm_crtc *crtc;
5307 int i;
77d22dca 5308
292b990e
ML
5309 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5310 if (needs_modeset(crtc->state))
5311 put_domains[to_intel_crtc(crtc)->pipe] =
5312 modeset_get_crtc_power_domains(crtc);
77d22dca
ID
5313 }
5314
27c329ed
ML
5315 if (dev_priv->display.modeset_commit_cdclk) {
5316 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5317
5318 if (cdclk != dev_priv->cdclk_freq &&
5319 !WARN_ON(!state->allow_modeset))
5320 dev_priv->display.modeset_commit_cdclk(state);
5321 }
50f6e502 5322
292b990e
ML
5323 for (i = 0; i < I915_MAX_PIPES; i++)
5324 if (put_domains[i])
5325 modeset_put_power_domains(dev_priv, put_domains[i]);
77d22dca
ID
5326}
5327
adafdc6f
MK
5328static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5329{
5330 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5331
5332 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5333 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5334 return max_cdclk_freq;
5335 else if (IS_CHERRYVIEW(dev_priv))
5336 return max_cdclk_freq*95/100;
5337 else if (INTEL_INFO(dev_priv)->gen < 4)
5338 return 2*max_cdclk_freq*90/100;
5339 else
5340 return max_cdclk_freq*90/100;
5341}
5342
560a7ae4
DL
5343static void intel_update_max_cdclk(struct drm_device *dev)
5344{
5345 struct drm_i915_private *dev_priv = dev->dev_private;
5346
ef11bdb3 5347 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
560a7ae4
DL
5348 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5349
5350 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5351 dev_priv->max_cdclk_freq = 675000;
5352 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5353 dev_priv->max_cdclk_freq = 540000;
5354 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5355 dev_priv->max_cdclk_freq = 450000;
5356 else
5357 dev_priv->max_cdclk_freq = 337500;
5358 } else if (IS_BROADWELL(dev)) {
5359 /*
5360 * FIXME with extra cooling we can allow
5361 * 540 MHz for ULX and 675 Mhz for ULT.
5362 * How can we know if extra cooling is
5363 * available? PCI ID, VTB, something else?
5364 */
5365 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5366 dev_priv->max_cdclk_freq = 450000;
5367 else if (IS_BDW_ULX(dev))
5368 dev_priv->max_cdclk_freq = 450000;
5369 else if (IS_BDW_ULT(dev))
5370 dev_priv->max_cdclk_freq = 540000;
5371 else
5372 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5373 } else if (IS_CHERRYVIEW(dev)) {
5374 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5375 } else if (IS_VALLEYVIEW(dev)) {
5376 dev_priv->max_cdclk_freq = 400000;
5377 } else {
5378 /* otherwise assume cdclk is fixed */
5379 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5380 }
5381
adafdc6f
MK
5382 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5383
560a7ae4
DL
5384 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5385 dev_priv->max_cdclk_freq);
adafdc6f
MK
5386
5387 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5388 dev_priv->max_dotclk_freq);
560a7ae4
DL
5389}
5390
5391static void intel_update_cdclk(struct drm_device *dev)
5392{
5393 struct drm_i915_private *dev_priv = dev->dev_private;
5394
5395 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5396 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5397 dev_priv->cdclk_freq);
5398
5399 /*
5400 * Program the gmbus_freq based on the cdclk frequency.
5401 * BSpec erroneously claims we should aim for 4MHz, but
5402 * in fact 1MHz is the correct frequency.
5403 */
5404 if (IS_VALLEYVIEW(dev)) {
5405 /*
5406 * Program the gmbus_freq based on the cdclk frequency.
5407 * BSpec erroneously claims we should aim for 4MHz, but
5408 * in fact 1MHz is the correct frequency.
5409 */
5410 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5411 }
5412
5413 if (dev_priv->max_cdclk_freq == 0)
5414 intel_update_max_cdclk(dev);
5415}
5416
70d0c574 5417static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5418{
5419 struct drm_i915_private *dev_priv = dev->dev_private;
5420 uint32_t divider;
5421 uint32_t ratio;
5422 uint32_t current_freq;
5423 int ret;
5424
5425 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5426 switch (frequency) {
5427 case 144000:
5428 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5429 ratio = BXT_DE_PLL_RATIO(60);
5430 break;
5431 case 288000:
5432 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5433 ratio = BXT_DE_PLL_RATIO(60);
5434 break;
5435 case 384000:
5436 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5437 ratio = BXT_DE_PLL_RATIO(60);
5438 break;
5439 case 576000:
5440 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5441 ratio = BXT_DE_PLL_RATIO(60);
5442 break;
5443 case 624000:
5444 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5445 ratio = BXT_DE_PLL_RATIO(65);
5446 break;
5447 case 19200:
5448 /*
5449 * Bypass frequency with DE PLL disabled. Init ratio, divider
5450 * to suppress GCC warning.
5451 */
5452 ratio = 0;
5453 divider = 0;
5454 break;
5455 default:
5456 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5457
5458 return;
5459 }
5460
5461 mutex_lock(&dev_priv->rps.hw_lock);
5462 /* Inform power controller of upcoming frequency change */
5463 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5464 0x80000000);
5465 mutex_unlock(&dev_priv->rps.hw_lock);
5466
5467 if (ret) {
5468 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5469 ret, frequency);
5470 return;
5471 }
5472
5473 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5474 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5475 current_freq = current_freq * 500 + 1000;
5476
5477 /*
5478 * DE PLL has to be disabled when
5479 * - setting to 19.2MHz (bypass, PLL isn't used)
5480 * - before setting to 624MHz (PLL needs toggling)
5481 * - before setting to any frequency from 624MHz (PLL needs toggling)
5482 */
5483 if (frequency == 19200 || frequency == 624000 ||
5484 current_freq == 624000) {
5485 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5486 /* Timeout 200us */
5487 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5488 1))
5489 DRM_ERROR("timout waiting for DE PLL unlock\n");
5490 }
5491
5492 if (frequency != 19200) {
5493 uint32_t val;
5494
5495 val = I915_READ(BXT_DE_PLL_CTL);
5496 val &= ~BXT_DE_PLL_RATIO_MASK;
5497 val |= ratio;
5498 I915_WRITE(BXT_DE_PLL_CTL, val);
5499
5500 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5501 /* Timeout 200us */
5502 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5503 DRM_ERROR("timeout waiting for DE PLL lock\n");
5504
5505 val = I915_READ(CDCLK_CTL);
5506 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5507 val |= divider;
5508 /*
5509 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5510 * enable otherwise.
5511 */
5512 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5513 if (frequency >= 500000)
5514 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5515
5516 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5517 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5518 val |= (frequency - 1000) / 500;
5519 I915_WRITE(CDCLK_CTL, val);
5520 }
5521
5522 mutex_lock(&dev_priv->rps.hw_lock);
5523 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5524 DIV_ROUND_UP(frequency, 25000));
5525 mutex_unlock(&dev_priv->rps.hw_lock);
5526
5527 if (ret) {
5528 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5529 ret, frequency);
5530 return;
5531 }
5532
a47871bd 5533 intel_update_cdclk(dev);
f8437dd1
VK
5534}
5535
5536void broxton_init_cdclk(struct drm_device *dev)
5537{
5538 struct drm_i915_private *dev_priv = dev->dev_private;
5539 uint32_t val;
5540
5541 /*
5542 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5543 * or else the reset will hang because there is no PCH to respond.
5544 * Move the handshake programming to initialization sequence.
5545 * Previously was left up to BIOS.
5546 */
5547 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5548 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5549 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5550
5551 /* Enable PG1 for cdclk */
5552 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5553
5554 /* check if cd clock is enabled */
5555 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5556 DRM_DEBUG_KMS("Display already initialized\n");
5557 return;
5558 }
5559
5560 /*
5561 * FIXME:
5562 * - The initial CDCLK needs to be read from VBT.
5563 * Need to make this change after VBT has changes for BXT.
5564 * - check if setting the max (or any) cdclk freq is really necessary
5565 * here, it belongs to modeset time
5566 */
5567 broxton_set_cdclk(dev, 624000);
5568
5569 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5570 POSTING_READ(DBUF_CTL);
5571
f8437dd1
VK
5572 udelay(10);
5573
5574 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5575 DRM_ERROR("DBuf power enable timeout!\n");
5576}
5577
5578void broxton_uninit_cdclk(struct drm_device *dev)
5579{
5580 struct drm_i915_private *dev_priv = dev->dev_private;
5581
5582 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5583 POSTING_READ(DBUF_CTL);
5584
f8437dd1
VK
5585 udelay(10);
5586
5587 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5588 DRM_ERROR("DBuf power disable timeout!\n");
5589
5590 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5591 broxton_set_cdclk(dev, 19200);
5592
5593 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5594}
5595
5d96d8af
DL
5596static const struct skl_cdclk_entry {
5597 unsigned int freq;
5598 unsigned int vco;
5599} skl_cdclk_frequencies[] = {
5600 { .freq = 308570, .vco = 8640 },
5601 { .freq = 337500, .vco = 8100 },
5602 { .freq = 432000, .vco = 8640 },
5603 { .freq = 450000, .vco = 8100 },
5604 { .freq = 540000, .vco = 8100 },
5605 { .freq = 617140, .vco = 8640 },
5606 { .freq = 675000, .vco = 8100 },
5607};
5608
5609static unsigned int skl_cdclk_decimal(unsigned int freq)
5610{
5611 return (freq - 1000) / 500;
5612}
5613
5614static unsigned int skl_cdclk_get_vco(unsigned int freq)
5615{
5616 unsigned int i;
5617
5618 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5619 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5620
5621 if (e->freq == freq)
5622 return e->vco;
5623 }
5624
5625 return 8100;
5626}
5627
5628static void
5629skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5630{
5631 unsigned int min_freq;
5632 u32 val;
5633
5634 /* select the minimum CDCLK before enabling DPLL 0 */
5635 val = I915_READ(CDCLK_CTL);
5636 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5637 val |= CDCLK_FREQ_337_308;
5638
5639 if (required_vco == 8640)
5640 min_freq = 308570;
5641 else
5642 min_freq = 337500;
5643
5644 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5645
5646 I915_WRITE(CDCLK_CTL, val);
5647 POSTING_READ(CDCLK_CTL);
5648
5649 /*
5650 * We always enable DPLL0 with the lowest link rate possible, but still
5651 * taking into account the VCO required to operate the eDP panel at the
5652 * desired frequency. The usual DP link rates operate with a VCO of
5653 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5654 * The modeset code is responsible for the selection of the exact link
5655 * rate later on, with the constraint of choosing a frequency that
5656 * works with required_vco.
5657 */
5658 val = I915_READ(DPLL_CTRL1);
5659
5660 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5661 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5662 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5663 if (required_vco == 8640)
5664 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5665 SKL_DPLL0);
5666 else
5667 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5668 SKL_DPLL0);
5669
5670 I915_WRITE(DPLL_CTRL1, val);
5671 POSTING_READ(DPLL_CTRL1);
5672
5673 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5674
5675 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5676 DRM_ERROR("DPLL0 not locked\n");
5677}
5678
5679static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5680{
5681 int ret;
5682 u32 val;
5683
5684 /* inform PCU we want to change CDCLK */
5685 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5686 mutex_lock(&dev_priv->rps.hw_lock);
5687 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5688 mutex_unlock(&dev_priv->rps.hw_lock);
5689
5690 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5691}
5692
5693static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5694{
5695 unsigned int i;
5696
5697 for (i = 0; i < 15; i++) {
5698 if (skl_cdclk_pcu_ready(dev_priv))
5699 return true;
5700 udelay(10);
5701 }
5702
5703 return false;
5704}
5705
5706static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5707{
560a7ae4 5708 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5709 u32 freq_select, pcu_ack;
5710
5711 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5712
5713 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5714 DRM_ERROR("failed to inform PCU about cdclk change\n");
5715 return;
5716 }
5717
5718 /* set CDCLK_CTL */
5719 switch(freq) {
5720 case 450000:
5721 case 432000:
5722 freq_select = CDCLK_FREQ_450_432;
5723 pcu_ack = 1;
5724 break;
5725 case 540000:
5726 freq_select = CDCLK_FREQ_540;
5727 pcu_ack = 2;
5728 break;
5729 case 308570:
5730 case 337500:
5731 default:
5732 freq_select = CDCLK_FREQ_337_308;
5733 pcu_ack = 0;
5734 break;
5735 case 617140:
5736 case 675000:
5737 freq_select = CDCLK_FREQ_675_617;
5738 pcu_ack = 3;
5739 break;
5740 }
5741
5742 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5743 POSTING_READ(CDCLK_CTL);
5744
5745 /* inform PCU of the change */
5746 mutex_lock(&dev_priv->rps.hw_lock);
5747 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5748 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5749
5750 intel_update_cdclk(dev);
5d96d8af
DL
5751}
5752
5753void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5754{
5755 /* disable DBUF power */
5756 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5757 POSTING_READ(DBUF_CTL);
5758
5759 udelay(10);
5760
5761 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5762 DRM_ERROR("DBuf power disable timeout\n");
5763
ab96c1ee
ID
5764 /* disable DPLL0 */
5765 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5766 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5767 DRM_ERROR("Couldn't disable DPLL0\n");
5d96d8af
DL
5768}
5769
5770void skl_init_cdclk(struct drm_i915_private *dev_priv)
5771{
5d96d8af
DL
5772 unsigned int required_vco;
5773
39d9b85a
GW
5774 /* DPLL0 not enabled (happens on early BIOS versions) */
5775 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5776 /* enable DPLL0 */
5777 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5778 skl_dpll0_enable(dev_priv, required_vco);
5d96d8af
DL
5779 }
5780
5d96d8af
DL
5781 /* set CDCLK to the frequency the BIOS chose */
5782 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5783
5784 /* enable DBUF power */
5785 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5786 POSTING_READ(DBUF_CTL);
5787
5788 udelay(10);
5789
5790 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5791 DRM_ERROR("DBuf power enable timeout\n");
5792}
5793
c73666f3
SK
5794int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5795{
5796 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5797 uint32_t cdctl = I915_READ(CDCLK_CTL);
5798 int freq = dev_priv->skl_boot_cdclk;
5799
f1b391a5
SK
5800 /*
5801 * check if the pre-os intialized the display
5802 * There is SWF18 scratchpad register defined which is set by the
5803 * pre-os which can be used by the OS drivers to check the status
5804 */
5805 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5806 goto sanitize;
5807
c73666f3
SK
5808 /* Is PLL enabled and locked ? */
5809 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5810 goto sanitize;
5811
5812 /* DPLL okay; verify the cdclock
5813 *
5814 * Noticed in some instances that the freq selection is correct but
5815 * decimal part is programmed wrong from BIOS where pre-os does not
5816 * enable display. Verify the same as well.
5817 */
5818 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5819 /* All well; nothing to sanitize */
5820 return false;
5821sanitize:
5822 /*
5823 * As of now initialize with max cdclk till
5824 * we get dynamic cdclk support
5825 * */
5826 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5827 skl_init_cdclk(dev_priv);
5828
5829 /* we did have to sanitize */
5830 return true;
5831}
5832
30a970c6
JB
5833/* Adjust CDclk dividers to allow high res or save power if possible */
5834static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5835{
5836 struct drm_i915_private *dev_priv = dev->dev_private;
5837 u32 val, cmd;
5838
164dfd28
VK
5839 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5840 != dev_priv->cdclk_freq);
d60c4473 5841
dfcab17e 5842 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5843 cmd = 2;
dfcab17e 5844 else if (cdclk == 266667)
30a970c6
JB
5845 cmd = 1;
5846 else
5847 cmd = 0;
5848
5849 mutex_lock(&dev_priv->rps.hw_lock);
5850 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5851 val &= ~DSPFREQGUAR_MASK;
5852 val |= (cmd << DSPFREQGUAR_SHIFT);
5853 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5854 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5855 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5856 50)) {
5857 DRM_ERROR("timed out waiting for CDclk change\n");
5858 }
5859 mutex_unlock(&dev_priv->rps.hw_lock);
5860
54433e91
VS
5861 mutex_lock(&dev_priv->sb_lock);
5862
dfcab17e 5863 if (cdclk == 400000) {
6bcda4f0 5864 u32 divider;
30a970c6 5865
6bcda4f0 5866 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5867
30a970c6
JB
5868 /* adjust cdclk divider */
5869 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 5870 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
5871 val |= divider;
5872 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5873
5874 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 5875 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
5876 50))
5877 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5878 }
5879
30a970c6
JB
5880 /* adjust self-refresh exit latency value */
5881 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5882 val &= ~0x7f;
5883
5884 /*
5885 * For high bandwidth configs, we set a higher latency in the bunit
5886 * so that the core display fetch happens in time to avoid underruns.
5887 */
dfcab17e 5888 if (cdclk == 400000)
30a970c6
JB
5889 val |= 4500 / 250; /* 4.5 usec */
5890 else
5891 val |= 3000 / 250; /* 3.0 usec */
5892 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5893
a580516d 5894 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5895
b6283055 5896 intel_update_cdclk(dev);
30a970c6
JB
5897}
5898
383c5a6a
VS
5899static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5900{
5901 struct drm_i915_private *dev_priv = dev->dev_private;
5902 u32 val, cmd;
5903
164dfd28
VK
5904 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5905 != dev_priv->cdclk_freq);
383c5a6a
VS
5906
5907 switch (cdclk) {
383c5a6a
VS
5908 case 333333:
5909 case 320000:
383c5a6a 5910 case 266667:
383c5a6a 5911 case 200000:
383c5a6a
VS
5912 break;
5913 default:
5f77eeb0 5914 MISSING_CASE(cdclk);
383c5a6a
VS
5915 return;
5916 }
5917
9d0d3fda
VS
5918 /*
5919 * Specs are full of misinformation, but testing on actual
5920 * hardware has shown that we just need to write the desired
5921 * CCK divider into the Punit register.
5922 */
5923 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5924
383c5a6a
VS
5925 mutex_lock(&dev_priv->rps.hw_lock);
5926 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5927 val &= ~DSPFREQGUAR_MASK_CHV;
5928 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5929 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5930 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5931 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5932 50)) {
5933 DRM_ERROR("timed out waiting for CDclk change\n");
5934 }
5935 mutex_unlock(&dev_priv->rps.hw_lock);
5936
b6283055 5937 intel_update_cdclk(dev);
383c5a6a
VS
5938}
5939
30a970c6
JB
5940static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5941 int max_pixclk)
5942{
6bcda4f0 5943 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5944 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5945
30a970c6
JB
5946 /*
5947 * Really only a few cases to deal with, as only 4 CDclks are supported:
5948 * 200MHz
5949 * 267MHz
29dc7ef3 5950 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5951 * 400MHz (VLV only)
5952 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5953 * of the lower bin and adjust if needed.
e37c67a1
VS
5954 *
5955 * We seem to get an unstable or solid color picture at 200MHz.
5956 * Not sure what's wrong. For now use 200MHz only when all pipes
5957 * are off.
30a970c6 5958 */
6cca3195
VS
5959 if (!IS_CHERRYVIEW(dev_priv) &&
5960 max_pixclk > freq_320*limit/100)
dfcab17e 5961 return 400000;
6cca3195 5962 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5963 return freq_320;
e37c67a1 5964 else if (max_pixclk > 0)
dfcab17e 5965 return 266667;
e37c67a1
VS
5966 else
5967 return 200000;
30a970c6
JB
5968}
5969
f8437dd1
VK
5970static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5971 int max_pixclk)
5972{
5973 /*
5974 * FIXME:
5975 * - remove the guardband, it's not needed on BXT
5976 * - set 19.2MHz bypass frequency if there are no active pipes
5977 */
5978 if (max_pixclk > 576000*9/10)
5979 return 624000;
5980 else if (max_pixclk > 384000*9/10)
5981 return 576000;
5982 else if (max_pixclk > 288000*9/10)
5983 return 384000;
5984 else if (max_pixclk > 144000*9/10)
5985 return 288000;
5986 else
5987 return 144000;
5988}
5989
a821fc46
ACO
5990/* Compute the max pixel clock for new configuration. Uses atomic state if
5991 * that's non-NULL, look at current state otherwise. */
5992static int intel_mode_max_pixclk(struct drm_device *dev,
5993 struct drm_atomic_state *state)
30a970c6 5994{
30a970c6 5995 struct intel_crtc *intel_crtc;
304603f4 5996 struct intel_crtc_state *crtc_state;
30a970c6
JB
5997 int max_pixclk = 0;
5998
d3fcc808 5999 for_each_intel_crtc(dev, intel_crtc) {
27c329ed 6000 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
304603f4
ACO
6001 if (IS_ERR(crtc_state))
6002 return PTR_ERR(crtc_state);
6003
6004 if (!crtc_state->base.enable)
6005 continue;
6006
6007 max_pixclk = max(max_pixclk,
6008 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
6009 }
6010
6011 return max_pixclk;
6012}
6013
27c329ed 6014static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6015{
27c329ed
ML
6016 struct drm_device *dev = state->dev;
6017 struct drm_i915_private *dev_priv = dev->dev_private;
6018 int max_pixclk = intel_mode_max_pixclk(dev, state);
30a970c6 6019
304603f4
ACO
6020 if (max_pixclk < 0)
6021 return max_pixclk;
30a970c6 6022
27c329ed
ML
6023 to_intel_atomic_state(state)->cdclk =
6024 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6025
27c329ed
ML
6026 return 0;
6027}
304603f4 6028
27c329ed
ML
6029static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6030{
6031 struct drm_device *dev = state->dev;
6032 struct drm_i915_private *dev_priv = dev->dev_private;
6033 int max_pixclk = intel_mode_max_pixclk(dev, state);
85a96e7a 6034
27c329ed
ML
6035 if (max_pixclk < 0)
6036 return max_pixclk;
85a96e7a 6037
27c329ed
ML
6038 to_intel_atomic_state(state)->cdclk =
6039 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 6040
27c329ed 6041 return 0;
30a970c6
JB
6042}
6043
1e69cd74
VS
6044static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6045{
6046 unsigned int credits, default_credits;
6047
6048 if (IS_CHERRYVIEW(dev_priv))
6049 default_credits = PFI_CREDIT(12);
6050 else
6051 default_credits = PFI_CREDIT(8);
6052
bfa7df01 6053 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6054 /* CHV suggested value is 31 or 63 */
6055 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6056 credits = PFI_CREDIT_63;
1e69cd74
VS
6057 else
6058 credits = PFI_CREDIT(15);
6059 } else {
6060 credits = default_credits;
6061 }
6062
6063 /*
6064 * WA - write default credits before re-programming
6065 * FIXME: should we also set the resend bit here?
6066 */
6067 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6068 default_credits);
6069
6070 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6071 credits | PFI_CREDIT_RESEND);
6072
6073 /*
6074 * FIXME is this guaranteed to clear
6075 * immediately or should we poll for it?
6076 */
6077 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6078}
6079
27c329ed 6080static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6081{
a821fc46 6082 struct drm_device *dev = old_state->dev;
27c329ed 6083 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
30a970c6 6084 struct drm_i915_private *dev_priv = dev->dev_private;
30a970c6 6085
27c329ed
ML
6086 /*
6087 * FIXME: We can end up here with all power domains off, yet
6088 * with a CDCLK frequency other than the minimum. To account
6089 * for this take the PIPE-A power domain, which covers the HW
6090 * blocks needed for the following programming. This can be
6091 * removed once it's guaranteed that we get here either with
6092 * the minimum CDCLK set, or the required power domains
6093 * enabled.
6094 */
6095 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6096
27c329ed
ML
6097 if (IS_CHERRYVIEW(dev))
6098 cherryview_set_cdclk(dev, req_cdclk);
6099 else
6100 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6101
27c329ed 6102 vlv_program_pfi_credits(dev_priv);
1e69cd74 6103
27c329ed 6104 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6105}
6106
89b667f8
JB
6107static void valleyview_crtc_enable(struct drm_crtc *crtc)
6108{
6109 struct drm_device *dev = crtc->dev;
a72e4c9f 6110 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6112 struct intel_encoder *encoder;
6113 int pipe = intel_crtc->pipe;
23538ef1 6114 bool is_dsi;
89b667f8 6115
53d9f4e9 6116 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6117 return;
6118
409ee761 6119 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 6120
6e3c9717 6121 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6122 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6123
6124 intel_set_pipe_timings(intel_crtc);
6125
c14b0485
VS
6126 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6127 struct drm_i915_private *dev_priv = dev->dev_private;
6128
6129 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6130 I915_WRITE(CHV_CANVAS(pipe), 0);
6131 }
6132
5b18e57c
DV
6133 i9xx_set_pipeconf(intel_crtc);
6134
89b667f8 6135 intel_crtc->active = true;
89b667f8 6136
a72e4c9f 6137 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6138
89b667f8
JB
6139 for_each_encoder_on_crtc(dev, crtc, encoder)
6140 if (encoder->pre_pll_enable)
6141 encoder->pre_pll_enable(encoder);
6142
9d556c99 6143 if (!is_dsi) {
c0b4c660
VS
6144 if (IS_CHERRYVIEW(dev)) {
6145 chv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6146 chv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660
VS
6147 } else {
6148 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6e3c9717 6149 vlv_enable_pll(intel_crtc, intel_crtc->config);
c0b4c660 6150 }
9d556c99 6151 }
89b667f8
JB
6152
6153 for_each_encoder_on_crtc(dev, crtc, encoder)
6154 if (encoder->pre_enable)
6155 encoder->pre_enable(encoder);
6156
2dd24552
JB
6157 i9xx_pfit_enable(intel_crtc);
6158
63cbb074
VS
6159 intel_crtc_load_lut(crtc);
6160
e1fdc473 6161 intel_enable_pipe(intel_crtc);
be6a6f8e 6162
4b3a9526
VS
6163 assert_vblank_disabled(crtc);
6164 drm_crtc_vblank_on(crtc);
6165
f9b61ff6
DV
6166 for_each_encoder_on_crtc(dev, crtc, encoder)
6167 encoder->enable(encoder);
89b667f8
JB
6168}
6169
f13c2ef3
DV
6170static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6171{
6172 struct drm_device *dev = crtc->base.dev;
6173 struct drm_i915_private *dev_priv = dev->dev_private;
6174
6e3c9717
ACO
6175 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6176 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6177}
6178
0b8765c6 6179static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6180{
6181 struct drm_device *dev = crtc->dev;
a72e4c9f 6182 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6184 struct intel_encoder *encoder;
79e53945 6185 int pipe = intel_crtc->pipe;
79e53945 6186
53d9f4e9 6187 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6188 return;
6189
f13c2ef3
DV
6190 i9xx_set_pll_dividers(intel_crtc);
6191
6e3c9717 6192 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6193 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6194
6195 intel_set_pipe_timings(intel_crtc);
6196
5b18e57c
DV
6197 i9xx_set_pipeconf(intel_crtc);
6198
f7abfe8b 6199 intel_crtc->active = true;
6b383a7f 6200
4a3436e8 6201 if (!IS_GEN2(dev))
a72e4c9f 6202 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6203
9d6d9f19
MK
6204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 if (encoder->pre_enable)
6206 encoder->pre_enable(encoder);
6207
f6736a1a
DV
6208 i9xx_enable_pll(intel_crtc);
6209
2dd24552
JB
6210 i9xx_pfit_enable(intel_crtc);
6211
63cbb074
VS
6212 intel_crtc_load_lut(crtc);
6213
f37fcc2a 6214 intel_update_watermarks(crtc);
e1fdc473 6215 intel_enable_pipe(intel_crtc);
be6a6f8e 6216
4b3a9526
VS
6217 assert_vblank_disabled(crtc);
6218 drm_crtc_vblank_on(crtc);
6219
f9b61ff6
DV
6220 for_each_encoder_on_crtc(dev, crtc, encoder)
6221 encoder->enable(encoder);
0b8765c6 6222}
79e53945 6223
87476d63
DV
6224static void i9xx_pfit_disable(struct intel_crtc *crtc)
6225{
6226 struct drm_device *dev = crtc->base.dev;
6227 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6228
6e3c9717 6229 if (!crtc->config->gmch_pfit.control)
328d8e82 6230 return;
87476d63 6231
328d8e82 6232 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6233
328d8e82
DV
6234 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6235 I915_READ(PFIT_CONTROL));
6236 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6237}
6238
0b8765c6
JB
6239static void i9xx_crtc_disable(struct drm_crtc *crtc)
6240{
6241 struct drm_device *dev = crtc->dev;
6242 struct drm_i915_private *dev_priv = dev->dev_private;
6243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6244 struct intel_encoder *encoder;
0b8765c6 6245 int pipe = intel_crtc->pipe;
ef9c3aee 6246
6304cd91
VS
6247 /*
6248 * On gen2 planes are double buffered but the pipe isn't, so we must
6249 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6250 * We also need to wait on all gmch platforms because of the
6251 * self-refresh mode constraint explained above.
6304cd91 6252 */
564ed191 6253 intel_wait_for_vblank(dev, pipe);
6304cd91 6254
4b3a9526
VS
6255 for_each_encoder_on_crtc(dev, crtc, encoder)
6256 encoder->disable(encoder);
6257
f9b61ff6
DV
6258 drm_crtc_vblank_off(crtc);
6259 assert_vblank_disabled(crtc);
6260
575f7ab7 6261 intel_disable_pipe(intel_crtc);
24a1f16d 6262
87476d63 6263 i9xx_pfit_disable(intel_crtc);
24a1f16d 6264
89b667f8
JB
6265 for_each_encoder_on_crtc(dev, crtc, encoder)
6266 if (encoder->post_disable)
6267 encoder->post_disable(encoder);
6268
409ee761 6269 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6270 if (IS_CHERRYVIEW(dev))
6271 chv_disable_pll(dev_priv, pipe);
6272 else if (IS_VALLEYVIEW(dev))
6273 vlv_disable_pll(dev_priv, pipe);
6274 else
1c4e0274 6275 i9xx_disable_pll(intel_crtc);
076ed3b2 6276 }
0b8765c6 6277
d6db995f
VS
6278 for_each_encoder_on_crtc(dev, crtc, encoder)
6279 if (encoder->post_pll_disable)
6280 encoder->post_pll_disable(encoder);
6281
4a3436e8 6282 if (!IS_GEN2(dev))
a72e4c9f 6283 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6284}
6285
b17d48e2
ML
6286static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6287{
6288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6289 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6290 enum intel_display_power_domain domain;
6291 unsigned long domains;
6292
6293 if (!intel_crtc->active)
6294 return;
6295
a539205a 6296 if (to_intel_plane_state(crtc->primary->state)->visible) {
fc32b1fd
ML
6297 WARN_ON(intel_crtc->unpin_work);
6298
a539205a
ML
6299 intel_pre_disable_primary(crtc);
6300 }
6301
d032ffa0 6302 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
b17d48e2 6303 dev_priv->display.crtc_disable(crtc);
37d9078b
MR
6304 intel_crtc->active = false;
6305 intel_update_watermarks(crtc);
1f7457b1 6306 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6307
6308 domains = intel_crtc->enabled_power_domains;
6309 for_each_power_domain(domain, domains)
6310 intel_display_power_put(dev_priv, domain);
6311 intel_crtc->enabled_power_domains = 0;
6312}
6313
6b72d486
ML
6314/*
6315 * turn all crtc's off, but do not adjust state
6316 * This has to be paired with a call to intel_modeset_setup_hw_state.
6317 */
70e0bd74 6318int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6319{
70e0bd74
ML
6320 struct drm_mode_config *config = &dev->mode_config;
6321 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6322 struct drm_atomic_state *state;
6b72d486 6323 struct drm_crtc *crtc;
70e0bd74
ML
6324 unsigned crtc_mask = 0;
6325 int ret = 0;
6326
6327 if (WARN_ON(!ctx))
6328 return 0;
6329
6330 lockdep_assert_held(&ctx->ww_ctx);
6331 state = drm_atomic_state_alloc(dev);
6332 if (WARN_ON(!state))
6333 return -ENOMEM;
6334
6335 state->acquire_ctx = ctx;
6336 state->allow_modeset = true;
6337
6338 for_each_crtc(dev, crtc) {
6339 struct drm_crtc_state *crtc_state =
6340 drm_atomic_get_crtc_state(state, crtc);
6b72d486 6341
70e0bd74
ML
6342 ret = PTR_ERR_OR_ZERO(crtc_state);
6343 if (ret)
6344 goto free;
6345
6346 if (!crtc_state->active)
6347 continue;
6348
6349 crtc_state->active = false;
6350 crtc_mask |= 1 << drm_crtc_index(crtc);
6351 }
6352
6353 if (crtc_mask) {
74c090b1 6354 ret = drm_atomic_commit(state);
70e0bd74
ML
6355
6356 if (!ret) {
6357 for_each_crtc(dev, crtc)
6358 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6359 crtc->state->active = true;
6360
6361 return ret;
6362 }
6363 }
6364
6365free:
6366 if (ret)
6367 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6368 drm_atomic_state_free(state);
6369 return ret;
ee7b9f93
JB
6370}
6371
ea5b213a 6372void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6373{
4ef69c7a 6374 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6375
ea5b213a
CW
6376 drm_encoder_cleanup(encoder);
6377 kfree(intel_encoder);
7e7d76c3
JB
6378}
6379
0a91ca29
DV
6380/* Cross check the actual hw state with our own modeset state tracking (and it's
6381 * internal consistency). */
b980514c 6382static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6383{
35dd3c64
ML
6384 struct drm_crtc *crtc = connector->base.state->crtc;
6385
6386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6387 connector->base.base.id,
6388 connector->base.name);
6389
0a91ca29 6390 if (connector->get_hw_state(connector)) {
e85376cb 6391 struct intel_encoder *encoder = connector->encoder;
35dd3c64 6392 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6393
35dd3c64
ML
6394 I915_STATE_WARN(!crtc,
6395 "connector enabled without attached crtc\n");
0a91ca29 6396
35dd3c64
ML
6397 if (!crtc)
6398 return;
6399
6400 I915_STATE_WARN(!crtc->state->active,
6401 "connector is active, but attached crtc isn't\n");
6402
e85376cb 6403 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
6404 return;
6405
e85376cb 6406 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
6407 "atomic encoder doesn't match attached encoder\n");
6408
e85376cb 6409 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
6410 "attached encoder crtc differs from connector crtc\n");
6411 } else {
4d688a2a
ML
6412 I915_STATE_WARN(crtc && crtc->state->active,
6413 "attached crtc is active, but connector isn't\n");
35dd3c64
ML
6414 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6415 "best encoder set without crtc!\n");
0a91ca29 6416 }
79e53945
JB
6417}
6418
08d9bc92
ACO
6419int intel_connector_init(struct intel_connector *connector)
6420{
6421 struct drm_connector_state *connector_state;
6422
6423 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6424 if (!connector_state)
6425 return -ENOMEM;
6426
6427 connector->base.state = connector_state;
6428 return 0;
6429}
6430
6431struct intel_connector *intel_connector_alloc(void)
6432{
6433 struct intel_connector *connector;
6434
6435 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6436 if (!connector)
6437 return NULL;
6438
6439 if (intel_connector_init(connector) < 0) {
6440 kfree(connector);
6441 return NULL;
6442 }
6443
6444 return connector;
6445}
6446
f0947c37
DV
6447/* Simple connector->get_hw_state implementation for encoders that support only
6448 * one connector and no cloning and hence the encoder state determines the state
6449 * of the connector. */
6450bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6451{
24929352 6452 enum pipe pipe = 0;
f0947c37 6453 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6454
f0947c37 6455 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6456}
6457
6d293983 6458static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6459{
6d293983
ACO
6460 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6461 return crtc_state->fdi_lanes;
d272ddfa
VS
6462
6463 return 0;
6464}
6465
6d293983 6466static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6467 struct intel_crtc_state *pipe_config)
1857e1da 6468{
6d293983
ACO
6469 struct drm_atomic_state *state = pipe_config->base.state;
6470 struct intel_crtc *other_crtc;
6471 struct intel_crtc_state *other_crtc_state;
6472
1857e1da
DV
6473 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6474 pipe_name(pipe), pipe_config->fdi_lanes);
6475 if (pipe_config->fdi_lanes > 4) {
6476 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6477 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6478 return -EINVAL;
1857e1da
DV
6479 }
6480
bafb6553 6481 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6482 if (pipe_config->fdi_lanes > 2) {
6483 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6484 pipe_config->fdi_lanes);
6d293983 6485 return -EINVAL;
1857e1da 6486 } else {
6d293983 6487 return 0;
1857e1da
DV
6488 }
6489 }
6490
6491 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6492 return 0;
1857e1da
DV
6493
6494 /* Ivybridge 3 pipe is really complicated */
6495 switch (pipe) {
6496 case PIPE_A:
6d293983 6497 return 0;
1857e1da 6498 case PIPE_B:
6d293983
ACO
6499 if (pipe_config->fdi_lanes <= 2)
6500 return 0;
6501
6502 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6503 other_crtc_state =
6504 intel_atomic_get_crtc_state(state, other_crtc);
6505 if (IS_ERR(other_crtc_state))
6506 return PTR_ERR(other_crtc_state);
6507
6508 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6509 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6510 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6511 return -EINVAL;
1857e1da 6512 }
6d293983 6513 return 0;
1857e1da 6514 case PIPE_C:
251cc67c
VS
6515 if (pipe_config->fdi_lanes > 2) {
6516 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6517 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6518 return -EINVAL;
251cc67c 6519 }
6d293983
ACO
6520
6521 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6522 other_crtc_state =
6523 intel_atomic_get_crtc_state(state, other_crtc);
6524 if (IS_ERR(other_crtc_state))
6525 return PTR_ERR(other_crtc_state);
6526
6527 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6528 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6529 return -EINVAL;
1857e1da 6530 }
6d293983 6531 return 0;
1857e1da
DV
6532 default:
6533 BUG();
6534 }
6535}
6536
e29c22c0
DV
6537#define RETRY 1
6538static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6539 struct intel_crtc_state *pipe_config)
877d48d5 6540{
1857e1da 6541 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 6542 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6543 int lane, link_bw, fdi_dotclock, ret;
6544 bool needs_recompute = false;
877d48d5 6545
e29c22c0 6546retry:
877d48d5
DV
6547 /* FDI is a binary signal running at ~2.7GHz, encoding
6548 * each output octet as 10 bits. The actual frequency
6549 * is stored as a divider into a 100MHz clock, and the
6550 * mode pixel clock is stored in units of 1KHz.
6551 * Hence the bw of each lane in terms of the mode signal
6552 * is:
6553 */
6554 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6555
241bfc38 6556 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6557
2bd89a07 6558 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6559 pipe_config->pipe_bpp);
6560
6561 pipe_config->fdi_lanes = lane;
6562
2bd89a07 6563 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6564 link_bw, &pipe_config->fdi_m_n);
1857e1da 6565
6d293983
ACO
6566 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6567 intel_crtc->pipe, pipe_config);
6568 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6569 pipe_config->pipe_bpp -= 2*3;
6570 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6571 pipe_config->pipe_bpp);
6572 needs_recompute = true;
6573 pipe_config->bw_constrained = true;
6574
6575 goto retry;
6576 }
6577
6578 if (needs_recompute)
6579 return RETRY;
6580
6d293983 6581 return ret;
877d48d5
DV
6582}
6583
8cfb3407
VS
6584static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6585 struct intel_crtc_state *pipe_config)
6586{
6587 if (pipe_config->pipe_bpp > 24)
6588 return false;
6589
6590 /* HSW can handle pixel rate up to cdclk? */
6591 if (IS_HASWELL(dev_priv->dev))
6592 return true;
6593
6594 /*
b432e5cf
VS
6595 * We compare against max which means we must take
6596 * the increased cdclk requirement into account when
6597 * calculating the new cdclk.
6598 *
6599 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6600 */
6601 return ilk_pipe_pixel_rate(pipe_config) <=
6602 dev_priv->max_cdclk_freq * 95 / 100;
6603}
6604
42db64ef 6605static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6606 struct intel_crtc_state *pipe_config)
42db64ef 6607{
8cfb3407
VS
6608 struct drm_device *dev = crtc->base.dev;
6609 struct drm_i915_private *dev_priv = dev->dev_private;
6610
d330a953 6611 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6612 hsw_crtc_supports_ips(crtc) &&
6613 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6614}
6615
39acb4aa
VS
6616static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6617{
6618 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6619
6620 /* GDG double wide on either pipe, otherwise pipe A only */
6621 return INTEL_INFO(dev_priv)->gen < 4 &&
6622 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6623}
6624
a43f6e0f 6625static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6626 struct intel_crtc_state *pipe_config)
79e53945 6627{
a43f6e0f 6628 struct drm_device *dev = crtc->base.dev;
8bd31e67 6629 struct drm_i915_private *dev_priv = dev->dev_private;
7c5f93b0 6630 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6631
ad3a4479 6632 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6633 if (INTEL_INFO(dev)->gen < 4) {
39acb4aa 6634 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
6635
6636 /*
39acb4aa 6637 * Enable double wide mode when the dot clock
cf532bb2 6638 * is > 90% of the (display) core speed.
cf532bb2 6639 */
39acb4aa
VS
6640 if (intel_crtc_supports_double_wide(crtc) &&
6641 adjusted_mode->crtc_clock > clock_limit) {
ad3a4479 6642 clock_limit *= 2;
cf532bb2 6643 pipe_config->double_wide = true;
ad3a4479
VS
6644 }
6645
39acb4aa
VS
6646 if (adjusted_mode->crtc_clock > clock_limit) {
6647 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6648 adjusted_mode->crtc_clock, clock_limit,
6649 yesno(pipe_config->double_wide));
e29c22c0 6650 return -EINVAL;
39acb4aa 6651 }
2c07245f 6652 }
89749350 6653
1d1d0e27
VS
6654 /*
6655 * Pipe horizontal size must be even in:
6656 * - DVO ganged mode
6657 * - LVDS dual channel mode
6658 * - Double wide pipe
6659 */
a93e255f 6660 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6661 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6662 pipe_config->pipe_src_w &= ~1;
6663
8693a824
DL
6664 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6665 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6666 */
6667 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
aad941d5 6668 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 6669 return -EINVAL;
44f46b42 6670
f5adf94e 6671 if (HAS_IPS(dev))
a43f6e0f
DV
6672 hsw_compute_ips_config(crtc, pipe_config);
6673
877d48d5 6674 if (pipe_config->has_pch_encoder)
a43f6e0f 6675 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6676
cf5a15be 6677 return 0;
79e53945
JB
6678}
6679
1652d19e
VS
6680static int skylake_get_display_clock_speed(struct drm_device *dev)
6681{
6682 struct drm_i915_private *dev_priv = to_i915(dev);
6683 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6684 uint32_t cdctl = I915_READ(CDCLK_CTL);
6685 uint32_t linkrate;
6686
414355a7 6687 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6688 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6689
6690 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6691 return 540000;
6692
6693 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6694 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6695
71cd8423
DL
6696 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6697 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6698 /* vco 8640 */
6699 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6700 case CDCLK_FREQ_450_432:
6701 return 432000;
6702 case CDCLK_FREQ_337_308:
6703 return 308570;
6704 case CDCLK_FREQ_675_617:
6705 return 617140;
6706 default:
6707 WARN(1, "Unknown cd freq selection\n");
6708 }
6709 } else {
6710 /* vco 8100 */
6711 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6712 case CDCLK_FREQ_450_432:
6713 return 450000;
6714 case CDCLK_FREQ_337_308:
6715 return 337500;
6716 case CDCLK_FREQ_675_617:
6717 return 675000;
6718 default:
6719 WARN(1, "Unknown cd freq selection\n");
6720 }
6721 }
6722
6723 /* error case, do as if DPLL0 isn't enabled */
6724 return 24000;
6725}
6726
acd3f3d3
BP
6727static int broxton_get_display_clock_speed(struct drm_device *dev)
6728{
6729 struct drm_i915_private *dev_priv = to_i915(dev);
6730 uint32_t cdctl = I915_READ(CDCLK_CTL);
6731 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6732 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6733 int cdclk;
6734
6735 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6736 return 19200;
6737
6738 cdclk = 19200 * pll_ratio / 2;
6739
6740 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6741 case BXT_CDCLK_CD2X_DIV_SEL_1:
6742 return cdclk; /* 576MHz or 624MHz */
6743 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6744 return cdclk * 2 / 3; /* 384MHz */
6745 case BXT_CDCLK_CD2X_DIV_SEL_2:
6746 return cdclk / 2; /* 288MHz */
6747 case BXT_CDCLK_CD2X_DIV_SEL_4:
6748 return cdclk / 4; /* 144MHz */
6749 }
6750
6751 /* error case, do as if DE PLL isn't enabled */
6752 return 19200;
6753}
6754
1652d19e
VS
6755static int broadwell_get_display_clock_speed(struct drm_device *dev)
6756{
6757 struct drm_i915_private *dev_priv = dev->dev_private;
6758 uint32_t lcpll = I915_READ(LCPLL_CTL);
6759 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6760
6761 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6762 return 800000;
6763 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6764 return 450000;
6765 else if (freq == LCPLL_CLK_FREQ_450)
6766 return 450000;
6767 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6768 return 540000;
6769 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6770 return 337500;
6771 else
6772 return 675000;
6773}
6774
6775static int haswell_get_display_clock_speed(struct drm_device *dev)
6776{
6777 struct drm_i915_private *dev_priv = dev->dev_private;
6778 uint32_t lcpll = I915_READ(LCPLL_CTL);
6779 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6780
6781 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6782 return 800000;
6783 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6784 return 450000;
6785 else if (freq == LCPLL_CLK_FREQ_450)
6786 return 450000;
6787 else if (IS_HSW_ULT(dev))
6788 return 337500;
6789 else
6790 return 540000;
79e53945
JB
6791}
6792
25eb05fc
JB
6793static int valleyview_get_display_clock_speed(struct drm_device *dev)
6794{
bfa7df01
VS
6795 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6796 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
6797}
6798
b37a6434
VS
6799static int ilk_get_display_clock_speed(struct drm_device *dev)
6800{
6801 return 450000;
6802}
6803
e70236a8
JB
6804static int i945_get_display_clock_speed(struct drm_device *dev)
6805{
6806 return 400000;
6807}
79e53945 6808
e70236a8 6809static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6810{
e907f170 6811 return 333333;
e70236a8 6812}
79e53945 6813
e70236a8
JB
6814static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6815{
6816 return 200000;
6817}
79e53945 6818
257a7ffc
DV
6819static int pnv_get_display_clock_speed(struct drm_device *dev)
6820{
6821 u16 gcfgc = 0;
6822
6823 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6824
6825 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6826 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6827 return 266667;
257a7ffc 6828 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6829 return 333333;
257a7ffc 6830 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6831 return 444444;
257a7ffc
DV
6832 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6833 return 200000;
6834 default:
6835 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6836 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6837 return 133333;
257a7ffc 6838 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6839 return 166667;
257a7ffc
DV
6840 }
6841}
6842
e70236a8
JB
6843static int i915gm_get_display_clock_speed(struct drm_device *dev)
6844{
6845 u16 gcfgc = 0;
79e53945 6846
e70236a8
JB
6847 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6848
6849 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6850 return 133333;
e70236a8
JB
6851 else {
6852 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6853 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6854 return 333333;
e70236a8
JB
6855 default:
6856 case GC_DISPLAY_CLOCK_190_200_MHZ:
6857 return 190000;
79e53945 6858 }
e70236a8
JB
6859 }
6860}
6861
6862static int i865_get_display_clock_speed(struct drm_device *dev)
6863{
e907f170 6864 return 266667;
e70236a8
JB
6865}
6866
1b1d2716 6867static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6868{
6869 u16 hpllcc = 0;
1b1d2716 6870
65cd2b3f
VS
6871 /*
6872 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6873 * encoding is different :(
6874 * FIXME is this the right way to detect 852GM/852GMV?
6875 */
6876 if (dev->pdev->revision == 0x1)
6877 return 133333;
6878
1b1d2716
VS
6879 pci_bus_read_config_word(dev->pdev->bus,
6880 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6881
e70236a8
JB
6882 /* Assume that the hardware is in the high speed state. This
6883 * should be the default.
6884 */
6885 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6886 case GC_CLOCK_133_200:
1b1d2716 6887 case GC_CLOCK_133_200_2:
e70236a8
JB
6888 case GC_CLOCK_100_200:
6889 return 200000;
6890 case GC_CLOCK_166_250:
6891 return 250000;
6892 case GC_CLOCK_100_133:
e907f170 6893 return 133333;
1b1d2716
VS
6894 case GC_CLOCK_133_266:
6895 case GC_CLOCK_133_266_2:
6896 case GC_CLOCK_166_266:
6897 return 266667;
e70236a8 6898 }
79e53945 6899
e70236a8
JB
6900 /* Shouldn't happen */
6901 return 0;
6902}
79e53945 6903
e70236a8
JB
6904static int i830_get_display_clock_speed(struct drm_device *dev)
6905{
e907f170 6906 return 133333;
79e53945
JB
6907}
6908
34edce2f
VS
6909static unsigned int intel_hpll_vco(struct drm_device *dev)
6910{
6911 struct drm_i915_private *dev_priv = dev->dev_private;
6912 static const unsigned int blb_vco[8] = {
6913 [0] = 3200000,
6914 [1] = 4000000,
6915 [2] = 5333333,
6916 [3] = 4800000,
6917 [4] = 6400000,
6918 };
6919 static const unsigned int pnv_vco[8] = {
6920 [0] = 3200000,
6921 [1] = 4000000,
6922 [2] = 5333333,
6923 [3] = 4800000,
6924 [4] = 2666667,
6925 };
6926 static const unsigned int cl_vco[8] = {
6927 [0] = 3200000,
6928 [1] = 4000000,
6929 [2] = 5333333,
6930 [3] = 6400000,
6931 [4] = 3333333,
6932 [5] = 3566667,
6933 [6] = 4266667,
6934 };
6935 static const unsigned int elk_vco[8] = {
6936 [0] = 3200000,
6937 [1] = 4000000,
6938 [2] = 5333333,
6939 [3] = 4800000,
6940 };
6941 static const unsigned int ctg_vco[8] = {
6942 [0] = 3200000,
6943 [1] = 4000000,
6944 [2] = 5333333,
6945 [3] = 6400000,
6946 [4] = 2666667,
6947 [5] = 4266667,
6948 };
6949 const unsigned int *vco_table;
6950 unsigned int vco;
6951 uint8_t tmp = 0;
6952
6953 /* FIXME other chipsets? */
6954 if (IS_GM45(dev))
6955 vco_table = ctg_vco;
6956 else if (IS_G4X(dev))
6957 vco_table = elk_vco;
6958 else if (IS_CRESTLINE(dev))
6959 vco_table = cl_vco;
6960 else if (IS_PINEVIEW(dev))
6961 vco_table = pnv_vco;
6962 else if (IS_G33(dev))
6963 vco_table = blb_vco;
6964 else
6965 return 0;
6966
6967 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6968
6969 vco = vco_table[tmp & 0x7];
6970 if (vco == 0)
6971 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6972 else
6973 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6974
6975 return vco;
6976}
6977
6978static int gm45_get_display_clock_speed(struct drm_device *dev)
6979{
6980 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6981 uint16_t tmp = 0;
6982
6983 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6984
6985 cdclk_sel = (tmp >> 12) & 0x1;
6986
6987 switch (vco) {
6988 case 2666667:
6989 case 4000000:
6990 case 5333333:
6991 return cdclk_sel ? 333333 : 222222;
6992 case 3200000:
6993 return cdclk_sel ? 320000 : 228571;
6994 default:
6995 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6996 return 222222;
6997 }
6998}
6999
7000static int i965gm_get_display_clock_speed(struct drm_device *dev)
7001{
7002 static const uint8_t div_3200[] = { 16, 10, 8 };
7003 static const uint8_t div_4000[] = { 20, 12, 10 };
7004 static const uint8_t div_5333[] = { 24, 16, 14 };
7005 const uint8_t *div_table;
7006 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7007 uint16_t tmp = 0;
7008
7009 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7010
7011 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7012
7013 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7014 goto fail;
7015
7016 switch (vco) {
7017 case 3200000:
7018 div_table = div_3200;
7019 break;
7020 case 4000000:
7021 div_table = div_4000;
7022 break;
7023 case 5333333:
7024 div_table = div_5333;
7025 break;
7026 default:
7027 goto fail;
7028 }
7029
7030 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7031
caf4e252 7032fail:
34edce2f
VS
7033 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7034 return 200000;
7035}
7036
7037static int g33_get_display_clock_speed(struct drm_device *dev)
7038{
7039 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7040 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7041 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7042 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7043 const uint8_t *div_table;
7044 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7045 uint16_t tmp = 0;
7046
7047 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7048
7049 cdclk_sel = (tmp >> 4) & 0x7;
7050
7051 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7052 goto fail;
7053
7054 switch (vco) {
7055 case 3200000:
7056 div_table = div_3200;
7057 break;
7058 case 4000000:
7059 div_table = div_4000;
7060 break;
7061 case 4800000:
7062 div_table = div_4800;
7063 break;
7064 case 5333333:
7065 div_table = div_5333;
7066 break;
7067 default:
7068 goto fail;
7069 }
7070
7071 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7072
caf4e252 7073fail:
34edce2f
VS
7074 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7075 return 190476;
7076}
7077
2c07245f 7078static void
a65851af 7079intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7080{
a65851af
VS
7081 while (*num > DATA_LINK_M_N_MASK ||
7082 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7083 *num >>= 1;
7084 *den >>= 1;
7085 }
7086}
7087
a65851af
VS
7088static void compute_m_n(unsigned int m, unsigned int n,
7089 uint32_t *ret_m, uint32_t *ret_n)
7090{
7091 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7092 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7093 intel_reduce_m_n_ratio(ret_m, ret_n);
7094}
7095
e69d0bc1
DV
7096void
7097intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7098 int pixel_clock, int link_clock,
7099 struct intel_link_m_n *m_n)
2c07245f 7100{
e69d0bc1 7101 m_n->tu = 64;
a65851af
VS
7102
7103 compute_m_n(bits_per_pixel * pixel_clock,
7104 link_clock * nlanes * 8,
7105 &m_n->gmch_m, &m_n->gmch_n);
7106
7107 compute_m_n(pixel_clock, link_clock,
7108 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7109}
7110
a7615030
CW
7111static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7112{
d330a953
JN
7113 if (i915.panel_use_ssc >= 0)
7114 return i915.panel_use_ssc != 0;
41aa3448 7115 return dev_priv->vbt.lvds_use_ssc
435793df 7116 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7117}
7118
a93e255f
ACO
7119static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7120 int num_connectors)
c65d77d8 7121{
a93e255f 7122 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7123 struct drm_i915_private *dev_priv = dev->dev_private;
7124 int refclk;
7125
a93e255f
ACO
7126 WARN_ON(!crtc_state->base.state);
7127
5ab7b0b7 7128 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7129 refclk = 100000;
a93e255f 7130 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7131 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7132 refclk = dev_priv->vbt.lvds_ssc_freq;
7133 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7134 } else if (!IS_GEN2(dev)) {
7135 refclk = 96000;
7136 } else {
7137 refclk = 48000;
7138 }
7139
7140 return refclk;
7141}
7142
7429e9d4 7143static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7144{
7df00d7a 7145 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7146}
f47709a9 7147
7429e9d4
DV
7148static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7149{
7150 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7151}
7152
f47709a9 7153static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7154 struct intel_crtc_state *crtc_state,
a7516a05
JB
7155 intel_clock_t *reduced_clock)
7156{
f47709a9 7157 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7158 u32 fp, fp2 = 0;
7159
7160 if (IS_PINEVIEW(dev)) {
190f68c5 7161 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7162 if (reduced_clock)
7429e9d4 7163 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7164 } else {
190f68c5 7165 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7166 if (reduced_clock)
7429e9d4 7167 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7168 }
7169
190f68c5 7170 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7171
f47709a9 7172 crtc->lowfreq_avail = false;
a93e255f 7173 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7174 reduced_clock) {
190f68c5 7175 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7176 crtc->lowfreq_avail = true;
a7516a05 7177 } else {
190f68c5 7178 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7179 }
7180}
7181
5e69f97f
CML
7182static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7183 pipe)
89b667f8
JB
7184{
7185 u32 reg_val;
7186
7187 /*
7188 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7189 * and set it to a reasonable value instead.
7190 */
ab3c759a 7191 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7192 reg_val &= 0xffffff00;
7193 reg_val |= 0x00000030;
ab3c759a 7194 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7195
ab3c759a 7196 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7197 reg_val &= 0x8cffffff;
7198 reg_val = 0x8c000000;
ab3c759a 7199 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7200
ab3c759a 7201 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7202 reg_val &= 0xffffff00;
ab3c759a 7203 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7204
ab3c759a 7205 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7206 reg_val &= 0x00ffffff;
7207 reg_val |= 0xb0000000;
ab3c759a 7208 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7209}
7210
b551842d
DV
7211static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7212 struct intel_link_m_n *m_n)
7213{
7214 struct drm_device *dev = crtc->base.dev;
7215 struct drm_i915_private *dev_priv = dev->dev_private;
7216 int pipe = crtc->pipe;
7217
e3b95f1e
DV
7218 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7219 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7220 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7221 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7222}
7223
7224static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7225 struct intel_link_m_n *m_n,
7226 struct intel_link_m_n *m2_n2)
b551842d
DV
7227{
7228 struct drm_device *dev = crtc->base.dev;
7229 struct drm_i915_private *dev_priv = dev->dev_private;
7230 int pipe = crtc->pipe;
6e3c9717 7231 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7232
7233 if (INTEL_INFO(dev)->gen >= 5) {
7234 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7235 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7236 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7237 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7238 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7239 * for gen < 8) and if DRRS is supported (to make sure the
7240 * registers are not unnecessarily accessed).
7241 */
44395bfe 7242 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7243 crtc->config->has_drrs) {
f769cd24
VK
7244 I915_WRITE(PIPE_DATA_M2(transcoder),
7245 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7246 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7247 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7248 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7249 }
b551842d 7250 } else {
e3b95f1e
DV
7251 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7252 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7253 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7254 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7255 }
7256}
7257
fe3cd48d 7258void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7259{
fe3cd48d
R
7260 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7261
7262 if (m_n == M1_N1) {
7263 dp_m_n = &crtc->config->dp_m_n;
7264 dp_m2_n2 = &crtc->config->dp_m2_n2;
7265 } else if (m_n == M2_N2) {
7266
7267 /*
7268 * M2_N2 registers are not supported. Hence m2_n2 divider value
7269 * needs to be programmed into M1_N1.
7270 */
7271 dp_m_n = &crtc->config->dp_m2_n2;
7272 } else {
7273 DRM_ERROR("Unsupported divider value\n");
7274 return;
7275 }
7276
6e3c9717
ACO
7277 if (crtc->config->has_pch_encoder)
7278 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7279 else
fe3cd48d 7280 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7281}
7282
251ac862
DV
7283static void vlv_compute_dpll(struct intel_crtc *crtc,
7284 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7285{
7286 u32 dpll, dpll_md;
7287
7288 /*
7289 * Enable DPIO clock input. We should never disable the reference
7290 * clock for pipe B, since VGA hotplug / manual detection depends
7291 * on it.
7292 */
60bfe44f
VS
7293 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7294 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
bdd4b6a6
DV
7295 /* We should never disable this, set it here for state tracking */
7296 if (crtc->pipe == PIPE_B)
7297 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7298 dpll |= DPLL_VCO_ENABLE;
d288f65f 7299 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7300
d288f65f 7301 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7302 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7303 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7304}
7305
d288f65f 7306static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7307 const struct intel_crtc_state *pipe_config)
a0c4da24 7308{
f47709a9 7309 struct drm_device *dev = crtc->base.dev;
a0c4da24 7310 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7311 int pipe = crtc->pipe;
bdd4b6a6 7312 u32 mdiv;
a0c4da24 7313 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7314 u32 coreclk, reg_val;
a0c4da24 7315
a580516d 7316 mutex_lock(&dev_priv->sb_lock);
09153000 7317
d288f65f
VS
7318 bestn = pipe_config->dpll.n;
7319 bestm1 = pipe_config->dpll.m1;
7320 bestm2 = pipe_config->dpll.m2;
7321 bestp1 = pipe_config->dpll.p1;
7322 bestp2 = pipe_config->dpll.p2;
a0c4da24 7323
89b667f8
JB
7324 /* See eDP HDMI DPIO driver vbios notes doc */
7325
7326 /* PLL B needs special handling */
bdd4b6a6 7327 if (pipe == PIPE_B)
5e69f97f 7328 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7329
7330 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7331 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7332
7333 /* Disable target IRef on PLL */
ab3c759a 7334 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7335 reg_val &= 0x00ffffff;
ab3c759a 7336 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7337
7338 /* Disable fast lock */
ab3c759a 7339 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7340
7341 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7342 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7343 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7344 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7345 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7346
7347 /*
7348 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7349 * but we don't support that).
7350 * Note: don't use the DAC post divider as it seems unstable.
7351 */
7352 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7354
a0c4da24 7355 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7357
89b667f8 7358 /* Set HBR and RBR LPF coefficients */
d288f65f 7359 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7360 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7361 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7363 0x009f0003);
89b667f8 7364 else
ab3c759a 7365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7366 0x00d0000f);
7367
681a8504 7368 if (pipe_config->has_dp_encoder) {
89b667f8 7369 /* Use SSC source */
bdd4b6a6 7370 if (pipe == PIPE_A)
ab3c759a 7371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7372 0x0df40000);
7373 else
ab3c759a 7374 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7375 0x0df70000);
7376 } else { /* HDMI or VGA */
7377 /* Use bend source */
bdd4b6a6 7378 if (pipe == PIPE_A)
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7380 0x0df70000);
7381 else
ab3c759a 7382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7383 0x0df40000);
7384 }
a0c4da24 7385
ab3c759a 7386 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7387 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7388 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7389 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7390 coreclk |= 0x01000000;
ab3c759a 7391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7392
ab3c759a 7393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7394 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7395}
7396
251ac862
DV
7397static void chv_compute_dpll(struct intel_crtc *crtc,
7398 struct intel_crtc_state *pipe_config)
1ae0d137 7399{
60bfe44f
VS
7400 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7401 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
1ae0d137
VS
7402 DPLL_VCO_ENABLE;
7403 if (crtc->pipe != PIPE_A)
d288f65f 7404 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7405
d288f65f
VS
7406 pipe_config->dpll_hw_state.dpll_md =
7407 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7408}
7409
d288f65f 7410static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7411 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7412{
7413 struct drm_device *dev = crtc->base.dev;
7414 struct drm_i915_private *dev_priv = dev->dev_private;
7415 int pipe = crtc->pipe;
7416 int dpll_reg = DPLL(crtc->pipe);
7417 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7418 u32 loopfilter, tribuf_calcntr;
9d556c99 7419 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7420 u32 dpio_val;
9cbe40c1 7421 int vco;
9d556c99 7422
d288f65f
VS
7423 bestn = pipe_config->dpll.n;
7424 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7425 bestm1 = pipe_config->dpll.m1;
7426 bestm2 = pipe_config->dpll.m2 >> 22;
7427 bestp1 = pipe_config->dpll.p1;
7428 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7429 vco = pipe_config->dpll.vco;
a945ce7e 7430 dpio_val = 0;
9cbe40c1 7431 loopfilter = 0;
9d556c99
CML
7432
7433 /*
7434 * Enable Refclk and SSC
7435 */
a11b0703 7436 I915_WRITE(dpll_reg,
d288f65f 7437 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7438
a580516d 7439 mutex_lock(&dev_priv->sb_lock);
9d556c99 7440
9d556c99
CML
7441 /* p1 and p2 divider */
7442 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7443 5 << DPIO_CHV_S1_DIV_SHIFT |
7444 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7445 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7446 1 << DPIO_CHV_K_DIV_SHIFT);
7447
7448 /* Feedback post-divider - m2 */
7449 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7450
7451 /* Feedback refclk divider - n and m1 */
7452 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7453 DPIO_CHV_M1_DIV_BY_2 |
7454 1 << DPIO_CHV_N_DIV_SHIFT);
7455
7456 /* M2 fraction division */
25a25dfc 7457 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7458
7459 /* M2 fraction division enable */
a945ce7e
VP
7460 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7461 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7462 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7463 if (bestm2_frac)
7464 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7465 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7466
de3a0fde
VP
7467 /* Program digital lock detect threshold */
7468 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7469 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7470 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7471 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7472 if (!bestm2_frac)
7473 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7474 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7475
9d556c99 7476 /* Loop filter */
9cbe40c1
VP
7477 if (vco == 5400000) {
7478 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7479 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7480 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7481 tribuf_calcntr = 0x9;
7482 } else if (vco <= 6200000) {
7483 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7484 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7485 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7486 tribuf_calcntr = 0x9;
7487 } else if (vco <= 6480000) {
7488 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7489 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7490 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7491 tribuf_calcntr = 0x8;
7492 } else {
7493 /* Not supported. Apply the same limits as in the max case */
7494 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7495 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7496 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7497 tribuf_calcntr = 0;
7498 }
9d556c99
CML
7499 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7500
968040b2 7501 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7502 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7503 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7504 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7505
9d556c99
CML
7506 /* AFC Recal */
7507 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7508 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7509 DPIO_AFC_RECAL);
7510
a580516d 7511 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7512}
7513
d288f65f
VS
7514/**
7515 * vlv_force_pll_on - forcibly enable just the PLL
7516 * @dev_priv: i915 private structure
7517 * @pipe: pipe PLL to enable
7518 * @dpll: PLL configuration
7519 *
7520 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7521 * in cases where we need the PLL enabled even when @pipe is not going to
7522 * be enabled.
7523 */
7524void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7525 const struct dpll *dpll)
7526{
7527 struct intel_crtc *crtc =
7528 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7529 struct intel_crtc_state pipe_config = {
a93e255f 7530 .base.crtc = &crtc->base,
d288f65f
VS
7531 .pixel_multiplier = 1,
7532 .dpll = *dpll,
7533 };
7534
7535 if (IS_CHERRYVIEW(dev)) {
251ac862 7536 chv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7537 chv_prepare_pll(crtc, &pipe_config);
7538 chv_enable_pll(crtc, &pipe_config);
7539 } else {
251ac862 7540 vlv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7541 vlv_prepare_pll(crtc, &pipe_config);
7542 vlv_enable_pll(crtc, &pipe_config);
7543 }
7544}
7545
7546/**
7547 * vlv_force_pll_off - forcibly disable just the PLL
7548 * @dev_priv: i915 private structure
7549 * @pipe: pipe PLL to disable
7550 *
7551 * Disable the PLL for @pipe. To be used in cases where we need
7552 * the PLL enabled even when @pipe is not going to be enabled.
7553 */
7554void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7555{
7556 if (IS_CHERRYVIEW(dev))
7557 chv_disable_pll(to_i915(dev), pipe);
7558 else
7559 vlv_disable_pll(to_i915(dev), pipe);
7560}
7561
251ac862
DV
7562static void i9xx_compute_dpll(struct intel_crtc *crtc,
7563 struct intel_crtc_state *crtc_state,
7564 intel_clock_t *reduced_clock,
7565 int num_connectors)
eb1cbe48 7566{
f47709a9 7567 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7568 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7569 u32 dpll;
7570 bool is_sdvo;
190f68c5 7571 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7572
190f68c5 7573 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7574
a93e255f
ACO
7575 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7576 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7577
7578 dpll = DPLL_VGA_MODE_DIS;
7579
a93e255f 7580 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7581 dpll |= DPLLB_MODE_LVDS;
7582 else
7583 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7584
ef1b460d 7585 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7586 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7587 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7588 }
198a037f
DV
7589
7590 if (is_sdvo)
4a33e48d 7591 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7592
190f68c5 7593 if (crtc_state->has_dp_encoder)
4a33e48d 7594 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7595
7596 /* compute bitmask from p1 value */
7597 if (IS_PINEVIEW(dev))
7598 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7599 else {
7600 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7601 if (IS_G4X(dev) && reduced_clock)
7602 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7603 }
7604 switch (clock->p2) {
7605 case 5:
7606 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7607 break;
7608 case 7:
7609 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7610 break;
7611 case 10:
7612 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7613 break;
7614 case 14:
7615 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7616 break;
7617 }
7618 if (INTEL_INFO(dev)->gen >= 4)
7619 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7620
190f68c5 7621 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7622 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7623 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7624 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7625 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7626 else
7627 dpll |= PLL_REF_INPUT_DREFCLK;
7628
7629 dpll |= DPLL_VCO_ENABLE;
190f68c5 7630 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7631
eb1cbe48 7632 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7633 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7634 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7635 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7636 }
7637}
7638
251ac862
DV
7639static void i8xx_compute_dpll(struct intel_crtc *crtc,
7640 struct intel_crtc_state *crtc_state,
7641 intel_clock_t *reduced_clock,
7642 int num_connectors)
eb1cbe48 7643{
f47709a9 7644 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7645 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7646 u32 dpll;
190f68c5 7647 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7648
190f68c5 7649 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7650
eb1cbe48
DV
7651 dpll = DPLL_VGA_MODE_DIS;
7652
a93e255f 7653 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7654 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7655 } else {
7656 if (clock->p1 == 2)
7657 dpll |= PLL_P1_DIVIDE_BY_TWO;
7658 else
7659 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7660 if (clock->p2 == 4)
7661 dpll |= PLL_P2_DIVIDE_BY_4;
7662 }
7663
a93e255f 7664 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7665 dpll |= DPLL_DVO_2X_MODE;
7666
a93e255f 7667 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7668 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7669 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7670 else
7671 dpll |= PLL_REF_INPUT_DREFCLK;
7672
7673 dpll |= DPLL_VCO_ENABLE;
190f68c5 7674 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7675}
7676
8a654f3b 7677static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7678{
7679 struct drm_device *dev = intel_crtc->base.dev;
7680 struct drm_i915_private *dev_priv = dev->dev_private;
7681 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7682 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 7683 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7684 uint32_t crtc_vtotal, crtc_vblank_end;
7685 int vsyncshift = 0;
4d8a62ea
DV
7686
7687 /* We need to be careful not to changed the adjusted mode, for otherwise
7688 * the hw state checker will get angry at the mismatch. */
7689 crtc_vtotal = adjusted_mode->crtc_vtotal;
7690 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7691
609aeaca 7692 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7693 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7694 crtc_vtotal -= 1;
7695 crtc_vblank_end -= 1;
609aeaca 7696
409ee761 7697 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7698 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7699 else
7700 vsyncshift = adjusted_mode->crtc_hsync_start -
7701 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7702 if (vsyncshift < 0)
7703 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7704 }
7705
7706 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7707 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7708
fe2b8f9d 7709 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7710 (adjusted_mode->crtc_hdisplay - 1) |
7711 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7712 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7713 (adjusted_mode->crtc_hblank_start - 1) |
7714 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7715 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7716 (adjusted_mode->crtc_hsync_start - 1) |
7717 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7718
fe2b8f9d 7719 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7720 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7721 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7722 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7723 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7724 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7725 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7726 (adjusted_mode->crtc_vsync_start - 1) |
7727 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7728
b5e508d4
PZ
7729 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7730 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7731 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7732 * bits. */
7733 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7734 (pipe == PIPE_B || pipe == PIPE_C))
7735 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7736
b0e77b9c
PZ
7737 /* pipesrc controls the size that is scaled from, which should
7738 * always be the user's requested size.
7739 */
7740 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7741 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7742 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7743}
7744
1bd1bd80 7745static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7746 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7747{
7748 struct drm_device *dev = crtc->base.dev;
7749 struct drm_i915_private *dev_priv = dev->dev_private;
7750 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7751 uint32_t tmp;
7752
7753 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7754 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7755 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7756 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7757 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7758 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7759 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7760 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7761 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7762
7763 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7764 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7765 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7766 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7767 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7768 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7769 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7770 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7771 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7772
7773 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7774 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7775 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7776 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7777 }
7778
7779 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7780 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7781 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7782
2d112de7
ACO
7783 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7784 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7785}
7786
f6a83288 7787void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7788 struct intel_crtc_state *pipe_config)
babea61d 7789{
2d112de7
ACO
7790 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7791 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7792 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7793 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7794
2d112de7
ACO
7795 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7796 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7797 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7798 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7799
2d112de7 7800 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 7801 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 7802
2d112de7
ACO
7803 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7804 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
7805
7806 mode->hsync = drm_mode_hsync(mode);
7807 mode->vrefresh = drm_mode_vrefresh(mode);
7808 drm_mode_set_name(mode);
babea61d
JB
7809}
7810
84b046f3
DV
7811static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7812{
7813 struct drm_device *dev = intel_crtc->base.dev;
7814 struct drm_i915_private *dev_priv = dev->dev_private;
7815 uint32_t pipeconf;
7816
9f11a9e4 7817 pipeconf = 0;
84b046f3 7818
b6b5d049
VS
7819 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7820 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7821 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7822
6e3c9717 7823 if (intel_crtc->config->double_wide)
cf532bb2 7824 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7825
ff9ce46e
DV
7826 /* only g4x and later have fancy bpc/dither controls */
7827 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7828 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7829 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7830 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7831 PIPECONF_DITHER_TYPE_SP;
84b046f3 7832
6e3c9717 7833 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7834 case 18:
7835 pipeconf |= PIPECONF_6BPC;
7836 break;
7837 case 24:
7838 pipeconf |= PIPECONF_8BPC;
7839 break;
7840 case 30:
7841 pipeconf |= PIPECONF_10BPC;
7842 break;
7843 default:
7844 /* Case prevented by intel_choose_pipe_bpp_dither. */
7845 BUG();
84b046f3
DV
7846 }
7847 }
7848
7849 if (HAS_PIPE_CXSR(dev)) {
7850 if (intel_crtc->lowfreq_avail) {
7851 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7852 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7853 } else {
7854 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7855 }
7856 }
7857
6e3c9717 7858 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7859 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7860 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7861 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7862 else
7863 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7864 } else
84b046f3
DV
7865 pipeconf |= PIPECONF_PROGRESSIVE;
7866
6e3c9717 7867 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7868 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7869
84b046f3
DV
7870 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7871 POSTING_READ(PIPECONF(intel_crtc->pipe));
7872}
7873
190f68c5
ACO
7874static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7875 struct intel_crtc_state *crtc_state)
79e53945 7876{
c7653199 7877 struct drm_device *dev = crtc->base.dev;
79e53945 7878 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7879 int refclk, num_connectors = 0;
c329a4ec
DV
7880 intel_clock_t clock;
7881 bool ok;
7882 bool is_dsi = false;
5eddb70b 7883 struct intel_encoder *encoder;
d4906093 7884 const intel_limit_t *limit;
55bb9992 7885 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7886 struct drm_connector *connector;
55bb9992
ACO
7887 struct drm_connector_state *connector_state;
7888 int i;
79e53945 7889
dd3cd74a
ACO
7890 memset(&crtc_state->dpll_hw_state, 0,
7891 sizeof(crtc_state->dpll_hw_state));
7892
da3ced29 7893 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7894 if (connector_state->crtc != &crtc->base)
7895 continue;
7896
7897 encoder = to_intel_encoder(connector_state->best_encoder);
7898
5eddb70b 7899 switch (encoder->type) {
e9fd1c02
JN
7900 case INTEL_OUTPUT_DSI:
7901 is_dsi = true;
7902 break;
6847d71b
PZ
7903 default:
7904 break;
79e53945 7905 }
43565a06 7906
c751ce4f 7907 num_connectors++;
79e53945
JB
7908 }
7909
f2335330 7910 if (is_dsi)
5b18e57c 7911 return 0;
f2335330 7912
190f68c5 7913 if (!crtc_state->clock_set) {
a93e255f 7914 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7915
e9fd1c02
JN
7916 /*
7917 * Returns a set of divisors for the desired target clock with
7918 * the given refclk, or FALSE. The returned values represent
7919 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7920 * 2) / p1 / p2.
7921 */
a93e255f
ACO
7922 limit = intel_limit(crtc_state, refclk);
7923 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7924 crtc_state->port_clock,
e9fd1c02 7925 refclk, NULL, &clock);
f2335330 7926 if (!ok) {
e9fd1c02
JN
7927 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7928 return -EINVAL;
7929 }
79e53945 7930
f2335330 7931 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7932 crtc_state->dpll.n = clock.n;
7933 crtc_state->dpll.m1 = clock.m1;
7934 crtc_state->dpll.m2 = clock.m2;
7935 crtc_state->dpll.p1 = clock.p1;
7936 crtc_state->dpll.p2 = clock.p2;
f47709a9 7937 }
7026d4ac 7938
e9fd1c02 7939 if (IS_GEN2(dev)) {
c329a4ec 7940 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7941 num_connectors);
9d556c99 7942 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7943 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7944 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7945 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7946 } else {
c329a4ec 7947 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7948 num_connectors);
e9fd1c02 7949 }
79e53945 7950
c8f7a0db 7951 return 0;
f564048e
EA
7952}
7953
2fa2fe9a 7954static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7955 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7956{
7957 struct drm_device *dev = crtc->base.dev;
7958 struct drm_i915_private *dev_priv = dev->dev_private;
7959 uint32_t tmp;
7960
dc9e7dec
VS
7961 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7962 return;
7963
2fa2fe9a 7964 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7965 if (!(tmp & PFIT_ENABLE))
7966 return;
2fa2fe9a 7967
06922821 7968 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7969 if (INTEL_INFO(dev)->gen < 4) {
7970 if (crtc->pipe != PIPE_B)
7971 return;
2fa2fe9a
DV
7972 } else {
7973 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7974 return;
7975 }
7976
06922821 7977 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7978 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7979 if (INTEL_INFO(dev)->gen < 5)
7980 pipe_config->gmch_pfit.lvds_border_bits =
7981 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7982}
7983
acbec814 7984static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7985 struct intel_crtc_state *pipe_config)
acbec814
JB
7986{
7987 struct drm_device *dev = crtc->base.dev;
7988 struct drm_i915_private *dev_priv = dev->dev_private;
7989 int pipe = pipe_config->cpu_transcoder;
7990 intel_clock_t clock;
7991 u32 mdiv;
662c6ecb 7992 int refclk = 100000;
acbec814 7993
f573de5a
SK
7994 /* In case of MIPI DPLL will not even be used */
7995 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7996 return;
7997
a580516d 7998 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7999 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8000 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8001
8002 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8003 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8004 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8005 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8006 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8007
dccbea3b 8008 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8009}
8010
5724dbd1
DL
8011static void
8012i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8013 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8014{
8015 struct drm_device *dev = crtc->base.dev;
8016 struct drm_i915_private *dev_priv = dev->dev_private;
8017 u32 val, base, offset;
8018 int pipe = crtc->pipe, plane = crtc->plane;
8019 int fourcc, pixel_format;
6761dd31 8020 unsigned int aligned_height;
b113d5ee 8021 struct drm_framebuffer *fb;
1b842c89 8022 struct intel_framebuffer *intel_fb;
1ad292b5 8023
42a7b088
DL
8024 val = I915_READ(DSPCNTR(plane));
8025 if (!(val & DISPLAY_PLANE_ENABLE))
8026 return;
8027
d9806c9f 8028 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8029 if (!intel_fb) {
1ad292b5
JB
8030 DRM_DEBUG_KMS("failed to alloc fb\n");
8031 return;
8032 }
8033
1b842c89
DL
8034 fb = &intel_fb->base;
8035
18c5247e
DV
8036 if (INTEL_INFO(dev)->gen >= 4) {
8037 if (val & DISPPLANE_TILED) {
49af449b 8038 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8039 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8040 }
8041 }
1ad292b5
JB
8042
8043 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8044 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8045 fb->pixel_format = fourcc;
8046 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8047
8048 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8049 if (plane_config->tiling)
1ad292b5
JB
8050 offset = I915_READ(DSPTILEOFF(plane));
8051 else
8052 offset = I915_READ(DSPLINOFF(plane));
8053 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8054 } else {
8055 base = I915_READ(DSPADDR(plane));
8056 }
8057 plane_config->base = base;
8058
8059 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8060 fb->width = ((val >> 16) & 0xfff) + 1;
8061 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8062
8063 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8064 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8065
b113d5ee 8066 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8067 fb->pixel_format,
8068 fb->modifier[0]);
1ad292b5 8069
f37b5c2b 8070 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8071
2844a921
DL
8072 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8073 pipe_name(pipe), plane, fb->width, fb->height,
8074 fb->bits_per_pixel, base, fb->pitches[0],
8075 plane_config->size);
1ad292b5 8076
2d14030b 8077 plane_config->fb = intel_fb;
1ad292b5
JB
8078}
8079
70b23a98 8080static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8081 struct intel_crtc_state *pipe_config)
70b23a98
VS
8082{
8083 struct drm_device *dev = crtc->base.dev;
8084 struct drm_i915_private *dev_priv = dev->dev_private;
8085 int pipe = pipe_config->cpu_transcoder;
8086 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8087 intel_clock_t clock;
0d7b6b11 8088 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8089 int refclk = 100000;
8090
a580516d 8091 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8092 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8093 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8094 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8095 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8096 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8097 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8098
8099 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8100 clock.m2 = (pll_dw0 & 0xff) << 22;
8101 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8102 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8103 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8104 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8105 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8106
dccbea3b 8107 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8108}
8109
0e8ffe1b 8110static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8111 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8112{
8113 struct drm_device *dev = crtc->base.dev;
8114 struct drm_i915_private *dev_priv = dev->dev_private;
8115 uint32_t tmp;
8116
f458ebbc
DV
8117 if (!intel_display_power_is_enabled(dev_priv,
8118 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8119 return false;
8120
e143a21c 8121 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8122 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8123
0e8ffe1b
DV
8124 tmp = I915_READ(PIPECONF(crtc->pipe));
8125 if (!(tmp & PIPECONF_ENABLE))
8126 return false;
8127
42571aef
VS
8128 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8129 switch (tmp & PIPECONF_BPC_MASK) {
8130 case PIPECONF_6BPC:
8131 pipe_config->pipe_bpp = 18;
8132 break;
8133 case PIPECONF_8BPC:
8134 pipe_config->pipe_bpp = 24;
8135 break;
8136 case PIPECONF_10BPC:
8137 pipe_config->pipe_bpp = 30;
8138 break;
8139 default:
8140 break;
8141 }
8142 }
8143
b5a9fa09
DV
8144 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8145 pipe_config->limited_color_range = true;
8146
282740f7
VS
8147 if (INTEL_INFO(dev)->gen < 4)
8148 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8149
1bd1bd80
DV
8150 intel_get_pipe_timings(crtc, pipe_config);
8151
2fa2fe9a
DV
8152 i9xx_get_pfit_config(crtc, pipe_config);
8153
6c49f241
DV
8154 if (INTEL_INFO(dev)->gen >= 4) {
8155 tmp = I915_READ(DPLL_MD(crtc->pipe));
8156 pipe_config->pixel_multiplier =
8157 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8158 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8159 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8160 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8161 tmp = I915_READ(DPLL(crtc->pipe));
8162 pipe_config->pixel_multiplier =
8163 ((tmp & SDVO_MULTIPLIER_MASK)
8164 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8165 } else {
8166 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8167 * port and will be fixed up in the encoder->get_config
8168 * function. */
8169 pipe_config->pixel_multiplier = 1;
8170 }
8bcc2795
DV
8171 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8172 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8173 /*
8174 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8175 * on 830. Filter it out here so that we don't
8176 * report errors due to that.
8177 */
8178 if (IS_I830(dev))
8179 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8180
8bcc2795
DV
8181 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8182 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8183 } else {
8184 /* Mask out read-only status bits. */
8185 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8186 DPLL_PORTC_READY_MASK |
8187 DPLL_PORTB_READY_MASK);
8bcc2795 8188 }
6c49f241 8189
70b23a98
VS
8190 if (IS_CHERRYVIEW(dev))
8191 chv_crtc_clock_get(crtc, pipe_config);
8192 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8193 vlv_crtc_clock_get(crtc, pipe_config);
8194 else
8195 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8196
0f64614d
VS
8197 /*
8198 * Normally the dotclock is filled in by the encoder .get_config()
8199 * but in case the pipe is enabled w/o any ports we need a sane
8200 * default.
8201 */
8202 pipe_config->base.adjusted_mode.crtc_clock =
8203 pipe_config->port_clock / pipe_config->pixel_multiplier;
8204
0e8ffe1b
DV
8205 return true;
8206}
8207
dde86e2d 8208static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8209{
8210 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8211 struct intel_encoder *encoder;
74cfd7ac 8212 u32 val, final;
13d83a67 8213 bool has_lvds = false;
199e5d79 8214 bool has_cpu_edp = false;
199e5d79 8215 bool has_panel = false;
99eb6a01
KP
8216 bool has_ck505 = false;
8217 bool can_ssc = false;
13d83a67
JB
8218
8219 /* We need to take the global config into account */
b2784e15 8220 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8221 switch (encoder->type) {
8222 case INTEL_OUTPUT_LVDS:
8223 has_panel = true;
8224 has_lvds = true;
8225 break;
8226 case INTEL_OUTPUT_EDP:
8227 has_panel = true;
2de6905f 8228 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8229 has_cpu_edp = true;
8230 break;
6847d71b
PZ
8231 default:
8232 break;
13d83a67
JB
8233 }
8234 }
8235
99eb6a01 8236 if (HAS_PCH_IBX(dev)) {
41aa3448 8237 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8238 can_ssc = has_ck505;
8239 } else {
8240 has_ck505 = false;
8241 can_ssc = true;
8242 }
8243
2de6905f
ID
8244 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8245 has_panel, has_lvds, has_ck505);
13d83a67
JB
8246
8247 /* Ironlake: try to setup display ref clock before DPLL
8248 * enabling. This is only under driver's control after
8249 * PCH B stepping, previous chipset stepping should be
8250 * ignoring this setting.
8251 */
74cfd7ac
CW
8252 val = I915_READ(PCH_DREF_CONTROL);
8253
8254 /* As we must carefully and slowly disable/enable each source in turn,
8255 * compute the final state we want first and check if we need to
8256 * make any changes at all.
8257 */
8258 final = val;
8259 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8260 if (has_ck505)
8261 final |= DREF_NONSPREAD_CK505_ENABLE;
8262 else
8263 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8264
8265 final &= ~DREF_SSC_SOURCE_MASK;
8266 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8267 final &= ~DREF_SSC1_ENABLE;
8268
8269 if (has_panel) {
8270 final |= DREF_SSC_SOURCE_ENABLE;
8271
8272 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8273 final |= DREF_SSC1_ENABLE;
8274
8275 if (has_cpu_edp) {
8276 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8277 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8278 else
8279 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8280 } else
8281 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8282 } else {
8283 final |= DREF_SSC_SOURCE_DISABLE;
8284 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8285 }
8286
8287 if (final == val)
8288 return;
8289
13d83a67 8290 /* Always enable nonspread source */
74cfd7ac 8291 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8292
99eb6a01 8293 if (has_ck505)
74cfd7ac 8294 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8295 else
74cfd7ac 8296 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8297
199e5d79 8298 if (has_panel) {
74cfd7ac
CW
8299 val &= ~DREF_SSC_SOURCE_MASK;
8300 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8301
199e5d79 8302 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8303 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8304 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8305 val |= DREF_SSC1_ENABLE;
e77166b5 8306 } else
74cfd7ac 8307 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8308
8309 /* Get SSC going before enabling the outputs */
74cfd7ac 8310 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8311 POSTING_READ(PCH_DREF_CONTROL);
8312 udelay(200);
8313
74cfd7ac 8314 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8315
8316 /* Enable CPU source on CPU attached eDP */
199e5d79 8317 if (has_cpu_edp) {
99eb6a01 8318 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8319 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8320 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8321 } else
74cfd7ac 8322 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8323 } else
74cfd7ac 8324 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8325
74cfd7ac 8326 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8327 POSTING_READ(PCH_DREF_CONTROL);
8328 udelay(200);
8329 } else {
8330 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8331
74cfd7ac 8332 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8333
8334 /* Turn off CPU output */
74cfd7ac 8335 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8336
74cfd7ac 8337 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8338 POSTING_READ(PCH_DREF_CONTROL);
8339 udelay(200);
8340
8341 /* Turn off the SSC source */
74cfd7ac
CW
8342 val &= ~DREF_SSC_SOURCE_MASK;
8343 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8344
8345 /* Turn off SSC1 */
74cfd7ac 8346 val &= ~DREF_SSC1_ENABLE;
199e5d79 8347
74cfd7ac 8348 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8349 POSTING_READ(PCH_DREF_CONTROL);
8350 udelay(200);
8351 }
74cfd7ac
CW
8352
8353 BUG_ON(val != final);
13d83a67
JB
8354}
8355
f31f2d55 8356static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8357{
f31f2d55 8358 uint32_t tmp;
dde86e2d 8359
0ff066a9
PZ
8360 tmp = I915_READ(SOUTH_CHICKEN2);
8361 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8362 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8363
0ff066a9
PZ
8364 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8365 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8366 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8367
0ff066a9
PZ
8368 tmp = I915_READ(SOUTH_CHICKEN2);
8369 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8370 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8371
0ff066a9
PZ
8372 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8373 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8374 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8375}
8376
8377/* WaMPhyProgramming:hsw */
8378static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8379{
8380 uint32_t tmp;
dde86e2d
PZ
8381
8382 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8383 tmp &= ~(0xFF << 24);
8384 tmp |= (0x12 << 24);
8385 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8386
dde86e2d
PZ
8387 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8388 tmp |= (1 << 11);
8389 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8390
8391 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8392 tmp |= (1 << 11);
8393 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8394
dde86e2d
PZ
8395 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8396 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8397 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8398
8399 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8400 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8401 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8402
0ff066a9
PZ
8403 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8404 tmp &= ~(7 << 13);
8405 tmp |= (5 << 13);
8406 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8407
0ff066a9
PZ
8408 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8409 tmp &= ~(7 << 13);
8410 tmp |= (5 << 13);
8411 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8412
8413 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8414 tmp &= ~0xFF;
8415 tmp |= 0x1C;
8416 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8417
8418 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8419 tmp &= ~0xFF;
8420 tmp |= 0x1C;
8421 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8422
8423 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8424 tmp &= ~(0xFF << 16);
8425 tmp |= (0x1C << 16);
8426 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8427
8428 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8429 tmp &= ~(0xFF << 16);
8430 tmp |= (0x1C << 16);
8431 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8432
0ff066a9
PZ
8433 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8434 tmp |= (1 << 27);
8435 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8436
0ff066a9
PZ
8437 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8438 tmp |= (1 << 27);
8439 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8440
0ff066a9
PZ
8441 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8442 tmp &= ~(0xF << 28);
8443 tmp |= (4 << 28);
8444 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8445
0ff066a9
PZ
8446 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8447 tmp &= ~(0xF << 28);
8448 tmp |= (4 << 28);
8449 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8450}
8451
2fa86a1f
PZ
8452/* Implements 3 different sequences from BSpec chapter "Display iCLK
8453 * Programming" based on the parameters passed:
8454 * - Sequence to enable CLKOUT_DP
8455 * - Sequence to enable CLKOUT_DP without spread
8456 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8457 */
8458static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8459 bool with_fdi)
f31f2d55
PZ
8460{
8461 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8462 uint32_t reg, tmp;
8463
8464 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8465 with_spread = true;
c2699524 8466 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 8467 with_fdi = false;
f31f2d55 8468
a580516d 8469 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8470
8471 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8472 tmp &= ~SBI_SSCCTL_DISABLE;
8473 tmp |= SBI_SSCCTL_PATHALT;
8474 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8475
8476 udelay(24);
8477
2fa86a1f
PZ
8478 if (with_spread) {
8479 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8480 tmp &= ~SBI_SSCCTL_PATHALT;
8481 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8482
2fa86a1f
PZ
8483 if (with_fdi) {
8484 lpt_reset_fdi_mphy(dev_priv);
8485 lpt_program_fdi_mphy(dev_priv);
8486 }
8487 }
dde86e2d 8488
c2699524 8489 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
8490 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8491 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8492 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8493
a580516d 8494 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8495}
8496
47701c3b
PZ
8497/* Sequence to disable CLKOUT_DP */
8498static void lpt_disable_clkout_dp(struct drm_device *dev)
8499{
8500 struct drm_i915_private *dev_priv = dev->dev_private;
8501 uint32_t reg, tmp;
8502
a580516d 8503 mutex_lock(&dev_priv->sb_lock);
47701c3b 8504
c2699524 8505 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
8506 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8507 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8508 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8509
8510 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8511 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8512 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8513 tmp |= SBI_SSCCTL_PATHALT;
8514 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8515 udelay(32);
8516 }
8517 tmp |= SBI_SSCCTL_DISABLE;
8518 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8519 }
8520
a580516d 8521 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8522}
8523
bf8fa3d3
PZ
8524static void lpt_init_pch_refclk(struct drm_device *dev)
8525{
bf8fa3d3
PZ
8526 struct intel_encoder *encoder;
8527 bool has_vga = false;
8528
b2784e15 8529 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8530 switch (encoder->type) {
8531 case INTEL_OUTPUT_ANALOG:
8532 has_vga = true;
8533 break;
6847d71b
PZ
8534 default:
8535 break;
bf8fa3d3
PZ
8536 }
8537 }
8538
47701c3b
PZ
8539 if (has_vga)
8540 lpt_enable_clkout_dp(dev, true, true);
8541 else
8542 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8543}
8544
dde86e2d
PZ
8545/*
8546 * Initialize reference clocks when the driver loads
8547 */
8548void intel_init_pch_refclk(struct drm_device *dev)
8549{
8550 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8551 ironlake_init_pch_refclk(dev);
8552 else if (HAS_PCH_LPT(dev))
8553 lpt_init_pch_refclk(dev);
8554}
8555
55bb9992 8556static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8557{
55bb9992 8558 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8559 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8560 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8561 struct drm_connector *connector;
55bb9992 8562 struct drm_connector_state *connector_state;
d9d444cb 8563 struct intel_encoder *encoder;
55bb9992 8564 int num_connectors = 0, i;
d9d444cb
JB
8565 bool is_lvds = false;
8566
da3ced29 8567 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8568 if (connector_state->crtc != crtc_state->base.crtc)
8569 continue;
8570
8571 encoder = to_intel_encoder(connector_state->best_encoder);
8572
d9d444cb
JB
8573 switch (encoder->type) {
8574 case INTEL_OUTPUT_LVDS:
8575 is_lvds = true;
8576 break;
6847d71b
PZ
8577 default:
8578 break;
d9d444cb
JB
8579 }
8580 num_connectors++;
8581 }
8582
8583 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8584 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8585 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8586 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8587 }
8588
8589 return 120000;
8590}
8591
6ff93609 8592static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8593{
c8203565 8594 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8596 int pipe = intel_crtc->pipe;
c8203565
PZ
8597 uint32_t val;
8598
78114071 8599 val = 0;
c8203565 8600
6e3c9717 8601 switch (intel_crtc->config->pipe_bpp) {
c8203565 8602 case 18:
dfd07d72 8603 val |= PIPECONF_6BPC;
c8203565
PZ
8604 break;
8605 case 24:
dfd07d72 8606 val |= PIPECONF_8BPC;
c8203565
PZ
8607 break;
8608 case 30:
dfd07d72 8609 val |= PIPECONF_10BPC;
c8203565
PZ
8610 break;
8611 case 36:
dfd07d72 8612 val |= PIPECONF_12BPC;
c8203565
PZ
8613 break;
8614 default:
cc769b62
PZ
8615 /* Case prevented by intel_choose_pipe_bpp_dither. */
8616 BUG();
c8203565
PZ
8617 }
8618
6e3c9717 8619 if (intel_crtc->config->dither)
c8203565
PZ
8620 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8621
6e3c9717 8622 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8623 val |= PIPECONF_INTERLACED_ILK;
8624 else
8625 val |= PIPECONF_PROGRESSIVE;
8626
6e3c9717 8627 if (intel_crtc->config->limited_color_range)
3685a8f3 8628 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8629
c8203565
PZ
8630 I915_WRITE(PIPECONF(pipe), val);
8631 POSTING_READ(PIPECONF(pipe));
8632}
8633
86d3efce
VS
8634/*
8635 * Set up the pipe CSC unit.
8636 *
8637 * Currently only full range RGB to limited range RGB conversion
8638 * is supported, but eventually this should handle various
8639 * RGB<->YCbCr scenarios as well.
8640 */
50f3b016 8641static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8642{
8643 struct drm_device *dev = crtc->dev;
8644 struct drm_i915_private *dev_priv = dev->dev_private;
8645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8646 int pipe = intel_crtc->pipe;
8647 uint16_t coeff = 0x7800; /* 1.0 */
8648
8649 /*
8650 * TODO: Check what kind of values actually come out of the pipe
8651 * with these coeff/postoff values and adjust to get the best
8652 * accuracy. Perhaps we even need to take the bpc value into
8653 * consideration.
8654 */
8655
6e3c9717 8656 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8657 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8658
8659 /*
8660 * GY/GU and RY/RU should be the other way around according
8661 * to BSpec, but reality doesn't agree. Just set them up in
8662 * a way that results in the correct picture.
8663 */
8664 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8665 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8666
8667 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8668 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8669
8670 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8671 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8672
8673 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8674 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8675 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8676
8677 if (INTEL_INFO(dev)->gen > 6) {
8678 uint16_t postoff = 0;
8679
6e3c9717 8680 if (intel_crtc->config->limited_color_range)
32cf0cb0 8681 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8682
8683 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8684 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8685 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8686
8687 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8688 } else {
8689 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8690
6e3c9717 8691 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8692 mode |= CSC_BLACK_SCREEN_OFFSET;
8693
8694 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8695 }
8696}
8697
6ff93609 8698static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8699{
756f85cf
PZ
8700 struct drm_device *dev = crtc->dev;
8701 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8703 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8704 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8705 uint32_t val;
8706
3eff4faa 8707 val = 0;
ee2b0b38 8708
6e3c9717 8709 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8710 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8711
6e3c9717 8712 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8713 val |= PIPECONF_INTERLACED_ILK;
8714 else
8715 val |= PIPECONF_PROGRESSIVE;
8716
702e7a56
PZ
8717 I915_WRITE(PIPECONF(cpu_transcoder), val);
8718 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8719
8720 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8721 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8722
3cdf122c 8723 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8724 val = 0;
8725
6e3c9717 8726 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8727 case 18:
8728 val |= PIPEMISC_DITHER_6_BPC;
8729 break;
8730 case 24:
8731 val |= PIPEMISC_DITHER_8_BPC;
8732 break;
8733 case 30:
8734 val |= PIPEMISC_DITHER_10_BPC;
8735 break;
8736 case 36:
8737 val |= PIPEMISC_DITHER_12_BPC;
8738 break;
8739 default:
8740 /* Case prevented by pipe_config_set_bpp. */
8741 BUG();
8742 }
8743
6e3c9717 8744 if (intel_crtc->config->dither)
756f85cf
PZ
8745 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8746
8747 I915_WRITE(PIPEMISC(pipe), val);
8748 }
ee2b0b38
PZ
8749}
8750
6591c6e4 8751static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8752 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8753 intel_clock_t *clock,
8754 bool *has_reduced_clock,
8755 intel_clock_t *reduced_clock)
8756{
8757 struct drm_device *dev = crtc->dev;
8758 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8759 int refclk;
d4906093 8760 const intel_limit_t *limit;
c329a4ec 8761 bool ret;
79e53945 8762
55bb9992 8763 refclk = ironlake_get_refclk(crtc_state);
79e53945 8764
d4906093
ML
8765 /*
8766 * Returns a set of divisors for the desired target clock with the given
8767 * refclk, or FALSE. The returned values represent the clock equation:
8768 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8769 */
a93e255f
ACO
8770 limit = intel_limit(crtc_state, refclk);
8771 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8772 crtc_state->port_clock,
ee9300bb 8773 refclk, NULL, clock);
6591c6e4
PZ
8774 if (!ret)
8775 return false;
cda4b7d3 8776
6591c6e4
PZ
8777 return true;
8778}
8779
d4b1931c
PZ
8780int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8781{
8782 /*
8783 * Account for spread spectrum to avoid
8784 * oversubscribing the link. Max center spread
8785 * is 2.5%; use 5% for safety's sake.
8786 */
8787 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8788 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8789}
8790
7429e9d4 8791static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8792{
7429e9d4 8793 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8794}
8795
de13a2e3 8796static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8797 struct intel_crtc_state *crtc_state,
7429e9d4 8798 u32 *fp,
9a7c7890 8799 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8800{
de13a2e3 8801 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8802 struct drm_device *dev = crtc->dev;
8803 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8804 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8805 struct drm_connector *connector;
55bb9992
ACO
8806 struct drm_connector_state *connector_state;
8807 struct intel_encoder *encoder;
de13a2e3 8808 uint32_t dpll;
55bb9992 8809 int factor, num_connectors = 0, i;
09ede541 8810 bool is_lvds = false, is_sdvo = false;
79e53945 8811
da3ced29 8812 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8813 if (connector_state->crtc != crtc_state->base.crtc)
8814 continue;
8815
8816 encoder = to_intel_encoder(connector_state->best_encoder);
8817
8818 switch (encoder->type) {
79e53945
JB
8819 case INTEL_OUTPUT_LVDS:
8820 is_lvds = true;
8821 break;
8822 case INTEL_OUTPUT_SDVO:
7d57382e 8823 case INTEL_OUTPUT_HDMI:
79e53945 8824 is_sdvo = true;
79e53945 8825 break;
6847d71b
PZ
8826 default:
8827 break;
79e53945 8828 }
43565a06 8829
c751ce4f 8830 num_connectors++;
79e53945 8831 }
79e53945 8832
c1858123 8833 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8834 factor = 21;
8835 if (is_lvds) {
8836 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8837 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8838 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8839 factor = 25;
190f68c5 8840 } else if (crtc_state->sdvo_tv_clock)
8febb297 8841 factor = 20;
c1858123 8842
190f68c5 8843 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8844 *fp |= FP_CB_TUNE;
2c07245f 8845
9a7c7890
DV
8846 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8847 *fp2 |= FP_CB_TUNE;
8848
5eddb70b 8849 dpll = 0;
2c07245f 8850
a07d6787
EA
8851 if (is_lvds)
8852 dpll |= DPLLB_MODE_LVDS;
8853 else
8854 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8855
190f68c5 8856 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8857 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8858
8859 if (is_sdvo)
4a33e48d 8860 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8861 if (crtc_state->has_dp_encoder)
4a33e48d 8862 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8863
a07d6787 8864 /* compute bitmask from p1 value */
190f68c5 8865 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8866 /* also FPA1 */
190f68c5 8867 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8868
190f68c5 8869 switch (crtc_state->dpll.p2) {
a07d6787
EA
8870 case 5:
8871 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8872 break;
8873 case 7:
8874 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8875 break;
8876 case 10:
8877 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8878 break;
8879 case 14:
8880 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8881 break;
79e53945
JB
8882 }
8883
b4c09f3b 8884 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8885 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8886 else
8887 dpll |= PLL_REF_INPUT_DREFCLK;
8888
959e16d6 8889 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8890}
8891
190f68c5
ACO
8892static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8893 struct intel_crtc_state *crtc_state)
de13a2e3 8894{
c7653199 8895 struct drm_device *dev = crtc->base.dev;
de13a2e3 8896 intel_clock_t clock, reduced_clock;
cbbab5bd 8897 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8898 bool ok, has_reduced_clock = false;
8b47047b 8899 bool is_lvds = false;
e2b78267 8900 struct intel_shared_dpll *pll;
de13a2e3 8901
dd3cd74a
ACO
8902 memset(&crtc_state->dpll_hw_state, 0,
8903 sizeof(crtc_state->dpll_hw_state));
8904
409ee761 8905 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8906
5dc5298b
PZ
8907 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8908 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8909
190f68c5 8910 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8911 &has_reduced_clock, &reduced_clock);
190f68c5 8912 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8913 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8914 return -EINVAL;
79e53945 8915 }
f47709a9 8916 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8917 if (!crtc_state->clock_set) {
8918 crtc_state->dpll.n = clock.n;
8919 crtc_state->dpll.m1 = clock.m1;
8920 crtc_state->dpll.m2 = clock.m2;
8921 crtc_state->dpll.p1 = clock.p1;
8922 crtc_state->dpll.p2 = clock.p2;
f47709a9 8923 }
79e53945 8924
5dc5298b 8925 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8926 if (crtc_state->has_pch_encoder) {
8927 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8928 if (has_reduced_clock)
7429e9d4 8929 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8930
190f68c5 8931 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8932 &fp, &reduced_clock,
8933 has_reduced_clock ? &fp2 : NULL);
8934
190f68c5
ACO
8935 crtc_state->dpll_hw_state.dpll = dpll;
8936 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8937 if (has_reduced_clock)
190f68c5 8938 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8939 else
190f68c5 8940 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8941
190f68c5 8942 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8943 if (pll == NULL) {
84f44ce7 8944 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8945 pipe_name(crtc->pipe));
4b645f14
JB
8946 return -EINVAL;
8947 }
3fb37703 8948 }
79e53945 8949
ab585dea 8950 if (is_lvds && has_reduced_clock)
c7653199 8951 crtc->lowfreq_avail = true;
bcd644e0 8952 else
c7653199 8953 crtc->lowfreq_avail = false;
e2b78267 8954
c8f7a0db 8955 return 0;
79e53945
JB
8956}
8957
eb14cb74
VS
8958static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8959 struct intel_link_m_n *m_n)
8960{
8961 struct drm_device *dev = crtc->base.dev;
8962 struct drm_i915_private *dev_priv = dev->dev_private;
8963 enum pipe pipe = crtc->pipe;
8964
8965 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8966 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8967 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8968 & ~TU_SIZE_MASK;
8969 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8970 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8971 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8972}
8973
8974static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8975 enum transcoder transcoder,
b95af8be
VK
8976 struct intel_link_m_n *m_n,
8977 struct intel_link_m_n *m2_n2)
72419203
DV
8978{
8979 struct drm_device *dev = crtc->base.dev;
8980 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8981 enum pipe pipe = crtc->pipe;
72419203 8982
eb14cb74
VS
8983 if (INTEL_INFO(dev)->gen >= 5) {
8984 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8985 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8986 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8987 & ~TU_SIZE_MASK;
8988 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8989 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8990 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8991 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8992 * gen < 8) and if DRRS is supported (to make sure the
8993 * registers are not unnecessarily read).
8994 */
8995 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8996 crtc->config->has_drrs) {
b95af8be
VK
8997 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8998 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8999 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9000 & ~TU_SIZE_MASK;
9001 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9002 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9003 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9004 }
eb14cb74
VS
9005 } else {
9006 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9007 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9008 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9009 & ~TU_SIZE_MASK;
9010 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9011 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9012 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9013 }
9014}
9015
9016void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9017 struct intel_crtc_state *pipe_config)
eb14cb74 9018{
681a8504 9019 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9020 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9021 else
9022 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9023 &pipe_config->dp_m_n,
9024 &pipe_config->dp_m2_n2);
eb14cb74 9025}
72419203 9026
eb14cb74 9027static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9028 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9029{
9030 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9031 &pipe_config->fdi_m_n, NULL);
72419203
DV
9032}
9033
bd2e244f 9034static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9035 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9036{
9037 struct drm_device *dev = crtc->base.dev;
9038 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9039 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9040 uint32_t ps_ctrl = 0;
9041 int id = -1;
9042 int i;
bd2e244f 9043
a1b2278e
CK
9044 /* find scaler attached to this pipe */
9045 for (i = 0; i < crtc->num_scalers; i++) {
9046 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9047 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9048 id = i;
9049 pipe_config->pch_pfit.enabled = true;
9050 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9051 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9052 break;
9053 }
9054 }
bd2e244f 9055
a1b2278e
CK
9056 scaler_state->scaler_id = id;
9057 if (id >= 0) {
9058 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9059 } else {
9060 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9061 }
9062}
9063
5724dbd1
DL
9064static void
9065skylake_get_initial_plane_config(struct intel_crtc *crtc,
9066 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9067{
9068 struct drm_device *dev = crtc->base.dev;
9069 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9070 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9071 int pipe = crtc->pipe;
9072 int fourcc, pixel_format;
6761dd31 9073 unsigned int aligned_height;
bc8d7dff 9074 struct drm_framebuffer *fb;
1b842c89 9075 struct intel_framebuffer *intel_fb;
bc8d7dff 9076
d9806c9f 9077 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9078 if (!intel_fb) {
bc8d7dff
DL
9079 DRM_DEBUG_KMS("failed to alloc fb\n");
9080 return;
9081 }
9082
1b842c89
DL
9083 fb = &intel_fb->base;
9084
bc8d7dff 9085 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9086 if (!(val & PLANE_CTL_ENABLE))
9087 goto error;
9088
bc8d7dff
DL
9089 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9090 fourcc = skl_format_to_fourcc(pixel_format,
9091 val & PLANE_CTL_ORDER_RGBX,
9092 val & PLANE_CTL_ALPHA_MASK);
9093 fb->pixel_format = fourcc;
9094 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9095
40f46283
DL
9096 tiling = val & PLANE_CTL_TILED_MASK;
9097 switch (tiling) {
9098 case PLANE_CTL_TILED_LINEAR:
9099 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9100 break;
9101 case PLANE_CTL_TILED_X:
9102 plane_config->tiling = I915_TILING_X;
9103 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9104 break;
9105 case PLANE_CTL_TILED_Y:
9106 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9107 break;
9108 case PLANE_CTL_TILED_YF:
9109 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9110 break;
9111 default:
9112 MISSING_CASE(tiling);
9113 goto error;
9114 }
9115
bc8d7dff
DL
9116 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9117 plane_config->base = base;
9118
9119 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9120
9121 val = I915_READ(PLANE_SIZE(pipe, 0));
9122 fb->height = ((val >> 16) & 0xfff) + 1;
9123 fb->width = ((val >> 0) & 0x1fff) + 1;
9124
9125 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9126 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9127 fb->pixel_format);
bc8d7dff
DL
9128 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9129
9130 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9131 fb->pixel_format,
9132 fb->modifier[0]);
bc8d7dff 9133
f37b5c2b 9134 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9135
9136 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9137 pipe_name(pipe), fb->width, fb->height,
9138 fb->bits_per_pixel, base, fb->pitches[0],
9139 plane_config->size);
9140
2d14030b 9141 plane_config->fb = intel_fb;
bc8d7dff
DL
9142 return;
9143
9144error:
9145 kfree(fb);
9146}
9147
2fa2fe9a 9148static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9149 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9150{
9151 struct drm_device *dev = crtc->base.dev;
9152 struct drm_i915_private *dev_priv = dev->dev_private;
9153 uint32_t tmp;
9154
9155 tmp = I915_READ(PF_CTL(crtc->pipe));
9156
9157 if (tmp & PF_ENABLE) {
fd4daa9c 9158 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9159 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9160 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9161
9162 /* We currently do not free assignements of panel fitters on
9163 * ivb/hsw (since we don't use the higher upscaling modes which
9164 * differentiates them) so just WARN about this case for now. */
9165 if (IS_GEN7(dev)) {
9166 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9167 PF_PIPE_SEL_IVB(crtc->pipe));
9168 }
2fa2fe9a 9169 }
79e53945
JB
9170}
9171
5724dbd1
DL
9172static void
9173ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9174 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9175{
9176 struct drm_device *dev = crtc->base.dev;
9177 struct drm_i915_private *dev_priv = dev->dev_private;
9178 u32 val, base, offset;
aeee5a49 9179 int pipe = crtc->pipe;
4c6baa59 9180 int fourcc, pixel_format;
6761dd31 9181 unsigned int aligned_height;
b113d5ee 9182 struct drm_framebuffer *fb;
1b842c89 9183 struct intel_framebuffer *intel_fb;
4c6baa59 9184
42a7b088
DL
9185 val = I915_READ(DSPCNTR(pipe));
9186 if (!(val & DISPLAY_PLANE_ENABLE))
9187 return;
9188
d9806c9f 9189 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9190 if (!intel_fb) {
4c6baa59
JB
9191 DRM_DEBUG_KMS("failed to alloc fb\n");
9192 return;
9193 }
9194
1b842c89
DL
9195 fb = &intel_fb->base;
9196
18c5247e
DV
9197 if (INTEL_INFO(dev)->gen >= 4) {
9198 if (val & DISPPLANE_TILED) {
49af449b 9199 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9200 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9201 }
9202 }
4c6baa59
JB
9203
9204 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9205 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9206 fb->pixel_format = fourcc;
9207 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9208
aeee5a49 9209 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9210 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9211 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9212 } else {
49af449b 9213 if (plane_config->tiling)
aeee5a49 9214 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9215 else
aeee5a49 9216 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9217 }
9218 plane_config->base = base;
9219
9220 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9221 fb->width = ((val >> 16) & 0xfff) + 1;
9222 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9223
9224 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9225 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9226
b113d5ee 9227 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9228 fb->pixel_format,
9229 fb->modifier[0]);
4c6baa59 9230
f37b5c2b 9231 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9232
2844a921
DL
9233 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9234 pipe_name(pipe), fb->width, fb->height,
9235 fb->bits_per_pixel, base, fb->pitches[0],
9236 plane_config->size);
b113d5ee 9237
2d14030b 9238 plane_config->fb = intel_fb;
4c6baa59
JB
9239}
9240
0e8ffe1b 9241static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9242 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9243{
9244 struct drm_device *dev = crtc->base.dev;
9245 struct drm_i915_private *dev_priv = dev->dev_private;
9246 uint32_t tmp;
9247
f458ebbc
DV
9248 if (!intel_display_power_is_enabled(dev_priv,
9249 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9250 return false;
9251
e143a21c 9252 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9253 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9254
0e8ffe1b
DV
9255 tmp = I915_READ(PIPECONF(crtc->pipe));
9256 if (!(tmp & PIPECONF_ENABLE))
9257 return false;
9258
42571aef
VS
9259 switch (tmp & PIPECONF_BPC_MASK) {
9260 case PIPECONF_6BPC:
9261 pipe_config->pipe_bpp = 18;
9262 break;
9263 case PIPECONF_8BPC:
9264 pipe_config->pipe_bpp = 24;
9265 break;
9266 case PIPECONF_10BPC:
9267 pipe_config->pipe_bpp = 30;
9268 break;
9269 case PIPECONF_12BPC:
9270 pipe_config->pipe_bpp = 36;
9271 break;
9272 default:
9273 break;
9274 }
9275
b5a9fa09
DV
9276 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9277 pipe_config->limited_color_range = true;
9278
ab9412ba 9279 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9280 struct intel_shared_dpll *pll;
9281
88adfff1
DV
9282 pipe_config->has_pch_encoder = true;
9283
627eb5a3
DV
9284 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9285 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9286 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9287
9288 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9289
c0d43d62 9290 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9291 pipe_config->shared_dpll =
9292 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9293 } else {
9294 tmp = I915_READ(PCH_DPLL_SEL);
9295 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9296 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9297 else
9298 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9299 }
66e985c0
DV
9300
9301 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9302
9303 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9304 &pipe_config->dpll_hw_state));
c93f54cf
DV
9305
9306 tmp = pipe_config->dpll_hw_state.dpll;
9307 pipe_config->pixel_multiplier =
9308 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9309 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9310
9311 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9312 } else {
9313 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9314 }
9315
1bd1bd80
DV
9316 intel_get_pipe_timings(crtc, pipe_config);
9317
2fa2fe9a
DV
9318 ironlake_get_pfit_config(crtc, pipe_config);
9319
0e8ffe1b
DV
9320 return true;
9321}
9322
be256dc7
PZ
9323static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9324{
9325 struct drm_device *dev = dev_priv->dev;
be256dc7 9326 struct intel_crtc *crtc;
be256dc7 9327
d3fcc808 9328 for_each_intel_crtc(dev, crtc)
e2c719b7 9329 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9330 pipe_name(crtc->pipe));
9331
e2c719b7
RC
9332 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9333 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
9334 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9335 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
e2c719b7
RC
9336 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9337 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9338 "CPU PWM1 enabled\n");
c5107b87 9339 if (IS_HASWELL(dev))
e2c719b7 9340 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9341 "CPU PWM2 enabled\n");
e2c719b7 9342 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9343 "PCH PWM1 enabled\n");
e2c719b7 9344 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9345 "Utility pin enabled\n");
e2c719b7 9346 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9347
9926ada1
PZ
9348 /*
9349 * In theory we can still leave IRQs enabled, as long as only the HPD
9350 * interrupts remain enabled. We used to check for that, but since it's
9351 * gen-specific and since we only disable LCPLL after we fully disable
9352 * the interrupts, the check below should be enough.
9353 */
e2c719b7 9354 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9355}
9356
9ccd5aeb
PZ
9357static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9358{
9359 struct drm_device *dev = dev_priv->dev;
9360
9361 if (IS_HASWELL(dev))
9362 return I915_READ(D_COMP_HSW);
9363 else
9364 return I915_READ(D_COMP_BDW);
9365}
9366
3c4c9b81
PZ
9367static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9368{
9369 struct drm_device *dev = dev_priv->dev;
9370
9371 if (IS_HASWELL(dev)) {
9372 mutex_lock(&dev_priv->rps.hw_lock);
9373 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9374 val))
f475dadf 9375 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9376 mutex_unlock(&dev_priv->rps.hw_lock);
9377 } else {
9ccd5aeb
PZ
9378 I915_WRITE(D_COMP_BDW, val);
9379 POSTING_READ(D_COMP_BDW);
3c4c9b81 9380 }
be256dc7
PZ
9381}
9382
9383/*
9384 * This function implements pieces of two sequences from BSpec:
9385 * - Sequence for display software to disable LCPLL
9386 * - Sequence for display software to allow package C8+
9387 * The steps implemented here are just the steps that actually touch the LCPLL
9388 * register. Callers should take care of disabling all the display engine
9389 * functions, doing the mode unset, fixing interrupts, etc.
9390 */
6ff58d53
PZ
9391static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9392 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9393{
9394 uint32_t val;
9395
9396 assert_can_disable_lcpll(dev_priv);
9397
9398 val = I915_READ(LCPLL_CTL);
9399
9400 if (switch_to_fclk) {
9401 val |= LCPLL_CD_SOURCE_FCLK;
9402 I915_WRITE(LCPLL_CTL, val);
9403
9404 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9405 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9406 DRM_ERROR("Switching to FCLK failed\n");
9407
9408 val = I915_READ(LCPLL_CTL);
9409 }
9410
9411 val |= LCPLL_PLL_DISABLE;
9412 I915_WRITE(LCPLL_CTL, val);
9413 POSTING_READ(LCPLL_CTL);
9414
9415 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9416 DRM_ERROR("LCPLL still locked\n");
9417
9ccd5aeb 9418 val = hsw_read_dcomp(dev_priv);
be256dc7 9419 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9420 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9421 ndelay(100);
9422
9ccd5aeb
PZ
9423 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9424 1))
be256dc7
PZ
9425 DRM_ERROR("D_COMP RCOMP still in progress\n");
9426
9427 if (allow_power_down) {
9428 val = I915_READ(LCPLL_CTL);
9429 val |= LCPLL_POWER_DOWN_ALLOW;
9430 I915_WRITE(LCPLL_CTL, val);
9431 POSTING_READ(LCPLL_CTL);
9432 }
9433}
9434
9435/*
9436 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9437 * source.
9438 */
6ff58d53 9439static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9440{
9441 uint32_t val;
9442
9443 val = I915_READ(LCPLL_CTL);
9444
9445 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9446 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9447 return;
9448
a8a8bd54
PZ
9449 /*
9450 * Make sure we're not on PC8 state before disabling PC8, otherwise
9451 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9452 */
59bad947 9453 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9454
be256dc7
PZ
9455 if (val & LCPLL_POWER_DOWN_ALLOW) {
9456 val &= ~LCPLL_POWER_DOWN_ALLOW;
9457 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9458 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9459 }
9460
9ccd5aeb 9461 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9462 val |= D_COMP_COMP_FORCE;
9463 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9464 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9465
9466 val = I915_READ(LCPLL_CTL);
9467 val &= ~LCPLL_PLL_DISABLE;
9468 I915_WRITE(LCPLL_CTL, val);
9469
9470 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9471 DRM_ERROR("LCPLL not locked yet\n");
9472
9473 if (val & LCPLL_CD_SOURCE_FCLK) {
9474 val = I915_READ(LCPLL_CTL);
9475 val &= ~LCPLL_CD_SOURCE_FCLK;
9476 I915_WRITE(LCPLL_CTL, val);
9477
9478 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9479 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9480 DRM_ERROR("Switching back to LCPLL failed\n");
9481 }
215733fa 9482
59bad947 9483 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9484 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9485}
9486
765dab67
PZ
9487/*
9488 * Package states C8 and deeper are really deep PC states that can only be
9489 * reached when all the devices on the system allow it, so even if the graphics
9490 * device allows PC8+, it doesn't mean the system will actually get to these
9491 * states. Our driver only allows PC8+ when going into runtime PM.
9492 *
9493 * The requirements for PC8+ are that all the outputs are disabled, the power
9494 * well is disabled and most interrupts are disabled, and these are also
9495 * requirements for runtime PM. When these conditions are met, we manually do
9496 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9497 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9498 * hang the machine.
9499 *
9500 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9501 * the state of some registers, so when we come back from PC8+ we need to
9502 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9503 * need to take care of the registers kept by RC6. Notice that this happens even
9504 * if we don't put the device in PCI D3 state (which is what currently happens
9505 * because of the runtime PM support).
9506 *
9507 * For more, read "Display Sequences for Package C8" on the hardware
9508 * documentation.
9509 */
a14cb6fc 9510void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9511{
c67a470b
PZ
9512 struct drm_device *dev = dev_priv->dev;
9513 uint32_t val;
9514
c67a470b
PZ
9515 DRM_DEBUG_KMS("Enabling package C8+\n");
9516
c2699524 9517 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9518 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9519 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9520 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9521 }
9522
9523 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9524 hsw_disable_lcpll(dev_priv, true, true);
9525}
9526
a14cb6fc 9527void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9528{
9529 struct drm_device *dev = dev_priv->dev;
9530 uint32_t val;
9531
c67a470b
PZ
9532 DRM_DEBUG_KMS("Disabling package C8+\n");
9533
9534 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9535 lpt_init_pch_refclk(dev);
9536
c2699524 9537 if (HAS_PCH_LPT_LP(dev)) {
c67a470b
PZ
9538 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9539 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9540 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9541 }
9542
9543 intel_prepare_ddi(dev);
c67a470b
PZ
9544}
9545
27c329ed 9546static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9547{
a821fc46 9548 struct drm_device *dev = old_state->dev;
27c329ed 9549 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
f8437dd1 9550
27c329ed 9551 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9552}
9553
b432e5cf 9554/* compute the max rate for new configuration */
27c329ed 9555static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9556{
b432e5cf 9557 struct intel_crtc *intel_crtc;
27c329ed 9558 struct intel_crtc_state *crtc_state;
b432e5cf 9559 int max_pixel_rate = 0;
b432e5cf 9560
27c329ed
ML
9561 for_each_intel_crtc(state->dev, intel_crtc) {
9562 int pixel_rate;
9563
9564 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9565 if (IS_ERR(crtc_state))
9566 return PTR_ERR(crtc_state);
9567
9568 if (!crtc_state->base.enable)
b432e5cf
VS
9569 continue;
9570
27c329ed 9571 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9572
9573 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
27c329ed 9574 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
b432e5cf
VS
9575 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9576
9577 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9578 }
9579
9580 return max_pixel_rate;
9581}
9582
9583static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9584{
9585 struct drm_i915_private *dev_priv = dev->dev_private;
9586 uint32_t val, data;
9587 int ret;
9588
9589 if (WARN((I915_READ(LCPLL_CTL) &
9590 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9591 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9592 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9593 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9594 "trying to change cdclk frequency with cdclk not enabled\n"))
9595 return;
9596
9597 mutex_lock(&dev_priv->rps.hw_lock);
9598 ret = sandybridge_pcode_write(dev_priv,
9599 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9600 mutex_unlock(&dev_priv->rps.hw_lock);
9601 if (ret) {
9602 DRM_ERROR("failed to inform pcode about cdclk change\n");
9603 return;
9604 }
9605
9606 val = I915_READ(LCPLL_CTL);
9607 val |= LCPLL_CD_SOURCE_FCLK;
9608 I915_WRITE(LCPLL_CTL, val);
9609
9610 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9611 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9612 DRM_ERROR("Switching to FCLK failed\n");
9613
9614 val = I915_READ(LCPLL_CTL);
9615 val &= ~LCPLL_CLK_FREQ_MASK;
9616
9617 switch (cdclk) {
9618 case 450000:
9619 val |= LCPLL_CLK_FREQ_450;
9620 data = 0;
9621 break;
9622 case 540000:
9623 val |= LCPLL_CLK_FREQ_54O_BDW;
9624 data = 1;
9625 break;
9626 case 337500:
9627 val |= LCPLL_CLK_FREQ_337_5_BDW;
9628 data = 2;
9629 break;
9630 case 675000:
9631 val |= LCPLL_CLK_FREQ_675_BDW;
9632 data = 3;
9633 break;
9634 default:
9635 WARN(1, "invalid cdclk frequency\n");
9636 return;
9637 }
9638
9639 I915_WRITE(LCPLL_CTL, val);
9640
9641 val = I915_READ(LCPLL_CTL);
9642 val &= ~LCPLL_CD_SOURCE_FCLK;
9643 I915_WRITE(LCPLL_CTL, val);
9644
9645 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9646 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9647 DRM_ERROR("Switching back to LCPLL failed\n");
9648
9649 mutex_lock(&dev_priv->rps.hw_lock);
9650 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9651 mutex_unlock(&dev_priv->rps.hw_lock);
9652
9653 intel_update_cdclk(dev);
9654
9655 WARN(cdclk != dev_priv->cdclk_freq,
9656 "cdclk requested %d kHz but got %d kHz\n",
9657 cdclk, dev_priv->cdclk_freq);
9658}
9659
27c329ed 9660static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9661{
27c329ed
ML
9662 struct drm_i915_private *dev_priv = to_i915(state->dev);
9663 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9664 int cdclk;
9665
9666 /*
9667 * FIXME should also account for plane ratio
9668 * once 64bpp pixel formats are supported.
9669 */
27c329ed 9670 if (max_pixclk > 540000)
b432e5cf 9671 cdclk = 675000;
27c329ed 9672 else if (max_pixclk > 450000)
b432e5cf 9673 cdclk = 540000;
27c329ed 9674 else if (max_pixclk > 337500)
b432e5cf
VS
9675 cdclk = 450000;
9676 else
9677 cdclk = 337500;
9678
9679 /*
9680 * FIXME move the cdclk caclulation to
9681 * compute_config() so we can fail gracegully.
9682 */
9683 if (cdclk > dev_priv->max_cdclk_freq) {
9684 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9685 cdclk, dev_priv->max_cdclk_freq);
9686 cdclk = dev_priv->max_cdclk_freq;
9687 }
9688
27c329ed 9689 to_intel_atomic_state(state)->cdclk = cdclk;
b432e5cf
VS
9690
9691 return 0;
9692}
9693
27c329ed 9694static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9695{
27c329ed
ML
9696 struct drm_device *dev = old_state->dev;
9697 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
b432e5cf 9698
27c329ed 9699 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9700}
9701
190f68c5
ACO
9702static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9703 struct intel_crtc_state *crtc_state)
09b4ddf9 9704{
190f68c5 9705 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9706 return -EINVAL;
716c2e55 9707
c7653199 9708 crtc->lowfreq_avail = false;
644cef34 9709
c8f7a0db 9710 return 0;
79e53945
JB
9711}
9712
3760b59c
S
9713static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9714 enum port port,
9715 struct intel_crtc_state *pipe_config)
9716{
9717 switch (port) {
9718 case PORT_A:
9719 pipe_config->ddi_pll_sel = SKL_DPLL0;
9720 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9721 break;
9722 case PORT_B:
9723 pipe_config->ddi_pll_sel = SKL_DPLL1;
9724 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9725 break;
9726 case PORT_C:
9727 pipe_config->ddi_pll_sel = SKL_DPLL2;
9728 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9729 break;
9730 default:
9731 DRM_ERROR("Incorrect port type\n");
9732 }
9733}
9734
96b7dfb7
S
9735static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9736 enum port port,
5cec258b 9737 struct intel_crtc_state *pipe_config)
96b7dfb7 9738{
3148ade7 9739 u32 temp, dpll_ctl1;
96b7dfb7
S
9740
9741 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9742 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9743
9744 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9745 case SKL_DPLL0:
9746 /*
9747 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9748 * of the shared DPLL framework and thus needs to be read out
9749 * separately
9750 */
9751 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9752 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9753 break;
96b7dfb7
S
9754 case SKL_DPLL1:
9755 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9756 break;
9757 case SKL_DPLL2:
9758 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9759 break;
9760 case SKL_DPLL3:
9761 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9762 break;
96b7dfb7
S
9763 }
9764}
9765
7d2c8175
DL
9766static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9767 enum port port,
5cec258b 9768 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9769{
9770 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9771
9772 switch (pipe_config->ddi_pll_sel) {
9773 case PORT_CLK_SEL_WRPLL1:
9774 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9775 break;
9776 case PORT_CLK_SEL_WRPLL2:
9777 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9778 break;
9779 }
9780}
9781
26804afd 9782static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9783 struct intel_crtc_state *pipe_config)
26804afd
DV
9784{
9785 struct drm_device *dev = crtc->base.dev;
9786 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9787 struct intel_shared_dpll *pll;
26804afd
DV
9788 enum port port;
9789 uint32_t tmp;
9790
9791 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9792
9793 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9794
ef11bdb3 9795 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
96b7dfb7 9796 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9797 else if (IS_BROXTON(dev))
9798 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9799 else
9800 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9801
d452c5b6
DV
9802 if (pipe_config->shared_dpll >= 0) {
9803 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9804
9805 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9806 &pipe_config->dpll_hw_state));
9807 }
9808
26804afd
DV
9809 /*
9810 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9811 * DDI E. So just check whether this pipe is wired to DDI E and whether
9812 * the PCH transcoder is on.
9813 */
ca370455
DL
9814 if (INTEL_INFO(dev)->gen < 9 &&
9815 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9816 pipe_config->has_pch_encoder = true;
9817
9818 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9819 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9820 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9821
9822 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9823 }
9824}
9825
0e8ffe1b 9826static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9827 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9828{
9829 struct drm_device *dev = crtc->base.dev;
9830 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9831 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9832 uint32_t tmp;
9833
f458ebbc 9834 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9835 POWER_DOMAIN_PIPE(crtc->pipe)))
9836 return false;
9837
e143a21c 9838 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9839 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9840
eccb140b
DV
9841 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9842 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9843 enum pipe trans_edp_pipe;
9844 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9845 default:
9846 WARN(1, "unknown pipe linked to edp transcoder\n");
9847 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9848 case TRANS_DDI_EDP_INPUT_A_ON:
9849 trans_edp_pipe = PIPE_A;
9850 break;
9851 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9852 trans_edp_pipe = PIPE_B;
9853 break;
9854 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9855 trans_edp_pipe = PIPE_C;
9856 break;
9857 }
9858
9859 if (trans_edp_pipe == crtc->pipe)
9860 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9861 }
9862
f458ebbc 9863 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9864 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9865 return false;
9866
eccb140b 9867 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9868 if (!(tmp & PIPECONF_ENABLE))
9869 return false;
9870
26804afd 9871 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9872
1bd1bd80
DV
9873 intel_get_pipe_timings(crtc, pipe_config);
9874
a1b2278e
CK
9875 if (INTEL_INFO(dev)->gen >= 9) {
9876 skl_init_scalers(dev, crtc, pipe_config);
9877 }
9878
2fa2fe9a 9879 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9880
9881 if (INTEL_INFO(dev)->gen >= 9) {
9882 pipe_config->scaler_state.scaler_id = -1;
9883 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9884 }
9885
bd2e244f 9886 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
1c132b44 9887 if (INTEL_INFO(dev)->gen >= 9)
bd2e244f 9888 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9889 else
1c132b44 9890 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 9891 }
88adfff1 9892
e59150dc
JB
9893 if (IS_HASWELL(dev))
9894 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9895 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9896
ebb69c95
CT
9897 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9898 pipe_config->pixel_multiplier =
9899 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9900 } else {
9901 pipe_config->pixel_multiplier = 1;
9902 }
6c49f241 9903
0e8ffe1b
DV
9904 return true;
9905}
9906
560b85bb
CW
9907static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9908{
9909 struct drm_device *dev = crtc->dev;
9910 struct drm_i915_private *dev_priv = dev->dev_private;
9911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9912 uint32_t cntl = 0, size = 0;
560b85bb 9913
dc41c154 9914 if (base) {
3dd512fb
MR
9915 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9916 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9917 unsigned int stride = roundup_pow_of_two(width) * 4;
9918
9919 switch (stride) {
9920 default:
9921 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9922 width, stride);
9923 stride = 256;
9924 /* fallthrough */
9925 case 256:
9926 case 512:
9927 case 1024:
9928 case 2048:
9929 break;
4b0e333e
CW
9930 }
9931
dc41c154
VS
9932 cntl |= CURSOR_ENABLE |
9933 CURSOR_GAMMA_ENABLE |
9934 CURSOR_FORMAT_ARGB |
9935 CURSOR_STRIDE(stride);
9936
9937 size = (height << 12) | width;
4b0e333e 9938 }
560b85bb 9939
dc41c154
VS
9940 if (intel_crtc->cursor_cntl != 0 &&
9941 (intel_crtc->cursor_base != base ||
9942 intel_crtc->cursor_size != size ||
9943 intel_crtc->cursor_cntl != cntl)) {
9944 /* On these chipsets we can only modify the base/size/stride
9945 * whilst the cursor is disabled.
9946 */
0b87c24e
VS
9947 I915_WRITE(CURCNTR(PIPE_A), 0);
9948 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 9949 intel_crtc->cursor_cntl = 0;
4b0e333e 9950 }
560b85bb 9951
99d1f387 9952 if (intel_crtc->cursor_base != base) {
0b87c24e 9953 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
9954 intel_crtc->cursor_base = base;
9955 }
4726e0b0 9956
dc41c154
VS
9957 if (intel_crtc->cursor_size != size) {
9958 I915_WRITE(CURSIZE, size);
9959 intel_crtc->cursor_size = size;
4b0e333e 9960 }
560b85bb 9961
4b0e333e 9962 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
9963 I915_WRITE(CURCNTR(PIPE_A), cntl);
9964 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 9965 intel_crtc->cursor_cntl = cntl;
560b85bb 9966 }
560b85bb
CW
9967}
9968
560b85bb 9969static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9970{
9971 struct drm_device *dev = crtc->dev;
9972 struct drm_i915_private *dev_priv = dev->dev_private;
9973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9974 int pipe = intel_crtc->pipe;
4b0e333e
CW
9975 uint32_t cntl;
9976
9977 cntl = 0;
9978 if (base) {
9979 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9980 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9981 case 64:
9982 cntl |= CURSOR_MODE_64_ARGB_AX;
9983 break;
9984 case 128:
9985 cntl |= CURSOR_MODE_128_ARGB_AX;
9986 break;
9987 case 256:
9988 cntl |= CURSOR_MODE_256_ARGB_AX;
9989 break;
9990 default:
3dd512fb 9991 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9992 return;
65a21cd6 9993 }
4b0e333e 9994 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 9995
fc6f93bc 9996 if (HAS_DDI(dev))
47bf17a7 9997 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9998 }
65a21cd6 9999
8e7d688b 10000 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
10001 cntl |= CURSOR_ROTATE_180;
10002
4b0e333e
CW
10003 if (intel_crtc->cursor_cntl != cntl) {
10004 I915_WRITE(CURCNTR(pipe), cntl);
10005 POSTING_READ(CURCNTR(pipe));
10006 intel_crtc->cursor_cntl = cntl;
65a21cd6 10007 }
4b0e333e 10008
65a21cd6 10009 /* and commit changes on next vblank */
5efb3e28
VS
10010 I915_WRITE(CURBASE(pipe), base);
10011 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10012
10013 intel_crtc->cursor_base = base;
65a21cd6
JB
10014}
10015
cda4b7d3 10016/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
10017static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10018 bool on)
cda4b7d3
CW
10019{
10020 struct drm_device *dev = crtc->dev;
10021 struct drm_i915_private *dev_priv = dev->dev_private;
10022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10023 int pipe = intel_crtc->pipe;
9b4101be
ML
10024 struct drm_plane_state *cursor_state = crtc->cursor->state;
10025 int x = cursor_state->crtc_x;
10026 int y = cursor_state->crtc_y;
d6e4db15 10027 u32 base = 0, pos = 0;
cda4b7d3 10028
d6e4db15 10029 if (on)
cda4b7d3 10030 base = intel_crtc->cursor_addr;
cda4b7d3 10031
6e3c9717 10032 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
10033 base = 0;
10034
6e3c9717 10035 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
10036 base = 0;
10037
10038 if (x < 0) {
9b4101be 10039 if (x + cursor_state->crtc_w <= 0)
cda4b7d3
CW
10040 base = 0;
10041
10042 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10043 x = -x;
10044 }
10045 pos |= x << CURSOR_X_SHIFT;
10046
10047 if (y < 0) {
9b4101be 10048 if (y + cursor_state->crtc_h <= 0)
cda4b7d3
CW
10049 base = 0;
10050
10051 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10052 y = -y;
10053 }
10054 pos |= y << CURSOR_Y_SHIFT;
10055
4b0e333e 10056 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
10057 return;
10058
5efb3e28
VS
10059 I915_WRITE(CURPOS(pipe), pos);
10060
4398ad45
VS
10061 /* ILK+ do this automagically */
10062 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 10063 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
9b4101be
ML
10064 base += (cursor_state->crtc_h *
10065 cursor_state->crtc_w - 1) * 4;
4398ad45
VS
10066 }
10067
8ac54669 10068 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
10069 i845_update_cursor(crtc, base);
10070 else
10071 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
10072}
10073
dc41c154
VS
10074static bool cursor_size_ok(struct drm_device *dev,
10075 uint32_t width, uint32_t height)
10076{
10077 if (width == 0 || height == 0)
10078 return false;
10079
10080 /*
10081 * 845g/865g are special in that they are only limited by
10082 * the width of their cursors, the height is arbitrary up to
10083 * the precision of the register. Everything else requires
10084 * square cursors, limited to a few power-of-two sizes.
10085 */
10086 if (IS_845G(dev) || IS_I865G(dev)) {
10087 if ((width & 63) != 0)
10088 return false;
10089
10090 if (width > (IS_845G(dev) ? 64 : 512))
10091 return false;
10092
10093 if (height > 1023)
10094 return false;
10095 } else {
10096 switch (width | height) {
10097 case 256:
10098 case 128:
10099 if (IS_GEN2(dev))
10100 return false;
10101 case 64:
10102 break;
10103 default:
10104 return false;
10105 }
10106 }
10107
10108 return true;
10109}
10110
79e53945 10111static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10112 u16 *blue, uint32_t start, uint32_t size)
79e53945 10113{
7203425a 10114 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10116
7203425a 10117 for (i = start; i < end; i++) {
79e53945
JB
10118 intel_crtc->lut_r[i] = red[i] >> 8;
10119 intel_crtc->lut_g[i] = green[i] >> 8;
10120 intel_crtc->lut_b[i] = blue[i] >> 8;
10121 }
10122
10123 intel_crtc_load_lut(crtc);
10124}
10125
79e53945
JB
10126/* VESA 640x480x72Hz mode to set on the pipe */
10127static struct drm_display_mode load_detect_mode = {
10128 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10129 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10130};
10131
a8bb6818
DV
10132struct drm_framebuffer *
10133__intel_framebuffer_create(struct drm_device *dev,
10134 struct drm_mode_fb_cmd2 *mode_cmd,
10135 struct drm_i915_gem_object *obj)
d2dff872
CW
10136{
10137 struct intel_framebuffer *intel_fb;
10138 int ret;
10139
10140 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 10141 if (!intel_fb)
d2dff872 10142 return ERR_PTR(-ENOMEM);
d2dff872
CW
10143
10144 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10145 if (ret)
10146 goto err;
d2dff872
CW
10147
10148 return &intel_fb->base;
dcb1394e 10149
dd4916c5 10150err:
dd4916c5 10151 kfree(intel_fb);
dd4916c5 10152 return ERR_PTR(ret);
d2dff872
CW
10153}
10154
b5ea642a 10155static struct drm_framebuffer *
a8bb6818
DV
10156intel_framebuffer_create(struct drm_device *dev,
10157 struct drm_mode_fb_cmd2 *mode_cmd,
10158 struct drm_i915_gem_object *obj)
10159{
10160 struct drm_framebuffer *fb;
10161 int ret;
10162
10163 ret = i915_mutex_lock_interruptible(dev);
10164 if (ret)
10165 return ERR_PTR(ret);
10166 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10167 mutex_unlock(&dev->struct_mutex);
10168
10169 return fb;
10170}
10171
d2dff872
CW
10172static u32
10173intel_framebuffer_pitch_for_width(int width, int bpp)
10174{
10175 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10176 return ALIGN(pitch, 64);
10177}
10178
10179static u32
10180intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10181{
10182 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10183 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10184}
10185
10186static struct drm_framebuffer *
10187intel_framebuffer_create_for_mode(struct drm_device *dev,
10188 struct drm_display_mode *mode,
10189 int depth, int bpp)
10190{
dcb1394e 10191 struct drm_framebuffer *fb;
d2dff872 10192 struct drm_i915_gem_object *obj;
0fed39bd 10193 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10194
10195 obj = i915_gem_alloc_object(dev,
10196 intel_framebuffer_size_for_mode(mode, bpp));
10197 if (obj == NULL)
10198 return ERR_PTR(-ENOMEM);
10199
10200 mode_cmd.width = mode->hdisplay;
10201 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10202 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10203 bpp);
5ca0c34a 10204 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 10205
dcb1394e
LW
10206 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10207 if (IS_ERR(fb))
10208 drm_gem_object_unreference_unlocked(&obj->base);
10209
10210 return fb;
d2dff872
CW
10211}
10212
10213static struct drm_framebuffer *
10214mode_fits_in_fbdev(struct drm_device *dev,
10215 struct drm_display_mode *mode)
10216{
0695726e 10217#ifdef CONFIG_DRM_FBDEV_EMULATION
d2dff872
CW
10218 struct drm_i915_private *dev_priv = dev->dev_private;
10219 struct drm_i915_gem_object *obj;
10220 struct drm_framebuffer *fb;
10221
4c0e5528 10222 if (!dev_priv->fbdev)
d2dff872
CW
10223 return NULL;
10224
4c0e5528 10225 if (!dev_priv->fbdev->fb)
d2dff872
CW
10226 return NULL;
10227
4c0e5528
DV
10228 obj = dev_priv->fbdev->fb->obj;
10229 BUG_ON(!obj);
10230
8bcd4553 10231 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10232 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10233 fb->bits_per_pixel))
d2dff872
CW
10234 return NULL;
10235
01f2c773 10236 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10237 return NULL;
10238
10239 return fb;
4520f53a
DV
10240#else
10241 return NULL;
10242#endif
d2dff872
CW
10243}
10244
d3a40d1b
ACO
10245static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10246 struct drm_crtc *crtc,
10247 struct drm_display_mode *mode,
10248 struct drm_framebuffer *fb,
10249 int x, int y)
10250{
10251 struct drm_plane_state *plane_state;
10252 int hdisplay, vdisplay;
10253 int ret;
10254
10255 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10256 if (IS_ERR(plane_state))
10257 return PTR_ERR(plane_state);
10258
10259 if (mode)
10260 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10261 else
10262 hdisplay = vdisplay = 0;
10263
10264 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10265 if (ret)
10266 return ret;
10267 drm_atomic_set_fb_for_plane(plane_state, fb);
10268 plane_state->crtc_x = 0;
10269 plane_state->crtc_y = 0;
10270 plane_state->crtc_w = hdisplay;
10271 plane_state->crtc_h = vdisplay;
10272 plane_state->src_x = x << 16;
10273 plane_state->src_y = y << 16;
10274 plane_state->src_w = hdisplay << 16;
10275 plane_state->src_h = vdisplay << 16;
10276
10277 return 0;
10278}
10279
d2434ab7 10280bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10281 struct drm_display_mode *mode,
51fd371b
RC
10282 struct intel_load_detect_pipe *old,
10283 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10284{
10285 struct intel_crtc *intel_crtc;
d2434ab7
DV
10286 struct intel_encoder *intel_encoder =
10287 intel_attached_encoder(connector);
79e53945 10288 struct drm_crtc *possible_crtc;
4ef69c7a 10289 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10290 struct drm_crtc *crtc = NULL;
10291 struct drm_device *dev = encoder->dev;
94352cf9 10292 struct drm_framebuffer *fb;
51fd371b 10293 struct drm_mode_config *config = &dev->mode_config;
83a57153 10294 struct drm_atomic_state *state = NULL;
944b0c76 10295 struct drm_connector_state *connector_state;
4be07317 10296 struct intel_crtc_state *crtc_state;
51fd371b 10297 int ret, i = -1;
79e53945 10298
d2dff872 10299 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10300 connector->base.id, connector->name,
8e329a03 10301 encoder->base.id, encoder->name);
d2dff872 10302
51fd371b
RC
10303retry:
10304 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10305 if (ret)
ad3c558f 10306 goto fail;
6e9f798d 10307
79e53945
JB
10308 /*
10309 * Algorithm gets a little messy:
7a5e4805 10310 *
79e53945
JB
10311 * - if the connector already has an assigned crtc, use it (but make
10312 * sure it's on first)
7a5e4805 10313 *
79e53945
JB
10314 * - try to find the first unused crtc that can drive this connector,
10315 * and use that if we find one
79e53945
JB
10316 */
10317
10318 /* See if we already have a CRTC for this connector */
10319 if (encoder->crtc) {
10320 crtc = encoder->crtc;
8261b191 10321
51fd371b 10322 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10323 if (ret)
ad3c558f 10324 goto fail;
4d02e2de 10325 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b 10326 if (ret)
ad3c558f 10327 goto fail;
7b24056b 10328
24218aac 10329 old->dpms_mode = connector->dpms;
8261b191
CW
10330 old->load_detect_temp = false;
10331
10332 /* Make sure the crtc and connector are running */
24218aac
DV
10333 if (connector->dpms != DRM_MODE_DPMS_ON)
10334 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10335
7173188d 10336 return true;
79e53945
JB
10337 }
10338
10339 /* Find an unused one (if possible) */
70e1e0ec 10340 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10341 i++;
10342 if (!(encoder->possible_crtcs & (1 << i)))
10343 continue;
83d65738 10344 if (possible_crtc->state->enable)
a459249c 10345 continue;
a459249c
VS
10346
10347 crtc = possible_crtc;
10348 break;
79e53945
JB
10349 }
10350
10351 /*
10352 * If we didn't find an unused CRTC, don't use any.
10353 */
10354 if (!crtc) {
7173188d 10355 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 10356 goto fail;
79e53945
JB
10357 }
10358
51fd371b
RC
10359 ret = drm_modeset_lock(&crtc->mutex, ctx);
10360 if (ret)
ad3c558f 10361 goto fail;
4d02e2de
DV
10362 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10363 if (ret)
ad3c558f 10364 goto fail;
79e53945
JB
10365
10366 intel_crtc = to_intel_crtc(crtc);
24218aac 10367 old->dpms_mode = connector->dpms;
8261b191 10368 old->load_detect_temp = true;
d2dff872 10369 old->release_fb = NULL;
79e53945 10370
83a57153
ACO
10371 state = drm_atomic_state_alloc(dev);
10372 if (!state)
10373 return false;
10374
10375 state->acquire_ctx = ctx;
10376
944b0c76
ACO
10377 connector_state = drm_atomic_get_connector_state(state, connector);
10378 if (IS_ERR(connector_state)) {
10379 ret = PTR_ERR(connector_state);
10380 goto fail;
10381 }
10382
10383 connector_state->crtc = crtc;
10384 connector_state->best_encoder = &intel_encoder->base;
10385
4be07317
ACO
10386 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10387 if (IS_ERR(crtc_state)) {
10388 ret = PTR_ERR(crtc_state);
10389 goto fail;
10390 }
10391
49d6fa21 10392 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10393
6492711d
CW
10394 if (!mode)
10395 mode = &load_detect_mode;
79e53945 10396
d2dff872
CW
10397 /* We need a framebuffer large enough to accommodate all accesses
10398 * that the plane may generate whilst we perform load detection.
10399 * We can not rely on the fbcon either being present (we get called
10400 * during its initialisation to detect all boot displays, or it may
10401 * not even exist) or that it is large enough to satisfy the
10402 * requested mode.
10403 */
94352cf9
DV
10404 fb = mode_fits_in_fbdev(dev, mode);
10405 if (fb == NULL) {
d2dff872 10406 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10407 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10408 old->release_fb = fb;
d2dff872
CW
10409 } else
10410 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10411 if (IS_ERR(fb)) {
d2dff872 10412 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10413 goto fail;
79e53945 10414 }
79e53945 10415
d3a40d1b
ACO
10416 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10417 if (ret)
10418 goto fail;
10419
8c7b5ccb
ACO
10420 drm_mode_copy(&crtc_state->base.mode, mode);
10421
74c090b1 10422 if (drm_atomic_commit(state)) {
6492711d 10423 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10424 if (old->release_fb)
10425 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10426 goto fail;
79e53945 10427 }
9128b040 10428 crtc->primary->crtc = crtc;
7173188d 10429
79e53945 10430 /* let the connector get through one full cycle before testing */
9d0498a2 10431 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10432 return true;
412b61d8 10433
ad3c558f 10434fail:
e5d958ef
ACO
10435 drm_atomic_state_free(state);
10436 state = NULL;
83a57153 10437
51fd371b
RC
10438 if (ret == -EDEADLK) {
10439 drm_modeset_backoff(ctx);
10440 goto retry;
10441 }
10442
412b61d8 10443 return false;
79e53945
JB
10444}
10445
d2434ab7 10446void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10447 struct intel_load_detect_pipe *old,
10448 struct drm_modeset_acquire_ctx *ctx)
79e53945 10449{
83a57153 10450 struct drm_device *dev = connector->dev;
d2434ab7
DV
10451 struct intel_encoder *intel_encoder =
10452 intel_attached_encoder(connector);
4ef69c7a 10453 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10454 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10456 struct drm_atomic_state *state;
944b0c76 10457 struct drm_connector_state *connector_state;
4be07317 10458 struct intel_crtc_state *crtc_state;
d3a40d1b 10459 int ret;
79e53945 10460
d2dff872 10461 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10462 connector->base.id, connector->name,
8e329a03 10463 encoder->base.id, encoder->name);
d2dff872 10464
8261b191 10465 if (old->load_detect_temp) {
83a57153 10466 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10467 if (!state)
10468 goto fail;
83a57153
ACO
10469
10470 state->acquire_ctx = ctx;
10471
944b0c76
ACO
10472 connector_state = drm_atomic_get_connector_state(state, connector);
10473 if (IS_ERR(connector_state))
10474 goto fail;
10475
4be07317
ACO
10476 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10477 if (IS_ERR(crtc_state))
10478 goto fail;
10479
944b0c76
ACO
10480 connector_state->best_encoder = NULL;
10481 connector_state->crtc = NULL;
10482
49d6fa21 10483 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10484
d3a40d1b
ACO
10485 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10486 0, 0);
10487 if (ret)
10488 goto fail;
10489
74c090b1 10490 ret = drm_atomic_commit(state);
2bfb4627
ACO
10491 if (ret)
10492 goto fail;
d2dff872 10493
36206361
DV
10494 if (old->release_fb) {
10495 drm_framebuffer_unregister_private(old->release_fb);
10496 drm_framebuffer_unreference(old->release_fb);
10497 }
d2dff872 10498
0622a53c 10499 return;
79e53945
JB
10500 }
10501
c751ce4f 10502 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10503 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10504 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10505
10506 return;
10507fail:
10508 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10509 drm_atomic_state_free(state);
79e53945
JB
10510}
10511
da4a1efa 10512static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10513 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10514{
10515 struct drm_i915_private *dev_priv = dev->dev_private;
10516 u32 dpll = pipe_config->dpll_hw_state.dpll;
10517
10518 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10519 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10520 else if (HAS_PCH_SPLIT(dev))
10521 return 120000;
10522 else if (!IS_GEN2(dev))
10523 return 96000;
10524 else
10525 return 48000;
10526}
10527
79e53945 10528/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10529static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10530 struct intel_crtc_state *pipe_config)
79e53945 10531{
f1f644dc 10532 struct drm_device *dev = crtc->base.dev;
79e53945 10533 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10534 int pipe = pipe_config->cpu_transcoder;
293623f7 10535 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10536 u32 fp;
10537 intel_clock_t clock;
dccbea3b 10538 int port_clock;
da4a1efa 10539 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10540
10541 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10542 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10543 else
293623f7 10544 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10545
10546 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10547 if (IS_PINEVIEW(dev)) {
10548 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10549 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10550 } else {
10551 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10552 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10553 }
10554
a6c45cf0 10555 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10556 if (IS_PINEVIEW(dev))
10557 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10558 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10559 else
10560 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10561 DPLL_FPA01_P1_POST_DIV_SHIFT);
10562
10563 switch (dpll & DPLL_MODE_MASK) {
10564 case DPLLB_MODE_DAC_SERIAL:
10565 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10566 5 : 10;
10567 break;
10568 case DPLLB_MODE_LVDS:
10569 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10570 7 : 14;
10571 break;
10572 default:
28c97730 10573 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10574 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10575 return;
79e53945
JB
10576 }
10577
ac58c3f0 10578 if (IS_PINEVIEW(dev))
dccbea3b 10579 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 10580 else
dccbea3b 10581 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 10582 } else {
0fb58223 10583 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10584 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10585
10586 if (is_lvds) {
10587 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10588 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10589
10590 if (lvds & LVDS_CLKB_POWER_UP)
10591 clock.p2 = 7;
10592 else
10593 clock.p2 = 14;
79e53945
JB
10594 } else {
10595 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10596 clock.p1 = 2;
10597 else {
10598 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10599 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10600 }
10601 if (dpll & PLL_P2_DIVIDE_BY_4)
10602 clock.p2 = 4;
10603 else
10604 clock.p2 = 2;
79e53945 10605 }
da4a1efa 10606
dccbea3b 10607 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
10608 }
10609
18442d08
VS
10610 /*
10611 * This value includes pixel_multiplier. We will use
241bfc38 10612 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10613 * encoder's get_config() function.
10614 */
dccbea3b 10615 pipe_config->port_clock = port_clock;
f1f644dc
JB
10616}
10617
6878da05
VS
10618int intel_dotclock_calculate(int link_freq,
10619 const struct intel_link_m_n *m_n)
f1f644dc 10620{
f1f644dc
JB
10621 /*
10622 * The calculation for the data clock is:
1041a02f 10623 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10624 * But we want to avoid losing precison if possible, so:
1041a02f 10625 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10626 *
10627 * and the link clock is simpler:
1041a02f 10628 * link_clock = (m * link_clock) / n
f1f644dc
JB
10629 */
10630
6878da05
VS
10631 if (!m_n->link_n)
10632 return 0;
f1f644dc 10633
6878da05
VS
10634 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10635}
f1f644dc 10636
18442d08 10637static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10638 struct intel_crtc_state *pipe_config)
6878da05
VS
10639{
10640 struct drm_device *dev = crtc->base.dev;
79e53945 10641
18442d08
VS
10642 /* read out port_clock from the DPLL */
10643 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10644
f1f644dc 10645 /*
18442d08 10646 * This value does not include pixel_multiplier.
241bfc38 10647 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10648 * agree once we know their relationship in the encoder's
10649 * get_config() function.
79e53945 10650 */
2d112de7 10651 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10652 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10653 &pipe_config->fdi_m_n);
79e53945
JB
10654}
10655
10656/** Returns the currently programmed mode of the given pipe. */
10657struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10658 struct drm_crtc *crtc)
10659{
548f245b 10660 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10662 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10663 struct drm_display_mode *mode;
5cec258b 10664 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10665 int htot = I915_READ(HTOTAL(cpu_transcoder));
10666 int hsync = I915_READ(HSYNC(cpu_transcoder));
10667 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10668 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10669 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10670
10671 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10672 if (!mode)
10673 return NULL;
10674
f1f644dc
JB
10675 /*
10676 * Construct a pipe_config sufficient for getting the clock info
10677 * back out of crtc_clock_get.
10678 *
10679 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10680 * to use a real value here instead.
10681 */
293623f7 10682 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10683 pipe_config.pixel_multiplier = 1;
293623f7
VS
10684 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10685 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10686 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10687 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10688
773ae034 10689 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10690 mode->hdisplay = (htot & 0xffff) + 1;
10691 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10692 mode->hsync_start = (hsync & 0xffff) + 1;
10693 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10694 mode->vdisplay = (vtot & 0xffff) + 1;
10695 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10696 mode->vsync_start = (vsync & 0xffff) + 1;
10697 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10698
10699 drm_mode_set_name(mode);
79e53945
JB
10700
10701 return mode;
10702}
10703
f047e395
CW
10704void intel_mark_busy(struct drm_device *dev)
10705{
c67a470b
PZ
10706 struct drm_i915_private *dev_priv = dev->dev_private;
10707
f62a0076
CW
10708 if (dev_priv->mm.busy)
10709 return;
10710
43694d69 10711 intel_runtime_pm_get(dev_priv);
c67a470b 10712 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10713 if (INTEL_INFO(dev)->gen >= 6)
10714 gen6_rps_busy(dev_priv);
f62a0076 10715 dev_priv->mm.busy = true;
f047e395
CW
10716}
10717
10718void intel_mark_idle(struct drm_device *dev)
652c393a 10719{
c67a470b 10720 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10721
f62a0076
CW
10722 if (!dev_priv->mm.busy)
10723 return;
10724
10725 dev_priv->mm.busy = false;
10726
3d13ef2e 10727 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10728 gen6_rps_idle(dev->dev_private);
bb4cdd53 10729
43694d69 10730 intel_runtime_pm_put(dev_priv);
652c393a
JB
10731}
10732
79e53945
JB
10733static void intel_crtc_destroy(struct drm_crtc *crtc)
10734{
10735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10736 struct drm_device *dev = crtc->dev;
10737 struct intel_unpin_work *work;
67e77c5a 10738
5e2d7afc 10739 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10740 work = intel_crtc->unpin_work;
10741 intel_crtc->unpin_work = NULL;
5e2d7afc 10742 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10743
10744 if (work) {
10745 cancel_work_sync(&work->work);
10746 kfree(work);
10747 }
79e53945
JB
10748
10749 drm_crtc_cleanup(crtc);
67e77c5a 10750
79e53945
JB
10751 kfree(intel_crtc);
10752}
10753
6b95a207
KH
10754static void intel_unpin_work_fn(struct work_struct *__work)
10755{
10756 struct intel_unpin_work *work =
10757 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10758 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10759 struct drm_device *dev = crtc->base.dev;
10760 struct drm_plane *primary = crtc->base.primary;
6b95a207 10761
b4a98e57 10762 mutex_lock(&dev->struct_mutex);
a9ff8714 10763 intel_unpin_fb_obj(work->old_fb, primary->state);
05394f39 10764 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10765
f06cc1b9 10766 if (work->flip_queued_req)
146d84f0 10767 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10768 mutex_unlock(&dev->struct_mutex);
10769
a9ff8714 10770 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
89ed88ba 10771 drm_framebuffer_unreference(work->old_fb);
f99d7069 10772
a9ff8714
VS
10773 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10774 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10775
6b95a207
KH
10776 kfree(work);
10777}
10778
1afe3e9d 10779static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10780 struct drm_crtc *crtc)
6b95a207 10781{
6b95a207
KH
10782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10783 struct intel_unpin_work *work;
6b95a207
KH
10784 unsigned long flags;
10785
10786 /* Ignore early vblank irqs */
10787 if (intel_crtc == NULL)
10788 return;
10789
f326038a
DV
10790 /*
10791 * This is called both by irq handlers and the reset code (to complete
10792 * lost pageflips) so needs the full irqsave spinlocks.
10793 */
6b95a207
KH
10794 spin_lock_irqsave(&dev->event_lock, flags);
10795 work = intel_crtc->unpin_work;
e7d841ca
CW
10796
10797 /* Ensure we don't miss a work->pending update ... */
10798 smp_rmb();
10799
10800 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10801 spin_unlock_irqrestore(&dev->event_lock, flags);
10802 return;
10803 }
10804
d6bbafa1 10805 page_flip_completed(intel_crtc);
0af7e4df 10806
6b95a207 10807 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10808}
10809
1afe3e9d
JB
10810void intel_finish_page_flip(struct drm_device *dev, int pipe)
10811{
fbee40df 10812 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10813 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10814
49b14a5c 10815 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10816}
10817
10818void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10819{
fbee40df 10820 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10821 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10822
49b14a5c 10823 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10824}
10825
75f7f3ec
VS
10826/* Is 'a' after or equal to 'b'? */
10827static bool g4x_flip_count_after_eq(u32 a, u32 b)
10828{
10829 return !((a - b) & 0x80000000);
10830}
10831
10832static bool page_flip_finished(struct intel_crtc *crtc)
10833{
10834 struct drm_device *dev = crtc->base.dev;
10835 struct drm_i915_private *dev_priv = dev->dev_private;
10836
bdfa7542
VS
10837 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10838 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10839 return true;
10840
75f7f3ec
VS
10841 /*
10842 * The relevant registers doen't exist on pre-ctg.
10843 * As the flip done interrupt doesn't trigger for mmio
10844 * flips on gmch platforms, a flip count check isn't
10845 * really needed there. But since ctg has the registers,
10846 * include it in the check anyway.
10847 */
10848 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10849 return true;
10850
10851 /*
10852 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10853 * used the same base address. In that case the mmio flip might
10854 * have completed, but the CS hasn't even executed the flip yet.
10855 *
10856 * A flip count check isn't enough as the CS might have updated
10857 * the base address just after start of vblank, but before we
10858 * managed to process the interrupt. This means we'd complete the
10859 * CS flip too soon.
10860 *
10861 * Combining both checks should get us a good enough result. It may
10862 * still happen that the CS flip has been executed, but has not
10863 * yet actually completed. But in case the base address is the same
10864 * anyway, we don't really care.
10865 */
10866 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10867 crtc->unpin_work->gtt_offset &&
fd8f507c 10868 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
75f7f3ec
VS
10869 crtc->unpin_work->flip_count);
10870}
10871
6b95a207
KH
10872void intel_prepare_page_flip(struct drm_device *dev, int plane)
10873{
fbee40df 10874 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10875 struct intel_crtc *intel_crtc =
10876 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10877 unsigned long flags;
10878
f326038a
DV
10879
10880 /*
10881 * This is called both by irq handlers and the reset code (to complete
10882 * lost pageflips) so needs the full irqsave spinlocks.
10883 *
10884 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10885 * generate a page-flip completion irq, i.e. every modeset
10886 * is also accompanied by a spurious intel_prepare_page_flip().
10887 */
6b95a207 10888 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10889 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10890 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10891 spin_unlock_irqrestore(&dev->event_lock, flags);
10892}
10893
6042639c 10894static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
e7d841ca
CW
10895{
10896 /* Ensure that the work item is consistent when activating it ... */
10897 smp_wmb();
6042639c 10898 atomic_set(&work->pending, INTEL_FLIP_PENDING);
e7d841ca
CW
10899 /* and that it is marked active as soon as the irq could fire. */
10900 smp_wmb();
10901}
10902
8c9f3aaf
JB
10903static int intel_gen2_queue_flip(struct drm_device *dev,
10904 struct drm_crtc *crtc,
10905 struct drm_framebuffer *fb,
ed8d1975 10906 struct drm_i915_gem_object *obj,
6258fbe2 10907 struct drm_i915_gem_request *req,
ed8d1975 10908 uint32_t flags)
8c9f3aaf 10909{
6258fbe2 10910 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10912 u32 flip_mask;
10913 int ret;
10914
5fb9de1a 10915 ret = intel_ring_begin(req, 6);
8c9f3aaf 10916 if (ret)
4fa62c89 10917 return ret;
8c9f3aaf
JB
10918
10919 /* Can't queue multiple flips, so wait for the previous
10920 * one to finish before executing the next.
10921 */
10922 if (intel_crtc->plane)
10923 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10924 else
10925 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10926 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10927 intel_ring_emit(ring, MI_NOOP);
10928 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10929 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10930 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10931 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10932 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca 10933
6042639c 10934 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10935 return 0;
8c9f3aaf
JB
10936}
10937
10938static int intel_gen3_queue_flip(struct drm_device *dev,
10939 struct drm_crtc *crtc,
10940 struct drm_framebuffer *fb,
ed8d1975 10941 struct drm_i915_gem_object *obj,
6258fbe2 10942 struct drm_i915_gem_request *req,
ed8d1975 10943 uint32_t flags)
8c9f3aaf 10944{
6258fbe2 10945 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10947 u32 flip_mask;
10948 int ret;
10949
5fb9de1a 10950 ret = intel_ring_begin(req, 6);
8c9f3aaf 10951 if (ret)
4fa62c89 10952 return ret;
8c9f3aaf
JB
10953
10954 if (intel_crtc->plane)
10955 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10956 else
10957 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10958 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10959 intel_ring_emit(ring, MI_NOOP);
10960 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10961 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10962 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10963 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10964 intel_ring_emit(ring, MI_NOOP);
10965
6042639c 10966 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 10967 return 0;
8c9f3aaf
JB
10968}
10969
10970static int intel_gen4_queue_flip(struct drm_device *dev,
10971 struct drm_crtc *crtc,
10972 struct drm_framebuffer *fb,
ed8d1975 10973 struct drm_i915_gem_object *obj,
6258fbe2 10974 struct drm_i915_gem_request *req,
ed8d1975 10975 uint32_t flags)
8c9f3aaf 10976{
6258fbe2 10977 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10978 struct drm_i915_private *dev_priv = dev->dev_private;
10979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10980 uint32_t pf, pipesrc;
10981 int ret;
10982
5fb9de1a 10983 ret = intel_ring_begin(req, 4);
8c9f3aaf 10984 if (ret)
4fa62c89 10985 return ret;
8c9f3aaf
JB
10986
10987 /* i965+ uses the linear or tiled offsets from the
10988 * Display Registers (which do not change across a page-flip)
10989 * so we need only reprogram the base address.
10990 */
6d90c952
DV
10991 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10992 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10993 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10994 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10995 obj->tiling_mode);
8c9f3aaf
JB
10996
10997 /* XXX Enabling the panel-fitter across page-flip is so far
10998 * untested on non-native modes, so ignore it for now.
10999 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11000 */
11001 pf = 0;
11002 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11003 intel_ring_emit(ring, pf | pipesrc);
e7d841ca 11004
6042639c 11005 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11006 return 0;
8c9f3aaf
JB
11007}
11008
11009static int intel_gen6_queue_flip(struct drm_device *dev,
11010 struct drm_crtc *crtc,
11011 struct drm_framebuffer *fb,
ed8d1975 11012 struct drm_i915_gem_object *obj,
6258fbe2 11013 struct drm_i915_gem_request *req,
ed8d1975 11014 uint32_t flags)
8c9f3aaf 11015{
6258fbe2 11016 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
11017 struct drm_i915_private *dev_priv = dev->dev_private;
11018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11019 uint32_t pf, pipesrc;
11020 int ret;
11021
5fb9de1a 11022 ret = intel_ring_begin(req, 4);
8c9f3aaf 11023 if (ret)
4fa62c89 11024 return ret;
8c9f3aaf 11025
6d90c952
DV
11026 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11027 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11028 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11029 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11030
dc257cf1
DV
11031 /* Contrary to the suggestions in the documentation,
11032 * "Enable Panel Fitter" does not seem to be required when page
11033 * flipping with a non-native mode, and worse causes a normal
11034 * modeset to fail.
11035 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11036 */
11037 pf = 0;
8c9f3aaf 11038 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11039 intel_ring_emit(ring, pf | pipesrc);
e7d841ca 11040
6042639c 11041 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11042 return 0;
8c9f3aaf
JB
11043}
11044
7c9017e5
JB
11045static int intel_gen7_queue_flip(struct drm_device *dev,
11046 struct drm_crtc *crtc,
11047 struct drm_framebuffer *fb,
ed8d1975 11048 struct drm_i915_gem_object *obj,
6258fbe2 11049 struct drm_i915_gem_request *req,
ed8d1975 11050 uint32_t flags)
7c9017e5 11051{
6258fbe2 11052 struct intel_engine_cs *ring = req->ring;
7c9017e5 11053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11054 uint32_t plane_bit = 0;
ffe74d75
CW
11055 int len, ret;
11056
eba905b2 11057 switch (intel_crtc->plane) {
cb05d8de
DV
11058 case PLANE_A:
11059 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11060 break;
11061 case PLANE_B:
11062 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11063 break;
11064 case PLANE_C:
11065 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11066 break;
11067 default:
11068 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11069 return -ENODEV;
cb05d8de
DV
11070 }
11071
ffe74d75 11072 len = 4;
f476828a 11073 if (ring->id == RCS) {
ffe74d75 11074 len += 6;
f476828a
DL
11075 /*
11076 * On Gen 8, SRM is now taking an extra dword to accommodate
11077 * 48bits addresses, and we need a NOOP for the batch size to
11078 * stay even.
11079 */
11080 if (IS_GEN8(dev))
11081 len += 2;
11082 }
ffe74d75 11083
f66fab8e
VS
11084 /*
11085 * BSpec MI_DISPLAY_FLIP for IVB:
11086 * "The full packet must be contained within the same cache line."
11087 *
11088 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11089 * cacheline, if we ever start emitting more commands before
11090 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11091 * then do the cacheline alignment, and finally emit the
11092 * MI_DISPLAY_FLIP.
11093 */
bba09b12 11094 ret = intel_ring_cacheline_align(req);
f66fab8e 11095 if (ret)
4fa62c89 11096 return ret;
f66fab8e 11097
5fb9de1a 11098 ret = intel_ring_begin(req, len);
7c9017e5 11099 if (ret)
4fa62c89 11100 return ret;
7c9017e5 11101
ffe74d75
CW
11102 /* Unmask the flip-done completion message. Note that the bspec says that
11103 * we should do this for both the BCS and RCS, and that we must not unmask
11104 * more than one flip event at any time (or ensure that one flip message
11105 * can be sent by waiting for flip-done prior to queueing new flips).
11106 * Experimentation says that BCS works despite DERRMR masking all
11107 * flip-done completion events and that unmasking all planes at once
11108 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11109 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11110 */
11111 if (ring->id == RCS) {
11112 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11113 intel_ring_emit(ring, DERRMR);
11114 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11115 DERRMR_PIPEB_PRI_FLIP_DONE |
11116 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a 11117 if (IS_GEN8(dev))
f1afe24f 11118 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
f476828a
DL
11119 MI_SRM_LRM_GLOBAL_GTT);
11120 else
f1afe24f 11121 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
f476828a 11122 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11123 intel_ring_emit(ring, DERRMR);
11124 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11125 if (IS_GEN8(dev)) {
11126 intel_ring_emit(ring, 0);
11127 intel_ring_emit(ring, MI_NOOP);
11128 }
ffe74d75
CW
11129 }
11130
cb05d8de 11131 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11132 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11133 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11134 intel_ring_emit(ring, (MI_NOOP));
e7d841ca 11135
6042639c 11136 intel_mark_page_flip_active(intel_crtc->unpin_work);
83d4092b 11137 return 0;
7c9017e5
JB
11138}
11139
84c33a64
SG
11140static bool use_mmio_flip(struct intel_engine_cs *ring,
11141 struct drm_i915_gem_object *obj)
11142{
11143 /*
11144 * This is not being used for older platforms, because
11145 * non-availability of flip done interrupt forces us to use
11146 * CS flips. Older platforms derive flip done using some clever
11147 * tricks involving the flip_pending status bits and vblank irqs.
11148 * So using MMIO flips there would disrupt this mechanism.
11149 */
11150
8e09bf83
CW
11151 if (ring == NULL)
11152 return true;
11153
84c33a64
SG
11154 if (INTEL_INFO(ring->dev)->gen < 5)
11155 return false;
11156
11157 if (i915.use_mmio_flip < 0)
11158 return false;
11159 else if (i915.use_mmio_flip > 0)
11160 return true;
14bf993e
OM
11161 else if (i915.enable_execlists)
11162 return true;
84c33a64 11163 else
b4716185 11164 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11165}
11166
6042639c 11167static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
86efe24a 11168 unsigned int rotation,
6042639c 11169 struct intel_unpin_work *work)
ff944564
DL
11170{
11171 struct drm_device *dev = intel_crtc->base.dev;
11172 struct drm_i915_private *dev_priv = dev->dev_private;
11173 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564 11174 const enum pipe pipe = intel_crtc->pipe;
86efe24a 11175 u32 ctl, stride, tile_height;
ff944564
DL
11176
11177 ctl = I915_READ(PLANE_CTL(pipe, 0));
11178 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11179 switch (fb->modifier[0]) {
11180 case DRM_FORMAT_MOD_NONE:
11181 break;
11182 case I915_FORMAT_MOD_X_TILED:
ff944564 11183 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11184 break;
11185 case I915_FORMAT_MOD_Y_TILED:
11186 ctl |= PLANE_CTL_TILED_Y;
11187 break;
11188 case I915_FORMAT_MOD_Yf_TILED:
11189 ctl |= PLANE_CTL_TILED_YF;
11190 break;
11191 default:
11192 MISSING_CASE(fb->modifier[0]);
11193 }
ff944564
DL
11194
11195 /*
11196 * The stride is either expressed as a multiple of 64 bytes chunks for
11197 * linear buffers or in number of tiles for tiled buffers.
11198 */
86efe24a
TU
11199 if (intel_rotation_90_or_270(rotation)) {
11200 /* stride = Surface height in tiles */
11201 tile_height = intel_tile_height(dev, fb->pixel_format,
11202 fb->modifier[0], 0);
11203 stride = DIV_ROUND_UP(fb->height, tile_height);
11204 } else {
11205 stride = fb->pitches[0] /
11206 intel_fb_stride_alignment(dev, fb->modifier[0],
11207 fb->pixel_format);
11208 }
ff944564
DL
11209
11210 /*
11211 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11212 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11213 */
11214 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11215 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11216
6042639c 11217 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
ff944564
DL
11218 POSTING_READ(PLANE_SURF(pipe, 0));
11219}
11220
6042639c
CW
11221static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11222 struct intel_unpin_work *work)
84c33a64
SG
11223{
11224 struct drm_device *dev = intel_crtc->base.dev;
11225 struct drm_i915_private *dev_priv = dev->dev_private;
11226 struct intel_framebuffer *intel_fb =
11227 to_intel_framebuffer(intel_crtc->base.primary->fb);
11228 struct drm_i915_gem_object *obj = intel_fb->obj;
11229 u32 dspcntr;
11230 u32 reg;
11231
84c33a64
SG
11232 reg = DSPCNTR(intel_crtc->plane);
11233 dspcntr = I915_READ(reg);
11234
c5d97472
DL
11235 if (obj->tiling_mode != I915_TILING_NONE)
11236 dspcntr |= DISPPLANE_TILED;
11237 else
11238 dspcntr &= ~DISPPLANE_TILED;
11239
84c33a64
SG
11240 I915_WRITE(reg, dspcntr);
11241
6042639c 11242 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
84c33a64 11243 POSTING_READ(DSPSURF(intel_crtc->plane));
ff944564
DL
11244}
11245
11246/*
11247 * XXX: This is the temporary way to update the plane registers until we get
11248 * around to using the usual plane update functions for MMIO flips
11249 */
6042639c 11250static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
ff944564 11251{
6042639c
CW
11252 struct intel_crtc *crtc = mmio_flip->crtc;
11253 struct intel_unpin_work *work;
11254
11255 spin_lock_irq(&crtc->base.dev->event_lock);
11256 work = crtc->unpin_work;
11257 spin_unlock_irq(&crtc->base.dev->event_lock);
11258 if (work == NULL)
11259 return;
ff944564 11260
6042639c 11261 intel_mark_page_flip_active(work);
ff944564 11262
6042639c 11263 intel_pipe_update_start(crtc);
ff944564 11264
6042639c 11265 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
86efe24a 11266 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
ff944564
DL
11267 else
11268 /* use_mmio_flip() retricts MMIO flips to ilk+ */
6042639c 11269 ilk_do_mmio_flip(crtc, work);
ff944564 11270
6042639c 11271 intel_pipe_update_end(crtc);
84c33a64
SG
11272}
11273
9362c7c5 11274static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11275{
b2cfe0ab
CW
11276 struct intel_mmio_flip *mmio_flip =
11277 container_of(work, struct intel_mmio_flip, work);
84c33a64 11278
6042639c 11279 if (mmio_flip->req) {
eed29a5b 11280 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11281 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11282 false, NULL,
11283 &mmio_flip->i915->rps.mmioflips));
6042639c
CW
11284 i915_gem_request_unreference__unlocked(mmio_flip->req);
11285 }
84c33a64 11286
6042639c 11287 intel_do_mmio_flip(mmio_flip);
b2cfe0ab 11288 kfree(mmio_flip);
84c33a64
SG
11289}
11290
11291static int intel_queue_mmio_flip(struct drm_device *dev,
11292 struct drm_crtc *crtc,
86efe24a 11293 struct drm_i915_gem_object *obj)
84c33a64 11294{
b2cfe0ab
CW
11295 struct intel_mmio_flip *mmio_flip;
11296
11297 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11298 if (mmio_flip == NULL)
11299 return -ENOMEM;
84c33a64 11300
bcafc4e3 11301 mmio_flip->i915 = to_i915(dev);
eed29a5b 11302 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11303 mmio_flip->crtc = to_intel_crtc(crtc);
86efe24a 11304 mmio_flip->rotation = crtc->primary->state->rotation;
536f5b5e 11305
b2cfe0ab
CW
11306 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11307 schedule_work(&mmio_flip->work);
84c33a64 11308
84c33a64
SG
11309 return 0;
11310}
11311
8c9f3aaf
JB
11312static int intel_default_queue_flip(struct drm_device *dev,
11313 struct drm_crtc *crtc,
11314 struct drm_framebuffer *fb,
ed8d1975 11315 struct drm_i915_gem_object *obj,
6258fbe2 11316 struct drm_i915_gem_request *req,
ed8d1975 11317 uint32_t flags)
8c9f3aaf
JB
11318{
11319 return -ENODEV;
11320}
11321
d6bbafa1
CW
11322static bool __intel_pageflip_stall_check(struct drm_device *dev,
11323 struct drm_crtc *crtc)
11324{
11325 struct drm_i915_private *dev_priv = dev->dev_private;
11326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11327 struct intel_unpin_work *work = intel_crtc->unpin_work;
11328 u32 addr;
11329
11330 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11331 return true;
11332
908565c2
CW
11333 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11334 return false;
11335
d6bbafa1
CW
11336 if (!work->enable_stall_check)
11337 return false;
11338
11339 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11340 if (work->flip_queued_req &&
11341 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11342 return false;
11343
1e3feefd 11344 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11345 }
11346
1e3feefd 11347 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11348 return false;
11349
11350 /* Potential stall - if we see that the flip has happened,
11351 * assume a missed interrupt. */
11352 if (INTEL_INFO(dev)->gen >= 4)
11353 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11354 else
11355 addr = I915_READ(DSPADDR(intel_crtc->plane));
11356
11357 /* There is a potential issue here with a false positive after a flip
11358 * to the same address. We could address this by checking for a
11359 * non-incrementing frame counter.
11360 */
11361 return addr == work->gtt_offset;
11362}
11363
11364void intel_check_page_flip(struct drm_device *dev, int pipe)
11365{
11366 struct drm_i915_private *dev_priv = dev->dev_private;
11367 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11369 struct intel_unpin_work *work;
f326038a 11370
6c51d46f 11371 WARN_ON(!in_interrupt());
d6bbafa1
CW
11372
11373 if (crtc == NULL)
11374 return;
11375
f326038a 11376 spin_lock(&dev->event_lock);
6ad790c0
CW
11377 work = intel_crtc->unpin_work;
11378 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11379 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11380 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11381 page_flip_completed(intel_crtc);
6ad790c0 11382 work = NULL;
d6bbafa1 11383 }
6ad790c0
CW
11384 if (work != NULL &&
11385 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11386 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11387 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11388}
11389
6b95a207
KH
11390static int intel_crtc_page_flip(struct drm_crtc *crtc,
11391 struct drm_framebuffer *fb,
ed8d1975
KP
11392 struct drm_pending_vblank_event *event,
11393 uint32_t page_flip_flags)
6b95a207
KH
11394{
11395 struct drm_device *dev = crtc->dev;
11396 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11397 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11398 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11400 struct drm_plane *primary = crtc->primary;
a071fa00 11401 enum pipe pipe = intel_crtc->pipe;
6b95a207 11402 struct intel_unpin_work *work;
a4872ba6 11403 struct intel_engine_cs *ring;
cf5d8a46 11404 bool mmio_flip;
91af127f 11405 struct drm_i915_gem_request *request = NULL;
52e68630 11406 int ret;
6b95a207 11407
2ff8fde1
MR
11408 /*
11409 * drm_mode_page_flip_ioctl() should already catch this, but double
11410 * check to be safe. In the future we may enable pageflipping from
11411 * a disabled primary plane.
11412 */
11413 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11414 return -EBUSY;
11415
e6a595d2 11416 /* Can't change pixel format via MI display flips. */
f4510a27 11417 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11418 return -EINVAL;
11419
11420 /*
11421 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11422 * Note that pitch changes could also affect these register.
11423 */
11424 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11425 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11426 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11427 return -EINVAL;
11428
f900db47
CW
11429 if (i915_terminally_wedged(&dev_priv->gpu_error))
11430 goto out_hang;
11431
b14c5679 11432 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11433 if (work == NULL)
11434 return -ENOMEM;
11435
6b95a207 11436 work->event = event;
b4a98e57 11437 work->crtc = crtc;
ab8d6675 11438 work->old_fb = old_fb;
6b95a207
KH
11439 INIT_WORK(&work->work, intel_unpin_work_fn);
11440
87b6b101 11441 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11442 if (ret)
11443 goto free_work;
11444
6b95a207 11445 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11446 spin_lock_irq(&dev->event_lock);
6b95a207 11447 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11448 /* Before declaring the flip queue wedged, check if
11449 * the hardware completed the operation behind our backs.
11450 */
11451 if (__intel_pageflip_stall_check(dev, crtc)) {
11452 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11453 page_flip_completed(intel_crtc);
11454 } else {
11455 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11456 spin_unlock_irq(&dev->event_lock);
468f0b44 11457
d6bbafa1
CW
11458 drm_crtc_vblank_put(crtc);
11459 kfree(work);
11460 return -EBUSY;
11461 }
6b95a207
KH
11462 }
11463 intel_crtc->unpin_work = work;
5e2d7afc 11464 spin_unlock_irq(&dev->event_lock);
6b95a207 11465
b4a98e57
CW
11466 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11467 flush_workqueue(dev_priv->wq);
11468
75dfca80 11469 /* Reference the objects for the scheduled work. */
ab8d6675 11470 drm_framebuffer_reference(work->old_fb);
05394f39 11471 drm_gem_object_reference(&obj->base);
6b95a207 11472
f4510a27 11473 crtc->primary->fb = fb;
afd65eb4 11474 update_state_fb(crtc->primary);
1ed1f968 11475
e1f99ce6 11476 work->pending_flip_obj = obj;
e1f99ce6 11477
89ed88ba
CW
11478 ret = i915_mutex_lock_interruptible(dev);
11479 if (ret)
11480 goto cleanup;
11481
b4a98e57 11482 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11483 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11484
75f7f3ec 11485 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
fd8f507c 11486 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
75f7f3ec 11487
4fa62c89
VS
11488 if (IS_VALLEYVIEW(dev)) {
11489 ring = &dev_priv->ring[BCS];
ab8d6675 11490 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11491 /* vlv: DISPLAY_FLIP fails to change tiling */
11492 ring = NULL;
48bf5b2d 11493 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11494 ring = &dev_priv->ring[BCS];
4fa62c89 11495 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11496 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11497 if (ring == NULL || ring->id != RCS)
11498 ring = &dev_priv->ring[BCS];
11499 } else {
11500 ring = &dev_priv->ring[RCS];
11501 }
11502
cf5d8a46
CW
11503 mmio_flip = use_mmio_flip(ring, obj);
11504
11505 /* When using CS flips, we want to emit semaphores between rings.
11506 * However, when using mmio flips we will create a task to do the
11507 * synchronisation, so all we want here is to pin the framebuffer
11508 * into the display plane and skip any waits.
11509 */
7580d774
ML
11510 if (!mmio_flip) {
11511 ret = i915_gem_object_sync(obj, ring, &request);
11512 if (ret)
11513 goto cleanup_pending;
11514 }
11515
82bc3b2d 11516 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
7580d774 11517 crtc->primary->state);
8c9f3aaf
JB
11518 if (ret)
11519 goto cleanup_pending;
6b95a207 11520
dedf278c
TU
11521 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11522 obj, 0);
11523 work->gtt_offset += intel_crtc->dspaddr_offset;
4fa62c89 11524
cf5d8a46 11525 if (mmio_flip) {
86efe24a 11526 ret = intel_queue_mmio_flip(dev, crtc, obj);
d6bbafa1
CW
11527 if (ret)
11528 goto cleanup_unpin;
11529
f06cc1b9
JH
11530 i915_gem_request_assign(&work->flip_queued_req,
11531 obj->last_write_req);
d6bbafa1 11532 } else {
6258fbe2
JH
11533 if (!request) {
11534 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11535 if (ret)
11536 goto cleanup_unpin;
11537 }
11538
11539 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11540 page_flip_flags);
11541 if (ret)
11542 goto cleanup_unpin;
11543
6258fbe2 11544 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11545 }
11546
91af127f 11547 if (request)
75289874 11548 i915_add_request_no_flush(request);
91af127f 11549
1e3feefd 11550 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11551 work->enable_stall_check = true;
4fa62c89 11552
ab8d6675 11553 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11554 to_intel_plane(primary)->frontbuffer_bit);
c80ac854 11555 mutex_unlock(&dev->struct_mutex);
a071fa00 11556
4e1e26f1 11557 intel_fbc_disable_crtc(intel_crtc);
a9ff8714
VS
11558 intel_frontbuffer_flip_prepare(dev,
11559 to_intel_plane(primary)->frontbuffer_bit);
6b95a207 11560
e5510fac
JB
11561 trace_i915_flip_request(intel_crtc->plane, obj);
11562
6b95a207 11563 return 0;
96b099fd 11564
4fa62c89 11565cleanup_unpin:
82bc3b2d 11566 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11567cleanup_pending:
91af127f
JH
11568 if (request)
11569 i915_gem_request_cancel(request);
b4a98e57 11570 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11571 mutex_unlock(&dev->struct_mutex);
11572cleanup:
f4510a27 11573 crtc->primary->fb = old_fb;
afd65eb4 11574 update_state_fb(crtc->primary);
89ed88ba
CW
11575
11576 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11577 drm_framebuffer_unreference(work->old_fb);
96b099fd 11578
5e2d7afc 11579 spin_lock_irq(&dev->event_lock);
96b099fd 11580 intel_crtc->unpin_work = NULL;
5e2d7afc 11581 spin_unlock_irq(&dev->event_lock);
96b099fd 11582
87b6b101 11583 drm_crtc_vblank_put(crtc);
7317c75e 11584free_work:
96b099fd
CW
11585 kfree(work);
11586
f900db47 11587 if (ret == -EIO) {
02e0efb5
ML
11588 struct drm_atomic_state *state;
11589 struct drm_plane_state *plane_state;
11590
f900db47 11591out_hang:
02e0efb5
ML
11592 state = drm_atomic_state_alloc(dev);
11593 if (!state)
11594 return -ENOMEM;
11595 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11596
11597retry:
11598 plane_state = drm_atomic_get_plane_state(state, primary);
11599 ret = PTR_ERR_OR_ZERO(plane_state);
11600 if (!ret) {
11601 drm_atomic_set_fb_for_plane(plane_state, fb);
11602
11603 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11604 if (!ret)
11605 ret = drm_atomic_commit(state);
11606 }
11607
11608 if (ret == -EDEADLK) {
11609 drm_modeset_backoff(state->acquire_ctx);
11610 drm_atomic_state_clear(state);
11611 goto retry;
11612 }
11613
11614 if (ret)
11615 drm_atomic_state_free(state);
11616
f0d3dad3 11617 if (ret == 0 && event) {
5e2d7afc 11618 spin_lock_irq(&dev->event_lock);
a071fa00 11619 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11620 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11621 }
f900db47 11622 }
96b099fd 11623 return ret;
6b95a207
KH
11624}
11625
da20eabd
ML
11626
11627/**
11628 * intel_wm_need_update - Check whether watermarks need updating
11629 * @plane: drm plane
11630 * @state: new plane state
11631 *
11632 * Check current plane state versus the new one to determine whether
11633 * watermarks need to be recalculated.
11634 *
11635 * Returns true or false.
11636 */
11637static bool intel_wm_need_update(struct drm_plane *plane,
11638 struct drm_plane_state *state)
11639{
d21fbe87
MR
11640 struct intel_plane_state *new = to_intel_plane_state(state);
11641 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11642
11643 /* Update watermarks on tiling or size changes. */
da20eabd
ML
11644 if (!plane->state->fb || !state->fb ||
11645 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
d21fbe87
MR
11646 plane->state->rotation != state->rotation ||
11647 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11648 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11649 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11650 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
2791a16c 11651 return true;
7809e5ae 11652
2791a16c 11653 return false;
7809e5ae
MR
11654}
11655
d21fbe87
MR
11656static bool needs_scaling(struct intel_plane_state *state)
11657{
11658 int src_w = drm_rect_width(&state->src) >> 16;
11659 int src_h = drm_rect_height(&state->src) >> 16;
11660 int dst_w = drm_rect_width(&state->dst);
11661 int dst_h = drm_rect_height(&state->dst);
11662
11663 return (src_w != dst_w || src_h != dst_h);
11664}
11665
da20eabd
ML
11666int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11667 struct drm_plane_state *plane_state)
11668{
11669 struct drm_crtc *crtc = crtc_state->crtc;
11670 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11671 struct drm_plane *plane = plane_state->plane;
11672 struct drm_device *dev = crtc->dev;
11673 struct drm_i915_private *dev_priv = dev->dev_private;
11674 struct intel_plane_state *old_plane_state =
11675 to_intel_plane_state(plane->state);
11676 int idx = intel_crtc->base.base.id, ret;
11677 int i = drm_plane_index(plane);
11678 bool mode_changed = needs_modeset(crtc_state);
11679 bool was_crtc_enabled = crtc->state->active;
11680 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
11681 bool turn_off, turn_on, visible, was_visible;
11682 struct drm_framebuffer *fb = plane_state->fb;
11683
11684 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11685 plane->type != DRM_PLANE_TYPE_CURSOR) {
11686 ret = skl_update_scaler_plane(
11687 to_intel_crtc_state(crtc_state),
11688 to_intel_plane_state(plane_state));
11689 if (ret)
11690 return ret;
11691 }
11692
da20eabd
ML
11693 was_visible = old_plane_state->visible;
11694 visible = to_intel_plane_state(plane_state)->visible;
11695
11696 if (!was_crtc_enabled && WARN_ON(was_visible))
11697 was_visible = false;
11698
11699 if (!is_crtc_enabled && WARN_ON(visible))
11700 visible = false;
11701
11702 if (!was_visible && !visible)
11703 return 0;
11704
11705 turn_off = was_visible && (!visible || mode_changed);
11706 turn_on = visible && (!was_visible || mode_changed);
11707
11708 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11709 plane->base.id, fb ? fb->base.id : -1);
11710
11711 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11712 plane->base.id, was_visible, visible,
11713 turn_off, turn_on, mode_changed);
11714
852eb00d 11715 if (turn_on) {
f015c551 11716 intel_crtc->atomic.update_wm_pre = true;
852eb00d
VS
11717 /* must disable cxsr around plane enable/disable */
11718 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11719 intel_crtc->atomic.disable_cxsr = true;
11720 /* to potentially re-enable cxsr */
11721 intel_crtc->atomic.wait_vblank = true;
11722 intel_crtc->atomic.update_wm_post = true;
11723 }
11724 } else if (turn_off) {
f015c551 11725 intel_crtc->atomic.update_wm_post = true;
852eb00d
VS
11726 /* must disable cxsr around plane enable/disable */
11727 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11728 if (is_crtc_enabled)
11729 intel_crtc->atomic.wait_vblank = true;
11730 intel_crtc->atomic.disable_cxsr = true;
11731 }
11732 } else if (intel_wm_need_update(plane, plane_state)) {
f015c551 11733 intel_crtc->atomic.update_wm_pre = true;
852eb00d 11734 }
da20eabd 11735
8be6ca85 11736 if (visible || was_visible)
a9ff8714
VS
11737 intel_crtc->atomic.fb_bits |=
11738 to_intel_plane(plane)->frontbuffer_bit;
11739
da20eabd
ML
11740 switch (plane->type) {
11741 case DRM_PLANE_TYPE_PRIMARY:
da20eabd
ML
11742 intel_crtc->atomic.pre_disable_primary = turn_off;
11743 intel_crtc->atomic.post_enable_primary = turn_on;
11744
066cf55b
RV
11745 if (turn_off) {
11746 /*
11747 * FIXME: Actually if we will still have any other
11748 * plane enabled on the pipe we could let IPS enabled
11749 * still, but for now lets consider that when we make
11750 * primary invisible by setting DSPCNTR to 0 on
11751 * update_primary_plane function IPS needs to be
11752 * disable.
11753 */
11754 intel_crtc->atomic.disable_ips = true;
11755
da20eabd 11756 intel_crtc->atomic.disable_fbc = true;
066cf55b 11757 }
da20eabd
ML
11758
11759 /*
11760 * FBC does not work on some platforms for rotated
11761 * planes, so disable it when rotation is not 0 and
11762 * update it when rotation is set back to 0.
11763 *
11764 * FIXME: This is redundant with the fbc update done in
11765 * the primary plane enable function except that that
11766 * one is done too late. We eventually need to unify
11767 * this.
11768 */
11769
11770 if (visible &&
11771 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11772 dev_priv->fbc.crtc == intel_crtc &&
11773 plane_state->rotation != BIT(DRM_ROTATE_0))
11774 intel_crtc->atomic.disable_fbc = true;
11775
11776 /*
11777 * BDW signals flip done immediately if the plane
11778 * is disabled, even if the plane enable is already
11779 * armed to occur at the next vblank :(
11780 */
11781 if (turn_on && IS_BROADWELL(dev))
11782 intel_crtc->atomic.wait_vblank = true;
11783
11784 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11785 break;
11786 case DRM_PLANE_TYPE_CURSOR:
da20eabd
ML
11787 break;
11788 case DRM_PLANE_TYPE_OVERLAY:
d21fbe87
MR
11789 /*
11790 * WaCxSRDisabledForSpriteScaling:ivb
11791 *
11792 * cstate->update_wm was already set above, so this flag will
11793 * take effect when we commit and program watermarks.
11794 */
11795 if (IS_IVYBRIDGE(dev) &&
11796 needs_scaling(to_intel_plane_state(plane_state)) &&
11797 !needs_scaling(old_plane_state)) {
11798 to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
11799 } else if (turn_off && !mode_changed) {
da20eabd
ML
11800 intel_crtc->atomic.wait_vblank = true;
11801 intel_crtc->atomic.update_sprite_watermarks |=
11802 1 << i;
11803 }
d21fbe87
MR
11804
11805 break;
da20eabd
ML
11806 }
11807 return 0;
11808}
11809
6d3a1ce7
ML
11810static bool encoders_cloneable(const struct intel_encoder *a,
11811 const struct intel_encoder *b)
11812{
11813 /* masks could be asymmetric, so check both ways */
11814 return a == b || (a->cloneable & (1 << b->type) &&
11815 b->cloneable & (1 << a->type));
11816}
11817
11818static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11819 struct intel_crtc *crtc,
11820 struct intel_encoder *encoder)
11821{
11822 struct intel_encoder *source_encoder;
11823 struct drm_connector *connector;
11824 struct drm_connector_state *connector_state;
11825 int i;
11826
11827 for_each_connector_in_state(state, connector, connector_state, i) {
11828 if (connector_state->crtc != &crtc->base)
11829 continue;
11830
11831 source_encoder =
11832 to_intel_encoder(connector_state->best_encoder);
11833 if (!encoders_cloneable(encoder, source_encoder))
11834 return false;
11835 }
11836
11837 return true;
11838}
11839
11840static bool check_encoder_cloning(struct drm_atomic_state *state,
11841 struct intel_crtc *crtc)
11842{
11843 struct intel_encoder *encoder;
11844 struct drm_connector *connector;
11845 struct drm_connector_state *connector_state;
11846 int i;
11847
11848 for_each_connector_in_state(state, connector, connector_state, i) {
11849 if (connector_state->crtc != &crtc->base)
11850 continue;
11851
11852 encoder = to_intel_encoder(connector_state->best_encoder);
11853 if (!check_single_encoder_cloning(state, crtc, encoder))
11854 return false;
11855 }
11856
11857 return true;
11858}
11859
11860static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11861 struct drm_crtc_state *crtc_state)
11862{
cf5a15be 11863 struct drm_device *dev = crtc->dev;
ad421372 11864 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11866 struct intel_crtc_state *pipe_config =
11867 to_intel_crtc_state(crtc_state);
6d3a1ce7 11868 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 11869 int ret;
6d3a1ce7
ML
11870 bool mode_changed = needs_modeset(crtc_state);
11871
11872 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11873 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11874 return -EINVAL;
11875 }
11876
852eb00d
VS
11877 if (mode_changed && !crtc_state->active)
11878 intel_crtc->atomic.update_wm_post = true;
eddfcbcd 11879
ad421372
ML
11880 if (mode_changed && crtc_state->enable &&
11881 dev_priv->display.crtc_compute_clock &&
11882 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11883 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11884 pipe_config);
11885 if (ret)
11886 return ret;
11887 }
11888
e435d6e5 11889 ret = 0;
86c8bbbe
MR
11890 if (dev_priv->display.compute_pipe_wm) {
11891 ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
11892 if (ret)
11893 return ret;
11894 }
11895
e435d6e5
ML
11896 if (INTEL_INFO(dev)->gen >= 9) {
11897 if (mode_changed)
11898 ret = skl_update_scaler_crtc(pipe_config);
11899
11900 if (!ret)
11901 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11902 pipe_config);
11903 }
11904
11905 return ret;
6d3a1ce7
ML
11906}
11907
65b38e0d 11908static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11909 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11910 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11911 .atomic_begin = intel_begin_crtc_commit,
11912 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11913 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11914};
11915
d29b2f9d
ACO
11916static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11917{
11918 struct intel_connector *connector;
11919
11920 for_each_intel_connector(dev, connector) {
11921 if (connector->base.encoder) {
11922 connector->base.state->best_encoder =
11923 connector->base.encoder;
11924 connector->base.state->crtc =
11925 connector->base.encoder->crtc;
11926 } else {
11927 connector->base.state->best_encoder = NULL;
11928 connector->base.state->crtc = NULL;
11929 }
11930 }
11931}
11932
050f7aeb 11933static void
eba905b2 11934connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11935 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11936{
11937 int bpp = pipe_config->pipe_bpp;
11938
11939 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11940 connector->base.base.id,
c23cc417 11941 connector->base.name);
050f7aeb
DV
11942
11943 /* Don't use an invalid EDID bpc value */
11944 if (connector->base.display_info.bpc &&
11945 connector->base.display_info.bpc * 3 < bpp) {
11946 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11947 bpp, connector->base.display_info.bpc*3);
11948 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11949 }
11950
11951 /* Clamp bpp to 8 on screens without EDID 1.4 */
11952 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11953 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11954 bpp);
11955 pipe_config->pipe_bpp = 24;
11956 }
11957}
11958
4e53c2e0 11959static int
050f7aeb 11960compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11961 struct intel_crtc_state *pipe_config)
4e53c2e0 11962{
050f7aeb 11963 struct drm_device *dev = crtc->base.dev;
1486017f 11964 struct drm_atomic_state *state;
da3ced29
ACO
11965 struct drm_connector *connector;
11966 struct drm_connector_state *connector_state;
1486017f 11967 int bpp, i;
4e53c2e0 11968
d328c9d7 11969 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11970 bpp = 10*3;
d328c9d7
DV
11971 else if (INTEL_INFO(dev)->gen >= 5)
11972 bpp = 12*3;
11973 else
11974 bpp = 8*3;
11975
4e53c2e0 11976
4e53c2e0
DV
11977 pipe_config->pipe_bpp = bpp;
11978
1486017f
ACO
11979 state = pipe_config->base.state;
11980
4e53c2e0 11981 /* Clamp display bpp to EDID value */
da3ced29
ACO
11982 for_each_connector_in_state(state, connector, connector_state, i) {
11983 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11984 continue;
11985
da3ced29
ACO
11986 connected_sink_compute_bpp(to_intel_connector(connector),
11987 pipe_config);
4e53c2e0
DV
11988 }
11989
11990 return bpp;
11991}
11992
644db711
DV
11993static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11994{
11995 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11996 "type: 0x%x flags: 0x%x\n",
1342830c 11997 mode->crtc_clock,
644db711
DV
11998 mode->crtc_hdisplay, mode->crtc_hsync_start,
11999 mode->crtc_hsync_end, mode->crtc_htotal,
12000 mode->crtc_vdisplay, mode->crtc_vsync_start,
12001 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12002}
12003
c0b03411 12004static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12005 struct intel_crtc_state *pipe_config,
c0b03411
DV
12006 const char *context)
12007{
6a60cd87
CK
12008 struct drm_device *dev = crtc->base.dev;
12009 struct drm_plane *plane;
12010 struct intel_plane *intel_plane;
12011 struct intel_plane_state *state;
12012 struct drm_framebuffer *fb;
12013
12014 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12015 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
12016
12017 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12018 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12019 pipe_config->pipe_bpp, pipe_config->dither);
12020 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12021 pipe_config->has_pch_encoder,
12022 pipe_config->fdi_lanes,
12023 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12024 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12025 pipe_config->fdi_m_n.tu);
90a6b7b0 12026 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
eb14cb74 12027 pipe_config->has_dp_encoder,
90a6b7b0 12028 pipe_config->lane_count,
eb14cb74
VS
12029 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12030 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12031 pipe_config->dp_m_n.tu);
b95af8be 12032
90a6b7b0 12033 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
b95af8be 12034 pipe_config->has_dp_encoder,
90a6b7b0 12035 pipe_config->lane_count,
b95af8be
VK
12036 pipe_config->dp_m2_n2.gmch_m,
12037 pipe_config->dp_m2_n2.gmch_n,
12038 pipe_config->dp_m2_n2.link_m,
12039 pipe_config->dp_m2_n2.link_n,
12040 pipe_config->dp_m2_n2.tu);
12041
55072d19
DV
12042 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12043 pipe_config->has_audio,
12044 pipe_config->has_infoframe);
12045
c0b03411 12046 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12047 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12048 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12049 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12050 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12051 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12052 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12053 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12054 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12055 crtc->num_scalers,
12056 pipe_config->scaler_state.scaler_users,
12057 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12058 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12059 pipe_config->gmch_pfit.control,
12060 pipe_config->gmch_pfit.pgm_ratios,
12061 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12062 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12063 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12064 pipe_config->pch_pfit.size,
12065 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12066 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12067 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12068
415ff0f6 12069 if (IS_BROXTON(dev)) {
05712c15 12070 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12071 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12072 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6
TU
12073 pipe_config->ddi_pll_sel,
12074 pipe_config->dpll_hw_state.ebb0,
05712c15 12075 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12076 pipe_config->dpll_hw_state.pll0,
12077 pipe_config->dpll_hw_state.pll1,
12078 pipe_config->dpll_hw_state.pll2,
12079 pipe_config->dpll_hw_state.pll3,
12080 pipe_config->dpll_hw_state.pll6,
12081 pipe_config->dpll_hw_state.pll8,
05712c15 12082 pipe_config->dpll_hw_state.pll9,
c8453338 12083 pipe_config->dpll_hw_state.pll10,
415ff0f6 12084 pipe_config->dpll_hw_state.pcsdw12);
ef11bdb3 12085 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
415ff0f6
TU
12086 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12087 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12088 pipe_config->ddi_pll_sel,
12089 pipe_config->dpll_hw_state.ctrl1,
12090 pipe_config->dpll_hw_state.cfgcr1,
12091 pipe_config->dpll_hw_state.cfgcr2);
12092 } else if (HAS_DDI(dev)) {
12093 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12094 pipe_config->ddi_pll_sel,
12095 pipe_config->dpll_hw_state.wrpll);
12096 } else {
12097 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12098 "fp0: 0x%x, fp1: 0x%x\n",
12099 pipe_config->dpll_hw_state.dpll,
12100 pipe_config->dpll_hw_state.dpll_md,
12101 pipe_config->dpll_hw_state.fp0,
12102 pipe_config->dpll_hw_state.fp1);
12103 }
12104
6a60cd87
CK
12105 DRM_DEBUG_KMS("planes on this crtc\n");
12106 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12107 intel_plane = to_intel_plane(plane);
12108 if (intel_plane->pipe != crtc->pipe)
12109 continue;
12110
12111 state = to_intel_plane_state(plane->state);
12112 fb = state->base.fb;
12113 if (!fb) {
12114 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12115 "disabled, scaler_id = %d\n",
12116 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12117 plane->base.id, intel_plane->pipe,
12118 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12119 drm_plane_index(plane), state->scaler_id);
12120 continue;
12121 }
12122
12123 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12124 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12125 plane->base.id, intel_plane->pipe,
12126 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12127 drm_plane_index(plane));
12128 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12129 fb->base.id, fb->width, fb->height, fb->pixel_format);
12130 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12131 state->scaler_id,
12132 state->src.x1 >> 16, state->src.y1 >> 16,
12133 drm_rect_width(&state->src) >> 16,
12134 drm_rect_height(&state->src) >> 16,
12135 state->dst.x1, state->dst.y1,
12136 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12137 }
c0b03411
DV
12138}
12139
5448a00d 12140static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12141{
5448a00d
ACO
12142 struct drm_device *dev = state->dev;
12143 struct intel_encoder *encoder;
da3ced29 12144 struct drm_connector *connector;
5448a00d 12145 struct drm_connector_state *connector_state;
00f0b378 12146 unsigned int used_ports = 0;
5448a00d 12147 int i;
00f0b378
VS
12148
12149 /*
12150 * Walk the connector list instead of the encoder
12151 * list to detect the problem on ddi platforms
12152 * where there's just one encoder per digital port.
12153 */
da3ced29 12154 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 12155 if (!connector_state->best_encoder)
00f0b378
VS
12156 continue;
12157
5448a00d
ACO
12158 encoder = to_intel_encoder(connector_state->best_encoder);
12159
12160 WARN_ON(!connector_state->crtc);
00f0b378
VS
12161
12162 switch (encoder->type) {
12163 unsigned int port_mask;
12164 case INTEL_OUTPUT_UNKNOWN:
12165 if (WARN_ON(!HAS_DDI(dev)))
12166 break;
12167 case INTEL_OUTPUT_DISPLAYPORT:
12168 case INTEL_OUTPUT_HDMI:
12169 case INTEL_OUTPUT_EDP:
12170 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12171
12172 /* the same port mustn't appear more than once */
12173 if (used_ports & port_mask)
12174 return false;
12175
12176 used_ports |= port_mask;
12177 default:
12178 break;
12179 }
12180 }
12181
12182 return true;
12183}
12184
83a57153
ACO
12185static void
12186clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12187{
12188 struct drm_crtc_state tmp_state;
663a3640 12189 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12190 struct intel_dpll_hw_state dpll_hw_state;
12191 enum intel_dpll_id shared_dpll;
8504c74c 12192 uint32_t ddi_pll_sel;
c4e2d043 12193 bool force_thru;
83a57153 12194
7546a384
ACO
12195 /* FIXME: before the switch to atomic started, a new pipe_config was
12196 * kzalloc'd. Code that depends on any field being zero should be
12197 * fixed, so that the crtc_state can be safely duplicated. For now,
12198 * only fields that are know to not cause problems are preserved. */
12199
83a57153 12200 tmp_state = crtc_state->base;
663a3640 12201 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12202 shared_dpll = crtc_state->shared_dpll;
12203 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12204 ddi_pll_sel = crtc_state->ddi_pll_sel;
c4e2d043 12205 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12206
83a57153 12207 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12208
83a57153 12209 crtc_state->base = tmp_state;
663a3640 12210 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12211 crtc_state->shared_dpll = shared_dpll;
12212 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12213 crtc_state->ddi_pll_sel = ddi_pll_sel;
c4e2d043 12214 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12215}
12216
548ee15b 12217static int
b8cecdf5 12218intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12219 struct intel_crtc_state *pipe_config)
ee7b9f93 12220{
b359283a 12221 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12222 struct intel_encoder *encoder;
da3ced29 12223 struct drm_connector *connector;
0b901879 12224 struct drm_connector_state *connector_state;
d328c9d7 12225 int base_bpp, ret = -EINVAL;
0b901879 12226 int i;
e29c22c0 12227 bool retry = true;
ee7b9f93 12228
83a57153 12229 clear_intel_crtc_state(pipe_config);
7758a113 12230
e143a21c
DV
12231 pipe_config->cpu_transcoder =
12232 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12233
2960bc9c
ID
12234 /*
12235 * Sanitize sync polarity flags based on requested ones. If neither
12236 * positive or negative polarity is requested, treat this as meaning
12237 * negative polarity.
12238 */
2d112de7 12239 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12240 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12241 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12242
2d112de7 12243 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12244 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12245 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12246
d328c9d7
DV
12247 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12248 pipe_config);
12249 if (base_bpp < 0)
4e53c2e0
DV
12250 goto fail;
12251
e41a56be
VS
12252 /*
12253 * Determine the real pipe dimensions. Note that stereo modes can
12254 * increase the actual pipe size due to the frame doubling and
12255 * insertion of additional space for blanks between the frame. This
12256 * is stored in the crtc timings. We use the requested mode to do this
12257 * computation to clearly distinguish it from the adjusted mode, which
12258 * can be changed by the connectors in the below retry loop.
12259 */
2d112de7 12260 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12261 &pipe_config->pipe_src_w,
12262 &pipe_config->pipe_src_h);
e41a56be 12263
e29c22c0 12264encoder_retry:
ef1b460d 12265 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12266 pipe_config->port_clock = 0;
ef1b460d 12267 pipe_config->pixel_multiplier = 1;
ff9a6750 12268
135c81b8 12269 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12270 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12271 CRTC_STEREO_DOUBLE);
135c81b8 12272
7758a113
DV
12273 /* Pass our mode to the connectors and the CRTC to give them a chance to
12274 * adjust it according to limitations or connector properties, and also
12275 * a chance to reject the mode entirely.
47f1c6c9 12276 */
da3ced29 12277 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12278 if (connector_state->crtc != crtc)
7758a113 12279 continue;
7ae89233 12280
0b901879
ACO
12281 encoder = to_intel_encoder(connector_state->best_encoder);
12282
efea6e8e
DV
12283 if (!(encoder->compute_config(encoder, pipe_config))) {
12284 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12285 goto fail;
12286 }
ee7b9f93 12287 }
47f1c6c9 12288
ff9a6750
DV
12289 /* Set default port clock if not overwritten by the encoder. Needs to be
12290 * done afterwards in case the encoder adjusts the mode. */
12291 if (!pipe_config->port_clock)
2d112de7 12292 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12293 * pipe_config->pixel_multiplier;
ff9a6750 12294
a43f6e0f 12295 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12296 if (ret < 0) {
7758a113
DV
12297 DRM_DEBUG_KMS("CRTC fixup failed\n");
12298 goto fail;
ee7b9f93 12299 }
e29c22c0
DV
12300
12301 if (ret == RETRY) {
12302 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12303 ret = -EINVAL;
12304 goto fail;
12305 }
12306
12307 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12308 retry = false;
12309 goto encoder_retry;
12310 }
12311
e8fa4270
DV
12312 /* Dithering seems to not pass-through bits correctly when it should, so
12313 * only enable it on 6bpc panels. */
12314 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 12315 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12316 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12317
7758a113 12318fail:
548ee15b 12319 return ret;
ee7b9f93 12320}
47f1c6c9 12321
ea9d758d 12322static void
4740b0f2 12323intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 12324{
0a9ab303
ACO
12325 struct drm_crtc *crtc;
12326 struct drm_crtc_state *crtc_state;
8a75d157 12327 int i;
ea9d758d 12328
7668851f 12329 /* Double check state. */
8a75d157 12330 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 12331 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12332
12333 /* Update hwmode for vblank functions */
12334 if (crtc->state->active)
12335 crtc->hwmode = crtc->state->adjusted_mode;
12336 else
12337 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
12338
12339 /*
12340 * Update legacy state to satisfy fbc code. This can
12341 * be removed when fbc uses the atomic state.
12342 */
12343 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12344 struct drm_plane_state *plane_state = crtc->primary->state;
12345
12346 crtc->primary->fb = plane_state->fb;
12347 crtc->x = plane_state->src_x >> 16;
12348 crtc->y = plane_state->src_y >> 16;
12349 }
ea9d758d 12350 }
ea9d758d
DV
12351}
12352
3bd26263 12353static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12354{
3bd26263 12355 int diff;
f1f644dc
JB
12356
12357 if (clock1 == clock2)
12358 return true;
12359
12360 if (!clock1 || !clock2)
12361 return false;
12362
12363 diff = abs(clock1 - clock2);
12364
12365 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12366 return true;
12367
12368 return false;
12369}
12370
25c5b266
DV
12371#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12372 list_for_each_entry((intel_crtc), \
12373 &(dev)->mode_config.crtc_list, \
12374 base.head) \
0973f18f 12375 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12376
cfb23ed6
ML
12377static bool
12378intel_compare_m_n(unsigned int m, unsigned int n,
12379 unsigned int m2, unsigned int n2,
12380 bool exact)
12381{
12382 if (m == m2 && n == n2)
12383 return true;
12384
12385 if (exact || !m || !n || !m2 || !n2)
12386 return false;
12387
12388 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12389
12390 if (m > m2) {
12391 while (m > m2) {
12392 m2 <<= 1;
12393 n2 <<= 1;
12394 }
12395 } else if (m < m2) {
12396 while (m < m2) {
12397 m <<= 1;
12398 n <<= 1;
12399 }
12400 }
12401
12402 return m == m2 && n == n2;
12403}
12404
12405static bool
12406intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12407 struct intel_link_m_n *m2_n2,
12408 bool adjust)
12409{
12410 if (m_n->tu == m2_n2->tu &&
12411 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12412 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12413 intel_compare_m_n(m_n->link_m, m_n->link_n,
12414 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12415 if (adjust)
12416 *m2_n2 = *m_n;
12417
12418 return true;
12419 }
12420
12421 return false;
12422}
12423
0e8ffe1b 12424static bool
2fa2fe9a 12425intel_pipe_config_compare(struct drm_device *dev,
5cec258b 12426 struct intel_crtc_state *current_config,
cfb23ed6
ML
12427 struct intel_crtc_state *pipe_config,
12428 bool adjust)
0e8ffe1b 12429{
cfb23ed6
ML
12430 bool ret = true;
12431
12432#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12433 do { \
12434 if (!adjust) \
12435 DRM_ERROR(fmt, ##__VA_ARGS__); \
12436 else \
12437 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12438 } while (0)
12439
66e985c0
DV
12440#define PIPE_CONF_CHECK_X(name) \
12441 if (current_config->name != pipe_config->name) { \
cfb23ed6 12442 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
DV
12443 "(expected 0x%08x, found 0x%08x)\n", \
12444 current_config->name, \
12445 pipe_config->name); \
cfb23ed6 12446 ret = false; \
66e985c0
DV
12447 }
12448
08a24034
DV
12449#define PIPE_CONF_CHECK_I(name) \
12450 if (current_config->name != pipe_config->name) { \
cfb23ed6 12451 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
DV
12452 "(expected %i, found %i)\n", \
12453 current_config->name, \
12454 pipe_config->name); \
cfb23ed6
ML
12455 ret = false; \
12456 }
12457
12458#define PIPE_CONF_CHECK_M_N(name) \
12459 if (!intel_compare_link_m_n(&current_config->name, \
12460 &pipe_config->name,\
12461 adjust)) { \
12462 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12463 "(expected tu %i gmch %i/%i link %i/%i, " \
12464 "found tu %i, gmch %i/%i link %i/%i)\n", \
12465 current_config->name.tu, \
12466 current_config->name.gmch_m, \
12467 current_config->name.gmch_n, \
12468 current_config->name.link_m, \
12469 current_config->name.link_n, \
12470 pipe_config->name.tu, \
12471 pipe_config->name.gmch_m, \
12472 pipe_config->name.gmch_n, \
12473 pipe_config->name.link_m, \
12474 pipe_config->name.link_n); \
12475 ret = false; \
12476 }
12477
12478#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12479 if (!intel_compare_link_m_n(&current_config->name, \
12480 &pipe_config->name, adjust) && \
12481 !intel_compare_link_m_n(&current_config->alt_name, \
12482 &pipe_config->name, adjust)) { \
12483 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12484 "(expected tu %i gmch %i/%i link %i/%i, " \
12485 "or tu %i gmch %i/%i link %i/%i, " \
12486 "found tu %i, gmch %i/%i link %i/%i)\n", \
12487 current_config->name.tu, \
12488 current_config->name.gmch_m, \
12489 current_config->name.gmch_n, \
12490 current_config->name.link_m, \
12491 current_config->name.link_n, \
12492 current_config->alt_name.tu, \
12493 current_config->alt_name.gmch_m, \
12494 current_config->alt_name.gmch_n, \
12495 current_config->alt_name.link_m, \
12496 current_config->alt_name.link_n, \
12497 pipe_config->name.tu, \
12498 pipe_config->name.gmch_m, \
12499 pipe_config->name.gmch_n, \
12500 pipe_config->name.link_m, \
12501 pipe_config->name.link_n); \
12502 ret = false; \
88adfff1
DV
12503 }
12504
b95af8be
VK
12505/* This is required for BDW+ where there is only one set of registers for
12506 * switching between high and low RR.
12507 * This macro can be used whenever a comparison has to be made between one
12508 * hw state and multiple sw state variables.
12509 */
12510#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12511 if ((current_config->name != pipe_config->name) && \
12512 (current_config->alt_name != pipe_config->name)) { \
cfb23ed6 12513 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
b95af8be
VK
12514 "(expected %i or %i, found %i)\n", \
12515 current_config->name, \
12516 current_config->alt_name, \
12517 pipe_config->name); \
cfb23ed6 12518 ret = false; \
b95af8be
VK
12519 }
12520
1bd1bd80
DV
12521#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12522 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 12523 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12524 "(expected %i, found %i)\n", \
12525 current_config->name & (mask), \
12526 pipe_config->name & (mask)); \
cfb23ed6 12527 ret = false; \
1bd1bd80
DV
12528 }
12529
5e550656
VS
12530#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12531 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 12532 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
12533 "(expected %i, found %i)\n", \
12534 current_config->name, \
12535 pipe_config->name); \
cfb23ed6 12536 ret = false; \
5e550656
VS
12537 }
12538
bb760063
DV
12539#define PIPE_CONF_QUIRK(quirk) \
12540 ((current_config->quirks | pipe_config->quirks) & (quirk))
12541
eccb140b
DV
12542 PIPE_CONF_CHECK_I(cpu_transcoder);
12543
08a24034
DV
12544 PIPE_CONF_CHECK_I(has_pch_encoder);
12545 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 12546 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 12547
eb14cb74 12548 PIPE_CONF_CHECK_I(has_dp_encoder);
90a6b7b0 12549 PIPE_CONF_CHECK_I(lane_count);
b95af8be
VK
12550
12551 if (INTEL_INFO(dev)->gen < 8) {
cfb23ed6
ML
12552 PIPE_CONF_CHECK_M_N(dp_m_n);
12553
12554 PIPE_CONF_CHECK_I(has_drrs);
12555 if (current_config->has_drrs)
12556 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12557 } else
12558 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 12559
2d112de7
ACO
12560 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12561 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12565 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12566
2d112de7
ACO
12567 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12568 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12569 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12570 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12571 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12572 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12573
c93f54cf 12574 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12575 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12576 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12577 IS_VALLEYVIEW(dev))
12578 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12579 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12580
9ed109a7
DV
12581 PIPE_CONF_CHECK_I(has_audio);
12582
2d112de7 12583 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12584 DRM_MODE_FLAG_INTERLACE);
12585
bb760063 12586 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12587 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12588 DRM_MODE_FLAG_PHSYNC);
2d112de7 12589 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12590 DRM_MODE_FLAG_NHSYNC);
2d112de7 12591 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12592 DRM_MODE_FLAG_PVSYNC);
2d112de7 12593 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12594 DRM_MODE_FLAG_NVSYNC);
12595 }
045ac3b5 12596
333b8ca8 12597 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a
DV
12598 /* pfit ratios are autocomputed by the hw on gen4+ */
12599 if (INTEL_INFO(dev)->gen < 4)
12600 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
333b8ca8 12601 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 12602
bfd16b2a
ML
12603 if (!adjust) {
12604 PIPE_CONF_CHECK_I(pipe_src_w);
12605 PIPE_CONF_CHECK_I(pipe_src_h);
12606
12607 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12608 if (current_config->pch_pfit.enabled) {
12609 PIPE_CONF_CHECK_X(pch_pfit.pos);
12610 PIPE_CONF_CHECK_X(pch_pfit.size);
12611 }
2fa2fe9a 12612
7aefe2b5
ML
12613 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12614 }
a1b2278e 12615
e59150dc
JB
12616 /* BDW+ don't expose a synchronous way to read the state */
12617 if (IS_HASWELL(dev))
12618 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12619
282740f7
VS
12620 PIPE_CONF_CHECK_I(double_wide);
12621
26804afd
DV
12622 PIPE_CONF_CHECK_X(ddi_pll_sel);
12623
c0d43d62 12624 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12625 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12626 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12627 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12628 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12629 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12630 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12631 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12632 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12633
42571aef
VS
12634 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12635 PIPE_CONF_CHECK_I(pipe_bpp);
12636
2d112de7 12637 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12638 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12639
66e985c0 12640#undef PIPE_CONF_CHECK_X
08a24034 12641#undef PIPE_CONF_CHECK_I
b95af8be 12642#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12643#undef PIPE_CONF_CHECK_FLAGS
5e550656 12644#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12645#undef PIPE_CONF_QUIRK
cfb23ed6 12646#undef INTEL_ERR_OR_DBG_KMS
88adfff1 12647
cfb23ed6 12648 return ret;
0e8ffe1b
DV
12649}
12650
08db6652
DL
12651static void check_wm_state(struct drm_device *dev)
12652{
12653 struct drm_i915_private *dev_priv = dev->dev_private;
12654 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12655 struct intel_crtc *intel_crtc;
12656 int plane;
12657
12658 if (INTEL_INFO(dev)->gen < 9)
12659 return;
12660
12661 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12662 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12663
12664 for_each_intel_crtc(dev, intel_crtc) {
12665 struct skl_ddb_entry *hw_entry, *sw_entry;
12666 const enum pipe pipe = intel_crtc->pipe;
12667
12668 if (!intel_crtc->active)
12669 continue;
12670
12671 /* planes */
dd740780 12672 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12673 hw_entry = &hw_ddb.plane[pipe][plane];
12674 sw_entry = &sw_ddb->plane[pipe][plane];
12675
12676 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12677 continue;
12678
12679 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12680 "(expected (%u,%u), found (%u,%u))\n",
12681 pipe_name(pipe), plane + 1,
12682 sw_entry->start, sw_entry->end,
12683 hw_entry->start, hw_entry->end);
12684 }
12685
12686 /* cursor */
4969d33e
MR
12687 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12688 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
08db6652
DL
12689
12690 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12691 continue;
12692
12693 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12694 "(expected (%u,%u), found (%u,%u))\n",
12695 pipe_name(pipe),
12696 sw_entry->start, sw_entry->end,
12697 hw_entry->start, hw_entry->end);
12698 }
12699}
12700
91d1b4bd 12701static void
35dd3c64
ML
12702check_connector_state(struct drm_device *dev,
12703 struct drm_atomic_state *old_state)
8af6cf88 12704{
35dd3c64
ML
12705 struct drm_connector_state *old_conn_state;
12706 struct drm_connector *connector;
12707 int i;
8af6cf88 12708
35dd3c64
ML
12709 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12710 struct drm_encoder *encoder = connector->encoder;
12711 struct drm_connector_state *state = connector->state;
ad3c558f 12712
8af6cf88
DV
12713 /* This also checks the encoder/connector hw state with the
12714 * ->get_hw_state callbacks. */
35dd3c64 12715 intel_connector_check_state(to_intel_connector(connector));
8af6cf88 12716
ad3c558f 12717 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 12718 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 12719 }
91d1b4bd
DV
12720}
12721
12722static void
12723check_encoder_state(struct drm_device *dev)
12724{
12725 struct intel_encoder *encoder;
12726 struct intel_connector *connector;
8af6cf88 12727
b2784e15 12728 for_each_intel_encoder(dev, encoder) {
8af6cf88 12729 bool enabled = false;
4d20cd86 12730 enum pipe pipe;
8af6cf88
DV
12731
12732 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12733 encoder->base.base.id,
8e329a03 12734 encoder->base.name);
8af6cf88 12735
3a3371ff 12736 for_each_intel_connector(dev, connector) {
4d20cd86 12737 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
DV
12738 continue;
12739 enabled = true;
ad3c558f
ML
12740
12741 I915_STATE_WARN(connector->base.state->crtc !=
12742 encoder->base.crtc,
12743 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 12744 }
0e32b39c 12745
e2c719b7 12746 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12747 "encoder's enabled state mismatch "
12748 "(expected %i, found %i)\n",
12749 !!encoder->base.crtc, enabled);
7c60d198
ML
12750
12751 if (!encoder->base.crtc) {
4d20cd86 12752 bool active;
7c60d198 12753
4d20cd86
ML
12754 active = encoder->get_hw_state(encoder, &pipe);
12755 I915_STATE_WARN(active,
12756 "encoder detached but still enabled on pipe %c.\n",
12757 pipe_name(pipe));
7c60d198 12758 }
8af6cf88 12759 }
91d1b4bd
DV
12760}
12761
12762static void
4d20cd86 12763check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
91d1b4bd 12764{
fbee40df 12765 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd 12766 struct intel_encoder *encoder;
4d20cd86
ML
12767 struct drm_crtc_state *old_crtc_state;
12768 struct drm_crtc *crtc;
12769 int i;
8af6cf88 12770
4d20cd86
ML
12771 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12773 struct intel_crtc_state *pipe_config, *sw_config;
7b89b8de 12774 bool active;
8af6cf88 12775
bfd16b2a
ML
12776 if (!needs_modeset(crtc->state) &&
12777 !to_intel_crtc_state(crtc->state)->update_pipe)
4d20cd86 12778 continue;
045ac3b5 12779
4d20cd86
ML
12780 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12781 pipe_config = to_intel_crtc_state(old_crtc_state);
12782 memset(pipe_config, 0, sizeof(*pipe_config));
12783 pipe_config->base.crtc = crtc;
12784 pipe_config->base.state = old_state;
8af6cf88 12785
4d20cd86
ML
12786 DRM_DEBUG_KMS("[CRTC:%d]\n",
12787 crtc->base.id);
8af6cf88 12788
4d20cd86
ML
12789 active = dev_priv->display.get_pipe_config(intel_crtc,
12790 pipe_config);
d62cf62a 12791
b6b5d049 12792 /* hw state is inconsistent with the pipe quirk */
4d20cd86
ML
12793 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12794 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12795 active = crtc->state->active;
6c49f241 12796
4d20cd86 12797 I915_STATE_WARN(crtc->state->active != active,
0e8ffe1b 12798 "crtc active state doesn't match with hw state "
4d20cd86 12799 "(expected %i, found %i)\n", crtc->state->active, active);
0e8ffe1b 12800
4d20cd86 12801 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
53d9f4e9 12802 "transitional active state does not match atomic hw state "
4d20cd86
ML
12803 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12804
12805 for_each_encoder_on_crtc(dev, crtc, encoder) {
12806 enum pipe pipe;
12807
12808 active = encoder->get_hw_state(encoder, &pipe);
12809 I915_STATE_WARN(active != crtc->state->active,
12810 "[ENCODER:%i] active %i with crtc active %i\n",
12811 encoder->base.base.id, active, crtc->state->active);
12812
12813 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12814 "Encoder connected to wrong pipe %c\n",
12815 pipe_name(pipe));
12816
12817 if (active)
12818 encoder->get_config(encoder, pipe_config);
12819 }
53d9f4e9 12820
4d20cd86 12821 if (!crtc->state->active)
cfb23ed6
ML
12822 continue;
12823
4d20cd86
ML
12824 sw_config = to_intel_crtc_state(crtc->state);
12825 if (!intel_pipe_config_compare(dev, sw_config,
12826 pipe_config, false)) {
e2c719b7 12827 I915_STATE_WARN(1, "pipe state doesn't match!\n");
4d20cd86 12828 intel_dump_pipe_config(intel_crtc, pipe_config,
c0b03411 12829 "[hw state]");
4d20cd86 12830 intel_dump_pipe_config(intel_crtc, sw_config,
c0b03411
DV
12831 "[sw state]");
12832 }
8af6cf88
DV
12833 }
12834}
12835
91d1b4bd
DV
12836static void
12837check_shared_dpll_state(struct drm_device *dev)
12838{
fbee40df 12839 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12840 struct intel_crtc *crtc;
12841 struct intel_dpll_hw_state dpll_hw_state;
12842 int i;
5358901f
DV
12843
12844 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12845 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12846 int enabled_crtcs = 0, active_crtcs = 0;
12847 bool active;
12848
12849 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12850
12851 DRM_DEBUG_KMS("%s\n", pll->name);
12852
12853 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12854
e2c719b7 12855 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12856 "more active pll users than references: %i vs %i\n",
3e369b76 12857 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12858 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12859 "pll in active use but not on in sw tracking\n");
e2c719b7 12860 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12861 "pll in on but not on in use in sw tracking\n");
e2c719b7 12862 I915_STATE_WARN(pll->on != active,
5358901f
DV
12863 "pll on state mismatch (expected %i, found %i)\n",
12864 pll->on, active);
12865
d3fcc808 12866 for_each_intel_crtc(dev, crtc) {
83d65738 12867 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12868 enabled_crtcs++;
12869 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12870 active_crtcs++;
12871 }
e2c719b7 12872 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12873 "pll active crtcs mismatch (expected %i, found %i)\n",
12874 pll->active, active_crtcs);
e2c719b7 12875 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12876 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12877 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12878
e2c719b7 12879 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12880 sizeof(dpll_hw_state)),
12881 "pll hw state mismatch\n");
5358901f 12882 }
8af6cf88
DV
12883}
12884
ee165b1a
ML
12885static void
12886intel_modeset_check_state(struct drm_device *dev,
12887 struct drm_atomic_state *old_state)
91d1b4bd 12888{
08db6652 12889 check_wm_state(dev);
35dd3c64 12890 check_connector_state(dev, old_state);
91d1b4bd 12891 check_encoder_state(dev);
4d20cd86 12892 check_crtc_state(dev, old_state);
91d1b4bd
DV
12893 check_shared_dpll_state(dev);
12894}
12895
5cec258b 12896void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12897 int dotclock)
12898{
12899 /*
12900 * FDI already provided one idea for the dotclock.
12901 * Yell if the encoder disagrees.
12902 */
2d112de7 12903 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12904 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12905 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12906}
12907
80715b2f
VS
12908static void update_scanline_offset(struct intel_crtc *crtc)
12909{
12910 struct drm_device *dev = crtc->base.dev;
12911
12912 /*
12913 * The scanline counter increments at the leading edge of hsync.
12914 *
12915 * On most platforms it starts counting from vtotal-1 on the
12916 * first active line. That means the scanline counter value is
12917 * always one less than what we would expect. Ie. just after
12918 * start of vblank, which also occurs at start of hsync (on the
12919 * last active line), the scanline counter will read vblank_start-1.
12920 *
12921 * On gen2 the scanline counter starts counting from 1 instead
12922 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12923 * to keep the value positive), instead of adding one.
12924 *
12925 * On HSW+ the behaviour of the scanline counter depends on the output
12926 * type. For DP ports it behaves like most other platforms, but on HDMI
12927 * there's an extra 1 line difference. So we need to add two instead of
12928 * one to the value.
12929 */
12930 if (IS_GEN2(dev)) {
124abe07 12931 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12932 int vtotal;
12933
124abe07
VS
12934 vtotal = adjusted_mode->crtc_vtotal;
12935 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
12936 vtotal /= 2;
12937
12938 crtc->scanline_offset = vtotal - 1;
12939 } else if (HAS_DDI(dev) &&
409ee761 12940 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12941 crtc->scanline_offset = 2;
12942 } else
12943 crtc->scanline_offset = 1;
12944}
12945
ad421372 12946static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12947{
225da59b 12948 struct drm_device *dev = state->dev;
ed6739ef 12949 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12950 struct intel_shared_dpll_config *shared_dpll = NULL;
ed6739ef 12951 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12952 struct intel_crtc_state *intel_crtc_state;
12953 struct drm_crtc *crtc;
12954 struct drm_crtc_state *crtc_state;
0a9ab303 12955 int i;
ed6739ef
ACO
12956
12957 if (!dev_priv->display.crtc_compute_clock)
ad421372 12958 return;
ed6739ef 12959
0a9ab303 12960 for_each_crtc_in_state(state, crtc, crtc_state, i) {
ad421372
ML
12961 int dpll;
12962
0a9ab303 12963 intel_crtc = to_intel_crtc(crtc);
4978cc93 12964 intel_crtc_state = to_intel_crtc_state(crtc_state);
ad421372 12965 dpll = intel_crtc_state->shared_dpll;
0a9ab303 12966
ad421372 12967 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
225da59b
ACO
12968 continue;
12969
ad421372 12970 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
0a9ab303 12971
ad421372
ML
12972 if (!shared_dpll)
12973 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12974
ad421372
ML
12975 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
12976 }
ed6739ef
ACO
12977}
12978
99d736a2
ML
12979/*
12980 * This implements the workaround described in the "notes" section of the mode
12981 * set sequence documentation. When going from no pipes or single pipe to
12982 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12983 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12984 */
12985static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12986{
12987 struct drm_crtc_state *crtc_state;
12988 struct intel_crtc *intel_crtc;
12989 struct drm_crtc *crtc;
12990 struct intel_crtc_state *first_crtc_state = NULL;
12991 struct intel_crtc_state *other_crtc_state = NULL;
12992 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12993 int i;
12994
12995 /* look at all crtc's that are going to be enabled in during modeset */
12996 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12997 intel_crtc = to_intel_crtc(crtc);
12998
12999 if (!crtc_state->active || !needs_modeset(crtc_state))
13000 continue;
13001
13002 if (first_crtc_state) {
13003 other_crtc_state = to_intel_crtc_state(crtc_state);
13004 break;
13005 } else {
13006 first_crtc_state = to_intel_crtc_state(crtc_state);
13007 first_pipe = intel_crtc->pipe;
13008 }
13009 }
13010
13011 /* No workaround needed? */
13012 if (!first_crtc_state)
13013 return 0;
13014
13015 /* w/a possibly needed, check how many crtc's are already enabled. */
13016 for_each_intel_crtc(state->dev, intel_crtc) {
13017 struct intel_crtc_state *pipe_config;
13018
13019 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13020 if (IS_ERR(pipe_config))
13021 return PTR_ERR(pipe_config);
13022
13023 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13024
13025 if (!pipe_config->base.active ||
13026 needs_modeset(&pipe_config->base))
13027 continue;
13028
13029 /* 2 or more enabled crtcs means no need for w/a */
13030 if (enabled_pipe != INVALID_PIPE)
13031 return 0;
13032
13033 enabled_pipe = intel_crtc->pipe;
13034 }
13035
13036 if (enabled_pipe != INVALID_PIPE)
13037 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13038 else if (other_crtc_state)
13039 other_crtc_state->hsw_workaround_pipe = first_pipe;
13040
13041 return 0;
13042}
13043
27c329ed
ML
13044static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13045{
13046 struct drm_crtc *crtc;
13047 struct drm_crtc_state *crtc_state;
13048 int ret = 0;
13049
13050 /* add all active pipes to the state */
13051 for_each_crtc(state->dev, crtc) {
13052 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13053 if (IS_ERR(crtc_state))
13054 return PTR_ERR(crtc_state);
13055
13056 if (!crtc_state->active || needs_modeset(crtc_state))
13057 continue;
13058
13059 crtc_state->mode_changed = true;
13060
13061 ret = drm_atomic_add_affected_connectors(state, crtc);
13062 if (ret)
13063 break;
13064
13065 ret = drm_atomic_add_affected_planes(state, crtc);
13066 if (ret)
13067 break;
13068 }
13069
13070 return ret;
13071}
13072
c347a676 13073static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
13074{
13075 struct drm_device *dev = state->dev;
27c329ed 13076 struct drm_i915_private *dev_priv = dev->dev_private;
054518dd
ACO
13077 int ret;
13078
b359283a
ML
13079 if (!check_digital_port_conflicts(state)) {
13080 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13081 return -EINVAL;
13082 }
13083
054518dd
ACO
13084 /*
13085 * See if the config requires any additional preparation, e.g.
13086 * to adjust global state with pipes off. We need to do this
13087 * here so we can get the modeset_pipe updated config for the new
13088 * mode set on this crtc. For other crtcs we need to use the
13089 * adjusted_mode bits in the crtc directly.
13090 */
27c329ed
ML
13091 if (dev_priv->display.modeset_calc_cdclk) {
13092 unsigned int cdclk;
b432e5cf 13093
27c329ed
ML
13094 ret = dev_priv->display.modeset_calc_cdclk(state);
13095
13096 cdclk = to_intel_atomic_state(state)->cdclk;
13097 if (!ret && cdclk != dev_priv->cdclk_freq)
13098 ret = intel_modeset_all_pipes(state);
13099
13100 if (ret < 0)
054518dd 13101 return ret;
27c329ed
ML
13102 } else
13103 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
054518dd 13104
ad421372 13105 intel_modeset_clear_plls(state);
054518dd 13106
99d736a2 13107 if (IS_HASWELL(dev))
ad421372 13108 return haswell_mode_set_planes_workaround(state);
99d736a2 13109
ad421372 13110 return 0;
c347a676
ACO
13111}
13112
aa363136
MR
13113/*
13114 * Handle calculation of various watermark data at the end of the atomic check
13115 * phase. The code here should be run after the per-crtc and per-plane 'check'
13116 * handlers to ensure that all derived state has been updated.
13117 */
13118static void calc_watermark_data(struct drm_atomic_state *state)
13119{
13120 struct drm_device *dev = state->dev;
13121 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13122 struct drm_crtc *crtc;
13123 struct drm_crtc_state *cstate;
13124 struct drm_plane *plane;
13125 struct drm_plane_state *pstate;
13126
13127 /*
13128 * Calculate watermark configuration details now that derived
13129 * plane/crtc state is all properly updated.
13130 */
13131 drm_for_each_crtc(crtc, dev) {
13132 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13133 crtc->state;
13134
13135 if (cstate->active)
13136 intel_state->wm_config.num_pipes_active++;
13137 }
13138 drm_for_each_legacy_plane(plane, dev) {
13139 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13140 plane->state;
13141
13142 if (!to_intel_plane_state(pstate)->visible)
13143 continue;
13144
13145 intel_state->wm_config.sprites_enabled = true;
13146 if (pstate->crtc_w != pstate->src_w >> 16 ||
13147 pstate->crtc_h != pstate->src_h >> 16)
13148 intel_state->wm_config.sprites_scaled = true;
13149 }
13150}
13151
74c090b1
ML
13152/**
13153 * intel_atomic_check - validate state object
13154 * @dev: drm device
13155 * @state: state to validate
13156 */
13157static int intel_atomic_check(struct drm_device *dev,
13158 struct drm_atomic_state *state)
c347a676 13159{
aa363136 13160 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
13161 struct drm_crtc *crtc;
13162 struct drm_crtc_state *crtc_state;
13163 int ret, i;
61333b60 13164 bool any_ms = false;
c347a676 13165
74c090b1 13166 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
13167 if (ret)
13168 return ret;
13169
c347a676 13170 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
13171 struct intel_crtc_state *pipe_config =
13172 to_intel_crtc_state(crtc_state);
1ed51de9
DV
13173
13174 /* Catch I915_MODE_FLAG_INHERITED */
13175 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13176 crtc_state->mode_changed = true;
cfb23ed6 13177
61333b60
ML
13178 if (!crtc_state->enable) {
13179 if (needs_modeset(crtc_state))
13180 any_ms = true;
c347a676 13181 continue;
61333b60 13182 }
c347a676 13183
26495481 13184 if (!needs_modeset(crtc_state))
cfb23ed6
ML
13185 continue;
13186
26495481
DV
13187 /* FIXME: For only active_changed we shouldn't need to do any
13188 * state recomputation at all. */
13189
1ed51de9
DV
13190 ret = drm_atomic_add_affected_connectors(state, crtc);
13191 if (ret)
13192 return ret;
b359283a 13193
cfb23ed6 13194 ret = intel_modeset_pipe_config(crtc, pipe_config);
c347a676
ACO
13195 if (ret)
13196 return ret;
13197
6764e9f8 13198 if (intel_pipe_config_compare(state->dev,
cfb23ed6 13199 to_intel_crtc_state(crtc->state),
1ed51de9 13200 pipe_config, true)) {
26495481 13201 crtc_state->mode_changed = false;
bfd16b2a 13202 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
DV
13203 }
13204
13205 if (needs_modeset(crtc_state)) {
13206 any_ms = true;
cfb23ed6
ML
13207
13208 ret = drm_atomic_add_affected_planes(state, crtc);
13209 if (ret)
13210 return ret;
13211 }
61333b60 13212
26495481
DV
13213 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13214 needs_modeset(crtc_state) ?
13215 "[modeset]" : "[fastset]");
c347a676
ACO
13216 }
13217
61333b60
ML
13218 if (any_ms) {
13219 ret = intel_modeset_checks(state);
13220
13221 if (ret)
13222 return ret;
27c329ed 13223 } else
aa363136 13224 intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
76305b1a 13225
aa363136
MR
13226 ret = drm_atomic_helper_check_planes(state->dev, state);
13227 if (ret)
13228 return ret;
13229
13230 calc_watermark_data(state);
13231
13232 return 0;
054518dd
ACO
13233}
13234
5008e874
ML
13235static int intel_atomic_prepare_commit(struct drm_device *dev,
13236 struct drm_atomic_state *state,
13237 bool async)
13238{
7580d774
ML
13239 struct drm_i915_private *dev_priv = dev->dev_private;
13240 struct drm_plane_state *plane_state;
5008e874 13241 struct drm_crtc_state *crtc_state;
7580d774 13242 struct drm_plane *plane;
5008e874
ML
13243 struct drm_crtc *crtc;
13244 int i, ret;
13245
13246 if (async) {
13247 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13248 return -EINVAL;
13249 }
13250
13251 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13252 ret = intel_crtc_wait_for_pending_flips(crtc);
13253 if (ret)
13254 return ret;
7580d774
ML
13255
13256 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13257 flush_workqueue(dev_priv->wq);
5008e874
ML
13258 }
13259
f935675f
ML
13260 ret = mutex_lock_interruptible(&dev->struct_mutex);
13261 if (ret)
13262 return ret;
13263
5008e874 13264 ret = drm_atomic_helper_prepare_planes(dev, state);
7580d774
ML
13265 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13266 u32 reset_counter;
13267
13268 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13269 mutex_unlock(&dev->struct_mutex);
13270
13271 for_each_plane_in_state(state, plane, plane_state, i) {
13272 struct intel_plane_state *intel_plane_state =
13273 to_intel_plane_state(plane_state);
13274
13275 if (!intel_plane_state->wait_req)
13276 continue;
13277
13278 ret = __i915_wait_request(intel_plane_state->wait_req,
13279 reset_counter, true,
13280 NULL, NULL);
13281
13282 /* Swallow -EIO errors to allow updates during hw lockup. */
13283 if (ret == -EIO)
13284 ret = 0;
13285
13286 if (ret)
13287 break;
13288 }
13289
13290 if (!ret)
13291 return 0;
13292
13293 mutex_lock(&dev->struct_mutex);
13294 drm_atomic_helper_cleanup_planes(dev, state);
13295 }
5008e874 13296
f935675f 13297 mutex_unlock(&dev->struct_mutex);
5008e874
ML
13298 return ret;
13299}
13300
74c090b1
ML
13301/**
13302 * intel_atomic_commit - commit validated state object
13303 * @dev: DRM device
13304 * @state: the top-level driver state object
13305 * @async: asynchronous commit
13306 *
13307 * This function commits a top-level state object that has been validated
13308 * with drm_atomic_helper_check().
13309 *
13310 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13311 * we can only handle plane-related operations and do not yet support
13312 * asynchronous commit.
13313 *
13314 * RETURNS
13315 * Zero for success or -errno.
13316 */
13317static int intel_atomic_commit(struct drm_device *dev,
13318 struct drm_atomic_state *state,
13319 bool async)
a6778b3c 13320{
fbee40df 13321 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303 13322 struct drm_crtc_state *crtc_state;
7580d774 13323 struct drm_crtc *crtc;
c0c36b94 13324 int ret = 0;
0a9ab303 13325 int i;
61333b60 13326 bool any_ms = false;
a6778b3c 13327
5008e874 13328 ret = intel_atomic_prepare_commit(dev, state, async);
7580d774
ML
13329 if (ret) {
13330 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
d4afb8cc 13331 return ret;
7580d774 13332 }
d4afb8cc 13333
1c5e19f8 13334 drm_atomic_helper_swap_state(dev, state);
aa363136 13335 dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
1c5e19f8 13336
0a9ab303 13337 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13339
61333b60
ML
13340 if (!needs_modeset(crtc->state))
13341 continue;
13342
13343 any_ms = true;
a539205a 13344 intel_pre_plane_update(intel_crtc);
460da916 13345
a539205a
ML
13346 if (crtc_state->active) {
13347 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13348 dev_priv->display.crtc_disable(crtc);
eddfcbcd
ML
13349 intel_crtc->active = false;
13350 intel_disable_shared_dpll(intel_crtc);
a539205a 13351 }
b8cecdf5 13352 }
7758a113 13353
ea9d758d
DV
13354 /* Only after disabling all output pipelines that will be changed can we
13355 * update the the output configuration. */
4740b0f2 13356 intel_modeset_update_crtc_state(state);
f6e5b160 13357
4740b0f2
ML
13358 if (any_ms) {
13359 intel_shared_dpll_commit(state);
13360
13361 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
61333b60 13362 modeset_update_crtc_power_domains(state);
4740b0f2 13363 }
47fab737 13364
a6778b3c 13365 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13366 for_each_crtc_in_state(state, crtc, crtc_state, i) {
f6ac4b2a
ML
13367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13368 bool modeset = needs_modeset(crtc->state);
bfd16b2a
ML
13369 bool update_pipe = !modeset &&
13370 to_intel_crtc_state(crtc->state)->update_pipe;
13371 unsigned long put_domains = 0;
f6ac4b2a 13372
9f836f90
PJ
13373 if (modeset)
13374 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
13375
f6ac4b2a 13376 if (modeset && crtc->state->active) {
a539205a
ML
13377 update_scanline_offset(to_intel_crtc(crtc));
13378 dev_priv->display.crtc_enable(crtc);
13379 }
80715b2f 13380
bfd16b2a
ML
13381 if (update_pipe) {
13382 put_domains = modeset_get_crtc_power_domains(crtc);
13383
13384 /* make sure intel_modeset_check_state runs */
13385 any_ms = true;
13386 }
13387
f6ac4b2a
ML
13388 if (!modeset)
13389 intel_pre_plane_update(intel_crtc);
13390
6173ee28
ML
13391 if (crtc->state->active &&
13392 (crtc->state->planes_changed || update_pipe))
62852622 13393 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
bfd16b2a
ML
13394
13395 if (put_domains)
13396 modeset_put_power_domains(dev_priv, put_domains);
13397
f6ac4b2a 13398 intel_post_plane_update(intel_crtc);
9f836f90
PJ
13399
13400 if (modeset)
13401 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
80715b2f 13402 }
a6778b3c 13403
a6778b3c 13404 /* FIXME: add subpixel order */
83a57153 13405
74c090b1 13406 drm_atomic_helper_wait_for_vblanks(dev, state);
f935675f
ML
13407
13408 mutex_lock(&dev->struct_mutex);
d4afb8cc 13409 drm_atomic_helper_cleanup_planes(dev, state);
f935675f 13410 mutex_unlock(&dev->struct_mutex);
2bfb4627 13411
74c090b1 13412 if (any_ms)
ee165b1a
ML
13413 intel_modeset_check_state(dev, state);
13414
13415 drm_atomic_state_free(state);
f30da187 13416
74c090b1 13417 return 0;
7f27126e
JB
13418}
13419
c0c36b94
CW
13420void intel_crtc_restore_mode(struct drm_crtc *crtc)
13421{
83a57153
ACO
13422 struct drm_device *dev = crtc->dev;
13423 struct drm_atomic_state *state;
e694eb02 13424 struct drm_crtc_state *crtc_state;
2bfb4627 13425 int ret;
83a57153
ACO
13426
13427 state = drm_atomic_state_alloc(dev);
13428 if (!state) {
e694eb02 13429 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
83a57153
ACO
13430 crtc->base.id);
13431 return;
13432 }
13433
e694eb02 13434 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 13435
e694eb02
ML
13436retry:
13437 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13438 ret = PTR_ERR_OR_ZERO(crtc_state);
13439 if (!ret) {
13440 if (!crtc_state->active)
13441 goto out;
83a57153 13442
e694eb02 13443 crtc_state->mode_changed = true;
74c090b1 13444 ret = drm_atomic_commit(state);
83a57153
ACO
13445 }
13446
e694eb02
ML
13447 if (ret == -EDEADLK) {
13448 drm_atomic_state_clear(state);
13449 drm_modeset_backoff(state->acquire_ctx);
13450 goto retry;
4ed9fb37 13451 }
4be07317 13452
2bfb4627 13453 if (ret)
e694eb02 13454out:
2bfb4627 13455 drm_atomic_state_free(state);
c0c36b94
CW
13456}
13457
25c5b266
DV
13458#undef for_each_intel_crtc_masked
13459
f6e5b160 13460static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13461 .gamma_set = intel_crtc_gamma_set,
74c090b1 13462 .set_config = drm_atomic_helper_set_config,
f6e5b160
CW
13463 .destroy = intel_crtc_destroy,
13464 .page_flip = intel_crtc_page_flip,
1356837e
MR
13465 .atomic_duplicate_state = intel_crtc_duplicate_state,
13466 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13467};
13468
5358901f
DV
13469static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13470 struct intel_shared_dpll *pll,
13471 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13472{
5358901f 13473 uint32_t val;
ee7b9f93 13474
f458ebbc 13475 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13476 return false;
13477
5358901f 13478 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13479 hw_state->dpll = val;
13480 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13481 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13482
13483 return val & DPLL_VCO_ENABLE;
13484}
13485
15bdd4cf
DV
13486static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13487 struct intel_shared_dpll *pll)
13488{
3e369b76
ACO
13489 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13490 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13491}
13492
e7b903d2
DV
13493static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13494 struct intel_shared_dpll *pll)
13495{
e7b903d2 13496 /* PCH refclock must be enabled first */
89eff4be 13497 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13498
3e369b76 13499 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13500
13501 /* Wait for the clocks to stabilize. */
13502 POSTING_READ(PCH_DPLL(pll->id));
13503 udelay(150);
13504
13505 /* The pixel multiplier can only be updated once the
13506 * DPLL is enabled and the clocks are stable.
13507 *
13508 * So write it again.
13509 */
3e369b76 13510 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13511 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13512 udelay(200);
13513}
13514
13515static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13516 struct intel_shared_dpll *pll)
13517{
13518 struct drm_device *dev = dev_priv->dev;
13519 struct intel_crtc *crtc;
e7b903d2
DV
13520
13521 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13522 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13523 if (intel_crtc_to_shared_dpll(crtc) == pll)
13524 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13525 }
13526
15bdd4cf
DV
13527 I915_WRITE(PCH_DPLL(pll->id), 0);
13528 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13529 udelay(200);
13530}
13531
46edb027
DV
13532static char *ibx_pch_dpll_names[] = {
13533 "PCH DPLL A",
13534 "PCH DPLL B",
13535};
13536
7c74ade1 13537static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13538{
e7b903d2 13539 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13540 int i;
13541
7c74ade1 13542 dev_priv->num_shared_dpll = 2;
ee7b9f93 13543
e72f9fbf 13544 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13545 dev_priv->shared_dplls[i].id = i;
13546 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13547 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13548 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13549 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13550 dev_priv->shared_dplls[i].get_hw_state =
13551 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13552 }
13553}
13554
7c74ade1
DV
13555static void intel_shared_dpll_init(struct drm_device *dev)
13556{
e7b903d2 13557 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13558
9cd86933
DV
13559 if (HAS_DDI(dev))
13560 intel_ddi_pll_init(dev);
13561 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13562 ibx_pch_dpll_init(dev);
13563 else
13564 dev_priv->num_shared_dpll = 0;
13565
13566 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13567}
13568
6beb8c23
MR
13569/**
13570 * intel_prepare_plane_fb - Prepare fb for usage on plane
13571 * @plane: drm plane to prepare for
13572 * @fb: framebuffer to prepare for presentation
13573 *
13574 * Prepares a framebuffer for usage on a display plane. Generally this
13575 * involves pinning the underlying object and updating the frontbuffer tracking
13576 * bits. Some older platforms need special physical address handling for
13577 * cursor planes.
13578 *
f935675f
ML
13579 * Must be called with struct_mutex held.
13580 *
6beb8c23
MR
13581 * Returns 0 on success, negative error code on failure.
13582 */
13583int
13584intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee 13585 const struct drm_plane_state *new_state)
465c120c
MR
13586{
13587 struct drm_device *dev = plane->dev;
844f9111 13588 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 13589 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23 13590 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 13591 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
6beb8c23 13592 int ret = 0;
465c120c 13593
1ee49399 13594 if (!obj && !old_obj)
465c120c
MR
13595 return 0;
13596
5008e874
ML
13597 if (old_obj) {
13598 struct drm_crtc_state *crtc_state =
13599 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13600
13601 /* Big Hammer, we also need to ensure that any pending
13602 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13603 * current scanout is retired before unpinning the old
13604 * framebuffer. Note that we rely on userspace rendering
13605 * into the buffer attached to the pipe they are waiting
13606 * on. If not, userspace generates a GPU hang with IPEHR
13607 * point to the MI_WAIT_FOR_EVENT.
13608 *
13609 * This should only fail upon a hung GPU, in which case we
13610 * can safely continue.
13611 */
13612 if (needs_modeset(crtc_state))
13613 ret = i915_gem_object_wait_rendering(old_obj, true);
13614
13615 /* Swallow -EIO errors to allow updates during hw lockup. */
13616 if (ret && ret != -EIO)
f935675f 13617 return ret;
5008e874
ML
13618 }
13619
1ee49399
ML
13620 if (!obj) {
13621 ret = 0;
13622 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
6beb8c23
MR
13623 INTEL_INFO(dev)->cursor_needs_physical) {
13624 int align = IS_I830(dev) ? 16 * 1024 : 256;
13625 ret = i915_gem_object_attach_phys(obj, align);
13626 if (ret)
13627 DRM_DEBUG_KMS("failed to attach phys object\n");
13628 } else {
7580d774 13629 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
6beb8c23 13630 }
465c120c 13631
7580d774
ML
13632 if (ret == 0) {
13633 if (obj) {
13634 struct intel_plane_state *plane_state =
13635 to_intel_plane_state(new_state);
13636
13637 i915_gem_request_assign(&plane_state->wait_req,
13638 obj->last_write_req);
13639 }
13640
a9ff8714 13641 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774 13642 }
fdd508a6 13643
6beb8c23
MR
13644 return ret;
13645}
13646
38f3ce3a
MR
13647/**
13648 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13649 * @plane: drm plane to clean up for
13650 * @fb: old framebuffer that was on plane
13651 *
13652 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
13653 *
13654 * Must be called with struct_mutex held.
38f3ce3a
MR
13655 */
13656void
13657intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee 13658 const struct drm_plane_state *old_state)
38f3ce3a
MR
13659{
13660 struct drm_device *dev = plane->dev;
1ee49399 13661 struct intel_plane *intel_plane = to_intel_plane(plane);
7580d774 13662 struct intel_plane_state *old_intel_state;
1ee49399
ML
13663 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13664 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 13665
7580d774
ML
13666 old_intel_state = to_intel_plane_state(old_state);
13667
1ee49399 13668 if (!obj && !old_obj)
38f3ce3a
MR
13669 return;
13670
1ee49399
ML
13671 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13672 !INTEL_INFO(dev)->cursor_needs_physical))
844f9111 13673 intel_unpin_fb_obj(old_state->fb, old_state);
1ee49399
ML
13674
13675 /* prepare_fb aborted? */
13676 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13677 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13678 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
7580d774
ML
13679
13680 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
13681
465c120c
MR
13682}
13683
6156a456
CK
13684int
13685skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13686{
13687 int max_scale;
13688 struct drm_device *dev;
13689 struct drm_i915_private *dev_priv;
13690 int crtc_clock, cdclk;
13691
13692 if (!intel_crtc || !crtc_state)
13693 return DRM_PLANE_HELPER_NO_SCALING;
13694
13695 dev = intel_crtc->base.dev;
13696 dev_priv = dev->dev_private;
13697 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13698 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 13699
54bf1ce6 13700 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
13701 return DRM_PLANE_HELPER_NO_SCALING;
13702
13703 /*
13704 * skl max scale is lower of:
13705 * close to 3 but not 3, -1 is for that purpose
13706 * or
13707 * cdclk/crtc_clock
13708 */
13709 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13710
13711 return max_scale;
13712}
13713
465c120c 13714static int
3c692a41 13715intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13716 struct intel_crtc_state *crtc_state,
3c692a41
GP
13717 struct intel_plane_state *state)
13718{
2b875c22
MR
13719 struct drm_crtc *crtc = state->base.crtc;
13720 struct drm_framebuffer *fb = state->base.fb;
6156a456 13721 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13722 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13723 bool can_position = false;
465c120c 13724
061e4b8d
ML
13725 /* use scaler when colorkey is not required */
13726 if (INTEL_INFO(plane->dev)->gen >= 9 &&
818ed961 13727 state->ckey.flags == I915_SET_COLORKEY_NONE) {
061e4b8d
ML
13728 min_scale = 1;
13729 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
d8106366 13730 can_position = true;
6156a456 13731 }
d8106366 13732
061e4b8d
ML
13733 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13734 &state->dst, &state->clip,
da20eabd
ML
13735 min_scale, max_scale,
13736 can_position, true,
13737 &state->visible);
14af293f
GP
13738}
13739
13740static void
13741intel_commit_primary_plane(struct drm_plane *plane,
13742 struct intel_plane_state *state)
13743{
2b875c22
MR
13744 struct drm_crtc *crtc = state->base.crtc;
13745 struct drm_framebuffer *fb = state->base.fb;
13746 struct drm_device *dev = plane->dev;
14af293f 13747 struct drm_i915_private *dev_priv = dev->dev_private;
14af293f 13748
ea2c67bb 13749 crtc = crtc ? crtc : plane->crtc;
ccc759dc 13750
d4b08630
ML
13751 dev_priv->display.update_primary_plane(crtc, fb,
13752 state->src.x1 >> 16,
13753 state->src.y1 >> 16);
465c120c
MR
13754}
13755
a8ad0d8e
ML
13756static void
13757intel_disable_primary_plane(struct drm_plane *plane,
7fabf5ef 13758 struct drm_crtc *crtc)
a8ad0d8e
ML
13759{
13760 struct drm_device *dev = plane->dev;
13761 struct drm_i915_private *dev_priv = dev->dev_private;
13762
a8ad0d8e
ML
13763 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13764}
13765
613d2b27
ML
13766static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13767 struct drm_crtc_state *old_crtc_state)
3c692a41 13768{
32b7eeec 13769 struct drm_device *dev = crtc->dev;
3c692a41 13770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
bfd16b2a
ML
13771 struct intel_crtc_state *old_intel_state =
13772 to_intel_crtc_state(old_crtc_state);
13773 bool modeset = needs_modeset(crtc->state);
3c692a41 13774
f015c551 13775 if (intel_crtc->atomic.update_wm_pre)
32b7eeec 13776 intel_update_watermarks(crtc);
3c692a41 13777
c34c9ee4 13778 /* Perform vblank evasion around commit operation */
62852622 13779 intel_pipe_update_start(intel_crtc);
0583236e 13780
bfd16b2a
ML
13781 if (modeset)
13782 return;
13783
13784 if (to_intel_crtc_state(crtc->state)->update_pipe)
13785 intel_update_pipe_config(intel_crtc, old_intel_state);
13786 else if (INTEL_INFO(dev)->gen >= 9)
0583236e 13787 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13788}
13789
613d2b27
ML
13790static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13791 struct drm_crtc_state *old_crtc_state)
32b7eeec 13792{
32b7eeec 13793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13794
62852622 13795 intel_pipe_update_end(intel_crtc);
3c692a41
GP
13796}
13797
cf4c7c12 13798/**
4a3b8769
MR
13799 * intel_plane_destroy - destroy a plane
13800 * @plane: plane to destroy
cf4c7c12 13801 *
4a3b8769
MR
13802 * Common destruction function for all types of planes (primary, cursor,
13803 * sprite).
cf4c7c12 13804 */
4a3b8769 13805void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13806{
13807 struct intel_plane *intel_plane = to_intel_plane(plane);
13808 drm_plane_cleanup(plane);
13809 kfree(intel_plane);
13810}
13811
65a3fea0 13812const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13813 .update_plane = drm_atomic_helper_update_plane,
13814 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13815 .destroy = intel_plane_destroy,
c196e1d6 13816 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13817 .atomic_get_property = intel_plane_atomic_get_property,
13818 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13819 .atomic_duplicate_state = intel_plane_duplicate_state,
13820 .atomic_destroy_state = intel_plane_destroy_state,
13821
465c120c
MR
13822};
13823
13824static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13825 int pipe)
13826{
13827 struct intel_plane *primary;
8e7d688b 13828 struct intel_plane_state *state;
465c120c 13829 const uint32_t *intel_primary_formats;
45e3743a 13830 unsigned int num_formats;
465c120c
MR
13831
13832 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13833 if (primary == NULL)
13834 return NULL;
13835
8e7d688b
MR
13836 state = intel_create_plane_state(&primary->base);
13837 if (!state) {
ea2c67bb
MR
13838 kfree(primary);
13839 return NULL;
13840 }
8e7d688b 13841 primary->base.state = &state->base;
ea2c67bb 13842
465c120c
MR
13843 primary->can_scale = false;
13844 primary->max_downscale = 1;
6156a456
CK
13845 if (INTEL_INFO(dev)->gen >= 9) {
13846 primary->can_scale = true;
af99ceda 13847 state->scaler_id = -1;
6156a456 13848 }
465c120c
MR
13849 primary->pipe = pipe;
13850 primary->plane = pipe;
a9ff8714 13851 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179
MR
13852 primary->check_plane = intel_check_primary_plane;
13853 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13854 primary->disable_plane = intel_disable_primary_plane;
465c120c
MR
13855 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13856 primary->plane = !pipe;
13857
6c0fd451
DL
13858 if (INTEL_INFO(dev)->gen >= 9) {
13859 intel_primary_formats = skl_primary_formats;
13860 num_formats = ARRAY_SIZE(skl_primary_formats);
13861 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13862 intel_primary_formats = i965_primary_formats;
13863 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13864 } else {
13865 intel_primary_formats = i8xx_primary_formats;
13866 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13867 }
13868
13869 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13870 &intel_plane_funcs,
465c120c
MR
13871 intel_primary_formats, num_formats,
13872 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13873
3b7a5119
SJ
13874 if (INTEL_INFO(dev)->gen >= 4)
13875 intel_create_rotation_property(dev, primary);
48404c1e 13876
ea2c67bb
MR
13877 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13878
465c120c
MR
13879 return &primary->base;
13880}
13881
3b7a5119
SJ
13882void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13883{
13884 if (!dev->mode_config.rotation_property) {
13885 unsigned long flags = BIT(DRM_ROTATE_0) |
13886 BIT(DRM_ROTATE_180);
13887
13888 if (INTEL_INFO(dev)->gen >= 9)
13889 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13890
13891 dev->mode_config.rotation_property =
13892 drm_mode_create_rotation_property(dev, flags);
13893 }
13894 if (dev->mode_config.rotation_property)
13895 drm_object_attach_property(&plane->base.base,
13896 dev->mode_config.rotation_property,
13897 plane->base.state->rotation);
13898}
13899
3d7d6510 13900static int
852e787c 13901intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13902 struct intel_crtc_state *crtc_state,
852e787c 13903 struct intel_plane_state *state)
3d7d6510 13904{
061e4b8d 13905 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13906 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13907 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
757f9a3e
GP
13908 unsigned stride;
13909 int ret;
3d7d6510 13910
061e4b8d
ML
13911 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13912 &state->dst, &state->clip,
3d7d6510
MR
13913 DRM_PLANE_HELPER_NO_SCALING,
13914 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13915 true, true, &state->visible);
757f9a3e
GP
13916 if (ret)
13917 return ret;
13918
757f9a3e
GP
13919 /* if we want to turn off the cursor ignore width and height */
13920 if (!obj)
da20eabd 13921 return 0;
757f9a3e 13922
757f9a3e 13923 /* Check for which cursor types we support */
061e4b8d 13924 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13925 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13926 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13927 return -EINVAL;
13928 }
13929
ea2c67bb
MR
13930 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13931 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13932 DRM_DEBUG_KMS("buffer is too small\n");
13933 return -ENOMEM;
13934 }
13935
3a656b54 13936 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 13937 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13938 return -EINVAL;
32b7eeec
MR
13939 }
13940
da20eabd 13941 return 0;
852e787c 13942}
3d7d6510 13943
a8ad0d8e
ML
13944static void
13945intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13946 struct drm_crtc *crtc)
a8ad0d8e 13947{
a8ad0d8e
ML
13948 intel_crtc_update_cursor(crtc, false);
13949}
13950
f4a2cf29 13951static void
852e787c
GP
13952intel_commit_cursor_plane(struct drm_plane *plane,
13953 struct intel_plane_state *state)
13954{
2b875c22 13955 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13956 struct drm_device *dev = plane->dev;
13957 struct intel_crtc *intel_crtc;
2b875c22 13958 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13959 uint32_t addr;
852e787c 13960
ea2c67bb
MR
13961 crtc = crtc ? crtc : plane->crtc;
13962 intel_crtc = to_intel_crtc(crtc);
13963
a912f12f
GP
13964 if (intel_crtc->cursor_bo == obj)
13965 goto update;
4ed91096 13966
f4a2cf29 13967 if (!obj)
a912f12f 13968 addr = 0;
f4a2cf29 13969 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13970 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13971 else
a912f12f 13972 addr = obj->phys_handle->busaddr;
852e787c 13973
a912f12f
GP
13974 intel_crtc->cursor_addr = addr;
13975 intel_crtc->cursor_bo = obj;
852e787c 13976
302d19ac 13977update:
62852622 13978 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13979}
13980
3d7d6510
MR
13981static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13982 int pipe)
13983{
13984 struct intel_plane *cursor;
8e7d688b 13985 struct intel_plane_state *state;
3d7d6510
MR
13986
13987 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13988 if (cursor == NULL)
13989 return NULL;
13990
8e7d688b
MR
13991 state = intel_create_plane_state(&cursor->base);
13992 if (!state) {
ea2c67bb
MR
13993 kfree(cursor);
13994 return NULL;
13995 }
8e7d688b 13996 cursor->base.state = &state->base;
ea2c67bb 13997
3d7d6510
MR
13998 cursor->can_scale = false;
13999 cursor->max_downscale = 1;
14000 cursor->pipe = pipe;
14001 cursor->plane = pipe;
a9ff8714 14002 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179
MR
14003 cursor->check_plane = intel_check_cursor_plane;
14004 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 14005 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14006
14007 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14008 &intel_plane_funcs,
3d7d6510
MR
14009 intel_cursor_formats,
14010 ARRAY_SIZE(intel_cursor_formats),
14011 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
14012
14013 if (INTEL_INFO(dev)->gen >= 4) {
14014 if (!dev->mode_config.rotation_property)
14015 dev->mode_config.rotation_property =
14016 drm_mode_create_rotation_property(dev,
14017 BIT(DRM_ROTATE_0) |
14018 BIT(DRM_ROTATE_180));
14019 if (dev->mode_config.rotation_property)
14020 drm_object_attach_property(&cursor->base.base,
14021 dev->mode_config.rotation_property,
8e7d688b 14022 state->base.rotation);
4398ad45
VS
14023 }
14024
af99ceda
CK
14025 if (INTEL_INFO(dev)->gen >=9)
14026 state->scaler_id = -1;
14027
ea2c67bb
MR
14028 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14029
3d7d6510
MR
14030 return &cursor->base;
14031}
14032
549e2bfb
CK
14033static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14034 struct intel_crtc_state *crtc_state)
14035{
14036 int i;
14037 struct intel_scaler *intel_scaler;
14038 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14039
14040 for (i = 0; i < intel_crtc->num_scalers; i++) {
14041 intel_scaler = &scaler_state->scalers[i];
14042 intel_scaler->in_use = 0;
549e2bfb
CK
14043 intel_scaler->mode = PS_SCALER_MODE_DYN;
14044 }
14045
14046 scaler_state->scaler_id = -1;
14047}
14048
b358d0a6 14049static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14050{
fbee40df 14051 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14052 struct intel_crtc *intel_crtc;
f5de6e07 14053 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14054 struct drm_plane *primary = NULL;
14055 struct drm_plane *cursor = NULL;
465c120c 14056 int i, ret;
79e53945 14057
955382f3 14058 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14059 if (intel_crtc == NULL)
14060 return;
14061
f5de6e07
ACO
14062 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14063 if (!crtc_state)
14064 goto fail;
550acefd
ACO
14065 intel_crtc->config = crtc_state;
14066 intel_crtc->base.state = &crtc_state->base;
07878248 14067 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14068
549e2bfb
CK
14069 /* initialize shared scalers */
14070 if (INTEL_INFO(dev)->gen >= 9) {
14071 if (pipe == PIPE_C)
14072 intel_crtc->num_scalers = 1;
14073 else
14074 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14075
14076 skl_init_scalers(dev, intel_crtc, crtc_state);
14077 }
14078
465c120c 14079 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14080 if (!primary)
14081 goto fail;
14082
14083 cursor = intel_cursor_plane_create(dev, pipe);
14084 if (!cursor)
14085 goto fail;
14086
465c120c 14087 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14088 cursor, &intel_crtc_funcs);
14089 if (ret)
14090 goto fail;
79e53945
JB
14091
14092 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14093 for (i = 0; i < 256; i++) {
14094 intel_crtc->lut_r[i] = i;
14095 intel_crtc->lut_g[i] = i;
14096 intel_crtc->lut_b[i] = i;
14097 }
14098
1f1c2e24
VS
14099 /*
14100 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14101 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14102 */
80824003
JB
14103 intel_crtc->pipe = pipe;
14104 intel_crtc->plane = pipe;
3a77c4c4 14105 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14106 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14107 intel_crtc->plane = !pipe;
80824003
JB
14108 }
14109
4b0e333e
CW
14110 intel_crtc->cursor_base = ~0;
14111 intel_crtc->cursor_cntl = ~0;
dc41c154 14112 intel_crtc->cursor_size = ~0;
8d7849db 14113
852eb00d
VS
14114 intel_crtc->wm.cxsr_allowed = true;
14115
22fd0fab
JB
14116 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14117 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14118 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14119 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14120
79e53945 14121 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14122
14123 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14124 return;
14125
14126fail:
14127 if (primary)
14128 drm_plane_cleanup(primary);
14129 if (cursor)
14130 drm_plane_cleanup(cursor);
f5de6e07 14131 kfree(crtc_state);
3d7d6510 14132 kfree(intel_crtc);
79e53945
JB
14133}
14134
752aa88a
JB
14135enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14136{
14137 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14138 struct drm_device *dev = connector->base.dev;
752aa88a 14139
51fd371b 14140 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14141
d3babd3f 14142 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14143 return INVALID_PIPE;
14144
14145 return to_intel_crtc(encoder->crtc)->pipe;
14146}
14147
08d7b3d1 14148int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14149 struct drm_file *file)
08d7b3d1 14150{
08d7b3d1 14151 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14152 struct drm_crtc *drmmode_crtc;
c05422d5 14153 struct intel_crtc *crtc;
08d7b3d1 14154
7707e653 14155 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14156
7707e653 14157 if (!drmmode_crtc) {
08d7b3d1 14158 DRM_ERROR("no such CRTC id\n");
3f2c2057 14159 return -ENOENT;
08d7b3d1
CW
14160 }
14161
7707e653 14162 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14163 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14164
c05422d5 14165 return 0;
08d7b3d1
CW
14166}
14167
66a9278e 14168static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14169{
66a9278e
DV
14170 struct drm_device *dev = encoder->base.dev;
14171 struct intel_encoder *source_encoder;
79e53945 14172 int index_mask = 0;
79e53945
JB
14173 int entry = 0;
14174
b2784e15 14175 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14176 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14177 index_mask |= (1 << entry);
14178
79e53945
JB
14179 entry++;
14180 }
4ef69c7a 14181
79e53945
JB
14182 return index_mask;
14183}
14184
4d302442
CW
14185static bool has_edp_a(struct drm_device *dev)
14186{
14187 struct drm_i915_private *dev_priv = dev->dev_private;
14188
14189 if (!IS_MOBILE(dev))
14190 return false;
14191
14192 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14193 return false;
14194
e3589908 14195 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14196 return false;
14197
14198 return true;
14199}
14200
84b4e042
JB
14201static bool intel_crt_present(struct drm_device *dev)
14202{
14203 struct drm_i915_private *dev_priv = dev->dev_private;
14204
884497ed
DL
14205 if (INTEL_INFO(dev)->gen >= 9)
14206 return false;
14207
cf404ce4 14208 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14209 return false;
14210
14211 if (IS_CHERRYVIEW(dev))
14212 return false;
14213
14214 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14215 return false;
14216
14217 return true;
14218}
14219
79e53945
JB
14220static void intel_setup_outputs(struct drm_device *dev)
14221{
725e30ad 14222 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14223 struct intel_encoder *encoder;
cb0953d7 14224 bool dpd_is_edp = false;
79e53945 14225
c9093354 14226 intel_lvds_init(dev);
79e53945 14227
84b4e042 14228 if (intel_crt_present(dev))
79935fca 14229 intel_crt_init(dev);
cb0953d7 14230
c776eb2e
VK
14231 if (IS_BROXTON(dev)) {
14232 /*
14233 * FIXME: Broxton doesn't support port detection via the
14234 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14235 * detect the ports.
14236 */
14237 intel_ddi_init(dev, PORT_A);
14238 intel_ddi_init(dev, PORT_B);
14239 intel_ddi_init(dev, PORT_C);
14240 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14241 int found;
14242
de31facd
JB
14243 /*
14244 * Haswell uses DDI functions to detect digital outputs.
14245 * On SKL pre-D0 the strap isn't connected, so we assume
14246 * it's there.
14247 */
77179400 14248 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 14249 /* WaIgnoreDDIAStrap: skl */
ef11bdb3 14250 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
0e72a5b5
ED
14251 intel_ddi_init(dev, PORT_A);
14252
14253 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14254 * register */
14255 found = I915_READ(SFUSE_STRAP);
14256
14257 if (found & SFUSE_STRAP_DDIB_DETECTED)
14258 intel_ddi_init(dev, PORT_B);
14259 if (found & SFUSE_STRAP_DDIC_DETECTED)
14260 intel_ddi_init(dev, PORT_C);
14261 if (found & SFUSE_STRAP_DDID_DETECTED)
14262 intel_ddi_init(dev, PORT_D);
2800e4c2
RV
14263 /*
14264 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14265 */
ef11bdb3 14266 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
2800e4c2
RV
14267 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14268 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14269 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14270 intel_ddi_init(dev, PORT_E);
14271
0e72a5b5 14272 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14273 int found;
5d8a7752 14274 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14275
14276 if (has_edp_a(dev))
14277 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14278
dc0fa718 14279 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14280 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14281 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14282 if (!found)
e2debe91 14283 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14284 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14285 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14286 }
14287
dc0fa718 14288 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14289 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14290
dc0fa718 14291 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14292 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14293
5eb08b69 14294 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14295 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14296
270b3042 14297 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14298 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14299 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14300 /*
14301 * The DP_DETECTED bit is the latched state of the DDC
14302 * SDA pin at boot. However since eDP doesn't require DDC
14303 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14304 * eDP ports may have been muxed to an alternate function.
14305 * Thus we can't rely on the DP_DETECTED bit alone to detect
14306 * eDP ports. Consult the VBT as well as DP_DETECTED to
14307 * detect eDP ports.
14308 */
e66eb81d 14309 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
d2182a66 14310 !intel_dp_is_edp(dev, PORT_B))
e66eb81d
VS
14311 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14312 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
e17ac6db 14313 intel_dp_is_edp(dev, PORT_B))
e66eb81d 14314 intel_dp_init(dev, VLV_DP_B, PORT_B);
585a94b8 14315
e66eb81d 14316 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
d2182a66 14317 !intel_dp_is_edp(dev, PORT_C))
e66eb81d
VS
14318 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14319 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
e17ac6db 14320 intel_dp_is_edp(dev, PORT_C))
e66eb81d 14321 intel_dp_init(dev, VLV_DP_C, PORT_C);
19c03924 14322
9418c1f1 14323 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14324 /* eDP not supported on port D, so don't check VBT */
e66eb81d
VS
14325 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14326 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14327 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14328 intel_dp_init(dev, CHV_DP_D, PORT_D);
9418c1f1
VS
14329 }
14330
3cfca973 14331 intel_dsi_init(dev);
09da55dc 14332 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
27185ae1 14333 bool found = false;
7d57382e 14334
e2debe91 14335 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14336 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14337 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
3fec3d2f 14338 if (!found && IS_G4X(dev)) {
b01f2c3a 14339 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14340 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14341 }
27185ae1 14342
3fec3d2f 14343 if (!found && IS_G4X(dev))
ab9d7c30 14344 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14345 }
13520b05
KH
14346
14347 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14348
e2debe91 14349 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14350 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14351 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14352 }
27185ae1 14353
e2debe91 14354 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14355
3fec3d2f 14356 if (IS_G4X(dev)) {
b01f2c3a 14357 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14358 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14359 }
3fec3d2f 14360 if (IS_G4X(dev))
ab9d7c30 14361 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14362 }
27185ae1 14363
3fec3d2f 14364 if (IS_G4X(dev) &&
e7281eab 14365 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14366 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14367 } else if (IS_GEN2(dev))
79e53945
JB
14368 intel_dvo_init(dev);
14369
103a196f 14370 if (SUPPORTS_TV(dev))
79e53945
JB
14371 intel_tv_init(dev);
14372
0bc12bcb 14373 intel_psr_init(dev);
7c8f8a70 14374
b2784e15 14375 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14376 encoder->base.possible_crtcs = encoder->crtc_mask;
14377 encoder->base.possible_clones =
66a9278e 14378 intel_encoder_clones(encoder);
79e53945 14379 }
47356eb6 14380
dde86e2d 14381 intel_init_pch_refclk(dev);
270b3042
DV
14382
14383 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14384}
14385
14386static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14387{
60a5ca01 14388 struct drm_device *dev = fb->dev;
79e53945 14389 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14390
ef2d633e 14391 drm_framebuffer_cleanup(fb);
60a5ca01 14392 mutex_lock(&dev->struct_mutex);
ef2d633e 14393 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14394 drm_gem_object_unreference(&intel_fb->obj->base);
14395 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14396 kfree(intel_fb);
14397}
14398
14399static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14400 struct drm_file *file,
79e53945
JB
14401 unsigned int *handle)
14402{
14403 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14404 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14405
05394f39 14406 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14407}
14408
86c98588
RV
14409static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14410 struct drm_file *file,
14411 unsigned flags, unsigned color,
14412 struct drm_clip_rect *clips,
14413 unsigned num_clips)
14414{
14415 struct drm_device *dev = fb->dev;
14416 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14417 struct drm_i915_gem_object *obj = intel_fb->obj;
14418
14419 mutex_lock(&dev->struct_mutex);
74b4ea1e 14420 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
14421 mutex_unlock(&dev->struct_mutex);
14422
14423 return 0;
14424}
14425
79e53945
JB
14426static const struct drm_framebuffer_funcs intel_fb_funcs = {
14427 .destroy = intel_user_framebuffer_destroy,
14428 .create_handle = intel_user_framebuffer_create_handle,
86c98588 14429 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
14430};
14431
b321803d
DL
14432static
14433u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14434 uint32_t pixel_format)
14435{
14436 u32 gen = INTEL_INFO(dev)->gen;
14437
14438 if (gen >= 9) {
14439 /* "The stride in bytes must not exceed the of the size of 8K
14440 * pixels and 32K bytes."
14441 */
14442 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14443 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14444 return 32*1024;
14445 } else if (gen >= 4) {
14446 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14447 return 16*1024;
14448 else
14449 return 32*1024;
14450 } else if (gen >= 3) {
14451 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14452 return 8*1024;
14453 else
14454 return 16*1024;
14455 } else {
14456 /* XXX DSPC is limited to 4k tiled */
14457 return 8*1024;
14458 }
14459}
14460
b5ea642a
DV
14461static int intel_framebuffer_init(struct drm_device *dev,
14462 struct intel_framebuffer *intel_fb,
14463 struct drm_mode_fb_cmd2 *mode_cmd,
14464 struct drm_i915_gem_object *obj)
79e53945 14465{
6761dd31 14466 unsigned int aligned_height;
79e53945 14467 int ret;
b321803d 14468 u32 pitch_limit, stride_alignment;
79e53945 14469
dd4916c5
DV
14470 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14471
2a80eada
DV
14472 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14473 /* Enforce that fb modifier and tiling mode match, but only for
14474 * X-tiled. This is needed for FBC. */
14475 if (!!(obj->tiling_mode == I915_TILING_X) !=
14476 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14477 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14478 return -EINVAL;
14479 }
14480 } else {
14481 if (obj->tiling_mode == I915_TILING_X)
14482 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14483 else if (obj->tiling_mode == I915_TILING_Y) {
14484 DRM_DEBUG("No Y tiling for legacy addfb\n");
14485 return -EINVAL;
14486 }
14487 }
14488
9a8f0a12
TU
14489 /* Passed in modifier sanity checking. */
14490 switch (mode_cmd->modifier[0]) {
14491 case I915_FORMAT_MOD_Y_TILED:
14492 case I915_FORMAT_MOD_Yf_TILED:
14493 if (INTEL_INFO(dev)->gen < 9) {
14494 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14495 mode_cmd->modifier[0]);
14496 return -EINVAL;
14497 }
14498 case DRM_FORMAT_MOD_NONE:
14499 case I915_FORMAT_MOD_X_TILED:
14500 break;
14501 default:
c0f40428
JB
14502 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14503 mode_cmd->modifier[0]);
57cd6508 14504 return -EINVAL;
c16ed4be 14505 }
57cd6508 14506
b321803d
DL
14507 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14508 mode_cmd->pixel_format);
14509 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14510 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14511 mode_cmd->pitches[0], stride_alignment);
57cd6508 14512 return -EINVAL;
c16ed4be 14513 }
57cd6508 14514
b321803d
DL
14515 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14516 mode_cmd->pixel_format);
a35cdaa0 14517 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14518 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14519 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14520 "tiled" : "linear",
a35cdaa0 14521 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14522 return -EINVAL;
c16ed4be 14523 }
5d7bd705 14524
2a80eada 14525 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14526 mode_cmd->pitches[0] != obj->stride) {
14527 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14528 mode_cmd->pitches[0], obj->stride);
5d7bd705 14529 return -EINVAL;
c16ed4be 14530 }
5d7bd705 14531
57779d06 14532 /* Reject formats not supported by any plane early. */
308e5bcb 14533 switch (mode_cmd->pixel_format) {
57779d06 14534 case DRM_FORMAT_C8:
04b3924d
VS
14535 case DRM_FORMAT_RGB565:
14536 case DRM_FORMAT_XRGB8888:
14537 case DRM_FORMAT_ARGB8888:
57779d06
VS
14538 break;
14539 case DRM_FORMAT_XRGB1555:
c16ed4be 14540 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14541 DRM_DEBUG("unsupported pixel format: %s\n",
14542 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14543 return -EINVAL;
c16ed4be 14544 }
57779d06 14545 break;
57779d06 14546 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14547 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14548 DRM_DEBUG("unsupported pixel format: %s\n",
14549 drm_get_format_name(mode_cmd->pixel_format));
14550 return -EINVAL;
14551 }
14552 break;
14553 case DRM_FORMAT_XBGR8888:
04b3924d 14554 case DRM_FORMAT_XRGB2101010:
57779d06 14555 case DRM_FORMAT_XBGR2101010:
c16ed4be 14556 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14557 DRM_DEBUG("unsupported pixel format: %s\n",
14558 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14559 return -EINVAL;
c16ed4be 14560 }
b5626747 14561 break;
7531208b
DL
14562 case DRM_FORMAT_ABGR2101010:
14563 if (!IS_VALLEYVIEW(dev)) {
14564 DRM_DEBUG("unsupported pixel format: %s\n",
14565 drm_get_format_name(mode_cmd->pixel_format));
14566 return -EINVAL;
14567 }
14568 break;
04b3924d
VS
14569 case DRM_FORMAT_YUYV:
14570 case DRM_FORMAT_UYVY:
14571 case DRM_FORMAT_YVYU:
14572 case DRM_FORMAT_VYUY:
c16ed4be 14573 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14574 DRM_DEBUG("unsupported pixel format: %s\n",
14575 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14576 return -EINVAL;
c16ed4be 14577 }
57cd6508
CW
14578 break;
14579 default:
4ee62c76
VS
14580 DRM_DEBUG("unsupported pixel format: %s\n",
14581 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14582 return -EINVAL;
14583 }
14584
90f9a336
VS
14585 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14586 if (mode_cmd->offsets[0] != 0)
14587 return -EINVAL;
14588
ec2c981e 14589 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14590 mode_cmd->pixel_format,
14591 mode_cmd->modifier[0]);
53155c0a
DV
14592 /* FIXME drm helper for size checks (especially planar formats)? */
14593 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14594 return -EINVAL;
14595
c7d73f6a
DV
14596 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14597 intel_fb->obj = obj;
80075d49 14598 intel_fb->obj->framebuffer_references++;
c7d73f6a 14599
79e53945
JB
14600 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14601 if (ret) {
14602 DRM_ERROR("framebuffer init failed %d\n", ret);
14603 return ret;
14604 }
14605
79e53945
JB
14606 return 0;
14607}
14608
79e53945
JB
14609static struct drm_framebuffer *
14610intel_user_framebuffer_create(struct drm_device *dev,
14611 struct drm_file *filp,
308e5bcb 14612 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14613{
dcb1394e 14614 struct drm_framebuffer *fb;
05394f39 14615 struct drm_i915_gem_object *obj;
79e53945 14616
308e5bcb
JB
14617 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14618 mode_cmd->handles[0]));
c8725226 14619 if (&obj->base == NULL)
cce13ff7 14620 return ERR_PTR(-ENOENT);
79e53945 14621
dcb1394e
LW
14622 fb = intel_framebuffer_create(dev, mode_cmd, obj);
14623 if (IS_ERR(fb))
14624 drm_gem_object_unreference_unlocked(&obj->base);
14625
14626 return fb;
79e53945
JB
14627}
14628
0695726e 14629#ifndef CONFIG_DRM_FBDEV_EMULATION
0632fef6 14630static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14631{
14632}
14633#endif
14634
79e53945 14635static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14636 .fb_create = intel_user_framebuffer_create,
0632fef6 14637 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14638 .atomic_check = intel_atomic_check,
14639 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14640 .atomic_state_alloc = intel_atomic_state_alloc,
14641 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14642};
14643
e70236a8
JB
14644/* Set up chip specific display functions */
14645static void intel_init_display(struct drm_device *dev)
14646{
14647 struct drm_i915_private *dev_priv = dev->dev_private;
14648
ee9300bb
DV
14649 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14650 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14651 else if (IS_CHERRYVIEW(dev))
14652 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14653 else if (IS_VALLEYVIEW(dev))
14654 dev_priv->display.find_dpll = vlv_find_best_dpll;
14655 else if (IS_PINEVIEW(dev))
14656 dev_priv->display.find_dpll = pnv_find_best_dpll;
14657 else
14658 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14659
bc8d7dff
DL
14660 if (INTEL_INFO(dev)->gen >= 9) {
14661 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14662 dev_priv->display.get_initial_plane_config =
14663 skylake_get_initial_plane_config;
bc8d7dff
DL
14664 dev_priv->display.crtc_compute_clock =
14665 haswell_crtc_compute_clock;
14666 dev_priv->display.crtc_enable = haswell_crtc_enable;
14667 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14668 dev_priv->display.update_primary_plane =
14669 skylake_update_primary_plane;
14670 } else if (HAS_DDI(dev)) {
0e8ffe1b 14671 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14672 dev_priv->display.get_initial_plane_config =
14673 ironlake_get_initial_plane_config;
797d0259
ACO
14674 dev_priv->display.crtc_compute_clock =
14675 haswell_crtc_compute_clock;
4f771f10
PZ
14676 dev_priv->display.crtc_enable = haswell_crtc_enable;
14677 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14678 dev_priv->display.update_primary_plane =
14679 ironlake_update_primary_plane;
09b4ddf9 14680 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14681 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14682 dev_priv->display.get_initial_plane_config =
14683 ironlake_get_initial_plane_config;
3fb37703
ACO
14684 dev_priv->display.crtc_compute_clock =
14685 ironlake_crtc_compute_clock;
76e5a89c
DV
14686 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14687 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14688 dev_priv->display.update_primary_plane =
14689 ironlake_update_primary_plane;
89b667f8
JB
14690 } else if (IS_VALLEYVIEW(dev)) {
14691 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14692 dev_priv->display.get_initial_plane_config =
14693 i9xx_get_initial_plane_config;
d6dfee7a 14694 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14695 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14696 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14697 dev_priv->display.update_primary_plane =
14698 i9xx_update_primary_plane;
f564048e 14699 } else {
0e8ffe1b 14700 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14701 dev_priv->display.get_initial_plane_config =
14702 i9xx_get_initial_plane_config;
d6dfee7a 14703 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14704 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14705 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14706 dev_priv->display.update_primary_plane =
14707 i9xx_update_primary_plane;
f564048e 14708 }
e70236a8 14709
e70236a8 14710 /* Returns the core display clock speed */
ef11bdb3 14711 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
1652d19e
VS
14712 dev_priv->display.get_display_clock_speed =
14713 skylake_get_display_clock_speed;
acd3f3d3
BP
14714 else if (IS_BROXTON(dev))
14715 dev_priv->display.get_display_clock_speed =
14716 broxton_get_display_clock_speed;
1652d19e
VS
14717 else if (IS_BROADWELL(dev))
14718 dev_priv->display.get_display_clock_speed =
14719 broadwell_get_display_clock_speed;
14720 else if (IS_HASWELL(dev))
14721 dev_priv->display.get_display_clock_speed =
14722 haswell_get_display_clock_speed;
14723 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14724 dev_priv->display.get_display_clock_speed =
14725 valleyview_get_display_clock_speed;
b37a6434
VS
14726 else if (IS_GEN5(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 ilk_get_display_clock_speed;
a7c66cd8 14729 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14730 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14731 dev_priv->display.get_display_clock_speed =
14732 i945_get_display_clock_speed;
34edce2f
VS
14733 else if (IS_GM45(dev))
14734 dev_priv->display.get_display_clock_speed =
14735 gm45_get_display_clock_speed;
14736 else if (IS_CRESTLINE(dev))
14737 dev_priv->display.get_display_clock_speed =
14738 i965gm_get_display_clock_speed;
14739 else if (IS_PINEVIEW(dev))
14740 dev_priv->display.get_display_clock_speed =
14741 pnv_get_display_clock_speed;
14742 else if (IS_G33(dev) || IS_G4X(dev))
14743 dev_priv->display.get_display_clock_speed =
14744 g33_get_display_clock_speed;
e70236a8
JB
14745 else if (IS_I915G(dev))
14746 dev_priv->display.get_display_clock_speed =
14747 i915_get_display_clock_speed;
257a7ffc 14748 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14749 dev_priv->display.get_display_clock_speed =
14750 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14751 else if (IS_PINEVIEW(dev))
14752 dev_priv->display.get_display_clock_speed =
14753 pnv_get_display_clock_speed;
e70236a8
JB
14754 else if (IS_I915GM(dev))
14755 dev_priv->display.get_display_clock_speed =
14756 i915gm_get_display_clock_speed;
14757 else if (IS_I865G(dev))
14758 dev_priv->display.get_display_clock_speed =
14759 i865_get_display_clock_speed;
f0f8a9ce 14760 else if (IS_I85X(dev))
e70236a8 14761 dev_priv->display.get_display_clock_speed =
1b1d2716 14762 i85x_get_display_clock_speed;
623e01e5
VS
14763 else { /* 830 */
14764 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14765 dev_priv->display.get_display_clock_speed =
14766 i830_get_display_clock_speed;
623e01e5 14767 }
e70236a8 14768
7c10a2b5 14769 if (IS_GEN5(dev)) {
3bb11b53 14770 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14771 } else if (IS_GEN6(dev)) {
14772 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14773 } else if (IS_IVYBRIDGE(dev)) {
14774 /* FIXME: detect B0+ stepping and use auto training */
14775 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14776 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14777 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14778 if (IS_BROADWELL(dev)) {
14779 dev_priv->display.modeset_commit_cdclk =
14780 broadwell_modeset_commit_cdclk;
14781 dev_priv->display.modeset_calc_cdclk =
14782 broadwell_modeset_calc_cdclk;
14783 }
30a970c6 14784 } else if (IS_VALLEYVIEW(dev)) {
27c329ed
ML
14785 dev_priv->display.modeset_commit_cdclk =
14786 valleyview_modeset_commit_cdclk;
14787 dev_priv->display.modeset_calc_cdclk =
14788 valleyview_modeset_calc_cdclk;
f8437dd1 14789 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14790 dev_priv->display.modeset_commit_cdclk =
14791 broxton_modeset_commit_cdclk;
14792 dev_priv->display.modeset_calc_cdclk =
14793 broxton_modeset_calc_cdclk;
e70236a8 14794 }
8c9f3aaf 14795
8c9f3aaf
JB
14796 switch (INTEL_INFO(dev)->gen) {
14797 case 2:
14798 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14799 break;
14800
14801 case 3:
14802 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14803 break;
14804
14805 case 4:
14806 case 5:
14807 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14808 break;
14809
14810 case 6:
14811 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14812 break;
7c9017e5 14813 case 7:
4e0bbc31 14814 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14815 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14816 break;
830c81db 14817 case 9:
ba343e02
TU
14818 /* Drop through - unsupported since execlist only. */
14819 default:
14820 /* Default just returns -ENODEV to indicate unsupported */
14821 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14822 }
7bd688cd 14823
e39b999a 14824 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14825}
14826
b690e96c
JB
14827/*
14828 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14829 * resume, or other times. This quirk makes sure that's the case for
14830 * affected systems.
14831 */
0206e353 14832static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14833{
14834 struct drm_i915_private *dev_priv = dev->dev_private;
14835
14836 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14837 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14838}
14839
b6b5d049
VS
14840static void quirk_pipeb_force(struct drm_device *dev)
14841{
14842 struct drm_i915_private *dev_priv = dev->dev_private;
14843
14844 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14845 DRM_INFO("applying pipe b force quirk\n");
14846}
14847
435793df
KP
14848/*
14849 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14850 */
14851static void quirk_ssc_force_disable(struct drm_device *dev)
14852{
14853 struct drm_i915_private *dev_priv = dev->dev_private;
14854 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14855 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14856}
14857
4dca20ef 14858/*
5a15ab5b
CE
14859 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14860 * brightness value
4dca20ef
CE
14861 */
14862static void quirk_invert_brightness(struct drm_device *dev)
14863{
14864 struct drm_i915_private *dev_priv = dev->dev_private;
14865 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14866 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14867}
14868
9c72cc6f
SD
14869/* Some VBT's incorrectly indicate no backlight is present */
14870static void quirk_backlight_present(struct drm_device *dev)
14871{
14872 struct drm_i915_private *dev_priv = dev->dev_private;
14873 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14874 DRM_INFO("applying backlight present quirk\n");
14875}
14876
b690e96c
JB
14877struct intel_quirk {
14878 int device;
14879 int subsystem_vendor;
14880 int subsystem_device;
14881 void (*hook)(struct drm_device *dev);
14882};
14883
5f85f176
EE
14884/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14885struct intel_dmi_quirk {
14886 void (*hook)(struct drm_device *dev);
14887 const struct dmi_system_id (*dmi_id_list)[];
14888};
14889
14890static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14891{
14892 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14893 return 1;
14894}
14895
14896static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14897 {
14898 .dmi_id_list = &(const struct dmi_system_id[]) {
14899 {
14900 .callback = intel_dmi_reverse_brightness,
14901 .ident = "NCR Corporation",
14902 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14903 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14904 },
14905 },
14906 { } /* terminating entry */
14907 },
14908 .hook = quirk_invert_brightness,
14909 },
14910};
14911
c43b5634 14912static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14913 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14914 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14915
b690e96c
JB
14916 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14917 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14918
5f080c0f
VS
14919 /* 830 needs to leave pipe A & dpll A up */
14920 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14921
b6b5d049
VS
14922 /* 830 needs to leave pipe B & dpll B up */
14923 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14924
435793df
KP
14925 /* Lenovo U160 cannot use SSC on LVDS */
14926 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14927
14928 /* Sony Vaio Y cannot use SSC on LVDS */
14929 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14930
be505f64
AH
14931 /* Acer Aspire 5734Z must invert backlight brightness */
14932 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14933
14934 /* Acer/eMachines G725 */
14935 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14936
14937 /* Acer/eMachines e725 */
14938 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14939
14940 /* Acer/Packard Bell NCL20 */
14941 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14942
14943 /* Acer Aspire 4736Z */
14944 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14945
14946 /* Acer Aspire 5336 */
14947 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14948
14949 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14950 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14951
dfb3d47b
SD
14952 /* Acer C720 Chromebook (Core i3 4005U) */
14953 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14954
b2a9601c 14955 /* Apple Macbook 2,1 (Core 2 T7400) */
14956 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14957
d4967d8c
SD
14958 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14959 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14960
14961 /* HP Chromebook 14 (Celeron 2955U) */
14962 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14963
14964 /* Dell Chromebook 11 */
14965 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14966};
14967
14968static void intel_init_quirks(struct drm_device *dev)
14969{
14970 struct pci_dev *d = dev->pdev;
14971 int i;
14972
14973 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14974 struct intel_quirk *q = &intel_quirks[i];
14975
14976 if (d->device == q->device &&
14977 (d->subsystem_vendor == q->subsystem_vendor ||
14978 q->subsystem_vendor == PCI_ANY_ID) &&
14979 (d->subsystem_device == q->subsystem_device ||
14980 q->subsystem_device == PCI_ANY_ID))
14981 q->hook(dev);
14982 }
5f85f176
EE
14983 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14984 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14985 intel_dmi_quirks[i].hook(dev);
14986 }
b690e96c
JB
14987}
14988
9cce37f4
JB
14989/* Disable the VGA plane that we never use */
14990static void i915_disable_vga(struct drm_device *dev)
14991{
14992 struct drm_i915_private *dev_priv = dev->dev_private;
14993 u8 sr1;
766aa1c4 14994 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14995
2b37c616 14996 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14997 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14998 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14999 sr1 = inb(VGA_SR_DATA);
15000 outb(sr1 | 1<<5, VGA_SR_DATA);
15001 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15002 udelay(300);
15003
01f5a626 15004 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15005 POSTING_READ(vga_reg);
15006}
15007
f817586c
DV
15008void intel_modeset_init_hw(struct drm_device *dev)
15009{
b6283055 15010 intel_update_cdclk(dev);
a8f78b58 15011 intel_prepare_ddi(dev);
f817586c 15012 intel_init_clock_gating(dev);
8090c6b9 15013 intel_enable_gt_powersave(dev);
f817586c
DV
15014}
15015
79e53945
JB
15016void intel_modeset_init(struct drm_device *dev)
15017{
652c393a 15018 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15019 int sprite, ret;
8cc87b75 15020 enum pipe pipe;
46f297fb 15021 struct intel_crtc *crtc;
79e53945
JB
15022
15023 drm_mode_config_init(dev);
15024
15025 dev->mode_config.min_width = 0;
15026 dev->mode_config.min_height = 0;
15027
019d96cb
DA
15028 dev->mode_config.preferred_depth = 24;
15029 dev->mode_config.prefer_shadow = 1;
15030
25bab385
TU
15031 dev->mode_config.allow_fb_modifiers = true;
15032
e6ecefaa 15033 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15034
b690e96c
JB
15035 intel_init_quirks(dev);
15036
1fa61106
ED
15037 intel_init_pm(dev);
15038
e3c74757
BW
15039 if (INTEL_INFO(dev)->num_pipes == 0)
15040 return;
15041
69f92f67
LW
15042 /*
15043 * There may be no VBT; and if the BIOS enabled SSC we can
15044 * just keep using it to avoid unnecessary flicker. Whereas if the
15045 * BIOS isn't using it, don't assume it will work even if the VBT
15046 * indicates as much.
15047 */
15048 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15049 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15050 DREF_SSC1_ENABLE);
15051
15052 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15053 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15054 bios_lvds_use_ssc ? "en" : "dis",
15055 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15056 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15057 }
15058 }
15059
e70236a8 15060 intel_init_display(dev);
7c10a2b5 15061 intel_init_audio(dev);
e70236a8 15062
a6c45cf0
CW
15063 if (IS_GEN2(dev)) {
15064 dev->mode_config.max_width = 2048;
15065 dev->mode_config.max_height = 2048;
15066 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15067 dev->mode_config.max_width = 4096;
15068 dev->mode_config.max_height = 4096;
79e53945 15069 } else {
a6c45cf0
CW
15070 dev->mode_config.max_width = 8192;
15071 dev->mode_config.max_height = 8192;
79e53945 15072 }
068be561 15073
dc41c154
VS
15074 if (IS_845G(dev) || IS_I865G(dev)) {
15075 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15076 dev->mode_config.cursor_height = 1023;
15077 } else if (IS_GEN2(dev)) {
068be561
DL
15078 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15079 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15080 } else {
15081 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15082 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15083 }
15084
5d4545ae 15085 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15086
28c97730 15087 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15088 INTEL_INFO(dev)->num_pipes,
15089 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15090
055e393f 15091 for_each_pipe(dev_priv, pipe) {
8cc87b75 15092 intel_crtc_init(dev, pipe);
3bdcfc0c 15093 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15094 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15095 if (ret)
06da8da2 15096 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15097 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15098 }
79e53945
JB
15099 }
15100
bfa7df01
VS
15101 intel_update_czclk(dev_priv);
15102 intel_update_cdclk(dev);
15103
e72f9fbf 15104 intel_shared_dpll_init(dev);
ee7b9f93 15105
9cce37f4
JB
15106 /* Just disable it once at startup */
15107 i915_disable_vga(dev);
79e53945 15108 intel_setup_outputs(dev);
11be49eb 15109
6e9f798d 15110 drm_modeset_lock_all(dev);
043e9bda 15111 intel_modeset_setup_hw_state(dev);
6e9f798d 15112 drm_modeset_unlock_all(dev);
46f297fb 15113
d3fcc808 15114 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
15115 struct intel_initial_plane_config plane_config = {};
15116
46f297fb
JB
15117 if (!crtc->active)
15118 continue;
15119
46f297fb 15120 /*
46f297fb
JB
15121 * Note that reserving the BIOS fb up front prevents us
15122 * from stuffing other stolen allocations like the ring
15123 * on top. This prevents some ugliness at boot time, and
15124 * can even allow for smooth boot transitions if the BIOS
15125 * fb is large enough for the active pipe configuration.
15126 */
eeebeac5
ML
15127 dev_priv->display.get_initial_plane_config(crtc,
15128 &plane_config);
15129
15130 /*
15131 * If the fb is shared between multiple heads, we'll
15132 * just get the first one.
15133 */
15134 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 15135 }
2c7111db
CW
15136}
15137
7fad798e
DV
15138static void intel_enable_pipe_a(struct drm_device *dev)
15139{
15140 struct intel_connector *connector;
15141 struct drm_connector *crt = NULL;
15142 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15143 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15144
15145 /* We can't just switch on the pipe A, we need to set things up with a
15146 * proper mode and output configuration. As a gross hack, enable pipe A
15147 * by enabling the load detect pipe once. */
3a3371ff 15148 for_each_intel_connector(dev, connector) {
7fad798e
DV
15149 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15150 crt = &connector->base;
15151 break;
15152 }
15153 }
15154
15155 if (!crt)
15156 return;
15157
208bf9fd 15158 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15159 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15160}
15161
fa555837
DV
15162static bool
15163intel_check_plane_mapping(struct intel_crtc *crtc)
15164{
7eb552ae
BW
15165 struct drm_device *dev = crtc->base.dev;
15166 struct drm_i915_private *dev_priv = dev->dev_private;
649636ef 15167 u32 val;
fa555837 15168
7eb552ae 15169 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15170 return true;
15171
649636ef 15172 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
DV
15173
15174 if ((val & DISPLAY_PLANE_ENABLE) &&
15175 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15176 return false;
15177
15178 return true;
15179}
15180
02e93c35
VS
15181static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15182{
15183 struct drm_device *dev = crtc->base.dev;
15184 struct intel_encoder *encoder;
15185
15186 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15187 return true;
15188
15189 return false;
15190}
15191
24929352
DV
15192static void intel_sanitize_crtc(struct intel_crtc *crtc)
15193{
15194 struct drm_device *dev = crtc->base.dev;
15195 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 15196 u32 reg;
24929352 15197
24929352 15198 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15199 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15200 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15201
d3eaf884 15202 /* restore vblank interrupts to correct state */
9625604c 15203 drm_crtc_vblank_reset(&crtc->base);
d297e103 15204 if (crtc->active) {
f9cd7b88
VS
15205 struct intel_plane *plane;
15206
9625604c 15207 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
15208
15209 /* Disable everything but the primary plane */
15210 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15211 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15212 continue;
15213
15214 plane->disable_plane(&plane->base, &crtc->base);
15215 }
9625604c 15216 }
d3eaf884 15217
24929352 15218 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15219 * disable the crtc (and hence change the state) if it is wrong. Note
15220 * that gen4+ has a fixed plane -> pipe mapping. */
15221 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15222 bool plane;
15223
24929352
DV
15224 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15225 crtc->base.base.id);
15226
15227 /* Pipe has the wrong plane attached and the plane is active.
15228 * Temporarily change the plane mapping and disable everything
15229 * ... */
15230 plane = crtc->plane;
b70709a6 15231 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15232 crtc->plane = !plane;
b17d48e2 15233 intel_crtc_disable_noatomic(&crtc->base);
24929352 15234 crtc->plane = plane;
24929352 15235 }
24929352 15236
7fad798e
DV
15237 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15238 crtc->pipe == PIPE_A && !crtc->active) {
15239 /* BIOS forgot to enable pipe A, this mostly happens after
15240 * resume. Force-enable the pipe to fix this, the update_dpms
15241 * call below we restore the pipe to the right state, but leave
15242 * the required bits on. */
15243 intel_enable_pipe_a(dev);
15244 }
15245
24929352
DV
15246 /* Adjust the state of the output pipe according to whether we
15247 * have active connectors/encoders. */
02e93c35 15248 if (!intel_crtc_has_encoders(crtc))
b17d48e2 15249 intel_crtc_disable_noatomic(&crtc->base);
24929352 15250
53d9f4e9 15251 if (crtc->active != crtc->base.state->active) {
02e93c35 15252 struct intel_encoder *encoder;
24929352
DV
15253
15254 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15255 * functions or because of calls to intel_crtc_disable_noatomic,
15256 * or because the pipe is force-enabled due to the
24929352
DV
15257 * pipe A quirk. */
15258 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15259 crtc->base.base.id,
83d65738 15260 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15261 crtc->active ? "enabled" : "disabled");
15262
4be40c98 15263 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
49d6fa21 15264 crtc->base.state->active = crtc->active;
24929352
DV
15265 crtc->base.enabled = crtc->active;
15266
15267 /* Because we only establish the connector -> encoder ->
15268 * crtc links if something is active, this means the
15269 * crtc is now deactivated. Break the links. connector
15270 * -> encoder links are only establish when things are
15271 * actually up, hence no need to break them. */
15272 WARN_ON(crtc->active);
15273
2d406bb0 15274 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
24929352 15275 encoder->base.crtc = NULL;
24929352 15276 }
c5ab3bc0 15277
a3ed6aad 15278 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15279 /*
15280 * We start out with underrun reporting disabled to avoid races.
15281 * For correct bookkeeping mark this on active crtcs.
15282 *
c5ab3bc0
DV
15283 * Also on gmch platforms we dont have any hardware bits to
15284 * disable the underrun reporting. Which means we need to start
15285 * out with underrun reporting disabled also on inactive pipes,
15286 * since otherwise we'll complain about the garbage we read when
15287 * e.g. coming up after runtime pm.
15288 *
4cc31489
DV
15289 * No protection against concurrent access is required - at
15290 * worst a fifo underrun happens which also sets this to false.
15291 */
15292 crtc->cpu_fifo_underrun_disabled = true;
15293 crtc->pch_fifo_underrun_disabled = true;
15294 }
24929352
DV
15295}
15296
15297static void intel_sanitize_encoder(struct intel_encoder *encoder)
15298{
15299 struct intel_connector *connector;
15300 struct drm_device *dev = encoder->base.dev;
873ffe69 15301 bool active = false;
24929352
DV
15302
15303 /* We need to check both for a crtc link (meaning that the
15304 * encoder is active and trying to read from a pipe) and the
15305 * pipe itself being active. */
15306 bool has_active_crtc = encoder->base.crtc &&
15307 to_intel_crtc(encoder->base.crtc)->active;
15308
873ffe69
ML
15309 for_each_intel_connector(dev, connector) {
15310 if (connector->base.encoder != &encoder->base)
15311 continue;
15312
15313 active = true;
15314 break;
15315 }
15316
15317 if (active && !has_active_crtc) {
24929352
DV
15318 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15319 encoder->base.base.id,
8e329a03 15320 encoder->base.name);
24929352
DV
15321
15322 /* Connector is active, but has no active pipe. This is
15323 * fallout from our resume register restoring. Disable
15324 * the encoder manually again. */
15325 if (encoder->base.crtc) {
15326 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15327 encoder->base.base.id,
8e329a03 15328 encoder->base.name);
24929352 15329 encoder->disable(encoder);
a62d1497
VS
15330 if (encoder->post_disable)
15331 encoder->post_disable(encoder);
24929352 15332 }
7f1950fb 15333 encoder->base.crtc = NULL;
24929352
DV
15334
15335 /* Inconsistent output/port/pipe state happens presumably due to
15336 * a bug in one of the get_hw_state functions. Or someplace else
15337 * in our code, like the register restore mess on resume. Clamp
15338 * things to off as a safer default. */
3a3371ff 15339 for_each_intel_connector(dev, connector) {
24929352
DV
15340 if (connector->encoder != encoder)
15341 continue;
7f1950fb
EE
15342 connector->base.dpms = DRM_MODE_DPMS_OFF;
15343 connector->base.encoder = NULL;
24929352
DV
15344 }
15345 }
15346 /* Enabled encoders without active connectors will be fixed in
15347 * the crtc fixup. */
15348}
15349
04098753 15350void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15351{
15352 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15353 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15354
04098753
ID
15355 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15356 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15357 i915_disable_vga(dev);
15358 }
15359}
15360
15361void i915_redisable_vga(struct drm_device *dev)
15362{
15363 struct drm_i915_private *dev_priv = dev->dev_private;
15364
8dc8a27c
PZ
15365 /* This function can be called both from intel_modeset_setup_hw_state or
15366 * at a very early point in our resume sequence, where the power well
15367 * structures are not yet restored. Since this function is at a very
15368 * paranoid "someone might have enabled VGA while we were not looking"
15369 * level, just check if the power well is enabled instead of trying to
15370 * follow the "don't touch the power well if we don't need it" policy
15371 * the rest of the driver uses. */
f458ebbc 15372 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15373 return;
15374
04098753 15375 i915_redisable_vga_power_on(dev);
0fde901f
KM
15376}
15377
f9cd7b88 15378static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 15379{
f9cd7b88 15380 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 15381
f9cd7b88 15382 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
15383}
15384
f9cd7b88
VS
15385/* FIXME read out full plane state for all planes */
15386static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 15387{
b26d3ea3 15388 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 15389 struct intel_plane_state *plane_state =
b26d3ea3 15390 to_intel_plane_state(primary->state);
d032ffa0 15391
19b8d387 15392 plane_state->visible = crtc->active &&
b26d3ea3
ML
15393 primary_get_hw_state(to_intel_plane(primary));
15394
15395 if (plane_state->visible)
15396 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
15397}
15398
30e984df 15399static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15400{
15401 struct drm_i915_private *dev_priv = dev->dev_private;
15402 enum pipe pipe;
24929352
DV
15403 struct intel_crtc *crtc;
15404 struct intel_encoder *encoder;
15405 struct intel_connector *connector;
5358901f 15406 int i;
24929352 15407
d3fcc808 15408 for_each_intel_crtc(dev, crtc) {
b06f8b0d 15409 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
6e3c9717 15410 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15411 crtc->config->base.crtc = &crtc->base;
3b117c8f 15412
0e8ffe1b 15413 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15414 crtc->config);
24929352 15415
49d6fa21 15416 crtc->base.state->active = crtc->active;
24929352 15417 crtc->base.enabled = crtc->active;
b70709a6 15418
f9cd7b88 15419 readout_plane_state(crtc);
24929352
DV
15420
15421 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15422 crtc->base.base.id,
15423 crtc->active ? "enabled" : "disabled");
15424 }
15425
5358901f
DV
15426 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15427 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15428
3e369b76
ACO
15429 pll->on = pll->get_hw_state(dev_priv, pll,
15430 &pll->config.hw_state);
5358901f 15431 pll->active = 0;
3e369b76 15432 pll->config.crtc_mask = 0;
d3fcc808 15433 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15434 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15435 pll->active++;
3e369b76 15436 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15437 }
5358901f 15438 }
5358901f 15439
1e6f2ddc 15440 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15441 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15442
3e369b76 15443 if (pll->config.crtc_mask)
bd2bb1b9 15444 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15445 }
15446
b2784e15 15447 for_each_intel_encoder(dev, encoder) {
24929352
DV
15448 pipe = 0;
15449
15450 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15451 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15452 encoder->base.crtc = &crtc->base;
6e3c9717 15453 encoder->get_config(encoder, crtc->config);
24929352
DV
15454 } else {
15455 encoder->base.crtc = NULL;
15456 }
15457
6f2bcceb 15458 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15459 encoder->base.base.id,
8e329a03 15460 encoder->base.name,
24929352 15461 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15462 pipe_name(pipe));
24929352
DV
15463 }
15464
3a3371ff 15465 for_each_intel_connector(dev, connector) {
24929352
DV
15466 if (connector->get_hw_state(connector)) {
15467 connector->base.dpms = DRM_MODE_DPMS_ON;
24929352
DV
15468 connector->base.encoder = &connector->encoder->base;
15469 } else {
15470 connector->base.dpms = DRM_MODE_DPMS_OFF;
15471 connector->base.encoder = NULL;
15472 }
15473 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15474 connector->base.base.id,
c23cc417 15475 connector->base.name,
24929352
DV
15476 connector->base.encoder ? "enabled" : "disabled");
15477 }
7f4c6284
VS
15478
15479 for_each_intel_crtc(dev, crtc) {
15480 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15481
15482 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15483 if (crtc->base.state->active) {
15484 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15485 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15486 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15487
15488 /*
15489 * The initial mode needs to be set in order to keep
15490 * the atomic core happy. It wants a valid mode if the
15491 * crtc's enabled, so we do the above call.
15492 *
15493 * At this point some state updated by the connectors
15494 * in their ->detect() callback has not run yet, so
15495 * no recalculation can be done yet.
15496 *
15497 * Even if we could do a recalculation and modeset
15498 * right now it would cause a double modeset if
15499 * fbdev or userspace chooses a different initial mode.
15500 *
15501 * If that happens, someone indicated they wanted a
15502 * mode change, which means it's safe to do a full
15503 * recalculation.
15504 */
15505 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
15506
15507 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15508 update_scanline_offset(crtc);
7f4c6284
VS
15509 }
15510 }
30e984df
DV
15511}
15512
043e9bda
ML
15513/* Scan out the current hw modeset state,
15514 * and sanitizes it to the current state
15515 */
15516static void
15517intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df
DV
15518{
15519 struct drm_i915_private *dev_priv = dev->dev_private;
15520 enum pipe pipe;
30e984df
DV
15521 struct intel_crtc *crtc;
15522 struct intel_encoder *encoder;
35c95375 15523 int i;
30e984df
DV
15524
15525 intel_modeset_readout_hw_state(dev);
24929352
DV
15526
15527 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15528 for_each_intel_encoder(dev, encoder) {
24929352
DV
15529 intel_sanitize_encoder(encoder);
15530 }
15531
055e393f 15532 for_each_pipe(dev_priv, pipe) {
24929352
DV
15533 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15534 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15535 intel_dump_pipe_config(crtc, crtc->config,
15536 "[setup_hw_state]");
24929352 15537 }
9a935856 15538
d29b2f9d
ACO
15539 intel_modeset_update_connector_atomic_state(dev);
15540
35c95375
DV
15541 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15542 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15543
15544 if (!pll->on || pll->active)
15545 continue;
15546
15547 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15548
15549 pll->disable(dev_priv, pll);
15550 pll->on = false;
15551 }
15552
26e1fe4f 15553 if (IS_VALLEYVIEW(dev))
6eb1a681
VS
15554 vlv_wm_get_hw_state(dev);
15555 else if (IS_GEN9(dev))
3078999f
PB
15556 skl_wm_get_hw_state(dev);
15557 else if (HAS_PCH_SPLIT(dev))
243e6a44 15558 ilk_wm_get_hw_state(dev);
292b990e
ML
15559
15560 for_each_intel_crtc(dev, crtc) {
15561 unsigned long put_domains;
15562
15563 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15564 if (WARN_ON(put_domains))
15565 modeset_put_power_domains(dev_priv, put_domains);
15566 }
15567 intel_display_set_init_power(dev_priv, false);
043e9bda 15568}
7d0bc1ea 15569
043e9bda
ML
15570void intel_display_resume(struct drm_device *dev)
15571{
15572 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15573 struct intel_connector *conn;
15574 struct intel_plane *plane;
15575 struct drm_crtc *crtc;
15576 int ret;
f30da187 15577
043e9bda
ML
15578 if (!state)
15579 return;
15580
15581 state->acquire_ctx = dev->mode_config.acquire_ctx;
15582
15583 /* preserve complete old state, including dpll */
15584 intel_atomic_get_shared_dpll_state(state);
15585
15586 for_each_crtc(dev, crtc) {
15587 struct drm_crtc_state *crtc_state =
15588 drm_atomic_get_crtc_state(state, crtc);
15589
15590 ret = PTR_ERR_OR_ZERO(crtc_state);
15591 if (ret)
15592 goto err;
15593
15594 /* force a restore */
15595 crtc_state->mode_changed = true;
45e2b5f6 15596 }
8af6cf88 15597
043e9bda
ML
15598 for_each_intel_plane(dev, plane) {
15599 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15600 if (ret)
15601 goto err;
15602 }
15603
15604 for_each_intel_connector(dev, conn) {
15605 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15606 if (ret)
15607 goto err;
15608 }
15609
15610 intel_modeset_setup_hw_state(dev);
15611
15612 i915_redisable_vga(dev);
74c090b1 15613 ret = drm_atomic_commit(state);
043e9bda
ML
15614 if (!ret)
15615 return;
15616
15617err:
15618 DRM_ERROR("Restoring old state failed with %i\n", ret);
15619 drm_atomic_state_free(state);
2c7111db
CW
15620}
15621
15622void intel_modeset_gem_init(struct drm_device *dev)
15623{
484b41dd 15624 struct drm_crtc *c;
2ff8fde1 15625 struct drm_i915_gem_object *obj;
e0d6149b 15626 int ret;
484b41dd 15627
ae48434c
ID
15628 mutex_lock(&dev->struct_mutex);
15629 intel_init_gt_powersave(dev);
15630 mutex_unlock(&dev->struct_mutex);
15631
1833b134 15632 intel_modeset_init_hw(dev);
02e792fb
DV
15633
15634 intel_setup_overlay(dev);
484b41dd
JB
15635
15636 /*
15637 * Make sure any fbs we allocated at startup are properly
15638 * pinned & fenced. When we do the allocation it's too early
15639 * for this.
15640 */
70e1e0ec 15641 for_each_crtc(dev, c) {
2ff8fde1
MR
15642 obj = intel_fb_obj(c->primary->fb);
15643 if (obj == NULL)
484b41dd
JB
15644 continue;
15645
e0d6149b
TU
15646 mutex_lock(&dev->struct_mutex);
15647 ret = intel_pin_and_fence_fb_obj(c->primary,
15648 c->primary->fb,
7580d774 15649 c->primary->state);
e0d6149b
TU
15650 mutex_unlock(&dev->struct_mutex);
15651 if (ret) {
484b41dd
JB
15652 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15653 to_intel_crtc(c)->pipe);
66e514c1
DA
15654 drm_framebuffer_unreference(c->primary->fb);
15655 c->primary->fb = NULL;
36750f28 15656 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15657 update_state_fb(c->primary);
36750f28 15658 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15659 }
15660 }
0962c3c9
VS
15661
15662 intel_backlight_register(dev);
79e53945
JB
15663}
15664
4932e2c3
ID
15665void intel_connector_unregister(struct intel_connector *intel_connector)
15666{
15667 struct drm_connector *connector = &intel_connector->base;
15668
15669 intel_panel_destroy_backlight(connector);
34ea3d38 15670 drm_connector_unregister(connector);
4932e2c3
ID
15671}
15672
79e53945
JB
15673void intel_modeset_cleanup(struct drm_device *dev)
15674{
652c393a 15675 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15676 struct drm_connector *connector;
652c393a 15677
2eb5252e
ID
15678 intel_disable_gt_powersave(dev);
15679
0962c3c9
VS
15680 intel_backlight_unregister(dev);
15681
fd0c0642
DV
15682 /*
15683 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15684 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15685 * experience fancy races otherwise.
15686 */
2aeb7d3a 15687 intel_irq_uninstall(dev_priv);
eb21b92b 15688
fd0c0642
DV
15689 /*
15690 * Due to the hpd irq storm handling the hotplug work can re-arm the
15691 * poll handlers. Hence disable polling after hpd handling is shut down.
15692 */
f87ea761 15693 drm_kms_helper_poll_fini(dev);
fd0c0642 15694
723bfd70
JB
15695 intel_unregister_dsm_handler();
15696
7733b49b 15697 intel_fbc_disable(dev_priv);
69341a5e 15698
1630fe75
CW
15699 /* flush any delayed tasks or pending work */
15700 flush_scheduled_work();
15701
db31af1d
JN
15702 /* destroy the backlight and sysfs files before encoders/connectors */
15703 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15704 struct intel_connector *intel_connector;
15705
15706 intel_connector = to_intel_connector(connector);
15707 intel_connector->unregister(intel_connector);
db31af1d 15708 }
d9255d57 15709
79e53945 15710 drm_mode_config_cleanup(dev);
4d7bb011
DV
15711
15712 intel_cleanup_overlay(dev);
ae48434c
ID
15713
15714 mutex_lock(&dev->struct_mutex);
15715 intel_cleanup_gt_powersave(dev);
15716 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15717}
15718
f1c79df3
ZW
15719/*
15720 * Return which encoder is currently attached for connector.
15721 */
df0e9248 15722struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15723{
df0e9248
CW
15724 return &intel_attached_encoder(connector)->base;
15725}
f1c79df3 15726
df0e9248
CW
15727void intel_connector_attach_encoder(struct intel_connector *connector,
15728 struct intel_encoder *encoder)
15729{
15730 connector->encoder = encoder;
15731 drm_mode_connector_attach_encoder(&connector->base,
15732 &encoder->base);
79e53945 15733}
28d52043
DA
15734
15735/*
15736 * set vga decode state - true == enable VGA decode
15737 */
15738int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15739{
15740 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15741 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15742 u16 gmch_ctrl;
15743
75fa041d
CW
15744 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15745 DRM_ERROR("failed to read control word\n");
15746 return -EIO;
15747 }
15748
c0cc8a55
CW
15749 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15750 return 0;
15751
28d52043
DA
15752 if (state)
15753 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15754 else
15755 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15756
15757 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15758 DRM_ERROR("failed to write control word\n");
15759 return -EIO;
15760 }
15761
28d52043
DA
15762 return 0;
15763}
c4a1d9e4 15764
c4a1d9e4 15765struct intel_display_error_state {
ff57f1b0
PZ
15766
15767 u32 power_well_driver;
15768
63b66e5b
CW
15769 int num_transcoders;
15770
c4a1d9e4
CW
15771 struct intel_cursor_error_state {
15772 u32 control;
15773 u32 position;
15774 u32 base;
15775 u32 size;
52331309 15776 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15777
15778 struct intel_pipe_error_state {
ddf9c536 15779 bool power_domain_on;
c4a1d9e4 15780 u32 source;
f301b1e1 15781 u32 stat;
52331309 15782 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15783
15784 struct intel_plane_error_state {
15785 u32 control;
15786 u32 stride;
15787 u32 size;
15788 u32 pos;
15789 u32 addr;
15790 u32 surface;
15791 u32 tile_offset;
52331309 15792 } plane[I915_MAX_PIPES];
63b66e5b
CW
15793
15794 struct intel_transcoder_error_state {
ddf9c536 15795 bool power_domain_on;
63b66e5b
CW
15796 enum transcoder cpu_transcoder;
15797
15798 u32 conf;
15799
15800 u32 htotal;
15801 u32 hblank;
15802 u32 hsync;
15803 u32 vtotal;
15804 u32 vblank;
15805 u32 vsync;
15806 } transcoder[4];
c4a1d9e4
CW
15807};
15808
15809struct intel_display_error_state *
15810intel_display_capture_error_state(struct drm_device *dev)
15811{
fbee40df 15812 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15813 struct intel_display_error_state *error;
63b66e5b
CW
15814 int transcoders[] = {
15815 TRANSCODER_A,
15816 TRANSCODER_B,
15817 TRANSCODER_C,
15818 TRANSCODER_EDP,
15819 };
c4a1d9e4
CW
15820 int i;
15821
63b66e5b
CW
15822 if (INTEL_INFO(dev)->num_pipes == 0)
15823 return NULL;
15824
9d1cb914 15825 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15826 if (error == NULL)
15827 return NULL;
15828
190be112 15829 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15830 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15831
055e393f 15832 for_each_pipe(dev_priv, i) {
ddf9c536 15833 error->pipe[i].power_domain_on =
f458ebbc
DV
15834 __intel_display_power_is_enabled(dev_priv,
15835 POWER_DOMAIN_PIPE(i));
ddf9c536 15836 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15837 continue;
15838
5efb3e28
VS
15839 error->cursor[i].control = I915_READ(CURCNTR(i));
15840 error->cursor[i].position = I915_READ(CURPOS(i));
15841 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15842
15843 error->plane[i].control = I915_READ(DSPCNTR(i));
15844 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15845 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15846 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15847 error->plane[i].pos = I915_READ(DSPPOS(i));
15848 }
ca291363
PZ
15849 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15850 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15851 if (INTEL_INFO(dev)->gen >= 4) {
15852 error->plane[i].surface = I915_READ(DSPSURF(i));
15853 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15854 }
15855
c4a1d9e4 15856 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15857
3abfce77 15858 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15859 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15860 }
15861
15862 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15863 if (HAS_DDI(dev_priv->dev))
15864 error->num_transcoders++; /* Account for eDP. */
15865
15866 for (i = 0; i < error->num_transcoders; i++) {
15867 enum transcoder cpu_transcoder = transcoders[i];
15868
ddf9c536 15869 error->transcoder[i].power_domain_on =
f458ebbc 15870 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15871 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15872 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15873 continue;
15874
63b66e5b
CW
15875 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15876
15877 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15878 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15879 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15880 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15881 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15882 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15883 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15884 }
15885
15886 return error;
15887}
15888
edc3d884
MK
15889#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15890
c4a1d9e4 15891void
edc3d884 15892intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15893 struct drm_device *dev,
15894 struct intel_display_error_state *error)
15895{
055e393f 15896 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15897 int i;
15898
63b66e5b
CW
15899 if (!error)
15900 return;
15901
edc3d884 15902 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15903 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15904 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15905 error->power_well_driver);
055e393f 15906 for_each_pipe(dev_priv, i) {
edc3d884 15907 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15908 err_printf(m, " Power: %s\n",
15909 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15910 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15911 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15912
15913 err_printf(m, "Plane [%d]:\n", i);
15914 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15915 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15916 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15917 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15918 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15919 }
4b71a570 15920 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15921 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15922 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15923 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15924 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15925 }
15926
edc3d884
MK
15927 err_printf(m, "Cursor [%d]:\n", i);
15928 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15929 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15930 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15931 }
63b66e5b
CW
15932
15933 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15934 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15935 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15936 err_printf(m, " Power: %s\n",
15937 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15938 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15939 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15940 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15941 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15942 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15943 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15944 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15945 }
c4a1d9e4 15946}
e2fcdaa9
VS
15947
15948void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15949{
15950 struct intel_crtc *crtc;
15951
15952 for_each_intel_crtc(dev, crtc) {
15953 struct intel_unpin_work *work;
e2fcdaa9 15954
5e2d7afc 15955 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15956
15957 work = crtc->unpin_work;
15958
15959 if (work && work->event &&
15960 work->event->base.file_priv == file) {
15961 kfree(work->event);
15962 work->event = NULL;
15963 }
15964
5e2d7afc 15965 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15966 }
15967}
This page took 4.038011 seconds and 5 git commands to generate.