drm/i915/skl: Implement drm_plane vfuncs
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
760285e7
DH
40#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
465c120c
MR
42#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
c0f372b3 44#include <linux/dma_remapping.h>
79e53945 45
465c120c
MR
46/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
3d7d6510
MR
71/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
ef9348c8 76#define DIV_ROUND_CLOSEST_ULL(ll, d) \
465c120c 77({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
ef9348c8 78
cc36513c
DV
79static void intel_increase_pllclock(struct drm_device *dev,
80 enum pipe pipe);
6b383a7f 81static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 82
f1f644dc
JB
83static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
84 struct intel_crtc_config *pipe_config);
18442d08
VS
85static void ironlake_pch_clock_get(struct intel_crtc *crtc,
86 struct intel_crtc_config *pipe_config);
f1f644dc 87
e7457a9a
DL
88static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
89 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
bdd4b6a6 102static void vlv_prepare_pll(struct intel_crtc *crtc);
1ae0d137 103static void chv_prepare_pll(struct intel_crtc *crtc);
e7457a9a 104
0e32b39c
DA
105static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
106{
107 if (!connector->mst_port)
108 return connector->encoder;
109 else
110 return &connector->mst_port->mst_encoders[pipe]->base;
111}
112
79e53945 113typedef struct {
0206e353 114 int min, max;
79e53945
JB
115} intel_range_t;
116
117typedef struct {
0206e353
AJ
118 int dot_limit;
119 int p2_slow, p2_fast;
79e53945
JB
120} intel_p2_t;
121
d4906093
ML
122typedef struct intel_limit intel_limit_t;
123struct intel_limit {
0206e353
AJ
124 intel_range_t dot, vco, n, m, m1, m2, p, p1;
125 intel_p2_t p2;
d4906093 126};
79e53945 127
d2acd215
DV
128int
129intel_pch_rawclk(struct drm_device *dev)
130{
131 struct drm_i915_private *dev_priv = dev->dev_private;
132
133 WARN_ON(!HAS_PCH_SPLIT(dev));
134
135 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
136}
137
021357ac
CW
138static inline u32 /* units of 100MHz */
139intel_fdi_link_freq(struct drm_device *dev)
140{
8b99e68c
CW
141 if (IS_GEN5(dev)) {
142 struct drm_i915_private *dev_priv = dev->dev_private;
143 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
144 } else
145 return 27;
021357ac
CW
146}
147
5d536e28 148static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 149 .dot = { .min = 25000, .max = 350000 },
9c333719 150 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 151 .n = { .min = 2, .max = 16 },
0206e353
AJ
152 .m = { .min = 96, .max = 140 },
153 .m1 = { .min = 18, .max = 26 },
154 .m2 = { .min = 6, .max = 16 },
155 .p = { .min = 4, .max = 128 },
156 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
157 .p2 = { .dot_limit = 165000,
158 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
159};
160
5d536e28
DV
161static const intel_limit_t intel_limits_i8xx_dvo = {
162 .dot = { .min = 25000, .max = 350000 },
9c333719 163 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 164 .n = { .min = 2, .max = 16 },
5d536e28
DV
165 .m = { .min = 96, .max = 140 },
166 .m1 = { .min = 18, .max = 26 },
167 .m2 = { .min = 6, .max = 16 },
168 .p = { .min = 4, .max = 128 },
169 .p1 = { .min = 2, .max = 33 },
170 .p2 = { .dot_limit = 165000,
171 .p2_slow = 4, .p2_fast = 4 },
172};
173
e4b36699 174static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 175 .dot = { .min = 25000, .max = 350000 },
9c333719 176 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 177 .n = { .min = 2, .max = 16 },
0206e353
AJ
178 .m = { .min = 96, .max = 140 },
179 .m1 = { .min = 18, .max = 26 },
180 .m2 = { .min = 6, .max = 16 },
181 .p = { .min = 4, .max = 128 },
182 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
183 .p2 = { .dot_limit = 165000,
184 .p2_slow = 14, .p2_fast = 7 },
e4b36699 185};
273e27ca 186
e4b36699 187static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
188 .dot = { .min = 20000, .max = 400000 },
189 .vco = { .min = 1400000, .max = 2800000 },
190 .n = { .min = 1, .max = 6 },
191 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
192 .m1 = { .min = 8, .max = 18 },
193 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
194 .p = { .min = 5, .max = 80 },
195 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
196 .p2 = { .dot_limit = 200000,
197 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
198};
199
200static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
201 .dot = { .min = 20000, .max = 400000 },
202 .vco = { .min = 1400000, .max = 2800000 },
203 .n = { .min = 1, .max = 6 },
204 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
205 .m1 = { .min = 8, .max = 18 },
206 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
207 .p = { .min = 7, .max = 98 },
208 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
209 .p2 = { .dot_limit = 112000,
210 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
211};
212
273e27ca 213
e4b36699 214static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
215 .dot = { .min = 25000, .max = 270000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 10, .max = 30 },
222 .p1 = { .min = 1, .max = 3},
223 .p2 = { .dot_limit = 270000,
224 .p2_slow = 10,
225 .p2_fast = 10
044c7c41 226 },
e4b36699
KP
227};
228
229static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
230 .dot = { .min = 22000, .max = 400000 },
231 .vco = { .min = 1750000, .max = 3500000},
232 .n = { .min = 1, .max = 4 },
233 .m = { .min = 104, .max = 138 },
234 .m1 = { .min = 16, .max = 23 },
235 .m2 = { .min = 5, .max = 11 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8},
238 .p2 = { .dot_limit = 165000,
239 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
240};
241
242static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
243 .dot = { .min = 20000, .max = 115000 },
244 .vco = { .min = 1750000, .max = 3500000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 104, .max = 138 },
247 .m1 = { .min = 17, .max = 23 },
248 .m2 = { .min = 5, .max = 11 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 0,
252 .p2_slow = 14, .p2_fast = 14
044c7c41 253 },
e4b36699
KP
254};
255
256static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
257 .dot = { .min = 80000, .max = 224000 },
258 .vco = { .min = 1750000, .max = 3500000 },
259 .n = { .min = 1, .max = 3 },
260 .m = { .min = 104, .max = 138 },
261 .m1 = { .min = 17, .max = 23 },
262 .m2 = { .min = 5, .max = 11 },
263 .p = { .min = 14, .max = 42 },
264 .p1 = { .min = 2, .max = 6 },
265 .p2 = { .dot_limit = 0,
266 .p2_slow = 7, .p2_fast = 7
044c7c41 267 },
e4b36699
KP
268};
269
f2b115e6 270static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
271 .dot = { .min = 20000, .max = 400000},
272 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 273 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
273e27ca 276 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
277 .m1 = { .min = 0, .max = 0 },
278 .m2 = { .min = 0, .max = 254 },
279 .p = { .min = 5, .max = 80 },
280 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
281 .p2 = { .dot_limit = 200000,
282 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
283};
284
f2b115e6 285static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
286 .dot = { .min = 20000, .max = 400000 },
287 .vco = { .min = 1700000, .max = 3500000 },
288 .n = { .min = 3, .max = 6 },
289 .m = { .min = 2, .max = 256 },
290 .m1 = { .min = 0, .max = 0 },
291 .m2 = { .min = 0, .max = 254 },
292 .p = { .min = 7, .max = 112 },
293 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
294 .p2 = { .dot_limit = 112000,
295 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
296};
297
273e27ca
EA
298/* Ironlake / Sandybridge
299 *
300 * We calculate clock using (register_value + 2) for N/M1/M2, so here
301 * the range value for them is (actual_value - 2).
302 */
b91ad0ec 303static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
304 .dot = { .min = 25000, .max = 350000 },
305 .vco = { .min = 1760000, .max = 3510000 },
306 .n = { .min = 1, .max = 5 },
307 .m = { .min = 79, .max = 127 },
308 .m1 = { .min = 12, .max = 22 },
309 .m2 = { .min = 5, .max = 9 },
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
312 .p2 = { .dot_limit = 225000,
313 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
314};
315
b91ad0ec 316static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
317 .dot = { .min = 25000, .max = 350000 },
318 .vco = { .min = 1760000, .max = 3510000 },
319 .n = { .min = 1, .max = 3 },
320 .m = { .min = 79, .max = 118 },
321 .m1 = { .min = 12, .max = 22 },
322 .m2 = { .min = 5, .max = 9 },
323 .p = { .min = 28, .max = 112 },
324 .p1 = { .min = 2, .max = 8 },
325 .p2 = { .dot_limit = 225000,
326 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
327};
328
329static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
330 .dot = { .min = 25000, .max = 350000 },
331 .vco = { .min = 1760000, .max = 3510000 },
332 .n = { .min = 1, .max = 3 },
333 .m = { .min = 79, .max = 127 },
334 .m1 = { .min = 12, .max = 22 },
335 .m2 = { .min = 5, .max = 9 },
336 .p = { .min = 14, .max = 56 },
337 .p1 = { .min = 2, .max = 8 },
338 .p2 = { .dot_limit = 225000,
339 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
340};
341
273e27ca 342/* LVDS 100mhz refclk limits. */
b91ad0ec 343static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
344 .dot = { .min = 25000, .max = 350000 },
345 .vco = { .min = 1760000, .max = 3510000 },
346 .n = { .min = 1, .max = 2 },
347 .m = { .min = 79, .max = 126 },
348 .m1 = { .min = 12, .max = 22 },
349 .m2 = { .min = 5, .max = 9 },
350 .p = { .min = 28, .max = 112 },
0206e353 351 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
352 .p2 = { .dot_limit = 225000,
353 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
354};
355
356static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
357 .dot = { .min = 25000, .max = 350000 },
358 .vco = { .min = 1760000, .max = 3510000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 79, .max = 126 },
361 .m1 = { .min = 12, .max = 22 },
362 .m2 = { .min = 5, .max = 9 },
363 .p = { .min = 14, .max = 42 },
0206e353 364 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
365 .p2 = { .dot_limit = 225000,
366 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
367};
368
dc730512 369static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
370 /*
371 * These are the data rate limits (measured in fast clocks)
372 * since those are the strictest limits we have. The fast
373 * clock and actual rate limits are more relaxed, so checking
374 * them would make no difference.
375 */
376 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 377 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 378 .n = { .min = 1, .max = 7 },
a0c4da24
JB
379 .m1 = { .min = 2, .max = 3 },
380 .m2 = { .min = 11, .max = 156 },
b99ab663 381 .p1 = { .min = 2, .max = 3 },
5fdc9c49 382 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
383};
384
ef9348c8
CML
385static const intel_limit_t intel_limits_chv = {
386 /*
387 * These are the data rate limits (measured in fast clocks)
388 * since those are the strictest limits we have. The fast
389 * clock and actual rate limits are more relaxed, so checking
390 * them would make no difference.
391 */
392 .dot = { .min = 25000 * 5, .max = 540000 * 5},
393 .vco = { .min = 4860000, .max = 6700000 },
394 .n = { .min = 1, .max = 1 },
395 .m1 = { .min = 2, .max = 2 },
396 .m2 = { .min = 24 << 22, .max = 175 << 22 },
397 .p1 = { .min = 2, .max = 4 },
398 .p2 = { .p2_slow = 1, .p2_fast = 14 },
399};
400
6b4bf1c4
VS
401static void vlv_clock(int refclk, intel_clock_t *clock)
402{
403 clock->m = clock->m1 * clock->m2;
404 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
405 if (WARN_ON(clock->n == 0 || clock->p == 0))
406 return;
fb03ac01
VS
407 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
408 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
409}
410
e0638cdf
PZ
411/**
412 * Returns whether any output on the specified pipe is of the specified type
413 */
414static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
415{
416 struct drm_device *dev = crtc->dev;
417 struct intel_encoder *encoder;
418
419 for_each_encoder_on_crtc(dev, crtc, encoder)
420 if (encoder->type == type)
421 return true;
422
423 return false;
424}
425
1b894b59
CW
426static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
427 int refclk)
2c07245f 428{
b91ad0ec 429 struct drm_device *dev = crtc->dev;
2c07245f 430 const intel_limit_t *limit;
b91ad0ec
ZW
431
432 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 433 if (intel_is_dual_link_lvds(dev)) {
1b894b59 434 if (refclk == 100000)
b91ad0ec
ZW
435 limit = &intel_limits_ironlake_dual_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_dual_lvds;
438 } else {
1b894b59 439 if (refclk == 100000)
b91ad0ec
ZW
440 limit = &intel_limits_ironlake_single_lvds_100m;
441 else
442 limit = &intel_limits_ironlake_single_lvds;
443 }
c6bb3538 444 } else
b91ad0ec 445 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
446
447 return limit;
448}
449
044c7c41
ML
450static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
451{
452 struct drm_device *dev = crtc->dev;
044c7c41
ML
453 const intel_limit_t *limit;
454
455 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 456 if (intel_is_dual_link_lvds(dev))
e4b36699 457 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 458 else
e4b36699 459 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
461 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 462 limit = &intel_limits_g4x_hdmi;
044c7c41 463 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 464 limit = &intel_limits_g4x_sdvo;
044c7c41 465 } else /* The option is for other outputs */
e4b36699 466 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
467
468 return limit;
469}
470
1b894b59 471static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
472{
473 struct drm_device *dev = crtc->dev;
474 const intel_limit_t *limit;
475
bad720ff 476 if (HAS_PCH_SPLIT(dev))
1b894b59 477 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 478 else if (IS_G4X(dev)) {
044c7c41 479 limit = intel_g4x_limit(crtc);
f2b115e6 480 } else if (IS_PINEVIEW(dev)) {
2177832f 481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 482 limit = &intel_limits_pineview_lvds;
2177832f 483 else
f2b115e6 484 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
485 } else if (IS_CHERRYVIEW(dev)) {
486 limit = &intel_limits_chv;
a0c4da24 487 } else if (IS_VALLEYVIEW(dev)) {
dc730512 488 limit = &intel_limits_vlv;
a6c45cf0
CW
489 } else if (!IS_GEN2(dev)) {
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
491 limit = &intel_limits_i9xx_lvds;
492 else
493 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
494 } else {
495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 496 limit = &intel_limits_i8xx_lvds;
5d536e28 497 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 498 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
499 else
500 limit = &intel_limits_i8xx_dac;
79e53945
JB
501 }
502 return limit;
503}
504
f2b115e6
AJ
505/* m1 is reserved as 0 in Pineview, n is a ring counter */
506static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 507{
2177832f
SL
508 clock->m = clock->m2 + 2;
509 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
510 if (WARN_ON(clock->n == 0 || clock->p == 0))
511 return;
fb03ac01
VS
512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
514}
515
7429e9d4
DV
516static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
517{
518 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
519}
520
ac58c3f0 521static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 522{
7429e9d4 523 clock->m = i9xx_dpll_compute_m(clock);
79e53945 524 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
525 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
526 return;
fb03ac01
VS
527 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
528 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
529}
530
ef9348c8
CML
531static void chv_clock(int refclk, intel_clock_t *clock)
532{
533 clock->m = clock->m1 * clock->m2;
534 clock->p = clock->p1 * clock->p2;
535 if (WARN_ON(clock->n == 0 || clock->p == 0))
536 return;
537 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
538 clock->n << 22);
539 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
540}
541
7c04d1d9 542#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
543/**
544 * Returns whether the given set of divisors are valid for a given refclk with
545 * the given connectors.
546 */
547
1b894b59
CW
548static bool intel_PLL_is_valid(struct drm_device *dev,
549 const intel_limit_t *limit,
550 const intel_clock_t *clock)
79e53945 551{
f01b7962
VS
552 if (clock->n < limit->n.min || limit->n.max < clock->n)
553 INTELPllInvalid("n out of range\n");
79e53945 554 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 555 INTELPllInvalid("p1 out of range\n");
79e53945 556 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 557 INTELPllInvalid("m2 out of range\n");
79e53945 558 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 559 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
560
561 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
562 if (clock->m1 <= clock->m2)
563 INTELPllInvalid("m1 <= m2\n");
564
565 if (!IS_VALLEYVIEW(dev)) {
566 if (clock->p < limit->p.min || limit->p.max < clock->p)
567 INTELPllInvalid("p out of range\n");
568 if (clock->m < limit->m.min || limit->m.max < clock->m)
569 INTELPllInvalid("m out of range\n");
570 }
571
79e53945 572 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 573 INTELPllInvalid("vco out of range\n");
79e53945
JB
574 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
575 * connector, etc., rather than just a single range.
576 */
577 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 578 INTELPllInvalid("dot out of range\n");
79e53945
JB
579
580 return true;
581}
582
d4906093 583static bool
ee9300bb 584i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
585 int target, int refclk, intel_clock_t *match_clock,
586 intel_clock_t *best_clock)
79e53945
JB
587{
588 struct drm_device *dev = crtc->dev;
79e53945 589 intel_clock_t clock;
79e53945
JB
590 int err = target;
591
a210b028 592 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 593 /*
a210b028
DV
594 * For LVDS just rely on its current settings for dual-channel.
595 * We haven't figured out how to reliably set up different
596 * single/dual channel state, if we even can.
79e53945 597 */
1974cad0 598 if (intel_is_dual_link_lvds(dev))
79e53945
JB
599 clock.p2 = limit->p2.p2_fast;
600 else
601 clock.p2 = limit->p2.p2_slow;
602 } else {
603 if (target < limit->p2.dot_limit)
604 clock.p2 = limit->p2.p2_slow;
605 else
606 clock.p2 = limit->p2.p2_fast;
607 }
608
0206e353 609 memset(best_clock, 0, sizeof(*best_clock));
79e53945 610
42158660
ZY
611 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
612 clock.m1++) {
613 for (clock.m2 = limit->m2.min;
614 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 615 if (clock.m2 >= clock.m1)
42158660
ZY
616 break;
617 for (clock.n = limit->n.min;
618 clock.n <= limit->n.max; clock.n++) {
619 for (clock.p1 = limit->p1.min;
620 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
621 int this_err;
622
ac58c3f0
DV
623 i9xx_clock(refclk, &clock);
624 if (!intel_PLL_is_valid(dev, limit,
625 &clock))
626 continue;
627 if (match_clock &&
628 clock.p != match_clock->p)
629 continue;
630
631 this_err = abs(clock.dot - target);
632 if (this_err < err) {
633 *best_clock = clock;
634 err = this_err;
635 }
636 }
637 }
638 }
639 }
640
641 return (err != target);
642}
643
644static bool
ee9300bb
DV
645pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
646 int target, int refclk, intel_clock_t *match_clock,
647 intel_clock_t *best_clock)
79e53945
JB
648{
649 struct drm_device *dev = crtc->dev;
79e53945 650 intel_clock_t clock;
79e53945
JB
651 int err = target;
652
a210b028 653 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 654 /*
a210b028
DV
655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
79e53945 658 */
1974cad0 659 if (intel_is_dual_link_lvds(dev))
79e53945
JB
660 clock.p2 = limit->p2.p2_fast;
661 else
662 clock.p2 = limit->p2.p2_slow;
663 } else {
664 if (target < limit->p2.dot_limit)
665 clock.p2 = limit->p2.p2_slow;
666 else
667 clock.p2 = limit->p2.p2_fast;
668 }
669
0206e353 670 memset(best_clock, 0, sizeof(*best_clock));
79e53945 671
42158660
ZY
672 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
673 clock.m1++) {
674 for (clock.m2 = limit->m2.min;
675 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
676 for (clock.n = limit->n.min;
677 clock.n <= limit->n.max; clock.n++) {
678 for (clock.p1 = limit->p1.min;
679 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
680 int this_err;
681
ac58c3f0 682 pineview_clock(refclk, &clock);
1b894b59
CW
683 if (!intel_PLL_is_valid(dev, limit,
684 &clock))
79e53945 685 continue;
cec2f356
SP
686 if (match_clock &&
687 clock.p != match_clock->p)
688 continue;
79e53945
JB
689
690 this_err = abs(clock.dot - target);
691 if (this_err < err) {
692 *best_clock = clock;
693 err = this_err;
694 }
695 }
696 }
697 }
698 }
699
700 return (err != target);
701}
702
d4906093 703static bool
ee9300bb
DV
704g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
d4906093
ML
707{
708 struct drm_device *dev = crtc->dev;
d4906093
ML
709 intel_clock_t clock;
710 int max_n;
711 bool found;
6ba770dc
AJ
712 /* approximately equals target * 0.00585 */
713 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
714 found = false;
715
716 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 717 if (intel_is_dual_link_lvds(dev))
d4906093
ML
718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
728 memset(best_clock, 0, sizeof(*best_clock));
729 max_n = limit->n.max;
f77f13e2 730 /* based on hardware requirement, prefer smaller n to precision */
d4906093 731 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 732 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
733 for (clock.m1 = limit->m1.max;
734 clock.m1 >= limit->m1.min; clock.m1--) {
735 for (clock.m2 = limit->m2.max;
736 clock.m2 >= limit->m2.min; clock.m2--) {
737 for (clock.p1 = limit->p1.max;
738 clock.p1 >= limit->p1.min; clock.p1--) {
739 int this_err;
740
ac58c3f0 741 i9xx_clock(refclk, &clock);
1b894b59
CW
742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
d4906093 744 continue;
1b894b59
CW
745
746 this_err = abs(clock.dot - target);
d4906093
ML
747 if (this_err < err_most) {
748 *best_clock = clock;
749 err_most = this_err;
750 max_n = clock.n;
751 found = true;
752 }
753 }
754 }
755 }
756 }
2c07245f
ZW
757 return found;
758}
759
a0c4da24 760static bool
ee9300bb
DV
761vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
762 int target, int refclk, intel_clock_t *match_clock,
763 intel_clock_t *best_clock)
a0c4da24 764{
f01b7962 765 struct drm_device *dev = crtc->dev;
6b4bf1c4 766 intel_clock_t clock;
69e4f900 767 unsigned int bestppm = 1000000;
27e639bf
VS
768 /* min update 19.2 MHz */
769 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 770 bool found = false;
a0c4da24 771
6b4bf1c4
VS
772 target *= 5; /* fast clock */
773
774 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
775
776 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 777 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 778 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 779 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 780 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 781 clock.p = clock.p1 * clock.p2;
a0c4da24 782 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 783 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
784 unsigned int ppm, diff;
785
6b4bf1c4
VS
786 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
787 refclk * clock.m1);
788
789 vlv_clock(refclk, &clock);
43b0ac53 790
f01b7962
VS
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
43b0ac53
VS
793 continue;
794
6b4bf1c4
VS
795 diff = abs(clock.dot - target);
796 ppm = div_u64(1000000ULL * diff, target);
797
798 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 799 bestppm = 0;
6b4bf1c4 800 *best_clock = clock;
49e497ef 801 found = true;
43b0ac53 802 }
6b4bf1c4 803
c686122c 804 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 805 bestppm = ppm;
6b4bf1c4 806 *best_clock = clock;
49e497ef 807 found = true;
a0c4da24
JB
808 }
809 }
810 }
811 }
812 }
a0c4da24 813
49e497ef 814 return found;
a0c4da24 815}
a4fc5ed6 816
ef9348c8
CML
817static bool
818chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
819 int target, int refclk, intel_clock_t *match_clock,
820 intel_clock_t *best_clock)
821{
822 struct drm_device *dev = crtc->dev;
823 intel_clock_t clock;
824 uint64_t m2;
825 int found = false;
826
827 memset(best_clock, 0, sizeof(*best_clock));
828
829 /*
830 * Based on hardware doc, the n always set to 1, and m1 always
831 * set to 2. If requires to support 200Mhz refclk, we need to
832 * revisit this because n may not 1 anymore.
833 */
834 clock.n = 1, clock.m1 = 2;
835 target *= 5; /* fast clock */
836
837 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
838 for (clock.p2 = limit->p2.p2_fast;
839 clock.p2 >= limit->p2.p2_slow;
840 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
841
842 clock.p = clock.p1 * clock.p2;
843
844 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
845 clock.n) << 22, refclk * clock.m1);
846
847 if (m2 > INT_MAX/clock.m1)
848 continue;
849
850 clock.m2 = m2;
851
852 chv_clock(refclk, &clock);
853
854 if (!intel_PLL_is_valid(dev, limit, &clock))
855 continue;
856
857 /* based on hardware requirement, prefer bigger p
858 */
859 if (clock.p > best_clock->p) {
860 *best_clock = clock;
861 found = true;
862 }
863 }
864 }
865
866 return found;
867}
868
20ddf665
VS
869bool intel_crtc_active(struct drm_crtc *crtc)
870{
871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
872
873 /* Be paranoid as we can arrive here with only partial
874 * state retrieved from the hardware during setup.
875 *
241bfc38 876 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
877 * as Haswell has gained clock readout/fastboot support.
878 *
66e514c1 879 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
880 * properly reconstruct framebuffers.
881 */
f4510a27 882 return intel_crtc->active && crtc->primary->fb &&
241bfc38 883 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
884}
885
a5c961d1
PZ
886enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
887 enum pipe pipe)
888{
889 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
891
3b117c8f 892 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
893}
894
57e22f4a 895static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 898 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
899
900 frame = I915_READ(frame_reg);
901
902 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
31e4b89a
DL
903 WARN(1, "vblank wait on pipe %c timed out\n",
904 pipe_name(pipe));
a928d536
PZ
905}
906
9d0498a2
JB
907/**
908 * intel_wait_for_vblank - wait for vblank on a given pipe
909 * @dev: drm device
910 * @pipe: pipe to wait for
911 *
912 * Wait for vblank to occur on a given pipe. Needed for various bits of
913 * mode setting code.
914 */
915void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 916{
9d0498a2 917 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 918 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 919
57e22f4a
VS
920 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
921 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
922 return;
923 }
924
300387c0
CW
925 /* Clear existing vblank status. Note this will clear any other
926 * sticky status fields as well.
927 *
928 * This races with i915_driver_irq_handler() with the result
929 * that either function could miss a vblank event. Here it is not
930 * fatal, as we will either wait upon the next vblank interrupt or
931 * timeout. Generally speaking intel_wait_for_vblank() is only
932 * called during modeset at which time the GPU should be idle and
933 * should *not* be performing page flips and thus not waiting on
934 * vblanks...
935 * Currently, the result of us stealing a vblank from the irq
936 * handler is that a single frame will be skipped during swapbuffers.
937 */
938 I915_WRITE(pipestat_reg,
939 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
940
9d0498a2 941 /* Wait for vblank interrupt bit to set */
481b6af3
CW
942 if (wait_for(I915_READ(pipestat_reg) &
943 PIPE_VBLANK_INTERRUPT_STATUS,
944 50))
31e4b89a
DL
945 DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
946 pipe_name(pipe));
9d0498a2
JB
947}
948
fbf49ea2
VS
949static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
950{
951 struct drm_i915_private *dev_priv = dev->dev_private;
952 u32 reg = PIPEDSL(pipe);
953 u32 line1, line2;
954 u32 line_mask;
955
956 if (IS_GEN2(dev))
957 line_mask = DSL_LINEMASK_GEN2;
958 else
959 line_mask = DSL_LINEMASK_GEN3;
960
961 line1 = I915_READ(reg) & line_mask;
962 mdelay(5);
963 line2 = I915_READ(reg) & line_mask;
964
965 return line1 == line2;
966}
967
ab7ad7f6
KP
968/*
969 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 970 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
971 *
972 * After disabling a pipe, we can't wait for vblank in the usual way,
973 * spinning on the vblank interrupt status bit, since we won't actually
974 * see an interrupt when the pipe is disabled.
975 *
ab7ad7f6
KP
976 * On Gen4 and above:
977 * wait for the pipe register state bit to turn off
978 *
979 * Otherwise:
980 * wait for the display line value to settle (it usually
981 * ends up stopping at the start of the next frame).
58e10eb9 982 *
9d0498a2 983 */
575f7ab7 984static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 985{
575f7ab7 986 struct drm_device *dev = crtc->base.dev;
9d0498a2 987 struct drm_i915_private *dev_priv = dev->dev_private;
575f7ab7
VS
988 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
989 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
990
991 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 992 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
993
994 /* Wait for the Pipe State to go off */
58e10eb9
CW
995 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
996 100))
284637d9 997 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 998 } else {
ab7ad7f6 999 /* Wait for the display line to settle */
fbf49ea2 1000 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1001 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1002 }
79e53945
JB
1003}
1004
b0ea7d37
DL
1005/*
1006 * ibx_digital_port_connected - is the specified port connected?
1007 * @dev_priv: i915 private structure
1008 * @port: the port to test
1009 *
1010 * Returns true if @port is connected, false otherwise.
1011 */
1012bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1013 struct intel_digital_port *port)
1014{
1015 u32 bit;
1016
c36346e3 1017 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1018 switch (port->port) {
c36346e3
DL
1019 case PORT_B:
1020 bit = SDE_PORTB_HOTPLUG;
1021 break;
1022 case PORT_C:
1023 bit = SDE_PORTC_HOTPLUG;
1024 break;
1025 case PORT_D:
1026 bit = SDE_PORTD_HOTPLUG;
1027 break;
1028 default:
1029 return true;
1030 }
1031 } else {
eba905b2 1032 switch (port->port) {
c36346e3
DL
1033 case PORT_B:
1034 bit = SDE_PORTB_HOTPLUG_CPT;
1035 break;
1036 case PORT_C:
1037 bit = SDE_PORTC_HOTPLUG_CPT;
1038 break;
1039 case PORT_D:
1040 bit = SDE_PORTD_HOTPLUG_CPT;
1041 break;
1042 default:
1043 return true;
1044 }
b0ea7d37
DL
1045 }
1046
1047 return I915_READ(SDEISR) & bit;
1048}
1049
b24e7179
JB
1050static const char *state_string(bool enabled)
1051{
1052 return enabled ? "on" : "off";
1053}
1054
1055/* Only for pre-ILK configs */
55607e8a
DV
1056void assert_pll(struct drm_i915_private *dev_priv,
1057 enum pipe pipe, bool state)
b24e7179
JB
1058{
1059 int reg;
1060 u32 val;
1061 bool cur_state;
1062
1063 reg = DPLL(pipe);
1064 val = I915_READ(reg);
1065 cur_state = !!(val & DPLL_VCO_ENABLE);
1066 WARN(cur_state != state,
1067 "PLL state assertion failure (expected %s, current %s)\n",
1068 state_string(state), state_string(cur_state));
1069}
b24e7179 1070
23538ef1
JN
1071/* XXX: the dsi pll is shared between MIPI DSI ports */
1072static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1073{
1074 u32 val;
1075 bool cur_state;
1076
1077 mutex_lock(&dev_priv->dpio_lock);
1078 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1079 mutex_unlock(&dev_priv->dpio_lock);
1080
1081 cur_state = val & DSI_PLL_VCO_EN;
1082 WARN(cur_state != state,
1083 "DSI PLL state assertion failure (expected %s, current %s)\n",
1084 state_string(state), state_string(cur_state));
1085}
1086#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1087#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1088
55607e8a 1089struct intel_shared_dpll *
e2b78267
DV
1090intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1091{
1092 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1093
a43f6e0f 1094 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1095 return NULL;
1096
a43f6e0f 1097 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1098}
1099
040484af 1100/* For ILK+ */
55607e8a
DV
1101void assert_shared_dpll(struct drm_i915_private *dev_priv,
1102 struct intel_shared_dpll *pll,
1103 bool state)
040484af 1104{
040484af 1105 bool cur_state;
5358901f 1106 struct intel_dpll_hw_state hw_state;
040484af 1107
92b27b08 1108 if (WARN (!pll,
46edb027 1109 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1110 return;
ee7b9f93 1111
5358901f 1112 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1113 WARN(cur_state != state,
5358901f
DV
1114 "%s assertion failure (expected %s, current %s)\n",
1115 pll->name, state_string(state), state_string(cur_state));
040484af 1116}
040484af
JB
1117
1118static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
ad80a810
PZ
1124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1125 pipe);
040484af 1126
affa9354
PZ
1127 if (HAS_DDI(dev_priv->dev)) {
1128 /* DDI does not have a specific FDI_TX register */
ad80a810 1129 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1130 val = I915_READ(reg);
ad80a810 1131 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1132 } else {
1133 reg = FDI_TX_CTL(pipe);
1134 val = I915_READ(reg);
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
040484af
JB
1137 WARN(cur_state != state,
1138 "FDI TX state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
1147 int reg;
1148 u32 val;
1149 bool cur_state;
1150
d63fa0dc
PZ
1151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
1153 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1154 WARN(cur_state != state,
1155 "FDI RX state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
1164 int reg;
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
3d13ef2e 1168 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1169 return;
1170
bf507ef7 1171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1172 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1173 return;
1174
040484af
JB
1175 reg = FDI_TX_CTL(pipe);
1176 val = I915_READ(reg);
1177 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1178}
1179
55607e8a
DV
1180void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
040484af
JB
1182{
1183 int reg;
1184 u32 val;
55607e8a 1185 bool cur_state;
040484af
JB
1186
1187 reg = FDI_RX_CTL(pipe);
1188 val = I915_READ(reg);
55607e8a
DV
1189 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1190 WARN(cur_state != state,
1191 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1192 state_string(state), state_string(cur_state));
040484af
JB
1193}
1194
ea0760cf
JB
1195static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1196 enum pipe pipe)
1197{
bedd4dba
JN
1198 struct drm_device *dev = dev_priv->dev;
1199 int pp_reg;
ea0760cf
JB
1200 u32 val;
1201 enum pipe panel_pipe = PIPE_A;
0de3b485 1202 bool locked = true;
ea0760cf 1203
bedd4dba
JN
1204 if (WARN_ON(HAS_DDI(dev)))
1205 return;
1206
1207 if (HAS_PCH_SPLIT(dev)) {
1208 u32 port_sel;
1209
ea0760cf 1210 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1211 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1212
1213 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1214 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1215 panel_pipe = PIPE_B;
1216 /* XXX: else fix for eDP */
1217 } else if (IS_VALLEYVIEW(dev)) {
1218 /* presumably write lock depends on pipe, not port select */
1219 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1220 panel_pipe = pipe;
ea0760cf
JB
1221 } else {
1222 pp_reg = PP_CONTROL;
bedd4dba
JN
1223 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1224 panel_pipe = PIPE_B;
ea0760cf
JB
1225 }
1226
1227 val = I915_READ(pp_reg);
1228 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1229 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1230 locked = false;
1231
ea0760cf
JB
1232 WARN(panel_pipe == pipe && locked,
1233 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1234 pipe_name(pipe));
ea0760cf
JB
1235}
1236
93ce0ba6
JN
1237static void assert_cursor(struct drm_i915_private *dev_priv,
1238 enum pipe pipe, bool state)
1239{
1240 struct drm_device *dev = dev_priv->dev;
1241 bool cur_state;
1242
d9d82081 1243 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1244 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1245 else
5efb3e28 1246 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1247
1248 WARN(cur_state != state,
1249 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1250 pipe_name(pipe), state_string(state), state_string(cur_state));
1251}
1252#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1253#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1254
b840d907
JB
1255void assert_pipe(struct drm_i915_private *dev_priv,
1256 enum pipe pipe, bool state)
b24e7179
JB
1257{
1258 int reg;
1259 u32 val;
63d7bbe9 1260 bool cur_state;
702e7a56
PZ
1261 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1262 pipe);
b24e7179 1263
b6b5d049
VS
1264 /* if we need the pipe quirk it must be always on */
1265 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1266 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1267 state = true;
1268
da7e29bd 1269 if (!intel_display_power_enabled(dev_priv,
b97186f0 1270 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1271 cur_state = false;
1272 } else {
1273 reg = PIPECONF(cpu_transcoder);
1274 val = I915_READ(reg);
1275 cur_state = !!(val & PIPECONF_ENABLE);
1276 }
1277
63d7bbe9
JB
1278 WARN(cur_state != state,
1279 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1280 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1281}
1282
931872fc
CW
1283static void assert_plane(struct drm_i915_private *dev_priv,
1284 enum plane plane, bool state)
b24e7179
JB
1285{
1286 int reg;
1287 u32 val;
931872fc 1288 bool cur_state;
b24e7179
JB
1289
1290 reg = DSPCNTR(plane);
1291 val = I915_READ(reg);
931872fc
CW
1292 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1293 WARN(cur_state != state,
1294 "plane %c assertion failure (expected %s, current %s)\n",
1295 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1296}
1297
931872fc
CW
1298#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1299#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1300
b24e7179
JB
1301static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1302 enum pipe pipe)
1303{
653e1026 1304 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1305 int reg, i;
1306 u32 val;
1307 int cur_pipe;
1308
653e1026
VS
1309 /* Primary planes are fixed to pipes on gen4+ */
1310 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1311 reg = DSPCNTR(pipe);
1312 val = I915_READ(reg);
83f26f16 1313 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1314 "plane %c assertion failure, should be disabled but not\n",
1315 plane_name(pipe));
19ec1358 1316 return;
28c05794 1317 }
19ec1358 1318
b24e7179 1319 /* Need to check both planes against the pipe */
055e393f 1320 for_each_pipe(dev_priv, i) {
b24e7179
JB
1321 reg = DSPCNTR(i);
1322 val = I915_READ(reg);
1323 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1324 DISPPLANE_SEL_PIPE_SHIFT;
1325 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1326 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1327 plane_name(i), pipe_name(pipe));
b24e7179
JB
1328 }
1329}
1330
19332d7a
JB
1331static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1332 enum pipe pipe)
1333{
20674eef 1334 struct drm_device *dev = dev_priv->dev;
1fe47785 1335 int reg, sprite;
19332d7a
JB
1336 u32 val;
1337
20674eef 1338 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1339 for_each_sprite(pipe, sprite) {
1340 reg = SPCNTR(pipe, sprite);
20674eef 1341 val = I915_READ(reg);
83f26f16 1342 WARN(val & SP_ENABLE,
20674eef 1343 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1344 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1345 }
1346 } else if (INTEL_INFO(dev)->gen >= 7) {
1347 reg = SPRCTL(pipe);
19332d7a 1348 val = I915_READ(reg);
83f26f16 1349 WARN(val & SPRITE_ENABLE,
06da8da2 1350 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1351 plane_name(pipe), pipe_name(pipe));
1352 } else if (INTEL_INFO(dev)->gen >= 5) {
1353 reg = DVSCNTR(pipe);
19332d7a 1354 val = I915_READ(reg);
83f26f16 1355 WARN(val & DVS_ENABLE,
06da8da2 1356 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1357 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1358 }
1359}
1360
08c71e5e
VS
1361static void assert_vblank_disabled(struct drm_crtc *crtc)
1362{
1363 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1364 drm_crtc_vblank_put(crtc);
1365}
1366
89eff4be 1367static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1368{
1369 u32 val;
1370 bool enabled;
1371
89eff4be 1372 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1373
92f2584a
JB
1374 val = I915_READ(PCH_DREF_CONTROL);
1375 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1376 DREF_SUPERSPREAD_SOURCE_MASK));
1377 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1378}
1379
ab9412ba
DV
1380static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1381 enum pipe pipe)
92f2584a
JB
1382{
1383 int reg;
1384 u32 val;
1385 bool enabled;
1386
ab9412ba 1387 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1388 val = I915_READ(reg);
1389 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1390 WARN(enabled,
1391 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1392 pipe_name(pipe));
92f2584a
JB
1393}
1394
4e634389
KP
1395static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1397{
1398 if ((val & DP_PORT_EN) == 0)
1399 return false;
1400
1401 if (HAS_PCH_CPT(dev_priv->dev)) {
1402 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1403 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1404 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1405 return false;
44f37d1f
CML
1406 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1407 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1408 return false;
f0575e92
KP
1409 } else {
1410 if ((val & DP_PIPE_MASK) != (pipe << 30))
1411 return false;
1412 }
1413 return true;
1414}
1415
1519b995
KP
1416static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 val)
1418{
dc0fa718 1419 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1420 return false;
1421
1422 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1423 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1424 return false;
44f37d1f
CML
1425 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1426 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1427 return false;
1519b995 1428 } else {
dc0fa718 1429 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1430 return false;
1431 }
1432 return true;
1433}
1434
1435static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1436 enum pipe pipe, u32 val)
1437{
1438 if ((val & LVDS_PORT_EN) == 0)
1439 return false;
1440
1441 if (HAS_PCH_CPT(dev_priv->dev)) {
1442 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1443 return false;
1444 } else {
1445 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1446 return false;
1447 }
1448 return true;
1449}
1450
1451static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, u32 val)
1453{
1454 if ((val & ADPA_DAC_ENABLE) == 0)
1455 return false;
1456 if (HAS_PCH_CPT(dev_priv->dev)) {
1457 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1458 return false;
1459 } else {
1460 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1461 return false;
1462 }
1463 return true;
1464}
1465
291906f1 1466static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1467 enum pipe pipe, int reg, u32 port_sel)
291906f1 1468{
47a05eca 1469 u32 val = I915_READ(reg);
4e634389 1470 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1471 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1472 reg, pipe_name(pipe));
de9a35ab 1473
75c5da27
DV
1474 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1475 && (val & DP_PIPEB_SELECT),
de9a35ab 1476 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1477}
1478
1479static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1480 enum pipe pipe, int reg)
1481{
47a05eca 1482 u32 val = I915_READ(reg);
b70ad586 1483 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1484 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1485 reg, pipe_name(pipe));
de9a35ab 1486
dc0fa718 1487 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1488 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1489 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1490}
1491
1492static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1493 enum pipe pipe)
1494{
1495 int reg;
1496 u32 val;
291906f1 1497
f0575e92
KP
1498 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1499 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1500 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1501
1502 reg = PCH_ADPA;
1503 val = I915_READ(reg);
b70ad586 1504 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1505 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1506 pipe_name(pipe));
291906f1
JB
1507
1508 reg = PCH_LVDS;
1509 val = I915_READ(reg);
b70ad586 1510 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1511 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1512 pipe_name(pipe));
291906f1 1513
e2debe91
PZ
1514 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1515 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1516 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1517}
1518
40e9cf64
JB
1519static void intel_init_dpio(struct drm_device *dev)
1520{
1521 struct drm_i915_private *dev_priv = dev->dev_private;
1522
1523 if (!IS_VALLEYVIEW(dev))
1524 return;
1525
a09caddd
CML
1526 /*
1527 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1528 * CHV x1 PHY (DP/HDMI D)
1529 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1530 */
1531 if (IS_CHERRYVIEW(dev)) {
1532 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1533 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1534 } else {
1535 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1536 }
5382f5f3
JB
1537}
1538
426115cf 1539static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1540{
426115cf
DV
1541 struct drm_device *dev = crtc->base.dev;
1542 struct drm_i915_private *dev_priv = dev->dev_private;
1543 int reg = DPLL(crtc->pipe);
1544 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1545
426115cf 1546 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1547
1548 /* No really, not for ILK+ */
1549 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1550
1551 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1552 if (IS_MOBILE(dev_priv->dev))
426115cf 1553 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1554
426115cf
DV
1555 I915_WRITE(reg, dpll);
1556 POSTING_READ(reg);
1557 udelay(150);
1558
1559 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1560 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1561
1562 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1563 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1564
1565 /* We do this three times for luck */
426115cf 1566 I915_WRITE(reg, dpll);
87442f73
DV
1567 POSTING_READ(reg);
1568 udelay(150); /* wait for warmup */
426115cf 1569 I915_WRITE(reg, dpll);
87442f73
DV
1570 POSTING_READ(reg);
1571 udelay(150); /* wait for warmup */
426115cf 1572 I915_WRITE(reg, dpll);
87442f73
DV
1573 POSTING_READ(reg);
1574 udelay(150); /* wait for warmup */
1575}
1576
9d556c99
CML
1577static void chv_enable_pll(struct intel_crtc *crtc)
1578{
1579 struct drm_device *dev = crtc->base.dev;
1580 struct drm_i915_private *dev_priv = dev->dev_private;
1581 int pipe = crtc->pipe;
1582 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1583 u32 tmp;
1584
1585 assert_pipe_disabled(dev_priv, crtc->pipe);
1586
1587 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1588
1589 mutex_lock(&dev_priv->dpio_lock);
1590
1591 /* Enable back the 10bit clock to display controller */
1592 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1593 tmp |= DPIO_DCLKP_EN;
1594 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1595
1596 /*
1597 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1598 */
1599 udelay(1);
1600
1601 /* Enable PLL */
a11b0703 1602 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
9d556c99
CML
1603
1604 /* Check PLL is locked */
a11b0703 1605 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1606 DRM_ERROR("PLL %d failed to lock\n", pipe);
1607
a11b0703
VS
1608 /* not sure when this should be written */
1609 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1610 POSTING_READ(DPLL_MD(pipe));
1611
9d556c99
CML
1612 mutex_unlock(&dev_priv->dpio_lock);
1613}
1614
66e3d5c0 1615static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1616{
66e3d5c0
DV
1617 struct drm_device *dev = crtc->base.dev;
1618 struct drm_i915_private *dev_priv = dev->dev_private;
1619 int reg = DPLL(crtc->pipe);
1620 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1621
66e3d5c0 1622 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1623
63d7bbe9 1624 /* No really, not for ILK+ */
3d13ef2e 1625 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1626
1627 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1628 if (IS_MOBILE(dev) && !IS_I830(dev))
1629 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1630
66e3d5c0
DV
1631 I915_WRITE(reg, dpll);
1632
1633 /* Wait for the clocks to stabilize. */
1634 POSTING_READ(reg);
1635 udelay(150);
1636
1637 if (INTEL_INFO(dev)->gen >= 4) {
1638 I915_WRITE(DPLL_MD(crtc->pipe),
1639 crtc->config.dpll_hw_state.dpll_md);
1640 } else {
1641 /* The pixel multiplier can only be updated once the
1642 * DPLL is enabled and the clocks are stable.
1643 *
1644 * So write it again.
1645 */
1646 I915_WRITE(reg, dpll);
1647 }
63d7bbe9
JB
1648
1649 /* We do this three times for luck */
66e3d5c0 1650 I915_WRITE(reg, dpll);
63d7bbe9
JB
1651 POSTING_READ(reg);
1652 udelay(150); /* wait for warmup */
66e3d5c0 1653 I915_WRITE(reg, dpll);
63d7bbe9
JB
1654 POSTING_READ(reg);
1655 udelay(150); /* wait for warmup */
66e3d5c0 1656 I915_WRITE(reg, dpll);
63d7bbe9
JB
1657 POSTING_READ(reg);
1658 udelay(150); /* wait for warmup */
1659}
1660
1661/**
50b44a44 1662 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1663 * @dev_priv: i915 private structure
1664 * @pipe: pipe PLL to disable
1665 *
1666 * Disable the PLL for @pipe, making sure the pipe is off first.
1667 *
1668 * Note! This is for pre-ILK only.
1669 */
50b44a44 1670static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1671{
b6b5d049
VS
1672 /* Don't disable pipe or pipe PLLs if needed */
1673 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1674 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1675 return;
1676
1677 /* Make sure the pipe isn't still relying on us */
1678 assert_pipe_disabled(dev_priv, pipe);
1679
50b44a44
DV
1680 I915_WRITE(DPLL(pipe), 0);
1681 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1682}
1683
f6071166
JB
1684static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1685{
1686 u32 val = 0;
1687
1688 /* Make sure the pipe isn't still relying on us */
1689 assert_pipe_disabled(dev_priv, pipe);
1690
e5cbfbfb
ID
1691 /*
1692 * Leave integrated clock source and reference clock enabled for pipe B.
1693 * The latter is needed for VGA hotplug / manual detection.
1694 */
f6071166 1695 if (pipe == PIPE_B)
e5cbfbfb 1696 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1697 I915_WRITE(DPLL(pipe), val);
1698 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1699
1700}
1701
1702static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1703{
d752048d 1704 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1705 u32 val;
1706
a11b0703
VS
1707 /* Make sure the pipe isn't still relying on us */
1708 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1709
a11b0703 1710 /* Set PLL en = 0 */
d17ec4ce 1711 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1712 if (pipe != PIPE_A)
1713 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1714 I915_WRITE(DPLL(pipe), val);
1715 POSTING_READ(DPLL(pipe));
d752048d
VS
1716
1717 mutex_lock(&dev_priv->dpio_lock);
1718
1719 /* Disable 10bit clock to display controller */
1720 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1721 val &= ~DPIO_DCLKP_EN;
1722 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1723
61407f6d
VS
1724 /* disable left/right clock distribution */
1725 if (pipe != PIPE_B) {
1726 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1727 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1728 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1729 } else {
1730 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1731 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1732 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1733 }
1734
d752048d 1735 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1736}
1737
e4607fcf
CML
1738void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1739 struct intel_digital_port *dport)
89b667f8
JB
1740{
1741 u32 port_mask;
00fc31b7 1742 int dpll_reg;
89b667f8 1743
e4607fcf
CML
1744 switch (dport->port) {
1745 case PORT_B:
89b667f8 1746 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1747 dpll_reg = DPLL(0);
e4607fcf
CML
1748 break;
1749 case PORT_C:
89b667f8 1750 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1751 dpll_reg = DPLL(0);
1752 break;
1753 case PORT_D:
1754 port_mask = DPLL_PORTD_READY_MASK;
1755 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1756 break;
1757 default:
1758 BUG();
1759 }
89b667f8 1760
00fc31b7 1761 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1762 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1763 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1764}
1765
b14b1055
DV
1766static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1767{
1768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1771
be19f0ff
CW
1772 if (WARN_ON(pll == NULL))
1773 return;
1774
b14b1055
DV
1775 WARN_ON(!pll->refcount);
1776 if (pll->active == 0) {
1777 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1778 WARN_ON(pll->on);
1779 assert_shared_dpll_disabled(dev_priv, pll);
1780
1781 pll->mode_set(dev_priv, pll);
1782 }
1783}
1784
92f2584a 1785/**
85b3894f 1786 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1787 * @dev_priv: i915 private structure
1788 * @pipe: pipe PLL to enable
1789 *
1790 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1791 * drives the transcoder clock.
1792 */
85b3894f 1793static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1794{
3d13ef2e
DL
1795 struct drm_device *dev = crtc->base.dev;
1796 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1797 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1798
87a875bb 1799 if (WARN_ON(pll == NULL))
48da64a8
CW
1800 return;
1801
1802 if (WARN_ON(pll->refcount == 0))
1803 return;
ee7b9f93 1804
74dd6928 1805 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1806 pll->name, pll->active, pll->on,
e2b78267 1807 crtc->base.base.id);
92f2584a 1808
cdbd2316
DV
1809 if (pll->active++) {
1810 WARN_ON(!pll->on);
e9d6944e 1811 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1812 return;
1813 }
f4a091c7 1814 WARN_ON(pll->on);
ee7b9f93 1815
bd2bb1b9
PZ
1816 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1817
46edb027 1818 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1819 pll->enable(dev_priv, pll);
ee7b9f93 1820 pll->on = true;
92f2584a
JB
1821}
1822
f6daaec2 1823static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1824{
3d13ef2e
DL
1825 struct drm_device *dev = crtc->base.dev;
1826 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1827 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1828
92f2584a 1829 /* PCH only available on ILK+ */
3d13ef2e 1830 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1831 if (WARN_ON(pll == NULL))
ee7b9f93 1832 return;
92f2584a 1833
48da64a8
CW
1834 if (WARN_ON(pll->refcount == 0))
1835 return;
7a419866 1836
46edb027
DV
1837 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1838 pll->name, pll->active, pll->on,
e2b78267 1839 crtc->base.base.id);
7a419866 1840
48da64a8 1841 if (WARN_ON(pll->active == 0)) {
e9d6944e 1842 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1843 return;
1844 }
1845
e9d6944e 1846 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1847 WARN_ON(!pll->on);
cdbd2316 1848 if (--pll->active)
7a419866 1849 return;
ee7b9f93 1850
46edb027 1851 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1852 pll->disable(dev_priv, pll);
ee7b9f93 1853 pll->on = false;
bd2bb1b9
PZ
1854
1855 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1856}
1857
b8a4f404
PZ
1858static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1859 enum pipe pipe)
040484af 1860{
23670b32 1861 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1862 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1864 uint32_t reg, val, pipeconf_val;
040484af
JB
1865
1866 /* PCH only available on ILK+ */
55522f37 1867 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1868
1869 /* Make sure PCH DPLL is enabled */
e72f9fbf 1870 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1871 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1872
1873 /* FDI must be feeding us bits for PCH ports */
1874 assert_fdi_tx_enabled(dev_priv, pipe);
1875 assert_fdi_rx_enabled(dev_priv, pipe);
1876
23670b32
DV
1877 if (HAS_PCH_CPT(dev)) {
1878 /* Workaround: Set the timing override bit before enabling the
1879 * pch transcoder. */
1880 reg = TRANS_CHICKEN2(pipe);
1881 val = I915_READ(reg);
1882 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1883 I915_WRITE(reg, val);
59c859d6 1884 }
23670b32 1885
ab9412ba 1886 reg = PCH_TRANSCONF(pipe);
040484af 1887 val = I915_READ(reg);
5f7f726d 1888 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1889
1890 if (HAS_PCH_IBX(dev_priv->dev)) {
1891 /*
1892 * make the BPC in transcoder be consistent with
1893 * that in pipeconf reg.
1894 */
dfd07d72
DV
1895 val &= ~PIPECONF_BPC_MASK;
1896 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1897 }
5f7f726d
PZ
1898
1899 val &= ~TRANS_INTERLACE_MASK;
1900 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1901 if (HAS_PCH_IBX(dev_priv->dev) &&
1902 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1903 val |= TRANS_LEGACY_INTERLACED_ILK;
1904 else
1905 val |= TRANS_INTERLACED;
5f7f726d
PZ
1906 else
1907 val |= TRANS_PROGRESSIVE;
1908
040484af
JB
1909 I915_WRITE(reg, val | TRANS_ENABLE);
1910 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1911 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1912}
1913
8fb033d7 1914static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1915 enum transcoder cpu_transcoder)
040484af 1916{
8fb033d7 1917 u32 val, pipeconf_val;
8fb033d7
PZ
1918
1919 /* PCH only available on ILK+ */
55522f37 1920 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 1921
8fb033d7 1922 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1923 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1924 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1925
223a6fdf
PZ
1926 /* Workaround: set timing override bit. */
1927 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1928 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1929 I915_WRITE(_TRANSA_CHICKEN2, val);
1930
25f3ef11 1931 val = TRANS_ENABLE;
937bb610 1932 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1933
9a76b1c6
PZ
1934 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1935 PIPECONF_INTERLACED_ILK)
a35f2679 1936 val |= TRANS_INTERLACED;
8fb033d7
PZ
1937 else
1938 val |= TRANS_PROGRESSIVE;
1939
ab9412ba
DV
1940 I915_WRITE(LPT_TRANSCONF, val);
1941 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1942 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1943}
1944
b8a4f404
PZ
1945static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1946 enum pipe pipe)
040484af 1947{
23670b32
DV
1948 struct drm_device *dev = dev_priv->dev;
1949 uint32_t reg, val;
040484af
JB
1950
1951 /* FDI relies on the transcoder */
1952 assert_fdi_tx_disabled(dev_priv, pipe);
1953 assert_fdi_rx_disabled(dev_priv, pipe);
1954
291906f1
JB
1955 /* Ports must be off as well */
1956 assert_pch_ports_disabled(dev_priv, pipe);
1957
ab9412ba 1958 reg = PCH_TRANSCONF(pipe);
040484af
JB
1959 val = I915_READ(reg);
1960 val &= ~TRANS_ENABLE;
1961 I915_WRITE(reg, val);
1962 /* wait for PCH transcoder off, transcoder state */
1963 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1964 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1965
1966 if (!HAS_PCH_IBX(dev)) {
1967 /* Workaround: Clear the timing override chicken bit again. */
1968 reg = TRANS_CHICKEN2(pipe);
1969 val = I915_READ(reg);
1970 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1971 I915_WRITE(reg, val);
1972 }
040484af
JB
1973}
1974
ab4d966c 1975static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1976{
8fb033d7
PZ
1977 u32 val;
1978
ab9412ba 1979 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1980 val &= ~TRANS_ENABLE;
ab9412ba 1981 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1982 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1983 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1984 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1985
1986 /* Workaround: clear timing override bit. */
1987 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1988 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1989 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1990}
1991
b24e7179 1992/**
309cfea8 1993 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1994 * @crtc: crtc responsible for the pipe
b24e7179 1995 *
0372264a 1996 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1997 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1998 */
e1fdc473 1999static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2000{
0372264a
PZ
2001 struct drm_device *dev = crtc->base.dev;
2002 struct drm_i915_private *dev_priv = dev->dev_private;
2003 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2004 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2005 pipe);
1a240d4d 2006 enum pipe pch_transcoder;
b24e7179
JB
2007 int reg;
2008 u32 val;
2009
58c6eaa2 2010 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2011 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2012 assert_sprites_disabled(dev_priv, pipe);
2013
681e5811 2014 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2015 pch_transcoder = TRANSCODER_A;
2016 else
2017 pch_transcoder = pipe;
2018
b24e7179
JB
2019 /*
2020 * A pipe without a PLL won't actually be able to drive bits from
2021 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2022 * need the check.
2023 */
2024 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 2025 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
2026 assert_dsi_pll_enabled(dev_priv);
2027 else
2028 assert_pll_enabled(dev_priv, pipe);
040484af 2029 else {
30421c4f 2030 if (crtc->config.has_pch_encoder) {
040484af 2031 /* if driving the PCH, we need FDI enabled */
cc391bbb 2032 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2033 assert_fdi_tx_pll_enabled(dev_priv,
2034 (enum pipe) cpu_transcoder);
040484af
JB
2035 }
2036 /* FIXME: assert CPU port conditions for SNB+ */
2037 }
b24e7179 2038
702e7a56 2039 reg = PIPECONF(cpu_transcoder);
b24e7179 2040 val = I915_READ(reg);
7ad25d48 2041 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2042 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2043 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2044 return;
7ad25d48 2045 }
00d70b15
CW
2046
2047 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2048 POSTING_READ(reg);
b24e7179
JB
2049}
2050
2051/**
309cfea8 2052 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2053 * @crtc: crtc whose pipes is to be disabled
b24e7179 2054 *
575f7ab7
VS
2055 * Disable the pipe of @crtc, making sure that various hardware
2056 * specific requirements are met, if applicable, e.g. plane
2057 * disabled, panel fitter off, etc.
b24e7179
JB
2058 *
2059 * Will wait until the pipe has shut down before returning.
2060 */
575f7ab7 2061static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2062{
575f7ab7
VS
2063 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2064 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2065 enum pipe pipe = crtc->pipe;
b24e7179
JB
2066 int reg;
2067 u32 val;
2068
2069 /*
2070 * Make sure planes won't keep trying to pump pixels to us,
2071 * or we might hang the display.
2072 */
2073 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2074 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2075 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2076
702e7a56 2077 reg = PIPECONF(cpu_transcoder);
b24e7179 2078 val = I915_READ(reg);
00d70b15
CW
2079 if ((val & PIPECONF_ENABLE) == 0)
2080 return;
2081
67adc644
VS
2082 /*
2083 * Double wide has implications for planes
2084 * so best keep it disabled when not needed.
2085 */
2086 if (crtc->config.double_wide)
2087 val &= ~PIPECONF_DOUBLE_WIDE;
2088
2089 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2090 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2091 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2092 val &= ~PIPECONF_ENABLE;
2093
2094 I915_WRITE(reg, val);
2095 if ((val & PIPECONF_ENABLE) == 0)
2096 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2097}
2098
d74362c9
KP
2099/*
2100 * Plane regs are double buffered, going from enabled->disabled needs a
2101 * trigger in order to latch. The display address reg provides this.
2102 */
1dba99f4
VS
2103void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2104 enum plane plane)
d74362c9 2105{
3d13ef2e
DL
2106 struct drm_device *dev = dev_priv->dev;
2107 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2108
2109 I915_WRITE(reg, I915_READ(reg));
2110 POSTING_READ(reg);
d74362c9
KP
2111}
2112
b24e7179 2113/**
262ca2b0 2114 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2115 * @plane: plane to be enabled
2116 * @crtc: crtc for the plane
b24e7179 2117 *
fdd508a6 2118 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2119 */
fdd508a6
VS
2120static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2121 struct drm_crtc *crtc)
b24e7179 2122{
fdd508a6
VS
2123 struct drm_device *dev = plane->dev;
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2126
2127 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2128 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2129
98ec7739
VS
2130 if (intel_crtc->primary_enabled)
2131 return;
0037f71c 2132
4c445e0e 2133 intel_crtc->primary_enabled = true;
939c2fe8 2134
fdd508a6
VS
2135 dev_priv->display.update_primary_plane(crtc, plane->fb,
2136 crtc->x, crtc->y);
33c3b0d1
VS
2137
2138 /*
2139 * BDW signals flip done immediately if the plane
2140 * is disabled, even if the plane enable is already
2141 * armed to occur at the next vblank :(
2142 */
2143 if (IS_BROADWELL(dev))
2144 intel_wait_for_vblank(dev, intel_crtc->pipe);
b24e7179
JB
2145}
2146
b24e7179 2147/**
262ca2b0 2148 * intel_disable_primary_hw_plane - disable the primary hardware plane
fdd508a6
VS
2149 * @plane: plane to be disabled
2150 * @crtc: crtc for the plane
b24e7179 2151 *
fdd508a6 2152 * Disable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2153 */
fdd508a6
VS
2154static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2155 struct drm_crtc *crtc)
b24e7179 2156{
fdd508a6
VS
2157 struct drm_device *dev = plane->dev;
2158 struct drm_i915_private *dev_priv = dev->dev_private;
2159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2160
2161 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b24e7179 2162
98ec7739
VS
2163 if (!intel_crtc->primary_enabled)
2164 return;
0037f71c 2165
4c445e0e 2166 intel_crtc->primary_enabled = false;
939c2fe8 2167
fdd508a6
VS
2168 dev_priv->display.update_primary_plane(crtc, plane->fb,
2169 crtc->x, crtc->y);
b24e7179
JB
2170}
2171
693db184
CW
2172static bool need_vtd_wa(struct drm_device *dev)
2173{
2174#ifdef CONFIG_INTEL_IOMMU
2175 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2176 return true;
2177#endif
2178 return false;
2179}
2180
a57ce0b2
JB
2181static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2182{
2183 int tile_height;
2184
2185 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2186 return ALIGN(height, tile_height);
2187}
2188
127bd2ac 2189int
48b956c5 2190intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2191 struct drm_i915_gem_object *obj,
a4872ba6 2192 struct intel_engine_cs *pipelined)
6b95a207 2193{
ce453d81 2194 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2195 u32 alignment;
2196 int ret;
2197
ebcdd39e
MR
2198 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2199
05394f39 2200 switch (obj->tiling_mode) {
6b95a207 2201 case I915_TILING_NONE:
1fada4cc
DL
2202 if (INTEL_INFO(dev)->gen >= 9)
2203 alignment = 256 * 1024;
2204 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2205 alignment = 128 * 1024;
a6c45cf0 2206 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2207 alignment = 4 * 1024;
2208 else
2209 alignment = 64 * 1024;
6b95a207
KH
2210 break;
2211 case I915_TILING_X:
1fada4cc
DL
2212 if (INTEL_INFO(dev)->gen >= 9)
2213 alignment = 256 * 1024;
2214 else {
2215 /* pin() will align the object as required by fence */
2216 alignment = 0;
2217 }
6b95a207
KH
2218 break;
2219 case I915_TILING_Y:
80075d49 2220 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2221 return -EINVAL;
2222 default:
2223 BUG();
2224 }
2225
693db184
CW
2226 /* Note that the w/a also requires 64 PTE of padding following the
2227 * bo. We currently fill all unused PTE with the shadow page and so
2228 * we should always have valid PTE following the scanout preventing
2229 * the VT-d warning.
2230 */
2231 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2232 alignment = 256 * 1024;
2233
d6dd6843
PZ
2234 /*
2235 * Global gtt pte registers are special registers which actually forward
2236 * writes to a chunk of system memory. Which means that there is no risk
2237 * that the register values disappear as soon as we call
2238 * intel_runtime_pm_put(), so it is correct to wrap only the
2239 * pin/unpin/fence and not more.
2240 */
2241 intel_runtime_pm_get(dev_priv);
2242
ce453d81 2243 dev_priv->mm.interruptible = false;
2da3b9b9 2244 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2245 if (ret)
ce453d81 2246 goto err_interruptible;
6b95a207
KH
2247
2248 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2249 * fence, whereas 965+ only requires a fence if using
2250 * framebuffer compression. For simplicity, we always install
2251 * a fence as the cost is not that onerous.
2252 */
06d98131 2253 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2254 if (ret)
2255 goto err_unpin;
1690e1eb 2256
9a5a53b3 2257 i915_gem_object_pin_fence(obj);
6b95a207 2258
ce453d81 2259 dev_priv->mm.interruptible = true;
d6dd6843 2260 intel_runtime_pm_put(dev_priv);
6b95a207 2261 return 0;
48b956c5
CW
2262
2263err_unpin:
cc98b413 2264 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2265err_interruptible:
2266 dev_priv->mm.interruptible = true;
d6dd6843 2267 intel_runtime_pm_put(dev_priv);
48b956c5 2268 return ret;
6b95a207
KH
2269}
2270
1690e1eb
CW
2271void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2272{
ebcdd39e
MR
2273 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2274
1690e1eb 2275 i915_gem_object_unpin_fence(obj);
cc98b413 2276 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2277}
2278
c2c75131
DV
2279/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2280 * is assumed to be a power-of-two. */
bc752862
CW
2281unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2282 unsigned int tiling_mode,
2283 unsigned int cpp,
2284 unsigned int pitch)
c2c75131 2285{
bc752862
CW
2286 if (tiling_mode != I915_TILING_NONE) {
2287 unsigned int tile_rows, tiles;
c2c75131 2288
bc752862
CW
2289 tile_rows = *y / 8;
2290 *y %= 8;
c2c75131 2291
bc752862
CW
2292 tiles = *x / (512/cpp);
2293 *x %= 512/cpp;
2294
2295 return tile_rows * pitch * 8 + tiles * 4096;
2296 } else {
2297 unsigned int offset;
2298
2299 offset = *y * pitch + *x * cpp;
2300 *y = 0;
2301 *x = (offset & 4095) / cpp;
2302 return offset & -4096;
2303 }
c2c75131
DV
2304}
2305
46f297fb
JB
2306int intel_format_to_fourcc(int format)
2307{
2308 switch (format) {
2309 case DISPPLANE_8BPP:
2310 return DRM_FORMAT_C8;
2311 case DISPPLANE_BGRX555:
2312 return DRM_FORMAT_XRGB1555;
2313 case DISPPLANE_BGRX565:
2314 return DRM_FORMAT_RGB565;
2315 default:
2316 case DISPPLANE_BGRX888:
2317 return DRM_FORMAT_XRGB8888;
2318 case DISPPLANE_RGBX888:
2319 return DRM_FORMAT_XBGR8888;
2320 case DISPPLANE_BGRX101010:
2321 return DRM_FORMAT_XRGB2101010;
2322 case DISPPLANE_RGBX101010:
2323 return DRM_FORMAT_XBGR2101010;
2324 }
2325}
2326
484b41dd 2327static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2328 struct intel_plane_config *plane_config)
2329{
2330 struct drm_device *dev = crtc->base.dev;
2331 struct drm_i915_gem_object *obj = NULL;
2332 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2333 u32 base = plane_config->base;
2334
ff2652ea
CW
2335 if (plane_config->size == 0)
2336 return false;
2337
46f297fb
JB
2338 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2339 plane_config->size);
2340 if (!obj)
484b41dd 2341 return false;
46f297fb
JB
2342
2343 if (plane_config->tiled) {
2344 obj->tiling_mode = I915_TILING_X;
66e514c1 2345 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2346 }
2347
66e514c1
DA
2348 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2349 mode_cmd.width = crtc->base.primary->fb->width;
2350 mode_cmd.height = crtc->base.primary->fb->height;
2351 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2352
2353 mutex_lock(&dev->struct_mutex);
2354
66e514c1 2355 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2356 &mode_cmd, obj)) {
46f297fb
JB
2357 DRM_DEBUG_KMS("intel fb init failed\n");
2358 goto out_unref_obj;
2359 }
2360
a071fa00 2361 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
46f297fb 2362 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2363
2364 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2365 return true;
46f297fb
JB
2366
2367out_unref_obj:
2368 drm_gem_object_unreference(&obj->base);
2369 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2370 return false;
2371}
2372
2373static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2374 struct intel_plane_config *plane_config)
2375{
2376 struct drm_device *dev = intel_crtc->base.dev;
2377 struct drm_crtc *c;
2378 struct intel_crtc *i;
2ff8fde1 2379 struct drm_i915_gem_object *obj;
484b41dd 2380
66e514c1 2381 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2382 return;
2383
2384 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2385 return;
2386
66e514c1
DA
2387 kfree(intel_crtc->base.primary->fb);
2388 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2389
2390 /*
2391 * Failed to alloc the obj, check to see if we should share
2392 * an fb with another CRTC instead
2393 */
70e1e0ec 2394 for_each_crtc(dev, c) {
484b41dd
JB
2395 i = to_intel_crtc(c);
2396
2397 if (c == &intel_crtc->base)
2398 continue;
2399
2ff8fde1
MR
2400 if (!i->active)
2401 continue;
2402
2403 obj = intel_fb_obj(c->primary->fb);
2404 if (obj == NULL)
484b41dd
JB
2405 continue;
2406
2ff8fde1 2407 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
66e514c1
DA
2408 drm_framebuffer_reference(c->primary->fb);
2409 intel_crtc->base.primary->fb = c->primary->fb;
2ff8fde1 2410 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
484b41dd
JB
2411 break;
2412 }
2413 }
46f297fb
JB
2414}
2415
29b9bde6
DV
2416static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2417 struct drm_framebuffer *fb,
2418 int x, int y)
81255565
JB
2419{
2420 struct drm_device *dev = crtc->dev;
2421 struct drm_i915_private *dev_priv = dev->dev_private;
2422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2423 struct drm_i915_gem_object *obj;
81255565 2424 int plane = intel_crtc->plane;
e506a0c6 2425 unsigned long linear_offset;
81255565 2426 u32 dspcntr;
f45651ba 2427 u32 reg = DSPCNTR(plane);
48404c1e 2428 int pixel_size;
f45651ba 2429
fdd508a6
VS
2430 if (!intel_crtc->primary_enabled) {
2431 I915_WRITE(reg, 0);
2432 if (INTEL_INFO(dev)->gen >= 4)
2433 I915_WRITE(DSPSURF(plane), 0);
2434 else
2435 I915_WRITE(DSPADDR(plane), 0);
2436 POSTING_READ(reg);
2437 return;
2438 }
2439
c9ba6fad
VS
2440 obj = intel_fb_obj(fb);
2441 if (WARN_ON(obj == NULL))
2442 return;
2443
2444 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2445
f45651ba
VS
2446 dspcntr = DISPPLANE_GAMMA_ENABLE;
2447
fdd508a6 2448 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2449
2450 if (INTEL_INFO(dev)->gen < 4) {
2451 if (intel_crtc->pipe == PIPE_B)
2452 dspcntr |= DISPPLANE_SEL_PIPE_B;
2453
2454 /* pipesrc and dspsize control the size that is scaled from,
2455 * which should always be the user's requested size.
2456 */
2457 I915_WRITE(DSPSIZE(plane),
2458 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2459 (intel_crtc->config.pipe_src_w - 1));
2460 I915_WRITE(DSPPOS(plane), 0);
2461 }
81255565 2462
57779d06
VS
2463 switch (fb->pixel_format) {
2464 case DRM_FORMAT_C8:
81255565
JB
2465 dspcntr |= DISPPLANE_8BPP;
2466 break;
57779d06
VS
2467 case DRM_FORMAT_XRGB1555:
2468 case DRM_FORMAT_ARGB1555:
2469 dspcntr |= DISPPLANE_BGRX555;
81255565 2470 break;
57779d06
VS
2471 case DRM_FORMAT_RGB565:
2472 dspcntr |= DISPPLANE_BGRX565;
2473 break;
2474 case DRM_FORMAT_XRGB8888:
2475 case DRM_FORMAT_ARGB8888:
2476 dspcntr |= DISPPLANE_BGRX888;
2477 break;
2478 case DRM_FORMAT_XBGR8888:
2479 case DRM_FORMAT_ABGR8888:
2480 dspcntr |= DISPPLANE_RGBX888;
2481 break;
2482 case DRM_FORMAT_XRGB2101010:
2483 case DRM_FORMAT_ARGB2101010:
2484 dspcntr |= DISPPLANE_BGRX101010;
2485 break;
2486 case DRM_FORMAT_XBGR2101010:
2487 case DRM_FORMAT_ABGR2101010:
2488 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2489 break;
2490 default:
baba133a 2491 BUG();
81255565 2492 }
57779d06 2493
f45651ba
VS
2494 if (INTEL_INFO(dev)->gen >= 4 &&
2495 obj->tiling_mode != I915_TILING_NONE)
2496 dspcntr |= DISPPLANE_TILED;
81255565 2497
de1aa629
VS
2498 if (IS_G4X(dev))
2499 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2500
b9897127 2501 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2502
c2c75131
DV
2503 if (INTEL_INFO(dev)->gen >= 4) {
2504 intel_crtc->dspaddr_offset =
bc752862 2505 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2506 pixel_size,
bc752862 2507 fb->pitches[0]);
c2c75131
DV
2508 linear_offset -= intel_crtc->dspaddr_offset;
2509 } else {
e506a0c6 2510 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2511 }
e506a0c6 2512
48404c1e
SJ
2513 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2514 dspcntr |= DISPPLANE_ROTATE_180;
2515
2516 x += (intel_crtc->config.pipe_src_w - 1);
2517 y += (intel_crtc->config.pipe_src_h - 1);
2518
2519 /* Finding the last pixel of the last line of the display
2520 data and adding to linear_offset*/
2521 linear_offset +=
2522 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2523 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2524 }
2525
2526 I915_WRITE(reg, dspcntr);
2527
f343c5f6
BW
2528 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2529 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2530 fb->pitches[0]);
01f2c773 2531 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2532 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2533 I915_WRITE(DSPSURF(plane),
2534 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2535 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2536 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2537 } else
f343c5f6 2538 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2539 POSTING_READ(reg);
17638cd6
JB
2540}
2541
29b9bde6
DV
2542static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2543 struct drm_framebuffer *fb,
2544 int x, int y)
17638cd6
JB
2545{
2546 struct drm_device *dev = crtc->dev;
2547 struct drm_i915_private *dev_priv = dev->dev_private;
2548 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c9ba6fad 2549 struct drm_i915_gem_object *obj;
17638cd6 2550 int plane = intel_crtc->plane;
e506a0c6 2551 unsigned long linear_offset;
17638cd6 2552 u32 dspcntr;
f45651ba 2553 u32 reg = DSPCNTR(plane);
48404c1e 2554 int pixel_size;
f45651ba 2555
fdd508a6
VS
2556 if (!intel_crtc->primary_enabled) {
2557 I915_WRITE(reg, 0);
2558 I915_WRITE(DSPSURF(plane), 0);
2559 POSTING_READ(reg);
2560 return;
2561 }
2562
c9ba6fad
VS
2563 obj = intel_fb_obj(fb);
2564 if (WARN_ON(obj == NULL))
2565 return;
2566
2567 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2568
f45651ba
VS
2569 dspcntr = DISPPLANE_GAMMA_ENABLE;
2570
fdd508a6 2571 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2572
2573 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2574 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2575
57779d06
VS
2576 switch (fb->pixel_format) {
2577 case DRM_FORMAT_C8:
17638cd6
JB
2578 dspcntr |= DISPPLANE_8BPP;
2579 break;
57779d06
VS
2580 case DRM_FORMAT_RGB565:
2581 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2582 break;
57779d06
VS
2583 case DRM_FORMAT_XRGB8888:
2584 case DRM_FORMAT_ARGB8888:
2585 dspcntr |= DISPPLANE_BGRX888;
2586 break;
2587 case DRM_FORMAT_XBGR8888:
2588 case DRM_FORMAT_ABGR8888:
2589 dspcntr |= DISPPLANE_RGBX888;
2590 break;
2591 case DRM_FORMAT_XRGB2101010:
2592 case DRM_FORMAT_ARGB2101010:
2593 dspcntr |= DISPPLANE_BGRX101010;
2594 break;
2595 case DRM_FORMAT_XBGR2101010:
2596 case DRM_FORMAT_ABGR2101010:
2597 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2598 break;
2599 default:
baba133a 2600 BUG();
17638cd6
JB
2601 }
2602
2603 if (obj->tiling_mode != I915_TILING_NONE)
2604 dspcntr |= DISPPLANE_TILED;
17638cd6 2605
f45651ba 2606 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2607 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2608
b9897127 2609 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2610 intel_crtc->dspaddr_offset =
bc752862 2611 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2612 pixel_size,
bc752862 2613 fb->pitches[0]);
c2c75131 2614 linear_offset -= intel_crtc->dspaddr_offset;
48404c1e
SJ
2615 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2616 dspcntr |= DISPPLANE_ROTATE_180;
2617
2618 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2619 x += (intel_crtc->config.pipe_src_w - 1);
2620 y += (intel_crtc->config.pipe_src_h - 1);
2621
2622 /* Finding the last pixel of the last line of the display
2623 data and adding to linear_offset*/
2624 linear_offset +=
2625 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2626 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2627 }
2628 }
2629
2630 I915_WRITE(reg, dspcntr);
17638cd6 2631
f343c5f6
BW
2632 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2633 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2634 fb->pitches[0]);
01f2c773 2635 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2636 I915_WRITE(DSPSURF(plane),
2637 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2638 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2639 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2640 } else {
2641 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2642 I915_WRITE(DSPLINOFF(plane), linear_offset);
2643 }
17638cd6 2644 POSTING_READ(reg);
17638cd6
JB
2645}
2646
70d21f0e
DL
2647static void skylake_update_primary_plane(struct drm_crtc *crtc,
2648 struct drm_framebuffer *fb,
2649 int x, int y)
2650{
2651 struct drm_device *dev = crtc->dev;
2652 struct drm_i915_private *dev_priv = dev->dev_private;
2653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2654 struct intel_framebuffer *intel_fb;
2655 struct drm_i915_gem_object *obj;
2656 int pipe = intel_crtc->pipe;
2657 u32 plane_ctl, stride;
2658
2659 if (!intel_crtc->primary_enabled) {
2660 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2661 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2662 POSTING_READ(PLANE_CTL(pipe, 0));
2663 return;
2664 }
2665
2666 plane_ctl = PLANE_CTL_ENABLE |
2667 PLANE_CTL_PIPE_GAMMA_ENABLE |
2668 PLANE_CTL_PIPE_CSC_ENABLE;
2669
2670 switch (fb->pixel_format) {
2671 case DRM_FORMAT_RGB565:
2672 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2673 break;
2674 case DRM_FORMAT_XRGB8888:
2675 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2676 break;
2677 case DRM_FORMAT_XBGR8888:
2678 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2679 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2680 break;
2681 case DRM_FORMAT_XRGB2101010:
2682 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2683 break;
2684 case DRM_FORMAT_XBGR2101010:
2685 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2686 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2687 break;
2688 default:
2689 BUG();
2690 }
2691
2692 intel_fb = to_intel_framebuffer(fb);
2693 obj = intel_fb->obj;
2694
2695 /*
2696 * The stride is either expressed as a multiple of 64 bytes chunks for
2697 * linear buffers or in number of tiles for tiled buffers.
2698 */
2699 switch (obj->tiling_mode) {
2700 case I915_TILING_NONE:
2701 stride = fb->pitches[0] >> 6;
2702 break;
2703 case I915_TILING_X:
2704 plane_ctl |= PLANE_CTL_TILED_X;
2705 stride = fb->pitches[0] >> 9;
2706 break;
2707 default:
2708 BUG();
2709 }
2710
2711 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
2712
2713 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2714
2715 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2716 i915_gem_obj_ggtt_offset(obj),
2717 x, y, fb->width, fb->height,
2718 fb->pitches[0]);
2719
2720 I915_WRITE(PLANE_POS(pipe, 0), 0);
2721 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2722 I915_WRITE(PLANE_SIZE(pipe, 0),
2723 (intel_crtc->config.pipe_src_h - 1) << 16 |
2724 (intel_crtc->config.pipe_src_w - 1));
2725 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2726 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2727
2728 POSTING_READ(PLANE_SURF(pipe, 0));
2729}
2730
17638cd6
JB
2731/* Assume fb object is pinned & idle & fenced and just update base pointers */
2732static int
2733intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2734 int x, int y, enum mode_set_atomic state)
2735{
2736 struct drm_device *dev = crtc->dev;
2737 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2738
6b8e6ed0
CW
2739 if (dev_priv->display.disable_fbc)
2740 dev_priv->display.disable_fbc(dev);
cc36513c 2741 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
81255565 2742
29b9bde6
DV
2743 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2744
2745 return 0;
81255565
JB
2746}
2747
96a02917
VS
2748void intel_display_handle_reset(struct drm_device *dev)
2749{
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751 struct drm_crtc *crtc;
2752
2753 /*
2754 * Flips in the rings have been nuked by the reset,
2755 * so complete all pending flips so that user space
2756 * will get its events and not get stuck.
2757 *
2758 * Also update the base address of all primary
2759 * planes to the the last fb to make sure we're
2760 * showing the correct fb after a reset.
2761 *
2762 * Need to make two loops over the crtcs so that we
2763 * don't try to grab a crtc mutex before the
2764 * pending_flip_queue really got woken up.
2765 */
2766
70e1e0ec 2767 for_each_crtc(dev, crtc) {
96a02917
VS
2768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2769 enum plane plane = intel_crtc->plane;
2770
2771 intel_prepare_page_flip(dev, plane);
2772 intel_finish_page_flip_plane(dev, plane);
2773 }
2774
70e1e0ec 2775 for_each_crtc(dev, crtc) {
96a02917
VS
2776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2777
51fd371b 2778 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
2779 /*
2780 * FIXME: Once we have proper support for primary planes (and
2781 * disabling them without disabling the entire crtc) allow again
66e514c1 2782 * a NULL crtc->primary->fb.
947fdaad 2783 */
f4510a27 2784 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2785 dev_priv->display.update_primary_plane(crtc,
66e514c1 2786 crtc->primary->fb,
262ca2b0
MR
2787 crtc->x,
2788 crtc->y);
51fd371b 2789 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
2790 }
2791}
2792
14667a4b
CW
2793static int
2794intel_finish_fb(struct drm_framebuffer *old_fb)
2795{
2ff8fde1 2796 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
2797 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2798 bool was_interruptible = dev_priv->mm.interruptible;
2799 int ret;
2800
14667a4b
CW
2801 /* Big Hammer, we also need to ensure that any pending
2802 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2803 * current scanout is retired before unpinning the old
2804 * framebuffer.
2805 *
2806 * This should only fail upon a hung GPU, in which case we
2807 * can safely continue.
2808 */
2809 dev_priv->mm.interruptible = false;
2810 ret = i915_gem_object_finish_gpu(obj);
2811 dev_priv->mm.interruptible = was_interruptible;
2812
2813 return ret;
2814}
2815
7d5e3799
CW
2816static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2817{
2818 struct drm_device *dev = crtc->dev;
2819 struct drm_i915_private *dev_priv = dev->dev_private;
2820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2821 unsigned long flags;
2822 bool pending;
2823
2824 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2825 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2826 return false;
2827
2828 spin_lock_irqsave(&dev->event_lock, flags);
2829 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2830 spin_unlock_irqrestore(&dev->event_lock, flags);
2831
2832 return pending;
2833}
2834
5c3b82e2 2835static int
3c4fdcfb 2836intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2837 struct drm_framebuffer *fb)
79e53945
JB
2838{
2839 struct drm_device *dev = crtc->dev;
6b8e6ed0 2840 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 2842 enum pipe pipe = intel_crtc->pipe;
2ff8fde1
MR
2843 struct drm_framebuffer *old_fb = crtc->primary->fb;
2844 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2845 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
5c3b82e2 2846 int ret;
79e53945 2847
7d5e3799
CW
2848 if (intel_crtc_has_pending_flip(crtc)) {
2849 DRM_ERROR("pipe is still busy with an old pageflip\n");
2850 return -EBUSY;
2851 }
2852
79e53945 2853 /* no fb bound */
94352cf9 2854 if (!fb) {
a5071c2f 2855 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2856 return 0;
2857 }
2858
7eb552ae 2859 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2860 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2861 plane_name(intel_crtc->plane),
2862 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2863 return -EINVAL;
79e53945
JB
2864 }
2865
5c3b82e2 2866 mutex_lock(&dev->struct_mutex);
a071fa00
DV
2867 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2868 if (ret == 0)
91565c85 2869 i915_gem_track_fb(old_obj, obj,
a071fa00 2870 INTEL_FRONTBUFFER_PRIMARY(pipe));
8ac36ec1 2871 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2872 if (ret != 0) {
a5071c2f 2873 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2874 return ret;
2875 }
79e53945 2876
bb2043de
DL
2877 /*
2878 * Update pipe size and adjust fitter if needed: the reason for this is
2879 * that in compute_mode_changes we check the native mode (not the pfit
2880 * mode) to see if we can flip rather than do a full mode set. In the
2881 * fastboot case, we'll flip, but if we don't update the pipesrc and
2882 * pfit state, we'll end up with a big fb scanned out into the wrong
2883 * sized surface.
2884 *
2885 * To fix this properly, we need to hoist the checks up into
2886 * compute_mode_changes (or above), check the actual pfit state and
2887 * whether the platform allows pfit disable with pipe active, and only
2888 * then update the pipesrc and pfit state, even on the flip path.
2889 */
d330a953 2890 if (i915.fastboot) {
d7bf63f2
DL
2891 const struct drm_display_mode *adjusted_mode =
2892 &intel_crtc->config.adjusted_mode;
2893
4d6a3e63 2894 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2895 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2896 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2897 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2898 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2899 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2900 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2901 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2902 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2903 }
0637d60d
JB
2904 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2905 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2906 }
2907
29b9bde6 2908 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3c4fdcfb 2909
f99d7069
DV
2910 if (intel_crtc->active)
2911 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2912
f4510a27 2913 crtc->primary->fb = fb;
6c4c86f5
DV
2914 crtc->x = x;
2915 crtc->y = y;
94352cf9 2916
b7f1de28 2917 if (old_fb) {
d7697eea
DV
2918 if (intel_crtc->active && old_fb != fb)
2919 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2920 mutex_lock(&dev->struct_mutex);
2ff8fde1 2921 intel_unpin_fb_obj(old_obj);
8ac36ec1 2922 mutex_unlock(&dev->struct_mutex);
b7f1de28 2923 }
652c393a 2924
8ac36ec1 2925 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2926 intel_update_fbc(dev);
5c3b82e2 2927 mutex_unlock(&dev->struct_mutex);
79e53945 2928
5c3b82e2 2929 return 0;
79e53945
JB
2930}
2931
5e84e1a4
ZW
2932static void intel_fdi_normal_train(struct drm_crtc *crtc)
2933{
2934 struct drm_device *dev = crtc->dev;
2935 struct drm_i915_private *dev_priv = dev->dev_private;
2936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2937 int pipe = intel_crtc->pipe;
2938 u32 reg, temp;
2939
2940 /* enable normal train */
2941 reg = FDI_TX_CTL(pipe);
2942 temp = I915_READ(reg);
61e499bf 2943 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2944 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2945 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2946 } else {
2947 temp &= ~FDI_LINK_TRAIN_NONE;
2948 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2949 }
5e84e1a4
ZW
2950 I915_WRITE(reg, temp);
2951
2952 reg = FDI_RX_CTL(pipe);
2953 temp = I915_READ(reg);
2954 if (HAS_PCH_CPT(dev)) {
2955 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2956 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2957 } else {
2958 temp &= ~FDI_LINK_TRAIN_NONE;
2959 temp |= FDI_LINK_TRAIN_NONE;
2960 }
2961 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2962
2963 /* wait one idle pattern time */
2964 POSTING_READ(reg);
2965 udelay(1000);
357555c0
JB
2966
2967 /* IVB wants error correction enabled */
2968 if (IS_IVYBRIDGE(dev))
2969 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2970 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2971}
2972
1fbc0d78 2973static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2974{
1fbc0d78
DV
2975 return crtc->base.enabled && crtc->active &&
2976 crtc->config.has_pch_encoder;
1e833f40
DV
2977}
2978
01a415fd
DV
2979static void ivb_modeset_global_resources(struct drm_device *dev)
2980{
2981 struct drm_i915_private *dev_priv = dev->dev_private;
2982 struct intel_crtc *pipe_B_crtc =
2983 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2984 struct intel_crtc *pipe_C_crtc =
2985 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2986 uint32_t temp;
2987
1e833f40
DV
2988 /*
2989 * When everything is off disable fdi C so that we could enable fdi B
2990 * with all lanes. Note that we don't care about enabled pipes without
2991 * an enabled pch encoder.
2992 */
2993 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2994 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2995 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2996 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2997
2998 temp = I915_READ(SOUTH_CHICKEN1);
2999 temp &= ~FDI_BC_BIFURCATION_SELECT;
3000 DRM_DEBUG_KMS("disabling fdi C rx\n");
3001 I915_WRITE(SOUTH_CHICKEN1, temp);
3002 }
3003}
3004
8db9d77b
ZW
3005/* The FDI link training functions for ILK/Ibexpeak. */
3006static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3007{
3008 struct drm_device *dev = crtc->dev;
3009 struct drm_i915_private *dev_priv = dev->dev_private;
3010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3011 int pipe = intel_crtc->pipe;
5eddb70b 3012 u32 reg, temp, tries;
8db9d77b 3013
1c8562f6 3014 /* FDI needs bits from pipe first */
0fc932b8 3015 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 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 I915_READ(reg);
e1a44743
AJ
3025 udelay(150);
3026
8db9d77b 3027 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3028 reg = FDI_TX_CTL(pipe);
3029 temp = I915_READ(reg);
627eb5a3
DV
3030 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3031 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3032 temp &= ~FDI_LINK_TRAIN_NONE;
3033 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3034 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3035
5eddb70b
CW
3036 reg = FDI_RX_CTL(pipe);
3037 temp = I915_READ(reg);
8db9d77b
ZW
3038 temp &= ~FDI_LINK_TRAIN_NONE;
3039 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3040 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3041
3042 POSTING_READ(reg);
8db9d77b
ZW
3043 udelay(150);
3044
5b2adf89 3045 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3046 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3047 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3048 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3049
5eddb70b 3050 reg = FDI_RX_IIR(pipe);
e1a44743 3051 for (tries = 0; tries < 5; tries++) {
5eddb70b 3052 temp = I915_READ(reg);
8db9d77b
ZW
3053 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3054
3055 if ((temp & FDI_RX_BIT_LOCK)) {
3056 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3057 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3058 break;
3059 }
8db9d77b 3060 }
e1a44743 3061 if (tries == 5)
5eddb70b 3062 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3063
3064 /* Train 2 */
5eddb70b
CW
3065 reg = FDI_TX_CTL(pipe);
3066 temp = I915_READ(reg);
8db9d77b
ZW
3067 temp &= ~FDI_LINK_TRAIN_NONE;
3068 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3069 I915_WRITE(reg, temp);
8db9d77b 3070
5eddb70b
CW
3071 reg = FDI_RX_CTL(pipe);
3072 temp = I915_READ(reg);
8db9d77b
ZW
3073 temp &= ~FDI_LINK_TRAIN_NONE;
3074 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3075 I915_WRITE(reg, temp);
8db9d77b 3076
5eddb70b
CW
3077 POSTING_READ(reg);
3078 udelay(150);
8db9d77b 3079
5eddb70b 3080 reg = FDI_RX_IIR(pipe);
e1a44743 3081 for (tries = 0; tries < 5; tries++) {
5eddb70b 3082 temp = I915_READ(reg);
8db9d77b
ZW
3083 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3084
3085 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3086 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3087 DRM_DEBUG_KMS("FDI train 2 done.\n");
3088 break;
3089 }
8db9d77b 3090 }
e1a44743 3091 if (tries == 5)
5eddb70b 3092 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3093
3094 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3095
8db9d77b
ZW
3096}
3097
0206e353 3098static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3099 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3100 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3101 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3102 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3103};
3104
3105/* The FDI link training functions for SNB/Cougarpoint. */
3106static void gen6_fdi_link_train(struct drm_crtc *crtc)
3107{
3108 struct drm_device *dev = crtc->dev;
3109 struct drm_i915_private *dev_priv = dev->dev_private;
3110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3111 int pipe = intel_crtc->pipe;
fa37d39e 3112 u32 reg, temp, i, retry;
8db9d77b 3113
e1a44743
AJ
3114 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3115 for train result */
5eddb70b
CW
3116 reg = FDI_RX_IMR(pipe);
3117 temp = I915_READ(reg);
e1a44743
AJ
3118 temp &= ~FDI_RX_SYMBOL_LOCK;
3119 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3120 I915_WRITE(reg, temp);
3121
3122 POSTING_READ(reg);
e1a44743
AJ
3123 udelay(150);
3124
8db9d77b 3125 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3126 reg = FDI_TX_CTL(pipe);
3127 temp = I915_READ(reg);
627eb5a3
DV
3128 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3129 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
3130 temp &= ~FDI_LINK_TRAIN_NONE;
3131 temp |= FDI_LINK_TRAIN_PATTERN_1;
3132 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3133 /* SNB-B */
3134 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3135 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3136
d74cf324
DV
3137 I915_WRITE(FDI_RX_MISC(pipe),
3138 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3139
5eddb70b
CW
3140 reg = FDI_RX_CTL(pipe);
3141 temp = I915_READ(reg);
8db9d77b
ZW
3142 if (HAS_PCH_CPT(dev)) {
3143 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3144 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3145 } else {
3146 temp &= ~FDI_LINK_TRAIN_NONE;
3147 temp |= FDI_LINK_TRAIN_PATTERN_1;
3148 }
5eddb70b
CW
3149 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3150
3151 POSTING_READ(reg);
8db9d77b
ZW
3152 udelay(150);
3153
0206e353 3154 for (i = 0; i < 4; i++) {
5eddb70b
CW
3155 reg = FDI_TX_CTL(pipe);
3156 temp = I915_READ(reg);
8db9d77b
ZW
3157 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3158 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3159 I915_WRITE(reg, temp);
3160
3161 POSTING_READ(reg);
8db9d77b
ZW
3162 udelay(500);
3163
fa37d39e
SP
3164 for (retry = 0; retry < 5; retry++) {
3165 reg = FDI_RX_IIR(pipe);
3166 temp = I915_READ(reg);
3167 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3168 if (temp & FDI_RX_BIT_LOCK) {
3169 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3170 DRM_DEBUG_KMS("FDI train 1 done.\n");
3171 break;
3172 }
3173 udelay(50);
8db9d77b 3174 }
fa37d39e
SP
3175 if (retry < 5)
3176 break;
8db9d77b
ZW
3177 }
3178 if (i == 4)
5eddb70b 3179 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3180
3181 /* Train 2 */
5eddb70b
CW
3182 reg = FDI_TX_CTL(pipe);
3183 temp = I915_READ(reg);
8db9d77b
ZW
3184 temp &= ~FDI_LINK_TRAIN_NONE;
3185 temp |= FDI_LINK_TRAIN_PATTERN_2;
3186 if (IS_GEN6(dev)) {
3187 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3188 /* SNB-B */
3189 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3190 }
5eddb70b 3191 I915_WRITE(reg, temp);
8db9d77b 3192
5eddb70b
CW
3193 reg = FDI_RX_CTL(pipe);
3194 temp = I915_READ(reg);
8db9d77b
ZW
3195 if (HAS_PCH_CPT(dev)) {
3196 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3197 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3198 } else {
3199 temp &= ~FDI_LINK_TRAIN_NONE;
3200 temp |= FDI_LINK_TRAIN_PATTERN_2;
3201 }
5eddb70b
CW
3202 I915_WRITE(reg, temp);
3203
3204 POSTING_READ(reg);
8db9d77b
ZW
3205 udelay(150);
3206
0206e353 3207 for (i = 0; i < 4; i++) {
5eddb70b
CW
3208 reg = FDI_TX_CTL(pipe);
3209 temp = I915_READ(reg);
8db9d77b
ZW
3210 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3211 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3212 I915_WRITE(reg, temp);
3213
3214 POSTING_READ(reg);
8db9d77b
ZW
3215 udelay(500);
3216
fa37d39e
SP
3217 for (retry = 0; retry < 5; retry++) {
3218 reg = FDI_RX_IIR(pipe);
3219 temp = I915_READ(reg);
3220 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3221 if (temp & FDI_RX_SYMBOL_LOCK) {
3222 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3223 DRM_DEBUG_KMS("FDI train 2 done.\n");
3224 break;
3225 }
3226 udelay(50);
8db9d77b 3227 }
fa37d39e
SP
3228 if (retry < 5)
3229 break;
8db9d77b
ZW
3230 }
3231 if (i == 4)
5eddb70b 3232 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3233
3234 DRM_DEBUG_KMS("FDI train done.\n");
3235}
3236
357555c0
JB
3237/* Manual link training for Ivy Bridge A0 parts */
3238static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3239{
3240 struct drm_device *dev = crtc->dev;
3241 struct drm_i915_private *dev_priv = dev->dev_private;
3242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3243 int pipe = intel_crtc->pipe;
139ccd3f 3244 u32 reg, temp, i, j;
357555c0
JB
3245
3246 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3247 for train result */
3248 reg = FDI_RX_IMR(pipe);
3249 temp = I915_READ(reg);
3250 temp &= ~FDI_RX_SYMBOL_LOCK;
3251 temp &= ~FDI_RX_BIT_LOCK;
3252 I915_WRITE(reg, temp);
3253
3254 POSTING_READ(reg);
3255 udelay(150);
3256
01a415fd
DV
3257 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3258 I915_READ(FDI_RX_IIR(pipe)));
3259
139ccd3f
JB
3260 /* Try each vswing and preemphasis setting twice before moving on */
3261 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3262 /* disable first in case we need to retry */
3263 reg = FDI_TX_CTL(pipe);
3264 temp = I915_READ(reg);
3265 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3266 temp &= ~FDI_TX_ENABLE;
3267 I915_WRITE(reg, temp);
357555c0 3268
139ccd3f
JB
3269 reg = FDI_RX_CTL(pipe);
3270 temp = I915_READ(reg);
3271 temp &= ~FDI_LINK_TRAIN_AUTO;
3272 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3273 temp &= ~FDI_RX_ENABLE;
3274 I915_WRITE(reg, temp);
357555c0 3275
139ccd3f 3276 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3277 reg = FDI_TX_CTL(pipe);
3278 temp = I915_READ(reg);
139ccd3f
JB
3279 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3280 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3281 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3282 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3283 temp |= snb_b_fdi_train_param[j/2];
3284 temp |= FDI_COMPOSITE_SYNC;
3285 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3286
139ccd3f
JB
3287 I915_WRITE(FDI_RX_MISC(pipe),
3288 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3289
139ccd3f 3290 reg = FDI_RX_CTL(pipe);
357555c0 3291 temp = I915_READ(reg);
139ccd3f
JB
3292 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3293 temp |= FDI_COMPOSITE_SYNC;
3294 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3295
139ccd3f
JB
3296 POSTING_READ(reg);
3297 udelay(1); /* should be 0.5us */
357555c0 3298
139ccd3f
JB
3299 for (i = 0; i < 4; i++) {
3300 reg = FDI_RX_IIR(pipe);
3301 temp = I915_READ(reg);
3302 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3303
139ccd3f
JB
3304 if (temp & FDI_RX_BIT_LOCK ||
3305 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3306 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3307 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3308 i);
3309 break;
3310 }
3311 udelay(1); /* should be 0.5us */
3312 }
3313 if (i == 4) {
3314 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3315 continue;
3316 }
357555c0 3317
139ccd3f 3318 /* Train 2 */
357555c0
JB
3319 reg = FDI_TX_CTL(pipe);
3320 temp = I915_READ(reg);
139ccd3f
JB
3321 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3322 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3323 I915_WRITE(reg, temp);
3324
3325 reg = FDI_RX_CTL(pipe);
3326 temp = I915_READ(reg);
3327 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3328 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3329 I915_WRITE(reg, temp);
3330
3331 POSTING_READ(reg);
139ccd3f 3332 udelay(2); /* should be 1.5us */
357555c0 3333
139ccd3f
JB
3334 for (i = 0; i < 4; i++) {
3335 reg = FDI_RX_IIR(pipe);
3336 temp = I915_READ(reg);
3337 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3338
139ccd3f
JB
3339 if (temp & FDI_RX_SYMBOL_LOCK ||
3340 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3341 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3342 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3343 i);
3344 goto train_done;
3345 }
3346 udelay(2); /* should be 1.5us */
357555c0 3347 }
139ccd3f
JB
3348 if (i == 4)
3349 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3350 }
357555c0 3351
139ccd3f 3352train_done:
357555c0
JB
3353 DRM_DEBUG_KMS("FDI train done.\n");
3354}
3355
88cefb6c 3356static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3357{
88cefb6c 3358 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3359 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3360 int pipe = intel_crtc->pipe;
5eddb70b 3361 u32 reg, temp;
79e53945 3362
c64e311e 3363
c98e9dcf 3364 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
627eb5a3
DV
3367 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3368 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3369 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3370 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3371
3372 POSTING_READ(reg);
c98e9dcf
JB
3373 udelay(200);
3374
3375 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3376 temp = I915_READ(reg);
3377 I915_WRITE(reg, temp | FDI_PCDCLK);
3378
3379 POSTING_READ(reg);
c98e9dcf
JB
3380 udelay(200);
3381
20749730
PZ
3382 /* Enable CPU FDI TX PLL, always on for Ironlake */
3383 reg = FDI_TX_CTL(pipe);
3384 temp = I915_READ(reg);
3385 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3386 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3387
20749730
PZ
3388 POSTING_READ(reg);
3389 udelay(100);
6be4a607 3390 }
0e23b99d
JB
3391}
3392
88cefb6c
DV
3393static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3394{
3395 struct drm_device *dev = intel_crtc->base.dev;
3396 struct drm_i915_private *dev_priv = dev->dev_private;
3397 int pipe = intel_crtc->pipe;
3398 u32 reg, temp;
3399
3400 /* Switch from PCDclk to Rawclk */
3401 reg = FDI_RX_CTL(pipe);
3402 temp = I915_READ(reg);
3403 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3404
3405 /* Disable CPU FDI TX PLL */
3406 reg = FDI_TX_CTL(pipe);
3407 temp = I915_READ(reg);
3408 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3409
3410 POSTING_READ(reg);
3411 udelay(100);
3412
3413 reg = FDI_RX_CTL(pipe);
3414 temp = I915_READ(reg);
3415 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3416
3417 /* Wait for the clocks to turn off. */
3418 POSTING_READ(reg);
3419 udelay(100);
3420}
3421
0fc932b8
JB
3422static void ironlake_fdi_disable(struct drm_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3427 int pipe = intel_crtc->pipe;
3428 u32 reg, temp;
3429
3430 /* disable CPU FDI tx and PCH FDI rx */
3431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
3433 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3434 POSTING_READ(reg);
3435
3436 reg = FDI_RX_CTL(pipe);
3437 temp = I915_READ(reg);
3438 temp &= ~(0x7 << 16);
dfd07d72 3439 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3440 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3441
3442 POSTING_READ(reg);
3443 udelay(100);
3444
3445 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3446 if (HAS_PCH_IBX(dev))
6f06ce18 3447 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3448
3449 /* still set train pattern 1 */
3450 reg = FDI_TX_CTL(pipe);
3451 temp = I915_READ(reg);
3452 temp &= ~FDI_LINK_TRAIN_NONE;
3453 temp |= FDI_LINK_TRAIN_PATTERN_1;
3454 I915_WRITE(reg, temp);
3455
3456 reg = FDI_RX_CTL(pipe);
3457 temp = I915_READ(reg);
3458 if (HAS_PCH_CPT(dev)) {
3459 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3460 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3461 } else {
3462 temp &= ~FDI_LINK_TRAIN_NONE;
3463 temp |= FDI_LINK_TRAIN_PATTERN_1;
3464 }
3465 /* BPC in FDI rx is consistent with that in PIPECONF */
3466 temp &= ~(0x07 << 16);
dfd07d72 3467 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3468 I915_WRITE(reg, temp);
3469
3470 POSTING_READ(reg);
3471 udelay(100);
3472}
3473
5dce5b93
CW
3474bool intel_has_pending_fb_unpin(struct drm_device *dev)
3475{
3476 struct intel_crtc *crtc;
3477
3478 /* Note that we don't need to be called with mode_config.lock here
3479 * as our list of CRTC objects is static for the lifetime of the
3480 * device and so cannot disappear as we iterate. Similarly, we can
3481 * happily treat the predicates as racy, atomic checks as userspace
3482 * cannot claim and pin a new fb without at least acquring the
3483 * struct_mutex and so serialising with us.
3484 */
d3fcc808 3485 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3486 if (atomic_read(&crtc->unpin_work_count) == 0)
3487 continue;
3488
3489 if (crtc->unpin_work)
3490 intel_wait_for_vblank(dev, crtc->pipe);
3491
3492 return true;
3493 }
3494
3495 return false;
3496}
3497
d6bbafa1
CW
3498static void page_flip_completed(struct intel_crtc *intel_crtc)
3499{
3500 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3501 struct intel_unpin_work *work = intel_crtc->unpin_work;
3502
3503 /* ensure that the unpin work is consistent wrt ->pending. */
3504 smp_rmb();
3505 intel_crtc->unpin_work = NULL;
3506
3507 if (work->event)
3508 drm_send_vblank_event(intel_crtc->base.dev,
3509 intel_crtc->pipe,
3510 work->event);
3511
3512 drm_crtc_vblank_put(&intel_crtc->base);
3513
3514 wake_up_all(&dev_priv->pending_flip_queue);
3515 queue_work(dev_priv->wq, &work->work);
3516
3517 trace_i915_flip_complete(intel_crtc->plane,
3518 work->pending_flip_obj);
3519}
3520
46a55d30 3521void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3522{
0f91128d 3523 struct drm_device *dev = crtc->dev;
5bb61643 3524 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3525
2c10d571 3526 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3527 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3528 !intel_crtc_has_pending_flip(crtc),
3529 60*HZ) == 0)) {
3530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3531 unsigned long flags;
2c10d571 3532
9c787942
CW
3533 spin_lock_irqsave(&dev->event_lock, flags);
3534 if (intel_crtc->unpin_work) {
3535 WARN_ONCE(1, "Removing stuck page flip\n");
3536 page_flip_completed(intel_crtc);
3537 }
3538 spin_unlock_irqrestore(&dev->event_lock, flags);
3539 }
5bb61643 3540
975d568a
CW
3541 if (crtc->primary->fb) {
3542 mutex_lock(&dev->struct_mutex);
3543 intel_finish_fb(crtc->primary->fb);
3544 mutex_unlock(&dev->struct_mutex);
3545 }
e6c3a2a6
CW
3546}
3547
e615efe4
ED
3548/* Program iCLKIP clock to the desired frequency */
3549static void lpt_program_iclkip(struct drm_crtc *crtc)
3550{
3551 struct drm_device *dev = crtc->dev;
3552 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3553 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3554 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3555 u32 temp;
3556
09153000
DV
3557 mutex_lock(&dev_priv->dpio_lock);
3558
e615efe4
ED
3559 /* It is necessary to ungate the pixclk gate prior to programming
3560 * the divisors, and gate it back when it is done.
3561 */
3562 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3563
3564 /* Disable SSCCTL */
3565 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3566 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3567 SBI_SSCCTL_DISABLE,
3568 SBI_ICLK);
e615efe4
ED
3569
3570 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3571 if (clock == 20000) {
e615efe4
ED
3572 auxdiv = 1;
3573 divsel = 0x41;
3574 phaseinc = 0x20;
3575 } else {
3576 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3577 * but the adjusted_mode->crtc_clock in in KHz. To get the
3578 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3579 * convert the virtual clock precision to KHz here for higher
3580 * precision.
3581 */
3582 u32 iclk_virtual_root_freq = 172800 * 1000;
3583 u32 iclk_pi_range = 64;
3584 u32 desired_divisor, msb_divisor_value, pi_value;
3585
12d7ceed 3586 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3587 msb_divisor_value = desired_divisor / iclk_pi_range;
3588 pi_value = desired_divisor % iclk_pi_range;
3589
3590 auxdiv = 0;
3591 divsel = msb_divisor_value - 2;
3592 phaseinc = pi_value;
3593 }
3594
3595 /* This should not happen with any sane values */
3596 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3597 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3598 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3599 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3600
3601 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3602 clock,
e615efe4
ED
3603 auxdiv,
3604 divsel,
3605 phasedir,
3606 phaseinc);
3607
3608 /* Program SSCDIVINTPHASE6 */
988d6ee8 3609 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3610 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3611 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3612 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3613 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3614 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3615 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3616 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3617
3618 /* Program SSCAUXDIV */
988d6ee8 3619 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3620 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3621 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3622 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3623
3624 /* Enable modulator and associated divider */
988d6ee8 3625 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3626 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3627 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3628
3629 /* Wait for initialization time */
3630 udelay(24);
3631
3632 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3633
3634 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3635}
3636
275f01b2
DV
3637static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3638 enum pipe pch_transcoder)
3639{
3640 struct drm_device *dev = crtc->base.dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3643
3644 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3645 I915_READ(HTOTAL(cpu_transcoder)));
3646 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3647 I915_READ(HBLANK(cpu_transcoder)));
3648 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3649 I915_READ(HSYNC(cpu_transcoder)));
3650
3651 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3652 I915_READ(VTOTAL(cpu_transcoder)));
3653 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3654 I915_READ(VBLANK(cpu_transcoder)));
3655 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3656 I915_READ(VSYNC(cpu_transcoder)));
3657 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3658 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3659}
3660
1fbc0d78
DV
3661static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3662{
3663 struct drm_i915_private *dev_priv = dev->dev_private;
3664 uint32_t temp;
3665
3666 temp = I915_READ(SOUTH_CHICKEN1);
3667 if (temp & FDI_BC_BIFURCATION_SELECT)
3668 return;
3669
3670 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3671 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3672
3673 temp |= FDI_BC_BIFURCATION_SELECT;
3674 DRM_DEBUG_KMS("enabling fdi C rx\n");
3675 I915_WRITE(SOUTH_CHICKEN1, temp);
3676 POSTING_READ(SOUTH_CHICKEN1);
3677}
3678
3679static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3680{
3681 struct drm_device *dev = intel_crtc->base.dev;
3682 struct drm_i915_private *dev_priv = dev->dev_private;
3683
3684 switch (intel_crtc->pipe) {
3685 case PIPE_A:
3686 break;
3687 case PIPE_B:
3688 if (intel_crtc->config.fdi_lanes > 2)
3689 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3690 else
3691 cpt_enable_fdi_bc_bifurcation(dev);
3692
3693 break;
3694 case PIPE_C:
3695 cpt_enable_fdi_bc_bifurcation(dev);
3696
3697 break;
3698 default:
3699 BUG();
3700 }
3701}
3702
f67a559d
JB
3703/*
3704 * Enable PCH resources required for PCH ports:
3705 * - PCH PLLs
3706 * - FDI training & RX/TX
3707 * - update transcoder timings
3708 * - DP transcoding bits
3709 * - transcoder
3710 */
3711static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3712{
3713 struct drm_device *dev = crtc->dev;
3714 struct drm_i915_private *dev_priv = dev->dev_private;
3715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3716 int pipe = intel_crtc->pipe;
ee7b9f93 3717 u32 reg, temp;
2c07245f 3718
ab9412ba 3719 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3720
1fbc0d78
DV
3721 if (IS_IVYBRIDGE(dev))
3722 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3723
cd986abb
DV
3724 /* Write the TU size bits before fdi link training, so that error
3725 * detection works. */
3726 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3727 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3728
c98e9dcf 3729 /* For PCH output, training FDI link */
674cf967 3730 dev_priv->display.fdi_link_train(crtc);
2c07245f 3731
3ad8a208
DV
3732 /* We need to program the right clock selection before writing the pixel
3733 * mutliplier into the DPLL. */
303b81e0 3734 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3735 u32 sel;
4b645f14 3736
c98e9dcf 3737 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3738 temp |= TRANS_DPLL_ENABLE(pipe);
3739 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3740 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3741 temp |= sel;
3742 else
3743 temp &= ~sel;
c98e9dcf 3744 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3745 }
5eddb70b 3746
3ad8a208
DV
3747 /* XXX: pch pll's can be enabled any time before we enable the PCH
3748 * transcoder, and we actually should do this to not upset any PCH
3749 * transcoder that already use the clock when we share it.
3750 *
3751 * Note that enable_shared_dpll tries to do the right thing, but
3752 * get_shared_dpll unconditionally resets the pll - we need that to have
3753 * the right LVDS enable sequence. */
85b3894f 3754 intel_enable_shared_dpll(intel_crtc);
3ad8a208 3755
d9b6cb56
JB
3756 /* set transcoder timing, panel must allow it */
3757 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3758 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3759
303b81e0 3760 intel_fdi_normal_train(crtc);
5e84e1a4 3761
c98e9dcf
JB
3762 /* For PCH DP, enable TRANS_DP_CTL */
3763 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3764 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3765 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3766 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3767 reg = TRANS_DP_CTL(pipe);
3768 temp = I915_READ(reg);
3769 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3770 TRANS_DP_SYNC_MASK |
3771 TRANS_DP_BPC_MASK);
5eddb70b
CW
3772 temp |= (TRANS_DP_OUTPUT_ENABLE |
3773 TRANS_DP_ENH_FRAMING);
9325c9f0 3774 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3775
3776 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3777 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3778 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3779 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3780
3781 switch (intel_trans_dp_port_sel(crtc)) {
3782 case PCH_DP_B:
5eddb70b 3783 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3784 break;
3785 case PCH_DP_C:
5eddb70b 3786 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3787 break;
3788 case PCH_DP_D:
5eddb70b 3789 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3790 break;
3791 default:
e95d41e1 3792 BUG();
32f9d658 3793 }
2c07245f 3794
5eddb70b 3795 I915_WRITE(reg, temp);
6be4a607 3796 }
b52eb4dc 3797
b8a4f404 3798 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3799}
3800
1507e5bd
PZ
3801static void lpt_pch_enable(struct drm_crtc *crtc)
3802{
3803 struct drm_device *dev = crtc->dev;
3804 struct drm_i915_private *dev_priv = dev->dev_private;
3805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3806 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3807
ab9412ba 3808 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3809
8c52b5e8 3810 lpt_program_iclkip(crtc);
1507e5bd 3811
0540e488 3812 /* Set transcoder timing. */
275f01b2 3813 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3814
937bb610 3815 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3816}
3817
716c2e55 3818void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3819{
e2b78267 3820 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3821
3822 if (pll == NULL)
3823 return;
3824
3825 if (pll->refcount == 0) {
46edb027 3826 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3827 return;
3828 }
3829
f4a091c7
DV
3830 if (--pll->refcount == 0) {
3831 WARN_ON(pll->on);
3832 WARN_ON(pll->active);
3833 }
3834
a43f6e0f 3835 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3836}
3837
716c2e55 3838struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3839{
e2b78267
DV
3840 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3841 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3842 enum intel_dpll_id i;
ee7b9f93 3843
ee7b9f93 3844 if (pll) {
46edb027
DV
3845 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3846 crtc->base.base.id, pll->name);
e2b78267 3847 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3848 }
3849
98b6bd99
DV
3850 if (HAS_PCH_IBX(dev_priv->dev)) {
3851 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3852 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3853 pll = &dev_priv->shared_dplls[i];
98b6bd99 3854
46edb027
DV
3855 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3856 crtc->base.base.id, pll->name);
98b6bd99 3857
f2a69f44
DV
3858 WARN_ON(pll->refcount);
3859
98b6bd99
DV
3860 goto found;
3861 }
3862
e72f9fbf
DV
3863 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3864 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3865
3866 /* Only want to check enabled timings first */
3867 if (pll->refcount == 0)
3868 continue;
3869
b89a1d39
DV
3870 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3871 sizeof(pll->hw_state)) == 0) {
46edb027 3872 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3873 crtc->base.base.id,
46edb027 3874 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3875
3876 goto found;
3877 }
3878 }
3879
3880 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3881 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3882 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3883 if (pll->refcount == 0) {
46edb027
DV
3884 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3885 crtc->base.base.id, pll->name);
ee7b9f93
JB
3886 goto found;
3887 }
3888 }
3889
3890 return NULL;
3891
3892found:
f2a69f44
DV
3893 if (pll->refcount == 0)
3894 pll->hw_state = crtc->config.dpll_hw_state;
3895
a43f6e0f 3896 crtc->config.shared_dpll = i;
46edb027
DV
3897 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3898 pipe_name(crtc->pipe));
ee7b9f93 3899
cdbd2316 3900 pll->refcount++;
e04c7350 3901
ee7b9f93
JB
3902 return pll;
3903}
3904
a1520318 3905static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3906{
3907 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3908 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3909 u32 temp;
3910
3911 temp = I915_READ(dslreg);
3912 udelay(500);
3913 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3914 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3915 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3916 }
3917}
3918
b074cec8
JB
3919static void ironlake_pfit_enable(struct intel_crtc *crtc)
3920{
3921 struct drm_device *dev = crtc->base.dev;
3922 struct drm_i915_private *dev_priv = dev->dev_private;
3923 int pipe = crtc->pipe;
3924
fd4daa9c 3925 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3926 /* Force use of hard-coded filter coefficients
3927 * as some pre-programmed values are broken,
3928 * e.g. x201.
3929 */
3930 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3931 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3932 PF_PIPE_SEL_IVB(pipe));
3933 else
3934 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3935 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3936 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3937 }
3938}
3939
bb53d4ae
VS
3940static void intel_enable_planes(struct drm_crtc *crtc)
3941{
3942 struct drm_device *dev = crtc->dev;
3943 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3944 struct drm_plane *plane;
bb53d4ae
VS
3945 struct intel_plane *intel_plane;
3946
af2b653b
MR
3947 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3948 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3949 if (intel_plane->pipe == pipe)
3950 intel_plane_restore(&intel_plane->base);
af2b653b 3951 }
bb53d4ae
VS
3952}
3953
3954static void intel_disable_planes(struct drm_crtc *crtc)
3955{
3956 struct drm_device *dev = crtc->dev;
3957 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3958 struct drm_plane *plane;
bb53d4ae
VS
3959 struct intel_plane *intel_plane;
3960
af2b653b
MR
3961 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3962 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3963 if (intel_plane->pipe == pipe)
3964 intel_plane_disable(&intel_plane->base);
af2b653b 3965 }
bb53d4ae
VS
3966}
3967
20bc8673 3968void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 3969{
cea165c3
VS
3970 struct drm_device *dev = crtc->base.dev;
3971 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531
PZ
3972
3973 if (!crtc->config.ips_enabled)
3974 return;
3975
cea165c3
VS
3976 /* We can only enable IPS after we enable a plane and wait for a vblank */
3977 intel_wait_for_vblank(dev, crtc->pipe);
3978
d77e4531 3979 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 3980 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3981 mutex_lock(&dev_priv->rps.hw_lock);
3982 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3983 mutex_unlock(&dev_priv->rps.hw_lock);
3984 /* Quoting Art Runyan: "its not safe to expect any particular
3985 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3986 * mailbox." Moreover, the mailbox may return a bogus state,
3987 * so we need to just enable it and continue on.
2a114cc1
BW
3988 */
3989 } else {
3990 I915_WRITE(IPS_CTL, IPS_ENABLE);
3991 /* The bit only becomes 1 in the next vblank, so this wait here
3992 * is essentially intel_wait_for_vblank. If we don't have this
3993 * and don't wait for vblanks until the end of crtc_enable, then
3994 * the HW state readout code will complain that the expected
3995 * IPS_CTL value is not the one we read. */
3996 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3997 DRM_ERROR("Timed out waiting for IPS enable\n");
3998 }
d77e4531
PZ
3999}
4000
20bc8673 4001void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4002{
4003 struct drm_device *dev = crtc->base.dev;
4004 struct drm_i915_private *dev_priv = dev->dev_private;
4005
4006 if (!crtc->config.ips_enabled)
4007 return;
4008
4009 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4010 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4011 mutex_lock(&dev_priv->rps.hw_lock);
4012 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4013 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4014 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4015 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4016 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4017 } else {
2a114cc1 4018 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4019 POSTING_READ(IPS_CTL);
4020 }
d77e4531
PZ
4021
4022 /* We need to wait for a vblank before we can disable the plane. */
4023 intel_wait_for_vblank(dev, crtc->pipe);
4024}
4025
4026/** Loads the palette/gamma unit for the CRTC with the prepared values */
4027static void intel_crtc_load_lut(struct drm_crtc *crtc)
4028{
4029 struct drm_device *dev = crtc->dev;
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4032 enum pipe pipe = intel_crtc->pipe;
4033 int palreg = PALETTE(pipe);
4034 int i;
4035 bool reenable_ips = false;
4036
4037 /* The clocks have to be on to load the palette. */
4038 if (!crtc->enabled || !intel_crtc->active)
4039 return;
4040
4041 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
4042 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4043 assert_dsi_pll_enabled(dev_priv);
4044 else
4045 assert_pll_enabled(dev_priv, pipe);
4046 }
4047
4048 /* use legacy palette for Ironlake */
7a1db49a 4049 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4050 palreg = LGC_PALETTE(pipe);
4051
4052 /* Workaround : Do not read or write the pipe palette/gamma data while
4053 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4054 */
41e6fc4c 4055 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
4056 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4057 GAMMA_MODE_MODE_SPLIT)) {
4058 hsw_disable_ips(intel_crtc);
4059 reenable_ips = true;
4060 }
4061
4062 for (i = 0; i < 256; i++) {
4063 I915_WRITE(palreg + 4 * i,
4064 (intel_crtc->lut_r[i] << 16) |
4065 (intel_crtc->lut_g[i] << 8) |
4066 intel_crtc->lut_b[i]);
4067 }
4068
4069 if (reenable_ips)
4070 hsw_enable_ips(intel_crtc);
4071}
4072
d3eedb1a
VS
4073static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4074{
4075 if (!enable && intel_crtc->overlay) {
4076 struct drm_device *dev = intel_crtc->base.dev;
4077 struct drm_i915_private *dev_priv = dev->dev_private;
4078
4079 mutex_lock(&dev->struct_mutex);
4080 dev_priv->mm.interruptible = false;
4081 (void) intel_overlay_switch_off(intel_crtc->overlay);
4082 dev_priv->mm.interruptible = true;
4083 mutex_unlock(&dev->struct_mutex);
4084 }
4085
4086 /* Let userspace switch the overlay on again. In most cases userspace
4087 * has to recompute where to put it anyway.
4088 */
4089}
4090
d3eedb1a 4091static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4092{
4093 struct drm_device *dev = crtc->dev;
a5c4d7bc
VS
4094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4095 int pipe = intel_crtc->pipe;
a5c4d7bc 4096
08c71e5e
VS
4097 assert_vblank_disabled(crtc);
4098
f98551ae
VS
4099 drm_vblank_on(dev, pipe);
4100
fdd508a6 4101 intel_enable_primary_hw_plane(crtc->primary, crtc);
a5c4d7bc
VS
4102 intel_enable_planes(crtc);
4103 intel_crtc_update_cursor(crtc, true);
d3eedb1a 4104 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
4105
4106 hsw_enable_ips(intel_crtc);
4107
4108 mutex_lock(&dev->struct_mutex);
4109 intel_update_fbc(dev);
4110 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4111
4112 /*
4113 * FIXME: Once we grow proper nuclear flip support out of this we need
4114 * to compute the mask of flip planes precisely. For the time being
4115 * consider this a flip from a NULL plane.
4116 */
4117 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4118}
4119
d3eedb1a 4120static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
4121{
4122 struct drm_device *dev = crtc->dev;
4123 struct drm_i915_private *dev_priv = dev->dev_private;
4124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4125 int pipe = intel_crtc->pipe;
4126 int plane = intel_crtc->plane;
4127
4128 intel_crtc_wait_for_pending_flips(crtc);
a5c4d7bc
VS
4129
4130 if (dev_priv->fbc.plane == plane)
4131 intel_disable_fbc(dev);
4132
4133 hsw_disable_ips(intel_crtc);
4134
d3eedb1a 4135 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
4136 intel_crtc_update_cursor(crtc, false);
4137 intel_disable_planes(crtc);
fdd508a6 4138 intel_disable_primary_hw_plane(crtc->primary, crtc);
f98551ae 4139
f99d7069
DV
4140 /*
4141 * FIXME: Once we grow proper nuclear flip support out of this we need
4142 * to compute the mask of flip planes precisely. For the time being
4143 * consider this a flip to a NULL plane.
4144 */
4145 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4146
f98551ae 4147 drm_vblank_off(dev, pipe);
08c71e5e
VS
4148
4149 assert_vblank_disabled(crtc);
a5c4d7bc
VS
4150}
4151
f67a559d
JB
4152static void ironlake_crtc_enable(struct drm_crtc *crtc)
4153{
4154 struct drm_device *dev = crtc->dev;
4155 struct drm_i915_private *dev_priv = dev->dev_private;
4156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4157 struct intel_encoder *encoder;
f67a559d 4158 int pipe = intel_crtc->pipe;
f67a559d 4159
08a48469
DV
4160 WARN_ON(!crtc->enabled);
4161
f67a559d
JB
4162 if (intel_crtc->active)
4163 return;
4164
b14b1055
DV
4165 if (intel_crtc->config.has_pch_encoder)
4166 intel_prepare_shared_dpll(intel_crtc);
4167
29407aab
DV
4168 if (intel_crtc->config.has_dp_encoder)
4169 intel_dp_set_m_n(intel_crtc);
4170
4171 intel_set_pipe_timings(intel_crtc);
4172
4173 if (intel_crtc->config.has_pch_encoder) {
4174 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4175 &intel_crtc->config.fdi_m_n, NULL);
29407aab
DV
4176 }
4177
4178 ironlake_set_pipeconf(crtc);
4179
f67a559d 4180 intel_crtc->active = true;
8664281b
PZ
4181
4182 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4183 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4184
f6736a1a 4185 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4186 if (encoder->pre_enable)
4187 encoder->pre_enable(encoder);
f67a559d 4188
5bfe2ac0 4189 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
4190 /* Note: FDI PLL enabling _must_ be done before we enable the
4191 * cpu pipes, hence this is separate from all the other fdi/pch
4192 * enabling. */
88cefb6c 4193 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4194 } else {
4195 assert_fdi_tx_disabled(dev_priv, pipe);
4196 assert_fdi_rx_disabled(dev_priv, pipe);
4197 }
f67a559d 4198
b074cec8 4199 ironlake_pfit_enable(intel_crtc);
f67a559d 4200
9c54c0dd
JB
4201 /*
4202 * On ILK+ LUT must be loaded before the pipe is running but with
4203 * clocks enabled
4204 */
4205 intel_crtc_load_lut(crtc);
4206
f37fcc2a 4207 intel_update_watermarks(crtc);
e1fdc473 4208 intel_enable_pipe(intel_crtc);
f67a559d 4209
5bfe2ac0 4210 if (intel_crtc->config.has_pch_encoder)
f67a559d 4211 ironlake_pch_enable(crtc);
c98e9dcf 4212
fa5c73b1
DV
4213 for_each_encoder_on_crtc(dev, crtc, encoder)
4214 encoder->enable(encoder);
61b77ddd
DV
4215
4216 if (HAS_PCH_CPT(dev))
a1520318 4217 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 4218
d3eedb1a 4219 intel_crtc_enable_planes(crtc);
6be4a607
JB
4220}
4221
42db64ef
PZ
4222/* IPS only exists on ULT machines and is tied to pipe A. */
4223static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4224{
f5adf94e 4225 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4226}
4227
e4916946
PZ
4228/*
4229 * This implements the workaround described in the "notes" section of the mode
4230 * set sequence documentation. When going from no pipes or single pipe to
4231 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4232 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4233 */
4234static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4235{
4236 struct drm_device *dev = crtc->base.dev;
4237 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4238
4239 /* We want to get the other_active_crtc only if there's only 1 other
4240 * active crtc. */
d3fcc808 4241 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4242 if (!crtc_it->active || crtc_it == crtc)
4243 continue;
4244
4245 if (other_active_crtc)
4246 return;
4247
4248 other_active_crtc = crtc_it;
4249 }
4250 if (!other_active_crtc)
4251 return;
4252
4253 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4254 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4255}
4256
4f771f10
PZ
4257static void haswell_crtc_enable(struct drm_crtc *crtc)
4258{
4259 struct drm_device *dev = crtc->dev;
4260 struct drm_i915_private *dev_priv = dev->dev_private;
4261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4262 struct intel_encoder *encoder;
4263 int pipe = intel_crtc->pipe;
4f771f10
PZ
4264
4265 WARN_ON(!crtc->enabled);
4266
4267 if (intel_crtc->active)
4268 return;
4269
df8ad70c
DV
4270 if (intel_crtc_to_shared_dpll(intel_crtc))
4271 intel_enable_shared_dpll(intel_crtc);
4272
229fca97
DV
4273 if (intel_crtc->config.has_dp_encoder)
4274 intel_dp_set_m_n(intel_crtc);
4275
4276 intel_set_pipe_timings(intel_crtc);
4277
4278 if (intel_crtc->config.has_pch_encoder) {
4279 intel_cpu_transcoder_set_m_n(intel_crtc,
f769cd24 4280 &intel_crtc->config.fdi_m_n, NULL);
229fca97
DV
4281 }
4282
4283 haswell_set_pipeconf(crtc);
4284
4285 intel_set_pipe_csc(crtc);
4286
4f771f10 4287 intel_crtc->active = true;
8664281b
PZ
4288
4289 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4f771f10
PZ
4290 for_each_encoder_on_crtc(dev, crtc, encoder)
4291 if (encoder->pre_enable)
4292 encoder->pre_enable(encoder);
4293
4fe9467d
ID
4294 if (intel_crtc->config.has_pch_encoder) {
4295 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4296 dev_priv->display.fdi_link_train(crtc);
4297 }
4298
1f544388 4299 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4300
b074cec8 4301 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
4302
4303 /*
4304 * On ILK+ LUT must be loaded before the pipe is running but with
4305 * clocks enabled
4306 */
4307 intel_crtc_load_lut(crtc);
4308
1f544388 4309 intel_ddi_set_pipe_settings(crtc);
8228c251 4310 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4311
f37fcc2a 4312 intel_update_watermarks(crtc);
e1fdc473 4313 intel_enable_pipe(intel_crtc);
42db64ef 4314
5bfe2ac0 4315 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4316 lpt_pch_enable(crtc);
4f771f10 4317
0e32b39c
DA
4318 if (intel_crtc->config.dp_encoder_is_mst)
4319 intel_ddi_set_vc_payload_alloc(crtc, true);
4320
8807e55b 4321 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4322 encoder->enable(encoder);
8807e55b
JN
4323 intel_opregion_notify_encoder(encoder, true);
4324 }
4f771f10 4325
e4916946
PZ
4326 /* If we change the relative order between pipe/planes enabling, we need
4327 * to change the workaround. */
4328 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4329 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4330}
4331
3f8dce3a
DV
4332static void ironlake_pfit_disable(struct intel_crtc *crtc)
4333{
4334 struct drm_device *dev = crtc->base.dev;
4335 struct drm_i915_private *dev_priv = dev->dev_private;
4336 int pipe = crtc->pipe;
4337
4338 /* To avoid upsetting the power well on haswell only disable the pfit if
4339 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4340 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4341 I915_WRITE(PF_CTL(pipe), 0);
4342 I915_WRITE(PF_WIN_POS(pipe), 0);
4343 I915_WRITE(PF_WIN_SZ(pipe), 0);
4344 }
4345}
4346
6be4a607
JB
4347static void ironlake_crtc_disable(struct drm_crtc *crtc)
4348{
4349 struct drm_device *dev = crtc->dev;
4350 struct drm_i915_private *dev_priv = dev->dev_private;
4351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4352 struct intel_encoder *encoder;
6be4a607 4353 int pipe = intel_crtc->pipe;
5eddb70b 4354 u32 reg, temp;
b52eb4dc 4355
f7abfe8b
CW
4356 if (!intel_crtc->active)
4357 return;
4358
d3eedb1a 4359 intel_crtc_disable_planes(crtc);
a5c4d7bc 4360
ea9d758d
DV
4361 for_each_encoder_on_crtc(dev, crtc, encoder)
4362 encoder->disable(encoder);
4363
d925c59a
DV
4364 if (intel_crtc->config.has_pch_encoder)
4365 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4366
575f7ab7 4367 intel_disable_pipe(intel_crtc);
32f9d658 4368
3f8dce3a 4369 ironlake_pfit_disable(intel_crtc);
2c07245f 4370
bf49ec8c
DV
4371 for_each_encoder_on_crtc(dev, crtc, encoder)
4372 if (encoder->post_disable)
4373 encoder->post_disable(encoder);
2c07245f 4374
d925c59a
DV
4375 if (intel_crtc->config.has_pch_encoder) {
4376 ironlake_fdi_disable(crtc);
913d8d11 4377
d925c59a
DV
4378 ironlake_disable_pch_transcoder(dev_priv, pipe);
4379 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4380
d925c59a
DV
4381 if (HAS_PCH_CPT(dev)) {
4382 /* disable TRANS_DP_CTL */
4383 reg = TRANS_DP_CTL(pipe);
4384 temp = I915_READ(reg);
4385 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4386 TRANS_DP_PORT_SEL_MASK);
4387 temp |= TRANS_DP_PORT_SEL_NONE;
4388 I915_WRITE(reg, temp);
4389
4390 /* disable DPLL_SEL */
4391 temp = I915_READ(PCH_DPLL_SEL);
11887397 4392 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4393 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4394 }
e3421a18 4395
d925c59a 4396 /* disable PCH DPLL */
e72f9fbf 4397 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4398
d925c59a
DV
4399 ironlake_fdi_pll_disable(intel_crtc);
4400 }
6b383a7f 4401
f7abfe8b 4402 intel_crtc->active = false;
46ba614c 4403 intel_update_watermarks(crtc);
d1ebd816
BW
4404
4405 mutex_lock(&dev->struct_mutex);
6b383a7f 4406 intel_update_fbc(dev);
d1ebd816 4407 mutex_unlock(&dev->struct_mutex);
6be4a607 4408}
1b3c7a47 4409
4f771f10 4410static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4411{
4f771f10
PZ
4412 struct drm_device *dev = crtc->dev;
4413 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 4415 struct intel_encoder *encoder;
3b117c8f 4416 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4417
4f771f10
PZ
4418 if (!intel_crtc->active)
4419 return;
4420
d3eedb1a 4421 intel_crtc_disable_planes(crtc);
dda9a66a 4422
8807e55b
JN
4423 for_each_encoder_on_crtc(dev, crtc, encoder) {
4424 intel_opregion_notify_encoder(encoder, false);
4f771f10 4425 encoder->disable(encoder);
8807e55b 4426 }
4f771f10 4427
8664281b
PZ
4428 if (intel_crtc->config.has_pch_encoder)
4429 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
575f7ab7 4430 intel_disable_pipe(intel_crtc);
4f771f10 4431
a4bf214f
VS
4432 if (intel_crtc->config.dp_encoder_is_mst)
4433 intel_ddi_set_vc_payload_alloc(crtc, false);
4434
ad80a810 4435 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4436
3f8dce3a 4437 ironlake_pfit_disable(intel_crtc);
4f771f10 4438
1f544388 4439 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 4440
88adfff1 4441 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4442 lpt_disable_pch_transcoder(dev_priv);
8664281b 4443 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4444 intel_ddi_fdi_disable(crtc);
83616634 4445 }
4f771f10 4446
97b040aa
ID
4447 for_each_encoder_on_crtc(dev, crtc, encoder)
4448 if (encoder->post_disable)
4449 encoder->post_disable(encoder);
4450
4f771f10 4451 intel_crtc->active = false;
46ba614c 4452 intel_update_watermarks(crtc);
4f771f10
PZ
4453
4454 mutex_lock(&dev->struct_mutex);
4455 intel_update_fbc(dev);
4456 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
4457
4458 if (intel_crtc_to_shared_dpll(intel_crtc))
4459 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
4460}
4461
ee7b9f93
JB
4462static void ironlake_crtc_off(struct drm_crtc *crtc)
4463{
4464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4465 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4466}
4467
6441ab5f 4468
2dd24552
JB
4469static void i9xx_pfit_enable(struct intel_crtc *crtc)
4470{
4471 struct drm_device *dev = crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473 struct intel_crtc_config *pipe_config = &crtc->config;
4474
328d8e82 4475 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4476 return;
4477
2dd24552 4478 /*
c0b03411
DV
4479 * The panel fitter should only be adjusted whilst the pipe is disabled,
4480 * according to register description and PRM.
2dd24552 4481 */
c0b03411
DV
4482 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4483 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4484
b074cec8
JB
4485 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4486 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4487
4488 /* Border color in case we don't scale up to the full screen. Black by
4489 * default, change to something else for debugging. */
4490 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4491}
4492
d05410f9
DA
4493static enum intel_display_power_domain port_to_power_domain(enum port port)
4494{
4495 switch (port) {
4496 case PORT_A:
4497 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4498 case PORT_B:
4499 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4500 case PORT_C:
4501 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4502 case PORT_D:
4503 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4504 default:
4505 WARN_ON_ONCE(1);
4506 return POWER_DOMAIN_PORT_OTHER;
4507 }
4508}
4509
77d22dca
ID
4510#define for_each_power_domain(domain, mask) \
4511 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4512 if ((1 << (domain)) & (mask))
4513
319be8ae
ID
4514enum intel_display_power_domain
4515intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4516{
4517 struct drm_device *dev = intel_encoder->base.dev;
4518 struct intel_digital_port *intel_dig_port;
4519
4520 switch (intel_encoder->type) {
4521 case INTEL_OUTPUT_UNKNOWN:
4522 /* Only DDI platforms should ever use this output type */
4523 WARN_ON_ONCE(!HAS_DDI(dev));
4524 case INTEL_OUTPUT_DISPLAYPORT:
4525 case INTEL_OUTPUT_HDMI:
4526 case INTEL_OUTPUT_EDP:
4527 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 4528 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
4529 case INTEL_OUTPUT_DP_MST:
4530 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4531 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
4532 case INTEL_OUTPUT_ANALOG:
4533 return POWER_DOMAIN_PORT_CRT;
4534 case INTEL_OUTPUT_DSI:
4535 return POWER_DOMAIN_PORT_DSI;
4536 default:
4537 return POWER_DOMAIN_PORT_OTHER;
4538 }
4539}
4540
4541static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4542{
319be8ae
ID
4543 struct drm_device *dev = crtc->dev;
4544 struct intel_encoder *intel_encoder;
4545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4546 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
4547 unsigned long mask;
4548 enum transcoder transcoder;
4549
4550 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4551
4552 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4553 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
fabf6e51
DV
4554 if (intel_crtc->config.pch_pfit.enabled ||
4555 intel_crtc->config.pch_pfit.force_thru)
77d22dca
ID
4556 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4557
319be8ae
ID
4558 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4559 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4560
77d22dca
ID
4561 return mask;
4562}
4563
4564void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4565 bool enable)
4566{
4567 if (dev_priv->power_domains.init_power_on == enable)
4568 return;
4569
4570 if (enable)
4571 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4572 else
4573 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4574
4575 dev_priv->power_domains.init_power_on = enable;
4576}
4577
4578static void modeset_update_crtc_power_domains(struct drm_device *dev)
4579{
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4582 struct intel_crtc *crtc;
4583
4584 /*
4585 * First get all needed power domains, then put all unneeded, to avoid
4586 * any unnecessary toggling of the power wells.
4587 */
d3fcc808 4588 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4589 enum intel_display_power_domain domain;
4590
4591 if (!crtc->base.enabled)
4592 continue;
4593
319be8ae 4594 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4595
4596 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4597 intel_display_power_get(dev_priv, domain);
4598 }
4599
d3fcc808 4600 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
4601 enum intel_display_power_domain domain;
4602
4603 for_each_power_domain(domain, crtc->enabled_power_domains)
4604 intel_display_power_put(dev_priv, domain);
4605
4606 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4607 }
4608
4609 intel_display_set_init_power(dev_priv, false);
4610}
4611
dfcab17e 4612/* returns HPLL frequency in kHz */
f8bf63fd 4613static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4614{
586f49dc 4615 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4616
586f49dc
JB
4617 /* Obtain SKU information */
4618 mutex_lock(&dev_priv->dpio_lock);
4619 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4620 CCK_FUSE_HPLL_FREQ_MASK;
4621 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4622
dfcab17e 4623 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
4624}
4625
f8bf63fd
VS
4626static void vlv_update_cdclk(struct drm_device *dev)
4627{
4628 struct drm_i915_private *dev_priv = dev->dev_private;
4629
4630 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4631 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4632 dev_priv->vlv_cdclk_freq);
4633
4634 /*
4635 * Program the gmbus_freq based on the cdclk frequency.
4636 * BSpec erroneously claims we should aim for 4MHz, but
4637 * in fact 1MHz is the correct frequency.
4638 */
4639 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4640}
4641
30a970c6
JB
4642/* Adjust CDclk dividers to allow high res or save power if possible */
4643static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4644{
4645 struct drm_i915_private *dev_priv = dev->dev_private;
4646 u32 val, cmd;
4647
d197b7d3 4648 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
d60c4473 4649
dfcab17e 4650 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 4651 cmd = 2;
dfcab17e 4652 else if (cdclk == 266667)
30a970c6
JB
4653 cmd = 1;
4654 else
4655 cmd = 0;
4656
4657 mutex_lock(&dev_priv->rps.hw_lock);
4658 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4659 val &= ~DSPFREQGUAR_MASK;
4660 val |= (cmd << DSPFREQGUAR_SHIFT);
4661 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4662 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4663 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4664 50)) {
4665 DRM_ERROR("timed out waiting for CDclk change\n");
4666 }
4667 mutex_unlock(&dev_priv->rps.hw_lock);
4668
dfcab17e 4669 if (cdclk == 400000) {
30a970c6
JB
4670 u32 divider, vco;
4671
4672 vco = valleyview_get_vco(dev_priv);
dfcab17e 4673 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
30a970c6
JB
4674
4675 mutex_lock(&dev_priv->dpio_lock);
4676 /* adjust cdclk divider */
4677 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 4678 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
4679 val |= divider;
4680 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
4681
4682 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4683 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4684 50))
4685 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
4686 mutex_unlock(&dev_priv->dpio_lock);
4687 }
4688
4689 mutex_lock(&dev_priv->dpio_lock);
4690 /* adjust self-refresh exit latency value */
4691 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4692 val &= ~0x7f;
4693
4694 /*
4695 * For high bandwidth configs, we set a higher latency in the bunit
4696 * so that the core display fetch happens in time to avoid underruns.
4697 */
dfcab17e 4698 if (cdclk == 400000)
30a970c6
JB
4699 val |= 4500 / 250; /* 4.5 usec */
4700 else
4701 val |= 3000 / 250; /* 3.0 usec */
4702 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4703 mutex_unlock(&dev_priv->dpio_lock);
4704
f8bf63fd 4705 vlv_update_cdclk(dev);
30a970c6
JB
4706}
4707
383c5a6a
VS
4708static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4709{
4710 struct drm_i915_private *dev_priv = dev->dev_private;
4711 u32 val, cmd;
4712
4713 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4714
4715 switch (cdclk) {
4716 case 400000:
4717 cmd = 3;
4718 break;
4719 case 333333:
4720 case 320000:
4721 cmd = 2;
4722 break;
4723 case 266667:
4724 cmd = 1;
4725 break;
4726 case 200000:
4727 cmd = 0;
4728 break;
4729 default:
4730 WARN_ON(1);
4731 return;
4732 }
4733
4734 mutex_lock(&dev_priv->rps.hw_lock);
4735 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4736 val &= ~DSPFREQGUAR_MASK_CHV;
4737 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4738 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4739 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4740 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4741 50)) {
4742 DRM_ERROR("timed out waiting for CDclk change\n");
4743 }
4744 mutex_unlock(&dev_priv->rps.hw_lock);
4745
4746 vlv_update_cdclk(dev);
4747}
4748
30a970c6
JB
4749static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4750 int max_pixclk)
4751{
29dc7ef3
VS
4752 int vco = valleyview_get_vco(dev_priv);
4753 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4754
d49a340d
VS
4755 /* FIXME: Punit isn't quite ready yet */
4756 if (IS_CHERRYVIEW(dev_priv->dev))
4757 return 400000;
4758
30a970c6
JB
4759 /*
4760 * Really only a few cases to deal with, as only 4 CDclks are supported:
4761 * 200MHz
4762 * 267MHz
29dc7ef3 4763 * 320/333MHz (depends on HPLL freq)
30a970c6
JB
4764 * 400MHz
4765 * So we check to see whether we're above 90% of the lower bin and
4766 * adjust if needed.
e37c67a1
VS
4767 *
4768 * We seem to get an unstable or solid color picture at 200MHz.
4769 * Not sure what's wrong. For now use 200MHz only when all pipes
4770 * are off.
30a970c6 4771 */
29dc7ef3 4772 if (max_pixclk > freq_320*9/10)
dfcab17e
VS
4773 return 400000;
4774 else if (max_pixclk > 266667*9/10)
29dc7ef3 4775 return freq_320;
e37c67a1 4776 else if (max_pixclk > 0)
dfcab17e 4777 return 266667;
e37c67a1
VS
4778 else
4779 return 200000;
30a970c6
JB
4780}
4781
2f2d7aa1
VS
4782/* compute the max pixel clock for new configuration */
4783static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4784{
4785 struct drm_device *dev = dev_priv->dev;
4786 struct intel_crtc *intel_crtc;
4787 int max_pixclk = 0;
4788
d3fcc808 4789 for_each_intel_crtc(dev, intel_crtc) {
2f2d7aa1 4790 if (intel_crtc->new_enabled)
30a970c6 4791 max_pixclk = max(max_pixclk,
2f2d7aa1 4792 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4793 }
4794
4795 return max_pixclk;
4796}
4797
4798static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4799 unsigned *prepare_pipes)
30a970c6
JB
4800{
4801 struct drm_i915_private *dev_priv = dev->dev_private;
4802 struct intel_crtc *intel_crtc;
2f2d7aa1 4803 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4804
d60c4473
ID
4805 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4806 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4807 return;
4808
2f2d7aa1 4809 /* disable/enable all currently active pipes while we change cdclk */
d3fcc808 4810 for_each_intel_crtc(dev, intel_crtc)
30a970c6
JB
4811 if (intel_crtc->base.enabled)
4812 *prepare_pipes |= (1 << intel_crtc->pipe);
4813}
4814
4815static void valleyview_modeset_global_resources(struct drm_device *dev)
4816{
4817 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4818 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4819 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4820
383c5a6a
VS
4821 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4822 if (IS_CHERRYVIEW(dev))
4823 cherryview_set_cdclk(dev, req_cdclk);
4824 else
4825 valleyview_set_cdclk(dev, req_cdclk);
4826 }
4827
77961eb9 4828 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4829}
4830
89b667f8
JB
4831static void valleyview_crtc_enable(struct drm_crtc *crtc)
4832{
4833 struct drm_device *dev = crtc->dev;
89b667f8
JB
4834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4835 struct intel_encoder *encoder;
4836 int pipe = intel_crtc->pipe;
23538ef1 4837 bool is_dsi;
89b667f8
JB
4838
4839 WARN_ON(!crtc->enabled);
4840
4841 if (intel_crtc->active)
4842 return;
4843
8525a235
SK
4844 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4845
1ae0d137
VS
4846 if (!is_dsi) {
4847 if (IS_CHERRYVIEW(dev))
4848 chv_prepare_pll(intel_crtc);
4849 else
4850 vlv_prepare_pll(intel_crtc);
4851 }
5b18e57c
DV
4852
4853 if (intel_crtc->config.has_dp_encoder)
4854 intel_dp_set_m_n(intel_crtc);
4855
4856 intel_set_pipe_timings(intel_crtc);
4857
5b18e57c
DV
4858 i9xx_set_pipeconf(intel_crtc);
4859
89b667f8 4860 intel_crtc->active = true;
89b667f8 4861
4a3436e8
VS
4862 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4863
89b667f8
JB
4864 for_each_encoder_on_crtc(dev, crtc, encoder)
4865 if (encoder->pre_pll_enable)
4866 encoder->pre_pll_enable(encoder);
4867
9d556c99
CML
4868 if (!is_dsi) {
4869 if (IS_CHERRYVIEW(dev))
4870 chv_enable_pll(intel_crtc);
4871 else
4872 vlv_enable_pll(intel_crtc);
4873 }
89b667f8
JB
4874
4875 for_each_encoder_on_crtc(dev, crtc, encoder)
4876 if (encoder->pre_enable)
4877 encoder->pre_enable(encoder);
4878
2dd24552
JB
4879 i9xx_pfit_enable(intel_crtc);
4880
63cbb074
VS
4881 intel_crtc_load_lut(crtc);
4882
f37fcc2a 4883 intel_update_watermarks(crtc);
e1fdc473 4884 intel_enable_pipe(intel_crtc);
be6a6f8e 4885
5004945f
JN
4886 for_each_encoder_on_crtc(dev, crtc, encoder)
4887 encoder->enable(encoder);
9ab0460b
VS
4888
4889 intel_crtc_enable_planes(crtc);
d40d9187 4890
56b80e1f
VS
4891 /* Underruns don't raise interrupts, so check manually. */
4892 i9xx_check_fifo_underruns(dev);
89b667f8
JB
4893}
4894
f13c2ef3
DV
4895static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4896{
4897 struct drm_device *dev = crtc->base.dev;
4898 struct drm_i915_private *dev_priv = dev->dev_private;
4899
4900 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4901 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4902}
4903
0b8765c6 4904static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4905{
4906 struct drm_device *dev = crtc->dev;
79e53945 4907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4908 struct intel_encoder *encoder;
79e53945 4909 int pipe = intel_crtc->pipe;
79e53945 4910
08a48469
DV
4911 WARN_ON(!crtc->enabled);
4912
f7abfe8b
CW
4913 if (intel_crtc->active)
4914 return;
4915
f13c2ef3
DV
4916 i9xx_set_pll_dividers(intel_crtc);
4917
5b18e57c
DV
4918 if (intel_crtc->config.has_dp_encoder)
4919 intel_dp_set_m_n(intel_crtc);
4920
4921 intel_set_pipe_timings(intel_crtc);
4922
5b18e57c
DV
4923 i9xx_set_pipeconf(intel_crtc);
4924
f7abfe8b 4925 intel_crtc->active = true;
6b383a7f 4926
4a3436e8
VS
4927 if (!IS_GEN2(dev))
4928 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4929
9d6d9f19
MK
4930 for_each_encoder_on_crtc(dev, crtc, encoder)
4931 if (encoder->pre_enable)
4932 encoder->pre_enable(encoder);
4933
f6736a1a
DV
4934 i9xx_enable_pll(intel_crtc);
4935
2dd24552
JB
4936 i9xx_pfit_enable(intel_crtc);
4937
63cbb074
VS
4938 intel_crtc_load_lut(crtc);
4939
f37fcc2a 4940 intel_update_watermarks(crtc);
e1fdc473 4941 intel_enable_pipe(intel_crtc);
be6a6f8e 4942
fa5c73b1
DV
4943 for_each_encoder_on_crtc(dev, crtc, encoder)
4944 encoder->enable(encoder);
9ab0460b
VS
4945
4946 intel_crtc_enable_planes(crtc);
d40d9187 4947
4a3436e8
VS
4948 /*
4949 * Gen2 reports pipe underruns whenever all planes are disabled.
4950 * So don't enable underrun reporting before at least some planes
4951 * are enabled.
4952 * FIXME: Need to fix the logic to work when we turn off all planes
4953 * but leave the pipe running.
4954 */
4955 if (IS_GEN2(dev))
4956 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4957
56b80e1f
VS
4958 /* Underruns don't raise interrupts, so check manually. */
4959 i9xx_check_fifo_underruns(dev);
0b8765c6 4960}
79e53945 4961
87476d63
DV
4962static void i9xx_pfit_disable(struct intel_crtc *crtc)
4963{
4964 struct drm_device *dev = crtc->base.dev;
4965 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4966
328d8e82
DV
4967 if (!crtc->config.gmch_pfit.control)
4968 return;
87476d63 4969
328d8e82 4970 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4971
328d8e82
DV
4972 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4973 I915_READ(PFIT_CONTROL));
4974 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4975}
4976
0b8765c6
JB
4977static void i9xx_crtc_disable(struct drm_crtc *crtc)
4978{
4979 struct drm_device *dev = crtc->dev;
4980 struct drm_i915_private *dev_priv = dev->dev_private;
4981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4982 struct intel_encoder *encoder;
0b8765c6 4983 int pipe = intel_crtc->pipe;
ef9c3aee 4984
f7abfe8b
CW
4985 if (!intel_crtc->active)
4986 return;
4987
4a3436e8
VS
4988 /*
4989 * Gen2 reports pipe underruns whenever all planes are disabled.
4990 * So diasble underrun reporting before all the planes get disabled.
4991 * FIXME: Need to fix the logic to work when we turn off all planes
4992 * but leave the pipe running.
4993 */
4994 if (IS_GEN2(dev))
4995 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4996
564ed191
ID
4997 /*
4998 * Vblank time updates from the shadow to live plane control register
4999 * are blocked if the memory self-refresh mode is active at that
5000 * moment. So to make sure the plane gets truly disabled, disable
5001 * first the self-refresh mode. The self-refresh enable bit in turn
5002 * will be checked/applied by the HW only at the next frame start
5003 * event which is after the vblank start event, so we need to have a
5004 * wait-for-vblank between disabling the plane and the pipe.
5005 */
5006 intel_set_memory_cxsr(dev_priv, false);
9ab0460b
VS
5007 intel_crtc_disable_planes(crtc);
5008
ea9d758d
DV
5009 for_each_encoder_on_crtc(dev, crtc, encoder)
5010 encoder->disable(encoder);
5011
6304cd91
VS
5012 /*
5013 * On gen2 planes are double buffered but the pipe isn't, so we must
5014 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
5015 * We also need to wait on all gmch platforms because of the
5016 * self-refresh mode constraint explained above.
6304cd91 5017 */
564ed191 5018 intel_wait_for_vblank(dev, pipe);
6304cd91 5019
575f7ab7 5020 intel_disable_pipe(intel_crtc);
24a1f16d 5021
87476d63 5022 i9xx_pfit_disable(intel_crtc);
24a1f16d 5023
89b667f8
JB
5024 for_each_encoder_on_crtc(dev, crtc, encoder)
5025 if (encoder->post_disable)
5026 encoder->post_disable(encoder);
5027
076ed3b2
CML
5028 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
5029 if (IS_CHERRYVIEW(dev))
5030 chv_disable_pll(dev_priv, pipe);
5031 else if (IS_VALLEYVIEW(dev))
5032 vlv_disable_pll(dev_priv, pipe);
5033 else
5034 i9xx_disable_pll(dev_priv, pipe);
5035 }
0b8765c6 5036
4a3436e8
VS
5037 if (!IS_GEN2(dev))
5038 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
5039
f7abfe8b 5040 intel_crtc->active = false;
46ba614c 5041 intel_update_watermarks(crtc);
f37fcc2a 5042
efa9624e 5043 mutex_lock(&dev->struct_mutex);
6b383a7f 5044 intel_update_fbc(dev);
efa9624e 5045 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
5046}
5047
ee7b9f93
JB
5048static void i9xx_crtc_off(struct drm_crtc *crtc)
5049{
5050}
5051
976f8a20
DV
5052static void intel_crtc_update_sarea(struct drm_crtc *crtc,
5053 bool enabled)
2c07245f
ZW
5054{
5055 struct drm_device *dev = crtc->dev;
5056 struct drm_i915_master_private *master_priv;
5057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5058 int pipe = intel_crtc->pipe;
79e53945
JB
5059
5060 if (!dev->primary->master)
5061 return;
5062
5063 master_priv = dev->primary->master->driver_priv;
5064 if (!master_priv->sarea_priv)
5065 return;
5066
79e53945
JB
5067 switch (pipe) {
5068 case 0:
5069 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
5070 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
5071 break;
5072 case 1:
5073 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
5074 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
5075 break;
5076 default:
9db4a9c7 5077 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
5078 break;
5079 }
79e53945
JB
5080}
5081
b04c5bd6
BF
5082/* Master function to enable/disable CRTC and corresponding power wells */
5083void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
5084{
5085 struct drm_device *dev = crtc->dev;
5086 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 5087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
5088 enum intel_display_power_domain domain;
5089 unsigned long domains;
976f8a20 5090
0e572fe7
DV
5091 if (enable) {
5092 if (!intel_crtc->active) {
e1e9fb84
DV
5093 domains = get_crtc_power_domains(crtc);
5094 for_each_power_domain(domain, domains)
5095 intel_display_power_get(dev_priv, domain);
5096 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
5097
5098 dev_priv->display.crtc_enable(crtc);
5099 }
5100 } else {
5101 if (intel_crtc->active) {
5102 dev_priv->display.crtc_disable(crtc);
5103
e1e9fb84
DV
5104 domains = intel_crtc->enabled_power_domains;
5105 for_each_power_domain(domain, domains)
5106 intel_display_power_put(dev_priv, domain);
5107 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
5108 }
5109 }
b04c5bd6
BF
5110}
5111
5112/**
5113 * Sets the power management mode of the pipe and plane.
5114 */
5115void intel_crtc_update_dpms(struct drm_crtc *crtc)
5116{
5117 struct drm_device *dev = crtc->dev;
5118 struct intel_encoder *intel_encoder;
5119 bool enable = false;
5120
5121 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5122 enable |= intel_encoder->connectors_active;
5123
5124 intel_crtc_control(crtc, enable);
976f8a20
DV
5125
5126 intel_crtc_update_sarea(crtc, enable);
5127}
5128
cdd59983
CW
5129static void intel_crtc_disable(struct drm_crtc *crtc)
5130{
cdd59983 5131 struct drm_device *dev = crtc->dev;
976f8a20 5132 struct drm_connector *connector;
ee7b9f93 5133 struct drm_i915_private *dev_priv = dev->dev_private;
2ff8fde1 5134 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
a071fa00 5135 enum pipe pipe = to_intel_crtc(crtc)->pipe;
cdd59983 5136
976f8a20
DV
5137 /* crtc should still be enabled when we disable it. */
5138 WARN_ON(!crtc->enabled);
5139
5140 dev_priv->display.crtc_disable(crtc);
5141 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
5142 dev_priv->display.off(crtc);
5143
f4510a27 5144 if (crtc->primary->fb) {
cdd59983 5145 mutex_lock(&dev->struct_mutex);
a071fa00
DV
5146 intel_unpin_fb_obj(old_obj);
5147 i915_gem_track_fb(old_obj, NULL,
5148 INTEL_FRONTBUFFER_PRIMARY(pipe));
cdd59983 5149 mutex_unlock(&dev->struct_mutex);
f4510a27 5150 crtc->primary->fb = NULL;
976f8a20
DV
5151 }
5152
5153 /* Update computed state. */
5154 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5155 if (!connector->encoder || !connector->encoder->crtc)
5156 continue;
5157
5158 if (connector->encoder->crtc != crtc)
5159 continue;
5160
5161 connector->dpms = DRM_MODE_DPMS_OFF;
5162 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
5163 }
5164}
5165
ea5b213a 5166void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 5167{
4ef69c7a 5168 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 5169
ea5b213a
CW
5170 drm_encoder_cleanup(encoder);
5171 kfree(intel_encoder);
7e7d76c3
JB
5172}
5173
9237329d 5174/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
5175 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5176 * state of the entire output pipe. */
9237329d 5177static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 5178{
5ab432ef
DV
5179 if (mode == DRM_MODE_DPMS_ON) {
5180 encoder->connectors_active = true;
5181
b2cabb0e 5182 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
5183 } else {
5184 encoder->connectors_active = false;
5185
b2cabb0e 5186 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 5187 }
79e53945
JB
5188}
5189
0a91ca29
DV
5190/* Cross check the actual hw state with our own modeset state tracking (and it's
5191 * internal consistency). */
b980514c 5192static void intel_connector_check_state(struct intel_connector *connector)
79e53945 5193{
0a91ca29
DV
5194 if (connector->get_hw_state(connector)) {
5195 struct intel_encoder *encoder = connector->encoder;
5196 struct drm_crtc *crtc;
5197 bool encoder_enabled;
5198 enum pipe pipe;
5199
5200 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5201 connector->base.base.id,
c23cc417 5202 connector->base.name);
0a91ca29 5203
0e32b39c
DA
5204 /* there is no real hw state for MST connectors */
5205 if (connector->mst_port)
5206 return;
5207
0a91ca29
DV
5208 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5209 "wrong connector dpms state\n");
5210 WARN(connector->base.encoder != &encoder->base,
5211 "active connector not linked to encoder\n");
0a91ca29 5212
36cd7444
DA
5213 if (encoder) {
5214 WARN(!encoder->connectors_active,
5215 "encoder->connectors_active not set\n");
5216
5217 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5218 WARN(!encoder_enabled, "encoder not enabled\n");
5219 if (WARN_ON(!encoder->base.crtc))
5220 return;
0a91ca29 5221
36cd7444 5222 crtc = encoder->base.crtc;
0a91ca29 5223
36cd7444
DA
5224 WARN(!crtc->enabled, "crtc not enabled\n");
5225 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5226 WARN(pipe != to_intel_crtc(crtc)->pipe,
5227 "encoder active on the wrong pipe\n");
5228 }
0a91ca29 5229 }
79e53945
JB
5230}
5231
5ab432ef
DV
5232/* Even simpler default implementation, if there's really no special case to
5233 * consider. */
5234void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 5235{
5ab432ef
DV
5236 /* All the simple cases only support two dpms states. */
5237 if (mode != DRM_MODE_DPMS_ON)
5238 mode = DRM_MODE_DPMS_OFF;
d4270e57 5239
5ab432ef
DV
5240 if (mode == connector->dpms)
5241 return;
5242
5243 connector->dpms = mode;
5244
5245 /* Only need to change hw state when actually enabled */
c9976dcf
CW
5246 if (connector->encoder)
5247 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 5248
b980514c 5249 intel_modeset_check_state(connector->dev);
79e53945
JB
5250}
5251
f0947c37
DV
5252/* Simple connector->get_hw_state implementation for encoders that support only
5253 * one connector and no cloning and hence the encoder state determines the state
5254 * of the connector. */
5255bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 5256{
24929352 5257 enum pipe pipe = 0;
f0947c37 5258 struct intel_encoder *encoder = connector->encoder;
ea5b213a 5259
f0947c37 5260 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
5261}
5262
1857e1da
DV
5263static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5264 struct intel_crtc_config *pipe_config)
5265{
5266 struct drm_i915_private *dev_priv = dev->dev_private;
5267 struct intel_crtc *pipe_B_crtc =
5268 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5269
5270 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5271 pipe_name(pipe), pipe_config->fdi_lanes);
5272 if (pipe_config->fdi_lanes > 4) {
5273 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5274 pipe_name(pipe), pipe_config->fdi_lanes);
5275 return false;
5276 }
5277
bafb6553 5278 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
5279 if (pipe_config->fdi_lanes > 2) {
5280 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5281 pipe_config->fdi_lanes);
5282 return false;
5283 } else {
5284 return true;
5285 }
5286 }
5287
5288 if (INTEL_INFO(dev)->num_pipes == 2)
5289 return true;
5290
5291 /* Ivybridge 3 pipe is really complicated */
5292 switch (pipe) {
5293 case PIPE_A:
5294 return true;
5295 case PIPE_B:
5296 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5297 pipe_config->fdi_lanes > 2) {
5298 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5299 pipe_name(pipe), pipe_config->fdi_lanes);
5300 return false;
5301 }
5302 return true;
5303 case PIPE_C:
1e833f40 5304 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
5305 pipe_B_crtc->config.fdi_lanes <= 2) {
5306 if (pipe_config->fdi_lanes > 2) {
5307 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5308 pipe_name(pipe), pipe_config->fdi_lanes);
5309 return false;
5310 }
5311 } else {
5312 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5313 return false;
5314 }
5315 return true;
5316 default:
5317 BUG();
5318 }
5319}
5320
e29c22c0
DV
5321#define RETRY 1
5322static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5323 struct intel_crtc_config *pipe_config)
877d48d5 5324{
1857e1da 5325 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 5326 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 5327 int lane, link_bw, fdi_dotclock;
e29c22c0 5328 bool setup_ok, needs_recompute = false;
877d48d5 5329
e29c22c0 5330retry:
877d48d5
DV
5331 /* FDI is a binary signal running at ~2.7GHz, encoding
5332 * each output octet as 10 bits. The actual frequency
5333 * is stored as a divider into a 100MHz clock, and the
5334 * mode pixel clock is stored in units of 1KHz.
5335 * Hence the bw of each lane in terms of the mode signal
5336 * is:
5337 */
5338 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5339
241bfc38 5340 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 5341
2bd89a07 5342 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
5343 pipe_config->pipe_bpp);
5344
5345 pipe_config->fdi_lanes = lane;
5346
2bd89a07 5347 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 5348 link_bw, &pipe_config->fdi_m_n);
1857e1da 5349
e29c22c0
DV
5350 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5351 intel_crtc->pipe, pipe_config);
5352 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5353 pipe_config->pipe_bpp -= 2*3;
5354 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5355 pipe_config->pipe_bpp);
5356 needs_recompute = true;
5357 pipe_config->bw_constrained = true;
5358
5359 goto retry;
5360 }
5361
5362 if (needs_recompute)
5363 return RETRY;
5364
5365 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
5366}
5367
42db64ef
PZ
5368static void hsw_compute_ips_config(struct intel_crtc *crtc,
5369 struct intel_crtc_config *pipe_config)
5370{
d330a953 5371 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 5372 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 5373 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
5374}
5375
a43f6e0f 5376static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 5377 struct intel_crtc_config *pipe_config)
79e53945 5378{
a43f6e0f 5379 struct drm_device *dev = crtc->base.dev;
b8cecdf5 5380 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 5381
ad3a4479 5382 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
5383 if (INTEL_INFO(dev)->gen < 4) {
5384 struct drm_i915_private *dev_priv = dev->dev_private;
5385 int clock_limit =
5386 dev_priv->display.get_display_clock_speed(dev);
5387
5388 /*
5389 * Enable pixel doubling when the dot clock
5390 * is > 90% of the (display) core speed.
5391 *
b397c96b
VS
5392 * GDG double wide on either pipe,
5393 * otherwise pipe A only.
cf532bb2 5394 */
b397c96b 5395 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 5396 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 5397 clock_limit *= 2;
cf532bb2 5398 pipe_config->double_wide = true;
ad3a4479
VS
5399 }
5400
241bfc38 5401 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 5402 return -EINVAL;
2c07245f 5403 }
89749350 5404
1d1d0e27
VS
5405 /*
5406 * Pipe horizontal size must be even in:
5407 * - DVO ganged mode
5408 * - LVDS dual channel mode
5409 * - Double wide pipe
5410 */
5411 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5412 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5413 pipe_config->pipe_src_w &= ~1;
5414
8693a824
DL
5415 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5416 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
5417 */
5418 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5419 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 5420 return -EINVAL;
44f46b42 5421
bd080ee5 5422 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 5423 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 5424 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
5425 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5426 * for lvds. */
5427 pipe_config->pipe_bpp = 8*3;
5428 }
5429
f5adf94e 5430 if (HAS_IPS(dev))
a43f6e0f
DV
5431 hsw_compute_ips_config(crtc, pipe_config);
5432
12030431
DV
5433 /*
5434 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5435 * old clock survives for now.
5436 */
5437 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
a43f6e0f 5438 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 5439
877d48d5 5440 if (pipe_config->has_pch_encoder)
a43f6e0f 5441 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 5442
e29c22c0 5443 return 0;
79e53945
JB
5444}
5445
25eb05fc
JB
5446static int valleyview_get_display_clock_speed(struct drm_device *dev)
5447{
d197b7d3
VS
5448 struct drm_i915_private *dev_priv = dev->dev_private;
5449 int vco = valleyview_get_vco(dev_priv);
5450 u32 val;
5451 int divider;
5452
d49a340d
VS
5453 /* FIXME: Punit isn't quite ready yet */
5454 if (IS_CHERRYVIEW(dev))
5455 return 400000;
5456
d197b7d3
VS
5457 mutex_lock(&dev_priv->dpio_lock);
5458 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5459 mutex_unlock(&dev_priv->dpio_lock);
5460
5461 divider = val & DISPLAY_FREQUENCY_VALUES;
5462
7d007f40
VS
5463 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5464 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5465 "cdclk change in progress\n");
5466
d197b7d3 5467 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
25eb05fc
JB
5468}
5469
e70236a8
JB
5470static int i945_get_display_clock_speed(struct drm_device *dev)
5471{
5472 return 400000;
5473}
79e53945 5474
e70236a8 5475static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 5476{
e70236a8
JB
5477 return 333000;
5478}
79e53945 5479
e70236a8
JB
5480static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5481{
5482 return 200000;
5483}
79e53945 5484
257a7ffc
DV
5485static int pnv_get_display_clock_speed(struct drm_device *dev)
5486{
5487 u16 gcfgc = 0;
5488
5489 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5490
5491 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5492 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5493 return 267000;
5494 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5495 return 333000;
5496 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5497 return 444000;
5498 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5499 return 200000;
5500 default:
5501 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5502 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5503 return 133000;
5504 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5505 return 167000;
5506 }
5507}
5508
e70236a8
JB
5509static int i915gm_get_display_clock_speed(struct drm_device *dev)
5510{
5511 u16 gcfgc = 0;
79e53945 5512
e70236a8
JB
5513 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5514
5515 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5516 return 133000;
5517 else {
5518 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5519 case GC_DISPLAY_CLOCK_333_MHZ:
5520 return 333000;
5521 default:
5522 case GC_DISPLAY_CLOCK_190_200_MHZ:
5523 return 190000;
79e53945 5524 }
e70236a8
JB
5525 }
5526}
5527
5528static int i865_get_display_clock_speed(struct drm_device *dev)
5529{
5530 return 266000;
5531}
5532
5533static int i855_get_display_clock_speed(struct drm_device *dev)
5534{
5535 u16 hpllcc = 0;
5536 /* Assume that the hardware is in the high speed state. This
5537 * should be the default.
5538 */
5539 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5540 case GC_CLOCK_133_200:
5541 case GC_CLOCK_100_200:
5542 return 200000;
5543 case GC_CLOCK_166_250:
5544 return 250000;
5545 case GC_CLOCK_100_133:
79e53945 5546 return 133000;
e70236a8 5547 }
79e53945 5548
e70236a8
JB
5549 /* Shouldn't happen */
5550 return 0;
5551}
79e53945 5552
e70236a8
JB
5553static int i830_get_display_clock_speed(struct drm_device *dev)
5554{
5555 return 133000;
79e53945
JB
5556}
5557
2c07245f 5558static void
a65851af 5559intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5560{
a65851af
VS
5561 while (*num > DATA_LINK_M_N_MASK ||
5562 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5563 *num >>= 1;
5564 *den >>= 1;
5565 }
5566}
5567
a65851af
VS
5568static void compute_m_n(unsigned int m, unsigned int n,
5569 uint32_t *ret_m, uint32_t *ret_n)
5570{
5571 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5572 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5573 intel_reduce_m_n_ratio(ret_m, ret_n);
5574}
5575
e69d0bc1
DV
5576void
5577intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5578 int pixel_clock, int link_clock,
5579 struct intel_link_m_n *m_n)
2c07245f 5580{
e69d0bc1 5581 m_n->tu = 64;
a65851af
VS
5582
5583 compute_m_n(bits_per_pixel * pixel_clock,
5584 link_clock * nlanes * 8,
5585 &m_n->gmch_m, &m_n->gmch_n);
5586
5587 compute_m_n(pixel_clock, link_clock,
5588 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5589}
5590
a7615030
CW
5591static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5592{
d330a953
JN
5593 if (i915.panel_use_ssc >= 0)
5594 return i915.panel_use_ssc != 0;
41aa3448 5595 return dev_priv->vbt.lvds_use_ssc
435793df 5596 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5597}
5598
c65d77d8
JB
5599static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5600{
5601 struct drm_device *dev = crtc->dev;
5602 struct drm_i915_private *dev_priv = dev->dev_private;
5603 int refclk;
5604
a0c4da24 5605 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5606 refclk = 100000;
a0c4da24 5607 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5608 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5609 refclk = dev_priv->vbt.lvds_ssc_freq;
5610 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5611 } else if (!IS_GEN2(dev)) {
5612 refclk = 96000;
5613 } else {
5614 refclk = 48000;
5615 }
5616
5617 return refclk;
5618}
5619
7429e9d4 5620static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5621{
7df00d7a 5622 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5623}
f47709a9 5624
7429e9d4
DV
5625static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5626{
5627 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5628}
5629
f47709a9 5630static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5631 intel_clock_t *reduced_clock)
5632{
f47709a9 5633 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
5634 u32 fp, fp2 = 0;
5635
5636 if (IS_PINEVIEW(dev)) {
7429e9d4 5637 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5638 if (reduced_clock)
7429e9d4 5639 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5640 } else {
7429e9d4 5641 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5642 if (reduced_clock)
7429e9d4 5643 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5644 }
5645
8bcc2795 5646 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5647
f47709a9
DV
5648 crtc->lowfreq_avail = false;
5649 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5650 reduced_clock && i915.powersave) {
8bcc2795 5651 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5652 crtc->lowfreq_avail = true;
a7516a05 5653 } else {
8bcc2795 5654 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5655 }
5656}
5657
5e69f97f
CML
5658static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5659 pipe)
89b667f8
JB
5660{
5661 u32 reg_val;
5662
5663 /*
5664 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5665 * and set it to a reasonable value instead.
5666 */
ab3c759a 5667 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5668 reg_val &= 0xffffff00;
5669 reg_val |= 0x00000030;
ab3c759a 5670 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5671
ab3c759a 5672 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5673 reg_val &= 0x8cffffff;
5674 reg_val = 0x8c000000;
ab3c759a 5675 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5676
ab3c759a 5677 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5678 reg_val &= 0xffffff00;
ab3c759a 5679 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5680
ab3c759a 5681 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5682 reg_val &= 0x00ffffff;
5683 reg_val |= 0xb0000000;
ab3c759a 5684 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5685}
5686
b551842d
DV
5687static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5688 struct intel_link_m_n *m_n)
5689{
5690 struct drm_device *dev = crtc->base.dev;
5691 struct drm_i915_private *dev_priv = dev->dev_private;
5692 int pipe = crtc->pipe;
5693
e3b95f1e
DV
5694 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5695 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5696 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5697 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5698}
5699
5700static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
5701 struct intel_link_m_n *m_n,
5702 struct intel_link_m_n *m2_n2)
b551842d
DV
5703{
5704 struct drm_device *dev = crtc->base.dev;
5705 struct drm_i915_private *dev_priv = dev->dev_private;
5706 int pipe = crtc->pipe;
5707 enum transcoder transcoder = crtc->config.cpu_transcoder;
5708
5709 if (INTEL_INFO(dev)->gen >= 5) {
5710 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5711 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5712 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5713 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
5714 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5715 * for gen < 8) and if DRRS is supported (to make sure the
5716 * registers are not unnecessarily accessed).
5717 */
5718 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5719 crtc->config.has_drrs) {
5720 I915_WRITE(PIPE_DATA_M2(transcoder),
5721 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5722 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5723 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5724 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5725 }
b551842d 5726 } else {
e3b95f1e
DV
5727 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5728 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5729 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5730 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5731 }
5732}
5733
f769cd24 5734void intel_dp_set_m_n(struct intel_crtc *crtc)
03afc4a2
DV
5735{
5736 if (crtc->config.has_pch_encoder)
5737 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5738 else
f769cd24
VK
5739 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5740 &crtc->config.dp_m2_n2);
03afc4a2
DV
5741}
5742
f47709a9 5743static void vlv_update_pll(struct intel_crtc *crtc)
bdd4b6a6
DV
5744{
5745 u32 dpll, dpll_md;
5746
5747 /*
5748 * Enable DPIO clock input. We should never disable the reference
5749 * clock for pipe B, since VGA hotplug / manual detection depends
5750 * on it.
5751 */
5752 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5753 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5754 /* We should never disable this, set it here for state tracking */
5755 if (crtc->pipe == PIPE_B)
5756 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5757 dpll |= DPLL_VCO_ENABLE;
5758 crtc->config.dpll_hw_state.dpll = dpll;
5759
5760 dpll_md = (crtc->config.pixel_multiplier - 1)
5761 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5762 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5763}
5764
5765static void vlv_prepare_pll(struct intel_crtc *crtc)
a0c4da24 5766{
f47709a9 5767 struct drm_device *dev = crtc->base.dev;
a0c4da24 5768 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5769 int pipe = crtc->pipe;
bdd4b6a6 5770 u32 mdiv;
a0c4da24 5771 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 5772 u32 coreclk, reg_val;
a0c4da24 5773
09153000
DV
5774 mutex_lock(&dev_priv->dpio_lock);
5775
f47709a9
DV
5776 bestn = crtc->config.dpll.n;
5777 bestm1 = crtc->config.dpll.m1;
5778 bestm2 = crtc->config.dpll.m2;
5779 bestp1 = crtc->config.dpll.p1;
5780 bestp2 = crtc->config.dpll.p2;
a0c4da24 5781
89b667f8
JB
5782 /* See eDP HDMI DPIO driver vbios notes doc */
5783
5784 /* PLL B needs special handling */
bdd4b6a6 5785 if (pipe == PIPE_B)
5e69f97f 5786 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5787
5788 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5789 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5790
5791 /* Disable target IRef on PLL */
ab3c759a 5792 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5793 reg_val &= 0x00ffffff;
ab3c759a 5794 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5795
5796 /* Disable fast lock */
ab3c759a 5797 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5798
5799 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5800 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5801 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5802 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5803 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5804
5805 /*
5806 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5807 * but we don't support that).
5808 * Note: don't use the DAC post divider as it seems unstable.
5809 */
5810 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5811 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5812
a0c4da24 5813 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5814 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5815
89b667f8 5816 /* Set HBR and RBR LPF coefficients */
ff9a6750 5817 if (crtc->config.port_clock == 162000 ||
99750bd4 5818 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5819 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5820 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5821 0x009f0003);
89b667f8 5822 else
ab3c759a 5823 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5824 0x00d0000f);
5825
5826 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5827 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5828 /* Use SSC source */
bdd4b6a6 5829 if (pipe == PIPE_A)
ab3c759a 5830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5831 0x0df40000);
5832 else
ab3c759a 5833 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5834 0x0df70000);
5835 } else { /* HDMI or VGA */
5836 /* Use bend source */
bdd4b6a6 5837 if (pipe == PIPE_A)
ab3c759a 5838 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5839 0x0df70000);
5840 else
ab3c759a 5841 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5842 0x0df40000);
5843 }
a0c4da24 5844
ab3c759a 5845 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5846 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5847 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5848 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5849 coreclk |= 0x01000000;
ab3c759a 5850 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5851
ab3c759a 5852 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 5853 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5854}
5855
9d556c99 5856static void chv_update_pll(struct intel_crtc *crtc)
1ae0d137
VS
5857{
5858 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5859 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5860 DPLL_VCO_ENABLE;
5861 if (crtc->pipe != PIPE_A)
5862 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5863
5864 crtc->config.dpll_hw_state.dpll_md =
5865 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5866}
5867
5868static void chv_prepare_pll(struct intel_crtc *crtc)
9d556c99
CML
5869{
5870 struct drm_device *dev = crtc->base.dev;
5871 struct drm_i915_private *dev_priv = dev->dev_private;
5872 int pipe = crtc->pipe;
5873 int dpll_reg = DPLL(crtc->pipe);
5874 enum dpio_channel port = vlv_pipe_to_channel(pipe);
580d3811 5875 u32 loopfilter, intcoeff;
9d556c99
CML
5876 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5877 int refclk;
5878
9d556c99
CML
5879 bestn = crtc->config.dpll.n;
5880 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5881 bestm1 = crtc->config.dpll.m1;
5882 bestm2 = crtc->config.dpll.m2 >> 22;
5883 bestp1 = crtc->config.dpll.p1;
5884 bestp2 = crtc->config.dpll.p2;
5885
5886 /*
5887 * Enable Refclk and SSC
5888 */
a11b0703
VS
5889 I915_WRITE(dpll_reg,
5890 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5891
5892 mutex_lock(&dev_priv->dpio_lock);
9d556c99 5893
9d556c99
CML
5894 /* p1 and p2 divider */
5895 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5896 5 << DPIO_CHV_S1_DIV_SHIFT |
5897 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5898 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5899 1 << DPIO_CHV_K_DIV_SHIFT);
5900
5901 /* Feedback post-divider - m2 */
5902 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5903
5904 /* Feedback refclk divider - n and m1 */
5905 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5906 DPIO_CHV_M1_DIV_BY_2 |
5907 1 << DPIO_CHV_N_DIV_SHIFT);
5908
5909 /* M2 fraction division */
5910 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5911
5912 /* M2 fraction division enable */
5913 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5914 DPIO_CHV_FRAC_DIV_EN |
5915 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5916
5917 /* Loop filter */
5918 refclk = i9xx_get_refclk(&crtc->base, 0);
5919 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5920 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5921 if (refclk == 100000)
5922 intcoeff = 11;
5923 else if (refclk == 38400)
5924 intcoeff = 10;
5925 else
5926 intcoeff = 9;
5927 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5928 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5929
5930 /* AFC Recal */
5931 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5932 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5933 DPIO_AFC_RECAL);
5934
5935 mutex_unlock(&dev_priv->dpio_lock);
5936}
5937
f47709a9
DV
5938static void i9xx_update_pll(struct intel_crtc *crtc,
5939 intel_clock_t *reduced_clock,
eb1cbe48
DV
5940 int num_connectors)
5941{
f47709a9 5942 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5943 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5944 u32 dpll;
5945 bool is_sdvo;
f47709a9 5946 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5947
f47709a9 5948 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5949
f47709a9
DV
5950 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5951 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5952
5953 dpll = DPLL_VGA_MODE_DIS;
5954
f47709a9 5955 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5956 dpll |= DPLLB_MODE_LVDS;
5957 else
5958 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5959
ef1b460d 5960 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5961 dpll |= (crtc->config.pixel_multiplier - 1)
5962 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5963 }
198a037f
DV
5964
5965 if (is_sdvo)
4a33e48d 5966 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5967
f47709a9 5968 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5969 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5970
5971 /* compute bitmask from p1 value */
5972 if (IS_PINEVIEW(dev))
5973 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5974 else {
5975 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5976 if (IS_G4X(dev) && reduced_clock)
5977 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5978 }
5979 switch (clock->p2) {
5980 case 5:
5981 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5982 break;
5983 case 7:
5984 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5985 break;
5986 case 10:
5987 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5988 break;
5989 case 14:
5990 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5991 break;
5992 }
5993 if (INTEL_INFO(dev)->gen >= 4)
5994 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5995
09ede541 5996 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5997 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5998 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5999 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6000 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6001 else
6002 dpll |= PLL_REF_INPUT_DREFCLK;
6003
6004 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
6005 crtc->config.dpll_hw_state.dpll = dpll;
6006
eb1cbe48 6007 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
6008 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
6009 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 6010 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
6011 }
6012}
6013
f47709a9 6014static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 6015 intel_clock_t *reduced_clock,
eb1cbe48
DV
6016 int num_connectors)
6017{
f47709a9 6018 struct drm_device *dev = crtc->base.dev;
eb1cbe48 6019 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 6020 u32 dpll;
f47709a9 6021 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 6022
f47709a9 6023 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 6024
eb1cbe48
DV
6025 dpll = DPLL_VGA_MODE_DIS;
6026
f47709a9 6027 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
6028 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6029 } else {
6030 if (clock->p1 == 2)
6031 dpll |= PLL_P1_DIVIDE_BY_TWO;
6032 else
6033 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6034 if (clock->p2 == 4)
6035 dpll |= PLL_P2_DIVIDE_BY_4;
6036 }
6037
4a33e48d
DV
6038 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
6039 dpll |= DPLL_DVO_2X_MODE;
6040
f47709a9 6041 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
6042 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6043 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6044 else
6045 dpll |= PLL_REF_INPUT_DREFCLK;
6046
6047 dpll |= DPLL_VCO_ENABLE;
8bcc2795 6048 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
6049}
6050
8a654f3b 6051static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
6052{
6053 struct drm_device *dev = intel_crtc->base.dev;
6054 struct drm_i915_private *dev_priv = dev->dev_private;
6055 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6056 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
6057 struct drm_display_mode *adjusted_mode =
6058 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
6059 uint32_t crtc_vtotal, crtc_vblank_end;
6060 int vsyncshift = 0;
4d8a62ea
DV
6061
6062 /* We need to be careful not to changed the adjusted mode, for otherwise
6063 * the hw state checker will get angry at the mismatch. */
6064 crtc_vtotal = adjusted_mode->crtc_vtotal;
6065 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 6066
609aeaca 6067 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 6068 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
6069 crtc_vtotal -= 1;
6070 crtc_vblank_end -= 1;
609aeaca
VS
6071
6072 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6073 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6074 else
6075 vsyncshift = adjusted_mode->crtc_hsync_start -
6076 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
6077 if (vsyncshift < 0)
6078 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
6079 }
6080
6081 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 6082 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 6083
fe2b8f9d 6084 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
6085 (adjusted_mode->crtc_hdisplay - 1) |
6086 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 6087 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
6088 (adjusted_mode->crtc_hblank_start - 1) |
6089 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 6090 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
6091 (adjusted_mode->crtc_hsync_start - 1) |
6092 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6093
fe2b8f9d 6094 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 6095 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 6096 ((crtc_vtotal - 1) << 16));
fe2b8f9d 6097 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 6098 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 6099 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 6100 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
6101 (adjusted_mode->crtc_vsync_start - 1) |
6102 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6103
b5e508d4
PZ
6104 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6105 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6106 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6107 * bits. */
6108 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6109 (pipe == PIPE_B || pipe == PIPE_C))
6110 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6111
b0e77b9c
PZ
6112 /* pipesrc controls the size that is scaled from, which should
6113 * always be the user's requested size.
6114 */
6115 I915_WRITE(PIPESRC(pipe),
37327abd
VS
6116 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6117 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
6118}
6119
1bd1bd80
DV
6120static void intel_get_pipe_timings(struct intel_crtc *crtc,
6121 struct intel_crtc_config *pipe_config)
6122{
6123 struct drm_device *dev = crtc->base.dev;
6124 struct drm_i915_private *dev_priv = dev->dev_private;
6125 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6126 uint32_t tmp;
6127
6128 tmp = I915_READ(HTOTAL(cpu_transcoder));
6129 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6130 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6131 tmp = I915_READ(HBLANK(cpu_transcoder));
6132 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6133 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6134 tmp = I915_READ(HSYNC(cpu_transcoder));
6135 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6136 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6137
6138 tmp = I915_READ(VTOTAL(cpu_transcoder));
6139 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6140 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6141 tmp = I915_READ(VBLANK(cpu_transcoder));
6142 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6143 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6144 tmp = I915_READ(VSYNC(cpu_transcoder));
6145 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6146 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6147
6148 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6149 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6150 pipe_config->adjusted_mode.crtc_vtotal += 1;
6151 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6152 }
6153
6154 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
6155 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6156 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6157
6158 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6159 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
6160}
6161
f6a83288
DV
6162void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6163 struct intel_crtc_config *pipe_config)
babea61d 6164{
f6a83288
DV
6165 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6166 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6167 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6168 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 6169
f6a83288
DV
6170 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6171 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6172 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6173 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 6174
f6a83288 6175 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 6176
f6a83288
DV
6177 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6178 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
6179}
6180
84b046f3
DV
6181static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6182{
6183 struct drm_device *dev = intel_crtc->base.dev;
6184 struct drm_i915_private *dev_priv = dev->dev_private;
6185 uint32_t pipeconf;
6186
9f11a9e4 6187 pipeconf = 0;
84b046f3 6188
b6b5d049
VS
6189 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6190 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6191 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 6192
cf532bb2
VS
6193 if (intel_crtc->config.double_wide)
6194 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 6195
ff9ce46e
DV
6196 /* only g4x and later have fancy bpc/dither controls */
6197 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
6198 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6199 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6200 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 6201 PIPECONF_DITHER_TYPE_SP;
84b046f3 6202
ff9ce46e
DV
6203 switch (intel_crtc->config.pipe_bpp) {
6204 case 18:
6205 pipeconf |= PIPECONF_6BPC;
6206 break;
6207 case 24:
6208 pipeconf |= PIPECONF_8BPC;
6209 break;
6210 case 30:
6211 pipeconf |= PIPECONF_10BPC;
6212 break;
6213 default:
6214 /* Case prevented by intel_choose_pipe_bpp_dither. */
6215 BUG();
84b046f3
DV
6216 }
6217 }
6218
6219 if (HAS_PIPE_CXSR(dev)) {
6220 if (intel_crtc->lowfreq_avail) {
6221 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6222 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6223 } else {
6224 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
6225 }
6226 }
6227
efc2cfff
VS
6228 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6229 if (INTEL_INFO(dev)->gen < 4 ||
6230 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6231 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6232 else
6233 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6234 } else
84b046f3
DV
6235 pipeconf |= PIPECONF_PROGRESSIVE;
6236
9f11a9e4
DV
6237 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6238 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 6239
84b046f3
DV
6240 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6241 POSTING_READ(PIPECONF(intel_crtc->pipe));
6242}
6243
f564048e 6244static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 6245 int x, int y,
94352cf9 6246 struct drm_framebuffer *fb)
79e53945
JB
6247{
6248 struct drm_device *dev = crtc->dev;
6249 struct drm_i915_private *dev_priv = dev->dev_private;
6250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
c751ce4f 6251 int refclk, num_connectors = 0;
652c393a 6252 intel_clock_t clock, reduced_clock;
a16af721 6253 bool ok, has_reduced_clock = false;
e9fd1c02 6254 bool is_lvds = false, is_dsi = false;
5eddb70b 6255 struct intel_encoder *encoder;
d4906093 6256 const intel_limit_t *limit;
79e53945 6257
6c2b7c12 6258 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 6259 switch (encoder->type) {
79e53945
JB
6260 case INTEL_OUTPUT_LVDS:
6261 is_lvds = true;
6262 break;
e9fd1c02
JN
6263 case INTEL_OUTPUT_DSI:
6264 is_dsi = true;
6265 break;
79e53945 6266 }
43565a06 6267
c751ce4f 6268 num_connectors++;
79e53945
JB
6269 }
6270
f2335330 6271 if (is_dsi)
5b18e57c 6272 return 0;
f2335330
JN
6273
6274 if (!intel_crtc->config.clock_set) {
6275 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 6276
e9fd1c02
JN
6277 /*
6278 * Returns a set of divisors for the desired target clock with
6279 * the given refclk, or FALSE. The returned values represent
6280 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6281 * 2) / p1 / p2.
6282 */
6283 limit = intel_limit(crtc, refclk);
6284 ok = dev_priv->display.find_dpll(limit, crtc,
6285 intel_crtc->config.port_clock,
6286 refclk, NULL, &clock);
f2335330 6287 if (!ok) {
e9fd1c02
JN
6288 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6289 return -EINVAL;
6290 }
79e53945 6291
f2335330
JN
6292 if (is_lvds && dev_priv->lvds_downclock_avail) {
6293 /*
6294 * Ensure we match the reduced clock's P to the target
6295 * clock. If the clocks don't match, we can't switch
6296 * the display clock by using the FP0/FP1. In such case
6297 * we will disable the LVDS downclock feature.
6298 */
6299 has_reduced_clock =
6300 dev_priv->display.find_dpll(limit, crtc,
6301 dev_priv->lvds_downclock,
6302 refclk, &clock,
6303 &reduced_clock);
6304 }
6305 /* Compat-code for transition, will disappear. */
f47709a9
DV
6306 intel_crtc->config.dpll.n = clock.n;
6307 intel_crtc->config.dpll.m1 = clock.m1;
6308 intel_crtc->config.dpll.m2 = clock.m2;
6309 intel_crtc->config.dpll.p1 = clock.p1;
6310 intel_crtc->config.dpll.p2 = clock.p2;
6311 }
7026d4ac 6312
e9fd1c02 6313 if (IS_GEN2(dev)) {
8a654f3b 6314 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
6315 has_reduced_clock ? &reduced_clock : NULL,
6316 num_connectors);
9d556c99
CML
6317 } else if (IS_CHERRYVIEW(dev)) {
6318 chv_update_pll(intel_crtc);
e9fd1c02 6319 } else if (IS_VALLEYVIEW(dev)) {
f2335330 6320 vlv_update_pll(intel_crtc);
e9fd1c02 6321 } else {
f47709a9 6322 i9xx_update_pll(intel_crtc,
eb1cbe48 6323 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 6324 num_connectors);
e9fd1c02 6325 }
79e53945 6326
c8f7a0db 6327 return 0;
f564048e
EA
6328}
6329
2fa2fe9a
DV
6330static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6331 struct intel_crtc_config *pipe_config)
6332{
6333 struct drm_device *dev = crtc->base.dev;
6334 struct drm_i915_private *dev_priv = dev->dev_private;
6335 uint32_t tmp;
6336
dc9e7dec
VS
6337 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6338 return;
6339
2fa2fe9a 6340 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
6341 if (!(tmp & PFIT_ENABLE))
6342 return;
2fa2fe9a 6343
06922821 6344 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
6345 if (INTEL_INFO(dev)->gen < 4) {
6346 if (crtc->pipe != PIPE_B)
6347 return;
2fa2fe9a
DV
6348 } else {
6349 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6350 return;
6351 }
6352
06922821 6353 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
6354 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6355 if (INTEL_INFO(dev)->gen < 5)
6356 pipe_config->gmch_pfit.lvds_border_bits =
6357 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6358}
6359
acbec814
JB
6360static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6361 struct intel_crtc_config *pipe_config)
6362{
6363 struct drm_device *dev = crtc->base.dev;
6364 struct drm_i915_private *dev_priv = dev->dev_private;
6365 int pipe = pipe_config->cpu_transcoder;
6366 intel_clock_t clock;
6367 u32 mdiv;
662c6ecb 6368 int refclk = 100000;
acbec814 6369
f573de5a
SK
6370 /* In case of MIPI DPLL will not even be used */
6371 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6372 return;
6373
acbec814 6374 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 6375 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
6376 mutex_unlock(&dev_priv->dpio_lock);
6377
6378 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6379 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6380 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6381 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6382 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6383
f646628b 6384 vlv_clock(refclk, &clock);
acbec814 6385
f646628b
VS
6386 /* clock.dot is the fast clock */
6387 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
6388}
6389
1ad292b5
JB
6390static void i9xx_get_plane_config(struct intel_crtc *crtc,
6391 struct intel_plane_config *plane_config)
6392{
6393 struct drm_device *dev = crtc->base.dev;
6394 struct drm_i915_private *dev_priv = dev->dev_private;
6395 u32 val, base, offset;
6396 int pipe = crtc->pipe, plane = crtc->plane;
6397 int fourcc, pixel_format;
6398 int aligned_height;
6399
66e514c1
DA
6400 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6401 if (!crtc->base.primary->fb) {
1ad292b5
JB
6402 DRM_DEBUG_KMS("failed to alloc fb\n");
6403 return;
6404 }
6405
6406 val = I915_READ(DSPCNTR(plane));
6407
6408 if (INTEL_INFO(dev)->gen >= 4)
6409 if (val & DISPPLANE_TILED)
6410 plane_config->tiled = true;
6411
6412 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6413 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6414 crtc->base.primary->fb->pixel_format = fourcc;
6415 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
6416 drm_format_plane_cpp(fourcc, 0) * 8;
6417
6418 if (INTEL_INFO(dev)->gen >= 4) {
6419 if (plane_config->tiled)
6420 offset = I915_READ(DSPTILEOFF(plane));
6421 else
6422 offset = I915_READ(DSPLINOFF(plane));
6423 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6424 } else {
6425 base = I915_READ(DSPADDR(plane));
6426 }
6427 plane_config->base = base;
6428
6429 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6430 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6431 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
6432
6433 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 6434 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
1ad292b5 6435
66e514c1 6436 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
6437 plane_config->tiled);
6438
1267a26b
FF
6439 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6440 aligned_height);
1ad292b5
JB
6441
6442 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6443 pipe, plane, crtc->base.primary->fb->width,
6444 crtc->base.primary->fb->height,
6445 crtc->base.primary->fb->bits_per_pixel, base,
6446 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
6447 plane_config->size);
6448
6449}
6450
70b23a98
VS
6451static void chv_crtc_clock_get(struct intel_crtc *crtc,
6452 struct intel_crtc_config *pipe_config)
6453{
6454 struct drm_device *dev = crtc->base.dev;
6455 struct drm_i915_private *dev_priv = dev->dev_private;
6456 int pipe = pipe_config->cpu_transcoder;
6457 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6458 intel_clock_t clock;
6459 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6460 int refclk = 100000;
6461
6462 mutex_lock(&dev_priv->dpio_lock);
6463 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6464 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6465 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6466 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6467 mutex_unlock(&dev_priv->dpio_lock);
6468
6469 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6470 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6471 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6472 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6473 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6474
6475 chv_clock(refclk, &clock);
6476
6477 /* clock.dot is the fast clock */
6478 pipe_config->port_clock = clock.dot / 5;
6479}
6480
0e8ffe1b
DV
6481static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6482 struct intel_crtc_config *pipe_config)
6483{
6484 struct drm_device *dev = crtc->base.dev;
6485 struct drm_i915_private *dev_priv = dev->dev_private;
6486 uint32_t tmp;
6487
b5482bd0
ID
6488 if (!intel_display_power_enabled(dev_priv,
6489 POWER_DOMAIN_PIPE(crtc->pipe)))
6490 return false;
6491
e143a21c 6492 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6493 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6494
0e8ffe1b
DV
6495 tmp = I915_READ(PIPECONF(crtc->pipe));
6496 if (!(tmp & PIPECONF_ENABLE))
6497 return false;
6498
42571aef
VS
6499 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6500 switch (tmp & PIPECONF_BPC_MASK) {
6501 case PIPECONF_6BPC:
6502 pipe_config->pipe_bpp = 18;
6503 break;
6504 case PIPECONF_8BPC:
6505 pipe_config->pipe_bpp = 24;
6506 break;
6507 case PIPECONF_10BPC:
6508 pipe_config->pipe_bpp = 30;
6509 break;
6510 default:
6511 break;
6512 }
6513 }
6514
b5a9fa09
DV
6515 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6516 pipe_config->limited_color_range = true;
6517
282740f7
VS
6518 if (INTEL_INFO(dev)->gen < 4)
6519 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6520
1bd1bd80
DV
6521 intel_get_pipe_timings(crtc, pipe_config);
6522
2fa2fe9a
DV
6523 i9xx_get_pfit_config(crtc, pipe_config);
6524
6c49f241
DV
6525 if (INTEL_INFO(dev)->gen >= 4) {
6526 tmp = I915_READ(DPLL_MD(crtc->pipe));
6527 pipe_config->pixel_multiplier =
6528 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6529 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 6530 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
6531 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6532 tmp = I915_READ(DPLL(crtc->pipe));
6533 pipe_config->pixel_multiplier =
6534 ((tmp & SDVO_MULTIPLIER_MASK)
6535 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6536 } else {
6537 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6538 * port and will be fixed up in the encoder->get_config
6539 * function. */
6540 pipe_config->pixel_multiplier = 1;
6541 }
8bcc2795
DV
6542 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6543 if (!IS_VALLEYVIEW(dev)) {
6544 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6545 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
6546 } else {
6547 /* Mask out read-only status bits. */
6548 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6549 DPLL_PORTC_READY_MASK |
6550 DPLL_PORTB_READY_MASK);
8bcc2795 6551 }
6c49f241 6552
70b23a98
VS
6553 if (IS_CHERRYVIEW(dev))
6554 chv_crtc_clock_get(crtc, pipe_config);
6555 else if (IS_VALLEYVIEW(dev))
acbec814
JB
6556 vlv_crtc_clock_get(crtc, pipe_config);
6557 else
6558 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 6559
0e8ffe1b
DV
6560 return true;
6561}
6562
dde86e2d 6563static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
6564{
6565 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 6566 struct intel_encoder *encoder;
74cfd7ac 6567 u32 val, final;
13d83a67 6568 bool has_lvds = false;
199e5d79 6569 bool has_cpu_edp = false;
199e5d79 6570 bool has_panel = false;
99eb6a01
KP
6571 bool has_ck505 = false;
6572 bool can_ssc = false;
13d83a67
JB
6573
6574 /* We need to take the global config into account */
b2784e15 6575 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
6576 switch (encoder->type) {
6577 case INTEL_OUTPUT_LVDS:
6578 has_panel = true;
6579 has_lvds = true;
6580 break;
6581 case INTEL_OUTPUT_EDP:
6582 has_panel = true;
2de6905f 6583 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
6584 has_cpu_edp = true;
6585 break;
13d83a67
JB
6586 }
6587 }
6588
99eb6a01 6589 if (HAS_PCH_IBX(dev)) {
41aa3448 6590 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
6591 can_ssc = has_ck505;
6592 } else {
6593 has_ck505 = false;
6594 can_ssc = true;
6595 }
6596
2de6905f
ID
6597 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6598 has_panel, has_lvds, has_ck505);
13d83a67
JB
6599
6600 /* Ironlake: try to setup display ref clock before DPLL
6601 * enabling. This is only under driver's control after
6602 * PCH B stepping, previous chipset stepping should be
6603 * ignoring this setting.
6604 */
74cfd7ac
CW
6605 val = I915_READ(PCH_DREF_CONTROL);
6606
6607 /* As we must carefully and slowly disable/enable each source in turn,
6608 * compute the final state we want first and check if we need to
6609 * make any changes at all.
6610 */
6611 final = val;
6612 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6613 if (has_ck505)
6614 final |= DREF_NONSPREAD_CK505_ENABLE;
6615 else
6616 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6617
6618 final &= ~DREF_SSC_SOURCE_MASK;
6619 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6620 final &= ~DREF_SSC1_ENABLE;
6621
6622 if (has_panel) {
6623 final |= DREF_SSC_SOURCE_ENABLE;
6624
6625 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6626 final |= DREF_SSC1_ENABLE;
6627
6628 if (has_cpu_edp) {
6629 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6630 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6631 else
6632 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6633 } else
6634 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6635 } else {
6636 final |= DREF_SSC_SOURCE_DISABLE;
6637 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6638 }
6639
6640 if (final == val)
6641 return;
6642
13d83a67 6643 /* Always enable nonspread source */
74cfd7ac 6644 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6645
99eb6a01 6646 if (has_ck505)
74cfd7ac 6647 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6648 else
74cfd7ac 6649 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6650
199e5d79 6651 if (has_panel) {
74cfd7ac
CW
6652 val &= ~DREF_SSC_SOURCE_MASK;
6653 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6654
199e5d79 6655 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6656 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6657 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6658 val |= DREF_SSC1_ENABLE;
e77166b5 6659 } else
74cfd7ac 6660 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6661
6662 /* Get SSC going before enabling the outputs */
74cfd7ac 6663 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6664 POSTING_READ(PCH_DREF_CONTROL);
6665 udelay(200);
6666
74cfd7ac 6667 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6668
6669 /* Enable CPU source on CPU attached eDP */
199e5d79 6670 if (has_cpu_edp) {
99eb6a01 6671 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6672 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6673 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 6674 } else
74cfd7ac 6675 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6676 } else
74cfd7ac 6677 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6678
74cfd7ac 6679 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6680 POSTING_READ(PCH_DREF_CONTROL);
6681 udelay(200);
6682 } else {
6683 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6684
74cfd7ac 6685 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6686
6687 /* Turn off CPU output */
74cfd7ac 6688 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6689
74cfd7ac 6690 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6691 POSTING_READ(PCH_DREF_CONTROL);
6692 udelay(200);
6693
6694 /* Turn off the SSC source */
74cfd7ac
CW
6695 val &= ~DREF_SSC_SOURCE_MASK;
6696 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6697
6698 /* Turn off SSC1 */
74cfd7ac 6699 val &= ~DREF_SSC1_ENABLE;
199e5d79 6700
74cfd7ac 6701 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6702 POSTING_READ(PCH_DREF_CONTROL);
6703 udelay(200);
6704 }
74cfd7ac
CW
6705
6706 BUG_ON(val != final);
13d83a67
JB
6707}
6708
f31f2d55 6709static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6710{
f31f2d55 6711 uint32_t tmp;
dde86e2d 6712
0ff066a9
PZ
6713 tmp = I915_READ(SOUTH_CHICKEN2);
6714 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6715 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6716
0ff066a9
PZ
6717 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6718 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6719 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6720
0ff066a9
PZ
6721 tmp = I915_READ(SOUTH_CHICKEN2);
6722 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6723 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6724
0ff066a9
PZ
6725 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6726 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6727 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6728}
6729
6730/* WaMPhyProgramming:hsw */
6731static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6732{
6733 uint32_t tmp;
dde86e2d
PZ
6734
6735 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6736 tmp &= ~(0xFF << 24);
6737 tmp |= (0x12 << 24);
6738 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6739
dde86e2d
PZ
6740 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6741 tmp |= (1 << 11);
6742 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6743
6744 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6745 tmp |= (1 << 11);
6746 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6747
dde86e2d
PZ
6748 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6749 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6750 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6751
6752 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6753 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6754 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6755
0ff066a9
PZ
6756 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6757 tmp &= ~(7 << 13);
6758 tmp |= (5 << 13);
6759 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6760
0ff066a9
PZ
6761 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6762 tmp &= ~(7 << 13);
6763 tmp |= (5 << 13);
6764 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6765
6766 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6767 tmp &= ~0xFF;
6768 tmp |= 0x1C;
6769 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6770
6771 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6772 tmp &= ~0xFF;
6773 tmp |= 0x1C;
6774 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6775
6776 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6777 tmp &= ~(0xFF << 16);
6778 tmp |= (0x1C << 16);
6779 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6780
6781 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6782 tmp &= ~(0xFF << 16);
6783 tmp |= (0x1C << 16);
6784 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6785
0ff066a9
PZ
6786 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6787 tmp |= (1 << 27);
6788 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6789
0ff066a9
PZ
6790 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6791 tmp |= (1 << 27);
6792 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6793
0ff066a9
PZ
6794 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6795 tmp &= ~(0xF << 28);
6796 tmp |= (4 << 28);
6797 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6798
0ff066a9
PZ
6799 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6800 tmp &= ~(0xF << 28);
6801 tmp |= (4 << 28);
6802 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6803}
6804
2fa86a1f
PZ
6805/* Implements 3 different sequences from BSpec chapter "Display iCLK
6806 * Programming" based on the parameters passed:
6807 * - Sequence to enable CLKOUT_DP
6808 * - Sequence to enable CLKOUT_DP without spread
6809 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6810 */
6811static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6812 bool with_fdi)
f31f2d55
PZ
6813{
6814 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6815 uint32_t reg, tmp;
6816
6817 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6818 with_spread = true;
6819 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6820 with_fdi, "LP PCH doesn't have FDI\n"))
6821 with_fdi = false;
f31f2d55
PZ
6822
6823 mutex_lock(&dev_priv->dpio_lock);
6824
6825 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6826 tmp &= ~SBI_SSCCTL_DISABLE;
6827 tmp |= SBI_SSCCTL_PATHALT;
6828 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6829
6830 udelay(24);
6831
2fa86a1f
PZ
6832 if (with_spread) {
6833 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6834 tmp &= ~SBI_SSCCTL_PATHALT;
6835 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6836
2fa86a1f
PZ
6837 if (with_fdi) {
6838 lpt_reset_fdi_mphy(dev_priv);
6839 lpt_program_fdi_mphy(dev_priv);
6840 }
6841 }
dde86e2d 6842
2fa86a1f
PZ
6843 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6844 SBI_GEN0 : SBI_DBUFF0;
6845 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6846 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6847 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6848
6849 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6850}
6851
47701c3b
PZ
6852/* Sequence to disable CLKOUT_DP */
6853static void lpt_disable_clkout_dp(struct drm_device *dev)
6854{
6855 struct drm_i915_private *dev_priv = dev->dev_private;
6856 uint32_t reg, tmp;
6857
6858 mutex_lock(&dev_priv->dpio_lock);
6859
6860 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6861 SBI_GEN0 : SBI_DBUFF0;
6862 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6863 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6864 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6865
6866 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6867 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6868 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6869 tmp |= SBI_SSCCTL_PATHALT;
6870 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6871 udelay(32);
6872 }
6873 tmp |= SBI_SSCCTL_DISABLE;
6874 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6875 }
6876
6877 mutex_unlock(&dev_priv->dpio_lock);
6878}
6879
bf8fa3d3
PZ
6880static void lpt_init_pch_refclk(struct drm_device *dev)
6881{
bf8fa3d3
PZ
6882 struct intel_encoder *encoder;
6883 bool has_vga = false;
6884
b2784e15 6885 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
6886 switch (encoder->type) {
6887 case INTEL_OUTPUT_ANALOG:
6888 has_vga = true;
6889 break;
6890 }
6891 }
6892
47701c3b
PZ
6893 if (has_vga)
6894 lpt_enable_clkout_dp(dev, true, true);
6895 else
6896 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6897}
6898
dde86e2d
PZ
6899/*
6900 * Initialize reference clocks when the driver loads
6901 */
6902void intel_init_pch_refclk(struct drm_device *dev)
6903{
6904 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6905 ironlake_init_pch_refclk(dev);
6906 else if (HAS_PCH_LPT(dev))
6907 lpt_init_pch_refclk(dev);
6908}
6909
d9d444cb
JB
6910static int ironlake_get_refclk(struct drm_crtc *crtc)
6911{
6912 struct drm_device *dev = crtc->dev;
6913 struct drm_i915_private *dev_priv = dev->dev_private;
6914 struct intel_encoder *encoder;
d9d444cb
JB
6915 int num_connectors = 0;
6916 bool is_lvds = false;
6917
6c2b7c12 6918 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6919 switch (encoder->type) {
6920 case INTEL_OUTPUT_LVDS:
6921 is_lvds = true;
6922 break;
d9d444cb
JB
6923 }
6924 num_connectors++;
6925 }
6926
6927 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6928 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6929 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6930 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6931 }
6932
6933 return 120000;
6934}
6935
6ff93609 6936static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6937{
c8203565 6938 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6940 int pipe = intel_crtc->pipe;
c8203565
PZ
6941 uint32_t val;
6942
78114071 6943 val = 0;
c8203565 6944
965e0c48 6945 switch (intel_crtc->config.pipe_bpp) {
c8203565 6946 case 18:
dfd07d72 6947 val |= PIPECONF_6BPC;
c8203565
PZ
6948 break;
6949 case 24:
dfd07d72 6950 val |= PIPECONF_8BPC;
c8203565
PZ
6951 break;
6952 case 30:
dfd07d72 6953 val |= PIPECONF_10BPC;
c8203565
PZ
6954 break;
6955 case 36:
dfd07d72 6956 val |= PIPECONF_12BPC;
c8203565
PZ
6957 break;
6958 default:
cc769b62
PZ
6959 /* Case prevented by intel_choose_pipe_bpp_dither. */
6960 BUG();
c8203565
PZ
6961 }
6962
d8b32247 6963 if (intel_crtc->config.dither)
c8203565
PZ
6964 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6965
6ff93609 6966 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6967 val |= PIPECONF_INTERLACED_ILK;
6968 else
6969 val |= PIPECONF_PROGRESSIVE;
6970
50f3b016 6971 if (intel_crtc->config.limited_color_range)
3685a8f3 6972 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6973
c8203565
PZ
6974 I915_WRITE(PIPECONF(pipe), val);
6975 POSTING_READ(PIPECONF(pipe));
6976}
6977
86d3efce
VS
6978/*
6979 * Set up the pipe CSC unit.
6980 *
6981 * Currently only full range RGB to limited range RGB conversion
6982 * is supported, but eventually this should handle various
6983 * RGB<->YCbCr scenarios as well.
6984 */
50f3b016 6985static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6986{
6987 struct drm_device *dev = crtc->dev;
6988 struct drm_i915_private *dev_priv = dev->dev_private;
6989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6990 int pipe = intel_crtc->pipe;
6991 uint16_t coeff = 0x7800; /* 1.0 */
6992
6993 /*
6994 * TODO: Check what kind of values actually come out of the pipe
6995 * with these coeff/postoff values and adjust to get the best
6996 * accuracy. Perhaps we even need to take the bpc value into
6997 * consideration.
6998 */
6999
50f3b016 7000 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7001 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7002
7003 /*
7004 * GY/GU and RY/RU should be the other way around according
7005 * to BSpec, but reality doesn't agree. Just set them up in
7006 * a way that results in the correct picture.
7007 */
7008 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7009 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7010
7011 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7012 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7013
7014 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7015 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7016
7017 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7018 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7019 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7020
7021 if (INTEL_INFO(dev)->gen > 6) {
7022 uint16_t postoff = 0;
7023
50f3b016 7024 if (intel_crtc->config.limited_color_range)
32cf0cb0 7025 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
7026
7027 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7028 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7029 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7030
7031 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7032 } else {
7033 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7034
50f3b016 7035 if (intel_crtc->config.limited_color_range)
86d3efce
VS
7036 mode |= CSC_BLACK_SCREEN_OFFSET;
7037
7038 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7039 }
7040}
7041
6ff93609 7042static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 7043{
756f85cf
PZ
7044 struct drm_device *dev = crtc->dev;
7045 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 7046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 7047 enum pipe pipe = intel_crtc->pipe;
3b117c8f 7048 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
7049 uint32_t val;
7050
3eff4faa 7051 val = 0;
ee2b0b38 7052
756f85cf 7053 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
7054 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7055
6ff93609 7056 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
7057 val |= PIPECONF_INTERLACED_ILK;
7058 else
7059 val |= PIPECONF_PROGRESSIVE;
7060
702e7a56
PZ
7061 I915_WRITE(PIPECONF(cpu_transcoder), val);
7062 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
7063
7064 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7065 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 7066
3cdf122c 7067 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
7068 val = 0;
7069
7070 switch (intel_crtc->config.pipe_bpp) {
7071 case 18:
7072 val |= PIPEMISC_DITHER_6_BPC;
7073 break;
7074 case 24:
7075 val |= PIPEMISC_DITHER_8_BPC;
7076 break;
7077 case 30:
7078 val |= PIPEMISC_DITHER_10_BPC;
7079 break;
7080 case 36:
7081 val |= PIPEMISC_DITHER_12_BPC;
7082 break;
7083 default:
7084 /* Case prevented by pipe_config_set_bpp. */
7085 BUG();
7086 }
7087
7088 if (intel_crtc->config.dither)
7089 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7090
7091 I915_WRITE(PIPEMISC(pipe), val);
7092 }
ee2b0b38
PZ
7093}
7094
6591c6e4 7095static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
7096 intel_clock_t *clock,
7097 bool *has_reduced_clock,
7098 intel_clock_t *reduced_clock)
7099{
7100 struct drm_device *dev = crtc->dev;
7101 struct drm_i915_private *dev_priv = dev->dev_private;
7102 struct intel_encoder *intel_encoder;
7103 int refclk;
d4906093 7104 const intel_limit_t *limit;
a16af721 7105 bool ret, is_lvds = false;
79e53945 7106
6591c6e4
PZ
7107 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7108 switch (intel_encoder->type) {
79e53945
JB
7109 case INTEL_OUTPUT_LVDS:
7110 is_lvds = true;
7111 break;
79e53945
JB
7112 }
7113 }
7114
d9d444cb 7115 refclk = ironlake_get_refclk(crtc);
79e53945 7116
d4906093
ML
7117 /*
7118 * Returns a set of divisors for the desired target clock with the given
7119 * refclk, or FALSE. The returned values represent the clock equation:
7120 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7121 */
1b894b59 7122 limit = intel_limit(crtc, refclk);
ff9a6750
DV
7123 ret = dev_priv->display.find_dpll(limit, crtc,
7124 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 7125 refclk, NULL, clock);
6591c6e4
PZ
7126 if (!ret)
7127 return false;
cda4b7d3 7128
ddc9003c 7129 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
7130 /*
7131 * Ensure we match the reduced clock's P to the target clock.
7132 * If the clocks don't match, we can't switch the display clock
7133 * by using the FP0/FP1. In such case we will disable the LVDS
7134 * downclock feature.
7135 */
ee9300bb
DV
7136 *has_reduced_clock =
7137 dev_priv->display.find_dpll(limit, crtc,
7138 dev_priv->lvds_downclock,
7139 refclk, clock,
7140 reduced_clock);
652c393a 7141 }
61e9653f 7142
6591c6e4
PZ
7143 return true;
7144}
7145
d4b1931c
PZ
7146int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7147{
7148 /*
7149 * Account for spread spectrum to avoid
7150 * oversubscribing the link. Max center spread
7151 * is 2.5%; use 5% for safety's sake.
7152 */
7153 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 7154 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
7155}
7156
7429e9d4 7157static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 7158{
7429e9d4 7159 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
7160}
7161
de13a2e3 7162static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 7163 u32 *fp,
9a7c7890 7164 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 7165{
de13a2e3 7166 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
7167 struct drm_device *dev = crtc->dev;
7168 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
7169 struct intel_encoder *intel_encoder;
7170 uint32_t dpll;
6cc5f341 7171 int factor, num_connectors = 0;
09ede541 7172 bool is_lvds = false, is_sdvo = false;
79e53945 7173
de13a2e3
PZ
7174 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7175 switch (intel_encoder->type) {
79e53945
JB
7176 case INTEL_OUTPUT_LVDS:
7177 is_lvds = true;
7178 break;
7179 case INTEL_OUTPUT_SDVO:
7d57382e 7180 case INTEL_OUTPUT_HDMI:
79e53945 7181 is_sdvo = true;
79e53945 7182 break;
79e53945 7183 }
43565a06 7184
c751ce4f 7185 num_connectors++;
79e53945 7186 }
79e53945 7187
c1858123 7188 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
7189 factor = 21;
7190 if (is_lvds) {
7191 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 7192 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 7193 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 7194 factor = 25;
09ede541 7195 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 7196 factor = 20;
c1858123 7197
7429e9d4 7198 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 7199 *fp |= FP_CB_TUNE;
2c07245f 7200
9a7c7890
DV
7201 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7202 *fp2 |= FP_CB_TUNE;
7203
5eddb70b 7204 dpll = 0;
2c07245f 7205
a07d6787
EA
7206 if (is_lvds)
7207 dpll |= DPLLB_MODE_LVDS;
7208 else
7209 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 7210
ef1b460d
DV
7211 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7212 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
7213
7214 if (is_sdvo)
4a33e48d 7215 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 7216 if (intel_crtc->config.has_dp_encoder)
4a33e48d 7217 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 7218
a07d6787 7219 /* compute bitmask from p1 value */
7429e9d4 7220 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 7221 /* also FPA1 */
7429e9d4 7222 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 7223
7429e9d4 7224 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
7225 case 5:
7226 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7227 break;
7228 case 7:
7229 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7230 break;
7231 case 10:
7232 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7233 break;
7234 case 14:
7235 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7236 break;
79e53945
JB
7237 }
7238
b4c09f3b 7239 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 7240 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
7241 else
7242 dpll |= PLL_REF_INPUT_DREFCLK;
7243
959e16d6 7244 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
7245}
7246
7247static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
7248 int x, int y,
7249 struct drm_framebuffer *fb)
7250{
7251 struct drm_device *dev = crtc->dev;
de13a2e3 7252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
de13a2e3
PZ
7253 int num_connectors = 0;
7254 intel_clock_t clock, reduced_clock;
cbbab5bd 7255 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 7256 bool ok, has_reduced_clock = false;
8b47047b 7257 bool is_lvds = false;
de13a2e3 7258 struct intel_encoder *encoder;
e2b78267 7259 struct intel_shared_dpll *pll;
de13a2e3
PZ
7260
7261 for_each_encoder_on_crtc(dev, crtc, encoder) {
7262 switch (encoder->type) {
7263 case INTEL_OUTPUT_LVDS:
7264 is_lvds = true;
7265 break;
de13a2e3
PZ
7266 }
7267
7268 num_connectors++;
a07d6787 7269 }
79e53945 7270
5dc5298b
PZ
7271 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7272 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 7273
ff9a6750 7274 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 7275 &has_reduced_clock, &reduced_clock);
ee9300bb 7276 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
7277 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7278 return -EINVAL;
79e53945 7279 }
f47709a9
DV
7280 /* Compat-code for transition, will disappear. */
7281 if (!intel_crtc->config.clock_set) {
7282 intel_crtc->config.dpll.n = clock.n;
7283 intel_crtc->config.dpll.m1 = clock.m1;
7284 intel_crtc->config.dpll.m2 = clock.m2;
7285 intel_crtc->config.dpll.p1 = clock.p1;
7286 intel_crtc->config.dpll.p2 = clock.p2;
7287 }
79e53945 7288
5dc5298b 7289 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 7290 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 7291 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 7292 if (has_reduced_clock)
7429e9d4 7293 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 7294
7429e9d4 7295 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
7296 &fp, &reduced_clock,
7297 has_reduced_clock ? &fp2 : NULL);
7298
959e16d6 7299 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
7300 intel_crtc->config.dpll_hw_state.fp0 = fp;
7301 if (has_reduced_clock)
7302 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7303 else
7304 intel_crtc->config.dpll_hw_state.fp1 = fp;
7305
b89a1d39 7306 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 7307 if (pll == NULL) {
84f44ce7 7308 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
29407aab 7309 pipe_name(intel_crtc->pipe));
4b645f14
JB
7310 return -EINVAL;
7311 }
ee7b9f93 7312 } else
e72f9fbf 7313 intel_put_shared_dpll(intel_crtc);
79e53945 7314
d330a953 7315 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
7316 intel_crtc->lowfreq_avail = true;
7317 else
7318 intel_crtc->lowfreq_avail = false;
e2b78267 7319
c8f7a0db 7320 return 0;
79e53945
JB
7321}
7322
eb14cb74
VS
7323static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7324 struct intel_link_m_n *m_n)
7325{
7326 struct drm_device *dev = crtc->base.dev;
7327 struct drm_i915_private *dev_priv = dev->dev_private;
7328 enum pipe pipe = crtc->pipe;
7329
7330 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7331 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7332 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7333 & ~TU_SIZE_MASK;
7334 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7335 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7336 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7337}
7338
7339static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7340 enum transcoder transcoder,
b95af8be
VK
7341 struct intel_link_m_n *m_n,
7342 struct intel_link_m_n *m2_n2)
72419203
DV
7343{
7344 struct drm_device *dev = crtc->base.dev;
7345 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 7346 enum pipe pipe = crtc->pipe;
72419203 7347
eb14cb74
VS
7348 if (INTEL_INFO(dev)->gen >= 5) {
7349 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7350 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7351 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7352 & ~TU_SIZE_MASK;
7353 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7354 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7355 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
7356 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7357 * gen < 8) and if DRRS is supported (to make sure the
7358 * registers are not unnecessarily read).
7359 */
7360 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7361 crtc->config.has_drrs) {
7362 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7363 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7364 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7365 & ~TU_SIZE_MASK;
7366 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7367 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7368 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7369 }
eb14cb74
VS
7370 } else {
7371 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7372 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7373 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7374 & ~TU_SIZE_MASK;
7375 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7376 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7377 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7378 }
7379}
7380
7381void intel_dp_get_m_n(struct intel_crtc *crtc,
7382 struct intel_crtc_config *pipe_config)
7383{
7384 if (crtc->config.has_pch_encoder)
7385 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7386 else
7387 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
7388 &pipe_config->dp_m_n,
7389 &pipe_config->dp_m2_n2);
eb14cb74 7390}
72419203 7391
eb14cb74
VS
7392static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7393 struct intel_crtc_config *pipe_config)
7394{
7395 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 7396 &pipe_config->fdi_m_n, NULL);
72419203
DV
7397}
7398
2fa2fe9a
DV
7399static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7400 struct intel_crtc_config *pipe_config)
7401{
7402 struct drm_device *dev = crtc->base.dev;
7403 struct drm_i915_private *dev_priv = dev->dev_private;
7404 uint32_t tmp;
7405
7406 tmp = I915_READ(PF_CTL(crtc->pipe));
7407
7408 if (tmp & PF_ENABLE) {
fd4daa9c 7409 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
7410 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7411 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
7412
7413 /* We currently do not free assignements of panel fitters on
7414 * ivb/hsw (since we don't use the higher upscaling modes which
7415 * differentiates them) so just WARN about this case for now. */
7416 if (IS_GEN7(dev)) {
7417 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7418 PF_PIPE_SEL_IVB(crtc->pipe));
7419 }
2fa2fe9a 7420 }
79e53945
JB
7421}
7422
4c6baa59
JB
7423static void ironlake_get_plane_config(struct intel_crtc *crtc,
7424 struct intel_plane_config *plane_config)
7425{
7426 struct drm_device *dev = crtc->base.dev;
7427 struct drm_i915_private *dev_priv = dev->dev_private;
7428 u32 val, base, offset;
7429 int pipe = crtc->pipe, plane = crtc->plane;
7430 int fourcc, pixel_format;
7431 int aligned_height;
7432
66e514c1
DA
7433 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7434 if (!crtc->base.primary->fb) {
4c6baa59
JB
7435 DRM_DEBUG_KMS("failed to alloc fb\n");
7436 return;
7437 }
7438
7439 val = I915_READ(DSPCNTR(plane));
7440
7441 if (INTEL_INFO(dev)->gen >= 4)
7442 if (val & DISPPLANE_TILED)
7443 plane_config->tiled = true;
7444
7445 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7446 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
7447 crtc->base.primary->fb->pixel_format = fourcc;
7448 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
7449 drm_format_plane_cpp(fourcc, 0) * 8;
7450
7451 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7452 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7453 offset = I915_READ(DSPOFFSET(plane));
7454 } else {
7455 if (plane_config->tiled)
7456 offset = I915_READ(DSPTILEOFF(plane));
7457 else
7458 offset = I915_READ(DSPLINOFF(plane));
7459 }
7460 plane_config->base = base;
7461
7462 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
7463 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7464 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
7465
7466 val = I915_READ(DSPSTRIDE(pipe));
026b96e2 7467 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
4c6baa59 7468
66e514c1 7469 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
7470 plane_config->tiled);
7471
1267a26b
FF
7472 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7473 aligned_height);
4c6baa59
JB
7474
7475 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
7476 pipe, plane, crtc->base.primary->fb->width,
7477 crtc->base.primary->fb->height,
7478 crtc->base.primary->fb->bits_per_pixel, base,
7479 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
7480 plane_config->size);
7481}
7482
0e8ffe1b
DV
7483static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7484 struct intel_crtc_config *pipe_config)
7485{
7486 struct drm_device *dev = crtc->base.dev;
7487 struct drm_i915_private *dev_priv = dev->dev_private;
7488 uint32_t tmp;
7489
930e8c9e
PZ
7490 if (!intel_display_power_enabled(dev_priv,
7491 POWER_DOMAIN_PIPE(crtc->pipe)))
7492 return false;
7493
e143a21c 7494 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7495 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7496
0e8ffe1b
DV
7497 tmp = I915_READ(PIPECONF(crtc->pipe));
7498 if (!(tmp & PIPECONF_ENABLE))
7499 return false;
7500
42571aef
VS
7501 switch (tmp & PIPECONF_BPC_MASK) {
7502 case PIPECONF_6BPC:
7503 pipe_config->pipe_bpp = 18;
7504 break;
7505 case PIPECONF_8BPC:
7506 pipe_config->pipe_bpp = 24;
7507 break;
7508 case PIPECONF_10BPC:
7509 pipe_config->pipe_bpp = 30;
7510 break;
7511 case PIPECONF_12BPC:
7512 pipe_config->pipe_bpp = 36;
7513 break;
7514 default:
7515 break;
7516 }
7517
b5a9fa09
DV
7518 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7519 pipe_config->limited_color_range = true;
7520
ab9412ba 7521 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
7522 struct intel_shared_dpll *pll;
7523
88adfff1
DV
7524 pipe_config->has_pch_encoder = true;
7525
627eb5a3
DV
7526 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7527 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7528 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7529
7530 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 7531
c0d43d62 7532 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
7533 pipe_config->shared_dpll =
7534 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
7535 } else {
7536 tmp = I915_READ(PCH_DPLL_SEL);
7537 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7538 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7539 else
7540 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7541 }
66e985c0
DV
7542
7543 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7544
7545 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7546 &pipe_config->dpll_hw_state));
c93f54cf
DV
7547
7548 tmp = pipe_config->dpll_hw_state.dpll;
7549 pipe_config->pixel_multiplier =
7550 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7551 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
7552
7553 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
7554 } else {
7555 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
7556 }
7557
1bd1bd80
DV
7558 intel_get_pipe_timings(crtc, pipe_config);
7559
2fa2fe9a
DV
7560 ironlake_get_pfit_config(crtc, pipe_config);
7561
0e8ffe1b
DV
7562 return true;
7563}
7564
be256dc7
PZ
7565static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7566{
7567 struct drm_device *dev = dev_priv->dev;
be256dc7 7568 struct intel_crtc *crtc;
be256dc7 7569
d3fcc808 7570 for_each_intel_crtc(dev, crtc)
798183c5 7571 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
7572 pipe_name(crtc->pipe));
7573
7574 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8cc3e169
DV
7575 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7576 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7577 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
be256dc7
PZ
7578 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7579 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7580 "CPU PWM1 enabled\n");
c5107b87
PZ
7581 if (IS_HASWELL(dev))
7582 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7583 "CPU PWM2 enabled\n");
be256dc7
PZ
7584 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7585 "PCH PWM1 enabled\n");
7586 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7587 "Utility pin enabled\n");
7588 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7589
9926ada1
PZ
7590 /*
7591 * In theory we can still leave IRQs enabled, as long as only the HPD
7592 * interrupts remain enabled. We used to check for that, but since it's
7593 * gen-specific and since we only disable LCPLL after we fully disable
7594 * the interrupts, the check below should be enough.
7595 */
9df7575f 7596 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
7597}
7598
9ccd5aeb
PZ
7599static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7600{
7601 struct drm_device *dev = dev_priv->dev;
7602
7603 if (IS_HASWELL(dev))
7604 return I915_READ(D_COMP_HSW);
7605 else
7606 return I915_READ(D_COMP_BDW);
7607}
7608
3c4c9b81
PZ
7609static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7610{
7611 struct drm_device *dev = dev_priv->dev;
7612
7613 if (IS_HASWELL(dev)) {
7614 mutex_lock(&dev_priv->rps.hw_lock);
7615 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7616 val))
f475dadf 7617 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
7618 mutex_unlock(&dev_priv->rps.hw_lock);
7619 } else {
9ccd5aeb
PZ
7620 I915_WRITE(D_COMP_BDW, val);
7621 POSTING_READ(D_COMP_BDW);
3c4c9b81 7622 }
be256dc7
PZ
7623}
7624
7625/*
7626 * This function implements pieces of two sequences from BSpec:
7627 * - Sequence for display software to disable LCPLL
7628 * - Sequence for display software to allow package C8+
7629 * The steps implemented here are just the steps that actually touch the LCPLL
7630 * register. Callers should take care of disabling all the display engine
7631 * functions, doing the mode unset, fixing interrupts, etc.
7632 */
6ff58d53
PZ
7633static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7634 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7635{
7636 uint32_t val;
7637
7638 assert_can_disable_lcpll(dev_priv);
7639
7640 val = I915_READ(LCPLL_CTL);
7641
7642 if (switch_to_fclk) {
7643 val |= LCPLL_CD_SOURCE_FCLK;
7644 I915_WRITE(LCPLL_CTL, val);
7645
7646 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7647 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7648 DRM_ERROR("Switching to FCLK failed\n");
7649
7650 val = I915_READ(LCPLL_CTL);
7651 }
7652
7653 val |= LCPLL_PLL_DISABLE;
7654 I915_WRITE(LCPLL_CTL, val);
7655 POSTING_READ(LCPLL_CTL);
7656
7657 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7658 DRM_ERROR("LCPLL still locked\n");
7659
9ccd5aeb 7660 val = hsw_read_dcomp(dev_priv);
be256dc7 7661 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7662 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7663 ndelay(100);
7664
9ccd5aeb
PZ
7665 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7666 1))
be256dc7
PZ
7667 DRM_ERROR("D_COMP RCOMP still in progress\n");
7668
7669 if (allow_power_down) {
7670 val = I915_READ(LCPLL_CTL);
7671 val |= LCPLL_POWER_DOWN_ALLOW;
7672 I915_WRITE(LCPLL_CTL, val);
7673 POSTING_READ(LCPLL_CTL);
7674 }
7675}
7676
7677/*
7678 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7679 * source.
7680 */
6ff58d53 7681static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7682{
7683 uint32_t val;
a8a8bd54 7684 unsigned long irqflags;
be256dc7
PZ
7685
7686 val = I915_READ(LCPLL_CTL);
7687
7688 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7689 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7690 return;
7691
a8a8bd54
PZ
7692 /*
7693 * Make sure we're not on PC8 state before disabling PC8, otherwise
7694 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7695 *
7696 * The other problem is that hsw_restore_lcpll() is called as part of
7697 * the runtime PM resume sequence, so we can't just call
7698 * gen6_gt_force_wake_get() because that function calls
7699 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7700 * while we are on the resume sequence. So to solve this problem we have
7701 * to call special forcewake code that doesn't touch runtime PM and
7702 * doesn't enable the forcewake delayed work.
7703 */
7704 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7705 if (dev_priv->uncore.forcewake_count++ == 0)
7706 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7707 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7708
be256dc7
PZ
7709 if (val & LCPLL_POWER_DOWN_ALLOW) {
7710 val &= ~LCPLL_POWER_DOWN_ALLOW;
7711 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7712 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7713 }
7714
9ccd5aeb 7715 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
7716 val |= D_COMP_COMP_FORCE;
7717 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7718 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7719
7720 val = I915_READ(LCPLL_CTL);
7721 val &= ~LCPLL_PLL_DISABLE;
7722 I915_WRITE(LCPLL_CTL, val);
7723
7724 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7725 DRM_ERROR("LCPLL not locked yet\n");
7726
7727 if (val & LCPLL_CD_SOURCE_FCLK) {
7728 val = I915_READ(LCPLL_CTL);
7729 val &= ~LCPLL_CD_SOURCE_FCLK;
7730 I915_WRITE(LCPLL_CTL, val);
7731
7732 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7733 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7734 DRM_ERROR("Switching back to LCPLL failed\n");
7735 }
215733fa 7736
a8a8bd54
PZ
7737 /* See the big comment above. */
7738 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7739 if (--dev_priv->uncore.forcewake_count == 0)
7740 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7741 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7742}
7743
765dab67
PZ
7744/*
7745 * Package states C8 and deeper are really deep PC states that can only be
7746 * reached when all the devices on the system allow it, so even if the graphics
7747 * device allows PC8+, it doesn't mean the system will actually get to these
7748 * states. Our driver only allows PC8+ when going into runtime PM.
7749 *
7750 * The requirements for PC8+ are that all the outputs are disabled, the power
7751 * well is disabled and most interrupts are disabled, and these are also
7752 * requirements for runtime PM. When these conditions are met, we manually do
7753 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7754 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7755 * hang the machine.
7756 *
7757 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7758 * the state of some registers, so when we come back from PC8+ we need to
7759 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7760 * need to take care of the registers kept by RC6. Notice that this happens even
7761 * if we don't put the device in PCI D3 state (which is what currently happens
7762 * because of the runtime PM support).
7763 *
7764 * For more, read "Display Sequences for Package C8" on the hardware
7765 * documentation.
7766 */
a14cb6fc 7767void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7768{
c67a470b
PZ
7769 struct drm_device *dev = dev_priv->dev;
7770 uint32_t val;
7771
c67a470b
PZ
7772 DRM_DEBUG_KMS("Enabling package C8+\n");
7773
c67a470b
PZ
7774 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7775 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7776 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7777 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7778 }
7779
7780 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7781 hsw_disable_lcpll(dev_priv, true, true);
7782}
7783
a14cb6fc 7784void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7785{
7786 struct drm_device *dev = dev_priv->dev;
7787 uint32_t val;
7788
c67a470b
PZ
7789 DRM_DEBUG_KMS("Disabling package C8+\n");
7790
7791 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7792 lpt_init_pch_refclk(dev);
7793
7794 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7795 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7796 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7797 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7798 }
7799
7800 intel_prepare_ddi(dev);
c67a470b
PZ
7801}
7802
9a952a0d
PZ
7803static void snb_modeset_global_resources(struct drm_device *dev)
7804{
7805 modeset_update_crtc_power_domains(dev);
7806}
7807
4f074129
ID
7808static void haswell_modeset_global_resources(struct drm_device *dev)
7809{
da723569 7810 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7811}
7812
09b4ddf9 7813static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7814 int x, int y,
7815 struct drm_framebuffer *fb)
7816{
09b4ddf9 7817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7818
566b734a 7819 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7820 return -EINVAL;
716c2e55 7821
644cef34
DV
7822 intel_crtc->lowfreq_avail = false;
7823
c8f7a0db 7824 return 0;
79e53945
JB
7825}
7826
7d2c8175
DL
7827static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7828 enum port port,
7829 struct intel_crtc_config *pipe_config)
7830{
7831 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7832
7833 switch (pipe_config->ddi_pll_sel) {
7834 case PORT_CLK_SEL_WRPLL1:
7835 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7836 break;
7837 case PORT_CLK_SEL_WRPLL2:
7838 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7839 break;
7840 }
7841}
7842
26804afd
DV
7843static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7844 struct intel_crtc_config *pipe_config)
7845{
7846 struct drm_device *dev = crtc->base.dev;
7847 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 7848 struct intel_shared_dpll *pll;
26804afd
DV
7849 enum port port;
7850 uint32_t tmp;
7851
7852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7853
7854 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7855
7d2c8175 7856 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 7857
d452c5b6
DV
7858 if (pipe_config->shared_dpll >= 0) {
7859 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7860
7861 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7862 &pipe_config->dpll_hw_state));
7863 }
7864
26804afd
DV
7865 /*
7866 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7867 * DDI E. So just check whether this pipe is wired to DDI E and whether
7868 * the PCH transcoder is on.
7869 */
ca370455
DL
7870 if (INTEL_INFO(dev)->gen < 9 &&
7871 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
7872 pipe_config->has_pch_encoder = true;
7873
7874 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7875 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7876 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7877
7878 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7879 }
7880}
7881
0e8ffe1b
DV
7882static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7883 struct intel_crtc_config *pipe_config)
7884{
7885 struct drm_device *dev = crtc->base.dev;
7886 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7887 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7888 uint32_t tmp;
7889
b5482bd0
ID
7890 if (!intel_display_power_enabled(dev_priv,
7891 POWER_DOMAIN_PIPE(crtc->pipe)))
7892 return false;
7893
e143a21c 7894 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7895 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7896
eccb140b
DV
7897 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7898 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7899 enum pipe trans_edp_pipe;
7900 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7901 default:
7902 WARN(1, "unknown pipe linked to edp transcoder\n");
7903 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7904 case TRANS_DDI_EDP_INPUT_A_ON:
7905 trans_edp_pipe = PIPE_A;
7906 break;
7907 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7908 trans_edp_pipe = PIPE_B;
7909 break;
7910 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7911 trans_edp_pipe = PIPE_C;
7912 break;
7913 }
7914
7915 if (trans_edp_pipe == crtc->pipe)
7916 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7917 }
7918
da7e29bd 7919 if (!intel_display_power_enabled(dev_priv,
eccb140b 7920 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7921 return false;
7922
eccb140b 7923 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7924 if (!(tmp & PIPECONF_ENABLE))
7925 return false;
7926
26804afd 7927 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 7928
1bd1bd80
DV
7929 intel_get_pipe_timings(crtc, pipe_config);
7930
2fa2fe9a 7931 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7932 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7933 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7934
e59150dc
JB
7935 if (IS_HASWELL(dev))
7936 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7937 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7938
6c49f241
DV
7939 pipe_config->pixel_multiplier = 1;
7940
0e8ffe1b
DV
7941 return true;
7942}
7943
1a91510d
JN
7944static struct {
7945 int clock;
7946 u32 config;
7947} hdmi_audio_clock[] = {
7948 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7949 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7950 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7951 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7952 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7953 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7954 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7955 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7956 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7957 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7958};
7959
7960/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7961static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7962{
7963 int i;
7964
7965 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7966 if (mode->clock == hdmi_audio_clock[i].clock)
7967 break;
7968 }
7969
7970 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7971 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7972 i = 1;
7973 }
7974
7975 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7976 hdmi_audio_clock[i].clock,
7977 hdmi_audio_clock[i].config);
7978
7979 return hdmi_audio_clock[i].config;
7980}
7981
3a9627f4
WF
7982static bool intel_eld_uptodate(struct drm_connector *connector,
7983 int reg_eldv, uint32_t bits_eldv,
7984 int reg_elda, uint32_t bits_elda,
7985 int reg_edid)
7986{
7987 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7988 uint8_t *eld = connector->eld;
7989 uint32_t i;
7990
7991 i = I915_READ(reg_eldv);
7992 i &= bits_eldv;
7993
7994 if (!eld[0])
7995 return !i;
7996
7997 if (!i)
7998 return false;
7999
8000 i = I915_READ(reg_elda);
8001 i &= ~bits_elda;
8002 I915_WRITE(reg_elda, i);
8003
8004 for (i = 0; i < eld[2]; i++)
8005 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
8006 return false;
8007
8008 return true;
8009}
8010
e0dac65e 8011static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
8012 struct drm_crtc *crtc,
8013 struct drm_display_mode *mode)
e0dac65e
WF
8014{
8015 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8016 uint8_t *eld = connector->eld;
8017 uint32_t eldv;
8018 uint32_t len;
8019 uint32_t i;
8020
8021 i = I915_READ(G4X_AUD_VID_DID);
8022
8023 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
8024 eldv = G4X_ELDV_DEVCL_DEVBLC;
8025 else
8026 eldv = G4X_ELDV_DEVCTG;
8027
3a9627f4
WF
8028 if (intel_eld_uptodate(connector,
8029 G4X_AUD_CNTL_ST, eldv,
8030 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
8031 G4X_HDMIW_HDMIEDID))
8032 return;
8033
e0dac65e
WF
8034 i = I915_READ(G4X_AUD_CNTL_ST);
8035 i &= ~(eldv | G4X_ELD_ADDR);
8036 len = (i >> 9) & 0x1f; /* ELD buffer size */
8037 I915_WRITE(G4X_AUD_CNTL_ST, i);
8038
8039 if (!eld[0])
8040 return;
8041
8042 len = min_t(uint8_t, eld[2], len);
8043 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8044 for (i = 0; i < len; i++)
8045 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
8046
8047 i = I915_READ(G4X_AUD_CNTL_ST);
8048 i |= eldv;
8049 I915_WRITE(G4X_AUD_CNTL_ST, i);
8050}
8051
83358c85 8052static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
8053 struct drm_crtc *crtc,
8054 struct drm_display_mode *mode)
83358c85
WX
8055{
8056 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8057 uint8_t *eld = connector->eld;
83358c85
WX
8058 uint32_t eldv;
8059 uint32_t i;
8060 int len;
8061 int pipe = to_intel_crtc(crtc)->pipe;
8062 int tmp;
8063
8064 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
8065 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
8066 int aud_config = HSW_AUD_CFG(pipe);
8067 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
8068
83358c85
WX
8069 /* Audio output enable */
8070 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
8071 tmp = I915_READ(aud_cntrl_st2);
8072 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
8073 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 8074 POSTING_READ(aud_cntrl_st2);
83358c85 8075
c7905792 8076 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
8077
8078 /* Set ELD valid state */
8079 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8080 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
8081 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
8082 I915_WRITE(aud_cntrl_st2, tmp);
8083 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 8084 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
8085
8086 /* Enable HDMI mode */
8087 tmp = I915_READ(aud_config);
7e7cb34f 8088 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
8089 /* clear N_programing_enable and N_value_index */
8090 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8091 I915_WRITE(aud_config, tmp);
8092
8093 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8094
8095 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8096
8097 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8098 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8099 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8100 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8101 } else {
8102 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8103 }
83358c85
WX
8104
8105 if (intel_eld_uptodate(connector,
8106 aud_cntrl_st2, eldv,
8107 aud_cntl_st, IBX_ELD_ADDRESS,
8108 hdmiw_hdmiedid))
8109 return;
8110
8111 i = I915_READ(aud_cntrl_st2);
8112 i &= ~eldv;
8113 I915_WRITE(aud_cntrl_st2, i);
8114
8115 if (!eld[0])
8116 return;
8117
8118 i = I915_READ(aud_cntl_st);
8119 i &= ~IBX_ELD_ADDRESS;
8120 I915_WRITE(aud_cntl_st, i);
8121 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8122 DRM_DEBUG_DRIVER("port num:%d\n", i);
8123
8124 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8125 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8126 for (i = 0; i < len; i++)
8127 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8128
8129 i = I915_READ(aud_cntrl_st2);
8130 i |= eldv;
8131 I915_WRITE(aud_cntrl_st2, i);
8132
8133}
8134
e0dac65e 8135static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
8136 struct drm_crtc *crtc,
8137 struct drm_display_mode *mode)
e0dac65e
WF
8138{
8139 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8140 uint8_t *eld = connector->eld;
8141 uint32_t eldv;
8142 uint32_t i;
8143 int len;
8144 int hdmiw_hdmiedid;
b6daa025 8145 int aud_config;
e0dac65e
WF
8146 int aud_cntl_st;
8147 int aud_cntrl_st2;
9b138a83 8148 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 8149
b3f33cbf 8150 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
8151 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8152 aud_config = IBX_AUD_CFG(pipe);
8153 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 8154 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
8155 } else if (IS_VALLEYVIEW(connector->dev)) {
8156 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8157 aud_config = VLV_AUD_CFG(pipe);
8158 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8159 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 8160 } else {
9b138a83
WX
8161 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8162 aud_config = CPT_AUD_CFG(pipe);
8163 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 8164 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
8165 }
8166
9b138a83 8167 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 8168
9ca2fe73
ML
8169 if (IS_VALLEYVIEW(connector->dev)) {
8170 struct intel_encoder *intel_encoder;
8171 struct intel_digital_port *intel_dig_port;
8172
8173 intel_encoder = intel_attached_encoder(connector);
8174 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8175 i = intel_dig_port->port;
8176 } else {
8177 i = I915_READ(aud_cntl_st);
8178 i = (i >> 29) & DIP_PORT_SEL_MASK;
8179 /* DIP_Port_Select, 0x1 = PortB */
8180 }
8181
e0dac65e
WF
8182 if (!i) {
8183 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8184 /* operate blindly on all ports */
1202b4c6
WF
8185 eldv = IBX_ELD_VALIDB;
8186 eldv |= IBX_ELD_VALIDB << 4;
8187 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 8188 } else {
2582a850 8189 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 8190 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
8191 }
8192
3a9627f4
WF
8193 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8194 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8195 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 8196 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
8197 } else {
8198 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8199 }
e0dac65e 8200
3a9627f4
WF
8201 if (intel_eld_uptodate(connector,
8202 aud_cntrl_st2, eldv,
8203 aud_cntl_st, IBX_ELD_ADDRESS,
8204 hdmiw_hdmiedid))
8205 return;
8206
e0dac65e
WF
8207 i = I915_READ(aud_cntrl_st2);
8208 i &= ~eldv;
8209 I915_WRITE(aud_cntrl_st2, i);
8210
8211 if (!eld[0])
8212 return;
8213
e0dac65e 8214 i = I915_READ(aud_cntl_st);
1202b4c6 8215 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
8216 I915_WRITE(aud_cntl_st, i);
8217
8218 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8219 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8220 for (i = 0; i < len; i++)
8221 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8222
8223 i = I915_READ(aud_cntrl_st2);
8224 i |= eldv;
8225 I915_WRITE(aud_cntrl_st2, i);
8226}
8227
8228void intel_write_eld(struct drm_encoder *encoder,
8229 struct drm_display_mode *mode)
8230{
8231 struct drm_crtc *crtc = encoder->crtc;
8232 struct drm_connector *connector;
8233 struct drm_device *dev = encoder->dev;
8234 struct drm_i915_private *dev_priv = dev->dev_private;
8235
8236 connector = drm_select_eld(encoder, mode);
8237 if (!connector)
8238 return;
8239
8240 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8241 connector->base.id,
c23cc417 8242 connector->name,
e0dac65e 8243 connector->encoder->base.id,
8e329a03 8244 connector->encoder->name);
e0dac65e
WF
8245
8246 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8247
8248 if (dev_priv->display.write_eld)
34427052 8249 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
8250}
8251
560b85bb
CW
8252static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8253{
8254 struct drm_device *dev = crtc->dev;
8255 struct drm_i915_private *dev_priv = dev->dev_private;
8256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 8257 uint32_t cntl = 0, size = 0;
560b85bb 8258
dc41c154
VS
8259 if (base) {
8260 unsigned int width = intel_crtc->cursor_width;
8261 unsigned int height = intel_crtc->cursor_height;
8262 unsigned int stride = roundup_pow_of_two(width) * 4;
8263
8264 switch (stride) {
8265 default:
8266 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8267 width, stride);
8268 stride = 256;
8269 /* fallthrough */
8270 case 256:
8271 case 512:
8272 case 1024:
8273 case 2048:
8274 break;
4b0e333e
CW
8275 }
8276
dc41c154
VS
8277 cntl |= CURSOR_ENABLE |
8278 CURSOR_GAMMA_ENABLE |
8279 CURSOR_FORMAT_ARGB |
8280 CURSOR_STRIDE(stride);
8281
8282 size = (height << 12) | width;
4b0e333e 8283 }
560b85bb 8284
dc41c154
VS
8285 if (intel_crtc->cursor_cntl != 0 &&
8286 (intel_crtc->cursor_base != base ||
8287 intel_crtc->cursor_size != size ||
8288 intel_crtc->cursor_cntl != cntl)) {
8289 /* On these chipsets we can only modify the base/size/stride
8290 * whilst the cursor is disabled.
8291 */
8292 I915_WRITE(_CURACNTR, 0);
4b0e333e 8293 POSTING_READ(_CURACNTR);
dc41c154 8294 intel_crtc->cursor_cntl = 0;
4b0e333e 8295 }
560b85bb 8296
dc41c154 8297 if (intel_crtc->cursor_base != base)
9db4a9c7 8298 I915_WRITE(_CURABASE, base);
4726e0b0 8299
dc41c154
VS
8300 if (intel_crtc->cursor_size != size) {
8301 I915_WRITE(CURSIZE, size);
8302 intel_crtc->cursor_size = size;
4b0e333e 8303 }
560b85bb 8304
4b0e333e 8305 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
8306 I915_WRITE(_CURACNTR, cntl);
8307 POSTING_READ(_CURACNTR);
4b0e333e 8308 intel_crtc->cursor_cntl = cntl;
560b85bb 8309 }
560b85bb
CW
8310}
8311
560b85bb 8312static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
8313{
8314 struct drm_device *dev = crtc->dev;
8315 struct drm_i915_private *dev_priv = dev->dev_private;
8316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8317 int pipe = intel_crtc->pipe;
4b0e333e
CW
8318 uint32_t cntl;
8319
8320 cntl = 0;
8321 if (base) {
8322 cntl = MCURSOR_GAMMA_ENABLE;
8323 switch (intel_crtc->cursor_width) {
4726e0b0
SK
8324 case 64:
8325 cntl |= CURSOR_MODE_64_ARGB_AX;
8326 break;
8327 case 128:
8328 cntl |= CURSOR_MODE_128_ARGB_AX;
8329 break;
8330 case 256:
8331 cntl |= CURSOR_MODE_256_ARGB_AX;
8332 break;
8333 default:
8334 WARN_ON(1);
8335 return;
65a21cd6 8336 }
4b0e333e 8337 cntl |= pipe << 28; /* Connect to correct pipe */
4b0e333e
CW
8338 }
8339 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8340 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 8341
4b0e333e
CW
8342 if (intel_crtc->cursor_cntl != cntl) {
8343 I915_WRITE(CURCNTR(pipe), cntl);
8344 POSTING_READ(CURCNTR(pipe));
8345 intel_crtc->cursor_cntl = cntl;
65a21cd6 8346 }
4b0e333e 8347
65a21cd6 8348 /* and commit changes on next vblank */
5efb3e28
VS
8349 I915_WRITE(CURBASE(pipe), base);
8350 POSTING_READ(CURBASE(pipe));
65a21cd6
JB
8351}
8352
cda4b7d3 8353/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
8354static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8355 bool on)
cda4b7d3
CW
8356{
8357 struct drm_device *dev = crtc->dev;
8358 struct drm_i915_private *dev_priv = dev->dev_private;
8359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8360 int pipe = intel_crtc->pipe;
3d7d6510
MR
8361 int x = crtc->cursor_x;
8362 int y = crtc->cursor_y;
d6e4db15 8363 u32 base = 0, pos = 0;
cda4b7d3 8364
d6e4db15 8365 if (on)
cda4b7d3 8366 base = intel_crtc->cursor_addr;
cda4b7d3 8367
d6e4db15
VS
8368 if (x >= intel_crtc->config.pipe_src_w)
8369 base = 0;
8370
8371 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
8372 base = 0;
8373
8374 if (x < 0) {
efc9064e 8375 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
8376 base = 0;
8377
8378 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8379 x = -x;
8380 }
8381 pos |= x << CURSOR_X_SHIFT;
8382
8383 if (y < 0) {
efc9064e 8384 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
8385 base = 0;
8386
8387 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8388 y = -y;
8389 }
8390 pos |= y << CURSOR_Y_SHIFT;
8391
4b0e333e 8392 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
8393 return;
8394
5efb3e28
VS
8395 I915_WRITE(CURPOS(pipe), pos);
8396
8ac54669 8397 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
8398 i845_update_cursor(crtc, base);
8399 else
8400 i9xx_update_cursor(crtc, base);
4b0e333e 8401 intel_crtc->cursor_base = base;
cda4b7d3
CW
8402}
8403
dc41c154
VS
8404static bool cursor_size_ok(struct drm_device *dev,
8405 uint32_t width, uint32_t height)
8406{
8407 if (width == 0 || height == 0)
8408 return false;
8409
8410 /*
8411 * 845g/865g are special in that they are only limited by
8412 * the width of their cursors, the height is arbitrary up to
8413 * the precision of the register. Everything else requires
8414 * square cursors, limited to a few power-of-two sizes.
8415 */
8416 if (IS_845G(dev) || IS_I865G(dev)) {
8417 if ((width & 63) != 0)
8418 return false;
8419
8420 if (width > (IS_845G(dev) ? 64 : 512))
8421 return false;
8422
8423 if (height > 1023)
8424 return false;
8425 } else {
8426 switch (width | height) {
8427 case 256:
8428 case 128:
8429 if (IS_GEN2(dev))
8430 return false;
8431 case 64:
8432 break;
8433 default:
8434 return false;
8435 }
8436 }
8437
8438 return true;
8439}
8440
e3287951
MR
8441/*
8442 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8443 *
8444 * Note that the object's reference will be consumed if the update fails. If
8445 * the update succeeds, the reference of the old object (if any) will be
8446 * consumed.
8447 */
8448static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8449 struct drm_i915_gem_object *obj,
8450 uint32_t width, uint32_t height)
79e53945
JB
8451{
8452 struct drm_device *dev = crtc->dev;
8453 struct drm_i915_private *dev_priv = dev->dev_private;
8454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 8455 enum pipe pipe = intel_crtc->pipe;
dc41c154 8456 unsigned old_width, stride;
cda4b7d3 8457 uint32_t addr;
3f8bc370 8458 int ret;
79e53945 8459
79e53945 8460 /* if we want to turn off the cursor ignore width and height */
e3287951 8461 if (!obj) {
28c97730 8462 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 8463 addr = 0;
5004417d 8464 mutex_lock(&dev->struct_mutex);
3f8bc370 8465 goto finish;
79e53945
JB
8466 }
8467
4726e0b0 8468 /* Check for which cursor types we support */
dc41c154 8469 if (!cursor_size_ok(dev, width, height)) {
4726e0b0 8470 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
8471 return -EINVAL;
8472 }
8473
dc41c154
VS
8474 stride = roundup_pow_of_two(width) * 4;
8475 if (obj->base.size < stride * height) {
e3287951 8476 DRM_DEBUG_KMS("buffer is too small\n");
34b8686e
DA
8477 ret = -ENOMEM;
8478 goto fail;
79e53945
JB
8479 }
8480
71acb5eb 8481 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 8482 mutex_lock(&dev->struct_mutex);
3d13ef2e 8483 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
8484 unsigned alignment;
8485
d9e86c0e 8486 if (obj->tiling_mode) {
3b25b31f 8487 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
8488 ret = -EINVAL;
8489 goto fail_locked;
8490 }
8491
d6dd6843
PZ
8492 /*
8493 * Global gtt pte registers are special registers which actually
8494 * forward writes to a chunk of system memory. Which means that
8495 * there is no risk that the register values disappear as soon
8496 * as we call intel_runtime_pm_put(), so it is correct to wrap
8497 * only the pin/unpin/fence and not more.
8498 */
8499 intel_runtime_pm_get(dev_priv);
8500
693db184
CW
8501 /* Note that the w/a also requires 2 PTE of padding following
8502 * the bo. We currently fill all unused PTE with the shadow
8503 * page and so we should always have valid PTE following the
8504 * cursor preventing the VT-d warning.
8505 */
8506 alignment = 0;
8507 if (need_vtd_wa(dev))
8508 alignment = 64*1024;
8509
8510 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 8511 if (ret) {
3b25b31f 8512 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
d6dd6843 8513 intel_runtime_pm_put(dev_priv);
2da3b9b9 8514 goto fail_locked;
e7b526bb
CW
8515 }
8516
d9e86c0e
CW
8517 ret = i915_gem_object_put_fence(obj);
8518 if (ret) {
3b25b31f 8519 DRM_DEBUG_KMS("failed to release fence for cursor");
d6dd6843 8520 intel_runtime_pm_put(dev_priv);
d9e86c0e
CW
8521 goto fail_unpin;
8522 }
8523
f343c5f6 8524 addr = i915_gem_obj_ggtt_offset(obj);
d6dd6843
PZ
8525
8526 intel_runtime_pm_put(dev_priv);
71acb5eb 8527 } else {
6eeefaf3 8528 int align = IS_I830(dev) ? 16 * 1024 : 256;
00731155 8529 ret = i915_gem_object_attach_phys(obj, align);
71acb5eb 8530 if (ret) {
3b25b31f 8531 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 8532 goto fail_locked;
71acb5eb 8533 }
00731155 8534 addr = obj->phys_handle->busaddr;
3f8bc370
KH
8535 }
8536
3f8bc370 8537 finish:
3f8bc370 8538 if (intel_crtc->cursor_bo) {
00731155 8539 if (!INTEL_INFO(dev)->cursor_needs_physical)
cc98b413 8540 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
3f8bc370 8541 }
80824003 8542
a071fa00
DV
8543 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8544 INTEL_FRONTBUFFER_CURSOR(pipe));
7f9872e0 8545 mutex_unlock(&dev->struct_mutex);
3f8bc370 8546
64f962e3
CW
8547 old_width = intel_crtc->cursor_width;
8548
3f8bc370 8549 intel_crtc->cursor_addr = addr;
05394f39 8550 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
8551 intel_crtc->cursor_width = width;
8552 intel_crtc->cursor_height = height;
8553
64f962e3
CW
8554 if (intel_crtc->active) {
8555 if (old_width != width)
8556 intel_update_watermarks(crtc);
f2f5f771 8557 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 8558 }
3f8bc370 8559
f99d7069
DV
8560 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8561
79e53945 8562 return 0;
e7b526bb 8563fail_unpin:
cc98b413 8564 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 8565fail_locked:
34b8686e 8566 mutex_unlock(&dev->struct_mutex);
bc9025bd 8567fail:
05394f39 8568 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 8569 return ret;
79e53945
JB
8570}
8571
79e53945 8572static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 8573 u16 *blue, uint32_t start, uint32_t size)
79e53945 8574{
7203425a 8575 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 8576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8577
7203425a 8578 for (i = start; i < end; i++) {
79e53945
JB
8579 intel_crtc->lut_r[i] = red[i] >> 8;
8580 intel_crtc->lut_g[i] = green[i] >> 8;
8581 intel_crtc->lut_b[i] = blue[i] >> 8;
8582 }
8583
8584 intel_crtc_load_lut(crtc);
8585}
8586
79e53945
JB
8587/* VESA 640x480x72Hz mode to set on the pipe */
8588static struct drm_display_mode load_detect_mode = {
8589 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8590 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8591};
8592
a8bb6818
DV
8593struct drm_framebuffer *
8594__intel_framebuffer_create(struct drm_device *dev,
8595 struct drm_mode_fb_cmd2 *mode_cmd,
8596 struct drm_i915_gem_object *obj)
d2dff872
CW
8597{
8598 struct intel_framebuffer *intel_fb;
8599 int ret;
8600
8601 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8602 if (!intel_fb) {
8603 drm_gem_object_unreference_unlocked(&obj->base);
8604 return ERR_PTR(-ENOMEM);
8605 }
8606
8607 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
8608 if (ret)
8609 goto err;
d2dff872
CW
8610
8611 return &intel_fb->base;
dd4916c5
DV
8612err:
8613 drm_gem_object_unreference_unlocked(&obj->base);
8614 kfree(intel_fb);
8615
8616 return ERR_PTR(ret);
d2dff872
CW
8617}
8618
b5ea642a 8619static struct drm_framebuffer *
a8bb6818
DV
8620intel_framebuffer_create(struct drm_device *dev,
8621 struct drm_mode_fb_cmd2 *mode_cmd,
8622 struct drm_i915_gem_object *obj)
8623{
8624 struct drm_framebuffer *fb;
8625 int ret;
8626
8627 ret = i915_mutex_lock_interruptible(dev);
8628 if (ret)
8629 return ERR_PTR(ret);
8630 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8631 mutex_unlock(&dev->struct_mutex);
8632
8633 return fb;
8634}
8635
d2dff872
CW
8636static u32
8637intel_framebuffer_pitch_for_width(int width, int bpp)
8638{
8639 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8640 return ALIGN(pitch, 64);
8641}
8642
8643static u32
8644intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8645{
8646 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 8647 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
8648}
8649
8650static struct drm_framebuffer *
8651intel_framebuffer_create_for_mode(struct drm_device *dev,
8652 struct drm_display_mode *mode,
8653 int depth, int bpp)
8654{
8655 struct drm_i915_gem_object *obj;
0fed39bd 8656 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8657
8658 obj = i915_gem_alloc_object(dev,
8659 intel_framebuffer_size_for_mode(mode, bpp));
8660 if (obj == NULL)
8661 return ERR_PTR(-ENOMEM);
8662
8663 mode_cmd.width = mode->hdisplay;
8664 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8665 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8666 bpp);
5ca0c34a 8667 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8668
8669 return intel_framebuffer_create(dev, &mode_cmd, obj);
8670}
8671
8672static struct drm_framebuffer *
8673mode_fits_in_fbdev(struct drm_device *dev,
8674 struct drm_display_mode *mode)
8675{
4520f53a 8676#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8677 struct drm_i915_private *dev_priv = dev->dev_private;
8678 struct drm_i915_gem_object *obj;
8679 struct drm_framebuffer *fb;
8680
4c0e5528 8681 if (!dev_priv->fbdev)
d2dff872
CW
8682 return NULL;
8683
4c0e5528 8684 if (!dev_priv->fbdev->fb)
d2dff872
CW
8685 return NULL;
8686
4c0e5528
DV
8687 obj = dev_priv->fbdev->fb->obj;
8688 BUG_ON(!obj);
8689
8bcd4553 8690 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8691 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8692 fb->bits_per_pixel))
d2dff872
CW
8693 return NULL;
8694
01f2c773 8695 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8696 return NULL;
8697
8698 return fb;
4520f53a
DV
8699#else
8700 return NULL;
8701#endif
d2dff872
CW
8702}
8703
d2434ab7 8704bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8705 struct drm_display_mode *mode,
51fd371b
RC
8706 struct intel_load_detect_pipe *old,
8707 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
8708{
8709 struct intel_crtc *intel_crtc;
d2434ab7
DV
8710 struct intel_encoder *intel_encoder =
8711 intel_attached_encoder(connector);
79e53945 8712 struct drm_crtc *possible_crtc;
4ef69c7a 8713 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8714 struct drm_crtc *crtc = NULL;
8715 struct drm_device *dev = encoder->dev;
94352cf9 8716 struct drm_framebuffer *fb;
51fd371b
RC
8717 struct drm_mode_config *config = &dev->mode_config;
8718 int ret, i = -1;
79e53945 8719
d2dff872 8720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8721 connector->base.id, connector->name,
8e329a03 8722 encoder->base.id, encoder->name);
d2dff872 8723
51fd371b
RC
8724retry:
8725 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8726 if (ret)
8727 goto fail_unlock;
6e9f798d 8728
79e53945
JB
8729 /*
8730 * Algorithm gets a little messy:
7a5e4805 8731 *
79e53945
JB
8732 * - if the connector already has an assigned crtc, use it (but make
8733 * sure it's on first)
7a5e4805 8734 *
79e53945
JB
8735 * - try to find the first unused crtc that can drive this connector,
8736 * and use that if we find one
79e53945
JB
8737 */
8738
8739 /* See if we already have a CRTC for this connector */
8740 if (encoder->crtc) {
8741 crtc = encoder->crtc;
8261b191 8742
51fd371b
RC
8743 ret = drm_modeset_lock(&crtc->mutex, ctx);
8744 if (ret)
8745 goto fail_unlock;
7b24056b 8746
24218aac 8747 old->dpms_mode = connector->dpms;
8261b191
CW
8748 old->load_detect_temp = false;
8749
8750 /* Make sure the crtc and connector are running */
24218aac
DV
8751 if (connector->dpms != DRM_MODE_DPMS_ON)
8752 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8753
7173188d 8754 return true;
79e53945
JB
8755 }
8756
8757 /* Find an unused one (if possible) */
70e1e0ec 8758 for_each_crtc(dev, possible_crtc) {
79e53945
JB
8759 i++;
8760 if (!(encoder->possible_crtcs & (1 << i)))
8761 continue;
a459249c
VS
8762 if (possible_crtc->enabled)
8763 continue;
8764 /* This can occur when applying the pipe A quirk on resume. */
8765 if (to_intel_crtc(possible_crtc)->new_enabled)
8766 continue;
8767
8768 crtc = possible_crtc;
8769 break;
79e53945
JB
8770 }
8771
8772 /*
8773 * If we didn't find an unused CRTC, don't use any.
8774 */
8775 if (!crtc) {
7173188d 8776 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 8777 goto fail_unlock;
79e53945
JB
8778 }
8779
51fd371b
RC
8780 ret = drm_modeset_lock(&crtc->mutex, ctx);
8781 if (ret)
8782 goto fail_unlock;
fc303101
DV
8783 intel_encoder->new_crtc = to_intel_crtc(crtc);
8784 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8785
8786 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8787 intel_crtc->new_enabled = true;
8788 intel_crtc->new_config = &intel_crtc->config;
24218aac 8789 old->dpms_mode = connector->dpms;
8261b191 8790 old->load_detect_temp = true;
d2dff872 8791 old->release_fb = NULL;
79e53945 8792
6492711d
CW
8793 if (!mode)
8794 mode = &load_detect_mode;
79e53945 8795
d2dff872
CW
8796 /* We need a framebuffer large enough to accommodate all accesses
8797 * that the plane may generate whilst we perform load detection.
8798 * We can not rely on the fbcon either being present (we get called
8799 * during its initialisation to detect all boot displays, or it may
8800 * not even exist) or that it is large enough to satisfy the
8801 * requested mode.
8802 */
94352cf9
DV
8803 fb = mode_fits_in_fbdev(dev, mode);
8804 if (fb == NULL) {
d2dff872 8805 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8806 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8807 old->release_fb = fb;
d2dff872
CW
8808 } else
8809 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8810 if (IS_ERR(fb)) {
d2dff872 8811 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8812 goto fail;
79e53945 8813 }
79e53945 8814
c0c36b94 8815 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8816 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8817 if (old->release_fb)
8818 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8819 goto fail;
79e53945 8820 }
7173188d 8821
79e53945 8822 /* let the connector get through one full cycle before testing */
9d0498a2 8823 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8824 return true;
412b61d8
VS
8825
8826 fail:
8827 intel_crtc->new_enabled = crtc->enabled;
8828 if (intel_crtc->new_enabled)
8829 intel_crtc->new_config = &intel_crtc->config;
8830 else
8831 intel_crtc->new_config = NULL;
51fd371b
RC
8832fail_unlock:
8833 if (ret == -EDEADLK) {
8834 drm_modeset_backoff(ctx);
8835 goto retry;
8836 }
8837
412b61d8 8838 return false;
79e53945
JB
8839}
8840
d2434ab7 8841void intel_release_load_detect_pipe(struct drm_connector *connector,
208bf9fd 8842 struct intel_load_detect_pipe *old)
79e53945 8843{
d2434ab7
DV
8844 struct intel_encoder *intel_encoder =
8845 intel_attached_encoder(connector);
4ef69c7a 8846 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8847 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8849
d2dff872 8850 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 8851 connector->base.id, connector->name,
8e329a03 8852 encoder->base.id, encoder->name);
d2dff872 8853
8261b191 8854 if (old->load_detect_temp) {
fc303101
DV
8855 to_intel_connector(connector)->new_encoder = NULL;
8856 intel_encoder->new_crtc = NULL;
412b61d8
VS
8857 intel_crtc->new_enabled = false;
8858 intel_crtc->new_config = NULL;
fc303101 8859 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8860
36206361
DV
8861 if (old->release_fb) {
8862 drm_framebuffer_unregister_private(old->release_fb);
8863 drm_framebuffer_unreference(old->release_fb);
8864 }
d2dff872 8865
0622a53c 8866 return;
79e53945
JB
8867 }
8868
c751ce4f 8869 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8870 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8871 connector->funcs->dpms(connector, old->dpms_mode);
79e53945
JB
8872}
8873
da4a1efa
VS
8874static int i9xx_pll_refclk(struct drm_device *dev,
8875 const struct intel_crtc_config *pipe_config)
8876{
8877 struct drm_i915_private *dev_priv = dev->dev_private;
8878 u32 dpll = pipe_config->dpll_hw_state.dpll;
8879
8880 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8881 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8882 else if (HAS_PCH_SPLIT(dev))
8883 return 120000;
8884 else if (!IS_GEN2(dev))
8885 return 96000;
8886 else
8887 return 48000;
8888}
8889
79e53945 8890/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8891static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8892 struct intel_crtc_config *pipe_config)
79e53945 8893{
f1f644dc 8894 struct drm_device *dev = crtc->base.dev;
79e53945 8895 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8896 int pipe = pipe_config->cpu_transcoder;
293623f7 8897 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8898 u32 fp;
8899 intel_clock_t clock;
da4a1efa 8900 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8901
8902 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8903 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8904 else
293623f7 8905 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8906
8907 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8908 if (IS_PINEVIEW(dev)) {
8909 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8910 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8911 } else {
8912 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8913 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8914 }
8915
a6c45cf0 8916 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8917 if (IS_PINEVIEW(dev))
8918 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8919 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8920 else
8921 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8922 DPLL_FPA01_P1_POST_DIV_SHIFT);
8923
8924 switch (dpll & DPLL_MODE_MASK) {
8925 case DPLLB_MODE_DAC_SERIAL:
8926 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8927 5 : 10;
8928 break;
8929 case DPLLB_MODE_LVDS:
8930 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8931 7 : 14;
8932 break;
8933 default:
28c97730 8934 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8935 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8936 return;
79e53945
JB
8937 }
8938
ac58c3f0 8939 if (IS_PINEVIEW(dev))
da4a1efa 8940 pineview_clock(refclk, &clock);
ac58c3f0 8941 else
da4a1efa 8942 i9xx_clock(refclk, &clock);
79e53945 8943 } else {
0fb58223 8944 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8945 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8946
8947 if (is_lvds) {
8948 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8949 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8950
8951 if (lvds & LVDS_CLKB_POWER_UP)
8952 clock.p2 = 7;
8953 else
8954 clock.p2 = 14;
79e53945
JB
8955 } else {
8956 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8957 clock.p1 = 2;
8958 else {
8959 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8960 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8961 }
8962 if (dpll & PLL_P2_DIVIDE_BY_4)
8963 clock.p2 = 4;
8964 else
8965 clock.p2 = 2;
79e53945 8966 }
da4a1efa
VS
8967
8968 i9xx_clock(refclk, &clock);
79e53945
JB
8969 }
8970
18442d08
VS
8971 /*
8972 * This value includes pixel_multiplier. We will use
241bfc38 8973 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8974 * encoder's get_config() function.
8975 */
8976 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8977}
8978
6878da05
VS
8979int intel_dotclock_calculate(int link_freq,
8980 const struct intel_link_m_n *m_n)
f1f644dc 8981{
f1f644dc
JB
8982 /*
8983 * The calculation for the data clock is:
1041a02f 8984 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8985 * But we want to avoid losing precison if possible, so:
1041a02f 8986 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8987 *
8988 * and the link clock is simpler:
1041a02f 8989 * link_clock = (m * link_clock) / n
f1f644dc
JB
8990 */
8991
6878da05
VS
8992 if (!m_n->link_n)
8993 return 0;
f1f644dc 8994
6878da05
VS
8995 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8996}
f1f644dc 8997
18442d08
VS
8998static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8999 struct intel_crtc_config *pipe_config)
6878da05
VS
9000{
9001 struct drm_device *dev = crtc->base.dev;
79e53945 9002
18442d08
VS
9003 /* read out port_clock from the DPLL */
9004 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 9005
f1f644dc 9006 /*
18442d08 9007 * This value does not include pixel_multiplier.
241bfc38 9008 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
9009 * agree once we know their relationship in the encoder's
9010 * get_config() function.
79e53945 9011 */
241bfc38 9012 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
9013 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9014 &pipe_config->fdi_m_n);
79e53945
JB
9015}
9016
9017/** Returns the currently programmed mode of the given pipe. */
9018struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9019 struct drm_crtc *crtc)
9020{
548f245b 9021 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 9022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 9023 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 9024 struct drm_display_mode *mode;
f1f644dc 9025 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
9026 int htot = I915_READ(HTOTAL(cpu_transcoder));
9027 int hsync = I915_READ(HSYNC(cpu_transcoder));
9028 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9029 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 9030 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
9031
9032 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9033 if (!mode)
9034 return NULL;
9035
f1f644dc
JB
9036 /*
9037 * Construct a pipe_config sufficient for getting the clock info
9038 * back out of crtc_clock_get.
9039 *
9040 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9041 * to use a real value here instead.
9042 */
293623f7 9043 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 9044 pipe_config.pixel_multiplier = 1;
293623f7
VS
9045 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9046 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9047 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
9048 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9049
773ae034 9050 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
9051 mode->hdisplay = (htot & 0xffff) + 1;
9052 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9053 mode->hsync_start = (hsync & 0xffff) + 1;
9054 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9055 mode->vdisplay = (vtot & 0xffff) + 1;
9056 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9057 mode->vsync_start = (vsync & 0xffff) + 1;
9058 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9059
9060 drm_mode_set_name(mode);
79e53945
JB
9061
9062 return mode;
9063}
9064
cc36513c
DV
9065static void intel_increase_pllclock(struct drm_device *dev,
9066 enum pipe pipe)
652c393a 9067{
fbee40df 9068 struct drm_i915_private *dev_priv = dev->dev_private;
dbdc6479
JB
9069 int dpll_reg = DPLL(pipe);
9070 int dpll;
652c393a 9071
baff296c 9072 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9073 return;
9074
9075 if (!dev_priv->lvds_downclock_avail)
9076 return;
9077
dbdc6479 9078 dpll = I915_READ(dpll_reg);
652c393a 9079 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 9080 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 9081
8ac5a6d5 9082 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
9083
9084 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
9085 I915_WRITE(dpll_reg, dpll);
9d0498a2 9086 intel_wait_for_vblank(dev, pipe);
dbdc6479 9087
652c393a
JB
9088 dpll = I915_READ(dpll_reg);
9089 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 9090 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 9091 }
652c393a
JB
9092}
9093
9094static void intel_decrease_pllclock(struct drm_crtc *crtc)
9095{
9096 struct drm_device *dev = crtc->dev;
fbee40df 9097 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 9099
baff296c 9100 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
9101 return;
9102
9103 if (!dev_priv->lvds_downclock_avail)
9104 return;
9105
9106 /*
9107 * Since this is called by a timer, we should never get here in
9108 * the manual case.
9109 */
9110 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
9111 int pipe = intel_crtc->pipe;
9112 int dpll_reg = DPLL(pipe);
9113 int dpll;
f6e5b160 9114
44d98a61 9115 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 9116
8ac5a6d5 9117 assert_panel_unlocked(dev_priv, pipe);
652c393a 9118
dc257cf1 9119 dpll = I915_READ(dpll_reg);
652c393a
JB
9120 dpll |= DISPLAY_RATE_SELECT_FPA1;
9121 I915_WRITE(dpll_reg, dpll);
9d0498a2 9122 intel_wait_for_vblank(dev, pipe);
652c393a
JB
9123 dpll = I915_READ(dpll_reg);
9124 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 9125 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
9126 }
9127
9128}
9129
f047e395
CW
9130void intel_mark_busy(struct drm_device *dev)
9131{
c67a470b
PZ
9132 struct drm_i915_private *dev_priv = dev->dev_private;
9133
f62a0076
CW
9134 if (dev_priv->mm.busy)
9135 return;
9136
43694d69 9137 intel_runtime_pm_get(dev_priv);
c67a470b 9138 i915_update_gfx_val(dev_priv);
f62a0076 9139 dev_priv->mm.busy = true;
f047e395
CW
9140}
9141
9142void intel_mark_idle(struct drm_device *dev)
652c393a 9143{
c67a470b 9144 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 9145 struct drm_crtc *crtc;
652c393a 9146
f62a0076
CW
9147 if (!dev_priv->mm.busy)
9148 return;
9149
9150 dev_priv->mm.busy = false;
9151
d330a953 9152 if (!i915.powersave)
bb4cdd53 9153 goto out;
652c393a 9154
70e1e0ec 9155 for_each_crtc(dev, crtc) {
f4510a27 9156 if (!crtc->primary->fb)
652c393a
JB
9157 continue;
9158
725a5b54 9159 intel_decrease_pllclock(crtc);
652c393a 9160 }
b29c19b6 9161
3d13ef2e 9162 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 9163 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
9164
9165out:
43694d69 9166 intel_runtime_pm_put(dev_priv);
652c393a
JB
9167}
9168
7c8f8a70 9169
f99d7069
DV
9170/**
9171 * intel_mark_fb_busy - mark given planes as busy
9172 * @dev: DRM device
9173 * @frontbuffer_bits: bits for the affected planes
9174 * @ring: optional ring for asynchronous commands
9175 *
9176 * This function gets called every time the screen contents change. It can be
9177 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9178 */
9179static void intel_mark_fb_busy(struct drm_device *dev,
9180 unsigned frontbuffer_bits,
9181 struct intel_engine_cs *ring)
652c393a 9182{
055e393f 9183 struct drm_i915_private *dev_priv = dev->dev_private;
cc36513c 9184 enum pipe pipe;
652c393a 9185
d330a953 9186 if (!i915.powersave)
acb87dfb
CW
9187 return;
9188
055e393f 9189 for_each_pipe(dev_priv, pipe) {
f99d7069 9190 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
c65355bb
CW
9191 continue;
9192
cc36513c 9193 intel_increase_pllclock(dev, pipe);
c65355bb
CW
9194 if (ring && intel_fbc_enabled(dev))
9195 ring->fbc_dirty = true;
652c393a
JB
9196 }
9197}
9198
f99d7069
DV
9199/**
9200 * intel_fb_obj_invalidate - invalidate frontbuffer object
9201 * @obj: GEM object to invalidate
9202 * @ring: set for asynchronous rendering
9203 *
9204 * This function gets called every time rendering on the given object starts and
9205 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9206 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9207 * until the rendering completes or a flip on this frontbuffer plane is
9208 * scheduled.
9209 */
9210void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9211 struct intel_engine_cs *ring)
9212{
9213 struct drm_device *dev = obj->base.dev;
9214 struct drm_i915_private *dev_priv = dev->dev_private;
9215
9216 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9217
9218 if (!obj->frontbuffer_bits)
9219 return;
9220
9221 if (ring) {
9222 mutex_lock(&dev_priv->fb_tracking.lock);
9223 dev_priv->fb_tracking.busy_bits
9224 |= obj->frontbuffer_bits;
9225 dev_priv->fb_tracking.flip_bits
9226 &= ~obj->frontbuffer_bits;
9227 mutex_unlock(&dev_priv->fb_tracking.lock);
9228 }
9229
9230 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9231
9ca15301 9232 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
f99d7069
DV
9233}
9234
9235/**
9236 * intel_frontbuffer_flush - flush frontbuffer
9237 * @dev: DRM device
9238 * @frontbuffer_bits: frontbuffer plane tracking bits
9239 *
9240 * This function gets called every time rendering on the given planes has
9241 * completed and frontbuffer caching can be started again. Flushes will get
9242 * delayed if they're blocked by some oustanding asynchronous rendering.
9243 *
9244 * Can be called without any locks held.
9245 */
9246void intel_frontbuffer_flush(struct drm_device *dev,
9247 unsigned frontbuffer_bits)
9248{
9249 struct drm_i915_private *dev_priv = dev->dev_private;
9250
9251 /* Delay flushing when rings are still busy.*/
9252 mutex_lock(&dev_priv->fb_tracking.lock);
9253 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9254 mutex_unlock(&dev_priv->fb_tracking.lock);
9255
9256 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9257
9ca15301 9258 intel_edp_psr_flush(dev, frontbuffer_bits);
c5ad011d 9259
c317adcd
VS
9260 /*
9261 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9262 * needs to be reworked into a proper frontbuffer tracking scheme like
9263 * psr employs.
9264 */
9265 if (IS_BROADWELL(dev))
c5ad011d 9266 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
f99d7069
DV
9267}
9268
9269/**
9270 * intel_fb_obj_flush - flush frontbuffer object
9271 * @obj: GEM object to flush
9272 * @retire: set when retiring asynchronous rendering
9273 *
9274 * This function gets called every time rendering on the given object has
9275 * completed and frontbuffer caching can be started again. If @retire is true
9276 * then any delayed flushes will be unblocked.
9277 */
9278void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9279 bool retire)
9280{
9281 struct drm_device *dev = obj->base.dev;
9282 struct drm_i915_private *dev_priv = dev->dev_private;
9283 unsigned frontbuffer_bits;
9284
9285 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9286
9287 if (!obj->frontbuffer_bits)
9288 return;
9289
9290 frontbuffer_bits = obj->frontbuffer_bits;
9291
9292 if (retire) {
9293 mutex_lock(&dev_priv->fb_tracking.lock);
9294 /* Filter out new bits since rendering started. */
9295 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9296
9297 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9298 mutex_unlock(&dev_priv->fb_tracking.lock);
9299 }
9300
9301 intel_frontbuffer_flush(dev, frontbuffer_bits);
9302}
9303
9304/**
9305 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9306 * @dev: DRM device
9307 * @frontbuffer_bits: frontbuffer plane tracking bits
9308 *
9309 * This function gets called after scheduling a flip on @obj. The actual
9310 * frontbuffer flushing will be delayed until completion is signalled with
9311 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9312 * flush will be cancelled.
9313 *
9314 * Can be called without any locks held.
9315 */
9316void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9317 unsigned frontbuffer_bits)
9318{
9319 struct drm_i915_private *dev_priv = dev->dev_private;
9320
9321 mutex_lock(&dev_priv->fb_tracking.lock);
9322 dev_priv->fb_tracking.flip_bits
9323 |= frontbuffer_bits;
9324 mutex_unlock(&dev_priv->fb_tracking.lock);
9325}
9326
9327/**
9328 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9329 * @dev: DRM device
9330 * @frontbuffer_bits: frontbuffer plane tracking bits
9331 *
9332 * This function gets called after the flip has been latched and will complete
9333 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9334 *
9335 * Can be called without any locks held.
9336 */
9337void intel_frontbuffer_flip_complete(struct drm_device *dev,
9338 unsigned frontbuffer_bits)
9339{
9340 struct drm_i915_private *dev_priv = dev->dev_private;
9341
9342 mutex_lock(&dev_priv->fb_tracking.lock);
9343 /* Mask any cancelled flips. */
9344 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9345 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9346 mutex_unlock(&dev_priv->fb_tracking.lock);
9347
9348 intel_frontbuffer_flush(dev, frontbuffer_bits);
9349}
9350
79e53945
JB
9351static void intel_crtc_destroy(struct drm_crtc *crtc)
9352{
9353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
9354 struct drm_device *dev = crtc->dev;
9355 struct intel_unpin_work *work;
9356 unsigned long flags;
9357
9358 spin_lock_irqsave(&dev->event_lock, flags);
9359 work = intel_crtc->unpin_work;
9360 intel_crtc->unpin_work = NULL;
9361 spin_unlock_irqrestore(&dev->event_lock, flags);
9362
9363 if (work) {
9364 cancel_work_sync(&work->work);
9365 kfree(work);
9366 }
79e53945
JB
9367
9368 drm_crtc_cleanup(crtc);
67e77c5a 9369
79e53945
JB
9370 kfree(intel_crtc);
9371}
9372
6b95a207
KH
9373static void intel_unpin_work_fn(struct work_struct *__work)
9374{
9375 struct intel_unpin_work *work =
9376 container_of(__work, struct intel_unpin_work, work);
b4a98e57 9377 struct drm_device *dev = work->crtc->dev;
f99d7069 9378 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 9379
b4a98e57 9380 mutex_lock(&dev->struct_mutex);
1690e1eb 9381 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
9382 drm_gem_object_unreference(&work->pending_flip_obj->base);
9383 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 9384
b4a98e57
CW
9385 intel_update_fbc(dev);
9386 mutex_unlock(&dev->struct_mutex);
9387
f99d7069
DV
9388 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9389
b4a98e57
CW
9390 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9391 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9392
6b95a207
KH
9393 kfree(work);
9394}
9395
1afe3e9d 9396static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 9397 struct drm_crtc *crtc)
6b95a207 9398{
6b95a207
KH
9399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9400 struct intel_unpin_work *work;
6b95a207
KH
9401 unsigned long flags;
9402
9403 /* Ignore early vblank irqs */
9404 if (intel_crtc == NULL)
9405 return;
9406
9407 spin_lock_irqsave(&dev->event_lock, flags);
9408 work = intel_crtc->unpin_work;
e7d841ca
CW
9409
9410 /* Ensure we don't miss a work->pending update ... */
9411 smp_rmb();
9412
9413 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
9414 spin_unlock_irqrestore(&dev->event_lock, flags);
9415 return;
9416 }
9417
d6bbafa1 9418 page_flip_completed(intel_crtc);
0af7e4df 9419
6b95a207 9420 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
9421}
9422
1afe3e9d
JB
9423void intel_finish_page_flip(struct drm_device *dev, int pipe)
9424{
fbee40df 9425 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9426 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9427
49b14a5c 9428 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9429}
9430
9431void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9432{
fbee40df 9433 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
9434 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9435
49b14a5c 9436 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
9437}
9438
75f7f3ec
VS
9439/* Is 'a' after or equal to 'b'? */
9440static bool g4x_flip_count_after_eq(u32 a, u32 b)
9441{
9442 return !((a - b) & 0x80000000);
9443}
9444
9445static bool page_flip_finished(struct intel_crtc *crtc)
9446{
9447 struct drm_device *dev = crtc->base.dev;
9448 struct drm_i915_private *dev_priv = dev->dev_private;
9449
9450 /*
9451 * The relevant registers doen't exist on pre-ctg.
9452 * As the flip done interrupt doesn't trigger for mmio
9453 * flips on gmch platforms, a flip count check isn't
9454 * really needed there. But since ctg has the registers,
9455 * include it in the check anyway.
9456 */
9457 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9458 return true;
9459
9460 /*
9461 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9462 * used the same base address. In that case the mmio flip might
9463 * have completed, but the CS hasn't even executed the flip yet.
9464 *
9465 * A flip count check isn't enough as the CS might have updated
9466 * the base address just after start of vblank, but before we
9467 * managed to process the interrupt. This means we'd complete the
9468 * CS flip too soon.
9469 *
9470 * Combining both checks should get us a good enough result. It may
9471 * still happen that the CS flip has been executed, but has not
9472 * yet actually completed. But in case the base address is the same
9473 * anyway, we don't really care.
9474 */
9475 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9476 crtc->unpin_work->gtt_offset &&
9477 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9478 crtc->unpin_work->flip_count);
9479}
9480
6b95a207
KH
9481void intel_prepare_page_flip(struct drm_device *dev, int plane)
9482{
fbee40df 9483 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
9484 struct intel_crtc *intel_crtc =
9485 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9486 unsigned long flags;
9487
e7d841ca
CW
9488 /* NB: An MMIO update of the plane base pointer will also
9489 * generate a page-flip completion irq, i.e. every modeset
9490 * is also accompanied by a spurious intel_prepare_page_flip().
9491 */
6b95a207 9492 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 9493 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 9494 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
9495 spin_unlock_irqrestore(&dev->event_lock, flags);
9496}
9497
eba905b2 9498static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
9499{
9500 /* Ensure that the work item is consistent when activating it ... */
9501 smp_wmb();
9502 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9503 /* and that it is marked active as soon as the irq could fire. */
9504 smp_wmb();
9505}
9506
8c9f3aaf
JB
9507static int intel_gen2_queue_flip(struct drm_device *dev,
9508 struct drm_crtc *crtc,
9509 struct drm_framebuffer *fb,
ed8d1975 9510 struct drm_i915_gem_object *obj,
a4872ba6 9511 struct intel_engine_cs *ring,
ed8d1975 9512 uint32_t flags)
8c9f3aaf 9513{
8c9f3aaf 9514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9515 u32 flip_mask;
9516 int ret;
9517
6d90c952 9518 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9519 if (ret)
4fa62c89 9520 return ret;
8c9f3aaf
JB
9521
9522 /* Can't queue multiple flips, so wait for the previous
9523 * one to finish before executing the next.
9524 */
9525 if (intel_crtc->plane)
9526 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9527 else
9528 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9529 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9530 intel_ring_emit(ring, MI_NOOP);
9531 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9532 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9533 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9534 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 9535 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
9536
9537 intel_mark_page_flip_active(intel_crtc);
09246732 9538 __intel_ring_advance(ring);
83d4092b 9539 return 0;
8c9f3aaf
JB
9540}
9541
9542static int intel_gen3_queue_flip(struct drm_device *dev,
9543 struct drm_crtc *crtc,
9544 struct drm_framebuffer *fb,
ed8d1975 9545 struct drm_i915_gem_object *obj,
a4872ba6 9546 struct intel_engine_cs *ring,
ed8d1975 9547 uint32_t flags)
8c9f3aaf 9548{
8c9f3aaf 9549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
9550 u32 flip_mask;
9551 int ret;
9552
6d90c952 9553 ret = intel_ring_begin(ring, 6);
8c9f3aaf 9554 if (ret)
4fa62c89 9555 return ret;
8c9f3aaf
JB
9556
9557 if (intel_crtc->plane)
9558 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9559 else
9560 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
9561 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9562 intel_ring_emit(ring, MI_NOOP);
9563 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9564 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9565 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9566 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
9567 intel_ring_emit(ring, MI_NOOP);
9568
e7d841ca 9569 intel_mark_page_flip_active(intel_crtc);
09246732 9570 __intel_ring_advance(ring);
83d4092b 9571 return 0;
8c9f3aaf
JB
9572}
9573
9574static int intel_gen4_queue_flip(struct drm_device *dev,
9575 struct drm_crtc *crtc,
9576 struct drm_framebuffer *fb,
ed8d1975 9577 struct drm_i915_gem_object *obj,
a4872ba6 9578 struct intel_engine_cs *ring,
ed8d1975 9579 uint32_t flags)
8c9f3aaf
JB
9580{
9581 struct drm_i915_private *dev_priv = dev->dev_private;
9582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9583 uint32_t pf, pipesrc;
9584 int ret;
9585
6d90c952 9586 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9587 if (ret)
4fa62c89 9588 return ret;
8c9f3aaf
JB
9589
9590 /* i965+ uses the linear or tiled offsets from the
9591 * Display Registers (which do not change across a page-flip)
9592 * so we need only reprogram the base address.
9593 */
6d90c952
DV
9594 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9595 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9596 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 9597 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 9598 obj->tiling_mode);
8c9f3aaf
JB
9599
9600 /* XXX Enabling the panel-fitter across page-flip is so far
9601 * untested on non-native modes, so ignore it for now.
9602 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9603 */
9604 pf = 0;
9605 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9606 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9607
9608 intel_mark_page_flip_active(intel_crtc);
09246732 9609 __intel_ring_advance(ring);
83d4092b 9610 return 0;
8c9f3aaf
JB
9611}
9612
9613static int intel_gen6_queue_flip(struct drm_device *dev,
9614 struct drm_crtc *crtc,
9615 struct drm_framebuffer *fb,
ed8d1975 9616 struct drm_i915_gem_object *obj,
a4872ba6 9617 struct intel_engine_cs *ring,
ed8d1975 9618 uint32_t flags)
8c9f3aaf
JB
9619{
9620 struct drm_i915_private *dev_priv = dev->dev_private;
9621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9622 uint32_t pf, pipesrc;
9623 int ret;
9624
6d90c952 9625 ret = intel_ring_begin(ring, 4);
8c9f3aaf 9626 if (ret)
4fa62c89 9627 return ret;
8c9f3aaf 9628
6d90c952
DV
9629 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9630 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9631 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 9632 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 9633
dc257cf1
DV
9634 /* Contrary to the suggestions in the documentation,
9635 * "Enable Panel Fitter" does not seem to be required when page
9636 * flipping with a non-native mode, and worse causes a normal
9637 * modeset to fail.
9638 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9639 */
9640 pf = 0;
8c9f3aaf 9641 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 9642 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
9643
9644 intel_mark_page_flip_active(intel_crtc);
09246732 9645 __intel_ring_advance(ring);
83d4092b 9646 return 0;
8c9f3aaf
JB
9647}
9648
7c9017e5
JB
9649static int intel_gen7_queue_flip(struct drm_device *dev,
9650 struct drm_crtc *crtc,
9651 struct drm_framebuffer *fb,
ed8d1975 9652 struct drm_i915_gem_object *obj,
a4872ba6 9653 struct intel_engine_cs *ring,
ed8d1975 9654 uint32_t flags)
7c9017e5 9655{
7c9017e5 9656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 9657 uint32_t plane_bit = 0;
ffe74d75
CW
9658 int len, ret;
9659
eba905b2 9660 switch (intel_crtc->plane) {
cb05d8de
DV
9661 case PLANE_A:
9662 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9663 break;
9664 case PLANE_B:
9665 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9666 break;
9667 case PLANE_C:
9668 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9669 break;
9670 default:
9671 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 9672 return -ENODEV;
cb05d8de
DV
9673 }
9674
ffe74d75 9675 len = 4;
f476828a 9676 if (ring->id == RCS) {
ffe74d75 9677 len += 6;
f476828a
DL
9678 /*
9679 * On Gen 8, SRM is now taking an extra dword to accommodate
9680 * 48bits addresses, and we need a NOOP for the batch size to
9681 * stay even.
9682 */
9683 if (IS_GEN8(dev))
9684 len += 2;
9685 }
ffe74d75 9686
f66fab8e
VS
9687 /*
9688 * BSpec MI_DISPLAY_FLIP for IVB:
9689 * "The full packet must be contained within the same cache line."
9690 *
9691 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9692 * cacheline, if we ever start emitting more commands before
9693 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9694 * then do the cacheline alignment, and finally emit the
9695 * MI_DISPLAY_FLIP.
9696 */
9697 ret = intel_ring_cacheline_align(ring);
9698 if (ret)
4fa62c89 9699 return ret;
f66fab8e 9700
ffe74d75 9701 ret = intel_ring_begin(ring, len);
7c9017e5 9702 if (ret)
4fa62c89 9703 return ret;
7c9017e5 9704
ffe74d75
CW
9705 /* Unmask the flip-done completion message. Note that the bspec says that
9706 * we should do this for both the BCS and RCS, and that we must not unmask
9707 * more than one flip event at any time (or ensure that one flip message
9708 * can be sent by waiting for flip-done prior to queueing new flips).
9709 * Experimentation says that BCS works despite DERRMR masking all
9710 * flip-done completion events and that unmasking all planes at once
9711 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9712 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9713 */
9714 if (ring->id == RCS) {
9715 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9716 intel_ring_emit(ring, DERRMR);
9717 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9718 DERRMR_PIPEB_PRI_FLIP_DONE |
9719 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
9720 if (IS_GEN8(dev))
9721 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9722 MI_SRM_LRM_GLOBAL_GTT);
9723 else
9724 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9725 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
9726 intel_ring_emit(ring, DERRMR);
9727 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
9728 if (IS_GEN8(dev)) {
9729 intel_ring_emit(ring, 0);
9730 intel_ring_emit(ring, MI_NOOP);
9731 }
ffe74d75
CW
9732 }
9733
cb05d8de 9734 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 9735 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 9736 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 9737 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
9738
9739 intel_mark_page_flip_active(intel_crtc);
09246732 9740 __intel_ring_advance(ring);
83d4092b 9741 return 0;
7c9017e5
JB
9742}
9743
84c33a64
SG
9744static bool use_mmio_flip(struct intel_engine_cs *ring,
9745 struct drm_i915_gem_object *obj)
9746{
9747 /*
9748 * This is not being used for older platforms, because
9749 * non-availability of flip done interrupt forces us to use
9750 * CS flips. Older platforms derive flip done using some clever
9751 * tricks involving the flip_pending status bits and vblank irqs.
9752 * So using MMIO flips there would disrupt this mechanism.
9753 */
9754
8e09bf83
CW
9755 if (ring == NULL)
9756 return true;
9757
84c33a64
SG
9758 if (INTEL_INFO(ring->dev)->gen < 5)
9759 return false;
9760
9761 if (i915.use_mmio_flip < 0)
9762 return false;
9763 else if (i915.use_mmio_flip > 0)
9764 return true;
14bf993e
OM
9765 else if (i915.enable_execlists)
9766 return true;
84c33a64
SG
9767 else
9768 return ring != obj->ring;
9769}
9770
9771static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9772{
9773 struct drm_device *dev = intel_crtc->base.dev;
9774 struct drm_i915_private *dev_priv = dev->dev_private;
9775 struct intel_framebuffer *intel_fb =
9776 to_intel_framebuffer(intel_crtc->base.primary->fb);
9777 struct drm_i915_gem_object *obj = intel_fb->obj;
9778 u32 dspcntr;
9779 u32 reg;
9780
9781 intel_mark_page_flip_active(intel_crtc);
9782
9783 reg = DSPCNTR(intel_crtc->plane);
9784 dspcntr = I915_READ(reg);
9785
9786 if (INTEL_INFO(dev)->gen >= 4) {
9787 if (obj->tiling_mode != I915_TILING_NONE)
9788 dspcntr |= DISPPLANE_TILED;
9789 else
9790 dspcntr &= ~DISPPLANE_TILED;
9791 }
9792 I915_WRITE(reg, dspcntr);
9793
9794 I915_WRITE(DSPSURF(intel_crtc->plane),
9795 intel_crtc->unpin_work->gtt_offset);
9796 POSTING_READ(DSPSURF(intel_crtc->plane));
9797}
9798
9799static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9800{
9801 struct intel_engine_cs *ring;
9802 int ret;
9803
9804 lockdep_assert_held(&obj->base.dev->struct_mutex);
9805
9806 if (!obj->last_write_seqno)
9807 return 0;
9808
9809 ring = obj->ring;
9810
9811 if (i915_seqno_passed(ring->get_seqno(ring, true),
9812 obj->last_write_seqno))
9813 return 0;
9814
9815 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9816 if (ret)
9817 return ret;
9818
9819 if (WARN_ON(!ring->irq_get(ring)))
9820 return 0;
9821
9822 return 1;
9823}
9824
9825void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9826{
9827 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9828 struct intel_crtc *intel_crtc;
9829 unsigned long irq_flags;
9830 u32 seqno;
9831
9832 seqno = ring->get_seqno(ring, false);
9833
9834 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9835 for_each_intel_crtc(ring->dev, intel_crtc) {
9836 struct intel_mmio_flip *mmio_flip;
9837
9838 mmio_flip = &intel_crtc->mmio_flip;
9839 if (mmio_flip->seqno == 0)
9840 continue;
9841
9842 if (ring->id != mmio_flip->ring_id)
9843 continue;
9844
9845 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9846 intel_do_mmio_flip(intel_crtc);
9847 mmio_flip->seqno = 0;
9848 ring->irq_put(ring);
9849 }
9850 }
9851 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9852}
9853
9854static int intel_queue_mmio_flip(struct drm_device *dev,
9855 struct drm_crtc *crtc,
9856 struct drm_framebuffer *fb,
9857 struct drm_i915_gem_object *obj,
9858 struct intel_engine_cs *ring,
9859 uint32_t flags)
9860{
9861 struct drm_i915_private *dev_priv = dev->dev_private;
9862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9863 unsigned long irq_flags;
9864 int ret;
9865
9866 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9867 return -EBUSY;
9868
9869 ret = intel_postpone_flip(obj);
9870 if (ret < 0)
9871 return ret;
9872 if (ret == 0) {
9873 intel_do_mmio_flip(intel_crtc);
9874 return 0;
9875 }
9876
9877 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9878 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9879 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9880 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9881
9882 /*
9883 * Double check to catch cases where irq fired before
9884 * mmio flip data was ready
9885 */
9886 intel_notify_mmio_flip(obj->ring);
9887 return 0;
9888}
9889
8c9f3aaf
JB
9890static int intel_default_queue_flip(struct drm_device *dev,
9891 struct drm_crtc *crtc,
9892 struct drm_framebuffer *fb,
ed8d1975 9893 struct drm_i915_gem_object *obj,
a4872ba6 9894 struct intel_engine_cs *ring,
ed8d1975 9895 uint32_t flags)
8c9f3aaf
JB
9896{
9897 return -ENODEV;
9898}
9899
d6bbafa1
CW
9900static bool __intel_pageflip_stall_check(struct drm_device *dev,
9901 struct drm_crtc *crtc)
9902{
9903 struct drm_i915_private *dev_priv = dev->dev_private;
9904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9905 struct intel_unpin_work *work = intel_crtc->unpin_work;
9906 u32 addr;
9907
9908 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9909 return true;
9910
9911 if (!work->enable_stall_check)
9912 return false;
9913
9914 if (work->flip_ready_vblank == 0) {
9915 if (work->flip_queued_ring &&
9916 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9917 work->flip_queued_seqno))
9918 return false;
9919
9920 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9921 }
9922
9923 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9924 return false;
9925
9926 /* Potential stall - if we see that the flip has happened,
9927 * assume a missed interrupt. */
9928 if (INTEL_INFO(dev)->gen >= 4)
9929 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9930 else
9931 addr = I915_READ(DSPADDR(intel_crtc->plane));
9932
9933 /* There is a potential issue here with a false positive after a flip
9934 * to the same address. We could address this by checking for a
9935 * non-incrementing frame counter.
9936 */
9937 return addr == work->gtt_offset;
9938}
9939
9940void intel_check_page_flip(struct drm_device *dev, int pipe)
9941{
9942 struct drm_i915_private *dev_priv = dev->dev_private;
9943 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9944 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9945 unsigned long flags;
9946
9947 if (crtc == NULL)
9948 return;
9949
9950 spin_lock_irqsave(&dev->event_lock, flags);
9951 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9952 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9953 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9954 page_flip_completed(intel_crtc);
9955 }
9956 spin_unlock_irqrestore(&dev->event_lock, flags);
9957}
9958
6b95a207
KH
9959static int intel_crtc_page_flip(struct drm_crtc *crtc,
9960 struct drm_framebuffer *fb,
ed8d1975
KP
9961 struct drm_pending_vblank_event *event,
9962 uint32_t page_flip_flags)
6b95a207
KH
9963{
9964 struct drm_device *dev = crtc->dev;
9965 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9966 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 9967 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 9968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a071fa00 9969 enum pipe pipe = intel_crtc->pipe;
6b95a207 9970 struct intel_unpin_work *work;
a4872ba6 9971 struct intel_engine_cs *ring;
8c9f3aaf 9972 unsigned long flags;
52e68630 9973 int ret;
6b95a207 9974
c76bb61a
DS
9975 //trigger software GT busyness calculation
9976 gen8_flip_interrupt(dev);
9977
2ff8fde1
MR
9978 /*
9979 * drm_mode_page_flip_ioctl() should already catch this, but double
9980 * check to be safe. In the future we may enable pageflipping from
9981 * a disabled primary plane.
9982 */
9983 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9984 return -EBUSY;
9985
e6a595d2 9986 /* Can't change pixel format via MI display flips. */
f4510a27 9987 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9988 return -EINVAL;
9989
9990 /*
9991 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9992 * Note that pitch changes could also affect these register.
9993 */
9994 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9995 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9996 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9997 return -EINVAL;
9998
f900db47
CW
9999 if (i915_terminally_wedged(&dev_priv->gpu_error))
10000 goto out_hang;
10001
b14c5679 10002 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10003 if (work == NULL)
10004 return -ENOMEM;
10005
6b95a207 10006 work->event = event;
b4a98e57 10007 work->crtc = crtc;
2ff8fde1 10008 work->old_fb_obj = intel_fb_obj(old_fb);
6b95a207
KH
10009 INIT_WORK(&work->work, intel_unpin_work_fn);
10010
87b6b101 10011 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10012 if (ret)
10013 goto free_work;
10014
6b95a207
KH
10015 /* We borrow the event spin lock for protecting unpin_work */
10016 spin_lock_irqsave(&dev->event_lock, flags);
10017 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10018 /* Before declaring the flip queue wedged, check if
10019 * the hardware completed the operation behind our backs.
10020 */
10021 if (__intel_pageflip_stall_check(dev, crtc)) {
10022 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10023 page_flip_completed(intel_crtc);
10024 } else {
10025 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10026 spin_unlock_irqrestore(&dev->event_lock, flags);
468f0b44 10027
d6bbafa1
CW
10028 drm_crtc_vblank_put(crtc);
10029 kfree(work);
10030 return -EBUSY;
10031 }
6b95a207
KH
10032 }
10033 intel_crtc->unpin_work = work;
10034 spin_unlock_irqrestore(&dev->event_lock, flags);
10035
b4a98e57
CW
10036 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10037 flush_workqueue(dev_priv->wq);
10038
79158103
CW
10039 ret = i915_mutex_lock_interruptible(dev);
10040 if (ret)
10041 goto cleanup;
6b95a207 10042
75dfca80 10043 /* Reference the objects for the scheduled work. */
05394f39
CW
10044 drm_gem_object_reference(&work->old_fb_obj->base);
10045 drm_gem_object_reference(&obj->base);
6b95a207 10046
f4510a27 10047 crtc->primary->fb = fb;
96b099fd 10048
e1f99ce6 10049 work->pending_flip_obj = obj;
e1f99ce6 10050
b4a98e57 10051 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10052 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10053
75f7f3ec 10054 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10055 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10056
4fa62c89
VS
10057 if (IS_VALLEYVIEW(dev)) {
10058 ring = &dev_priv->ring[BCS];
8e09bf83
CW
10059 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
10060 /* vlv: DISPLAY_FLIP fails to change tiling */
10061 ring = NULL;
2a92d5bc
CW
10062 } else if (IS_IVYBRIDGE(dev)) {
10063 ring = &dev_priv->ring[BCS];
4fa62c89
VS
10064 } else if (INTEL_INFO(dev)->gen >= 7) {
10065 ring = obj->ring;
10066 if (ring == NULL || ring->id != RCS)
10067 ring = &dev_priv->ring[BCS];
10068 } else {
10069 ring = &dev_priv->ring[RCS];
10070 }
10071
10072 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf
JB
10073 if (ret)
10074 goto cleanup_pending;
6b95a207 10075
4fa62c89
VS
10076 work->gtt_offset =
10077 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
10078
d6bbafa1 10079 if (use_mmio_flip(ring, obj)) {
84c33a64
SG
10080 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10081 page_flip_flags);
d6bbafa1
CW
10082 if (ret)
10083 goto cleanup_unpin;
10084
10085 work->flip_queued_seqno = obj->last_write_seqno;
10086 work->flip_queued_ring = obj->ring;
10087 } else {
84c33a64 10088 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10089 page_flip_flags);
10090 if (ret)
10091 goto cleanup_unpin;
10092
10093 work->flip_queued_seqno = intel_ring_get_seqno(ring);
10094 work->flip_queued_ring = ring;
10095 }
10096
10097 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
10098 work->enable_stall_check = true;
4fa62c89 10099
a071fa00
DV
10100 i915_gem_track_fb(work->old_fb_obj, obj,
10101 INTEL_FRONTBUFFER_PRIMARY(pipe));
10102
7782de3b 10103 intel_disable_fbc(dev);
f99d7069 10104 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
10105 mutex_unlock(&dev->struct_mutex);
10106
e5510fac
JB
10107 trace_i915_flip_request(intel_crtc->plane, obj);
10108
6b95a207 10109 return 0;
96b099fd 10110
4fa62c89
VS
10111cleanup_unpin:
10112 intel_unpin_fb_obj(obj);
8c9f3aaf 10113cleanup_pending:
b4a98e57 10114 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 10115 crtc->primary->fb = old_fb;
05394f39
CW
10116 drm_gem_object_unreference(&work->old_fb_obj->base);
10117 drm_gem_object_unreference(&obj->base);
96b099fd
CW
10118 mutex_unlock(&dev->struct_mutex);
10119
79158103 10120cleanup:
96b099fd
CW
10121 spin_lock_irqsave(&dev->event_lock, flags);
10122 intel_crtc->unpin_work = NULL;
10123 spin_unlock_irqrestore(&dev->event_lock, flags);
10124
87b6b101 10125 drm_crtc_vblank_put(crtc);
7317c75e 10126free_work:
96b099fd
CW
10127 kfree(work);
10128
f900db47
CW
10129 if (ret == -EIO) {
10130out_hang:
10131 intel_crtc_wait_for_pending_flips(crtc);
10132 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
10133 if (ret == 0 && event)
a071fa00 10134 drm_send_vblank_event(dev, pipe, event);
f900db47 10135 }
96b099fd 10136 return ret;
6b95a207
KH
10137}
10138
f6e5b160 10139static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
10140 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10141 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
10142};
10143
9a935856
DV
10144/**
10145 * intel_modeset_update_staged_output_state
10146 *
10147 * Updates the staged output configuration state, e.g. after we've read out the
10148 * current hw state.
10149 */
10150static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 10151{
7668851f 10152 struct intel_crtc *crtc;
9a935856
DV
10153 struct intel_encoder *encoder;
10154 struct intel_connector *connector;
f6e5b160 10155
9a935856
DV
10156 list_for_each_entry(connector, &dev->mode_config.connector_list,
10157 base.head) {
10158 connector->new_encoder =
10159 to_intel_encoder(connector->base.encoder);
10160 }
f6e5b160 10161
b2784e15 10162 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10163 encoder->new_crtc =
10164 to_intel_crtc(encoder->base.crtc);
10165 }
7668851f 10166
d3fcc808 10167 for_each_intel_crtc(dev, crtc) {
7668851f 10168 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
10169
10170 if (crtc->new_enabled)
10171 crtc->new_config = &crtc->config;
10172 else
10173 crtc->new_config = NULL;
7668851f 10174 }
f6e5b160
CW
10175}
10176
9a935856
DV
10177/**
10178 * intel_modeset_commit_output_state
10179 *
10180 * This function copies the stage display pipe configuration to the real one.
10181 */
10182static void intel_modeset_commit_output_state(struct drm_device *dev)
10183{
7668851f 10184 struct intel_crtc *crtc;
9a935856
DV
10185 struct intel_encoder *encoder;
10186 struct intel_connector *connector;
f6e5b160 10187
9a935856
DV
10188 list_for_each_entry(connector, &dev->mode_config.connector_list,
10189 base.head) {
10190 connector->base.encoder = &connector->new_encoder->base;
10191 }
f6e5b160 10192
b2784e15 10193 for_each_intel_encoder(dev, encoder) {
9a935856
DV
10194 encoder->base.crtc = &encoder->new_crtc->base;
10195 }
7668851f 10196
d3fcc808 10197 for_each_intel_crtc(dev, crtc) {
7668851f
VS
10198 crtc->base.enabled = crtc->new_enabled;
10199 }
9a935856
DV
10200}
10201
050f7aeb 10202static void
eba905b2 10203connected_sink_compute_bpp(struct intel_connector *connector,
050f7aeb
DV
10204 struct intel_crtc_config *pipe_config)
10205{
10206 int bpp = pipe_config->pipe_bpp;
10207
10208 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10209 connector->base.base.id,
c23cc417 10210 connector->base.name);
050f7aeb
DV
10211
10212 /* Don't use an invalid EDID bpc value */
10213 if (connector->base.display_info.bpc &&
10214 connector->base.display_info.bpc * 3 < bpp) {
10215 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10216 bpp, connector->base.display_info.bpc*3);
10217 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10218 }
10219
10220 /* Clamp bpp to 8 on screens without EDID 1.4 */
10221 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10222 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10223 bpp);
10224 pipe_config->pipe_bpp = 24;
10225 }
10226}
10227
4e53c2e0 10228static int
050f7aeb
DV
10229compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10230 struct drm_framebuffer *fb,
10231 struct intel_crtc_config *pipe_config)
4e53c2e0 10232{
050f7aeb
DV
10233 struct drm_device *dev = crtc->base.dev;
10234 struct intel_connector *connector;
4e53c2e0
DV
10235 int bpp;
10236
d42264b1
DV
10237 switch (fb->pixel_format) {
10238 case DRM_FORMAT_C8:
4e53c2e0
DV
10239 bpp = 8*3; /* since we go through a colormap */
10240 break;
d42264b1
DV
10241 case DRM_FORMAT_XRGB1555:
10242 case DRM_FORMAT_ARGB1555:
10243 /* checked in intel_framebuffer_init already */
10244 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10245 return -EINVAL;
10246 case DRM_FORMAT_RGB565:
4e53c2e0
DV
10247 bpp = 6*3; /* min is 18bpp */
10248 break;
d42264b1
DV
10249 case DRM_FORMAT_XBGR8888:
10250 case DRM_FORMAT_ABGR8888:
10251 /* checked in intel_framebuffer_init already */
10252 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10253 return -EINVAL;
10254 case DRM_FORMAT_XRGB8888:
10255 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
10256 bpp = 8*3;
10257 break;
d42264b1
DV
10258 case DRM_FORMAT_XRGB2101010:
10259 case DRM_FORMAT_ARGB2101010:
10260 case DRM_FORMAT_XBGR2101010:
10261 case DRM_FORMAT_ABGR2101010:
10262 /* checked in intel_framebuffer_init already */
10263 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 10264 return -EINVAL;
4e53c2e0
DV
10265 bpp = 10*3;
10266 break;
baba133a 10267 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
10268 default:
10269 DRM_DEBUG_KMS("unsupported depth\n");
10270 return -EINVAL;
10271 }
10272
4e53c2e0
DV
10273 pipe_config->pipe_bpp = bpp;
10274
10275 /* Clamp display bpp to EDID value */
10276 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 10277 base.head) {
1b829e05
DV
10278 if (!connector->new_encoder ||
10279 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
10280 continue;
10281
050f7aeb 10282 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
10283 }
10284
10285 return bpp;
10286}
10287
644db711
DV
10288static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10289{
10290 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10291 "type: 0x%x flags: 0x%x\n",
1342830c 10292 mode->crtc_clock,
644db711
DV
10293 mode->crtc_hdisplay, mode->crtc_hsync_start,
10294 mode->crtc_hsync_end, mode->crtc_htotal,
10295 mode->crtc_vdisplay, mode->crtc_vsync_start,
10296 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10297}
10298
c0b03411
DV
10299static void intel_dump_pipe_config(struct intel_crtc *crtc,
10300 struct intel_crtc_config *pipe_config,
10301 const char *context)
10302{
10303 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10304 context, pipe_name(crtc->pipe));
10305
10306 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10307 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10308 pipe_config->pipe_bpp, pipe_config->dither);
10309 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10310 pipe_config->has_pch_encoder,
10311 pipe_config->fdi_lanes,
10312 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10313 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10314 pipe_config->fdi_m_n.tu);
eb14cb74
VS
10315 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10316 pipe_config->has_dp_encoder,
10317 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10318 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10319 pipe_config->dp_m_n.tu);
b95af8be
VK
10320
10321 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10322 pipe_config->has_dp_encoder,
10323 pipe_config->dp_m2_n2.gmch_m,
10324 pipe_config->dp_m2_n2.gmch_n,
10325 pipe_config->dp_m2_n2.link_m,
10326 pipe_config->dp_m2_n2.link_n,
10327 pipe_config->dp_m2_n2.tu);
10328
c0b03411
DV
10329 DRM_DEBUG_KMS("requested mode:\n");
10330 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10331 DRM_DEBUG_KMS("adjusted mode:\n");
10332 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 10333 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 10334 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
10335 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10336 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
10337 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10338 pipe_config->gmch_pfit.control,
10339 pipe_config->gmch_pfit.pgm_ratios,
10340 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 10341 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 10342 pipe_config->pch_pfit.pos,
fd4daa9c
CW
10343 pipe_config->pch_pfit.size,
10344 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 10345 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 10346 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
10347}
10348
bc079e8b
VS
10349static bool encoders_cloneable(const struct intel_encoder *a,
10350 const struct intel_encoder *b)
accfc0c5 10351{
bc079e8b
VS
10352 /* masks could be asymmetric, so check both ways */
10353 return a == b || (a->cloneable & (1 << b->type) &&
10354 b->cloneable & (1 << a->type));
10355}
10356
10357static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10358 struct intel_encoder *encoder)
10359{
10360 struct drm_device *dev = crtc->base.dev;
10361 struct intel_encoder *source_encoder;
10362
b2784e15 10363 for_each_intel_encoder(dev, source_encoder) {
bc079e8b
VS
10364 if (source_encoder->new_crtc != crtc)
10365 continue;
10366
10367 if (!encoders_cloneable(encoder, source_encoder))
10368 return false;
10369 }
10370
10371 return true;
10372}
10373
10374static bool check_encoder_cloning(struct intel_crtc *crtc)
10375{
10376 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
10377 struct intel_encoder *encoder;
10378
b2784e15 10379 for_each_intel_encoder(dev, encoder) {
bc079e8b 10380 if (encoder->new_crtc != crtc)
accfc0c5
DV
10381 continue;
10382
bc079e8b
VS
10383 if (!check_single_encoder_cloning(crtc, encoder))
10384 return false;
accfc0c5
DV
10385 }
10386
bc079e8b 10387 return true;
accfc0c5
DV
10388}
10389
b8cecdf5
DV
10390static struct intel_crtc_config *
10391intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 10392 struct drm_framebuffer *fb,
b8cecdf5 10393 struct drm_display_mode *mode)
ee7b9f93 10394{
7758a113 10395 struct drm_device *dev = crtc->dev;
7758a113 10396 struct intel_encoder *encoder;
b8cecdf5 10397 struct intel_crtc_config *pipe_config;
e29c22c0
DV
10398 int plane_bpp, ret = -EINVAL;
10399 bool retry = true;
ee7b9f93 10400
bc079e8b 10401 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
10402 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10403 return ERR_PTR(-EINVAL);
10404 }
10405
b8cecdf5
DV
10406 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10407 if (!pipe_config)
7758a113
DV
10408 return ERR_PTR(-ENOMEM);
10409
b8cecdf5
DV
10410 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10411 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 10412
e143a21c
DV
10413 pipe_config->cpu_transcoder =
10414 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 10415 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 10416
2960bc9c
ID
10417 /*
10418 * Sanitize sync polarity flags based on requested ones. If neither
10419 * positive or negative polarity is requested, treat this as meaning
10420 * negative polarity.
10421 */
10422 if (!(pipe_config->adjusted_mode.flags &
10423 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10424 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10425
10426 if (!(pipe_config->adjusted_mode.flags &
10427 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10428 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10429
050f7aeb
DV
10430 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10431 * plane pixel format and any sink constraints into account. Returns the
10432 * source plane bpp so that dithering can be selected on mismatches
10433 * after encoders and crtc also have had their say. */
10434 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10435 fb, pipe_config);
4e53c2e0
DV
10436 if (plane_bpp < 0)
10437 goto fail;
10438
e41a56be
VS
10439 /*
10440 * Determine the real pipe dimensions. Note that stereo modes can
10441 * increase the actual pipe size due to the frame doubling and
10442 * insertion of additional space for blanks between the frame. This
10443 * is stored in the crtc timings. We use the requested mode to do this
10444 * computation to clearly distinguish it from the adjusted mode, which
10445 * can be changed by the connectors in the below retry loop.
10446 */
10447 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10448 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10449 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10450
e29c22c0 10451encoder_retry:
ef1b460d 10452 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 10453 pipe_config->port_clock = 0;
ef1b460d 10454 pipe_config->pixel_multiplier = 1;
ff9a6750 10455
135c81b8 10456 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 10457 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 10458
7758a113
DV
10459 /* Pass our mode to the connectors and the CRTC to give them a chance to
10460 * adjust it according to limitations or connector properties, and also
10461 * a chance to reject the mode entirely.
47f1c6c9 10462 */
b2784e15 10463 for_each_intel_encoder(dev, encoder) {
47f1c6c9 10464
7758a113
DV
10465 if (&encoder->new_crtc->base != crtc)
10466 continue;
7ae89233 10467
efea6e8e
DV
10468 if (!(encoder->compute_config(encoder, pipe_config))) {
10469 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
10470 goto fail;
10471 }
ee7b9f93 10472 }
47f1c6c9 10473
ff9a6750
DV
10474 /* Set default port clock if not overwritten by the encoder. Needs to be
10475 * done afterwards in case the encoder adjusts the mode. */
10476 if (!pipe_config->port_clock)
241bfc38
DL
10477 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10478 * pipe_config->pixel_multiplier;
ff9a6750 10479
a43f6e0f 10480 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 10481 if (ret < 0) {
7758a113
DV
10482 DRM_DEBUG_KMS("CRTC fixup failed\n");
10483 goto fail;
ee7b9f93 10484 }
e29c22c0
DV
10485
10486 if (ret == RETRY) {
10487 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10488 ret = -EINVAL;
10489 goto fail;
10490 }
10491
10492 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10493 retry = false;
10494 goto encoder_retry;
10495 }
10496
4e53c2e0
DV
10497 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10498 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10499 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10500
b8cecdf5 10501 return pipe_config;
7758a113 10502fail:
b8cecdf5 10503 kfree(pipe_config);
e29c22c0 10504 return ERR_PTR(ret);
ee7b9f93 10505}
47f1c6c9 10506
e2e1ed41
DV
10507/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10508 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10509static void
10510intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10511 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
10512{
10513 struct intel_crtc *intel_crtc;
e2e1ed41
DV
10514 struct drm_device *dev = crtc->dev;
10515 struct intel_encoder *encoder;
10516 struct intel_connector *connector;
10517 struct drm_crtc *tmp_crtc;
79e53945 10518
e2e1ed41 10519 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 10520
e2e1ed41
DV
10521 /* Check which crtcs have changed outputs connected to them, these need
10522 * to be part of the prepare_pipes mask. We don't (yet) support global
10523 * modeset across multiple crtcs, so modeset_pipes will only have one
10524 * bit set at most. */
10525 list_for_each_entry(connector, &dev->mode_config.connector_list,
10526 base.head) {
10527 if (connector->base.encoder == &connector->new_encoder->base)
10528 continue;
79e53945 10529
e2e1ed41
DV
10530 if (connector->base.encoder) {
10531 tmp_crtc = connector->base.encoder->crtc;
10532
10533 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10534 }
10535
10536 if (connector->new_encoder)
10537 *prepare_pipes |=
10538 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
10539 }
10540
b2784e15 10541 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
10542 if (encoder->base.crtc == &encoder->new_crtc->base)
10543 continue;
10544
10545 if (encoder->base.crtc) {
10546 tmp_crtc = encoder->base.crtc;
10547
10548 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10549 }
10550
10551 if (encoder->new_crtc)
10552 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
10553 }
10554
7668851f 10555 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 10556 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10557 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 10558 continue;
7e7d76c3 10559
7668851f 10560 if (!intel_crtc->new_enabled)
e2e1ed41 10561 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
10562 else
10563 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
10564 }
10565
e2e1ed41
DV
10566
10567 /* set_mode is also used to update properties on life display pipes. */
10568 intel_crtc = to_intel_crtc(crtc);
7668851f 10569 if (intel_crtc->new_enabled)
e2e1ed41
DV
10570 *prepare_pipes |= 1 << intel_crtc->pipe;
10571
b6c5164d
DV
10572 /*
10573 * For simplicity do a full modeset on any pipe where the output routing
10574 * changed. We could be more clever, but that would require us to be
10575 * more careful with calling the relevant encoder->mode_set functions.
10576 */
e2e1ed41
DV
10577 if (*prepare_pipes)
10578 *modeset_pipes = *prepare_pipes;
10579
10580 /* ... and mask these out. */
10581 *modeset_pipes &= ~(*disable_pipes);
10582 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
10583
10584 /*
10585 * HACK: We don't (yet) fully support global modesets. intel_set_config
10586 * obies this rule, but the modeset restore mode of
10587 * intel_modeset_setup_hw_state does not.
10588 */
10589 *modeset_pipes &= 1 << intel_crtc->pipe;
10590 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
10591
10592 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10593 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 10594}
79e53945 10595
ea9d758d 10596static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 10597{
ea9d758d 10598 struct drm_encoder *encoder;
f6e5b160 10599 struct drm_device *dev = crtc->dev;
f6e5b160 10600
ea9d758d
DV
10601 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10602 if (encoder->crtc == crtc)
10603 return true;
10604
10605 return false;
10606}
10607
10608static void
10609intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10610{
10611 struct intel_encoder *intel_encoder;
10612 struct intel_crtc *intel_crtc;
10613 struct drm_connector *connector;
10614
b2784e15 10615 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
10616 if (!intel_encoder->base.crtc)
10617 continue;
10618
10619 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10620
10621 if (prepare_pipes & (1 << intel_crtc->pipe))
10622 intel_encoder->connectors_active = false;
10623 }
10624
10625 intel_modeset_commit_output_state(dev);
10626
7668851f 10627 /* Double check state. */
d3fcc808 10628 for_each_intel_crtc(dev, intel_crtc) {
7668851f 10629 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
10630 WARN_ON(intel_crtc->new_config &&
10631 intel_crtc->new_config != &intel_crtc->config);
10632 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
10633 }
10634
10635 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10636 if (!connector->encoder || !connector->encoder->crtc)
10637 continue;
10638
10639 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10640
10641 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
10642 struct drm_property *dpms_property =
10643 dev->mode_config.dpms_property;
10644
ea9d758d 10645 connector->dpms = DRM_MODE_DPMS_ON;
662595df 10646 drm_object_property_set_value(&connector->base,
68d34720
DV
10647 dpms_property,
10648 DRM_MODE_DPMS_ON);
ea9d758d
DV
10649
10650 intel_encoder = to_intel_encoder(connector->encoder);
10651 intel_encoder->connectors_active = true;
10652 }
10653 }
10654
10655}
10656
3bd26263 10657static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 10658{
3bd26263 10659 int diff;
f1f644dc
JB
10660
10661 if (clock1 == clock2)
10662 return true;
10663
10664 if (!clock1 || !clock2)
10665 return false;
10666
10667 diff = abs(clock1 - clock2);
10668
10669 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10670 return true;
10671
10672 return false;
10673}
10674
25c5b266
DV
10675#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10676 list_for_each_entry((intel_crtc), \
10677 &(dev)->mode_config.crtc_list, \
10678 base.head) \
0973f18f 10679 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 10680
0e8ffe1b 10681static bool
2fa2fe9a
DV
10682intel_pipe_config_compare(struct drm_device *dev,
10683 struct intel_crtc_config *current_config,
0e8ffe1b
DV
10684 struct intel_crtc_config *pipe_config)
10685{
66e985c0
DV
10686#define PIPE_CONF_CHECK_X(name) \
10687 if (current_config->name != pipe_config->name) { \
10688 DRM_ERROR("mismatch in " #name " " \
10689 "(expected 0x%08x, found 0x%08x)\n", \
10690 current_config->name, \
10691 pipe_config->name); \
10692 return false; \
10693 }
10694
08a24034
DV
10695#define PIPE_CONF_CHECK_I(name) \
10696 if (current_config->name != pipe_config->name) { \
10697 DRM_ERROR("mismatch in " #name " " \
10698 "(expected %i, found %i)\n", \
10699 current_config->name, \
10700 pipe_config->name); \
10701 return false; \
88adfff1
DV
10702 }
10703
b95af8be
VK
10704/* This is required for BDW+ where there is only one set of registers for
10705 * switching between high and low RR.
10706 * This macro can be used whenever a comparison has to be made between one
10707 * hw state and multiple sw state variables.
10708 */
10709#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10710 if ((current_config->name != pipe_config->name) && \
10711 (current_config->alt_name != pipe_config->name)) { \
10712 DRM_ERROR("mismatch in " #name " " \
10713 "(expected %i or %i, found %i)\n", \
10714 current_config->name, \
10715 current_config->alt_name, \
10716 pipe_config->name); \
10717 return false; \
10718 }
10719
1bd1bd80
DV
10720#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10721 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 10722 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
10723 "(expected %i, found %i)\n", \
10724 current_config->name & (mask), \
10725 pipe_config->name & (mask)); \
10726 return false; \
10727 }
10728
5e550656
VS
10729#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10730 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10731 DRM_ERROR("mismatch in " #name " " \
10732 "(expected %i, found %i)\n", \
10733 current_config->name, \
10734 pipe_config->name); \
10735 return false; \
10736 }
10737
bb760063
DV
10738#define PIPE_CONF_QUIRK(quirk) \
10739 ((current_config->quirks | pipe_config->quirks) & (quirk))
10740
eccb140b
DV
10741 PIPE_CONF_CHECK_I(cpu_transcoder);
10742
08a24034
DV
10743 PIPE_CONF_CHECK_I(has_pch_encoder);
10744 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
10745 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10746 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10747 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10748 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10749 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 10750
eb14cb74 10751 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
10752
10753 if (INTEL_INFO(dev)->gen < 8) {
10754 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10755 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10756 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10757 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10758 PIPE_CONF_CHECK_I(dp_m_n.tu);
10759
10760 if (current_config->has_drrs) {
10761 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10762 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10763 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10764 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10765 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10766 }
10767 } else {
10768 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10769 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10770 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10771 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10772 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10773 }
eb14cb74 10774
1bd1bd80
DV
10775 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10776 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10777 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10778 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10779 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10780 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10781
10782 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10783 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10784 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10785 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10786 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10787 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10788
c93f54cf 10789 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 10790 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
10791 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10792 IS_VALLEYVIEW(dev))
10793 PIPE_CONF_CHECK_I(limited_color_range);
6c49f241 10794
9ed109a7
DV
10795 PIPE_CONF_CHECK_I(has_audio);
10796
1bd1bd80
DV
10797 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10798 DRM_MODE_FLAG_INTERLACE);
10799
bb760063
DV
10800 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10801 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10802 DRM_MODE_FLAG_PHSYNC);
10803 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10804 DRM_MODE_FLAG_NHSYNC);
10805 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10806 DRM_MODE_FLAG_PVSYNC);
10807 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10808 DRM_MODE_FLAG_NVSYNC);
10809 }
045ac3b5 10810
37327abd
VS
10811 PIPE_CONF_CHECK_I(pipe_src_w);
10812 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 10813
9953599b
DV
10814 /*
10815 * FIXME: BIOS likes to set up a cloned config with lvds+external
10816 * screen. Since we don't yet re-compute the pipe config when moving
10817 * just the lvds port away to another pipe the sw tracking won't match.
10818 *
10819 * Proper atomic modesets with recomputed global state will fix this.
10820 * Until then just don't check gmch state for inherited modes.
10821 */
10822 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10823 PIPE_CONF_CHECK_I(gmch_pfit.control);
10824 /* pfit ratios are autocomputed by the hw on gen4+ */
10825 if (INTEL_INFO(dev)->gen < 4)
10826 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10827 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10828 }
10829
fd4daa9c
CW
10830 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10831 if (current_config->pch_pfit.enabled) {
10832 PIPE_CONF_CHECK_I(pch_pfit.pos);
10833 PIPE_CONF_CHECK_I(pch_pfit.size);
10834 }
2fa2fe9a 10835
e59150dc
JB
10836 /* BDW+ don't expose a synchronous way to read the state */
10837 if (IS_HASWELL(dev))
10838 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 10839
282740f7
VS
10840 PIPE_CONF_CHECK_I(double_wide);
10841
26804afd
DV
10842 PIPE_CONF_CHECK_X(ddi_pll_sel);
10843
c0d43d62 10844 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 10845 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 10846 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
10847 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10848 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 10849 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
c0d43d62 10850
42571aef
VS
10851 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10852 PIPE_CONF_CHECK_I(pipe_bpp);
10853
a9a7e98a
JB
10854 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10855 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 10856
66e985c0 10857#undef PIPE_CONF_CHECK_X
08a24034 10858#undef PIPE_CONF_CHECK_I
b95af8be 10859#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 10860#undef PIPE_CONF_CHECK_FLAGS
5e550656 10861#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 10862#undef PIPE_CONF_QUIRK
88adfff1 10863
0e8ffe1b
DV
10864 return true;
10865}
10866
91d1b4bd
DV
10867static void
10868check_connector_state(struct drm_device *dev)
8af6cf88 10869{
8af6cf88
DV
10870 struct intel_connector *connector;
10871
10872 list_for_each_entry(connector, &dev->mode_config.connector_list,
10873 base.head) {
10874 /* This also checks the encoder/connector hw state with the
10875 * ->get_hw_state callbacks. */
10876 intel_connector_check_state(connector);
10877
10878 WARN(&connector->new_encoder->base != connector->base.encoder,
10879 "connector's staged encoder doesn't match current encoder\n");
10880 }
91d1b4bd
DV
10881}
10882
10883static void
10884check_encoder_state(struct drm_device *dev)
10885{
10886 struct intel_encoder *encoder;
10887 struct intel_connector *connector;
8af6cf88 10888
b2784e15 10889 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10890 bool enabled = false;
10891 bool active = false;
10892 enum pipe pipe, tracked_pipe;
10893
10894 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10895 encoder->base.base.id,
8e329a03 10896 encoder->base.name);
8af6cf88
DV
10897
10898 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10899 "encoder's stage crtc doesn't match current crtc\n");
10900 WARN(encoder->connectors_active && !encoder->base.crtc,
10901 "encoder's active_connectors set, but no crtc\n");
10902
10903 list_for_each_entry(connector, &dev->mode_config.connector_list,
10904 base.head) {
10905 if (connector->base.encoder != &encoder->base)
10906 continue;
10907 enabled = true;
10908 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10909 active = true;
10910 }
0e32b39c
DA
10911 /*
10912 * for MST connectors if we unplug the connector is gone
10913 * away but the encoder is still connected to a crtc
10914 * until a modeset happens in response to the hotplug.
10915 */
10916 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10917 continue;
10918
8af6cf88
DV
10919 WARN(!!encoder->base.crtc != enabled,
10920 "encoder's enabled state mismatch "
10921 "(expected %i, found %i)\n",
10922 !!encoder->base.crtc, enabled);
10923 WARN(active && !encoder->base.crtc,
10924 "active encoder with no crtc\n");
10925
10926 WARN(encoder->connectors_active != active,
10927 "encoder's computed active state doesn't match tracked active state "
10928 "(expected %i, found %i)\n", active, encoder->connectors_active);
10929
10930 active = encoder->get_hw_state(encoder, &pipe);
10931 WARN(active != encoder->connectors_active,
10932 "encoder's hw state doesn't match sw tracking "
10933 "(expected %i, found %i)\n",
10934 encoder->connectors_active, active);
10935
10936 if (!encoder->base.crtc)
10937 continue;
10938
10939 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10940 WARN(active && pipe != tracked_pipe,
10941 "active encoder's pipe doesn't match"
10942 "(expected %i, found %i)\n",
10943 tracked_pipe, pipe);
10944
10945 }
91d1b4bd
DV
10946}
10947
10948static void
10949check_crtc_state(struct drm_device *dev)
10950{
fbee40df 10951 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
10952 struct intel_crtc *crtc;
10953 struct intel_encoder *encoder;
10954 struct intel_crtc_config pipe_config;
8af6cf88 10955
d3fcc808 10956 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
10957 bool enabled = false;
10958 bool active = false;
10959
045ac3b5
JB
10960 memset(&pipe_config, 0, sizeof(pipe_config));
10961
8af6cf88
DV
10962 DRM_DEBUG_KMS("[CRTC:%d]\n",
10963 crtc->base.base.id);
10964
10965 WARN(crtc->active && !crtc->base.enabled,
10966 "active crtc, but not enabled in sw tracking\n");
10967
b2784e15 10968 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
10969 if (encoder->base.crtc != &crtc->base)
10970 continue;
10971 enabled = true;
10972 if (encoder->connectors_active)
10973 active = true;
10974 }
6c49f241 10975
8af6cf88
DV
10976 WARN(active != crtc->active,
10977 "crtc's computed active state doesn't match tracked active state "
10978 "(expected %i, found %i)\n", active, crtc->active);
10979 WARN(enabled != crtc->base.enabled,
10980 "crtc's computed enabled state doesn't match tracked enabled state "
10981 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10982
0e8ffe1b
DV
10983 active = dev_priv->display.get_pipe_config(crtc,
10984 &pipe_config);
d62cf62a 10985
b6b5d049
VS
10986 /* hw state is inconsistent with the pipe quirk */
10987 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10988 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
10989 active = crtc->active;
10990
b2784e15 10991 for_each_intel_encoder(dev, encoder) {
3eaba51c 10992 enum pipe pipe;
6c49f241
DV
10993 if (encoder->base.crtc != &crtc->base)
10994 continue;
1d37b689 10995 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
10996 encoder->get_config(encoder, &pipe_config);
10997 }
10998
0e8ffe1b
DV
10999 WARN(crtc->active != active,
11000 "crtc active state doesn't match with hw state "
11001 "(expected %i, found %i)\n", crtc->active, active);
11002
c0b03411
DV
11003 if (active &&
11004 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
11005 WARN(1, "pipe state doesn't match!\n");
11006 intel_dump_pipe_config(crtc, &pipe_config,
11007 "[hw state]");
11008 intel_dump_pipe_config(crtc, &crtc->config,
11009 "[sw state]");
11010 }
8af6cf88
DV
11011 }
11012}
11013
91d1b4bd
DV
11014static void
11015check_shared_dpll_state(struct drm_device *dev)
11016{
fbee40df 11017 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
11018 struct intel_crtc *crtc;
11019 struct intel_dpll_hw_state dpll_hw_state;
11020 int i;
5358901f
DV
11021
11022 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11023 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11024 int enabled_crtcs = 0, active_crtcs = 0;
11025 bool active;
11026
11027 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11028
11029 DRM_DEBUG_KMS("%s\n", pll->name);
11030
11031 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11032
11033 WARN(pll->active > pll->refcount,
11034 "more active pll users than references: %i vs %i\n",
11035 pll->active, pll->refcount);
11036 WARN(pll->active && !pll->on,
11037 "pll in active use but not on in sw tracking\n");
35c95375
DV
11038 WARN(pll->on && !pll->active,
11039 "pll in on but not on in use in sw tracking\n");
5358901f
DV
11040 WARN(pll->on != active,
11041 "pll on state mismatch (expected %i, found %i)\n",
11042 pll->on, active);
11043
d3fcc808 11044 for_each_intel_crtc(dev, crtc) {
5358901f
DV
11045 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
11046 enabled_crtcs++;
11047 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11048 active_crtcs++;
11049 }
11050 WARN(pll->active != active_crtcs,
11051 "pll active crtcs mismatch (expected %i, found %i)\n",
11052 pll->active, active_crtcs);
11053 WARN(pll->refcount != enabled_crtcs,
11054 "pll enabled crtcs mismatch (expected %i, found %i)\n",
11055 pll->refcount, enabled_crtcs);
66e985c0
DV
11056
11057 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
11058 sizeof(dpll_hw_state)),
11059 "pll hw state mismatch\n");
5358901f 11060 }
8af6cf88
DV
11061}
11062
91d1b4bd
DV
11063void
11064intel_modeset_check_state(struct drm_device *dev)
11065{
11066 check_connector_state(dev);
11067 check_encoder_state(dev);
11068 check_crtc_state(dev);
11069 check_shared_dpll_state(dev);
11070}
11071
18442d08
VS
11072void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
11073 int dotclock)
11074{
11075 /*
11076 * FDI already provided one idea for the dotclock.
11077 * Yell if the encoder disagrees.
11078 */
241bfc38 11079 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 11080 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 11081 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
11082}
11083
80715b2f
VS
11084static void update_scanline_offset(struct intel_crtc *crtc)
11085{
11086 struct drm_device *dev = crtc->base.dev;
11087
11088 /*
11089 * The scanline counter increments at the leading edge of hsync.
11090 *
11091 * On most platforms it starts counting from vtotal-1 on the
11092 * first active line. That means the scanline counter value is
11093 * always one less than what we would expect. Ie. just after
11094 * start of vblank, which also occurs at start of hsync (on the
11095 * last active line), the scanline counter will read vblank_start-1.
11096 *
11097 * On gen2 the scanline counter starts counting from 1 instead
11098 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11099 * to keep the value positive), instead of adding one.
11100 *
11101 * On HSW+ the behaviour of the scanline counter depends on the output
11102 * type. For DP ports it behaves like most other platforms, but on HDMI
11103 * there's an extra 1 line difference. So we need to add two instead of
11104 * one to the value.
11105 */
11106 if (IS_GEN2(dev)) {
11107 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
11108 int vtotal;
11109
11110 vtotal = mode->crtc_vtotal;
11111 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11112 vtotal /= 2;
11113
11114 crtc->scanline_offset = vtotal - 1;
11115 } else if (HAS_DDI(dev) &&
11116 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
11117 crtc->scanline_offset = 2;
11118 } else
11119 crtc->scanline_offset = 1;
11120}
11121
f30da187
DV
11122static int __intel_set_mode(struct drm_crtc *crtc,
11123 struct drm_display_mode *mode,
11124 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
11125{
11126 struct drm_device *dev = crtc->dev;
fbee40df 11127 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 11128 struct drm_display_mode *saved_mode;
b8cecdf5 11129 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
11130 struct intel_crtc *intel_crtc;
11131 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 11132 int ret = 0;
a6778b3c 11133
4b4b9238 11134 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
11135 if (!saved_mode)
11136 return -ENOMEM;
a6778b3c 11137
e2e1ed41 11138 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
11139 &prepare_pipes, &disable_pipes);
11140
3ac18232 11141 *saved_mode = crtc->mode;
a6778b3c 11142
25c5b266
DV
11143 /* Hack: Because we don't (yet) support global modeset on multiple
11144 * crtcs, we don't keep track of the new mode for more than one crtc.
11145 * Hence simply check whether any bit is set in modeset_pipes in all the
11146 * pieces of code that are not yet converted to deal with mutliple crtcs
11147 * changing their mode at the same time. */
25c5b266 11148 if (modeset_pipes) {
4e53c2e0 11149 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
11150 if (IS_ERR(pipe_config)) {
11151 ret = PTR_ERR(pipe_config);
11152 pipe_config = NULL;
11153
3ac18232 11154 goto out;
25c5b266 11155 }
c0b03411
DV
11156 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11157 "[modeset]");
50741abc 11158 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 11159 }
a6778b3c 11160
30a970c6
JB
11161 /*
11162 * See if the config requires any additional preparation, e.g.
11163 * to adjust global state with pipes off. We need to do this
11164 * here so we can get the modeset_pipe updated config for the new
11165 * mode set on this crtc. For other crtcs we need to use the
11166 * adjusted_mode bits in the crtc directly.
11167 */
c164f833 11168 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 11169 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 11170
c164f833
VS
11171 /* may have added more to prepare_pipes than we should */
11172 prepare_pipes &= ~disable_pipes;
11173 }
11174
460da916
DV
11175 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11176 intel_crtc_disable(&intel_crtc->base);
11177
ea9d758d
DV
11178 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11179 if (intel_crtc->base.enabled)
11180 dev_priv->display.crtc_disable(&intel_crtc->base);
11181 }
a6778b3c 11182
6c4c86f5
DV
11183 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11184 * to set it here already despite that we pass it down the callchain.
f6e5b160 11185 */
b8cecdf5 11186 if (modeset_pipes) {
25c5b266 11187 crtc->mode = *mode;
b8cecdf5
DV
11188 /* mode_set/enable/disable functions rely on a correct pipe
11189 * config. */
11190 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 11191 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
11192
11193 /*
11194 * Calculate and store various constants which
11195 * are later needed by vblank and swap-completion
11196 * timestamping. They are derived from true hwmode.
11197 */
11198 drm_calc_timestamping_constants(crtc,
11199 &pipe_config->adjusted_mode);
b8cecdf5 11200 }
7758a113 11201
ea9d758d
DV
11202 /* Only after disabling all output pipelines that will be changed can we
11203 * update the the output configuration. */
11204 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 11205
47fab737
DV
11206 if (dev_priv->display.modeset_global_resources)
11207 dev_priv->display.modeset_global_resources(dev);
11208
a6778b3c
DV
11209 /* Set up the DPLL and any encoders state that needs to adjust or depend
11210 * on the DPLL.
f6e5b160 11211 */
25c5b266 11212 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
2ff8fde1
MR
11213 struct drm_framebuffer *old_fb = crtc->primary->fb;
11214 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11215 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
4c10794f
DV
11216
11217 mutex_lock(&dev->struct_mutex);
11218 ret = intel_pin_and_fence_fb_obj(dev,
a071fa00 11219 obj,
4c10794f
DV
11220 NULL);
11221 if (ret != 0) {
11222 DRM_ERROR("pin & fence failed\n");
11223 mutex_unlock(&dev->struct_mutex);
11224 goto done;
11225 }
2ff8fde1 11226 if (old_fb)
a071fa00 11227 intel_unpin_fb_obj(old_obj);
a071fa00
DV
11228 i915_gem_track_fb(old_obj, obj,
11229 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
4c10794f
DV
11230 mutex_unlock(&dev->struct_mutex);
11231
11232 crtc->primary->fb = fb;
11233 crtc->x = x;
11234 crtc->y = y;
11235
4271b753
DV
11236 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11237 x, y, fb);
c0c36b94
CW
11238 if (ret)
11239 goto done;
a6778b3c
DV
11240 }
11241
11242 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
11243 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11244 update_scanline_offset(intel_crtc);
11245
25c5b266 11246 dev_priv->display.crtc_enable(&intel_crtc->base);
80715b2f 11247 }
a6778b3c 11248
a6778b3c
DV
11249 /* FIXME: add subpixel order */
11250done:
4b4b9238 11251 if (ret && crtc->enabled)
3ac18232 11252 crtc->mode = *saved_mode;
a6778b3c 11253
3ac18232 11254out:
b8cecdf5 11255 kfree(pipe_config);
3ac18232 11256 kfree(saved_mode);
a6778b3c 11257 return ret;
f6e5b160
CW
11258}
11259
e7457a9a
DL
11260static int intel_set_mode(struct drm_crtc *crtc,
11261 struct drm_display_mode *mode,
11262 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
11263{
11264 int ret;
11265
11266 ret = __intel_set_mode(crtc, mode, x, y, fb);
11267
11268 if (ret == 0)
11269 intel_modeset_check_state(crtc->dev);
11270
11271 return ret;
11272}
11273
c0c36b94
CW
11274void intel_crtc_restore_mode(struct drm_crtc *crtc)
11275{
f4510a27 11276 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
11277}
11278
25c5b266
DV
11279#undef for_each_intel_crtc_masked
11280
d9e55608
DV
11281static void intel_set_config_free(struct intel_set_config *config)
11282{
11283 if (!config)
11284 return;
11285
1aa4b628
DV
11286 kfree(config->save_connector_encoders);
11287 kfree(config->save_encoder_crtcs);
7668851f 11288 kfree(config->save_crtc_enabled);
d9e55608
DV
11289 kfree(config);
11290}
11291
85f9eb71
DV
11292static int intel_set_config_save_state(struct drm_device *dev,
11293 struct intel_set_config *config)
11294{
7668851f 11295 struct drm_crtc *crtc;
85f9eb71
DV
11296 struct drm_encoder *encoder;
11297 struct drm_connector *connector;
11298 int count;
11299
7668851f
VS
11300 config->save_crtc_enabled =
11301 kcalloc(dev->mode_config.num_crtc,
11302 sizeof(bool), GFP_KERNEL);
11303 if (!config->save_crtc_enabled)
11304 return -ENOMEM;
11305
1aa4b628
DV
11306 config->save_encoder_crtcs =
11307 kcalloc(dev->mode_config.num_encoder,
11308 sizeof(struct drm_crtc *), GFP_KERNEL);
11309 if (!config->save_encoder_crtcs)
85f9eb71
DV
11310 return -ENOMEM;
11311
1aa4b628
DV
11312 config->save_connector_encoders =
11313 kcalloc(dev->mode_config.num_connector,
11314 sizeof(struct drm_encoder *), GFP_KERNEL);
11315 if (!config->save_connector_encoders)
85f9eb71
DV
11316 return -ENOMEM;
11317
11318 /* Copy data. Note that driver private data is not affected.
11319 * Should anything bad happen only the expected state is
11320 * restored, not the drivers personal bookkeeping.
11321 */
7668851f 11322 count = 0;
70e1e0ec 11323 for_each_crtc(dev, crtc) {
7668851f
VS
11324 config->save_crtc_enabled[count++] = crtc->enabled;
11325 }
11326
85f9eb71
DV
11327 count = 0;
11328 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 11329 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
11330 }
11331
11332 count = 0;
11333 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 11334 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
11335 }
11336
11337 return 0;
11338}
11339
11340static void intel_set_config_restore_state(struct drm_device *dev,
11341 struct intel_set_config *config)
11342{
7668851f 11343 struct intel_crtc *crtc;
9a935856
DV
11344 struct intel_encoder *encoder;
11345 struct intel_connector *connector;
85f9eb71
DV
11346 int count;
11347
7668851f 11348 count = 0;
d3fcc808 11349 for_each_intel_crtc(dev, crtc) {
7668851f 11350 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
11351
11352 if (crtc->new_enabled)
11353 crtc->new_config = &crtc->config;
11354 else
11355 crtc->new_config = NULL;
7668851f
VS
11356 }
11357
85f9eb71 11358 count = 0;
b2784e15 11359 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11360 encoder->new_crtc =
11361 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
11362 }
11363
11364 count = 0;
9a935856
DV
11365 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11366 connector->new_encoder =
11367 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
11368 }
11369}
11370
e3de42b6 11371static bool
2e57f47d 11372is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
11373{
11374 int i;
11375
2e57f47d
CW
11376 if (set->num_connectors == 0)
11377 return false;
11378
11379 if (WARN_ON(set->connectors == NULL))
11380 return false;
11381
11382 for (i = 0; i < set->num_connectors; i++)
11383 if (set->connectors[i]->encoder &&
11384 set->connectors[i]->encoder->crtc == set->crtc &&
11385 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
11386 return true;
11387
11388 return false;
11389}
11390
5e2b584e
DV
11391static void
11392intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11393 struct intel_set_config *config)
11394{
11395
11396 /* We should be able to check here if the fb has the same properties
11397 * and then just flip_or_move it */
2e57f47d
CW
11398 if (is_crtc_connector_off(set)) {
11399 config->mode_changed = true;
f4510a27 11400 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
11401 /*
11402 * If we have no fb, we can only flip as long as the crtc is
11403 * active, otherwise we need a full mode set. The crtc may
11404 * be active if we've only disabled the primary plane, or
11405 * in fastboot situations.
11406 */
f4510a27 11407 if (set->crtc->primary->fb == NULL) {
319d9827
JB
11408 struct intel_crtc *intel_crtc =
11409 to_intel_crtc(set->crtc);
11410
3b150f08 11411 if (intel_crtc->active) {
319d9827
JB
11412 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11413 config->fb_changed = true;
11414 } else {
11415 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11416 config->mode_changed = true;
11417 }
5e2b584e
DV
11418 } else if (set->fb == NULL) {
11419 config->mode_changed = true;
72f4901e 11420 } else if (set->fb->pixel_format !=
f4510a27 11421 set->crtc->primary->fb->pixel_format) {
5e2b584e 11422 config->mode_changed = true;
e3de42b6 11423 } else {
5e2b584e 11424 config->fb_changed = true;
e3de42b6 11425 }
5e2b584e
DV
11426 }
11427
835c5873 11428 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
11429 config->fb_changed = true;
11430
11431 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11432 DRM_DEBUG_KMS("modes are different, full mode set\n");
11433 drm_mode_debug_printmodeline(&set->crtc->mode);
11434 drm_mode_debug_printmodeline(set->mode);
11435 config->mode_changed = true;
11436 }
a1d95703
CW
11437
11438 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11439 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
11440}
11441
2e431051 11442static int
9a935856
DV
11443intel_modeset_stage_output_state(struct drm_device *dev,
11444 struct drm_mode_set *set,
11445 struct intel_set_config *config)
50f56119 11446{
9a935856
DV
11447 struct intel_connector *connector;
11448 struct intel_encoder *encoder;
7668851f 11449 struct intel_crtc *crtc;
f3f08572 11450 int ro;
50f56119 11451
9abdda74 11452 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
11453 * of connectors. For paranoia, double-check this. */
11454 WARN_ON(!set->fb && (set->num_connectors != 0));
11455 WARN_ON(set->fb && (set->num_connectors == 0));
11456
9a935856
DV
11457 list_for_each_entry(connector, &dev->mode_config.connector_list,
11458 base.head) {
11459 /* Otherwise traverse passed in connector list and get encoders
11460 * for them. */
50f56119 11461 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11462 if (set->connectors[ro] == &connector->base) {
0e32b39c 11463 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
11464 break;
11465 }
11466 }
11467
9a935856
DV
11468 /* If we disable the crtc, disable all its connectors. Also, if
11469 * the connector is on the changing crtc but not on the new
11470 * connector list, disable it. */
11471 if ((!set->fb || ro == set->num_connectors) &&
11472 connector->base.encoder &&
11473 connector->base.encoder->crtc == set->crtc) {
11474 connector->new_encoder = NULL;
11475
11476 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11477 connector->base.base.id,
c23cc417 11478 connector->base.name);
9a935856
DV
11479 }
11480
11481
11482 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 11483 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 11484 config->mode_changed = true;
50f56119
DV
11485 }
11486 }
9a935856 11487 /* connector->new_encoder is now updated for all connectors. */
50f56119 11488
9a935856 11489 /* Update crtc of enabled connectors. */
9a935856
DV
11490 list_for_each_entry(connector, &dev->mode_config.connector_list,
11491 base.head) {
7668851f
VS
11492 struct drm_crtc *new_crtc;
11493
9a935856 11494 if (!connector->new_encoder)
50f56119
DV
11495 continue;
11496
9a935856 11497 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
11498
11499 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 11500 if (set->connectors[ro] == &connector->base)
50f56119
DV
11501 new_crtc = set->crtc;
11502 }
11503
11504 /* Make sure the new CRTC will work with the encoder */
14509916
TR
11505 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11506 new_crtc)) {
5e2b584e 11507 return -EINVAL;
50f56119 11508 }
0e32b39c 11509 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856
DV
11510
11511 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11512 connector->base.base.id,
c23cc417 11513 connector->base.name,
9a935856
DV
11514 new_crtc->base.id);
11515 }
11516
11517 /* Check for any encoders that needs to be disabled. */
b2784e15 11518 for_each_intel_encoder(dev, encoder) {
5a65f358 11519 int num_connectors = 0;
9a935856
DV
11520 list_for_each_entry(connector,
11521 &dev->mode_config.connector_list,
11522 base.head) {
11523 if (connector->new_encoder == encoder) {
11524 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 11525 num_connectors++;
9a935856
DV
11526 }
11527 }
5a65f358
PZ
11528
11529 if (num_connectors == 0)
11530 encoder->new_crtc = NULL;
11531 else if (num_connectors > 1)
11532 return -EINVAL;
11533
9a935856
DV
11534 /* Only now check for crtc changes so we don't miss encoders
11535 * that will be disabled. */
11536 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 11537 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 11538 config->mode_changed = true;
50f56119
DV
11539 }
11540 }
9a935856 11541 /* Now we've also updated encoder->new_crtc for all encoders. */
0e32b39c
DA
11542 list_for_each_entry(connector, &dev->mode_config.connector_list,
11543 base.head) {
11544 if (connector->new_encoder)
11545 if (connector->new_encoder != connector->encoder)
11546 connector->encoder = connector->new_encoder;
11547 }
d3fcc808 11548 for_each_intel_crtc(dev, crtc) {
7668851f
VS
11549 crtc->new_enabled = false;
11550
b2784e15 11551 for_each_intel_encoder(dev, encoder) {
7668851f
VS
11552 if (encoder->new_crtc == crtc) {
11553 crtc->new_enabled = true;
11554 break;
11555 }
11556 }
11557
11558 if (crtc->new_enabled != crtc->base.enabled) {
11559 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11560 crtc->new_enabled ? "en" : "dis");
11561 config->mode_changed = true;
11562 }
7bd0a8e7
VS
11563
11564 if (crtc->new_enabled)
11565 crtc->new_config = &crtc->config;
11566 else
11567 crtc->new_config = NULL;
7668851f
VS
11568 }
11569
2e431051
DV
11570 return 0;
11571}
11572
7d00a1f5
VS
11573static void disable_crtc_nofb(struct intel_crtc *crtc)
11574{
11575 struct drm_device *dev = crtc->base.dev;
11576 struct intel_encoder *encoder;
11577 struct intel_connector *connector;
11578
11579 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11580 pipe_name(crtc->pipe));
11581
11582 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11583 if (connector->new_encoder &&
11584 connector->new_encoder->new_crtc == crtc)
11585 connector->new_encoder = NULL;
11586 }
11587
b2784e15 11588 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
11589 if (encoder->new_crtc == crtc)
11590 encoder->new_crtc = NULL;
11591 }
11592
11593 crtc->new_enabled = false;
7bd0a8e7 11594 crtc->new_config = NULL;
7d00a1f5
VS
11595}
11596
2e431051
DV
11597static int intel_crtc_set_config(struct drm_mode_set *set)
11598{
11599 struct drm_device *dev;
2e431051
DV
11600 struct drm_mode_set save_set;
11601 struct intel_set_config *config;
11602 int ret;
2e431051 11603
8d3e375e
DV
11604 BUG_ON(!set);
11605 BUG_ON(!set->crtc);
11606 BUG_ON(!set->crtc->helper_private);
2e431051 11607
7e53f3a4
DV
11608 /* Enforce sane interface api - has been abused by the fb helper. */
11609 BUG_ON(!set->mode && set->fb);
11610 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 11611
2e431051
DV
11612 if (set->fb) {
11613 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11614 set->crtc->base.id, set->fb->base.id,
11615 (int)set->num_connectors, set->x, set->y);
11616 } else {
11617 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
11618 }
11619
11620 dev = set->crtc->dev;
11621
11622 ret = -ENOMEM;
11623 config = kzalloc(sizeof(*config), GFP_KERNEL);
11624 if (!config)
11625 goto out_config;
11626
11627 ret = intel_set_config_save_state(dev, config);
11628 if (ret)
11629 goto out_config;
11630
11631 save_set.crtc = set->crtc;
11632 save_set.mode = &set->crtc->mode;
11633 save_set.x = set->crtc->x;
11634 save_set.y = set->crtc->y;
f4510a27 11635 save_set.fb = set->crtc->primary->fb;
2e431051
DV
11636
11637 /* Compute whether we need a full modeset, only an fb base update or no
11638 * change at all. In the future we might also check whether only the
11639 * mode changed, e.g. for LVDS where we only change the panel fitter in
11640 * such cases. */
11641 intel_set_config_compute_mode_changes(set, config);
11642
9a935856 11643 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
11644 if (ret)
11645 goto fail;
11646
5e2b584e 11647 if (config->mode_changed) {
c0c36b94
CW
11648 ret = intel_set_mode(set->crtc, set->mode,
11649 set->x, set->y, set->fb);
5e2b584e 11650 } else if (config->fb_changed) {
3b150f08
MR
11651 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11652
4878cae2
VS
11653 intel_crtc_wait_for_pending_flips(set->crtc);
11654
4f660f49 11655 ret = intel_pipe_set_base(set->crtc,
94352cf9 11656 set->x, set->y, set->fb);
3b150f08
MR
11657
11658 /*
11659 * We need to make sure the primary plane is re-enabled if it
11660 * has previously been turned off.
11661 */
11662 if (!intel_crtc->primary_enabled && ret == 0) {
11663 WARN_ON(!intel_crtc->active);
fdd508a6 11664 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
3b150f08
MR
11665 }
11666
7ca51a3a
JB
11667 /*
11668 * In the fastboot case this may be our only check of the
11669 * state after boot. It would be better to only do it on
11670 * the first update, but we don't have a nice way of doing that
11671 * (and really, set_config isn't used much for high freq page
11672 * flipping, so increasing its cost here shouldn't be a big
11673 * deal).
11674 */
d330a953 11675 if (i915.fastboot && ret == 0)
7ca51a3a 11676 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
11677 }
11678
2d05eae1 11679 if (ret) {
bf67dfeb
DV
11680 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11681 set->crtc->base.id, ret);
50f56119 11682fail:
2d05eae1 11683 intel_set_config_restore_state(dev, config);
50f56119 11684
7d00a1f5
VS
11685 /*
11686 * HACK: if the pipe was on, but we didn't have a framebuffer,
11687 * force the pipe off to avoid oopsing in the modeset code
11688 * due to fb==NULL. This should only happen during boot since
11689 * we don't yet reconstruct the FB from the hardware state.
11690 */
11691 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11692 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11693
2d05eae1
CW
11694 /* Try to restore the config */
11695 if (config->mode_changed &&
11696 intel_set_mode(save_set.crtc, save_set.mode,
11697 save_set.x, save_set.y, save_set.fb))
11698 DRM_ERROR("failed to restore config after modeset failure\n");
11699 }
50f56119 11700
d9e55608
DV
11701out_config:
11702 intel_set_config_free(config);
50f56119
DV
11703 return ret;
11704}
f6e5b160
CW
11705
11706static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 11707 .gamma_set = intel_crtc_gamma_set,
50f56119 11708 .set_config = intel_crtc_set_config,
f6e5b160
CW
11709 .destroy = intel_crtc_destroy,
11710 .page_flip = intel_crtc_page_flip,
11711};
11712
5358901f
DV
11713static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11714 struct intel_shared_dpll *pll,
11715 struct intel_dpll_hw_state *hw_state)
ee7b9f93 11716{
5358901f 11717 uint32_t val;
ee7b9f93 11718
bd2bb1b9
PZ
11719 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11720 return false;
11721
5358901f 11722 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
11723 hw_state->dpll = val;
11724 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11725 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
11726
11727 return val & DPLL_VCO_ENABLE;
11728}
11729
15bdd4cf
DV
11730static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11731 struct intel_shared_dpll *pll)
11732{
11733 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11734 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11735}
11736
e7b903d2
DV
11737static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11738 struct intel_shared_dpll *pll)
11739{
e7b903d2 11740 /* PCH refclock must be enabled first */
89eff4be 11741 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 11742
15bdd4cf
DV
11743 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11744
11745 /* Wait for the clocks to stabilize. */
11746 POSTING_READ(PCH_DPLL(pll->id));
11747 udelay(150);
11748
11749 /* The pixel multiplier can only be updated once the
11750 * DPLL is enabled and the clocks are stable.
11751 *
11752 * So write it again.
11753 */
11754 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11755 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11756 udelay(200);
11757}
11758
11759static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11760 struct intel_shared_dpll *pll)
11761{
11762 struct drm_device *dev = dev_priv->dev;
11763 struct intel_crtc *crtc;
e7b903d2
DV
11764
11765 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 11766 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
11767 if (intel_crtc_to_shared_dpll(crtc) == pll)
11768 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
11769 }
11770
15bdd4cf
DV
11771 I915_WRITE(PCH_DPLL(pll->id), 0);
11772 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
11773 udelay(200);
11774}
11775
46edb027
DV
11776static char *ibx_pch_dpll_names[] = {
11777 "PCH DPLL A",
11778 "PCH DPLL B",
11779};
11780
7c74ade1 11781static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 11782{
e7b903d2 11783 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
11784 int i;
11785
7c74ade1 11786 dev_priv->num_shared_dpll = 2;
ee7b9f93 11787
e72f9fbf 11788 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
11789 dev_priv->shared_dplls[i].id = i;
11790 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 11791 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
11792 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11793 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
11794 dev_priv->shared_dplls[i].get_hw_state =
11795 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
11796 }
11797}
11798
7c74ade1
DV
11799static void intel_shared_dpll_init(struct drm_device *dev)
11800{
e7b903d2 11801 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 11802
9cd86933
DV
11803 if (HAS_DDI(dev))
11804 intel_ddi_pll_init(dev);
11805 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
11806 ibx_pch_dpll_init(dev);
11807 else
11808 dev_priv->num_shared_dpll = 0;
11809
11810 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
11811}
11812
465c120c
MR
11813static int
11814intel_primary_plane_disable(struct drm_plane *plane)
11815{
11816 struct drm_device *dev = plane->dev;
465c120c
MR
11817 struct intel_crtc *intel_crtc;
11818
11819 if (!plane->fb)
11820 return 0;
11821
11822 BUG_ON(!plane->crtc);
11823
11824 intel_crtc = to_intel_crtc(plane->crtc);
11825
11826 /*
11827 * Even though we checked plane->fb above, it's still possible that
11828 * the primary plane has been implicitly disabled because the crtc
11829 * coordinates given weren't visible, or because we detected
11830 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11831 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11832 * In either case, we need to unpin the FB and let the fb pointer get
11833 * updated, but otherwise we don't need to touch the hardware.
11834 */
11835 if (!intel_crtc->primary_enabled)
11836 goto disable_unpin;
11837
11838 intel_crtc_wait_for_pending_flips(plane->crtc);
fdd508a6
VS
11839 intel_disable_primary_hw_plane(plane, plane->crtc);
11840
465c120c 11841disable_unpin:
4c34574f 11842 mutex_lock(&dev->struct_mutex);
2ff8fde1 11843 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
a071fa00 11844 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
2ff8fde1 11845 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
4c34574f 11846 mutex_unlock(&dev->struct_mutex);
465c120c
MR
11847 plane->fb = NULL;
11848
11849 return 0;
11850}
11851
11852static int
11853intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11854 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11855 unsigned int crtc_w, unsigned int crtc_h,
11856 uint32_t src_x, uint32_t src_y,
11857 uint32_t src_w, uint32_t src_h)
11858{
11859 struct drm_device *dev = crtc->dev;
48404c1e 11860 struct drm_i915_private *dev_priv = dev->dev_private;
465c120c 11861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2ff8fde1
MR
11862 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11863 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
465c120c
MR
11864 struct drm_rect dest = {
11865 /* integer pixels */
11866 .x1 = crtc_x,
11867 .y1 = crtc_y,
11868 .x2 = crtc_x + crtc_w,
11869 .y2 = crtc_y + crtc_h,
11870 };
11871 struct drm_rect src = {
11872 /* 16.16 fixed point */
11873 .x1 = src_x,
11874 .y1 = src_y,
11875 .x2 = src_x + src_w,
11876 .y2 = src_y + src_h,
11877 };
11878 const struct drm_rect clip = {
11879 /* integer pixels */
11880 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11881 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11882 };
ce54d85a
SJ
11883 const struct {
11884 int crtc_x, crtc_y;
11885 unsigned int crtc_w, crtc_h;
11886 uint32_t src_x, src_y, src_w, src_h;
11887 } orig = {
11888 .crtc_x = crtc_x,
11889 .crtc_y = crtc_y,
11890 .crtc_w = crtc_w,
11891 .crtc_h = crtc_h,
11892 .src_x = src_x,
11893 .src_y = src_y,
11894 .src_w = src_w,
11895 .src_h = src_h,
11896 };
11897 struct intel_plane *intel_plane = to_intel_plane(plane);
465c120c
MR
11898 bool visible;
11899 int ret;
11900
11901 ret = drm_plane_helper_check_update(plane, crtc, fb,
11902 &src, &dest, &clip,
11903 DRM_PLANE_HELPER_NO_SCALING,
11904 DRM_PLANE_HELPER_NO_SCALING,
11905 false, true, &visible);
11906
11907 if (ret)
11908 return ret;
11909
11910 /*
11911 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11912 * updating the fb pointer, and returning without touching the
11913 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11914 * turn on the display with all planes setup as desired.
11915 */
11916 if (!crtc->enabled) {
4c34574f
MR
11917 mutex_lock(&dev->struct_mutex);
11918
465c120c
MR
11919 /*
11920 * If we already called setplane while the crtc was disabled,
11921 * we may have an fb pinned; unpin it.
11922 */
11923 if (plane->fb)
a071fa00
DV
11924 intel_unpin_fb_obj(old_obj);
11925
11926 i915_gem_track_fb(old_obj, obj,
11927 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
465c120c
MR
11928
11929 /* Pin and return without programming hardware */
4c34574f
MR
11930 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11931 mutex_unlock(&dev->struct_mutex);
11932
11933 return ret;
465c120c
MR
11934 }
11935
11936 intel_crtc_wait_for_pending_flips(crtc);
11937
11938 /*
11939 * If clipping results in a non-visible primary plane, we'll disable
11940 * the primary plane. Note that this is a bit different than what
11941 * happens if userspace explicitly disables the plane by passing fb=0
11942 * because plane->fb still gets set and pinned.
11943 */
11944 if (!visible) {
4c34574f
MR
11945 mutex_lock(&dev->struct_mutex);
11946
465c120c
MR
11947 /*
11948 * Try to pin the new fb first so that we can bail out if we
11949 * fail.
11950 */
11951 if (plane->fb != fb) {
a071fa00 11952 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
4c34574f
MR
11953 if (ret) {
11954 mutex_unlock(&dev->struct_mutex);
465c120c 11955 return ret;
4c34574f 11956 }
465c120c
MR
11957 }
11958
a071fa00
DV
11959 i915_gem_track_fb(old_obj, obj,
11960 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11961
465c120c 11962 if (intel_crtc->primary_enabled)
fdd508a6 11963 intel_disable_primary_hw_plane(plane, crtc);
465c120c
MR
11964
11965
11966 if (plane->fb != fb)
11967 if (plane->fb)
a071fa00 11968 intel_unpin_fb_obj(old_obj);
465c120c 11969
4c34574f
MR
11970 mutex_unlock(&dev->struct_mutex);
11971
ce54d85a 11972 } else {
48404c1e
SJ
11973 if (intel_crtc && intel_crtc->active &&
11974 intel_crtc->primary_enabled) {
11975 /*
11976 * FBC does not work on some platforms for rotated
11977 * planes, so disable it when rotation is not 0 and
11978 * update it when rotation is set back to 0.
11979 *
11980 * FIXME: This is redundant with the fbc update done in
11981 * the primary plane enable function except that that
11982 * one is done too late. We eventually need to unify
11983 * this.
11984 */
11985 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11986 dev_priv->fbc.plane == intel_crtc->plane &&
11987 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11988 intel_disable_fbc(dev);
11989 }
11990 }
ce54d85a
SJ
11991 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11992 if (ret)
11993 return ret;
465c120c 11994
ce54d85a
SJ
11995 if (!intel_crtc->primary_enabled)
11996 intel_enable_primary_hw_plane(plane, crtc);
11997 }
465c120c 11998
ce54d85a
SJ
11999 intel_plane->crtc_x = orig.crtc_x;
12000 intel_plane->crtc_y = orig.crtc_y;
12001 intel_plane->crtc_w = orig.crtc_w;
12002 intel_plane->crtc_h = orig.crtc_h;
12003 intel_plane->src_x = orig.src_x;
12004 intel_plane->src_y = orig.src_y;
12005 intel_plane->src_w = orig.src_w;
12006 intel_plane->src_h = orig.src_h;
12007 intel_plane->obj = obj;
465c120c
MR
12008
12009 return 0;
12010}
12011
3d7d6510
MR
12012/* Common destruction function for both primary and cursor planes */
12013static void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
12014{
12015 struct intel_plane *intel_plane = to_intel_plane(plane);
12016 drm_plane_cleanup(plane);
12017 kfree(intel_plane);
12018}
12019
12020static const struct drm_plane_funcs intel_primary_plane_funcs = {
12021 .update_plane = intel_primary_plane_setplane,
12022 .disable_plane = intel_primary_plane_disable,
3d7d6510 12023 .destroy = intel_plane_destroy,
48404c1e 12024 .set_property = intel_plane_set_property
465c120c
MR
12025};
12026
12027static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12028 int pipe)
12029{
12030 struct intel_plane *primary;
12031 const uint32_t *intel_primary_formats;
12032 int num_formats;
12033
12034 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12035 if (primary == NULL)
12036 return NULL;
12037
12038 primary->can_scale = false;
12039 primary->max_downscale = 1;
12040 primary->pipe = pipe;
12041 primary->plane = pipe;
48404c1e 12042 primary->rotation = BIT(DRM_ROTATE_0);
465c120c
MR
12043 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12044 primary->plane = !pipe;
12045
12046 if (INTEL_INFO(dev)->gen <= 3) {
12047 intel_primary_formats = intel_primary_formats_gen2;
12048 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12049 } else {
12050 intel_primary_formats = intel_primary_formats_gen4;
12051 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12052 }
12053
12054 drm_universal_plane_init(dev, &primary->base, 0,
12055 &intel_primary_plane_funcs,
12056 intel_primary_formats, num_formats,
12057 DRM_PLANE_TYPE_PRIMARY);
48404c1e
SJ
12058
12059 if (INTEL_INFO(dev)->gen >= 4) {
12060 if (!dev->mode_config.rotation_property)
12061 dev->mode_config.rotation_property =
12062 drm_mode_create_rotation_property(dev,
12063 BIT(DRM_ROTATE_0) |
12064 BIT(DRM_ROTATE_180));
12065 if (dev->mode_config.rotation_property)
12066 drm_object_attach_property(&primary->base.base,
12067 dev->mode_config.rotation_property,
12068 primary->rotation);
12069 }
12070
465c120c
MR
12071 return &primary->base;
12072}
12073
3d7d6510
MR
12074static int
12075intel_cursor_plane_disable(struct drm_plane *plane)
12076{
12077 if (!plane->fb)
12078 return 0;
12079
12080 BUG_ON(!plane->crtc);
12081
12082 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
12083}
12084
12085static int
12086intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
12087 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
12088 unsigned int crtc_w, unsigned int crtc_h,
12089 uint32_t src_x, uint32_t src_y,
12090 uint32_t src_w, uint32_t src_h)
12091{
12092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12093 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12094 struct drm_i915_gem_object *obj = intel_fb->obj;
12095 struct drm_rect dest = {
12096 /* integer pixels */
12097 .x1 = crtc_x,
12098 .y1 = crtc_y,
12099 .x2 = crtc_x + crtc_w,
12100 .y2 = crtc_y + crtc_h,
12101 };
12102 struct drm_rect src = {
12103 /* 16.16 fixed point */
12104 .x1 = src_x,
12105 .y1 = src_y,
12106 .x2 = src_x + src_w,
12107 .y2 = src_y + src_h,
12108 };
12109 const struct drm_rect clip = {
12110 /* integer pixels */
1add143c
VS
12111 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
12112 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
3d7d6510
MR
12113 };
12114 bool visible;
12115 int ret;
12116
12117 ret = drm_plane_helper_check_update(plane, crtc, fb,
12118 &src, &dest, &clip,
12119 DRM_PLANE_HELPER_NO_SCALING,
12120 DRM_PLANE_HELPER_NO_SCALING,
12121 true, true, &visible);
12122 if (ret)
12123 return ret;
12124
12125 crtc->cursor_x = crtc_x;
12126 crtc->cursor_y = crtc_y;
12127 if (fb != crtc->cursor->fb) {
12128 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
12129 } else {
12130 intel_crtc_update_cursor(crtc, visible);
4ed91096
DV
12131
12132 intel_frontbuffer_flip(crtc->dev,
12133 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
12134
3d7d6510
MR
12135 return 0;
12136 }
12137}
12138static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12139 .update_plane = intel_cursor_plane_update,
12140 .disable_plane = intel_cursor_plane_disable,
12141 .destroy = intel_plane_destroy,
12142};
12143
12144static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12145 int pipe)
12146{
12147 struct intel_plane *cursor;
12148
12149 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12150 if (cursor == NULL)
12151 return NULL;
12152
12153 cursor->can_scale = false;
12154 cursor->max_downscale = 1;
12155 cursor->pipe = pipe;
12156 cursor->plane = pipe;
12157
12158 drm_universal_plane_init(dev, &cursor->base, 0,
12159 &intel_cursor_plane_funcs,
12160 intel_cursor_formats,
12161 ARRAY_SIZE(intel_cursor_formats),
12162 DRM_PLANE_TYPE_CURSOR);
12163 return &cursor->base;
12164}
12165
b358d0a6 12166static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 12167{
fbee40df 12168 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 12169 struct intel_crtc *intel_crtc;
3d7d6510
MR
12170 struct drm_plane *primary = NULL;
12171 struct drm_plane *cursor = NULL;
465c120c 12172 int i, ret;
79e53945 12173
955382f3 12174 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
12175 if (intel_crtc == NULL)
12176 return;
12177
465c120c 12178 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
12179 if (!primary)
12180 goto fail;
12181
12182 cursor = intel_cursor_plane_create(dev, pipe);
12183 if (!cursor)
12184 goto fail;
12185
465c120c 12186 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
12187 cursor, &intel_crtc_funcs);
12188 if (ret)
12189 goto fail;
79e53945
JB
12190
12191 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
12192 for (i = 0; i < 256; i++) {
12193 intel_crtc->lut_r[i] = i;
12194 intel_crtc->lut_g[i] = i;
12195 intel_crtc->lut_b[i] = i;
12196 }
12197
1f1c2e24
VS
12198 /*
12199 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 12200 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 12201 */
80824003
JB
12202 intel_crtc->pipe = pipe;
12203 intel_crtc->plane = pipe;
3a77c4c4 12204 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 12205 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 12206 intel_crtc->plane = !pipe;
80824003
JB
12207 }
12208
4b0e333e
CW
12209 intel_crtc->cursor_base = ~0;
12210 intel_crtc->cursor_cntl = ~0;
dc41c154 12211 intel_crtc->cursor_size = ~0;
8d7849db 12212
22fd0fab
JB
12213 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12214 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12215 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12216 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12217
79e53945 12218 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
12219
12220 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
12221 return;
12222
12223fail:
12224 if (primary)
12225 drm_plane_cleanup(primary);
12226 if (cursor)
12227 drm_plane_cleanup(cursor);
12228 kfree(intel_crtc);
79e53945
JB
12229}
12230
752aa88a
JB
12231enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12232{
12233 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 12234 struct drm_device *dev = connector->base.dev;
752aa88a 12235
51fd371b 12236 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a
JB
12237
12238 if (!encoder)
12239 return INVALID_PIPE;
12240
12241 return to_intel_crtc(encoder->crtc)->pipe;
12242}
12243
08d7b3d1 12244int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 12245 struct drm_file *file)
08d7b3d1 12246{
08d7b3d1 12247 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 12248 struct drm_crtc *drmmode_crtc;
c05422d5 12249 struct intel_crtc *crtc;
08d7b3d1 12250
1cff8f6b
DV
12251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12252 return -ENODEV;
08d7b3d1 12253
7707e653 12254 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 12255
7707e653 12256 if (!drmmode_crtc) {
08d7b3d1 12257 DRM_ERROR("no such CRTC id\n");
3f2c2057 12258 return -ENOENT;
08d7b3d1
CW
12259 }
12260
7707e653 12261 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 12262 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 12263
c05422d5 12264 return 0;
08d7b3d1
CW
12265}
12266
66a9278e 12267static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 12268{
66a9278e
DV
12269 struct drm_device *dev = encoder->base.dev;
12270 struct intel_encoder *source_encoder;
79e53945 12271 int index_mask = 0;
79e53945
JB
12272 int entry = 0;
12273
b2784e15 12274 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 12275 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
12276 index_mask |= (1 << entry);
12277
79e53945
JB
12278 entry++;
12279 }
4ef69c7a 12280
79e53945
JB
12281 return index_mask;
12282}
12283
4d302442
CW
12284static bool has_edp_a(struct drm_device *dev)
12285{
12286 struct drm_i915_private *dev_priv = dev->dev_private;
12287
12288 if (!IS_MOBILE(dev))
12289 return false;
12290
12291 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12292 return false;
12293
e3589908 12294 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
12295 return false;
12296
12297 return true;
12298}
12299
ba0fbca4
DL
12300const char *intel_output_name(int output)
12301{
12302 static const char *names[] = {
12303 [INTEL_OUTPUT_UNUSED] = "Unused",
12304 [INTEL_OUTPUT_ANALOG] = "Analog",
12305 [INTEL_OUTPUT_DVO] = "DVO",
12306 [INTEL_OUTPUT_SDVO] = "SDVO",
12307 [INTEL_OUTPUT_LVDS] = "LVDS",
12308 [INTEL_OUTPUT_TVOUT] = "TV",
12309 [INTEL_OUTPUT_HDMI] = "HDMI",
12310 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12311 [INTEL_OUTPUT_EDP] = "eDP",
12312 [INTEL_OUTPUT_DSI] = "DSI",
12313 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12314 };
12315
12316 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12317 return "Invalid";
12318
12319 return names[output];
12320}
12321
84b4e042
JB
12322static bool intel_crt_present(struct drm_device *dev)
12323{
12324 struct drm_i915_private *dev_priv = dev->dev_private;
12325
884497ed
DL
12326 if (INTEL_INFO(dev)->gen >= 9)
12327 return false;
12328
84b4e042
JB
12329 if (IS_ULT(dev))
12330 return false;
12331
12332 if (IS_CHERRYVIEW(dev))
12333 return false;
12334
12335 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12336 return false;
12337
12338 return true;
12339}
12340
79e53945
JB
12341static void intel_setup_outputs(struct drm_device *dev)
12342{
725e30ad 12343 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 12344 struct intel_encoder *encoder;
cb0953d7 12345 bool dpd_is_edp = false;
79e53945 12346
c9093354 12347 intel_lvds_init(dev);
79e53945 12348
84b4e042 12349 if (intel_crt_present(dev))
79935fca 12350 intel_crt_init(dev);
cb0953d7 12351
affa9354 12352 if (HAS_DDI(dev)) {
0e72a5b5
ED
12353 int found;
12354
12355 /* Haswell uses DDI functions to detect digital outputs */
12356 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12357 /* DDI A only supports eDP */
12358 if (found)
12359 intel_ddi_init(dev, PORT_A);
12360
12361 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12362 * register */
12363 found = I915_READ(SFUSE_STRAP);
12364
12365 if (found & SFUSE_STRAP_DDIB_DETECTED)
12366 intel_ddi_init(dev, PORT_B);
12367 if (found & SFUSE_STRAP_DDIC_DETECTED)
12368 intel_ddi_init(dev, PORT_C);
12369 if (found & SFUSE_STRAP_DDID_DETECTED)
12370 intel_ddi_init(dev, PORT_D);
12371 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 12372 int found;
5d8a7752 12373 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
12374
12375 if (has_edp_a(dev))
12376 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 12377
dc0fa718 12378 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 12379 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 12380 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 12381 if (!found)
e2debe91 12382 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 12383 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 12384 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
12385 }
12386
dc0fa718 12387 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 12388 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 12389
dc0fa718 12390 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 12391 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 12392
5eb08b69 12393 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 12394 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 12395
270b3042 12396 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 12397 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 12398 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
12399 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12400 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12401 PORT_B);
12402 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12403 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12404 }
12405
6f6005a5
JB
12406 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12407 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12408 PORT_C);
12409 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 12410 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 12411 }
19c03924 12412
9418c1f1
VS
12413 if (IS_CHERRYVIEW(dev)) {
12414 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12415 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12416 PORT_D);
12417 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12418 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12419 }
12420 }
12421
3cfca973 12422 intel_dsi_init(dev);
103a196f 12423 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 12424 bool found = false;
7d57382e 12425
e2debe91 12426 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12427 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 12428 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
12429 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12430 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 12431 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 12432 }
27185ae1 12433
e7281eab 12434 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12435 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 12436 }
13520b05
KH
12437
12438 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 12439
e2debe91 12440 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 12441 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 12442 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 12443 }
27185ae1 12444
e2debe91 12445 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 12446
b01f2c3a
JB
12447 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12448 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 12449 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 12450 }
e7281eab 12451 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 12452 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 12453 }
27185ae1 12454
b01f2c3a 12455 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 12456 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 12457 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 12458 } else if (IS_GEN2(dev))
79e53945
JB
12459 intel_dvo_init(dev);
12460
103a196f 12461 if (SUPPORTS_TV(dev))
79e53945
JB
12462 intel_tv_init(dev);
12463
7c8f8a70
RV
12464 intel_edp_psr_init(dev);
12465
b2784e15 12466 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
12467 encoder->base.possible_crtcs = encoder->crtc_mask;
12468 encoder->base.possible_clones =
66a9278e 12469 intel_encoder_clones(encoder);
79e53945 12470 }
47356eb6 12471
dde86e2d 12472 intel_init_pch_refclk(dev);
270b3042
DV
12473
12474 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
12475}
12476
12477static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12478{
60a5ca01 12479 struct drm_device *dev = fb->dev;
79e53945 12480 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 12481
ef2d633e 12482 drm_framebuffer_cleanup(fb);
60a5ca01 12483 mutex_lock(&dev->struct_mutex);
ef2d633e 12484 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
12485 drm_gem_object_unreference(&intel_fb->obj->base);
12486 mutex_unlock(&dev->struct_mutex);
79e53945
JB
12487 kfree(intel_fb);
12488}
12489
12490static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 12491 struct drm_file *file,
79e53945
JB
12492 unsigned int *handle)
12493{
12494 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 12495 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 12496
05394f39 12497 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
12498}
12499
12500static const struct drm_framebuffer_funcs intel_fb_funcs = {
12501 .destroy = intel_user_framebuffer_destroy,
12502 .create_handle = intel_user_framebuffer_create_handle,
12503};
12504
b5ea642a
DV
12505static int intel_framebuffer_init(struct drm_device *dev,
12506 struct intel_framebuffer *intel_fb,
12507 struct drm_mode_fb_cmd2 *mode_cmd,
12508 struct drm_i915_gem_object *obj)
79e53945 12509{
a57ce0b2 12510 int aligned_height;
a35cdaa0 12511 int pitch_limit;
79e53945
JB
12512 int ret;
12513
dd4916c5
DV
12514 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12515
c16ed4be
CW
12516 if (obj->tiling_mode == I915_TILING_Y) {
12517 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 12518 return -EINVAL;
c16ed4be 12519 }
57cd6508 12520
c16ed4be
CW
12521 if (mode_cmd->pitches[0] & 63) {
12522 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12523 mode_cmd->pitches[0]);
57cd6508 12524 return -EINVAL;
c16ed4be 12525 }
57cd6508 12526
a35cdaa0
CW
12527 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12528 pitch_limit = 32*1024;
12529 } else if (INTEL_INFO(dev)->gen >= 4) {
12530 if (obj->tiling_mode)
12531 pitch_limit = 16*1024;
12532 else
12533 pitch_limit = 32*1024;
12534 } else if (INTEL_INFO(dev)->gen >= 3) {
12535 if (obj->tiling_mode)
12536 pitch_limit = 8*1024;
12537 else
12538 pitch_limit = 16*1024;
12539 } else
12540 /* XXX DSPC is limited to 4k tiled */
12541 pitch_limit = 8*1024;
12542
12543 if (mode_cmd->pitches[0] > pitch_limit) {
12544 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12545 obj->tiling_mode ? "tiled" : "linear",
12546 mode_cmd->pitches[0], pitch_limit);
5d7bd705 12547 return -EINVAL;
c16ed4be 12548 }
5d7bd705
VS
12549
12550 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
12551 mode_cmd->pitches[0] != obj->stride) {
12552 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12553 mode_cmd->pitches[0], obj->stride);
5d7bd705 12554 return -EINVAL;
c16ed4be 12555 }
5d7bd705 12556
57779d06 12557 /* Reject formats not supported by any plane early. */
308e5bcb 12558 switch (mode_cmd->pixel_format) {
57779d06 12559 case DRM_FORMAT_C8:
04b3924d
VS
12560 case DRM_FORMAT_RGB565:
12561 case DRM_FORMAT_XRGB8888:
12562 case DRM_FORMAT_ARGB8888:
57779d06
VS
12563 break;
12564 case DRM_FORMAT_XRGB1555:
12565 case DRM_FORMAT_ARGB1555:
c16ed4be 12566 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
12567 DRM_DEBUG("unsupported pixel format: %s\n",
12568 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12569 return -EINVAL;
c16ed4be 12570 }
57779d06
VS
12571 break;
12572 case DRM_FORMAT_XBGR8888:
12573 case DRM_FORMAT_ABGR8888:
04b3924d
VS
12574 case DRM_FORMAT_XRGB2101010:
12575 case DRM_FORMAT_ARGB2101010:
57779d06
VS
12576 case DRM_FORMAT_XBGR2101010:
12577 case DRM_FORMAT_ABGR2101010:
c16ed4be 12578 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
12579 DRM_DEBUG("unsupported pixel format: %s\n",
12580 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12581 return -EINVAL;
c16ed4be 12582 }
b5626747 12583 break;
04b3924d
VS
12584 case DRM_FORMAT_YUYV:
12585 case DRM_FORMAT_UYVY:
12586 case DRM_FORMAT_YVYU:
12587 case DRM_FORMAT_VYUY:
c16ed4be 12588 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
12589 DRM_DEBUG("unsupported pixel format: %s\n",
12590 drm_get_format_name(mode_cmd->pixel_format));
57779d06 12591 return -EINVAL;
c16ed4be 12592 }
57cd6508
CW
12593 break;
12594 default:
4ee62c76
VS
12595 DRM_DEBUG("unsupported pixel format: %s\n",
12596 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
12597 return -EINVAL;
12598 }
12599
90f9a336
VS
12600 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12601 if (mode_cmd->offsets[0] != 0)
12602 return -EINVAL;
12603
a57ce0b2
JB
12604 aligned_height = intel_align_height(dev, mode_cmd->height,
12605 obj->tiling_mode);
53155c0a
DV
12606 /* FIXME drm helper for size checks (especially planar formats)? */
12607 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12608 return -EINVAL;
12609
c7d73f6a
DV
12610 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12611 intel_fb->obj = obj;
80075d49 12612 intel_fb->obj->framebuffer_references++;
c7d73f6a 12613
79e53945
JB
12614 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12615 if (ret) {
12616 DRM_ERROR("framebuffer init failed %d\n", ret);
12617 return ret;
12618 }
12619
79e53945
JB
12620 return 0;
12621}
12622
79e53945
JB
12623static struct drm_framebuffer *
12624intel_user_framebuffer_create(struct drm_device *dev,
12625 struct drm_file *filp,
308e5bcb 12626 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 12627{
05394f39 12628 struct drm_i915_gem_object *obj;
79e53945 12629
308e5bcb
JB
12630 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12631 mode_cmd->handles[0]));
c8725226 12632 if (&obj->base == NULL)
cce13ff7 12633 return ERR_PTR(-ENOENT);
79e53945 12634
d2dff872 12635 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
12636}
12637
4520f53a 12638#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 12639static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
12640{
12641}
12642#endif
12643
79e53945 12644static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 12645 .fb_create = intel_user_framebuffer_create,
0632fef6 12646 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
12647};
12648
e70236a8
JB
12649/* Set up chip specific display functions */
12650static void intel_init_display(struct drm_device *dev)
12651{
12652 struct drm_i915_private *dev_priv = dev->dev_private;
12653
ee9300bb
DV
12654 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12655 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
12656 else if (IS_CHERRYVIEW(dev))
12657 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
12658 else if (IS_VALLEYVIEW(dev))
12659 dev_priv->display.find_dpll = vlv_find_best_dpll;
12660 else if (IS_PINEVIEW(dev))
12661 dev_priv->display.find_dpll = pnv_find_best_dpll;
12662 else
12663 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12664
affa9354 12665 if (HAS_DDI(dev)) {
0e8ffe1b 12666 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 12667 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 12668 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
12669 dev_priv->display.crtc_enable = haswell_crtc_enable;
12670 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 12671 dev_priv->display.off = ironlake_crtc_off;
70d21f0e
DL
12672 if (INTEL_INFO(dev)->gen >= 9)
12673 dev_priv->display.update_primary_plane =
12674 skylake_update_primary_plane;
12675 else
12676 dev_priv->display.update_primary_plane =
12677 ironlake_update_primary_plane;
09b4ddf9 12678 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 12679 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 12680 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 12681 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
12682 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12683 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 12684 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
12685 dev_priv->display.update_primary_plane =
12686 ironlake_update_primary_plane;
89b667f8
JB
12687 } else if (IS_VALLEYVIEW(dev)) {
12688 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12689 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
12690 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12691 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12692 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12693 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12694 dev_priv->display.update_primary_plane =
12695 i9xx_update_primary_plane;
f564048e 12696 } else {
0e8ffe1b 12697 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 12698 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 12699 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
12700 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12701 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 12702 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
12703 dev_priv->display.update_primary_plane =
12704 i9xx_update_primary_plane;
f564048e 12705 }
e70236a8 12706
e70236a8 12707 /* Returns the core display clock speed */
25eb05fc
JB
12708 if (IS_VALLEYVIEW(dev))
12709 dev_priv->display.get_display_clock_speed =
12710 valleyview_get_display_clock_speed;
12711 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
12712 dev_priv->display.get_display_clock_speed =
12713 i945_get_display_clock_speed;
12714 else if (IS_I915G(dev))
12715 dev_priv->display.get_display_clock_speed =
12716 i915_get_display_clock_speed;
257a7ffc 12717 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
12718 dev_priv->display.get_display_clock_speed =
12719 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
12720 else if (IS_PINEVIEW(dev))
12721 dev_priv->display.get_display_clock_speed =
12722 pnv_get_display_clock_speed;
e70236a8
JB
12723 else if (IS_I915GM(dev))
12724 dev_priv->display.get_display_clock_speed =
12725 i915gm_get_display_clock_speed;
12726 else if (IS_I865G(dev))
12727 dev_priv->display.get_display_clock_speed =
12728 i865_get_display_clock_speed;
f0f8a9ce 12729 else if (IS_I85X(dev))
e70236a8
JB
12730 dev_priv->display.get_display_clock_speed =
12731 i855_get_display_clock_speed;
12732 else /* 852, 830 */
12733 dev_priv->display.get_display_clock_speed =
12734 i830_get_display_clock_speed;
12735
3bb11b53 12736 if (IS_G4X(dev)) {
e0dac65e 12737 dev_priv->display.write_eld = g4x_write_eld;
3bb11b53
SJ
12738 } else if (IS_GEN5(dev)) {
12739 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12740 dev_priv->display.write_eld = ironlake_write_eld;
12741 } else if (IS_GEN6(dev)) {
12742 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12743 dev_priv->display.write_eld = ironlake_write_eld;
12744 dev_priv->display.modeset_global_resources =
12745 snb_modeset_global_resources;
12746 } else if (IS_IVYBRIDGE(dev)) {
12747 /* FIXME: detect B0+ stepping and use auto training */
12748 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12749 dev_priv->display.write_eld = ironlake_write_eld;
12750 dev_priv->display.modeset_global_resources =
12751 ivb_modeset_global_resources;
059b2fe9 12752 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53
SJ
12753 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12754 dev_priv->display.write_eld = haswell_write_eld;
12755 dev_priv->display.modeset_global_resources =
12756 haswell_modeset_global_resources;
30a970c6
JB
12757 } else if (IS_VALLEYVIEW(dev)) {
12758 dev_priv->display.modeset_global_resources =
12759 valleyview_modeset_global_resources;
9ca2fe73 12760 dev_priv->display.write_eld = ironlake_write_eld;
02c29259
S
12761 } else if (INTEL_INFO(dev)->gen >= 9) {
12762 dev_priv->display.write_eld = haswell_write_eld;
12763 dev_priv->display.modeset_global_resources =
12764 haswell_modeset_global_resources;
e70236a8 12765 }
8c9f3aaf
JB
12766
12767 /* Default just returns -ENODEV to indicate unsupported */
12768 dev_priv->display.queue_flip = intel_default_queue_flip;
12769
12770 switch (INTEL_INFO(dev)->gen) {
12771 case 2:
12772 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12773 break;
12774
12775 case 3:
12776 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12777 break;
12778
12779 case 4:
12780 case 5:
12781 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12782 break;
12783
12784 case 6:
12785 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12786 break;
7c9017e5 12787 case 7:
4e0bbc31 12788 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
12789 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12790 break;
8c9f3aaf 12791 }
7bd688cd
JN
12792
12793 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
12794
12795 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
12796}
12797
b690e96c
JB
12798/*
12799 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12800 * resume, or other times. This quirk makes sure that's the case for
12801 * affected systems.
12802 */
0206e353 12803static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
12804{
12805 struct drm_i915_private *dev_priv = dev->dev_private;
12806
12807 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 12808 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
12809}
12810
b6b5d049
VS
12811static void quirk_pipeb_force(struct drm_device *dev)
12812{
12813 struct drm_i915_private *dev_priv = dev->dev_private;
12814
12815 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12816 DRM_INFO("applying pipe b force quirk\n");
12817}
12818
435793df
KP
12819/*
12820 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12821 */
12822static void quirk_ssc_force_disable(struct drm_device *dev)
12823{
12824 struct drm_i915_private *dev_priv = dev->dev_private;
12825 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 12826 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
12827}
12828
4dca20ef 12829/*
5a15ab5b
CE
12830 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12831 * brightness value
4dca20ef
CE
12832 */
12833static void quirk_invert_brightness(struct drm_device *dev)
12834{
12835 struct drm_i915_private *dev_priv = dev->dev_private;
12836 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 12837 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
12838}
12839
9c72cc6f
SD
12840/* Some VBT's incorrectly indicate no backlight is present */
12841static void quirk_backlight_present(struct drm_device *dev)
12842{
12843 struct drm_i915_private *dev_priv = dev->dev_private;
12844 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12845 DRM_INFO("applying backlight present quirk\n");
12846}
12847
b690e96c
JB
12848struct intel_quirk {
12849 int device;
12850 int subsystem_vendor;
12851 int subsystem_device;
12852 void (*hook)(struct drm_device *dev);
12853};
12854
5f85f176
EE
12855/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12856struct intel_dmi_quirk {
12857 void (*hook)(struct drm_device *dev);
12858 const struct dmi_system_id (*dmi_id_list)[];
12859};
12860
12861static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12862{
12863 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12864 return 1;
12865}
12866
12867static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12868 {
12869 .dmi_id_list = &(const struct dmi_system_id[]) {
12870 {
12871 .callback = intel_dmi_reverse_brightness,
12872 .ident = "NCR Corporation",
12873 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12874 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12875 },
12876 },
12877 { } /* terminating entry */
12878 },
12879 .hook = quirk_invert_brightness,
12880 },
12881};
12882
c43b5634 12883static struct intel_quirk intel_quirks[] = {
b690e96c 12884 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 12885 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 12886
b690e96c
JB
12887 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12888 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12889
b690e96c
JB
12890 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12891 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12892
5f080c0f
VS
12893 /* 830 needs to leave pipe A & dpll A up */
12894 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12895
b6b5d049
VS
12896 /* 830 needs to leave pipe B & dpll B up */
12897 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12898
435793df
KP
12899 /* Lenovo U160 cannot use SSC on LVDS */
12900 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
12901
12902 /* Sony Vaio Y cannot use SSC on LVDS */
12903 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 12904
be505f64
AH
12905 /* Acer Aspire 5734Z must invert backlight brightness */
12906 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12907
12908 /* Acer/eMachines G725 */
12909 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12910
12911 /* Acer/eMachines e725 */
12912 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12913
12914 /* Acer/Packard Bell NCL20 */
12915 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12916
12917 /* Acer Aspire 4736Z */
12918 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
12919
12920 /* Acer Aspire 5336 */
12921 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
12922
12923 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12924 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 12925
dfb3d47b
SD
12926 /* Acer C720 Chromebook (Core i3 4005U) */
12927 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12928
d4967d8c
SD
12929 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12930 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
12931
12932 /* HP Chromebook 14 (Celeron 2955U) */
12933 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
b690e96c
JB
12934};
12935
12936static void intel_init_quirks(struct drm_device *dev)
12937{
12938 struct pci_dev *d = dev->pdev;
12939 int i;
12940
12941 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12942 struct intel_quirk *q = &intel_quirks[i];
12943
12944 if (d->device == q->device &&
12945 (d->subsystem_vendor == q->subsystem_vendor ||
12946 q->subsystem_vendor == PCI_ANY_ID) &&
12947 (d->subsystem_device == q->subsystem_device ||
12948 q->subsystem_device == PCI_ANY_ID))
12949 q->hook(dev);
12950 }
5f85f176
EE
12951 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12952 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12953 intel_dmi_quirks[i].hook(dev);
12954 }
b690e96c
JB
12955}
12956
9cce37f4
JB
12957/* Disable the VGA plane that we never use */
12958static void i915_disable_vga(struct drm_device *dev)
12959{
12960 struct drm_i915_private *dev_priv = dev->dev_private;
12961 u8 sr1;
766aa1c4 12962 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 12963
2b37c616 12964 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 12965 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 12966 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
12967 sr1 = inb(VGA_SR_DATA);
12968 outb(sr1 | 1<<5, VGA_SR_DATA);
12969 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12970 udelay(300);
12971
69769f9a
VS
12972 /*
12973 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12974 * from S3 without preserving (some of?) the other bits.
12975 */
12976 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
9cce37f4
JB
12977 POSTING_READ(vga_reg);
12978}
12979
f817586c
DV
12980void intel_modeset_init_hw(struct drm_device *dev)
12981{
a8f78b58
ED
12982 intel_prepare_ddi(dev);
12983
f8bf63fd
VS
12984 if (IS_VALLEYVIEW(dev))
12985 vlv_update_cdclk(dev);
12986
f817586c
DV
12987 intel_init_clock_gating(dev);
12988
8090c6b9 12989 intel_enable_gt_powersave(dev);
f817586c
DV
12990}
12991
7d708ee4
ID
12992void intel_modeset_suspend_hw(struct drm_device *dev)
12993{
12994 intel_suspend_hw(dev);
12995}
12996
79e53945
JB
12997void intel_modeset_init(struct drm_device *dev)
12998{
652c393a 12999 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 13000 int sprite, ret;
8cc87b75 13001 enum pipe pipe;
46f297fb 13002 struct intel_crtc *crtc;
79e53945
JB
13003
13004 drm_mode_config_init(dev);
13005
13006 dev->mode_config.min_width = 0;
13007 dev->mode_config.min_height = 0;
13008
019d96cb
DA
13009 dev->mode_config.preferred_depth = 24;
13010 dev->mode_config.prefer_shadow = 1;
13011
e6ecefaa 13012 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 13013
b690e96c
JB
13014 intel_init_quirks(dev);
13015
1fa61106
ED
13016 intel_init_pm(dev);
13017
e3c74757
BW
13018 if (INTEL_INFO(dev)->num_pipes == 0)
13019 return;
13020
e70236a8
JB
13021 intel_init_display(dev);
13022
a6c45cf0
CW
13023 if (IS_GEN2(dev)) {
13024 dev->mode_config.max_width = 2048;
13025 dev->mode_config.max_height = 2048;
13026 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
13027 dev->mode_config.max_width = 4096;
13028 dev->mode_config.max_height = 4096;
79e53945 13029 } else {
a6c45cf0
CW
13030 dev->mode_config.max_width = 8192;
13031 dev->mode_config.max_height = 8192;
79e53945 13032 }
068be561 13033
dc41c154
VS
13034 if (IS_845G(dev) || IS_I865G(dev)) {
13035 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13036 dev->mode_config.cursor_height = 1023;
13037 } else if (IS_GEN2(dev)) {
068be561
DL
13038 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13039 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13040 } else {
13041 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13042 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13043 }
13044
5d4545ae 13045 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 13046
28c97730 13047 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
13048 INTEL_INFO(dev)->num_pipes,
13049 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 13050
055e393f 13051 for_each_pipe(dev_priv, pipe) {
8cc87b75 13052 intel_crtc_init(dev, pipe);
1fe47785
DL
13053 for_each_sprite(pipe, sprite) {
13054 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 13055 if (ret)
06da8da2 13056 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 13057 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 13058 }
79e53945
JB
13059 }
13060
f42bb70d
JB
13061 intel_init_dpio(dev);
13062
e72f9fbf 13063 intel_shared_dpll_init(dev);
ee7b9f93 13064
69769f9a
VS
13065 /* save the BIOS value before clobbering it */
13066 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
9cce37f4
JB
13067 /* Just disable it once at startup */
13068 i915_disable_vga(dev);
79e53945 13069 intel_setup_outputs(dev);
11be49eb
CW
13070
13071 /* Just in case the BIOS is doing something questionable. */
13072 intel_disable_fbc(dev);
fa9fa083 13073
6e9f798d 13074 drm_modeset_lock_all(dev);
fa9fa083 13075 intel_modeset_setup_hw_state(dev, false);
6e9f798d 13076 drm_modeset_unlock_all(dev);
46f297fb 13077
d3fcc808 13078 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
13079 if (!crtc->active)
13080 continue;
13081
46f297fb 13082 /*
46f297fb
JB
13083 * Note that reserving the BIOS fb up front prevents us
13084 * from stuffing other stolen allocations like the ring
13085 * on top. This prevents some ugliness at boot time, and
13086 * can even allow for smooth boot transitions if the BIOS
13087 * fb is large enough for the active pipe configuration.
13088 */
13089 if (dev_priv->display.get_plane_config) {
13090 dev_priv->display.get_plane_config(crtc,
13091 &crtc->plane_config);
13092 /*
13093 * If the fb is shared between multiple heads, we'll
13094 * just get the first one.
13095 */
484b41dd 13096 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 13097 }
46f297fb 13098 }
2c7111db
CW
13099}
13100
7fad798e
DV
13101static void intel_enable_pipe_a(struct drm_device *dev)
13102{
13103 struct intel_connector *connector;
13104 struct drm_connector *crt = NULL;
13105 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 13106 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
13107
13108 /* We can't just switch on the pipe A, we need to set things up with a
13109 * proper mode and output configuration. As a gross hack, enable pipe A
13110 * by enabling the load detect pipe once. */
13111 list_for_each_entry(connector,
13112 &dev->mode_config.connector_list,
13113 base.head) {
13114 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13115 crt = &connector->base;
13116 break;
13117 }
13118 }
13119
13120 if (!crt)
13121 return;
13122
208bf9fd
VS
13123 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13124 intel_release_load_detect_pipe(crt, &load_detect_temp);
7fad798e
DV
13125}
13126
fa555837
DV
13127static bool
13128intel_check_plane_mapping(struct intel_crtc *crtc)
13129{
7eb552ae
BW
13130 struct drm_device *dev = crtc->base.dev;
13131 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
13132 u32 reg, val;
13133
7eb552ae 13134 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
13135 return true;
13136
13137 reg = DSPCNTR(!crtc->plane);
13138 val = I915_READ(reg);
13139
13140 if ((val & DISPLAY_PLANE_ENABLE) &&
13141 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13142 return false;
13143
13144 return true;
13145}
13146
24929352
DV
13147static void intel_sanitize_crtc(struct intel_crtc *crtc)
13148{
13149 struct drm_device *dev = crtc->base.dev;
13150 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 13151 u32 reg;
24929352 13152
24929352 13153 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 13154 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
13155 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13156
d3eaf884 13157 /* restore vblank interrupts to correct state */
d297e103
VS
13158 if (crtc->active) {
13159 update_scanline_offset(crtc);
d3eaf884 13160 drm_vblank_on(dev, crtc->pipe);
d297e103 13161 } else
d3eaf884
VS
13162 drm_vblank_off(dev, crtc->pipe);
13163
24929352 13164 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
13165 * disable the crtc (and hence change the state) if it is wrong. Note
13166 * that gen4+ has a fixed plane -> pipe mapping. */
13167 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
13168 struct intel_connector *connector;
13169 bool plane;
13170
24929352
DV
13171 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13172 crtc->base.base.id);
13173
13174 /* Pipe has the wrong plane attached and the plane is active.
13175 * Temporarily change the plane mapping and disable everything
13176 * ... */
13177 plane = crtc->plane;
13178 crtc->plane = !plane;
9c8958bc 13179 crtc->primary_enabled = true;
24929352
DV
13180 dev_priv->display.crtc_disable(&crtc->base);
13181 crtc->plane = plane;
13182
13183 /* ... and break all links. */
13184 list_for_each_entry(connector, &dev->mode_config.connector_list,
13185 base.head) {
13186 if (connector->encoder->base.crtc != &crtc->base)
13187 continue;
13188
7f1950fb
EE
13189 connector->base.dpms = DRM_MODE_DPMS_OFF;
13190 connector->base.encoder = NULL;
24929352 13191 }
7f1950fb
EE
13192 /* multiple connectors may have the same encoder:
13193 * handle them and break crtc link separately */
13194 list_for_each_entry(connector, &dev->mode_config.connector_list,
13195 base.head)
13196 if (connector->encoder->base.crtc == &crtc->base) {
13197 connector->encoder->base.crtc = NULL;
13198 connector->encoder->connectors_active = false;
13199 }
24929352
DV
13200
13201 WARN_ON(crtc->active);
13202 crtc->base.enabled = false;
13203 }
24929352 13204
7fad798e
DV
13205 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13206 crtc->pipe == PIPE_A && !crtc->active) {
13207 /* BIOS forgot to enable pipe A, this mostly happens after
13208 * resume. Force-enable the pipe to fix this, the update_dpms
13209 * call below we restore the pipe to the right state, but leave
13210 * the required bits on. */
13211 intel_enable_pipe_a(dev);
13212 }
13213
24929352
DV
13214 /* Adjust the state of the output pipe according to whether we
13215 * have active connectors/encoders. */
13216 intel_crtc_update_dpms(&crtc->base);
13217
13218 if (crtc->active != crtc->base.enabled) {
13219 struct intel_encoder *encoder;
13220
13221 /* This can happen either due to bugs in the get_hw_state
13222 * functions or because the pipe is force-enabled due to the
13223 * pipe A quirk. */
13224 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13225 crtc->base.base.id,
13226 crtc->base.enabled ? "enabled" : "disabled",
13227 crtc->active ? "enabled" : "disabled");
13228
13229 crtc->base.enabled = crtc->active;
13230
13231 /* Because we only establish the connector -> encoder ->
13232 * crtc links if something is active, this means the
13233 * crtc is now deactivated. Break the links. connector
13234 * -> encoder links are only establish when things are
13235 * actually up, hence no need to break them. */
13236 WARN_ON(crtc->active);
13237
13238 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13239 WARN_ON(encoder->connectors_active);
13240 encoder->base.crtc = NULL;
13241 }
13242 }
c5ab3bc0 13243
a3ed6aad 13244 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
13245 /*
13246 * We start out with underrun reporting disabled to avoid races.
13247 * For correct bookkeeping mark this on active crtcs.
13248 *
c5ab3bc0
DV
13249 * Also on gmch platforms we dont have any hardware bits to
13250 * disable the underrun reporting. Which means we need to start
13251 * out with underrun reporting disabled also on inactive pipes,
13252 * since otherwise we'll complain about the garbage we read when
13253 * e.g. coming up after runtime pm.
13254 *
4cc31489
DV
13255 * No protection against concurrent access is required - at
13256 * worst a fifo underrun happens which also sets this to false.
13257 */
13258 crtc->cpu_fifo_underrun_disabled = true;
13259 crtc->pch_fifo_underrun_disabled = true;
13260 }
24929352
DV
13261}
13262
13263static void intel_sanitize_encoder(struct intel_encoder *encoder)
13264{
13265 struct intel_connector *connector;
13266 struct drm_device *dev = encoder->base.dev;
13267
13268 /* We need to check both for a crtc link (meaning that the
13269 * encoder is active and trying to read from a pipe) and the
13270 * pipe itself being active. */
13271 bool has_active_crtc = encoder->base.crtc &&
13272 to_intel_crtc(encoder->base.crtc)->active;
13273
13274 if (encoder->connectors_active && !has_active_crtc) {
13275 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13276 encoder->base.base.id,
8e329a03 13277 encoder->base.name);
24929352
DV
13278
13279 /* Connector is active, but has no active pipe. This is
13280 * fallout from our resume register restoring. Disable
13281 * the encoder manually again. */
13282 if (encoder->base.crtc) {
13283 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13284 encoder->base.base.id,
8e329a03 13285 encoder->base.name);
24929352 13286 encoder->disable(encoder);
a62d1497
VS
13287 if (encoder->post_disable)
13288 encoder->post_disable(encoder);
24929352 13289 }
7f1950fb
EE
13290 encoder->base.crtc = NULL;
13291 encoder->connectors_active = false;
24929352
DV
13292
13293 /* Inconsistent output/port/pipe state happens presumably due to
13294 * a bug in one of the get_hw_state functions. Or someplace else
13295 * in our code, like the register restore mess on resume. Clamp
13296 * things to off as a safer default. */
13297 list_for_each_entry(connector,
13298 &dev->mode_config.connector_list,
13299 base.head) {
13300 if (connector->encoder != encoder)
13301 continue;
7f1950fb
EE
13302 connector->base.dpms = DRM_MODE_DPMS_OFF;
13303 connector->base.encoder = NULL;
24929352
DV
13304 }
13305 }
13306 /* Enabled encoders without active connectors will be fixed in
13307 * the crtc fixup. */
13308}
13309
04098753 13310void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
13311{
13312 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 13313 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 13314
04098753
ID
13315 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13316 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13317 i915_disable_vga(dev);
13318 }
13319}
13320
13321void i915_redisable_vga(struct drm_device *dev)
13322{
13323 struct drm_i915_private *dev_priv = dev->dev_private;
13324
8dc8a27c
PZ
13325 /* This function can be called both from intel_modeset_setup_hw_state or
13326 * at a very early point in our resume sequence, where the power well
13327 * structures are not yet restored. Since this function is at a very
13328 * paranoid "someone might have enabled VGA while we were not looking"
13329 * level, just check if the power well is enabled instead of trying to
13330 * follow the "don't touch the power well if we don't need it" policy
13331 * the rest of the driver uses. */
04098753 13332 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
13333 return;
13334
04098753 13335 i915_redisable_vga_power_on(dev);
0fde901f
KM
13336}
13337
98ec7739
VS
13338static bool primary_get_hw_state(struct intel_crtc *crtc)
13339{
13340 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13341
13342 if (!crtc->active)
13343 return false;
13344
13345 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13346}
13347
30e984df 13348static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
13349{
13350 struct drm_i915_private *dev_priv = dev->dev_private;
13351 enum pipe pipe;
24929352
DV
13352 struct intel_crtc *crtc;
13353 struct intel_encoder *encoder;
13354 struct intel_connector *connector;
5358901f 13355 int i;
24929352 13356
d3fcc808 13357 for_each_intel_crtc(dev, crtc) {
88adfff1 13358 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 13359
9953599b
DV
13360 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13361
0e8ffe1b
DV
13362 crtc->active = dev_priv->display.get_pipe_config(crtc,
13363 &crtc->config);
24929352
DV
13364
13365 crtc->base.enabled = crtc->active;
98ec7739 13366 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
13367
13368 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13369 crtc->base.base.id,
13370 crtc->active ? "enabled" : "disabled");
13371 }
13372
5358901f
DV
13373 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13374 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13375
13376 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13377 pll->active = 0;
d3fcc808 13378 for_each_intel_crtc(dev, crtc) {
5358901f
DV
13379 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13380 pll->active++;
13381 }
13382 pll->refcount = pll->active;
13383
35c95375
DV
13384 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13385 pll->name, pll->refcount, pll->on);
bd2bb1b9
PZ
13386
13387 if (pll->refcount)
13388 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
13389 }
13390
b2784e15 13391 for_each_intel_encoder(dev, encoder) {
24929352
DV
13392 pipe = 0;
13393
13394 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
13395 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13396 encoder->base.crtc = &crtc->base;
1d37b689 13397 encoder->get_config(encoder, &crtc->config);
24929352
DV
13398 } else {
13399 encoder->base.crtc = NULL;
13400 }
13401
13402 encoder->connectors_active = false;
6f2bcceb 13403 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 13404 encoder->base.base.id,
8e329a03 13405 encoder->base.name,
24929352 13406 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 13407 pipe_name(pipe));
24929352
DV
13408 }
13409
13410 list_for_each_entry(connector, &dev->mode_config.connector_list,
13411 base.head) {
13412 if (connector->get_hw_state(connector)) {
13413 connector->base.dpms = DRM_MODE_DPMS_ON;
13414 connector->encoder->connectors_active = true;
13415 connector->base.encoder = &connector->encoder->base;
13416 } else {
13417 connector->base.dpms = DRM_MODE_DPMS_OFF;
13418 connector->base.encoder = NULL;
13419 }
13420 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13421 connector->base.base.id,
c23cc417 13422 connector->base.name,
24929352
DV
13423 connector->base.encoder ? "enabled" : "disabled");
13424 }
30e984df
DV
13425}
13426
13427/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13428 * and i915 state tracking structures. */
13429void intel_modeset_setup_hw_state(struct drm_device *dev,
13430 bool force_restore)
13431{
13432 struct drm_i915_private *dev_priv = dev->dev_private;
13433 enum pipe pipe;
30e984df
DV
13434 struct intel_crtc *crtc;
13435 struct intel_encoder *encoder;
35c95375 13436 int i;
30e984df
DV
13437
13438 intel_modeset_readout_hw_state(dev);
24929352 13439
babea61d
JB
13440 /*
13441 * Now that we have the config, copy it to each CRTC struct
13442 * Note that this could go away if we move to using crtc_config
13443 * checking everywhere.
13444 */
d3fcc808 13445 for_each_intel_crtc(dev, crtc) {
d330a953 13446 if (crtc->active && i915.fastboot) {
f6a83288 13447 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
13448 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13449 crtc->base.base.id);
13450 drm_mode_debug_printmodeline(&crtc->base.mode);
13451 }
13452 }
13453
24929352 13454 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 13455 for_each_intel_encoder(dev, encoder) {
24929352
DV
13456 intel_sanitize_encoder(encoder);
13457 }
13458
055e393f 13459 for_each_pipe(dev_priv, pipe) {
24929352
DV
13460 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13461 intel_sanitize_crtc(crtc);
c0b03411 13462 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 13463 }
9a935856 13464
35c95375
DV
13465 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13466 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13467
13468 if (!pll->on || pll->active)
13469 continue;
13470
13471 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13472
13473 pll->disable(dev_priv, pll);
13474 pll->on = false;
13475 }
13476
96f90c54 13477 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
13478 ilk_wm_get_hw_state(dev);
13479
45e2b5f6 13480 if (force_restore) {
7d0bc1ea
VS
13481 i915_redisable_vga(dev);
13482
f30da187
DV
13483 /*
13484 * We need to use raw interfaces for restoring state to avoid
13485 * checking (bogus) intermediate states.
13486 */
055e393f 13487 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
13488 struct drm_crtc *crtc =
13489 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
13490
13491 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 13492 crtc->primary->fb);
45e2b5f6
DV
13493 }
13494 } else {
13495 intel_modeset_update_staged_output_state(dev);
13496 }
8af6cf88
DV
13497
13498 intel_modeset_check_state(dev);
2c7111db
CW
13499}
13500
13501void intel_modeset_gem_init(struct drm_device *dev)
13502{
484b41dd 13503 struct drm_crtc *c;
2ff8fde1 13504 struct drm_i915_gem_object *obj;
484b41dd 13505
ae48434c
ID
13506 mutex_lock(&dev->struct_mutex);
13507 intel_init_gt_powersave(dev);
13508 mutex_unlock(&dev->struct_mutex);
13509
1833b134 13510 intel_modeset_init_hw(dev);
02e792fb
DV
13511
13512 intel_setup_overlay(dev);
484b41dd
JB
13513
13514 /*
13515 * Make sure any fbs we allocated at startup are properly
13516 * pinned & fenced. When we do the allocation it's too early
13517 * for this.
13518 */
13519 mutex_lock(&dev->struct_mutex);
70e1e0ec 13520 for_each_crtc(dev, c) {
2ff8fde1
MR
13521 obj = intel_fb_obj(c->primary->fb);
13522 if (obj == NULL)
484b41dd
JB
13523 continue;
13524
2ff8fde1 13525 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
484b41dd
JB
13526 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13527 to_intel_crtc(c)->pipe);
66e514c1
DA
13528 drm_framebuffer_unreference(c->primary->fb);
13529 c->primary->fb = NULL;
484b41dd
JB
13530 }
13531 }
13532 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13533}
13534
4932e2c3
ID
13535void intel_connector_unregister(struct intel_connector *intel_connector)
13536{
13537 struct drm_connector *connector = &intel_connector->base;
13538
13539 intel_panel_destroy_backlight(connector);
34ea3d38 13540 drm_connector_unregister(connector);
4932e2c3
ID
13541}
13542
79e53945
JB
13543void intel_modeset_cleanup(struct drm_device *dev)
13544{
652c393a 13545 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 13546 struct drm_connector *connector;
652c393a 13547
fd0c0642
DV
13548 /*
13549 * Interrupts and polling as the first thing to avoid creating havoc.
13550 * Too much stuff here (turning of rps, connectors, ...) would
13551 * experience fancy races otherwise.
13552 */
13553 drm_irq_uninstall(dev);
1d0d343a 13554 intel_hpd_cancel_work(dev_priv);
eb21b92b
JB
13555 dev_priv->pm._irqs_disabled = true;
13556
fd0c0642
DV
13557 /*
13558 * Due to the hpd irq storm handling the hotplug work can re-arm the
13559 * poll handlers. Hence disable polling after hpd handling is shut down.
13560 */
f87ea761 13561 drm_kms_helper_poll_fini(dev);
fd0c0642 13562
652c393a
JB
13563 mutex_lock(&dev->struct_mutex);
13564
723bfd70
JB
13565 intel_unregister_dsm_handler();
13566
973d04f9 13567 intel_disable_fbc(dev);
e70236a8 13568
8090c6b9 13569 intel_disable_gt_powersave(dev);
0cdab21f 13570
930ebb46
DV
13571 ironlake_teardown_rc6(dev);
13572
69341a5e
KH
13573 mutex_unlock(&dev->struct_mutex);
13574
1630fe75
CW
13575 /* flush any delayed tasks or pending work */
13576 flush_scheduled_work();
13577
db31af1d
JN
13578 /* destroy the backlight and sysfs files before encoders/connectors */
13579 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
13580 struct intel_connector *intel_connector;
13581
13582 intel_connector = to_intel_connector(connector);
13583 intel_connector->unregister(intel_connector);
db31af1d 13584 }
d9255d57 13585
79e53945 13586 drm_mode_config_cleanup(dev);
4d7bb011
DV
13587
13588 intel_cleanup_overlay(dev);
ae48434c
ID
13589
13590 mutex_lock(&dev->struct_mutex);
13591 intel_cleanup_gt_powersave(dev);
13592 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13593}
13594
f1c79df3
ZW
13595/*
13596 * Return which encoder is currently attached for connector.
13597 */
df0e9248 13598struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 13599{
df0e9248
CW
13600 return &intel_attached_encoder(connector)->base;
13601}
f1c79df3 13602
df0e9248
CW
13603void intel_connector_attach_encoder(struct intel_connector *connector,
13604 struct intel_encoder *encoder)
13605{
13606 connector->encoder = encoder;
13607 drm_mode_connector_attach_encoder(&connector->base,
13608 &encoder->base);
79e53945 13609}
28d52043
DA
13610
13611/*
13612 * set vga decode state - true == enable VGA decode
13613 */
13614int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13615{
13616 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 13617 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
13618 u16 gmch_ctrl;
13619
75fa041d
CW
13620 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13621 DRM_ERROR("failed to read control word\n");
13622 return -EIO;
13623 }
13624
c0cc8a55
CW
13625 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13626 return 0;
13627
28d52043
DA
13628 if (state)
13629 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13630 else
13631 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
13632
13633 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13634 DRM_ERROR("failed to write control word\n");
13635 return -EIO;
13636 }
13637
28d52043
DA
13638 return 0;
13639}
c4a1d9e4 13640
c4a1d9e4 13641struct intel_display_error_state {
ff57f1b0
PZ
13642
13643 u32 power_well_driver;
13644
63b66e5b
CW
13645 int num_transcoders;
13646
c4a1d9e4
CW
13647 struct intel_cursor_error_state {
13648 u32 control;
13649 u32 position;
13650 u32 base;
13651 u32 size;
52331309 13652 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
13653
13654 struct intel_pipe_error_state {
ddf9c536 13655 bool power_domain_on;
c4a1d9e4 13656 u32 source;
f301b1e1 13657 u32 stat;
52331309 13658 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
13659
13660 struct intel_plane_error_state {
13661 u32 control;
13662 u32 stride;
13663 u32 size;
13664 u32 pos;
13665 u32 addr;
13666 u32 surface;
13667 u32 tile_offset;
52331309 13668 } plane[I915_MAX_PIPES];
63b66e5b
CW
13669
13670 struct intel_transcoder_error_state {
ddf9c536 13671 bool power_domain_on;
63b66e5b
CW
13672 enum transcoder cpu_transcoder;
13673
13674 u32 conf;
13675
13676 u32 htotal;
13677 u32 hblank;
13678 u32 hsync;
13679 u32 vtotal;
13680 u32 vblank;
13681 u32 vsync;
13682 } transcoder[4];
c4a1d9e4
CW
13683};
13684
13685struct intel_display_error_state *
13686intel_display_capture_error_state(struct drm_device *dev)
13687{
fbee40df 13688 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 13689 struct intel_display_error_state *error;
63b66e5b
CW
13690 int transcoders[] = {
13691 TRANSCODER_A,
13692 TRANSCODER_B,
13693 TRANSCODER_C,
13694 TRANSCODER_EDP,
13695 };
c4a1d9e4
CW
13696 int i;
13697
63b66e5b
CW
13698 if (INTEL_INFO(dev)->num_pipes == 0)
13699 return NULL;
13700
9d1cb914 13701 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
13702 if (error == NULL)
13703 return NULL;
13704
190be112 13705 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
13706 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13707
055e393f 13708 for_each_pipe(dev_priv, i) {
ddf9c536 13709 error->pipe[i].power_domain_on =
bfafe93a
ID
13710 intel_display_power_enabled_unlocked(dev_priv,
13711 POWER_DOMAIN_PIPE(i));
ddf9c536 13712 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
13713 continue;
13714
5efb3e28
VS
13715 error->cursor[i].control = I915_READ(CURCNTR(i));
13716 error->cursor[i].position = I915_READ(CURPOS(i));
13717 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
13718
13719 error->plane[i].control = I915_READ(DSPCNTR(i));
13720 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 13721 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 13722 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
13723 error->plane[i].pos = I915_READ(DSPPOS(i));
13724 }
ca291363
PZ
13725 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13726 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
13727 if (INTEL_INFO(dev)->gen >= 4) {
13728 error->plane[i].surface = I915_READ(DSPSURF(i));
13729 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13730 }
13731
c4a1d9e4 13732 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 13733
3abfce77 13734 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 13735 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
13736 }
13737
13738 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13739 if (HAS_DDI(dev_priv->dev))
13740 error->num_transcoders++; /* Account for eDP. */
13741
13742 for (i = 0; i < error->num_transcoders; i++) {
13743 enum transcoder cpu_transcoder = transcoders[i];
13744
ddf9c536 13745 error->transcoder[i].power_domain_on =
bfafe93a 13746 intel_display_power_enabled_unlocked(dev_priv,
38cc1daf 13747 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 13748 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
13749 continue;
13750
63b66e5b
CW
13751 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13752
13753 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13754 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13755 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13756 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13757 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13758 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13759 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
13760 }
13761
13762 return error;
13763}
13764
edc3d884
MK
13765#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13766
c4a1d9e4 13767void
edc3d884 13768intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
13769 struct drm_device *dev,
13770 struct intel_display_error_state *error)
13771{
055e393f 13772 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
13773 int i;
13774
63b66e5b
CW
13775 if (!error)
13776 return;
13777
edc3d884 13778 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 13779 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 13780 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 13781 error->power_well_driver);
055e393f 13782 for_each_pipe(dev_priv, i) {
edc3d884 13783 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
13784 err_printf(m, " Power: %s\n",
13785 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 13786 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 13787 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
13788
13789 err_printf(m, "Plane [%d]:\n", i);
13790 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13791 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 13792 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
13793 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13794 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 13795 }
4b71a570 13796 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 13797 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 13798 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
13799 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13800 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
13801 }
13802
edc3d884
MK
13803 err_printf(m, "Cursor [%d]:\n", i);
13804 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13805 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13806 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 13807 }
63b66e5b
CW
13808
13809 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 13810 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 13811 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
13812 err_printf(m, " Power: %s\n",
13813 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
13814 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13815 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13816 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13817 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13818 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13819 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13820 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13821 }
c4a1d9e4 13822}
e2fcdaa9
VS
13823
13824void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13825{
13826 struct intel_crtc *crtc;
13827
13828 for_each_intel_crtc(dev, crtc) {
13829 struct intel_unpin_work *work;
13830 unsigned long irqflags;
13831
13832 spin_lock_irqsave(&dev->event_lock, irqflags);
13833
13834 work = crtc->unpin_work;
13835
13836 if (work && work->event &&
13837 work->event->base.file_priv == file) {
13838 kfree(work->event);
13839 work->event = NULL;
13840 }
13841
13842 spin_unlock_irqrestore(&dev->event_lock, irqflags);
13843 }
13844}
This page took 2.571703 seconds and 5 git commands to generate.