2 * Copyright © 2006-2007 Intel Corporation
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:
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
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.
24 * Eric Anholt <eric@anholt.net>
26 * $FreeBSD: src/sys/dev/drm2/i915/intel_display.c,v 1.2 2012/05/24 19:13:54 dim Exp $
30 #include <sys/limits.h>
33 #include <drm/drm_edid.h>
34 #include "intel_drv.h"
35 #include <drm/i915_drm.h>
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
40 #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
42 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
43 static void intel_increase_pllclock(struct drm_crtc *crtc);
44 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
67 #define INTEL_P2_NUM 2
68 typedef struct intel_limit intel_limit_t;
70 intel_range_t dot, vco, n, m, m1, m2, p, p1;
72 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
73 int, int, intel_clock_t *, intel_clock_t *);
77 #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
80 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
81 int target, int refclk, intel_clock_t *match_clock,
82 intel_clock_t *best_clock);
84 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
85 int target, int refclk, intel_clock_t *match_clock,
86 intel_clock_t *best_clock);
89 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
90 int target, int refclk, intel_clock_t *match_clock,
91 intel_clock_t *best_clock);
93 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
94 int target, int refclk, intel_clock_t *match_clock,
95 intel_clock_t *best_clock);
97 static inline u32 /* units of 100MHz */
98 intel_fdi_link_freq(struct drm_device *dev)
101 struct drm_i915_private *dev_priv = dev->dev_private;
102 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
107 static const intel_limit_t intel_limits_i8xx_dvo = {
108 .dot = { .min = 25000, .max = 350000 },
109 .vco = { .min = 930000, .max = 1400000 },
110 .n = { .min = 3, .max = 16 },
111 .m = { .min = 96, .max = 140 },
112 .m1 = { .min = 18, .max = 26 },
113 .m2 = { .min = 6, .max = 16 },
114 .p = { .min = 4, .max = 128 },
115 .p1 = { .min = 2, .max = 33 },
116 .p2 = { .dot_limit = 165000,
117 .p2_slow = 4, .p2_fast = 2 },
118 .find_pll = intel_find_best_PLL,
121 static const intel_limit_t intel_limits_i8xx_lvds = {
122 .dot = { .min = 25000, .max = 350000 },
123 .vco = { .min = 930000, .max = 1400000 },
124 .n = { .min = 3, .max = 16 },
125 .m = { .min = 96, .max = 140 },
126 .m1 = { .min = 18, .max = 26 },
127 .m2 = { .min = 6, .max = 16 },
128 .p = { .min = 4, .max = 128 },
129 .p1 = { .min = 1, .max = 6 },
130 .p2 = { .dot_limit = 165000,
131 .p2_slow = 14, .p2_fast = 7 },
132 .find_pll = intel_find_best_PLL,
135 static const intel_limit_t intel_limits_i9xx_sdvo = {
136 .dot = { .min = 20000, .max = 400000 },
137 .vco = { .min = 1400000, .max = 2800000 },
138 .n = { .min = 1, .max = 6 },
139 .m = { .min = 70, .max = 120 },
140 .m1 = { .min = 10, .max = 22 },
141 .m2 = { .min = 5, .max = 9 },
142 .p = { .min = 5, .max = 80 },
143 .p1 = { .min = 1, .max = 8 },
144 .p2 = { .dot_limit = 200000,
145 .p2_slow = 10, .p2_fast = 5 },
146 .find_pll = intel_find_best_PLL,
149 static const intel_limit_t intel_limits_i9xx_lvds = {
150 .dot = { .min = 20000, .max = 400000 },
151 .vco = { .min = 1400000, .max = 2800000 },
152 .n = { .min = 1, .max = 6 },
153 .m = { .min = 70, .max = 120 },
154 .m1 = { .min = 10, .max = 22 },
155 .m2 = { .min = 5, .max = 9 },
156 .p = { .min = 7, .max = 98 },
157 .p1 = { .min = 1, .max = 8 },
158 .p2 = { .dot_limit = 112000,
159 .p2_slow = 14, .p2_fast = 7 },
160 .find_pll = intel_find_best_PLL,
164 static const intel_limit_t intel_limits_g4x_sdvo = {
165 .dot = { .min = 25000, .max = 270000 },
166 .vco = { .min = 1750000, .max = 3500000},
167 .n = { .min = 1, .max = 4 },
168 .m = { .min = 104, .max = 138 },
169 .m1 = { .min = 17, .max = 23 },
170 .m2 = { .min = 5, .max = 11 },
171 .p = { .min = 10, .max = 30 },
172 .p1 = { .min = 1, .max = 3},
173 .p2 = { .dot_limit = 270000,
177 .find_pll = intel_g4x_find_best_PLL,
180 static const intel_limit_t intel_limits_g4x_hdmi = {
181 .dot = { .min = 22000, .max = 400000 },
182 .vco = { .min = 1750000, .max = 3500000},
183 .n = { .min = 1, .max = 4 },
184 .m = { .min = 104, .max = 138 },
185 .m1 = { .min = 16, .max = 23 },
186 .m2 = { .min = 5, .max = 11 },
187 .p = { .min = 5, .max = 80 },
188 .p1 = { .min = 1, .max = 8},
189 .p2 = { .dot_limit = 165000,
190 .p2_slow = 10, .p2_fast = 5 },
191 .find_pll = intel_g4x_find_best_PLL,
194 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
195 .dot = { .min = 20000, .max = 115000 },
196 .vco = { .min = 1750000, .max = 3500000 },
197 .n = { .min = 1, .max = 3 },
198 .m = { .min = 104, .max = 138 },
199 .m1 = { .min = 17, .max = 23 },
200 .m2 = { .min = 5, .max = 11 },
201 .p = { .min = 28, .max = 112 },
202 .p1 = { .min = 2, .max = 8 },
203 .p2 = { .dot_limit = 0,
204 .p2_slow = 14, .p2_fast = 14
206 .find_pll = intel_g4x_find_best_PLL,
209 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
210 .dot = { .min = 80000, .max = 224000 },
211 .vco = { .min = 1750000, .max = 3500000 },
212 .n = { .min = 1, .max = 3 },
213 .m = { .min = 104, .max = 138 },
214 .m1 = { .min = 17, .max = 23 },
215 .m2 = { .min = 5, .max = 11 },
216 .p = { .min = 14, .max = 42 },
217 .p1 = { .min = 2, .max = 6 },
218 .p2 = { .dot_limit = 0,
219 .p2_slow = 7, .p2_fast = 7
221 .find_pll = intel_g4x_find_best_PLL,
224 static const intel_limit_t intel_limits_g4x_display_port = {
225 .dot = { .min = 161670, .max = 227000 },
226 .vco = { .min = 1750000, .max = 3500000},
227 .n = { .min = 1, .max = 2 },
228 .m = { .min = 97, .max = 108 },
229 .m1 = { .min = 0x10, .max = 0x12 },
230 .m2 = { .min = 0x05, .max = 0x06 },
231 .p = { .min = 10, .max = 20 },
232 .p1 = { .min = 1, .max = 2},
233 .p2 = { .dot_limit = 0,
234 .p2_slow = 10, .p2_fast = 10 },
235 .find_pll = intel_find_pll_g4x_dp,
238 static const intel_limit_t intel_limits_pineview_sdvo = {
239 .dot = { .min = 20000, .max = 400000},
240 .vco = { .min = 1700000, .max = 3500000 },
241 /* Pineview's Ncounter is a ring counter */
242 .n = { .min = 3, .max = 6 },
243 .m = { .min = 2, .max = 256 },
244 /* Pineview only has one combined m divider, which we treat as m2. */
245 .m1 = { .min = 0, .max = 0 },
246 .m2 = { .min = 0, .max = 254 },
247 .p = { .min = 5, .max = 80 },
248 .p1 = { .min = 1, .max = 8 },
249 .p2 = { .dot_limit = 200000,
250 .p2_slow = 10, .p2_fast = 5 },
251 .find_pll = intel_find_best_PLL,
254 static const intel_limit_t intel_limits_pineview_lvds = {
255 .dot = { .min = 20000, .max = 400000 },
256 .vco = { .min = 1700000, .max = 3500000 },
257 .n = { .min = 3, .max = 6 },
258 .m = { .min = 2, .max = 256 },
259 .m1 = { .min = 0, .max = 0 },
260 .m2 = { .min = 0, .max = 254 },
261 .p = { .min = 7, .max = 112 },
262 .p1 = { .min = 1, .max = 8 },
263 .p2 = { .dot_limit = 112000,
264 .p2_slow = 14, .p2_fast = 14 },
265 .find_pll = intel_find_best_PLL,
268 /* Ironlake / Sandybridge
270 * We calculate clock using (register_value + 2) for N/M1/M2, so here
271 * the range value for them is (actual_value - 2).
273 static const intel_limit_t intel_limits_ironlake_dac = {
274 .dot = { .min = 25000, .max = 350000 },
275 .vco = { .min = 1760000, .max = 3510000 },
276 .n = { .min = 1, .max = 5 },
277 .m = { .min = 79, .max = 127 },
278 .m1 = { .min = 12, .max = 22 },
279 .m2 = { .min = 5, .max = 9 },
280 .p = { .min = 5, .max = 80 },
281 .p1 = { .min = 1, .max = 8 },
282 .p2 = { .dot_limit = 225000,
283 .p2_slow = 10, .p2_fast = 5 },
284 .find_pll = intel_g4x_find_best_PLL,
287 static const intel_limit_t intel_limits_ironlake_single_lvds = {
288 .dot = { .min = 25000, .max = 350000 },
289 .vco = { .min = 1760000, .max = 3510000 },
290 .n = { .min = 1, .max = 3 },
291 .m = { .min = 79, .max = 118 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 28, .max = 112 },
295 .p1 = { .min = 2, .max = 8 },
296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 14, .p2_fast = 14 },
298 .find_pll = intel_g4x_find_best_PLL,
301 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 3 },
305 .m = { .min = 79, .max = 127 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 14, .max = 56 },
309 .p1 = { .min = 2, .max = 8 },
310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 7, .p2_fast = 7 },
312 .find_pll = intel_g4x_find_best_PLL,
315 /* LVDS 100mhz refclk limits. */
316 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
317 .dot = { .min = 25000, .max = 350000 },
318 .vco = { .min = 1760000, .max = 3510000 },
319 .n = { .min = 1, .max = 2 },
320 .m = { .min = 79, .max = 126 },
321 .m1 = { .min = 12, .max = 22 },
322 .m2 = { .min = 5, .max = 9 },
323 .p = { .min = 28, .max = 112 },
324 .p1 = { .min = 2, .max = 8 },
325 .p2 = { .dot_limit = 225000,
326 .p2_slow = 14, .p2_fast = 14 },
327 .find_pll = intel_g4x_find_best_PLL,
330 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
331 .dot = { .min = 25000, .max = 350000 },
332 .vco = { .min = 1760000, .max = 3510000 },
333 .n = { .min = 1, .max = 3 },
334 .m = { .min = 79, .max = 126 },
335 .m1 = { .min = 12, .max = 22 },
336 .m2 = { .min = 5, .max = 9 },
337 .p = { .min = 14, .max = 42 },
338 .p1 = { .min = 2, .max = 6 },
339 .p2 = { .dot_limit = 225000,
340 .p2_slow = 7, .p2_fast = 7 },
341 .find_pll = intel_g4x_find_best_PLL,
344 static const intel_limit_t intel_limits_ironlake_display_port = {
345 .dot = { .min = 25000, .max = 350000 },
346 .vco = { .min = 1760000, .max = 3510000},
347 .n = { .min = 1, .max = 2 },
348 .m = { .min = 81, .max = 90 },
349 .m1 = { .min = 12, .max = 22 },
350 .m2 = { .min = 5, .max = 9 },
351 .p = { .min = 10, .max = 20 },
352 .p1 = { .min = 1, .max = 2},
353 .p2 = { .dot_limit = 0,
354 .p2_slow = 10, .p2_fast = 10 },
355 .find_pll = intel_find_pll_ironlake_dp,
358 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
361 struct drm_device *dev = crtc->dev;
362 struct drm_i915_private *dev_priv = dev->dev_private;
363 const intel_limit_t *limit;
365 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
366 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
367 LVDS_CLKB_POWER_UP) {
368 /* LVDS dual channel */
369 if (refclk == 100000)
370 limit = &intel_limits_ironlake_dual_lvds_100m;
372 limit = &intel_limits_ironlake_dual_lvds;
374 if (refclk == 100000)
375 limit = &intel_limits_ironlake_single_lvds_100m;
377 limit = &intel_limits_ironlake_single_lvds;
379 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
381 limit = &intel_limits_ironlake_display_port;
383 limit = &intel_limits_ironlake_dac;
388 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
390 struct drm_device *dev = crtc->dev;
391 struct drm_i915_private *dev_priv = dev->dev_private;
392 const intel_limit_t *limit;
394 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
395 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
397 /* LVDS with dual channel */
398 limit = &intel_limits_g4x_dual_channel_lvds;
400 /* LVDS with dual channel */
401 limit = &intel_limits_g4x_single_channel_lvds;
402 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
403 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
404 limit = &intel_limits_g4x_hdmi;
405 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
406 limit = &intel_limits_g4x_sdvo;
407 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
408 limit = &intel_limits_g4x_display_port;
409 } else /* The option is for other outputs */
410 limit = &intel_limits_i9xx_sdvo;
415 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
417 struct drm_device *dev = crtc->dev;
418 const intel_limit_t *limit;
420 if (HAS_PCH_SPLIT(dev))
421 limit = intel_ironlake_limit(crtc, refclk);
422 else if (IS_G4X(dev)) {
423 limit = intel_g4x_limit(crtc);
424 } else if (IS_PINEVIEW(dev)) {
425 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
426 limit = &intel_limits_pineview_lvds;
428 limit = &intel_limits_pineview_sdvo;
429 } else if (!IS_GEN2(dev)) {
430 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
431 limit = &intel_limits_i9xx_lvds;
433 limit = &intel_limits_i9xx_sdvo;
435 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
436 limit = &intel_limits_i8xx_lvds;
438 limit = &intel_limits_i8xx_dvo;
443 /* m1 is reserved as 0 in Pineview, n is a ring counter */
444 static void pineview_clock(int refclk, intel_clock_t *clock)
446 clock->m = clock->m2 + 2;
447 clock->p = clock->p1 * clock->p2;
448 clock->vco = refclk * clock->m / clock->n;
449 clock->dot = clock->vco / clock->p;
452 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
454 if (IS_PINEVIEW(dev)) {
455 pineview_clock(refclk, clock);
458 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
459 clock->p = clock->p1 * clock->p2;
460 clock->vco = refclk * clock->m / (clock->n + 2);
461 clock->dot = clock->vco / clock->p;
465 * Returns whether any output on the specified pipe is of the specified type
467 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
469 struct drm_device *dev = crtc->dev;
470 struct drm_mode_config *mode_config = &dev->mode_config;
471 struct intel_encoder *encoder;
473 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
474 if (encoder->base.crtc == crtc && encoder->type == type)
480 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
482 * Returns whether the given set of divisors are valid for a given refclk with
483 * the given connectors.
486 static bool intel_PLL_is_valid(struct drm_device *dev,
487 const intel_limit_t *limit,
488 const intel_clock_t *clock)
490 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
491 INTELPllInvalid("p1 out of range\n");
492 if (clock->p < limit->p.min || limit->p.max < clock->p)
493 INTELPllInvalid("p out of range\n");
494 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
495 INTELPllInvalid("m2 out of range\n");
496 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
497 INTELPllInvalid("m1 out of range\n");
498 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
499 INTELPllInvalid("m1 <= m2\n");
500 if (clock->m < limit->m.min || limit->m.max < clock->m)
501 INTELPllInvalid("m out of range\n");
502 if (clock->n < limit->n.min || limit->n.max < clock->n)
503 INTELPllInvalid("n out of range\n");
504 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
505 INTELPllInvalid("vco out of range\n");
506 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
507 * connector, etc., rather than just a single range.
509 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
510 INTELPllInvalid("dot out of range\n");
516 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
517 int target, int refclk, intel_clock_t *match_clock,
518 intel_clock_t *best_clock)
521 struct drm_device *dev = crtc->dev;
522 struct drm_i915_private *dev_priv = dev->dev_private;
526 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
527 (I915_READ(LVDS)) != 0) {
529 * For LVDS, if the panel is on, just rely on its current
530 * settings for dual-channel. We haven't figured out how to
531 * reliably set up different single/dual channel state, if we
534 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
536 clock.p2 = limit->p2.p2_fast;
538 clock.p2 = limit->p2.p2_slow;
540 if (target < limit->p2.dot_limit)
541 clock.p2 = limit->p2.p2_slow;
543 clock.p2 = limit->p2.p2_fast;
546 memset(best_clock, 0, sizeof(*best_clock));
548 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
550 for (clock.m2 = limit->m2.min;
551 clock.m2 <= limit->m2.max; clock.m2++) {
552 /* m1 is always 0 in Pineview */
553 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
555 for (clock.n = limit->n.min;
556 clock.n <= limit->n.max; clock.n++) {
557 for (clock.p1 = limit->p1.min;
558 clock.p1 <= limit->p1.max; clock.p1++) {
561 intel_clock(dev, refclk, &clock);
562 if (!intel_PLL_is_valid(dev, limit,
566 clock.p != match_clock->p)
569 this_err = abs(clock.dot - target);
570 if (this_err < err) {
579 return (err != target);
583 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
584 int target, int refclk, intel_clock_t *match_clock,
585 intel_clock_t *best_clock)
587 struct drm_device *dev = crtc->dev;
588 struct drm_i915_private *dev_priv = dev->dev_private;
592 /* approximately equals target * 0.00585 */
593 int err_most = (target >> 8) + (target >> 9);
596 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
599 if (HAS_PCH_SPLIT(dev))
603 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
605 clock.p2 = limit->p2.p2_fast;
607 clock.p2 = limit->p2.p2_slow;
609 if (target < limit->p2.dot_limit)
610 clock.p2 = limit->p2.p2_slow;
612 clock.p2 = limit->p2.p2_fast;
615 memset(best_clock, 0, sizeof(*best_clock));
616 max_n = limit->n.max;
617 /* based on hardware requirement, prefer smaller n to precision */
618 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
619 /* based on hardware requirement, prefere larger m1,m2 */
620 for (clock.m1 = limit->m1.max;
621 clock.m1 >= limit->m1.min; clock.m1--) {
622 for (clock.m2 = limit->m2.max;
623 clock.m2 >= limit->m2.min; clock.m2--) {
624 for (clock.p1 = limit->p1.max;
625 clock.p1 >= limit->p1.min; clock.p1--) {
628 intel_clock(dev, refclk, &clock);
629 if (!intel_PLL_is_valid(dev, limit,
633 clock.p != match_clock->p)
636 this_err = abs(clock.dot - target);
637 if (this_err < err_most) {
651 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
652 int target, int refclk, intel_clock_t *match_clock,
653 intel_clock_t *best_clock)
655 struct drm_device *dev = crtc->dev;
658 if (target < 200000) {
671 intel_clock(dev, refclk, &clock);
672 memcpy(best_clock, &clock, sizeof(intel_clock_t));
676 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
678 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
679 int target, int refclk, intel_clock_t *match_clock,
680 intel_clock_t *best_clock)
683 if (target < 200000) {
696 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
697 clock.p = (clock.p1 * clock.p2);
698 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
700 memcpy(best_clock, &clock, sizeof(intel_clock_t));
705 * intel_wait_for_vblank - wait for vblank on a given pipe
707 * @pipe: pipe to wait for
709 * Wait for vblank to occur on a given pipe. Needed for various bits of
712 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
714 struct drm_i915_private *dev_priv = dev->dev_private;
715 int pipestat_reg = PIPESTAT(pipe);
717 /* Clear existing vblank status. Note this will clear any other
718 * sticky status fields as well.
720 * This races with i915_driver_irq_handler() with the result
721 * that either function could miss a vblank event. Here it is not
722 * fatal, as we will either wait upon the next vblank interrupt or
723 * timeout. Generally speaking intel_wait_for_vblank() is only
724 * called during modeset at which time the GPU should be idle and
725 * should *not* be performing page flips and thus not waiting on
727 * Currently, the result of us stealing a vblank from the irq
728 * handler is that a single frame will be skipped during swapbuffers.
730 I915_WRITE(pipestat_reg,
731 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
733 /* Wait for vblank interrupt bit to set */
734 if (_intel_wait_for(dev,
735 I915_READ(pipestat_reg) & PIPE_VBLANK_INTERRUPT_STATUS,
737 DRM_DEBUG_KMS("vblank wait timed out\n");
741 * intel_wait_for_pipe_off - wait for pipe to turn off
743 * @pipe: pipe to wait for
745 * After disabling a pipe, we can't wait for vblank in the usual way,
746 * spinning on the vblank interrupt status bit, since we won't actually
747 * see an interrupt when the pipe is disabled.
750 * wait for the pipe register state bit to turn off
753 * wait for the display line value to settle (it usually
754 * ends up stopping at the start of the next frame).
757 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
759 struct drm_i915_private *dev_priv = dev->dev_private;
761 if (INTEL_INFO(dev)->gen >= 4) {
762 int reg = PIPECONF(pipe);
764 /* Wait for the Pipe State to go off */
765 if (_intel_wait_for(dev,
766 (I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0, 100,
768 DRM_DEBUG_KMS("pipe_off wait timed out\n");
770 u32 last_line, line_mask;
771 int reg = PIPEDSL(pipe);
772 unsigned long timeout = jiffies + msecs_to_jiffies(100);
775 line_mask = DSL_LINEMASK_GEN2;
777 line_mask = DSL_LINEMASK_GEN3;
779 /* Wait for the display line to settle */
781 last_line = I915_READ(reg) & line_mask;
783 } while (((I915_READ(reg) & line_mask) != last_line) &&
784 time_after(timeout, jiffies));
785 if (time_after(jiffies, timeout))
786 DRM_DEBUG_KMS("pipe_off wait timed out\n");
790 static const char *state_string(bool enabled)
792 return enabled ? "on" : "off";
795 /* Only for pre-ILK configs */
796 static void assert_pll(struct drm_i915_private *dev_priv,
797 enum i915_pipe pipe, bool state)
804 val = I915_READ(reg);
805 cur_state = !!(val & DPLL_VCO_ENABLE);
806 if (cur_state != state)
807 kprintf("PLL state assertion failure (expected %s, current %s)\n",
808 state_string(state), state_string(cur_state));
810 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
811 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
814 static void assert_pch_pll(struct drm_i915_private *dev_priv,
815 enum i915_pipe pipe, bool state)
821 if (HAS_PCH_CPT(dev_priv->dev)) {
824 pch_dpll = I915_READ(PCH_DPLL_SEL);
826 /* Make sure the selected PLL is enabled to the transcoder */
827 KASSERT(((pch_dpll >> (4 * pipe)) & 8) != 0,
828 ("transcoder %d PLL not enabled\n", pipe));
830 /* Convert the transcoder pipe number to a pll pipe number */
831 pipe = (pch_dpll >> (4 * pipe)) & 1;
834 reg = _PCH_DPLL(pipe);
835 val = I915_READ(reg);
836 cur_state = !!(val & DPLL_VCO_ENABLE);
837 if (cur_state != state)
838 kprintf("PCH PLL state assertion failure (expected %s, current %s)\n",
839 state_string(state), state_string(cur_state));
841 #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
842 #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
844 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
845 enum i915_pipe pipe, bool state)
851 reg = FDI_TX_CTL(pipe);
852 val = I915_READ(reg);
853 cur_state = !!(val & FDI_TX_ENABLE);
854 if (cur_state != state)
855 kprintf("FDI TX state assertion failure (expected %s, current %s)\n",
856 state_string(state), state_string(cur_state));
858 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
859 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
861 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
862 enum i915_pipe pipe, bool state)
868 reg = FDI_RX_CTL(pipe);
869 val = I915_READ(reg);
870 cur_state = !!(val & FDI_RX_ENABLE);
871 if (cur_state != state)
872 kprintf("FDI RX state assertion failure (expected %s, current %s)\n",
873 state_string(state), state_string(cur_state));
875 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
876 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
878 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
884 /* ILK FDI PLL is always enabled */
885 if (dev_priv->info->gen == 5)
888 reg = FDI_TX_CTL(pipe);
889 val = I915_READ(reg);
890 if (!(val & FDI_TX_PLL_ENABLE))
891 kprintf("FDI TX PLL assertion failure, should be active but is disabled\n");
894 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
900 reg = FDI_RX_CTL(pipe);
901 val = I915_READ(reg);
902 if (!(val & FDI_RX_PLL_ENABLE))
903 kprintf("FDI RX PLL assertion failure, should be active but is disabled\n");
906 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
909 int pp_reg, lvds_reg;
911 enum i915_pipe panel_pipe = PIPE_A;
914 if (HAS_PCH_SPLIT(dev_priv->dev)) {
915 pp_reg = PCH_PP_CONTROL;
922 val = I915_READ(pp_reg);
923 if (!(val & PANEL_POWER_ON) ||
924 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
927 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
930 if (panel_pipe == pipe && locked)
931 kprintf("panel assertion failure, pipe %c regs locked\n",
935 void assert_pipe(struct drm_i915_private *dev_priv,
936 enum i915_pipe pipe, bool state)
942 /* if we need the pipe A quirk it must be always on */
943 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
946 reg = PIPECONF(pipe);
947 val = I915_READ(reg);
948 cur_state = !!(val & PIPECONF_ENABLE);
949 if (cur_state != state)
950 kprintf("pipe %c assertion failure (expected %s, current %s)\n",
951 pipe_name(pipe), state_string(state), state_string(cur_state));
954 static void assert_plane(struct drm_i915_private *dev_priv,
955 enum plane plane, bool state)
961 reg = DSPCNTR(plane);
962 val = I915_READ(reg);
963 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
964 if (cur_state != state)
965 kprintf("plane %c assertion failure, (expected %s, current %s)\n",
966 plane_name(plane), state_string(state), state_string(cur_state));
969 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
970 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
972 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
979 /* Planes are fixed to pipes on ILK+ */
980 if (HAS_PCH_SPLIT(dev_priv->dev)) {
982 val = I915_READ(reg);
983 if ((val & DISPLAY_PLANE_ENABLE) != 0)
984 kprintf("plane %c assertion failure, should be disabled but not\n",
989 /* Need to check both planes against the pipe */
990 for (i = 0; i < 2; i++) {
992 val = I915_READ(reg);
993 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
994 DISPPLANE_SEL_PIPE_SHIFT;
995 if ((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe)
996 kprintf("plane %c assertion failure, should be off on pipe %c but is still active\n",
997 plane_name(i), pipe_name(pipe));
1001 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1006 val = I915_READ(PCH_DREF_CONTROL);
1007 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1008 DREF_SUPERSPREAD_SOURCE_MASK));
1010 kprintf("PCH refclk assertion failure, should be active but is disabled\n");
1013 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1014 enum i915_pipe pipe)
1020 reg = TRANSCONF(pipe);
1021 val = I915_READ(reg);
1022 enabled = !!(val & TRANS_ENABLE);
1024 kprintf("transcoder assertion failed, should be off on pipe %c but is still active\n",
1028 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1029 enum i915_pipe pipe, u32 val)
1031 if ((val & PORT_ENABLE) == 0)
1034 if (HAS_PCH_CPT(dev_priv->dev)) {
1035 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1038 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1044 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1045 enum i915_pipe pipe, u32 val)
1047 if ((val & LVDS_PORT_EN) == 0)
1050 if (HAS_PCH_CPT(dev_priv->dev)) {
1051 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1054 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1060 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1061 enum i915_pipe pipe, u32 val)
1063 if ((val & ADPA_DAC_ENABLE) == 0)
1065 if (HAS_PCH_CPT(dev_priv->dev)) {
1066 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1069 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1075 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1076 enum i915_pipe pipe, u32 port_sel, u32 val)
1078 if ((val & DP_PORT_EN) == 0)
1081 if (HAS_PCH_CPT(dev_priv->dev)) {
1082 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1083 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1084 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1087 if ((val & DP_PIPE_MASK) != (pipe << 30))
1093 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1094 enum i915_pipe pipe, int reg, u32 port_sel)
1096 u32 val = I915_READ(reg);
1097 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val))
1098 kprintf("PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1099 reg, pipe_name(pipe));
1102 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1103 enum i915_pipe pipe, int reg)
1105 u32 val = I915_READ(reg);
1106 if (hdmi_pipe_enabled(dev_priv, val, pipe))
1107 kprintf("PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1108 reg, pipe_name(pipe));
1111 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1112 enum i915_pipe pipe)
1117 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1118 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1119 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1122 val = I915_READ(reg);
1123 if (adpa_pipe_enabled(dev_priv, val, pipe))
1124 kprintf("PCH VGA enabled on transcoder %c, should be disabled\n",
1128 val = I915_READ(reg);
1129 if (lvds_pipe_enabled(dev_priv, val, pipe))
1130 kprintf("PCH LVDS enabled on transcoder %c, should be disabled\n",
1133 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1134 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1135 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1139 * intel_enable_pll - enable a PLL
1140 * @dev_priv: i915 private structure
1141 * @pipe: pipe PLL to enable
1143 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1144 * make sure the PLL reg is writable first though, since the panel write
1145 * protect mechanism may be enabled.
1147 * Note! This is for pre-ILK only.
1149 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1154 /* No really, not for ILK+ */
1155 KASSERT(dev_priv->info->gen < 5, ("Wrong device gen"));
1157 /* PLL is protected by panel, make sure we can write it */
1158 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1159 assert_panel_unlocked(dev_priv, pipe);
1162 val = I915_READ(reg);
1163 val |= DPLL_VCO_ENABLE;
1165 /* We do this three times for luck */
1166 I915_WRITE(reg, val);
1168 DELAY(150); /* wait for warmup */
1169 I915_WRITE(reg, val);
1171 DELAY(150); /* wait for warmup */
1172 I915_WRITE(reg, val);
1174 DELAY(150); /* wait for warmup */
1178 * intel_disable_pll - disable a PLL
1179 * @dev_priv: i915 private structure
1180 * @pipe: pipe PLL to disable
1182 * Disable the PLL for @pipe, making sure the pipe is off first.
1184 * Note! This is for pre-ILK only.
1186 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1191 /* Don't disable pipe A or pipe A PLLs if needed */
1192 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1195 /* Make sure the pipe isn't still relying on us */
1196 assert_pipe_disabled(dev_priv, pipe);
1199 val = I915_READ(reg);
1200 val &= ~DPLL_VCO_ENABLE;
1201 I915_WRITE(reg, val);
1206 * intel_enable_pch_pll - enable PCH PLL
1207 * @dev_priv: i915 private structure
1208 * @pipe: pipe PLL to enable
1210 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1211 * drives the transcoder clock.
1213 static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1214 enum i915_pipe pipe)
1222 /* PCH only available on ILK+ */
1223 KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1225 /* PCH refclock must be enabled first */
1226 assert_pch_refclk_enabled(dev_priv);
1228 reg = _PCH_DPLL(pipe);
1229 val = I915_READ(reg);
1230 val |= DPLL_VCO_ENABLE;
1231 I915_WRITE(reg, val);
1236 static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1237 enum i915_pipe pipe)
1240 u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1241 pll_sel = TRANSC_DPLL_ENABLE;
1246 /* PCH only available on ILK+ */
1247 KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1249 /* Make sure transcoder isn't still depending on us */
1250 assert_transcoder_disabled(dev_priv, pipe);
1253 pll_sel |= TRANSC_DPLLA_SEL;
1255 pll_sel |= TRANSC_DPLLB_SEL;
1258 if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1261 reg = _PCH_DPLL(pipe);
1262 val = I915_READ(reg);
1263 val &= ~DPLL_VCO_ENABLE;
1264 I915_WRITE(reg, val);
1269 static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1270 enum i915_pipe pipe)
1273 u32 val, pipeconf_val;
1274 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1276 /* PCH only available on ILK+ */
1277 KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1279 /* Make sure PCH DPLL is enabled */
1280 assert_pch_pll_enabled(dev_priv, pipe);
1282 /* FDI must be feeding us bits for PCH ports */
1283 assert_fdi_tx_enabled(dev_priv, pipe);
1284 assert_fdi_rx_enabled(dev_priv, pipe);
1287 reg = TRANSCONF(pipe);
1288 val = I915_READ(reg);
1289 pipeconf_val = I915_READ(PIPECONF(pipe));
1291 if (HAS_PCH_IBX(dev_priv->dev)) {
1293 * make the BPC in transcoder be consistent with
1294 * that in pipeconf reg.
1296 val &= ~PIPE_BPC_MASK;
1297 val |= pipeconf_val & PIPE_BPC_MASK;
1300 val &= ~TRANS_INTERLACE_MASK;
1301 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1302 if (HAS_PCH_IBX(dev_priv->dev) &&
1303 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1304 val |= TRANS_LEGACY_INTERLACED_ILK;
1306 val |= TRANS_INTERLACED;
1308 val |= TRANS_PROGRESSIVE;
1310 I915_WRITE(reg, val | TRANS_ENABLE);
1311 if (_intel_wait_for(dev_priv->dev, I915_READ(reg) & TRANS_STATE_ENABLE,
1313 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1316 static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1317 enum i915_pipe pipe)
1322 /* FDI relies on the transcoder */
1323 assert_fdi_tx_disabled(dev_priv, pipe);
1324 assert_fdi_rx_disabled(dev_priv, pipe);
1326 /* Ports must be off as well */
1327 assert_pch_ports_disabled(dev_priv, pipe);
1329 reg = TRANSCONF(pipe);
1330 val = I915_READ(reg);
1331 val &= ~TRANS_ENABLE;
1332 I915_WRITE(reg, val);
1333 /* wait for PCH transcoder off, transcoder state */
1334 if (_intel_wait_for(dev_priv->dev,
1335 (I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50,
1337 DRM_ERROR("failed to disable transcoder %d\n", pipe);
1341 * intel_enable_pipe - enable a pipe, asserting requirements
1342 * @dev_priv: i915 private structure
1343 * @pipe: pipe to enable
1344 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1346 * Enable @pipe, making sure that various hardware specific requirements
1347 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1349 * @pipe should be %PIPE_A or %PIPE_B.
1351 * Will wait until the pipe is actually running (i.e. first vblank) before
1354 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum i915_pipe pipe,
1361 * A pipe without a PLL won't actually be able to drive bits from
1362 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1365 if (!HAS_PCH_SPLIT(dev_priv->dev))
1366 assert_pll_enabled(dev_priv, pipe);
1369 /* if driving the PCH, we need FDI enabled */
1370 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1371 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1373 /* FIXME: assert CPU port conditions for SNB+ */
1376 reg = PIPECONF(pipe);
1377 val = I915_READ(reg);
1378 if (val & PIPECONF_ENABLE)
1381 I915_WRITE(reg, val | PIPECONF_ENABLE);
1382 intel_wait_for_vblank(dev_priv->dev, pipe);
1386 * intel_disable_pipe - disable a pipe, asserting requirements
1387 * @dev_priv: i915 private structure
1388 * @pipe: pipe to disable
1390 * Disable @pipe, making sure that various hardware specific requirements
1391 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1393 * @pipe should be %PIPE_A or %PIPE_B.
1395 * Will wait until the pipe has shut down before returning.
1397 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1398 enum i915_pipe pipe)
1404 * Make sure planes won't keep trying to pump pixels to us,
1405 * or we might hang the display.
1407 assert_planes_disabled(dev_priv, pipe);
1409 /* Don't disable pipe A or pipe A PLLs if needed */
1410 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1413 reg = PIPECONF(pipe);
1414 val = I915_READ(reg);
1415 if ((val & PIPECONF_ENABLE) == 0)
1418 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1419 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1423 * Plane regs are double buffered, going from enabled->disabled needs a
1424 * trigger in order to latch. The display address reg provides this.
1426 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1429 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1430 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1434 * intel_enable_plane - enable a display plane on a given pipe
1435 * @dev_priv: i915 private structure
1436 * @plane: plane to enable
1437 * @pipe: pipe being fed
1439 * Enable @plane on @pipe, making sure that @pipe is running first.
1441 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1442 enum plane plane, enum i915_pipe pipe)
1447 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1448 assert_pipe_enabled(dev_priv, pipe);
1450 reg = DSPCNTR(plane);
1451 val = I915_READ(reg);
1452 if (val & DISPLAY_PLANE_ENABLE)
1455 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1456 intel_flush_display_plane(dev_priv, plane);
1457 intel_wait_for_vblank(dev_priv->dev, pipe);
1461 * intel_disable_plane - disable a display plane
1462 * @dev_priv: i915 private structure
1463 * @plane: plane to disable
1464 * @pipe: pipe consuming the data
1466 * Disable @plane; should be an independent operation.
1468 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1469 enum plane plane, enum i915_pipe pipe)
1474 reg = DSPCNTR(plane);
1475 val = I915_READ(reg);
1476 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1479 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1480 intel_flush_display_plane(dev_priv, plane);
1481 intel_wait_for_vblank(dev_priv->dev, pipe);
1484 static void disable_pch_dp(struct drm_i915_private *dev_priv,
1485 enum i915_pipe pipe, int reg, u32 port_sel)
1487 u32 val = I915_READ(reg);
1488 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
1489 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
1490 I915_WRITE(reg, val & ~DP_PORT_EN);
1494 static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1495 enum i915_pipe pipe, int reg)
1497 u32 val = I915_READ(reg);
1498 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
1499 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1501 I915_WRITE(reg, val & ~PORT_ENABLE);
1505 /* Disable any ports connected to this transcoder */
1506 static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1507 enum i915_pipe pipe)
1511 val = I915_READ(PCH_PP_CONTROL);
1512 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1514 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1515 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1516 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1519 val = I915_READ(reg);
1520 if (adpa_pipe_enabled(dev_priv, val, pipe))
1521 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1524 val = I915_READ(reg);
1525 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1526 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
1527 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1532 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1533 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1534 disable_pch_hdmi(dev_priv, pipe, HDMID);
1538 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1539 struct drm_i915_gem_object *obj,
1540 struct intel_ring_buffer *pipelined)
1542 struct drm_i915_private *dev_priv = dev->dev_private;
1546 alignment = 0; /* shut gcc */
1547 switch (obj->tiling_mode) {
1548 case I915_TILING_NONE:
1549 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1550 alignment = 128 * 1024;
1551 else if (INTEL_INFO(dev)->gen >= 4)
1552 alignment = 4 * 1024;
1554 alignment = 64 * 1024;
1557 /* pin() will align the object as required by fence */
1561 /* FIXME: Is this true? */
1562 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1565 KASSERT(0, ("Wrong tiling for fb obj"));
1568 dev_priv->mm.interruptible = false;
1569 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1571 goto err_interruptible;
1573 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1574 * fence, whereas 965+ only requires a fence if using
1575 * framebuffer compression. For simplicity, we always install
1576 * a fence as the cost is not that onerous.
1578 if (obj->tiling_mode != I915_TILING_NONE) {
1579 ret = i915_gem_object_get_fence(obj, pipelined);
1583 i915_gem_object_pin_fence(obj);
1586 dev_priv->mm.interruptible = true;
1590 i915_gem_object_unpin(obj);
1592 dev_priv->mm.interruptible = true;
1596 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1598 i915_gem_object_unpin_fence(obj);
1599 i915_gem_object_unpin(obj);
1602 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1603 * is assumed to be a power-of-two. */
1604 unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1608 int tile_rows, tiles;
1612 tiles = *x / (512/bpp);
1615 return tile_rows * pitch * 8 + tiles * 4096;
1618 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1621 struct drm_device *dev = crtc->dev;
1622 struct drm_i915_private *dev_priv = dev->dev_private;
1623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1624 struct intel_framebuffer *intel_fb;
1625 struct drm_i915_gem_object *obj;
1626 int plane = intel_crtc->plane;
1627 unsigned long Start, Offset;
1636 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1640 intel_fb = to_intel_framebuffer(fb);
1641 obj = intel_fb->obj;
1643 reg = DSPCNTR(plane);
1644 dspcntr = I915_READ(reg);
1645 /* Mask out pixel format bits in case we change it */
1646 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1647 switch (fb->bits_per_pixel) {
1649 dspcntr |= DISPPLANE_8BPP;
1652 if (fb->depth == 15)
1653 dspcntr |= DISPPLANE_BGRX555;
1655 dspcntr |= DISPPLANE_BGRX565;
1659 dspcntr |= DISPPLANE_BGRX888;
1662 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1665 if (INTEL_INFO(dev)->gen >= 4) {
1666 if (obj->tiling_mode != I915_TILING_NONE)
1667 dspcntr |= DISPPLANE_TILED;
1669 dspcntr &= ~DISPPLANE_TILED;
1672 I915_WRITE(reg, dspcntr);
1674 Start = obj->gtt_offset;
1675 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1677 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1678 Start, Offset, x, y, fb->pitches[0]);
1679 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1680 if (INTEL_INFO(dev)->gen >= 4) {
1681 I915_WRITE(DSPSURF(plane), Start);
1682 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1683 I915_WRITE(DSPADDR(plane), Offset);
1685 I915_WRITE(DSPADDR(plane), Start + Offset);
1691 static int ironlake_update_plane(struct drm_crtc *crtc,
1692 struct drm_framebuffer *fb, int x, int y)
1694 struct drm_device *dev = crtc->dev;
1695 struct drm_i915_private *dev_priv = dev->dev_private;
1696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1697 struct intel_framebuffer *intel_fb;
1698 struct drm_i915_gem_object *obj;
1699 int plane = intel_crtc->plane;
1700 unsigned long Start, Offset;
1710 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1714 intel_fb = to_intel_framebuffer(fb);
1715 obj = intel_fb->obj;
1717 reg = DSPCNTR(plane);
1718 dspcntr = I915_READ(reg);
1719 /* Mask out pixel format bits in case we change it */
1720 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1721 switch (fb->bits_per_pixel) {
1723 dspcntr |= DISPPLANE_8BPP;
1726 if (fb->depth != 16) {
1727 DRM_ERROR("bpp 16, depth %d\n", fb->depth);
1731 dspcntr |= DISPPLANE_BGRX565;
1735 if (fb->depth == 24)
1736 dspcntr |= DISPPLANE_BGRX888;
1737 else if (fb->depth == 30)
1738 dspcntr |= DISPPLANE_BGRX101010;
1740 DRM_ERROR("bpp %d depth %d\n", fb->bits_per_pixel,
1746 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1750 if (obj->tiling_mode != I915_TILING_NONE)
1751 dspcntr |= DISPPLANE_TILED;
1753 dspcntr &= ~DISPPLANE_TILED;
1756 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1758 I915_WRITE(reg, dspcntr);
1760 Start = obj->gtt_offset;
1761 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1763 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1764 Start, Offset, x, y, fb->pitches[0]);
1765 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1766 I915_WRITE(DSPSURF(plane), Start);
1767 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1768 I915_WRITE(DSPADDR(plane), Offset);
1774 /* Assume fb object is pinned & idle & fenced and just update base pointers */
1776 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1777 int x, int y, enum mode_set_atomic state)
1779 struct drm_device *dev = crtc->dev;
1780 struct drm_i915_private *dev_priv = dev->dev_private;
1782 if (dev_priv->display.disable_fbc)
1783 dev_priv->display.disable_fbc(dev);
1784 intel_increase_pllclock(crtc);
1786 return dev_priv->display.update_plane(crtc, fb, x, y);
1790 intel_finish_fb(struct drm_framebuffer *old_fb)
1792 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
1793 struct drm_device *dev = obj->base.dev;
1794 struct drm_i915_private *dev_priv = dev->dev_private;
1795 bool was_interruptible = dev_priv->mm.interruptible;
1798 /* XXX */ lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1799 while (!atomic_read(&dev_priv->mm.wedged) &&
1800 atomic_read(&obj->pending_flip) != 0) {
1801 lksleep(&obj->pending_flip, &dev->event_lock,
1804 /* XXX */ lockmgr(&dev->event_lock, LK_RELEASE);
1806 /* Big Hammer, we also need to ensure that any pending
1807 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1808 * current scanout is retired before unpinning the old
1811 * This should only fail upon a hung GPU, in which case we
1812 * can safely continue.
1814 dev_priv->mm.interruptible = false;
1815 ret = i915_gem_object_finish_gpu(obj);
1816 dev_priv->mm.interruptible = was_interruptible;
1821 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1822 struct drm_framebuffer *old_fb)
1824 struct drm_device *dev = crtc->dev;
1826 struct drm_i915_master_private *master_priv;
1828 drm_i915_private_t *dev_priv = dev->dev_private;
1830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1835 DRM_ERROR("No FB bound\n");
1839 switch (intel_crtc->plane) {
1844 if (IS_IVYBRIDGE(dev))
1846 /* fall through otherwise */
1848 DRM_ERROR("no plane for crtc\n");
1853 ret = intel_pin_and_fence_fb_obj(dev,
1854 to_intel_framebuffer(crtc->fb)->obj,
1858 DRM_ERROR("pin & fence failed\n");
1863 intel_finish_fb(old_fb);
1865 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1866 LEAVE_ATOMIC_MODE_SET);
1868 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
1870 DRM_ERROR("failed to update base address\n");
1875 intel_wait_for_vblank(dev, intel_crtc->pipe);
1876 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
1882 if (!dev->primary->master)
1885 master_priv = dev->primary->master->driver_priv;
1886 if (!master_priv->sarea_priv)
1889 if (intel_crtc->pipe) {
1890 master_priv->sarea_priv->pipeB_x = x;
1891 master_priv->sarea_priv->pipeB_y = y;
1893 master_priv->sarea_priv->pipeA_x = x;
1894 master_priv->sarea_priv->pipeA_y = y;
1898 if (!dev_priv->sarea_priv)
1901 if (intel_crtc->pipe) {
1902 dev_priv->sarea_priv->planeB_x = x;
1903 dev_priv->sarea_priv->planeB_y = y;
1905 dev_priv->sarea_priv->planeA_x = x;
1906 dev_priv->sarea_priv->planeA_y = y;
1913 static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
1915 struct drm_device *dev = crtc->dev;
1916 struct drm_i915_private *dev_priv = dev->dev_private;
1919 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
1920 dpa_ctl = I915_READ(DP_A);
1921 dpa_ctl &= ~DP_PLL_FREQ_MASK;
1923 if (clock < 200000) {
1925 dpa_ctl |= DP_PLL_FREQ_160MHZ;
1926 /* workaround for 160Mhz:
1927 1) program 0x4600c bits 15:0 = 0x8124
1928 2) program 0x46010 bit 0 = 1
1929 3) program 0x46034 bit 24 = 1
1930 4) program 0x64000 bit 14 = 1
1932 temp = I915_READ(0x4600c);
1934 I915_WRITE(0x4600c, temp | 0x8124);
1936 temp = I915_READ(0x46010);
1937 I915_WRITE(0x46010, temp | 1);
1939 temp = I915_READ(0x46034);
1940 I915_WRITE(0x46034, temp | (1 << 24));
1942 dpa_ctl |= DP_PLL_FREQ_270MHZ;
1944 I915_WRITE(DP_A, dpa_ctl);
1950 static void intel_fdi_normal_train(struct drm_crtc *crtc)
1952 struct drm_device *dev = crtc->dev;
1953 struct drm_i915_private *dev_priv = dev->dev_private;
1954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1955 int pipe = intel_crtc->pipe;
1958 /* enable normal train */
1959 reg = FDI_TX_CTL(pipe);
1960 temp = I915_READ(reg);
1961 if (IS_IVYBRIDGE(dev)) {
1962 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
1963 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
1965 temp &= ~FDI_LINK_TRAIN_NONE;
1966 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1968 I915_WRITE(reg, temp);
1970 reg = FDI_RX_CTL(pipe);
1971 temp = I915_READ(reg);
1972 if (HAS_PCH_CPT(dev)) {
1973 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1974 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1976 temp &= ~FDI_LINK_TRAIN_NONE;
1977 temp |= FDI_LINK_TRAIN_NONE;
1979 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1981 /* wait one idle pattern time */
1985 /* IVB wants error correction enabled */
1986 if (IS_IVYBRIDGE(dev))
1987 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
1988 FDI_FE_ERRC_ENABLE);
1991 static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
1993 struct drm_i915_private *dev_priv = dev->dev_private;
1994 u32 flags = I915_READ(SOUTH_CHICKEN1);
1996 flags |= FDI_PHASE_SYNC_OVR(pipe);
1997 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
1998 flags |= FDI_PHASE_SYNC_EN(pipe);
1999 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2000 POSTING_READ(SOUTH_CHICKEN1);
2003 /* The FDI link training functions for ILK/Ibexpeak. */
2004 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2006 struct drm_device *dev = crtc->dev;
2007 struct drm_i915_private *dev_priv = dev->dev_private;
2008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2009 int pipe = intel_crtc->pipe;
2010 int plane = intel_crtc->plane;
2011 u32 reg, temp, tries;
2013 /* FDI needs bits from pipe & plane first */
2014 assert_pipe_enabled(dev_priv, pipe);
2015 assert_plane_enabled(dev_priv, plane);
2017 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2019 reg = FDI_RX_IMR(pipe);
2020 temp = I915_READ(reg);
2021 temp &= ~FDI_RX_SYMBOL_LOCK;
2022 temp &= ~FDI_RX_BIT_LOCK;
2023 I915_WRITE(reg, temp);
2027 /* enable CPU FDI TX and PCH FDI RX */
2028 reg = FDI_TX_CTL(pipe);
2029 temp = I915_READ(reg);
2031 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2032 temp &= ~FDI_LINK_TRAIN_NONE;
2033 temp |= FDI_LINK_TRAIN_PATTERN_1;
2034 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2036 reg = FDI_RX_CTL(pipe);
2037 temp = I915_READ(reg);
2038 temp &= ~FDI_LINK_TRAIN_NONE;
2039 temp |= FDI_LINK_TRAIN_PATTERN_1;
2040 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2045 /* Ironlake workaround, enable clock pointer after FDI enable*/
2046 if (HAS_PCH_IBX(dev)) {
2047 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2048 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2049 FDI_RX_PHASE_SYNC_POINTER_EN);
2052 reg = FDI_RX_IIR(pipe);
2053 for (tries = 0; tries < 5; tries++) {
2054 temp = I915_READ(reg);
2055 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2057 if ((temp & FDI_RX_BIT_LOCK)) {
2058 DRM_DEBUG_KMS("FDI train 1 done.\n");
2059 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2064 DRM_ERROR("FDI train 1 fail!\n");
2067 reg = FDI_TX_CTL(pipe);
2068 temp = I915_READ(reg);
2069 temp &= ~FDI_LINK_TRAIN_NONE;
2070 temp |= FDI_LINK_TRAIN_PATTERN_2;
2071 I915_WRITE(reg, temp);
2073 reg = FDI_RX_CTL(pipe);
2074 temp = I915_READ(reg);
2075 temp &= ~FDI_LINK_TRAIN_NONE;
2076 temp |= FDI_LINK_TRAIN_PATTERN_2;
2077 I915_WRITE(reg, temp);
2082 reg = FDI_RX_IIR(pipe);
2083 for (tries = 0; tries < 5; tries++) {
2084 temp = I915_READ(reg);
2085 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2087 if (temp & FDI_RX_SYMBOL_LOCK) {
2088 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2089 DRM_DEBUG_KMS("FDI train 2 done.\n");
2094 DRM_ERROR("FDI train 2 fail!\n");
2096 DRM_DEBUG_KMS("FDI train done\n");
2100 static const int snb_b_fdi_train_param[] = {
2101 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2102 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2103 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2104 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2107 /* The FDI link training functions for SNB/Cougarpoint. */
2108 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2110 struct drm_device *dev = crtc->dev;
2111 struct drm_i915_private *dev_priv = dev->dev_private;
2112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2113 int pipe = intel_crtc->pipe;
2116 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2118 reg = FDI_RX_IMR(pipe);
2119 temp = I915_READ(reg);
2120 temp &= ~FDI_RX_SYMBOL_LOCK;
2121 temp &= ~FDI_RX_BIT_LOCK;
2122 I915_WRITE(reg, temp);
2127 /* enable CPU FDI TX and PCH FDI RX */
2128 reg = FDI_TX_CTL(pipe);
2129 temp = I915_READ(reg);
2131 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2132 temp &= ~FDI_LINK_TRAIN_NONE;
2133 temp |= FDI_LINK_TRAIN_PATTERN_1;
2134 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2136 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2137 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2139 reg = FDI_RX_CTL(pipe);
2140 temp = I915_READ(reg);
2141 if (HAS_PCH_CPT(dev)) {
2142 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2143 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2145 temp &= ~FDI_LINK_TRAIN_NONE;
2146 temp |= FDI_LINK_TRAIN_PATTERN_1;
2148 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2153 if (HAS_PCH_CPT(dev))
2154 cpt_phase_pointer_enable(dev, pipe);
2156 for (i = 0; i < 4; i++) {
2157 reg = FDI_TX_CTL(pipe);
2158 temp = I915_READ(reg);
2159 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2160 temp |= snb_b_fdi_train_param[i];
2161 I915_WRITE(reg, temp);
2166 reg = FDI_RX_IIR(pipe);
2167 temp = I915_READ(reg);
2168 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2170 if (temp & FDI_RX_BIT_LOCK) {
2171 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2172 DRM_DEBUG_KMS("FDI train 1 done.\n");
2177 DRM_ERROR("FDI train 1 fail!\n");
2180 reg = FDI_TX_CTL(pipe);
2181 temp = I915_READ(reg);
2182 temp &= ~FDI_LINK_TRAIN_NONE;
2183 temp |= FDI_LINK_TRAIN_PATTERN_2;
2185 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2187 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2189 I915_WRITE(reg, temp);
2191 reg = FDI_RX_CTL(pipe);
2192 temp = I915_READ(reg);
2193 if (HAS_PCH_CPT(dev)) {
2194 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2195 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2197 temp &= ~FDI_LINK_TRAIN_NONE;
2198 temp |= FDI_LINK_TRAIN_PATTERN_2;
2200 I915_WRITE(reg, temp);
2205 for (i = 0; i < 4; i++) {
2206 reg = FDI_TX_CTL(pipe);
2207 temp = I915_READ(reg);
2208 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2209 temp |= snb_b_fdi_train_param[i];
2210 I915_WRITE(reg, temp);
2215 reg = FDI_RX_IIR(pipe);
2216 temp = I915_READ(reg);
2217 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2219 if (temp & FDI_RX_SYMBOL_LOCK) {
2220 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2221 DRM_DEBUG_KMS("FDI train 2 done.\n");
2226 DRM_ERROR("FDI train 2 fail!\n");
2228 DRM_DEBUG_KMS("FDI train done.\n");
2231 /* Manual link training for Ivy Bridge A0 parts */
2232 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2234 struct drm_device *dev = crtc->dev;
2235 struct drm_i915_private *dev_priv = dev->dev_private;
2236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2237 int pipe = intel_crtc->pipe;
2240 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2242 reg = FDI_RX_IMR(pipe);
2243 temp = I915_READ(reg);
2244 temp &= ~FDI_RX_SYMBOL_LOCK;
2245 temp &= ~FDI_RX_BIT_LOCK;
2246 I915_WRITE(reg, temp);
2251 /* enable CPU FDI TX and PCH FDI RX */
2252 reg = FDI_TX_CTL(pipe);
2253 temp = I915_READ(reg);
2255 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2256 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2257 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2258 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2259 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2260 temp |= FDI_COMPOSITE_SYNC;
2261 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2263 reg = FDI_RX_CTL(pipe);
2264 temp = I915_READ(reg);
2265 temp &= ~FDI_LINK_TRAIN_AUTO;
2266 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2267 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2268 temp |= FDI_COMPOSITE_SYNC;
2269 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2274 for (i = 0; i < 4; i++) {
2275 reg = FDI_TX_CTL(pipe);
2276 temp = I915_READ(reg);
2277 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2278 temp |= snb_b_fdi_train_param[i];
2279 I915_WRITE(reg, temp);
2284 reg = FDI_RX_IIR(pipe);
2285 temp = I915_READ(reg);
2286 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2288 if (temp & FDI_RX_BIT_LOCK ||
2289 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2290 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2291 DRM_DEBUG_KMS("FDI train 1 done.\n");
2296 DRM_ERROR("FDI train 1 fail!\n");
2299 reg = FDI_TX_CTL(pipe);
2300 temp = I915_READ(reg);
2301 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2302 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2303 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2304 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2305 I915_WRITE(reg, temp);
2307 reg = FDI_RX_CTL(pipe);
2308 temp = I915_READ(reg);
2309 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2310 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2311 I915_WRITE(reg, temp);
2316 for (i = 0; i < 4; i++ ) {
2317 reg = FDI_TX_CTL(pipe);
2318 temp = I915_READ(reg);
2319 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2320 temp |= snb_b_fdi_train_param[i];
2321 I915_WRITE(reg, temp);
2326 reg = FDI_RX_IIR(pipe);
2327 temp = I915_READ(reg);
2328 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2330 if (temp & FDI_RX_SYMBOL_LOCK) {
2331 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2332 DRM_DEBUG_KMS("FDI train 2 done.\n");
2337 DRM_ERROR("FDI train 2 fail!\n");
2339 DRM_DEBUG_KMS("FDI train done.\n");
2342 static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
2344 struct drm_device *dev = crtc->dev;
2345 struct drm_i915_private *dev_priv = dev->dev_private;
2346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2347 int pipe = intel_crtc->pipe;
2350 /* Write the TU size bits so error detection works */
2351 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2352 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2354 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2355 reg = FDI_RX_CTL(pipe);
2356 temp = I915_READ(reg);
2357 temp &= ~((0x7 << 19) | (0x7 << 16));
2358 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2359 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2360 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2365 /* Switch from Rawclk to PCDclk */
2366 temp = I915_READ(reg);
2367 I915_WRITE(reg, temp | FDI_PCDCLK);
2372 /* Enable CPU FDI TX PLL, always on for Ironlake */
2373 reg = FDI_TX_CTL(pipe);
2374 temp = I915_READ(reg);
2375 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2376 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2383 static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2385 struct drm_i915_private *dev_priv = dev->dev_private;
2386 u32 flags = I915_READ(SOUTH_CHICKEN1);
2388 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2389 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2390 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2391 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2392 POSTING_READ(SOUTH_CHICKEN1);
2395 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2397 struct drm_device *dev = crtc->dev;
2398 struct drm_i915_private *dev_priv = dev->dev_private;
2399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2400 int pipe = intel_crtc->pipe;
2403 /* disable CPU FDI tx and PCH FDI rx */
2404 reg = FDI_TX_CTL(pipe);
2405 temp = I915_READ(reg);
2406 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2409 reg = FDI_RX_CTL(pipe);
2410 temp = I915_READ(reg);
2411 temp &= ~(0x7 << 16);
2412 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2413 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2418 /* Ironlake workaround, disable clock pointer after downing FDI */
2419 if (HAS_PCH_IBX(dev)) {
2420 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2421 I915_WRITE(FDI_RX_CHICKEN(pipe),
2422 I915_READ(FDI_RX_CHICKEN(pipe) &
2423 ~FDI_RX_PHASE_SYNC_POINTER_EN));
2424 } else if (HAS_PCH_CPT(dev)) {
2425 cpt_phase_pointer_disable(dev, pipe);
2428 /* still set train pattern 1 */
2429 reg = FDI_TX_CTL(pipe);
2430 temp = I915_READ(reg);
2431 temp &= ~FDI_LINK_TRAIN_NONE;
2432 temp |= FDI_LINK_TRAIN_PATTERN_1;
2433 I915_WRITE(reg, temp);
2435 reg = FDI_RX_CTL(pipe);
2436 temp = I915_READ(reg);
2437 if (HAS_PCH_CPT(dev)) {
2438 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2439 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2441 temp &= ~FDI_LINK_TRAIN_NONE;
2442 temp |= FDI_LINK_TRAIN_PATTERN_1;
2444 /* BPC in FDI rx is consistent with that in PIPECONF */
2445 temp &= ~(0x07 << 16);
2446 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2447 I915_WRITE(reg, temp);
2454 * When we disable a pipe, we need to clear any pending scanline wait events
2455 * to avoid hanging the ring, which we assume we are waiting on.
2457 static void intel_clear_scanline_wait(struct drm_device *dev)
2459 struct drm_i915_private *dev_priv = dev->dev_private;
2460 struct intel_ring_buffer *ring;
2464 /* Can't break the hang on i8xx */
2467 ring = LP_RING(dev_priv);
2468 tmp = I915_READ_CTL(ring);
2469 if (tmp & RING_WAIT)
2470 I915_WRITE_CTL(ring, tmp);
2473 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2475 struct drm_i915_gem_object *obj;
2476 struct drm_i915_private *dev_priv;
2477 struct drm_device *dev;
2479 if (crtc->fb == NULL)
2482 obj = to_intel_framebuffer(crtc->fb)->obj;
2484 dev_priv = dev->dev_private;
2485 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2486 while (atomic_read(&obj->pending_flip) != 0)
2487 lksleep(&obj->pending_flip, &dev->event_lock, 0, "915wfl", 0);
2488 lockmgr(&dev->event_lock, LK_RELEASE);
2491 static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2493 struct drm_device *dev = crtc->dev;
2494 struct drm_mode_config *mode_config = &dev->mode_config;
2495 struct intel_encoder *encoder;
2498 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2499 * must be driven by its own crtc; no sharing is possible.
2501 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2502 if (encoder->base.crtc != crtc)
2505 switch (encoder->type) {
2506 case INTEL_OUTPUT_EDP:
2507 if (!intel_encoder_is_pch_edp(&encoder->base))
2517 * Enable PCH resources required for PCH ports:
2519 * - FDI training & RX/TX
2520 * - update transcoder timings
2521 * - DP transcoding bits
2524 static void ironlake_pch_enable(struct drm_crtc *crtc)
2526 struct drm_device *dev = crtc->dev;
2527 struct drm_i915_private *dev_priv = dev->dev_private;
2528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2529 int pipe = intel_crtc->pipe;
2530 u32 reg, temp, transc_sel;
2532 /* For PCH output, training FDI link */
2533 dev_priv->display.fdi_link_train(crtc);
2535 intel_enable_pch_pll(dev_priv, pipe);
2537 if (HAS_PCH_CPT(dev)) {
2538 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2541 /* Be sure PCH DPLL SEL is set */
2542 temp = I915_READ(PCH_DPLL_SEL);
2544 temp &= ~(TRANSA_DPLLB_SEL);
2545 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
2546 } else if (pipe == 1) {
2547 temp &= ~(TRANSB_DPLLB_SEL);
2548 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2549 } else if (pipe == 2) {
2550 temp &= ~(TRANSC_DPLLB_SEL);
2551 temp |= (TRANSC_DPLL_ENABLE | transc_sel);
2553 I915_WRITE(PCH_DPLL_SEL, temp);
2556 /* set transcoder timing, panel must allow it */
2557 assert_panel_unlocked(dev_priv, pipe);
2558 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2559 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2560 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2562 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2563 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2564 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
2565 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
2567 intel_fdi_normal_train(crtc);
2569 /* For PCH DP, enable TRANS_DP_CTL */
2570 if (HAS_PCH_CPT(dev) &&
2571 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2572 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2573 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
2574 reg = TRANS_DP_CTL(pipe);
2575 temp = I915_READ(reg);
2576 temp &= ~(TRANS_DP_PORT_SEL_MASK |
2577 TRANS_DP_SYNC_MASK |
2579 temp |= (TRANS_DP_OUTPUT_ENABLE |
2580 TRANS_DP_ENH_FRAMING);
2581 temp |= bpc << 9; /* same format but at 11:9 */
2583 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
2584 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
2585 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
2586 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
2588 switch (intel_trans_dp_port_sel(crtc)) {
2590 temp |= TRANS_DP_PORT_SEL_B;
2593 temp |= TRANS_DP_PORT_SEL_C;
2596 temp |= TRANS_DP_PORT_SEL_D;
2599 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
2600 temp |= TRANS_DP_PORT_SEL_B;
2604 I915_WRITE(reg, temp);
2607 intel_enable_transcoder(dev_priv, pipe);
2610 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
2612 struct drm_i915_private *dev_priv = dev->dev_private;
2613 int dslreg = PIPEDSL(pipe);
2616 temp = I915_READ(dslreg);
2618 if (wait_for(I915_READ(dslreg) != temp, 5)) {
2619 if (wait_for(I915_READ(dslreg) != temp, 5))
2620 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
2624 static void ironlake_crtc_enable(struct drm_crtc *crtc)
2626 struct drm_device *dev = crtc->dev;
2627 struct drm_i915_private *dev_priv = dev->dev_private;
2628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2629 int pipe = intel_crtc->pipe;
2630 int plane = intel_crtc->plane;
2634 if (intel_crtc->active)
2637 intel_crtc->active = true;
2638 intel_update_watermarks(dev);
2640 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2641 temp = I915_READ(PCH_LVDS);
2642 if ((temp & LVDS_PORT_EN) == 0)
2643 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
2646 is_pch_port = intel_crtc_driving_pch(crtc);
2649 ironlake_fdi_pll_enable(crtc);
2651 ironlake_fdi_disable(crtc);
2653 /* Enable panel fitting for LVDS */
2654 if (dev_priv->pch_pf_size &&
2655 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
2656 /* Force use of hard-coded filter coefficients
2657 * as some pre-programmed values are broken,
2660 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
2661 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
2662 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
2665 intel_enable_pipe(dev_priv, pipe, is_pch_port);
2666 intel_enable_plane(dev_priv, plane, pipe);
2669 ironlake_pch_enable(crtc);
2671 intel_crtc_load_lut(crtc);
2674 intel_update_fbc(dev);
2677 intel_crtc_update_cursor(crtc, true);
2680 static void ironlake_crtc_disable(struct drm_crtc *crtc)
2682 struct drm_device *dev = crtc->dev;
2683 struct drm_i915_private *dev_priv = dev->dev_private;
2684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2685 int pipe = intel_crtc->pipe;
2686 int plane = intel_crtc->plane;
2689 if (!intel_crtc->active)
2692 intel_crtc_wait_for_pending_flips(crtc);
2693 drm_vblank_off(dev, pipe);
2694 intel_crtc_update_cursor(crtc, false);
2696 intel_disable_plane(dev_priv, plane, pipe);
2698 if (dev_priv->cfb_plane == plane)
2699 intel_disable_fbc(dev);
2701 intel_disable_pipe(dev_priv, pipe);
2704 I915_WRITE(PF_CTL(pipe), 0);
2705 I915_WRITE(PF_WIN_SZ(pipe), 0);
2707 ironlake_fdi_disable(crtc);
2709 /* This is a horrible layering violation; we should be doing this in
2710 * the connector/encoder ->prepare instead, but we don't always have
2711 * enough information there about the config to know whether it will
2712 * actually be necessary or just cause undesired flicker.
2714 intel_disable_pch_ports(dev_priv, pipe);
2716 intel_disable_transcoder(dev_priv, pipe);
2718 if (HAS_PCH_CPT(dev)) {
2719 /* disable TRANS_DP_CTL */
2720 reg = TRANS_DP_CTL(pipe);
2721 temp = I915_READ(reg);
2722 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2723 temp |= TRANS_DP_PORT_SEL_NONE;
2724 I915_WRITE(reg, temp);
2726 /* disable DPLL_SEL */
2727 temp = I915_READ(PCH_DPLL_SEL);
2730 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2733 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2736 /* C shares PLL A or B */
2737 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
2740 KASSERT(1, ("Wrong pipe %d", pipe)); /* wtf */
2742 I915_WRITE(PCH_DPLL_SEL, temp);
2745 /* disable PCH DPLL */
2746 if (!intel_crtc->no_pll)
2747 intel_disable_pch_pll(dev_priv, pipe);
2749 /* Switch from PCDclk to Rawclk */
2750 reg = FDI_RX_CTL(pipe);
2751 temp = I915_READ(reg);
2752 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2754 /* Disable CPU FDI TX PLL */
2755 reg = FDI_TX_CTL(pipe);
2756 temp = I915_READ(reg);
2757 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2762 reg = FDI_RX_CTL(pipe);
2763 temp = I915_READ(reg);
2764 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2766 /* Wait for the clocks to turn off. */
2770 intel_crtc->active = false;
2771 intel_update_watermarks(dev);
2774 intel_update_fbc(dev);
2775 intel_clear_scanline_wait(dev);
2779 static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2782 int pipe = intel_crtc->pipe;
2783 int plane = intel_crtc->plane;
2785 /* XXX: When our outputs are all unaware of DPMS modes other than off
2786 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2789 case DRM_MODE_DPMS_ON:
2790 case DRM_MODE_DPMS_STANDBY:
2791 case DRM_MODE_DPMS_SUSPEND:
2792 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
2793 ironlake_crtc_enable(crtc);
2796 case DRM_MODE_DPMS_OFF:
2797 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
2798 ironlake_crtc_disable(crtc);
2803 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2805 if (!enable && intel_crtc->overlay) {
2806 struct drm_device *dev = intel_crtc->base.dev;
2807 struct drm_i915_private *dev_priv = dev->dev_private;
2810 dev_priv->mm.interruptible = false;
2811 (void) intel_overlay_switch_off(intel_crtc->overlay);
2812 dev_priv->mm.interruptible = true;
2816 /* Let userspace switch the overlay on again. In most cases userspace
2817 * has to recompute where to put it anyway.
2821 static void i9xx_crtc_enable(struct drm_crtc *crtc)
2823 struct drm_device *dev = crtc->dev;
2824 struct drm_i915_private *dev_priv = dev->dev_private;
2825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2826 int pipe = intel_crtc->pipe;
2827 int plane = intel_crtc->plane;
2829 if (intel_crtc->active)
2832 intel_crtc->active = true;
2833 intel_update_watermarks(dev);
2835 intel_enable_pll(dev_priv, pipe);
2836 intel_enable_pipe(dev_priv, pipe, false);
2837 intel_enable_plane(dev_priv, plane, pipe);
2839 intel_crtc_load_lut(crtc);
2840 intel_update_fbc(dev);
2842 /* Give the overlay scaler a chance to enable if it's on this pipe */
2843 intel_crtc_dpms_overlay(intel_crtc, true);
2844 intel_crtc_update_cursor(crtc, true);
2847 static void i9xx_crtc_disable(struct drm_crtc *crtc)
2849 struct drm_device *dev = crtc->dev;
2850 struct drm_i915_private *dev_priv = dev->dev_private;
2851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2852 int pipe = intel_crtc->pipe;
2853 int plane = intel_crtc->plane;
2855 if (!intel_crtc->active)
2858 /* Give the overlay scaler a chance to disable if it's on this pipe */
2859 intel_crtc_wait_for_pending_flips(crtc);
2860 drm_vblank_off(dev, pipe);
2861 intel_crtc_dpms_overlay(intel_crtc, false);
2862 intel_crtc_update_cursor(crtc, false);
2864 if (dev_priv->cfb_plane == plane)
2865 intel_disable_fbc(dev);
2867 intel_disable_plane(dev_priv, plane, pipe);
2868 intel_disable_pipe(dev_priv, pipe);
2869 intel_disable_pll(dev_priv, pipe);
2871 intel_crtc->active = false;
2872 intel_update_fbc(dev);
2873 intel_update_watermarks(dev);
2874 intel_clear_scanline_wait(dev);
2877 static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2879 /* XXX: When our outputs are all unaware of DPMS modes other than off
2880 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2883 case DRM_MODE_DPMS_ON:
2884 case DRM_MODE_DPMS_STANDBY:
2885 case DRM_MODE_DPMS_SUSPEND:
2886 i9xx_crtc_enable(crtc);
2888 case DRM_MODE_DPMS_OFF:
2889 i9xx_crtc_disable(crtc);
2895 * Sets the power management mode of the pipe and plane.
2897 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2899 struct drm_device *dev = crtc->dev;
2900 struct drm_i915_private *dev_priv = dev->dev_private;
2902 struct drm_i915_master_private *master_priv;
2904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2905 int pipe = intel_crtc->pipe;
2908 if (intel_crtc->dpms_mode == mode)
2911 intel_crtc->dpms_mode = mode;
2913 dev_priv->display.dpms(crtc, mode);
2916 if (!dev->primary->master)
2919 master_priv = dev->primary->master->driver_priv;
2920 if (!master_priv->sarea_priv)
2923 if (!dev_priv->sarea_priv)
2927 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2932 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2933 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2935 dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
2936 dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
2941 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2942 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2944 dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
2945 dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
2949 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
2954 static void intel_crtc_disable(struct drm_crtc *crtc)
2956 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2957 struct drm_device *dev = crtc->dev;
2959 /* Flush any pending WAITs before we disable the pipe. Note that
2960 * we need to drop the struct_mutex in order to acquire it again
2961 * during the lowlevel dpms routines around a couple of the
2962 * operations. It does not look trivial nor desirable to move
2963 * that locking higher. So instead we leave a window for the
2964 * submission of further commands on the fb before we can actually
2965 * disable it. This race with userspace exists anyway, and we can
2966 * only rely on the pipe being disabled by userspace after it
2967 * receives the hotplug notification and has flushed any pending
2972 intel_finish_fb(crtc->fb);
2976 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2977 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
2978 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
2982 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
2987 /* Prepare for a mode set.
2989 * Note we could be a lot smarter here. We need to figure out which outputs
2990 * will be enabled, which disabled (in short, how the config will changes)
2991 * and perform the minimum necessary steps to accomplish that, e.g. updating
2992 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2993 * panel fitting is in the proper state, etc.
2995 static void i9xx_crtc_prepare(struct drm_crtc *crtc)
2997 i9xx_crtc_disable(crtc);
3000 static void i9xx_crtc_commit(struct drm_crtc *crtc)
3002 i9xx_crtc_enable(crtc);
3005 static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3007 ironlake_crtc_disable(crtc);
3010 static void ironlake_crtc_commit(struct drm_crtc *crtc)
3012 ironlake_crtc_enable(crtc);
3015 void intel_encoder_prepare(struct drm_encoder *encoder)
3017 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3018 /* lvds has its own version of prepare see intel_lvds_prepare */
3019 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3022 void intel_encoder_commit(struct drm_encoder *encoder)
3024 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3025 struct drm_device *dev = encoder->dev;
3026 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3027 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3029 /* lvds has its own version of commit see intel_lvds_commit */
3030 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3032 if (HAS_PCH_CPT(dev))
3033 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3036 void intel_encoder_destroy(struct drm_encoder *encoder)
3038 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3040 drm_encoder_cleanup(encoder);
3041 drm_free(intel_encoder, DRM_MEM_KMS);
3044 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3045 const struct drm_display_mode *mode,
3046 struct drm_display_mode *adjusted_mode)
3048 struct drm_device *dev = crtc->dev;
3050 if (HAS_PCH_SPLIT(dev)) {
3051 /* FDI link clock is fixed at 2.7G */
3052 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3056 /* All interlaced capable intel hw wants timings in frames. Note though
3057 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3058 * timings, so we need to be careful not to clobber these.*/
3059 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3060 drm_mode_set_crtcinfo(adjusted_mode, 0);
3065 static int i945_get_display_clock_speed(struct drm_device *dev)
3070 static int i915_get_display_clock_speed(struct drm_device *dev)
3075 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3080 static int i915gm_get_display_clock_speed(struct drm_device *dev)
3084 gcfgc = pci_read_config(dev->dev, GCFGC, 2);
3086 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3089 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3090 case GC_DISPLAY_CLOCK_333_MHZ:
3093 case GC_DISPLAY_CLOCK_190_200_MHZ:
3099 static int i865_get_display_clock_speed(struct drm_device *dev)
3104 static int i855_get_display_clock_speed(struct drm_device *dev)
3107 /* Assume that the hardware is in the high speed state. This
3108 * should be the default.
3110 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3111 case GC_CLOCK_133_200:
3112 case GC_CLOCK_100_200:
3114 case GC_CLOCK_166_250:
3116 case GC_CLOCK_100_133:
3120 /* Shouldn't happen */
3124 static int i830_get_display_clock_speed(struct drm_device *dev)
3138 fdi_reduce_ratio(u32 *num, u32 *den)
3140 while (*num > 0xffffff || *den > 0xffffff) {
3147 ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3148 int link_clock, struct fdi_m_n *m_n)
3150 m_n->tu = 64; /* default size */
3152 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3153 m_n->gmch_m = bits_per_pixel * pixel_clock;
3154 m_n->gmch_n = link_clock * nlanes * 8;
3155 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3157 m_n->link_m = pixel_clock;
3158 m_n->link_n = link_clock;
3159 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3162 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3164 if (i915_panel_use_ssc >= 0)
3165 return i915_panel_use_ssc != 0;
3166 return dev_priv->lvds_use_ssc
3167 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
3171 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3172 * @crtc: CRTC structure
3173 * @mode: requested mode
3175 * A pipe may be connected to one or more outputs. Based on the depth of the
3176 * attached framebuffer, choose a good color depth to use on the pipe.
3178 * If possible, match the pipe depth to the fb depth. In some cases, this
3179 * isn't ideal, because the connected output supports a lesser or restricted
3180 * set of depths. Resolve that here:
3181 * LVDS typically supports only 6bpc, so clamp down in that case
3182 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3183 * Displays may support a restricted set as well, check EDID and clamp as
3185 * DP may want to dither down to 6bpc to fit larger modes
3188 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3189 * true if they don't match).
3191 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
3192 unsigned int *pipe_bpp,
3193 struct drm_display_mode *mode)
3195 struct drm_device *dev = crtc->dev;
3196 struct drm_i915_private *dev_priv = dev->dev_private;
3197 struct drm_encoder *encoder;
3198 struct drm_connector *connector;
3199 unsigned int display_bpc = UINT_MAX, bpc;
3201 /* Walk the encoders & connectors on this crtc, get min bpc */
3202 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3203 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3205 if (encoder->crtc != crtc)
3208 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3209 unsigned int lvds_bpc;
3211 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3217 if (lvds_bpc < display_bpc) {
3218 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
3219 display_bpc = lvds_bpc;
3224 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
3225 /* Use VBT settings if we have an eDP panel */
3226 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
3228 if (edp_bpc < display_bpc) {
3229 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
3230 display_bpc = edp_bpc;
3235 /* Not one of the known troublemakers, check the EDID */
3236 list_for_each_entry(connector, &dev->mode_config.connector_list,
3238 if (connector->encoder != encoder)
3241 /* Don't use an invalid EDID bpc value */
3242 if (connector->display_info.bpc &&
3243 connector->display_info.bpc < display_bpc) {
3244 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
3245 display_bpc = connector->display_info.bpc;
3250 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3251 * through, clamp it down. (Note: >12bpc will be caught below.)
3253 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3254 if (display_bpc > 8 && display_bpc < 12) {
3255 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
3258 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
3264 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3265 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3270 * We could just drive the pipe at the highest bpc all the time and
3271 * enable dithering as needed, but that costs bandwidth. So choose
3272 * the minimum value that expresses the full color range of the fb but
3273 * also stays within the max display bpc discovered above.
3276 switch (crtc->fb->depth) {
3278 bpc = 8; /* since we go through a colormap */
3282 bpc = 6; /* min is 18bpp */
3294 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3295 bpc = min((unsigned int)8, display_bpc);
3299 display_bpc = min(display_bpc, bpc);
3301 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3304 *pipe_bpp = display_bpc * 3;
3306 return display_bpc != bpc;
3309 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3311 struct drm_device *dev = crtc->dev;
3312 struct drm_i915_private *dev_priv = dev->dev_private;
3315 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3316 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3317 refclk = dev_priv->lvds_ssc_freq * 1000;
3318 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3320 } else if (!IS_GEN2(dev)) {
3329 static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3330 intel_clock_t *clock)
3332 /* SDVO TV has fixed PLL values depend on its clock range,
3333 this mirrors vbios setting. */
3334 if (adjusted_mode->clock >= 100000
3335 && adjusted_mode->clock < 140500) {
3341 } else if (adjusted_mode->clock >= 140500
3342 && adjusted_mode->clock <= 200000) {
3351 static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3352 intel_clock_t *clock,
3353 intel_clock_t *reduced_clock)
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
3357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3358 int pipe = intel_crtc->pipe;
3361 if (IS_PINEVIEW(dev)) {
3362 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
3364 fp2 = (1 << reduced_clock->n) << 16 |
3365 reduced_clock->m1 << 8 | reduced_clock->m2;
3367 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
3369 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
3373 I915_WRITE(FP0(pipe), fp);
3375 intel_crtc->lowfreq_avail = false;
3376 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3377 reduced_clock && i915_powersave) {
3378 I915_WRITE(FP1(pipe), fp2);
3379 intel_crtc->lowfreq_avail = true;
3381 I915_WRITE(FP1(pipe), fp);
3385 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
3386 struct drm_display_mode *mode,
3387 struct drm_display_mode *adjusted_mode,
3389 struct drm_framebuffer *old_fb)
3391 struct drm_device *dev = crtc->dev;
3392 struct drm_i915_private *dev_priv = dev->dev_private;
3393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3394 int pipe = intel_crtc->pipe;
3395 int plane = intel_crtc->plane;
3396 int refclk, num_connectors = 0;
3397 intel_clock_t clock, reduced_clock;
3398 u32 dpll, dspcntr, pipeconf, vsyncshift;
3399 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
3400 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3401 struct drm_mode_config *mode_config = &dev->mode_config;
3402 struct intel_encoder *encoder;
3403 const intel_limit_t *limit;
3408 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3409 if (encoder->base.crtc != crtc)
3412 switch (encoder->type) {
3413 case INTEL_OUTPUT_LVDS:
3416 case INTEL_OUTPUT_SDVO:
3417 case INTEL_OUTPUT_HDMI:
3419 if (encoder->needs_tv_clock)
3422 case INTEL_OUTPUT_DVO:
3425 case INTEL_OUTPUT_TVOUT:
3428 case INTEL_OUTPUT_ANALOG:
3431 case INTEL_OUTPUT_DISPLAYPORT:
3439 refclk = i9xx_get_refclk(crtc, num_connectors);
3442 * Returns a set of divisors for the desired target clock with the given
3443 * refclk, or false. The returned values represent the clock equation:
3444 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3446 limit = intel_limit(crtc, refclk);
3447 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3450 DRM_ERROR("Couldn't find PLL settings for mode!\n");
3454 /* Ensure that the cursor is valid for the new mode before changing... */
3455 intel_crtc_update_cursor(crtc, true);
3457 if (is_lvds && dev_priv->lvds_downclock_avail) {
3459 * Ensure we match the reduced clock's P to the target clock.
3460 * If the clocks don't match, we can't switch the display clock
3461 * by using the FP0/FP1. In such case we will disable the LVDS
3462 * downclock feature.
3464 has_reduced_clock = limit->find_pll(limit, crtc,
3465 dev_priv->lvds_downclock,
3471 if (is_sdvo && is_tv)
3472 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
3474 i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
3475 &reduced_clock : NULL);
3477 dpll = DPLL_VGA_MODE_DIS;
3479 if (!IS_GEN2(dev)) {
3481 dpll |= DPLLB_MODE_LVDS;
3483 dpll |= DPLLB_MODE_DAC_SERIAL;
3485 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
3486 if (pixel_multiplier > 1) {
3487 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3488 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
3490 dpll |= DPLL_DVO_HIGH_SPEED;
3493 dpll |= DPLL_DVO_HIGH_SPEED;
3495 /* compute bitmask from p1 value */
3496 if (IS_PINEVIEW(dev))
3497 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
3499 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3500 if (IS_G4X(dev) && has_reduced_clock)
3501 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3505 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3508 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3511 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3514 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3517 if (INTEL_INFO(dev)->gen >= 4)
3518 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3521 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3524 dpll |= PLL_P1_DIVIDE_BY_TWO;
3526 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3528 dpll |= PLL_P2_DIVIDE_BY_4;
3532 if (is_sdvo && is_tv)
3533 dpll |= PLL_REF_INPUT_TVCLKINBC;
3535 /* XXX: just matching BIOS for now */
3536 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
3538 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
3539 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3541 dpll |= PLL_REF_INPUT_DREFCLK;
3543 /* setup pipeconf */
3544 pipeconf = I915_READ(PIPECONF(pipe));
3546 /* Set up the display plane register */
3547 dspcntr = DISPPLANE_GAMMA_ENABLE;
3550 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
3552 dspcntr |= DISPPLANE_SEL_PIPE_B;
3554 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
3555 /* Enable pixel doubling when the dot clock is > 90% of the (display)
3558 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3562 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
3563 pipeconf |= PIPECONF_DOUBLE_WIDE;
3565 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
3568 /* default to 8bpc */
3569 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
3571 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3572 pipeconf |= PIPECONF_BPP_6 |
3573 PIPECONF_DITHER_EN |
3574 PIPECONF_DITHER_TYPE_SP;
3578 dpll |= DPLL_VCO_ENABLE;
3580 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
3581 drm_mode_debug_printmodeline(mode);
3583 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
3585 POSTING_READ(DPLL(pipe));
3588 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3589 * This is an exception to the general rule that mode_set doesn't turn
3593 temp = I915_READ(LVDS);
3594 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
3596 temp |= LVDS_PIPEB_SELECT;
3598 temp &= ~LVDS_PIPEB_SELECT;
3600 /* set the corresponsding LVDS_BORDER bit */
3601 temp |= dev_priv->lvds_border_bits;
3602 /* Set the B0-B3 data pairs corresponding to whether we're going to
3603 * set the DPLLs for dual-channel mode or not.
3606 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3608 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3610 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3611 * appropriately here, but we need to look more thoroughly into how
3612 * panels behave in the two modes.
3614 /* set the dithering flag on LVDS as needed */
3615 if (INTEL_INFO(dev)->gen >= 4) {
3616 if (dev_priv->lvds_dither)
3617 temp |= LVDS_ENABLE_DITHER;
3619 temp &= ~LVDS_ENABLE_DITHER;
3621 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
3622 lvds_sync |= LVDS_HSYNC_POLARITY;
3623 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
3624 lvds_sync |= LVDS_VSYNC_POLARITY;
3625 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
3627 char flags[2] = "-+";
3628 DRM_INFO("Changing LVDS panel from "
3629 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
3630 flags[!(temp & LVDS_HSYNC_POLARITY)],
3631 flags[!(temp & LVDS_VSYNC_POLARITY)],
3632 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
3633 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
3634 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
3637 I915_WRITE(LVDS, temp);
3641 intel_dp_set_m_n(crtc, mode, adjusted_mode);
3644 I915_WRITE(DPLL(pipe), dpll);
3646 /* Wait for the clocks to stabilize. */
3647 POSTING_READ(DPLL(pipe));
3650 if (INTEL_INFO(dev)->gen >= 4) {
3653 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
3655 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
3659 I915_WRITE(DPLL_MD(pipe), temp);
3661 /* The pixel multiplier can only be updated once the
3662 * DPLL is enabled and the clocks are stable.
3664 * So write it again.
3666 I915_WRITE(DPLL(pipe), dpll);
3669 if (HAS_PIPE_CXSR(dev)) {
3670 if (intel_crtc->lowfreq_avail) {
3671 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
3672 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
3674 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
3675 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3679 pipeconf &= ~PIPECONF_INTERLACE_MASK;
3680 if (!IS_GEN2(dev) &&
3681 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
3682 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
3683 /* the chip adds 2 halflines automatically */
3684 adjusted_mode->crtc_vtotal -= 1;
3685 adjusted_mode->crtc_vblank_end -= 1;
3686 vsyncshift = adjusted_mode->crtc_hsync_start
3687 - adjusted_mode->crtc_htotal/2;
3689 pipeconf |= PIPECONF_PROGRESSIVE;
3694 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
3696 I915_WRITE(HTOTAL(pipe),
3697 (adjusted_mode->crtc_hdisplay - 1) |
3698 ((adjusted_mode->crtc_htotal - 1) << 16));
3699 I915_WRITE(HBLANK(pipe),
3700 (adjusted_mode->crtc_hblank_start - 1) |
3701 ((adjusted_mode->crtc_hblank_end - 1) << 16));
3702 I915_WRITE(HSYNC(pipe),
3703 (adjusted_mode->crtc_hsync_start - 1) |
3704 ((adjusted_mode->crtc_hsync_end - 1) << 16));
3706 I915_WRITE(VTOTAL(pipe),
3707 (adjusted_mode->crtc_vdisplay - 1) |
3708 ((adjusted_mode->crtc_vtotal - 1) << 16));
3709 I915_WRITE(VBLANK(pipe),
3710 (adjusted_mode->crtc_vblank_start - 1) |
3711 ((adjusted_mode->crtc_vblank_end - 1) << 16));
3712 I915_WRITE(VSYNC(pipe),
3713 (adjusted_mode->crtc_vsync_start - 1) |
3714 ((adjusted_mode->crtc_vsync_end - 1) << 16));
3716 /* pipesrc and dspsize control the size that is scaled from,
3717 * which should always be the user's requested size.
3719 I915_WRITE(DSPSIZE(plane),
3720 ((mode->vdisplay - 1) << 16) |
3721 (mode->hdisplay - 1));
3722 I915_WRITE(DSPPOS(plane), 0);
3723 I915_WRITE(PIPESRC(pipe),
3724 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
3726 I915_WRITE(PIPECONF(pipe), pipeconf);
3727 POSTING_READ(PIPECONF(pipe));
3728 intel_enable_pipe(dev_priv, pipe, false);
3730 intel_wait_for_vblank(dev, pipe);
3732 I915_WRITE(DSPCNTR(plane), dspcntr);
3733 POSTING_READ(DSPCNTR(plane));
3734 intel_enable_plane(dev_priv, plane, pipe);
3736 ret = intel_pipe_set_base(crtc, x, y, old_fb);
3738 intel_update_watermarks(dev);
3744 * Initialize reference clocks when the driver loads
3746 void ironlake_init_pch_refclk(struct drm_device *dev)
3748 struct drm_i915_private *dev_priv = dev->dev_private;
3749 struct drm_mode_config *mode_config = &dev->mode_config;
3750 struct intel_encoder *encoder;
3752 bool has_lvds = false;
3753 bool has_cpu_edp = false;
3754 bool has_pch_edp = false;
3755 bool has_panel = false;
3756 bool has_ck505 = false;
3757 bool can_ssc = false;
3759 /* We need to take the global config into account */
3760 list_for_each_entry(encoder, &mode_config->encoder_list,
3762 switch (encoder->type) {
3763 case INTEL_OUTPUT_LVDS:
3767 case INTEL_OUTPUT_EDP:
3769 if (intel_encoder_is_pch_edp(&encoder->base))
3777 if (HAS_PCH_IBX(dev)) {
3778 has_ck505 = dev_priv->display_clock_mode;
3779 can_ssc = has_ck505;
3785 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
3786 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
3789 /* Ironlake: try to setup display ref clock before DPLL
3790 * enabling. This is only under driver's control after
3791 * PCH B stepping, previous chipset stepping should be
3792 * ignoring this setting.
3794 temp = I915_READ(PCH_DREF_CONTROL);
3795 /* Always enable nonspread source */
3796 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
3799 temp |= DREF_NONSPREAD_CK505_ENABLE;
3801 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
3804 temp &= ~DREF_SSC_SOURCE_MASK;
3805 temp |= DREF_SSC_SOURCE_ENABLE;
3807 /* SSC must be turned on before enabling the CPU output */
3808 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3809 DRM_DEBUG_KMS("Using SSC on panel\n");
3810 temp |= DREF_SSC1_ENABLE;
3812 temp &= ~DREF_SSC1_ENABLE;
3814 /* Get SSC going before enabling the outputs */
3815 I915_WRITE(PCH_DREF_CONTROL, temp);
3816 POSTING_READ(PCH_DREF_CONTROL);
3819 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3821 /* Enable CPU source on CPU attached eDP */
3823 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3824 DRM_DEBUG_KMS("Using SSC on eDP\n");
3825 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
3828 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
3830 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3832 I915_WRITE(PCH_DREF_CONTROL, temp);
3833 POSTING_READ(PCH_DREF_CONTROL);
3836 DRM_DEBUG_KMS("Disabling SSC entirely\n");
3838 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3840 /* Turn off CPU output */
3841 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3843 I915_WRITE(PCH_DREF_CONTROL, temp);
3844 POSTING_READ(PCH_DREF_CONTROL);
3847 /* Turn off the SSC source */
3848 temp &= ~DREF_SSC_SOURCE_MASK;
3849 temp |= DREF_SSC_SOURCE_DISABLE;
3852 temp &= ~ DREF_SSC1_ENABLE;
3854 I915_WRITE(PCH_DREF_CONTROL, temp);
3855 POSTING_READ(PCH_DREF_CONTROL);
3860 static int ironlake_get_refclk(struct drm_crtc *crtc)
3862 struct drm_device *dev = crtc->dev;
3863 struct drm_i915_private *dev_priv = dev->dev_private;
3864 struct intel_encoder *encoder;
3865 struct drm_mode_config *mode_config = &dev->mode_config;
3866 struct intel_encoder *edp_encoder = NULL;
3867 int num_connectors = 0;
3868 bool is_lvds = false;
3870 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3871 if (encoder->base.crtc != crtc)
3874 switch (encoder->type) {
3875 case INTEL_OUTPUT_LVDS:
3878 case INTEL_OUTPUT_EDP:
3879 edp_encoder = encoder;
3885 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3886 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3887 dev_priv->lvds_ssc_freq);
3888 return dev_priv->lvds_ssc_freq * 1000;
3894 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
3895 struct drm_display_mode *mode,
3896 struct drm_display_mode *adjusted_mode,
3898 struct drm_framebuffer *old_fb)
3900 struct drm_device *dev = crtc->dev;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
3902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3903 int pipe = intel_crtc->pipe;
3904 int plane = intel_crtc->plane;
3905 int refclk, num_connectors = 0;
3906 intel_clock_t clock, reduced_clock;
3907 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
3908 bool ok, has_reduced_clock = false, is_sdvo = false;
3909 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3910 struct intel_encoder *has_edp_encoder = NULL;
3911 struct drm_mode_config *mode_config = &dev->mode_config;
3912 struct intel_encoder *encoder;
3913 const intel_limit_t *limit;
3915 struct fdi_m_n m_n = {0};
3918 int target_clock, pixel_multiplier, lane, link_bw, factor;
3919 unsigned int pipe_bpp;
3922 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3923 if (encoder->base.crtc != crtc)
3926 switch (encoder->type) {
3927 case INTEL_OUTPUT_LVDS:
3930 case INTEL_OUTPUT_SDVO:
3931 case INTEL_OUTPUT_HDMI:
3933 if (encoder->needs_tv_clock)
3936 case INTEL_OUTPUT_TVOUT:
3939 case INTEL_OUTPUT_ANALOG:
3942 case INTEL_OUTPUT_DISPLAYPORT:
3945 case INTEL_OUTPUT_EDP:
3946 has_edp_encoder = encoder;
3953 refclk = ironlake_get_refclk(crtc);
3956 * Returns a set of divisors for the desired target clock with the given
3957 * refclk, or false. The returned values represent the clock equation:
3958 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3960 limit = intel_limit(crtc, refclk);
3961 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3964 DRM_ERROR("Couldn't find PLL settings for mode!\n");
3968 /* Ensure that the cursor is valid for the new mode before changing... */
3969 intel_crtc_update_cursor(crtc, true);
3971 if (is_lvds && dev_priv->lvds_downclock_avail) {
3973 * Ensure we match the reduced clock's P to the target clock.
3974 * If the clocks don't match, we can't switch the display clock
3975 * by using the FP0/FP1. In such case we will disable the LVDS
3976 * downclock feature.
3978 has_reduced_clock = limit->find_pll(limit, crtc,
3979 dev_priv->lvds_downclock,
3984 /* SDVO TV has fixed PLL values depend on its clock range,
3985 this mirrors vbios setting. */
3986 if (is_sdvo && is_tv) {
3987 if (adjusted_mode->clock >= 100000
3988 && adjusted_mode->clock < 140500) {
3994 } else if (adjusted_mode->clock >= 140500
3995 && adjusted_mode->clock <= 200000) {
4005 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4007 /* CPU eDP doesn't require FDI link, so just set DP M/N
4008 according to current link config */
4009 if (has_edp_encoder &&
4010 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4011 target_clock = mode->clock;
4012 intel_edp_link_config(has_edp_encoder,
4015 /* [e]DP over FDI requires target mode clock
4016 instead of link clock */
4017 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4018 target_clock = mode->clock;
4020 target_clock = adjusted_mode->clock;
4022 /* FDI is a binary signal running at ~2.7GHz, encoding
4023 * each output octet as 10 bits. The actual frequency
4024 * is stored as a divider into a 100MHz clock, and the
4025 * mode pixel clock is stored in units of 1KHz.
4026 * Hence the bw of each lane in terms of the mode signal
4029 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4032 /* determine panel color depth */
4033 temp = I915_READ(PIPECONF(pipe));
4034 temp &= ~PIPE_BPC_MASK;
4035 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
4050 kprintf("intel_choose_pipe_bpp returned invalid value %d\n",
4057 intel_crtc->bpp = pipe_bpp;
4058 I915_WRITE(PIPECONF(pipe), temp);
4062 * Account for spread spectrum to avoid
4063 * oversubscribing the link. Max center spread
4064 * is 2.5%; use 5% for safety's sake.
4066 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
4067 lane = bps / (link_bw * 8) + 1;
4070 intel_crtc->fdi_lanes = lane;
4072 if (pixel_multiplier > 1)
4073 link_bw *= pixel_multiplier;
4074 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4077 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4078 if (has_reduced_clock)
4079 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4082 /* Enable autotuning of the PLL clock (if permissible) */
4085 if ((intel_panel_use_ssc(dev_priv) &&
4086 dev_priv->lvds_ssc_freq == 100) ||
4087 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4089 } else if (is_sdvo && is_tv)
4092 if (clock.m < factor * clock.n)
4098 dpll |= DPLLB_MODE_LVDS;
4100 dpll |= DPLLB_MODE_DAC_SERIAL;
4102 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4103 if (pixel_multiplier > 1) {
4104 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4106 dpll |= DPLL_DVO_HIGH_SPEED;
4108 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4109 dpll |= DPLL_DVO_HIGH_SPEED;
4111 /* compute bitmask from p1 value */
4112 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4114 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4118 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4121 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4124 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4127 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4131 if (is_sdvo && is_tv)
4132 dpll |= PLL_REF_INPUT_TVCLKINBC;
4134 /* XXX: just matching BIOS for now */
4135 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4137 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4138 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4140 dpll |= PLL_REF_INPUT_DREFCLK;
4142 /* setup pipeconf */
4143 pipeconf = I915_READ(PIPECONF(pipe));
4145 /* Set up the display plane register */
4146 dspcntr = DISPPLANE_GAMMA_ENABLE;
4148 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
4149 drm_mode_debug_printmodeline(mode);
4151 /* PCH eDP needs FDI, but CPU eDP does not */
4152 if (!intel_crtc->no_pll) {
4153 if (!has_edp_encoder ||
4154 intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4155 I915_WRITE(_PCH_FP0(pipe), fp);
4156 I915_WRITE(_PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4158 POSTING_READ(_PCH_DPLL(pipe));
4162 if (dpll == (I915_READ(_PCH_DPLL(0)) & 0x7fffffff) &&
4163 fp == I915_READ(_PCH_FP0(0))) {
4164 intel_crtc->use_pll_a = true;
4165 DRM_DEBUG_KMS("using pipe a dpll\n");
4166 } else if (dpll == (I915_READ(_PCH_DPLL(1)) & 0x7fffffff) &&
4167 fp == I915_READ(_PCH_FP0(1))) {
4168 intel_crtc->use_pll_a = false;
4169 DRM_DEBUG_KMS("using pipe b dpll\n");
4171 DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
4176 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4177 * This is an exception to the general rule that mode_set doesn't turn
4181 temp = I915_READ(PCH_LVDS);
4182 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4183 if (HAS_PCH_CPT(dev)) {
4184 temp &= ~PORT_TRANS_SEL_MASK;
4185 temp |= PORT_TRANS_SEL_CPT(pipe);
4188 temp |= LVDS_PIPEB_SELECT;
4190 temp &= ~LVDS_PIPEB_SELECT;
4193 /* set the corresponsding LVDS_BORDER bit */
4194 temp |= dev_priv->lvds_border_bits;
4195 /* Set the B0-B3 data pairs corresponding to whether we're going to
4196 * set the DPLLs for dual-channel mode or not.
4199 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4201 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4203 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4204 * appropriately here, but we need to look more thoroughly into how
4205 * panels behave in the two modes.
4207 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
4208 lvds_sync |= LVDS_HSYNC_POLARITY;
4209 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
4210 lvds_sync |= LVDS_VSYNC_POLARITY;
4211 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
4213 char flags[2] = "-+";
4214 DRM_INFO("Changing LVDS panel from "
4215 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
4216 flags[!(temp & LVDS_HSYNC_POLARITY)],
4217 flags[!(temp & LVDS_VSYNC_POLARITY)],
4218 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
4219 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
4220 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4223 I915_WRITE(PCH_LVDS, temp);
4226 pipeconf &= ~PIPECONF_DITHER_EN;
4227 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4228 if ((is_lvds && dev_priv->lvds_dither) || dither) {
4229 pipeconf |= PIPECONF_DITHER_EN;
4230 pipeconf |= PIPECONF_DITHER_TYPE_SP;
4232 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4233 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4235 /* For non-DP output, clear any trans DP clock recovery setting.*/
4236 I915_WRITE(TRANSDATA_M1(pipe), 0);
4237 I915_WRITE(TRANSDATA_N1(pipe), 0);
4238 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
4239 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
4242 if (!intel_crtc->no_pll &&
4243 (!has_edp_encoder ||
4244 intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
4245 I915_WRITE(_PCH_DPLL(pipe), dpll);
4247 /* Wait for the clocks to stabilize. */
4248 POSTING_READ(_PCH_DPLL(pipe));
4251 /* The pixel multiplier can only be updated once the
4252 * DPLL is enabled and the clocks are stable.
4254 * So write it again.
4256 I915_WRITE(_PCH_DPLL(pipe), dpll);
4259 intel_crtc->lowfreq_avail = false;
4260 if (!intel_crtc->no_pll) {
4261 if (is_lvds && has_reduced_clock && i915_powersave) {
4262 I915_WRITE(_PCH_FP1(pipe), fp2);
4263 intel_crtc->lowfreq_avail = true;
4264 if (HAS_PIPE_CXSR(dev)) {
4265 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4266 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4269 I915_WRITE(_PCH_FP1(pipe), fp);
4270 if (HAS_PIPE_CXSR(dev)) {
4271 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4272 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4277 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4278 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4279 pipeconf |= PIPECONF_INTERLACED_ILK;
4280 /* the chip adds 2 halflines automatically */
4281 adjusted_mode->crtc_vtotal -= 1;
4282 adjusted_mode->crtc_vblank_end -= 1;
4283 I915_WRITE(VSYNCSHIFT(pipe),
4284 adjusted_mode->crtc_hsync_start
4285 - adjusted_mode->crtc_htotal/2);
4287 pipeconf |= PIPECONF_PROGRESSIVE;
4288 I915_WRITE(VSYNCSHIFT(pipe), 0);
4291 I915_WRITE(HTOTAL(pipe),
4292 (adjusted_mode->crtc_hdisplay - 1) |
4293 ((adjusted_mode->crtc_htotal - 1) << 16));
4294 I915_WRITE(HBLANK(pipe),
4295 (adjusted_mode->crtc_hblank_start - 1) |
4296 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4297 I915_WRITE(HSYNC(pipe),
4298 (adjusted_mode->crtc_hsync_start - 1) |
4299 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4301 I915_WRITE(VTOTAL(pipe),
4302 (adjusted_mode->crtc_vdisplay - 1) |
4303 ((adjusted_mode->crtc_vtotal - 1) << 16));
4304 I915_WRITE(VBLANK(pipe),
4305 (adjusted_mode->crtc_vblank_start - 1) |
4306 ((adjusted_mode->crtc_vblank_end - 1) << 16));
4307 I915_WRITE(VSYNC(pipe),
4308 (adjusted_mode->crtc_vsync_start - 1) |
4309 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4311 /* pipesrc controls the size that is scaled from, which should
4312 * always be the user's requested size.
4314 I915_WRITE(PIPESRC(pipe),
4315 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4317 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4318 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4319 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4320 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
4322 if (has_edp_encoder &&
4323 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4324 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
4327 I915_WRITE(PIPECONF(pipe), pipeconf);
4328 POSTING_READ(PIPECONF(pipe));
4330 intel_wait_for_vblank(dev, pipe);
4332 I915_WRITE(DSPCNTR(plane), dspcntr);
4333 POSTING_READ(DSPCNTR(plane));
4335 ret = intel_pipe_set_base(crtc, x, y, old_fb);
4337 intel_update_watermarks(dev);
4342 static int intel_crtc_mode_set(struct drm_crtc *crtc,
4343 struct drm_display_mode *mode,
4344 struct drm_display_mode *adjusted_mode,
4346 struct drm_framebuffer *old_fb)
4348 struct drm_device *dev = crtc->dev;
4349 struct drm_i915_private *dev_priv = dev->dev_private;
4350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4351 int pipe = intel_crtc->pipe;
4354 drm_vblank_pre_modeset(dev, pipe);
4356 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
4358 drm_vblank_post_modeset(dev, pipe);
4361 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4363 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
4368 static bool intel_eld_uptodate(struct drm_connector *connector,
4369 int reg_eldv, uint32_t bits_eldv,
4370 int reg_elda, uint32_t bits_elda,
4373 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4374 uint8_t *eld = connector->eld;
4377 i = I915_READ(reg_eldv);
4386 i = I915_READ(reg_elda);
4388 I915_WRITE(reg_elda, i);
4390 for (i = 0; i < eld[2]; i++)
4391 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
4397 static void g4x_write_eld(struct drm_connector *connector,
4398 struct drm_crtc *crtc)
4400 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4401 uint8_t *eld = connector->eld;
4406 i = I915_READ(G4X_AUD_VID_DID);
4408 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
4409 eldv = G4X_ELDV_DEVCL_DEVBLC;
4411 eldv = G4X_ELDV_DEVCTG;
4413 if (intel_eld_uptodate(connector,
4414 G4X_AUD_CNTL_ST, eldv,
4415 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
4416 G4X_HDMIW_HDMIEDID))
4419 i = I915_READ(G4X_AUD_CNTL_ST);
4420 i &= ~(eldv | G4X_ELD_ADDR);
4421 len = (i >> 9) & 0x1f; /* ELD buffer size */
4422 I915_WRITE(G4X_AUD_CNTL_ST, i);
4427 if (eld[2] < (uint8_t)len)
4429 DRM_DEBUG_KMS("ELD size %d\n", len);
4430 for (i = 0; i < len; i++)
4431 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
4433 i = I915_READ(G4X_AUD_CNTL_ST);
4435 I915_WRITE(G4X_AUD_CNTL_ST, i);
4438 static void ironlake_write_eld(struct drm_connector *connector,
4439 struct drm_crtc *crtc)
4441 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4442 uint8_t *eld = connector->eld;
4451 if (HAS_PCH_IBX(connector->dev)) {
4452 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
4453 aud_config = IBX_AUD_CONFIG_A;
4454 aud_cntl_st = IBX_AUD_CNTL_ST_A;
4455 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
4457 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
4458 aud_config = CPT_AUD_CONFIG_A;
4459 aud_cntl_st = CPT_AUD_CNTL_ST_A;
4460 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
4463 i = to_intel_crtc(crtc)->pipe;
4464 hdmiw_hdmiedid += i * 0x100;
4465 aud_cntl_st += i * 0x100;
4466 aud_config += i * 0x100;
4468 DRM_DEBUG_KMS("ELD on pipe %c\n", pipe_name(i));
4470 i = I915_READ(aud_cntl_st);
4471 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
4473 DRM_DEBUG_KMS("Audio directed to unknown port\n");
4474 /* operate blindly on all ports */
4475 eldv = IBX_ELD_VALIDB;
4476 eldv |= IBX_ELD_VALIDB << 4;
4477 eldv |= IBX_ELD_VALIDB << 8;
4479 DRM_DEBUG_KMS("ELD on port %c\n", 'A' + i);
4480 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
4483 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
4484 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
4485 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
4486 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
4488 I915_WRITE(aud_config, 0);
4490 if (intel_eld_uptodate(connector,
4491 aud_cntrl_st2, eldv,
4492 aud_cntl_st, IBX_ELD_ADDRESS,
4496 i = I915_READ(aud_cntrl_st2);
4498 I915_WRITE(aud_cntrl_st2, i);
4503 i = I915_READ(aud_cntl_st);
4504 i &= ~IBX_ELD_ADDRESS;
4505 I915_WRITE(aud_cntl_st, i);
4507 /* 84 bytes of hw ELD buffer */
4509 if (eld[2] < (uint8_t)len)
4511 DRM_DEBUG_KMS("ELD size %d\n", len);
4512 for (i = 0; i < len; i++)
4513 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
4515 i = I915_READ(aud_cntrl_st2);
4517 I915_WRITE(aud_cntrl_st2, i);
4520 void intel_write_eld(struct drm_encoder *encoder,
4521 struct drm_display_mode *mode)
4523 struct drm_crtc *crtc = encoder->crtc;
4524 struct drm_connector *connector;
4525 struct drm_device *dev = encoder->dev;
4526 struct drm_i915_private *dev_priv = dev->dev_private;
4528 connector = drm_select_eld(encoder, mode);
4532 DRM_DEBUG_KMS("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4534 drm_get_connector_name(connector),
4535 connector->encoder->base.id,
4536 drm_get_encoder_name(connector->encoder));
4538 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
4540 if (dev_priv->display.write_eld)
4541 dev_priv->display.write_eld(connector, crtc);
4544 /** Loads the palette/gamma unit for the CRTC with the prepared values */
4545 void intel_crtc_load_lut(struct drm_crtc *crtc)
4547 struct drm_device *dev = crtc->dev;
4548 struct drm_i915_private *dev_priv = dev->dev_private;
4549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 int palreg = PALETTE(intel_crtc->pipe);
4553 /* The clocks have to be on to load the palette. */
4554 if (!crtc->enabled || !intel_crtc->active)
4557 /* use legacy palette for Ironlake */
4558 if (HAS_PCH_SPLIT(dev))
4559 palreg = LGC_PALETTE(intel_crtc->pipe);
4561 for (i = 0; i < 256; i++) {
4562 I915_WRITE(palreg + 4 * i,
4563 (intel_crtc->lut_r[i] << 16) |
4564 (intel_crtc->lut_g[i] << 8) |
4565 intel_crtc->lut_b[i]);
4569 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4571 struct drm_device *dev = crtc->dev;
4572 struct drm_i915_private *dev_priv = dev->dev_private;
4573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4574 bool visible = base != 0;
4577 if (intel_crtc->cursor_visible == visible)
4580 cntl = I915_READ(_CURACNTR);
4582 /* On these chipsets we can only modify the base whilst
4583 * the cursor is disabled.
4585 I915_WRITE(_CURABASE, base);
4587 cntl &= ~(CURSOR_FORMAT_MASK);
4588 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4589 cntl |= CURSOR_ENABLE |
4590 CURSOR_GAMMA_ENABLE |
4593 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4594 I915_WRITE(_CURACNTR, cntl);
4596 intel_crtc->cursor_visible = visible;
4599 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4601 struct drm_device *dev = crtc->dev;
4602 struct drm_i915_private *dev_priv = dev->dev_private;
4603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4604 int pipe = intel_crtc->pipe;
4605 bool visible = base != 0;
4607 if (intel_crtc->cursor_visible != visible) {
4608 uint32_t cntl = I915_READ(CURCNTR(pipe));
4610 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4611 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4612 cntl |= pipe << 28; /* Connect to correct pipe */
4614 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4615 cntl |= CURSOR_MODE_DISABLE;
4617 I915_WRITE(CURCNTR(pipe), cntl);
4619 intel_crtc->cursor_visible = visible;
4621 /* and commit changes on next vblank */
4622 I915_WRITE(CURBASE(pipe), base);
4625 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
4627 struct drm_device *dev = crtc->dev;
4628 struct drm_i915_private *dev_priv = dev->dev_private;
4629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4630 int pipe = intel_crtc->pipe;
4631 bool visible = base != 0;
4633 if (intel_crtc->cursor_visible != visible) {
4634 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
4636 cntl &= ~CURSOR_MODE;
4637 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4639 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4640 cntl |= CURSOR_MODE_DISABLE;
4642 I915_WRITE(CURCNTR_IVB(pipe), cntl);
4644 intel_crtc->cursor_visible = visible;
4646 /* and commit changes on next vblank */
4647 I915_WRITE(CURBASE_IVB(pipe), base);
4650 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
4651 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4654 struct drm_device *dev = crtc->dev;
4655 struct drm_i915_private *dev_priv = dev->dev_private;
4656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4657 int pipe = intel_crtc->pipe;
4658 int x = intel_crtc->cursor_x;
4659 int y = intel_crtc->cursor_y;
4665 if (on && crtc->enabled && crtc->fb) {
4666 base = intel_crtc->cursor_addr;
4667 if (x > (int) crtc->fb->width)
4670 if (y > (int) crtc->fb->height)
4676 if (x + intel_crtc->cursor_width < 0)
4679 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4682 pos |= x << CURSOR_X_SHIFT;
4685 if (y + intel_crtc->cursor_height < 0)
4688 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4691 pos |= y << CURSOR_Y_SHIFT;
4693 visible = base != 0;
4694 if (!visible && !intel_crtc->cursor_visible)
4697 if (IS_IVYBRIDGE(dev)) {
4698 I915_WRITE(CURPOS_IVB(pipe), pos);
4699 ivb_update_cursor(crtc, base);
4701 I915_WRITE(CURPOS(pipe), pos);
4702 if (IS_845G(dev) || IS_I865G(dev))
4703 i845_update_cursor(crtc, base);
4705 i9xx_update_cursor(crtc, base);
4709 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
4710 struct drm_file *file,
4712 uint32_t width, uint32_t height)
4714 struct drm_device *dev = crtc->dev;
4715 struct drm_i915_private *dev_priv = dev->dev_private;
4716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4717 struct drm_i915_gem_object *obj;
4721 DRM_DEBUG_KMS("\n");
4723 /* if we want to turn off the cursor ignore width and height */
4725 DRM_DEBUG_KMS("cursor off\n");
4732 /* Currently we only support 64x64 cursors */
4733 if (width != 64 || height != 64) {
4734 DRM_ERROR("we currently only support 64x64 cursors\n");
4738 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4739 if (&obj->base == NULL)
4742 if (obj->base.size < width * height * 4) {
4743 DRM_ERROR("buffer is to small\n");
4748 /* we only need to pin inside GTT if cursor is non-phy */
4750 if (!dev_priv->info->cursor_needs_physical) {
4751 if (obj->tiling_mode) {
4752 DRM_ERROR("cursor cannot be tiled\n");
4757 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
4759 DRM_ERROR("failed to move cursor bo into the GTT\n");
4763 ret = i915_gem_object_put_fence(obj);
4765 DRM_ERROR("failed to release fence for cursor\n");
4769 addr = obj->gtt_offset;
4771 int align = IS_I830(dev) ? 16 * 1024 : 256;
4772 ret = i915_gem_attach_phys_object(dev, obj,
4773 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4776 DRM_ERROR("failed to attach phys object\n");
4779 addr = obj->phys_obj->handle->busaddr;
4783 I915_WRITE(CURSIZE, (height << 12) | width);
4786 if (intel_crtc->cursor_bo) {
4787 if (dev_priv->info->cursor_needs_physical) {
4788 if (intel_crtc->cursor_bo != obj)
4789 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4791 i915_gem_object_unpin(intel_crtc->cursor_bo);
4792 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
4797 intel_crtc->cursor_addr = addr;
4798 intel_crtc->cursor_bo = obj;
4799 intel_crtc->cursor_width = width;
4800 intel_crtc->cursor_height = height;
4802 intel_crtc_update_cursor(crtc, true);
4806 i915_gem_object_unpin(obj);
4810 drm_gem_object_unreference_unlocked(&obj->base);
4814 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4818 intel_crtc->cursor_x = x;
4819 intel_crtc->cursor_y = y;
4821 intel_crtc_update_cursor(crtc, true);
4826 /** Sets the color ramps on behalf of RandR */
4827 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4828 u16 blue, int regno)
4830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4832 intel_crtc->lut_r[regno] = red >> 8;
4833 intel_crtc->lut_g[regno] = green >> 8;
4834 intel_crtc->lut_b[regno] = blue >> 8;
4837 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4838 u16 *blue, int regno)
4840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4842 *red = intel_crtc->lut_r[regno] << 8;
4843 *green = intel_crtc->lut_g[regno] << 8;
4844 *blue = intel_crtc->lut_b[regno] << 8;
4847 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
4848 u16 *blue, uint32_t start, uint32_t size)
4850 int end = (start + size > 256) ? 256 : start + size, i;
4851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4853 for (i = start; i < end; i++) {
4854 intel_crtc->lut_r[i] = red[i] >> 8;
4855 intel_crtc->lut_g[i] = green[i] >> 8;
4856 intel_crtc->lut_b[i] = blue[i] >> 8;
4859 intel_crtc_load_lut(crtc);
4863 * Get a pipe with a simple mode set on it for doing load-based monitor
4866 * It will be up to the load-detect code to adjust the pipe as appropriate for
4867 * its requirements. The pipe will be connected to no other encoders.
4869 * Currently this code will only succeed if there is a pipe with no encoders
4870 * configured for it. In the future, it could choose to temporarily disable
4871 * some outputs to free up a pipe for its use.
4873 * \return crtc, or NULL if no pipes are available.
4876 /* VESA 640x480x72Hz mode to set on the pipe */
4877 static struct drm_display_mode load_detect_mode = {
4878 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4879 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4883 intel_framebuffer_create(struct drm_device *dev,
4884 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_i915_gem_object *obj,
4885 struct drm_framebuffer **res)
4887 struct intel_framebuffer *intel_fb;
4890 intel_fb = kmalloc(sizeof(*intel_fb), DRM_MEM_KMS, M_WAITOK | M_ZERO);
4891 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
4893 drm_gem_object_unreference_unlocked(&obj->base);
4894 drm_free(intel_fb, DRM_MEM_KMS);
4898 *res = &intel_fb->base;
4903 intel_framebuffer_pitch_for_width(int width, int bpp)
4905 u32 pitch = howmany(width * bpp, 8);
4906 return roundup2(pitch, 64);
4910 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
4912 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
4913 return roundup2(pitch * mode->vdisplay, PAGE_SIZE);
4917 intel_framebuffer_create_for_mode(struct drm_device *dev,
4918 struct drm_display_mode *mode, int depth, int bpp,
4919 struct drm_framebuffer **res)
4921 struct drm_i915_gem_object *obj;
4922 struct drm_mode_fb_cmd2 mode_cmd;
4924 obj = i915_gem_alloc_object(dev,
4925 intel_framebuffer_size_for_mode(mode, bpp));
4929 mode_cmd.width = mode->hdisplay;
4930 mode_cmd.height = mode->vdisplay;
4931 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
4933 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
4935 return (intel_framebuffer_create(dev, &mode_cmd, obj, res));
4939 mode_fits_in_fbdev(struct drm_device *dev,
4940 struct drm_display_mode *mode, struct drm_framebuffer **res)
4942 struct drm_i915_private *dev_priv = dev->dev_private;
4943 struct drm_i915_gem_object *obj;
4944 struct drm_framebuffer *fb;
4946 if (dev_priv->fbdev == NULL) {
4951 obj = dev_priv->fbdev->ifb.obj;
4957 fb = &dev_priv->fbdev->ifb.base;
4958 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
4959 fb->bits_per_pixel)) {
4964 if (obj->base.size < mode->vdisplay * fb->pitches[0]) {
4973 bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
4974 struct drm_connector *connector,
4975 struct drm_display_mode *mode,
4976 struct intel_load_detect_pipe *old)
4978 struct intel_crtc *intel_crtc;
4979 struct drm_crtc *possible_crtc;
4980 struct drm_encoder *encoder = &intel_encoder->base;
4981 struct drm_crtc *crtc = NULL;
4982 struct drm_device *dev = encoder->dev;
4983 struct drm_framebuffer *old_fb;
4986 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4987 connector->base.id, drm_get_connector_name(connector),
4988 encoder->base.id, drm_get_encoder_name(encoder));
4991 * Algorithm gets a little messy:
4993 * - if the connector already has an assigned crtc, use it (but make
4994 * sure it's on first)
4996 * - try to find the first unused crtc that can drive this connector,
4997 * and use that if we find one
5000 /* See if we already have a CRTC for this connector */
5001 if (encoder->crtc) {
5002 crtc = encoder->crtc;
5004 intel_crtc = to_intel_crtc(crtc);
5005 old->dpms_mode = intel_crtc->dpms_mode;
5006 old->load_detect_temp = false;
5008 /* Make sure the crtc and connector are running */
5009 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
5010 struct drm_encoder_helper_funcs *encoder_funcs;
5011 struct drm_crtc_helper_funcs *crtc_funcs;
5013 crtc_funcs = crtc->helper_private;
5014 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
5016 encoder_funcs = encoder->helper_private;
5017 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
5023 /* Find an unused one (if possible) */
5024 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5026 if (!(encoder->possible_crtcs & (1 << i)))
5028 if (!possible_crtc->enabled) {
5029 crtc = possible_crtc;
5035 * If we didn't find an unused CRTC, don't use any.
5038 DRM_DEBUG_KMS("no pipe available for load-detect\n");
5042 encoder->crtc = crtc;
5043 connector->encoder = encoder;
5045 intel_crtc = to_intel_crtc(crtc);
5046 old->dpms_mode = intel_crtc->dpms_mode;
5047 old->load_detect_temp = true;
5048 old->release_fb = NULL;
5051 mode = &load_detect_mode;
5055 /* We need a framebuffer large enough to accommodate all accesses
5056 * that the plane may generate whilst we perform load detection.
5057 * We can not rely on the fbcon either being present (we get called
5058 * during its initialisation to detect all boot displays, or it may
5059 * not even exist) or that it is large enough to satisfy the
5062 r = mode_fits_in_fbdev(dev, mode, &crtc->fb);
5063 if (crtc->fb == NULL) {
5064 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5065 r = intel_framebuffer_create_for_mode(dev, mode, 24, 32,
5067 old->release_fb = crtc->fb;
5069 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5071 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5076 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
5077 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
5078 if (old->release_fb)
5079 old->release_fb->funcs->destroy(old->release_fb);
5084 /* let the connector get through one full cycle before testing */
5085 intel_wait_for_vblank(dev, intel_crtc->pipe);
5090 void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
5091 struct drm_connector *connector,
5092 struct intel_load_detect_pipe *old)
5094 struct drm_encoder *encoder = &intel_encoder->base;
5095 struct drm_device *dev = encoder->dev;
5096 struct drm_crtc *crtc = encoder->crtc;
5097 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5098 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5100 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5101 connector->base.id, drm_get_connector_name(connector),
5102 encoder->base.id, drm_get_encoder_name(encoder));
5104 if (old->load_detect_temp) {
5105 connector->encoder = NULL;
5106 drm_helper_disable_unused_functions(dev);
5108 if (old->release_fb)
5109 old->release_fb->funcs->destroy(old->release_fb);
5114 /* Switch crtc and encoder back off if necessary */
5115 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
5116 encoder_funcs->dpms(encoder, old->dpms_mode);
5117 crtc_funcs->dpms(crtc, old->dpms_mode);
5121 /* Returns the clock of the currently programmed mode of the given pipe. */
5122 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5124 struct drm_i915_private *dev_priv = dev->dev_private;
5125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5126 int pipe = intel_crtc->pipe;
5127 u32 dpll = I915_READ(DPLL(pipe));
5129 intel_clock_t clock;
5131 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5132 fp = I915_READ(FP0(pipe));
5134 fp = I915_READ(FP1(pipe));
5136 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5137 if (IS_PINEVIEW(dev)) {
5138 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5139 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5141 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5142 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5145 if (!IS_GEN2(dev)) {
5146 if (IS_PINEVIEW(dev))
5147 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5148 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5150 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5151 DPLL_FPA01_P1_POST_DIV_SHIFT);
5153 switch (dpll & DPLL_MODE_MASK) {
5154 case DPLLB_MODE_DAC_SERIAL:
5155 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5158 case DPLLB_MODE_LVDS:
5159 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5163 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
5164 "mode\n", (int)(dpll & DPLL_MODE_MASK));
5168 /* XXX: Handle the 100Mhz refclk */
5169 intel_clock(dev, 96000, &clock);
5171 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5174 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5175 DPLL_FPA01_P1_POST_DIV_SHIFT);
5178 if ((dpll & PLL_REF_INPUT_MASK) ==
5179 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5180 /* XXX: might not be 66MHz */
5181 intel_clock(dev, 66000, &clock);
5183 intel_clock(dev, 48000, &clock);
5185 if (dpll & PLL_P1_DIVIDE_BY_TWO)
5188 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5189 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5191 if (dpll & PLL_P2_DIVIDE_BY_4)
5196 intel_clock(dev, 48000, &clock);
5200 /* XXX: It would be nice to validate the clocks, but we can't reuse
5201 * i830PllIsValid() because it relies on the xf86_config connector
5202 * configuration being accurate, which it isn't necessarily.
5208 /** Returns the currently programmed mode of the given pipe. */
5209 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5210 struct drm_crtc *crtc)
5212 struct drm_i915_private *dev_priv = dev->dev_private;
5213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5214 int pipe = intel_crtc->pipe;
5215 struct drm_display_mode *mode;
5216 int htot = I915_READ(HTOTAL(pipe));
5217 int hsync = I915_READ(HSYNC(pipe));
5218 int vtot = I915_READ(VTOTAL(pipe));
5219 int vsync = I915_READ(VSYNC(pipe));
5221 mode = kmalloc(sizeof(*mode), DRM_MEM_KMS, M_WAITOK | M_ZERO);
5223 mode->clock = intel_crtc_clock_get(dev, crtc);
5224 mode->hdisplay = (htot & 0xffff) + 1;
5225 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5226 mode->hsync_start = (hsync & 0xffff) + 1;
5227 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5228 mode->vdisplay = (vtot & 0xffff) + 1;
5229 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5230 mode->vsync_start = (vsync & 0xffff) + 1;
5231 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5233 drm_mode_set_name(mode);
5234 drm_mode_set_crtcinfo(mode, 0);
5239 static void intel_increase_pllclock(struct drm_crtc *crtc)
5241 struct drm_device *dev = crtc->dev;
5242 drm_i915_private_t *dev_priv = dev->dev_private;
5243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5244 int pipe = intel_crtc->pipe;
5245 int dpll_reg = DPLL(pipe);
5248 if (HAS_PCH_SPLIT(dev))
5251 if (!dev_priv->lvds_downclock_avail)
5254 dpll = I915_READ(dpll_reg);
5255 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
5256 DRM_DEBUG_DRIVER("upclocking LVDS\n");
5258 assert_panel_unlocked(dev_priv, pipe);
5260 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5261 I915_WRITE(dpll_reg, dpll);
5262 intel_wait_for_vblank(dev, pipe);
5264 dpll = I915_READ(dpll_reg);
5265 if (dpll & DISPLAY_RATE_SELECT_FPA1)
5266 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
5270 static void intel_decrease_pllclock(struct drm_crtc *crtc)
5272 struct drm_device *dev = crtc->dev;
5273 drm_i915_private_t *dev_priv = dev->dev_private;
5274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5276 if (HAS_PCH_SPLIT(dev))
5279 if (!dev_priv->lvds_downclock_avail)
5283 * Since this is called by a timer, we should never get here in
5286 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
5287 int pipe = intel_crtc->pipe;
5288 int dpll_reg = DPLL(pipe);
5291 DRM_DEBUG_DRIVER("downclocking LVDS\n");
5293 assert_panel_unlocked(dev_priv, pipe);
5295 dpll = I915_READ(dpll_reg);
5296 dpll |= DISPLAY_RATE_SELECT_FPA1;
5297 I915_WRITE(dpll_reg, dpll);
5298 intel_wait_for_vblank(dev, pipe);
5299 dpll = I915_READ(dpll_reg);
5300 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
5301 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
5306 * intel_idle_update - adjust clocks for idleness
5307 * @work: work struct
5309 * Either the GPU or display (or both) went idle. Check the busy status
5310 * here and adjust the CRTC and GPU clocks as necessary.
5312 static void intel_idle_update(void *arg, int pending)
5314 drm_i915_private_t *dev_priv = arg;
5315 struct drm_device *dev = dev_priv->dev;
5316 struct drm_crtc *crtc;
5317 struct intel_crtc *intel_crtc;
5319 if (!i915_powersave)
5324 i915_update_gfx_val(dev_priv);
5326 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5327 /* Skip inactive CRTCs */
5331 intel_crtc = to_intel_crtc(crtc);
5332 if (!intel_crtc->busy)
5333 intel_decrease_pllclock(crtc);
5339 void intel_mark_busy(struct drm_device *dev)
5341 i915_update_gfx_val(dev->dev_private);
5344 static void intel_crtc_destroy(struct drm_crtc *crtc)
5346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5347 struct drm_device *dev = crtc->dev;
5348 struct drm_i915_private *dev_priv = dev->dev_private;
5349 struct intel_unpin_work *work;
5351 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5352 work = intel_crtc->unpin_work;
5353 intel_crtc->unpin_work = NULL;
5354 lockmgr(&dev->event_lock, LK_RELEASE);
5357 taskqueue_cancel(dev_priv->tq, &work->task, NULL);
5358 taskqueue_drain(dev_priv->tq, &work->task);
5359 drm_free(work, DRM_MEM_KMS);
5362 drm_crtc_cleanup(crtc);
5364 drm_free(intel_crtc, DRM_MEM_KMS);
5367 static void intel_unpin_work_fn(void *arg, int pending)
5369 struct intel_unpin_work *work = arg;
5370 struct drm_device *dev;
5374 intel_unpin_fb_obj(work->old_fb_obj);
5375 drm_gem_object_unreference(&work->pending_flip_obj->base);
5376 drm_gem_object_unreference(&work->old_fb_obj->base);
5378 intel_update_fbc(work->dev);
5380 drm_free(work, DRM_MEM_KMS);
5383 static void do_intel_finish_page_flip(struct drm_device *dev,
5384 struct drm_crtc *crtc)
5386 drm_i915_private_t *dev_priv = dev->dev_private;
5387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5388 struct intel_unpin_work *work;
5389 struct drm_i915_gem_object *obj;
5391 /* Ignore early vblank irqs */
5392 if (intel_crtc == NULL)
5395 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5396 work = intel_crtc->unpin_work;
5397 if (work == NULL || !atomic_read(&work->pending)) {
5398 lockmgr(&dev->event_lock, LK_RELEASE);
5402 intel_crtc->unpin_work = NULL;
5405 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
5407 drm_vblank_put(dev, intel_crtc->pipe);
5409 lockmgr(&dev->event_lock, LK_RELEASE);
5411 obj = work->old_fb_obj;
5413 atomic_clear_mask(1 << intel_crtc->plane,
5414 &obj->pending_flip.counter);
5415 wakeup(&obj->pending_flip);
5417 taskqueue_enqueue(dev_priv->tq, &work->task);
5420 void intel_finish_page_flip(struct drm_device *dev, int pipe)
5422 drm_i915_private_t *dev_priv = dev->dev_private;
5423 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5425 do_intel_finish_page_flip(dev, crtc);
5428 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5430 drm_i915_private_t *dev_priv = dev->dev_private;
5431 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5433 do_intel_finish_page_flip(dev, crtc);
5436 void intel_prepare_page_flip(struct drm_device *dev, int plane)
5438 drm_i915_private_t *dev_priv = dev->dev_private;
5439 struct intel_crtc *intel_crtc =
5440 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5442 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5443 if (intel_crtc->unpin_work)
5444 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
5445 lockmgr(&dev->event_lock, LK_RELEASE);
5448 static int intel_gen2_queue_flip(struct drm_device *dev,
5449 struct drm_crtc *crtc,
5450 struct drm_framebuffer *fb,
5451 struct drm_i915_gem_object *obj)
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5455 unsigned long offset;
5459 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5463 /* Offset into the new buffer for cases of shared fbs between CRTCs */
5464 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5466 ret = BEGIN_LP_RING(6);
5470 /* Can't queue multiple flips, so wait for the previous
5471 * one to finish before executing the next.
5473 if (intel_crtc->plane)
5474 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5476 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5477 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5479 OUT_RING(MI_DISPLAY_FLIP |
5480 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5481 OUT_RING(fb->pitches[0]);
5482 OUT_RING(obj->gtt_offset + offset);
5483 OUT_RING(0); /* aux display base address, unused */
5489 static int intel_gen3_queue_flip(struct drm_device *dev,
5490 struct drm_crtc *crtc,
5491 struct drm_framebuffer *fb,
5492 struct drm_i915_gem_object *obj)
5494 struct drm_i915_private *dev_priv = dev->dev_private;
5495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5496 unsigned long offset;
5500 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5504 /* Offset into the new buffer for cases of shared fbs between CRTCs */
5505 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5507 ret = BEGIN_LP_RING(6);
5511 if (intel_crtc->plane)
5512 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5514 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5515 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5517 OUT_RING(MI_DISPLAY_FLIP_I915 |
5518 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5519 OUT_RING(fb->pitches[0]);
5520 OUT_RING(obj->gtt_offset + offset);
5528 static int intel_gen4_queue_flip(struct drm_device *dev,
5529 struct drm_crtc *crtc,
5530 struct drm_framebuffer *fb,
5531 struct drm_i915_gem_object *obj)
5533 struct drm_i915_private *dev_priv = dev->dev_private;
5534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5535 uint32_t pf, pipesrc;
5538 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5542 ret = BEGIN_LP_RING(4);
5546 /* i965+ uses the linear or tiled offsets from the
5547 * Display Registers (which do not change across a page-flip)
5548 * so we need only reprogram the base address.
5550 OUT_RING(MI_DISPLAY_FLIP |
5551 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5552 OUT_RING(fb->pitches[0]);
5553 OUT_RING(obj->gtt_offset | obj->tiling_mode);
5555 /* XXX Enabling the panel-fitter across page-flip is so far
5556 * untested on non-native modes, so ignore it for now.
5557 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5560 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5561 OUT_RING(pf | pipesrc);
5567 static int intel_gen6_queue_flip(struct drm_device *dev,
5568 struct drm_crtc *crtc,
5569 struct drm_framebuffer *fb,
5570 struct drm_i915_gem_object *obj)
5572 struct drm_i915_private *dev_priv = dev->dev_private;
5573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5574 uint32_t pf, pipesrc;
5577 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5581 ret = BEGIN_LP_RING(4);
5585 OUT_RING(MI_DISPLAY_FLIP |
5586 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5587 OUT_RING(fb->pitches[0] | obj->tiling_mode);
5588 OUT_RING(obj->gtt_offset);
5590 /* Contrary to the suggestions in the documentation,
5591 * "Enable Panel Fitter" does not seem to be required when page
5592 * flipping with a non-native mode, and worse causes a normal
5594 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
5597 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5598 OUT_RING(pf | pipesrc);
5605 * On gen7 we currently use the blit ring because (in early silicon at least)
5606 * the render ring doesn't give us interrpts for page flip completion, which
5607 * means clients will hang after the first flip is queued. Fortunately the
5608 * blit ring generates interrupts properly, so use it instead.
5610 static int intel_gen7_queue_flip(struct drm_device *dev,
5611 struct drm_crtc *crtc,
5612 struct drm_framebuffer *fb,
5613 struct drm_i915_gem_object *obj)
5615 struct drm_i915_private *dev_priv = dev->dev_private;
5616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5617 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
5620 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
5624 ret = intel_ring_begin(ring, 4);
5628 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
5629 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
5630 intel_ring_emit(ring, (obj->gtt_offset));
5631 intel_ring_emit(ring, (MI_NOOP));
5632 intel_ring_advance(ring);
5637 static int intel_default_queue_flip(struct drm_device *dev,
5638 struct drm_crtc *crtc,
5639 struct drm_framebuffer *fb,
5640 struct drm_i915_gem_object *obj)
5645 static int intel_crtc_page_flip(struct drm_crtc *crtc,
5646 struct drm_framebuffer *fb,
5647 struct drm_pending_vblank_event *event)
5649 struct drm_device *dev = crtc->dev;
5650 struct drm_i915_private *dev_priv = dev->dev_private;
5651 struct intel_framebuffer *intel_fb;
5652 struct drm_i915_gem_object *obj;
5653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5654 struct intel_unpin_work *work;
5657 work = kmalloc(sizeof *work, DRM_MEM_KMS, M_WAITOK | M_ZERO);
5659 work->event = event;
5660 work->dev = crtc->dev;
5661 intel_fb = to_intel_framebuffer(crtc->fb);
5662 work->old_fb_obj = intel_fb->obj;
5663 TASK_INIT(&work->task, 0, intel_unpin_work_fn, work);
5665 ret = drm_vblank_get(dev, intel_crtc->pipe);
5669 /* We borrow the event spin lock for protecting unpin_work */
5670 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5671 if (intel_crtc->unpin_work) {
5672 lockmgr(&dev->event_lock, LK_RELEASE);
5673 drm_free(work, DRM_MEM_KMS);
5674 drm_vblank_put(dev, intel_crtc->pipe);
5676 DRM_DEBUG("flip queue: crtc already busy\n");
5679 intel_crtc->unpin_work = work;
5680 lockmgr(&dev->event_lock, LK_RELEASE);
5682 intel_fb = to_intel_framebuffer(fb);
5683 obj = intel_fb->obj;
5687 /* Reference the objects for the scheduled work. */
5688 drm_gem_object_reference(&work->old_fb_obj->base);
5689 drm_gem_object_reference(&obj->base);
5693 work->pending_flip_obj = obj;
5695 work->enable_stall_check = true;
5697 /* Block clients from rendering to the new back buffer until
5698 * the flip occurs and the object is no longer visible.
5700 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5702 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
5704 goto cleanup_pending;
5705 intel_disable_fbc(dev);
5711 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5712 drm_gem_object_unreference(&work->old_fb_obj->base);
5713 drm_gem_object_unreference(&obj->base);
5716 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5717 intel_crtc->unpin_work = NULL;
5718 lockmgr(&dev->event_lock, LK_RELEASE);
5720 drm_vblank_put(dev, intel_crtc->pipe);
5722 drm_free(work, DRM_MEM_KMS);
5727 static void intel_sanitize_modesetting(struct drm_device *dev,
5728 int pipe, int plane)
5730 struct drm_i915_private *dev_priv = dev->dev_private;
5733 /* Clear any frame start delays used for debugging left by the BIOS */
5734 for_each_pipe(pipe) {
5735 reg = PIPECONF(pipe);
5736 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
5739 if (HAS_PCH_SPLIT(dev))
5742 /* Who knows what state these registers were left in by the BIOS or
5745 * If we leave the registers in a conflicting state (e.g. with the
5746 * display plane reading from the other pipe than the one we intend
5747 * to use) then when we attempt to teardown the active mode, we will
5748 * not disable the pipes and planes in the correct order -- leaving
5749 * a plane reading from a disabled pipe and possibly leading to
5750 * undefined behaviour.
5753 reg = DSPCNTR(plane);
5754 val = I915_READ(reg);
5756 if ((val & DISPLAY_PLANE_ENABLE) == 0)
5758 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5761 /* This display plane is active and attached to the other CPU pipe. */
5764 /* Disable the plane and wait for it to stop reading from the pipe. */
5765 intel_disable_plane(dev_priv, plane, pipe);
5766 intel_disable_pipe(dev_priv, pipe);
5769 static void intel_crtc_reset(struct drm_crtc *crtc)
5771 struct drm_device *dev = crtc->dev;
5772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5774 /* Reset flags back to the 'unknown' status so that they
5775 * will be correctly set on the initial modeset.
5777 intel_crtc->dpms_mode = -1;
5779 /* We need to fix up any BIOS configuration that conflicts with
5782 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
5785 static struct drm_crtc_helper_funcs intel_helper_funcs = {
5786 .dpms = intel_crtc_dpms,
5787 .mode_fixup = intel_crtc_mode_fixup,
5788 .mode_set = intel_crtc_mode_set,
5789 .mode_set_base = intel_pipe_set_base,
5790 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5791 .load_lut = intel_crtc_load_lut,
5792 .disable = intel_crtc_disable,
5795 static const struct drm_crtc_funcs intel_crtc_funcs = {
5796 .reset = intel_crtc_reset,
5797 .cursor_set = intel_crtc_cursor_set,
5798 .cursor_move = intel_crtc_cursor_move,
5799 .gamma_set = intel_crtc_gamma_set,
5800 .set_config = drm_crtc_helper_set_config,
5801 .destroy = intel_crtc_destroy,
5802 .page_flip = intel_crtc_page_flip,
5805 static void intel_crtc_init(struct drm_device *dev, int pipe)
5807 drm_i915_private_t *dev_priv = dev->dev_private;
5808 struct intel_crtc *intel_crtc;
5811 intel_crtc = kmalloc(sizeof(struct intel_crtc) +
5812 (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
5813 DRM_MEM_KMS, M_WAITOK | M_ZERO);
5815 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5817 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
5818 for (i = 0; i < 256; i++) {
5819 intel_crtc->lut_r[i] = i;
5820 intel_crtc->lut_g[i] = i;
5821 intel_crtc->lut_b[i] = i;
5824 /* Swap pipes & planes for FBC on pre-965 */
5825 intel_crtc->pipe = pipe;
5826 intel_crtc->plane = pipe;
5827 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
5828 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
5829 intel_crtc->plane = !pipe;
5832 KASSERT(pipe < DRM_ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) &&
5833 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] == NULL,
5834 ("plane_to_crtc is already initialized"));
5835 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5836 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5838 intel_crtc_reset(&intel_crtc->base);
5839 intel_crtc->active = true; /* force the pipe off on setup_init_config */
5840 intel_crtc->bpp = 24; /* default for pre-Ironlake */
5842 if (HAS_PCH_SPLIT(dev)) {
5843 if (pipe == 2 && IS_IVYBRIDGE(dev))
5844 intel_crtc->no_pll = true;
5845 intel_helper_funcs.prepare = ironlake_crtc_prepare;
5846 intel_helper_funcs.commit = ironlake_crtc_commit;
5848 intel_helper_funcs.prepare = i9xx_crtc_prepare;
5849 intel_helper_funcs.commit = i9xx_crtc_commit;
5852 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5854 intel_crtc->busy = false;
5856 callout_init_mp(&intel_crtc->idle_callout);
5859 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
5860 struct drm_file *file)
5862 drm_i915_private_t *dev_priv = dev->dev_private;
5863 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
5864 struct drm_mode_object *drmmode_obj;
5865 struct intel_crtc *crtc;
5868 DRM_ERROR("called with no initialization\n");
5872 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5873 DRM_MODE_OBJECT_CRTC);
5876 DRM_ERROR("no such CRTC id\n");
5880 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5881 pipe_from_crtc_id->pipe = crtc->pipe;
5886 static int intel_encoder_clones(struct drm_device *dev, int type_mask)
5888 struct intel_encoder *encoder;
5892 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5893 if (type_mask & encoder->clone_mask)
5894 index_mask |= (1 << entry);
5901 static bool has_edp_a(struct drm_device *dev)
5903 struct drm_i915_private *dev_priv = dev->dev_private;
5905 if (!IS_MOBILE(dev))
5908 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5912 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
5918 static void intel_setup_outputs(struct drm_device *dev)
5920 struct drm_i915_private *dev_priv = dev->dev_private;
5921 struct intel_encoder *encoder;
5922 bool dpd_is_edp = false;
5925 has_lvds = intel_lvds_init(dev);
5926 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
5927 /* disable the panel fitter on everything but LVDS */
5928 I915_WRITE(PFIT_CONTROL, 0);
5931 if (HAS_PCH_SPLIT(dev)) {
5932 dpd_is_edp = intel_dpd_is_edp(dev);
5935 intel_dp_init(dev, DP_A);
5937 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5938 intel_dp_init(dev, PCH_DP_D);
5941 intel_crt_init(dev);
5943 if (HAS_PCH_SPLIT(dev)) {
5947 "HDMIB %d PCH_DP_B %d HDMIC %d HDMID %d PCH_DP_C %d PCH_DP_D %d LVDS %d\n",
5948 (I915_READ(HDMIB) & PORT_DETECTED) != 0,
5949 (I915_READ(PCH_DP_B) & DP_DETECTED) != 0,
5950 (I915_READ(HDMIC) & PORT_DETECTED) != 0,
5951 (I915_READ(HDMID) & PORT_DETECTED) != 0,
5952 (I915_READ(PCH_DP_C) & DP_DETECTED) != 0,
5953 (I915_READ(PCH_DP_D) & DP_DETECTED) != 0,
5954 (I915_READ(PCH_LVDS) & LVDS_DETECTED) != 0);
5956 if (I915_READ(HDMIB) & PORT_DETECTED) {
5957 /* PCH SDVOB multiplex with HDMIB */
5958 found = intel_sdvo_init(dev, PCH_SDVOB);
5960 intel_hdmi_init(dev, HDMIB);
5961 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
5962 intel_dp_init(dev, PCH_DP_B);
5965 if (I915_READ(HDMIC) & PORT_DETECTED)
5966 intel_hdmi_init(dev, HDMIC);
5968 if (I915_READ(HDMID) & PORT_DETECTED)
5969 intel_hdmi_init(dev, HDMID);
5971 if (I915_READ(PCH_DP_C) & DP_DETECTED)
5972 intel_dp_init(dev, PCH_DP_C);
5974 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
5975 intel_dp_init(dev, PCH_DP_D);
5977 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
5980 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5981 DRM_DEBUG_KMS("probing SDVOB\n");
5982 found = intel_sdvo_init(dev, SDVOB);
5983 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
5984 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
5985 intel_hdmi_init(dev, SDVOB);
5988 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
5989 DRM_DEBUG_KMS("probing DP_B\n");
5990 intel_dp_init(dev, DP_B);
5994 /* Before G4X SDVOC doesn't have its own detect register */
5996 if (I915_READ(SDVOB) & SDVO_DETECTED) {
5997 DRM_DEBUG_KMS("probing SDVOC\n");
5998 found = intel_sdvo_init(dev, SDVOC);
6001 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
6003 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
6004 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
6005 intel_hdmi_init(dev, SDVOC);
6007 if (SUPPORTS_INTEGRATED_DP(dev)) {
6008 DRM_DEBUG_KMS("probing DP_C\n");
6009 intel_dp_init(dev, DP_C);
6013 if (SUPPORTS_INTEGRATED_DP(dev) &&
6014 (I915_READ(DP_D) & DP_DETECTED)) {
6015 DRM_DEBUG_KMS("probing DP_D\n");
6016 intel_dp_init(dev, DP_D);
6018 } else if (IS_GEN2(dev)) {
6022 intel_dvo_init(dev);
6026 if (SUPPORTS_TV(dev))
6029 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
6030 encoder->base.possible_crtcs = encoder->crtc_mask;
6031 encoder->base.possible_clones =
6032 intel_encoder_clones(dev, encoder->clone_mask);
6035 /* disable all the possible outputs/crtcs before entering KMS mode */
6036 drm_helper_disable_unused_functions(dev);
6038 if (HAS_PCH_SPLIT(dev))
6039 ironlake_init_pch_refclk(dev);
6042 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
6044 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6046 drm_framebuffer_cleanup(fb);
6047 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
6049 drm_free(intel_fb, DRM_MEM_KMS);
6052 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
6053 struct drm_file *file,
6054 unsigned int *handle)
6056 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6057 struct drm_i915_gem_object *obj = intel_fb->obj;
6059 return drm_gem_handle_create(file, &obj->base, handle);
6062 static const struct drm_framebuffer_funcs intel_fb_funcs = {
6063 .destroy = intel_user_framebuffer_destroy,
6064 .create_handle = intel_user_framebuffer_create_handle,
6067 int intel_framebuffer_init(struct drm_device *dev,
6068 struct intel_framebuffer *intel_fb,
6069 struct drm_mode_fb_cmd2 *mode_cmd,
6070 struct drm_i915_gem_object *obj)
6074 if (obj->tiling_mode == I915_TILING_Y)
6077 if (mode_cmd->pitches[0] & 63)
6080 switch (mode_cmd->pixel_format) {
6081 case DRM_FORMAT_RGB332:
6082 case DRM_FORMAT_RGB565:
6083 case DRM_FORMAT_XRGB8888:
6084 case DRM_FORMAT_XBGR8888:
6085 case DRM_FORMAT_ARGB8888:
6086 case DRM_FORMAT_XRGB2101010:
6087 case DRM_FORMAT_ARGB2101010:
6088 /* RGB formats are common across chipsets */
6090 case DRM_FORMAT_YUYV:
6091 case DRM_FORMAT_UYVY:
6092 case DRM_FORMAT_YVYU:
6093 case DRM_FORMAT_VYUY:
6096 DRM_DEBUG_KMS("unsupported pixel format %u\n",
6097 mode_cmd->pixel_format);
6101 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
6103 DRM_ERROR("framebuffer init failed %d\n", ret);
6107 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
6108 intel_fb->obj = obj;
6113 intel_user_framebuffer_create(struct drm_device *dev,
6114 struct drm_file *filp, struct drm_mode_fb_cmd2 *mode_cmd,
6115 struct drm_framebuffer **res)
6117 struct drm_i915_gem_object *obj;
6119 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
6120 mode_cmd->handles[0]));
6121 if (&obj->base == NULL)
6124 return (intel_framebuffer_create(dev, mode_cmd, obj, res));
6127 static const struct drm_mode_config_funcs intel_mode_funcs = {
6128 .fb_create = intel_user_framebuffer_create,
6129 .output_poll_changed = intel_fb_output_poll_changed,
6132 /* Set up chip specific display functions */
6133 static void intel_init_display(struct drm_device *dev)
6135 struct drm_i915_private *dev_priv = dev->dev_private;
6137 /* We always want a DPMS function */
6138 if (HAS_PCH_SPLIT(dev)) {
6139 dev_priv->display.dpms = ironlake_crtc_dpms;
6140 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
6141 dev_priv->display.update_plane = ironlake_update_plane;
6143 dev_priv->display.dpms = i9xx_crtc_dpms;
6144 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
6145 dev_priv->display.update_plane = i9xx_update_plane;
6148 if (I915_HAS_FBC(dev)) {
6149 if (HAS_PCH_SPLIT(dev)) {
6150 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6151 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6152 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6153 } else if (IS_GM45(dev)) {
6154 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6155 dev_priv->display.enable_fbc = g4x_enable_fbc;
6156 dev_priv->display.disable_fbc = g4x_disable_fbc;
6157 } else if (IS_CRESTLINE(dev)) {
6158 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6159 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6160 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6162 /* 855GM needs testing */
6165 /* Returns the core display clock speed */
6166 if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
6167 dev_priv->display.get_display_clock_speed =
6168 i945_get_display_clock_speed;
6169 else if (IS_I915G(dev))
6170 dev_priv->display.get_display_clock_speed =
6171 i915_get_display_clock_speed;
6172 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
6173 dev_priv->display.get_display_clock_speed =
6174 i9xx_misc_get_display_clock_speed;
6175 else if (IS_I915GM(dev))
6176 dev_priv->display.get_display_clock_speed =
6177 i915gm_get_display_clock_speed;
6178 else if (IS_I865G(dev))
6179 dev_priv->display.get_display_clock_speed =
6180 i865_get_display_clock_speed;
6181 else if (IS_I85X(dev))
6182 dev_priv->display.get_display_clock_speed =
6183 i855_get_display_clock_speed;
6185 dev_priv->display.get_display_clock_speed =
6186 i830_get_display_clock_speed;
6188 /* For FIFO watermark updates */
6189 if (HAS_PCH_SPLIT(dev)) {
6190 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
6191 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
6193 /* IVB configs may use multi-threaded forcewake */
6194 if (IS_IVYBRIDGE(dev)) {
6197 /* A small trick here - if the bios hasn't configured MT forcewake,
6198 * and if the device is in RC6, then force_wake_mt_get will not wake
6199 * the device and the ECOBUS read will return zero. Which will be
6200 * (correctly) interpreted by the test below as MT forcewake being
6204 __gen6_gt_force_wake_mt_get(dev_priv);
6205 ecobus = I915_READ_NOTRACE(ECOBUS);
6206 __gen6_gt_force_wake_mt_put(dev_priv);
6209 if (ecobus & FORCEWAKE_MT_ENABLE) {
6210 DRM_DEBUG_KMS("Using MT version of forcewake\n");
6211 dev_priv->display.force_wake_get =
6212 __gen6_gt_force_wake_mt_get;
6213 dev_priv->display.force_wake_put =
6214 __gen6_gt_force_wake_mt_put;
6218 if (HAS_PCH_IBX(dev))
6219 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
6220 else if (HAS_PCH_CPT(dev))
6221 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
6224 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6225 dev_priv->display.update_wm = ironlake_update_wm;
6227 DRM_DEBUG_KMS("Failed to get proper latency. "
6229 dev_priv->display.update_wm = NULL;
6231 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
6232 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
6233 dev_priv->display.write_eld = ironlake_write_eld;
6234 } else if (IS_GEN6(dev)) {
6235 if (SNB_READ_WM0_LATENCY()) {
6236 dev_priv->display.update_wm = sandybridge_update_wm;
6237 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
6239 DRM_DEBUG_KMS("Failed to read display plane latency. "
6241 dev_priv->display.update_wm = NULL;
6243 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
6244 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
6245 dev_priv->display.write_eld = ironlake_write_eld;
6246 } else if (IS_IVYBRIDGE(dev)) {
6247 /* FIXME: detect B0+ stepping and use auto training */
6248 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
6249 if (SNB_READ_WM0_LATENCY()) {
6250 dev_priv->display.update_wm = sandybridge_update_wm;
6251 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
6253 DRM_DEBUG_KMS("Failed to read display plane latency. "
6255 dev_priv->display.update_wm = NULL;
6257 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
6258 dev_priv->display.write_eld = ironlake_write_eld;
6260 dev_priv->display.update_wm = NULL;
6261 } else if (IS_PINEVIEW(dev)) {
6262 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6265 dev_priv->mem_freq)) {
6266 DRM_INFO("failed to find known CxSR latency "
6267 "(found ddr%s fsb freq %d, mem freq %d), "
6269 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6270 dev_priv->fsb_freq, dev_priv->mem_freq);
6271 /* Disable CxSR and never update its watermark again */
6272 pineview_disable_cxsr(dev);
6273 dev_priv->display.update_wm = NULL;
6275 dev_priv->display.update_wm = pineview_update_wm;
6276 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6277 } else if (IS_G4X(dev)) {
6278 dev_priv->display.write_eld = g4x_write_eld;
6279 dev_priv->display.update_wm = g4x_update_wm;
6280 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6281 } else if (IS_GEN4(dev)) {
6282 dev_priv->display.update_wm = i965_update_wm;
6283 if (IS_CRESTLINE(dev))
6284 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6285 else if (IS_BROADWATER(dev))
6286 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6287 } else if (IS_GEN3(dev)) {
6288 dev_priv->display.update_wm = i9xx_update_wm;
6289 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6290 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6291 } else if (IS_I865G(dev)) {
6292 dev_priv->display.update_wm = i830_update_wm;
6293 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6294 dev_priv->display.get_fifo_size = i830_get_fifo_size;
6295 } else if (IS_I85X(dev)) {
6296 dev_priv->display.update_wm = i9xx_update_wm;
6297 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
6298 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6300 dev_priv->display.update_wm = i830_update_wm;
6301 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6303 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6305 dev_priv->display.get_fifo_size = i830_get_fifo_size;
6308 /* Default just returns -ENODEV to indicate unsupported */
6309 dev_priv->display.queue_flip = intel_default_queue_flip;
6311 switch (INTEL_INFO(dev)->gen) {
6313 dev_priv->display.queue_flip = intel_gen2_queue_flip;
6317 dev_priv->display.queue_flip = intel_gen3_queue_flip;
6322 dev_priv->display.queue_flip = intel_gen4_queue_flip;
6326 dev_priv->display.queue_flip = intel_gen6_queue_flip;
6329 dev_priv->display.queue_flip = intel_gen7_queue_flip;
6335 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6336 * resume, or other times. This quirk makes sure that's the case for
6339 static void quirk_pipea_force(struct drm_device *dev)
6341 struct drm_i915_private *dev_priv = dev->dev_private;
6343 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6344 DRM_DEBUG("applying pipe a force quirk\n");
6348 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
6350 static void quirk_ssc_force_disable(struct drm_device *dev)
6352 struct drm_i915_private *dev_priv = dev->dev_private;
6353 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
6356 struct intel_quirk {
6358 int subsystem_vendor;
6359 int subsystem_device;
6360 void (*hook)(struct drm_device *dev);
6363 #define PCI_ANY_ID (~0u)
6365 struct intel_quirk intel_quirks[] = {
6366 /* HP Mini needs pipe A force quirk (LP: #322104) */
6367 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
6369 /* Thinkpad R31 needs pipe A force quirk */
6370 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6371 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6372 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6374 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6375 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6376 /* ThinkPad X40 needs pipe A force quirk */
6378 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6379 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6381 /* 855 & before need to leave pipe A & dpll A up */
6382 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6383 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6385 /* Lenovo U160 cannot use SSC on LVDS */
6386 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
6388 /* Sony Vaio Y cannot use SSC on LVDS */
6389 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
6392 static void intel_init_quirks(struct drm_device *dev)
6394 struct intel_quirk *q;
6399 for (i = 0; i < DRM_ARRAY_SIZE(intel_quirks); i++) {
6400 q = &intel_quirks[i];
6401 if (pci_get_device(d) == q->device &&
6402 (pci_get_subvendor(d) == q->subsystem_vendor ||
6403 q->subsystem_vendor == PCI_ANY_ID) &&
6404 (pci_get_subdevice(d) == q->subsystem_device ||
6405 q->subsystem_device == PCI_ANY_ID))
6410 /* Disable the VGA plane that we never use */
6411 static void i915_disable_vga(struct drm_device *dev)
6413 struct drm_i915_private *dev_priv = dev->dev_private;
6417 if (HAS_PCH_SPLIT(dev))
6418 vga_reg = CPU_VGACNTRL;
6423 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6425 outb(VGA_SR_INDEX, 1);
6426 sr1 = inb(VGA_SR_DATA);
6427 outb(VGA_SR_DATA, sr1 | 1 << 5);
6429 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6433 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6434 POSTING_READ(vga_reg);
6437 void intel_modeset_init(struct drm_device *dev)
6439 struct drm_i915_private *dev_priv = dev->dev_private;
6442 drm_mode_config_init(dev);
6444 dev->mode_config.min_width = 0;
6445 dev->mode_config.min_height = 0;
6447 dev->mode_config.preferred_depth = 24;
6448 dev->mode_config.prefer_shadow = 1;
6450 dev->mode_config.funcs = __DECONST(struct drm_mode_config_funcs *,
6453 intel_init_quirks(dev);
6455 intel_init_display(dev);
6458 dev->mode_config.max_width = 2048;
6459 dev->mode_config.max_height = 2048;
6460 } else if (IS_GEN3(dev)) {
6461 dev->mode_config.max_width = 4096;
6462 dev->mode_config.max_height = 4096;
6464 dev->mode_config.max_width = 8192;
6465 dev->mode_config.max_height = 8192;
6467 dev->mode_config.fb_base = dev->agp->base;
6469 DRM_DEBUG_KMS("%d display pipe%s available.\n",
6470 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
6472 for (i = 0; i < dev_priv->num_pipe; i++) {
6473 intel_crtc_init(dev, i);
6474 ret = intel_plane_init(dev, i);
6476 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
6479 /* Just disable it once at startup */
6480 i915_disable_vga(dev);
6481 intel_setup_outputs(dev);
6483 intel_init_clock_gating(dev);
6485 if (IS_IRONLAKE_M(dev)) {
6486 ironlake_enable_drps(dev);
6487 intel_init_emon(dev);
6491 gen6_enable_rps(dev_priv);
6492 gen6_update_ring_freq(dev_priv);
6495 TASK_INIT(&dev_priv->idle_task, 0, intel_idle_update, dev_priv);
6496 callout_init_mp(&dev_priv->idle_callout);
6499 void intel_modeset_gem_init(struct drm_device *dev)
6501 if (IS_IRONLAKE_M(dev))
6502 ironlake_enable_rc6(dev);
6504 intel_setup_overlay(dev);
6507 void intel_modeset_cleanup(struct drm_device *dev)
6509 struct drm_i915_private *dev_priv = dev->dev_private;
6510 struct drm_crtc *crtc;
6511 struct intel_crtc *intel_crtc;
6513 drm_kms_helper_poll_fini(dev);
6517 intel_unregister_dsm_handler();
6520 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6521 /* Skip inactive CRTCs */
6525 intel_crtc = to_intel_crtc(crtc);
6526 intel_increase_pllclock(crtc);
6529 intel_disable_fbc(dev);
6531 if (IS_IRONLAKE_M(dev))
6532 ironlake_disable_drps(dev);
6534 gen6_disable_rps(dev);
6536 if (IS_IRONLAKE_M(dev))
6537 ironlake_disable_rc6(dev);
6539 /* Disable the irq before mode object teardown, for the irq might
6540 * enqueue unpin/hotplug work. */
6541 drm_irq_uninstall(dev);
6544 if (taskqueue_cancel(dev_priv->tq, &dev_priv->hotplug_task, NULL))
6545 taskqueue_drain(dev_priv->tq, &dev_priv->hotplug_task);
6546 if (taskqueue_cancel(dev_priv->tq, &dev_priv->rps_task, NULL))
6547 taskqueue_drain(dev_priv->tq, &dev_priv->rps_task);
6549 /* Shut off idle work before the crtcs get freed. */
6550 if (taskqueue_cancel(dev_priv->tq, &dev_priv->idle_task, NULL))
6551 taskqueue_drain(dev_priv->tq, &dev_priv->idle_task);
6553 drm_mode_config_cleanup(dev);
6557 * Return which encoder is currently attached for connector.
6559 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
6561 return &intel_attached_encoder(connector)->base;
6564 void intel_connector_attach_encoder(struct intel_connector *connector,
6565 struct intel_encoder *encoder)
6567 connector->encoder = encoder;
6568 drm_mode_connector_attach_encoder(&connector->base,
6573 * set vga decode state - true == enable VGA decode
6575 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6577 struct drm_i915_private *dev_priv;
6578 device_t bridge_dev;
6581 dev_priv = dev->dev_private;
6582 bridge_dev = intel_gtt_get_bridge_device();
6583 gmch_ctrl = pci_read_config(bridge_dev, INTEL_GMCH_CTRL, 2);
6585 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6587 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6588 pci_write_config(bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl, 2);
6592 struct intel_display_error_state {
6593 struct intel_cursor_error_state {
6600 struct intel_pipe_error_state {
6612 struct intel_plane_error_state {
6623 struct intel_display_error_state *
6624 intel_display_capture_error_state(struct drm_device *dev)
6626 drm_i915_private_t *dev_priv = dev->dev_private;
6627 struct intel_display_error_state *error;
6630 error = kmalloc(sizeof(*error), DRM_MEM_KMS, M_NOWAIT);
6634 for (i = 0; i < 2; i++) {
6635 error->cursor[i].control = I915_READ(CURCNTR(i));
6636 error->cursor[i].position = I915_READ(CURPOS(i));
6637 error->cursor[i].base = I915_READ(CURBASE(i));
6639 error->plane[i].control = I915_READ(DSPCNTR(i));
6640 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6641 error->plane[i].size = I915_READ(DSPSIZE(i));
6642 error->plane[i].pos = I915_READ(DSPPOS(i));
6643 error->plane[i].addr = I915_READ(DSPADDR(i));
6644 if (INTEL_INFO(dev)->gen >= 4) {
6645 error->plane[i].surface = I915_READ(DSPSURF(i));
6646 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6649 error->pipe[i].conf = I915_READ(PIPECONF(i));
6650 error->pipe[i].source = I915_READ(PIPESRC(i));
6651 error->pipe[i].htotal = I915_READ(HTOTAL(i));
6652 error->pipe[i].hblank = I915_READ(HBLANK(i));
6653 error->pipe[i].hsync = I915_READ(HSYNC(i));
6654 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6655 error->pipe[i].vblank = I915_READ(VBLANK(i));
6656 error->pipe[i].vsync = I915_READ(VSYNC(i));
6663 intel_display_print_error_state(struct sbuf *m,
6664 struct drm_device *dev,
6665 struct intel_display_error_state *error)
6669 for (i = 0; i < 2; i++) {
6670 sbuf_printf(m, "Pipe [%d]:\n", i);
6671 sbuf_printf(m, " CONF: %08x\n", error->pipe[i].conf);
6672 sbuf_printf(m, " SRC: %08x\n", error->pipe[i].source);
6673 sbuf_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
6674 sbuf_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
6675 sbuf_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
6676 sbuf_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
6677 sbuf_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
6678 sbuf_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
6680 sbuf_printf(m, "Plane [%d]:\n", i);
6681 sbuf_printf(m, " CNTR: %08x\n", error->plane[i].control);
6682 sbuf_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
6683 sbuf_printf(m, " SIZE: %08x\n", error->plane[i].size);
6684 sbuf_printf(m, " POS: %08x\n", error->plane[i].pos);
6685 sbuf_printf(m, " ADDR: %08x\n", error->plane[i].addr);
6686 if (INTEL_INFO(dev)->gen >= 4) {
6687 sbuf_printf(m, " SURF: %08x\n", error->plane[i].surface);
6688 sbuf_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
6691 sbuf_printf(m, "Cursor [%d]:\n", i);
6692 sbuf_printf(m, " CNTR: %08x\n", error->cursor[i].control);
6693 sbuf_printf(m, " POS: %08x\n", error->cursor[i].position);
6694 sbuf_printf(m, " BASE: %08x\n", error->cursor[i].base);