drm/i915: static inline for intel_wait_for_vblank
[linux-block.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
fbf49ea2
VS
895static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
898 u32 reg = PIPEDSL(pipe);
899 u32 line1, line2;
900 u32 line_mask;
901
902 if (IS_GEN2(dev))
903 line_mask = DSL_LINEMASK_GEN2;
904 else
905 line_mask = DSL_LINEMASK_GEN3;
906
907 line1 = I915_READ(reg) & line_mask;
908 mdelay(5);
909 line2 = I915_READ(reg) & line_mask;
910
911 return line1 == line2;
912}
913
ab7ad7f6
KP
914/*
915 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 916 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
917 *
918 * After disabling a pipe, we can't wait for vblank in the usual way,
919 * spinning on the vblank interrupt status bit, since we won't actually
920 * see an interrupt when the pipe is disabled.
921 *
ab7ad7f6
KP
922 * On Gen4 and above:
923 * wait for the pipe register state bit to turn off
924 *
925 * Otherwise:
926 * wait for the display line value to settle (it usually
927 * ends up stopping at the start of the next frame).
58e10eb9 928 *
9d0498a2 929 */
575f7ab7 930static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 931{
575f7ab7 932 struct drm_device *dev = crtc->base.dev;
9d0498a2 933 struct drm_i915_private *dev_priv = dev->dev_private;
575f7ab7
VS
934 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
935 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
936
937 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 938 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
939
940 /* Wait for the Pipe State to go off */
58e10eb9
CW
941 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
942 100))
284637d9 943 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 944 } else {
ab7ad7f6 945 /* Wait for the display line to settle */
fbf49ea2 946 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 947 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 948 }
79e53945
JB
949}
950
b0ea7d37
DL
951/*
952 * ibx_digital_port_connected - is the specified port connected?
953 * @dev_priv: i915 private structure
954 * @port: the port to test
955 *
956 * Returns true if @port is connected, false otherwise.
957 */
958bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
959 struct intel_digital_port *port)
960{
961 u32 bit;
962
c36346e3 963 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 964 switch (port->port) {
c36346e3
DL
965 case PORT_B:
966 bit = SDE_PORTB_HOTPLUG;
967 break;
968 case PORT_C:
969 bit = SDE_PORTC_HOTPLUG;
970 break;
971 case PORT_D:
972 bit = SDE_PORTD_HOTPLUG;
973 break;
974 default:
975 return true;
976 }
977 } else {
eba905b2 978 switch (port->port) {
c36346e3
DL
979 case PORT_B:
980 bit = SDE_PORTB_HOTPLUG_CPT;
981 break;
982 case PORT_C:
983 bit = SDE_PORTC_HOTPLUG_CPT;
984 break;
985 case PORT_D:
986 bit = SDE_PORTD_HOTPLUG_CPT;
987 break;
988 default:
989 return true;
990 }
b0ea7d37
DL
991 }
992
993 return I915_READ(SDEISR) & bit;
994}
995
b24e7179
JB
996static const char *state_string(bool enabled)
997{
998 return enabled ? "on" : "off";
999}
1000
1001/* Only for pre-ILK configs */
55607e8a
DV
1002void assert_pll(struct drm_i915_private *dev_priv,
1003 enum pipe pipe, bool state)
b24e7179
JB
1004{
1005 int reg;
1006 u32 val;
1007 bool cur_state;
1008
1009 reg = DPLL(pipe);
1010 val = I915_READ(reg);
1011 cur_state = !!(val & DPLL_VCO_ENABLE);
1012 WARN(cur_state != state,
1013 "PLL state assertion failure (expected %s, current %s)\n",
1014 state_string(state), state_string(cur_state));
1015}
b24e7179 1016
23538ef1
JN
1017/* XXX: the dsi pll is shared between MIPI DSI ports */
1018static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1019{
1020 u32 val;
1021 bool cur_state;
1022
1023 mutex_lock(&dev_priv->dpio_lock);
1024 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1025 mutex_unlock(&dev_priv->dpio_lock);
1026
1027 cur_state = val & DSI_PLL_VCO_EN;
1028 WARN(cur_state != state,
1029 "DSI PLL state assertion failure (expected %s, current %s)\n",
1030 state_string(state), state_string(cur_state));
1031}
1032#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1033#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1034
55607e8a 1035struct intel_shared_dpll *
e2b78267
DV
1036intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1037{
1038 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1039
a43f6e0f 1040 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1041 return NULL;
1042
a43f6e0f 1043 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1044}
1045
040484af 1046/* For ILK+ */
55607e8a
DV
1047void assert_shared_dpll(struct drm_i915_private *dev_priv,
1048 struct intel_shared_dpll *pll,
1049 bool state)
040484af 1050{
040484af 1051 bool cur_state;
5358901f 1052 struct intel_dpll_hw_state hw_state;
040484af 1053
92b27b08 1054 if (WARN (!pll,
46edb027 1055 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1056 return;
ee7b9f93 1057
5358901f 1058 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1059 WARN(cur_state != state,
5358901f
DV
1060 "%s assertion failure (expected %s, current %s)\n",
1061 pll->name, state_string(state), state_string(cur_state));
040484af 1062}
040484af
JB
1063
1064static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
1066{
1067 int reg;
1068 u32 val;
1069 bool cur_state;
ad80a810
PZ
1070 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1071 pipe);
040484af 1072
affa9354
PZ
1073 if (HAS_DDI(dev_priv->dev)) {
1074 /* DDI does not have a specific FDI_TX register */
ad80a810 1075 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1076 val = I915_READ(reg);
ad80a810 1077 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1078 } else {
1079 reg = FDI_TX_CTL(pipe);
1080 val = I915_READ(reg);
1081 cur_state = !!(val & FDI_TX_ENABLE);
1082 }
040484af
JB
1083 WARN(cur_state != state,
1084 "FDI TX state assertion failure (expected %s, current %s)\n",
1085 state_string(state), state_string(cur_state));
1086}
1087#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1088#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1089
1090static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1091 enum pipe pipe, bool state)
1092{
1093 int reg;
1094 u32 val;
1095 bool cur_state;
1096
d63fa0dc
PZ
1097 reg = FDI_RX_CTL(pipe);
1098 val = I915_READ(reg);
1099 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1100 WARN(cur_state != state,
1101 "FDI RX state assertion failure (expected %s, current %s)\n",
1102 state_string(state), state_string(cur_state));
1103}
1104#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1105#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1106
1107static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1108 enum pipe pipe)
1109{
1110 int reg;
1111 u32 val;
1112
1113 /* ILK FDI PLL is always enabled */
3d13ef2e 1114 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1115 return;
1116
bf507ef7 1117 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1118 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1119 return;
1120
040484af
JB
1121 reg = FDI_TX_CTL(pipe);
1122 val = I915_READ(reg);
1123 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1124}
1125
55607e8a
DV
1126void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
040484af
JB
1128{
1129 int reg;
1130 u32 val;
55607e8a 1131 bool cur_state;
040484af
JB
1132
1133 reg = FDI_RX_CTL(pipe);
1134 val = I915_READ(reg);
55607e8a
DV
1135 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1136 WARN(cur_state != state,
1137 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1138 state_string(state), state_string(cur_state));
040484af
JB
1139}
1140
ea0760cf
JB
1141static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1142 enum pipe pipe)
1143{
bedd4dba
JN
1144 struct drm_device *dev = dev_priv->dev;
1145 int pp_reg;
ea0760cf
JB
1146 u32 val;
1147 enum pipe panel_pipe = PIPE_A;
0de3b485 1148 bool locked = true;
ea0760cf 1149
bedd4dba
JN
1150 if (WARN_ON(HAS_DDI(dev)))
1151 return;
1152
1153 if (HAS_PCH_SPLIT(dev)) {
1154 u32 port_sel;
1155
ea0760cf 1156 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1157 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1158
1159 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1160 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1161 panel_pipe = PIPE_B;
1162 /* XXX: else fix for eDP */
1163 } else if (IS_VALLEYVIEW(dev)) {
1164 /* presumably write lock depends on pipe, not port select */
1165 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1166 panel_pipe = pipe;
ea0760cf
JB
1167 } else {
1168 pp_reg = PP_CONTROL;
bedd4dba
JN
1169 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1170 panel_pipe = PIPE_B;
ea0760cf
JB
1171 }
1172
1173 val = I915_READ(pp_reg);
1174 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1175 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1176 locked = false;
1177
ea0760cf
JB
1178 WARN(panel_pipe == pipe && locked,
1179 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1180 pipe_name(pipe));
ea0760cf
JB
1181}
1182
93ce0ba6
JN
1183static void assert_cursor(struct drm_i915_private *dev_priv,
1184 enum pipe pipe, bool state)
1185{
1186 struct drm_device *dev = dev_priv->dev;
1187 bool cur_state;
1188
d9d82081 1189 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1190 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1191 else
5efb3e28 1192 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1193
1194 WARN(cur_state != state,
1195 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1196 pipe_name(pipe), state_string(state), state_string(cur_state));
1197}
1198#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1199#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1200
b840d907
JB
1201void assert_pipe(struct drm_i915_private *dev_priv,
1202 enum pipe pipe, bool state)
b24e7179
JB
1203{
1204 int reg;
1205 u32 val;
63d7bbe9 1206 bool cur_state;
702e7a56
PZ
1207 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1208 pipe);
b24e7179 1209
b6b5d049
VS
1210 /* if we need the pipe quirk it must be always on */
1211 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1212 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1213 state = true;
1214
da7e29bd 1215 if (!intel_display_power_enabled(dev_priv,
b97186f0 1216 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1217 cur_state = false;
1218 } else {
1219 reg = PIPECONF(cpu_transcoder);
1220 val = I915_READ(reg);
1221 cur_state = !!(val & PIPECONF_ENABLE);
1222 }
1223
63d7bbe9
JB
1224 WARN(cur_state != state,
1225 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1226 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1227}
1228
931872fc
CW
1229static void assert_plane(struct drm_i915_private *dev_priv,
1230 enum plane plane, bool state)
b24e7179
JB
1231{
1232 int reg;
1233 u32 val;
931872fc 1234 bool cur_state;
b24e7179
JB
1235
1236 reg = DSPCNTR(plane);
1237 val = I915_READ(reg);
931872fc
CW
1238 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1239 WARN(cur_state != state,
1240 "plane %c assertion failure (expected %s, current %s)\n",
1241 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1242}
1243
931872fc
CW
1244#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1245#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1246
b24e7179
JB
1247static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1248 enum pipe pipe)
1249{
653e1026 1250 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1251 int reg, i;
1252 u32 val;
1253 int cur_pipe;
1254
653e1026
VS
1255 /* Primary planes are fixed to pipes on gen4+ */
1256 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1257 reg = DSPCNTR(pipe);
1258 val = I915_READ(reg);
83f26f16 1259 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1260 "plane %c assertion failure, should be disabled but not\n",
1261 plane_name(pipe));
19ec1358 1262 return;
28c05794 1263 }
19ec1358 1264
b24e7179 1265 /* Need to check both planes against the pipe */
055e393f 1266 for_each_pipe(dev_priv, i) {
b24e7179
JB
1267 reg = DSPCNTR(i);
1268 val = I915_READ(reg);
1269 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1270 DISPPLANE_SEL_PIPE_SHIFT;
1271 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1272 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1273 plane_name(i), pipe_name(pipe));
b24e7179
JB
1274 }
1275}
1276
19332d7a
JB
1277static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1278 enum pipe pipe)
1279{
20674eef 1280 struct drm_device *dev = dev_priv->dev;
1fe47785 1281 int reg, sprite;
19332d7a
JB
1282 u32 val;
1283
20674eef 1284 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1285 for_each_sprite(pipe, sprite) {
1286 reg = SPCNTR(pipe, sprite);
20674eef 1287 val = I915_READ(reg);
83f26f16 1288 WARN(val & SP_ENABLE,
20674eef 1289 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1290 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1291 }
1292 } else if (INTEL_INFO(dev)->gen >= 7) {
1293 reg = SPRCTL(pipe);
19332d7a 1294 val = I915_READ(reg);
83f26f16 1295 WARN(val & SPRITE_ENABLE,
06da8da2 1296 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1297 plane_name(pipe), pipe_name(pipe));
1298 } else if (INTEL_INFO(dev)->gen >= 5) {
1299 reg = DVSCNTR(pipe);
19332d7a 1300 val = I915_READ(reg);
83f26f16 1301 WARN(val & DVS_ENABLE,
06da8da2 1302 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1303 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1304 }
1305}
1306
08c71e5e
VS
1307static void assert_vblank_disabled(struct drm_crtc *crtc)
1308{
1309 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1310 drm_crtc_vblank_put(crtc);
1311}
1312
89eff4be 1313static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1314{
1315 u32 val;
1316 bool enabled;
1317
89eff4be 1318 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1319
92f2584a
JB
1320 val = I915_READ(PCH_DREF_CONTROL);
1321 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1322 DREF_SUPERSPREAD_SOURCE_MASK));
1323 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1324}
1325
ab9412ba
DV
1326static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1327 enum pipe pipe)
92f2584a
JB
1328{
1329 int reg;
1330 u32 val;
1331 bool enabled;
1332
ab9412ba 1333 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1334 val = I915_READ(reg);
1335 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1336 WARN(enabled,
1337 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1338 pipe_name(pipe));
92f2584a
JB
1339}
1340
4e634389
KP
1341static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1342 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1343{
1344 if ((val & DP_PORT_EN) == 0)
1345 return false;
1346
1347 if (HAS_PCH_CPT(dev_priv->dev)) {
1348 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1349 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1350 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1351 return false;
44f37d1f
CML
1352 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1353 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1354 return false;
f0575e92
KP
1355 } else {
1356 if ((val & DP_PIPE_MASK) != (pipe << 30))
1357 return false;
1358 }
1359 return true;
1360}
1361
1519b995
KP
1362static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
dc0fa718 1365 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1366 return false;
1367
1368 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1369 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1370 return false;
44f37d1f
CML
1371 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1372 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1373 return false;
1519b995 1374 } else {
dc0fa718 1375 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1376 return false;
1377 }
1378 return true;
1379}
1380
1381static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1382 enum pipe pipe, u32 val)
1383{
1384 if ((val & LVDS_PORT_EN) == 0)
1385 return false;
1386
1387 if (HAS_PCH_CPT(dev_priv->dev)) {
1388 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1389 return false;
1390 } else {
1391 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1392 return false;
1393 }
1394 return true;
1395}
1396
1397static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe, u32 val)
1399{
1400 if ((val & ADPA_DAC_ENABLE) == 0)
1401 return false;
1402 if (HAS_PCH_CPT(dev_priv->dev)) {
1403 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1404 return false;
1405 } else {
1406 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1407 return false;
1408 }
1409 return true;
1410}
1411
291906f1 1412static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1413 enum pipe pipe, int reg, u32 port_sel)
291906f1 1414{
47a05eca 1415 u32 val = I915_READ(reg);
4e634389 1416 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1417 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1418 reg, pipe_name(pipe));
de9a35ab 1419
75c5da27
DV
1420 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1421 && (val & DP_PIPEB_SELECT),
de9a35ab 1422 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1423}
1424
1425static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1426 enum pipe pipe, int reg)
1427{
47a05eca 1428 u32 val = I915_READ(reg);
b70ad586 1429 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1430 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1431 reg, pipe_name(pipe));
de9a35ab 1432
dc0fa718 1433 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1434 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1435 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1436}
1437
1438static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
1440{
1441 int reg;
1442 u32 val;
291906f1 1443
f0575e92
KP
1444 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1445 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1446 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1447
1448 reg = PCH_ADPA;
1449 val = I915_READ(reg);
b70ad586 1450 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1451 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1452 pipe_name(pipe));
291906f1
JB
1453
1454 reg = PCH_LVDS;
1455 val = I915_READ(reg);
b70ad586 1456 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1457 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1458 pipe_name(pipe));
291906f1 1459
e2debe91
PZ
1460 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1461 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1462 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1463}
1464
40e9cf64
JB
1465static void intel_init_dpio(struct drm_device *dev)
1466{
1467 struct drm_i915_private *dev_priv = dev->dev_private;
1468
1469 if (!IS_VALLEYVIEW(dev))
1470 return;
1471
a09caddd
CML
1472 /*
1473 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1474 * CHV x1 PHY (DP/HDMI D)
1475 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1476 */
1477 if (IS_CHERRYVIEW(dev)) {
1478 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1479 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1480 } else {
1481 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1482 }
5382f5f3
JB
1483}
1484
426115cf 1485static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1486{
426115cf
DV
1487 struct drm_device *dev = crtc->base.dev;
1488 struct drm_i915_private *dev_priv = dev->dev_private;
1489 int reg = DPLL(crtc->pipe);
1490 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1491
426115cf 1492 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1493
1494 /* No really, not for ILK+ */
1495 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1496
1497 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1498 if (IS_MOBILE(dev_priv->dev))
426115cf 1499 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1500
426115cf
DV
1501 I915_WRITE(reg, dpll);
1502 POSTING_READ(reg);
1503 udelay(150);
1504
1505 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1506 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1507
1508 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1509 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1510
1511 /* We do this three times for luck */
426115cf 1512 I915_WRITE(reg, dpll);
87442f73
DV
1513 POSTING_READ(reg);
1514 udelay(150); /* wait for warmup */
426115cf 1515 I915_WRITE(reg, dpll);
87442f73
DV
1516 POSTING_READ(reg);
1517 udelay(150); /* wait for warmup */
426115cf 1518 I915_WRITE(reg, dpll);
87442f73
DV
1519 POSTING_READ(reg);
1520 udelay(150); /* wait for warmup */
1521}
1522
9d556c99
CML
1523static void chv_enable_pll(struct intel_crtc *crtc)
1524{
1525 struct drm_device *dev = crtc->base.dev;
1526 struct drm_i915_private *dev_priv = dev->dev_private;
1527 int pipe = crtc->pipe;
1528 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1529 u32 tmp;
1530
1531 assert_pipe_disabled(dev_priv, crtc->pipe);
1532
1533 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1534
1535 mutex_lock(&dev_priv->dpio_lock);
1536
1537 /* Enable back the 10bit clock to display controller */
1538 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1539 tmp |= DPIO_DCLKP_EN;
1540 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1541
1542 /*
1543 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1544 */
1545 udelay(1);
1546
1547 /* Enable PLL */
a11b0703 1548 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1549
1550 /* Check PLL is locked */
a11b0703 1551 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1552 DRM_ERROR("PLL %d failed to lock\n", pipe);
1553
a11b0703
VS
1554 /* not sure when this should be written */
1555 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1556 POSTING_READ(DPLL_MD(pipe));
1557
9d556c99
CML
1558 mutex_unlock(&dev_priv->dpio_lock);
1559}
1560
1c4e0274
VS
1561static int intel_num_dvo_pipes(struct drm_device *dev)
1562{
1563 struct intel_crtc *crtc;
1564 int count = 0;
1565
1566 for_each_intel_crtc(dev, crtc)
1567 count += crtc->active &&
1568 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
1569
1570 return count;
1571}
1572
66e3d5c0 1573static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1574{
66e3d5c0
DV
1575 struct drm_device *dev = crtc->base.dev;
1576 struct drm_i915_private *dev_priv = dev->dev_private;
1577 int reg = DPLL(crtc->pipe);
1578 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1579
66e3d5c0 1580 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1581
63d7bbe9 1582 /* No really, not for ILK+ */
3d13ef2e 1583 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1584
1585 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1586 if (IS_MOBILE(dev) && !IS_I830(dev))
1587 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1588
1c4e0274
VS
1589 /* Enable DVO 2x clock on both PLLs if necessary */
1590 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1591 /*
1592 * It appears to be important that we don't enable this
1593 * for the current pipe before otherwise configuring the
1594 * PLL. No idea how this should be handled if multiple
1595 * DVO outputs are enabled simultaneosly.
1596 */
1597 dpll |= DPLL_DVO_2X_MODE;
1598 I915_WRITE(DPLL(!crtc->pipe),
1599 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1600 }
66e3d5c0
DV
1601
1602 /* Wait for the clocks to stabilize. */
1603 POSTING_READ(reg);
1604 udelay(150);
1605
1606 if (INTEL_INFO(dev)->gen >= 4) {
1607 I915_WRITE(DPLL_MD(crtc->pipe),
1608 crtc->config.dpll_hw_state.dpll_md);
1609 } else {
1610 /* The pixel multiplier can only be updated once the
1611 * DPLL is enabled and the clocks are stable.
1612 *
1613 * So write it again.
1614 */
1615 I915_WRITE(reg, dpll);
1616 }
63d7bbe9
JB
1617
1618 /* We do this three times for luck */
66e3d5c0 1619 I915_WRITE(reg, dpll);
63d7bbe9
JB
1620 POSTING_READ(reg);
1621 udelay(150); /* wait for warmup */
66e3d5c0 1622 I915_WRITE(reg, dpll);
63d7bbe9
JB
1623 POSTING_READ(reg);
1624 udelay(150); /* wait for warmup */
66e3d5c0 1625 I915_WRITE(reg, dpll);
63d7bbe9
JB
1626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
1628}
1629
1630/**
50b44a44 1631 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1632 * @dev_priv: i915 private structure
1633 * @pipe: pipe PLL to disable
1634 *
1635 * Disable the PLL for @pipe, making sure the pipe is off first.
1636 *
1637 * Note! This is for pre-ILK only.
1638 */
1c4e0274 1639static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1640{
1c4e0274
VS
1641 struct drm_device *dev = crtc->base.dev;
1642 struct drm_i915_private *dev_priv = dev->dev_private;
1643 enum pipe pipe = crtc->pipe;
1644
1645 /* Disable DVO 2x clock on both PLLs if necessary */
1646 if (IS_I830(dev) &&
1647 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
1648 intel_num_dvo_pipes(dev) == 1) {
1649 I915_WRITE(DPLL(PIPE_B),
1650 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1651 I915_WRITE(DPLL(PIPE_A),
1652 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1653 }
1654
b6b5d049
VS
1655 /* Don't disable pipe or pipe PLLs if needed */
1656 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1657 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1658 return;
1659
1660 /* Make sure the pipe isn't still relying on us */
1661 assert_pipe_disabled(dev_priv, pipe);
1662
50b44a44
DV
1663 I915_WRITE(DPLL(pipe), 0);
1664 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1665}
1666
f6071166
JB
1667static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1668{
1669 u32 val = 0;
1670
1671 /* Make sure the pipe isn't still relying on us */
1672 assert_pipe_disabled(dev_priv, pipe);
1673
e5cbfbfb
ID
1674 /*
1675 * Leave integrated clock source and reference clock enabled for pipe B.
1676 * The latter is needed for VGA hotplug / manual detection.
1677 */
f6071166 1678 if (pipe == PIPE_B)
e5cbfbfb 1679 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1680 I915_WRITE(DPLL(pipe), val);
1681 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1682
1683}
1684
1685static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1686{
d752048d 1687 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1688 u32 val;
1689
a11b0703
VS
1690 /* Make sure the pipe isn't still relying on us */
1691 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1692
a11b0703 1693 /* Set PLL en = 0 */
d17ec4ce 1694 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1695 if (pipe != PIPE_A)
1696 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1697 I915_WRITE(DPLL(pipe), val);
1698 POSTING_READ(DPLL(pipe));
d752048d
VS
1699
1700 mutex_lock(&dev_priv->dpio_lock);
1701
1702 /* Disable 10bit clock to display controller */
1703 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1704 val &= ~DPIO_DCLKP_EN;
1705 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1706
61407f6d
VS
1707 /* disable left/right clock distribution */
1708 if (pipe != PIPE_B) {
1709 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1710 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1711 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1712 } else {
1713 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1714 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1715 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1716 }
1717
d752048d 1718 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1719}
1720
e4607fcf
CML
1721void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1722 struct intel_digital_port *dport)
89b667f8
JB
1723{
1724 u32 port_mask;
00fc31b7 1725 int dpll_reg;
89b667f8 1726
e4607fcf
CML
1727 switch (dport->port) {
1728 case PORT_B:
89b667f8 1729 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1730 dpll_reg = DPLL(0);
e4607fcf
CML
1731 break;
1732 case PORT_C:
89b667f8 1733 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1734 dpll_reg = DPLL(0);
1735 break;
1736 case PORT_D:
1737 port_mask = DPLL_PORTD_READY_MASK;
1738 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1739 break;
1740 default:
1741 BUG();
1742 }
89b667f8 1743
00fc31b7 1744 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1745 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1746 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1747}
1748
b14b1055
DV
1749static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1750{
1751 struct drm_device *dev = crtc->base.dev;
1752 struct drm_i915_private *dev_priv = dev->dev_private;
1753 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1754
be19f0ff
CW
1755 if (WARN_ON(pll == NULL))
1756 return;
1757
b14b1055
DV
1758 WARN_ON(!pll->refcount);
1759 if (pll->active == 0) {
1760 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1761 WARN_ON(pll->on);
1762 assert_shared_dpll_disabled(dev_priv, pll);
1763
1764 pll->mode_set(dev_priv, pll);
1765 }
1766}
1767
92f2584a 1768/**
85b3894f 1769 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1770 * @dev_priv: i915 private structure
1771 * @pipe: pipe PLL to enable
1772 *
1773 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1774 * drives the transcoder clock.
1775 */
85b3894f 1776static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1777{
3d13ef2e
DL
1778 struct drm_device *dev = crtc->base.dev;
1779 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1780 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1781
87a875bb 1782 if (WARN_ON(pll == NULL))
48da64a8
CW
1783 return;
1784
1785 if (WARN_ON(pll->refcount == 0))
1786 return;
ee7b9f93 1787
74dd6928 1788 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1789 pll->name, pll->active, pll->on,
e2b78267 1790 crtc->base.base.id);
92f2584a 1791
cdbd2316
DV
1792 if (pll->active++) {
1793 WARN_ON(!pll->on);
e9d6944e 1794 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1795 return;
1796 }
f4a091c7 1797 WARN_ON(pll->on);
ee7b9f93 1798
bd2bb1b9
PZ
1799 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1800
46edb027 1801 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1802 pll->enable(dev_priv, pll);
ee7b9f93 1803 pll->on = true;
92f2584a
JB
1804}
1805
f6daaec2 1806static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1807{
3d13ef2e
DL
1808 struct drm_device *dev = crtc->base.dev;
1809 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1810 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1811
92f2584a 1812 /* PCH only available on ILK+ */
3d13ef2e 1813 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1814 if (WARN_ON(pll == NULL))
ee7b9f93 1815 return;
92f2584a 1816
48da64a8
CW
1817 if (WARN_ON(pll->refcount == 0))
1818 return;
7a419866 1819
46edb027
DV
1820 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1821 pll->name, pll->active, pll->on,
e2b78267 1822 crtc->base.base.id);
7a419866 1823
48da64a8 1824 if (WARN_ON(pll->active == 0)) {
e9d6944e 1825 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1826 return;
1827 }
1828
e9d6944e 1829 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1830 WARN_ON(!pll->on);
cdbd2316 1831 if (--pll->active)
7a419866 1832 return;
ee7b9f93 1833
46edb027 1834 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1835 pll->disable(dev_priv, pll);
ee7b9f93 1836 pll->on = false;
bd2bb1b9
PZ
1837
1838 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1839}
1840
b8a4f404
PZ
1841static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1842 enum pipe pipe)
040484af 1843{
23670b32 1844 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1845 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1847 uint32_t reg, val, pipeconf_val;
040484af
JB
1848
1849 /* PCH only available on ILK+ */
55522f37 1850 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1851
1852 /* Make sure PCH DPLL is enabled */
e72f9fbf 1853 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1854 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1855
1856 /* FDI must be feeding us bits for PCH ports */
1857 assert_fdi_tx_enabled(dev_priv, pipe);
1858 assert_fdi_rx_enabled(dev_priv, pipe);
1859
23670b32
DV
1860 if (HAS_PCH_CPT(dev)) {
1861 /* Workaround: Set the timing override bit before enabling the
1862 * pch transcoder. */
1863 reg = TRANS_CHICKEN2(pipe);
1864 val = I915_READ(reg);
1865 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1866 I915_WRITE(reg, val);
59c859d6 1867 }
23670b32 1868
ab9412ba 1869 reg = PCH_TRANSCONF(pipe);
040484af 1870 val = I915_READ(reg);
5f7f726d 1871 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1872
1873 if (HAS_PCH_IBX(dev_priv->dev)) {
1874 /*
1875 * make the BPC in transcoder be consistent with
1876 * that in pipeconf reg.
1877 */
dfd07d72
DV
1878 val &= ~PIPECONF_BPC_MASK;
1879 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1880 }
5f7f726d
PZ
1881
1882 val &= ~TRANS_INTERLACE_MASK;
1883 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1884 if (HAS_PCH_IBX(dev_priv->dev) &&
1885 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1886 val |= TRANS_LEGACY_INTERLACED_ILK;
1887 else
1888 val |= TRANS_INTERLACED;
5f7f726d
PZ
1889 else
1890 val |= TRANS_PROGRESSIVE;
1891
040484af
JB
1892 I915_WRITE(reg, val | TRANS_ENABLE);
1893 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1894 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1895}
1896
8fb033d7 1897static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1898 enum transcoder cpu_transcoder)
040484af 1899{
8fb033d7 1900 u32 val, pipeconf_val;
8fb033d7
PZ
1901
1902 /* PCH only available on ILK+ */
55522f37 1903 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1904
8fb033d7 1905 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1906 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1907 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1908
223a6fdf
PZ
1909 /* Workaround: set timing override bit. */
1910 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1911 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1912 I915_WRITE(_TRANSA_CHICKEN2, val);
1913
25f3ef11 1914 val = TRANS_ENABLE;
937bb610 1915 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1916
9a76b1c6
PZ
1917 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1918 PIPECONF_INTERLACED_ILK)
a35f2679 1919 val |= TRANS_INTERLACED;
8fb033d7
PZ
1920 else
1921 val |= TRANS_PROGRESSIVE;
1922
ab9412ba
DV
1923 I915_WRITE(LPT_TRANSCONF, val);
1924 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1925 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1926}
1927
b8a4f404
PZ
1928static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1929 enum pipe pipe)
040484af 1930{
23670b32
DV
1931 struct drm_device *dev = dev_priv->dev;
1932 uint32_t reg, val;
040484af
JB
1933
1934 /* FDI relies on the transcoder */
1935 assert_fdi_tx_disabled(dev_priv, pipe);
1936 assert_fdi_rx_disabled(dev_priv, pipe);
1937
291906f1
JB
1938 /* Ports must be off as well */
1939 assert_pch_ports_disabled(dev_priv, pipe);
1940
ab9412ba 1941 reg = PCH_TRANSCONF(pipe);
040484af
JB
1942 val = I915_READ(reg);
1943 val &= ~TRANS_ENABLE;
1944 I915_WRITE(reg, val);
1945 /* wait for PCH transcoder off, transcoder state */
1946 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1947 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1948
1949 if (!HAS_PCH_IBX(dev)) {
1950 /* Workaround: Clear the timing override chicken bit again. */
1951 reg = TRANS_CHICKEN2(pipe);
1952 val = I915_READ(reg);
1953 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1954 I915_WRITE(reg, val);
1955 }
040484af
JB
1956}
1957
ab4d966c 1958static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1959{
8fb033d7
PZ
1960 u32 val;
1961
ab9412ba 1962 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1963 val &= ~TRANS_ENABLE;
ab9412ba 1964 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1965 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1966 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1967 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1968
1969 /* Workaround: clear timing override bit. */
1970 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1971 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1972 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1973}
1974
b24e7179 1975/**
309cfea8 1976 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1977 * @crtc: crtc responsible for the pipe
b24e7179 1978 *
0372264a 1979 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1980 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1981 */
e1fdc473 1982static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1983{
0372264a
PZ
1984 struct drm_device *dev = crtc->base.dev;
1985 struct drm_i915_private *dev_priv = dev->dev_private;
1986 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1987 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1988 pipe);
1a240d4d 1989 enum pipe pch_transcoder;
b24e7179
JB
1990 int reg;
1991 u32 val;
1992
58c6eaa2 1993 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1994 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1995 assert_sprites_disabled(dev_priv, pipe);
1996
681e5811 1997 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1998 pch_transcoder = TRANSCODER_A;
1999 else
2000 pch_transcoder = pipe;
2001
b24e7179
JB
2002 /*
2003 * A pipe without a PLL won't actually be able to drive bits from
2004 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2005 * need the check.
2006 */
2007 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2008 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2009 assert_dsi_pll_enabled(dev_priv);
2010 else
2011 assert_pll_enabled(dev_priv, pipe);
040484af 2012 else {
30421c4f 2013 if (crtc->config.has_pch_encoder) {
040484af 2014 /* if driving the PCH, we need FDI enabled */
cc391bbb 2015 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2016 assert_fdi_tx_pll_enabled(dev_priv,
2017 (enum pipe) cpu_transcoder);
040484af
JB
2018 }
2019 /* FIXME: assert CPU port conditions for SNB+ */
2020 }
b24e7179 2021
702e7a56 2022 reg = PIPECONF(cpu_transcoder);
b24e7179 2023 val = I915_READ(reg);
7ad25d48 2024 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2025 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2026 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2027 return;
7ad25d48 2028 }
00d70b15
CW
2029
2030 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2031 POSTING_READ(reg);
b24e7179
JB
2032}
2033
2034/**
309cfea8 2035 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2036 * @crtc: crtc whose pipes is to be disabled
b24e7179 2037 *
575f7ab7
VS
2038 * Disable the pipe of @crtc, making sure that various hardware
2039 * specific requirements are met, if applicable, e.g. plane
2040 * disabled, panel fitter off, etc.
b24e7179
JB
2041 *
2042 * Will wait until the pipe has shut down before returning.
2043 */
575f7ab7 2044static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2045{
575f7ab7
VS
2046 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2047 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2048 enum pipe pipe = crtc->pipe;
b24e7179
JB
2049 int reg;
2050 u32 val;
2051
2052 /*
2053 * Make sure planes won't keep trying to pump pixels to us,
2054 * or we might hang the display.
2055 */
2056 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2057 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2058 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2059
702e7a56 2060 reg = PIPECONF(cpu_transcoder);
b24e7179 2061 val = I915_READ(reg);
00d70b15
CW
2062 if ((val & PIPECONF_ENABLE) == 0)
2063 return;
2064
67adc644
VS
2065 /*
2066 * Double wide has implications for planes
2067 * so best keep it disabled when not needed.
2068 */
2069 if (crtc->config.double_wide)
2070 val &= ~PIPECONF_DOUBLE_WIDE;
2071
2072 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2073 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2074 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2075 val &= ~PIPECONF_ENABLE;
2076
2077 I915_WRITE(reg, val);
2078 if ((val & PIPECONF_ENABLE) == 0)
2079 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2080}
2081
d74362c9
KP
2082/*
2083 * Plane regs are double buffered, going from enabled->disabled needs a
2084 * trigger in order to latch. The display address reg provides this.
2085 */
1dba99f4
VS
2086void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2087 enum plane plane)
d74362c9 2088{
3d13ef2e
DL
2089 struct drm_device *dev = dev_priv->dev;
2090 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2091
2092 I915_WRITE(reg, I915_READ(reg));
2093 POSTING_READ(reg);
d74362c9
KP
2094}
2095
b24e7179 2096/**
262ca2b0 2097 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2098 * @plane: plane to be enabled
2099 * @crtc: crtc for the plane
b24e7179 2100 *
fdd508a6 2101 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2102 */
fdd508a6
VS
2103static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2104 struct drm_crtc *crtc)
b24e7179 2105{
fdd508a6
VS
2106 struct drm_device *dev = plane->dev;
2107 struct drm_i915_private *dev_priv = dev->dev_private;
2108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2109
2110 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2111 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2112
98ec7739
VS
2113 if (intel_crtc->primary_enabled)
2114 return;
0037f71c 2115
4c445e0e 2116 intel_crtc->primary_enabled = true;
939c2fe8 2117
fdd508a6
VS
2118 dev_priv->display.update_primary_plane(crtc, plane->fb,
2119 crtc->x, crtc->y);
33c3b0d1
VS
2120
2121 /*
2122 * BDW signals flip done immediately if the plane
2123 * is disabled, even if the plane enable is already
2124 * armed to occur at the next vblank :(
2125 */
2126 if (IS_BROADWELL(dev))
2127 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2128}
2129
b24e7179 2130/**
262ca2b0 2131 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2132 * @plane: plane to be disabled
2133 * @crtc: crtc for the plane
b24e7179 2134 *
fdd508a6 2135 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2136 */
fdd508a6
VS
2137static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2138 struct drm_crtc *crtc)
b24e7179 2139{
fdd508a6
VS
2140 struct drm_device *dev = plane->dev;
2141 struct drm_i915_private *dev_priv = dev->dev_private;
2142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2143
2144 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2145
98ec7739
VS
2146 if (!intel_crtc->primary_enabled)
2147 return;
0037f71c 2148
4c445e0e 2149 intel_crtc->primary_enabled = false;
939c2fe8 2150
fdd508a6
VS
2151 dev_priv->display.update_primary_plane(crtc, plane->fb,
2152 crtc->x, crtc->y);
b24e7179
JB
2153}
2154
693db184
CW
2155static bool need_vtd_wa(struct drm_device *dev)
2156{
2157#ifdef CONFIG_INTEL_IOMMU
2158 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2159 return true;
2160#endif
2161 return false;
2162}
2163
a57ce0b2
JB
2164static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2165{
2166 int tile_height;
2167
2168 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2169 return ALIGN(height, tile_height);
2170}
2171
127bd2ac 2172int
48b956c5 2173intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2174 struct drm_i915_gem_object *obj,
a4872ba6 2175 struct intel_engine_cs *pipelined)
6b95a207 2176{
ce453d81 2177 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2178 u32 alignment;
2179 int ret;
2180
ebcdd39e
MR
2181 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2182
05394f39 2183 switch (obj->tiling_mode) {
6b95a207 2184 case I915_TILING_NONE:
534843da
CW
2185 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2186 alignment = 128 * 1024;
a6c45cf0 2187 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2188 alignment = 4 * 1024;
2189 else
2190 alignment = 64 * 1024;
6b95a207
KH
2191 break;
2192 case I915_TILING_X:
2193 /* pin() will align the object as required by fence */
2194 alignment = 0;
2195 break;
2196 case I915_TILING_Y:
80075d49 2197 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2198 return -EINVAL;
2199 default:
2200 BUG();
2201 }
2202
693db184
CW
2203 /* Note that the w/a also requires 64 PTE of padding following the
2204 * bo. We currently fill all unused PTE with the shadow page and so
2205 * we should always have valid PTE following the scanout preventing
2206 * the VT-d warning.
2207 */
2208 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2209 alignment = 256 * 1024;
2210
d6dd6843
PZ
2211 /*
2212 * Global gtt pte registers are special registers which actually forward
2213 * writes to a chunk of system memory. Which means that there is no risk
2214 * that the register values disappear as soon as we call
2215 * intel_runtime_pm_put(), so it is correct to wrap only the
2216 * pin/unpin/fence and not more.
2217 */
2218 intel_runtime_pm_get(dev_priv);
2219
ce453d81 2220 dev_priv->mm.interruptible = false;
2da3b9b9 2221 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2222 if (ret)
ce453d81 2223 goto err_interruptible;
6b95a207
KH
2224
2225 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2226 * fence, whereas 965+ only requires a fence if using
2227 * framebuffer compression. For simplicity, we always install
2228 * a fence as the cost is not that onerous.
2229 */
06d98131 2230 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2231 if (ret)
2232 goto err_unpin;
1690e1eb 2233
9a5a53b3 2234 i915_gem_object_pin_fence(obj);
6b95a207 2235
ce453d81 2236 dev_priv->mm.interruptible = true;
d6dd6843 2237 intel_runtime_pm_put(dev_priv);
6b95a207 2238 return 0;
48b956c5
CW
2239
2240err_unpin:
cc98b413 2241 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2242err_interruptible:
2243 dev_priv->mm.interruptible = true;
d6dd6843 2244 intel_runtime_pm_put(dev_priv);
48b956c5 2245 return ret;
6b95a207
KH
2246}
2247
1690e1eb
CW
2248void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2249{
ebcdd39e
MR
2250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
1690e1eb 2252 i915_gem_object_unpin_fence(obj);
cc98b413 2253 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2254}
2255
c2c75131
DV
2256/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2257 * is assumed to be a power-of-two. */
bc752862
CW
2258unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2259 unsigned int tiling_mode,
2260 unsigned int cpp,
2261 unsigned int pitch)
c2c75131 2262{
bc752862
CW
2263 if (tiling_mode != I915_TILING_NONE) {
2264 unsigned int tile_rows, tiles;
c2c75131 2265
bc752862
CW
2266 tile_rows = *y / 8;
2267 *y %= 8;
c2c75131 2268
bc752862
CW
2269 tiles = *x / (512/cpp);
2270 *x %= 512/cpp;
2271
2272 return tile_rows * pitch * 8 + tiles * 4096;
2273 } else {
2274 unsigned int offset;
2275
2276 offset = *y * pitch + *x * cpp;
2277 *y = 0;
2278 *x = (offset & 4095) / cpp;
2279 return offset & -4096;
2280 }
c2c75131
DV
2281}
2282
46f297fb
JB
2283int intel_format_to_fourcc(int format)
2284{
2285 switch (format) {
2286 case DISPPLANE_8BPP:
2287 return DRM_FORMAT_C8;
2288 case DISPPLANE_BGRX555:
2289 return DRM_FORMAT_XRGB1555;
2290 case DISPPLANE_BGRX565:
2291 return DRM_FORMAT_RGB565;
2292 default:
2293 case DISPPLANE_BGRX888:
2294 return DRM_FORMAT_XRGB8888;
2295 case DISPPLANE_RGBX888:
2296 return DRM_FORMAT_XBGR8888;
2297 case DISPPLANE_BGRX101010:
2298 return DRM_FORMAT_XRGB2101010;
2299 case DISPPLANE_RGBX101010:
2300 return DRM_FORMAT_XBGR2101010;
2301 }
2302}
2303
484b41dd 2304static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2305 struct intel_plane_config *plane_config)
2306{
2307 struct drm_device *dev = crtc->base.dev;
2308 struct drm_i915_gem_object *obj = NULL;
2309 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2310 u32 base = plane_config->base;
2311
ff2652ea
CW
2312 if (plane_config->size == 0)
2313 return false;
2314
46f297fb
JB
2315 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2316 plane_config->size);
2317 if (!obj)
484b41dd 2318 return false;
46f297fb
JB
2319
2320 if (plane_config->tiled) {
2321 obj->tiling_mode = I915_TILING_X;
66e514c1 2322 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2323 }
2324
66e514c1
DA
2325 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2326 mode_cmd.width = crtc->base.primary->fb->width;
2327 mode_cmd.height = crtc->base.primary->fb->height;
2328 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2329
2330 mutex_lock(&dev->struct_mutex);
2331
66e514c1 2332 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2333 &mode_cmd, obj)) {
46f297fb
JB
2334 DRM_DEBUG_KMS("intel fb init failed\n");
2335 goto out_unref_obj;
2336 }
2337
a071fa00 2338 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2339 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2340
2341 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2342 return true;
46f297fb
JB
2343
2344out_unref_obj:
2345 drm_gem_object_unreference(&obj->base);
2346 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2347 return false;
2348}
2349
2350static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2351 struct intel_plane_config *plane_config)
2352{
2353 struct drm_device *dev = intel_crtc->base.dev;
2354 struct drm_crtc *c;
2355 struct intel_crtc *i;
2ff8fde1 2356 struct drm_i915_gem_object *obj;
484b41dd 2357
66e514c1 2358 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2359 return;
2360
2361 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2362 return;
2363
66e514c1
DA
2364 kfree(intel_crtc->base.primary->fb);
2365 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2366
2367 /*
2368 * Failed to alloc the obj, check to see if we should share
2369 * an fb with another CRTC instead
2370 */
70e1e0ec 2371 for_each_crtc(dev, c) {
484b41dd
JB
2372 i = to_intel_crtc(c);
2373
2374 if (c == &intel_crtc->base)
2375 continue;
2376
2ff8fde1
MR
2377 if (!i->active)
2378 continue;
2379
2380 obj = intel_fb_obj(c->primary->fb);
2381 if (obj == NULL)
484b41dd
JB
2382 continue;
2383
2ff8fde1 2384 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2385 drm_framebuffer_reference(c->primary->fb);
2386 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2387 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2388 break;
2389 }
2390 }
46f297fb
JB
2391}
2392
29b9bde6
DV
2393static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2394 struct drm_framebuffer *fb,
2395 int x, int y)
81255565
JB
2396{
2397 struct drm_device *dev = crtc->dev;
2398 struct drm_i915_private *dev_priv = dev->dev_private;
2399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2400 struct drm_i915_gem_object *obj;
81255565 2401 int plane = intel_crtc->plane;
e506a0c6 2402 unsigned long linear_offset;
81255565 2403 u32 dspcntr;
f45651ba 2404 u32 reg = DSPCNTR(plane);
48404c1e 2405 int pixel_size;
f45651ba 2406
fdd508a6
VS
2407 if (!intel_crtc->primary_enabled) {
2408 I915_WRITE(reg, 0);
2409 if (INTEL_INFO(dev)->gen >= 4)
2410 I915_WRITE(DSPSURF(plane), 0);
2411 else
2412 I915_WRITE(DSPADDR(plane), 0);
2413 POSTING_READ(reg);
2414 return;
2415 }
2416
c9ba6fad
VS
2417 obj = intel_fb_obj(fb);
2418 if (WARN_ON(obj == NULL))
2419 return;
2420
2421 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2422
f45651ba
VS
2423 dspcntr = DISPPLANE_GAMMA_ENABLE;
2424
fdd508a6 2425 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2426
2427 if (INTEL_INFO(dev)->gen < 4) {
2428 if (intel_crtc->pipe == PIPE_B)
2429 dspcntr |= DISPPLANE_SEL_PIPE_B;
2430
2431 /* pipesrc and dspsize control the size that is scaled from,
2432 * which should always be the user's requested size.
2433 */
2434 I915_WRITE(DSPSIZE(plane),
2435 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2436 (intel_crtc->config.pipe_src_w - 1));
2437 I915_WRITE(DSPPOS(plane), 0);
2438 }
81255565 2439
57779d06
VS
2440 switch (fb->pixel_format) {
2441 case DRM_FORMAT_C8:
81255565
JB
2442 dspcntr |= DISPPLANE_8BPP;
2443 break;
57779d06
VS
2444 case DRM_FORMAT_XRGB1555:
2445 case DRM_FORMAT_ARGB1555:
2446 dspcntr |= DISPPLANE_BGRX555;
81255565 2447 break;
57779d06
VS
2448 case DRM_FORMAT_RGB565:
2449 dspcntr |= DISPPLANE_BGRX565;
2450 break;
2451 case DRM_FORMAT_XRGB8888:
2452 case DRM_FORMAT_ARGB8888:
2453 dspcntr |= DISPPLANE_BGRX888;
2454 break;
2455 case DRM_FORMAT_XBGR8888:
2456 case DRM_FORMAT_ABGR8888:
2457 dspcntr |= DISPPLANE_RGBX888;
2458 break;
2459 case DRM_FORMAT_XRGB2101010:
2460 case DRM_FORMAT_ARGB2101010:
2461 dspcntr |= DISPPLANE_BGRX101010;
2462 break;
2463 case DRM_FORMAT_XBGR2101010:
2464 case DRM_FORMAT_ABGR2101010:
2465 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2466 break;
2467 default:
baba133a 2468 BUG();
81255565 2469 }
57779d06 2470
f45651ba
VS
2471 if (INTEL_INFO(dev)->gen >= 4 &&
2472 obj->tiling_mode != I915_TILING_NONE)
2473 dspcntr |= DISPPLANE_TILED;
81255565 2474
de1aa629
VS
2475 if (IS_G4X(dev))
2476 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2477
b9897127 2478 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2479
c2c75131
DV
2480 if (INTEL_INFO(dev)->gen >= 4) {
2481 intel_crtc->dspaddr_offset =
bc752862 2482 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2483 pixel_size,
bc752862 2484 fb->pitches[0]);
c2c75131
DV
2485 linear_offset -= intel_crtc->dspaddr_offset;
2486 } else {
e506a0c6 2487 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2488 }
e506a0c6 2489
48404c1e
SJ
2490 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2491 dspcntr |= DISPPLANE_ROTATE_180;
2492
2493 x += (intel_crtc->config.pipe_src_w - 1);
2494 y += (intel_crtc->config.pipe_src_h - 1);
2495
2496 /* Finding the last pixel of the last line of the display
2497 data and adding to linear_offset*/
2498 linear_offset +=
2499 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2500 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2501 }
2502
2503 I915_WRITE(reg, dspcntr);
2504
f343c5f6
BW
2505 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2506 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2507 fb->pitches[0]);
01f2c773 2508 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2509 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2510 I915_WRITE(DSPSURF(plane),
2511 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2512 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2513 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2514 } else
f343c5f6 2515 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2516 POSTING_READ(reg);
17638cd6
JB
2517}
2518
29b9bde6
DV
2519static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2520 struct drm_framebuffer *fb,
2521 int x, int y)
17638cd6
JB
2522{
2523 struct drm_device *dev = crtc->dev;
2524 struct drm_i915_private *dev_priv = dev->dev_private;
2525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2526 struct drm_i915_gem_object *obj;
17638cd6 2527 int plane = intel_crtc->plane;
e506a0c6 2528 unsigned long linear_offset;
17638cd6 2529 u32 dspcntr;
f45651ba 2530 u32 reg = DSPCNTR(plane);
48404c1e 2531 int pixel_size;
f45651ba 2532
fdd508a6
VS
2533 if (!intel_crtc->primary_enabled) {
2534 I915_WRITE(reg, 0);
2535 I915_WRITE(DSPSURF(plane), 0);
2536 POSTING_READ(reg);
2537 return;
2538 }
2539
c9ba6fad
VS
2540 obj = intel_fb_obj(fb);
2541 if (WARN_ON(obj == NULL))
2542 return;
2543
2544 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2545
f45651ba
VS
2546 dspcntr = DISPPLANE_GAMMA_ENABLE;
2547
fdd508a6 2548 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2549
2550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2551 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2552
57779d06
VS
2553 switch (fb->pixel_format) {
2554 case DRM_FORMAT_C8:
17638cd6
JB
2555 dspcntr |= DISPPLANE_8BPP;
2556 break;
57779d06
VS
2557 case DRM_FORMAT_RGB565:
2558 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2559 break;
57779d06
VS
2560 case DRM_FORMAT_XRGB8888:
2561 case DRM_FORMAT_ARGB8888:
2562 dspcntr |= DISPPLANE_BGRX888;
2563 break;
2564 case DRM_FORMAT_XBGR8888:
2565 case DRM_FORMAT_ABGR8888:
2566 dspcntr |= DISPPLANE_RGBX888;
2567 break;
2568 case DRM_FORMAT_XRGB2101010:
2569 case DRM_FORMAT_ARGB2101010:
2570 dspcntr |= DISPPLANE_BGRX101010;
2571 break;
2572 case DRM_FORMAT_XBGR2101010:
2573 case DRM_FORMAT_ABGR2101010:
2574 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2575 break;
2576 default:
baba133a 2577 BUG();
17638cd6
JB
2578 }
2579
2580 if (obj->tiling_mode != I915_TILING_NONE)
2581 dspcntr |= DISPPLANE_TILED;
17638cd6 2582
f45651ba 2583 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2584 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2585
b9897127 2586 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2587 intel_crtc->dspaddr_offset =
bc752862 2588 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2589 pixel_size,
bc752862 2590 fb->pitches[0]);
c2c75131 2591 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2592 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2593 dspcntr |= DISPPLANE_ROTATE_180;
2594
2595 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2596 x += (intel_crtc->config.pipe_src_w - 1);
2597 y += (intel_crtc->config.pipe_src_h - 1);
2598
2599 /* Finding the last pixel of the last line of the display
2600 data and adding to linear_offset*/
2601 linear_offset +=
2602 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2603 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2604 }
2605 }
2606
2607 I915_WRITE(reg, dspcntr);
17638cd6 2608
f343c5f6
BW
2609 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2610 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2611 fb->pitches[0]);
01f2c773 2612 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2613 I915_WRITE(DSPSURF(plane),
2614 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2615 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2616 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2617 } else {
2618 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2619 I915_WRITE(DSPLINOFF(plane), linear_offset);
2620 }
17638cd6 2621 POSTING_READ(reg);
17638cd6
JB
2622}
2623
2624/* Assume fb object is pinned & idle & fenced and just update base pointers */
2625static int
2626intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2627 int x, int y, enum mode_set_atomic state)
2628{
2629 struct drm_device *dev = crtc->dev;
2630 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2631
6b8e6ed0
CW
2632 if (dev_priv->display.disable_fbc)
2633 dev_priv->display.disable_fbc(dev);
cc36513c 2634 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
81255565 2635
29b9bde6
DV
2636 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2637
2638 return 0;
81255565
JB
2639}
2640
96a02917
VS
2641void intel_display_handle_reset(struct drm_device *dev)
2642{
2643 struct drm_i915_private *dev_priv = dev->dev_private;
2644 struct drm_crtc *crtc;
2645
2646 /*
2647 * Flips in the rings have been nuked by the reset,
2648 * so complete all pending flips so that user space
2649 * will get its events and not get stuck.
2650 *
2651 * Also update the base address of all primary
2652 * planes to the the last fb to make sure we're
2653 * showing the correct fb after a reset.
2654 *
2655 * Need to make two loops over the crtcs so that we
2656 * don't try to grab a crtc mutex before the
2657 * pending_flip_queue really got woken up.
2658 */
2659
70e1e0ec 2660 for_each_crtc(dev, crtc) {
96a02917
VS
2661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2662 enum plane plane = intel_crtc->plane;
2663
2664 intel_prepare_page_flip(dev, plane);
2665 intel_finish_page_flip_plane(dev, plane);
2666 }
2667
70e1e0ec 2668 for_each_crtc(dev, crtc) {
96a02917
VS
2669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2670
51fd371b 2671 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2672 /*
2673 * FIXME: Once we have proper support for primary planes (and
2674 * disabling them without disabling the entire crtc) allow again
66e514c1 2675 * a NULL crtc->primary->fb.
947fdaad 2676 */
f4510a27 2677 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2678 dev_priv->display.update_primary_plane(crtc,
66e514c1 2679 crtc->primary->fb,
262ca2b0
MR
2680 crtc->x,
2681 crtc->y);
51fd371b 2682 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2683 }
2684}
2685
14667a4b
CW
2686static int
2687intel_finish_fb(struct drm_framebuffer *old_fb)
2688{
2ff8fde1 2689 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2690 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2691 bool was_interruptible = dev_priv->mm.interruptible;
2692 int ret;
2693
14667a4b
CW
2694 /* Big Hammer, we also need to ensure that any pending
2695 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2696 * current scanout is retired before unpinning the old
2697 * framebuffer.
2698 *
2699 * This should only fail upon a hung GPU, in which case we
2700 * can safely continue.
2701 */
2702 dev_priv->mm.interruptible = false;
2703 ret = i915_gem_object_finish_gpu(obj);
2704 dev_priv->mm.interruptible = was_interruptible;
2705
2706 return ret;
2707}
2708
7d5e3799
CW
2709static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2710{
2711 struct drm_device *dev = crtc->dev;
2712 struct drm_i915_private *dev_priv = dev->dev_private;
2713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2714 unsigned long flags;
2715 bool pending;
2716
2717 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2718 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2719 return false;
2720
2721 spin_lock_irqsave(&dev->event_lock, flags);
2722 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2723 spin_unlock_irqrestore(&dev->event_lock, flags);
2724
2725 return pending;
2726}
2727
e30e8f75
GP
2728static void intel_update_pipe_size(struct intel_crtc *crtc)
2729{
2730 struct drm_device *dev = crtc->base.dev;
2731 struct drm_i915_private *dev_priv = dev->dev_private;
2732 const struct drm_display_mode *adjusted_mode;
2733
2734 if (!i915.fastboot)
2735 return;
2736
2737 /*
2738 * Update pipe size and adjust fitter if needed: the reason for this is
2739 * that in compute_mode_changes we check the native mode (not the pfit
2740 * mode) to see if we can flip rather than do a full mode set. In the
2741 * fastboot case, we'll flip, but if we don't update the pipesrc and
2742 * pfit state, we'll end up with a big fb scanned out into the wrong
2743 * sized surface.
2744 *
2745 * To fix this properly, we need to hoist the checks up into
2746 * compute_mode_changes (or above), check the actual pfit state and
2747 * whether the platform allows pfit disable with pipe active, and only
2748 * then update the pipesrc and pfit state, even on the flip path.
2749 */
2750
2751 adjusted_mode = &crtc->config.adjusted_mode;
2752
2753 I915_WRITE(PIPESRC(crtc->pipe),
2754 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2755 (adjusted_mode->crtc_vdisplay - 1));
2756 if (!crtc->config.pch_pfit.enabled &&
2757 (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) ||
2758 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))) {
2759 I915_WRITE(PF_CTL(crtc->pipe), 0);
2760 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2761 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2762 }
2763 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2764 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2765}
2766
5c3b82e2 2767static int
3c4fdcfb 2768intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2769 struct drm_framebuffer *fb)
79e53945
JB
2770{
2771 struct drm_device *dev = crtc->dev;
6b8e6ed0 2772 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2774 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2775 struct drm_framebuffer *old_fb = crtc->primary->fb;
2776 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2777 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2778 int ret;
79e53945 2779
7d5e3799
CW
2780 if (intel_crtc_has_pending_flip(crtc)) {
2781 DRM_ERROR("pipe is still busy with an old pageflip\n");
2782 return -EBUSY;
2783 }
2784
79e53945 2785 /* no fb bound */
94352cf9 2786 if (!fb) {
a5071c2f 2787 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2788 return 0;
2789 }
2790
7eb552ae 2791 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2792 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2793 plane_name(intel_crtc->plane),
2794 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2795 return -EINVAL;
79e53945
JB
2796 }
2797
5c3b82e2 2798 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2799 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2800 if (ret == 0)
91565c85 2801 i915_gem_track_fb(old_obj, obj,
a071fa00 2802 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2803 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2804 if (ret != 0) {
a5071c2f 2805 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2806 return ret;
2807 }
79e53945 2808
e30e8f75 2809 intel_update_pipe_size(intel_crtc);
4d6a3e63 2810
29b9bde6 2811 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2812
f99d7069
DV
2813 if (intel_crtc->active)
2814 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2815
f4510a27 2816 crtc->primary->fb = fb;
6c4c86f5
DV
2817 crtc->x = x;
2818 crtc->y = y;
94352cf9 2819
b7f1de28 2820 if (old_fb) {
d7697eea
DV
2821 if (intel_crtc->active && old_fb != fb)
2822 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2823 mutex_lock(&dev->struct_mutex);
2ff8fde1 2824 intel_unpin_fb_obj(old_obj);
8ac36ec1 2825 mutex_unlock(&dev->struct_mutex);
b7f1de28 2826 }
652c393a 2827
8ac36ec1 2828 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2829 intel_update_fbc(dev);
5c3b82e2 2830 mutex_unlock(&dev->struct_mutex);
79e53945 2831
5c3b82e2 2832 return 0;
79e53945
JB
2833}
2834
5e84e1a4
ZW
2835static void intel_fdi_normal_train(struct drm_crtc *crtc)
2836{
2837 struct drm_device *dev = crtc->dev;
2838 struct drm_i915_private *dev_priv = dev->dev_private;
2839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2840 int pipe = intel_crtc->pipe;
2841 u32 reg, temp;
2842
2843 /* enable normal train */
2844 reg = FDI_TX_CTL(pipe);
2845 temp = I915_READ(reg);
61e499bf 2846 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2847 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2848 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2849 } else {
2850 temp &= ~FDI_LINK_TRAIN_NONE;
2851 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2852 }
5e84e1a4
ZW
2853 I915_WRITE(reg, temp);
2854
2855 reg = FDI_RX_CTL(pipe);
2856 temp = I915_READ(reg);
2857 if (HAS_PCH_CPT(dev)) {
2858 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2859 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2860 } else {
2861 temp &= ~FDI_LINK_TRAIN_NONE;
2862 temp |= FDI_LINK_TRAIN_NONE;
2863 }
2864 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2865
2866 /* wait one idle pattern time */
2867 POSTING_READ(reg);
2868 udelay(1000);
357555c0
JB
2869
2870 /* IVB wants error correction enabled */
2871 if (IS_IVYBRIDGE(dev))
2872 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2873 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2874}
2875
1fbc0d78 2876static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2877{
1fbc0d78
DV
2878 return crtc->base.enabled && crtc->active &&
2879 crtc->config.has_pch_encoder;
1e833f40
DV
2880}
2881
01a415fd
DV
2882static void ivb_modeset_global_resources(struct drm_device *dev)
2883{
2884 struct drm_i915_private *dev_priv = dev->dev_private;
2885 struct intel_crtc *pipe_B_crtc =
2886 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2887 struct intel_crtc *pipe_C_crtc =
2888 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2889 uint32_t temp;
2890
1e833f40
DV
2891 /*
2892 * When everything is off disable fdi C so that we could enable fdi B
2893 * with all lanes. Note that we don't care about enabled pipes without
2894 * an enabled pch encoder.
2895 */
2896 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2897 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2898 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2899 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2900
2901 temp = I915_READ(SOUTH_CHICKEN1);
2902 temp &= ~FDI_BC_BIFURCATION_SELECT;
2903 DRM_DEBUG_KMS("disabling fdi C rx\n");
2904 I915_WRITE(SOUTH_CHICKEN1, temp);
2905 }
2906}
2907
8db9d77b
ZW
2908/* The FDI link training functions for ILK/Ibexpeak. */
2909static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2910{
2911 struct drm_device *dev = crtc->dev;
2912 struct drm_i915_private *dev_priv = dev->dev_private;
2913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2914 int pipe = intel_crtc->pipe;
5eddb70b 2915 u32 reg, temp, tries;
8db9d77b 2916
1c8562f6 2917 /* FDI needs bits from pipe first */
0fc932b8 2918 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2919
e1a44743
AJ
2920 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2921 for train result */
5eddb70b
CW
2922 reg = FDI_RX_IMR(pipe);
2923 temp = I915_READ(reg);
e1a44743
AJ
2924 temp &= ~FDI_RX_SYMBOL_LOCK;
2925 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2926 I915_WRITE(reg, temp);
2927 I915_READ(reg);
e1a44743
AJ
2928 udelay(150);
2929
8db9d77b 2930 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2931 reg = FDI_TX_CTL(pipe);
2932 temp = I915_READ(reg);
627eb5a3
DV
2933 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2934 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2935 temp &= ~FDI_LINK_TRAIN_NONE;
2936 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2937 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2938
5eddb70b
CW
2939 reg = FDI_RX_CTL(pipe);
2940 temp = I915_READ(reg);
8db9d77b
ZW
2941 temp &= ~FDI_LINK_TRAIN_NONE;
2942 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2943 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2944
2945 POSTING_READ(reg);
8db9d77b
ZW
2946 udelay(150);
2947
5b2adf89 2948 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2949 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2950 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2951 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2952
5eddb70b 2953 reg = FDI_RX_IIR(pipe);
e1a44743 2954 for (tries = 0; tries < 5; tries++) {
5eddb70b 2955 temp = I915_READ(reg);
8db9d77b
ZW
2956 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2957
2958 if ((temp & FDI_RX_BIT_LOCK)) {
2959 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2960 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2961 break;
2962 }
8db9d77b 2963 }
e1a44743 2964 if (tries == 5)
5eddb70b 2965 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2966
2967 /* Train 2 */
5eddb70b
CW
2968 reg = FDI_TX_CTL(pipe);
2969 temp = I915_READ(reg);
8db9d77b
ZW
2970 temp &= ~FDI_LINK_TRAIN_NONE;
2971 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2972 I915_WRITE(reg, temp);
8db9d77b 2973
5eddb70b
CW
2974 reg = FDI_RX_CTL(pipe);
2975 temp = I915_READ(reg);
8db9d77b
ZW
2976 temp &= ~FDI_LINK_TRAIN_NONE;
2977 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2978 I915_WRITE(reg, temp);
8db9d77b 2979
5eddb70b
CW
2980 POSTING_READ(reg);
2981 udelay(150);
8db9d77b 2982
5eddb70b 2983 reg = FDI_RX_IIR(pipe);
e1a44743 2984 for (tries = 0; tries < 5; tries++) {
5eddb70b 2985 temp = I915_READ(reg);
8db9d77b
ZW
2986 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2987
2988 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2989 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2990 DRM_DEBUG_KMS("FDI train 2 done.\n");
2991 break;
2992 }
8db9d77b 2993 }
e1a44743 2994 if (tries == 5)
5eddb70b 2995 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2996
2997 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2998
8db9d77b
ZW
2999}
3000
0206e353 3001static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3002 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3003 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3004 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3005 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3006};
3007
3008/* The FDI link training functions for SNB/Cougarpoint. */
3009static void gen6_fdi_link_train(struct drm_crtc *crtc)
3010{
3011 struct drm_device *dev = crtc->dev;
3012 struct drm_i915_private *dev_priv = dev->dev_private;
3013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3014 int pipe = intel_crtc->pipe;
fa37d39e 3015 u32 reg, temp, i, retry;
8db9d77b 3016
e1a44743
AJ
3017 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3018 for train result */
5eddb70b
CW
3019 reg = FDI_RX_IMR(pipe);
3020 temp = I915_READ(reg);
e1a44743
AJ
3021 temp &= ~FDI_RX_SYMBOL_LOCK;
3022 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3023 I915_WRITE(reg, temp);
3024
3025 POSTING_READ(reg);
e1a44743
AJ
3026 udelay(150);
3027
8db9d77b 3028 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3029 reg = FDI_TX_CTL(pipe);
3030 temp = I915_READ(reg);
627eb5a3
DV
3031 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3032 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3033 temp &= ~FDI_LINK_TRAIN_NONE;
3034 temp |= FDI_LINK_TRAIN_PATTERN_1;
3035 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3036 /* SNB-B */
3037 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3038 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3039
d74cf324
DV
3040 I915_WRITE(FDI_RX_MISC(pipe),
3041 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3042
5eddb70b
CW
3043 reg = FDI_RX_CTL(pipe);
3044 temp = I915_READ(reg);
8db9d77b
ZW
3045 if (HAS_PCH_CPT(dev)) {
3046 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3047 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3048 } else {
3049 temp &= ~FDI_LINK_TRAIN_NONE;
3050 temp |= FDI_LINK_TRAIN_PATTERN_1;
3051 }
5eddb70b
CW
3052 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3053
3054 POSTING_READ(reg);
8db9d77b
ZW
3055 udelay(150);
3056
0206e353 3057 for (i = 0; i < 4; i++) {
5eddb70b
CW
3058 reg = FDI_TX_CTL(pipe);
3059 temp = I915_READ(reg);
8db9d77b
ZW
3060 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3061 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3062 I915_WRITE(reg, temp);
3063
3064 POSTING_READ(reg);
8db9d77b
ZW
3065 udelay(500);
3066
fa37d39e
SP
3067 for (retry = 0; retry < 5; retry++) {
3068 reg = FDI_RX_IIR(pipe);
3069 temp = I915_READ(reg);
3070 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3071 if (temp & FDI_RX_BIT_LOCK) {
3072 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3073 DRM_DEBUG_KMS("FDI train 1 done.\n");
3074 break;
3075 }
3076 udelay(50);
8db9d77b 3077 }
fa37d39e
SP
3078 if (retry < 5)
3079 break;
8db9d77b
ZW
3080 }
3081 if (i == 4)
5eddb70b 3082 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3083
3084 /* Train 2 */
5eddb70b
CW
3085 reg = FDI_TX_CTL(pipe);
3086 temp = I915_READ(reg);
8db9d77b
ZW
3087 temp &= ~FDI_LINK_TRAIN_NONE;
3088 temp |= FDI_LINK_TRAIN_PATTERN_2;
3089 if (IS_GEN6(dev)) {
3090 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3091 /* SNB-B */
3092 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3093 }
5eddb70b 3094 I915_WRITE(reg, temp);
8db9d77b 3095
5eddb70b
CW
3096 reg = FDI_RX_CTL(pipe);
3097 temp = I915_READ(reg);
8db9d77b
ZW
3098 if (HAS_PCH_CPT(dev)) {
3099 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3100 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3101 } else {
3102 temp &= ~FDI_LINK_TRAIN_NONE;
3103 temp |= FDI_LINK_TRAIN_PATTERN_2;
3104 }
5eddb70b
CW
3105 I915_WRITE(reg, temp);
3106
3107 POSTING_READ(reg);
8db9d77b
ZW
3108 udelay(150);
3109
0206e353 3110 for (i = 0; i < 4; i++) {
5eddb70b
CW
3111 reg = FDI_TX_CTL(pipe);
3112 temp = I915_READ(reg);
8db9d77b
ZW
3113 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3114 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3115 I915_WRITE(reg, temp);
3116
3117 POSTING_READ(reg);
8db9d77b
ZW
3118 udelay(500);
3119
fa37d39e
SP
3120 for (retry = 0; retry < 5; retry++) {
3121 reg = FDI_RX_IIR(pipe);
3122 temp = I915_READ(reg);
3123 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3124 if (temp & FDI_RX_SYMBOL_LOCK) {
3125 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3126 DRM_DEBUG_KMS("FDI train 2 done.\n");
3127 break;
3128 }
3129 udelay(50);
8db9d77b 3130 }
fa37d39e
SP
3131 if (retry < 5)
3132 break;
8db9d77b
ZW
3133 }
3134 if (i == 4)
5eddb70b 3135 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3136
3137 DRM_DEBUG_KMS("FDI train done.\n");
3138}
3139
357555c0
JB
3140/* Manual link training for Ivy Bridge A0 parts */
3141static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3142{
3143 struct drm_device *dev = crtc->dev;
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3146 int pipe = intel_crtc->pipe;
139ccd3f 3147 u32 reg, temp, i, j;
357555c0
JB
3148
3149 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3150 for train result */
3151 reg = FDI_RX_IMR(pipe);
3152 temp = I915_READ(reg);
3153 temp &= ~FDI_RX_SYMBOL_LOCK;
3154 temp &= ~FDI_RX_BIT_LOCK;
3155 I915_WRITE(reg, temp);
3156
3157 POSTING_READ(reg);
3158 udelay(150);
3159
01a415fd
DV
3160 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3161 I915_READ(FDI_RX_IIR(pipe)));
3162
139ccd3f
JB
3163 /* Try each vswing and preemphasis setting twice before moving on */
3164 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3165 /* disable first in case we need to retry */
3166 reg = FDI_TX_CTL(pipe);
3167 temp = I915_READ(reg);
3168 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3169 temp &= ~FDI_TX_ENABLE;
3170 I915_WRITE(reg, temp);
357555c0 3171
139ccd3f
JB
3172 reg = FDI_RX_CTL(pipe);
3173 temp = I915_READ(reg);
3174 temp &= ~FDI_LINK_TRAIN_AUTO;
3175 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3176 temp &= ~FDI_RX_ENABLE;
3177 I915_WRITE(reg, temp);
357555c0 3178
139ccd3f 3179 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3180 reg = FDI_TX_CTL(pipe);
3181 temp = I915_READ(reg);
139ccd3f
JB
3182 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3183 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3184 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3185 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3186 temp |= snb_b_fdi_train_param[j/2];
3187 temp |= FDI_COMPOSITE_SYNC;
3188 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3189
139ccd3f
JB
3190 I915_WRITE(FDI_RX_MISC(pipe),
3191 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3192
139ccd3f 3193 reg = FDI_RX_CTL(pipe);
357555c0 3194 temp = I915_READ(reg);
139ccd3f
JB
3195 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3196 temp |= FDI_COMPOSITE_SYNC;
3197 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3198
139ccd3f
JB
3199 POSTING_READ(reg);
3200 udelay(1); /* should be 0.5us */
357555c0 3201
139ccd3f
JB
3202 for (i = 0; i < 4; i++) {
3203 reg = FDI_RX_IIR(pipe);
3204 temp = I915_READ(reg);
3205 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3206
139ccd3f
JB
3207 if (temp & FDI_RX_BIT_LOCK ||
3208 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3209 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3210 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3211 i);
3212 break;
3213 }
3214 udelay(1); /* should be 0.5us */
3215 }
3216 if (i == 4) {
3217 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3218 continue;
3219 }
357555c0 3220
139ccd3f 3221 /* Train 2 */
357555c0
JB
3222 reg = FDI_TX_CTL(pipe);
3223 temp = I915_READ(reg);
139ccd3f
JB
3224 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3225 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3226 I915_WRITE(reg, temp);
3227
3228 reg = FDI_RX_CTL(pipe);
3229 temp = I915_READ(reg);
3230 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3231 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3232 I915_WRITE(reg, temp);
3233
3234 POSTING_READ(reg);
139ccd3f 3235 udelay(2); /* should be 1.5us */
357555c0 3236
139ccd3f
JB
3237 for (i = 0; i < 4; i++) {
3238 reg = FDI_RX_IIR(pipe);
3239 temp = I915_READ(reg);
3240 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3241
139ccd3f
JB
3242 if (temp & FDI_RX_SYMBOL_LOCK ||
3243 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3244 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3245 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3246 i);
3247 goto train_done;
3248 }
3249 udelay(2); /* should be 1.5us */
357555c0 3250 }
139ccd3f
JB
3251 if (i == 4)
3252 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3253 }
357555c0 3254
139ccd3f 3255train_done:
357555c0
JB
3256 DRM_DEBUG_KMS("FDI train done.\n");
3257}
3258
88cefb6c 3259static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3260{
88cefb6c 3261 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3262 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3263 int pipe = intel_crtc->pipe;
5eddb70b 3264 u32 reg, temp;
79e53945 3265
c64e311e 3266
c98e9dcf 3267 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3268 reg = FDI_RX_CTL(pipe);
3269 temp = I915_READ(reg);
627eb5a3
DV
3270 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3271 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3272 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3273 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3274
3275 POSTING_READ(reg);
c98e9dcf
JB
3276 udelay(200);
3277
3278 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3279 temp = I915_READ(reg);
3280 I915_WRITE(reg, temp | FDI_PCDCLK);
3281
3282 POSTING_READ(reg);
c98e9dcf
JB
3283 udelay(200);
3284
20749730
PZ
3285 /* Enable CPU FDI TX PLL, always on for Ironlake */
3286 reg = FDI_TX_CTL(pipe);
3287 temp = I915_READ(reg);
3288 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3289 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3290
20749730
PZ
3291 POSTING_READ(reg);
3292 udelay(100);
6be4a607 3293 }
0e23b99d
JB
3294}
3295
88cefb6c
DV
3296static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3297{
3298 struct drm_device *dev = intel_crtc->base.dev;
3299 struct drm_i915_private *dev_priv = dev->dev_private;
3300 int pipe = intel_crtc->pipe;
3301 u32 reg, temp;
3302
3303 /* Switch from PCDclk to Rawclk */
3304 reg = FDI_RX_CTL(pipe);
3305 temp = I915_READ(reg);
3306 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3307
3308 /* Disable CPU FDI TX PLL */
3309 reg = FDI_TX_CTL(pipe);
3310 temp = I915_READ(reg);
3311 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3312
3313 POSTING_READ(reg);
3314 udelay(100);
3315
3316 reg = FDI_RX_CTL(pipe);
3317 temp = I915_READ(reg);
3318 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3319
3320 /* Wait for the clocks to turn off. */
3321 POSTING_READ(reg);
3322 udelay(100);
3323}
3324
0fc932b8
JB
3325static void ironlake_fdi_disable(struct drm_crtc *crtc)
3326{
3327 struct drm_device *dev = crtc->dev;
3328 struct drm_i915_private *dev_priv = dev->dev_private;
3329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3330 int pipe = intel_crtc->pipe;
3331 u32 reg, temp;
3332
3333 /* disable CPU FDI tx and PCH FDI rx */
3334 reg = FDI_TX_CTL(pipe);
3335 temp = I915_READ(reg);
3336 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3337 POSTING_READ(reg);
3338
3339 reg = FDI_RX_CTL(pipe);
3340 temp = I915_READ(reg);
3341 temp &= ~(0x7 << 16);
dfd07d72 3342 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3343 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3344
3345 POSTING_READ(reg);
3346 udelay(100);
3347
3348 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3349 if (HAS_PCH_IBX(dev))
6f06ce18 3350 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3351
3352 /* still set train pattern 1 */
3353 reg = FDI_TX_CTL(pipe);
3354 temp = I915_READ(reg);
3355 temp &= ~FDI_LINK_TRAIN_NONE;
3356 temp |= FDI_LINK_TRAIN_PATTERN_1;
3357 I915_WRITE(reg, temp);
3358
3359 reg = FDI_RX_CTL(pipe);
3360 temp = I915_READ(reg);
3361 if (HAS_PCH_CPT(dev)) {
3362 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3363 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3364 } else {
3365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_1;
3367 }
3368 /* BPC in FDI rx is consistent with that in PIPECONF */
3369 temp &= ~(0x07 << 16);
dfd07d72 3370 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3371 I915_WRITE(reg, temp);
3372
3373 POSTING_READ(reg);
3374 udelay(100);
3375}
3376
5dce5b93
CW
3377bool intel_has_pending_fb_unpin(struct drm_device *dev)
3378{
3379 struct intel_crtc *crtc;
3380
3381 /* Note that we don't need to be called with mode_config.lock here
3382 * as our list of CRTC objects is static for the lifetime of the
3383 * device and so cannot disappear as we iterate. Similarly, we can
3384 * happily treat the predicates as racy, atomic checks as userspace
3385 * cannot claim and pin a new fb without at least acquring the
3386 * struct_mutex and so serialising with us.
3387 */
d3fcc808 3388 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3389 if (atomic_read(&crtc->unpin_work_count) == 0)
3390 continue;
3391
3392 if (crtc->unpin_work)
3393 intel_wait_for_vblank(dev, crtc->pipe);
3394
3395 return true;
3396 }
3397
3398 return false;
3399}
3400
d6bbafa1
CW
3401static void page_flip_completed(struct intel_crtc *intel_crtc)
3402{
3403 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3404 struct intel_unpin_work *work = intel_crtc->unpin_work;
3405
3406 /* ensure that the unpin work is consistent wrt ->pending. */
3407 smp_rmb();
3408 intel_crtc->unpin_work = NULL;
3409
3410 if (work->event)
3411 drm_send_vblank_event(intel_crtc->base.dev,
3412 intel_crtc->pipe,
3413 work->event);
3414
3415 drm_crtc_vblank_put(&intel_crtc->base);
3416
3417 wake_up_all(&dev_priv->pending_flip_queue);
3418 queue_work(dev_priv->wq, &work->work);
3419
3420 trace_i915_flip_complete(intel_crtc->plane,
3421 work->pending_flip_obj);
3422}
3423
46a55d30 3424void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3425{
0f91128d 3426 struct drm_device *dev = crtc->dev;
5bb61643 3427 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3428
2c10d571 3429 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3430 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3431 !intel_crtc_has_pending_flip(crtc),
3432 60*HZ) == 0)) {
3433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3434 unsigned long flags;
2c10d571 3435
9c787942
CW
3436 spin_lock_irqsave(&dev->event_lock, flags);
3437 if (intel_crtc->unpin_work) {
3438 WARN_ONCE(1, "Removing stuck page flip\n");
3439 page_flip_completed(intel_crtc);
3440 }
3441 spin_unlock_irqrestore(&dev->event_lock, flags);
3442 }
5bb61643 3443
975d568a
CW
3444 if (crtc->primary->fb) {
3445 mutex_lock(&dev->struct_mutex);
3446 intel_finish_fb(crtc->primary->fb);
3447 mutex_unlock(&dev->struct_mutex);
3448 }
e6c3a2a6
CW
3449}
3450
e615efe4
ED
3451/* Program iCLKIP clock to the desired frequency */
3452static void lpt_program_iclkip(struct drm_crtc *crtc)
3453{
3454 struct drm_device *dev = crtc->dev;
3455 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3456 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3457 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3458 u32 temp;
3459
09153000
DV
3460 mutex_lock(&dev_priv->dpio_lock);
3461
e615efe4
ED
3462 /* It is necessary to ungate the pixclk gate prior to programming
3463 * the divisors, and gate it back when it is done.
3464 */
3465 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3466
3467 /* Disable SSCCTL */
3468 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3469 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3470 SBI_SSCCTL_DISABLE,
3471 SBI_ICLK);
e615efe4
ED
3472
3473 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3474 if (clock == 20000) {
e615efe4
ED
3475 auxdiv = 1;
3476 divsel = 0x41;
3477 phaseinc = 0x20;
3478 } else {
3479 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3480 * but the adjusted_mode->crtc_clock in in KHz. To get the
3481 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3482 * convert the virtual clock precision to KHz here for higher
3483 * precision.
3484 */
3485 u32 iclk_virtual_root_freq = 172800 * 1000;
3486 u32 iclk_pi_range = 64;
3487 u32 desired_divisor, msb_divisor_value, pi_value;
3488
12d7ceed 3489 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3490 msb_divisor_value = desired_divisor / iclk_pi_range;
3491 pi_value = desired_divisor % iclk_pi_range;
3492
3493 auxdiv = 0;
3494 divsel = msb_divisor_value - 2;
3495 phaseinc = pi_value;
3496 }
3497
3498 /* This should not happen with any sane values */
3499 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3500 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3501 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3502 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3503
3504 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3505 clock,
e615efe4
ED
3506 auxdiv,
3507 divsel,
3508 phasedir,
3509 phaseinc);
3510
3511 /* Program SSCDIVINTPHASE6 */
988d6ee8 3512 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3513 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3514 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3515 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3516 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3517 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3518 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3519 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3520
3521 /* Program SSCAUXDIV */
988d6ee8 3522 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3523 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3524 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3525 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3526
3527 /* Enable modulator and associated divider */
988d6ee8 3528 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3529 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3530 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3531
3532 /* Wait for initialization time */
3533 udelay(24);
3534
3535 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3536
3537 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3538}
3539
275f01b2
DV
3540static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3541 enum pipe pch_transcoder)
3542{
3543 struct drm_device *dev = crtc->base.dev;
3544 struct drm_i915_private *dev_priv = dev->dev_private;
3545 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3546
3547 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3548 I915_READ(HTOTAL(cpu_transcoder)));
3549 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3550 I915_READ(HBLANK(cpu_transcoder)));
3551 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3552 I915_READ(HSYNC(cpu_transcoder)));
3553
3554 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3555 I915_READ(VTOTAL(cpu_transcoder)));
3556 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3557 I915_READ(VBLANK(cpu_transcoder)));
3558 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3559 I915_READ(VSYNC(cpu_transcoder)));
3560 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3561 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3562}
3563
1fbc0d78
DV
3564static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3565{
3566 struct drm_i915_private *dev_priv = dev->dev_private;
3567 uint32_t temp;
3568
3569 temp = I915_READ(SOUTH_CHICKEN1);
3570 if (temp & FDI_BC_BIFURCATION_SELECT)
3571 return;
3572
3573 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3574 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3575
3576 temp |= FDI_BC_BIFURCATION_SELECT;
3577 DRM_DEBUG_KMS("enabling fdi C rx\n");
3578 I915_WRITE(SOUTH_CHICKEN1, temp);
3579 POSTING_READ(SOUTH_CHICKEN1);
3580}
3581
3582static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3583{
3584 struct drm_device *dev = intel_crtc->base.dev;
3585 struct drm_i915_private *dev_priv = dev->dev_private;
3586
3587 switch (intel_crtc->pipe) {
3588 case PIPE_A:
3589 break;
3590 case PIPE_B:
3591 if (intel_crtc->config.fdi_lanes > 2)
3592 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3593 else
3594 cpt_enable_fdi_bc_bifurcation(dev);
3595
3596 break;
3597 case PIPE_C:
3598 cpt_enable_fdi_bc_bifurcation(dev);
3599
3600 break;
3601 default:
3602 BUG();
3603 }
3604}
3605
f67a559d
JB
3606/*
3607 * Enable PCH resources required for PCH ports:
3608 * - PCH PLLs
3609 * - FDI training & RX/TX
3610 * - update transcoder timings
3611 * - DP transcoding bits
3612 * - transcoder
3613 */
3614static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3615{
3616 struct drm_device *dev = crtc->dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3619 int pipe = intel_crtc->pipe;
ee7b9f93 3620 u32 reg, temp;
2c07245f 3621
ab9412ba 3622 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3623
1fbc0d78
DV
3624 if (IS_IVYBRIDGE(dev))
3625 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3626
cd986abb
DV
3627 /* Write the TU size bits before fdi link training, so that error
3628 * detection works. */
3629 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3630 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3631
c98e9dcf 3632 /* For PCH output, training FDI link */
674cf967 3633 dev_priv->display.fdi_link_train(crtc);
2c07245f 3634
3ad8a208
DV
3635 /* We need to program the right clock selection before writing the pixel
3636 * mutliplier into the DPLL. */
303b81e0 3637 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3638 u32 sel;
4b645f14 3639
c98e9dcf 3640 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3641 temp |= TRANS_DPLL_ENABLE(pipe);
3642 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3643 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3644 temp |= sel;
3645 else
3646 temp &= ~sel;
c98e9dcf 3647 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3648 }
5eddb70b 3649
3ad8a208
DV
3650 /* XXX: pch pll's can be enabled any time before we enable the PCH
3651 * transcoder, and we actually should do this to not upset any PCH
3652 * transcoder that already use the clock when we share it.
3653 *
3654 * Note that enable_shared_dpll tries to do the right thing, but
3655 * get_shared_dpll unconditionally resets the pll - we need that to have
3656 * the right LVDS enable sequence. */
85b3894f 3657 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3658
d9b6cb56
JB
3659 /* set transcoder timing, panel must allow it */
3660 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3661 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3662
303b81e0 3663 intel_fdi_normal_train(crtc);
5e84e1a4 3664
c98e9dcf
JB
3665 /* For PCH DP, enable TRANS_DP_CTL */
3666 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3667 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3668 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3669 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3670 reg = TRANS_DP_CTL(pipe);
3671 temp = I915_READ(reg);
3672 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3673 TRANS_DP_SYNC_MASK |
3674 TRANS_DP_BPC_MASK);
5eddb70b
CW
3675 temp |= (TRANS_DP_OUTPUT_ENABLE |
3676 TRANS_DP_ENH_FRAMING);
9325c9f0 3677 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3678
3679 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3680 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3681 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3682 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3683
3684 switch (intel_trans_dp_port_sel(crtc)) {
3685 case PCH_DP_B:
5eddb70b 3686 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3687 break;
3688 case PCH_DP_C:
5eddb70b 3689 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3690 break;
3691 case PCH_DP_D:
5eddb70b 3692 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3693 break;
3694 default:
e95d41e1 3695 BUG();
32f9d658 3696 }
2c07245f 3697
5eddb70b 3698 I915_WRITE(reg, temp);
6be4a607 3699 }
b52eb4dc 3700
b8a4f404 3701 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3702}
3703
1507e5bd
PZ
3704static void lpt_pch_enable(struct drm_crtc *crtc)
3705{
3706 struct drm_device *dev = crtc->dev;
3707 struct drm_i915_private *dev_priv = dev->dev_private;
3708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3709 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3710
ab9412ba 3711 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3712
8c52b5e8 3713 lpt_program_iclkip(crtc);
1507e5bd 3714
0540e488 3715 /* Set transcoder timing. */
275f01b2 3716 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3717
937bb610 3718 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3719}
3720
716c2e55 3721void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3722{
e2b78267 3723 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3724
3725 if (pll == NULL)
3726 return;
3727
3728 if (pll->refcount == 0) {
46edb027 3729 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3730 return;
3731 }
3732
f4a091c7
DV
3733 if (--pll->refcount == 0) {
3734 WARN_ON(pll->on);
3735 WARN_ON(pll->active);
3736 }
3737
a43f6e0f 3738 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3739}
3740
716c2e55 3741struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3742{
e2b78267
DV
3743 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3744 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3745 enum intel_dpll_id i;
ee7b9f93 3746
ee7b9f93 3747 if (pll) {
46edb027
DV
3748 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3749 crtc->base.base.id, pll->name);
e2b78267 3750 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3751 }
3752
98b6bd99
DV
3753 if (HAS_PCH_IBX(dev_priv->dev)) {
3754 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3755 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3756 pll = &dev_priv->shared_dplls[i];
98b6bd99 3757
46edb027
DV
3758 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3759 crtc->base.base.id, pll->name);
98b6bd99 3760
f2a69f44
DV
3761 WARN_ON(pll->refcount);
3762
98b6bd99
DV
3763 goto found;
3764 }
3765
e72f9fbf
DV
3766 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3767 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3768
3769 /* Only want to check enabled timings first */
3770 if (pll->refcount == 0)
3771 continue;
3772
b89a1d39
DV
3773 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3774 sizeof(pll->hw_state)) == 0) {
46edb027 3775 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3776 crtc->base.base.id,
46edb027 3777 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3778
3779 goto found;
3780 }
3781 }
3782
3783 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3784 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3785 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3786 if (pll->refcount == 0) {
46edb027
DV
3787 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3788 crtc->base.base.id, pll->name);
ee7b9f93
JB
3789 goto found;
3790 }
3791 }
3792
3793 return NULL;
3794
3795found:
f2a69f44
DV
3796 if (pll->refcount == 0)
3797 pll->hw_state = crtc->config.dpll_hw_state;
3798
a43f6e0f 3799 crtc->config.shared_dpll = i;
46edb027
DV
3800 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3801 pipe_name(crtc->pipe));
ee7b9f93 3802
cdbd2316 3803 pll->refcount++;
e04c7350 3804
ee7b9f93
JB
3805 return pll;
3806}
3807
a1520318 3808static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3809{
3810 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3811 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3812 u32 temp;
3813
3814 temp = I915_READ(dslreg);
3815 udelay(500);
3816 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3817 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3818 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3819 }
3820}
3821
b074cec8
JB
3822static void ironlake_pfit_enable(struct intel_crtc *crtc)
3823{
3824 struct drm_device *dev = crtc->base.dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 int pipe = crtc->pipe;
3827
fd4daa9c 3828 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3829 /* Force use of hard-coded filter coefficients
3830 * as some pre-programmed values are broken,
3831 * e.g. x201.
3832 */
3833 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3834 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3835 PF_PIPE_SEL_IVB(pipe));
3836 else
3837 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3838 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3839 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3840 }
3841}
3842
bb53d4ae
VS
3843static void intel_enable_planes(struct drm_crtc *crtc)
3844{
3845 struct drm_device *dev = crtc->dev;
3846 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3847 struct drm_plane *plane;
bb53d4ae
VS
3848 struct intel_plane *intel_plane;
3849
af2b653b
MR
3850 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3851 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3852 if (intel_plane->pipe == pipe)
3853 intel_plane_restore(&intel_plane->base);
af2b653b 3854 }
bb53d4ae
VS
3855}
3856
3857static void intel_disable_planes(struct drm_crtc *crtc)
3858{
3859 struct drm_device *dev = crtc->dev;
3860 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3861 struct drm_plane *plane;
bb53d4ae
VS
3862 struct intel_plane *intel_plane;
3863
af2b653b
MR
3864 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3865 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3866 if (intel_plane->pipe == pipe)
3867 intel_plane_disable(&intel_plane->base);
af2b653b 3868 }
bb53d4ae
VS
3869}
3870
20bc8673 3871void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3872{
cea165c3
VS
3873 struct drm_device *dev = crtc->base.dev;
3874 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3875
3876 if (!crtc->config.ips_enabled)
3877 return;
3878
cea165c3
VS
3879 /* We can only enable IPS after we enable a plane and wait for a vblank */
3880 intel_wait_for_vblank(dev, crtc->pipe);
3881
d77e4531 3882 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3883 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3884 mutex_lock(&dev_priv->rps.hw_lock);
3885 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3886 mutex_unlock(&dev_priv->rps.hw_lock);
3887 /* Quoting Art Runyan: "its not safe to expect any particular
3888 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3889 * mailbox." Moreover, the mailbox may return a bogus state,
3890 * so we need to just enable it and continue on.
2a114cc1
BW
3891 */
3892 } else {
3893 I915_WRITE(IPS_CTL, IPS_ENABLE);
3894 /* The bit only becomes 1 in the next vblank, so this wait here
3895 * is essentially intel_wait_for_vblank. If we don't have this
3896 * and don't wait for vblanks until the end of crtc_enable, then
3897 * the HW state readout code will complain that the expected
3898 * IPS_CTL value is not the one we read. */
3899 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3900 DRM_ERROR("Timed out waiting for IPS enable\n");
3901 }
d77e4531
PZ
3902}
3903
20bc8673 3904void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3905{
3906 struct drm_device *dev = crtc->base.dev;
3907 struct drm_i915_private *dev_priv = dev->dev_private;
3908
3909 if (!crtc->config.ips_enabled)
3910 return;
3911
3912 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3913 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3914 mutex_lock(&dev_priv->rps.hw_lock);
3915 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3916 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3917 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3918 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3919 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3920 } else {
2a114cc1 3921 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3922 POSTING_READ(IPS_CTL);
3923 }
d77e4531
PZ
3924
3925 /* We need to wait for a vblank before we can disable the plane. */
3926 intel_wait_for_vblank(dev, crtc->pipe);
3927}
3928
3929/** Loads the palette/gamma unit for the CRTC with the prepared values */
3930static void intel_crtc_load_lut(struct drm_crtc *crtc)
3931{
3932 struct drm_device *dev = crtc->dev;
3933 struct drm_i915_private *dev_priv = dev->dev_private;
3934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3935 enum pipe pipe = intel_crtc->pipe;
3936 int palreg = PALETTE(pipe);
3937 int i;
3938 bool reenable_ips = false;
3939
3940 /* The clocks have to be on to load the palette. */
3941 if (!crtc->enabled || !intel_crtc->active)
3942 return;
3943
3944 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3945 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3946 assert_dsi_pll_enabled(dev_priv);
3947 else
3948 assert_pll_enabled(dev_priv, pipe);
3949 }
3950
3951 /* use legacy palette for Ironlake */
7a1db49a 3952 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
3953 palreg = LGC_PALETTE(pipe);
3954
3955 /* Workaround : Do not read or write the pipe palette/gamma data while
3956 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3957 */
41e6fc4c 3958 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3959 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3960 GAMMA_MODE_MODE_SPLIT)) {
3961 hsw_disable_ips(intel_crtc);
3962 reenable_ips = true;
3963 }
3964
3965 for (i = 0; i < 256; i++) {
3966 I915_WRITE(palreg + 4 * i,
3967 (intel_crtc->lut_r[i] << 16) |
3968 (intel_crtc->lut_g[i] << 8) |
3969 intel_crtc->lut_b[i]);
3970 }
3971
3972 if (reenable_ips)
3973 hsw_enable_ips(intel_crtc);
3974}
3975
d3eedb1a
VS
3976static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3977{
3978 if (!enable && intel_crtc->overlay) {
3979 struct drm_device *dev = intel_crtc->base.dev;
3980 struct drm_i915_private *dev_priv = dev->dev_private;
3981
3982 mutex_lock(&dev->struct_mutex);
3983 dev_priv->mm.interruptible = false;
3984 (void) intel_overlay_switch_off(intel_crtc->overlay);
3985 dev_priv->mm.interruptible = true;
3986 mutex_unlock(&dev->struct_mutex);
3987 }
3988
3989 /* Let userspace switch the overlay on again. In most cases userspace
3990 * has to recompute where to put it anyway.
3991 */
3992}
3993
d3eedb1a 3994static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3995{
3996 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
3997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3998 int pipe = intel_crtc->pipe;
a5c4d7bc 3999
08c71e5e
VS
4000 assert_vblank_disabled(crtc);
4001
f98551ae
VS
4002 drm_vblank_on(dev, pipe);
4003
fdd508a6 4004 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
4005 intel_enable_planes(crtc);
4006 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4007 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4008
4009 hsw_enable_ips(intel_crtc);
4010
4011 mutex_lock(&dev->struct_mutex);
4012 intel_update_fbc(dev);
4013 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4014
4015 /*
4016 * FIXME: Once we grow proper nuclear flip support out of this we need
4017 * to compute the mask of flip planes precisely. For the time being
4018 * consider this a flip from a NULL plane.
4019 */
4020 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4021}
4022
d3eedb1a 4023static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4024{
4025 struct drm_device *dev = crtc->dev;
4026 struct drm_i915_private *dev_priv = dev->dev_private;
4027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4028 int pipe = intel_crtc->pipe;
4029 int plane = intel_crtc->plane;
4030
4031 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4032
4033 if (dev_priv->fbc.plane == plane)
4034 intel_disable_fbc(dev);
4035
4036 hsw_disable_ips(intel_crtc);
4037
d3eedb1a 4038 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
4039 intel_crtc_update_cursor(crtc, false);
4040 intel_disable_planes(crtc);
fdd508a6 4041 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4042
f99d7069
DV
4043 /*
4044 * FIXME: Once we grow proper nuclear flip support out of this we need
4045 * to compute the mask of flip planes precisely. For the time being
4046 * consider this a flip to a NULL plane.
4047 */
4048 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4049
f98551ae 4050 drm_vblank_off(dev, pipe);
08c71e5e
VS
4051
4052 assert_vblank_disabled(crtc);
a5c4d7bc
VS
4053}
4054
f67a559d
JB
4055static void ironlake_crtc_enable(struct drm_crtc *crtc)
4056{
4057 struct drm_device *dev = crtc->dev;
4058 struct drm_i915_private *dev_priv = dev->dev_private;
4059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4060 struct intel_encoder *encoder;
f67a559d 4061 int pipe = intel_crtc->pipe;
f67a559d 4062
08a48469
DV
4063 WARN_ON(!crtc->enabled);
4064
f67a559d
JB
4065 if (intel_crtc->active)
4066 return;
4067
b14b1055
DV
4068 if (intel_crtc->config.has_pch_encoder)
4069 intel_prepare_shared_dpll(intel_crtc);
4070
29407aab
DV
4071 if (intel_crtc->config.has_dp_encoder)
4072 intel_dp_set_m_n(intel_crtc);
4073
4074 intel_set_pipe_timings(intel_crtc);
4075
4076 if (intel_crtc->config.has_pch_encoder) {
4077 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4078 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4079 }
4080
4081 ironlake_set_pipeconf(crtc);
4082
f67a559d 4083 intel_crtc->active = true;
8664281b
PZ
4084
4085 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4086 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4087
f6736a1a 4088 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4089 if (encoder->pre_enable)
4090 encoder->pre_enable(encoder);
f67a559d 4091
5bfe2ac0 4092 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4093 /* Note: FDI PLL enabling _must_ be done before we enable the
4094 * cpu pipes, hence this is separate from all the other fdi/pch
4095 * enabling. */
88cefb6c 4096 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4097 } else {
4098 assert_fdi_tx_disabled(dev_priv, pipe);
4099 assert_fdi_rx_disabled(dev_priv, pipe);
4100 }
f67a559d 4101
b074cec8 4102 ironlake_pfit_enable(intel_crtc);
f67a559d 4103
9c54c0dd
JB
4104 /*
4105 * On ILK+ LUT must be loaded before the pipe is running but with
4106 * clocks enabled
4107 */
4108 intel_crtc_load_lut(crtc);
4109
f37fcc2a 4110 intel_update_watermarks(crtc);
e1fdc473 4111 intel_enable_pipe(intel_crtc);
f67a559d 4112
5bfe2ac0 4113 if (intel_crtc->config.has_pch_encoder)
f67a559d 4114 ironlake_pch_enable(crtc);
c98e9dcf 4115
fa5c73b1
DV
4116 for_each_encoder_on_crtc(dev, crtc, encoder)
4117 encoder->enable(encoder);
61b77ddd
DV
4118
4119 if (HAS_PCH_CPT(dev))
a1520318 4120 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4121
d3eedb1a 4122 intel_crtc_enable_planes(crtc);
6be4a607
JB
4123}
4124
42db64ef
PZ
4125/* IPS only exists on ULT machines and is tied to pipe A. */
4126static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4127{
f5adf94e 4128 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4129}
4130
e4916946
PZ
4131/*
4132 * This implements the workaround described in the "notes" section of the mode
4133 * set sequence documentation. When going from no pipes or single pipe to
4134 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4135 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4136 */
4137static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4138{
4139 struct drm_device *dev = crtc->base.dev;
4140 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4141
4142 /* We want to get the other_active_crtc only if there's only 1 other
4143 * active crtc. */
d3fcc808 4144 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4145 if (!crtc_it->active || crtc_it == crtc)
4146 continue;
4147
4148 if (other_active_crtc)
4149 return;
4150
4151 other_active_crtc = crtc_it;
4152 }
4153 if (!other_active_crtc)
4154 return;
4155
4156 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4157 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4158}
4159
4f771f10
PZ
4160static void haswell_crtc_enable(struct drm_crtc *crtc)
4161{
4162 struct drm_device *dev = crtc->dev;
4163 struct drm_i915_private *dev_priv = dev->dev_private;
4164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4165 struct intel_encoder *encoder;
4166 int pipe = intel_crtc->pipe;
4f771f10
PZ
4167
4168 WARN_ON(!crtc->enabled);
4169
4170 if (intel_crtc->active)
4171 return;
4172
df8ad70c
DV
4173 if (intel_crtc_to_shared_dpll(intel_crtc))
4174 intel_enable_shared_dpll(intel_crtc);
4175
229fca97
DV
4176 if (intel_crtc->config.has_dp_encoder)
4177 intel_dp_set_m_n(intel_crtc);
4178
4179 intel_set_pipe_timings(intel_crtc);
4180
4181 if (intel_crtc->config.has_pch_encoder) {
4182 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4183 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4184 }
4185
4186 haswell_set_pipeconf(crtc);
4187
4188 intel_set_pipe_csc(crtc);
4189
4f771f10 4190 intel_crtc->active = true;
8664281b
PZ
4191
4192 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4f771f10
PZ
4193 for_each_encoder_on_crtc(dev, crtc, encoder)
4194 if (encoder->pre_enable)
4195 encoder->pre_enable(encoder);
4196
4fe9467d
ID
4197 if (intel_crtc->config.has_pch_encoder) {
4198 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4199 dev_priv->display.fdi_link_train(crtc);
4200 }
4201
1f544388 4202 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4203
b074cec8 4204 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4205
4206 /*
4207 * On ILK+ LUT must be loaded before the pipe is running but with
4208 * clocks enabled
4209 */
4210 intel_crtc_load_lut(crtc);
4211
1f544388 4212 intel_ddi_set_pipe_settings(crtc);
8228c251 4213 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4214
f37fcc2a 4215 intel_update_watermarks(crtc);
e1fdc473 4216 intel_enable_pipe(intel_crtc);
42db64ef 4217
5bfe2ac0 4218 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4219 lpt_pch_enable(crtc);
4f771f10 4220
0e32b39c
DA
4221 if (intel_crtc->config.dp_encoder_is_mst)
4222 intel_ddi_set_vc_payload_alloc(crtc, true);
4223
8807e55b 4224 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4225 encoder->enable(encoder);
8807e55b
JN
4226 intel_opregion_notify_encoder(encoder, true);
4227 }
4f771f10 4228
e4916946
PZ
4229 /* If we change the relative order between pipe/planes enabling, we need
4230 * to change the workaround. */
4231 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4232 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4233}
4234
3f8dce3a
DV
4235static void ironlake_pfit_disable(struct intel_crtc *crtc)
4236{
4237 struct drm_device *dev = crtc->base.dev;
4238 struct drm_i915_private *dev_priv = dev->dev_private;
4239 int pipe = crtc->pipe;
4240
4241 /* To avoid upsetting the power well on haswell only disable the pfit if
4242 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4243 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4244 I915_WRITE(PF_CTL(pipe), 0);
4245 I915_WRITE(PF_WIN_POS(pipe), 0);
4246 I915_WRITE(PF_WIN_SZ(pipe), 0);
4247 }
4248}
4249
6be4a607
JB
4250static void ironlake_crtc_disable(struct drm_crtc *crtc)
4251{
4252 struct drm_device *dev = crtc->dev;
4253 struct drm_i915_private *dev_priv = dev->dev_private;
4254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4255 struct intel_encoder *encoder;
6be4a607 4256 int pipe = intel_crtc->pipe;
5eddb70b 4257 u32 reg, temp;
b52eb4dc 4258
f7abfe8b
CW
4259 if (!intel_crtc->active)
4260 return;
4261
d3eedb1a 4262 intel_crtc_disable_planes(crtc);
a5c4d7bc 4263
ea9d758d
DV
4264 for_each_encoder_on_crtc(dev, crtc, encoder)
4265 encoder->disable(encoder);
4266
d925c59a
DV
4267 if (intel_crtc->config.has_pch_encoder)
4268 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4269
575f7ab7 4270 intel_disable_pipe(intel_crtc);
32f9d658 4271
3f8dce3a 4272 ironlake_pfit_disable(intel_crtc);
2c07245f 4273
bf49ec8c
DV
4274 for_each_encoder_on_crtc(dev, crtc, encoder)
4275 if (encoder->post_disable)
4276 encoder->post_disable(encoder);
2c07245f 4277
d925c59a
DV
4278 if (intel_crtc->config.has_pch_encoder) {
4279 ironlake_fdi_disable(crtc);
913d8d11 4280
d925c59a
DV
4281 ironlake_disable_pch_transcoder(dev_priv, pipe);
4282 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4283
d925c59a
DV
4284 if (HAS_PCH_CPT(dev)) {
4285 /* disable TRANS_DP_CTL */
4286 reg = TRANS_DP_CTL(pipe);
4287 temp = I915_READ(reg);
4288 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4289 TRANS_DP_PORT_SEL_MASK);
4290 temp |= TRANS_DP_PORT_SEL_NONE;
4291 I915_WRITE(reg, temp);
4292
4293 /* disable DPLL_SEL */
4294 temp = I915_READ(PCH_DPLL_SEL);
11887397 4295 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4296 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4297 }
e3421a18 4298
d925c59a 4299 /* disable PCH DPLL */
e72f9fbf 4300 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4301
d925c59a
DV
4302 ironlake_fdi_pll_disable(intel_crtc);
4303 }
6b383a7f 4304
f7abfe8b 4305 intel_crtc->active = false;
46ba614c 4306 intel_update_watermarks(crtc);
d1ebd816
BW
4307
4308 mutex_lock(&dev->struct_mutex);
6b383a7f 4309 intel_update_fbc(dev);
d1ebd816 4310 mutex_unlock(&dev->struct_mutex);
6be4a607 4311}
1b3c7a47 4312
4f771f10 4313static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4314{
4f771f10
PZ
4315 struct drm_device *dev = crtc->dev;
4316 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4318 struct intel_encoder *encoder;
3b117c8f 4319 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4320
4f771f10
PZ
4321 if (!intel_crtc->active)
4322 return;
4323
d3eedb1a 4324 intel_crtc_disable_planes(crtc);
dda9a66a 4325
8807e55b
JN
4326 for_each_encoder_on_crtc(dev, crtc, encoder) {
4327 intel_opregion_notify_encoder(encoder, false);
4f771f10 4328 encoder->disable(encoder);
8807e55b 4329 }
4f771f10 4330
8664281b
PZ
4331 if (intel_crtc->config.has_pch_encoder)
4332 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
575f7ab7 4333 intel_disable_pipe(intel_crtc);
4f771f10 4334
a4bf214f
VS
4335 if (intel_crtc->config.dp_encoder_is_mst)
4336 intel_ddi_set_vc_payload_alloc(crtc, false);
4337
ad80a810 4338 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4339
3f8dce3a 4340 ironlake_pfit_disable(intel_crtc);
4f771f10 4341
1f544388 4342 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4343
88adfff1 4344 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4345 lpt_disable_pch_transcoder(dev_priv);
8664281b 4346 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4347 intel_ddi_fdi_disable(crtc);
83616634 4348 }
4f771f10 4349
97b040aa
ID
4350 for_each_encoder_on_crtc(dev, crtc, encoder)
4351 if (encoder->post_disable)
4352 encoder->post_disable(encoder);
4353
4f771f10 4354 intel_crtc->active = false;
46ba614c 4355 intel_update_watermarks(crtc);
4f771f10
PZ
4356
4357 mutex_lock(&dev->struct_mutex);
4358 intel_update_fbc(dev);
4359 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4360
4361 if (intel_crtc_to_shared_dpll(intel_crtc))
4362 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4363}
4364
ee7b9f93
JB
4365static void ironlake_crtc_off(struct drm_crtc *crtc)
4366{
4367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4368 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4369}
4370
6441ab5f 4371
2dd24552
JB
4372static void i9xx_pfit_enable(struct intel_crtc *crtc)
4373{
4374 struct drm_device *dev = crtc->base.dev;
4375 struct drm_i915_private *dev_priv = dev->dev_private;
4376 struct intel_crtc_config *pipe_config = &crtc->config;
4377
328d8e82 4378 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4379 return;
4380
2dd24552 4381 /*
c0b03411
DV
4382 * The panel fitter should only be adjusted whilst the pipe is disabled,
4383 * according to register description and PRM.
2dd24552 4384 */
c0b03411
DV
4385 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4386 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4387
b074cec8
JB
4388 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4389 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4390
4391 /* Border color in case we don't scale up to the full screen. Black by
4392 * default, change to something else for debugging. */
4393 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4394}
4395
d05410f9
DA
4396static enum intel_display_power_domain port_to_power_domain(enum port port)
4397{
4398 switch (port) {
4399 case PORT_A:
4400 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4401 case PORT_B:
4402 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4403 case PORT_C:
4404 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4405 case PORT_D:
4406 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4407 default:
4408 WARN_ON_ONCE(1);
4409 return POWER_DOMAIN_PORT_OTHER;
4410 }
4411}
4412
77d22dca
ID
4413#define for_each_power_domain(domain, mask) \
4414 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4415 if ((1 << (domain)) & (mask))
4416
319be8ae
ID
4417enum intel_display_power_domain
4418intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4419{
4420 struct drm_device *dev = intel_encoder->base.dev;
4421 struct intel_digital_port *intel_dig_port;
4422
4423 switch (intel_encoder->type) {
4424 case INTEL_OUTPUT_UNKNOWN:
4425 /* Only DDI platforms should ever use this output type */
4426 WARN_ON_ONCE(!HAS_DDI(dev));
4427 case INTEL_OUTPUT_DISPLAYPORT:
4428 case INTEL_OUTPUT_HDMI:
4429 case INTEL_OUTPUT_EDP:
4430 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4431 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4432 case INTEL_OUTPUT_DP_MST:
4433 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4434 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4435 case INTEL_OUTPUT_ANALOG:
4436 return POWER_DOMAIN_PORT_CRT;
4437 case INTEL_OUTPUT_DSI:
4438 return POWER_DOMAIN_PORT_DSI;
4439 default:
4440 return POWER_DOMAIN_PORT_OTHER;
4441 }
4442}
4443
4444static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4445{
319be8ae
ID
4446 struct drm_device *dev = crtc->dev;
4447 struct intel_encoder *intel_encoder;
4448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4449 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4450 unsigned long mask;
4451 enum transcoder transcoder;
4452
4453 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4454
4455 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4456 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4457 if (intel_crtc->config.pch_pfit.enabled ||
4458 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4459 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4460
319be8ae
ID
4461 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4462 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4463
77d22dca
ID
4464 return mask;
4465}
4466
4467void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4468 bool enable)
4469{
4470 if (dev_priv->power_domains.init_power_on == enable)
4471 return;
4472
4473 if (enable)
4474 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4475 else
4476 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4477
4478 dev_priv->power_domains.init_power_on = enable;
4479}
4480
4481static void modeset_update_crtc_power_domains(struct drm_device *dev)
4482{
4483 struct drm_i915_private *dev_priv = dev->dev_private;
4484 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4485 struct intel_crtc *crtc;
4486
4487 /*
4488 * First get all needed power domains, then put all unneeded, to avoid
4489 * any unnecessary toggling of the power wells.
4490 */
d3fcc808 4491 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4492 enum intel_display_power_domain domain;
4493
4494 if (!crtc->base.enabled)
4495 continue;
4496
319be8ae 4497 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4498
4499 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4500 intel_display_power_get(dev_priv, domain);
4501 }
4502
d3fcc808 4503 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4504 enum intel_display_power_domain domain;
4505
4506 for_each_power_domain(domain, crtc->enabled_power_domains)
4507 intel_display_power_put(dev_priv, domain);
4508
4509 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4510 }
4511
4512 intel_display_set_init_power(dev_priv, false);
4513}
4514
dfcab17e 4515/* returns HPLL frequency in kHz */
f8bf63fd 4516static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4517{
586f49dc 4518 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4519
586f49dc
JB
4520 /* Obtain SKU information */
4521 mutex_lock(&dev_priv->dpio_lock);
4522 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4523 CCK_FUSE_HPLL_FREQ_MASK;
4524 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4525
dfcab17e 4526 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4527}
4528
f8bf63fd
VS
4529static void vlv_update_cdclk(struct drm_device *dev)
4530{
4531 struct drm_i915_private *dev_priv = dev->dev_private;
4532
4533 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4534 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4535 dev_priv->vlv_cdclk_freq);
4536
4537 /*
4538 * Program the gmbus_freq based on the cdclk frequency.
4539 * BSpec erroneously claims we should aim for 4MHz, but
4540 * in fact 1MHz is the correct frequency.
4541 */
4542 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4543}
4544
30a970c6
JB
4545/* Adjust CDclk dividers to allow high res or save power if possible */
4546static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4547{
4548 struct drm_i915_private *dev_priv = dev->dev_private;
4549 u32 val, cmd;
4550
d197b7d3 4551 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4552
dfcab17e 4553 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4554 cmd = 2;
dfcab17e 4555 else if (cdclk == 266667)
30a970c6
JB
4556 cmd = 1;
4557 else
4558 cmd = 0;
4559
4560 mutex_lock(&dev_priv->rps.hw_lock);
4561 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4562 val &= ~DSPFREQGUAR_MASK;
4563 val |= (cmd << DSPFREQGUAR_SHIFT);
4564 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4565 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4566 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4567 50)) {
4568 DRM_ERROR("timed out waiting for CDclk change\n");
4569 }
4570 mutex_unlock(&dev_priv->rps.hw_lock);
4571
dfcab17e 4572 if (cdclk == 400000) {
30a970c6
JB
4573 u32 divider, vco;
4574
4575 vco = valleyview_get_vco(dev_priv);
dfcab17e 4576 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4577
4578 mutex_lock(&dev_priv->dpio_lock);
4579 /* adjust cdclk divider */
4580 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4581 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4582 val |= divider;
4583 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4584
4585 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4586 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4587 50))
4588 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4589 mutex_unlock(&dev_priv->dpio_lock);
4590 }
4591
4592 mutex_lock(&dev_priv->dpio_lock);
4593 /* adjust self-refresh exit latency value */
4594 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4595 val &= ~0x7f;
4596
4597 /*
4598 * For high bandwidth configs, we set a higher latency in the bunit
4599 * so that the core display fetch happens in time to avoid underruns.
4600 */
dfcab17e 4601 if (cdclk == 400000)
30a970c6
JB
4602 val |= 4500 / 250; /* 4.5 usec */
4603 else
4604 val |= 3000 / 250; /* 3.0 usec */
4605 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4606 mutex_unlock(&dev_priv->dpio_lock);
4607
f8bf63fd 4608 vlv_update_cdclk(dev);
30a970c6
JB
4609}
4610
383c5a6a
VS
4611static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4612{
4613 struct drm_i915_private *dev_priv = dev->dev_private;
4614 u32 val, cmd;
4615
4616 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4617
4618 switch (cdclk) {
4619 case 400000:
4620 cmd = 3;
4621 break;
4622 case 333333:
4623 case 320000:
4624 cmd = 2;
4625 break;
4626 case 266667:
4627 cmd = 1;
4628 break;
4629 case 200000:
4630 cmd = 0;
4631 break;
4632 default:
4633 WARN_ON(1);
4634 return;
4635 }
4636
4637 mutex_lock(&dev_priv->rps.hw_lock);
4638 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4639 val &= ~DSPFREQGUAR_MASK_CHV;
4640 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4641 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4642 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4643 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4644 50)) {
4645 DRM_ERROR("timed out waiting for CDclk change\n");
4646 }
4647 mutex_unlock(&dev_priv->rps.hw_lock);
4648
4649 vlv_update_cdclk(dev);
4650}
4651
30a970c6
JB
4652static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4653 int max_pixclk)
4654{
29dc7ef3
VS
4655 int vco = valleyview_get_vco(dev_priv);
4656 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4657
d49a340d
VS
4658 /* FIXME: Punit isn't quite ready yet */
4659 if (IS_CHERRYVIEW(dev_priv->dev))
4660 return 400000;
4661
30a970c6
JB
4662 /*
4663 * Really only a few cases to deal with, as only 4 CDclks are supported:
4664 * 200MHz
4665 * 267MHz
29dc7ef3 4666 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4667 * 400MHz
4668 * So we check to see whether we're above 90% of the lower bin and
4669 * adjust if needed.
e37c67a1
VS
4670 *
4671 * We seem to get an unstable or solid color picture at 200MHz.
4672 * Not sure what's wrong. For now use 200MHz only when all pipes
4673 * are off.
30a970c6 4674 */
29dc7ef3 4675 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4676 return 400000;
4677 else if (max_pixclk > 266667*9/10)
29dc7ef3 4678 return freq_320;
e37c67a1 4679 else if (max_pixclk > 0)
dfcab17e 4680 return 266667;
e37c67a1
VS
4681 else
4682 return 200000;
30a970c6
JB
4683}
4684
2f2d7aa1
VS
4685/* compute the max pixel clock for new configuration */
4686static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4687{
4688 struct drm_device *dev = dev_priv->dev;
4689 struct intel_crtc *intel_crtc;
4690 int max_pixclk = 0;
4691
d3fcc808 4692 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4693 if (intel_crtc->new_enabled)
30a970c6 4694 max_pixclk = max(max_pixclk,
2f2d7aa1 4695 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4696 }
4697
4698 return max_pixclk;
4699}
4700
4701static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4702 unsigned *prepare_pipes)
30a970c6
JB
4703{
4704 struct drm_i915_private *dev_priv = dev->dev_private;
4705 struct intel_crtc *intel_crtc;
2f2d7aa1 4706 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4707
d60c4473
ID
4708 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4709 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4710 return;
4711
2f2d7aa1 4712 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4713 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4714 if (intel_crtc->base.enabled)
4715 *prepare_pipes |= (1 << intel_crtc->pipe);
4716}
4717
4718static void valleyview_modeset_global_resources(struct drm_device *dev)
4719{
4720 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4721 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4722 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4723
383c5a6a
VS
4724 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4725 if (IS_CHERRYVIEW(dev))
4726 cherryview_set_cdclk(dev, req_cdclk);
4727 else
4728 valleyview_set_cdclk(dev, req_cdclk);
4729 }
4730
77961eb9 4731 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4732}
4733
89b667f8
JB
4734static void valleyview_crtc_enable(struct drm_crtc *crtc)
4735{
4736 struct drm_device *dev = crtc->dev;
89b667f8
JB
4737 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4738 struct intel_encoder *encoder;
4739 int pipe = intel_crtc->pipe;
23538ef1 4740 bool is_dsi;
89b667f8
JB
4741
4742 WARN_ON(!crtc->enabled);
4743
4744 if (intel_crtc->active)
4745 return;
4746
8525a235
SK
4747 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4748
1ae0d137
VS
4749 if (!is_dsi) {
4750 if (IS_CHERRYVIEW(dev))
4751 chv_prepare_pll(intel_crtc);
4752 else
4753 vlv_prepare_pll(intel_crtc);
4754 }
5b18e57c
DV
4755
4756 if (intel_crtc->config.has_dp_encoder)
4757 intel_dp_set_m_n(intel_crtc);
4758
4759 intel_set_pipe_timings(intel_crtc);
4760
5b18e57c
DV
4761 i9xx_set_pipeconf(intel_crtc);
4762
89b667f8 4763 intel_crtc->active = true;
89b667f8 4764
4a3436e8
VS
4765 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4766
89b667f8
JB
4767 for_each_encoder_on_crtc(dev, crtc, encoder)
4768 if (encoder->pre_pll_enable)
4769 encoder->pre_pll_enable(encoder);
4770
9d556c99
CML
4771 if (!is_dsi) {
4772 if (IS_CHERRYVIEW(dev))
4773 chv_enable_pll(intel_crtc);
4774 else
4775 vlv_enable_pll(intel_crtc);
4776 }
89b667f8
JB
4777
4778 for_each_encoder_on_crtc(dev, crtc, encoder)
4779 if (encoder->pre_enable)
4780 encoder->pre_enable(encoder);
4781
2dd24552
JB
4782 i9xx_pfit_enable(intel_crtc);
4783
63cbb074
VS
4784 intel_crtc_load_lut(crtc);
4785
f37fcc2a 4786 intel_update_watermarks(crtc);
e1fdc473 4787 intel_enable_pipe(intel_crtc);
be6a6f8e 4788
5004945f
JN
4789 for_each_encoder_on_crtc(dev, crtc, encoder)
4790 encoder->enable(encoder);
9ab0460b
VS
4791
4792 intel_crtc_enable_planes(crtc);
d40d9187 4793
56b80e1f
VS
4794 /* Underruns don't raise interrupts, so check manually. */
4795 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4796}
4797
f13c2ef3
DV
4798static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4799{
4800 struct drm_device *dev = crtc->base.dev;
4801 struct drm_i915_private *dev_priv = dev->dev_private;
4802
4803 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4804 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4805}
4806
0b8765c6 4807static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4808{
4809 struct drm_device *dev = crtc->dev;
79e53945 4810 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4811 struct intel_encoder *encoder;
79e53945 4812 int pipe = intel_crtc->pipe;
79e53945 4813
08a48469
DV
4814 WARN_ON(!crtc->enabled);
4815
f7abfe8b
CW
4816 if (intel_crtc->active)
4817 return;
4818
f13c2ef3
DV
4819 i9xx_set_pll_dividers(intel_crtc);
4820
5b18e57c
DV
4821 if (intel_crtc->config.has_dp_encoder)
4822 intel_dp_set_m_n(intel_crtc);
4823
4824 intel_set_pipe_timings(intel_crtc);
4825
5b18e57c
DV
4826 i9xx_set_pipeconf(intel_crtc);
4827
f7abfe8b 4828 intel_crtc->active = true;
6b383a7f 4829
4a3436e8
VS
4830 if (!IS_GEN2(dev))
4831 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4832
9d6d9f19
MK
4833 for_each_encoder_on_crtc(dev, crtc, encoder)
4834 if (encoder->pre_enable)
4835 encoder->pre_enable(encoder);
4836
f6736a1a
DV
4837 i9xx_enable_pll(intel_crtc);
4838
2dd24552
JB
4839 i9xx_pfit_enable(intel_crtc);
4840
63cbb074
VS
4841 intel_crtc_load_lut(crtc);
4842
f37fcc2a 4843 intel_update_watermarks(crtc);
e1fdc473 4844 intel_enable_pipe(intel_crtc);
be6a6f8e 4845
fa5c73b1
DV
4846 for_each_encoder_on_crtc(dev, crtc, encoder)
4847 encoder->enable(encoder);
9ab0460b
VS
4848
4849 intel_crtc_enable_planes(crtc);
d40d9187 4850
4a3436e8
VS
4851 /*
4852 * Gen2 reports pipe underruns whenever all planes are disabled.
4853 * So don't enable underrun reporting before at least some planes
4854 * are enabled.
4855 * FIXME: Need to fix the logic to work when we turn off all planes
4856 * but leave the pipe running.
4857 */
4858 if (IS_GEN2(dev))
4859 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4860
56b80e1f
VS
4861 /* Underruns don't raise interrupts, so check manually. */
4862 i9xx_check_fifo_underruns(dev);
0b8765c6 4863}
79e53945 4864
87476d63
DV
4865static void i9xx_pfit_disable(struct intel_crtc *crtc)
4866{
4867 struct drm_device *dev = crtc->base.dev;
4868 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4869
328d8e82
DV
4870 if (!crtc->config.gmch_pfit.control)
4871 return;
87476d63 4872
328d8e82 4873 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4874
328d8e82
DV
4875 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4876 I915_READ(PFIT_CONTROL));
4877 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4878}
4879
0b8765c6
JB
4880static void i9xx_crtc_disable(struct drm_crtc *crtc)
4881{
4882 struct drm_device *dev = crtc->dev;
4883 struct drm_i915_private *dev_priv = dev->dev_private;
4884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4885 struct intel_encoder *encoder;
0b8765c6 4886 int pipe = intel_crtc->pipe;
ef9c3aee 4887
f7abfe8b
CW
4888 if (!intel_crtc->active)
4889 return;
4890
4a3436e8
VS
4891 /*
4892 * Gen2 reports pipe underruns whenever all planes are disabled.
4893 * So diasble underrun reporting before all the planes get disabled.
4894 * FIXME: Need to fix the logic to work when we turn off all planes
4895 * but leave the pipe running.
4896 */
4897 if (IS_GEN2(dev))
4898 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4899
564ed191
ID
4900 /*
4901 * Vblank time updates from the shadow to live plane control register
4902 * are blocked if the memory self-refresh mode is active at that
4903 * moment. So to make sure the plane gets truly disabled, disable
4904 * first the self-refresh mode. The self-refresh enable bit in turn
4905 * will be checked/applied by the HW only at the next frame start
4906 * event which is after the vblank start event, so we need to have a
4907 * wait-for-vblank between disabling the plane and the pipe.
4908 */
4909 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
4910 intel_crtc_disable_planes(crtc);
4911
ea9d758d
DV
4912 for_each_encoder_on_crtc(dev, crtc, encoder)
4913 encoder->disable(encoder);
4914
6304cd91
VS
4915 /*
4916 * On gen2 planes are double buffered but the pipe isn't, so we must
4917 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
4918 * We also need to wait on all gmch platforms because of the
4919 * self-refresh mode constraint explained above.
6304cd91 4920 */
564ed191 4921 intel_wait_for_vblank(dev, pipe);
6304cd91 4922
575f7ab7 4923 intel_disable_pipe(intel_crtc);
24a1f16d 4924
87476d63 4925 i9xx_pfit_disable(intel_crtc);
24a1f16d 4926
89b667f8
JB
4927 for_each_encoder_on_crtc(dev, crtc, encoder)
4928 if (encoder->post_disable)
4929 encoder->post_disable(encoder);
4930
076ed3b2
CML
4931 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4932 if (IS_CHERRYVIEW(dev))
4933 chv_disable_pll(dev_priv, pipe);
4934 else if (IS_VALLEYVIEW(dev))
4935 vlv_disable_pll(dev_priv, pipe);
4936 else
1c4e0274 4937 i9xx_disable_pll(intel_crtc);
076ed3b2 4938 }
0b8765c6 4939
4a3436e8
VS
4940 if (!IS_GEN2(dev))
4941 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4942
f7abfe8b 4943 intel_crtc->active = false;
46ba614c 4944 intel_update_watermarks(crtc);
f37fcc2a 4945
efa9624e 4946 mutex_lock(&dev->struct_mutex);
6b383a7f 4947 intel_update_fbc(dev);
efa9624e 4948 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
4949}
4950
ee7b9f93
JB
4951static void i9xx_crtc_off(struct drm_crtc *crtc)
4952{
4953}
4954
976f8a20
DV
4955static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4956 bool enabled)
2c07245f
ZW
4957{
4958 struct drm_device *dev = crtc->dev;
4959 struct drm_i915_master_private *master_priv;
4960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4961 int pipe = intel_crtc->pipe;
79e53945
JB
4962
4963 if (!dev->primary->master)
4964 return;
4965
4966 master_priv = dev->primary->master->driver_priv;
4967 if (!master_priv->sarea_priv)
4968 return;
4969
79e53945
JB
4970 switch (pipe) {
4971 case 0:
4972 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4973 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4974 break;
4975 case 1:
4976 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4977 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4978 break;
4979 default:
9db4a9c7 4980 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4981 break;
4982 }
79e53945
JB
4983}
4984
b04c5bd6
BF
4985/* Master function to enable/disable CRTC and corresponding power wells */
4986void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
4987{
4988 struct drm_device *dev = crtc->dev;
4989 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 4990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
4991 enum intel_display_power_domain domain;
4992 unsigned long domains;
976f8a20 4993
0e572fe7
DV
4994 if (enable) {
4995 if (!intel_crtc->active) {
e1e9fb84
DV
4996 domains = get_crtc_power_domains(crtc);
4997 for_each_power_domain(domain, domains)
4998 intel_display_power_get(dev_priv, domain);
4999 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5000
5001 dev_priv->display.crtc_enable(crtc);
5002 }
5003 } else {
5004 if (intel_crtc->active) {
5005 dev_priv->display.crtc_disable(crtc);
5006
e1e9fb84
DV
5007 domains = intel_crtc->enabled_power_domains;
5008 for_each_power_domain(domain, domains)
5009 intel_display_power_put(dev_priv, domain);
5010 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5011 }
5012 }
b04c5bd6
BF
5013}
5014
5015/**
5016 * Sets the power management mode of the pipe and plane.
5017 */
5018void intel_crtc_update_dpms(struct drm_crtc *crtc)
5019{
5020 struct drm_device *dev = crtc->dev;
5021 struct intel_encoder *intel_encoder;
5022 bool enable = false;
5023
5024 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5025 enable |= intel_encoder->connectors_active;
5026
5027 intel_crtc_control(crtc, enable);
976f8a20
DV
5028
5029 intel_crtc_update_sarea(crtc, enable);
5030}
5031
cdd59983
CW
5032static void intel_crtc_disable(struct drm_crtc *crtc)
5033{
cdd59983 5034 struct drm_device *dev = crtc->dev;
976f8a20 5035 struct drm_connector *connector;
ee7b9f93 5036 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 5037 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 5038 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 5039
976f8a20
DV
5040 /* crtc should still be enabled when we disable it. */
5041 WARN_ON(!crtc->enabled);
5042
5043 dev_priv->display.crtc_disable(crtc);
5044 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
5045 dev_priv->display.off(crtc);
5046
f4510a27 5047 if (crtc->primary->fb) {
cdd59983 5048 mutex_lock(&dev->struct_mutex);
a071fa00
DV
5049 intel_unpin_fb_obj(old_obj);
5050 i915_gem_track_fb(old_obj, NULL,
5051 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 5052 mutex_unlock(&dev->struct_mutex);
f4510a27 5053 crtc->primary->fb = NULL;
976f8a20
DV
5054 }
5055
5056 /* Update computed state. */
5057 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5058 if (!connector->encoder || !connector->encoder->crtc)
5059 continue;
5060
5061 if (connector->encoder->crtc != crtc)
5062 continue;
5063
5064 connector->dpms = DRM_MODE_DPMS_OFF;
5065 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5066 }
5067}
5068
ea5b213a 5069void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5070{
4ef69c7a 5071 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5072
ea5b213a
CW
5073 drm_encoder_cleanup(encoder);
5074 kfree(intel_encoder);
7e7d76c3
JB
5075}
5076
9237329d 5077/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5078 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5079 * state of the entire output pipe. */
9237329d 5080static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5081{
5ab432ef
DV
5082 if (mode == DRM_MODE_DPMS_ON) {
5083 encoder->connectors_active = true;
5084
b2cabb0e 5085 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5086 } else {
5087 encoder->connectors_active = false;
5088
b2cabb0e 5089 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5090 }
79e53945
JB
5091}
5092
0a91ca29
DV
5093/* Cross check the actual hw state with our own modeset state tracking (and it's
5094 * internal consistency). */
b980514c 5095static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5096{
0a91ca29
DV
5097 if (connector->get_hw_state(connector)) {
5098 struct intel_encoder *encoder = connector->encoder;
5099 struct drm_crtc *crtc;
5100 bool encoder_enabled;
5101 enum pipe pipe;
5102
5103 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5104 connector->base.base.id,
c23cc417 5105 connector->base.name);
0a91ca29 5106
0e32b39c
DA
5107 /* there is no real hw state for MST connectors */
5108 if (connector->mst_port)
5109 return;
5110
0a91ca29
DV
5111 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5112 "wrong connector dpms state\n");
5113 WARN(connector->base.encoder != &encoder->base,
5114 "active connector not linked to encoder\n");
0a91ca29 5115
36cd7444
DA
5116 if (encoder) {
5117 WARN(!encoder->connectors_active,
5118 "encoder->connectors_active not set\n");
5119
5120 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5121 WARN(!encoder_enabled, "encoder not enabled\n");
5122 if (WARN_ON(!encoder->base.crtc))
5123 return;
0a91ca29 5124
36cd7444 5125 crtc = encoder->base.crtc;
0a91ca29 5126
36cd7444
DA
5127 WARN(!crtc->enabled, "crtc not enabled\n");
5128 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5129 WARN(pipe != to_intel_crtc(crtc)->pipe,
5130 "encoder active on the wrong pipe\n");
5131 }
0a91ca29 5132 }
79e53945
JB
5133}
5134
5ab432ef
DV
5135/* Even simpler default implementation, if there's really no special case to
5136 * consider. */
5137void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5138{
5ab432ef
DV
5139 /* All the simple cases only support two dpms states. */
5140 if (mode != DRM_MODE_DPMS_ON)
5141 mode = DRM_MODE_DPMS_OFF;
d4270e57 5142
5ab432ef
DV
5143 if (mode == connector->dpms)
5144 return;
5145
5146 connector->dpms = mode;
5147
5148 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5149 if (connector->encoder)
5150 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5151
b980514c 5152 intel_modeset_check_state(connector->dev);
79e53945
JB
5153}
5154
f0947c37
DV
5155/* Simple connector->get_hw_state implementation for encoders that support only
5156 * one connector and no cloning and hence the encoder state determines the state
5157 * of the connector. */
5158bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5159{
24929352 5160 enum pipe pipe = 0;
f0947c37 5161 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5162
f0947c37 5163 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5164}
5165
1857e1da
DV
5166static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5167 struct intel_crtc_config *pipe_config)
5168{
5169 struct drm_i915_private *dev_priv = dev->dev_private;
5170 struct intel_crtc *pipe_B_crtc =
5171 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5172
5173 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5174 pipe_name(pipe), pipe_config->fdi_lanes);
5175 if (pipe_config->fdi_lanes > 4) {
5176 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5177 pipe_name(pipe), pipe_config->fdi_lanes);
5178 return false;
5179 }
5180
bafb6553 5181 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5182 if (pipe_config->fdi_lanes > 2) {
5183 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5184 pipe_config->fdi_lanes);
5185 return false;
5186 } else {
5187 return true;
5188 }
5189 }
5190
5191 if (INTEL_INFO(dev)->num_pipes == 2)
5192 return true;
5193
5194 /* Ivybridge 3 pipe is really complicated */
5195 switch (pipe) {
5196 case PIPE_A:
5197 return true;
5198 case PIPE_B:
5199 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5200 pipe_config->fdi_lanes > 2) {
5201 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5202 pipe_name(pipe), pipe_config->fdi_lanes);
5203 return false;
5204 }
5205 return true;
5206 case PIPE_C:
1e833f40 5207 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5208 pipe_B_crtc->config.fdi_lanes <= 2) {
5209 if (pipe_config->fdi_lanes > 2) {
5210 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5211 pipe_name(pipe), pipe_config->fdi_lanes);
5212 return false;
5213 }
5214 } else {
5215 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5216 return false;
5217 }
5218 return true;
5219 default:
5220 BUG();
5221 }
5222}
5223
e29c22c0
DV
5224#define RETRY 1
5225static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5226 struct intel_crtc_config *pipe_config)
877d48d5 5227{
1857e1da 5228 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5229 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5230 int lane, link_bw, fdi_dotclock;
e29c22c0 5231 bool setup_ok, needs_recompute = false;
877d48d5 5232
e29c22c0 5233retry:
877d48d5
DV
5234 /* FDI is a binary signal running at ~2.7GHz, encoding
5235 * each output octet as 10 bits. The actual frequency
5236 * is stored as a divider into a 100MHz clock, and the
5237 * mode pixel clock is stored in units of 1KHz.
5238 * Hence the bw of each lane in terms of the mode signal
5239 * is:
5240 */
5241 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5242
241bfc38 5243 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5244
2bd89a07 5245 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5246 pipe_config->pipe_bpp);
5247
5248 pipe_config->fdi_lanes = lane;
5249
2bd89a07 5250 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5251 link_bw, &pipe_config->fdi_m_n);
1857e1da 5252
e29c22c0
DV
5253 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5254 intel_crtc->pipe, pipe_config);
5255 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5256 pipe_config->pipe_bpp -= 2*3;
5257 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5258 pipe_config->pipe_bpp);
5259 needs_recompute = true;
5260 pipe_config->bw_constrained = true;
5261
5262 goto retry;
5263 }
5264
5265 if (needs_recompute)
5266 return RETRY;
5267
5268 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5269}
5270
42db64ef
PZ
5271static void hsw_compute_ips_config(struct intel_crtc *crtc,
5272 struct intel_crtc_config *pipe_config)
5273{
d330a953 5274 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5275 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5276 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5277}
5278
a43f6e0f 5279static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5280 struct intel_crtc_config *pipe_config)
79e53945 5281{
a43f6e0f 5282 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5283 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5284
ad3a4479 5285 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5286 if (INTEL_INFO(dev)->gen < 4) {
5287 struct drm_i915_private *dev_priv = dev->dev_private;
5288 int clock_limit =
5289 dev_priv->display.get_display_clock_speed(dev);
5290
5291 /*
5292 * Enable pixel doubling when the dot clock
5293 * is > 90% of the (display) core speed.
5294 *
b397c96b
VS
5295 * GDG double wide on either pipe,
5296 * otherwise pipe A only.
cf532bb2 5297 */
b397c96b 5298 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5299 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5300 clock_limit *= 2;
cf532bb2 5301 pipe_config->double_wide = true;
ad3a4479
VS
5302 }
5303
241bfc38 5304 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5305 return -EINVAL;
2c07245f 5306 }
89749350 5307
1d1d0e27
VS
5308 /*
5309 * Pipe horizontal size must be even in:
5310 * - DVO ganged mode
5311 * - LVDS dual channel mode
5312 * - Double wide pipe
5313 */
5314 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5315 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5316 pipe_config->pipe_src_w &= ~1;
5317
8693a824
DL
5318 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5319 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5320 */
5321 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5322 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5323 return -EINVAL;
44f46b42 5324
bd080ee5 5325 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5326 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5327 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5328 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5329 * for lvds. */
5330 pipe_config->pipe_bpp = 8*3;
5331 }
5332
f5adf94e 5333 if (HAS_IPS(dev))
a43f6e0f
DV
5334 hsw_compute_ips_config(crtc, pipe_config);
5335
12030431
DV
5336 /*
5337 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5338 * old clock survives for now.
5339 */
5340 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5341 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5342
877d48d5 5343 if (pipe_config->has_pch_encoder)
a43f6e0f 5344 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5345
e29c22c0 5346 return 0;
79e53945
JB
5347}
5348
25eb05fc
JB
5349static int valleyview_get_display_clock_speed(struct drm_device *dev)
5350{
d197b7d3
VS
5351 struct drm_i915_private *dev_priv = dev->dev_private;
5352 int vco = valleyview_get_vco(dev_priv);
5353 u32 val;
5354 int divider;
5355
d49a340d
VS
5356 /* FIXME: Punit isn't quite ready yet */
5357 if (IS_CHERRYVIEW(dev))
5358 return 400000;
5359
d197b7d3
VS
5360 mutex_lock(&dev_priv->dpio_lock);
5361 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5362 mutex_unlock(&dev_priv->dpio_lock);
5363
5364 divider = val & DISPLAY_FREQUENCY_VALUES;
5365
7d007f40
VS
5366 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5367 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5368 "cdclk change in progress\n");
5369
d197b7d3 5370 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5371}
5372
e70236a8
JB
5373static int i945_get_display_clock_speed(struct drm_device *dev)
5374{
5375 return 400000;
5376}
79e53945 5377
e70236a8 5378static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5379{
e70236a8
JB
5380 return 333000;
5381}
79e53945 5382
e70236a8
JB
5383static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5384{
5385 return 200000;
5386}
79e53945 5387
257a7ffc
DV
5388static int pnv_get_display_clock_speed(struct drm_device *dev)
5389{
5390 u16 gcfgc = 0;
5391
5392 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5393
5394 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5395 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5396 return 267000;
5397 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5398 return 333000;
5399 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5400 return 444000;
5401 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5402 return 200000;
5403 default:
5404 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5405 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5406 return 133000;
5407 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5408 return 167000;
5409 }
5410}
5411
e70236a8
JB
5412static int i915gm_get_display_clock_speed(struct drm_device *dev)
5413{
5414 u16 gcfgc = 0;
79e53945 5415
e70236a8
JB
5416 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5417
5418 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5419 return 133000;
5420 else {
5421 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5422 case GC_DISPLAY_CLOCK_333_MHZ:
5423 return 333000;
5424 default:
5425 case GC_DISPLAY_CLOCK_190_200_MHZ:
5426 return 190000;
79e53945 5427 }
e70236a8
JB
5428 }
5429}
5430
5431static int i865_get_display_clock_speed(struct drm_device *dev)
5432{
5433 return 266000;
5434}
5435
5436static int i855_get_display_clock_speed(struct drm_device *dev)
5437{
5438 u16 hpllcc = 0;
5439 /* Assume that the hardware is in the high speed state. This
5440 * should be the default.
5441 */
5442 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5443 case GC_CLOCK_133_200:
5444 case GC_CLOCK_100_200:
5445 return 200000;
5446 case GC_CLOCK_166_250:
5447 return 250000;
5448 case GC_CLOCK_100_133:
79e53945 5449 return 133000;
e70236a8 5450 }
79e53945 5451
e70236a8
JB
5452 /* Shouldn't happen */
5453 return 0;
5454}
79e53945 5455
e70236a8
JB
5456static int i830_get_display_clock_speed(struct drm_device *dev)
5457{
5458 return 133000;
79e53945
JB
5459}
5460
2c07245f 5461static void
a65851af 5462intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5463{
a65851af
VS
5464 while (*num > DATA_LINK_M_N_MASK ||
5465 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5466 *num >>= 1;
5467 *den >>= 1;
5468 }
5469}
5470
a65851af
VS
5471static void compute_m_n(unsigned int m, unsigned int n,
5472 uint32_t *ret_m, uint32_t *ret_n)
5473{
5474 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5475 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5476 intel_reduce_m_n_ratio(ret_m, ret_n);
5477}
5478
e69d0bc1
DV
5479void
5480intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5481 int pixel_clock, int link_clock,
5482 struct intel_link_m_n *m_n)
2c07245f 5483{
e69d0bc1 5484 m_n->tu = 64;
a65851af
VS
5485
5486 compute_m_n(bits_per_pixel * pixel_clock,
5487 link_clock * nlanes * 8,
5488 &m_n->gmch_m, &m_n->gmch_n);
5489
5490 compute_m_n(pixel_clock, link_clock,
5491 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5492}
5493
a7615030
CW
5494static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5495{
d330a953
JN
5496 if (i915.panel_use_ssc >= 0)
5497 return i915.panel_use_ssc != 0;
41aa3448 5498 return dev_priv->vbt.lvds_use_ssc
435793df 5499 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5500}
5501
c65d77d8
JB
5502static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5503{
5504 struct drm_device *dev = crtc->dev;
5505 struct drm_i915_private *dev_priv = dev->dev_private;
5506 int refclk;
5507
a0c4da24 5508 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5509 refclk = 100000;
a0c4da24 5510 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5511 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5512 refclk = dev_priv->vbt.lvds_ssc_freq;
5513 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5514 } else if (!IS_GEN2(dev)) {
5515 refclk = 96000;
5516 } else {
5517 refclk = 48000;
5518 }
5519
5520 return refclk;
5521}
5522
7429e9d4 5523static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5524{
7df00d7a 5525 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5526}
f47709a9 5527
7429e9d4
DV
5528static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5529{
5530 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5531}
5532
f47709a9 5533static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5534 intel_clock_t *reduced_clock)
5535{
f47709a9 5536 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5537 u32 fp, fp2 = 0;
5538
5539 if (IS_PINEVIEW(dev)) {
7429e9d4 5540 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5541 if (reduced_clock)
7429e9d4 5542 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5543 } else {
7429e9d4 5544 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5545 if (reduced_clock)
7429e9d4 5546 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5547 }
5548
8bcc2795 5549 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5550
f47709a9
DV
5551 crtc->lowfreq_avail = false;
5552 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5553 reduced_clock && i915.powersave) {
8bcc2795 5554 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5555 crtc->lowfreq_avail = true;
a7516a05 5556 } else {
8bcc2795 5557 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5558 }
5559}
5560
5e69f97f
CML
5561static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5562 pipe)
89b667f8
JB
5563{
5564 u32 reg_val;
5565
5566 /*
5567 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5568 * and set it to a reasonable value instead.
5569 */
ab3c759a 5570 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5571 reg_val &= 0xffffff00;
5572 reg_val |= 0x00000030;
ab3c759a 5573 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5574
ab3c759a 5575 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5576 reg_val &= 0x8cffffff;
5577 reg_val = 0x8c000000;
ab3c759a 5578 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5579
ab3c759a 5580 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5581 reg_val &= 0xffffff00;
ab3c759a 5582 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5583
ab3c759a 5584 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5585 reg_val &= 0x00ffffff;
5586 reg_val |= 0xb0000000;
ab3c759a 5587 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5588}
5589
b551842d
DV
5590static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5591 struct intel_link_m_n *m_n)
5592{
5593 struct drm_device *dev = crtc->base.dev;
5594 struct drm_i915_private *dev_priv = dev->dev_private;
5595 int pipe = crtc->pipe;
5596
e3b95f1e
DV
5597 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5598 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5599 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5600 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5601}
5602
5603static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5604 struct intel_link_m_n *m_n,
5605 struct intel_link_m_n *m2_n2)
b551842d
DV
5606{
5607 struct drm_device *dev = crtc->base.dev;
5608 struct drm_i915_private *dev_priv = dev->dev_private;
5609 int pipe = crtc->pipe;
5610 enum transcoder transcoder = crtc->config.cpu_transcoder;
5611
5612 if (INTEL_INFO(dev)->gen >= 5) {
5613 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5614 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5615 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5616 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5617 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5618 * for gen < 8) and if DRRS is supported (to make sure the
5619 * registers are not unnecessarily accessed).
5620 */
5621 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5622 crtc->config.has_drrs) {
5623 I915_WRITE(PIPE_DATA_M2(transcoder),
5624 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5625 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5626 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5627 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5628 }
b551842d 5629 } else {
e3b95f1e
DV
5630 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5631 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5632 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5633 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5634 }
5635}
5636
f769cd24 5637void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5638{
5639 if (crtc->config.has_pch_encoder)
5640 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5641 else
f769cd24
VK
5642 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5643 &crtc->config.dp_m2_n2);
03afc4a2
DV
5644}
5645
f47709a9 5646static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5647{
5648 u32 dpll, dpll_md;
5649
5650 /*
5651 * Enable DPIO clock input. We should never disable the reference
5652 * clock for pipe B, since VGA hotplug / manual detection depends
5653 * on it.
5654 */
5655 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5656 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5657 /* We should never disable this, set it here for state tracking */
5658 if (crtc->pipe == PIPE_B)
5659 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5660 dpll |= DPLL_VCO_ENABLE;
5661 crtc->config.dpll_hw_state.dpll = dpll;
5662
5663 dpll_md = (crtc->config.pixel_multiplier - 1)
5664 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5665 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5666}
5667
5668static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5669{
f47709a9 5670 struct drm_device *dev = crtc->base.dev;
a0c4da24 5671 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5672 int pipe = crtc->pipe;
bdd4b6a6 5673 u32 mdiv;
a0c4da24 5674 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5675 u32 coreclk, reg_val;
a0c4da24 5676
09153000
DV
5677 mutex_lock(&dev_priv->dpio_lock);
5678
f47709a9
DV
5679 bestn = crtc->config.dpll.n;
5680 bestm1 = crtc->config.dpll.m1;
5681 bestm2 = crtc->config.dpll.m2;
5682 bestp1 = crtc->config.dpll.p1;
5683 bestp2 = crtc->config.dpll.p2;
a0c4da24 5684
89b667f8
JB
5685 /* See eDP HDMI DPIO driver vbios notes doc */
5686
5687 /* PLL B needs special handling */
bdd4b6a6 5688 if (pipe == PIPE_B)
5e69f97f 5689 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5690
5691 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5692 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5693
5694 /* Disable target IRef on PLL */
ab3c759a 5695 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5696 reg_val &= 0x00ffffff;
ab3c759a 5697 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5698
5699 /* Disable fast lock */
ab3c759a 5700 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5701
5702 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5703 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5704 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5705 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5706 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5707
5708 /*
5709 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5710 * but we don't support that).
5711 * Note: don't use the DAC post divider as it seems unstable.
5712 */
5713 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5714 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5715
a0c4da24 5716 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5717 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5718
89b667f8 5719 /* Set HBR and RBR LPF coefficients */
ff9a6750 5720 if (crtc->config.port_clock == 162000 ||
99750bd4 5721 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5722 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5723 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5724 0x009f0003);
89b667f8 5725 else
ab3c759a 5726 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5727 0x00d0000f);
5728
5729 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5730 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5731 /* Use SSC source */
bdd4b6a6 5732 if (pipe == PIPE_A)
ab3c759a 5733 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5734 0x0df40000);
5735 else
ab3c759a 5736 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5737 0x0df70000);
5738 } else { /* HDMI or VGA */
5739 /* Use bend source */
bdd4b6a6 5740 if (pipe == PIPE_A)
ab3c759a 5741 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5742 0x0df70000);
5743 else
ab3c759a 5744 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5745 0x0df40000);
5746 }
a0c4da24 5747
ab3c759a 5748 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5749 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5750 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5751 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5752 coreclk |= 0x01000000;
ab3c759a 5753 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5754
ab3c759a 5755 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5756 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5757}
5758
9d556c99 5759static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5760{
5761 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5762 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5763 DPLL_VCO_ENABLE;
5764 if (crtc->pipe != PIPE_A)
5765 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5766
5767 crtc->config.dpll_hw_state.dpll_md =
5768 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5769}
5770
5771static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5772{
5773 struct drm_device *dev = crtc->base.dev;
5774 struct drm_i915_private *dev_priv = dev->dev_private;
5775 int pipe = crtc->pipe;
5776 int dpll_reg = DPLL(crtc->pipe);
5777 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5778 u32 loopfilter, intcoeff;
9d556c99
CML
5779 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5780 int refclk;
5781
9d556c99
CML
5782 bestn = crtc->config.dpll.n;
5783 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5784 bestm1 = crtc->config.dpll.m1;
5785 bestm2 = crtc->config.dpll.m2 >> 22;
5786 bestp1 = crtc->config.dpll.p1;
5787 bestp2 = crtc->config.dpll.p2;
5788
5789 /*
5790 * Enable Refclk and SSC
5791 */
a11b0703
VS
5792 I915_WRITE(dpll_reg,
5793 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5794
5795 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5796
9d556c99
CML
5797 /* p1 and p2 divider */
5798 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5799 5 << DPIO_CHV_S1_DIV_SHIFT |
5800 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5801 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5802 1 << DPIO_CHV_K_DIV_SHIFT);
5803
5804 /* Feedback post-divider - m2 */
5805 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5806
5807 /* Feedback refclk divider - n and m1 */
5808 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5809 DPIO_CHV_M1_DIV_BY_2 |
5810 1 << DPIO_CHV_N_DIV_SHIFT);
5811
5812 /* M2 fraction division */
5813 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5814
5815 /* M2 fraction division enable */
5816 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5817 DPIO_CHV_FRAC_DIV_EN |
5818 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5819
5820 /* Loop filter */
5821 refclk = i9xx_get_refclk(&crtc->base, 0);
5822 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5823 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5824 if (refclk == 100000)
5825 intcoeff = 11;
5826 else if (refclk == 38400)
5827 intcoeff = 10;
5828 else
5829 intcoeff = 9;
5830 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5831 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5832
5833 /* AFC Recal */
5834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5835 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5836 DPIO_AFC_RECAL);
5837
5838 mutex_unlock(&dev_priv->dpio_lock);
5839}
5840
f47709a9
DV
5841static void i9xx_update_pll(struct intel_crtc *crtc,
5842 intel_clock_t *reduced_clock,
eb1cbe48
DV
5843 int num_connectors)
5844{
f47709a9 5845 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5846 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5847 u32 dpll;
5848 bool is_sdvo;
f47709a9 5849 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5850
f47709a9 5851 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5852
f47709a9
DV
5853 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5854 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5855
5856 dpll = DPLL_VGA_MODE_DIS;
5857
f47709a9 5858 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5859 dpll |= DPLLB_MODE_LVDS;
5860 else
5861 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5862
ef1b460d 5863 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5864 dpll |= (crtc->config.pixel_multiplier - 1)
5865 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5866 }
198a037f
DV
5867
5868 if (is_sdvo)
4a33e48d 5869 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5870
f47709a9 5871 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5872 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5873
5874 /* compute bitmask from p1 value */
5875 if (IS_PINEVIEW(dev))
5876 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5877 else {
5878 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5879 if (IS_G4X(dev) && reduced_clock)
5880 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5881 }
5882 switch (clock->p2) {
5883 case 5:
5884 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5885 break;
5886 case 7:
5887 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5888 break;
5889 case 10:
5890 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5891 break;
5892 case 14:
5893 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5894 break;
5895 }
5896 if (INTEL_INFO(dev)->gen >= 4)
5897 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5898
09ede541 5899 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5900 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5901 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5902 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5903 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5904 else
5905 dpll |= PLL_REF_INPUT_DREFCLK;
5906
5907 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5908 crtc->config.dpll_hw_state.dpll = dpll;
5909
eb1cbe48 5910 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5911 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5912 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5913 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5914 }
5915}
5916
f47709a9 5917static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5918 intel_clock_t *reduced_clock,
eb1cbe48
DV
5919 int num_connectors)
5920{
f47709a9 5921 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5922 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5923 u32 dpll;
f47709a9 5924 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5925
f47709a9 5926 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5927
eb1cbe48
DV
5928 dpll = DPLL_VGA_MODE_DIS;
5929
f47709a9 5930 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5931 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5932 } else {
5933 if (clock->p1 == 2)
5934 dpll |= PLL_P1_DIVIDE_BY_TWO;
5935 else
5936 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5937 if (clock->p2 == 4)
5938 dpll |= PLL_P2_DIVIDE_BY_4;
5939 }
5940
1c4e0274 5941 if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
4a33e48d
DV
5942 dpll |= DPLL_DVO_2X_MODE;
5943
f47709a9 5944 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5945 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5946 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5947 else
5948 dpll |= PLL_REF_INPUT_DREFCLK;
5949
5950 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5951 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5952}
5953
8a654f3b 5954static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5955{
5956 struct drm_device *dev = intel_crtc->base.dev;
5957 struct drm_i915_private *dev_priv = dev->dev_private;
5958 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5959 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5960 struct drm_display_mode *adjusted_mode =
5961 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5962 uint32_t crtc_vtotal, crtc_vblank_end;
5963 int vsyncshift = 0;
4d8a62ea
DV
5964
5965 /* We need to be careful not to changed the adjusted mode, for otherwise
5966 * the hw state checker will get angry at the mismatch. */
5967 crtc_vtotal = adjusted_mode->crtc_vtotal;
5968 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5969
609aeaca 5970 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5971 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5972 crtc_vtotal -= 1;
5973 crtc_vblank_end -= 1;
609aeaca
VS
5974
5975 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5976 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5977 else
5978 vsyncshift = adjusted_mode->crtc_hsync_start -
5979 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5980 if (vsyncshift < 0)
5981 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5982 }
5983
5984 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5985 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5986
fe2b8f9d 5987 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5988 (adjusted_mode->crtc_hdisplay - 1) |
5989 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5990 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5991 (adjusted_mode->crtc_hblank_start - 1) |
5992 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5993 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5994 (adjusted_mode->crtc_hsync_start - 1) |
5995 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5996
fe2b8f9d 5997 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5998 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5999 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6000 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6001 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6002 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6003 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6004 (adjusted_mode->crtc_vsync_start - 1) |
6005 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6006
b5e508d4
PZ
6007 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6008 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6009 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6010 * bits. */
6011 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6012 (pipe == PIPE_B || pipe == PIPE_C))
6013 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6014
b0e77b9c
PZ
6015 /* pipesrc controls the size that is scaled from, which should
6016 * always be the user's requested size.
6017 */
6018 I915_WRITE(PIPESRC(pipe),
37327abd
VS
6019 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6020 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
6021}
6022
1bd1bd80
DV
6023static void intel_get_pipe_timings(struct intel_crtc *crtc,
6024 struct intel_crtc_config *pipe_config)
6025{
6026 struct drm_device *dev = crtc->base.dev;
6027 struct drm_i915_private *dev_priv = dev->dev_private;
6028 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6029 uint32_t tmp;
6030
6031 tmp = I915_READ(HTOTAL(cpu_transcoder));
6032 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6033 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6034 tmp = I915_READ(HBLANK(cpu_transcoder));
6035 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6036 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6037 tmp = I915_READ(HSYNC(cpu_transcoder));
6038 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6039 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6040
6041 tmp = I915_READ(VTOTAL(cpu_transcoder));
6042 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6043 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6044 tmp = I915_READ(VBLANK(cpu_transcoder));
6045 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6046 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6047 tmp = I915_READ(VSYNC(cpu_transcoder));
6048 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6049 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6050
6051 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6052 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6053 pipe_config->adjusted_mode.crtc_vtotal += 1;
6054 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6055 }
6056
6057 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6058 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6059 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6060
6061 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6062 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6063}
6064
f6a83288
DV
6065void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6066 struct intel_crtc_config *pipe_config)
babea61d 6067{
f6a83288
DV
6068 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6069 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6070 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6071 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6072
f6a83288
DV
6073 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6074 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6075 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6076 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6077
f6a83288 6078 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6079
f6a83288
DV
6080 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6081 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6082}
6083
84b046f3
DV
6084static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6085{
6086 struct drm_device *dev = intel_crtc->base.dev;
6087 struct drm_i915_private *dev_priv = dev->dev_private;
6088 uint32_t pipeconf;
6089
9f11a9e4 6090 pipeconf = 0;
84b046f3 6091
b6b5d049
VS
6092 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6093 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6094 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6095
cf532bb2
VS
6096 if (intel_crtc->config.double_wide)
6097 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6098
ff9ce46e
DV
6099 /* only g4x and later have fancy bpc/dither controls */
6100 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6101 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6102 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6103 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6104 PIPECONF_DITHER_TYPE_SP;
84b046f3 6105
ff9ce46e
DV
6106 switch (intel_crtc->config.pipe_bpp) {
6107 case 18:
6108 pipeconf |= PIPECONF_6BPC;
6109 break;
6110 case 24:
6111 pipeconf |= PIPECONF_8BPC;
6112 break;
6113 case 30:
6114 pipeconf |= PIPECONF_10BPC;
6115 break;
6116 default:
6117 /* Case prevented by intel_choose_pipe_bpp_dither. */
6118 BUG();
84b046f3
DV
6119 }
6120 }
6121
6122 if (HAS_PIPE_CXSR(dev)) {
6123 if (intel_crtc->lowfreq_avail) {
6124 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6125 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6126 } else {
6127 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6128 }
6129 }
6130
efc2cfff
VS
6131 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6132 if (INTEL_INFO(dev)->gen < 4 ||
6133 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6134 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6135 else
6136 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6137 } else
84b046f3
DV
6138 pipeconf |= PIPECONF_PROGRESSIVE;
6139
9f11a9e4
DV
6140 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6141 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6142
84b046f3
DV
6143 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6144 POSTING_READ(PIPECONF(intel_crtc->pipe));
6145}
6146
f564048e 6147static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6148 int x, int y,
94352cf9 6149 struct drm_framebuffer *fb)
79e53945
JB
6150{
6151 struct drm_device *dev = crtc->dev;
6152 struct drm_i915_private *dev_priv = dev->dev_private;
6153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6154 int refclk, num_connectors = 0;
652c393a 6155 intel_clock_t clock, reduced_clock;
a16af721 6156 bool ok, has_reduced_clock = false;
e9fd1c02 6157 bool is_lvds = false, is_dsi = false;
5eddb70b 6158 struct intel_encoder *encoder;
d4906093 6159 const intel_limit_t *limit;
79e53945 6160
6c2b7c12 6161 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6162 switch (encoder->type) {
79e53945
JB
6163 case INTEL_OUTPUT_LVDS:
6164 is_lvds = true;
6165 break;
e9fd1c02
JN
6166 case INTEL_OUTPUT_DSI:
6167 is_dsi = true;
6168 break;
79e53945 6169 }
43565a06 6170
c751ce4f 6171 num_connectors++;
79e53945
JB
6172 }
6173
f2335330 6174 if (is_dsi)
5b18e57c 6175 return 0;
f2335330
JN
6176
6177 if (!intel_crtc->config.clock_set) {
6178 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6179
e9fd1c02
JN
6180 /*
6181 * Returns a set of divisors for the desired target clock with
6182 * the given refclk, or FALSE. The returned values represent
6183 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6184 * 2) / p1 / p2.
6185 */
6186 limit = intel_limit(crtc, refclk);
6187 ok = dev_priv->display.find_dpll(limit, crtc,
6188 intel_crtc->config.port_clock,
6189 refclk, NULL, &clock);
f2335330 6190 if (!ok) {
e9fd1c02
JN
6191 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6192 return -EINVAL;
6193 }
79e53945 6194
f2335330
JN
6195 if (is_lvds && dev_priv->lvds_downclock_avail) {
6196 /*
6197 * Ensure we match the reduced clock's P to the target
6198 * clock. If the clocks don't match, we can't switch
6199 * the display clock by using the FP0/FP1. In such case
6200 * we will disable the LVDS downclock feature.
6201 */
6202 has_reduced_clock =
6203 dev_priv->display.find_dpll(limit, crtc,
6204 dev_priv->lvds_downclock,
6205 refclk, &clock,
6206 &reduced_clock);
6207 }
6208 /* Compat-code for transition, will disappear. */
f47709a9
DV
6209 intel_crtc->config.dpll.n = clock.n;
6210 intel_crtc->config.dpll.m1 = clock.m1;
6211 intel_crtc->config.dpll.m2 = clock.m2;
6212 intel_crtc->config.dpll.p1 = clock.p1;
6213 intel_crtc->config.dpll.p2 = clock.p2;
6214 }
7026d4ac 6215
e9fd1c02 6216 if (IS_GEN2(dev)) {
8a654f3b 6217 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6218 has_reduced_clock ? &reduced_clock : NULL,
6219 num_connectors);
9d556c99
CML
6220 } else if (IS_CHERRYVIEW(dev)) {
6221 chv_update_pll(intel_crtc);
e9fd1c02 6222 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6223 vlv_update_pll(intel_crtc);
e9fd1c02 6224 } else {
f47709a9 6225 i9xx_update_pll(intel_crtc,
eb1cbe48 6226 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6227 num_connectors);
e9fd1c02 6228 }
79e53945 6229
c8f7a0db 6230 return 0;
f564048e
EA
6231}
6232
2fa2fe9a
DV
6233static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6234 struct intel_crtc_config *pipe_config)
6235{
6236 struct drm_device *dev = crtc->base.dev;
6237 struct drm_i915_private *dev_priv = dev->dev_private;
6238 uint32_t tmp;
6239
dc9e7dec
VS
6240 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6241 return;
6242
2fa2fe9a 6243 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6244 if (!(tmp & PFIT_ENABLE))
6245 return;
2fa2fe9a 6246
06922821 6247 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6248 if (INTEL_INFO(dev)->gen < 4) {
6249 if (crtc->pipe != PIPE_B)
6250 return;
2fa2fe9a
DV
6251 } else {
6252 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6253 return;
6254 }
6255
06922821 6256 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6257 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6258 if (INTEL_INFO(dev)->gen < 5)
6259 pipe_config->gmch_pfit.lvds_border_bits =
6260 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6261}
6262
acbec814
JB
6263static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6264 struct intel_crtc_config *pipe_config)
6265{
6266 struct drm_device *dev = crtc->base.dev;
6267 struct drm_i915_private *dev_priv = dev->dev_private;
6268 int pipe = pipe_config->cpu_transcoder;
6269 intel_clock_t clock;
6270 u32 mdiv;
662c6ecb 6271 int refclk = 100000;
acbec814 6272
f573de5a
SK
6273 /* In case of MIPI DPLL will not even be used */
6274 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6275 return;
6276
acbec814 6277 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6278 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6279 mutex_unlock(&dev_priv->dpio_lock);
6280
6281 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6282 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6283 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6284 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6285 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6286
f646628b 6287 vlv_clock(refclk, &clock);
acbec814 6288
f646628b
VS
6289 /* clock.dot is the fast clock */
6290 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6291}
6292
1ad292b5
JB
6293static void i9xx_get_plane_config(struct intel_crtc *crtc,
6294 struct intel_plane_config *plane_config)
6295{
6296 struct drm_device *dev = crtc->base.dev;
6297 struct drm_i915_private *dev_priv = dev->dev_private;
6298 u32 val, base, offset;
6299 int pipe = crtc->pipe, plane = crtc->plane;
6300 int fourcc, pixel_format;
6301 int aligned_height;
6302
66e514c1
DA
6303 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6304 if (!crtc->base.primary->fb) {
1ad292b5
JB
6305 DRM_DEBUG_KMS("failed to alloc fb\n");
6306 return;
6307 }
6308
6309 val = I915_READ(DSPCNTR(plane));
6310
6311 if (INTEL_INFO(dev)->gen >= 4)
6312 if (val & DISPPLANE_TILED)
6313 plane_config->tiled = true;
6314
6315 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6316 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6317 crtc->base.primary->fb->pixel_format = fourcc;
6318 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6319 drm_format_plane_cpp(fourcc, 0) * 8;
6320
6321 if (INTEL_INFO(dev)->gen >= 4) {
6322 if (plane_config->tiled)
6323 offset = I915_READ(DSPTILEOFF(plane));
6324 else
6325 offset = I915_READ(DSPLINOFF(plane));
6326 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6327 } else {
6328 base = I915_READ(DSPADDR(plane));
6329 }
6330 plane_config->base = base;
6331
6332 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6333 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6334 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6335
6336 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6337 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6338
66e514c1 6339 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6340 plane_config->tiled);
6341
1267a26b
FF
6342 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6343 aligned_height);
1ad292b5
JB
6344
6345 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6346 pipe, plane, crtc->base.primary->fb->width,
6347 crtc->base.primary->fb->height,
6348 crtc->base.primary->fb->bits_per_pixel, base,
6349 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6350 plane_config->size);
6351
6352}
6353
70b23a98
VS
6354static void chv_crtc_clock_get(struct intel_crtc *crtc,
6355 struct intel_crtc_config *pipe_config)
6356{
6357 struct drm_device *dev = crtc->base.dev;
6358 struct drm_i915_private *dev_priv = dev->dev_private;
6359 int pipe = pipe_config->cpu_transcoder;
6360 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6361 intel_clock_t clock;
6362 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6363 int refclk = 100000;
6364
6365 mutex_lock(&dev_priv->dpio_lock);
6366 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6367 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6368 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6369 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6370 mutex_unlock(&dev_priv->dpio_lock);
6371
6372 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6373 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6374 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6375 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6376 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6377
6378 chv_clock(refclk, &clock);
6379
6380 /* clock.dot is the fast clock */
6381 pipe_config->port_clock = clock.dot / 5;
6382}
6383
0e8ffe1b
DV
6384static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6385 struct intel_crtc_config *pipe_config)
6386{
6387 struct drm_device *dev = crtc->base.dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
6389 uint32_t tmp;
6390
b5482bd0
ID
6391 if (!intel_display_power_enabled(dev_priv,
6392 POWER_DOMAIN_PIPE(crtc->pipe)))
6393 return false;
6394
e143a21c 6395 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6396 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6397
0e8ffe1b
DV
6398 tmp = I915_READ(PIPECONF(crtc->pipe));
6399 if (!(tmp & PIPECONF_ENABLE))
6400 return false;
6401
42571aef
VS
6402 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6403 switch (tmp & PIPECONF_BPC_MASK) {
6404 case PIPECONF_6BPC:
6405 pipe_config->pipe_bpp = 18;
6406 break;
6407 case PIPECONF_8BPC:
6408 pipe_config->pipe_bpp = 24;
6409 break;
6410 case PIPECONF_10BPC:
6411 pipe_config->pipe_bpp = 30;
6412 break;
6413 default:
6414 break;
6415 }
6416 }
6417
b5a9fa09
DV
6418 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6419 pipe_config->limited_color_range = true;
6420
282740f7
VS
6421 if (INTEL_INFO(dev)->gen < 4)
6422 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6423
1bd1bd80
DV
6424 intel_get_pipe_timings(crtc, pipe_config);
6425
2fa2fe9a
DV
6426 i9xx_get_pfit_config(crtc, pipe_config);
6427
6c49f241
DV
6428 if (INTEL_INFO(dev)->gen >= 4) {
6429 tmp = I915_READ(DPLL_MD(crtc->pipe));
6430 pipe_config->pixel_multiplier =
6431 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6432 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6433 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6434 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6435 tmp = I915_READ(DPLL(crtc->pipe));
6436 pipe_config->pixel_multiplier =
6437 ((tmp & SDVO_MULTIPLIER_MASK)
6438 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6439 } else {
6440 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6441 * port and will be fixed up in the encoder->get_config
6442 * function. */
6443 pipe_config->pixel_multiplier = 1;
6444 }
8bcc2795
DV
6445 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6446 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
6447 /*
6448 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6449 * on 830. Filter it out here so that we don't
6450 * report errors due to that.
6451 */
6452 if (IS_I830(dev))
6453 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6454
8bcc2795
DV
6455 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6456 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6457 } else {
6458 /* Mask out read-only status bits. */
6459 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6460 DPLL_PORTC_READY_MASK |
6461 DPLL_PORTB_READY_MASK);
8bcc2795 6462 }
6c49f241 6463
70b23a98
VS
6464 if (IS_CHERRYVIEW(dev))
6465 chv_crtc_clock_get(crtc, pipe_config);
6466 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6467 vlv_crtc_clock_get(crtc, pipe_config);
6468 else
6469 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6470
0e8ffe1b
DV
6471 return true;
6472}
6473
dde86e2d 6474static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6475{
6476 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6477 struct intel_encoder *encoder;
74cfd7ac 6478 u32 val, final;
13d83a67 6479 bool has_lvds = false;
199e5d79 6480 bool has_cpu_edp = false;
199e5d79 6481 bool has_panel = false;
99eb6a01
KP
6482 bool has_ck505 = false;
6483 bool can_ssc = false;
13d83a67
JB
6484
6485 /* We need to take the global config into account */
b2784e15 6486 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6487 switch (encoder->type) {
6488 case INTEL_OUTPUT_LVDS:
6489 has_panel = true;
6490 has_lvds = true;
6491 break;
6492 case INTEL_OUTPUT_EDP:
6493 has_panel = true;
2de6905f 6494 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6495 has_cpu_edp = true;
6496 break;
13d83a67
JB
6497 }
6498 }
6499
99eb6a01 6500 if (HAS_PCH_IBX(dev)) {
41aa3448 6501 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6502 can_ssc = has_ck505;
6503 } else {
6504 has_ck505 = false;
6505 can_ssc = true;
6506 }
6507
2de6905f
ID
6508 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6509 has_panel, has_lvds, has_ck505);
13d83a67
JB
6510
6511 /* Ironlake: try to setup display ref clock before DPLL
6512 * enabling. This is only under driver's control after
6513 * PCH B stepping, previous chipset stepping should be
6514 * ignoring this setting.
6515 */
74cfd7ac
CW
6516 val = I915_READ(PCH_DREF_CONTROL);
6517
6518 /* As we must carefully and slowly disable/enable each source in turn,
6519 * compute the final state we want first and check if we need to
6520 * make any changes at all.
6521 */
6522 final = val;
6523 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6524 if (has_ck505)
6525 final |= DREF_NONSPREAD_CK505_ENABLE;
6526 else
6527 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6528
6529 final &= ~DREF_SSC_SOURCE_MASK;
6530 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6531 final &= ~DREF_SSC1_ENABLE;
6532
6533 if (has_panel) {
6534 final |= DREF_SSC_SOURCE_ENABLE;
6535
6536 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6537 final |= DREF_SSC1_ENABLE;
6538
6539 if (has_cpu_edp) {
6540 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6541 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6542 else
6543 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6544 } else
6545 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6546 } else {
6547 final |= DREF_SSC_SOURCE_DISABLE;
6548 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6549 }
6550
6551 if (final == val)
6552 return;
6553
13d83a67 6554 /* Always enable nonspread source */
74cfd7ac 6555 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6556
99eb6a01 6557 if (has_ck505)
74cfd7ac 6558 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6559 else
74cfd7ac 6560 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6561
199e5d79 6562 if (has_panel) {
74cfd7ac
CW
6563 val &= ~DREF_SSC_SOURCE_MASK;
6564 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6565
199e5d79 6566 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6567 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6568 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6569 val |= DREF_SSC1_ENABLE;
e77166b5 6570 } else
74cfd7ac 6571 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6572
6573 /* Get SSC going before enabling the outputs */
74cfd7ac 6574 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6575 POSTING_READ(PCH_DREF_CONTROL);
6576 udelay(200);
6577
74cfd7ac 6578 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6579
6580 /* Enable CPU source on CPU attached eDP */
199e5d79 6581 if (has_cpu_edp) {
99eb6a01 6582 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6583 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6584 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6585 } else
74cfd7ac 6586 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6587 } else
74cfd7ac 6588 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6589
74cfd7ac 6590 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6591 POSTING_READ(PCH_DREF_CONTROL);
6592 udelay(200);
6593 } else {
6594 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6595
74cfd7ac 6596 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6597
6598 /* Turn off CPU output */
74cfd7ac 6599 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6600
74cfd7ac 6601 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6602 POSTING_READ(PCH_DREF_CONTROL);
6603 udelay(200);
6604
6605 /* Turn off the SSC source */
74cfd7ac
CW
6606 val &= ~DREF_SSC_SOURCE_MASK;
6607 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6608
6609 /* Turn off SSC1 */
74cfd7ac 6610 val &= ~DREF_SSC1_ENABLE;
199e5d79 6611
74cfd7ac 6612 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6613 POSTING_READ(PCH_DREF_CONTROL);
6614 udelay(200);
6615 }
74cfd7ac
CW
6616
6617 BUG_ON(val != final);
13d83a67
JB
6618}
6619
f31f2d55 6620static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6621{
f31f2d55 6622 uint32_t tmp;
dde86e2d 6623
0ff066a9
PZ
6624 tmp = I915_READ(SOUTH_CHICKEN2);
6625 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6626 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6627
0ff066a9
PZ
6628 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6629 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6630 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6631
0ff066a9
PZ
6632 tmp = I915_READ(SOUTH_CHICKEN2);
6633 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6634 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6635
0ff066a9
PZ
6636 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6637 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6638 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6639}
6640
6641/* WaMPhyProgramming:hsw */
6642static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6643{
6644 uint32_t tmp;
dde86e2d
PZ
6645
6646 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6647 tmp &= ~(0xFF << 24);
6648 tmp |= (0x12 << 24);
6649 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6650
dde86e2d
PZ
6651 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6652 tmp |= (1 << 11);
6653 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6654
6655 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6656 tmp |= (1 << 11);
6657 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6658
dde86e2d
PZ
6659 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6660 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6661 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6662
6663 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6664 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6665 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6666
0ff066a9
PZ
6667 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6668 tmp &= ~(7 << 13);
6669 tmp |= (5 << 13);
6670 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6671
0ff066a9
PZ
6672 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6673 tmp &= ~(7 << 13);
6674 tmp |= (5 << 13);
6675 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6676
6677 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6678 tmp &= ~0xFF;
6679 tmp |= 0x1C;
6680 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6681
6682 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6683 tmp &= ~0xFF;
6684 tmp |= 0x1C;
6685 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6686
6687 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6688 tmp &= ~(0xFF << 16);
6689 tmp |= (0x1C << 16);
6690 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6691
6692 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6693 tmp &= ~(0xFF << 16);
6694 tmp |= (0x1C << 16);
6695 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6696
0ff066a9
PZ
6697 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6698 tmp |= (1 << 27);
6699 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6700
0ff066a9
PZ
6701 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6702 tmp |= (1 << 27);
6703 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6704
0ff066a9
PZ
6705 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6706 tmp &= ~(0xF << 28);
6707 tmp |= (4 << 28);
6708 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6709
0ff066a9
PZ
6710 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6711 tmp &= ~(0xF << 28);
6712 tmp |= (4 << 28);
6713 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6714}
6715
2fa86a1f
PZ
6716/* Implements 3 different sequences from BSpec chapter "Display iCLK
6717 * Programming" based on the parameters passed:
6718 * - Sequence to enable CLKOUT_DP
6719 * - Sequence to enable CLKOUT_DP without spread
6720 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6721 */
6722static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6723 bool with_fdi)
f31f2d55
PZ
6724{
6725 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6726 uint32_t reg, tmp;
6727
6728 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6729 with_spread = true;
6730 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6731 with_fdi, "LP PCH doesn't have FDI\n"))
6732 with_fdi = false;
f31f2d55
PZ
6733
6734 mutex_lock(&dev_priv->dpio_lock);
6735
6736 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6737 tmp &= ~SBI_SSCCTL_DISABLE;
6738 tmp |= SBI_SSCCTL_PATHALT;
6739 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6740
6741 udelay(24);
6742
2fa86a1f
PZ
6743 if (with_spread) {
6744 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6745 tmp &= ~SBI_SSCCTL_PATHALT;
6746 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6747
2fa86a1f
PZ
6748 if (with_fdi) {
6749 lpt_reset_fdi_mphy(dev_priv);
6750 lpt_program_fdi_mphy(dev_priv);
6751 }
6752 }
dde86e2d 6753
2fa86a1f
PZ
6754 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6755 SBI_GEN0 : SBI_DBUFF0;
6756 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6757 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6758 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6759
6760 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6761}
6762
47701c3b
PZ
6763/* Sequence to disable CLKOUT_DP */
6764static void lpt_disable_clkout_dp(struct drm_device *dev)
6765{
6766 struct drm_i915_private *dev_priv = dev->dev_private;
6767 uint32_t reg, tmp;
6768
6769 mutex_lock(&dev_priv->dpio_lock);
6770
6771 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6772 SBI_GEN0 : SBI_DBUFF0;
6773 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6774 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6775 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6776
6777 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6778 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6779 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6780 tmp |= SBI_SSCCTL_PATHALT;
6781 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6782 udelay(32);
6783 }
6784 tmp |= SBI_SSCCTL_DISABLE;
6785 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6786 }
6787
6788 mutex_unlock(&dev_priv->dpio_lock);
6789}
6790
bf8fa3d3
PZ
6791static void lpt_init_pch_refclk(struct drm_device *dev)
6792{
bf8fa3d3
PZ
6793 struct intel_encoder *encoder;
6794 bool has_vga = false;
6795
b2784e15 6796 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6797 switch (encoder->type) {
6798 case INTEL_OUTPUT_ANALOG:
6799 has_vga = true;
6800 break;
6801 }
6802 }
6803
47701c3b
PZ
6804 if (has_vga)
6805 lpt_enable_clkout_dp(dev, true, true);
6806 else
6807 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6808}
6809
dde86e2d
PZ
6810/*
6811 * Initialize reference clocks when the driver loads
6812 */
6813void intel_init_pch_refclk(struct drm_device *dev)
6814{
6815 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6816 ironlake_init_pch_refclk(dev);
6817 else if (HAS_PCH_LPT(dev))
6818 lpt_init_pch_refclk(dev);
6819}
6820
d9d444cb
JB
6821static int ironlake_get_refclk(struct drm_crtc *crtc)
6822{
6823 struct drm_device *dev = crtc->dev;
6824 struct drm_i915_private *dev_priv = dev->dev_private;
6825 struct intel_encoder *encoder;
d9d444cb
JB
6826 int num_connectors = 0;
6827 bool is_lvds = false;
6828
6c2b7c12 6829 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6830 switch (encoder->type) {
6831 case INTEL_OUTPUT_LVDS:
6832 is_lvds = true;
6833 break;
d9d444cb
JB
6834 }
6835 num_connectors++;
6836 }
6837
6838 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6839 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6840 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6841 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6842 }
6843
6844 return 120000;
6845}
6846
6ff93609 6847static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6848{
c8203565 6849 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6850 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6851 int pipe = intel_crtc->pipe;
c8203565
PZ
6852 uint32_t val;
6853
78114071 6854 val = 0;
c8203565 6855
965e0c48 6856 switch (intel_crtc->config.pipe_bpp) {
c8203565 6857 case 18:
dfd07d72 6858 val |= PIPECONF_6BPC;
c8203565
PZ
6859 break;
6860 case 24:
dfd07d72 6861 val |= PIPECONF_8BPC;
c8203565
PZ
6862 break;
6863 case 30:
dfd07d72 6864 val |= PIPECONF_10BPC;
c8203565
PZ
6865 break;
6866 case 36:
dfd07d72 6867 val |= PIPECONF_12BPC;
c8203565
PZ
6868 break;
6869 default:
cc769b62
PZ
6870 /* Case prevented by intel_choose_pipe_bpp_dither. */
6871 BUG();
c8203565
PZ
6872 }
6873
d8b32247 6874 if (intel_crtc->config.dither)
c8203565
PZ
6875 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6876
6ff93609 6877 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6878 val |= PIPECONF_INTERLACED_ILK;
6879 else
6880 val |= PIPECONF_PROGRESSIVE;
6881
50f3b016 6882 if (intel_crtc->config.limited_color_range)
3685a8f3 6883 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6884
c8203565
PZ
6885 I915_WRITE(PIPECONF(pipe), val);
6886 POSTING_READ(PIPECONF(pipe));
6887}
6888
86d3efce
VS
6889/*
6890 * Set up the pipe CSC unit.
6891 *
6892 * Currently only full range RGB to limited range RGB conversion
6893 * is supported, but eventually this should handle various
6894 * RGB<->YCbCr scenarios as well.
6895 */
50f3b016 6896static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6897{
6898 struct drm_device *dev = crtc->dev;
6899 struct drm_i915_private *dev_priv = dev->dev_private;
6900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6901 int pipe = intel_crtc->pipe;
6902 uint16_t coeff = 0x7800; /* 1.0 */
6903
6904 /*
6905 * TODO: Check what kind of values actually come out of the pipe
6906 * with these coeff/postoff values and adjust to get the best
6907 * accuracy. Perhaps we even need to take the bpc value into
6908 * consideration.
6909 */
6910
50f3b016 6911 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6912 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6913
6914 /*
6915 * GY/GU and RY/RU should be the other way around according
6916 * to BSpec, but reality doesn't agree. Just set them up in
6917 * a way that results in the correct picture.
6918 */
6919 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6920 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6921
6922 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6923 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6924
6925 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6926 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6927
6928 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6929 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6930 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6931
6932 if (INTEL_INFO(dev)->gen > 6) {
6933 uint16_t postoff = 0;
6934
50f3b016 6935 if (intel_crtc->config.limited_color_range)
32cf0cb0 6936 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6937
6938 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6939 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6940 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6941
6942 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6943 } else {
6944 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6945
50f3b016 6946 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6947 mode |= CSC_BLACK_SCREEN_OFFSET;
6948
6949 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6950 }
6951}
6952
6ff93609 6953static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6954{
756f85cf
PZ
6955 struct drm_device *dev = crtc->dev;
6956 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6958 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6959 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6960 uint32_t val;
6961
3eff4faa 6962 val = 0;
ee2b0b38 6963
756f85cf 6964 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6965 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6966
6ff93609 6967 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6968 val |= PIPECONF_INTERLACED_ILK;
6969 else
6970 val |= PIPECONF_PROGRESSIVE;
6971
702e7a56
PZ
6972 I915_WRITE(PIPECONF(cpu_transcoder), val);
6973 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6974
6975 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6976 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6977
6978 if (IS_BROADWELL(dev)) {
6979 val = 0;
6980
6981 switch (intel_crtc->config.pipe_bpp) {
6982 case 18:
6983 val |= PIPEMISC_DITHER_6_BPC;
6984 break;
6985 case 24:
6986 val |= PIPEMISC_DITHER_8_BPC;
6987 break;
6988 case 30:
6989 val |= PIPEMISC_DITHER_10_BPC;
6990 break;
6991 case 36:
6992 val |= PIPEMISC_DITHER_12_BPC;
6993 break;
6994 default:
6995 /* Case prevented by pipe_config_set_bpp. */
6996 BUG();
6997 }
6998
6999 if (intel_crtc->config.dither)
7000 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7001
7002 I915_WRITE(PIPEMISC(pipe), val);
7003 }
ee2b0b38
PZ
7004}
7005
6591c6e4 7006static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
7007 intel_clock_t *clock,
7008 bool *has_reduced_clock,
7009 intel_clock_t *reduced_clock)
7010{
7011 struct drm_device *dev = crtc->dev;
7012 struct drm_i915_private *dev_priv = dev->dev_private;
7013 struct intel_encoder *intel_encoder;
7014 int refclk;
d4906093 7015 const intel_limit_t *limit;
a16af721 7016 bool ret, is_lvds = false;
79e53945 7017
6591c6e4
PZ
7018 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7019 switch (intel_encoder->type) {
79e53945
JB
7020 case INTEL_OUTPUT_LVDS:
7021 is_lvds = true;
7022 break;
79e53945
JB
7023 }
7024 }
7025
d9d444cb 7026 refclk = ironlake_get_refclk(crtc);
79e53945 7027
d4906093
ML
7028 /*
7029 * Returns a set of divisors for the desired target clock with the given
7030 * refclk, or FALSE. The returned values represent the clock equation:
7031 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7032 */
1b894b59 7033 limit = intel_limit(crtc, refclk);
ff9a6750
DV
7034 ret = dev_priv->display.find_dpll(limit, crtc,
7035 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 7036 refclk, NULL, clock);
6591c6e4
PZ
7037 if (!ret)
7038 return false;
cda4b7d3 7039
ddc9003c 7040 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7041 /*
7042 * Ensure we match the reduced clock's P to the target clock.
7043 * If the clocks don't match, we can't switch the display clock
7044 * by using the FP0/FP1. In such case we will disable the LVDS
7045 * downclock feature.
7046 */
ee9300bb
DV
7047 *has_reduced_clock =
7048 dev_priv->display.find_dpll(limit, crtc,
7049 dev_priv->lvds_downclock,
7050 refclk, clock,
7051 reduced_clock);
652c393a 7052 }
61e9653f 7053
6591c6e4
PZ
7054 return true;
7055}
7056
d4b1931c
PZ
7057int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7058{
7059 /*
7060 * Account for spread spectrum to avoid
7061 * oversubscribing the link. Max center spread
7062 * is 2.5%; use 5% for safety's sake.
7063 */
7064 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7065 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7066}
7067
7429e9d4 7068static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7069{
7429e9d4 7070 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7071}
7072
de13a2e3 7073static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7074 u32 *fp,
9a7c7890 7075 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7076{
de13a2e3 7077 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7078 struct drm_device *dev = crtc->dev;
7079 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7080 struct intel_encoder *intel_encoder;
7081 uint32_t dpll;
6cc5f341 7082 int factor, num_connectors = 0;
09ede541 7083 bool is_lvds = false, is_sdvo = false;
79e53945 7084
de13a2e3
PZ
7085 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7086 switch (intel_encoder->type) {
79e53945
JB
7087 case INTEL_OUTPUT_LVDS:
7088 is_lvds = true;
7089 break;
7090 case INTEL_OUTPUT_SDVO:
7d57382e 7091 case INTEL_OUTPUT_HDMI:
79e53945 7092 is_sdvo = true;
79e53945 7093 break;
79e53945 7094 }
43565a06 7095
c751ce4f 7096 num_connectors++;
79e53945 7097 }
79e53945 7098
c1858123 7099 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7100 factor = 21;
7101 if (is_lvds) {
7102 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7103 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7104 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7105 factor = 25;
09ede541 7106 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7107 factor = 20;
c1858123 7108
7429e9d4 7109 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7110 *fp |= FP_CB_TUNE;
2c07245f 7111
9a7c7890
DV
7112 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7113 *fp2 |= FP_CB_TUNE;
7114
5eddb70b 7115 dpll = 0;
2c07245f 7116
a07d6787
EA
7117 if (is_lvds)
7118 dpll |= DPLLB_MODE_LVDS;
7119 else
7120 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7121
ef1b460d
DV
7122 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7123 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7124
7125 if (is_sdvo)
4a33e48d 7126 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7127 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7128 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7129
a07d6787 7130 /* compute bitmask from p1 value */
7429e9d4 7131 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7132 /* also FPA1 */
7429e9d4 7133 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7134
7429e9d4 7135 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7136 case 5:
7137 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7138 break;
7139 case 7:
7140 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7141 break;
7142 case 10:
7143 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7144 break;
7145 case 14:
7146 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7147 break;
79e53945
JB
7148 }
7149
b4c09f3b 7150 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7151 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7152 else
7153 dpll |= PLL_REF_INPUT_DREFCLK;
7154
959e16d6 7155 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7156}
7157
7158static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7159 int x, int y,
7160 struct drm_framebuffer *fb)
7161{
7162 struct drm_device *dev = crtc->dev;
de13a2e3 7163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7164 int num_connectors = 0;
7165 intel_clock_t clock, reduced_clock;
cbbab5bd 7166 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7167 bool ok, has_reduced_clock = false;
8b47047b 7168 bool is_lvds = false;
de13a2e3 7169 struct intel_encoder *encoder;
e2b78267 7170 struct intel_shared_dpll *pll;
de13a2e3
PZ
7171
7172 for_each_encoder_on_crtc(dev, crtc, encoder) {
7173 switch (encoder->type) {
7174 case INTEL_OUTPUT_LVDS:
7175 is_lvds = true;
7176 break;
de13a2e3
PZ
7177 }
7178
7179 num_connectors++;
a07d6787 7180 }
79e53945 7181
5dc5298b
PZ
7182 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7183 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7184
ff9a6750 7185 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7186 &has_reduced_clock, &reduced_clock);
ee9300bb 7187 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7188 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7189 return -EINVAL;
79e53945 7190 }
f47709a9
DV
7191 /* Compat-code for transition, will disappear. */
7192 if (!intel_crtc->config.clock_set) {
7193 intel_crtc->config.dpll.n = clock.n;
7194 intel_crtc->config.dpll.m1 = clock.m1;
7195 intel_crtc->config.dpll.m2 = clock.m2;
7196 intel_crtc->config.dpll.p1 = clock.p1;
7197 intel_crtc->config.dpll.p2 = clock.p2;
7198 }
79e53945 7199
5dc5298b 7200 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7201 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7202 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7203 if (has_reduced_clock)
7429e9d4 7204 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7205
7429e9d4 7206 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7207 &fp, &reduced_clock,
7208 has_reduced_clock ? &fp2 : NULL);
7209
959e16d6 7210 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7211 intel_crtc->config.dpll_hw_state.fp0 = fp;
7212 if (has_reduced_clock)
7213 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7214 else
7215 intel_crtc->config.dpll_hw_state.fp1 = fp;
7216
b89a1d39 7217 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7218 if (pll == NULL) {
84f44ce7 7219 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7220 pipe_name(intel_crtc->pipe));
4b645f14
JB
7221 return -EINVAL;
7222 }
ee7b9f93 7223 } else
e72f9fbf 7224 intel_put_shared_dpll(intel_crtc);
79e53945 7225
d330a953 7226 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7227 intel_crtc->lowfreq_avail = true;
7228 else
7229 intel_crtc->lowfreq_avail = false;
e2b78267 7230
c8f7a0db 7231 return 0;
79e53945
JB
7232}
7233
eb14cb74
VS
7234static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7235 struct intel_link_m_n *m_n)
7236{
7237 struct drm_device *dev = crtc->base.dev;
7238 struct drm_i915_private *dev_priv = dev->dev_private;
7239 enum pipe pipe = crtc->pipe;
7240
7241 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7242 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7243 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7244 & ~TU_SIZE_MASK;
7245 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7246 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7247 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7248}
7249
7250static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7251 enum transcoder transcoder,
b95af8be
VK
7252 struct intel_link_m_n *m_n,
7253 struct intel_link_m_n *m2_n2)
72419203
DV
7254{
7255 struct drm_device *dev = crtc->base.dev;
7256 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7257 enum pipe pipe = crtc->pipe;
72419203 7258
eb14cb74
VS
7259 if (INTEL_INFO(dev)->gen >= 5) {
7260 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7261 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7262 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7263 & ~TU_SIZE_MASK;
7264 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7265 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7266 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7267 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7268 * gen < 8) and if DRRS is supported (to make sure the
7269 * registers are not unnecessarily read).
7270 */
7271 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7272 crtc->config.has_drrs) {
7273 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7274 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7275 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7276 & ~TU_SIZE_MASK;
7277 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7278 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7279 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7280 }
eb14cb74
VS
7281 } else {
7282 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7283 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7284 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7285 & ~TU_SIZE_MASK;
7286 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7287 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7288 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7289 }
7290}
7291
7292void intel_dp_get_m_n(struct intel_crtc *crtc,
7293 struct intel_crtc_config *pipe_config)
7294{
7295 if (crtc->config.has_pch_encoder)
7296 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7297 else
7298 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7299 &pipe_config->dp_m_n,
7300 &pipe_config->dp_m2_n2);
eb14cb74 7301}
72419203 7302
eb14cb74
VS
7303static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7304 struct intel_crtc_config *pipe_config)
7305{
7306 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7307 &pipe_config->fdi_m_n, NULL);
72419203
DV
7308}
7309
2fa2fe9a
DV
7310static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7311 struct intel_crtc_config *pipe_config)
7312{
7313 struct drm_device *dev = crtc->base.dev;
7314 struct drm_i915_private *dev_priv = dev->dev_private;
7315 uint32_t tmp;
7316
7317 tmp = I915_READ(PF_CTL(crtc->pipe));
7318
7319 if (tmp & PF_ENABLE) {
fd4daa9c 7320 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7321 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7322 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7323
7324 /* We currently do not free assignements of panel fitters on
7325 * ivb/hsw (since we don't use the higher upscaling modes which
7326 * differentiates them) so just WARN about this case for now. */
7327 if (IS_GEN7(dev)) {
7328 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7329 PF_PIPE_SEL_IVB(crtc->pipe));
7330 }
2fa2fe9a 7331 }
79e53945
JB
7332}
7333
4c6baa59
JB
7334static void ironlake_get_plane_config(struct intel_crtc *crtc,
7335 struct intel_plane_config *plane_config)
7336{
7337 struct drm_device *dev = crtc->base.dev;
7338 struct drm_i915_private *dev_priv = dev->dev_private;
7339 u32 val, base, offset;
7340 int pipe = crtc->pipe, plane = crtc->plane;
7341 int fourcc, pixel_format;
7342 int aligned_height;
7343
66e514c1
DA
7344 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7345 if (!crtc->base.primary->fb) {
4c6baa59
JB
7346 DRM_DEBUG_KMS("failed to alloc fb\n");
7347 return;
7348 }
7349
7350 val = I915_READ(DSPCNTR(plane));
7351
7352 if (INTEL_INFO(dev)->gen >= 4)
7353 if (val & DISPPLANE_TILED)
7354 plane_config->tiled = true;
7355
7356 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7357 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7358 crtc->base.primary->fb->pixel_format = fourcc;
7359 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7360 drm_format_plane_cpp(fourcc, 0) * 8;
7361
7362 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7363 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7364 offset = I915_READ(DSPOFFSET(plane));
7365 } else {
7366 if (plane_config->tiled)
7367 offset = I915_READ(DSPTILEOFF(plane));
7368 else
7369 offset = I915_READ(DSPLINOFF(plane));
7370 }
7371 plane_config->base = base;
7372
7373 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7374 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7375 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7376
7377 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7378 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7379
66e514c1 7380 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7381 plane_config->tiled);
7382
1267a26b
FF
7383 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7384 aligned_height);
4c6baa59
JB
7385
7386 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7387 pipe, plane, crtc->base.primary->fb->width,
7388 crtc->base.primary->fb->height,
7389 crtc->base.primary->fb->bits_per_pixel, base,
7390 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7391 plane_config->size);
7392}
7393
0e8ffe1b
DV
7394static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7395 struct intel_crtc_config *pipe_config)
7396{
7397 struct drm_device *dev = crtc->base.dev;
7398 struct drm_i915_private *dev_priv = dev->dev_private;
7399 uint32_t tmp;
7400
930e8c9e
PZ
7401 if (!intel_display_power_enabled(dev_priv,
7402 POWER_DOMAIN_PIPE(crtc->pipe)))
7403 return false;
7404
e143a21c 7405 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7406 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7407
0e8ffe1b
DV
7408 tmp = I915_READ(PIPECONF(crtc->pipe));
7409 if (!(tmp & PIPECONF_ENABLE))
7410 return false;
7411
42571aef
VS
7412 switch (tmp & PIPECONF_BPC_MASK) {
7413 case PIPECONF_6BPC:
7414 pipe_config->pipe_bpp = 18;
7415 break;
7416 case PIPECONF_8BPC:
7417 pipe_config->pipe_bpp = 24;
7418 break;
7419 case PIPECONF_10BPC:
7420 pipe_config->pipe_bpp = 30;
7421 break;
7422 case PIPECONF_12BPC:
7423 pipe_config->pipe_bpp = 36;
7424 break;
7425 default:
7426 break;
7427 }
7428
b5a9fa09
DV
7429 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7430 pipe_config->limited_color_range = true;
7431
ab9412ba 7432 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7433 struct intel_shared_dpll *pll;
7434
88adfff1
DV
7435 pipe_config->has_pch_encoder = true;
7436
627eb5a3
DV
7437 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7438 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7439 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7440
7441 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7442
c0d43d62 7443 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7444 pipe_config->shared_dpll =
7445 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7446 } else {
7447 tmp = I915_READ(PCH_DPLL_SEL);
7448 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7449 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7450 else
7451 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7452 }
66e985c0
DV
7453
7454 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7455
7456 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7457 &pipe_config->dpll_hw_state));
c93f54cf
DV
7458
7459 tmp = pipe_config->dpll_hw_state.dpll;
7460 pipe_config->pixel_multiplier =
7461 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7462 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7463
7464 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7465 } else {
7466 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7467 }
7468
1bd1bd80
DV
7469 intel_get_pipe_timings(crtc, pipe_config);
7470
2fa2fe9a
DV
7471 ironlake_get_pfit_config(crtc, pipe_config);
7472
0e8ffe1b
DV
7473 return true;
7474}
7475
be256dc7
PZ
7476static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7477{
7478 struct drm_device *dev = dev_priv->dev;
be256dc7 7479 struct intel_crtc *crtc;
be256dc7 7480
d3fcc808 7481 for_each_intel_crtc(dev, crtc)
798183c5 7482 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7483 pipe_name(crtc->pipe));
7484
7485 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7486 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7487 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7488 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7489 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7490 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7491 "CPU PWM1 enabled\n");
c5107b87
PZ
7492 if (IS_HASWELL(dev))
7493 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7494 "CPU PWM2 enabled\n");
be256dc7
PZ
7495 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7496 "PCH PWM1 enabled\n");
7497 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7498 "Utility pin enabled\n");
7499 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7500
9926ada1
PZ
7501 /*
7502 * In theory we can still leave IRQs enabled, as long as only the HPD
7503 * interrupts remain enabled. We used to check for that, but since it's
7504 * gen-specific and since we only disable LCPLL after we fully disable
7505 * the interrupts, the check below should be enough.
7506 */
9df7575f 7507 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7508}
7509
9ccd5aeb
PZ
7510static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7511{
7512 struct drm_device *dev = dev_priv->dev;
7513
7514 if (IS_HASWELL(dev))
7515 return I915_READ(D_COMP_HSW);
7516 else
7517 return I915_READ(D_COMP_BDW);
7518}
7519
3c4c9b81
PZ
7520static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7521{
7522 struct drm_device *dev = dev_priv->dev;
7523
7524 if (IS_HASWELL(dev)) {
7525 mutex_lock(&dev_priv->rps.hw_lock);
7526 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7527 val))
f475dadf 7528 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7529 mutex_unlock(&dev_priv->rps.hw_lock);
7530 } else {
9ccd5aeb
PZ
7531 I915_WRITE(D_COMP_BDW, val);
7532 POSTING_READ(D_COMP_BDW);
3c4c9b81 7533 }
be256dc7
PZ
7534}
7535
7536/*
7537 * This function implements pieces of two sequences from BSpec:
7538 * - Sequence for display software to disable LCPLL
7539 * - Sequence for display software to allow package C8+
7540 * The steps implemented here are just the steps that actually touch the LCPLL
7541 * register. Callers should take care of disabling all the display engine
7542 * functions, doing the mode unset, fixing interrupts, etc.
7543 */
6ff58d53
PZ
7544static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7545 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7546{
7547 uint32_t val;
7548
7549 assert_can_disable_lcpll(dev_priv);
7550
7551 val = I915_READ(LCPLL_CTL);
7552
7553 if (switch_to_fclk) {
7554 val |= LCPLL_CD_SOURCE_FCLK;
7555 I915_WRITE(LCPLL_CTL, val);
7556
7557 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7558 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7559 DRM_ERROR("Switching to FCLK failed\n");
7560
7561 val = I915_READ(LCPLL_CTL);
7562 }
7563
7564 val |= LCPLL_PLL_DISABLE;
7565 I915_WRITE(LCPLL_CTL, val);
7566 POSTING_READ(LCPLL_CTL);
7567
7568 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7569 DRM_ERROR("LCPLL still locked\n");
7570
9ccd5aeb 7571 val = hsw_read_dcomp(dev_priv);
be256dc7 7572 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7573 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7574 ndelay(100);
7575
9ccd5aeb
PZ
7576 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7577 1))
be256dc7
PZ
7578 DRM_ERROR("D_COMP RCOMP still in progress\n");
7579
7580 if (allow_power_down) {
7581 val = I915_READ(LCPLL_CTL);
7582 val |= LCPLL_POWER_DOWN_ALLOW;
7583 I915_WRITE(LCPLL_CTL, val);
7584 POSTING_READ(LCPLL_CTL);
7585 }
7586}
7587
7588/*
7589 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7590 * source.
7591 */
6ff58d53 7592static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7593{
7594 uint32_t val;
a8a8bd54 7595 unsigned long irqflags;
be256dc7
PZ
7596
7597 val = I915_READ(LCPLL_CTL);
7598
7599 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7600 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7601 return;
7602
a8a8bd54
PZ
7603 /*
7604 * Make sure we're not on PC8 state before disabling PC8, otherwise
7605 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7606 *
7607 * The other problem is that hsw_restore_lcpll() is called as part of
7608 * the runtime PM resume sequence, so we can't just call
7609 * gen6_gt_force_wake_get() because that function calls
7610 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7611 * while we are on the resume sequence. So to solve this problem we have
7612 * to call special forcewake code that doesn't touch runtime PM and
7613 * doesn't enable the forcewake delayed work.
7614 */
7615 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7616 if (dev_priv->uncore.forcewake_count++ == 0)
7617 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7618 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7619
be256dc7
PZ
7620 if (val & LCPLL_POWER_DOWN_ALLOW) {
7621 val &= ~LCPLL_POWER_DOWN_ALLOW;
7622 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7623 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7624 }
7625
9ccd5aeb 7626 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7627 val |= D_COMP_COMP_FORCE;
7628 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7629 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7630
7631 val = I915_READ(LCPLL_CTL);
7632 val &= ~LCPLL_PLL_DISABLE;
7633 I915_WRITE(LCPLL_CTL, val);
7634
7635 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7636 DRM_ERROR("LCPLL not locked yet\n");
7637
7638 if (val & LCPLL_CD_SOURCE_FCLK) {
7639 val = I915_READ(LCPLL_CTL);
7640 val &= ~LCPLL_CD_SOURCE_FCLK;
7641 I915_WRITE(LCPLL_CTL, val);
7642
7643 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7644 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7645 DRM_ERROR("Switching back to LCPLL failed\n");
7646 }
215733fa 7647
a8a8bd54
PZ
7648 /* See the big comment above. */
7649 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7650 if (--dev_priv->uncore.forcewake_count == 0)
7651 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7652 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7653}
7654
765dab67
PZ
7655/*
7656 * Package states C8 and deeper are really deep PC states that can only be
7657 * reached when all the devices on the system allow it, so even if the graphics
7658 * device allows PC8+, it doesn't mean the system will actually get to these
7659 * states. Our driver only allows PC8+ when going into runtime PM.
7660 *
7661 * The requirements for PC8+ are that all the outputs are disabled, the power
7662 * well is disabled and most interrupts are disabled, and these are also
7663 * requirements for runtime PM. When these conditions are met, we manually do
7664 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7665 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7666 * hang the machine.
7667 *
7668 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7669 * the state of some registers, so when we come back from PC8+ we need to
7670 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7671 * need to take care of the registers kept by RC6. Notice that this happens even
7672 * if we don't put the device in PCI D3 state (which is what currently happens
7673 * because of the runtime PM support).
7674 *
7675 * For more, read "Display Sequences for Package C8" on the hardware
7676 * documentation.
7677 */
a14cb6fc 7678void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7679{
c67a470b
PZ
7680 struct drm_device *dev = dev_priv->dev;
7681 uint32_t val;
7682
c67a470b
PZ
7683 DRM_DEBUG_KMS("Enabling package C8+\n");
7684
c67a470b
PZ
7685 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7686 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7687 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7688 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7689 }
7690
7691 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7692 hsw_disable_lcpll(dev_priv, true, true);
7693}
7694
a14cb6fc 7695void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7696{
7697 struct drm_device *dev = dev_priv->dev;
7698 uint32_t val;
7699
c67a470b
PZ
7700 DRM_DEBUG_KMS("Disabling package C8+\n");
7701
7702 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7703 lpt_init_pch_refclk(dev);
7704
7705 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7706 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7707 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7708 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7709 }
7710
7711 intel_prepare_ddi(dev);
c67a470b
PZ
7712}
7713
9a952a0d
PZ
7714static void snb_modeset_global_resources(struct drm_device *dev)
7715{
7716 modeset_update_crtc_power_domains(dev);
7717}
7718
4f074129
ID
7719static void haswell_modeset_global_resources(struct drm_device *dev)
7720{
da723569 7721 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7722}
7723
09b4ddf9 7724static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7725 int x, int y,
7726 struct drm_framebuffer *fb)
7727{
09b4ddf9 7728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7729
566b734a 7730 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7731 return -EINVAL;
716c2e55 7732
644cef34
DV
7733 intel_crtc->lowfreq_avail = false;
7734
c8f7a0db 7735 return 0;
79e53945
JB
7736}
7737
7d2c8175
DL
7738static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7739 enum port port,
7740 struct intel_crtc_config *pipe_config)
7741{
7742 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7743
7744 switch (pipe_config->ddi_pll_sel) {
7745 case PORT_CLK_SEL_WRPLL1:
7746 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7747 break;
7748 case PORT_CLK_SEL_WRPLL2:
7749 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7750 break;
7751 }
7752}
7753
26804afd
DV
7754static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7755 struct intel_crtc_config *pipe_config)
7756{
7757 struct drm_device *dev = crtc->base.dev;
7758 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7759 struct intel_shared_dpll *pll;
26804afd
DV
7760 enum port port;
7761 uint32_t tmp;
7762
7763 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7764
7765 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7766
7d2c8175 7767 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7768
d452c5b6
DV
7769 if (pipe_config->shared_dpll >= 0) {
7770 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7771
7772 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7773 &pipe_config->dpll_hw_state));
7774 }
7775
26804afd
DV
7776 /*
7777 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7778 * DDI E. So just check whether this pipe is wired to DDI E and whether
7779 * the PCH transcoder is on.
7780 */
7781 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7782 pipe_config->has_pch_encoder = true;
7783
7784 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7785 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7786 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7787
7788 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7789 }
7790}
7791
0e8ffe1b
DV
7792static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7793 struct intel_crtc_config *pipe_config)
7794{
7795 struct drm_device *dev = crtc->base.dev;
7796 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7797 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7798 uint32_t tmp;
7799
b5482bd0
ID
7800 if (!intel_display_power_enabled(dev_priv,
7801 POWER_DOMAIN_PIPE(crtc->pipe)))
7802 return false;
7803
e143a21c 7804 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7805 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7806
eccb140b
DV
7807 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7808 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7809 enum pipe trans_edp_pipe;
7810 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7811 default:
7812 WARN(1, "unknown pipe linked to edp transcoder\n");
7813 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7814 case TRANS_DDI_EDP_INPUT_A_ON:
7815 trans_edp_pipe = PIPE_A;
7816 break;
7817 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7818 trans_edp_pipe = PIPE_B;
7819 break;
7820 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7821 trans_edp_pipe = PIPE_C;
7822 break;
7823 }
7824
7825 if (trans_edp_pipe == crtc->pipe)
7826 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7827 }
7828
da7e29bd 7829 if (!intel_display_power_enabled(dev_priv,
eccb140b 7830 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7831 return false;
7832
eccb140b 7833 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7834 if (!(tmp & PIPECONF_ENABLE))
7835 return false;
7836
26804afd 7837 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7838
1bd1bd80
DV
7839 intel_get_pipe_timings(crtc, pipe_config);
7840
2fa2fe9a 7841 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7842 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7843 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7844
e59150dc
JB
7845 if (IS_HASWELL(dev))
7846 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7847 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7848
6c49f241
DV
7849 pipe_config->pixel_multiplier = 1;
7850
0e8ffe1b
DV
7851 return true;
7852}
7853
1a91510d
JN
7854static struct {
7855 int clock;
7856 u32 config;
7857} hdmi_audio_clock[] = {
7858 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7859 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7860 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7861 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7862 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7863 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7864 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7865 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7866 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7867 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7868};
7869
7870/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7871static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7872{
7873 int i;
7874
7875 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7876 if (mode->clock == hdmi_audio_clock[i].clock)
7877 break;
7878 }
7879
7880 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7881 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7882 i = 1;
7883 }
7884
7885 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7886 hdmi_audio_clock[i].clock,
7887 hdmi_audio_clock[i].config);
7888
7889 return hdmi_audio_clock[i].config;
7890}
7891
3a9627f4
WF
7892static bool intel_eld_uptodate(struct drm_connector *connector,
7893 int reg_eldv, uint32_t bits_eldv,
7894 int reg_elda, uint32_t bits_elda,
7895 int reg_edid)
7896{
7897 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7898 uint8_t *eld = connector->eld;
7899 uint32_t i;
7900
7901 i = I915_READ(reg_eldv);
7902 i &= bits_eldv;
7903
7904 if (!eld[0])
7905 return !i;
7906
7907 if (!i)
7908 return false;
7909
7910 i = I915_READ(reg_elda);
7911 i &= ~bits_elda;
7912 I915_WRITE(reg_elda, i);
7913
7914 for (i = 0; i < eld[2]; i++)
7915 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7916 return false;
7917
7918 return true;
7919}
7920
e0dac65e 7921static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7922 struct drm_crtc *crtc,
7923 struct drm_display_mode *mode)
e0dac65e
WF
7924{
7925 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7926 uint8_t *eld = connector->eld;
7927 uint32_t eldv;
7928 uint32_t len;
7929 uint32_t i;
7930
7931 i = I915_READ(G4X_AUD_VID_DID);
7932
7933 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7934 eldv = G4X_ELDV_DEVCL_DEVBLC;
7935 else
7936 eldv = G4X_ELDV_DEVCTG;
7937
3a9627f4
WF
7938 if (intel_eld_uptodate(connector,
7939 G4X_AUD_CNTL_ST, eldv,
7940 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7941 G4X_HDMIW_HDMIEDID))
7942 return;
7943
e0dac65e
WF
7944 i = I915_READ(G4X_AUD_CNTL_ST);
7945 i &= ~(eldv | G4X_ELD_ADDR);
7946 len = (i >> 9) & 0x1f; /* ELD buffer size */
7947 I915_WRITE(G4X_AUD_CNTL_ST, i);
7948
7949 if (!eld[0])
7950 return;
7951
7952 len = min_t(uint8_t, eld[2], len);
7953 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7954 for (i = 0; i < len; i++)
7955 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7956
7957 i = I915_READ(G4X_AUD_CNTL_ST);
7958 i |= eldv;
7959 I915_WRITE(G4X_AUD_CNTL_ST, i);
7960}
7961
83358c85 7962static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7963 struct drm_crtc *crtc,
7964 struct drm_display_mode *mode)
83358c85
WX
7965{
7966 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7967 uint8_t *eld = connector->eld;
83358c85
WX
7968 uint32_t eldv;
7969 uint32_t i;
7970 int len;
7971 int pipe = to_intel_crtc(crtc)->pipe;
7972 int tmp;
7973
7974 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7975 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7976 int aud_config = HSW_AUD_CFG(pipe);
7977 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7978
83358c85
WX
7979 /* Audio output enable */
7980 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7981 tmp = I915_READ(aud_cntrl_st2);
7982 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7983 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7984 POSTING_READ(aud_cntrl_st2);
83358c85 7985
c7905792 7986 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7987
7988 /* Set ELD valid state */
7989 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7990 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7991 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7992 I915_WRITE(aud_cntrl_st2, tmp);
7993 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7994 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7995
7996 /* Enable HDMI mode */
7997 tmp = I915_READ(aud_config);
7e7cb34f 7998 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7999 /* clear N_programing_enable and N_value_index */
8000 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8001 I915_WRITE(aud_config, tmp);
8002
8003 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8004
8005 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8006
8007 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8008 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8009 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8010 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8011 } else {
8012 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8013 }
83358c85
WX
8014
8015 if (intel_eld_uptodate(connector,
8016 aud_cntrl_st2, eldv,
8017 aud_cntl_st, IBX_ELD_ADDRESS,
8018 hdmiw_hdmiedid))
8019 return;
8020
8021 i = I915_READ(aud_cntrl_st2);
8022 i &= ~eldv;
8023 I915_WRITE(aud_cntrl_st2, i);
8024
8025 if (!eld[0])
8026 return;
8027
8028 i = I915_READ(aud_cntl_st);
8029 i &= ~IBX_ELD_ADDRESS;
8030 I915_WRITE(aud_cntl_st, i);
8031 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8032 DRM_DEBUG_DRIVER("port num:%d\n", i);
8033
8034 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8035 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8036 for (i = 0; i < len; i++)
8037 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8038
8039 i = I915_READ(aud_cntrl_st2);
8040 i |= eldv;
8041 I915_WRITE(aud_cntrl_st2, i);
8042
8043}
8044
e0dac65e 8045static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
8046 struct drm_crtc *crtc,
8047 struct drm_display_mode *mode)
e0dac65e
WF
8048{
8049 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8050 uint8_t *eld = connector->eld;
8051 uint32_t eldv;
8052 uint32_t i;
8053 int len;
8054 int hdmiw_hdmiedid;
b6daa025 8055 int aud_config;
e0dac65e
WF
8056 int aud_cntl_st;
8057 int aud_cntrl_st2;
9b138a83 8058 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 8059
b3f33cbf 8060 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
8061 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8062 aud_config = IBX_AUD_CFG(pipe);
8063 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8064 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8065 } else if (IS_VALLEYVIEW(connector->dev)) {
8066 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8067 aud_config = VLV_AUD_CFG(pipe);
8068 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8069 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8070 } else {
9b138a83
WX
8071 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8072 aud_config = CPT_AUD_CFG(pipe);
8073 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8074 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8075 }
8076
9b138a83 8077 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8078
9ca2fe73
ML
8079 if (IS_VALLEYVIEW(connector->dev)) {
8080 struct intel_encoder *intel_encoder;
8081 struct intel_digital_port *intel_dig_port;
8082
8083 intel_encoder = intel_attached_encoder(connector);
8084 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8085 i = intel_dig_port->port;
8086 } else {
8087 i = I915_READ(aud_cntl_st);
8088 i = (i >> 29) & DIP_PORT_SEL_MASK;
8089 /* DIP_Port_Select, 0x1 = PortB */
8090 }
8091
e0dac65e
WF
8092 if (!i) {
8093 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8094 /* operate blindly on all ports */
1202b4c6
WF
8095 eldv = IBX_ELD_VALIDB;
8096 eldv |= IBX_ELD_VALIDB << 4;
8097 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8098 } else {
2582a850 8099 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8100 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8101 }
8102
3a9627f4
WF
8103 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8104 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8105 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8106 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8107 } else {
8108 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8109 }
e0dac65e 8110
3a9627f4
WF
8111 if (intel_eld_uptodate(connector,
8112 aud_cntrl_st2, eldv,
8113 aud_cntl_st, IBX_ELD_ADDRESS,
8114 hdmiw_hdmiedid))
8115 return;
8116
e0dac65e
WF
8117 i = I915_READ(aud_cntrl_st2);
8118 i &= ~eldv;
8119 I915_WRITE(aud_cntrl_st2, i);
8120
8121 if (!eld[0])
8122 return;
8123
e0dac65e 8124 i = I915_READ(aud_cntl_st);
1202b4c6 8125 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8126 I915_WRITE(aud_cntl_st, i);
8127
8128 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8129 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8130 for (i = 0; i < len; i++)
8131 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8132
8133 i = I915_READ(aud_cntrl_st2);
8134 i |= eldv;
8135 I915_WRITE(aud_cntrl_st2, i);
8136}
8137
8138void intel_write_eld(struct drm_encoder *encoder,
8139 struct drm_display_mode *mode)
8140{
8141 struct drm_crtc *crtc = encoder->crtc;
8142 struct drm_connector *connector;
8143 struct drm_device *dev = encoder->dev;
8144 struct drm_i915_private *dev_priv = dev->dev_private;
8145
8146 connector = drm_select_eld(encoder, mode);
8147 if (!connector)
8148 return;
8149
8150 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8151 connector->base.id,
c23cc417 8152 connector->name,
e0dac65e 8153 connector->encoder->base.id,
8e329a03 8154 connector->encoder->name);
e0dac65e
WF
8155
8156 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8157
8158 if (dev_priv->display.write_eld)
34427052 8159 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8160}
8161
560b85bb
CW
8162static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8163{
8164 struct drm_device *dev = crtc->dev;
8165 struct drm_i915_private *dev_priv = dev->dev_private;
8166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8167 uint32_t cntl = 0, size = 0;
560b85bb 8168
dc41c154
VS
8169 if (base) {
8170 unsigned int width = intel_crtc->cursor_width;
8171 unsigned int height = intel_crtc->cursor_height;
8172 unsigned int stride = roundup_pow_of_two(width) * 4;
8173
8174 switch (stride) {
8175 default:
8176 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8177 width, stride);
8178 stride = 256;
8179 /* fallthrough */
8180 case 256:
8181 case 512:
8182 case 1024:
8183 case 2048:
8184 break;
4b0e333e
CW
8185 }
8186
dc41c154
VS
8187 cntl |= CURSOR_ENABLE |
8188 CURSOR_GAMMA_ENABLE |
8189 CURSOR_FORMAT_ARGB |
8190 CURSOR_STRIDE(stride);
8191
8192 size = (height << 12) | width;
4b0e333e 8193 }
560b85bb 8194
dc41c154
VS
8195 if (intel_crtc->cursor_cntl != 0 &&
8196 (intel_crtc->cursor_base != base ||
8197 intel_crtc->cursor_size != size ||
8198 intel_crtc->cursor_cntl != cntl)) {
8199 /* On these chipsets we can only modify the base/size/stride
8200 * whilst the cursor is disabled.
8201 */
8202 I915_WRITE(_CURACNTR, 0);
4b0e333e 8203 POSTING_READ(_CURACNTR);
dc41c154 8204 intel_crtc->cursor_cntl = 0;
4b0e333e 8205 }
560b85bb 8206
99d1f387 8207 if (intel_crtc->cursor_base != base) {
9db4a9c7 8208 I915_WRITE(_CURABASE, base);
99d1f387
VS
8209 intel_crtc->cursor_base = base;
8210 }
4726e0b0 8211
dc41c154
VS
8212 if (intel_crtc->cursor_size != size) {
8213 I915_WRITE(CURSIZE, size);
8214 intel_crtc->cursor_size = size;
4b0e333e 8215 }
560b85bb 8216
4b0e333e 8217 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8218 I915_WRITE(_CURACNTR, cntl);
8219 POSTING_READ(_CURACNTR);
4b0e333e 8220 intel_crtc->cursor_cntl = cntl;
560b85bb 8221 }
560b85bb
CW
8222}
8223
560b85bb 8224static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8225{
8226 struct drm_device *dev = crtc->dev;
8227 struct drm_i915_private *dev_priv = dev->dev_private;
8228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8229 int pipe = intel_crtc->pipe;
4b0e333e
CW
8230 uint32_t cntl;
8231
8232 cntl = 0;
8233 if (base) {
8234 cntl = MCURSOR_GAMMA_ENABLE;
8235 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8236 case 64:
8237 cntl |= CURSOR_MODE_64_ARGB_AX;
8238 break;
8239 case 128:
8240 cntl |= CURSOR_MODE_128_ARGB_AX;
8241 break;
8242 case 256:
8243 cntl |= CURSOR_MODE_256_ARGB_AX;
8244 break;
8245 default:
8246 WARN_ON(1);
8247 return;
65a21cd6 8248 }
4b0e333e 8249 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
8250
8251 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8252 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 8253 }
65a21cd6 8254
4b0e333e
CW
8255 if (intel_crtc->cursor_cntl != cntl) {
8256 I915_WRITE(CURCNTR(pipe), cntl);
8257 POSTING_READ(CURCNTR(pipe));
8258 intel_crtc->cursor_cntl = cntl;
65a21cd6 8259 }
4b0e333e 8260
65a21cd6 8261 /* and commit changes on next vblank */
5efb3e28
VS
8262 I915_WRITE(CURBASE(pipe), base);
8263 POSTING_READ(CURBASE(pipe));
99d1f387
VS
8264
8265 intel_crtc->cursor_base = base;
65a21cd6
JB
8266}
8267
cda4b7d3 8268/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8269static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8270 bool on)
cda4b7d3
CW
8271{
8272 struct drm_device *dev = crtc->dev;
8273 struct drm_i915_private *dev_priv = dev->dev_private;
8274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8275 int pipe = intel_crtc->pipe;
3d7d6510
MR
8276 int x = crtc->cursor_x;
8277 int y = crtc->cursor_y;
d6e4db15 8278 u32 base = 0, pos = 0;
cda4b7d3 8279
d6e4db15 8280 if (on)
cda4b7d3 8281 base = intel_crtc->cursor_addr;
cda4b7d3 8282
d6e4db15
VS
8283 if (x >= intel_crtc->config.pipe_src_w)
8284 base = 0;
8285
8286 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8287 base = 0;
8288
8289 if (x < 0) {
efc9064e 8290 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8291 base = 0;
8292
8293 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8294 x = -x;
8295 }
8296 pos |= x << CURSOR_X_SHIFT;
8297
8298 if (y < 0) {
efc9064e 8299 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8300 base = 0;
8301
8302 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8303 y = -y;
8304 }
8305 pos |= y << CURSOR_Y_SHIFT;
8306
4b0e333e 8307 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8308 return;
8309
5efb3e28
VS
8310 I915_WRITE(CURPOS(pipe), pos);
8311
8ac54669 8312 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8313 i845_update_cursor(crtc, base);
8314 else
8315 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
8316}
8317
dc41c154
VS
8318static bool cursor_size_ok(struct drm_device *dev,
8319 uint32_t width, uint32_t height)
8320{
8321 if (width == 0 || height == 0)
8322 return false;
8323
8324 /*
8325 * 845g/865g are special in that they are only limited by
8326 * the width of their cursors, the height is arbitrary up to
8327 * the precision of the register. Everything else requires
8328 * square cursors, limited to a few power-of-two sizes.
8329 */
8330 if (IS_845G(dev) || IS_I865G(dev)) {
8331 if ((width & 63) != 0)
8332 return false;
8333
8334 if (width > (IS_845G(dev) ? 64 : 512))
8335 return false;
8336
8337 if (height > 1023)
8338 return false;
8339 } else {
8340 switch (width | height) {
8341 case 256:
8342 case 128:
8343 if (IS_GEN2(dev))
8344 return false;
8345 case 64:
8346 break;
8347 default:
8348 return false;
8349 }
8350 }
8351
8352 return true;
8353}
8354
e3287951
MR
8355/*
8356 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8357 *
8358 * Note that the object's reference will be consumed if the update fails. If
8359 * the update succeeds, the reference of the old object (if any) will be
8360 * consumed.
8361 */
8362static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8363 struct drm_i915_gem_object *obj,
8364 uint32_t width, uint32_t height)
79e53945
JB
8365{
8366 struct drm_device *dev = crtc->dev;
8367 struct drm_i915_private *dev_priv = dev->dev_private;
8368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8369 enum pipe pipe = intel_crtc->pipe;
dc41c154 8370 unsigned old_width, stride;
cda4b7d3 8371 uint32_t addr;
3f8bc370 8372 int ret;
79e53945 8373
79e53945 8374 /* if we want to turn off the cursor ignore width and height */
e3287951 8375 if (!obj) {
28c97730 8376 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8377 addr = 0;
5004417d 8378 mutex_lock(&dev->struct_mutex);
3f8bc370 8379 goto finish;
79e53945
JB
8380 }
8381
4726e0b0 8382 /* Check for which cursor types we support */
dc41c154 8383 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8384 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8385 return -EINVAL;
8386 }
8387
dc41c154
VS
8388 stride = roundup_pow_of_two(width) * 4;
8389 if (obj->base.size < stride * height) {
e3287951 8390 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8391 ret = -ENOMEM;
8392 goto fail;
79e53945
JB
8393 }
8394
71acb5eb 8395 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8396 mutex_lock(&dev->struct_mutex);
3d13ef2e 8397 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8398 unsigned alignment;
8399
d9e86c0e 8400 if (obj->tiling_mode) {
3b25b31f 8401 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8402 ret = -EINVAL;
8403 goto fail_locked;
8404 }
8405
d6dd6843
PZ
8406 /*
8407 * Global gtt pte registers are special registers which actually
8408 * forward writes to a chunk of system memory. Which means that
8409 * there is no risk that the register values disappear as soon
8410 * as we call intel_runtime_pm_put(), so it is correct to wrap
8411 * only the pin/unpin/fence and not more.
8412 */
8413 intel_runtime_pm_get(dev_priv);
8414
693db184
CW
8415 /* Note that the w/a also requires 2 PTE of padding following
8416 * the bo. We currently fill all unused PTE with the shadow
8417 * page and so we should always have valid PTE following the
8418 * cursor preventing the VT-d warning.
8419 */
8420 alignment = 0;
8421 if (need_vtd_wa(dev))
8422 alignment = 64*1024;
8423
8424 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8425 if (ret) {
3b25b31f 8426 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
d6dd6843 8427 intel_runtime_pm_put(dev_priv);
2da3b9b9 8428 goto fail_locked;
e7b526bb
CW
8429 }
8430
d9e86c0e
CW
8431 ret = i915_gem_object_put_fence(obj);
8432 if (ret) {
3b25b31f 8433 DRM_DEBUG_KMS("failed to release fence for cursor");
d6dd6843 8434 intel_runtime_pm_put(dev_priv);
d9e86c0e
CW
8435 goto fail_unpin;
8436 }
8437
f343c5f6 8438 addr = i915_gem_obj_ggtt_offset(obj);
d6dd6843
PZ
8439
8440 intel_runtime_pm_put(dev_priv);
71acb5eb 8441 } else {
6eeefaf3 8442 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8443 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8444 if (ret) {
3b25b31f 8445 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8446 goto fail_locked;
71acb5eb 8447 }
00731155 8448 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8449 }
8450
3f8bc370 8451 finish:
3f8bc370 8452 if (intel_crtc->cursor_bo) {
00731155 8453 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8454 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8455 }
80824003 8456
a071fa00
DV
8457 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8458 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8459 mutex_unlock(&dev->struct_mutex);
3f8bc370 8460
64f962e3
CW
8461 old_width = intel_crtc->cursor_width;
8462
3f8bc370 8463 intel_crtc->cursor_addr = addr;
05394f39 8464 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8465 intel_crtc->cursor_width = width;
8466 intel_crtc->cursor_height = height;
8467
64f962e3
CW
8468 if (intel_crtc->active) {
8469 if (old_width != width)
8470 intel_update_watermarks(crtc);
f2f5f771 8471 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8472 }
3f8bc370 8473
f99d7069
DV
8474 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8475
79e53945 8476 return 0;
e7b526bb 8477fail_unpin:
cc98b413 8478 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8479fail_locked:
34b8686e 8480 mutex_unlock(&dev->struct_mutex);
bc9025bd 8481fail:
05394f39 8482 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8483 return ret;
79e53945
JB
8484}
8485
79e53945 8486static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8487 u16 *blue, uint32_t start, uint32_t size)
79e53945 8488{
7203425a 8489 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8491
7203425a 8492 for (i = start; i < end; i++) {
79e53945
JB
8493 intel_crtc->lut_r[i] = red[i] >> 8;
8494 intel_crtc->lut_g[i] = green[i] >> 8;
8495 intel_crtc->lut_b[i] = blue[i] >> 8;
8496 }
8497
8498 intel_crtc_load_lut(crtc);
8499}
8500
79e53945
JB
8501/* VESA 640x480x72Hz mode to set on the pipe */
8502static struct drm_display_mode load_detect_mode = {
8503 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8504 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8505};
8506
a8bb6818
DV
8507struct drm_framebuffer *
8508__intel_framebuffer_create(struct drm_device *dev,
8509 struct drm_mode_fb_cmd2 *mode_cmd,
8510 struct drm_i915_gem_object *obj)
d2dff872
CW
8511{
8512 struct intel_framebuffer *intel_fb;
8513 int ret;
8514
8515 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8516 if (!intel_fb) {
8517 drm_gem_object_unreference_unlocked(&obj->base);
8518 return ERR_PTR(-ENOMEM);
8519 }
8520
8521 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8522 if (ret)
8523 goto err;
d2dff872
CW
8524
8525 return &intel_fb->base;
dd4916c5
DV
8526err:
8527 drm_gem_object_unreference_unlocked(&obj->base);
8528 kfree(intel_fb);
8529
8530 return ERR_PTR(ret);
d2dff872
CW
8531}
8532
b5ea642a 8533static struct drm_framebuffer *
a8bb6818
DV
8534intel_framebuffer_create(struct drm_device *dev,
8535 struct drm_mode_fb_cmd2 *mode_cmd,
8536 struct drm_i915_gem_object *obj)
8537{
8538 struct drm_framebuffer *fb;
8539 int ret;
8540
8541 ret = i915_mutex_lock_interruptible(dev);
8542 if (ret)
8543 return ERR_PTR(ret);
8544 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8545 mutex_unlock(&dev->struct_mutex);
8546
8547 return fb;
8548}
8549
d2dff872
CW
8550static u32
8551intel_framebuffer_pitch_for_width(int width, int bpp)
8552{
8553 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8554 return ALIGN(pitch, 64);
8555}
8556
8557static u32
8558intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8559{
8560 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8561 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8562}
8563
8564static struct drm_framebuffer *
8565intel_framebuffer_create_for_mode(struct drm_device *dev,
8566 struct drm_display_mode *mode,
8567 int depth, int bpp)
8568{
8569 struct drm_i915_gem_object *obj;
0fed39bd 8570 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8571
8572 obj = i915_gem_alloc_object(dev,
8573 intel_framebuffer_size_for_mode(mode, bpp));
8574 if (obj == NULL)
8575 return ERR_PTR(-ENOMEM);
8576
8577 mode_cmd.width = mode->hdisplay;
8578 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8579 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8580 bpp);
5ca0c34a 8581 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8582
8583 return intel_framebuffer_create(dev, &mode_cmd, obj);
8584}
8585
8586static struct drm_framebuffer *
8587mode_fits_in_fbdev(struct drm_device *dev,
8588 struct drm_display_mode *mode)
8589{
4520f53a 8590#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8591 struct drm_i915_private *dev_priv = dev->dev_private;
8592 struct drm_i915_gem_object *obj;
8593 struct drm_framebuffer *fb;
8594
4c0e5528 8595 if (!dev_priv->fbdev)
d2dff872
CW
8596 return NULL;
8597
4c0e5528 8598 if (!dev_priv->fbdev->fb)
d2dff872
CW
8599 return NULL;
8600
4c0e5528
DV
8601 obj = dev_priv->fbdev->fb->obj;
8602 BUG_ON(!obj);
8603
8bcd4553 8604 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8605 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8606 fb->bits_per_pixel))
d2dff872
CW
8607 return NULL;
8608
01f2c773 8609 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8610 return NULL;
8611
8612 return fb;
4520f53a
DV
8613#else
8614 return NULL;
8615#endif
d2dff872
CW
8616}
8617
d2434ab7 8618bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8619 struct drm_display_mode *mode,
51fd371b
RC
8620 struct intel_load_detect_pipe *old,
8621 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8622{
8623 struct intel_crtc *intel_crtc;
d2434ab7
DV
8624 struct intel_encoder *intel_encoder =
8625 intel_attached_encoder(connector);
79e53945 8626 struct drm_crtc *possible_crtc;
4ef69c7a 8627 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8628 struct drm_crtc *crtc = NULL;
8629 struct drm_device *dev = encoder->dev;
94352cf9 8630 struct drm_framebuffer *fb;
51fd371b
RC
8631 struct drm_mode_config *config = &dev->mode_config;
8632 int ret, i = -1;
79e53945 8633
d2dff872 8634 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8635 connector->base.id, connector->name,
8e329a03 8636 encoder->base.id, encoder->name);
d2dff872 8637
51fd371b
RC
8638retry:
8639 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8640 if (ret)
8641 goto fail_unlock;
6e9f798d 8642
79e53945
JB
8643 /*
8644 * Algorithm gets a little messy:
7a5e4805 8645 *
79e53945
JB
8646 * - if the connector already has an assigned crtc, use it (but make
8647 * sure it's on first)
7a5e4805 8648 *
79e53945
JB
8649 * - try to find the first unused crtc that can drive this connector,
8650 * and use that if we find one
79e53945
JB
8651 */
8652
8653 /* See if we already have a CRTC for this connector */
8654 if (encoder->crtc) {
8655 crtc = encoder->crtc;
8261b191 8656
51fd371b
RC
8657 ret = drm_modeset_lock(&crtc->mutex, ctx);
8658 if (ret)
8659 goto fail_unlock;
7b24056b 8660
24218aac 8661 old->dpms_mode = connector->dpms;
8261b191
CW
8662 old->load_detect_temp = false;
8663
8664 /* Make sure the crtc and connector are running */
24218aac
DV
8665 if (connector->dpms != DRM_MODE_DPMS_ON)
8666 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8667
7173188d 8668 return true;
79e53945
JB
8669 }
8670
8671 /* Find an unused one (if possible) */
70e1e0ec 8672 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8673 i++;
8674 if (!(encoder->possible_crtcs & (1 << i)))
8675 continue;
a459249c
VS
8676 if (possible_crtc->enabled)
8677 continue;
8678 /* This can occur when applying the pipe A quirk on resume. */
8679 if (to_intel_crtc(possible_crtc)->new_enabled)
8680 continue;
8681
8682 crtc = possible_crtc;
8683 break;
79e53945
JB
8684 }
8685
8686 /*
8687 * If we didn't find an unused CRTC, don't use any.
8688 */
8689 if (!crtc) {
7173188d 8690 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8691 goto fail_unlock;
79e53945
JB
8692 }
8693
51fd371b
RC
8694 ret = drm_modeset_lock(&crtc->mutex, ctx);
8695 if (ret)
8696 goto fail_unlock;
fc303101
DV
8697 intel_encoder->new_crtc = to_intel_crtc(crtc);
8698 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8699
8700 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8701 intel_crtc->new_enabled = true;
8702 intel_crtc->new_config = &intel_crtc->config;
24218aac 8703 old->dpms_mode = connector->dpms;
8261b191 8704 old->load_detect_temp = true;
d2dff872 8705 old->release_fb = NULL;
79e53945 8706
6492711d
CW
8707 if (!mode)
8708 mode = &load_detect_mode;
79e53945 8709
d2dff872
CW
8710 /* We need a framebuffer large enough to accommodate all accesses
8711 * that the plane may generate whilst we perform load detection.
8712 * We can not rely on the fbcon either being present (we get called
8713 * during its initialisation to detect all boot displays, or it may
8714 * not even exist) or that it is large enough to satisfy the
8715 * requested mode.
8716 */
94352cf9
DV
8717 fb = mode_fits_in_fbdev(dev, mode);
8718 if (fb == NULL) {
d2dff872 8719 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8720 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8721 old->release_fb = fb;
d2dff872
CW
8722 } else
8723 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8724 if (IS_ERR(fb)) {
d2dff872 8725 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8726 goto fail;
79e53945 8727 }
79e53945 8728
c0c36b94 8729 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8730 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8731 if (old->release_fb)
8732 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8733 goto fail;
79e53945 8734 }
7173188d 8735
79e53945 8736 /* let the connector get through one full cycle before testing */
9d0498a2 8737 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8738 return true;
412b61d8
VS
8739
8740 fail:
8741 intel_crtc->new_enabled = crtc->enabled;
8742 if (intel_crtc->new_enabled)
8743 intel_crtc->new_config = &intel_crtc->config;
8744 else
8745 intel_crtc->new_config = NULL;
51fd371b
RC
8746fail_unlock:
8747 if (ret == -EDEADLK) {
8748 drm_modeset_backoff(ctx);
8749 goto retry;
8750 }
8751
412b61d8 8752 return false;
79e53945
JB
8753}
8754
d2434ab7 8755void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8756 struct intel_load_detect_pipe *old)
79e53945 8757{
d2434ab7
DV
8758 struct intel_encoder *intel_encoder =
8759 intel_attached_encoder(connector);
4ef69c7a 8760 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8761 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8763
d2dff872 8764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8765 connector->base.id, connector->name,
8e329a03 8766 encoder->base.id, encoder->name);
d2dff872 8767
8261b191 8768 if (old->load_detect_temp) {
fc303101
DV
8769 to_intel_connector(connector)->new_encoder = NULL;
8770 intel_encoder->new_crtc = NULL;
412b61d8
VS
8771 intel_crtc->new_enabled = false;
8772 intel_crtc->new_config = NULL;
fc303101 8773 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8774
36206361
DV
8775 if (old->release_fb) {
8776 drm_framebuffer_unregister_private(old->release_fb);
8777 drm_framebuffer_unreference(old->release_fb);
8778 }
d2dff872 8779
0622a53c 8780 return;
79e53945
JB
8781 }
8782
c751ce4f 8783 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8784 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8785 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8786}
8787
da4a1efa
VS
8788static int i9xx_pll_refclk(struct drm_device *dev,
8789 const struct intel_crtc_config *pipe_config)
8790{
8791 struct drm_i915_private *dev_priv = dev->dev_private;
8792 u32 dpll = pipe_config->dpll_hw_state.dpll;
8793
8794 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8795 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8796 else if (HAS_PCH_SPLIT(dev))
8797 return 120000;
8798 else if (!IS_GEN2(dev))
8799 return 96000;
8800 else
8801 return 48000;
8802}
8803
79e53945 8804/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8805static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8806 struct intel_crtc_config *pipe_config)
79e53945 8807{
f1f644dc 8808 struct drm_device *dev = crtc->base.dev;
79e53945 8809 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8810 int pipe = pipe_config->cpu_transcoder;
293623f7 8811 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8812 u32 fp;
8813 intel_clock_t clock;
da4a1efa 8814 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8815
8816 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8817 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8818 else
293623f7 8819 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8820
8821 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8822 if (IS_PINEVIEW(dev)) {
8823 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8824 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8825 } else {
8826 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8827 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8828 }
8829
a6c45cf0 8830 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8831 if (IS_PINEVIEW(dev))
8832 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8833 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8834 else
8835 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8836 DPLL_FPA01_P1_POST_DIV_SHIFT);
8837
8838 switch (dpll & DPLL_MODE_MASK) {
8839 case DPLLB_MODE_DAC_SERIAL:
8840 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8841 5 : 10;
8842 break;
8843 case DPLLB_MODE_LVDS:
8844 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8845 7 : 14;
8846 break;
8847 default:
28c97730 8848 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8849 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8850 return;
79e53945
JB
8851 }
8852
ac58c3f0 8853 if (IS_PINEVIEW(dev))
da4a1efa 8854 pineview_clock(refclk, &clock);
ac58c3f0 8855 else
da4a1efa 8856 i9xx_clock(refclk, &clock);
79e53945 8857 } else {
0fb58223 8858 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8859 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8860
8861 if (is_lvds) {
8862 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8863 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8864
8865 if (lvds & LVDS_CLKB_POWER_UP)
8866 clock.p2 = 7;
8867 else
8868 clock.p2 = 14;
79e53945
JB
8869 } else {
8870 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8871 clock.p1 = 2;
8872 else {
8873 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8874 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8875 }
8876 if (dpll & PLL_P2_DIVIDE_BY_4)
8877 clock.p2 = 4;
8878 else
8879 clock.p2 = 2;
79e53945 8880 }
da4a1efa
VS
8881
8882 i9xx_clock(refclk, &clock);
79e53945
JB
8883 }
8884
18442d08
VS
8885 /*
8886 * This value includes pixel_multiplier. We will use
241bfc38 8887 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8888 * encoder's get_config() function.
8889 */
8890 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8891}
8892
6878da05
VS
8893int intel_dotclock_calculate(int link_freq,
8894 const struct intel_link_m_n *m_n)
f1f644dc 8895{
f1f644dc
JB
8896 /*
8897 * The calculation for the data clock is:
1041a02f 8898 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8899 * But we want to avoid losing precison if possible, so:
1041a02f 8900 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8901 *
8902 * and the link clock is simpler:
1041a02f 8903 * link_clock = (m * link_clock) / n
f1f644dc
JB
8904 */
8905
6878da05
VS
8906 if (!m_n->link_n)
8907 return 0;
f1f644dc 8908
6878da05
VS
8909 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8910}
f1f644dc 8911
18442d08
VS
8912static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8913 struct intel_crtc_config *pipe_config)
6878da05
VS
8914{
8915 struct drm_device *dev = crtc->base.dev;
79e53945 8916
18442d08
VS
8917 /* read out port_clock from the DPLL */
8918 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8919
f1f644dc 8920 /*
18442d08 8921 * This value does not include pixel_multiplier.
241bfc38 8922 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8923 * agree once we know their relationship in the encoder's
8924 * get_config() function.
79e53945 8925 */
241bfc38 8926 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8927 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8928 &pipe_config->fdi_m_n);
79e53945
JB
8929}
8930
8931/** Returns the currently programmed mode of the given pipe. */
8932struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8933 struct drm_crtc *crtc)
8934{
548f245b 8935 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8937 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8938 struct drm_display_mode *mode;
f1f644dc 8939 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8940 int htot = I915_READ(HTOTAL(cpu_transcoder));
8941 int hsync = I915_READ(HSYNC(cpu_transcoder));
8942 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8943 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8944 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8945
8946 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8947 if (!mode)
8948 return NULL;
8949
f1f644dc
JB
8950 /*
8951 * Construct a pipe_config sufficient for getting the clock info
8952 * back out of crtc_clock_get.
8953 *
8954 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8955 * to use a real value here instead.
8956 */
293623f7 8957 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8958 pipe_config.pixel_multiplier = 1;
293623f7
VS
8959 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8960 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8961 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8962 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8963
773ae034 8964 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8965 mode->hdisplay = (htot & 0xffff) + 1;
8966 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8967 mode->hsync_start = (hsync & 0xffff) + 1;
8968 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8969 mode->vdisplay = (vtot & 0xffff) + 1;
8970 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8971 mode->vsync_start = (vsync & 0xffff) + 1;
8972 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8973
8974 drm_mode_set_name(mode);
79e53945
JB
8975
8976 return mode;
8977}
8978
cc36513c
DV
8979static void intel_increase_pllclock(struct drm_device *dev,
8980 enum pipe pipe)
652c393a 8981{
fbee40df 8982 struct drm_i915_private *dev_priv = dev->dev_private;
dbdc6479
JB
8983 int dpll_reg = DPLL(pipe);
8984 int dpll;
652c393a 8985
baff296c 8986 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
8987 return;
8988
8989 if (!dev_priv->lvds_downclock_avail)
8990 return;
8991
dbdc6479 8992 dpll = I915_READ(dpll_reg);
652c393a 8993 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8994 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8995
8ac5a6d5 8996 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8997
8998 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8999 I915_WRITE(dpll_reg, dpll);
9d0498a2 9000 intel_wait_for_vblank(dev, pipe);
dbdc6479 9001
652c393a
JB
9002 dpll = I915_READ(dpll_reg);
9003 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 9004 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 9005 }
652c393a
JB
9006}
9007
9008static void intel_decrease_pllclock(struct drm_crtc *crtc)
9009{
9010 struct drm_device *dev = crtc->dev;
fbee40df 9011 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 9013
baff296c 9014 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9015 return;
9016
9017 if (!dev_priv->lvds_downclock_avail)
9018 return;
9019
9020 /*
9021 * Since this is called by a timer, we should never get here in
9022 * the manual case.
9023 */
9024 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
9025 int pipe = intel_crtc->pipe;
9026 int dpll_reg = DPLL(pipe);
9027 int dpll;
f6e5b160 9028
44d98a61 9029 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9030
8ac5a6d5 9031 assert_panel_unlocked(dev_priv, pipe);
652c393a 9032
dc257cf1 9033 dpll = I915_READ(dpll_reg);
652c393a
JB
9034 dpll |= DISPLAY_RATE_SELECT_FPA1;
9035 I915_WRITE(dpll_reg, dpll);
9d0498a2 9036 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9037 dpll = I915_READ(dpll_reg);
9038 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9039 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9040 }
9041
9042}
9043
f047e395
CW
9044void intel_mark_busy(struct drm_device *dev)
9045{
c67a470b
PZ
9046 struct drm_i915_private *dev_priv = dev->dev_private;
9047
f62a0076
CW
9048 if (dev_priv->mm.busy)
9049 return;
9050
43694d69 9051 intel_runtime_pm_get(dev_priv);
c67a470b 9052 i915_update_gfx_val(dev_priv);
f62a0076 9053 dev_priv->mm.busy = true;
f047e395
CW
9054}
9055
9056void intel_mark_idle(struct drm_device *dev)
652c393a 9057{
c67a470b 9058 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9059 struct drm_crtc *crtc;
652c393a 9060
f62a0076
CW
9061 if (!dev_priv->mm.busy)
9062 return;
9063
9064 dev_priv->mm.busy = false;
9065
d330a953 9066 if (!i915.powersave)
bb4cdd53 9067 goto out;
652c393a 9068
70e1e0ec 9069 for_each_crtc(dev, crtc) {
f4510a27 9070 if (!crtc->primary->fb)
652c393a
JB
9071 continue;
9072
725a5b54 9073 intel_decrease_pllclock(crtc);
652c393a 9074 }
b29c19b6 9075
3d13ef2e 9076 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9077 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9078
9079out:
43694d69 9080 intel_runtime_pm_put(dev_priv);
652c393a
JB
9081}
9082
7c8f8a70 9083
f99d7069
DV
9084/**
9085 * intel_mark_fb_busy - mark given planes as busy
9086 * @dev: DRM device
9087 * @frontbuffer_bits: bits for the affected planes
9088 * @ring: optional ring for asynchronous commands
9089 *
9090 * This function gets called every time the screen contents change. It can be
9091 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9092 */
9093static void intel_mark_fb_busy(struct drm_device *dev,
9094 unsigned frontbuffer_bits,
9095 struct intel_engine_cs *ring)
652c393a 9096{
055e393f 9097 struct drm_i915_private *dev_priv = dev->dev_private;
cc36513c 9098 enum pipe pipe;
652c393a 9099
d330a953 9100 if (!i915.powersave)
acb87dfb
CW
9101 return;
9102
055e393f 9103 for_each_pipe(dev_priv, pipe) {
f99d7069 9104 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
c65355bb
CW
9105 continue;
9106
cc36513c 9107 intel_increase_pllclock(dev, pipe);
c65355bb
CW
9108 if (ring && intel_fbc_enabled(dev))
9109 ring->fbc_dirty = true;
652c393a
JB
9110 }
9111}
9112
f99d7069
DV
9113/**
9114 * intel_fb_obj_invalidate - invalidate frontbuffer object
9115 * @obj: GEM object to invalidate
9116 * @ring: set for asynchronous rendering
9117 *
9118 * This function gets called every time rendering on the given object starts and
9119 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9120 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9121 * until the rendering completes or a flip on this frontbuffer plane is
9122 * scheduled.
9123 */
9124void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9125 struct intel_engine_cs *ring)
9126{
9127 struct drm_device *dev = obj->base.dev;
9128 struct drm_i915_private *dev_priv = dev->dev_private;
9129
9130 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9131
9132 if (!obj->frontbuffer_bits)
9133 return;
9134
9135 if (ring) {
9136 mutex_lock(&dev_priv->fb_tracking.lock);
9137 dev_priv->fb_tracking.busy_bits
9138 |= obj->frontbuffer_bits;
9139 dev_priv->fb_tracking.flip_bits
9140 &= ~obj->frontbuffer_bits;
9141 mutex_unlock(&dev_priv->fb_tracking.lock);
9142 }
9143
9144 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9145
9ca15301 9146 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
f99d7069
DV
9147}
9148
9149/**
9150 * intel_frontbuffer_flush - flush frontbuffer
9151 * @dev: DRM device
9152 * @frontbuffer_bits: frontbuffer plane tracking bits
9153 *
9154 * This function gets called every time rendering on the given planes has
9155 * completed and frontbuffer caching can be started again. Flushes will get
9156 * delayed if they're blocked by some oustanding asynchronous rendering.
9157 *
9158 * Can be called without any locks held.
9159 */
9160void intel_frontbuffer_flush(struct drm_device *dev,
9161 unsigned frontbuffer_bits)
9162{
9163 struct drm_i915_private *dev_priv = dev->dev_private;
9164
9165 /* Delay flushing when rings are still busy.*/
9166 mutex_lock(&dev_priv->fb_tracking.lock);
9167 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9168 mutex_unlock(&dev_priv->fb_tracking.lock);
9169
9170 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9171
9ca15301 9172 intel_edp_psr_flush(dev, frontbuffer_bits);
c5ad011d 9173
c317adcd
VS
9174 /*
9175 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9176 * needs to be reworked into a proper frontbuffer tracking scheme like
9177 * psr employs.
9178 */
9179 if (IS_BROADWELL(dev))
c5ad011d 9180 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
f99d7069
DV
9181}
9182
9183/**
9184 * intel_fb_obj_flush - flush frontbuffer object
9185 * @obj: GEM object to flush
9186 * @retire: set when retiring asynchronous rendering
9187 *
9188 * This function gets called every time rendering on the given object has
9189 * completed and frontbuffer caching can be started again. If @retire is true
9190 * then any delayed flushes will be unblocked.
9191 */
9192void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9193 bool retire)
9194{
9195 struct drm_device *dev = obj->base.dev;
9196 struct drm_i915_private *dev_priv = dev->dev_private;
9197 unsigned frontbuffer_bits;
9198
9199 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9200
9201 if (!obj->frontbuffer_bits)
9202 return;
9203
9204 frontbuffer_bits = obj->frontbuffer_bits;
9205
9206 if (retire) {
9207 mutex_lock(&dev_priv->fb_tracking.lock);
9208 /* Filter out new bits since rendering started. */
9209 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9210
9211 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9212 mutex_unlock(&dev_priv->fb_tracking.lock);
9213 }
9214
9215 intel_frontbuffer_flush(dev, frontbuffer_bits);
9216}
9217
9218/**
9219 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9220 * @dev: DRM device
9221 * @frontbuffer_bits: frontbuffer plane tracking bits
9222 *
9223 * This function gets called after scheduling a flip on @obj. The actual
9224 * frontbuffer flushing will be delayed until completion is signalled with
9225 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9226 * flush will be cancelled.
9227 *
9228 * Can be called without any locks held.
9229 */
9230void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9231 unsigned frontbuffer_bits)
9232{
9233 struct drm_i915_private *dev_priv = dev->dev_private;
9234
9235 mutex_lock(&dev_priv->fb_tracking.lock);
9236 dev_priv->fb_tracking.flip_bits
9237 |= frontbuffer_bits;
9238 mutex_unlock(&dev_priv->fb_tracking.lock);
9239}
9240
9241/**
9242 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9243 * @dev: DRM device
9244 * @frontbuffer_bits: frontbuffer plane tracking bits
9245 *
9246 * This function gets called after the flip has been latched and will complete
9247 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9248 *
9249 * Can be called without any locks held.
9250 */
9251void intel_frontbuffer_flip_complete(struct drm_device *dev,
9252 unsigned frontbuffer_bits)
9253{
9254 struct drm_i915_private *dev_priv = dev->dev_private;
9255
9256 mutex_lock(&dev_priv->fb_tracking.lock);
9257 /* Mask any cancelled flips. */
9258 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9259 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9260 mutex_unlock(&dev_priv->fb_tracking.lock);
9261
9262 intel_frontbuffer_flush(dev, frontbuffer_bits);
9263}
9264
79e53945
JB
9265static void intel_crtc_destroy(struct drm_crtc *crtc)
9266{
9267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9268 struct drm_device *dev = crtc->dev;
9269 struct intel_unpin_work *work;
9270 unsigned long flags;
9271
9272 spin_lock_irqsave(&dev->event_lock, flags);
9273 work = intel_crtc->unpin_work;
9274 intel_crtc->unpin_work = NULL;
9275 spin_unlock_irqrestore(&dev->event_lock, flags);
9276
9277 if (work) {
9278 cancel_work_sync(&work->work);
9279 kfree(work);
9280 }
79e53945
JB
9281
9282 drm_crtc_cleanup(crtc);
67e77c5a 9283
79e53945
JB
9284 kfree(intel_crtc);
9285}
9286
6b95a207
KH
9287static void intel_unpin_work_fn(struct work_struct *__work)
9288{
9289 struct intel_unpin_work *work =
9290 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9291 struct drm_device *dev = work->crtc->dev;
f99d7069 9292 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9293
b4a98e57 9294 mutex_lock(&dev->struct_mutex);
1690e1eb 9295 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9296 drm_gem_object_unreference(&work->pending_flip_obj->base);
9297 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9298
b4a98e57
CW
9299 intel_update_fbc(dev);
9300 mutex_unlock(&dev->struct_mutex);
9301
f99d7069
DV
9302 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9303
b4a98e57
CW
9304 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9305 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9306
6b95a207
KH
9307 kfree(work);
9308}
9309
1afe3e9d 9310static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9311 struct drm_crtc *crtc)
6b95a207 9312{
6b95a207
KH
9313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9314 struct intel_unpin_work *work;
6b95a207
KH
9315 unsigned long flags;
9316
9317 /* Ignore early vblank irqs */
9318 if (intel_crtc == NULL)
9319 return;
9320
9321 spin_lock_irqsave(&dev->event_lock, flags);
9322 work = intel_crtc->unpin_work;
e7d841ca
CW
9323
9324 /* Ensure we don't miss a work->pending update ... */
9325 smp_rmb();
9326
9327 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9328 spin_unlock_irqrestore(&dev->event_lock, flags);
9329 return;
9330 }
9331
d6bbafa1 9332 page_flip_completed(intel_crtc);
0af7e4df 9333
6b95a207 9334 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9335}
9336
1afe3e9d
JB
9337void intel_finish_page_flip(struct drm_device *dev, int pipe)
9338{
fbee40df 9339 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9340 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9341
49b14a5c 9342 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9343}
9344
9345void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9346{
fbee40df 9347 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9348 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9349
49b14a5c 9350 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9351}
9352
75f7f3ec
VS
9353/* Is 'a' after or equal to 'b'? */
9354static bool g4x_flip_count_after_eq(u32 a, u32 b)
9355{
9356 return !((a - b) & 0x80000000);
9357}
9358
9359static bool page_flip_finished(struct intel_crtc *crtc)
9360{
9361 struct drm_device *dev = crtc->base.dev;
9362 struct drm_i915_private *dev_priv = dev->dev_private;
9363
9364 /*
9365 * The relevant registers doen't exist on pre-ctg.
9366 * As the flip done interrupt doesn't trigger for mmio
9367 * flips on gmch platforms, a flip count check isn't
9368 * really needed there. But since ctg has the registers,
9369 * include it in the check anyway.
9370 */
9371 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9372 return true;
9373
9374 /*
9375 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9376 * used the same base address. In that case the mmio flip might
9377 * have completed, but the CS hasn't even executed the flip yet.
9378 *
9379 * A flip count check isn't enough as the CS might have updated
9380 * the base address just after start of vblank, but before we
9381 * managed to process the interrupt. This means we'd complete the
9382 * CS flip too soon.
9383 *
9384 * Combining both checks should get us a good enough result. It may
9385 * still happen that the CS flip has been executed, but has not
9386 * yet actually completed. But in case the base address is the same
9387 * anyway, we don't really care.
9388 */
9389 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9390 crtc->unpin_work->gtt_offset &&
9391 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9392 crtc->unpin_work->flip_count);
9393}
9394
6b95a207
KH
9395void intel_prepare_page_flip(struct drm_device *dev, int plane)
9396{
fbee40df 9397 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9398 struct intel_crtc *intel_crtc =
9399 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9400 unsigned long flags;
9401
e7d841ca
CW
9402 /* NB: An MMIO update of the plane base pointer will also
9403 * generate a page-flip completion irq, i.e. every modeset
9404 * is also accompanied by a spurious intel_prepare_page_flip().
9405 */
6b95a207 9406 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9407 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9408 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9409 spin_unlock_irqrestore(&dev->event_lock, flags);
9410}
9411
eba905b2 9412static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9413{
9414 /* Ensure that the work item is consistent when activating it ... */
9415 smp_wmb();
9416 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9417 /* and that it is marked active as soon as the irq could fire. */
9418 smp_wmb();
9419}
9420
8c9f3aaf
JB
9421static int intel_gen2_queue_flip(struct drm_device *dev,
9422 struct drm_crtc *crtc,
9423 struct drm_framebuffer *fb,
ed8d1975 9424 struct drm_i915_gem_object *obj,
a4872ba6 9425 struct intel_engine_cs *ring,
ed8d1975 9426 uint32_t flags)
8c9f3aaf 9427{
8c9f3aaf 9428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9429 u32 flip_mask;
9430 int ret;
9431
6d90c952 9432 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9433 if (ret)
4fa62c89 9434 return ret;
8c9f3aaf
JB
9435
9436 /* Can't queue multiple flips, so wait for the previous
9437 * one to finish before executing the next.
9438 */
9439 if (intel_crtc->plane)
9440 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9441 else
9442 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9443 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9444 intel_ring_emit(ring, MI_NOOP);
9445 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9446 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9447 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9448 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9449 intel_ring_emit(ring, 0); /* aux display base address, unused */
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_gen3_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 9462{
8c9f3aaf 9463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9464 u32 flip_mask;
9465 int ret;
9466
6d90c952 9467 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9468 if (ret)
4fa62c89 9469 return ret;
8c9f3aaf
JB
9470
9471 if (intel_crtc->plane)
9472 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9473 else
9474 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9475 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9476 intel_ring_emit(ring, MI_NOOP);
9477 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9478 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9479 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9480 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9481 intel_ring_emit(ring, MI_NOOP);
9482
e7d841ca 9483 intel_mark_page_flip_active(intel_crtc);
09246732 9484 __intel_ring_advance(ring);
83d4092b 9485 return 0;
8c9f3aaf
JB
9486}
9487
9488static int intel_gen4_queue_flip(struct drm_device *dev,
9489 struct drm_crtc *crtc,
9490 struct drm_framebuffer *fb,
ed8d1975 9491 struct drm_i915_gem_object *obj,
a4872ba6 9492 struct intel_engine_cs *ring,
ed8d1975 9493 uint32_t flags)
8c9f3aaf
JB
9494{
9495 struct drm_i915_private *dev_priv = dev->dev_private;
9496 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9497 uint32_t pf, pipesrc;
9498 int ret;
9499
6d90c952 9500 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9501 if (ret)
4fa62c89 9502 return ret;
8c9f3aaf
JB
9503
9504 /* i965+ uses the linear or tiled offsets from the
9505 * Display Registers (which do not change across a page-flip)
9506 * so we need only reprogram the base address.
9507 */
6d90c952
DV
9508 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9509 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9510 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9511 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9512 obj->tiling_mode);
8c9f3aaf
JB
9513
9514 /* XXX Enabling the panel-fitter across page-flip is so far
9515 * untested on non-native modes, so ignore it for now.
9516 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9517 */
9518 pf = 0;
9519 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9520 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9521
9522 intel_mark_page_flip_active(intel_crtc);
09246732 9523 __intel_ring_advance(ring);
83d4092b 9524 return 0;
8c9f3aaf
JB
9525}
9526
9527static int intel_gen6_queue_flip(struct drm_device *dev,
9528 struct drm_crtc *crtc,
9529 struct drm_framebuffer *fb,
ed8d1975 9530 struct drm_i915_gem_object *obj,
a4872ba6 9531 struct intel_engine_cs *ring,
ed8d1975 9532 uint32_t flags)
8c9f3aaf
JB
9533{
9534 struct drm_i915_private *dev_priv = dev->dev_private;
9535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9536 uint32_t pf, pipesrc;
9537 int ret;
9538
6d90c952 9539 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9540 if (ret)
4fa62c89 9541 return ret;
8c9f3aaf 9542
6d90c952
DV
9543 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9544 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9545 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9546 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9547
dc257cf1
DV
9548 /* Contrary to the suggestions in the documentation,
9549 * "Enable Panel Fitter" does not seem to be required when page
9550 * flipping with a non-native mode, and worse causes a normal
9551 * modeset to fail.
9552 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9553 */
9554 pf = 0;
8c9f3aaf 9555 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9556 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9557
9558 intel_mark_page_flip_active(intel_crtc);
09246732 9559 __intel_ring_advance(ring);
83d4092b 9560 return 0;
8c9f3aaf
JB
9561}
9562
7c9017e5
JB
9563static int intel_gen7_queue_flip(struct drm_device *dev,
9564 struct drm_crtc *crtc,
9565 struct drm_framebuffer *fb,
ed8d1975 9566 struct drm_i915_gem_object *obj,
a4872ba6 9567 struct intel_engine_cs *ring,
ed8d1975 9568 uint32_t flags)
7c9017e5 9569{
7c9017e5 9570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9571 uint32_t plane_bit = 0;
ffe74d75
CW
9572 int len, ret;
9573
eba905b2 9574 switch (intel_crtc->plane) {
cb05d8de
DV
9575 case PLANE_A:
9576 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9577 break;
9578 case PLANE_B:
9579 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9580 break;
9581 case PLANE_C:
9582 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9583 break;
9584 default:
9585 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9586 return -ENODEV;
cb05d8de
DV
9587 }
9588
ffe74d75 9589 len = 4;
f476828a 9590 if (ring->id == RCS) {
ffe74d75 9591 len += 6;
f476828a
DL
9592 /*
9593 * On Gen 8, SRM is now taking an extra dword to accommodate
9594 * 48bits addresses, and we need a NOOP for the batch size to
9595 * stay even.
9596 */
9597 if (IS_GEN8(dev))
9598 len += 2;
9599 }
ffe74d75 9600
f66fab8e
VS
9601 /*
9602 * BSpec MI_DISPLAY_FLIP for IVB:
9603 * "The full packet must be contained within the same cache line."
9604 *
9605 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9606 * cacheline, if we ever start emitting more commands before
9607 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9608 * then do the cacheline alignment, and finally emit the
9609 * MI_DISPLAY_FLIP.
9610 */
9611 ret = intel_ring_cacheline_align(ring);
9612 if (ret)
4fa62c89 9613 return ret;
f66fab8e 9614
ffe74d75 9615 ret = intel_ring_begin(ring, len);
7c9017e5 9616 if (ret)
4fa62c89 9617 return ret;
7c9017e5 9618
ffe74d75
CW
9619 /* Unmask the flip-done completion message. Note that the bspec says that
9620 * we should do this for both the BCS and RCS, and that we must not unmask
9621 * more than one flip event at any time (or ensure that one flip message
9622 * can be sent by waiting for flip-done prior to queueing new flips).
9623 * Experimentation says that BCS works despite DERRMR masking all
9624 * flip-done completion events and that unmasking all planes at once
9625 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9626 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9627 */
9628 if (ring->id == RCS) {
9629 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9630 intel_ring_emit(ring, DERRMR);
9631 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9632 DERRMR_PIPEB_PRI_FLIP_DONE |
9633 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9634 if (IS_GEN8(dev))
9635 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9636 MI_SRM_LRM_GLOBAL_GTT);
9637 else
9638 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9639 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9640 intel_ring_emit(ring, DERRMR);
9641 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9642 if (IS_GEN8(dev)) {
9643 intel_ring_emit(ring, 0);
9644 intel_ring_emit(ring, MI_NOOP);
9645 }
ffe74d75
CW
9646 }
9647
cb05d8de 9648 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9649 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9650 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9651 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9652
9653 intel_mark_page_flip_active(intel_crtc);
09246732 9654 __intel_ring_advance(ring);
83d4092b 9655 return 0;
7c9017e5
JB
9656}
9657
84c33a64
SG
9658static bool use_mmio_flip(struct intel_engine_cs *ring,
9659 struct drm_i915_gem_object *obj)
9660{
9661 /*
9662 * This is not being used for older platforms, because
9663 * non-availability of flip done interrupt forces us to use
9664 * CS flips. Older platforms derive flip done using some clever
9665 * tricks involving the flip_pending status bits and vblank irqs.
9666 * So using MMIO flips there would disrupt this mechanism.
9667 */
9668
8e09bf83
CW
9669 if (ring == NULL)
9670 return true;
9671
84c33a64
SG
9672 if (INTEL_INFO(ring->dev)->gen < 5)
9673 return false;
9674
9675 if (i915.use_mmio_flip < 0)
9676 return false;
9677 else if (i915.use_mmio_flip > 0)
9678 return true;
14bf993e
OM
9679 else if (i915.enable_execlists)
9680 return true;
84c33a64
SG
9681 else
9682 return ring != obj->ring;
9683}
9684
9685static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9686{
9687 struct drm_device *dev = intel_crtc->base.dev;
9688 struct drm_i915_private *dev_priv = dev->dev_private;
9689 struct intel_framebuffer *intel_fb =
9690 to_intel_framebuffer(intel_crtc->base.primary->fb);
9691 struct drm_i915_gem_object *obj = intel_fb->obj;
9692 u32 dspcntr;
9693 u32 reg;
9694
9695 intel_mark_page_flip_active(intel_crtc);
9696
9697 reg = DSPCNTR(intel_crtc->plane);
9698 dspcntr = I915_READ(reg);
9699
9700 if (INTEL_INFO(dev)->gen >= 4) {
9701 if (obj->tiling_mode != I915_TILING_NONE)
9702 dspcntr |= DISPPLANE_TILED;
9703 else
9704 dspcntr &= ~DISPPLANE_TILED;
9705 }
9706 I915_WRITE(reg, dspcntr);
9707
9708 I915_WRITE(DSPSURF(intel_crtc->plane),
9709 intel_crtc->unpin_work->gtt_offset);
9710 POSTING_READ(DSPSURF(intel_crtc->plane));
9711}
9712
9713static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9714{
9715 struct intel_engine_cs *ring;
9716 int ret;
9717
9718 lockdep_assert_held(&obj->base.dev->struct_mutex);
9719
9720 if (!obj->last_write_seqno)
9721 return 0;
9722
9723 ring = obj->ring;
9724
9725 if (i915_seqno_passed(ring->get_seqno(ring, true),
9726 obj->last_write_seqno))
9727 return 0;
9728
9729 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9730 if (ret)
9731 return ret;
9732
9733 if (WARN_ON(!ring->irq_get(ring)))
9734 return 0;
9735
9736 return 1;
9737}
9738
9739void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9740{
9741 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9742 struct intel_crtc *intel_crtc;
9743 unsigned long irq_flags;
9744 u32 seqno;
9745
9746 seqno = ring->get_seqno(ring, false);
9747
9748 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9749 for_each_intel_crtc(ring->dev, intel_crtc) {
9750 struct intel_mmio_flip *mmio_flip;
9751
9752 mmio_flip = &intel_crtc->mmio_flip;
9753 if (mmio_flip->seqno == 0)
9754 continue;
9755
9756 if (ring->id != mmio_flip->ring_id)
9757 continue;
9758
9759 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9760 intel_do_mmio_flip(intel_crtc);
9761 mmio_flip->seqno = 0;
9762 ring->irq_put(ring);
9763 }
9764 }
9765 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9766}
9767
9768static int intel_queue_mmio_flip(struct drm_device *dev,
9769 struct drm_crtc *crtc,
9770 struct drm_framebuffer *fb,
9771 struct drm_i915_gem_object *obj,
9772 struct intel_engine_cs *ring,
9773 uint32_t flags)
9774{
9775 struct drm_i915_private *dev_priv = dev->dev_private;
9776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9777 unsigned long irq_flags;
9778 int ret;
9779
9780 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9781 return -EBUSY;
9782
9783 ret = intel_postpone_flip(obj);
9784 if (ret < 0)
9785 return ret;
9786 if (ret == 0) {
9787 intel_do_mmio_flip(intel_crtc);
9788 return 0;
9789 }
9790
9791 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9792 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9793 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9794 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9795
9796 /*
9797 * Double check to catch cases where irq fired before
9798 * mmio flip data was ready
9799 */
9800 intel_notify_mmio_flip(obj->ring);
9801 return 0;
9802}
9803
8c9f3aaf
JB
9804static int intel_default_queue_flip(struct drm_device *dev,
9805 struct drm_crtc *crtc,
9806 struct drm_framebuffer *fb,
ed8d1975 9807 struct drm_i915_gem_object *obj,
a4872ba6 9808 struct intel_engine_cs *ring,
ed8d1975 9809 uint32_t flags)
8c9f3aaf
JB
9810{
9811 return -ENODEV;
9812}
9813
d6bbafa1
CW
9814static bool __intel_pageflip_stall_check(struct drm_device *dev,
9815 struct drm_crtc *crtc)
9816{
9817 struct drm_i915_private *dev_priv = dev->dev_private;
9818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9819 struct intel_unpin_work *work = intel_crtc->unpin_work;
9820 u32 addr;
9821
9822 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9823 return true;
9824
9825 if (!work->enable_stall_check)
9826 return false;
9827
9828 if (work->flip_ready_vblank == 0) {
9829 if (work->flip_queued_ring &&
9830 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9831 work->flip_queued_seqno))
9832 return false;
9833
9834 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9835 }
9836
9837 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9838 return false;
9839
9840 /* Potential stall - if we see that the flip has happened,
9841 * assume a missed interrupt. */
9842 if (INTEL_INFO(dev)->gen >= 4)
9843 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9844 else
9845 addr = I915_READ(DSPADDR(intel_crtc->plane));
9846
9847 /* There is a potential issue here with a false positive after a flip
9848 * to the same address. We could address this by checking for a
9849 * non-incrementing frame counter.
9850 */
9851 return addr == work->gtt_offset;
9852}
9853
9854void intel_check_page_flip(struct drm_device *dev, int pipe)
9855{
9856 struct drm_i915_private *dev_priv = dev->dev_private;
9857 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9859 unsigned long flags;
9860
9861 if (crtc == NULL)
9862 return;
9863
9864 spin_lock_irqsave(&dev->event_lock, flags);
9865 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9866 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9867 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9868 page_flip_completed(intel_crtc);
9869 }
9870 spin_unlock_irqrestore(&dev->event_lock, flags);
9871}
9872
6b95a207
KH
9873static int intel_crtc_page_flip(struct drm_crtc *crtc,
9874 struct drm_framebuffer *fb,
ed8d1975
KP
9875 struct drm_pending_vblank_event *event,
9876 uint32_t page_flip_flags)
6b95a207
KH
9877{
9878 struct drm_device *dev = crtc->dev;
9879 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9880 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9881 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9883 enum pipe pipe = intel_crtc->pipe;
6b95a207 9884 struct intel_unpin_work *work;
a4872ba6 9885 struct intel_engine_cs *ring;
8c9f3aaf 9886 unsigned long flags;
52e68630 9887 int ret;
6b95a207 9888
c76bb61a
DS
9889 //trigger software GT busyness calculation
9890 gen8_flip_interrupt(dev);
9891
2ff8fde1
MR
9892 /*
9893 * drm_mode_page_flip_ioctl() should already catch this, but double
9894 * check to be safe. In the future we may enable pageflipping from
9895 * a disabled primary plane.
9896 */
9897 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9898 return -EBUSY;
9899
e6a595d2 9900 /* Can't change pixel format via MI display flips. */
f4510a27 9901 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9902 return -EINVAL;
9903
9904 /*
9905 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9906 * Note that pitch changes could also affect these register.
9907 */
9908 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9909 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9910 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9911 return -EINVAL;
9912
f900db47
CW
9913 if (i915_terminally_wedged(&dev_priv->gpu_error))
9914 goto out_hang;
9915
b14c5679 9916 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9917 if (work == NULL)
9918 return -ENOMEM;
9919
6b95a207 9920 work->event = event;
b4a98e57 9921 work->crtc = crtc;
2ff8fde1 9922 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
9923 INIT_WORK(&work->work, intel_unpin_work_fn);
9924
87b6b101 9925 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
9926 if (ret)
9927 goto free_work;
9928
6b95a207
KH
9929 /* We borrow the event spin lock for protecting unpin_work */
9930 spin_lock_irqsave(&dev->event_lock, flags);
9931 if (intel_crtc->unpin_work) {
d6bbafa1
CW
9932 /* Before declaring the flip queue wedged, check if
9933 * the hardware completed the operation behind our backs.
9934 */
9935 if (__intel_pageflip_stall_check(dev, crtc)) {
9936 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9937 page_flip_completed(intel_crtc);
9938 } else {
9939 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
9940 spin_unlock_irqrestore(&dev->event_lock, flags);
468f0b44 9941
d6bbafa1
CW
9942 drm_crtc_vblank_put(crtc);
9943 kfree(work);
9944 return -EBUSY;
9945 }
6b95a207
KH
9946 }
9947 intel_crtc->unpin_work = work;
9948 spin_unlock_irqrestore(&dev->event_lock, flags);
9949
b4a98e57
CW
9950 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9951 flush_workqueue(dev_priv->wq);
9952
79158103
CW
9953 ret = i915_mutex_lock_interruptible(dev);
9954 if (ret)
9955 goto cleanup;
6b95a207 9956
75dfca80 9957 /* Reference the objects for the scheduled work. */
05394f39
CW
9958 drm_gem_object_reference(&work->old_fb_obj->base);
9959 drm_gem_object_reference(&obj->base);
6b95a207 9960
f4510a27 9961 crtc->primary->fb = fb;
96b099fd 9962
e1f99ce6 9963 work->pending_flip_obj = obj;
e1f99ce6 9964
b4a98e57 9965 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9966 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9967
75f7f3ec 9968 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 9969 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 9970
4fa62c89
VS
9971 if (IS_VALLEYVIEW(dev)) {
9972 ring = &dev_priv->ring[BCS];
8e09bf83
CW
9973 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9974 /* vlv: DISPLAY_FLIP fails to change tiling */
9975 ring = NULL;
2a92d5bc
CW
9976 } else if (IS_IVYBRIDGE(dev)) {
9977 ring = &dev_priv->ring[BCS];
4fa62c89
VS
9978 } else if (INTEL_INFO(dev)->gen >= 7) {
9979 ring = obj->ring;
9980 if (ring == NULL || ring->id != RCS)
9981 ring = &dev_priv->ring[BCS];
9982 } else {
9983 ring = &dev_priv->ring[RCS];
9984 }
9985
9986 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
9987 if (ret)
9988 goto cleanup_pending;
6b95a207 9989
4fa62c89
VS
9990 work->gtt_offset =
9991 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9992
d6bbafa1 9993 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
9994 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9995 page_flip_flags);
d6bbafa1
CW
9996 if (ret)
9997 goto cleanup_unpin;
9998
9999 work->flip_queued_seqno = obj->last_write_seqno;
10000 work->flip_queued_ring = obj->ring;
10001 } else {
84c33a64 10002 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10003 page_flip_flags);
10004 if (ret)
10005 goto cleanup_unpin;
10006
10007 work->flip_queued_seqno = intel_ring_get_seqno(ring);
10008 work->flip_queued_ring = ring;
10009 }
10010
10011 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
10012 work->enable_stall_check = true;
4fa62c89 10013
a071fa00
DV
10014 i915_gem_track_fb(work->old_fb_obj, obj,
10015 INTEL_FRONTBUFFER_PRIMARY(pipe));
10016
7782de3b 10017 intel_disable_fbc(dev);
f99d7069 10018 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
10019 mutex_unlock(&dev->struct_mutex);
10020
e5510fac
JB
10021 trace_i915_flip_request(intel_crtc->plane, obj);
10022
6b95a207 10023 return 0;
96b099fd 10024
4fa62c89
VS
10025cleanup_unpin:
10026 intel_unpin_fb_obj(obj);
8c9f3aaf 10027cleanup_pending:
b4a98e57 10028 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 10029 crtc->primary->fb = old_fb;
05394f39
CW
10030 drm_gem_object_unreference(&work->old_fb_obj->base);
10031 drm_gem_object_unreference(&obj->base);
96b099fd
CW
10032 mutex_unlock(&dev->struct_mutex);
10033
79158103 10034cleanup:
96b099fd
CW
10035 spin_lock_irqsave(&dev->event_lock, flags);
10036 intel_crtc->unpin_work = NULL;
10037 spin_unlock_irqrestore(&dev->event_lock, flags);
10038
87b6b101 10039 drm_crtc_vblank_put(crtc);
7317c75e 10040free_work:
96b099fd
CW
10041 kfree(work);
10042
f900db47
CW
10043 if (ret == -EIO) {
10044out_hang:
10045 intel_crtc_wait_for_pending_flips(crtc);
10046 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
f0d3dad3
CW
10047 if (ret == 0 && event) {
10048 spin_lock_irqsave(&dev->event_lock, flags);
a071fa00 10049 drm_send_vblank_event(dev, pipe, event);
f0d3dad3
CW
10050 spin_unlock_irqrestore(&dev->event_lock, flags);
10051 }
f900db47 10052 }
96b099fd 10053 return ret;
6b95a207
KH
10054}
10055
f6e5b160 10056static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
10057 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10058 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
10059};
10060
9a935856
DV
10061/**
10062 * intel_modeset_update_staged_output_state
10063 *
10064 * Updates the staged output configuration state, e.g. after we've read out the
10065 * current hw state.
10066 */
10067static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 10068{
7668851f 10069 struct intel_crtc *crtc;
9a935856
DV
10070 struct intel_encoder *encoder;
10071 struct intel_connector *connector;
f6e5b160 10072
9a935856
DV
10073 list_for_each_entry(connector, &dev->mode_config.connector_list,
10074 base.head) {
10075 connector->new_encoder =
10076 to_intel_encoder(connector->base.encoder);
10077 }
f6e5b160 10078
b2784e15 10079 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10080 encoder->new_crtc =
10081 to_intel_crtc(encoder->base.crtc);
10082 }
7668851f 10083
d3fcc808 10084 for_each_intel_crtc(dev, crtc) {
7668851f 10085 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
10086
10087 if (crtc->new_enabled)
10088 crtc->new_config = &crtc->config;
10089 else
10090 crtc->new_config = NULL;
7668851f 10091 }
f6e5b160
CW
10092}
10093
9a935856
DV
10094/**
10095 * intel_modeset_commit_output_state
10096 *
10097 * This function copies the stage display pipe configuration to the real one.
10098 */
10099static void intel_modeset_commit_output_state(struct drm_device *dev)
10100{
7668851f 10101 struct intel_crtc *crtc;
9a935856
DV
10102 struct intel_encoder *encoder;
10103 struct intel_connector *connector;
f6e5b160 10104
9a935856
DV
10105 list_for_each_entry(connector, &dev->mode_config.connector_list,
10106 base.head) {
10107 connector->base.encoder = &connector->new_encoder->base;
10108 }
f6e5b160 10109
b2784e15 10110 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10111 encoder->base.crtc = &encoder->new_crtc->base;
10112 }
7668851f 10113
d3fcc808 10114 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10115 crtc->base.enabled = crtc->new_enabled;
10116 }
9a935856
DV
10117}
10118
050f7aeb 10119static void
eba905b2 10120connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
10121 struct intel_crtc_config *pipe_config)
10122{
10123 int bpp = pipe_config->pipe_bpp;
10124
10125 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10126 connector->base.base.id,
c23cc417 10127 connector->base.name);
050f7aeb
DV
10128
10129 /* Don't use an invalid EDID bpc value */
10130 if (connector->base.display_info.bpc &&
10131 connector->base.display_info.bpc * 3 < bpp) {
10132 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10133 bpp, connector->base.display_info.bpc*3);
10134 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10135 }
10136
10137 /* Clamp bpp to 8 on screens without EDID 1.4 */
10138 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10139 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10140 bpp);
10141 pipe_config->pipe_bpp = 24;
10142 }
10143}
10144
4e53c2e0 10145static int
050f7aeb
DV
10146compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10147 struct drm_framebuffer *fb,
10148 struct intel_crtc_config *pipe_config)
4e53c2e0 10149{
050f7aeb
DV
10150 struct drm_device *dev = crtc->base.dev;
10151 struct intel_connector *connector;
4e53c2e0
DV
10152 int bpp;
10153
d42264b1
DV
10154 switch (fb->pixel_format) {
10155 case DRM_FORMAT_C8:
4e53c2e0
DV
10156 bpp = 8*3; /* since we go through a colormap */
10157 break;
d42264b1
DV
10158 case DRM_FORMAT_XRGB1555:
10159 case DRM_FORMAT_ARGB1555:
10160 /* checked in intel_framebuffer_init already */
10161 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10162 return -EINVAL;
10163 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10164 bpp = 6*3; /* min is 18bpp */
10165 break;
d42264b1
DV
10166 case DRM_FORMAT_XBGR8888:
10167 case DRM_FORMAT_ABGR8888:
10168 /* checked in intel_framebuffer_init already */
10169 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10170 return -EINVAL;
10171 case DRM_FORMAT_XRGB8888:
10172 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10173 bpp = 8*3;
10174 break;
d42264b1
DV
10175 case DRM_FORMAT_XRGB2101010:
10176 case DRM_FORMAT_ARGB2101010:
10177 case DRM_FORMAT_XBGR2101010:
10178 case DRM_FORMAT_ABGR2101010:
10179 /* checked in intel_framebuffer_init already */
10180 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10181 return -EINVAL;
4e53c2e0
DV
10182 bpp = 10*3;
10183 break;
baba133a 10184 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10185 default:
10186 DRM_DEBUG_KMS("unsupported depth\n");
10187 return -EINVAL;
10188 }
10189
4e53c2e0
DV
10190 pipe_config->pipe_bpp = bpp;
10191
10192 /* Clamp display bpp to EDID value */
10193 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10194 base.head) {
1b829e05
DV
10195 if (!connector->new_encoder ||
10196 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10197 continue;
10198
050f7aeb 10199 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10200 }
10201
10202 return bpp;
10203}
10204
644db711
DV
10205static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10206{
10207 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10208 "type: 0x%x flags: 0x%x\n",
1342830c 10209 mode->crtc_clock,
644db711
DV
10210 mode->crtc_hdisplay, mode->crtc_hsync_start,
10211 mode->crtc_hsync_end, mode->crtc_htotal,
10212 mode->crtc_vdisplay, mode->crtc_vsync_start,
10213 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10214}
10215
c0b03411
DV
10216static void intel_dump_pipe_config(struct intel_crtc *crtc,
10217 struct intel_crtc_config *pipe_config,
10218 const char *context)
10219{
10220 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10221 context, pipe_name(crtc->pipe));
10222
10223 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10224 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10225 pipe_config->pipe_bpp, pipe_config->dither);
10226 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10227 pipe_config->has_pch_encoder,
10228 pipe_config->fdi_lanes,
10229 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10230 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10231 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10232 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10233 pipe_config->has_dp_encoder,
10234 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10235 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10236 pipe_config->dp_m_n.tu);
b95af8be
VK
10237
10238 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10239 pipe_config->has_dp_encoder,
10240 pipe_config->dp_m2_n2.gmch_m,
10241 pipe_config->dp_m2_n2.gmch_n,
10242 pipe_config->dp_m2_n2.link_m,
10243 pipe_config->dp_m2_n2.link_n,
10244 pipe_config->dp_m2_n2.tu);
10245
c0b03411
DV
10246 DRM_DEBUG_KMS("requested mode:\n");
10247 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10248 DRM_DEBUG_KMS("adjusted mode:\n");
10249 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10250 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10251 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10252 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10253 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10254 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10255 pipe_config->gmch_pfit.control,
10256 pipe_config->gmch_pfit.pgm_ratios,
10257 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10258 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10259 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10260 pipe_config->pch_pfit.size,
10261 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10262 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10263 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10264}
10265
bc079e8b
VS
10266static bool encoders_cloneable(const struct intel_encoder *a,
10267 const struct intel_encoder *b)
accfc0c5 10268{
bc079e8b
VS
10269 /* masks could be asymmetric, so check both ways */
10270 return a == b || (a->cloneable & (1 << b->type) &&
10271 b->cloneable & (1 << a->type));
10272}
10273
10274static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10275 struct intel_encoder *encoder)
10276{
10277 struct drm_device *dev = crtc->base.dev;
10278 struct intel_encoder *source_encoder;
10279
b2784e15 10280 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10281 if (source_encoder->new_crtc != crtc)
10282 continue;
10283
10284 if (!encoders_cloneable(encoder, source_encoder))
10285 return false;
10286 }
10287
10288 return true;
10289}
10290
10291static bool check_encoder_cloning(struct intel_crtc *crtc)
10292{
10293 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10294 struct intel_encoder *encoder;
10295
b2784e15 10296 for_each_intel_encoder(dev, encoder) {
bc079e8b 10297 if (encoder->new_crtc != crtc)
accfc0c5
DV
10298 continue;
10299
bc079e8b
VS
10300 if (!check_single_encoder_cloning(crtc, encoder))
10301 return false;
accfc0c5
DV
10302 }
10303
bc079e8b 10304 return true;
accfc0c5
DV
10305}
10306
b8cecdf5
DV
10307static struct intel_crtc_config *
10308intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10309 struct drm_framebuffer *fb,
b8cecdf5 10310 struct drm_display_mode *mode)
ee7b9f93 10311{
7758a113 10312 struct drm_device *dev = crtc->dev;
7758a113 10313 struct intel_encoder *encoder;
b8cecdf5 10314 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10315 int plane_bpp, ret = -EINVAL;
10316 bool retry = true;
ee7b9f93 10317
bc079e8b 10318 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10319 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10320 return ERR_PTR(-EINVAL);
10321 }
10322
b8cecdf5
DV
10323 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10324 if (!pipe_config)
7758a113
DV
10325 return ERR_PTR(-ENOMEM);
10326
b8cecdf5
DV
10327 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10328 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10329
e143a21c
DV
10330 pipe_config->cpu_transcoder =
10331 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10332 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10333
2960bc9c
ID
10334 /*
10335 * Sanitize sync polarity flags based on requested ones. If neither
10336 * positive or negative polarity is requested, treat this as meaning
10337 * negative polarity.
10338 */
10339 if (!(pipe_config->adjusted_mode.flags &
10340 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10341 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10342
10343 if (!(pipe_config->adjusted_mode.flags &
10344 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10345 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10346
050f7aeb
DV
10347 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10348 * plane pixel format and any sink constraints into account. Returns the
10349 * source plane bpp so that dithering can be selected on mismatches
10350 * after encoders and crtc also have had their say. */
10351 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10352 fb, pipe_config);
4e53c2e0
DV
10353 if (plane_bpp < 0)
10354 goto fail;
10355
e41a56be
VS
10356 /*
10357 * Determine the real pipe dimensions. Note that stereo modes can
10358 * increase the actual pipe size due to the frame doubling and
10359 * insertion of additional space for blanks between the frame. This
10360 * is stored in the crtc timings. We use the requested mode to do this
10361 * computation to clearly distinguish it from the adjusted mode, which
10362 * can be changed by the connectors in the below retry loop.
10363 */
10364 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10365 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10366 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10367
e29c22c0 10368encoder_retry:
ef1b460d 10369 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10370 pipe_config->port_clock = 0;
ef1b460d 10371 pipe_config->pixel_multiplier = 1;
ff9a6750 10372
135c81b8 10373 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10374 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10375
7758a113
DV
10376 /* Pass our mode to the connectors and the CRTC to give them a chance to
10377 * adjust it according to limitations or connector properties, and also
10378 * a chance to reject the mode entirely.
47f1c6c9 10379 */
b2784e15 10380 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10381
7758a113
DV
10382 if (&encoder->new_crtc->base != crtc)
10383 continue;
7ae89233 10384
efea6e8e
DV
10385 if (!(encoder->compute_config(encoder, pipe_config))) {
10386 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10387 goto fail;
10388 }
ee7b9f93 10389 }
47f1c6c9 10390
ff9a6750
DV
10391 /* Set default port clock if not overwritten by the encoder. Needs to be
10392 * done afterwards in case the encoder adjusts the mode. */
10393 if (!pipe_config->port_clock)
241bfc38
DL
10394 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10395 * pipe_config->pixel_multiplier;
ff9a6750 10396
a43f6e0f 10397 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10398 if (ret < 0) {
7758a113
DV
10399 DRM_DEBUG_KMS("CRTC fixup failed\n");
10400 goto fail;
ee7b9f93 10401 }
e29c22c0
DV
10402
10403 if (ret == RETRY) {
10404 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10405 ret = -EINVAL;
10406 goto fail;
10407 }
10408
10409 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10410 retry = false;
10411 goto encoder_retry;
10412 }
10413
4e53c2e0
DV
10414 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10415 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10416 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10417
b8cecdf5 10418 return pipe_config;
7758a113 10419fail:
b8cecdf5 10420 kfree(pipe_config);
e29c22c0 10421 return ERR_PTR(ret);
ee7b9f93 10422}
47f1c6c9 10423
e2e1ed41
DV
10424/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10425 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10426static void
10427intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10428 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10429{
10430 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10431 struct drm_device *dev = crtc->dev;
10432 struct intel_encoder *encoder;
10433 struct intel_connector *connector;
10434 struct drm_crtc *tmp_crtc;
79e53945 10435
e2e1ed41 10436 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10437
e2e1ed41
DV
10438 /* Check which crtcs have changed outputs connected to them, these need
10439 * to be part of the prepare_pipes mask. We don't (yet) support global
10440 * modeset across multiple crtcs, so modeset_pipes will only have one
10441 * bit set at most. */
10442 list_for_each_entry(connector, &dev->mode_config.connector_list,
10443 base.head) {
10444 if (connector->base.encoder == &connector->new_encoder->base)
10445 continue;
79e53945 10446
e2e1ed41
DV
10447 if (connector->base.encoder) {
10448 tmp_crtc = connector->base.encoder->crtc;
10449
10450 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10451 }
10452
10453 if (connector->new_encoder)
10454 *prepare_pipes |=
10455 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10456 }
10457
b2784e15 10458 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10459 if (encoder->base.crtc == &encoder->new_crtc->base)
10460 continue;
10461
10462 if (encoder->base.crtc) {
10463 tmp_crtc = encoder->base.crtc;
10464
10465 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10466 }
10467
10468 if (encoder->new_crtc)
10469 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10470 }
10471
7668851f 10472 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10473 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10474 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10475 continue;
7e7d76c3 10476
7668851f 10477 if (!intel_crtc->new_enabled)
e2e1ed41 10478 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10479 else
10480 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10481 }
10482
e2e1ed41
DV
10483
10484 /* set_mode is also used to update properties on life display pipes. */
10485 intel_crtc = to_intel_crtc(crtc);
7668851f 10486 if (intel_crtc->new_enabled)
e2e1ed41
DV
10487 *prepare_pipes |= 1 << intel_crtc->pipe;
10488
b6c5164d
DV
10489 /*
10490 * For simplicity do a full modeset on any pipe where the output routing
10491 * changed. We could be more clever, but that would require us to be
10492 * more careful with calling the relevant encoder->mode_set functions.
10493 */
e2e1ed41
DV
10494 if (*prepare_pipes)
10495 *modeset_pipes = *prepare_pipes;
10496
10497 /* ... and mask these out. */
10498 *modeset_pipes &= ~(*disable_pipes);
10499 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10500
10501 /*
10502 * HACK: We don't (yet) fully support global modesets. intel_set_config
10503 * obies this rule, but the modeset restore mode of
10504 * intel_modeset_setup_hw_state does not.
10505 */
10506 *modeset_pipes &= 1 << intel_crtc->pipe;
10507 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10508
10509 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10510 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10511}
79e53945 10512
ea9d758d 10513static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10514{
ea9d758d 10515 struct drm_encoder *encoder;
f6e5b160 10516 struct drm_device *dev = crtc->dev;
f6e5b160 10517
ea9d758d
DV
10518 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10519 if (encoder->crtc == crtc)
10520 return true;
10521
10522 return false;
10523}
10524
10525static void
10526intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10527{
10528 struct intel_encoder *intel_encoder;
10529 struct intel_crtc *intel_crtc;
10530 struct drm_connector *connector;
10531
b2784e15 10532 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10533 if (!intel_encoder->base.crtc)
10534 continue;
10535
10536 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10537
10538 if (prepare_pipes & (1 << intel_crtc->pipe))
10539 intel_encoder->connectors_active = false;
10540 }
10541
10542 intel_modeset_commit_output_state(dev);
10543
7668851f 10544 /* Double check state. */
d3fcc808 10545 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10546 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10547 WARN_ON(intel_crtc->new_config &&
10548 intel_crtc->new_config != &intel_crtc->config);
10549 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10550 }
10551
10552 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10553 if (!connector->encoder || !connector->encoder->crtc)
10554 continue;
10555
10556 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10557
10558 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10559 struct drm_property *dpms_property =
10560 dev->mode_config.dpms_property;
10561
ea9d758d 10562 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10563 drm_object_property_set_value(&connector->base,
68d34720
DV
10564 dpms_property,
10565 DRM_MODE_DPMS_ON);
ea9d758d
DV
10566
10567 intel_encoder = to_intel_encoder(connector->encoder);
10568 intel_encoder->connectors_active = true;
10569 }
10570 }
10571
10572}
10573
3bd26263 10574static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10575{
3bd26263 10576 int diff;
f1f644dc
JB
10577
10578 if (clock1 == clock2)
10579 return true;
10580
10581 if (!clock1 || !clock2)
10582 return false;
10583
10584 diff = abs(clock1 - clock2);
10585
10586 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10587 return true;
10588
10589 return false;
10590}
10591
25c5b266
DV
10592#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10593 list_for_each_entry((intel_crtc), \
10594 &(dev)->mode_config.crtc_list, \
10595 base.head) \
0973f18f 10596 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10597
0e8ffe1b 10598static bool
2fa2fe9a
DV
10599intel_pipe_config_compare(struct drm_device *dev,
10600 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10601 struct intel_crtc_config *pipe_config)
10602{
66e985c0
DV
10603#define PIPE_CONF_CHECK_X(name) \
10604 if (current_config->name != pipe_config->name) { \
10605 DRM_ERROR("mismatch in " #name " " \
10606 "(expected 0x%08x, found 0x%08x)\n", \
10607 current_config->name, \
10608 pipe_config->name); \
10609 return false; \
10610 }
10611
08a24034
DV
10612#define PIPE_CONF_CHECK_I(name) \
10613 if (current_config->name != pipe_config->name) { \
10614 DRM_ERROR("mismatch in " #name " " \
10615 "(expected %i, found %i)\n", \
10616 current_config->name, \
10617 pipe_config->name); \
10618 return false; \
88adfff1
DV
10619 }
10620
b95af8be
VK
10621/* This is required for BDW+ where there is only one set of registers for
10622 * switching between high and low RR.
10623 * This macro can be used whenever a comparison has to be made between one
10624 * hw state and multiple sw state variables.
10625 */
10626#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10627 if ((current_config->name != pipe_config->name) && \
10628 (current_config->alt_name != pipe_config->name)) { \
10629 DRM_ERROR("mismatch in " #name " " \
10630 "(expected %i or %i, found %i)\n", \
10631 current_config->name, \
10632 current_config->alt_name, \
10633 pipe_config->name); \
10634 return false; \
10635 }
10636
1bd1bd80
DV
10637#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10638 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10639 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10640 "(expected %i, found %i)\n", \
10641 current_config->name & (mask), \
10642 pipe_config->name & (mask)); \
10643 return false; \
10644 }
10645
5e550656
VS
10646#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10647 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10648 DRM_ERROR("mismatch in " #name " " \
10649 "(expected %i, found %i)\n", \
10650 current_config->name, \
10651 pipe_config->name); \
10652 return false; \
10653 }
10654
bb760063
DV
10655#define PIPE_CONF_QUIRK(quirk) \
10656 ((current_config->quirks | pipe_config->quirks) & (quirk))
10657
eccb140b
DV
10658 PIPE_CONF_CHECK_I(cpu_transcoder);
10659
08a24034
DV
10660 PIPE_CONF_CHECK_I(has_pch_encoder);
10661 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10662 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10663 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10664 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10665 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10666 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10667
eb14cb74 10668 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10669
10670 if (INTEL_INFO(dev)->gen < 8) {
10671 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10672 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10673 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10674 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10675 PIPE_CONF_CHECK_I(dp_m_n.tu);
10676
10677 if (current_config->has_drrs) {
10678 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10679 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10680 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10681 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10682 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10683 }
10684 } else {
10685 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10686 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10687 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10688 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10689 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10690 }
eb14cb74 10691
1bd1bd80
DV
10692 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10693 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10694 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10695 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10696 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10697 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10698
10699 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10700 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10701 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10702 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10703 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10704 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10705
c93f54cf 10706 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10707 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10708 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10709 IS_VALLEYVIEW(dev))
10710 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10711
9ed109a7
DV
10712 PIPE_CONF_CHECK_I(has_audio);
10713
1bd1bd80
DV
10714 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10715 DRM_MODE_FLAG_INTERLACE);
10716
bb760063
DV
10717 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10718 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10719 DRM_MODE_FLAG_PHSYNC);
10720 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10721 DRM_MODE_FLAG_NHSYNC);
10722 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10723 DRM_MODE_FLAG_PVSYNC);
10724 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10725 DRM_MODE_FLAG_NVSYNC);
10726 }
045ac3b5 10727
37327abd
VS
10728 PIPE_CONF_CHECK_I(pipe_src_w);
10729 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10730
9953599b
DV
10731 /*
10732 * FIXME: BIOS likes to set up a cloned config with lvds+external
10733 * screen. Since we don't yet re-compute the pipe config when moving
10734 * just the lvds port away to another pipe the sw tracking won't match.
10735 *
10736 * Proper atomic modesets with recomputed global state will fix this.
10737 * Until then just don't check gmch state for inherited modes.
10738 */
10739 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10740 PIPE_CONF_CHECK_I(gmch_pfit.control);
10741 /* pfit ratios are autocomputed by the hw on gen4+ */
10742 if (INTEL_INFO(dev)->gen < 4)
10743 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10744 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10745 }
10746
fd4daa9c
CW
10747 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10748 if (current_config->pch_pfit.enabled) {
10749 PIPE_CONF_CHECK_I(pch_pfit.pos);
10750 PIPE_CONF_CHECK_I(pch_pfit.size);
10751 }
2fa2fe9a 10752
e59150dc
JB
10753 /* BDW+ don't expose a synchronous way to read the state */
10754 if (IS_HASWELL(dev))
10755 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10756
282740f7
VS
10757 PIPE_CONF_CHECK_I(double_wide);
10758
26804afd
DV
10759 PIPE_CONF_CHECK_X(ddi_pll_sel);
10760
c0d43d62 10761 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10762 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10763 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10764 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10765 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10766 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10767
42571aef
VS
10768 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10769 PIPE_CONF_CHECK_I(pipe_bpp);
10770
a9a7e98a
JB
10771 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10772 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10773
66e985c0 10774#undef PIPE_CONF_CHECK_X
08a24034 10775#undef PIPE_CONF_CHECK_I
b95af8be 10776#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10777#undef PIPE_CONF_CHECK_FLAGS
5e550656 10778#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10779#undef PIPE_CONF_QUIRK
88adfff1 10780
0e8ffe1b
DV
10781 return true;
10782}
10783
91d1b4bd
DV
10784static void
10785check_connector_state(struct drm_device *dev)
8af6cf88 10786{
8af6cf88
DV
10787 struct intel_connector *connector;
10788
10789 list_for_each_entry(connector, &dev->mode_config.connector_list,
10790 base.head) {
10791 /* This also checks the encoder/connector hw state with the
10792 * ->get_hw_state callbacks. */
10793 intel_connector_check_state(connector);
10794
10795 WARN(&connector->new_encoder->base != connector->base.encoder,
10796 "connector's staged encoder doesn't match current encoder\n");
10797 }
91d1b4bd
DV
10798}
10799
10800static void
10801check_encoder_state(struct drm_device *dev)
10802{
10803 struct intel_encoder *encoder;
10804 struct intel_connector *connector;
8af6cf88 10805
b2784e15 10806 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10807 bool enabled = false;
10808 bool active = false;
10809 enum pipe pipe, tracked_pipe;
10810
10811 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10812 encoder->base.base.id,
8e329a03 10813 encoder->base.name);
8af6cf88
DV
10814
10815 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10816 "encoder's stage crtc doesn't match current crtc\n");
10817 WARN(encoder->connectors_active && !encoder->base.crtc,
10818 "encoder's active_connectors set, but no crtc\n");
10819
10820 list_for_each_entry(connector, &dev->mode_config.connector_list,
10821 base.head) {
10822 if (connector->base.encoder != &encoder->base)
10823 continue;
10824 enabled = true;
10825 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10826 active = true;
10827 }
0e32b39c
DA
10828 /*
10829 * for MST connectors if we unplug the connector is gone
10830 * away but the encoder is still connected to a crtc
10831 * until a modeset happens in response to the hotplug.
10832 */
10833 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10834 continue;
10835
8af6cf88
DV
10836 WARN(!!encoder->base.crtc != enabled,
10837 "encoder's enabled state mismatch "
10838 "(expected %i, found %i)\n",
10839 !!encoder->base.crtc, enabled);
10840 WARN(active && !encoder->base.crtc,
10841 "active encoder with no crtc\n");
10842
10843 WARN(encoder->connectors_active != active,
10844 "encoder's computed active state doesn't match tracked active state "
10845 "(expected %i, found %i)\n", active, encoder->connectors_active);
10846
10847 active = encoder->get_hw_state(encoder, &pipe);
10848 WARN(active != encoder->connectors_active,
10849 "encoder's hw state doesn't match sw tracking "
10850 "(expected %i, found %i)\n",
10851 encoder->connectors_active, active);
10852
10853 if (!encoder->base.crtc)
10854 continue;
10855
10856 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10857 WARN(active && pipe != tracked_pipe,
10858 "active encoder's pipe doesn't match"
10859 "(expected %i, found %i)\n",
10860 tracked_pipe, pipe);
10861
10862 }
91d1b4bd
DV
10863}
10864
10865static void
10866check_crtc_state(struct drm_device *dev)
10867{
fbee40df 10868 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10869 struct intel_crtc *crtc;
10870 struct intel_encoder *encoder;
10871 struct intel_crtc_config pipe_config;
8af6cf88 10872
d3fcc808 10873 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10874 bool enabled = false;
10875 bool active = false;
10876
045ac3b5
JB
10877 memset(&pipe_config, 0, sizeof(pipe_config));
10878
8af6cf88
DV
10879 DRM_DEBUG_KMS("[CRTC:%d]\n",
10880 crtc->base.base.id);
10881
10882 WARN(crtc->active && !crtc->base.enabled,
10883 "active crtc, but not enabled in sw tracking\n");
10884
b2784e15 10885 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10886 if (encoder->base.crtc != &crtc->base)
10887 continue;
10888 enabled = true;
10889 if (encoder->connectors_active)
10890 active = true;
10891 }
6c49f241 10892
8af6cf88
DV
10893 WARN(active != crtc->active,
10894 "crtc's computed active state doesn't match tracked active state "
10895 "(expected %i, found %i)\n", active, crtc->active);
10896 WARN(enabled != crtc->base.enabled,
10897 "crtc's computed enabled state doesn't match tracked enabled state "
10898 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10899
0e8ffe1b
DV
10900 active = dev_priv->display.get_pipe_config(crtc,
10901 &pipe_config);
d62cf62a 10902
b6b5d049
VS
10903 /* hw state is inconsistent with the pipe quirk */
10904 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10905 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10906 active = crtc->active;
10907
b2784e15 10908 for_each_intel_encoder(dev, encoder) {
3eaba51c 10909 enum pipe pipe;
6c49f241
DV
10910 if (encoder->base.crtc != &crtc->base)
10911 continue;
1d37b689 10912 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10913 encoder->get_config(encoder, &pipe_config);
10914 }
10915
0e8ffe1b
DV
10916 WARN(crtc->active != active,
10917 "crtc active state doesn't match with hw state "
10918 "(expected %i, found %i)\n", crtc->active, active);
10919
c0b03411
DV
10920 if (active &&
10921 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10922 WARN(1, "pipe state doesn't match!\n");
10923 intel_dump_pipe_config(crtc, &pipe_config,
10924 "[hw state]");
10925 intel_dump_pipe_config(crtc, &crtc->config,
10926 "[sw state]");
10927 }
8af6cf88
DV
10928 }
10929}
10930
91d1b4bd
DV
10931static void
10932check_shared_dpll_state(struct drm_device *dev)
10933{
fbee40df 10934 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10935 struct intel_crtc *crtc;
10936 struct intel_dpll_hw_state dpll_hw_state;
10937 int i;
5358901f
DV
10938
10939 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10940 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10941 int enabled_crtcs = 0, active_crtcs = 0;
10942 bool active;
10943
10944 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10945
10946 DRM_DEBUG_KMS("%s\n", pll->name);
10947
10948 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10949
10950 WARN(pll->active > pll->refcount,
10951 "more active pll users than references: %i vs %i\n",
10952 pll->active, pll->refcount);
10953 WARN(pll->active && !pll->on,
10954 "pll in active use but not on in sw tracking\n");
35c95375
DV
10955 WARN(pll->on && !pll->active,
10956 "pll in on but not on in use in sw tracking\n");
5358901f
DV
10957 WARN(pll->on != active,
10958 "pll on state mismatch (expected %i, found %i)\n",
10959 pll->on, active);
10960
d3fcc808 10961 for_each_intel_crtc(dev, crtc) {
5358901f
DV
10962 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10963 enabled_crtcs++;
10964 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10965 active_crtcs++;
10966 }
10967 WARN(pll->active != active_crtcs,
10968 "pll active crtcs mismatch (expected %i, found %i)\n",
10969 pll->active, active_crtcs);
10970 WARN(pll->refcount != enabled_crtcs,
10971 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10972 pll->refcount, enabled_crtcs);
66e985c0
DV
10973
10974 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10975 sizeof(dpll_hw_state)),
10976 "pll hw state mismatch\n");
5358901f 10977 }
8af6cf88
DV
10978}
10979
91d1b4bd
DV
10980void
10981intel_modeset_check_state(struct drm_device *dev)
10982{
10983 check_connector_state(dev);
10984 check_encoder_state(dev);
10985 check_crtc_state(dev);
10986 check_shared_dpll_state(dev);
10987}
10988
18442d08
VS
10989void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10990 int dotclock)
10991{
10992 /*
10993 * FDI already provided one idea for the dotclock.
10994 * Yell if the encoder disagrees.
10995 */
241bfc38 10996 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10997 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10998 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10999}
11000
80715b2f
VS
11001static void update_scanline_offset(struct intel_crtc *crtc)
11002{
11003 struct drm_device *dev = crtc->base.dev;
11004
11005 /*
11006 * The scanline counter increments at the leading edge of hsync.
11007 *
11008 * On most platforms it starts counting from vtotal-1 on the
11009 * first active line. That means the scanline counter value is
11010 * always one less than what we would expect. Ie. just after
11011 * start of vblank, which also occurs at start of hsync (on the
11012 * last active line), the scanline counter will read vblank_start-1.
11013 *
11014 * On gen2 the scanline counter starts counting from 1 instead
11015 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11016 * to keep the value positive), instead of adding one.
11017 *
11018 * On HSW+ the behaviour of the scanline counter depends on the output
11019 * type. For DP ports it behaves like most other platforms, but on HDMI
11020 * there's an extra 1 line difference. So we need to add two instead of
11021 * one to the value.
11022 */
11023 if (IS_GEN2(dev)) {
11024 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
11025 int vtotal;
11026
11027 vtotal = mode->crtc_vtotal;
11028 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11029 vtotal /= 2;
11030
11031 crtc->scanline_offset = vtotal - 1;
11032 } else if (HAS_DDI(dev) &&
11033 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
11034 crtc->scanline_offset = 2;
11035 } else
11036 crtc->scanline_offset = 1;
11037}
11038
f30da187
DV
11039static int __intel_set_mode(struct drm_crtc *crtc,
11040 struct drm_display_mode *mode,
11041 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
11042{
11043 struct drm_device *dev = crtc->dev;
fbee40df 11044 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 11045 struct drm_display_mode *saved_mode;
b8cecdf5 11046 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
11047 struct intel_crtc *intel_crtc;
11048 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 11049 int ret = 0;
a6778b3c 11050
4b4b9238 11051 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
11052 if (!saved_mode)
11053 return -ENOMEM;
a6778b3c 11054
e2e1ed41 11055 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
11056 &prepare_pipes, &disable_pipes);
11057
3ac18232 11058 *saved_mode = crtc->mode;
a6778b3c 11059
25c5b266
DV
11060 /* Hack: Because we don't (yet) support global modeset on multiple
11061 * crtcs, we don't keep track of the new mode for more than one crtc.
11062 * Hence simply check whether any bit is set in modeset_pipes in all the
11063 * pieces of code that are not yet converted to deal with mutliple crtcs
11064 * changing their mode at the same time. */
25c5b266 11065 if (modeset_pipes) {
4e53c2e0 11066 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
11067 if (IS_ERR(pipe_config)) {
11068 ret = PTR_ERR(pipe_config);
11069 pipe_config = NULL;
11070
3ac18232 11071 goto out;
25c5b266 11072 }
c0b03411
DV
11073 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11074 "[modeset]");
50741abc 11075 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 11076 }
a6778b3c 11077
30a970c6
JB
11078 /*
11079 * See if the config requires any additional preparation, e.g.
11080 * to adjust global state with pipes off. We need to do this
11081 * here so we can get the modeset_pipe updated config for the new
11082 * mode set on this crtc. For other crtcs we need to use the
11083 * adjusted_mode bits in the crtc directly.
11084 */
c164f833 11085 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 11086 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 11087
c164f833
VS
11088 /* may have added more to prepare_pipes than we should */
11089 prepare_pipes &= ~disable_pipes;
11090 }
11091
460da916
DV
11092 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11093 intel_crtc_disable(&intel_crtc->base);
11094
ea9d758d
DV
11095 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11096 if (intel_crtc->base.enabled)
11097 dev_priv->display.crtc_disable(&intel_crtc->base);
11098 }
a6778b3c 11099
6c4c86f5
DV
11100 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11101 * to set it here already despite that we pass it down the callchain.
f6e5b160 11102 */
b8cecdf5 11103 if (modeset_pipes) {
25c5b266 11104 crtc->mode = *mode;
b8cecdf5
DV
11105 /* mode_set/enable/disable functions rely on a correct pipe
11106 * config. */
11107 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 11108 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
11109
11110 /*
11111 * Calculate and store various constants which
11112 * are later needed by vblank and swap-completion
11113 * timestamping. They are derived from true hwmode.
11114 */
11115 drm_calc_timestamping_constants(crtc,
11116 &pipe_config->adjusted_mode);
b8cecdf5 11117 }
7758a113 11118
ea9d758d
DV
11119 /* Only after disabling all output pipelines that will be changed can we
11120 * update the the output configuration. */
11121 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11122
47fab737
DV
11123 if (dev_priv->display.modeset_global_resources)
11124 dev_priv->display.modeset_global_resources(dev);
11125
a6778b3c
DV
11126 /* Set up the DPLL and any encoders state that needs to adjust or depend
11127 * on the DPLL.
f6e5b160 11128 */
25c5b266 11129 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
11130 struct drm_framebuffer *old_fb = crtc->primary->fb;
11131 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11132 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
11133
11134 mutex_lock(&dev->struct_mutex);
11135 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 11136 obj,
4c10794f
DV
11137 NULL);
11138 if (ret != 0) {
11139 DRM_ERROR("pin & fence failed\n");
11140 mutex_unlock(&dev->struct_mutex);
11141 goto done;
11142 }
2ff8fde1 11143 if (old_fb)
a071fa00 11144 intel_unpin_fb_obj(old_obj);
a071fa00
DV
11145 i915_gem_track_fb(old_obj, obj,
11146 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
11147 mutex_unlock(&dev->struct_mutex);
11148
11149 crtc->primary->fb = fb;
11150 crtc->x = x;
11151 crtc->y = y;
11152
4271b753
DV
11153 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11154 x, y, fb);
c0c36b94
CW
11155 if (ret)
11156 goto done;
a6778b3c
DV
11157 }
11158
11159 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11160 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11161 update_scanline_offset(intel_crtc);
11162
25c5b266 11163 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11164 }
a6778b3c 11165
a6778b3c
DV
11166 /* FIXME: add subpixel order */
11167done:
4b4b9238 11168 if (ret && crtc->enabled)
3ac18232 11169 crtc->mode = *saved_mode;
a6778b3c 11170
3ac18232 11171out:
b8cecdf5 11172 kfree(pipe_config);
3ac18232 11173 kfree(saved_mode);
a6778b3c 11174 return ret;
f6e5b160
CW
11175}
11176
e7457a9a
DL
11177static int intel_set_mode(struct drm_crtc *crtc,
11178 struct drm_display_mode *mode,
11179 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11180{
11181 int ret;
11182
11183 ret = __intel_set_mode(crtc, mode, x, y, fb);
11184
11185 if (ret == 0)
11186 intel_modeset_check_state(crtc->dev);
11187
11188 return ret;
11189}
11190
c0c36b94
CW
11191void intel_crtc_restore_mode(struct drm_crtc *crtc)
11192{
f4510a27 11193 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11194}
11195
25c5b266
DV
11196#undef for_each_intel_crtc_masked
11197
d9e55608
DV
11198static void intel_set_config_free(struct intel_set_config *config)
11199{
11200 if (!config)
11201 return;
11202
1aa4b628
DV
11203 kfree(config->save_connector_encoders);
11204 kfree(config->save_encoder_crtcs);
7668851f 11205 kfree(config->save_crtc_enabled);
d9e55608
DV
11206 kfree(config);
11207}
11208
85f9eb71
DV
11209static int intel_set_config_save_state(struct drm_device *dev,
11210 struct intel_set_config *config)
11211{
7668851f 11212 struct drm_crtc *crtc;
85f9eb71
DV
11213 struct drm_encoder *encoder;
11214 struct drm_connector *connector;
11215 int count;
11216
7668851f
VS
11217 config->save_crtc_enabled =
11218 kcalloc(dev->mode_config.num_crtc,
11219 sizeof(bool), GFP_KERNEL);
11220 if (!config->save_crtc_enabled)
11221 return -ENOMEM;
11222
1aa4b628
DV
11223 config->save_encoder_crtcs =
11224 kcalloc(dev->mode_config.num_encoder,
11225 sizeof(struct drm_crtc *), GFP_KERNEL);
11226 if (!config->save_encoder_crtcs)
85f9eb71
DV
11227 return -ENOMEM;
11228
1aa4b628
DV
11229 config->save_connector_encoders =
11230 kcalloc(dev->mode_config.num_connector,
11231 sizeof(struct drm_encoder *), GFP_KERNEL);
11232 if (!config->save_connector_encoders)
85f9eb71
DV
11233 return -ENOMEM;
11234
11235 /* Copy data. Note that driver private data is not affected.
11236 * Should anything bad happen only the expected state is
11237 * restored, not the drivers personal bookkeeping.
11238 */
7668851f 11239 count = 0;
70e1e0ec 11240 for_each_crtc(dev, crtc) {
7668851f
VS
11241 config->save_crtc_enabled[count++] = crtc->enabled;
11242 }
11243
85f9eb71
DV
11244 count = 0;
11245 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11246 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11247 }
11248
11249 count = 0;
11250 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11251 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11252 }
11253
11254 return 0;
11255}
11256
11257static void intel_set_config_restore_state(struct drm_device *dev,
11258 struct intel_set_config *config)
11259{
7668851f 11260 struct intel_crtc *crtc;
9a935856
DV
11261 struct intel_encoder *encoder;
11262 struct intel_connector *connector;
85f9eb71
DV
11263 int count;
11264
7668851f 11265 count = 0;
d3fcc808 11266 for_each_intel_crtc(dev, crtc) {
7668851f 11267 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11268
11269 if (crtc->new_enabled)
11270 crtc->new_config = &crtc->config;
11271 else
11272 crtc->new_config = NULL;
7668851f
VS
11273 }
11274
85f9eb71 11275 count = 0;
b2784e15 11276 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11277 encoder->new_crtc =
11278 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11279 }
11280
11281 count = 0;
9a935856
DV
11282 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11283 connector->new_encoder =
11284 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11285 }
11286}
11287
e3de42b6 11288static bool
2e57f47d 11289is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11290{
11291 int i;
11292
2e57f47d
CW
11293 if (set->num_connectors == 0)
11294 return false;
11295
11296 if (WARN_ON(set->connectors == NULL))
11297 return false;
11298
11299 for (i = 0; i < set->num_connectors; i++)
11300 if (set->connectors[i]->encoder &&
11301 set->connectors[i]->encoder->crtc == set->crtc &&
11302 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11303 return true;
11304
11305 return false;
11306}
11307
5e2b584e
DV
11308static void
11309intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11310 struct intel_set_config *config)
11311{
11312
11313 /* We should be able to check here if the fb has the same properties
11314 * and then just flip_or_move it */
2e57f47d
CW
11315 if (is_crtc_connector_off(set)) {
11316 config->mode_changed = true;
f4510a27 11317 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11318 /*
11319 * If we have no fb, we can only flip as long as the crtc is
11320 * active, otherwise we need a full mode set. The crtc may
11321 * be active if we've only disabled the primary plane, or
11322 * in fastboot situations.
11323 */
f4510a27 11324 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11325 struct intel_crtc *intel_crtc =
11326 to_intel_crtc(set->crtc);
11327
3b150f08 11328 if (intel_crtc->active) {
319d9827
JB
11329 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11330 config->fb_changed = true;
11331 } else {
11332 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11333 config->mode_changed = true;
11334 }
5e2b584e
DV
11335 } else if (set->fb == NULL) {
11336 config->mode_changed = true;
72f4901e 11337 } else if (set->fb->pixel_format !=
f4510a27 11338 set->crtc->primary->fb->pixel_format) {
5e2b584e 11339 config->mode_changed = true;
e3de42b6 11340 } else {
5e2b584e 11341 config->fb_changed = true;
e3de42b6 11342 }
5e2b584e
DV
11343 }
11344
835c5873 11345 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11346 config->fb_changed = true;
11347
11348 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11349 DRM_DEBUG_KMS("modes are different, full mode set\n");
11350 drm_mode_debug_printmodeline(&set->crtc->mode);
11351 drm_mode_debug_printmodeline(set->mode);
11352 config->mode_changed = true;
11353 }
a1d95703
CW
11354
11355 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11356 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11357}
11358
2e431051 11359static int
9a935856
DV
11360intel_modeset_stage_output_state(struct drm_device *dev,
11361 struct drm_mode_set *set,
11362 struct intel_set_config *config)
50f56119 11363{
9a935856
DV
11364 struct intel_connector *connector;
11365 struct intel_encoder *encoder;
7668851f 11366 struct intel_crtc *crtc;
f3f08572 11367 int ro;
50f56119 11368
9abdda74 11369 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11370 * of connectors. For paranoia, double-check this. */
11371 WARN_ON(!set->fb && (set->num_connectors != 0));
11372 WARN_ON(set->fb && (set->num_connectors == 0));
11373
9a935856
DV
11374 list_for_each_entry(connector, &dev->mode_config.connector_list,
11375 base.head) {
11376 /* Otherwise traverse passed in connector list and get encoders
11377 * for them. */
50f56119 11378 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11379 if (set->connectors[ro] == &connector->base) {
0e32b39c 11380 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11381 break;
11382 }
11383 }
11384
9a935856
DV
11385 /* If we disable the crtc, disable all its connectors. Also, if
11386 * the connector is on the changing crtc but not on the new
11387 * connector list, disable it. */
11388 if ((!set->fb || ro == set->num_connectors) &&
11389 connector->base.encoder &&
11390 connector->base.encoder->crtc == set->crtc) {
11391 connector->new_encoder = NULL;
11392
11393 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11394 connector->base.base.id,
c23cc417 11395 connector->base.name);
9a935856
DV
11396 }
11397
11398
11399 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11400 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11401 config->mode_changed = true;
50f56119
DV
11402 }
11403 }
9a935856 11404 /* connector->new_encoder is now updated for all connectors. */
50f56119 11405
9a935856 11406 /* Update crtc of enabled connectors. */
9a935856
DV
11407 list_for_each_entry(connector, &dev->mode_config.connector_list,
11408 base.head) {
7668851f
VS
11409 struct drm_crtc *new_crtc;
11410
9a935856 11411 if (!connector->new_encoder)
50f56119
DV
11412 continue;
11413
9a935856 11414 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11415
11416 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11417 if (set->connectors[ro] == &connector->base)
50f56119
DV
11418 new_crtc = set->crtc;
11419 }
11420
11421 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11422 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11423 new_crtc)) {
5e2b584e 11424 return -EINVAL;
50f56119 11425 }
0e32b39c 11426 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11427
11428 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11429 connector->base.base.id,
c23cc417 11430 connector->base.name,
9a935856
DV
11431 new_crtc->base.id);
11432 }
11433
11434 /* Check for any encoders that needs to be disabled. */
b2784e15 11435 for_each_intel_encoder(dev, encoder) {
5a65f358 11436 int num_connectors = 0;
9a935856
DV
11437 list_for_each_entry(connector,
11438 &dev->mode_config.connector_list,
11439 base.head) {
11440 if (connector->new_encoder == encoder) {
11441 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11442 num_connectors++;
9a935856
DV
11443 }
11444 }
5a65f358
PZ
11445
11446 if (num_connectors == 0)
11447 encoder->new_crtc = NULL;
11448 else if (num_connectors > 1)
11449 return -EINVAL;
11450
9a935856
DV
11451 /* Only now check for crtc changes so we don't miss encoders
11452 * that will be disabled. */
11453 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11454 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11455 config->mode_changed = true;
50f56119
DV
11456 }
11457 }
9a935856 11458 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11459 list_for_each_entry(connector, &dev->mode_config.connector_list,
11460 base.head) {
11461 if (connector->new_encoder)
11462 if (connector->new_encoder != connector->encoder)
11463 connector->encoder = connector->new_encoder;
11464 }
d3fcc808 11465 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11466 crtc->new_enabled = false;
11467
b2784e15 11468 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11469 if (encoder->new_crtc == crtc) {
11470 crtc->new_enabled = true;
11471 break;
11472 }
11473 }
11474
11475 if (crtc->new_enabled != crtc->base.enabled) {
11476 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11477 crtc->new_enabled ? "en" : "dis");
11478 config->mode_changed = true;
11479 }
7bd0a8e7
VS
11480
11481 if (crtc->new_enabled)
11482 crtc->new_config = &crtc->config;
11483 else
11484 crtc->new_config = NULL;
7668851f
VS
11485 }
11486
2e431051
DV
11487 return 0;
11488}
11489
7d00a1f5
VS
11490static void disable_crtc_nofb(struct intel_crtc *crtc)
11491{
11492 struct drm_device *dev = crtc->base.dev;
11493 struct intel_encoder *encoder;
11494 struct intel_connector *connector;
11495
11496 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11497 pipe_name(crtc->pipe));
11498
11499 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11500 if (connector->new_encoder &&
11501 connector->new_encoder->new_crtc == crtc)
11502 connector->new_encoder = NULL;
11503 }
11504
b2784e15 11505 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11506 if (encoder->new_crtc == crtc)
11507 encoder->new_crtc = NULL;
11508 }
11509
11510 crtc->new_enabled = false;
7bd0a8e7 11511 crtc->new_config = NULL;
7d00a1f5
VS
11512}
11513
2e431051
DV
11514static int intel_crtc_set_config(struct drm_mode_set *set)
11515{
11516 struct drm_device *dev;
2e431051
DV
11517 struct drm_mode_set save_set;
11518 struct intel_set_config *config;
11519 int ret;
2e431051 11520
8d3e375e
DV
11521 BUG_ON(!set);
11522 BUG_ON(!set->crtc);
11523 BUG_ON(!set->crtc->helper_private);
2e431051 11524
7e53f3a4
DV
11525 /* Enforce sane interface api - has been abused by the fb helper. */
11526 BUG_ON(!set->mode && set->fb);
11527 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11528
2e431051
DV
11529 if (set->fb) {
11530 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11531 set->crtc->base.id, set->fb->base.id,
11532 (int)set->num_connectors, set->x, set->y);
11533 } else {
11534 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11535 }
11536
11537 dev = set->crtc->dev;
11538
11539 ret = -ENOMEM;
11540 config = kzalloc(sizeof(*config), GFP_KERNEL);
11541 if (!config)
11542 goto out_config;
11543
11544 ret = intel_set_config_save_state(dev, config);
11545 if (ret)
11546 goto out_config;
11547
11548 save_set.crtc = set->crtc;
11549 save_set.mode = &set->crtc->mode;
11550 save_set.x = set->crtc->x;
11551 save_set.y = set->crtc->y;
f4510a27 11552 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11553
11554 /* Compute whether we need a full modeset, only an fb base update or no
11555 * change at all. In the future we might also check whether only the
11556 * mode changed, e.g. for LVDS where we only change the panel fitter in
11557 * such cases. */
11558 intel_set_config_compute_mode_changes(set, config);
11559
9a935856 11560 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11561 if (ret)
11562 goto fail;
11563
5e2b584e 11564 if (config->mode_changed) {
c0c36b94
CW
11565 ret = intel_set_mode(set->crtc, set->mode,
11566 set->x, set->y, set->fb);
5e2b584e 11567 } else if (config->fb_changed) {
3b150f08
MR
11568 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11569
4878cae2
VS
11570 intel_crtc_wait_for_pending_flips(set->crtc);
11571
4f660f49 11572 ret = intel_pipe_set_base(set->crtc,
94352cf9 11573 set->x, set->y, set->fb);
3b150f08
MR
11574
11575 /*
11576 * We need to make sure the primary plane is re-enabled if it
11577 * has previously been turned off.
11578 */
11579 if (!intel_crtc->primary_enabled && ret == 0) {
11580 WARN_ON(!intel_crtc->active);
fdd508a6 11581 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11582 }
11583
7ca51a3a
JB
11584 /*
11585 * In the fastboot case this may be our only check of the
11586 * state after boot. It would be better to only do it on
11587 * the first update, but we don't have a nice way of doing that
11588 * (and really, set_config isn't used much for high freq page
11589 * flipping, so increasing its cost here shouldn't be a big
11590 * deal).
11591 */
d330a953 11592 if (i915.fastboot && ret == 0)
7ca51a3a 11593 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11594 }
11595
2d05eae1 11596 if (ret) {
bf67dfeb
DV
11597 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11598 set->crtc->base.id, ret);
50f56119 11599fail:
2d05eae1 11600 intel_set_config_restore_state(dev, config);
50f56119 11601
7d00a1f5
VS
11602 /*
11603 * HACK: if the pipe was on, but we didn't have a framebuffer,
11604 * force the pipe off to avoid oopsing in the modeset code
11605 * due to fb==NULL. This should only happen during boot since
11606 * we don't yet reconstruct the FB from the hardware state.
11607 */
11608 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11609 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11610
2d05eae1
CW
11611 /* Try to restore the config */
11612 if (config->mode_changed &&
11613 intel_set_mode(save_set.crtc, save_set.mode,
11614 save_set.x, save_set.y, save_set.fb))
11615 DRM_ERROR("failed to restore config after modeset failure\n");
11616 }
50f56119 11617
d9e55608
DV
11618out_config:
11619 intel_set_config_free(config);
50f56119
DV
11620 return ret;
11621}
f6e5b160
CW
11622
11623static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11624 .gamma_set = intel_crtc_gamma_set,
50f56119 11625 .set_config = intel_crtc_set_config,
f6e5b160
CW
11626 .destroy = intel_crtc_destroy,
11627 .page_flip = intel_crtc_page_flip,
11628};
11629
5358901f
DV
11630static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11631 struct intel_shared_dpll *pll,
11632 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11633{
5358901f 11634 uint32_t val;
ee7b9f93 11635
bd2bb1b9
PZ
11636 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11637 return false;
11638
5358901f 11639 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11640 hw_state->dpll = val;
11641 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11642 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11643
11644 return val & DPLL_VCO_ENABLE;
11645}
11646
15bdd4cf
DV
11647static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11648 struct intel_shared_dpll *pll)
11649{
11650 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11651 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11652}
11653
e7b903d2
DV
11654static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11655 struct intel_shared_dpll *pll)
11656{
e7b903d2 11657 /* PCH refclock must be enabled first */
89eff4be 11658 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11659
15bdd4cf
DV
11660 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11661
11662 /* Wait for the clocks to stabilize. */
11663 POSTING_READ(PCH_DPLL(pll->id));
11664 udelay(150);
11665
11666 /* The pixel multiplier can only be updated once the
11667 * DPLL is enabled and the clocks are stable.
11668 *
11669 * So write it again.
11670 */
11671 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11672 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11673 udelay(200);
11674}
11675
11676static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11677 struct intel_shared_dpll *pll)
11678{
11679 struct drm_device *dev = dev_priv->dev;
11680 struct intel_crtc *crtc;
e7b903d2
DV
11681
11682 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11683 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11684 if (intel_crtc_to_shared_dpll(crtc) == pll)
11685 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11686 }
11687
15bdd4cf
DV
11688 I915_WRITE(PCH_DPLL(pll->id), 0);
11689 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11690 udelay(200);
11691}
11692
46edb027
DV
11693static char *ibx_pch_dpll_names[] = {
11694 "PCH DPLL A",
11695 "PCH DPLL B",
11696};
11697
7c74ade1 11698static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11699{
e7b903d2 11700 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11701 int i;
11702
7c74ade1 11703 dev_priv->num_shared_dpll = 2;
ee7b9f93 11704
e72f9fbf 11705 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11706 dev_priv->shared_dplls[i].id = i;
11707 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11708 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11709 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11710 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11711 dev_priv->shared_dplls[i].get_hw_state =
11712 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11713 }
11714}
11715
7c74ade1
DV
11716static void intel_shared_dpll_init(struct drm_device *dev)
11717{
e7b903d2 11718 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11719
9cd86933
DV
11720 if (HAS_DDI(dev))
11721 intel_ddi_pll_init(dev);
11722 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11723 ibx_pch_dpll_init(dev);
11724 else
11725 dev_priv->num_shared_dpll = 0;
11726
11727 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11728}
11729
465c120c
MR
11730static int
11731intel_primary_plane_disable(struct drm_plane *plane)
11732{
11733 struct drm_device *dev = plane->dev;
465c120c
MR
11734 struct intel_crtc *intel_crtc;
11735
11736 if (!plane->fb)
11737 return 0;
11738
11739 BUG_ON(!plane->crtc);
11740
11741 intel_crtc = to_intel_crtc(plane->crtc);
11742
11743 /*
11744 * Even though we checked plane->fb above, it's still possible that
11745 * the primary plane has been implicitly disabled because the crtc
11746 * coordinates given weren't visible, or because we detected
11747 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11748 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11749 * In either case, we need to unpin the FB and let the fb pointer get
11750 * updated, but otherwise we don't need to touch the hardware.
11751 */
11752 if (!intel_crtc->primary_enabled)
11753 goto disable_unpin;
11754
11755 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11756 intel_disable_primary_hw_plane(plane, plane->crtc);
11757
465c120c 11758disable_unpin:
4c34574f 11759 mutex_lock(&dev->struct_mutex);
2ff8fde1 11760 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11761 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11762 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11763 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11764 plane->fb = NULL;
11765
11766 return 0;
11767}
11768
11769static int
3c692a41
GP
11770intel_check_primary_plane(struct drm_plane *plane,
11771 struct intel_plane_state *state)
11772{
11773 struct drm_crtc *crtc = state->crtc;
11774 struct drm_framebuffer *fb = state->fb;
11775 struct drm_rect *dest = &state->dst;
11776 struct drm_rect *src = &state->src;
11777 const struct drm_rect *clip = &state->clip;
11778
11779 return drm_plane_helper_check_update(plane, crtc, fb,
11780 src, dest, clip,
11781 DRM_PLANE_HELPER_NO_SCALING,
11782 DRM_PLANE_HELPER_NO_SCALING,
11783 false, true, &state->visible);
11784}
11785
11786static int
11787intel_commit_primary_plane(struct drm_plane *plane,
11788 struct intel_plane_state *state)
465c120c 11789{
3c692a41
GP
11790 struct drm_crtc *crtc = state->crtc;
11791 struct drm_framebuffer *fb = state->fb;
465c120c 11792 struct drm_device *dev = crtc->dev;
48404c1e 11793 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2ff8fde1
MR
11795 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11796 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
ce54d85a 11797 struct intel_plane *intel_plane = to_intel_plane(plane);
3c692a41 11798 struct drm_rect *src = &state->src;
465c120c
MR
11799 int ret;
11800
465c120c
MR
11801 intel_crtc_wait_for_pending_flips(crtc);
11802
11803 /*
11804 * If clipping results in a non-visible primary plane, we'll disable
11805 * the primary plane. Note that this is a bit different than what
11806 * happens if userspace explicitly disables the plane by passing fb=0
11807 * because plane->fb still gets set and pinned.
11808 */
3c692a41 11809 if (!state->visible) {
4c34574f
MR
11810 mutex_lock(&dev->struct_mutex);
11811
465c120c
MR
11812 /*
11813 * Try to pin the new fb first so that we can bail out if we
11814 * fail.
11815 */
11816 if (plane->fb != fb) {
a071fa00 11817 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
4c34574f
MR
11818 if (ret) {
11819 mutex_unlock(&dev->struct_mutex);
465c120c 11820 return ret;
4c34574f 11821 }
465c120c
MR
11822 }
11823
a071fa00
DV
11824 i915_gem_track_fb(old_obj, obj,
11825 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11826
465c120c 11827 if (intel_crtc->primary_enabled)
fdd508a6 11828 intel_disable_primary_hw_plane(plane, crtc);
465c120c
MR
11829
11830
11831 if (plane->fb != fb)
11832 if (plane->fb)
a071fa00 11833 intel_unpin_fb_obj(old_obj);
465c120c 11834
4c34574f
MR
11835 mutex_unlock(&dev->struct_mutex);
11836
ce54d85a 11837 } else {
48404c1e
SJ
11838 if (intel_crtc && intel_crtc->active &&
11839 intel_crtc->primary_enabled) {
11840 /*
11841 * FBC does not work on some platforms for rotated
11842 * planes, so disable it when rotation is not 0 and
11843 * update it when rotation is set back to 0.
11844 *
11845 * FIXME: This is redundant with the fbc update done in
11846 * the primary plane enable function except that that
11847 * one is done too late. We eventually need to unify
11848 * this.
11849 */
11850 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11851 dev_priv->fbc.plane == intel_crtc->plane &&
11852 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11853 intel_disable_fbc(dev);
11854 }
11855 }
3c692a41 11856 ret = intel_pipe_set_base(crtc, src->x1, src->y1, fb);
ce54d85a
SJ
11857 if (ret)
11858 return ret;
465c120c 11859
ce54d85a
SJ
11860 if (!intel_crtc->primary_enabled)
11861 intel_enable_primary_hw_plane(plane, crtc);
11862 }
465c120c 11863
3c692a41
GP
11864 intel_plane->crtc_x = state->orig_dst.x1;
11865 intel_plane->crtc_y = state->orig_dst.y1;
11866 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11867 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11868 intel_plane->src_x = state->orig_src.x1;
11869 intel_plane->src_y = state->orig_src.y1;
11870 intel_plane->src_w = drm_rect_width(&state->orig_src);
11871 intel_plane->src_h = drm_rect_height(&state->orig_src);
ce54d85a 11872 intel_plane->obj = obj;
465c120c
MR
11873
11874 return 0;
11875}
11876
3c692a41
GP
11877static int
11878intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11879 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11880 unsigned int crtc_w, unsigned int crtc_h,
11881 uint32_t src_x, uint32_t src_y,
11882 uint32_t src_w, uint32_t src_h)
11883{
11884 struct intel_plane_state state;
11885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11886 int ret;
11887
11888 state.crtc = crtc;
11889 state.fb = fb;
11890
11891 /* sample coordinates in 16.16 fixed point */
11892 state.src.x1 = src_x;
11893 state.src.x2 = src_x + src_w;
11894 state.src.y1 = src_y;
11895 state.src.y2 = src_y + src_h;
11896
11897 /* integer pixels */
11898 state.dst.x1 = crtc_x;
11899 state.dst.x2 = crtc_x + crtc_w;
11900 state.dst.y1 = crtc_y;
11901 state.dst.y2 = crtc_y + crtc_h;
11902
11903 state.clip.x1 = 0;
11904 state.clip.y1 = 0;
11905 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11906 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11907
11908 state.orig_src = state.src;
11909 state.orig_dst = state.dst;
11910
11911 ret = intel_check_primary_plane(plane, &state);
11912 if (ret)
11913 return ret;
11914
11915 intel_commit_primary_plane(plane, &state);
11916
11917 return 0;
11918}
11919
3d7d6510
MR
11920/* Common destruction function for both primary and cursor planes */
11921static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
11922{
11923 struct intel_plane *intel_plane = to_intel_plane(plane);
11924 drm_plane_cleanup(plane);
11925 kfree(intel_plane);
11926}
11927
11928static const struct drm_plane_funcs intel_primary_plane_funcs = {
11929 .update_plane = intel_primary_plane_setplane,
11930 .disable_plane = intel_primary_plane_disable,
3d7d6510 11931 .destroy = intel_plane_destroy,
48404c1e 11932 .set_property = intel_plane_set_property
465c120c
MR
11933};
11934
11935static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11936 int pipe)
11937{
11938 struct intel_plane *primary;
11939 const uint32_t *intel_primary_formats;
11940 int num_formats;
11941
11942 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11943 if (primary == NULL)
11944 return NULL;
11945
11946 primary->can_scale = false;
11947 primary->max_downscale = 1;
11948 primary->pipe = pipe;
11949 primary->plane = pipe;
48404c1e 11950 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
11951 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11952 primary->plane = !pipe;
11953
11954 if (INTEL_INFO(dev)->gen <= 3) {
11955 intel_primary_formats = intel_primary_formats_gen2;
11956 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11957 } else {
11958 intel_primary_formats = intel_primary_formats_gen4;
11959 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11960 }
11961
11962 drm_universal_plane_init(dev, &primary->base, 0,
11963 &intel_primary_plane_funcs,
11964 intel_primary_formats, num_formats,
11965 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
11966
11967 if (INTEL_INFO(dev)->gen >= 4) {
11968 if (!dev->mode_config.rotation_property)
11969 dev->mode_config.rotation_property =
11970 drm_mode_create_rotation_property(dev,
11971 BIT(DRM_ROTATE_0) |
11972 BIT(DRM_ROTATE_180));
11973 if (dev->mode_config.rotation_property)
11974 drm_object_attach_property(&primary->base.base,
11975 dev->mode_config.rotation_property,
11976 primary->rotation);
11977 }
11978
465c120c
MR
11979 return &primary->base;
11980}
11981
3d7d6510
MR
11982static int
11983intel_cursor_plane_disable(struct drm_plane *plane)
11984{
11985 if (!plane->fb)
11986 return 0;
11987
11988 BUG_ON(!plane->crtc);
11989
11990 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11991}
11992
11993static int
852e787c
GP
11994intel_check_cursor_plane(struct drm_plane *plane,
11995 struct intel_plane_state *state)
3d7d6510 11996{
852e787c
GP
11997 struct drm_crtc *crtc = state->crtc;
11998 struct drm_framebuffer *fb = state->fb;
11999 struct drm_rect *dest = &state->dst;
12000 struct drm_rect *src = &state->src;
12001 const struct drm_rect *clip = &state->clip;
3d7d6510 12002
852e787c
GP
12003 return drm_plane_helper_check_update(plane, crtc, fb,
12004 src, dest, clip,
3d7d6510
MR
12005 DRM_PLANE_HELPER_NO_SCALING,
12006 DRM_PLANE_HELPER_NO_SCALING,
852e787c
GP
12007 true, true, &state->visible);
12008}
3d7d6510 12009
852e787c
GP
12010static int
12011intel_commit_cursor_plane(struct drm_plane *plane,
12012 struct intel_plane_state *state)
12013{
12014 struct drm_crtc *crtc = state->crtc;
12015 struct drm_framebuffer *fb = state->fb;
12016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12017 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12018 struct drm_i915_gem_object *obj = intel_fb->obj;
12019 int crtc_w, crtc_h;
12020
12021 crtc->cursor_x = state->orig_dst.x1;
12022 crtc->cursor_y = state->orig_dst.y1;
3d7d6510 12023 if (fb != crtc->cursor->fb) {
852e787c
GP
12024 crtc_w = drm_rect_width(&state->orig_dst);
12025 crtc_h = drm_rect_height(&state->orig_dst);
3d7d6510
MR
12026 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
12027 } else {
852e787c 12028 intel_crtc_update_cursor(crtc, state->visible);
4ed91096
DV
12029
12030 intel_frontbuffer_flip(crtc->dev,
12031 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
12032
3d7d6510
MR
12033 return 0;
12034 }
12035}
852e787c
GP
12036
12037static int
12038intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
12039 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
12040 unsigned int crtc_w, unsigned int crtc_h,
12041 uint32_t src_x, uint32_t src_y,
12042 uint32_t src_w, uint32_t src_h)
12043{
12044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12045 struct intel_plane_state state;
12046 int ret;
12047
12048 state.crtc = crtc;
12049 state.fb = fb;
12050
12051 /* sample coordinates in 16.16 fixed point */
12052 state.src.x1 = src_x;
12053 state.src.x2 = src_x + src_w;
12054 state.src.y1 = src_y;
12055 state.src.y2 = src_y + src_h;
12056
12057 /* integer pixels */
12058 state.dst.x1 = crtc_x;
12059 state.dst.x2 = crtc_x + crtc_w;
12060 state.dst.y1 = crtc_y;
12061 state.dst.y2 = crtc_y + crtc_h;
12062
12063 state.clip.x1 = 0;
12064 state.clip.y1 = 0;
12065 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
12066 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
12067
12068 state.orig_src = state.src;
12069 state.orig_dst = state.dst;
12070
12071 ret = intel_check_cursor_plane(plane, &state);
12072 if (ret)
12073 return ret;
12074
12075 return intel_commit_cursor_plane(plane, &state);
12076}
12077
3d7d6510
MR
12078static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12079 .update_plane = intel_cursor_plane_update,
12080 .disable_plane = intel_cursor_plane_disable,
12081 .destroy = intel_plane_destroy,
12082};
12083
12084static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12085 int pipe)
12086{
12087 struct intel_plane *cursor;
12088
12089 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12090 if (cursor == NULL)
12091 return NULL;
12092
12093 cursor->can_scale = false;
12094 cursor->max_downscale = 1;
12095 cursor->pipe = pipe;
12096 cursor->plane = pipe;
12097
12098 drm_universal_plane_init(dev, &cursor->base, 0,
12099 &intel_cursor_plane_funcs,
12100 intel_cursor_formats,
12101 ARRAY_SIZE(intel_cursor_formats),
12102 DRM_PLANE_TYPE_CURSOR);
12103 return &cursor->base;
12104}
12105
b358d0a6 12106static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12107{
fbee40df 12108 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12109 struct intel_crtc *intel_crtc;
3d7d6510
MR
12110 struct drm_plane *primary = NULL;
12111 struct drm_plane *cursor = NULL;
465c120c 12112 int i, ret;
79e53945 12113
955382f3 12114 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12115 if (intel_crtc == NULL)
12116 return;
12117
465c120c 12118 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12119 if (!primary)
12120 goto fail;
12121
12122 cursor = intel_cursor_plane_create(dev, pipe);
12123 if (!cursor)
12124 goto fail;
12125
465c120c 12126 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12127 cursor, &intel_crtc_funcs);
12128 if (ret)
12129 goto fail;
79e53945
JB
12130
12131 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12132 for (i = 0; i < 256; i++) {
12133 intel_crtc->lut_r[i] = i;
12134 intel_crtc->lut_g[i] = i;
12135 intel_crtc->lut_b[i] = i;
12136 }
12137
1f1c2e24
VS
12138 /*
12139 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12140 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12141 */
80824003
JB
12142 intel_crtc->pipe = pipe;
12143 intel_crtc->plane = pipe;
3a77c4c4 12144 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12145 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12146 intel_crtc->plane = !pipe;
80824003
JB
12147 }
12148
4b0e333e
CW
12149 intel_crtc->cursor_base = ~0;
12150 intel_crtc->cursor_cntl = ~0;
dc41c154 12151 intel_crtc->cursor_size = ~0;
8d7849db 12152
22fd0fab
JB
12153 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12154 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12155 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12156 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12157
79e53945 12158 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12159
12160 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12161 return;
12162
12163fail:
12164 if (primary)
12165 drm_plane_cleanup(primary);
12166 if (cursor)
12167 drm_plane_cleanup(cursor);
12168 kfree(intel_crtc);
79e53945
JB
12169}
12170
752aa88a
JB
12171enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12172{
12173 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12174 struct drm_device *dev = connector->base.dev;
752aa88a 12175
51fd371b 12176 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12177
12178 if (!encoder)
12179 return INVALID_PIPE;
12180
12181 return to_intel_crtc(encoder->crtc)->pipe;
12182}
12183
08d7b3d1 12184int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12185 struct drm_file *file)
08d7b3d1 12186{
08d7b3d1 12187 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12188 struct drm_crtc *drmmode_crtc;
c05422d5 12189 struct intel_crtc *crtc;
08d7b3d1 12190
1cff8f6b
DV
12191 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12192 return -ENODEV;
08d7b3d1 12193
7707e653 12194 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12195
7707e653 12196 if (!drmmode_crtc) {
08d7b3d1 12197 DRM_ERROR("no such CRTC id\n");
3f2c2057 12198 return -ENOENT;
08d7b3d1
CW
12199 }
12200
7707e653 12201 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12202 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12203
c05422d5 12204 return 0;
08d7b3d1
CW
12205}
12206
66a9278e 12207static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12208{
66a9278e
DV
12209 struct drm_device *dev = encoder->base.dev;
12210 struct intel_encoder *source_encoder;
79e53945 12211 int index_mask = 0;
79e53945
JB
12212 int entry = 0;
12213
b2784e15 12214 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12215 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12216 index_mask |= (1 << entry);
12217
79e53945
JB
12218 entry++;
12219 }
4ef69c7a 12220
79e53945
JB
12221 return index_mask;
12222}
12223
4d302442
CW
12224static bool has_edp_a(struct drm_device *dev)
12225{
12226 struct drm_i915_private *dev_priv = dev->dev_private;
12227
12228 if (!IS_MOBILE(dev))
12229 return false;
12230
12231 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12232 return false;
12233
e3589908 12234 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12235 return false;
12236
12237 return true;
12238}
12239
ba0fbca4
DL
12240const char *intel_output_name(int output)
12241{
12242 static const char *names[] = {
12243 [INTEL_OUTPUT_UNUSED] = "Unused",
12244 [INTEL_OUTPUT_ANALOG] = "Analog",
12245 [INTEL_OUTPUT_DVO] = "DVO",
12246 [INTEL_OUTPUT_SDVO] = "SDVO",
12247 [INTEL_OUTPUT_LVDS] = "LVDS",
12248 [INTEL_OUTPUT_TVOUT] = "TV",
12249 [INTEL_OUTPUT_HDMI] = "HDMI",
12250 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12251 [INTEL_OUTPUT_EDP] = "eDP",
12252 [INTEL_OUTPUT_DSI] = "DSI",
12253 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12254 };
12255
12256 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12257 return "Invalid";
12258
12259 return names[output];
12260}
12261
84b4e042
JB
12262static bool intel_crt_present(struct drm_device *dev)
12263{
12264 struct drm_i915_private *dev_priv = dev->dev_private;
12265
12266 if (IS_ULT(dev))
12267 return false;
12268
12269 if (IS_CHERRYVIEW(dev))
12270 return false;
12271
12272 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12273 return false;
12274
12275 return true;
12276}
12277
79e53945
JB
12278static void intel_setup_outputs(struct drm_device *dev)
12279{
725e30ad 12280 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12281 struct intel_encoder *encoder;
cb0953d7 12282 bool dpd_is_edp = false;
79e53945 12283
c9093354 12284 intel_lvds_init(dev);
79e53945 12285
84b4e042 12286 if (intel_crt_present(dev))
79935fca 12287 intel_crt_init(dev);
cb0953d7 12288
affa9354 12289 if (HAS_DDI(dev)) {
0e72a5b5
ED
12290 int found;
12291
12292 /* Haswell uses DDI functions to detect digital outputs */
12293 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12294 /* DDI A only supports eDP */
12295 if (found)
12296 intel_ddi_init(dev, PORT_A);
12297
12298 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12299 * register */
12300 found = I915_READ(SFUSE_STRAP);
12301
12302 if (found & SFUSE_STRAP_DDIB_DETECTED)
12303 intel_ddi_init(dev, PORT_B);
12304 if (found & SFUSE_STRAP_DDIC_DETECTED)
12305 intel_ddi_init(dev, PORT_C);
12306 if (found & SFUSE_STRAP_DDID_DETECTED)
12307 intel_ddi_init(dev, PORT_D);
12308 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12309 int found;
5d8a7752 12310 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12311
12312 if (has_edp_a(dev))
12313 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12314
dc0fa718 12315 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12316 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12317 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12318 if (!found)
e2debe91 12319 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12320 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12321 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12322 }
12323
dc0fa718 12324 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12325 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12326
dc0fa718 12327 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12328 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12329
5eb08b69 12330 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12331 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12332
270b3042 12333 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12334 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12335 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
12336 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12337 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12338 PORT_B);
12339 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12340 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12341 }
12342
6f6005a5
JB
12343 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12344 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12345 PORT_C);
12346 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 12347 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 12348 }
19c03924 12349
9418c1f1
VS
12350 if (IS_CHERRYVIEW(dev)) {
12351 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12352 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12353 PORT_D);
12354 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12355 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12356 }
12357 }
12358
3cfca973 12359 intel_dsi_init(dev);
103a196f 12360 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12361 bool found = false;
7d57382e 12362
e2debe91 12363 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12364 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12365 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12366 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12367 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12368 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12369 }
27185ae1 12370
e7281eab 12371 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12372 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12373 }
13520b05
KH
12374
12375 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12376
e2debe91 12377 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12378 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12379 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12380 }
27185ae1 12381
e2debe91 12382 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12383
b01f2c3a
JB
12384 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12385 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12386 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12387 }
e7281eab 12388 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12389 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12390 }
27185ae1 12391
b01f2c3a 12392 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12393 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12394 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12395 } else if (IS_GEN2(dev))
79e53945
JB
12396 intel_dvo_init(dev);
12397
103a196f 12398 if (SUPPORTS_TV(dev))
79e53945
JB
12399 intel_tv_init(dev);
12400
7c8f8a70
RV
12401 intel_edp_psr_init(dev);
12402
b2784e15 12403 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12404 encoder->base.possible_crtcs = encoder->crtc_mask;
12405 encoder->base.possible_clones =
66a9278e 12406 intel_encoder_clones(encoder);
79e53945 12407 }
47356eb6 12408
dde86e2d 12409 intel_init_pch_refclk(dev);
270b3042
DV
12410
12411 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12412}
12413
12414static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12415{
60a5ca01 12416 struct drm_device *dev = fb->dev;
79e53945 12417 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12418
ef2d633e 12419 drm_framebuffer_cleanup(fb);
60a5ca01 12420 mutex_lock(&dev->struct_mutex);
ef2d633e 12421 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12422 drm_gem_object_unreference(&intel_fb->obj->base);
12423 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12424 kfree(intel_fb);
12425}
12426
12427static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12428 struct drm_file *file,
79e53945
JB
12429 unsigned int *handle)
12430{
12431 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12432 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12433
05394f39 12434 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12435}
12436
12437static const struct drm_framebuffer_funcs intel_fb_funcs = {
12438 .destroy = intel_user_framebuffer_destroy,
12439 .create_handle = intel_user_framebuffer_create_handle,
12440};
12441
b5ea642a
DV
12442static int intel_framebuffer_init(struct drm_device *dev,
12443 struct intel_framebuffer *intel_fb,
12444 struct drm_mode_fb_cmd2 *mode_cmd,
12445 struct drm_i915_gem_object *obj)
79e53945 12446{
a57ce0b2 12447 int aligned_height;
a35cdaa0 12448 int pitch_limit;
79e53945
JB
12449 int ret;
12450
dd4916c5
DV
12451 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12452
c16ed4be
CW
12453 if (obj->tiling_mode == I915_TILING_Y) {
12454 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12455 return -EINVAL;
c16ed4be 12456 }
57cd6508 12457
c16ed4be
CW
12458 if (mode_cmd->pitches[0] & 63) {
12459 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12460 mode_cmd->pitches[0]);
57cd6508 12461 return -EINVAL;
c16ed4be 12462 }
57cd6508 12463
a35cdaa0
CW
12464 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12465 pitch_limit = 32*1024;
12466 } else if (INTEL_INFO(dev)->gen >= 4) {
12467 if (obj->tiling_mode)
12468 pitch_limit = 16*1024;
12469 else
12470 pitch_limit = 32*1024;
12471 } else if (INTEL_INFO(dev)->gen >= 3) {
12472 if (obj->tiling_mode)
12473 pitch_limit = 8*1024;
12474 else
12475 pitch_limit = 16*1024;
12476 } else
12477 /* XXX DSPC is limited to 4k tiled */
12478 pitch_limit = 8*1024;
12479
12480 if (mode_cmd->pitches[0] > pitch_limit) {
12481 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12482 obj->tiling_mode ? "tiled" : "linear",
12483 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12484 return -EINVAL;
c16ed4be 12485 }
5d7bd705
VS
12486
12487 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12488 mode_cmd->pitches[0] != obj->stride) {
12489 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12490 mode_cmd->pitches[0], obj->stride);
5d7bd705 12491 return -EINVAL;
c16ed4be 12492 }
5d7bd705 12493
57779d06 12494 /* Reject formats not supported by any plane early. */
308e5bcb 12495 switch (mode_cmd->pixel_format) {
57779d06 12496 case DRM_FORMAT_C8:
04b3924d
VS
12497 case DRM_FORMAT_RGB565:
12498 case DRM_FORMAT_XRGB8888:
12499 case DRM_FORMAT_ARGB8888:
57779d06
VS
12500 break;
12501 case DRM_FORMAT_XRGB1555:
12502 case DRM_FORMAT_ARGB1555:
c16ed4be 12503 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12504 DRM_DEBUG("unsupported pixel format: %s\n",
12505 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12506 return -EINVAL;
c16ed4be 12507 }
57779d06
VS
12508 break;
12509 case DRM_FORMAT_XBGR8888:
12510 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12511 case DRM_FORMAT_XRGB2101010:
12512 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12513 case DRM_FORMAT_XBGR2101010:
12514 case DRM_FORMAT_ABGR2101010:
c16ed4be 12515 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12516 DRM_DEBUG("unsupported pixel format: %s\n",
12517 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12518 return -EINVAL;
c16ed4be 12519 }
b5626747 12520 break;
04b3924d
VS
12521 case DRM_FORMAT_YUYV:
12522 case DRM_FORMAT_UYVY:
12523 case DRM_FORMAT_YVYU:
12524 case DRM_FORMAT_VYUY:
c16ed4be 12525 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12526 DRM_DEBUG("unsupported pixel format: %s\n",
12527 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12528 return -EINVAL;
c16ed4be 12529 }
57cd6508
CW
12530 break;
12531 default:
4ee62c76
VS
12532 DRM_DEBUG("unsupported pixel format: %s\n",
12533 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12534 return -EINVAL;
12535 }
12536
90f9a336
VS
12537 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12538 if (mode_cmd->offsets[0] != 0)
12539 return -EINVAL;
12540
a57ce0b2
JB
12541 aligned_height = intel_align_height(dev, mode_cmd->height,
12542 obj->tiling_mode);
53155c0a
DV
12543 /* FIXME drm helper for size checks (especially planar formats)? */
12544 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12545 return -EINVAL;
12546
c7d73f6a
DV
12547 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12548 intel_fb->obj = obj;
80075d49 12549 intel_fb->obj->framebuffer_references++;
c7d73f6a 12550
79e53945
JB
12551 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12552 if (ret) {
12553 DRM_ERROR("framebuffer init failed %d\n", ret);
12554 return ret;
12555 }
12556
79e53945
JB
12557 return 0;
12558}
12559
79e53945
JB
12560static struct drm_framebuffer *
12561intel_user_framebuffer_create(struct drm_device *dev,
12562 struct drm_file *filp,
308e5bcb 12563 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12564{
05394f39 12565 struct drm_i915_gem_object *obj;
79e53945 12566
308e5bcb
JB
12567 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12568 mode_cmd->handles[0]));
c8725226 12569 if (&obj->base == NULL)
cce13ff7 12570 return ERR_PTR(-ENOENT);
79e53945 12571
d2dff872 12572 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12573}
12574
4520f53a 12575#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12576static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12577{
12578}
12579#endif
12580
79e53945 12581static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12582 .fb_create = intel_user_framebuffer_create,
0632fef6 12583 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12584};
12585
e70236a8
JB
12586/* Set up chip specific display functions */
12587static void intel_init_display(struct drm_device *dev)
12588{
12589 struct drm_i915_private *dev_priv = dev->dev_private;
12590
ee9300bb
DV
12591 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12592 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12593 else if (IS_CHERRYVIEW(dev))
12594 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12595 else if (IS_VALLEYVIEW(dev))
12596 dev_priv->display.find_dpll = vlv_find_best_dpll;
12597 else if (IS_PINEVIEW(dev))
12598 dev_priv->display.find_dpll = pnv_find_best_dpll;
12599 else
12600 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12601
affa9354 12602 if (HAS_DDI(dev)) {
0e8ffe1b 12603 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12604 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12605 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12606 dev_priv->display.crtc_enable = haswell_crtc_enable;
12607 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12608 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12609 dev_priv->display.update_primary_plane =
12610 ironlake_update_primary_plane;
09b4ddf9 12611 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12612 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12613 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12614 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12615 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12616 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12617 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12618 dev_priv->display.update_primary_plane =
12619 ironlake_update_primary_plane;
89b667f8
JB
12620 } else if (IS_VALLEYVIEW(dev)) {
12621 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12622 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12623 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12624 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12625 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12626 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12627 dev_priv->display.update_primary_plane =
12628 i9xx_update_primary_plane;
f564048e 12629 } else {
0e8ffe1b 12630 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12631 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12632 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12633 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12634 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12635 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12636 dev_priv->display.update_primary_plane =
12637 i9xx_update_primary_plane;
f564048e 12638 }
e70236a8 12639
e70236a8 12640 /* Returns the core display clock speed */
25eb05fc
JB
12641 if (IS_VALLEYVIEW(dev))
12642 dev_priv->display.get_display_clock_speed =
12643 valleyview_get_display_clock_speed;
12644 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12645 dev_priv->display.get_display_clock_speed =
12646 i945_get_display_clock_speed;
12647 else if (IS_I915G(dev))
12648 dev_priv->display.get_display_clock_speed =
12649 i915_get_display_clock_speed;
257a7ffc 12650 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12651 dev_priv->display.get_display_clock_speed =
12652 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12653 else if (IS_PINEVIEW(dev))
12654 dev_priv->display.get_display_clock_speed =
12655 pnv_get_display_clock_speed;
e70236a8
JB
12656 else if (IS_I915GM(dev))
12657 dev_priv->display.get_display_clock_speed =
12658 i915gm_get_display_clock_speed;
12659 else if (IS_I865G(dev))
12660 dev_priv->display.get_display_clock_speed =
12661 i865_get_display_clock_speed;
f0f8a9ce 12662 else if (IS_I85X(dev))
e70236a8
JB
12663 dev_priv->display.get_display_clock_speed =
12664 i855_get_display_clock_speed;
12665 else /* 852, 830 */
12666 dev_priv->display.get_display_clock_speed =
12667 i830_get_display_clock_speed;
12668
3bb11b53 12669 if (IS_G4X(dev)) {
e0dac65e 12670 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12671 } else if (IS_GEN5(dev)) {
12672 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12673 dev_priv->display.write_eld = ironlake_write_eld;
12674 } else if (IS_GEN6(dev)) {
12675 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12676 dev_priv->display.write_eld = ironlake_write_eld;
12677 dev_priv->display.modeset_global_resources =
12678 snb_modeset_global_resources;
12679 } else if (IS_IVYBRIDGE(dev)) {
12680 /* FIXME: detect B0+ stepping and use auto training */
12681 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12682 dev_priv->display.write_eld = ironlake_write_eld;
12683 dev_priv->display.modeset_global_resources =
12684 ivb_modeset_global_resources;
059b2fe9 12685 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53
SJ
12686 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12687 dev_priv->display.write_eld = haswell_write_eld;
12688 dev_priv->display.modeset_global_resources =
12689 haswell_modeset_global_resources;
30a970c6
JB
12690 } else if (IS_VALLEYVIEW(dev)) {
12691 dev_priv->display.modeset_global_resources =
12692 valleyview_modeset_global_resources;
9ca2fe73 12693 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 12694 }
8c9f3aaf
JB
12695
12696 /* Default just returns -ENODEV to indicate unsupported */
12697 dev_priv->display.queue_flip = intel_default_queue_flip;
12698
12699 switch (INTEL_INFO(dev)->gen) {
12700 case 2:
12701 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12702 break;
12703
12704 case 3:
12705 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12706 break;
12707
12708 case 4:
12709 case 5:
12710 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12711 break;
12712
12713 case 6:
12714 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12715 break;
7c9017e5 12716 case 7:
4e0bbc31 12717 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12718 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12719 break;
8c9f3aaf 12720 }
7bd688cd
JN
12721
12722 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12723
12724 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12725}
12726
b690e96c
JB
12727/*
12728 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12729 * resume, or other times. This quirk makes sure that's the case for
12730 * affected systems.
12731 */
0206e353 12732static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12733{
12734 struct drm_i915_private *dev_priv = dev->dev_private;
12735
12736 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12737 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12738}
12739
b6b5d049
VS
12740static void quirk_pipeb_force(struct drm_device *dev)
12741{
12742 struct drm_i915_private *dev_priv = dev->dev_private;
12743
12744 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12745 DRM_INFO("applying pipe b force quirk\n");
12746}
12747
435793df
KP
12748/*
12749 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12750 */
12751static void quirk_ssc_force_disable(struct drm_device *dev)
12752{
12753 struct drm_i915_private *dev_priv = dev->dev_private;
12754 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12755 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12756}
12757
4dca20ef 12758/*
5a15ab5b
CE
12759 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12760 * brightness value
4dca20ef
CE
12761 */
12762static void quirk_invert_brightness(struct drm_device *dev)
12763{
12764 struct drm_i915_private *dev_priv = dev->dev_private;
12765 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12766 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12767}
12768
9c72cc6f
SD
12769/* Some VBT's incorrectly indicate no backlight is present */
12770static void quirk_backlight_present(struct drm_device *dev)
12771{
12772 struct drm_i915_private *dev_priv = dev->dev_private;
12773 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12774 DRM_INFO("applying backlight present quirk\n");
12775}
12776
b690e96c
JB
12777struct intel_quirk {
12778 int device;
12779 int subsystem_vendor;
12780 int subsystem_device;
12781 void (*hook)(struct drm_device *dev);
12782};
12783
5f85f176
EE
12784/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12785struct intel_dmi_quirk {
12786 void (*hook)(struct drm_device *dev);
12787 const struct dmi_system_id (*dmi_id_list)[];
12788};
12789
12790static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12791{
12792 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12793 return 1;
12794}
12795
12796static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12797 {
12798 .dmi_id_list = &(const struct dmi_system_id[]) {
12799 {
12800 .callback = intel_dmi_reverse_brightness,
12801 .ident = "NCR Corporation",
12802 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12803 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12804 },
12805 },
12806 { } /* terminating entry */
12807 },
12808 .hook = quirk_invert_brightness,
12809 },
12810};
12811
c43b5634 12812static struct intel_quirk intel_quirks[] = {
b690e96c 12813 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12814 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12815
b690e96c
JB
12816 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12817 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12818
b690e96c
JB
12819 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12820 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12821
5f080c0f
VS
12822 /* 830 needs to leave pipe A & dpll A up */
12823 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12824
b6b5d049
VS
12825 /* 830 needs to leave pipe B & dpll B up */
12826 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12827
435793df
KP
12828 /* Lenovo U160 cannot use SSC on LVDS */
12829 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12830
12831 /* Sony Vaio Y cannot use SSC on LVDS */
12832 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12833
be505f64
AH
12834 /* Acer Aspire 5734Z must invert backlight brightness */
12835 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12836
12837 /* Acer/eMachines G725 */
12838 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12839
12840 /* Acer/eMachines e725 */
12841 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12842
12843 /* Acer/Packard Bell NCL20 */
12844 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12845
12846 /* Acer Aspire 4736Z */
12847 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12848
12849 /* Acer Aspire 5336 */
12850 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12851
12852 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12853 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 12854
dfb3d47b
SD
12855 /* Acer C720 Chromebook (Core i3 4005U) */
12856 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12857
d4967d8c
SD
12858 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12859 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12860
12861 /* HP Chromebook 14 (Celeron 2955U) */
12862 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12863};
12864
12865static void intel_init_quirks(struct drm_device *dev)
12866{
12867 struct pci_dev *d = dev->pdev;
12868 int i;
12869
12870 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12871 struct intel_quirk *q = &intel_quirks[i];
12872
12873 if (d->device == q->device &&
12874 (d->subsystem_vendor == q->subsystem_vendor ||
12875 q->subsystem_vendor == PCI_ANY_ID) &&
12876 (d->subsystem_device == q->subsystem_device ||
12877 q->subsystem_device == PCI_ANY_ID))
12878 q->hook(dev);
12879 }
5f85f176
EE
12880 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12881 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12882 intel_dmi_quirks[i].hook(dev);
12883 }
b690e96c
JB
12884}
12885
9cce37f4
JB
12886/* Disable the VGA plane that we never use */
12887static void i915_disable_vga(struct drm_device *dev)
12888{
12889 struct drm_i915_private *dev_priv = dev->dev_private;
12890 u8 sr1;
766aa1c4 12891 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12892
2b37c616 12893 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12894 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12895 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12896 sr1 = inb(VGA_SR_DATA);
12897 outb(sr1 | 1<<5, VGA_SR_DATA);
12898 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12899 udelay(300);
12900
69769f9a
VS
12901 /*
12902 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12903 * from S3 without preserving (some of?) the other bits.
12904 */
12905 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
9cce37f4
JB
12906 POSTING_READ(vga_reg);
12907}
12908
f817586c
DV
12909void intel_modeset_init_hw(struct drm_device *dev)
12910{
a8f78b58
ED
12911 intel_prepare_ddi(dev);
12912
f8bf63fd
VS
12913 if (IS_VALLEYVIEW(dev))
12914 vlv_update_cdclk(dev);
12915
f817586c
DV
12916 intel_init_clock_gating(dev);
12917
8090c6b9 12918 intel_enable_gt_powersave(dev);
f817586c
DV
12919}
12920
7d708ee4
ID
12921void intel_modeset_suspend_hw(struct drm_device *dev)
12922{
12923 intel_suspend_hw(dev);
12924}
12925
79e53945
JB
12926void intel_modeset_init(struct drm_device *dev)
12927{
652c393a 12928 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 12929 int sprite, ret;
8cc87b75 12930 enum pipe pipe;
46f297fb 12931 struct intel_crtc *crtc;
79e53945
JB
12932
12933 drm_mode_config_init(dev);
12934
12935 dev->mode_config.min_width = 0;
12936 dev->mode_config.min_height = 0;
12937
019d96cb
DA
12938 dev->mode_config.preferred_depth = 24;
12939 dev->mode_config.prefer_shadow = 1;
12940
e6ecefaa 12941 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 12942
b690e96c
JB
12943 intel_init_quirks(dev);
12944
1fa61106
ED
12945 intel_init_pm(dev);
12946
e3c74757
BW
12947 if (INTEL_INFO(dev)->num_pipes == 0)
12948 return;
12949
e70236a8
JB
12950 intel_init_display(dev);
12951
a6c45cf0
CW
12952 if (IS_GEN2(dev)) {
12953 dev->mode_config.max_width = 2048;
12954 dev->mode_config.max_height = 2048;
12955 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
12956 dev->mode_config.max_width = 4096;
12957 dev->mode_config.max_height = 4096;
79e53945 12958 } else {
a6c45cf0
CW
12959 dev->mode_config.max_width = 8192;
12960 dev->mode_config.max_height = 8192;
79e53945 12961 }
068be561 12962
dc41c154
VS
12963 if (IS_845G(dev) || IS_I865G(dev)) {
12964 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12965 dev->mode_config.cursor_height = 1023;
12966 } else if (IS_GEN2(dev)) {
068be561
DL
12967 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12968 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12969 } else {
12970 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12971 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12972 }
12973
5d4545ae 12974 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 12975
28c97730 12976 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
12977 INTEL_INFO(dev)->num_pipes,
12978 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 12979
055e393f 12980 for_each_pipe(dev_priv, pipe) {
8cc87b75 12981 intel_crtc_init(dev, pipe);
1fe47785
DL
12982 for_each_sprite(pipe, sprite) {
12983 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 12984 if (ret)
06da8da2 12985 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 12986 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 12987 }
79e53945
JB
12988 }
12989
f42bb70d
JB
12990 intel_init_dpio(dev);
12991
e72f9fbf 12992 intel_shared_dpll_init(dev);
ee7b9f93 12993
69769f9a
VS
12994 /* save the BIOS value before clobbering it */
12995 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
9cce37f4
JB
12996 /* Just disable it once at startup */
12997 i915_disable_vga(dev);
79e53945 12998 intel_setup_outputs(dev);
11be49eb
CW
12999
13000 /* Just in case the BIOS is doing something questionable. */
13001 intel_disable_fbc(dev);
fa9fa083 13002
6e9f798d 13003 drm_modeset_lock_all(dev);
fa9fa083 13004 intel_modeset_setup_hw_state(dev, false);
6e9f798d 13005 drm_modeset_unlock_all(dev);
46f297fb 13006
d3fcc808 13007 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
13008 if (!crtc->active)
13009 continue;
13010
46f297fb 13011 /*
46f297fb
JB
13012 * Note that reserving the BIOS fb up front prevents us
13013 * from stuffing other stolen allocations like the ring
13014 * on top. This prevents some ugliness at boot time, and
13015 * can even allow for smooth boot transitions if the BIOS
13016 * fb is large enough for the active pipe configuration.
13017 */
13018 if (dev_priv->display.get_plane_config) {
13019 dev_priv->display.get_plane_config(crtc,
13020 &crtc->plane_config);
13021 /*
13022 * If the fb is shared between multiple heads, we'll
13023 * just get the first one.
13024 */
484b41dd 13025 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 13026 }
46f297fb 13027 }
2c7111db
CW
13028}
13029
7fad798e
DV
13030static void intel_enable_pipe_a(struct drm_device *dev)
13031{
13032 struct intel_connector *connector;
13033 struct drm_connector *crt = NULL;
13034 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 13035 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
13036
13037 /* We can't just switch on the pipe A, we need to set things up with a
13038 * proper mode and output configuration. As a gross hack, enable pipe A
13039 * by enabling the load detect pipe once. */
13040 list_for_each_entry(connector,
13041 &dev->mode_config.connector_list,
13042 base.head) {
13043 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13044 crt = &connector->base;
13045 break;
13046 }
13047 }
13048
13049 if (!crt)
13050 return;
13051
208bf9fd
VS
13052 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13053 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
13054}
13055
fa555837
DV
13056static bool
13057intel_check_plane_mapping(struct intel_crtc *crtc)
13058{
7eb552ae
BW
13059 struct drm_device *dev = crtc->base.dev;
13060 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
13061 u32 reg, val;
13062
7eb552ae 13063 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13064 return true;
13065
13066 reg = DSPCNTR(!crtc->plane);
13067 val = I915_READ(reg);
13068
13069 if ((val & DISPLAY_PLANE_ENABLE) &&
13070 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13071 return false;
13072
13073 return true;
13074}
13075
24929352
DV
13076static void intel_sanitize_crtc(struct intel_crtc *crtc)
13077{
13078 struct drm_device *dev = crtc->base.dev;
13079 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13080 u32 reg;
24929352 13081
24929352 13082 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 13083 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
13084 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13085
d3eaf884 13086 /* restore vblank interrupts to correct state */
d297e103
VS
13087 if (crtc->active) {
13088 update_scanline_offset(crtc);
d3eaf884 13089 drm_vblank_on(dev, crtc->pipe);
d297e103 13090 } else
d3eaf884
VS
13091 drm_vblank_off(dev, crtc->pipe);
13092
24929352 13093 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13094 * disable the crtc (and hence change the state) if it is wrong. Note
13095 * that gen4+ has a fixed plane -> pipe mapping. */
13096 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13097 struct intel_connector *connector;
13098 bool plane;
13099
24929352
DV
13100 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13101 crtc->base.base.id);
13102
13103 /* Pipe has the wrong plane attached and the plane is active.
13104 * Temporarily change the plane mapping and disable everything
13105 * ... */
13106 plane = crtc->plane;
13107 crtc->plane = !plane;
9c8958bc 13108 crtc->primary_enabled = true;
24929352
DV
13109 dev_priv->display.crtc_disable(&crtc->base);
13110 crtc->plane = plane;
13111
13112 /* ... and break all links. */
13113 list_for_each_entry(connector, &dev->mode_config.connector_list,
13114 base.head) {
13115 if (connector->encoder->base.crtc != &crtc->base)
13116 continue;
13117
7f1950fb
EE
13118 connector->base.dpms = DRM_MODE_DPMS_OFF;
13119 connector->base.encoder = NULL;
24929352 13120 }
7f1950fb
EE
13121 /* multiple connectors may have the same encoder:
13122 * handle them and break crtc link separately */
13123 list_for_each_entry(connector, &dev->mode_config.connector_list,
13124 base.head)
13125 if (connector->encoder->base.crtc == &crtc->base) {
13126 connector->encoder->base.crtc = NULL;
13127 connector->encoder->connectors_active = false;
13128 }
24929352
DV
13129
13130 WARN_ON(crtc->active);
13131 crtc->base.enabled = false;
13132 }
24929352 13133
7fad798e
DV
13134 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13135 crtc->pipe == PIPE_A && !crtc->active) {
13136 /* BIOS forgot to enable pipe A, this mostly happens after
13137 * resume. Force-enable the pipe to fix this, the update_dpms
13138 * call below we restore the pipe to the right state, but leave
13139 * the required bits on. */
13140 intel_enable_pipe_a(dev);
13141 }
13142
24929352
DV
13143 /* Adjust the state of the output pipe according to whether we
13144 * have active connectors/encoders. */
13145 intel_crtc_update_dpms(&crtc->base);
13146
13147 if (crtc->active != crtc->base.enabled) {
13148 struct intel_encoder *encoder;
13149
13150 /* This can happen either due to bugs in the get_hw_state
13151 * functions or because the pipe is force-enabled due to the
13152 * pipe A quirk. */
13153 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13154 crtc->base.base.id,
13155 crtc->base.enabled ? "enabled" : "disabled",
13156 crtc->active ? "enabled" : "disabled");
13157
13158 crtc->base.enabled = crtc->active;
13159
13160 /* Because we only establish the connector -> encoder ->
13161 * crtc links if something is active, this means the
13162 * crtc is now deactivated. Break the links. connector
13163 * -> encoder links are only establish when things are
13164 * actually up, hence no need to break them. */
13165 WARN_ON(crtc->active);
13166
13167 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13168 WARN_ON(encoder->connectors_active);
13169 encoder->base.crtc = NULL;
13170 }
13171 }
c5ab3bc0 13172
a3ed6aad 13173 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13174 /*
13175 * We start out with underrun reporting disabled to avoid races.
13176 * For correct bookkeeping mark this on active crtcs.
13177 *
c5ab3bc0
DV
13178 * Also on gmch platforms we dont have any hardware bits to
13179 * disable the underrun reporting. Which means we need to start
13180 * out with underrun reporting disabled also on inactive pipes,
13181 * since otherwise we'll complain about the garbage we read when
13182 * e.g. coming up after runtime pm.
13183 *
4cc31489
DV
13184 * No protection against concurrent access is required - at
13185 * worst a fifo underrun happens which also sets this to false.
13186 */
13187 crtc->cpu_fifo_underrun_disabled = true;
13188 crtc->pch_fifo_underrun_disabled = true;
13189 }
24929352
DV
13190}
13191
13192static void intel_sanitize_encoder(struct intel_encoder *encoder)
13193{
13194 struct intel_connector *connector;
13195 struct drm_device *dev = encoder->base.dev;
13196
13197 /* We need to check both for a crtc link (meaning that the
13198 * encoder is active and trying to read from a pipe) and the
13199 * pipe itself being active. */
13200 bool has_active_crtc = encoder->base.crtc &&
13201 to_intel_crtc(encoder->base.crtc)->active;
13202
13203 if (encoder->connectors_active && !has_active_crtc) {
13204 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13205 encoder->base.base.id,
8e329a03 13206 encoder->base.name);
24929352
DV
13207
13208 /* Connector is active, but has no active pipe. This is
13209 * fallout from our resume register restoring. Disable
13210 * the encoder manually again. */
13211 if (encoder->base.crtc) {
13212 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13213 encoder->base.base.id,
8e329a03 13214 encoder->base.name);
24929352 13215 encoder->disable(encoder);
a62d1497
VS
13216 if (encoder->post_disable)
13217 encoder->post_disable(encoder);
24929352 13218 }
7f1950fb
EE
13219 encoder->base.crtc = NULL;
13220 encoder->connectors_active = false;
24929352
DV
13221
13222 /* Inconsistent output/port/pipe state happens presumably due to
13223 * a bug in one of the get_hw_state functions. Or someplace else
13224 * in our code, like the register restore mess on resume. Clamp
13225 * things to off as a safer default. */
13226 list_for_each_entry(connector,
13227 &dev->mode_config.connector_list,
13228 base.head) {
13229 if (connector->encoder != encoder)
13230 continue;
7f1950fb
EE
13231 connector->base.dpms = DRM_MODE_DPMS_OFF;
13232 connector->base.encoder = NULL;
24929352
DV
13233 }
13234 }
13235 /* Enabled encoders without active connectors will be fixed in
13236 * the crtc fixup. */
13237}
13238
04098753 13239void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13240{
13241 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13242 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13243
04098753
ID
13244 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13245 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13246 i915_disable_vga(dev);
13247 }
13248}
13249
13250void i915_redisable_vga(struct drm_device *dev)
13251{
13252 struct drm_i915_private *dev_priv = dev->dev_private;
13253
8dc8a27c
PZ
13254 /* This function can be called both from intel_modeset_setup_hw_state or
13255 * at a very early point in our resume sequence, where the power well
13256 * structures are not yet restored. Since this function is at a very
13257 * paranoid "someone might have enabled VGA while we were not looking"
13258 * level, just check if the power well is enabled instead of trying to
13259 * follow the "don't touch the power well if we don't need it" policy
13260 * the rest of the driver uses. */
04098753 13261 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13262 return;
13263
04098753 13264 i915_redisable_vga_power_on(dev);
0fde901f
KM
13265}
13266
98ec7739
VS
13267static bool primary_get_hw_state(struct intel_crtc *crtc)
13268{
13269 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13270
13271 if (!crtc->active)
13272 return false;
13273
13274 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13275}
13276
30e984df 13277static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13278{
13279 struct drm_i915_private *dev_priv = dev->dev_private;
13280 enum pipe pipe;
24929352
DV
13281 struct intel_crtc *crtc;
13282 struct intel_encoder *encoder;
13283 struct intel_connector *connector;
5358901f 13284 int i;
24929352 13285
d3fcc808 13286 for_each_intel_crtc(dev, crtc) {
88adfff1 13287 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13288
9953599b
DV
13289 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13290
0e8ffe1b
DV
13291 crtc->active = dev_priv->display.get_pipe_config(crtc,
13292 &crtc->config);
24929352
DV
13293
13294 crtc->base.enabled = crtc->active;
98ec7739 13295 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13296
13297 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13298 crtc->base.base.id,
13299 crtc->active ? "enabled" : "disabled");
13300 }
13301
5358901f
DV
13302 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13303 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13304
13305 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13306 pll->active = 0;
d3fcc808 13307 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13308 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13309 pll->active++;
13310 }
13311 pll->refcount = pll->active;
13312
35c95375
DV
13313 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13314 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13315
13316 if (pll->refcount)
13317 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13318 }
13319
b2784e15 13320 for_each_intel_encoder(dev, encoder) {
24929352
DV
13321 pipe = 0;
13322
13323 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13324 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13325 encoder->base.crtc = &crtc->base;
1d37b689 13326 encoder->get_config(encoder, &crtc->config);
24929352
DV
13327 } else {
13328 encoder->base.crtc = NULL;
13329 }
13330
13331 encoder->connectors_active = false;
6f2bcceb 13332 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13333 encoder->base.base.id,
8e329a03 13334 encoder->base.name,
24929352 13335 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13336 pipe_name(pipe));
24929352
DV
13337 }
13338
13339 list_for_each_entry(connector, &dev->mode_config.connector_list,
13340 base.head) {
13341 if (connector->get_hw_state(connector)) {
13342 connector->base.dpms = DRM_MODE_DPMS_ON;
13343 connector->encoder->connectors_active = true;
13344 connector->base.encoder = &connector->encoder->base;
13345 } else {
13346 connector->base.dpms = DRM_MODE_DPMS_OFF;
13347 connector->base.encoder = NULL;
13348 }
13349 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13350 connector->base.base.id,
c23cc417 13351 connector->base.name,
24929352
DV
13352 connector->base.encoder ? "enabled" : "disabled");
13353 }
30e984df
DV
13354}
13355
13356/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13357 * and i915 state tracking structures. */
13358void intel_modeset_setup_hw_state(struct drm_device *dev,
13359 bool force_restore)
13360{
13361 struct drm_i915_private *dev_priv = dev->dev_private;
13362 enum pipe pipe;
30e984df
DV
13363 struct intel_crtc *crtc;
13364 struct intel_encoder *encoder;
35c95375 13365 int i;
30e984df
DV
13366
13367 intel_modeset_readout_hw_state(dev);
24929352 13368
babea61d
JB
13369 /*
13370 * Now that we have the config, copy it to each CRTC struct
13371 * Note that this could go away if we move to using crtc_config
13372 * checking everywhere.
13373 */
d3fcc808 13374 for_each_intel_crtc(dev, crtc) {
d330a953 13375 if (crtc->active && i915.fastboot) {
f6a83288 13376 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13377 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13378 crtc->base.base.id);
13379 drm_mode_debug_printmodeline(&crtc->base.mode);
13380 }
13381 }
13382
24929352 13383 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13384 for_each_intel_encoder(dev, encoder) {
24929352
DV
13385 intel_sanitize_encoder(encoder);
13386 }
13387
055e393f 13388 for_each_pipe(dev_priv, pipe) {
24929352
DV
13389 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13390 intel_sanitize_crtc(crtc);
c0b03411 13391 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13392 }
9a935856 13393
35c95375
DV
13394 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13395 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13396
13397 if (!pll->on || pll->active)
13398 continue;
13399
13400 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13401
13402 pll->disable(dev_priv, pll);
13403 pll->on = false;
13404 }
13405
96f90c54 13406 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13407 ilk_wm_get_hw_state(dev);
13408
45e2b5f6 13409 if (force_restore) {
7d0bc1ea
VS
13410 i915_redisable_vga(dev);
13411
f30da187
DV
13412 /*
13413 * We need to use raw interfaces for restoring state to avoid
13414 * checking (bogus) intermediate states.
13415 */
055e393f 13416 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13417 struct drm_crtc *crtc =
13418 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13419
13420 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13421 crtc->primary->fb);
45e2b5f6
DV
13422 }
13423 } else {
13424 intel_modeset_update_staged_output_state(dev);
13425 }
8af6cf88
DV
13426
13427 intel_modeset_check_state(dev);
2c7111db
CW
13428}
13429
13430void intel_modeset_gem_init(struct drm_device *dev)
13431{
484b41dd 13432 struct drm_crtc *c;
2ff8fde1 13433 struct drm_i915_gem_object *obj;
484b41dd 13434
ae48434c
ID
13435 mutex_lock(&dev->struct_mutex);
13436 intel_init_gt_powersave(dev);
13437 mutex_unlock(&dev->struct_mutex);
13438
1833b134 13439 intel_modeset_init_hw(dev);
02e792fb
DV
13440
13441 intel_setup_overlay(dev);
484b41dd
JB
13442
13443 /*
13444 * Make sure any fbs we allocated at startup are properly
13445 * pinned & fenced. When we do the allocation it's too early
13446 * for this.
13447 */
13448 mutex_lock(&dev->struct_mutex);
70e1e0ec 13449 for_each_crtc(dev, c) {
2ff8fde1
MR
13450 obj = intel_fb_obj(c->primary->fb);
13451 if (obj == NULL)
484b41dd
JB
13452 continue;
13453
2ff8fde1 13454 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13455 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13456 to_intel_crtc(c)->pipe);
66e514c1
DA
13457 drm_framebuffer_unreference(c->primary->fb);
13458 c->primary->fb = NULL;
484b41dd
JB
13459 }
13460 }
13461 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13462}
13463
4932e2c3
ID
13464void intel_connector_unregister(struct intel_connector *intel_connector)
13465{
13466 struct drm_connector *connector = &intel_connector->base;
13467
13468 intel_panel_destroy_backlight(connector);
34ea3d38 13469 drm_connector_unregister(connector);
4932e2c3
ID
13470}
13471
79e53945
JB
13472void intel_modeset_cleanup(struct drm_device *dev)
13473{
652c393a 13474 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13475 struct drm_connector *connector;
652c393a 13476
fd0c0642
DV
13477 /*
13478 * Interrupts and polling as the first thing to avoid creating havoc.
13479 * Too much stuff here (turning of rps, connectors, ...) would
13480 * experience fancy races otherwise.
13481 */
13482 drm_irq_uninstall(dev);
1d0d343a 13483 intel_hpd_cancel_work(dev_priv);
eb21b92b
JB
13484 dev_priv->pm._irqs_disabled = true;
13485
fd0c0642
DV
13486 /*
13487 * Due to the hpd irq storm handling the hotplug work can re-arm the
13488 * poll handlers. Hence disable polling after hpd handling is shut down.
13489 */
f87ea761 13490 drm_kms_helper_poll_fini(dev);
fd0c0642 13491
652c393a
JB
13492 mutex_lock(&dev->struct_mutex);
13493
723bfd70
JB
13494 intel_unregister_dsm_handler();
13495
973d04f9 13496 intel_disable_fbc(dev);
e70236a8 13497
8090c6b9 13498 intel_disable_gt_powersave(dev);
0cdab21f 13499
930ebb46
DV
13500 ironlake_teardown_rc6(dev);
13501
69341a5e
KH
13502 mutex_unlock(&dev->struct_mutex);
13503
1630fe75
CW
13504 /* flush any delayed tasks or pending work */
13505 flush_scheduled_work();
13506
db31af1d
JN
13507 /* destroy the backlight and sysfs files before encoders/connectors */
13508 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13509 struct intel_connector *intel_connector;
13510
13511 intel_connector = to_intel_connector(connector);
13512 intel_connector->unregister(intel_connector);
db31af1d 13513 }
d9255d57 13514
79e53945 13515 drm_mode_config_cleanup(dev);
4d7bb011
DV
13516
13517 intel_cleanup_overlay(dev);
ae48434c
ID
13518
13519 mutex_lock(&dev->struct_mutex);
13520 intel_cleanup_gt_powersave(dev);
13521 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13522}
13523
f1c79df3
ZW
13524/*
13525 * Return which encoder is currently attached for connector.
13526 */
df0e9248 13527struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13528{
df0e9248
CW
13529 return &intel_attached_encoder(connector)->base;
13530}
f1c79df3 13531
df0e9248
CW
13532void intel_connector_attach_encoder(struct intel_connector *connector,
13533 struct intel_encoder *encoder)
13534{
13535 connector->encoder = encoder;
13536 drm_mode_connector_attach_encoder(&connector->base,
13537 &encoder->base);
79e53945 13538}
28d52043
DA
13539
13540/*
13541 * set vga decode state - true == enable VGA decode
13542 */
13543int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13544{
13545 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13546 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13547 u16 gmch_ctrl;
13548
75fa041d
CW
13549 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13550 DRM_ERROR("failed to read control word\n");
13551 return -EIO;
13552 }
13553
c0cc8a55
CW
13554 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13555 return 0;
13556
28d52043
DA
13557 if (state)
13558 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13559 else
13560 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13561
13562 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13563 DRM_ERROR("failed to write control word\n");
13564 return -EIO;
13565 }
13566
28d52043
DA
13567 return 0;
13568}
c4a1d9e4 13569
c4a1d9e4 13570struct intel_display_error_state {
ff57f1b0
PZ
13571
13572 u32 power_well_driver;
13573
63b66e5b
CW
13574 int num_transcoders;
13575
c4a1d9e4
CW
13576 struct intel_cursor_error_state {
13577 u32 control;
13578 u32 position;
13579 u32 base;
13580 u32 size;
52331309 13581 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13582
13583 struct intel_pipe_error_state {
ddf9c536 13584 bool power_domain_on;
c4a1d9e4 13585 u32 source;
f301b1e1 13586 u32 stat;
52331309 13587 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13588
13589 struct intel_plane_error_state {
13590 u32 control;
13591 u32 stride;
13592 u32 size;
13593 u32 pos;
13594 u32 addr;
13595 u32 surface;
13596 u32 tile_offset;
52331309 13597 } plane[I915_MAX_PIPES];
63b66e5b
CW
13598
13599 struct intel_transcoder_error_state {
ddf9c536 13600 bool power_domain_on;
63b66e5b
CW
13601 enum transcoder cpu_transcoder;
13602
13603 u32 conf;
13604
13605 u32 htotal;
13606 u32 hblank;
13607 u32 hsync;
13608 u32 vtotal;
13609 u32 vblank;
13610 u32 vsync;
13611 } transcoder[4];
c4a1d9e4
CW
13612};
13613
13614struct intel_display_error_state *
13615intel_display_capture_error_state(struct drm_device *dev)
13616{
fbee40df 13617 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13618 struct intel_display_error_state *error;
63b66e5b
CW
13619 int transcoders[] = {
13620 TRANSCODER_A,
13621 TRANSCODER_B,
13622 TRANSCODER_C,
13623 TRANSCODER_EDP,
13624 };
c4a1d9e4
CW
13625 int i;
13626
63b66e5b
CW
13627 if (INTEL_INFO(dev)->num_pipes == 0)
13628 return NULL;
13629
9d1cb914 13630 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13631 if (error == NULL)
13632 return NULL;
13633
190be112 13634 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13635 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13636
055e393f 13637 for_each_pipe(dev_priv, i) {
ddf9c536 13638 error->pipe[i].power_domain_on =
bfafe93a
ID
13639 intel_display_power_enabled_unlocked(dev_priv,
13640 POWER_DOMAIN_PIPE(i));
ddf9c536 13641 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13642 continue;
13643
5efb3e28
VS
13644 error->cursor[i].control = I915_READ(CURCNTR(i));
13645 error->cursor[i].position = I915_READ(CURPOS(i));
13646 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13647
13648 error->plane[i].control = I915_READ(DSPCNTR(i));
13649 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13650 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13651 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13652 error->plane[i].pos = I915_READ(DSPPOS(i));
13653 }
ca291363
PZ
13654 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13655 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13656 if (INTEL_INFO(dev)->gen >= 4) {
13657 error->plane[i].surface = I915_READ(DSPSURF(i));
13658 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13659 }
13660
c4a1d9e4 13661 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13662
3abfce77 13663 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13664 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13665 }
13666
13667 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13668 if (HAS_DDI(dev_priv->dev))
13669 error->num_transcoders++; /* Account for eDP. */
13670
13671 for (i = 0; i < error->num_transcoders; i++) {
13672 enum transcoder cpu_transcoder = transcoders[i];
13673
ddf9c536 13674 error->transcoder[i].power_domain_on =
bfafe93a 13675 intel_display_power_enabled_unlocked(dev_priv,
38cc1daf 13676 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13677 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13678 continue;
13679
63b66e5b
CW
13680 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13681
13682 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13683 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13684 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13685 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13686 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13687 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13688 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13689 }
13690
13691 return error;
13692}
13693
edc3d884
MK
13694#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13695
c4a1d9e4 13696void
edc3d884 13697intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13698 struct drm_device *dev,
13699 struct intel_display_error_state *error)
13700{
055e393f 13701 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13702 int i;
13703
63b66e5b
CW
13704 if (!error)
13705 return;
13706
edc3d884 13707 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13708 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13709 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13710 error->power_well_driver);
055e393f 13711 for_each_pipe(dev_priv, i) {
edc3d884 13712 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13713 err_printf(m, " Power: %s\n",
13714 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13715 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13716 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13717
13718 err_printf(m, "Plane [%d]:\n", i);
13719 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13720 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13721 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13722 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13723 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13724 }
4b71a570 13725 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13726 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13727 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13728 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13729 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13730 }
13731
edc3d884
MK
13732 err_printf(m, "Cursor [%d]:\n", i);
13733 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13734 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13735 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13736 }
63b66e5b
CW
13737
13738 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13739 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13740 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13741 err_printf(m, " Power: %s\n",
13742 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13743 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13744 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13745 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13746 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13747 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13748 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13749 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13750 }
c4a1d9e4 13751}
e2fcdaa9
VS
13752
13753void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13754{
13755 struct intel_crtc *crtc;
13756
13757 for_each_intel_crtc(dev, crtc) {
13758 struct intel_unpin_work *work;
13759 unsigned long irqflags;
13760
13761 spin_lock_irqsave(&dev->event_lock, irqflags);
13762
13763 work = crtc->unpin_work;
13764
13765 if (work && work->event &&
13766 work->event->base.file_priv == file) {
13767 kfree(work->event);
13768 work->event = NULL;
13769 }
13770
13771 spin_unlock_irqrestore(&dev->event_lock, irqflags);
13772 }
13773}