drm/i915: Idle unused rings on gen2/3 during init/resume
[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
cc36513c
DV
79static void intel_increase_pllclock(struct drm_device *dev,
80 enum pipe pipe);
6b383a7f 81static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 82
f1f644dc
JB
83static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
84 struct intel_crtc_config *pipe_config);
18442d08
VS
85static void ironlake_pch_clock_get(struct intel_crtc *crtc,
86 struct intel_crtc_config *pipe_config);
f1f644dc 87
e7457a9a
DL
88static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
89 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 102static void vlv_prepare_pll(struct intel_crtc *crtc);
1ae0d137 103static void chv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 104
0e32b39c
DA
105static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
106{
107 if (!connector->mst_port)
108 return connector->encoder;
109 else
110 return &connector->mst_port->mst_encoders[pipe]->base;
111}
112
79e53945 113typedef struct {
0206e353 114 int min, max;
79e53945
JB
115} intel_range_t;
116
117typedef struct {
0206e353
AJ
118 int dot_limit;
119 int p2_slow, p2_fast;
79e53945
JB
120} intel_p2_t;
121
d4906093
ML
122typedef struct intel_limit intel_limit_t;
123struct intel_limit {
0206e353
AJ
124 intel_range_t dot, vco, n, m, m1, m2, p, p1;
125 intel_p2_t p2;
d4906093 126};
79e53945 127
d2acd215
DV
128int
129intel_pch_rawclk(struct drm_device *dev)
130{
131 struct drm_i915_private *dev_priv = dev->dev_private;
132
133 WARN_ON(!HAS_PCH_SPLIT(dev));
134
135 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
136}
137
021357ac
CW
138static inline u32 /* units of 100MHz */
139intel_fdi_link_freq(struct drm_device *dev)
140{
8b99e68c
CW
141 if (IS_GEN5(dev)) {
142 struct drm_i915_private *dev_priv = dev->dev_private;
143 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
144 } else
145 return 27;
021357ac
CW
146}
147
5d536e28 148static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 149 .dot = { .min = 25000, .max = 350000 },
9c333719 150 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 151 .n = { .min = 2, .max = 16 },
0206e353
AJ
152 .m = { .min = 96, .max = 140 },
153 .m1 = { .min = 18, .max = 26 },
154 .m2 = { .min = 6, .max = 16 },
155 .p = { .min = 4, .max = 128 },
156 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
157 .p2 = { .dot_limit = 165000,
158 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
159};
160
5d536e28
DV
161static const intel_limit_t intel_limits_i8xx_dvo = {
162 .dot = { .min = 25000, .max = 350000 },
9c333719 163 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 164 .n = { .min = 2, .max = 16 },
5d536e28
DV
165 .m = { .min = 96, .max = 140 },
166 .m1 = { .min = 18, .max = 26 },
167 .m2 = { .min = 6, .max = 16 },
168 .p = { .min = 4, .max = 128 },
169 .p1 = { .min = 2, .max = 33 },
170 .p2 = { .dot_limit = 165000,
171 .p2_slow = 4, .p2_fast = 4 },
172};
173
e4b36699 174static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 175 .dot = { .min = 25000, .max = 350000 },
9c333719 176 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 177 .n = { .min = 2, .max = 16 },
0206e353
AJ
178 .m = { .min = 96, .max = 140 },
179 .m1 = { .min = 18, .max = 26 },
180 .m2 = { .min = 6, .max = 16 },
181 .p = { .min = 4, .max = 128 },
182 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
183 .p2 = { .dot_limit = 165000,
184 .p2_slow = 14, .p2_fast = 7 },
e4b36699 185};
273e27ca 186
e4b36699 187static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
188 .dot = { .min = 20000, .max = 400000 },
189 .vco = { .min = 1400000, .max = 2800000 },
190 .n = { .min = 1, .max = 6 },
191 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
192 .m1 = { .min = 8, .max = 18 },
193 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
194 .p = { .min = 5, .max = 80 },
195 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
196 .p2 = { .dot_limit = 200000,
197 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
198};
199
200static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
201 .dot = { .min = 20000, .max = 400000 },
202 .vco = { .min = 1400000, .max = 2800000 },
203 .n = { .min = 1, .max = 6 },
204 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
205 .m1 = { .min = 8, .max = 18 },
206 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
207 .p = { .min = 7, .max = 98 },
208 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
209 .p2 = { .dot_limit = 112000,
210 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
211};
212
273e27ca 213
e4b36699 214static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
215 .dot = { .min = 25000, .max = 270000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 10, .max = 30 },
222 .p1 = { .min = 1, .max = 3},
223 .p2 = { .dot_limit = 270000,
224 .p2_slow = 10,
225 .p2_fast = 10
044c7c41 226 },
e4b36699
KP
227};
228
229static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
230 .dot = { .min = 22000, .max = 400000 },
231 .vco = { .min = 1750000, .max = 3500000},
232 .n = { .min = 1, .max = 4 },
233 .m = { .min = 104, .max = 138 },
234 .m1 = { .min = 16, .max = 23 },
235 .m2 = { .min = 5, .max = 11 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8},
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
240};
241
242static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
243 .dot = { .min = 20000, .max = 115000 },
244 .vco = { .min = 1750000, .max = 3500000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 104, .max = 138 },
247 .m1 = { .min = 17, .max = 23 },
248 .m2 = { .min = 5, .max = 11 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 0,
252 .p2_slow = 14, .p2_fast = 14
044c7c41 253 },
e4b36699
KP
254};
255
256static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
257 .dot = { .min = 80000, .max = 224000 },
258 .vco = { .min = 1750000, .max = 3500000 },
259 .n = { .min = 1, .max = 3 },
260 .m = { .min = 104, .max = 138 },
261 .m1 = { .min = 17, .max = 23 },
262 .m2 = { .min = 5, .max = 11 },
263 .p = { .min = 14, .max = 42 },
264 .p1 = { .min = 2, .max = 6 },
265 .p2 = { .dot_limit = 0,
266 .p2_slow = 7, .p2_fast = 7
044c7c41 267 },
e4b36699
KP
268};
269
f2b115e6 270static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
271 .dot = { .min = 20000, .max = 400000},
272 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 273 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
273e27ca 276 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
277 .m1 = { .min = 0, .max = 0 },
278 .m2 = { .min = 0, .max = 254 },
279 .p = { .min = 5, .max = 80 },
280 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
281 .p2 = { .dot_limit = 200000,
282 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
283};
284
f2b115e6 285static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
286 .dot = { .min = 20000, .max = 400000 },
287 .vco = { .min = 1700000, .max = 3500000 },
288 .n = { .min = 3, .max = 6 },
289 .m = { .min = 2, .max = 256 },
290 .m1 = { .min = 0, .max = 0 },
291 .m2 = { .min = 0, .max = 254 },
292 .p = { .min = 7, .max = 112 },
293 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
294 .p2 = { .dot_limit = 112000,
295 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
296};
297
273e27ca
EA
298/* Ironlake / Sandybridge
299 *
300 * We calculate clock using (register_value + 2) for N/M1/M2, so here
301 * the range value for them is (actual_value - 2).
302 */
b91ad0ec 303static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
304 .dot = { .min = 25000, .max = 350000 },
305 .vco = { .min = 1760000, .max = 3510000 },
306 .n = { .min = 1, .max = 5 },
307 .m = { .min = 79, .max = 127 },
308 .m1 = { .min = 12, .max = 22 },
309 .m2 = { .min = 5, .max = 9 },
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
312 .p2 = { .dot_limit = 225000,
313 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
314};
315
b91ad0ec 316static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
317 .dot = { .min = 25000, .max = 350000 },
318 .vco = { .min = 1760000, .max = 3510000 },
319 .n = { .min = 1, .max = 3 },
320 .m = { .min = 79, .max = 118 },
321 .m1 = { .min = 12, .max = 22 },
322 .m2 = { .min = 5, .max = 9 },
323 .p = { .min = 28, .max = 112 },
324 .p1 = { .min = 2, .max = 8 },
325 .p2 = { .dot_limit = 225000,
326 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
327};
328
329static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 350000 },
331 .vco = { .min = 1760000, .max = 3510000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 79, .max = 127 },
334 .m1 = { .min = 12, .max = 22 },
335 .m2 = { .min = 5, .max = 9 },
336 .p = { .min = 14, .max = 56 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 225000,
339 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
340};
341
273e27ca 342/* LVDS 100mhz refclk limits. */
b91ad0ec 343static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
344 .dot = { .min = 25000, .max = 350000 },
345 .vco = { .min = 1760000, .max = 3510000 },
346 .n = { .min = 1, .max = 2 },
347 .m = { .min = 79, .max = 126 },
348 .m1 = { .min = 12, .max = 22 },
349 .m2 = { .min = 5, .max = 9 },
350 .p = { .min = 28, .max = 112 },
0206e353 351 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
352 .p2 = { .dot_limit = 225000,
353 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
354};
355
356static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
357 .dot = { .min = 25000, .max = 350000 },
358 .vco = { .min = 1760000, .max = 3510000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 79, .max = 126 },
361 .m1 = { .min = 12, .max = 22 },
362 .m2 = { .min = 5, .max = 9 },
363 .p = { .min = 14, .max = 42 },
0206e353 364 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
365 .p2 = { .dot_limit = 225000,
366 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
367};
368
dc730512 369static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
370 /*
371 * These are the data rate limits (measured in fast clocks)
372 * since those are the strictest limits we have. The fast
373 * clock and actual rate limits are more relaxed, so checking
374 * them would make no difference.
375 */
376 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 377 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 378 .n = { .min = 1, .max = 7 },
a0c4da24
JB
379 .m1 = { .min = 2, .max = 3 },
380 .m2 = { .min = 11, .max = 156 },
b99ab663 381 .p1 = { .min = 2, .max = 3 },
5fdc9c49 382 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
383};
384
ef9348c8
CML
385static const intel_limit_t intel_limits_chv = {
386 /*
387 * These are the data rate limits (measured in fast clocks)
388 * since those are the strictest limits we have. The fast
389 * clock and actual rate limits are more relaxed, so checking
390 * them would make no difference.
391 */
392 .dot = { .min = 25000 * 5, .max = 540000 * 5},
393 .vco = { .min = 4860000, .max = 6700000 },
394 .n = { .min = 1, .max = 1 },
395 .m1 = { .min = 2, .max = 2 },
396 .m2 = { .min = 24 << 22, .max = 175 << 22 },
397 .p1 = { .min = 2, .max = 4 },
398 .p2 = { .p2_slow = 1, .p2_fast = 14 },
399};
400
6b4bf1c4
VS
401static void vlv_clock(int refclk, intel_clock_t *clock)
402{
403 clock->m = clock->m1 * clock->m2;
404 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
405 if (WARN_ON(clock->n == 0 || clock->p == 0))
406 return;
fb03ac01
VS
407 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
408 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
409}
410
e0638cdf
PZ
411/**
412 * Returns whether any output on the specified pipe is of the specified type
413 */
414static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
415{
416 struct drm_device *dev = crtc->dev;
417 struct intel_encoder *encoder;
418
419 for_each_encoder_on_crtc(dev, crtc, encoder)
420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
1b894b59
CW
426static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
427 int refclk)
2c07245f 428{
b91ad0ec 429 struct drm_device *dev = crtc->dev;
2c07245f 430 const intel_limit_t *limit;
b91ad0ec
ZW
431
432 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 433 if (intel_is_dual_link_lvds(dev)) {
1b894b59 434 if (refclk == 100000)
b91ad0ec
ZW
435 limit = &intel_limits_ironlake_dual_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_dual_lvds;
438 } else {
1b894b59 439 if (refclk == 100000)
b91ad0ec
ZW
440 limit = &intel_limits_ironlake_single_lvds_100m;
441 else
442 limit = &intel_limits_ironlake_single_lvds;
443 }
c6bb3538 444 } else
b91ad0ec 445 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
446
447 return limit;
448}
449
044c7c41
ML
450static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
451{
452 struct drm_device *dev = crtc->dev;
044c7c41
ML
453 const intel_limit_t *limit;
454
455 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 456 if (intel_is_dual_link_lvds(dev))
e4b36699 457 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 458 else
e4b36699 459 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
461 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 462 limit = &intel_limits_g4x_hdmi;
044c7c41 463 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 464 limit = &intel_limits_g4x_sdvo;
044c7c41 465 } else /* The option is for other outputs */
e4b36699 466 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
467
468 return limit;
469}
470
1b894b59 471static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
472{
473 struct drm_device *dev = crtc->dev;
474 const intel_limit_t *limit;
475
bad720ff 476 if (HAS_PCH_SPLIT(dev))
1b894b59 477 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 478 else if (IS_G4X(dev)) {
044c7c41 479 limit = intel_g4x_limit(crtc);
f2b115e6 480 } else if (IS_PINEVIEW(dev)) {
2177832f 481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 482 limit = &intel_limits_pineview_lvds;
2177832f 483 else
f2b115e6 484 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
485 } else if (IS_CHERRYVIEW(dev)) {
486 limit = &intel_limits_chv;
a0c4da24 487 } else if (IS_VALLEYVIEW(dev)) {
dc730512 488 limit = &intel_limits_vlv;
a6c45cf0
CW
489 } else if (!IS_GEN2(dev)) {
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
491 limit = &intel_limits_i9xx_lvds;
492 else
493 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
494 } else {
495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 496 limit = &intel_limits_i8xx_lvds;
5d536e28 497 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 498 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
499 else
500 limit = &intel_limits_i8xx_dac;
79e53945
JB
501 }
502 return limit;
503}
504
f2b115e6
AJ
505/* m1 is reserved as 0 in Pineview, n is a ring counter */
506static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 507{
2177832f
SL
508 clock->m = clock->m2 + 2;
509 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
510 if (WARN_ON(clock->n == 0 || clock->p == 0))
511 return;
fb03ac01
VS
512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
514}
515
7429e9d4
DV
516static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
517{
518 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
519}
520
ac58c3f0 521static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 522{
7429e9d4 523 clock->m = i9xx_dpll_compute_m(clock);
79e53945 524 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
525 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
526 return;
fb03ac01
VS
527 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
528 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
529}
530
ef9348c8
CML
531static void chv_clock(int refclk, intel_clock_t *clock)
532{
533 clock->m = clock->m1 * clock->m2;
534 clock->p = clock->p1 * clock->p2;
535 if (WARN_ON(clock->n == 0 || clock->p == 0))
536 return;
537 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
538 clock->n << 22);
539 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
540}
541
7c04d1d9 542#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
543/**
544 * Returns whether the given set of divisors are valid for a given refclk with
545 * the given connectors.
546 */
547
1b894b59
CW
548static bool intel_PLL_is_valid(struct drm_device *dev,
549 const intel_limit_t *limit,
550 const intel_clock_t *clock)
79e53945 551{
f01b7962
VS
552 if (clock->n < limit->n.min || limit->n.max < clock->n)
553 INTELPllInvalid("n out of range\n");
79e53945 554 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 555 INTELPllInvalid("p1 out of range\n");
79e53945 556 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 557 INTELPllInvalid("m2 out of range\n");
79e53945 558 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 559 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
560
561 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
562 if (clock->m1 <= clock->m2)
563 INTELPllInvalid("m1 <= m2\n");
564
565 if (!IS_VALLEYVIEW(dev)) {
566 if (clock->p < limit->p.min || limit->p.max < clock->p)
567 INTELPllInvalid("p out of range\n");
568 if (clock->m < limit->m.min || limit->m.max < clock->m)
569 INTELPllInvalid("m out of range\n");
570 }
571
79e53945 572 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 573 INTELPllInvalid("vco out of range\n");
79e53945
JB
574 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
575 * connector, etc., rather than just a single range.
576 */
577 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 578 INTELPllInvalid("dot out of range\n");
79e53945
JB
579
580 return true;
581}
582
d4906093 583static bool
ee9300bb 584i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
585 int target, int refclk, intel_clock_t *match_clock,
586 intel_clock_t *best_clock)
79e53945
JB
587{
588 struct drm_device *dev = crtc->dev;
79e53945 589 intel_clock_t clock;
79e53945
JB
590 int err = target;
591
a210b028 592 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 593 /*
a210b028
DV
594 * For LVDS just rely on its current settings for dual-channel.
595 * We haven't figured out how to reliably set up different
596 * single/dual channel state, if we even can.
79e53945 597 */
1974cad0 598 if (intel_is_dual_link_lvds(dev))
79e53945
JB
599 clock.p2 = limit->p2.p2_fast;
600 else
601 clock.p2 = limit->p2.p2_slow;
602 } else {
603 if (target < limit->p2.dot_limit)
604 clock.p2 = limit->p2.p2_slow;
605 else
606 clock.p2 = limit->p2.p2_fast;
607 }
608
0206e353 609 memset(best_clock, 0, sizeof(*best_clock));
79e53945 610
42158660
ZY
611 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
612 clock.m1++) {
613 for (clock.m2 = limit->m2.min;
614 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 615 if (clock.m2 >= clock.m1)
42158660
ZY
616 break;
617 for (clock.n = limit->n.min;
618 clock.n <= limit->n.max; clock.n++) {
619 for (clock.p1 = limit->p1.min;
620 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
621 int this_err;
622
ac58c3f0
DV
623 i9xx_clock(refclk, &clock);
624 if (!intel_PLL_is_valid(dev, limit,
625 &clock))
626 continue;
627 if (match_clock &&
628 clock.p != match_clock->p)
629 continue;
630
631 this_err = abs(clock.dot - target);
632 if (this_err < err) {
633 *best_clock = clock;
634 err = this_err;
635 }
636 }
637 }
638 }
639 }
640
641 return (err != target);
642}
643
644static bool
ee9300bb
DV
645pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
646 int target, int refclk, intel_clock_t *match_clock,
647 intel_clock_t *best_clock)
79e53945
JB
648{
649 struct drm_device *dev = crtc->dev;
79e53945 650 intel_clock_t clock;
79e53945
JB
651 int err = target;
652
a210b028 653 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 654 /*
a210b028
DV
655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
79e53945 658 */
1974cad0 659 if (intel_is_dual_link_lvds(dev))
79e53945
JB
660 clock.p2 = limit->p2.p2_fast;
661 else
662 clock.p2 = limit->p2.p2_slow;
663 } else {
664 if (target < limit->p2.dot_limit)
665 clock.p2 = limit->p2.p2_slow;
666 else
667 clock.p2 = limit->p2.p2_fast;
668 }
669
0206e353 670 memset(best_clock, 0, sizeof(*best_clock));
79e53945 671
42158660
ZY
672 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
673 clock.m1++) {
674 for (clock.m2 = limit->m2.min;
675 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
676 for (clock.n = limit->n.min;
677 clock.n <= limit->n.max; clock.n++) {
678 for (clock.p1 = limit->p1.min;
679 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
680 int this_err;
681
ac58c3f0 682 pineview_clock(refclk, &clock);
1b894b59
CW
683 if (!intel_PLL_is_valid(dev, limit,
684 &clock))
79e53945 685 continue;
cec2f356
SP
686 if (match_clock &&
687 clock.p != match_clock->p)
688 continue;
79e53945
JB
689
690 this_err = abs(clock.dot - target);
691 if (this_err < err) {
692 *best_clock = clock;
693 err = this_err;
694 }
695 }
696 }
697 }
698 }
699
700 return (err != target);
701}
702
d4906093 703static bool
ee9300bb
DV
704g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
d4906093
ML
707{
708 struct drm_device *dev = crtc->dev;
d4906093
ML
709 intel_clock_t clock;
710 int max_n;
711 bool found;
6ba770dc
AJ
712 /* approximately equals target * 0.00585 */
713 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
714 found = false;
715
716 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 717 if (intel_is_dual_link_lvds(dev))
d4906093
ML
718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
728 memset(best_clock, 0, sizeof(*best_clock));
729 max_n = limit->n.max;
f77f13e2 730 /* based on hardware requirement, prefer smaller n to precision */
d4906093 731 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 732 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
733 for (clock.m1 = limit->m1.max;
734 clock.m1 >= limit->m1.min; clock.m1--) {
735 for (clock.m2 = limit->m2.max;
736 clock.m2 >= limit->m2.min; clock.m2--) {
737 for (clock.p1 = limit->p1.max;
738 clock.p1 >= limit->p1.min; clock.p1--) {
739 int this_err;
740
ac58c3f0 741 i9xx_clock(refclk, &clock);
1b894b59
CW
742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
d4906093 744 continue;
1b894b59
CW
745
746 this_err = abs(clock.dot - target);
d4906093
ML
747 if (this_err < err_most) {
748 *best_clock = clock;
749 err_most = this_err;
750 max_n = clock.n;
751 found = true;
752 }
753 }
754 }
755 }
756 }
2c07245f
ZW
757 return found;
758}
759
a0c4da24 760static bool
ee9300bb
DV
761vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
762 int target, int refclk, intel_clock_t *match_clock,
763 intel_clock_t *best_clock)
a0c4da24 764{
f01b7962 765 struct drm_device *dev = crtc->dev;
6b4bf1c4 766 intel_clock_t clock;
69e4f900 767 unsigned int bestppm = 1000000;
27e639bf
VS
768 /* min update 19.2 MHz */
769 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 770 bool found = false;
a0c4da24 771
6b4bf1c4
VS
772 target *= 5; /* fast clock */
773
774 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
775
776 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 777 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 778 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 779 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 780 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 781 clock.p = clock.p1 * clock.p2;
a0c4da24 782 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 783 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
784 unsigned int ppm, diff;
785
6b4bf1c4
VS
786 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
787 refclk * clock.m1);
788
789 vlv_clock(refclk, &clock);
43b0ac53 790
f01b7962
VS
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
43b0ac53
VS
793 continue;
794
6b4bf1c4
VS
795 diff = abs(clock.dot - target);
796 ppm = div_u64(1000000ULL * diff, target);
797
798 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 799 bestppm = 0;
6b4bf1c4 800 *best_clock = clock;
49e497ef 801 found = true;
43b0ac53 802 }
6b4bf1c4 803
c686122c 804 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 805 bestppm = ppm;
6b4bf1c4 806 *best_clock = clock;
49e497ef 807 found = true;
a0c4da24
JB
808 }
809 }
810 }
811 }
812 }
a0c4da24 813
49e497ef 814 return found;
a0c4da24 815}
a4fc5ed6 816
ef9348c8
CML
817static bool
818chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
819 int target, int refclk, intel_clock_t *match_clock,
820 intel_clock_t *best_clock)
821{
822 struct drm_device *dev = crtc->dev;
823 intel_clock_t clock;
824 uint64_t m2;
825 int found = false;
826
827 memset(best_clock, 0, sizeof(*best_clock));
828
829 /*
830 * Based on hardware doc, the n always set to 1, and m1 always
831 * set to 2. If requires to support 200Mhz refclk, we need to
832 * revisit this because n may not 1 anymore.
833 */
834 clock.n = 1, clock.m1 = 2;
835 target *= 5; /* fast clock */
836
837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
838 for (clock.p2 = limit->p2.p2_fast;
839 clock.p2 >= limit->p2.p2_slow;
840 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
841
842 clock.p = clock.p1 * clock.p2;
843
844 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
845 clock.n) << 22, refclk * clock.m1);
846
847 if (m2 > INT_MAX/clock.m1)
848 continue;
849
850 clock.m2 = m2;
851
852 chv_clock(refclk, &clock);
853
854 if (!intel_PLL_is_valid(dev, limit, &clock))
855 continue;
856
857 /* based on hardware requirement, prefer bigger p
858 */
859 if (clock.p > best_clock->p) {
860 *best_clock = clock;
861 found = true;
862 }
863 }
864 }
865
866 return found;
867}
868
20ddf665
VS
869bool intel_crtc_active(struct drm_crtc *crtc)
870{
871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
872
873 /* Be paranoid as we can arrive here with only partial
874 * state retrieved from the hardware during setup.
875 *
241bfc38 876 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
877 * as Haswell has gained clock readout/fastboot support.
878 *
66e514c1 879 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
880 * properly reconstruct framebuffers.
881 */
f4510a27 882 return intel_crtc->active && crtc->primary->fb &&
241bfc38 883 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
884}
885
a5c961d1
PZ
886enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
887 enum pipe pipe)
888{
889 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
891
3b117c8f 892 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
893}
894
57e22f4a 895static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 898 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
899
900 frame = I915_READ(frame_reg);
901
902 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
31e4b89a
DL
903 WARN(1, "vblank wait on pipe %c timed out\n",
904 pipe_name(pipe));
a928d536
PZ
905}
906
9d0498a2
JB
907/**
908 * intel_wait_for_vblank - wait for vblank on a given pipe
909 * @dev: drm device
910 * @pipe: pipe to wait for
911 *
912 * Wait for vblank to occur on a given pipe. Needed for various bits of
913 * mode setting code.
914 */
915void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 916{
9d0498a2 917 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 918 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 919
57e22f4a
VS
920 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
921 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
922 return;
923 }
924
300387c0
CW
925 /* Clear existing vblank status. Note this will clear any other
926 * sticky status fields as well.
927 *
928 * This races with i915_driver_irq_handler() with the result
929 * that either function could miss a vblank event. Here it is not
930 * fatal, as we will either wait upon the next vblank interrupt or
931 * timeout. Generally speaking intel_wait_for_vblank() is only
932 * called during modeset at which time the GPU should be idle and
933 * should *not* be performing page flips and thus not waiting on
934 * vblanks...
935 * Currently, the result of us stealing a vblank from the irq
936 * handler is that a single frame will be skipped during swapbuffers.
937 */
938 I915_WRITE(pipestat_reg,
939 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
940
9d0498a2 941 /* Wait for vblank interrupt bit to set */
481b6af3
CW
942 if (wait_for(I915_READ(pipestat_reg) &
943 PIPE_VBLANK_INTERRUPT_STATUS,
944 50))
31e4b89a
DL
945 DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
946 pipe_name(pipe));
9d0498a2
JB
947}
948
fbf49ea2
VS
949static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
950{
951 struct drm_i915_private *dev_priv = dev->dev_private;
952 u32 reg = PIPEDSL(pipe);
953 u32 line1, line2;
954 u32 line_mask;
955
956 if (IS_GEN2(dev))
957 line_mask = DSL_LINEMASK_GEN2;
958 else
959 line_mask = DSL_LINEMASK_GEN3;
960
961 line1 = I915_READ(reg) & line_mask;
962 mdelay(5);
963 line2 = I915_READ(reg) & line_mask;
964
965 return line1 == line2;
966}
967
ab7ad7f6
KP
968/*
969 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
970 * @dev: drm device
971 * @pipe: pipe to wait for
972 *
973 * After disabling a pipe, we can't wait for vblank in the usual way,
974 * spinning on the vblank interrupt status bit, since we won't actually
975 * see an interrupt when the pipe is disabled.
976 *
ab7ad7f6
KP
977 * On Gen4 and above:
978 * wait for the pipe register state bit to turn off
979 *
980 * Otherwise:
981 * wait for the display line value to settle (it usually
982 * ends up stopping at the start of the next frame).
58e10eb9 983 *
9d0498a2 984 */
58e10eb9 985void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
986{
987 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
988 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
989 pipe);
ab7ad7f6
KP
990
991 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 992 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
993
994 /* Wait for the Pipe State to go off */
58e10eb9
CW
995 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
996 100))
284637d9 997 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 998 } else {
ab7ad7f6 999 /* Wait for the display line to settle */
fbf49ea2 1000 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1001 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1002 }
79e53945
JB
1003}
1004
b0ea7d37
DL
1005/*
1006 * ibx_digital_port_connected - is the specified port connected?
1007 * @dev_priv: i915 private structure
1008 * @port: the port to test
1009 *
1010 * Returns true if @port is connected, false otherwise.
1011 */
1012bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1013 struct intel_digital_port *port)
1014{
1015 u32 bit;
1016
c36346e3 1017 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1018 switch (port->port) {
c36346e3
DL
1019 case PORT_B:
1020 bit = SDE_PORTB_HOTPLUG;
1021 break;
1022 case PORT_C:
1023 bit = SDE_PORTC_HOTPLUG;
1024 break;
1025 case PORT_D:
1026 bit = SDE_PORTD_HOTPLUG;
1027 break;
1028 default:
1029 return true;
1030 }
1031 } else {
eba905b2 1032 switch (port->port) {
c36346e3
DL
1033 case PORT_B:
1034 bit = SDE_PORTB_HOTPLUG_CPT;
1035 break;
1036 case PORT_C:
1037 bit = SDE_PORTC_HOTPLUG_CPT;
1038 break;
1039 case PORT_D:
1040 bit = SDE_PORTD_HOTPLUG_CPT;
1041 break;
1042 default:
1043 return true;
1044 }
b0ea7d37
DL
1045 }
1046
1047 return I915_READ(SDEISR) & bit;
1048}
1049
b24e7179
JB
1050static const char *state_string(bool enabled)
1051{
1052 return enabled ? "on" : "off";
1053}
1054
1055/* Only for pre-ILK configs */
55607e8a
DV
1056void assert_pll(struct drm_i915_private *dev_priv,
1057 enum pipe pipe, bool state)
b24e7179
JB
1058{
1059 int reg;
1060 u32 val;
1061 bool cur_state;
1062
1063 reg = DPLL(pipe);
1064 val = I915_READ(reg);
1065 cur_state = !!(val & DPLL_VCO_ENABLE);
1066 WARN(cur_state != state,
1067 "PLL state assertion failure (expected %s, current %s)\n",
1068 state_string(state), state_string(cur_state));
1069}
b24e7179 1070
23538ef1
JN
1071/* XXX: the dsi pll is shared between MIPI DSI ports */
1072static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1073{
1074 u32 val;
1075 bool cur_state;
1076
1077 mutex_lock(&dev_priv->dpio_lock);
1078 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1079 mutex_unlock(&dev_priv->dpio_lock);
1080
1081 cur_state = val & DSI_PLL_VCO_EN;
1082 WARN(cur_state != state,
1083 "DSI PLL state assertion failure (expected %s, current %s)\n",
1084 state_string(state), state_string(cur_state));
1085}
1086#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1087#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1088
55607e8a 1089struct intel_shared_dpll *
e2b78267
DV
1090intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1091{
1092 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1093
a43f6e0f 1094 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1095 return NULL;
1096
a43f6e0f 1097 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1098}
1099
040484af 1100/* For ILK+ */
55607e8a
DV
1101void assert_shared_dpll(struct drm_i915_private *dev_priv,
1102 struct intel_shared_dpll *pll,
1103 bool state)
040484af 1104{
040484af 1105 bool cur_state;
5358901f 1106 struct intel_dpll_hw_state hw_state;
040484af 1107
92b27b08 1108 if (WARN (!pll,
46edb027 1109 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1110 return;
ee7b9f93 1111
5358901f 1112 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1113 WARN(cur_state != state,
5358901f
DV
1114 "%s assertion failure (expected %s, current %s)\n",
1115 pll->name, state_string(state), state_string(cur_state));
040484af 1116}
040484af
JB
1117
1118static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
ad80a810
PZ
1124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1125 pipe);
040484af 1126
affa9354
PZ
1127 if (HAS_DDI(dev_priv->dev)) {
1128 /* DDI does not have a specific FDI_TX register */
ad80a810 1129 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1130 val = I915_READ(reg);
ad80a810 1131 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1132 } else {
1133 reg = FDI_TX_CTL(pipe);
1134 val = I915_READ(reg);
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
040484af
JB
1137 WARN(cur_state != state,
1138 "FDI TX state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
1147 int reg;
1148 u32 val;
1149 bool cur_state;
1150
d63fa0dc
PZ
1151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
1153 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1154 WARN(cur_state != state,
1155 "FDI RX state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
1164 int reg;
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
3d13ef2e 1168 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1169 return;
1170
bf507ef7 1171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1172 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1173 return;
1174
040484af
JB
1175 reg = FDI_TX_CTL(pipe);
1176 val = I915_READ(reg);
1177 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1178}
1179
55607e8a
DV
1180void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
040484af
JB
1182{
1183 int reg;
1184 u32 val;
55607e8a 1185 bool cur_state;
040484af
JB
1186
1187 reg = FDI_RX_CTL(pipe);
1188 val = I915_READ(reg);
55607e8a
DV
1189 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1190 WARN(cur_state != state,
1191 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1192 state_string(state), state_string(cur_state));
040484af
JB
1193}
1194
ea0760cf
JB
1195static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1196 enum pipe pipe)
1197{
bedd4dba
JN
1198 struct drm_device *dev = dev_priv->dev;
1199 int pp_reg;
ea0760cf
JB
1200 u32 val;
1201 enum pipe panel_pipe = PIPE_A;
0de3b485 1202 bool locked = true;
ea0760cf 1203
bedd4dba
JN
1204 if (WARN_ON(HAS_DDI(dev)))
1205 return;
1206
1207 if (HAS_PCH_SPLIT(dev)) {
1208 u32 port_sel;
1209
ea0760cf 1210 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1211 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1212
1213 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1214 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1215 panel_pipe = PIPE_B;
1216 /* XXX: else fix for eDP */
1217 } else if (IS_VALLEYVIEW(dev)) {
1218 /* presumably write lock depends on pipe, not port select */
1219 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1220 panel_pipe = pipe;
ea0760cf
JB
1221 } else {
1222 pp_reg = PP_CONTROL;
bedd4dba
JN
1223 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1224 panel_pipe = PIPE_B;
ea0760cf
JB
1225 }
1226
1227 val = I915_READ(pp_reg);
1228 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1229 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1230 locked = false;
1231
ea0760cf
JB
1232 WARN(panel_pipe == pipe && locked,
1233 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1234 pipe_name(pipe));
ea0760cf
JB
1235}
1236
93ce0ba6
JN
1237static void assert_cursor(struct drm_i915_private *dev_priv,
1238 enum pipe pipe, bool state)
1239{
1240 struct drm_device *dev = dev_priv->dev;
1241 bool cur_state;
1242
d9d82081 1243 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1244 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1245 else
5efb3e28 1246 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1247
1248 WARN(cur_state != state,
1249 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1250 pipe_name(pipe), state_string(state), state_string(cur_state));
1251}
1252#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1253#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1254
b840d907
JB
1255void assert_pipe(struct drm_i915_private *dev_priv,
1256 enum pipe pipe, bool state)
b24e7179
JB
1257{
1258 int reg;
1259 u32 val;
63d7bbe9 1260 bool cur_state;
702e7a56
PZ
1261 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1262 pipe);
b24e7179 1263
8e636784
DV
1264 /* if we need the pipe A quirk it must be always on */
1265 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1266 state = true;
1267
da7e29bd 1268 if (!intel_display_power_enabled(dev_priv,
b97186f0 1269 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1270 cur_state = false;
1271 } else {
1272 reg = PIPECONF(cpu_transcoder);
1273 val = I915_READ(reg);
1274 cur_state = !!(val & PIPECONF_ENABLE);
1275 }
1276
63d7bbe9
JB
1277 WARN(cur_state != state,
1278 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1279 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1280}
1281
931872fc
CW
1282static void assert_plane(struct drm_i915_private *dev_priv,
1283 enum plane plane, bool state)
b24e7179
JB
1284{
1285 int reg;
1286 u32 val;
931872fc 1287 bool cur_state;
b24e7179
JB
1288
1289 reg = DSPCNTR(plane);
1290 val = I915_READ(reg);
931872fc
CW
1291 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1292 WARN(cur_state != state,
1293 "plane %c assertion failure (expected %s, current %s)\n",
1294 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1295}
1296
931872fc
CW
1297#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1298#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1299
b24e7179
JB
1300static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1301 enum pipe pipe)
1302{
653e1026 1303 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1304 int reg, i;
1305 u32 val;
1306 int cur_pipe;
1307
653e1026
VS
1308 /* Primary planes are fixed to pipes on gen4+ */
1309 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1310 reg = DSPCNTR(pipe);
1311 val = I915_READ(reg);
83f26f16 1312 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1313 "plane %c assertion failure, should be disabled but not\n",
1314 plane_name(pipe));
19ec1358 1315 return;
28c05794 1316 }
19ec1358 1317
b24e7179 1318 /* Need to check both planes against the pipe */
055e393f 1319 for_each_pipe(dev_priv, i) {
b24e7179
JB
1320 reg = DSPCNTR(i);
1321 val = I915_READ(reg);
1322 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1323 DISPPLANE_SEL_PIPE_SHIFT;
1324 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1325 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1326 plane_name(i), pipe_name(pipe));
b24e7179
JB
1327 }
1328}
1329
19332d7a
JB
1330static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1331 enum pipe pipe)
1332{
20674eef 1333 struct drm_device *dev = dev_priv->dev;
1fe47785 1334 int reg, sprite;
19332d7a
JB
1335 u32 val;
1336
20674eef 1337 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1338 for_each_sprite(pipe, sprite) {
1339 reg = SPCNTR(pipe, sprite);
20674eef 1340 val = I915_READ(reg);
83f26f16 1341 WARN(val & SP_ENABLE,
20674eef 1342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1343 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1344 }
1345 } else if (INTEL_INFO(dev)->gen >= 7) {
1346 reg = SPRCTL(pipe);
19332d7a 1347 val = I915_READ(reg);
83f26f16 1348 WARN(val & SPRITE_ENABLE,
06da8da2 1349 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1350 plane_name(pipe), pipe_name(pipe));
1351 } else if (INTEL_INFO(dev)->gen >= 5) {
1352 reg = DVSCNTR(pipe);
19332d7a 1353 val = I915_READ(reg);
83f26f16 1354 WARN(val & DVS_ENABLE,
06da8da2 1355 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1356 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1357 }
1358}
1359
89eff4be 1360static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1361{
1362 u32 val;
1363 bool enabled;
1364
89eff4be 1365 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1366
92f2584a
JB
1367 val = I915_READ(PCH_DREF_CONTROL);
1368 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1369 DREF_SUPERSPREAD_SOURCE_MASK));
1370 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1371}
1372
ab9412ba
DV
1373static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1374 enum pipe pipe)
92f2584a
JB
1375{
1376 int reg;
1377 u32 val;
1378 bool enabled;
1379
ab9412ba 1380 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1381 val = I915_READ(reg);
1382 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1383 WARN(enabled,
1384 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1385 pipe_name(pipe));
92f2584a
JB
1386}
1387
4e634389
KP
1388static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1390{
1391 if ((val & DP_PORT_EN) == 0)
1392 return false;
1393
1394 if (HAS_PCH_CPT(dev_priv->dev)) {
1395 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1396 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1397 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1398 return false;
44f37d1f
CML
1399 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1400 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1401 return false;
f0575e92
KP
1402 } else {
1403 if ((val & DP_PIPE_MASK) != (pipe << 30))
1404 return false;
1405 }
1406 return true;
1407}
1408
1519b995
KP
1409static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1410 enum pipe pipe, u32 val)
1411{
dc0fa718 1412 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1413 return false;
1414
1415 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1416 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1417 return false;
44f37d1f
CML
1418 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1419 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1420 return false;
1519b995 1421 } else {
dc0fa718 1422 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1423 return false;
1424 }
1425 return true;
1426}
1427
1428static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1429 enum pipe pipe, u32 val)
1430{
1431 if ((val & LVDS_PORT_EN) == 0)
1432 return false;
1433
1434 if (HAS_PCH_CPT(dev_priv->dev)) {
1435 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1436 return false;
1437 } else {
1438 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1439 return false;
1440 }
1441 return true;
1442}
1443
1444static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1445 enum pipe pipe, u32 val)
1446{
1447 if ((val & ADPA_DAC_ENABLE) == 0)
1448 return false;
1449 if (HAS_PCH_CPT(dev_priv->dev)) {
1450 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1451 return false;
1452 } else {
1453 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1454 return false;
1455 }
1456 return true;
1457}
1458
291906f1 1459static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1460 enum pipe pipe, int reg, u32 port_sel)
291906f1 1461{
47a05eca 1462 u32 val = I915_READ(reg);
4e634389 1463 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1464 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1465 reg, pipe_name(pipe));
de9a35ab 1466
75c5da27
DV
1467 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1468 && (val & DP_PIPEB_SELECT),
de9a35ab 1469 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1470}
1471
1472static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1473 enum pipe pipe, int reg)
1474{
47a05eca 1475 u32 val = I915_READ(reg);
b70ad586 1476 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1477 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1478 reg, pipe_name(pipe));
de9a35ab 1479
dc0fa718 1480 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1481 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1482 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1483}
1484
1485static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1486 enum pipe pipe)
1487{
1488 int reg;
1489 u32 val;
291906f1 1490
f0575e92
KP
1491 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1492 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1493 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1494
1495 reg = PCH_ADPA;
1496 val = I915_READ(reg);
b70ad586 1497 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1498 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1499 pipe_name(pipe));
291906f1
JB
1500
1501 reg = PCH_LVDS;
1502 val = I915_READ(reg);
b70ad586 1503 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1504 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1505 pipe_name(pipe));
291906f1 1506
e2debe91
PZ
1507 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1508 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1509 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1510}
1511
40e9cf64
JB
1512static void intel_init_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
a09caddd
CML
1519 /*
1520 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1521 * CHV x1 PHY (DP/HDMI D)
1522 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1523 */
1524 if (IS_CHERRYVIEW(dev)) {
1525 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1526 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1527 } else {
1528 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1529 }
5382f5f3
JB
1530}
1531
426115cf 1532static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1533{
426115cf
DV
1534 struct drm_device *dev = crtc->base.dev;
1535 struct drm_i915_private *dev_priv = dev->dev_private;
1536 int reg = DPLL(crtc->pipe);
1537 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1538
426115cf 1539 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1540
1541 /* No really, not for ILK+ */
1542 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1543
1544 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1545 if (IS_MOBILE(dev_priv->dev))
426115cf 1546 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1547
426115cf
DV
1548 I915_WRITE(reg, dpll);
1549 POSTING_READ(reg);
1550 udelay(150);
1551
1552 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1553 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1554
1555 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1556 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1557
1558 /* We do this three times for luck */
426115cf 1559 I915_WRITE(reg, dpll);
87442f73
DV
1560 POSTING_READ(reg);
1561 udelay(150); /* wait for warmup */
426115cf 1562 I915_WRITE(reg, dpll);
87442f73
DV
1563 POSTING_READ(reg);
1564 udelay(150); /* wait for warmup */
426115cf 1565 I915_WRITE(reg, dpll);
87442f73
DV
1566 POSTING_READ(reg);
1567 udelay(150); /* wait for warmup */
1568}
1569
9d556c99
CML
1570static void chv_enable_pll(struct intel_crtc *crtc)
1571{
1572 struct drm_device *dev = crtc->base.dev;
1573 struct drm_i915_private *dev_priv = dev->dev_private;
1574 int pipe = crtc->pipe;
1575 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1576 u32 tmp;
1577
1578 assert_pipe_disabled(dev_priv, crtc->pipe);
1579
1580 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1581
1582 mutex_lock(&dev_priv->dpio_lock);
1583
1584 /* Enable back the 10bit clock to display controller */
1585 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1586 tmp |= DPIO_DCLKP_EN;
1587 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1588
1589 /*
1590 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1591 */
1592 udelay(1);
1593
1594 /* Enable PLL */
a11b0703 1595 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1596
1597 /* Check PLL is locked */
a11b0703 1598 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1599 DRM_ERROR("PLL %d failed to lock\n", pipe);
1600
a11b0703
VS
1601 /* not sure when this should be written */
1602 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1603 POSTING_READ(DPLL_MD(pipe));
1604
9d556c99
CML
1605 mutex_unlock(&dev_priv->dpio_lock);
1606}
1607
66e3d5c0 1608static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1609{
66e3d5c0
DV
1610 struct drm_device *dev = crtc->base.dev;
1611 struct drm_i915_private *dev_priv = dev->dev_private;
1612 int reg = DPLL(crtc->pipe);
1613 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1614
66e3d5c0 1615 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1616
63d7bbe9 1617 /* No really, not for ILK+ */
3d13ef2e 1618 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1619
1620 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1621 if (IS_MOBILE(dev) && !IS_I830(dev))
1622 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1623
66e3d5c0
DV
1624 I915_WRITE(reg, dpll);
1625
1626 /* Wait for the clocks to stabilize. */
1627 POSTING_READ(reg);
1628 udelay(150);
1629
1630 if (INTEL_INFO(dev)->gen >= 4) {
1631 I915_WRITE(DPLL_MD(crtc->pipe),
1632 crtc->config.dpll_hw_state.dpll_md);
1633 } else {
1634 /* The pixel multiplier can only be updated once the
1635 * DPLL is enabled and the clocks are stable.
1636 *
1637 * So write it again.
1638 */
1639 I915_WRITE(reg, dpll);
1640 }
63d7bbe9
JB
1641
1642 /* We do this three times for luck */
66e3d5c0 1643 I915_WRITE(reg, dpll);
63d7bbe9
JB
1644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
66e3d5c0 1646 I915_WRITE(reg, dpll);
63d7bbe9
JB
1647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
66e3d5c0 1649 I915_WRITE(reg, dpll);
63d7bbe9
JB
1650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
1652}
1653
1654/**
50b44a44 1655 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1656 * @dev_priv: i915 private structure
1657 * @pipe: pipe PLL to disable
1658 *
1659 * Disable the PLL for @pipe, making sure the pipe is off first.
1660 *
1661 * Note! This is for pre-ILK only.
1662 */
50b44a44 1663static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1664{
63d7bbe9
JB
1665 /* Don't disable pipe A or pipe A PLLs if needed */
1666 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1667 return;
1668
1669 /* Make sure the pipe isn't still relying on us */
1670 assert_pipe_disabled(dev_priv, pipe);
1671
50b44a44
DV
1672 I915_WRITE(DPLL(pipe), 0);
1673 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1674}
1675
f6071166
JB
1676static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1677{
1678 u32 val = 0;
1679
1680 /* Make sure the pipe isn't still relying on us */
1681 assert_pipe_disabled(dev_priv, pipe);
1682
e5cbfbfb
ID
1683 /*
1684 * Leave integrated clock source and reference clock enabled for pipe B.
1685 * The latter is needed for VGA hotplug / manual detection.
1686 */
f6071166 1687 if (pipe == PIPE_B)
e5cbfbfb 1688 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1689 I915_WRITE(DPLL(pipe), val);
1690 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1691
1692}
1693
1694static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1695{
d752048d 1696 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1697 u32 val;
1698
a11b0703
VS
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1701
a11b0703 1702 /* Set PLL en = 0 */
d17ec4ce 1703 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1704 if (pipe != PIPE_A)
1705 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1706 I915_WRITE(DPLL(pipe), val);
1707 POSTING_READ(DPLL(pipe));
d752048d
VS
1708
1709 mutex_lock(&dev_priv->dpio_lock);
1710
1711 /* Disable 10bit clock to display controller */
1712 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1713 val &= ~DPIO_DCLKP_EN;
1714 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1715
61407f6d
VS
1716 /* disable left/right clock distribution */
1717 if (pipe != PIPE_B) {
1718 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1719 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1720 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1721 } else {
1722 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1723 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1724 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1725 }
1726
d752048d 1727 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1728}
1729
e4607fcf
CML
1730void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1731 struct intel_digital_port *dport)
89b667f8
JB
1732{
1733 u32 port_mask;
00fc31b7 1734 int dpll_reg;
89b667f8 1735
e4607fcf
CML
1736 switch (dport->port) {
1737 case PORT_B:
89b667f8 1738 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1739 dpll_reg = DPLL(0);
e4607fcf
CML
1740 break;
1741 case PORT_C:
89b667f8 1742 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1743 dpll_reg = DPLL(0);
1744 break;
1745 case PORT_D:
1746 port_mask = DPLL_PORTD_READY_MASK;
1747 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1748 break;
1749 default:
1750 BUG();
1751 }
89b667f8 1752
00fc31b7 1753 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1754 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1755 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1756}
1757
b14b1055
DV
1758static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1759{
1760 struct drm_device *dev = crtc->base.dev;
1761 struct drm_i915_private *dev_priv = dev->dev_private;
1762 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1763
be19f0ff
CW
1764 if (WARN_ON(pll == NULL))
1765 return;
1766
b14b1055
DV
1767 WARN_ON(!pll->refcount);
1768 if (pll->active == 0) {
1769 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1770 WARN_ON(pll->on);
1771 assert_shared_dpll_disabled(dev_priv, pll);
1772
1773 pll->mode_set(dev_priv, pll);
1774 }
1775}
1776
92f2584a 1777/**
85b3894f 1778 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1779 * @dev_priv: i915 private structure
1780 * @pipe: pipe PLL to enable
1781 *
1782 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1783 * drives the transcoder clock.
1784 */
85b3894f 1785static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1786{
3d13ef2e
DL
1787 struct drm_device *dev = crtc->base.dev;
1788 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1789 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1790
87a875bb 1791 if (WARN_ON(pll == NULL))
48da64a8
CW
1792 return;
1793
1794 if (WARN_ON(pll->refcount == 0))
1795 return;
ee7b9f93 1796
74dd6928 1797 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1798 pll->name, pll->active, pll->on,
e2b78267 1799 crtc->base.base.id);
92f2584a 1800
cdbd2316
DV
1801 if (pll->active++) {
1802 WARN_ON(!pll->on);
e9d6944e 1803 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1804 return;
1805 }
f4a091c7 1806 WARN_ON(pll->on);
ee7b9f93 1807
bd2bb1b9
PZ
1808 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1809
46edb027 1810 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1811 pll->enable(dev_priv, pll);
ee7b9f93 1812 pll->on = true;
92f2584a
JB
1813}
1814
f6daaec2 1815static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1816{
3d13ef2e
DL
1817 struct drm_device *dev = crtc->base.dev;
1818 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1819 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1820
92f2584a 1821 /* PCH only available on ILK+ */
3d13ef2e 1822 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1823 if (WARN_ON(pll == NULL))
ee7b9f93 1824 return;
92f2584a 1825
48da64a8
CW
1826 if (WARN_ON(pll->refcount == 0))
1827 return;
7a419866 1828
46edb027
DV
1829 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1830 pll->name, pll->active, pll->on,
e2b78267 1831 crtc->base.base.id);
7a419866 1832
48da64a8 1833 if (WARN_ON(pll->active == 0)) {
e9d6944e 1834 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1835 return;
1836 }
1837
e9d6944e 1838 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1839 WARN_ON(!pll->on);
cdbd2316 1840 if (--pll->active)
7a419866 1841 return;
ee7b9f93 1842
46edb027 1843 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1844 pll->disable(dev_priv, pll);
ee7b9f93 1845 pll->on = false;
bd2bb1b9
PZ
1846
1847 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1848}
1849
b8a4f404
PZ
1850static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1851 enum pipe pipe)
040484af 1852{
23670b32 1853 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1854 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1856 uint32_t reg, val, pipeconf_val;
040484af
JB
1857
1858 /* PCH only available on ILK+ */
3d13ef2e 1859 BUG_ON(INTEL_INFO(dev)->gen < 5);
040484af
JB
1860
1861 /* Make sure PCH DPLL is enabled */
e72f9fbf 1862 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1863 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1864
1865 /* FDI must be feeding us bits for PCH ports */
1866 assert_fdi_tx_enabled(dev_priv, pipe);
1867 assert_fdi_rx_enabled(dev_priv, pipe);
1868
23670b32
DV
1869 if (HAS_PCH_CPT(dev)) {
1870 /* Workaround: Set the timing override bit before enabling the
1871 * pch transcoder. */
1872 reg = TRANS_CHICKEN2(pipe);
1873 val = I915_READ(reg);
1874 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1875 I915_WRITE(reg, val);
59c859d6 1876 }
23670b32 1877
ab9412ba 1878 reg = PCH_TRANSCONF(pipe);
040484af 1879 val = I915_READ(reg);
5f7f726d 1880 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1881
1882 if (HAS_PCH_IBX(dev_priv->dev)) {
1883 /*
1884 * make the BPC in transcoder be consistent with
1885 * that in pipeconf reg.
1886 */
dfd07d72
DV
1887 val &= ~PIPECONF_BPC_MASK;
1888 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1889 }
5f7f726d
PZ
1890
1891 val &= ~TRANS_INTERLACE_MASK;
1892 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1893 if (HAS_PCH_IBX(dev_priv->dev) &&
1894 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1895 val |= TRANS_LEGACY_INTERLACED_ILK;
1896 else
1897 val |= TRANS_INTERLACED;
5f7f726d
PZ
1898 else
1899 val |= TRANS_PROGRESSIVE;
1900
040484af
JB
1901 I915_WRITE(reg, val | TRANS_ENABLE);
1902 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1903 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1904}
1905
8fb033d7 1906static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1907 enum transcoder cpu_transcoder)
040484af 1908{
8fb033d7 1909 u32 val, pipeconf_val;
8fb033d7
PZ
1910
1911 /* PCH only available on ILK+ */
3d13ef2e 1912 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
8fb033d7 1913
8fb033d7 1914 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1915 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1916 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1917
223a6fdf
PZ
1918 /* Workaround: set timing override bit. */
1919 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1920 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1921 I915_WRITE(_TRANSA_CHICKEN2, val);
1922
25f3ef11 1923 val = TRANS_ENABLE;
937bb610 1924 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1925
9a76b1c6
PZ
1926 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1927 PIPECONF_INTERLACED_ILK)
a35f2679 1928 val |= TRANS_INTERLACED;
8fb033d7
PZ
1929 else
1930 val |= TRANS_PROGRESSIVE;
1931
ab9412ba
DV
1932 I915_WRITE(LPT_TRANSCONF, val);
1933 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1934 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1935}
1936
b8a4f404
PZ
1937static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1938 enum pipe pipe)
040484af 1939{
23670b32
DV
1940 struct drm_device *dev = dev_priv->dev;
1941 uint32_t reg, val;
040484af
JB
1942
1943 /* FDI relies on the transcoder */
1944 assert_fdi_tx_disabled(dev_priv, pipe);
1945 assert_fdi_rx_disabled(dev_priv, pipe);
1946
291906f1
JB
1947 /* Ports must be off as well */
1948 assert_pch_ports_disabled(dev_priv, pipe);
1949
ab9412ba 1950 reg = PCH_TRANSCONF(pipe);
040484af
JB
1951 val = I915_READ(reg);
1952 val &= ~TRANS_ENABLE;
1953 I915_WRITE(reg, val);
1954 /* wait for PCH transcoder off, transcoder state */
1955 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1956 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1957
1958 if (!HAS_PCH_IBX(dev)) {
1959 /* Workaround: Clear the timing override chicken bit again. */
1960 reg = TRANS_CHICKEN2(pipe);
1961 val = I915_READ(reg);
1962 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1963 I915_WRITE(reg, val);
1964 }
040484af
JB
1965}
1966
ab4d966c 1967static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1968{
8fb033d7
PZ
1969 u32 val;
1970
ab9412ba 1971 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1972 val &= ~TRANS_ENABLE;
ab9412ba 1973 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1974 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1975 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1976 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1977
1978 /* Workaround: clear timing override bit. */
1979 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1980 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1981 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1982}
1983
b24e7179 1984/**
309cfea8 1985 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1986 * @crtc: crtc responsible for the pipe
b24e7179 1987 *
0372264a 1988 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1989 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1990 */
e1fdc473 1991static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1992{
0372264a
PZ
1993 struct drm_device *dev = crtc->base.dev;
1994 struct drm_i915_private *dev_priv = dev->dev_private;
1995 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1996 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1997 pipe);
1a240d4d 1998 enum pipe pch_transcoder;
b24e7179
JB
1999 int reg;
2000 u32 val;
2001
58c6eaa2 2002 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2003 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2004 assert_sprites_disabled(dev_priv, pipe);
2005
681e5811 2006 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2007 pch_transcoder = TRANSCODER_A;
2008 else
2009 pch_transcoder = pipe;
2010
b24e7179
JB
2011 /*
2012 * A pipe without a PLL won't actually be able to drive bits from
2013 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2014 * need the check.
2015 */
2016 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2017 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2018 assert_dsi_pll_enabled(dev_priv);
2019 else
2020 assert_pll_enabled(dev_priv, pipe);
040484af 2021 else {
30421c4f 2022 if (crtc->config.has_pch_encoder) {
040484af 2023 /* if driving the PCH, we need FDI enabled */
cc391bbb 2024 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2025 assert_fdi_tx_pll_enabled(dev_priv,
2026 (enum pipe) cpu_transcoder);
040484af
JB
2027 }
2028 /* FIXME: assert CPU port conditions for SNB+ */
2029 }
b24e7179 2030
702e7a56 2031 reg = PIPECONF(cpu_transcoder);
b24e7179 2032 val = I915_READ(reg);
7ad25d48
PZ
2033 if (val & PIPECONF_ENABLE) {
2034 WARN_ON(!(pipe == PIPE_A &&
2035 dev_priv->quirks & QUIRK_PIPEA_FORCE));
00d70b15 2036 return;
7ad25d48 2037 }
00d70b15
CW
2038
2039 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2040 POSTING_READ(reg);
b24e7179
JB
2041}
2042
2043/**
309cfea8 2044 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
2045 * @dev_priv: i915 private structure
2046 * @pipe: pipe to disable
2047 *
2048 * Disable @pipe, making sure that various hardware specific requirements
2049 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2050 *
2051 * @pipe should be %PIPE_A or %PIPE_B.
2052 *
2053 * Will wait until the pipe has shut down before returning.
2054 */
2055static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2056 enum pipe pipe)
2057{
702e7a56
PZ
2058 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2059 pipe);
b24e7179
JB
2060 int reg;
2061 u32 val;
2062
2063 /*
2064 * Make sure planes won't keep trying to pump pixels to us,
2065 * or we might hang the display.
2066 */
2067 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2068 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2069 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
2070
2071 /* Don't disable pipe A or pipe A PLLs if needed */
2072 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2073 return;
2074
702e7a56 2075 reg = PIPECONF(cpu_transcoder);
b24e7179 2076 val = I915_READ(reg);
00d70b15
CW
2077 if ((val & PIPECONF_ENABLE) == 0)
2078 return;
2079
2080 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
2081 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2082}
2083
d74362c9
KP
2084/*
2085 * Plane regs are double buffered, going from enabled->disabled needs a
2086 * trigger in order to latch. The display address reg provides this.
2087 */
1dba99f4
VS
2088void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2089 enum plane plane)
d74362c9 2090{
3d13ef2e
DL
2091 struct drm_device *dev = dev_priv->dev;
2092 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2093
2094 I915_WRITE(reg, I915_READ(reg));
2095 POSTING_READ(reg);
d74362c9
KP
2096}
2097
b24e7179 2098/**
262ca2b0 2099 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2100 * @plane: plane to be enabled
2101 * @crtc: crtc for the plane
b24e7179 2102 *
fdd508a6 2103 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2104 */
fdd508a6
VS
2105static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2106 struct drm_crtc *crtc)
b24e7179 2107{
fdd508a6
VS
2108 struct drm_device *dev = plane->dev;
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2111
2112 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2113 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2114
98ec7739
VS
2115 if (intel_crtc->primary_enabled)
2116 return;
0037f71c 2117
4c445e0e 2118 intel_crtc->primary_enabled = true;
939c2fe8 2119
fdd508a6
VS
2120 dev_priv->display.update_primary_plane(crtc, plane->fb,
2121 crtc->x, crtc->y);
33c3b0d1
VS
2122
2123 /*
2124 * BDW signals flip done immediately if the plane
2125 * is disabled, even if the plane enable is already
2126 * armed to occur at the next vblank :(
2127 */
2128 if (IS_BROADWELL(dev))
2129 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2130}
2131
b24e7179 2132/**
262ca2b0 2133 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2134 * @plane: plane to be disabled
2135 * @crtc: crtc for the plane
b24e7179 2136 *
fdd508a6 2137 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2138 */
fdd508a6
VS
2139static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2140 struct drm_crtc *crtc)
b24e7179 2141{
fdd508a6
VS
2142 struct drm_device *dev = plane->dev;
2143 struct drm_i915_private *dev_priv = dev->dev_private;
2144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2145
2146 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2147
98ec7739
VS
2148 if (!intel_crtc->primary_enabled)
2149 return;
0037f71c 2150
4c445e0e 2151 intel_crtc->primary_enabled = false;
939c2fe8 2152
fdd508a6
VS
2153 dev_priv->display.update_primary_plane(crtc, plane->fb,
2154 crtc->x, crtc->y);
b24e7179
JB
2155}
2156
693db184
CW
2157static bool need_vtd_wa(struct drm_device *dev)
2158{
2159#ifdef CONFIG_INTEL_IOMMU
2160 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2161 return true;
2162#endif
2163 return false;
2164}
2165
a57ce0b2
JB
2166static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2167{
2168 int tile_height;
2169
2170 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2171 return ALIGN(height, tile_height);
2172}
2173
127bd2ac 2174int
48b956c5 2175intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2176 struct drm_i915_gem_object *obj,
a4872ba6 2177 struct intel_engine_cs *pipelined)
6b95a207 2178{
ce453d81 2179 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2180 u32 alignment;
2181 int ret;
2182
ebcdd39e
MR
2183 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2184
05394f39 2185 switch (obj->tiling_mode) {
6b95a207 2186 case I915_TILING_NONE:
534843da
CW
2187 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2188 alignment = 128 * 1024;
a6c45cf0 2189 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2190 alignment = 4 * 1024;
2191 else
2192 alignment = 64 * 1024;
6b95a207
KH
2193 break;
2194 case I915_TILING_X:
2195 /* pin() will align the object as required by fence */
2196 alignment = 0;
2197 break;
2198 case I915_TILING_Y:
80075d49 2199 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2200 return -EINVAL;
2201 default:
2202 BUG();
2203 }
2204
693db184
CW
2205 /* Note that the w/a also requires 64 PTE of padding following the
2206 * bo. We currently fill all unused PTE with the shadow page and so
2207 * we should always have valid PTE following the scanout preventing
2208 * the VT-d warning.
2209 */
2210 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2211 alignment = 256 * 1024;
2212
ce453d81 2213 dev_priv->mm.interruptible = false;
2da3b9b9 2214 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2215 if (ret)
ce453d81 2216 goto err_interruptible;
6b95a207
KH
2217
2218 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2219 * fence, whereas 965+ only requires a fence if using
2220 * framebuffer compression. For simplicity, we always install
2221 * a fence as the cost is not that onerous.
2222 */
06d98131 2223 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2224 if (ret)
2225 goto err_unpin;
1690e1eb 2226
9a5a53b3 2227 i915_gem_object_pin_fence(obj);
6b95a207 2228
ce453d81 2229 dev_priv->mm.interruptible = true;
6b95a207 2230 return 0;
48b956c5
CW
2231
2232err_unpin:
cc98b413 2233 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2234err_interruptible:
2235 dev_priv->mm.interruptible = true;
48b956c5 2236 return ret;
6b95a207
KH
2237}
2238
1690e1eb
CW
2239void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2240{
ebcdd39e
MR
2241 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2242
1690e1eb 2243 i915_gem_object_unpin_fence(obj);
cc98b413 2244 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2245}
2246
c2c75131
DV
2247/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2248 * is assumed to be a power-of-two. */
bc752862
CW
2249unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2250 unsigned int tiling_mode,
2251 unsigned int cpp,
2252 unsigned int pitch)
c2c75131 2253{
bc752862
CW
2254 if (tiling_mode != I915_TILING_NONE) {
2255 unsigned int tile_rows, tiles;
c2c75131 2256
bc752862
CW
2257 tile_rows = *y / 8;
2258 *y %= 8;
c2c75131 2259
bc752862
CW
2260 tiles = *x / (512/cpp);
2261 *x %= 512/cpp;
2262
2263 return tile_rows * pitch * 8 + tiles * 4096;
2264 } else {
2265 unsigned int offset;
2266
2267 offset = *y * pitch + *x * cpp;
2268 *y = 0;
2269 *x = (offset & 4095) / cpp;
2270 return offset & -4096;
2271 }
c2c75131
DV
2272}
2273
46f297fb
JB
2274int intel_format_to_fourcc(int format)
2275{
2276 switch (format) {
2277 case DISPPLANE_8BPP:
2278 return DRM_FORMAT_C8;
2279 case DISPPLANE_BGRX555:
2280 return DRM_FORMAT_XRGB1555;
2281 case DISPPLANE_BGRX565:
2282 return DRM_FORMAT_RGB565;
2283 default:
2284 case DISPPLANE_BGRX888:
2285 return DRM_FORMAT_XRGB8888;
2286 case DISPPLANE_RGBX888:
2287 return DRM_FORMAT_XBGR8888;
2288 case DISPPLANE_BGRX101010:
2289 return DRM_FORMAT_XRGB2101010;
2290 case DISPPLANE_RGBX101010:
2291 return DRM_FORMAT_XBGR2101010;
2292 }
2293}
2294
484b41dd 2295static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2296 struct intel_plane_config *plane_config)
2297{
2298 struct drm_device *dev = crtc->base.dev;
2299 struct drm_i915_gem_object *obj = NULL;
2300 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2301 u32 base = plane_config->base;
2302
ff2652ea
CW
2303 if (plane_config->size == 0)
2304 return false;
2305
46f297fb
JB
2306 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2307 plane_config->size);
2308 if (!obj)
484b41dd 2309 return false;
46f297fb
JB
2310
2311 if (plane_config->tiled) {
2312 obj->tiling_mode = I915_TILING_X;
66e514c1 2313 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2314 }
2315
66e514c1
DA
2316 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2317 mode_cmd.width = crtc->base.primary->fb->width;
2318 mode_cmd.height = crtc->base.primary->fb->height;
2319 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2320
2321 mutex_lock(&dev->struct_mutex);
2322
66e514c1 2323 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2324 &mode_cmd, obj)) {
46f297fb
JB
2325 DRM_DEBUG_KMS("intel fb init failed\n");
2326 goto out_unref_obj;
2327 }
2328
a071fa00 2329 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2330 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2331
2332 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2333 return true;
46f297fb
JB
2334
2335out_unref_obj:
2336 drm_gem_object_unreference(&obj->base);
2337 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2338 return false;
2339}
2340
2341static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2342 struct intel_plane_config *plane_config)
2343{
2344 struct drm_device *dev = intel_crtc->base.dev;
2345 struct drm_crtc *c;
2346 struct intel_crtc *i;
2ff8fde1 2347 struct drm_i915_gem_object *obj;
484b41dd 2348
66e514c1 2349 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2350 return;
2351
2352 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2353 return;
2354
66e514c1
DA
2355 kfree(intel_crtc->base.primary->fb);
2356 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2357
2358 /*
2359 * Failed to alloc the obj, check to see if we should share
2360 * an fb with another CRTC instead
2361 */
70e1e0ec 2362 for_each_crtc(dev, c) {
484b41dd
JB
2363 i = to_intel_crtc(c);
2364
2365 if (c == &intel_crtc->base)
2366 continue;
2367
2ff8fde1
MR
2368 if (!i->active)
2369 continue;
2370
2371 obj = intel_fb_obj(c->primary->fb);
2372 if (obj == NULL)
484b41dd
JB
2373 continue;
2374
2ff8fde1 2375 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2376 drm_framebuffer_reference(c->primary->fb);
2377 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2378 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2379 break;
2380 }
2381 }
46f297fb
JB
2382}
2383
29b9bde6
DV
2384static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2385 struct drm_framebuffer *fb,
2386 int x, int y)
81255565
JB
2387{
2388 struct drm_device *dev = crtc->dev;
2389 struct drm_i915_private *dev_priv = dev->dev_private;
2390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2391 struct drm_i915_gem_object *obj;
81255565 2392 int plane = intel_crtc->plane;
e506a0c6 2393 unsigned long linear_offset;
81255565 2394 u32 dspcntr;
f45651ba 2395 u32 reg = DSPCNTR(plane);
48404c1e
SJ
2396 int pixel_size;
2397
fdd508a6
VS
2398 if (!intel_crtc->primary_enabled) {
2399 I915_WRITE(reg, 0);
2400 if (INTEL_INFO(dev)->gen >= 4)
2401 I915_WRITE(DSPSURF(plane), 0);
2402 else
2403 I915_WRITE(DSPADDR(plane), 0);
2404 POSTING_READ(reg);
2405 return;
2406 }
2407
c9ba6fad
VS
2408 obj = intel_fb_obj(fb);
2409 if (WARN_ON(obj == NULL))
2410 return;
2411
2412 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2413
f45651ba
VS
2414 dspcntr = DISPPLANE_GAMMA_ENABLE;
2415
fdd508a6 2416 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2417
2418 if (INTEL_INFO(dev)->gen < 4) {
2419 if (intel_crtc->pipe == PIPE_B)
2420 dspcntr |= DISPPLANE_SEL_PIPE_B;
2421
2422 /* pipesrc and dspsize control the size that is scaled from,
2423 * which should always be the user's requested size.
2424 */
2425 I915_WRITE(DSPSIZE(plane),
2426 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2427 (intel_crtc->config.pipe_src_w - 1));
2428 I915_WRITE(DSPPOS(plane), 0);
2429 }
81255565 2430
57779d06
VS
2431 switch (fb->pixel_format) {
2432 case DRM_FORMAT_C8:
81255565
JB
2433 dspcntr |= DISPPLANE_8BPP;
2434 break;
57779d06
VS
2435 case DRM_FORMAT_XRGB1555:
2436 case DRM_FORMAT_ARGB1555:
2437 dspcntr |= DISPPLANE_BGRX555;
81255565 2438 break;
57779d06
VS
2439 case DRM_FORMAT_RGB565:
2440 dspcntr |= DISPPLANE_BGRX565;
2441 break;
2442 case DRM_FORMAT_XRGB8888:
2443 case DRM_FORMAT_ARGB8888:
2444 dspcntr |= DISPPLANE_BGRX888;
2445 break;
2446 case DRM_FORMAT_XBGR8888:
2447 case DRM_FORMAT_ABGR8888:
2448 dspcntr |= DISPPLANE_RGBX888;
2449 break;
2450 case DRM_FORMAT_XRGB2101010:
2451 case DRM_FORMAT_ARGB2101010:
2452 dspcntr |= DISPPLANE_BGRX101010;
2453 break;
2454 case DRM_FORMAT_XBGR2101010:
2455 case DRM_FORMAT_ABGR2101010:
2456 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2457 break;
2458 default:
baba133a 2459 BUG();
81255565 2460 }
57779d06 2461
f45651ba
VS
2462 if (INTEL_INFO(dev)->gen >= 4 &&
2463 obj->tiling_mode != I915_TILING_NONE)
2464 dspcntr |= DISPPLANE_TILED;
81255565 2465
de1aa629
VS
2466 if (IS_G4X(dev))
2467 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2468
b9897127 2469 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2470
c2c75131
DV
2471 if (INTEL_INFO(dev)->gen >= 4) {
2472 intel_crtc->dspaddr_offset =
bc752862 2473 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2474 pixel_size,
bc752862 2475 fb->pitches[0]);
c2c75131
DV
2476 linear_offset -= intel_crtc->dspaddr_offset;
2477 } else {
e506a0c6 2478 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2479 }
e506a0c6 2480
48404c1e
SJ
2481 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2482 dspcntr |= DISPPLANE_ROTATE_180;
2483
2484 x += (intel_crtc->config.pipe_src_w - 1);
2485 y += (intel_crtc->config.pipe_src_h - 1);
2486
2487 /* Finding the last pixel of the last line of the display
2488 data and adding to linear_offset*/
2489 linear_offset +=
2490 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2491 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2492 }
2493
2494 I915_WRITE(reg, dspcntr);
2495
f343c5f6
BW
2496 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2497 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2498 fb->pitches[0]);
01f2c773 2499 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2500 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2501 I915_WRITE(DSPSURF(plane),
2502 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2503 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2504 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2505 } else
f343c5f6 2506 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2507 POSTING_READ(reg);
17638cd6
JB
2508}
2509
29b9bde6
DV
2510static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2511 struct drm_framebuffer *fb,
2512 int x, int y)
17638cd6
JB
2513{
2514 struct drm_device *dev = crtc->dev;
2515 struct drm_i915_private *dev_priv = dev->dev_private;
2516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2517 struct drm_i915_gem_object *obj;
17638cd6 2518 int plane = intel_crtc->plane;
e506a0c6 2519 unsigned long linear_offset;
17638cd6 2520 u32 dspcntr;
f45651ba 2521 u32 reg = DSPCNTR(plane);
48404c1e
SJ
2522 int pixel_size;
2523
fdd508a6
VS
2524 if (!intel_crtc->primary_enabled) {
2525 I915_WRITE(reg, 0);
2526 I915_WRITE(DSPSURF(plane), 0);
2527 POSTING_READ(reg);
2528 return;
2529 }
2530
c9ba6fad
VS
2531 obj = intel_fb_obj(fb);
2532 if (WARN_ON(obj == NULL))
2533 return;
2534
2535 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2536
f45651ba
VS
2537 dspcntr = DISPPLANE_GAMMA_ENABLE;
2538
fdd508a6 2539 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2540
2541 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2542 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2543
57779d06
VS
2544 switch (fb->pixel_format) {
2545 case DRM_FORMAT_C8:
17638cd6
JB
2546 dspcntr |= DISPPLANE_8BPP;
2547 break;
57779d06
VS
2548 case DRM_FORMAT_RGB565:
2549 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2550 break;
57779d06
VS
2551 case DRM_FORMAT_XRGB8888:
2552 case DRM_FORMAT_ARGB8888:
2553 dspcntr |= DISPPLANE_BGRX888;
2554 break;
2555 case DRM_FORMAT_XBGR8888:
2556 case DRM_FORMAT_ABGR8888:
2557 dspcntr |= DISPPLANE_RGBX888;
2558 break;
2559 case DRM_FORMAT_XRGB2101010:
2560 case DRM_FORMAT_ARGB2101010:
2561 dspcntr |= DISPPLANE_BGRX101010;
2562 break;
2563 case DRM_FORMAT_XBGR2101010:
2564 case DRM_FORMAT_ABGR2101010:
2565 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2566 break;
2567 default:
baba133a 2568 BUG();
17638cd6
JB
2569 }
2570
2571 if (obj->tiling_mode != I915_TILING_NONE)
2572 dspcntr |= DISPPLANE_TILED;
17638cd6 2573
f45651ba 2574 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2575 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2576
b9897127 2577 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2578 intel_crtc->dspaddr_offset =
bc752862 2579 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2580 pixel_size,
bc752862 2581 fb->pitches[0]);
c2c75131 2582 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2583 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2584 dspcntr |= DISPPLANE_ROTATE_180;
2585
2586 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2587 x += (intel_crtc->config.pipe_src_w - 1);
2588 y += (intel_crtc->config.pipe_src_h - 1);
2589
2590 /* Finding the last pixel of the last line of the display
2591 data and adding to linear_offset*/
2592 linear_offset +=
2593 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2594 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2595 }
2596 }
2597
2598 I915_WRITE(reg, dspcntr);
17638cd6 2599
f343c5f6
BW
2600 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2601 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2602 fb->pitches[0]);
01f2c773 2603 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2604 I915_WRITE(DSPSURF(plane),
2605 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2606 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2607 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2608 } else {
2609 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2610 I915_WRITE(DSPLINOFF(plane), linear_offset);
2611 }
17638cd6 2612 POSTING_READ(reg);
17638cd6
JB
2613}
2614
2615/* Assume fb object is pinned & idle & fenced and just update base pointers */
2616static int
2617intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2618 int x, int y, enum mode_set_atomic state)
2619{
2620 struct drm_device *dev = crtc->dev;
2621 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2622
6b8e6ed0
CW
2623 if (dev_priv->display.disable_fbc)
2624 dev_priv->display.disable_fbc(dev);
cc36513c 2625 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
81255565 2626
29b9bde6
DV
2627 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2628
2629 return 0;
81255565
JB
2630}
2631
96a02917
VS
2632void intel_display_handle_reset(struct drm_device *dev)
2633{
2634 struct drm_i915_private *dev_priv = dev->dev_private;
2635 struct drm_crtc *crtc;
2636
2637 /*
2638 * Flips in the rings have been nuked by the reset,
2639 * so complete all pending flips so that user space
2640 * will get its events and not get stuck.
2641 *
2642 * Also update the base address of all primary
2643 * planes to the the last fb to make sure we're
2644 * showing the correct fb after a reset.
2645 *
2646 * Need to make two loops over the crtcs so that we
2647 * don't try to grab a crtc mutex before the
2648 * pending_flip_queue really got woken up.
2649 */
2650
70e1e0ec 2651 for_each_crtc(dev, crtc) {
96a02917
VS
2652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2653 enum plane plane = intel_crtc->plane;
2654
2655 intel_prepare_page_flip(dev, plane);
2656 intel_finish_page_flip_plane(dev, plane);
2657 }
2658
70e1e0ec 2659 for_each_crtc(dev, crtc) {
96a02917
VS
2660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2661
51fd371b 2662 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2663 /*
2664 * FIXME: Once we have proper support for primary planes (and
2665 * disabling them without disabling the entire crtc) allow again
66e514c1 2666 * a NULL crtc->primary->fb.
947fdaad 2667 */
f4510a27 2668 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2669 dev_priv->display.update_primary_plane(crtc,
66e514c1 2670 crtc->primary->fb,
262ca2b0
MR
2671 crtc->x,
2672 crtc->y);
51fd371b 2673 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2674 }
2675}
2676
14667a4b
CW
2677static int
2678intel_finish_fb(struct drm_framebuffer *old_fb)
2679{
2ff8fde1 2680 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2681 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2682 bool was_interruptible = dev_priv->mm.interruptible;
2683 int ret;
2684
14667a4b
CW
2685 /* Big Hammer, we also need to ensure that any pending
2686 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2687 * current scanout is retired before unpinning the old
2688 * framebuffer.
2689 *
2690 * This should only fail upon a hung GPU, in which case we
2691 * can safely continue.
2692 */
2693 dev_priv->mm.interruptible = false;
2694 ret = i915_gem_object_finish_gpu(obj);
2695 dev_priv->mm.interruptible = was_interruptible;
2696
2697 return ret;
2698}
2699
7d5e3799
CW
2700static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2701{
2702 struct drm_device *dev = crtc->dev;
2703 struct drm_i915_private *dev_priv = dev->dev_private;
2704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2705 unsigned long flags;
2706 bool pending;
2707
2708 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2709 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2710 return false;
2711
2712 spin_lock_irqsave(&dev->event_lock, flags);
2713 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2714 spin_unlock_irqrestore(&dev->event_lock, flags);
2715
2716 return pending;
2717}
2718
5c3b82e2 2719static int
3c4fdcfb 2720intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2721 struct drm_framebuffer *fb)
79e53945
JB
2722{
2723 struct drm_device *dev = crtc->dev;
6b8e6ed0 2724 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2726 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2727 struct drm_framebuffer *old_fb = crtc->primary->fb;
2728 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2729 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2730 int ret;
79e53945 2731
7d5e3799
CW
2732 if (intel_crtc_has_pending_flip(crtc)) {
2733 DRM_ERROR("pipe is still busy with an old pageflip\n");
2734 return -EBUSY;
2735 }
2736
79e53945 2737 /* no fb bound */
94352cf9 2738 if (!fb) {
a5071c2f 2739 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2740 return 0;
2741 }
2742
7eb552ae 2743 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2744 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2745 plane_name(intel_crtc->plane),
2746 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2747 return -EINVAL;
79e53945
JB
2748 }
2749
5c3b82e2 2750 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2751 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2752 if (ret == 0)
91565c85 2753 i915_gem_track_fb(old_obj, obj,
a071fa00 2754 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2755 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2756 if (ret != 0) {
a5071c2f 2757 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2758 return ret;
2759 }
79e53945 2760
bb2043de
DL
2761 /*
2762 * Update pipe size and adjust fitter if needed: the reason for this is
2763 * that in compute_mode_changes we check the native mode (not the pfit
2764 * mode) to see if we can flip rather than do a full mode set. In the
2765 * fastboot case, we'll flip, but if we don't update the pipesrc and
2766 * pfit state, we'll end up with a big fb scanned out into the wrong
2767 * sized surface.
2768 *
2769 * To fix this properly, we need to hoist the checks up into
2770 * compute_mode_changes (or above), check the actual pfit state and
2771 * whether the platform allows pfit disable with pipe active, and only
2772 * then update the pipesrc and pfit state, even on the flip path.
2773 */
d330a953 2774 if (i915.fastboot) {
d7bf63f2
DL
2775 const struct drm_display_mode *adjusted_mode =
2776 &intel_crtc->config.adjusted_mode;
2777
4d6a3e63 2778 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2779 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2780 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2781 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2782 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2783 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2784 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2785 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2786 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2787 }
0637d60d
JB
2788 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2789 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2790 }
2791
29b9bde6 2792 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2793
f99d7069
DV
2794 if (intel_crtc->active)
2795 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2796
f4510a27 2797 crtc->primary->fb = fb;
6c4c86f5
DV
2798 crtc->x = x;
2799 crtc->y = y;
94352cf9 2800
b7f1de28 2801 if (old_fb) {
d7697eea
DV
2802 if (intel_crtc->active && old_fb != fb)
2803 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2804 mutex_lock(&dev->struct_mutex);
2ff8fde1 2805 intel_unpin_fb_obj(old_obj);
8ac36ec1 2806 mutex_unlock(&dev->struct_mutex);
b7f1de28 2807 }
652c393a 2808
8ac36ec1 2809 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2810 intel_update_fbc(dev);
5c3b82e2 2811 mutex_unlock(&dev->struct_mutex);
79e53945 2812
5c3b82e2 2813 return 0;
79e53945
JB
2814}
2815
5e84e1a4
ZW
2816static void intel_fdi_normal_train(struct drm_crtc *crtc)
2817{
2818 struct drm_device *dev = crtc->dev;
2819 struct drm_i915_private *dev_priv = dev->dev_private;
2820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2821 int pipe = intel_crtc->pipe;
2822 u32 reg, temp;
2823
2824 /* enable normal train */
2825 reg = FDI_TX_CTL(pipe);
2826 temp = I915_READ(reg);
61e499bf 2827 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2828 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2829 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2830 } else {
2831 temp &= ~FDI_LINK_TRAIN_NONE;
2832 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2833 }
5e84e1a4
ZW
2834 I915_WRITE(reg, temp);
2835
2836 reg = FDI_RX_CTL(pipe);
2837 temp = I915_READ(reg);
2838 if (HAS_PCH_CPT(dev)) {
2839 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2840 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2841 } else {
2842 temp &= ~FDI_LINK_TRAIN_NONE;
2843 temp |= FDI_LINK_TRAIN_NONE;
2844 }
2845 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2846
2847 /* wait one idle pattern time */
2848 POSTING_READ(reg);
2849 udelay(1000);
357555c0
JB
2850
2851 /* IVB wants error correction enabled */
2852 if (IS_IVYBRIDGE(dev))
2853 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2854 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2855}
2856
1fbc0d78 2857static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2858{
1fbc0d78
DV
2859 return crtc->base.enabled && crtc->active &&
2860 crtc->config.has_pch_encoder;
1e833f40
DV
2861}
2862
01a415fd
DV
2863static void ivb_modeset_global_resources(struct drm_device *dev)
2864{
2865 struct drm_i915_private *dev_priv = dev->dev_private;
2866 struct intel_crtc *pipe_B_crtc =
2867 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2868 struct intel_crtc *pipe_C_crtc =
2869 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2870 uint32_t temp;
2871
1e833f40
DV
2872 /*
2873 * When everything is off disable fdi C so that we could enable fdi B
2874 * with all lanes. Note that we don't care about enabled pipes without
2875 * an enabled pch encoder.
2876 */
2877 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2878 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2879 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2880 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2881
2882 temp = I915_READ(SOUTH_CHICKEN1);
2883 temp &= ~FDI_BC_BIFURCATION_SELECT;
2884 DRM_DEBUG_KMS("disabling fdi C rx\n");
2885 I915_WRITE(SOUTH_CHICKEN1, temp);
2886 }
2887}
2888
8db9d77b
ZW
2889/* The FDI link training functions for ILK/Ibexpeak. */
2890static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2891{
2892 struct drm_device *dev = crtc->dev;
2893 struct drm_i915_private *dev_priv = dev->dev_private;
2894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2895 int pipe = intel_crtc->pipe;
5eddb70b 2896 u32 reg, temp, tries;
8db9d77b 2897
1c8562f6 2898 /* FDI needs bits from pipe first */
0fc932b8 2899 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2900
e1a44743
AJ
2901 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2902 for train result */
5eddb70b
CW
2903 reg = FDI_RX_IMR(pipe);
2904 temp = I915_READ(reg);
e1a44743
AJ
2905 temp &= ~FDI_RX_SYMBOL_LOCK;
2906 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2907 I915_WRITE(reg, temp);
2908 I915_READ(reg);
e1a44743
AJ
2909 udelay(150);
2910
8db9d77b 2911 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2912 reg = FDI_TX_CTL(pipe);
2913 temp = I915_READ(reg);
627eb5a3
DV
2914 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2915 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2916 temp &= ~FDI_LINK_TRAIN_NONE;
2917 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2918 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2919
5eddb70b
CW
2920 reg = FDI_RX_CTL(pipe);
2921 temp = I915_READ(reg);
8db9d77b
ZW
2922 temp &= ~FDI_LINK_TRAIN_NONE;
2923 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2924 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2925
2926 POSTING_READ(reg);
8db9d77b
ZW
2927 udelay(150);
2928
5b2adf89 2929 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2930 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2931 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2932 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2933
5eddb70b 2934 reg = FDI_RX_IIR(pipe);
e1a44743 2935 for (tries = 0; tries < 5; tries++) {
5eddb70b 2936 temp = I915_READ(reg);
8db9d77b
ZW
2937 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2938
2939 if ((temp & FDI_RX_BIT_LOCK)) {
2940 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2941 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2942 break;
2943 }
8db9d77b 2944 }
e1a44743 2945 if (tries == 5)
5eddb70b 2946 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2947
2948 /* Train 2 */
5eddb70b
CW
2949 reg = FDI_TX_CTL(pipe);
2950 temp = I915_READ(reg);
8db9d77b
ZW
2951 temp &= ~FDI_LINK_TRAIN_NONE;
2952 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2953 I915_WRITE(reg, temp);
8db9d77b 2954
5eddb70b
CW
2955 reg = FDI_RX_CTL(pipe);
2956 temp = I915_READ(reg);
8db9d77b
ZW
2957 temp &= ~FDI_LINK_TRAIN_NONE;
2958 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2959 I915_WRITE(reg, temp);
8db9d77b 2960
5eddb70b
CW
2961 POSTING_READ(reg);
2962 udelay(150);
8db9d77b 2963
5eddb70b 2964 reg = FDI_RX_IIR(pipe);
e1a44743 2965 for (tries = 0; tries < 5; tries++) {
5eddb70b 2966 temp = I915_READ(reg);
8db9d77b
ZW
2967 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2968
2969 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2970 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2971 DRM_DEBUG_KMS("FDI train 2 done.\n");
2972 break;
2973 }
8db9d77b 2974 }
e1a44743 2975 if (tries == 5)
5eddb70b 2976 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2977
2978 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2979
8db9d77b
ZW
2980}
2981
0206e353 2982static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2983 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2984 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2985 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2986 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2987};
2988
2989/* The FDI link training functions for SNB/Cougarpoint. */
2990static void gen6_fdi_link_train(struct drm_crtc *crtc)
2991{
2992 struct drm_device *dev = crtc->dev;
2993 struct drm_i915_private *dev_priv = dev->dev_private;
2994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2995 int pipe = intel_crtc->pipe;
fa37d39e 2996 u32 reg, temp, i, retry;
8db9d77b 2997
e1a44743
AJ
2998 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2999 for train result */
5eddb70b
CW
3000 reg = FDI_RX_IMR(pipe);
3001 temp = I915_READ(reg);
e1a44743
AJ
3002 temp &= ~FDI_RX_SYMBOL_LOCK;
3003 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3004 I915_WRITE(reg, temp);
3005
3006 POSTING_READ(reg);
e1a44743
AJ
3007 udelay(150);
3008
8db9d77b 3009 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3010 reg = FDI_TX_CTL(pipe);
3011 temp = I915_READ(reg);
627eb5a3
DV
3012 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3013 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3014 temp &= ~FDI_LINK_TRAIN_NONE;
3015 temp |= FDI_LINK_TRAIN_PATTERN_1;
3016 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3017 /* SNB-B */
3018 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3019 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3020
d74cf324
DV
3021 I915_WRITE(FDI_RX_MISC(pipe),
3022 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3023
5eddb70b
CW
3024 reg = FDI_RX_CTL(pipe);
3025 temp = I915_READ(reg);
8db9d77b
ZW
3026 if (HAS_PCH_CPT(dev)) {
3027 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3028 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3029 } else {
3030 temp &= ~FDI_LINK_TRAIN_NONE;
3031 temp |= FDI_LINK_TRAIN_PATTERN_1;
3032 }
5eddb70b
CW
3033 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3034
3035 POSTING_READ(reg);
8db9d77b
ZW
3036 udelay(150);
3037
0206e353 3038 for (i = 0; i < 4; i++) {
5eddb70b
CW
3039 reg = FDI_TX_CTL(pipe);
3040 temp = I915_READ(reg);
8db9d77b
ZW
3041 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3042 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3043 I915_WRITE(reg, temp);
3044
3045 POSTING_READ(reg);
8db9d77b
ZW
3046 udelay(500);
3047
fa37d39e
SP
3048 for (retry = 0; retry < 5; retry++) {
3049 reg = FDI_RX_IIR(pipe);
3050 temp = I915_READ(reg);
3051 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3052 if (temp & FDI_RX_BIT_LOCK) {
3053 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3054 DRM_DEBUG_KMS("FDI train 1 done.\n");
3055 break;
3056 }
3057 udelay(50);
8db9d77b 3058 }
fa37d39e
SP
3059 if (retry < 5)
3060 break;
8db9d77b
ZW
3061 }
3062 if (i == 4)
5eddb70b 3063 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3064
3065 /* Train 2 */
5eddb70b
CW
3066 reg = FDI_TX_CTL(pipe);
3067 temp = I915_READ(reg);
8db9d77b
ZW
3068 temp &= ~FDI_LINK_TRAIN_NONE;
3069 temp |= FDI_LINK_TRAIN_PATTERN_2;
3070 if (IS_GEN6(dev)) {
3071 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3072 /* SNB-B */
3073 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3074 }
5eddb70b 3075 I915_WRITE(reg, temp);
8db9d77b 3076
5eddb70b
CW
3077 reg = FDI_RX_CTL(pipe);
3078 temp = I915_READ(reg);
8db9d77b
ZW
3079 if (HAS_PCH_CPT(dev)) {
3080 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3081 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3082 } else {
3083 temp &= ~FDI_LINK_TRAIN_NONE;
3084 temp |= FDI_LINK_TRAIN_PATTERN_2;
3085 }
5eddb70b
CW
3086 I915_WRITE(reg, temp);
3087
3088 POSTING_READ(reg);
8db9d77b
ZW
3089 udelay(150);
3090
0206e353 3091 for (i = 0; i < 4; i++) {
5eddb70b
CW
3092 reg = FDI_TX_CTL(pipe);
3093 temp = I915_READ(reg);
8db9d77b
ZW
3094 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3095 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3096 I915_WRITE(reg, temp);
3097
3098 POSTING_READ(reg);
8db9d77b
ZW
3099 udelay(500);
3100
fa37d39e
SP
3101 for (retry = 0; retry < 5; retry++) {
3102 reg = FDI_RX_IIR(pipe);
3103 temp = I915_READ(reg);
3104 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3105 if (temp & FDI_RX_SYMBOL_LOCK) {
3106 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3107 DRM_DEBUG_KMS("FDI train 2 done.\n");
3108 break;
3109 }
3110 udelay(50);
8db9d77b 3111 }
fa37d39e
SP
3112 if (retry < 5)
3113 break;
8db9d77b
ZW
3114 }
3115 if (i == 4)
5eddb70b 3116 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3117
3118 DRM_DEBUG_KMS("FDI train done.\n");
3119}
3120
357555c0
JB
3121/* Manual link training for Ivy Bridge A0 parts */
3122static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3123{
3124 struct drm_device *dev = crtc->dev;
3125 struct drm_i915_private *dev_priv = dev->dev_private;
3126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3127 int pipe = intel_crtc->pipe;
139ccd3f 3128 u32 reg, temp, i, j;
357555c0
JB
3129
3130 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3131 for train result */
3132 reg = FDI_RX_IMR(pipe);
3133 temp = I915_READ(reg);
3134 temp &= ~FDI_RX_SYMBOL_LOCK;
3135 temp &= ~FDI_RX_BIT_LOCK;
3136 I915_WRITE(reg, temp);
3137
3138 POSTING_READ(reg);
3139 udelay(150);
3140
01a415fd
DV
3141 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3142 I915_READ(FDI_RX_IIR(pipe)));
3143
139ccd3f
JB
3144 /* Try each vswing and preemphasis setting twice before moving on */
3145 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3146 /* disable first in case we need to retry */
3147 reg = FDI_TX_CTL(pipe);
3148 temp = I915_READ(reg);
3149 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3150 temp &= ~FDI_TX_ENABLE;
3151 I915_WRITE(reg, temp);
357555c0 3152
139ccd3f
JB
3153 reg = FDI_RX_CTL(pipe);
3154 temp = I915_READ(reg);
3155 temp &= ~FDI_LINK_TRAIN_AUTO;
3156 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3157 temp &= ~FDI_RX_ENABLE;
3158 I915_WRITE(reg, temp);
357555c0 3159
139ccd3f 3160 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3161 reg = FDI_TX_CTL(pipe);
3162 temp = I915_READ(reg);
139ccd3f
JB
3163 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3164 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3165 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3166 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3167 temp |= snb_b_fdi_train_param[j/2];
3168 temp |= FDI_COMPOSITE_SYNC;
3169 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3170
139ccd3f
JB
3171 I915_WRITE(FDI_RX_MISC(pipe),
3172 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3173
139ccd3f 3174 reg = FDI_RX_CTL(pipe);
357555c0 3175 temp = I915_READ(reg);
139ccd3f
JB
3176 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3177 temp |= FDI_COMPOSITE_SYNC;
3178 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3179
139ccd3f
JB
3180 POSTING_READ(reg);
3181 udelay(1); /* should be 0.5us */
357555c0 3182
139ccd3f
JB
3183 for (i = 0; i < 4; i++) {
3184 reg = FDI_RX_IIR(pipe);
3185 temp = I915_READ(reg);
3186 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3187
139ccd3f
JB
3188 if (temp & FDI_RX_BIT_LOCK ||
3189 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3190 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3191 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3192 i);
3193 break;
3194 }
3195 udelay(1); /* should be 0.5us */
3196 }
3197 if (i == 4) {
3198 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3199 continue;
3200 }
357555c0 3201
139ccd3f 3202 /* Train 2 */
357555c0
JB
3203 reg = FDI_TX_CTL(pipe);
3204 temp = I915_READ(reg);
139ccd3f
JB
3205 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3206 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3207 I915_WRITE(reg, temp);
3208
3209 reg = FDI_RX_CTL(pipe);
3210 temp = I915_READ(reg);
3211 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3212 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3213 I915_WRITE(reg, temp);
3214
3215 POSTING_READ(reg);
139ccd3f 3216 udelay(2); /* should be 1.5us */
357555c0 3217
139ccd3f
JB
3218 for (i = 0; i < 4; i++) {
3219 reg = FDI_RX_IIR(pipe);
3220 temp = I915_READ(reg);
3221 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3222
139ccd3f
JB
3223 if (temp & FDI_RX_SYMBOL_LOCK ||
3224 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3225 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3226 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3227 i);
3228 goto train_done;
3229 }
3230 udelay(2); /* should be 1.5us */
357555c0 3231 }
139ccd3f
JB
3232 if (i == 4)
3233 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3234 }
357555c0 3235
139ccd3f 3236train_done:
357555c0
JB
3237 DRM_DEBUG_KMS("FDI train done.\n");
3238}
3239
88cefb6c 3240static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3241{
88cefb6c 3242 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3243 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3244 int pipe = intel_crtc->pipe;
5eddb70b 3245 u32 reg, temp;
79e53945 3246
c64e311e 3247
c98e9dcf 3248 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3249 reg = FDI_RX_CTL(pipe);
3250 temp = I915_READ(reg);
627eb5a3
DV
3251 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3252 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3253 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3254 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3255
3256 POSTING_READ(reg);
c98e9dcf
JB
3257 udelay(200);
3258
3259 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3260 temp = I915_READ(reg);
3261 I915_WRITE(reg, temp | FDI_PCDCLK);
3262
3263 POSTING_READ(reg);
c98e9dcf
JB
3264 udelay(200);
3265
20749730
PZ
3266 /* Enable CPU FDI TX PLL, always on for Ironlake */
3267 reg = FDI_TX_CTL(pipe);
3268 temp = I915_READ(reg);
3269 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3270 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3271
20749730
PZ
3272 POSTING_READ(reg);
3273 udelay(100);
6be4a607 3274 }
0e23b99d
JB
3275}
3276
88cefb6c
DV
3277static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3278{
3279 struct drm_device *dev = intel_crtc->base.dev;
3280 struct drm_i915_private *dev_priv = dev->dev_private;
3281 int pipe = intel_crtc->pipe;
3282 u32 reg, temp;
3283
3284 /* Switch from PCDclk to Rawclk */
3285 reg = FDI_RX_CTL(pipe);
3286 temp = I915_READ(reg);
3287 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3288
3289 /* Disable CPU FDI TX PLL */
3290 reg = FDI_TX_CTL(pipe);
3291 temp = I915_READ(reg);
3292 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3293
3294 POSTING_READ(reg);
3295 udelay(100);
3296
3297 reg = FDI_RX_CTL(pipe);
3298 temp = I915_READ(reg);
3299 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3300
3301 /* Wait for the clocks to turn off. */
3302 POSTING_READ(reg);
3303 udelay(100);
3304}
3305
0fc932b8
JB
3306static void ironlake_fdi_disable(struct drm_crtc *crtc)
3307{
3308 struct drm_device *dev = crtc->dev;
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3311 int pipe = intel_crtc->pipe;
3312 u32 reg, temp;
3313
3314 /* disable CPU FDI tx and PCH FDI rx */
3315 reg = FDI_TX_CTL(pipe);
3316 temp = I915_READ(reg);
3317 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3318 POSTING_READ(reg);
3319
3320 reg = FDI_RX_CTL(pipe);
3321 temp = I915_READ(reg);
3322 temp &= ~(0x7 << 16);
dfd07d72 3323 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3324 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3325
3326 POSTING_READ(reg);
3327 udelay(100);
3328
3329 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3330 if (HAS_PCH_IBX(dev))
6f06ce18 3331 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3332
3333 /* still set train pattern 1 */
3334 reg = FDI_TX_CTL(pipe);
3335 temp = I915_READ(reg);
3336 temp &= ~FDI_LINK_TRAIN_NONE;
3337 temp |= FDI_LINK_TRAIN_PATTERN_1;
3338 I915_WRITE(reg, temp);
3339
3340 reg = FDI_RX_CTL(pipe);
3341 temp = I915_READ(reg);
3342 if (HAS_PCH_CPT(dev)) {
3343 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3344 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3345 } else {
3346 temp &= ~FDI_LINK_TRAIN_NONE;
3347 temp |= FDI_LINK_TRAIN_PATTERN_1;
3348 }
3349 /* BPC in FDI rx is consistent with that in PIPECONF */
3350 temp &= ~(0x07 << 16);
dfd07d72 3351 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3352 I915_WRITE(reg, temp);
3353
3354 POSTING_READ(reg);
3355 udelay(100);
3356}
3357
5dce5b93
CW
3358bool intel_has_pending_fb_unpin(struct drm_device *dev)
3359{
3360 struct intel_crtc *crtc;
3361
3362 /* Note that we don't need to be called with mode_config.lock here
3363 * as our list of CRTC objects is static for the lifetime of the
3364 * device and so cannot disappear as we iterate. Similarly, we can
3365 * happily treat the predicates as racy, atomic checks as userspace
3366 * cannot claim and pin a new fb without at least acquring the
3367 * struct_mutex and so serialising with us.
3368 */
d3fcc808 3369 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3370 if (atomic_read(&crtc->unpin_work_count) == 0)
3371 continue;
3372
3373 if (crtc->unpin_work)
3374 intel_wait_for_vblank(dev, crtc->pipe);
3375
3376 return true;
3377 }
3378
3379 return false;
3380}
3381
46a55d30 3382void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3383{
0f91128d 3384 struct drm_device *dev = crtc->dev;
5bb61643 3385 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3386
2c10d571 3387 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
eed6d67d
DV
3388 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3389 !intel_crtc_has_pending_flip(crtc),
3390 60*HZ) == 0);
5bb61643 3391
975d568a
CW
3392 if (crtc->primary->fb) {
3393 mutex_lock(&dev->struct_mutex);
3394 intel_finish_fb(crtc->primary->fb);
3395 mutex_unlock(&dev->struct_mutex);
3396 }
e6c3a2a6
CW
3397}
3398
e615efe4
ED
3399/* Program iCLKIP clock to the desired frequency */
3400static void lpt_program_iclkip(struct drm_crtc *crtc)
3401{
3402 struct drm_device *dev = crtc->dev;
3403 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3404 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3405 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3406 u32 temp;
3407
09153000
DV
3408 mutex_lock(&dev_priv->dpio_lock);
3409
e615efe4
ED
3410 /* It is necessary to ungate the pixclk gate prior to programming
3411 * the divisors, and gate it back when it is done.
3412 */
3413 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3414
3415 /* Disable SSCCTL */
3416 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3417 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3418 SBI_SSCCTL_DISABLE,
3419 SBI_ICLK);
e615efe4
ED
3420
3421 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3422 if (clock == 20000) {
e615efe4
ED
3423 auxdiv = 1;
3424 divsel = 0x41;
3425 phaseinc = 0x20;
3426 } else {
3427 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3428 * but the adjusted_mode->crtc_clock in in KHz. To get the
3429 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3430 * convert the virtual clock precision to KHz here for higher
3431 * precision.
3432 */
3433 u32 iclk_virtual_root_freq = 172800 * 1000;
3434 u32 iclk_pi_range = 64;
3435 u32 desired_divisor, msb_divisor_value, pi_value;
3436
12d7ceed 3437 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3438 msb_divisor_value = desired_divisor / iclk_pi_range;
3439 pi_value = desired_divisor % iclk_pi_range;
3440
3441 auxdiv = 0;
3442 divsel = msb_divisor_value - 2;
3443 phaseinc = pi_value;
3444 }
3445
3446 /* This should not happen with any sane values */
3447 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3448 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3449 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3450 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3451
3452 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3453 clock,
e615efe4
ED
3454 auxdiv,
3455 divsel,
3456 phasedir,
3457 phaseinc);
3458
3459 /* Program SSCDIVINTPHASE6 */
988d6ee8 3460 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3461 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3462 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3463 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3464 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3465 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3466 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3467 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3468
3469 /* Program SSCAUXDIV */
988d6ee8 3470 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3471 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3472 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3473 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3474
3475 /* Enable modulator and associated divider */
988d6ee8 3476 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3477 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3478 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3479
3480 /* Wait for initialization time */
3481 udelay(24);
3482
3483 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3484
3485 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3486}
3487
275f01b2
DV
3488static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3489 enum pipe pch_transcoder)
3490{
3491 struct drm_device *dev = crtc->base.dev;
3492 struct drm_i915_private *dev_priv = dev->dev_private;
3493 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3494
3495 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3496 I915_READ(HTOTAL(cpu_transcoder)));
3497 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3498 I915_READ(HBLANK(cpu_transcoder)));
3499 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3500 I915_READ(HSYNC(cpu_transcoder)));
3501
3502 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3503 I915_READ(VTOTAL(cpu_transcoder)));
3504 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3505 I915_READ(VBLANK(cpu_transcoder)));
3506 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3507 I915_READ(VSYNC(cpu_transcoder)));
3508 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3509 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3510}
3511
1fbc0d78
DV
3512static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3513{
3514 struct drm_i915_private *dev_priv = dev->dev_private;
3515 uint32_t temp;
3516
3517 temp = I915_READ(SOUTH_CHICKEN1);
3518 if (temp & FDI_BC_BIFURCATION_SELECT)
3519 return;
3520
3521 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3522 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3523
3524 temp |= FDI_BC_BIFURCATION_SELECT;
3525 DRM_DEBUG_KMS("enabling fdi C rx\n");
3526 I915_WRITE(SOUTH_CHICKEN1, temp);
3527 POSTING_READ(SOUTH_CHICKEN1);
3528}
3529
3530static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3531{
3532 struct drm_device *dev = intel_crtc->base.dev;
3533 struct drm_i915_private *dev_priv = dev->dev_private;
3534
3535 switch (intel_crtc->pipe) {
3536 case PIPE_A:
3537 break;
3538 case PIPE_B:
3539 if (intel_crtc->config.fdi_lanes > 2)
3540 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3541 else
3542 cpt_enable_fdi_bc_bifurcation(dev);
3543
3544 break;
3545 case PIPE_C:
3546 cpt_enable_fdi_bc_bifurcation(dev);
3547
3548 break;
3549 default:
3550 BUG();
3551 }
3552}
3553
f67a559d
JB
3554/*
3555 * Enable PCH resources required for PCH ports:
3556 * - PCH PLLs
3557 * - FDI training & RX/TX
3558 * - update transcoder timings
3559 * - DP transcoding bits
3560 * - transcoder
3561 */
3562static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3563{
3564 struct drm_device *dev = crtc->dev;
3565 struct drm_i915_private *dev_priv = dev->dev_private;
3566 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3567 int pipe = intel_crtc->pipe;
ee7b9f93 3568 u32 reg, temp;
2c07245f 3569
ab9412ba 3570 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3571
1fbc0d78
DV
3572 if (IS_IVYBRIDGE(dev))
3573 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3574
cd986abb
DV
3575 /* Write the TU size bits before fdi link training, so that error
3576 * detection works. */
3577 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3578 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3579
c98e9dcf 3580 /* For PCH output, training FDI link */
674cf967 3581 dev_priv->display.fdi_link_train(crtc);
2c07245f 3582
3ad8a208
DV
3583 /* We need to program the right clock selection before writing the pixel
3584 * mutliplier into the DPLL. */
303b81e0 3585 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3586 u32 sel;
4b645f14 3587
c98e9dcf 3588 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3589 temp |= TRANS_DPLL_ENABLE(pipe);
3590 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3591 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3592 temp |= sel;
3593 else
3594 temp &= ~sel;
c98e9dcf 3595 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3596 }
5eddb70b 3597
3ad8a208
DV
3598 /* XXX: pch pll's can be enabled any time before we enable the PCH
3599 * transcoder, and we actually should do this to not upset any PCH
3600 * transcoder that already use the clock when we share it.
3601 *
3602 * Note that enable_shared_dpll tries to do the right thing, but
3603 * get_shared_dpll unconditionally resets the pll - we need that to have
3604 * the right LVDS enable sequence. */
85b3894f 3605 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3606
d9b6cb56
JB
3607 /* set transcoder timing, panel must allow it */
3608 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3609 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3610
303b81e0 3611 intel_fdi_normal_train(crtc);
5e84e1a4 3612
c98e9dcf
JB
3613 /* For PCH DP, enable TRANS_DP_CTL */
3614 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3615 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3616 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3617 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3618 reg = TRANS_DP_CTL(pipe);
3619 temp = I915_READ(reg);
3620 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3621 TRANS_DP_SYNC_MASK |
3622 TRANS_DP_BPC_MASK);
5eddb70b
CW
3623 temp |= (TRANS_DP_OUTPUT_ENABLE |
3624 TRANS_DP_ENH_FRAMING);
9325c9f0 3625 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3626
3627 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3628 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3629 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3630 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3631
3632 switch (intel_trans_dp_port_sel(crtc)) {
3633 case PCH_DP_B:
5eddb70b 3634 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3635 break;
3636 case PCH_DP_C:
5eddb70b 3637 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3638 break;
3639 case PCH_DP_D:
5eddb70b 3640 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3641 break;
3642 default:
e95d41e1 3643 BUG();
32f9d658 3644 }
2c07245f 3645
5eddb70b 3646 I915_WRITE(reg, temp);
6be4a607 3647 }
b52eb4dc 3648
b8a4f404 3649 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3650}
3651
1507e5bd
PZ
3652static void lpt_pch_enable(struct drm_crtc *crtc)
3653{
3654 struct drm_device *dev = crtc->dev;
3655 struct drm_i915_private *dev_priv = dev->dev_private;
3656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3657 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3658
ab9412ba 3659 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3660
8c52b5e8 3661 lpt_program_iclkip(crtc);
1507e5bd 3662
0540e488 3663 /* Set transcoder timing. */
275f01b2 3664 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3665
937bb610 3666 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3667}
3668
716c2e55 3669void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3670{
e2b78267 3671 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3672
3673 if (pll == NULL)
3674 return;
3675
3676 if (pll->refcount == 0) {
46edb027 3677 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3678 return;
3679 }
3680
f4a091c7
DV
3681 if (--pll->refcount == 0) {
3682 WARN_ON(pll->on);
3683 WARN_ON(pll->active);
3684 }
3685
a43f6e0f 3686 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3687}
3688
716c2e55 3689struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3690{
e2b78267
DV
3691 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3692 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3693 enum intel_dpll_id i;
ee7b9f93 3694
ee7b9f93 3695 if (pll) {
46edb027
DV
3696 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3697 crtc->base.base.id, pll->name);
e2b78267 3698 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3699 }
3700
98b6bd99
DV
3701 if (HAS_PCH_IBX(dev_priv->dev)) {
3702 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3703 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3704 pll = &dev_priv->shared_dplls[i];
98b6bd99 3705
46edb027
DV
3706 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3707 crtc->base.base.id, pll->name);
98b6bd99 3708
f2a69f44
DV
3709 WARN_ON(pll->refcount);
3710
98b6bd99
DV
3711 goto found;
3712 }
3713
e72f9fbf
DV
3714 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3715 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3716
3717 /* Only want to check enabled timings first */
3718 if (pll->refcount == 0)
3719 continue;
3720
b89a1d39
DV
3721 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3722 sizeof(pll->hw_state)) == 0) {
46edb027 3723 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3724 crtc->base.base.id,
46edb027 3725 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3726
3727 goto found;
3728 }
3729 }
3730
3731 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3732 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3733 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3734 if (pll->refcount == 0) {
46edb027
DV
3735 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3736 crtc->base.base.id, pll->name);
ee7b9f93
JB
3737 goto found;
3738 }
3739 }
3740
3741 return NULL;
3742
3743found:
f2a69f44
DV
3744 if (pll->refcount == 0)
3745 pll->hw_state = crtc->config.dpll_hw_state;
3746
a43f6e0f 3747 crtc->config.shared_dpll = i;
46edb027
DV
3748 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3749 pipe_name(crtc->pipe));
ee7b9f93 3750
cdbd2316 3751 pll->refcount++;
e04c7350 3752
ee7b9f93
JB
3753 return pll;
3754}
3755
a1520318 3756static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3757{
3758 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3759 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3760 u32 temp;
3761
3762 temp = I915_READ(dslreg);
3763 udelay(500);
3764 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3765 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3766 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3767 }
3768}
3769
b074cec8
JB
3770static void ironlake_pfit_enable(struct intel_crtc *crtc)
3771{
3772 struct drm_device *dev = crtc->base.dev;
3773 struct drm_i915_private *dev_priv = dev->dev_private;
3774 int pipe = crtc->pipe;
3775
fd4daa9c 3776 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3777 /* Force use of hard-coded filter coefficients
3778 * as some pre-programmed values are broken,
3779 * e.g. x201.
3780 */
3781 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3782 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3783 PF_PIPE_SEL_IVB(pipe));
3784 else
3785 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3786 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3787 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3788 }
3789}
3790
bb53d4ae
VS
3791static void intel_enable_planes(struct drm_crtc *crtc)
3792{
3793 struct drm_device *dev = crtc->dev;
3794 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3795 struct drm_plane *plane;
bb53d4ae
VS
3796 struct intel_plane *intel_plane;
3797
af2b653b
MR
3798 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3799 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3800 if (intel_plane->pipe == pipe)
3801 intel_plane_restore(&intel_plane->base);
af2b653b 3802 }
bb53d4ae
VS
3803}
3804
3805static void intel_disable_planes(struct drm_crtc *crtc)
3806{
3807 struct drm_device *dev = crtc->dev;
3808 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3809 struct drm_plane *plane;
bb53d4ae
VS
3810 struct intel_plane *intel_plane;
3811
af2b653b
MR
3812 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3813 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3814 if (intel_plane->pipe == pipe)
3815 intel_plane_disable(&intel_plane->base);
af2b653b 3816 }
bb53d4ae
VS
3817}
3818
20bc8673 3819void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3820{
cea165c3
VS
3821 struct drm_device *dev = crtc->base.dev;
3822 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3823
3824 if (!crtc->config.ips_enabled)
3825 return;
3826
cea165c3
VS
3827 /* We can only enable IPS after we enable a plane and wait for a vblank */
3828 intel_wait_for_vblank(dev, crtc->pipe);
3829
d77e4531 3830 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3831 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3832 mutex_lock(&dev_priv->rps.hw_lock);
3833 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3834 mutex_unlock(&dev_priv->rps.hw_lock);
3835 /* Quoting Art Runyan: "its not safe to expect any particular
3836 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3837 * mailbox." Moreover, the mailbox may return a bogus state,
3838 * so we need to just enable it and continue on.
2a114cc1
BW
3839 */
3840 } else {
3841 I915_WRITE(IPS_CTL, IPS_ENABLE);
3842 /* The bit only becomes 1 in the next vblank, so this wait here
3843 * is essentially intel_wait_for_vblank. If we don't have this
3844 * and don't wait for vblanks until the end of crtc_enable, then
3845 * the HW state readout code will complain that the expected
3846 * IPS_CTL value is not the one we read. */
3847 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3848 DRM_ERROR("Timed out waiting for IPS enable\n");
3849 }
d77e4531
PZ
3850}
3851
20bc8673 3852void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3853{
3854 struct drm_device *dev = crtc->base.dev;
3855 struct drm_i915_private *dev_priv = dev->dev_private;
3856
3857 if (!crtc->config.ips_enabled)
3858 return;
3859
3860 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3861 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3862 mutex_lock(&dev_priv->rps.hw_lock);
3863 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3864 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3865 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3866 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3867 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3868 } else {
2a114cc1 3869 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3870 POSTING_READ(IPS_CTL);
3871 }
d77e4531
PZ
3872
3873 /* We need to wait for a vblank before we can disable the plane. */
3874 intel_wait_for_vblank(dev, crtc->pipe);
3875}
3876
3877/** Loads the palette/gamma unit for the CRTC with the prepared values */
3878static void intel_crtc_load_lut(struct drm_crtc *crtc)
3879{
3880 struct drm_device *dev = crtc->dev;
3881 struct drm_i915_private *dev_priv = dev->dev_private;
3882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3883 enum pipe pipe = intel_crtc->pipe;
3884 int palreg = PALETTE(pipe);
3885 int i;
3886 bool reenable_ips = false;
3887
3888 /* The clocks have to be on to load the palette. */
3889 if (!crtc->enabled || !intel_crtc->active)
3890 return;
3891
3892 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3893 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3894 assert_dsi_pll_enabled(dev_priv);
3895 else
3896 assert_pll_enabled(dev_priv, pipe);
3897 }
3898
3899 /* use legacy palette for Ironlake */
7a1db49a 3900 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
3901 palreg = LGC_PALETTE(pipe);
3902
3903 /* Workaround : Do not read or write the pipe palette/gamma data while
3904 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3905 */
41e6fc4c 3906 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3907 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3908 GAMMA_MODE_MODE_SPLIT)) {
3909 hsw_disable_ips(intel_crtc);
3910 reenable_ips = true;
3911 }
3912
3913 for (i = 0; i < 256; i++) {
3914 I915_WRITE(palreg + 4 * i,
3915 (intel_crtc->lut_r[i] << 16) |
3916 (intel_crtc->lut_g[i] << 8) |
3917 intel_crtc->lut_b[i]);
3918 }
3919
3920 if (reenable_ips)
3921 hsw_enable_ips(intel_crtc);
3922}
3923
d3eedb1a
VS
3924static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3925{
3926 if (!enable && intel_crtc->overlay) {
3927 struct drm_device *dev = intel_crtc->base.dev;
3928 struct drm_i915_private *dev_priv = dev->dev_private;
3929
3930 mutex_lock(&dev->struct_mutex);
3931 dev_priv->mm.interruptible = false;
3932 (void) intel_overlay_switch_off(intel_crtc->overlay);
3933 dev_priv->mm.interruptible = true;
3934 mutex_unlock(&dev->struct_mutex);
3935 }
3936
3937 /* Let userspace switch the overlay on again. In most cases userspace
3938 * has to recompute where to put it anyway.
3939 */
3940}
3941
d3eedb1a 3942static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3943{
3944 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
3945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3946 int pipe = intel_crtc->pipe;
a5c4d7bc 3947
f98551ae
VS
3948 drm_vblank_on(dev, pipe);
3949
fdd508a6 3950 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
3951 intel_enable_planes(crtc);
3952 intel_crtc_update_cursor(crtc, true);
d3eedb1a 3953 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
3954
3955 hsw_enable_ips(intel_crtc);
3956
3957 mutex_lock(&dev->struct_mutex);
3958 intel_update_fbc(dev);
3959 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
3960
3961 /*
3962 * FIXME: Once we grow proper nuclear flip support out of this we need
3963 * to compute the mask of flip planes precisely. For the time being
3964 * consider this a flip from a NULL plane.
3965 */
3966 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
3967}
3968
d3eedb1a 3969static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3970{
3971 struct drm_device *dev = crtc->dev;
3972 struct drm_i915_private *dev_priv = dev->dev_private;
3973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3974 int pipe = intel_crtc->pipe;
3975 int plane = intel_crtc->plane;
3976
3977 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
3978
3979 if (dev_priv->fbc.plane == plane)
3980 intel_disable_fbc(dev);
3981
3982 hsw_disable_ips(intel_crtc);
3983
d3eedb1a 3984 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
3985 intel_crtc_update_cursor(crtc, false);
3986 intel_disable_planes(crtc);
fdd508a6 3987 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 3988
f99d7069
DV
3989 /*
3990 * FIXME: Once we grow proper nuclear flip support out of this we need
3991 * to compute the mask of flip planes precisely. For the time being
3992 * consider this a flip to a NULL plane.
3993 */
3994 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
3995
f98551ae 3996 drm_vblank_off(dev, pipe);
a5c4d7bc
VS
3997}
3998
f67a559d
JB
3999static void ironlake_crtc_enable(struct drm_crtc *crtc)
4000{
4001 struct drm_device *dev = crtc->dev;
4002 struct drm_i915_private *dev_priv = dev->dev_private;
4003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4004 struct intel_encoder *encoder;
f67a559d 4005 int pipe = intel_crtc->pipe;
f67a559d 4006
08a48469
DV
4007 WARN_ON(!crtc->enabled);
4008
f67a559d
JB
4009 if (intel_crtc->active)
4010 return;
4011
b14b1055
DV
4012 if (intel_crtc->config.has_pch_encoder)
4013 intel_prepare_shared_dpll(intel_crtc);
4014
29407aab
DV
4015 if (intel_crtc->config.has_dp_encoder)
4016 intel_dp_set_m_n(intel_crtc);
4017
4018 intel_set_pipe_timings(intel_crtc);
4019
4020 if (intel_crtc->config.has_pch_encoder) {
4021 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4022 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4023 }
4024
4025 ironlake_set_pipeconf(crtc);
4026
f67a559d 4027 intel_crtc->active = true;
8664281b
PZ
4028
4029 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4030 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4031
f6736a1a 4032 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4033 if (encoder->pre_enable)
4034 encoder->pre_enable(encoder);
f67a559d 4035
5bfe2ac0 4036 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4037 /* Note: FDI PLL enabling _must_ be done before we enable the
4038 * cpu pipes, hence this is separate from all the other fdi/pch
4039 * enabling. */
88cefb6c 4040 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4041 } else {
4042 assert_fdi_tx_disabled(dev_priv, pipe);
4043 assert_fdi_rx_disabled(dev_priv, pipe);
4044 }
f67a559d 4045
b074cec8 4046 ironlake_pfit_enable(intel_crtc);
f67a559d 4047
9c54c0dd
JB
4048 /*
4049 * On ILK+ LUT must be loaded before the pipe is running but with
4050 * clocks enabled
4051 */
4052 intel_crtc_load_lut(crtc);
4053
f37fcc2a 4054 intel_update_watermarks(crtc);
e1fdc473 4055 intel_enable_pipe(intel_crtc);
f67a559d 4056
5bfe2ac0 4057 if (intel_crtc->config.has_pch_encoder)
f67a559d 4058 ironlake_pch_enable(crtc);
c98e9dcf 4059
fa5c73b1
DV
4060 for_each_encoder_on_crtc(dev, crtc, encoder)
4061 encoder->enable(encoder);
61b77ddd
DV
4062
4063 if (HAS_PCH_CPT(dev))
a1520318 4064 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4065
d3eedb1a 4066 intel_crtc_enable_planes(crtc);
6be4a607
JB
4067}
4068
42db64ef
PZ
4069/* IPS only exists on ULT machines and is tied to pipe A. */
4070static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4071{
f5adf94e 4072 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4073}
4074
e4916946
PZ
4075/*
4076 * This implements the workaround described in the "notes" section of the mode
4077 * set sequence documentation. When going from no pipes or single pipe to
4078 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4079 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4080 */
4081static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4082{
4083 struct drm_device *dev = crtc->base.dev;
4084 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4085
4086 /* We want to get the other_active_crtc only if there's only 1 other
4087 * active crtc. */
d3fcc808 4088 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4089 if (!crtc_it->active || crtc_it == crtc)
4090 continue;
4091
4092 if (other_active_crtc)
4093 return;
4094
4095 other_active_crtc = crtc_it;
4096 }
4097 if (!other_active_crtc)
4098 return;
4099
4100 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4101 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4102}
4103
4f771f10
PZ
4104static void haswell_crtc_enable(struct drm_crtc *crtc)
4105{
4106 struct drm_device *dev = crtc->dev;
4107 struct drm_i915_private *dev_priv = dev->dev_private;
4108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4109 struct intel_encoder *encoder;
4110 int pipe = intel_crtc->pipe;
4f771f10
PZ
4111
4112 WARN_ON(!crtc->enabled);
4113
4114 if (intel_crtc->active)
4115 return;
4116
df8ad70c
DV
4117 if (intel_crtc_to_shared_dpll(intel_crtc))
4118 intel_enable_shared_dpll(intel_crtc);
4119
229fca97
DV
4120 if (intel_crtc->config.has_dp_encoder)
4121 intel_dp_set_m_n(intel_crtc);
4122
4123 intel_set_pipe_timings(intel_crtc);
4124
4125 if (intel_crtc->config.has_pch_encoder) {
4126 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4127 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4128 }
4129
4130 haswell_set_pipeconf(crtc);
4131
4132 intel_set_pipe_csc(crtc);
4133
4f771f10 4134 intel_crtc->active = true;
8664281b
PZ
4135
4136 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4f771f10
PZ
4137 for_each_encoder_on_crtc(dev, crtc, encoder)
4138 if (encoder->pre_enable)
4139 encoder->pre_enable(encoder);
4140
4fe9467d
ID
4141 if (intel_crtc->config.has_pch_encoder) {
4142 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4143 dev_priv->display.fdi_link_train(crtc);
4144 }
4145
1f544388 4146 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4147
b074cec8 4148 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4149
4150 /*
4151 * On ILK+ LUT must be loaded before the pipe is running but with
4152 * clocks enabled
4153 */
4154 intel_crtc_load_lut(crtc);
4155
1f544388 4156 intel_ddi_set_pipe_settings(crtc);
8228c251 4157 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4158
f37fcc2a 4159 intel_update_watermarks(crtc);
e1fdc473 4160 intel_enable_pipe(intel_crtc);
42db64ef 4161
5bfe2ac0 4162 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4163 lpt_pch_enable(crtc);
4f771f10 4164
0e32b39c
DA
4165 if (intel_crtc->config.dp_encoder_is_mst)
4166 intel_ddi_set_vc_payload_alloc(crtc, true);
4167
8807e55b 4168 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4169 encoder->enable(encoder);
8807e55b
JN
4170 intel_opregion_notify_encoder(encoder, true);
4171 }
4f771f10 4172
e4916946
PZ
4173 /* If we change the relative order between pipe/planes enabling, we need
4174 * to change the workaround. */
4175 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4176 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4177}
4178
3f8dce3a
DV
4179static void ironlake_pfit_disable(struct intel_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->base.dev;
4182 struct drm_i915_private *dev_priv = dev->dev_private;
4183 int pipe = crtc->pipe;
4184
4185 /* To avoid upsetting the power well on haswell only disable the pfit if
4186 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4187 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4188 I915_WRITE(PF_CTL(pipe), 0);
4189 I915_WRITE(PF_WIN_POS(pipe), 0);
4190 I915_WRITE(PF_WIN_SZ(pipe), 0);
4191 }
4192}
4193
6be4a607
JB
4194static void ironlake_crtc_disable(struct drm_crtc *crtc)
4195{
4196 struct drm_device *dev = crtc->dev;
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4199 struct intel_encoder *encoder;
6be4a607 4200 int pipe = intel_crtc->pipe;
5eddb70b 4201 u32 reg, temp;
b52eb4dc 4202
f7abfe8b
CW
4203 if (!intel_crtc->active)
4204 return;
4205
d3eedb1a 4206 intel_crtc_disable_planes(crtc);
a5c4d7bc 4207
ea9d758d
DV
4208 for_each_encoder_on_crtc(dev, crtc, encoder)
4209 encoder->disable(encoder);
4210
d925c59a
DV
4211 if (intel_crtc->config.has_pch_encoder)
4212 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4213
b24e7179 4214 intel_disable_pipe(dev_priv, pipe);
32f9d658 4215
0e32b39c
DA
4216 if (intel_crtc->config.dp_encoder_is_mst)
4217 intel_ddi_set_vc_payload_alloc(crtc, false);
4218
3f8dce3a 4219 ironlake_pfit_disable(intel_crtc);
2c07245f 4220
bf49ec8c
DV
4221 for_each_encoder_on_crtc(dev, crtc, encoder)
4222 if (encoder->post_disable)
4223 encoder->post_disable(encoder);
2c07245f 4224
d925c59a
DV
4225 if (intel_crtc->config.has_pch_encoder) {
4226 ironlake_fdi_disable(crtc);
913d8d11 4227
d925c59a
DV
4228 ironlake_disable_pch_transcoder(dev_priv, pipe);
4229 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4230
d925c59a
DV
4231 if (HAS_PCH_CPT(dev)) {
4232 /* disable TRANS_DP_CTL */
4233 reg = TRANS_DP_CTL(pipe);
4234 temp = I915_READ(reg);
4235 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4236 TRANS_DP_PORT_SEL_MASK);
4237 temp |= TRANS_DP_PORT_SEL_NONE;
4238 I915_WRITE(reg, temp);
4239
4240 /* disable DPLL_SEL */
4241 temp = I915_READ(PCH_DPLL_SEL);
11887397 4242 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4243 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4244 }
e3421a18 4245
d925c59a 4246 /* disable PCH DPLL */
e72f9fbf 4247 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4248
d925c59a
DV
4249 ironlake_fdi_pll_disable(intel_crtc);
4250 }
6b383a7f 4251
f7abfe8b 4252 intel_crtc->active = false;
46ba614c 4253 intel_update_watermarks(crtc);
d1ebd816
BW
4254
4255 mutex_lock(&dev->struct_mutex);
6b383a7f 4256 intel_update_fbc(dev);
d1ebd816 4257 mutex_unlock(&dev->struct_mutex);
6be4a607 4258}
1b3c7a47 4259
4f771f10 4260static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4261{
4f771f10
PZ
4262 struct drm_device *dev = crtc->dev;
4263 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
4265 struct intel_encoder *encoder;
4266 int pipe = intel_crtc->pipe;
3b117c8f 4267 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4268
4f771f10
PZ
4269 if (!intel_crtc->active)
4270 return;
4271
d3eedb1a 4272 intel_crtc_disable_planes(crtc);
dda9a66a 4273
8807e55b
JN
4274 for_each_encoder_on_crtc(dev, crtc, encoder) {
4275 intel_opregion_notify_encoder(encoder, false);
4f771f10 4276 encoder->disable(encoder);
8807e55b 4277 }
4f771f10 4278
8664281b
PZ
4279 if (intel_crtc->config.has_pch_encoder)
4280 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
4281 intel_disable_pipe(dev_priv, pipe);
4282
ad80a810 4283 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4284
3f8dce3a 4285 ironlake_pfit_disable(intel_crtc);
4f771f10 4286
1f544388 4287 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4288
88adfff1 4289 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4290 lpt_disable_pch_transcoder(dev_priv);
8664281b 4291 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4292 intel_ddi_fdi_disable(crtc);
83616634 4293 }
4f771f10 4294
97b040aa
ID
4295 for_each_encoder_on_crtc(dev, crtc, encoder)
4296 if (encoder->post_disable)
4297 encoder->post_disable(encoder);
4298
4f771f10 4299 intel_crtc->active = false;
46ba614c 4300 intel_update_watermarks(crtc);
4f771f10
PZ
4301
4302 mutex_lock(&dev->struct_mutex);
4303 intel_update_fbc(dev);
4304 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4305
4306 if (intel_crtc_to_shared_dpll(intel_crtc))
4307 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4308}
4309
ee7b9f93
JB
4310static void ironlake_crtc_off(struct drm_crtc *crtc)
4311{
4312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4313 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4314}
4315
6441ab5f 4316
2dd24552
JB
4317static void i9xx_pfit_enable(struct intel_crtc *crtc)
4318{
4319 struct drm_device *dev = crtc->base.dev;
4320 struct drm_i915_private *dev_priv = dev->dev_private;
4321 struct intel_crtc_config *pipe_config = &crtc->config;
4322
328d8e82 4323 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4324 return;
4325
2dd24552 4326 /*
c0b03411
DV
4327 * The panel fitter should only be adjusted whilst the pipe is disabled,
4328 * according to register description and PRM.
2dd24552 4329 */
c0b03411
DV
4330 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4331 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4332
b074cec8
JB
4333 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4334 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4335
4336 /* Border color in case we don't scale up to the full screen. Black by
4337 * default, change to something else for debugging. */
4338 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4339}
4340
d05410f9
DA
4341static enum intel_display_power_domain port_to_power_domain(enum port port)
4342{
4343 switch (port) {
4344 case PORT_A:
4345 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4346 case PORT_B:
4347 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4348 case PORT_C:
4349 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4350 case PORT_D:
4351 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4352 default:
4353 WARN_ON_ONCE(1);
4354 return POWER_DOMAIN_PORT_OTHER;
4355 }
4356}
4357
77d22dca
ID
4358#define for_each_power_domain(domain, mask) \
4359 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4360 if ((1 << (domain)) & (mask))
4361
319be8ae
ID
4362enum intel_display_power_domain
4363intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4364{
4365 struct drm_device *dev = intel_encoder->base.dev;
4366 struct intel_digital_port *intel_dig_port;
4367
4368 switch (intel_encoder->type) {
4369 case INTEL_OUTPUT_UNKNOWN:
4370 /* Only DDI platforms should ever use this output type */
4371 WARN_ON_ONCE(!HAS_DDI(dev));
4372 case INTEL_OUTPUT_DISPLAYPORT:
4373 case INTEL_OUTPUT_HDMI:
4374 case INTEL_OUTPUT_EDP:
4375 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4376 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4377 case INTEL_OUTPUT_DP_MST:
4378 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4379 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4380 case INTEL_OUTPUT_ANALOG:
4381 return POWER_DOMAIN_PORT_CRT;
4382 case INTEL_OUTPUT_DSI:
4383 return POWER_DOMAIN_PORT_DSI;
4384 default:
4385 return POWER_DOMAIN_PORT_OTHER;
4386 }
4387}
4388
4389static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4390{
319be8ae
ID
4391 struct drm_device *dev = crtc->dev;
4392 struct intel_encoder *intel_encoder;
4393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4394 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4395 unsigned long mask;
4396 enum transcoder transcoder;
4397
4398 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4399
4400 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4401 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4402 if (intel_crtc->config.pch_pfit.enabled ||
4403 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4404 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4405
319be8ae
ID
4406 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4407 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4408
77d22dca
ID
4409 return mask;
4410}
4411
4412void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4413 bool enable)
4414{
4415 if (dev_priv->power_domains.init_power_on == enable)
4416 return;
4417
4418 if (enable)
4419 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4420 else
4421 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4422
4423 dev_priv->power_domains.init_power_on = enable;
4424}
4425
4426static void modeset_update_crtc_power_domains(struct drm_device *dev)
4427{
4428 struct drm_i915_private *dev_priv = dev->dev_private;
4429 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4430 struct intel_crtc *crtc;
4431
4432 /*
4433 * First get all needed power domains, then put all unneeded, to avoid
4434 * any unnecessary toggling of the power wells.
4435 */
d3fcc808 4436 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4437 enum intel_display_power_domain domain;
4438
4439 if (!crtc->base.enabled)
4440 continue;
4441
319be8ae 4442 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4443
4444 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4445 intel_display_power_get(dev_priv, domain);
4446 }
4447
d3fcc808 4448 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4449 enum intel_display_power_domain domain;
4450
4451 for_each_power_domain(domain, crtc->enabled_power_domains)
4452 intel_display_power_put(dev_priv, domain);
4453
4454 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4455 }
4456
4457 intel_display_set_init_power(dev_priv, false);
4458}
4459
dfcab17e 4460/* returns HPLL frequency in kHz */
f8bf63fd 4461static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4462{
586f49dc 4463 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4464
586f49dc
JB
4465 /* Obtain SKU information */
4466 mutex_lock(&dev_priv->dpio_lock);
4467 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4468 CCK_FUSE_HPLL_FREQ_MASK;
4469 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4470
dfcab17e 4471 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4472}
4473
f8bf63fd
VS
4474static void vlv_update_cdclk(struct drm_device *dev)
4475{
4476 struct drm_i915_private *dev_priv = dev->dev_private;
4477
4478 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4479 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4480 dev_priv->vlv_cdclk_freq);
4481
4482 /*
4483 * Program the gmbus_freq based on the cdclk frequency.
4484 * BSpec erroneously claims we should aim for 4MHz, but
4485 * in fact 1MHz is the correct frequency.
4486 */
4487 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4488}
4489
30a970c6
JB
4490/* Adjust CDclk dividers to allow high res or save power if possible */
4491static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4492{
4493 struct drm_i915_private *dev_priv = dev->dev_private;
4494 u32 val, cmd;
4495
d197b7d3 4496 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4497
dfcab17e 4498 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4499 cmd = 2;
dfcab17e 4500 else if (cdclk == 266667)
30a970c6
JB
4501 cmd = 1;
4502 else
4503 cmd = 0;
4504
4505 mutex_lock(&dev_priv->rps.hw_lock);
4506 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4507 val &= ~DSPFREQGUAR_MASK;
4508 val |= (cmd << DSPFREQGUAR_SHIFT);
4509 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4510 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4511 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4512 50)) {
4513 DRM_ERROR("timed out waiting for CDclk change\n");
4514 }
4515 mutex_unlock(&dev_priv->rps.hw_lock);
4516
dfcab17e 4517 if (cdclk == 400000) {
30a970c6
JB
4518 u32 divider, vco;
4519
4520 vco = valleyview_get_vco(dev_priv);
dfcab17e 4521 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4522
4523 mutex_lock(&dev_priv->dpio_lock);
4524 /* adjust cdclk divider */
4525 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4526 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4527 val |= divider;
4528 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4529
4530 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4531 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4532 50))
4533 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4534 mutex_unlock(&dev_priv->dpio_lock);
4535 }
4536
4537 mutex_lock(&dev_priv->dpio_lock);
4538 /* adjust self-refresh exit latency value */
4539 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4540 val &= ~0x7f;
4541
4542 /*
4543 * For high bandwidth configs, we set a higher latency in the bunit
4544 * so that the core display fetch happens in time to avoid underruns.
4545 */
dfcab17e 4546 if (cdclk == 400000)
30a970c6
JB
4547 val |= 4500 / 250; /* 4.5 usec */
4548 else
4549 val |= 3000 / 250; /* 3.0 usec */
4550 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4551 mutex_unlock(&dev_priv->dpio_lock);
4552
f8bf63fd 4553 vlv_update_cdclk(dev);
30a970c6
JB
4554}
4555
383c5a6a
VS
4556static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4557{
4558 struct drm_i915_private *dev_priv = dev->dev_private;
4559 u32 val, cmd;
4560
4561 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4562
4563 switch (cdclk) {
4564 case 400000:
4565 cmd = 3;
4566 break;
4567 case 333333:
4568 case 320000:
4569 cmd = 2;
4570 break;
4571 case 266667:
4572 cmd = 1;
4573 break;
4574 case 200000:
4575 cmd = 0;
4576 break;
4577 default:
4578 WARN_ON(1);
4579 return;
4580 }
4581
4582 mutex_lock(&dev_priv->rps.hw_lock);
4583 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4584 val &= ~DSPFREQGUAR_MASK_CHV;
4585 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4586 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4587 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4588 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4589 50)) {
4590 DRM_ERROR("timed out waiting for CDclk change\n");
4591 }
4592 mutex_unlock(&dev_priv->rps.hw_lock);
4593
4594 vlv_update_cdclk(dev);
4595}
4596
30a970c6
JB
4597static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4598 int max_pixclk)
4599{
29dc7ef3
VS
4600 int vco = valleyview_get_vco(dev_priv);
4601 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4602
d49a340d
VS
4603 /* FIXME: Punit isn't quite ready yet */
4604 if (IS_CHERRYVIEW(dev_priv->dev))
4605 return 400000;
4606
30a970c6
JB
4607 /*
4608 * Really only a few cases to deal with, as only 4 CDclks are supported:
4609 * 200MHz
4610 * 267MHz
29dc7ef3 4611 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4612 * 400MHz
4613 * So we check to see whether we're above 90% of the lower bin and
4614 * adjust if needed.
e37c67a1
VS
4615 *
4616 * We seem to get an unstable or solid color picture at 200MHz.
4617 * Not sure what's wrong. For now use 200MHz only when all pipes
4618 * are off.
30a970c6 4619 */
29dc7ef3 4620 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4621 return 400000;
4622 else if (max_pixclk > 266667*9/10)
29dc7ef3 4623 return freq_320;
e37c67a1 4624 else if (max_pixclk > 0)
dfcab17e 4625 return 266667;
e37c67a1
VS
4626 else
4627 return 200000;
30a970c6
JB
4628}
4629
2f2d7aa1
VS
4630/* compute the max pixel clock for new configuration */
4631static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4632{
4633 struct drm_device *dev = dev_priv->dev;
4634 struct intel_crtc *intel_crtc;
4635 int max_pixclk = 0;
4636
d3fcc808 4637 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4638 if (intel_crtc->new_enabled)
30a970c6 4639 max_pixclk = max(max_pixclk,
2f2d7aa1 4640 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4641 }
4642
4643 return max_pixclk;
4644}
4645
4646static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4647 unsigned *prepare_pipes)
30a970c6
JB
4648{
4649 struct drm_i915_private *dev_priv = dev->dev_private;
4650 struct intel_crtc *intel_crtc;
2f2d7aa1 4651 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4652
d60c4473
ID
4653 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4654 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4655 return;
4656
2f2d7aa1 4657 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4658 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4659 if (intel_crtc->base.enabled)
4660 *prepare_pipes |= (1 << intel_crtc->pipe);
4661}
4662
4663static void valleyview_modeset_global_resources(struct drm_device *dev)
4664{
4665 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4666 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4667 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4668
383c5a6a
VS
4669 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4670 if (IS_CHERRYVIEW(dev))
4671 cherryview_set_cdclk(dev, req_cdclk);
4672 else
4673 valleyview_set_cdclk(dev, req_cdclk);
4674 }
4675
77961eb9 4676 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4677}
4678
89b667f8
JB
4679static void valleyview_crtc_enable(struct drm_crtc *crtc)
4680{
4681 struct drm_device *dev = crtc->dev;
89b667f8
JB
4682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4683 struct intel_encoder *encoder;
4684 int pipe = intel_crtc->pipe;
23538ef1 4685 bool is_dsi;
89b667f8
JB
4686
4687 WARN_ON(!crtc->enabled);
4688
4689 if (intel_crtc->active)
4690 return;
4691
8525a235
SK
4692 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4693
1ae0d137
VS
4694 if (!is_dsi) {
4695 if (IS_CHERRYVIEW(dev))
4696 chv_prepare_pll(intel_crtc);
4697 else
4698 vlv_prepare_pll(intel_crtc);
4699 }
bdd4b6a6 4700
5b18e57c
DV
4701 if (intel_crtc->config.has_dp_encoder)
4702 intel_dp_set_m_n(intel_crtc);
4703
4704 intel_set_pipe_timings(intel_crtc);
4705
5b18e57c
DV
4706 i9xx_set_pipeconf(intel_crtc);
4707
89b667f8 4708 intel_crtc->active = true;
89b667f8 4709
4a3436e8
VS
4710 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4711
89b667f8
JB
4712 for_each_encoder_on_crtc(dev, crtc, encoder)
4713 if (encoder->pre_pll_enable)
4714 encoder->pre_pll_enable(encoder);
4715
9d556c99
CML
4716 if (!is_dsi) {
4717 if (IS_CHERRYVIEW(dev))
4718 chv_enable_pll(intel_crtc);
4719 else
4720 vlv_enable_pll(intel_crtc);
4721 }
89b667f8
JB
4722
4723 for_each_encoder_on_crtc(dev, crtc, encoder)
4724 if (encoder->pre_enable)
4725 encoder->pre_enable(encoder);
4726
2dd24552
JB
4727 i9xx_pfit_enable(intel_crtc);
4728
63cbb074
VS
4729 intel_crtc_load_lut(crtc);
4730
f37fcc2a 4731 intel_update_watermarks(crtc);
e1fdc473 4732 intel_enable_pipe(intel_crtc);
be6a6f8e 4733
5004945f
JN
4734 for_each_encoder_on_crtc(dev, crtc, encoder)
4735 encoder->enable(encoder);
9ab0460b
VS
4736
4737 intel_crtc_enable_planes(crtc);
d40d9187 4738
56b80e1f
VS
4739 /* Underruns don't raise interrupts, so check manually. */
4740 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4741}
4742
f13c2ef3
DV
4743static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4744{
4745 struct drm_device *dev = crtc->base.dev;
4746 struct drm_i915_private *dev_priv = dev->dev_private;
4747
4748 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4749 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4750}
4751
0b8765c6 4752static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4753{
4754 struct drm_device *dev = crtc->dev;
79e53945 4755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4756 struct intel_encoder *encoder;
79e53945 4757 int pipe = intel_crtc->pipe;
79e53945 4758
08a48469
DV
4759 WARN_ON(!crtc->enabled);
4760
f7abfe8b
CW
4761 if (intel_crtc->active)
4762 return;
4763
f13c2ef3
DV
4764 i9xx_set_pll_dividers(intel_crtc);
4765
5b18e57c
DV
4766 if (intel_crtc->config.has_dp_encoder)
4767 intel_dp_set_m_n(intel_crtc);
4768
4769 intel_set_pipe_timings(intel_crtc);
4770
5b18e57c
DV
4771 i9xx_set_pipeconf(intel_crtc);
4772
f7abfe8b 4773 intel_crtc->active = true;
6b383a7f 4774
4a3436e8
VS
4775 if (!IS_GEN2(dev))
4776 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4777
9d6d9f19
MK
4778 for_each_encoder_on_crtc(dev, crtc, encoder)
4779 if (encoder->pre_enable)
4780 encoder->pre_enable(encoder);
4781
f6736a1a
DV
4782 i9xx_enable_pll(intel_crtc);
4783
2dd24552
JB
4784 i9xx_pfit_enable(intel_crtc);
4785
63cbb074
VS
4786 intel_crtc_load_lut(crtc);
4787
f37fcc2a 4788 intel_update_watermarks(crtc);
e1fdc473 4789 intel_enable_pipe(intel_crtc);
be6a6f8e 4790
fa5c73b1
DV
4791 for_each_encoder_on_crtc(dev, crtc, encoder)
4792 encoder->enable(encoder);
9ab0460b
VS
4793
4794 intel_crtc_enable_planes(crtc);
d40d9187 4795
4a3436e8
VS
4796 /*
4797 * Gen2 reports pipe underruns whenever all planes are disabled.
4798 * So don't enable underrun reporting before at least some planes
4799 * are enabled.
4800 * FIXME: Need to fix the logic to work when we turn off all planes
4801 * but leave the pipe running.
4802 */
4803 if (IS_GEN2(dev))
4804 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4805
56b80e1f
VS
4806 /* Underruns don't raise interrupts, so check manually. */
4807 i9xx_check_fifo_underruns(dev);
0b8765c6 4808}
79e53945 4809
87476d63
DV
4810static void i9xx_pfit_disable(struct intel_crtc *crtc)
4811{
4812 struct drm_device *dev = crtc->base.dev;
4813 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4814
328d8e82
DV
4815 if (!crtc->config.gmch_pfit.control)
4816 return;
87476d63 4817
328d8e82 4818 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4819
328d8e82
DV
4820 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4821 I915_READ(PFIT_CONTROL));
4822 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4823}
4824
0b8765c6
JB
4825static void i9xx_crtc_disable(struct drm_crtc *crtc)
4826{
4827 struct drm_device *dev = crtc->dev;
4828 struct drm_i915_private *dev_priv = dev->dev_private;
4829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4830 struct intel_encoder *encoder;
0b8765c6 4831 int pipe = intel_crtc->pipe;
ef9c3aee 4832
f7abfe8b
CW
4833 if (!intel_crtc->active)
4834 return;
4835
4a3436e8
VS
4836 /*
4837 * Gen2 reports pipe underruns whenever all planes are disabled.
4838 * So diasble underrun reporting before all the planes get disabled.
4839 * FIXME: Need to fix the logic to work when we turn off all planes
4840 * but leave the pipe running.
4841 */
4842 if (IS_GEN2(dev))
4843 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4844
564ed191
ID
4845 /*
4846 * Vblank time updates from the shadow to live plane control register
4847 * are blocked if the memory self-refresh mode is active at that
4848 * moment. So to make sure the plane gets truly disabled, disable
4849 * first the self-refresh mode. The self-refresh enable bit in turn
4850 * will be checked/applied by the HW only at the next frame start
4851 * event which is after the vblank start event, so we need to have a
4852 * wait-for-vblank between disabling the plane and the pipe.
4853 */
4854 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
4855 intel_crtc_disable_planes(crtc);
4856
ea9d758d
DV
4857 for_each_encoder_on_crtc(dev, crtc, encoder)
4858 encoder->disable(encoder);
4859
6304cd91
VS
4860 /*
4861 * On gen2 planes are double buffered but the pipe isn't, so we must
4862 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
4863 * We also need to wait on all gmch platforms because of the
4864 * self-refresh mode constraint explained above.
6304cd91 4865 */
564ed191 4866 intel_wait_for_vblank(dev, pipe);
6304cd91 4867
b24e7179 4868 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4869
87476d63 4870 i9xx_pfit_disable(intel_crtc);
24a1f16d 4871
89b667f8
JB
4872 for_each_encoder_on_crtc(dev, crtc, encoder)
4873 if (encoder->post_disable)
4874 encoder->post_disable(encoder);
4875
076ed3b2
CML
4876 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4877 if (IS_CHERRYVIEW(dev))
4878 chv_disable_pll(dev_priv, pipe);
4879 else if (IS_VALLEYVIEW(dev))
4880 vlv_disable_pll(dev_priv, pipe);
4881 else
4882 i9xx_disable_pll(dev_priv, pipe);
4883 }
0b8765c6 4884
4a3436e8
VS
4885 if (!IS_GEN2(dev))
4886 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4887
f7abfe8b 4888 intel_crtc->active = false;
46ba614c 4889 intel_update_watermarks(crtc);
f37fcc2a 4890
efa9624e 4891 mutex_lock(&dev->struct_mutex);
6b383a7f 4892 intel_update_fbc(dev);
efa9624e 4893 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
4894}
4895
ee7b9f93
JB
4896static void i9xx_crtc_off(struct drm_crtc *crtc)
4897{
4898}
4899
976f8a20
DV
4900static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4901 bool enabled)
2c07245f
ZW
4902{
4903 struct drm_device *dev = crtc->dev;
4904 struct drm_i915_master_private *master_priv;
4905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4906 int pipe = intel_crtc->pipe;
79e53945
JB
4907
4908 if (!dev->primary->master)
4909 return;
4910
4911 master_priv = dev->primary->master->driver_priv;
4912 if (!master_priv->sarea_priv)
4913 return;
4914
79e53945
JB
4915 switch (pipe) {
4916 case 0:
4917 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4918 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4919 break;
4920 case 1:
4921 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4922 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4923 break;
4924 default:
9db4a9c7 4925 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4926 break;
4927 }
79e53945
JB
4928}
4929
b04c5bd6
BF
4930/* Master function to enable/disable CRTC and corresponding power wells */
4931void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
4932{
4933 struct drm_device *dev = crtc->dev;
4934 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 4935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
4936 enum intel_display_power_domain domain;
4937 unsigned long domains;
976f8a20 4938
0e572fe7
DV
4939 if (enable) {
4940 if (!intel_crtc->active) {
e1e9fb84
DV
4941 domains = get_crtc_power_domains(crtc);
4942 for_each_power_domain(domain, domains)
4943 intel_display_power_get(dev_priv, domain);
4944 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
4945
4946 dev_priv->display.crtc_enable(crtc);
4947 }
4948 } else {
4949 if (intel_crtc->active) {
4950 dev_priv->display.crtc_disable(crtc);
4951
e1e9fb84
DV
4952 domains = intel_crtc->enabled_power_domains;
4953 for_each_power_domain(domain, domains)
4954 intel_display_power_put(dev_priv, domain);
4955 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
4956 }
4957 }
b04c5bd6
BF
4958}
4959
4960/**
4961 * Sets the power management mode of the pipe and plane.
4962 */
4963void intel_crtc_update_dpms(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct intel_encoder *intel_encoder;
4967 bool enable = false;
4968
4969 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4970 enable |= intel_encoder->connectors_active;
4971
4972 intel_crtc_control(crtc, enable);
976f8a20
DV
4973
4974 intel_crtc_update_sarea(crtc, enable);
4975}
4976
cdd59983
CW
4977static void intel_crtc_disable(struct drm_crtc *crtc)
4978{
cdd59983 4979 struct drm_device *dev = crtc->dev;
976f8a20 4980 struct drm_connector *connector;
ee7b9f93 4981 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 4982 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 4983 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 4984
976f8a20
DV
4985 /* crtc should still be enabled when we disable it. */
4986 WARN_ON(!crtc->enabled);
4987
4988 dev_priv->display.crtc_disable(crtc);
4989 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4990 dev_priv->display.off(crtc);
4991
f4510a27 4992 if (crtc->primary->fb) {
cdd59983 4993 mutex_lock(&dev->struct_mutex);
a071fa00
DV
4994 intel_unpin_fb_obj(old_obj);
4995 i915_gem_track_fb(old_obj, NULL,
4996 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 4997 mutex_unlock(&dev->struct_mutex);
f4510a27 4998 crtc->primary->fb = NULL;
976f8a20
DV
4999 }
5000
5001 /* Update computed state. */
5002 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5003 if (!connector->encoder || !connector->encoder->crtc)
5004 continue;
5005
5006 if (connector->encoder->crtc != crtc)
5007 continue;
5008
5009 connector->dpms = DRM_MODE_DPMS_OFF;
5010 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5011 }
5012}
5013
ea5b213a 5014void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5015{
4ef69c7a 5016 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5017
ea5b213a
CW
5018 drm_encoder_cleanup(encoder);
5019 kfree(intel_encoder);
7e7d76c3
JB
5020}
5021
9237329d 5022/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5023 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5024 * state of the entire output pipe. */
9237329d 5025static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5026{
5ab432ef
DV
5027 if (mode == DRM_MODE_DPMS_ON) {
5028 encoder->connectors_active = true;
5029
b2cabb0e 5030 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5031 } else {
5032 encoder->connectors_active = false;
5033
b2cabb0e 5034 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5035 }
79e53945
JB
5036}
5037
0a91ca29
DV
5038/* Cross check the actual hw state with our own modeset state tracking (and it's
5039 * internal consistency). */
b980514c 5040static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5041{
0a91ca29
DV
5042 if (connector->get_hw_state(connector)) {
5043 struct intel_encoder *encoder = connector->encoder;
5044 struct drm_crtc *crtc;
5045 bool encoder_enabled;
5046 enum pipe pipe;
5047
5048 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5049 connector->base.base.id,
c23cc417 5050 connector->base.name);
0a91ca29 5051
0e32b39c
DA
5052 /* there is no real hw state for MST connectors */
5053 if (connector->mst_port)
5054 return;
5055
0a91ca29
DV
5056 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5057 "wrong connector dpms state\n");
5058 WARN(connector->base.encoder != &encoder->base,
5059 "active connector not linked to encoder\n");
0a91ca29 5060
36cd7444
DA
5061 if (encoder) {
5062 WARN(!encoder->connectors_active,
5063 "encoder->connectors_active not set\n");
5064
5065 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5066 WARN(!encoder_enabled, "encoder not enabled\n");
5067 if (WARN_ON(!encoder->base.crtc))
5068 return;
0a91ca29 5069
36cd7444 5070 crtc = encoder->base.crtc;
0a91ca29 5071
36cd7444
DA
5072 WARN(!crtc->enabled, "crtc not enabled\n");
5073 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5074 WARN(pipe != to_intel_crtc(crtc)->pipe,
5075 "encoder active on the wrong pipe\n");
5076 }
0a91ca29 5077 }
79e53945
JB
5078}
5079
5ab432ef
DV
5080/* Even simpler default implementation, if there's really no special case to
5081 * consider. */
5082void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5083{
5ab432ef
DV
5084 /* All the simple cases only support two dpms states. */
5085 if (mode != DRM_MODE_DPMS_ON)
5086 mode = DRM_MODE_DPMS_OFF;
d4270e57 5087
5ab432ef
DV
5088 if (mode == connector->dpms)
5089 return;
5090
5091 connector->dpms = mode;
5092
5093 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5094 if (connector->encoder)
5095 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5096
b980514c 5097 intel_modeset_check_state(connector->dev);
79e53945
JB
5098}
5099
f0947c37
DV
5100/* Simple connector->get_hw_state implementation for encoders that support only
5101 * one connector and no cloning and hence the encoder state determines the state
5102 * of the connector. */
5103bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5104{
24929352 5105 enum pipe pipe = 0;
f0947c37 5106 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5107
f0947c37 5108 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5109}
5110
1857e1da
DV
5111static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5112 struct intel_crtc_config *pipe_config)
5113{
5114 struct drm_i915_private *dev_priv = dev->dev_private;
5115 struct intel_crtc *pipe_B_crtc =
5116 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5117
5118 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5119 pipe_name(pipe), pipe_config->fdi_lanes);
5120 if (pipe_config->fdi_lanes > 4) {
5121 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5122 pipe_name(pipe), pipe_config->fdi_lanes);
5123 return false;
5124 }
5125
bafb6553 5126 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5127 if (pipe_config->fdi_lanes > 2) {
5128 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5129 pipe_config->fdi_lanes);
5130 return false;
5131 } else {
5132 return true;
5133 }
5134 }
5135
5136 if (INTEL_INFO(dev)->num_pipes == 2)
5137 return true;
5138
5139 /* Ivybridge 3 pipe is really complicated */
5140 switch (pipe) {
5141 case PIPE_A:
5142 return true;
5143 case PIPE_B:
5144 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5145 pipe_config->fdi_lanes > 2) {
5146 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5147 pipe_name(pipe), pipe_config->fdi_lanes);
5148 return false;
5149 }
5150 return true;
5151 case PIPE_C:
1e833f40 5152 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5153 pipe_B_crtc->config.fdi_lanes <= 2) {
5154 if (pipe_config->fdi_lanes > 2) {
5155 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5156 pipe_name(pipe), pipe_config->fdi_lanes);
5157 return false;
5158 }
5159 } else {
5160 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5161 return false;
5162 }
5163 return true;
5164 default:
5165 BUG();
5166 }
5167}
5168
e29c22c0
DV
5169#define RETRY 1
5170static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5171 struct intel_crtc_config *pipe_config)
877d48d5 5172{
1857e1da 5173 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5174 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5175 int lane, link_bw, fdi_dotclock;
e29c22c0 5176 bool setup_ok, needs_recompute = false;
877d48d5 5177
e29c22c0 5178retry:
877d48d5
DV
5179 /* FDI is a binary signal running at ~2.7GHz, encoding
5180 * each output octet as 10 bits. The actual frequency
5181 * is stored as a divider into a 100MHz clock, and the
5182 * mode pixel clock is stored in units of 1KHz.
5183 * Hence the bw of each lane in terms of the mode signal
5184 * is:
5185 */
5186 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5187
241bfc38 5188 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5189
2bd89a07 5190 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5191 pipe_config->pipe_bpp);
5192
5193 pipe_config->fdi_lanes = lane;
5194
2bd89a07 5195 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5196 link_bw, &pipe_config->fdi_m_n);
1857e1da 5197
e29c22c0
DV
5198 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5199 intel_crtc->pipe, pipe_config);
5200 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5201 pipe_config->pipe_bpp -= 2*3;
5202 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5203 pipe_config->pipe_bpp);
5204 needs_recompute = true;
5205 pipe_config->bw_constrained = true;
5206
5207 goto retry;
5208 }
5209
5210 if (needs_recompute)
5211 return RETRY;
5212
5213 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5214}
5215
42db64ef
PZ
5216static void hsw_compute_ips_config(struct intel_crtc *crtc,
5217 struct intel_crtc_config *pipe_config)
5218{
d330a953 5219 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5220 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5221 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5222}
5223
a43f6e0f 5224static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5225 struct intel_crtc_config *pipe_config)
79e53945 5226{
a43f6e0f 5227 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5228 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5229
ad3a4479 5230 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5231 if (INTEL_INFO(dev)->gen < 4) {
5232 struct drm_i915_private *dev_priv = dev->dev_private;
5233 int clock_limit =
5234 dev_priv->display.get_display_clock_speed(dev);
5235
5236 /*
5237 * Enable pixel doubling when the dot clock
5238 * is > 90% of the (display) core speed.
5239 *
b397c96b
VS
5240 * GDG double wide on either pipe,
5241 * otherwise pipe A only.
cf532bb2 5242 */
b397c96b 5243 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5244 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5245 clock_limit *= 2;
cf532bb2 5246 pipe_config->double_wide = true;
ad3a4479
VS
5247 }
5248
241bfc38 5249 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5250 return -EINVAL;
2c07245f 5251 }
89749350 5252
1d1d0e27
VS
5253 /*
5254 * Pipe horizontal size must be even in:
5255 * - DVO ganged mode
5256 * - LVDS dual channel mode
5257 * - Double wide pipe
5258 */
5259 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5260 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5261 pipe_config->pipe_src_w &= ~1;
5262
8693a824
DL
5263 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5264 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5265 */
5266 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5267 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5268 return -EINVAL;
44f46b42 5269
bd080ee5 5270 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5271 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5272 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5273 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5274 * for lvds. */
5275 pipe_config->pipe_bpp = 8*3;
5276 }
5277
f5adf94e 5278 if (HAS_IPS(dev))
a43f6e0f
DV
5279 hsw_compute_ips_config(crtc, pipe_config);
5280
12030431
DV
5281 /*
5282 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5283 * old clock survives for now.
5284 */
5285 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5286 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5287
877d48d5 5288 if (pipe_config->has_pch_encoder)
a43f6e0f 5289 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5290
e29c22c0 5291 return 0;
79e53945
JB
5292}
5293
25eb05fc
JB
5294static int valleyview_get_display_clock_speed(struct drm_device *dev)
5295{
d197b7d3
VS
5296 struct drm_i915_private *dev_priv = dev->dev_private;
5297 int vco = valleyview_get_vco(dev_priv);
5298 u32 val;
5299 int divider;
5300
d49a340d
VS
5301 /* FIXME: Punit isn't quite ready yet */
5302 if (IS_CHERRYVIEW(dev))
5303 return 400000;
5304
d197b7d3
VS
5305 mutex_lock(&dev_priv->dpio_lock);
5306 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5307 mutex_unlock(&dev_priv->dpio_lock);
5308
5309 divider = val & DISPLAY_FREQUENCY_VALUES;
5310
7d007f40
VS
5311 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5312 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5313 "cdclk change in progress\n");
5314
d197b7d3 5315 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5316}
5317
e70236a8
JB
5318static int i945_get_display_clock_speed(struct drm_device *dev)
5319{
5320 return 400000;
5321}
79e53945 5322
e70236a8 5323static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5324{
e70236a8
JB
5325 return 333000;
5326}
79e53945 5327
e70236a8
JB
5328static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5329{
5330 return 200000;
5331}
79e53945 5332
257a7ffc
DV
5333static int pnv_get_display_clock_speed(struct drm_device *dev)
5334{
5335 u16 gcfgc = 0;
5336
5337 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5338
5339 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5340 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5341 return 267000;
5342 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5343 return 333000;
5344 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5345 return 444000;
5346 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5347 return 200000;
5348 default:
5349 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5350 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5351 return 133000;
5352 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5353 return 167000;
5354 }
5355}
5356
e70236a8
JB
5357static int i915gm_get_display_clock_speed(struct drm_device *dev)
5358{
5359 u16 gcfgc = 0;
79e53945 5360
e70236a8
JB
5361 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5362
5363 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5364 return 133000;
5365 else {
5366 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5367 case GC_DISPLAY_CLOCK_333_MHZ:
5368 return 333000;
5369 default:
5370 case GC_DISPLAY_CLOCK_190_200_MHZ:
5371 return 190000;
79e53945 5372 }
e70236a8
JB
5373 }
5374}
5375
5376static int i865_get_display_clock_speed(struct drm_device *dev)
5377{
5378 return 266000;
5379}
5380
5381static int i855_get_display_clock_speed(struct drm_device *dev)
5382{
5383 u16 hpllcc = 0;
5384 /* Assume that the hardware is in the high speed state. This
5385 * should be the default.
5386 */
5387 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5388 case GC_CLOCK_133_200:
5389 case GC_CLOCK_100_200:
5390 return 200000;
5391 case GC_CLOCK_166_250:
5392 return 250000;
5393 case GC_CLOCK_100_133:
79e53945 5394 return 133000;
e70236a8 5395 }
79e53945 5396
e70236a8
JB
5397 /* Shouldn't happen */
5398 return 0;
5399}
79e53945 5400
e70236a8
JB
5401static int i830_get_display_clock_speed(struct drm_device *dev)
5402{
5403 return 133000;
79e53945
JB
5404}
5405
2c07245f 5406static void
a65851af 5407intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5408{
a65851af
VS
5409 while (*num > DATA_LINK_M_N_MASK ||
5410 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5411 *num >>= 1;
5412 *den >>= 1;
5413 }
5414}
5415
a65851af
VS
5416static void compute_m_n(unsigned int m, unsigned int n,
5417 uint32_t *ret_m, uint32_t *ret_n)
5418{
5419 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5420 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5421 intel_reduce_m_n_ratio(ret_m, ret_n);
5422}
5423
e69d0bc1
DV
5424void
5425intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5426 int pixel_clock, int link_clock,
5427 struct intel_link_m_n *m_n)
2c07245f 5428{
e69d0bc1 5429 m_n->tu = 64;
a65851af
VS
5430
5431 compute_m_n(bits_per_pixel * pixel_clock,
5432 link_clock * nlanes * 8,
5433 &m_n->gmch_m, &m_n->gmch_n);
5434
5435 compute_m_n(pixel_clock, link_clock,
5436 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5437}
5438
a7615030
CW
5439static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5440{
d330a953
JN
5441 if (i915.panel_use_ssc >= 0)
5442 return i915.panel_use_ssc != 0;
41aa3448 5443 return dev_priv->vbt.lvds_use_ssc
435793df 5444 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5445}
5446
c65d77d8
JB
5447static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5448{
5449 struct drm_device *dev = crtc->dev;
5450 struct drm_i915_private *dev_priv = dev->dev_private;
5451 int refclk;
5452
a0c4da24 5453 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5454 refclk = 100000;
a0c4da24 5455 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5456 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5457 refclk = dev_priv->vbt.lvds_ssc_freq;
5458 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5459 } else if (!IS_GEN2(dev)) {
5460 refclk = 96000;
5461 } else {
5462 refclk = 48000;
5463 }
5464
5465 return refclk;
5466}
5467
7429e9d4 5468static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5469{
7df00d7a 5470 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5471}
f47709a9 5472
7429e9d4
DV
5473static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5474{
5475 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5476}
5477
f47709a9 5478static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5479 intel_clock_t *reduced_clock)
5480{
f47709a9 5481 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5482 u32 fp, fp2 = 0;
5483
5484 if (IS_PINEVIEW(dev)) {
7429e9d4 5485 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5486 if (reduced_clock)
7429e9d4 5487 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5488 } else {
7429e9d4 5489 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5490 if (reduced_clock)
7429e9d4 5491 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5492 }
5493
8bcc2795 5494 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5495
f47709a9
DV
5496 crtc->lowfreq_avail = false;
5497 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5498 reduced_clock && i915.powersave) {
8bcc2795 5499 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5500 crtc->lowfreq_avail = true;
a7516a05 5501 } else {
8bcc2795 5502 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5503 }
5504}
5505
5e69f97f
CML
5506static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5507 pipe)
89b667f8
JB
5508{
5509 u32 reg_val;
5510
5511 /*
5512 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5513 * and set it to a reasonable value instead.
5514 */
ab3c759a 5515 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5516 reg_val &= 0xffffff00;
5517 reg_val |= 0x00000030;
ab3c759a 5518 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5519
ab3c759a 5520 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5521 reg_val &= 0x8cffffff;
5522 reg_val = 0x8c000000;
ab3c759a 5523 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5524
ab3c759a 5525 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5526 reg_val &= 0xffffff00;
ab3c759a 5527 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5528
ab3c759a 5529 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5530 reg_val &= 0x00ffffff;
5531 reg_val |= 0xb0000000;
ab3c759a 5532 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5533}
5534
b551842d
DV
5535static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5536 struct intel_link_m_n *m_n)
5537{
5538 struct drm_device *dev = crtc->base.dev;
5539 struct drm_i915_private *dev_priv = dev->dev_private;
5540 int pipe = crtc->pipe;
5541
e3b95f1e
DV
5542 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5543 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5544 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5545 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5546}
5547
5548static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5549 struct intel_link_m_n *m_n,
5550 struct intel_link_m_n *m2_n2)
b551842d
DV
5551{
5552 struct drm_device *dev = crtc->base.dev;
5553 struct drm_i915_private *dev_priv = dev->dev_private;
5554 int pipe = crtc->pipe;
5555 enum transcoder transcoder = crtc->config.cpu_transcoder;
5556
5557 if (INTEL_INFO(dev)->gen >= 5) {
5558 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5559 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5560 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5561 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5562 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5563 * for gen < 8) and if DRRS is supported (to make sure the
5564 * registers are not unnecessarily accessed).
5565 */
5566 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5567 crtc->config.has_drrs) {
5568 I915_WRITE(PIPE_DATA_M2(transcoder),
5569 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5570 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5571 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5572 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5573 }
b551842d 5574 } else {
e3b95f1e
DV
5575 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5576 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5577 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5578 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5579 }
5580}
5581
f769cd24 5582void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5583{
5584 if (crtc->config.has_pch_encoder)
5585 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5586 else
f769cd24
VK
5587 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5588 &crtc->config.dp_m2_n2);
03afc4a2
DV
5589}
5590
f47709a9 5591static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5592{
5593 u32 dpll, dpll_md;
5594
5595 /*
5596 * Enable DPIO clock input. We should never disable the reference
5597 * clock for pipe B, since VGA hotplug / manual detection depends
5598 * on it.
5599 */
5600 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5601 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5602 /* We should never disable this, set it here for state tracking */
5603 if (crtc->pipe == PIPE_B)
5604 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5605 dpll |= DPLL_VCO_ENABLE;
5606 crtc->config.dpll_hw_state.dpll = dpll;
5607
5608 dpll_md = (crtc->config.pixel_multiplier - 1)
5609 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5610 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5611}
5612
5613static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5614{
f47709a9 5615 struct drm_device *dev = crtc->base.dev;
a0c4da24 5616 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5617 int pipe = crtc->pipe;
bdd4b6a6 5618 u32 mdiv;
a0c4da24 5619 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5620 u32 coreclk, reg_val;
a0c4da24 5621
09153000
DV
5622 mutex_lock(&dev_priv->dpio_lock);
5623
f47709a9
DV
5624 bestn = crtc->config.dpll.n;
5625 bestm1 = crtc->config.dpll.m1;
5626 bestm2 = crtc->config.dpll.m2;
5627 bestp1 = crtc->config.dpll.p1;
5628 bestp2 = crtc->config.dpll.p2;
a0c4da24 5629
89b667f8
JB
5630 /* See eDP HDMI DPIO driver vbios notes doc */
5631
5632 /* PLL B needs special handling */
bdd4b6a6 5633 if (pipe == PIPE_B)
5e69f97f 5634 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5635
5636 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5637 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5638
5639 /* Disable target IRef on PLL */
ab3c759a 5640 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5641 reg_val &= 0x00ffffff;
ab3c759a 5642 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5643
5644 /* Disable fast lock */
ab3c759a 5645 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5646
5647 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5648 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5649 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5650 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5651 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5652
5653 /*
5654 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5655 * but we don't support that).
5656 * Note: don't use the DAC post divider as it seems unstable.
5657 */
5658 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5659 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5660
a0c4da24 5661 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5662 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5663
89b667f8 5664 /* Set HBR and RBR LPF coefficients */
ff9a6750 5665 if (crtc->config.port_clock == 162000 ||
99750bd4 5666 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5667 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5668 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5669 0x009f0003);
89b667f8 5670 else
ab3c759a 5671 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5672 0x00d0000f);
5673
5674 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5675 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5676 /* Use SSC source */
bdd4b6a6 5677 if (pipe == PIPE_A)
ab3c759a 5678 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5679 0x0df40000);
5680 else
ab3c759a 5681 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5682 0x0df70000);
5683 } else { /* HDMI or VGA */
5684 /* Use bend source */
bdd4b6a6 5685 if (pipe == PIPE_A)
ab3c759a 5686 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5687 0x0df70000);
5688 else
ab3c759a 5689 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5690 0x0df40000);
5691 }
a0c4da24 5692
ab3c759a 5693 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5694 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5695 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5696 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5697 coreclk |= 0x01000000;
ab3c759a 5698 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5699
ab3c759a 5700 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5701 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5702}
5703
9d556c99 5704static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5705{
5706 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5707 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5708 DPLL_VCO_ENABLE;
5709 if (crtc->pipe != PIPE_A)
5710 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5711
5712 crtc->config.dpll_hw_state.dpll_md =
5713 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5714}
5715
5716static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5717{
5718 struct drm_device *dev = crtc->base.dev;
5719 struct drm_i915_private *dev_priv = dev->dev_private;
5720 int pipe = crtc->pipe;
5721 int dpll_reg = DPLL(crtc->pipe);
5722 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5723 u32 loopfilter, intcoeff;
9d556c99
CML
5724 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5725 int refclk;
5726
9d556c99
CML
5727 bestn = crtc->config.dpll.n;
5728 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5729 bestm1 = crtc->config.dpll.m1;
5730 bestm2 = crtc->config.dpll.m2 >> 22;
5731 bestp1 = crtc->config.dpll.p1;
5732 bestp2 = crtc->config.dpll.p2;
5733
5734 /*
5735 * Enable Refclk and SSC
5736 */
a11b0703
VS
5737 I915_WRITE(dpll_reg,
5738 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5739
5740 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5741
9d556c99
CML
5742 /* p1 and p2 divider */
5743 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5744 5 << DPIO_CHV_S1_DIV_SHIFT |
5745 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5746 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5747 1 << DPIO_CHV_K_DIV_SHIFT);
5748
5749 /* Feedback post-divider - m2 */
5750 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5751
5752 /* Feedback refclk divider - n and m1 */
5753 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5754 DPIO_CHV_M1_DIV_BY_2 |
5755 1 << DPIO_CHV_N_DIV_SHIFT);
5756
5757 /* M2 fraction division */
5758 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5759
5760 /* M2 fraction division enable */
5761 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5762 DPIO_CHV_FRAC_DIV_EN |
5763 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5764
5765 /* Loop filter */
5766 refclk = i9xx_get_refclk(&crtc->base, 0);
5767 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5768 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5769 if (refclk == 100000)
5770 intcoeff = 11;
5771 else if (refclk == 38400)
5772 intcoeff = 10;
5773 else
5774 intcoeff = 9;
5775 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5776 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5777
5778 /* AFC Recal */
5779 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5780 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5781 DPIO_AFC_RECAL);
5782
5783 mutex_unlock(&dev_priv->dpio_lock);
5784}
5785
f47709a9
DV
5786static void i9xx_update_pll(struct intel_crtc *crtc,
5787 intel_clock_t *reduced_clock,
eb1cbe48
DV
5788 int num_connectors)
5789{
f47709a9 5790 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5791 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5792 u32 dpll;
5793 bool is_sdvo;
f47709a9 5794 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5795
f47709a9 5796 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5797
f47709a9
DV
5798 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5799 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5800
5801 dpll = DPLL_VGA_MODE_DIS;
5802
f47709a9 5803 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5804 dpll |= DPLLB_MODE_LVDS;
5805 else
5806 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5807
ef1b460d 5808 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5809 dpll |= (crtc->config.pixel_multiplier - 1)
5810 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5811 }
198a037f
DV
5812
5813 if (is_sdvo)
4a33e48d 5814 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5815
f47709a9 5816 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5817 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5818
5819 /* compute bitmask from p1 value */
5820 if (IS_PINEVIEW(dev))
5821 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5822 else {
5823 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5824 if (IS_G4X(dev) && reduced_clock)
5825 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5826 }
5827 switch (clock->p2) {
5828 case 5:
5829 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5830 break;
5831 case 7:
5832 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5833 break;
5834 case 10:
5835 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5836 break;
5837 case 14:
5838 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5839 break;
5840 }
5841 if (INTEL_INFO(dev)->gen >= 4)
5842 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5843
09ede541 5844 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5845 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5846 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5847 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5848 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5849 else
5850 dpll |= PLL_REF_INPUT_DREFCLK;
5851
5852 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5853 crtc->config.dpll_hw_state.dpll = dpll;
5854
eb1cbe48 5855 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5856 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5857 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5858 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5859 }
5860}
5861
f47709a9 5862static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5863 intel_clock_t *reduced_clock,
eb1cbe48
DV
5864 int num_connectors)
5865{
f47709a9 5866 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5867 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5868 u32 dpll;
f47709a9 5869 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5870
f47709a9 5871 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5872
eb1cbe48
DV
5873 dpll = DPLL_VGA_MODE_DIS;
5874
f47709a9 5875 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5876 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5877 } else {
5878 if (clock->p1 == 2)
5879 dpll |= PLL_P1_DIVIDE_BY_TWO;
5880 else
5881 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5882 if (clock->p2 == 4)
5883 dpll |= PLL_P2_DIVIDE_BY_4;
5884 }
5885
4a33e48d
DV
5886 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5887 dpll |= DPLL_DVO_2X_MODE;
5888
f47709a9 5889 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5890 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5891 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5892 else
5893 dpll |= PLL_REF_INPUT_DREFCLK;
5894
5895 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5896 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5897}
5898
8a654f3b 5899static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5900{
5901 struct drm_device *dev = intel_crtc->base.dev;
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5904 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5905 struct drm_display_mode *adjusted_mode =
5906 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5907 uint32_t crtc_vtotal, crtc_vblank_end;
5908 int vsyncshift = 0;
4d8a62ea
DV
5909
5910 /* We need to be careful not to changed the adjusted mode, for otherwise
5911 * the hw state checker will get angry at the mismatch. */
5912 crtc_vtotal = adjusted_mode->crtc_vtotal;
5913 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5914
609aeaca 5915 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5916 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5917 crtc_vtotal -= 1;
5918 crtc_vblank_end -= 1;
609aeaca
VS
5919
5920 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5921 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5922 else
5923 vsyncshift = adjusted_mode->crtc_hsync_start -
5924 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5925 if (vsyncshift < 0)
5926 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5927 }
5928
5929 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5930 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5931
fe2b8f9d 5932 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5933 (adjusted_mode->crtc_hdisplay - 1) |
5934 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5935 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5936 (adjusted_mode->crtc_hblank_start - 1) |
5937 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5938 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5939 (adjusted_mode->crtc_hsync_start - 1) |
5940 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5941
fe2b8f9d 5942 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5943 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5944 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5945 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5946 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5947 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5948 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5949 (adjusted_mode->crtc_vsync_start - 1) |
5950 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5951
b5e508d4
PZ
5952 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5953 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5954 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5955 * bits. */
5956 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5957 (pipe == PIPE_B || pipe == PIPE_C))
5958 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5959
b0e77b9c
PZ
5960 /* pipesrc controls the size that is scaled from, which should
5961 * always be the user's requested size.
5962 */
5963 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5964 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5965 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5966}
5967
1bd1bd80
DV
5968static void intel_get_pipe_timings(struct intel_crtc *crtc,
5969 struct intel_crtc_config *pipe_config)
5970{
5971 struct drm_device *dev = crtc->base.dev;
5972 struct drm_i915_private *dev_priv = dev->dev_private;
5973 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5974 uint32_t tmp;
5975
5976 tmp = I915_READ(HTOTAL(cpu_transcoder));
5977 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5978 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5979 tmp = I915_READ(HBLANK(cpu_transcoder));
5980 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5981 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5982 tmp = I915_READ(HSYNC(cpu_transcoder));
5983 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5984 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5985
5986 tmp = I915_READ(VTOTAL(cpu_transcoder));
5987 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5988 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5989 tmp = I915_READ(VBLANK(cpu_transcoder));
5990 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5991 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5992 tmp = I915_READ(VSYNC(cpu_transcoder));
5993 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5994 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5995
5996 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5997 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5998 pipe_config->adjusted_mode.crtc_vtotal += 1;
5999 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6000 }
6001
6002 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6003 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6004 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6005
6006 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6007 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6008}
6009
f6a83288
DV
6010void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6011 struct intel_crtc_config *pipe_config)
babea61d 6012{
f6a83288
DV
6013 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6014 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6015 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6016 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6017
f6a83288
DV
6018 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6019 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6020 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6021 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6022
f6a83288 6023 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6024
f6a83288
DV
6025 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6026 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6027}
6028
84b046f3
DV
6029static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6030{
6031 struct drm_device *dev = intel_crtc->base.dev;
6032 struct drm_i915_private *dev_priv = dev->dev_private;
6033 uint32_t pipeconf;
6034
9f11a9e4 6035 pipeconf = 0;
84b046f3 6036
67c72a12
DV
6037 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
6038 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
6039 pipeconf |= PIPECONF_ENABLE;
6040
cf532bb2
VS
6041 if (intel_crtc->config.double_wide)
6042 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6043
ff9ce46e
DV
6044 /* only g4x and later have fancy bpc/dither controls */
6045 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6046 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6047 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6048 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6049 PIPECONF_DITHER_TYPE_SP;
84b046f3 6050
ff9ce46e
DV
6051 switch (intel_crtc->config.pipe_bpp) {
6052 case 18:
6053 pipeconf |= PIPECONF_6BPC;
6054 break;
6055 case 24:
6056 pipeconf |= PIPECONF_8BPC;
6057 break;
6058 case 30:
6059 pipeconf |= PIPECONF_10BPC;
6060 break;
6061 default:
6062 /* Case prevented by intel_choose_pipe_bpp_dither. */
6063 BUG();
84b046f3
DV
6064 }
6065 }
6066
6067 if (HAS_PIPE_CXSR(dev)) {
6068 if (intel_crtc->lowfreq_avail) {
6069 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6070 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6071 } else {
6072 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6073 }
6074 }
6075
efc2cfff
VS
6076 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6077 if (INTEL_INFO(dev)->gen < 4 ||
6078 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6079 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6080 else
6081 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6082 } else
84b046f3
DV
6083 pipeconf |= PIPECONF_PROGRESSIVE;
6084
9f11a9e4
DV
6085 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6086 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6087
84b046f3
DV
6088 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6089 POSTING_READ(PIPECONF(intel_crtc->pipe));
6090}
6091
f564048e 6092static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6093 int x, int y,
94352cf9 6094 struct drm_framebuffer *fb)
79e53945
JB
6095{
6096 struct drm_device *dev = crtc->dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6099 int refclk, num_connectors = 0;
652c393a 6100 intel_clock_t clock, reduced_clock;
a16af721 6101 bool ok, has_reduced_clock = false;
e9fd1c02 6102 bool is_lvds = false, is_dsi = false;
5eddb70b 6103 struct intel_encoder *encoder;
d4906093 6104 const intel_limit_t *limit;
79e53945 6105
6c2b7c12 6106 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6107 switch (encoder->type) {
79e53945
JB
6108 case INTEL_OUTPUT_LVDS:
6109 is_lvds = true;
6110 break;
e9fd1c02
JN
6111 case INTEL_OUTPUT_DSI:
6112 is_dsi = true;
6113 break;
79e53945 6114 }
43565a06 6115
c751ce4f 6116 num_connectors++;
79e53945
JB
6117 }
6118
f2335330 6119 if (is_dsi)
5b18e57c 6120 return 0;
f2335330
JN
6121
6122 if (!intel_crtc->config.clock_set) {
6123 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6124
e9fd1c02
JN
6125 /*
6126 * Returns a set of divisors for the desired target clock with
6127 * the given refclk, or FALSE. The returned values represent
6128 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6129 * 2) / p1 / p2.
6130 */
6131 limit = intel_limit(crtc, refclk);
6132 ok = dev_priv->display.find_dpll(limit, crtc,
6133 intel_crtc->config.port_clock,
6134 refclk, NULL, &clock);
f2335330 6135 if (!ok) {
e9fd1c02
JN
6136 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6137 return -EINVAL;
6138 }
79e53945 6139
f2335330
JN
6140 if (is_lvds && dev_priv->lvds_downclock_avail) {
6141 /*
6142 * Ensure we match the reduced clock's P to the target
6143 * clock. If the clocks don't match, we can't switch
6144 * the display clock by using the FP0/FP1. In such case
6145 * we will disable the LVDS downclock feature.
6146 */
6147 has_reduced_clock =
6148 dev_priv->display.find_dpll(limit, crtc,
6149 dev_priv->lvds_downclock,
6150 refclk, &clock,
6151 &reduced_clock);
6152 }
6153 /* Compat-code for transition, will disappear. */
f47709a9
DV
6154 intel_crtc->config.dpll.n = clock.n;
6155 intel_crtc->config.dpll.m1 = clock.m1;
6156 intel_crtc->config.dpll.m2 = clock.m2;
6157 intel_crtc->config.dpll.p1 = clock.p1;
6158 intel_crtc->config.dpll.p2 = clock.p2;
6159 }
7026d4ac 6160
e9fd1c02 6161 if (IS_GEN2(dev)) {
8a654f3b 6162 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6163 has_reduced_clock ? &reduced_clock : NULL,
6164 num_connectors);
9d556c99
CML
6165 } else if (IS_CHERRYVIEW(dev)) {
6166 chv_update_pll(intel_crtc);
e9fd1c02 6167 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6168 vlv_update_pll(intel_crtc);
e9fd1c02 6169 } else {
f47709a9 6170 i9xx_update_pll(intel_crtc,
eb1cbe48 6171 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6172 num_connectors);
e9fd1c02 6173 }
79e53945 6174
c8f7a0db 6175 return 0;
f564048e
EA
6176}
6177
2fa2fe9a
DV
6178static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6179 struct intel_crtc_config *pipe_config)
6180{
6181 struct drm_device *dev = crtc->base.dev;
6182 struct drm_i915_private *dev_priv = dev->dev_private;
6183 uint32_t tmp;
6184
dc9e7dec
VS
6185 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6186 return;
6187
2fa2fe9a 6188 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6189 if (!(tmp & PFIT_ENABLE))
6190 return;
2fa2fe9a 6191
06922821 6192 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6193 if (INTEL_INFO(dev)->gen < 4) {
6194 if (crtc->pipe != PIPE_B)
6195 return;
2fa2fe9a
DV
6196 } else {
6197 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6198 return;
6199 }
6200
06922821 6201 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6202 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6203 if (INTEL_INFO(dev)->gen < 5)
6204 pipe_config->gmch_pfit.lvds_border_bits =
6205 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6206}
6207
acbec814
JB
6208static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6209 struct intel_crtc_config *pipe_config)
6210{
6211 struct drm_device *dev = crtc->base.dev;
6212 struct drm_i915_private *dev_priv = dev->dev_private;
6213 int pipe = pipe_config->cpu_transcoder;
6214 intel_clock_t clock;
6215 u32 mdiv;
662c6ecb 6216 int refclk = 100000;
acbec814 6217
f573de5a
SK
6218 /* In case of MIPI DPLL will not even be used */
6219 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6220 return;
6221
acbec814 6222 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6223 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6224 mutex_unlock(&dev_priv->dpio_lock);
6225
6226 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6227 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6228 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6229 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6230 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6231
f646628b 6232 vlv_clock(refclk, &clock);
acbec814 6233
f646628b
VS
6234 /* clock.dot is the fast clock */
6235 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6236}
6237
1ad292b5
JB
6238static void i9xx_get_plane_config(struct intel_crtc *crtc,
6239 struct intel_plane_config *plane_config)
6240{
6241 struct drm_device *dev = crtc->base.dev;
6242 struct drm_i915_private *dev_priv = dev->dev_private;
6243 u32 val, base, offset;
6244 int pipe = crtc->pipe, plane = crtc->plane;
6245 int fourcc, pixel_format;
6246 int aligned_height;
6247
66e514c1
DA
6248 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6249 if (!crtc->base.primary->fb) {
1ad292b5
JB
6250 DRM_DEBUG_KMS("failed to alloc fb\n");
6251 return;
6252 }
6253
6254 val = I915_READ(DSPCNTR(plane));
6255
6256 if (INTEL_INFO(dev)->gen >= 4)
6257 if (val & DISPPLANE_TILED)
6258 plane_config->tiled = true;
6259
6260 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6261 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6262 crtc->base.primary->fb->pixel_format = fourcc;
6263 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6264 drm_format_plane_cpp(fourcc, 0) * 8;
6265
6266 if (INTEL_INFO(dev)->gen >= 4) {
6267 if (plane_config->tiled)
6268 offset = I915_READ(DSPTILEOFF(plane));
6269 else
6270 offset = I915_READ(DSPLINOFF(plane));
6271 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6272 } else {
6273 base = I915_READ(DSPADDR(plane));
6274 }
6275 plane_config->base = base;
6276
6277 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6278 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6279 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6280
6281 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6282 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6283
66e514c1 6284 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6285 plane_config->tiled);
6286
1267a26b
FF
6287 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6288 aligned_height);
1ad292b5
JB
6289
6290 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6291 pipe, plane, crtc->base.primary->fb->width,
6292 crtc->base.primary->fb->height,
6293 crtc->base.primary->fb->bits_per_pixel, base,
6294 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6295 plane_config->size);
6296
6297}
6298
70b23a98
VS
6299static void chv_crtc_clock_get(struct intel_crtc *crtc,
6300 struct intel_crtc_config *pipe_config)
6301{
6302 struct drm_device *dev = crtc->base.dev;
6303 struct drm_i915_private *dev_priv = dev->dev_private;
6304 int pipe = pipe_config->cpu_transcoder;
6305 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6306 intel_clock_t clock;
6307 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6308 int refclk = 100000;
6309
6310 mutex_lock(&dev_priv->dpio_lock);
6311 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6312 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6313 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6314 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6315 mutex_unlock(&dev_priv->dpio_lock);
6316
6317 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6318 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6319 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6320 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6321 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6322
6323 chv_clock(refclk, &clock);
6324
6325 /* clock.dot is the fast clock */
6326 pipe_config->port_clock = clock.dot / 5;
6327}
6328
0e8ffe1b
DV
6329static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6330 struct intel_crtc_config *pipe_config)
6331{
6332 struct drm_device *dev = crtc->base.dev;
6333 struct drm_i915_private *dev_priv = dev->dev_private;
6334 uint32_t tmp;
6335
b5482bd0
ID
6336 if (!intel_display_power_enabled(dev_priv,
6337 POWER_DOMAIN_PIPE(crtc->pipe)))
6338 return false;
6339
e143a21c 6340 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6341 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6342
0e8ffe1b
DV
6343 tmp = I915_READ(PIPECONF(crtc->pipe));
6344 if (!(tmp & PIPECONF_ENABLE))
6345 return false;
6346
42571aef
VS
6347 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6348 switch (tmp & PIPECONF_BPC_MASK) {
6349 case PIPECONF_6BPC:
6350 pipe_config->pipe_bpp = 18;
6351 break;
6352 case PIPECONF_8BPC:
6353 pipe_config->pipe_bpp = 24;
6354 break;
6355 case PIPECONF_10BPC:
6356 pipe_config->pipe_bpp = 30;
6357 break;
6358 default:
6359 break;
6360 }
6361 }
6362
b5a9fa09
DV
6363 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6364 pipe_config->limited_color_range = true;
6365
282740f7
VS
6366 if (INTEL_INFO(dev)->gen < 4)
6367 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6368
1bd1bd80
DV
6369 intel_get_pipe_timings(crtc, pipe_config);
6370
2fa2fe9a
DV
6371 i9xx_get_pfit_config(crtc, pipe_config);
6372
6c49f241
DV
6373 if (INTEL_INFO(dev)->gen >= 4) {
6374 tmp = I915_READ(DPLL_MD(crtc->pipe));
6375 pipe_config->pixel_multiplier =
6376 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6377 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6378 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6379 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6380 tmp = I915_READ(DPLL(crtc->pipe));
6381 pipe_config->pixel_multiplier =
6382 ((tmp & SDVO_MULTIPLIER_MASK)
6383 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6384 } else {
6385 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6386 * port and will be fixed up in the encoder->get_config
6387 * function. */
6388 pipe_config->pixel_multiplier = 1;
6389 }
8bcc2795
DV
6390 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6391 if (!IS_VALLEYVIEW(dev)) {
6392 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6393 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6394 } else {
6395 /* Mask out read-only status bits. */
6396 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6397 DPLL_PORTC_READY_MASK |
6398 DPLL_PORTB_READY_MASK);
8bcc2795 6399 }
6c49f241 6400
70b23a98
VS
6401 if (IS_CHERRYVIEW(dev))
6402 chv_crtc_clock_get(crtc, pipe_config);
6403 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6404 vlv_crtc_clock_get(crtc, pipe_config);
6405 else
6406 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6407
0e8ffe1b
DV
6408 return true;
6409}
6410
dde86e2d 6411static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6412{
6413 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6414 struct intel_encoder *encoder;
74cfd7ac 6415 u32 val, final;
13d83a67 6416 bool has_lvds = false;
199e5d79 6417 bool has_cpu_edp = false;
199e5d79 6418 bool has_panel = false;
99eb6a01
KP
6419 bool has_ck505 = false;
6420 bool can_ssc = false;
13d83a67
JB
6421
6422 /* We need to take the global config into account */
b2784e15 6423 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6424 switch (encoder->type) {
6425 case INTEL_OUTPUT_LVDS:
6426 has_panel = true;
6427 has_lvds = true;
6428 break;
6429 case INTEL_OUTPUT_EDP:
6430 has_panel = true;
2de6905f 6431 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6432 has_cpu_edp = true;
6433 break;
13d83a67
JB
6434 }
6435 }
6436
99eb6a01 6437 if (HAS_PCH_IBX(dev)) {
41aa3448 6438 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6439 can_ssc = has_ck505;
6440 } else {
6441 has_ck505 = false;
6442 can_ssc = true;
6443 }
6444
2de6905f
ID
6445 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6446 has_panel, has_lvds, has_ck505);
13d83a67
JB
6447
6448 /* Ironlake: try to setup display ref clock before DPLL
6449 * enabling. This is only under driver's control after
6450 * PCH B stepping, previous chipset stepping should be
6451 * ignoring this setting.
6452 */
74cfd7ac
CW
6453 val = I915_READ(PCH_DREF_CONTROL);
6454
6455 /* As we must carefully and slowly disable/enable each source in turn,
6456 * compute the final state we want first and check if we need to
6457 * make any changes at all.
6458 */
6459 final = val;
6460 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6461 if (has_ck505)
6462 final |= DREF_NONSPREAD_CK505_ENABLE;
6463 else
6464 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6465
6466 final &= ~DREF_SSC_SOURCE_MASK;
6467 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6468 final &= ~DREF_SSC1_ENABLE;
6469
6470 if (has_panel) {
6471 final |= DREF_SSC_SOURCE_ENABLE;
6472
6473 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6474 final |= DREF_SSC1_ENABLE;
6475
6476 if (has_cpu_edp) {
6477 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6478 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6479 else
6480 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6481 } else
6482 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6483 } else {
6484 final |= DREF_SSC_SOURCE_DISABLE;
6485 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6486 }
6487
6488 if (final == val)
6489 return;
6490
13d83a67 6491 /* Always enable nonspread source */
74cfd7ac 6492 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6493
99eb6a01 6494 if (has_ck505)
74cfd7ac 6495 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6496 else
74cfd7ac 6497 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6498
199e5d79 6499 if (has_panel) {
74cfd7ac
CW
6500 val &= ~DREF_SSC_SOURCE_MASK;
6501 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6502
199e5d79 6503 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6504 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6505 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6506 val |= DREF_SSC1_ENABLE;
e77166b5 6507 } else
74cfd7ac 6508 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6509
6510 /* Get SSC going before enabling the outputs */
74cfd7ac 6511 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6512 POSTING_READ(PCH_DREF_CONTROL);
6513 udelay(200);
6514
74cfd7ac 6515 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6516
6517 /* Enable CPU source on CPU attached eDP */
199e5d79 6518 if (has_cpu_edp) {
99eb6a01 6519 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6520 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6521 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6522 } else
74cfd7ac 6523 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6524 } else
74cfd7ac 6525 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6526
74cfd7ac 6527 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6528 POSTING_READ(PCH_DREF_CONTROL);
6529 udelay(200);
6530 } else {
6531 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6532
74cfd7ac 6533 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6534
6535 /* Turn off CPU output */
74cfd7ac 6536 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6537
74cfd7ac 6538 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6539 POSTING_READ(PCH_DREF_CONTROL);
6540 udelay(200);
6541
6542 /* Turn off the SSC source */
74cfd7ac
CW
6543 val &= ~DREF_SSC_SOURCE_MASK;
6544 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6545
6546 /* Turn off SSC1 */
74cfd7ac 6547 val &= ~DREF_SSC1_ENABLE;
199e5d79 6548
74cfd7ac 6549 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6550 POSTING_READ(PCH_DREF_CONTROL);
6551 udelay(200);
6552 }
74cfd7ac
CW
6553
6554 BUG_ON(val != final);
13d83a67
JB
6555}
6556
f31f2d55 6557static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6558{
f31f2d55 6559 uint32_t tmp;
dde86e2d 6560
0ff066a9
PZ
6561 tmp = I915_READ(SOUTH_CHICKEN2);
6562 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6563 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6564
0ff066a9
PZ
6565 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6566 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6567 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6568
0ff066a9
PZ
6569 tmp = I915_READ(SOUTH_CHICKEN2);
6570 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6571 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6572
0ff066a9
PZ
6573 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6574 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6575 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6576}
6577
6578/* WaMPhyProgramming:hsw */
6579static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6580{
6581 uint32_t tmp;
dde86e2d
PZ
6582
6583 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6584 tmp &= ~(0xFF << 24);
6585 tmp |= (0x12 << 24);
6586 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6587
dde86e2d
PZ
6588 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6589 tmp |= (1 << 11);
6590 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6591
6592 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6593 tmp |= (1 << 11);
6594 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6595
dde86e2d
PZ
6596 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6597 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6598 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6599
6600 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6601 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6602 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6603
0ff066a9
PZ
6604 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6605 tmp &= ~(7 << 13);
6606 tmp |= (5 << 13);
6607 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6608
0ff066a9
PZ
6609 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6610 tmp &= ~(7 << 13);
6611 tmp |= (5 << 13);
6612 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6613
6614 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6615 tmp &= ~0xFF;
6616 tmp |= 0x1C;
6617 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6618
6619 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6620 tmp &= ~0xFF;
6621 tmp |= 0x1C;
6622 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6623
6624 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6625 tmp &= ~(0xFF << 16);
6626 tmp |= (0x1C << 16);
6627 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6628
6629 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6630 tmp &= ~(0xFF << 16);
6631 tmp |= (0x1C << 16);
6632 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6633
0ff066a9
PZ
6634 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6635 tmp |= (1 << 27);
6636 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6637
0ff066a9
PZ
6638 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6639 tmp |= (1 << 27);
6640 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6641
0ff066a9
PZ
6642 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6643 tmp &= ~(0xF << 28);
6644 tmp |= (4 << 28);
6645 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6646
0ff066a9
PZ
6647 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6648 tmp &= ~(0xF << 28);
6649 tmp |= (4 << 28);
6650 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6651}
6652
2fa86a1f
PZ
6653/* Implements 3 different sequences from BSpec chapter "Display iCLK
6654 * Programming" based on the parameters passed:
6655 * - Sequence to enable CLKOUT_DP
6656 * - Sequence to enable CLKOUT_DP without spread
6657 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6658 */
6659static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6660 bool with_fdi)
f31f2d55
PZ
6661{
6662 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6663 uint32_t reg, tmp;
6664
6665 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6666 with_spread = true;
6667 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6668 with_fdi, "LP PCH doesn't have FDI\n"))
6669 with_fdi = false;
f31f2d55
PZ
6670
6671 mutex_lock(&dev_priv->dpio_lock);
6672
6673 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6674 tmp &= ~SBI_SSCCTL_DISABLE;
6675 tmp |= SBI_SSCCTL_PATHALT;
6676 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6677
6678 udelay(24);
6679
2fa86a1f
PZ
6680 if (with_spread) {
6681 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6682 tmp &= ~SBI_SSCCTL_PATHALT;
6683 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6684
2fa86a1f
PZ
6685 if (with_fdi) {
6686 lpt_reset_fdi_mphy(dev_priv);
6687 lpt_program_fdi_mphy(dev_priv);
6688 }
6689 }
dde86e2d 6690
2fa86a1f
PZ
6691 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6692 SBI_GEN0 : SBI_DBUFF0;
6693 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6694 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6695 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6696
6697 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6698}
6699
47701c3b
PZ
6700/* Sequence to disable CLKOUT_DP */
6701static void lpt_disable_clkout_dp(struct drm_device *dev)
6702{
6703 struct drm_i915_private *dev_priv = dev->dev_private;
6704 uint32_t reg, tmp;
6705
6706 mutex_lock(&dev_priv->dpio_lock);
6707
6708 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6709 SBI_GEN0 : SBI_DBUFF0;
6710 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6711 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6712 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6713
6714 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6715 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6716 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6717 tmp |= SBI_SSCCTL_PATHALT;
6718 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6719 udelay(32);
6720 }
6721 tmp |= SBI_SSCCTL_DISABLE;
6722 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6723 }
6724
6725 mutex_unlock(&dev_priv->dpio_lock);
6726}
6727
bf8fa3d3
PZ
6728static void lpt_init_pch_refclk(struct drm_device *dev)
6729{
bf8fa3d3
PZ
6730 struct intel_encoder *encoder;
6731 bool has_vga = false;
6732
b2784e15 6733 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6734 switch (encoder->type) {
6735 case INTEL_OUTPUT_ANALOG:
6736 has_vga = true;
6737 break;
6738 }
6739 }
6740
47701c3b
PZ
6741 if (has_vga)
6742 lpt_enable_clkout_dp(dev, true, true);
6743 else
6744 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6745}
6746
dde86e2d
PZ
6747/*
6748 * Initialize reference clocks when the driver loads
6749 */
6750void intel_init_pch_refclk(struct drm_device *dev)
6751{
6752 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6753 ironlake_init_pch_refclk(dev);
6754 else if (HAS_PCH_LPT(dev))
6755 lpt_init_pch_refclk(dev);
6756}
6757
d9d444cb
JB
6758static int ironlake_get_refclk(struct drm_crtc *crtc)
6759{
6760 struct drm_device *dev = crtc->dev;
6761 struct drm_i915_private *dev_priv = dev->dev_private;
6762 struct intel_encoder *encoder;
d9d444cb
JB
6763 int num_connectors = 0;
6764 bool is_lvds = false;
6765
6c2b7c12 6766 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6767 switch (encoder->type) {
6768 case INTEL_OUTPUT_LVDS:
6769 is_lvds = true;
6770 break;
d9d444cb
JB
6771 }
6772 num_connectors++;
6773 }
6774
6775 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6776 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6777 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6778 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6779 }
6780
6781 return 120000;
6782}
6783
6ff93609 6784static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6785{
c8203565 6786 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6787 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6788 int pipe = intel_crtc->pipe;
c8203565
PZ
6789 uint32_t val;
6790
78114071 6791 val = 0;
c8203565 6792
965e0c48 6793 switch (intel_crtc->config.pipe_bpp) {
c8203565 6794 case 18:
dfd07d72 6795 val |= PIPECONF_6BPC;
c8203565
PZ
6796 break;
6797 case 24:
dfd07d72 6798 val |= PIPECONF_8BPC;
c8203565
PZ
6799 break;
6800 case 30:
dfd07d72 6801 val |= PIPECONF_10BPC;
c8203565
PZ
6802 break;
6803 case 36:
dfd07d72 6804 val |= PIPECONF_12BPC;
c8203565
PZ
6805 break;
6806 default:
cc769b62
PZ
6807 /* Case prevented by intel_choose_pipe_bpp_dither. */
6808 BUG();
c8203565
PZ
6809 }
6810
d8b32247 6811 if (intel_crtc->config.dither)
c8203565
PZ
6812 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6813
6ff93609 6814 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6815 val |= PIPECONF_INTERLACED_ILK;
6816 else
6817 val |= PIPECONF_PROGRESSIVE;
6818
50f3b016 6819 if (intel_crtc->config.limited_color_range)
3685a8f3 6820 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6821
c8203565
PZ
6822 I915_WRITE(PIPECONF(pipe), val);
6823 POSTING_READ(PIPECONF(pipe));
6824}
6825
86d3efce
VS
6826/*
6827 * Set up the pipe CSC unit.
6828 *
6829 * Currently only full range RGB to limited range RGB conversion
6830 * is supported, but eventually this should handle various
6831 * RGB<->YCbCr scenarios as well.
6832 */
50f3b016 6833static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6834{
6835 struct drm_device *dev = crtc->dev;
6836 struct drm_i915_private *dev_priv = dev->dev_private;
6837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6838 int pipe = intel_crtc->pipe;
6839 uint16_t coeff = 0x7800; /* 1.0 */
6840
6841 /*
6842 * TODO: Check what kind of values actually come out of the pipe
6843 * with these coeff/postoff values and adjust to get the best
6844 * accuracy. Perhaps we even need to take the bpc value into
6845 * consideration.
6846 */
6847
50f3b016 6848 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6849 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6850
6851 /*
6852 * GY/GU and RY/RU should be the other way around according
6853 * to BSpec, but reality doesn't agree. Just set them up in
6854 * a way that results in the correct picture.
6855 */
6856 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6857 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6858
6859 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6860 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6861
6862 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6863 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6864
6865 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6866 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6867 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6868
6869 if (INTEL_INFO(dev)->gen > 6) {
6870 uint16_t postoff = 0;
6871
50f3b016 6872 if (intel_crtc->config.limited_color_range)
32cf0cb0 6873 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6874
6875 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6876 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6877 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6878
6879 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6880 } else {
6881 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6882
50f3b016 6883 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6884 mode |= CSC_BLACK_SCREEN_OFFSET;
6885
6886 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6887 }
6888}
6889
6ff93609 6890static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6891{
756f85cf
PZ
6892 struct drm_device *dev = crtc->dev;
6893 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6895 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6896 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6897 uint32_t val;
6898
3eff4faa 6899 val = 0;
ee2b0b38 6900
756f85cf 6901 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6902 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6903
6ff93609 6904 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6905 val |= PIPECONF_INTERLACED_ILK;
6906 else
6907 val |= PIPECONF_PROGRESSIVE;
6908
702e7a56
PZ
6909 I915_WRITE(PIPECONF(cpu_transcoder), val);
6910 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6911
6912 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6913 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6914
6915 if (IS_BROADWELL(dev)) {
6916 val = 0;
6917
6918 switch (intel_crtc->config.pipe_bpp) {
6919 case 18:
6920 val |= PIPEMISC_DITHER_6_BPC;
6921 break;
6922 case 24:
6923 val |= PIPEMISC_DITHER_8_BPC;
6924 break;
6925 case 30:
6926 val |= PIPEMISC_DITHER_10_BPC;
6927 break;
6928 case 36:
6929 val |= PIPEMISC_DITHER_12_BPC;
6930 break;
6931 default:
6932 /* Case prevented by pipe_config_set_bpp. */
6933 BUG();
6934 }
6935
6936 if (intel_crtc->config.dither)
6937 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6938
6939 I915_WRITE(PIPEMISC(pipe), val);
6940 }
ee2b0b38
PZ
6941}
6942
6591c6e4 6943static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6944 intel_clock_t *clock,
6945 bool *has_reduced_clock,
6946 intel_clock_t *reduced_clock)
6947{
6948 struct drm_device *dev = crtc->dev;
6949 struct drm_i915_private *dev_priv = dev->dev_private;
6950 struct intel_encoder *intel_encoder;
6951 int refclk;
d4906093 6952 const intel_limit_t *limit;
a16af721 6953 bool ret, is_lvds = false;
79e53945 6954
6591c6e4
PZ
6955 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6956 switch (intel_encoder->type) {
79e53945
JB
6957 case INTEL_OUTPUT_LVDS:
6958 is_lvds = true;
6959 break;
79e53945
JB
6960 }
6961 }
6962
d9d444cb 6963 refclk = ironlake_get_refclk(crtc);
79e53945 6964
d4906093
ML
6965 /*
6966 * Returns a set of divisors for the desired target clock with the given
6967 * refclk, or FALSE. The returned values represent the clock equation:
6968 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6969 */
1b894b59 6970 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6971 ret = dev_priv->display.find_dpll(limit, crtc,
6972 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6973 refclk, NULL, clock);
6591c6e4
PZ
6974 if (!ret)
6975 return false;
cda4b7d3 6976
ddc9003c 6977 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6978 /*
6979 * Ensure we match the reduced clock's P to the target clock.
6980 * If the clocks don't match, we can't switch the display clock
6981 * by using the FP0/FP1. In such case we will disable the LVDS
6982 * downclock feature.
6983 */
ee9300bb
DV
6984 *has_reduced_clock =
6985 dev_priv->display.find_dpll(limit, crtc,
6986 dev_priv->lvds_downclock,
6987 refclk, clock,
6988 reduced_clock);
652c393a 6989 }
61e9653f 6990
6591c6e4
PZ
6991 return true;
6992}
6993
d4b1931c
PZ
6994int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6995{
6996 /*
6997 * Account for spread spectrum to avoid
6998 * oversubscribing the link. Max center spread
6999 * is 2.5%; use 5% for safety's sake.
7000 */
7001 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7002 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7003}
7004
7429e9d4 7005static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7006{
7429e9d4 7007 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7008}
7009
de13a2e3 7010static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7011 u32 *fp,
9a7c7890 7012 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7013{
de13a2e3 7014 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7015 struct drm_device *dev = crtc->dev;
7016 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7017 struct intel_encoder *intel_encoder;
7018 uint32_t dpll;
6cc5f341 7019 int factor, num_connectors = 0;
09ede541 7020 bool is_lvds = false, is_sdvo = false;
79e53945 7021
de13a2e3
PZ
7022 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7023 switch (intel_encoder->type) {
79e53945
JB
7024 case INTEL_OUTPUT_LVDS:
7025 is_lvds = true;
7026 break;
7027 case INTEL_OUTPUT_SDVO:
7d57382e 7028 case INTEL_OUTPUT_HDMI:
79e53945 7029 is_sdvo = true;
79e53945 7030 break;
79e53945 7031 }
43565a06 7032
c751ce4f 7033 num_connectors++;
79e53945 7034 }
79e53945 7035
c1858123 7036 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7037 factor = 21;
7038 if (is_lvds) {
7039 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7040 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7041 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7042 factor = 25;
09ede541 7043 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7044 factor = 20;
c1858123 7045
7429e9d4 7046 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7047 *fp |= FP_CB_TUNE;
2c07245f 7048
9a7c7890
DV
7049 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7050 *fp2 |= FP_CB_TUNE;
7051
5eddb70b 7052 dpll = 0;
2c07245f 7053
a07d6787
EA
7054 if (is_lvds)
7055 dpll |= DPLLB_MODE_LVDS;
7056 else
7057 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7058
ef1b460d
DV
7059 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7060 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7061
7062 if (is_sdvo)
4a33e48d 7063 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7064 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7065 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7066
a07d6787 7067 /* compute bitmask from p1 value */
7429e9d4 7068 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7069 /* also FPA1 */
7429e9d4 7070 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7071
7429e9d4 7072 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7073 case 5:
7074 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7075 break;
7076 case 7:
7077 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7078 break;
7079 case 10:
7080 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7081 break;
7082 case 14:
7083 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7084 break;
79e53945
JB
7085 }
7086
b4c09f3b 7087 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7088 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7089 else
7090 dpll |= PLL_REF_INPUT_DREFCLK;
7091
959e16d6 7092 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7093}
7094
7095static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7096 int x, int y,
7097 struct drm_framebuffer *fb)
7098{
7099 struct drm_device *dev = crtc->dev;
de13a2e3 7100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7101 int num_connectors = 0;
7102 intel_clock_t clock, reduced_clock;
cbbab5bd 7103 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7104 bool ok, has_reduced_clock = false;
8b47047b 7105 bool is_lvds = false;
de13a2e3 7106 struct intel_encoder *encoder;
e2b78267 7107 struct intel_shared_dpll *pll;
de13a2e3
PZ
7108
7109 for_each_encoder_on_crtc(dev, crtc, encoder) {
7110 switch (encoder->type) {
7111 case INTEL_OUTPUT_LVDS:
7112 is_lvds = true;
7113 break;
de13a2e3
PZ
7114 }
7115
7116 num_connectors++;
a07d6787 7117 }
79e53945 7118
5dc5298b
PZ
7119 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7120 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7121
ff9a6750 7122 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7123 &has_reduced_clock, &reduced_clock);
ee9300bb 7124 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7125 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7126 return -EINVAL;
79e53945 7127 }
f47709a9
DV
7128 /* Compat-code for transition, will disappear. */
7129 if (!intel_crtc->config.clock_set) {
7130 intel_crtc->config.dpll.n = clock.n;
7131 intel_crtc->config.dpll.m1 = clock.m1;
7132 intel_crtc->config.dpll.m2 = clock.m2;
7133 intel_crtc->config.dpll.p1 = clock.p1;
7134 intel_crtc->config.dpll.p2 = clock.p2;
7135 }
79e53945 7136
5dc5298b 7137 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7138 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7139 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7140 if (has_reduced_clock)
7429e9d4 7141 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7142
7429e9d4 7143 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7144 &fp, &reduced_clock,
7145 has_reduced_clock ? &fp2 : NULL);
7146
959e16d6 7147 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7148 intel_crtc->config.dpll_hw_state.fp0 = fp;
7149 if (has_reduced_clock)
7150 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7151 else
7152 intel_crtc->config.dpll_hw_state.fp1 = fp;
7153
b89a1d39 7154 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7155 if (pll == NULL) {
84f44ce7 7156 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7157 pipe_name(intel_crtc->pipe));
4b645f14
JB
7158 return -EINVAL;
7159 }
ee7b9f93 7160 } else
e72f9fbf 7161 intel_put_shared_dpll(intel_crtc);
79e53945 7162
d330a953 7163 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7164 intel_crtc->lowfreq_avail = true;
7165 else
7166 intel_crtc->lowfreq_avail = false;
e2b78267 7167
c8f7a0db 7168 return 0;
79e53945
JB
7169}
7170
eb14cb74
VS
7171static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7172 struct intel_link_m_n *m_n)
7173{
7174 struct drm_device *dev = crtc->base.dev;
7175 struct drm_i915_private *dev_priv = dev->dev_private;
7176 enum pipe pipe = crtc->pipe;
7177
7178 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7179 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7180 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7181 & ~TU_SIZE_MASK;
7182 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7183 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7184 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7185}
7186
7187static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7188 enum transcoder transcoder,
b95af8be
VK
7189 struct intel_link_m_n *m_n,
7190 struct intel_link_m_n *m2_n2)
72419203
DV
7191{
7192 struct drm_device *dev = crtc->base.dev;
7193 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7194 enum pipe pipe = crtc->pipe;
72419203 7195
eb14cb74
VS
7196 if (INTEL_INFO(dev)->gen >= 5) {
7197 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7198 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7199 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7200 & ~TU_SIZE_MASK;
7201 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7202 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7203 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7204 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7205 * gen < 8) and if DRRS is supported (to make sure the
7206 * registers are not unnecessarily read).
7207 */
7208 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7209 crtc->config.has_drrs) {
7210 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7211 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7212 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7213 & ~TU_SIZE_MASK;
7214 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7215 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7216 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7217 }
eb14cb74
VS
7218 } else {
7219 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7220 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7221 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7222 & ~TU_SIZE_MASK;
7223 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7224 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7225 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7226 }
7227}
7228
7229void intel_dp_get_m_n(struct intel_crtc *crtc,
7230 struct intel_crtc_config *pipe_config)
7231{
7232 if (crtc->config.has_pch_encoder)
7233 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7234 else
7235 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7236 &pipe_config->dp_m_n,
7237 &pipe_config->dp_m2_n2);
eb14cb74 7238}
72419203 7239
eb14cb74
VS
7240static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7241 struct intel_crtc_config *pipe_config)
7242{
7243 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7244 &pipe_config->fdi_m_n, NULL);
72419203
DV
7245}
7246
2fa2fe9a
DV
7247static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7248 struct intel_crtc_config *pipe_config)
7249{
7250 struct drm_device *dev = crtc->base.dev;
7251 struct drm_i915_private *dev_priv = dev->dev_private;
7252 uint32_t tmp;
7253
7254 tmp = I915_READ(PF_CTL(crtc->pipe));
7255
7256 if (tmp & PF_ENABLE) {
fd4daa9c 7257 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7258 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7259 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7260
7261 /* We currently do not free assignements of panel fitters on
7262 * ivb/hsw (since we don't use the higher upscaling modes which
7263 * differentiates them) so just WARN about this case for now. */
7264 if (IS_GEN7(dev)) {
7265 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7266 PF_PIPE_SEL_IVB(crtc->pipe));
7267 }
2fa2fe9a 7268 }
79e53945
JB
7269}
7270
4c6baa59
JB
7271static void ironlake_get_plane_config(struct intel_crtc *crtc,
7272 struct intel_plane_config *plane_config)
7273{
7274 struct drm_device *dev = crtc->base.dev;
7275 struct drm_i915_private *dev_priv = dev->dev_private;
7276 u32 val, base, offset;
7277 int pipe = crtc->pipe, plane = crtc->plane;
7278 int fourcc, pixel_format;
7279 int aligned_height;
7280
66e514c1
DA
7281 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7282 if (!crtc->base.primary->fb) {
4c6baa59
JB
7283 DRM_DEBUG_KMS("failed to alloc fb\n");
7284 return;
7285 }
7286
7287 val = I915_READ(DSPCNTR(plane));
7288
7289 if (INTEL_INFO(dev)->gen >= 4)
7290 if (val & DISPPLANE_TILED)
7291 plane_config->tiled = true;
7292
7293 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7294 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7295 crtc->base.primary->fb->pixel_format = fourcc;
7296 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7297 drm_format_plane_cpp(fourcc, 0) * 8;
7298
7299 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7300 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7301 offset = I915_READ(DSPOFFSET(plane));
7302 } else {
7303 if (plane_config->tiled)
7304 offset = I915_READ(DSPTILEOFF(plane));
7305 else
7306 offset = I915_READ(DSPLINOFF(plane));
7307 }
7308 plane_config->base = base;
7309
7310 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7311 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7312 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7313
7314 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7315 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7316
66e514c1 7317 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7318 plane_config->tiled);
7319
1267a26b
FF
7320 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7321 aligned_height);
4c6baa59
JB
7322
7323 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7324 pipe, plane, crtc->base.primary->fb->width,
7325 crtc->base.primary->fb->height,
7326 crtc->base.primary->fb->bits_per_pixel, base,
7327 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7328 plane_config->size);
7329}
7330
0e8ffe1b
DV
7331static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7332 struct intel_crtc_config *pipe_config)
7333{
7334 struct drm_device *dev = crtc->base.dev;
7335 struct drm_i915_private *dev_priv = dev->dev_private;
7336 uint32_t tmp;
7337
930e8c9e
PZ
7338 if (!intel_display_power_enabled(dev_priv,
7339 POWER_DOMAIN_PIPE(crtc->pipe)))
7340 return false;
7341
e143a21c 7342 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7343 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7344
0e8ffe1b
DV
7345 tmp = I915_READ(PIPECONF(crtc->pipe));
7346 if (!(tmp & PIPECONF_ENABLE))
7347 return false;
7348
42571aef
VS
7349 switch (tmp & PIPECONF_BPC_MASK) {
7350 case PIPECONF_6BPC:
7351 pipe_config->pipe_bpp = 18;
7352 break;
7353 case PIPECONF_8BPC:
7354 pipe_config->pipe_bpp = 24;
7355 break;
7356 case PIPECONF_10BPC:
7357 pipe_config->pipe_bpp = 30;
7358 break;
7359 case PIPECONF_12BPC:
7360 pipe_config->pipe_bpp = 36;
7361 break;
7362 default:
7363 break;
7364 }
7365
b5a9fa09
DV
7366 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7367 pipe_config->limited_color_range = true;
7368
ab9412ba 7369 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7370 struct intel_shared_dpll *pll;
7371
88adfff1
DV
7372 pipe_config->has_pch_encoder = true;
7373
627eb5a3
DV
7374 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7375 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7376 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7377
7378 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7379
c0d43d62 7380 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7381 pipe_config->shared_dpll =
7382 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7383 } else {
7384 tmp = I915_READ(PCH_DPLL_SEL);
7385 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7386 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7387 else
7388 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7389 }
66e985c0
DV
7390
7391 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7392
7393 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7394 &pipe_config->dpll_hw_state));
c93f54cf
DV
7395
7396 tmp = pipe_config->dpll_hw_state.dpll;
7397 pipe_config->pixel_multiplier =
7398 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7399 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7400
7401 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7402 } else {
7403 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7404 }
7405
1bd1bd80
DV
7406 intel_get_pipe_timings(crtc, pipe_config);
7407
2fa2fe9a
DV
7408 ironlake_get_pfit_config(crtc, pipe_config);
7409
0e8ffe1b
DV
7410 return true;
7411}
7412
be256dc7
PZ
7413static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7414{
7415 struct drm_device *dev = dev_priv->dev;
be256dc7 7416 struct intel_crtc *crtc;
be256dc7 7417
d3fcc808 7418 for_each_intel_crtc(dev, crtc)
798183c5 7419 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7420 pipe_name(crtc->pipe));
7421
7422 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7423 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7424 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7425 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7426 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7427 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7428 "CPU PWM1 enabled\n");
c5107b87
PZ
7429 if (IS_HASWELL(dev))
7430 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7431 "CPU PWM2 enabled\n");
be256dc7
PZ
7432 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7433 "PCH PWM1 enabled\n");
7434 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7435 "Utility pin enabled\n");
7436 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7437
9926ada1
PZ
7438 /*
7439 * In theory we can still leave IRQs enabled, as long as only the HPD
7440 * interrupts remain enabled. We used to check for that, but since it's
7441 * gen-specific and since we only disable LCPLL after we fully disable
7442 * the interrupts, the check below should be enough.
7443 */
9df7575f 7444 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7445}
7446
9ccd5aeb
PZ
7447static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7448{
7449 struct drm_device *dev = dev_priv->dev;
7450
7451 if (IS_HASWELL(dev))
7452 return I915_READ(D_COMP_HSW);
7453 else
7454 return I915_READ(D_COMP_BDW);
7455}
7456
3c4c9b81
PZ
7457static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7458{
7459 struct drm_device *dev = dev_priv->dev;
7460
7461 if (IS_HASWELL(dev)) {
7462 mutex_lock(&dev_priv->rps.hw_lock);
7463 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7464 val))
f475dadf 7465 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7466 mutex_unlock(&dev_priv->rps.hw_lock);
7467 } else {
9ccd5aeb
PZ
7468 I915_WRITE(D_COMP_BDW, val);
7469 POSTING_READ(D_COMP_BDW);
3c4c9b81 7470 }
be256dc7
PZ
7471}
7472
7473/*
7474 * This function implements pieces of two sequences from BSpec:
7475 * - Sequence for display software to disable LCPLL
7476 * - Sequence for display software to allow package C8+
7477 * The steps implemented here are just the steps that actually touch the LCPLL
7478 * register. Callers should take care of disabling all the display engine
7479 * functions, doing the mode unset, fixing interrupts, etc.
7480 */
6ff58d53
PZ
7481static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7482 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7483{
7484 uint32_t val;
7485
7486 assert_can_disable_lcpll(dev_priv);
7487
7488 val = I915_READ(LCPLL_CTL);
7489
7490 if (switch_to_fclk) {
7491 val |= LCPLL_CD_SOURCE_FCLK;
7492 I915_WRITE(LCPLL_CTL, val);
7493
7494 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7495 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7496 DRM_ERROR("Switching to FCLK failed\n");
7497
7498 val = I915_READ(LCPLL_CTL);
7499 }
7500
7501 val |= LCPLL_PLL_DISABLE;
7502 I915_WRITE(LCPLL_CTL, val);
7503 POSTING_READ(LCPLL_CTL);
7504
7505 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7506 DRM_ERROR("LCPLL still locked\n");
7507
9ccd5aeb 7508 val = hsw_read_dcomp(dev_priv);
be256dc7 7509 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7510 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7511 ndelay(100);
7512
9ccd5aeb
PZ
7513 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7514 1))
be256dc7
PZ
7515 DRM_ERROR("D_COMP RCOMP still in progress\n");
7516
7517 if (allow_power_down) {
7518 val = I915_READ(LCPLL_CTL);
7519 val |= LCPLL_POWER_DOWN_ALLOW;
7520 I915_WRITE(LCPLL_CTL, val);
7521 POSTING_READ(LCPLL_CTL);
7522 }
7523}
7524
7525/*
7526 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7527 * source.
7528 */
6ff58d53 7529static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7530{
7531 uint32_t val;
a8a8bd54 7532 unsigned long irqflags;
be256dc7
PZ
7533
7534 val = I915_READ(LCPLL_CTL);
7535
7536 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7537 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7538 return;
7539
a8a8bd54
PZ
7540 /*
7541 * Make sure we're not on PC8 state before disabling PC8, otherwise
7542 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7543 *
7544 * The other problem is that hsw_restore_lcpll() is called as part of
7545 * the runtime PM resume sequence, so we can't just call
7546 * gen6_gt_force_wake_get() because that function calls
7547 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7548 * while we are on the resume sequence. So to solve this problem we have
7549 * to call special forcewake code that doesn't touch runtime PM and
7550 * doesn't enable the forcewake delayed work.
7551 */
7552 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7553 if (dev_priv->uncore.forcewake_count++ == 0)
7554 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7555 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7556
be256dc7
PZ
7557 if (val & LCPLL_POWER_DOWN_ALLOW) {
7558 val &= ~LCPLL_POWER_DOWN_ALLOW;
7559 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7560 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7561 }
7562
9ccd5aeb 7563 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7564 val |= D_COMP_COMP_FORCE;
7565 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7566 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7567
7568 val = I915_READ(LCPLL_CTL);
7569 val &= ~LCPLL_PLL_DISABLE;
7570 I915_WRITE(LCPLL_CTL, val);
7571
7572 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7573 DRM_ERROR("LCPLL not locked yet\n");
7574
7575 if (val & LCPLL_CD_SOURCE_FCLK) {
7576 val = I915_READ(LCPLL_CTL);
7577 val &= ~LCPLL_CD_SOURCE_FCLK;
7578 I915_WRITE(LCPLL_CTL, val);
7579
7580 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7581 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7582 DRM_ERROR("Switching back to LCPLL failed\n");
7583 }
215733fa 7584
a8a8bd54
PZ
7585 /* See the big comment above. */
7586 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7587 if (--dev_priv->uncore.forcewake_count == 0)
7588 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7589 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7590}
7591
765dab67
PZ
7592/*
7593 * Package states C8 and deeper are really deep PC states that can only be
7594 * reached when all the devices on the system allow it, so even if the graphics
7595 * device allows PC8+, it doesn't mean the system will actually get to these
7596 * states. Our driver only allows PC8+ when going into runtime PM.
7597 *
7598 * The requirements for PC8+ are that all the outputs are disabled, the power
7599 * well is disabled and most interrupts are disabled, and these are also
7600 * requirements for runtime PM. When these conditions are met, we manually do
7601 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7602 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7603 * hang the machine.
7604 *
7605 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7606 * the state of some registers, so when we come back from PC8+ we need to
7607 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7608 * need to take care of the registers kept by RC6. Notice that this happens even
7609 * if we don't put the device in PCI D3 state (which is what currently happens
7610 * because of the runtime PM support).
7611 *
7612 * For more, read "Display Sequences for Package C8" on the hardware
7613 * documentation.
7614 */
a14cb6fc 7615void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7616{
c67a470b
PZ
7617 struct drm_device *dev = dev_priv->dev;
7618 uint32_t val;
7619
c67a470b
PZ
7620 DRM_DEBUG_KMS("Enabling package C8+\n");
7621
c67a470b
PZ
7622 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7623 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7624 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7625 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7626 }
7627
7628 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7629 hsw_disable_lcpll(dev_priv, true, true);
7630}
7631
a14cb6fc 7632void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7633{
7634 struct drm_device *dev = dev_priv->dev;
7635 uint32_t val;
7636
c67a470b
PZ
7637 DRM_DEBUG_KMS("Disabling package C8+\n");
7638
7639 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7640 lpt_init_pch_refclk(dev);
7641
7642 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7643 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7644 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7645 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7646 }
7647
7648 intel_prepare_ddi(dev);
c67a470b
PZ
7649}
7650
9a952a0d
PZ
7651static void snb_modeset_global_resources(struct drm_device *dev)
7652{
7653 modeset_update_crtc_power_domains(dev);
7654}
7655
4f074129
ID
7656static void haswell_modeset_global_resources(struct drm_device *dev)
7657{
da723569 7658 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7659}
7660
09b4ddf9 7661static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7662 int x, int y,
7663 struct drm_framebuffer *fb)
7664{
09b4ddf9 7665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7666
566b734a 7667 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7668 return -EINVAL;
716c2e55 7669
644cef34
DV
7670 intel_crtc->lowfreq_avail = false;
7671
c8f7a0db 7672 return 0;
79e53945
JB
7673}
7674
7d2c8175
DL
7675static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7676 enum port port,
7677 struct intel_crtc_config *pipe_config)
7678{
7679 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7680
7681 switch (pipe_config->ddi_pll_sel) {
7682 case PORT_CLK_SEL_WRPLL1:
7683 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7684 break;
7685 case PORT_CLK_SEL_WRPLL2:
7686 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7687 break;
7688 }
7689}
7690
26804afd
DV
7691static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7692 struct intel_crtc_config *pipe_config)
7693{
7694 struct drm_device *dev = crtc->base.dev;
7695 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7696 struct intel_shared_dpll *pll;
26804afd
DV
7697 enum port port;
7698 uint32_t tmp;
7699
7700 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7701
7702 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7703
7d2c8175 7704 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7705
d452c5b6
DV
7706 if (pipe_config->shared_dpll >= 0) {
7707 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7708
7709 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7710 &pipe_config->dpll_hw_state));
7711 }
7712
26804afd
DV
7713 /*
7714 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7715 * DDI E. So just check whether this pipe is wired to DDI E and whether
7716 * the PCH transcoder is on.
7717 */
7718 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7719 pipe_config->has_pch_encoder = true;
7720
7721 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7722 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7723 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7724
7725 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7726 }
7727}
7728
0e8ffe1b
DV
7729static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7730 struct intel_crtc_config *pipe_config)
7731{
7732 struct drm_device *dev = crtc->base.dev;
7733 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7734 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7735 uint32_t tmp;
7736
b5482bd0
ID
7737 if (!intel_display_power_enabled(dev_priv,
7738 POWER_DOMAIN_PIPE(crtc->pipe)))
7739 return false;
7740
e143a21c 7741 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7742 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7743
eccb140b
DV
7744 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7745 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7746 enum pipe trans_edp_pipe;
7747 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7748 default:
7749 WARN(1, "unknown pipe linked to edp transcoder\n");
7750 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7751 case TRANS_DDI_EDP_INPUT_A_ON:
7752 trans_edp_pipe = PIPE_A;
7753 break;
7754 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7755 trans_edp_pipe = PIPE_B;
7756 break;
7757 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7758 trans_edp_pipe = PIPE_C;
7759 break;
7760 }
7761
7762 if (trans_edp_pipe == crtc->pipe)
7763 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7764 }
7765
da7e29bd 7766 if (!intel_display_power_enabled(dev_priv,
eccb140b 7767 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7768 return false;
7769
eccb140b 7770 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7771 if (!(tmp & PIPECONF_ENABLE))
7772 return false;
7773
26804afd 7774 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7775
1bd1bd80
DV
7776 intel_get_pipe_timings(crtc, pipe_config);
7777
2fa2fe9a 7778 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7779 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7780 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7781
e59150dc
JB
7782 if (IS_HASWELL(dev))
7783 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7784 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7785
6c49f241
DV
7786 pipe_config->pixel_multiplier = 1;
7787
0e8ffe1b
DV
7788 return true;
7789}
7790
1a91510d
JN
7791static struct {
7792 int clock;
7793 u32 config;
7794} hdmi_audio_clock[] = {
7795 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7796 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7797 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7798 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7799 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7800 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7801 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7802 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7803 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7804 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7805};
7806
7807/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7808static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7809{
7810 int i;
7811
7812 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7813 if (mode->clock == hdmi_audio_clock[i].clock)
7814 break;
7815 }
7816
7817 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7818 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7819 i = 1;
7820 }
7821
7822 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7823 hdmi_audio_clock[i].clock,
7824 hdmi_audio_clock[i].config);
7825
7826 return hdmi_audio_clock[i].config;
7827}
7828
3a9627f4
WF
7829static bool intel_eld_uptodate(struct drm_connector *connector,
7830 int reg_eldv, uint32_t bits_eldv,
7831 int reg_elda, uint32_t bits_elda,
7832 int reg_edid)
7833{
7834 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7835 uint8_t *eld = connector->eld;
7836 uint32_t i;
7837
7838 i = I915_READ(reg_eldv);
7839 i &= bits_eldv;
7840
7841 if (!eld[0])
7842 return !i;
7843
7844 if (!i)
7845 return false;
7846
7847 i = I915_READ(reg_elda);
7848 i &= ~bits_elda;
7849 I915_WRITE(reg_elda, i);
7850
7851 for (i = 0; i < eld[2]; i++)
7852 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7853 return false;
7854
7855 return true;
7856}
7857
e0dac65e 7858static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7859 struct drm_crtc *crtc,
7860 struct drm_display_mode *mode)
e0dac65e
WF
7861{
7862 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7863 uint8_t *eld = connector->eld;
7864 uint32_t eldv;
7865 uint32_t len;
7866 uint32_t i;
7867
7868 i = I915_READ(G4X_AUD_VID_DID);
7869
7870 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7871 eldv = G4X_ELDV_DEVCL_DEVBLC;
7872 else
7873 eldv = G4X_ELDV_DEVCTG;
7874
3a9627f4
WF
7875 if (intel_eld_uptodate(connector,
7876 G4X_AUD_CNTL_ST, eldv,
7877 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7878 G4X_HDMIW_HDMIEDID))
7879 return;
7880
e0dac65e
WF
7881 i = I915_READ(G4X_AUD_CNTL_ST);
7882 i &= ~(eldv | G4X_ELD_ADDR);
7883 len = (i >> 9) & 0x1f; /* ELD buffer size */
7884 I915_WRITE(G4X_AUD_CNTL_ST, i);
7885
7886 if (!eld[0])
7887 return;
7888
7889 len = min_t(uint8_t, eld[2], len);
7890 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7891 for (i = 0; i < len; i++)
7892 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7893
7894 i = I915_READ(G4X_AUD_CNTL_ST);
7895 i |= eldv;
7896 I915_WRITE(G4X_AUD_CNTL_ST, i);
7897}
7898
83358c85 7899static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7900 struct drm_crtc *crtc,
7901 struct drm_display_mode *mode)
83358c85
WX
7902{
7903 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7904 uint8_t *eld = connector->eld;
83358c85
WX
7905 uint32_t eldv;
7906 uint32_t i;
7907 int len;
7908 int pipe = to_intel_crtc(crtc)->pipe;
7909 int tmp;
7910
7911 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7912 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7913 int aud_config = HSW_AUD_CFG(pipe);
7914 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7915
83358c85
WX
7916 /* Audio output enable */
7917 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7918 tmp = I915_READ(aud_cntrl_st2);
7919 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7920 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7921 POSTING_READ(aud_cntrl_st2);
83358c85 7922
c7905792 7923 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7924
7925 /* Set ELD valid state */
7926 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7927 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7928 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7929 I915_WRITE(aud_cntrl_st2, tmp);
7930 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7931 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7932
7933 /* Enable HDMI mode */
7934 tmp = I915_READ(aud_config);
7e7cb34f 7935 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7936 /* clear N_programing_enable and N_value_index */
7937 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7938 I915_WRITE(aud_config, tmp);
7939
7940 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7941
7942 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7943
7944 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7945 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7946 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7947 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7948 } else {
7949 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7950 }
83358c85
WX
7951
7952 if (intel_eld_uptodate(connector,
7953 aud_cntrl_st2, eldv,
7954 aud_cntl_st, IBX_ELD_ADDRESS,
7955 hdmiw_hdmiedid))
7956 return;
7957
7958 i = I915_READ(aud_cntrl_st2);
7959 i &= ~eldv;
7960 I915_WRITE(aud_cntrl_st2, i);
7961
7962 if (!eld[0])
7963 return;
7964
7965 i = I915_READ(aud_cntl_st);
7966 i &= ~IBX_ELD_ADDRESS;
7967 I915_WRITE(aud_cntl_st, i);
7968 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7969 DRM_DEBUG_DRIVER("port num:%d\n", i);
7970
7971 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7972 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7973 for (i = 0; i < len; i++)
7974 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7975
7976 i = I915_READ(aud_cntrl_st2);
7977 i |= eldv;
7978 I915_WRITE(aud_cntrl_st2, i);
7979
7980}
7981
e0dac65e 7982static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7983 struct drm_crtc *crtc,
7984 struct drm_display_mode *mode)
e0dac65e
WF
7985{
7986 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7987 uint8_t *eld = connector->eld;
7988 uint32_t eldv;
7989 uint32_t i;
7990 int len;
7991 int hdmiw_hdmiedid;
b6daa025 7992 int aud_config;
e0dac65e
WF
7993 int aud_cntl_st;
7994 int aud_cntrl_st2;
9b138a83 7995 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7996
b3f33cbf 7997 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7998 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7999 aud_config = IBX_AUD_CFG(pipe);
8000 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8001 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8002 } else if (IS_VALLEYVIEW(connector->dev)) {
8003 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8004 aud_config = VLV_AUD_CFG(pipe);
8005 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8006 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8007 } else {
9b138a83
WX
8008 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8009 aud_config = CPT_AUD_CFG(pipe);
8010 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8011 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8012 }
8013
9b138a83 8014 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8015
9ca2fe73
ML
8016 if (IS_VALLEYVIEW(connector->dev)) {
8017 struct intel_encoder *intel_encoder;
8018 struct intel_digital_port *intel_dig_port;
8019
8020 intel_encoder = intel_attached_encoder(connector);
8021 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8022 i = intel_dig_port->port;
8023 } else {
8024 i = I915_READ(aud_cntl_st);
8025 i = (i >> 29) & DIP_PORT_SEL_MASK;
8026 /* DIP_Port_Select, 0x1 = PortB */
8027 }
8028
e0dac65e
WF
8029 if (!i) {
8030 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8031 /* operate blindly on all ports */
1202b4c6
WF
8032 eldv = IBX_ELD_VALIDB;
8033 eldv |= IBX_ELD_VALIDB << 4;
8034 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8035 } else {
2582a850 8036 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8037 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8038 }
8039
3a9627f4
WF
8040 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8041 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8042 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8043 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8044 } else {
8045 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8046 }
e0dac65e 8047
3a9627f4
WF
8048 if (intel_eld_uptodate(connector,
8049 aud_cntrl_st2, eldv,
8050 aud_cntl_st, IBX_ELD_ADDRESS,
8051 hdmiw_hdmiedid))
8052 return;
8053
e0dac65e
WF
8054 i = I915_READ(aud_cntrl_st2);
8055 i &= ~eldv;
8056 I915_WRITE(aud_cntrl_st2, i);
8057
8058 if (!eld[0])
8059 return;
8060
e0dac65e 8061 i = I915_READ(aud_cntl_st);
1202b4c6 8062 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8063 I915_WRITE(aud_cntl_st, i);
8064
8065 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8066 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8067 for (i = 0; i < len; i++)
8068 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8069
8070 i = I915_READ(aud_cntrl_st2);
8071 i |= eldv;
8072 I915_WRITE(aud_cntrl_st2, i);
8073}
8074
8075void intel_write_eld(struct drm_encoder *encoder,
8076 struct drm_display_mode *mode)
8077{
8078 struct drm_crtc *crtc = encoder->crtc;
8079 struct drm_connector *connector;
8080 struct drm_device *dev = encoder->dev;
8081 struct drm_i915_private *dev_priv = dev->dev_private;
8082
8083 connector = drm_select_eld(encoder, mode);
8084 if (!connector)
8085 return;
8086
8087 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8088 connector->base.id,
c23cc417 8089 connector->name,
e0dac65e 8090 connector->encoder->base.id,
8e329a03 8091 connector->encoder->name);
e0dac65e
WF
8092
8093 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8094
8095 if (dev_priv->display.write_eld)
34427052 8096 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8097}
8098
560b85bb
CW
8099static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8100{
8101 struct drm_device *dev = crtc->dev;
8102 struct drm_i915_private *dev_priv = dev->dev_private;
8103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8104 uint32_t cntl = 0, size = 0;
560b85bb 8105
dc41c154
VS
8106 if (base) {
8107 unsigned int width = intel_crtc->cursor_width;
8108 unsigned int height = intel_crtc->cursor_height;
8109 unsigned int stride = roundup_pow_of_two(width) * 4;
8110
8111 switch (stride) {
8112 default:
8113 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8114 width, stride);
8115 stride = 256;
8116 /* fallthrough */
8117 case 256:
8118 case 512:
8119 case 1024:
8120 case 2048:
8121 break;
4b0e333e
CW
8122 }
8123
dc41c154
VS
8124 cntl |= CURSOR_ENABLE |
8125 CURSOR_GAMMA_ENABLE |
8126 CURSOR_FORMAT_ARGB |
8127 CURSOR_STRIDE(stride);
8128
8129 size = (height << 12) | width;
4b0e333e 8130 }
560b85bb 8131
dc41c154
VS
8132 if (intel_crtc->cursor_cntl != 0 &&
8133 (intel_crtc->cursor_base != base ||
8134 intel_crtc->cursor_size != size ||
8135 intel_crtc->cursor_cntl != cntl)) {
8136 /* On these chipsets we can only modify the base/size/stride
8137 * whilst the cursor is disabled.
8138 */
8139 I915_WRITE(_CURACNTR, 0);
4b0e333e 8140 POSTING_READ(_CURACNTR);
dc41c154 8141 intel_crtc->cursor_cntl = 0;
4b0e333e 8142 }
560b85bb 8143
dc41c154 8144 if (intel_crtc->cursor_base != base)
9db4a9c7 8145 I915_WRITE(_CURABASE, base);
4726e0b0 8146
dc41c154
VS
8147 if (intel_crtc->cursor_size != size) {
8148 I915_WRITE(CURSIZE, size);
8149 intel_crtc->cursor_size = size;
4b0e333e 8150 }
560b85bb 8151
4b0e333e 8152 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8153 I915_WRITE(_CURACNTR, cntl);
8154 POSTING_READ(_CURACNTR);
4b0e333e 8155 intel_crtc->cursor_cntl = cntl;
560b85bb 8156 }
560b85bb
CW
8157}
8158
560b85bb 8159static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8160{
8161 struct drm_device *dev = crtc->dev;
8162 struct drm_i915_private *dev_priv = dev->dev_private;
8163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8164 int pipe = intel_crtc->pipe;
4b0e333e
CW
8165 uint32_t cntl;
8166
8167 cntl = 0;
8168 if (base) {
8169 cntl = MCURSOR_GAMMA_ENABLE;
8170 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8171 case 64:
8172 cntl |= CURSOR_MODE_64_ARGB_AX;
8173 break;
8174 case 128:
8175 cntl |= CURSOR_MODE_128_ARGB_AX;
8176 break;
8177 case 256:
8178 cntl |= CURSOR_MODE_256_ARGB_AX;
8179 break;
8180 default:
8181 WARN_ON(1);
8182 return;
65a21cd6 8183 }
4b0e333e 8184 cntl |= pipe << 28; /* Connect to correct pipe */
4b0e333e
CW
8185 }
8186 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8187 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 8188
4b0e333e
CW
8189 if (intel_crtc->cursor_cntl != cntl) {
8190 I915_WRITE(CURCNTR(pipe), cntl);
8191 POSTING_READ(CURCNTR(pipe));
8192 intel_crtc->cursor_cntl = cntl;
65a21cd6 8193 }
4b0e333e 8194
65a21cd6 8195 /* and commit changes on next vblank */
5efb3e28
VS
8196 I915_WRITE(CURBASE(pipe), base);
8197 POSTING_READ(CURBASE(pipe));
65a21cd6
JB
8198}
8199
cda4b7d3 8200/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8201static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8202 bool on)
cda4b7d3
CW
8203{
8204 struct drm_device *dev = crtc->dev;
8205 struct drm_i915_private *dev_priv = dev->dev_private;
8206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8207 int pipe = intel_crtc->pipe;
3d7d6510
MR
8208 int x = crtc->cursor_x;
8209 int y = crtc->cursor_y;
d6e4db15 8210 u32 base = 0, pos = 0;
cda4b7d3 8211
d6e4db15 8212 if (on)
cda4b7d3 8213 base = intel_crtc->cursor_addr;
cda4b7d3 8214
d6e4db15
VS
8215 if (x >= intel_crtc->config.pipe_src_w)
8216 base = 0;
8217
8218 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8219 base = 0;
8220
8221 if (x < 0) {
efc9064e 8222 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8223 base = 0;
8224
8225 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8226 x = -x;
8227 }
8228 pos |= x << CURSOR_X_SHIFT;
8229
8230 if (y < 0) {
efc9064e 8231 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8232 base = 0;
8233
8234 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8235 y = -y;
8236 }
8237 pos |= y << CURSOR_Y_SHIFT;
8238
4b0e333e 8239 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8240 return;
8241
5efb3e28
VS
8242 I915_WRITE(CURPOS(pipe), pos);
8243
8ac54669 8244 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8245 i845_update_cursor(crtc, base);
8246 else
8247 i9xx_update_cursor(crtc, base);
4b0e333e 8248 intel_crtc->cursor_base = base;
cda4b7d3
CW
8249}
8250
dc41c154
VS
8251static bool cursor_size_ok(struct drm_device *dev,
8252 uint32_t width, uint32_t height)
8253{
8254 if (width == 0 || height == 0)
8255 return false;
8256
8257 /*
8258 * 845g/865g are special in that they are only limited by
8259 * the width of their cursors, the height is arbitrary up to
8260 * the precision of the register. Everything else requires
8261 * square cursors, limited to a few power-of-two sizes.
8262 */
8263 if (IS_845G(dev) || IS_I865G(dev)) {
8264 if ((width & 63) != 0)
8265 return false;
8266
8267 if (width > (IS_845G(dev) ? 64 : 512))
8268 return false;
8269
8270 if (height > 1023)
8271 return false;
8272 } else {
8273 switch (width | height) {
8274 case 256:
8275 case 128:
8276 if (IS_GEN2(dev))
8277 return false;
8278 case 64:
8279 break;
8280 default:
8281 return false;
8282 }
8283 }
8284
8285 return true;
8286}
8287
e3287951
MR
8288/*
8289 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8290 *
8291 * Note that the object's reference will be consumed if the update fails. If
8292 * the update succeeds, the reference of the old object (if any) will be
8293 * consumed.
8294 */
8295static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8296 struct drm_i915_gem_object *obj,
8297 uint32_t width, uint32_t height)
79e53945
JB
8298{
8299 struct drm_device *dev = crtc->dev;
79e53945 8300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8301 enum pipe pipe = intel_crtc->pipe;
dc41c154 8302 unsigned old_width, stride;
cda4b7d3 8303 uint32_t addr;
3f8bc370 8304 int ret;
79e53945 8305
79e53945 8306 /* if we want to turn off the cursor ignore width and height */
e3287951 8307 if (!obj) {
28c97730 8308 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8309 addr = 0;
5004417d 8310 mutex_lock(&dev->struct_mutex);
3f8bc370 8311 goto finish;
79e53945
JB
8312 }
8313
4726e0b0 8314 /* Check for which cursor types we support */
dc41c154 8315 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8316 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8317 return -EINVAL;
8318 }
8319
dc41c154
VS
8320 stride = roundup_pow_of_two(width) * 4;
8321 if (obj->base.size < stride * height) {
e3287951 8322 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8323 ret = -ENOMEM;
8324 goto fail;
79e53945
JB
8325 }
8326
71acb5eb 8327 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8328 mutex_lock(&dev->struct_mutex);
3d13ef2e 8329 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8330 unsigned alignment;
8331
d9e86c0e 8332 if (obj->tiling_mode) {
3b25b31f 8333 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8334 ret = -EINVAL;
8335 goto fail_locked;
8336 }
8337
693db184
CW
8338 /* Note that the w/a also requires 2 PTE of padding following
8339 * the bo. We currently fill all unused PTE with the shadow
8340 * page and so we should always have valid PTE following the
8341 * cursor preventing the VT-d warning.
8342 */
8343 alignment = 0;
8344 if (need_vtd_wa(dev))
8345 alignment = 64*1024;
8346
8347 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8348 if (ret) {
3b25b31f 8349 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 8350 goto fail_locked;
e7b526bb
CW
8351 }
8352
d9e86c0e
CW
8353 ret = i915_gem_object_put_fence(obj);
8354 if (ret) {
3b25b31f 8355 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
8356 goto fail_unpin;
8357 }
8358
f343c5f6 8359 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 8360 } else {
6eeefaf3 8361 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8362 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8363 if (ret) {
3b25b31f 8364 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8365 goto fail_locked;
71acb5eb 8366 }
00731155 8367 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8368 }
8369
3f8bc370 8370 finish:
3f8bc370 8371 if (intel_crtc->cursor_bo) {
00731155 8372 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8373 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8374 }
80824003 8375
a071fa00
DV
8376 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8377 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8378 mutex_unlock(&dev->struct_mutex);
3f8bc370 8379
64f962e3
CW
8380 old_width = intel_crtc->cursor_width;
8381
3f8bc370 8382 intel_crtc->cursor_addr = addr;
05394f39 8383 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8384 intel_crtc->cursor_width = width;
8385 intel_crtc->cursor_height = height;
8386
64f962e3
CW
8387 if (intel_crtc->active) {
8388 if (old_width != width)
8389 intel_update_watermarks(crtc);
f2f5f771 8390 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8391 }
3f8bc370 8392
f99d7069
DV
8393 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8394
79e53945 8395 return 0;
e7b526bb 8396fail_unpin:
cc98b413 8397 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8398fail_locked:
34b8686e 8399 mutex_unlock(&dev->struct_mutex);
bc9025bd 8400fail:
05394f39 8401 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8402 return ret;
79e53945
JB
8403}
8404
79e53945 8405static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8406 u16 *blue, uint32_t start, uint32_t size)
79e53945 8407{
7203425a 8408 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8410
7203425a 8411 for (i = start; i < end; i++) {
79e53945
JB
8412 intel_crtc->lut_r[i] = red[i] >> 8;
8413 intel_crtc->lut_g[i] = green[i] >> 8;
8414 intel_crtc->lut_b[i] = blue[i] >> 8;
8415 }
8416
8417 intel_crtc_load_lut(crtc);
8418}
8419
79e53945
JB
8420/* VESA 640x480x72Hz mode to set on the pipe */
8421static struct drm_display_mode load_detect_mode = {
8422 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8423 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8424};
8425
a8bb6818
DV
8426struct drm_framebuffer *
8427__intel_framebuffer_create(struct drm_device *dev,
8428 struct drm_mode_fb_cmd2 *mode_cmd,
8429 struct drm_i915_gem_object *obj)
d2dff872
CW
8430{
8431 struct intel_framebuffer *intel_fb;
8432 int ret;
8433
8434 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8435 if (!intel_fb) {
8436 drm_gem_object_unreference_unlocked(&obj->base);
8437 return ERR_PTR(-ENOMEM);
8438 }
8439
8440 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8441 if (ret)
8442 goto err;
d2dff872
CW
8443
8444 return &intel_fb->base;
dd4916c5
DV
8445err:
8446 drm_gem_object_unreference_unlocked(&obj->base);
8447 kfree(intel_fb);
8448
8449 return ERR_PTR(ret);
d2dff872
CW
8450}
8451
b5ea642a 8452static struct drm_framebuffer *
a8bb6818
DV
8453intel_framebuffer_create(struct drm_device *dev,
8454 struct drm_mode_fb_cmd2 *mode_cmd,
8455 struct drm_i915_gem_object *obj)
8456{
8457 struct drm_framebuffer *fb;
8458 int ret;
8459
8460 ret = i915_mutex_lock_interruptible(dev);
8461 if (ret)
8462 return ERR_PTR(ret);
8463 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8464 mutex_unlock(&dev->struct_mutex);
8465
8466 return fb;
8467}
8468
d2dff872
CW
8469static u32
8470intel_framebuffer_pitch_for_width(int width, int bpp)
8471{
8472 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8473 return ALIGN(pitch, 64);
8474}
8475
8476static u32
8477intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8478{
8479 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8480 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8481}
8482
8483static struct drm_framebuffer *
8484intel_framebuffer_create_for_mode(struct drm_device *dev,
8485 struct drm_display_mode *mode,
8486 int depth, int bpp)
8487{
8488 struct drm_i915_gem_object *obj;
0fed39bd 8489 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8490
8491 obj = i915_gem_alloc_object(dev,
8492 intel_framebuffer_size_for_mode(mode, bpp));
8493 if (obj == NULL)
8494 return ERR_PTR(-ENOMEM);
8495
8496 mode_cmd.width = mode->hdisplay;
8497 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8498 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8499 bpp);
5ca0c34a 8500 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8501
8502 return intel_framebuffer_create(dev, &mode_cmd, obj);
8503}
8504
8505static struct drm_framebuffer *
8506mode_fits_in_fbdev(struct drm_device *dev,
8507 struct drm_display_mode *mode)
8508{
4520f53a 8509#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8510 struct drm_i915_private *dev_priv = dev->dev_private;
8511 struct drm_i915_gem_object *obj;
8512 struct drm_framebuffer *fb;
8513
4c0e5528 8514 if (!dev_priv->fbdev)
d2dff872
CW
8515 return NULL;
8516
4c0e5528 8517 if (!dev_priv->fbdev->fb)
d2dff872
CW
8518 return NULL;
8519
4c0e5528
DV
8520 obj = dev_priv->fbdev->fb->obj;
8521 BUG_ON(!obj);
8522
8bcd4553 8523 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8524 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8525 fb->bits_per_pixel))
d2dff872
CW
8526 return NULL;
8527
01f2c773 8528 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8529 return NULL;
8530
8531 return fb;
4520f53a
DV
8532#else
8533 return NULL;
8534#endif
d2dff872
CW
8535}
8536
d2434ab7 8537bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8538 struct drm_display_mode *mode,
51fd371b
RC
8539 struct intel_load_detect_pipe *old,
8540 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8541{
8542 struct intel_crtc *intel_crtc;
d2434ab7
DV
8543 struct intel_encoder *intel_encoder =
8544 intel_attached_encoder(connector);
79e53945 8545 struct drm_crtc *possible_crtc;
4ef69c7a 8546 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8547 struct drm_crtc *crtc = NULL;
8548 struct drm_device *dev = encoder->dev;
94352cf9 8549 struct drm_framebuffer *fb;
51fd371b
RC
8550 struct drm_mode_config *config = &dev->mode_config;
8551 int ret, i = -1;
79e53945 8552
d2dff872 8553 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8554 connector->base.id, connector->name,
8e329a03 8555 encoder->base.id, encoder->name);
d2dff872 8556
51fd371b
RC
8557retry:
8558 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8559 if (ret)
8560 goto fail_unlock;
6e9f798d 8561
79e53945
JB
8562 /*
8563 * Algorithm gets a little messy:
7a5e4805 8564 *
79e53945
JB
8565 * - if the connector already has an assigned crtc, use it (but make
8566 * sure it's on first)
7a5e4805 8567 *
79e53945
JB
8568 * - try to find the first unused crtc that can drive this connector,
8569 * and use that if we find one
79e53945
JB
8570 */
8571
8572 /* See if we already have a CRTC for this connector */
8573 if (encoder->crtc) {
8574 crtc = encoder->crtc;
8261b191 8575
51fd371b
RC
8576 ret = drm_modeset_lock(&crtc->mutex, ctx);
8577 if (ret)
8578 goto fail_unlock;
7b24056b 8579
24218aac 8580 old->dpms_mode = connector->dpms;
8261b191
CW
8581 old->load_detect_temp = false;
8582
8583 /* Make sure the crtc and connector are running */
24218aac
DV
8584 if (connector->dpms != DRM_MODE_DPMS_ON)
8585 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8586
7173188d 8587 return true;
79e53945
JB
8588 }
8589
8590 /* Find an unused one (if possible) */
70e1e0ec 8591 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8592 i++;
8593 if (!(encoder->possible_crtcs & (1 << i)))
8594 continue;
a459249c
VS
8595 if (possible_crtc->enabled)
8596 continue;
8597 /* This can occur when applying the pipe A quirk on resume. */
8598 if (to_intel_crtc(possible_crtc)->new_enabled)
8599 continue;
8600
8601 crtc = possible_crtc;
8602 break;
79e53945
JB
8603 }
8604
8605 /*
8606 * If we didn't find an unused CRTC, don't use any.
8607 */
8608 if (!crtc) {
7173188d 8609 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8610 goto fail_unlock;
79e53945
JB
8611 }
8612
51fd371b
RC
8613 ret = drm_modeset_lock(&crtc->mutex, ctx);
8614 if (ret)
8615 goto fail_unlock;
fc303101
DV
8616 intel_encoder->new_crtc = to_intel_crtc(crtc);
8617 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8618
8619 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8620 intel_crtc->new_enabled = true;
8621 intel_crtc->new_config = &intel_crtc->config;
24218aac 8622 old->dpms_mode = connector->dpms;
8261b191 8623 old->load_detect_temp = true;
d2dff872 8624 old->release_fb = NULL;
79e53945 8625
6492711d
CW
8626 if (!mode)
8627 mode = &load_detect_mode;
79e53945 8628
d2dff872
CW
8629 /* We need a framebuffer large enough to accommodate all accesses
8630 * that the plane may generate whilst we perform load detection.
8631 * We can not rely on the fbcon either being present (we get called
8632 * during its initialisation to detect all boot displays, or it may
8633 * not even exist) or that it is large enough to satisfy the
8634 * requested mode.
8635 */
94352cf9
DV
8636 fb = mode_fits_in_fbdev(dev, mode);
8637 if (fb == NULL) {
d2dff872 8638 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8639 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8640 old->release_fb = fb;
d2dff872
CW
8641 } else
8642 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8643 if (IS_ERR(fb)) {
d2dff872 8644 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8645 goto fail;
79e53945 8646 }
79e53945 8647
c0c36b94 8648 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8649 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8650 if (old->release_fb)
8651 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8652 goto fail;
79e53945 8653 }
7173188d 8654
79e53945 8655 /* let the connector get through one full cycle before testing */
9d0498a2 8656 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8657 return true;
412b61d8
VS
8658
8659 fail:
8660 intel_crtc->new_enabled = crtc->enabled;
8661 if (intel_crtc->new_enabled)
8662 intel_crtc->new_config = &intel_crtc->config;
8663 else
8664 intel_crtc->new_config = NULL;
51fd371b
RC
8665fail_unlock:
8666 if (ret == -EDEADLK) {
8667 drm_modeset_backoff(ctx);
8668 goto retry;
8669 }
8670
412b61d8 8671 return false;
79e53945
JB
8672}
8673
d2434ab7 8674void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8675 struct intel_load_detect_pipe *old)
79e53945 8676{
d2434ab7
DV
8677 struct intel_encoder *intel_encoder =
8678 intel_attached_encoder(connector);
4ef69c7a 8679 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8680 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8682
d2dff872 8683 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8684 connector->base.id, connector->name,
8e329a03 8685 encoder->base.id, encoder->name);
d2dff872 8686
8261b191 8687 if (old->load_detect_temp) {
fc303101
DV
8688 to_intel_connector(connector)->new_encoder = NULL;
8689 intel_encoder->new_crtc = NULL;
412b61d8
VS
8690 intel_crtc->new_enabled = false;
8691 intel_crtc->new_config = NULL;
fc303101 8692 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8693
36206361
DV
8694 if (old->release_fb) {
8695 drm_framebuffer_unregister_private(old->release_fb);
8696 drm_framebuffer_unreference(old->release_fb);
8697 }
d2dff872 8698
0622a53c 8699 return;
79e53945
JB
8700 }
8701
c751ce4f 8702 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8703 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8704 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8705}
8706
da4a1efa
VS
8707static int i9xx_pll_refclk(struct drm_device *dev,
8708 const struct intel_crtc_config *pipe_config)
8709{
8710 struct drm_i915_private *dev_priv = dev->dev_private;
8711 u32 dpll = pipe_config->dpll_hw_state.dpll;
8712
8713 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8714 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8715 else if (HAS_PCH_SPLIT(dev))
8716 return 120000;
8717 else if (!IS_GEN2(dev))
8718 return 96000;
8719 else
8720 return 48000;
8721}
8722
79e53945 8723/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8724static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8725 struct intel_crtc_config *pipe_config)
79e53945 8726{
f1f644dc 8727 struct drm_device *dev = crtc->base.dev;
79e53945 8728 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8729 int pipe = pipe_config->cpu_transcoder;
293623f7 8730 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8731 u32 fp;
8732 intel_clock_t clock;
da4a1efa 8733 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8734
8735 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8736 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8737 else
293623f7 8738 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8739
8740 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8741 if (IS_PINEVIEW(dev)) {
8742 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8743 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8744 } else {
8745 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8746 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8747 }
8748
a6c45cf0 8749 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8750 if (IS_PINEVIEW(dev))
8751 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8752 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8753 else
8754 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8755 DPLL_FPA01_P1_POST_DIV_SHIFT);
8756
8757 switch (dpll & DPLL_MODE_MASK) {
8758 case DPLLB_MODE_DAC_SERIAL:
8759 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8760 5 : 10;
8761 break;
8762 case DPLLB_MODE_LVDS:
8763 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8764 7 : 14;
8765 break;
8766 default:
28c97730 8767 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8768 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8769 return;
79e53945
JB
8770 }
8771
ac58c3f0 8772 if (IS_PINEVIEW(dev))
da4a1efa 8773 pineview_clock(refclk, &clock);
ac58c3f0 8774 else
da4a1efa 8775 i9xx_clock(refclk, &clock);
79e53945 8776 } else {
0fb58223 8777 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8778 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8779
8780 if (is_lvds) {
8781 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8782 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8783
8784 if (lvds & LVDS_CLKB_POWER_UP)
8785 clock.p2 = 7;
8786 else
8787 clock.p2 = 14;
79e53945
JB
8788 } else {
8789 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8790 clock.p1 = 2;
8791 else {
8792 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8793 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8794 }
8795 if (dpll & PLL_P2_DIVIDE_BY_4)
8796 clock.p2 = 4;
8797 else
8798 clock.p2 = 2;
79e53945 8799 }
da4a1efa
VS
8800
8801 i9xx_clock(refclk, &clock);
79e53945
JB
8802 }
8803
18442d08
VS
8804 /*
8805 * This value includes pixel_multiplier. We will use
241bfc38 8806 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8807 * encoder's get_config() function.
8808 */
8809 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8810}
8811
6878da05
VS
8812int intel_dotclock_calculate(int link_freq,
8813 const struct intel_link_m_n *m_n)
f1f644dc 8814{
f1f644dc
JB
8815 /*
8816 * The calculation for the data clock is:
1041a02f 8817 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8818 * But we want to avoid losing precison if possible, so:
1041a02f 8819 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8820 *
8821 * and the link clock is simpler:
1041a02f 8822 * link_clock = (m * link_clock) / n
f1f644dc
JB
8823 */
8824
6878da05
VS
8825 if (!m_n->link_n)
8826 return 0;
f1f644dc 8827
6878da05
VS
8828 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8829}
f1f644dc 8830
18442d08
VS
8831static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8832 struct intel_crtc_config *pipe_config)
6878da05
VS
8833{
8834 struct drm_device *dev = crtc->base.dev;
79e53945 8835
18442d08
VS
8836 /* read out port_clock from the DPLL */
8837 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8838
f1f644dc 8839 /*
18442d08 8840 * This value does not include pixel_multiplier.
241bfc38 8841 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8842 * agree once we know their relationship in the encoder's
8843 * get_config() function.
79e53945 8844 */
241bfc38 8845 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8846 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8847 &pipe_config->fdi_m_n);
79e53945
JB
8848}
8849
8850/** Returns the currently programmed mode of the given pipe. */
8851struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8852 struct drm_crtc *crtc)
8853{
548f245b 8854 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8856 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8857 struct drm_display_mode *mode;
f1f644dc 8858 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8859 int htot = I915_READ(HTOTAL(cpu_transcoder));
8860 int hsync = I915_READ(HSYNC(cpu_transcoder));
8861 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8862 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8863 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8864
8865 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8866 if (!mode)
8867 return NULL;
8868
f1f644dc
JB
8869 /*
8870 * Construct a pipe_config sufficient for getting the clock info
8871 * back out of crtc_clock_get.
8872 *
8873 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8874 * to use a real value here instead.
8875 */
293623f7 8876 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8877 pipe_config.pixel_multiplier = 1;
293623f7
VS
8878 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8879 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8880 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8881 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8882
773ae034 8883 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8884 mode->hdisplay = (htot & 0xffff) + 1;
8885 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8886 mode->hsync_start = (hsync & 0xffff) + 1;
8887 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8888 mode->vdisplay = (vtot & 0xffff) + 1;
8889 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8890 mode->vsync_start = (vsync & 0xffff) + 1;
8891 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8892
8893 drm_mode_set_name(mode);
79e53945
JB
8894
8895 return mode;
8896}
8897
cc36513c
DV
8898static void intel_increase_pllclock(struct drm_device *dev,
8899 enum pipe pipe)
652c393a 8900{
fbee40df 8901 struct drm_i915_private *dev_priv = dev->dev_private;
dbdc6479
JB
8902 int dpll_reg = DPLL(pipe);
8903 int dpll;
652c393a 8904
baff296c 8905 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8906 return;
8907
8908 if (!dev_priv->lvds_downclock_avail)
8909 return;
8910
dbdc6479 8911 dpll = I915_READ(dpll_reg);
652c393a 8912 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8913 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8914
8ac5a6d5 8915 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8916
8917 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8918 I915_WRITE(dpll_reg, dpll);
9d0498a2 8919 intel_wait_for_vblank(dev, pipe);
dbdc6479 8920
652c393a
JB
8921 dpll = I915_READ(dpll_reg);
8922 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8923 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8924 }
652c393a
JB
8925}
8926
8927static void intel_decrease_pllclock(struct drm_crtc *crtc)
8928{
8929 struct drm_device *dev = crtc->dev;
fbee40df 8930 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8932
baff296c 8933 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8934 return;
8935
8936 if (!dev_priv->lvds_downclock_avail)
8937 return;
8938
8939 /*
8940 * Since this is called by a timer, we should never get here in
8941 * the manual case.
8942 */
8943 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8944 int pipe = intel_crtc->pipe;
8945 int dpll_reg = DPLL(pipe);
8946 int dpll;
f6e5b160 8947
44d98a61 8948 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8949
8ac5a6d5 8950 assert_panel_unlocked(dev_priv, pipe);
652c393a 8951
dc257cf1 8952 dpll = I915_READ(dpll_reg);
652c393a
JB
8953 dpll |= DISPLAY_RATE_SELECT_FPA1;
8954 I915_WRITE(dpll_reg, dpll);
9d0498a2 8955 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8956 dpll = I915_READ(dpll_reg);
8957 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8958 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8959 }
8960
8961}
8962
f047e395
CW
8963void intel_mark_busy(struct drm_device *dev)
8964{
c67a470b
PZ
8965 struct drm_i915_private *dev_priv = dev->dev_private;
8966
f62a0076
CW
8967 if (dev_priv->mm.busy)
8968 return;
8969
43694d69 8970 intel_runtime_pm_get(dev_priv);
c67a470b 8971 i915_update_gfx_val(dev_priv);
f62a0076 8972 dev_priv->mm.busy = true;
f047e395
CW
8973}
8974
8975void intel_mark_idle(struct drm_device *dev)
652c393a 8976{
c67a470b 8977 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8978 struct drm_crtc *crtc;
652c393a 8979
f62a0076
CW
8980 if (!dev_priv->mm.busy)
8981 return;
8982
8983 dev_priv->mm.busy = false;
8984
d330a953 8985 if (!i915.powersave)
bb4cdd53 8986 goto out;
652c393a 8987
70e1e0ec 8988 for_each_crtc(dev, crtc) {
f4510a27 8989 if (!crtc->primary->fb)
652c393a
JB
8990 continue;
8991
725a5b54 8992 intel_decrease_pllclock(crtc);
652c393a 8993 }
b29c19b6 8994
3d13ef2e 8995 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 8996 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
8997
8998out:
43694d69 8999 intel_runtime_pm_put(dev_priv);
652c393a
JB
9000}
9001
7c8f8a70 9002
f99d7069
DV
9003/**
9004 * intel_mark_fb_busy - mark given planes as busy
9005 * @dev: DRM device
9006 * @frontbuffer_bits: bits for the affected planes
9007 * @ring: optional ring for asynchronous commands
9008 *
9009 * This function gets called every time the screen contents change. It can be
9010 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9011 */
9012static void intel_mark_fb_busy(struct drm_device *dev,
9013 unsigned frontbuffer_bits,
9014 struct intel_engine_cs *ring)
652c393a 9015{
055e393f 9016 struct drm_i915_private *dev_priv = dev->dev_private;
cc36513c 9017 enum pipe pipe;
652c393a 9018
d330a953 9019 if (!i915.powersave)
acb87dfb
CW
9020 return;
9021
055e393f 9022 for_each_pipe(dev_priv, pipe) {
f99d7069 9023 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
c65355bb
CW
9024 continue;
9025
cc36513c 9026 intel_increase_pllclock(dev, pipe);
c65355bb
CW
9027 if (ring && intel_fbc_enabled(dev))
9028 ring->fbc_dirty = true;
652c393a
JB
9029 }
9030}
9031
f99d7069
DV
9032/**
9033 * intel_fb_obj_invalidate - invalidate frontbuffer object
9034 * @obj: GEM object to invalidate
9035 * @ring: set for asynchronous rendering
9036 *
9037 * This function gets called every time rendering on the given object starts and
9038 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9039 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9040 * until the rendering completes or a flip on this frontbuffer plane is
9041 * scheduled.
9042 */
9043void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9044 struct intel_engine_cs *ring)
9045{
9046 struct drm_device *dev = obj->base.dev;
9047 struct drm_i915_private *dev_priv = dev->dev_private;
9048
9049 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9050
9051 if (!obj->frontbuffer_bits)
9052 return;
9053
9054 if (ring) {
9055 mutex_lock(&dev_priv->fb_tracking.lock);
9056 dev_priv->fb_tracking.busy_bits
9057 |= obj->frontbuffer_bits;
9058 dev_priv->fb_tracking.flip_bits
9059 &= ~obj->frontbuffer_bits;
9060 mutex_unlock(&dev_priv->fb_tracking.lock);
9061 }
9062
9063 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9064
9ca15301 9065 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
f99d7069
DV
9066}
9067
9068/**
9069 * intel_frontbuffer_flush - flush frontbuffer
9070 * @dev: DRM device
9071 * @frontbuffer_bits: frontbuffer plane tracking bits
9072 *
9073 * This function gets called every time rendering on the given planes has
9074 * completed and frontbuffer caching can be started again. Flushes will get
9075 * delayed if they're blocked by some oustanding asynchronous rendering.
9076 *
9077 * Can be called without any locks held.
9078 */
9079void intel_frontbuffer_flush(struct drm_device *dev,
9080 unsigned frontbuffer_bits)
9081{
9082 struct drm_i915_private *dev_priv = dev->dev_private;
9083
9084 /* Delay flushing when rings are still busy.*/
9085 mutex_lock(&dev_priv->fb_tracking.lock);
9086 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9087 mutex_unlock(&dev_priv->fb_tracking.lock);
9088
9089 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9090
9ca15301 9091 intel_edp_psr_flush(dev, frontbuffer_bits);
c5ad011d
RV
9092
9093 if (IS_GEN8(dev))
9094 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
f99d7069
DV
9095}
9096
9097/**
9098 * intel_fb_obj_flush - flush frontbuffer object
9099 * @obj: GEM object to flush
9100 * @retire: set when retiring asynchronous rendering
9101 *
9102 * This function gets called every time rendering on the given object has
9103 * completed and frontbuffer caching can be started again. If @retire is true
9104 * then any delayed flushes will be unblocked.
9105 */
9106void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9107 bool retire)
9108{
9109 struct drm_device *dev = obj->base.dev;
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111 unsigned frontbuffer_bits;
9112
9113 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9114
9115 if (!obj->frontbuffer_bits)
9116 return;
9117
9118 frontbuffer_bits = obj->frontbuffer_bits;
9119
9120 if (retire) {
9121 mutex_lock(&dev_priv->fb_tracking.lock);
9122 /* Filter out new bits since rendering started. */
9123 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9124
9125 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9126 mutex_unlock(&dev_priv->fb_tracking.lock);
9127 }
9128
9129 intel_frontbuffer_flush(dev, frontbuffer_bits);
9130}
9131
9132/**
9133 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9134 * @dev: DRM device
9135 * @frontbuffer_bits: frontbuffer plane tracking bits
9136 *
9137 * This function gets called after scheduling a flip on @obj. The actual
9138 * frontbuffer flushing will be delayed until completion is signalled with
9139 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9140 * flush will be cancelled.
9141 *
9142 * Can be called without any locks held.
9143 */
9144void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9145 unsigned frontbuffer_bits)
9146{
9147 struct drm_i915_private *dev_priv = dev->dev_private;
9148
9149 mutex_lock(&dev_priv->fb_tracking.lock);
9150 dev_priv->fb_tracking.flip_bits
9151 |= frontbuffer_bits;
9152 mutex_unlock(&dev_priv->fb_tracking.lock);
9153}
9154
9155/**
9156 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9157 * @dev: DRM device
9158 * @frontbuffer_bits: frontbuffer plane tracking bits
9159 *
9160 * This function gets called after the flip has been latched and will complete
9161 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9162 *
9163 * Can be called without any locks held.
9164 */
9165void intel_frontbuffer_flip_complete(struct drm_device *dev,
9166 unsigned frontbuffer_bits)
9167{
9168 struct drm_i915_private *dev_priv = dev->dev_private;
9169
9170 mutex_lock(&dev_priv->fb_tracking.lock);
9171 /* Mask any cancelled flips. */
9172 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9173 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9174 mutex_unlock(&dev_priv->fb_tracking.lock);
9175
9176 intel_frontbuffer_flush(dev, frontbuffer_bits);
9177}
9178
79e53945
JB
9179static void intel_crtc_destroy(struct drm_crtc *crtc)
9180{
9181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9182 struct drm_device *dev = crtc->dev;
9183 struct intel_unpin_work *work;
9184 unsigned long flags;
9185
9186 spin_lock_irqsave(&dev->event_lock, flags);
9187 work = intel_crtc->unpin_work;
9188 intel_crtc->unpin_work = NULL;
9189 spin_unlock_irqrestore(&dev->event_lock, flags);
9190
9191 if (work) {
9192 cancel_work_sync(&work->work);
9193 kfree(work);
9194 }
79e53945
JB
9195
9196 drm_crtc_cleanup(crtc);
67e77c5a 9197
79e53945
JB
9198 kfree(intel_crtc);
9199}
9200
6b95a207
KH
9201static void intel_unpin_work_fn(struct work_struct *__work)
9202{
9203 struct intel_unpin_work *work =
9204 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9205 struct drm_device *dev = work->crtc->dev;
f99d7069 9206 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9207
b4a98e57 9208 mutex_lock(&dev->struct_mutex);
1690e1eb 9209 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9210 drm_gem_object_unreference(&work->pending_flip_obj->base);
9211 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9212
b4a98e57
CW
9213 intel_update_fbc(dev);
9214 mutex_unlock(&dev->struct_mutex);
9215
f99d7069
DV
9216 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9217
b4a98e57
CW
9218 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9219 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9220
6b95a207
KH
9221 kfree(work);
9222}
9223
1afe3e9d 9224static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9225 struct drm_crtc *crtc)
6b95a207 9226{
fbee40df 9227 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9229 struct intel_unpin_work *work;
6b95a207
KH
9230 unsigned long flags;
9231
9232 /* Ignore early vblank irqs */
9233 if (intel_crtc == NULL)
9234 return;
9235
9236 spin_lock_irqsave(&dev->event_lock, flags);
9237 work = intel_crtc->unpin_work;
e7d841ca
CW
9238
9239 /* Ensure we don't miss a work->pending update ... */
9240 smp_rmb();
9241
9242 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9243 spin_unlock_irqrestore(&dev->event_lock, flags);
9244 return;
9245 }
9246
e7d841ca
CW
9247 /* and that the unpin work is consistent wrt ->pending. */
9248 smp_rmb();
9249
6b95a207 9250 intel_crtc->unpin_work = NULL;
6b95a207 9251
45a066eb
RC
9252 if (work->event)
9253 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 9254
87b6b101 9255 drm_crtc_vblank_put(crtc);
0af7e4df 9256
6b95a207
KH
9257 spin_unlock_irqrestore(&dev->event_lock, flags);
9258
2c10d571 9259 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
9260
9261 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
9262
9263 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
9264}
9265
1afe3e9d
JB
9266void intel_finish_page_flip(struct drm_device *dev, int pipe)
9267{
fbee40df 9268 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9269 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9270
49b14a5c 9271 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9272}
9273
9274void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9275{
fbee40df 9276 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9277 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9278
49b14a5c 9279 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9280}
9281
75f7f3ec
VS
9282/* Is 'a' after or equal to 'b'? */
9283static bool g4x_flip_count_after_eq(u32 a, u32 b)
9284{
9285 return !((a - b) & 0x80000000);
9286}
9287
9288static bool page_flip_finished(struct intel_crtc *crtc)
9289{
9290 struct drm_device *dev = crtc->base.dev;
9291 struct drm_i915_private *dev_priv = dev->dev_private;
9292
9293 /*
9294 * The relevant registers doen't exist on pre-ctg.
9295 * As the flip done interrupt doesn't trigger for mmio
9296 * flips on gmch platforms, a flip count check isn't
9297 * really needed there. But since ctg has the registers,
9298 * include it in the check anyway.
9299 */
9300 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9301 return true;
9302
9303 /*
9304 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9305 * used the same base address. In that case the mmio flip might
9306 * have completed, but the CS hasn't even executed the flip yet.
9307 *
9308 * A flip count check isn't enough as the CS might have updated
9309 * the base address just after start of vblank, but before we
9310 * managed to process the interrupt. This means we'd complete the
9311 * CS flip too soon.
9312 *
9313 * Combining both checks should get us a good enough result. It may
9314 * still happen that the CS flip has been executed, but has not
9315 * yet actually completed. But in case the base address is the same
9316 * anyway, we don't really care.
9317 */
9318 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9319 crtc->unpin_work->gtt_offset &&
9320 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9321 crtc->unpin_work->flip_count);
9322}
9323
6b95a207
KH
9324void intel_prepare_page_flip(struct drm_device *dev, int plane)
9325{
fbee40df 9326 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9327 struct intel_crtc *intel_crtc =
9328 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9329 unsigned long flags;
9330
e7d841ca
CW
9331 /* NB: An MMIO update of the plane base pointer will also
9332 * generate a page-flip completion irq, i.e. every modeset
9333 * is also accompanied by a spurious intel_prepare_page_flip().
9334 */
6b95a207 9335 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9336 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9337 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9338 spin_unlock_irqrestore(&dev->event_lock, flags);
9339}
9340
eba905b2 9341static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9342{
9343 /* Ensure that the work item is consistent when activating it ... */
9344 smp_wmb();
9345 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9346 /* and that it is marked active as soon as the irq could fire. */
9347 smp_wmb();
9348}
9349
8c9f3aaf
JB
9350static int intel_gen2_queue_flip(struct drm_device *dev,
9351 struct drm_crtc *crtc,
9352 struct drm_framebuffer *fb,
ed8d1975 9353 struct drm_i915_gem_object *obj,
a4872ba6 9354 struct intel_engine_cs *ring,
ed8d1975 9355 uint32_t flags)
8c9f3aaf 9356{
8c9f3aaf 9357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9358 u32 flip_mask;
9359 int ret;
9360
6d90c952 9361 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9362 if (ret)
4fa62c89 9363 return ret;
8c9f3aaf
JB
9364
9365 /* Can't queue multiple flips, so wait for the previous
9366 * one to finish before executing the next.
9367 */
9368 if (intel_crtc->plane)
9369 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9370 else
9371 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9372 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9373 intel_ring_emit(ring, MI_NOOP);
9374 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9375 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9376 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9377 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9378 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9379
9380 intel_mark_page_flip_active(intel_crtc);
09246732 9381 __intel_ring_advance(ring);
83d4092b 9382 return 0;
8c9f3aaf
JB
9383}
9384
9385static int intel_gen3_queue_flip(struct drm_device *dev,
9386 struct drm_crtc *crtc,
9387 struct drm_framebuffer *fb,
ed8d1975 9388 struct drm_i915_gem_object *obj,
a4872ba6 9389 struct intel_engine_cs *ring,
ed8d1975 9390 uint32_t flags)
8c9f3aaf 9391{
8c9f3aaf 9392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9393 u32 flip_mask;
9394 int ret;
9395
6d90c952 9396 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9397 if (ret)
4fa62c89 9398 return ret;
8c9f3aaf
JB
9399
9400 if (intel_crtc->plane)
9401 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9402 else
9403 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9404 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9405 intel_ring_emit(ring, MI_NOOP);
9406 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9407 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9408 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9409 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9410 intel_ring_emit(ring, MI_NOOP);
9411
e7d841ca 9412 intel_mark_page_flip_active(intel_crtc);
09246732 9413 __intel_ring_advance(ring);
83d4092b 9414 return 0;
8c9f3aaf
JB
9415}
9416
9417static int intel_gen4_queue_flip(struct drm_device *dev,
9418 struct drm_crtc *crtc,
9419 struct drm_framebuffer *fb,
ed8d1975 9420 struct drm_i915_gem_object *obj,
a4872ba6 9421 struct intel_engine_cs *ring,
ed8d1975 9422 uint32_t flags)
8c9f3aaf
JB
9423{
9424 struct drm_i915_private *dev_priv = dev->dev_private;
9425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9426 uint32_t pf, pipesrc;
9427 int ret;
9428
6d90c952 9429 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9430 if (ret)
4fa62c89 9431 return ret;
8c9f3aaf
JB
9432
9433 /* i965+ uses the linear or tiled offsets from the
9434 * Display Registers (which do not change across a page-flip)
9435 * so we need only reprogram the base address.
9436 */
6d90c952
DV
9437 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9438 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9439 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9440 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9441 obj->tiling_mode);
8c9f3aaf
JB
9442
9443 /* XXX Enabling the panel-fitter across page-flip is so far
9444 * untested on non-native modes, so ignore it for now.
9445 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9446 */
9447 pf = 0;
9448 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9449 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9450
9451 intel_mark_page_flip_active(intel_crtc);
09246732 9452 __intel_ring_advance(ring);
83d4092b 9453 return 0;
8c9f3aaf
JB
9454}
9455
9456static int intel_gen6_queue_flip(struct drm_device *dev,
9457 struct drm_crtc *crtc,
9458 struct drm_framebuffer *fb,
ed8d1975 9459 struct drm_i915_gem_object *obj,
a4872ba6 9460 struct intel_engine_cs *ring,
ed8d1975 9461 uint32_t flags)
8c9f3aaf
JB
9462{
9463 struct drm_i915_private *dev_priv = dev->dev_private;
9464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9465 uint32_t pf, pipesrc;
9466 int ret;
9467
6d90c952 9468 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9469 if (ret)
4fa62c89 9470 return ret;
8c9f3aaf 9471
6d90c952
DV
9472 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9473 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9474 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9475 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9476
dc257cf1
DV
9477 /* Contrary to the suggestions in the documentation,
9478 * "Enable Panel Fitter" does not seem to be required when page
9479 * flipping with a non-native mode, and worse causes a normal
9480 * modeset to fail.
9481 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9482 */
9483 pf = 0;
8c9f3aaf 9484 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9485 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9486
9487 intel_mark_page_flip_active(intel_crtc);
09246732 9488 __intel_ring_advance(ring);
83d4092b 9489 return 0;
8c9f3aaf
JB
9490}
9491
7c9017e5
JB
9492static int intel_gen7_queue_flip(struct drm_device *dev,
9493 struct drm_crtc *crtc,
9494 struct drm_framebuffer *fb,
ed8d1975 9495 struct drm_i915_gem_object *obj,
a4872ba6 9496 struct intel_engine_cs *ring,
ed8d1975 9497 uint32_t flags)
7c9017e5 9498{
7c9017e5 9499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9500 uint32_t plane_bit = 0;
ffe74d75
CW
9501 int len, ret;
9502
eba905b2 9503 switch (intel_crtc->plane) {
cb05d8de
DV
9504 case PLANE_A:
9505 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9506 break;
9507 case PLANE_B:
9508 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9509 break;
9510 case PLANE_C:
9511 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9512 break;
9513 default:
9514 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9515 return -ENODEV;
cb05d8de
DV
9516 }
9517
ffe74d75 9518 len = 4;
f476828a 9519 if (ring->id == RCS) {
ffe74d75 9520 len += 6;
f476828a
DL
9521 /*
9522 * On Gen 8, SRM is now taking an extra dword to accommodate
9523 * 48bits addresses, and we need a NOOP for the batch size to
9524 * stay even.
9525 */
9526 if (IS_GEN8(dev))
9527 len += 2;
9528 }
ffe74d75 9529
f66fab8e
VS
9530 /*
9531 * BSpec MI_DISPLAY_FLIP for IVB:
9532 * "The full packet must be contained within the same cache line."
9533 *
9534 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9535 * cacheline, if we ever start emitting more commands before
9536 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9537 * then do the cacheline alignment, and finally emit the
9538 * MI_DISPLAY_FLIP.
9539 */
9540 ret = intel_ring_cacheline_align(ring);
9541 if (ret)
4fa62c89 9542 return ret;
f66fab8e 9543
ffe74d75 9544 ret = intel_ring_begin(ring, len);
7c9017e5 9545 if (ret)
4fa62c89 9546 return ret;
7c9017e5 9547
ffe74d75
CW
9548 /* Unmask the flip-done completion message. Note that the bspec says that
9549 * we should do this for both the BCS and RCS, and that we must not unmask
9550 * more than one flip event at any time (or ensure that one flip message
9551 * can be sent by waiting for flip-done prior to queueing new flips).
9552 * Experimentation says that BCS works despite DERRMR masking all
9553 * flip-done completion events and that unmasking all planes at once
9554 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9555 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9556 */
9557 if (ring->id == RCS) {
9558 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9559 intel_ring_emit(ring, DERRMR);
9560 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9561 DERRMR_PIPEB_PRI_FLIP_DONE |
9562 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9563 if (IS_GEN8(dev))
9564 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9565 MI_SRM_LRM_GLOBAL_GTT);
9566 else
9567 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9568 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9569 intel_ring_emit(ring, DERRMR);
9570 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9571 if (IS_GEN8(dev)) {
9572 intel_ring_emit(ring, 0);
9573 intel_ring_emit(ring, MI_NOOP);
9574 }
ffe74d75
CW
9575 }
9576
cb05d8de 9577 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9578 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9579 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9580 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9581
9582 intel_mark_page_flip_active(intel_crtc);
09246732 9583 __intel_ring_advance(ring);
83d4092b 9584 return 0;
7c9017e5
JB
9585}
9586
84c33a64
SG
9587static bool use_mmio_flip(struct intel_engine_cs *ring,
9588 struct drm_i915_gem_object *obj)
9589{
9590 /*
9591 * This is not being used for older platforms, because
9592 * non-availability of flip done interrupt forces us to use
9593 * CS flips. Older platforms derive flip done using some clever
9594 * tricks involving the flip_pending status bits and vblank irqs.
9595 * So using MMIO flips there would disrupt this mechanism.
9596 */
9597
8e09bf83
CW
9598 if (ring == NULL)
9599 return true;
9600
84c33a64
SG
9601 if (INTEL_INFO(ring->dev)->gen < 5)
9602 return false;
9603
9604 if (i915.use_mmio_flip < 0)
9605 return false;
9606 else if (i915.use_mmio_flip > 0)
9607 return true;
14bf993e
OM
9608 else if (i915.enable_execlists)
9609 return true;
84c33a64
SG
9610 else
9611 return ring != obj->ring;
9612}
9613
9614static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9615{
9616 struct drm_device *dev = intel_crtc->base.dev;
9617 struct drm_i915_private *dev_priv = dev->dev_private;
9618 struct intel_framebuffer *intel_fb =
9619 to_intel_framebuffer(intel_crtc->base.primary->fb);
9620 struct drm_i915_gem_object *obj = intel_fb->obj;
9621 u32 dspcntr;
9622 u32 reg;
9623
9624 intel_mark_page_flip_active(intel_crtc);
9625
9626 reg = DSPCNTR(intel_crtc->plane);
9627 dspcntr = I915_READ(reg);
9628
9629 if (INTEL_INFO(dev)->gen >= 4) {
9630 if (obj->tiling_mode != I915_TILING_NONE)
9631 dspcntr |= DISPPLANE_TILED;
9632 else
9633 dspcntr &= ~DISPPLANE_TILED;
9634 }
9635 I915_WRITE(reg, dspcntr);
9636
9637 I915_WRITE(DSPSURF(intel_crtc->plane),
9638 intel_crtc->unpin_work->gtt_offset);
9639 POSTING_READ(DSPSURF(intel_crtc->plane));
9640}
9641
9642static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9643{
9644 struct intel_engine_cs *ring;
9645 int ret;
9646
9647 lockdep_assert_held(&obj->base.dev->struct_mutex);
9648
9649 if (!obj->last_write_seqno)
9650 return 0;
9651
9652 ring = obj->ring;
9653
9654 if (i915_seqno_passed(ring->get_seqno(ring, true),
9655 obj->last_write_seqno))
9656 return 0;
9657
9658 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9659 if (ret)
9660 return ret;
9661
9662 if (WARN_ON(!ring->irq_get(ring)))
9663 return 0;
9664
9665 return 1;
9666}
9667
9668void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9669{
9670 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9671 struct intel_crtc *intel_crtc;
9672 unsigned long irq_flags;
9673 u32 seqno;
9674
9675 seqno = ring->get_seqno(ring, false);
9676
9677 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9678 for_each_intel_crtc(ring->dev, intel_crtc) {
9679 struct intel_mmio_flip *mmio_flip;
9680
9681 mmio_flip = &intel_crtc->mmio_flip;
9682 if (mmio_flip->seqno == 0)
9683 continue;
9684
9685 if (ring->id != mmio_flip->ring_id)
9686 continue;
9687
9688 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9689 intel_do_mmio_flip(intel_crtc);
9690 mmio_flip->seqno = 0;
9691 ring->irq_put(ring);
9692 }
9693 }
9694 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9695}
9696
9697static int intel_queue_mmio_flip(struct drm_device *dev,
9698 struct drm_crtc *crtc,
9699 struct drm_framebuffer *fb,
9700 struct drm_i915_gem_object *obj,
9701 struct intel_engine_cs *ring,
9702 uint32_t flags)
9703{
9704 struct drm_i915_private *dev_priv = dev->dev_private;
9705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9706 unsigned long irq_flags;
9707 int ret;
9708
9709 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9710 return -EBUSY;
9711
9712 ret = intel_postpone_flip(obj);
9713 if (ret < 0)
9714 return ret;
9715 if (ret == 0) {
9716 intel_do_mmio_flip(intel_crtc);
9717 return 0;
9718 }
9719
9720 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9721 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9722 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9723 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9724
9725 /*
9726 * Double check to catch cases where irq fired before
9727 * mmio flip data was ready
9728 */
9729 intel_notify_mmio_flip(obj->ring);
9730 return 0;
9731}
9732
8c9f3aaf
JB
9733static int intel_default_queue_flip(struct drm_device *dev,
9734 struct drm_crtc *crtc,
9735 struct drm_framebuffer *fb,
ed8d1975 9736 struct drm_i915_gem_object *obj,
a4872ba6 9737 struct intel_engine_cs *ring,
ed8d1975 9738 uint32_t flags)
8c9f3aaf
JB
9739{
9740 return -ENODEV;
9741}
9742
6b95a207
KH
9743static int intel_crtc_page_flip(struct drm_crtc *crtc,
9744 struct drm_framebuffer *fb,
ed8d1975
KP
9745 struct drm_pending_vblank_event *event,
9746 uint32_t page_flip_flags)
6b95a207
KH
9747{
9748 struct drm_device *dev = crtc->dev;
9749 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9750 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9751 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9753 enum pipe pipe = intel_crtc->pipe;
6b95a207 9754 struct intel_unpin_work *work;
a4872ba6 9755 struct intel_engine_cs *ring;
8c9f3aaf 9756 unsigned long flags;
52e68630 9757 int ret;
6b95a207 9758
c76bb61a
DS
9759 //trigger software GT busyness calculation
9760 gen8_flip_interrupt(dev);
9761
2ff8fde1
MR
9762 /*
9763 * drm_mode_page_flip_ioctl() should already catch this, but double
9764 * check to be safe. In the future we may enable pageflipping from
9765 * a disabled primary plane.
9766 */
9767 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9768 return -EBUSY;
9769
e6a595d2 9770 /* Can't change pixel format via MI display flips. */
f4510a27 9771 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9772 return -EINVAL;
9773
9774 /*
9775 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9776 * Note that pitch changes could also affect these register.
9777 */
9778 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9779 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9780 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9781 return -EINVAL;
9782
f900db47
CW
9783 if (i915_terminally_wedged(&dev_priv->gpu_error))
9784 goto out_hang;
9785
b14c5679 9786 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9787 if (work == NULL)
9788 return -ENOMEM;
9789
6b95a207 9790 work->event = event;
b4a98e57 9791 work->crtc = crtc;
2ff8fde1 9792 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
9793 INIT_WORK(&work->work, intel_unpin_work_fn);
9794
87b6b101 9795 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9796 if (ret)
9797 goto free_work;
9798
6b95a207
KH
9799 /* We borrow the event spin lock for protecting unpin_work */
9800 spin_lock_irqsave(&dev->event_lock, flags);
9801 if (intel_crtc->unpin_work) {
9802 spin_unlock_irqrestore(&dev->event_lock, flags);
9803 kfree(work);
87b6b101 9804 drm_crtc_vblank_put(crtc);
468f0b44
CW
9805
9806 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
9807 return -EBUSY;
9808 }
9809 intel_crtc->unpin_work = work;
9810 spin_unlock_irqrestore(&dev->event_lock, flags);
9811
b4a98e57
CW
9812 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9813 flush_workqueue(dev_priv->wq);
9814
79158103
CW
9815 ret = i915_mutex_lock_interruptible(dev);
9816 if (ret)
9817 goto cleanup;
6b95a207 9818
75dfca80 9819 /* Reference the objects for the scheduled work. */
05394f39
CW
9820 drm_gem_object_reference(&work->old_fb_obj->base);
9821 drm_gem_object_reference(&obj->base);
6b95a207 9822
f4510a27 9823 crtc->primary->fb = fb;
96b099fd 9824
e1f99ce6 9825 work->pending_flip_obj = obj;
e1f99ce6 9826
4e5359cd
SF
9827 work->enable_stall_check = true;
9828
b4a98e57 9829 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9830 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9831
75f7f3ec 9832 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9833 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9834
4fa62c89
VS
9835 if (IS_VALLEYVIEW(dev)) {
9836 ring = &dev_priv->ring[BCS];
8e09bf83
CW
9837 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9838 /* vlv: DISPLAY_FLIP fails to change tiling */
9839 ring = NULL;
2a92d5bc
CW
9840 } else if (IS_IVYBRIDGE(dev)) {
9841 ring = &dev_priv->ring[BCS];
4fa62c89
VS
9842 } else if (INTEL_INFO(dev)->gen >= 7) {
9843 ring = obj->ring;
9844 if (ring == NULL || ring->id != RCS)
9845 ring = &dev_priv->ring[BCS];
9846 } else {
9847 ring = &dev_priv->ring[RCS];
9848 }
9849
9850 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9851 if (ret)
9852 goto cleanup_pending;
6b95a207 9853
4fa62c89
VS
9854 work->gtt_offset =
9855 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9856
84c33a64
SG
9857 if (use_mmio_flip(ring, obj))
9858 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9859 page_flip_flags);
9860 else
9861 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
9862 page_flip_flags);
4fa62c89
VS
9863 if (ret)
9864 goto cleanup_unpin;
9865
a071fa00
DV
9866 i915_gem_track_fb(work->old_fb_obj, obj,
9867 INTEL_FRONTBUFFER_PRIMARY(pipe));
9868
7782de3b 9869 intel_disable_fbc(dev);
f99d7069 9870 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
9871 mutex_unlock(&dev->struct_mutex);
9872
e5510fac
JB
9873 trace_i915_flip_request(intel_crtc->plane, obj);
9874
6b95a207 9875 return 0;
96b099fd 9876
4fa62c89
VS
9877cleanup_unpin:
9878 intel_unpin_fb_obj(obj);
8c9f3aaf 9879cleanup_pending:
b4a98e57 9880 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9881 crtc->primary->fb = old_fb;
05394f39
CW
9882 drm_gem_object_unreference(&work->old_fb_obj->base);
9883 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9884 mutex_unlock(&dev->struct_mutex);
9885
79158103 9886cleanup:
96b099fd
CW
9887 spin_lock_irqsave(&dev->event_lock, flags);
9888 intel_crtc->unpin_work = NULL;
9889 spin_unlock_irqrestore(&dev->event_lock, flags);
9890
87b6b101 9891 drm_crtc_vblank_put(crtc);
7317c75e 9892free_work:
96b099fd
CW
9893 kfree(work);
9894
f900db47
CW
9895 if (ret == -EIO) {
9896out_hang:
9897 intel_crtc_wait_for_pending_flips(crtc);
9898 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9899 if (ret == 0 && event)
a071fa00 9900 drm_send_vblank_event(dev, pipe, event);
f900db47 9901 }
96b099fd 9902 return ret;
6b95a207
KH
9903}
9904
f6e5b160 9905static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9906 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9907 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9908};
9909
9a935856
DV
9910/**
9911 * intel_modeset_update_staged_output_state
9912 *
9913 * Updates the staged output configuration state, e.g. after we've read out the
9914 * current hw state.
9915 */
9916static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9917{
7668851f 9918 struct intel_crtc *crtc;
9a935856
DV
9919 struct intel_encoder *encoder;
9920 struct intel_connector *connector;
f6e5b160 9921
9a935856
DV
9922 list_for_each_entry(connector, &dev->mode_config.connector_list,
9923 base.head) {
9924 connector->new_encoder =
9925 to_intel_encoder(connector->base.encoder);
9926 }
f6e5b160 9927
b2784e15 9928 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9929 encoder->new_crtc =
9930 to_intel_crtc(encoder->base.crtc);
9931 }
7668851f 9932
d3fcc808 9933 for_each_intel_crtc(dev, crtc) {
7668851f 9934 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9935
9936 if (crtc->new_enabled)
9937 crtc->new_config = &crtc->config;
9938 else
9939 crtc->new_config = NULL;
7668851f 9940 }
f6e5b160
CW
9941}
9942
9a935856
DV
9943/**
9944 * intel_modeset_commit_output_state
9945 *
9946 * This function copies the stage display pipe configuration to the real one.
9947 */
9948static void intel_modeset_commit_output_state(struct drm_device *dev)
9949{
7668851f 9950 struct intel_crtc *crtc;
9a935856
DV
9951 struct intel_encoder *encoder;
9952 struct intel_connector *connector;
f6e5b160 9953
9a935856
DV
9954 list_for_each_entry(connector, &dev->mode_config.connector_list,
9955 base.head) {
9956 connector->base.encoder = &connector->new_encoder->base;
9957 }
f6e5b160 9958
b2784e15 9959 for_each_intel_encoder(dev, encoder) {
9a935856
DV
9960 encoder->base.crtc = &encoder->new_crtc->base;
9961 }
7668851f 9962
d3fcc808 9963 for_each_intel_crtc(dev, crtc) {
7668851f
VS
9964 crtc->base.enabled = crtc->new_enabled;
9965 }
9a935856
DV
9966}
9967
050f7aeb 9968static void
eba905b2 9969connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
9970 struct intel_crtc_config *pipe_config)
9971{
9972 int bpp = pipe_config->pipe_bpp;
9973
9974 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9975 connector->base.base.id,
c23cc417 9976 connector->base.name);
050f7aeb
DV
9977
9978 /* Don't use an invalid EDID bpc value */
9979 if (connector->base.display_info.bpc &&
9980 connector->base.display_info.bpc * 3 < bpp) {
9981 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9982 bpp, connector->base.display_info.bpc*3);
9983 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9984 }
9985
9986 /* Clamp bpp to 8 on screens without EDID 1.4 */
9987 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9988 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9989 bpp);
9990 pipe_config->pipe_bpp = 24;
9991 }
9992}
9993
4e53c2e0 9994static int
050f7aeb
DV
9995compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9996 struct drm_framebuffer *fb,
9997 struct intel_crtc_config *pipe_config)
4e53c2e0 9998{
050f7aeb
DV
9999 struct drm_device *dev = crtc->base.dev;
10000 struct intel_connector *connector;
4e53c2e0
DV
10001 int bpp;
10002
d42264b1
DV
10003 switch (fb->pixel_format) {
10004 case DRM_FORMAT_C8:
4e53c2e0
DV
10005 bpp = 8*3; /* since we go through a colormap */
10006 break;
d42264b1
DV
10007 case DRM_FORMAT_XRGB1555:
10008 case DRM_FORMAT_ARGB1555:
10009 /* checked in intel_framebuffer_init already */
10010 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10011 return -EINVAL;
10012 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10013 bpp = 6*3; /* min is 18bpp */
10014 break;
d42264b1
DV
10015 case DRM_FORMAT_XBGR8888:
10016 case DRM_FORMAT_ABGR8888:
10017 /* checked in intel_framebuffer_init already */
10018 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10019 return -EINVAL;
10020 case DRM_FORMAT_XRGB8888:
10021 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10022 bpp = 8*3;
10023 break;
d42264b1
DV
10024 case DRM_FORMAT_XRGB2101010:
10025 case DRM_FORMAT_ARGB2101010:
10026 case DRM_FORMAT_XBGR2101010:
10027 case DRM_FORMAT_ABGR2101010:
10028 /* checked in intel_framebuffer_init already */
10029 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10030 return -EINVAL;
4e53c2e0
DV
10031 bpp = 10*3;
10032 break;
baba133a 10033 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10034 default:
10035 DRM_DEBUG_KMS("unsupported depth\n");
10036 return -EINVAL;
10037 }
10038
4e53c2e0
DV
10039 pipe_config->pipe_bpp = bpp;
10040
10041 /* Clamp display bpp to EDID value */
10042 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10043 base.head) {
1b829e05
DV
10044 if (!connector->new_encoder ||
10045 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10046 continue;
10047
050f7aeb 10048 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10049 }
10050
10051 return bpp;
10052}
10053
644db711
DV
10054static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10055{
10056 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10057 "type: 0x%x flags: 0x%x\n",
1342830c 10058 mode->crtc_clock,
644db711
DV
10059 mode->crtc_hdisplay, mode->crtc_hsync_start,
10060 mode->crtc_hsync_end, mode->crtc_htotal,
10061 mode->crtc_vdisplay, mode->crtc_vsync_start,
10062 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10063}
10064
c0b03411
DV
10065static void intel_dump_pipe_config(struct intel_crtc *crtc,
10066 struct intel_crtc_config *pipe_config,
10067 const char *context)
10068{
10069 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10070 context, pipe_name(crtc->pipe));
10071
10072 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10073 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10074 pipe_config->pipe_bpp, pipe_config->dither);
10075 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10076 pipe_config->has_pch_encoder,
10077 pipe_config->fdi_lanes,
10078 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10079 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10080 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10081 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10082 pipe_config->has_dp_encoder,
10083 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10084 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10085 pipe_config->dp_m_n.tu);
b95af8be
VK
10086
10087 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10088 pipe_config->has_dp_encoder,
10089 pipe_config->dp_m2_n2.gmch_m,
10090 pipe_config->dp_m2_n2.gmch_n,
10091 pipe_config->dp_m2_n2.link_m,
10092 pipe_config->dp_m2_n2.link_n,
10093 pipe_config->dp_m2_n2.tu);
10094
c0b03411
DV
10095 DRM_DEBUG_KMS("requested mode:\n");
10096 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10097 DRM_DEBUG_KMS("adjusted mode:\n");
10098 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10099 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10100 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10101 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10102 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10103 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10104 pipe_config->gmch_pfit.control,
10105 pipe_config->gmch_pfit.pgm_ratios,
10106 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10107 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10108 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10109 pipe_config->pch_pfit.size,
10110 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10111 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10112 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10113}
10114
bc079e8b
VS
10115static bool encoders_cloneable(const struct intel_encoder *a,
10116 const struct intel_encoder *b)
accfc0c5 10117{
bc079e8b
VS
10118 /* masks could be asymmetric, so check both ways */
10119 return a == b || (a->cloneable & (1 << b->type) &&
10120 b->cloneable & (1 << a->type));
10121}
10122
10123static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10124 struct intel_encoder *encoder)
10125{
10126 struct drm_device *dev = crtc->base.dev;
10127 struct intel_encoder *source_encoder;
10128
b2784e15 10129 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10130 if (source_encoder->new_crtc != crtc)
10131 continue;
10132
10133 if (!encoders_cloneable(encoder, source_encoder))
10134 return false;
10135 }
10136
10137 return true;
10138}
10139
10140static bool check_encoder_cloning(struct intel_crtc *crtc)
10141{
10142 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10143 struct intel_encoder *encoder;
10144
b2784e15 10145 for_each_intel_encoder(dev, encoder) {
bc079e8b 10146 if (encoder->new_crtc != crtc)
accfc0c5
DV
10147 continue;
10148
bc079e8b
VS
10149 if (!check_single_encoder_cloning(crtc, encoder))
10150 return false;
accfc0c5
DV
10151 }
10152
bc079e8b 10153 return true;
accfc0c5
DV
10154}
10155
b8cecdf5
DV
10156static struct intel_crtc_config *
10157intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10158 struct drm_framebuffer *fb,
b8cecdf5 10159 struct drm_display_mode *mode)
ee7b9f93 10160{
7758a113 10161 struct drm_device *dev = crtc->dev;
7758a113 10162 struct intel_encoder *encoder;
b8cecdf5 10163 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10164 int plane_bpp, ret = -EINVAL;
10165 bool retry = true;
ee7b9f93 10166
bc079e8b 10167 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10168 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10169 return ERR_PTR(-EINVAL);
10170 }
10171
b8cecdf5
DV
10172 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10173 if (!pipe_config)
7758a113
DV
10174 return ERR_PTR(-ENOMEM);
10175
b8cecdf5
DV
10176 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10177 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10178
e143a21c
DV
10179 pipe_config->cpu_transcoder =
10180 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10181 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10182
2960bc9c
ID
10183 /*
10184 * Sanitize sync polarity flags based on requested ones. If neither
10185 * positive or negative polarity is requested, treat this as meaning
10186 * negative polarity.
10187 */
10188 if (!(pipe_config->adjusted_mode.flags &
10189 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10190 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10191
10192 if (!(pipe_config->adjusted_mode.flags &
10193 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10194 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10195
050f7aeb
DV
10196 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10197 * plane pixel format and any sink constraints into account. Returns the
10198 * source plane bpp so that dithering can be selected on mismatches
10199 * after encoders and crtc also have had their say. */
10200 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10201 fb, pipe_config);
4e53c2e0
DV
10202 if (plane_bpp < 0)
10203 goto fail;
10204
e41a56be
VS
10205 /*
10206 * Determine the real pipe dimensions. Note that stereo modes can
10207 * increase the actual pipe size due to the frame doubling and
10208 * insertion of additional space for blanks between the frame. This
10209 * is stored in the crtc timings. We use the requested mode to do this
10210 * computation to clearly distinguish it from the adjusted mode, which
10211 * can be changed by the connectors in the below retry loop.
10212 */
10213 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10214 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10215 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10216
e29c22c0 10217encoder_retry:
ef1b460d 10218 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10219 pipe_config->port_clock = 0;
ef1b460d 10220 pipe_config->pixel_multiplier = 1;
ff9a6750 10221
135c81b8 10222 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10223 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10224
7758a113
DV
10225 /* Pass our mode to the connectors and the CRTC to give them a chance to
10226 * adjust it according to limitations or connector properties, and also
10227 * a chance to reject the mode entirely.
47f1c6c9 10228 */
b2784e15 10229 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10230
7758a113
DV
10231 if (&encoder->new_crtc->base != crtc)
10232 continue;
7ae89233 10233
efea6e8e
DV
10234 if (!(encoder->compute_config(encoder, pipe_config))) {
10235 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10236 goto fail;
10237 }
ee7b9f93 10238 }
47f1c6c9 10239
ff9a6750
DV
10240 /* Set default port clock if not overwritten by the encoder. Needs to be
10241 * done afterwards in case the encoder adjusts the mode. */
10242 if (!pipe_config->port_clock)
241bfc38
DL
10243 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10244 * pipe_config->pixel_multiplier;
ff9a6750 10245
a43f6e0f 10246 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10247 if (ret < 0) {
7758a113
DV
10248 DRM_DEBUG_KMS("CRTC fixup failed\n");
10249 goto fail;
ee7b9f93 10250 }
e29c22c0
DV
10251
10252 if (ret == RETRY) {
10253 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10254 ret = -EINVAL;
10255 goto fail;
10256 }
10257
10258 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10259 retry = false;
10260 goto encoder_retry;
10261 }
10262
4e53c2e0
DV
10263 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10264 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10265 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10266
b8cecdf5 10267 return pipe_config;
7758a113 10268fail:
b8cecdf5 10269 kfree(pipe_config);
e29c22c0 10270 return ERR_PTR(ret);
ee7b9f93 10271}
47f1c6c9 10272
e2e1ed41
DV
10273/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10274 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10275static void
10276intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10277 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10278{
10279 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10280 struct drm_device *dev = crtc->dev;
10281 struct intel_encoder *encoder;
10282 struct intel_connector *connector;
10283 struct drm_crtc *tmp_crtc;
79e53945 10284
e2e1ed41 10285 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10286
e2e1ed41
DV
10287 /* Check which crtcs have changed outputs connected to them, these need
10288 * to be part of the prepare_pipes mask. We don't (yet) support global
10289 * modeset across multiple crtcs, so modeset_pipes will only have one
10290 * bit set at most. */
10291 list_for_each_entry(connector, &dev->mode_config.connector_list,
10292 base.head) {
10293 if (connector->base.encoder == &connector->new_encoder->base)
10294 continue;
79e53945 10295
e2e1ed41
DV
10296 if (connector->base.encoder) {
10297 tmp_crtc = connector->base.encoder->crtc;
10298
10299 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10300 }
10301
10302 if (connector->new_encoder)
10303 *prepare_pipes |=
10304 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10305 }
10306
b2784e15 10307 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10308 if (encoder->base.crtc == &encoder->new_crtc->base)
10309 continue;
10310
10311 if (encoder->base.crtc) {
10312 tmp_crtc = encoder->base.crtc;
10313
10314 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10315 }
10316
10317 if (encoder->new_crtc)
10318 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10319 }
10320
7668851f 10321 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10322 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10323 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10324 continue;
7e7d76c3 10325
7668851f 10326 if (!intel_crtc->new_enabled)
e2e1ed41 10327 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10328 else
10329 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10330 }
10331
e2e1ed41
DV
10332
10333 /* set_mode is also used to update properties on life display pipes. */
10334 intel_crtc = to_intel_crtc(crtc);
7668851f 10335 if (intel_crtc->new_enabled)
e2e1ed41
DV
10336 *prepare_pipes |= 1 << intel_crtc->pipe;
10337
b6c5164d
DV
10338 /*
10339 * For simplicity do a full modeset on any pipe where the output routing
10340 * changed. We could be more clever, but that would require us to be
10341 * more careful with calling the relevant encoder->mode_set functions.
10342 */
e2e1ed41
DV
10343 if (*prepare_pipes)
10344 *modeset_pipes = *prepare_pipes;
10345
10346 /* ... and mask these out. */
10347 *modeset_pipes &= ~(*disable_pipes);
10348 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10349
10350 /*
10351 * HACK: We don't (yet) fully support global modesets. intel_set_config
10352 * obies this rule, but the modeset restore mode of
10353 * intel_modeset_setup_hw_state does not.
10354 */
10355 *modeset_pipes &= 1 << intel_crtc->pipe;
10356 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10357
10358 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10359 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10360}
79e53945 10361
ea9d758d 10362static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10363{
ea9d758d 10364 struct drm_encoder *encoder;
f6e5b160 10365 struct drm_device *dev = crtc->dev;
f6e5b160 10366
ea9d758d
DV
10367 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10368 if (encoder->crtc == crtc)
10369 return true;
10370
10371 return false;
10372}
10373
10374static void
10375intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10376{
10377 struct intel_encoder *intel_encoder;
10378 struct intel_crtc *intel_crtc;
10379 struct drm_connector *connector;
10380
b2784e15 10381 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10382 if (!intel_encoder->base.crtc)
10383 continue;
10384
10385 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10386
10387 if (prepare_pipes & (1 << intel_crtc->pipe))
10388 intel_encoder->connectors_active = false;
10389 }
10390
10391 intel_modeset_commit_output_state(dev);
10392
7668851f 10393 /* Double check state. */
d3fcc808 10394 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10395 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10396 WARN_ON(intel_crtc->new_config &&
10397 intel_crtc->new_config != &intel_crtc->config);
10398 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10399 }
10400
10401 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10402 if (!connector->encoder || !connector->encoder->crtc)
10403 continue;
10404
10405 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10406
10407 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10408 struct drm_property *dpms_property =
10409 dev->mode_config.dpms_property;
10410
ea9d758d 10411 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10412 drm_object_property_set_value(&connector->base,
68d34720
DV
10413 dpms_property,
10414 DRM_MODE_DPMS_ON);
ea9d758d
DV
10415
10416 intel_encoder = to_intel_encoder(connector->encoder);
10417 intel_encoder->connectors_active = true;
10418 }
10419 }
10420
10421}
10422
3bd26263 10423static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10424{
3bd26263 10425 int diff;
f1f644dc
JB
10426
10427 if (clock1 == clock2)
10428 return true;
10429
10430 if (!clock1 || !clock2)
10431 return false;
10432
10433 diff = abs(clock1 - clock2);
10434
10435 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10436 return true;
10437
10438 return false;
10439}
10440
25c5b266
DV
10441#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10442 list_for_each_entry((intel_crtc), \
10443 &(dev)->mode_config.crtc_list, \
10444 base.head) \
0973f18f 10445 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10446
0e8ffe1b 10447static bool
2fa2fe9a
DV
10448intel_pipe_config_compare(struct drm_device *dev,
10449 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10450 struct intel_crtc_config *pipe_config)
10451{
66e985c0
DV
10452#define PIPE_CONF_CHECK_X(name) \
10453 if (current_config->name != pipe_config->name) { \
10454 DRM_ERROR("mismatch in " #name " " \
10455 "(expected 0x%08x, found 0x%08x)\n", \
10456 current_config->name, \
10457 pipe_config->name); \
10458 return false; \
10459 }
10460
08a24034
DV
10461#define PIPE_CONF_CHECK_I(name) \
10462 if (current_config->name != pipe_config->name) { \
10463 DRM_ERROR("mismatch in " #name " " \
10464 "(expected %i, found %i)\n", \
10465 current_config->name, \
10466 pipe_config->name); \
10467 return false; \
88adfff1
DV
10468 }
10469
b95af8be
VK
10470/* This is required for BDW+ where there is only one set of registers for
10471 * switching between high and low RR.
10472 * This macro can be used whenever a comparison has to be made between one
10473 * hw state and multiple sw state variables.
10474 */
10475#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10476 if ((current_config->name != pipe_config->name) && \
10477 (current_config->alt_name != pipe_config->name)) { \
10478 DRM_ERROR("mismatch in " #name " " \
10479 "(expected %i or %i, found %i)\n", \
10480 current_config->name, \
10481 current_config->alt_name, \
10482 pipe_config->name); \
10483 return false; \
10484 }
10485
1bd1bd80
DV
10486#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10487 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10488 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10489 "(expected %i, found %i)\n", \
10490 current_config->name & (mask), \
10491 pipe_config->name & (mask)); \
10492 return false; \
10493 }
10494
5e550656
VS
10495#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10496 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10497 DRM_ERROR("mismatch in " #name " " \
10498 "(expected %i, found %i)\n", \
10499 current_config->name, \
10500 pipe_config->name); \
10501 return false; \
10502 }
10503
bb760063
DV
10504#define PIPE_CONF_QUIRK(quirk) \
10505 ((current_config->quirks | pipe_config->quirks) & (quirk))
10506
eccb140b
DV
10507 PIPE_CONF_CHECK_I(cpu_transcoder);
10508
08a24034
DV
10509 PIPE_CONF_CHECK_I(has_pch_encoder);
10510 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10511 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10512 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10513 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10514 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10515 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10516
eb14cb74 10517 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10518
10519 if (INTEL_INFO(dev)->gen < 8) {
10520 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10521 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10522 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10523 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10524 PIPE_CONF_CHECK_I(dp_m_n.tu);
10525
10526 if (current_config->has_drrs) {
10527 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10528 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10529 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10530 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10531 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10532 }
10533 } else {
10534 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10535 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10536 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10537 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10538 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10539 }
eb14cb74 10540
1bd1bd80
DV
10541 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10542 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10543 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10544 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10545 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10546 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10547
10548 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10549 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10550 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10551 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10552 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10553 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10554
c93f54cf 10555 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10556 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10557 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10558 IS_VALLEYVIEW(dev))
10559 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10560
9ed109a7
DV
10561 PIPE_CONF_CHECK_I(has_audio);
10562
1bd1bd80
DV
10563 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10564 DRM_MODE_FLAG_INTERLACE);
10565
bb760063
DV
10566 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10567 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10568 DRM_MODE_FLAG_PHSYNC);
10569 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10570 DRM_MODE_FLAG_NHSYNC);
10571 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10572 DRM_MODE_FLAG_PVSYNC);
10573 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10574 DRM_MODE_FLAG_NVSYNC);
10575 }
045ac3b5 10576
37327abd
VS
10577 PIPE_CONF_CHECK_I(pipe_src_w);
10578 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10579
9953599b
DV
10580 /*
10581 * FIXME: BIOS likes to set up a cloned config with lvds+external
10582 * screen. Since we don't yet re-compute the pipe config when moving
10583 * just the lvds port away to another pipe the sw tracking won't match.
10584 *
10585 * Proper atomic modesets with recomputed global state will fix this.
10586 * Until then just don't check gmch state for inherited modes.
10587 */
10588 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10589 PIPE_CONF_CHECK_I(gmch_pfit.control);
10590 /* pfit ratios are autocomputed by the hw on gen4+ */
10591 if (INTEL_INFO(dev)->gen < 4)
10592 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10593 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10594 }
10595
fd4daa9c
CW
10596 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10597 if (current_config->pch_pfit.enabled) {
10598 PIPE_CONF_CHECK_I(pch_pfit.pos);
10599 PIPE_CONF_CHECK_I(pch_pfit.size);
10600 }
2fa2fe9a 10601
e59150dc
JB
10602 /* BDW+ don't expose a synchronous way to read the state */
10603 if (IS_HASWELL(dev))
10604 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10605
282740f7
VS
10606 PIPE_CONF_CHECK_I(double_wide);
10607
26804afd
DV
10608 PIPE_CONF_CHECK_X(ddi_pll_sel);
10609
c0d43d62 10610 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10611 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10612 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10613 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10614 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10615 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10616
42571aef
VS
10617 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10618 PIPE_CONF_CHECK_I(pipe_bpp);
10619
a9a7e98a
JB
10620 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10621 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10622
66e985c0 10623#undef PIPE_CONF_CHECK_X
08a24034 10624#undef PIPE_CONF_CHECK_I
b95af8be 10625#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10626#undef PIPE_CONF_CHECK_FLAGS
5e550656 10627#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10628#undef PIPE_CONF_QUIRK
88adfff1 10629
0e8ffe1b
DV
10630 return true;
10631}
10632
91d1b4bd
DV
10633static void
10634check_connector_state(struct drm_device *dev)
8af6cf88 10635{
8af6cf88
DV
10636 struct intel_connector *connector;
10637
10638 list_for_each_entry(connector, &dev->mode_config.connector_list,
10639 base.head) {
10640 /* This also checks the encoder/connector hw state with the
10641 * ->get_hw_state callbacks. */
10642 intel_connector_check_state(connector);
10643
10644 WARN(&connector->new_encoder->base != connector->base.encoder,
10645 "connector's staged encoder doesn't match current encoder\n");
10646 }
91d1b4bd
DV
10647}
10648
10649static void
10650check_encoder_state(struct drm_device *dev)
10651{
10652 struct intel_encoder *encoder;
10653 struct intel_connector *connector;
8af6cf88 10654
b2784e15 10655 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10656 bool enabled = false;
10657 bool active = false;
10658 enum pipe pipe, tracked_pipe;
10659
10660 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10661 encoder->base.base.id,
8e329a03 10662 encoder->base.name);
8af6cf88
DV
10663
10664 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10665 "encoder's stage crtc doesn't match current crtc\n");
10666 WARN(encoder->connectors_active && !encoder->base.crtc,
10667 "encoder's active_connectors set, but no crtc\n");
10668
10669 list_for_each_entry(connector, &dev->mode_config.connector_list,
10670 base.head) {
10671 if (connector->base.encoder != &encoder->base)
10672 continue;
10673 enabled = true;
10674 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10675 active = true;
10676 }
0e32b39c
DA
10677 /*
10678 * for MST connectors if we unplug the connector is gone
10679 * away but the encoder is still connected to a crtc
10680 * until a modeset happens in response to the hotplug.
10681 */
10682 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10683 continue;
10684
8af6cf88
DV
10685 WARN(!!encoder->base.crtc != enabled,
10686 "encoder's enabled state mismatch "
10687 "(expected %i, found %i)\n",
10688 !!encoder->base.crtc, enabled);
10689 WARN(active && !encoder->base.crtc,
10690 "active encoder with no crtc\n");
10691
10692 WARN(encoder->connectors_active != active,
10693 "encoder's computed active state doesn't match tracked active state "
10694 "(expected %i, found %i)\n", active, encoder->connectors_active);
10695
10696 active = encoder->get_hw_state(encoder, &pipe);
10697 WARN(active != encoder->connectors_active,
10698 "encoder's hw state doesn't match sw tracking "
10699 "(expected %i, found %i)\n",
10700 encoder->connectors_active, active);
10701
10702 if (!encoder->base.crtc)
10703 continue;
10704
10705 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10706 WARN(active && pipe != tracked_pipe,
10707 "active encoder's pipe doesn't match"
10708 "(expected %i, found %i)\n",
10709 tracked_pipe, pipe);
10710
10711 }
91d1b4bd
DV
10712}
10713
10714static void
10715check_crtc_state(struct drm_device *dev)
10716{
fbee40df 10717 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10718 struct intel_crtc *crtc;
10719 struct intel_encoder *encoder;
10720 struct intel_crtc_config pipe_config;
8af6cf88 10721
d3fcc808 10722 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10723 bool enabled = false;
10724 bool active = false;
10725
045ac3b5
JB
10726 memset(&pipe_config, 0, sizeof(pipe_config));
10727
8af6cf88
DV
10728 DRM_DEBUG_KMS("[CRTC:%d]\n",
10729 crtc->base.base.id);
10730
10731 WARN(crtc->active && !crtc->base.enabled,
10732 "active crtc, but not enabled in sw tracking\n");
10733
b2784e15 10734 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10735 if (encoder->base.crtc != &crtc->base)
10736 continue;
10737 enabled = true;
10738 if (encoder->connectors_active)
10739 active = true;
10740 }
6c49f241 10741
8af6cf88
DV
10742 WARN(active != crtc->active,
10743 "crtc's computed active state doesn't match tracked active state "
10744 "(expected %i, found %i)\n", active, crtc->active);
10745 WARN(enabled != crtc->base.enabled,
10746 "crtc's computed enabled state doesn't match tracked enabled state "
10747 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10748
0e8ffe1b
DV
10749 active = dev_priv->display.get_pipe_config(crtc,
10750 &pipe_config);
d62cf62a
DV
10751
10752 /* hw state is inconsistent with the pipe A quirk */
10753 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10754 active = crtc->active;
10755
b2784e15 10756 for_each_intel_encoder(dev, encoder) {
3eaba51c 10757 enum pipe pipe;
6c49f241
DV
10758 if (encoder->base.crtc != &crtc->base)
10759 continue;
1d37b689 10760 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10761 encoder->get_config(encoder, &pipe_config);
10762 }
10763
0e8ffe1b
DV
10764 WARN(crtc->active != active,
10765 "crtc active state doesn't match with hw state "
10766 "(expected %i, found %i)\n", crtc->active, active);
10767
c0b03411
DV
10768 if (active &&
10769 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10770 WARN(1, "pipe state doesn't match!\n");
10771 intel_dump_pipe_config(crtc, &pipe_config,
10772 "[hw state]");
10773 intel_dump_pipe_config(crtc, &crtc->config,
10774 "[sw state]");
10775 }
8af6cf88
DV
10776 }
10777}
10778
91d1b4bd
DV
10779static void
10780check_shared_dpll_state(struct drm_device *dev)
10781{
fbee40df 10782 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10783 struct intel_crtc *crtc;
10784 struct intel_dpll_hw_state dpll_hw_state;
10785 int i;
5358901f
DV
10786
10787 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10788 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10789 int enabled_crtcs = 0, active_crtcs = 0;
10790 bool active;
10791
10792 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10793
10794 DRM_DEBUG_KMS("%s\n", pll->name);
10795
10796 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10797
10798 WARN(pll->active > pll->refcount,
10799 "more active pll users than references: %i vs %i\n",
10800 pll->active, pll->refcount);
10801 WARN(pll->active && !pll->on,
10802 "pll in active use but not on in sw tracking\n");
35c95375
DV
10803 WARN(pll->on && !pll->active,
10804 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10805 WARN(pll->on != active,
10806 "pll on state mismatch (expected %i, found %i)\n",
10807 pll->on, active);
10808
d3fcc808 10809 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10810 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10811 enabled_crtcs++;
10812 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10813 active_crtcs++;
10814 }
10815 WARN(pll->active != active_crtcs,
10816 "pll active crtcs mismatch (expected %i, found %i)\n",
10817 pll->active, active_crtcs);
10818 WARN(pll->refcount != enabled_crtcs,
10819 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10820 pll->refcount, enabled_crtcs);
66e985c0
DV
10821
10822 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10823 sizeof(dpll_hw_state)),
10824 "pll hw state mismatch\n");
5358901f 10825 }
8af6cf88
DV
10826}
10827
91d1b4bd
DV
10828void
10829intel_modeset_check_state(struct drm_device *dev)
10830{
10831 check_connector_state(dev);
10832 check_encoder_state(dev);
10833 check_crtc_state(dev);
10834 check_shared_dpll_state(dev);
10835}
10836
18442d08
VS
10837void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10838 int dotclock)
10839{
10840 /*
10841 * FDI already provided one idea for the dotclock.
10842 * Yell if the encoder disagrees.
10843 */
241bfc38 10844 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10845 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10846 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10847}
10848
80715b2f
VS
10849static void update_scanline_offset(struct intel_crtc *crtc)
10850{
10851 struct drm_device *dev = crtc->base.dev;
10852
10853 /*
10854 * The scanline counter increments at the leading edge of hsync.
10855 *
10856 * On most platforms it starts counting from vtotal-1 on the
10857 * first active line. That means the scanline counter value is
10858 * always one less than what we would expect. Ie. just after
10859 * start of vblank, which also occurs at start of hsync (on the
10860 * last active line), the scanline counter will read vblank_start-1.
10861 *
10862 * On gen2 the scanline counter starts counting from 1 instead
10863 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10864 * to keep the value positive), instead of adding one.
10865 *
10866 * On HSW+ the behaviour of the scanline counter depends on the output
10867 * type. For DP ports it behaves like most other platforms, but on HDMI
10868 * there's an extra 1 line difference. So we need to add two instead of
10869 * one to the value.
10870 */
10871 if (IS_GEN2(dev)) {
10872 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10873 int vtotal;
10874
10875 vtotal = mode->crtc_vtotal;
10876 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10877 vtotal /= 2;
10878
10879 crtc->scanline_offset = vtotal - 1;
10880 } else if (HAS_DDI(dev) &&
10881 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10882 crtc->scanline_offset = 2;
10883 } else
10884 crtc->scanline_offset = 1;
10885}
10886
f30da187
DV
10887static int __intel_set_mode(struct drm_crtc *crtc,
10888 struct drm_display_mode *mode,
10889 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10890{
10891 struct drm_device *dev = crtc->dev;
fbee40df 10892 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10893 struct drm_display_mode *saved_mode;
b8cecdf5 10894 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10895 struct intel_crtc *intel_crtc;
10896 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10897 int ret = 0;
a6778b3c 10898
4b4b9238 10899 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10900 if (!saved_mode)
10901 return -ENOMEM;
a6778b3c 10902
e2e1ed41 10903 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10904 &prepare_pipes, &disable_pipes);
10905
3ac18232 10906 *saved_mode = crtc->mode;
a6778b3c 10907
25c5b266
DV
10908 /* Hack: Because we don't (yet) support global modeset on multiple
10909 * crtcs, we don't keep track of the new mode for more than one crtc.
10910 * Hence simply check whether any bit is set in modeset_pipes in all the
10911 * pieces of code that are not yet converted to deal with mutliple crtcs
10912 * changing their mode at the same time. */
25c5b266 10913 if (modeset_pipes) {
4e53c2e0 10914 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10915 if (IS_ERR(pipe_config)) {
10916 ret = PTR_ERR(pipe_config);
10917 pipe_config = NULL;
10918
3ac18232 10919 goto out;
25c5b266 10920 }
c0b03411
DV
10921 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10922 "[modeset]");
50741abc 10923 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10924 }
a6778b3c 10925
30a970c6
JB
10926 /*
10927 * See if the config requires any additional preparation, e.g.
10928 * to adjust global state with pipes off. We need to do this
10929 * here so we can get the modeset_pipe updated config for the new
10930 * mode set on this crtc. For other crtcs we need to use the
10931 * adjusted_mode bits in the crtc directly.
10932 */
c164f833 10933 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10934 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10935
c164f833
VS
10936 /* may have added more to prepare_pipes than we should */
10937 prepare_pipes &= ~disable_pipes;
10938 }
10939
460da916
DV
10940 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10941 intel_crtc_disable(&intel_crtc->base);
10942
ea9d758d
DV
10943 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10944 if (intel_crtc->base.enabled)
10945 dev_priv->display.crtc_disable(&intel_crtc->base);
10946 }
a6778b3c 10947
6c4c86f5
DV
10948 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10949 * to set it here already despite that we pass it down the callchain.
f6e5b160 10950 */
b8cecdf5 10951 if (modeset_pipes) {
25c5b266 10952 crtc->mode = *mode;
b8cecdf5
DV
10953 /* mode_set/enable/disable functions rely on a correct pipe
10954 * config. */
10955 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10956 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10957
10958 /*
10959 * Calculate and store various constants which
10960 * are later needed by vblank and swap-completion
10961 * timestamping. They are derived from true hwmode.
10962 */
10963 drm_calc_timestamping_constants(crtc,
10964 &pipe_config->adjusted_mode);
b8cecdf5 10965 }
7758a113 10966
ea9d758d
DV
10967 /* Only after disabling all output pipelines that will be changed can we
10968 * update the the output configuration. */
10969 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10970
47fab737
DV
10971 if (dev_priv->display.modeset_global_resources)
10972 dev_priv->display.modeset_global_resources(dev);
10973
a6778b3c
DV
10974 /* Set up the DPLL and any encoders state that needs to adjust or depend
10975 * on the DPLL.
f6e5b160 10976 */
25c5b266 10977 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
10978 struct drm_framebuffer *old_fb = crtc->primary->fb;
10979 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
10980 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
10981
10982 mutex_lock(&dev->struct_mutex);
10983 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 10984 obj,
4c10794f
DV
10985 NULL);
10986 if (ret != 0) {
10987 DRM_ERROR("pin & fence failed\n");
10988 mutex_unlock(&dev->struct_mutex);
10989 goto done;
10990 }
2ff8fde1 10991 if (old_fb)
a071fa00 10992 intel_unpin_fb_obj(old_obj);
a071fa00
DV
10993 i915_gem_track_fb(old_obj, obj,
10994 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
10995 mutex_unlock(&dev->struct_mutex);
10996
10997 crtc->primary->fb = fb;
10998 crtc->x = x;
10999 crtc->y = y;
11000
4271b753
DV
11001 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11002 x, y, fb);
c0c36b94
CW
11003 if (ret)
11004 goto done;
a6778b3c
DV
11005 }
11006
11007 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11008 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11009 update_scanline_offset(intel_crtc);
11010
25c5b266 11011 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11012 }
a6778b3c 11013
a6778b3c
DV
11014 /* FIXME: add subpixel order */
11015done:
4b4b9238 11016 if (ret && crtc->enabled)
3ac18232 11017 crtc->mode = *saved_mode;
a6778b3c 11018
3ac18232 11019out:
b8cecdf5 11020 kfree(pipe_config);
3ac18232 11021 kfree(saved_mode);
a6778b3c 11022 return ret;
f6e5b160
CW
11023}
11024
e7457a9a
DL
11025static int intel_set_mode(struct drm_crtc *crtc,
11026 struct drm_display_mode *mode,
11027 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11028{
11029 int ret;
11030
11031 ret = __intel_set_mode(crtc, mode, x, y, fb);
11032
11033 if (ret == 0)
11034 intel_modeset_check_state(crtc->dev);
11035
11036 return ret;
11037}
11038
c0c36b94
CW
11039void intel_crtc_restore_mode(struct drm_crtc *crtc)
11040{
f4510a27 11041 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11042}
11043
25c5b266
DV
11044#undef for_each_intel_crtc_masked
11045
d9e55608
DV
11046static void intel_set_config_free(struct intel_set_config *config)
11047{
11048 if (!config)
11049 return;
11050
1aa4b628
DV
11051 kfree(config->save_connector_encoders);
11052 kfree(config->save_encoder_crtcs);
7668851f 11053 kfree(config->save_crtc_enabled);
d9e55608
DV
11054 kfree(config);
11055}
11056
85f9eb71
DV
11057static int intel_set_config_save_state(struct drm_device *dev,
11058 struct intel_set_config *config)
11059{
7668851f 11060 struct drm_crtc *crtc;
85f9eb71
DV
11061 struct drm_encoder *encoder;
11062 struct drm_connector *connector;
11063 int count;
11064
7668851f
VS
11065 config->save_crtc_enabled =
11066 kcalloc(dev->mode_config.num_crtc,
11067 sizeof(bool), GFP_KERNEL);
11068 if (!config->save_crtc_enabled)
11069 return -ENOMEM;
11070
1aa4b628
DV
11071 config->save_encoder_crtcs =
11072 kcalloc(dev->mode_config.num_encoder,
11073 sizeof(struct drm_crtc *), GFP_KERNEL);
11074 if (!config->save_encoder_crtcs)
85f9eb71
DV
11075 return -ENOMEM;
11076
1aa4b628
DV
11077 config->save_connector_encoders =
11078 kcalloc(dev->mode_config.num_connector,
11079 sizeof(struct drm_encoder *), GFP_KERNEL);
11080 if (!config->save_connector_encoders)
85f9eb71
DV
11081 return -ENOMEM;
11082
11083 /* Copy data. Note that driver private data is not affected.
11084 * Should anything bad happen only the expected state is
11085 * restored, not the drivers personal bookkeeping.
11086 */
7668851f 11087 count = 0;
70e1e0ec 11088 for_each_crtc(dev, crtc) {
7668851f
VS
11089 config->save_crtc_enabled[count++] = crtc->enabled;
11090 }
11091
85f9eb71
DV
11092 count = 0;
11093 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11094 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11095 }
11096
11097 count = 0;
11098 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11099 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11100 }
11101
11102 return 0;
11103}
11104
11105static void intel_set_config_restore_state(struct drm_device *dev,
11106 struct intel_set_config *config)
11107{
7668851f 11108 struct intel_crtc *crtc;
9a935856
DV
11109 struct intel_encoder *encoder;
11110 struct intel_connector *connector;
85f9eb71
DV
11111 int count;
11112
7668851f 11113 count = 0;
d3fcc808 11114 for_each_intel_crtc(dev, crtc) {
7668851f 11115 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11116
11117 if (crtc->new_enabled)
11118 crtc->new_config = &crtc->config;
11119 else
11120 crtc->new_config = NULL;
7668851f
VS
11121 }
11122
85f9eb71 11123 count = 0;
b2784e15 11124 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11125 encoder->new_crtc =
11126 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11127 }
11128
11129 count = 0;
9a935856
DV
11130 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11131 connector->new_encoder =
11132 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11133 }
11134}
11135
e3de42b6 11136static bool
2e57f47d 11137is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11138{
11139 int i;
11140
2e57f47d
CW
11141 if (set->num_connectors == 0)
11142 return false;
11143
11144 if (WARN_ON(set->connectors == NULL))
11145 return false;
11146
11147 for (i = 0; i < set->num_connectors; i++)
11148 if (set->connectors[i]->encoder &&
11149 set->connectors[i]->encoder->crtc == set->crtc &&
11150 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11151 return true;
11152
11153 return false;
11154}
11155
5e2b584e
DV
11156static void
11157intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11158 struct intel_set_config *config)
11159{
11160
11161 /* We should be able to check here if the fb has the same properties
11162 * and then just flip_or_move it */
2e57f47d
CW
11163 if (is_crtc_connector_off(set)) {
11164 config->mode_changed = true;
f4510a27 11165 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11166 /*
11167 * If we have no fb, we can only flip as long as the crtc is
11168 * active, otherwise we need a full mode set. The crtc may
11169 * be active if we've only disabled the primary plane, or
11170 * in fastboot situations.
11171 */
f4510a27 11172 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11173 struct intel_crtc *intel_crtc =
11174 to_intel_crtc(set->crtc);
11175
3b150f08 11176 if (intel_crtc->active) {
319d9827
JB
11177 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11178 config->fb_changed = true;
11179 } else {
11180 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11181 config->mode_changed = true;
11182 }
5e2b584e
DV
11183 } else if (set->fb == NULL) {
11184 config->mode_changed = true;
72f4901e 11185 } else if (set->fb->pixel_format !=
f4510a27 11186 set->crtc->primary->fb->pixel_format) {
5e2b584e 11187 config->mode_changed = true;
e3de42b6 11188 } else {
5e2b584e 11189 config->fb_changed = true;
e3de42b6 11190 }
5e2b584e
DV
11191 }
11192
835c5873 11193 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11194 config->fb_changed = true;
11195
11196 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11197 DRM_DEBUG_KMS("modes are different, full mode set\n");
11198 drm_mode_debug_printmodeline(&set->crtc->mode);
11199 drm_mode_debug_printmodeline(set->mode);
11200 config->mode_changed = true;
11201 }
a1d95703
CW
11202
11203 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11204 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11205}
11206
2e431051 11207static int
9a935856
DV
11208intel_modeset_stage_output_state(struct drm_device *dev,
11209 struct drm_mode_set *set,
11210 struct intel_set_config *config)
50f56119 11211{
9a935856
DV
11212 struct intel_connector *connector;
11213 struct intel_encoder *encoder;
7668851f 11214 struct intel_crtc *crtc;
f3f08572 11215 int ro;
50f56119 11216
9abdda74 11217 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11218 * of connectors. For paranoia, double-check this. */
11219 WARN_ON(!set->fb && (set->num_connectors != 0));
11220 WARN_ON(set->fb && (set->num_connectors == 0));
11221
9a935856
DV
11222 list_for_each_entry(connector, &dev->mode_config.connector_list,
11223 base.head) {
11224 /* Otherwise traverse passed in connector list and get encoders
11225 * for them. */
50f56119 11226 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11227 if (set->connectors[ro] == &connector->base) {
0e32b39c 11228 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11229 break;
11230 }
11231 }
11232
9a935856
DV
11233 /* If we disable the crtc, disable all its connectors. Also, if
11234 * the connector is on the changing crtc but not on the new
11235 * connector list, disable it. */
11236 if ((!set->fb || ro == set->num_connectors) &&
11237 connector->base.encoder &&
11238 connector->base.encoder->crtc == set->crtc) {
11239 connector->new_encoder = NULL;
11240
11241 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11242 connector->base.base.id,
c23cc417 11243 connector->base.name);
9a935856
DV
11244 }
11245
11246
11247 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11248 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11249 config->mode_changed = true;
50f56119
DV
11250 }
11251 }
9a935856 11252 /* connector->new_encoder is now updated for all connectors. */
50f56119 11253
9a935856 11254 /* Update crtc of enabled connectors. */
9a935856
DV
11255 list_for_each_entry(connector, &dev->mode_config.connector_list,
11256 base.head) {
7668851f
VS
11257 struct drm_crtc *new_crtc;
11258
9a935856 11259 if (!connector->new_encoder)
50f56119
DV
11260 continue;
11261
9a935856 11262 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11263
11264 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11265 if (set->connectors[ro] == &connector->base)
50f56119
DV
11266 new_crtc = set->crtc;
11267 }
11268
11269 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11270 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11271 new_crtc)) {
5e2b584e 11272 return -EINVAL;
50f56119 11273 }
0e32b39c 11274 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11275
11276 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11277 connector->base.base.id,
c23cc417 11278 connector->base.name,
9a935856
DV
11279 new_crtc->base.id);
11280 }
11281
11282 /* Check for any encoders that needs to be disabled. */
b2784e15 11283 for_each_intel_encoder(dev, encoder) {
5a65f358 11284 int num_connectors = 0;
9a935856
DV
11285 list_for_each_entry(connector,
11286 &dev->mode_config.connector_list,
11287 base.head) {
11288 if (connector->new_encoder == encoder) {
11289 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11290 num_connectors++;
9a935856
DV
11291 }
11292 }
5a65f358
PZ
11293
11294 if (num_connectors == 0)
11295 encoder->new_crtc = NULL;
11296 else if (num_connectors > 1)
11297 return -EINVAL;
11298
9a935856
DV
11299 /* Only now check for crtc changes so we don't miss encoders
11300 * that will be disabled. */
11301 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11302 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11303 config->mode_changed = true;
50f56119
DV
11304 }
11305 }
9a935856 11306 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11307 list_for_each_entry(connector, &dev->mode_config.connector_list,
11308 base.head) {
11309 if (connector->new_encoder)
11310 if (connector->new_encoder != connector->encoder)
11311 connector->encoder = connector->new_encoder;
11312 }
d3fcc808 11313 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11314 crtc->new_enabled = false;
11315
b2784e15 11316 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11317 if (encoder->new_crtc == crtc) {
11318 crtc->new_enabled = true;
11319 break;
11320 }
11321 }
11322
11323 if (crtc->new_enabled != crtc->base.enabled) {
11324 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11325 crtc->new_enabled ? "en" : "dis");
11326 config->mode_changed = true;
11327 }
7bd0a8e7
VS
11328
11329 if (crtc->new_enabled)
11330 crtc->new_config = &crtc->config;
11331 else
11332 crtc->new_config = NULL;
7668851f
VS
11333 }
11334
2e431051
DV
11335 return 0;
11336}
11337
7d00a1f5
VS
11338static void disable_crtc_nofb(struct intel_crtc *crtc)
11339{
11340 struct drm_device *dev = crtc->base.dev;
11341 struct intel_encoder *encoder;
11342 struct intel_connector *connector;
11343
11344 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11345 pipe_name(crtc->pipe));
11346
11347 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11348 if (connector->new_encoder &&
11349 connector->new_encoder->new_crtc == crtc)
11350 connector->new_encoder = NULL;
11351 }
11352
b2784e15 11353 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11354 if (encoder->new_crtc == crtc)
11355 encoder->new_crtc = NULL;
11356 }
11357
11358 crtc->new_enabled = false;
7bd0a8e7 11359 crtc->new_config = NULL;
7d00a1f5
VS
11360}
11361
2e431051
DV
11362static int intel_crtc_set_config(struct drm_mode_set *set)
11363{
11364 struct drm_device *dev;
2e431051
DV
11365 struct drm_mode_set save_set;
11366 struct intel_set_config *config;
11367 int ret;
2e431051 11368
8d3e375e
DV
11369 BUG_ON(!set);
11370 BUG_ON(!set->crtc);
11371 BUG_ON(!set->crtc->helper_private);
2e431051 11372
7e53f3a4
DV
11373 /* Enforce sane interface api - has been abused by the fb helper. */
11374 BUG_ON(!set->mode && set->fb);
11375 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11376
2e431051
DV
11377 if (set->fb) {
11378 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11379 set->crtc->base.id, set->fb->base.id,
11380 (int)set->num_connectors, set->x, set->y);
11381 } else {
11382 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11383 }
11384
11385 dev = set->crtc->dev;
11386
11387 ret = -ENOMEM;
11388 config = kzalloc(sizeof(*config), GFP_KERNEL);
11389 if (!config)
11390 goto out_config;
11391
11392 ret = intel_set_config_save_state(dev, config);
11393 if (ret)
11394 goto out_config;
11395
11396 save_set.crtc = set->crtc;
11397 save_set.mode = &set->crtc->mode;
11398 save_set.x = set->crtc->x;
11399 save_set.y = set->crtc->y;
f4510a27 11400 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11401
11402 /* Compute whether we need a full modeset, only an fb base update or no
11403 * change at all. In the future we might also check whether only the
11404 * mode changed, e.g. for LVDS where we only change the panel fitter in
11405 * such cases. */
11406 intel_set_config_compute_mode_changes(set, config);
11407
9a935856 11408 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11409 if (ret)
11410 goto fail;
11411
5e2b584e 11412 if (config->mode_changed) {
c0c36b94
CW
11413 ret = intel_set_mode(set->crtc, set->mode,
11414 set->x, set->y, set->fb);
5e2b584e 11415 } else if (config->fb_changed) {
3b150f08
MR
11416 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11417
4878cae2
VS
11418 intel_crtc_wait_for_pending_flips(set->crtc);
11419
4f660f49 11420 ret = intel_pipe_set_base(set->crtc,
94352cf9 11421 set->x, set->y, set->fb);
3b150f08
MR
11422
11423 /*
11424 * We need to make sure the primary plane is re-enabled if it
11425 * has previously been turned off.
11426 */
11427 if (!intel_crtc->primary_enabled && ret == 0) {
11428 WARN_ON(!intel_crtc->active);
fdd508a6 11429 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11430 }
11431
7ca51a3a
JB
11432 /*
11433 * In the fastboot case this may be our only check of the
11434 * state after boot. It would be better to only do it on
11435 * the first update, but we don't have a nice way of doing that
11436 * (and really, set_config isn't used much for high freq page
11437 * flipping, so increasing its cost here shouldn't be a big
11438 * deal).
11439 */
d330a953 11440 if (i915.fastboot && ret == 0)
7ca51a3a 11441 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11442 }
11443
2d05eae1 11444 if (ret) {
bf67dfeb
DV
11445 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11446 set->crtc->base.id, ret);
50f56119 11447fail:
2d05eae1 11448 intel_set_config_restore_state(dev, config);
50f56119 11449
7d00a1f5
VS
11450 /*
11451 * HACK: if the pipe was on, but we didn't have a framebuffer,
11452 * force the pipe off to avoid oopsing in the modeset code
11453 * due to fb==NULL. This should only happen during boot since
11454 * we don't yet reconstruct the FB from the hardware state.
11455 */
11456 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11457 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11458
2d05eae1
CW
11459 /* Try to restore the config */
11460 if (config->mode_changed &&
11461 intel_set_mode(save_set.crtc, save_set.mode,
11462 save_set.x, save_set.y, save_set.fb))
11463 DRM_ERROR("failed to restore config after modeset failure\n");
11464 }
50f56119 11465
d9e55608
DV
11466out_config:
11467 intel_set_config_free(config);
50f56119
DV
11468 return ret;
11469}
f6e5b160
CW
11470
11471static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11472 .gamma_set = intel_crtc_gamma_set,
50f56119 11473 .set_config = intel_crtc_set_config,
f6e5b160
CW
11474 .destroy = intel_crtc_destroy,
11475 .page_flip = intel_crtc_page_flip,
11476};
11477
5358901f
DV
11478static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11479 struct intel_shared_dpll *pll,
11480 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11481{
5358901f 11482 uint32_t val;
ee7b9f93 11483
bd2bb1b9
PZ
11484 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11485 return false;
11486
5358901f 11487 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11488 hw_state->dpll = val;
11489 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11490 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11491
11492 return val & DPLL_VCO_ENABLE;
11493}
11494
15bdd4cf
DV
11495static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11496 struct intel_shared_dpll *pll)
11497{
11498 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11499 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11500}
11501
e7b903d2
DV
11502static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11503 struct intel_shared_dpll *pll)
11504{
e7b903d2 11505 /* PCH refclock must be enabled first */
89eff4be 11506 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11507
15bdd4cf
DV
11508 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11509
11510 /* Wait for the clocks to stabilize. */
11511 POSTING_READ(PCH_DPLL(pll->id));
11512 udelay(150);
11513
11514 /* The pixel multiplier can only be updated once the
11515 * DPLL is enabled and the clocks are stable.
11516 *
11517 * So write it again.
11518 */
11519 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11520 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11521 udelay(200);
11522}
11523
11524static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11525 struct intel_shared_dpll *pll)
11526{
11527 struct drm_device *dev = dev_priv->dev;
11528 struct intel_crtc *crtc;
e7b903d2
DV
11529
11530 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11531 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11532 if (intel_crtc_to_shared_dpll(crtc) == pll)
11533 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11534 }
11535
15bdd4cf
DV
11536 I915_WRITE(PCH_DPLL(pll->id), 0);
11537 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11538 udelay(200);
11539}
11540
46edb027
DV
11541static char *ibx_pch_dpll_names[] = {
11542 "PCH DPLL A",
11543 "PCH DPLL B",
11544};
11545
7c74ade1 11546static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11547{
e7b903d2 11548 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11549 int i;
11550
7c74ade1 11551 dev_priv->num_shared_dpll = 2;
ee7b9f93 11552
e72f9fbf 11553 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11554 dev_priv->shared_dplls[i].id = i;
11555 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11556 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11557 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11558 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11559 dev_priv->shared_dplls[i].get_hw_state =
11560 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11561 }
11562}
11563
7c74ade1
DV
11564static void intel_shared_dpll_init(struct drm_device *dev)
11565{
e7b903d2 11566 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11567
9cd86933
DV
11568 if (HAS_DDI(dev))
11569 intel_ddi_pll_init(dev);
11570 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11571 ibx_pch_dpll_init(dev);
11572 else
11573 dev_priv->num_shared_dpll = 0;
11574
11575 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11576}
11577
465c120c
MR
11578static int
11579intel_primary_plane_disable(struct drm_plane *plane)
11580{
11581 struct drm_device *dev = plane->dev;
465c120c
MR
11582 struct intel_crtc *intel_crtc;
11583
11584 if (!plane->fb)
11585 return 0;
11586
11587 BUG_ON(!plane->crtc);
11588
11589 intel_crtc = to_intel_crtc(plane->crtc);
11590
11591 /*
11592 * Even though we checked plane->fb above, it's still possible that
11593 * the primary plane has been implicitly disabled because the crtc
11594 * coordinates given weren't visible, or because we detected
11595 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11596 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11597 * In either case, we need to unpin the FB and let the fb pointer get
11598 * updated, but otherwise we don't need to touch the hardware.
11599 */
11600 if (!intel_crtc->primary_enabled)
11601 goto disable_unpin;
11602
11603 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11604 intel_disable_primary_hw_plane(plane, plane->crtc);
11605
465c120c 11606disable_unpin:
4c34574f 11607 mutex_lock(&dev->struct_mutex);
2ff8fde1 11608 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11609 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11610 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11611 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11612 plane->fb = NULL;
11613
11614 return 0;
11615}
11616
11617static int
11618intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11619 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11620 unsigned int crtc_w, unsigned int crtc_h,
11621 uint32_t src_x, uint32_t src_y,
11622 uint32_t src_w, uint32_t src_h)
11623{
11624 struct drm_device *dev = crtc->dev;
48404c1e 11625 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2ff8fde1
MR
11627 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11628 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
465c120c
MR
11629 struct drm_rect dest = {
11630 /* integer pixels */
11631 .x1 = crtc_x,
11632 .y1 = crtc_y,
11633 .x2 = crtc_x + crtc_w,
11634 .y2 = crtc_y + crtc_h,
11635 };
11636 struct drm_rect src = {
11637 /* 16.16 fixed point */
11638 .x1 = src_x,
11639 .y1 = src_y,
11640 .x2 = src_x + src_w,
11641 .y2 = src_y + src_h,
11642 };
11643 const struct drm_rect clip = {
11644 /* integer pixels */
11645 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11646 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11647 };
ce54d85a
SJ
11648 const struct {
11649 int crtc_x, crtc_y;
11650 unsigned int crtc_w, crtc_h;
11651 uint32_t src_x, src_y, src_w, src_h;
11652 } orig = {
11653 .crtc_x = crtc_x,
11654 .crtc_y = crtc_y,
11655 .crtc_w = crtc_w,
11656 .crtc_h = crtc_h,
11657 .src_x = src_x,
11658 .src_y = src_y,
11659 .src_w = src_w,
11660 .src_h = src_h,
11661 };
11662 struct intel_plane *intel_plane = to_intel_plane(plane);
465c120c
MR
11663 bool visible;
11664 int ret;
11665
11666 ret = drm_plane_helper_check_update(plane, crtc, fb,
11667 &src, &dest, &clip,
11668 DRM_PLANE_HELPER_NO_SCALING,
11669 DRM_PLANE_HELPER_NO_SCALING,
11670 false, true, &visible);
11671
11672 if (ret)
11673 return ret;
11674
11675 /*
11676 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11677 * updating the fb pointer, and returning without touching the
11678 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11679 * turn on the display with all planes setup as desired.
11680 */
11681 if (!crtc->enabled) {
4c34574f
MR
11682 mutex_lock(&dev->struct_mutex);
11683
465c120c
MR
11684 /*
11685 * If we already called setplane while the crtc was disabled,
11686 * we may have an fb pinned; unpin it.
11687 */
11688 if (plane->fb)
a071fa00
DV
11689 intel_unpin_fb_obj(old_obj);
11690
11691 i915_gem_track_fb(old_obj, obj,
11692 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
465c120c
MR
11693
11694 /* Pin and return without programming hardware */
4c34574f
MR
11695 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11696 mutex_unlock(&dev->struct_mutex);
11697
11698 return ret;
465c120c
MR
11699 }
11700
11701 intel_crtc_wait_for_pending_flips(crtc);
11702
11703 /*
11704 * If clipping results in a non-visible primary plane, we'll disable
11705 * the primary plane. Note that this is a bit different than what
11706 * happens if userspace explicitly disables the plane by passing fb=0
11707 * because plane->fb still gets set and pinned.
11708 */
11709 if (!visible) {
4c34574f
MR
11710 mutex_lock(&dev->struct_mutex);
11711
465c120c
MR
11712 /*
11713 * Try to pin the new fb first so that we can bail out if we
11714 * fail.
11715 */
11716 if (plane->fb != fb) {
a071fa00 11717 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
4c34574f
MR
11718 if (ret) {
11719 mutex_unlock(&dev->struct_mutex);
465c120c 11720 return ret;
4c34574f 11721 }
465c120c
MR
11722 }
11723
a071fa00
DV
11724 i915_gem_track_fb(old_obj, obj,
11725 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11726
465c120c 11727 if (intel_crtc->primary_enabled)
fdd508a6 11728 intel_disable_primary_hw_plane(plane, crtc);
465c120c
MR
11729
11730
11731 if (plane->fb != fb)
11732 if (plane->fb)
a071fa00 11733 intel_unpin_fb_obj(old_obj);
465c120c 11734
4c34574f
MR
11735 mutex_unlock(&dev->struct_mutex);
11736
ce54d85a 11737 } else {
48404c1e
SJ
11738 if (intel_crtc && intel_crtc->active &&
11739 intel_crtc->primary_enabled) {
11740 /*
11741 * FBC does not work on some platforms for rotated
11742 * planes, so disable it when rotation is not 0 and
11743 * update it when rotation is set back to 0.
11744 *
11745 * FIXME: This is redundant with the fbc update done in
11746 * the primary plane enable function except that that
11747 * one is done too late. We eventually need to unify
11748 * this.
11749 */
11750 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11751 dev_priv->fbc.plane == intel_crtc->plane &&
11752 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11753 intel_disable_fbc(dev);
11754 }
11755 }
ce54d85a
SJ
11756 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11757 if (ret)
11758 return ret;
465c120c 11759
ce54d85a
SJ
11760 if (!intel_crtc->primary_enabled)
11761 intel_enable_primary_hw_plane(plane, crtc);
11762 }
465c120c 11763
ce54d85a
SJ
11764 intel_plane->crtc_x = orig.crtc_x;
11765 intel_plane->crtc_y = orig.crtc_y;
11766 intel_plane->crtc_w = orig.crtc_w;
11767 intel_plane->crtc_h = orig.crtc_h;
11768 intel_plane->src_x = orig.src_x;
11769 intel_plane->src_y = orig.src_y;
11770 intel_plane->src_w = orig.src_w;
11771 intel_plane->src_h = orig.src_h;
11772 intel_plane->obj = obj;
465c120c
MR
11773
11774 return 0;
11775}
11776
3d7d6510
MR
11777/* Common destruction function for both primary and cursor planes */
11778static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11779{
11780 struct intel_plane *intel_plane = to_intel_plane(plane);
11781 drm_plane_cleanup(plane);
11782 kfree(intel_plane);
11783}
11784
11785static const struct drm_plane_funcs intel_primary_plane_funcs = {
11786 .update_plane = intel_primary_plane_setplane,
11787 .disable_plane = intel_primary_plane_disable,
3d7d6510 11788 .destroy = intel_plane_destroy,
48404c1e 11789 .set_property = intel_plane_set_property
465c120c
MR
11790};
11791
11792static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11793 int pipe)
11794{
11795 struct intel_plane *primary;
11796 const uint32_t *intel_primary_formats;
11797 int num_formats;
11798
11799 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11800 if (primary == NULL)
11801 return NULL;
11802
11803 primary->can_scale = false;
11804 primary->max_downscale = 1;
11805 primary->pipe = pipe;
11806 primary->plane = pipe;
48404c1e 11807 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
11808 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11809 primary->plane = !pipe;
11810
11811 if (INTEL_INFO(dev)->gen <= 3) {
11812 intel_primary_formats = intel_primary_formats_gen2;
11813 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11814 } else {
11815 intel_primary_formats = intel_primary_formats_gen4;
11816 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11817 }
11818
11819 drm_universal_plane_init(dev, &primary->base, 0,
11820 &intel_primary_plane_funcs,
11821 intel_primary_formats, num_formats,
11822 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
11823
11824 if (INTEL_INFO(dev)->gen >= 4) {
11825 if (!dev->mode_config.rotation_property)
11826 dev->mode_config.rotation_property =
11827 drm_mode_create_rotation_property(dev,
11828 BIT(DRM_ROTATE_0) |
11829 BIT(DRM_ROTATE_180));
11830 if (dev->mode_config.rotation_property)
11831 drm_object_attach_property(&primary->base.base,
11832 dev->mode_config.rotation_property,
11833 primary->rotation);
11834 }
11835
465c120c
MR
11836 return &primary->base;
11837}
11838
3d7d6510
MR
11839static int
11840intel_cursor_plane_disable(struct drm_plane *plane)
11841{
11842 if (!plane->fb)
11843 return 0;
11844
11845 BUG_ON(!plane->crtc);
11846
11847 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11848}
11849
11850static int
11851intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11852 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11853 unsigned int crtc_w, unsigned int crtc_h,
11854 uint32_t src_x, uint32_t src_y,
11855 uint32_t src_w, uint32_t src_h)
11856{
11857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11858 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11859 struct drm_i915_gem_object *obj = intel_fb->obj;
11860 struct drm_rect dest = {
11861 /* integer pixels */
11862 .x1 = crtc_x,
11863 .y1 = crtc_y,
11864 .x2 = crtc_x + crtc_w,
11865 .y2 = crtc_y + crtc_h,
11866 };
11867 struct drm_rect src = {
11868 /* 16.16 fixed point */
11869 .x1 = src_x,
11870 .y1 = src_y,
11871 .x2 = src_x + src_w,
11872 .y2 = src_y + src_h,
11873 };
11874 const struct drm_rect clip = {
11875 /* integer pixels */
1add143c
VS
11876 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11877 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
3d7d6510
MR
11878 };
11879 bool visible;
11880 int ret;
11881
11882 ret = drm_plane_helper_check_update(plane, crtc, fb,
11883 &src, &dest, &clip,
11884 DRM_PLANE_HELPER_NO_SCALING,
11885 DRM_PLANE_HELPER_NO_SCALING,
11886 true, true, &visible);
11887 if (ret)
11888 return ret;
11889
11890 crtc->cursor_x = crtc_x;
11891 crtc->cursor_y = crtc_y;
11892 if (fb != crtc->cursor->fb) {
11893 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11894 } else {
11895 intel_crtc_update_cursor(crtc, visible);
4ed91096
DV
11896
11897 intel_frontbuffer_flip(crtc->dev,
11898 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11899
3d7d6510
MR
11900 return 0;
11901 }
11902}
11903static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11904 .update_plane = intel_cursor_plane_update,
11905 .disable_plane = intel_cursor_plane_disable,
11906 .destroy = intel_plane_destroy,
11907};
11908
11909static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11910 int pipe)
11911{
11912 struct intel_plane *cursor;
11913
11914 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11915 if (cursor == NULL)
11916 return NULL;
11917
11918 cursor->can_scale = false;
11919 cursor->max_downscale = 1;
11920 cursor->pipe = pipe;
11921 cursor->plane = pipe;
11922
11923 drm_universal_plane_init(dev, &cursor->base, 0,
11924 &intel_cursor_plane_funcs,
11925 intel_cursor_formats,
11926 ARRAY_SIZE(intel_cursor_formats),
11927 DRM_PLANE_TYPE_CURSOR);
11928 return &cursor->base;
11929}
11930
b358d0a6 11931static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 11932{
fbee40df 11933 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 11934 struct intel_crtc *intel_crtc;
3d7d6510
MR
11935 struct drm_plane *primary = NULL;
11936 struct drm_plane *cursor = NULL;
465c120c 11937 int i, ret;
79e53945 11938
955382f3 11939 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
11940 if (intel_crtc == NULL)
11941 return;
11942
465c120c 11943 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
11944 if (!primary)
11945 goto fail;
11946
11947 cursor = intel_cursor_plane_create(dev, pipe);
11948 if (!cursor)
11949 goto fail;
11950
465c120c 11951 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
11952 cursor, &intel_crtc_funcs);
11953 if (ret)
11954 goto fail;
79e53945
JB
11955
11956 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
11957 for (i = 0; i < 256; i++) {
11958 intel_crtc->lut_r[i] = i;
11959 intel_crtc->lut_g[i] = i;
11960 intel_crtc->lut_b[i] = i;
11961 }
11962
1f1c2e24
VS
11963 /*
11964 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 11965 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 11966 */
80824003
JB
11967 intel_crtc->pipe = pipe;
11968 intel_crtc->plane = pipe;
3a77c4c4 11969 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 11970 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 11971 intel_crtc->plane = !pipe;
80824003
JB
11972 }
11973
4b0e333e
CW
11974 intel_crtc->cursor_base = ~0;
11975 intel_crtc->cursor_cntl = ~0;
dc41c154 11976 intel_crtc->cursor_size = ~0;
4b0e333e 11977
22fd0fab
JB
11978 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11979 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11980 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11981 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11982
79e53945 11983 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
11984
11985 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
11986 return;
11987
11988fail:
11989 if (primary)
11990 drm_plane_cleanup(primary);
11991 if (cursor)
11992 drm_plane_cleanup(cursor);
11993 kfree(intel_crtc);
79e53945
JB
11994}
11995
752aa88a
JB
11996enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11997{
11998 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 11999 struct drm_device *dev = connector->base.dev;
752aa88a 12000
51fd371b 12001 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12002
12003 if (!encoder)
12004 return INVALID_PIPE;
12005
12006 return to_intel_crtc(encoder->crtc)->pipe;
12007}
12008
08d7b3d1 12009int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12010 struct drm_file *file)
08d7b3d1 12011{
08d7b3d1 12012 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12013 struct drm_crtc *drmmode_crtc;
c05422d5 12014 struct intel_crtc *crtc;
08d7b3d1 12015
1cff8f6b
DV
12016 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12017 return -ENODEV;
08d7b3d1 12018
7707e653 12019 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12020
7707e653 12021 if (!drmmode_crtc) {
08d7b3d1 12022 DRM_ERROR("no such CRTC id\n");
3f2c2057 12023 return -ENOENT;
08d7b3d1
CW
12024 }
12025
7707e653 12026 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12027 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12028
c05422d5 12029 return 0;
08d7b3d1
CW
12030}
12031
66a9278e 12032static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12033{
66a9278e
DV
12034 struct drm_device *dev = encoder->base.dev;
12035 struct intel_encoder *source_encoder;
79e53945 12036 int index_mask = 0;
79e53945
JB
12037 int entry = 0;
12038
b2784e15 12039 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12040 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12041 index_mask |= (1 << entry);
12042
79e53945
JB
12043 entry++;
12044 }
4ef69c7a 12045
79e53945
JB
12046 return index_mask;
12047}
12048
4d302442
CW
12049static bool has_edp_a(struct drm_device *dev)
12050{
12051 struct drm_i915_private *dev_priv = dev->dev_private;
12052
12053 if (!IS_MOBILE(dev))
12054 return false;
12055
12056 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12057 return false;
12058
e3589908 12059 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12060 return false;
12061
12062 return true;
12063}
12064
ba0fbca4
DL
12065const char *intel_output_name(int output)
12066{
12067 static const char *names[] = {
12068 [INTEL_OUTPUT_UNUSED] = "Unused",
12069 [INTEL_OUTPUT_ANALOG] = "Analog",
12070 [INTEL_OUTPUT_DVO] = "DVO",
12071 [INTEL_OUTPUT_SDVO] = "SDVO",
12072 [INTEL_OUTPUT_LVDS] = "LVDS",
12073 [INTEL_OUTPUT_TVOUT] = "TV",
12074 [INTEL_OUTPUT_HDMI] = "HDMI",
12075 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12076 [INTEL_OUTPUT_EDP] = "eDP",
12077 [INTEL_OUTPUT_DSI] = "DSI",
12078 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12079 };
12080
12081 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12082 return "Invalid";
12083
12084 return names[output];
12085}
12086
84b4e042
JB
12087static bool intel_crt_present(struct drm_device *dev)
12088{
12089 struct drm_i915_private *dev_priv = dev->dev_private;
12090
12091 if (IS_ULT(dev))
12092 return false;
12093
12094 if (IS_CHERRYVIEW(dev))
12095 return false;
12096
12097 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12098 return false;
12099
12100 return true;
12101}
12102
79e53945
JB
12103static void intel_setup_outputs(struct drm_device *dev)
12104{
725e30ad 12105 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12106 struct intel_encoder *encoder;
cb0953d7 12107 bool dpd_is_edp = false;
79e53945 12108
c9093354 12109 intel_lvds_init(dev);
79e53945 12110
84b4e042 12111 if (intel_crt_present(dev))
79935fca 12112 intel_crt_init(dev);
cb0953d7 12113
affa9354 12114 if (HAS_DDI(dev)) {
0e72a5b5
ED
12115 int found;
12116
12117 /* Haswell uses DDI functions to detect digital outputs */
12118 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12119 /* DDI A only supports eDP */
12120 if (found)
12121 intel_ddi_init(dev, PORT_A);
12122
12123 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12124 * register */
12125 found = I915_READ(SFUSE_STRAP);
12126
12127 if (found & SFUSE_STRAP_DDIB_DETECTED)
12128 intel_ddi_init(dev, PORT_B);
12129 if (found & SFUSE_STRAP_DDIC_DETECTED)
12130 intel_ddi_init(dev, PORT_C);
12131 if (found & SFUSE_STRAP_DDID_DETECTED)
12132 intel_ddi_init(dev, PORT_D);
12133 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12134 int found;
5d8a7752 12135 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12136
12137 if (has_edp_a(dev))
12138 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12139
dc0fa718 12140 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12141 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12142 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12143 if (!found)
e2debe91 12144 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12145 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12146 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12147 }
12148
dc0fa718 12149 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12150 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12151
dc0fa718 12152 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12153 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12154
5eb08b69 12155 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12156 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12157
270b3042 12158 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12159 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12160 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
12161 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12162 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12163 PORT_B);
12164 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12165 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12166 }
12167
6f6005a5
JB
12168 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12169 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12170 PORT_C);
12171 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 12172 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 12173 }
19c03924 12174
9418c1f1
VS
12175 if (IS_CHERRYVIEW(dev)) {
12176 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12177 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12178 PORT_D);
12179 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12180 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12181 }
12182 }
12183
3cfca973 12184 intel_dsi_init(dev);
103a196f 12185 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12186 bool found = false;
7d57382e 12187
e2debe91 12188 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12189 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12190 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12191 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12192 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12193 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12194 }
27185ae1 12195
e7281eab 12196 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12197 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12198 }
13520b05
KH
12199
12200 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12201
e2debe91 12202 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12203 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12204 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12205 }
27185ae1 12206
e2debe91 12207 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12208
b01f2c3a
JB
12209 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12210 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12211 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12212 }
e7281eab 12213 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12214 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12215 }
27185ae1 12216
b01f2c3a 12217 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12218 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12219 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12220 } else if (IS_GEN2(dev))
79e53945
JB
12221 intel_dvo_init(dev);
12222
103a196f 12223 if (SUPPORTS_TV(dev))
79e53945
JB
12224 intel_tv_init(dev);
12225
7c8f8a70
RV
12226 intel_edp_psr_init(dev);
12227
b2784e15 12228 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12229 encoder->base.possible_crtcs = encoder->crtc_mask;
12230 encoder->base.possible_clones =
66a9278e 12231 intel_encoder_clones(encoder);
79e53945 12232 }
47356eb6 12233
dde86e2d 12234 intel_init_pch_refclk(dev);
270b3042
DV
12235
12236 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12237}
12238
12239static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12240{
60a5ca01 12241 struct drm_device *dev = fb->dev;
79e53945 12242 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12243
ef2d633e 12244 drm_framebuffer_cleanup(fb);
60a5ca01 12245 mutex_lock(&dev->struct_mutex);
ef2d633e 12246 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12247 drm_gem_object_unreference(&intel_fb->obj->base);
12248 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12249 kfree(intel_fb);
12250}
12251
12252static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12253 struct drm_file *file,
79e53945
JB
12254 unsigned int *handle)
12255{
12256 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12257 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12258
05394f39 12259 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12260}
12261
12262static const struct drm_framebuffer_funcs intel_fb_funcs = {
12263 .destroy = intel_user_framebuffer_destroy,
12264 .create_handle = intel_user_framebuffer_create_handle,
12265};
12266
b5ea642a
DV
12267static int intel_framebuffer_init(struct drm_device *dev,
12268 struct intel_framebuffer *intel_fb,
12269 struct drm_mode_fb_cmd2 *mode_cmd,
12270 struct drm_i915_gem_object *obj)
79e53945 12271{
a57ce0b2 12272 int aligned_height;
a35cdaa0 12273 int pitch_limit;
79e53945
JB
12274 int ret;
12275
dd4916c5
DV
12276 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12277
c16ed4be
CW
12278 if (obj->tiling_mode == I915_TILING_Y) {
12279 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12280 return -EINVAL;
c16ed4be 12281 }
57cd6508 12282
c16ed4be
CW
12283 if (mode_cmd->pitches[0] & 63) {
12284 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12285 mode_cmd->pitches[0]);
57cd6508 12286 return -EINVAL;
c16ed4be 12287 }
57cd6508 12288
a35cdaa0
CW
12289 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12290 pitch_limit = 32*1024;
12291 } else if (INTEL_INFO(dev)->gen >= 4) {
12292 if (obj->tiling_mode)
12293 pitch_limit = 16*1024;
12294 else
12295 pitch_limit = 32*1024;
12296 } else if (INTEL_INFO(dev)->gen >= 3) {
12297 if (obj->tiling_mode)
12298 pitch_limit = 8*1024;
12299 else
12300 pitch_limit = 16*1024;
12301 } else
12302 /* XXX DSPC is limited to 4k tiled */
12303 pitch_limit = 8*1024;
12304
12305 if (mode_cmd->pitches[0] > pitch_limit) {
12306 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12307 obj->tiling_mode ? "tiled" : "linear",
12308 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12309 return -EINVAL;
c16ed4be 12310 }
5d7bd705
VS
12311
12312 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12313 mode_cmd->pitches[0] != obj->stride) {
12314 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12315 mode_cmd->pitches[0], obj->stride);
5d7bd705 12316 return -EINVAL;
c16ed4be 12317 }
5d7bd705 12318
57779d06 12319 /* Reject formats not supported by any plane early. */
308e5bcb 12320 switch (mode_cmd->pixel_format) {
57779d06 12321 case DRM_FORMAT_C8:
04b3924d
VS
12322 case DRM_FORMAT_RGB565:
12323 case DRM_FORMAT_XRGB8888:
12324 case DRM_FORMAT_ARGB8888:
57779d06
VS
12325 break;
12326 case DRM_FORMAT_XRGB1555:
12327 case DRM_FORMAT_ARGB1555:
c16ed4be 12328 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12329 DRM_DEBUG("unsupported pixel format: %s\n",
12330 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12331 return -EINVAL;
c16ed4be 12332 }
57779d06
VS
12333 break;
12334 case DRM_FORMAT_XBGR8888:
12335 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12336 case DRM_FORMAT_XRGB2101010:
12337 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12338 case DRM_FORMAT_XBGR2101010:
12339 case DRM_FORMAT_ABGR2101010:
c16ed4be 12340 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12341 DRM_DEBUG("unsupported pixel format: %s\n",
12342 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12343 return -EINVAL;
c16ed4be 12344 }
b5626747 12345 break;
04b3924d
VS
12346 case DRM_FORMAT_YUYV:
12347 case DRM_FORMAT_UYVY:
12348 case DRM_FORMAT_YVYU:
12349 case DRM_FORMAT_VYUY:
c16ed4be 12350 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12351 DRM_DEBUG("unsupported pixel format: %s\n",
12352 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12353 return -EINVAL;
c16ed4be 12354 }
57cd6508
CW
12355 break;
12356 default:
4ee62c76
VS
12357 DRM_DEBUG("unsupported pixel format: %s\n",
12358 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12359 return -EINVAL;
12360 }
12361
90f9a336
VS
12362 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12363 if (mode_cmd->offsets[0] != 0)
12364 return -EINVAL;
12365
a57ce0b2
JB
12366 aligned_height = intel_align_height(dev, mode_cmd->height,
12367 obj->tiling_mode);
53155c0a
DV
12368 /* FIXME drm helper for size checks (especially planar formats)? */
12369 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12370 return -EINVAL;
12371
c7d73f6a
DV
12372 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12373 intel_fb->obj = obj;
80075d49 12374 intel_fb->obj->framebuffer_references++;
c7d73f6a 12375
79e53945
JB
12376 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12377 if (ret) {
12378 DRM_ERROR("framebuffer init failed %d\n", ret);
12379 return ret;
12380 }
12381
79e53945
JB
12382 return 0;
12383}
12384
79e53945
JB
12385static struct drm_framebuffer *
12386intel_user_framebuffer_create(struct drm_device *dev,
12387 struct drm_file *filp,
308e5bcb 12388 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12389{
05394f39 12390 struct drm_i915_gem_object *obj;
79e53945 12391
308e5bcb
JB
12392 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12393 mode_cmd->handles[0]));
c8725226 12394 if (&obj->base == NULL)
cce13ff7 12395 return ERR_PTR(-ENOENT);
79e53945 12396
d2dff872 12397 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12398}
12399
4520f53a 12400#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12401static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12402{
12403}
12404#endif
12405
79e53945 12406static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12407 .fb_create = intel_user_framebuffer_create,
0632fef6 12408 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12409};
12410
e70236a8
JB
12411/* Set up chip specific display functions */
12412static void intel_init_display(struct drm_device *dev)
12413{
12414 struct drm_i915_private *dev_priv = dev->dev_private;
12415
ee9300bb
DV
12416 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12417 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12418 else if (IS_CHERRYVIEW(dev))
12419 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12420 else if (IS_VALLEYVIEW(dev))
12421 dev_priv->display.find_dpll = vlv_find_best_dpll;
12422 else if (IS_PINEVIEW(dev))
12423 dev_priv->display.find_dpll = pnv_find_best_dpll;
12424 else
12425 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12426
affa9354 12427 if (HAS_DDI(dev)) {
0e8ffe1b 12428 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12429 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12430 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12431 dev_priv->display.crtc_enable = haswell_crtc_enable;
12432 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12433 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12434 dev_priv->display.update_primary_plane =
12435 ironlake_update_primary_plane;
09b4ddf9 12436 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12437 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12438 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12439 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12440 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12441 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12442 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12443 dev_priv->display.update_primary_plane =
12444 ironlake_update_primary_plane;
89b667f8
JB
12445 } else if (IS_VALLEYVIEW(dev)) {
12446 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12447 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12448 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12449 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12450 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12451 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12452 dev_priv->display.update_primary_plane =
12453 i9xx_update_primary_plane;
f564048e 12454 } else {
0e8ffe1b 12455 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12456 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12457 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12458 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12459 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12460 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12461 dev_priv->display.update_primary_plane =
12462 i9xx_update_primary_plane;
f564048e 12463 }
e70236a8 12464
e70236a8 12465 /* Returns the core display clock speed */
25eb05fc
JB
12466 if (IS_VALLEYVIEW(dev))
12467 dev_priv->display.get_display_clock_speed =
12468 valleyview_get_display_clock_speed;
12469 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12470 dev_priv->display.get_display_clock_speed =
12471 i945_get_display_clock_speed;
12472 else if (IS_I915G(dev))
12473 dev_priv->display.get_display_clock_speed =
12474 i915_get_display_clock_speed;
257a7ffc 12475 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12476 dev_priv->display.get_display_clock_speed =
12477 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12478 else if (IS_PINEVIEW(dev))
12479 dev_priv->display.get_display_clock_speed =
12480 pnv_get_display_clock_speed;
e70236a8
JB
12481 else if (IS_I915GM(dev))
12482 dev_priv->display.get_display_clock_speed =
12483 i915gm_get_display_clock_speed;
12484 else if (IS_I865G(dev))
12485 dev_priv->display.get_display_clock_speed =
12486 i865_get_display_clock_speed;
f0f8a9ce 12487 else if (IS_I85X(dev))
e70236a8
JB
12488 dev_priv->display.get_display_clock_speed =
12489 i855_get_display_clock_speed;
12490 else /* 852, 830 */
12491 dev_priv->display.get_display_clock_speed =
12492 i830_get_display_clock_speed;
12493
3bb11b53 12494 if (IS_G4X(dev)) {
e0dac65e 12495 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12496 } else if (IS_GEN5(dev)) {
12497 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12498 dev_priv->display.write_eld = ironlake_write_eld;
12499 } else if (IS_GEN6(dev)) {
12500 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12501 dev_priv->display.write_eld = ironlake_write_eld;
12502 dev_priv->display.modeset_global_resources =
12503 snb_modeset_global_resources;
12504 } else if (IS_IVYBRIDGE(dev)) {
12505 /* FIXME: detect B0+ stepping and use auto training */
12506 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12507 dev_priv->display.write_eld = ironlake_write_eld;
12508 dev_priv->display.modeset_global_resources =
12509 ivb_modeset_global_resources;
12510 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
12511 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12512 dev_priv->display.write_eld = haswell_write_eld;
12513 dev_priv->display.modeset_global_resources =
12514 haswell_modeset_global_resources;
30a970c6
JB
12515 } else if (IS_VALLEYVIEW(dev)) {
12516 dev_priv->display.modeset_global_resources =
12517 valleyview_modeset_global_resources;
9ca2fe73 12518 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 12519 }
8c9f3aaf
JB
12520
12521 /* Default just returns -ENODEV to indicate unsupported */
12522 dev_priv->display.queue_flip = intel_default_queue_flip;
12523
12524 switch (INTEL_INFO(dev)->gen) {
12525 case 2:
12526 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12527 break;
12528
12529 case 3:
12530 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12531 break;
12532
12533 case 4:
12534 case 5:
12535 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12536 break;
12537
12538 case 6:
12539 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12540 break;
7c9017e5 12541 case 7:
4e0bbc31 12542 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12543 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12544 break;
8c9f3aaf 12545 }
7bd688cd
JN
12546
12547 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
12548}
12549
b690e96c
JB
12550/*
12551 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12552 * resume, or other times. This quirk makes sure that's the case for
12553 * affected systems.
12554 */
0206e353 12555static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12556{
12557 struct drm_i915_private *dev_priv = dev->dev_private;
12558
12559 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12560 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12561}
12562
435793df
KP
12563/*
12564 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12565 */
12566static void quirk_ssc_force_disable(struct drm_device *dev)
12567{
12568 struct drm_i915_private *dev_priv = dev->dev_private;
12569 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12570 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12571}
12572
4dca20ef 12573/*
5a15ab5b
CE
12574 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12575 * brightness value
4dca20ef
CE
12576 */
12577static void quirk_invert_brightness(struct drm_device *dev)
12578{
12579 struct drm_i915_private *dev_priv = dev->dev_private;
12580 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12581 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12582}
12583
9c72cc6f
SD
12584/* Some VBT's incorrectly indicate no backlight is present */
12585static void quirk_backlight_present(struct drm_device *dev)
12586{
12587 struct drm_i915_private *dev_priv = dev->dev_private;
12588 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12589 DRM_INFO("applying backlight present quirk\n");
12590}
12591
b690e96c
JB
12592struct intel_quirk {
12593 int device;
12594 int subsystem_vendor;
12595 int subsystem_device;
12596 void (*hook)(struct drm_device *dev);
12597};
12598
5f85f176
EE
12599/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12600struct intel_dmi_quirk {
12601 void (*hook)(struct drm_device *dev);
12602 const struct dmi_system_id (*dmi_id_list)[];
12603};
12604
12605static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12606{
12607 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12608 return 1;
12609}
12610
12611static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12612 {
12613 .dmi_id_list = &(const struct dmi_system_id[]) {
12614 {
12615 .callback = intel_dmi_reverse_brightness,
12616 .ident = "NCR Corporation",
12617 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12618 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12619 },
12620 },
12621 { } /* terminating entry */
12622 },
12623 .hook = quirk_invert_brightness,
12624 },
12625};
12626
c43b5634 12627static struct intel_quirk intel_quirks[] = {
b690e96c 12628 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12629 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12630
b690e96c
JB
12631 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12632 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12633
b690e96c
JB
12634 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12635 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12636
435793df
KP
12637 /* Lenovo U160 cannot use SSC on LVDS */
12638 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12639
12640 /* Sony Vaio Y cannot use SSC on LVDS */
12641 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12642
be505f64
AH
12643 /* Acer Aspire 5734Z must invert backlight brightness */
12644 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12645
12646 /* Acer/eMachines G725 */
12647 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12648
12649 /* Acer/eMachines e725 */
12650 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12651
12652 /* Acer/Packard Bell NCL20 */
12653 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12654
12655 /* Acer Aspire 4736Z */
12656 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12657
12658 /* Acer Aspire 5336 */
12659 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12660
12661 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12662 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c
SD
12663
12664 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12665 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12666
12667 /* HP Chromebook 14 (Celeron 2955U) */
12668 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12669};
12670
12671static void intel_init_quirks(struct drm_device *dev)
12672{
12673 struct pci_dev *d = dev->pdev;
12674 int i;
12675
12676 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12677 struct intel_quirk *q = &intel_quirks[i];
12678
12679 if (d->device == q->device &&
12680 (d->subsystem_vendor == q->subsystem_vendor ||
12681 q->subsystem_vendor == PCI_ANY_ID) &&
12682 (d->subsystem_device == q->subsystem_device ||
12683 q->subsystem_device == PCI_ANY_ID))
12684 q->hook(dev);
12685 }
5f85f176
EE
12686 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12687 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12688 intel_dmi_quirks[i].hook(dev);
12689 }
b690e96c
JB
12690}
12691
9cce37f4
JB
12692/* Disable the VGA plane that we never use */
12693static void i915_disable_vga(struct drm_device *dev)
12694{
12695 struct drm_i915_private *dev_priv = dev->dev_private;
12696 u8 sr1;
766aa1c4 12697 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12698
2b37c616 12699 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12700 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12701 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12702 sr1 = inb(VGA_SR_DATA);
12703 outb(sr1 | 1<<5, VGA_SR_DATA);
12704 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12705 udelay(300);
12706
12707 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
12708 POSTING_READ(vga_reg);
12709}
12710
f817586c
DV
12711void intel_modeset_init_hw(struct drm_device *dev)
12712{
a8f78b58
ED
12713 intel_prepare_ddi(dev);
12714
f8bf63fd
VS
12715 if (IS_VALLEYVIEW(dev))
12716 vlv_update_cdclk(dev);
12717
f817586c
DV
12718 intel_init_clock_gating(dev);
12719
8090c6b9 12720 intel_enable_gt_powersave(dev);
f817586c
DV
12721}
12722
7d708ee4
ID
12723void intel_modeset_suspend_hw(struct drm_device *dev)
12724{
12725 intel_suspend_hw(dev);
12726}
12727
79e53945
JB
12728void intel_modeset_init(struct drm_device *dev)
12729{
652c393a 12730 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12731 int sprite, ret;
8cc87b75 12732 enum pipe pipe;
46f297fb 12733 struct intel_crtc *crtc;
79e53945
JB
12734
12735 drm_mode_config_init(dev);
12736
12737 dev->mode_config.min_width = 0;
12738 dev->mode_config.min_height = 0;
12739
019d96cb
DA
12740 dev->mode_config.preferred_depth = 24;
12741 dev->mode_config.prefer_shadow = 1;
12742
e6ecefaa 12743 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12744
b690e96c
JB
12745 intel_init_quirks(dev);
12746
1fa61106
ED
12747 intel_init_pm(dev);
12748
e3c74757
BW
12749 if (INTEL_INFO(dev)->num_pipes == 0)
12750 return;
12751
e70236a8
JB
12752 intel_init_display(dev);
12753
a6c45cf0
CW
12754 if (IS_GEN2(dev)) {
12755 dev->mode_config.max_width = 2048;
12756 dev->mode_config.max_height = 2048;
12757 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12758 dev->mode_config.max_width = 4096;
12759 dev->mode_config.max_height = 4096;
79e53945 12760 } else {
a6c45cf0
CW
12761 dev->mode_config.max_width = 8192;
12762 dev->mode_config.max_height = 8192;
79e53945 12763 }
068be561 12764
dc41c154
VS
12765 if (IS_845G(dev) || IS_I865G(dev)) {
12766 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12767 dev->mode_config.cursor_height = 1023;
12768 } else if (IS_GEN2(dev)) {
068be561
DL
12769 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12770 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12771 } else {
12772 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12773 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12774 }
12775
5d4545ae 12776 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12777
28c97730 12778 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12779 INTEL_INFO(dev)->num_pipes,
12780 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12781
055e393f 12782 for_each_pipe(dev_priv, pipe) {
8cc87b75 12783 intel_crtc_init(dev, pipe);
1fe47785
DL
12784 for_each_sprite(pipe, sprite) {
12785 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12786 if (ret)
06da8da2 12787 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12788 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12789 }
79e53945
JB
12790 }
12791
f42bb70d
JB
12792 intel_init_dpio(dev);
12793
e72f9fbf 12794 intel_shared_dpll_init(dev);
ee7b9f93 12795
9cce37f4
JB
12796 /* Just disable it once at startup */
12797 i915_disable_vga(dev);
79e53945 12798 intel_setup_outputs(dev);
11be49eb
CW
12799
12800 /* Just in case the BIOS is doing something questionable. */
12801 intel_disable_fbc(dev);
fa9fa083 12802
6e9f798d 12803 drm_modeset_lock_all(dev);
fa9fa083 12804 intel_modeset_setup_hw_state(dev, false);
6e9f798d 12805 drm_modeset_unlock_all(dev);
46f297fb 12806
d3fcc808 12807 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
12808 if (!crtc->active)
12809 continue;
12810
46f297fb 12811 /*
46f297fb
JB
12812 * Note that reserving the BIOS fb up front prevents us
12813 * from stuffing other stolen allocations like the ring
12814 * on top. This prevents some ugliness at boot time, and
12815 * can even allow for smooth boot transitions if the BIOS
12816 * fb is large enough for the active pipe configuration.
12817 */
12818 if (dev_priv->display.get_plane_config) {
12819 dev_priv->display.get_plane_config(crtc,
12820 &crtc->plane_config);
12821 /*
12822 * If the fb is shared between multiple heads, we'll
12823 * just get the first one.
12824 */
484b41dd 12825 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 12826 }
46f297fb 12827 }
2c7111db
CW
12828}
12829
7fad798e
DV
12830static void intel_enable_pipe_a(struct drm_device *dev)
12831{
12832 struct intel_connector *connector;
12833 struct drm_connector *crt = NULL;
12834 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 12835 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
12836
12837 /* We can't just switch on the pipe A, we need to set things up with a
12838 * proper mode and output configuration. As a gross hack, enable pipe A
12839 * by enabling the load detect pipe once. */
12840 list_for_each_entry(connector,
12841 &dev->mode_config.connector_list,
12842 base.head) {
12843 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12844 crt = &connector->base;
12845 break;
12846 }
12847 }
12848
12849 if (!crt)
12850 return;
12851
208bf9fd
VS
12852 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12853 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
12854}
12855
fa555837
DV
12856static bool
12857intel_check_plane_mapping(struct intel_crtc *crtc)
12858{
7eb552ae
BW
12859 struct drm_device *dev = crtc->base.dev;
12860 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
12861 u32 reg, val;
12862
7eb552ae 12863 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
12864 return true;
12865
12866 reg = DSPCNTR(!crtc->plane);
12867 val = I915_READ(reg);
12868
12869 if ((val & DISPLAY_PLANE_ENABLE) &&
12870 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12871 return false;
12872
12873 return true;
12874}
12875
24929352
DV
12876static void intel_sanitize_crtc(struct intel_crtc *crtc)
12877{
12878 struct drm_device *dev = crtc->base.dev;
12879 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 12880 u32 reg;
24929352 12881
24929352 12882 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 12883 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
12884 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12885
d3eaf884
VS
12886 /* restore vblank interrupts to correct state */
12887 if (crtc->active)
12888 drm_vblank_on(dev, crtc->pipe);
12889 else
12890 drm_vblank_off(dev, crtc->pipe);
12891
24929352 12892 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
12893 * disable the crtc (and hence change the state) if it is wrong. Note
12894 * that gen4+ has a fixed plane -> pipe mapping. */
12895 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
12896 struct intel_connector *connector;
12897 bool plane;
12898
24929352
DV
12899 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12900 crtc->base.base.id);
12901
12902 /* Pipe has the wrong plane attached and the plane is active.
12903 * Temporarily change the plane mapping and disable everything
12904 * ... */
12905 plane = crtc->plane;
12906 crtc->plane = !plane;
9c8958bc 12907 crtc->primary_enabled = true;
24929352
DV
12908 dev_priv->display.crtc_disable(&crtc->base);
12909 crtc->plane = plane;
12910
12911 /* ... and break all links. */
12912 list_for_each_entry(connector, &dev->mode_config.connector_list,
12913 base.head) {
12914 if (connector->encoder->base.crtc != &crtc->base)
12915 continue;
12916
7f1950fb
EE
12917 connector->base.dpms = DRM_MODE_DPMS_OFF;
12918 connector->base.encoder = NULL;
24929352 12919 }
7f1950fb
EE
12920 /* multiple connectors may have the same encoder:
12921 * handle them and break crtc link separately */
12922 list_for_each_entry(connector, &dev->mode_config.connector_list,
12923 base.head)
12924 if (connector->encoder->base.crtc == &crtc->base) {
12925 connector->encoder->base.crtc = NULL;
12926 connector->encoder->connectors_active = false;
12927 }
24929352
DV
12928
12929 WARN_ON(crtc->active);
12930 crtc->base.enabled = false;
12931 }
24929352 12932
7fad798e
DV
12933 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12934 crtc->pipe == PIPE_A && !crtc->active) {
12935 /* BIOS forgot to enable pipe A, this mostly happens after
12936 * resume. Force-enable the pipe to fix this, the update_dpms
12937 * call below we restore the pipe to the right state, but leave
12938 * the required bits on. */
12939 intel_enable_pipe_a(dev);
12940 }
12941
24929352
DV
12942 /* Adjust the state of the output pipe according to whether we
12943 * have active connectors/encoders. */
12944 intel_crtc_update_dpms(&crtc->base);
12945
12946 if (crtc->active != crtc->base.enabled) {
12947 struct intel_encoder *encoder;
12948
12949 /* This can happen either due to bugs in the get_hw_state
12950 * functions or because the pipe is force-enabled due to the
12951 * pipe A quirk. */
12952 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12953 crtc->base.base.id,
12954 crtc->base.enabled ? "enabled" : "disabled",
12955 crtc->active ? "enabled" : "disabled");
12956
12957 crtc->base.enabled = crtc->active;
12958
12959 /* Because we only establish the connector -> encoder ->
12960 * crtc links if something is active, this means the
12961 * crtc is now deactivated. Break the links. connector
12962 * -> encoder links are only establish when things are
12963 * actually up, hence no need to break them. */
12964 WARN_ON(crtc->active);
12965
12966 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12967 WARN_ON(encoder->connectors_active);
12968 encoder->base.crtc = NULL;
12969 }
12970 }
c5ab3bc0
DV
12971
12972 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
4cc31489
DV
12973 /*
12974 * We start out with underrun reporting disabled to avoid races.
12975 * For correct bookkeeping mark this on active crtcs.
12976 *
c5ab3bc0
DV
12977 * Also on gmch platforms we dont have any hardware bits to
12978 * disable the underrun reporting. Which means we need to start
12979 * out with underrun reporting disabled also on inactive pipes,
12980 * since otherwise we'll complain about the garbage we read when
12981 * e.g. coming up after runtime pm.
12982 *
4cc31489
DV
12983 * No protection against concurrent access is required - at
12984 * worst a fifo underrun happens which also sets this to false.
12985 */
12986 crtc->cpu_fifo_underrun_disabled = true;
12987 crtc->pch_fifo_underrun_disabled = true;
80715b2f
VS
12988
12989 update_scanline_offset(crtc);
4cc31489 12990 }
24929352
DV
12991}
12992
12993static void intel_sanitize_encoder(struct intel_encoder *encoder)
12994{
12995 struct intel_connector *connector;
12996 struct drm_device *dev = encoder->base.dev;
12997
12998 /* We need to check both for a crtc link (meaning that the
12999 * encoder is active and trying to read from a pipe) and the
13000 * pipe itself being active. */
13001 bool has_active_crtc = encoder->base.crtc &&
13002 to_intel_crtc(encoder->base.crtc)->active;
13003
13004 if (encoder->connectors_active && !has_active_crtc) {
13005 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13006 encoder->base.base.id,
8e329a03 13007 encoder->base.name);
24929352
DV
13008
13009 /* Connector is active, but has no active pipe. This is
13010 * fallout from our resume register restoring. Disable
13011 * the encoder manually again. */
13012 if (encoder->base.crtc) {
13013 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13014 encoder->base.base.id,
8e329a03 13015 encoder->base.name);
24929352 13016 encoder->disable(encoder);
a62d1497
VS
13017 if (encoder->post_disable)
13018 encoder->post_disable(encoder);
24929352 13019 }
7f1950fb
EE
13020 encoder->base.crtc = NULL;
13021 encoder->connectors_active = false;
24929352
DV
13022
13023 /* Inconsistent output/port/pipe state happens presumably due to
13024 * a bug in one of the get_hw_state functions. Or someplace else
13025 * in our code, like the register restore mess on resume. Clamp
13026 * things to off as a safer default. */
13027 list_for_each_entry(connector,
13028 &dev->mode_config.connector_list,
13029 base.head) {
13030 if (connector->encoder != encoder)
13031 continue;
7f1950fb
EE
13032 connector->base.dpms = DRM_MODE_DPMS_OFF;
13033 connector->base.encoder = NULL;
24929352
DV
13034 }
13035 }
13036 /* Enabled encoders without active connectors will be fixed in
13037 * the crtc fixup. */
13038}
13039
04098753 13040void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13041{
13042 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13043 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13044
04098753
ID
13045 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13046 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13047 i915_disable_vga(dev);
13048 }
13049}
13050
13051void i915_redisable_vga(struct drm_device *dev)
13052{
13053 struct drm_i915_private *dev_priv = dev->dev_private;
13054
8dc8a27c
PZ
13055 /* This function can be called both from intel_modeset_setup_hw_state or
13056 * at a very early point in our resume sequence, where the power well
13057 * structures are not yet restored. Since this function is at a very
13058 * paranoid "someone might have enabled VGA while we were not looking"
13059 * level, just check if the power well is enabled instead of trying to
13060 * follow the "don't touch the power well if we don't need it" policy
13061 * the rest of the driver uses. */
04098753 13062 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13063 return;
13064
04098753 13065 i915_redisable_vga_power_on(dev);
0fde901f
KM
13066}
13067
98ec7739
VS
13068static bool primary_get_hw_state(struct intel_crtc *crtc)
13069{
13070 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13071
13072 if (!crtc->active)
13073 return false;
13074
13075 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13076}
13077
30e984df 13078static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13079{
13080 struct drm_i915_private *dev_priv = dev->dev_private;
13081 enum pipe pipe;
24929352
DV
13082 struct intel_crtc *crtc;
13083 struct intel_encoder *encoder;
13084 struct intel_connector *connector;
5358901f 13085 int i;
24929352 13086
d3fcc808 13087 for_each_intel_crtc(dev, crtc) {
88adfff1 13088 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13089
9953599b
DV
13090 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13091
0e8ffe1b
DV
13092 crtc->active = dev_priv->display.get_pipe_config(crtc,
13093 &crtc->config);
24929352
DV
13094
13095 crtc->base.enabled = crtc->active;
98ec7739 13096 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13097
13098 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13099 crtc->base.base.id,
13100 crtc->active ? "enabled" : "disabled");
13101 }
13102
5358901f
DV
13103 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13104 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13105
13106 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13107 pll->active = 0;
d3fcc808 13108 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13109 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13110 pll->active++;
13111 }
13112 pll->refcount = pll->active;
13113
35c95375
DV
13114 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13115 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13116
13117 if (pll->refcount)
13118 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13119 }
13120
b2784e15 13121 for_each_intel_encoder(dev, encoder) {
24929352
DV
13122 pipe = 0;
13123
13124 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13125 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13126 encoder->base.crtc = &crtc->base;
1d37b689 13127 encoder->get_config(encoder, &crtc->config);
24929352
DV
13128 } else {
13129 encoder->base.crtc = NULL;
13130 }
13131
13132 encoder->connectors_active = false;
6f2bcceb 13133 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13134 encoder->base.base.id,
8e329a03 13135 encoder->base.name,
24929352 13136 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13137 pipe_name(pipe));
24929352
DV
13138 }
13139
13140 list_for_each_entry(connector, &dev->mode_config.connector_list,
13141 base.head) {
13142 if (connector->get_hw_state(connector)) {
13143 connector->base.dpms = DRM_MODE_DPMS_ON;
13144 connector->encoder->connectors_active = true;
13145 connector->base.encoder = &connector->encoder->base;
13146 } else {
13147 connector->base.dpms = DRM_MODE_DPMS_OFF;
13148 connector->base.encoder = NULL;
13149 }
13150 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13151 connector->base.base.id,
c23cc417 13152 connector->base.name,
24929352
DV
13153 connector->base.encoder ? "enabled" : "disabled");
13154 }
30e984df
DV
13155}
13156
13157/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13158 * and i915 state tracking structures. */
13159void intel_modeset_setup_hw_state(struct drm_device *dev,
13160 bool force_restore)
13161{
13162 struct drm_i915_private *dev_priv = dev->dev_private;
13163 enum pipe pipe;
30e984df
DV
13164 struct intel_crtc *crtc;
13165 struct intel_encoder *encoder;
35c95375 13166 int i;
30e984df
DV
13167
13168 intel_modeset_readout_hw_state(dev);
24929352 13169
babea61d
JB
13170 /*
13171 * Now that we have the config, copy it to each CRTC struct
13172 * Note that this could go away if we move to using crtc_config
13173 * checking everywhere.
13174 */
d3fcc808 13175 for_each_intel_crtc(dev, crtc) {
d330a953 13176 if (crtc->active && i915.fastboot) {
f6a83288 13177 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13178 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13179 crtc->base.base.id);
13180 drm_mode_debug_printmodeline(&crtc->base.mode);
13181 }
13182 }
13183
24929352 13184 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13185 for_each_intel_encoder(dev, encoder) {
24929352
DV
13186 intel_sanitize_encoder(encoder);
13187 }
13188
055e393f 13189 for_each_pipe(dev_priv, pipe) {
24929352
DV
13190 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13191 intel_sanitize_crtc(crtc);
c0b03411 13192 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13193 }
9a935856 13194
35c95375
DV
13195 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13196 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13197
13198 if (!pll->on || pll->active)
13199 continue;
13200
13201 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13202
13203 pll->disable(dev_priv, pll);
13204 pll->on = false;
13205 }
13206
96f90c54 13207 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13208 ilk_wm_get_hw_state(dev);
13209
45e2b5f6 13210 if (force_restore) {
7d0bc1ea
VS
13211 i915_redisable_vga(dev);
13212
f30da187
DV
13213 /*
13214 * We need to use raw interfaces for restoring state to avoid
13215 * checking (bogus) intermediate states.
13216 */
055e393f 13217 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13218 struct drm_crtc *crtc =
13219 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13220
13221 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13222 crtc->primary->fb);
45e2b5f6
DV
13223 }
13224 } else {
13225 intel_modeset_update_staged_output_state(dev);
13226 }
8af6cf88
DV
13227
13228 intel_modeset_check_state(dev);
2c7111db
CW
13229}
13230
13231void intel_modeset_gem_init(struct drm_device *dev)
13232{
484b41dd 13233 struct drm_crtc *c;
2ff8fde1 13234 struct drm_i915_gem_object *obj;
484b41dd 13235
ae48434c
ID
13236 mutex_lock(&dev->struct_mutex);
13237 intel_init_gt_powersave(dev);
13238 mutex_unlock(&dev->struct_mutex);
13239
1833b134 13240 intel_modeset_init_hw(dev);
02e792fb
DV
13241
13242 intel_setup_overlay(dev);
484b41dd
JB
13243
13244 /*
13245 * Make sure any fbs we allocated at startup are properly
13246 * pinned & fenced. When we do the allocation it's too early
13247 * for this.
13248 */
13249 mutex_lock(&dev->struct_mutex);
70e1e0ec 13250 for_each_crtc(dev, c) {
2ff8fde1
MR
13251 obj = intel_fb_obj(c->primary->fb);
13252 if (obj == NULL)
484b41dd
JB
13253 continue;
13254
2ff8fde1 13255 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13256 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13257 to_intel_crtc(c)->pipe);
66e514c1
DA
13258 drm_framebuffer_unreference(c->primary->fb);
13259 c->primary->fb = NULL;
484b41dd
JB
13260 }
13261 }
13262 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13263}
13264
4932e2c3
ID
13265void intel_connector_unregister(struct intel_connector *intel_connector)
13266{
13267 struct drm_connector *connector = &intel_connector->base;
13268
13269 intel_panel_destroy_backlight(connector);
34ea3d38 13270 drm_connector_unregister(connector);
4932e2c3
ID
13271}
13272
79e53945
JB
13273void intel_modeset_cleanup(struct drm_device *dev)
13274{
652c393a 13275 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13276 struct drm_connector *connector;
652c393a 13277
fd0c0642
DV
13278 /*
13279 * Interrupts and polling as the first thing to avoid creating havoc.
13280 * Too much stuff here (turning of rps, connectors, ...) would
13281 * experience fancy races otherwise.
13282 */
13283 drm_irq_uninstall(dev);
1d0d343a 13284 intel_hpd_cancel_work(dev_priv);
eb21b92b
JB
13285 dev_priv->pm._irqs_disabled = true;
13286
fd0c0642
DV
13287 /*
13288 * Due to the hpd irq storm handling the hotplug work can re-arm the
13289 * poll handlers. Hence disable polling after hpd handling is shut down.
13290 */
f87ea761 13291 drm_kms_helper_poll_fini(dev);
fd0c0642 13292
652c393a
JB
13293 mutex_lock(&dev->struct_mutex);
13294
723bfd70
JB
13295 intel_unregister_dsm_handler();
13296
973d04f9 13297 intel_disable_fbc(dev);
e70236a8 13298
8090c6b9 13299 intel_disable_gt_powersave(dev);
0cdab21f 13300
930ebb46
DV
13301 ironlake_teardown_rc6(dev);
13302
69341a5e
KH
13303 mutex_unlock(&dev->struct_mutex);
13304
1630fe75
CW
13305 /* flush any delayed tasks or pending work */
13306 flush_scheduled_work();
13307
db31af1d
JN
13308 /* destroy the backlight and sysfs files before encoders/connectors */
13309 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13310 struct intel_connector *intel_connector;
13311
13312 intel_connector = to_intel_connector(connector);
13313 intel_connector->unregister(intel_connector);
db31af1d 13314 }
d9255d57 13315
79e53945 13316 drm_mode_config_cleanup(dev);
4d7bb011
DV
13317
13318 intel_cleanup_overlay(dev);
ae48434c
ID
13319
13320 mutex_lock(&dev->struct_mutex);
13321 intel_cleanup_gt_powersave(dev);
13322 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13323}
13324
f1c79df3
ZW
13325/*
13326 * Return which encoder is currently attached for connector.
13327 */
df0e9248 13328struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13329{
df0e9248
CW
13330 return &intel_attached_encoder(connector)->base;
13331}
f1c79df3 13332
df0e9248
CW
13333void intel_connector_attach_encoder(struct intel_connector *connector,
13334 struct intel_encoder *encoder)
13335{
13336 connector->encoder = encoder;
13337 drm_mode_connector_attach_encoder(&connector->base,
13338 &encoder->base);
79e53945 13339}
28d52043
DA
13340
13341/*
13342 * set vga decode state - true == enable VGA decode
13343 */
13344int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13345{
13346 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13347 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13348 u16 gmch_ctrl;
13349
75fa041d
CW
13350 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13351 DRM_ERROR("failed to read control word\n");
13352 return -EIO;
13353 }
13354
c0cc8a55
CW
13355 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13356 return 0;
13357
28d52043
DA
13358 if (state)
13359 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13360 else
13361 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13362
13363 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13364 DRM_ERROR("failed to write control word\n");
13365 return -EIO;
13366 }
13367
28d52043
DA
13368 return 0;
13369}
c4a1d9e4 13370
c4a1d9e4 13371struct intel_display_error_state {
ff57f1b0
PZ
13372
13373 u32 power_well_driver;
13374
63b66e5b
CW
13375 int num_transcoders;
13376
c4a1d9e4
CW
13377 struct intel_cursor_error_state {
13378 u32 control;
13379 u32 position;
13380 u32 base;
13381 u32 size;
52331309 13382 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13383
13384 struct intel_pipe_error_state {
ddf9c536 13385 bool power_domain_on;
c4a1d9e4 13386 u32 source;
f301b1e1 13387 u32 stat;
52331309 13388 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13389
13390 struct intel_plane_error_state {
13391 u32 control;
13392 u32 stride;
13393 u32 size;
13394 u32 pos;
13395 u32 addr;
13396 u32 surface;
13397 u32 tile_offset;
52331309 13398 } plane[I915_MAX_PIPES];
63b66e5b
CW
13399
13400 struct intel_transcoder_error_state {
ddf9c536 13401 bool power_domain_on;
63b66e5b
CW
13402 enum transcoder cpu_transcoder;
13403
13404 u32 conf;
13405
13406 u32 htotal;
13407 u32 hblank;
13408 u32 hsync;
13409 u32 vtotal;
13410 u32 vblank;
13411 u32 vsync;
13412 } transcoder[4];
c4a1d9e4
CW
13413};
13414
13415struct intel_display_error_state *
13416intel_display_capture_error_state(struct drm_device *dev)
13417{
fbee40df 13418 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13419 struct intel_display_error_state *error;
63b66e5b
CW
13420 int transcoders[] = {
13421 TRANSCODER_A,
13422 TRANSCODER_B,
13423 TRANSCODER_C,
13424 TRANSCODER_EDP,
13425 };
c4a1d9e4
CW
13426 int i;
13427
63b66e5b
CW
13428 if (INTEL_INFO(dev)->num_pipes == 0)
13429 return NULL;
13430
9d1cb914 13431 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13432 if (error == NULL)
13433 return NULL;
13434
190be112 13435 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13436 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13437
055e393f 13438 for_each_pipe(dev_priv, i) {
ddf9c536 13439 error->pipe[i].power_domain_on =
bfafe93a
ID
13440 intel_display_power_enabled_unlocked(dev_priv,
13441 POWER_DOMAIN_PIPE(i));
ddf9c536 13442 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13443 continue;
13444
5efb3e28
VS
13445 error->cursor[i].control = I915_READ(CURCNTR(i));
13446 error->cursor[i].position = I915_READ(CURPOS(i));
13447 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13448
13449 error->plane[i].control = I915_READ(DSPCNTR(i));
13450 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13451 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13452 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13453 error->plane[i].pos = I915_READ(DSPPOS(i));
13454 }
ca291363
PZ
13455 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13456 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13457 if (INTEL_INFO(dev)->gen >= 4) {
13458 error->plane[i].surface = I915_READ(DSPSURF(i));
13459 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13460 }
13461
c4a1d9e4 13462 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13463
3abfce77 13464 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13465 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13466 }
13467
13468 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13469 if (HAS_DDI(dev_priv->dev))
13470 error->num_transcoders++; /* Account for eDP. */
13471
13472 for (i = 0; i < error->num_transcoders; i++) {
13473 enum transcoder cpu_transcoder = transcoders[i];
13474
ddf9c536 13475 error->transcoder[i].power_domain_on =
bfafe93a 13476 intel_display_power_enabled_unlocked(dev_priv,
38cc1daf 13477 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13478 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13479 continue;
13480
63b66e5b
CW
13481 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13482
13483 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13484 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13485 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13486 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13487 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13488 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13489 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13490 }
13491
13492 return error;
13493}
13494
edc3d884
MK
13495#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13496
c4a1d9e4 13497void
edc3d884 13498intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13499 struct drm_device *dev,
13500 struct intel_display_error_state *error)
13501{
055e393f 13502 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13503 int i;
13504
63b66e5b
CW
13505 if (!error)
13506 return;
13507
edc3d884 13508 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13509 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13510 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13511 error->power_well_driver);
055e393f 13512 for_each_pipe(dev_priv, i) {
edc3d884 13513 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13514 err_printf(m, " Power: %s\n",
13515 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13516 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13517 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13518
13519 err_printf(m, "Plane [%d]:\n", i);
13520 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13521 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13522 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13523 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13524 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13525 }
4b71a570 13526 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13527 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13528 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13529 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13530 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13531 }
13532
edc3d884
MK
13533 err_printf(m, "Cursor [%d]:\n", i);
13534 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13535 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13536 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13537 }
63b66e5b
CW
13538
13539 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13540 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13541 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13542 err_printf(m, " Power: %s\n",
13543 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13544 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13545 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13546 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13547 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13548 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13549 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13550 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13551 }
c4a1d9e4 13552}
e2fcdaa9
VS
13553
13554void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13555{
13556 struct intel_crtc *crtc;
13557
13558 for_each_intel_crtc(dev, crtc) {
13559 struct intel_unpin_work *work;
13560 unsigned long irqflags;
13561
13562 spin_lock_irqsave(&dev->event_lock, irqflags);
13563
13564 work = crtc->unpin_work;
13565
13566 if (work && work->event &&
13567 work->event->base.file_priv == file) {
13568 kfree(work->event);
13569 work->event = NULL;
13570 }
13571
13572 spin_unlock_irqrestore(&dev->event_lock, irqflags);
13573 }
13574}
This page took 3.997749 seconds and 5 git commands to generate.