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>
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include <drm/drm_edid.h>
33 #include "intel_drv.h"
34 #include <drm/i915_drm.h>
36 #include "i915_trace.h"
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
40 static void intel_increase_pllclock(struct drm_crtc *crtc);
41 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
52 #define INTEL_P2_NUM 2
53 typedef struct intel_limit intel_limit_t;
55 intel_range_t dot, vco, n, m, m1, m2, p, p1;
60 #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
63 intel_pch_rawclk(struct drm_device *dev)
65 struct drm_i915_private *dev_priv = dev->dev_private;
67 WARN_ON(!HAS_PCH_SPLIT(dev));
69 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
72 static inline u32 /* units of 100MHz */
73 intel_fdi_link_freq(struct drm_device *dev)
76 struct drm_i915_private *dev_priv = dev->dev_private;
77 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
82 static const intel_limit_t intel_limits_i8xx_dvo = {
83 .dot = { .min = 25000, .max = 350000 },
84 .vco = { .min = 930000, .max = 1400000 },
85 .n = { .min = 3, .max = 16 },
86 .m = { .min = 96, .max = 140 },
87 .m1 = { .min = 18, .max = 26 },
88 .m2 = { .min = 6, .max = 16 },
89 .p = { .min = 4, .max = 128 },
90 .p1 = { .min = 2, .max = 33 },
91 .p2 = { .dot_limit = 165000,
92 .p2_slow = 4, .p2_fast = 2 },
95 static const intel_limit_t intel_limits_i8xx_lvds = {
96 .dot = { .min = 25000, .max = 350000 },
97 .vco = { .min = 930000, .max = 1400000 },
98 .n = { .min = 3, .max = 16 },
99 .m = { .min = 96, .max = 140 },
100 .m1 = { .min = 18, .max = 26 },
101 .m2 = { .min = 6, .max = 16 },
102 .p = { .min = 4, .max = 128 },
103 .p1 = { .min = 1, .max = 6 },
104 .p2 = { .dot_limit = 165000,
105 .p2_slow = 14, .p2_fast = 7 },
108 static const intel_limit_t intel_limits_i9xx_sdvo = {
109 .dot = { .min = 20000, .max = 400000 },
110 .vco = { .min = 1400000, .max = 2800000 },
111 .n = { .min = 1, .max = 6 },
112 .m = { .min = 70, .max = 120 },
113 .m1 = { .min = 8, .max = 18 },
114 .m2 = { .min = 3, .max = 7 },
115 .p = { .min = 5, .max = 80 },
116 .p1 = { .min = 1, .max = 8 },
117 .p2 = { .dot_limit = 200000,
118 .p2_slow = 10, .p2_fast = 5 },
121 static const intel_limit_t intel_limits_i9xx_lvds = {
122 .dot = { .min = 20000, .max = 400000 },
123 .vco = { .min = 1400000, .max = 2800000 },
124 .n = { .min = 1, .max = 6 },
125 .m = { .min = 70, .max = 120 },
126 .m1 = { .min = 8, .max = 18 },
127 .m2 = { .min = 3, .max = 7 },
128 .p = { .min = 7, .max = 98 },
129 .p1 = { .min = 1, .max = 8 },
130 .p2 = { .dot_limit = 112000,
131 .p2_slow = 14, .p2_fast = 7 },
135 static const intel_limit_t intel_limits_g4x_sdvo = {
136 .dot = { .min = 25000, .max = 270000 },
137 .vco = { .min = 1750000, .max = 3500000},
138 .n = { .min = 1, .max = 4 },
139 .m = { .min = 104, .max = 138 },
140 .m1 = { .min = 17, .max = 23 },
141 .m2 = { .min = 5, .max = 11 },
142 .p = { .min = 10, .max = 30 },
143 .p1 = { .min = 1, .max = 3},
144 .p2 = { .dot_limit = 270000,
150 static const intel_limit_t intel_limits_g4x_hdmi = {
151 .dot = { .min = 22000, .max = 400000 },
152 .vco = { .min = 1750000, .max = 3500000},
153 .n = { .min = 1, .max = 4 },
154 .m = { .min = 104, .max = 138 },
155 .m1 = { .min = 16, .max = 23 },
156 .m2 = { .min = 5, .max = 11 },
157 .p = { .min = 5, .max = 80 },
158 .p1 = { .min = 1, .max = 8},
159 .p2 = { .dot_limit = 165000,
160 .p2_slow = 10, .p2_fast = 5 },
163 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
164 .dot = { .min = 20000, .max = 115000 },
165 .vco = { .min = 1750000, .max = 3500000 },
166 .n = { .min = 1, .max = 3 },
167 .m = { .min = 104, .max = 138 },
168 .m1 = { .min = 17, .max = 23 },
169 .m2 = { .min = 5, .max = 11 },
170 .p = { .min = 28, .max = 112 },
171 .p1 = { .min = 2, .max = 8 },
172 .p2 = { .dot_limit = 0,
173 .p2_slow = 14, .p2_fast = 14
177 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
178 .dot = { .min = 80000, .max = 224000 },
179 .vco = { .min = 1750000, .max = 3500000 },
180 .n = { .min = 1, .max = 3 },
181 .m = { .min = 104, .max = 138 },
182 .m1 = { .min = 17, .max = 23 },
183 .m2 = { .min = 5, .max = 11 },
184 .p = { .min = 14, .max = 42 },
185 .p1 = { .min = 2, .max = 6 },
186 .p2 = { .dot_limit = 0,
187 .p2_slow = 7, .p2_fast = 7
191 static const intel_limit_t intel_limits_pineview_sdvo = {
192 .dot = { .min = 20000, .max = 400000},
193 .vco = { .min = 1700000, .max = 3500000 },
194 /* Pineview's Ncounter is a ring counter */
195 .n = { .min = 3, .max = 6 },
196 .m = { .min = 2, .max = 256 },
197 /* Pineview only has one combined m divider, which we treat as m2. */
198 .m1 = { .min = 0, .max = 0 },
199 .m2 = { .min = 0, .max = 254 },
200 .p = { .min = 5, .max = 80 },
201 .p1 = { .min = 1, .max = 8 },
202 .p2 = { .dot_limit = 200000,
203 .p2_slow = 10, .p2_fast = 5 },
206 static const intel_limit_t intel_limits_pineview_lvds = {
207 .dot = { .min = 20000, .max = 400000 },
208 .vco = { .min = 1700000, .max = 3500000 },
209 .n = { .min = 3, .max = 6 },
210 .m = { .min = 2, .max = 256 },
211 .m1 = { .min = 0, .max = 0 },
212 .m2 = { .min = 0, .max = 254 },
213 .p = { .min = 7, .max = 112 },
214 .p1 = { .min = 1, .max = 8 },
215 .p2 = { .dot_limit = 112000,
216 .p2_slow = 14, .p2_fast = 14 },
219 /* Ironlake / Sandybridge
221 * We calculate clock using (register_value + 2) for N/M1/M2, so here
222 * the range value for them is (actual_value - 2).
224 static const intel_limit_t intel_limits_ironlake_dac = {
225 .dot = { .min = 25000, .max = 350000 },
226 .vco = { .min = 1760000, .max = 3510000 },
227 .n = { .min = 1, .max = 5 },
228 .m = { .min = 79, .max = 127 },
229 .m1 = { .min = 12, .max = 22 },
230 .m2 = { .min = 5, .max = 9 },
231 .p = { .min = 5, .max = 80 },
232 .p1 = { .min = 1, .max = 8 },
233 .p2 = { .dot_limit = 225000,
234 .p2_slow = 10, .p2_fast = 5 },
237 static const intel_limit_t intel_limits_ironlake_single_lvds = {
238 .dot = { .min = 25000, .max = 350000 },
239 .vco = { .min = 1760000, .max = 3510000 },
240 .n = { .min = 1, .max = 3 },
241 .m = { .min = 79, .max = 118 },
242 .m1 = { .min = 12, .max = 22 },
243 .m2 = { .min = 5, .max = 9 },
244 .p = { .min = 28, .max = 112 },
245 .p1 = { .min = 2, .max = 8 },
246 .p2 = { .dot_limit = 225000,
247 .p2_slow = 14, .p2_fast = 14 },
250 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
251 .dot = { .min = 25000, .max = 350000 },
252 .vco = { .min = 1760000, .max = 3510000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 79, .max = 127 },
255 .m1 = { .min = 12, .max = 22 },
256 .m2 = { .min = 5, .max = 9 },
257 .p = { .min = 14, .max = 56 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 225000,
260 .p2_slow = 7, .p2_fast = 7 },
263 /* LVDS 100mhz refclk limits. */
264 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
265 .dot = { .min = 25000, .max = 350000 },
266 .vco = { .min = 1760000, .max = 3510000 },
267 .n = { .min = 1, .max = 2 },
268 .m = { .min = 79, .max = 126 },
269 .m1 = { .min = 12, .max = 22 },
270 .m2 = { .min = 5, .max = 9 },
271 .p = { .min = 28, .max = 112 },
272 .p1 = { .min = 2, .max = 8 },
273 .p2 = { .dot_limit = 225000,
274 .p2_slow = 14, .p2_fast = 14 },
277 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
278 .dot = { .min = 25000, .max = 350000 },
279 .vco = { .min = 1760000, .max = 3510000 },
280 .n = { .min = 1, .max = 3 },
281 .m = { .min = 79, .max = 126 },
282 .m1 = { .min = 12, .max = 22 },
283 .m2 = { .min = 5, .max = 9 },
284 .p = { .min = 14, .max = 42 },
285 .p1 = { .min = 2, .max = 6 },
286 .p2 = { .dot_limit = 225000,
287 .p2_slow = 7, .p2_fast = 7 },
290 static const intel_limit_t intel_limits_vlv_dac = {
291 .dot = { .min = 25000, .max = 270000 },
292 .vco = { .min = 4000000, .max = 6000000 },
293 .n = { .min = 1, .max = 7 },
294 .m = { .min = 22, .max = 450 }, /* guess */
295 .m1 = { .min = 2, .max = 3 },
296 .m2 = { .min = 11, .max = 156 },
297 .p = { .min = 10, .max = 30 },
298 .p1 = { .min = 1, .max = 3 },
299 .p2 = { .dot_limit = 270000,
300 .p2_slow = 2, .p2_fast = 20 },
303 static const intel_limit_t intel_limits_vlv_hdmi = {
304 .dot = { .min = 25000, .max = 270000 },
305 .vco = { .min = 4000000, .max = 6000000 },
306 .n = { .min = 1, .max = 7 },
307 .m = { .min = 60, .max = 300 }, /* guess */
308 .m1 = { .min = 2, .max = 3 },
309 .m2 = { .min = 11, .max = 156 },
310 .p = { .min = 10, .max = 30 },
311 .p1 = { .min = 2, .max = 3 },
312 .p2 = { .dot_limit = 270000,
313 .p2_slow = 2, .p2_fast = 20 },
316 static const intel_limit_t intel_limits_vlv_dp = {
317 .dot = { .min = 25000, .max = 270000 },
318 .vco = { .min = 4000000, .max = 6000000 },
319 .n = { .min = 1, .max = 7 },
320 .m = { .min = 22, .max = 450 },
321 .m1 = { .min = 2, .max = 3 },
322 .m2 = { .min = 11, .max = 156 },
323 .p = { .min = 10, .max = 30 },
324 .p1 = { .min = 1, .max = 3 },
325 .p2 = { .dot_limit = 270000,
326 .p2_slow = 2, .p2_fast = 20 },
329 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
332 struct drm_device *dev = crtc->dev;
333 const intel_limit_t *limit;
335 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
336 if (intel_is_dual_link_lvds(dev)) {
337 if (refclk == 100000)
338 limit = &intel_limits_ironlake_dual_lvds_100m;
340 limit = &intel_limits_ironlake_dual_lvds;
342 if (refclk == 100000)
343 limit = &intel_limits_ironlake_single_lvds_100m;
345 limit = &intel_limits_ironlake_single_lvds;
348 limit = &intel_limits_ironlake_dac;
353 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
355 struct drm_device *dev = crtc->dev;
356 const intel_limit_t *limit;
358 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
359 if (intel_is_dual_link_lvds(dev))
360 limit = &intel_limits_g4x_dual_channel_lvds;
362 limit = &intel_limits_g4x_single_channel_lvds;
363 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
364 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
365 limit = &intel_limits_g4x_hdmi;
366 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
367 limit = &intel_limits_g4x_sdvo;
368 } else /* The option is for other outputs */
369 limit = &intel_limits_i9xx_sdvo;
374 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
376 struct drm_device *dev = crtc->dev;
377 const intel_limit_t *limit;
379 if (HAS_PCH_SPLIT(dev))
380 limit = intel_ironlake_limit(crtc, refclk);
381 else if (IS_G4X(dev)) {
382 limit = intel_g4x_limit(crtc);
383 } else if (IS_PINEVIEW(dev)) {
384 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
385 limit = &intel_limits_pineview_lvds;
387 limit = &intel_limits_pineview_sdvo;
388 } else if (IS_VALLEYVIEW(dev)) {
389 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
390 limit = &intel_limits_vlv_dac;
391 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
392 limit = &intel_limits_vlv_hdmi;
394 limit = &intel_limits_vlv_dp;
395 } else if (!IS_GEN2(dev)) {
396 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
397 limit = &intel_limits_i9xx_lvds;
399 limit = &intel_limits_i9xx_sdvo;
401 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
402 limit = &intel_limits_i8xx_lvds;
404 limit = &intel_limits_i8xx_dvo;
409 /* m1 is reserved as 0 in Pineview, n is a ring counter */
410 static void pineview_clock(int refclk, intel_clock_t *clock)
412 clock->m = clock->m2 + 2;
413 clock->p = clock->p1 * clock->p2;
414 clock->vco = refclk * clock->m / clock->n;
415 clock->dot = clock->vco / clock->p;
418 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
420 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
423 static void i9xx_clock(int refclk, intel_clock_t *clock)
425 clock->m = i9xx_dpll_compute_m(clock);
426 clock->p = clock->p1 * clock->p2;
427 clock->vco = refclk * clock->m / (clock->n + 2);
428 clock->dot = clock->vco / clock->p;
432 * Returns whether any output on the specified pipe is of the specified type
434 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
436 struct drm_device *dev = crtc->dev;
437 struct intel_encoder *encoder;
439 for_each_encoder_on_crtc(dev, crtc, encoder)
440 if (encoder->type == type)
446 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
448 * Returns whether the given set of divisors are valid for a given refclk with
449 * the given connectors.
452 static bool intel_PLL_is_valid(struct drm_device *dev,
453 const intel_limit_t *limit,
454 const intel_clock_t *clock)
456 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
457 INTELPllInvalid("p1 out of range\n");
458 if (clock->p < limit->p.min || limit->p.max < clock->p)
459 INTELPllInvalid("p out of range\n");
460 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
461 INTELPllInvalid("m2 out of range\n");
462 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
463 INTELPllInvalid("m1 out of range\n");
464 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
465 INTELPllInvalid("m1 <= m2\n");
466 if (clock->m < limit->m.min || limit->m.max < clock->m)
467 INTELPllInvalid("m out of range\n");
468 if (clock->n < limit->n.min || limit->n.max < clock->n)
469 INTELPllInvalid("n out of range\n");
470 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
471 INTELPllInvalid("vco out of range\n");
472 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
473 * connector, etc., rather than just a single range.
475 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
476 INTELPllInvalid("dot out of range\n");
482 i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
483 int target, int refclk, intel_clock_t *match_clock,
484 intel_clock_t *best_clock)
486 struct drm_device *dev = crtc->dev;
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
492 * For LVDS just rely on its current settings for dual-channel.
493 * We haven't figured out how to reliably set up different
494 * single/dual channel state, if we even can.
496 if (intel_is_dual_link_lvds(dev))
497 clock.p2 = limit->p2.p2_fast;
499 clock.p2 = limit->p2.p2_slow;
501 if (target < limit->p2.dot_limit)
502 clock.p2 = limit->p2.p2_slow;
504 clock.p2 = limit->p2.p2_fast;
507 memset(best_clock, 0, sizeof(*best_clock));
509 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
511 for (clock.m2 = limit->m2.min;
512 clock.m2 <= limit->m2.max; clock.m2++) {
513 if (clock.m2 >= clock.m1)
515 for (clock.n = limit->n.min;
516 clock.n <= limit->n.max; clock.n++) {
517 for (clock.p1 = limit->p1.min;
518 clock.p1 <= limit->p1.max; clock.p1++) {
521 i9xx_clock(refclk, &clock);
522 if (!intel_PLL_is_valid(dev, limit,
526 clock.p != match_clock->p)
529 this_err = abs(clock.dot - target);
530 if (this_err < err) {
539 return (err != target);
543 pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
544 int target, int refclk, intel_clock_t *match_clock,
545 intel_clock_t *best_clock)
547 struct drm_device *dev = crtc->dev;
551 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
553 * For LVDS just rely on its current settings for dual-channel.
554 * We haven't figured out how to reliably set up different
555 * single/dual channel state, if we even can.
557 if (intel_is_dual_link_lvds(dev))
558 clock.p2 = limit->p2.p2_fast;
560 clock.p2 = limit->p2.p2_slow;
562 if (target < limit->p2.dot_limit)
563 clock.p2 = limit->p2.p2_slow;
565 clock.p2 = limit->p2.p2_fast;
568 memset(best_clock, 0, sizeof(*best_clock));
570 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
572 for (clock.m2 = limit->m2.min;
573 clock.m2 <= limit->m2.max; clock.m2++) {
574 for (clock.n = limit->n.min;
575 clock.n <= limit->n.max; clock.n++) {
576 for (clock.p1 = limit->p1.min;
577 clock.p1 <= limit->p1.max; clock.p1++) {
580 pineview_clock(refclk, &clock);
581 if (!intel_PLL_is_valid(dev, limit,
585 clock.p != match_clock->p)
588 this_err = abs(clock.dot - target);
589 if (this_err < err) {
598 return (err != target);
602 g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
603 int target, int refclk, intel_clock_t *match_clock,
604 intel_clock_t *best_clock)
606 struct drm_device *dev = crtc->dev;
610 /* approximately equals target * 0.00585 */
611 int err_most = (target >> 8) + (target >> 9);
614 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
615 if (intel_is_dual_link_lvds(dev))
616 clock.p2 = limit->p2.p2_fast;
618 clock.p2 = limit->p2.p2_slow;
620 if (target < limit->p2.dot_limit)
621 clock.p2 = limit->p2.p2_slow;
623 clock.p2 = limit->p2.p2_fast;
626 memset(best_clock, 0, sizeof(*best_clock));
627 max_n = limit->n.max;
628 /* based on hardware requirement, prefer smaller n to precision */
629 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
630 /* based on hardware requirement, prefere larger m1,m2 */
631 for (clock.m1 = limit->m1.max;
632 clock.m1 >= limit->m1.min; clock.m1--) {
633 for (clock.m2 = limit->m2.max;
634 clock.m2 >= limit->m2.min; clock.m2--) {
635 for (clock.p1 = limit->p1.max;
636 clock.p1 >= limit->p1.min; clock.p1--) {
639 i9xx_clock(refclk, &clock);
640 if (!intel_PLL_is_valid(dev, limit,
644 this_err = abs(clock.dot - target);
645 if (this_err < err_most) {
659 vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
660 int target, int refclk, intel_clock_t *match_clock,
661 intel_clock_t *best_clock)
663 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
665 u32 updrate, minupdate, fracbits, p;
666 unsigned long bestppm, ppm, absppm;
670 dotclk = target * 1000;
673 fastclk = dotclk / (2*100);
677 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
678 bestm1 = bestm2 = bestp1 = bestp2 = 0;
680 /* based on hardware requirement, prefer smaller n to precision */
681 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
682 updrate = refclk / n;
683 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
684 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
688 /* based on hardware requirement, prefer bigger m1,m2 values */
689 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
690 m2 = (((2*(fastclk * p * n / m1 )) +
691 refclk) / (2*refclk));
694 if (vco >= limit->vco.min && vco < limit->vco.max) {
695 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
696 absppm = (ppm > 0) ? ppm : (-ppm);
697 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
701 if (absppm < bestppm - 10) {
718 best_clock->n = bestn;
719 best_clock->m1 = bestm1;
720 best_clock->m2 = bestm2;
721 best_clock->p1 = bestp1;
722 best_clock->p2 = bestp2;
727 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
730 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
733 return intel_crtc->config.cpu_transcoder;
736 static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
738 struct drm_i915_private *dev_priv = dev->dev_private;
739 u32 frame, frame_reg = PIPEFRAME(pipe);
741 frame = I915_READ(frame_reg);
743 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
744 DRM_DEBUG_KMS("vblank wait timed out\n");
748 * intel_wait_for_vblank - wait for vblank on a given pipe
750 * @pipe: pipe to wait for
752 * Wait for vblank to occur on a given pipe. Needed for various bits of
755 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
757 struct drm_i915_private *dev_priv = dev->dev_private;
758 int pipestat_reg = PIPESTAT(pipe);
760 if (INTEL_INFO(dev)->gen >= 5) {
761 ironlake_wait_for_vblank(dev, pipe);
765 /* Clear existing vblank status. Note this will clear any other
766 * sticky status fields as well.
768 * This races with i915_driver_irq_handler() with the result
769 * that either function could miss a vblank event. Here it is not
770 * fatal, as we will either wait upon the next vblank interrupt or
771 * timeout. Generally speaking intel_wait_for_vblank() is only
772 * called during modeset at which time the GPU should be idle and
773 * should *not* be performing page flips and thus not waiting on
775 * Currently, the result of us stealing a vblank from the irq
776 * handler is that a single frame will be skipped during swapbuffers.
778 I915_WRITE(pipestat_reg,
779 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
781 /* Wait for vblank interrupt bit to set */
782 if (wait_for(I915_READ(pipestat_reg) &
783 PIPE_VBLANK_INTERRUPT_STATUS,
785 DRM_DEBUG_KMS("vblank wait timed out\n");
789 * intel_wait_for_pipe_off - wait for pipe to turn off
791 * @pipe: pipe to wait for
793 * After disabling a pipe, we can't wait for vblank in the usual way,
794 * spinning on the vblank interrupt status bit, since we won't actually
795 * see an interrupt when the pipe is disabled.
798 * wait for the pipe register state bit to turn off
801 * wait for the display line value to settle (it usually
802 * ends up stopping at the start of the next frame).
805 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
807 struct drm_i915_private *dev_priv = dev->dev_private;
808 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
811 if (INTEL_INFO(dev)->gen >= 4) {
812 int reg = PIPECONF(cpu_transcoder);
814 /* Wait for the Pipe State to go off */
815 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
817 WARN(1, "pipe_off wait timed out\n");
819 u32 last_line, line_mask;
820 int reg = PIPEDSL(pipe);
821 unsigned long timeout = jiffies + msecs_to_jiffies(100);
824 line_mask = DSL_LINEMASK_GEN2;
826 line_mask = DSL_LINEMASK_GEN3;
828 /* Wait for the display line to settle */
830 last_line = I915_READ(reg) & line_mask;
832 } while (((I915_READ(reg) & line_mask) != last_line) &&
833 time_after(timeout, jiffies));
834 if (time_after(jiffies, timeout))
835 WARN(1, "pipe_off wait timed out\n");
840 * ibx_digital_port_connected - is the specified port connected?
841 * @dev_priv: i915 private structure
842 * @port: the port to test
844 * Returns true if @port is connected, false otherwise.
846 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
847 struct intel_digital_port *port)
851 if (HAS_PCH_IBX(dev_priv->dev)) {
854 bit = SDE_PORTB_HOTPLUG;
857 bit = SDE_PORTC_HOTPLUG;
860 bit = SDE_PORTD_HOTPLUG;
868 bit = SDE_PORTB_HOTPLUG_CPT;
871 bit = SDE_PORTC_HOTPLUG_CPT;
874 bit = SDE_PORTD_HOTPLUG_CPT;
881 return I915_READ(SDEISR) & bit;
884 static const char *state_string(bool enabled)
886 return enabled ? "on" : "off";
889 /* Only for pre-ILK configs */
890 static void assert_pll(struct drm_i915_private *dev_priv,
891 enum i915_pipe pipe, bool state)
898 val = I915_READ(reg);
899 cur_state = !!(val & DPLL_VCO_ENABLE);
900 WARN(cur_state != state,
901 "PLL state assertion failure (expected %s, current %s)\n",
902 state_string(state), state_string(cur_state));
904 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
905 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
907 static struct intel_shared_dpll *
908 intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
910 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
912 if (crtc->config.shared_dpll < 0)
915 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
919 static void assert_shared_dpll(struct drm_i915_private *dev_priv,
920 struct intel_shared_dpll *pll,
924 struct intel_dpll_hw_state hw_state;
926 if (HAS_PCH_LPT(dev_priv->dev)) {
927 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
932 "asserting DPLL %s with no DPLL\n", state_string(state)))
935 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
936 WARN(cur_state != state,
937 "%s assertion failure (expected %s, current %s)\n",
938 pll->name, state_string(state), state_string(cur_state));
940 #define assert_shared_dpll_enabled(d, p) assert_shared_dpll(d, p, true)
941 #define assert_shared_dpll_disabled(d, p) assert_shared_dpll(d, p, false)
943 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
944 enum i915_pipe pipe, bool state)
949 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
952 if (HAS_DDI(dev_priv->dev)) {
953 /* DDI does not have a specific FDI_TX register */
954 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
955 val = I915_READ(reg);
956 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
958 reg = FDI_TX_CTL(pipe);
959 val = I915_READ(reg);
960 cur_state = !!(val & FDI_TX_ENABLE);
962 WARN(cur_state != state,
963 "FDI TX state assertion failure (expected %s, current %s)\n",
964 state_string(state), state_string(cur_state));
966 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
967 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
969 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
970 enum i915_pipe pipe, bool state)
976 reg = FDI_RX_CTL(pipe);
977 val = I915_READ(reg);
978 cur_state = !!(val & FDI_RX_ENABLE);
979 WARN(cur_state != state,
980 "FDI RX state assertion failure (expected %s, current %s)\n",
981 state_string(state), state_string(cur_state));
983 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
984 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
986 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
992 /* ILK FDI PLL is always enabled */
993 if (dev_priv->info->gen == 5)
996 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
997 if (HAS_DDI(dev_priv->dev))
1000 reg = FDI_TX_CTL(pipe);
1001 val = I915_READ(reg);
1002 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1005 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1006 enum i915_pipe pipe)
1011 reg = FDI_RX_CTL(pipe);
1012 val = I915_READ(reg);
1013 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1016 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1017 enum i915_pipe pipe)
1019 int pp_reg, lvds_reg;
1021 enum i915_pipe panel_pipe = PIPE_A;
1024 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1025 pp_reg = PCH_PP_CONTROL;
1026 lvds_reg = PCH_LVDS;
1028 pp_reg = PP_CONTROL;
1032 val = I915_READ(pp_reg);
1033 if (!(val & PANEL_POWER_ON) ||
1034 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1037 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1038 panel_pipe = PIPE_B;
1040 WARN(panel_pipe == pipe && locked,
1041 "panel assertion failure, pipe %c regs locked\n",
1045 void assert_pipe(struct drm_i915_private *dev_priv,
1046 enum i915_pipe pipe, bool state)
1051 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1054 /* if we need the pipe A quirk it must be always on */
1055 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1058 if (!intel_display_power_enabled(dev_priv->dev,
1059 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1062 reg = PIPECONF(cpu_transcoder);
1063 val = I915_READ(reg);
1064 cur_state = !!(val & PIPECONF_ENABLE);
1067 WARN(cur_state != state,
1068 "pipe %c assertion failure (expected %s, current %s)\n",
1069 pipe_name(pipe), state_string(state), state_string(cur_state));
1072 static void assert_plane(struct drm_i915_private *dev_priv,
1073 enum plane plane, bool state)
1079 reg = DSPCNTR(plane);
1080 val = I915_READ(reg);
1081 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1082 WARN(cur_state != state,
1083 "plane %c assertion failure (expected %s, current %s)\n",
1084 plane_name(plane), state_string(state), state_string(cur_state));
1087 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1088 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1090 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1091 enum i915_pipe pipe)
1093 struct drm_device *dev = dev_priv->dev;
1098 /* Primary planes are fixed to pipes on gen4+ */
1099 if (INTEL_INFO(dev)->gen >= 4) {
1100 reg = DSPCNTR(pipe);
1101 val = I915_READ(reg);
1102 WARN((val & DISPLAY_PLANE_ENABLE),
1103 "plane %c assertion failure, should be disabled but not\n",
1108 /* Need to check both planes against the pipe */
1109 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
1111 val = I915_READ(reg);
1112 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1113 DISPPLANE_SEL_PIPE_SHIFT;
1114 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1115 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1116 plane_name(i), pipe_name(pipe));
1120 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1121 enum i915_pipe pipe)
1123 struct drm_device *dev = dev_priv->dev;
1127 if (IS_VALLEYVIEW(dev)) {
1128 for (i = 0; i < dev_priv->num_plane; i++) {
1129 reg = SPCNTR(pipe, i);
1130 val = I915_READ(reg);
1131 WARN((val & SP_ENABLE),
1132 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1133 sprite_name(pipe, i), pipe_name(pipe));
1135 } else if (INTEL_INFO(dev)->gen >= 7) {
1137 val = I915_READ(reg);
1138 WARN((val & SPRITE_ENABLE),
1139 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1140 plane_name(pipe), pipe_name(pipe));
1141 } else if (INTEL_INFO(dev)->gen >= 5) {
1142 reg = DVSCNTR(pipe);
1143 val = I915_READ(reg);
1144 WARN((val & DVS_ENABLE),
1145 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1146 plane_name(pipe), pipe_name(pipe));
1150 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1155 if (HAS_PCH_LPT(dev_priv->dev)) {
1156 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1160 val = I915_READ(PCH_DREF_CONTROL);
1161 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1162 DREF_SUPERSPREAD_SOURCE_MASK));
1163 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1166 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1167 enum i915_pipe pipe)
1173 reg = PCH_TRANSCONF(pipe);
1174 val = I915_READ(reg);
1175 enabled = !!(val & TRANS_ENABLE);
1177 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1181 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1182 enum i915_pipe pipe, u32 port_sel, u32 val)
1184 if ((val & DP_PORT_EN) == 0)
1187 if (HAS_PCH_CPT(dev_priv->dev)) {
1188 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1189 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1190 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1193 if ((val & DP_PIPE_MASK) != (pipe << 30))
1199 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1200 enum i915_pipe pipe, u32 val)
1202 if ((val & SDVO_ENABLE) == 0)
1205 if (HAS_PCH_CPT(dev_priv->dev)) {
1206 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1209 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1215 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1216 enum i915_pipe pipe, u32 val)
1218 if ((val & LVDS_PORT_EN) == 0)
1221 if (HAS_PCH_CPT(dev_priv->dev)) {
1222 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1225 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1231 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1232 enum i915_pipe pipe, u32 val)
1234 if ((val & ADPA_DAC_ENABLE) == 0)
1236 if (HAS_PCH_CPT(dev_priv->dev)) {
1237 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1240 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1246 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1247 enum i915_pipe pipe, int reg, u32 port_sel)
1249 u32 val = I915_READ(reg);
1250 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1251 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1252 reg, pipe_name(pipe));
1254 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1255 && (val & DP_PIPEB_SELECT),
1256 "IBX PCH dp port still using transcoder B\n");
1259 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1260 enum i915_pipe pipe, int reg)
1262 u32 val = I915_READ(reg);
1263 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1264 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1265 reg, pipe_name(pipe));
1267 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1268 && (val & SDVO_PIPE_B_SELECT),
1269 "IBX PCH hdmi port still using transcoder B\n");
1272 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1273 enum i915_pipe pipe)
1278 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1279 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1280 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1283 val = I915_READ(reg);
1284 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1285 "PCH VGA enabled on transcoder %c, should be disabled\n",
1289 val = I915_READ(reg);
1290 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1291 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1294 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1295 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1296 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1300 * intel_enable_pll - enable a PLL
1301 * @dev_priv: i915 private structure
1302 * @pipe: pipe PLL to enable
1304 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1305 * make sure the PLL reg is writable first though, since the panel write
1306 * protect mechanism may be enabled.
1308 * Note! This is for pre-ILK only.
1310 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1312 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1317 assert_pipe_disabled(dev_priv, pipe);
1319 /* No really, not for ILK+ */
1320 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1322 /* PLL is protected by panel, make sure we can write it */
1323 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1324 assert_panel_unlocked(dev_priv, pipe);
1327 val = I915_READ(reg);
1328 val |= DPLL_VCO_ENABLE;
1330 /* We do this three times for luck */
1331 I915_WRITE(reg, val);
1333 udelay(150); /* wait for warmup */
1334 I915_WRITE(reg, val);
1336 udelay(150); /* wait for warmup */
1337 I915_WRITE(reg, val);
1339 udelay(150); /* wait for warmup */
1343 * intel_disable_pll - disable a PLL
1344 * @dev_priv: i915 private structure
1345 * @pipe: pipe PLL to disable
1347 * Disable the PLL for @pipe, making sure the pipe is off first.
1349 * Note! This is for pre-ILK only.
1351 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1356 /* Don't disable pipe A or pipe A PLLs if needed */
1357 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1360 /* Make sure the pipe isn't still relying on us */
1361 assert_pipe_disabled(dev_priv, pipe);
1364 val = I915_READ(reg);
1365 val &= ~DPLL_VCO_ENABLE;
1366 I915_WRITE(reg, val);
1370 void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1375 port_mask = DPLL_PORTB_READY_MASK;
1377 port_mask = DPLL_PORTC_READY_MASK;
1379 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1380 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1381 'B' + port, I915_READ(DPLL(0)));
1385 * ironlake_enable_shared_dpll - enable PCH PLL
1386 * @dev_priv: i915 private structure
1387 * @pipe: pipe PLL to enable
1389 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1390 * drives the transcoder clock.
1392 static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
1394 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1395 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1397 /* PCH PLLs only available on ILK, SNB and IVB */
1398 BUG_ON(dev_priv->info->gen < 5);
1399 if (WARN_ON(pll == NULL))
1402 if (WARN_ON(pll->refcount == 0))
1405 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1406 pll->name, pll->active, pll->on,
1407 crtc->base.base.id);
1409 if (pll->active++) {
1411 assert_shared_dpll_enabled(dev_priv, pll);
1416 DRM_DEBUG_KMS("enabling %s\n", pll->name);
1417 pll->enable(dev_priv, pll);
1421 static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1423 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1424 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1426 /* PCH only available on ILK+ */
1427 BUG_ON(dev_priv->info->gen < 5);
1428 if (WARN_ON(pll == NULL))
1431 if (WARN_ON(pll->refcount == 0))
1434 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1435 pll->name, pll->active, pll->on,
1436 crtc->base.base.id);
1438 if (WARN_ON(pll->active == 0)) {
1439 assert_shared_dpll_disabled(dev_priv, pll);
1443 assert_shared_dpll_enabled(dev_priv, pll);
1448 DRM_DEBUG_KMS("disabling %s\n", pll->name);
1449 pll->disable(dev_priv, pll);
1453 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1454 enum i915_pipe pipe)
1456 struct drm_device *dev = dev_priv->dev;
1457 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1459 uint32_t reg, val, pipeconf_val;
1461 /* PCH only available on ILK+ */
1462 BUG_ON(dev_priv->info->gen < 5);
1464 /* Make sure PCH DPLL is enabled */
1465 assert_shared_dpll_enabled(dev_priv,
1466 intel_crtc_to_shared_dpll(intel_crtc));
1468 /* FDI must be feeding us bits for PCH ports */
1469 assert_fdi_tx_enabled(dev_priv, pipe);
1470 assert_fdi_rx_enabled(dev_priv, pipe);
1472 if (HAS_PCH_CPT(dev)) {
1473 /* Workaround: Set the timing override bit before enabling the
1474 * pch transcoder. */
1475 reg = TRANS_CHICKEN2(pipe);
1476 val = I915_READ(reg);
1477 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1478 I915_WRITE(reg, val);
1481 reg = PCH_TRANSCONF(pipe);
1482 val = I915_READ(reg);
1483 pipeconf_val = I915_READ(PIPECONF(pipe));
1485 if (HAS_PCH_IBX(dev_priv->dev)) {
1487 * make the BPC in transcoder be consistent with
1488 * that in pipeconf reg.
1490 val &= ~PIPECONF_BPC_MASK;
1491 val |= pipeconf_val & PIPECONF_BPC_MASK;
1494 val &= ~TRANS_INTERLACE_MASK;
1495 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1496 if (HAS_PCH_IBX(dev_priv->dev) &&
1497 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1498 val |= TRANS_LEGACY_INTERLACED_ILK;
1500 val |= TRANS_INTERLACED;
1502 val |= TRANS_PROGRESSIVE;
1504 I915_WRITE(reg, val | TRANS_ENABLE);
1505 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1506 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1509 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1510 enum transcoder cpu_transcoder)
1512 u32 val, pipeconf_val;
1514 /* PCH only available on ILK+ */
1515 BUG_ON(dev_priv->info->gen < 5);
1517 /* FDI must be feeding us bits for PCH ports */
1518 assert_fdi_tx_enabled(dev_priv, (enum i915_pipe) cpu_transcoder);
1519 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1521 /* Workaround: set timing override bit. */
1522 val = I915_READ(_TRANSA_CHICKEN2);
1523 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1524 I915_WRITE(_TRANSA_CHICKEN2, val);
1527 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1529 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1530 PIPECONF_INTERLACED_ILK)
1531 val |= TRANS_INTERLACED;
1533 val |= TRANS_PROGRESSIVE;
1535 I915_WRITE(LPT_TRANSCONF, val);
1536 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1537 DRM_ERROR("Failed to enable PCH transcoder\n");
1540 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1541 enum i915_pipe pipe)
1543 struct drm_device *dev = dev_priv->dev;
1546 /* FDI relies on the transcoder */
1547 assert_fdi_tx_disabled(dev_priv, pipe);
1548 assert_fdi_rx_disabled(dev_priv, pipe);
1550 /* Ports must be off as well */
1551 assert_pch_ports_disabled(dev_priv, pipe);
1553 reg = PCH_TRANSCONF(pipe);
1554 val = I915_READ(reg);
1555 val &= ~TRANS_ENABLE;
1556 I915_WRITE(reg, val);
1557 /* wait for PCH transcoder off, transcoder state */
1558 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1559 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1561 if (!HAS_PCH_IBX(dev)) {
1562 /* Workaround: Clear the timing override chicken bit again. */
1563 reg = TRANS_CHICKEN2(pipe);
1564 val = I915_READ(reg);
1565 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1566 I915_WRITE(reg, val);
1570 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1574 val = I915_READ(LPT_TRANSCONF);
1575 val &= ~TRANS_ENABLE;
1576 I915_WRITE(LPT_TRANSCONF, val);
1577 /* wait for PCH transcoder off, transcoder state */
1578 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1579 DRM_ERROR("Failed to disable PCH transcoder\n");
1581 /* Workaround: clear timing override bit. */
1582 val = I915_READ(_TRANSA_CHICKEN2);
1583 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1584 I915_WRITE(_TRANSA_CHICKEN2, val);
1588 * intel_enable_pipe - enable a pipe, asserting requirements
1589 * @dev_priv: i915 private structure
1590 * @pipe: pipe to enable
1591 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1593 * Enable @pipe, making sure that various hardware specific requirements
1594 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1596 * @pipe should be %PIPE_A or %PIPE_B.
1598 * Will wait until the pipe is actually running (i.e. first vblank) before
1601 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum i915_pipe pipe,
1604 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1606 enum i915_pipe pch_transcoder;
1610 assert_planes_disabled(dev_priv, pipe);
1611 assert_sprites_disabled(dev_priv, pipe);
1613 if (HAS_PCH_LPT(dev_priv->dev))
1614 pch_transcoder = TRANSCODER_A;
1616 pch_transcoder = pipe;
1619 * A pipe without a PLL won't actually be able to drive bits from
1620 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1623 if (!HAS_PCH_SPLIT(dev_priv->dev))
1624 assert_pll_enabled(dev_priv, pipe);
1627 /* if driving the PCH, we need FDI enabled */
1628 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1629 assert_fdi_tx_pll_enabled(dev_priv,
1630 (enum i915_pipe) cpu_transcoder);
1632 /* FIXME: assert CPU port conditions for SNB+ */
1635 reg = PIPECONF(cpu_transcoder);
1636 val = I915_READ(reg);
1637 if (val & PIPECONF_ENABLE)
1640 I915_WRITE(reg, val | PIPECONF_ENABLE);
1641 intel_wait_for_vblank(dev_priv->dev, pipe);
1645 * intel_disable_pipe - disable a pipe, asserting requirements
1646 * @dev_priv: i915 private structure
1647 * @pipe: pipe to disable
1649 * Disable @pipe, making sure that various hardware specific requirements
1650 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1652 * @pipe should be %PIPE_A or %PIPE_B.
1654 * Will wait until the pipe has shut down before returning.
1656 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1657 enum i915_pipe pipe)
1659 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1665 * Make sure planes won't keep trying to pump pixels to us,
1666 * or we might hang the display.
1668 assert_planes_disabled(dev_priv, pipe);
1669 assert_sprites_disabled(dev_priv, pipe);
1671 /* Don't disable pipe A or pipe A PLLs if needed */
1672 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1675 reg = PIPECONF(cpu_transcoder);
1676 val = I915_READ(reg);
1677 if ((val & PIPECONF_ENABLE) == 0)
1680 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1681 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1685 * Plane regs are double buffered, going from enabled->disabled needs a
1686 * trigger in order to latch. The display address reg provides this.
1688 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1691 if (dev_priv->info->gen >= 4)
1692 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1694 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1698 * intel_enable_plane - enable a display plane on a given pipe
1699 * @dev_priv: i915 private structure
1700 * @plane: plane to enable
1701 * @pipe: pipe being fed
1703 * Enable @plane on @pipe, making sure that @pipe is running first.
1705 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1706 enum plane plane, enum i915_pipe pipe)
1711 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1712 assert_pipe_enabled(dev_priv, pipe);
1714 reg = DSPCNTR(plane);
1715 val = I915_READ(reg);
1716 if (val & DISPLAY_PLANE_ENABLE)
1719 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1720 intel_flush_display_plane(dev_priv, plane);
1721 intel_wait_for_vblank(dev_priv->dev, pipe);
1725 * intel_disable_plane - disable a display plane
1726 * @dev_priv: i915 private structure
1727 * @plane: plane to disable
1728 * @pipe: pipe consuming the data
1730 * Disable @plane; should be an independent operation.
1732 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1733 enum plane plane, enum i915_pipe pipe)
1738 reg = DSPCNTR(plane);
1739 val = I915_READ(reg);
1740 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1743 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1744 intel_flush_display_plane(dev_priv, plane);
1745 intel_wait_for_vblank(dev_priv->dev, pipe);
1748 static bool need_vtd_wa(struct drm_device *dev)
1750 #ifdef CONFIG_INTEL_IOMMU
1751 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1758 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1759 struct drm_i915_gem_object *obj,
1760 struct intel_ring_buffer *pipelined)
1762 struct drm_i915_private *dev_priv = dev->dev_private;
1766 switch (obj->tiling_mode) {
1767 case I915_TILING_NONE:
1768 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1769 alignment = 128 * 1024;
1770 else if (INTEL_INFO(dev)->gen >= 4)
1771 alignment = 4 * 1024;
1773 alignment = 64 * 1024;
1776 /* pin() will align the object as required by fence */
1780 /* Despite that we check this in framebuffer_init userspace can
1781 * screw us over and change the tiling after the fact. Only
1782 * pinned buffers can't change their tiling. */
1783 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
1789 /* Note that the w/a also requires 64 PTE of padding following the
1790 * bo. We currently fill all unused PTE with the shadow page and so
1791 * we should always have valid PTE following the scanout preventing
1794 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1795 alignment = 256 * 1024;
1797 dev_priv->mm.interruptible = false;
1798 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1800 goto err_interruptible;
1802 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1803 * fence, whereas 965+ only requires a fence if using
1804 * framebuffer compression. For simplicity, we always install
1805 * a fence as the cost is not that onerous.
1807 ret = i915_gem_object_get_fence(obj);
1811 i915_gem_object_pin_fence(obj);
1813 dev_priv->mm.interruptible = true;
1817 i915_gem_object_unpin(obj);
1819 dev_priv->mm.interruptible = true;
1823 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1825 i915_gem_object_unpin_fence(obj);
1826 i915_gem_object_unpin(obj);
1829 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1830 * is assumed to be a power-of-two. */
1831 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1832 unsigned int tiling_mode,
1836 if (tiling_mode != I915_TILING_NONE) {
1837 unsigned int tile_rows, tiles;
1842 tiles = *x / (512/cpp);
1845 return tile_rows * pitch * 8 + tiles * 4096;
1847 unsigned int offset;
1849 offset = *y * pitch + *x * cpp;
1851 *x = (offset & 4095) / cpp;
1852 return offset & -4096;
1856 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1859 struct drm_device *dev = crtc->dev;
1860 struct drm_i915_private *dev_priv = dev->dev_private;
1861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1862 struct intel_framebuffer *intel_fb;
1863 struct drm_i915_gem_object *obj;
1864 int plane = intel_crtc->plane;
1865 unsigned long linear_offset;
1874 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1878 intel_fb = to_intel_framebuffer(fb);
1879 obj = intel_fb->obj;
1881 reg = DSPCNTR(plane);
1882 dspcntr = I915_READ(reg);
1883 /* Mask out pixel format bits in case we change it */
1884 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1885 switch (fb->pixel_format) {
1887 dspcntr |= DISPPLANE_8BPP;
1889 case DRM_FORMAT_XRGB1555:
1890 case DRM_FORMAT_ARGB1555:
1891 dspcntr |= DISPPLANE_BGRX555;
1893 case DRM_FORMAT_RGB565:
1894 dspcntr |= DISPPLANE_BGRX565;
1896 case DRM_FORMAT_XRGB8888:
1897 case DRM_FORMAT_ARGB8888:
1898 dspcntr |= DISPPLANE_BGRX888;
1900 case DRM_FORMAT_XBGR8888:
1901 case DRM_FORMAT_ABGR8888:
1902 dspcntr |= DISPPLANE_RGBX888;
1904 case DRM_FORMAT_XRGB2101010:
1905 case DRM_FORMAT_ARGB2101010:
1906 dspcntr |= DISPPLANE_BGRX101010;
1908 case DRM_FORMAT_XBGR2101010:
1909 case DRM_FORMAT_ABGR2101010:
1910 dspcntr |= DISPPLANE_RGBX101010;
1916 if (INTEL_INFO(dev)->gen >= 4) {
1917 if (obj->tiling_mode != I915_TILING_NONE)
1918 dspcntr |= DISPPLANE_TILED;
1920 dspcntr &= ~DISPPLANE_TILED;
1924 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1926 I915_WRITE(reg, dspcntr);
1928 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1930 if (INTEL_INFO(dev)->gen >= 4) {
1931 intel_crtc->dspaddr_offset =
1932 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1933 fb->bits_per_pixel / 8,
1935 linear_offset -= intel_crtc->dspaddr_offset;
1937 intel_crtc->dspaddr_offset = linear_offset;
1940 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
1941 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
1942 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1943 if (INTEL_INFO(dev)->gen >= 4) {
1944 I915_MODIFY_DISPBASE(DSPSURF(plane),
1945 obj->gtt_offset + intel_crtc->dspaddr_offset);
1946 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1947 I915_WRITE(DSPLINOFF(plane), linear_offset);
1949 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
1955 static int ironlake_update_plane(struct drm_crtc *crtc,
1956 struct drm_framebuffer *fb, int x, int y)
1958 struct drm_device *dev = crtc->dev;
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1961 struct intel_framebuffer *intel_fb;
1962 struct drm_i915_gem_object *obj;
1963 int plane = intel_crtc->plane;
1964 unsigned long linear_offset;
1974 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1978 intel_fb = to_intel_framebuffer(fb);
1979 obj = intel_fb->obj;
1981 reg = DSPCNTR(plane);
1982 dspcntr = I915_READ(reg);
1983 /* Mask out pixel format bits in case we change it */
1984 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1985 switch (fb->pixel_format) {
1987 dspcntr |= DISPPLANE_8BPP;
1989 case DRM_FORMAT_RGB565:
1990 dspcntr |= DISPPLANE_BGRX565;
1992 case DRM_FORMAT_XRGB8888:
1993 case DRM_FORMAT_ARGB8888:
1994 dspcntr |= DISPPLANE_BGRX888;
1996 case DRM_FORMAT_XBGR8888:
1997 case DRM_FORMAT_ABGR8888:
1998 dspcntr |= DISPPLANE_RGBX888;
2000 case DRM_FORMAT_XRGB2101010:
2001 case DRM_FORMAT_ARGB2101010:
2002 dspcntr |= DISPPLANE_BGRX101010;
2004 case DRM_FORMAT_XBGR2101010:
2005 case DRM_FORMAT_ABGR2101010:
2006 dspcntr |= DISPPLANE_RGBX101010;
2012 if (obj->tiling_mode != I915_TILING_NONE)
2013 dspcntr |= DISPPLANE_TILED;
2015 dspcntr &= ~DISPPLANE_TILED;
2018 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2020 I915_WRITE(reg, dspcntr);
2022 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2023 intel_crtc->dspaddr_offset =
2024 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2025 fb->bits_per_pixel / 8,
2027 linear_offset -= intel_crtc->dspaddr_offset;
2029 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2030 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2031 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2032 I915_MODIFY_DISPBASE(DSPSURF(plane),
2033 obj->gtt_offset + intel_crtc->dspaddr_offset);
2034 if (IS_HASWELL(dev)) {
2035 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2037 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2038 I915_WRITE(DSPLINOFF(plane), linear_offset);
2045 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2047 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2048 int x, int y, enum mode_set_atomic state)
2050 struct drm_device *dev = crtc->dev;
2051 struct drm_i915_private *dev_priv = dev->dev_private;
2053 if (dev_priv->display.disable_fbc)
2054 dev_priv->display.disable_fbc(dev);
2055 intel_increase_pllclock(crtc);
2057 return dev_priv->display.update_plane(crtc, fb, x, y);
2060 void intel_display_handle_reset(struct drm_device *dev)
2062 struct drm_i915_private *dev_priv = dev->dev_private;
2063 struct drm_crtc *crtc;
2066 * Flips in the rings have been nuked by the reset,
2067 * so complete all pending flips so that user space
2068 * will get its events and not get stuck.
2070 * Also update the base address of all primary
2071 * planes to the the last fb to make sure we're
2072 * showing the correct fb after a reset.
2074 * Need to make two loops over the crtcs so that we
2075 * don't try to grab a crtc mutex before the
2076 * pending_flip_queue really got woken up.
2079 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2081 enum plane plane = intel_crtc->plane;
2083 intel_prepare_page_flip(dev, plane);
2084 intel_finish_page_flip_plane(dev, plane);
2087 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2090 mutex_lock(&crtc->mutex);
2091 if (intel_crtc->active)
2092 dev_priv->display.update_plane(crtc, crtc->fb,
2094 mutex_unlock(&crtc->mutex);
2099 intel_finish_fb(struct drm_framebuffer *old_fb)
2101 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2102 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2103 bool was_interruptible = dev_priv->mm.interruptible;
2106 /* Big Hammer, we also need to ensure that any pending
2107 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2108 * current scanout is retired before unpinning the old
2111 * This should only fail upon a hung GPU, in which case we
2112 * can safely continue.
2114 dev_priv->mm.interruptible = false;
2115 ret = i915_gem_object_finish_gpu(obj);
2116 dev_priv->mm.interruptible = was_interruptible;
2121 static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2123 struct drm_device *dev = crtc->dev;
2125 struct drm_i915_master_private *master_priv;
2127 drm_i915_private_t *dev_priv = dev->dev_private;
2129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2132 if (!dev->primary->master)
2135 master_priv = dev->primary->master->driver_priv;
2136 if (!master_priv->sarea_priv)
2139 if (!dev_priv->sarea_priv)
2143 switch (intel_crtc->pipe) {
2146 master_priv->sarea_priv->pipeA_x = x;
2147 master_priv->sarea_priv->pipeA_y = y;
2149 dev_priv->sarea_priv->planeA_x = x;
2150 dev_priv->sarea_priv->planeA_y = y;
2155 master_priv->sarea_priv->pipeB_x = x;
2156 master_priv->sarea_priv->pipeB_y = y;
2158 dev_priv->sarea_priv->planeB_x = x;
2159 dev_priv->sarea_priv->planeB_y = y;
2168 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2169 struct drm_framebuffer *fb)
2171 struct drm_device *dev = crtc->dev;
2172 struct drm_i915_private *dev_priv = dev->dev_private;
2173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2174 struct drm_framebuffer *old_fb;
2179 DRM_ERROR("No FB bound\n");
2183 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2184 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2185 plane_name(intel_crtc->plane),
2186 INTEL_INFO(dev)->num_pipes);
2190 mutex_lock(&dev->struct_mutex);
2191 ret = intel_pin_and_fence_fb_obj(dev,
2192 to_intel_framebuffer(fb)->obj,
2195 mutex_unlock(&dev->struct_mutex);
2196 DRM_ERROR("pin & fence failed\n");
2200 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2202 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2203 mutex_unlock(&dev->struct_mutex);
2204 DRM_ERROR("failed to update base address\n");
2214 if (intel_crtc->active && old_fb != fb)
2215 intel_wait_for_vblank(dev, intel_crtc->pipe);
2216 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2219 intel_update_fbc(dev);
2220 mutex_unlock(&dev->struct_mutex);
2222 intel_crtc_update_sarea_pos(crtc, x, y);
2227 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2229 struct drm_device *dev = crtc->dev;
2230 struct drm_i915_private *dev_priv = dev->dev_private;
2231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2232 int pipe = intel_crtc->pipe;
2235 /* enable normal train */
2236 reg = FDI_TX_CTL(pipe);
2237 temp = I915_READ(reg);
2238 if (IS_IVYBRIDGE(dev)) {
2239 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2240 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2242 temp &= ~FDI_LINK_TRAIN_NONE;
2243 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2245 I915_WRITE(reg, temp);
2247 reg = FDI_RX_CTL(pipe);
2248 temp = I915_READ(reg);
2249 if (HAS_PCH_CPT(dev)) {
2250 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2251 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2253 temp &= ~FDI_LINK_TRAIN_NONE;
2254 temp |= FDI_LINK_TRAIN_NONE;
2256 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2258 /* wait one idle pattern time */
2262 /* IVB wants error correction enabled */
2263 if (IS_IVYBRIDGE(dev))
2264 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2265 FDI_FE_ERRC_ENABLE);
2268 static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2270 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2273 static void ivb_modeset_global_resources(struct drm_device *dev)
2275 struct drm_i915_private *dev_priv = dev->dev_private;
2276 struct intel_crtc *pipe_B_crtc =
2277 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2278 struct intel_crtc *pipe_C_crtc =
2279 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2283 * When everything is off disable fdi C so that we could enable fdi B
2284 * with all lanes. Note that we don't care about enabled pipes without
2285 * an enabled pch encoder.
2287 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2288 !pipe_has_enabled_pch(pipe_C_crtc)) {
2289 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2290 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2292 temp = I915_READ(SOUTH_CHICKEN1);
2293 temp &= ~FDI_BC_BIFURCATION_SELECT;
2294 DRM_DEBUG_KMS("disabling fdi C rx\n");
2295 I915_WRITE(SOUTH_CHICKEN1, temp);
2299 /* The FDI link training functions for ILK/Ibexpeak. */
2300 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2302 struct drm_device *dev = crtc->dev;
2303 struct drm_i915_private *dev_priv = dev->dev_private;
2304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2305 int pipe = intel_crtc->pipe;
2306 int plane = intel_crtc->plane;
2307 u32 reg, temp, tries;
2309 /* FDI needs bits from pipe & plane first */
2310 assert_pipe_enabled(dev_priv, pipe);
2311 assert_plane_enabled(dev_priv, plane);
2313 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2315 reg = FDI_RX_IMR(pipe);
2316 temp = I915_READ(reg);
2317 temp &= ~FDI_RX_SYMBOL_LOCK;
2318 temp &= ~FDI_RX_BIT_LOCK;
2319 I915_WRITE(reg, temp);
2323 /* enable CPU FDI TX and PCH FDI RX */
2324 reg = FDI_TX_CTL(pipe);
2325 temp = I915_READ(reg);
2326 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2327 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2328 temp &= ~FDI_LINK_TRAIN_NONE;
2329 temp |= FDI_LINK_TRAIN_PATTERN_1;
2330 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2332 reg = FDI_RX_CTL(pipe);
2333 temp = I915_READ(reg);
2334 temp &= ~FDI_LINK_TRAIN_NONE;
2335 temp |= FDI_LINK_TRAIN_PATTERN_1;
2336 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2341 /* Ironlake workaround, enable clock pointer after FDI enable*/
2342 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2343 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2344 FDI_RX_PHASE_SYNC_POINTER_EN);
2346 reg = FDI_RX_IIR(pipe);
2347 for (tries = 0; tries < 5; tries++) {
2348 temp = I915_READ(reg);
2349 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2351 if ((temp & FDI_RX_BIT_LOCK)) {
2352 DRM_DEBUG_KMS("FDI train 1 done.\n");
2353 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2358 DRM_ERROR("FDI train 1 fail!\n");
2361 reg = FDI_TX_CTL(pipe);
2362 temp = I915_READ(reg);
2363 temp &= ~FDI_LINK_TRAIN_NONE;
2364 temp |= FDI_LINK_TRAIN_PATTERN_2;
2365 I915_WRITE(reg, temp);
2367 reg = FDI_RX_CTL(pipe);
2368 temp = I915_READ(reg);
2369 temp &= ~FDI_LINK_TRAIN_NONE;
2370 temp |= FDI_LINK_TRAIN_PATTERN_2;
2371 I915_WRITE(reg, temp);
2376 reg = FDI_RX_IIR(pipe);
2377 for (tries = 0; tries < 5; tries++) {
2378 temp = I915_READ(reg);
2379 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2381 if (temp & FDI_RX_SYMBOL_LOCK) {
2382 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2383 DRM_DEBUG_KMS("FDI train 2 done.\n");
2388 DRM_ERROR("FDI train 2 fail!\n");
2390 DRM_DEBUG_KMS("FDI train done\n");
2394 static const int snb_b_fdi_train_param[] = {
2395 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2396 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2397 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2398 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2401 /* The FDI link training functions for SNB/Cougarpoint. */
2402 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2404 struct drm_device *dev = crtc->dev;
2405 struct drm_i915_private *dev_priv = dev->dev_private;
2406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2407 int pipe = intel_crtc->pipe;
2408 u32 reg, temp, i, retry;
2410 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2412 reg = FDI_RX_IMR(pipe);
2413 temp = I915_READ(reg);
2414 temp &= ~FDI_RX_SYMBOL_LOCK;
2415 temp &= ~FDI_RX_BIT_LOCK;
2416 I915_WRITE(reg, temp);
2421 /* enable CPU FDI TX and PCH FDI RX */
2422 reg = FDI_TX_CTL(pipe);
2423 temp = I915_READ(reg);
2424 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2425 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2426 temp &= ~FDI_LINK_TRAIN_NONE;
2427 temp |= FDI_LINK_TRAIN_PATTERN_1;
2428 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2430 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2431 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2433 I915_WRITE(FDI_RX_MISC(pipe),
2434 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2436 reg = FDI_RX_CTL(pipe);
2437 temp = I915_READ(reg);
2438 if (HAS_PCH_CPT(dev)) {
2439 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2440 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2442 temp &= ~FDI_LINK_TRAIN_NONE;
2443 temp |= FDI_LINK_TRAIN_PATTERN_1;
2445 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2450 for (i = 0; i < 4; i++) {
2451 reg = FDI_TX_CTL(pipe);
2452 temp = I915_READ(reg);
2453 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2454 temp |= snb_b_fdi_train_param[i];
2455 I915_WRITE(reg, temp);
2460 for (retry = 0; retry < 5; retry++) {
2461 reg = FDI_RX_IIR(pipe);
2462 temp = I915_READ(reg);
2463 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2464 if (temp & FDI_RX_BIT_LOCK) {
2465 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2466 DRM_DEBUG_KMS("FDI train 1 done.\n");
2475 DRM_ERROR("FDI train 1 fail!\n");
2478 reg = FDI_TX_CTL(pipe);
2479 temp = I915_READ(reg);
2480 temp &= ~FDI_LINK_TRAIN_NONE;
2481 temp |= FDI_LINK_TRAIN_PATTERN_2;
2483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2485 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2487 I915_WRITE(reg, temp);
2489 reg = FDI_RX_CTL(pipe);
2490 temp = I915_READ(reg);
2491 if (HAS_PCH_CPT(dev)) {
2492 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2493 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2495 temp &= ~FDI_LINK_TRAIN_NONE;
2496 temp |= FDI_LINK_TRAIN_PATTERN_2;
2498 I915_WRITE(reg, temp);
2503 for (i = 0; i < 4; i++) {
2504 reg = FDI_TX_CTL(pipe);
2505 temp = I915_READ(reg);
2506 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2507 temp |= snb_b_fdi_train_param[i];
2508 I915_WRITE(reg, temp);
2513 for (retry = 0; retry < 5; retry++) {
2514 reg = FDI_RX_IIR(pipe);
2515 temp = I915_READ(reg);
2516 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2517 if (temp & FDI_RX_SYMBOL_LOCK) {
2518 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2519 DRM_DEBUG_KMS("FDI train 2 done.\n");
2528 DRM_ERROR("FDI train 2 fail!\n");
2530 DRM_DEBUG_KMS("FDI train done.\n");
2533 /* Manual link training for Ivy Bridge A0 parts */
2534 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2536 struct drm_device *dev = crtc->dev;
2537 struct drm_i915_private *dev_priv = dev->dev_private;
2538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2539 int pipe = intel_crtc->pipe;
2542 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2544 reg = FDI_RX_IMR(pipe);
2545 temp = I915_READ(reg);
2546 temp &= ~FDI_RX_SYMBOL_LOCK;
2547 temp &= ~FDI_RX_BIT_LOCK;
2548 I915_WRITE(reg, temp);
2553 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2554 I915_READ(FDI_RX_IIR(pipe)));
2556 /* enable CPU FDI TX and PCH FDI RX */
2557 reg = FDI_TX_CTL(pipe);
2558 temp = I915_READ(reg);
2559 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2560 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2561 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2562 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2563 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2564 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2565 temp |= FDI_COMPOSITE_SYNC;
2566 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2568 I915_WRITE(FDI_RX_MISC(pipe),
2569 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2571 reg = FDI_RX_CTL(pipe);
2572 temp = I915_READ(reg);
2573 temp &= ~FDI_LINK_TRAIN_AUTO;
2574 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2575 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2576 temp |= FDI_COMPOSITE_SYNC;
2577 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2582 for (i = 0; i < 4; i++) {
2583 reg = FDI_TX_CTL(pipe);
2584 temp = I915_READ(reg);
2585 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2586 temp |= snb_b_fdi_train_param[i];
2587 I915_WRITE(reg, temp);
2592 reg = FDI_RX_IIR(pipe);
2593 temp = I915_READ(reg);
2594 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2596 if (temp & FDI_RX_BIT_LOCK ||
2597 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2598 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2599 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
2604 DRM_ERROR("FDI train 1 fail!\n");
2607 reg = FDI_TX_CTL(pipe);
2608 temp = I915_READ(reg);
2609 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2610 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2611 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2612 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2613 I915_WRITE(reg, temp);
2615 reg = FDI_RX_CTL(pipe);
2616 temp = I915_READ(reg);
2617 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2618 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2619 I915_WRITE(reg, temp);
2624 for (i = 0; i < 4; i++) {
2625 reg = FDI_TX_CTL(pipe);
2626 temp = I915_READ(reg);
2627 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2628 temp |= snb_b_fdi_train_param[i];
2629 I915_WRITE(reg, temp);
2634 reg = FDI_RX_IIR(pipe);
2635 temp = I915_READ(reg);
2636 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2638 if (temp & FDI_RX_SYMBOL_LOCK) {
2639 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2640 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
2645 DRM_ERROR("FDI train 2 fail!\n");
2647 DRM_DEBUG_KMS("FDI train done.\n");
2650 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2652 struct drm_device *dev = intel_crtc->base.dev;
2653 struct drm_i915_private *dev_priv = dev->dev_private;
2654 int pipe = intel_crtc->pipe;
2658 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2659 reg = FDI_RX_CTL(pipe);
2660 temp = I915_READ(reg);
2661 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2662 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2663 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2664 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2669 /* Switch from Rawclk to PCDclk */
2670 temp = I915_READ(reg);
2671 I915_WRITE(reg, temp | FDI_PCDCLK);
2676 /* Enable CPU FDI TX PLL, always on for Ironlake */
2677 reg = FDI_TX_CTL(pipe);
2678 temp = I915_READ(reg);
2679 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2680 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2687 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2689 struct drm_device *dev = intel_crtc->base.dev;
2690 struct drm_i915_private *dev_priv = dev->dev_private;
2691 int pipe = intel_crtc->pipe;
2694 /* Switch from PCDclk to Rawclk */
2695 reg = FDI_RX_CTL(pipe);
2696 temp = I915_READ(reg);
2697 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2699 /* Disable CPU FDI TX PLL */
2700 reg = FDI_TX_CTL(pipe);
2701 temp = I915_READ(reg);
2702 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2707 reg = FDI_RX_CTL(pipe);
2708 temp = I915_READ(reg);
2709 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2711 /* Wait for the clocks to turn off. */
2716 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2718 struct drm_device *dev = crtc->dev;
2719 struct drm_i915_private *dev_priv = dev->dev_private;
2720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2721 int pipe = intel_crtc->pipe;
2724 /* disable CPU FDI tx and PCH FDI rx */
2725 reg = FDI_TX_CTL(pipe);
2726 temp = I915_READ(reg);
2727 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2730 reg = FDI_RX_CTL(pipe);
2731 temp = I915_READ(reg);
2732 temp &= ~(0x7 << 16);
2733 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2734 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2739 /* Ironlake workaround, disable clock pointer after downing FDI */
2740 if (HAS_PCH_IBX(dev)) {
2741 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2744 /* still set train pattern 1 */
2745 reg = FDI_TX_CTL(pipe);
2746 temp = I915_READ(reg);
2747 temp &= ~FDI_LINK_TRAIN_NONE;
2748 temp |= FDI_LINK_TRAIN_PATTERN_1;
2749 I915_WRITE(reg, temp);
2751 reg = FDI_RX_CTL(pipe);
2752 temp = I915_READ(reg);
2753 if (HAS_PCH_CPT(dev)) {
2754 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2755 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2757 temp &= ~FDI_LINK_TRAIN_NONE;
2758 temp |= FDI_LINK_TRAIN_PATTERN_1;
2760 /* BPC in FDI rx is consistent with that in PIPECONF */
2761 temp &= ~(0x07 << 16);
2762 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2763 I915_WRITE(reg, temp);
2769 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2771 struct drm_device *dev = crtc->dev;
2772 struct drm_i915_private *dev_priv = dev->dev_private;
2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2776 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2777 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2780 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2781 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2782 lockmgr(&dev->event_lock, LK_RELEASE);
2787 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2789 struct drm_device *dev = crtc->dev;
2790 struct drm_i915_private *dev_priv = dev->dev_private;
2792 if (crtc->fb == NULL)
2795 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2797 wait_event(dev_priv->pending_flip_queue,
2798 !intel_crtc_has_pending_flip(crtc));
2800 mutex_lock(&dev->struct_mutex);
2801 intel_finish_fb(crtc->fb);
2802 mutex_unlock(&dev->struct_mutex);
2805 /* Program iCLKIP clock to the desired frequency */
2806 static void lpt_program_iclkip(struct drm_crtc *crtc)
2808 struct drm_device *dev = crtc->dev;
2809 struct drm_i915_private *dev_priv = dev->dev_private;
2810 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2813 mutex_lock(&dev_priv->dpio_lock);
2815 /* It is necessary to ungate the pixclk gate prior to programming
2816 * the divisors, and gate it back when it is done.
2818 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2820 /* Disable SSCCTL */
2821 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2822 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2826 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2827 if (crtc->mode.clock == 20000) {
2832 /* The iCLK virtual clock root frequency is in MHz,
2833 * but the crtc->mode.clock in in KHz. To get the divisors,
2834 * it is necessary to divide one by another, so we
2835 * convert the virtual clock precision to KHz here for higher
2838 u32 iclk_virtual_root_freq = 172800 * 1000;
2839 u32 iclk_pi_range = 64;
2840 u32 desired_divisor, msb_divisor_value, pi_value;
2842 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2843 msb_divisor_value = desired_divisor / iclk_pi_range;
2844 pi_value = desired_divisor % iclk_pi_range;
2847 divsel = msb_divisor_value - 2;
2848 phaseinc = pi_value;
2851 /* This should not happen with any sane values */
2852 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2853 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2854 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2855 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2857 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2864 /* Program SSCDIVINTPHASE6 */
2865 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2866 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2867 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2868 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2869 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2870 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2871 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2872 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2874 /* Program SSCAUXDIV */
2875 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2876 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2877 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2878 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2880 /* Enable modulator and associated divider */
2881 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2882 temp &= ~SBI_SSCCTL_DISABLE;
2883 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2885 /* Wait for initialization time */
2888 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2890 mutex_unlock(&dev_priv->dpio_lock);
2893 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2894 enum i915_pipe pch_transcoder)
2896 struct drm_device *dev = crtc->base.dev;
2897 struct drm_i915_private *dev_priv = dev->dev_private;
2898 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2900 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2901 I915_READ(HTOTAL(cpu_transcoder)));
2902 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2903 I915_READ(HBLANK(cpu_transcoder)));
2904 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2905 I915_READ(HSYNC(cpu_transcoder)));
2907 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2908 I915_READ(VTOTAL(cpu_transcoder)));
2909 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2910 I915_READ(VBLANK(cpu_transcoder)));
2911 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2912 I915_READ(VSYNC(cpu_transcoder)));
2913 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2914 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2918 * Enable PCH resources required for PCH ports:
2920 * - FDI training & RX/TX
2921 * - update transcoder timings
2922 * - DP transcoding bits
2925 static void ironlake_pch_enable(struct drm_crtc *crtc)
2927 struct drm_device *dev = crtc->dev;
2928 struct drm_i915_private *dev_priv = dev->dev_private;
2929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2930 int pipe = intel_crtc->pipe;
2933 assert_pch_transcoder_disabled(dev_priv, pipe);
2935 /* Write the TU size bits before fdi link training, so that error
2936 * detection works. */
2937 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2938 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2940 /* For PCH output, training FDI link */
2941 dev_priv->display.fdi_link_train(crtc);
2943 /* XXX: pch pll's can be enabled any time before we enable the PCH
2944 * transcoder, and we actually should do this to not upset any PCH
2945 * transcoder that already use the clock when we share it.
2947 * Note that enable_shared_dpll tries to do the right thing, but
2948 * get_shared_dpll unconditionally resets the pll - we need that to have
2949 * the right LVDS enable sequence. */
2950 ironlake_enable_shared_dpll(intel_crtc);
2952 if (HAS_PCH_CPT(dev)) {
2955 temp = I915_READ(PCH_DPLL_SEL);
2956 temp |= TRANS_DPLL_ENABLE(pipe);
2957 sel = TRANS_DPLLB_SEL(pipe);
2958 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
2962 I915_WRITE(PCH_DPLL_SEL, temp);
2965 /* set transcoder timing, panel must allow it */
2966 assert_panel_unlocked(dev_priv, pipe);
2967 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
2969 intel_fdi_normal_train(crtc);
2971 /* For PCH DP, enable TRANS_DP_CTL */
2972 if (HAS_PCH_CPT(dev) &&
2973 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2974 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2975 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
2976 reg = TRANS_DP_CTL(pipe);
2977 temp = I915_READ(reg);
2978 temp &= ~(TRANS_DP_PORT_SEL_MASK |
2979 TRANS_DP_SYNC_MASK |
2981 temp |= (TRANS_DP_OUTPUT_ENABLE |
2982 TRANS_DP_ENH_FRAMING);
2983 temp |= bpc << 9; /* same format but at 11:9 */
2985 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
2986 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
2987 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
2988 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
2990 switch (intel_trans_dp_port_sel(crtc)) {
2992 temp |= TRANS_DP_PORT_SEL_B;
2995 temp |= TRANS_DP_PORT_SEL_C;
2998 temp |= TRANS_DP_PORT_SEL_D;
3004 I915_WRITE(reg, temp);
3007 ironlake_enable_pch_transcoder(dev_priv, pipe);
3010 static void lpt_pch_enable(struct drm_crtc *crtc)
3012 struct drm_device *dev = crtc->dev;
3013 struct drm_i915_private *dev_priv = dev->dev_private;
3014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3015 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3017 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3019 lpt_program_iclkip(crtc);
3021 /* Set transcoder timing. */
3022 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3024 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3027 static void intel_put_shared_dpll(struct intel_crtc *crtc)
3029 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3034 if (pll->refcount == 0) {
3035 WARN(1, "bad %s refcount\n", pll->name);
3039 if (--pll->refcount == 0) {
3041 WARN_ON(pll->active);
3044 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
3047 static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc, u32 dpll, u32 fp)
3049 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3050 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3051 enum intel_dpll_id i;
3054 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3055 crtc->base.base.id, pll->name);
3056 intel_put_shared_dpll(crtc);
3059 if (HAS_PCH_IBX(dev_priv->dev)) {
3060 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3062 pll = &dev_priv->shared_dplls[i];
3064 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3065 crtc->base.base.id, pll->name);
3070 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3071 pll = &dev_priv->shared_dplls[i];
3073 /* Only want to check enabled timings first */
3074 if (pll->refcount == 0)
3077 if (dpll == (I915_READ(PCH_DPLL(pll->id)) & 0x7fffffff) &&
3078 fp == I915_READ(PCH_FP0(pll->id))) {
3079 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
3081 pll->name, pll->refcount, pll->active);
3087 /* Ok no matching timings, maybe there's a free one? */
3088 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3089 pll = &dev_priv->shared_dplls[i];
3090 if (pll->refcount == 0) {
3091 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3092 crtc->base.base.id, pll->name);
3100 crtc->config.shared_dpll = i;
3101 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3102 pipe_name(crtc->pipe));
3104 if (pll->active == 0) {
3105 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3106 sizeof(pll->hw_state));
3108 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
3110 assert_shared_dpll_disabled(dev_priv, pll);
3112 /* Wait for the clocks to stabilize before rewriting the regs */
3113 I915_WRITE(PCH_DPLL(pll->id), dpll & ~DPLL_VCO_ENABLE);
3114 POSTING_READ(PCH_DPLL(pll->id));
3117 I915_WRITE(PCH_FP0(pll->id), fp);
3118 I915_WRITE(PCH_DPLL(pll->id), dpll & ~DPLL_VCO_ENABLE);
3125 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3127 struct drm_i915_private *dev_priv = dev->dev_private;
3128 int dslreg = PIPEDSL(pipe);
3131 temp = I915_READ(dslreg);
3133 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3134 if (wait_for(I915_READ(dslreg) != temp, 5))
3135 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3139 static void ironlake_pfit_enable(struct intel_crtc *crtc)
3141 struct drm_device *dev = crtc->base.dev;
3142 struct drm_i915_private *dev_priv = dev->dev_private;
3143 int pipe = crtc->pipe;
3145 if (crtc->config.pch_pfit.size) {
3146 /* Force use of hard-coded filter coefficients
3147 * as some pre-programmed values are broken,
3150 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3151 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3152 PF_PIPE_SEL_IVB(pipe));
3154 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3155 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3156 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3160 static void intel_enable_planes(struct drm_crtc *crtc)
3162 struct drm_device *dev = crtc->dev;
3163 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3164 struct intel_plane *intel_plane;
3166 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3167 if (intel_plane->pipe == pipe)
3168 intel_plane_restore(&intel_plane->base);
3171 static void intel_disable_planes(struct drm_crtc *crtc)
3173 struct drm_device *dev = crtc->dev;
3174 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3175 struct intel_plane *intel_plane;
3177 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3178 if (intel_plane->pipe == pipe)
3179 intel_plane_disable(&intel_plane->base);
3182 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3184 struct drm_device *dev = crtc->dev;
3185 struct drm_i915_private *dev_priv = dev->dev_private;
3186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3187 struct intel_encoder *encoder;
3188 int pipe = intel_crtc->pipe;
3189 int plane = intel_crtc->plane;
3192 WARN_ON(!crtc->enabled);
3194 if (intel_crtc->active)
3197 intel_crtc->active = true;
3199 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3200 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3202 intel_update_watermarks(dev);
3204 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3205 temp = I915_READ(PCH_LVDS);
3206 if ((temp & LVDS_PORT_EN) == 0)
3207 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3211 if (intel_crtc->config.has_pch_encoder) {
3212 /* Note: FDI PLL enabling _must_ be done before we enable the
3213 * cpu pipes, hence this is separate from all the other fdi/pch
3215 ironlake_fdi_pll_enable(intel_crtc);
3217 assert_fdi_tx_disabled(dev_priv, pipe);
3218 assert_fdi_rx_disabled(dev_priv, pipe);
3221 for_each_encoder_on_crtc(dev, crtc, encoder)
3222 if (encoder->pre_enable)
3223 encoder->pre_enable(encoder);
3225 ironlake_pfit_enable(intel_crtc);
3228 * On ILK+ LUT must be loaded before the pipe is running but with
3231 intel_crtc_load_lut(crtc);
3233 intel_enable_pipe(dev_priv, pipe,
3234 intel_crtc->config.has_pch_encoder);
3235 intel_enable_plane(dev_priv, plane, pipe);
3236 intel_enable_planes(crtc);
3237 intel_crtc_update_cursor(crtc, true);
3239 if (intel_crtc->config.has_pch_encoder)
3240 ironlake_pch_enable(crtc);
3242 mutex_lock(&dev->struct_mutex);
3243 intel_update_fbc(dev);
3244 mutex_unlock(&dev->struct_mutex);
3246 for_each_encoder_on_crtc(dev, crtc, encoder)
3247 encoder->enable(encoder);
3249 if (HAS_PCH_CPT(dev))
3250 cpt_verify_modeset(dev, intel_crtc->pipe);
3253 * There seems to be a race in PCH platform hw (at least on some
3254 * outputs) where an enabled pipe still completes any pageflip right
3255 * away (as if the pipe is off) instead of waiting for vblank. As soon
3256 * as the first vblank happend, everything works as expected. Hence just
3257 * wait for one vblank before returning to avoid strange things
3260 intel_wait_for_vblank(dev, intel_crtc->pipe);
3263 /* IPS only exists on ULT machines and is tied to pipe A. */
3264 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3266 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
3269 static void hsw_enable_ips(struct intel_crtc *crtc)
3271 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3273 if (!crtc->config.ips_enabled)
3276 /* We can only enable IPS after we enable a plane and wait for a vblank.
3277 * We guarantee that the plane is enabled by calling intel_enable_ips
3278 * only after intel_enable_plane. And intel_enable_plane already waits
3279 * for a vblank, so all we need to do here is to enable the IPS bit. */
3280 assert_plane_enabled(dev_priv, crtc->plane);
3281 I915_WRITE(IPS_CTL, IPS_ENABLE);
3284 static void hsw_disable_ips(struct intel_crtc *crtc)
3286 struct drm_device *dev = crtc->base.dev;
3287 struct drm_i915_private *dev_priv = dev->dev_private;
3289 if (!crtc->config.ips_enabled)
3292 assert_plane_enabled(dev_priv, crtc->plane);
3293 I915_WRITE(IPS_CTL, 0);
3295 /* We need to wait for a vblank before we can disable the plane. */
3296 intel_wait_for_vblank(dev, crtc->pipe);
3299 static void haswell_crtc_enable(struct drm_crtc *crtc)
3301 struct drm_device *dev = crtc->dev;
3302 struct drm_i915_private *dev_priv = dev->dev_private;
3303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3304 struct intel_encoder *encoder;
3305 int pipe = intel_crtc->pipe;
3306 int plane = intel_crtc->plane;
3308 WARN_ON(!crtc->enabled);
3310 if (intel_crtc->active)
3313 intel_crtc->active = true;
3315 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3316 if (intel_crtc->config.has_pch_encoder)
3317 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3319 intel_update_watermarks(dev);
3321 if (intel_crtc->config.has_pch_encoder)
3322 dev_priv->display.fdi_link_train(crtc);
3324 for_each_encoder_on_crtc(dev, crtc, encoder)
3325 if (encoder->pre_enable)
3326 encoder->pre_enable(encoder);
3328 intel_ddi_enable_pipe_clock(intel_crtc);
3330 ironlake_pfit_enable(intel_crtc);
3333 * On ILK+ LUT must be loaded before the pipe is running but with
3336 intel_crtc_load_lut(crtc);
3338 intel_ddi_set_pipe_settings(crtc);
3339 intel_ddi_enable_transcoder_func(crtc);
3341 intel_enable_pipe(dev_priv, pipe,
3342 intel_crtc->config.has_pch_encoder);
3343 intel_enable_plane(dev_priv, plane, pipe);
3344 intel_enable_planes(crtc);
3345 intel_crtc_update_cursor(crtc, true);
3347 hsw_enable_ips(intel_crtc);
3349 if (intel_crtc->config.has_pch_encoder)
3350 lpt_pch_enable(crtc);
3352 mutex_lock(&dev->struct_mutex);
3353 intel_update_fbc(dev);
3354 mutex_unlock(&dev->struct_mutex);
3356 for_each_encoder_on_crtc(dev, crtc, encoder)
3357 encoder->enable(encoder);
3360 * There seems to be a race in PCH platform hw (at least on some
3361 * outputs) where an enabled pipe still completes any pageflip right
3362 * away (as if the pipe is off) instead of waiting for vblank. As soon
3363 * as the first vblank happend, everything works as expected. Hence just
3364 * wait for one vblank before returning to avoid strange things
3367 intel_wait_for_vblank(dev, intel_crtc->pipe);
3370 static void ironlake_pfit_disable(struct intel_crtc *crtc)
3372 struct drm_device *dev = crtc->base.dev;
3373 struct drm_i915_private *dev_priv = dev->dev_private;
3374 int pipe = crtc->pipe;
3376 /* To avoid upsetting the power well on haswell only disable the pfit if
3377 * it's in use. The hw state code will make sure we get this right. */
3378 if (crtc->config.pch_pfit.size) {
3379 I915_WRITE(PF_CTL(pipe), 0);
3380 I915_WRITE(PF_WIN_POS(pipe), 0);
3381 I915_WRITE(PF_WIN_SZ(pipe), 0);
3385 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3387 struct drm_device *dev = crtc->dev;
3388 struct drm_i915_private *dev_priv = dev->dev_private;
3389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3390 struct intel_encoder *encoder;
3391 int pipe = intel_crtc->pipe;
3392 int plane = intel_crtc->plane;
3396 if (!intel_crtc->active)
3399 for_each_encoder_on_crtc(dev, crtc, encoder)
3400 encoder->disable(encoder);
3402 intel_crtc_wait_for_pending_flips(crtc);
3403 drm_vblank_off(dev, pipe);
3405 if (dev_priv->cfb_plane == plane)
3406 intel_disable_fbc(dev);
3408 intel_crtc_update_cursor(crtc, false);
3409 intel_disable_planes(crtc);
3410 intel_disable_plane(dev_priv, plane, pipe);
3412 if (intel_crtc->config.has_pch_encoder)
3413 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3415 intel_disable_pipe(dev_priv, pipe);
3417 ironlake_pfit_disable(intel_crtc);
3419 for_each_encoder_on_crtc(dev, crtc, encoder)
3420 if (encoder->post_disable)
3421 encoder->post_disable(encoder);
3423 if (intel_crtc->config.has_pch_encoder) {
3424 ironlake_fdi_disable(crtc);
3426 ironlake_disable_pch_transcoder(dev_priv, pipe);
3427 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3429 if (HAS_PCH_CPT(dev)) {
3430 /* disable TRANS_DP_CTL */
3431 reg = TRANS_DP_CTL(pipe);
3432 temp = I915_READ(reg);
3433 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3434 TRANS_DP_PORT_SEL_MASK);
3435 temp |= TRANS_DP_PORT_SEL_NONE;
3436 I915_WRITE(reg, temp);
3438 /* disable DPLL_SEL */
3439 temp = I915_READ(PCH_DPLL_SEL);
3440 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
3441 I915_WRITE(PCH_DPLL_SEL, temp);
3444 /* disable PCH DPLL */
3445 intel_disable_shared_dpll(intel_crtc);
3447 ironlake_fdi_pll_disable(intel_crtc);
3450 intel_crtc->active = false;
3451 intel_update_watermarks(dev);
3453 mutex_lock(&dev->struct_mutex);
3454 intel_update_fbc(dev);
3455 mutex_unlock(&dev->struct_mutex);
3458 static void haswell_crtc_disable(struct drm_crtc *crtc)
3460 struct drm_device *dev = crtc->dev;
3461 struct drm_i915_private *dev_priv = dev->dev_private;
3462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3463 struct intel_encoder *encoder;
3464 int pipe = intel_crtc->pipe;
3465 int plane = intel_crtc->plane;
3466 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3468 if (!intel_crtc->active)
3471 for_each_encoder_on_crtc(dev, crtc, encoder)
3472 encoder->disable(encoder);
3474 intel_crtc_wait_for_pending_flips(crtc);
3475 drm_vblank_off(dev, pipe);
3477 /* FBC must be disabled before disabling the plane on HSW. */
3478 if (dev_priv->cfb_plane == plane)
3479 intel_disable_fbc(dev);
3481 hsw_disable_ips(intel_crtc);
3483 intel_crtc_update_cursor(crtc, false);
3484 intel_disable_planes(crtc);
3485 intel_disable_plane(dev_priv, plane, pipe);
3487 if (intel_crtc->config.has_pch_encoder)
3488 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3489 intel_disable_pipe(dev_priv, pipe);
3491 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3493 ironlake_pfit_disable(intel_crtc);
3495 intel_ddi_disable_pipe_clock(intel_crtc);
3497 for_each_encoder_on_crtc(dev, crtc, encoder)
3498 if (encoder->post_disable)
3499 encoder->post_disable(encoder);
3501 if (intel_crtc->config.has_pch_encoder) {
3502 lpt_disable_pch_transcoder(dev_priv);
3503 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3504 intel_ddi_fdi_disable(crtc);
3507 intel_crtc->active = false;
3508 intel_update_watermarks(dev);
3510 mutex_lock(&dev->struct_mutex);
3511 intel_update_fbc(dev);
3512 mutex_unlock(&dev->struct_mutex);
3515 static void ironlake_crtc_off(struct drm_crtc *crtc)
3517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3518 intel_put_shared_dpll(intel_crtc);
3521 static void haswell_crtc_off(struct drm_crtc *crtc)
3523 intel_ddi_put_crtc_pll(crtc);
3526 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3528 if (!enable && intel_crtc->overlay) {
3529 struct drm_device *dev = intel_crtc->base.dev;
3530 struct drm_i915_private *dev_priv = dev->dev_private;
3532 mutex_lock(&dev->struct_mutex);
3533 dev_priv->mm.interruptible = false;
3534 (void) intel_overlay_switch_off(intel_crtc->overlay);
3535 dev_priv->mm.interruptible = true;
3536 mutex_unlock(&dev->struct_mutex);
3539 /* Let userspace switch the overlay on again. In most cases userspace
3540 * has to recompute where to put it anyway.
3545 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3546 * cursor plane briefly if not already running after enabling the display
3548 * This workaround avoids occasional blank screens when self refresh is
3552 g4x_fixup_plane(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
3554 u32 cntl = I915_READ(CURCNTR(pipe));
3556 if ((cntl & CURSOR_MODE) == 0) {
3557 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3559 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3560 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3561 intel_wait_for_vblank(dev_priv->dev, pipe);
3562 I915_WRITE(CURCNTR(pipe), cntl);
3563 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3564 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3568 static void i9xx_pfit_enable(struct intel_crtc *crtc)
3570 struct drm_device *dev = crtc->base.dev;
3571 struct drm_i915_private *dev_priv = dev->dev_private;
3572 struct intel_crtc_config *pipe_config = &crtc->config;
3574 if (!crtc->config.gmch_pfit.control)
3578 * The panel fitter should only be adjusted whilst the pipe is disabled,
3579 * according to register description and PRM.
3581 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3582 assert_pipe_disabled(dev_priv, crtc->pipe);
3584 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3585 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
3587 /* Border color in case we don't scale up to the full screen. Black by
3588 * default, change to something else for debugging. */
3589 I915_WRITE(BCLRPAT(crtc->pipe), 0);
3592 static void valleyview_crtc_enable(struct drm_crtc *crtc)
3594 struct drm_device *dev = crtc->dev;
3595 struct drm_i915_private *dev_priv = dev->dev_private;
3596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3597 struct intel_encoder *encoder;
3598 int pipe = intel_crtc->pipe;
3599 int plane = intel_crtc->plane;
3601 WARN_ON(!crtc->enabled);
3603 if (intel_crtc->active)
3606 intel_crtc->active = true;
3607 intel_update_watermarks(dev);
3609 mutex_lock(&dev_priv->dpio_lock);
3611 for_each_encoder_on_crtc(dev, crtc, encoder)
3612 if (encoder->pre_pll_enable)
3613 encoder->pre_pll_enable(encoder);
3615 intel_enable_pll(dev_priv, pipe);
3617 for_each_encoder_on_crtc(dev, crtc, encoder)
3618 if (encoder->pre_enable)
3619 encoder->pre_enable(encoder);
3621 /* VLV wants encoder enabling _before_ the pipe is up. */
3622 for_each_encoder_on_crtc(dev, crtc, encoder)
3623 encoder->enable(encoder);
3625 i9xx_pfit_enable(intel_crtc);
3627 intel_crtc_load_lut(crtc);
3629 intel_enable_pipe(dev_priv, pipe, false);
3630 intel_enable_plane(dev_priv, plane, pipe);
3631 intel_enable_planes(crtc);
3632 intel_crtc_update_cursor(crtc, true);
3634 intel_update_fbc(dev);
3636 mutex_unlock(&dev_priv->dpio_lock);
3639 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3641 struct drm_device *dev = crtc->dev;
3642 struct drm_i915_private *dev_priv = dev->dev_private;
3643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3644 struct intel_encoder *encoder;
3645 int pipe = intel_crtc->pipe;
3646 int plane = intel_crtc->plane;
3648 WARN_ON(!crtc->enabled);
3650 if (intel_crtc->active)
3653 intel_crtc->active = true;
3654 intel_update_watermarks(dev);
3656 intel_enable_pll(dev_priv, pipe);
3658 for_each_encoder_on_crtc(dev, crtc, encoder)
3659 if (encoder->pre_enable)
3660 encoder->pre_enable(encoder);
3662 i9xx_pfit_enable(intel_crtc);
3664 intel_crtc_load_lut(crtc);
3666 intel_enable_pipe(dev_priv, pipe, false);
3667 intel_enable_plane(dev_priv, plane, pipe);
3668 intel_enable_planes(crtc);
3669 /* The fixup needs to happen before cursor is enabled */
3671 g4x_fixup_plane(dev_priv, pipe);
3672 intel_crtc_update_cursor(crtc, true);
3674 /* Give the overlay scaler a chance to enable if it's on this pipe */
3675 intel_crtc_dpms_overlay(intel_crtc, true);
3677 intel_update_fbc(dev);
3679 for_each_encoder_on_crtc(dev, crtc, encoder)
3680 encoder->enable(encoder);
3683 static void i9xx_pfit_disable(struct intel_crtc *crtc)
3685 struct drm_device *dev = crtc->base.dev;
3686 struct drm_i915_private *dev_priv = dev->dev_private;
3688 if (!crtc->config.gmch_pfit.control)
3691 assert_pipe_disabled(dev_priv, crtc->pipe);
3693 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3694 I915_READ(PFIT_CONTROL));
3695 I915_WRITE(PFIT_CONTROL, 0);
3698 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3700 struct drm_device *dev = crtc->dev;
3701 struct drm_i915_private *dev_priv = dev->dev_private;
3702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3703 struct intel_encoder *encoder;
3704 int pipe = intel_crtc->pipe;
3705 int plane = intel_crtc->plane;
3707 if (!intel_crtc->active)
3710 for_each_encoder_on_crtc(dev, crtc, encoder)
3711 encoder->disable(encoder);
3713 /* Give the overlay scaler a chance to disable if it's on this pipe */
3714 intel_crtc_wait_for_pending_flips(crtc);
3715 drm_vblank_off(dev, pipe);
3717 if (dev_priv->cfb_plane == plane)
3718 intel_disable_fbc(dev);
3720 intel_crtc_dpms_overlay(intel_crtc, false);
3721 intel_crtc_update_cursor(crtc, false);
3722 intel_disable_planes(crtc);
3723 intel_disable_plane(dev_priv, plane, pipe);
3725 intel_disable_pipe(dev_priv, pipe);
3727 i9xx_pfit_disable(intel_crtc);
3729 for_each_encoder_on_crtc(dev, crtc, encoder)
3730 if (encoder->post_disable)
3731 encoder->post_disable(encoder);
3733 intel_disable_pll(dev_priv, pipe);
3735 intel_crtc->active = false;
3736 intel_update_fbc(dev);
3737 intel_update_watermarks(dev);
3740 static void i9xx_crtc_off(struct drm_crtc *crtc)
3744 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3747 struct drm_device *dev = crtc->dev;
3748 struct drm_i915_private *dev_priv = dev->dev_private;
3750 struct drm_i915_master_private *master_priv;
3752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3753 int pipe = intel_crtc->pipe;
3756 if (!dev->primary->master)
3759 master_priv = dev->primary->master->driver_priv;
3760 if (!master_priv->sarea_priv)
3763 if (!dev_priv->sarea_priv)
3770 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3771 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3773 dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
3774 dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
3779 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3780 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3782 dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
3783 dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
3787 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3793 * Sets the power management mode of the pipe and plane.
3795 void intel_crtc_update_dpms(struct drm_crtc *crtc)
3797 struct drm_device *dev = crtc->dev;
3798 struct drm_i915_private *dev_priv = dev->dev_private;
3799 struct intel_encoder *intel_encoder;
3800 bool enable = false;
3802 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3803 enable |= intel_encoder->connectors_active;
3806 dev_priv->display.crtc_enable(crtc);
3808 dev_priv->display.crtc_disable(crtc);
3810 intel_crtc_update_sarea(crtc, enable);
3813 static void intel_crtc_disable(struct drm_crtc *crtc)
3815 struct drm_device *dev = crtc->dev;
3816 struct drm_connector *connector;
3817 struct drm_i915_private *dev_priv = dev->dev_private;
3818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3820 /* crtc should still be enabled when we disable it. */
3821 WARN_ON(!crtc->enabled);
3823 dev_priv->display.crtc_disable(crtc);
3824 intel_crtc->eld_vld = false;
3825 intel_crtc_update_sarea(crtc, false);
3826 dev_priv->display.off(crtc);
3828 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3829 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3832 mutex_lock(&dev->struct_mutex);
3833 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3834 mutex_unlock(&dev->struct_mutex);
3838 /* Update computed state. */
3839 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3840 if (!connector->encoder || !connector->encoder->crtc)
3843 if (connector->encoder->crtc != crtc)
3846 connector->dpms = DRM_MODE_DPMS_OFF;
3847 to_intel_encoder(connector->encoder)->connectors_active = false;
3851 void intel_modeset_disable(struct drm_device *dev)
3853 struct drm_crtc *crtc;
3855 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3857 intel_crtc_disable(crtc);
3861 void intel_encoder_destroy(struct drm_encoder *encoder)
3863 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3865 drm_encoder_cleanup(encoder);
3866 kfree(intel_encoder);
3869 /* Simple dpms helper for encodres with just one connector, no cloning and only
3870 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3871 * state of the entire output pipe. */
3872 void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3874 if (mode == DRM_MODE_DPMS_ON) {
3875 encoder->connectors_active = true;
3877 intel_crtc_update_dpms(encoder->base.crtc);
3879 encoder->connectors_active = false;
3881 intel_crtc_update_dpms(encoder->base.crtc);
3885 /* Cross check the actual hw state with our own modeset state tracking (and it's
3886 * internal consistency). */
3887 static void intel_connector_check_state(struct intel_connector *connector)
3889 if (connector->get_hw_state(connector)) {
3890 struct intel_encoder *encoder = connector->encoder;
3891 struct drm_crtc *crtc;
3892 bool encoder_enabled;
3893 enum i915_pipe pipe;
3895 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3896 connector->base.base.id,
3897 drm_get_connector_name(&connector->base));
3899 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3900 "wrong connector dpms state\n");
3901 WARN(connector->base.encoder != &encoder->base,
3902 "active connector not linked to encoder\n");
3903 WARN(!encoder->connectors_active,
3904 "encoder->connectors_active not set\n");
3906 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3907 WARN(!encoder_enabled, "encoder not enabled\n");
3908 if (WARN_ON(!encoder->base.crtc))
3911 crtc = encoder->base.crtc;
3913 WARN(!crtc->enabled, "crtc not enabled\n");
3914 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3915 WARN(pipe != to_intel_crtc(crtc)->pipe,
3916 "encoder active on the wrong pipe\n");
3920 /* Even simpler default implementation, if there's really no special case to
3922 void intel_connector_dpms(struct drm_connector *connector, int mode)
3924 struct intel_encoder *encoder = intel_attached_encoder(connector);
3926 /* All the simple cases only support two dpms states. */
3927 if (mode != DRM_MODE_DPMS_ON)
3928 mode = DRM_MODE_DPMS_OFF;
3930 if (mode == connector->dpms)
3933 connector->dpms = mode;
3935 /* Only need to change hw state when actually enabled */
3936 if (encoder->base.crtc)
3937 intel_encoder_dpms(encoder, mode);
3939 WARN_ON(encoder->connectors_active != false);
3941 intel_modeset_check_state(connector->dev);
3944 /* Simple connector->get_hw_state implementation for encoders that support only
3945 * one connector and no cloning and hence the encoder state determines the state
3946 * of the connector. */
3947 bool intel_connector_get_hw_state(struct intel_connector *connector)
3949 enum i915_pipe pipe = 0;
3950 struct intel_encoder *encoder = connector->encoder;
3952 return encoder->get_hw_state(encoder, &pipe);
3955 static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum i915_pipe pipe,
3956 struct intel_crtc_config *pipe_config)
3958 struct drm_i915_private *dev_priv = dev->dev_private;
3959 struct intel_crtc *pipe_B_crtc =
3960 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3962 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3963 pipe_name(pipe), pipe_config->fdi_lanes);
3964 if (pipe_config->fdi_lanes > 4) {
3965 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3966 pipe_name(pipe), pipe_config->fdi_lanes);
3970 if (IS_HASWELL(dev)) {
3971 if (pipe_config->fdi_lanes > 2) {
3972 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
3973 pipe_config->fdi_lanes);
3980 if (INTEL_INFO(dev)->num_pipes == 2)
3983 /* Ivybridge 3 pipe is really complicated */
3988 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
3989 pipe_config->fdi_lanes > 2) {
3990 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
3991 pipe_name(pipe), pipe_config->fdi_lanes);
3996 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
3997 pipe_B_crtc->config.fdi_lanes <= 2) {
3998 if (pipe_config->fdi_lanes > 2) {
3999 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4000 pipe_name(pipe), pipe_config->fdi_lanes);
4004 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4014 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4015 struct intel_crtc_config *pipe_config)
4017 struct drm_device *dev = intel_crtc->base.dev;
4018 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4019 int lane, link_bw, fdi_dotclock;
4020 bool setup_ok, needs_recompute = false;
4023 /* FDI is a binary signal running at ~2.7GHz, encoding
4024 * each output octet as 10 bits. The actual frequency
4025 * is stored as a divider into a 100MHz clock, and the
4026 * mode pixel clock is stored in units of 1KHz.
4027 * Hence the bw of each lane in terms of the mode signal
4030 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4032 fdi_dotclock = adjusted_mode->clock;
4033 fdi_dotclock /= pipe_config->pixel_multiplier;
4035 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
4036 pipe_config->pipe_bpp);
4038 pipe_config->fdi_lanes = lane;
4040 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
4041 link_bw, &pipe_config->fdi_m_n);
4043 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4044 intel_crtc->pipe, pipe_config);
4045 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4046 pipe_config->pipe_bpp -= 2*3;
4047 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4048 pipe_config->pipe_bpp);
4049 needs_recompute = true;
4050 pipe_config->bw_constrained = true;
4055 if (needs_recompute)
4058 return setup_ok ? 0 : -EINVAL;
4061 static void hsw_compute_ips_config(struct intel_crtc *crtc,
4062 struct intel_crtc_config *pipe_config)
4064 pipe_config->ips_enabled = i915_enable_ips &&
4065 hsw_crtc_supports_ips(crtc) &&
4066 pipe_config->pipe_bpp == 24;
4069 static int intel_crtc_compute_config(struct intel_crtc *crtc,
4070 struct intel_crtc_config *pipe_config)
4072 struct drm_device *dev = crtc->base.dev;
4073 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4075 if (HAS_PCH_SPLIT(dev)) {
4076 /* FDI link clock is fixed at 2.7G */
4077 if (pipe_config->requested_mode.clock * 3
4078 > IRONLAKE_FDI_FREQ * 4)
4082 /* All interlaced capable intel hw wants timings in frames. Note though
4083 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4084 * timings, so we need to be careful not to clobber these.*/
4085 if (!pipe_config->timings_set)
4086 drm_mode_set_crtcinfo(adjusted_mode, 0);
4088 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4089 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4091 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4092 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4095 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4096 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4097 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4098 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4100 pipe_config->pipe_bpp = 8*3;
4104 hsw_compute_ips_config(crtc, pipe_config);
4106 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4107 * clock survives for now. */
4108 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4109 pipe_config->shared_dpll = crtc->config.shared_dpll;
4111 if (pipe_config->has_pch_encoder)
4112 return ironlake_fdi_compute_config(crtc, pipe_config);
4117 static int valleyview_get_display_clock_speed(struct drm_device *dev)
4119 return 400000; /* FIXME */
4122 static int i945_get_display_clock_speed(struct drm_device *dev)
4127 static int i915_get_display_clock_speed(struct drm_device *dev)
4132 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4137 static int i915gm_get_display_clock_speed(struct drm_device *dev)
4141 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4143 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4146 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4147 case GC_DISPLAY_CLOCK_333_MHZ:
4150 case GC_DISPLAY_CLOCK_190_200_MHZ:
4156 static int i865_get_display_clock_speed(struct drm_device *dev)
4161 static int i855_get_display_clock_speed(struct drm_device *dev)
4164 /* Assume that the hardware is in the high speed state. This
4165 * should be the default.
4167 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4168 case GC_CLOCK_133_200:
4169 case GC_CLOCK_100_200:
4171 case GC_CLOCK_166_250:
4173 case GC_CLOCK_100_133:
4177 /* Shouldn't happen */
4181 static int i830_get_display_clock_speed(struct drm_device *dev)
4187 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
4189 while (*num > DATA_LINK_M_N_MASK ||
4190 *den > DATA_LINK_M_N_MASK) {
4196 static void compute_m_n(unsigned int m, unsigned int n,
4197 uint32_t *ret_m, uint32_t *ret_n)
4199 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4200 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4201 intel_reduce_m_n_ratio(ret_m, ret_n);
4205 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4206 int pixel_clock, int link_clock,
4207 struct intel_link_m_n *m_n)
4211 compute_m_n(bits_per_pixel * pixel_clock,
4212 link_clock * nlanes * 8,
4213 &m_n->gmch_m, &m_n->gmch_n);
4215 compute_m_n(pixel_clock, link_clock,
4216 &m_n->link_m, &m_n->link_n);
4219 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4221 if (i915_panel_use_ssc >= 0)
4222 return i915_panel_use_ssc != 0;
4223 return dev_priv->vbt.lvds_use_ssc
4224 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4227 static int vlv_get_refclk(struct drm_crtc *crtc)
4229 struct drm_device *dev = crtc->dev;
4230 struct drm_i915_private *dev_priv = dev->dev_private;
4231 int refclk = 27000; /* for DP & HDMI */
4233 return 100000; /* only one validated so far */
4235 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4237 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4238 if (intel_panel_use_ssc(dev_priv))
4242 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4249 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4251 struct drm_device *dev = crtc->dev;
4252 struct drm_i915_private *dev_priv = dev->dev_private;
4255 if (IS_VALLEYVIEW(dev)) {
4256 refclk = vlv_get_refclk(crtc);
4257 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4258 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4259 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
4260 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4262 } else if (!IS_GEN2(dev)) {
4271 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4273 return (1 << dpll->n) << 16 | dpll->m2;
4276 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4278 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4281 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4282 intel_clock_t *reduced_clock)
4284 struct drm_device *dev = crtc->base.dev;
4285 struct drm_i915_private *dev_priv = dev->dev_private;
4286 int pipe = crtc->pipe;
4289 if (IS_PINEVIEW(dev)) {
4290 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
4292 fp2 = pnv_dpll_compute_fp(reduced_clock);
4294 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
4296 fp2 = i9xx_dpll_compute_fp(reduced_clock);
4299 I915_WRITE(FP0(pipe), fp);
4301 crtc->lowfreq_avail = false;
4302 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4303 reduced_clock && i915_powersave) {
4304 I915_WRITE(FP1(pipe), fp2);
4305 crtc->lowfreq_avail = true;
4307 I915_WRITE(FP1(pipe), fp);
4311 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4316 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4317 * and set it to a reasonable value instead.
4319 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4320 reg_val &= 0xffffff00;
4321 reg_val |= 0x00000030;
4322 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4324 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4325 reg_val &= 0x8cffffff;
4326 reg_val = 0x8c000000;
4327 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4329 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4330 reg_val &= 0xffffff00;
4331 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4333 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4334 reg_val &= 0x00ffffff;
4335 reg_val |= 0xb0000000;
4336 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4339 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4340 struct intel_link_m_n *m_n)
4342 struct drm_device *dev = crtc->base.dev;
4343 struct drm_i915_private *dev_priv = dev->dev_private;
4344 int pipe = crtc->pipe;
4346 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4347 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4348 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4349 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4352 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4353 struct intel_link_m_n *m_n)
4355 struct drm_device *dev = crtc->base.dev;
4356 struct drm_i915_private *dev_priv = dev->dev_private;
4357 int pipe = crtc->pipe;
4358 enum transcoder transcoder = crtc->config.cpu_transcoder;
4360 if (INTEL_INFO(dev)->gen >= 5) {
4361 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4362 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4363 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4364 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4366 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4367 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4368 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4369 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
4373 static void intel_dp_set_m_n(struct intel_crtc *crtc)
4375 if (crtc->config.has_pch_encoder)
4376 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4378 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4381 static void vlv_update_pll(struct intel_crtc *crtc)
4383 struct drm_device *dev = crtc->base.dev;
4384 struct drm_i915_private *dev_priv = dev->dev_private;
4385 struct intel_encoder *encoder;
4386 int pipe = crtc->pipe;
4388 u32 bestn, bestm1, bestm2, bestp1, bestp2;
4390 u32 coreclk, reg_val, dpll_md;
4392 mutex_lock(&dev_priv->dpio_lock);
4394 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4396 bestn = crtc->config.dpll.n;
4397 bestm1 = crtc->config.dpll.m1;
4398 bestm2 = crtc->config.dpll.m2;
4399 bestp1 = crtc->config.dpll.p1;
4400 bestp2 = crtc->config.dpll.p2;
4402 /* See eDP HDMI DPIO driver vbios notes doc */
4404 /* PLL B needs special handling */
4406 vlv_pllb_recal_opamp(dev_priv);
4408 /* Set up Tx target for periodic Rcomp update */
4409 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4411 /* Disable target IRef on PLL */
4412 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4413 reg_val &= 0x00ffffff;
4414 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4416 /* Disable fast lock */
4417 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4419 /* Set idtafcrecal before PLL is enabled */
4420 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4421 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4422 mdiv |= ((bestn << DPIO_N_SHIFT));
4423 mdiv |= (1 << DPIO_K_SHIFT);
4426 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4427 * but we don't support that).
4428 * Note: don't use the DAC post divider as it seems unstable.
4430 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4431 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4433 mdiv |= DPIO_ENABLE_CALIBRATION;
4434 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4436 /* Set HBR and RBR LPF coefficients */
4437 if (crtc->config.port_clock == 162000 ||
4438 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
4439 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4440 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4443 vlv_dpio_write(dev_priv, DPIO_LPF_COEFF(pipe),
4446 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4447 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4448 /* Use SSC source */
4450 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4453 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4455 } else { /* HDMI or VGA */
4456 /* Use bend source */
4458 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4461 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4465 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4466 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4467 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4468 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4469 coreclk |= 0x01000000;
4470 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4472 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4474 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4475 if (encoder->pre_pll_enable)
4476 encoder->pre_pll_enable(encoder);
4478 /* Enable DPIO clock input */
4479 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4480 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4482 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4484 dpll |= DPLL_VCO_ENABLE;
4485 I915_WRITE(DPLL(pipe), dpll);
4486 POSTING_READ(DPLL(pipe));
4489 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4490 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4492 dpll_md = (crtc->config.pixel_multiplier - 1)
4493 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4494 I915_WRITE(DPLL_MD(pipe), dpll_md);
4495 POSTING_READ(DPLL_MD(pipe));
4497 if (crtc->config.has_dp_encoder)
4498 intel_dp_set_m_n(crtc);
4500 mutex_unlock(&dev_priv->dpio_lock);
4503 static void i9xx_update_pll(struct intel_crtc *crtc,
4504 intel_clock_t *reduced_clock,
4507 struct drm_device *dev = crtc->base.dev;
4508 struct drm_i915_private *dev_priv = dev->dev_private;
4509 struct intel_encoder *encoder;
4510 int pipe = crtc->pipe;
4513 struct dpll *clock = &crtc->config.dpll;
4515 i9xx_update_pll_dividers(crtc, reduced_clock);
4517 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4518 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4520 dpll = DPLL_VGA_MODE_DIS;
4522 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4523 dpll |= DPLLB_MODE_LVDS;
4525 dpll |= DPLLB_MODE_DAC_SERIAL;
4527 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4528 dpll |= (crtc->config.pixel_multiplier - 1)
4529 << SDVO_MULTIPLIER_SHIFT_HIRES;
4533 dpll |= DPLL_DVO_HIGH_SPEED;
4535 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4536 dpll |= DPLL_DVO_HIGH_SPEED;
4538 /* compute bitmask from p1 value */
4539 if (IS_PINEVIEW(dev))
4540 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4542 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4543 if (IS_G4X(dev) && reduced_clock)
4544 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4546 switch (clock->p2) {
4548 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4551 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4554 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4557 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4560 if (INTEL_INFO(dev)->gen >= 4)
4561 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4563 if (crtc->config.sdvo_tv_clock)
4564 dpll |= PLL_REF_INPUT_TVCLKINBC;
4565 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4566 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4567 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4569 dpll |= PLL_REF_INPUT_DREFCLK;
4571 dpll |= DPLL_VCO_ENABLE;
4572 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4573 POSTING_READ(DPLL(pipe));
4576 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4577 if (encoder->pre_pll_enable)
4578 encoder->pre_pll_enable(encoder);
4580 if (crtc->config.has_dp_encoder)
4581 intel_dp_set_m_n(crtc);
4583 I915_WRITE(DPLL(pipe), dpll);
4585 /* Wait for the clocks to stabilize. */
4586 POSTING_READ(DPLL(pipe));
4589 if (INTEL_INFO(dev)->gen >= 4) {
4590 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4591 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4592 I915_WRITE(DPLL_MD(pipe), dpll_md);
4594 /* The pixel multiplier can only be updated once the
4595 * DPLL is enabled and the clocks are stable.
4597 * So write it again.
4599 I915_WRITE(DPLL(pipe), dpll);
4603 static void i8xx_update_pll(struct intel_crtc *crtc,
4604 intel_clock_t *reduced_clock,
4607 struct drm_device *dev = crtc->base.dev;
4608 struct drm_i915_private *dev_priv = dev->dev_private;
4609 struct intel_encoder *encoder;
4610 int pipe = crtc->pipe;
4612 struct dpll *clock = &crtc->config.dpll;
4614 i9xx_update_pll_dividers(crtc, reduced_clock);
4616 dpll = DPLL_VGA_MODE_DIS;
4618 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4619 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4622 dpll |= PLL_P1_DIVIDE_BY_TWO;
4624 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4626 dpll |= PLL_P2_DIVIDE_BY_4;
4629 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4630 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4631 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4633 dpll |= PLL_REF_INPUT_DREFCLK;
4635 dpll |= DPLL_VCO_ENABLE;
4636 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4637 POSTING_READ(DPLL(pipe));
4640 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4641 if (encoder->pre_pll_enable)
4642 encoder->pre_pll_enable(encoder);
4644 I915_WRITE(DPLL(pipe), dpll);
4646 /* Wait for the clocks to stabilize. */
4647 POSTING_READ(DPLL(pipe));
4650 /* The pixel multiplier can only be updated once the
4651 * DPLL is enabled and the clocks are stable.
4653 * So write it again.
4655 I915_WRITE(DPLL(pipe), dpll);
4658 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
4660 struct drm_device *dev = intel_crtc->base.dev;
4661 struct drm_i915_private *dev_priv = dev->dev_private;
4662 enum i915_pipe pipe = intel_crtc->pipe;
4663 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4664 struct drm_display_mode *adjusted_mode =
4665 &intel_crtc->config.adjusted_mode;
4666 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4667 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4669 /* We need to be careful not to changed the adjusted mode, for otherwise
4670 * the hw state checker will get angry at the mismatch. */
4671 crtc_vtotal = adjusted_mode->crtc_vtotal;
4672 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4674 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4675 /* the chip adds 2 halflines automatically */
4677 crtc_vblank_end -= 1;
4678 vsyncshift = adjusted_mode->crtc_hsync_start
4679 - adjusted_mode->crtc_htotal / 2;
4684 if (INTEL_INFO(dev)->gen > 3)
4685 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4687 I915_WRITE(HTOTAL(cpu_transcoder),
4688 (adjusted_mode->crtc_hdisplay - 1) |
4689 ((adjusted_mode->crtc_htotal - 1) << 16));
4690 I915_WRITE(HBLANK(cpu_transcoder),
4691 (adjusted_mode->crtc_hblank_start - 1) |
4692 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4693 I915_WRITE(HSYNC(cpu_transcoder),
4694 (adjusted_mode->crtc_hsync_start - 1) |
4695 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4697 I915_WRITE(VTOTAL(cpu_transcoder),
4698 (adjusted_mode->crtc_vdisplay - 1) |
4699 ((crtc_vtotal - 1) << 16));
4700 I915_WRITE(VBLANK(cpu_transcoder),
4701 (adjusted_mode->crtc_vblank_start - 1) |
4702 ((crtc_vblank_end - 1) << 16));
4703 I915_WRITE(VSYNC(cpu_transcoder),
4704 (adjusted_mode->crtc_vsync_start - 1) |
4705 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4707 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4708 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4709 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4711 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4712 (pipe == PIPE_B || pipe == PIPE_C))
4713 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4715 /* pipesrc controls the size that is scaled from, which should
4716 * always be the user's requested size.
4718 I915_WRITE(PIPESRC(pipe),
4719 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4722 static void intel_get_pipe_timings(struct intel_crtc *crtc,
4723 struct intel_crtc_config *pipe_config)
4725 struct drm_device *dev = crtc->base.dev;
4726 struct drm_i915_private *dev_priv = dev->dev_private;
4727 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4730 tmp = I915_READ(HTOTAL(cpu_transcoder));
4731 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4732 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4733 tmp = I915_READ(HBLANK(cpu_transcoder));
4734 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4735 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4736 tmp = I915_READ(HSYNC(cpu_transcoder));
4737 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4738 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4740 tmp = I915_READ(VTOTAL(cpu_transcoder));
4741 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4742 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4743 tmp = I915_READ(VBLANK(cpu_transcoder));
4744 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4745 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4746 tmp = I915_READ(VSYNC(cpu_transcoder));
4747 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4748 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4750 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4751 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4752 pipe_config->adjusted_mode.crtc_vtotal += 1;
4753 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4756 tmp = I915_READ(PIPESRC(crtc->pipe));
4757 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4758 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4761 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4763 struct drm_device *dev = intel_crtc->base.dev;
4764 struct drm_i915_private *dev_priv = dev->dev_private;
4769 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4770 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4773 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4776 if (intel_crtc->config.requested_mode.clock >
4777 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4778 pipeconf |= PIPECONF_DOUBLE_WIDE;
4781 /* only g4x and later have fancy bpc/dither controls */
4782 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4783 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4784 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4785 pipeconf |= PIPECONF_DITHER_EN |
4786 PIPECONF_DITHER_TYPE_SP;
4788 switch (intel_crtc->config.pipe_bpp) {
4790 pipeconf |= PIPECONF_6BPC;
4793 pipeconf |= PIPECONF_8BPC;
4796 pipeconf |= PIPECONF_10BPC;
4799 /* Case prevented by intel_choose_pipe_bpp_dither. */
4804 if (HAS_PIPE_CXSR(dev)) {
4805 if (intel_crtc->lowfreq_avail) {
4806 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4807 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4809 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4813 if (!IS_GEN2(dev) &&
4814 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4815 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4817 pipeconf |= PIPECONF_PROGRESSIVE;
4819 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
4820 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4822 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4823 POSTING_READ(PIPECONF(intel_crtc->pipe));
4826 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4828 struct drm_framebuffer *fb)
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_private *dev_priv = dev->dev_private;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4834 int pipe = intel_crtc->pipe;
4835 int plane = intel_crtc->plane;
4836 int refclk, num_connectors = 0;
4837 intel_clock_t clock, reduced_clock;
4839 bool ok, has_reduced_clock = false;
4840 bool is_lvds = false;
4841 struct intel_encoder *encoder;
4842 const intel_limit_t *limit;
4845 for_each_encoder_on_crtc(dev, crtc, encoder) {
4846 switch (encoder->type) {
4847 case INTEL_OUTPUT_LVDS:
4855 refclk = i9xx_get_refclk(crtc, num_connectors);
4858 * Returns a set of divisors for the desired target clock with the given
4859 * refclk, or FALSE. The returned values represent the clock equation:
4860 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4862 limit = intel_limit(crtc, refclk);
4863 ok = dev_priv->display.find_dpll(limit, crtc,
4864 intel_crtc->config.port_clock,
4865 refclk, NULL, &clock);
4866 if (!ok && !intel_crtc->config.clock_set) {
4867 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4871 /* Ensure that the cursor is valid for the new mode before changing... */
4872 intel_crtc_update_cursor(crtc, true);
4874 if (is_lvds && dev_priv->lvds_downclock_avail) {
4876 * Ensure we match the reduced clock's P to the target clock.
4877 * If the clocks don't match, we can't switch the display clock
4878 * by using the FP0/FP1. In such case we will disable the LVDS
4879 * downclock feature.
4882 dev_priv->display.find_dpll(limit, crtc,
4883 dev_priv->lvds_downclock,
4887 /* Compat-code for transition, will disappear. */
4888 if (!intel_crtc->config.clock_set) {
4889 intel_crtc->config.dpll.n = clock.n;
4890 intel_crtc->config.dpll.m1 = clock.m1;
4891 intel_crtc->config.dpll.m2 = clock.m2;
4892 intel_crtc->config.dpll.p1 = clock.p1;
4893 intel_crtc->config.dpll.p2 = clock.p2;
4897 i8xx_update_pll(intel_crtc,
4898 has_reduced_clock ? &reduced_clock : NULL,
4900 else if (IS_VALLEYVIEW(dev))
4901 vlv_update_pll(intel_crtc);
4903 i9xx_update_pll(intel_crtc,
4904 has_reduced_clock ? &reduced_clock : NULL,
4907 /* Set up the display plane register */
4908 dspcntr = DISPPLANE_GAMMA_ENABLE;
4910 if (!IS_VALLEYVIEW(dev)) {
4912 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4914 dspcntr |= DISPPLANE_SEL_PIPE_B;
4917 intel_set_pipe_timings(intel_crtc);
4919 /* pipesrc and dspsize control the size that is scaled from,
4920 * which should always be the user's requested size.
4922 I915_WRITE(DSPSIZE(plane),
4923 ((mode->vdisplay - 1) << 16) |
4924 (mode->hdisplay - 1));
4925 I915_WRITE(DSPPOS(plane), 0);
4927 i9xx_set_pipeconf(intel_crtc);
4929 I915_WRITE(DSPCNTR(plane), dspcntr);
4930 POSTING_READ(DSPCNTR(plane));
4932 ret = intel_pipe_set_base(crtc, x, y, fb);
4934 intel_update_watermarks(dev);
4939 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4940 struct intel_crtc_config *pipe_config)
4942 struct drm_device *dev = crtc->base.dev;
4943 struct drm_i915_private *dev_priv = dev->dev_private;
4946 tmp = I915_READ(PFIT_CONTROL);
4947 if (!(tmp & PFIT_ENABLE))
4950 /* Check whether the pfit is attached to our pipe. */
4951 if (INTEL_INFO(dev)->gen < 4) {
4952 if (crtc->pipe != PIPE_B)
4955 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4959 pipe_config->gmch_pfit.control = tmp;
4960 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4961 if (INTEL_INFO(dev)->gen < 5)
4962 pipe_config->gmch_pfit.lvds_border_bits =
4963 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4966 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4967 struct intel_crtc_config *pipe_config)
4969 struct drm_device *dev = crtc->base.dev;
4970 struct drm_i915_private *dev_priv = dev->dev_private;
4973 pipe_config->cpu_transcoder = crtc->pipe;
4974 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
4976 tmp = I915_READ(PIPECONF(crtc->pipe));
4977 if (!(tmp & PIPECONF_ENABLE))
4980 intel_get_pipe_timings(crtc, pipe_config);
4982 i9xx_get_pfit_config(crtc, pipe_config);
4984 if (INTEL_INFO(dev)->gen >= 4) {
4985 tmp = I915_READ(DPLL_MD(crtc->pipe));
4986 pipe_config->pixel_multiplier =
4987 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4988 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
4989 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4990 tmp = I915_READ(DPLL(crtc->pipe));
4991 pipe_config->pixel_multiplier =
4992 ((tmp & SDVO_MULTIPLIER_MASK)
4993 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
4995 /* Note that on i915G/GM the pixel multiplier is in the sdvo
4996 * port and will be fixed up in the encoder->get_config
4998 pipe_config->pixel_multiplier = 1;
5004 static void ironlake_init_pch_refclk(struct drm_device *dev)
5006 struct drm_i915_private *dev_priv = dev->dev_private;
5007 struct drm_mode_config *mode_config = &dev->mode_config;
5008 struct intel_encoder *encoder;
5010 bool has_lvds = false;
5011 bool has_cpu_edp = false;
5012 bool has_panel = false;
5013 bool has_ck505 = false;
5014 bool can_ssc = false;
5016 /* We need to take the global config into account */
5017 list_for_each_entry(encoder, &mode_config->encoder_list,
5019 switch (encoder->type) {
5020 case INTEL_OUTPUT_LVDS:
5024 case INTEL_OUTPUT_EDP:
5026 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
5032 if (HAS_PCH_IBX(dev)) {
5033 has_ck505 = dev_priv->vbt.display_clock_mode;
5034 can_ssc = has_ck505;
5040 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5041 has_panel, has_lvds, has_ck505);
5043 /* Ironlake: try to setup display ref clock before DPLL
5044 * enabling. This is only under driver's control after
5045 * PCH B stepping, previous chipset stepping should be
5046 * ignoring this setting.
5048 val = I915_READ(PCH_DREF_CONTROL);
5050 /* As we must carefully and slowly disable/enable each source in turn,
5051 * compute the final state we want first and check if we need to
5052 * make any changes at all.
5055 final &= ~DREF_NONSPREAD_SOURCE_MASK;
5057 final |= DREF_NONSPREAD_CK505_ENABLE;
5059 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5061 final &= ~DREF_SSC_SOURCE_MASK;
5062 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5063 final &= ~DREF_SSC1_ENABLE;
5066 final |= DREF_SSC_SOURCE_ENABLE;
5068 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5069 final |= DREF_SSC1_ENABLE;
5072 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5073 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5075 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5077 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5079 final |= DREF_SSC_SOURCE_DISABLE;
5080 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5086 /* Always enable nonspread source */
5087 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5090 val |= DREF_NONSPREAD_CK505_ENABLE;
5092 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5095 val &= ~DREF_SSC_SOURCE_MASK;
5096 val |= DREF_SSC_SOURCE_ENABLE;
5098 /* SSC must be turned on before enabling the CPU output */
5099 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5100 DRM_DEBUG_KMS("Using SSC on panel\n");
5101 val |= DREF_SSC1_ENABLE;
5103 val &= ~DREF_SSC1_ENABLE;
5105 /* Get SSC going before enabling the outputs */
5106 I915_WRITE(PCH_DREF_CONTROL, val);
5107 POSTING_READ(PCH_DREF_CONTROL);
5110 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5112 /* Enable CPU source on CPU attached eDP */
5114 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5115 DRM_DEBUG_KMS("Using SSC on eDP\n");
5116 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5119 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5121 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5123 I915_WRITE(PCH_DREF_CONTROL, val);
5124 POSTING_READ(PCH_DREF_CONTROL);
5127 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5129 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5131 /* Turn off CPU output */
5132 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5134 I915_WRITE(PCH_DREF_CONTROL, val);
5135 POSTING_READ(PCH_DREF_CONTROL);
5138 /* Turn off the SSC source */
5139 val &= ~DREF_SSC_SOURCE_MASK;
5140 val |= DREF_SSC_SOURCE_DISABLE;
5143 val &= ~DREF_SSC1_ENABLE;
5145 I915_WRITE(PCH_DREF_CONTROL, val);
5146 POSTING_READ(PCH_DREF_CONTROL);
5150 BUG_ON(val != final);
5153 /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5154 static void lpt_init_pch_refclk(struct drm_device *dev)
5156 struct drm_i915_private *dev_priv = dev->dev_private;
5157 struct drm_mode_config *mode_config = &dev->mode_config;
5158 struct intel_encoder *encoder;
5159 bool has_vga = false;
5160 bool is_sdv = false;
5163 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5164 switch (encoder->type) {
5165 case INTEL_OUTPUT_ANALOG:
5174 mutex_lock(&dev_priv->dpio_lock);
5176 /* XXX: Rip out SDV support once Haswell ships for real. */
5177 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5180 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5181 tmp &= ~SBI_SSCCTL_DISABLE;
5182 tmp |= SBI_SSCCTL_PATHALT;
5183 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5187 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5188 tmp &= ~SBI_SSCCTL_PATHALT;
5189 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5192 tmp = I915_READ(SOUTH_CHICKEN2);
5193 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5194 I915_WRITE(SOUTH_CHICKEN2, tmp);
5196 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5197 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5198 DRM_ERROR("FDI mPHY reset assert timeout\n");
5200 tmp = I915_READ(SOUTH_CHICKEN2);
5201 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5202 I915_WRITE(SOUTH_CHICKEN2, tmp);
5204 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5205 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5207 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5210 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5211 tmp &= ~(0xFF << 24);
5212 tmp |= (0x12 << 24);
5213 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5216 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5218 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5221 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5223 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5225 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5227 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5230 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5231 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5232 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5234 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5235 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5236 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5238 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5240 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5242 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5244 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5247 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5248 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5249 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5251 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5252 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5253 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5256 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5259 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5261 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5264 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5267 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5270 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5272 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5275 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5277 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5278 tmp &= ~(0xFF << 16);
5279 tmp |= (0x1C << 16);
5280 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5282 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5283 tmp &= ~(0xFF << 16);
5284 tmp |= (0x1C << 16);
5285 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5288 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5290 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5292 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5294 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5296 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5297 tmp &= ~(0xF << 28);
5299 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5301 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5302 tmp &= ~(0xF << 28);
5304 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5307 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5308 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5309 tmp |= SBI_DBUFF0_ENABLE;
5310 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
5312 mutex_unlock(&dev_priv->dpio_lock);
5316 * Initialize reference clocks when the driver loads
5318 void intel_init_pch_refclk(struct drm_device *dev)
5320 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5321 ironlake_init_pch_refclk(dev);
5322 else if (HAS_PCH_LPT(dev))
5323 lpt_init_pch_refclk(dev);
5326 static int ironlake_get_refclk(struct drm_crtc *crtc)
5328 struct drm_device *dev = crtc->dev;
5329 struct drm_i915_private *dev_priv = dev->dev_private;
5330 struct intel_encoder *encoder;
5331 int num_connectors = 0;
5332 bool is_lvds = false;
5334 for_each_encoder_on_crtc(dev, crtc, encoder) {
5335 switch (encoder->type) {
5336 case INTEL_OUTPUT_LVDS:
5343 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5344 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5345 dev_priv->vbt.lvds_ssc_freq);
5346 return dev_priv->vbt.lvds_ssc_freq * 1000;
5352 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
5354 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5356 int pipe = intel_crtc->pipe;
5361 switch (intel_crtc->config.pipe_bpp) {
5363 val |= PIPECONF_6BPC;
5366 val |= PIPECONF_8BPC;
5369 val |= PIPECONF_10BPC;
5372 val |= PIPECONF_12BPC;
5375 /* Case prevented by intel_choose_pipe_bpp_dither. */
5379 if (intel_crtc->config.dither)
5380 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5382 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5383 val |= PIPECONF_INTERLACED_ILK;
5385 val |= PIPECONF_PROGRESSIVE;
5387 if (intel_crtc->config.limited_color_range)
5388 val |= PIPECONF_COLOR_RANGE_SELECT;
5390 I915_WRITE(PIPECONF(pipe), val);
5391 POSTING_READ(PIPECONF(pipe));
5395 * Set up the pipe CSC unit.
5397 * Currently only full range RGB to limited range RGB conversion
5398 * is supported, but eventually this should handle various
5399 * RGB<->YCbCr scenarios as well.
5401 static void intel_set_pipe_csc(struct drm_crtc *crtc)
5403 struct drm_device *dev = crtc->dev;
5404 struct drm_i915_private *dev_priv = dev->dev_private;
5405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5406 int pipe = intel_crtc->pipe;
5407 uint16_t coeff = 0x7800; /* 1.0 */
5410 * TODO: Check what kind of values actually come out of the pipe
5411 * with these coeff/postoff values and adjust to get the best
5412 * accuracy. Perhaps we even need to take the bpc value into
5416 if (intel_crtc->config.limited_color_range)
5417 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5420 * GY/GU and RY/RU should be the other way around according
5421 * to BSpec, but reality doesn't agree. Just set them up in
5422 * a way that results in the correct picture.
5424 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5425 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5427 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5428 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5430 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5431 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5433 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5434 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5435 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5437 if (INTEL_INFO(dev)->gen > 6) {
5438 uint16_t postoff = 0;
5440 if (intel_crtc->config.limited_color_range)
5441 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5443 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5444 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5445 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5447 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5449 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5451 if (intel_crtc->config.limited_color_range)
5452 mode |= CSC_BLACK_SCREEN_OFFSET;
5454 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5458 static void haswell_set_pipeconf(struct drm_crtc *crtc)
5460 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5462 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5467 if (intel_crtc->config.dither)
5468 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5470 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5471 val |= PIPECONF_INTERLACED_ILK;
5473 val |= PIPECONF_PROGRESSIVE;
5475 I915_WRITE(PIPECONF(cpu_transcoder), val);
5476 POSTING_READ(PIPECONF(cpu_transcoder));
5478 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
5479 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
5482 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5483 intel_clock_t *clock,
5484 bool *has_reduced_clock,
5485 intel_clock_t *reduced_clock)
5487 struct drm_device *dev = crtc->dev;
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489 struct intel_encoder *intel_encoder;
5491 const intel_limit_t *limit;
5492 bool ret, is_lvds = false;
5494 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5495 switch (intel_encoder->type) {
5496 case INTEL_OUTPUT_LVDS:
5502 refclk = ironlake_get_refclk(crtc);
5505 * Returns a set of divisors for the desired target clock with the given
5506 * refclk, or FALSE. The returned values represent the clock equation:
5507 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5509 limit = intel_limit(crtc, refclk);
5510 ret = dev_priv->display.find_dpll(limit, crtc,
5511 to_intel_crtc(crtc)->config.port_clock,
5512 refclk, NULL, clock);
5516 if (is_lvds && dev_priv->lvds_downclock_avail) {
5518 * Ensure we match the reduced clock's P to the target clock.
5519 * If the clocks don't match, we can't switch the display clock
5520 * by using the FP0/FP1. In such case we will disable the LVDS
5521 * downclock feature.
5523 *has_reduced_clock =
5524 dev_priv->display.find_dpll(limit, crtc,
5525 dev_priv->lvds_downclock,
5533 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5535 struct drm_i915_private *dev_priv = dev->dev_private;
5538 temp = I915_READ(SOUTH_CHICKEN1);
5539 if (temp & FDI_BC_BIFURCATION_SELECT)
5542 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5543 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5545 temp |= FDI_BC_BIFURCATION_SELECT;
5546 DRM_DEBUG_KMS("enabling fdi C rx\n");
5547 I915_WRITE(SOUTH_CHICKEN1, temp);
5548 POSTING_READ(SOUTH_CHICKEN1);
5551 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
5553 struct drm_device *dev = intel_crtc->base.dev;
5554 struct drm_i915_private *dev_priv = dev->dev_private;
5556 switch (intel_crtc->pipe) {
5560 if (intel_crtc->config.fdi_lanes > 2)
5561 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5563 cpt_enable_fdi_bc_bifurcation(dev);
5567 cpt_enable_fdi_bc_bifurcation(dev);
5575 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5578 * Account for spread spectrum to avoid
5579 * oversubscribing the link. Max center spread
5580 * is 2.5%; use 5% for safety's sake.
5582 u32 bps = target_clock * bpp * 21 / 20;
5583 return bps / (link_bw * 8) + 1;
5586 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5588 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5591 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5593 intel_clock_t *reduced_clock, u32 *fp2)
5595 struct drm_crtc *crtc = &intel_crtc->base;
5596 struct drm_device *dev = crtc->dev;
5597 struct drm_i915_private *dev_priv = dev->dev_private;
5598 struct intel_encoder *intel_encoder;
5600 int factor, num_connectors = 0;
5601 bool is_lvds = false, is_sdvo = false;
5603 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5604 switch (intel_encoder->type) {
5605 case INTEL_OUTPUT_LVDS:
5608 case INTEL_OUTPUT_SDVO:
5609 case INTEL_OUTPUT_HDMI:
5617 /* Enable autotuning of the PLL clock (if permissible) */
5620 if ((intel_panel_use_ssc(dev_priv) &&
5621 dev_priv->vbt.lvds_ssc_freq == 100) ||
5622 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5624 } else if (intel_crtc->config.sdvo_tv_clock)
5627 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
5630 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5636 dpll |= DPLLB_MODE_LVDS;
5638 dpll |= DPLLB_MODE_DAC_SERIAL;
5640 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5641 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5644 dpll |= DPLL_DVO_HIGH_SPEED;
5645 if (intel_crtc->config.has_dp_encoder)
5646 dpll |= DPLL_DVO_HIGH_SPEED;
5648 /* compute bitmask from p1 value */
5649 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5651 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5653 switch (intel_crtc->config.dpll.p2) {
5655 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5658 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5661 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5664 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5668 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5669 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5671 dpll |= PLL_REF_INPUT_DREFCLK;
5673 return dpll | DPLL_VCO_ENABLE;
5676 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5678 struct drm_framebuffer *fb)
5680 struct drm_device *dev = crtc->dev;
5681 struct drm_i915_private *dev_priv = dev->dev_private;
5682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5683 int pipe = intel_crtc->pipe;
5684 int plane = intel_crtc->plane;
5685 int num_connectors = 0;
5686 intel_clock_t clock, reduced_clock;
5687 u32 dpll = 0, fp = 0, fp2 = 0;
5688 bool ok, has_reduced_clock = false;
5689 bool is_lvds = false;
5690 struct intel_encoder *encoder;
5691 struct intel_shared_dpll *pll;
5694 for_each_encoder_on_crtc(dev, crtc, encoder) {
5695 switch (encoder->type) {
5696 case INTEL_OUTPUT_LVDS:
5704 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5705 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5707 ok = ironlake_compute_clocks(crtc, &clock,
5708 &has_reduced_clock, &reduced_clock);
5709 if (!ok && !intel_crtc->config.clock_set) {
5710 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5713 /* Compat-code for transition, will disappear. */
5714 if (!intel_crtc->config.clock_set) {
5715 intel_crtc->config.dpll.n = clock.n;
5716 intel_crtc->config.dpll.m1 = clock.m1;
5717 intel_crtc->config.dpll.m2 = clock.m2;
5718 intel_crtc->config.dpll.p1 = clock.p1;
5719 intel_crtc->config.dpll.p2 = clock.p2;
5722 /* Ensure that the cursor is valid for the new mode before changing... */
5723 intel_crtc_update_cursor(crtc, true);
5725 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5726 if (intel_crtc->config.has_pch_encoder) {
5727 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
5728 if (has_reduced_clock)
5729 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
5731 dpll = ironlake_compute_dpll(intel_crtc,
5732 &fp, &reduced_clock,
5733 has_reduced_clock ? &fp2 : NULL);
5735 intel_crtc->config.dpll_hw_state.dpll = dpll;
5736 intel_crtc->config.dpll_hw_state.fp0 = fp;
5737 if (has_reduced_clock)
5738 intel_crtc->config.dpll_hw_state.fp1 = fp2;
5740 intel_crtc->config.dpll_hw_state.fp1 = fp;
5742 pll = intel_get_shared_dpll(intel_crtc, dpll, fp);
5744 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5749 intel_put_shared_dpll(intel_crtc);
5751 if (intel_crtc->config.has_dp_encoder)
5752 intel_dp_set_m_n(intel_crtc);
5754 for_each_encoder_on_crtc(dev, crtc, encoder)
5755 if (encoder->pre_pll_enable)
5756 encoder->pre_pll_enable(encoder);
5758 if (is_lvds && has_reduced_clock && i915_powersave)
5759 intel_crtc->lowfreq_avail = true;
5761 intel_crtc->lowfreq_avail = false;
5763 if (intel_crtc->config.has_pch_encoder) {
5764 pll = intel_crtc_to_shared_dpll(intel_crtc);
5766 I915_WRITE(PCH_DPLL(pll->id), dpll);
5768 /* Wait for the clocks to stabilize. */
5769 POSTING_READ(PCH_DPLL(pll->id));
5772 /* The pixel multiplier can only be updated once the
5773 * DPLL is enabled and the clocks are stable.
5775 * So write it again.
5777 I915_WRITE(PCH_DPLL(pll->id), dpll);
5779 if (has_reduced_clock)
5780 I915_WRITE(PCH_FP1(pll->id), fp2);
5782 I915_WRITE(PCH_FP1(pll->id), fp);
5785 intel_set_pipe_timings(intel_crtc);
5787 if (intel_crtc->config.has_pch_encoder) {
5788 intel_cpu_transcoder_set_m_n(intel_crtc,
5789 &intel_crtc->config.fdi_m_n);
5792 if (IS_IVYBRIDGE(dev))
5793 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
5795 ironlake_set_pipeconf(crtc);
5797 /* Set up the display plane register */
5798 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5799 POSTING_READ(DSPCNTR(plane));
5801 ret = intel_pipe_set_base(crtc, x, y, fb);
5803 intel_update_watermarks(dev);
5808 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5809 struct intel_crtc_config *pipe_config)
5811 struct drm_device *dev = crtc->base.dev;
5812 struct drm_i915_private *dev_priv = dev->dev_private;
5813 enum transcoder transcoder = pipe_config->cpu_transcoder;
5815 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5816 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5817 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5819 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5820 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5821 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5824 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5825 struct intel_crtc_config *pipe_config)
5827 struct drm_device *dev = crtc->base.dev;
5828 struct drm_i915_private *dev_priv = dev->dev_private;
5831 tmp = I915_READ(PF_CTL(crtc->pipe));
5833 if (tmp & PF_ENABLE) {
5834 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5835 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
5837 /* We currently do not free assignements of panel fitters on
5838 * ivb/hsw (since we don't use the higher upscaling modes which
5839 * differentiates them) so just WARN about this case for now. */
5841 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5842 PF_PIPE_SEL_IVB(crtc->pipe));
5847 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5848 struct intel_crtc_config *pipe_config)
5850 struct drm_device *dev = crtc->base.dev;
5851 struct drm_i915_private *dev_priv = dev->dev_private;
5854 pipe_config->cpu_transcoder = crtc->pipe;
5855 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5857 tmp = I915_READ(PIPECONF(crtc->pipe));
5858 if (!(tmp & PIPECONF_ENABLE))
5861 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5862 struct intel_shared_dpll *pll;
5864 pipe_config->has_pch_encoder = true;
5866 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5867 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5868 FDI_DP_PORT_WIDTH_SHIFT) + 1;
5870 ironlake_get_fdi_m_n_config(crtc, pipe_config);
5872 /* XXX: Can't properly read out the pch dpll pixel multiplier
5873 * since we don't have state tracking for pch clocks yet. */
5874 pipe_config->pixel_multiplier = 1;
5876 if (HAS_PCH_IBX(dev_priv->dev)) {
5877 pipe_config->shared_dpll = crtc->pipe;
5879 tmp = I915_READ(PCH_DPLL_SEL);
5880 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5881 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
5883 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
5886 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
5888 WARN_ON(!pll->get_hw_state(dev_priv, pll,
5889 &pipe_config->dpll_hw_state));
5891 pipe_config->pixel_multiplier = 1;
5894 intel_get_pipe_timings(crtc, pipe_config);
5896 ironlake_get_pfit_config(crtc, pipe_config);
5901 static void haswell_modeset_global_resources(struct drm_device *dev)
5903 bool enable = false;
5904 struct intel_crtc *crtc;
5906 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5907 if (!crtc->base.enabled)
5910 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5911 crtc->config.cpu_transcoder != TRANSCODER_EDP)
5915 intel_set_power_well(dev, enable);
5918 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5920 struct drm_framebuffer *fb)
5922 struct drm_device *dev = crtc->dev;
5923 struct drm_i915_private *dev_priv = dev->dev_private;
5924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5925 int plane = intel_crtc->plane;
5928 if (!intel_ddi_pll_mode_set(crtc))
5931 /* Ensure that the cursor is valid for the new mode before changing... */
5932 intel_crtc_update_cursor(crtc, true);
5934 if (intel_crtc->config.has_dp_encoder)
5935 intel_dp_set_m_n(intel_crtc);
5937 intel_crtc->lowfreq_avail = false;
5939 intel_set_pipe_timings(intel_crtc);
5941 if (intel_crtc->config.has_pch_encoder) {
5942 intel_cpu_transcoder_set_m_n(intel_crtc,
5943 &intel_crtc->config.fdi_m_n);
5946 haswell_set_pipeconf(crtc);
5948 intel_set_pipe_csc(crtc);
5950 /* Set up the display plane register */
5951 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
5952 POSTING_READ(DSPCNTR(plane));
5954 ret = intel_pipe_set_base(crtc, x, y, fb);
5956 intel_update_watermarks(dev);
5961 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5962 struct intel_crtc_config *pipe_config)
5964 struct drm_device *dev = crtc->base.dev;
5965 struct drm_i915_private *dev_priv = dev->dev_private;
5966 enum intel_display_power_domain pfit_domain;
5969 pipe_config->cpu_transcoder = crtc->pipe;
5970 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
5972 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5973 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5974 enum i915_pipe trans_edp_pipe;
5975 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5977 WARN(1, "unknown pipe linked to edp transcoder\n");
5978 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5979 case TRANS_DDI_EDP_INPUT_A_ON:
5980 trans_edp_pipe = PIPE_A;
5982 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5983 trans_edp_pipe = PIPE_B;
5985 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5986 trans_edp_pipe = PIPE_C;
5990 if (trans_edp_pipe == crtc->pipe)
5991 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5994 if (!intel_display_power_enabled(dev,
5995 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
5998 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
5999 if (!(tmp & PIPECONF_ENABLE))
6003 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6004 * DDI E. So just check whether this pipe is wired to DDI E and whether
6005 * the PCH transcoder is on.
6007 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6008 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6009 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6010 pipe_config->has_pch_encoder = true;
6012 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6013 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6014 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6016 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6019 intel_get_pipe_timings(crtc, pipe_config);
6021 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6022 if (intel_display_power_enabled(dev, pfit_domain))
6023 ironlake_get_pfit_config(crtc, pipe_config);
6025 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6026 (I915_READ(IPS_CTL) & IPS_ENABLE);
6028 pipe_config->pixel_multiplier = 1;
6033 static int intel_crtc_mode_set(struct drm_crtc *crtc,
6035 struct drm_framebuffer *fb)
6037 struct drm_device *dev = crtc->dev;
6038 struct drm_i915_private *dev_priv = dev->dev_private;
6039 struct drm_encoder_helper_funcs *encoder_funcs;
6040 struct intel_encoder *encoder;
6041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6042 struct drm_display_mode *adjusted_mode =
6043 &intel_crtc->config.adjusted_mode;
6044 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6045 int pipe = intel_crtc->pipe;
6048 drm_vblank_pre_modeset(dev, pipe);
6050 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6052 drm_vblank_post_modeset(dev, pipe);
6057 for_each_encoder_on_crtc(dev, crtc, encoder) {
6058 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6059 encoder->base.base.id,
6060 drm_get_encoder_name(&encoder->base),
6061 mode->base.id, mode->name);
6062 if (encoder->mode_set) {
6063 encoder->mode_set(encoder);
6065 encoder_funcs = encoder->base.helper_private;
6066 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6073 static bool intel_eld_uptodate(struct drm_connector *connector,
6074 int reg_eldv, uint32_t bits_eldv,
6075 int reg_elda, uint32_t bits_elda,
6078 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6079 uint8_t *eld = connector->eld;
6082 i = I915_READ(reg_eldv);
6091 i = I915_READ(reg_elda);
6093 I915_WRITE(reg_elda, i);
6095 for (i = 0; i < eld[2]; i++)
6096 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6102 static void g4x_write_eld(struct drm_connector *connector,
6103 struct drm_crtc *crtc)
6105 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6106 uint8_t *eld = connector->eld;
6111 i = I915_READ(G4X_AUD_VID_DID);
6113 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6114 eldv = G4X_ELDV_DEVCL_DEVBLC;
6116 eldv = G4X_ELDV_DEVCTG;
6118 if (intel_eld_uptodate(connector,
6119 G4X_AUD_CNTL_ST, eldv,
6120 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6121 G4X_HDMIW_HDMIEDID))
6124 i = I915_READ(G4X_AUD_CNTL_ST);
6125 i &= ~(eldv | G4X_ELD_ADDR);
6126 len = (i >> 9) & 0x1f; /* ELD buffer size */
6127 I915_WRITE(G4X_AUD_CNTL_ST, i);
6132 len = min_t(uint8_t, eld[2], len);
6133 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6134 for (i = 0; i < len; i++)
6135 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6137 i = I915_READ(G4X_AUD_CNTL_ST);
6139 I915_WRITE(G4X_AUD_CNTL_ST, i);
6142 static void haswell_write_eld(struct drm_connector *connector,
6143 struct drm_crtc *crtc)
6145 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6146 uint8_t *eld = connector->eld;
6147 struct drm_device *dev = crtc->dev;
6148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6152 int pipe = to_intel_crtc(crtc)->pipe;
6155 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6156 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6157 int aud_config = HSW_AUD_CFG(pipe);
6158 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6161 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6163 /* Audio output enable */
6164 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6165 tmp = I915_READ(aud_cntrl_st2);
6166 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6167 I915_WRITE(aud_cntrl_st2, tmp);
6169 /* Wait for 1 vertical blank */
6170 intel_wait_for_vblank(dev, pipe);
6172 /* Set ELD valid state */
6173 tmp = I915_READ(aud_cntrl_st2);
6174 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6175 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6176 I915_WRITE(aud_cntrl_st2, tmp);
6177 tmp = I915_READ(aud_cntrl_st2);
6178 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6180 /* Enable HDMI mode */
6181 tmp = I915_READ(aud_config);
6182 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6183 /* clear N_programing_enable and N_value_index */
6184 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6185 I915_WRITE(aud_config, tmp);
6187 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6189 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6190 intel_crtc->eld_vld = true;
6192 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6193 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6194 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6195 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6197 I915_WRITE(aud_config, 0);
6199 if (intel_eld_uptodate(connector,
6200 aud_cntrl_st2, eldv,
6201 aud_cntl_st, IBX_ELD_ADDRESS,
6205 i = I915_READ(aud_cntrl_st2);
6207 I915_WRITE(aud_cntrl_st2, i);
6212 i = I915_READ(aud_cntl_st);
6213 i &= ~IBX_ELD_ADDRESS;
6214 I915_WRITE(aud_cntl_st, i);
6215 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6216 DRM_DEBUG_DRIVER("port num:%d\n", i);
6218 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6219 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6220 for (i = 0; i < len; i++)
6221 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6223 i = I915_READ(aud_cntrl_st2);
6225 I915_WRITE(aud_cntrl_st2, i);
6229 static void ironlake_write_eld(struct drm_connector *connector,
6230 struct drm_crtc *crtc)
6232 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6233 uint8_t *eld = connector->eld;
6241 int pipe = to_intel_crtc(crtc)->pipe;
6243 if (HAS_PCH_IBX(connector->dev)) {
6244 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6245 aud_config = IBX_AUD_CFG(pipe);
6246 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6247 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6249 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6250 aud_config = CPT_AUD_CFG(pipe);
6251 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6252 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6255 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6257 i = I915_READ(aud_cntl_st);
6258 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6260 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6261 /* operate blindly on all ports */
6262 eldv = IBX_ELD_VALIDB;
6263 eldv |= IBX_ELD_VALIDB << 4;
6264 eldv |= IBX_ELD_VALIDB << 8;
6266 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6267 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6270 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6271 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6272 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6273 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6275 I915_WRITE(aud_config, 0);
6277 if (intel_eld_uptodate(connector,
6278 aud_cntrl_st2, eldv,
6279 aud_cntl_st, IBX_ELD_ADDRESS,
6283 i = I915_READ(aud_cntrl_st2);
6285 I915_WRITE(aud_cntrl_st2, i);
6290 i = I915_READ(aud_cntl_st);
6291 i &= ~IBX_ELD_ADDRESS;
6292 I915_WRITE(aud_cntl_st, i);
6294 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6295 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6296 for (i = 0; i < len; i++)
6297 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6299 i = I915_READ(aud_cntrl_st2);
6301 I915_WRITE(aud_cntrl_st2, i);
6304 void intel_write_eld(struct drm_encoder *encoder,
6305 struct drm_display_mode *mode)
6307 struct drm_crtc *crtc = encoder->crtc;
6308 struct drm_connector *connector;
6309 struct drm_device *dev = encoder->dev;
6310 struct drm_i915_private *dev_priv = dev->dev_private;
6312 connector = drm_select_eld(encoder, mode);
6316 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6318 drm_get_connector_name(connector),
6319 connector->encoder->base.id,
6320 drm_get_encoder_name(connector->encoder));
6322 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6324 if (dev_priv->display.write_eld)
6325 dev_priv->display.write_eld(connector, crtc);
6328 /** Loads the palette/gamma unit for the CRTC with the prepared values */
6329 void intel_crtc_load_lut(struct drm_crtc *crtc)
6331 struct drm_device *dev = crtc->dev;
6332 struct drm_i915_private *dev_priv = dev->dev_private;
6333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6334 enum i915_pipe pipe = intel_crtc->pipe;
6335 int palreg = PALETTE(pipe);
6337 bool reenable_ips = false;
6339 /* The clocks have to be on to load the palette. */
6340 if (!crtc->enabled || !intel_crtc->active)
6343 if (!HAS_PCH_SPLIT(dev_priv->dev))
6344 assert_pll_enabled(dev_priv, pipe);
6346 /* use legacy palette for Ironlake */
6347 if (HAS_PCH_SPLIT(dev))
6348 palreg = LGC_PALETTE(pipe);
6350 /* Workaround : Do not read or write the pipe palette/gamma data while
6351 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6353 if (intel_crtc->config.ips_enabled &&
6354 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6355 GAMMA_MODE_MODE_SPLIT)) {
6356 hsw_disable_ips(intel_crtc);
6357 reenable_ips = true;
6360 for (i = 0; i < 256; i++) {
6361 I915_WRITE(palreg + 4 * i,
6362 (intel_crtc->lut_r[i] << 16) |
6363 (intel_crtc->lut_g[i] << 8) |
6364 intel_crtc->lut_b[i]);
6368 hsw_enable_ips(intel_crtc);
6371 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6373 struct drm_device *dev = crtc->dev;
6374 struct drm_i915_private *dev_priv = dev->dev_private;
6375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6376 bool visible = base != 0;
6379 if (intel_crtc->cursor_visible == visible)
6382 cntl = I915_READ(_CURACNTR);
6384 /* On these chipsets we can only modify the base whilst
6385 * the cursor is disabled.
6387 I915_WRITE(_CURABASE, base);
6389 cntl &= ~(CURSOR_FORMAT_MASK);
6390 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6391 cntl |= CURSOR_ENABLE |
6392 CURSOR_GAMMA_ENABLE |
6395 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6396 I915_WRITE(_CURACNTR, cntl);
6398 intel_crtc->cursor_visible = visible;
6401 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6403 struct drm_device *dev = crtc->dev;
6404 struct drm_i915_private *dev_priv = dev->dev_private;
6405 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6406 int pipe = intel_crtc->pipe;
6407 bool visible = base != 0;
6409 if (intel_crtc->cursor_visible != visible) {
6410 uint32_t cntl = I915_READ(CURCNTR(pipe));
6412 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6413 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6414 cntl |= pipe << 28; /* Connect to correct pipe */
6416 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6417 cntl |= CURSOR_MODE_DISABLE;
6419 I915_WRITE(CURCNTR(pipe), cntl);
6421 intel_crtc->cursor_visible = visible;
6423 /* and commit changes on next vblank */
6424 I915_WRITE(CURBASE(pipe), base);
6427 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6429 struct drm_device *dev = crtc->dev;
6430 struct drm_i915_private *dev_priv = dev->dev_private;
6431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6432 int pipe = intel_crtc->pipe;
6433 bool visible = base != 0;
6435 if (intel_crtc->cursor_visible != visible) {
6436 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6438 cntl &= ~CURSOR_MODE;
6439 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6441 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6442 cntl |= CURSOR_MODE_DISABLE;
6444 if (IS_HASWELL(dev))
6445 cntl |= CURSOR_PIPE_CSC_ENABLE;
6446 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6448 intel_crtc->cursor_visible = visible;
6450 /* and commit changes on next vblank */
6451 I915_WRITE(CURBASE_IVB(pipe), base);
6454 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6455 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6458 struct drm_device *dev = crtc->dev;
6459 struct drm_i915_private *dev_priv = dev->dev_private;
6460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6461 int pipe = intel_crtc->pipe;
6462 int x = intel_crtc->cursor_x;
6463 int y = intel_crtc->cursor_y;
6469 if (on && crtc->enabled && crtc->fb) {
6470 base = intel_crtc->cursor_addr;
6471 if (x > (int) crtc->fb->width)
6474 if (y > (int) crtc->fb->height)
6480 if (x + intel_crtc->cursor_width < 0)
6483 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6486 pos |= x << CURSOR_X_SHIFT;
6489 if (y + intel_crtc->cursor_height < 0)
6492 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6495 pos |= y << CURSOR_Y_SHIFT;
6497 visible = base != 0;
6498 if (!visible && !intel_crtc->cursor_visible)
6501 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6502 I915_WRITE(CURPOS_IVB(pipe), pos);
6503 ivb_update_cursor(crtc, base);
6505 I915_WRITE(CURPOS(pipe), pos);
6506 if (IS_845G(dev) || IS_I865G(dev))
6507 i845_update_cursor(crtc, base);
6509 i9xx_update_cursor(crtc, base);
6513 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6514 struct drm_file *file,
6516 uint32_t width, uint32_t height)
6518 struct drm_device *dev = crtc->dev;
6519 struct drm_i915_private *dev_priv = dev->dev_private;
6520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6521 struct drm_i915_gem_object *obj;
6525 /* if we want to turn off the cursor ignore width and height */
6527 DRM_DEBUG_KMS("cursor off\n");
6530 mutex_lock(&dev->struct_mutex);
6534 /* Currently we only support 64x64 cursors */
6535 if (width != 64 || height != 64) {
6536 DRM_ERROR("we currently only support 64x64 cursors\n");
6540 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6541 if (&obj->base == NULL)
6544 if (obj->base.size < width * height * 4) {
6545 DRM_ERROR("buffer is to small\n");
6550 /* we only need to pin inside GTT if cursor is non-phy */
6551 mutex_lock(&dev->struct_mutex);
6552 if (!dev_priv->info->cursor_needs_physical) {
6555 if (obj->tiling_mode) {
6556 DRM_ERROR("cursor cannot be tiled\n");
6561 /* Note that the w/a also requires 2 PTE of padding following
6562 * the bo. We currently fill all unused PTE with the shadow
6563 * page and so we should always have valid PTE following the
6564 * cursor preventing the VT-d warning.
6567 if (need_vtd_wa(dev))
6568 alignment = 64*1024;
6570 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6572 DRM_ERROR("failed to move cursor bo into the GTT\n");
6576 ret = i915_gem_object_put_fence(obj);
6578 DRM_ERROR("failed to release fence for cursor");
6582 addr = obj->gtt_offset;
6584 int align = IS_I830(dev) ? 16 * 1024 : 256;
6585 ret = i915_gem_attach_phys_object(dev, obj,
6586 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6589 DRM_ERROR("failed to attach phys object\n");
6592 addr = obj->phys_obj->handle->busaddr;
6596 I915_WRITE(CURSIZE, (height << 12) | width);
6599 if (intel_crtc->cursor_bo) {
6600 if (dev_priv->info->cursor_needs_physical) {
6601 if (intel_crtc->cursor_bo != obj)
6602 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6604 i915_gem_object_unpin(intel_crtc->cursor_bo);
6605 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6608 mutex_unlock(&dev->struct_mutex);
6610 intel_crtc->cursor_addr = addr;
6611 intel_crtc->cursor_bo = obj;
6612 intel_crtc->cursor_width = width;
6613 intel_crtc->cursor_height = height;
6615 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6619 i915_gem_object_unpin(obj);
6621 mutex_unlock(&dev->struct_mutex);
6623 drm_gem_object_unreference_unlocked(&obj->base);
6627 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6631 intel_crtc->cursor_x = x;
6632 intel_crtc->cursor_y = y;
6634 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6639 /** Sets the color ramps on behalf of RandR */
6640 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6641 u16 blue, int regno)
6643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6645 intel_crtc->lut_r[regno] = red >> 8;
6646 intel_crtc->lut_g[regno] = green >> 8;
6647 intel_crtc->lut_b[regno] = blue >> 8;
6650 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6651 u16 *blue, int regno)
6653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6655 *red = intel_crtc->lut_r[regno] << 8;
6656 *green = intel_crtc->lut_g[regno] << 8;
6657 *blue = intel_crtc->lut_b[regno] << 8;
6660 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
6661 u16 *blue, uint32_t start, uint32_t size)
6663 int end = (start + size > 256) ? 256 : start + size, i;
6664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6666 for (i = start; i < end; i++) {
6667 intel_crtc->lut_r[i] = red[i] >> 8;
6668 intel_crtc->lut_g[i] = green[i] >> 8;
6669 intel_crtc->lut_b[i] = blue[i] >> 8;
6672 intel_crtc_load_lut(crtc);
6675 /* VESA 640x480x72Hz mode to set on the pipe */
6676 static struct drm_display_mode load_detect_mode = {
6677 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6678 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6681 static struct drm_framebuffer *
6682 intel_framebuffer_create(struct drm_device *dev,
6683 struct drm_mode_fb_cmd2 *mode_cmd,
6684 struct drm_i915_gem_object *obj)
6686 struct intel_framebuffer *intel_fb;
6689 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6691 drm_gem_object_unreference_unlocked(&obj->base);
6692 return ERR_PTR(-ENOMEM);
6695 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6697 drm_gem_object_unreference_unlocked(&obj->base);
6699 return ERR_PTR(ret);
6702 return &intel_fb->base;
6706 intel_framebuffer_pitch_for_width(int width, int bpp)
6708 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6709 return ALIGN(pitch, 64);
6713 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6715 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6716 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6719 static struct drm_framebuffer *
6720 intel_framebuffer_create_for_mode(struct drm_device *dev,
6721 struct drm_display_mode *mode,
6724 struct drm_i915_gem_object *obj;
6725 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
6727 obj = i915_gem_alloc_object(dev,
6728 intel_framebuffer_size_for_mode(mode, bpp));
6730 return ERR_PTR(-ENOMEM);
6732 mode_cmd.width = mode->hdisplay;
6733 mode_cmd.height = mode->vdisplay;
6734 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6736 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
6738 return intel_framebuffer_create(dev, &mode_cmd, obj);
6741 static struct drm_framebuffer *
6742 mode_fits_in_fbdev(struct drm_device *dev,
6743 struct drm_display_mode *mode)
6745 struct drm_i915_private *dev_priv = dev->dev_private;
6746 struct drm_i915_gem_object *obj;
6747 struct drm_framebuffer *fb;
6749 if (dev_priv->fbdev == NULL)
6752 obj = dev_priv->fbdev->ifb.obj;
6756 fb = &dev_priv->fbdev->ifb.base;
6757 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6758 fb->bits_per_pixel))
6761 if (obj->base.size < mode->vdisplay * fb->pitches[0])
6767 bool intel_get_load_detect_pipe(struct drm_connector *connector,
6768 struct drm_display_mode *mode,
6769 struct intel_load_detect_pipe *old)
6771 struct intel_crtc *intel_crtc;
6772 struct intel_encoder *intel_encoder =
6773 intel_attached_encoder(connector);
6774 struct drm_crtc *possible_crtc;
6775 struct drm_encoder *encoder = &intel_encoder->base;
6776 struct drm_crtc *crtc = NULL;
6777 struct drm_device *dev = encoder->dev;
6778 struct drm_framebuffer *fb;
6781 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6782 connector->base.id, drm_get_connector_name(connector),
6783 encoder->base.id, drm_get_encoder_name(encoder));
6786 * Algorithm gets a little messy:
6788 * - if the connector already has an assigned crtc, use it (but make
6789 * sure it's on first)
6791 * - try to find the first unused crtc that can drive this connector,
6792 * and use that if we find one
6795 /* See if we already have a CRTC for this connector */
6796 if (encoder->crtc) {
6797 crtc = encoder->crtc;
6799 mutex_lock(&crtc->mutex);
6801 old->dpms_mode = connector->dpms;
6802 old->load_detect_temp = false;
6804 /* Make sure the crtc and connector are running */
6805 if (connector->dpms != DRM_MODE_DPMS_ON)
6806 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
6811 /* Find an unused one (if possible) */
6812 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6814 if (!(encoder->possible_crtcs & (1 << i)))
6816 if (!possible_crtc->enabled) {
6817 crtc = possible_crtc;
6823 * If we didn't find an unused CRTC, don't use any.
6826 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6830 mutex_lock(&crtc->mutex);
6831 intel_encoder->new_crtc = to_intel_crtc(crtc);
6832 to_intel_connector(connector)->new_encoder = intel_encoder;
6834 intel_crtc = to_intel_crtc(crtc);
6835 old->dpms_mode = connector->dpms;
6836 old->load_detect_temp = true;
6837 old->release_fb = NULL;
6840 mode = &load_detect_mode;
6842 /* We need a framebuffer large enough to accommodate all accesses
6843 * that the plane may generate whilst we perform load detection.
6844 * We can not rely on the fbcon either being present (we get called
6845 * during its initialisation to detect all boot displays, or it may
6846 * not even exist) or that it is large enough to satisfy the
6849 fb = mode_fits_in_fbdev(dev, mode);
6851 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6852 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6853 old->release_fb = fb;
6855 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6857 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6858 mutex_unlock(&crtc->mutex);
6862 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6863 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
6864 if (old->release_fb)
6865 old->release_fb->funcs->destroy(old->release_fb);
6866 mutex_unlock(&crtc->mutex);
6870 /* let the connector get through one full cycle before testing */
6871 intel_wait_for_vblank(dev, intel_crtc->pipe);
6875 void intel_release_load_detect_pipe(struct drm_connector *connector,
6876 struct intel_load_detect_pipe *old)
6878 struct intel_encoder *intel_encoder =
6879 intel_attached_encoder(connector);
6880 struct drm_encoder *encoder = &intel_encoder->base;
6881 struct drm_crtc *crtc = encoder->crtc;
6883 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6884 connector->base.id, drm_get_connector_name(connector),
6885 encoder->base.id, drm_get_encoder_name(encoder));
6887 if (old->load_detect_temp) {
6888 to_intel_connector(connector)->new_encoder = NULL;
6889 intel_encoder->new_crtc = NULL;
6890 intel_set_mode(crtc, NULL, 0, 0, NULL);
6892 if (old->release_fb) {
6893 drm_framebuffer_unregister_private(old->release_fb);
6894 drm_framebuffer_unreference(old->release_fb);
6897 mutex_unlock(&crtc->mutex);
6901 /* Switch crtc and encoder back off if necessary */
6902 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6903 connector->funcs->dpms(connector, old->dpms_mode);
6905 mutex_unlock(&crtc->mutex);
6908 /* Returns the clock of the currently programmed mode of the given pipe. */
6909 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6911 struct drm_i915_private *dev_priv = dev->dev_private;
6912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6913 int pipe = intel_crtc->pipe;
6914 u32 dpll = I915_READ(DPLL(pipe));
6916 intel_clock_t clock;
6918 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6919 fp = I915_READ(FP0(pipe));
6921 fp = I915_READ(FP1(pipe));
6923 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6924 if (IS_PINEVIEW(dev)) {
6925 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6926 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6928 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6929 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6932 if (!IS_GEN2(dev)) {
6933 if (IS_PINEVIEW(dev))
6934 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6935 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6937 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6938 DPLL_FPA01_P1_POST_DIV_SHIFT);
6940 switch (dpll & DPLL_MODE_MASK) {
6941 case DPLLB_MODE_DAC_SERIAL:
6942 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6945 case DPLLB_MODE_LVDS:
6946 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6950 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
6951 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6955 if (IS_PINEVIEW(dev))
6956 pineview_clock(96000, &clock);
6958 i9xx_clock(96000, &clock);
6960 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6963 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6964 DPLL_FPA01_P1_POST_DIV_SHIFT);
6967 if ((dpll & PLL_REF_INPUT_MASK) ==
6968 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6969 /* XXX: might not be 66MHz */
6970 i9xx_clock(66000, &clock);
6972 i9xx_clock(48000, &clock);
6974 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6977 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6978 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6980 if (dpll & PLL_P2_DIVIDE_BY_4)
6985 i9xx_clock(48000, &clock);
6989 /* XXX: It would be nice to validate the clocks, but we can't reuse
6990 * i830PllIsValid() because it relies on the xf86_config connector
6991 * configuration being accurate, which it isn't necessarily.
6997 /** Returns the currently programmed mode of the given pipe. */
6998 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6999 struct drm_crtc *crtc)
7001 struct drm_i915_private *dev_priv = dev->dev_private;
7002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7003 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
7004 struct drm_display_mode *mode;
7005 int htot = I915_READ(HTOTAL(cpu_transcoder));
7006 int hsync = I915_READ(HSYNC(cpu_transcoder));
7007 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7008 int vsync = I915_READ(VSYNC(cpu_transcoder));
7010 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7014 mode->clock = intel_crtc_clock_get(dev, crtc);
7015 mode->hdisplay = (htot & 0xffff) + 1;
7016 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7017 mode->hsync_start = (hsync & 0xffff) + 1;
7018 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7019 mode->vdisplay = (vtot & 0xffff) + 1;
7020 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7021 mode->vsync_start = (vsync & 0xffff) + 1;
7022 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7024 drm_mode_set_name(mode);
7029 static void intel_increase_pllclock(struct drm_crtc *crtc)
7031 struct drm_device *dev = crtc->dev;
7032 drm_i915_private_t *dev_priv = dev->dev_private;
7033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7034 int pipe = intel_crtc->pipe;
7035 int dpll_reg = DPLL(pipe);
7038 if (HAS_PCH_SPLIT(dev))
7041 if (!dev_priv->lvds_downclock_avail)
7044 dpll = I915_READ(dpll_reg);
7045 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
7046 DRM_DEBUG_DRIVER("upclocking LVDS\n");
7048 assert_panel_unlocked(dev_priv, pipe);
7050 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7051 I915_WRITE(dpll_reg, dpll);
7052 intel_wait_for_vblank(dev, pipe);
7054 dpll = I915_READ(dpll_reg);
7055 if (dpll & DISPLAY_RATE_SELECT_FPA1)
7056 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
7060 static void intel_decrease_pllclock(struct drm_crtc *crtc)
7062 struct drm_device *dev = crtc->dev;
7063 drm_i915_private_t *dev_priv = dev->dev_private;
7064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7066 if (HAS_PCH_SPLIT(dev))
7069 if (!dev_priv->lvds_downclock_avail)
7073 * Since this is called by a timer, we should never get here in
7076 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7077 int pipe = intel_crtc->pipe;
7078 int dpll_reg = DPLL(pipe);
7081 DRM_DEBUG_DRIVER("downclocking LVDS\n");
7083 assert_panel_unlocked(dev_priv, pipe);
7085 dpll = I915_READ(dpll_reg);
7086 dpll |= DISPLAY_RATE_SELECT_FPA1;
7087 I915_WRITE(dpll_reg, dpll);
7088 intel_wait_for_vblank(dev, pipe);
7089 dpll = I915_READ(dpll_reg);
7090 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7091 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7096 void intel_mark_busy(struct drm_device *dev)
7098 i915_update_gfx_val(dev->dev_private);
7101 void intel_mark_idle(struct drm_device *dev)
7103 struct drm_crtc *crtc;
7105 if (!i915_powersave)
7108 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7112 intel_decrease_pllclock(crtc);
7116 void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7117 struct intel_ring_buffer *ring)
7119 struct drm_device *dev = obj->base.dev;
7120 struct drm_crtc *crtc;
7122 if (!i915_powersave)
7125 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7129 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7132 intel_increase_pllclock(crtc);
7133 if (ring && intel_fbc_enabled(dev))
7134 ring->fbc_dirty = true;
7138 static void intel_crtc_destroy(struct drm_crtc *crtc)
7140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7141 struct drm_device *dev = crtc->dev;
7142 struct intel_unpin_work *work;
7144 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
7145 work = intel_crtc->unpin_work;
7146 intel_crtc->unpin_work = NULL;
7147 lockmgr(&dev->event_lock, LK_RELEASE);
7150 cancel_work_sync(&work->work);
7154 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7156 drm_crtc_cleanup(crtc);
7161 static void intel_unpin_work_fn(struct work_struct *__work)
7163 struct intel_unpin_work *work =
7164 container_of(__work, struct intel_unpin_work, work);
7165 struct drm_device *dev = work->crtc->dev;
7167 mutex_lock(&dev->struct_mutex);
7168 intel_unpin_fb_obj(work->old_fb_obj);
7169 drm_gem_object_unreference(&work->pending_flip_obj->base);
7170 drm_gem_object_unreference(&work->old_fb_obj->base);
7172 intel_update_fbc(dev);
7173 mutex_unlock(&dev->struct_mutex);
7175 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7176 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7181 static void do_intel_finish_page_flip(struct drm_device *dev,
7182 struct drm_crtc *crtc)
7184 drm_i915_private_t *dev_priv = dev->dev_private;
7185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7186 struct intel_unpin_work *work;
7188 /* Ignore early vblank irqs */
7189 if (intel_crtc == NULL)
7192 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
7193 work = intel_crtc->unpin_work;
7195 /* Ensure we don't miss a work->pending update ... */
7198 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7199 lockmgr(&dev->event_lock, LK_RELEASE);
7203 /* and that the unpin work is consistent wrt ->pending. */
7206 intel_crtc->unpin_work = NULL;
7209 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7211 drm_vblank_put(dev, intel_crtc->pipe);
7213 lockmgr(&dev->event_lock, LK_RELEASE);
7215 wake_up_all(&dev_priv->pending_flip_queue);
7217 queue_work(dev_priv->wq, &work->work);
7219 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7222 void intel_finish_page_flip(struct drm_device *dev, int pipe)
7224 drm_i915_private_t *dev_priv = dev->dev_private;
7225 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7227 do_intel_finish_page_flip(dev, crtc);
7230 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7232 drm_i915_private_t *dev_priv = dev->dev_private;
7233 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7235 do_intel_finish_page_flip(dev, crtc);
7238 void intel_prepare_page_flip(struct drm_device *dev, int plane)
7240 drm_i915_private_t *dev_priv = dev->dev_private;
7241 struct intel_crtc *intel_crtc =
7242 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7244 /* NB: An MMIO update of the plane base pointer will also
7245 * generate a page-flip completion irq, i.e. every modeset
7246 * is also accompanied by a spurious intel_prepare_page_flip().
7248 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
7249 if (intel_crtc->unpin_work)
7250 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7251 lockmgr(&dev->event_lock, LK_RELEASE);
7254 inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7256 /* Ensure that the work item is consistent when activating it ... */
7258 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7259 /* and that it is marked active as soon as the irq could fire. */
7263 static int intel_gen2_queue_flip(struct drm_device *dev,
7264 struct drm_crtc *crtc,
7265 struct drm_framebuffer *fb,
7266 struct drm_i915_gem_object *obj)
7268 struct drm_i915_private *dev_priv = dev->dev_private;
7269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7271 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7274 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7278 ret = intel_ring_begin(ring, 6);
7282 /* Can't queue multiple flips, so wait for the previous
7283 * one to finish before executing the next.
7285 if (intel_crtc->plane)
7286 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7288 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7289 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7290 intel_ring_emit(ring, MI_NOOP);
7291 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7292 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7293 intel_ring_emit(ring, fb->pitches[0]);
7294 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7295 intel_ring_emit(ring, 0); /* aux display base address, unused */
7297 intel_mark_page_flip_active(intel_crtc);
7298 intel_ring_advance(ring);
7302 intel_unpin_fb_obj(obj);
7307 static int intel_gen3_queue_flip(struct drm_device *dev,
7308 struct drm_crtc *crtc,
7309 struct drm_framebuffer *fb,
7310 struct drm_i915_gem_object *obj)
7312 struct drm_i915_private *dev_priv = dev->dev_private;
7313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7315 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7318 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7322 ret = intel_ring_begin(ring, 6);
7326 if (intel_crtc->plane)
7327 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7329 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7330 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7331 intel_ring_emit(ring, MI_NOOP);
7332 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7333 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7334 intel_ring_emit(ring, fb->pitches[0]);
7335 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7336 intel_ring_emit(ring, MI_NOOP);
7338 intel_mark_page_flip_active(intel_crtc);
7339 intel_ring_advance(ring);
7343 intel_unpin_fb_obj(obj);
7348 static int intel_gen4_queue_flip(struct drm_device *dev,
7349 struct drm_crtc *crtc,
7350 struct drm_framebuffer *fb,
7351 struct drm_i915_gem_object *obj)
7353 struct drm_i915_private *dev_priv = dev->dev_private;
7354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7355 uint32_t pf, pipesrc;
7356 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7359 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7363 ret = intel_ring_begin(ring, 4);
7367 /* i965+ uses the linear or tiled offsets from the
7368 * Display Registers (which do not change across a page-flip)
7369 * so we need only reprogram the base address.
7371 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7372 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7373 intel_ring_emit(ring, fb->pitches[0]);
7374 intel_ring_emit(ring,
7375 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7378 /* XXX Enabling the panel-fitter across page-flip is so far
7379 * untested on non-native modes, so ignore it for now.
7380 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7383 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7384 intel_ring_emit(ring, pf | pipesrc);
7386 intel_mark_page_flip_active(intel_crtc);
7387 intel_ring_advance(ring);
7391 intel_unpin_fb_obj(obj);
7396 static int intel_gen6_queue_flip(struct drm_device *dev,
7397 struct drm_crtc *crtc,
7398 struct drm_framebuffer *fb,
7399 struct drm_i915_gem_object *obj)
7401 struct drm_i915_private *dev_priv = dev->dev_private;
7402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7403 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7404 uint32_t pf, pipesrc;
7407 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7411 ret = intel_ring_begin(ring, 4);
7415 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7416 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7417 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7418 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7420 /* Contrary to the suggestions in the documentation,
7421 * "Enable Panel Fitter" does not seem to be required when page
7422 * flipping with a non-native mode, and worse causes a normal
7424 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7427 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7428 intel_ring_emit(ring, pf | pipesrc);
7430 intel_mark_page_flip_active(intel_crtc);
7431 intel_ring_advance(ring);
7435 intel_unpin_fb_obj(obj);
7441 * On gen7 we currently use the blit ring because (in early silicon at least)
7442 * the render ring doesn't give us interrpts for page flip completion, which
7443 * means clients will hang after the first flip is queued. Fortunately the
7444 * blit ring generates interrupts properly, so use it instead.
7446 static int intel_gen7_queue_flip(struct drm_device *dev,
7447 struct drm_crtc *crtc,
7448 struct drm_framebuffer *fb,
7449 struct drm_i915_gem_object *obj)
7451 struct drm_i915_private *dev_priv = dev->dev_private;
7452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7453 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7454 uint32_t plane_bit = 0;
7457 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7461 switch(intel_crtc->plane) {
7463 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7466 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7469 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7472 WARN_ONCE(1, "unknown plane in flip command\n");
7477 ret = intel_ring_begin(ring, 4);
7481 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7482 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7483 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7484 intel_ring_emit(ring, (MI_NOOP));
7486 intel_mark_page_flip_active(intel_crtc);
7487 intel_ring_advance(ring);
7491 intel_unpin_fb_obj(obj);
7496 static int intel_default_queue_flip(struct drm_device *dev,
7497 struct drm_crtc *crtc,
7498 struct drm_framebuffer *fb,
7499 struct drm_i915_gem_object *obj)
7504 static int intel_crtc_page_flip(struct drm_crtc *crtc,
7505 struct drm_framebuffer *fb,
7506 struct drm_pending_vblank_event *event)
7508 struct drm_device *dev = crtc->dev;
7509 struct drm_i915_private *dev_priv = dev->dev_private;
7510 struct drm_framebuffer *old_fb = crtc->fb;
7511 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7513 struct intel_unpin_work *work;
7516 /* Can't change pixel format via MI display flips. */
7517 if (fb->pixel_format != crtc->fb->pixel_format)
7521 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7522 * Note that pitch changes could also affect these register.
7524 if (INTEL_INFO(dev)->gen > 3 &&
7525 (fb->offsets[0] != crtc->fb->offsets[0] ||
7526 fb->pitches[0] != crtc->fb->pitches[0]))
7529 work = kzalloc(sizeof *work, GFP_KERNEL);
7533 work->event = event;
7535 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
7536 INIT_WORK(&work->work, intel_unpin_work_fn);
7538 ret = drm_vblank_get(dev, intel_crtc->pipe);
7542 /* We borrow the event spin lock for protecting unpin_work */
7543 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
7544 if (intel_crtc->unpin_work) {
7545 lockmgr(&dev->event_lock, LK_RELEASE);
7547 drm_vblank_put(dev, intel_crtc->pipe);
7549 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
7552 intel_crtc->unpin_work = work;
7553 lockmgr(&dev->event_lock, LK_RELEASE);
7555 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7556 flush_workqueue(dev_priv->wq);
7558 ret = i915_mutex_lock_interruptible(dev);
7562 /* Reference the objects for the scheduled work. */
7563 drm_gem_object_reference(&work->old_fb_obj->base);
7564 drm_gem_object_reference(&obj->base);
7568 work->pending_flip_obj = obj;
7570 work->enable_stall_check = true;
7572 atomic_inc(&intel_crtc->unpin_work_count);
7573 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
7575 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7577 goto cleanup_pending;
7579 intel_disable_fbc(dev);
7580 intel_mark_fb_busy(obj, NULL);
7581 mutex_unlock(&dev->struct_mutex);
7583 trace_i915_flip_request(intel_crtc->plane, obj);
7588 atomic_dec(&intel_crtc->unpin_work_count);
7590 drm_gem_object_unreference(&work->old_fb_obj->base);
7591 drm_gem_object_unreference(&obj->base);
7592 mutex_unlock(&dev->struct_mutex);
7595 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
7596 intel_crtc->unpin_work = NULL;
7597 lockmgr(&dev->event_lock, LK_RELEASE);
7599 drm_vblank_put(dev, intel_crtc->pipe);
7606 static struct drm_crtc_helper_funcs intel_helper_funcs = {
7607 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7608 .load_lut = intel_crtc_load_lut,
7611 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7612 struct drm_crtc *crtc)
7614 struct drm_device *dev;
7615 struct drm_crtc *tmp;
7618 WARN(!crtc, "checking null crtc?\n");
7619 /* profmakx: this is to prevent the kernel from panicing */
7626 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7632 if (encoder->possible_crtcs & crtc_mask)
7638 * intel_modeset_update_staged_output_state
7640 * Updates the staged output configuration state, e.g. after we've read out the
7643 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7645 struct intel_encoder *encoder;
7646 struct intel_connector *connector;
7648 list_for_each_entry(connector, &dev->mode_config.connector_list,
7650 connector->new_encoder =
7651 to_intel_encoder(connector->base.encoder);
7654 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7657 to_intel_crtc(encoder->base.crtc);
7662 * intel_modeset_commit_output_state
7664 * This function copies the stage display pipe configuration to the real one.
7666 static void intel_modeset_commit_output_state(struct drm_device *dev)
7668 struct intel_encoder *encoder;
7669 struct intel_connector *connector;
7671 list_for_each_entry(connector, &dev->mode_config.connector_list,
7673 connector->base.encoder = &connector->new_encoder->base;
7676 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7678 encoder->base.crtc = &encoder->new_crtc->base;
7683 connected_sink_compute_bpp(struct intel_connector * connector,
7684 struct intel_crtc_config *pipe_config)
7686 int bpp = pipe_config->pipe_bpp;
7688 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7689 connector->base.base.id,
7690 drm_get_connector_name(&connector->base));
7692 /* Don't use an invalid EDID bpc value */
7693 if (connector->base.display_info.bpc &&
7694 connector->base.display_info.bpc * 3 < bpp) {
7695 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7696 bpp, connector->base.display_info.bpc*3);
7697 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7700 /* Clamp bpp to 8 on screens without EDID 1.4 */
7701 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7702 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7704 pipe_config->pipe_bpp = 24;
7709 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7710 struct drm_framebuffer *fb,
7711 struct intel_crtc_config *pipe_config)
7713 struct drm_device *dev = crtc->base.dev;
7714 struct intel_connector *connector;
7717 switch (fb->pixel_format) {
7719 bpp = 8*3; /* since we go through a colormap */
7721 case DRM_FORMAT_XRGB1555:
7722 case DRM_FORMAT_ARGB1555:
7723 /* checked in intel_framebuffer_init already */
7724 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7726 case DRM_FORMAT_RGB565:
7727 bpp = 6*3; /* min is 18bpp */
7729 case DRM_FORMAT_XBGR8888:
7730 case DRM_FORMAT_ABGR8888:
7731 /* checked in intel_framebuffer_init already */
7732 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7734 case DRM_FORMAT_XRGB8888:
7735 case DRM_FORMAT_ARGB8888:
7738 case DRM_FORMAT_XRGB2101010:
7739 case DRM_FORMAT_ARGB2101010:
7740 case DRM_FORMAT_XBGR2101010:
7741 case DRM_FORMAT_ABGR2101010:
7742 /* checked in intel_framebuffer_init already */
7743 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7747 /* TODO: gen4+ supports 16 bpc floating point, too. */
7749 DRM_DEBUG_KMS("unsupported depth\n");
7753 pipe_config->pipe_bpp = bpp;
7755 /* Clamp display bpp to EDID value */
7756 list_for_each_entry(connector, &dev->mode_config.connector_list,
7758 if (!connector->new_encoder ||
7759 connector->new_encoder->new_crtc != crtc)
7762 connected_sink_compute_bpp(connector, pipe_config);
7768 static void intel_dump_pipe_config(struct intel_crtc *crtc,
7769 struct intel_crtc_config *pipe_config,
7770 const char *context)
7772 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7773 context, pipe_name(crtc->pipe));
7775 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7776 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7777 pipe_config->pipe_bpp, pipe_config->dither);
7778 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7779 pipe_config->has_pch_encoder,
7780 pipe_config->fdi_lanes,
7781 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7782 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7783 pipe_config->fdi_m_n.tu);
7784 DRM_DEBUG_KMS("requested mode:\n");
7785 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7786 DRM_DEBUG_KMS("adjusted mode:\n");
7787 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7788 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7789 pipe_config->gmch_pfit.control,
7790 pipe_config->gmch_pfit.pgm_ratios,
7791 pipe_config->gmch_pfit.lvds_border_bits);
7792 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7793 pipe_config->pch_pfit.pos,
7794 pipe_config->pch_pfit.size);
7795 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
7798 static bool check_encoder_cloning(struct drm_crtc *crtc)
7800 int num_encoders = 0;
7801 bool uncloneable_encoders = false;
7802 struct intel_encoder *encoder;
7804 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7806 if (&encoder->new_crtc->base != crtc)
7810 if (!encoder->cloneable)
7811 uncloneable_encoders = true;
7814 return !(num_encoders > 1 && uncloneable_encoders);
7817 static struct intel_crtc_config *
7818 intel_modeset_pipe_config(struct drm_crtc *crtc,
7819 struct drm_framebuffer *fb,
7820 struct drm_display_mode *mode)
7822 struct drm_device *dev = crtc->dev;
7823 struct drm_encoder_helper_funcs *encoder_funcs;
7824 struct intel_encoder *encoder;
7825 struct intel_crtc_config *pipe_config;
7826 int plane_bpp, ret = -EINVAL;
7829 if (!check_encoder_cloning(crtc)) {
7830 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7831 return ERR_PTR(-EINVAL);
7834 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7836 return ERR_PTR(-ENOMEM);
7838 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7839 drm_mode_copy(&pipe_config->requested_mode, mode);
7840 pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
7841 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7843 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7844 * plane pixel format and any sink constraints into account. Returns the
7845 * source plane bpp so that dithering can be selected on mismatches
7846 * after encoders and crtc also have had their say. */
7847 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7853 /* Ensure the port clock defaults are reset when retrying. */
7854 pipe_config->port_clock = 0;
7855 pipe_config->pixel_multiplier = 1;
7857 /* Pass our mode to the connectors and the CRTC to give them a chance to
7858 * adjust it according to limitations or connector properties, and also
7859 * a chance to reject the mode entirely.
7861 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7864 if (&encoder->new_crtc->base != crtc)
7867 if (encoder->compute_config) {
7868 if (!(encoder->compute_config(encoder, pipe_config))) {
7869 DRM_DEBUG_KMS("Encoder config failure\n");
7876 encoder_funcs = encoder->base.helper_private;
7877 if (!(encoder_funcs->mode_fixup(&encoder->base,
7878 &pipe_config->requested_mode,
7879 &pipe_config->adjusted_mode))) {
7880 DRM_DEBUG_KMS("Encoder fixup failed\n");
7885 /* Set default port clock if not overwritten by the encoder. Needs to be
7886 * done afterwards in case the encoder adjusts the mode. */
7887 if (!pipe_config->port_clock)
7888 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7890 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
7892 DRM_DEBUG_KMS("CRTC fixup failed\n");
7897 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7902 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7907 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7908 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7909 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7914 return ERR_PTR(ret);
7917 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
7918 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7920 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7921 unsigned *prepare_pipes, unsigned *disable_pipes)
7923 struct intel_crtc *intel_crtc;
7924 struct drm_device *dev = crtc->dev;
7925 struct intel_encoder *encoder;
7926 struct intel_connector *connector;
7927 struct drm_crtc *tmp_crtc;
7929 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7931 /* Check which crtcs have changed outputs connected to them, these need
7932 * to be part of the prepare_pipes mask. We don't (yet) support global
7933 * modeset across multiple crtcs, so modeset_pipes will only have one
7934 * bit set at most. */
7935 list_for_each_entry(connector, &dev->mode_config.connector_list,
7937 if (connector->base.encoder == &connector->new_encoder->base)
7940 if (connector->base.encoder) {
7941 tmp_crtc = connector->base.encoder->crtc;
7943 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7946 if (connector->new_encoder)
7948 1 << connector->new_encoder->new_crtc->pipe;
7951 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7953 if (encoder->base.crtc == &encoder->new_crtc->base)
7956 if (encoder->base.crtc) {
7957 tmp_crtc = encoder->base.crtc;
7959 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7962 if (encoder->new_crtc)
7963 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7966 /* Check for any pipes that will be fully disabled ... */
7967 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7971 /* Don't try to disable disabled crtcs. */
7972 if (!intel_crtc->base.enabled)
7975 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7977 if (encoder->new_crtc == intel_crtc)
7982 *disable_pipes |= 1 << intel_crtc->pipe;
7986 /* set_mode is also used to update properties on life display pipes. */
7987 intel_crtc = to_intel_crtc(crtc);
7989 *prepare_pipes |= 1 << intel_crtc->pipe;
7992 * For simplicity do a full modeset on any pipe where the output routing
7993 * changed. We could be more clever, but that would require us to be
7994 * more careful with calling the relevant encoder->mode_set functions.
7997 *modeset_pipes = *prepare_pipes;
7999 /* ... and mask these out. */
8000 *modeset_pipes &= ~(*disable_pipes);
8001 *prepare_pipes &= ~(*disable_pipes);
8004 * HACK: We don't (yet) fully support global modesets. intel_set_config
8005 * obies this rule, but the modeset restore mode of
8006 * intel_modeset_setup_hw_state does not.
8008 *modeset_pipes &= 1 << intel_crtc->pipe;
8009 *prepare_pipes &= 1 << intel_crtc->pipe;
8011 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8012 *modeset_pipes, *prepare_pipes, *disable_pipes);
8015 static bool intel_crtc_in_use(struct drm_crtc *crtc)
8017 struct drm_encoder *encoder;
8018 struct drm_device *dev = crtc->dev;
8020 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8021 if (encoder->crtc == crtc)
8028 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8030 struct intel_encoder *intel_encoder;
8031 struct intel_crtc *intel_crtc;
8032 struct drm_connector *connector;
8034 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8036 if (!intel_encoder->base.crtc)
8039 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8041 if (prepare_pipes & (1 << intel_crtc->pipe))
8042 intel_encoder->connectors_active = false;
8045 intel_modeset_commit_output_state(dev);
8047 /* Update computed state. */
8048 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8050 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8053 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8054 if (!connector->encoder || !connector->encoder->crtc)
8057 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8059 if (prepare_pipes & (1 << intel_crtc->pipe)) {
8060 struct drm_property *dpms_property =
8061 dev->mode_config.dpms_property;
8063 connector->dpms = DRM_MODE_DPMS_ON;
8064 drm_object_property_set_value(&connector->base,
8068 intel_encoder = to_intel_encoder(connector->encoder);
8069 intel_encoder->connectors_active = true;
8075 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8076 list_for_each_entry((intel_crtc), \
8077 &(dev)->mode_config.crtc_list, \
8079 if (mask & (1 <<(intel_crtc)->pipe))
8082 intel_pipe_config_compare(struct drm_device *dev,
8083 struct intel_crtc_config *current_config,
8084 struct intel_crtc_config *pipe_config)
8086 #define PIPE_CONF_CHECK_X(name) \
8087 if (current_config->name != pipe_config->name) { \
8088 DRM_ERROR("mismatch in " #name " " \
8089 "(expected 0x%08x, found 0x%08x)\n", \
8090 current_config->name, \
8091 pipe_config->name); \
8095 #define PIPE_CONF_CHECK_I(name) \
8096 if (current_config->name != pipe_config->name) { \
8097 DRM_ERROR("mismatch in " #name " " \
8098 "(expected %i, found %i)\n", \
8099 current_config->name, \
8100 pipe_config->name); \
8104 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
8105 if ((current_config->name ^ pipe_config->name) & (mask)) { \
8106 DRM_ERROR("mismatch in " #name " " \
8107 "(expected %i, found %i)\n", \
8108 current_config->name & (mask), \
8109 pipe_config->name & (mask)); \
8113 #define PIPE_CONF_QUIRK(quirk) \
8114 ((current_config->quirks | pipe_config->quirks) & (quirk))
8116 PIPE_CONF_CHECK_I(cpu_transcoder);
8118 PIPE_CONF_CHECK_I(has_pch_encoder);
8119 PIPE_CONF_CHECK_I(fdi_lanes);
8120 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8121 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8122 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8123 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8124 PIPE_CONF_CHECK_I(fdi_m_n.tu);
8126 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8127 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8128 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8129 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8130 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8131 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8133 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8134 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8135 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8136 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8137 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8138 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8140 if (!HAS_PCH_SPLIT(dev))
8141 PIPE_CONF_CHECK_I(pixel_multiplier);
8143 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8144 DRM_MODE_FLAG_INTERLACE);
8146 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8147 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8148 DRM_MODE_FLAG_PHSYNC);
8149 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8150 DRM_MODE_FLAG_NHSYNC);
8151 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8152 DRM_MODE_FLAG_PVSYNC);
8153 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8154 DRM_MODE_FLAG_NVSYNC);
8157 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8158 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8160 PIPE_CONF_CHECK_I(gmch_pfit.control);
8161 /* pfit ratios are autocomputed by the hw on gen4+ */
8162 if (INTEL_INFO(dev)->gen < 4)
8163 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8164 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8165 PIPE_CONF_CHECK_I(pch_pfit.pos);
8166 PIPE_CONF_CHECK_I(pch_pfit.size);
8168 PIPE_CONF_CHECK_I(ips_enabled);
8170 PIPE_CONF_CHECK_I(shared_dpll);
8171 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8172 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8173 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
8175 #undef PIPE_CONF_CHECK_X
8176 #undef PIPE_CONF_CHECK_I
8177 #undef PIPE_CONF_CHECK_FLAGS
8178 #undef PIPE_CONF_QUIRK
8184 check_connector_state(struct drm_device *dev)
8186 struct intel_connector *connector;
8188 list_for_each_entry(connector, &dev->mode_config.connector_list,
8190 /* This also checks the encoder/connector hw state with the
8191 * ->get_hw_state callbacks. */
8192 intel_connector_check_state(connector);
8194 WARN(&connector->new_encoder->base != connector->base.encoder,
8195 "connector's staged encoder doesn't match current encoder\n");
8200 check_encoder_state(struct drm_device *dev)
8202 struct intel_encoder *encoder;
8203 struct intel_connector *connector;
8205 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8207 bool enabled = false;
8208 bool active = false;
8209 enum i915_pipe pipe, tracked_pipe;
8211 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8212 encoder->base.base.id,
8213 drm_get_encoder_name(&encoder->base));
8215 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8216 "encoder's stage crtc doesn't match current crtc\n");
8217 WARN(encoder->connectors_active && !encoder->base.crtc,
8218 "encoder's active_connectors set, but no crtc\n");
8220 list_for_each_entry(connector, &dev->mode_config.connector_list,
8222 if (connector->base.encoder != &encoder->base)
8225 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8228 WARN(!!encoder->base.crtc != enabled,
8229 "encoder's enabled state mismatch "
8230 "(expected %i, found %i)\n",
8231 !!encoder->base.crtc, enabled);
8232 WARN(active && !encoder->base.crtc,
8233 "active encoder with no crtc\n");
8235 WARN(encoder->connectors_active != active,
8236 "encoder's computed active state doesn't match tracked active state "
8237 "(expected %i, found %i)\n", active, encoder->connectors_active);
8239 active = encoder->get_hw_state(encoder, &pipe);
8240 WARN(active != encoder->connectors_active,
8241 "encoder's hw state doesn't match sw tracking "
8242 "(expected %i, found %i)\n",
8243 encoder->connectors_active, active);
8245 if (!encoder->base.crtc)
8248 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8249 WARN(active && pipe != tracked_pipe,
8250 "active encoder's pipe doesn't match"
8251 "(expected %i, found %i)\n",
8252 tracked_pipe, pipe);
8258 check_crtc_state(struct drm_device *dev)
8260 drm_i915_private_t *dev_priv = dev->dev_private;
8261 struct intel_crtc *crtc;
8262 struct intel_encoder *encoder;
8263 struct intel_crtc_config pipe_config;
8265 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8267 bool enabled = false;
8268 bool active = false;
8270 memset(&pipe_config, 0, sizeof(pipe_config));
8272 DRM_DEBUG_KMS("[CRTC:%d]\n",
8273 crtc->base.base.id);
8275 WARN(crtc->active && !crtc->base.enabled,
8276 "active crtc, but not enabled in sw tracking\n");
8278 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8280 if (encoder->base.crtc != &crtc->base)
8283 if (encoder->connectors_active)
8287 WARN(active != crtc->active,
8288 "crtc's computed active state doesn't match tracked active state "
8289 "(expected %i, found %i)\n", active, crtc->active);
8290 WARN(enabled != crtc->base.enabled,
8291 "crtc's computed enabled state doesn't match tracked enabled state "
8292 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8294 active = dev_priv->display.get_pipe_config(crtc,
8297 /* hw state is inconsistent with the pipe A quirk */
8298 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
8299 active = crtc->active;
8301 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8303 enum i915_pipe pipe;
8304 if (encoder->base.crtc != &crtc->base)
8306 if (encoder->get_config &&
8307 encoder->get_hw_state(encoder, &pipe))
8308 encoder->get_config(encoder, &pipe_config);
8311 WARN(crtc->active != active,
8312 "crtc active state doesn't match with hw state "
8313 "(expected %i, found %i)\n", crtc->active, active);
8316 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8317 WARN(1, "pipe state doesn't match!\n");
8318 intel_dump_pipe_config(crtc, &pipe_config,
8320 intel_dump_pipe_config(crtc, &crtc->config,
8327 check_shared_dpll_state(struct drm_device *dev)
8329 drm_i915_private_t *dev_priv = dev->dev_private;
8330 struct intel_crtc *crtc;
8331 struct intel_dpll_hw_state dpll_hw_state;
8334 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8335 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
8336 int enabled_crtcs = 0, active_crtcs = 0;
8339 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8341 DRM_DEBUG_KMS("%s\n", pll->name);
8343 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
8345 WARN(pll->active > pll->refcount,
8346 "more active pll users than references: %i vs %i\n",
8347 pll->active, pll->refcount);
8348 WARN(pll->active && !pll->on,
8349 "pll in active use but not on in sw tracking\n");
8350 WARN(pll->on && !pll->active,
8351 "pll in on but not on in use in sw tracking\n");
8352 WARN(pll->on != active,
8353 "pll on state mismatch (expected %i, found %i)\n",
8356 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8358 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
8360 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
8363 WARN(pll->active != active_crtcs,
8364 "pll active crtcs mismatch (expected %i, found %i)\n",
8365 pll->active, active_crtcs);
8366 WARN(pll->refcount != enabled_crtcs,
8367 "pll enabled crtcs mismatch (expected %i, found %i)\n",
8368 pll->refcount, enabled_crtcs);
8370 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
8371 sizeof(dpll_hw_state)),
8372 "pll hw state mismatch\n");
8377 intel_modeset_check_state(struct drm_device *dev)
8379 check_connector_state(dev);
8380 check_encoder_state(dev);
8381 check_crtc_state(dev);
8382 check_shared_dpll_state(dev);
8385 static int __intel_set_mode(struct drm_crtc *crtc,
8386 struct drm_display_mode *mode,
8387 int x, int y, struct drm_framebuffer *fb)
8389 struct drm_device *dev = crtc->dev;
8390 drm_i915_private_t *dev_priv = dev->dev_private;
8391 struct drm_display_mode *saved_mode, *saved_hwmode;
8392 struct intel_crtc_config *pipe_config = NULL;
8393 struct intel_crtc *intel_crtc;
8394 unsigned disable_pipes, prepare_pipes, modeset_pipes;
8397 saved_mode = kmalloc(2 * sizeof(*saved_mode), M_DRM, M_WAITOK);
8400 saved_hwmode = saved_mode + 1;
8402 intel_modeset_affected_pipes(crtc, &modeset_pipes,
8403 &prepare_pipes, &disable_pipes);
8405 *saved_hwmode = crtc->hwmode;
8406 *saved_mode = crtc->mode;
8408 /* Hack: Because we don't (yet) support global modeset on multiple
8409 * crtcs, we don't keep track of the new mode for more than one crtc.
8410 * Hence simply check whether any bit is set in modeset_pipes in all the
8411 * pieces of code that are not yet converted to deal with mutliple crtcs
8412 * changing their mode at the same time. */
8413 if (modeset_pipes) {
8414 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8415 if (IS_ERR(pipe_config)) {
8416 ret = PTR_ERR(pipe_config);
8421 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8425 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8426 intel_crtc_disable(&intel_crtc->base);
8428 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8429 if (intel_crtc->base.enabled)
8430 dev_priv->display.crtc_disable(&intel_crtc->base);
8433 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8434 * to set it here already despite that we pass it down the callchain.
8436 if (modeset_pipes) {
8438 /* mode_set/enable/disable functions rely on a correct pipe
8440 to_intel_crtc(crtc)->config = *pipe_config;
8443 /* Only after disabling all output pipelines that will be changed can we
8444 * update the the output configuration. */
8445 intel_modeset_update_state(dev, prepare_pipes);
8447 if (dev_priv->display.modeset_global_resources)
8448 dev_priv->display.modeset_global_resources(dev);
8450 /* Set up the DPLL and any encoders state that needs to adjust or depend
8453 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8454 ret = intel_crtc_mode_set(&intel_crtc->base,
8460 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
8461 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8462 dev_priv->display.crtc_enable(&intel_crtc->base);
8464 if (modeset_pipes) {
8465 /* Store real post-adjustment hardware mode. */
8466 crtc->hwmode = pipe_config->adjusted_mode;
8468 /* Calculate and store various constants which
8469 * are later needed by vblank and swap-completion
8470 * timestamping. They are derived from true hwmode.
8472 drm_calc_timestamping_constants(crtc);
8475 /* FIXME: add subpixel order */
8477 if (ret && crtc->enabled) {
8478 crtc->hwmode = *saved_hwmode;
8479 crtc->mode = *saved_mode;
8488 int intel_set_mode(struct drm_crtc *crtc,
8489 struct drm_display_mode *mode,
8490 int x, int y, struct drm_framebuffer *fb)
8494 ret = __intel_set_mode(crtc, mode, x, y, fb);
8497 intel_modeset_check_state(crtc->dev);
8502 void intel_crtc_restore_mode(struct drm_crtc *crtc)
8504 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8507 #undef for_each_intel_crtc_masked
8509 static void intel_set_config_free(struct intel_set_config *config)
8514 kfree(config->save_connector_encoders);
8515 kfree(config->save_encoder_crtcs);
8519 static int intel_set_config_save_state(struct drm_device *dev,
8520 struct intel_set_config *config)
8522 struct drm_encoder *encoder;
8523 struct drm_connector *connector;
8526 config->save_encoder_crtcs =
8527 kmalloc(dev->mode_config.num_encoder *
8528 sizeof(struct drm_crtc *), M_DRM, M_WAITOK | M_ZERO );
8529 if (!config->save_encoder_crtcs)
8532 config->save_connector_encoders =
8533 kmalloc(dev->mode_config.num_connector *
8534 sizeof(struct drm_encoder *), M_DRM, M_WAITOK | M_ZERO );
8535 if (!config->save_connector_encoders)
8538 /* Copy data. Note that driver private data is not affected.
8539 * Should anything bad happen only the expected state is
8540 * restored, not the drivers personal bookkeeping.
8543 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
8544 config->save_encoder_crtcs[count++] = encoder->crtc;
8548 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8549 config->save_connector_encoders[count++] = connector->encoder;
8555 static void intel_set_config_restore_state(struct drm_device *dev,
8556 struct intel_set_config *config)
8558 struct intel_encoder *encoder;
8559 struct intel_connector *connector;
8563 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8565 to_intel_crtc(config->save_encoder_crtcs[count++]);
8569 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8570 connector->new_encoder =
8571 to_intel_encoder(config->save_connector_encoders[count++]);
8576 is_crtc_connector_off(struct drm_mode_set *set)
8580 if (set->num_connectors == 0)
8583 if (WARN_ON(set->connectors == NULL))
8586 for (i = 0; i < set->num_connectors; i++)
8587 if (set->connectors[i]->encoder &&
8588 set->connectors[i]->encoder->crtc == set->crtc &&
8589 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
8596 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8597 struct intel_set_config *config)
8600 /* We should be able to check here if the fb has the same properties
8601 * and then just flip_or_move it */
8602 if (is_crtc_connector_off(set)) {
8603 config->mode_changed = true;
8604 } else if (set->crtc->fb != set->fb) {
8605 /* If we have no fb then treat it as a full mode set */
8606 if (set->crtc->fb == NULL) {
8607 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8608 config->mode_changed = true;
8609 } else if (set->fb == NULL) {
8610 config->mode_changed = true;
8611 } else if (set->fb->pixel_format !=
8612 set->crtc->fb->pixel_format) {
8613 config->mode_changed = true;
8615 config->fb_changed = true;
8619 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
8620 config->fb_changed = true;
8622 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8623 DRM_DEBUG_KMS("modes are different, full mode set\n");
8624 drm_mode_debug_printmodeline(&set->crtc->mode);
8625 drm_mode_debug_printmodeline(set->mode);
8626 config->mode_changed = true;
8631 intel_modeset_stage_output_state(struct drm_device *dev,
8632 struct drm_mode_set *set,
8633 struct intel_set_config *config)
8635 struct drm_crtc *new_crtc;
8636 struct intel_connector *connector;
8637 struct intel_encoder *encoder;
8640 /* The upper layers ensure that we either disable a crtc or have a list
8641 * of connectors. For paranoia, double-check this. */
8642 WARN_ON(!set->fb && (set->num_connectors != 0));
8643 WARN_ON(set->fb && (set->num_connectors == 0));
8646 list_for_each_entry(connector, &dev->mode_config.connector_list,
8648 /* Otherwise traverse passed in connector list and get encoders
8650 for (ro = 0; ro < set->num_connectors; ro++) {
8651 if (set->connectors[ro] == &connector->base) {
8652 connector->new_encoder = connector->encoder;
8657 /* If we disable the crtc, disable all its connectors. Also, if
8658 * the connector is on the changing crtc but not on the new
8659 * connector list, disable it. */
8660 if ((!set->fb || ro == set->num_connectors) &&
8661 connector->base.encoder &&
8662 connector->base.encoder->crtc == set->crtc) {
8663 connector->new_encoder = NULL;
8665 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8666 connector->base.base.id,
8667 drm_get_connector_name(&connector->base));
8671 if (&connector->new_encoder->base != connector->base.encoder) {
8672 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
8673 config->mode_changed = true;
8676 /* connector->new_encoder is now updated for all connectors. */
8678 /* Update crtc of enabled connectors. */
8680 list_for_each_entry(connector, &dev->mode_config.connector_list,
8682 if (!connector->new_encoder)
8685 new_crtc = connector->new_encoder->base.crtc;
8687 for (ro = 0; ro < set->num_connectors; ro++) {
8688 if (set->connectors[ro] == &connector->base)
8689 new_crtc = set->crtc;
8692 /* Make sure the new CRTC will work with the encoder */
8693 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8697 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8699 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8700 connector->base.base.id,
8701 drm_get_connector_name(&connector->base),
8705 /* Check for any encoders that needs to be disabled. */
8706 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8708 list_for_each_entry(connector,
8709 &dev->mode_config.connector_list,
8711 if (connector->new_encoder == encoder) {
8712 WARN_ON(!connector->new_encoder->new_crtc);
8717 encoder->new_crtc = NULL;
8719 /* Only now check for crtc changes so we don't miss encoders
8720 * that will be disabled. */
8721 if (&encoder->new_crtc->base != encoder->base.crtc) {
8722 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
8723 config->mode_changed = true;
8726 /* Now we've also updated encoder->new_crtc for all encoders. */
8731 static int intel_crtc_set_config(struct drm_mode_set *set)
8733 struct drm_device *dev;
8734 struct drm_mode_set save_set;
8735 struct intel_set_config *config;
8740 BUG_ON(!set->crtc->helper_private);
8742 /* Enforce sane interface api - has been abused by the fb helper. */
8743 BUG_ON(!set->mode && set->fb);
8744 BUG_ON(set->fb && set->num_connectors == 0);
8747 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8748 set->crtc->base.id, set->fb->base.id,
8749 (int)set->num_connectors, set->x, set->y);
8751 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
8754 dev = set->crtc->dev;
8757 config = kzalloc(sizeof(*config), GFP_KERNEL);
8761 ret = intel_set_config_save_state(dev, config);
8765 save_set.crtc = set->crtc;
8766 save_set.mode = &set->crtc->mode;
8767 save_set.x = set->crtc->x;
8768 save_set.y = set->crtc->y;
8769 save_set.fb = set->crtc->fb;
8771 /* Compute whether we need a full modeset, only an fb base update or no
8772 * change at all. In the future we might also check whether only the
8773 * mode changed, e.g. for LVDS where we only change the panel fitter in
8775 intel_set_config_compute_mode_changes(set, config);
8777 ret = intel_modeset_stage_output_state(dev, set, config);
8781 if (config->mode_changed) {
8782 ret = intel_set_mode(set->crtc, set->mode,
8783 set->x, set->y, set->fb);
8784 } else if (config->fb_changed) {
8785 intel_crtc_wait_for_pending_flips(set->crtc);
8787 ret = intel_pipe_set_base(set->crtc,
8788 set->x, set->y, set->fb);
8792 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
8793 set->crtc->base.id, ret);
8795 intel_set_config_restore_state(dev, config);
8797 /* Try to restore the config */
8798 if (config->mode_changed &&
8799 intel_set_mode(save_set.crtc, save_set.mode,
8800 save_set.x, save_set.y, save_set.fb))
8801 DRM_ERROR("failed to restore config after modeset failure\n");
8805 intel_set_config_free(config);
8809 static const struct drm_crtc_funcs intel_crtc_funcs = {
8810 .cursor_set = intel_crtc_cursor_set,
8811 .cursor_move = intel_crtc_cursor_move,
8812 .gamma_set = intel_crtc_gamma_set,
8813 .set_config = intel_crtc_set_config,
8814 .destroy = intel_crtc_destroy,
8815 .page_flip = intel_crtc_page_flip,
8818 static void intel_cpu_pll_init(struct drm_device *dev)
8821 intel_ddi_pll_init(dev);
8824 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
8825 struct intel_shared_dpll *pll,
8826 struct intel_dpll_hw_state *hw_state)
8830 val = I915_READ(PCH_DPLL(pll->id));
8831 hw_state->dpll = val;
8832 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
8833 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
8835 return val & DPLL_VCO_ENABLE;
8838 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
8839 struct intel_shared_dpll *pll)
8843 /* PCH refclock must be enabled first */
8844 assert_pch_refclk_enabled(dev_priv);
8846 reg = PCH_DPLL(pll->id);
8847 val = I915_READ(reg);
8848 val |= DPLL_VCO_ENABLE;
8849 I915_WRITE(reg, val);
8854 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
8855 struct intel_shared_dpll *pll)
8857 struct drm_device *dev = dev_priv->dev;
8858 struct intel_crtc *crtc;
8861 /* Make sure no transcoder isn't still depending on us. */
8862 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
8863 if (intel_crtc_to_shared_dpll(crtc) == pll)
8864 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
8867 reg = PCH_DPLL(pll->id);
8868 val = I915_READ(reg);
8869 val &= ~DPLL_VCO_ENABLE;
8870 I915_WRITE(reg, val);
8875 static char *ibx_pch_dpll_names[] = {
8880 static void ibx_pch_dpll_init(struct drm_device *dev)
8882 struct drm_i915_private *dev_priv = dev->dev_private;
8885 dev_priv->num_shared_dpll = 2;
8887 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8888 dev_priv->shared_dplls[i].id = i;
8889 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
8890 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
8891 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
8892 dev_priv->shared_dplls[i].get_hw_state =
8893 ibx_pch_dpll_get_hw_state;
8897 static void intel_shared_dpll_init(struct drm_device *dev)
8899 struct drm_i915_private *dev_priv = dev->dev_private;
8901 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8902 ibx_pch_dpll_init(dev);
8904 dev_priv->num_shared_dpll = 0;
8906 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
8907 DRM_DEBUG_KMS("%i shared PLLs initialized\n",
8908 dev_priv->num_shared_dpll);
8911 static void intel_crtc_init(struct drm_device *dev, int pipe)
8913 drm_i915_private_t *dev_priv = dev->dev_private;
8914 struct intel_crtc *intel_crtc;
8917 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8918 if (intel_crtc == NULL)
8921 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8923 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
8924 for (i = 0; i < 256; i++) {
8925 intel_crtc->lut_r[i] = i;
8926 intel_crtc->lut_g[i] = i;
8927 intel_crtc->lut_b[i] = i;
8930 /* Swap pipes & planes for FBC on pre-965 */
8931 intel_crtc->pipe = pipe;
8932 intel_crtc->plane = pipe;
8933 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
8934 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
8935 intel_crtc->plane = !pipe;
8938 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8939 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8940 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8941 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8943 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8946 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
8947 struct drm_file *file)
8949 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
8950 struct drm_mode_object *drmmode_obj;
8951 struct intel_crtc *crtc;
8953 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8956 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8957 DRM_MODE_OBJECT_CRTC);
8960 DRM_ERROR("no such CRTC id\n");
8964 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8965 pipe_from_crtc_id->pipe = crtc->pipe;
8970 static int intel_encoder_clones(struct intel_encoder *encoder)
8972 struct drm_device *dev = encoder->base.dev;
8973 struct intel_encoder *source_encoder;
8977 list_for_each_entry(source_encoder,
8978 &dev->mode_config.encoder_list, base.head) {
8980 if (encoder == source_encoder)
8981 index_mask |= (1 << entry);
8983 /* Intel hw has only one MUX where enocoders could be cloned. */
8984 if (encoder->cloneable && source_encoder->cloneable)
8985 index_mask |= (1 << entry);
8993 static bool has_edp_a(struct drm_device *dev)
8995 struct drm_i915_private *dev_priv = dev->dev_private;
8997 if (!IS_MOBILE(dev))
9000 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
9004 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
9010 static void intel_setup_outputs(struct drm_device *dev)
9012 struct drm_i915_private *dev_priv = dev->dev_private;
9013 struct intel_encoder *encoder;
9014 bool dpd_is_edp = false;
9016 intel_lvds_init(dev);
9019 intel_crt_init(dev);
9024 /* Haswell uses DDI functions to detect digital outputs */
9025 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
9026 /* DDI A only supports eDP */
9028 intel_ddi_init(dev, PORT_A);
9030 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
9032 found = I915_READ(SFUSE_STRAP);
9034 if (found & SFUSE_STRAP_DDIB_DETECTED)
9035 intel_ddi_init(dev, PORT_B);
9036 if (found & SFUSE_STRAP_DDIC_DETECTED)
9037 intel_ddi_init(dev, PORT_C);
9038 if (found & SFUSE_STRAP_DDID_DETECTED)
9039 intel_ddi_init(dev, PORT_D);
9040 } else if (HAS_PCH_SPLIT(dev)) {
9042 dpd_is_edp = intel_dpd_is_edp(dev);
9045 intel_dp_init(dev, DP_A, PORT_A);
9047 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
9048 /* PCH SDVOB multiplex with HDMIB */
9049 found = intel_sdvo_init(dev, PCH_SDVOB, true);
9051 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
9052 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
9053 intel_dp_init(dev, PCH_DP_B, PORT_B);
9056 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
9057 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
9059 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
9060 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
9062 if (I915_READ(PCH_DP_C) & DP_DETECTED)
9063 intel_dp_init(dev, PCH_DP_C, PORT_C);
9065 if (I915_READ(PCH_DP_D) & DP_DETECTED)
9066 intel_dp_init(dev, PCH_DP_D, PORT_D);
9067 } else if (IS_VALLEYVIEW(dev)) {
9068 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
9069 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
9070 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
9072 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
9073 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
9075 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
9076 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
9078 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
9081 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9082 DRM_DEBUG_KMS("probing SDVOB\n");
9083 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
9084 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
9085 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
9086 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
9089 if (!found && SUPPORTS_INTEGRATED_DP(dev))
9090 intel_dp_init(dev, DP_B, PORT_B);
9093 /* Before G4X SDVOC doesn't have its own detect register */
9095 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
9096 DRM_DEBUG_KMS("probing SDVOC\n");
9097 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
9100 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
9102 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
9103 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
9104 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
9106 if (SUPPORTS_INTEGRATED_DP(dev))
9107 intel_dp_init(dev, DP_C, PORT_C);
9110 if (SUPPORTS_INTEGRATED_DP(dev) &&
9111 (I915_READ(DP_D) & DP_DETECTED))
9112 intel_dp_init(dev, DP_D, PORT_D);
9113 } else if (IS_GEN2(dev)) {
9115 intel_dvo_init(dev);
9119 if (SUPPORTS_TV(dev))
9122 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
9123 encoder->base.possible_crtcs = encoder->crtc_mask;
9124 encoder->base.possible_clones =
9125 intel_encoder_clones(encoder);
9128 intel_init_pch_refclk(dev);
9130 drm_helper_move_panel_connectors_to_head(dev);
9133 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
9135 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9137 drm_framebuffer_cleanup(fb);
9138 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
9143 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
9144 struct drm_file *file,
9145 unsigned int *handle)
9147 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
9148 struct drm_i915_gem_object *obj = intel_fb->obj;
9150 return drm_gem_handle_create(file, &obj->base, handle);
9153 static const struct drm_framebuffer_funcs intel_fb_funcs = {
9154 .destroy = intel_user_framebuffer_destroy,
9155 .create_handle = intel_user_framebuffer_create_handle,
9158 int intel_framebuffer_init(struct drm_device *dev,
9159 struct intel_framebuffer *intel_fb,
9160 struct drm_mode_fb_cmd2 *mode_cmd,
9161 struct drm_i915_gem_object *obj)
9166 if (obj->tiling_mode == I915_TILING_Y) {
9167 DRM_DEBUG("hardware does not support tiling Y\n");
9171 if (mode_cmd->pitches[0] & 63) {
9172 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9173 mode_cmd->pitches[0]);
9177 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
9178 pitch_limit = 32*1024;
9179 } else if (INTEL_INFO(dev)->gen >= 4) {
9180 if (obj->tiling_mode)
9181 pitch_limit = 16*1024;
9183 pitch_limit = 32*1024;
9184 } else if (INTEL_INFO(dev)->gen >= 3) {
9185 if (obj->tiling_mode)
9186 pitch_limit = 8*1024;
9188 pitch_limit = 16*1024;
9190 /* XXX DSPC is limited to 4k tiled */
9191 pitch_limit = 8*1024;
9193 if (mode_cmd->pitches[0] > pitch_limit) {
9194 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
9195 obj->tiling_mode ? "tiled" : "linear",
9196 mode_cmd->pitches[0], pitch_limit);
9200 if (obj->tiling_mode != I915_TILING_NONE &&
9201 mode_cmd->pitches[0] != obj->stride) {
9202 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9203 mode_cmd->pitches[0], obj->stride);
9207 /* Reject formats not supported by any plane early. */
9208 switch (mode_cmd->pixel_format) {
9210 case DRM_FORMAT_RGB565:
9211 case DRM_FORMAT_XRGB8888:
9212 case DRM_FORMAT_ARGB8888:
9214 case DRM_FORMAT_XRGB1555:
9215 case DRM_FORMAT_ARGB1555:
9216 if (INTEL_INFO(dev)->gen > 3) {
9217 DRM_DEBUG("unsupported pixel format: %s\n",
9218 drm_get_format_name(mode_cmd->pixel_format));
9222 case DRM_FORMAT_XBGR8888:
9223 case DRM_FORMAT_ABGR8888:
9224 case DRM_FORMAT_XRGB2101010:
9225 case DRM_FORMAT_ARGB2101010:
9226 case DRM_FORMAT_XBGR2101010:
9227 case DRM_FORMAT_ABGR2101010:
9228 if (INTEL_INFO(dev)->gen < 4) {
9229 DRM_DEBUG("unsupported pixel format: %s\n",
9230 drm_get_format_name(mode_cmd->pixel_format));
9234 case DRM_FORMAT_YUYV:
9235 case DRM_FORMAT_UYVY:
9236 case DRM_FORMAT_YVYU:
9237 case DRM_FORMAT_VYUY:
9238 if (INTEL_INFO(dev)->gen < 5) {
9239 DRM_DEBUG("unsupported pixel format: %s\n",
9240 drm_get_format_name(mode_cmd->pixel_format));
9245 DRM_DEBUG("unsupported pixel format: %s\n",
9246 drm_get_format_name(mode_cmd->pixel_format));
9250 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9251 if (mode_cmd->offsets[0] != 0)
9254 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9255 intel_fb->obj = obj;
9257 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9259 DRM_ERROR("framebuffer init failed %d\n", ret);
9266 static struct drm_framebuffer *
9267 intel_user_framebuffer_create(struct drm_device *dev,
9268 struct drm_file *filp,
9269 struct drm_mode_fb_cmd2 *mode_cmd)
9271 struct drm_i915_gem_object *obj;
9273 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9274 mode_cmd->handles[0]));
9275 if (&obj->base == NULL)
9276 return ERR_PTR(-ENOENT);
9278 return intel_framebuffer_create(dev, mode_cmd, obj);
9281 static const struct drm_mode_config_funcs intel_mode_funcs = {
9282 .fb_create = intel_user_framebuffer_create,
9283 .output_poll_changed = intel_fb_output_poll_changed,
9286 /* Set up chip specific display functions */
9287 static void intel_init_display(struct drm_device *dev)
9289 struct drm_i915_private *dev_priv = dev->dev_private;
9291 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9292 dev_priv->display.find_dpll = g4x_find_best_dpll;
9293 else if (IS_VALLEYVIEW(dev))
9294 dev_priv->display.find_dpll = vlv_find_best_dpll;
9295 else if (IS_PINEVIEW(dev))
9296 dev_priv->display.find_dpll = pnv_find_best_dpll;
9298 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9301 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
9302 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
9303 dev_priv->display.crtc_enable = haswell_crtc_enable;
9304 dev_priv->display.crtc_disable = haswell_crtc_disable;
9305 dev_priv->display.off = haswell_crtc_off;
9306 dev_priv->display.update_plane = ironlake_update_plane;
9307 } else if (HAS_PCH_SPLIT(dev)) {
9308 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
9309 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
9310 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9311 dev_priv->display.crtc_disable = ironlake_crtc_disable;
9312 dev_priv->display.off = ironlake_crtc_off;
9313 dev_priv->display.update_plane = ironlake_update_plane;
9314 } else if (IS_VALLEYVIEW(dev)) {
9315 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9316 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9317 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9318 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9319 dev_priv->display.off = i9xx_crtc_off;
9320 dev_priv->display.update_plane = i9xx_update_plane;
9322 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9323 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9324 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9325 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9326 dev_priv->display.off = i9xx_crtc_off;
9327 dev_priv->display.update_plane = i9xx_update_plane;
9330 /* Returns the core display clock speed */
9331 if (IS_VALLEYVIEW(dev))
9332 dev_priv->display.get_display_clock_speed =
9333 valleyview_get_display_clock_speed;
9334 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
9335 dev_priv->display.get_display_clock_speed =
9336 i945_get_display_clock_speed;
9337 else if (IS_I915G(dev))
9338 dev_priv->display.get_display_clock_speed =
9339 i915_get_display_clock_speed;
9340 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
9341 dev_priv->display.get_display_clock_speed =
9342 i9xx_misc_get_display_clock_speed;
9343 else if (IS_I915GM(dev))
9344 dev_priv->display.get_display_clock_speed =
9345 i915gm_get_display_clock_speed;
9346 else if (IS_I865G(dev))
9347 dev_priv->display.get_display_clock_speed =
9348 i865_get_display_clock_speed;
9349 else if (IS_I85X(dev))
9350 dev_priv->display.get_display_clock_speed =
9351 i855_get_display_clock_speed;
9353 dev_priv->display.get_display_clock_speed =
9354 i830_get_display_clock_speed;
9356 if (HAS_PCH_SPLIT(dev)) {
9358 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
9359 dev_priv->display.write_eld = ironlake_write_eld;
9360 } else if (IS_GEN6(dev)) {
9361 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
9362 dev_priv->display.write_eld = ironlake_write_eld;
9363 } else if (IS_IVYBRIDGE(dev)) {
9364 /* FIXME: detect B0+ stepping and use auto training */
9365 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
9366 dev_priv->display.write_eld = ironlake_write_eld;
9367 dev_priv->display.modeset_global_resources =
9368 ivb_modeset_global_resources;
9369 } else if (IS_HASWELL(dev)) {
9370 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
9371 dev_priv->display.write_eld = haswell_write_eld;
9372 dev_priv->display.modeset_global_resources =
9373 haswell_modeset_global_resources;
9375 } else if (IS_G4X(dev)) {
9376 dev_priv->display.write_eld = g4x_write_eld;
9379 /* Default just returns -ENODEV to indicate unsupported */
9380 dev_priv->display.queue_flip = intel_default_queue_flip;
9382 switch (INTEL_INFO(dev)->gen) {
9384 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9388 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9393 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9397 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9400 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9406 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9407 * resume, or other times. This quirk makes sure that's the case for
9410 static void quirk_pipea_force(struct drm_device *dev)
9412 struct drm_i915_private *dev_priv = dev->dev_private;
9414 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9415 DRM_INFO("applying pipe a force quirk\n");
9419 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9421 static void quirk_ssc_force_disable(struct drm_device *dev)
9423 struct drm_i915_private *dev_priv = dev->dev_private;
9424 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9425 DRM_INFO("applying lvds SSC disable quirk\n");
9429 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9432 static void quirk_invert_brightness(struct drm_device *dev)
9434 struct drm_i915_private *dev_priv = dev->dev_private;
9435 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9436 DRM_INFO("applying inverted panel brightness quirk\n");
9440 * Some machines (Dell XPS13) suffer broken backlight controls if
9441 * BLM_PCH_PWM_ENABLE is set.
9443 static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
9445 struct drm_i915_private *dev_priv = dev->dev_private;
9446 dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
9447 DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
9450 struct intel_quirk {
9452 int subsystem_vendor;
9453 int subsystem_device;
9454 void (*hook)(struct drm_device *dev);
9457 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9458 struct intel_dmi_quirk {
9459 void (*hook)(struct drm_device *dev);
9460 const struct dmi_system_id (*dmi_id_list)[];
9463 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9465 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9469 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9471 .dmi_id_list = &(const struct dmi_system_id[]) {
9473 .callback = intel_dmi_reverse_brightness,
9474 .ident = "NCR Corporation",
9475 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9476 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9479 { } /* terminating entry */
9481 .hook = quirk_invert_brightness,
9485 static struct intel_quirk intel_quirks[] = {
9486 /* HP Mini needs pipe A force quirk (LP: #322104) */
9487 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9489 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9490 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9492 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9493 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9495 /* 830/845 need to leave pipe A & dpll A up */
9496 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9497 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9499 /* Lenovo U160 cannot use SSC on LVDS */
9500 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9502 /* Sony Vaio Y cannot use SSC on LVDS */
9503 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9505 /* Acer Aspire 5734Z must invert backlight brightness */
9506 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
9508 /* Acer/eMachines G725 */
9509 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
9511 /* Acer/eMachines e725 */
9512 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
9514 /* Acer/Packard Bell NCL20 */
9515 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
9517 /* Acer Aspire 4736Z */
9518 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
9520 /* Dell XPS13 HD Sandy Bridge */
9521 { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
9522 /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
9523 { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
9526 static void intel_init_quirks(struct drm_device *dev)
9528 struct device *d = dev->dev;
9531 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9532 struct intel_quirk *q = &intel_quirks[i];
9534 if (pci_get_device(d) == q->device &&
9535 (pci_get_subvendor(d) == q->subsystem_vendor ||
9536 q->subsystem_vendor == PCI_ANY_ID) &&
9537 (pci_get_subdevice(d) == q->subsystem_device ||
9538 q->subsystem_device == PCI_ANY_ID))
9541 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9542 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9543 intel_dmi_quirks[i].hook(dev);
9547 /* Disable the VGA plane that we never use */
9548 static void i915_disable_vga(struct drm_device *dev)
9550 struct drm_i915_private *dev_priv = dev->dev_private;
9552 u32 vga_reg = i915_vgacntrl_reg(dev);
9555 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9557 outb(VGA_SR_INDEX, SR01);
9558 sr1 = inb(VGA_SR_DATA);
9559 outb(VGA_SR_DATA, sr1 | 1 << 5);
9561 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9565 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9566 POSTING_READ(vga_reg);
9569 void intel_modeset_init_hw(struct drm_device *dev)
9571 intel_init_power_well(dev);
9573 intel_prepare_ddi(dev);
9575 intel_init_clock_gating(dev);
9577 mutex_lock(&dev->struct_mutex);
9578 intel_enable_gt_powersave(dev);
9579 mutex_unlock(&dev->struct_mutex);
9582 void intel_modeset_suspend_hw(struct drm_device *dev)
9584 intel_suspend_hw(dev);
9587 void intel_modeset_init(struct drm_device *dev)
9589 struct drm_i915_private *dev_priv = dev->dev_private;
9592 drm_mode_config_init(dev);
9594 dev->mode_config.min_width = 0;
9595 dev->mode_config.min_height = 0;
9597 dev->mode_config.preferred_depth = 24;
9598 dev->mode_config.prefer_shadow = 1;
9600 dev->mode_config.funcs = &intel_mode_funcs;
9602 intel_init_quirks(dev);
9606 if (INTEL_INFO(dev)->num_pipes == 0)
9609 intel_init_display(dev);
9612 dev->mode_config.max_width = 2048;
9613 dev->mode_config.max_height = 2048;
9614 } else if (IS_GEN3(dev)) {
9615 dev->mode_config.max_width = 4096;
9616 dev->mode_config.max_height = 4096;
9618 dev->mode_config.max_width = 8192;
9619 dev->mode_config.max_height = 8192;
9621 dev->mode_config.fb_base = dev->agp->base;
9623 DRM_DEBUG_KMS("%d display pipe%s available.\n",
9624 INTEL_INFO(dev)->num_pipes,
9625 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
9627 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
9628 intel_crtc_init(dev, i);
9629 for (j = 0; j < dev_priv->num_plane; j++) {
9630 ret = intel_plane_init(dev, i, j);
9632 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9633 pipe_name(i), sprite_name(i, j), ret);
9637 intel_cpu_pll_init(dev);
9638 intel_shared_dpll_init(dev);
9640 /* Just disable it once at startup */
9641 i915_disable_vga(dev);
9642 intel_setup_outputs(dev);
9644 /* Just in case the BIOS is doing something questionable. */
9645 intel_disable_fbc(dev);
9649 intel_connector_break_all_links(struct intel_connector *connector)
9651 connector->base.dpms = DRM_MODE_DPMS_OFF;
9652 connector->base.encoder = NULL;
9653 connector->encoder->connectors_active = false;
9654 connector->encoder->base.crtc = NULL;
9657 static void intel_enable_pipe_a(struct drm_device *dev)
9659 struct intel_connector *connector;
9660 struct drm_connector *crt = NULL;
9661 struct intel_load_detect_pipe load_detect_temp;
9663 /* We can't just switch on the pipe A, we need to set things up with a
9664 * proper mode and output configuration. As a gross hack, enable pipe A
9665 * by enabling the load detect pipe once. */
9666 list_for_each_entry(connector,
9667 &dev->mode_config.connector_list,
9669 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9670 crt = &connector->base;
9678 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9679 intel_release_load_detect_pipe(crt, &load_detect_temp);
9685 intel_check_plane_mapping(struct intel_crtc *crtc)
9687 struct drm_device *dev = crtc->base.dev;
9688 struct drm_i915_private *dev_priv = dev->dev_private;
9691 if (INTEL_INFO(dev)->num_pipes == 1)
9694 reg = DSPCNTR(!crtc->plane);
9695 val = I915_READ(reg);
9697 if ((val & DISPLAY_PLANE_ENABLE) &&
9698 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9704 static void intel_sanitize_crtc(struct intel_crtc *crtc)
9706 struct drm_device *dev = crtc->base.dev;
9707 struct drm_i915_private *dev_priv = dev->dev_private;
9710 /* Clear any frame start delays used for debugging left by the BIOS */
9711 reg = PIPECONF(crtc->config.cpu_transcoder);
9712 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9714 /* We need to sanitize the plane -> pipe mapping first because this will
9715 * disable the crtc (and hence change the state) if it is wrong. Note
9716 * that gen4+ has a fixed plane -> pipe mapping. */
9717 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
9718 struct intel_connector *connector;
9721 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9722 crtc->base.base.id);
9724 /* Pipe has the wrong plane attached and the plane is active.
9725 * Temporarily change the plane mapping and disable everything
9727 plane = crtc->plane;
9728 crtc->plane = !plane;
9729 dev_priv->display.crtc_disable(&crtc->base);
9730 crtc->plane = plane;
9732 /* ... and break all links. */
9733 list_for_each_entry(connector, &dev->mode_config.connector_list,
9735 if (connector->encoder->base.crtc != &crtc->base)
9738 intel_connector_break_all_links(connector);
9741 WARN_ON(crtc->active);
9742 crtc->base.enabled = false;
9745 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9746 crtc->pipe == PIPE_A && !crtc->active) {
9747 /* BIOS forgot to enable pipe A, this mostly happens after
9748 * resume. Force-enable the pipe to fix this, the update_dpms
9749 * call below we restore the pipe to the right state, but leave
9750 * the required bits on. */
9751 intel_enable_pipe_a(dev);
9754 /* Adjust the state of the output pipe according to whether we
9755 * have active connectors/encoders. */
9756 intel_crtc_update_dpms(&crtc->base);
9758 if (crtc->active != crtc->base.enabled) {
9759 struct intel_encoder *encoder;
9761 /* This can happen either due to bugs in the get_hw_state
9762 * functions or because the pipe is force-enabled due to the
9764 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9766 crtc->base.enabled ? "enabled" : "disabled",
9767 crtc->active ? "enabled" : "disabled");
9769 crtc->base.enabled = crtc->active;
9771 /* Because we only establish the connector -> encoder ->
9772 * crtc links if something is active, this means the
9773 * crtc is now deactivated. Break the links. connector
9774 * -> encoder links are only establish when things are
9775 * actually up, hence no need to break them. */
9776 WARN_ON(crtc->active);
9778 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9779 WARN_ON(encoder->connectors_active);
9780 encoder->base.crtc = NULL;
9785 static void intel_sanitize_encoder(struct intel_encoder *encoder)
9787 struct intel_connector *connector;
9788 struct drm_device *dev = encoder->base.dev;
9790 /* We need to check both for a crtc link (meaning that the
9791 * encoder is active and trying to read from a pipe) and the
9792 * pipe itself being active. */
9793 bool has_active_crtc = encoder->base.crtc &&
9794 to_intel_crtc(encoder->base.crtc)->active;
9796 if (encoder->connectors_active && !has_active_crtc) {
9797 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9798 encoder->base.base.id,
9799 drm_get_encoder_name(&encoder->base));
9801 /* Connector is active, but has no active pipe. This is
9802 * fallout from our resume register restoring. Disable
9803 * the encoder manually again. */
9804 if (encoder->base.crtc) {
9805 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9806 encoder->base.base.id,
9807 drm_get_encoder_name(&encoder->base));
9808 encoder->disable(encoder);
9811 /* Inconsistent output/port/pipe state happens presumably due to
9812 * a bug in one of the get_hw_state functions. Or someplace else
9813 * in our code, like the register restore mess on resume. Clamp
9814 * things to off as a safer default. */
9815 list_for_each_entry(connector,
9816 &dev->mode_config.connector_list,
9818 if (connector->encoder != encoder)
9821 intel_connector_break_all_links(connector);
9824 /* Enabled encoders without active connectors will be fixed in
9825 * the crtc fixup. */
9828 void i915_redisable_vga(struct drm_device *dev)
9830 struct drm_i915_private *dev_priv = dev->dev_private;
9831 u32 vga_reg = i915_vgacntrl_reg(dev);
9833 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9834 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
9835 i915_disable_vga(dev);
9839 static void intel_modeset_readout_hw_state(struct drm_device *dev)
9841 struct drm_i915_private *dev_priv = dev->dev_private;
9842 enum i915_pipe pipe;
9843 struct intel_crtc *crtc;
9844 struct intel_encoder *encoder;
9845 struct intel_connector *connector;
9848 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9850 memset(&crtc->config, 0, sizeof(crtc->config));
9852 crtc->active = dev_priv->display.get_pipe_config(crtc,
9855 crtc->base.enabled = crtc->active;
9857 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9859 crtc->active ? "enabled" : "disabled");
9862 /* FIXME: Smash this into the new shared dpll infrastructure. */
9864 intel_ddi_setup_hw_pll_state(dev);
9866 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9867 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9869 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
9871 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9873 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9876 pll->refcount = pll->active;
9878 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
9879 pll->name, pll->refcount, pll->on);
9882 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9886 if (encoder->get_hw_state(encoder, &pipe)) {
9887 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9888 encoder->base.crtc = &crtc->base;
9889 if (encoder->get_config)
9890 encoder->get_config(encoder, &crtc->config);
9892 encoder->base.crtc = NULL;
9895 encoder->connectors_active = false;
9896 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9897 encoder->base.base.id,
9898 drm_get_encoder_name(&encoder->base),
9899 encoder->base.crtc ? "enabled" : "disabled",
9903 list_for_each_entry(connector, &dev->mode_config.connector_list,
9905 if (connector->get_hw_state(connector)) {
9906 connector->base.dpms = DRM_MODE_DPMS_ON;
9907 connector->encoder->connectors_active = true;
9908 connector->base.encoder = &connector->encoder->base;
9910 connector->base.dpms = DRM_MODE_DPMS_OFF;
9911 connector->base.encoder = NULL;
9913 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9914 connector->base.base.id,
9915 drm_get_connector_name(&connector->base),
9916 connector->base.encoder ? "enabled" : "disabled");
9920 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9921 * and i915 state tracking structures. */
9922 void intel_modeset_setup_hw_state(struct drm_device *dev,
9925 struct drm_i915_private *dev_priv = dev->dev_private;
9926 enum i915_pipe pipe;
9927 struct drm_plane *plane;
9928 struct intel_crtc *crtc;
9929 struct intel_encoder *encoder;
9932 intel_modeset_readout_hw_state(dev);
9934 /* HW state is read out, now we need to sanitize this mess. */
9935 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9937 intel_sanitize_encoder(encoder);
9940 for_each_pipe(pipe) {
9941 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9942 intel_sanitize_crtc(crtc);
9943 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
9946 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9947 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9949 if (!pll->on || pll->active)
9952 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
9954 pll->disable(dev_priv, pll);
9958 if (force_restore) {
9960 * We need to use raw interfaces for restoring state to avoid
9961 * checking (bogus) intermediate states.
9963 for_each_pipe(pipe) {
9964 struct drm_crtc *crtc =
9965 dev_priv->pipe_to_crtc_mapping[pipe];
9967 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9970 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9971 intel_plane_restore(plane);
9973 i915_redisable_vga(dev);
9975 intel_modeset_update_staged_output_state(dev);
9978 intel_modeset_check_state(dev);
9980 drm_mode_config_reset(dev);
9983 void intel_modeset_gem_init(struct drm_device *dev)
9985 intel_modeset_init_hw(dev);
9987 intel_setup_overlay(dev);
9989 intel_modeset_setup_hw_state(dev, false);
9992 void intel_modeset_cleanup(struct drm_device *dev)
9994 struct drm_i915_private *dev_priv = dev->dev_private;
9995 struct drm_crtc *crtc;
9996 struct intel_crtc *intel_crtc;
9999 * Interrupts and polling as the first thing to avoid creating havoc.
10000 * Too much stuff here (turning of rps, connectors, ...) would
10001 * experience fancy races otherwise.
10003 drm_irq_uninstall(dev);
10004 cancel_work_sync(&dev_priv->hotplug_work);
10006 * Due to the hpd irq storm handling the hotplug work can re-arm the
10007 * poll handlers. Hence disable polling after hpd handling is shut down.
10009 drm_kms_helper_poll_fini(dev);
10011 mutex_lock(&dev->struct_mutex);
10013 intel_unregister_dsm_handler();
10015 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10016 /* Skip inactive CRTCs */
10020 intel_crtc = to_intel_crtc(crtc);
10021 intel_increase_pllclock(crtc);
10024 intel_disable_fbc(dev);
10026 intel_disable_gt_powersave(dev);
10028 ironlake_teardown_rc6(dev);
10030 mutex_unlock(&dev->struct_mutex);
10032 /* flush any delayed tasks or pending work */
10033 flush_scheduled_work();
10035 /* destroy backlight, if any, before the connectors */
10036 intel_panel_destroy_backlight(dev);
10038 drm_mode_config_cleanup(dev);
10040 intel_cleanup_overlay(dev);
10044 * Return which encoder is currently attached for connector.
10046 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
10048 return &intel_attached_encoder(connector)->base;
10051 void intel_connector_attach_encoder(struct intel_connector *connector,
10052 struct intel_encoder *encoder)
10054 connector->encoder = encoder;
10055 drm_mode_connector_attach_encoder(&connector->base,
10060 * set vga decode state - true == enable VGA decode
10062 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
10064 struct drm_i915_private *dev_priv = dev->dev_private;
10067 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
10069 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
10071 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
10072 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
10076 #ifdef CONFIG_DEBUG_FS
10077 #include <linux/seq_file.h>
10079 struct intel_display_error_state {
10081 u32 power_well_driver;
10083 int num_transcoders;
10085 struct intel_cursor_error_state {
10090 } cursor[I915_MAX_PIPES];
10092 struct intel_pipe_error_state {
10094 } pipe[I915_MAX_PIPES];
10096 struct intel_plane_error_state {
10104 } plane[I915_MAX_PIPES];
10106 struct intel_transcoder_error_state {
10107 enum transcoder cpu_transcoder;
10120 struct intel_display_error_state *
10121 intel_display_capture_error_state(struct drm_device *dev)
10123 drm_i915_private_t *dev_priv = dev->dev_private;
10124 struct intel_display_error_state *error;
10125 int transcoders[] = {
10133 if (INTEL_INFO(dev)->num_pipes == 0)
10136 error = kmalloc(sizeof(*error), GFP_ATOMIC);
10140 if (HAS_POWER_WELL(dev))
10141 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
10144 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
10145 error->cursor[i].control = I915_READ(CURCNTR(i));
10146 error->cursor[i].position = I915_READ(CURPOS(i));
10147 error->cursor[i].base = I915_READ(CURBASE(i));
10149 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
10150 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
10151 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
10154 error->plane[i].control = I915_READ(DSPCNTR(i));
10155 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
10156 if (INTEL_INFO(dev)->gen <= 3) {
10157 error->plane[i].size = I915_READ(DSPSIZE(i));
10158 error->plane[i].pos = I915_READ(DSPPOS(i));
10160 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10161 error->plane[i].addr = I915_READ(DSPADDR(i));
10162 if (INTEL_INFO(dev)->gen >= 4) {
10163 error->plane[i].surface = I915_READ(DSPSURF(i));
10164 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
10167 error->pipe[i].source = I915_READ(PIPESRC(i));
10170 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
10171 if (HAS_DDI(dev_priv->dev))
10172 error->num_transcoders++; /* Account for eDP. */
10174 for (i = 0; i < error->num_transcoders; i++) {
10175 enum transcoder cpu_transcoder = transcoders[i];
10177 error->transcoder[i].cpu_transcoder = cpu_transcoder;
10179 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
10180 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
10181 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
10182 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
10183 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
10184 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
10185 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
10188 /* In the code above we read the registers without checking if the power
10189 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
10190 * prevent the next I915_WRITE from detecting it and printing an error
10192 if (HAS_POWER_WELL(dev))
10193 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
10198 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
10201 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
10202 struct drm_device *dev,
10203 struct intel_display_error_state *error)
10210 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
10211 if (HAS_POWER_WELL(dev))
10212 err_printf(m, "PWR_WELL_CTL2: %08x\n",
10213 error->power_well_driver);
10215 err_printf(m, "Pipe [%d]:\n", i);
10216 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
10218 err_printf(m, "Plane [%d]:\n", i);
10219 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
10220 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
10221 if (INTEL_INFO(dev)->gen <= 3) {
10222 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
10223 err_printf(m, " POS: %08x\n", error->plane[i].pos);
10225 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
10226 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
10227 if (INTEL_INFO(dev)->gen >= 4) {
10228 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
10229 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
10232 err_printf(m, "Cursor [%d]:\n", i);
10233 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
10234 err_printf(m, " POS: %08x\n", error->cursor[i].position);
10235 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
10238 for (i = 0; i < error->num_transcoders; i++) {
10239 err_printf(m, " CPU transcoder: %c\n",
10240 transcoder_name(error->transcoder[i].cpu_transcoder));
10241 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
10242 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
10243 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
10244 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
10245 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
10246 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
10247 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);