drm/i915/chv: find the best divisor for the target clock v4
[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>
c0f372b3 42#include <linux/dma_remapping.h>
79e53945 43
ef9348c8
CML
44#define DIV_ROUND_CLOSEST_ULL(ll, d) \
45 ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
46
3dec0095 47static void intel_increase_pllclock(struct drm_crtc *crtc);
6b383a7f 48static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 49
f1f644dc
JB
50static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
18442d08
VS
52static void ironlake_pch_clock_get(struct intel_crtc *crtc,
53 struct intel_crtc_config *pipe_config);
f1f644dc 54
e7457a9a
DL
55static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
56 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
57static int intel_framebuffer_init(struct drm_device *dev,
58 struct intel_framebuffer *ifb,
59 struct drm_mode_fb_cmd2 *mode_cmd,
60 struct drm_i915_gem_object *obj);
e7457a9a 61
79e53945 62typedef struct {
0206e353 63 int min, max;
79e53945
JB
64} intel_range_t;
65
66typedef struct {
0206e353
AJ
67 int dot_limit;
68 int p2_slow, p2_fast;
79e53945
JB
69} intel_p2_t;
70
d4906093
ML
71typedef struct intel_limit intel_limit_t;
72struct intel_limit {
0206e353
AJ
73 intel_range_t dot, vco, n, m, m1, m2, p, p1;
74 intel_p2_t p2;
d4906093 75};
79e53945 76
d2acd215
DV
77int
78intel_pch_rawclk(struct drm_device *dev)
79{
80 struct drm_i915_private *dev_priv = dev->dev_private;
81
82 WARN_ON(!HAS_PCH_SPLIT(dev));
83
84 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
85}
86
021357ac
CW
87static inline u32 /* units of 100MHz */
88intel_fdi_link_freq(struct drm_device *dev)
89{
8b99e68c
CW
90 if (IS_GEN5(dev)) {
91 struct drm_i915_private *dev_priv = dev->dev_private;
92 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
93 } else
94 return 27;
021357ac
CW
95}
96
5d536e28 97static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 98 .dot = { .min = 25000, .max = 350000 },
9c333719 99 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 100 .n = { .min = 2, .max = 16 },
0206e353
AJ
101 .m = { .min = 96, .max = 140 },
102 .m1 = { .min = 18, .max = 26 },
103 .m2 = { .min = 6, .max = 16 },
104 .p = { .min = 4, .max = 128 },
105 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
106 .p2 = { .dot_limit = 165000,
107 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
108};
109
5d536e28
DV
110static const intel_limit_t intel_limits_i8xx_dvo = {
111 .dot = { .min = 25000, .max = 350000 },
9c333719 112 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 113 .n = { .min = 2, .max = 16 },
5d536e28
DV
114 .m = { .min = 96, .max = 140 },
115 .m1 = { .min = 18, .max = 26 },
116 .m2 = { .min = 6, .max = 16 },
117 .p = { .min = 4, .max = 128 },
118 .p1 = { .min = 2, .max = 33 },
119 .p2 = { .dot_limit = 165000,
120 .p2_slow = 4, .p2_fast = 4 },
121};
122
e4b36699 123static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 124 .dot = { .min = 25000, .max = 350000 },
9c333719 125 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 126 .n = { .min = 2, .max = 16 },
0206e353
AJ
127 .m = { .min = 96, .max = 140 },
128 .m1 = { .min = 18, .max = 26 },
129 .m2 = { .min = 6, .max = 16 },
130 .p = { .min = 4, .max = 128 },
131 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
132 .p2 = { .dot_limit = 165000,
133 .p2_slow = 14, .p2_fast = 7 },
e4b36699 134};
273e27ca 135
e4b36699 136static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
137 .dot = { .min = 20000, .max = 400000 },
138 .vco = { .min = 1400000, .max = 2800000 },
139 .n = { .min = 1, .max = 6 },
140 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
141 .m1 = { .min = 8, .max = 18 },
142 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
143 .p = { .min = 5, .max = 80 },
144 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
145 .p2 = { .dot_limit = 200000,
146 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
147};
148
149static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
150 .dot = { .min = 20000, .max = 400000 },
151 .vco = { .min = 1400000, .max = 2800000 },
152 .n = { .min = 1, .max = 6 },
153 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
154 .m1 = { .min = 8, .max = 18 },
155 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
156 .p = { .min = 7, .max = 98 },
157 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
158 .p2 = { .dot_limit = 112000,
159 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
160};
161
273e27ca 162
e4b36699 163static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
164 .dot = { .min = 25000, .max = 270000 },
165 .vco = { .min = 1750000, .max = 3500000},
166 .n = { .min = 1, .max = 4 },
167 .m = { .min = 104, .max = 138 },
168 .m1 = { .min = 17, .max = 23 },
169 .m2 = { .min = 5, .max = 11 },
170 .p = { .min = 10, .max = 30 },
171 .p1 = { .min = 1, .max = 3},
172 .p2 = { .dot_limit = 270000,
173 .p2_slow = 10,
174 .p2_fast = 10
044c7c41 175 },
e4b36699
KP
176};
177
178static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
179 .dot = { .min = 22000, .max = 400000 },
180 .vco = { .min = 1750000, .max = 3500000},
181 .n = { .min = 1, .max = 4 },
182 .m = { .min = 104, .max = 138 },
183 .m1 = { .min = 16, .max = 23 },
184 .m2 = { .min = 5, .max = 11 },
185 .p = { .min = 5, .max = 80 },
186 .p1 = { .min = 1, .max = 8},
187 .p2 = { .dot_limit = 165000,
188 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
189};
190
191static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
192 .dot = { .min = 20000, .max = 115000 },
193 .vco = { .min = 1750000, .max = 3500000 },
194 .n = { .min = 1, .max = 3 },
195 .m = { .min = 104, .max = 138 },
196 .m1 = { .min = 17, .max = 23 },
197 .m2 = { .min = 5, .max = 11 },
198 .p = { .min = 28, .max = 112 },
199 .p1 = { .min = 2, .max = 8 },
200 .p2 = { .dot_limit = 0,
201 .p2_slow = 14, .p2_fast = 14
044c7c41 202 },
e4b36699
KP
203};
204
205static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
206 .dot = { .min = 80000, .max = 224000 },
207 .vco = { .min = 1750000, .max = 3500000 },
208 .n = { .min = 1, .max = 3 },
209 .m = { .min = 104, .max = 138 },
210 .m1 = { .min = 17, .max = 23 },
211 .m2 = { .min = 5, .max = 11 },
212 .p = { .min = 14, .max = 42 },
213 .p1 = { .min = 2, .max = 6 },
214 .p2 = { .dot_limit = 0,
215 .p2_slow = 7, .p2_fast = 7
044c7c41 216 },
e4b36699
KP
217};
218
f2b115e6 219static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
220 .dot = { .min = 20000, .max = 400000},
221 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 222 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
223 .n = { .min = 3, .max = 6 },
224 .m = { .min = 2, .max = 256 },
273e27ca 225 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
226 .m1 = { .min = 0, .max = 0 },
227 .m2 = { .min = 0, .max = 254 },
228 .p = { .min = 5, .max = 80 },
229 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
230 .p2 = { .dot_limit = 200000,
231 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
232};
233
f2b115e6 234static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
235 .dot = { .min = 20000, .max = 400000 },
236 .vco = { .min = 1700000, .max = 3500000 },
237 .n = { .min = 3, .max = 6 },
238 .m = { .min = 2, .max = 256 },
239 .m1 = { .min = 0, .max = 0 },
240 .m2 = { .min = 0, .max = 254 },
241 .p = { .min = 7, .max = 112 },
242 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
243 .p2 = { .dot_limit = 112000,
244 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
245};
246
273e27ca
EA
247/* Ironlake / Sandybridge
248 *
249 * We calculate clock using (register_value + 2) for N/M1/M2, so here
250 * the range value for them is (actual_value - 2).
251 */
b91ad0ec 252static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
253 .dot = { .min = 25000, .max = 350000 },
254 .vco = { .min = 1760000, .max = 3510000 },
255 .n = { .min = 1, .max = 5 },
256 .m = { .min = 79, .max = 127 },
257 .m1 = { .min = 12, .max = 22 },
258 .m2 = { .min = 5, .max = 9 },
259 .p = { .min = 5, .max = 80 },
260 .p1 = { .min = 1, .max = 8 },
261 .p2 = { .dot_limit = 225000,
262 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
263};
264
b91ad0ec 265static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
266 .dot = { .min = 25000, .max = 350000 },
267 .vco = { .min = 1760000, .max = 3510000 },
268 .n = { .min = 1, .max = 3 },
269 .m = { .min = 79, .max = 118 },
270 .m1 = { .min = 12, .max = 22 },
271 .m2 = { .min = 5, .max = 9 },
272 .p = { .min = 28, .max = 112 },
273 .p1 = { .min = 2, .max = 8 },
274 .p2 = { .dot_limit = 225000,
275 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
276};
277
278static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
279 .dot = { .min = 25000, .max = 350000 },
280 .vco = { .min = 1760000, .max = 3510000 },
281 .n = { .min = 1, .max = 3 },
282 .m = { .min = 79, .max = 127 },
283 .m1 = { .min = 12, .max = 22 },
284 .m2 = { .min = 5, .max = 9 },
285 .p = { .min = 14, .max = 56 },
286 .p1 = { .min = 2, .max = 8 },
287 .p2 = { .dot_limit = 225000,
288 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
289};
290
273e27ca 291/* LVDS 100mhz refclk limits. */
b91ad0ec 292static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
293 .dot = { .min = 25000, .max = 350000 },
294 .vco = { .min = 1760000, .max = 3510000 },
295 .n = { .min = 1, .max = 2 },
296 .m = { .min = 79, .max = 126 },
297 .m1 = { .min = 12, .max = 22 },
298 .m2 = { .min = 5, .max = 9 },
299 .p = { .min = 28, .max = 112 },
0206e353 300 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
301 .p2 = { .dot_limit = 225000,
302 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
303};
304
305static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
306 .dot = { .min = 25000, .max = 350000 },
307 .vco = { .min = 1760000, .max = 3510000 },
308 .n = { .min = 1, .max = 3 },
309 .m = { .min = 79, .max = 126 },
310 .m1 = { .min = 12, .max = 22 },
311 .m2 = { .min = 5, .max = 9 },
312 .p = { .min = 14, .max = 42 },
0206e353 313 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
314 .p2 = { .dot_limit = 225000,
315 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
316};
317
dc730512 318static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
319 /*
320 * These are the data rate limits (measured in fast clocks)
321 * since those are the strictest limits we have. The fast
322 * clock and actual rate limits are more relaxed, so checking
323 * them would make no difference.
324 */
325 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 326 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 327 .n = { .min = 1, .max = 7 },
a0c4da24
JB
328 .m1 = { .min = 2, .max = 3 },
329 .m2 = { .min = 11, .max = 156 },
b99ab663 330 .p1 = { .min = 2, .max = 3 },
5fdc9c49 331 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
332};
333
ef9348c8
CML
334static const intel_limit_t intel_limits_chv = {
335 /*
336 * These are the data rate limits (measured in fast clocks)
337 * since those are the strictest limits we have. The fast
338 * clock and actual rate limits are more relaxed, so checking
339 * them would make no difference.
340 */
341 .dot = { .min = 25000 * 5, .max = 540000 * 5},
342 .vco = { .min = 4860000, .max = 6700000 },
343 .n = { .min = 1, .max = 1 },
344 .m1 = { .min = 2, .max = 2 },
345 .m2 = { .min = 24 << 22, .max = 175 << 22 },
346 .p1 = { .min = 2, .max = 4 },
347 .p2 = { .p2_slow = 1, .p2_fast = 14 },
348};
349
6b4bf1c4
VS
350static void vlv_clock(int refclk, intel_clock_t *clock)
351{
352 clock->m = clock->m1 * clock->m2;
353 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
354 if (WARN_ON(clock->n == 0 || clock->p == 0))
355 return;
fb03ac01
VS
356 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
357 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
358}
359
e0638cdf
PZ
360/**
361 * Returns whether any output on the specified pipe is of the specified type
362 */
363static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
364{
365 struct drm_device *dev = crtc->dev;
366 struct intel_encoder *encoder;
367
368 for_each_encoder_on_crtc(dev, crtc, encoder)
369 if (encoder->type == type)
370 return true;
371
372 return false;
373}
374
1b894b59
CW
375static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
376 int refclk)
2c07245f 377{
b91ad0ec 378 struct drm_device *dev = crtc->dev;
2c07245f 379 const intel_limit_t *limit;
b91ad0ec
ZW
380
381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 382 if (intel_is_dual_link_lvds(dev)) {
1b894b59 383 if (refclk == 100000)
b91ad0ec
ZW
384 limit = &intel_limits_ironlake_dual_lvds_100m;
385 else
386 limit = &intel_limits_ironlake_dual_lvds;
387 } else {
1b894b59 388 if (refclk == 100000)
b91ad0ec
ZW
389 limit = &intel_limits_ironlake_single_lvds_100m;
390 else
391 limit = &intel_limits_ironlake_single_lvds;
392 }
c6bb3538 393 } else
b91ad0ec 394 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
395
396 return limit;
397}
398
044c7c41
ML
399static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
400{
401 struct drm_device *dev = crtc->dev;
044c7c41
ML
402 const intel_limit_t *limit;
403
404 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 405 if (intel_is_dual_link_lvds(dev))
e4b36699 406 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 407 else
e4b36699 408 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
409 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
410 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 411 limit = &intel_limits_g4x_hdmi;
044c7c41 412 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 413 limit = &intel_limits_g4x_sdvo;
044c7c41 414 } else /* The option is for other outputs */
e4b36699 415 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
416
417 return limit;
418}
419
1b894b59 420static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
421{
422 struct drm_device *dev = crtc->dev;
423 const intel_limit_t *limit;
424
bad720ff 425 if (HAS_PCH_SPLIT(dev))
1b894b59 426 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 427 else if (IS_G4X(dev)) {
044c7c41 428 limit = intel_g4x_limit(crtc);
f2b115e6 429 } else if (IS_PINEVIEW(dev)) {
2177832f 430 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 431 limit = &intel_limits_pineview_lvds;
2177832f 432 else
f2b115e6 433 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
434 } else if (IS_CHERRYVIEW(dev)) {
435 limit = &intel_limits_chv;
a0c4da24 436 } else if (IS_VALLEYVIEW(dev)) {
dc730512 437 limit = &intel_limits_vlv;
a6c45cf0
CW
438 } else if (!IS_GEN2(dev)) {
439 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
440 limit = &intel_limits_i9xx_lvds;
441 else
442 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
443 } else {
444 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 445 limit = &intel_limits_i8xx_lvds;
5d536e28 446 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 447 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
448 else
449 limit = &intel_limits_i8xx_dac;
79e53945
JB
450 }
451 return limit;
452}
453
f2b115e6
AJ
454/* m1 is reserved as 0 in Pineview, n is a ring counter */
455static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 456{
2177832f
SL
457 clock->m = clock->m2 + 2;
458 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
459 if (WARN_ON(clock->n == 0 || clock->p == 0))
460 return;
fb03ac01
VS
461 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
462 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
463}
464
7429e9d4
DV
465static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
466{
467 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
468}
469
ac58c3f0 470static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 471{
7429e9d4 472 clock->m = i9xx_dpll_compute_m(clock);
79e53945 473 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
474 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
475 return;
fb03ac01
VS
476 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
477 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
478}
479
ef9348c8
CML
480static void chv_clock(int refclk, intel_clock_t *clock)
481{
482 clock->m = clock->m1 * clock->m2;
483 clock->p = clock->p1 * clock->p2;
484 if (WARN_ON(clock->n == 0 || clock->p == 0))
485 return;
486 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
487 clock->n << 22);
488 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
489}
490
7c04d1d9 491#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
492/**
493 * Returns whether the given set of divisors are valid for a given refclk with
494 * the given connectors.
495 */
496
1b894b59
CW
497static bool intel_PLL_is_valid(struct drm_device *dev,
498 const intel_limit_t *limit,
499 const intel_clock_t *clock)
79e53945 500{
f01b7962
VS
501 if (clock->n < limit->n.min || limit->n.max < clock->n)
502 INTELPllInvalid("n out of range\n");
79e53945 503 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 504 INTELPllInvalid("p1 out of range\n");
79e53945 505 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 506 INTELPllInvalid("m2 out of range\n");
79e53945 507 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 508 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
509
510 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
511 if (clock->m1 <= clock->m2)
512 INTELPllInvalid("m1 <= m2\n");
513
514 if (!IS_VALLEYVIEW(dev)) {
515 if (clock->p < limit->p.min || limit->p.max < clock->p)
516 INTELPllInvalid("p out of range\n");
517 if (clock->m < limit->m.min || limit->m.max < clock->m)
518 INTELPllInvalid("m out of range\n");
519 }
520
79e53945 521 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 522 INTELPllInvalid("vco out of range\n");
79e53945
JB
523 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
524 * connector, etc., rather than just a single range.
525 */
526 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 527 INTELPllInvalid("dot out of range\n");
79e53945
JB
528
529 return true;
530}
531
d4906093 532static bool
ee9300bb 533i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
534 int target, int refclk, intel_clock_t *match_clock,
535 intel_clock_t *best_clock)
79e53945
JB
536{
537 struct drm_device *dev = crtc->dev;
79e53945 538 intel_clock_t clock;
79e53945
JB
539 int err = target;
540
a210b028 541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 542 /*
a210b028
DV
543 * For LVDS just rely on its current settings for dual-channel.
544 * We haven't figured out how to reliably set up different
545 * single/dual channel state, if we even can.
79e53945 546 */
1974cad0 547 if (intel_is_dual_link_lvds(dev))
79e53945
JB
548 clock.p2 = limit->p2.p2_fast;
549 else
550 clock.p2 = limit->p2.p2_slow;
551 } else {
552 if (target < limit->p2.dot_limit)
553 clock.p2 = limit->p2.p2_slow;
554 else
555 clock.p2 = limit->p2.p2_fast;
556 }
557
0206e353 558 memset(best_clock, 0, sizeof(*best_clock));
79e53945 559
42158660
ZY
560 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
561 clock.m1++) {
562 for (clock.m2 = limit->m2.min;
563 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 564 if (clock.m2 >= clock.m1)
42158660
ZY
565 break;
566 for (clock.n = limit->n.min;
567 clock.n <= limit->n.max; clock.n++) {
568 for (clock.p1 = limit->p1.min;
569 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
570 int this_err;
571
ac58c3f0
DV
572 i9xx_clock(refclk, &clock);
573 if (!intel_PLL_is_valid(dev, limit,
574 &clock))
575 continue;
576 if (match_clock &&
577 clock.p != match_clock->p)
578 continue;
579
580 this_err = abs(clock.dot - target);
581 if (this_err < err) {
582 *best_clock = clock;
583 err = this_err;
584 }
585 }
586 }
587 }
588 }
589
590 return (err != target);
591}
592
593static bool
ee9300bb
DV
594pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
595 int target, int refclk, intel_clock_t *match_clock,
596 intel_clock_t *best_clock)
79e53945
JB
597{
598 struct drm_device *dev = crtc->dev;
79e53945 599 intel_clock_t clock;
79e53945
JB
600 int err = target;
601
a210b028 602 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 603 /*
a210b028
DV
604 * For LVDS just rely on its current settings for dual-channel.
605 * We haven't figured out how to reliably set up different
606 * single/dual channel state, if we even can.
79e53945 607 */
1974cad0 608 if (intel_is_dual_link_lvds(dev))
79e53945
JB
609 clock.p2 = limit->p2.p2_fast;
610 else
611 clock.p2 = limit->p2.p2_slow;
612 } else {
613 if (target < limit->p2.dot_limit)
614 clock.p2 = limit->p2.p2_slow;
615 else
616 clock.p2 = limit->p2.p2_fast;
617 }
618
0206e353 619 memset(best_clock, 0, sizeof(*best_clock));
79e53945 620
42158660
ZY
621 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
622 clock.m1++) {
623 for (clock.m2 = limit->m2.min;
624 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
625 for (clock.n = limit->n.min;
626 clock.n <= limit->n.max; clock.n++) {
627 for (clock.p1 = limit->p1.min;
628 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
629 int this_err;
630
ac58c3f0 631 pineview_clock(refclk, &clock);
1b894b59
CW
632 if (!intel_PLL_is_valid(dev, limit,
633 &clock))
79e53945 634 continue;
cec2f356
SP
635 if (match_clock &&
636 clock.p != match_clock->p)
637 continue;
79e53945
JB
638
639 this_err = abs(clock.dot - target);
640 if (this_err < err) {
641 *best_clock = clock;
642 err = this_err;
643 }
644 }
645 }
646 }
647 }
648
649 return (err != target);
650}
651
d4906093 652static bool
ee9300bb
DV
653g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
654 int target, int refclk, intel_clock_t *match_clock,
655 intel_clock_t *best_clock)
d4906093
ML
656{
657 struct drm_device *dev = crtc->dev;
d4906093
ML
658 intel_clock_t clock;
659 int max_n;
660 bool found;
6ba770dc
AJ
661 /* approximately equals target * 0.00585 */
662 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
663 found = false;
664
665 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 666 if (intel_is_dual_link_lvds(dev))
d4906093
ML
667 clock.p2 = limit->p2.p2_fast;
668 else
669 clock.p2 = limit->p2.p2_slow;
670 } else {
671 if (target < limit->p2.dot_limit)
672 clock.p2 = limit->p2.p2_slow;
673 else
674 clock.p2 = limit->p2.p2_fast;
675 }
676
677 memset(best_clock, 0, sizeof(*best_clock));
678 max_n = limit->n.max;
f77f13e2 679 /* based on hardware requirement, prefer smaller n to precision */
d4906093 680 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 681 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
682 for (clock.m1 = limit->m1.max;
683 clock.m1 >= limit->m1.min; clock.m1--) {
684 for (clock.m2 = limit->m2.max;
685 clock.m2 >= limit->m2.min; clock.m2--) {
686 for (clock.p1 = limit->p1.max;
687 clock.p1 >= limit->p1.min; clock.p1--) {
688 int this_err;
689
ac58c3f0 690 i9xx_clock(refclk, &clock);
1b894b59
CW
691 if (!intel_PLL_is_valid(dev, limit,
692 &clock))
d4906093 693 continue;
1b894b59
CW
694
695 this_err = abs(clock.dot - target);
d4906093
ML
696 if (this_err < err_most) {
697 *best_clock = clock;
698 err_most = this_err;
699 max_n = clock.n;
700 found = true;
701 }
702 }
703 }
704 }
705 }
2c07245f
ZW
706 return found;
707}
708
a0c4da24 709static bool
ee9300bb
DV
710vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
711 int target, int refclk, intel_clock_t *match_clock,
712 intel_clock_t *best_clock)
a0c4da24 713{
f01b7962 714 struct drm_device *dev = crtc->dev;
6b4bf1c4 715 intel_clock_t clock;
69e4f900 716 unsigned int bestppm = 1000000;
27e639bf
VS
717 /* min update 19.2 MHz */
718 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 719 bool found = false;
a0c4da24 720
6b4bf1c4
VS
721 target *= 5; /* fast clock */
722
723 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
724
725 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 726 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 727 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 728 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 729 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 730 clock.p = clock.p1 * clock.p2;
a0c4da24 731 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
733 unsigned int ppm, diff;
734
6b4bf1c4
VS
735 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
736 refclk * clock.m1);
737
738 vlv_clock(refclk, &clock);
43b0ac53 739
f01b7962
VS
740 if (!intel_PLL_is_valid(dev, limit,
741 &clock))
43b0ac53
VS
742 continue;
743
6b4bf1c4
VS
744 diff = abs(clock.dot - target);
745 ppm = div_u64(1000000ULL * diff, target);
746
747 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 748 bestppm = 0;
6b4bf1c4 749 *best_clock = clock;
49e497ef 750 found = true;
43b0ac53 751 }
6b4bf1c4 752
c686122c 753 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 754 bestppm = ppm;
6b4bf1c4 755 *best_clock = clock;
49e497ef 756 found = true;
a0c4da24
JB
757 }
758 }
759 }
760 }
761 }
a0c4da24 762
49e497ef 763 return found;
a0c4da24 764}
a4fc5ed6 765
ef9348c8
CML
766static bool
767chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
770{
771 struct drm_device *dev = crtc->dev;
772 intel_clock_t clock;
773 uint64_t m2;
774 int found = false;
775
776 memset(best_clock, 0, sizeof(*best_clock));
777
778 /*
779 * Based on hardware doc, the n always set to 1, and m1 always
780 * set to 2. If requires to support 200Mhz refclk, we need to
781 * revisit this because n may not 1 anymore.
782 */
783 clock.n = 1, clock.m1 = 2;
784 target *= 5; /* fast clock */
785
786 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
787 for (clock.p2 = limit->p2.p2_fast;
788 clock.p2 >= limit->p2.p2_slow;
789 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
790
791 clock.p = clock.p1 * clock.p2;
792
793 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
794 clock.n) << 22, refclk * clock.m1);
795
796 if (m2 > INT_MAX/clock.m1)
797 continue;
798
799 clock.m2 = m2;
800
801 chv_clock(refclk, &clock);
802
803 if (!intel_PLL_is_valid(dev, limit, &clock))
804 continue;
805
806 /* based on hardware requirement, prefer bigger p
807 */
808 if (clock.p > best_clock->p) {
809 *best_clock = clock;
810 found = true;
811 }
812 }
813 }
814
815 return found;
816}
817
20ddf665
VS
818bool intel_crtc_active(struct drm_crtc *crtc)
819{
820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
821
822 /* Be paranoid as we can arrive here with only partial
823 * state retrieved from the hardware during setup.
824 *
241bfc38 825 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
826 * as Haswell has gained clock readout/fastboot support.
827 *
66e514c1 828 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665
VS
829 * properly reconstruct framebuffers.
830 */
f4510a27 831 return intel_crtc->active && crtc->primary->fb &&
241bfc38 832 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
833}
834
a5c961d1
PZ
835enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
836 enum pipe pipe)
837{
838 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
840
3b117c8f 841 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
842}
843
57e22f4a 844static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
845{
846 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 847 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
848
849 frame = I915_READ(frame_reg);
850
851 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
93937071 852 WARN(1, "vblank wait timed out\n");
a928d536
PZ
853}
854
9d0498a2
JB
855/**
856 * intel_wait_for_vblank - wait for vblank on a given pipe
857 * @dev: drm device
858 * @pipe: pipe to wait for
859 *
860 * Wait for vblank to occur on a given pipe. Needed for various bits of
861 * mode setting code.
862 */
863void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 864{
9d0498a2 865 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 866 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 867
57e22f4a
VS
868 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
869 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
870 return;
871 }
872
300387c0
CW
873 /* Clear existing vblank status. Note this will clear any other
874 * sticky status fields as well.
875 *
876 * This races with i915_driver_irq_handler() with the result
877 * that either function could miss a vblank event. Here it is not
878 * fatal, as we will either wait upon the next vblank interrupt or
879 * timeout. Generally speaking intel_wait_for_vblank() is only
880 * called during modeset at which time the GPU should be idle and
881 * should *not* be performing page flips and thus not waiting on
882 * vblanks...
883 * Currently, the result of us stealing a vblank from the irq
884 * handler is that a single frame will be skipped during swapbuffers.
885 */
886 I915_WRITE(pipestat_reg,
887 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
888
9d0498a2 889 /* Wait for vblank interrupt bit to set */
481b6af3
CW
890 if (wait_for(I915_READ(pipestat_reg) &
891 PIPE_VBLANK_INTERRUPT_STATUS,
892 50))
9d0498a2
JB
893 DRM_DEBUG_KMS("vblank wait timed out\n");
894}
895
fbf49ea2
VS
896static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
897{
898 struct drm_i915_private *dev_priv = dev->dev_private;
899 u32 reg = PIPEDSL(pipe);
900 u32 line1, line2;
901 u32 line_mask;
902
903 if (IS_GEN2(dev))
904 line_mask = DSL_LINEMASK_GEN2;
905 else
906 line_mask = DSL_LINEMASK_GEN3;
907
908 line1 = I915_READ(reg) & line_mask;
909 mdelay(5);
910 line2 = I915_READ(reg) & line_mask;
911
912 return line1 == line2;
913}
914
ab7ad7f6
KP
915/*
916 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
917 * @dev: drm device
918 * @pipe: pipe to wait for
919 *
920 * After disabling a pipe, we can't wait for vblank in the usual way,
921 * spinning on the vblank interrupt status bit, since we won't actually
922 * see an interrupt when the pipe is disabled.
923 *
ab7ad7f6
KP
924 * On Gen4 and above:
925 * wait for the pipe register state bit to turn off
926 *
927 * Otherwise:
928 * wait for the display line value to settle (it usually
929 * ends up stopping at the start of the next frame).
58e10eb9 930 *
9d0498a2 931 */
58e10eb9 932void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
933{
934 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
935 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
936 pipe);
ab7ad7f6
KP
937
938 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 939 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
940
941 /* Wait for the Pipe State to go off */
58e10eb9
CW
942 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
943 100))
284637d9 944 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 945 } else {
ab7ad7f6 946 /* Wait for the display line to settle */
fbf49ea2 947 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 948 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 949 }
79e53945
JB
950}
951
b0ea7d37
DL
952/*
953 * ibx_digital_port_connected - is the specified port connected?
954 * @dev_priv: i915 private structure
955 * @port: the port to test
956 *
957 * Returns true if @port is connected, false otherwise.
958 */
959bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
960 struct intel_digital_port *port)
961{
962 u32 bit;
963
c36346e3
DL
964 if (HAS_PCH_IBX(dev_priv->dev)) {
965 switch(port->port) {
966 case PORT_B:
967 bit = SDE_PORTB_HOTPLUG;
968 break;
969 case PORT_C:
970 bit = SDE_PORTC_HOTPLUG;
971 break;
972 case PORT_D:
973 bit = SDE_PORTD_HOTPLUG;
974 break;
975 default:
976 return true;
977 }
978 } else {
979 switch(port->port) {
980 case PORT_B:
981 bit = SDE_PORTB_HOTPLUG_CPT;
982 break;
983 case PORT_C:
984 bit = SDE_PORTC_HOTPLUG_CPT;
985 break;
986 case PORT_D:
987 bit = SDE_PORTD_HOTPLUG_CPT;
988 break;
989 default:
990 return true;
991 }
b0ea7d37
DL
992 }
993
994 return I915_READ(SDEISR) & bit;
995}
996
b24e7179
JB
997static const char *state_string(bool enabled)
998{
999 return enabled ? "on" : "off";
1000}
1001
1002/* Only for pre-ILK configs */
55607e8a
DV
1003void assert_pll(struct drm_i915_private *dev_priv,
1004 enum pipe pipe, bool state)
b24e7179
JB
1005{
1006 int reg;
1007 u32 val;
1008 bool cur_state;
1009
1010 reg = DPLL(pipe);
1011 val = I915_READ(reg);
1012 cur_state = !!(val & DPLL_VCO_ENABLE);
1013 WARN(cur_state != state,
1014 "PLL state assertion failure (expected %s, current %s)\n",
1015 state_string(state), state_string(cur_state));
1016}
b24e7179 1017
23538ef1
JN
1018/* XXX: the dsi pll is shared between MIPI DSI ports */
1019static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1020{
1021 u32 val;
1022 bool cur_state;
1023
1024 mutex_lock(&dev_priv->dpio_lock);
1025 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1026 mutex_unlock(&dev_priv->dpio_lock);
1027
1028 cur_state = val & DSI_PLL_VCO_EN;
1029 WARN(cur_state != state,
1030 "DSI PLL state assertion failure (expected %s, current %s)\n",
1031 state_string(state), state_string(cur_state));
1032}
1033#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1034#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1035
55607e8a 1036struct intel_shared_dpll *
e2b78267
DV
1037intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1038{
1039 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1040
a43f6e0f 1041 if (crtc->config.shared_dpll < 0)
e2b78267
DV
1042 return NULL;
1043
a43f6e0f 1044 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
1045}
1046
040484af 1047/* For ILK+ */
55607e8a
DV
1048void assert_shared_dpll(struct drm_i915_private *dev_priv,
1049 struct intel_shared_dpll *pll,
1050 bool state)
040484af 1051{
040484af 1052 bool cur_state;
5358901f 1053 struct intel_dpll_hw_state hw_state;
040484af 1054
9d82aa17
ED
1055 if (HAS_PCH_LPT(dev_priv->dev)) {
1056 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1057 return;
1058 }
1059
92b27b08 1060 if (WARN (!pll,
46edb027 1061 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1062 return;
ee7b9f93 1063
5358901f 1064 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 1065 WARN(cur_state != state,
5358901f
DV
1066 "%s assertion failure (expected %s, current %s)\n",
1067 pll->name, state_string(state), state_string(cur_state));
040484af 1068}
040484af
JB
1069
1070static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1071 enum pipe pipe, bool state)
1072{
1073 int reg;
1074 u32 val;
1075 bool cur_state;
ad80a810
PZ
1076 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1077 pipe);
040484af 1078
affa9354
PZ
1079 if (HAS_DDI(dev_priv->dev)) {
1080 /* DDI does not have a specific FDI_TX register */
ad80a810 1081 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1082 val = I915_READ(reg);
ad80a810 1083 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1084 } else {
1085 reg = FDI_TX_CTL(pipe);
1086 val = I915_READ(reg);
1087 cur_state = !!(val & FDI_TX_ENABLE);
1088 }
040484af
JB
1089 WARN(cur_state != state,
1090 "FDI TX state assertion failure (expected %s, current %s)\n",
1091 state_string(state), state_string(cur_state));
1092}
1093#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1094#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1095
1096static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1097 enum pipe pipe, bool state)
1098{
1099 int reg;
1100 u32 val;
1101 bool cur_state;
1102
d63fa0dc
PZ
1103 reg = FDI_RX_CTL(pipe);
1104 val = I915_READ(reg);
1105 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1106 WARN(cur_state != state,
1107 "FDI RX state assertion failure (expected %s, current %s)\n",
1108 state_string(state), state_string(cur_state));
1109}
1110#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1111#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1112
1113static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1114 enum pipe pipe)
1115{
1116 int reg;
1117 u32 val;
1118
1119 /* ILK FDI PLL is always enabled */
3d13ef2e 1120 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1121 return;
1122
bf507ef7 1123 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1124 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1125 return;
1126
040484af
JB
1127 reg = FDI_TX_CTL(pipe);
1128 val = I915_READ(reg);
1129 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1130}
1131
55607e8a
DV
1132void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
040484af
JB
1134{
1135 int reg;
1136 u32 val;
55607e8a 1137 bool cur_state;
040484af
JB
1138
1139 reg = FDI_RX_CTL(pipe);
1140 val = I915_READ(reg);
55607e8a
DV
1141 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1142 WARN(cur_state != state,
1143 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1144 state_string(state), state_string(cur_state));
040484af
JB
1145}
1146
ea0760cf
JB
1147static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1148 enum pipe pipe)
1149{
1150 int pp_reg, lvds_reg;
1151 u32 val;
1152 enum pipe panel_pipe = PIPE_A;
0de3b485 1153 bool locked = true;
ea0760cf
JB
1154
1155 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1156 pp_reg = PCH_PP_CONTROL;
1157 lvds_reg = PCH_LVDS;
1158 } else {
1159 pp_reg = PP_CONTROL;
1160 lvds_reg = LVDS;
1161 }
1162
1163 val = I915_READ(pp_reg);
1164 if (!(val & PANEL_POWER_ON) ||
1165 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1166 locked = false;
1167
1168 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1169 panel_pipe = PIPE_B;
1170
1171 WARN(panel_pipe == pipe && locked,
1172 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1173 pipe_name(pipe));
ea0760cf
JB
1174}
1175
93ce0ba6
JN
1176static void assert_cursor(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
1178{
1179 struct drm_device *dev = dev_priv->dev;
1180 bool cur_state;
1181
d9d82081 1182 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1183 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1184 else if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev))
93ce0ba6 1185 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
d9d82081
PZ
1186 else
1187 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1188
1189 WARN(cur_state != state,
1190 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1191 pipe_name(pipe), state_string(state), state_string(cur_state));
1192}
1193#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1194#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1195
b840d907
JB
1196void assert_pipe(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, bool state)
b24e7179
JB
1198{
1199 int reg;
1200 u32 val;
63d7bbe9 1201 bool cur_state;
702e7a56
PZ
1202 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1203 pipe);
b24e7179 1204
8e636784
DV
1205 /* if we need the pipe A quirk it must be always on */
1206 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1207 state = true;
1208
da7e29bd 1209 if (!intel_display_power_enabled(dev_priv,
b97186f0 1210 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1211 cur_state = false;
1212 } else {
1213 reg = PIPECONF(cpu_transcoder);
1214 val = I915_READ(reg);
1215 cur_state = !!(val & PIPECONF_ENABLE);
1216 }
1217
63d7bbe9
JB
1218 WARN(cur_state != state,
1219 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1220 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1221}
1222
931872fc
CW
1223static void assert_plane(struct drm_i915_private *dev_priv,
1224 enum plane plane, bool state)
b24e7179
JB
1225{
1226 int reg;
1227 u32 val;
931872fc 1228 bool cur_state;
b24e7179
JB
1229
1230 reg = DSPCNTR(plane);
1231 val = I915_READ(reg);
931872fc
CW
1232 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1233 WARN(cur_state != state,
1234 "plane %c assertion failure (expected %s, current %s)\n",
1235 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1236}
1237
931872fc
CW
1238#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1239#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1240
b24e7179
JB
1241static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1242 enum pipe pipe)
1243{
653e1026 1244 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1245 int reg, i;
1246 u32 val;
1247 int cur_pipe;
1248
653e1026
VS
1249 /* Primary planes are fixed to pipes on gen4+ */
1250 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1251 reg = DSPCNTR(pipe);
1252 val = I915_READ(reg);
83f26f16 1253 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1254 "plane %c assertion failure, should be disabled but not\n",
1255 plane_name(pipe));
19ec1358 1256 return;
28c05794 1257 }
19ec1358 1258
b24e7179 1259 /* Need to check both planes against the pipe */
08e2a7de 1260 for_each_pipe(i) {
b24e7179
JB
1261 reg = DSPCNTR(i);
1262 val = I915_READ(reg);
1263 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1264 DISPPLANE_SEL_PIPE_SHIFT;
1265 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1266 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1267 plane_name(i), pipe_name(pipe));
b24e7179
JB
1268 }
1269}
1270
19332d7a
JB
1271static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1272 enum pipe pipe)
1273{
20674eef 1274 struct drm_device *dev = dev_priv->dev;
1fe47785 1275 int reg, sprite;
19332d7a
JB
1276 u32 val;
1277
20674eef 1278 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1279 for_each_sprite(pipe, sprite) {
1280 reg = SPCNTR(pipe, sprite);
20674eef 1281 val = I915_READ(reg);
83f26f16 1282 WARN(val & SP_ENABLE,
20674eef 1283 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1284 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1285 }
1286 } else if (INTEL_INFO(dev)->gen >= 7) {
1287 reg = SPRCTL(pipe);
19332d7a 1288 val = I915_READ(reg);
83f26f16 1289 WARN(val & SPRITE_ENABLE,
06da8da2 1290 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1291 plane_name(pipe), pipe_name(pipe));
1292 } else if (INTEL_INFO(dev)->gen >= 5) {
1293 reg = DVSCNTR(pipe);
19332d7a 1294 val = I915_READ(reg);
83f26f16 1295 WARN(val & DVS_ENABLE,
06da8da2 1296 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1297 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1298 }
1299}
1300
89eff4be 1301static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1302{
1303 u32 val;
1304 bool enabled;
1305
89eff4be 1306 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1307
92f2584a
JB
1308 val = I915_READ(PCH_DREF_CONTROL);
1309 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1310 DREF_SUPERSPREAD_SOURCE_MASK));
1311 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1312}
1313
ab9412ba
DV
1314static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1315 enum pipe pipe)
92f2584a
JB
1316{
1317 int reg;
1318 u32 val;
1319 bool enabled;
1320
ab9412ba 1321 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1322 val = I915_READ(reg);
1323 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1324 WARN(enabled,
1325 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1326 pipe_name(pipe));
92f2584a
JB
1327}
1328
4e634389
KP
1329static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1330 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1331{
1332 if ((val & DP_PORT_EN) == 0)
1333 return false;
1334
1335 if (HAS_PCH_CPT(dev_priv->dev)) {
1336 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1337 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1338 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1339 return false;
1340 } else {
1341 if ((val & DP_PIPE_MASK) != (pipe << 30))
1342 return false;
1343 }
1344 return true;
1345}
1346
1519b995
KP
1347static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1348 enum pipe pipe, u32 val)
1349{
dc0fa718 1350 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1351 return false;
1352
1353 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1354 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995
KP
1355 return false;
1356 } else {
dc0fa718 1357 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1358 return false;
1359 }
1360 return true;
1361}
1362
1363static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe, u32 val)
1365{
1366 if ((val & LVDS_PORT_EN) == 0)
1367 return false;
1368
1369 if (HAS_PCH_CPT(dev_priv->dev)) {
1370 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1371 return false;
1372 } else {
1373 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1374 return false;
1375 }
1376 return true;
1377}
1378
1379static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1380 enum pipe pipe, u32 val)
1381{
1382 if ((val & ADPA_DAC_ENABLE) == 0)
1383 return false;
1384 if (HAS_PCH_CPT(dev_priv->dev)) {
1385 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1386 return false;
1387 } else {
1388 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1389 return false;
1390 }
1391 return true;
1392}
1393
291906f1 1394static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1395 enum pipe pipe, int reg, u32 port_sel)
291906f1 1396{
47a05eca 1397 u32 val = I915_READ(reg);
4e634389 1398 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1399 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1400 reg, pipe_name(pipe));
de9a35ab 1401
75c5da27
DV
1402 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1403 && (val & DP_PIPEB_SELECT),
de9a35ab 1404 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1405}
1406
1407static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, int reg)
1409{
47a05eca 1410 u32 val = I915_READ(reg);
b70ad586 1411 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1412 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1413 reg, pipe_name(pipe));
de9a35ab 1414
dc0fa718 1415 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1416 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1417 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1418}
1419
1420static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1421 enum pipe pipe)
1422{
1423 int reg;
1424 u32 val;
291906f1 1425
f0575e92
KP
1426 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1427 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1428 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1429
1430 reg = PCH_ADPA;
1431 val = I915_READ(reg);
b70ad586 1432 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1433 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1434 pipe_name(pipe));
291906f1
JB
1435
1436 reg = PCH_LVDS;
1437 val = I915_READ(reg);
b70ad586 1438 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1439 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1440 pipe_name(pipe));
291906f1 1441
e2debe91
PZ
1442 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1443 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1444 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1445}
1446
40e9cf64
JB
1447static void intel_init_dpio(struct drm_device *dev)
1448{
1449 struct drm_i915_private *dev_priv = dev->dev_private;
1450
1451 if (!IS_VALLEYVIEW(dev))
1452 return;
1453
a09caddd
CML
1454 /*
1455 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1456 * CHV x1 PHY (DP/HDMI D)
1457 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1458 */
1459 if (IS_CHERRYVIEW(dev)) {
1460 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1461 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1462 } else {
1463 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1464 }
5382f5f3
JB
1465}
1466
1467static void intel_reset_dpio(struct drm_device *dev)
1468{
1469 struct drm_i915_private *dev_priv = dev->dev_private;
1470
1471 if (!IS_VALLEYVIEW(dev))
1472 return;
1473
e5cbfbfb
ID
1474 /*
1475 * Enable the CRI clock source so we can get at the display and the
1476 * reference clock for VGA hotplug / manual detection.
1477 */
404faabc 1478 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
e5cbfbfb 1479 DPLL_REFA_CLK_ENABLE_VLV |
404faabc
ID
1480 DPLL_INTEGRATED_CRI_CLK_VLV);
1481
076ed3b2
CML
1482 if (IS_CHERRYVIEW(dev)) {
1483 enum dpio_phy phy;
1484 u32 val;
1485
1486 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1487 /* Poll for phypwrgood signal */
1488 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1489 PHY_POWERGOOD(phy), 1))
1490 DRM_ERROR("Display PHY %d is not power up\n", phy);
1491
1492 /*
1493 * Deassert common lane reset for PHY.
1494 *
1495 * This should only be done on init and resume from S3
1496 * with both PLLs disabled, or we risk losing DPIO and
1497 * PLL synchronization.
1498 */
1499 val = I915_READ(DISPLAY_PHY_CONTROL);
1500 I915_WRITE(DISPLAY_PHY_CONTROL,
1501 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1502 }
1503
1504 } else {
1505 /*
1506 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1507 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1508 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1509 * b. The other bits such as sfr settings / modesel may all
1510 * be set to 0.
1511 *
1512 * This should only be done on init and resume from S3 with
1513 * both PLLs disabled, or we risk losing DPIO and PLL
1514 * synchronization.
1515 */
1516 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1517 }
40e9cf64
JB
1518}
1519
426115cf 1520static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1521{
426115cf
DV
1522 struct drm_device *dev = crtc->base.dev;
1523 struct drm_i915_private *dev_priv = dev->dev_private;
1524 int reg = DPLL(crtc->pipe);
1525 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1526
426115cf 1527 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1528
1529 /* No really, not for ILK+ */
1530 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1531
1532 /* PLL is protected by panel, make sure we can write it */
1533 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
426115cf 1534 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1535
426115cf
DV
1536 I915_WRITE(reg, dpll);
1537 POSTING_READ(reg);
1538 udelay(150);
1539
1540 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1541 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1542
1543 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1544 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1545
1546 /* We do this three times for luck */
426115cf 1547 I915_WRITE(reg, dpll);
87442f73
DV
1548 POSTING_READ(reg);
1549 udelay(150); /* wait for warmup */
426115cf 1550 I915_WRITE(reg, dpll);
87442f73
DV
1551 POSTING_READ(reg);
1552 udelay(150); /* wait for warmup */
426115cf 1553 I915_WRITE(reg, dpll);
87442f73
DV
1554 POSTING_READ(reg);
1555 udelay(150); /* wait for warmup */
1556}
1557
66e3d5c0 1558static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1559{
66e3d5c0
DV
1560 struct drm_device *dev = crtc->base.dev;
1561 struct drm_i915_private *dev_priv = dev->dev_private;
1562 int reg = DPLL(crtc->pipe);
1563 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1564
66e3d5c0 1565 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1566
63d7bbe9 1567 /* No really, not for ILK+ */
3d13ef2e 1568 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1569
1570 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1571 if (IS_MOBILE(dev) && !IS_I830(dev))
1572 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1573
66e3d5c0
DV
1574 I915_WRITE(reg, dpll);
1575
1576 /* Wait for the clocks to stabilize. */
1577 POSTING_READ(reg);
1578 udelay(150);
1579
1580 if (INTEL_INFO(dev)->gen >= 4) {
1581 I915_WRITE(DPLL_MD(crtc->pipe),
1582 crtc->config.dpll_hw_state.dpll_md);
1583 } else {
1584 /* The pixel multiplier can only be updated once the
1585 * DPLL is enabled and the clocks are stable.
1586 *
1587 * So write it again.
1588 */
1589 I915_WRITE(reg, dpll);
1590 }
63d7bbe9
JB
1591
1592 /* We do this three times for luck */
66e3d5c0 1593 I915_WRITE(reg, dpll);
63d7bbe9
JB
1594 POSTING_READ(reg);
1595 udelay(150); /* wait for warmup */
66e3d5c0 1596 I915_WRITE(reg, dpll);
63d7bbe9
JB
1597 POSTING_READ(reg);
1598 udelay(150); /* wait for warmup */
66e3d5c0 1599 I915_WRITE(reg, dpll);
63d7bbe9
JB
1600 POSTING_READ(reg);
1601 udelay(150); /* wait for warmup */
1602}
1603
1604/**
50b44a44 1605 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1606 * @dev_priv: i915 private structure
1607 * @pipe: pipe PLL to disable
1608 *
1609 * Disable the PLL for @pipe, making sure the pipe is off first.
1610 *
1611 * Note! This is for pre-ILK only.
1612 */
50b44a44 1613static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1614{
63d7bbe9
JB
1615 /* Don't disable pipe A or pipe A PLLs if needed */
1616 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1617 return;
1618
1619 /* Make sure the pipe isn't still relying on us */
1620 assert_pipe_disabled(dev_priv, pipe);
1621
50b44a44
DV
1622 I915_WRITE(DPLL(pipe), 0);
1623 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1624}
1625
f6071166
JB
1626static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1627{
1628 u32 val = 0;
1629
1630 /* Make sure the pipe isn't still relying on us */
1631 assert_pipe_disabled(dev_priv, pipe);
1632
e5cbfbfb
ID
1633 /*
1634 * Leave integrated clock source and reference clock enabled for pipe B.
1635 * The latter is needed for VGA hotplug / manual detection.
1636 */
f6071166 1637 if (pipe == PIPE_B)
e5cbfbfb 1638 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1639 I915_WRITE(DPLL(pipe), val);
1640 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1641
1642}
1643
1644static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1645{
1646 int dpll = DPLL(pipe);
1647 u32 val;
1648
1649 /* Set PLL en = 0 */
1650 val = I915_READ(dpll);
1651 val &= ~DPLL_VCO_ENABLE;
1652 I915_WRITE(dpll, val);
1653
f6071166
JB
1654}
1655
e4607fcf
CML
1656void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1657 struct intel_digital_port *dport)
89b667f8
JB
1658{
1659 u32 port_mask;
00fc31b7 1660 int dpll_reg;
89b667f8 1661
e4607fcf
CML
1662 switch (dport->port) {
1663 case PORT_B:
89b667f8 1664 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1665 dpll_reg = DPLL(0);
e4607fcf
CML
1666 break;
1667 case PORT_C:
89b667f8 1668 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1669 dpll_reg = DPLL(0);
1670 break;
1671 case PORT_D:
1672 port_mask = DPLL_PORTD_READY_MASK;
1673 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1674 break;
1675 default:
1676 BUG();
1677 }
89b667f8 1678
00fc31b7 1679 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1680 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1681 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1682}
1683
92f2584a 1684/**
e72f9fbf 1685 * ironlake_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1686 * @dev_priv: i915 private structure
1687 * @pipe: pipe PLL to enable
1688 *
1689 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1690 * drives the transcoder clock.
1691 */
e2b78267 1692static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1693{
3d13ef2e
DL
1694 struct drm_device *dev = crtc->base.dev;
1695 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1696 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1697
48da64a8 1698 /* PCH PLLs only available on ILK, SNB and IVB */
3d13ef2e 1699 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1700 if (WARN_ON(pll == NULL))
48da64a8
CW
1701 return;
1702
1703 if (WARN_ON(pll->refcount == 0))
1704 return;
ee7b9f93 1705
46edb027
DV
1706 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1707 pll->name, pll->active, pll->on,
e2b78267 1708 crtc->base.base.id);
92f2584a 1709
cdbd2316
DV
1710 if (pll->active++) {
1711 WARN_ON(!pll->on);
e9d6944e 1712 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1713 return;
1714 }
f4a091c7 1715 WARN_ON(pll->on);
ee7b9f93 1716
46edb027 1717 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1718 pll->enable(dev_priv, pll);
ee7b9f93 1719 pll->on = true;
92f2584a
JB
1720}
1721
e2b78267 1722static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1723{
3d13ef2e
DL
1724 struct drm_device *dev = crtc->base.dev;
1725 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1726 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1727
92f2584a 1728 /* PCH only available on ILK+ */
3d13ef2e 1729 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1730 if (WARN_ON(pll == NULL))
ee7b9f93 1731 return;
92f2584a 1732
48da64a8
CW
1733 if (WARN_ON(pll->refcount == 0))
1734 return;
7a419866 1735
46edb027
DV
1736 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1737 pll->name, pll->active, pll->on,
e2b78267 1738 crtc->base.base.id);
7a419866 1739
48da64a8 1740 if (WARN_ON(pll->active == 0)) {
e9d6944e 1741 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1742 return;
1743 }
1744
e9d6944e 1745 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1746 WARN_ON(!pll->on);
cdbd2316 1747 if (--pll->active)
7a419866 1748 return;
ee7b9f93 1749
46edb027 1750 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1751 pll->disable(dev_priv, pll);
ee7b9f93 1752 pll->on = false;
92f2584a
JB
1753}
1754
b8a4f404
PZ
1755static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1756 enum pipe pipe)
040484af 1757{
23670b32 1758 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1759 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1761 uint32_t reg, val, pipeconf_val;
040484af
JB
1762
1763 /* PCH only available on ILK+ */
3d13ef2e 1764 BUG_ON(INTEL_INFO(dev)->gen < 5);
040484af
JB
1765
1766 /* Make sure PCH DPLL is enabled */
e72f9fbf 1767 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1768 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1769
1770 /* FDI must be feeding us bits for PCH ports */
1771 assert_fdi_tx_enabled(dev_priv, pipe);
1772 assert_fdi_rx_enabled(dev_priv, pipe);
1773
23670b32
DV
1774 if (HAS_PCH_CPT(dev)) {
1775 /* Workaround: Set the timing override bit before enabling the
1776 * pch transcoder. */
1777 reg = TRANS_CHICKEN2(pipe);
1778 val = I915_READ(reg);
1779 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1780 I915_WRITE(reg, val);
59c859d6 1781 }
23670b32 1782
ab9412ba 1783 reg = PCH_TRANSCONF(pipe);
040484af 1784 val = I915_READ(reg);
5f7f726d 1785 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1786
1787 if (HAS_PCH_IBX(dev_priv->dev)) {
1788 /*
1789 * make the BPC in transcoder be consistent with
1790 * that in pipeconf reg.
1791 */
dfd07d72
DV
1792 val &= ~PIPECONF_BPC_MASK;
1793 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1794 }
5f7f726d
PZ
1795
1796 val &= ~TRANS_INTERLACE_MASK;
1797 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1798 if (HAS_PCH_IBX(dev_priv->dev) &&
1799 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1800 val |= TRANS_LEGACY_INTERLACED_ILK;
1801 else
1802 val |= TRANS_INTERLACED;
5f7f726d
PZ
1803 else
1804 val |= TRANS_PROGRESSIVE;
1805
040484af
JB
1806 I915_WRITE(reg, val | TRANS_ENABLE);
1807 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1808 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1809}
1810
8fb033d7 1811static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1812 enum transcoder cpu_transcoder)
040484af 1813{
8fb033d7 1814 u32 val, pipeconf_val;
8fb033d7
PZ
1815
1816 /* PCH only available on ILK+ */
3d13ef2e 1817 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
8fb033d7 1818
8fb033d7 1819 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1820 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1821 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1822
223a6fdf
PZ
1823 /* Workaround: set timing override bit. */
1824 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1825 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1826 I915_WRITE(_TRANSA_CHICKEN2, val);
1827
25f3ef11 1828 val = TRANS_ENABLE;
937bb610 1829 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1830
9a76b1c6
PZ
1831 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1832 PIPECONF_INTERLACED_ILK)
a35f2679 1833 val |= TRANS_INTERLACED;
8fb033d7
PZ
1834 else
1835 val |= TRANS_PROGRESSIVE;
1836
ab9412ba
DV
1837 I915_WRITE(LPT_TRANSCONF, val);
1838 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1839 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1840}
1841
b8a4f404
PZ
1842static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1843 enum pipe pipe)
040484af 1844{
23670b32
DV
1845 struct drm_device *dev = dev_priv->dev;
1846 uint32_t reg, val;
040484af
JB
1847
1848 /* FDI relies on the transcoder */
1849 assert_fdi_tx_disabled(dev_priv, pipe);
1850 assert_fdi_rx_disabled(dev_priv, pipe);
1851
291906f1
JB
1852 /* Ports must be off as well */
1853 assert_pch_ports_disabled(dev_priv, pipe);
1854
ab9412ba 1855 reg = PCH_TRANSCONF(pipe);
040484af
JB
1856 val = I915_READ(reg);
1857 val &= ~TRANS_ENABLE;
1858 I915_WRITE(reg, val);
1859 /* wait for PCH transcoder off, transcoder state */
1860 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1861 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1862
1863 if (!HAS_PCH_IBX(dev)) {
1864 /* Workaround: Clear the timing override chicken bit again. */
1865 reg = TRANS_CHICKEN2(pipe);
1866 val = I915_READ(reg);
1867 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1868 I915_WRITE(reg, val);
1869 }
040484af
JB
1870}
1871
ab4d966c 1872static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1873{
8fb033d7
PZ
1874 u32 val;
1875
ab9412ba 1876 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1877 val &= ~TRANS_ENABLE;
ab9412ba 1878 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1879 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1880 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1881 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1882
1883 /* Workaround: clear timing override bit. */
1884 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1885 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1886 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1887}
1888
b24e7179 1889/**
309cfea8 1890 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1891 * @crtc: crtc responsible for the pipe
b24e7179 1892 *
0372264a 1893 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1894 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1895 */
e1fdc473 1896static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1897{
0372264a
PZ
1898 struct drm_device *dev = crtc->base.dev;
1899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1901 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1902 pipe);
1a240d4d 1903 enum pipe pch_transcoder;
b24e7179
JB
1904 int reg;
1905 u32 val;
1906
58c6eaa2 1907 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1908 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1909 assert_sprites_disabled(dev_priv, pipe);
1910
681e5811 1911 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1912 pch_transcoder = TRANSCODER_A;
1913 else
1914 pch_transcoder = pipe;
1915
b24e7179
JB
1916 /*
1917 * A pipe without a PLL won't actually be able to drive bits from
1918 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1919 * need the check.
1920 */
1921 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 1922 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
1923 assert_dsi_pll_enabled(dev_priv);
1924 else
1925 assert_pll_enabled(dev_priv, pipe);
040484af 1926 else {
30421c4f 1927 if (crtc->config.has_pch_encoder) {
040484af 1928 /* if driving the PCH, we need FDI enabled */
cc391bbb 1929 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1930 assert_fdi_tx_pll_enabled(dev_priv,
1931 (enum pipe) cpu_transcoder);
040484af
JB
1932 }
1933 /* FIXME: assert CPU port conditions for SNB+ */
1934 }
b24e7179 1935
702e7a56 1936 reg = PIPECONF(cpu_transcoder);
b24e7179 1937 val = I915_READ(reg);
7ad25d48
PZ
1938 if (val & PIPECONF_ENABLE) {
1939 WARN_ON(!(pipe == PIPE_A &&
1940 dev_priv->quirks & QUIRK_PIPEA_FORCE));
00d70b15 1941 return;
7ad25d48 1942 }
00d70b15
CW
1943
1944 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1945 POSTING_READ(reg);
b24e7179
JB
1946}
1947
1948/**
309cfea8 1949 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
1950 * @dev_priv: i915 private structure
1951 * @pipe: pipe to disable
1952 *
1953 * Disable @pipe, making sure that various hardware specific requirements
1954 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1955 *
1956 * @pipe should be %PIPE_A or %PIPE_B.
1957 *
1958 * Will wait until the pipe has shut down before returning.
1959 */
1960static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1961 enum pipe pipe)
1962{
702e7a56
PZ
1963 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1964 pipe);
b24e7179
JB
1965 int reg;
1966 u32 val;
1967
1968 /*
1969 * Make sure planes won't keep trying to pump pixels to us,
1970 * or we might hang the display.
1971 */
1972 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1973 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1974 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
1975
1976 /* Don't disable pipe A or pipe A PLLs if needed */
1977 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1978 return;
1979
702e7a56 1980 reg = PIPECONF(cpu_transcoder);
b24e7179 1981 val = I915_READ(reg);
00d70b15
CW
1982 if ((val & PIPECONF_ENABLE) == 0)
1983 return;
1984
1985 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
1986 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1987}
1988
d74362c9
KP
1989/*
1990 * Plane regs are double buffered, going from enabled->disabled needs a
1991 * trigger in order to latch. The display address reg provides this.
1992 */
1dba99f4
VS
1993void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1994 enum plane plane)
d74362c9 1995{
3d13ef2e
DL
1996 struct drm_device *dev = dev_priv->dev;
1997 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
1998
1999 I915_WRITE(reg, I915_READ(reg));
2000 POSTING_READ(reg);
d74362c9
KP
2001}
2002
b24e7179 2003/**
262ca2b0 2004 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
b24e7179
JB
2005 * @dev_priv: i915 private structure
2006 * @plane: plane to enable
2007 * @pipe: pipe being fed
2008 *
2009 * Enable @plane on @pipe, making sure that @pipe is running first.
2010 */
262ca2b0
MR
2011static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2012 enum plane plane, enum pipe pipe)
b24e7179 2013{
939c2fe8
VS
2014 struct intel_crtc *intel_crtc =
2015 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2016 int reg;
2017 u32 val;
2018
2019 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2020 assert_pipe_enabled(dev_priv, pipe);
2021
98ec7739
VS
2022 if (intel_crtc->primary_enabled)
2023 return;
0037f71c 2024
4c445e0e 2025 intel_crtc->primary_enabled = true;
939c2fe8 2026
b24e7179
JB
2027 reg = DSPCNTR(plane);
2028 val = I915_READ(reg);
10efa932 2029 WARN_ON(val & DISPLAY_PLANE_ENABLE);
00d70b15
CW
2030
2031 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1dba99f4 2032 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2033 intel_wait_for_vblank(dev_priv->dev, pipe);
2034}
2035
b24e7179 2036/**
262ca2b0 2037 * intel_disable_primary_hw_plane - disable the primary hardware plane
b24e7179
JB
2038 * @dev_priv: i915 private structure
2039 * @plane: plane to disable
2040 * @pipe: pipe consuming the data
2041 *
2042 * Disable @plane; should be an independent operation.
2043 */
262ca2b0
MR
2044static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2045 enum plane plane, enum pipe pipe)
b24e7179 2046{
939c2fe8
VS
2047 struct intel_crtc *intel_crtc =
2048 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
2049 int reg;
2050 u32 val;
2051
98ec7739
VS
2052 if (!intel_crtc->primary_enabled)
2053 return;
0037f71c 2054
4c445e0e 2055 intel_crtc->primary_enabled = false;
939c2fe8 2056
b24e7179
JB
2057 reg = DSPCNTR(plane);
2058 val = I915_READ(reg);
10efa932 2059 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
00d70b15
CW
2060
2061 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1dba99f4 2062 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
2063 intel_wait_for_vblank(dev_priv->dev, pipe);
2064}
2065
693db184
CW
2066static bool need_vtd_wa(struct drm_device *dev)
2067{
2068#ifdef CONFIG_INTEL_IOMMU
2069 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2070 return true;
2071#endif
2072 return false;
2073}
2074
a57ce0b2
JB
2075static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2076{
2077 int tile_height;
2078
2079 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2080 return ALIGN(height, tile_height);
2081}
2082
127bd2ac 2083int
48b956c5 2084intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 2085 struct drm_i915_gem_object *obj,
919926ae 2086 struct intel_ring_buffer *pipelined)
6b95a207 2087{
ce453d81 2088 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
2089 u32 alignment;
2090 int ret;
2091
05394f39 2092 switch (obj->tiling_mode) {
6b95a207 2093 case I915_TILING_NONE:
534843da
CW
2094 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2095 alignment = 128 * 1024;
a6c45cf0 2096 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2097 alignment = 4 * 1024;
2098 else
2099 alignment = 64 * 1024;
6b95a207
KH
2100 break;
2101 case I915_TILING_X:
2102 /* pin() will align the object as required by fence */
2103 alignment = 0;
2104 break;
2105 case I915_TILING_Y:
80075d49 2106 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
2107 return -EINVAL;
2108 default:
2109 BUG();
2110 }
2111
693db184
CW
2112 /* Note that the w/a also requires 64 PTE of padding following the
2113 * bo. We currently fill all unused PTE with the shadow page and so
2114 * we should always have valid PTE following the scanout preventing
2115 * the VT-d warning.
2116 */
2117 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2118 alignment = 256 * 1024;
2119
ce453d81 2120 dev_priv->mm.interruptible = false;
2da3b9b9 2121 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 2122 if (ret)
ce453d81 2123 goto err_interruptible;
6b95a207
KH
2124
2125 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2126 * fence, whereas 965+ only requires a fence if using
2127 * framebuffer compression. For simplicity, we always install
2128 * a fence as the cost is not that onerous.
2129 */
06d98131 2130 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2131 if (ret)
2132 goto err_unpin;
1690e1eb 2133
9a5a53b3 2134 i915_gem_object_pin_fence(obj);
6b95a207 2135
ce453d81 2136 dev_priv->mm.interruptible = true;
6b95a207 2137 return 0;
48b956c5
CW
2138
2139err_unpin:
cc98b413 2140 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2141err_interruptible:
2142 dev_priv->mm.interruptible = true;
48b956c5 2143 return ret;
6b95a207
KH
2144}
2145
1690e1eb
CW
2146void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2147{
2148 i915_gem_object_unpin_fence(obj);
cc98b413 2149 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2150}
2151
c2c75131
DV
2152/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2153 * is assumed to be a power-of-two. */
bc752862
CW
2154unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2155 unsigned int tiling_mode,
2156 unsigned int cpp,
2157 unsigned int pitch)
c2c75131 2158{
bc752862
CW
2159 if (tiling_mode != I915_TILING_NONE) {
2160 unsigned int tile_rows, tiles;
c2c75131 2161
bc752862
CW
2162 tile_rows = *y / 8;
2163 *y %= 8;
c2c75131 2164
bc752862
CW
2165 tiles = *x / (512/cpp);
2166 *x %= 512/cpp;
2167
2168 return tile_rows * pitch * 8 + tiles * 4096;
2169 } else {
2170 unsigned int offset;
2171
2172 offset = *y * pitch + *x * cpp;
2173 *y = 0;
2174 *x = (offset & 4095) / cpp;
2175 return offset & -4096;
2176 }
c2c75131
DV
2177}
2178
46f297fb
JB
2179int intel_format_to_fourcc(int format)
2180{
2181 switch (format) {
2182 case DISPPLANE_8BPP:
2183 return DRM_FORMAT_C8;
2184 case DISPPLANE_BGRX555:
2185 return DRM_FORMAT_XRGB1555;
2186 case DISPPLANE_BGRX565:
2187 return DRM_FORMAT_RGB565;
2188 default:
2189 case DISPPLANE_BGRX888:
2190 return DRM_FORMAT_XRGB8888;
2191 case DISPPLANE_RGBX888:
2192 return DRM_FORMAT_XBGR8888;
2193 case DISPPLANE_BGRX101010:
2194 return DRM_FORMAT_XRGB2101010;
2195 case DISPPLANE_RGBX101010:
2196 return DRM_FORMAT_XBGR2101010;
2197 }
2198}
2199
484b41dd 2200static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2201 struct intel_plane_config *plane_config)
2202{
2203 struct drm_device *dev = crtc->base.dev;
2204 struct drm_i915_gem_object *obj = NULL;
2205 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2206 u32 base = plane_config->base;
2207
ff2652ea
CW
2208 if (plane_config->size == 0)
2209 return false;
2210
46f297fb
JB
2211 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2212 plane_config->size);
2213 if (!obj)
484b41dd 2214 return false;
46f297fb
JB
2215
2216 if (plane_config->tiled) {
2217 obj->tiling_mode = I915_TILING_X;
66e514c1 2218 obj->stride = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2219 }
2220
66e514c1
DA
2221 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2222 mode_cmd.width = crtc->base.primary->fb->width;
2223 mode_cmd.height = crtc->base.primary->fb->height;
2224 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
46f297fb
JB
2225
2226 mutex_lock(&dev->struct_mutex);
2227
66e514c1 2228 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
484b41dd 2229 &mode_cmd, obj)) {
46f297fb
JB
2230 DRM_DEBUG_KMS("intel fb init failed\n");
2231 goto out_unref_obj;
2232 }
2233
2234 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2235
2236 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2237 return true;
46f297fb
JB
2238
2239out_unref_obj:
2240 drm_gem_object_unreference(&obj->base);
2241 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2242 return false;
2243}
2244
2245static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2246 struct intel_plane_config *plane_config)
2247{
2248 struct drm_device *dev = intel_crtc->base.dev;
2249 struct drm_crtc *c;
2250 struct intel_crtc *i;
2251 struct intel_framebuffer *fb;
2252
66e514c1 2253 if (!intel_crtc->base.primary->fb)
484b41dd
JB
2254 return;
2255
2256 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2257 return;
2258
66e514c1
DA
2259 kfree(intel_crtc->base.primary->fb);
2260 intel_crtc->base.primary->fb = NULL;
484b41dd
JB
2261
2262 /*
2263 * Failed to alloc the obj, check to see if we should share
2264 * an fb with another CRTC instead
2265 */
2266 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
2267 i = to_intel_crtc(c);
2268
2269 if (c == &intel_crtc->base)
2270 continue;
2271
66e514c1 2272 if (!i->active || !c->primary->fb)
484b41dd
JB
2273 continue;
2274
66e514c1 2275 fb = to_intel_framebuffer(c->primary->fb);
484b41dd 2276 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
66e514c1
DA
2277 drm_framebuffer_reference(c->primary->fb);
2278 intel_crtc->base.primary->fb = c->primary->fb;
484b41dd
JB
2279 break;
2280 }
2281 }
46f297fb
JB
2282}
2283
262ca2b0
MR
2284static int i9xx_update_primary_plane(struct drm_crtc *crtc,
2285 struct drm_framebuffer *fb,
2286 int x, int y)
81255565
JB
2287{
2288 struct drm_device *dev = crtc->dev;
2289 struct drm_i915_private *dev_priv = dev->dev_private;
2290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2291 struct intel_framebuffer *intel_fb;
05394f39 2292 struct drm_i915_gem_object *obj;
81255565 2293 int plane = intel_crtc->plane;
e506a0c6 2294 unsigned long linear_offset;
81255565 2295 u32 dspcntr;
5eddb70b 2296 u32 reg;
81255565 2297
81255565
JB
2298 intel_fb = to_intel_framebuffer(fb);
2299 obj = intel_fb->obj;
81255565 2300
5eddb70b
CW
2301 reg = DSPCNTR(plane);
2302 dspcntr = I915_READ(reg);
81255565
JB
2303 /* Mask out pixel format bits in case we change it */
2304 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2305 switch (fb->pixel_format) {
2306 case DRM_FORMAT_C8:
81255565
JB
2307 dspcntr |= DISPPLANE_8BPP;
2308 break;
57779d06
VS
2309 case DRM_FORMAT_XRGB1555:
2310 case DRM_FORMAT_ARGB1555:
2311 dspcntr |= DISPPLANE_BGRX555;
81255565 2312 break;
57779d06
VS
2313 case DRM_FORMAT_RGB565:
2314 dspcntr |= DISPPLANE_BGRX565;
2315 break;
2316 case DRM_FORMAT_XRGB8888:
2317 case DRM_FORMAT_ARGB8888:
2318 dspcntr |= DISPPLANE_BGRX888;
2319 break;
2320 case DRM_FORMAT_XBGR8888:
2321 case DRM_FORMAT_ABGR8888:
2322 dspcntr |= DISPPLANE_RGBX888;
2323 break;
2324 case DRM_FORMAT_XRGB2101010:
2325 case DRM_FORMAT_ARGB2101010:
2326 dspcntr |= DISPPLANE_BGRX101010;
2327 break;
2328 case DRM_FORMAT_XBGR2101010:
2329 case DRM_FORMAT_ABGR2101010:
2330 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2331 break;
2332 default:
baba133a 2333 BUG();
81255565 2334 }
57779d06 2335
a6c45cf0 2336 if (INTEL_INFO(dev)->gen >= 4) {
05394f39 2337 if (obj->tiling_mode != I915_TILING_NONE)
81255565
JB
2338 dspcntr |= DISPPLANE_TILED;
2339 else
2340 dspcntr &= ~DISPPLANE_TILED;
2341 }
2342
de1aa629
VS
2343 if (IS_G4X(dev))
2344 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2345
5eddb70b 2346 I915_WRITE(reg, dspcntr);
81255565 2347
e506a0c6 2348 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
81255565 2349
c2c75131
DV
2350 if (INTEL_INFO(dev)->gen >= 4) {
2351 intel_crtc->dspaddr_offset =
bc752862
CW
2352 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2353 fb->bits_per_pixel / 8,
2354 fb->pitches[0]);
c2c75131
DV
2355 linear_offset -= intel_crtc->dspaddr_offset;
2356 } else {
e506a0c6 2357 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2358 }
e506a0c6 2359
f343c5f6
BW
2360 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2361 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2362 fb->pitches[0]);
01f2c773 2363 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2364 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2365 I915_WRITE(DSPSURF(plane),
2366 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2367 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2368 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2369 } else
f343c5f6 2370 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2371 POSTING_READ(reg);
81255565 2372
17638cd6
JB
2373 return 0;
2374}
2375
262ca2b0
MR
2376static int ironlake_update_primary_plane(struct drm_crtc *crtc,
2377 struct drm_framebuffer *fb,
2378 int x, int y)
17638cd6
JB
2379{
2380 struct drm_device *dev = crtc->dev;
2381 struct drm_i915_private *dev_priv = dev->dev_private;
2382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2383 struct intel_framebuffer *intel_fb;
2384 struct drm_i915_gem_object *obj;
2385 int plane = intel_crtc->plane;
e506a0c6 2386 unsigned long linear_offset;
17638cd6
JB
2387 u32 dspcntr;
2388 u32 reg;
2389
17638cd6
JB
2390 intel_fb = to_intel_framebuffer(fb);
2391 obj = intel_fb->obj;
2392
2393 reg = DSPCNTR(plane);
2394 dspcntr = I915_READ(reg);
2395 /* Mask out pixel format bits in case we change it */
2396 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2397 switch (fb->pixel_format) {
2398 case DRM_FORMAT_C8:
17638cd6
JB
2399 dspcntr |= DISPPLANE_8BPP;
2400 break;
57779d06
VS
2401 case DRM_FORMAT_RGB565:
2402 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2403 break;
57779d06
VS
2404 case DRM_FORMAT_XRGB8888:
2405 case DRM_FORMAT_ARGB8888:
2406 dspcntr |= DISPPLANE_BGRX888;
2407 break;
2408 case DRM_FORMAT_XBGR8888:
2409 case DRM_FORMAT_ABGR8888:
2410 dspcntr |= DISPPLANE_RGBX888;
2411 break;
2412 case DRM_FORMAT_XRGB2101010:
2413 case DRM_FORMAT_ARGB2101010:
2414 dspcntr |= DISPPLANE_BGRX101010;
2415 break;
2416 case DRM_FORMAT_XBGR2101010:
2417 case DRM_FORMAT_ABGR2101010:
2418 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2419 break;
2420 default:
baba133a 2421 BUG();
17638cd6
JB
2422 }
2423
2424 if (obj->tiling_mode != I915_TILING_NONE)
2425 dspcntr |= DISPPLANE_TILED;
2426 else
2427 dspcntr &= ~DISPPLANE_TILED;
2428
b42c6009 2429 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1f5d76db
PZ
2430 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2431 else
2432 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6
JB
2433
2434 I915_WRITE(reg, dspcntr);
2435
e506a0c6 2436 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
c2c75131 2437 intel_crtc->dspaddr_offset =
bc752862
CW
2438 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2439 fb->bits_per_pixel / 8,
2440 fb->pitches[0]);
c2c75131 2441 linear_offset -= intel_crtc->dspaddr_offset;
17638cd6 2442
f343c5f6
BW
2443 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2444 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2445 fb->pitches[0]);
01f2c773 2446 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2447 I915_WRITE(DSPSURF(plane),
2448 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2449 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2450 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2451 } else {
2452 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2453 I915_WRITE(DSPLINOFF(plane), linear_offset);
2454 }
17638cd6
JB
2455 POSTING_READ(reg);
2456
2457 return 0;
2458}
2459
2460/* Assume fb object is pinned & idle & fenced and just update base pointers */
2461static int
2462intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2463 int x, int y, enum mode_set_atomic state)
2464{
2465 struct drm_device *dev = crtc->dev;
2466 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2467
6b8e6ed0
CW
2468 if (dev_priv->display.disable_fbc)
2469 dev_priv->display.disable_fbc(dev);
3dec0095 2470 intel_increase_pllclock(crtc);
81255565 2471
262ca2b0 2472 return dev_priv->display.update_primary_plane(crtc, fb, x, y);
81255565
JB
2473}
2474
96a02917
VS
2475void intel_display_handle_reset(struct drm_device *dev)
2476{
2477 struct drm_i915_private *dev_priv = dev->dev_private;
2478 struct drm_crtc *crtc;
2479
2480 /*
2481 * Flips in the rings have been nuked by the reset,
2482 * so complete all pending flips so that user space
2483 * will get its events and not get stuck.
2484 *
2485 * Also update the base address of all primary
2486 * planes to the the last fb to make sure we're
2487 * showing the correct fb after a reset.
2488 *
2489 * Need to make two loops over the crtcs so that we
2490 * don't try to grab a crtc mutex before the
2491 * pending_flip_queue really got woken up.
2492 */
2493
2494 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 enum plane plane = intel_crtc->plane;
2497
2498 intel_prepare_page_flip(dev, plane);
2499 intel_finish_page_flip_plane(dev, plane);
2500 }
2501
2502 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2504
2505 mutex_lock(&crtc->mutex);
947fdaad
CW
2506 /*
2507 * FIXME: Once we have proper support for primary planes (and
2508 * disabling them without disabling the entire crtc) allow again
66e514c1 2509 * a NULL crtc->primary->fb.
947fdaad 2510 */
f4510a27 2511 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 2512 dev_priv->display.update_primary_plane(crtc,
66e514c1 2513 crtc->primary->fb,
262ca2b0
MR
2514 crtc->x,
2515 crtc->y);
96a02917
VS
2516 mutex_unlock(&crtc->mutex);
2517 }
2518}
2519
14667a4b
CW
2520static int
2521intel_finish_fb(struct drm_framebuffer *old_fb)
2522{
2523 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2524 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2525 bool was_interruptible = dev_priv->mm.interruptible;
2526 int ret;
2527
14667a4b
CW
2528 /* Big Hammer, we also need to ensure that any pending
2529 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2530 * current scanout is retired before unpinning the old
2531 * framebuffer.
2532 *
2533 * This should only fail upon a hung GPU, in which case we
2534 * can safely continue.
2535 */
2536 dev_priv->mm.interruptible = false;
2537 ret = i915_gem_object_finish_gpu(obj);
2538 dev_priv->mm.interruptible = was_interruptible;
2539
2540 return ret;
2541}
2542
7d5e3799
CW
2543static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2544{
2545 struct drm_device *dev = crtc->dev;
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2548 unsigned long flags;
2549 bool pending;
2550
2551 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2552 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2553 return false;
2554
2555 spin_lock_irqsave(&dev->event_lock, flags);
2556 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2557 spin_unlock_irqrestore(&dev->event_lock, flags);
2558
2559 return pending;
2560}
2561
5c3b82e2 2562static int
3c4fdcfb 2563intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2564 struct drm_framebuffer *fb)
79e53945
JB
2565{
2566 struct drm_device *dev = crtc->dev;
6b8e6ed0 2567 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
94352cf9 2569 struct drm_framebuffer *old_fb;
5c3b82e2 2570 int ret;
79e53945 2571
7d5e3799
CW
2572 if (intel_crtc_has_pending_flip(crtc)) {
2573 DRM_ERROR("pipe is still busy with an old pageflip\n");
2574 return -EBUSY;
2575 }
2576
79e53945 2577 /* no fb bound */
94352cf9 2578 if (!fb) {
a5071c2f 2579 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2580 return 0;
2581 }
2582
7eb552ae 2583 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2584 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2585 plane_name(intel_crtc->plane),
2586 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2587 return -EINVAL;
79e53945
JB
2588 }
2589
5c3b82e2 2590 mutex_lock(&dev->struct_mutex);
265db958 2591 ret = intel_pin_and_fence_fb_obj(dev,
94352cf9 2592 to_intel_framebuffer(fb)->obj,
919926ae 2593 NULL);
8ac36ec1 2594 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2595 if (ret != 0) {
a5071c2f 2596 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2597 return ret;
2598 }
79e53945 2599
bb2043de
DL
2600 /*
2601 * Update pipe size and adjust fitter if needed: the reason for this is
2602 * that in compute_mode_changes we check the native mode (not the pfit
2603 * mode) to see if we can flip rather than do a full mode set. In the
2604 * fastboot case, we'll flip, but if we don't update the pipesrc and
2605 * pfit state, we'll end up with a big fb scanned out into the wrong
2606 * sized surface.
2607 *
2608 * To fix this properly, we need to hoist the checks up into
2609 * compute_mode_changes (or above), check the actual pfit state and
2610 * whether the platform allows pfit disable with pipe active, and only
2611 * then update the pipesrc and pfit state, even on the flip path.
2612 */
d330a953 2613 if (i915.fastboot) {
d7bf63f2
DL
2614 const struct drm_display_mode *adjusted_mode =
2615 &intel_crtc->config.adjusted_mode;
2616
4d6a3e63 2617 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2618 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2619 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2620 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2621 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2622 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2623 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2624 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2625 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2626 }
0637d60d
JB
2627 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2628 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2629 }
2630
262ca2b0 2631 ret = dev_priv->display.update_primary_plane(crtc, fb, x, y);
4e6cfefc 2632 if (ret) {
8ac36ec1 2633 mutex_lock(&dev->struct_mutex);
94352cf9 2634 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
5c3b82e2 2635 mutex_unlock(&dev->struct_mutex);
a5071c2f 2636 DRM_ERROR("failed to update base address\n");
4e6cfefc 2637 return ret;
79e53945 2638 }
3c4fdcfb 2639
f4510a27
MR
2640 old_fb = crtc->primary->fb;
2641 crtc->primary->fb = fb;
6c4c86f5
DV
2642 crtc->x = x;
2643 crtc->y = y;
94352cf9 2644
b7f1de28 2645 if (old_fb) {
d7697eea
DV
2646 if (intel_crtc->active && old_fb != fb)
2647 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2648 mutex_lock(&dev->struct_mutex);
1690e1eb 2649 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
8ac36ec1 2650 mutex_unlock(&dev->struct_mutex);
b7f1de28 2651 }
652c393a 2652
8ac36ec1 2653 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2654 intel_update_fbc(dev);
4906557e 2655 intel_edp_psr_update(dev);
5c3b82e2 2656 mutex_unlock(&dev->struct_mutex);
79e53945 2657
5c3b82e2 2658 return 0;
79e53945
JB
2659}
2660
5e84e1a4
ZW
2661static void intel_fdi_normal_train(struct drm_crtc *crtc)
2662{
2663 struct drm_device *dev = crtc->dev;
2664 struct drm_i915_private *dev_priv = dev->dev_private;
2665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2666 int pipe = intel_crtc->pipe;
2667 u32 reg, temp;
2668
2669 /* enable normal train */
2670 reg = FDI_TX_CTL(pipe);
2671 temp = I915_READ(reg);
61e499bf 2672 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2673 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2674 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2675 } else {
2676 temp &= ~FDI_LINK_TRAIN_NONE;
2677 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2678 }
5e84e1a4
ZW
2679 I915_WRITE(reg, temp);
2680
2681 reg = FDI_RX_CTL(pipe);
2682 temp = I915_READ(reg);
2683 if (HAS_PCH_CPT(dev)) {
2684 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2685 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2686 } else {
2687 temp &= ~FDI_LINK_TRAIN_NONE;
2688 temp |= FDI_LINK_TRAIN_NONE;
2689 }
2690 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2691
2692 /* wait one idle pattern time */
2693 POSTING_READ(reg);
2694 udelay(1000);
357555c0
JB
2695
2696 /* IVB wants error correction enabled */
2697 if (IS_IVYBRIDGE(dev))
2698 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2699 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2700}
2701
1fbc0d78 2702static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2703{
1fbc0d78
DV
2704 return crtc->base.enabled && crtc->active &&
2705 crtc->config.has_pch_encoder;
1e833f40
DV
2706}
2707
01a415fd
DV
2708static void ivb_modeset_global_resources(struct drm_device *dev)
2709{
2710 struct drm_i915_private *dev_priv = dev->dev_private;
2711 struct intel_crtc *pipe_B_crtc =
2712 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2713 struct intel_crtc *pipe_C_crtc =
2714 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2715 uint32_t temp;
2716
1e833f40
DV
2717 /*
2718 * When everything is off disable fdi C so that we could enable fdi B
2719 * with all lanes. Note that we don't care about enabled pipes without
2720 * an enabled pch encoder.
2721 */
2722 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2723 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2724 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2725 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2726
2727 temp = I915_READ(SOUTH_CHICKEN1);
2728 temp &= ~FDI_BC_BIFURCATION_SELECT;
2729 DRM_DEBUG_KMS("disabling fdi C rx\n");
2730 I915_WRITE(SOUTH_CHICKEN1, temp);
2731 }
2732}
2733
8db9d77b
ZW
2734/* The FDI link training functions for ILK/Ibexpeak. */
2735static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2740 int pipe = intel_crtc->pipe;
5eddb70b 2741 u32 reg, temp, tries;
8db9d77b 2742
1c8562f6 2743 /* FDI needs bits from pipe first */
0fc932b8 2744 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 2745
e1a44743
AJ
2746 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2747 for train result */
5eddb70b
CW
2748 reg = FDI_RX_IMR(pipe);
2749 temp = I915_READ(reg);
e1a44743
AJ
2750 temp &= ~FDI_RX_SYMBOL_LOCK;
2751 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2752 I915_WRITE(reg, temp);
2753 I915_READ(reg);
e1a44743
AJ
2754 udelay(150);
2755
8db9d77b 2756 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2757 reg = FDI_TX_CTL(pipe);
2758 temp = I915_READ(reg);
627eb5a3
DV
2759 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2760 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2761 temp &= ~FDI_LINK_TRAIN_NONE;
2762 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2763 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2764
5eddb70b
CW
2765 reg = FDI_RX_CTL(pipe);
2766 temp = I915_READ(reg);
8db9d77b
ZW
2767 temp &= ~FDI_LINK_TRAIN_NONE;
2768 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2769 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2770
2771 POSTING_READ(reg);
8db9d77b
ZW
2772 udelay(150);
2773
5b2adf89 2774 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2775 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2776 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2777 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2778
5eddb70b 2779 reg = FDI_RX_IIR(pipe);
e1a44743 2780 for (tries = 0; tries < 5; tries++) {
5eddb70b 2781 temp = I915_READ(reg);
8db9d77b
ZW
2782 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2783
2784 if ((temp & FDI_RX_BIT_LOCK)) {
2785 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2786 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2787 break;
2788 }
8db9d77b 2789 }
e1a44743 2790 if (tries == 5)
5eddb70b 2791 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2792
2793 /* Train 2 */
5eddb70b
CW
2794 reg = FDI_TX_CTL(pipe);
2795 temp = I915_READ(reg);
8db9d77b
ZW
2796 temp &= ~FDI_LINK_TRAIN_NONE;
2797 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2798 I915_WRITE(reg, temp);
8db9d77b 2799
5eddb70b
CW
2800 reg = FDI_RX_CTL(pipe);
2801 temp = I915_READ(reg);
8db9d77b
ZW
2802 temp &= ~FDI_LINK_TRAIN_NONE;
2803 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2804 I915_WRITE(reg, temp);
8db9d77b 2805
5eddb70b
CW
2806 POSTING_READ(reg);
2807 udelay(150);
8db9d77b 2808
5eddb70b 2809 reg = FDI_RX_IIR(pipe);
e1a44743 2810 for (tries = 0; tries < 5; tries++) {
5eddb70b 2811 temp = I915_READ(reg);
8db9d77b
ZW
2812 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2813
2814 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2815 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2816 DRM_DEBUG_KMS("FDI train 2 done.\n");
2817 break;
2818 }
8db9d77b 2819 }
e1a44743 2820 if (tries == 5)
5eddb70b 2821 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2822
2823 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2824
8db9d77b
ZW
2825}
2826
0206e353 2827static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2828 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2829 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2830 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2831 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2832};
2833
2834/* The FDI link training functions for SNB/Cougarpoint. */
2835static void gen6_fdi_link_train(struct drm_crtc *crtc)
2836{
2837 struct drm_device *dev = crtc->dev;
2838 struct drm_i915_private *dev_priv = dev->dev_private;
2839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2840 int pipe = intel_crtc->pipe;
fa37d39e 2841 u32 reg, temp, i, retry;
8db9d77b 2842
e1a44743
AJ
2843 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2844 for train result */
5eddb70b
CW
2845 reg = FDI_RX_IMR(pipe);
2846 temp = I915_READ(reg);
e1a44743
AJ
2847 temp &= ~FDI_RX_SYMBOL_LOCK;
2848 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2849 I915_WRITE(reg, temp);
2850
2851 POSTING_READ(reg);
e1a44743
AJ
2852 udelay(150);
2853
8db9d77b 2854 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2855 reg = FDI_TX_CTL(pipe);
2856 temp = I915_READ(reg);
627eb5a3
DV
2857 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2858 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2859 temp &= ~FDI_LINK_TRAIN_NONE;
2860 temp |= FDI_LINK_TRAIN_PATTERN_1;
2861 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2862 /* SNB-B */
2863 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 2864 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2865
d74cf324
DV
2866 I915_WRITE(FDI_RX_MISC(pipe),
2867 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2868
5eddb70b
CW
2869 reg = FDI_RX_CTL(pipe);
2870 temp = I915_READ(reg);
8db9d77b
ZW
2871 if (HAS_PCH_CPT(dev)) {
2872 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2873 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2874 } else {
2875 temp &= ~FDI_LINK_TRAIN_NONE;
2876 temp |= FDI_LINK_TRAIN_PATTERN_1;
2877 }
5eddb70b
CW
2878 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2879
2880 POSTING_READ(reg);
8db9d77b
ZW
2881 udelay(150);
2882
0206e353 2883 for (i = 0; i < 4; i++) {
5eddb70b
CW
2884 reg = FDI_TX_CTL(pipe);
2885 temp = I915_READ(reg);
8db9d77b
ZW
2886 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2887 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2888 I915_WRITE(reg, temp);
2889
2890 POSTING_READ(reg);
8db9d77b
ZW
2891 udelay(500);
2892
fa37d39e
SP
2893 for (retry = 0; retry < 5; retry++) {
2894 reg = FDI_RX_IIR(pipe);
2895 temp = I915_READ(reg);
2896 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2897 if (temp & FDI_RX_BIT_LOCK) {
2898 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2899 DRM_DEBUG_KMS("FDI train 1 done.\n");
2900 break;
2901 }
2902 udelay(50);
8db9d77b 2903 }
fa37d39e
SP
2904 if (retry < 5)
2905 break;
8db9d77b
ZW
2906 }
2907 if (i == 4)
5eddb70b 2908 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2909
2910 /* Train 2 */
5eddb70b
CW
2911 reg = FDI_TX_CTL(pipe);
2912 temp = I915_READ(reg);
8db9d77b
ZW
2913 temp &= ~FDI_LINK_TRAIN_NONE;
2914 temp |= FDI_LINK_TRAIN_PATTERN_2;
2915 if (IS_GEN6(dev)) {
2916 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2917 /* SNB-B */
2918 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2919 }
5eddb70b 2920 I915_WRITE(reg, temp);
8db9d77b 2921
5eddb70b
CW
2922 reg = FDI_RX_CTL(pipe);
2923 temp = I915_READ(reg);
8db9d77b
ZW
2924 if (HAS_PCH_CPT(dev)) {
2925 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2926 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2927 } else {
2928 temp &= ~FDI_LINK_TRAIN_NONE;
2929 temp |= FDI_LINK_TRAIN_PATTERN_2;
2930 }
5eddb70b
CW
2931 I915_WRITE(reg, temp);
2932
2933 POSTING_READ(reg);
8db9d77b
ZW
2934 udelay(150);
2935
0206e353 2936 for (i = 0; i < 4; i++) {
5eddb70b
CW
2937 reg = FDI_TX_CTL(pipe);
2938 temp = I915_READ(reg);
8db9d77b
ZW
2939 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2940 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2941 I915_WRITE(reg, temp);
2942
2943 POSTING_READ(reg);
8db9d77b
ZW
2944 udelay(500);
2945
fa37d39e
SP
2946 for (retry = 0; retry < 5; retry++) {
2947 reg = FDI_RX_IIR(pipe);
2948 temp = I915_READ(reg);
2949 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2950 if (temp & FDI_RX_SYMBOL_LOCK) {
2951 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2952 DRM_DEBUG_KMS("FDI train 2 done.\n");
2953 break;
2954 }
2955 udelay(50);
8db9d77b 2956 }
fa37d39e
SP
2957 if (retry < 5)
2958 break;
8db9d77b
ZW
2959 }
2960 if (i == 4)
5eddb70b 2961 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2962
2963 DRM_DEBUG_KMS("FDI train done.\n");
2964}
2965
357555c0
JB
2966/* Manual link training for Ivy Bridge A0 parts */
2967static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2968{
2969 struct drm_device *dev = crtc->dev;
2970 struct drm_i915_private *dev_priv = dev->dev_private;
2971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2972 int pipe = intel_crtc->pipe;
139ccd3f 2973 u32 reg, temp, i, j;
357555c0
JB
2974
2975 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2976 for train result */
2977 reg = FDI_RX_IMR(pipe);
2978 temp = I915_READ(reg);
2979 temp &= ~FDI_RX_SYMBOL_LOCK;
2980 temp &= ~FDI_RX_BIT_LOCK;
2981 I915_WRITE(reg, temp);
2982
2983 POSTING_READ(reg);
2984 udelay(150);
2985
01a415fd
DV
2986 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2987 I915_READ(FDI_RX_IIR(pipe)));
2988
139ccd3f
JB
2989 /* Try each vswing and preemphasis setting twice before moving on */
2990 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2991 /* disable first in case we need to retry */
2992 reg = FDI_TX_CTL(pipe);
2993 temp = I915_READ(reg);
2994 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2995 temp &= ~FDI_TX_ENABLE;
2996 I915_WRITE(reg, temp);
357555c0 2997
139ccd3f
JB
2998 reg = FDI_RX_CTL(pipe);
2999 temp = I915_READ(reg);
3000 temp &= ~FDI_LINK_TRAIN_AUTO;
3001 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3002 temp &= ~FDI_RX_ENABLE;
3003 I915_WRITE(reg, temp);
357555c0 3004
139ccd3f 3005 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3006 reg = FDI_TX_CTL(pipe);
3007 temp = I915_READ(reg);
139ccd3f
JB
3008 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3009 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3010 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3011 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3012 temp |= snb_b_fdi_train_param[j/2];
3013 temp |= FDI_COMPOSITE_SYNC;
3014 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3015
139ccd3f
JB
3016 I915_WRITE(FDI_RX_MISC(pipe),
3017 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3018
139ccd3f 3019 reg = FDI_RX_CTL(pipe);
357555c0 3020 temp = I915_READ(reg);
139ccd3f
JB
3021 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3022 temp |= FDI_COMPOSITE_SYNC;
3023 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3024
139ccd3f
JB
3025 POSTING_READ(reg);
3026 udelay(1); /* should be 0.5us */
357555c0 3027
139ccd3f
JB
3028 for (i = 0; i < 4; i++) {
3029 reg = FDI_RX_IIR(pipe);
3030 temp = I915_READ(reg);
3031 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3032
139ccd3f
JB
3033 if (temp & FDI_RX_BIT_LOCK ||
3034 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3035 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3036 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3037 i);
3038 break;
3039 }
3040 udelay(1); /* should be 0.5us */
3041 }
3042 if (i == 4) {
3043 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3044 continue;
3045 }
357555c0 3046
139ccd3f 3047 /* Train 2 */
357555c0
JB
3048 reg = FDI_TX_CTL(pipe);
3049 temp = I915_READ(reg);
139ccd3f
JB
3050 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3051 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3052 I915_WRITE(reg, temp);
3053
3054 reg = FDI_RX_CTL(pipe);
3055 temp = I915_READ(reg);
3056 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3057 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3058 I915_WRITE(reg, temp);
3059
3060 POSTING_READ(reg);
139ccd3f 3061 udelay(2); /* should be 1.5us */
357555c0 3062
139ccd3f
JB
3063 for (i = 0; i < 4; i++) {
3064 reg = FDI_RX_IIR(pipe);
3065 temp = I915_READ(reg);
3066 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3067
139ccd3f
JB
3068 if (temp & FDI_RX_SYMBOL_LOCK ||
3069 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3070 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3071 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3072 i);
3073 goto train_done;
3074 }
3075 udelay(2); /* should be 1.5us */
357555c0 3076 }
139ccd3f
JB
3077 if (i == 4)
3078 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3079 }
357555c0 3080
139ccd3f 3081train_done:
357555c0
JB
3082 DRM_DEBUG_KMS("FDI train done.\n");
3083}
3084
88cefb6c 3085static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3086{
88cefb6c 3087 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3088 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3089 int pipe = intel_crtc->pipe;
5eddb70b 3090 u32 reg, temp;
79e53945 3091
c64e311e 3092
c98e9dcf 3093 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3094 reg = FDI_RX_CTL(pipe);
3095 temp = I915_READ(reg);
627eb5a3
DV
3096 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3097 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 3098 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3099 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3100
3101 POSTING_READ(reg);
c98e9dcf
JB
3102 udelay(200);
3103
3104 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3105 temp = I915_READ(reg);
3106 I915_WRITE(reg, temp | FDI_PCDCLK);
3107
3108 POSTING_READ(reg);
c98e9dcf
JB
3109 udelay(200);
3110
20749730
PZ
3111 /* Enable CPU FDI TX PLL, always on for Ironlake */
3112 reg = FDI_TX_CTL(pipe);
3113 temp = I915_READ(reg);
3114 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3115 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3116
20749730
PZ
3117 POSTING_READ(reg);
3118 udelay(100);
6be4a607 3119 }
0e23b99d
JB
3120}
3121
88cefb6c
DV
3122static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3123{
3124 struct drm_device *dev = intel_crtc->base.dev;
3125 struct drm_i915_private *dev_priv = dev->dev_private;
3126 int pipe = intel_crtc->pipe;
3127 u32 reg, temp;
3128
3129 /* Switch from PCDclk to Rawclk */
3130 reg = FDI_RX_CTL(pipe);
3131 temp = I915_READ(reg);
3132 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3133
3134 /* Disable CPU FDI TX PLL */
3135 reg = FDI_TX_CTL(pipe);
3136 temp = I915_READ(reg);
3137 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3138
3139 POSTING_READ(reg);
3140 udelay(100);
3141
3142 reg = FDI_RX_CTL(pipe);
3143 temp = I915_READ(reg);
3144 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3145
3146 /* Wait for the clocks to turn off. */
3147 POSTING_READ(reg);
3148 udelay(100);
3149}
3150
0fc932b8
JB
3151static void ironlake_fdi_disable(struct drm_crtc *crtc)
3152{
3153 struct drm_device *dev = crtc->dev;
3154 struct drm_i915_private *dev_priv = dev->dev_private;
3155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3156 int pipe = intel_crtc->pipe;
3157 u32 reg, temp;
3158
3159 /* disable CPU FDI tx and PCH FDI rx */
3160 reg = FDI_TX_CTL(pipe);
3161 temp = I915_READ(reg);
3162 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3163 POSTING_READ(reg);
3164
3165 reg = FDI_RX_CTL(pipe);
3166 temp = I915_READ(reg);
3167 temp &= ~(0x7 << 16);
dfd07d72 3168 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3169 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3170
3171 POSTING_READ(reg);
3172 udelay(100);
3173
3174 /* Ironlake workaround, disable clock pointer after downing FDI */
6f06ce18
JB
3175 if (HAS_PCH_IBX(dev)) {
3176 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
6f06ce18 3177 }
0fc932b8
JB
3178
3179 /* still set train pattern 1 */
3180 reg = FDI_TX_CTL(pipe);
3181 temp = I915_READ(reg);
3182 temp &= ~FDI_LINK_TRAIN_NONE;
3183 temp |= FDI_LINK_TRAIN_PATTERN_1;
3184 I915_WRITE(reg, temp);
3185
3186 reg = FDI_RX_CTL(pipe);
3187 temp = I915_READ(reg);
3188 if (HAS_PCH_CPT(dev)) {
3189 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3190 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3191 } else {
3192 temp &= ~FDI_LINK_TRAIN_NONE;
3193 temp |= FDI_LINK_TRAIN_PATTERN_1;
3194 }
3195 /* BPC in FDI rx is consistent with that in PIPECONF */
3196 temp &= ~(0x07 << 16);
dfd07d72 3197 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3198 I915_WRITE(reg, temp);
3199
3200 POSTING_READ(reg);
3201 udelay(100);
3202}
3203
5dce5b93
CW
3204bool intel_has_pending_fb_unpin(struct drm_device *dev)
3205{
3206 struct intel_crtc *crtc;
3207
3208 /* Note that we don't need to be called with mode_config.lock here
3209 * as our list of CRTC objects is static for the lifetime of the
3210 * device and so cannot disappear as we iterate. Similarly, we can
3211 * happily treat the predicates as racy, atomic checks as userspace
3212 * cannot claim and pin a new fb without at least acquring the
3213 * struct_mutex and so serialising with us.
3214 */
3215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3216 if (atomic_read(&crtc->unpin_work_count) == 0)
3217 continue;
3218
3219 if (crtc->unpin_work)
3220 intel_wait_for_vblank(dev, crtc->pipe);
3221
3222 return true;
3223 }
3224
3225 return false;
3226}
3227
e6c3a2a6
CW
3228static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3229{
0f91128d 3230 struct drm_device *dev = crtc->dev;
5bb61643 3231 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3232
f4510a27 3233 if (crtc->primary->fb == NULL)
e6c3a2a6
CW
3234 return;
3235
2c10d571
DV
3236 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3237
5bb61643
CW
3238 wait_event(dev_priv->pending_flip_queue,
3239 !intel_crtc_has_pending_flip(crtc));
3240
0f91128d 3241 mutex_lock(&dev->struct_mutex);
f4510a27 3242 intel_finish_fb(crtc->primary->fb);
0f91128d 3243 mutex_unlock(&dev->struct_mutex);
e6c3a2a6
CW
3244}
3245
e615efe4
ED
3246/* Program iCLKIP clock to the desired frequency */
3247static void lpt_program_iclkip(struct drm_crtc *crtc)
3248{
3249 struct drm_device *dev = crtc->dev;
3250 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3251 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3252 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3253 u32 temp;
3254
09153000
DV
3255 mutex_lock(&dev_priv->dpio_lock);
3256
e615efe4
ED
3257 /* It is necessary to ungate the pixclk gate prior to programming
3258 * the divisors, and gate it back when it is done.
3259 */
3260 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3261
3262 /* Disable SSCCTL */
3263 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3264 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3265 SBI_SSCCTL_DISABLE,
3266 SBI_ICLK);
e615efe4
ED
3267
3268 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3269 if (clock == 20000) {
e615efe4
ED
3270 auxdiv = 1;
3271 divsel = 0x41;
3272 phaseinc = 0x20;
3273 } else {
3274 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3275 * but the adjusted_mode->crtc_clock in in KHz. To get the
3276 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3277 * convert the virtual clock precision to KHz here for higher
3278 * precision.
3279 */
3280 u32 iclk_virtual_root_freq = 172800 * 1000;
3281 u32 iclk_pi_range = 64;
3282 u32 desired_divisor, msb_divisor_value, pi_value;
3283
12d7ceed 3284 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3285 msb_divisor_value = desired_divisor / iclk_pi_range;
3286 pi_value = desired_divisor % iclk_pi_range;
3287
3288 auxdiv = 0;
3289 divsel = msb_divisor_value - 2;
3290 phaseinc = pi_value;
3291 }
3292
3293 /* This should not happen with any sane values */
3294 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3295 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3296 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3297 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3298
3299 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3300 clock,
e615efe4
ED
3301 auxdiv,
3302 divsel,
3303 phasedir,
3304 phaseinc);
3305
3306 /* Program SSCDIVINTPHASE6 */
988d6ee8 3307 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3308 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3309 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3310 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3311 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3312 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3313 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3314 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3315
3316 /* Program SSCAUXDIV */
988d6ee8 3317 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3318 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3319 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3320 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3321
3322 /* Enable modulator and associated divider */
988d6ee8 3323 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3324 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3325 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3326
3327 /* Wait for initialization time */
3328 udelay(24);
3329
3330 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3331
3332 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3333}
3334
275f01b2
DV
3335static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3336 enum pipe pch_transcoder)
3337{
3338 struct drm_device *dev = crtc->base.dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3341
3342 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3343 I915_READ(HTOTAL(cpu_transcoder)));
3344 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3345 I915_READ(HBLANK(cpu_transcoder)));
3346 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3347 I915_READ(HSYNC(cpu_transcoder)));
3348
3349 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3350 I915_READ(VTOTAL(cpu_transcoder)));
3351 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3352 I915_READ(VBLANK(cpu_transcoder)));
3353 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3354 I915_READ(VSYNC(cpu_transcoder)));
3355 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3356 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3357}
3358
1fbc0d78
DV
3359static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3360{
3361 struct drm_i915_private *dev_priv = dev->dev_private;
3362 uint32_t temp;
3363
3364 temp = I915_READ(SOUTH_CHICKEN1);
3365 if (temp & FDI_BC_BIFURCATION_SELECT)
3366 return;
3367
3368 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3369 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3370
3371 temp |= FDI_BC_BIFURCATION_SELECT;
3372 DRM_DEBUG_KMS("enabling fdi C rx\n");
3373 I915_WRITE(SOUTH_CHICKEN1, temp);
3374 POSTING_READ(SOUTH_CHICKEN1);
3375}
3376
3377static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3378{
3379 struct drm_device *dev = intel_crtc->base.dev;
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381
3382 switch (intel_crtc->pipe) {
3383 case PIPE_A:
3384 break;
3385 case PIPE_B:
3386 if (intel_crtc->config.fdi_lanes > 2)
3387 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3388 else
3389 cpt_enable_fdi_bc_bifurcation(dev);
3390
3391 break;
3392 case PIPE_C:
3393 cpt_enable_fdi_bc_bifurcation(dev);
3394
3395 break;
3396 default:
3397 BUG();
3398 }
3399}
3400
f67a559d
JB
3401/*
3402 * Enable PCH resources required for PCH ports:
3403 * - PCH PLLs
3404 * - FDI training & RX/TX
3405 * - update transcoder timings
3406 * - DP transcoding bits
3407 * - transcoder
3408 */
3409static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3410{
3411 struct drm_device *dev = crtc->dev;
3412 struct drm_i915_private *dev_priv = dev->dev_private;
3413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3414 int pipe = intel_crtc->pipe;
ee7b9f93 3415 u32 reg, temp;
2c07245f 3416
ab9412ba 3417 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3418
1fbc0d78
DV
3419 if (IS_IVYBRIDGE(dev))
3420 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3421
cd986abb
DV
3422 /* Write the TU size bits before fdi link training, so that error
3423 * detection works. */
3424 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3425 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3426
c98e9dcf 3427 /* For PCH output, training FDI link */
674cf967 3428 dev_priv->display.fdi_link_train(crtc);
2c07245f 3429
3ad8a208
DV
3430 /* We need to program the right clock selection before writing the pixel
3431 * mutliplier into the DPLL. */
303b81e0 3432 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3433 u32 sel;
4b645f14 3434
c98e9dcf 3435 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3436 temp |= TRANS_DPLL_ENABLE(pipe);
3437 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3438 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3439 temp |= sel;
3440 else
3441 temp &= ~sel;
c98e9dcf 3442 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3443 }
5eddb70b 3444
3ad8a208
DV
3445 /* XXX: pch pll's can be enabled any time before we enable the PCH
3446 * transcoder, and we actually should do this to not upset any PCH
3447 * transcoder that already use the clock when we share it.
3448 *
3449 * Note that enable_shared_dpll tries to do the right thing, but
3450 * get_shared_dpll unconditionally resets the pll - we need that to have
3451 * the right LVDS enable sequence. */
3452 ironlake_enable_shared_dpll(intel_crtc);
3453
d9b6cb56
JB
3454 /* set transcoder timing, panel must allow it */
3455 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3456 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3457
303b81e0 3458 intel_fdi_normal_train(crtc);
5e84e1a4 3459
c98e9dcf
JB
3460 /* For PCH DP, enable TRANS_DP_CTL */
3461 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3462 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3463 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3464 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3465 reg = TRANS_DP_CTL(pipe);
3466 temp = I915_READ(reg);
3467 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3468 TRANS_DP_SYNC_MASK |
3469 TRANS_DP_BPC_MASK);
5eddb70b
CW
3470 temp |= (TRANS_DP_OUTPUT_ENABLE |
3471 TRANS_DP_ENH_FRAMING);
9325c9f0 3472 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3473
3474 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3475 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3476 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3477 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3478
3479 switch (intel_trans_dp_port_sel(crtc)) {
3480 case PCH_DP_B:
5eddb70b 3481 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3482 break;
3483 case PCH_DP_C:
5eddb70b 3484 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3485 break;
3486 case PCH_DP_D:
5eddb70b 3487 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3488 break;
3489 default:
e95d41e1 3490 BUG();
32f9d658 3491 }
2c07245f 3492
5eddb70b 3493 I915_WRITE(reg, temp);
6be4a607 3494 }
b52eb4dc 3495
b8a4f404 3496 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3497}
3498
1507e5bd
PZ
3499static void lpt_pch_enable(struct drm_crtc *crtc)
3500{
3501 struct drm_device *dev = crtc->dev;
3502 struct drm_i915_private *dev_priv = dev->dev_private;
3503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3504 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3505
ab9412ba 3506 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3507
8c52b5e8 3508 lpt_program_iclkip(crtc);
1507e5bd 3509
0540e488 3510 /* Set transcoder timing. */
275f01b2 3511 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3512
937bb610 3513 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3514}
3515
e2b78267 3516static void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3517{
e2b78267 3518 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3519
3520 if (pll == NULL)
3521 return;
3522
3523 if (pll->refcount == 0) {
46edb027 3524 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3525 return;
3526 }
3527
f4a091c7
DV
3528 if (--pll->refcount == 0) {
3529 WARN_ON(pll->on);
3530 WARN_ON(pll->active);
3531 }
3532
a43f6e0f 3533 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3534}
3535
b89a1d39 3536static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3537{
e2b78267
DV
3538 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3539 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3540 enum intel_dpll_id i;
ee7b9f93 3541
ee7b9f93 3542 if (pll) {
46edb027
DV
3543 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3544 crtc->base.base.id, pll->name);
e2b78267 3545 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3546 }
3547
98b6bd99
DV
3548 if (HAS_PCH_IBX(dev_priv->dev)) {
3549 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3550 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3551 pll = &dev_priv->shared_dplls[i];
98b6bd99 3552
46edb027
DV
3553 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3554 crtc->base.base.id, pll->name);
98b6bd99
DV
3555
3556 goto found;
3557 }
3558
e72f9fbf
DV
3559 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3560 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3561
3562 /* Only want to check enabled timings first */
3563 if (pll->refcount == 0)
3564 continue;
3565
b89a1d39
DV
3566 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3567 sizeof(pll->hw_state)) == 0) {
46edb027 3568 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3569 crtc->base.base.id,
46edb027 3570 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3571
3572 goto found;
3573 }
3574 }
3575
3576 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3577 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3578 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3579 if (pll->refcount == 0) {
46edb027
DV
3580 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3581 crtc->base.base.id, pll->name);
ee7b9f93
JB
3582 goto found;
3583 }
3584 }
3585
3586 return NULL;
3587
3588found:
a43f6e0f 3589 crtc->config.shared_dpll = i;
46edb027
DV
3590 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3591 pipe_name(crtc->pipe));
ee7b9f93 3592
cdbd2316 3593 if (pll->active == 0) {
66e985c0
DV
3594 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3595 sizeof(pll->hw_state));
3596
46edb027 3597 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
cdbd2316 3598 WARN_ON(pll->on);
e9d6944e 3599 assert_shared_dpll_disabled(dev_priv, pll);
ee7b9f93 3600
15bdd4cf 3601 pll->mode_set(dev_priv, pll);
cdbd2316
DV
3602 }
3603 pll->refcount++;
e04c7350 3604
ee7b9f93
JB
3605 return pll;
3606}
3607
a1520318 3608static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3609{
3610 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3611 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3612 u32 temp;
3613
3614 temp = I915_READ(dslreg);
3615 udelay(500);
3616 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3617 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3618 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3619 }
3620}
3621
b074cec8
JB
3622static void ironlake_pfit_enable(struct intel_crtc *crtc)
3623{
3624 struct drm_device *dev = crtc->base.dev;
3625 struct drm_i915_private *dev_priv = dev->dev_private;
3626 int pipe = crtc->pipe;
3627
fd4daa9c 3628 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3629 /* Force use of hard-coded filter coefficients
3630 * as some pre-programmed values are broken,
3631 * e.g. x201.
3632 */
3633 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3634 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3635 PF_PIPE_SEL_IVB(pipe));
3636 else
3637 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3638 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3639 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3640 }
3641}
3642
bb53d4ae
VS
3643static void intel_enable_planes(struct drm_crtc *crtc)
3644{
3645 struct drm_device *dev = crtc->dev;
3646 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3647 struct drm_plane *plane;
bb53d4ae
VS
3648 struct intel_plane *intel_plane;
3649
af2b653b
MR
3650 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3651 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3652 if (intel_plane->pipe == pipe)
3653 intel_plane_restore(&intel_plane->base);
af2b653b 3654 }
bb53d4ae
VS
3655}
3656
3657static void intel_disable_planes(struct drm_crtc *crtc)
3658{
3659 struct drm_device *dev = crtc->dev;
3660 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 3661 struct drm_plane *plane;
bb53d4ae
VS
3662 struct intel_plane *intel_plane;
3663
af2b653b
MR
3664 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3665 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
3666 if (intel_plane->pipe == pipe)
3667 intel_plane_disable(&intel_plane->base);
af2b653b 3668 }
bb53d4ae
VS
3669}
3670
20bc8673 3671void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3672{
3673 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3674
3675 if (!crtc->config.ips_enabled)
3676 return;
3677
3678 /* We can only enable IPS after we enable a plane and wait for a vblank.
3679 * We guarantee that the plane is enabled by calling intel_enable_ips
3680 * only after intel_enable_plane. And intel_enable_plane already waits
3681 * for a vblank, so all we need to do here is to enable the IPS bit. */
3682 assert_plane_enabled(dev_priv, crtc->plane);
2a114cc1
BW
3683 if (IS_BROADWELL(crtc->base.dev)) {
3684 mutex_lock(&dev_priv->rps.hw_lock);
3685 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3686 mutex_unlock(&dev_priv->rps.hw_lock);
3687 /* Quoting Art Runyan: "its not safe to expect any particular
3688 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3689 * mailbox." Moreover, the mailbox may return a bogus state,
3690 * so we need to just enable it and continue on.
2a114cc1
BW
3691 */
3692 } else {
3693 I915_WRITE(IPS_CTL, IPS_ENABLE);
3694 /* The bit only becomes 1 in the next vblank, so this wait here
3695 * is essentially intel_wait_for_vblank. If we don't have this
3696 * and don't wait for vblanks until the end of crtc_enable, then
3697 * the HW state readout code will complain that the expected
3698 * IPS_CTL value is not the one we read. */
3699 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3700 DRM_ERROR("Timed out waiting for IPS enable\n");
3701 }
d77e4531
PZ
3702}
3703
20bc8673 3704void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3705{
3706 struct drm_device *dev = crtc->base.dev;
3707 struct drm_i915_private *dev_priv = dev->dev_private;
3708
3709 if (!crtc->config.ips_enabled)
3710 return;
3711
3712 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 3713 if (IS_BROADWELL(dev)) {
2a114cc1
BW
3714 mutex_lock(&dev_priv->rps.hw_lock);
3715 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3716 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
3717 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3718 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3719 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 3720 } else {
2a114cc1 3721 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3722 POSTING_READ(IPS_CTL);
3723 }
d77e4531
PZ
3724
3725 /* We need to wait for a vblank before we can disable the plane. */
3726 intel_wait_for_vblank(dev, crtc->pipe);
3727}
3728
3729/** Loads the palette/gamma unit for the CRTC with the prepared values */
3730static void intel_crtc_load_lut(struct drm_crtc *crtc)
3731{
3732 struct drm_device *dev = crtc->dev;
3733 struct drm_i915_private *dev_priv = dev->dev_private;
3734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3735 enum pipe pipe = intel_crtc->pipe;
3736 int palreg = PALETTE(pipe);
3737 int i;
3738 bool reenable_ips = false;
3739
3740 /* The clocks have to be on to load the palette. */
3741 if (!crtc->enabled || !intel_crtc->active)
3742 return;
3743
3744 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3745 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3746 assert_dsi_pll_enabled(dev_priv);
3747 else
3748 assert_pll_enabled(dev_priv, pipe);
3749 }
3750
3751 /* use legacy palette for Ironlake */
3752 if (HAS_PCH_SPLIT(dev))
3753 palreg = LGC_PALETTE(pipe);
3754
3755 /* Workaround : Do not read or write the pipe palette/gamma data while
3756 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3757 */
41e6fc4c 3758 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3759 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3760 GAMMA_MODE_MODE_SPLIT)) {
3761 hsw_disable_ips(intel_crtc);
3762 reenable_ips = true;
3763 }
3764
3765 for (i = 0; i < 256; i++) {
3766 I915_WRITE(palreg + 4 * i,
3767 (intel_crtc->lut_r[i] << 16) |
3768 (intel_crtc->lut_g[i] << 8) |
3769 intel_crtc->lut_b[i]);
3770 }
3771
3772 if (reenable_ips)
3773 hsw_enable_ips(intel_crtc);
3774}
3775
d3eedb1a
VS
3776static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3777{
3778 if (!enable && intel_crtc->overlay) {
3779 struct drm_device *dev = intel_crtc->base.dev;
3780 struct drm_i915_private *dev_priv = dev->dev_private;
3781
3782 mutex_lock(&dev->struct_mutex);
3783 dev_priv->mm.interruptible = false;
3784 (void) intel_overlay_switch_off(intel_crtc->overlay);
3785 dev_priv->mm.interruptible = true;
3786 mutex_unlock(&dev->struct_mutex);
3787 }
3788
3789 /* Let userspace switch the overlay on again. In most cases userspace
3790 * has to recompute where to put it anyway.
3791 */
3792}
3793
3794/**
3795 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3796 * cursor plane briefly if not already running after enabling the display
3797 * plane.
3798 * This workaround avoids occasional blank screens when self refresh is
3799 * enabled.
3800 */
3801static void
3802g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3803{
3804 u32 cntl = I915_READ(CURCNTR(pipe));
3805
3806 if ((cntl & CURSOR_MODE) == 0) {
3807 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3808
3809 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3810 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3811 intel_wait_for_vblank(dev_priv->dev, pipe);
3812 I915_WRITE(CURCNTR(pipe), cntl);
3813 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3814 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3815 }
3816}
3817
3818static void intel_crtc_enable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3819{
3820 struct drm_device *dev = crtc->dev;
3821 struct drm_i915_private *dev_priv = dev->dev_private;
3822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3823 int pipe = intel_crtc->pipe;
3824 int plane = intel_crtc->plane;
3825
3826 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3827 intel_enable_planes(crtc);
d3eedb1a
VS
3828 /* The fixup needs to happen before cursor is enabled */
3829 if (IS_G4X(dev))
3830 g4x_fixup_plane(dev_priv, pipe);
a5c4d7bc 3831 intel_crtc_update_cursor(crtc, true);
d3eedb1a 3832 intel_crtc_dpms_overlay(intel_crtc, true);
a5c4d7bc
VS
3833
3834 hsw_enable_ips(intel_crtc);
3835
3836 mutex_lock(&dev->struct_mutex);
3837 intel_update_fbc(dev);
3838 mutex_unlock(&dev->struct_mutex);
3839}
3840
d3eedb1a 3841static void intel_crtc_disable_planes(struct drm_crtc *crtc)
a5c4d7bc
VS
3842{
3843 struct drm_device *dev = crtc->dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846 int pipe = intel_crtc->pipe;
3847 int plane = intel_crtc->plane;
3848
3849 intel_crtc_wait_for_pending_flips(crtc);
3850 drm_vblank_off(dev, pipe);
3851
3852 if (dev_priv->fbc.plane == plane)
3853 intel_disable_fbc(dev);
3854
3855 hsw_disable_ips(intel_crtc);
3856
d3eedb1a 3857 intel_crtc_dpms_overlay(intel_crtc, false);
a5c4d7bc
VS
3858 intel_crtc_update_cursor(crtc, false);
3859 intel_disable_planes(crtc);
3860 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
3861}
3862
f67a559d
JB
3863static void ironlake_crtc_enable(struct drm_crtc *crtc)
3864{
3865 struct drm_device *dev = crtc->dev;
3866 struct drm_i915_private *dev_priv = dev->dev_private;
3867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3868 struct intel_encoder *encoder;
f67a559d 3869 int pipe = intel_crtc->pipe;
f67a559d 3870
08a48469
DV
3871 WARN_ON(!crtc->enabled);
3872
f67a559d
JB
3873 if (intel_crtc->active)
3874 return;
3875
3876 intel_crtc->active = true;
8664281b
PZ
3877
3878 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3879 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3880
f6736a1a 3881 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
3882 if (encoder->pre_enable)
3883 encoder->pre_enable(encoder);
f67a559d 3884
5bfe2ac0 3885 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
3886 /* Note: FDI PLL enabling _must_ be done before we enable the
3887 * cpu pipes, hence this is separate from all the other fdi/pch
3888 * enabling. */
88cefb6c 3889 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
3890 } else {
3891 assert_fdi_tx_disabled(dev_priv, pipe);
3892 assert_fdi_rx_disabled(dev_priv, pipe);
3893 }
f67a559d 3894
b074cec8 3895 ironlake_pfit_enable(intel_crtc);
f67a559d 3896
9c54c0dd
JB
3897 /*
3898 * On ILK+ LUT must be loaded before the pipe is running but with
3899 * clocks enabled
3900 */
3901 intel_crtc_load_lut(crtc);
3902
f37fcc2a 3903 intel_update_watermarks(crtc);
e1fdc473 3904 intel_enable_pipe(intel_crtc);
f67a559d 3905
5bfe2ac0 3906 if (intel_crtc->config.has_pch_encoder)
f67a559d 3907 ironlake_pch_enable(crtc);
c98e9dcf 3908
fa5c73b1
DV
3909 for_each_encoder_on_crtc(dev, crtc, encoder)
3910 encoder->enable(encoder);
61b77ddd
DV
3911
3912 if (HAS_PCH_CPT(dev))
a1520318 3913 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100 3914
d3eedb1a 3915 intel_crtc_enable_planes(crtc);
a5c4d7bc 3916
6ce94100
DV
3917 /*
3918 * There seems to be a race in PCH platform hw (at least on some
3919 * outputs) where an enabled pipe still completes any pageflip right
3920 * away (as if the pipe is off) instead of waiting for vblank. As soon
3921 * as the first vblank happend, everything works as expected. Hence just
3922 * wait for one vblank before returning to avoid strange things
3923 * happening.
3924 */
3925 intel_wait_for_vblank(dev, intel_crtc->pipe);
6be4a607
JB
3926}
3927
42db64ef
PZ
3928/* IPS only exists on ULT machines and is tied to pipe A. */
3929static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3930{
f5adf94e 3931 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
3932}
3933
e4916946
PZ
3934/*
3935 * This implements the workaround described in the "notes" section of the mode
3936 * set sequence documentation. When going from no pipes or single pipe to
3937 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3938 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3939 */
3940static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3941{
3942 struct drm_device *dev = crtc->base.dev;
3943 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3944
3945 /* We want to get the other_active_crtc only if there's only 1 other
3946 * active crtc. */
3947 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3948 if (!crtc_it->active || crtc_it == crtc)
3949 continue;
3950
3951 if (other_active_crtc)
3952 return;
3953
3954 other_active_crtc = crtc_it;
3955 }
3956 if (!other_active_crtc)
3957 return;
3958
3959 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3960 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3961}
3962
4f771f10
PZ
3963static void haswell_crtc_enable(struct drm_crtc *crtc)
3964{
3965 struct drm_device *dev = crtc->dev;
3966 struct drm_i915_private *dev_priv = dev->dev_private;
3967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3968 struct intel_encoder *encoder;
3969 int pipe = intel_crtc->pipe;
4f771f10
PZ
3970
3971 WARN_ON(!crtc->enabled);
3972
3973 if (intel_crtc->active)
3974 return;
3975
3976 intel_crtc->active = true;
8664281b
PZ
3977
3978 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3979 if (intel_crtc->config.has_pch_encoder)
3980 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3981
5bfe2ac0 3982 if (intel_crtc->config.has_pch_encoder)
04945641 3983 dev_priv->display.fdi_link_train(crtc);
4f771f10
PZ
3984
3985 for_each_encoder_on_crtc(dev, crtc, encoder)
3986 if (encoder->pre_enable)
3987 encoder->pre_enable(encoder);
3988
1f544388 3989 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 3990
b074cec8 3991 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
3992
3993 /*
3994 * On ILK+ LUT must be loaded before the pipe is running but with
3995 * clocks enabled
3996 */
3997 intel_crtc_load_lut(crtc);
3998
1f544388 3999 intel_ddi_set_pipe_settings(crtc);
8228c251 4000 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4001
f37fcc2a 4002 intel_update_watermarks(crtc);
e1fdc473 4003 intel_enable_pipe(intel_crtc);
42db64ef 4004
5bfe2ac0 4005 if (intel_crtc->config.has_pch_encoder)
1507e5bd 4006 lpt_pch_enable(crtc);
4f771f10 4007
8807e55b 4008 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4009 encoder->enable(encoder);
8807e55b
JN
4010 intel_opregion_notify_encoder(encoder, true);
4011 }
4f771f10 4012
e4916946
PZ
4013 /* If we change the relative order between pipe/planes enabling, we need
4014 * to change the workaround. */
4015 haswell_mode_set_planes_workaround(intel_crtc);
d3eedb1a 4016 intel_crtc_enable_planes(crtc);
4f771f10
PZ
4017}
4018
3f8dce3a
DV
4019static void ironlake_pfit_disable(struct intel_crtc *crtc)
4020{
4021 struct drm_device *dev = crtc->base.dev;
4022 struct drm_i915_private *dev_priv = dev->dev_private;
4023 int pipe = crtc->pipe;
4024
4025 /* To avoid upsetting the power well on haswell only disable the pfit if
4026 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 4027 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
4028 I915_WRITE(PF_CTL(pipe), 0);
4029 I915_WRITE(PF_WIN_POS(pipe), 0);
4030 I915_WRITE(PF_WIN_SZ(pipe), 0);
4031 }
4032}
4033
6be4a607
JB
4034static void ironlake_crtc_disable(struct drm_crtc *crtc)
4035{
4036 struct drm_device *dev = crtc->dev;
4037 struct drm_i915_private *dev_priv = dev->dev_private;
4038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4039 struct intel_encoder *encoder;
6be4a607 4040 int pipe = intel_crtc->pipe;
5eddb70b 4041 u32 reg, temp;
b52eb4dc 4042
f7abfe8b
CW
4043 if (!intel_crtc->active)
4044 return;
4045
d3eedb1a 4046 intel_crtc_disable_planes(crtc);
a5c4d7bc 4047
ea9d758d
DV
4048 for_each_encoder_on_crtc(dev, crtc, encoder)
4049 encoder->disable(encoder);
4050
d925c59a
DV
4051 if (intel_crtc->config.has_pch_encoder)
4052 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4053
b24e7179 4054 intel_disable_pipe(dev_priv, pipe);
32f9d658 4055
3f8dce3a 4056 ironlake_pfit_disable(intel_crtc);
2c07245f 4057
bf49ec8c
DV
4058 for_each_encoder_on_crtc(dev, crtc, encoder)
4059 if (encoder->post_disable)
4060 encoder->post_disable(encoder);
2c07245f 4061
d925c59a
DV
4062 if (intel_crtc->config.has_pch_encoder) {
4063 ironlake_fdi_disable(crtc);
913d8d11 4064
d925c59a
DV
4065 ironlake_disable_pch_transcoder(dev_priv, pipe);
4066 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 4067
d925c59a
DV
4068 if (HAS_PCH_CPT(dev)) {
4069 /* disable TRANS_DP_CTL */
4070 reg = TRANS_DP_CTL(pipe);
4071 temp = I915_READ(reg);
4072 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4073 TRANS_DP_PORT_SEL_MASK);
4074 temp |= TRANS_DP_PORT_SEL_NONE;
4075 I915_WRITE(reg, temp);
4076
4077 /* disable DPLL_SEL */
4078 temp = I915_READ(PCH_DPLL_SEL);
11887397 4079 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 4080 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 4081 }
e3421a18 4082
d925c59a 4083 /* disable PCH DPLL */
e72f9fbf 4084 intel_disable_shared_dpll(intel_crtc);
8db9d77b 4085
d925c59a
DV
4086 ironlake_fdi_pll_disable(intel_crtc);
4087 }
6b383a7f 4088
f7abfe8b 4089 intel_crtc->active = false;
46ba614c 4090 intel_update_watermarks(crtc);
d1ebd816
BW
4091
4092 mutex_lock(&dev->struct_mutex);
6b383a7f 4093 intel_update_fbc(dev);
d1ebd816 4094 mutex_unlock(&dev->struct_mutex);
6be4a607 4095}
1b3c7a47 4096
4f771f10 4097static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 4098{
4f771f10
PZ
4099 struct drm_device *dev = crtc->dev;
4100 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 4101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
4102 struct intel_encoder *encoder;
4103 int pipe = intel_crtc->pipe;
3b117c8f 4104 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 4105
4f771f10
PZ
4106 if (!intel_crtc->active)
4107 return;
4108
d3eedb1a 4109 intel_crtc_disable_planes(crtc);
dda9a66a 4110
8807e55b
JN
4111 for_each_encoder_on_crtc(dev, crtc, encoder) {
4112 intel_opregion_notify_encoder(encoder, false);
4f771f10 4113 encoder->disable(encoder);
8807e55b 4114 }
4f771f10 4115
8664281b
PZ
4116 if (intel_crtc->config.has_pch_encoder)
4117 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
4118 intel_disable_pipe(dev_priv, pipe);
4119
ad80a810 4120 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 4121
3f8dce3a 4122 ironlake_pfit_disable(intel_crtc);
4f771f10 4123
1f544388 4124 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10
PZ
4125
4126 for_each_encoder_on_crtc(dev, crtc, encoder)
4127 if (encoder->post_disable)
4128 encoder->post_disable(encoder);
4129
88adfff1 4130 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 4131 lpt_disable_pch_transcoder(dev_priv);
8664281b 4132 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 4133 intel_ddi_fdi_disable(crtc);
83616634 4134 }
4f771f10
PZ
4135
4136 intel_crtc->active = false;
46ba614c 4137 intel_update_watermarks(crtc);
4f771f10
PZ
4138
4139 mutex_lock(&dev->struct_mutex);
4140 intel_update_fbc(dev);
4141 mutex_unlock(&dev->struct_mutex);
4142}
4143
ee7b9f93
JB
4144static void ironlake_crtc_off(struct drm_crtc *crtc)
4145{
4146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 4147 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
4148}
4149
6441ab5f
PZ
4150static void haswell_crtc_off(struct drm_crtc *crtc)
4151{
4152 intel_ddi_put_crtc_pll(crtc);
4153}
4154
2dd24552
JB
4155static void i9xx_pfit_enable(struct intel_crtc *crtc)
4156{
4157 struct drm_device *dev = crtc->base.dev;
4158 struct drm_i915_private *dev_priv = dev->dev_private;
4159 struct intel_crtc_config *pipe_config = &crtc->config;
4160
328d8e82 4161 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4162 return;
4163
2dd24552 4164 /*
c0b03411
DV
4165 * The panel fitter should only be adjusted whilst the pipe is disabled,
4166 * according to register description and PRM.
2dd24552 4167 */
c0b03411
DV
4168 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4169 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4170
b074cec8
JB
4171 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4172 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4173
4174 /* Border color in case we don't scale up to the full screen. Black by
4175 * default, change to something else for debugging. */
4176 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4177}
4178
77d22dca
ID
4179#define for_each_power_domain(domain, mask) \
4180 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4181 if ((1 << (domain)) & (mask))
4182
319be8ae
ID
4183enum intel_display_power_domain
4184intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4185{
4186 struct drm_device *dev = intel_encoder->base.dev;
4187 struct intel_digital_port *intel_dig_port;
4188
4189 switch (intel_encoder->type) {
4190 case INTEL_OUTPUT_UNKNOWN:
4191 /* Only DDI platforms should ever use this output type */
4192 WARN_ON_ONCE(!HAS_DDI(dev));
4193 case INTEL_OUTPUT_DISPLAYPORT:
4194 case INTEL_OUTPUT_HDMI:
4195 case INTEL_OUTPUT_EDP:
4196 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4197 switch (intel_dig_port->port) {
4198 case PORT_A:
4199 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4200 case PORT_B:
4201 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4202 case PORT_C:
4203 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4204 case PORT_D:
4205 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4206 default:
4207 WARN_ON_ONCE(1);
4208 return POWER_DOMAIN_PORT_OTHER;
4209 }
4210 case INTEL_OUTPUT_ANALOG:
4211 return POWER_DOMAIN_PORT_CRT;
4212 case INTEL_OUTPUT_DSI:
4213 return POWER_DOMAIN_PORT_DSI;
4214 default:
4215 return POWER_DOMAIN_PORT_OTHER;
4216 }
4217}
4218
4219static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4220{
319be8ae
ID
4221 struct drm_device *dev = crtc->dev;
4222 struct intel_encoder *intel_encoder;
4223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4224 enum pipe pipe = intel_crtc->pipe;
4225 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
77d22dca
ID
4226 unsigned long mask;
4227 enum transcoder transcoder;
4228
4229 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4230
4231 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4232 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4233 if (pfit_enabled)
4234 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4235
319be8ae
ID
4236 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4237 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4238
77d22dca
ID
4239 return mask;
4240}
4241
4242void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4243 bool enable)
4244{
4245 if (dev_priv->power_domains.init_power_on == enable)
4246 return;
4247
4248 if (enable)
4249 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4250 else
4251 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4252
4253 dev_priv->power_domains.init_power_on = enable;
4254}
4255
4256static void modeset_update_crtc_power_domains(struct drm_device *dev)
4257{
4258 struct drm_i915_private *dev_priv = dev->dev_private;
4259 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4260 struct intel_crtc *crtc;
4261
4262 /*
4263 * First get all needed power domains, then put all unneeded, to avoid
4264 * any unnecessary toggling of the power wells.
4265 */
4266 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4267 enum intel_display_power_domain domain;
4268
4269 if (!crtc->base.enabled)
4270 continue;
4271
319be8ae 4272 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4273
4274 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4275 intel_display_power_get(dev_priv, domain);
4276 }
4277
4278 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4279 enum intel_display_power_domain domain;
4280
4281 for_each_power_domain(domain, crtc->enabled_power_domains)
4282 intel_display_power_put(dev_priv, domain);
4283
4284 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4285 }
4286
4287 intel_display_set_init_power(dev_priv, false);
4288}
4289
586f49dc 4290int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4291{
586f49dc 4292 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4293
586f49dc
JB
4294 /* Obtain SKU information */
4295 mutex_lock(&dev_priv->dpio_lock);
4296 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4297 CCK_FUSE_HPLL_FREQ_MASK;
4298 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4299
586f49dc 4300 return vco_freq[hpll_freq];
30a970c6
JB
4301}
4302
4303/* Adjust CDclk dividers to allow high res or save power if possible */
4304static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4305{
4306 struct drm_i915_private *dev_priv = dev->dev_private;
4307 u32 val, cmd;
4308
d60c4473
ID
4309 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4310 dev_priv->vlv_cdclk_freq = cdclk;
4311
30a970c6
JB
4312 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4313 cmd = 2;
4314 else if (cdclk == 266)
4315 cmd = 1;
4316 else
4317 cmd = 0;
4318
4319 mutex_lock(&dev_priv->rps.hw_lock);
4320 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4321 val &= ~DSPFREQGUAR_MASK;
4322 val |= (cmd << DSPFREQGUAR_SHIFT);
4323 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4324 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4325 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4326 50)) {
4327 DRM_ERROR("timed out waiting for CDclk change\n");
4328 }
4329 mutex_unlock(&dev_priv->rps.hw_lock);
4330
4331 if (cdclk == 400) {
4332 u32 divider, vco;
4333
4334 vco = valleyview_get_vco(dev_priv);
4335 divider = ((vco << 1) / cdclk) - 1;
4336
4337 mutex_lock(&dev_priv->dpio_lock);
4338 /* adjust cdclk divider */
4339 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4340 val &= ~0xf;
4341 val |= divider;
4342 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4343 mutex_unlock(&dev_priv->dpio_lock);
4344 }
4345
4346 mutex_lock(&dev_priv->dpio_lock);
4347 /* adjust self-refresh exit latency value */
4348 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4349 val &= ~0x7f;
4350
4351 /*
4352 * For high bandwidth configs, we set a higher latency in the bunit
4353 * so that the core display fetch happens in time to avoid underruns.
4354 */
4355 if (cdclk == 400)
4356 val |= 4500 / 250; /* 4.5 usec */
4357 else
4358 val |= 3000 / 250; /* 3.0 usec */
4359 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4360 mutex_unlock(&dev_priv->dpio_lock);
4361
4362 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4363 intel_i2c_reset(dev);
4364}
4365
d60c4473 4366int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4367{
4368 int cur_cdclk, vco;
4369 int divider;
4370
4371 vco = valleyview_get_vco(dev_priv);
4372
4373 mutex_lock(&dev_priv->dpio_lock);
4374 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4375 mutex_unlock(&dev_priv->dpio_lock);
4376
4377 divider &= 0xf;
4378
4379 cur_cdclk = (vco << 1) / (divider + 1);
4380
4381 return cur_cdclk;
4382}
4383
4384static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4385 int max_pixclk)
4386{
30a970c6
JB
4387 /*
4388 * Really only a few cases to deal with, as only 4 CDclks are supported:
4389 * 200MHz
4390 * 267MHz
4391 * 320MHz
4392 * 400MHz
4393 * So we check to see whether we're above 90% of the lower bin and
4394 * adjust if needed.
4395 */
4396 if (max_pixclk > 288000) {
4397 return 400;
4398 } else if (max_pixclk > 240000) {
4399 return 320;
4400 } else
4401 return 266;
4402 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4403}
4404
2f2d7aa1
VS
4405/* compute the max pixel clock for new configuration */
4406static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4407{
4408 struct drm_device *dev = dev_priv->dev;
4409 struct intel_crtc *intel_crtc;
4410 int max_pixclk = 0;
4411
4412 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4413 base.head) {
2f2d7aa1 4414 if (intel_crtc->new_enabled)
30a970c6 4415 max_pixclk = max(max_pixclk,
2f2d7aa1 4416 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4417 }
4418
4419 return max_pixclk;
4420}
4421
4422static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4423 unsigned *prepare_pipes)
30a970c6
JB
4424{
4425 struct drm_i915_private *dev_priv = dev->dev_private;
4426 struct intel_crtc *intel_crtc;
2f2d7aa1 4427 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6 4428
d60c4473
ID
4429 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4430 dev_priv->vlv_cdclk_freq)
30a970c6
JB
4431 return;
4432
2f2d7aa1 4433 /* disable/enable all currently active pipes while we change cdclk */
30a970c6
JB
4434 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4435 base.head)
4436 if (intel_crtc->base.enabled)
4437 *prepare_pipes |= (1 << intel_crtc->pipe);
4438}
4439
4440static void valleyview_modeset_global_resources(struct drm_device *dev)
4441{
4442 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4443 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4444 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4445
d60c4473 4446 if (req_cdclk != dev_priv->vlv_cdclk_freq)
30a970c6 4447 valleyview_set_cdclk(dev, req_cdclk);
77961eb9 4448 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4449}
4450
89b667f8
JB
4451static void valleyview_crtc_enable(struct drm_crtc *crtc)
4452{
4453 struct drm_device *dev = crtc->dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
4455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4456 struct intel_encoder *encoder;
4457 int pipe = intel_crtc->pipe;
23538ef1 4458 bool is_dsi;
89b667f8
JB
4459
4460 WARN_ON(!crtc->enabled);
4461
4462 if (intel_crtc->active)
4463 return;
4464
4465 intel_crtc->active = true;
89b667f8 4466
89b667f8
JB
4467 for_each_encoder_on_crtc(dev, crtc, encoder)
4468 if (encoder->pre_pll_enable)
4469 encoder->pre_pll_enable(encoder);
4470
23538ef1
JN
4471 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4472
e9fd1c02
JN
4473 if (!is_dsi)
4474 vlv_enable_pll(intel_crtc);
89b667f8
JB
4475
4476 for_each_encoder_on_crtc(dev, crtc, encoder)
4477 if (encoder->pre_enable)
4478 encoder->pre_enable(encoder);
4479
2dd24552
JB
4480 i9xx_pfit_enable(intel_crtc);
4481
63cbb074
VS
4482 intel_crtc_load_lut(crtc);
4483
f37fcc2a 4484 intel_update_watermarks(crtc);
e1fdc473 4485 intel_enable_pipe(intel_crtc);
be6a6f8e 4486 intel_wait_for_vblank(dev_priv->dev, pipe);
2d9d2b0b 4487 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
be6a6f8e 4488
d3eedb1a 4489 intel_crtc_enable_planes(crtc);
5004945f
JN
4490
4491 for_each_encoder_on_crtc(dev, crtc, encoder)
4492 encoder->enable(encoder);
89b667f8
JB
4493}
4494
0b8765c6 4495static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4496{
4497 struct drm_device *dev = crtc->dev;
79e53945
JB
4498 struct drm_i915_private *dev_priv = dev->dev_private;
4499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4500 struct intel_encoder *encoder;
79e53945 4501 int pipe = intel_crtc->pipe;
79e53945 4502
08a48469
DV
4503 WARN_ON(!crtc->enabled);
4504
f7abfe8b
CW
4505 if (intel_crtc->active)
4506 return;
4507
4508 intel_crtc->active = true;
6b383a7f 4509
9d6d9f19
MK
4510 for_each_encoder_on_crtc(dev, crtc, encoder)
4511 if (encoder->pre_enable)
4512 encoder->pre_enable(encoder);
4513
f6736a1a
DV
4514 i9xx_enable_pll(intel_crtc);
4515
2dd24552
JB
4516 i9xx_pfit_enable(intel_crtc);
4517
63cbb074
VS
4518 intel_crtc_load_lut(crtc);
4519
f37fcc2a 4520 intel_update_watermarks(crtc);
e1fdc473 4521 intel_enable_pipe(intel_crtc);
be6a6f8e 4522 intel_wait_for_vblank(dev_priv->dev, pipe);
2d9d2b0b 4523 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
be6a6f8e 4524
d3eedb1a 4525 intel_crtc_enable_planes(crtc);
ef9c3aee 4526
fa5c73b1
DV
4527 for_each_encoder_on_crtc(dev, crtc, encoder)
4528 encoder->enable(encoder);
0b8765c6 4529}
79e53945 4530
87476d63
DV
4531static void i9xx_pfit_disable(struct intel_crtc *crtc)
4532{
4533 struct drm_device *dev = crtc->base.dev;
4534 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4535
328d8e82
DV
4536 if (!crtc->config.gmch_pfit.control)
4537 return;
87476d63 4538
328d8e82 4539 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4540
328d8e82
DV
4541 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4542 I915_READ(PFIT_CONTROL));
4543 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4544}
4545
0b8765c6
JB
4546static void i9xx_crtc_disable(struct drm_crtc *crtc)
4547{
4548 struct drm_device *dev = crtc->dev;
4549 struct drm_i915_private *dev_priv = dev->dev_private;
4550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4551 struct intel_encoder *encoder;
0b8765c6 4552 int pipe = intel_crtc->pipe;
ef9c3aee 4553
f7abfe8b
CW
4554 if (!intel_crtc->active)
4555 return;
4556
ea9d758d
DV
4557 for_each_encoder_on_crtc(dev, crtc, encoder)
4558 encoder->disable(encoder);
4559
d3eedb1a 4560 intel_crtc_disable_planes(crtc);
0d5b8c61 4561
2d9d2b0b 4562 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
b24e7179 4563 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4564
87476d63 4565 i9xx_pfit_disable(intel_crtc);
24a1f16d 4566
89b667f8
JB
4567 for_each_encoder_on_crtc(dev, crtc, encoder)
4568 if (encoder->post_disable)
4569 encoder->post_disable(encoder);
4570
076ed3b2
CML
4571 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4572 if (IS_CHERRYVIEW(dev))
4573 chv_disable_pll(dev_priv, pipe);
4574 else if (IS_VALLEYVIEW(dev))
4575 vlv_disable_pll(dev_priv, pipe);
4576 else
4577 i9xx_disable_pll(dev_priv, pipe);
4578 }
0b8765c6 4579
f7abfe8b 4580 intel_crtc->active = false;
46ba614c 4581 intel_update_watermarks(crtc);
f37fcc2a 4582
6b383a7f 4583 intel_update_fbc(dev);
0b8765c6
JB
4584}
4585
ee7b9f93
JB
4586static void i9xx_crtc_off(struct drm_crtc *crtc)
4587{
4588}
4589
976f8a20
DV
4590static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4591 bool enabled)
2c07245f
ZW
4592{
4593 struct drm_device *dev = crtc->dev;
4594 struct drm_i915_master_private *master_priv;
4595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4596 int pipe = intel_crtc->pipe;
79e53945
JB
4597
4598 if (!dev->primary->master)
4599 return;
4600
4601 master_priv = dev->primary->master->driver_priv;
4602 if (!master_priv->sarea_priv)
4603 return;
4604
79e53945
JB
4605 switch (pipe) {
4606 case 0:
4607 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4608 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4609 break;
4610 case 1:
4611 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4612 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4613 break;
4614 default:
9db4a9c7 4615 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4616 break;
4617 }
79e53945
JB
4618}
4619
976f8a20
DV
4620/**
4621 * Sets the power management mode of the pipe and plane.
4622 */
4623void intel_crtc_update_dpms(struct drm_crtc *crtc)
4624{
4625 struct drm_device *dev = crtc->dev;
4626 struct drm_i915_private *dev_priv = dev->dev_private;
4627 struct intel_encoder *intel_encoder;
4628 bool enable = false;
4629
4630 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4631 enable |= intel_encoder->connectors_active;
4632
4633 if (enable)
4634 dev_priv->display.crtc_enable(crtc);
4635 else
4636 dev_priv->display.crtc_disable(crtc);
4637
4638 intel_crtc_update_sarea(crtc, enable);
4639}
4640
cdd59983
CW
4641static void intel_crtc_disable(struct drm_crtc *crtc)
4642{
cdd59983 4643 struct drm_device *dev = crtc->dev;
976f8a20 4644 struct drm_connector *connector;
ee7b9f93 4645 struct drm_i915_private *dev_priv = dev->dev_private;
7b9f35a6 4646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cdd59983 4647
976f8a20
DV
4648 /* crtc should still be enabled when we disable it. */
4649 WARN_ON(!crtc->enabled);
4650
4651 dev_priv->display.crtc_disable(crtc);
c77bf565 4652 intel_crtc->eld_vld = false;
976f8a20 4653 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4654 dev_priv->display.off(crtc);
4655
931872fc 4656 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
93ce0ba6 4657 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
931872fc 4658 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
cdd59983 4659
f4510a27 4660 if (crtc->primary->fb) {
cdd59983 4661 mutex_lock(&dev->struct_mutex);
f4510a27 4662 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
cdd59983 4663 mutex_unlock(&dev->struct_mutex);
f4510a27 4664 crtc->primary->fb = NULL;
976f8a20
DV
4665 }
4666
4667 /* Update computed state. */
4668 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4669 if (!connector->encoder || !connector->encoder->crtc)
4670 continue;
4671
4672 if (connector->encoder->crtc != crtc)
4673 continue;
4674
4675 connector->dpms = DRM_MODE_DPMS_OFF;
4676 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
4677 }
4678}
4679
ea5b213a 4680void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 4681{
4ef69c7a 4682 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 4683
ea5b213a
CW
4684 drm_encoder_cleanup(encoder);
4685 kfree(intel_encoder);
7e7d76c3
JB
4686}
4687
9237329d 4688/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
4689 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4690 * state of the entire output pipe. */
9237329d 4691static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 4692{
5ab432ef
DV
4693 if (mode == DRM_MODE_DPMS_ON) {
4694 encoder->connectors_active = true;
4695
b2cabb0e 4696 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
4697 } else {
4698 encoder->connectors_active = false;
4699
b2cabb0e 4700 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 4701 }
79e53945
JB
4702}
4703
0a91ca29
DV
4704/* Cross check the actual hw state with our own modeset state tracking (and it's
4705 * internal consistency). */
b980514c 4706static void intel_connector_check_state(struct intel_connector *connector)
79e53945 4707{
0a91ca29
DV
4708 if (connector->get_hw_state(connector)) {
4709 struct intel_encoder *encoder = connector->encoder;
4710 struct drm_crtc *crtc;
4711 bool encoder_enabled;
4712 enum pipe pipe;
4713
4714 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4715 connector->base.base.id,
4716 drm_get_connector_name(&connector->base));
4717
4718 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4719 "wrong connector dpms state\n");
4720 WARN(connector->base.encoder != &encoder->base,
4721 "active connector not linked to encoder\n");
4722 WARN(!encoder->connectors_active,
4723 "encoder->connectors_active not set\n");
4724
4725 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4726 WARN(!encoder_enabled, "encoder not enabled\n");
4727 if (WARN_ON(!encoder->base.crtc))
4728 return;
4729
4730 crtc = encoder->base.crtc;
4731
4732 WARN(!crtc->enabled, "crtc not enabled\n");
4733 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4734 WARN(pipe != to_intel_crtc(crtc)->pipe,
4735 "encoder active on the wrong pipe\n");
4736 }
79e53945
JB
4737}
4738
5ab432ef
DV
4739/* Even simpler default implementation, if there's really no special case to
4740 * consider. */
4741void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 4742{
5ab432ef
DV
4743 /* All the simple cases only support two dpms states. */
4744 if (mode != DRM_MODE_DPMS_ON)
4745 mode = DRM_MODE_DPMS_OFF;
d4270e57 4746
5ab432ef
DV
4747 if (mode == connector->dpms)
4748 return;
4749
4750 connector->dpms = mode;
4751
4752 /* Only need to change hw state when actually enabled */
c9976dcf
CW
4753 if (connector->encoder)
4754 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 4755
b980514c 4756 intel_modeset_check_state(connector->dev);
79e53945
JB
4757}
4758
f0947c37
DV
4759/* Simple connector->get_hw_state implementation for encoders that support only
4760 * one connector and no cloning and hence the encoder state determines the state
4761 * of the connector. */
4762bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 4763{
24929352 4764 enum pipe pipe = 0;
f0947c37 4765 struct intel_encoder *encoder = connector->encoder;
ea5b213a 4766
f0947c37 4767 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
4768}
4769
1857e1da
DV
4770static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4771 struct intel_crtc_config *pipe_config)
4772{
4773 struct drm_i915_private *dev_priv = dev->dev_private;
4774 struct intel_crtc *pipe_B_crtc =
4775 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4776
4777 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4778 pipe_name(pipe), pipe_config->fdi_lanes);
4779 if (pipe_config->fdi_lanes > 4) {
4780 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4781 pipe_name(pipe), pipe_config->fdi_lanes);
4782 return false;
4783 }
4784
bafb6553 4785 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
4786 if (pipe_config->fdi_lanes > 2) {
4787 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4788 pipe_config->fdi_lanes);
4789 return false;
4790 } else {
4791 return true;
4792 }
4793 }
4794
4795 if (INTEL_INFO(dev)->num_pipes == 2)
4796 return true;
4797
4798 /* Ivybridge 3 pipe is really complicated */
4799 switch (pipe) {
4800 case PIPE_A:
4801 return true;
4802 case PIPE_B:
4803 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4804 pipe_config->fdi_lanes > 2) {
4805 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4806 pipe_name(pipe), pipe_config->fdi_lanes);
4807 return false;
4808 }
4809 return true;
4810 case PIPE_C:
1e833f40 4811 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
4812 pipe_B_crtc->config.fdi_lanes <= 2) {
4813 if (pipe_config->fdi_lanes > 2) {
4814 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4815 pipe_name(pipe), pipe_config->fdi_lanes);
4816 return false;
4817 }
4818 } else {
4819 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4820 return false;
4821 }
4822 return true;
4823 default:
4824 BUG();
4825 }
4826}
4827
e29c22c0
DV
4828#define RETRY 1
4829static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4830 struct intel_crtc_config *pipe_config)
877d48d5 4831{
1857e1da 4832 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 4833 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 4834 int lane, link_bw, fdi_dotclock;
e29c22c0 4835 bool setup_ok, needs_recompute = false;
877d48d5 4836
e29c22c0 4837retry:
877d48d5
DV
4838 /* FDI is a binary signal running at ~2.7GHz, encoding
4839 * each output octet as 10 bits. The actual frequency
4840 * is stored as a divider into a 100MHz clock, and the
4841 * mode pixel clock is stored in units of 1KHz.
4842 * Hence the bw of each lane in terms of the mode signal
4843 * is:
4844 */
4845 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4846
241bfc38 4847 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 4848
2bd89a07 4849 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
4850 pipe_config->pipe_bpp);
4851
4852 pipe_config->fdi_lanes = lane;
4853
2bd89a07 4854 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 4855 link_bw, &pipe_config->fdi_m_n);
1857e1da 4856
e29c22c0
DV
4857 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4858 intel_crtc->pipe, pipe_config);
4859 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4860 pipe_config->pipe_bpp -= 2*3;
4861 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4862 pipe_config->pipe_bpp);
4863 needs_recompute = true;
4864 pipe_config->bw_constrained = true;
4865
4866 goto retry;
4867 }
4868
4869 if (needs_recompute)
4870 return RETRY;
4871
4872 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
4873}
4874
42db64ef
PZ
4875static void hsw_compute_ips_config(struct intel_crtc *crtc,
4876 struct intel_crtc_config *pipe_config)
4877{
d330a953 4878 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 4879 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 4880 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
4881}
4882
a43f6e0f 4883static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 4884 struct intel_crtc_config *pipe_config)
79e53945 4885{
a43f6e0f 4886 struct drm_device *dev = crtc->base.dev;
b8cecdf5 4887 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 4888
ad3a4479 4889 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
4890 if (INTEL_INFO(dev)->gen < 4) {
4891 struct drm_i915_private *dev_priv = dev->dev_private;
4892 int clock_limit =
4893 dev_priv->display.get_display_clock_speed(dev);
4894
4895 /*
4896 * Enable pixel doubling when the dot clock
4897 * is > 90% of the (display) core speed.
4898 *
b397c96b
VS
4899 * GDG double wide on either pipe,
4900 * otherwise pipe A only.
cf532bb2 4901 */
b397c96b 4902 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 4903 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 4904 clock_limit *= 2;
cf532bb2 4905 pipe_config->double_wide = true;
ad3a4479
VS
4906 }
4907
241bfc38 4908 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 4909 return -EINVAL;
2c07245f 4910 }
89749350 4911
1d1d0e27
VS
4912 /*
4913 * Pipe horizontal size must be even in:
4914 * - DVO ganged mode
4915 * - LVDS dual channel mode
4916 * - Double wide pipe
4917 */
4918 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4919 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4920 pipe_config->pipe_src_w &= ~1;
4921
8693a824
DL
4922 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4923 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
4924 */
4925 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4926 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 4927 return -EINVAL;
44f46b42 4928
bd080ee5 4929 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 4930 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 4931 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
4932 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4933 * for lvds. */
4934 pipe_config->pipe_bpp = 8*3;
4935 }
4936
f5adf94e 4937 if (HAS_IPS(dev))
a43f6e0f
DV
4938 hsw_compute_ips_config(crtc, pipe_config);
4939
4940 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4941 * clock survives for now. */
4942 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4943 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 4944
877d48d5 4945 if (pipe_config->has_pch_encoder)
a43f6e0f 4946 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 4947
e29c22c0 4948 return 0;
79e53945
JB
4949}
4950
25eb05fc
JB
4951static int valleyview_get_display_clock_speed(struct drm_device *dev)
4952{
4953 return 400000; /* FIXME */
4954}
4955
e70236a8
JB
4956static int i945_get_display_clock_speed(struct drm_device *dev)
4957{
4958 return 400000;
4959}
79e53945 4960
e70236a8 4961static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 4962{
e70236a8
JB
4963 return 333000;
4964}
79e53945 4965
e70236a8
JB
4966static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4967{
4968 return 200000;
4969}
79e53945 4970
257a7ffc
DV
4971static int pnv_get_display_clock_speed(struct drm_device *dev)
4972{
4973 u16 gcfgc = 0;
4974
4975 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4976
4977 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4978 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4979 return 267000;
4980 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4981 return 333000;
4982 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4983 return 444000;
4984 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4985 return 200000;
4986 default:
4987 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4988 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4989 return 133000;
4990 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4991 return 167000;
4992 }
4993}
4994
e70236a8
JB
4995static int i915gm_get_display_clock_speed(struct drm_device *dev)
4996{
4997 u16 gcfgc = 0;
79e53945 4998
e70236a8
JB
4999 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5000
5001 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5002 return 133000;
5003 else {
5004 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5005 case GC_DISPLAY_CLOCK_333_MHZ:
5006 return 333000;
5007 default:
5008 case GC_DISPLAY_CLOCK_190_200_MHZ:
5009 return 190000;
79e53945 5010 }
e70236a8
JB
5011 }
5012}
5013
5014static int i865_get_display_clock_speed(struct drm_device *dev)
5015{
5016 return 266000;
5017}
5018
5019static int i855_get_display_clock_speed(struct drm_device *dev)
5020{
5021 u16 hpllcc = 0;
5022 /* Assume that the hardware is in the high speed state. This
5023 * should be the default.
5024 */
5025 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5026 case GC_CLOCK_133_200:
5027 case GC_CLOCK_100_200:
5028 return 200000;
5029 case GC_CLOCK_166_250:
5030 return 250000;
5031 case GC_CLOCK_100_133:
79e53945 5032 return 133000;
e70236a8 5033 }
79e53945 5034
e70236a8
JB
5035 /* Shouldn't happen */
5036 return 0;
5037}
79e53945 5038
e70236a8
JB
5039static int i830_get_display_clock_speed(struct drm_device *dev)
5040{
5041 return 133000;
79e53945
JB
5042}
5043
2c07245f 5044static void
a65851af 5045intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 5046{
a65851af
VS
5047 while (*num > DATA_LINK_M_N_MASK ||
5048 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
5049 *num >>= 1;
5050 *den >>= 1;
5051 }
5052}
5053
a65851af
VS
5054static void compute_m_n(unsigned int m, unsigned int n,
5055 uint32_t *ret_m, uint32_t *ret_n)
5056{
5057 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5058 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5059 intel_reduce_m_n_ratio(ret_m, ret_n);
5060}
5061
e69d0bc1
DV
5062void
5063intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5064 int pixel_clock, int link_clock,
5065 struct intel_link_m_n *m_n)
2c07245f 5066{
e69d0bc1 5067 m_n->tu = 64;
a65851af
VS
5068
5069 compute_m_n(bits_per_pixel * pixel_clock,
5070 link_clock * nlanes * 8,
5071 &m_n->gmch_m, &m_n->gmch_n);
5072
5073 compute_m_n(pixel_clock, link_clock,
5074 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
5075}
5076
a7615030
CW
5077static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5078{
d330a953
JN
5079 if (i915.panel_use_ssc >= 0)
5080 return i915.panel_use_ssc != 0;
41aa3448 5081 return dev_priv->vbt.lvds_use_ssc
435793df 5082 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
5083}
5084
c65d77d8
JB
5085static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5086{
5087 struct drm_device *dev = crtc->dev;
5088 struct drm_i915_private *dev_priv = dev->dev_private;
5089 int refclk;
5090
a0c4da24 5091 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5092 refclk = 100000;
a0c4da24 5093 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5094 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5095 refclk = dev_priv->vbt.lvds_ssc_freq;
5096 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5097 } else if (!IS_GEN2(dev)) {
5098 refclk = 96000;
5099 } else {
5100 refclk = 48000;
5101 }
5102
5103 return refclk;
5104}
5105
7429e9d4 5106static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5107{
7df00d7a 5108 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5109}
f47709a9 5110
7429e9d4
DV
5111static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5112{
5113 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5114}
5115
f47709a9 5116static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5117 intel_clock_t *reduced_clock)
5118{
f47709a9 5119 struct drm_device *dev = crtc->base.dev;
a7516a05 5120 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5121 int pipe = crtc->pipe;
a7516a05
JB
5122 u32 fp, fp2 = 0;
5123
5124 if (IS_PINEVIEW(dev)) {
7429e9d4 5125 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5126 if (reduced_clock)
7429e9d4 5127 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5128 } else {
7429e9d4 5129 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5130 if (reduced_clock)
7429e9d4 5131 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5132 }
5133
5134 I915_WRITE(FP0(pipe), fp);
8bcc2795 5135 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5136
f47709a9
DV
5137 crtc->lowfreq_avail = false;
5138 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5139 reduced_clock && i915.powersave) {
a7516a05 5140 I915_WRITE(FP1(pipe), fp2);
8bcc2795 5141 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5142 crtc->lowfreq_avail = true;
a7516a05
JB
5143 } else {
5144 I915_WRITE(FP1(pipe), fp);
8bcc2795 5145 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5146 }
5147}
5148
5e69f97f
CML
5149static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5150 pipe)
89b667f8
JB
5151{
5152 u32 reg_val;
5153
5154 /*
5155 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5156 * and set it to a reasonable value instead.
5157 */
ab3c759a 5158 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5159 reg_val &= 0xffffff00;
5160 reg_val |= 0x00000030;
ab3c759a 5161 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5162
ab3c759a 5163 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5164 reg_val &= 0x8cffffff;
5165 reg_val = 0x8c000000;
ab3c759a 5166 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5167
ab3c759a 5168 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5169 reg_val &= 0xffffff00;
ab3c759a 5170 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5171
ab3c759a 5172 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5173 reg_val &= 0x00ffffff;
5174 reg_val |= 0xb0000000;
ab3c759a 5175 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5176}
5177
b551842d
DV
5178static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5179 struct intel_link_m_n *m_n)
5180{
5181 struct drm_device *dev = crtc->base.dev;
5182 struct drm_i915_private *dev_priv = dev->dev_private;
5183 int pipe = crtc->pipe;
5184
e3b95f1e
DV
5185 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5186 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5187 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5188 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5189}
5190
5191static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5192 struct intel_link_m_n *m_n)
5193{
5194 struct drm_device *dev = crtc->base.dev;
5195 struct drm_i915_private *dev_priv = dev->dev_private;
5196 int pipe = crtc->pipe;
5197 enum transcoder transcoder = crtc->config.cpu_transcoder;
5198
5199 if (INTEL_INFO(dev)->gen >= 5) {
5200 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5201 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5202 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5203 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5204 } else {
e3b95f1e
DV
5205 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5206 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5207 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5208 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5209 }
5210}
5211
03afc4a2
DV
5212static void intel_dp_set_m_n(struct intel_crtc *crtc)
5213{
5214 if (crtc->config.has_pch_encoder)
5215 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5216 else
5217 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5218}
5219
f47709a9 5220static void vlv_update_pll(struct intel_crtc *crtc)
a0c4da24 5221{
f47709a9 5222 struct drm_device *dev = crtc->base.dev;
a0c4da24 5223 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5224 int pipe = crtc->pipe;
89b667f8 5225 u32 dpll, mdiv;
a0c4da24 5226 u32 bestn, bestm1, bestm2, bestp1, bestp2;
198a037f 5227 u32 coreclk, reg_val, dpll_md;
a0c4da24 5228
09153000
DV
5229 mutex_lock(&dev_priv->dpio_lock);
5230
f47709a9
DV
5231 bestn = crtc->config.dpll.n;
5232 bestm1 = crtc->config.dpll.m1;
5233 bestm2 = crtc->config.dpll.m2;
5234 bestp1 = crtc->config.dpll.p1;
5235 bestp2 = crtc->config.dpll.p2;
a0c4da24 5236
89b667f8
JB
5237 /* See eDP HDMI DPIO driver vbios notes doc */
5238
5239 /* PLL B needs special handling */
5240 if (pipe)
5e69f97f 5241 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5242
5243 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5244 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5245
5246 /* Disable target IRef on PLL */
ab3c759a 5247 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5248 reg_val &= 0x00ffffff;
ab3c759a 5249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5250
5251 /* Disable fast lock */
ab3c759a 5252 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5253
5254 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5255 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5256 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5257 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5258 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5259
5260 /*
5261 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5262 * but we don't support that).
5263 * Note: don't use the DAC post divider as it seems unstable.
5264 */
5265 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5267
a0c4da24 5268 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5270
89b667f8 5271 /* Set HBR and RBR LPF coefficients */
ff9a6750 5272 if (crtc->config.port_clock == 162000 ||
99750bd4 5273 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5274 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5276 0x009f0003);
89b667f8 5277 else
ab3c759a 5278 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5279 0x00d0000f);
5280
5281 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5282 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5283 /* Use SSC source */
5284 if (!pipe)
ab3c759a 5285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5286 0x0df40000);
5287 else
ab3c759a 5288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5289 0x0df70000);
5290 } else { /* HDMI or VGA */
5291 /* Use bend source */
5292 if (!pipe)
ab3c759a 5293 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5294 0x0df70000);
5295 else
ab3c759a 5296 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5297 0x0df40000);
5298 }
a0c4da24 5299
ab3c759a 5300 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5301 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5302 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5303 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5304 coreclk |= 0x01000000;
ab3c759a 5305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5306
ab3c759a 5307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a0c4da24 5308
e5cbfbfb
ID
5309 /*
5310 * Enable DPIO clock input. We should never disable the reference
5311 * clock for pipe B, since VGA hotplug / manual detection depends
5312 * on it.
5313 */
89b667f8
JB
5314 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5315 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
f6071166
JB
5316 /* We should never disable this, set it here for state tracking */
5317 if (pipe == PIPE_B)
89b667f8 5318 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
a0c4da24 5319 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5320 crtc->config.dpll_hw_state.dpll = dpll;
5321
ef1b460d
DV
5322 dpll_md = (crtc->config.pixel_multiplier - 1)
5323 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795
DV
5324 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5325
09153000 5326 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5327}
5328
f47709a9
DV
5329static void i9xx_update_pll(struct intel_crtc *crtc,
5330 intel_clock_t *reduced_clock,
eb1cbe48
DV
5331 int num_connectors)
5332{
f47709a9 5333 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5334 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5335 u32 dpll;
5336 bool is_sdvo;
f47709a9 5337 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5338
f47709a9 5339 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5340
f47709a9
DV
5341 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5342 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5343
5344 dpll = DPLL_VGA_MODE_DIS;
5345
f47709a9 5346 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5347 dpll |= DPLLB_MODE_LVDS;
5348 else
5349 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5350
ef1b460d 5351 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5352 dpll |= (crtc->config.pixel_multiplier - 1)
5353 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5354 }
198a037f
DV
5355
5356 if (is_sdvo)
4a33e48d 5357 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5358
f47709a9 5359 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5360 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5361
5362 /* compute bitmask from p1 value */
5363 if (IS_PINEVIEW(dev))
5364 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5365 else {
5366 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5367 if (IS_G4X(dev) && reduced_clock)
5368 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5369 }
5370 switch (clock->p2) {
5371 case 5:
5372 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5373 break;
5374 case 7:
5375 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5376 break;
5377 case 10:
5378 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5379 break;
5380 case 14:
5381 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5382 break;
5383 }
5384 if (INTEL_INFO(dev)->gen >= 4)
5385 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5386
09ede541 5387 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5388 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5389 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5390 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5391 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5392 else
5393 dpll |= PLL_REF_INPUT_DREFCLK;
5394
5395 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5396 crtc->config.dpll_hw_state.dpll = dpll;
5397
eb1cbe48 5398 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5399 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5400 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5401 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
5402 }
5403}
5404
f47709a9 5405static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5406 intel_clock_t *reduced_clock,
eb1cbe48
DV
5407 int num_connectors)
5408{
f47709a9 5409 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5410 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5411 u32 dpll;
f47709a9 5412 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5413
f47709a9 5414 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5415
eb1cbe48
DV
5416 dpll = DPLL_VGA_MODE_DIS;
5417
f47709a9 5418 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5419 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5420 } else {
5421 if (clock->p1 == 2)
5422 dpll |= PLL_P1_DIVIDE_BY_TWO;
5423 else
5424 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5425 if (clock->p2 == 4)
5426 dpll |= PLL_P2_DIVIDE_BY_4;
5427 }
5428
4a33e48d
DV
5429 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5430 dpll |= DPLL_DVO_2X_MODE;
5431
f47709a9 5432 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5433 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5434 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5435 else
5436 dpll |= PLL_REF_INPUT_DREFCLK;
5437
5438 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5439 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5440}
5441
8a654f3b 5442static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5443{
5444 struct drm_device *dev = intel_crtc->base.dev;
5445 struct drm_i915_private *dev_priv = dev->dev_private;
5446 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5447 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5448 struct drm_display_mode *adjusted_mode =
5449 &intel_crtc->config.adjusted_mode;
1caea6e9
VS
5450 uint32_t crtc_vtotal, crtc_vblank_end;
5451 int vsyncshift = 0;
4d8a62ea
DV
5452
5453 /* We need to be careful not to changed the adjusted mode, for otherwise
5454 * the hw state checker will get angry at the mismatch. */
5455 crtc_vtotal = adjusted_mode->crtc_vtotal;
5456 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 5457
609aeaca 5458 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 5459 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5460 crtc_vtotal -= 1;
5461 crtc_vblank_end -= 1;
609aeaca
VS
5462
5463 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5464 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5465 else
5466 vsyncshift = adjusted_mode->crtc_hsync_start -
5467 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
5468 if (vsyncshift < 0)
5469 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
5470 }
5471
5472 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5473 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5474
fe2b8f9d 5475 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5476 (adjusted_mode->crtc_hdisplay - 1) |
5477 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5478 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5479 (adjusted_mode->crtc_hblank_start - 1) |
5480 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5481 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5482 (adjusted_mode->crtc_hsync_start - 1) |
5483 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5484
fe2b8f9d 5485 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5486 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5487 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5488 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5489 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5490 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5491 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5492 (adjusted_mode->crtc_vsync_start - 1) |
5493 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5494
b5e508d4
PZ
5495 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5496 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5497 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5498 * bits. */
5499 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5500 (pipe == PIPE_B || pipe == PIPE_C))
5501 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5502
b0e77b9c
PZ
5503 /* pipesrc controls the size that is scaled from, which should
5504 * always be the user's requested size.
5505 */
5506 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5507 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5508 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5509}
5510
1bd1bd80
DV
5511static void intel_get_pipe_timings(struct intel_crtc *crtc,
5512 struct intel_crtc_config *pipe_config)
5513{
5514 struct drm_device *dev = crtc->base.dev;
5515 struct drm_i915_private *dev_priv = dev->dev_private;
5516 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5517 uint32_t tmp;
5518
5519 tmp = I915_READ(HTOTAL(cpu_transcoder));
5520 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5521 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5522 tmp = I915_READ(HBLANK(cpu_transcoder));
5523 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5524 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5525 tmp = I915_READ(HSYNC(cpu_transcoder));
5526 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5527 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5528
5529 tmp = I915_READ(VTOTAL(cpu_transcoder));
5530 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5531 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5532 tmp = I915_READ(VBLANK(cpu_transcoder));
5533 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5534 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5535 tmp = I915_READ(VSYNC(cpu_transcoder));
5536 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5537 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5538
5539 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5540 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5541 pipe_config->adjusted_mode.crtc_vtotal += 1;
5542 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5543 }
5544
5545 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
5546 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5547 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5548
5549 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5550 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
5551}
5552
f6a83288
DV
5553void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5554 struct intel_crtc_config *pipe_config)
babea61d 5555{
f6a83288
DV
5556 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5557 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5558 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5559 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 5560
f6a83288
DV
5561 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5562 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5563 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5564 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 5565
f6a83288 5566 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 5567
f6a83288
DV
5568 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5569 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
5570}
5571
84b046f3
DV
5572static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5573{
5574 struct drm_device *dev = intel_crtc->base.dev;
5575 struct drm_i915_private *dev_priv = dev->dev_private;
5576 uint32_t pipeconf;
5577
9f11a9e4 5578 pipeconf = 0;
84b046f3 5579
67c72a12
DV
5580 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5581 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5582 pipeconf |= PIPECONF_ENABLE;
5583
cf532bb2
VS
5584 if (intel_crtc->config.double_wide)
5585 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 5586
ff9ce46e
DV
5587 /* only g4x and later have fancy bpc/dither controls */
5588 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
5589 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5590 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5591 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 5592 PIPECONF_DITHER_TYPE_SP;
84b046f3 5593
ff9ce46e
DV
5594 switch (intel_crtc->config.pipe_bpp) {
5595 case 18:
5596 pipeconf |= PIPECONF_6BPC;
5597 break;
5598 case 24:
5599 pipeconf |= PIPECONF_8BPC;
5600 break;
5601 case 30:
5602 pipeconf |= PIPECONF_10BPC;
5603 break;
5604 default:
5605 /* Case prevented by intel_choose_pipe_bpp_dither. */
5606 BUG();
84b046f3
DV
5607 }
5608 }
5609
5610 if (HAS_PIPE_CXSR(dev)) {
5611 if (intel_crtc->lowfreq_avail) {
5612 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5613 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5614 } else {
5615 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
5616 }
5617 }
5618
efc2cfff
VS
5619 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5620 if (INTEL_INFO(dev)->gen < 4 ||
5621 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5622 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5623 else
5624 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5625 } else
84b046f3
DV
5626 pipeconf |= PIPECONF_PROGRESSIVE;
5627
9f11a9e4
DV
5628 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5629 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 5630
84b046f3
DV
5631 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5632 POSTING_READ(PIPECONF(intel_crtc->pipe));
5633}
5634
f564048e 5635static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 5636 int x, int y,
94352cf9 5637 struct drm_framebuffer *fb)
79e53945
JB
5638{
5639 struct drm_device *dev = crtc->dev;
5640 struct drm_i915_private *dev_priv = dev->dev_private;
5641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5642 int pipe = intel_crtc->pipe;
80824003 5643 int plane = intel_crtc->plane;
c751ce4f 5644 int refclk, num_connectors = 0;
652c393a 5645 intel_clock_t clock, reduced_clock;
84b046f3 5646 u32 dspcntr;
a16af721 5647 bool ok, has_reduced_clock = false;
e9fd1c02 5648 bool is_lvds = false, is_dsi = false;
5eddb70b 5649 struct intel_encoder *encoder;
d4906093 5650 const intel_limit_t *limit;
5c3b82e2 5651 int ret;
79e53945 5652
6c2b7c12 5653 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 5654 switch (encoder->type) {
79e53945
JB
5655 case INTEL_OUTPUT_LVDS:
5656 is_lvds = true;
5657 break;
e9fd1c02
JN
5658 case INTEL_OUTPUT_DSI:
5659 is_dsi = true;
5660 break;
79e53945 5661 }
43565a06 5662
c751ce4f 5663 num_connectors++;
79e53945
JB
5664 }
5665
f2335330
JN
5666 if (is_dsi)
5667 goto skip_dpll;
5668
5669 if (!intel_crtc->config.clock_set) {
5670 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 5671
e9fd1c02
JN
5672 /*
5673 * Returns a set of divisors for the desired target clock with
5674 * the given refclk, or FALSE. The returned values represent
5675 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5676 * 2) / p1 / p2.
5677 */
5678 limit = intel_limit(crtc, refclk);
5679 ok = dev_priv->display.find_dpll(limit, crtc,
5680 intel_crtc->config.port_clock,
5681 refclk, NULL, &clock);
f2335330 5682 if (!ok) {
e9fd1c02
JN
5683 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5684 return -EINVAL;
5685 }
79e53945 5686
f2335330
JN
5687 if (is_lvds && dev_priv->lvds_downclock_avail) {
5688 /*
5689 * Ensure we match the reduced clock's P to the target
5690 * clock. If the clocks don't match, we can't switch
5691 * the display clock by using the FP0/FP1. In such case
5692 * we will disable the LVDS downclock feature.
5693 */
5694 has_reduced_clock =
5695 dev_priv->display.find_dpll(limit, crtc,
5696 dev_priv->lvds_downclock,
5697 refclk, &clock,
5698 &reduced_clock);
5699 }
5700 /* Compat-code for transition, will disappear. */
f47709a9
DV
5701 intel_crtc->config.dpll.n = clock.n;
5702 intel_crtc->config.dpll.m1 = clock.m1;
5703 intel_crtc->config.dpll.m2 = clock.m2;
5704 intel_crtc->config.dpll.p1 = clock.p1;
5705 intel_crtc->config.dpll.p2 = clock.p2;
5706 }
7026d4ac 5707
e9fd1c02 5708 if (IS_GEN2(dev)) {
8a654f3b 5709 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
5710 has_reduced_clock ? &reduced_clock : NULL,
5711 num_connectors);
e9fd1c02 5712 } else if (IS_VALLEYVIEW(dev)) {
f2335330 5713 vlv_update_pll(intel_crtc);
e9fd1c02 5714 } else {
f47709a9 5715 i9xx_update_pll(intel_crtc,
eb1cbe48 5716 has_reduced_clock ? &reduced_clock : NULL,
89b667f8 5717 num_connectors);
e9fd1c02 5718 }
79e53945 5719
f2335330 5720skip_dpll:
79e53945
JB
5721 /* Set up the display plane register */
5722 dspcntr = DISPPLANE_GAMMA_ENABLE;
5723
da6ecc5d
JB
5724 if (!IS_VALLEYVIEW(dev)) {
5725 if (pipe == 0)
5726 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5727 else
5728 dspcntr |= DISPPLANE_SEL_PIPE_B;
5729 }
79e53945 5730
2070f00c
VS
5731 if (intel_crtc->config.has_dp_encoder)
5732 intel_dp_set_m_n(intel_crtc);
5733
8a654f3b 5734 intel_set_pipe_timings(intel_crtc);
5eddb70b
CW
5735
5736 /* pipesrc and dspsize control the size that is scaled from,
5737 * which should always be the user's requested size.
79e53945 5738 */
929c77fb 5739 I915_WRITE(DSPSIZE(plane),
37327abd
VS
5740 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5741 (intel_crtc->config.pipe_src_w - 1));
929c77fb 5742 I915_WRITE(DSPPOS(plane), 0);
2c07245f 5743
84b046f3
DV
5744 i9xx_set_pipeconf(intel_crtc);
5745
f564048e
EA
5746 I915_WRITE(DSPCNTR(plane), dspcntr);
5747 POSTING_READ(DSPCNTR(plane));
5748
94352cf9 5749 ret = intel_pipe_set_base(crtc, x, y, fb);
f564048e 5750
f564048e
EA
5751 return ret;
5752}
5753
2fa2fe9a
DV
5754static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5755 struct intel_crtc_config *pipe_config)
5756{
5757 struct drm_device *dev = crtc->base.dev;
5758 struct drm_i915_private *dev_priv = dev->dev_private;
5759 uint32_t tmp;
5760
dc9e7dec
VS
5761 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
5762 return;
5763
2fa2fe9a 5764 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
5765 if (!(tmp & PFIT_ENABLE))
5766 return;
2fa2fe9a 5767
06922821 5768 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
5769 if (INTEL_INFO(dev)->gen < 4) {
5770 if (crtc->pipe != PIPE_B)
5771 return;
2fa2fe9a
DV
5772 } else {
5773 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5774 return;
5775 }
5776
06922821 5777 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
5778 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5779 if (INTEL_INFO(dev)->gen < 5)
5780 pipe_config->gmch_pfit.lvds_border_bits =
5781 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5782}
5783
acbec814
JB
5784static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5785 struct intel_crtc_config *pipe_config)
5786{
5787 struct drm_device *dev = crtc->base.dev;
5788 struct drm_i915_private *dev_priv = dev->dev_private;
5789 int pipe = pipe_config->cpu_transcoder;
5790 intel_clock_t clock;
5791 u32 mdiv;
662c6ecb 5792 int refclk = 100000;
acbec814
JB
5793
5794 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 5795 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
5796 mutex_unlock(&dev_priv->dpio_lock);
5797
5798 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5799 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5800 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5801 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5802 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5803
f646628b 5804 vlv_clock(refclk, &clock);
acbec814 5805
f646628b
VS
5806 /* clock.dot is the fast clock */
5807 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
5808}
5809
1ad292b5
JB
5810static void i9xx_get_plane_config(struct intel_crtc *crtc,
5811 struct intel_plane_config *plane_config)
5812{
5813 struct drm_device *dev = crtc->base.dev;
5814 struct drm_i915_private *dev_priv = dev->dev_private;
5815 u32 val, base, offset;
5816 int pipe = crtc->pipe, plane = crtc->plane;
5817 int fourcc, pixel_format;
5818 int aligned_height;
5819
66e514c1
DA
5820 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
5821 if (!crtc->base.primary->fb) {
1ad292b5
JB
5822 DRM_DEBUG_KMS("failed to alloc fb\n");
5823 return;
5824 }
5825
5826 val = I915_READ(DSPCNTR(plane));
5827
5828 if (INTEL_INFO(dev)->gen >= 4)
5829 if (val & DISPPLANE_TILED)
5830 plane_config->tiled = true;
5831
5832 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
5833 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
5834 crtc->base.primary->fb->pixel_format = fourcc;
5835 crtc->base.primary->fb->bits_per_pixel =
1ad292b5
JB
5836 drm_format_plane_cpp(fourcc, 0) * 8;
5837
5838 if (INTEL_INFO(dev)->gen >= 4) {
5839 if (plane_config->tiled)
5840 offset = I915_READ(DSPTILEOFF(plane));
5841 else
5842 offset = I915_READ(DSPLINOFF(plane));
5843 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
5844 } else {
5845 base = I915_READ(DSPADDR(plane));
5846 }
5847 plane_config->base = base;
5848
5849 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
5850 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
5851 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
5852
5853 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 5854 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
1ad292b5 5855
66e514c1 5856 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
1ad292b5
JB
5857 plane_config->tiled);
5858
66e514c1 5859 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
1ad292b5
JB
5860 aligned_height, PAGE_SIZE);
5861
5862 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
5863 pipe, plane, crtc->base.primary->fb->width,
5864 crtc->base.primary->fb->height,
5865 crtc->base.primary->fb->bits_per_pixel, base,
5866 crtc->base.primary->fb->pitches[0],
1ad292b5
JB
5867 plane_config->size);
5868
5869}
5870
0e8ffe1b
DV
5871static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5872 struct intel_crtc_config *pipe_config)
5873{
5874 struct drm_device *dev = crtc->base.dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 uint32_t tmp;
5877
b5482bd0
ID
5878 if (!intel_display_power_enabled(dev_priv,
5879 POWER_DOMAIN_PIPE(crtc->pipe)))
5880 return false;
5881
e143a21c 5882 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 5883 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 5884
0e8ffe1b
DV
5885 tmp = I915_READ(PIPECONF(crtc->pipe));
5886 if (!(tmp & PIPECONF_ENABLE))
5887 return false;
5888
42571aef
VS
5889 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5890 switch (tmp & PIPECONF_BPC_MASK) {
5891 case PIPECONF_6BPC:
5892 pipe_config->pipe_bpp = 18;
5893 break;
5894 case PIPECONF_8BPC:
5895 pipe_config->pipe_bpp = 24;
5896 break;
5897 case PIPECONF_10BPC:
5898 pipe_config->pipe_bpp = 30;
5899 break;
5900 default:
5901 break;
5902 }
5903 }
5904
282740f7
VS
5905 if (INTEL_INFO(dev)->gen < 4)
5906 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5907
1bd1bd80
DV
5908 intel_get_pipe_timings(crtc, pipe_config);
5909
2fa2fe9a
DV
5910 i9xx_get_pfit_config(crtc, pipe_config);
5911
6c49f241
DV
5912 if (INTEL_INFO(dev)->gen >= 4) {
5913 tmp = I915_READ(DPLL_MD(crtc->pipe));
5914 pipe_config->pixel_multiplier =
5915 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5916 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 5917 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
5918 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5919 tmp = I915_READ(DPLL(crtc->pipe));
5920 pipe_config->pixel_multiplier =
5921 ((tmp & SDVO_MULTIPLIER_MASK)
5922 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5923 } else {
5924 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5925 * port and will be fixed up in the encoder->get_config
5926 * function. */
5927 pipe_config->pixel_multiplier = 1;
5928 }
8bcc2795
DV
5929 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5930 if (!IS_VALLEYVIEW(dev)) {
5931 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5932 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
5933 } else {
5934 /* Mask out read-only status bits. */
5935 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5936 DPLL_PORTC_READY_MASK |
5937 DPLL_PORTB_READY_MASK);
8bcc2795 5938 }
6c49f241 5939
acbec814
JB
5940 if (IS_VALLEYVIEW(dev))
5941 vlv_crtc_clock_get(crtc, pipe_config);
5942 else
5943 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 5944
0e8ffe1b
DV
5945 return true;
5946}
5947
dde86e2d 5948static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
5949{
5950 struct drm_i915_private *dev_priv = dev->dev_private;
5951 struct drm_mode_config *mode_config = &dev->mode_config;
13d83a67 5952 struct intel_encoder *encoder;
74cfd7ac 5953 u32 val, final;
13d83a67 5954 bool has_lvds = false;
199e5d79 5955 bool has_cpu_edp = false;
199e5d79 5956 bool has_panel = false;
99eb6a01
KP
5957 bool has_ck505 = false;
5958 bool can_ssc = false;
13d83a67
JB
5959
5960 /* We need to take the global config into account */
199e5d79
KP
5961 list_for_each_entry(encoder, &mode_config->encoder_list,
5962 base.head) {
5963 switch (encoder->type) {
5964 case INTEL_OUTPUT_LVDS:
5965 has_panel = true;
5966 has_lvds = true;
5967 break;
5968 case INTEL_OUTPUT_EDP:
5969 has_panel = true;
2de6905f 5970 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
5971 has_cpu_edp = true;
5972 break;
13d83a67
JB
5973 }
5974 }
5975
99eb6a01 5976 if (HAS_PCH_IBX(dev)) {
41aa3448 5977 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
5978 can_ssc = has_ck505;
5979 } else {
5980 has_ck505 = false;
5981 can_ssc = true;
5982 }
5983
2de6905f
ID
5984 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5985 has_panel, has_lvds, has_ck505);
13d83a67
JB
5986
5987 /* Ironlake: try to setup display ref clock before DPLL
5988 * enabling. This is only under driver's control after
5989 * PCH B stepping, previous chipset stepping should be
5990 * ignoring this setting.
5991 */
74cfd7ac
CW
5992 val = I915_READ(PCH_DREF_CONTROL);
5993
5994 /* As we must carefully and slowly disable/enable each source in turn,
5995 * compute the final state we want first and check if we need to
5996 * make any changes at all.
5997 */
5998 final = val;
5999 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6000 if (has_ck505)
6001 final |= DREF_NONSPREAD_CK505_ENABLE;
6002 else
6003 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6004
6005 final &= ~DREF_SSC_SOURCE_MASK;
6006 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6007 final &= ~DREF_SSC1_ENABLE;
6008
6009 if (has_panel) {
6010 final |= DREF_SSC_SOURCE_ENABLE;
6011
6012 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6013 final |= DREF_SSC1_ENABLE;
6014
6015 if (has_cpu_edp) {
6016 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6017 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6018 else
6019 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6020 } else
6021 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6022 } else {
6023 final |= DREF_SSC_SOURCE_DISABLE;
6024 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6025 }
6026
6027 if (final == val)
6028 return;
6029
13d83a67 6030 /* Always enable nonspread source */
74cfd7ac 6031 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 6032
99eb6a01 6033 if (has_ck505)
74cfd7ac 6034 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 6035 else
74cfd7ac 6036 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 6037
199e5d79 6038 if (has_panel) {
74cfd7ac
CW
6039 val &= ~DREF_SSC_SOURCE_MASK;
6040 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 6041
199e5d79 6042 /* SSC must be turned on before enabling the CPU output */
99eb6a01 6043 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6044 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 6045 val |= DREF_SSC1_ENABLE;
e77166b5 6046 } else
74cfd7ac 6047 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
6048
6049 /* Get SSC going before enabling the outputs */
74cfd7ac 6050 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6051 POSTING_READ(PCH_DREF_CONTROL);
6052 udelay(200);
6053
74cfd7ac 6054 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
6055
6056 /* Enable CPU source on CPU attached eDP */
199e5d79 6057 if (has_cpu_edp) {
99eb6a01 6058 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 6059 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 6060 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
199e5d79 6061 }
13d83a67 6062 else
74cfd7ac 6063 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 6064 } else
74cfd7ac 6065 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6066
74cfd7ac 6067 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6068 POSTING_READ(PCH_DREF_CONTROL);
6069 udelay(200);
6070 } else {
6071 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6072
74cfd7ac 6073 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
6074
6075 /* Turn off CPU output */
74cfd7ac 6076 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 6077
74cfd7ac 6078 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
6079 POSTING_READ(PCH_DREF_CONTROL);
6080 udelay(200);
6081
6082 /* Turn off the SSC source */
74cfd7ac
CW
6083 val &= ~DREF_SSC_SOURCE_MASK;
6084 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
6085
6086 /* Turn off SSC1 */
74cfd7ac 6087 val &= ~DREF_SSC1_ENABLE;
199e5d79 6088
74cfd7ac 6089 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
6090 POSTING_READ(PCH_DREF_CONTROL);
6091 udelay(200);
6092 }
74cfd7ac
CW
6093
6094 BUG_ON(val != final);
13d83a67
JB
6095}
6096
f31f2d55 6097static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6098{
f31f2d55 6099 uint32_t tmp;
dde86e2d 6100
0ff066a9
PZ
6101 tmp = I915_READ(SOUTH_CHICKEN2);
6102 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6103 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6104
0ff066a9
PZ
6105 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6106 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6107 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6108
0ff066a9
PZ
6109 tmp = I915_READ(SOUTH_CHICKEN2);
6110 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6111 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6112
0ff066a9
PZ
6113 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6114 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6115 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6116}
6117
6118/* WaMPhyProgramming:hsw */
6119static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6120{
6121 uint32_t tmp;
dde86e2d
PZ
6122
6123 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6124 tmp &= ~(0xFF << 24);
6125 tmp |= (0x12 << 24);
6126 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6127
dde86e2d
PZ
6128 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6129 tmp |= (1 << 11);
6130 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6131
6132 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6133 tmp |= (1 << 11);
6134 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6135
dde86e2d
PZ
6136 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6137 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6138 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6139
6140 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6141 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6142 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6143
0ff066a9
PZ
6144 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6145 tmp &= ~(7 << 13);
6146 tmp |= (5 << 13);
6147 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6148
0ff066a9
PZ
6149 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6150 tmp &= ~(7 << 13);
6151 tmp |= (5 << 13);
6152 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6153
6154 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6155 tmp &= ~0xFF;
6156 tmp |= 0x1C;
6157 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6158
6159 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6160 tmp &= ~0xFF;
6161 tmp |= 0x1C;
6162 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6163
6164 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6165 tmp &= ~(0xFF << 16);
6166 tmp |= (0x1C << 16);
6167 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6168
6169 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6170 tmp &= ~(0xFF << 16);
6171 tmp |= (0x1C << 16);
6172 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6173
0ff066a9
PZ
6174 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6175 tmp |= (1 << 27);
6176 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6177
0ff066a9
PZ
6178 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6179 tmp |= (1 << 27);
6180 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6181
0ff066a9
PZ
6182 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6183 tmp &= ~(0xF << 28);
6184 tmp |= (4 << 28);
6185 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6186
0ff066a9
PZ
6187 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6188 tmp &= ~(0xF << 28);
6189 tmp |= (4 << 28);
6190 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6191}
6192
2fa86a1f
PZ
6193/* Implements 3 different sequences from BSpec chapter "Display iCLK
6194 * Programming" based on the parameters passed:
6195 * - Sequence to enable CLKOUT_DP
6196 * - Sequence to enable CLKOUT_DP without spread
6197 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6198 */
6199static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6200 bool with_fdi)
f31f2d55
PZ
6201{
6202 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6203 uint32_t reg, tmp;
6204
6205 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6206 with_spread = true;
6207 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6208 with_fdi, "LP PCH doesn't have FDI\n"))
6209 with_fdi = false;
f31f2d55
PZ
6210
6211 mutex_lock(&dev_priv->dpio_lock);
6212
6213 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6214 tmp &= ~SBI_SSCCTL_DISABLE;
6215 tmp |= SBI_SSCCTL_PATHALT;
6216 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6217
6218 udelay(24);
6219
2fa86a1f
PZ
6220 if (with_spread) {
6221 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6222 tmp &= ~SBI_SSCCTL_PATHALT;
6223 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6224
2fa86a1f
PZ
6225 if (with_fdi) {
6226 lpt_reset_fdi_mphy(dev_priv);
6227 lpt_program_fdi_mphy(dev_priv);
6228 }
6229 }
dde86e2d 6230
2fa86a1f
PZ
6231 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6232 SBI_GEN0 : SBI_DBUFF0;
6233 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6234 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6235 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6236
6237 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6238}
6239
47701c3b
PZ
6240/* Sequence to disable CLKOUT_DP */
6241static void lpt_disable_clkout_dp(struct drm_device *dev)
6242{
6243 struct drm_i915_private *dev_priv = dev->dev_private;
6244 uint32_t reg, tmp;
6245
6246 mutex_lock(&dev_priv->dpio_lock);
6247
6248 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6249 SBI_GEN0 : SBI_DBUFF0;
6250 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6251 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6252 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6253
6254 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6255 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6256 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6257 tmp |= SBI_SSCCTL_PATHALT;
6258 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6259 udelay(32);
6260 }
6261 tmp |= SBI_SSCCTL_DISABLE;
6262 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6263 }
6264
6265 mutex_unlock(&dev_priv->dpio_lock);
6266}
6267
bf8fa3d3
PZ
6268static void lpt_init_pch_refclk(struct drm_device *dev)
6269{
6270 struct drm_mode_config *mode_config = &dev->mode_config;
6271 struct intel_encoder *encoder;
6272 bool has_vga = false;
6273
6274 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6275 switch (encoder->type) {
6276 case INTEL_OUTPUT_ANALOG:
6277 has_vga = true;
6278 break;
6279 }
6280 }
6281
47701c3b
PZ
6282 if (has_vga)
6283 lpt_enable_clkout_dp(dev, true, true);
6284 else
6285 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6286}
6287
dde86e2d
PZ
6288/*
6289 * Initialize reference clocks when the driver loads
6290 */
6291void intel_init_pch_refclk(struct drm_device *dev)
6292{
6293 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6294 ironlake_init_pch_refclk(dev);
6295 else if (HAS_PCH_LPT(dev))
6296 lpt_init_pch_refclk(dev);
6297}
6298
d9d444cb
JB
6299static int ironlake_get_refclk(struct drm_crtc *crtc)
6300{
6301 struct drm_device *dev = crtc->dev;
6302 struct drm_i915_private *dev_priv = dev->dev_private;
6303 struct intel_encoder *encoder;
d9d444cb
JB
6304 int num_connectors = 0;
6305 bool is_lvds = false;
6306
6c2b7c12 6307 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6308 switch (encoder->type) {
6309 case INTEL_OUTPUT_LVDS:
6310 is_lvds = true;
6311 break;
d9d444cb
JB
6312 }
6313 num_connectors++;
6314 }
6315
6316 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6317 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6318 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6319 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6320 }
6321
6322 return 120000;
6323}
6324
6ff93609 6325static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6326{
c8203565 6327 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6329 int pipe = intel_crtc->pipe;
c8203565
PZ
6330 uint32_t val;
6331
78114071 6332 val = 0;
c8203565 6333
965e0c48 6334 switch (intel_crtc->config.pipe_bpp) {
c8203565 6335 case 18:
dfd07d72 6336 val |= PIPECONF_6BPC;
c8203565
PZ
6337 break;
6338 case 24:
dfd07d72 6339 val |= PIPECONF_8BPC;
c8203565
PZ
6340 break;
6341 case 30:
dfd07d72 6342 val |= PIPECONF_10BPC;
c8203565
PZ
6343 break;
6344 case 36:
dfd07d72 6345 val |= PIPECONF_12BPC;
c8203565
PZ
6346 break;
6347 default:
cc769b62
PZ
6348 /* Case prevented by intel_choose_pipe_bpp_dither. */
6349 BUG();
c8203565
PZ
6350 }
6351
d8b32247 6352 if (intel_crtc->config.dither)
c8203565
PZ
6353 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6354
6ff93609 6355 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6356 val |= PIPECONF_INTERLACED_ILK;
6357 else
6358 val |= PIPECONF_PROGRESSIVE;
6359
50f3b016 6360 if (intel_crtc->config.limited_color_range)
3685a8f3 6361 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6362
c8203565
PZ
6363 I915_WRITE(PIPECONF(pipe), val);
6364 POSTING_READ(PIPECONF(pipe));
6365}
6366
86d3efce
VS
6367/*
6368 * Set up the pipe CSC unit.
6369 *
6370 * Currently only full range RGB to limited range RGB conversion
6371 * is supported, but eventually this should handle various
6372 * RGB<->YCbCr scenarios as well.
6373 */
50f3b016 6374static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6375{
6376 struct drm_device *dev = crtc->dev;
6377 struct drm_i915_private *dev_priv = dev->dev_private;
6378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6379 int pipe = intel_crtc->pipe;
6380 uint16_t coeff = 0x7800; /* 1.0 */
6381
6382 /*
6383 * TODO: Check what kind of values actually come out of the pipe
6384 * with these coeff/postoff values and adjust to get the best
6385 * accuracy. Perhaps we even need to take the bpc value into
6386 * consideration.
6387 */
6388
50f3b016 6389 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6390 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6391
6392 /*
6393 * GY/GU and RY/RU should be the other way around according
6394 * to BSpec, but reality doesn't agree. Just set them up in
6395 * a way that results in the correct picture.
6396 */
6397 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6398 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6399
6400 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6401 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6402
6403 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6404 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6405
6406 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6407 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6408 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6409
6410 if (INTEL_INFO(dev)->gen > 6) {
6411 uint16_t postoff = 0;
6412
50f3b016 6413 if (intel_crtc->config.limited_color_range)
32cf0cb0 6414 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6415
6416 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6417 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6418 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6419
6420 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6421 } else {
6422 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6423
50f3b016 6424 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6425 mode |= CSC_BLACK_SCREEN_OFFSET;
6426
6427 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6428 }
6429}
6430
6ff93609 6431static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6432{
756f85cf
PZ
6433 struct drm_device *dev = crtc->dev;
6434 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6436 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6437 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6438 uint32_t val;
6439
3eff4faa 6440 val = 0;
ee2b0b38 6441
756f85cf 6442 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6443 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6444
6ff93609 6445 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6446 val |= PIPECONF_INTERLACED_ILK;
6447 else
6448 val |= PIPECONF_PROGRESSIVE;
6449
702e7a56
PZ
6450 I915_WRITE(PIPECONF(cpu_transcoder), val);
6451 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6452
6453 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6454 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6455
6456 if (IS_BROADWELL(dev)) {
6457 val = 0;
6458
6459 switch (intel_crtc->config.pipe_bpp) {
6460 case 18:
6461 val |= PIPEMISC_DITHER_6_BPC;
6462 break;
6463 case 24:
6464 val |= PIPEMISC_DITHER_8_BPC;
6465 break;
6466 case 30:
6467 val |= PIPEMISC_DITHER_10_BPC;
6468 break;
6469 case 36:
6470 val |= PIPEMISC_DITHER_12_BPC;
6471 break;
6472 default:
6473 /* Case prevented by pipe_config_set_bpp. */
6474 BUG();
6475 }
6476
6477 if (intel_crtc->config.dither)
6478 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6479
6480 I915_WRITE(PIPEMISC(pipe), val);
6481 }
ee2b0b38
PZ
6482}
6483
6591c6e4 6484static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6485 intel_clock_t *clock,
6486 bool *has_reduced_clock,
6487 intel_clock_t *reduced_clock)
6488{
6489 struct drm_device *dev = crtc->dev;
6490 struct drm_i915_private *dev_priv = dev->dev_private;
6491 struct intel_encoder *intel_encoder;
6492 int refclk;
d4906093 6493 const intel_limit_t *limit;
a16af721 6494 bool ret, is_lvds = false;
79e53945 6495
6591c6e4
PZ
6496 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6497 switch (intel_encoder->type) {
79e53945
JB
6498 case INTEL_OUTPUT_LVDS:
6499 is_lvds = true;
6500 break;
79e53945
JB
6501 }
6502 }
6503
d9d444cb 6504 refclk = ironlake_get_refclk(crtc);
79e53945 6505
d4906093
ML
6506 /*
6507 * Returns a set of divisors for the desired target clock with the given
6508 * refclk, or FALSE. The returned values represent the clock equation:
6509 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6510 */
1b894b59 6511 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6512 ret = dev_priv->display.find_dpll(limit, crtc,
6513 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6514 refclk, NULL, clock);
6591c6e4
PZ
6515 if (!ret)
6516 return false;
cda4b7d3 6517
ddc9003c 6518 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6519 /*
6520 * Ensure we match the reduced clock's P to the target clock.
6521 * If the clocks don't match, we can't switch the display clock
6522 * by using the FP0/FP1. In such case we will disable the LVDS
6523 * downclock feature.
6524 */
ee9300bb
DV
6525 *has_reduced_clock =
6526 dev_priv->display.find_dpll(limit, crtc,
6527 dev_priv->lvds_downclock,
6528 refclk, clock,
6529 reduced_clock);
652c393a 6530 }
61e9653f 6531
6591c6e4
PZ
6532 return true;
6533}
6534
d4b1931c
PZ
6535int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6536{
6537 /*
6538 * Account for spread spectrum to avoid
6539 * oversubscribing the link. Max center spread
6540 * is 2.5%; use 5% for safety's sake.
6541 */
6542 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 6543 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
6544}
6545
7429e9d4 6546static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 6547{
7429e9d4 6548 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
6549}
6550
de13a2e3 6551static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 6552 u32 *fp,
9a7c7890 6553 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 6554{
de13a2e3 6555 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
6556 struct drm_device *dev = crtc->dev;
6557 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
6558 struct intel_encoder *intel_encoder;
6559 uint32_t dpll;
6cc5f341 6560 int factor, num_connectors = 0;
09ede541 6561 bool is_lvds = false, is_sdvo = false;
79e53945 6562
de13a2e3
PZ
6563 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6564 switch (intel_encoder->type) {
79e53945
JB
6565 case INTEL_OUTPUT_LVDS:
6566 is_lvds = true;
6567 break;
6568 case INTEL_OUTPUT_SDVO:
7d57382e 6569 case INTEL_OUTPUT_HDMI:
79e53945 6570 is_sdvo = true;
79e53945 6571 break;
79e53945 6572 }
43565a06 6573
c751ce4f 6574 num_connectors++;
79e53945 6575 }
79e53945 6576
c1858123 6577 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
6578 factor = 21;
6579 if (is_lvds) {
6580 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 6581 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 6582 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 6583 factor = 25;
09ede541 6584 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 6585 factor = 20;
c1858123 6586
7429e9d4 6587 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 6588 *fp |= FP_CB_TUNE;
2c07245f 6589
9a7c7890
DV
6590 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6591 *fp2 |= FP_CB_TUNE;
6592
5eddb70b 6593 dpll = 0;
2c07245f 6594
a07d6787
EA
6595 if (is_lvds)
6596 dpll |= DPLLB_MODE_LVDS;
6597 else
6598 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 6599
ef1b460d
DV
6600 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6601 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
6602
6603 if (is_sdvo)
4a33e48d 6604 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 6605 if (intel_crtc->config.has_dp_encoder)
4a33e48d 6606 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 6607
a07d6787 6608 /* compute bitmask from p1 value */
7429e9d4 6609 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 6610 /* also FPA1 */
7429e9d4 6611 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 6612
7429e9d4 6613 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
6614 case 5:
6615 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6616 break;
6617 case 7:
6618 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6619 break;
6620 case 10:
6621 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6622 break;
6623 case 14:
6624 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6625 break;
79e53945
JB
6626 }
6627
b4c09f3b 6628 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 6629 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
6630 else
6631 dpll |= PLL_REF_INPUT_DREFCLK;
6632
959e16d6 6633 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
6634}
6635
6636static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
6637 int x, int y,
6638 struct drm_framebuffer *fb)
6639{
6640 struct drm_device *dev = crtc->dev;
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6643 int pipe = intel_crtc->pipe;
6644 int plane = intel_crtc->plane;
6645 int num_connectors = 0;
6646 intel_clock_t clock, reduced_clock;
cbbab5bd 6647 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 6648 bool ok, has_reduced_clock = false;
8b47047b 6649 bool is_lvds = false;
de13a2e3 6650 struct intel_encoder *encoder;
e2b78267 6651 struct intel_shared_dpll *pll;
de13a2e3 6652 int ret;
de13a2e3
PZ
6653
6654 for_each_encoder_on_crtc(dev, crtc, encoder) {
6655 switch (encoder->type) {
6656 case INTEL_OUTPUT_LVDS:
6657 is_lvds = true;
6658 break;
de13a2e3
PZ
6659 }
6660
6661 num_connectors++;
a07d6787 6662 }
79e53945 6663
5dc5298b
PZ
6664 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6665 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 6666
ff9a6750 6667 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 6668 &has_reduced_clock, &reduced_clock);
ee9300bb 6669 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
6670 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6671 return -EINVAL;
79e53945 6672 }
f47709a9
DV
6673 /* Compat-code for transition, will disappear. */
6674 if (!intel_crtc->config.clock_set) {
6675 intel_crtc->config.dpll.n = clock.n;
6676 intel_crtc->config.dpll.m1 = clock.m1;
6677 intel_crtc->config.dpll.m2 = clock.m2;
6678 intel_crtc->config.dpll.p1 = clock.p1;
6679 intel_crtc->config.dpll.p2 = clock.p2;
6680 }
79e53945 6681
5dc5298b 6682 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 6683 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 6684 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 6685 if (has_reduced_clock)
7429e9d4 6686 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 6687
7429e9d4 6688 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
6689 &fp, &reduced_clock,
6690 has_reduced_clock ? &fp2 : NULL);
6691
959e16d6 6692 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
6693 intel_crtc->config.dpll_hw_state.fp0 = fp;
6694 if (has_reduced_clock)
6695 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6696 else
6697 intel_crtc->config.dpll_hw_state.fp1 = fp;
6698
b89a1d39 6699 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 6700 if (pll == NULL) {
84f44ce7
VS
6701 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6702 pipe_name(pipe));
4b645f14
JB
6703 return -EINVAL;
6704 }
ee7b9f93 6705 } else
e72f9fbf 6706 intel_put_shared_dpll(intel_crtc);
79e53945 6707
03afc4a2
DV
6708 if (intel_crtc->config.has_dp_encoder)
6709 intel_dp_set_m_n(intel_crtc);
79e53945 6710
d330a953 6711 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
6712 intel_crtc->lowfreq_avail = true;
6713 else
6714 intel_crtc->lowfreq_avail = false;
e2b78267 6715
8a654f3b 6716 intel_set_pipe_timings(intel_crtc);
5eddb70b 6717
ca3a0ff8 6718 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
6719 intel_cpu_transcoder_set_m_n(intel_crtc,
6720 &intel_crtc->config.fdi_m_n);
6721 }
2c07245f 6722
6ff93609 6723 ironlake_set_pipeconf(crtc);
79e53945 6724
a1f9e77e
PZ
6725 /* Set up the display plane register */
6726 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
b24e7179 6727 POSTING_READ(DSPCNTR(plane));
79e53945 6728
94352cf9 6729 ret = intel_pipe_set_base(crtc, x, y, fb);
7662c8bd 6730
1857e1da 6731 return ret;
79e53945
JB
6732}
6733
eb14cb74
VS
6734static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6735 struct intel_link_m_n *m_n)
6736{
6737 struct drm_device *dev = crtc->base.dev;
6738 struct drm_i915_private *dev_priv = dev->dev_private;
6739 enum pipe pipe = crtc->pipe;
6740
6741 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6742 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6743 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6744 & ~TU_SIZE_MASK;
6745 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6746 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6747 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6748}
6749
6750static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6751 enum transcoder transcoder,
6752 struct intel_link_m_n *m_n)
72419203
DV
6753{
6754 struct drm_device *dev = crtc->base.dev;
6755 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 6756 enum pipe pipe = crtc->pipe;
72419203 6757
eb14cb74
VS
6758 if (INTEL_INFO(dev)->gen >= 5) {
6759 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6760 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6761 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6762 & ~TU_SIZE_MASK;
6763 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6764 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6765 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6766 } else {
6767 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6768 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6769 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6770 & ~TU_SIZE_MASK;
6771 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6772 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6773 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6774 }
6775}
6776
6777void intel_dp_get_m_n(struct intel_crtc *crtc,
6778 struct intel_crtc_config *pipe_config)
6779{
6780 if (crtc->config.has_pch_encoder)
6781 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6782 else
6783 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6784 &pipe_config->dp_m_n);
6785}
72419203 6786
eb14cb74
VS
6787static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6788 struct intel_crtc_config *pipe_config)
6789{
6790 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6791 &pipe_config->fdi_m_n);
72419203
DV
6792}
6793
2fa2fe9a
DV
6794static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6795 struct intel_crtc_config *pipe_config)
6796{
6797 struct drm_device *dev = crtc->base.dev;
6798 struct drm_i915_private *dev_priv = dev->dev_private;
6799 uint32_t tmp;
6800
6801 tmp = I915_READ(PF_CTL(crtc->pipe));
6802
6803 if (tmp & PF_ENABLE) {
fd4daa9c 6804 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
6805 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6806 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
6807
6808 /* We currently do not free assignements of panel fitters on
6809 * ivb/hsw (since we don't use the higher upscaling modes which
6810 * differentiates them) so just WARN about this case for now. */
6811 if (IS_GEN7(dev)) {
6812 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6813 PF_PIPE_SEL_IVB(crtc->pipe));
6814 }
2fa2fe9a 6815 }
79e53945
JB
6816}
6817
4c6baa59
JB
6818static void ironlake_get_plane_config(struct intel_crtc *crtc,
6819 struct intel_plane_config *plane_config)
6820{
6821 struct drm_device *dev = crtc->base.dev;
6822 struct drm_i915_private *dev_priv = dev->dev_private;
6823 u32 val, base, offset;
6824 int pipe = crtc->pipe, plane = crtc->plane;
6825 int fourcc, pixel_format;
6826 int aligned_height;
6827
66e514c1
DA
6828 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6829 if (!crtc->base.primary->fb) {
4c6baa59
JB
6830 DRM_DEBUG_KMS("failed to alloc fb\n");
6831 return;
6832 }
6833
6834 val = I915_READ(DSPCNTR(plane));
6835
6836 if (INTEL_INFO(dev)->gen >= 4)
6837 if (val & DISPPLANE_TILED)
6838 plane_config->tiled = true;
6839
6840 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6841 fourcc = intel_format_to_fourcc(pixel_format);
66e514c1
DA
6842 crtc->base.primary->fb->pixel_format = fourcc;
6843 crtc->base.primary->fb->bits_per_pixel =
4c6baa59
JB
6844 drm_format_plane_cpp(fourcc, 0) * 8;
6845
6846 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6847 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
6848 offset = I915_READ(DSPOFFSET(plane));
6849 } else {
6850 if (plane_config->tiled)
6851 offset = I915_READ(DSPTILEOFF(plane));
6852 else
6853 offset = I915_READ(DSPLINOFF(plane));
6854 }
6855 plane_config->base = base;
6856
6857 val = I915_READ(PIPESRC(pipe));
66e514c1
DA
6858 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6859 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
6860
6861 val = I915_READ(DSPSTRIDE(pipe));
66e514c1 6862 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
4c6baa59 6863
66e514c1 6864 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
4c6baa59
JB
6865 plane_config->tiled);
6866
66e514c1 6867 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
4c6baa59
JB
6868 aligned_height, PAGE_SIZE);
6869
6870 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
66e514c1
DA
6871 pipe, plane, crtc->base.primary->fb->width,
6872 crtc->base.primary->fb->height,
6873 crtc->base.primary->fb->bits_per_pixel, base,
6874 crtc->base.primary->fb->pitches[0],
4c6baa59
JB
6875 plane_config->size);
6876}
6877
0e8ffe1b
DV
6878static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6879 struct intel_crtc_config *pipe_config)
6880{
6881 struct drm_device *dev = crtc->base.dev;
6882 struct drm_i915_private *dev_priv = dev->dev_private;
6883 uint32_t tmp;
6884
e143a21c 6885 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6886 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6887
0e8ffe1b
DV
6888 tmp = I915_READ(PIPECONF(crtc->pipe));
6889 if (!(tmp & PIPECONF_ENABLE))
6890 return false;
6891
42571aef
VS
6892 switch (tmp & PIPECONF_BPC_MASK) {
6893 case PIPECONF_6BPC:
6894 pipe_config->pipe_bpp = 18;
6895 break;
6896 case PIPECONF_8BPC:
6897 pipe_config->pipe_bpp = 24;
6898 break;
6899 case PIPECONF_10BPC:
6900 pipe_config->pipe_bpp = 30;
6901 break;
6902 case PIPECONF_12BPC:
6903 pipe_config->pipe_bpp = 36;
6904 break;
6905 default:
6906 break;
6907 }
6908
ab9412ba 6909 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
6910 struct intel_shared_dpll *pll;
6911
88adfff1
DV
6912 pipe_config->has_pch_encoder = true;
6913
627eb5a3
DV
6914 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6915 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6916 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
6917
6918 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 6919
c0d43d62 6920 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
6921 pipe_config->shared_dpll =
6922 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
6923 } else {
6924 tmp = I915_READ(PCH_DPLL_SEL);
6925 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6926 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6927 else
6928 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6929 }
66e985c0
DV
6930
6931 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6932
6933 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6934 &pipe_config->dpll_hw_state));
c93f54cf
DV
6935
6936 tmp = pipe_config->dpll_hw_state.dpll;
6937 pipe_config->pixel_multiplier =
6938 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6939 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
6940
6941 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
6942 } else {
6943 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
6944 }
6945
1bd1bd80
DV
6946 intel_get_pipe_timings(crtc, pipe_config);
6947
2fa2fe9a
DV
6948 ironlake_get_pfit_config(crtc, pipe_config);
6949
0e8ffe1b
DV
6950 return true;
6951}
6952
be256dc7
PZ
6953static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6954{
6955 struct drm_device *dev = dev_priv->dev;
6956 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6957 struct intel_crtc *crtc;
be256dc7
PZ
6958
6959 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
798183c5 6960 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
6961 pipe_name(crtc->pipe));
6962
6963 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6964 WARN(plls->spll_refcount, "SPLL enabled\n");
6965 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6966 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6967 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6968 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6969 "CPU PWM1 enabled\n");
6970 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6971 "CPU PWM2 enabled\n");
6972 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6973 "PCH PWM1 enabled\n");
6974 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6975 "Utility pin enabled\n");
6976 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6977
9926ada1
PZ
6978 /*
6979 * In theory we can still leave IRQs enabled, as long as only the HPD
6980 * interrupts remain enabled. We used to check for that, but since it's
6981 * gen-specific and since we only disable LCPLL after we fully disable
6982 * the interrupts, the check below should be enough.
6983 */
6984 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
be256dc7
PZ
6985}
6986
3c4c9b81
PZ
6987static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
6988{
6989 struct drm_device *dev = dev_priv->dev;
6990
6991 if (IS_HASWELL(dev)) {
6992 mutex_lock(&dev_priv->rps.hw_lock);
6993 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
6994 val))
6995 DRM_ERROR("Failed to disable D_COMP\n");
6996 mutex_unlock(&dev_priv->rps.hw_lock);
6997 } else {
6998 I915_WRITE(D_COMP, val);
6999 }
7000 POSTING_READ(D_COMP);
be256dc7
PZ
7001}
7002
7003/*
7004 * This function implements pieces of two sequences from BSpec:
7005 * - Sequence for display software to disable LCPLL
7006 * - Sequence for display software to allow package C8+
7007 * The steps implemented here are just the steps that actually touch the LCPLL
7008 * register. Callers should take care of disabling all the display engine
7009 * functions, doing the mode unset, fixing interrupts, etc.
7010 */
6ff58d53
PZ
7011static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7012 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
7013{
7014 uint32_t val;
7015
7016 assert_can_disable_lcpll(dev_priv);
7017
7018 val = I915_READ(LCPLL_CTL);
7019
7020 if (switch_to_fclk) {
7021 val |= LCPLL_CD_SOURCE_FCLK;
7022 I915_WRITE(LCPLL_CTL, val);
7023
7024 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7025 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7026 DRM_ERROR("Switching to FCLK failed\n");
7027
7028 val = I915_READ(LCPLL_CTL);
7029 }
7030
7031 val |= LCPLL_PLL_DISABLE;
7032 I915_WRITE(LCPLL_CTL, val);
7033 POSTING_READ(LCPLL_CTL);
7034
7035 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7036 DRM_ERROR("LCPLL still locked\n");
7037
7038 val = I915_READ(D_COMP);
7039 val |= D_COMP_COMP_DISABLE;
3c4c9b81 7040 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7041 ndelay(100);
7042
7043 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7044 DRM_ERROR("D_COMP RCOMP still in progress\n");
7045
7046 if (allow_power_down) {
7047 val = I915_READ(LCPLL_CTL);
7048 val |= LCPLL_POWER_DOWN_ALLOW;
7049 I915_WRITE(LCPLL_CTL, val);
7050 POSTING_READ(LCPLL_CTL);
7051 }
7052}
7053
7054/*
7055 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7056 * source.
7057 */
6ff58d53 7058static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
7059{
7060 uint32_t val;
a8a8bd54 7061 unsigned long irqflags;
be256dc7
PZ
7062
7063 val = I915_READ(LCPLL_CTL);
7064
7065 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7066 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7067 return;
7068
a8a8bd54
PZ
7069 /*
7070 * Make sure we're not on PC8 state before disabling PC8, otherwise
7071 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7072 *
7073 * The other problem is that hsw_restore_lcpll() is called as part of
7074 * the runtime PM resume sequence, so we can't just call
7075 * gen6_gt_force_wake_get() because that function calls
7076 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7077 * while we are on the resume sequence. So to solve this problem we have
7078 * to call special forcewake code that doesn't touch runtime PM and
7079 * doesn't enable the forcewake delayed work.
7080 */
7081 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7082 if (dev_priv->uncore.forcewake_count++ == 0)
7083 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7084 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
215733fa 7085
be256dc7
PZ
7086 if (val & LCPLL_POWER_DOWN_ALLOW) {
7087 val &= ~LCPLL_POWER_DOWN_ALLOW;
7088 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 7089 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
7090 }
7091
7092 val = I915_READ(D_COMP);
7093 val |= D_COMP_COMP_FORCE;
7094 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 7095 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
7096
7097 val = I915_READ(LCPLL_CTL);
7098 val &= ~LCPLL_PLL_DISABLE;
7099 I915_WRITE(LCPLL_CTL, val);
7100
7101 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7102 DRM_ERROR("LCPLL not locked yet\n");
7103
7104 if (val & LCPLL_CD_SOURCE_FCLK) {
7105 val = I915_READ(LCPLL_CTL);
7106 val &= ~LCPLL_CD_SOURCE_FCLK;
7107 I915_WRITE(LCPLL_CTL, val);
7108
7109 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7110 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7111 DRM_ERROR("Switching back to LCPLL failed\n");
7112 }
215733fa 7113
a8a8bd54
PZ
7114 /* See the big comment above. */
7115 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7116 if (--dev_priv->uncore.forcewake_count == 0)
7117 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7118 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
be256dc7
PZ
7119}
7120
765dab67
PZ
7121/*
7122 * Package states C8 and deeper are really deep PC states that can only be
7123 * reached when all the devices on the system allow it, so even if the graphics
7124 * device allows PC8+, it doesn't mean the system will actually get to these
7125 * states. Our driver only allows PC8+ when going into runtime PM.
7126 *
7127 * The requirements for PC8+ are that all the outputs are disabled, the power
7128 * well is disabled and most interrupts are disabled, and these are also
7129 * requirements for runtime PM. When these conditions are met, we manually do
7130 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7131 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7132 * hang the machine.
7133 *
7134 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7135 * the state of some registers, so when we come back from PC8+ we need to
7136 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7137 * need to take care of the registers kept by RC6. Notice that this happens even
7138 * if we don't put the device in PCI D3 state (which is what currently happens
7139 * because of the runtime PM support).
7140 *
7141 * For more, read "Display Sequences for Package C8" on the hardware
7142 * documentation.
7143 */
a14cb6fc 7144void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 7145{
c67a470b
PZ
7146 struct drm_device *dev = dev_priv->dev;
7147 uint32_t val;
7148
c67a470b
PZ
7149 DRM_DEBUG_KMS("Enabling package C8+\n");
7150
c67a470b
PZ
7151 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7152 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7153 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7154 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7155 }
7156
7157 lpt_disable_clkout_dp(dev);
c67a470b
PZ
7158 hsw_disable_lcpll(dev_priv, true, true);
7159}
7160
a14cb6fc 7161void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
7162{
7163 struct drm_device *dev = dev_priv->dev;
7164 uint32_t val;
7165
c67a470b
PZ
7166 DRM_DEBUG_KMS("Disabling package C8+\n");
7167
7168 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
7169 lpt_init_pch_refclk(dev);
7170
7171 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7172 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7173 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7174 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7175 }
7176
7177 intel_prepare_ddi(dev);
c67a470b
PZ
7178}
7179
9a952a0d
PZ
7180static void snb_modeset_global_resources(struct drm_device *dev)
7181{
7182 modeset_update_crtc_power_domains(dev);
7183}
7184
4f074129
ID
7185static void haswell_modeset_global_resources(struct drm_device *dev)
7186{
da723569 7187 modeset_update_crtc_power_domains(dev);
d6dd9eb1
DV
7188}
7189
09b4ddf9 7190static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7191 int x, int y,
7192 struct drm_framebuffer *fb)
7193{
7194 struct drm_device *dev = crtc->dev;
7195 struct drm_i915_private *dev_priv = dev->dev_private;
7196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7197 int plane = intel_crtc->plane;
09b4ddf9 7198 int ret;
09b4ddf9 7199
566b734a 7200 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7201 return -EINVAL;
566b734a 7202 intel_ddi_pll_enable(intel_crtc);
6441ab5f 7203
03afc4a2
DV
7204 if (intel_crtc->config.has_dp_encoder)
7205 intel_dp_set_m_n(intel_crtc);
09b4ddf9
PZ
7206
7207 intel_crtc->lowfreq_avail = false;
09b4ddf9 7208
8a654f3b 7209 intel_set_pipe_timings(intel_crtc);
09b4ddf9 7210
ca3a0ff8 7211 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
7212 intel_cpu_transcoder_set_m_n(intel_crtc,
7213 &intel_crtc->config.fdi_m_n);
7214 }
09b4ddf9 7215
6ff93609 7216 haswell_set_pipeconf(crtc);
09b4ddf9 7217
50f3b016 7218 intel_set_pipe_csc(crtc);
86d3efce 7219
09b4ddf9 7220 /* Set up the display plane register */
86d3efce 7221 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
09b4ddf9
PZ
7222 POSTING_READ(DSPCNTR(plane));
7223
7224 ret = intel_pipe_set_base(crtc, x, y, fb);
7225
1f803ee5 7226 return ret;
79e53945
JB
7227}
7228
0e8ffe1b
DV
7229static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7230 struct intel_crtc_config *pipe_config)
7231{
7232 struct drm_device *dev = crtc->base.dev;
7233 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7234 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7235 uint32_t tmp;
7236
b5482bd0
ID
7237 if (!intel_display_power_enabled(dev_priv,
7238 POWER_DOMAIN_PIPE(crtc->pipe)))
7239 return false;
7240
e143a21c 7241 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7242 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7243
eccb140b
DV
7244 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7245 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7246 enum pipe trans_edp_pipe;
7247 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7248 default:
7249 WARN(1, "unknown pipe linked to edp transcoder\n");
7250 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7251 case TRANS_DDI_EDP_INPUT_A_ON:
7252 trans_edp_pipe = PIPE_A;
7253 break;
7254 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7255 trans_edp_pipe = PIPE_B;
7256 break;
7257 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7258 trans_edp_pipe = PIPE_C;
7259 break;
7260 }
7261
7262 if (trans_edp_pipe == crtc->pipe)
7263 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7264 }
7265
da7e29bd 7266 if (!intel_display_power_enabled(dev_priv,
eccb140b 7267 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7268 return false;
7269
eccb140b 7270 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7271 if (!(tmp & PIPECONF_ENABLE))
7272 return false;
7273
88adfff1 7274 /*
f196e6be 7275 * Haswell has only FDI/PCH transcoder A. It is which is connected to
88adfff1
DV
7276 * DDI E. So just check whether this pipe is wired to DDI E and whether
7277 * the PCH transcoder is on.
7278 */
eccb140b 7279 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
88adfff1 7280 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
ab9412ba 7281 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
88adfff1
DV
7282 pipe_config->has_pch_encoder = true;
7283
627eb5a3
DV
7284 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7285 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7286 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7287
7288 ironlake_get_fdi_m_n_config(crtc, pipe_config);
627eb5a3
DV
7289 }
7290
1bd1bd80
DV
7291 intel_get_pipe_timings(crtc, pipe_config);
7292
2fa2fe9a 7293 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7294 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7295 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7296
e59150dc
JB
7297 if (IS_HASWELL(dev))
7298 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7299 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7300
6c49f241
DV
7301 pipe_config->pixel_multiplier = 1;
7302
0e8ffe1b
DV
7303 return true;
7304}
7305
f564048e 7306static int intel_crtc_mode_set(struct drm_crtc *crtc,
f564048e 7307 int x, int y,
94352cf9 7308 struct drm_framebuffer *fb)
f564048e
EA
7309{
7310 struct drm_device *dev = crtc->dev;
7311 struct drm_i915_private *dev_priv = dev->dev_private;
9256aa19 7312 struct intel_encoder *encoder;
0b701d27 7313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b8cecdf5 7314 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
0b701d27 7315 int pipe = intel_crtc->pipe;
f564048e
EA
7316 int ret;
7317
0b701d27 7318 drm_vblank_pre_modeset(dev, pipe);
7662c8bd 7319
b8cecdf5
DV
7320 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7321
79e53945 7322 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 7323
9256aa19
DV
7324 if (ret != 0)
7325 return ret;
7326
7327 for_each_encoder_on_crtc(dev, crtc, encoder) {
7328 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7329 encoder->base.base.id,
7330 drm_get_encoder_name(&encoder->base),
7331 mode->base.id, mode->name);
0d56bf0b
DV
7332
7333 if (encoder->mode_set)
7334 encoder->mode_set(encoder);
9256aa19
DV
7335 }
7336
7337 return 0;
79e53945
JB
7338}
7339
1a91510d
JN
7340static struct {
7341 int clock;
7342 u32 config;
7343} hdmi_audio_clock[] = {
7344 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7345 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7346 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7347 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7348 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7349 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7350 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7351 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7352 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7353 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7354};
7355
7356/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7357static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7358{
7359 int i;
7360
7361 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7362 if (mode->clock == hdmi_audio_clock[i].clock)
7363 break;
7364 }
7365
7366 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7367 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7368 i = 1;
7369 }
7370
7371 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7372 hdmi_audio_clock[i].clock,
7373 hdmi_audio_clock[i].config);
7374
7375 return hdmi_audio_clock[i].config;
7376}
7377
3a9627f4
WF
7378static bool intel_eld_uptodate(struct drm_connector *connector,
7379 int reg_eldv, uint32_t bits_eldv,
7380 int reg_elda, uint32_t bits_elda,
7381 int reg_edid)
7382{
7383 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7384 uint8_t *eld = connector->eld;
7385 uint32_t i;
7386
7387 i = I915_READ(reg_eldv);
7388 i &= bits_eldv;
7389
7390 if (!eld[0])
7391 return !i;
7392
7393 if (!i)
7394 return false;
7395
7396 i = I915_READ(reg_elda);
7397 i &= ~bits_elda;
7398 I915_WRITE(reg_elda, i);
7399
7400 for (i = 0; i < eld[2]; i++)
7401 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7402 return false;
7403
7404 return true;
7405}
7406
e0dac65e 7407static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7408 struct drm_crtc *crtc,
7409 struct drm_display_mode *mode)
e0dac65e
WF
7410{
7411 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7412 uint8_t *eld = connector->eld;
7413 uint32_t eldv;
7414 uint32_t len;
7415 uint32_t i;
7416
7417 i = I915_READ(G4X_AUD_VID_DID);
7418
7419 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7420 eldv = G4X_ELDV_DEVCL_DEVBLC;
7421 else
7422 eldv = G4X_ELDV_DEVCTG;
7423
3a9627f4
WF
7424 if (intel_eld_uptodate(connector,
7425 G4X_AUD_CNTL_ST, eldv,
7426 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7427 G4X_HDMIW_HDMIEDID))
7428 return;
7429
e0dac65e
WF
7430 i = I915_READ(G4X_AUD_CNTL_ST);
7431 i &= ~(eldv | G4X_ELD_ADDR);
7432 len = (i >> 9) & 0x1f; /* ELD buffer size */
7433 I915_WRITE(G4X_AUD_CNTL_ST, i);
7434
7435 if (!eld[0])
7436 return;
7437
7438 len = min_t(uint8_t, eld[2], len);
7439 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7440 for (i = 0; i < len; i++)
7441 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7442
7443 i = I915_READ(G4X_AUD_CNTL_ST);
7444 i |= eldv;
7445 I915_WRITE(G4X_AUD_CNTL_ST, i);
7446}
7447
83358c85 7448static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7449 struct drm_crtc *crtc,
7450 struct drm_display_mode *mode)
83358c85
WX
7451{
7452 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7453 uint8_t *eld = connector->eld;
7b9f35a6 7454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83358c85
WX
7455 uint32_t eldv;
7456 uint32_t i;
7457 int len;
7458 int pipe = to_intel_crtc(crtc)->pipe;
7459 int tmp;
7460
7461 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7462 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7463 int aud_config = HSW_AUD_CFG(pipe);
7464 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7465
83358c85
WX
7466 /* Audio output enable */
7467 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7468 tmp = I915_READ(aud_cntrl_st2);
7469 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7470 I915_WRITE(aud_cntrl_st2, tmp);
c7905792 7471 POSTING_READ(aud_cntrl_st2);
83358c85 7472
c7905792 7473 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
83358c85
WX
7474
7475 /* Set ELD valid state */
7476 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7477 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7478 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7479 I915_WRITE(aud_cntrl_st2, tmp);
7480 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7481 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7482
7483 /* Enable HDMI mode */
7484 tmp = I915_READ(aud_config);
7e7cb34f 7485 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7486 /* clear N_programing_enable and N_value_index */
7487 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7488 I915_WRITE(aud_config, tmp);
7489
7490 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7491
7492 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7b9f35a6 7493 intel_crtc->eld_vld = true;
83358c85
WX
7494
7495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7496 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7497 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7498 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7499 } else {
7500 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7501 }
83358c85
WX
7502
7503 if (intel_eld_uptodate(connector,
7504 aud_cntrl_st2, eldv,
7505 aud_cntl_st, IBX_ELD_ADDRESS,
7506 hdmiw_hdmiedid))
7507 return;
7508
7509 i = I915_READ(aud_cntrl_st2);
7510 i &= ~eldv;
7511 I915_WRITE(aud_cntrl_st2, i);
7512
7513 if (!eld[0])
7514 return;
7515
7516 i = I915_READ(aud_cntl_st);
7517 i &= ~IBX_ELD_ADDRESS;
7518 I915_WRITE(aud_cntl_st, i);
7519 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7520 DRM_DEBUG_DRIVER("port num:%d\n", i);
7521
7522 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7523 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7524 for (i = 0; i < len; i++)
7525 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7526
7527 i = I915_READ(aud_cntrl_st2);
7528 i |= eldv;
7529 I915_WRITE(aud_cntrl_st2, i);
7530
7531}
7532
e0dac65e 7533static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7534 struct drm_crtc *crtc,
7535 struct drm_display_mode *mode)
e0dac65e
WF
7536{
7537 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7538 uint8_t *eld = connector->eld;
7539 uint32_t eldv;
7540 uint32_t i;
7541 int len;
7542 int hdmiw_hdmiedid;
b6daa025 7543 int aud_config;
e0dac65e
WF
7544 int aud_cntl_st;
7545 int aud_cntrl_st2;
9b138a83 7546 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7547
b3f33cbf 7548 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7549 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7550 aud_config = IBX_AUD_CFG(pipe);
7551 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 7552 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
7553 } else if (IS_VALLEYVIEW(connector->dev)) {
7554 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7555 aud_config = VLV_AUD_CFG(pipe);
7556 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7557 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 7558 } else {
9b138a83
WX
7559 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7560 aud_config = CPT_AUD_CFG(pipe);
7561 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 7562 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
7563 }
7564
9b138a83 7565 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 7566
9ca2fe73
ML
7567 if (IS_VALLEYVIEW(connector->dev)) {
7568 struct intel_encoder *intel_encoder;
7569 struct intel_digital_port *intel_dig_port;
7570
7571 intel_encoder = intel_attached_encoder(connector);
7572 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7573 i = intel_dig_port->port;
7574 } else {
7575 i = I915_READ(aud_cntl_st);
7576 i = (i >> 29) & DIP_PORT_SEL_MASK;
7577 /* DIP_Port_Select, 0x1 = PortB */
7578 }
7579
e0dac65e
WF
7580 if (!i) {
7581 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7582 /* operate blindly on all ports */
1202b4c6
WF
7583 eldv = IBX_ELD_VALIDB;
7584 eldv |= IBX_ELD_VALIDB << 4;
7585 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 7586 } else {
2582a850 7587 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 7588 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
7589 }
7590
3a9627f4
WF
7591 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7592 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7593 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 7594 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7595 } else {
7596 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7597 }
e0dac65e 7598
3a9627f4
WF
7599 if (intel_eld_uptodate(connector,
7600 aud_cntrl_st2, eldv,
7601 aud_cntl_st, IBX_ELD_ADDRESS,
7602 hdmiw_hdmiedid))
7603 return;
7604
e0dac65e
WF
7605 i = I915_READ(aud_cntrl_st2);
7606 i &= ~eldv;
7607 I915_WRITE(aud_cntrl_st2, i);
7608
7609 if (!eld[0])
7610 return;
7611
e0dac65e 7612 i = I915_READ(aud_cntl_st);
1202b4c6 7613 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
7614 I915_WRITE(aud_cntl_st, i);
7615
7616 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7617 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7618 for (i = 0; i < len; i++)
7619 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7620
7621 i = I915_READ(aud_cntrl_st2);
7622 i |= eldv;
7623 I915_WRITE(aud_cntrl_st2, i);
7624}
7625
7626void intel_write_eld(struct drm_encoder *encoder,
7627 struct drm_display_mode *mode)
7628{
7629 struct drm_crtc *crtc = encoder->crtc;
7630 struct drm_connector *connector;
7631 struct drm_device *dev = encoder->dev;
7632 struct drm_i915_private *dev_priv = dev->dev_private;
7633
7634 connector = drm_select_eld(encoder, mode);
7635 if (!connector)
7636 return;
7637
7638 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7639 connector->base.id,
7640 drm_get_connector_name(connector),
7641 connector->encoder->base.id,
7642 drm_get_encoder_name(connector->encoder));
7643
7644 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7645
7646 if (dev_priv->display.write_eld)
34427052 7647 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
7648}
7649
560b85bb
CW
7650static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7651{
7652 struct drm_device *dev = crtc->dev;
7653 struct drm_i915_private *dev_priv = dev->dev_private;
7654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7655 bool visible = base != 0;
7656 u32 cntl;
7657
7658 if (intel_crtc->cursor_visible == visible)
7659 return;
7660
9db4a9c7 7661 cntl = I915_READ(_CURACNTR);
560b85bb
CW
7662 if (visible) {
7663 /* On these chipsets we can only modify the base whilst
7664 * the cursor is disabled.
7665 */
9db4a9c7 7666 I915_WRITE(_CURABASE, base);
560b85bb
CW
7667
7668 cntl &= ~(CURSOR_FORMAT_MASK);
7669 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7670 cntl |= CURSOR_ENABLE |
7671 CURSOR_GAMMA_ENABLE |
7672 CURSOR_FORMAT_ARGB;
7673 } else
7674 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
9db4a9c7 7675 I915_WRITE(_CURACNTR, cntl);
560b85bb
CW
7676
7677 intel_crtc->cursor_visible = visible;
7678}
7679
7680static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7681{
7682 struct drm_device *dev = crtc->dev;
7683 struct drm_i915_private *dev_priv = dev->dev_private;
7684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7685 int pipe = intel_crtc->pipe;
7686 bool visible = base != 0;
7687
7688 if (intel_crtc->cursor_visible != visible) {
4726e0b0 7689 int16_t width = intel_crtc->cursor_width;
548f245b 7690 uint32_t cntl = I915_READ(CURCNTR(pipe));
560b85bb
CW
7691 if (base) {
7692 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4726e0b0
SK
7693 cntl |= MCURSOR_GAMMA_ENABLE;
7694
7695 switch (width) {
7696 case 64:
7697 cntl |= CURSOR_MODE_64_ARGB_AX;
7698 break;
7699 case 128:
7700 cntl |= CURSOR_MODE_128_ARGB_AX;
7701 break;
7702 case 256:
7703 cntl |= CURSOR_MODE_256_ARGB_AX;
7704 break;
7705 default:
7706 WARN_ON(1);
7707 return;
7708 }
560b85bb
CW
7709 cntl |= pipe << 28; /* Connect to correct pipe */
7710 } else {
7711 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7712 cntl |= CURSOR_MODE_DISABLE;
7713 }
9db4a9c7 7714 I915_WRITE(CURCNTR(pipe), cntl);
560b85bb
CW
7715
7716 intel_crtc->cursor_visible = visible;
7717 }
7718 /* and commit changes on next vblank */
b2ea8ef5 7719 POSTING_READ(CURCNTR(pipe));
9db4a9c7 7720 I915_WRITE(CURBASE(pipe), base);
b2ea8ef5 7721 POSTING_READ(CURBASE(pipe));
560b85bb
CW
7722}
7723
65a21cd6
JB
7724static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7725{
7726 struct drm_device *dev = crtc->dev;
7727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7729 int pipe = intel_crtc->pipe;
7730 bool visible = base != 0;
7731
7732 if (intel_crtc->cursor_visible != visible) {
4726e0b0 7733 int16_t width = intel_crtc->cursor_width;
65a21cd6
JB
7734 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7735 if (base) {
7736 cntl &= ~CURSOR_MODE;
4726e0b0
SK
7737 cntl |= MCURSOR_GAMMA_ENABLE;
7738 switch (width) {
7739 case 64:
7740 cntl |= CURSOR_MODE_64_ARGB_AX;
7741 break;
7742 case 128:
7743 cntl |= CURSOR_MODE_128_ARGB_AX;
7744 break;
7745 case 256:
7746 cntl |= CURSOR_MODE_256_ARGB_AX;
7747 break;
7748 default:
7749 WARN_ON(1);
7750 return;
7751 }
65a21cd6
JB
7752 } else {
7753 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7754 cntl |= CURSOR_MODE_DISABLE;
7755 }
6bbfa1c5 7756 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
86d3efce 7757 cntl |= CURSOR_PIPE_CSC_ENABLE;
1f5d76db
PZ
7758 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7759 }
65a21cd6
JB
7760 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7761
7762 intel_crtc->cursor_visible = visible;
7763 }
7764 /* and commit changes on next vblank */
b2ea8ef5 7765 POSTING_READ(CURCNTR_IVB(pipe));
65a21cd6 7766 I915_WRITE(CURBASE_IVB(pipe), base);
b2ea8ef5 7767 POSTING_READ(CURBASE_IVB(pipe));
65a21cd6
JB
7768}
7769
cda4b7d3 7770/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
7771static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7772 bool on)
cda4b7d3
CW
7773{
7774 struct drm_device *dev = crtc->dev;
7775 struct drm_i915_private *dev_priv = dev->dev_private;
7776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7777 int pipe = intel_crtc->pipe;
7778 int x = intel_crtc->cursor_x;
7779 int y = intel_crtc->cursor_y;
d6e4db15 7780 u32 base = 0, pos = 0;
cda4b7d3
CW
7781 bool visible;
7782
d6e4db15 7783 if (on)
cda4b7d3 7784 base = intel_crtc->cursor_addr;
cda4b7d3 7785
d6e4db15
VS
7786 if (x >= intel_crtc->config.pipe_src_w)
7787 base = 0;
7788
7789 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
7790 base = 0;
7791
7792 if (x < 0) {
efc9064e 7793 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
7794 base = 0;
7795
7796 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7797 x = -x;
7798 }
7799 pos |= x << CURSOR_X_SHIFT;
7800
7801 if (y < 0) {
efc9064e 7802 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
7803 base = 0;
7804
7805 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7806 y = -y;
7807 }
7808 pos |= y << CURSOR_Y_SHIFT;
7809
7810 visible = base != 0;
560b85bb 7811 if (!visible && !intel_crtc->cursor_visible)
cda4b7d3
CW
7812 return;
7813
b3dc685e 7814 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
65a21cd6
JB
7815 I915_WRITE(CURPOS_IVB(pipe), pos);
7816 ivb_update_cursor(crtc, base);
7817 } else {
7818 I915_WRITE(CURPOS(pipe), pos);
7819 if (IS_845G(dev) || IS_I865G(dev))
7820 i845_update_cursor(crtc, base);
7821 else
7822 i9xx_update_cursor(crtc, base);
7823 }
cda4b7d3
CW
7824}
7825
79e53945 7826static int intel_crtc_cursor_set(struct drm_crtc *crtc,
05394f39 7827 struct drm_file *file,
79e53945
JB
7828 uint32_t handle,
7829 uint32_t width, uint32_t height)
7830{
7831 struct drm_device *dev = crtc->dev;
7832 struct drm_i915_private *dev_priv = dev->dev_private;
7833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
05394f39 7834 struct drm_i915_gem_object *obj;
64f962e3 7835 unsigned old_width;
cda4b7d3 7836 uint32_t addr;
3f8bc370 7837 int ret;
79e53945 7838
79e53945
JB
7839 /* if we want to turn off the cursor ignore width and height */
7840 if (!handle) {
28c97730 7841 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 7842 addr = 0;
05394f39 7843 obj = NULL;
5004417d 7844 mutex_lock(&dev->struct_mutex);
3f8bc370 7845 goto finish;
79e53945
JB
7846 }
7847
4726e0b0
SK
7848 /* Check for which cursor types we support */
7849 if (!((width == 64 && height == 64) ||
7850 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
7851 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
7852 DRM_DEBUG("Cursor dimension not supported\n");
79e53945
JB
7853 return -EINVAL;
7854 }
7855
05394f39 7856 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
c8725226 7857 if (&obj->base == NULL)
79e53945
JB
7858 return -ENOENT;
7859
05394f39 7860 if (obj->base.size < width * height * 4) {
3b25b31f 7861 DRM_DEBUG_KMS("buffer is to small\n");
34b8686e
DA
7862 ret = -ENOMEM;
7863 goto fail;
79e53945
JB
7864 }
7865
71acb5eb 7866 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 7867 mutex_lock(&dev->struct_mutex);
3d13ef2e 7868 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
7869 unsigned alignment;
7870
d9e86c0e 7871 if (obj->tiling_mode) {
3b25b31f 7872 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
7873 ret = -EINVAL;
7874 goto fail_locked;
7875 }
7876
693db184
CW
7877 /* Note that the w/a also requires 2 PTE of padding following
7878 * the bo. We currently fill all unused PTE with the shadow
7879 * page and so we should always have valid PTE following the
7880 * cursor preventing the VT-d warning.
7881 */
7882 alignment = 0;
7883 if (need_vtd_wa(dev))
7884 alignment = 64*1024;
7885
7886 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 7887 if (ret) {
3b25b31f 7888 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 7889 goto fail_locked;
e7b526bb
CW
7890 }
7891
d9e86c0e
CW
7892 ret = i915_gem_object_put_fence(obj);
7893 if (ret) {
3b25b31f 7894 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
7895 goto fail_unpin;
7896 }
7897
f343c5f6 7898 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 7899 } else {
6eeefaf3 7900 int align = IS_I830(dev) ? 16 * 1024 : 256;
05394f39 7901 ret = i915_gem_attach_phys_object(dev, obj,
6eeefaf3
CW
7902 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7903 align);
71acb5eb 7904 if (ret) {
3b25b31f 7905 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 7906 goto fail_locked;
71acb5eb 7907 }
05394f39 7908 addr = obj->phys_obj->handle->busaddr;
3f8bc370
KH
7909 }
7910
a6c45cf0 7911 if (IS_GEN2(dev))
14b60391
JB
7912 I915_WRITE(CURSIZE, (height << 12) | width);
7913
3f8bc370 7914 finish:
3f8bc370 7915 if (intel_crtc->cursor_bo) {
3d13ef2e 7916 if (INTEL_INFO(dev)->cursor_needs_physical) {
05394f39 7917 if (intel_crtc->cursor_bo != obj)
71acb5eb
DA
7918 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7919 } else
cc98b413 7920 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
05394f39 7921 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
3f8bc370 7922 }
80824003 7923
7f9872e0 7924 mutex_unlock(&dev->struct_mutex);
3f8bc370 7925
64f962e3
CW
7926 old_width = intel_crtc->cursor_width;
7927
3f8bc370 7928 intel_crtc->cursor_addr = addr;
05394f39 7929 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
7930 intel_crtc->cursor_width = width;
7931 intel_crtc->cursor_height = height;
7932
64f962e3
CW
7933 if (intel_crtc->active) {
7934 if (old_width != width)
7935 intel_update_watermarks(crtc);
f2f5f771 7936 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
64f962e3 7937 }
3f8bc370 7938
79e53945 7939 return 0;
e7b526bb 7940fail_unpin:
cc98b413 7941 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 7942fail_locked:
34b8686e 7943 mutex_unlock(&dev->struct_mutex);
bc9025bd 7944fail:
05394f39 7945 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 7946 return ret;
79e53945
JB
7947}
7948
7949static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7950{
79e53945 7951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7952
92e76c8c
VS
7953 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7954 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
652c393a 7955
f2f5f771
VS
7956 if (intel_crtc->active)
7957 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
79e53945
JB
7958
7959 return 0;
b8c00ac5
DA
7960}
7961
79e53945 7962static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 7963 u16 *blue, uint32_t start, uint32_t size)
79e53945 7964{
7203425a 7965 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 7966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7967
7203425a 7968 for (i = start; i < end; i++) {
79e53945
JB
7969 intel_crtc->lut_r[i] = red[i] >> 8;
7970 intel_crtc->lut_g[i] = green[i] >> 8;
7971 intel_crtc->lut_b[i] = blue[i] >> 8;
7972 }
7973
7974 intel_crtc_load_lut(crtc);
7975}
7976
79e53945
JB
7977/* VESA 640x480x72Hz mode to set on the pipe */
7978static struct drm_display_mode load_detect_mode = {
7979 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7980 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7981};
7982
a8bb6818
DV
7983struct drm_framebuffer *
7984__intel_framebuffer_create(struct drm_device *dev,
7985 struct drm_mode_fb_cmd2 *mode_cmd,
7986 struct drm_i915_gem_object *obj)
d2dff872
CW
7987{
7988 struct intel_framebuffer *intel_fb;
7989 int ret;
7990
7991 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7992 if (!intel_fb) {
7993 drm_gem_object_unreference_unlocked(&obj->base);
7994 return ERR_PTR(-ENOMEM);
7995 }
7996
7997 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
7998 if (ret)
7999 goto err;
d2dff872
CW
8000
8001 return &intel_fb->base;
dd4916c5
DV
8002err:
8003 drm_gem_object_unreference_unlocked(&obj->base);
8004 kfree(intel_fb);
8005
8006 return ERR_PTR(ret);
d2dff872
CW
8007}
8008
b5ea642a 8009static struct drm_framebuffer *
a8bb6818
DV
8010intel_framebuffer_create(struct drm_device *dev,
8011 struct drm_mode_fb_cmd2 *mode_cmd,
8012 struct drm_i915_gem_object *obj)
8013{
8014 struct drm_framebuffer *fb;
8015 int ret;
8016
8017 ret = i915_mutex_lock_interruptible(dev);
8018 if (ret)
8019 return ERR_PTR(ret);
8020 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8021 mutex_unlock(&dev->struct_mutex);
8022
8023 return fb;
8024}
8025
d2dff872
CW
8026static u32
8027intel_framebuffer_pitch_for_width(int width, int bpp)
8028{
8029 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8030 return ALIGN(pitch, 64);
8031}
8032
8033static u32
8034intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8035{
8036 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8037 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8038}
8039
8040static struct drm_framebuffer *
8041intel_framebuffer_create_for_mode(struct drm_device *dev,
8042 struct drm_display_mode *mode,
8043 int depth, int bpp)
8044{
8045 struct drm_i915_gem_object *obj;
0fed39bd 8046 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
8047
8048 obj = i915_gem_alloc_object(dev,
8049 intel_framebuffer_size_for_mode(mode, bpp));
8050 if (obj == NULL)
8051 return ERR_PTR(-ENOMEM);
8052
8053 mode_cmd.width = mode->hdisplay;
8054 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8055 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8056 bpp);
5ca0c34a 8057 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8058
8059 return intel_framebuffer_create(dev, &mode_cmd, obj);
8060}
8061
8062static struct drm_framebuffer *
8063mode_fits_in_fbdev(struct drm_device *dev,
8064 struct drm_display_mode *mode)
8065{
4520f53a 8066#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8067 struct drm_i915_private *dev_priv = dev->dev_private;
8068 struct drm_i915_gem_object *obj;
8069 struct drm_framebuffer *fb;
8070
4c0e5528 8071 if (!dev_priv->fbdev)
d2dff872
CW
8072 return NULL;
8073
4c0e5528 8074 if (!dev_priv->fbdev->fb)
d2dff872
CW
8075 return NULL;
8076
4c0e5528
DV
8077 obj = dev_priv->fbdev->fb->obj;
8078 BUG_ON(!obj);
8079
8bcd4553 8080 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8081 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8082 fb->bits_per_pixel))
d2dff872
CW
8083 return NULL;
8084
01f2c773 8085 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8086 return NULL;
8087
8088 return fb;
4520f53a
DV
8089#else
8090 return NULL;
8091#endif
d2dff872
CW
8092}
8093
d2434ab7 8094bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8095 struct drm_display_mode *mode,
8261b191 8096 struct intel_load_detect_pipe *old)
79e53945
JB
8097{
8098 struct intel_crtc *intel_crtc;
d2434ab7
DV
8099 struct intel_encoder *intel_encoder =
8100 intel_attached_encoder(connector);
79e53945 8101 struct drm_crtc *possible_crtc;
4ef69c7a 8102 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8103 struct drm_crtc *crtc = NULL;
8104 struct drm_device *dev = encoder->dev;
94352cf9 8105 struct drm_framebuffer *fb;
79e53945
JB
8106 int i = -1;
8107
d2dff872
CW
8108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8109 connector->base.id, drm_get_connector_name(connector),
8110 encoder->base.id, drm_get_encoder_name(encoder));
8111
79e53945
JB
8112 /*
8113 * Algorithm gets a little messy:
7a5e4805 8114 *
79e53945
JB
8115 * - if the connector already has an assigned crtc, use it (but make
8116 * sure it's on first)
7a5e4805 8117 *
79e53945
JB
8118 * - try to find the first unused crtc that can drive this connector,
8119 * and use that if we find one
79e53945
JB
8120 */
8121
8122 /* See if we already have a CRTC for this connector */
8123 if (encoder->crtc) {
8124 crtc = encoder->crtc;
8261b191 8125
7b24056b
DV
8126 mutex_lock(&crtc->mutex);
8127
24218aac 8128 old->dpms_mode = connector->dpms;
8261b191
CW
8129 old->load_detect_temp = false;
8130
8131 /* Make sure the crtc and connector are running */
24218aac
DV
8132 if (connector->dpms != DRM_MODE_DPMS_ON)
8133 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8134
7173188d 8135 return true;
79e53945
JB
8136 }
8137
8138 /* Find an unused one (if possible) */
8139 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
8140 i++;
8141 if (!(encoder->possible_crtcs & (1 << i)))
8142 continue;
8143 if (!possible_crtc->enabled) {
8144 crtc = possible_crtc;
8145 break;
8146 }
79e53945
JB
8147 }
8148
8149 /*
8150 * If we didn't find an unused CRTC, don't use any.
8151 */
8152 if (!crtc) {
7173188d
CW
8153 DRM_DEBUG_KMS("no pipe available for load-detect\n");
8154 return false;
79e53945
JB
8155 }
8156
7b24056b 8157 mutex_lock(&crtc->mutex);
fc303101
DV
8158 intel_encoder->new_crtc = to_intel_crtc(crtc);
8159 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8160
8161 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8162 intel_crtc->new_enabled = true;
8163 intel_crtc->new_config = &intel_crtc->config;
24218aac 8164 old->dpms_mode = connector->dpms;
8261b191 8165 old->load_detect_temp = true;
d2dff872 8166 old->release_fb = NULL;
79e53945 8167
6492711d
CW
8168 if (!mode)
8169 mode = &load_detect_mode;
79e53945 8170
d2dff872
CW
8171 /* We need a framebuffer large enough to accommodate all accesses
8172 * that the plane may generate whilst we perform load detection.
8173 * We can not rely on the fbcon either being present (we get called
8174 * during its initialisation to detect all boot displays, or it may
8175 * not even exist) or that it is large enough to satisfy the
8176 * requested mode.
8177 */
94352cf9
DV
8178 fb = mode_fits_in_fbdev(dev, mode);
8179 if (fb == NULL) {
d2dff872 8180 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8181 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8182 old->release_fb = fb;
d2dff872
CW
8183 } else
8184 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8185 if (IS_ERR(fb)) {
d2dff872 8186 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8187 goto fail;
79e53945 8188 }
79e53945 8189
c0c36b94 8190 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8191 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8192 if (old->release_fb)
8193 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8194 goto fail;
79e53945 8195 }
7173188d 8196
79e53945 8197 /* let the connector get through one full cycle before testing */
9d0498a2 8198 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8199 return true;
412b61d8
VS
8200
8201 fail:
8202 intel_crtc->new_enabled = crtc->enabled;
8203 if (intel_crtc->new_enabled)
8204 intel_crtc->new_config = &intel_crtc->config;
8205 else
8206 intel_crtc->new_config = NULL;
8207 mutex_unlock(&crtc->mutex);
8208 return false;
79e53945
JB
8209}
8210
d2434ab7 8211void intel_release_load_detect_pipe(struct drm_connector *connector,
8261b191 8212 struct intel_load_detect_pipe *old)
79e53945 8213{
d2434ab7
DV
8214 struct intel_encoder *intel_encoder =
8215 intel_attached_encoder(connector);
4ef69c7a 8216 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8217 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8219
d2dff872
CW
8220 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8221 connector->base.id, drm_get_connector_name(connector),
8222 encoder->base.id, drm_get_encoder_name(encoder));
8223
8261b191 8224 if (old->load_detect_temp) {
fc303101
DV
8225 to_intel_connector(connector)->new_encoder = NULL;
8226 intel_encoder->new_crtc = NULL;
412b61d8
VS
8227 intel_crtc->new_enabled = false;
8228 intel_crtc->new_config = NULL;
fc303101 8229 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8230
36206361
DV
8231 if (old->release_fb) {
8232 drm_framebuffer_unregister_private(old->release_fb);
8233 drm_framebuffer_unreference(old->release_fb);
8234 }
d2dff872 8235
67c96400 8236 mutex_unlock(&crtc->mutex);
0622a53c 8237 return;
79e53945
JB
8238 }
8239
c751ce4f 8240 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8241 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8242 connector->funcs->dpms(connector, old->dpms_mode);
7b24056b
DV
8243
8244 mutex_unlock(&crtc->mutex);
79e53945
JB
8245}
8246
da4a1efa
VS
8247static int i9xx_pll_refclk(struct drm_device *dev,
8248 const struct intel_crtc_config *pipe_config)
8249{
8250 struct drm_i915_private *dev_priv = dev->dev_private;
8251 u32 dpll = pipe_config->dpll_hw_state.dpll;
8252
8253 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8254 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8255 else if (HAS_PCH_SPLIT(dev))
8256 return 120000;
8257 else if (!IS_GEN2(dev))
8258 return 96000;
8259 else
8260 return 48000;
8261}
8262
79e53945 8263/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8264static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8265 struct intel_crtc_config *pipe_config)
79e53945 8266{
f1f644dc 8267 struct drm_device *dev = crtc->base.dev;
79e53945 8268 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8269 int pipe = pipe_config->cpu_transcoder;
293623f7 8270 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8271 u32 fp;
8272 intel_clock_t clock;
da4a1efa 8273 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8274
8275 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8276 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8277 else
293623f7 8278 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8279
8280 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8281 if (IS_PINEVIEW(dev)) {
8282 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8283 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8284 } else {
8285 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8286 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8287 }
8288
a6c45cf0 8289 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8290 if (IS_PINEVIEW(dev))
8291 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8292 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8293 else
8294 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8295 DPLL_FPA01_P1_POST_DIV_SHIFT);
8296
8297 switch (dpll & DPLL_MODE_MASK) {
8298 case DPLLB_MODE_DAC_SERIAL:
8299 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8300 5 : 10;
8301 break;
8302 case DPLLB_MODE_LVDS:
8303 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8304 7 : 14;
8305 break;
8306 default:
28c97730 8307 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8308 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8309 return;
79e53945
JB
8310 }
8311
ac58c3f0 8312 if (IS_PINEVIEW(dev))
da4a1efa 8313 pineview_clock(refclk, &clock);
ac58c3f0 8314 else
da4a1efa 8315 i9xx_clock(refclk, &clock);
79e53945 8316 } else {
0fb58223 8317 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8318 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8319
8320 if (is_lvds) {
8321 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8322 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8323
8324 if (lvds & LVDS_CLKB_POWER_UP)
8325 clock.p2 = 7;
8326 else
8327 clock.p2 = 14;
79e53945
JB
8328 } else {
8329 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8330 clock.p1 = 2;
8331 else {
8332 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8333 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8334 }
8335 if (dpll & PLL_P2_DIVIDE_BY_4)
8336 clock.p2 = 4;
8337 else
8338 clock.p2 = 2;
79e53945 8339 }
da4a1efa
VS
8340
8341 i9xx_clock(refclk, &clock);
79e53945
JB
8342 }
8343
18442d08
VS
8344 /*
8345 * This value includes pixel_multiplier. We will use
241bfc38 8346 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8347 * encoder's get_config() function.
8348 */
8349 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8350}
8351
6878da05
VS
8352int intel_dotclock_calculate(int link_freq,
8353 const struct intel_link_m_n *m_n)
f1f644dc 8354{
f1f644dc
JB
8355 /*
8356 * The calculation for the data clock is:
1041a02f 8357 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8358 * But we want to avoid losing precison if possible, so:
1041a02f 8359 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8360 *
8361 * and the link clock is simpler:
1041a02f 8362 * link_clock = (m * link_clock) / n
f1f644dc
JB
8363 */
8364
6878da05
VS
8365 if (!m_n->link_n)
8366 return 0;
f1f644dc 8367
6878da05
VS
8368 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8369}
f1f644dc 8370
18442d08
VS
8371static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8372 struct intel_crtc_config *pipe_config)
6878da05
VS
8373{
8374 struct drm_device *dev = crtc->base.dev;
79e53945 8375
18442d08
VS
8376 /* read out port_clock from the DPLL */
8377 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8378
f1f644dc 8379 /*
18442d08 8380 * This value does not include pixel_multiplier.
241bfc38 8381 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8382 * agree once we know their relationship in the encoder's
8383 * get_config() function.
79e53945 8384 */
241bfc38 8385 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8386 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8387 &pipe_config->fdi_m_n);
79e53945
JB
8388}
8389
8390/** Returns the currently programmed mode of the given pipe. */
8391struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8392 struct drm_crtc *crtc)
8393{
548f245b 8394 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8396 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8397 struct drm_display_mode *mode;
f1f644dc 8398 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8399 int htot = I915_READ(HTOTAL(cpu_transcoder));
8400 int hsync = I915_READ(HSYNC(cpu_transcoder));
8401 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8402 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8403 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8404
8405 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8406 if (!mode)
8407 return NULL;
8408
f1f644dc
JB
8409 /*
8410 * Construct a pipe_config sufficient for getting the clock info
8411 * back out of crtc_clock_get.
8412 *
8413 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8414 * to use a real value here instead.
8415 */
293623f7 8416 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8417 pipe_config.pixel_multiplier = 1;
293623f7
VS
8418 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8419 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8420 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8421 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8422
773ae034 8423 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8424 mode->hdisplay = (htot & 0xffff) + 1;
8425 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8426 mode->hsync_start = (hsync & 0xffff) + 1;
8427 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8428 mode->vdisplay = (vtot & 0xffff) + 1;
8429 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8430 mode->vsync_start = (vsync & 0xffff) + 1;
8431 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8432
8433 drm_mode_set_name(mode);
79e53945
JB
8434
8435 return mode;
8436}
8437
3dec0095 8438static void intel_increase_pllclock(struct drm_crtc *crtc)
652c393a
JB
8439{
8440 struct drm_device *dev = crtc->dev;
fbee40df 8441 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a
JB
8442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8443 int pipe = intel_crtc->pipe;
dbdc6479
JB
8444 int dpll_reg = DPLL(pipe);
8445 int dpll;
652c393a 8446
bad720ff 8447 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8448 return;
8449
8450 if (!dev_priv->lvds_downclock_avail)
8451 return;
8452
dbdc6479 8453 dpll = I915_READ(dpll_reg);
652c393a 8454 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8455 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8456
8ac5a6d5 8457 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8458
8459 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8460 I915_WRITE(dpll_reg, dpll);
9d0498a2 8461 intel_wait_for_vblank(dev, pipe);
dbdc6479 8462
652c393a
JB
8463 dpll = I915_READ(dpll_reg);
8464 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8465 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8466 }
652c393a
JB
8467}
8468
8469static void intel_decrease_pllclock(struct drm_crtc *crtc)
8470{
8471 struct drm_device *dev = crtc->dev;
fbee40df 8472 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8474
bad720ff 8475 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8476 return;
8477
8478 if (!dev_priv->lvds_downclock_avail)
8479 return;
8480
8481 /*
8482 * Since this is called by a timer, we should never get here in
8483 * the manual case.
8484 */
8485 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8486 int pipe = intel_crtc->pipe;
8487 int dpll_reg = DPLL(pipe);
8488 int dpll;
f6e5b160 8489
44d98a61 8490 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8491
8ac5a6d5 8492 assert_panel_unlocked(dev_priv, pipe);
652c393a 8493
dc257cf1 8494 dpll = I915_READ(dpll_reg);
652c393a
JB
8495 dpll |= DISPLAY_RATE_SELECT_FPA1;
8496 I915_WRITE(dpll_reg, dpll);
9d0498a2 8497 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8498 dpll = I915_READ(dpll_reg);
8499 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8500 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8501 }
8502
8503}
8504
f047e395
CW
8505void intel_mark_busy(struct drm_device *dev)
8506{
c67a470b
PZ
8507 struct drm_i915_private *dev_priv = dev->dev_private;
8508
f62a0076
CW
8509 if (dev_priv->mm.busy)
8510 return;
8511
43694d69 8512 intel_runtime_pm_get(dev_priv);
c67a470b 8513 i915_update_gfx_val(dev_priv);
f62a0076 8514 dev_priv->mm.busy = true;
f047e395
CW
8515}
8516
8517void intel_mark_idle(struct drm_device *dev)
652c393a 8518{
c67a470b 8519 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8520 struct drm_crtc *crtc;
652c393a 8521
f62a0076
CW
8522 if (!dev_priv->mm.busy)
8523 return;
8524
8525 dev_priv->mm.busy = false;
8526
d330a953 8527 if (!i915.powersave)
bb4cdd53 8528 goto out;
652c393a 8529
652c393a 8530 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
f4510a27 8531 if (!crtc->primary->fb)
652c393a
JB
8532 continue;
8533
725a5b54 8534 intel_decrease_pllclock(crtc);
652c393a 8535 }
b29c19b6 8536
3d13ef2e 8537 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 8538 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
8539
8540out:
43694d69 8541 intel_runtime_pm_put(dev_priv);
652c393a
JB
8542}
8543
c65355bb
CW
8544void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8545 struct intel_ring_buffer *ring)
652c393a 8546{
f047e395
CW
8547 struct drm_device *dev = obj->base.dev;
8548 struct drm_crtc *crtc;
652c393a 8549
d330a953 8550 if (!i915.powersave)
acb87dfb
CW
8551 return;
8552
652c393a 8553 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
f4510a27 8554 if (!crtc->primary->fb)
652c393a
JB
8555 continue;
8556
f4510a27 8557 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
c65355bb
CW
8558 continue;
8559
8560 intel_increase_pllclock(crtc);
8561 if (ring && intel_fbc_enabled(dev))
8562 ring->fbc_dirty = true;
652c393a
JB
8563 }
8564}
8565
79e53945
JB
8566static void intel_crtc_destroy(struct drm_crtc *crtc)
8567{
8568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
8569 struct drm_device *dev = crtc->dev;
8570 struct intel_unpin_work *work;
8571 unsigned long flags;
8572
8573 spin_lock_irqsave(&dev->event_lock, flags);
8574 work = intel_crtc->unpin_work;
8575 intel_crtc->unpin_work = NULL;
8576 spin_unlock_irqrestore(&dev->event_lock, flags);
8577
8578 if (work) {
8579 cancel_work_sync(&work->work);
8580 kfree(work);
8581 }
79e53945 8582
40ccc72b
MK
8583 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8584
79e53945 8585 drm_crtc_cleanup(crtc);
67e77c5a 8586
79e53945
JB
8587 kfree(intel_crtc);
8588}
8589
6b95a207
KH
8590static void intel_unpin_work_fn(struct work_struct *__work)
8591{
8592 struct intel_unpin_work *work =
8593 container_of(__work, struct intel_unpin_work, work);
b4a98e57 8594 struct drm_device *dev = work->crtc->dev;
6b95a207 8595
b4a98e57 8596 mutex_lock(&dev->struct_mutex);
1690e1eb 8597 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
8598 drm_gem_object_unreference(&work->pending_flip_obj->base);
8599 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 8600
b4a98e57
CW
8601 intel_update_fbc(dev);
8602 mutex_unlock(&dev->struct_mutex);
8603
8604 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8605 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8606
6b95a207
KH
8607 kfree(work);
8608}
8609
1afe3e9d 8610static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 8611 struct drm_crtc *crtc)
6b95a207 8612{
fbee40df 8613 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
8614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8615 struct intel_unpin_work *work;
6b95a207
KH
8616 unsigned long flags;
8617
8618 /* Ignore early vblank irqs */
8619 if (intel_crtc == NULL)
8620 return;
8621
8622 spin_lock_irqsave(&dev->event_lock, flags);
8623 work = intel_crtc->unpin_work;
e7d841ca
CW
8624
8625 /* Ensure we don't miss a work->pending update ... */
8626 smp_rmb();
8627
8628 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
8629 spin_unlock_irqrestore(&dev->event_lock, flags);
8630 return;
8631 }
8632
e7d841ca
CW
8633 /* and that the unpin work is consistent wrt ->pending. */
8634 smp_rmb();
8635
6b95a207 8636 intel_crtc->unpin_work = NULL;
6b95a207 8637
45a066eb
RC
8638 if (work->event)
8639 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 8640
0af7e4df
MK
8641 drm_vblank_put(dev, intel_crtc->pipe);
8642
6b95a207
KH
8643 spin_unlock_irqrestore(&dev->event_lock, flags);
8644
2c10d571 8645 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
8646
8647 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
8648
8649 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
8650}
8651
1afe3e9d
JB
8652void intel_finish_page_flip(struct drm_device *dev, int pipe)
8653{
fbee40df 8654 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8655 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8656
49b14a5c 8657 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8658}
8659
8660void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8661{
fbee40df 8662 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
8663 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8664
49b14a5c 8665 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8666}
8667
6b95a207
KH
8668void intel_prepare_page_flip(struct drm_device *dev, int plane)
8669{
fbee40df 8670 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
8671 struct intel_crtc *intel_crtc =
8672 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8673 unsigned long flags;
8674
e7d841ca
CW
8675 /* NB: An MMIO update of the plane base pointer will also
8676 * generate a page-flip completion irq, i.e. every modeset
8677 * is also accompanied by a spurious intel_prepare_page_flip().
8678 */
6b95a207 8679 spin_lock_irqsave(&dev->event_lock, flags);
e7d841ca
CW
8680 if (intel_crtc->unpin_work)
8681 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
8682 spin_unlock_irqrestore(&dev->event_lock, flags);
8683}
8684
e7d841ca
CW
8685inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8686{
8687 /* Ensure that the work item is consistent when activating it ... */
8688 smp_wmb();
8689 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8690 /* and that it is marked active as soon as the irq could fire. */
8691 smp_wmb();
8692}
8693
8c9f3aaf
JB
8694static int intel_gen2_queue_flip(struct drm_device *dev,
8695 struct drm_crtc *crtc,
8696 struct drm_framebuffer *fb,
ed8d1975
KP
8697 struct drm_i915_gem_object *obj,
8698 uint32_t flags)
8c9f3aaf
JB
8699{
8700 struct drm_i915_private *dev_priv = dev->dev_private;
8701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8702 u32 flip_mask;
6d90c952 8703 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8704 int ret;
8705
6d90c952 8706 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8707 if (ret)
83d4092b 8708 goto err;
8c9f3aaf 8709
6d90c952 8710 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8711 if (ret)
83d4092b 8712 goto err_unpin;
8c9f3aaf
JB
8713
8714 /* Can't queue multiple flips, so wait for the previous
8715 * one to finish before executing the next.
8716 */
8717 if (intel_crtc->plane)
8718 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8719 else
8720 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8721 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8722 intel_ring_emit(ring, MI_NOOP);
8723 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8724 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8725 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8726 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952 8727 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
8728
8729 intel_mark_page_flip_active(intel_crtc);
09246732 8730 __intel_ring_advance(ring);
83d4092b
CW
8731 return 0;
8732
8733err_unpin:
8734 intel_unpin_fb_obj(obj);
8735err:
8c9f3aaf
JB
8736 return ret;
8737}
8738
8739static int intel_gen3_queue_flip(struct drm_device *dev,
8740 struct drm_crtc *crtc,
8741 struct drm_framebuffer *fb,
ed8d1975
KP
8742 struct drm_i915_gem_object *obj,
8743 uint32_t flags)
8c9f3aaf
JB
8744{
8745 struct drm_i915_private *dev_priv = dev->dev_private;
8746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8747 u32 flip_mask;
6d90c952 8748 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8749 int ret;
8750
6d90c952 8751 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8752 if (ret)
83d4092b 8753 goto err;
8c9f3aaf 8754
6d90c952 8755 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8756 if (ret)
83d4092b 8757 goto err_unpin;
8c9f3aaf
JB
8758
8759 if (intel_crtc->plane)
8760 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8761 else
8762 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8763 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8764 intel_ring_emit(ring, MI_NOOP);
8765 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8766 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8767 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8768 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952
DV
8769 intel_ring_emit(ring, MI_NOOP);
8770
e7d841ca 8771 intel_mark_page_flip_active(intel_crtc);
09246732 8772 __intel_ring_advance(ring);
83d4092b
CW
8773 return 0;
8774
8775err_unpin:
8776 intel_unpin_fb_obj(obj);
8777err:
8c9f3aaf
JB
8778 return ret;
8779}
8780
8781static int intel_gen4_queue_flip(struct drm_device *dev,
8782 struct drm_crtc *crtc,
8783 struct drm_framebuffer *fb,
ed8d1975
KP
8784 struct drm_i915_gem_object *obj,
8785 uint32_t flags)
8c9f3aaf
JB
8786{
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8789 uint32_t pf, pipesrc;
6d90c952 8790 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8791 int ret;
8792
6d90c952 8793 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8794 if (ret)
83d4092b 8795 goto err;
8c9f3aaf 8796
6d90c952 8797 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8798 if (ret)
83d4092b 8799 goto err_unpin;
8c9f3aaf
JB
8800
8801 /* i965+ uses the linear or tiled offsets from the
8802 * Display Registers (which do not change across a page-flip)
8803 * so we need only reprogram the base address.
8804 */
6d90c952
DV
8805 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8806 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8807 intel_ring_emit(ring, fb->pitches[0]);
c2c75131 8808 intel_ring_emit(ring,
f343c5f6 8809 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
c2c75131 8810 obj->tiling_mode);
8c9f3aaf
JB
8811
8812 /* XXX Enabling the panel-fitter across page-flip is so far
8813 * untested on non-native modes, so ignore it for now.
8814 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8815 */
8816 pf = 0;
8817 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8818 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8819
8820 intel_mark_page_flip_active(intel_crtc);
09246732 8821 __intel_ring_advance(ring);
83d4092b
CW
8822 return 0;
8823
8824err_unpin:
8825 intel_unpin_fb_obj(obj);
8826err:
8c9f3aaf
JB
8827 return ret;
8828}
8829
8830static int intel_gen6_queue_flip(struct drm_device *dev,
8831 struct drm_crtc *crtc,
8832 struct drm_framebuffer *fb,
ed8d1975
KP
8833 struct drm_i915_gem_object *obj,
8834 uint32_t flags)
8c9f3aaf
JB
8835{
8836 struct drm_i915_private *dev_priv = dev->dev_private;
8837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6d90c952 8838 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8839 uint32_t pf, pipesrc;
8840 int ret;
8841
6d90c952 8842 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8843 if (ret)
83d4092b 8844 goto err;
8c9f3aaf 8845
6d90c952 8846 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8847 if (ret)
83d4092b 8848 goto err_unpin;
8c9f3aaf 8849
6d90c952
DV
8850 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8851 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8852 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
f343c5f6 8853 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
8c9f3aaf 8854
dc257cf1
DV
8855 /* Contrary to the suggestions in the documentation,
8856 * "Enable Panel Fitter" does not seem to be required when page
8857 * flipping with a non-native mode, and worse causes a normal
8858 * modeset to fail.
8859 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8860 */
8861 pf = 0;
8c9f3aaf 8862 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8863 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8864
8865 intel_mark_page_flip_active(intel_crtc);
09246732 8866 __intel_ring_advance(ring);
83d4092b
CW
8867 return 0;
8868
8869err_unpin:
8870 intel_unpin_fb_obj(obj);
8871err:
8c9f3aaf
JB
8872 return ret;
8873}
8874
7c9017e5
JB
8875static int intel_gen7_queue_flip(struct drm_device *dev,
8876 struct drm_crtc *crtc,
8877 struct drm_framebuffer *fb,
ed8d1975
KP
8878 struct drm_i915_gem_object *obj,
8879 uint32_t flags)
7c9017e5
JB
8880{
8881 struct drm_i915_private *dev_priv = dev->dev_private;
8882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ffe74d75 8883 struct intel_ring_buffer *ring;
cb05d8de 8884 uint32_t plane_bit = 0;
ffe74d75
CW
8885 int len, ret;
8886
8887 ring = obj->ring;
1c5fd085 8888 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
ffe74d75 8889 ring = &dev_priv->ring[BCS];
7c9017e5
JB
8890
8891 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8892 if (ret)
83d4092b 8893 goto err;
7c9017e5 8894
cb05d8de
DV
8895 switch(intel_crtc->plane) {
8896 case PLANE_A:
8897 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8898 break;
8899 case PLANE_B:
8900 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8901 break;
8902 case PLANE_C:
8903 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8904 break;
8905 default:
8906 WARN_ONCE(1, "unknown plane in flip command\n");
8907 ret = -ENODEV;
ab3951eb 8908 goto err_unpin;
cb05d8de
DV
8909 }
8910
ffe74d75 8911 len = 4;
f476828a 8912 if (ring->id == RCS) {
ffe74d75 8913 len += 6;
f476828a
DL
8914 /*
8915 * On Gen 8, SRM is now taking an extra dword to accommodate
8916 * 48bits addresses, and we need a NOOP for the batch size to
8917 * stay even.
8918 */
8919 if (IS_GEN8(dev))
8920 len += 2;
8921 }
ffe74d75 8922
f66fab8e
VS
8923 /*
8924 * BSpec MI_DISPLAY_FLIP for IVB:
8925 * "The full packet must be contained within the same cache line."
8926 *
8927 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
8928 * cacheline, if we ever start emitting more commands before
8929 * the MI_DISPLAY_FLIP we may need to first emit everything else,
8930 * then do the cacheline alignment, and finally emit the
8931 * MI_DISPLAY_FLIP.
8932 */
8933 ret = intel_ring_cacheline_align(ring);
8934 if (ret)
8935 goto err_unpin;
8936
ffe74d75 8937 ret = intel_ring_begin(ring, len);
7c9017e5 8938 if (ret)
83d4092b 8939 goto err_unpin;
7c9017e5 8940
ffe74d75
CW
8941 /* Unmask the flip-done completion message. Note that the bspec says that
8942 * we should do this for both the BCS and RCS, and that we must not unmask
8943 * more than one flip event at any time (or ensure that one flip message
8944 * can be sent by waiting for flip-done prior to queueing new flips).
8945 * Experimentation says that BCS works despite DERRMR masking all
8946 * flip-done completion events and that unmasking all planes at once
8947 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8948 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8949 */
8950 if (ring->id == RCS) {
8951 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8952 intel_ring_emit(ring, DERRMR);
8953 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8954 DERRMR_PIPEB_PRI_FLIP_DONE |
8955 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
8956 if (IS_GEN8(dev))
8957 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
8958 MI_SRM_LRM_GLOBAL_GTT);
8959 else
8960 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
8961 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
8962 intel_ring_emit(ring, DERRMR);
8963 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
8964 if (IS_GEN8(dev)) {
8965 intel_ring_emit(ring, 0);
8966 intel_ring_emit(ring, MI_NOOP);
8967 }
ffe74d75
CW
8968 }
8969
cb05d8de 8970 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 8971 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
f343c5f6 8972 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7c9017e5 8973 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
8974
8975 intel_mark_page_flip_active(intel_crtc);
09246732 8976 __intel_ring_advance(ring);
83d4092b
CW
8977 return 0;
8978
8979err_unpin:
8980 intel_unpin_fb_obj(obj);
8981err:
7c9017e5
JB
8982 return ret;
8983}
8984
8c9f3aaf
JB
8985static int intel_default_queue_flip(struct drm_device *dev,
8986 struct drm_crtc *crtc,
8987 struct drm_framebuffer *fb,
ed8d1975
KP
8988 struct drm_i915_gem_object *obj,
8989 uint32_t flags)
8c9f3aaf
JB
8990{
8991 return -ENODEV;
8992}
8993
6b95a207
KH
8994static int intel_crtc_page_flip(struct drm_crtc *crtc,
8995 struct drm_framebuffer *fb,
ed8d1975
KP
8996 struct drm_pending_vblank_event *event,
8997 uint32_t page_flip_flags)
6b95a207
KH
8998{
8999 struct drm_device *dev = crtc->dev;
9000 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 9001 struct drm_framebuffer *old_fb = crtc->primary->fb;
4a35f83b 9002 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
6b95a207
KH
9003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9004 struct intel_unpin_work *work;
8c9f3aaf 9005 unsigned long flags;
52e68630 9006 int ret;
6b95a207 9007
e6a595d2 9008 /* Can't change pixel format via MI display flips. */
f4510a27 9009 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
9010 return -EINVAL;
9011
9012 /*
9013 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9014 * Note that pitch changes could also affect these register.
9015 */
9016 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
9017 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9018 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
9019 return -EINVAL;
9020
f900db47
CW
9021 if (i915_terminally_wedged(&dev_priv->gpu_error))
9022 goto out_hang;
9023
b14c5679 9024 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
9025 if (work == NULL)
9026 return -ENOMEM;
9027
6b95a207 9028 work->event = event;
b4a98e57 9029 work->crtc = crtc;
4a35f83b 9030 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
6b95a207
KH
9031 INIT_WORK(&work->work, intel_unpin_work_fn);
9032
7317c75e
JB
9033 ret = drm_vblank_get(dev, intel_crtc->pipe);
9034 if (ret)
9035 goto free_work;
9036
6b95a207
KH
9037 /* We borrow the event spin lock for protecting unpin_work */
9038 spin_lock_irqsave(&dev->event_lock, flags);
9039 if (intel_crtc->unpin_work) {
9040 spin_unlock_irqrestore(&dev->event_lock, flags);
9041 kfree(work);
7317c75e 9042 drm_vblank_put(dev, intel_crtc->pipe);
468f0b44
CW
9043
9044 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
9045 return -EBUSY;
9046 }
9047 intel_crtc->unpin_work = work;
9048 spin_unlock_irqrestore(&dev->event_lock, flags);
9049
b4a98e57
CW
9050 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9051 flush_workqueue(dev_priv->wq);
9052
79158103
CW
9053 ret = i915_mutex_lock_interruptible(dev);
9054 if (ret)
9055 goto cleanup;
6b95a207 9056
75dfca80 9057 /* Reference the objects for the scheduled work. */
05394f39
CW
9058 drm_gem_object_reference(&work->old_fb_obj->base);
9059 drm_gem_object_reference(&obj->base);
6b95a207 9060
f4510a27 9061 crtc->primary->fb = fb;
96b099fd 9062
e1f99ce6 9063 work->pending_flip_obj = obj;
e1f99ce6 9064
4e5359cd
SF
9065 work->enable_stall_check = true;
9066
b4a98e57 9067 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 9068 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 9069
ed8d1975 9070 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
8c9f3aaf
JB
9071 if (ret)
9072 goto cleanup_pending;
6b95a207 9073
7782de3b 9074 intel_disable_fbc(dev);
c65355bb 9075 intel_mark_fb_busy(obj, NULL);
6b95a207
KH
9076 mutex_unlock(&dev->struct_mutex);
9077
e5510fac
JB
9078 trace_i915_flip_request(intel_crtc->plane, obj);
9079
6b95a207 9080 return 0;
96b099fd 9081
8c9f3aaf 9082cleanup_pending:
b4a98e57 9083 atomic_dec(&intel_crtc->unpin_work_count);
f4510a27 9084 crtc->primary->fb = old_fb;
05394f39
CW
9085 drm_gem_object_unreference(&work->old_fb_obj->base);
9086 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9087 mutex_unlock(&dev->struct_mutex);
9088
79158103 9089cleanup:
96b099fd
CW
9090 spin_lock_irqsave(&dev->event_lock, flags);
9091 intel_crtc->unpin_work = NULL;
9092 spin_unlock_irqrestore(&dev->event_lock, flags);
9093
7317c75e
JB
9094 drm_vblank_put(dev, intel_crtc->pipe);
9095free_work:
96b099fd
CW
9096 kfree(work);
9097
f900db47
CW
9098 if (ret == -EIO) {
9099out_hang:
9100 intel_crtc_wait_for_pending_flips(crtc);
9101 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9102 if (ret == 0 && event)
9103 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9104 }
96b099fd 9105 return ret;
6b95a207
KH
9106}
9107
f6e5b160 9108static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9109 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9110 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9111};
9112
9a935856
DV
9113/**
9114 * intel_modeset_update_staged_output_state
9115 *
9116 * Updates the staged output configuration state, e.g. after we've read out the
9117 * current hw state.
9118 */
9119static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9120{
7668851f 9121 struct intel_crtc *crtc;
9a935856
DV
9122 struct intel_encoder *encoder;
9123 struct intel_connector *connector;
f6e5b160 9124
9a935856
DV
9125 list_for_each_entry(connector, &dev->mode_config.connector_list,
9126 base.head) {
9127 connector->new_encoder =
9128 to_intel_encoder(connector->base.encoder);
9129 }
f6e5b160 9130
9a935856
DV
9131 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9132 base.head) {
9133 encoder->new_crtc =
9134 to_intel_crtc(encoder->base.crtc);
9135 }
7668851f
VS
9136
9137 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9138 base.head) {
9139 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9140
9141 if (crtc->new_enabled)
9142 crtc->new_config = &crtc->config;
9143 else
9144 crtc->new_config = NULL;
7668851f 9145 }
f6e5b160
CW
9146}
9147
9a935856
DV
9148/**
9149 * intel_modeset_commit_output_state
9150 *
9151 * This function copies the stage display pipe configuration to the real one.
9152 */
9153static void intel_modeset_commit_output_state(struct drm_device *dev)
9154{
7668851f 9155 struct intel_crtc *crtc;
9a935856
DV
9156 struct intel_encoder *encoder;
9157 struct intel_connector *connector;
f6e5b160 9158
9a935856
DV
9159 list_for_each_entry(connector, &dev->mode_config.connector_list,
9160 base.head) {
9161 connector->base.encoder = &connector->new_encoder->base;
9162 }
f6e5b160 9163
9a935856
DV
9164 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9165 base.head) {
9166 encoder->base.crtc = &encoder->new_crtc->base;
9167 }
7668851f
VS
9168
9169 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9170 base.head) {
9171 crtc->base.enabled = crtc->new_enabled;
9172 }
9a935856
DV
9173}
9174
050f7aeb
DV
9175static void
9176connected_sink_compute_bpp(struct intel_connector * connector,
9177 struct intel_crtc_config *pipe_config)
9178{
9179 int bpp = pipe_config->pipe_bpp;
9180
9181 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9182 connector->base.base.id,
9183 drm_get_connector_name(&connector->base));
9184
9185 /* Don't use an invalid EDID bpc value */
9186 if (connector->base.display_info.bpc &&
9187 connector->base.display_info.bpc * 3 < bpp) {
9188 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9189 bpp, connector->base.display_info.bpc*3);
9190 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9191 }
9192
9193 /* Clamp bpp to 8 on screens without EDID 1.4 */
9194 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9195 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9196 bpp);
9197 pipe_config->pipe_bpp = 24;
9198 }
9199}
9200
4e53c2e0 9201static int
050f7aeb
DV
9202compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9203 struct drm_framebuffer *fb,
9204 struct intel_crtc_config *pipe_config)
4e53c2e0 9205{
050f7aeb
DV
9206 struct drm_device *dev = crtc->base.dev;
9207 struct intel_connector *connector;
4e53c2e0
DV
9208 int bpp;
9209
d42264b1
DV
9210 switch (fb->pixel_format) {
9211 case DRM_FORMAT_C8:
4e53c2e0
DV
9212 bpp = 8*3; /* since we go through a colormap */
9213 break;
d42264b1
DV
9214 case DRM_FORMAT_XRGB1555:
9215 case DRM_FORMAT_ARGB1555:
9216 /* checked in intel_framebuffer_init already */
9217 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9218 return -EINVAL;
9219 case DRM_FORMAT_RGB565:
4e53c2e0
DV
9220 bpp = 6*3; /* min is 18bpp */
9221 break;
d42264b1
DV
9222 case DRM_FORMAT_XBGR8888:
9223 case DRM_FORMAT_ABGR8888:
9224 /* checked in intel_framebuffer_init already */
9225 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9226 return -EINVAL;
9227 case DRM_FORMAT_XRGB8888:
9228 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
9229 bpp = 8*3;
9230 break;
d42264b1
DV
9231 case DRM_FORMAT_XRGB2101010:
9232 case DRM_FORMAT_ARGB2101010:
9233 case DRM_FORMAT_XBGR2101010:
9234 case DRM_FORMAT_ABGR2101010:
9235 /* checked in intel_framebuffer_init already */
9236 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 9237 return -EINVAL;
4e53c2e0
DV
9238 bpp = 10*3;
9239 break;
baba133a 9240 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
9241 default:
9242 DRM_DEBUG_KMS("unsupported depth\n");
9243 return -EINVAL;
9244 }
9245
4e53c2e0
DV
9246 pipe_config->pipe_bpp = bpp;
9247
9248 /* Clamp display bpp to EDID value */
9249 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 9250 base.head) {
1b829e05
DV
9251 if (!connector->new_encoder ||
9252 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
9253 continue;
9254
050f7aeb 9255 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
9256 }
9257
9258 return bpp;
9259}
9260
644db711
DV
9261static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9262{
9263 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9264 "type: 0x%x flags: 0x%x\n",
1342830c 9265 mode->crtc_clock,
644db711
DV
9266 mode->crtc_hdisplay, mode->crtc_hsync_start,
9267 mode->crtc_hsync_end, mode->crtc_htotal,
9268 mode->crtc_vdisplay, mode->crtc_vsync_start,
9269 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9270}
9271
c0b03411
DV
9272static void intel_dump_pipe_config(struct intel_crtc *crtc,
9273 struct intel_crtc_config *pipe_config,
9274 const char *context)
9275{
9276 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9277 context, pipe_name(crtc->pipe));
9278
9279 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9280 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9281 pipe_config->pipe_bpp, pipe_config->dither);
9282 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9283 pipe_config->has_pch_encoder,
9284 pipe_config->fdi_lanes,
9285 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9286 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9287 pipe_config->fdi_m_n.tu);
eb14cb74
VS
9288 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9289 pipe_config->has_dp_encoder,
9290 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9291 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9292 pipe_config->dp_m_n.tu);
c0b03411
DV
9293 DRM_DEBUG_KMS("requested mode:\n");
9294 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9295 DRM_DEBUG_KMS("adjusted mode:\n");
9296 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 9297 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 9298 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
9299 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9300 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
9301 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9302 pipe_config->gmch_pfit.control,
9303 pipe_config->gmch_pfit.pgm_ratios,
9304 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 9305 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 9306 pipe_config->pch_pfit.pos,
fd4daa9c
CW
9307 pipe_config->pch_pfit.size,
9308 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 9309 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 9310 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
9311}
9312
bc079e8b
VS
9313static bool encoders_cloneable(const struct intel_encoder *a,
9314 const struct intel_encoder *b)
accfc0c5 9315{
bc079e8b
VS
9316 /* masks could be asymmetric, so check both ways */
9317 return a == b || (a->cloneable & (1 << b->type) &&
9318 b->cloneable & (1 << a->type));
9319}
9320
9321static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9322 struct intel_encoder *encoder)
9323{
9324 struct drm_device *dev = crtc->base.dev;
9325 struct intel_encoder *source_encoder;
9326
9327 list_for_each_entry(source_encoder,
9328 &dev->mode_config.encoder_list, base.head) {
9329 if (source_encoder->new_crtc != crtc)
9330 continue;
9331
9332 if (!encoders_cloneable(encoder, source_encoder))
9333 return false;
9334 }
9335
9336 return true;
9337}
9338
9339static bool check_encoder_cloning(struct intel_crtc *crtc)
9340{
9341 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
9342 struct intel_encoder *encoder;
9343
bc079e8b
VS
9344 list_for_each_entry(encoder,
9345 &dev->mode_config.encoder_list, base.head) {
9346 if (encoder->new_crtc != crtc)
accfc0c5
DV
9347 continue;
9348
bc079e8b
VS
9349 if (!check_single_encoder_cloning(crtc, encoder))
9350 return false;
accfc0c5
DV
9351 }
9352
bc079e8b 9353 return true;
accfc0c5
DV
9354}
9355
b8cecdf5
DV
9356static struct intel_crtc_config *
9357intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 9358 struct drm_framebuffer *fb,
b8cecdf5 9359 struct drm_display_mode *mode)
ee7b9f93 9360{
7758a113 9361 struct drm_device *dev = crtc->dev;
7758a113 9362 struct intel_encoder *encoder;
b8cecdf5 9363 struct intel_crtc_config *pipe_config;
e29c22c0
DV
9364 int plane_bpp, ret = -EINVAL;
9365 bool retry = true;
ee7b9f93 9366
bc079e8b 9367 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
9368 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9369 return ERR_PTR(-EINVAL);
9370 }
9371
b8cecdf5
DV
9372 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9373 if (!pipe_config)
7758a113
DV
9374 return ERR_PTR(-ENOMEM);
9375
b8cecdf5
DV
9376 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9377 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 9378
e143a21c
DV
9379 pipe_config->cpu_transcoder =
9380 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 9381 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 9382
2960bc9c
ID
9383 /*
9384 * Sanitize sync polarity flags based on requested ones. If neither
9385 * positive or negative polarity is requested, treat this as meaning
9386 * negative polarity.
9387 */
9388 if (!(pipe_config->adjusted_mode.flags &
9389 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9390 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9391
9392 if (!(pipe_config->adjusted_mode.flags &
9393 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9394 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9395
050f7aeb
DV
9396 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9397 * plane pixel format and any sink constraints into account. Returns the
9398 * source plane bpp so that dithering can be selected on mismatches
9399 * after encoders and crtc also have had their say. */
9400 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9401 fb, pipe_config);
4e53c2e0
DV
9402 if (plane_bpp < 0)
9403 goto fail;
9404
e41a56be
VS
9405 /*
9406 * Determine the real pipe dimensions. Note that stereo modes can
9407 * increase the actual pipe size due to the frame doubling and
9408 * insertion of additional space for blanks between the frame. This
9409 * is stored in the crtc timings. We use the requested mode to do this
9410 * computation to clearly distinguish it from the adjusted mode, which
9411 * can be changed by the connectors in the below retry loop.
9412 */
9413 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9414 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9415 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9416
e29c22c0 9417encoder_retry:
ef1b460d 9418 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 9419 pipe_config->port_clock = 0;
ef1b460d 9420 pipe_config->pixel_multiplier = 1;
ff9a6750 9421
135c81b8 9422 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 9423 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 9424
7758a113
DV
9425 /* Pass our mode to the connectors and the CRTC to give them a chance to
9426 * adjust it according to limitations or connector properties, and also
9427 * a chance to reject the mode entirely.
47f1c6c9 9428 */
7758a113
DV
9429 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9430 base.head) {
47f1c6c9 9431
7758a113
DV
9432 if (&encoder->new_crtc->base != crtc)
9433 continue;
7ae89233 9434
efea6e8e
DV
9435 if (!(encoder->compute_config(encoder, pipe_config))) {
9436 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
9437 goto fail;
9438 }
ee7b9f93 9439 }
47f1c6c9 9440
ff9a6750
DV
9441 /* Set default port clock if not overwritten by the encoder. Needs to be
9442 * done afterwards in case the encoder adjusts the mode. */
9443 if (!pipe_config->port_clock)
241bfc38
DL
9444 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9445 * pipe_config->pixel_multiplier;
ff9a6750 9446
a43f6e0f 9447 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 9448 if (ret < 0) {
7758a113
DV
9449 DRM_DEBUG_KMS("CRTC fixup failed\n");
9450 goto fail;
ee7b9f93 9451 }
e29c22c0
DV
9452
9453 if (ret == RETRY) {
9454 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9455 ret = -EINVAL;
9456 goto fail;
9457 }
9458
9459 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9460 retry = false;
9461 goto encoder_retry;
9462 }
9463
4e53c2e0
DV
9464 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9465 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9466 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9467
b8cecdf5 9468 return pipe_config;
7758a113 9469fail:
b8cecdf5 9470 kfree(pipe_config);
e29c22c0 9471 return ERR_PTR(ret);
ee7b9f93 9472}
47f1c6c9 9473
e2e1ed41
DV
9474/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9475 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9476static void
9477intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9478 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
9479{
9480 struct intel_crtc *intel_crtc;
e2e1ed41
DV
9481 struct drm_device *dev = crtc->dev;
9482 struct intel_encoder *encoder;
9483 struct intel_connector *connector;
9484 struct drm_crtc *tmp_crtc;
79e53945 9485
e2e1ed41 9486 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 9487
e2e1ed41
DV
9488 /* Check which crtcs have changed outputs connected to them, these need
9489 * to be part of the prepare_pipes mask. We don't (yet) support global
9490 * modeset across multiple crtcs, so modeset_pipes will only have one
9491 * bit set at most. */
9492 list_for_each_entry(connector, &dev->mode_config.connector_list,
9493 base.head) {
9494 if (connector->base.encoder == &connector->new_encoder->base)
9495 continue;
79e53945 9496
e2e1ed41
DV
9497 if (connector->base.encoder) {
9498 tmp_crtc = connector->base.encoder->crtc;
9499
9500 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9501 }
9502
9503 if (connector->new_encoder)
9504 *prepare_pipes |=
9505 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
9506 }
9507
e2e1ed41
DV
9508 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9509 base.head) {
9510 if (encoder->base.crtc == &encoder->new_crtc->base)
9511 continue;
9512
9513 if (encoder->base.crtc) {
9514 tmp_crtc = encoder->base.crtc;
9515
9516 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9517 }
9518
9519 if (encoder->new_crtc)
9520 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
9521 }
9522
7668851f 9523 /* Check for pipes that will be enabled/disabled ... */
e2e1ed41
DV
9524 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9525 base.head) {
7668851f 9526 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 9527 continue;
7e7d76c3 9528
7668851f 9529 if (!intel_crtc->new_enabled)
e2e1ed41 9530 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
9531 else
9532 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
9533 }
9534
e2e1ed41
DV
9535
9536 /* set_mode is also used to update properties on life display pipes. */
9537 intel_crtc = to_intel_crtc(crtc);
7668851f 9538 if (intel_crtc->new_enabled)
e2e1ed41
DV
9539 *prepare_pipes |= 1 << intel_crtc->pipe;
9540
b6c5164d
DV
9541 /*
9542 * For simplicity do a full modeset on any pipe where the output routing
9543 * changed. We could be more clever, but that would require us to be
9544 * more careful with calling the relevant encoder->mode_set functions.
9545 */
e2e1ed41
DV
9546 if (*prepare_pipes)
9547 *modeset_pipes = *prepare_pipes;
9548
9549 /* ... and mask these out. */
9550 *modeset_pipes &= ~(*disable_pipes);
9551 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
9552
9553 /*
9554 * HACK: We don't (yet) fully support global modesets. intel_set_config
9555 * obies this rule, but the modeset restore mode of
9556 * intel_modeset_setup_hw_state does not.
9557 */
9558 *modeset_pipes &= 1 << intel_crtc->pipe;
9559 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
9560
9561 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9562 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 9563}
79e53945 9564
ea9d758d 9565static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 9566{
ea9d758d 9567 struct drm_encoder *encoder;
f6e5b160 9568 struct drm_device *dev = crtc->dev;
f6e5b160 9569
ea9d758d
DV
9570 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9571 if (encoder->crtc == crtc)
9572 return true;
9573
9574 return false;
9575}
9576
9577static void
9578intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9579{
9580 struct intel_encoder *intel_encoder;
9581 struct intel_crtc *intel_crtc;
9582 struct drm_connector *connector;
9583
9584 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9585 base.head) {
9586 if (!intel_encoder->base.crtc)
9587 continue;
9588
9589 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9590
9591 if (prepare_pipes & (1 << intel_crtc->pipe))
9592 intel_encoder->connectors_active = false;
9593 }
9594
9595 intel_modeset_commit_output_state(dev);
9596
7668851f 9597 /* Double check state. */
ea9d758d
DV
9598 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9599 base.head) {
7668851f 9600 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
9601 WARN_ON(intel_crtc->new_config &&
9602 intel_crtc->new_config != &intel_crtc->config);
9603 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
9604 }
9605
9606 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9607 if (!connector->encoder || !connector->encoder->crtc)
9608 continue;
9609
9610 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9611
9612 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
9613 struct drm_property *dpms_property =
9614 dev->mode_config.dpms_property;
9615
ea9d758d 9616 connector->dpms = DRM_MODE_DPMS_ON;
662595df 9617 drm_object_property_set_value(&connector->base,
68d34720
DV
9618 dpms_property,
9619 DRM_MODE_DPMS_ON);
ea9d758d
DV
9620
9621 intel_encoder = to_intel_encoder(connector->encoder);
9622 intel_encoder->connectors_active = true;
9623 }
9624 }
9625
9626}
9627
3bd26263 9628static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 9629{
3bd26263 9630 int diff;
f1f644dc
JB
9631
9632 if (clock1 == clock2)
9633 return true;
9634
9635 if (!clock1 || !clock2)
9636 return false;
9637
9638 diff = abs(clock1 - clock2);
9639
9640 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9641 return true;
9642
9643 return false;
9644}
9645
25c5b266
DV
9646#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9647 list_for_each_entry((intel_crtc), \
9648 &(dev)->mode_config.crtc_list, \
9649 base.head) \
0973f18f 9650 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 9651
0e8ffe1b 9652static bool
2fa2fe9a
DV
9653intel_pipe_config_compare(struct drm_device *dev,
9654 struct intel_crtc_config *current_config,
0e8ffe1b
DV
9655 struct intel_crtc_config *pipe_config)
9656{
66e985c0
DV
9657#define PIPE_CONF_CHECK_X(name) \
9658 if (current_config->name != pipe_config->name) { \
9659 DRM_ERROR("mismatch in " #name " " \
9660 "(expected 0x%08x, found 0x%08x)\n", \
9661 current_config->name, \
9662 pipe_config->name); \
9663 return false; \
9664 }
9665
08a24034
DV
9666#define PIPE_CONF_CHECK_I(name) \
9667 if (current_config->name != pipe_config->name) { \
9668 DRM_ERROR("mismatch in " #name " " \
9669 "(expected %i, found %i)\n", \
9670 current_config->name, \
9671 pipe_config->name); \
9672 return false; \
88adfff1
DV
9673 }
9674
1bd1bd80
DV
9675#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9676 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 9677 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
9678 "(expected %i, found %i)\n", \
9679 current_config->name & (mask), \
9680 pipe_config->name & (mask)); \
9681 return false; \
9682 }
9683
5e550656
VS
9684#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9685 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9686 DRM_ERROR("mismatch in " #name " " \
9687 "(expected %i, found %i)\n", \
9688 current_config->name, \
9689 pipe_config->name); \
9690 return false; \
9691 }
9692
bb760063
DV
9693#define PIPE_CONF_QUIRK(quirk) \
9694 ((current_config->quirks | pipe_config->quirks) & (quirk))
9695
eccb140b
DV
9696 PIPE_CONF_CHECK_I(cpu_transcoder);
9697
08a24034
DV
9698 PIPE_CONF_CHECK_I(has_pch_encoder);
9699 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
9700 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9701 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9702 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9703 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9704 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 9705
eb14cb74
VS
9706 PIPE_CONF_CHECK_I(has_dp_encoder);
9707 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9708 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9709 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9710 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9711 PIPE_CONF_CHECK_I(dp_m_n.tu);
9712
1bd1bd80
DV
9713 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9714 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9715 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9716 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9717 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9718 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9719
9720 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9721 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9722 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9723 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9724 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9725 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9726
c93f54cf 9727 PIPE_CONF_CHECK_I(pixel_multiplier);
6c49f241 9728
1bd1bd80
DV
9729 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9730 DRM_MODE_FLAG_INTERLACE);
9731
bb760063
DV
9732 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9733 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9734 DRM_MODE_FLAG_PHSYNC);
9735 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9736 DRM_MODE_FLAG_NHSYNC);
9737 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9738 DRM_MODE_FLAG_PVSYNC);
9739 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9740 DRM_MODE_FLAG_NVSYNC);
9741 }
045ac3b5 9742
37327abd
VS
9743 PIPE_CONF_CHECK_I(pipe_src_w);
9744 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 9745
9953599b
DV
9746 /*
9747 * FIXME: BIOS likes to set up a cloned config with lvds+external
9748 * screen. Since we don't yet re-compute the pipe config when moving
9749 * just the lvds port away to another pipe the sw tracking won't match.
9750 *
9751 * Proper atomic modesets with recomputed global state will fix this.
9752 * Until then just don't check gmch state for inherited modes.
9753 */
9754 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
9755 PIPE_CONF_CHECK_I(gmch_pfit.control);
9756 /* pfit ratios are autocomputed by the hw on gen4+ */
9757 if (INTEL_INFO(dev)->gen < 4)
9758 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9759 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
9760 }
9761
fd4daa9c
CW
9762 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9763 if (current_config->pch_pfit.enabled) {
9764 PIPE_CONF_CHECK_I(pch_pfit.pos);
9765 PIPE_CONF_CHECK_I(pch_pfit.size);
9766 }
2fa2fe9a 9767
e59150dc
JB
9768 /* BDW+ don't expose a synchronous way to read the state */
9769 if (IS_HASWELL(dev))
9770 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 9771
282740f7
VS
9772 PIPE_CONF_CHECK_I(double_wide);
9773
c0d43d62 9774 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 9775 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 9776 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
9777 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9778 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
c0d43d62 9779
42571aef
VS
9780 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9781 PIPE_CONF_CHECK_I(pipe_bpp);
9782
a9a7e98a
JB
9783 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
9784 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 9785
66e985c0 9786#undef PIPE_CONF_CHECK_X
08a24034 9787#undef PIPE_CONF_CHECK_I
1bd1bd80 9788#undef PIPE_CONF_CHECK_FLAGS
5e550656 9789#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 9790#undef PIPE_CONF_QUIRK
88adfff1 9791
0e8ffe1b
DV
9792 return true;
9793}
9794
91d1b4bd
DV
9795static void
9796check_connector_state(struct drm_device *dev)
8af6cf88 9797{
8af6cf88
DV
9798 struct intel_connector *connector;
9799
9800 list_for_each_entry(connector, &dev->mode_config.connector_list,
9801 base.head) {
9802 /* This also checks the encoder/connector hw state with the
9803 * ->get_hw_state callbacks. */
9804 intel_connector_check_state(connector);
9805
9806 WARN(&connector->new_encoder->base != connector->base.encoder,
9807 "connector's staged encoder doesn't match current encoder\n");
9808 }
91d1b4bd
DV
9809}
9810
9811static void
9812check_encoder_state(struct drm_device *dev)
9813{
9814 struct intel_encoder *encoder;
9815 struct intel_connector *connector;
8af6cf88
DV
9816
9817 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9818 base.head) {
9819 bool enabled = false;
9820 bool active = false;
9821 enum pipe pipe, tracked_pipe;
9822
9823 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9824 encoder->base.base.id,
9825 drm_get_encoder_name(&encoder->base));
9826
9827 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9828 "encoder's stage crtc doesn't match current crtc\n");
9829 WARN(encoder->connectors_active && !encoder->base.crtc,
9830 "encoder's active_connectors set, but no crtc\n");
9831
9832 list_for_each_entry(connector, &dev->mode_config.connector_list,
9833 base.head) {
9834 if (connector->base.encoder != &encoder->base)
9835 continue;
9836 enabled = true;
9837 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9838 active = true;
9839 }
9840 WARN(!!encoder->base.crtc != enabled,
9841 "encoder's enabled state mismatch "
9842 "(expected %i, found %i)\n",
9843 !!encoder->base.crtc, enabled);
9844 WARN(active && !encoder->base.crtc,
9845 "active encoder with no crtc\n");
9846
9847 WARN(encoder->connectors_active != active,
9848 "encoder's computed active state doesn't match tracked active state "
9849 "(expected %i, found %i)\n", active, encoder->connectors_active);
9850
9851 active = encoder->get_hw_state(encoder, &pipe);
9852 WARN(active != encoder->connectors_active,
9853 "encoder's hw state doesn't match sw tracking "
9854 "(expected %i, found %i)\n",
9855 encoder->connectors_active, active);
9856
9857 if (!encoder->base.crtc)
9858 continue;
9859
9860 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9861 WARN(active && pipe != tracked_pipe,
9862 "active encoder's pipe doesn't match"
9863 "(expected %i, found %i)\n",
9864 tracked_pipe, pipe);
9865
9866 }
91d1b4bd
DV
9867}
9868
9869static void
9870check_crtc_state(struct drm_device *dev)
9871{
fbee40df 9872 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
9873 struct intel_crtc *crtc;
9874 struct intel_encoder *encoder;
9875 struct intel_crtc_config pipe_config;
8af6cf88
DV
9876
9877 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9878 base.head) {
9879 bool enabled = false;
9880 bool active = false;
9881
045ac3b5
JB
9882 memset(&pipe_config, 0, sizeof(pipe_config));
9883
8af6cf88
DV
9884 DRM_DEBUG_KMS("[CRTC:%d]\n",
9885 crtc->base.base.id);
9886
9887 WARN(crtc->active && !crtc->base.enabled,
9888 "active crtc, but not enabled in sw tracking\n");
9889
9890 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9891 base.head) {
9892 if (encoder->base.crtc != &crtc->base)
9893 continue;
9894 enabled = true;
9895 if (encoder->connectors_active)
9896 active = true;
9897 }
6c49f241 9898
8af6cf88
DV
9899 WARN(active != crtc->active,
9900 "crtc's computed active state doesn't match tracked active state "
9901 "(expected %i, found %i)\n", active, crtc->active);
9902 WARN(enabled != crtc->base.enabled,
9903 "crtc's computed enabled state doesn't match tracked enabled state "
9904 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9905
0e8ffe1b
DV
9906 active = dev_priv->display.get_pipe_config(crtc,
9907 &pipe_config);
d62cf62a
DV
9908
9909 /* hw state is inconsistent with the pipe A quirk */
9910 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9911 active = crtc->active;
9912
6c49f241
DV
9913 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9914 base.head) {
3eaba51c 9915 enum pipe pipe;
6c49f241
DV
9916 if (encoder->base.crtc != &crtc->base)
9917 continue;
1d37b689 9918 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
9919 encoder->get_config(encoder, &pipe_config);
9920 }
9921
0e8ffe1b
DV
9922 WARN(crtc->active != active,
9923 "crtc active state doesn't match with hw state "
9924 "(expected %i, found %i)\n", crtc->active, active);
9925
c0b03411
DV
9926 if (active &&
9927 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9928 WARN(1, "pipe state doesn't match!\n");
9929 intel_dump_pipe_config(crtc, &pipe_config,
9930 "[hw state]");
9931 intel_dump_pipe_config(crtc, &crtc->config,
9932 "[sw state]");
9933 }
8af6cf88
DV
9934 }
9935}
9936
91d1b4bd
DV
9937static void
9938check_shared_dpll_state(struct drm_device *dev)
9939{
fbee40df 9940 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
9941 struct intel_crtc *crtc;
9942 struct intel_dpll_hw_state dpll_hw_state;
9943 int i;
5358901f
DV
9944
9945 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9946 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9947 int enabled_crtcs = 0, active_crtcs = 0;
9948 bool active;
9949
9950 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9951
9952 DRM_DEBUG_KMS("%s\n", pll->name);
9953
9954 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9955
9956 WARN(pll->active > pll->refcount,
9957 "more active pll users than references: %i vs %i\n",
9958 pll->active, pll->refcount);
9959 WARN(pll->active && !pll->on,
9960 "pll in active use but not on in sw tracking\n");
35c95375
DV
9961 WARN(pll->on && !pll->active,
9962 "pll in on but not on in use in sw tracking\n");
5358901f
DV
9963 WARN(pll->on != active,
9964 "pll on state mismatch (expected %i, found %i)\n",
9965 pll->on, active);
9966
9967 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9968 base.head) {
9969 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9970 enabled_crtcs++;
9971 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9972 active_crtcs++;
9973 }
9974 WARN(pll->active != active_crtcs,
9975 "pll active crtcs mismatch (expected %i, found %i)\n",
9976 pll->active, active_crtcs);
9977 WARN(pll->refcount != enabled_crtcs,
9978 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9979 pll->refcount, enabled_crtcs);
66e985c0
DV
9980
9981 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9982 sizeof(dpll_hw_state)),
9983 "pll hw state mismatch\n");
5358901f 9984 }
8af6cf88
DV
9985}
9986
91d1b4bd
DV
9987void
9988intel_modeset_check_state(struct drm_device *dev)
9989{
9990 check_connector_state(dev);
9991 check_encoder_state(dev);
9992 check_crtc_state(dev);
9993 check_shared_dpll_state(dev);
9994}
9995
18442d08
VS
9996void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9997 int dotclock)
9998{
9999 /*
10000 * FDI already provided one idea for the dotclock.
10001 * Yell if the encoder disagrees.
10002 */
241bfc38 10003 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 10004 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 10005 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
10006}
10007
f30da187
DV
10008static int __intel_set_mode(struct drm_crtc *crtc,
10009 struct drm_display_mode *mode,
10010 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
10011{
10012 struct drm_device *dev = crtc->dev;
fbee40df 10013 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 10014 struct drm_display_mode *saved_mode;
b8cecdf5 10015 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
10016 struct intel_crtc *intel_crtc;
10017 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 10018 int ret = 0;
a6778b3c 10019
4b4b9238 10020 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
10021 if (!saved_mode)
10022 return -ENOMEM;
a6778b3c 10023
e2e1ed41 10024 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
10025 &prepare_pipes, &disable_pipes);
10026
3ac18232 10027 *saved_mode = crtc->mode;
a6778b3c 10028
25c5b266
DV
10029 /* Hack: Because we don't (yet) support global modeset on multiple
10030 * crtcs, we don't keep track of the new mode for more than one crtc.
10031 * Hence simply check whether any bit is set in modeset_pipes in all the
10032 * pieces of code that are not yet converted to deal with mutliple crtcs
10033 * changing their mode at the same time. */
25c5b266 10034 if (modeset_pipes) {
4e53c2e0 10035 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
10036 if (IS_ERR(pipe_config)) {
10037 ret = PTR_ERR(pipe_config);
10038 pipe_config = NULL;
10039
3ac18232 10040 goto out;
25c5b266 10041 }
c0b03411
DV
10042 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10043 "[modeset]");
50741abc 10044 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 10045 }
a6778b3c 10046
30a970c6
JB
10047 /*
10048 * See if the config requires any additional preparation, e.g.
10049 * to adjust global state with pipes off. We need to do this
10050 * here so we can get the modeset_pipe updated config for the new
10051 * mode set on this crtc. For other crtcs we need to use the
10052 * adjusted_mode bits in the crtc directly.
10053 */
c164f833 10054 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 10055 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 10056
c164f833
VS
10057 /* may have added more to prepare_pipes than we should */
10058 prepare_pipes &= ~disable_pipes;
10059 }
10060
460da916
DV
10061 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10062 intel_crtc_disable(&intel_crtc->base);
10063
ea9d758d
DV
10064 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10065 if (intel_crtc->base.enabled)
10066 dev_priv->display.crtc_disable(&intel_crtc->base);
10067 }
a6778b3c 10068
6c4c86f5
DV
10069 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10070 * to set it here already despite that we pass it down the callchain.
f6e5b160 10071 */
b8cecdf5 10072 if (modeset_pipes) {
25c5b266 10073 crtc->mode = *mode;
b8cecdf5
DV
10074 /* mode_set/enable/disable functions rely on a correct pipe
10075 * config. */
10076 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 10077 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
10078
10079 /*
10080 * Calculate and store various constants which
10081 * are later needed by vblank and swap-completion
10082 * timestamping. They are derived from true hwmode.
10083 */
10084 drm_calc_timestamping_constants(crtc,
10085 &pipe_config->adjusted_mode);
b8cecdf5 10086 }
7758a113 10087
ea9d758d
DV
10088 /* Only after disabling all output pipelines that will be changed can we
10089 * update the the output configuration. */
10090 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10091
47fab737
DV
10092 if (dev_priv->display.modeset_global_resources)
10093 dev_priv->display.modeset_global_resources(dev);
10094
a6778b3c
DV
10095 /* Set up the DPLL and any encoders state that needs to adjust or depend
10096 * on the DPLL.
f6e5b160 10097 */
25c5b266 10098 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
c0c36b94 10099 ret = intel_crtc_mode_set(&intel_crtc->base,
c0c36b94
CW
10100 x, y, fb);
10101 if (ret)
10102 goto done;
a6778b3c
DV
10103 }
10104
10105 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
25c5b266
DV
10106 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
10107 dev_priv->display.crtc_enable(&intel_crtc->base);
a6778b3c 10108
a6778b3c
DV
10109 /* FIXME: add subpixel order */
10110done:
4b4b9238 10111 if (ret && crtc->enabled)
3ac18232 10112 crtc->mode = *saved_mode;
a6778b3c 10113
3ac18232 10114out:
b8cecdf5 10115 kfree(pipe_config);
3ac18232 10116 kfree(saved_mode);
a6778b3c 10117 return ret;
f6e5b160
CW
10118}
10119
e7457a9a
DL
10120static int intel_set_mode(struct drm_crtc *crtc,
10121 struct drm_display_mode *mode,
10122 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
10123{
10124 int ret;
10125
10126 ret = __intel_set_mode(crtc, mode, x, y, fb);
10127
10128 if (ret == 0)
10129 intel_modeset_check_state(crtc->dev);
10130
10131 return ret;
10132}
10133
c0c36b94
CW
10134void intel_crtc_restore_mode(struct drm_crtc *crtc)
10135{
f4510a27 10136 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
c0c36b94
CW
10137}
10138
25c5b266
DV
10139#undef for_each_intel_crtc_masked
10140
d9e55608
DV
10141static void intel_set_config_free(struct intel_set_config *config)
10142{
10143 if (!config)
10144 return;
10145
1aa4b628
DV
10146 kfree(config->save_connector_encoders);
10147 kfree(config->save_encoder_crtcs);
7668851f 10148 kfree(config->save_crtc_enabled);
d9e55608
DV
10149 kfree(config);
10150}
10151
85f9eb71
DV
10152static int intel_set_config_save_state(struct drm_device *dev,
10153 struct intel_set_config *config)
10154{
7668851f 10155 struct drm_crtc *crtc;
85f9eb71
DV
10156 struct drm_encoder *encoder;
10157 struct drm_connector *connector;
10158 int count;
10159
7668851f
VS
10160 config->save_crtc_enabled =
10161 kcalloc(dev->mode_config.num_crtc,
10162 sizeof(bool), GFP_KERNEL);
10163 if (!config->save_crtc_enabled)
10164 return -ENOMEM;
10165
1aa4b628
DV
10166 config->save_encoder_crtcs =
10167 kcalloc(dev->mode_config.num_encoder,
10168 sizeof(struct drm_crtc *), GFP_KERNEL);
10169 if (!config->save_encoder_crtcs)
85f9eb71
DV
10170 return -ENOMEM;
10171
1aa4b628
DV
10172 config->save_connector_encoders =
10173 kcalloc(dev->mode_config.num_connector,
10174 sizeof(struct drm_encoder *), GFP_KERNEL);
10175 if (!config->save_connector_encoders)
85f9eb71
DV
10176 return -ENOMEM;
10177
10178 /* Copy data. Note that driver private data is not affected.
10179 * Should anything bad happen only the expected state is
10180 * restored, not the drivers personal bookkeeping.
10181 */
7668851f
VS
10182 count = 0;
10183 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10184 config->save_crtc_enabled[count++] = crtc->enabled;
10185 }
10186
85f9eb71
DV
10187 count = 0;
10188 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 10189 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
10190 }
10191
10192 count = 0;
10193 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 10194 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
10195 }
10196
10197 return 0;
10198}
10199
10200static void intel_set_config_restore_state(struct drm_device *dev,
10201 struct intel_set_config *config)
10202{
7668851f 10203 struct intel_crtc *crtc;
9a935856
DV
10204 struct intel_encoder *encoder;
10205 struct intel_connector *connector;
85f9eb71
DV
10206 int count;
10207
7668851f
VS
10208 count = 0;
10209 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10210 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
10211
10212 if (crtc->new_enabled)
10213 crtc->new_config = &crtc->config;
10214 else
10215 crtc->new_config = NULL;
7668851f
VS
10216 }
10217
85f9eb71 10218 count = 0;
9a935856
DV
10219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10220 encoder->new_crtc =
10221 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
10222 }
10223
10224 count = 0;
9a935856
DV
10225 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10226 connector->new_encoder =
10227 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
10228 }
10229}
10230
e3de42b6 10231static bool
2e57f47d 10232is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
10233{
10234 int i;
10235
2e57f47d
CW
10236 if (set->num_connectors == 0)
10237 return false;
10238
10239 if (WARN_ON(set->connectors == NULL))
10240 return false;
10241
10242 for (i = 0; i < set->num_connectors; i++)
10243 if (set->connectors[i]->encoder &&
10244 set->connectors[i]->encoder->crtc == set->crtc &&
10245 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
10246 return true;
10247
10248 return false;
10249}
10250
5e2b584e
DV
10251static void
10252intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10253 struct intel_set_config *config)
10254{
10255
10256 /* We should be able to check here if the fb has the same properties
10257 * and then just flip_or_move it */
2e57f47d
CW
10258 if (is_crtc_connector_off(set)) {
10259 config->mode_changed = true;
f4510a27 10260 } else if (set->crtc->primary->fb != set->fb) {
5e2b584e 10261 /* If we have no fb then treat it as a full mode set */
f4510a27 10262 if (set->crtc->primary->fb == NULL) {
319d9827
JB
10263 struct intel_crtc *intel_crtc =
10264 to_intel_crtc(set->crtc);
10265
d330a953 10266 if (intel_crtc->active && i915.fastboot) {
319d9827
JB
10267 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10268 config->fb_changed = true;
10269 } else {
10270 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10271 config->mode_changed = true;
10272 }
5e2b584e
DV
10273 } else if (set->fb == NULL) {
10274 config->mode_changed = true;
72f4901e 10275 } else if (set->fb->pixel_format !=
f4510a27 10276 set->crtc->primary->fb->pixel_format) {
5e2b584e 10277 config->mode_changed = true;
e3de42b6 10278 } else {
5e2b584e 10279 config->fb_changed = true;
e3de42b6 10280 }
5e2b584e
DV
10281 }
10282
835c5873 10283 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
10284 config->fb_changed = true;
10285
10286 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10287 DRM_DEBUG_KMS("modes are different, full mode set\n");
10288 drm_mode_debug_printmodeline(&set->crtc->mode);
10289 drm_mode_debug_printmodeline(set->mode);
10290 config->mode_changed = true;
10291 }
a1d95703
CW
10292
10293 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10294 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
10295}
10296
2e431051 10297static int
9a935856
DV
10298intel_modeset_stage_output_state(struct drm_device *dev,
10299 struct drm_mode_set *set,
10300 struct intel_set_config *config)
50f56119 10301{
9a935856
DV
10302 struct intel_connector *connector;
10303 struct intel_encoder *encoder;
7668851f 10304 struct intel_crtc *crtc;
f3f08572 10305 int ro;
50f56119 10306
9abdda74 10307 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
10308 * of connectors. For paranoia, double-check this. */
10309 WARN_ON(!set->fb && (set->num_connectors != 0));
10310 WARN_ON(set->fb && (set->num_connectors == 0));
10311
9a935856
DV
10312 list_for_each_entry(connector, &dev->mode_config.connector_list,
10313 base.head) {
10314 /* Otherwise traverse passed in connector list and get encoders
10315 * for them. */
50f56119 10316 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856
DV
10317 if (set->connectors[ro] == &connector->base) {
10318 connector->new_encoder = connector->encoder;
50f56119
DV
10319 break;
10320 }
10321 }
10322
9a935856
DV
10323 /* If we disable the crtc, disable all its connectors. Also, if
10324 * the connector is on the changing crtc but not on the new
10325 * connector list, disable it. */
10326 if ((!set->fb || ro == set->num_connectors) &&
10327 connector->base.encoder &&
10328 connector->base.encoder->crtc == set->crtc) {
10329 connector->new_encoder = NULL;
10330
10331 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10332 connector->base.base.id,
10333 drm_get_connector_name(&connector->base));
10334 }
10335
10336
10337 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 10338 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 10339 config->mode_changed = true;
50f56119
DV
10340 }
10341 }
9a935856 10342 /* connector->new_encoder is now updated for all connectors. */
50f56119 10343
9a935856 10344 /* Update crtc of enabled connectors. */
9a935856
DV
10345 list_for_each_entry(connector, &dev->mode_config.connector_list,
10346 base.head) {
7668851f
VS
10347 struct drm_crtc *new_crtc;
10348
9a935856 10349 if (!connector->new_encoder)
50f56119
DV
10350 continue;
10351
9a935856 10352 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
10353
10354 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 10355 if (set->connectors[ro] == &connector->base)
50f56119
DV
10356 new_crtc = set->crtc;
10357 }
10358
10359 /* Make sure the new CRTC will work with the encoder */
14509916
TR
10360 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10361 new_crtc)) {
5e2b584e 10362 return -EINVAL;
50f56119 10363 }
9a935856
DV
10364 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10365
10366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10367 connector->base.base.id,
10368 drm_get_connector_name(&connector->base),
10369 new_crtc->base.id);
10370 }
10371
10372 /* Check for any encoders that needs to be disabled. */
10373 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10374 base.head) {
5a65f358 10375 int num_connectors = 0;
9a935856
DV
10376 list_for_each_entry(connector,
10377 &dev->mode_config.connector_list,
10378 base.head) {
10379 if (connector->new_encoder == encoder) {
10380 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 10381 num_connectors++;
9a935856
DV
10382 }
10383 }
5a65f358
PZ
10384
10385 if (num_connectors == 0)
10386 encoder->new_crtc = NULL;
10387 else if (num_connectors > 1)
10388 return -EINVAL;
10389
9a935856
DV
10390 /* Only now check for crtc changes so we don't miss encoders
10391 * that will be disabled. */
10392 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 10393 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 10394 config->mode_changed = true;
50f56119
DV
10395 }
10396 }
9a935856 10397 /* Now we've also updated encoder->new_crtc for all encoders. */
50f56119 10398
7668851f
VS
10399 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10400 base.head) {
10401 crtc->new_enabled = false;
10402
10403 list_for_each_entry(encoder,
10404 &dev->mode_config.encoder_list,
10405 base.head) {
10406 if (encoder->new_crtc == crtc) {
10407 crtc->new_enabled = true;
10408 break;
10409 }
10410 }
10411
10412 if (crtc->new_enabled != crtc->base.enabled) {
10413 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10414 crtc->new_enabled ? "en" : "dis");
10415 config->mode_changed = true;
10416 }
7bd0a8e7
VS
10417
10418 if (crtc->new_enabled)
10419 crtc->new_config = &crtc->config;
10420 else
10421 crtc->new_config = NULL;
7668851f
VS
10422 }
10423
2e431051
DV
10424 return 0;
10425}
10426
7d00a1f5
VS
10427static void disable_crtc_nofb(struct intel_crtc *crtc)
10428{
10429 struct drm_device *dev = crtc->base.dev;
10430 struct intel_encoder *encoder;
10431 struct intel_connector *connector;
10432
10433 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10434 pipe_name(crtc->pipe));
10435
10436 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10437 if (connector->new_encoder &&
10438 connector->new_encoder->new_crtc == crtc)
10439 connector->new_encoder = NULL;
10440 }
10441
10442 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10443 if (encoder->new_crtc == crtc)
10444 encoder->new_crtc = NULL;
10445 }
10446
10447 crtc->new_enabled = false;
7bd0a8e7 10448 crtc->new_config = NULL;
7d00a1f5
VS
10449}
10450
2e431051
DV
10451static int intel_crtc_set_config(struct drm_mode_set *set)
10452{
10453 struct drm_device *dev;
2e431051
DV
10454 struct drm_mode_set save_set;
10455 struct intel_set_config *config;
10456 int ret;
2e431051 10457
8d3e375e
DV
10458 BUG_ON(!set);
10459 BUG_ON(!set->crtc);
10460 BUG_ON(!set->crtc->helper_private);
2e431051 10461
7e53f3a4
DV
10462 /* Enforce sane interface api - has been abused by the fb helper. */
10463 BUG_ON(!set->mode && set->fb);
10464 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 10465
2e431051
DV
10466 if (set->fb) {
10467 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10468 set->crtc->base.id, set->fb->base.id,
10469 (int)set->num_connectors, set->x, set->y);
10470 } else {
10471 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
10472 }
10473
10474 dev = set->crtc->dev;
10475
10476 ret = -ENOMEM;
10477 config = kzalloc(sizeof(*config), GFP_KERNEL);
10478 if (!config)
10479 goto out_config;
10480
10481 ret = intel_set_config_save_state(dev, config);
10482 if (ret)
10483 goto out_config;
10484
10485 save_set.crtc = set->crtc;
10486 save_set.mode = &set->crtc->mode;
10487 save_set.x = set->crtc->x;
10488 save_set.y = set->crtc->y;
f4510a27 10489 save_set.fb = set->crtc->primary->fb;
2e431051
DV
10490
10491 /* Compute whether we need a full modeset, only an fb base update or no
10492 * change at all. In the future we might also check whether only the
10493 * mode changed, e.g. for LVDS where we only change the panel fitter in
10494 * such cases. */
10495 intel_set_config_compute_mode_changes(set, config);
10496
9a935856 10497 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
10498 if (ret)
10499 goto fail;
10500
5e2b584e 10501 if (config->mode_changed) {
c0c36b94
CW
10502 ret = intel_set_mode(set->crtc, set->mode,
10503 set->x, set->y, set->fb);
5e2b584e 10504 } else if (config->fb_changed) {
4878cae2
VS
10505 intel_crtc_wait_for_pending_flips(set->crtc);
10506
4f660f49 10507 ret = intel_pipe_set_base(set->crtc,
94352cf9 10508 set->x, set->y, set->fb);
7ca51a3a
JB
10509 /*
10510 * In the fastboot case this may be our only check of the
10511 * state after boot. It would be better to only do it on
10512 * the first update, but we don't have a nice way of doing that
10513 * (and really, set_config isn't used much for high freq page
10514 * flipping, so increasing its cost here shouldn't be a big
10515 * deal).
10516 */
d330a953 10517 if (i915.fastboot && ret == 0)
7ca51a3a 10518 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
10519 }
10520
2d05eae1 10521 if (ret) {
bf67dfeb
DV
10522 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10523 set->crtc->base.id, ret);
50f56119 10524fail:
2d05eae1 10525 intel_set_config_restore_state(dev, config);
50f56119 10526
7d00a1f5
VS
10527 /*
10528 * HACK: if the pipe was on, but we didn't have a framebuffer,
10529 * force the pipe off to avoid oopsing in the modeset code
10530 * due to fb==NULL. This should only happen during boot since
10531 * we don't yet reconstruct the FB from the hardware state.
10532 */
10533 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10534 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10535
2d05eae1
CW
10536 /* Try to restore the config */
10537 if (config->mode_changed &&
10538 intel_set_mode(save_set.crtc, save_set.mode,
10539 save_set.x, save_set.y, save_set.fb))
10540 DRM_ERROR("failed to restore config after modeset failure\n");
10541 }
50f56119 10542
d9e55608
DV
10543out_config:
10544 intel_set_config_free(config);
50f56119
DV
10545 return ret;
10546}
f6e5b160
CW
10547
10548static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160
CW
10549 .cursor_set = intel_crtc_cursor_set,
10550 .cursor_move = intel_crtc_cursor_move,
10551 .gamma_set = intel_crtc_gamma_set,
50f56119 10552 .set_config = intel_crtc_set_config,
f6e5b160
CW
10553 .destroy = intel_crtc_destroy,
10554 .page_flip = intel_crtc_page_flip,
10555};
10556
79f689aa
PZ
10557static void intel_cpu_pll_init(struct drm_device *dev)
10558{
affa9354 10559 if (HAS_DDI(dev))
79f689aa
PZ
10560 intel_ddi_pll_init(dev);
10561}
10562
5358901f
DV
10563static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10564 struct intel_shared_dpll *pll,
10565 struct intel_dpll_hw_state *hw_state)
ee7b9f93 10566{
5358901f 10567 uint32_t val;
ee7b9f93 10568
5358901f 10569 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
10570 hw_state->dpll = val;
10571 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10572 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
10573
10574 return val & DPLL_VCO_ENABLE;
10575}
10576
15bdd4cf
DV
10577static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10578 struct intel_shared_dpll *pll)
10579{
10580 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10581 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10582}
10583
e7b903d2
DV
10584static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10585 struct intel_shared_dpll *pll)
10586{
e7b903d2 10587 /* PCH refclock must be enabled first */
89eff4be 10588 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 10589
15bdd4cf
DV
10590 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10591
10592 /* Wait for the clocks to stabilize. */
10593 POSTING_READ(PCH_DPLL(pll->id));
10594 udelay(150);
10595
10596 /* The pixel multiplier can only be updated once the
10597 * DPLL is enabled and the clocks are stable.
10598 *
10599 * So write it again.
10600 */
10601 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10602 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10603 udelay(200);
10604}
10605
10606static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10607 struct intel_shared_dpll *pll)
10608{
10609 struct drm_device *dev = dev_priv->dev;
10610 struct intel_crtc *crtc;
e7b903d2
DV
10611
10612 /* Make sure no transcoder isn't still depending on us. */
10613 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10614 if (intel_crtc_to_shared_dpll(crtc) == pll)
10615 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
10616 }
10617
15bdd4cf
DV
10618 I915_WRITE(PCH_DPLL(pll->id), 0);
10619 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10620 udelay(200);
10621}
10622
46edb027
DV
10623static char *ibx_pch_dpll_names[] = {
10624 "PCH DPLL A",
10625 "PCH DPLL B",
10626};
10627
7c74ade1 10628static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 10629{
e7b903d2 10630 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
10631 int i;
10632
7c74ade1 10633 dev_priv->num_shared_dpll = 2;
ee7b9f93 10634
e72f9fbf 10635 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
10636 dev_priv->shared_dplls[i].id = i;
10637 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 10638 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
10639 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10640 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
10641 dev_priv->shared_dplls[i].get_hw_state =
10642 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
10643 }
10644}
10645
7c74ade1
DV
10646static void intel_shared_dpll_init(struct drm_device *dev)
10647{
e7b903d2 10648 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1
DV
10649
10650 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10651 ibx_pch_dpll_init(dev);
10652 else
10653 dev_priv->num_shared_dpll = 0;
10654
10655 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
10656}
10657
b358d0a6 10658static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 10659{
fbee40df 10660 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945
JB
10661 struct intel_crtc *intel_crtc;
10662 int i;
10663
955382f3 10664 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
10665 if (intel_crtc == NULL)
10666 return;
10667
10668 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10669
10670 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
10671 for (i = 0; i < 256; i++) {
10672 intel_crtc->lut_r[i] = i;
10673 intel_crtc->lut_g[i] = i;
10674 intel_crtc->lut_b[i] = i;
10675 }
10676
1f1c2e24
VS
10677 /*
10678 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10679 * is hooked to plane B. Hence we want plane A feeding pipe B.
10680 */
80824003
JB
10681 intel_crtc->pipe = pipe;
10682 intel_crtc->plane = pipe;
3a77c4c4 10683 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 10684 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 10685 intel_crtc->plane = !pipe;
80824003
JB
10686 }
10687
8d7849db
VS
10688 init_waitqueue_head(&intel_crtc->vbl_wait);
10689
22fd0fab
JB
10690 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10691 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10692 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10693 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10694
79e53945 10695 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
79e53945
JB
10696}
10697
752aa88a
JB
10698enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10699{
10700 struct drm_encoder *encoder = connector->base.encoder;
10701
10702 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10703
10704 if (!encoder)
10705 return INVALID_PIPE;
10706
10707 return to_intel_crtc(encoder->crtc)->pipe;
10708}
10709
08d7b3d1 10710int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 10711 struct drm_file *file)
08d7b3d1 10712{
08d7b3d1 10713 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
10714 struct drm_mode_object *drmmode_obj;
10715 struct intel_crtc *crtc;
08d7b3d1 10716
1cff8f6b
DV
10717 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10718 return -ENODEV;
08d7b3d1 10719
c05422d5
DV
10720 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10721 DRM_MODE_OBJECT_CRTC);
08d7b3d1 10722
c05422d5 10723 if (!drmmode_obj) {
08d7b3d1 10724 DRM_ERROR("no such CRTC id\n");
3f2c2057 10725 return -ENOENT;
08d7b3d1
CW
10726 }
10727
c05422d5
DV
10728 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10729 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 10730
c05422d5 10731 return 0;
08d7b3d1
CW
10732}
10733
66a9278e 10734static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 10735{
66a9278e
DV
10736 struct drm_device *dev = encoder->base.dev;
10737 struct intel_encoder *source_encoder;
79e53945 10738 int index_mask = 0;
79e53945
JB
10739 int entry = 0;
10740
66a9278e
DV
10741 list_for_each_entry(source_encoder,
10742 &dev->mode_config.encoder_list, base.head) {
bc079e8b 10743 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
10744 index_mask |= (1 << entry);
10745
79e53945
JB
10746 entry++;
10747 }
4ef69c7a 10748
79e53945
JB
10749 return index_mask;
10750}
10751
4d302442
CW
10752static bool has_edp_a(struct drm_device *dev)
10753{
10754 struct drm_i915_private *dev_priv = dev->dev_private;
10755
10756 if (!IS_MOBILE(dev))
10757 return false;
10758
10759 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10760 return false;
10761
e3589908 10762 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
10763 return false;
10764
10765 return true;
10766}
10767
ba0fbca4
DL
10768const char *intel_output_name(int output)
10769{
10770 static const char *names[] = {
10771 [INTEL_OUTPUT_UNUSED] = "Unused",
10772 [INTEL_OUTPUT_ANALOG] = "Analog",
10773 [INTEL_OUTPUT_DVO] = "DVO",
10774 [INTEL_OUTPUT_SDVO] = "SDVO",
10775 [INTEL_OUTPUT_LVDS] = "LVDS",
10776 [INTEL_OUTPUT_TVOUT] = "TV",
10777 [INTEL_OUTPUT_HDMI] = "HDMI",
10778 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
10779 [INTEL_OUTPUT_EDP] = "eDP",
10780 [INTEL_OUTPUT_DSI] = "DSI",
10781 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
10782 };
10783
10784 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
10785 return "Invalid";
10786
10787 return names[output];
10788}
10789
79e53945
JB
10790static void intel_setup_outputs(struct drm_device *dev)
10791{
725e30ad 10792 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 10793 struct intel_encoder *encoder;
cb0953d7 10794 bool dpd_is_edp = false;
79e53945 10795
c9093354 10796 intel_lvds_init(dev);
79e53945 10797
c40c0f5b 10798 if (!IS_ULT(dev))
79935fca 10799 intel_crt_init(dev);
cb0953d7 10800
affa9354 10801 if (HAS_DDI(dev)) {
0e72a5b5
ED
10802 int found;
10803
10804 /* Haswell uses DDI functions to detect digital outputs */
10805 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10806 /* DDI A only supports eDP */
10807 if (found)
10808 intel_ddi_init(dev, PORT_A);
10809
10810 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10811 * register */
10812 found = I915_READ(SFUSE_STRAP);
10813
10814 if (found & SFUSE_STRAP_DDIB_DETECTED)
10815 intel_ddi_init(dev, PORT_B);
10816 if (found & SFUSE_STRAP_DDIC_DETECTED)
10817 intel_ddi_init(dev, PORT_C);
10818 if (found & SFUSE_STRAP_DDID_DETECTED)
10819 intel_ddi_init(dev, PORT_D);
10820 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 10821 int found;
5d8a7752 10822 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
10823
10824 if (has_edp_a(dev))
10825 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 10826
dc0fa718 10827 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 10828 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 10829 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 10830 if (!found)
e2debe91 10831 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 10832 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 10833 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
10834 }
10835
dc0fa718 10836 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 10837 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 10838
dc0fa718 10839 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 10840 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 10841
5eb08b69 10842 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 10843 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 10844
270b3042 10845 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 10846 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 10847 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
10848 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10849 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10850 PORT_B);
10851 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10852 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10853 }
10854
6f6005a5
JB
10855 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10856 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10857 PORT_C);
10858 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 10859 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 10860 }
19c03924 10861
3cfca973 10862 intel_dsi_init(dev);
103a196f 10863 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 10864 bool found = false;
7d57382e 10865
e2debe91 10866 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10867 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 10868 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
10869 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10870 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 10871 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 10872 }
27185ae1 10873
e7281eab 10874 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10875 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 10876 }
13520b05
KH
10877
10878 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 10879
e2debe91 10880 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10881 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 10882 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 10883 }
27185ae1 10884
e2debe91 10885 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 10886
b01f2c3a
JB
10887 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10888 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 10889 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 10890 }
e7281eab 10891 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10892 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 10893 }
27185ae1 10894
b01f2c3a 10895 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 10896 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 10897 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 10898 } else if (IS_GEN2(dev))
79e53945
JB
10899 intel_dvo_init(dev);
10900
103a196f 10901 if (SUPPORTS_TV(dev))
79e53945
JB
10902 intel_tv_init(dev);
10903
4ef69c7a
CW
10904 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10905 encoder->base.possible_crtcs = encoder->crtc_mask;
10906 encoder->base.possible_clones =
66a9278e 10907 intel_encoder_clones(encoder);
79e53945 10908 }
47356eb6 10909
dde86e2d 10910 intel_init_pch_refclk(dev);
270b3042
DV
10911
10912 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
10913}
10914
10915static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10916{
10917 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 10918
ef2d633e
DV
10919 drm_framebuffer_cleanup(fb);
10920 WARN_ON(!intel_fb->obj->framebuffer_references--);
10921 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
79e53945
JB
10922 kfree(intel_fb);
10923}
10924
10925static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 10926 struct drm_file *file,
79e53945
JB
10927 unsigned int *handle)
10928{
10929 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 10930 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 10931
05394f39 10932 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
10933}
10934
10935static const struct drm_framebuffer_funcs intel_fb_funcs = {
10936 .destroy = intel_user_framebuffer_destroy,
10937 .create_handle = intel_user_framebuffer_create_handle,
10938};
10939
b5ea642a
DV
10940static int intel_framebuffer_init(struct drm_device *dev,
10941 struct intel_framebuffer *intel_fb,
10942 struct drm_mode_fb_cmd2 *mode_cmd,
10943 struct drm_i915_gem_object *obj)
79e53945 10944{
a57ce0b2 10945 int aligned_height;
a35cdaa0 10946 int pitch_limit;
79e53945
JB
10947 int ret;
10948
dd4916c5
DV
10949 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10950
c16ed4be
CW
10951 if (obj->tiling_mode == I915_TILING_Y) {
10952 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 10953 return -EINVAL;
c16ed4be 10954 }
57cd6508 10955
c16ed4be
CW
10956 if (mode_cmd->pitches[0] & 63) {
10957 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10958 mode_cmd->pitches[0]);
57cd6508 10959 return -EINVAL;
c16ed4be 10960 }
57cd6508 10961
a35cdaa0
CW
10962 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10963 pitch_limit = 32*1024;
10964 } else if (INTEL_INFO(dev)->gen >= 4) {
10965 if (obj->tiling_mode)
10966 pitch_limit = 16*1024;
10967 else
10968 pitch_limit = 32*1024;
10969 } else if (INTEL_INFO(dev)->gen >= 3) {
10970 if (obj->tiling_mode)
10971 pitch_limit = 8*1024;
10972 else
10973 pitch_limit = 16*1024;
10974 } else
10975 /* XXX DSPC is limited to 4k tiled */
10976 pitch_limit = 8*1024;
10977
10978 if (mode_cmd->pitches[0] > pitch_limit) {
10979 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10980 obj->tiling_mode ? "tiled" : "linear",
10981 mode_cmd->pitches[0], pitch_limit);
5d7bd705 10982 return -EINVAL;
c16ed4be 10983 }
5d7bd705
VS
10984
10985 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
10986 mode_cmd->pitches[0] != obj->stride) {
10987 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10988 mode_cmd->pitches[0], obj->stride);
5d7bd705 10989 return -EINVAL;
c16ed4be 10990 }
5d7bd705 10991
57779d06 10992 /* Reject formats not supported by any plane early. */
308e5bcb 10993 switch (mode_cmd->pixel_format) {
57779d06 10994 case DRM_FORMAT_C8:
04b3924d
VS
10995 case DRM_FORMAT_RGB565:
10996 case DRM_FORMAT_XRGB8888:
10997 case DRM_FORMAT_ARGB8888:
57779d06
VS
10998 break;
10999 case DRM_FORMAT_XRGB1555:
11000 case DRM_FORMAT_ARGB1555:
c16ed4be 11001 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
11002 DRM_DEBUG("unsupported pixel format: %s\n",
11003 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11004 return -EINVAL;
c16ed4be 11005 }
57779d06
VS
11006 break;
11007 case DRM_FORMAT_XBGR8888:
11008 case DRM_FORMAT_ABGR8888:
04b3924d
VS
11009 case DRM_FORMAT_XRGB2101010:
11010 case DRM_FORMAT_ARGB2101010:
57779d06
VS
11011 case DRM_FORMAT_XBGR2101010:
11012 case DRM_FORMAT_ABGR2101010:
c16ed4be 11013 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
11014 DRM_DEBUG("unsupported pixel format: %s\n",
11015 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11016 return -EINVAL;
c16ed4be 11017 }
b5626747 11018 break;
04b3924d
VS
11019 case DRM_FORMAT_YUYV:
11020 case DRM_FORMAT_UYVY:
11021 case DRM_FORMAT_YVYU:
11022 case DRM_FORMAT_VYUY:
c16ed4be 11023 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
11024 DRM_DEBUG("unsupported pixel format: %s\n",
11025 drm_get_format_name(mode_cmd->pixel_format));
57779d06 11026 return -EINVAL;
c16ed4be 11027 }
57cd6508
CW
11028 break;
11029 default:
4ee62c76
VS
11030 DRM_DEBUG("unsupported pixel format: %s\n",
11031 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
11032 return -EINVAL;
11033 }
11034
90f9a336
VS
11035 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11036 if (mode_cmd->offsets[0] != 0)
11037 return -EINVAL;
11038
a57ce0b2
JB
11039 aligned_height = intel_align_height(dev, mode_cmd->height,
11040 obj->tiling_mode);
53155c0a
DV
11041 /* FIXME drm helper for size checks (especially planar formats)? */
11042 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11043 return -EINVAL;
11044
c7d73f6a
DV
11045 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11046 intel_fb->obj = obj;
80075d49 11047 intel_fb->obj->framebuffer_references++;
c7d73f6a 11048
79e53945
JB
11049 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11050 if (ret) {
11051 DRM_ERROR("framebuffer init failed %d\n", ret);
11052 return ret;
11053 }
11054
79e53945
JB
11055 return 0;
11056}
11057
79e53945
JB
11058static struct drm_framebuffer *
11059intel_user_framebuffer_create(struct drm_device *dev,
11060 struct drm_file *filp,
308e5bcb 11061 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 11062{
05394f39 11063 struct drm_i915_gem_object *obj;
79e53945 11064
308e5bcb
JB
11065 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11066 mode_cmd->handles[0]));
c8725226 11067 if (&obj->base == NULL)
cce13ff7 11068 return ERR_PTR(-ENOENT);
79e53945 11069
d2dff872 11070 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
11071}
11072
4520f53a 11073#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 11074static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
11075{
11076}
11077#endif
11078
79e53945 11079static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 11080 .fb_create = intel_user_framebuffer_create,
0632fef6 11081 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
11082};
11083
e70236a8
JB
11084/* Set up chip specific display functions */
11085static void intel_init_display(struct drm_device *dev)
11086{
11087 struct drm_i915_private *dev_priv = dev->dev_private;
11088
ee9300bb
DV
11089 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11090 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
11091 else if (IS_CHERRYVIEW(dev))
11092 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
11093 else if (IS_VALLEYVIEW(dev))
11094 dev_priv->display.find_dpll = vlv_find_best_dpll;
11095 else if (IS_PINEVIEW(dev))
11096 dev_priv->display.find_dpll = pnv_find_best_dpll;
11097 else
11098 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11099
affa9354 11100 if (HAS_DDI(dev)) {
0e8ffe1b 11101 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 11102 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 11103 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
11104 dev_priv->display.crtc_enable = haswell_crtc_enable;
11105 dev_priv->display.crtc_disable = haswell_crtc_disable;
6441ab5f 11106 dev_priv->display.off = haswell_crtc_off;
262ca2b0
MR
11107 dev_priv->display.update_primary_plane =
11108 ironlake_update_primary_plane;
09b4ddf9 11109 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 11110 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 11111 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 11112 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
11113 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11114 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 11115 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
11116 dev_priv->display.update_primary_plane =
11117 ironlake_update_primary_plane;
89b667f8
JB
11118 } else if (IS_VALLEYVIEW(dev)) {
11119 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11120 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
11121 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11122 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11123 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11124 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11125 dev_priv->display.update_primary_plane =
11126 i9xx_update_primary_plane;
f564048e 11127 } else {
0e8ffe1b 11128 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11129 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 11130 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
11131 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11132 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 11133 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
11134 dev_priv->display.update_primary_plane =
11135 i9xx_update_primary_plane;
f564048e 11136 }
e70236a8 11137
e70236a8 11138 /* Returns the core display clock speed */
25eb05fc
JB
11139 if (IS_VALLEYVIEW(dev))
11140 dev_priv->display.get_display_clock_speed =
11141 valleyview_get_display_clock_speed;
11142 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
11143 dev_priv->display.get_display_clock_speed =
11144 i945_get_display_clock_speed;
11145 else if (IS_I915G(dev))
11146 dev_priv->display.get_display_clock_speed =
11147 i915_get_display_clock_speed;
257a7ffc 11148 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
11149 dev_priv->display.get_display_clock_speed =
11150 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
11151 else if (IS_PINEVIEW(dev))
11152 dev_priv->display.get_display_clock_speed =
11153 pnv_get_display_clock_speed;
e70236a8
JB
11154 else if (IS_I915GM(dev))
11155 dev_priv->display.get_display_clock_speed =
11156 i915gm_get_display_clock_speed;
11157 else if (IS_I865G(dev))
11158 dev_priv->display.get_display_clock_speed =
11159 i865_get_display_clock_speed;
f0f8a9ce 11160 else if (IS_I85X(dev))
e70236a8
JB
11161 dev_priv->display.get_display_clock_speed =
11162 i855_get_display_clock_speed;
11163 else /* 852, 830 */
11164 dev_priv->display.get_display_clock_speed =
11165 i830_get_display_clock_speed;
11166
7f8a8569 11167 if (HAS_PCH_SPLIT(dev)) {
f00a3ddf 11168 if (IS_GEN5(dev)) {
674cf967 11169 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
e0dac65e 11170 dev_priv->display.write_eld = ironlake_write_eld;
1398261a 11171 } else if (IS_GEN6(dev)) {
674cf967 11172 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
e0dac65e 11173 dev_priv->display.write_eld = ironlake_write_eld;
9a952a0d
PZ
11174 dev_priv->display.modeset_global_resources =
11175 snb_modeset_global_resources;
357555c0
JB
11176 } else if (IS_IVYBRIDGE(dev)) {
11177 /* FIXME: detect B0+ stepping and use auto training */
11178 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
e0dac65e 11179 dev_priv->display.write_eld = ironlake_write_eld;
01a415fd
DV
11180 dev_priv->display.modeset_global_resources =
11181 ivb_modeset_global_resources;
4e0bbc31 11182 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
c82e4d26 11183 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
83358c85 11184 dev_priv->display.write_eld = haswell_write_eld;
d6dd9eb1
DV
11185 dev_priv->display.modeset_global_resources =
11186 haswell_modeset_global_resources;
a0e63c22 11187 }
6067aaea 11188 } else if (IS_G4X(dev)) {
e0dac65e 11189 dev_priv->display.write_eld = g4x_write_eld;
30a970c6
JB
11190 } else if (IS_VALLEYVIEW(dev)) {
11191 dev_priv->display.modeset_global_resources =
11192 valleyview_modeset_global_resources;
9ca2fe73 11193 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 11194 }
8c9f3aaf
JB
11195
11196 /* Default just returns -ENODEV to indicate unsupported */
11197 dev_priv->display.queue_flip = intel_default_queue_flip;
11198
11199 switch (INTEL_INFO(dev)->gen) {
11200 case 2:
11201 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11202 break;
11203
11204 case 3:
11205 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11206 break;
11207
11208 case 4:
11209 case 5:
11210 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11211 break;
11212
11213 case 6:
11214 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11215 break;
7c9017e5 11216 case 7:
4e0bbc31 11217 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
11218 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11219 break;
8c9f3aaf 11220 }
7bd688cd
JN
11221
11222 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
11223}
11224
b690e96c
JB
11225/*
11226 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11227 * resume, or other times. This quirk makes sure that's the case for
11228 * affected systems.
11229 */
0206e353 11230static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
11231{
11232 struct drm_i915_private *dev_priv = dev->dev_private;
11233
11234 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 11235 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
11236}
11237
435793df
KP
11238/*
11239 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11240 */
11241static void quirk_ssc_force_disable(struct drm_device *dev)
11242{
11243 struct drm_i915_private *dev_priv = dev->dev_private;
11244 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 11245 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
11246}
11247
4dca20ef 11248/*
5a15ab5b
CE
11249 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11250 * brightness value
4dca20ef
CE
11251 */
11252static void quirk_invert_brightness(struct drm_device *dev)
11253{
11254 struct drm_i915_private *dev_priv = dev->dev_private;
11255 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 11256 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
11257}
11258
b690e96c
JB
11259struct intel_quirk {
11260 int device;
11261 int subsystem_vendor;
11262 int subsystem_device;
11263 void (*hook)(struct drm_device *dev);
11264};
11265
5f85f176
EE
11266/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11267struct intel_dmi_quirk {
11268 void (*hook)(struct drm_device *dev);
11269 const struct dmi_system_id (*dmi_id_list)[];
11270};
11271
11272static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11273{
11274 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11275 return 1;
11276}
11277
11278static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11279 {
11280 .dmi_id_list = &(const struct dmi_system_id[]) {
11281 {
11282 .callback = intel_dmi_reverse_brightness,
11283 .ident = "NCR Corporation",
11284 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11285 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11286 },
11287 },
11288 { } /* terminating entry */
11289 },
11290 .hook = quirk_invert_brightness,
11291 },
11292};
11293
c43b5634 11294static struct intel_quirk intel_quirks[] = {
b690e96c 11295 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 11296 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 11297
b690e96c
JB
11298 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11299 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11300
b690e96c
JB
11301 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11302 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11303
a4945f95 11304 /* 830 needs to leave pipe A & dpll A up */
dcdaed6e 11305 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
435793df
KP
11306
11307 /* Lenovo U160 cannot use SSC on LVDS */
11308 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
11309
11310 /* Sony Vaio Y cannot use SSC on LVDS */
11311 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 11312
be505f64
AH
11313 /* Acer Aspire 5734Z must invert backlight brightness */
11314 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11315
11316 /* Acer/eMachines G725 */
11317 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11318
11319 /* Acer/eMachines e725 */
11320 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11321
11322 /* Acer/Packard Bell NCL20 */
11323 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11324
11325 /* Acer Aspire 4736Z */
11326 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
11327
11328 /* Acer Aspire 5336 */
11329 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
b690e96c
JB
11330};
11331
11332static void intel_init_quirks(struct drm_device *dev)
11333{
11334 struct pci_dev *d = dev->pdev;
11335 int i;
11336
11337 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11338 struct intel_quirk *q = &intel_quirks[i];
11339
11340 if (d->device == q->device &&
11341 (d->subsystem_vendor == q->subsystem_vendor ||
11342 q->subsystem_vendor == PCI_ANY_ID) &&
11343 (d->subsystem_device == q->subsystem_device ||
11344 q->subsystem_device == PCI_ANY_ID))
11345 q->hook(dev);
11346 }
5f85f176
EE
11347 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11348 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11349 intel_dmi_quirks[i].hook(dev);
11350 }
b690e96c
JB
11351}
11352
9cce37f4
JB
11353/* Disable the VGA plane that we never use */
11354static void i915_disable_vga(struct drm_device *dev)
11355{
11356 struct drm_i915_private *dev_priv = dev->dev_private;
11357 u8 sr1;
766aa1c4 11358 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 11359
2b37c616 11360 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 11361 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 11362 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
11363 sr1 = inb(VGA_SR_DATA);
11364 outb(sr1 | 1<<5, VGA_SR_DATA);
11365 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11366 udelay(300);
11367
11368 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11369 POSTING_READ(vga_reg);
11370}
11371
f817586c
DV
11372void intel_modeset_init_hw(struct drm_device *dev)
11373{
a8f78b58
ED
11374 intel_prepare_ddi(dev);
11375
f817586c
DV
11376 intel_init_clock_gating(dev);
11377
5382f5f3 11378 intel_reset_dpio(dev);
40e9cf64 11379
8090c6b9 11380 intel_enable_gt_powersave(dev);
f817586c
DV
11381}
11382
7d708ee4
ID
11383void intel_modeset_suspend_hw(struct drm_device *dev)
11384{
11385 intel_suspend_hw(dev);
11386}
11387
79e53945
JB
11388void intel_modeset_init(struct drm_device *dev)
11389{
652c393a 11390 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 11391 int sprite, ret;
8cc87b75 11392 enum pipe pipe;
46f297fb 11393 struct intel_crtc *crtc;
79e53945
JB
11394
11395 drm_mode_config_init(dev);
11396
11397 dev->mode_config.min_width = 0;
11398 dev->mode_config.min_height = 0;
11399
019d96cb
DA
11400 dev->mode_config.preferred_depth = 24;
11401 dev->mode_config.prefer_shadow = 1;
11402
e6ecefaa 11403 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 11404
b690e96c
JB
11405 intel_init_quirks(dev);
11406
1fa61106
ED
11407 intel_init_pm(dev);
11408
e3c74757
BW
11409 if (INTEL_INFO(dev)->num_pipes == 0)
11410 return;
11411
e70236a8
JB
11412 intel_init_display(dev);
11413
a6c45cf0
CW
11414 if (IS_GEN2(dev)) {
11415 dev->mode_config.max_width = 2048;
11416 dev->mode_config.max_height = 2048;
11417 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
11418 dev->mode_config.max_width = 4096;
11419 dev->mode_config.max_height = 4096;
79e53945 11420 } else {
a6c45cf0
CW
11421 dev->mode_config.max_width = 8192;
11422 dev->mode_config.max_height = 8192;
79e53945 11423 }
068be561
DL
11424
11425 if (IS_GEN2(dev)) {
11426 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
11427 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
11428 } else {
11429 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
11430 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
11431 }
11432
5d4545ae 11433 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 11434
28c97730 11435 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
11436 INTEL_INFO(dev)->num_pipes,
11437 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 11438
8cc87b75
DL
11439 for_each_pipe(pipe) {
11440 intel_crtc_init(dev, pipe);
1fe47785
DL
11441 for_each_sprite(pipe, sprite) {
11442 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 11443 if (ret)
06da8da2 11444 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 11445 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 11446 }
79e53945
JB
11447 }
11448
f42bb70d 11449 intel_init_dpio(dev);
5382f5f3 11450 intel_reset_dpio(dev);
f42bb70d 11451
79f689aa 11452 intel_cpu_pll_init(dev);
e72f9fbf 11453 intel_shared_dpll_init(dev);
ee7b9f93 11454
9cce37f4
JB
11455 /* Just disable it once at startup */
11456 i915_disable_vga(dev);
79e53945 11457 intel_setup_outputs(dev);
11be49eb
CW
11458
11459 /* Just in case the BIOS is doing something questionable. */
11460 intel_disable_fbc(dev);
fa9fa083 11461
8b687df4 11462 mutex_lock(&dev->mode_config.mutex);
fa9fa083 11463 intel_modeset_setup_hw_state(dev, false);
8b687df4 11464 mutex_unlock(&dev->mode_config.mutex);
46f297fb
JB
11465
11466 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11467 base.head) {
11468 if (!crtc->active)
11469 continue;
11470
46f297fb 11471 /*
46f297fb
JB
11472 * Note that reserving the BIOS fb up front prevents us
11473 * from stuffing other stolen allocations like the ring
11474 * on top. This prevents some ugliness at boot time, and
11475 * can even allow for smooth boot transitions if the BIOS
11476 * fb is large enough for the active pipe configuration.
11477 */
11478 if (dev_priv->display.get_plane_config) {
11479 dev_priv->display.get_plane_config(crtc,
11480 &crtc->plane_config);
11481 /*
11482 * If the fb is shared between multiple heads, we'll
11483 * just get the first one.
11484 */
484b41dd 11485 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 11486 }
46f297fb 11487 }
2c7111db
CW
11488}
11489
24929352
DV
11490static void
11491intel_connector_break_all_links(struct intel_connector *connector)
11492{
11493 connector->base.dpms = DRM_MODE_DPMS_OFF;
11494 connector->base.encoder = NULL;
11495 connector->encoder->connectors_active = false;
11496 connector->encoder->base.crtc = NULL;
11497}
11498
7fad798e
DV
11499static void intel_enable_pipe_a(struct drm_device *dev)
11500{
11501 struct intel_connector *connector;
11502 struct drm_connector *crt = NULL;
11503 struct intel_load_detect_pipe load_detect_temp;
11504
11505 /* We can't just switch on the pipe A, we need to set things up with a
11506 * proper mode and output configuration. As a gross hack, enable pipe A
11507 * by enabling the load detect pipe once. */
11508 list_for_each_entry(connector,
11509 &dev->mode_config.connector_list,
11510 base.head) {
11511 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11512 crt = &connector->base;
11513 break;
11514 }
11515 }
11516
11517 if (!crt)
11518 return;
11519
11520 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
11521 intel_release_load_detect_pipe(crt, &load_detect_temp);
11522
652c393a 11523
7fad798e
DV
11524}
11525
fa555837
DV
11526static bool
11527intel_check_plane_mapping(struct intel_crtc *crtc)
11528{
7eb552ae
BW
11529 struct drm_device *dev = crtc->base.dev;
11530 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
11531 u32 reg, val;
11532
7eb552ae 11533 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
11534 return true;
11535
11536 reg = DSPCNTR(!crtc->plane);
11537 val = I915_READ(reg);
11538
11539 if ((val & DISPLAY_PLANE_ENABLE) &&
11540 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11541 return false;
11542
11543 return true;
11544}
11545
24929352
DV
11546static void intel_sanitize_crtc(struct intel_crtc *crtc)
11547{
11548 struct drm_device *dev = crtc->base.dev;
11549 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 11550 u32 reg;
24929352 11551
24929352 11552 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 11553 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
11554 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11555
11556 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
11557 * disable the crtc (and hence change the state) if it is wrong. Note
11558 * that gen4+ has a fixed plane -> pipe mapping. */
11559 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
11560 struct intel_connector *connector;
11561 bool plane;
11562
24929352
DV
11563 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11564 crtc->base.base.id);
11565
11566 /* Pipe has the wrong plane attached and the plane is active.
11567 * Temporarily change the plane mapping and disable everything
11568 * ... */
11569 plane = crtc->plane;
11570 crtc->plane = !plane;
11571 dev_priv->display.crtc_disable(&crtc->base);
11572 crtc->plane = plane;
11573
11574 /* ... and break all links. */
11575 list_for_each_entry(connector, &dev->mode_config.connector_list,
11576 base.head) {
11577 if (connector->encoder->base.crtc != &crtc->base)
11578 continue;
11579
11580 intel_connector_break_all_links(connector);
11581 }
11582
11583 WARN_ON(crtc->active);
11584 crtc->base.enabled = false;
11585 }
24929352 11586
7fad798e
DV
11587 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11588 crtc->pipe == PIPE_A && !crtc->active) {
11589 /* BIOS forgot to enable pipe A, this mostly happens after
11590 * resume. Force-enable the pipe to fix this, the update_dpms
11591 * call below we restore the pipe to the right state, but leave
11592 * the required bits on. */
11593 intel_enable_pipe_a(dev);
11594 }
11595
24929352
DV
11596 /* Adjust the state of the output pipe according to whether we
11597 * have active connectors/encoders. */
11598 intel_crtc_update_dpms(&crtc->base);
11599
11600 if (crtc->active != crtc->base.enabled) {
11601 struct intel_encoder *encoder;
11602
11603 /* This can happen either due to bugs in the get_hw_state
11604 * functions or because the pipe is force-enabled due to the
11605 * pipe A quirk. */
11606 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11607 crtc->base.base.id,
11608 crtc->base.enabled ? "enabled" : "disabled",
11609 crtc->active ? "enabled" : "disabled");
11610
11611 crtc->base.enabled = crtc->active;
11612
11613 /* Because we only establish the connector -> encoder ->
11614 * crtc links if something is active, this means the
11615 * crtc is now deactivated. Break the links. connector
11616 * -> encoder links are only establish when things are
11617 * actually up, hence no need to break them. */
11618 WARN_ON(crtc->active);
11619
11620 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11621 WARN_ON(encoder->connectors_active);
11622 encoder->base.crtc = NULL;
11623 }
11624 }
4cc31489
DV
11625 if (crtc->active) {
11626 /*
11627 * We start out with underrun reporting disabled to avoid races.
11628 * For correct bookkeeping mark this on active crtcs.
11629 *
11630 * No protection against concurrent access is required - at
11631 * worst a fifo underrun happens which also sets this to false.
11632 */
11633 crtc->cpu_fifo_underrun_disabled = true;
11634 crtc->pch_fifo_underrun_disabled = true;
11635 }
24929352
DV
11636}
11637
11638static void intel_sanitize_encoder(struct intel_encoder *encoder)
11639{
11640 struct intel_connector *connector;
11641 struct drm_device *dev = encoder->base.dev;
11642
11643 /* We need to check both for a crtc link (meaning that the
11644 * encoder is active and trying to read from a pipe) and the
11645 * pipe itself being active. */
11646 bool has_active_crtc = encoder->base.crtc &&
11647 to_intel_crtc(encoder->base.crtc)->active;
11648
11649 if (encoder->connectors_active && !has_active_crtc) {
11650 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11651 encoder->base.base.id,
11652 drm_get_encoder_name(&encoder->base));
11653
11654 /* Connector is active, but has no active pipe. This is
11655 * fallout from our resume register restoring. Disable
11656 * the encoder manually again. */
11657 if (encoder->base.crtc) {
11658 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11659 encoder->base.base.id,
11660 drm_get_encoder_name(&encoder->base));
11661 encoder->disable(encoder);
11662 }
11663
11664 /* Inconsistent output/port/pipe state happens presumably due to
11665 * a bug in one of the get_hw_state functions. Or someplace else
11666 * in our code, like the register restore mess on resume. Clamp
11667 * things to off as a safer default. */
11668 list_for_each_entry(connector,
11669 &dev->mode_config.connector_list,
11670 base.head) {
11671 if (connector->encoder != encoder)
11672 continue;
11673
11674 intel_connector_break_all_links(connector);
11675 }
11676 }
11677 /* Enabled encoders without active connectors will be fixed in
11678 * the crtc fixup. */
11679}
11680
04098753 11681void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
11682{
11683 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 11684 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 11685
04098753
ID
11686 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
11687 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
11688 i915_disable_vga(dev);
11689 }
11690}
11691
11692void i915_redisable_vga(struct drm_device *dev)
11693{
11694 struct drm_i915_private *dev_priv = dev->dev_private;
11695
8dc8a27c
PZ
11696 /* This function can be called both from intel_modeset_setup_hw_state or
11697 * at a very early point in our resume sequence, where the power well
11698 * structures are not yet restored. Since this function is at a very
11699 * paranoid "someone might have enabled VGA while we were not looking"
11700 * level, just check if the power well is enabled instead of trying to
11701 * follow the "don't touch the power well if we don't need it" policy
11702 * the rest of the driver uses. */
04098753 11703 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
11704 return;
11705
04098753 11706 i915_redisable_vga_power_on(dev);
0fde901f
KM
11707}
11708
98ec7739
VS
11709static bool primary_get_hw_state(struct intel_crtc *crtc)
11710{
11711 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
11712
11713 if (!crtc->active)
11714 return false;
11715
11716 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
11717}
11718
30e984df 11719static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
11720{
11721 struct drm_i915_private *dev_priv = dev->dev_private;
11722 enum pipe pipe;
24929352
DV
11723 struct intel_crtc *crtc;
11724 struct intel_encoder *encoder;
11725 struct intel_connector *connector;
5358901f 11726 int i;
24929352 11727
0e8ffe1b
DV
11728 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11729 base.head) {
88adfff1 11730 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 11731
9953599b
DV
11732 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
11733
0e8ffe1b
DV
11734 crtc->active = dev_priv->display.get_pipe_config(crtc,
11735 &crtc->config);
24929352
DV
11736
11737 crtc->base.enabled = crtc->active;
98ec7739 11738 crtc->primary_enabled = primary_get_hw_state(crtc);
24929352
DV
11739
11740 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11741 crtc->base.base.id,
11742 crtc->active ? "enabled" : "disabled");
11743 }
11744
5358901f 11745 /* FIXME: Smash this into the new shared dpll infrastructure. */
affa9354 11746 if (HAS_DDI(dev))
6441ab5f
PZ
11747 intel_ddi_setup_hw_pll_state(dev);
11748
5358901f
DV
11749 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11750 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11751
11752 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11753 pll->active = 0;
11754 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11755 base.head) {
11756 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11757 pll->active++;
11758 }
11759 pll->refcount = pll->active;
11760
35c95375
DV
11761 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11762 pll->name, pll->refcount, pll->on);
5358901f
DV
11763 }
11764
24929352
DV
11765 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11766 base.head) {
11767 pipe = 0;
11768
11769 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
11770 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11771 encoder->base.crtc = &crtc->base;
1d37b689 11772 encoder->get_config(encoder, &crtc->config);
24929352
DV
11773 } else {
11774 encoder->base.crtc = NULL;
11775 }
11776
11777 encoder->connectors_active = false;
6f2bcceb 11778 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352
DV
11779 encoder->base.base.id,
11780 drm_get_encoder_name(&encoder->base),
11781 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 11782 pipe_name(pipe));
24929352
DV
11783 }
11784
11785 list_for_each_entry(connector, &dev->mode_config.connector_list,
11786 base.head) {
11787 if (connector->get_hw_state(connector)) {
11788 connector->base.dpms = DRM_MODE_DPMS_ON;
11789 connector->encoder->connectors_active = true;
11790 connector->base.encoder = &connector->encoder->base;
11791 } else {
11792 connector->base.dpms = DRM_MODE_DPMS_OFF;
11793 connector->base.encoder = NULL;
11794 }
11795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11796 connector->base.base.id,
11797 drm_get_connector_name(&connector->base),
11798 connector->base.encoder ? "enabled" : "disabled");
11799 }
30e984df
DV
11800}
11801
11802/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11803 * and i915 state tracking structures. */
11804void intel_modeset_setup_hw_state(struct drm_device *dev,
11805 bool force_restore)
11806{
11807 struct drm_i915_private *dev_priv = dev->dev_private;
11808 enum pipe pipe;
30e984df
DV
11809 struct intel_crtc *crtc;
11810 struct intel_encoder *encoder;
35c95375 11811 int i;
30e984df
DV
11812
11813 intel_modeset_readout_hw_state(dev);
24929352 11814
babea61d
JB
11815 /*
11816 * Now that we have the config, copy it to each CRTC struct
11817 * Note that this could go away if we move to using crtc_config
11818 * checking everywhere.
11819 */
11820 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11821 base.head) {
d330a953 11822 if (crtc->active && i915.fastboot) {
f6a83288 11823 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
11824 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11825 crtc->base.base.id);
11826 drm_mode_debug_printmodeline(&crtc->base.mode);
11827 }
11828 }
11829
24929352
DV
11830 /* HW state is read out, now we need to sanitize this mess. */
11831 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11832 base.head) {
11833 intel_sanitize_encoder(encoder);
11834 }
11835
11836 for_each_pipe(pipe) {
11837 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11838 intel_sanitize_crtc(crtc);
c0b03411 11839 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 11840 }
9a935856 11841
35c95375
DV
11842 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11843 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11844
11845 if (!pll->on || pll->active)
11846 continue;
11847
11848 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11849
11850 pll->disable(dev_priv, pll);
11851 pll->on = false;
11852 }
11853
96f90c54 11854 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
11855 ilk_wm_get_hw_state(dev);
11856
45e2b5f6 11857 if (force_restore) {
7d0bc1ea
VS
11858 i915_redisable_vga(dev);
11859
f30da187
DV
11860 /*
11861 * We need to use raw interfaces for restoring state to avoid
11862 * checking (bogus) intermediate states.
11863 */
45e2b5f6 11864 for_each_pipe(pipe) {
b5644d05
JB
11865 struct drm_crtc *crtc =
11866 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
11867
11868 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
f4510a27 11869 crtc->primary->fb);
45e2b5f6
DV
11870 }
11871 } else {
11872 intel_modeset_update_staged_output_state(dev);
11873 }
8af6cf88
DV
11874
11875 intel_modeset_check_state(dev);
2c7111db
CW
11876}
11877
11878void intel_modeset_gem_init(struct drm_device *dev)
11879{
484b41dd
JB
11880 struct drm_crtc *c;
11881 struct intel_framebuffer *fb;
11882
ae48434c
ID
11883 mutex_lock(&dev->struct_mutex);
11884 intel_init_gt_powersave(dev);
11885 mutex_unlock(&dev->struct_mutex);
11886
1833b134 11887 intel_modeset_init_hw(dev);
02e792fb
DV
11888
11889 intel_setup_overlay(dev);
484b41dd
JB
11890
11891 /*
11892 * Make sure any fbs we allocated at startup are properly
11893 * pinned & fenced. When we do the allocation it's too early
11894 * for this.
11895 */
11896 mutex_lock(&dev->struct_mutex);
11897 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
66e514c1 11898 if (!c->primary->fb)
484b41dd
JB
11899 continue;
11900
66e514c1 11901 fb = to_intel_framebuffer(c->primary->fb);
484b41dd
JB
11902 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
11903 DRM_ERROR("failed to pin boot fb on pipe %d\n",
11904 to_intel_crtc(c)->pipe);
66e514c1
DA
11905 drm_framebuffer_unreference(c->primary->fb);
11906 c->primary->fb = NULL;
484b41dd
JB
11907 }
11908 }
11909 mutex_unlock(&dev->struct_mutex);
79e53945
JB
11910}
11911
4932e2c3
ID
11912void intel_connector_unregister(struct intel_connector *intel_connector)
11913{
11914 struct drm_connector *connector = &intel_connector->base;
11915
11916 intel_panel_destroy_backlight(connector);
11917 drm_sysfs_connector_remove(connector);
11918}
11919
79e53945
JB
11920void intel_modeset_cleanup(struct drm_device *dev)
11921{
652c393a
JB
11922 struct drm_i915_private *dev_priv = dev->dev_private;
11923 struct drm_crtc *crtc;
d9255d57 11924 struct drm_connector *connector;
652c393a 11925
fd0c0642
DV
11926 /*
11927 * Interrupts and polling as the first thing to avoid creating havoc.
11928 * Too much stuff here (turning of rps, connectors, ...) would
11929 * experience fancy races otherwise.
11930 */
11931 drm_irq_uninstall(dev);
11932 cancel_work_sync(&dev_priv->hotplug_work);
11933 /*
11934 * Due to the hpd irq storm handling the hotplug work can re-arm the
11935 * poll handlers. Hence disable polling after hpd handling is shut down.
11936 */
f87ea761 11937 drm_kms_helper_poll_fini(dev);
fd0c0642 11938
652c393a
JB
11939 mutex_lock(&dev->struct_mutex);
11940
723bfd70
JB
11941 intel_unregister_dsm_handler();
11942
652c393a
JB
11943 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11944 /* Skip inactive CRTCs */
f4510a27 11945 if (!crtc->primary->fb)
652c393a
JB
11946 continue;
11947
3dec0095 11948 intel_increase_pllclock(crtc);
652c393a
JB
11949 }
11950
973d04f9 11951 intel_disable_fbc(dev);
e70236a8 11952
8090c6b9 11953 intel_disable_gt_powersave(dev);
0cdab21f 11954
930ebb46
DV
11955 ironlake_teardown_rc6(dev);
11956
69341a5e
KH
11957 mutex_unlock(&dev->struct_mutex);
11958
1630fe75
CW
11959 /* flush any delayed tasks or pending work */
11960 flush_scheduled_work();
11961
db31af1d
JN
11962 /* destroy the backlight and sysfs files before encoders/connectors */
11963 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
11964 struct intel_connector *intel_connector;
11965
11966 intel_connector = to_intel_connector(connector);
11967 intel_connector->unregister(intel_connector);
db31af1d 11968 }
d9255d57 11969
79e53945 11970 drm_mode_config_cleanup(dev);
4d7bb011
DV
11971
11972 intel_cleanup_overlay(dev);
ae48434c
ID
11973
11974 mutex_lock(&dev->struct_mutex);
11975 intel_cleanup_gt_powersave(dev);
11976 mutex_unlock(&dev->struct_mutex);
79e53945
JB
11977}
11978
f1c79df3
ZW
11979/*
11980 * Return which encoder is currently attached for connector.
11981 */
df0e9248 11982struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 11983{
df0e9248
CW
11984 return &intel_attached_encoder(connector)->base;
11985}
f1c79df3 11986
df0e9248
CW
11987void intel_connector_attach_encoder(struct intel_connector *connector,
11988 struct intel_encoder *encoder)
11989{
11990 connector->encoder = encoder;
11991 drm_mode_connector_attach_encoder(&connector->base,
11992 &encoder->base);
79e53945 11993}
28d52043
DA
11994
11995/*
11996 * set vga decode state - true == enable VGA decode
11997 */
11998int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11999{
12000 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 12001 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
12002 u16 gmch_ctrl;
12003
75fa041d
CW
12004 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12005 DRM_ERROR("failed to read control word\n");
12006 return -EIO;
12007 }
12008
c0cc8a55
CW
12009 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12010 return 0;
12011
28d52043
DA
12012 if (state)
12013 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12014 else
12015 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
12016
12017 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12018 DRM_ERROR("failed to write control word\n");
12019 return -EIO;
12020 }
12021
28d52043
DA
12022 return 0;
12023}
c4a1d9e4 12024
c4a1d9e4 12025struct intel_display_error_state {
ff57f1b0
PZ
12026
12027 u32 power_well_driver;
12028
63b66e5b
CW
12029 int num_transcoders;
12030
c4a1d9e4
CW
12031 struct intel_cursor_error_state {
12032 u32 control;
12033 u32 position;
12034 u32 base;
12035 u32 size;
52331309 12036 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
12037
12038 struct intel_pipe_error_state {
ddf9c536 12039 bool power_domain_on;
c4a1d9e4 12040 u32 source;
f301b1e1 12041 u32 stat;
52331309 12042 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
12043
12044 struct intel_plane_error_state {
12045 u32 control;
12046 u32 stride;
12047 u32 size;
12048 u32 pos;
12049 u32 addr;
12050 u32 surface;
12051 u32 tile_offset;
52331309 12052 } plane[I915_MAX_PIPES];
63b66e5b
CW
12053
12054 struct intel_transcoder_error_state {
ddf9c536 12055 bool power_domain_on;
63b66e5b
CW
12056 enum transcoder cpu_transcoder;
12057
12058 u32 conf;
12059
12060 u32 htotal;
12061 u32 hblank;
12062 u32 hsync;
12063 u32 vtotal;
12064 u32 vblank;
12065 u32 vsync;
12066 } transcoder[4];
c4a1d9e4
CW
12067};
12068
12069struct intel_display_error_state *
12070intel_display_capture_error_state(struct drm_device *dev)
12071{
fbee40df 12072 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 12073 struct intel_display_error_state *error;
63b66e5b
CW
12074 int transcoders[] = {
12075 TRANSCODER_A,
12076 TRANSCODER_B,
12077 TRANSCODER_C,
12078 TRANSCODER_EDP,
12079 };
c4a1d9e4
CW
12080 int i;
12081
63b66e5b
CW
12082 if (INTEL_INFO(dev)->num_pipes == 0)
12083 return NULL;
12084
9d1cb914 12085 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
12086 if (error == NULL)
12087 return NULL;
12088
190be112 12089 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
12090 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12091
52331309 12092 for_each_pipe(i) {
ddf9c536 12093 error->pipe[i].power_domain_on =
da7e29bd
ID
12094 intel_display_power_enabled_sw(dev_priv,
12095 POWER_DOMAIN_PIPE(i));
ddf9c536 12096 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
12097 continue;
12098
a18c4c3d
PZ
12099 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
12100 error->cursor[i].control = I915_READ(CURCNTR(i));
12101 error->cursor[i].position = I915_READ(CURPOS(i));
12102 error->cursor[i].base = I915_READ(CURBASE(i));
12103 } else {
12104 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
12105 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
12106 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
12107 }
c4a1d9e4
CW
12108
12109 error->plane[i].control = I915_READ(DSPCNTR(i));
12110 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 12111 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 12112 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
12113 error->plane[i].pos = I915_READ(DSPPOS(i));
12114 }
ca291363
PZ
12115 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12116 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
12117 if (INTEL_INFO(dev)->gen >= 4) {
12118 error->plane[i].surface = I915_READ(DSPSURF(i));
12119 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12120 }
12121
c4a1d9e4 12122 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1
ID
12123
12124 if (!HAS_PCH_SPLIT(dev))
12125 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
12126 }
12127
12128 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12129 if (HAS_DDI(dev_priv->dev))
12130 error->num_transcoders++; /* Account for eDP. */
12131
12132 for (i = 0; i < error->num_transcoders; i++) {
12133 enum transcoder cpu_transcoder = transcoders[i];
12134
ddf9c536 12135 error->transcoder[i].power_domain_on =
da7e29bd 12136 intel_display_power_enabled_sw(dev_priv,
38cc1daf 12137 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 12138 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
12139 continue;
12140
63b66e5b
CW
12141 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12142
12143 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12144 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12145 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12146 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12147 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12148 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12149 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
12150 }
12151
12152 return error;
12153}
12154
edc3d884
MK
12155#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12156
c4a1d9e4 12157void
edc3d884 12158intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
12159 struct drm_device *dev,
12160 struct intel_display_error_state *error)
12161{
12162 int i;
12163
63b66e5b
CW
12164 if (!error)
12165 return;
12166
edc3d884 12167 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 12168 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 12169 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 12170 error->power_well_driver);
52331309 12171 for_each_pipe(i) {
edc3d884 12172 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
12173 err_printf(m, " Power: %s\n",
12174 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 12175 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 12176 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
12177
12178 err_printf(m, "Plane [%d]:\n", i);
12179 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12180 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 12181 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
12182 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12183 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 12184 }
4b71a570 12185 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 12186 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 12187 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
12188 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12189 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
12190 }
12191
edc3d884
MK
12192 err_printf(m, "Cursor [%d]:\n", i);
12193 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12194 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12195 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 12196 }
63b66e5b
CW
12197
12198 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 12199 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 12200 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
12201 err_printf(m, " Power: %s\n",
12202 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
12203 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12204 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12205 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12206 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12207 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12208 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12209 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12210 }
c4a1d9e4 12211}
This page took 7.335703 seconds and 5 git commands to generate.