drm/i915: don't take runtime PM reference around freeze/thaw
[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"
760285e7
DH
40#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
465c120c
MR
42#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
c0f372b3 44#include <linux/dma_remapping.h>
79e53945 45
465c120c
MR
46/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
3d7d6510
MR
71/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
ef9348c8 76#define DIV_ROUND_CLOSEST_ULL(ll, d) \
465c120c 77({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
ef9348c8 78
3dec0095 79static void intel_increase_pllclock(struct drm_crtc *crtc);
6b383a7f 80static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 81
f1f644dc
JB
82static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
83 struct intel_crtc_config *pipe_config);
18442d08
VS
84static void ironlake_pch_clock_get(struct intel_crtc *crtc,
85 struct intel_crtc_config *pipe_config);
f1f644dc 86
e7457a9a
DL
87static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
88 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
89static int intel_framebuffer_init(struct drm_device *dev,
90 struct intel_framebuffer *ifb,
91 struct drm_mode_fb_cmd2 *mode_cmd,
92 struct drm_i915_gem_object *obj);
5b18e57c
DV
93static void intel_dp_set_m_n(struct intel_crtc *crtc);
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab
DV
96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
97 struct intel_link_m_n *m_n);
98static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
99static void haswell_set_pipeconf(struct drm_crtc *crtc);
100static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 101static void vlv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 102
79e53945 103typedef struct {
0206e353 104 int min, max;
79e53945
JB
105} intel_range_t;
106
107typedef struct {
0206e353
AJ
108 int dot_limit;
109 int p2_slow, p2_fast;
79e53945
JB
110} intel_p2_t;
111
d4906093
ML
112typedef struct intel_limit intel_limit_t;
113struct intel_limit {
0206e353
AJ
114 intel_range_t dot, vco, n, m, m1, m2, p, p1;
115 intel_p2_t p2;
d4906093 116};
79e53945 117
d2acd215
DV
118int
119intel_pch_rawclk(struct drm_device *dev)
120{
121 struct drm_i915_private *dev_priv = dev->dev_private;
122
123 WARN_ON(!HAS_PCH_SPLIT(dev));
124
125 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
126}
127
021357ac
CW
128static inline u32 /* units of 100MHz */
129intel_fdi_link_freq(struct drm_device *dev)
130{
8b99e68c
CW
131 if (IS_GEN5(dev)) {
132 struct drm_i915_private *dev_priv = dev->dev_private;
133 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
134 } else
135 return 27;
021357ac
CW
136}
137
5d536e28 138static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 139 .dot = { .min = 25000, .max = 350000 },
9c333719 140 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 141 .n = { .min = 2, .max = 16 },
0206e353
AJ
142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
149};
150
5d536e28
DV
151static const intel_limit_t intel_limits_i8xx_dvo = {
152 .dot = { .min = 25000, .max = 350000 },
9c333719 153 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 154 .n = { .min = 2, .max = 16 },
5d536e28
DV
155 .m = { .min = 96, .max = 140 },
156 .m1 = { .min = 18, .max = 26 },
157 .m2 = { .min = 6, .max = 16 },
158 .p = { .min = 4, .max = 128 },
159 .p1 = { .min = 2, .max = 33 },
160 .p2 = { .dot_limit = 165000,
161 .p2_slow = 4, .p2_fast = 4 },
162};
163
e4b36699 164static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 165 .dot = { .min = 25000, .max = 350000 },
9c333719 166 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 167 .n = { .min = 2, .max = 16 },
0206e353
AJ
168 .m = { .min = 96, .max = 140 },
169 .m1 = { .min = 18, .max = 26 },
170 .m2 = { .min = 6, .max = 16 },
171 .p = { .min = 4, .max = 128 },
172 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
173 .p2 = { .dot_limit = 165000,
174 .p2_slow = 14, .p2_fast = 7 },
e4b36699 175};
273e27ca 176
e4b36699 177static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
178 .dot = { .min = 20000, .max = 400000 },
179 .vco = { .min = 1400000, .max = 2800000 },
180 .n = { .min = 1, .max = 6 },
181 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
182 .m1 = { .min = 8, .max = 18 },
183 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
184 .p = { .min = 5, .max = 80 },
185 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
186 .p2 = { .dot_limit = 200000,
187 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
188};
189
190static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
191 .dot = { .min = 20000, .max = 400000 },
192 .vco = { .min = 1400000, .max = 2800000 },
193 .n = { .min = 1, .max = 6 },
194 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
195 .m1 = { .min = 8, .max = 18 },
196 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
197 .p = { .min = 7, .max = 98 },
198 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
199 .p2 = { .dot_limit = 112000,
200 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
201};
202
273e27ca 203
e4b36699 204static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
205 .dot = { .min = 25000, .max = 270000 },
206 .vco = { .min = 1750000, .max = 3500000},
207 .n = { .min = 1, .max = 4 },
208 .m = { .min = 104, .max = 138 },
209 .m1 = { .min = 17, .max = 23 },
210 .m2 = { .min = 5, .max = 11 },
211 .p = { .min = 10, .max = 30 },
212 .p1 = { .min = 1, .max = 3},
213 .p2 = { .dot_limit = 270000,
214 .p2_slow = 10,
215 .p2_fast = 10
044c7c41 216 },
e4b36699
KP
217};
218
219static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
220 .dot = { .min = 22000, .max = 400000 },
221 .vco = { .min = 1750000, .max = 3500000},
222 .n = { .min = 1, .max = 4 },
223 .m = { .min = 104, .max = 138 },
224 .m1 = { .min = 16, .max = 23 },
225 .m2 = { .min = 5, .max = 11 },
226 .p = { .min = 5, .max = 80 },
227 .p1 = { .min = 1, .max = 8},
228 .p2 = { .dot_limit = 165000,
229 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
230};
231
232static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
233 .dot = { .min = 20000, .max = 115000 },
234 .vco = { .min = 1750000, .max = 3500000 },
235 .n = { .min = 1, .max = 3 },
236 .m = { .min = 104, .max = 138 },
237 .m1 = { .min = 17, .max = 23 },
238 .m2 = { .min = 5, .max = 11 },
239 .p = { .min = 28, .max = 112 },
240 .p1 = { .min = 2, .max = 8 },
241 .p2 = { .dot_limit = 0,
242 .p2_slow = 14, .p2_fast = 14
044c7c41 243 },
e4b36699
KP
244};
245
246static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
247 .dot = { .min = 80000, .max = 224000 },
248 .vco = { .min = 1750000, .max = 3500000 },
249 .n = { .min = 1, .max = 3 },
250 .m = { .min = 104, .max = 138 },
251 .m1 = { .min = 17, .max = 23 },
252 .m2 = { .min = 5, .max = 11 },
253 .p = { .min = 14, .max = 42 },
254 .p1 = { .min = 2, .max = 6 },
255 .p2 = { .dot_limit = 0,
256 .p2_slow = 7, .p2_fast = 7
044c7c41 257 },
e4b36699
KP
258};
259
f2b115e6 260static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
261 .dot = { .min = 20000, .max = 400000},
262 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 263 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
264 .n = { .min = 3, .max = 6 },
265 .m = { .min = 2, .max = 256 },
273e27ca 266 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
267 .m1 = { .min = 0, .max = 0 },
268 .m2 = { .min = 0, .max = 254 },
269 .p = { .min = 5, .max = 80 },
270 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
271 .p2 = { .dot_limit = 200000,
272 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
273};
274
f2b115e6 275static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
276 .dot = { .min = 20000, .max = 400000 },
277 .vco = { .min = 1700000, .max = 3500000 },
278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
280 .m1 = { .min = 0, .max = 0 },
281 .m2 = { .min = 0, .max = 254 },
282 .p = { .min = 7, .max = 112 },
283 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
284 .p2 = { .dot_limit = 112000,
285 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
286};
287
273e27ca
EA
288/* Ironlake / Sandybridge
289 *
290 * We calculate clock using (register_value + 2) for N/M1/M2, so here
291 * the range value for them is (actual_value - 2).
292 */
b91ad0ec 293static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
294 .dot = { .min = 25000, .max = 350000 },
295 .vco = { .min = 1760000, .max = 3510000 },
296 .n = { .min = 1, .max = 5 },
297 .m = { .min = 79, .max = 127 },
298 .m1 = { .min = 12, .max = 22 },
299 .m2 = { .min = 5, .max = 9 },
300 .p = { .min = 5, .max = 80 },
301 .p1 = { .min = 1, .max = 8 },
302 .p2 = { .dot_limit = 225000,
303 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
304};
305
b91ad0ec 306static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
307 .dot = { .min = 25000, .max = 350000 },
308 .vco = { .min = 1760000, .max = 3510000 },
309 .n = { .min = 1, .max = 3 },
310 .m = { .min = 79, .max = 118 },
311 .m1 = { .min = 12, .max = 22 },
312 .m2 = { .min = 5, .max = 9 },
313 .p = { .min = 28, .max = 112 },
314 .p1 = { .min = 2, .max = 8 },
315 .p2 = { .dot_limit = 225000,
316 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
317};
318
319static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
320 .dot = { .min = 25000, .max = 350000 },
321 .vco = { .min = 1760000, .max = 3510000 },
322 .n = { .min = 1, .max = 3 },
323 .m = { .min = 79, .max = 127 },
324 .m1 = { .min = 12, .max = 22 },
325 .m2 = { .min = 5, .max = 9 },
326 .p = { .min = 14, .max = 56 },
327 .p1 = { .min = 2, .max = 8 },
328 .p2 = { .dot_limit = 225000,
329 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
330};
331
273e27ca 332/* LVDS 100mhz refclk limits. */
b91ad0ec 333static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
0206e353 341 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
344};
345
346static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
347 .dot = { .min = 25000, .max = 350000 },
348 .vco = { .min = 1760000, .max = 3510000 },
349 .n = { .min = 1, .max = 3 },
350 .m = { .min = 79, .max = 126 },
351 .m1 = { .min = 12, .max = 22 },
352 .m2 = { .min = 5, .max = 9 },
353 .p = { .min = 14, .max = 42 },
0206e353 354 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
355 .p2 = { .dot_limit = 225000,
356 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
357};
358
dc730512 359static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
360 /*
361 * These are the data rate limits (measured in fast clocks)
362 * since those are the strictest limits we have. The fast
363 * clock and actual rate limits are more relaxed, so checking
364 * them would make no difference.
365 */
366 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 367 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 368 .n = { .min = 1, .max = 7 },
a0c4da24
JB
369 .m1 = { .min = 2, .max = 3 },
370 .m2 = { .min = 11, .max = 156 },
b99ab663 371 .p1 = { .min = 2, .max = 3 },
5fdc9c49 372 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
373};
374
ef9348c8
CML
375static const intel_limit_t intel_limits_chv = {
376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 540000 * 5},
383 .vco = { .min = 4860000, .max = 6700000 },
384 .n = { .min = 1, .max = 1 },
385 .m1 = { .min = 2, .max = 2 },
386 .m2 = { .min = 24 << 22, .max = 175 << 22 },
387 .p1 = { .min = 2, .max = 4 },
388 .p2 = { .p2_slow = 1, .p2_fast = 14 },
389};
390
6b4bf1c4
VS
391static void vlv_clock(int refclk, intel_clock_t *clock)
392{
393 clock->m = clock->m1 * clock->m2;
394 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
395 if (WARN_ON(clock->n == 0 || clock->p == 0))
396 return;
fb03ac01
VS
397 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
398 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
399}
400
e0638cdf
PZ
401/**
402 * Returns whether any output on the specified pipe is of the specified type
403 */
404static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
405{
406 struct drm_device *dev = crtc->dev;
407 struct intel_encoder *encoder;
408
409 for_each_encoder_on_crtc(dev, crtc, encoder)
410 if (encoder->type == type)
411 return true;
412
413 return false;
414}
415
1b894b59
CW
416static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
417 int refclk)
2c07245f 418{
b91ad0ec 419 struct drm_device *dev = crtc->dev;
2c07245f 420 const intel_limit_t *limit;
b91ad0ec
ZW
421
422 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 423 if (intel_is_dual_link_lvds(dev)) {
1b894b59 424 if (refclk == 100000)
b91ad0ec
ZW
425 limit = &intel_limits_ironlake_dual_lvds_100m;
426 else
427 limit = &intel_limits_ironlake_dual_lvds;
428 } else {
1b894b59 429 if (refclk == 100000)
b91ad0ec
ZW
430 limit = &intel_limits_ironlake_single_lvds_100m;
431 else
432 limit = &intel_limits_ironlake_single_lvds;
433 }
c6bb3538 434 } else
b91ad0ec 435 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
436
437 return limit;
438}
439
044c7c41
ML
440static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
441{
442 struct drm_device *dev = crtc->dev;
044c7c41
ML
443 const intel_limit_t *limit;
444
445 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 446 if (intel_is_dual_link_lvds(dev))
e4b36699 447 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 448 else
e4b36699 449 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
450 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
451 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 452 limit = &intel_limits_g4x_hdmi;
044c7c41 453 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 454 limit = &intel_limits_g4x_sdvo;
044c7c41 455 } else /* The option is for other outputs */
e4b36699 456 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
457
458 return limit;
459}
460
1b894b59 461static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
462{
463 struct drm_device *dev = crtc->dev;
464 const intel_limit_t *limit;
465
bad720ff 466 if (HAS_PCH_SPLIT(dev))
1b894b59 467 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 468 else if (IS_G4X(dev)) {
044c7c41 469 limit = intel_g4x_limit(crtc);
f2b115e6 470 } else if (IS_PINEVIEW(dev)) {
2177832f 471 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 472 limit = &intel_limits_pineview_lvds;
2177832f 473 else
f2b115e6 474 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
475 } else if (IS_CHERRYVIEW(dev)) {
476 limit = &intel_limits_chv;
a0c4da24 477 } else if (IS_VALLEYVIEW(dev)) {
dc730512 478 limit = &intel_limits_vlv;
a6c45cf0
CW
479 } else if (!IS_GEN2(dev)) {
480 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
481 limit = &intel_limits_i9xx_lvds;
482 else
483 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
484 } else {
485 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 486 limit = &intel_limits_i8xx_lvds;
5d536e28 487 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 488 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
489 else
490 limit = &intel_limits_i8xx_dac;
79e53945
JB
491 }
492 return limit;
493}
494
f2b115e6
AJ
495/* m1 is reserved as 0 in Pineview, n is a ring counter */
496static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 497{
2177832f
SL
498 clock->m = clock->m2 + 2;
499 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
500 if (WARN_ON(clock->n == 0 || clock->p == 0))
501 return;
fb03ac01
VS
502 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
503 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
504}
505
7429e9d4
DV
506static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
507{
508 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
509}
510
ac58c3f0 511static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 512{
7429e9d4 513 clock->m = i9xx_dpll_compute_m(clock);
79e53945 514 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
515 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
516 return;
fb03ac01
VS
517 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
518 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
519}
520
ef9348c8
CML
521static void chv_clock(int refclk, intel_clock_t *clock)
522{
523 clock->m = clock->m1 * clock->m2;
524 clock->p = clock->p1 * clock->p2;
525 if (WARN_ON(clock->n == 0 || clock->p == 0))
526 return;
527 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
528 clock->n << 22);
529 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
530}
531
7c04d1d9 532#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
533/**
534 * Returns whether the given set of divisors are valid for a given refclk with
535 * the given connectors.
536 */
537
1b894b59
CW
538static bool intel_PLL_is_valid(struct drm_device *dev,
539 const intel_limit_t *limit,
540 const intel_clock_t *clock)
79e53945 541{
f01b7962
VS
542 if (clock->n < limit->n.min || limit->n.max < clock->n)
543 INTELPllInvalid("n out of range\n");
79e53945 544 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 545 INTELPllInvalid("p1 out of range\n");
79e53945 546 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 547 INTELPllInvalid("m2 out of range\n");
79e53945 548 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 549 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
550
551 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
552 if (clock->m1 <= clock->m2)
553 INTELPllInvalid("m1 <= m2\n");
554
555 if (!IS_VALLEYVIEW(dev)) {
556 if (clock->p < limit->p.min || limit->p.max < clock->p)
557 INTELPllInvalid("p out of range\n");
558 if (clock->m < limit->m.min || limit->m.max < clock->m)
559 INTELPllInvalid("m out of range\n");
560 }
561
79e53945 562 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 563 INTELPllInvalid("vco out of range\n");
79e53945
JB
564 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
565 * connector, etc., rather than just a single range.
566 */
567 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 568 INTELPllInvalid("dot out of range\n");
79e53945
JB
569
570 return true;
571}
572
d4906093 573static bool
ee9300bb 574i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
575 int target, int refclk, intel_clock_t *match_clock,
576 intel_clock_t *best_clock)
79e53945
JB
577{
578 struct drm_device *dev = crtc->dev;
79e53945 579 intel_clock_t clock;
79e53945
JB
580 int err = target;
581
a210b028 582 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 583 /*
a210b028
DV
584 * For LVDS just rely on its current settings for dual-channel.
585 * We haven't figured out how to reliably set up different
586 * single/dual channel state, if we even can.
79e53945 587 */
1974cad0 588 if (intel_is_dual_link_lvds(dev))
79e53945
JB
589 clock.p2 = limit->p2.p2_fast;
590 else
591 clock.p2 = limit->p2.p2_slow;
592 } else {
593 if (target < limit->p2.dot_limit)
594 clock.p2 = limit->p2.p2_slow;
595 else
596 clock.p2 = limit->p2.p2_fast;
597 }
598
0206e353 599 memset(best_clock, 0, sizeof(*best_clock));
79e53945 600
42158660
ZY
601 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
602 clock.m1++) {
603 for (clock.m2 = limit->m2.min;
604 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 605 if (clock.m2 >= clock.m1)
42158660
ZY
606 break;
607 for (clock.n = limit->n.min;
608 clock.n <= limit->n.max; clock.n++) {
609 for (clock.p1 = limit->p1.min;
610 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
611 int this_err;
612
ac58c3f0
DV
613 i9xx_clock(refclk, &clock);
614 if (!intel_PLL_is_valid(dev, limit,
615 &clock))
616 continue;
617 if (match_clock &&
618 clock.p != match_clock->p)
619 continue;
620
621 this_err = abs(clock.dot - target);
622 if (this_err < err) {
623 *best_clock = clock;
624 err = this_err;
625 }
626 }
627 }
628 }
629 }
630
631 return (err != target);
632}
633
634static bool
ee9300bb
DV
635pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
636 int target, int refclk, intel_clock_t *match_clock,
637 intel_clock_t *best_clock)
79e53945
JB
638{
639 struct drm_device *dev = crtc->dev;
79e53945 640 intel_clock_t clock;
79e53945
JB
641 int err = target;
642
a210b028 643 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 644 /*
a210b028
DV
645 * For LVDS just rely on its current settings for dual-channel.
646 * We haven't figured out how to reliably set up different
647 * single/dual channel state, if we even can.
79e53945 648 */
1974cad0 649 if (intel_is_dual_link_lvds(dev))
79e53945
JB
650 clock.p2 = limit->p2.p2_fast;
651 else
652 clock.p2 = limit->p2.p2_slow;
653 } else {
654 if (target < limit->p2.dot_limit)
655 clock.p2 = limit->p2.p2_slow;
656 else
657 clock.p2 = limit->p2.p2_fast;
658 }
659
0206e353 660 memset(best_clock, 0, sizeof(*best_clock));
79e53945 661
42158660
ZY
662 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
663 clock.m1++) {
664 for (clock.m2 = limit->m2.min;
665 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
666 for (clock.n = limit->n.min;
667 clock.n <= limit->n.max; clock.n++) {
668 for (clock.p1 = limit->p1.min;
669 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
670 int this_err;
671
ac58c3f0 672 pineview_clock(refclk, &clock);
1b894b59
CW
673 if (!intel_PLL_is_valid(dev, limit,
674 &clock))
79e53945 675 continue;
cec2f356
SP
676 if (match_clock &&
677 clock.p != match_clock->p)
678 continue;
79e53945
JB
679
680 this_err = abs(clock.dot - target);
681 if (this_err < err) {
682 *best_clock = clock;
683 err = this_err;
684 }
685 }
686 }
687 }
688 }
689
690 return (err != target);
691}
692
d4906093 693static bool
ee9300bb
DV
694g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
695 int target, int refclk, intel_clock_t *match_clock,
696 intel_clock_t *best_clock)
d4906093
ML
697{
698 struct drm_device *dev = crtc->dev;
d4906093
ML
699 intel_clock_t clock;
700 int max_n;
701 bool found;
6ba770dc
AJ
702 /* approximately equals target * 0.00585 */
703 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
704 found = false;
705
706 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 707 if (intel_is_dual_link_lvds(dev))
d4906093
ML
708 clock.p2 = limit->p2.p2_fast;
709 else
710 clock.p2 = limit->p2.p2_slow;
711 } else {
712 if (target < limit->p2.dot_limit)
713 clock.p2 = limit->p2.p2_slow;
714 else
715 clock.p2 = limit->p2.p2_fast;
716 }
717
718 memset(best_clock, 0, sizeof(*best_clock));
719 max_n = limit->n.max;
f77f13e2 720 /* based on hardware requirement, prefer smaller n to precision */
d4906093 721 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 722 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
723 for (clock.m1 = limit->m1.max;
724 clock.m1 >= limit->m1.min; clock.m1--) {
725 for (clock.m2 = limit->m2.max;
726 clock.m2 >= limit->m2.min; clock.m2--) {
727 for (clock.p1 = limit->p1.max;
728 clock.p1 >= limit->p1.min; clock.p1--) {
729 int this_err;
730
ac58c3f0 731 i9xx_clock(refclk, &clock);
1b894b59
CW
732 if (!intel_PLL_is_valid(dev, limit,
733 &clock))
d4906093 734 continue;
1b894b59
CW
735
736 this_err = abs(clock.dot - target);
d4906093
ML
737 if (this_err < err_most) {
738 *best_clock = clock;
739 err_most = this_err;
740 max_n = clock.n;
741 found = true;
742 }
743 }
744 }
745 }
746 }
2c07245f
ZW
747 return found;
748}
749
a0c4da24 750static bool
ee9300bb
DV
751vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
752 int target, int refclk, intel_clock_t *match_clock,
753 intel_clock_t *best_clock)
a0c4da24 754{
f01b7962 755 struct drm_device *dev = crtc->dev;
6b4bf1c4 756 intel_clock_t clock;
69e4f900 757 unsigned int bestppm = 1000000;
27e639bf
VS
758 /* min update 19.2 MHz */
759 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 760 bool found = false;
a0c4da24 761
6b4bf1c4
VS
762 target *= 5; /* fast clock */
763
764 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
765
766 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 767 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 768 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 769 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 770 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 771 clock.p = clock.p1 * clock.p2;
a0c4da24 772 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 773 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
774 unsigned int ppm, diff;
775
6b4bf1c4
VS
776 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
777 refclk * clock.m1);
778
779 vlv_clock(refclk, &clock);
43b0ac53 780
f01b7962
VS
781 if (!intel_PLL_is_valid(dev, limit,
782 &clock))
43b0ac53
VS
783 continue;
784
6b4bf1c4
VS
785 diff = abs(clock.dot - target);
786 ppm = div_u64(1000000ULL * diff, target);
787
788 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 789 bestppm = 0;
6b4bf1c4 790 *best_clock = clock;
49e497ef 791 found = true;
43b0ac53 792 }
6b4bf1c4 793
c686122c 794 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 795 bestppm = ppm;
6b4bf1c4 796 *best_clock = clock;
49e497ef 797 found = true;
a0c4da24
JB
798 }
799 }
800 }
801 }
802 }
a0c4da24 803
49e497ef 804 return found;
a0c4da24 805}
a4fc5ed6 806
ef9348c8
CML
807static bool
808chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
811{
812 struct drm_device *dev = crtc->dev;
813 intel_clock_t clock;
814 uint64_t m2;
815 int found = false;
816
817 memset(best_clock, 0, sizeof(*best_clock));
818
819 /*
820 * Based on hardware doc, the n always set to 1, and m1 always
821 * set to 2. If requires to support 200Mhz refclk, we need to
822 * revisit this because n may not 1 anymore.
823 */
824 clock.n = 1, clock.m1 = 2;
825 target *= 5; /* fast clock */
826
827 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
828 for (clock.p2 = limit->p2.p2_fast;
829 clock.p2 >= limit->p2.p2_slow;
830 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
831
832 clock.p = clock.p1 * clock.p2;
833
834 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
835 clock.n) << 22, refclk * clock.m1);
836
837 if (m2 > INT_MAX/clock.m1)
838 continue;
839
840 clock.m2 = m2;
841
842 chv_clock(refclk, &clock);
843
844 if (!intel_PLL_is_valid(dev, limit, &clock))
845 continue;
846
847 /* based on hardware requirement, prefer bigger p
848 */
849 if (clock.p > best_clock->p) {
850 *best_clock = clock;
851 found = true;
852 }
853 }
854 }
855
856 return found;
857}
858
20ddf665
VS
859bool intel_crtc_active(struct drm_crtc *crtc)
860{
861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
862
863 /* Be paranoid as we can arrive here with only partial
864 * state retrieved from the hardware during setup.
865 *
241bfc38 866 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
867 * as Haswell has gained clock readout/fastboot support.
868 *
66e514c1 869 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
870 * properly reconstruct framebuffers.
871 */
f4510a27 872 return intel_crtc->active && crtc->primary->fb &&
241bfc38 873 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
874}
875
a5c961d1
PZ
876enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
877 enum pipe pipe)
878{
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
881
3b117c8f 882 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
883}
884
57e22f4a 885static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
886{
887 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 888 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
889
890 frame = I915_READ(frame_reg);
891
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
93937071 893 WARN(1, "vblank wait timed out\n");
a928d536
PZ
894}
895
9d0498a2
JB
896/**
897 * intel_wait_for_vblank - wait for vblank on a given pipe
898 * @dev: drm device
899 * @pipe: pipe to wait for
900 *
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
902 * mode setting code.
903 */
904void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 905{
9d0498a2 906 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 907 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 908
57e22f4a
VS
909 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
910 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
911 return;
912 }
913
300387c0
CW
914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
916 *
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
923 * vblanks...
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
926 */
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
929
9d0498a2 930 /* Wait for vblank interrupt bit to set */
481b6af3
CW
931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
933 50))
9d0498a2
JB
934 DRM_DEBUG_KMS("vblank wait timed out\n");
935}
936
fbf49ea2
VS
937static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
938{
939 struct drm_i915_private *dev_priv = dev->dev_private;
940 u32 reg = PIPEDSL(pipe);
941 u32 line1, line2;
942 u32 line_mask;
943
944 if (IS_GEN2(dev))
945 line_mask = DSL_LINEMASK_GEN2;
946 else
947 line_mask = DSL_LINEMASK_GEN3;
948
949 line1 = I915_READ(reg) & line_mask;
950 mdelay(5);
951 line2 = I915_READ(reg) & line_mask;
952
953 return line1 == line2;
954}
955
ab7ad7f6
KP
956/*
957 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
958 * @dev: drm device
959 * @pipe: pipe to wait for
960 *
961 * After disabling a pipe, we can't wait for vblank in the usual way,
962 * spinning on the vblank interrupt status bit, since we won't actually
963 * see an interrupt when the pipe is disabled.
964 *
ab7ad7f6
KP
965 * On Gen4 and above:
966 * wait for the pipe register state bit to turn off
967 *
968 * Otherwise:
969 * wait for the display line value to settle (it usually
970 * ends up stopping at the start of the next frame).
58e10eb9 971 *
9d0498a2 972 */
58e10eb9 973void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
974{
975 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
976 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
977 pipe);
ab7ad7f6
KP
978
979 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 980 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
981
982 /* Wait for the Pipe State to go off */
58e10eb9
CW
983 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
984 100))
284637d9 985 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 986 } else {
ab7ad7f6 987 /* Wait for the display line to settle */
fbf49ea2 988 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 989 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 990 }
79e53945
JB
991}
992
b0ea7d37
DL
993/*
994 * ibx_digital_port_connected - is the specified port connected?
995 * @dev_priv: i915 private structure
996 * @port: the port to test
997 *
998 * Returns true if @port is connected, false otherwise.
999 */
1000bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1001 struct intel_digital_port *port)
1002{
1003 u32 bit;
1004
c36346e3 1005 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1006 switch (port->port) {
c36346e3
DL
1007 case PORT_B:
1008 bit = SDE_PORTB_HOTPLUG;
1009 break;
1010 case PORT_C:
1011 bit = SDE_PORTC_HOTPLUG;
1012 break;
1013 case PORT_D:
1014 bit = SDE_PORTD_HOTPLUG;
1015 break;
1016 default:
1017 return true;
1018 }
1019 } else {
eba905b2 1020 switch (port->port) {
c36346e3
DL
1021 case PORT_B:
1022 bit = SDE_PORTB_HOTPLUG_CPT;
1023 break;
1024 case PORT_C:
1025 bit = SDE_PORTC_HOTPLUG_CPT;
1026 break;
1027 case PORT_D:
1028 bit = SDE_PORTD_HOTPLUG_CPT;
1029 break;
1030 default:
1031 return true;
1032 }
b0ea7d37
DL
1033 }
1034
1035 return I915_READ(SDEISR) & bit;
1036}
1037
b24e7179
JB
1038static const char *state_string(bool enabled)
1039{
1040 return enabled ? "on" : "off";
1041}
1042
1043/* Only for pre-ILK configs */
55607e8a
DV
1044void assert_pll(struct drm_i915_private *dev_priv,
1045 enum pipe pipe, bool state)
b24e7179
JB
1046{
1047 int reg;
1048 u32 val;
1049 bool cur_state;
1050
1051 reg = DPLL(pipe);
1052 val = I915_READ(reg);
1053 cur_state = !!(val & DPLL_VCO_ENABLE);
1054 WARN(cur_state != state,
1055 "PLL state assertion failure (expected %s, current %s)\n",
1056 state_string(state), state_string(cur_state));
1057}
b24e7179 1058
23538ef1
JN
1059/* XXX: the dsi pll is shared between MIPI DSI ports */
1060static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1061{
1062 u32 val;
1063 bool cur_state;
1064
1065 mutex_lock(&dev_priv->dpio_lock);
1066 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1067 mutex_unlock(&dev_priv->dpio_lock);
1068
1069 cur_state = val & DSI_PLL_VCO_EN;
1070 WARN(cur_state != state,
1071 "DSI PLL state assertion failure (expected %s, current %s)\n",
1072 state_string(state), state_string(cur_state));
1073}
1074#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1075#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1076
55607e8a 1077struct intel_shared_dpll *
e2b78267
DV
1078intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1079{
1080 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1081
a43f6e0f 1082 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1083 return NULL;
1084
a43f6e0f 1085 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1086}
1087
040484af 1088/* For ILK+ */
55607e8a
DV
1089void assert_shared_dpll(struct drm_i915_private *dev_priv,
1090 struct intel_shared_dpll *pll,
1091 bool state)
040484af 1092{
040484af 1093 bool cur_state;
5358901f 1094 struct intel_dpll_hw_state hw_state;
040484af 1095
9d82aa17
ED
1096 if (HAS_PCH_LPT(dev_priv->dev)) {
1097 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1098 return;
1099 }
1100
92b27b08 1101 if (WARN (!pll,
46edb027 1102 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1103 return;
ee7b9f93 1104
5358901f 1105 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1106 WARN(cur_state != state,
5358901f
DV
1107 "%s assertion failure (expected %s, current %s)\n",
1108 pll->name, state_string(state), state_string(cur_state));
040484af 1109}
040484af
JB
1110
1111static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1112 enum pipe pipe, bool state)
1113{
1114 int reg;
1115 u32 val;
1116 bool cur_state;
ad80a810
PZ
1117 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1118 pipe);
040484af 1119
affa9354
PZ
1120 if (HAS_DDI(dev_priv->dev)) {
1121 /* DDI does not have a specific FDI_TX register */
ad80a810 1122 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1123 val = I915_READ(reg);
ad80a810 1124 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1125 } else {
1126 reg = FDI_TX_CTL(pipe);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & FDI_TX_ENABLE);
1129 }
040484af
JB
1130 WARN(cur_state != state,
1131 "FDI TX state assertion failure (expected %s, current %s)\n",
1132 state_string(state), state_string(cur_state));
1133}
1134#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1135#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1136
1137static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1138 enum pipe pipe, bool state)
1139{
1140 int reg;
1141 u32 val;
1142 bool cur_state;
1143
d63fa0dc
PZ
1144 reg = FDI_RX_CTL(pipe);
1145 val = I915_READ(reg);
1146 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1147 WARN(cur_state != state,
1148 "FDI RX state assertion failure (expected %s, current %s)\n",
1149 state_string(state), state_string(cur_state));
1150}
1151#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1152#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1153
1154static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1155 enum pipe pipe)
1156{
1157 int reg;
1158 u32 val;
1159
1160 /* ILK FDI PLL is always enabled */
3d13ef2e 1161 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1162 return;
1163
bf507ef7 1164 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1165 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1166 return;
1167
040484af
JB
1168 reg = FDI_TX_CTL(pipe);
1169 val = I915_READ(reg);
1170 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1171}
1172
55607e8a
DV
1173void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1174 enum pipe pipe, bool state)
040484af
JB
1175{
1176 int reg;
1177 u32 val;
55607e8a 1178 bool cur_state;
040484af
JB
1179
1180 reg = FDI_RX_CTL(pipe);
1181 val = I915_READ(reg);
55607e8a
DV
1182 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1183 WARN(cur_state != state,
1184 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1185 state_string(state), state_string(cur_state));
040484af
JB
1186}
1187
ea0760cf
JB
1188static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1189 enum pipe pipe)
1190{
1191 int pp_reg, lvds_reg;
1192 u32 val;
1193 enum pipe panel_pipe = PIPE_A;
0de3b485 1194 bool locked = true;
ea0760cf
JB
1195
1196 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1197 pp_reg = PCH_PP_CONTROL;
1198 lvds_reg = PCH_LVDS;
1199 } else {
1200 pp_reg = PP_CONTROL;
1201 lvds_reg = LVDS;
1202 }
1203
1204 val = I915_READ(pp_reg);
1205 if (!(val & PANEL_POWER_ON) ||
1206 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1207 locked = false;
1208
1209 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211
1212 WARN(panel_pipe == pipe && locked,
1213 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1214 pipe_name(pipe));
ea0760cf
JB
1215}
1216
93ce0ba6
JN
1217static void assert_cursor(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
1219{
1220 struct drm_device *dev = dev_priv->dev;
1221 bool cur_state;
1222
d9d82081 1223 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1224 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1225 else
5efb3e28 1226 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1227
1228 WARN(cur_state != state,
1229 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1230 pipe_name(pipe), state_string(state), state_string(cur_state));
1231}
1232#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1233#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1234
b840d907
JB
1235void assert_pipe(struct drm_i915_private *dev_priv,
1236 enum pipe pipe, bool state)
b24e7179
JB
1237{
1238 int reg;
1239 u32 val;
63d7bbe9 1240 bool cur_state;
702e7a56
PZ
1241 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1242 pipe);
b24e7179 1243
8e636784
DV
1244 /* if we need the pipe A quirk it must be always on */
1245 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1246 state = true;
1247
da7e29bd 1248 if (!intel_display_power_enabled(dev_priv,
b97186f0 1249 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1250 cur_state = false;
1251 } else {
1252 reg = PIPECONF(cpu_transcoder);
1253 val = I915_READ(reg);
1254 cur_state = !!(val & PIPECONF_ENABLE);
1255 }
1256
63d7bbe9
JB
1257 WARN(cur_state != state,
1258 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1259 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1260}
1261
931872fc
CW
1262static void assert_plane(struct drm_i915_private *dev_priv,
1263 enum plane plane, bool state)
b24e7179
JB
1264{
1265 int reg;
1266 u32 val;
931872fc 1267 bool cur_state;
b24e7179
JB
1268
1269 reg = DSPCNTR(plane);
1270 val = I915_READ(reg);
931872fc
CW
1271 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1272 WARN(cur_state != state,
1273 "plane %c assertion failure (expected %s, current %s)\n",
1274 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1275}
1276
931872fc
CW
1277#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1278#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1279
b24e7179
JB
1280static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1281 enum pipe pipe)
1282{
653e1026 1283 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1284 int reg, i;
1285 u32 val;
1286 int cur_pipe;
1287
653e1026
VS
1288 /* Primary planes are fixed to pipes on gen4+ */
1289 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1290 reg = DSPCNTR(pipe);
1291 val = I915_READ(reg);
83f26f16 1292 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1293 "plane %c assertion failure, should be disabled but not\n",
1294 plane_name(pipe));
19ec1358 1295 return;
28c05794 1296 }
19ec1358 1297
b24e7179 1298 /* Need to check both planes against the pipe */
08e2a7de 1299 for_each_pipe(i) {
b24e7179
JB
1300 reg = DSPCNTR(i);
1301 val = I915_READ(reg);
1302 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1303 DISPPLANE_SEL_PIPE_SHIFT;
1304 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1305 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1306 plane_name(i), pipe_name(pipe));
b24e7179
JB
1307 }
1308}
1309
19332d7a
JB
1310static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1311 enum pipe pipe)
1312{
20674eef 1313 struct drm_device *dev = dev_priv->dev;
1fe47785 1314 int reg, sprite;
19332d7a
JB
1315 u32 val;
1316
20674eef 1317 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1318 for_each_sprite(pipe, sprite) {
1319 reg = SPCNTR(pipe, sprite);
20674eef 1320 val = I915_READ(reg);
83f26f16 1321 WARN(val & SP_ENABLE,
20674eef 1322 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1323 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1324 }
1325 } else if (INTEL_INFO(dev)->gen >= 7) {
1326 reg = SPRCTL(pipe);
19332d7a 1327 val = I915_READ(reg);
83f26f16 1328 WARN(val & SPRITE_ENABLE,
06da8da2 1329 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1330 plane_name(pipe), pipe_name(pipe));
1331 } else if (INTEL_INFO(dev)->gen >= 5) {
1332 reg = DVSCNTR(pipe);
19332d7a 1333 val = I915_READ(reg);
83f26f16 1334 WARN(val & DVS_ENABLE,
06da8da2 1335 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1336 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1337 }
1338}
1339
89eff4be 1340static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1341{
1342 u32 val;
1343 bool enabled;
1344
89eff4be 1345 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1346
92f2584a
JB
1347 val = I915_READ(PCH_DREF_CONTROL);
1348 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1349 DREF_SUPERSPREAD_SOURCE_MASK));
1350 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1351}
1352
ab9412ba
DV
1353static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe)
92f2584a
JB
1355{
1356 int reg;
1357 u32 val;
1358 bool enabled;
1359
ab9412ba 1360 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1361 val = I915_READ(reg);
1362 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1363 WARN(enabled,
1364 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1365 pipe_name(pipe));
92f2584a
JB
1366}
1367
4e634389
KP
1368static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1370{
1371 if ((val & DP_PORT_EN) == 0)
1372 return false;
1373
1374 if (HAS_PCH_CPT(dev_priv->dev)) {
1375 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1376 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1377 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1378 return false;
44f37d1f
CML
1379 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1380 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1381 return false;
f0575e92
KP
1382 } else {
1383 if ((val & DP_PIPE_MASK) != (pipe << 30))
1384 return false;
1385 }
1386 return true;
1387}
1388
1519b995
KP
1389static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1390 enum pipe pipe, u32 val)
1391{
dc0fa718 1392 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1393 return false;
1394
1395 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1396 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1397 return false;
44f37d1f
CML
1398 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1399 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1400 return false;
1519b995 1401 } else {
dc0fa718 1402 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1403 return false;
1404 }
1405 return true;
1406}
1407
1408static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1409 enum pipe pipe, u32 val)
1410{
1411 if ((val & LVDS_PORT_EN) == 0)
1412 return false;
1413
1414 if (HAS_PCH_CPT(dev_priv->dev)) {
1415 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1416 return false;
1417 } else {
1418 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1419 return false;
1420 }
1421 return true;
1422}
1423
1424static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe, u32 val)
1426{
1427 if ((val & ADPA_DAC_ENABLE) == 0)
1428 return false;
1429 if (HAS_PCH_CPT(dev_priv->dev)) {
1430 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1431 return false;
1432 } else {
1433 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1434 return false;
1435 }
1436 return true;
1437}
1438
291906f1 1439static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1440 enum pipe pipe, int reg, u32 port_sel)
291906f1 1441{
47a05eca 1442 u32 val = I915_READ(reg);
4e634389 1443 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1444 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1445 reg, pipe_name(pipe));
de9a35ab 1446
75c5da27
DV
1447 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1448 && (val & DP_PIPEB_SELECT),
de9a35ab 1449 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1450}
1451
1452static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1453 enum pipe pipe, int reg)
1454{
47a05eca 1455 u32 val = I915_READ(reg);
b70ad586 1456 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1457 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1458 reg, pipe_name(pipe));
de9a35ab 1459
dc0fa718 1460 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1461 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1462 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1463}
1464
1465static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
1467{
1468 int reg;
1469 u32 val;
291906f1 1470
f0575e92
KP
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1474
1475 reg = PCH_ADPA;
1476 val = I915_READ(reg);
b70ad586 1477 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1478 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1479 pipe_name(pipe));
291906f1
JB
1480
1481 reg = PCH_LVDS;
1482 val = I915_READ(reg);
b70ad586 1483 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1485 pipe_name(pipe));
291906f1 1486
e2debe91
PZ
1487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1490}
1491
40e9cf64
JB
1492static void intel_init_dpio(struct drm_device *dev)
1493{
1494 struct drm_i915_private *dev_priv = dev->dev_private;
1495
1496 if (!IS_VALLEYVIEW(dev))
1497 return;
1498
a09caddd
CML
1499 /*
1500 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1501 * CHV x1 PHY (DP/HDMI D)
1502 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1503 */
1504 if (IS_CHERRYVIEW(dev)) {
1505 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1506 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1507 } else {
1508 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1509 }
5382f5f3
JB
1510}
1511
1512static void intel_reset_dpio(struct drm_device *dev)
1513{
1514 struct drm_i915_private *dev_priv = dev->dev_private;
1515
1516 if (!IS_VALLEYVIEW(dev))
1517 return;
1518
076ed3b2
CML
1519 if (IS_CHERRYVIEW(dev)) {
1520 enum dpio_phy phy;
1521 u32 val;
1522
1523 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1524 /* Poll for phypwrgood signal */
1525 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1526 PHY_POWERGOOD(phy), 1))
1527 DRM_ERROR("Display PHY %d is not power up\n", phy);
1528
1529 /*
1530 * Deassert common lane reset for PHY.
1531 *
1532 * This should only be done on init and resume from S3
1533 * with both PLLs disabled, or we risk losing DPIO and
1534 * PLL synchronization.
1535 */
1536 val = I915_READ(DISPLAY_PHY_CONTROL);
1537 I915_WRITE(DISPLAY_PHY_CONTROL,
1538 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1539 }
1540
1541 } else {
1542 /*
57021059
JB
1543 * If DPIO has already been reset, e.g. by BIOS, just skip all
1544 * this.
076ed3b2 1545 */
57021059
JB
1546 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1547 return;
1548
1549 /*
1550 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1551 * Need to assert and de-assert PHY SB reset by gating the
1552 * common lane power, then un-gating it.
1553 * Simply ungating isn't enough to reset the PHY enough to get
1554 * ports and lanes running.
1555 */
1556 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1557 false);
1558 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1559 true);
076ed3b2 1560 }
40e9cf64
JB
1561}
1562
426115cf 1563static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1564{
426115cf
DV
1565 struct drm_device *dev = crtc->base.dev;
1566 struct drm_i915_private *dev_priv = dev->dev_private;
1567 int reg = DPLL(crtc->pipe);
1568 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1569
426115cf 1570 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1571
1572 /* No really, not for ILK+ */
1573 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1574
1575 /* PLL is protected by panel, make sure we can write it */
1576 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
426115cf 1577 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1578
426115cf
DV
1579 I915_WRITE(reg, dpll);
1580 POSTING_READ(reg);
1581 udelay(150);
1582
1583 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1584 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1585
1586 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1587 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1588
1589 /* We do this three times for luck */
426115cf 1590 I915_WRITE(reg, dpll);
87442f73
DV
1591 POSTING_READ(reg);
1592 udelay(150); /* wait for warmup */
426115cf 1593 I915_WRITE(reg, dpll);
87442f73
DV
1594 POSTING_READ(reg);
1595 udelay(150); /* wait for warmup */
426115cf 1596 I915_WRITE(reg, dpll);
87442f73
DV
1597 POSTING_READ(reg);
1598 udelay(150); /* wait for warmup */
1599}
1600
9d556c99
CML
1601static void chv_enable_pll(struct intel_crtc *crtc)
1602{
1603 struct drm_device *dev = crtc->base.dev;
1604 struct drm_i915_private *dev_priv = dev->dev_private;
1605 int pipe = crtc->pipe;
1606 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1607 u32 tmp;
1608
1609 assert_pipe_disabled(dev_priv, crtc->pipe);
1610
1611 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1612
1613 mutex_lock(&dev_priv->dpio_lock);
1614
1615 /* Enable back the 10bit clock to display controller */
1616 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1617 tmp |= DPIO_DCLKP_EN;
1618 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1619
1620 /*
1621 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1622 */
1623 udelay(1);
1624
1625 /* Enable PLL */
a11b0703 1626 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1627
1628 /* Check PLL is locked */
a11b0703 1629 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1630 DRM_ERROR("PLL %d failed to lock\n", pipe);
1631
a11b0703
VS
1632 /* not sure when this should be written */
1633 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1634 POSTING_READ(DPLL_MD(pipe));
1635
9d556c99
CML
1636 mutex_unlock(&dev_priv->dpio_lock);
1637}
1638
66e3d5c0 1639static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1640{
66e3d5c0
DV
1641 struct drm_device *dev = crtc->base.dev;
1642 struct drm_i915_private *dev_priv = dev->dev_private;
1643 int reg = DPLL(crtc->pipe);
1644 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1645
66e3d5c0 1646 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1647
63d7bbe9 1648 /* No really, not for ILK+ */
3d13ef2e 1649 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1650
1651 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1652 if (IS_MOBILE(dev) && !IS_I830(dev))
1653 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1654
66e3d5c0
DV
1655 I915_WRITE(reg, dpll);
1656
1657 /* Wait for the clocks to stabilize. */
1658 POSTING_READ(reg);
1659 udelay(150);
1660
1661 if (INTEL_INFO(dev)->gen >= 4) {
1662 I915_WRITE(DPLL_MD(crtc->pipe),
1663 crtc->config.dpll_hw_state.dpll_md);
1664 } else {
1665 /* The pixel multiplier can only be updated once the
1666 * DPLL is enabled and the clocks are stable.
1667 *
1668 * So write it again.
1669 */
1670 I915_WRITE(reg, dpll);
1671 }
63d7bbe9
JB
1672
1673 /* We do this three times for luck */
66e3d5c0 1674 I915_WRITE(reg, dpll);
63d7bbe9
JB
1675 POSTING_READ(reg);
1676 udelay(150); /* wait for warmup */
66e3d5c0 1677 I915_WRITE(reg, dpll);
63d7bbe9
JB
1678 POSTING_READ(reg);
1679 udelay(150); /* wait for warmup */
66e3d5c0 1680 I915_WRITE(reg, dpll);
63d7bbe9
JB
1681 POSTING_READ(reg);
1682 udelay(150); /* wait for warmup */
1683}
1684
1685/**
50b44a44 1686 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1687 * @dev_priv: i915 private structure
1688 * @pipe: pipe PLL to disable
1689 *
1690 * Disable the PLL for @pipe, making sure the pipe is off first.
1691 *
1692 * Note! This is for pre-ILK only.
1693 */
50b44a44 1694static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1695{
63d7bbe9
JB
1696 /* Don't disable pipe A or pipe A PLLs if needed */
1697 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1698 return;
1699
1700 /* Make sure the pipe isn't still relying on us */
1701 assert_pipe_disabled(dev_priv, pipe);
1702
50b44a44
DV
1703 I915_WRITE(DPLL(pipe), 0);
1704 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1705}
1706
f6071166
JB
1707static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1708{
1709 u32 val = 0;
1710
1711 /* Make sure the pipe isn't still relying on us */
1712 assert_pipe_disabled(dev_priv, pipe);
1713
e5cbfbfb
ID
1714 /*
1715 * Leave integrated clock source and reference clock enabled for pipe B.
1716 * The latter is needed for VGA hotplug / manual detection.
1717 */
f6071166 1718 if (pipe == PIPE_B)
e5cbfbfb 1719 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1720 I915_WRITE(DPLL(pipe), val);
1721 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1722
1723}
1724
1725static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1726{
d752048d 1727 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1728 u32 val;
1729
a11b0703
VS
1730 /* Make sure the pipe isn't still relying on us */
1731 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1732
a11b0703
VS
1733 /* Set PLL en = 0 */
1734 val = DPLL_SSC_REF_CLOCK_CHV;
1735 if (pipe != PIPE_A)
1736 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1737 I915_WRITE(DPLL(pipe), val);
1738 POSTING_READ(DPLL(pipe));
d752048d
VS
1739
1740 mutex_lock(&dev_priv->dpio_lock);
1741
1742 /* Disable 10bit clock to display controller */
1743 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1744 val &= ~DPIO_DCLKP_EN;
1745 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1746
61407f6d
VS
1747 /* disable left/right clock distribution */
1748 if (pipe != PIPE_B) {
1749 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1750 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1751 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1752 } else {
1753 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1754 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1755 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1756 }
1757
d752048d 1758 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1759}
1760
e4607fcf
CML
1761void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1762 struct intel_digital_port *dport)
89b667f8
JB
1763{
1764 u32 port_mask;
00fc31b7 1765 int dpll_reg;
89b667f8 1766
e4607fcf
CML
1767 switch (dport->port) {
1768 case PORT_B:
89b667f8 1769 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1770 dpll_reg = DPLL(0);
e4607fcf
CML
1771 break;
1772 case PORT_C:
89b667f8 1773 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1774 dpll_reg = DPLL(0);
1775 break;
1776 case PORT_D:
1777 port_mask = DPLL_PORTD_READY_MASK;
1778 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1779 break;
1780 default:
1781 BUG();
1782 }
89b667f8 1783
00fc31b7 1784 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1785 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1786 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1787}
1788
b14b1055
DV
1789static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1790{
1791 struct drm_device *dev = crtc->base.dev;
1792 struct drm_i915_private *dev_priv = dev->dev_private;
1793 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1794
be19f0ff
CW
1795 if (WARN_ON(pll == NULL))
1796 return;
1797
b14b1055
DV
1798 WARN_ON(!pll->refcount);
1799 if (pll->active == 0) {
1800 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1801 WARN_ON(pll->on);
1802 assert_shared_dpll_disabled(dev_priv, pll);
1803
1804 pll->mode_set(dev_priv, pll);
1805 }
1806}
1807
92f2584a 1808/**
85b3894f 1809 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1810 * @dev_priv: i915 private structure
1811 * @pipe: pipe PLL to enable
1812 *
1813 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1814 * drives the transcoder clock.
1815 */
85b3894f 1816static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1817{
3d13ef2e
DL
1818 struct drm_device *dev = crtc->base.dev;
1819 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1820 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1821
87a875bb 1822 if (WARN_ON(pll == NULL))
48da64a8
CW
1823 return;
1824
1825 if (WARN_ON(pll->refcount == 0))
1826 return;
ee7b9f93 1827
46edb027
DV
1828 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1829 pll->name, pll->active, pll->on,
e2b78267 1830 crtc->base.base.id);
92f2584a 1831
cdbd2316
DV
1832 if (pll->active++) {
1833 WARN_ON(!pll->on);
e9d6944e 1834 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1835 return;
1836 }
f4a091c7 1837 WARN_ON(pll->on);
ee7b9f93 1838
46edb027 1839 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1840 pll->enable(dev_priv, pll);
ee7b9f93 1841 pll->on = true;
92f2584a
JB
1842}
1843
e2b78267 1844static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1845{
3d13ef2e
DL
1846 struct drm_device *dev = crtc->base.dev;
1847 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1848 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1849
92f2584a 1850 /* PCH only available on ILK+ */
3d13ef2e 1851 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1852 if (WARN_ON(pll == NULL))
ee7b9f93 1853 return;
92f2584a 1854
48da64a8
CW
1855 if (WARN_ON(pll->refcount == 0))
1856 return;
7a419866 1857
46edb027
DV
1858 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1859 pll->name, pll->active, pll->on,
e2b78267 1860 crtc->base.base.id);
7a419866 1861
48da64a8 1862 if (WARN_ON(pll->active == 0)) {
e9d6944e 1863 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1864 return;
1865 }
1866
e9d6944e 1867 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1868 WARN_ON(!pll->on);
cdbd2316 1869 if (--pll->active)
7a419866 1870 return;
ee7b9f93 1871
46edb027 1872 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1873 pll->disable(dev_priv, pll);
ee7b9f93 1874 pll->on = false;
92f2584a
JB
1875}
1876
b8a4f404
PZ
1877static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
040484af 1879{
23670b32 1880 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1881 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1883 uint32_t reg, val, pipeconf_val;
040484af
JB
1884
1885 /* PCH only available on ILK+ */
3d13ef2e 1886 BUG_ON(INTEL_INFO(dev)->gen < 5);
040484af
JB
1887
1888 /* Make sure PCH DPLL is enabled */
e72f9fbf 1889 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1890 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1891
1892 /* FDI must be feeding us bits for PCH ports */
1893 assert_fdi_tx_enabled(dev_priv, pipe);
1894 assert_fdi_rx_enabled(dev_priv, pipe);
1895
23670b32
DV
1896 if (HAS_PCH_CPT(dev)) {
1897 /* Workaround: Set the timing override bit before enabling the
1898 * pch transcoder. */
1899 reg = TRANS_CHICKEN2(pipe);
1900 val = I915_READ(reg);
1901 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1902 I915_WRITE(reg, val);
59c859d6 1903 }
23670b32 1904
ab9412ba 1905 reg = PCH_TRANSCONF(pipe);
040484af 1906 val = I915_READ(reg);
5f7f726d 1907 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1908
1909 if (HAS_PCH_IBX(dev_priv->dev)) {
1910 /*
1911 * make the BPC in transcoder be consistent with
1912 * that in pipeconf reg.
1913 */
dfd07d72
DV
1914 val &= ~PIPECONF_BPC_MASK;
1915 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1916 }
5f7f726d
PZ
1917
1918 val &= ~TRANS_INTERLACE_MASK;
1919 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1920 if (HAS_PCH_IBX(dev_priv->dev) &&
1921 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1922 val |= TRANS_LEGACY_INTERLACED_ILK;
1923 else
1924 val |= TRANS_INTERLACED;
5f7f726d
PZ
1925 else
1926 val |= TRANS_PROGRESSIVE;
1927
040484af
JB
1928 I915_WRITE(reg, val | TRANS_ENABLE);
1929 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1930 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1931}
1932
8fb033d7 1933static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1934 enum transcoder cpu_transcoder)
040484af 1935{
8fb033d7 1936 u32 val, pipeconf_val;
8fb033d7
PZ
1937
1938 /* PCH only available on ILK+ */
3d13ef2e 1939 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
8fb033d7 1940
8fb033d7 1941 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1942 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1943 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1944
223a6fdf
PZ
1945 /* Workaround: set timing override bit. */
1946 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1947 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1948 I915_WRITE(_TRANSA_CHICKEN2, val);
1949
25f3ef11 1950 val = TRANS_ENABLE;
937bb610 1951 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1952
9a76b1c6
PZ
1953 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1954 PIPECONF_INTERLACED_ILK)
a35f2679 1955 val |= TRANS_INTERLACED;
8fb033d7
PZ
1956 else
1957 val |= TRANS_PROGRESSIVE;
1958
ab9412ba
DV
1959 I915_WRITE(LPT_TRANSCONF, val);
1960 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1961 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1962}
1963
b8a4f404
PZ
1964static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1965 enum pipe pipe)
040484af 1966{
23670b32
DV
1967 struct drm_device *dev = dev_priv->dev;
1968 uint32_t reg, val;
040484af
JB
1969
1970 /* FDI relies on the transcoder */
1971 assert_fdi_tx_disabled(dev_priv, pipe);
1972 assert_fdi_rx_disabled(dev_priv, pipe);
1973
291906f1
JB
1974 /* Ports must be off as well */
1975 assert_pch_ports_disabled(dev_priv, pipe);
1976
ab9412ba 1977 reg = PCH_TRANSCONF(pipe);
040484af
JB
1978 val = I915_READ(reg);
1979 val &= ~TRANS_ENABLE;
1980 I915_WRITE(reg, val);
1981 /* wait for PCH transcoder off, transcoder state */
1982 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1983 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1984
1985 if (!HAS_PCH_IBX(dev)) {
1986 /* Workaround: Clear the timing override chicken bit again. */
1987 reg = TRANS_CHICKEN2(pipe);
1988 val = I915_READ(reg);
1989 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1990 I915_WRITE(reg, val);
1991 }
040484af
JB
1992}
1993
ab4d966c 1994static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1995{
8fb033d7
PZ
1996 u32 val;
1997
ab9412ba 1998 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1999 val &= ~TRANS_ENABLE;
ab9412ba 2000 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2001 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2002 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2003 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2004
2005 /* Workaround: clear timing override bit. */
2006 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2007 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2008 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2009}
2010
b24e7179 2011/**
309cfea8 2012 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2013 * @crtc: crtc responsible for the pipe
b24e7179 2014 *
0372264a 2015 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2016 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2017 */
e1fdc473 2018static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2019{
0372264a
PZ
2020 struct drm_device *dev = crtc->base.dev;
2021 struct drm_i915_private *dev_priv = dev->dev_private;
2022 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2023 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2024 pipe);
1a240d4d 2025 enum pipe pch_transcoder;
b24e7179
JB
2026 int reg;
2027 u32 val;
2028
58c6eaa2 2029 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2030 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2031 assert_sprites_disabled(dev_priv, pipe);
2032
681e5811 2033 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2034 pch_transcoder = TRANSCODER_A;
2035 else
2036 pch_transcoder = pipe;
2037
b24e7179
JB
2038 /*
2039 * A pipe without a PLL won't actually be able to drive bits from
2040 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2041 * need the check.
2042 */
2043 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2044 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2045 assert_dsi_pll_enabled(dev_priv);
2046 else
2047 assert_pll_enabled(dev_priv, pipe);
040484af 2048 else {
30421c4f 2049 if (crtc->config.has_pch_encoder) {
040484af 2050 /* if driving the PCH, we need FDI enabled */
cc391bbb 2051 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2052 assert_fdi_tx_pll_enabled(dev_priv,
2053 (enum pipe) cpu_transcoder);
040484af
JB
2054 }
2055 /* FIXME: assert CPU port conditions for SNB+ */
2056 }
b24e7179 2057
702e7a56 2058 reg = PIPECONF(cpu_transcoder);
b24e7179 2059 val = I915_READ(reg);
7ad25d48
PZ
2060 if (val & PIPECONF_ENABLE) {
2061 WARN_ON(!(pipe == PIPE_A &&
2062 dev_priv->quirks & QUIRK_PIPEA_FORCE));
00d70b15 2063 return;
7ad25d48 2064 }
00d70b15
CW
2065
2066 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2067 POSTING_READ(reg);
b24e7179
JB
2068}
2069
2070/**
309cfea8 2071 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
2072 * @dev_priv: i915 private structure
2073 * @pipe: pipe to disable
2074 *
2075 * Disable @pipe, making sure that various hardware specific requirements
2076 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2077 *
2078 * @pipe should be %PIPE_A or %PIPE_B.
2079 *
2080 * Will wait until the pipe has shut down before returning.
2081 */
2082static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2083 enum pipe pipe)
2084{
702e7a56
PZ
2085 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2086 pipe);
b24e7179
JB
2087 int reg;
2088 u32 val;
2089
2090 /*
2091 * Make sure planes won't keep trying to pump pixels to us,
2092 * or we might hang the display.
2093 */
2094 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2095 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2096 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
2097
2098 /* Don't disable pipe A or pipe A PLLs if needed */
2099 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2100 return;
2101
702e7a56 2102 reg = PIPECONF(cpu_transcoder);
b24e7179 2103 val = I915_READ(reg);
00d70b15
CW
2104 if ((val & PIPECONF_ENABLE) == 0)
2105 return;
2106
2107 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
2108 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2109}
2110
d74362c9
KP
2111/*
2112 * Plane regs are double buffered, going from enabled->disabled needs a
2113 * trigger in order to latch. The display address reg provides this.
2114 */
1dba99f4
VS
2115void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2116 enum plane plane)
d74362c9 2117{
3d13ef2e
DL
2118 struct drm_device *dev = dev_priv->dev;
2119 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2120
2121 I915_WRITE(reg, I915_READ(reg));
2122 POSTING_READ(reg);
d74362c9
KP
2123}
2124
b24e7179 2125/**
262ca2b0 2126 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
b24e7179
JB
2127 * @dev_priv: i915 private structure
2128 * @plane: plane to enable
2129 * @pipe: pipe being fed
2130 *
2131 * Enable @plane on @pipe, making sure that @pipe is running first.
2132 */
262ca2b0
MR
2133static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2134 enum plane plane, enum pipe pipe)
b24e7179 2135{
939c2fe8
VS
2136 struct intel_crtc *intel_crtc =
2137 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2138 int reg;
2139 u32 val;
2140
2141 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2142 assert_pipe_enabled(dev_priv, pipe);
2143
98ec7739
VS
2144 if (intel_crtc->primary_enabled)
2145 return;
0037f71c 2146
4c445e0e 2147 intel_crtc->primary_enabled = true;
939c2fe8 2148
b24e7179
JB
2149 reg = DSPCNTR(plane);
2150 val = I915_READ(reg);
10efa932 2151 WARN_ON(val & DISPLAY_PLANE_ENABLE);
00d70b15
CW
2152
2153 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1dba99f4 2154 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2155}
2156
b24e7179 2157/**
262ca2b0 2158 * intel_disable_primary_hw_plane - disable the primary hardware plane
b24e7179
JB
2159 * @dev_priv: i915 private structure
2160 * @plane: plane to disable
2161 * @pipe: pipe consuming the data
2162 *
2163 * Disable @plane; should be an independent operation.
2164 */
262ca2b0
MR
2165static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2166 enum plane plane, enum pipe pipe)
b24e7179 2167{
939c2fe8
VS
2168 struct intel_crtc *intel_crtc =
2169 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2170 int reg;
2171 u32 val;
2172
98ec7739
VS
2173 if (!intel_crtc->primary_enabled)
2174 return;
0037f71c 2175
4c445e0e 2176 intel_crtc->primary_enabled = false;
939c2fe8 2177
b24e7179
JB
2178 reg = DSPCNTR(plane);
2179 val = I915_READ(reg);
10efa932 2180 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
00d70b15
CW
2181
2182 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1dba99f4 2183 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2184}
2185
693db184
CW
2186static bool need_vtd_wa(struct drm_device *dev)
2187{
2188#ifdef CONFIG_INTEL_IOMMU
2189 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2190 return true;
2191#endif
2192 return false;
2193}
2194
a57ce0b2
JB
2195static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2196{
2197 int tile_height;
2198
2199 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2200 return ALIGN(height, tile_height);
2201}
2202
127bd2ac 2203int
48b956c5 2204intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2205 struct drm_i915_gem_object *obj,
a4872ba6 2206 struct intel_engine_cs *pipelined)
6b95a207 2207{
ce453d81 2208 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2209 u32 alignment;
2210 int ret;
2211
05394f39 2212 switch (obj->tiling_mode) {
6b95a207 2213 case I915_TILING_NONE:
534843da
CW
2214 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2215 alignment = 128 * 1024;
a6c45cf0 2216 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2217 alignment = 4 * 1024;
2218 else
2219 alignment = 64 * 1024;
6b95a207
KH
2220 break;
2221 case I915_TILING_X:
2222 /* pin() will align the object as required by fence */
2223 alignment = 0;
2224 break;
2225 case I915_TILING_Y:
80075d49 2226 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2227 return -EINVAL;
2228 default:
2229 BUG();
2230 }
2231
693db184
CW
2232 /* Note that the w/a also requires 64 PTE of padding following the
2233 * bo. We currently fill all unused PTE with the shadow page and so
2234 * we should always have valid PTE following the scanout preventing
2235 * the VT-d warning.
2236 */
2237 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2238 alignment = 256 * 1024;
2239
ce453d81 2240 dev_priv->mm.interruptible = false;
2da3b9b9 2241 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2242 if (ret)
ce453d81 2243 goto err_interruptible;
6b95a207
KH
2244
2245 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2246 * fence, whereas 965+ only requires a fence if using
2247 * framebuffer compression. For simplicity, we always install
2248 * a fence as the cost is not that onerous.
2249 */
06d98131 2250 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2251 if (ret)
2252 goto err_unpin;
1690e1eb 2253
9a5a53b3 2254 i915_gem_object_pin_fence(obj);
6b95a207 2255
ce453d81 2256 dev_priv->mm.interruptible = true;
6b95a207 2257 return 0;
48b956c5
CW
2258
2259err_unpin:
cc98b413 2260 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2261err_interruptible:
2262 dev_priv->mm.interruptible = true;
48b956c5 2263 return ret;
6b95a207
KH
2264}
2265
1690e1eb
CW
2266void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2267{
2268 i915_gem_object_unpin_fence(obj);
cc98b413 2269 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2270}
2271
c2c75131
DV
2272/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2273 * is assumed to be a power-of-two. */
bc752862
CW
2274unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2275 unsigned int tiling_mode,
2276 unsigned int cpp,
2277 unsigned int pitch)
c2c75131 2278{
bc752862
CW
2279 if (tiling_mode != I915_TILING_NONE) {
2280 unsigned int tile_rows, tiles;
c2c75131 2281
bc752862
CW
2282 tile_rows = *y / 8;
2283 *y %= 8;
c2c75131 2284
bc752862
CW
2285 tiles = *x / (512/cpp);
2286 *x %= 512/cpp;
2287
2288 return tile_rows * pitch * 8 + tiles * 4096;
2289 } else {
2290 unsigned int offset;
2291
2292 offset = *y * pitch + *x * cpp;
2293 *y = 0;
2294 *x = (offset & 4095) / cpp;
2295 return offset & -4096;
2296 }
c2c75131
DV
2297}
2298
46f297fb
JB
2299int intel_format_to_fourcc(int format)
2300{
2301 switch (format) {
2302 case DISPPLANE_8BPP:
2303 return DRM_FORMAT_C8;
2304 case DISPPLANE_BGRX555:
2305 return DRM_FORMAT_XRGB1555;
2306 case DISPPLANE_BGRX565:
2307 return DRM_FORMAT_RGB565;
2308 default:
2309 case DISPPLANE_BGRX888:
2310 return DRM_FORMAT_XRGB8888;
2311 case DISPPLANE_RGBX888:
2312 return DRM_FORMAT_XBGR8888;
2313 case DISPPLANE_BGRX101010:
2314 return DRM_FORMAT_XRGB2101010;
2315 case DISPPLANE_RGBX101010:
2316 return DRM_FORMAT_XBGR2101010;
2317 }
2318}
2319
484b41dd 2320static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2321 struct intel_plane_config *plane_config)
2322{
2323 struct drm_device *dev = crtc->base.dev;
2324 struct drm_i915_gem_object *obj = NULL;
2325 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2326 u32 base = plane_config->base;
2327
ff2652ea
CW
2328 if (plane_config->size == 0)
2329 return false;
2330
46f297fb
JB
2331 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2332 plane_config->size);
2333 if (!obj)
484b41dd 2334 return false;
46f297fb
JB
2335
2336 if (plane_config->tiled) {
2337 obj->tiling_mode = I915_TILING_X;
66e514c1 2338 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2339 }
2340
66e514c1
DA
2341 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2342 mode_cmd.width = crtc->base.primary->fb->width;
2343 mode_cmd.height = crtc->base.primary->fb->height;
2344 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2345
2346 mutex_lock(&dev->struct_mutex);
2347
66e514c1 2348 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2349 &mode_cmd, obj)) {
46f297fb
JB
2350 DRM_DEBUG_KMS("intel fb init failed\n");
2351 goto out_unref_obj;
2352 }
2353
a071fa00 2354 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2355 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2356
2357 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2358 return true;
46f297fb
JB
2359
2360out_unref_obj:
2361 drm_gem_object_unreference(&obj->base);
2362 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2363 return false;
2364}
2365
2366static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2367 struct intel_plane_config *plane_config)
2368{
2369 struct drm_device *dev = intel_crtc->base.dev;
2370 struct drm_crtc *c;
2371 struct intel_crtc *i;
2372 struct intel_framebuffer *fb;
2373
66e514c1 2374 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2375 return;
2376
2377 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2378 return;
2379
66e514c1
DA
2380 kfree(intel_crtc->base.primary->fb);
2381 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2382
2383 /*
2384 * Failed to alloc the obj, check to see if we should share
2385 * an fb with another CRTC instead
2386 */
70e1e0ec 2387 for_each_crtc(dev, c) {
484b41dd
JB
2388 i = to_intel_crtc(c);
2389
2390 if (c == &intel_crtc->base)
2391 continue;
2392
66e514c1 2393 if (!i->active || !c->primary->fb)
484b41dd
JB
2394 continue;
2395
66e514c1 2396 fb = to_intel_framebuffer(c->primary->fb);
484b41dd 2397 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
66e514c1
DA
2398 drm_framebuffer_reference(c->primary->fb);
2399 intel_crtc->base.primary->fb = c->primary->fb;
a071fa00 2400 fb->obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2401 break;
2402 }
2403 }
46f297fb
JB
2404}
2405
29b9bde6
DV
2406static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2407 struct drm_framebuffer *fb,
2408 int x, int y)
81255565
JB
2409{
2410 struct drm_device *dev = crtc->dev;
2411 struct drm_i915_private *dev_priv = dev->dev_private;
2412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2413 struct intel_framebuffer *intel_fb;
05394f39 2414 struct drm_i915_gem_object *obj;
81255565 2415 int plane = intel_crtc->plane;
e506a0c6 2416 unsigned long linear_offset;
81255565 2417 u32 dspcntr;
5eddb70b 2418 u32 reg;
81255565 2419
81255565
JB
2420 intel_fb = to_intel_framebuffer(fb);
2421 obj = intel_fb->obj;
81255565 2422
5eddb70b
CW
2423 reg = DSPCNTR(plane);
2424 dspcntr = I915_READ(reg);
81255565
JB
2425 /* Mask out pixel format bits in case we change it */
2426 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2427 switch (fb->pixel_format) {
2428 case DRM_FORMAT_C8:
81255565
JB
2429 dspcntr |= DISPPLANE_8BPP;
2430 break;
57779d06
VS
2431 case DRM_FORMAT_XRGB1555:
2432 case DRM_FORMAT_ARGB1555:
2433 dspcntr |= DISPPLANE_BGRX555;
81255565 2434 break;
57779d06
VS
2435 case DRM_FORMAT_RGB565:
2436 dspcntr |= DISPPLANE_BGRX565;
2437 break;
2438 case DRM_FORMAT_XRGB8888:
2439 case DRM_FORMAT_ARGB8888:
2440 dspcntr |= DISPPLANE_BGRX888;
2441 break;
2442 case DRM_FORMAT_XBGR8888:
2443 case DRM_FORMAT_ABGR8888:
2444 dspcntr |= DISPPLANE_RGBX888;
2445 break;
2446 case DRM_FORMAT_XRGB2101010:
2447 case DRM_FORMAT_ARGB2101010:
2448 dspcntr |= DISPPLANE_BGRX101010;
2449 break;
2450 case DRM_FORMAT_XBGR2101010:
2451 case DRM_FORMAT_ABGR2101010:
2452 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2453 break;
2454 default:
baba133a 2455 BUG();
81255565 2456 }
57779d06 2457
a6c45cf0 2458 if (INTEL_INFO(dev)->gen >= 4) {
05394f39 2459 if (obj->tiling_mode != I915_TILING_NONE)
81255565
JB
2460 dspcntr |= DISPPLANE_TILED;
2461 else
2462 dspcntr &= ~DISPPLANE_TILED;
2463 }
2464
de1aa629
VS
2465 if (IS_G4X(dev))
2466 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2467
5eddb70b 2468 I915_WRITE(reg, dspcntr);
81255565 2469
e506a0c6 2470 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
81255565 2471
c2c75131
DV
2472 if (INTEL_INFO(dev)->gen >= 4) {
2473 intel_crtc->dspaddr_offset =
bc752862
CW
2474 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2475 fb->bits_per_pixel / 8,
2476 fb->pitches[0]);
c2c75131
DV
2477 linear_offset -= intel_crtc->dspaddr_offset;
2478 } else {
e506a0c6 2479 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2480 }
e506a0c6 2481
f343c5f6
BW
2482 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2483 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2484 fb->pitches[0]);
01f2c773 2485 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2486 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2487 I915_WRITE(DSPSURF(plane),
2488 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2489 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2490 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2491 } else
f343c5f6 2492 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2493 POSTING_READ(reg);
17638cd6
JB
2494}
2495
29b9bde6
DV
2496static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2497 struct drm_framebuffer *fb,
2498 int x, int y)
17638cd6
JB
2499{
2500 struct drm_device *dev = crtc->dev;
2501 struct drm_i915_private *dev_priv = dev->dev_private;
2502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2503 struct intel_framebuffer *intel_fb;
2504 struct drm_i915_gem_object *obj;
2505 int plane = intel_crtc->plane;
e506a0c6 2506 unsigned long linear_offset;
17638cd6
JB
2507 u32 dspcntr;
2508 u32 reg;
2509
17638cd6
JB
2510 intel_fb = to_intel_framebuffer(fb);
2511 obj = intel_fb->obj;
2512
2513 reg = DSPCNTR(plane);
2514 dspcntr = I915_READ(reg);
2515 /* Mask out pixel format bits in case we change it */
2516 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2517 switch (fb->pixel_format) {
2518 case DRM_FORMAT_C8:
17638cd6
JB
2519 dspcntr |= DISPPLANE_8BPP;
2520 break;
57779d06
VS
2521 case DRM_FORMAT_RGB565:
2522 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2523 break;
57779d06
VS
2524 case DRM_FORMAT_XRGB8888:
2525 case DRM_FORMAT_ARGB8888:
2526 dspcntr |= DISPPLANE_BGRX888;
2527 break;
2528 case DRM_FORMAT_XBGR8888:
2529 case DRM_FORMAT_ABGR8888:
2530 dspcntr |= DISPPLANE_RGBX888;
2531 break;
2532 case DRM_FORMAT_XRGB2101010:
2533 case DRM_FORMAT_ARGB2101010:
2534 dspcntr |= DISPPLANE_BGRX101010;
2535 break;
2536 case DRM_FORMAT_XBGR2101010:
2537 case DRM_FORMAT_ABGR2101010:
2538 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2539 break;
2540 default:
baba133a 2541 BUG();
17638cd6
JB
2542 }
2543
2544 if (obj->tiling_mode != I915_TILING_NONE)
2545 dspcntr |= DISPPLANE_TILED;
2546 else
2547 dspcntr &= ~DISPPLANE_TILED;
2548
b42c6009 2549 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1f5d76db
PZ
2550 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2551 else
2552 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6
JB
2553
2554 I915_WRITE(reg, dspcntr);
2555
e506a0c6 2556 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
c2c75131 2557 intel_crtc->dspaddr_offset =
bc752862
CW
2558 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2559 fb->bits_per_pixel / 8,
2560 fb->pitches[0]);
c2c75131 2561 linear_offset -= intel_crtc->dspaddr_offset;
17638cd6 2562
f343c5f6
BW
2563 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2564 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2565 fb->pitches[0]);
01f2c773 2566 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2567 I915_WRITE(DSPSURF(plane),
2568 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2569 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2570 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2571 } else {
2572 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2573 I915_WRITE(DSPLINOFF(plane), linear_offset);
2574 }
17638cd6 2575 POSTING_READ(reg);
17638cd6
JB
2576}
2577
2578/* Assume fb object is pinned & idle & fenced and just update base pointers */
2579static int
2580intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2581 int x, int y, enum mode_set_atomic state)
2582{
2583 struct drm_device *dev = crtc->dev;
2584 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2585
6b8e6ed0
CW
2586 if (dev_priv->display.disable_fbc)
2587 dev_priv->display.disable_fbc(dev);
3dec0095 2588 intel_increase_pllclock(crtc);
81255565 2589
29b9bde6
DV
2590 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2591
2592 return 0;
81255565
JB
2593}
2594
96a02917
VS
2595void intel_display_handle_reset(struct drm_device *dev)
2596{
2597 struct drm_i915_private *dev_priv = dev->dev_private;
2598 struct drm_crtc *crtc;
2599
2600 /*
2601 * Flips in the rings have been nuked by the reset,
2602 * so complete all pending flips so that user space
2603 * will get its events and not get stuck.
2604 *
2605 * Also update the base address of all primary
2606 * planes to the the last fb to make sure we're
2607 * showing the correct fb after a reset.
2608 *
2609 * Need to make two loops over the crtcs so that we
2610 * don't try to grab a crtc mutex before the
2611 * pending_flip_queue really got woken up.
2612 */
2613
70e1e0ec 2614 for_each_crtc(dev, crtc) {
96a02917
VS
2615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2616 enum plane plane = intel_crtc->plane;
2617
2618 intel_prepare_page_flip(dev, plane);
2619 intel_finish_page_flip_plane(dev, plane);
2620 }
2621
70e1e0ec 2622 for_each_crtc(dev, crtc) {
96a02917
VS
2623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2624
51fd371b 2625 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2626 /*
2627 * FIXME: Once we have proper support for primary planes (and
2628 * disabling them without disabling the entire crtc) allow again
66e514c1 2629 * a NULL crtc->primary->fb.
947fdaad 2630 */
f4510a27 2631 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2632 dev_priv->display.update_primary_plane(crtc,
66e514c1 2633 crtc->primary->fb,
262ca2b0
MR
2634 crtc->x,
2635 crtc->y);
51fd371b 2636 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2637 }
2638}
2639
14667a4b
CW
2640static int
2641intel_finish_fb(struct drm_framebuffer *old_fb)
2642{
2643 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2644 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2645 bool was_interruptible = dev_priv->mm.interruptible;
2646 int ret;
2647
14667a4b
CW
2648 /* Big Hammer, we also need to ensure that any pending
2649 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2650 * current scanout is retired before unpinning the old
2651 * framebuffer.
2652 *
2653 * This should only fail upon a hung GPU, in which case we
2654 * can safely continue.
2655 */
2656 dev_priv->mm.interruptible = false;
2657 ret = i915_gem_object_finish_gpu(obj);
2658 dev_priv->mm.interruptible = was_interruptible;
2659
2660 return ret;
2661}
2662
7d5e3799
CW
2663static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2664{
2665 struct drm_device *dev = crtc->dev;
2666 struct drm_i915_private *dev_priv = dev->dev_private;
2667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2668 unsigned long flags;
2669 bool pending;
2670
2671 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2672 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2673 return false;
2674
2675 spin_lock_irqsave(&dev->event_lock, flags);
2676 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2677 spin_unlock_irqrestore(&dev->event_lock, flags);
2678
2679 return pending;
2680}
2681
5c3b82e2 2682static int
3c4fdcfb 2683intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2684 struct drm_framebuffer *fb)
79e53945
JB
2685{
2686 struct drm_device *dev = crtc->dev;
6b8e6ed0 2687 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2689 enum pipe pipe = intel_crtc->pipe;
94352cf9 2690 struct drm_framebuffer *old_fb;
a071fa00 2691 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
5c3b82e2 2692 int ret;
79e53945 2693
7d5e3799
CW
2694 if (intel_crtc_has_pending_flip(crtc)) {
2695 DRM_ERROR("pipe is still busy with an old pageflip\n");
2696 return -EBUSY;
2697 }
2698
79e53945 2699 /* no fb bound */
94352cf9 2700 if (!fb) {
a5071c2f 2701 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2702 return 0;
2703 }
2704
7eb552ae 2705 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2706 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2707 plane_name(intel_crtc->plane),
2708 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2709 return -EINVAL;
79e53945
JB
2710 }
2711
a071fa00
DV
2712 old_fb = crtc->primary->fb;
2713
5c3b82e2 2714 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2715 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2716 if (ret == 0)
2717 i915_gem_track_fb(to_intel_framebuffer(old_fb)->obj, obj,
2718 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2719 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2720 if (ret != 0) {
a5071c2f 2721 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2722 return ret;
2723 }
79e53945 2724
bb2043de
DL
2725 /*
2726 * Update pipe size and adjust fitter if needed: the reason for this is
2727 * that in compute_mode_changes we check the native mode (not the pfit
2728 * mode) to see if we can flip rather than do a full mode set. In the
2729 * fastboot case, we'll flip, but if we don't update the pipesrc and
2730 * pfit state, we'll end up with a big fb scanned out into the wrong
2731 * sized surface.
2732 *
2733 * To fix this properly, we need to hoist the checks up into
2734 * compute_mode_changes (or above), check the actual pfit state and
2735 * whether the platform allows pfit disable with pipe active, and only
2736 * then update the pipesrc and pfit state, even on the flip path.
2737 */
d330a953 2738 if (i915.fastboot) {
d7bf63f2
DL
2739 const struct drm_display_mode *adjusted_mode =
2740 &intel_crtc->config.adjusted_mode;
2741
4d6a3e63 2742 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2743 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2744 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2745 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2746 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2747 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2748 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2749 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2750 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2751 }
0637d60d
JB
2752 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2753 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2754 }
2755
29b9bde6 2756 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2757
f4510a27 2758 crtc->primary->fb = fb;
6c4c86f5
DV
2759 crtc->x = x;
2760 crtc->y = y;
94352cf9 2761
b7f1de28 2762 if (old_fb) {
d7697eea
DV
2763 if (intel_crtc->active && old_fb != fb)
2764 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2765 mutex_lock(&dev->struct_mutex);
1690e1eb 2766 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
8ac36ec1 2767 mutex_unlock(&dev->struct_mutex);
b7f1de28 2768 }
652c393a 2769
8ac36ec1 2770 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2771 intel_update_fbc(dev);
5c3b82e2 2772 mutex_unlock(&dev->struct_mutex);
79e53945 2773
5c3b82e2 2774 return 0;
79e53945
JB
2775}
2776
5e84e1a4
ZW
2777static void intel_fdi_normal_train(struct drm_crtc *crtc)
2778{
2779 struct drm_device *dev = crtc->dev;
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2782 int pipe = intel_crtc->pipe;
2783 u32 reg, temp;
2784
2785 /* enable normal train */
2786 reg = FDI_TX_CTL(pipe);
2787 temp = I915_READ(reg);
61e499bf 2788 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2789 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2790 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2791 } else {
2792 temp &= ~FDI_LINK_TRAIN_NONE;
2793 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2794 }
5e84e1a4
ZW
2795 I915_WRITE(reg, temp);
2796
2797 reg = FDI_RX_CTL(pipe);
2798 temp = I915_READ(reg);
2799 if (HAS_PCH_CPT(dev)) {
2800 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2801 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2802 } else {
2803 temp &= ~FDI_LINK_TRAIN_NONE;
2804 temp |= FDI_LINK_TRAIN_NONE;
2805 }
2806 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2807
2808 /* wait one idle pattern time */
2809 POSTING_READ(reg);
2810 udelay(1000);
357555c0
JB
2811
2812 /* IVB wants error correction enabled */
2813 if (IS_IVYBRIDGE(dev))
2814 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2815 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2816}
2817
1fbc0d78 2818static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2819{
1fbc0d78
DV
2820 return crtc->base.enabled && crtc->active &&
2821 crtc->config.has_pch_encoder;
1e833f40
DV
2822}
2823
01a415fd
DV
2824static void ivb_modeset_global_resources(struct drm_device *dev)
2825{
2826 struct drm_i915_private *dev_priv = dev->dev_private;
2827 struct intel_crtc *pipe_B_crtc =
2828 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2829 struct intel_crtc *pipe_C_crtc =
2830 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2831 uint32_t temp;
2832
1e833f40
DV
2833 /*
2834 * When everything is off disable fdi C so that we could enable fdi B
2835 * with all lanes. Note that we don't care about enabled pipes without
2836 * an enabled pch encoder.
2837 */
2838 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2839 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2840 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2841 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2842
2843 temp = I915_READ(SOUTH_CHICKEN1);
2844 temp &= ~FDI_BC_BIFURCATION_SELECT;
2845 DRM_DEBUG_KMS("disabling fdi C rx\n");
2846 I915_WRITE(SOUTH_CHICKEN1, temp);
2847 }
2848}
2849
8db9d77b
ZW
2850/* The FDI link training functions for ILK/Ibexpeak. */
2851static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2852{
2853 struct drm_device *dev = crtc->dev;
2854 struct drm_i915_private *dev_priv = dev->dev_private;
2855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2856 int pipe = intel_crtc->pipe;
5eddb70b 2857 u32 reg, temp, tries;
8db9d77b 2858
1c8562f6 2859 /* FDI needs bits from pipe first */
0fc932b8 2860 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2861
e1a44743
AJ
2862 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2863 for train result */
5eddb70b
CW
2864 reg = FDI_RX_IMR(pipe);
2865 temp = I915_READ(reg);
e1a44743
AJ
2866 temp &= ~FDI_RX_SYMBOL_LOCK;
2867 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2868 I915_WRITE(reg, temp);
2869 I915_READ(reg);
e1a44743
AJ
2870 udelay(150);
2871
8db9d77b 2872 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2873 reg = FDI_TX_CTL(pipe);
2874 temp = I915_READ(reg);
627eb5a3
DV
2875 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2876 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2877 temp &= ~FDI_LINK_TRAIN_NONE;
2878 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2879 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2880
5eddb70b
CW
2881 reg = FDI_RX_CTL(pipe);
2882 temp = I915_READ(reg);
8db9d77b
ZW
2883 temp &= ~FDI_LINK_TRAIN_NONE;
2884 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2885 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2886
2887 POSTING_READ(reg);
8db9d77b
ZW
2888 udelay(150);
2889
5b2adf89 2890 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2891 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2892 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2893 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2894
5eddb70b 2895 reg = FDI_RX_IIR(pipe);
e1a44743 2896 for (tries = 0; tries < 5; tries++) {
5eddb70b 2897 temp = I915_READ(reg);
8db9d77b
ZW
2898 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2899
2900 if ((temp & FDI_RX_BIT_LOCK)) {
2901 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2902 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2903 break;
2904 }
8db9d77b 2905 }
e1a44743 2906 if (tries == 5)
5eddb70b 2907 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2908
2909 /* Train 2 */
5eddb70b
CW
2910 reg = FDI_TX_CTL(pipe);
2911 temp = I915_READ(reg);
8db9d77b
ZW
2912 temp &= ~FDI_LINK_TRAIN_NONE;
2913 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2914 I915_WRITE(reg, temp);
8db9d77b 2915
5eddb70b
CW
2916 reg = FDI_RX_CTL(pipe);
2917 temp = I915_READ(reg);
8db9d77b
ZW
2918 temp &= ~FDI_LINK_TRAIN_NONE;
2919 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2920 I915_WRITE(reg, temp);
8db9d77b 2921
5eddb70b
CW
2922 POSTING_READ(reg);
2923 udelay(150);
8db9d77b 2924
5eddb70b 2925 reg = FDI_RX_IIR(pipe);
e1a44743 2926 for (tries = 0; tries < 5; tries++) {
5eddb70b 2927 temp = I915_READ(reg);
8db9d77b
ZW
2928 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2929
2930 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2931 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2932 DRM_DEBUG_KMS("FDI train 2 done.\n");
2933 break;
2934 }
8db9d77b 2935 }
e1a44743 2936 if (tries == 5)
5eddb70b 2937 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2938
2939 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2940
8db9d77b
ZW
2941}
2942
0206e353 2943static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2944 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2945 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2946 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2947 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2948};
2949
2950/* The FDI link training functions for SNB/Cougarpoint. */
2951static void gen6_fdi_link_train(struct drm_crtc *crtc)
2952{
2953 struct drm_device *dev = crtc->dev;
2954 struct drm_i915_private *dev_priv = dev->dev_private;
2955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2956 int pipe = intel_crtc->pipe;
fa37d39e 2957 u32 reg, temp, i, retry;
8db9d77b 2958
e1a44743
AJ
2959 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2960 for train result */
5eddb70b
CW
2961 reg = FDI_RX_IMR(pipe);
2962 temp = I915_READ(reg);
e1a44743
AJ
2963 temp &= ~FDI_RX_SYMBOL_LOCK;
2964 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2965 I915_WRITE(reg, temp);
2966
2967 POSTING_READ(reg);
e1a44743
AJ
2968 udelay(150);
2969
8db9d77b 2970 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2971 reg = FDI_TX_CTL(pipe);
2972 temp = I915_READ(reg);
627eb5a3
DV
2973 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2974 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2975 temp &= ~FDI_LINK_TRAIN_NONE;
2976 temp |= FDI_LINK_TRAIN_PATTERN_1;
2977 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2978 /* SNB-B */
2979 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 2980 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2981
d74cf324
DV
2982 I915_WRITE(FDI_RX_MISC(pipe),
2983 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2984
5eddb70b
CW
2985 reg = FDI_RX_CTL(pipe);
2986 temp = I915_READ(reg);
8db9d77b
ZW
2987 if (HAS_PCH_CPT(dev)) {
2988 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2989 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2990 } else {
2991 temp &= ~FDI_LINK_TRAIN_NONE;
2992 temp |= FDI_LINK_TRAIN_PATTERN_1;
2993 }
5eddb70b
CW
2994 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2995
2996 POSTING_READ(reg);
8db9d77b
ZW
2997 udelay(150);
2998
0206e353 2999 for (i = 0; i < 4; i++) {
5eddb70b
CW
3000 reg = FDI_TX_CTL(pipe);
3001 temp = I915_READ(reg);
8db9d77b
ZW
3002 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3003 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3004 I915_WRITE(reg, temp);
3005
3006 POSTING_READ(reg);
8db9d77b
ZW
3007 udelay(500);
3008
fa37d39e
SP
3009 for (retry = 0; retry < 5; retry++) {
3010 reg = FDI_RX_IIR(pipe);
3011 temp = I915_READ(reg);
3012 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3013 if (temp & FDI_RX_BIT_LOCK) {
3014 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3015 DRM_DEBUG_KMS("FDI train 1 done.\n");
3016 break;
3017 }
3018 udelay(50);
8db9d77b 3019 }
fa37d39e
SP
3020 if (retry < 5)
3021 break;
8db9d77b
ZW
3022 }
3023 if (i == 4)
5eddb70b 3024 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3025
3026 /* Train 2 */
5eddb70b
CW
3027 reg = FDI_TX_CTL(pipe);
3028 temp = I915_READ(reg);
8db9d77b
ZW
3029 temp &= ~FDI_LINK_TRAIN_NONE;
3030 temp |= FDI_LINK_TRAIN_PATTERN_2;
3031 if (IS_GEN6(dev)) {
3032 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3033 /* SNB-B */
3034 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3035 }
5eddb70b 3036 I915_WRITE(reg, temp);
8db9d77b 3037
5eddb70b
CW
3038 reg = FDI_RX_CTL(pipe);
3039 temp = I915_READ(reg);
8db9d77b
ZW
3040 if (HAS_PCH_CPT(dev)) {
3041 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3042 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3043 } else {
3044 temp &= ~FDI_LINK_TRAIN_NONE;
3045 temp |= FDI_LINK_TRAIN_PATTERN_2;
3046 }
5eddb70b
CW
3047 I915_WRITE(reg, temp);
3048
3049 POSTING_READ(reg);
8db9d77b
ZW
3050 udelay(150);
3051
0206e353 3052 for (i = 0; i < 4; i++) {
5eddb70b
CW
3053 reg = FDI_TX_CTL(pipe);
3054 temp = I915_READ(reg);
8db9d77b
ZW
3055 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3056 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3057 I915_WRITE(reg, temp);
3058
3059 POSTING_READ(reg);
8db9d77b
ZW
3060 udelay(500);
3061
fa37d39e
SP
3062 for (retry = 0; retry < 5; retry++) {
3063 reg = FDI_RX_IIR(pipe);
3064 temp = I915_READ(reg);
3065 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3066 if (temp & FDI_RX_SYMBOL_LOCK) {
3067 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3068 DRM_DEBUG_KMS("FDI train 2 done.\n");
3069 break;
3070 }
3071 udelay(50);
8db9d77b 3072 }
fa37d39e
SP
3073 if (retry < 5)
3074 break;
8db9d77b
ZW
3075 }
3076 if (i == 4)
5eddb70b 3077 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3078
3079 DRM_DEBUG_KMS("FDI train done.\n");
3080}
3081
357555c0
JB
3082/* Manual link training for Ivy Bridge A0 parts */
3083static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3084{
3085 struct drm_device *dev = crtc->dev;
3086 struct drm_i915_private *dev_priv = dev->dev_private;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3088 int pipe = intel_crtc->pipe;
139ccd3f 3089 u32 reg, temp, i, j;
357555c0
JB
3090
3091 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3092 for train result */
3093 reg = FDI_RX_IMR(pipe);
3094 temp = I915_READ(reg);
3095 temp &= ~FDI_RX_SYMBOL_LOCK;
3096 temp &= ~FDI_RX_BIT_LOCK;
3097 I915_WRITE(reg, temp);
3098
3099 POSTING_READ(reg);
3100 udelay(150);
3101
01a415fd
DV
3102 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3103 I915_READ(FDI_RX_IIR(pipe)));
3104
139ccd3f
JB
3105 /* Try each vswing and preemphasis setting twice before moving on */
3106 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3107 /* disable first in case we need to retry */
3108 reg = FDI_TX_CTL(pipe);
3109 temp = I915_READ(reg);
3110 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3111 temp &= ~FDI_TX_ENABLE;
3112 I915_WRITE(reg, temp);
357555c0 3113
139ccd3f
JB
3114 reg = FDI_RX_CTL(pipe);
3115 temp = I915_READ(reg);
3116 temp &= ~FDI_LINK_TRAIN_AUTO;
3117 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3118 temp &= ~FDI_RX_ENABLE;
3119 I915_WRITE(reg, temp);
357555c0 3120
139ccd3f 3121 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3122 reg = FDI_TX_CTL(pipe);
3123 temp = I915_READ(reg);
139ccd3f
JB
3124 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3125 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3126 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3127 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3128 temp |= snb_b_fdi_train_param[j/2];
3129 temp |= FDI_COMPOSITE_SYNC;
3130 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3131
139ccd3f
JB
3132 I915_WRITE(FDI_RX_MISC(pipe),
3133 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3134
139ccd3f 3135 reg = FDI_RX_CTL(pipe);
357555c0 3136 temp = I915_READ(reg);
139ccd3f
JB
3137 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3138 temp |= FDI_COMPOSITE_SYNC;
3139 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3140
139ccd3f
JB
3141 POSTING_READ(reg);
3142 udelay(1); /* should be 0.5us */
357555c0 3143
139ccd3f
JB
3144 for (i = 0; i < 4; i++) {
3145 reg = FDI_RX_IIR(pipe);
3146 temp = I915_READ(reg);
3147 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3148
139ccd3f
JB
3149 if (temp & FDI_RX_BIT_LOCK ||
3150 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3151 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3152 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3153 i);
3154 break;
3155 }
3156 udelay(1); /* should be 0.5us */
3157 }
3158 if (i == 4) {
3159 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3160 continue;
3161 }
357555c0 3162
139ccd3f 3163 /* Train 2 */
357555c0
JB
3164 reg = FDI_TX_CTL(pipe);
3165 temp = I915_READ(reg);
139ccd3f
JB
3166 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3167 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3168 I915_WRITE(reg, temp);
3169
3170 reg = FDI_RX_CTL(pipe);
3171 temp = I915_READ(reg);
3172 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3173 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3174 I915_WRITE(reg, temp);
3175
3176 POSTING_READ(reg);
139ccd3f 3177 udelay(2); /* should be 1.5us */
357555c0 3178
139ccd3f
JB
3179 for (i = 0; i < 4; i++) {
3180 reg = FDI_RX_IIR(pipe);
3181 temp = I915_READ(reg);
3182 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3183
139ccd3f
JB
3184 if (temp & FDI_RX_SYMBOL_LOCK ||
3185 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3186 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3187 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3188 i);
3189 goto train_done;
3190 }
3191 udelay(2); /* should be 1.5us */
357555c0 3192 }
139ccd3f
JB
3193 if (i == 4)
3194 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3195 }
357555c0 3196
139ccd3f 3197train_done:
357555c0
JB
3198 DRM_DEBUG_KMS("FDI train done.\n");
3199}
3200
88cefb6c 3201static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3202{
88cefb6c 3203 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3204 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3205 int pipe = intel_crtc->pipe;
5eddb70b 3206 u32 reg, temp;
79e53945 3207
c64e311e 3208
c98e9dcf 3209 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3210 reg = FDI_RX_CTL(pipe);
3211 temp = I915_READ(reg);
627eb5a3
DV
3212 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3213 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3214 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3215 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3216
3217 POSTING_READ(reg);
c98e9dcf
JB
3218 udelay(200);
3219
3220 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3221 temp = I915_READ(reg);
3222 I915_WRITE(reg, temp | FDI_PCDCLK);
3223
3224 POSTING_READ(reg);
c98e9dcf
JB
3225 udelay(200);
3226
20749730
PZ
3227 /* Enable CPU FDI TX PLL, always on for Ironlake */
3228 reg = FDI_TX_CTL(pipe);
3229 temp = I915_READ(reg);
3230 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3231 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3232
20749730
PZ
3233 POSTING_READ(reg);
3234 udelay(100);
6be4a607 3235 }
0e23b99d
JB
3236}
3237
88cefb6c
DV
3238static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3239{
3240 struct drm_device *dev = intel_crtc->base.dev;
3241 struct drm_i915_private *dev_priv = dev->dev_private;
3242 int pipe = intel_crtc->pipe;
3243 u32 reg, temp;
3244
3245 /* Switch from PCDclk to Rawclk */
3246 reg = FDI_RX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3249
3250 /* Disable CPU FDI TX PLL */
3251 reg = FDI_TX_CTL(pipe);
3252 temp = I915_READ(reg);
3253 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3254
3255 POSTING_READ(reg);
3256 udelay(100);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3261
3262 /* Wait for the clocks to turn off. */
3263 POSTING_READ(reg);
3264 udelay(100);
3265}
3266
0fc932b8
JB
3267static void ironlake_fdi_disable(struct drm_crtc *crtc)
3268{
3269 struct drm_device *dev = crtc->dev;
3270 struct drm_i915_private *dev_priv = dev->dev_private;
3271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3272 int pipe = intel_crtc->pipe;
3273 u32 reg, temp;
3274
3275 /* disable CPU FDI tx and PCH FDI rx */
3276 reg = FDI_TX_CTL(pipe);
3277 temp = I915_READ(reg);
3278 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3279 POSTING_READ(reg);
3280
3281 reg = FDI_RX_CTL(pipe);
3282 temp = I915_READ(reg);
3283 temp &= ~(0x7 << 16);
dfd07d72 3284 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3285 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3286
3287 POSTING_READ(reg);
3288 udelay(100);
3289
3290 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3291 if (HAS_PCH_IBX(dev))
6f06ce18 3292 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3293
3294 /* still set train pattern 1 */
3295 reg = FDI_TX_CTL(pipe);
3296 temp = I915_READ(reg);
3297 temp &= ~FDI_LINK_TRAIN_NONE;
3298 temp |= FDI_LINK_TRAIN_PATTERN_1;
3299 I915_WRITE(reg, temp);
3300
3301 reg = FDI_RX_CTL(pipe);
3302 temp = I915_READ(reg);
3303 if (HAS_PCH_CPT(dev)) {
3304 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3305 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3306 } else {
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
3309 }
3310 /* BPC in FDI rx is consistent with that in PIPECONF */
3311 temp &= ~(0x07 << 16);
dfd07d72 3312 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3313 I915_WRITE(reg, temp);
3314
3315 POSTING_READ(reg);
3316 udelay(100);
3317}
3318
5dce5b93
CW
3319bool intel_has_pending_fb_unpin(struct drm_device *dev)
3320{
3321 struct intel_crtc *crtc;
3322
3323 /* Note that we don't need to be called with mode_config.lock here
3324 * as our list of CRTC objects is static for the lifetime of the
3325 * device and so cannot disappear as we iterate. Similarly, we can
3326 * happily treat the predicates as racy, atomic checks as userspace
3327 * cannot claim and pin a new fb without at least acquring the
3328 * struct_mutex and so serialising with us.
3329 */
d3fcc808 3330 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3331 if (atomic_read(&crtc->unpin_work_count) == 0)
3332 continue;
3333
3334 if (crtc->unpin_work)
3335 intel_wait_for_vblank(dev, crtc->pipe);
3336
3337 return true;
3338 }
3339
3340 return false;
3341}
3342
46a55d30 3343void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3344{
0f91128d 3345 struct drm_device *dev = crtc->dev;
5bb61643 3346 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3347
f4510a27 3348 if (crtc->primary->fb == NULL)
e6c3a2a6
CW
3349 return;
3350
2c10d571
DV
3351 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3352
eed6d67d
DV
3353 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3354 !intel_crtc_has_pending_flip(crtc),
3355 60*HZ) == 0);
5bb61643 3356
0f91128d 3357 mutex_lock(&dev->struct_mutex);
f4510a27 3358 intel_finish_fb(crtc->primary->fb);
0f91128d 3359 mutex_unlock(&dev->struct_mutex);
e6c3a2a6
CW
3360}
3361
e615efe4
ED
3362/* Program iCLKIP clock to the desired frequency */
3363static void lpt_program_iclkip(struct drm_crtc *crtc)
3364{
3365 struct drm_device *dev = crtc->dev;
3366 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3367 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3368 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3369 u32 temp;
3370
09153000
DV
3371 mutex_lock(&dev_priv->dpio_lock);
3372
e615efe4
ED
3373 /* It is necessary to ungate the pixclk gate prior to programming
3374 * the divisors, and gate it back when it is done.
3375 */
3376 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3377
3378 /* Disable SSCCTL */
3379 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3380 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3381 SBI_SSCCTL_DISABLE,
3382 SBI_ICLK);
e615efe4
ED
3383
3384 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3385 if (clock == 20000) {
e615efe4
ED
3386 auxdiv = 1;
3387 divsel = 0x41;
3388 phaseinc = 0x20;
3389 } else {
3390 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3391 * but the adjusted_mode->crtc_clock in in KHz. To get the
3392 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3393 * convert the virtual clock precision to KHz here for higher
3394 * precision.
3395 */
3396 u32 iclk_virtual_root_freq = 172800 * 1000;
3397 u32 iclk_pi_range = 64;
3398 u32 desired_divisor, msb_divisor_value, pi_value;
3399
12d7ceed 3400 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3401 msb_divisor_value = desired_divisor / iclk_pi_range;
3402 pi_value = desired_divisor % iclk_pi_range;
3403
3404 auxdiv = 0;
3405 divsel = msb_divisor_value - 2;
3406 phaseinc = pi_value;
3407 }
3408
3409 /* This should not happen with any sane values */
3410 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3411 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3412 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3413 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3414
3415 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3416 clock,
e615efe4
ED
3417 auxdiv,
3418 divsel,
3419 phasedir,
3420 phaseinc);
3421
3422 /* Program SSCDIVINTPHASE6 */
988d6ee8 3423 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3424 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3425 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3426 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3427 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3428 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3429 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3430 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3431
3432 /* Program SSCAUXDIV */
988d6ee8 3433 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3434 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3435 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3436 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3437
3438 /* Enable modulator and associated divider */
988d6ee8 3439 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3440 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3441 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3442
3443 /* Wait for initialization time */
3444 udelay(24);
3445
3446 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3447
3448 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3449}
3450
275f01b2
DV
3451static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3452 enum pipe pch_transcoder)
3453{
3454 struct drm_device *dev = crtc->base.dev;
3455 struct drm_i915_private *dev_priv = dev->dev_private;
3456 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3457
3458 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3459 I915_READ(HTOTAL(cpu_transcoder)));
3460 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3461 I915_READ(HBLANK(cpu_transcoder)));
3462 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3463 I915_READ(HSYNC(cpu_transcoder)));
3464
3465 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3466 I915_READ(VTOTAL(cpu_transcoder)));
3467 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3468 I915_READ(VBLANK(cpu_transcoder)));
3469 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3470 I915_READ(VSYNC(cpu_transcoder)));
3471 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3472 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3473}
3474
1fbc0d78
DV
3475static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3476{
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 uint32_t temp;
3479
3480 temp = I915_READ(SOUTH_CHICKEN1);
3481 if (temp & FDI_BC_BIFURCATION_SELECT)
3482 return;
3483
3484 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3485 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3486
3487 temp |= FDI_BC_BIFURCATION_SELECT;
3488 DRM_DEBUG_KMS("enabling fdi C rx\n");
3489 I915_WRITE(SOUTH_CHICKEN1, temp);
3490 POSTING_READ(SOUTH_CHICKEN1);
3491}
3492
3493static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3494{
3495 struct drm_device *dev = intel_crtc->base.dev;
3496 struct drm_i915_private *dev_priv = dev->dev_private;
3497
3498 switch (intel_crtc->pipe) {
3499 case PIPE_A:
3500 break;
3501 case PIPE_B:
3502 if (intel_crtc->config.fdi_lanes > 2)
3503 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3504 else
3505 cpt_enable_fdi_bc_bifurcation(dev);
3506
3507 break;
3508 case PIPE_C:
3509 cpt_enable_fdi_bc_bifurcation(dev);
3510
3511 break;
3512 default:
3513 BUG();
3514 }
3515}
3516
f67a559d
JB
3517/*
3518 * Enable PCH resources required for PCH ports:
3519 * - PCH PLLs
3520 * - FDI training & RX/TX
3521 * - update transcoder timings
3522 * - DP transcoding bits
3523 * - transcoder
3524 */
3525static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3526{
3527 struct drm_device *dev = crtc->dev;
3528 struct drm_i915_private *dev_priv = dev->dev_private;
3529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3530 int pipe = intel_crtc->pipe;
ee7b9f93 3531 u32 reg, temp;
2c07245f 3532
ab9412ba 3533 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3534
1fbc0d78
DV
3535 if (IS_IVYBRIDGE(dev))
3536 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3537
cd986abb
DV
3538 /* Write the TU size bits before fdi link training, so that error
3539 * detection works. */
3540 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3541 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3542
c98e9dcf 3543 /* For PCH output, training FDI link */
674cf967 3544 dev_priv->display.fdi_link_train(crtc);
2c07245f 3545
3ad8a208
DV
3546 /* We need to program the right clock selection before writing the pixel
3547 * mutliplier into the DPLL. */
303b81e0 3548 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3549 u32 sel;
4b645f14 3550
c98e9dcf 3551 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3552 temp |= TRANS_DPLL_ENABLE(pipe);
3553 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3554 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3555 temp |= sel;
3556 else
3557 temp &= ~sel;
c98e9dcf 3558 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3559 }
5eddb70b 3560
3ad8a208
DV
3561 /* XXX: pch pll's can be enabled any time before we enable the PCH
3562 * transcoder, and we actually should do this to not upset any PCH
3563 * transcoder that already use the clock when we share it.
3564 *
3565 * Note that enable_shared_dpll tries to do the right thing, but
3566 * get_shared_dpll unconditionally resets the pll - we need that to have
3567 * the right LVDS enable sequence. */
85b3894f 3568 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3569
d9b6cb56
JB
3570 /* set transcoder timing, panel must allow it */
3571 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3572 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3573
303b81e0 3574 intel_fdi_normal_train(crtc);
5e84e1a4 3575
c98e9dcf
JB
3576 /* For PCH DP, enable TRANS_DP_CTL */
3577 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3578 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3579 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3580 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3581 reg = TRANS_DP_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3584 TRANS_DP_SYNC_MASK |
3585 TRANS_DP_BPC_MASK);
5eddb70b
CW
3586 temp |= (TRANS_DP_OUTPUT_ENABLE |
3587 TRANS_DP_ENH_FRAMING);
9325c9f0 3588 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3589
3590 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3591 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3592 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3593 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3594
3595 switch (intel_trans_dp_port_sel(crtc)) {
3596 case PCH_DP_B:
5eddb70b 3597 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3598 break;
3599 case PCH_DP_C:
5eddb70b 3600 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3601 break;
3602 case PCH_DP_D:
5eddb70b 3603 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3604 break;
3605 default:
e95d41e1 3606 BUG();
32f9d658 3607 }
2c07245f 3608
5eddb70b 3609 I915_WRITE(reg, temp);
6be4a607 3610 }
b52eb4dc 3611
b8a4f404 3612 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3613}
3614
1507e5bd
PZ
3615static void lpt_pch_enable(struct drm_crtc *crtc)
3616{
3617 struct drm_device *dev = crtc->dev;
3618 struct drm_i915_private *dev_priv = dev->dev_private;
3619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3620 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3621
ab9412ba 3622 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3623
8c52b5e8 3624 lpt_program_iclkip(crtc);
1507e5bd 3625
0540e488 3626 /* Set transcoder timing. */
275f01b2 3627 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3628
937bb610 3629 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3630}
3631
e2b78267 3632static void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3633{
e2b78267 3634 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3635
3636 if (pll == NULL)
3637 return;
3638
3639 if (pll->refcount == 0) {
46edb027 3640 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3641 return;
3642 }
3643
f4a091c7
DV
3644 if (--pll->refcount == 0) {
3645 WARN_ON(pll->on);
3646 WARN_ON(pll->active);
3647 }
3648
a43f6e0f 3649 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3650}
3651
b89a1d39 3652static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3653{
e2b78267
DV
3654 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3655 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3656 enum intel_dpll_id i;
ee7b9f93 3657
ee7b9f93 3658 if (pll) {
46edb027
DV
3659 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3660 crtc->base.base.id, pll->name);
e2b78267 3661 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3662 }
3663
98b6bd99
DV
3664 if (HAS_PCH_IBX(dev_priv->dev)) {
3665 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3666 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3667 pll = &dev_priv->shared_dplls[i];
98b6bd99 3668
46edb027
DV
3669 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3670 crtc->base.base.id, pll->name);
98b6bd99 3671
f2a69f44
DV
3672 WARN_ON(pll->refcount);
3673
98b6bd99
DV
3674 goto found;
3675 }
3676
e72f9fbf
DV
3677 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3678 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3679
3680 /* Only want to check enabled timings first */
3681 if (pll->refcount == 0)
3682 continue;
3683
b89a1d39
DV
3684 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3685 sizeof(pll->hw_state)) == 0) {
46edb027 3686 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3687 crtc->base.base.id,
46edb027 3688 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3689
3690 goto found;
3691 }
3692 }
3693
3694 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3695 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3696 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3697 if (pll->refcount == 0) {
46edb027
DV
3698 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3699 crtc->base.base.id, pll->name);
ee7b9f93
JB
3700 goto found;
3701 }
3702 }
3703
3704 return NULL;
3705
3706found:
f2a69f44
DV
3707 if (pll->refcount == 0)
3708 pll->hw_state = crtc->config.dpll_hw_state;
3709
a43f6e0f 3710 crtc->config.shared_dpll = i;
46edb027
DV
3711 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3712 pipe_name(crtc->pipe));
ee7b9f93 3713
cdbd2316 3714 pll->refcount++;
e04c7350 3715
ee7b9f93
JB
3716 return pll;
3717}
3718
a1520318 3719static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3720{
3721 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3722 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3723 u32 temp;
3724
3725 temp = I915_READ(dslreg);
3726 udelay(500);
3727 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3728 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3729 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3730 }
3731}
3732
b074cec8
JB
3733static void ironlake_pfit_enable(struct intel_crtc *crtc)
3734{
3735 struct drm_device *dev = crtc->base.dev;
3736 struct drm_i915_private *dev_priv = dev->dev_private;
3737 int pipe = crtc->pipe;
3738
fd4daa9c 3739 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3740 /* Force use of hard-coded filter coefficients
3741 * as some pre-programmed values are broken,
3742 * e.g. x201.
3743 */
3744 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3745 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3746 PF_PIPE_SEL_IVB(pipe));
3747 else
3748 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3749 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3750 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3751 }
3752}
3753
bb53d4ae
VS
3754static void intel_enable_planes(struct drm_crtc *crtc)
3755{
3756 struct drm_device *dev = crtc->dev;
3757 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3758 struct drm_plane *plane;
bb53d4ae
VS
3759 struct intel_plane *intel_plane;
3760
af2b653b
MR
3761 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3762 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3763 if (intel_plane->pipe == pipe)
3764 intel_plane_restore(&intel_plane->base);
af2b653b 3765 }
bb53d4ae
VS
3766}
3767
3768static void intel_disable_planes(struct drm_crtc *crtc)
3769{
3770 struct drm_device *dev = crtc->dev;
3771 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3772 struct drm_plane *plane;
bb53d4ae
VS
3773 struct intel_plane *intel_plane;
3774
af2b653b
MR
3775 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3776 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3777 if (intel_plane->pipe == pipe)
3778 intel_plane_disable(&intel_plane->base);
af2b653b 3779 }
bb53d4ae
VS
3780}
3781
20bc8673 3782void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3783{
cea165c3
VS
3784 struct drm_device *dev = crtc->base.dev;
3785 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3786
3787 if (!crtc->config.ips_enabled)
3788 return;
3789
cea165c3
VS
3790 /* We can only enable IPS after we enable a plane and wait for a vblank */
3791 intel_wait_for_vblank(dev, crtc->pipe);
3792
d77e4531 3793 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3794 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3795 mutex_lock(&dev_priv->rps.hw_lock);
3796 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3797 mutex_unlock(&dev_priv->rps.hw_lock);
3798 /* Quoting Art Runyan: "its not safe to expect any particular
3799 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3800 * mailbox." Moreover, the mailbox may return a bogus state,
3801 * so we need to just enable it and continue on.
2a114cc1
BW
3802 */
3803 } else {
3804 I915_WRITE(IPS_CTL, IPS_ENABLE);
3805 /* The bit only becomes 1 in the next vblank, so this wait here
3806 * is essentially intel_wait_for_vblank. If we don't have this
3807 * and don't wait for vblanks until the end of crtc_enable, then
3808 * the HW state readout code will complain that the expected
3809 * IPS_CTL value is not the one we read. */
3810 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3811 DRM_ERROR("Timed out waiting for IPS enable\n");
3812 }
d77e4531
PZ
3813}
3814
20bc8673 3815void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3816{
3817 struct drm_device *dev = crtc->base.dev;
3818 struct drm_i915_private *dev_priv = dev->dev_private;
3819
3820 if (!crtc->config.ips_enabled)
3821 return;
3822
3823 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3824 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3825 mutex_lock(&dev_priv->rps.hw_lock);
3826 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3827 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3828 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3829 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3830 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3831 } else {
2a114cc1 3832 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3833 POSTING_READ(IPS_CTL);
3834 }
d77e4531
PZ
3835
3836 /* We need to wait for a vblank before we can disable the plane. */
3837 intel_wait_for_vblank(dev, crtc->pipe);
3838}
3839
3840/** Loads the palette/gamma unit for the CRTC with the prepared values */
3841static void intel_crtc_load_lut(struct drm_crtc *crtc)
3842{
3843 struct drm_device *dev = crtc->dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846 enum pipe pipe = intel_crtc->pipe;
3847 int palreg = PALETTE(pipe);
3848 int i;
3849 bool reenable_ips = false;
3850
3851 /* The clocks have to be on to load the palette. */
3852 if (!crtc->enabled || !intel_crtc->active)
3853 return;
3854
3855 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3856 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3857 assert_dsi_pll_enabled(dev_priv);
3858 else
3859 assert_pll_enabled(dev_priv, pipe);
3860 }
3861
3862 /* use legacy palette for Ironlake */
3863 if (HAS_PCH_SPLIT(dev))
3864 palreg = LGC_PALETTE(pipe);
3865
3866 /* Workaround : Do not read or write the pipe palette/gamma data while
3867 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3868 */
41e6fc4c 3869 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3870 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3871 GAMMA_MODE_MODE_SPLIT)) {
3872 hsw_disable_ips(intel_crtc);
3873 reenable_ips = true;
3874 }
3875
3876 for (i = 0; i < 256; i++) {
3877 I915_WRITE(palreg + 4 * i,
3878 (intel_crtc->lut_r[i] << 16) |
3879 (intel_crtc->lut_g[i] << 8) |
3880 intel_crtc->lut_b[i]);
3881 }
3882
3883 if (reenable_ips)
3884 hsw_enable_ips(intel_crtc);
3885}
3886
d3eedb1a
VS
3887static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3888{
3889 if (!enable && intel_crtc->overlay) {
3890 struct drm_device *dev = intel_crtc->base.dev;
3891 struct drm_i915_private *dev_priv = dev->dev_private;
3892
3893 mutex_lock(&dev->struct_mutex);
3894 dev_priv->mm.interruptible = false;
3895 (void) intel_overlay_switch_off(intel_crtc->overlay);
3896 dev_priv->mm.interruptible = true;
3897 mutex_unlock(&dev->struct_mutex);
3898 }
3899
3900 /* Let userspace switch the overlay on again. In most cases userspace
3901 * has to recompute where to put it anyway.
3902 */
3903}
3904
3905/**
3906 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3907 * cursor plane briefly if not already running after enabling the display
3908 * plane.
3909 * This workaround avoids occasional blank screens when self refresh is
3910 * enabled.
3911 */
3912static void
3913g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3914{
3915 u32 cntl = I915_READ(CURCNTR(pipe));
3916
3917 if ((cntl & CURSOR_MODE) == 0) {
3918 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3919
3920 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3921 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3922 intel_wait_for_vblank(dev_priv->dev, pipe);
3923 I915_WRITE(CURCNTR(pipe), cntl);
3924 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3925 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3926 }
3927}
3928
3929static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
3933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3934 int pipe = intel_crtc->pipe;
3935 int plane = intel_crtc->plane;
3936
f98551ae
VS
3937 drm_vblank_on(dev, pipe);
3938
a5c4d7bc
VS
3939 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3940 intel_enable_planes(crtc);
d3eedb1a
VS
3941 /* The fixup needs to happen before cursor is enabled */
3942 if (IS_G4X(dev))
3943 g4x_fixup_plane(dev_priv, pipe);
a5c4d7bc 3944 intel_crtc_update_cursor(crtc, true);
d3eedb1a 3945 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
3946
3947 hsw_enable_ips(intel_crtc);
3948
3949 mutex_lock(&dev->struct_mutex);
3950 intel_update_fbc(dev);
3951 mutex_unlock(&dev->struct_mutex);
3952}
3953
d3eedb1a 3954static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3955{
3956 struct drm_device *dev = crtc->dev;
3957 struct drm_i915_private *dev_priv = dev->dev_private;
3958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3959 int pipe = intel_crtc->pipe;
3960 int plane = intel_crtc->plane;
3961
3962 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
3963
3964 if (dev_priv->fbc.plane == plane)
3965 intel_disable_fbc(dev);
3966
3967 hsw_disable_ips(intel_crtc);
3968
d3eedb1a 3969 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
3970 intel_crtc_update_cursor(crtc, false);
3971 intel_disable_planes(crtc);
3972 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
f98551ae
VS
3973
3974 drm_vblank_off(dev, pipe);
a5c4d7bc
VS
3975}
3976
f67a559d
JB
3977static void ironlake_crtc_enable(struct drm_crtc *crtc)
3978{
3979 struct drm_device *dev = crtc->dev;
3980 struct drm_i915_private *dev_priv = dev->dev_private;
3981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3982 struct intel_encoder *encoder;
f67a559d 3983 int pipe = intel_crtc->pipe;
29407aab 3984 enum plane plane = intel_crtc->plane;
f67a559d 3985
08a48469
DV
3986 WARN_ON(!crtc->enabled);
3987
f67a559d
JB
3988 if (intel_crtc->active)
3989 return;
3990
b14b1055
DV
3991 if (intel_crtc->config.has_pch_encoder)
3992 intel_prepare_shared_dpll(intel_crtc);
3993
29407aab
DV
3994 if (intel_crtc->config.has_dp_encoder)
3995 intel_dp_set_m_n(intel_crtc);
3996
3997 intel_set_pipe_timings(intel_crtc);
3998
3999 if (intel_crtc->config.has_pch_encoder) {
4000 intel_cpu_transcoder_set_m_n(intel_crtc,
4001 &intel_crtc->config.fdi_m_n);
4002 }
4003
4004 ironlake_set_pipeconf(crtc);
4005
4006 /* Set up the display plane register */
4007 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
4008 POSTING_READ(DSPCNTR(plane));
4009
4010 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4011 crtc->x, crtc->y);
4012
f67a559d 4013 intel_crtc->active = true;
8664281b
PZ
4014
4015 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4016 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4017
f6736a1a 4018 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4019 if (encoder->pre_enable)
4020 encoder->pre_enable(encoder);
f67a559d 4021
5bfe2ac0 4022 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4023 /* Note: FDI PLL enabling _must_ be done before we enable the
4024 * cpu pipes, hence this is separate from all the other fdi/pch
4025 * enabling. */
88cefb6c 4026 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4027 } else {
4028 assert_fdi_tx_disabled(dev_priv, pipe);
4029 assert_fdi_rx_disabled(dev_priv, pipe);
4030 }
f67a559d 4031
b074cec8 4032 ironlake_pfit_enable(intel_crtc);
f67a559d 4033
9c54c0dd
JB
4034 /*
4035 * On ILK+ LUT must be loaded before the pipe is running but with
4036 * clocks enabled
4037 */
4038 intel_crtc_load_lut(crtc);
4039
f37fcc2a 4040 intel_update_watermarks(crtc);
e1fdc473 4041 intel_enable_pipe(intel_crtc);
f67a559d 4042
5bfe2ac0 4043 if (intel_crtc->config.has_pch_encoder)
f67a559d 4044 ironlake_pch_enable(crtc);
c98e9dcf 4045
fa5c73b1
DV
4046 for_each_encoder_on_crtc(dev, crtc, encoder)
4047 encoder->enable(encoder);
61b77ddd
DV
4048
4049 if (HAS_PCH_CPT(dev))
a1520318 4050 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4051
d3eedb1a 4052 intel_crtc_enable_planes(crtc);
6be4a607
JB
4053}
4054
42db64ef
PZ
4055/* IPS only exists on ULT machines and is tied to pipe A. */
4056static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4057{
f5adf94e 4058 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4059}
4060
e4916946
PZ
4061/*
4062 * This implements the workaround described in the "notes" section of the mode
4063 * set sequence documentation. When going from no pipes or single pipe to
4064 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4065 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4066 */
4067static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4068{
4069 struct drm_device *dev = crtc->base.dev;
4070 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4071
4072 /* We want to get the other_active_crtc only if there's only 1 other
4073 * active crtc. */
d3fcc808 4074 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4075 if (!crtc_it->active || crtc_it == crtc)
4076 continue;
4077
4078 if (other_active_crtc)
4079 return;
4080
4081 other_active_crtc = crtc_it;
4082 }
4083 if (!other_active_crtc)
4084 return;
4085
4086 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4087 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4088}
4089
4f771f10
PZ
4090static void haswell_crtc_enable(struct drm_crtc *crtc)
4091{
4092 struct drm_device *dev = crtc->dev;
4093 struct drm_i915_private *dev_priv = dev->dev_private;
4094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4095 struct intel_encoder *encoder;
4096 int pipe = intel_crtc->pipe;
229fca97 4097 enum plane plane = intel_crtc->plane;
4f771f10
PZ
4098
4099 WARN_ON(!crtc->enabled);
4100
4101 if (intel_crtc->active)
4102 return;
4103
229fca97
DV
4104 if (intel_crtc->config.has_dp_encoder)
4105 intel_dp_set_m_n(intel_crtc);
4106
4107 intel_set_pipe_timings(intel_crtc);
4108
4109 if (intel_crtc->config.has_pch_encoder) {
4110 intel_cpu_transcoder_set_m_n(intel_crtc,
4111 &intel_crtc->config.fdi_m_n);
4112 }
4113
4114 haswell_set_pipeconf(crtc);
4115
4116 intel_set_pipe_csc(crtc);
4117
4118 /* Set up the display plane register */
4119 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4120 POSTING_READ(DSPCNTR(plane));
4121
4122 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4123 crtc->x, crtc->y);
4124
4f771f10 4125 intel_crtc->active = true;
8664281b
PZ
4126
4127 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4128 if (intel_crtc->config.has_pch_encoder)
4129 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4130
5bfe2ac0 4131 if (intel_crtc->config.has_pch_encoder)
04945641 4132 dev_priv->display.fdi_link_train(crtc);
4f771f10
PZ
4133
4134 for_each_encoder_on_crtc(dev, crtc, encoder)
4135 if (encoder->pre_enable)
4136 encoder->pre_enable(encoder);
4137
1f544388 4138 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4139
b074cec8 4140 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4141
4142 /*
4143 * On ILK+ LUT must be loaded before the pipe is running but with
4144 * clocks enabled
4145 */
4146 intel_crtc_load_lut(crtc);
4147
1f544388 4148 intel_ddi_set_pipe_settings(crtc);
8228c251 4149 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4150
f37fcc2a 4151 intel_update_watermarks(crtc);
e1fdc473 4152 intel_enable_pipe(intel_crtc);
42db64ef 4153
5bfe2ac0 4154 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4155 lpt_pch_enable(crtc);
4f771f10 4156
8807e55b 4157 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4158 encoder->enable(encoder);
8807e55b
JN
4159 intel_opregion_notify_encoder(encoder, true);
4160 }
4f771f10 4161
e4916946
PZ
4162 /* If we change the relative order between pipe/planes enabling, we need
4163 * to change the workaround. */
4164 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4165 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4166}
4167
3f8dce3a
DV
4168static void ironlake_pfit_disable(struct intel_crtc *crtc)
4169{
4170 struct drm_device *dev = crtc->base.dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 int pipe = crtc->pipe;
4173
4174 /* To avoid upsetting the power well on haswell only disable the pfit if
4175 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4176 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4177 I915_WRITE(PF_CTL(pipe), 0);
4178 I915_WRITE(PF_WIN_POS(pipe), 0);
4179 I915_WRITE(PF_WIN_SZ(pipe), 0);
4180 }
4181}
4182
6be4a607
JB
4183static void ironlake_crtc_disable(struct drm_crtc *crtc)
4184{
4185 struct drm_device *dev = crtc->dev;
4186 struct drm_i915_private *dev_priv = dev->dev_private;
4187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4188 struct intel_encoder *encoder;
6be4a607 4189 int pipe = intel_crtc->pipe;
5eddb70b 4190 u32 reg, temp;
b52eb4dc 4191
f7abfe8b
CW
4192 if (!intel_crtc->active)
4193 return;
4194
d3eedb1a 4195 intel_crtc_disable_planes(crtc);
a5c4d7bc 4196
ea9d758d
DV
4197 for_each_encoder_on_crtc(dev, crtc, encoder)
4198 encoder->disable(encoder);
4199
d925c59a
DV
4200 if (intel_crtc->config.has_pch_encoder)
4201 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4202
b24e7179 4203 intel_disable_pipe(dev_priv, pipe);
32f9d658 4204
3f8dce3a 4205 ironlake_pfit_disable(intel_crtc);
2c07245f 4206
bf49ec8c
DV
4207 for_each_encoder_on_crtc(dev, crtc, encoder)
4208 if (encoder->post_disable)
4209 encoder->post_disable(encoder);
2c07245f 4210
d925c59a
DV
4211 if (intel_crtc->config.has_pch_encoder) {
4212 ironlake_fdi_disable(crtc);
913d8d11 4213
d925c59a
DV
4214 ironlake_disable_pch_transcoder(dev_priv, pipe);
4215 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4216
d925c59a
DV
4217 if (HAS_PCH_CPT(dev)) {
4218 /* disable TRANS_DP_CTL */
4219 reg = TRANS_DP_CTL(pipe);
4220 temp = I915_READ(reg);
4221 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4222 TRANS_DP_PORT_SEL_MASK);
4223 temp |= TRANS_DP_PORT_SEL_NONE;
4224 I915_WRITE(reg, temp);
4225
4226 /* disable DPLL_SEL */
4227 temp = I915_READ(PCH_DPLL_SEL);
11887397 4228 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4229 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4230 }
e3421a18 4231
d925c59a 4232 /* disable PCH DPLL */
e72f9fbf 4233 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4234
d925c59a
DV
4235 ironlake_fdi_pll_disable(intel_crtc);
4236 }
6b383a7f 4237
f7abfe8b 4238 intel_crtc->active = false;
46ba614c 4239 intel_update_watermarks(crtc);
d1ebd816
BW
4240
4241 mutex_lock(&dev->struct_mutex);
6b383a7f 4242 intel_update_fbc(dev);
d1ebd816 4243 mutex_unlock(&dev->struct_mutex);
6be4a607 4244}
1b3c7a47 4245
4f771f10 4246static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4247{
4f771f10
PZ
4248 struct drm_device *dev = crtc->dev;
4249 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
4251 struct intel_encoder *encoder;
4252 int pipe = intel_crtc->pipe;
3b117c8f 4253 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4254
4f771f10
PZ
4255 if (!intel_crtc->active)
4256 return;
4257
d3eedb1a 4258 intel_crtc_disable_planes(crtc);
dda9a66a 4259
8807e55b
JN
4260 for_each_encoder_on_crtc(dev, crtc, encoder) {
4261 intel_opregion_notify_encoder(encoder, false);
4f771f10 4262 encoder->disable(encoder);
8807e55b 4263 }
4f771f10 4264
8664281b
PZ
4265 if (intel_crtc->config.has_pch_encoder)
4266 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
4267 intel_disable_pipe(dev_priv, pipe);
4268
ad80a810 4269 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4270
3f8dce3a 4271 ironlake_pfit_disable(intel_crtc);
4f771f10 4272
1f544388 4273 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10
PZ
4274
4275 for_each_encoder_on_crtc(dev, crtc, encoder)
4276 if (encoder->post_disable)
4277 encoder->post_disable(encoder);
4278
88adfff1 4279 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4280 lpt_disable_pch_transcoder(dev_priv);
8664281b 4281 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4282 intel_ddi_fdi_disable(crtc);
83616634 4283 }
4f771f10
PZ
4284
4285 intel_crtc->active = false;
46ba614c 4286 intel_update_watermarks(crtc);
4f771f10
PZ
4287
4288 mutex_lock(&dev->struct_mutex);
4289 intel_update_fbc(dev);
4290 mutex_unlock(&dev->struct_mutex);
4291}
4292
ee7b9f93
JB
4293static void ironlake_crtc_off(struct drm_crtc *crtc)
4294{
4295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4296 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4297}
4298
6441ab5f
PZ
4299static void haswell_crtc_off(struct drm_crtc *crtc)
4300{
4301 intel_ddi_put_crtc_pll(crtc);
4302}
4303
2dd24552
JB
4304static void i9xx_pfit_enable(struct intel_crtc *crtc)
4305{
4306 struct drm_device *dev = crtc->base.dev;
4307 struct drm_i915_private *dev_priv = dev->dev_private;
4308 struct intel_crtc_config *pipe_config = &crtc->config;
4309
328d8e82 4310 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4311 return;
4312
2dd24552 4313 /*
c0b03411
DV
4314 * The panel fitter should only be adjusted whilst the pipe is disabled,
4315 * according to register description and PRM.
2dd24552 4316 */
c0b03411
DV
4317 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4318 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4319
b074cec8
JB
4320 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4321 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4322
4323 /* Border color in case we don't scale up to the full screen. Black by
4324 * default, change to something else for debugging. */
4325 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4326}
4327
77d22dca
ID
4328#define for_each_power_domain(domain, mask) \
4329 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4330 if ((1 << (domain)) & (mask))
4331
319be8ae
ID
4332enum intel_display_power_domain
4333intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4334{
4335 struct drm_device *dev = intel_encoder->base.dev;
4336 struct intel_digital_port *intel_dig_port;
4337
4338 switch (intel_encoder->type) {
4339 case INTEL_OUTPUT_UNKNOWN:
4340 /* Only DDI platforms should ever use this output type */
4341 WARN_ON_ONCE(!HAS_DDI(dev));
4342 case INTEL_OUTPUT_DISPLAYPORT:
4343 case INTEL_OUTPUT_HDMI:
4344 case INTEL_OUTPUT_EDP:
4345 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4346 switch (intel_dig_port->port) {
4347 case PORT_A:
4348 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4349 case PORT_B:
4350 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4351 case PORT_C:
4352 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4353 case PORT_D:
4354 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4355 default:
4356 WARN_ON_ONCE(1);
4357 return POWER_DOMAIN_PORT_OTHER;
4358 }
4359 case INTEL_OUTPUT_ANALOG:
4360 return POWER_DOMAIN_PORT_CRT;
4361 case INTEL_OUTPUT_DSI:
4362 return POWER_DOMAIN_PORT_DSI;
4363 default:
4364 return POWER_DOMAIN_PORT_OTHER;
4365 }
4366}
4367
4368static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4369{
319be8ae
ID
4370 struct drm_device *dev = crtc->dev;
4371 struct intel_encoder *intel_encoder;
4372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4373 enum pipe pipe = intel_crtc->pipe;
4374 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
77d22dca
ID
4375 unsigned long mask;
4376 enum transcoder transcoder;
4377
4378 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4379
4380 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4381 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4382 if (pfit_enabled)
4383 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4384
319be8ae
ID
4385 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4386 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4387
77d22dca
ID
4388 return mask;
4389}
4390
4391void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4392 bool enable)
4393{
4394 if (dev_priv->power_domains.init_power_on == enable)
4395 return;
4396
4397 if (enable)
4398 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4399 else
4400 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4401
4402 dev_priv->power_domains.init_power_on = enable;
4403}
4404
4405static void modeset_update_crtc_power_domains(struct drm_device *dev)
4406{
4407 struct drm_i915_private *dev_priv = dev->dev_private;
4408 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4409 struct intel_crtc *crtc;
4410
4411 /*
4412 * First get all needed power domains, then put all unneeded, to avoid
4413 * any unnecessary toggling of the power wells.
4414 */
d3fcc808 4415 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4416 enum intel_display_power_domain domain;
4417
4418 if (!crtc->base.enabled)
4419 continue;
4420
319be8ae 4421 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4422
4423 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4424 intel_display_power_get(dev_priv, domain);
4425 }
4426
d3fcc808 4427 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4428 enum intel_display_power_domain domain;
4429
4430 for_each_power_domain(domain, crtc->enabled_power_domains)
4431 intel_display_power_put(dev_priv, domain);
4432
4433 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4434 }
4435
4436 intel_display_set_init_power(dev_priv, false);
4437}
4438
586f49dc 4439int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4440{
586f49dc 4441 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4442
586f49dc
JB
4443 /* Obtain SKU information */
4444 mutex_lock(&dev_priv->dpio_lock);
4445 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4446 CCK_FUSE_HPLL_FREQ_MASK;
4447 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4448
586f49dc 4449 return vco_freq[hpll_freq];
30a970c6
JB
4450}
4451
4452/* Adjust CDclk dividers to allow high res or save power if possible */
4453static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4454{
4455 struct drm_i915_private *dev_priv = dev->dev_private;
4456 u32 val, cmd;
4457
d60c4473
ID
4458 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4459 dev_priv->vlv_cdclk_freq = cdclk;
4460
30a970c6
JB
4461 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4462 cmd = 2;
4463 else if (cdclk == 266)
4464 cmd = 1;
4465 else
4466 cmd = 0;
4467
4468 mutex_lock(&dev_priv->rps.hw_lock);
4469 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4470 val &= ~DSPFREQGUAR_MASK;
4471 val |= (cmd << DSPFREQGUAR_SHIFT);
4472 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4473 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4474 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4475 50)) {
4476 DRM_ERROR("timed out waiting for CDclk change\n");
4477 }
4478 mutex_unlock(&dev_priv->rps.hw_lock);
4479
4480 if (cdclk == 400) {
4481 u32 divider, vco;
4482
4483 vco = valleyview_get_vco(dev_priv);
4484 divider = ((vco << 1) / cdclk) - 1;
4485
4486 mutex_lock(&dev_priv->dpio_lock);
4487 /* adjust cdclk divider */
4488 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4489 val &= ~0xf;
4490 val |= divider;
4491 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4492 mutex_unlock(&dev_priv->dpio_lock);
4493 }
4494
4495 mutex_lock(&dev_priv->dpio_lock);
4496 /* adjust self-refresh exit latency value */
4497 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4498 val &= ~0x7f;
4499
4500 /*
4501 * For high bandwidth configs, we set a higher latency in the bunit
4502 * so that the core display fetch happens in time to avoid underruns.
4503 */
4504 if (cdclk == 400)
4505 val |= 4500 / 250; /* 4.5 usec */
4506 else
4507 val |= 3000 / 250; /* 3.0 usec */
4508 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4509 mutex_unlock(&dev_priv->dpio_lock);
4510
4511 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4512 intel_i2c_reset(dev);
4513}
4514
d60c4473 4515int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4516{
4517 int cur_cdclk, vco;
4518 int divider;
4519
4520 vco = valleyview_get_vco(dev_priv);
4521
4522 mutex_lock(&dev_priv->dpio_lock);
4523 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4524 mutex_unlock(&dev_priv->dpio_lock);
4525
4526 divider &= 0xf;
4527
4528 cur_cdclk = (vco << 1) / (divider + 1);
4529
4530 return cur_cdclk;
4531}
4532
4533static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4534 int max_pixclk)
4535{
30a970c6
JB
4536 /*
4537 * Really only a few cases to deal with, as only 4 CDclks are supported:
4538 * 200MHz
4539 * 267MHz
4540 * 320MHz
4541 * 400MHz
4542 * So we check to see whether we're above 90% of the lower bin and
4543 * adjust if needed.
4544 */
4545 if (max_pixclk > 288000) {
4546 return 400;
4547 } else if (max_pixclk > 240000) {
4548 return 320;
4549 } else
4550 return 266;
4551 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4552}
4553
2f2d7aa1
VS
4554/* compute the max pixel clock for new configuration */
4555static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4556{
4557 struct drm_device *dev = dev_priv->dev;
4558 struct intel_crtc *intel_crtc;
4559 int max_pixclk = 0;
4560
d3fcc808 4561 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4562 if (intel_crtc->new_enabled)
30a970c6 4563 max_pixclk = max(max_pixclk,
2f2d7aa1 4564 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4565 }
4566
4567 return max_pixclk;
4568}
4569
4570static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4571 unsigned *prepare_pipes)
30a970c6
JB
4572{
4573 struct drm_i915_private *dev_priv = dev->dev_private;
4574 struct intel_crtc *intel_crtc;
2f2d7aa1 4575 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4576
d60c4473
ID
4577 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4578 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4579 return;
4580
2f2d7aa1 4581 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4582 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4583 if (intel_crtc->base.enabled)
4584 *prepare_pipes |= (1 << intel_crtc->pipe);
4585}
4586
4587static void valleyview_modeset_global_resources(struct drm_device *dev)
4588{
4589 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4590 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4591 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4592
d60c4473 4593 if (req_cdclk != dev_priv->vlv_cdclk_freq)
30a970c6 4594 valleyview_set_cdclk(dev, req_cdclk);
77961eb9 4595 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4596}
4597
89b667f8
JB
4598static void valleyview_crtc_enable(struct drm_crtc *crtc)
4599{
4600 struct drm_device *dev = crtc->dev;
5b18e57c 4601 struct drm_i915_private *dev_priv = dev->dev_private;
89b667f8
JB
4602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4603 struct intel_encoder *encoder;
4604 int pipe = intel_crtc->pipe;
5b18e57c 4605 int plane = intel_crtc->plane;
23538ef1 4606 bool is_dsi;
5b18e57c 4607 u32 dspcntr;
89b667f8
JB
4608
4609 WARN_ON(!crtc->enabled);
4610
4611 if (intel_crtc->active)
4612 return;
4613
bdd4b6a6
DV
4614 vlv_prepare_pll(intel_crtc);
4615
5b18e57c
DV
4616 /* Set up the display plane register */
4617 dspcntr = DISPPLANE_GAMMA_ENABLE;
4618
4619 if (intel_crtc->config.has_dp_encoder)
4620 intel_dp_set_m_n(intel_crtc);
4621
4622 intel_set_pipe_timings(intel_crtc);
4623
4624 /* pipesrc and dspsize control the size that is scaled from,
4625 * which should always be the user's requested size.
4626 */
4627 I915_WRITE(DSPSIZE(plane),
4628 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4629 (intel_crtc->config.pipe_src_w - 1));
4630 I915_WRITE(DSPPOS(plane), 0);
4631
4632 i9xx_set_pipeconf(intel_crtc);
4633
4634 I915_WRITE(DSPCNTR(plane), dspcntr);
4635 POSTING_READ(DSPCNTR(plane));
4636
4637 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4638 crtc->x, crtc->y);
4639
89b667f8 4640 intel_crtc->active = true;
89b667f8 4641
4a3436e8
VS
4642 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4643
89b667f8
JB
4644 for_each_encoder_on_crtc(dev, crtc, encoder)
4645 if (encoder->pre_pll_enable)
4646 encoder->pre_pll_enable(encoder);
4647
23538ef1
JN
4648 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4649
9d556c99
CML
4650 if (!is_dsi) {
4651 if (IS_CHERRYVIEW(dev))
4652 chv_enable_pll(intel_crtc);
4653 else
4654 vlv_enable_pll(intel_crtc);
4655 }
89b667f8
JB
4656
4657 for_each_encoder_on_crtc(dev, crtc, encoder)
4658 if (encoder->pre_enable)
4659 encoder->pre_enable(encoder);
4660
2dd24552
JB
4661 i9xx_pfit_enable(intel_crtc);
4662
63cbb074
VS
4663 intel_crtc_load_lut(crtc);
4664
f37fcc2a 4665 intel_update_watermarks(crtc);
e1fdc473 4666 intel_enable_pipe(intel_crtc);
be6a6f8e 4667
5004945f
JN
4668 for_each_encoder_on_crtc(dev, crtc, encoder)
4669 encoder->enable(encoder);
9ab0460b
VS
4670
4671 intel_crtc_enable_planes(crtc);
d40d9187 4672
56b80e1f
VS
4673 /* Underruns don't raise interrupts, so check manually. */
4674 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4675}
4676
f13c2ef3
DV
4677static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4678{
4679 struct drm_device *dev = crtc->base.dev;
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681
4682 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4683 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4684}
4685
0b8765c6 4686static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4687{
4688 struct drm_device *dev = crtc->dev;
5b18e57c 4689 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 4690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4691 struct intel_encoder *encoder;
79e53945 4692 int pipe = intel_crtc->pipe;
5b18e57c
DV
4693 int plane = intel_crtc->plane;
4694 u32 dspcntr;
79e53945 4695
08a48469
DV
4696 WARN_ON(!crtc->enabled);
4697
f7abfe8b
CW
4698 if (intel_crtc->active)
4699 return;
4700
f13c2ef3
DV
4701 i9xx_set_pll_dividers(intel_crtc);
4702
5b18e57c
DV
4703 /* Set up the display plane register */
4704 dspcntr = DISPPLANE_GAMMA_ENABLE;
4705
4706 if (pipe == 0)
4707 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4708 else
4709 dspcntr |= DISPPLANE_SEL_PIPE_B;
4710
4711 if (intel_crtc->config.has_dp_encoder)
4712 intel_dp_set_m_n(intel_crtc);
4713
4714 intel_set_pipe_timings(intel_crtc);
4715
4716 /* pipesrc and dspsize control the size that is scaled from,
4717 * which should always be the user's requested size.
4718 */
4719 I915_WRITE(DSPSIZE(plane),
4720 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4721 (intel_crtc->config.pipe_src_w - 1));
4722 I915_WRITE(DSPPOS(plane), 0);
4723
4724 i9xx_set_pipeconf(intel_crtc);
4725
4726 I915_WRITE(DSPCNTR(plane), dspcntr);
4727 POSTING_READ(DSPCNTR(plane));
4728
4729 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4730 crtc->x, crtc->y);
4731
f7abfe8b 4732 intel_crtc->active = true;
6b383a7f 4733
4a3436e8
VS
4734 if (!IS_GEN2(dev))
4735 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4736
9d6d9f19
MK
4737 for_each_encoder_on_crtc(dev, crtc, encoder)
4738 if (encoder->pre_enable)
4739 encoder->pre_enable(encoder);
4740
f6736a1a
DV
4741 i9xx_enable_pll(intel_crtc);
4742
2dd24552
JB
4743 i9xx_pfit_enable(intel_crtc);
4744
63cbb074
VS
4745 intel_crtc_load_lut(crtc);
4746
f37fcc2a 4747 intel_update_watermarks(crtc);
e1fdc473 4748 intel_enable_pipe(intel_crtc);
be6a6f8e 4749
fa5c73b1
DV
4750 for_each_encoder_on_crtc(dev, crtc, encoder)
4751 encoder->enable(encoder);
9ab0460b
VS
4752
4753 intel_crtc_enable_planes(crtc);
d40d9187 4754
4a3436e8
VS
4755 /*
4756 * Gen2 reports pipe underruns whenever all planes are disabled.
4757 * So don't enable underrun reporting before at least some planes
4758 * are enabled.
4759 * FIXME: Need to fix the logic to work when we turn off all planes
4760 * but leave the pipe running.
4761 */
4762 if (IS_GEN2(dev))
4763 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4764
56b80e1f
VS
4765 /* Underruns don't raise interrupts, so check manually. */
4766 i9xx_check_fifo_underruns(dev);
0b8765c6 4767}
79e53945 4768
87476d63
DV
4769static void i9xx_pfit_disable(struct intel_crtc *crtc)
4770{
4771 struct drm_device *dev = crtc->base.dev;
4772 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4773
328d8e82
DV
4774 if (!crtc->config.gmch_pfit.control)
4775 return;
87476d63 4776
328d8e82 4777 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4778
328d8e82
DV
4779 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4780 I915_READ(PFIT_CONTROL));
4781 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4782}
4783
0b8765c6
JB
4784static void i9xx_crtc_disable(struct drm_crtc *crtc)
4785{
4786 struct drm_device *dev = crtc->dev;
4787 struct drm_i915_private *dev_priv = dev->dev_private;
4788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4789 struct intel_encoder *encoder;
0b8765c6 4790 int pipe = intel_crtc->pipe;
ef9c3aee 4791
f7abfe8b
CW
4792 if (!intel_crtc->active)
4793 return;
4794
4a3436e8
VS
4795 /*
4796 * Gen2 reports pipe underruns whenever all planes are disabled.
4797 * So diasble underrun reporting before all the planes get disabled.
4798 * FIXME: Need to fix the logic to work when we turn off all planes
4799 * but leave the pipe running.
4800 */
4801 if (IS_GEN2(dev))
4802 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4803
9ab0460b
VS
4804 intel_crtc_disable_planes(crtc);
4805
ea9d758d
DV
4806 for_each_encoder_on_crtc(dev, crtc, encoder)
4807 encoder->disable(encoder);
4808
6304cd91
VS
4809 /*
4810 * On gen2 planes are double buffered but the pipe isn't, so we must
4811 * wait for planes to fully turn off before disabling the pipe.
4812 */
4813 if (IS_GEN2(dev))
4814 intel_wait_for_vblank(dev, pipe);
4815
b24e7179 4816 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4817
87476d63 4818 i9xx_pfit_disable(intel_crtc);
24a1f16d 4819
89b667f8
JB
4820 for_each_encoder_on_crtc(dev, crtc, encoder)
4821 if (encoder->post_disable)
4822 encoder->post_disable(encoder);
4823
076ed3b2
CML
4824 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4825 if (IS_CHERRYVIEW(dev))
4826 chv_disable_pll(dev_priv, pipe);
4827 else if (IS_VALLEYVIEW(dev))
4828 vlv_disable_pll(dev_priv, pipe);
4829 else
4830 i9xx_disable_pll(dev_priv, pipe);
4831 }
0b8765c6 4832
4a3436e8
VS
4833 if (!IS_GEN2(dev))
4834 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4835
f7abfe8b 4836 intel_crtc->active = false;
46ba614c 4837 intel_update_watermarks(crtc);
f37fcc2a 4838
efa9624e 4839 mutex_lock(&dev->struct_mutex);
6b383a7f 4840 intel_update_fbc(dev);
efa9624e 4841 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
4842}
4843
ee7b9f93
JB
4844static void i9xx_crtc_off(struct drm_crtc *crtc)
4845{
4846}
4847
976f8a20
DV
4848static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4849 bool enabled)
2c07245f
ZW
4850{
4851 struct drm_device *dev = crtc->dev;
4852 struct drm_i915_master_private *master_priv;
4853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4854 int pipe = intel_crtc->pipe;
79e53945
JB
4855
4856 if (!dev->primary->master)
4857 return;
4858
4859 master_priv = dev->primary->master->driver_priv;
4860 if (!master_priv->sarea_priv)
4861 return;
4862
79e53945
JB
4863 switch (pipe) {
4864 case 0:
4865 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4866 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4867 break;
4868 case 1:
4869 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4870 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4871 break;
4872 default:
9db4a9c7 4873 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4874 break;
4875 }
79e53945
JB
4876}
4877
976f8a20
DV
4878/**
4879 * Sets the power management mode of the pipe and plane.
4880 */
4881void intel_crtc_update_dpms(struct drm_crtc *crtc)
4882{
4883 struct drm_device *dev = crtc->dev;
4884 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 4885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
976f8a20 4886 struct intel_encoder *intel_encoder;
0e572fe7
DV
4887 enum intel_display_power_domain domain;
4888 unsigned long domains;
976f8a20
DV
4889 bool enable = false;
4890
4891 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4892 enable |= intel_encoder->connectors_active;
4893
0e572fe7
DV
4894 if (enable) {
4895 if (!intel_crtc->active) {
4896 /*
4897 * FIXME: DDI plls and relevant code isn't converted
4898 * yet, so do runtime PM for DPMS only for all other
4899 * platforms for now.
4900 */
4901 if (!HAS_DDI(dev)) {
4902 domains = get_crtc_power_domains(crtc);
4903 for_each_power_domain(domain, domains)
4904 intel_display_power_get(dev_priv, domain);
4905 intel_crtc->enabled_power_domains = domains;
4906 }
4907
4908 dev_priv->display.crtc_enable(crtc);
4909 }
4910 } else {
4911 if (intel_crtc->active) {
4912 dev_priv->display.crtc_disable(crtc);
4913
4914 if (!HAS_DDI(dev)) {
4915 domains = intel_crtc->enabled_power_domains;
4916 for_each_power_domain(domain, domains)
4917 intel_display_power_put(dev_priv, domain);
4918 intel_crtc->enabled_power_domains = 0;
4919 }
4920 }
4921 }
976f8a20
DV
4922
4923 intel_crtc_update_sarea(crtc, enable);
4924}
4925
cdd59983
CW
4926static void intel_crtc_disable(struct drm_crtc *crtc)
4927{
cdd59983 4928 struct drm_device *dev = crtc->dev;
976f8a20 4929 struct drm_connector *connector;
ee7b9f93 4930 struct drm_i915_private *dev_priv = dev->dev_private;
a071fa00
DV
4931 struct drm_i915_gem_object *old_obj;
4932 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 4933
976f8a20
DV
4934 /* crtc should still be enabled when we disable it. */
4935 WARN_ON(!crtc->enabled);
4936
4937 dev_priv->display.crtc_disable(crtc);
4938 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4939 dev_priv->display.off(crtc);
4940
931872fc 4941 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
a071fa00
DV
4942 assert_cursor_disabled(dev_priv, pipe);
4943 assert_pipe_disabled(dev->dev_private, pipe);
cdd59983 4944
f4510a27 4945 if (crtc->primary->fb) {
a071fa00 4946 old_obj = to_intel_framebuffer(crtc->primary->fb)->obj;
cdd59983 4947 mutex_lock(&dev->struct_mutex);
a071fa00
DV
4948 intel_unpin_fb_obj(old_obj);
4949 i915_gem_track_fb(old_obj, NULL,
4950 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 4951 mutex_unlock(&dev->struct_mutex);
f4510a27 4952 crtc->primary->fb = NULL;
976f8a20
DV
4953 }
4954
4955 /* Update computed state. */
4956 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4957 if (!connector->encoder || !connector->encoder->crtc)
4958 continue;
4959
4960 if (connector->encoder->crtc != crtc)
4961 continue;
4962
4963 connector->dpms = DRM_MODE_DPMS_OFF;
4964 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
4965 }
4966}
4967
ea5b213a 4968void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 4969{
4ef69c7a 4970 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 4971
ea5b213a
CW
4972 drm_encoder_cleanup(encoder);
4973 kfree(intel_encoder);
7e7d76c3
JB
4974}
4975
9237329d 4976/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
4977 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4978 * state of the entire output pipe. */
9237329d 4979static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 4980{
5ab432ef
DV
4981 if (mode == DRM_MODE_DPMS_ON) {
4982 encoder->connectors_active = true;
4983
b2cabb0e 4984 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
4985 } else {
4986 encoder->connectors_active = false;
4987
b2cabb0e 4988 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 4989 }
79e53945
JB
4990}
4991
0a91ca29
DV
4992/* Cross check the actual hw state with our own modeset state tracking (and it's
4993 * internal consistency). */
b980514c 4994static void intel_connector_check_state(struct intel_connector *connector)
79e53945 4995{
0a91ca29
DV
4996 if (connector->get_hw_state(connector)) {
4997 struct intel_encoder *encoder = connector->encoder;
4998 struct drm_crtc *crtc;
4999 bool encoder_enabled;
5000 enum pipe pipe;
5001
5002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5003 connector->base.base.id,
c23cc417 5004 connector->base.name);
0a91ca29
DV
5005
5006 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5007 "wrong connector dpms state\n");
5008 WARN(connector->base.encoder != &encoder->base,
5009 "active connector not linked to encoder\n");
5010 WARN(!encoder->connectors_active,
5011 "encoder->connectors_active not set\n");
5012
5013 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5014 WARN(!encoder_enabled, "encoder not enabled\n");
5015 if (WARN_ON(!encoder->base.crtc))
5016 return;
5017
5018 crtc = encoder->base.crtc;
5019
5020 WARN(!crtc->enabled, "crtc not enabled\n");
5021 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5022 WARN(pipe != to_intel_crtc(crtc)->pipe,
5023 "encoder active on the wrong pipe\n");
5024 }
79e53945
JB
5025}
5026
5ab432ef
DV
5027/* Even simpler default implementation, if there's really no special case to
5028 * consider. */
5029void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5030{
5ab432ef
DV
5031 /* All the simple cases only support two dpms states. */
5032 if (mode != DRM_MODE_DPMS_ON)
5033 mode = DRM_MODE_DPMS_OFF;
d4270e57 5034
5ab432ef
DV
5035 if (mode == connector->dpms)
5036 return;
5037
5038 connector->dpms = mode;
5039
5040 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5041 if (connector->encoder)
5042 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5043
b980514c 5044 intel_modeset_check_state(connector->dev);
79e53945
JB
5045}
5046
f0947c37
DV
5047/* Simple connector->get_hw_state implementation for encoders that support only
5048 * one connector and no cloning and hence the encoder state determines the state
5049 * of the connector. */
5050bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5051{
24929352 5052 enum pipe pipe = 0;
f0947c37 5053 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5054
f0947c37 5055 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5056}
5057
1857e1da
DV
5058static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5059 struct intel_crtc_config *pipe_config)
5060{
5061 struct drm_i915_private *dev_priv = dev->dev_private;
5062 struct intel_crtc *pipe_B_crtc =
5063 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5064
5065 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5066 pipe_name(pipe), pipe_config->fdi_lanes);
5067 if (pipe_config->fdi_lanes > 4) {
5068 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5069 pipe_name(pipe), pipe_config->fdi_lanes);
5070 return false;
5071 }
5072
bafb6553 5073 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5074 if (pipe_config->fdi_lanes > 2) {
5075 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5076 pipe_config->fdi_lanes);
5077 return false;
5078 } else {
5079 return true;
5080 }
5081 }
5082
5083 if (INTEL_INFO(dev)->num_pipes == 2)
5084 return true;
5085
5086 /* Ivybridge 3 pipe is really complicated */
5087 switch (pipe) {
5088 case PIPE_A:
5089 return true;
5090 case PIPE_B:
5091 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5092 pipe_config->fdi_lanes > 2) {
5093 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5094 pipe_name(pipe), pipe_config->fdi_lanes);
5095 return false;
5096 }
5097 return true;
5098 case PIPE_C:
1e833f40 5099 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5100 pipe_B_crtc->config.fdi_lanes <= 2) {
5101 if (pipe_config->fdi_lanes > 2) {
5102 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5103 pipe_name(pipe), pipe_config->fdi_lanes);
5104 return false;
5105 }
5106 } else {
5107 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5108 return false;
5109 }
5110 return true;
5111 default:
5112 BUG();
5113 }
5114}
5115
e29c22c0
DV
5116#define RETRY 1
5117static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5118 struct intel_crtc_config *pipe_config)
877d48d5 5119{
1857e1da 5120 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5121 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5122 int lane, link_bw, fdi_dotclock;
e29c22c0 5123 bool setup_ok, needs_recompute = false;
877d48d5 5124
e29c22c0 5125retry:
877d48d5
DV
5126 /* FDI is a binary signal running at ~2.7GHz, encoding
5127 * each output octet as 10 bits. The actual frequency
5128 * is stored as a divider into a 100MHz clock, and the
5129 * mode pixel clock is stored in units of 1KHz.
5130 * Hence the bw of each lane in terms of the mode signal
5131 * is:
5132 */
5133 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5134
241bfc38 5135 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5136
2bd89a07 5137 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5138 pipe_config->pipe_bpp);
5139
5140 pipe_config->fdi_lanes = lane;
5141
2bd89a07 5142 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5143 link_bw, &pipe_config->fdi_m_n);
1857e1da 5144
e29c22c0
DV
5145 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5146 intel_crtc->pipe, pipe_config);
5147 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5148 pipe_config->pipe_bpp -= 2*3;
5149 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5150 pipe_config->pipe_bpp);
5151 needs_recompute = true;
5152 pipe_config->bw_constrained = true;
5153
5154 goto retry;
5155 }
5156
5157 if (needs_recompute)
5158 return RETRY;
5159
5160 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5161}
5162
42db64ef
PZ
5163static void hsw_compute_ips_config(struct intel_crtc *crtc,
5164 struct intel_crtc_config *pipe_config)
5165{
d330a953 5166 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5167 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5168 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5169}
5170
a43f6e0f 5171static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5172 struct intel_crtc_config *pipe_config)
79e53945 5173{
a43f6e0f 5174 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5175 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5176
ad3a4479 5177 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5178 if (INTEL_INFO(dev)->gen < 4) {
5179 struct drm_i915_private *dev_priv = dev->dev_private;
5180 int clock_limit =
5181 dev_priv->display.get_display_clock_speed(dev);
5182
5183 /*
5184 * Enable pixel doubling when the dot clock
5185 * is > 90% of the (display) core speed.
5186 *
b397c96b
VS
5187 * GDG double wide on either pipe,
5188 * otherwise pipe A only.
cf532bb2 5189 */
b397c96b 5190 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5191 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5192 clock_limit *= 2;
cf532bb2 5193 pipe_config->double_wide = true;
ad3a4479
VS
5194 }
5195
241bfc38 5196 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5197 return -EINVAL;
2c07245f 5198 }
89749350 5199
1d1d0e27
VS
5200 /*
5201 * Pipe horizontal size must be even in:
5202 * - DVO ganged mode
5203 * - LVDS dual channel mode
5204 * - Double wide pipe
5205 */
5206 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5207 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5208 pipe_config->pipe_src_w &= ~1;
5209
8693a824
DL
5210 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5211 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5212 */
5213 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5214 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5215 return -EINVAL;
44f46b42 5216
bd080ee5 5217 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5218 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5219 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5220 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5221 * for lvds. */
5222 pipe_config->pipe_bpp = 8*3;
5223 }
5224
f5adf94e 5225 if (HAS_IPS(dev))
a43f6e0f
DV
5226 hsw_compute_ips_config(crtc, pipe_config);
5227
5228 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5229 * clock survives for now. */
5230 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5231 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5232
877d48d5 5233 if (pipe_config->has_pch_encoder)
a43f6e0f 5234 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5235
e29c22c0 5236 return 0;
79e53945
JB
5237}
5238
25eb05fc
JB
5239static int valleyview_get_display_clock_speed(struct drm_device *dev)
5240{
5241 return 400000; /* FIXME */
5242}
5243
e70236a8
JB
5244static int i945_get_display_clock_speed(struct drm_device *dev)
5245{
5246 return 400000;
5247}
79e53945 5248
e70236a8 5249static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5250{
e70236a8
JB
5251 return 333000;
5252}
79e53945 5253
e70236a8
JB
5254static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5255{
5256 return 200000;
5257}
79e53945 5258
257a7ffc
DV
5259static int pnv_get_display_clock_speed(struct drm_device *dev)
5260{
5261 u16 gcfgc = 0;
5262
5263 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5264
5265 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5266 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5267 return 267000;
5268 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5269 return 333000;
5270 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5271 return 444000;
5272 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5273 return 200000;
5274 default:
5275 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5276 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5277 return 133000;
5278 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5279 return 167000;
5280 }
5281}
5282
e70236a8
JB
5283static int i915gm_get_display_clock_speed(struct drm_device *dev)
5284{
5285 u16 gcfgc = 0;
79e53945 5286
e70236a8
JB
5287 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5288
5289 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5290 return 133000;
5291 else {
5292 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5293 case GC_DISPLAY_CLOCK_333_MHZ:
5294 return 333000;
5295 default:
5296 case GC_DISPLAY_CLOCK_190_200_MHZ:
5297 return 190000;
79e53945 5298 }
e70236a8
JB
5299 }
5300}
5301
5302static int i865_get_display_clock_speed(struct drm_device *dev)
5303{
5304 return 266000;
5305}
5306
5307static int i855_get_display_clock_speed(struct drm_device *dev)
5308{
5309 u16 hpllcc = 0;
5310 /* Assume that the hardware is in the high speed state. This
5311 * should be the default.
5312 */
5313 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5314 case GC_CLOCK_133_200:
5315 case GC_CLOCK_100_200:
5316 return 200000;
5317 case GC_CLOCK_166_250:
5318 return 250000;
5319 case GC_CLOCK_100_133:
79e53945 5320 return 133000;
e70236a8 5321 }
79e53945 5322
e70236a8
JB
5323 /* Shouldn't happen */
5324 return 0;
5325}
79e53945 5326
e70236a8
JB
5327static int i830_get_display_clock_speed(struct drm_device *dev)
5328{
5329 return 133000;
79e53945
JB
5330}
5331
2c07245f 5332static void
a65851af 5333intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5334{
a65851af
VS
5335 while (*num > DATA_LINK_M_N_MASK ||
5336 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5337 *num >>= 1;
5338 *den >>= 1;
5339 }
5340}
5341
a65851af
VS
5342static void compute_m_n(unsigned int m, unsigned int n,
5343 uint32_t *ret_m, uint32_t *ret_n)
5344{
5345 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5346 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5347 intel_reduce_m_n_ratio(ret_m, ret_n);
5348}
5349
e69d0bc1
DV
5350void
5351intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5352 int pixel_clock, int link_clock,
5353 struct intel_link_m_n *m_n)
2c07245f 5354{
e69d0bc1 5355 m_n->tu = 64;
a65851af
VS
5356
5357 compute_m_n(bits_per_pixel * pixel_clock,
5358 link_clock * nlanes * 8,
5359 &m_n->gmch_m, &m_n->gmch_n);
5360
5361 compute_m_n(pixel_clock, link_clock,
5362 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5363}
5364
a7615030
CW
5365static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5366{
d330a953
JN
5367 if (i915.panel_use_ssc >= 0)
5368 return i915.panel_use_ssc != 0;
41aa3448 5369 return dev_priv->vbt.lvds_use_ssc
435793df 5370 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5371}
5372
c65d77d8
JB
5373static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5374{
5375 struct drm_device *dev = crtc->dev;
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377 int refclk;
5378
a0c4da24 5379 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5380 refclk = 100000;
a0c4da24 5381 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5382 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5383 refclk = dev_priv->vbt.lvds_ssc_freq;
5384 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5385 } else if (!IS_GEN2(dev)) {
5386 refclk = 96000;
5387 } else {
5388 refclk = 48000;
5389 }
5390
5391 return refclk;
5392}
5393
7429e9d4 5394static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5395{
7df00d7a 5396 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5397}
f47709a9 5398
7429e9d4
DV
5399static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5400{
5401 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5402}
5403
f47709a9 5404static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5405 intel_clock_t *reduced_clock)
5406{
f47709a9 5407 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5408 u32 fp, fp2 = 0;
5409
5410 if (IS_PINEVIEW(dev)) {
7429e9d4 5411 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5412 if (reduced_clock)
7429e9d4 5413 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5414 } else {
7429e9d4 5415 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5416 if (reduced_clock)
7429e9d4 5417 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5418 }
5419
8bcc2795 5420 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5421
f47709a9
DV
5422 crtc->lowfreq_avail = false;
5423 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5424 reduced_clock && i915.powersave) {
8bcc2795 5425 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5426 crtc->lowfreq_avail = true;
a7516a05 5427 } else {
8bcc2795 5428 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5429 }
5430}
5431
5e69f97f
CML
5432static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5433 pipe)
89b667f8
JB
5434{
5435 u32 reg_val;
5436
5437 /*
5438 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5439 * and set it to a reasonable value instead.
5440 */
ab3c759a 5441 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5442 reg_val &= 0xffffff00;
5443 reg_val |= 0x00000030;
ab3c759a 5444 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5445
ab3c759a 5446 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5447 reg_val &= 0x8cffffff;
5448 reg_val = 0x8c000000;
ab3c759a 5449 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5450
ab3c759a 5451 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5452 reg_val &= 0xffffff00;
ab3c759a 5453 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5454
ab3c759a 5455 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5456 reg_val &= 0x00ffffff;
5457 reg_val |= 0xb0000000;
ab3c759a 5458 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5459}
5460
b551842d
DV
5461static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5462 struct intel_link_m_n *m_n)
5463{
5464 struct drm_device *dev = crtc->base.dev;
5465 struct drm_i915_private *dev_priv = dev->dev_private;
5466 int pipe = crtc->pipe;
5467
e3b95f1e
DV
5468 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5469 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5470 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5471 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5472}
5473
5474static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5475 struct intel_link_m_n *m_n)
5476{
5477 struct drm_device *dev = crtc->base.dev;
5478 struct drm_i915_private *dev_priv = dev->dev_private;
5479 int pipe = crtc->pipe;
5480 enum transcoder transcoder = crtc->config.cpu_transcoder;
5481
5482 if (INTEL_INFO(dev)->gen >= 5) {
5483 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5484 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5485 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5486 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5487 } else {
e3b95f1e
DV
5488 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5489 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5490 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5491 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5492 }
5493}
5494
03afc4a2
DV
5495static void intel_dp_set_m_n(struct intel_crtc *crtc)
5496{
5497 if (crtc->config.has_pch_encoder)
5498 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5499 else
5500 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5501}
5502
f47709a9 5503static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5504{
5505 u32 dpll, dpll_md;
5506
5507 /*
5508 * Enable DPIO clock input. We should never disable the reference
5509 * clock for pipe B, since VGA hotplug / manual detection depends
5510 * on it.
5511 */
5512 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5513 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5514 /* We should never disable this, set it here for state tracking */
5515 if (crtc->pipe == PIPE_B)
5516 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5517 dpll |= DPLL_VCO_ENABLE;
5518 crtc->config.dpll_hw_state.dpll = dpll;
5519
5520 dpll_md = (crtc->config.pixel_multiplier - 1)
5521 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5522 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5523}
5524
5525static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5526{
f47709a9 5527 struct drm_device *dev = crtc->base.dev;
a0c4da24 5528 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5529 int pipe = crtc->pipe;
bdd4b6a6 5530 u32 mdiv;
a0c4da24 5531 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5532 u32 coreclk, reg_val;
a0c4da24 5533
09153000
DV
5534 mutex_lock(&dev_priv->dpio_lock);
5535
f47709a9
DV
5536 bestn = crtc->config.dpll.n;
5537 bestm1 = crtc->config.dpll.m1;
5538 bestm2 = crtc->config.dpll.m2;
5539 bestp1 = crtc->config.dpll.p1;
5540 bestp2 = crtc->config.dpll.p2;
a0c4da24 5541
89b667f8
JB
5542 /* See eDP HDMI DPIO driver vbios notes doc */
5543
5544 /* PLL B needs special handling */
bdd4b6a6 5545 if (pipe == PIPE_B)
5e69f97f 5546 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5547
5548 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5549 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5550
5551 /* Disable target IRef on PLL */
ab3c759a 5552 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5553 reg_val &= 0x00ffffff;
ab3c759a 5554 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5555
5556 /* Disable fast lock */
ab3c759a 5557 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5558
5559 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5560 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5561 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5562 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5563 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5564
5565 /*
5566 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5567 * but we don't support that).
5568 * Note: don't use the DAC post divider as it seems unstable.
5569 */
5570 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5571 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5572
a0c4da24 5573 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5575
89b667f8 5576 /* Set HBR and RBR LPF coefficients */
ff9a6750 5577 if (crtc->config.port_clock == 162000 ||
99750bd4 5578 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5579 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5580 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5581 0x009f0003);
89b667f8 5582 else
ab3c759a 5583 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5584 0x00d0000f);
5585
5586 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5587 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5588 /* Use SSC source */
bdd4b6a6 5589 if (pipe == PIPE_A)
ab3c759a 5590 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5591 0x0df40000);
5592 else
ab3c759a 5593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5594 0x0df70000);
5595 } else { /* HDMI or VGA */
5596 /* Use bend source */
bdd4b6a6 5597 if (pipe == PIPE_A)
ab3c759a 5598 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5599 0x0df70000);
5600 else
ab3c759a 5601 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5602 0x0df40000);
5603 }
a0c4da24 5604
ab3c759a 5605 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5606 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5607 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5608 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5609 coreclk |= 0x01000000;
ab3c759a 5610 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5611
ab3c759a 5612 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5613 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5614}
5615
9d556c99
CML
5616static void chv_update_pll(struct intel_crtc *crtc)
5617{
5618 struct drm_device *dev = crtc->base.dev;
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620 int pipe = crtc->pipe;
5621 int dpll_reg = DPLL(crtc->pipe);
5622 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5623 u32 loopfilter, intcoeff;
9d556c99
CML
5624 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5625 int refclk;
5626
a11b0703
VS
5627 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5628 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5629 DPLL_VCO_ENABLE;
5630 if (pipe != PIPE_A)
5631 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5632
5633 crtc->config.dpll_hw_state.dpll_md =
5634 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
9d556c99
CML
5635
5636 bestn = crtc->config.dpll.n;
5637 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5638 bestm1 = crtc->config.dpll.m1;
5639 bestm2 = crtc->config.dpll.m2 >> 22;
5640 bestp1 = crtc->config.dpll.p1;
5641 bestp2 = crtc->config.dpll.p2;
5642
5643 /*
5644 * Enable Refclk and SSC
5645 */
a11b0703
VS
5646 I915_WRITE(dpll_reg,
5647 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5648
5649 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5650
9d556c99
CML
5651 /* p1 and p2 divider */
5652 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5653 5 << DPIO_CHV_S1_DIV_SHIFT |
5654 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5655 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5656 1 << DPIO_CHV_K_DIV_SHIFT);
5657
5658 /* Feedback post-divider - m2 */
5659 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5660
5661 /* Feedback refclk divider - n and m1 */
5662 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5663 DPIO_CHV_M1_DIV_BY_2 |
5664 1 << DPIO_CHV_N_DIV_SHIFT);
5665
5666 /* M2 fraction division */
5667 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5668
5669 /* M2 fraction division enable */
5670 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5671 DPIO_CHV_FRAC_DIV_EN |
5672 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5673
5674 /* Loop filter */
5675 refclk = i9xx_get_refclk(&crtc->base, 0);
5676 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5677 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5678 if (refclk == 100000)
5679 intcoeff = 11;
5680 else if (refclk == 38400)
5681 intcoeff = 10;
5682 else
5683 intcoeff = 9;
5684 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5685 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5686
5687 /* AFC Recal */
5688 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5689 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5690 DPIO_AFC_RECAL);
5691
5692 mutex_unlock(&dev_priv->dpio_lock);
5693}
5694
f47709a9
DV
5695static void i9xx_update_pll(struct intel_crtc *crtc,
5696 intel_clock_t *reduced_clock,
eb1cbe48
DV
5697 int num_connectors)
5698{
f47709a9 5699 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5700 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5701 u32 dpll;
5702 bool is_sdvo;
f47709a9 5703 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5704
f47709a9 5705 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5706
f47709a9
DV
5707 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5708 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5709
5710 dpll = DPLL_VGA_MODE_DIS;
5711
f47709a9 5712 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5713 dpll |= DPLLB_MODE_LVDS;
5714 else
5715 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5716
ef1b460d 5717 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5718 dpll |= (crtc->config.pixel_multiplier - 1)
5719 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5720 }
198a037f
DV
5721
5722 if (is_sdvo)
4a33e48d 5723 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5724
f47709a9 5725 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5726 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5727
5728 /* compute bitmask from p1 value */
5729 if (IS_PINEVIEW(dev))
5730 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5731 else {
5732 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5733 if (IS_G4X(dev) && reduced_clock)
5734 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5735 }
5736 switch (clock->p2) {
5737 case 5:
5738 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5739 break;
5740 case 7:
5741 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5742 break;
5743 case 10:
5744 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5745 break;
5746 case 14:
5747 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5748 break;
5749 }
5750 if (INTEL_INFO(dev)->gen >= 4)
5751 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5752
09ede541 5753 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5754 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5755 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5756 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5757 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5758 else
5759 dpll |= PLL_REF_INPUT_DREFCLK;
5760
5761 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5762 crtc->config.dpll_hw_state.dpll = dpll;
5763
eb1cbe48 5764 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5765 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5766 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5767 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5768 }
5769}
5770
f47709a9 5771static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5772 intel_clock_t *reduced_clock,
eb1cbe48
DV
5773 int num_connectors)
5774{
f47709a9 5775 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5776 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5777 u32 dpll;
f47709a9 5778 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5779
f47709a9 5780 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5781
eb1cbe48
DV
5782 dpll = DPLL_VGA_MODE_DIS;
5783
f47709a9 5784 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5785 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5786 } else {
5787 if (clock->p1 == 2)
5788 dpll |= PLL_P1_DIVIDE_BY_TWO;
5789 else
5790 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5791 if (clock->p2 == 4)
5792 dpll |= PLL_P2_DIVIDE_BY_4;
5793 }
5794
4a33e48d
DV
5795 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5796 dpll |= DPLL_DVO_2X_MODE;
5797
f47709a9 5798 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5799 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5800 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5801 else
5802 dpll |= PLL_REF_INPUT_DREFCLK;
5803
5804 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5805 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5806}
5807
8a654f3b 5808static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5809{
5810 struct drm_device *dev = intel_crtc->base.dev;
5811 struct drm_i915_private *dev_priv = dev->dev_private;
5812 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5813 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5814 struct drm_display_mode *adjusted_mode =
5815 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5816 uint32_t crtc_vtotal, crtc_vblank_end;
5817 int vsyncshift = 0;
4d8a62ea
DV
5818
5819 /* We need to be careful not to changed the adjusted mode, for otherwise
5820 * the hw state checker will get angry at the mismatch. */
5821 crtc_vtotal = adjusted_mode->crtc_vtotal;
5822 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5823
609aeaca 5824 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5825 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5826 crtc_vtotal -= 1;
5827 crtc_vblank_end -= 1;
609aeaca
VS
5828
5829 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5830 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5831 else
5832 vsyncshift = adjusted_mode->crtc_hsync_start -
5833 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5834 if (vsyncshift < 0)
5835 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5836 }
5837
5838 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5839 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5840
fe2b8f9d 5841 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5842 (adjusted_mode->crtc_hdisplay - 1) |
5843 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5844 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5845 (adjusted_mode->crtc_hblank_start - 1) |
5846 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5847 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5848 (adjusted_mode->crtc_hsync_start - 1) |
5849 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5850
fe2b8f9d 5851 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5852 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5853 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5854 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5855 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5856 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5857 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5858 (adjusted_mode->crtc_vsync_start - 1) |
5859 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5860
b5e508d4
PZ
5861 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5862 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5863 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5864 * bits. */
5865 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5866 (pipe == PIPE_B || pipe == PIPE_C))
5867 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5868
b0e77b9c
PZ
5869 /* pipesrc controls the size that is scaled from, which should
5870 * always be the user's requested size.
5871 */
5872 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5873 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5874 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5875}
5876
1bd1bd80
DV
5877static void intel_get_pipe_timings(struct intel_crtc *crtc,
5878 struct intel_crtc_config *pipe_config)
5879{
5880 struct drm_device *dev = crtc->base.dev;
5881 struct drm_i915_private *dev_priv = dev->dev_private;
5882 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5883 uint32_t tmp;
5884
5885 tmp = I915_READ(HTOTAL(cpu_transcoder));
5886 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5887 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5888 tmp = I915_READ(HBLANK(cpu_transcoder));
5889 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5890 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5891 tmp = I915_READ(HSYNC(cpu_transcoder));
5892 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5893 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5894
5895 tmp = I915_READ(VTOTAL(cpu_transcoder));
5896 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5897 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5898 tmp = I915_READ(VBLANK(cpu_transcoder));
5899 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5900 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5901 tmp = I915_READ(VSYNC(cpu_transcoder));
5902 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5903 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5904
5905 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5906 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5907 pipe_config->adjusted_mode.crtc_vtotal += 1;
5908 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5909 }
5910
5911 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
5912 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5913 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5914
5915 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5916 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
5917}
5918
f6a83288
DV
5919void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5920 struct intel_crtc_config *pipe_config)
babea61d 5921{
f6a83288
DV
5922 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5923 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5924 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5925 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 5926
f6a83288
DV
5927 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5928 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5929 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5930 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 5931
f6a83288 5932 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 5933
f6a83288
DV
5934 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5935 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
5936}
5937
84b046f3
DV
5938static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5939{
5940 struct drm_device *dev = intel_crtc->base.dev;
5941 struct drm_i915_private *dev_priv = dev->dev_private;
5942 uint32_t pipeconf;
5943
9f11a9e4 5944 pipeconf = 0;
84b046f3 5945
67c72a12
DV
5946 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5947 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5948 pipeconf |= PIPECONF_ENABLE;
5949
cf532bb2
VS
5950 if (intel_crtc->config.double_wide)
5951 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 5952
ff9ce46e
DV
5953 /* only g4x and later have fancy bpc/dither controls */
5954 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
5955 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5956 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5957 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 5958 PIPECONF_DITHER_TYPE_SP;
84b046f3 5959
ff9ce46e
DV
5960 switch (intel_crtc->config.pipe_bpp) {
5961 case 18:
5962 pipeconf |= PIPECONF_6BPC;
5963 break;
5964 case 24:
5965 pipeconf |= PIPECONF_8BPC;
5966 break;
5967 case 30:
5968 pipeconf |= PIPECONF_10BPC;
5969 break;
5970 default:
5971 /* Case prevented by intel_choose_pipe_bpp_dither. */
5972 BUG();
84b046f3
DV
5973 }
5974 }
5975
5976 if (HAS_PIPE_CXSR(dev)) {
5977 if (intel_crtc->lowfreq_avail) {
5978 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5979 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5980 } else {
5981 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
5982 }
5983 }
5984
efc2cfff
VS
5985 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5986 if (INTEL_INFO(dev)->gen < 4 ||
5987 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5988 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5989 else
5990 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5991 } else
84b046f3
DV
5992 pipeconf |= PIPECONF_PROGRESSIVE;
5993
9f11a9e4
DV
5994 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5995 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 5996
84b046f3
DV
5997 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5998 POSTING_READ(PIPECONF(intel_crtc->pipe));
5999}
6000
f564048e 6001static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6002 int x, int y,
94352cf9 6003 struct drm_framebuffer *fb)
79e53945
JB
6004{
6005 struct drm_device *dev = crtc->dev;
6006 struct drm_i915_private *dev_priv = dev->dev_private;
6007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6008 int refclk, num_connectors = 0;
652c393a 6009 intel_clock_t clock, reduced_clock;
a16af721 6010 bool ok, has_reduced_clock = false;
e9fd1c02 6011 bool is_lvds = false, is_dsi = false;
5eddb70b 6012 struct intel_encoder *encoder;
d4906093 6013 const intel_limit_t *limit;
79e53945 6014
6c2b7c12 6015 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6016 switch (encoder->type) {
79e53945
JB
6017 case INTEL_OUTPUT_LVDS:
6018 is_lvds = true;
6019 break;
e9fd1c02
JN
6020 case INTEL_OUTPUT_DSI:
6021 is_dsi = true;
6022 break;
79e53945 6023 }
43565a06 6024
c751ce4f 6025 num_connectors++;
79e53945
JB
6026 }
6027
f2335330 6028 if (is_dsi)
5b18e57c 6029 return 0;
f2335330
JN
6030
6031 if (!intel_crtc->config.clock_set) {
6032 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6033
e9fd1c02
JN
6034 /*
6035 * Returns a set of divisors for the desired target clock with
6036 * the given refclk, or FALSE. The returned values represent
6037 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6038 * 2) / p1 / p2.
6039 */
6040 limit = intel_limit(crtc, refclk);
6041 ok = dev_priv->display.find_dpll(limit, crtc,
6042 intel_crtc->config.port_clock,
6043 refclk, NULL, &clock);
f2335330 6044 if (!ok) {
e9fd1c02
JN
6045 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6046 return -EINVAL;
6047 }
79e53945 6048
f2335330
JN
6049 if (is_lvds && dev_priv->lvds_downclock_avail) {
6050 /*
6051 * Ensure we match the reduced clock's P to the target
6052 * clock. If the clocks don't match, we can't switch
6053 * the display clock by using the FP0/FP1. In such case
6054 * we will disable the LVDS downclock feature.
6055 */
6056 has_reduced_clock =
6057 dev_priv->display.find_dpll(limit, crtc,
6058 dev_priv->lvds_downclock,
6059 refclk, &clock,
6060 &reduced_clock);
6061 }
6062 /* Compat-code for transition, will disappear. */
f47709a9
DV
6063 intel_crtc->config.dpll.n = clock.n;
6064 intel_crtc->config.dpll.m1 = clock.m1;
6065 intel_crtc->config.dpll.m2 = clock.m2;
6066 intel_crtc->config.dpll.p1 = clock.p1;
6067 intel_crtc->config.dpll.p2 = clock.p2;
6068 }
7026d4ac 6069
e9fd1c02 6070 if (IS_GEN2(dev)) {
8a654f3b 6071 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6072 has_reduced_clock ? &reduced_clock : NULL,
6073 num_connectors);
9d556c99
CML
6074 } else if (IS_CHERRYVIEW(dev)) {
6075 chv_update_pll(intel_crtc);
e9fd1c02 6076 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6077 vlv_update_pll(intel_crtc);
e9fd1c02 6078 } else {
f47709a9 6079 i9xx_update_pll(intel_crtc,
eb1cbe48 6080 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6081 num_connectors);
e9fd1c02 6082 }
79e53945 6083
c8f7a0db 6084 return 0;
f564048e
EA
6085}
6086
2fa2fe9a
DV
6087static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6088 struct intel_crtc_config *pipe_config)
6089{
6090 struct drm_device *dev = crtc->base.dev;
6091 struct drm_i915_private *dev_priv = dev->dev_private;
6092 uint32_t tmp;
6093
dc9e7dec
VS
6094 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6095 return;
6096
2fa2fe9a 6097 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6098 if (!(tmp & PFIT_ENABLE))
6099 return;
2fa2fe9a 6100
06922821 6101 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6102 if (INTEL_INFO(dev)->gen < 4) {
6103 if (crtc->pipe != PIPE_B)
6104 return;
2fa2fe9a
DV
6105 } else {
6106 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6107 return;
6108 }
6109
06922821 6110 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6111 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6112 if (INTEL_INFO(dev)->gen < 5)
6113 pipe_config->gmch_pfit.lvds_border_bits =
6114 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6115}
6116
acbec814
JB
6117static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6118 struct intel_crtc_config *pipe_config)
6119{
6120 struct drm_device *dev = crtc->base.dev;
6121 struct drm_i915_private *dev_priv = dev->dev_private;
6122 int pipe = pipe_config->cpu_transcoder;
6123 intel_clock_t clock;
6124 u32 mdiv;
662c6ecb 6125 int refclk = 100000;
acbec814
JB
6126
6127 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6128 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6129 mutex_unlock(&dev_priv->dpio_lock);
6130
6131 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6132 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6133 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6134 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6135 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6136
f646628b 6137 vlv_clock(refclk, &clock);
acbec814 6138
f646628b
VS
6139 /* clock.dot is the fast clock */
6140 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6141}
6142
1ad292b5
JB
6143static void i9xx_get_plane_config(struct intel_crtc *crtc,
6144 struct intel_plane_config *plane_config)
6145{
6146 struct drm_device *dev = crtc->base.dev;
6147 struct drm_i915_private *dev_priv = dev->dev_private;
6148 u32 val, base, offset;
6149 int pipe = crtc->pipe, plane = crtc->plane;
6150 int fourcc, pixel_format;
6151 int aligned_height;
6152
66e514c1
DA
6153 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6154 if (!crtc->base.primary->fb) {
1ad292b5
JB
6155 DRM_DEBUG_KMS("failed to alloc fb\n");
6156 return;
6157 }
6158
6159 val = I915_READ(DSPCNTR(plane));
6160
6161 if (INTEL_INFO(dev)->gen >= 4)
6162 if (val & DISPPLANE_TILED)
6163 plane_config->tiled = true;
6164
6165 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6166 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6167 crtc->base.primary->fb->pixel_format = fourcc;
6168 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6169 drm_format_plane_cpp(fourcc, 0) * 8;
6170
6171 if (INTEL_INFO(dev)->gen >= 4) {
6172 if (plane_config->tiled)
6173 offset = I915_READ(DSPTILEOFF(plane));
6174 else
6175 offset = I915_READ(DSPLINOFF(plane));
6176 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6177 } else {
6178 base = I915_READ(DSPADDR(plane));
6179 }
6180 plane_config->base = base;
6181
6182 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6183 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6184 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6185
6186 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 6187 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
1ad292b5 6188
66e514c1 6189 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6190 plane_config->tiled);
6191
66e514c1 6192 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
1ad292b5
JB
6193 aligned_height, PAGE_SIZE);
6194
6195 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6196 pipe, plane, crtc->base.primary->fb->width,
6197 crtc->base.primary->fb->height,
6198 crtc->base.primary->fb->bits_per_pixel, base,
6199 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6200 plane_config->size);
6201
6202}
6203
70b23a98
VS
6204static void chv_crtc_clock_get(struct intel_crtc *crtc,
6205 struct intel_crtc_config *pipe_config)
6206{
6207 struct drm_device *dev = crtc->base.dev;
6208 struct drm_i915_private *dev_priv = dev->dev_private;
6209 int pipe = pipe_config->cpu_transcoder;
6210 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6211 intel_clock_t clock;
6212 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6213 int refclk = 100000;
6214
6215 mutex_lock(&dev_priv->dpio_lock);
6216 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6217 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6218 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6219 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6220 mutex_unlock(&dev_priv->dpio_lock);
6221
6222 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6223 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6224 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6225 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6226 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6227
6228 chv_clock(refclk, &clock);
6229
6230 /* clock.dot is the fast clock */
6231 pipe_config->port_clock = clock.dot / 5;
6232}
6233
0e8ffe1b
DV
6234static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6235 struct intel_crtc_config *pipe_config)
6236{
6237 struct drm_device *dev = crtc->base.dev;
6238 struct drm_i915_private *dev_priv = dev->dev_private;
6239 uint32_t tmp;
6240
b5482bd0
ID
6241 if (!intel_display_power_enabled(dev_priv,
6242 POWER_DOMAIN_PIPE(crtc->pipe)))
6243 return false;
6244
e143a21c 6245 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6246 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6247
0e8ffe1b
DV
6248 tmp = I915_READ(PIPECONF(crtc->pipe));
6249 if (!(tmp & PIPECONF_ENABLE))
6250 return false;
6251
42571aef
VS
6252 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6253 switch (tmp & PIPECONF_BPC_MASK) {
6254 case PIPECONF_6BPC:
6255 pipe_config->pipe_bpp = 18;
6256 break;
6257 case PIPECONF_8BPC:
6258 pipe_config->pipe_bpp = 24;
6259 break;
6260 case PIPECONF_10BPC:
6261 pipe_config->pipe_bpp = 30;
6262 break;
6263 default:
6264 break;
6265 }
6266 }
6267
b5a9fa09
DV
6268 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6269 pipe_config->limited_color_range = true;
6270
282740f7
VS
6271 if (INTEL_INFO(dev)->gen < 4)
6272 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6273
1bd1bd80
DV
6274 intel_get_pipe_timings(crtc, pipe_config);
6275
2fa2fe9a
DV
6276 i9xx_get_pfit_config(crtc, pipe_config);
6277
6c49f241
DV
6278 if (INTEL_INFO(dev)->gen >= 4) {
6279 tmp = I915_READ(DPLL_MD(crtc->pipe));
6280 pipe_config->pixel_multiplier =
6281 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6282 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6283 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6284 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6285 tmp = I915_READ(DPLL(crtc->pipe));
6286 pipe_config->pixel_multiplier =
6287 ((tmp & SDVO_MULTIPLIER_MASK)
6288 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6289 } else {
6290 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6291 * port and will be fixed up in the encoder->get_config
6292 * function. */
6293 pipe_config->pixel_multiplier = 1;
6294 }
8bcc2795
DV
6295 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6296 if (!IS_VALLEYVIEW(dev)) {
6297 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6298 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6299 } else {
6300 /* Mask out read-only status bits. */
6301 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6302 DPLL_PORTC_READY_MASK |
6303 DPLL_PORTB_READY_MASK);
8bcc2795 6304 }
6c49f241 6305
70b23a98
VS
6306 if (IS_CHERRYVIEW(dev))
6307 chv_crtc_clock_get(crtc, pipe_config);
6308 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6309 vlv_crtc_clock_get(crtc, pipe_config);
6310 else
6311 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6312
0e8ffe1b
DV
6313 return true;
6314}
6315
dde86e2d 6316static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6317{
6318 struct drm_i915_private *dev_priv = dev->dev_private;
6319 struct drm_mode_config *mode_config = &dev->mode_config;
13d83a67 6320 struct intel_encoder *encoder;
74cfd7ac 6321 u32 val, final;
13d83a67 6322 bool has_lvds = false;
199e5d79 6323 bool has_cpu_edp = false;
199e5d79 6324 bool has_panel = false;
99eb6a01
KP
6325 bool has_ck505 = false;
6326 bool can_ssc = false;
13d83a67
JB
6327
6328 /* We need to take the global config into account */
199e5d79
KP
6329 list_for_each_entry(encoder, &mode_config->encoder_list,
6330 base.head) {
6331 switch (encoder->type) {
6332 case INTEL_OUTPUT_LVDS:
6333 has_panel = true;
6334 has_lvds = true;
6335 break;
6336 case INTEL_OUTPUT_EDP:
6337 has_panel = true;
2de6905f 6338 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6339 has_cpu_edp = true;
6340 break;
13d83a67
JB
6341 }
6342 }
6343
99eb6a01 6344 if (HAS_PCH_IBX(dev)) {
41aa3448 6345 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6346 can_ssc = has_ck505;
6347 } else {
6348 has_ck505 = false;
6349 can_ssc = true;
6350 }
6351
2de6905f
ID
6352 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6353 has_panel, has_lvds, has_ck505);
13d83a67
JB
6354
6355 /* Ironlake: try to setup display ref clock before DPLL
6356 * enabling. This is only under driver's control after
6357 * PCH B stepping, previous chipset stepping should be
6358 * ignoring this setting.
6359 */
74cfd7ac
CW
6360 val = I915_READ(PCH_DREF_CONTROL);
6361
6362 /* As we must carefully and slowly disable/enable each source in turn,
6363 * compute the final state we want first and check if we need to
6364 * make any changes at all.
6365 */
6366 final = val;
6367 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6368 if (has_ck505)
6369 final |= DREF_NONSPREAD_CK505_ENABLE;
6370 else
6371 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6372
6373 final &= ~DREF_SSC_SOURCE_MASK;
6374 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6375 final &= ~DREF_SSC1_ENABLE;
6376
6377 if (has_panel) {
6378 final |= DREF_SSC_SOURCE_ENABLE;
6379
6380 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6381 final |= DREF_SSC1_ENABLE;
6382
6383 if (has_cpu_edp) {
6384 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6385 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6386 else
6387 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6388 } else
6389 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6390 } else {
6391 final |= DREF_SSC_SOURCE_DISABLE;
6392 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6393 }
6394
6395 if (final == val)
6396 return;
6397
13d83a67 6398 /* Always enable nonspread source */
74cfd7ac 6399 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6400
99eb6a01 6401 if (has_ck505)
74cfd7ac 6402 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6403 else
74cfd7ac 6404 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6405
199e5d79 6406 if (has_panel) {
74cfd7ac
CW
6407 val &= ~DREF_SSC_SOURCE_MASK;
6408 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6409
199e5d79 6410 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6411 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6412 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6413 val |= DREF_SSC1_ENABLE;
e77166b5 6414 } else
74cfd7ac 6415 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6416
6417 /* Get SSC going before enabling the outputs */
74cfd7ac 6418 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6419 POSTING_READ(PCH_DREF_CONTROL);
6420 udelay(200);
6421
74cfd7ac 6422 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6423
6424 /* Enable CPU source on CPU attached eDP */
199e5d79 6425 if (has_cpu_edp) {
99eb6a01 6426 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6427 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6428 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6429 } else
74cfd7ac 6430 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6431 } else
74cfd7ac 6432 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6433
74cfd7ac 6434 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6435 POSTING_READ(PCH_DREF_CONTROL);
6436 udelay(200);
6437 } else {
6438 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6439
74cfd7ac 6440 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6441
6442 /* Turn off CPU output */
74cfd7ac 6443 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6444
74cfd7ac 6445 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6446 POSTING_READ(PCH_DREF_CONTROL);
6447 udelay(200);
6448
6449 /* Turn off the SSC source */
74cfd7ac
CW
6450 val &= ~DREF_SSC_SOURCE_MASK;
6451 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6452
6453 /* Turn off SSC1 */
74cfd7ac 6454 val &= ~DREF_SSC1_ENABLE;
199e5d79 6455
74cfd7ac 6456 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6457 POSTING_READ(PCH_DREF_CONTROL);
6458 udelay(200);
6459 }
74cfd7ac
CW
6460
6461 BUG_ON(val != final);
13d83a67
JB
6462}
6463
f31f2d55 6464static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6465{
f31f2d55 6466 uint32_t tmp;
dde86e2d 6467
0ff066a9
PZ
6468 tmp = I915_READ(SOUTH_CHICKEN2);
6469 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6470 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6471
0ff066a9
PZ
6472 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6473 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6474 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6475
0ff066a9
PZ
6476 tmp = I915_READ(SOUTH_CHICKEN2);
6477 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6478 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6479
0ff066a9
PZ
6480 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6481 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6482 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6483}
6484
6485/* WaMPhyProgramming:hsw */
6486static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6487{
6488 uint32_t tmp;
dde86e2d
PZ
6489
6490 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6491 tmp &= ~(0xFF << 24);
6492 tmp |= (0x12 << 24);
6493 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6494
dde86e2d
PZ
6495 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6496 tmp |= (1 << 11);
6497 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6498
6499 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6500 tmp |= (1 << 11);
6501 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6502
dde86e2d
PZ
6503 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6504 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6505 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6506
6507 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6508 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6509 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6510
0ff066a9
PZ
6511 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6512 tmp &= ~(7 << 13);
6513 tmp |= (5 << 13);
6514 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6515
0ff066a9
PZ
6516 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6517 tmp &= ~(7 << 13);
6518 tmp |= (5 << 13);
6519 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6520
6521 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6522 tmp &= ~0xFF;
6523 tmp |= 0x1C;
6524 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6525
6526 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6527 tmp &= ~0xFF;
6528 tmp |= 0x1C;
6529 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6530
6531 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6532 tmp &= ~(0xFF << 16);
6533 tmp |= (0x1C << 16);
6534 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6535
6536 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6537 tmp &= ~(0xFF << 16);
6538 tmp |= (0x1C << 16);
6539 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6540
0ff066a9
PZ
6541 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6542 tmp |= (1 << 27);
6543 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6544
0ff066a9
PZ
6545 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6546 tmp |= (1 << 27);
6547 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6548
0ff066a9
PZ
6549 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6550 tmp &= ~(0xF << 28);
6551 tmp |= (4 << 28);
6552 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6553
0ff066a9
PZ
6554 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6555 tmp &= ~(0xF << 28);
6556 tmp |= (4 << 28);
6557 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6558}
6559
2fa86a1f
PZ
6560/* Implements 3 different sequences from BSpec chapter "Display iCLK
6561 * Programming" based on the parameters passed:
6562 * - Sequence to enable CLKOUT_DP
6563 * - Sequence to enable CLKOUT_DP without spread
6564 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6565 */
6566static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6567 bool with_fdi)
f31f2d55
PZ
6568{
6569 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6570 uint32_t reg, tmp;
6571
6572 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6573 with_spread = true;
6574 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6575 with_fdi, "LP PCH doesn't have FDI\n"))
6576 with_fdi = false;
f31f2d55
PZ
6577
6578 mutex_lock(&dev_priv->dpio_lock);
6579
6580 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6581 tmp &= ~SBI_SSCCTL_DISABLE;
6582 tmp |= SBI_SSCCTL_PATHALT;
6583 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6584
6585 udelay(24);
6586
2fa86a1f
PZ
6587 if (with_spread) {
6588 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6589 tmp &= ~SBI_SSCCTL_PATHALT;
6590 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6591
2fa86a1f
PZ
6592 if (with_fdi) {
6593 lpt_reset_fdi_mphy(dev_priv);
6594 lpt_program_fdi_mphy(dev_priv);
6595 }
6596 }
dde86e2d 6597
2fa86a1f
PZ
6598 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6599 SBI_GEN0 : SBI_DBUFF0;
6600 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6601 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6602 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6603
6604 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6605}
6606
47701c3b
PZ
6607/* Sequence to disable CLKOUT_DP */
6608static void lpt_disable_clkout_dp(struct drm_device *dev)
6609{
6610 struct drm_i915_private *dev_priv = dev->dev_private;
6611 uint32_t reg, tmp;
6612
6613 mutex_lock(&dev_priv->dpio_lock);
6614
6615 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6616 SBI_GEN0 : SBI_DBUFF0;
6617 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6618 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6619 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6620
6621 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6622 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6623 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6624 tmp |= SBI_SSCCTL_PATHALT;
6625 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6626 udelay(32);
6627 }
6628 tmp |= SBI_SSCCTL_DISABLE;
6629 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6630 }
6631
6632 mutex_unlock(&dev_priv->dpio_lock);
6633}
6634
bf8fa3d3
PZ
6635static void lpt_init_pch_refclk(struct drm_device *dev)
6636{
6637 struct drm_mode_config *mode_config = &dev->mode_config;
6638 struct intel_encoder *encoder;
6639 bool has_vga = false;
6640
6641 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6642 switch (encoder->type) {
6643 case INTEL_OUTPUT_ANALOG:
6644 has_vga = true;
6645 break;
6646 }
6647 }
6648
47701c3b
PZ
6649 if (has_vga)
6650 lpt_enable_clkout_dp(dev, true, true);
6651 else
6652 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6653}
6654
dde86e2d
PZ
6655/*
6656 * Initialize reference clocks when the driver loads
6657 */
6658void intel_init_pch_refclk(struct drm_device *dev)
6659{
6660 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6661 ironlake_init_pch_refclk(dev);
6662 else if (HAS_PCH_LPT(dev))
6663 lpt_init_pch_refclk(dev);
6664}
6665
d9d444cb
JB
6666static int ironlake_get_refclk(struct drm_crtc *crtc)
6667{
6668 struct drm_device *dev = crtc->dev;
6669 struct drm_i915_private *dev_priv = dev->dev_private;
6670 struct intel_encoder *encoder;
d9d444cb
JB
6671 int num_connectors = 0;
6672 bool is_lvds = false;
6673
6c2b7c12 6674 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6675 switch (encoder->type) {
6676 case INTEL_OUTPUT_LVDS:
6677 is_lvds = true;
6678 break;
d9d444cb
JB
6679 }
6680 num_connectors++;
6681 }
6682
6683 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6684 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6685 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6686 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6687 }
6688
6689 return 120000;
6690}
6691
6ff93609 6692static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6693{
c8203565 6694 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6696 int pipe = intel_crtc->pipe;
c8203565
PZ
6697 uint32_t val;
6698
78114071 6699 val = 0;
c8203565 6700
965e0c48 6701 switch (intel_crtc->config.pipe_bpp) {
c8203565 6702 case 18:
dfd07d72 6703 val |= PIPECONF_6BPC;
c8203565
PZ
6704 break;
6705 case 24:
dfd07d72 6706 val |= PIPECONF_8BPC;
c8203565
PZ
6707 break;
6708 case 30:
dfd07d72 6709 val |= PIPECONF_10BPC;
c8203565
PZ
6710 break;
6711 case 36:
dfd07d72 6712 val |= PIPECONF_12BPC;
c8203565
PZ
6713 break;
6714 default:
cc769b62
PZ
6715 /* Case prevented by intel_choose_pipe_bpp_dither. */
6716 BUG();
c8203565
PZ
6717 }
6718
d8b32247 6719 if (intel_crtc->config.dither)
c8203565
PZ
6720 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6721
6ff93609 6722 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6723 val |= PIPECONF_INTERLACED_ILK;
6724 else
6725 val |= PIPECONF_PROGRESSIVE;
6726
50f3b016 6727 if (intel_crtc->config.limited_color_range)
3685a8f3 6728 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6729
c8203565
PZ
6730 I915_WRITE(PIPECONF(pipe), val);
6731 POSTING_READ(PIPECONF(pipe));
6732}
6733
86d3efce
VS
6734/*
6735 * Set up the pipe CSC unit.
6736 *
6737 * Currently only full range RGB to limited range RGB conversion
6738 * is supported, but eventually this should handle various
6739 * RGB<->YCbCr scenarios as well.
6740 */
50f3b016 6741static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6742{
6743 struct drm_device *dev = crtc->dev;
6744 struct drm_i915_private *dev_priv = dev->dev_private;
6745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6746 int pipe = intel_crtc->pipe;
6747 uint16_t coeff = 0x7800; /* 1.0 */
6748
6749 /*
6750 * TODO: Check what kind of values actually come out of the pipe
6751 * with these coeff/postoff values and adjust to get the best
6752 * accuracy. Perhaps we even need to take the bpc value into
6753 * consideration.
6754 */
6755
50f3b016 6756 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6757 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6758
6759 /*
6760 * GY/GU and RY/RU should be the other way around according
6761 * to BSpec, but reality doesn't agree. Just set them up in
6762 * a way that results in the correct picture.
6763 */
6764 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6765 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6766
6767 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6768 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6769
6770 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6771 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6772
6773 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6774 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6775 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6776
6777 if (INTEL_INFO(dev)->gen > 6) {
6778 uint16_t postoff = 0;
6779
50f3b016 6780 if (intel_crtc->config.limited_color_range)
32cf0cb0 6781 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6782
6783 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6784 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6785 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6786
6787 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6788 } else {
6789 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6790
50f3b016 6791 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6792 mode |= CSC_BLACK_SCREEN_OFFSET;
6793
6794 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6795 }
6796}
6797
6ff93609 6798static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6799{
756f85cf
PZ
6800 struct drm_device *dev = crtc->dev;
6801 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6803 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6804 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6805 uint32_t val;
6806
3eff4faa 6807 val = 0;
ee2b0b38 6808
756f85cf 6809 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6810 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6811
6ff93609 6812 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6813 val |= PIPECONF_INTERLACED_ILK;
6814 else
6815 val |= PIPECONF_PROGRESSIVE;
6816
702e7a56
PZ
6817 I915_WRITE(PIPECONF(cpu_transcoder), val);
6818 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6819
6820 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6821 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6822
6823 if (IS_BROADWELL(dev)) {
6824 val = 0;
6825
6826 switch (intel_crtc->config.pipe_bpp) {
6827 case 18:
6828 val |= PIPEMISC_DITHER_6_BPC;
6829 break;
6830 case 24:
6831 val |= PIPEMISC_DITHER_8_BPC;
6832 break;
6833 case 30:
6834 val |= PIPEMISC_DITHER_10_BPC;
6835 break;
6836 case 36:
6837 val |= PIPEMISC_DITHER_12_BPC;
6838 break;
6839 default:
6840 /* Case prevented by pipe_config_set_bpp. */
6841 BUG();
6842 }
6843
6844 if (intel_crtc->config.dither)
6845 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6846
6847 I915_WRITE(PIPEMISC(pipe), val);
6848 }
ee2b0b38
PZ
6849}
6850
6591c6e4 6851static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6852 intel_clock_t *clock,
6853 bool *has_reduced_clock,
6854 intel_clock_t *reduced_clock)
6855{
6856 struct drm_device *dev = crtc->dev;
6857 struct drm_i915_private *dev_priv = dev->dev_private;
6858 struct intel_encoder *intel_encoder;
6859 int refclk;
d4906093 6860 const intel_limit_t *limit;
a16af721 6861 bool ret, is_lvds = false;
79e53945 6862
6591c6e4
PZ
6863 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6864 switch (intel_encoder->type) {
79e53945
JB
6865 case INTEL_OUTPUT_LVDS:
6866 is_lvds = true;
6867 break;
79e53945
JB
6868 }
6869 }
6870
d9d444cb 6871 refclk = ironlake_get_refclk(crtc);
79e53945 6872
d4906093
ML
6873 /*
6874 * Returns a set of divisors for the desired target clock with the given
6875 * refclk, or FALSE. The returned values represent the clock equation:
6876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6877 */
1b894b59 6878 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6879 ret = dev_priv->display.find_dpll(limit, crtc,
6880 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6881 refclk, NULL, clock);
6591c6e4
PZ
6882 if (!ret)
6883 return false;
cda4b7d3 6884
ddc9003c 6885 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6886 /*
6887 * Ensure we match the reduced clock's P to the target clock.
6888 * If the clocks don't match, we can't switch the display clock
6889 * by using the FP0/FP1. In such case we will disable the LVDS
6890 * downclock feature.
6891 */
ee9300bb
DV
6892 *has_reduced_clock =
6893 dev_priv->display.find_dpll(limit, crtc,
6894 dev_priv->lvds_downclock,
6895 refclk, clock,
6896 reduced_clock);
652c393a 6897 }
61e9653f 6898
6591c6e4
PZ
6899 return true;
6900}
6901
d4b1931c
PZ
6902int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6903{
6904 /*
6905 * Account for spread spectrum to avoid
6906 * oversubscribing the link. Max center spread
6907 * is 2.5%; use 5% for safety's sake.
6908 */
6909 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 6910 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
6911}
6912
7429e9d4 6913static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 6914{
7429e9d4 6915 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
6916}
6917
de13a2e3 6918static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 6919 u32 *fp,
9a7c7890 6920 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 6921{
de13a2e3 6922 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
6923 struct drm_device *dev = crtc->dev;
6924 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
6925 struct intel_encoder *intel_encoder;
6926 uint32_t dpll;
6cc5f341 6927 int factor, num_connectors = 0;
09ede541 6928 bool is_lvds = false, is_sdvo = false;
79e53945 6929
de13a2e3
PZ
6930 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6931 switch (intel_encoder->type) {
79e53945
JB
6932 case INTEL_OUTPUT_LVDS:
6933 is_lvds = true;
6934 break;
6935 case INTEL_OUTPUT_SDVO:
7d57382e 6936 case INTEL_OUTPUT_HDMI:
79e53945 6937 is_sdvo = true;
79e53945 6938 break;
79e53945 6939 }
43565a06 6940
c751ce4f 6941 num_connectors++;
79e53945 6942 }
79e53945 6943
c1858123 6944 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
6945 factor = 21;
6946 if (is_lvds) {
6947 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 6948 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 6949 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 6950 factor = 25;
09ede541 6951 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 6952 factor = 20;
c1858123 6953
7429e9d4 6954 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 6955 *fp |= FP_CB_TUNE;
2c07245f 6956
9a7c7890
DV
6957 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6958 *fp2 |= FP_CB_TUNE;
6959
5eddb70b 6960 dpll = 0;
2c07245f 6961
a07d6787
EA
6962 if (is_lvds)
6963 dpll |= DPLLB_MODE_LVDS;
6964 else
6965 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 6966
ef1b460d
DV
6967 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6968 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
6969
6970 if (is_sdvo)
4a33e48d 6971 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 6972 if (intel_crtc->config.has_dp_encoder)
4a33e48d 6973 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 6974
a07d6787 6975 /* compute bitmask from p1 value */
7429e9d4 6976 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 6977 /* also FPA1 */
7429e9d4 6978 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 6979
7429e9d4 6980 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
6981 case 5:
6982 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6983 break;
6984 case 7:
6985 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6986 break;
6987 case 10:
6988 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6989 break;
6990 case 14:
6991 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6992 break;
79e53945
JB
6993 }
6994
b4c09f3b 6995 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 6996 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
6997 else
6998 dpll |= PLL_REF_INPUT_DREFCLK;
6999
959e16d6 7000 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7001}
7002
7003static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7004 int x, int y,
7005 struct drm_framebuffer *fb)
7006{
7007 struct drm_device *dev = crtc->dev;
de13a2e3 7008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7009 int num_connectors = 0;
7010 intel_clock_t clock, reduced_clock;
cbbab5bd 7011 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7012 bool ok, has_reduced_clock = false;
8b47047b 7013 bool is_lvds = false;
de13a2e3 7014 struct intel_encoder *encoder;
e2b78267 7015 struct intel_shared_dpll *pll;
de13a2e3
PZ
7016
7017 for_each_encoder_on_crtc(dev, crtc, encoder) {
7018 switch (encoder->type) {
7019 case INTEL_OUTPUT_LVDS:
7020 is_lvds = true;
7021 break;
de13a2e3
PZ
7022 }
7023
7024 num_connectors++;
a07d6787 7025 }
79e53945 7026
5dc5298b
PZ
7027 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7028 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7029
ff9a6750 7030 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7031 &has_reduced_clock, &reduced_clock);
ee9300bb 7032 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7033 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7034 return -EINVAL;
79e53945 7035 }
f47709a9
DV
7036 /* Compat-code for transition, will disappear. */
7037 if (!intel_crtc->config.clock_set) {
7038 intel_crtc->config.dpll.n = clock.n;
7039 intel_crtc->config.dpll.m1 = clock.m1;
7040 intel_crtc->config.dpll.m2 = clock.m2;
7041 intel_crtc->config.dpll.p1 = clock.p1;
7042 intel_crtc->config.dpll.p2 = clock.p2;
7043 }
79e53945 7044
5dc5298b 7045 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7046 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7047 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7048 if (has_reduced_clock)
7429e9d4 7049 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7050
7429e9d4 7051 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7052 &fp, &reduced_clock,
7053 has_reduced_clock ? &fp2 : NULL);
7054
959e16d6 7055 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7056 intel_crtc->config.dpll_hw_state.fp0 = fp;
7057 if (has_reduced_clock)
7058 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7059 else
7060 intel_crtc->config.dpll_hw_state.fp1 = fp;
7061
b89a1d39 7062 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7063 if (pll == NULL) {
84f44ce7 7064 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7065 pipe_name(intel_crtc->pipe));
4b645f14
JB
7066 return -EINVAL;
7067 }
ee7b9f93 7068 } else
e72f9fbf 7069 intel_put_shared_dpll(intel_crtc);
79e53945 7070
d330a953 7071 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7072 intel_crtc->lowfreq_avail = true;
7073 else
7074 intel_crtc->lowfreq_avail = false;
e2b78267 7075
c8f7a0db 7076 return 0;
79e53945
JB
7077}
7078
eb14cb74
VS
7079static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7080 struct intel_link_m_n *m_n)
7081{
7082 struct drm_device *dev = crtc->base.dev;
7083 struct drm_i915_private *dev_priv = dev->dev_private;
7084 enum pipe pipe = crtc->pipe;
7085
7086 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7087 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7088 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7089 & ~TU_SIZE_MASK;
7090 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7091 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7092 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7093}
7094
7095static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7096 enum transcoder transcoder,
7097 struct intel_link_m_n *m_n)
72419203
DV
7098{
7099 struct drm_device *dev = crtc->base.dev;
7100 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7101 enum pipe pipe = crtc->pipe;
72419203 7102
eb14cb74
VS
7103 if (INTEL_INFO(dev)->gen >= 5) {
7104 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7105 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7106 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7107 & ~TU_SIZE_MASK;
7108 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7109 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7110 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7111 } else {
7112 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7113 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7114 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7115 & ~TU_SIZE_MASK;
7116 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7117 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7118 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7119 }
7120}
7121
7122void intel_dp_get_m_n(struct intel_crtc *crtc,
7123 struct intel_crtc_config *pipe_config)
7124{
7125 if (crtc->config.has_pch_encoder)
7126 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7127 else
7128 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7129 &pipe_config->dp_m_n);
7130}
72419203 7131
eb14cb74
VS
7132static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7133 struct intel_crtc_config *pipe_config)
7134{
7135 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7136 &pipe_config->fdi_m_n);
72419203
DV
7137}
7138
2fa2fe9a
DV
7139static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7140 struct intel_crtc_config *pipe_config)
7141{
7142 struct drm_device *dev = crtc->base.dev;
7143 struct drm_i915_private *dev_priv = dev->dev_private;
7144 uint32_t tmp;
7145
7146 tmp = I915_READ(PF_CTL(crtc->pipe));
7147
7148 if (tmp & PF_ENABLE) {
fd4daa9c 7149 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7150 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7151 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7152
7153 /* We currently do not free assignements of panel fitters on
7154 * ivb/hsw (since we don't use the higher upscaling modes which
7155 * differentiates them) so just WARN about this case for now. */
7156 if (IS_GEN7(dev)) {
7157 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7158 PF_PIPE_SEL_IVB(crtc->pipe));
7159 }
2fa2fe9a 7160 }
79e53945
JB
7161}
7162
4c6baa59
JB
7163static void ironlake_get_plane_config(struct intel_crtc *crtc,
7164 struct intel_plane_config *plane_config)
7165{
7166 struct drm_device *dev = crtc->base.dev;
7167 struct drm_i915_private *dev_priv = dev->dev_private;
7168 u32 val, base, offset;
7169 int pipe = crtc->pipe, plane = crtc->plane;
7170 int fourcc, pixel_format;
7171 int aligned_height;
7172
66e514c1
DA
7173 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7174 if (!crtc->base.primary->fb) {
4c6baa59
JB
7175 DRM_DEBUG_KMS("failed to alloc fb\n");
7176 return;
7177 }
7178
7179 val = I915_READ(DSPCNTR(plane));
7180
7181 if (INTEL_INFO(dev)->gen >= 4)
7182 if (val & DISPPLANE_TILED)
7183 plane_config->tiled = true;
7184
7185 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7186 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7187 crtc->base.primary->fb->pixel_format = fourcc;
7188 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7189 drm_format_plane_cpp(fourcc, 0) * 8;
7190
7191 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7192 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7193 offset = I915_READ(DSPOFFSET(plane));
7194 } else {
7195 if (plane_config->tiled)
7196 offset = I915_READ(DSPTILEOFF(plane));
7197 else
7198 offset = I915_READ(DSPLINOFF(plane));
7199 }
7200 plane_config->base = base;
7201
7202 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7203 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7204 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7205
7206 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 7207 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
4c6baa59 7208
66e514c1 7209 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7210 plane_config->tiled);
7211
66e514c1 7212 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
4c6baa59
JB
7213 aligned_height, PAGE_SIZE);
7214
7215 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7216 pipe, plane, crtc->base.primary->fb->width,
7217 crtc->base.primary->fb->height,
7218 crtc->base.primary->fb->bits_per_pixel, base,
7219 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7220 plane_config->size);
7221}
7222
0e8ffe1b
DV
7223static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7224 struct intel_crtc_config *pipe_config)
7225{
7226 struct drm_device *dev = crtc->base.dev;
7227 struct drm_i915_private *dev_priv = dev->dev_private;
7228 uint32_t tmp;
7229
e143a21c 7230 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7231 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7232
0e8ffe1b
DV
7233 tmp = I915_READ(PIPECONF(crtc->pipe));
7234 if (!(tmp & PIPECONF_ENABLE))
7235 return false;
7236
42571aef
VS
7237 switch (tmp & PIPECONF_BPC_MASK) {
7238 case PIPECONF_6BPC:
7239 pipe_config->pipe_bpp = 18;
7240 break;
7241 case PIPECONF_8BPC:
7242 pipe_config->pipe_bpp = 24;
7243 break;
7244 case PIPECONF_10BPC:
7245 pipe_config->pipe_bpp = 30;
7246 break;
7247 case PIPECONF_12BPC:
7248 pipe_config->pipe_bpp = 36;
7249 break;
7250 default:
7251 break;
7252 }
7253
b5a9fa09
DV
7254 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7255 pipe_config->limited_color_range = true;
7256
ab9412ba 7257 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7258 struct intel_shared_dpll *pll;
7259
88adfff1
DV
7260 pipe_config->has_pch_encoder = true;
7261
627eb5a3
DV
7262 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7263 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7264 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7265
7266 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7267
c0d43d62 7268 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7269 pipe_config->shared_dpll =
7270 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7271 } else {
7272 tmp = I915_READ(PCH_DPLL_SEL);
7273 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7274 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7275 else
7276 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7277 }
66e985c0
DV
7278
7279 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7280
7281 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7282 &pipe_config->dpll_hw_state));
c93f54cf
DV
7283
7284 tmp = pipe_config->dpll_hw_state.dpll;
7285 pipe_config->pixel_multiplier =
7286 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7287 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7288
7289 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7290 } else {
7291 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7292 }
7293
1bd1bd80
DV
7294 intel_get_pipe_timings(crtc, pipe_config);
7295
2fa2fe9a
DV
7296 ironlake_get_pfit_config(crtc, pipe_config);
7297
0e8ffe1b
DV
7298 return true;
7299}
7300
be256dc7
PZ
7301static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7302{
7303 struct drm_device *dev = dev_priv->dev;
7304 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7305 struct intel_crtc *crtc;
be256dc7 7306
d3fcc808 7307 for_each_intel_crtc(dev, crtc)
798183c5 7308 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7309 pipe_name(crtc->pipe));
7310
7311 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7312 WARN(plls->spll_refcount, "SPLL enabled\n");
7313 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7314 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7315 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7316 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7317 "CPU PWM1 enabled\n");
7318 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7319 "CPU PWM2 enabled\n");
7320 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7321 "PCH PWM1 enabled\n");
7322 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7323 "Utility pin enabled\n");
7324 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7325
9926ada1
PZ
7326 /*
7327 * In theory we can still leave IRQs enabled, as long as only the HPD
7328 * interrupts remain enabled. We used to check for that, but since it's
7329 * gen-specific and since we only disable LCPLL after we fully disable
7330 * the interrupts, the check below should be enough.
7331 */
7332 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
be256dc7
PZ
7333}
7334
3c4c9b81
PZ
7335static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7336{
7337 struct drm_device *dev = dev_priv->dev;
7338
7339 if (IS_HASWELL(dev)) {
7340 mutex_lock(&dev_priv->rps.hw_lock);
7341 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7342 val))
7343 DRM_ERROR("Failed to disable D_COMP\n");
7344 mutex_unlock(&dev_priv->rps.hw_lock);
7345 } else {
7346 I915_WRITE(D_COMP, val);
7347 }
7348 POSTING_READ(D_COMP);
be256dc7
PZ
7349}
7350
7351/*
7352 * This function implements pieces of two sequences from BSpec:
7353 * - Sequence for display software to disable LCPLL
7354 * - Sequence for display software to allow package C8+
7355 * The steps implemented here are just the steps that actually touch the LCPLL
7356 * register. Callers should take care of disabling all the display engine
7357 * functions, doing the mode unset, fixing interrupts, etc.
7358 */
6ff58d53
PZ
7359static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7360 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7361{
7362 uint32_t val;
7363
7364 assert_can_disable_lcpll(dev_priv);
7365
7366 val = I915_READ(LCPLL_CTL);
7367
7368 if (switch_to_fclk) {
7369 val |= LCPLL_CD_SOURCE_FCLK;
7370 I915_WRITE(LCPLL_CTL, val);
7371
7372 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7373 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7374 DRM_ERROR("Switching to FCLK failed\n");
7375
7376 val = I915_READ(LCPLL_CTL);
7377 }
7378
7379 val |= LCPLL_PLL_DISABLE;
7380 I915_WRITE(LCPLL_CTL, val);
7381 POSTING_READ(LCPLL_CTL);
7382
7383 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7384 DRM_ERROR("LCPLL still locked\n");
7385
7386 val = I915_READ(D_COMP);
7387 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7388 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7389 ndelay(100);
7390
7391 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7392 DRM_ERROR("D_COMP RCOMP still in progress\n");
7393
7394 if (allow_power_down) {
7395 val = I915_READ(LCPLL_CTL);
7396 val |= LCPLL_POWER_DOWN_ALLOW;
7397 I915_WRITE(LCPLL_CTL, val);
7398 POSTING_READ(LCPLL_CTL);
7399 }
7400}
7401
7402/*
7403 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7404 * source.
7405 */
6ff58d53 7406static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7407{
7408 uint32_t val;
a8a8bd54 7409 unsigned long irqflags;
be256dc7
PZ
7410
7411 val = I915_READ(LCPLL_CTL);
7412
7413 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7414 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7415 return;
7416
a8a8bd54
PZ
7417 /*
7418 * Make sure we're not on PC8 state before disabling PC8, otherwise
7419 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7420 *
7421 * The other problem is that hsw_restore_lcpll() is called as part of
7422 * the runtime PM resume sequence, so we can't just call
7423 * gen6_gt_force_wake_get() because that function calls
7424 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7425 * while we are on the resume sequence. So to solve this problem we have
7426 * to call special forcewake code that doesn't touch runtime PM and
7427 * doesn't enable the forcewake delayed work.
7428 */
7429 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7430 if (dev_priv->uncore.forcewake_count++ == 0)
7431 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7432 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7433
be256dc7
PZ
7434 if (val & LCPLL_POWER_DOWN_ALLOW) {
7435 val &= ~LCPLL_POWER_DOWN_ALLOW;
7436 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7437 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7438 }
7439
7440 val = I915_READ(D_COMP);
7441 val |= D_COMP_COMP_FORCE;
7442 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7443 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7444
7445 val = I915_READ(LCPLL_CTL);
7446 val &= ~LCPLL_PLL_DISABLE;
7447 I915_WRITE(LCPLL_CTL, val);
7448
7449 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7450 DRM_ERROR("LCPLL not locked yet\n");
7451
7452 if (val & LCPLL_CD_SOURCE_FCLK) {
7453 val = I915_READ(LCPLL_CTL);
7454 val &= ~LCPLL_CD_SOURCE_FCLK;
7455 I915_WRITE(LCPLL_CTL, val);
7456
7457 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7458 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7459 DRM_ERROR("Switching back to LCPLL failed\n");
7460 }
215733fa 7461
a8a8bd54
PZ
7462 /* See the big comment above. */
7463 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7464 if (--dev_priv->uncore.forcewake_count == 0)
7465 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7466 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7467}
7468
765dab67
PZ
7469/*
7470 * Package states C8 and deeper are really deep PC states that can only be
7471 * reached when all the devices on the system allow it, so even if the graphics
7472 * device allows PC8+, it doesn't mean the system will actually get to these
7473 * states. Our driver only allows PC8+ when going into runtime PM.
7474 *
7475 * The requirements for PC8+ are that all the outputs are disabled, the power
7476 * well is disabled and most interrupts are disabled, and these are also
7477 * requirements for runtime PM. When these conditions are met, we manually do
7478 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7479 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7480 * hang the machine.
7481 *
7482 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7483 * the state of some registers, so when we come back from PC8+ we need to
7484 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7485 * need to take care of the registers kept by RC6. Notice that this happens even
7486 * if we don't put the device in PCI D3 state (which is what currently happens
7487 * because of the runtime PM support).
7488 *
7489 * For more, read "Display Sequences for Package C8" on the hardware
7490 * documentation.
7491 */
a14cb6fc 7492void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7493{
c67a470b
PZ
7494 struct drm_device *dev = dev_priv->dev;
7495 uint32_t val;
7496
c67a470b
PZ
7497 DRM_DEBUG_KMS("Enabling package C8+\n");
7498
c67a470b
PZ
7499 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7500 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7501 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7502 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7503 }
7504
7505 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7506 hsw_disable_lcpll(dev_priv, true, true);
7507}
7508
a14cb6fc 7509void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7510{
7511 struct drm_device *dev = dev_priv->dev;
7512 uint32_t val;
7513
c67a470b
PZ
7514 DRM_DEBUG_KMS("Disabling package C8+\n");
7515
7516 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7517 lpt_init_pch_refclk(dev);
7518
7519 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7520 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7521 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7522 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7523 }
7524
7525 intel_prepare_ddi(dev);
c67a470b
PZ
7526}
7527
9a952a0d
PZ
7528static void snb_modeset_global_resources(struct drm_device *dev)
7529{
7530 modeset_update_crtc_power_domains(dev);
7531}
7532
4f074129
ID
7533static void haswell_modeset_global_resources(struct drm_device *dev)
7534{
da723569 7535 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7536}
7537
09b4ddf9 7538static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7539 int x, int y,
7540 struct drm_framebuffer *fb)
7541{
09b4ddf9 7542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7543
566b734a 7544 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7545 return -EINVAL;
566b734a 7546 intel_ddi_pll_enable(intel_crtc);
6441ab5f 7547
644cef34
DV
7548 intel_crtc->lowfreq_avail = false;
7549
c8f7a0db 7550 return 0;
79e53945
JB
7551}
7552
0e8ffe1b
DV
7553static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7554 struct intel_crtc_config *pipe_config)
7555{
7556 struct drm_device *dev = crtc->base.dev;
7557 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7558 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7559 uint32_t tmp;
7560
b5482bd0
ID
7561 if (!intel_display_power_enabled(dev_priv,
7562 POWER_DOMAIN_PIPE(crtc->pipe)))
7563 return false;
7564
e143a21c 7565 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7566 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7567
eccb140b
DV
7568 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7569 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7570 enum pipe trans_edp_pipe;
7571 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7572 default:
7573 WARN(1, "unknown pipe linked to edp transcoder\n");
7574 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7575 case TRANS_DDI_EDP_INPUT_A_ON:
7576 trans_edp_pipe = PIPE_A;
7577 break;
7578 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7579 trans_edp_pipe = PIPE_B;
7580 break;
7581 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7582 trans_edp_pipe = PIPE_C;
7583 break;
7584 }
7585
7586 if (trans_edp_pipe == crtc->pipe)
7587 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7588 }
7589
da7e29bd 7590 if (!intel_display_power_enabled(dev_priv,
eccb140b 7591 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7592 return false;
7593
eccb140b 7594 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7595 if (!(tmp & PIPECONF_ENABLE))
7596 return false;
7597
88adfff1 7598 /*
f196e6be 7599 * Haswell has only FDI/PCH transcoder A. It is which is connected to
88adfff1
DV
7600 * DDI E. So just check whether this pipe is wired to DDI E and whether
7601 * the PCH transcoder is on.
7602 */
eccb140b 7603 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
88adfff1 7604 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
ab9412ba 7605 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
88adfff1
DV
7606 pipe_config->has_pch_encoder = true;
7607
627eb5a3
DV
7608 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7609 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7610 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7611
7612 ironlake_get_fdi_m_n_config(crtc, pipe_config);
627eb5a3
DV
7613 }
7614
1bd1bd80
DV
7615 intel_get_pipe_timings(crtc, pipe_config);
7616
2fa2fe9a 7617 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7618 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7619 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7620
e59150dc
JB
7621 if (IS_HASWELL(dev))
7622 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7623 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7624
6c49f241
DV
7625 pipe_config->pixel_multiplier = 1;
7626
0e8ffe1b
DV
7627 return true;
7628}
7629
1a91510d
JN
7630static struct {
7631 int clock;
7632 u32 config;
7633} hdmi_audio_clock[] = {
7634 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7635 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7636 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7637 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7638 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7639 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7640 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7641 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7642 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7643 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7644};
7645
7646/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7647static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7648{
7649 int i;
7650
7651 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7652 if (mode->clock == hdmi_audio_clock[i].clock)
7653 break;
7654 }
7655
7656 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7657 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7658 i = 1;
7659 }
7660
7661 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7662 hdmi_audio_clock[i].clock,
7663 hdmi_audio_clock[i].config);
7664
7665 return hdmi_audio_clock[i].config;
7666}
7667
3a9627f4
WF
7668static bool intel_eld_uptodate(struct drm_connector *connector,
7669 int reg_eldv, uint32_t bits_eldv,
7670 int reg_elda, uint32_t bits_elda,
7671 int reg_edid)
7672{
7673 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7674 uint8_t *eld = connector->eld;
7675 uint32_t i;
7676
7677 i = I915_READ(reg_eldv);
7678 i &= bits_eldv;
7679
7680 if (!eld[0])
7681 return !i;
7682
7683 if (!i)
7684 return false;
7685
7686 i = I915_READ(reg_elda);
7687 i &= ~bits_elda;
7688 I915_WRITE(reg_elda, i);
7689
7690 for (i = 0; i < eld[2]; i++)
7691 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7692 return false;
7693
7694 return true;
7695}
7696
e0dac65e 7697static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7698 struct drm_crtc *crtc,
7699 struct drm_display_mode *mode)
e0dac65e
WF
7700{
7701 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7702 uint8_t *eld = connector->eld;
7703 uint32_t eldv;
7704 uint32_t len;
7705 uint32_t i;
7706
7707 i = I915_READ(G4X_AUD_VID_DID);
7708
7709 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7710 eldv = G4X_ELDV_DEVCL_DEVBLC;
7711 else
7712 eldv = G4X_ELDV_DEVCTG;
7713
3a9627f4
WF
7714 if (intel_eld_uptodate(connector,
7715 G4X_AUD_CNTL_ST, eldv,
7716 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7717 G4X_HDMIW_HDMIEDID))
7718 return;
7719
e0dac65e
WF
7720 i = I915_READ(G4X_AUD_CNTL_ST);
7721 i &= ~(eldv | G4X_ELD_ADDR);
7722 len = (i >> 9) & 0x1f; /* ELD buffer size */
7723 I915_WRITE(G4X_AUD_CNTL_ST, i);
7724
7725 if (!eld[0])
7726 return;
7727
7728 len = min_t(uint8_t, eld[2], len);
7729 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7730 for (i = 0; i < len; i++)
7731 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7732
7733 i = I915_READ(G4X_AUD_CNTL_ST);
7734 i |= eldv;
7735 I915_WRITE(G4X_AUD_CNTL_ST, i);
7736}
7737
83358c85 7738static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7739 struct drm_crtc *crtc,
7740 struct drm_display_mode *mode)
83358c85
WX
7741{
7742 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7743 uint8_t *eld = connector->eld;
83358c85
WX
7744 uint32_t eldv;
7745 uint32_t i;
7746 int len;
7747 int pipe = to_intel_crtc(crtc)->pipe;
7748 int tmp;
7749
7750 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7751 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7752 int aud_config = HSW_AUD_CFG(pipe);
7753 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7754
83358c85
WX
7755 /* Audio output enable */
7756 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7757 tmp = I915_READ(aud_cntrl_st2);
7758 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7759 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7760 POSTING_READ(aud_cntrl_st2);
83358c85 7761
c7905792 7762 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7763
7764 /* Set ELD valid state */
7765 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7766 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7767 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7768 I915_WRITE(aud_cntrl_st2, tmp);
7769 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7770 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7771
7772 /* Enable HDMI mode */
7773 tmp = I915_READ(aud_config);
7e7cb34f 7774 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7775 /* clear N_programing_enable and N_value_index */
7776 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7777 I915_WRITE(aud_config, tmp);
7778
7779 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7780
7781 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7782
7783 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7784 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7785 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7786 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7787 } else {
7788 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7789 }
83358c85
WX
7790
7791 if (intel_eld_uptodate(connector,
7792 aud_cntrl_st2, eldv,
7793 aud_cntl_st, IBX_ELD_ADDRESS,
7794 hdmiw_hdmiedid))
7795 return;
7796
7797 i = I915_READ(aud_cntrl_st2);
7798 i &= ~eldv;
7799 I915_WRITE(aud_cntrl_st2, i);
7800
7801 if (!eld[0])
7802 return;
7803
7804 i = I915_READ(aud_cntl_st);
7805 i &= ~IBX_ELD_ADDRESS;
7806 I915_WRITE(aud_cntl_st, i);
7807 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7808 DRM_DEBUG_DRIVER("port num:%d\n", i);
7809
7810 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7811 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7812 for (i = 0; i < len; i++)
7813 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7814
7815 i = I915_READ(aud_cntrl_st2);
7816 i |= eldv;
7817 I915_WRITE(aud_cntrl_st2, i);
7818
7819}
7820
e0dac65e 7821static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7822 struct drm_crtc *crtc,
7823 struct drm_display_mode *mode)
e0dac65e
WF
7824{
7825 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7826 uint8_t *eld = connector->eld;
7827 uint32_t eldv;
7828 uint32_t i;
7829 int len;
7830 int hdmiw_hdmiedid;
b6daa025 7831 int aud_config;
e0dac65e
WF
7832 int aud_cntl_st;
7833 int aud_cntrl_st2;
9b138a83 7834 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7835
b3f33cbf 7836 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7837 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7838 aud_config = IBX_AUD_CFG(pipe);
7839 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 7840 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
7841 } else if (IS_VALLEYVIEW(connector->dev)) {
7842 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7843 aud_config = VLV_AUD_CFG(pipe);
7844 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7845 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 7846 } else {
9b138a83
WX
7847 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7848 aud_config = CPT_AUD_CFG(pipe);
7849 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 7850 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
7851 }
7852
9b138a83 7853 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 7854
9ca2fe73
ML
7855 if (IS_VALLEYVIEW(connector->dev)) {
7856 struct intel_encoder *intel_encoder;
7857 struct intel_digital_port *intel_dig_port;
7858
7859 intel_encoder = intel_attached_encoder(connector);
7860 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7861 i = intel_dig_port->port;
7862 } else {
7863 i = I915_READ(aud_cntl_st);
7864 i = (i >> 29) & DIP_PORT_SEL_MASK;
7865 /* DIP_Port_Select, 0x1 = PortB */
7866 }
7867
e0dac65e
WF
7868 if (!i) {
7869 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7870 /* operate blindly on all ports */
1202b4c6
WF
7871 eldv = IBX_ELD_VALIDB;
7872 eldv |= IBX_ELD_VALIDB << 4;
7873 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 7874 } else {
2582a850 7875 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 7876 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
7877 }
7878
3a9627f4
WF
7879 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7880 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7881 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 7882 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7883 } else {
7884 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7885 }
e0dac65e 7886
3a9627f4
WF
7887 if (intel_eld_uptodate(connector,
7888 aud_cntrl_st2, eldv,
7889 aud_cntl_st, IBX_ELD_ADDRESS,
7890 hdmiw_hdmiedid))
7891 return;
7892
e0dac65e
WF
7893 i = I915_READ(aud_cntrl_st2);
7894 i &= ~eldv;
7895 I915_WRITE(aud_cntrl_st2, i);
7896
7897 if (!eld[0])
7898 return;
7899
e0dac65e 7900 i = I915_READ(aud_cntl_st);
1202b4c6 7901 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
7902 I915_WRITE(aud_cntl_st, i);
7903
7904 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7905 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7906 for (i = 0; i < len; i++)
7907 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7908
7909 i = I915_READ(aud_cntrl_st2);
7910 i |= eldv;
7911 I915_WRITE(aud_cntrl_st2, i);
7912}
7913
7914void intel_write_eld(struct drm_encoder *encoder,
7915 struct drm_display_mode *mode)
7916{
7917 struct drm_crtc *crtc = encoder->crtc;
7918 struct drm_connector *connector;
7919 struct drm_device *dev = encoder->dev;
7920 struct drm_i915_private *dev_priv = dev->dev_private;
7921
7922 connector = drm_select_eld(encoder, mode);
7923 if (!connector)
7924 return;
7925
7926 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7927 connector->base.id,
c23cc417 7928 connector->name,
e0dac65e 7929 connector->encoder->base.id,
8e329a03 7930 connector->encoder->name);
e0dac65e
WF
7931
7932 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7933
7934 if (dev_priv->display.write_eld)
34427052 7935 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
7936}
7937
560b85bb
CW
7938static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7939{
7940 struct drm_device *dev = crtc->dev;
7941 struct drm_i915_private *dev_priv = dev->dev_private;
7942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4b0e333e 7943 uint32_t cntl;
560b85bb 7944
4b0e333e 7945 if (base != intel_crtc->cursor_base) {
560b85bb
CW
7946 /* On these chipsets we can only modify the base whilst
7947 * the cursor is disabled.
7948 */
4b0e333e
CW
7949 if (intel_crtc->cursor_cntl) {
7950 I915_WRITE(_CURACNTR, 0);
7951 POSTING_READ(_CURACNTR);
7952 intel_crtc->cursor_cntl = 0;
7953 }
7954
9db4a9c7 7955 I915_WRITE(_CURABASE, base);
4b0e333e
CW
7956 POSTING_READ(_CURABASE);
7957 }
560b85bb 7958
4b0e333e
CW
7959 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7960 cntl = 0;
7961 if (base)
7962 cntl = (CURSOR_ENABLE |
560b85bb 7963 CURSOR_GAMMA_ENABLE |
4b0e333e
CW
7964 CURSOR_FORMAT_ARGB);
7965 if (intel_crtc->cursor_cntl != cntl) {
7966 I915_WRITE(_CURACNTR, cntl);
7967 POSTING_READ(_CURACNTR);
7968 intel_crtc->cursor_cntl = cntl;
7969 }
560b85bb
CW
7970}
7971
7972static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7973{
7974 struct drm_device *dev = crtc->dev;
7975 struct drm_i915_private *dev_priv = dev->dev_private;
7976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7977 int pipe = intel_crtc->pipe;
4b0e333e 7978 uint32_t cntl;
4726e0b0 7979
4b0e333e
CW
7980 cntl = 0;
7981 if (base) {
7982 cntl = MCURSOR_GAMMA_ENABLE;
7983 switch (intel_crtc->cursor_width) {
4726e0b0
SK
7984 case 64:
7985 cntl |= CURSOR_MODE_64_ARGB_AX;
7986 break;
7987 case 128:
7988 cntl |= CURSOR_MODE_128_ARGB_AX;
7989 break;
7990 case 256:
7991 cntl |= CURSOR_MODE_256_ARGB_AX;
7992 break;
7993 default:
7994 WARN_ON(1);
7995 return;
560b85bb 7996 }
4b0e333e
CW
7997 cntl |= pipe << 28; /* Connect to correct pipe */
7998 }
7999 if (intel_crtc->cursor_cntl != cntl) {
9db4a9c7 8000 I915_WRITE(CURCNTR(pipe), cntl);
4b0e333e
CW
8001 POSTING_READ(CURCNTR(pipe));
8002 intel_crtc->cursor_cntl = cntl;
560b85bb 8003 }
4b0e333e 8004
560b85bb 8005 /* and commit changes on next vblank */
9db4a9c7 8006 I915_WRITE(CURBASE(pipe), base);
b2ea8ef5 8007 POSTING_READ(CURBASE(pipe));
560b85bb
CW
8008}
8009
65a21cd6
JB
8010static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
8011{
8012 struct drm_device *dev = crtc->dev;
8013 struct drm_i915_private *dev_priv = dev->dev_private;
8014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8015 int pipe = intel_crtc->pipe;
4b0e333e
CW
8016 uint32_t cntl;
8017
8018 cntl = 0;
8019 if (base) {
8020 cntl = MCURSOR_GAMMA_ENABLE;
8021 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8022 case 64:
8023 cntl |= CURSOR_MODE_64_ARGB_AX;
8024 break;
8025 case 128:
8026 cntl |= CURSOR_MODE_128_ARGB_AX;
8027 break;
8028 case 256:
8029 cntl |= CURSOR_MODE_256_ARGB_AX;
8030 break;
8031 default:
8032 WARN_ON(1);
8033 return;
65a21cd6 8034 }
4b0e333e
CW
8035 }
8036 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8037 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 8038
4b0e333e
CW
8039 if (intel_crtc->cursor_cntl != cntl) {
8040 I915_WRITE(CURCNTR(pipe), cntl);
8041 POSTING_READ(CURCNTR(pipe));
8042 intel_crtc->cursor_cntl = cntl;
65a21cd6 8043 }
4b0e333e 8044
65a21cd6 8045 /* and commit changes on next vblank */
5efb3e28
VS
8046 I915_WRITE(CURBASE(pipe), base);
8047 POSTING_READ(CURBASE(pipe));
65a21cd6
JB
8048}
8049
cda4b7d3 8050/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8051static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8052 bool on)
cda4b7d3
CW
8053{
8054 struct drm_device *dev = crtc->dev;
8055 struct drm_i915_private *dev_priv = dev->dev_private;
8056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8057 int pipe = intel_crtc->pipe;
3d7d6510
MR
8058 int x = crtc->cursor_x;
8059 int y = crtc->cursor_y;
d6e4db15 8060 u32 base = 0, pos = 0;
cda4b7d3 8061
d6e4db15 8062 if (on)
cda4b7d3 8063 base = intel_crtc->cursor_addr;
cda4b7d3 8064
d6e4db15
VS
8065 if (x >= intel_crtc->config.pipe_src_w)
8066 base = 0;
8067
8068 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8069 base = 0;
8070
8071 if (x < 0) {
efc9064e 8072 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8073 base = 0;
8074
8075 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8076 x = -x;
8077 }
8078 pos |= x << CURSOR_X_SHIFT;
8079
8080 if (y < 0) {
efc9064e 8081 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8082 base = 0;
8083
8084 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8085 y = -y;
8086 }
8087 pos |= y << CURSOR_Y_SHIFT;
8088
4b0e333e 8089 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8090 return;
8091
5efb3e28
VS
8092 I915_WRITE(CURPOS(pipe), pos);
8093
8094 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
65a21cd6 8095 ivb_update_cursor(crtc, base);
5efb3e28
VS
8096 else if (IS_845G(dev) || IS_I865G(dev))
8097 i845_update_cursor(crtc, base);
8098 else
8099 i9xx_update_cursor(crtc, base);
4b0e333e 8100 intel_crtc->cursor_base = base;
cda4b7d3
CW
8101}
8102
e3287951
MR
8103/*
8104 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8105 *
8106 * Note that the object's reference will be consumed if the update fails. If
8107 * the update succeeds, the reference of the old object (if any) will be
8108 * consumed.
8109 */
8110static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8111 struct drm_i915_gem_object *obj,
8112 uint32_t width, uint32_t height)
79e53945
JB
8113{
8114 struct drm_device *dev = crtc->dev;
8115 struct drm_i915_private *dev_priv = dev->dev_private;
8116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8117 enum pipe pipe = intel_crtc->pipe;
64f962e3 8118 unsigned old_width;
cda4b7d3 8119 uint32_t addr;
3f8bc370 8120 int ret;
79e53945 8121
79e53945 8122 /* if we want to turn off the cursor ignore width and height */
e3287951 8123 if (!obj) {
28c97730 8124 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8125 addr = 0;
05394f39 8126 obj = NULL;
5004417d 8127 mutex_lock(&dev->struct_mutex);
3f8bc370 8128 goto finish;
79e53945
JB
8129 }
8130
4726e0b0
SK
8131 /* Check for which cursor types we support */
8132 if (!((width == 64 && height == 64) ||
8133 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8134 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8135 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8136 return -EINVAL;
8137 }
8138
05394f39 8139 if (obj->base.size < width * height * 4) {
e3287951 8140 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8141 ret = -ENOMEM;
8142 goto fail;
79e53945
JB
8143 }
8144
71acb5eb 8145 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8146 mutex_lock(&dev->struct_mutex);
3d13ef2e 8147 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8148 unsigned alignment;
8149
d9e86c0e 8150 if (obj->tiling_mode) {
3b25b31f 8151 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8152 ret = -EINVAL;
8153 goto fail_locked;
8154 }
8155
693db184
CW
8156 /* Note that the w/a also requires 2 PTE of padding following
8157 * the bo. We currently fill all unused PTE with the shadow
8158 * page and so we should always have valid PTE following the
8159 * cursor preventing the VT-d warning.
8160 */
8161 alignment = 0;
8162 if (need_vtd_wa(dev))
8163 alignment = 64*1024;
8164
8165 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8166 if (ret) {
3b25b31f 8167 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 8168 goto fail_locked;
e7b526bb
CW
8169 }
8170
d9e86c0e
CW
8171 ret = i915_gem_object_put_fence(obj);
8172 if (ret) {
3b25b31f 8173 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
8174 goto fail_unpin;
8175 }
8176
f343c5f6 8177 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 8178 } else {
6eeefaf3 8179 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8180 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8181 if (ret) {
3b25b31f 8182 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8183 goto fail_locked;
71acb5eb 8184 }
00731155 8185 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8186 }
8187
a6c45cf0 8188 if (IS_GEN2(dev))
14b60391
JB
8189 I915_WRITE(CURSIZE, (height << 12) | width);
8190
3f8bc370 8191 finish:
3f8bc370 8192 if (intel_crtc->cursor_bo) {
00731155 8193 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8194 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8195 }
80824003 8196
a071fa00
DV
8197 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8198 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8199 mutex_unlock(&dev->struct_mutex);
3f8bc370 8200
64f962e3
CW
8201 old_width = intel_crtc->cursor_width;
8202
3f8bc370 8203 intel_crtc->cursor_addr = addr;
05394f39 8204 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8205 intel_crtc->cursor_width = width;
8206 intel_crtc->cursor_height = height;
8207
64f962e3
CW
8208 if (intel_crtc->active) {
8209 if (old_width != width)
8210 intel_update_watermarks(crtc);
f2f5f771 8211 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8212 }
3f8bc370 8213
79e53945 8214 return 0;
e7b526bb 8215fail_unpin:
cc98b413 8216 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8217fail_locked:
34b8686e 8218 mutex_unlock(&dev->struct_mutex);
bc9025bd 8219fail:
05394f39 8220 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8221 return ret;
79e53945
JB
8222}
8223
79e53945 8224static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8225 u16 *blue, uint32_t start, uint32_t size)
79e53945 8226{
7203425a 8227 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8229
7203425a 8230 for (i = start; i < end; i++) {
79e53945
JB
8231 intel_crtc->lut_r[i] = red[i] >> 8;
8232 intel_crtc->lut_g[i] = green[i] >> 8;
8233 intel_crtc->lut_b[i] = blue[i] >> 8;
8234 }
8235
8236 intel_crtc_load_lut(crtc);
8237}
8238
79e53945
JB
8239/* VESA 640x480x72Hz mode to set on the pipe */
8240static struct drm_display_mode load_detect_mode = {
8241 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8242 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8243};
8244
a8bb6818
DV
8245struct drm_framebuffer *
8246__intel_framebuffer_create(struct drm_device *dev,
8247 struct drm_mode_fb_cmd2 *mode_cmd,
8248 struct drm_i915_gem_object *obj)
d2dff872
CW
8249{
8250 struct intel_framebuffer *intel_fb;
8251 int ret;
8252
8253 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8254 if (!intel_fb) {
8255 drm_gem_object_unreference_unlocked(&obj->base);
8256 return ERR_PTR(-ENOMEM);
8257 }
8258
8259 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8260 if (ret)
8261 goto err;
d2dff872
CW
8262
8263 return &intel_fb->base;
dd4916c5
DV
8264err:
8265 drm_gem_object_unreference_unlocked(&obj->base);
8266 kfree(intel_fb);
8267
8268 return ERR_PTR(ret);
d2dff872
CW
8269}
8270
b5ea642a 8271static struct drm_framebuffer *
a8bb6818
DV
8272intel_framebuffer_create(struct drm_device *dev,
8273 struct drm_mode_fb_cmd2 *mode_cmd,
8274 struct drm_i915_gem_object *obj)
8275{
8276 struct drm_framebuffer *fb;
8277 int ret;
8278
8279 ret = i915_mutex_lock_interruptible(dev);
8280 if (ret)
8281 return ERR_PTR(ret);
8282 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8283 mutex_unlock(&dev->struct_mutex);
8284
8285 return fb;
8286}
8287
d2dff872
CW
8288static u32
8289intel_framebuffer_pitch_for_width(int width, int bpp)
8290{
8291 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8292 return ALIGN(pitch, 64);
8293}
8294
8295static u32
8296intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8297{
8298 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8299 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8300}
8301
8302static struct drm_framebuffer *
8303intel_framebuffer_create_for_mode(struct drm_device *dev,
8304 struct drm_display_mode *mode,
8305 int depth, int bpp)
8306{
8307 struct drm_i915_gem_object *obj;
0fed39bd 8308 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8309
8310 obj = i915_gem_alloc_object(dev,
8311 intel_framebuffer_size_for_mode(mode, bpp));
8312 if (obj == NULL)
8313 return ERR_PTR(-ENOMEM);
8314
8315 mode_cmd.width = mode->hdisplay;
8316 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8317 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8318 bpp);
5ca0c34a 8319 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8320
8321 return intel_framebuffer_create(dev, &mode_cmd, obj);
8322}
8323
8324static struct drm_framebuffer *
8325mode_fits_in_fbdev(struct drm_device *dev,
8326 struct drm_display_mode *mode)
8327{
4520f53a 8328#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8329 struct drm_i915_private *dev_priv = dev->dev_private;
8330 struct drm_i915_gem_object *obj;
8331 struct drm_framebuffer *fb;
8332
4c0e5528 8333 if (!dev_priv->fbdev)
d2dff872
CW
8334 return NULL;
8335
4c0e5528 8336 if (!dev_priv->fbdev->fb)
d2dff872
CW
8337 return NULL;
8338
4c0e5528
DV
8339 obj = dev_priv->fbdev->fb->obj;
8340 BUG_ON(!obj);
8341
8bcd4553 8342 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8343 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8344 fb->bits_per_pixel))
d2dff872
CW
8345 return NULL;
8346
01f2c773 8347 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8348 return NULL;
8349
8350 return fb;
4520f53a
DV
8351#else
8352 return NULL;
8353#endif
d2dff872
CW
8354}
8355
d2434ab7 8356bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8357 struct drm_display_mode *mode,
51fd371b
RC
8358 struct intel_load_detect_pipe *old,
8359 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8360{
8361 struct intel_crtc *intel_crtc;
d2434ab7
DV
8362 struct intel_encoder *intel_encoder =
8363 intel_attached_encoder(connector);
79e53945 8364 struct drm_crtc *possible_crtc;
4ef69c7a 8365 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8366 struct drm_crtc *crtc = NULL;
8367 struct drm_device *dev = encoder->dev;
94352cf9 8368 struct drm_framebuffer *fb;
51fd371b
RC
8369 struct drm_mode_config *config = &dev->mode_config;
8370 int ret, i = -1;
79e53945 8371
d2dff872 8372 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8373 connector->base.id, connector->name,
8e329a03 8374 encoder->base.id, encoder->name);
d2dff872 8375
51fd371b
RC
8376 drm_modeset_acquire_init(ctx, 0);
8377
8378retry:
8379 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8380 if (ret)
8381 goto fail_unlock;
6e9f798d 8382
79e53945
JB
8383 /*
8384 * Algorithm gets a little messy:
7a5e4805 8385 *
79e53945
JB
8386 * - if the connector already has an assigned crtc, use it (but make
8387 * sure it's on first)
7a5e4805 8388 *
79e53945
JB
8389 * - try to find the first unused crtc that can drive this connector,
8390 * and use that if we find one
79e53945
JB
8391 */
8392
8393 /* See if we already have a CRTC for this connector */
8394 if (encoder->crtc) {
8395 crtc = encoder->crtc;
8261b191 8396
51fd371b
RC
8397 ret = drm_modeset_lock(&crtc->mutex, ctx);
8398 if (ret)
8399 goto fail_unlock;
7b24056b 8400
24218aac 8401 old->dpms_mode = connector->dpms;
8261b191
CW
8402 old->load_detect_temp = false;
8403
8404 /* Make sure the crtc and connector are running */
24218aac
DV
8405 if (connector->dpms != DRM_MODE_DPMS_ON)
8406 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8407
7173188d 8408 return true;
79e53945
JB
8409 }
8410
8411 /* Find an unused one (if possible) */
70e1e0ec 8412 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8413 i++;
8414 if (!(encoder->possible_crtcs & (1 << i)))
8415 continue;
8416 if (!possible_crtc->enabled) {
8417 crtc = possible_crtc;
8418 break;
8419 }
79e53945
JB
8420 }
8421
8422 /*
8423 * If we didn't find an unused CRTC, don't use any.
8424 */
8425 if (!crtc) {
7173188d 8426 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8427 goto fail_unlock;
79e53945
JB
8428 }
8429
51fd371b
RC
8430 ret = drm_modeset_lock(&crtc->mutex, ctx);
8431 if (ret)
8432 goto fail_unlock;
fc303101
DV
8433 intel_encoder->new_crtc = to_intel_crtc(crtc);
8434 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8435
8436 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8437 intel_crtc->new_enabled = true;
8438 intel_crtc->new_config = &intel_crtc->config;
24218aac 8439 old->dpms_mode = connector->dpms;
8261b191 8440 old->load_detect_temp = true;
d2dff872 8441 old->release_fb = NULL;
79e53945 8442
6492711d
CW
8443 if (!mode)
8444 mode = &load_detect_mode;
79e53945 8445
d2dff872
CW
8446 /* We need a framebuffer large enough to accommodate all accesses
8447 * that the plane may generate whilst we perform load detection.
8448 * We can not rely on the fbcon either being present (we get called
8449 * during its initialisation to detect all boot displays, or it may
8450 * not even exist) or that it is large enough to satisfy the
8451 * requested mode.
8452 */
94352cf9
DV
8453 fb = mode_fits_in_fbdev(dev, mode);
8454 if (fb == NULL) {
d2dff872 8455 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8456 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8457 old->release_fb = fb;
d2dff872
CW
8458 } else
8459 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8460 if (IS_ERR(fb)) {
d2dff872 8461 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8462 goto fail;
79e53945 8463 }
79e53945 8464
c0c36b94 8465 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8466 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8467 if (old->release_fb)
8468 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8469 goto fail;
79e53945 8470 }
7173188d 8471
79e53945 8472 /* let the connector get through one full cycle before testing */
9d0498a2 8473 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8474 return true;
412b61d8
VS
8475
8476 fail:
8477 intel_crtc->new_enabled = crtc->enabled;
8478 if (intel_crtc->new_enabled)
8479 intel_crtc->new_config = &intel_crtc->config;
8480 else
8481 intel_crtc->new_config = NULL;
51fd371b
RC
8482fail_unlock:
8483 if (ret == -EDEADLK) {
8484 drm_modeset_backoff(ctx);
8485 goto retry;
8486 }
8487
8488 drm_modeset_drop_locks(ctx);
8489 drm_modeset_acquire_fini(ctx);
6e9f798d 8490
412b61d8 8491 return false;
79e53945
JB
8492}
8493
d2434ab7 8494void intel_release_load_detect_pipe(struct drm_connector *connector,
51fd371b
RC
8495 struct intel_load_detect_pipe *old,
8496 struct drm_modeset_acquire_ctx *ctx)
79e53945 8497{
d2434ab7
DV
8498 struct intel_encoder *intel_encoder =
8499 intel_attached_encoder(connector);
4ef69c7a 8500 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8501 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8503
d2dff872 8504 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8505 connector->base.id, connector->name,
8e329a03 8506 encoder->base.id, encoder->name);
d2dff872 8507
8261b191 8508 if (old->load_detect_temp) {
fc303101
DV
8509 to_intel_connector(connector)->new_encoder = NULL;
8510 intel_encoder->new_crtc = NULL;
412b61d8
VS
8511 intel_crtc->new_enabled = false;
8512 intel_crtc->new_config = NULL;
fc303101 8513 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8514
36206361
DV
8515 if (old->release_fb) {
8516 drm_framebuffer_unregister_private(old->release_fb);
8517 drm_framebuffer_unreference(old->release_fb);
8518 }
d2dff872 8519
51fd371b 8520 goto unlock;
0622a53c 8521 return;
79e53945
JB
8522 }
8523
c751ce4f 8524 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8525 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8526 connector->funcs->dpms(connector, old->dpms_mode);
7b24056b 8527
51fd371b
RC
8528unlock:
8529 drm_modeset_drop_locks(ctx);
8530 drm_modeset_acquire_fini(ctx);
79e53945
JB
8531}
8532
da4a1efa
VS
8533static int i9xx_pll_refclk(struct drm_device *dev,
8534 const struct intel_crtc_config *pipe_config)
8535{
8536 struct drm_i915_private *dev_priv = dev->dev_private;
8537 u32 dpll = pipe_config->dpll_hw_state.dpll;
8538
8539 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8540 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8541 else if (HAS_PCH_SPLIT(dev))
8542 return 120000;
8543 else if (!IS_GEN2(dev))
8544 return 96000;
8545 else
8546 return 48000;
8547}
8548
79e53945 8549/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8550static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8551 struct intel_crtc_config *pipe_config)
79e53945 8552{
f1f644dc 8553 struct drm_device *dev = crtc->base.dev;
79e53945 8554 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8555 int pipe = pipe_config->cpu_transcoder;
293623f7 8556 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8557 u32 fp;
8558 intel_clock_t clock;
da4a1efa 8559 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8560
8561 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8562 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8563 else
293623f7 8564 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8565
8566 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8567 if (IS_PINEVIEW(dev)) {
8568 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8569 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8570 } else {
8571 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8572 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8573 }
8574
a6c45cf0 8575 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8576 if (IS_PINEVIEW(dev))
8577 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8578 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8579 else
8580 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8581 DPLL_FPA01_P1_POST_DIV_SHIFT);
8582
8583 switch (dpll & DPLL_MODE_MASK) {
8584 case DPLLB_MODE_DAC_SERIAL:
8585 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8586 5 : 10;
8587 break;
8588 case DPLLB_MODE_LVDS:
8589 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8590 7 : 14;
8591 break;
8592 default:
28c97730 8593 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8594 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8595 return;
79e53945
JB
8596 }
8597
ac58c3f0 8598 if (IS_PINEVIEW(dev))
da4a1efa 8599 pineview_clock(refclk, &clock);
ac58c3f0 8600 else
da4a1efa 8601 i9xx_clock(refclk, &clock);
79e53945 8602 } else {
0fb58223 8603 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8604 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8605
8606 if (is_lvds) {
8607 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8608 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8609
8610 if (lvds & LVDS_CLKB_POWER_UP)
8611 clock.p2 = 7;
8612 else
8613 clock.p2 = 14;
79e53945
JB
8614 } else {
8615 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8616 clock.p1 = 2;
8617 else {
8618 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8619 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8620 }
8621 if (dpll & PLL_P2_DIVIDE_BY_4)
8622 clock.p2 = 4;
8623 else
8624 clock.p2 = 2;
79e53945 8625 }
da4a1efa
VS
8626
8627 i9xx_clock(refclk, &clock);
79e53945
JB
8628 }
8629
18442d08
VS
8630 /*
8631 * This value includes pixel_multiplier. We will use
241bfc38 8632 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8633 * encoder's get_config() function.
8634 */
8635 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8636}
8637
6878da05
VS
8638int intel_dotclock_calculate(int link_freq,
8639 const struct intel_link_m_n *m_n)
f1f644dc 8640{
f1f644dc
JB
8641 /*
8642 * The calculation for the data clock is:
1041a02f 8643 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8644 * But we want to avoid losing precison if possible, so:
1041a02f 8645 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8646 *
8647 * and the link clock is simpler:
1041a02f 8648 * link_clock = (m * link_clock) / n
f1f644dc
JB
8649 */
8650
6878da05
VS
8651 if (!m_n->link_n)
8652 return 0;
f1f644dc 8653
6878da05
VS
8654 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8655}
f1f644dc 8656
18442d08
VS
8657static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8658 struct intel_crtc_config *pipe_config)
6878da05
VS
8659{
8660 struct drm_device *dev = crtc->base.dev;
79e53945 8661
18442d08
VS
8662 /* read out port_clock from the DPLL */
8663 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8664
f1f644dc 8665 /*
18442d08 8666 * This value does not include pixel_multiplier.
241bfc38 8667 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8668 * agree once we know their relationship in the encoder's
8669 * get_config() function.
79e53945 8670 */
241bfc38 8671 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8672 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8673 &pipe_config->fdi_m_n);
79e53945
JB
8674}
8675
8676/** Returns the currently programmed mode of the given pipe. */
8677struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8678 struct drm_crtc *crtc)
8679{
548f245b 8680 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8682 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8683 struct drm_display_mode *mode;
f1f644dc 8684 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8685 int htot = I915_READ(HTOTAL(cpu_transcoder));
8686 int hsync = I915_READ(HSYNC(cpu_transcoder));
8687 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8688 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8689 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8690
8691 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8692 if (!mode)
8693 return NULL;
8694
f1f644dc
JB
8695 /*
8696 * Construct a pipe_config sufficient for getting the clock info
8697 * back out of crtc_clock_get.
8698 *
8699 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8700 * to use a real value here instead.
8701 */
293623f7 8702 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8703 pipe_config.pixel_multiplier = 1;
293623f7
VS
8704 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8705 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8706 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8707 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8708
773ae034 8709 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8710 mode->hdisplay = (htot & 0xffff) + 1;
8711 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8712 mode->hsync_start = (hsync & 0xffff) + 1;
8713 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8714 mode->vdisplay = (vtot & 0xffff) + 1;
8715 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8716 mode->vsync_start = (vsync & 0xffff) + 1;
8717 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8718
8719 drm_mode_set_name(mode);
79e53945
JB
8720
8721 return mode;
8722}
8723
3dec0095 8724static void intel_increase_pllclock(struct drm_crtc *crtc)
652c393a
JB
8725{
8726 struct drm_device *dev = crtc->dev;
fbee40df 8727 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a
JB
8728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8729 int pipe = intel_crtc->pipe;
dbdc6479
JB
8730 int dpll_reg = DPLL(pipe);
8731 int dpll;
652c393a 8732
bad720ff 8733 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8734 return;
8735
8736 if (!dev_priv->lvds_downclock_avail)
8737 return;
8738
dbdc6479 8739 dpll = I915_READ(dpll_reg);
652c393a 8740 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8741 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8742
8ac5a6d5 8743 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8744
8745 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8746 I915_WRITE(dpll_reg, dpll);
9d0498a2 8747 intel_wait_for_vblank(dev, pipe);
dbdc6479 8748
652c393a
JB
8749 dpll = I915_READ(dpll_reg);
8750 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8751 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8752 }
652c393a
JB
8753}
8754
8755static void intel_decrease_pllclock(struct drm_crtc *crtc)
8756{
8757 struct drm_device *dev = crtc->dev;
fbee40df 8758 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8760
bad720ff 8761 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8762 return;
8763
8764 if (!dev_priv->lvds_downclock_avail)
8765 return;
8766
8767 /*
8768 * Since this is called by a timer, we should never get here in
8769 * the manual case.
8770 */
8771 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8772 int pipe = intel_crtc->pipe;
8773 int dpll_reg = DPLL(pipe);
8774 int dpll;
f6e5b160 8775
44d98a61 8776 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8777
8ac5a6d5 8778 assert_panel_unlocked(dev_priv, pipe);
652c393a 8779
dc257cf1 8780 dpll = I915_READ(dpll_reg);
652c393a
JB
8781 dpll |= DISPLAY_RATE_SELECT_FPA1;
8782 I915_WRITE(dpll_reg, dpll);
9d0498a2 8783 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8784 dpll = I915_READ(dpll_reg);
8785 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8786 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8787 }
8788
8789}
8790
f047e395
CW
8791void intel_mark_busy(struct drm_device *dev)
8792{
c67a470b
PZ
8793 struct drm_i915_private *dev_priv = dev->dev_private;
8794
f62a0076
CW
8795 if (dev_priv->mm.busy)
8796 return;
8797
43694d69 8798 intel_runtime_pm_get(dev_priv);
c67a470b 8799 i915_update_gfx_val(dev_priv);
f62a0076 8800 dev_priv->mm.busy = true;
f047e395
CW
8801}
8802
8803void intel_mark_idle(struct drm_device *dev)
652c393a 8804{
c67a470b 8805 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8806 struct drm_crtc *crtc;
652c393a 8807
f62a0076
CW
8808 if (!dev_priv->mm.busy)
8809 return;
8810
8811 dev_priv->mm.busy = false;
8812
d330a953 8813 if (!i915.powersave)
bb4cdd53 8814 goto out;
652c393a 8815
70e1e0ec 8816 for_each_crtc(dev, crtc) {
f4510a27 8817 if (!crtc->primary->fb)
652c393a
JB
8818 continue;
8819
725a5b54 8820 intel_decrease_pllclock(crtc);
652c393a 8821 }
b29c19b6 8822
3d13ef2e 8823 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 8824 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
8825
8826out:
43694d69 8827 intel_runtime_pm_put(dev_priv);
652c393a
JB
8828}
8829
7c8f8a70 8830
c65355bb 8831void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
a4872ba6 8832 struct intel_engine_cs *ring)
652c393a 8833{
f047e395
CW
8834 struct drm_device *dev = obj->base.dev;
8835 struct drm_crtc *crtc;
652c393a 8836
3108e99e 8837 intel_edp_psr_exit(dev);
7c8f8a70 8838
d330a953 8839 if (!i915.powersave)
acb87dfb
CW
8840 return;
8841
70e1e0ec 8842 for_each_crtc(dev, crtc) {
f4510a27 8843 if (!crtc->primary->fb)
652c393a
JB
8844 continue;
8845
f4510a27 8846 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
c65355bb
CW
8847 continue;
8848
8849 intel_increase_pllclock(crtc);
8850 if (ring && intel_fbc_enabled(dev))
8851 ring->fbc_dirty = true;
652c393a
JB
8852 }
8853}
8854
79e53945
JB
8855static void intel_crtc_destroy(struct drm_crtc *crtc)
8856{
8857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
8858 struct drm_device *dev = crtc->dev;
8859 struct intel_unpin_work *work;
8860 unsigned long flags;
8861
8862 spin_lock_irqsave(&dev->event_lock, flags);
8863 work = intel_crtc->unpin_work;
8864 intel_crtc->unpin_work = NULL;
8865 spin_unlock_irqrestore(&dev->event_lock, flags);
8866
8867 if (work) {
8868 cancel_work_sync(&work->work);
8869 kfree(work);
8870 }
79e53945
JB
8871
8872 drm_crtc_cleanup(crtc);
67e77c5a 8873
79e53945
JB
8874 kfree(intel_crtc);
8875}
8876
6b95a207
KH
8877static void intel_unpin_work_fn(struct work_struct *__work)
8878{
8879 struct intel_unpin_work *work =
8880 container_of(__work, struct intel_unpin_work, work);
b4a98e57 8881 struct drm_device *dev = work->crtc->dev;
6b95a207 8882
b4a98e57 8883 mutex_lock(&dev->struct_mutex);
1690e1eb 8884 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
8885 drm_gem_object_unreference(&work->pending_flip_obj->base);
8886 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 8887
b4a98e57
CW
8888 intel_update_fbc(dev);
8889 mutex_unlock(&dev->struct_mutex);
8890
8891 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8892 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8893
6b95a207
KH
8894 kfree(work);
8895}
8896
1afe3e9d 8897static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 8898 struct drm_crtc *crtc)
6b95a207 8899{
fbee40df 8900 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
8901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8902 struct intel_unpin_work *work;
6b95a207
KH
8903 unsigned long flags;
8904
8905 /* Ignore early vblank irqs */
8906 if (intel_crtc == NULL)
8907 return;
8908
8909 spin_lock_irqsave(&dev->event_lock, flags);
8910 work = intel_crtc->unpin_work;
e7d841ca
CW
8911
8912 /* Ensure we don't miss a work->pending update ... */
8913 smp_rmb();
8914
8915 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
8916 spin_unlock_irqrestore(&dev->event_lock, flags);
8917 return;
8918 }
8919
e7d841ca
CW
8920 /* and that the unpin work is consistent wrt ->pending. */
8921 smp_rmb();
8922
6b95a207 8923 intel_crtc->unpin_work = NULL;
6b95a207 8924
45a066eb
RC
8925 if (work->event)
8926 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 8927
87b6b101 8928 drm_crtc_vblank_put(crtc);
0af7e4df 8929
6b95a207
KH
8930 spin_unlock_irqrestore(&dev->event_lock, flags);
8931
2c10d571 8932 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
8933
8934 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
8935
8936 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
8937}
8938
1afe3e9d
JB
8939void intel_finish_page_flip(struct drm_device *dev, int pipe)
8940{
fbee40df 8941 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8942 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8943
49b14a5c 8944 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8945}
8946
8947void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8948{
fbee40df 8949 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8950 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8951
49b14a5c 8952 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8953}
8954
75f7f3ec
VS
8955/* Is 'a' after or equal to 'b'? */
8956static bool g4x_flip_count_after_eq(u32 a, u32 b)
8957{
8958 return !((a - b) & 0x80000000);
8959}
8960
8961static bool page_flip_finished(struct intel_crtc *crtc)
8962{
8963 struct drm_device *dev = crtc->base.dev;
8964 struct drm_i915_private *dev_priv = dev->dev_private;
8965
8966 /*
8967 * The relevant registers doen't exist on pre-ctg.
8968 * As the flip done interrupt doesn't trigger for mmio
8969 * flips on gmch platforms, a flip count check isn't
8970 * really needed there. But since ctg has the registers,
8971 * include it in the check anyway.
8972 */
8973 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8974 return true;
8975
8976 /*
8977 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8978 * used the same base address. In that case the mmio flip might
8979 * have completed, but the CS hasn't even executed the flip yet.
8980 *
8981 * A flip count check isn't enough as the CS might have updated
8982 * the base address just after start of vblank, but before we
8983 * managed to process the interrupt. This means we'd complete the
8984 * CS flip too soon.
8985 *
8986 * Combining both checks should get us a good enough result. It may
8987 * still happen that the CS flip has been executed, but has not
8988 * yet actually completed. But in case the base address is the same
8989 * anyway, we don't really care.
8990 */
8991 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8992 crtc->unpin_work->gtt_offset &&
8993 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8994 crtc->unpin_work->flip_count);
8995}
8996
6b95a207
KH
8997void intel_prepare_page_flip(struct drm_device *dev, int plane)
8998{
fbee40df 8999 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9000 struct intel_crtc *intel_crtc =
9001 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9002 unsigned long flags;
9003
e7d841ca
CW
9004 /* NB: An MMIO update of the plane base pointer will also
9005 * generate a page-flip completion irq, i.e. every modeset
9006 * is also accompanied by a spurious intel_prepare_page_flip().
9007 */
6b95a207 9008 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9009 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9010 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9011 spin_unlock_irqrestore(&dev->event_lock, flags);
9012}
9013
eba905b2 9014static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9015{
9016 /* Ensure that the work item is consistent when activating it ... */
9017 smp_wmb();
9018 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9019 /* and that it is marked active as soon as the irq could fire. */
9020 smp_wmb();
9021}
9022
8c9f3aaf
JB
9023static int intel_gen2_queue_flip(struct drm_device *dev,
9024 struct drm_crtc *crtc,
9025 struct drm_framebuffer *fb,
ed8d1975 9026 struct drm_i915_gem_object *obj,
a4872ba6 9027 struct intel_engine_cs *ring,
ed8d1975 9028 uint32_t flags)
8c9f3aaf 9029{
8c9f3aaf 9030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9031 u32 flip_mask;
9032 int ret;
9033
6d90c952 9034 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9035 if (ret)
4fa62c89 9036 return ret;
8c9f3aaf
JB
9037
9038 /* Can't queue multiple flips, so wait for the previous
9039 * one to finish before executing the next.
9040 */
9041 if (intel_crtc->plane)
9042 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9043 else
9044 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9045 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9046 intel_ring_emit(ring, MI_NOOP);
9047 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9048 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9049 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9050 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9051 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9052
9053 intel_mark_page_flip_active(intel_crtc);
09246732 9054 __intel_ring_advance(ring);
83d4092b 9055 return 0;
8c9f3aaf
JB
9056}
9057
9058static int intel_gen3_queue_flip(struct drm_device *dev,
9059 struct drm_crtc *crtc,
9060 struct drm_framebuffer *fb,
ed8d1975 9061 struct drm_i915_gem_object *obj,
a4872ba6 9062 struct intel_engine_cs *ring,
ed8d1975 9063 uint32_t flags)
8c9f3aaf 9064{
8c9f3aaf 9065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9066 u32 flip_mask;
9067 int ret;
9068
6d90c952 9069 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9070 if (ret)
4fa62c89 9071 return ret;
8c9f3aaf
JB
9072
9073 if (intel_crtc->plane)
9074 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9075 else
9076 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9077 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9078 intel_ring_emit(ring, MI_NOOP);
9079 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9080 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9081 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9082 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9083 intel_ring_emit(ring, MI_NOOP);
9084
e7d841ca 9085 intel_mark_page_flip_active(intel_crtc);
09246732 9086 __intel_ring_advance(ring);
83d4092b 9087 return 0;
8c9f3aaf
JB
9088}
9089
9090static int intel_gen4_queue_flip(struct drm_device *dev,
9091 struct drm_crtc *crtc,
9092 struct drm_framebuffer *fb,
ed8d1975 9093 struct drm_i915_gem_object *obj,
a4872ba6 9094 struct intel_engine_cs *ring,
ed8d1975 9095 uint32_t flags)
8c9f3aaf
JB
9096{
9097 struct drm_i915_private *dev_priv = dev->dev_private;
9098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9099 uint32_t pf, pipesrc;
9100 int ret;
9101
6d90c952 9102 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9103 if (ret)
4fa62c89 9104 return ret;
8c9f3aaf
JB
9105
9106 /* i965+ uses the linear or tiled offsets from the
9107 * Display Registers (which do not change across a page-flip)
9108 * so we need only reprogram the base address.
9109 */
6d90c952
DV
9110 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9111 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9112 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9113 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9114 obj->tiling_mode);
8c9f3aaf
JB
9115
9116 /* XXX Enabling the panel-fitter across page-flip is so far
9117 * untested on non-native modes, so ignore it for now.
9118 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9119 */
9120 pf = 0;
9121 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9122 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9123
9124 intel_mark_page_flip_active(intel_crtc);
09246732 9125 __intel_ring_advance(ring);
83d4092b 9126 return 0;
8c9f3aaf
JB
9127}
9128
9129static int intel_gen6_queue_flip(struct drm_device *dev,
9130 struct drm_crtc *crtc,
9131 struct drm_framebuffer *fb,
ed8d1975 9132 struct drm_i915_gem_object *obj,
a4872ba6 9133 struct intel_engine_cs *ring,
ed8d1975 9134 uint32_t flags)
8c9f3aaf
JB
9135{
9136 struct drm_i915_private *dev_priv = dev->dev_private;
9137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9138 uint32_t pf, pipesrc;
9139 int ret;
9140
6d90c952 9141 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9142 if (ret)
4fa62c89 9143 return ret;
8c9f3aaf 9144
6d90c952
DV
9145 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9146 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9147 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9148 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9149
dc257cf1
DV
9150 /* Contrary to the suggestions in the documentation,
9151 * "Enable Panel Fitter" does not seem to be required when page
9152 * flipping with a non-native mode, and worse causes a normal
9153 * modeset to fail.
9154 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9155 */
9156 pf = 0;
8c9f3aaf 9157 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9158 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9159
9160 intel_mark_page_flip_active(intel_crtc);
09246732 9161 __intel_ring_advance(ring);
83d4092b 9162 return 0;
8c9f3aaf
JB
9163}
9164
7c9017e5
JB
9165static int intel_gen7_queue_flip(struct drm_device *dev,
9166 struct drm_crtc *crtc,
9167 struct drm_framebuffer *fb,
ed8d1975 9168 struct drm_i915_gem_object *obj,
a4872ba6 9169 struct intel_engine_cs *ring,
ed8d1975 9170 uint32_t flags)
7c9017e5 9171{
7c9017e5 9172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9173 uint32_t plane_bit = 0;
ffe74d75
CW
9174 int len, ret;
9175
eba905b2 9176 switch (intel_crtc->plane) {
cb05d8de
DV
9177 case PLANE_A:
9178 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9179 break;
9180 case PLANE_B:
9181 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9182 break;
9183 case PLANE_C:
9184 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9185 break;
9186 default:
9187 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9188 return -ENODEV;
cb05d8de
DV
9189 }
9190
ffe74d75 9191 len = 4;
f476828a 9192 if (ring->id == RCS) {
ffe74d75 9193 len += 6;
f476828a
DL
9194 /*
9195 * On Gen 8, SRM is now taking an extra dword to accommodate
9196 * 48bits addresses, and we need a NOOP for the batch size to
9197 * stay even.
9198 */
9199 if (IS_GEN8(dev))
9200 len += 2;
9201 }
ffe74d75 9202
f66fab8e
VS
9203 /*
9204 * BSpec MI_DISPLAY_FLIP for IVB:
9205 * "The full packet must be contained within the same cache line."
9206 *
9207 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9208 * cacheline, if we ever start emitting more commands before
9209 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9210 * then do the cacheline alignment, and finally emit the
9211 * MI_DISPLAY_FLIP.
9212 */
9213 ret = intel_ring_cacheline_align(ring);
9214 if (ret)
4fa62c89 9215 return ret;
f66fab8e 9216
ffe74d75 9217 ret = intel_ring_begin(ring, len);
7c9017e5 9218 if (ret)
4fa62c89 9219 return ret;
7c9017e5 9220
ffe74d75
CW
9221 /* Unmask the flip-done completion message. Note that the bspec says that
9222 * we should do this for both the BCS and RCS, and that we must not unmask
9223 * more than one flip event at any time (or ensure that one flip message
9224 * can be sent by waiting for flip-done prior to queueing new flips).
9225 * Experimentation says that BCS works despite DERRMR masking all
9226 * flip-done completion events and that unmasking all planes at once
9227 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9228 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9229 */
9230 if (ring->id == RCS) {
9231 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9232 intel_ring_emit(ring, DERRMR);
9233 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9234 DERRMR_PIPEB_PRI_FLIP_DONE |
9235 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9236 if (IS_GEN8(dev))
9237 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9238 MI_SRM_LRM_GLOBAL_GTT);
9239 else
9240 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9241 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9242 intel_ring_emit(ring, DERRMR);
9243 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9244 if (IS_GEN8(dev)) {
9245 intel_ring_emit(ring, 0);
9246 intel_ring_emit(ring, MI_NOOP);
9247 }
ffe74d75
CW
9248 }
9249
cb05d8de 9250 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9251 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9252 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9253 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9254
9255 intel_mark_page_flip_active(intel_crtc);
09246732 9256 __intel_ring_advance(ring);
83d4092b 9257 return 0;
7c9017e5
JB
9258}
9259
84c33a64
SG
9260static bool use_mmio_flip(struct intel_engine_cs *ring,
9261 struct drm_i915_gem_object *obj)
9262{
9263 /*
9264 * This is not being used for older platforms, because
9265 * non-availability of flip done interrupt forces us to use
9266 * CS flips. Older platforms derive flip done using some clever
9267 * tricks involving the flip_pending status bits and vblank irqs.
9268 * So using MMIO flips there would disrupt this mechanism.
9269 */
9270
9271 if (INTEL_INFO(ring->dev)->gen < 5)
9272 return false;
9273
9274 if (i915.use_mmio_flip < 0)
9275 return false;
9276 else if (i915.use_mmio_flip > 0)
9277 return true;
9278 else
9279 return ring != obj->ring;
9280}
9281
9282static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9283{
9284 struct drm_device *dev = intel_crtc->base.dev;
9285 struct drm_i915_private *dev_priv = dev->dev_private;
9286 struct intel_framebuffer *intel_fb =
9287 to_intel_framebuffer(intel_crtc->base.primary->fb);
9288 struct drm_i915_gem_object *obj = intel_fb->obj;
9289 u32 dspcntr;
9290 u32 reg;
9291
9292 intel_mark_page_flip_active(intel_crtc);
9293
9294 reg = DSPCNTR(intel_crtc->plane);
9295 dspcntr = I915_READ(reg);
9296
9297 if (INTEL_INFO(dev)->gen >= 4) {
9298 if (obj->tiling_mode != I915_TILING_NONE)
9299 dspcntr |= DISPPLANE_TILED;
9300 else
9301 dspcntr &= ~DISPPLANE_TILED;
9302 }
9303 I915_WRITE(reg, dspcntr);
9304
9305 I915_WRITE(DSPSURF(intel_crtc->plane),
9306 intel_crtc->unpin_work->gtt_offset);
9307 POSTING_READ(DSPSURF(intel_crtc->plane));
9308}
9309
9310static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9311{
9312 struct intel_engine_cs *ring;
9313 int ret;
9314
9315 lockdep_assert_held(&obj->base.dev->struct_mutex);
9316
9317 if (!obj->last_write_seqno)
9318 return 0;
9319
9320 ring = obj->ring;
9321
9322 if (i915_seqno_passed(ring->get_seqno(ring, true),
9323 obj->last_write_seqno))
9324 return 0;
9325
9326 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9327 if (ret)
9328 return ret;
9329
9330 if (WARN_ON(!ring->irq_get(ring)))
9331 return 0;
9332
9333 return 1;
9334}
9335
9336void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9337{
9338 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9339 struct intel_crtc *intel_crtc;
9340 unsigned long irq_flags;
9341 u32 seqno;
9342
9343 seqno = ring->get_seqno(ring, false);
9344
9345 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9346 for_each_intel_crtc(ring->dev, intel_crtc) {
9347 struct intel_mmio_flip *mmio_flip;
9348
9349 mmio_flip = &intel_crtc->mmio_flip;
9350 if (mmio_flip->seqno == 0)
9351 continue;
9352
9353 if (ring->id != mmio_flip->ring_id)
9354 continue;
9355
9356 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9357 intel_do_mmio_flip(intel_crtc);
9358 mmio_flip->seqno = 0;
9359 ring->irq_put(ring);
9360 }
9361 }
9362 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9363}
9364
9365static int intel_queue_mmio_flip(struct drm_device *dev,
9366 struct drm_crtc *crtc,
9367 struct drm_framebuffer *fb,
9368 struct drm_i915_gem_object *obj,
9369 struct intel_engine_cs *ring,
9370 uint32_t flags)
9371{
9372 struct drm_i915_private *dev_priv = dev->dev_private;
9373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9374 unsigned long irq_flags;
9375 int ret;
9376
9377 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9378 return -EBUSY;
9379
9380 ret = intel_postpone_flip(obj);
9381 if (ret < 0)
9382 return ret;
9383 if (ret == 0) {
9384 intel_do_mmio_flip(intel_crtc);
9385 return 0;
9386 }
9387
9388 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9389 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9390 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9391 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9392
9393 /*
9394 * Double check to catch cases where irq fired before
9395 * mmio flip data was ready
9396 */
9397 intel_notify_mmio_flip(obj->ring);
9398 return 0;
9399}
9400
8c9f3aaf
JB
9401static int intel_default_queue_flip(struct drm_device *dev,
9402 struct drm_crtc *crtc,
9403 struct drm_framebuffer *fb,
ed8d1975 9404 struct drm_i915_gem_object *obj,
a4872ba6 9405 struct intel_engine_cs *ring,
ed8d1975 9406 uint32_t flags)
8c9f3aaf
JB
9407{
9408 return -ENODEV;
9409}
9410
6b95a207
KH
9411static int intel_crtc_page_flip(struct drm_crtc *crtc,
9412 struct drm_framebuffer *fb,
ed8d1975
KP
9413 struct drm_pending_vblank_event *event,
9414 uint32_t page_flip_flags)
6b95a207
KH
9415{
9416 struct drm_device *dev = crtc->dev;
9417 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9418 struct drm_framebuffer *old_fb = crtc->primary->fb;
4a35f83b 9419 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
6b95a207 9420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9421 enum pipe pipe = intel_crtc->pipe;
6b95a207 9422 struct intel_unpin_work *work;
a4872ba6 9423 struct intel_engine_cs *ring;
8c9f3aaf 9424 unsigned long flags;
52e68630 9425 int ret;
6b95a207 9426
e6a595d2 9427 /* Can't change pixel format via MI display flips. */
f4510a27 9428 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9429 return -EINVAL;
9430
9431 /*
9432 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9433 * Note that pitch changes could also affect these register.
9434 */
9435 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9436 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9437 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9438 return -EINVAL;
9439
f900db47
CW
9440 if (i915_terminally_wedged(&dev_priv->gpu_error))
9441 goto out_hang;
9442
b14c5679 9443 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9444 if (work == NULL)
9445 return -ENOMEM;
9446
7c8f8a70 9447 /* Exit PSR early in page flip */
3108e99e 9448 intel_edp_psr_exit(dev);
7c8f8a70 9449
6b95a207 9450 work->event = event;
b4a98e57 9451 work->crtc = crtc;
4a35f83b 9452 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
6b95a207
KH
9453 INIT_WORK(&work->work, intel_unpin_work_fn);
9454
87b6b101 9455 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9456 if (ret)
9457 goto free_work;
9458
6b95a207
KH
9459 /* We borrow the event spin lock for protecting unpin_work */
9460 spin_lock_irqsave(&dev->event_lock, flags);
9461 if (intel_crtc->unpin_work) {
9462 spin_unlock_irqrestore(&dev->event_lock, flags);
9463 kfree(work);
87b6b101 9464 drm_crtc_vblank_put(crtc);
468f0b44
CW
9465
9466 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
9467 return -EBUSY;
9468 }
9469 intel_crtc->unpin_work = work;
9470 spin_unlock_irqrestore(&dev->event_lock, flags);
9471
b4a98e57
CW
9472 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9473 flush_workqueue(dev_priv->wq);
9474
79158103
CW
9475 ret = i915_mutex_lock_interruptible(dev);
9476 if (ret)
9477 goto cleanup;
6b95a207 9478
75dfca80 9479 /* Reference the objects for the scheduled work. */
05394f39
CW
9480 drm_gem_object_reference(&work->old_fb_obj->base);
9481 drm_gem_object_reference(&obj->base);
6b95a207 9482
f4510a27 9483 crtc->primary->fb = fb;
96b099fd 9484
e1f99ce6 9485 work->pending_flip_obj = obj;
e1f99ce6 9486
4e5359cd
SF
9487 work->enable_stall_check = true;
9488
b4a98e57 9489 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9490 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9491
75f7f3ec 9492 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9493 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9494
4fa62c89
VS
9495 if (IS_VALLEYVIEW(dev)) {
9496 ring = &dev_priv->ring[BCS];
9497 } else if (INTEL_INFO(dev)->gen >= 7) {
9498 ring = obj->ring;
9499 if (ring == NULL || ring->id != RCS)
9500 ring = &dev_priv->ring[BCS];
9501 } else {
9502 ring = &dev_priv->ring[RCS];
9503 }
9504
9505 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9506 if (ret)
9507 goto cleanup_pending;
6b95a207 9508
4fa62c89
VS
9509 work->gtt_offset =
9510 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9511
84c33a64
SG
9512 if (use_mmio_flip(ring, obj))
9513 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9514 page_flip_flags);
9515 else
9516 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
9517 page_flip_flags);
4fa62c89
VS
9518 if (ret)
9519 goto cleanup_unpin;
9520
a071fa00
DV
9521 i915_gem_track_fb(work->old_fb_obj, obj,
9522 INTEL_FRONTBUFFER_PRIMARY(pipe));
9523
7782de3b 9524 intel_disable_fbc(dev);
c65355bb 9525 intel_mark_fb_busy(obj, NULL);
6b95a207
KH
9526 mutex_unlock(&dev->struct_mutex);
9527
e5510fac
JB
9528 trace_i915_flip_request(intel_crtc->plane, obj);
9529
6b95a207 9530 return 0;
96b099fd 9531
4fa62c89
VS
9532cleanup_unpin:
9533 intel_unpin_fb_obj(obj);
8c9f3aaf 9534cleanup_pending:
b4a98e57 9535 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9536 crtc->primary->fb = old_fb;
05394f39
CW
9537 drm_gem_object_unreference(&work->old_fb_obj->base);
9538 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9539 mutex_unlock(&dev->struct_mutex);
9540
79158103 9541cleanup:
96b099fd
CW
9542 spin_lock_irqsave(&dev->event_lock, flags);
9543 intel_crtc->unpin_work = NULL;
9544 spin_unlock_irqrestore(&dev->event_lock, flags);
9545
87b6b101 9546 drm_crtc_vblank_put(crtc);
7317c75e 9547free_work:
96b099fd
CW
9548 kfree(work);
9549
f900db47
CW
9550 if (ret == -EIO) {
9551out_hang:
9552 intel_crtc_wait_for_pending_flips(crtc);
9553 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9554 if (ret == 0 && event)
a071fa00 9555 drm_send_vblank_event(dev, pipe, event);
f900db47 9556 }
96b099fd 9557 return ret;
6b95a207
KH
9558}
9559
f6e5b160 9560static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9561 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9562 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9563};
9564
9a935856
DV
9565/**
9566 * intel_modeset_update_staged_output_state
9567 *
9568 * Updates the staged output configuration state, e.g. after we've read out the
9569 * current hw state.
9570 */
9571static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9572{
7668851f 9573 struct intel_crtc *crtc;
9a935856
DV
9574 struct intel_encoder *encoder;
9575 struct intel_connector *connector;
f6e5b160 9576
9a935856
DV
9577 list_for_each_entry(connector, &dev->mode_config.connector_list,
9578 base.head) {
9579 connector->new_encoder =
9580 to_intel_encoder(connector->base.encoder);
9581 }
f6e5b160 9582
9a935856
DV
9583 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9584 base.head) {
9585 encoder->new_crtc =
9586 to_intel_crtc(encoder->base.crtc);
9587 }
7668851f 9588
d3fcc808 9589 for_each_intel_crtc(dev, crtc) {
7668851f 9590 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9591
9592 if (crtc->new_enabled)
9593 crtc->new_config = &crtc->config;
9594 else
9595 crtc->new_config = NULL;
7668851f 9596 }
f6e5b160
CW
9597}
9598
9a935856
DV
9599/**
9600 * intel_modeset_commit_output_state
9601 *
9602 * This function copies the stage display pipe configuration to the real one.
9603 */
9604static void intel_modeset_commit_output_state(struct drm_device *dev)
9605{
7668851f 9606 struct intel_crtc *crtc;
9a935856
DV
9607 struct intel_encoder *encoder;
9608 struct intel_connector *connector;
f6e5b160 9609
9a935856
DV
9610 list_for_each_entry(connector, &dev->mode_config.connector_list,
9611 base.head) {
9612 connector->base.encoder = &connector->new_encoder->base;
9613 }
f6e5b160 9614
9a935856
DV
9615 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9616 base.head) {
9617 encoder->base.crtc = &encoder->new_crtc->base;
9618 }
7668851f 9619
d3fcc808 9620 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9621 crtc->base.enabled = crtc->new_enabled;
9622 }
9a935856
DV
9623}
9624
050f7aeb 9625static void
eba905b2 9626connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
9627 struct intel_crtc_config *pipe_config)
9628{
9629 int bpp = pipe_config->pipe_bpp;
9630
9631 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9632 connector->base.base.id,
c23cc417 9633 connector->base.name);
050f7aeb
DV
9634
9635 /* Don't use an invalid EDID bpc value */
9636 if (connector->base.display_info.bpc &&
9637 connector->base.display_info.bpc * 3 < bpp) {
9638 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9639 bpp, connector->base.display_info.bpc*3);
9640 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9641 }
9642
9643 /* Clamp bpp to 8 on screens without EDID 1.4 */
9644 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9645 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9646 bpp);
9647 pipe_config->pipe_bpp = 24;
9648 }
9649}
9650
4e53c2e0 9651static int
050f7aeb
DV
9652compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9653 struct drm_framebuffer *fb,
9654 struct intel_crtc_config *pipe_config)
4e53c2e0 9655{
050f7aeb
DV
9656 struct drm_device *dev = crtc->base.dev;
9657 struct intel_connector *connector;
4e53c2e0
DV
9658 int bpp;
9659
d42264b1
DV
9660 switch (fb->pixel_format) {
9661 case DRM_FORMAT_C8:
4e53c2e0
DV
9662 bpp = 8*3; /* since we go through a colormap */
9663 break;
d42264b1
DV
9664 case DRM_FORMAT_XRGB1555:
9665 case DRM_FORMAT_ARGB1555:
9666 /* checked in intel_framebuffer_init already */
9667 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9668 return -EINVAL;
9669 case DRM_FORMAT_RGB565:
4e53c2e0
DV
9670 bpp = 6*3; /* min is 18bpp */
9671 break;
d42264b1
DV
9672 case DRM_FORMAT_XBGR8888:
9673 case DRM_FORMAT_ABGR8888:
9674 /* checked in intel_framebuffer_init already */
9675 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9676 return -EINVAL;
9677 case DRM_FORMAT_XRGB8888:
9678 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
9679 bpp = 8*3;
9680 break;
d42264b1
DV
9681 case DRM_FORMAT_XRGB2101010:
9682 case DRM_FORMAT_ARGB2101010:
9683 case DRM_FORMAT_XBGR2101010:
9684 case DRM_FORMAT_ABGR2101010:
9685 /* checked in intel_framebuffer_init already */
9686 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 9687 return -EINVAL;
4e53c2e0
DV
9688 bpp = 10*3;
9689 break;
baba133a 9690 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
9691 default:
9692 DRM_DEBUG_KMS("unsupported depth\n");
9693 return -EINVAL;
9694 }
9695
4e53c2e0
DV
9696 pipe_config->pipe_bpp = bpp;
9697
9698 /* Clamp display bpp to EDID value */
9699 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 9700 base.head) {
1b829e05
DV
9701 if (!connector->new_encoder ||
9702 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
9703 continue;
9704
050f7aeb 9705 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
9706 }
9707
9708 return bpp;
9709}
9710
644db711
DV
9711static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9712{
9713 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9714 "type: 0x%x flags: 0x%x\n",
1342830c 9715 mode->crtc_clock,
644db711
DV
9716 mode->crtc_hdisplay, mode->crtc_hsync_start,
9717 mode->crtc_hsync_end, mode->crtc_htotal,
9718 mode->crtc_vdisplay, mode->crtc_vsync_start,
9719 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9720}
9721
c0b03411
DV
9722static void intel_dump_pipe_config(struct intel_crtc *crtc,
9723 struct intel_crtc_config *pipe_config,
9724 const char *context)
9725{
9726 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9727 context, pipe_name(crtc->pipe));
9728
9729 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9730 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9731 pipe_config->pipe_bpp, pipe_config->dither);
9732 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9733 pipe_config->has_pch_encoder,
9734 pipe_config->fdi_lanes,
9735 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9736 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9737 pipe_config->fdi_m_n.tu);
eb14cb74
VS
9738 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9739 pipe_config->has_dp_encoder,
9740 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9741 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9742 pipe_config->dp_m_n.tu);
c0b03411
DV
9743 DRM_DEBUG_KMS("requested mode:\n");
9744 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9745 DRM_DEBUG_KMS("adjusted mode:\n");
9746 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 9747 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 9748 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
9749 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9750 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
9751 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9752 pipe_config->gmch_pfit.control,
9753 pipe_config->gmch_pfit.pgm_ratios,
9754 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 9755 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 9756 pipe_config->pch_pfit.pos,
fd4daa9c
CW
9757 pipe_config->pch_pfit.size,
9758 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 9759 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 9760 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
9761}
9762
bc079e8b
VS
9763static bool encoders_cloneable(const struct intel_encoder *a,
9764 const struct intel_encoder *b)
accfc0c5 9765{
bc079e8b
VS
9766 /* masks could be asymmetric, so check both ways */
9767 return a == b || (a->cloneable & (1 << b->type) &&
9768 b->cloneable & (1 << a->type));
9769}
9770
9771static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9772 struct intel_encoder *encoder)
9773{
9774 struct drm_device *dev = crtc->base.dev;
9775 struct intel_encoder *source_encoder;
9776
9777 list_for_each_entry(source_encoder,
9778 &dev->mode_config.encoder_list, base.head) {
9779 if (source_encoder->new_crtc != crtc)
9780 continue;
9781
9782 if (!encoders_cloneable(encoder, source_encoder))
9783 return false;
9784 }
9785
9786 return true;
9787}
9788
9789static bool check_encoder_cloning(struct intel_crtc *crtc)
9790{
9791 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
9792 struct intel_encoder *encoder;
9793
bc079e8b
VS
9794 list_for_each_entry(encoder,
9795 &dev->mode_config.encoder_list, base.head) {
9796 if (encoder->new_crtc != crtc)
accfc0c5
DV
9797 continue;
9798
bc079e8b
VS
9799 if (!check_single_encoder_cloning(crtc, encoder))
9800 return false;
accfc0c5
DV
9801 }
9802
bc079e8b 9803 return true;
accfc0c5
DV
9804}
9805
b8cecdf5
DV
9806static struct intel_crtc_config *
9807intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 9808 struct drm_framebuffer *fb,
b8cecdf5 9809 struct drm_display_mode *mode)
ee7b9f93 9810{
7758a113 9811 struct drm_device *dev = crtc->dev;
7758a113 9812 struct intel_encoder *encoder;
b8cecdf5 9813 struct intel_crtc_config *pipe_config;
e29c22c0
DV
9814 int plane_bpp, ret = -EINVAL;
9815 bool retry = true;
ee7b9f93 9816
bc079e8b 9817 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
9818 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9819 return ERR_PTR(-EINVAL);
9820 }
9821
b8cecdf5
DV
9822 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9823 if (!pipe_config)
7758a113
DV
9824 return ERR_PTR(-ENOMEM);
9825
b8cecdf5
DV
9826 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9827 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 9828
e143a21c
DV
9829 pipe_config->cpu_transcoder =
9830 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 9831 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 9832
2960bc9c
ID
9833 /*
9834 * Sanitize sync polarity flags based on requested ones. If neither
9835 * positive or negative polarity is requested, treat this as meaning
9836 * negative polarity.
9837 */
9838 if (!(pipe_config->adjusted_mode.flags &
9839 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9840 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9841
9842 if (!(pipe_config->adjusted_mode.flags &
9843 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9844 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9845
050f7aeb
DV
9846 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9847 * plane pixel format and any sink constraints into account. Returns the
9848 * source plane bpp so that dithering can be selected on mismatches
9849 * after encoders and crtc also have had their say. */
9850 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9851 fb, pipe_config);
4e53c2e0
DV
9852 if (plane_bpp < 0)
9853 goto fail;
9854
e41a56be
VS
9855 /*
9856 * Determine the real pipe dimensions. Note that stereo modes can
9857 * increase the actual pipe size due to the frame doubling and
9858 * insertion of additional space for blanks between the frame. This
9859 * is stored in the crtc timings. We use the requested mode to do this
9860 * computation to clearly distinguish it from the adjusted mode, which
9861 * can be changed by the connectors in the below retry loop.
9862 */
9863 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9864 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9865 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9866
e29c22c0 9867encoder_retry:
ef1b460d 9868 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 9869 pipe_config->port_clock = 0;
ef1b460d 9870 pipe_config->pixel_multiplier = 1;
ff9a6750 9871
135c81b8 9872 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 9873 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 9874
7758a113
DV
9875 /* Pass our mode to the connectors and the CRTC to give them a chance to
9876 * adjust it according to limitations or connector properties, and also
9877 * a chance to reject the mode entirely.
47f1c6c9 9878 */
7758a113
DV
9879 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9880 base.head) {
47f1c6c9 9881
7758a113
DV
9882 if (&encoder->new_crtc->base != crtc)
9883 continue;
7ae89233 9884
efea6e8e
DV
9885 if (!(encoder->compute_config(encoder, pipe_config))) {
9886 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
9887 goto fail;
9888 }
ee7b9f93 9889 }
47f1c6c9 9890
ff9a6750
DV
9891 /* Set default port clock if not overwritten by the encoder. Needs to be
9892 * done afterwards in case the encoder adjusts the mode. */
9893 if (!pipe_config->port_clock)
241bfc38
DL
9894 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9895 * pipe_config->pixel_multiplier;
ff9a6750 9896
a43f6e0f 9897 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 9898 if (ret < 0) {
7758a113
DV
9899 DRM_DEBUG_KMS("CRTC fixup failed\n");
9900 goto fail;
ee7b9f93 9901 }
e29c22c0
DV
9902
9903 if (ret == RETRY) {
9904 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9905 ret = -EINVAL;
9906 goto fail;
9907 }
9908
9909 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9910 retry = false;
9911 goto encoder_retry;
9912 }
9913
4e53c2e0
DV
9914 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9915 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9916 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9917
b8cecdf5 9918 return pipe_config;
7758a113 9919fail:
b8cecdf5 9920 kfree(pipe_config);
e29c22c0 9921 return ERR_PTR(ret);
ee7b9f93 9922}
47f1c6c9 9923
e2e1ed41
DV
9924/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9925 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9926static void
9927intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9928 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
9929{
9930 struct intel_crtc *intel_crtc;
e2e1ed41
DV
9931 struct drm_device *dev = crtc->dev;
9932 struct intel_encoder *encoder;
9933 struct intel_connector *connector;
9934 struct drm_crtc *tmp_crtc;
79e53945 9935
e2e1ed41 9936 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 9937
e2e1ed41
DV
9938 /* Check which crtcs have changed outputs connected to them, these need
9939 * to be part of the prepare_pipes mask. We don't (yet) support global
9940 * modeset across multiple crtcs, so modeset_pipes will only have one
9941 * bit set at most. */
9942 list_for_each_entry(connector, &dev->mode_config.connector_list,
9943 base.head) {
9944 if (connector->base.encoder == &connector->new_encoder->base)
9945 continue;
79e53945 9946
e2e1ed41
DV
9947 if (connector->base.encoder) {
9948 tmp_crtc = connector->base.encoder->crtc;
9949
9950 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9951 }
9952
9953 if (connector->new_encoder)
9954 *prepare_pipes |=
9955 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
9956 }
9957
e2e1ed41
DV
9958 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9959 base.head) {
9960 if (encoder->base.crtc == &encoder->new_crtc->base)
9961 continue;
9962
9963 if (encoder->base.crtc) {
9964 tmp_crtc = encoder->base.crtc;
9965
9966 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9967 }
9968
9969 if (encoder->new_crtc)
9970 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
9971 }
9972
7668851f 9973 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 9974 for_each_intel_crtc(dev, intel_crtc) {
7668851f 9975 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 9976 continue;
7e7d76c3 9977
7668851f 9978 if (!intel_crtc->new_enabled)
e2e1ed41 9979 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
9980 else
9981 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
9982 }
9983
e2e1ed41
DV
9984
9985 /* set_mode is also used to update properties on life display pipes. */
9986 intel_crtc = to_intel_crtc(crtc);
7668851f 9987 if (intel_crtc->new_enabled)
e2e1ed41
DV
9988 *prepare_pipes |= 1 << intel_crtc->pipe;
9989
b6c5164d
DV
9990 /*
9991 * For simplicity do a full modeset on any pipe where the output routing
9992 * changed. We could be more clever, but that would require us to be
9993 * more careful with calling the relevant encoder->mode_set functions.
9994 */
e2e1ed41
DV
9995 if (*prepare_pipes)
9996 *modeset_pipes = *prepare_pipes;
9997
9998 /* ... and mask these out. */
9999 *modeset_pipes &= ~(*disable_pipes);
10000 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10001
10002 /*
10003 * HACK: We don't (yet) fully support global modesets. intel_set_config
10004 * obies this rule, but the modeset restore mode of
10005 * intel_modeset_setup_hw_state does not.
10006 */
10007 *modeset_pipes &= 1 << intel_crtc->pipe;
10008 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10009
10010 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10011 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10012}
79e53945 10013
ea9d758d 10014static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10015{
ea9d758d 10016 struct drm_encoder *encoder;
f6e5b160 10017 struct drm_device *dev = crtc->dev;
f6e5b160 10018
ea9d758d
DV
10019 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10020 if (encoder->crtc == crtc)
10021 return true;
10022
10023 return false;
10024}
10025
10026static void
10027intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10028{
10029 struct intel_encoder *intel_encoder;
10030 struct intel_crtc *intel_crtc;
10031 struct drm_connector *connector;
10032
10033 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
10034 base.head) {
10035 if (!intel_encoder->base.crtc)
10036 continue;
10037
10038 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10039
10040 if (prepare_pipes & (1 << intel_crtc->pipe))
10041 intel_encoder->connectors_active = false;
10042 }
10043
10044 intel_modeset_commit_output_state(dev);
10045
7668851f 10046 /* Double check state. */
d3fcc808 10047 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10048 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10049 WARN_ON(intel_crtc->new_config &&
10050 intel_crtc->new_config != &intel_crtc->config);
10051 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10052 }
10053
10054 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10055 if (!connector->encoder || !connector->encoder->crtc)
10056 continue;
10057
10058 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10059
10060 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10061 struct drm_property *dpms_property =
10062 dev->mode_config.dpms_property;
10063
ea9d758d 10064 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10065 drm_object_property_set_value(&connector->base,
68d34720
DV
10066 dpms_property,
10067 DRM_MODE_DPMS_ON);
ea9d758d
DV
10068
10069 intel_encoder = to_intel_encoder(connector->encoder);
10070 intel_encoder->connectors_active = true;
10071 }
10072 }
10073
10074}
10075
3bd26263 10076static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10077{
3bd26263 10078 int diff;
f1f644dc
JB
10079
10080 if (clock1 == clock2)
10081 return true;
10082
10083 if (!clock1 || !clock2)
10084 return false;
10085
10086 diff = abs(clock1 - clock2);
10087
10088 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10089 return true;
10090
10091 return false;
10092}
10093
25c5b266
DV
10094#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10095 list_for_each_entry((intel_crtc), \
10096 &(dev)->mode_config.crtc_list, \
10097 base.head) \
0973f18f 10098 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10099
0e8ffe1b 10100static bool
2fa2fe9a
DV
10101intel_pipe_config_compare(struct drm_device *dev,
10102 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10103 struct intel_crtc_config *pipe_config)
10104{
66e985c0
DV
10105#define PIPE_CONF_CHECK_X(name) \
10106 if (current_config->name != pipe_config->name) { \
10107 DRM_ERROR("mismatch in " #name " " \
10108 "(expected 0x%08x, found 0x%08x)\n", \
10109 current_config->name, \
10110 pipe_config->name); \
10111 return false; \
10112 }
10113
08a24034
DV
10114#define PIPE_CONF_CHECK_I(name) \
10115 if (current_config->name != pipe_config->name) { \
10116 DRM_ERROR("mismatch in " #name " " \
10117 "(expected %i, found %i)\n", \
10118 current_config->name, \
10119 pipe_config->name); \
10120 return false; \
88adfff1
DV
10121 }
10122
1bd1bd80
DV
10123#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10124 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10125 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10126 "(expected %i, found %i)\n", \
10127 current_config->name & (mask), \
10128 pipe_config->name & (mask)); \
10129 return false; \
10130 }
10131
5e550656
VS
10132#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10133 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10134 DRM_ERROR("mismatch in " #name " " \
10135 "(expected %i, found %i)\n", \
10136 current_config->name, \
10137 pipe_config->name); \
10138 return false; \
10139 }
10140
bb760063
DV
10141#define PIPE_CONF_QUIRK(quirk) \
10142 ((current_config->quirks | pipe_config->quirks) & (quirk))
10143
eccb140b
DV
10144 PIPE_CONF_CHECK_I(cpu_transcoder);
10145
08a24034
DV
10146 PIPE_CONF_CHECK_I(has_pch_encoder);
10147 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10148 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10149 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10150 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10151 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10152 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10153
eb14cb74
VS
10154 PIPE_CONF_CHECK_I(has_dp_encoder);
10155 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10156 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10157 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10158 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10159 PIPE_CONF_CHECK_I(dp_m_n.tu);
10160
1bd1bd80
DV
10161 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10162 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10163 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10164 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10165 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10166 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10167
10168 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10169 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10170 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10171 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10172 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10173 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10174
c93f54cf 10175 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10176 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10177 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10178 IS_VALLEYVIEW(dev))
10179 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10180
9ed109a7
DV
10181 PIPE_CONF_CHECK_I(has_audio);
10182
1bd1bd80
DV
10183 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10184 DRM_MODE_FLAG_INTERLACE);
10185
bb760063
DV
10186 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10187 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10188 DRM_MODE_FLAG_PHSYNC);
10189 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10190 DRM_MODE_FLAG_NHSYNC);
10191 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10192 DRM_MODE_FLAG_PVSYNC);
10193 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10194 DRM_MODE_FLAG_NVSYNC);
10195 }
045ac3b5 10196
37327abd
VS
10197 PIPE_CONF_CHECK_I(pipe_src_w);
10198 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10199
9953599b
DV
10200 /*
10201 * FIXME: BIOS likes to set up a cloned config with lvds+external
10202 * screen. Since we don't yet re-compute the pipe config when moving
10203 * just the lvds port away to another pipe the sw tracking won't match.
10204 *
10205 * Proper atomic modesets with recomputed global state will fix this.
10206 * Until then just don't check gmch state for inherited modes.
10207 */
10208 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10209 PIPE_CONF_CHECK_I(gmch_pfit.control);
10210 /* pfit ratios are autocomputed by the hw on gen4+ */
10211 if (INTEL_INFO(dev)->gen < 4)
10212 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10213 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10214 }
10215
fd4daa9c
CW
10216 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10217 if (current_config->pch_pfit.enabled) {
10218 PIPE_CONF_CHECK_I(pch_pfit.pos);
10219 PIPE_CONF_CHECK_I(pch_pfit.size);
10220 }
2fa2fe9a 10221
e59150dc
JB
10222 /* BDW+ don't expose a synchronous way to read the state */
10223 if (IS_HASWELL(dev))
10224 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10225
282740f7
VS
10226 PIPE_CONF_CHECK_I(double_wide);
10227
c0d43d62 10228 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10229 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10230 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10231 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10232 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
c0d43d62 10233
42571aef
VS
10234 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10235 PIPE_CONF_CHECK_I(pipe_bpp);
10236
a9a7e98a
JB
10237 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10238 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10239
66e985c0 10240#undef PIPE_CONF_CHECK_X
08a24034 10241#undef PIPE_CONF_CHECK_I
1bd1bd80 10242#undef PIPE_CONF_CHECK_FLAGS
5e550656 10243#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10244#undef PIPE_CONF_QUIRK
88adfff1 10245
0e8ffe1b
DV
10246 return true;
10247}
10248
91d1b4bd
DV
10249static void
10250check_connector_state(struct drm_device *dev)
8af6cf88 10251{
8af6cf88
DV
10252 struct intel_connector *connector;
10253
10254 list_for_each_entry(connector, &dev->mode_config.connector_list,
10255 base.head) {
10256 /* This also checks the encoder/connector hw state with the
10257 * ->get_hw_state callbacks. */
10258 intel_connector_check_state(connector);
10259
10260 WARN(&connector->new_encoder->base != connector->base.encoder,
10261 "connector's staged encoder doesn't match current encoder\n");
10262 }
91d1b4bd
DV
10263}
10264
10265static void
10266check_encoder_state(struct drm_device *dev)
10267{
10268 struct intel_encoder *encoder;
10269 struct intel_connector *connector;
8af6cf88
DV
10270
10271 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10272 base.head) {
10273 bool enabled = false;
10274 bool active = false;
10275 enum pipe pipe, tracked_pipe;
10276
10277 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10278 encoder->base.base.id,
8e329a03 10279 encoder->base.name);
8af6cf88
DV
10280
10281 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10282 "encoder's stage crtc doesn't match current crtc\n");
10283 WARN(encoder->connectors_active && !encoder->base.crtc,
10284 "encoder's active_connectors set, but no crtc\n");
10285
10286 list_for_each_entry(connector, &dev->mode_config.connector_list,
10287 base.head) {
10288 if (connector->base.encoder != &encoder->base)
10289 continue;
10290 enabled = true;
10291 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10292 active = true;
10293 }
10294 WARN(!!encoder->base.crtc != enabled,
10295 "encoder's enabled state mismatch "
10296 "(expected %i, found %i)\n",
10297 !!encoder->base.crtc, enabled);
10298 WARN(active && !encoder->base.crtc,
10299 "active encoder with no crtc\n");
10300
10301 WARN(encoder->connectors_active != active,
10302 "encoder's computed active state doesn't match tracked active state "
10303 "(expected %i, found %i)\n", active, encoder->connectors_active);
10304
10305 active = encoder->get_hw_state(encoder, &pipe);
10306 WARN(active != encoder->connectors_active,
10307 "encoder's hw state doesn't match sw tracking "
10308 "(expected %i, found %i)\n",
10309 encoder->connectors_active, active);
10310
10311 if (!encoder->base.crtc)
10312 continue;
10313
10314 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10315 WARN(active && pipe != tracked_pipe,
10316 "active encoder's pipe doesn't match"
10317 "(expected %i, found %i)\n",
10318 tracked_pipe, pipe);
10319
10320 }
91d1b4bd
DV
10321}
10322
10323static void
10324check_crtc_state(struct drm_device *dev)
10325{
fbee40df 10326 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10327 struct intel_crtc *crtc;
10328 struct intel_encoder *encoder;
10329 struct intel_crtc_config pipe_config;
8af6cf88 10330
d3fcc808 10331 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10332 bool enabled = false;
10333 bool active = false;
10334
045ac3b5
JB
10335 memset(&pipe_config, 0, sizeof(pipe_config));
10336
8af6cf88
DV
10337 DRM_DEBUG_KMS("[CRTC:%d]\n",
10338 crtc->base.base.id);
10339
10340 WARN(crtc->active && !crtc->base.enabled,
10341 "active crtc, but not enabled in sw tracking\n");
10342
10343 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10344 base.head) {
10345 if (encoder->base.crtc != &crtc->base)
10346 continue;
10347 enabled = true;
10348 if (encoder->connectors_active)
10349 active = true;
10350 }
6c49f241 10351
8af6cf88
DV
10352 WARN(active != crtc->active,
10353 "crtc's computed active state doesn't match tracked active state "
10354 "(expected %i, found %i)\n", active, crtc->active);
10355 WARN(enabled != crtc->base.enabled,
10356 "crtc's computed enabled state doesn't match tracked enabled state "
10357 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10358
0e8ffe1b
DV
10359 active = dev_priv->display.get_pipe_config(crtc,
10360 &pipe_config);
d62cf62a
DV
10361
10362 /* hw state is inconsistent with the pipe A quirk */
10363 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10364 active = crtc->active;
10365
6c49f241
DV
10366 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10367 base.head) {
3eaba51c 10368 enum pipe pipe;
6c49f241
DV
10369 if (encoder->base.crtc != &crtc->base)
10370 continue;
1d37b689 10371 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10372 encoder->get_config(encoder, &pipe_config);
10373 }
10374
0e8ffe1b
DV
10375 WARN(crtc->active != active,
10376 "crtc active state doesn't match with hw state "
10377 "(expected %i, found %i)\n", crtc->active, active);
10378
c0b03411
DV
10379 if (active &&
10380 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10381 WARN(1, "pipe state doesn't match!\n");
10382 intel_dump_pipe_config(crtc, &pipe_config,
10383 "[hw state]");
10384 intel_dump_pipe_config(crtc, &crtc->config,
10385 "[sw state]");
10386 }
8af6cf88
DV
10387 }
10388}
10389
91d1b4bd
DV
10390static void
10391check_shared_dpll_state(struct drm_device *dev)
10392{
fbee40df 10393 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10394 struct intel_crtc *crtc;
10395 struct intel_dpll_hw_state dpll_hw_state;
10396 int i;
5358901f
DV
10397
10398 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10399 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10400 int enabled_crtcs = 0, active_crtcs = 0;
10401 bool active;
10402
10403 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10404
10405 DRM_DEBUG_KMS("%s\n", pll->name);
10406
10407 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10408
10409 WARN(pll->active > pll->refcount,
10410 "more active pll users than references: %i vs %i\n",
10411 pll->active, pll->refcount);
10412 WARN(pll->active && !pll->on,
10413 "pll in active use but not on in sw tracking\n");
35c95375
DV
10414 WARN(pll->on && !pll->active,
10415 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10416 WARN(pll->on != active,
10417 "pll on state mismatch (expected %i, found %i)\n",
10418 pll->on, active);
10419
d3fcc808 10420 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10421 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10422 enabled_crtcs++;
10423 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10424 active_crtcs++;
10425 }
10426 WARN(pll->active != active_crtcs,
10427 "pll active crtcs mismatch (expected %i, found %i)\n",
10428 pll->active, active_crtcs);
10429 WARN(pll->refcount != enabled_crtcs,
10430 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10431 pll->refcount, enabled_crtcs);
66e985c0
DV
10432
10433 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10434 sizeof(dpll_hw_state)),
10435 "pll hw state mismatch\n");
5358901f 10436 }
8af6cf88
DV
10437}
10438
91d1b4bd
DV
10439void
10440intel_modeset_check_state(struct drm_device *dev)
10441{
10442 check_connector_state(dev);
10443 check_encoder_state(dev);
10444 check_crtc_state(dev);
10445 check_shared_dpll_state(dev);
10446}
10447
18442d08
VS
10448void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10449 int dotclock)
10450{
10451 /*
10452 * FDI already provided one idea for the dotclock.
10453 * Yell if the encoder disagrees.
10454 */
241bfc38 10455 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10456 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10457 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10458}
10459
80715b2f
VS
10460static void update_scanline_offset(struct intel_crtc *crtc)
10461{
10462 struct drm_device *dev = crtc->base.dev;
10463
10464 /*
10465 * The scanline counter increments at the leading edge of hsync.
10466 *
10467 * On most platforms it starts counting from vtotal-1 on the
10468 * first active line. That means the scanline counter value is
10469 * always one less than what we would expect. Ie. just after
10470 * start of vblank, which also occurs at start of hsync (on the
10471 * last active line), the scanline counter will read vblank_start-1.
10472 *
10473 * On gen2 the scanline counter starts counting from 1 instead
10474 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10475 * to keep the value positive), instead of adding one.
10476 *
10477 * On HSW+ the behaviour of the scanline counter depends on the output
10478 * type. For DP ports it behaves like most other platforms, but on HDMI
10479 * there's an extra 1 line difference. So we need to add two instead of
10480 * one to the value.
10481 */
10482 if (IS_GEN2(dev)) {
10483 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10484 int vtotal;
10485
10486 vtotal = mode->crtc_vtotal;
10487 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10488 vtotal /= 2;
10489
10490 crtc->scanline_offset = vtotal - 1;
10491 } else if (HAS_DDI(dev) &&
10492 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10493 crtc->scanline_offset = 2;
10494 } else
10495 crtc->scanline_offset = 1;
10496}
10497
f30da187
DV
10498static int __intel_set_mode(struct drm_crtc *crtc,
10499 struct drm_display_mode *mode,
10500 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10501{
10502 struct drm_device *dev = crtc->dev;
fbee40df 10503 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10504 struct drm_display_mode *saved_mode;
b8cecdf5 10505 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10506 struct intel_crtc *intel_crtc;
10507 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10508 int ret = 0;
a6778b3c 10509
4b4b9238 10510 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10511 if (!saved_mode)
10512 return -ENOMEM;
a6778b3c 10513
e2e1ed41 10514 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10515 &prepare_pipes, &disable_pipes);
10516
3ac18232 10517 *saved_mode = crtc->mode;
a6778b3c 10518
25c5b266
DV
10519 /* Hack: Because we don't (yet) support global modeset on multiple
10520 * crtcs, we don't keep track of the new mode for more than one crtc.
10521 * Hence simply check whether any bit is set in modeset_pipes in all the
10522 * pieces of code that are not yet converted to deal with mutliple crtcs
10523 * changing their mode at the same time. */
25c5b266 10524 if (modeset_pipes) {
4e53c2e0 10525 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10526 if (IS_ERR(pipe_config)) {
10527 ret = PTR_ERR(pipe_config);
10528 pipe_config = NULL;
10529
3ac18232 10530 goto out;
25c5b266 10531 }
c0b03411
DV
10532 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10533 "[modeset]");
50741abc 10534 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10535 }
a6778b3c 10536
30a970c6
JB
10537 /*
10538 * See if the config requires any additional preparation, e.g.
10539 * to adjust global state with pipes off. We need to do this
10540 * here so we can get the modeset_pipe updated config for the new
10541 * mode set on this crtc. For other crtcs we need to use the
10542 * adjusted_mode bits in the crtc directly.
10543 */
c164f833 10544 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10545 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10546
c164f833
VS
10547 /* may have added more to prepare_pipes than we should */
10548 prepare_pipes &= ~disable_pipes;
10549 }
10550
460da916
DV
10551 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10552 intel_crtc_disable(&intel_crtc->base);
10553
ea9d758d
DV
10554 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10555 if (intel_crtc->base.enabled)
10556 dev_priv->display.crtc_disable(&intel_crtc->base);
10557 }
a6778b3c 10558
6c4c86f5
DV
10559 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10560 * to set it here already despite that we pass it down the callchain.
f6e5b160 10561 */
b8cecdf5 10562 if (modeset_pipes) {
25c5b266 10563 crtc->mode = *mode;
b8cecdf5
DV
10564 /* mode_set/enable/disable functions rely on a correct pipe
10565 * config. */
10566 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10567 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10568
10569 /*
10570 * Calculate and store various constants which
10571 * are later needed by vblank and swap-completion
10572 * timestamping. They are derived from true hwmode.
10573 */
10574 drm_calc_timestamping_constants(crtc,
10575 &pipe_config->adjusted_mode);
b8cecdf5 10576 }
7758a113 10577
ea9d758d
DV
10578 /* Only after disabling all output pipelines that will be changed can we
10579 * update the the output configuration. */
10580 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10581
47fab737
DV
10582 if (dev_priv->display.modeset_global_resources)
10583 dev_priv->display.modeset_global_resources(dev);
10584
a6778b3c
DV
10585 /* Set up the DPLL and any encoders state that needs to adjust or depend
10586 * on the DPLL.
f6e5b160 10587 */
25c5b266 10588 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
4c10794f 10589 struct drm_framebuffer *old_fb;
a071fa00
DV
10590 struct drm_i915_gem_object *old_obj = NULL;
10591 struct drm_i915_gem_object *obj =
10592 to_intel_framebuffer(fb)->obj;
4c10794f
DV
10593
10594 mutex_lock(&dev->struct_mutex);
10595 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 10596 obj,
4c10794f
DV
10597 NULL);
10598 if (ret != 0) {
10599 DRM_ERROR("pin & fence failed\n");
10600 mutex_unlock(&dev->struct_mutex);
10601 goto done;
10602 }
10603 old_fb = crtc->primary->fb;
a071fa00
DV
10604 if (old_fb) {
10605 old_obj = to_intel_framebuffer(old_fb)->obj;
10606 intel_unpin_fb_obj(old_obj);
10607 }
10608 i915_gem_track_fb(old_obj, obj,
10609 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
10610 mutex_unlock(&dev->struct_mutex);
10611
10612 crtc->primary->fb = fb;
10613 crtc->x = x;
10614 crtc->y = y;
10615
4271b753
DV
10616 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10617 x, y, fb);
c0c36b94
CW
10618 if (ret)
10619 goto done;
a6778b3c
DV
10620 }
10621
10622 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
10623 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10624 update_scanline_offset(intel_crtc);
10625
25c5b266 10626 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 10627 }
a6778b3c 10628
a6778b3c
DV
10629 /* FIXME: add subpixel order */
10630done:
4b4b9238 10631 if (ret && crtc->enabled)
3ac18232 10632 crtc->mode = *saved_mode;
a6778b3c 10633
3ac18232 10634out:
b8cecdf5 10635 kfree(pipe_config);
3ac18232 10636 kfree(saved_mode);
a6778b3c 10637 return ret;
f6e5b160
CW
10638}
10639
e7457a9a
DL
10640static int intel_set_mode(struct drm_crtc *crtc,
10641 struct drm_display_mode *mode,
10642 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
10643{
10644 int ret;
10645
10646 ret = __intel_set_mode(crtc, mode, x, y, fb);
10647
10648 if (ret == 0)
10649 intel_modeset_check_state(crtc->dev);
10650
10651 return ret;
10652}
10653
c0c36b94
CW
10654void intel_crtc_restore_mode(struct drm_crtc *crtc)
10655{
f4510a27 10656 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
10657}
10658
25c5b266
DV
10659#undef for_each_intel_crtc_masked
10660
d9e55608
DV
10661static void intel_set_config_free(struct intel_set_config *config)
10662{
10663 if (!config)
10664 return;
10665
1aa4b628
DV
10666 kfree(config->save_connector_encoders);
10667 kfree(config->save_encoder_crtcs);
7668851f 10668 kfree(config->save_crtc_enabled);
d9e55608
DV
10669 kfree(config);
10670}
10671
85f9eb71
DV
10672static int intel_set_config_save_state(struct drm_device *dev,
10673 struct intel_set_config *config)
10674{
7668851f 10675 struct drm_crtc *crtc;
85f9eb71
DV
10676 struct drm_encoder *encoder;
10677 struct drm_connector *connector;
10678 int count;
10679
7668851f
VS
10680 config->save_crtc_enabled =
10681 kcalloc(dev->mode_config.num_crtc,
10682 sizeof(bool), GFP_KERNEL);
10683 if (!config->save_crtc_enabled)
10684 return -ENOMEM;
10685
1aa4b628
DV
10686 config->save_encoder_crtcs =
10687 kcalloc(dev->mode_config.num_encoder,
10688 sizeof(struct drm_crtc *), GFP_KERNEL);
10689 if (!config->save_encoder_crtcs)
85f9eb71
DV
10690 return -ENOMEM;
10691
1aa4b628
DV
10692 config->save_connector_encoders =
10693 kcalloc(dev->mode_config.num_connector,
10694 sizeof(struct drm_encoder *), GFP_KERNEL);
10695 if (!config->save_connector_encoders)
85f9eb71
DV
10696 return -ENOMEM;
10697
10698 /* Copy data. Note that driver private data is not affected.
10699 * Should anything bad happen only the expected state is
10700 * restored, not the drivers personal bookkeeping.
10701 */
7668851f 10702 count = 0;
70e1e0ec 10703 for_each_crtc(dev, crtc) {
7668851f
VS
10704 config->save_crtc_enabled[count++] = crtc->enabled;
10705 }
10706
85f9eb71
DV
10707 count = 0;
10708 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 10709 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
10710 }
10711
10712 count = 0;
10713 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 10714 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
10715 }
10716
10717 return 0;
10718}
10719
10720static void intel_set_config_restore_state(struct drm_device *dev,
10721 struct intel_set_config *config)
10722{
7668851f 10723 struct intel_crtc *crtc;
9a935856
DV
10724 struct intel_encoder *encoder;
10725 struct intel_connector *connector;
85f9eb71
DV
10726 int count;
10727
7668851f 10728 count = 0;
d3fcc808 10729 for_each_intel_crtc(dev, crtc) {
7668851f 10730 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
10731
10732 if (crtc->new_enabled)
10733 crtc->new_config = &crtc->config;
10734 else
10735 crtc->new_config = NULL;
7668851f
VS
10736 }
10737
85f9eb71 10738 count = 0;
9a935856
DV
10739 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10740 encoder->new_crtc =
10741 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
10742 }
10743
10744 count = 0;
9a935856
DV
10745 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10746 connector->new_encoder =
10747 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
10748 }
10749}
10750
e3de42b6 10751static bool
2e57f47d 10752is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
10753{
10754 int i;
10755
2e57f47d
CW
10756 if (set->num_connectors == 0)
10757 return false;
10758
10759 if (WARN_ON(set->connectors == NULL))
10760 return false;
10761
10762 for (i = 0; i < set->num_connectors; i++)
10763 if (set->connectors[i]->encoder &&
10764 set->connectors[i]->encoder->crtc == set->crtc &&
10765 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
10766 return true;
10767
10768 return false;
10769}
10770
5e2b584e
DV
10771static void
10772intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10773 struct intel_set_config *config)
10774{
10775
10776 /* We should be able to check here if the fb has the same properties
10777 * and then just flip_or_move it */
2e57f47d
CW
10778 if (is_crtc_connector_off(set)) {
10779 config->mode_changed = true;
f4510a27 10780 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
10781 /*
10782 * If we have no fb, we can only flip as long as the crtc is
10783 * active, otherwise we need a full mode set. The crtc may
10784 * be active if we've only disabled the primary plane, or
10785 * in fastboot situations.
10786 */
f4510a27 10787 if (set->crtc->primary->fb == NULL) {
319d9827
JB
10788 struct intel_crtc *intel_crtc =
10789 to_intel_crtc(set->crtc);
10790
3b150f08 10791 if (intel_crtc->active) {
319d9827
JB
10792 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10793 config->fb_changed = true;
10794 } else {
10795 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10796 config->mode_changed = true;
10797 }
5e2b584e
DV
10798 } else if (set->fb == NULL) {
10799 config->mode_changed = true;
72f4901e 10800 } else if (set->fb->pixel_format !=
f4510a27 10801 set->crtc->primary->fb->pixel_format) {
5e2b584e 10802 config->mode_changed = true;
e3de42b6 10803 } else {
5e2b584e 10804 config->fb_changed = true;
e3de42b6 10805 }
5e2b584e
DV
10806 }
10807
835c5873 10808 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
10809 config->fb_changed = true;
10810
10811 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10812 DRM_DEBUG_KMS("modes are different, full mode set\n");
10813 drm_mode_debug_printmodeline(&set->crtc->mode);
10814 drm_mode_debug_printmodeline(set->mode);
10815 config->mode_changed = true;
10816 }
a1d95703
CW
10817
10818 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10819 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
10820}
10821
2e431051 10822static int
9a935856
DV
10823intel_modeset_stage_output_state(struct drm_device *dev,
10824 struct drm_mode_set *set,
10825 struct intel_set_config *config)
50f56119 10826{
9a935856
DV
10827 struct intel_connector *connector;
10828 struct intel_encoder *encoder;
7668851f 10829 struct intel_crtc *crtc;
f3f08572 10830 int ro;
50f56119 10831
9abdda74 10832 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
10833 * of connectors. For paranoia, double-check this. */
10834 WARN_ON(!set->fb && (set->num_connectors != 0));
10835 WARN_ON(set->fb && (set->num_connectors == 0));
10836
9a935856
DV
10837 list_for_each_entry(connector, &dev->mode_config.connector_list,
10838 base.head) {
10839 /* Otherwise traverse passed in connector list and get encoders
10840 * for them. */
50f56119 10841 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856
DV
10842 if (set->connectors[ro] == &connector->base) {
10843 connector->new_encoder = connector->encoder;
50f56119
DV
10844 break;
10845 }
10846 }
10847
9a935856
DV
10848 /* If we disable the crtc, disable all its connectors. Also, if
10849 * the connector is on the changing crtc but not on the new
10850 * connector list, disable it. */
10851 if ((!set->fb || ro == set->num_connectors) &&
10852 connector->base.encoder &&
10853 connector->base.encoder->crtc == set->crtc) {
10854 connector->new_encoder = NULL;
10855
10856 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10857 connector->base.base.id,
c23cc417 10858 connector->base.name);
9a935856
DV
10859 }
10860
10861
10862 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 10863 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 10864 config->mode_changed = true;
50f56119
DV
10865 }
10866 }
9a935856 10867 /* connector->new_encoder is now updated for all connectors. */
50f56119 10868
9a935856 10869 /* Update crtc of enabled connectors. */
9a935856
DV
10870 list_for_each_entry(connector, &dev->mode_config.connector_list,
10871 base.head) {
7668851f
VS
10872 struct drm_crtc *new_crtc;
10873
9a935856 10874 if (!connector->new_encoder)
50f56119
DV
10875 continue;
10876
9a935856 10877 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
10878
10879 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 10880 if (set->connectors[ro] == &connector->base)
50f56119
DV
10881 new_crtc = set->crtc;
10882 }
10883
10884 /* Make sure the new CRTC will work with the encoder */
14509916
TR
10885 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10886 new_crtc)) {
5e2b584e 10887 return -EINVAL;
50f56119 10888 }
9a935856
DV
10889 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10890
10891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10892 connector->base.base.id,
c23cc417 10893 connector->base.name,
9a935856
DV
10894 new_crtc->base.id);
10895 }
10896
10897 /* Check for any encoders that needs to be disabled. */
10898 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10899 base.head) {
5a65f358 10900 int num_connectors = 0;
9a935856
DV
10901 list_for_each_entry(connector,
10902 &dev->mode_config.connector_list,
10903 base.head) {
10904 if (connector->new_encoder == encoder) {
10905 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 10906 num_connectors++;
9a935856
DV
10907 }
10908 }
5a65f358
PZ
10909
10910 if (num_connectors == 0)
10911 encoder->new_crtc = NULL;
10912 else if (num_connectors > 1)
10913 return -EINVAL;
10914
9a935856
DV
10915 /* Only now check for crtc changes so we don't miss encoders
10916 * that will be disabled. */
10917 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 10918 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 10919 config->mode_changed = true;
50f56119
DV
10920 }
10921 }
9a935856 10922 /* Now we've also updated encoder->new_crtc for all encoders. */
50f56119 10923
d3fcc808 10924 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10925 crtc->new_enabled = false;
10926
10927 list_for_each_entry(encoder,
10928 &dev->mode_config.encoder_list,
10929 base.head) {
10930 if (encoder->new_crtc == crtc) {
10931 crtc->new_enabled = true;
10932 break;
10933 }
10934 }
10935
10936 if (crtc->new_enabled != crtc->base.enabled) {
10937 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10938 crtc->new_enabled ? "en" : "dis");
10939 config->mode_changed = true;
10940 }
7bd0a8e7
VS
10941
10942 if (crtc->new_enabled)
10943 crtc->new_config = &crtc->config;
10944 else
10945 crtc->new_config = NULL;
7668851f
VS
10946 }
10947
2e431051
DV
10948 return 0;
10949}
10950
7d00a1f5
VS
10951static void disable_crtc_nofb(struct intel_crtc *crtc)
10952{
10953 struct drm_device *dev = crtc->base.dev;
10954 struct intel_encoder *encoder;
10955 struct intel_connector *connector;
10956
10957 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10958 pipe_name(crtc->pipe));
10959
10960 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10961 if (connector->new_encoder &&
10962 connector->new_encoder->new_crtc == crtc)
10963 connector->new_encoder = NULL;
10964 }
10965
10966 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10967 if (encoder->new_crtc == crtc)
10968 encoder->new_crtc = NULL;
10969 }
10970
10971 crtc->new_enabled = false;
7bd0a8e7 10972 crtc->new_config = NULL;
7d00a1f5
VS
10973}
10974
2e431051
DV
10975static int intel_crtc_set_config(struct drm_mode_set *set)
10976{
10977 struct drm_device *dev;
2e431051
DV
10978 struct drm_mode_set save_set;
10979 struct intel_set_config *config;
10980 int ret;
2e431051 10981
8d3e375e
DV
10982 BUG_ON(!set);
10983 BUG_ON(!set->crtc);
10984 BUG_ON(!set->crtc->helper_private);
2e431051 10985
7e53f3a4
DV
10986 /* Enforce sane interface api - has been abused by the fb helper. */
10987 BUG_ON(!set->mode && set->fb);
10988 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 10989
2e431051
DV
10990 if (set->fb) {
10991 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10992 set->crtc->base.id, set->fb->base.id,
10993 (int)set->num_connectors, set->x, set->y);
10994 } else {
10995 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
10996 }
10997
10998 dev = set->crtc->dev;
10999
11000 ret = -ENOMEM;
11001 config = kzalloc(sizeof(*config), GFP_KERNEL);
11002 if (!config)
11003 goto out_config;
11004
11005 ret = intel_set_config_save_state(dev, config);
11006 if (ret)
11007 goto out_config;
11008
11009 save_set.crtc = set->crtc;
11010 save_set.mode = &set->crtc->mode;
11011 save_set.x = set->crtc->x;
11012 save_set.y = set->crtc->y;
f4510a27 11013 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11014
11015 /* Compute whether we need a full modeset, only an fb base update or no
11016 * change at all. In the future we might also check whether only the
11017 * mode changed, e.g. for LVDS where we only change the panel fitter in
11018 * such cases. */
11019 intel_set_config_compute_mode_changes(set, config);
11020
9a935856 11021 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11022 if (ret)
11023 goto fail;
11024
5e2b584e 11025 if (config->mode_changed) {
c0c36b94
CW
11026 ret = intel_set_mode(set->crtc, set->mode,
11027 set->x, set->y, set->fb);
5e2b584e 11028 } else if (config->fb_changed) {
3b150f08
MR
11029 struct drm_i915_private *dev_priv = dev->dev_private;
11030 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11031
4878cae2
VS
11032 intel_crtc_wait_for_pending_flips(set->crtc);
11033
4f660f49 11034 ret = intel_pipe_set_base(set->crtc,
94352cf9 11035 set->x, set->y, set->fb);
3b150f08
MR
11036
11037 /*
11038 * We need to make sure the primary plane is re-enabled if it
11039 * has previously been turned off.
11040 */
11041 if (!intel_crtc->primary_enabled && ret == 0) {
11042 WARN_ON(!intel_crtc->active);
11043 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11044 intel_crtc->pipe);
11045 }
11046
7ca51a3a
JB
11047 /*
11048 * In the fastboot case this may be our only check of the
11049 * state after boot. It would be better to only do it on
11050 * the first update, but we don't have a nice way of doing that
11051 * (and really, set_config isn't used much for high freq page
11052 * flipping, so increasing its cost here shouldn't be a big
11053 * deal).
11054 */
d330a953 11055 if (i915.fastboot && ret == 0)
7ca51a3a 11056 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11057 }
11058
2d05eae1 11059 if (ret) {
bf67dfeb
DV
11060 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11061 set->crtc->base.id, ret);
50f56119 11062fail:
2d05eae1 11063 intel_set_config_restore_state(dev, config);
50f56119 11064
7d00a1f5
VS
11065 /*
11066 * HACK: if the pipe was on, but we didn't have a framebuffer,
11067 * force the pipe off to avoid oopsing in the modeset code
11068 * due to fb==NULL. This should only happen during boot since
11069 * we don't yet reconstruct the FB from the hardware state.
11070 */
11071 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11072 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11073
2d05eae1
CW
11074 /* Try to restore the config */
11075 if (config->mode_changed &&
11076 intel_set_mode(save_set.crtc, save_set.mode,
11077 save_set.x, save_set.y, save_set.fb))
11078 DRM_ERROR("failed to restore config after modeset failure\n");
11079 }
50f56119 11080
d9e55608
DV
11081out_config:
11082 intel_set_config_free(config);
50f56119
DV
11083 return ret;
11084}
f6e5b160
CW
11085
11086static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11087 .gamma_set = intel_crtc_gamma_set,
50f56119 11088 .set_config = intel_crtc_set_config,
f6e5b160
CW
11089 .destroy = intel_crtc_destroy,
11090 .page_flip = intel_crtc_page_flip,
11091};
11092
79f689aa
PZ
11093static void intel_cpu_pll_init(struct drm_device *dev)
11094{
affa9354 11095 if (HAS_DDI(dev))
79f689aa
PZ
11096 intel_ddi_pll_init(dev);
11097}
11098
5358901f
DV
11099static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11100 struct intel_shared_dpll *pll,
11101 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11102{
5358901f 11103 uint32_t val;
ee7b9f93 11104
5358901f 11105 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11106 hw_state->dpll = val;
11107 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11108 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11109
11110 return val & DPLL_VCO_ENABLE;
11111}
11112
15bdd4cf
DV
11113static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11114 struct intel_shared_dpll *pll)
11115{
11116 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11117 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11118}
11119
e7b903d2
DV
11120static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11121 struct intel_shared_dpll *pll)
11122{
e7b903d2 11123 /* PCH refclock must be enabled first */
89eff4be 11124 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11125
15bdd4cf
DV
11126 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11127
11128 /* Wait for the clocks to stabilize. */
11129 POSTING_READ(PCH_DPLL(pll->id));
11130 udelay(150);
11131
11132 /* The pixel multiplier can only be updated once the
11133 * DPLL is enabled and the clocks are stable.
11134 *
11135 * So write it again.
11136 */
11137 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11138 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11139 udelay(200);
11140}
11141
11142static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11143 struct intel_shared_dpll *pll)
11144{
11145 struct drm_device *dev = dev_priv->dev;
11146 struct intel_crtc *crtc;
e7b903d2
DV
11147
11148 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11149 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11150 if (intel_crtc_to_shared_dpll(crtc) == pll)
11151 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11152 }
11153
15bdd4cf
DV
11154 I915_WRITE(PCH_DPLL(pll->id), 0);
11155 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11156 udelay(200);
11157}
11158
46edb027
DV
11159static char *ibx_pch_dpll_names[] = {
11160 "PCH DPLL A",
11161 "PCH DPLL B",
11162};
11163
7c74ade1 11164static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11165{
e7b903d2 11166 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11167 int i;
11168
7c74ade1 11169 dev_priv->num_shared_dpll = 2;
ee7b9f93 11170
e72f9fbf 11171 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11172 dev_priv->shared_dplls[i].id = i;
11173 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11174 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11175 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11176 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11177 dev_priv->shared_dplls[i].get_hw_state =
11178 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11179 }
11180}
11181
7c74ade1
DV
11182static void intel_shared_dpll_init(struct drm_device *dev)
11183{
e7b903d2 11184 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1
DV
11185
11186 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11187 ibx_pch_dpll_init(dev);
11188 else
11189 dev_priv->num_shared_dpll = 0;
11190
11191 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11192}
11193
465c120c
MR
11194static int
11195intel_primary_plane_disable(struct drm_plane *plane)
11196{
11197 struct drm_device *dev = plane->dev;
11198 struct drm_i915_private *dev_priv = dev->dev_private;
11199 struct intel_plane *intel_plane = to_intel_plane(plane);
11200 struct intel_crtc *intel_crtc;
11201
11202 if (!plane->fb)
11203 return 0;
11204
11205 BUG_ON(!plane->crtc);
11206
11207 intel_crtc = to_intel_crtc(plane->crtc);
11208
11209 /*
11210 * Even though we checked plane->fb above, it's still possible that
11211 * the primary plane has been implicitly disabled because the crtc
11212 * coordinates given weren't visible, or because we detected
11213 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11214 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11215 * In either case, we need to unpin the FB and let the fb pointer get
11216 * updated, but otherwise we don't need to touch the hardware.
11217 */
11218 if (!intel_crtc->primary_enabled)
11219 goto disable_unpin;
11220
11221 intel_crtc_wait_for_pending_flips(plane->crtc);
11222 intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
11223 intel_plane->pipe);
465c120c 11224disable_unpin:
a071fa00
DV
11225 i915_gem_track_fb(to_intel_framebuffer(plane->fb)->obj, NULL,
11226 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
465c120c
MR
11227 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11228 plane->fb = NULL;
11229
11230 return 0;
11231}
11232
11233static int
11234intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11235 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11236 unsigned int crtc_w, unsigned int crtc_h,
11237 uint32_t src_x, uint32_t src_y,
11238 uint32_t src_w, uint32_t src_h)
11239{
11240 struct drm_device *dev = crtc->dev;
11241 struct drm_i915_private *dev_priv = dev->dev_private;
11242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11243 struct intel_plane *intel_plane = to_intel_plane(plane);
a071fa00 11244 struct drm_i915_gem_object *obj, *old_obj = NULL;
465c120c
MR
11245 struct drm_rect dest = {
11246 /* integer pixels */
11247 .x1 = crtc_x,
11248 .y1 = crtc_y,
11249 .x2 = crtc_x + crtc_w,
11250 .y2 = crtc_y + crtc_h,
11251 };
11252 struct drm_rect src = {
11253 /* 16.16 fixed point */
11254 .x1 = src_x,
11255 .y1 = src_y,
11256 .x2 = src_x + src_w,
11257 .y2 = src_y + src_h,
11258 };
11259 const struct drm_rect clip = {
11260 /* integer pixels */
11261 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11262 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11263 };
11264 bool visible;
11265 int ret;
11266
11267 ret = drm_plane_helper_check_update(plane, crtc, fb,
11268 &src, &dest, &clip,
11269 DRM_PLANE_HELPER_NO_SCALING,
11270 DRM_PLANE_HELPER_NO_SCALING,
11271 false, true, &visible);
11272
11273 if (ret)
11274 return ret;
11275
a071fa00
DV
11276 if (plane->fb)
11277 old_obj = to_intel_framebuffer(plane->fb)->obj;
11278 obj = to_intel_framebuffer(fb)->obj;
11279
465c120c
MR
11280 /*
11281 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11282 * updating the fb pointer, and returning without touching the
11283 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11284 * turn on the display with all planes setup as desired.
11285 */
11286 if (!crtc->enabled) {
11287 /*
11288 * If we already called setplane while the crtc was disabled,
11289 * we may have an fb pinned; unpin it.
11290 */
11291 if (plane->fb)
a071fa00
DV
11292 intel_unpin_fb_obj(old_obj);
11293
11294 i915_gem_track_fb(old_obj, obj,
11295 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
465c120c
MR
11296
11297 /* Pin and return without programming hardware */
a071fa00 11298 return intel_pin_and_fence_fb_obj(dev, obj, NULL);
465c120c
MR
11299 }
11300
11301 intel_crtc_wait_for_pending_flips(crtc);
11302
11303 /*
11304 * If clipping results in a non-visible primary plane, we'll disable
11305 * the primary plane. Note that this is a bit different than what
11306 * happens if userspace explicitly disables the plane by passing fb=0
11307 * because plane->fb still gets set and pinned.
11308 */
11309 if (!visible) {
11310 /*
11311 * Try to pin the new fb first so that we can bail out if we
11312 * fail.
11313 */
11314 if (plane->fb != fb) {
a071fa00 11315 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
465c120c
MR
11316 if (ret)
11317 return ret;
11318 }
11319
a071fa00
DV
11320 i915_gem_track_fb(old_obj, obj,
11321 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11322
465c120c
MR
11323 if (intel_crtc->primary_enabled)
11324 intel_disable_primary_hw_plane(dev_priv,
11325 intel_plane->plane,
11326 intel_plane->pipe);
11327
11328
11329 if (plane->fb != fb)
11330 if (plane->fb)
a071fa00 11331 intel_unpin_fb_obj(old_obj);
465c120c
MR
11332
11333 return 0;
11334 }
11335
11336 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11337 if (ret)
11338 return ret;
11339
11340 if (!intel_crtc->primary_enabled)
11341 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11342 intel_crtc->pipe);
11343
11344 return 0;
11345}
11346
3d7d6510
MR
11347/* Common destruction function for both primary and cursor planes */
11348static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11349{
11350 struct intel_plane *intel_plane = to_intel_plane(plane);
11351 drm_plane_cleanup(plane);
11352 kfree(intel_plane);
11353}
11354
11355static const struct drm_plane_funcs intel_primary_plane_funcs = {
11356 .update_plane = intel_primary_plane_setplane,
11357 .disable_plane = intel_primary_plane_disable,
3d7d6510 11358 .destroy = intel_plane_destroy,
465c120c
MR
11359};
11360
11361static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11362 int pipe)
11363{
11364 struct intel_plane *primary;
11365 const uint32_t *intel_primary_formats;
11366 int num_formats;
11367
11368 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11369 if (primary == NULL)
11370 return NULL;
11371
11372 primary->can_scale = false;
11373 primary->max_downscale = 1;
11374 primary->pipe = pipe;
11375 primary->plane = pipe;
11376 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11377 primary->plane = !pipe;
11378
11379 if (INTEL_INFO(dev)->gen <= 3) {
11380 intel_primary_formats = intel_primary_formats_gen2;
11381 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11382 } else {
11383 intel_primary_formats = intel_primary_formats_gen4;
11384 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11385 }
11386
11387 drm_universal_plane_init(dev, &primary->base, 0,
11388 &intel_primary_plane_funcs,
11389 intel_primary_formats, num_formats,
11390 DRM_PLANE_TYPE_PRIMARY);
11391 return &primary->base;
11392}
11393
3d7d6510
MR
11394static int
11395intel_cursor_plane_disable(struct drm_plane *plane)
11396{
11397 if (!plane->fb)
11398 return 0;
11399
11400 BUG_ON(!plane->crtc);
11401
11402 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11403}
11404
11405static int
11406intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11407 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11408 unsigned int crtc_w, unsigned int crtc_h,
11409 uint32_t src_x, uint32_t src_y,
11410 uint32_t src_w, uint32_t src_h)
11411{
11412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11413 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11414 struct drm_i915_gem_object *obj = intel_fb->obj;
11415 struct drm_rect dest = {
11416 /* integer pixels */
11417 .x1 = crtc_x,
11418 .y1 = crtc_y,
11419 .x2 = crtc_x + crtc_w,
11420 .y2 = crtc_y + crtc_h,
11421 };
11422 struct drm_rect src = {
11423 /* 16.16 fixed point */
11424 .x1 = src_x,
11425 .y1 = src_y,
11426 .x2 = src_x + src_w,
11427 .y2 = src_y + src_h,
11428 };
11429 const struct drm_rect clip = {
11430 /* integer pixels */
11431 .x2 = intel_crtc->config.pipe_src_w,
11432 .y2 = intel_crtc->config.pipe_src_h,
11433 };
11434 bool visible;
11435 int ret;
11436
11437 ret = drm_plane_helper_check_update(plane, crtc, fb,
11438 &src, &dest, &clip,
11439 DRM_PLANE_HELPER_NO_SCALING,
11440 DRM_PLANE_HELPER_NO_SCALING,
11441 true, true, &visible);
11442 if (ret)
11443 return ret;
11444
11445 crtc->cursor_x = crtc_x;
11446 crtc->cursor_y = crtc_y;
11447 if (fb != crtc->cursor->fb) {
11448 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11449 } else {
11450 intel_crtc_update_cursor(crtc, visible);
11451 return 0;
11452 }
11453}
11454static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11455 .update_plane = intel_cursor_plane_update,
11456 .disable_plane = intel_cursor_plane_disable,
11457 .destroy = intel_plane_destroy,
11458};
11459
11460static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11461 int pipe)
11462{
11463 struct intel_plane *cursor;
11464
11465 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11466 if (cursor == NULL)
11467 return NULL;
11468
11469 cursor->can_scale = false;
11470 cursor->max_downscale = 1;
11471 cursor->pipe = pipe;
11472 cursor->plane = pipe;
11473
11474 drm_universal_plane_init(dev, &cursor->base, 0,
11475 &intel_cursor_plane_funcs,
11476 intel_cursor_formats,
11477 ARRAY_SIZE(intel_cursor_formats),
11478 DRM_PLANE_TYPE_CURSOR);
11479 return &cursor->base;
11480}
11481
b358d0a6 11482static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 11483{
fbee40df 11484 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 11485 struct intel_crtc *intel_crtc;
3d7d6510
MR
11486 struct drm_plane *primary = NULL;
11487 struct drm_plane *cursor = NULL;
465c120c 11488 int i, ret;
79e53945 11489
955382f3 11490 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
11491 if (intel_crtc == NULL)
11492 return;
11493
465c120c 11494 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
11495 if (!primary)
11496 goto fail;
11497
11498 cursor = intel_cursor_plane_create(dev, pipe);
11499 if (!cursor)
11500 goto fail;
11501
465c120c 11502 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
11503 cursor, &intel_crtc_funcs);
11504 if (ret)
11505 goto fail;
79e53945
JB
11506
11507 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
11508 for (i = 0; i < 256; i++) {
11509 intel_crtc->lut_r[i] = i;
11510 intel_crtc->lut_g[i] = i;
11511 intel_crtc->lut_b[i] = i;
11512 }
11513
1f1c2e24
VS
11514 /*
11515 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 11516 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 11517 */
80824003
JB
11518 intel_crtc->pipe = pipe;
11519 intel_crtc->plane = pipe;
3a77c4c4 11520 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 11521 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 11522 intel_crtc->plane = !pipe;
80824003
JB
11523 }
11524
4b0e333e
CW
11525 intel_crtc->cursor_base = ~0;
11526 intel_crtc->cursor_cntl = ~0;
11527
8d7849db
VS
11528 init_waitqueue_head(&intel_crtc->vbl_wait);
11529
22fd0fab
JB
11530 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11531 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11532 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11533 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11534
79e53945 11535 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
11536
11537 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
11538 return;
11539
11540fail:
11541 if (primary)
11542 drm_plane_cleanup(primary);
11543 if (cursor)
11544 drm_plane_cleanup(cursor);
11545 kfree(intel_crtc);
79e53945
JB
11546}
11547
752aa88a
JB
11548enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11549{
11550 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 11551 struct drm_device *dev = connector->base.dev;
752aa88a 11552
51fd371b 11553 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
11554
11555 if (!encoder)
11556 return INVALID_PIPE;
11557
11558 return to_intel_crtc(encoder->crtc)->pipe;
11559}
11560
08d7b3d1 11561int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 11562 struct drm_file *file)
08d7b3d1 11563{
08d7b3d1 11564 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
11565 struct drm_mode_object *drmmode_obj;
11566 struct intel_crtc *crtc;
08d7b3d1 11567
1cff8f6b
DV
11568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11569 return -ENODEV;
08d7b3d1 11570
c05422d5
DV
11571 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11572 DRM_MODE_OBJECT_CRTC);
08d7b3d1 11573
c05422d5 11574 if (!drmmode_obj) {
08d7b3d1 11575 DRM_ERROR("no such CRTC id\n");
3f2c2057 11576 return -ENOENT;
08d7b3d1
CW
11577 }
11578
c05422d5
DV
11579 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11580 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 11581
c05422d5 11582 return 0;
08d7b3d1
CW
11583}
11584
66a9278e 11585static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 11586{
66a9278e
DV
11587 struct drm_device *dev = encoder->base.dev;
11588 struct intel_encoder *source_encoder;
79e53945 11589 int index_mask = 0;
79e53945
JB
11590 int entry = 0;
11591
66a9278e
DV
11592 list_for_each_entry(source_encoder,
11593 &dev->mode_config.encoder_list, base.head) {
bc079e8b 11594 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
11595 index_mask |= (1 << entry);
11596
79e53945
JB
11597 entry++;
11598 }
4ef69c7a 11599
79e53945
JB
11600 return index_mask;
11601}
11602
4d302442
CW
11603static bool has_edp_a(struct drm_device *dev)
11604{
11605 struct drm_i915_private *dev_priv = dev->dev_private;
11606
11607 if (!IS_MOBILE(dev))
11608 return false;
11609
11610 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11611 return false;
11612
e3589908 11613 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
11614 return false;
11615
11616 return true;
11617}
11618
ba0fbca4
DL
11619const char *intel_output_name(int output)
11620{
11621 static const char *names[] = {
11622 [INTEL_OUTPUT_UNUSED] = "Unused",
11623 [INTEL_OUTPUT_ANALOG] = "Analog",
11624 [INTEL_OUTPUT_DVO] = "DVO",
11625 [INTEL_OUTPUT_SDVO] = "SDVO",
11626 [INTEL_OUTPUT_LVDS] = "LVDS",
11627 [INTEL_OUTPUT_TVOUT] = "TV",
11628 [INTEL_OUTPUT_HDMI] = "HDMI",
11629 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11630 [INTEL_OUTPUT_EDP] = "eDP",
11631 [INTEL_OUTPUT_DSI] = "DSI",
11632 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11633 };
11634
11635 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11636 return "Invalid";
11637
11638 return names[output];
11639}
11640
79e53945
JB
11641static void intel_setup_outputs(struct drm_device *dev)
11642{
725e30ad 11643 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 11644 struct intel_encoder *encoder;
cb0953d7 11645 bool dpd_is_edp = false;
79e53945 11646
c9093354 11647 intel_lvds_init(dev);
79e53945 11648
27da3bdf 11649 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
79935fca 11650 intel_crt_init(dev);
cb0953d7 11651
affa9354 11652 if (HAS_DDI(dev)) {
0e72a5b5
ED
11653 int found;
11654
11655 /* Haswell uses DDI functions to detect digital outputs */
11656 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11657 /* DDI A only supports eDP */
11658 if (found)
11659 intel_ddi_init(dev, PORT_A);
11660
11661 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11662 * register */
11663 found = I915_READ(SFUSE_STRAP);
11664
11665 if (found & SFUSE_STRAP_DDIB_DETECTED)
11666 intel_ddi_init(dev, PORT_B);
11667 if (found & SFUSE_STRAP_DDIC_DETECTED)
11668 intel_ddi_init(dev, PORT_C);
11669 if (found & SFUSE_STRAP_DDID_DETECTED)
11670 intel_ddi_init(dev, PORT_D);
11671 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 11672 int found;
5d8a7752 11673 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
11674
11675 if (has_edp_a(dev))
11676 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 11677
dc0fa718 11678 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 11679 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 11680 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 11681 if (!found)
e2debe91 11682 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 11683 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 11684 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
11685 }
11686
dc0fa718 11687 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 11688 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 11689
dc0fa718 11690 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 11691 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 11692
5eb08b69 11693 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 11694 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 11695
270b3042 11696 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 11697 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 11698 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
11699 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11700 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11701 PORT_B);
11702 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11703 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11704 }
11705
6f6005a5
JB
11706 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11707 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11708 PORT_C);
11709 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 11710 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 11711 }
19c03924 11712
9418c1f1
VS
11713 if (IS_CHERRYVIEW(dev)) {
11714 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11715 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11716 PORT_D);
11717 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11718 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11719 }
11720 }
11721
3cfca973 11722 intel_dsi_init(dev);
103a196f 11723 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 11724 bool found = false;
7d57382e 11725
e2debe91 11726 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 11727 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 11728 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
11729 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11730 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 11731 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 11732 }
27185ae1 11733
e7281eab 11734 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 11735 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 11736 }
13520b05
KH
11737
11738 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 11739
e2debe91 11740 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 11741 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 11742 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 11743 }
27185ae1 11744
e2debe91 11745 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 11746
b01f2c3a
JB
11747 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11748 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 11749 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 11750 }
e7281eab 11751 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 11752 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 11753 }
27185ae1 11754
b01f2c3a 11755 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 11756 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 11757 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 11758 } else if (IS_GEN2(dev))
79e53945
JB
11759 intel_dvo_init(dev);
11760
103a196f 11761 if (SUPPORTS_TV(dev))
79e53945
JB
11762 intel_tv_init(dev);
11763
7c8f8a70
RV
11764 intel_edp_psr_init(dev);
11765
4ef69c7a
CW
11766 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11767 encoder->base.possible_crtcs = encoder->crtc_mask;
11768 encoder->base.possible_clones =
66a9278e 11769 intel_encoder_clones(encoder);
79e53945 11770 }
47356eb6 11771
dde86e2d 11772 intel_init_pch_refclk(dev);
270b3042
DV
11773
11774 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
11775}
11776
11777static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11778{
60a5ca01 11779 struct drm_device *dev = fb->dev;
79e53945 11780 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 11781
ef2d633e 11782 drm_framebuffer_cleanup(fb);
60a5ca01 11783 mutex_lock(&dev->struct_mutex);
ef2d633e 11784 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
11785 drm_gem_object_unreference(&intel_fb->obj->base);
11786 mutex_unlock(&dev->struct_mutex);
79e53945
JB
11787 kfree(intel_fb);
11788}
11789
11790static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 11791 struct drm_file *file,
79e53945
JB
11792 unsigned int *handle)
11793{
11794 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 11795 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 11796
05394f39 11797 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
11798}
11799
11800static const struct drm_framebuffer_funcs intel_fb_funcs = {
11801 .destroy = intel_user_framebuffer_destroy,
11802 .create_handle = intel_user_framebuffer_create_handle,
11803};
11804
b5ea642a
DV
11805static int intel_framebuffer_init(struct drm_device *dev,
11806 struct intel_framebuffer *intel_fb,
11807 struct drm_mode_fb_cmd2 *mode_cmd,
11808 struct drm_i915_gem_object *obj)
79e53945 11809{
a57ce0b2 11810 int aligned_height;
a35cdaa0 11811 int pitch_limit;
79e53945
JB
11812 int ret;
11813
dd4916c5
DV
11814 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11815
c16ed4be
CW
11816 if (obj->tiling_mode == I915_TILING_Y) {
11817 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 11818 return -EINVAL;
c16ed4be 11819 }
57cd6508 11820
c16ed4be
CW
11821 if (mode_cmd->pitches[0] & 63) {
11822 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11823 mode_cmd->pitches[0]);
57cd6508 11824 return -EINVAL;
c16ed4be 11825 }
57cd6508 11826
a35cdaa0
CW
11827 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11828 pitch_limit = 32*1024;
11829 } else if (INTEL_INFO(dev)->gen >= 4) {
11830 if (obj->tiling_mode)
11831 pitch_limit = 16*1024;
11832 else
11833 pitch_limit = 32*1024;
11834 } else if (INTEL_INFO(dev)->gen >= 3) {
11835 if (obj->tiling_mode)
11836 pitch_limit = 8*1024;
11837 else
11838 pitch_limit = 16*1024;
11839 } else
11840 /* XXX DSPC is limited to 4k tiled */
11841 pitch_limit = 8*1024;
11842
11843 if (mode_cmd->pitches[0] > pitch_limit) {
11844 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11845 obj->tiling_mode ? "tiled" : "linear",
11846 mode_cmd->pitches[0], pitch_limit);
5d7bd705 11847 return -EINVAL;
c16ed4be 11848 }
5d7bd705
VS
11849
11850 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
11851 mode_cmd->pitches[0] != obj->stride) {
11852 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11853 mode_cmd->pitches[0], obj->stride);
5d7bd705 11854 return -EINVAL;
c16ed4be 11855 }
5d7bd705 11856
57779d06 11857 /* Reject formats not supported by any plane early. */
308e5bcb 11858 switch (mode_cmd->pixel_format) {
57779d06 11859 case DRM_FORMAT_C8:
04b3924d
VS
11860 case DRM_FORMAT_RGB565:
11861 case DRM_FORMAT_XRGB8888:
11862 case DRM_FORMAT_ARGB8888:
57779d06
VS
11863 break;
11864 case DRM_FORMAT_XRGB1555:
11865 case DRM_FORMAT_ARGB1555:
c16ed4be 11866 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
11867 DRM_DEBUG("unsupported pixel format: %s\n",
11868 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11869 return -EINVAL;
c16ed4be 11870 }
57779d06
VS
11871 break;
11872 case DRM_FORMAT_XBGR8888:
11873 case DRM_FORMAT_ABGR8888:
04b3924d
VS
11874 case DRM_FORMAT_XRGB2101010:
11875 case DRM_FORMAT_ARGB2101010:
57779d06
VS
11876 case DRM_FORMAT_XBGR2101010:
11877 case DRM_FORMAT_ABGR2101010:
c16ed4be 11878 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
11879 DRM_DEBUG("unsupported pixel format: %s\n",
11880 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11881 return -EINVAL;
c16ed4be 11882 }
b5626747 11883 break;
04b3924d
VS
11884 case DRM_FORMAT_YUYV:
11885 case DRM_FORMAT_UYVY:
11886 case DRM_FORMAT_YVYU:
11887 case DRM_FORMAT_VYUY:
c16ed4be 11888 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
11889 DRM_DEBUG("unsupported pixel format: %s\n",
11890 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11891 return -EINVAL;
c16ed4be 11892 }
57cd6508
CW
11893 break;
11894 default:
4ee62c76
VS
11895 DRM_DEBUG("unsupported pixel format: %s\n",
11896 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
11897 return -EINVAL;
11898 }
11899
90f9a336
VS
11900 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11901 if (mode_cmd->offsets[0] != 0)
11902 return -EINVAL;
11903
a57ce0b2
JB
11904 aligned_height = intel_align_height(dev, mode_cmd->height,
11905 obj->tiling_mode);
53155c0a
DV
11906 /* FIXME drm helper for size checks (especially planar formats)? */
11907 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11908 return -EINVAL;
11909
c7d73f6a
DV
11910 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11911 intel_fb->obj = obj;
80075d49 11912 intel_fb->obj->framebuffer_references++;
c7d73f6a 11913
79e53945
JB
11914 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11915 if (ret) {
11916 DRM_ERROR("framebuffer init failed %d\n", ret);
11917 return ret;
11918 }
11919
79e53945
JB
11920 return 0;
11921}
11922
79e53945
JB
11923static struct drm_framebuffer *
11924intel_user_framebuffer_create(struct drm_device *dev,
11925 struct drm_file *filp,
308e5bcb 11926 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 11927{
05394f39 11928 struct drm_i915_gem_object *obj;
79e53945 11929
308e5bcb
JB
11930 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11931 mode_cmd->handles[0]));
c8725226 11932 if (&obj->base == NULL)
cce13ff7 11933 return ERR_PTR(-ENOENT);
79e53945 11934
d2dff872 11935 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
11936}
11937
4520f53a 11938#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 11939static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
11940{
11941}
11942#endif
11943
79e53945 11944static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 11945 .fb_create = intel_user_framebuffer_create,
0632fef6 11946 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
11947};
11948
e70236a8
JB
11949/* Set up chip specific display functions */
11950static void intel_init_display(struct drm_device *dev)
11951{
11952 struct drm_i915_private *dev_priv = dev->dev_private;
11953
ee9300bb
DV
11954 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11955 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
11956 else if (IS_CHERRYVIEW(dev))
11957 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
11958 else if (IS_VALLEYVIEW(dev))
11959 dev_priv->display.find_dpll = vlv_find_best_dpll;
11960 else if (IS_PINEVIEW(dev))
11961 dev_priv->display.find_dpll = pnv_find_best_dpll;
11962 else
11963 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11964
affa9354 11965 if (HAS_DDI(dev)) {
0e8ffe1b 11966 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 11967 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 11968 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
11969 dev_priv->display.crtc_enable = haswell_crtc_enable;
11970 dev_priv->display.crtc_disable = haswell_crtc_disable;
6441ab5f 11971 dev_priv->display.off = haswell_crtc_off;
262ca2b0
MR
11972 dev_priv->display.update_primary_plane =
11973 ironlake_update_primary_plane;
09b4ddf9 11974 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 11975 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 11976 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 11977 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
11978 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11979 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 11980 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
11981 dev_priv->display.update_primary_plane =
11982 ironlake_update_primary_plane;
89b667f8
JB
11983 } else if (IS_VALLEYVIEW(dev)) {
11984 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11985 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
11986 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11987 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11988 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11989 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11990 dev_priv->display.update_primary_plane =
11991 i9xx_update_primary_plane;
f564048e 11992 } else {
0e8ffe1b 11993 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11994 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 11995 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
11996 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11997 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 11998 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11999 dev_priv->display.update_primary_plane =
12000 i9xx_update_primary_plane;
f564048e 12001 }
e70236a8 12002
e70236a8 12003 /* Returns the core display clock speed */
25eb05fc
JB
12004 if (IS_VALLEYVIEW(dev))
12005 dev_priv->display.get_display_clock_speed =
12006 valleyview_get_display_clock_speed;
12007 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12008 dev_priv->display.get_display_clock_speed =
12009 i945_get_display_clock_speed;
12010 else if (IS_I915G(dev))
12011 dev_priv->display.get_display_clock_speed =
12012 i915_get_display_clock_speed;
257a7ffc 12013 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12014 dev_priv->display.get_display_clock_speed =
12015 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12016 else if (IS_PINEVIEW(dev))
12017 dev_priv->display.get_display_clock_speed =
12018 pnv_get_display_clock_speed;
e70236a8
JB
12019 else if (IS_I915GM(dev))
12020 dev_priv->display.get_display_clock_speed =
12021 i915gm_get_display_clock_speed;
12022 else if (IS_I865G(dev))
12023 dev_priv->display.get_display_clock_speed =
12024 i865_get_display_clock_speed;
f0f8a9ce 12025 else if (IS_I85X(dev))
e70236a8
JB
12026 dev_priv->display.get_display_clock_speed =
12027 i855_get_display_clock_speed;
12028 else /* 852, 830 */
12029 dev_priv->display.get_display_clock_speed =
12030 i830_get_display_clock_speed;
12031
7f8a8569 12032 if (HAS_PCH_SPLIT(dev)) {
f00a3ddf 12033 if (IS_GEN5(dev)) {
674cf967 12034 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
e0dac65e 12035 dev_priv->display.write_eld = ironlake_write_eld;
1398261a 12036 } else if (IS_GEN6(dev)) {
674cf967 12037 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
e0dac65e 12038 dev_priv->display.write_eld = ironlake_write_eld;
9a952a0d
PZ
12039 dev_priv->display.modeset_global_resources =
12040 snb_modeset_global_resources;
357555c0
JB
12041 } else if (IS_IVYBRIDGE(dev)) {
12042 /* FIXME: detect B0+ stepping and use auto training */
12043 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
e0dac65e 12044 dev_priv->display.write_eld = ironlake_write_eld;
01a415fd
DV
12045 dev_priv->display.modeset_global_resources =
12046 ivb_modeset_global_resources;
4e0bbc31 12047 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
c82e4d26 12048 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
83358c85 12049 dev_priv->display.write_eld = haswell_write_eld;
d6dd9eb1
DV
12050 dev_priv->display.modeset_global_resources =
12051 haswell_modeset_global_resources;
a0e63c22 12052 }
6067aaea 12053 } else if (IS_G4X(dev)) {
e0dac65e 12054 dev_priv->display.write_eld = g4x_write_eld;
30a970c6
JB
12055 } else if (IS_VALLEYVIEW(dev)) {
12056 dev_priv->display.modeset_global_resources =
12057 valleyview_modeset_global_resources;
9ca2fe73 12058 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 12059 }
8c9f3aaf
JB
12060
12061 /* Default just returns -ENODEV to indicate unsupported */
12062 dev_priv->display.queue_flip = intel_default_queue_flip;
12063
12064 switch (INTEL_INFO(dev)->gen) {
12065 case 2:
12066 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12067 break;
12068
12069 case 3:
12070 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12071 break;
12072
12073 case 4:
12074 case 5:
12075 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12076 break;
12077
12078 case 6:
12079 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12080 break;
7c9017e5 12081 case 7:
4e0bbc31 12082 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12083 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12084 break;
8c9f3aaf 12085 }
7bd688cd
JN
12086
12087 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
12088}
12089
b690e96c
JB
12090/*
12091 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12092 * resume, or other times. This quirk makes sure that's the case for
12093 * affected systems.
12094 */
0206e353 12095static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12096{
12097 struct drm_i915_private *dev_priv = dev->dev_private;
12098
12099 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12100 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12101}
12102
435793df
KP
12103/*
12104 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12105 */
12106static void quirk_ssc_force_disable(struct drm_device *dev)
12107{
12108 struct drm_i915_private *dev_priv = dev->dev_private;
12109 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12110 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12111}
12112
4dca20ef 12113/*
5a15ab5b
CE
12114 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12115 * brightness value
4dca20ef
CE
12116 */
12117static void quirk_invert_brightness(struct drm_device *dev)
12118{
12119 struct drm_i915_private *dev_priv = dev->dev_private;
12120 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12121 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12122}
12123
b690e96c
JB
12124struct intel_quirk {
12125 int device;
12126 int subsystem_vendor;
12127 int subsystem_device;
12128 void (*hook)(struct drm_device *dev);
12129};
12130
5f85f176
EE
12131/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12132struct intel_dmi_quirk {
12133 void (*hook)(struct drm_device *dev);
12134 const struct dmi_system_id (*dmi_id_list)[];
12135};
12136
12137static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12138{
12139 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12140 return 1;
12141}
12142
12143static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12144 {
12145 .dmi_id_list = &(const struct dmi_system_id[]) {
12146 {
12147 .callback = intel_dmi_reverse_brightness,
12148 .ident = "NCR Corporation",
12149 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12150 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12151 },
12152 },
12153 { } /* terminating entry */
12154 },
12155 .hook = quirk_invert_brightness,
12156 },
12157};
12158
c43b5634 12159static struct intel_quirk intel_quirks[] = {
b690e96c 12160 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12161 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12162
b690e96c
JB
12163 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12164 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12165
b690e96c
JB
12166 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12167 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12168
435793df
KP
12169 /* Lenovo U160 cannot use SSC on LVDS */
12170 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12171
12172 /* Sony Vaio Y cannot use SSC on LVDS */
12173 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12174
be505f64
AH
12175 /* Acer Aspire 5734Z must invert backlight brightness */
12176 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12177
12178 /* Acer/eMachines G725 */
12179 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12180
12181 /* Acer/eMachines e725 */
12182 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12183
12184 /* Acer/Packard Bell NCL20 */
12185 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12186
12187 /* Acer Aspire 4736Z */
12188 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12189
12190 /* Acer Aspire 5336 */
12191 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
b690e96c
JB
12192};
12193
12194static void intel_init_quirks(struct drm_device *dev)
12195{
12196 struct pci_dev *d = dev->pdev;
12197 int i;
12198
12199 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12200 struct intel_quirk *q = &intel_quirks[i];
12201
12202 if (d->device == q->device &&
12203 (d->subsystem_vendor == q->subsystem_vendor ||
12204 q->subsystem_vendor == PCI_ANY_ID) &&
12205 (d->subsystem_device == q->subsystem_device ||
12206 q->subsystem_device == PCI_ANY_ID))
12207 q->hook(dev);
12208 }
5f85f176
EE
12209 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12210 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12211 intel_dmi_quirks[i].hook(dev);
12212 }
b690e96c
JB
12213}
12214
9cce37f4
JB
12215/* Disable the VGA plane that we never use */
12216static void i915_disable_vga(struct drm_device *dev)
12217{
12218 struct drm_i915_private *dev_priv = dev->dev_private;
12219 u8 sr1;
766aa1c4 12220 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12221
2b37c616 12222 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12223 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12224 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12225 sr1 = inb(VGA_SR_DATA);
12226 outb(sr1 | 1<<5, VGA_SR_DATA);
12227 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12228 udelay(300);
12229
12230 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
12231 POSTING_READ(vga_reg);
12232}
12233
f817586c
DV
12234void intel_modeset_init_hw(struct drm_device *dev)
12235{
a8f78b58
ED
12236 intel_prepare_ddi(dev);
12237
f817586c
DV
12238 intel_init_clock_gating(dev);
12239
5382f5f3 12240 intel_reset_dpio(dev);
40e9cf64 12241
8090c6b9 12242 intel_enable_gt_powersave(dev);
f817586c
DV
12243}
12244
7d708ee4
ID
12245void intel_modeset_suspend_hw(struct drm_device *dev)
12246{
12247 intel_suspend_hw(dev);
12248}
12249
79e53945
JB
12250void intel_modeset_init(struct drm_device *dev)
12251{
652c393a 12252 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12253 int sprite, ret;
8cc87b75 12254 enum pipe pipe;
46f297fb 12255 struct intel_crtc *crtc;
79e53945
JB
12256
12257 drm_mode_config_init(dev);
12258
12259 dev->mode_config.min_width = 0;
12260 dev->mode_config.min_height = 0;
12261
019d96cb
DA
12262 dev->mode_config.preferred_depth = 24;
12263 dev->mode_config.prefer_shadow = 1;
12264
e6ecefaa 12265 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12266
b690e96c
JB
12267 intel_init_quirks(dev);
12268
1fa61106
ED
12269 intel_init_pm(dev);
12270
e3c74757
BW
12271 if (INTEL_INFO(dev)->num_pipes == 0)
12272 return;
12273
e70236a8
JB
12274 intel_init_display(dev);
12275
a6c45cf0
CW
12276 if (IS_GEN2(dev)) {
12277 dev->mode_config.max_width = 2048;
12278 dev->mode_config.max_height = 2048;
12279 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12280 dev->mode_config.max_width = 4096;
12281 dev->mode_config.max_height = 4096;
79e53945 12282 } else {
a6c45cf0
CW
12283 dev->mode_config.max_width = 8192;
12284 dev->mode_config.max_height = 8192;
79e53945 12285 }
068be561
DL
12286
12287 if (IS_GEN2(dev)) {
12288 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12289 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12290 } else {
12291 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12292 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12293 }
12294
5d4545ae 12295 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12296
28c97730 12297 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12298 INTEL_INFO(dev)->num_pipes,
12299 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12300
8cc87b75
DL
12301 for_each_pipe(pipe) {
12302 intel_crtc_init(dev, pipe);
1fe47785
DL
12303 for_each_sprite(pipe, sprite) {
12304 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12305 if (ret)
06da8da2 12306 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12307 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12308 }
79e53945
JB
12309 }
12310
f42bb70d 12311 intel_init_dpio(dev);
5382f5f3 12312 intel_reset_dpio(dev);
f42bb70d 12313
79f689aa 12314 intel_cpu_pll_init(dev);
e72f9fbf 12315 intel_shared_dpll_init(dev);
ee7b9f93 12316
9cce37f4
JB
12317 /* Just disable it once at startup */
12318 i915_disable_vga(dev);
79e53945 12319 intel_setup_outputs(dev);
11be49eb
CW
12320
12321 /* Just in case the BIOS is doing something questionable. */
12322 intel_disable_fbc(dev);
fa9fa083 12323
6e9f798d 12324 drm_modeset_lock_all(dev);
fa9fa083 12325 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12326 drm_modeset_unlock_all(dev);
46f297fb 12327
d3fcc808 12328 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12329 if (!crtc->active)
12330 continue;
12331
46f297fb 12332 /*
46f297fb
JB
12333 * Note that reserving the BIOS fb up front prevents us
12334 * from stuffing other stolen allocations like the ring
12335 * on top. This prevents some ugliness at boot time, and
12336 * can even allow for smooth boot transitions if the BIOS
12337 * fb is large enough for the active pipe configuration.
12338 */
12339 if (dev_priv->display.get_plane_config) {
12340 dev_priv->display.get_plane_config(crtc,
12341 &crtc->plane_config);
12342 /*
12343 * If the fb is shared between multiple heads, we'll
12344 * just get the first one.
12345 */
484b41dd 12346 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12347 }
46f297fb 12348 }
2c7111db
CW
12349}
12350
7fad798e
DV
12351static void intel_enable_pipe_a(struct drm_device *dev)
12352{
12353 struct intel_connector *connector;
12354 struct drm_connector *crt = NULL;
12355 struct intel_load_detect_pipe load_detect_temp;
51fd371b 12356 struct drm_modeset_acquire_ctx ctx;
7fad798e
DV
12357
12358 /* We can't just switch on the pipe A, we need to set things up with a
12359 * proper mode and output configuration. As a gross hack, enable pipe A
12360 * by enabling the load detect pipe once. */
12361 list_for_each_entry(connector,
12362 &dev->mode_config.connector_list,
12363 base.head) {
12364 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12365 crt = &connector->base;
12366 break;
12367 }
12368 }
12369
12370 if (!crt)
12371 return;
12372
51fd371b
RC
12373 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
12374 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
7fad798e 12375
652c393a 12376
7fad798e
DV
12377}
12378
fa555837
DV
12379static bool
12380intel_check_plane_mapping(struct intel_crtc *crtc)
12381{
7eb552ae
BW
12382 struct drm_device *dev = crtc->base.dev;
12383 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12384 u32 reg, val;
12385
7eb552ae 12386 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
12387 return true;
12388
12389 reg = DSPCNTR(!crtc->plane);
12390 val = I915_READ(reg);
12391
12392 if ((val & DISPLAY_PLANE_ENABLE) &&
12393 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12394 return false;
12395
12396 return true;
12397}
12398
24929352
DV
12399static void intel_sanitize_crtc(struct intel_crtc *crtc)
12400{
12401 struct drm_device *dev = crtc->base.dev;
12402 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 12403 u32 reg;
24929352 12404
24929352 12405 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 12406 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
12407 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12408
d3eaf884
VS
12409 /* restore vblank interrupts to correct state */
12410 if (crtc->active)
12411 drm_vblank_on(dev, crtc->pipe);
12412 else
12413 drm_vblank_off(dev, crtc->pipe);
12414
24929352 12415 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
12416 * disable the crtc (and hence change the state) if it is wrong. Note
12417 * that gen4+ has a fixed plane -> pipe mapping. */
12418 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
12419 struct intel_connector *connector;
12420 bool plane;
12421
24929352
DV
12422 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12423 crtc->base.base.id);
12424
12425 /* Pipe has the wrong plane attached and the plane is active.
12426 * Temporarily change the plane mapping and disable everything
12427 * ... */
12428 plane = crtc->plane;
12429 crtc->plane = !plane;
12430 dev_priv->display.crtc_disable(&crtc->base);
12431 crtc->plane = plane;
12432
12433 /* ... and break all links. */
12434 list_for_each_entry(connector, &dev->mode_config.connector_list,
12435 base.head) {
12436 if (connector->encoder->base.crtc != &crtc->base)
12437 continue;
12438
7f1950fb
EE
12439 connector->base.dpms = DRM_MODE_DPMS_OFF;
12440 connector->base.encoder = NULL;
24929352 12441 }
7f1950fb
EE
12442 /* multiple connectors may have the same encoder:
12443 * handle them and break crtc link separately */
12444 list_for_each_entry(connector, &dev->mode_config.connector_list,
12445 base.head)
12446 if (connector->encoder->base.crtc == &crtc->base) {
12447 connector->encoder->base.crtc = NULL;
12448 connector->encoder->connectors_active = false;
12449 }
24929352
DV
12450
12451 WARN_ON(crtc->active);
12452 crtc->base.enabled = false;
12453 }
24929352 12454
7fad798e
DV
12455 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12456 crtc->pipe == PIPE_A && !crtc->active) {
12457 /* BIOS forgot to enable pipe A, this mostly happens after
12458 * resume. Force-enable the pipe to fix this, the update_dpms
12459 * call below we restore the pipe to the right state, but leave
12460 * the required bits on. */
12461 intel_enable_pipe_a(dev);
12462 }
12463
24929352
DV
12464 /* Adjust the state of the output pipe according to whether we
12465 * have active connectors/encoders. */
12466 intel_crtc_update_dpms(&crtc->base);
12467
12468 if (crtc->active != crtc->base.enabled) {
12469 struct intel_encoder *encoder;
12470
12471 /* This can happen either due to bugs in the get_hw_state
12472 * functions or because the pipe is force-enabled due to the
12473 * pipe A quirk. */
12474 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12475 crtc->base.base.id,
12476 crtc->base.enabled ? "enabled" : "disabled",
12477 crtc->active ? "enabled" : "disabled");
12478
12479 crtc->base.enabled = crtc->active;
12480
12481 /* Because we only establish the connector -> encoder ->
12482 * crtc links if something is active, this means the
12483 * crtc is now deactivated. Break the links. connector
12484 * -> encoder links are only establish when things are
12485 * actually up, hence no need to break them. */
12486 WARN_ON(crtc->active);
12487
12488 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12489 WARN_ON(encoder->connectors_active);
12490 encoder->base.crtc = NULL;
12491 }
12492 }
c5ab3bc0
DV
12493
12494 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
4cc31489
DV
12495 /*
12496 * We start out with underrun reporting disabled to avoid races.
12497 * For correct bookkeeping mark this on active crtcs.
12498 *
c5ab3bc0
DV
12499 * Also on gmch platforms we dont have any hardware bits to
12500 * disable the underrun reporting. Which means we need to start
12501 * out with underrun reporting disabled also on inactive pipes,
12502 * since otherwise we'll complain about the garbage we read when
12503 * e.g. coming up after runtime pm.
12504 *
4cc31489
DV
12505 * No protection against concurrent access is required - at
12506 * worst a fifo underrun happens which also sets this to false.
12507 */
12508 crtc->cpu_fifo_underrun_disabled = true;
12509 crtc->pch_fifo_underrun_disabled = true;
80715b2f
VS
12510
12511 update_scanline_offset(crtc);
4cc31489 12512 }
24929352
DV
12513}
12514
12515static void intel_sanitize_encoder(struct intel_encoder *encoder)
12516{
12517 struct intel_connector *connector;
12518 struct drm_device *dev = encoder->base.dev;
12519
12520 /* We need to check both for a crtc link (meaning that the
12521 * encoder is active and trying to read from a pipe) and the
12522 * pipe itself being active. */
12523 bool has_active_crtc = encoder->base.crtc &&
12524 to_intel_crtc(encoder->base.crtc)->active;
12525
12526 if (encoder->connectors_active && !has_active_crtc) {
12527 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12528 encoder->base.base.id,
8e329a03 12529 encoder->base.name);
24929352
DV
12530
12531 /* Connector is active, but has no active pipe. This is
12532 * fallout from our resume register restoring. Disable
12533 * the encoder manually again. */
12534 if (encoder->base.crtc) {
12535 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12536 encoder->base.base.id,
8e329a03 12537 encoder->base.name);
24929352
DV
12538 encoder->disable(encoder);
12539 }
7f1950fb
EE
12540 encoder->base.crtc = NULL;
12541 encoder->connectors_active = false;
24929352
DV
12542
12543 /* Inconsistent output/port/pipe state happens presumably due to
12544 * a bug in one of the get_hw_state functions. Or someplace else
12545 * in our code, like the register restore mess on resume. Clamp
12546 * things to off as a safer default. */
12547 list_for_each_entry(connector,
12548 &dev->mode_config.connector_list,
12549 base.head) {
12550 if (connector->encoder != encoder)
12551 continue;
7f1950fb
EE
12552 connector->base.dpms = DRM_MODE_DPMS_OFF;
12553 connector->base.encoder = NULL;
24929352
DV
12554 }
12555 }
12556 /* Enabled encoders without active connectors will be fixed in
12557 * the crtc fixup. */
12558}
12559
04098753 12560void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
12561{
12562 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 12563 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 12564
04098753
ID
12565 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12566 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12567 i915_disable_vga(dev);
12568 }
12569}
12570
12571void i915_redisable_vga(struct drm_device *dev)
12572{
12573 struct drm_i915_private *dev_priv = dev->dev_private;
12574
8dc8a27c
PZ
12575 /* This function can be called both from intel_modeset_setup_hw_state or
12576 * at a very early point in our resume sequence, where the power well
12577 * structures are not yet restored. Since this function is at a very
12578 * paranoid "someone might have enabled VGA while we were not looking"
12579 * level, just check if the power well is enabled instead of trying to
12580 * follow the "don't touch the power well if we don't need it" policy
12581 * the rest of the driver uses. */
04098753 12582 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
12583 return;
12584
04098753 12585 i915_redisable_vga_power_on(dev);
0fde901f
KM
12586}
12587
98ec7739
VS
12588static bool primary_get_hw_state(struct intel_crtc *crtc)
12589{
12590 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12591
12592 if (!crtc->active)
12593 return false;
12594
12595 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12596}
12597
30e984df 12598static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
12599{
12600 struct drm_i915_private *dev_priv = dev->dev_private;
12601 enum pipe pipe;
24929352
DV
12602 struct intel_crtc *crtc;
12603 struct intel_encoder *encoder;
12604 struct intel_connector *connector;
5358901f 12605 int i;
24929352 12606
d3fcc808 12607 for_each_intel_crtc(dev, crtc) {
88adfff1 12608 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 12609
9953599b
DV
12610 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12611
0e8ffe1b
DV
12612 crtc->active = dev_priv->display.get_pipe_config(crtc,
12613 &crtc->config);
24929352
DV
12614
12615 crtc->base.enabled = crtc->active;
98ec7739 12616 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
12617
12618 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12619 crtc->base.base.id,
12620 crtc->active ? "enabled" : "disabled");
12621 }
12622
5358901f 12623 /* FIXME: Smash this into the new shared dpll infrastructure. */
affa9354 12624 if (HAS_DDI(dev))
6441ab5f
PZ
12625 intel_ddi_setup_hw_pll_state(dev);
12626
5358901f
DV
12627 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12628 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12629
12630 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12631 pll->active = 0;
d3fcc808 12632 for_each_intel_crtc(dev, crtc) {
5358901f
DV
12633 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12634 pll->active++;
12635 }
12636 pll->refcount = pll->active;
12637
35c95375
DV
12638 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12639 pll->name, pll->refcount, pll->on);
5358901f
DV
12640 }
12641
24929352
DV
12642 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12643 base.head) {
12644 pipe = 0;
12645
12646 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
12647 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12648 encoder->base.crtc = &crtc->base;
1d37b689 12649 encoder->get_config(encoder, &crtc->config);
24929352
DV
12650 } else {
12651 encoder->base.crtc = NULL;
12652 }
12653
12654 encoder->connectors_active = false;
6f2bcceb 12655 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 12656 encoder->base.base.id,
8e329a03 12657 encoder->base.name,
24929352 12658 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 12659 pipe_name(pipe));
24929352
DV
12660 }
12661
12662 list_for_each_entry(connector, &dev->mode_config.connector_list,
12663 base.head) {
12664 if (connector->get_hw_state(connector)) {
12665 connector->base.dpms = DRM_MODE_DPMS_ON;
12666 connector->encoder->connectors_active = true;
12667 connector->base.encoder = &connector->encoder->base;
12668 } else {
12669 connector->base.dpms = DRM_MODE_DPMS_OFF;
12670 connector->base.encoder = NULL;
12671 }
12672 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12673 connector->base.base.id,
c23cc417 12674 connector->base.name,
24929352
DV
12675 connector->base.encoder ? "enabled" : "disabled");
12676 }
30e984df
DV
12677}
12678
12679/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12680 * and i915 state tracking structures. */
12681void intel_modeset_setup_hw_state(struct drm_device *dev,
12682 bool force_restore)
12683{
12684 struct drm_i915_private *dev_priv = dev->dev_private;
12685 enum pipe pipe;
30e984df
DV
12686 struct intel_crtc *crtc;
12687 struct intel_encoder *encoder;
35c95375 12688 int i;
30e984df
DV
12689
12690 intel_modeset_readout_hw_state(dev);
24929352 12691
babea61d
JB
12692 /*
12693 * Now that we have the config, copy it to each CRTC struct
12694 * Note that this could go away if we move to using crtc_config
12695 * checking everywhere.
12696 */
d3fcc808 12697 for_each_intel_crtc(dev, crtc) {
d330a953 12698 if (crtc->active && i915.fastboot) {
f6a83288 12699 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
12700 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12701 crtc->base.base.id);
12702 drm_mode_debug_printmodeline(&crtc->base.mode);
12703 }
12704 }
12705
24929352
DV
12706 /* HW state is read out, now we need to sanitize this mess. */
12707 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12708 base.head) {
12709 intel_sanitize_encoder(encoder);
12710 }
12711
12712 for_each_pipe(pipe) {
12713 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12714 intel_sanitize_crtc(crtc);
c0b03411 12715 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 12716 }
9a935856 12717
35c95375
DV
12718 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12719 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12720
12721 if (!pll->on || pll->active)
12722 continue;
12723
12724 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12725
12726 pll->disable(dev_priv, pll);
12727 pll->on = false;
12728 }
12729
96f90c54 12730 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
12731 ilk_wm_get_hw_state(dev);
12732
45e2b5f6 12733 if (force_restore) {
7d0bc1ea
VS
12734 i915_redisable_vga(dev);
12735
f30da187
DV
12736 /*
12737 * We need to use raw interfaces for restoring state to avoid
12738 * checking (bogus) intermediate states.
12739 */
45e2b5f6 12740 for_each_pipe(pipe) {
b5644d05
JB
12741 struct drm_crtc *crtc =
12742 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
12743
12744 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 12745 crtc->primary->fb);
45e2b5f6
DV
12746 }
12747 } else {
12748 intel_modeset_update_staged_output_state(dev);
12749 }
8af6cf88
DV
12750
12751 intel_modeset_check_state(dev);
2c7111db
CW
12752}
12753
12754void intel_modeset_gem_init(struct drm_device *dev)
12755{
484b41dd
JB
12756 struct drm_crtc *c;
12757 struct intel_framebuffer *fb;
12758
ae48434c
ID
12759 mutex_lock(&dev->struct_mutex);
12760 intel_init_gt_powersave(dev);
12761 mutex_unlock(&dev->struct_mutex);
12762
1833b134 12763 intel_modeset_init_hw(dev);
02e792fb
DV
12764
12765 intel_setup_overlay(dev);
484b41dd
JB
12766
12767 /*
12768 * Make sure any fbs we allocated at startup are properly
12769 * pinned & fenced. When we do the allocation it's too early
12770 * for this.
12771 */
12772 mutex_lock(&dev->struct_mutex);
70e1e0ec 12773 for_each_crtc(dev, c) {
66e514c1 12774 if (!c->primary->fb)
484b41dd
JB
12775 continue;
12776
66e514c1 12777 fb = to_intel_framebuffer(c->primary->fb);
484b41dd
JB
12778 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12779 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12780 to_intel_crtc(c)->pipe);
66e514c1
DA
12781 drm_framebuffer_unreference(c->primary->fb);
12782 c->primary->fb = NULL;
484b41dd
JB
12783 }
12784 }
12785 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12786}
12787
4932e2c3
ID
12788void intel_connector_unregister(struct intel_connector *intel_connector)
12789{
12790 struct drm_connector *connector = &intel_connector->base;
12791
12792 intel_panel_destroy_backlight(connector);
12793 drm_sysfs_connector_remove(connector);
12794}
12795
79e53945
JB
12796void intel_modeset_cleanup(struct drm_device *dev)
12797{
652c393a
JB
12798 struct drm_i915_private *dev_priv = dev->dev_private;
12799 struct drm_crtc *crtc;
d9255d57 12800 struct drm_connector *connector;
652c393a 12801
fd0c0642
DV
12802 /*
12803 * Interrupts and polling as the first thing to avoid creating havoc.
12804 * Too much stuff here (turning of rps, connectors, ...) would
12805 * experience fancy races otherwise.
12806 */
12807 drm_irq_uninstall(dev);
12808 cancel_work_sync(&dev_priv->hotplug_work);
12809 /*
12810 * Due to the hpd irq storm handling the hotplug work can re-arm the
12811 * poll handlers. Hence disable polling after hpd handling is shut down.
12812 */
f87ea761 12813 drm_kms_helper_poll_fini(dev);
fd0c0642 12814
652c393a
JB
12815 mutex_lock(&dev->struct_mutex);
12816
723bfd70
JB
12817 intel_unregister_dsm_handler();
12818
70e1e0ec 12819 for_each_crtc(dev, crtc) {
652c393a 12820 /* Skip inactive CRTCs */
f4510a27 12821 if (!crtc->primary->fb)
652c393a
JB
12822 continue;
12823
3dec0095 12824 intel_increase_pllclock(crtc);
652c393a
JB
12825 }
12826
973d04f9 12827 intel_disable_fbc(dev);
e70236a8 12828
8090c6b9 12829 intel_disable_gt_powersave(dev);
0cdab21f 12830
930ebb46
DV
12831 ironlake_teardown_rc6(dev);
12832
69341a5e
KH
12833 mutex_unlock(&dev->struct_mutex);
12834
1630fe75
CW
12835 /* flush any delayed tasks or pending work */
12836 flush_scheduled_work();
12837
db31af1d
JN
12838 /* destroy the backlight and sysfs files before encoders/connectors */
12839 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
12840 struct intel_connector *intel_connector;
12841
12842 intel_connector = to_intel_connector(connector);
12843 intel_connector->unregister(intel_connector);
db31af1d 12844 }
d9255d57 12845
79e53945 12846 drm_mode_config_cleanup(dev);
4d7bb011
DV
12847
12848 intel_cleanup_overlay(dev);
ae48434c
ID
12849
12850 mutex_lock(&dev->struct_mutex);
12851 intel_cleanup_gt_powersave(dev);
12852 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12853}
12854
f1c79df3
ZW
12855/*
12856 * Return which encoder is currently attached for connector.
12857 */
df0e9248 12858struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 12859{
df0e9248
CW
12860 return &intel_attached_encoder(connector)->base;
12861}
f1c79df3 12862
df0e9248
CW
12863void intel_connector_attach_encoder(struct intel_connector *connector,
12864 struct intel_encoder *encoder)
12865{
12866 connector->encoder = encoder;
12867 drm_mode_connector_attach_encoder(&connector->base,
12868 &encoder->base);
79e53945 12869}
28d52043
DA
12870
12871/*
12872 * set vga decode state - true == enable VGA decode
12873 */
12874int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12875{
12876 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 12877 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
12878 u16 gmch_ctrl;
12879
75fa041d
CW
12880 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12881 DRM_ERROR("failed to read control word\n");
12882 return -EIO;
12883 }
12884
c0cc8a55
CW
12885 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12886 return 0;
12887
28d52043
DA
12888 if (state)
12889 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12890 else
12891 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
12892
12893 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12894 DRM_ERROR("failed to write control word\n");
12895 return -EIO;
12896 }
12897
28d52043
DA
12898 return 0;
12899}
c4a1d9e4 12900
c4a1d9e4 12901struct intel_display_error_state {
ff57f1b0
PZ
12902
12903 u32 power_well_driver;
12904
63b66e5b
CW
12905 int num_transcoders;
12906
c4a1d9e4
CW
12907 struct intel_cursor_error_state {
12908 u32 control;
12909 u32 position;
12910 u32 base;
12911 u32 size;
52331309 12912 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
12913
12914 struct intel_pipe_error_state {
ddf9c536 12915 bool power_domain_on;
c4a1d9e4 12916 u32 source;
f301b1e1 12917 u32 stat;
52331309 12918 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
12919
12920 struct intel_plane_error_state {
12921 u32 control;
12922 u32 stride;
12923 u32 size;
12924 u32 pos;
12925 u32 addr;
12926 u32 surface;
12927 u32 tile_offset;
52331309 12928 } plane[I915_MAX_PIPES];
63b66e5b
CW
12929
12930 struct intel_transcoder_error_state {
ddf9c536 12931 bool power_domain_on;
63b66e5b
CW
12932 enum transcoder cpu_transcoder;
12933
12934 u32 conf;
12935
12936 u32 htotal;
12937 u32 hblank;
12938 u32 hsync;
12939 u32 vtotal;
12940 u32 vblank;
12941 u32 vsync;
12942 } transcoder[4];
c4a1d9e4
CW
12943};
12944
12945struct intel_display_error_state *
12946intel_display_capture_error_state(struct drm_device *dev)
12947{
fbee40df 12948 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 12949 struct intel_display_error_state *error;
63b66e5b
CW
12950 int transcoders[] = {
12951 TRANSCODER_A,
12952 TRANSCODER_B,
12953 TRANSCODER_C,
12954 TRANSCODER_EDP,
12955 };
c4a1d9e4
CW
12956 int i;
12957
63b66e5b
CW
12958 if (INTEL_INFO(dev)->num_pipes == 0)
12959 return NULL;
12960
9d1cb914 12961 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
12962 if (error == NULL)
12963 return NULL;
12964
190be112 12965 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
12966 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12967
52331309 12968 for_each_pipe(i) {
ddf9c536 12969 error->pipe[i].power_domain_on =
da7e29bd
ID
12970 intel_display_power_enabled_sw(dev_priv,
12971 POWER_DOMAIN_PIPE(i));
ddf9c536 12972 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
12973 continue;
12974
5efb3e28
VS
12975 error->cursor[i].control = I915_READ(CURCNTR(i));
12976 error->cursor[i].position = I915_READ(CURPOS(i));
12977 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
12978
12979 error->plane[i].control = I915_READ(DSPCNTR(i));
12980 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 12981 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 12982 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
12983 error->plane[i].pos = I915_READ(DSPPOS(i));
12984 }
ca291363
PZ
12985 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12986 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
12987 if (INTEL_INFO(dev)->gen >= 4) {
12988 error->plane[i].surface = I915_READ(DSPSURF(i));
12989 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12990 }
12991
c4a1d9e4 12992 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1
ID
12993
12994 if (!HAS_PCH_SPLIT(dev))
12995 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
12996 }
12997
12998 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12999 if (HAS_DDI(dev_priv->dev))
13000 error->num_transcoders++; /* Account for eDP. */
13001
13002 for (i = 0; i < error->num_transcoders; i++) {
13003 enum transcoder cpu_transcoder = transcoders[i];
13004
ddf9c536 13005 error->transcoder[i].power_domain_on =
da7e29bd 13006 intel_display_power_enabled_sw(dev_priv,
38cc1daf 13007 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13008 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13009 continue;
13010
63b66e5b
CW
13011 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13012
13013 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13014 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13015 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13016 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13017 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13018 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13019 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13020 }
13021
13022 return error;
13023}
13024
edc3d884
MK
13025#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13026
c4a1d9e4 13027void
edc3d884 13028intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13029 struct drm_device *dev,
13030 struct intel_display_error_state *error)
13031{
13032 int i;
13033
63b66e5b
CW
13034 if (!error)
13035 return;
13036
edc3d884 13037 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13038 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13039 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13040 error->power_well_driver);
52331309 13041 for_each_pipe(i) {
edc3d884 13042 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13043 err_printf(m, " Power: %s\n",
13044 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13045 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13046 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13047
13048 err_printf(m, "Plane [%d]:\n", i);
13049 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13050 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13051 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13052 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13053 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13054 }
4b71a570 13055 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13056 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13057 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13058 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13059 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13060 }
13061
edc3d884
MK
13062 err_printf(m, "Cursor [%d]:\n", i);
13063 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13064 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13065 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13066 }
63b66e5b
CW
13067
13068 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13069 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13070 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13071 err_printf(m, " Power: %s\n",
13072 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13073 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13074 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13075 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13076 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13077 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13078 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13079 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13080 }
c4a1d9e4 13081}
This page took 2.16478 seconds and 5 git commands to generate.