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/input.h>
30 #include <linux/i2c.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/vgaarb.h>
34 #include <drm/drm_edid.h>
36 #include "intel_drv.h"
37 #include <drm/i915_drm.h>
39 #include "i915_trace.h"
40 #include <drm/drm_dp_helper.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <linux/dma_remapping.h>
44 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
45 static void intel_increase_pllclock(struct drm_crtc *crtc);
46 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
57 #define INTEL_P2_NUM 2
58 typedef struct intel_limit intel_limit_t;
60 intel_range_t dot, vco, n, m, m1, m2, p, p1;
65 #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
68 intel_pch_rawclk(struct drm_device *dev)
70 struct drm_i915_private *dev_priv = dev->dev_private;
72 WARN_ON(!HAS_PCH_SPLIT(dev));
74 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
77 static inline u32 /* units of 100MHz */
78 intel_fdi_link_freq(struct drm_device *dev)
81 struct drm_i915_private *dev_priv = dev->dev_private;
82 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
87 static const intel_limit_t intel_limits_i8xx_dvo = {
88 .dot = { .min = 25000, .max = 350000 },
89 .vco = { .min = 930000, .max = 1400000 },
90 .n = { .min = 3, .max = 16 },
91 .m = { .min = 96, .max = 140 },
92 .m1 = { .min = 18, .max = 26 },
93 .m2 = { .min = 6, .max = 16 },
94 .p = { .min = 4, .max = 128 },
95 .p1 = { .min = 2, .max = 33 },
96 .p2 = { .dot_limit = 165000,
97 .p2_slow = 4, .p2_fast = 2 },
100 static const intel_limit_t intel_limits_i8xx_lvds = {
101 .dot = { .min = 25000, .max = 350000 },
102 .vco = { .min = 930000, .max = 1400000 },
103 .n = { .min = 3, .max = 16 },
104 .m = { .min = 96, .max = 140 },
105 .m1 = { .min = 18, .max = 26 },
106 .m2 = { .min = 6, .max = 16 },
107 .p = { .min = 4, .max = 128 },
108 .p1 = { .min = 1, .max = 6 },
109 .p2 = { .dot_limit = 165000,
110 .p2_slow = 14, .p2_fast = 7 },
113 static const intel_limit_t intel_limits_i9xx_sdvo = {
114 .dot = { .min = 20000, .max = 400000 },
115 .vco = { .min = 1400000, .max = 2800000 },
116 .n = { .min = 1, .max = 6 },
117 .m = { .min = 70, .max = 120 },
118 .m1 = { .min = 8, .max = 18 },
119 .m2 = { .min = 3, .max = 7 },
120 .p = { .min = 5, .max = 80 },
121 .p1 = { .min = 1, .max = 8 },
122 .p2 = { .dot_limit = 200000,
123 .p2_slow = 10, .p2_fast = 5 },
126 static const intel_limit_t intel_limits_i9xx_lvds = {
127 .dot = { .min = 20000, .max = 400000 },
128 .vco = { .min = 1400000, .max = 2800000 },
129 .n = { .min = 1, .max = 6 },
130 .m = { .min = 70, .max = 120 },
131 .m1 = { .min = 8, .max = 18 },
132 .m2 = { .min = 3, .max = 7 },
133 .p = { .min = 7, .max = 98 },
134 .p1 = { .min = 1, .max = 8 },
135 .p2 = { .dot_limit = 112000,
136 .p2_slow = 14, .p2_fast = 7 },
140 static const intel_limit_t intel_limits_g4x_sdvo = {
141 .dot = { .min = 25000, .max = 270000 },
142 .vco = { .min = 1750000, .max = 3500000},
143 .n = { .min = 1, .max = 4 },
144 .m = { .min = 104, .max = 138 },
145 .m1 = { .min = 17, .max = 23 },
146 .m2 = { .min = 5, .max = 11 },
147 .p = { .min = 10, .max = 30 },
148 .p1 = { .min = 1, .max = 3},
149 .p2 = { .dot_limit = 270000,
155 static const intel_limit_t intel_limits_g4x_hdmi = {
156 .dot = { .min = 22000, .max = 400000 },
157 .vco = { .min = 1750000, .max = 3500000},
158 .n = { .min = 1, .max = 4 },
159 .m = { .min = 104, .max = 138 },
160 .m1 = { .min = 16, .max = 23 },
161 .m2 = { .min = 5, .max = 11 },
162 .p = { .min = 5, .max = 80 },
163 .p1 = { .min = 1, .max = 8},
164 .p2 = { .dot_limit = 165000,
165 .p2_slow = 10, .p2_fast = 5 },
168 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
169 .dot = { .min = 20000, .max = 115000 },
170 .vco = { .min = 1750000, .max = 3500000 },
171 .n = { .min = 1, .max = 3 },
172 .m = { .min = 104, .max = 138 },
173 .m1 = { .min = 17, .max = 23 },
174 .m2 = { .min = 5, .max = 11 },
175 .p = { .min = 28, .max = 112 },
176 .p1 = { .min = 2, .max = 8 },
177 .p2 = { .dot_limit = 0,
178 .p2_slow = 14, .p2_fast = 14
182 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
183 .dot = { .min = 80000, .max = 224000 },
184 .vco = { .min = 1750000, .max = 3500000 },
185 .n = { .min = 1, .max = 3 },
186 .m = { .min = 104, .max = 138 },
187 .m1 = { .min = 17, .max = 23 },
188 .m2 = { .min = 5, .max = 11 },
189 .p = { .min = 14, .max = 42 },
190 .p1 = { .min = 2, .max = 6 },
191 .p2 = { .dot_limit = 0,
192 .p2_slow = 7, .p2_fast = 7
196 static const intel_limit_t intel_limits_pineview_sdvo = {
197 .dot = { .min = 20000, .max = 400000},
198 .vco = { .min = 1700000, .max = 3500000 },
199 /* Pineview's Ncounter is a ring counter */
200 .n = { .min = 3, .max = 6 },
201 .m = { .min = 2, .max = 256 },
202 /* Pineview only has one combined m divider, which we treat as m2. */
203 .m1 = { .min = 0, .max = 0 },
204 .m2 = { .min = 0, .max = 254 },
205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
211 static const intel_limit_t intel_limits_pineview_lvds = {
212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1700000, .max = 3500000 },
214 .n = { .min = 3, .max = 6 },
215 .m = { .min = 2, .max = 256 },
216 .m1 = { .min = 0, .max = 0 },
217 .m2 = { .min = 0, .max = 254 },
218 .p = { .min = 7, .max = 112 },
219 .p1 = { .min = 1, .max = 8 },
220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 14 },
224 /* Ironlake / Sandybridge
226 * We calculate clock using (register_value + 2) for N/M1/M2, so here
227 * the range value for them is (actual_value - 2).
229 static const intel_limit_t intel_limits_ironlake_dac = {
230 .dot = { .min = 25000, .max = 350000 },
231 .vco = { .min = 1760000, .max = 3510000 },
232 .n = { .min = 1, .max = 5 },
233 .m = { .min = 79, .max = 127 },
234 .m1 = { .min = 12, .max = 22 },
235 .m2 = { .min = 5, .max = 9 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8 },
238 .p2 = { .dot_limit = 225000,
239 .p2_slow = 10, .p2_fast = 5 },
242 static const intel_limit_t intel_limits_ironlake_single_lvds = {
243 .dot = { .min = 25000, .max = 350000 },
244 .vco = { .min = 1760000, .max = 3510000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 79, .max = 118 },
247 .m1 = { .min = 12, .max = 22 },
248 .m2 = { .min = 5, .max = 9 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 225000,
252 .p2_slow = 14, .p2_fast = 14 },
255 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
256 .dot = { .min = 25000, .max = 350000 },
257 .vco = { .min = 1760000, .max = 3510000 },
258 .n = { .min = 1, .max = 3 },
259 .m = { .min = 79, .max = 127 },
260 .m1 = { .min = 12, .max = 22 },
261 .m2 = { .min = 5, .max = 9 },
262 .p = { .min = 14, .max = 56 },
263 .p1 = { .min = 2, .max = 8 },
264 .p2 = { .dot_limit = 225000,
265 .p2_slow = 7, .p2_fast = 7 },
268 /* LVDS 100mhz refclk limits. */
269 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
270 .dot = { .min = 25000, .max = 350000 },
271 .vco = { .min = 1760000, .max = 3510000 },
272 .n = { .min = 1, .max = 2 },
273 .m = { .min = 79, .max = 126 },
274 .m1 = { .min = 12, .max = 22 },
275 .m2 = { .min = 5, .max = 9 },
276 .p = { .min = 28, .max = 112 },
277 .p1 = { .min = 2, .max = 8 },
278 .p2 = { .dot_limit = 225000,
279 .p2_slow = 14, .p2_fast = 14 },
282 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
283 .dot = { .min = 25000, .max = 350000 },
284 .vco = { .min = 1760000, .max = 3510000 },
285 .n = { .min = 1, .max = 3 },
286 .m = { .min = 79, .max = 126 },
287 .m1 = { .min = 12, .max = 22 },
288 .m2 = { .min = 5, .max = 9 },
289 .p = { .min = 14, .max = 42 },
290 .p1 = { .min = 2, .max = 6 },
291 .p2 = { .dot_limit = 225000,
292 .p2_slow = 7, .p2_fast = 7 },
295 static const intel_limit_t intel_limits_vlv_dac = {
296 .dot = { .min = 25000, .max = 270000 },
297 .vco = { .min = 4000000, .max = 6000000 },
298 .n = { .min = 1, .max = 7 },
299 .m = { .min = 22, .max = 450 }, /* guess */
300 .m1 = { .min = 2, .max = 3 },
301 .m2 = { .min = 11, .max = 156 },
302 .p = { .min = 10, .max = 30 },
303 .p1 = { .min = 1, .max = 3 },
304 .p2 = { .dot_limit = 270000,
305 .p2_slow = 2, .p2_fast = 20 },
308 static const intel_limit_t intel_limits_vlv_hdmi = {
309 .dot = { .min = 25000, .max = 270000 },
310 .vco = { .min = 4000000, .max = 6000000 },
311 .n = { .min = 1, .max = 7 },
312 .m = { .min = 60, .max = 300 }, /* guess */
313 .m1 = { .min = 2, .max = 3 },
314 .m2 = { .min = 11, .max = 156 },
315 .p = { .min = 10, .max = 30 },
316 .p1 = { .min = 2, .max = 3 },
317 .p2 = { .dot_limit = 270000,
318 .p2_slow = 2, .p2_fast = 20 },
321 static const intel_limit_t intel_limits_vlv_dp = {
322 .dot = { .min = 25000, .max = 270000 },
323 .vco = { .min = 4000000, .max = 6000000 },
324 .n = { .min = 1, .max = 7 },
325 .m = { .min = 22, .max = 450 },
326 .m1 = { .min = 2, .max = 3 },
327 .m2 = { .min = 11, .max = 156 },
328 .p = { .min = 10, .max = 30 },
329 .p1 = { .min = 1, .max = 3 },
330 .p2 = { .dot_limit = 270000,
331 .p2_slow = 2, .p2_fast = 20 },
334 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
337 struct drm_device *dev = crtc->dev;
338 const intel_limit_t *limit;
340 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
341 if (intel_is_dual_link_lvds(dev)) {
342 if (refclk == 100000)
343 limit = &intel_limits_ironlake_dual_lvds_100m;
345 limit = &intel_limits_ironlake_dual_lvds;
347 if (refclk == 100000)
348 limit = &intel_limits_ironlake_single_lvds_100m;
350 limit = &intel_limits_ironlake_single_lvds;
353 limit = &intel_limits_ironlake_dac;
358 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
360 struct drm_device *dev = crtc->dev;
361 const intel_limit_t *limit;
363 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
364 if (intel_is_dual_link_lvds(dev))
365 limit = &intel_limits_g4x_dual_channel_lvds;
367 limit = &intel_limits_g4x_single_channel_lvds;
368 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
369 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
370 limit = &intel_limits_g4x_hdmi;
371 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
372 limit = &intel_limits_g4x_sdvo;
373 } else /* The option is for other outputs */
374 limit = &intel_limits_i9xx_sdvo;
379 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
381 struct drm_device *dev = crtc->dev;
382 const intel_limit_t *limit;
384 if (HAS_PCH_SPLIT(dev))
385 limit = intel_ironlake_limit(crtc, refclk);
386 else if (IS_G4X(dev)) {
387 limit = intel_g4x_limit(crtc);
388 } else if (IS_PINEVIEW(dev)) {
389 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
390 limit = &intel_limits_pineview_lvds;
392 limit = &intel_limits_pineview_sdvo;
393 } else if (IS_VALLEYVIEW(dev)) {
394 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
395 limit = &intel_limits_vlv_dac;
396 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
397 limit = &intel_limits_vlv_hdmi;
399 limit = &intel_limits_vlv_dp;
400 } else if (!IS_GEN2(dev)) {
401 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
402 limit = &intel_limits_i9xx_lvds;
404 limit = &intel_limits_i9xx_sdvo;
406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
407 limit = &intel_limits_i8xx_lvds;
409 limit = &intel_limits_i8xx_dvo;
414 /* m1 is reserved as 0 in Pineview, n is a ring counter */
415 static void pineview_clock(int refclk, intel_clock_t *clock)
417 clock->m = clock->m2 + 2;
418 clock->p = clock->p1 * clock->p2;
419 clock->vco = refclk * clock->m / clock->n;
420 clock->dot = clock->vco / clock->p;
423 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
425 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
428 static void i9xx_clock(int refclk, intel_clock_t *clock)
430 clock->m = i9xx_dpll_compute_m(clock);
431 clock->p = clock->p1 * clock->p2;
432 clock->vco = refclk * clock->m / (clock->n + 2);
433 clock->dot = clock->vco / clock->p;
437 * Returns whether any output on the specified pipe is of the specified type
439 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
441 struct drm_device *dev = crtc->dev;
442 struct intel_encoder *encoder;
444 for_each_encoder_on_crtc(dev, crtc, encoder)
445 if (encoder->type == type)
451 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
453 * Returns whether the given set of divisors are valid for a given refclk with
454 * the given connectors.
457 static bool intel_PLL_is_valid(struct drm_device *dev,
458 const intel_limit_t *limit,
459 const intel_clock_t *clock)
461 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
462 INTELPllInvalid("p1 out of range\n");
463 if (clock->p < limit->p.min || limit->p.max < clock->p)
464 INTELPllInvalid("p out of range\n");
465 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
466 INTELPllInvalid("m2 out of range\n");
467 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
468 INTELPllInvalid("m1 out of range\n");
469 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
470 INTELPllInvalid("m1 <= m2\n");
471 if (clock->m < limit->m.min || limit->m.max < clock->m)
472 INTELPllInvalid("m out of range\n");
473 if (clock->n < limit->n.min || limit->n.max < clock->n)
474 INTELPllInvalid("n out of range\n");
475 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
476 INTELPllInvalid("vco out of range\n");
477 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
478 * connector, etc., rather than just a single range.
480 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
481 INTELPllInvalid("dot out of range\n");
487 i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
488 int target, int refclk, intel_clock_t *match_clock,
489 intel_clock_t *best_clock)
491 struct drm_device *dev = crtc->dev;
495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
497 * For LVDS just rely on its current settings for dual-channel.
498 * We haven't figured out how to reliably set up different
499 * single/dual channel state, if we even can.
501 if (intel_is_dual_link_lvds(dev))
502 clock.p2 = limit->p2.p2_fast;
504 clock.p2 = limit->p2.p2_slow;
506 if (target < limit->p2.dot_limit)
507 clock.p2 = limit->p2.p2_slow;
509 clock.p2 = limit->p2.p2_fast;
512 memset(best_clock, 0, sizeof(*best_clock));
514 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
516 for (clock.m2 = limit->m2.min;
517 clock.m2 <= limit->m2.max; clock.m2++) {
518 if (clock.m2 >= clock.m1)
520 for (clock.n = limit->n.min;
521 clock.n <= limit->n.max; clock.n++) {
522 for (clock.p1 = limit->p1.min;
523 clock.p1 <= limit->p1.max; clock.p1++) {
526 i9xx_clock(refclk, &clock);
527 if (!intel_PLL_is_valid(dev, limit,
531 clock.p != match_clock->p)
534 this_err = abs(clock.dot - target);
535 if (this_err < err) {
544 return (err != target);
548 pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
549 int target, int refclk, intel_clock_t *match_clock,
550 intel_clock_t *best_clock)
552 struct drm_device *dev = crtc->dev;
556 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
558 * For LVDS just rely on its current settings for dual-channel.
559 * We haven't figured out how to reliably set up different
560 * single/dual channel state, if we even can.
562 if (intel_is_dual_link_lvds(dev))
563 clock.p2 = limit->p2.p2_fast;
565 clock.p2 = limit->p2.p2_slow;
567 if (target < limit->p2.dot_limit)
568 clock.p2 = limit->p2.p2_slow;
570 clock.p2 = limit->p2.p2_fast;
573 memset(best_clock, 0, sizeof(*best_clock));
575 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
577 for (clock.m2 = limit->m2.min;
578 clock.m2 <= limit->m2.max; clock.m2++) {
579 for (clock.n = limit->n.min;
580 clock.n <= limit->n.max; clock.n++) {
581 for (clock.p1 = limit->p1.min;
582 clock.p1 <= limit->p1.max; clock.p1++) {
585 pineview_clock(refclk, &clock);
586 if (!intel_PLL_is_valid(dev, limit,
590 clock.p != match_clock->p)
593 this_err = abs(clock.dot - target);
594 if (this_err < err) {
603 return (err != target);
607 g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
608 int target, int refclk, intel_clock_t *match_clock,
609 intel_clock_t *best_clock)
611 struct drm_device *dev = crtc->dev;
615 /* approximately equals target * 0.00585 */
616 int err_most = (target >> 8) + (target >> 9);
619 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
620 if (intel_is_dual_link_lvds(dev))
621 clock.p2 = limit->p2.p2_fast;
623 clock.p2 = limit->p2.p2_slow;
625 if (target < limit->p2.dot_limit)
626 clock.p2 = limit->p2.p2_slow;
628 clock.p2 = limit->p2.p2_fast;
631 memset(best_clock, 0, sizeof(*best_clock));
632 max_n = limit->n.max;
633 /* based on hardware requirement, prefer smaller n to precision */
634 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
635 /* based on hardware requirement, prefere larger m1,m2 */
636 for (clock.m1 = limit->m1.max;
637 clock.m1 >= limit->m1.min; clock.m1--) {
638 for (clock.m2 = limit->m2.max;
639 clock.m2 >= limit->m2.min; clock.m2--) {
640 for (clock.p1 = limit->p1.max;
641 clock.p1 >= limit->p1.min; clock.p1--) {
644 i9xx_clock(refclk, &clock);
645 if (!intel_PLL_is_valid(dev, limit,
649 this_err = abs(clock.dot - target);
650 if (this_err < err_most) {
664 vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
665 int target, int refclk, intel_clock_t *match_clock,
666 intel_clock_t *best_clock)
668 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
670 u32 updrate, minupdate, fracbits, p;
671 unsigned long bestppm, ppm, absppm;
675 dotclk = target * 1000;
678 fastclk = dotclk / (2*100);
682 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
683 bestm1 = bestm2 = bestp1 = bestp2 = 0;
685 /* based on hardware requirement, prefer smaller n to precision */
686 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
687 updrate = refclk / n;
688 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
689 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
693 /* based on hardware requirement, prefer bigger m1,m2 values */
694 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
695 m2 = (((2*(fastclk * p * n / m1 )) +
696 refclk) / (2*refclk));
699 if (vco >= limit->vco.min && vco < limit->vco.max) {
700 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
701 absppm = (ppm > 0) ? ppm : (-ppm);
702 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
706 if (absppm < bestppm - 10) {
723 best_clock->n = bestn;
724 best_clock->m1 = bestm1;
725 best_clock->m2 = bestm2;
726 best_clock->p1 = bestp1;
727 best_clock->p2 = bestp2;
732 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
735 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
738 return intel_crtc->config.cpu_transcoder;
741 static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
743 struct drm_i915_private *dev_priv = dev->dev_private;
744 u32 frame, frame_reg = PIPEFRAME(pipe);
746 frame = I915_READ(frame_reg);
748 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
749 DRM_DEBUG_KMS("vblank wait timed out\n");
753 * intel_wait_for_vblank - wait for vblank on a given pipe
755 * @pipe: pipe to wait for
757 * Wait for vblank to occur on a given pipe. Needed for various bits of
760 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
762 struct drm_i915_private *dev_priv = dev->dev_private;
763 int pipestat_reg = PIPESTAT(pipe);
765 if (INTEL_INFO(dev)->gen >= 5) {
766 ironlake_wait_for_vblank(dev, pipe);
770 /* Clear existing vblank status. Note this will clear any other
771 * sticky status fields as well.
773 * This races with i915_driver_irq_handler() with the result
774 * that either function could miss a vblank event. Here it is not
775 * fatal, as we will either wait upon the next vblank interrupt or
776 * timeout. Generally speaking intel_wait_for_vblank() is only
777 * called during modeset at which time the GPU should be idle and
778 * should *not* be performing page flips and thus not waiting on
780 * Currently, the result of us stealing a vblank from the irq
781 * handler is that a single frame will be skipped during swapbuffers.
783 I915_WRITE(pipestat_reg,
784 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
786 /* Wait for vblank interrupt bit to set */
787 if (wait_for(I915_READ(pipestat_reg) &
788 PIPE_VBLANK_INTERRUPT_STATUS,
790 DRM_DEBUG_KMS("vblank wait timed out\n");
794 * intel_wait_for_pipe_off - wait for pipe to turn off
796 * @pipe: pipe to wait for
798 * After disabling a pipe, we can't wait for vblank in the usual way,
799 * spinning on the vblank interrupt status bit, since we won't actually
800 * see an interrupt when the pipe is disabled.
803 * wait for the pipe register state bit to turn off
806 * wait for the display line value to settle (it usually
807 * ends up stopping at the start of the next frame).
810 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
812 struct drm_i915_private *dev_priv = dev->dev_private;
813 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
816 if (INTEL_INFO(dev)->gen >= 4) {
817 int reg = PIPECONF(cpu_transcoder);
819 /* Wait for the Pipe State to go off */
820 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
822 WARN(1, "pipe_off wait timed out\n");
824 u32 last_line, line_mask;
825 int reg = PIPEDSL(pipe);
826 unsigned long timeout = jiffies + msecs_to_jiffies(100);
829 line_mask = DSL_LINEMASK_GEN2;
831 line_mask = DSL_LINEMASK_GEN3;
833 /* Wait for the display line to settle */
835 last_line = I915_READ(reg) & line_mask;
837 } while (((I915_READ(reg) & line_mask) != last_line) &&
838 time_after(timeout, jiffies));
839 if (time_after(jiffies, timeout))
840 WARN(1, "pipe_off wait timed out\n");
845 * ibx_digital_port_connected - is the specified port connected?
846 * @dev_priv: i915 private structure
847 * @port: the port to test
849 * Returns true if @port is connected, false otherwise.
851 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
852 struct intel_digital_port *port)
856 if (HAS_PCH_IBX(dev_priv->dev)) {
859 bit = SDE_PORTB_HOTPLUG;
862 bit = SDE_PORTC_HOTPLUG;
865 bit = SDE_PORTD_HOTPLUG;
873 bit = SDE_PORTB_HOTPLUG_CPT;
876 bit = SDE_PORTC_HOTPLUG_CPT;
879 bit = SDE_PORTD_HOTPLUG_CPT;
886 return I915_READ(SDEISR) & bit;
889 static const char *state_string(bool enabled)
891 return enabled ? "on" : "off";
894 /* Only for pre-ILK configs */
895 static void assert_pll(struct drm_i915_private *dev_priv,
896 enum pipe pipe, bool state)
903 val = I915_READ(reg);
904 cur_state = !!(val & DPLL_VCO_ENABLE);
905 WARN(cur_state != state,
906 "PLL state assertion failure (expected %s, current %s)\n",
907 state_string(state), state_string(cur_state));
909 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
910 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
913 static void assert_pch_pll(struct drm_i915_private *dev_priv,
914 struct intel_pch_pll *pll,
915 struct intel_crtc *crtc,
921 if (HAS_PCH_LPT(dev_priv->dev)) {
922 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
927 "asserting PCH PLL %s with no PLL\n", state_string(state)))
930 val = I915_READ(pll->pll_reg);
931 cur_state = !!(val & DPLL_VCO_ENABLE);
932 WARN(cur_state != state,
933 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
934 pll->pll_reg, state_string(state), state_string(cur_state), val);
936 /* Make sure the selected PLL is correctly attached to the transcoder */
937 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
940 pch_dpll = I915_READ(PCH_DPLL_SEL);
941 cur_state = pll->pll_reg == _PCH_DPLL_B;
942 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
943 "PLL[%d] not attached to this transcoder %c: %08x\n",
944 cur_state, pipe_name(crtc->pipe), pch_dpll)) {
945 cur_state = !!(val >> (4*crtc->pipe + 3));
946 WARN(cur_state != state,
947 "PLL[%d] not %s on this transcoder %c: %08x\n",
948 pll->pll_reg == _PCH_DPLL_B,
950 pipe_name(crtc->pipe),
955 #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
956 #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
958 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
959 enum pipe pipe, bool state)
964 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
967 if (HAS_DDI(dev_priv->dev)) {
968 /* DDI does not have a specific FDI_TX register */
969 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
970 val = I915_READ(reg);
971 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
973 reg = FDI_TX_CTL(pipe);
974 val = I915_READ(reg);
975 cur_state = !!(val & FDI_TX_ENABLE);
977 WARN(cur_state != state,
978 "FDI TX state assertion failure (expected %s, current %s)\n",
979 state_string(state), state_string(cur_state));
981 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
982 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
984 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
985 enum pipe pipe, bool state)
991 reg = FDI_RX_CTL(pipe);
992 val = I915_READ(reg);
993 cur_state = !!(val & FDI_RX_ENABLE);
994 WARN(cur_state != state,
995 "FDI RX state assertion failure (expected %s, current %s)\n",
996 state_string(state), state_string(cur_state));
998 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
999 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1001 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1007 /* ILK FDI PLL is always enabled */
1008 if (dev_priv->info->gen == 5)
1011 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1012 if (HAS_DDI(dev_priv->dev))
1015 reg = FDI_TX_CTL(pipe);
1016 val = I915_READ(reg);
1017 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1020 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1026 reg = FDI_RX_CTL(pipe);
1027 val = I915_READ(reg);
1028 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1031 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1034 int pp_reg, lvds_reg;
1036 enum pipe panel_pipe = PIPE_A;
1039 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1040 pp_reg = PCH_PP_CONTROL;
1041 lvds_reg = PCH_LVDS;
1043 pp_reg = PP_CONTROL;
1047 val = I915_READ(pp_reg);
1048 if (!(val & PANEL_POWER_ON) ||
1049 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1052 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1053 panel_pipe = PIPE_B;
1055 WARN(panel_pipe == pipe && locked,
1056 "panel assertion failure, pipe %c regs locked\n",
1060 void assert_pipe(struct drm_i915_private *dev_priv,
1061 enum pipe pipe, bool state)
1066 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1069 /* if we need the pipe A quirk it must be always on */
1070 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1073 if (!intel_display_power_enabled(dev_priv->dev,
1074 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1077 reg = PIPECONF(cpu_transcoder);
1078 val = I915_READ(reg);
1079 cur_state = !!(val & PIPECONF_ENABLE);
1082 WARN(cur_state != state,
1083 "pipe %c assertion failure (expected %s, current %s)\n",
1084 pipe_name(pipe), state_string(state), state_string(cur_state));
1087 static void assert_plane(struct drm_i915_private *dev_priv,
1088 enum plane plane, bool state)
1094 reg = DSPCNTR(plane);
1095 val = I915_READ(reg);
1096 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1097 WARN(cur_state != state,
1098 "plane %c assertion failure (expected %s, current %s)\n",
1099 plane_name(plane), state_string(state), state_string(cur_state));
1102 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1103 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1105 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1108 struct drm_device *dev = dev_priv->dev;
1113 /* Primary planes are fixed to pipes on gen4+ */
1114 if (INTEL_INFO(dev)->gen >= 4) {
1115 reg = DSPCNTR(pipe);
1116 val = I915_READ(reg);
1117 WARN((val & DISPLAY_PLANE_ENABLE),
1118 "plane %c assertion failure, should be disabled but not\n",
1123 /* Need to check both planes against the pipe */
1124 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
1126 val = I915_READ(reg);
1127 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1128 DISPPLANE_SEL_PIPE_SHIFT;
1129 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1130 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1131 plane_name(i), pipe_name(pipe));
1135 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1138 struct drm_device *dev = dev_priv->dev;
1142 if (IS_VALLEYVIEW(dev)) {
1143 for (i = 0; i < dev_priv->num_plane; i++) {
1144 reg = SPCNTR(pipe, i);
1145 val = I915_READ(reg);
1146 WARN((val & SP_ENABLE),
1147 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1148 sprite_name(pipe, i), pipe_name(pipe));
1150 } else if (INTEL_INFO(dev)->gen >= 7) {
1152 val = I915_READ(reg);
1153 WARN((val & SPRITE_ENABLE),
1154 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1155 plane_name(pipe), pipe_name(pipe));
1156 } else if (INTEL_INFO(dev)->gen >= 5) {
1157 reg = DVSCNTR(pipe);
1158 val = I915_READ(reg);
1159 WARN((val & DVS_ENABLE),
1160 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1161 plane_name(pipe), pipe_name(pipe));
1165 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1170 if (HAS_PCH_LPT(dev_priv->dev)) {
1171 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1175 val = I915_READ(PCH_DREF_CONTROL);
1176 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1177 DREF_SUPERSPREAD_SOURCE_MASK));
1178 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1181 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1188 reg = PCH_TRANSCONF(pipe);
1189 val = I915_READ(reg);
1190 enabled = !!(val & TRANS_ENABLE);
1192 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1196 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, u32 port_sel, u32 val)
1199 if ((val & DP_PORT_EN) == 0)
1202 if (HAS_PCH_CPT(dev_priv->dev)) {
1203 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1204 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1205 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1208 if ((val & DP_PIPE_MASK) != (pipe << 30))
1214 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1215 enum pipe pipe, u32 val)
1217 if ((val & SDVO_ENABLE) == 0)
1220 if (HAS_PCH_CPT(dev_priv->dev)) {
1221 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1224 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1230 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, u32 val)
1233 if ((val & LVDS_PORT_EN) == 0)
1236 if (HAS_PCH_CPT(dev_priv->dev)) {
1237 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1240 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1246 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, u32 val)
1249 if ((val & ADPA_DAC_ENABLE) == 0)
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
1252 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1255 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1261 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1262 enum pipe pipe, int reg, u32 port_sel)
1264 u32 val = I915_READ(reg);
1265 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1266 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1267 reg, pipe_name(pipe));
1269 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1270 && (val & DP_PIPEB_SELECT),
1271 "IBX PCH dp port still using transcoder B\n");
1274 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1275 enum pipe pipe, int reg)
1277 u32 val = I915_READ(reg);
1278 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1279 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1280 reg, pipe_name(pipe));
1282 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1283 && (val & SDVO_PIPE_B_SELECT),
1284 "IBX PCH hdmi port still using transcoder B\n");
1287 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1293 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1294 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1295 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1298 val = I915_READ(reg);
1299 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1300 "PCH VGA enabled on transcoder %c, should be disabled\n",
1304 val = I915_READ(reg);
1305 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1306 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1309 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1310 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1311 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1315 * intel_enable_pll - enable a PLL
1316 * @dev_priv: i915 private structure
1317 * @pipe: pipe PLL to enable
1319 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1320 * make sure the PLL reg is writable first though, since the panel write
1321 * protect mechanism may be enabled.
1323 * Note! This is for pre-ILK only.
1325 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1327 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1332 assert_pipe_disabled(dev_priv, pipe);
1334 /* No really, not for ILK+ */
1335 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1337 /* PLL is protected by panel, make sure we can write it */
1338 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1339 assert_panel_unlocked(dev_priv, pipe);
1342 val = I915_READ(reg);
1343 val |= DPLL_VCO_ENABLE;
1345 /* We do this three times for luck */
1346 I915_WRITE(reg, val);
1348 udelay(150); /* wait for warmup */
1349 I915_WRITE(reg, val);
1351 udelay(150); /* wait for warmup */
1352 I915_WRITE(reg, val);
1354 udelay(150); /* wait for warmup */
1358 * intel_disable_pll - disable a PLL
1359 * @dev_priv: i915 private structure
1360 * @pipe: pipe PLL to disable
1362 * Disable the PLL for @pipe, making sure the pipe is off first.
1364 * Note! This is for pre-ILK only.
1366 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1371 /* Don't disable pipe A or pipe A PLLs if needed */
1372 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1375 /* Make sure the pipe isn't still relying on us */
1376 assert_pipe_disabled(dev_priv, pipe);
1379 val = I915_READ(reg);
1380 val &= ~DPLL_VCO_ENABLE;
1381 I915_WRITE(reg, val);
1385 void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1390 port_mask = DPLL_PORTB_READY_MASK;
1392 port_mask = DPLL_PORTC_READY_MASK;
1394 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1395 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1396 'B' + port, I915_READ(DPLL(0)));
1400 * ironlake_enable_pch_pll - enable PCH PLL
1401 * @dev_priv: i915 private structure
1402 * @pipe: pipe PLL to enable
1404 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1405 * drives the transcoder clock.
1407 static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
1409 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1410 struct intel_pch_pll *pll;
1414 /* PCH PLLs only available on ILK, SNB and IVB */
1415 BUG_ON(dev_priv->info->gen < 5);
1416 pll = intel_crtc->pch_pll;
1420 if (WARN_ON(pll->refcount == 0))
1423 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1424 pll->pll_reg, pll->active, pll->on,
1425 intel_crtc->base.base.id);
1427 /* PCH refclock must be enabled first */
1428 assert_pch_refclk_enabled(dev_priv);
1430 if (pll->active++) {
1432 assert_pch_pll_enabled(dev_priv, pll, NULL);
1437 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1440 val = I915_READ(reg);
1441 val |= DPLL_VCO_ENABLE;
1442 I915_WRITE(reg, val);
1449 static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1451 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1452 struct intel_pch_pll *pll = intel_crtc->pch_pll;
1456 /* PCH only available on ILK+ */
1457 BUG_ON(dev_priv->info->gen < 5);
1461 if (WARN_ON(pll->refcount == 0))
1464 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1465 pll->pll_reg, pll->active, pll->on,
1466 intel_crtc->base.base.id);
1468 if (WARN_ON(pll->active == 0)) {
1469 assert_pch_pll_disabled(dev_priv, pll, NULL);
1473 assert_pch_pll_enabled(dev_priv, pll, NULL);
1478 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1480 /* Make sure transcoder isn't still depending on us */
1481 assert_pch_transcoder_disabled(dev_priv, intel_crtc->pipe);
1484 val = I915_READ(reg);
1485 val &= ~DPLL_VCO_ENABLE;
1486 I915_WRITE(reg, val);
1493 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1496 struct drm_device *dev = dev_priv->dev;
1497 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1498 uint32_t reg, val, pipeconf_val;
1500 /* PCH only available on ILK+ */
1501 BUG_ON(dev_priv->info->gen < 5);
1503 /* Make sure PCH DPLL is enabled */
1504 assert_pch_pll_enabled(dev_priv,
1505 to_intel_crtc(crtc)->pch_pll,
1506 to_intel_crtc(crtc));
1508 /* FDI must be feeding us bits for PCH ports */
1509 assert_fdi_tx_enabled(dev_priv, pipe);
1510 assert_fdi_rx_enabled(dev_priv, pipe);
1512 if (HAS_PCH_CPT(dev)) {
1513 /* Workaround: Set the timing override bit before enabling the
1514 * pch transcoder. */
1515 reg = TRANS_CHICKEN2(pipe);
1516 val = I915_READ(reg);
1517 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1518 I915_WRITE(reg, val);
1521 reg = PCH_TRANSCONF(pipe);
1522 val = I915_READ(reg);
1523 pipeconf_val = I915_READ(PIPECONF(pipe));
1525 if (HAS_PCH_IBX(dev_priv->dev)) {
1527 * make the BPC in transcoder be consistent with
1528 * that in pipeconf reg.
1530 val &= ~PIPECONF_BPC_MASK;
1531 val |= pipeconf_val & PIPECONF_BPC_MASK;
1534 val &= ~TRANS_INTERLACE_MASK;
1535 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1536 if (HAS_PCH_IBX(dev_priv->dev) &&
1537 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1538 val |= TRANS_LEGACY_INTERLACED_ILK;
1540 val |= TRANS_INTERLACED;
1542 val |= TRANS_PROGRESSIVE;
1544 I915_WRITE(reg, val | TRANS_ENABLE);
1545 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1546 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1549 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1550 enum transcoder cpu_transcoder)
1552 u32 val, pipeconf_val;
1554 /* PCH only available on ILK+ */
1555 BUG_ON(dev_priv->info->gen < 5);
1557 /* FDI must be feeding us bits for PCH ports */
1558 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1559 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1561 /* Workaround: set timing override bit. */
1562 val = I915_READ(_TRANSA_CHICKEN2);
1563 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1564 I915_WRITE(_TRANSA_CHICKEN2, val);
1567 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1569 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1570 PIPECONF_INTERLACED_ILK)
1571 val |= TRANS_INTERLACED;
1573 val |= TRANS_PROGRESSIVE;
1575 I915_WRITE(LPT_TRANSCONF, val);
1576 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1577 DRM_ERROR("Failed to enable PCH transcoder\n");
1580 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1583 struct drm_device *dev = dev_priv->dev;
1586 /* FDI relies on the transcoder */
1587 assert_fdi_tx_disabled(dev_priv, pipe);
1588 assert_fdi_rx_disabled(dev_priv, pipe);
1590 /* Ports must be off as well */
1591 assert_pch_ports_disabled(dev_priv, pipe);
1593 reg = PCH_TRANSCONF(pipe);
1594 val = I915_READ(reg);
1595 val &= ~TRANS_ENABLE;
1596 I915_WRITE(reg, val);
1597 /* wait for PCH transcoder off, transcoder state */
1598 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1599 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1601 if (!HAS_PCH_IBX(dev)) {
1602 /* Workaround: Clear the timing override chicken bit again. */
1603 reg = TRANS_CHICKEN2(pipe);
1604 val = I915_READ(reg);
1605 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1606 I915_WRITE(reg, val);
1610 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1614 val = I915_READ(LPT_TRANSCONF);
1615 val &= ~TRANS_ENABLE;
1616 I915_WRITE(LPT_TRANSCONF, val);
1617 /* wait for PCH transcoder off, transcoder state */
1618 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1619 DRM_ERROR("Failed to disable PCH transcoder\n");
1621 /* Workaround: clear timing override bit. */
1622 val = I915_READ(_TRANSA_CHICKEN2);
1623 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1624 I915_WRITE(_TRANSA_CHICKEN2, val);
1628 * intel_enable_pipe - enable a pipe, asserting requirements
1629 * @dev_priv: i915 private structure
1630 * @pipe: pipe to enable
1631 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1633 * Enable @pipe, making sure that various hardware specific requirements
1634 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1636 * @pipe should be %PIPE_A or %PIPE_B.
1638 * Will wait until the pipe is actually running (i.e. first vblank) before
1641 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1644 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1646 enum pipe pch_transcoder;
1650 assert_planes_disabled(dev_priv, pipe);
1651 assert_sprites_disabled(dev_priv, pipe);
1653 if (HAS_PCH_LPT(dev_priv->dev))
1654 pch_transcoder = TRANSCODER_A;
1656 pch_transcoder = pipe;
1659 * A pipe without a PLL won't actually be able to drive bits from
1660 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1663 if (!HAS_PCH_SPLIT(dev_priv->dev))
1664 assert_pll_enabled(dev_priv, pipe);
1667 /* if driving the PCH, we need FDI enabled */
1668 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1669 assert_fdi_tx_pll_enabled(dev_priv,
1670 (enum pipe) cpu_transcoder);
1672 /* FIXME: assert CPU port conditions for SNB+ */
1675 reg = PIPECONF(cpu_transcoder);
1676 val = I915_READ(reg);
1677 if (val & PIPECONF_ENABLE)
1680 I915_WRITE(reg, val | PIPECONF_ENABLE);
1681 intel_wait_for_vblank(dev_priv->dev, pipe);
1685 * intel_disable_pipe - disable a pipe, asserting requirements
1686 * @dev_priv: i915 private structure
1687 * @pipe: pipe to disable
1689 * Disable @pipe, making sure that various hardware specific requirements
1690 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1692 * @pipe should be %PIPE_A or %PIPE_B.
1694 * Will wait until the pipe has shut down before returning.
1696 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1699 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1705 * Make sure planes won't keep trying to pump pixels to us,
1706 * or we might hang the display.
1708 assert_planes_disabled(dev_priv, pipe);
1709 assert_sprites_disabled(dev_priv, pipe);
1711 /* Don't disable pipe A or pipe A PLLs if needed */
1712 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1715 reg = PIPECONF(cpu_transcoder);
1716 val = I915_READ(reg);
1717 if ((val & PIPECONF_ENABLE) == 0)
1720 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1721 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1725 * Plane regs are double buffered, going from enabled->disabled needs a
1726 * trigger in order to latch. The display address reg provides this.
1728 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1731 if (dev_priv->info->gen >= 4)
1732 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1734 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1738 * intel_enable_plane - enable a display plane on a given pipe
1739 * @dev_priv: i915 private structure
1740 * @plane: plane to enable
1741 * @pipe: pipe being fed
1743 * Enable @plane on @pipe, making sure that @pipe is running first.
1745 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1746 enum plane plane, enum pipe pipe)
1751 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1752 assert_pipe_enabled(dev_priv, pipe);
1754 reg = DSPCNTR(plane);
1755 val = I915_READ(reg);
1756 if (val & DISPLAY_PLANE_ENABLE)
1759 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1760 intel_flush_display_plane(dev_priv, plane);
1761 intel_wait_for_vblank(dev_priv->dev, pipe);
1765 * intel_disable_plane - disable a display plane
1766 * @dev_priv: i915 private structure
1767 * @plane: plane to disable
1768 * @pipe: pipe consuming the data
1770 * Disable @plane; should be an independent operation.
1772 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1773 enum plane plane, enum pipe pipe)
1778 reg = DSPCNTR(plane);
1779 val = I915_READ(reg);
1780 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1783 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1784 intel_flush_display_plane(dev_priv, plane);
1785 intel_wait_for_vblank(dev_priv->dev, pipe);
1788 static bool need_vtd_wa(struct drm_device *dev)
1790 #ifdef CONFIG_INTEL_IOMMU
1791 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1798 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1799 struct drm_i915_gem_object *obj,
1800 struct intel_ring_buffer *pipelined)
1802 struct drm_i915_private *dev_priv = dev->dev_private;
1806 switch (obj->tiling_mode) {
1807 case I915_TILING_NONE:
1808 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1809 alignment = 128 * 1024;
1810 else if (INTEL_INFO(dev)->gen >= 4)
1811 alignment = 4 * 1024;
1813 alignment = 64 * 1024;
1816 /* pin() will align the object as required by fence */
1820 /* Despite that we check this in framebuffer_init userspace can
1821 * screw us over and change the tiling after the fact. Only
1822 * pinned buffers can't change their tiling. */
1823 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
1829 /* Note that the w/a also requires 64 PTE of padding following the
1830 * bo. We currently fill all unused PTE with the shadow page and so
1831 * we should always have valid PTE following the scanout preventing
1834 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1835 alignment = 256 * 1024;
1837 dev_priv->mm.interruptible = false;
1838 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1840 goto err_interruptible;
1842 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1843 * fence, whereas 965+ only requires a fence if using
1844 * framebuffer compression. For simplicity, we always install
1845 * a fence as the cost is not that onerous.
1847 ret = i915_gem_object_get_fence(obj);
1851 i915_gem_object_pin_fence(obj);
1853 dev_priv->mm.interruptible = true;
1857 i915_gem_object_unpin(obj);
1859 dev_priv->mm.interruptible = true;
1863 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1865 i915_gem_object_unpin_fence(obj);
1866 i915_gem_object_unpin(obj);
1869 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1870 * is assumed to be a power-of-two. */
1871 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1872 unsigned int tiling_mode,
1876 if (tiling_mode != I915_TILING_NONE) {
1877 unsigned int tile_rows, tiles;
1882 tiles = *x / (512/cpp);
1885 return tile_rows * pitch * 8 + tiles * 4096;
1887 unsigned int offset;
1889 offset = *y * pitch + *x * cpp;
1891 *x = (offset & 4095) / cpp;
1892 return offset & -4096;
1896 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1899 struct drm_device *dev = crtc->dev;
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1902 struct intel_framebuffer *intel_fb;
1903 struct drm_i915_gem_object *obj;
1904 int plane = intel_crtc->plane;
1905 unsigned long linear_offset;
1914 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1918 intel_fb = to_intel_framebuffer(fb);
1919 obj = intel_fb->obj;
1921 reg = DSPCNTR(plane);
1922 dspcntr = I915_READ(reg);
1923 /* Mask out pixel format bits in case we change it */
1924 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1925 switch (fb->pixel_format) {
1927 dspcntr |= DISPPLANE_8BPP;
1929 case DRM_FORMAT_XRGB1555:
1930 case DRM_FORMAT_ARGB1555:
1931 dspcntr |= DISPPLANE_BGRX555;
1933 case DRM_FORMAT_RGB565:
1934 dspcntr |= DISPPLANE_BGRX565;
1936 case DRM_FORMAT_XRGB8888:
1937 case DRM_FORMAT_ARGB8888:
1938 dspcntr |= DISPPLANE_BGRX888;
1940 case DRM_FORMAT_XBGR8888:
1941 case DRM_FORMAT_ABGR8888:
1942 dspcntr |= DISPPLANE_RGBX888;
1944 case DRM_FORMAT_XRGB2101010:
1945 case DRM_FORMAT_ARGB2101010:
1946 dspcntr |= DISPPLANE_BGRX101010;
1948 case DRM_FORMAT_XBGR2101010:
1949 case DRM_FORMAT_ABGR2101010:
1950 dspcntr |= DISPPLANE_RGBX101010;
1956 if (INTEL_INFO(dev)->gen >= 4) {
1957 if (obj->tiling_mode != I915_TILING_NONE)
1958 dspcntr |= DISPPLANE_TILED;
1960 dspcntr &= ~DISPPLANE_TILED;
1964 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1966 I915_WRITE(reg, dspcntr);
1968 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1970 if (INTEL_INFO(dev)->gen >= 4) {
1971 intel_crtc->dspaddr_offset =
1972 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1973 fb->bits_per_pixel / 8,
1975 linear_offset -= intel_crtc->dspaddr_offset;
1977 intel_crtc->dspaddr_offset = linear_offset;
1980 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
1981 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
1982 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1983 if (INTEL_INFO(dev)->gen >= 4) {
1984 I915_MODIFY_DISPBASE(DSPSURF(plane),
1985 obj->gtt_offset + intel_crtc->dspaddr_offset);
1986 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1987 I915_WRITE(DSPLINOFF(plane), linear_offset);
1989 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
1995 static int ironlake_update_plane(struct drm_crtc *crtc,
1996 struct drm_framebuffer *fb, int x, int y)
1998 struct drm_device *dev = crtc->dev;
1999 struct drm_i915_private *dev_priv = dev->dev_private;
2000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2001 struct intel_framebuffer *intel_fb;
2002 struct drm_i915_gem_object *obj;
2003 int plane = intel_crtc->plane;
2004 unsigned long linear_offset;
2014 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2018 intel_fb = to_intel_framebuffer(fb);
2019 obj = intel_fb->obj;
2021 reg = DSPCNTR(plane);
2022 dspcntr = I915_READ(reg);
2023 /* Mask out pixel format bits in case we change it */
2024 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2025 switch (fb->pixel_format) {
2027 dspcntr |= DISPPLANE_8BPP;
2029 case DRM_FORMAT_RGB565:
2030 dspcntr |= DISPPLANE_BGRX565;
2032 case DRM_FORMAT_XRGB8888:
2033 case DRM_FORMAT_ARGB8888:
2034 dspcntr |= DISPPLANE_BGRX888;
2036 case DRM_FORMAT_XBGR8888:
2037 case DRM_FORMAT_ABGR8888:
2038 dspcntr |= DISPPLANE_RGBX888;
2040 case DRM_FORMAT_XRGB2101010:
2041 case DRM_FORMAT_ARGB2101010:
2042 dspcntr |= DISPPLANE_BGRX101010;
2044 case DRM_FORMAT_XBGR2101010:
2045 case DRM_FORMAT_ABGR2101010:
2046 dspcntr |= DISPPLANE_RGBX101010;
2052 if (obj->tiling_mode != I915_TILING_NONE)
2053 dspcntr |= DISPPLANE_TILED;
2055 dspcntr &= ~DISPPLANE_TILED;
2058 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2060 I915_WRITE(reg, dspcntr);
2062 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2063 intel_crtc->dspaddr_offset =
2064 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2065 fb->bits_per_pixel / 8,
2067 linear_offset -= intel_crtc->dspaddr_offset;
2069 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2070 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2071 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2072 I915_MODIFY_DISPBASE(DSPSURF(plane),
2073 obj->gtt_offset + intel_crtc->dspaddr_offset);
2074 if (IS_HASWELL(dev)) {
2075 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2077 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2078 I915_WRITE(DSPLINOFF(plane), linear_offset);
2085 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2087 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2088 int x, int y, enum mode_set_atomic state)
2090 struct drm_device *dev = crtc->dev;
2091 struct drm_i915_private *dev_priv = dev->dev_private;
2093 if (dev_priv->display.disable_fbc)
2094 dev_priv->display.disable_fbc(dev);
2095 intel_increase_pllclock(crtc);
2097 return dev_priv->display.update_plane(crtc, fb, x, y);
2100 void intel_display_handle_reset(struct drm_device *dev)
2102 struct drm_i915_private *dev_priv = dev->dev_private;
2103 struct drm_crtc *crtc;
2106 * Flips in the rings have been nuked by the reset,
2107 * so complete all pending flips so that user space
2108 * will get its events and not get stuck.
2110 * Also update the base address of all primary
2111 * planes to the the last fb to make sure we're
2112 * showing the correct fb after a reset.
2114 * Need to make two loops over the crtcs so that we
2115 * don't try to grab a crtc mutex before the
2116 * pending_flip_queue really got woken up.
2119 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 enum plane plane = intel_crtc->plane;
2123 intel_prepare_page_flip(dev, plane);
2124 intel_finish_page_flip_plane(dev, plane);
2127 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2130 mutex_lock(&crtc->mutex);
2131 if (intel_crtc->active)
2132 dev_priv->display.update_plane(crtc, crtc->fb,
2134 mutex_unlock(&crtc->mutex);
2139 intel_finish_fb(struct drm_framebuffer *old_fb)
2141 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2142 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2143 bool was_interruptible = dev_priv->mm.interruptible;
2146 /* Big Hammer, we also need to ensure that any pending
2147 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2148 * current scanout is retired before unpinning the old
2151 * This should only fail upon a hung GPU, in which case we
2152 * can safely continue.
2154 dev_priv->mm.interruptible = false;
2155 ret = i915_gem_object_finish_gpu(obj);
2156 dev_priv->mm.interruptible = was_interruptible;
2161 static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2163 struct drm_device *dev = crtc->dev;
2164 struct drm_i915_master_private *master_priv;
2165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2167 if (!dev->primary->master)
2170 master_priv = dev->primary->master->driver_priv;
2171 if (!master_priv->sarea_priv)
2174 switch (intel_crtc->pipe) {
2176 master_priv->sarea_priv->pipeA_x = x;
2177 master_priv->sarea_priv->pipeA_y = y;
2180 master_priv->sarea_priv->pipeB_x = x;
2181 master_priv->sarea_priv->pipeB_y = y;
2189 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2190 struct drm_framebuffer *fb)
2192 struct drm_device *dev = crtc->dev;
2193 struct drm_i915_private *dev_priv = dev->dev_private;
2194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2195 struct drm_framebuffer *old_fb;
2200 DRM_ERROR("No FB bound\n");
2204 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2205 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2206 plane_name(intel_crtc->plane),
2207 INTEL_INFO(dev)->num_pipes);
2211 mutex_lock(&dev->struct_mutex);
2212 ret = intel_pin_and_fence_fb_obj(dev,
2213 to_intel_framebuffer(fb)->obj,
2216 mutex_unlock(&dev->struct_mutex);
2217 DRM_ERROR("pin & fence failed\n");
2221 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2223 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2224 mutex_unlock(&dev->struct_mutex);
2225 DRM_ERROR("failed to update base address\n");
2235 if (intel_crtc->active && old_fb != fb)
2236 intel_wait_for_vblank(dev, intel_crtc->pipe);
2237 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2240 intel_update_fbc(dev);
2241 mutex_unlock(&dev->struct_mutex);
2243 intel_crtc_update_sarea_pos(crtc, x, y);
2248 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2250 struct drm_device *dev = crtc->dev;
2251 struct drm_i915_private *dev_priv = dev->dev_private;
2252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2253 int pipe = intel_crtc->pipe;
2256 /* enable normal train */
2257 reg = FDI_TX_CTL(pipe);
2258 temp = I915_READ(reg);
2259 if (IS_IVYBRIDGE(dev)) {
2260 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2261 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2263 temp &= ~FDI_LINK_TRAIN_NONE;
2264 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2266 I915_WRITE(reg, temp);
2268 reg = FDI_RX_CTL(pipe);
2269 temp = I915_READ(reg);
2270 if (HAS_PCH_CPT(dev)) {
2271 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2272 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2274 temp &= ~FDI_LINK_TRAIN_NONE;
2275 temp |= FDI_LINK_TRAIN_NONE;
2277 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2279 /* wait one idle pattern time */
2283 /* IVB wants error correction enabled */
2284 if (IS_IVYBRIDGE(dev))
2285 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2286 FDI_FE_ERRC_ENABLE);
2289 static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2291 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2294 static void ivb_modeset_global_resources(struct drm_device *dev)
2296 struct drm_i915_private *dev_priv = dev->dev_private;
2297 struct intel_crtc *pipe_B_crtc =
2298 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2299 struct intel_crtc *pipe_C_crtc =
2300 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2304 * When everything is off disable fdi C so that we could enable fdi B
2305 * with all lanes. Note that we don't care about enabled pipes without
2306 * an enabled pch encoder.
2308 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2309 !pipe_has_enabled_pch(pipe_C_crtc)) {
2310 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2311 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2313 temp = I915_READ(SOUTH_CHICKEN1);
2314 temp &= ~FDI_BC_BIFURCATION_SELECT;
2315 DRM_DEBUG_KMS("disabling fdi C rx\n");
2316 I915_WRITE(SOUTH_CHICKEN1, temp);
2320 /* The FDI link training functions for ILK/Ibexpeak. */
2321 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2323 struct drm_device *dev = crtc->dev;
2324 struct drm_i915_private *dev_priv = dev->dev_private;
2325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2326 int pipe = intel_crtc->pipe;
2327 int plane = intel_crtc->plane;
2328 u32 reg, temp, tries;
2330 /* FDI needs bits from pipe & plane first */
2331 assert_pipe_enabled(dev_priv, pipe);
2332 assert_plane_enabled(dev_priv, plane);
2334 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2336 reg = FDI_RX_IMR(pipe);
2337 temp = I915_READ(reg);
2338 temp &= ~FDI_RX_SYMBOL_LOCK;
2339 temp &= ~FDI_RX_BIT_LOCK;
2340 I915_WRITE(reg, temp);
2344 /* enable CPU FDI TX and PCH FDI RX */
2345 reg = FDI_TX_CTL(pipe);
2346 temp = I915_READ(reg);
2347 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2348 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2349 temp &= ~FDI_LINK_TRAIN_NONE;
2350 temp |= FDI_LINK_TRAIN_PATTERN_1;
2351 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2353 reg = FDI_RX_CTL(pipe);
2354 temp = I915_READ(reg);
2355 temp &= ~FDI_LINK_TRAIN_NONE;
2356 temp |= FDI_LINK_TRAIN_PATTERN_1;
2357 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2362 /* Ironlake workaround, enable clock pointer after FDI enable*/
2363 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2364 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2365 FDI_RX_PHASE_SYNC_POINTER_EN);
2367 reg = FDI_RX_IIR(pipe);
2368 for (tries = 0; tries < 5; tries++) {
2369 temp = I915_READ(reg);
2370 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2372 if ((temp & FDI_RX_BIT_LOCK)) {
2373 DRM_DEBUG_KMS("FDI train 1 done.\n");
2374 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2379 DRM_ERROR("FDI train 1 fail!\n");
2382 reg = FDI_TX_CTL(pipe);
2383 temp = I915_READ(reg);
2384 temp &= ~FDI_LINK_TRAIN_NONE;
2385 temp |= FDI_LINK_TRAIN_PATTERN_2;
2386 I915_WRITE(reg, temp);
2388 reg = FDI_RX_CTL(pipe);
2389 temp = I915_READ(reg);
2390 temp &= ~FDI_LINK_TRAIN_NONE;
2391 temp |= FDI_LINK_TRAIN_PATTERN_2;
2392 I915_WRITE(reg, temp);
2397 reg = FDI_RX_IIR(pipe);
2398 for (tries = 0; tries < 5; tries++) {
2399 temp = I915_READ(reg);
2400 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2402 if (temp & FDI_RX_SYMBOL_LOCK) {
2403 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2404 DRM_DEBUG_KMS("FDI train 2 done.\n");
2409 DRM_ERROR("FDI train 2 fail!\n");
2411 DRM_DEBUG_KMS("FDI train done\n");
2415 static const int snb_b_fdi_train_param[] = {
2416 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2417 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2418 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2419 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2422 /* The FDI link training functions for SNB/Cougarpoint. */
2423 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2425 struct drm_device *dev = crtc->dev;
2426 struct drm_i915_private *dev_priv = dev->dev_private;
2427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2428 int pipe = intel_crtc->pipe;
2429 u32 reg, temp, i, retry;
2431 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2433 reg = FDI_RX_IMR(pipe);
2434 temp = I915_READ(reg);
2435 temp &= ~FDI_RX_SYMBOL_LOCK;
2436 temp &= ~FDI_RX_BIT_LOCK;
2437 I915_WRITE(reg, temp);
2442 /* enable CPU FDI TX and PCH FDI RX */
2443 reg = FDI_TX_CTL(pipe);
2444 temp = I915_READ(reg);
2445 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2446 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2447 temp &= ~FDI_LINK_TRAIN_NONE;
2448 temp |= FDI_LINK_TRAIN_PATTERN_1;
2449 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2451 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2452 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2454 I915_WRITE(FDI_RX_MISC(pipe),
2455 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2457 reg = FDI_RX_CTL(pipe);
2458 temp = I915_READ(reg);
2459 if (HAS_PCH_CPT(dev)) {
2460 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2461 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2463 temp &= ~FDI_LINK_TRAIN_NONE;
2464 temp |= FDI_LINK_TRAIN_PATTERN_1;
2466 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2471 for (i = 0; i < 4; i++) {
2472 reg = FDI_TX_CTL(pipe);
2473 temp = I915_READ(reg);
2474 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2475 temp |= snb_b_fdi_train_param[i];
2476 I915_WRITE(reg, temp);
2481 for (retry = 0; retry < 5; retry++) {
2482 reg = FDI_RX_IIR(pipe);
2483 temp = I915_READ(reg);
2484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2485 if (temp & FDI_RX_BIT_LOCK) {
2486 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2487 DRM_DEBUG_KMS("FDI train 1 done.\n");
2496 DRM_ERROR("FDI train 1 fail!\n");
2499 reg = FDI_TX_CTL(pipe);
2500 temp = I915_READ(reg);
2501 temp &= ~FDI_LINK_TRAIN_NONE;
2502 temp |= FDI_LINK_TRAIN_PATTERN_2;
2504 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2506 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2508 I915_WRITE(reg, temp);
2510 reg = FDI_RX_CTL(pipe);
2511 temp = I915_READ(reg);
2512 if (HAS_PCH_CPT(dev)) {
2513 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2514 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2516 temp &= ~FDI_LINK_TRAIN_NONE;
2517 temp |= FDI_LINK_TRAIN_PATTERN_2;
2519 I915_WRITE(reg, temp);
2524 for (i = 0; i < 4; i++) {
2525 reg = FDI_TX_CTL(pipe);
2526 temp = I915_READ(reg);
2527 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2528 temp |= snb_b_fdi_train_param[i];
2529 I915_WRITE(reg, temp);
2534 for (retry = 0; retry < 5; retry++) {
2535 reg = FDI_RX_IIR(pipe);
2536 temp = I915_READ(reg);
2537 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2538 if (temp & FDI_RX_SYMBOL_LOCK) {
2539 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2540 DRM_DEBUG_KMS("FDI train 2 done.\n");
2549 DRM_ERROR("FDI train 2 fail!\n");
2551 DRM_DEBUG_KMS("FDI train done.\n");
2554 /* Manual link training for Ivy Bridge A0 parts */
2555 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2557 struct drm_device *dev = crtc->dev;
2558 struct drm_i915_private *dev_priv = dev->dev_private;
2559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2560 int pipe = intel_crtc->pipe;
2563 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2565 reg = FDI_RX_IMR(pipe);
2566 temp = I915_READ(reg);
2567 temp &= ~FDI_RX_SYMBOL_LOCK;
2568 temp &= ~FDI_RX_BIT_LOCK;
2569 I915_WRITE(reg, temp);
2574 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2575 I915_READ(FDI_RX_IIR(pipe)));
2577 /* enable CPU FDI TX and PCH FDI RX */
2578 reg = FDI_TX_CTL(pipe);
2579 temp = I915_READ(reg);
2580 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2581 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2582 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2583 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2584 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2585 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2586 temp |= FDI_COMPOSITE_SYNC;
2587 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2589 I915_WRITE(FDI_RX_MISC(pipe),
2590 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2592 reg = FDI_RX_CTL(pipe);
2593 temp = I915_READ(reg);
2594 temp &= ~FDI_LINK_TRAIN_AUTO;
2595 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2596 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2597 temp |= FDI_COMPOSITE_SYNC;
2598 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2603 for (i = 0; i < 4; i++) {
2604 reg = FDI_TX_CTL(pipe);
2605 temp = I915_READ(reg);
2606 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2607 temp |= snb_b_fdi_train_param[i];
2608 I915_WRITE(reg, temp);
2613 reg = FDI_RX_IIR(pipe);
2614 temp = I915_READ(reg);
2615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2617 if (temp & FDI_RX_BIT_LOCK ||
2618 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2619 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2620 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
2625 DRM_ERROR("FDI train 1 fail!\n");
2628 reg = FDI_TX_CTL(pipe);
2629 temp = I915_READ(reg);
2630 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2631 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2633 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2634 I915_WRITE(reg, temp);
2636 reg = FDI_RX_CTL(pipe);
2637 temp = I915_READ(reg);
2638 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2639 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2640 I915_WRITE(reg, temp);
2645 for (i = 0; i < 4; i++) {
2646 reg = FDI_TX_CTL(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2649 temp |= snb_b_fdi_train_param[i];
2650 I915_WRITE(reg, temp);
2655 reg = FDI_RX_IIR(pipe);
2656 temp = I915_READ(reg);
2657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2659 if (temp & FDI_RX_SYMBOL_LOCK) {
2660 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2661 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
2666 DRM_ERROR("FDI train 2 fail!\n");
2668 DRM_DEBUG_KMS("FDI train done.\n");
2671 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2673 struct drm_device *dev = intel_crtc->base.dev;
2674 struct drm_i915_private *dev_priv = dev->dev_private;
2675 int pipe = intel_crtc->pipe;
2679 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2680 reg = FDI_RX_CTL(pipe);
2681 temp = I915_READ(reg);
2682 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2683 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2684 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2685 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2690 /* Switch from Rawclk to PCDclk */
2691 temp = I915_READ(reg);
2692 I915_WRITE(reg, temp | FDI_PCDCLK);
2697 /* Enable CPU FDI TX PLL, always on for Ironlake */
2698 reg = FDI_TX_CTL(pipe);
2699 temp = I915_READ(reg);
2700 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2701 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2708 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2710 struct drm_device *dev = intel_crtc->base.dev;
2711 struct drm_i915_private *dev_priv = dev->dev_private;
2712 int pipe = intel_crtc->pipe;
2715 /* Switch from PCDclk to Rawclk */
2716 reg = FDI_RX_CTL(pipe);
2717 temp = I915_READ(reg);
2718 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2720 /* Disable CPU FDI TX PLL */
2721 reg = FDI_TX_CTL(pipe);
2722 temp = I915_READ(reg);
2723 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2728 reg = FDI_RX_CTL(pipe);
2729 temp = I915_READ(reg);
2730 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2732 /* Wait for the clocks to turn off. */
2737 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2739 struct drm_device *dev = crtc->dev;
2740 struct drm_i915_private *dev_priv = dev->dev_private;
2741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2742 int pipe = intel_crtc->pipe;
2745 /* disable CPU FDI tx and PCH FDI rx */
2746 reg = FDI_TX_CTL(pipe);
2747 temp = I915_READ(reg);
2748 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2751 reg = FDI_RX_CTL(pipe);
2752 temp = I915_READ(reg);
2753 temp &= ~(0x7 << 16);
2754 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2755 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2760 /* Ironlake workaround, disable clock pointer after downing FDI */
2761 if (HAS_PCH_IBX(dev)) {
2762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2765 /* still set train pattern 1 */
2766 reg = FDI_TX_CTL(pipe);
2767 temp = I915_READ(reg);
2768 temp &= ~FDI_LINK_TRAIN_NONE;
2769 temp |= FDI_LINK_TRAIN_PATTERN_1;
2770 I915_WRITE(reg, temp);
2772 reg = FDI_RX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 if (HAS_PCH_CPT(dev)) {
2775 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2776 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2778 temp &= ~FDI_LINK_TRAIN_NONE;
2779 temp |= FDI_LINK_TRAIN_PATTERN_1;
2781 /* BPC in FDI rx is consistent with that in PIPECONF */
2782 temp &= ~(0x07 << 16);
2783 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2784 I915_WRITE(reg, temp);
2790 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2792 struct drm_device *dev = crtc->dev;
2793 struct drm_i915_private *dev_priv = dev->dev_private;
2794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2795 unsigned long flags;
2798 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2799 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2802 spin_lock_irqsave(&dev->event_lock, flags);
2803 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2804 spin_unlock_irqrestore(&dev->event_lock, flags);
2809 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2811 struct drm_device *dev = crtc->dev;
2812 struct drm_i915_private *dev_priv = dev->dev_private;
2814 if (crtc->fb == NULL)
2817 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2819 wait_event(dev_priv->pending_flip_queue,
2820 !intel_crtc_has_pending_flip(crtc));
2822 mutex_lock(&dev->struct_mutex);
2823 intel_finish_fb(crtc->fb);
2824 mutex_unlock(&dev->struct_mutex);
2827 /* Program iCLKIP clock to the desired frequency */
2828 static void lpt_program_iclkip(struct drm_crtc *crtc)
2830 struct drm_device *dev = crtc->dev;
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2835 mutex_lock(&dev_priv->dpio_lock);
2837 /* It is necessary to ungate the pixclk gate prior to programming
2838 * the divisors, and gate it back when it is done.
2840 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2842 /* Disable SSCCTL */
2843 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2844 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2848 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2849 if (crtc->mode.clock == 20000) {
2854 /* The iCLK virtual clock root frequency is in MHz,
2855 * but the crtc->mode.clock in in KHz. To get the divisors,
2856 * it is necessary to divide one by another, so we
2857 * convert the virtual clock precision to KHz here for higher
2860 u32 iclk_virtual_root_freq = 172800 * 1000;
2861 u32 iclk_pi_range = 64;
2862 u32 desired_divisor, msb_divisor_value, pi_value;
2864 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2865 msb_divisor_value = desired_divisor / iclk_pi_range;
2866 pi_value = desired_divisor % iclk_pi_range;
2869 divsel = msb_divisor_value - 2;
2870 phaseinc = pi_value;
2873 /* This should not happen with any sane values */
2874 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2875 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2876 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2877 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2879 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2886 /* Program SSCDIVINTPHASE6 */
2887 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2888 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2889 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2890 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2891 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2892 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2893 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2894 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2896 /* Program SSCAUXDIV */
2897 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2898 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2899 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2900 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2902 /* Enable modulator and associated divider */
2903 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2904 temp &= ~SBI_SSCCTL_DISABLE;
2905 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2907 /* Wait for initialization time */
2910 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2912 mutex_unlock(&dev_priv->dpio_lock);
2915 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2916 enum pipe pch_transcoder)
2918 struct drm_device *dev = crtc->base.dev;
2919 struct drm_i915_private *dev_priv = dev->dev_private;
2920 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2922 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2923 I915_READ(HTOTAL(cpu_transcoder)));
2924 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2925 I915_READ(HBLANK(cpu_transcoder)));
2926 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2927 I915_READ(HSYNC(cpu_transcoder)));
2929 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2930 I915_READ(VTOTAL(cpu_transcoder)));
2931 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2932 I915_READ(VBLANK(cpu_transcoder)));
2933 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2934 I915_READ(VSYNC(cpu_transcoder)));
2935 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2936 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2940 * Enable PCH resources required for PCH ports:
2942 * - FDI training & RX/TX
2943 * - update transcoder timings
2944 * - DP transcoding bits
2947 static void ironlake_pch_enable(struct drm_crtc *crtc)
2949 struct drm_device *dev = crtc->dev;
2950 struct drm_i915_private *dev_priv = dev->dev_private;
2951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2952 int pipe = intel_crtc->pipe;
2955 assert_pch_transcoder_disabled(dev_priv, pipe);
2957 /* Write the TU size bits before fdi link training, so that error
2958 * detection works. */
2959 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2960 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2962 /* For PCH output, training FDI link */
2963 dev_priv->display.fdi_link_train(crtc);
2965 /* XXX: pch pll's can be enabled any time before we enable the PCH
2966 * transcoder, and we actually should do this to not upset any PCH
2967 * transcoder that already use the clock when we share it.
2969 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
2970 * unconditionally resets the pll - we need that to have the right LVDS
2971 * enable sequence. */
2972 ironlake_enable_pch_pll(intel_crtc);
2974 if (HAS_PCH_CPT(dev)) {
2977 temp = I915_READ(PCH_DPLL_SEL);
2981 temp |= TRANSA_DPLL_ENABLE;
2982 sel = TRANSA_DPLLB_SEL;
2985 temp |= TRANSB_DPLL_ENABLE;
2986 sel = TRANSB_DPLLB_SEL;
2989 temp |= TRANSC_DPLL_ENABLE;
2990 sel = TRANSC_DPLLB_SEL;
2993 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
2997 I915_WRITE(PCH_DPLL_SEL, temp);
3000 /* set transcoder timing, panel must allow it */
3001 assert_panel_unlocked(dev_priv, pipe);
3002 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3004 intel_fdi_normal_train(crtc);
3006 /* For PCH DP, enable TRANS_DP_CTL */
3007 if (HAS_PCH_CPT(dev) &&
3008 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3009 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3010 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3011 reg = TRANS_DP_CTL(pipe);
3012 temp = I915_READ(reg);
3013 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3014 TRANS_DP_SYNC_MASK |
3016 temp |= (TRANS_DP_OUTPUT_ENABLE |
3017 TRANS_DP_ENH_FRAMING);
3018 temp |= bpc << 9; /* same format but at 11:9 */
3020 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3021 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3022 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3023 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3025 switch (intel_trans_dp_port_sel(crtc)) {
3027 temp |= TRANS_DP_PORT_SEL_B;
3030 temp |= TRANS_DP_PORT_SEL_C;
3033 temp |= TRANS_DP_PORT_SEL_D;
3039 I915_WRITE(reg, temp);
3042 ironlake_enable_pch_transcoder(dev_priv, pipe);
3045 static void lpt_pch_enable(struct drm_crtc *crtc)
3047 struct drm_device *dev = crtc->dev;
3048 struct drm_i915_private *dev_priv = dev->dev_private;
3049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3050 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3052 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3054 lpt_program_iclkip(crtc);
3056 /* Set transcoder timing. */
3057 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3059 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3062 static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3064 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3069 if (pll->refcount == 0) {
3070 WARN(1, "bad PCH PLL refcount\n");
3074 if (--pll->refcount == 0) {
3076 WARN_ON(pll->active);
3079 intel_crtc->pch_pll = NULL;
3082 static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3084 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3085 struct intel_pch_pll *pll;
3088 pll = intel_crtc->pch_pll;
3090 DRM_DEBUG_KMS("CRTC:%d dropping existing PCH PLL %x\n",
3091 intel_crtc->base.base.id, pll->pll_reg);
3092 intel_put_pch_pll(intel_crtc);
3095 if (HAS_PCH_IBX(dev_priv->dev)) {
3096 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3097 i = intel_crtc->pipe;
3098 pll = &dev_priv->pch_plls[i];
3100 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3101 intel_crtc->base.base.id, pll->pll_reg);
3106 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3107 pll = &dev_priv->pch_plls[i];
3109 /* Only want to check enabled timings first */
3110 if (pll->refcount == 0)
3113 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3114 fp == I915_READ(pll->fp0_reg)) {
3115 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3116 intel_crtc->base.base.id,
3117 pll->pll_reg, pll->refcount, pll->active);
3123 /* Ok no matching timings, maybe there's a free one? */
3124 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3125 pll = &dev_priv->pch_plls[i];
3126 if (pll->refcount == 0) {
3127 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3128 intel_crtc->base.base.id, pll->pll_reg);
3136 intel_crtc->pch_pll = pll;
3137 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
3138 if (pll->active == 0) {
3139 DRM_DEBUG_DRIVER("setting up pll %d\n", i);
3141 assert_pch_pll_disabled(dev_priv, pll, NULL);
3143 /* Wait for the clocks to stabilize before rewriting the regs */
3144 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3145 POSTING_READ(pll->pll_reg);
3148 I915_WRITE(pll->fp0_reg, fp);
3149 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3156 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3158 struct drm_i915_private *dev_priv = dev->dev_private;
3159 int dslreg = PIPEDSL(pipe);
3162 temp = I915_READ(dslreg);
3164 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3165 if (wait_for(I915_READ(dslreg) != temp, 5))
3166 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3170 static void ironlake_pfit_enable(struct intel_crtc *crtc)
3172 struct drm_device *dev = crtc->base.dev;
3173 struct drm_i915_private *dev_priv = dev->dev_private;
3174 int pipe = crtc->pipe;
3176 if (crtc->config.pch_pfit.size) {
3177 /* Force use of hard-coded filter coefficients
3178 * as some pre-programmed values are broken,
3181 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3182 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3183 PF_PIPE_SEL_IVB(pipe));
3185 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3186 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3187 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3191 static void intel_enable_planes(struct drm_crtc *crtc)
3193 struct drm_device *dev = crtc->dev;
3194 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3195 struct intel_plane *intel_plane;
3197 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3198 if (intel_plane->pipe == pipe)
3199 intel_plane_restore(&intel_plane->base);
3202 static void intel_disable_planes(struct drm_crtc *crtc)
3204 struct drm_device *dev = crtc->dev;
3205 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3206 struct intel_plane *intel_plane;
3208 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3209 if (intel_plane->pipe == pipe)
3210 intel_plane_disable(&intel_plane->base);
3213 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3215 struct drm_device *dev = crtc->dev;
3216 struct drm_i915_private *dev_priv = dev->dev_private;
3217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3218 struct intel_encoder *encoder;
3219 int pipe = intel_crtc->pipe;
3220 int plane = intel_crtc->plane;
3223 WARN_ON(!crtc->enabled);
3225 if (intel_crtc->active)
3228 intel_crtc->active = true;
3230 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3231 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3233 intel_update_watermarks(dev);
3235 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3236 temp = I915_READ(PCH_LVDS);
3237 if ((temp & LVDS_PORT_EN) == 0)
3238 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3242 if (intel_crtc->config.has_pch_encoder) {
3243 /* Note: FDI PLL enabling _must_ be done before we enable the
3244 * cpu pipes, hence this is separate from all the other fdi/pch
3246 ironlake_fdi_pll_enable(intel_crtc);
3248 assert_fdi_tx_disabled(dev_priv, pipe);
3249 assert_fdi_rx_disabled(dev_priv, pipe);
3252 for_each_encoder_on_crtc(dev, crtc, encoder)
3253 if (encoder->pre_enable)
3254 encoder->pre_enable(encoder);
3256 /* Enable panel fitting for LVDS */
3257 ironlake_pfit_enable(intel_crtc);
3260 * On ILK+ LUT must be loaded before the pipe is running but with
3263 intel_crtc_load_lut(crtc);
3265 intel_enable_pipe(dev_priv, pipe,
3266 intel_crtc->config.has_pch_encoder);
3267 intel_enable_plane(dev_priv, plane, pipe);
3268 intel_enable_planes(crtc);
3269 intel_crtc_update_cursor(crtc, true);
3271 if (intel_crtc->config.has_pch_encoder)
3272 ironlake_pch_enable(crtc);
3274 mutex_lock(&dev->struct_mutex);
3275 intel_update_fbc(dev);
3276 mutex_unlock(&dev->struct_mutex);
3278 for_each_encoder_on_crtc(dev, crtc, encoder)
3279 encoder->enable(encoder);
3281 if (HAS_PCH_CPT(dev))
3282 cpt_verify_modeset(dev, intel_crtc->pipe);
3285 * There seems to be a race in PCH platform hw (at least on some
3286 * outputs) where an enabled pipe still completes any pageflip right
3287 * away (as if the pipe is off) instead of waiting for vblank. As soon
3288 * as the first vblank happend, everything works as expected. Hence just
3289 * wait for one vblank before returning to avoid strange things
3292 intel_wait_for_vblank(dev, intel_crtc->pipe);
3295 /* IPS only exists on ULT machines and is tied to pipe A. */
3296 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3298 return IS_ULT(crtc->base.dev) && crtc->pipe == PIPE_A;
3301 static void hsw_enable_ips(struct intel_crtc *crtc)
3303 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3305 if (!crtc->config.ips_enabled)
3308 /* We can only enable IPS after we enable a plane and wait for a vblank.
3309 * We guarantee that the plane is enabled by calling intel_enable_ips
3310 * only after intel_enable_plane. And intel_enable_plane already waits
3311 * for a vblank, so all we need to do here is to enable the IPS bit. */
3312 assert_plane_enabled(dev_priv, crtc->plane);
3313 I915_WRITE(IPS_CTL, IPS_ENABLE);
3316 static void hsw_disable_ips(struct intel_crtc *crtc)
3318 struct drm_device *dev = crtc->base.dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3321 if (!crtc->config.ips_enabled)
3324 assert_plane_enabled(dev_priv, crtc->plane);
3325 I915_WRITE(IPS_CTL, 0);
3327 /* We need to wait for a vblank before we can disable the plane. */
3328 intel_wait_for_vblank(dev, crtc->pipe);
3331 static void haswell_crtc_enable(struct drm_crtc *crtc)
3333 struct drm_device *dev = crtc->dev;
3334 struct drm_i915_private *dev_priv = dev->dev_private;
3335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3336 struct intel_encoder *encoder;
3337 int pipe = intel_crtc->pipe;
3338 int plane = intel_crtc->plane;
3340 WARN_ON(!crtc->enabled);
3342 if (intel_crtc->active)
3345 intel_crtc->active = true;
3347 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3348 if (intel_crtc->config.has_pch_encoder)
3349 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3351 intel_update_watermarks(dev);
3353 if (intel_crtc->config.has_pch_encoder)
3354 dev_priv->display.fdi_link_train(crtc);
3356 for_each_encoder_on_crtc(dev, crtc, encoder)
3357 if (encoder->pre_enable)
3358 encoder->pre_enable(encoder);
3360 intel_ddi_enable_pipe_clock(intel_crtc);
3362 /* Enable panel fitting for eDP */
3363 ironlake_pfit_enable(intel_crtc);
3366 * On ILK+ LUT must be loaded before the pipe is running but with
3369 intel_crtc_load_lut(crtc);
3371 intel_ddi_set_pipe_settings(crtc);
3372 intel_ddi_enable_transcoder_func(crtc);
3374 intel_enable_pipe(dev_priv, pipe,
3375 intel_crtc->config.has_pch_encoder);
3376 intel_enable_plane(dev_priv, plane, pipe);
3377 intel_enable_planes(crtc);
3378 intel_crtc_update_cursor(crtc, true);
3380 hsw_enable_ips(intel_crtc);
3382 if (intel_crtc->config.has_pch_encoder)
3383 lpt_pch_enable(crtc);
3385 mutex_lock(&dev->struct_mutex);
3386 intel_update_fbc(dev);
3387 mutex_unlock(&dev->struct_mutex);
3389 for_each_encoder_on_crtc(dev, crtc, encoder)
3390 encoder->enable(encoder);
3393 * There seems to be a race in PCH platform hw (at least on some
3394 * outputs) where an enabled pipe still completes any pageflip right
3395 * away (as if the pipe is off) instead of waiting for vblank. As soon
3396 * as the first vblank happend, everything works as expected. Hence just
3397 * wait for one vblank before returning to avoid strange things
3400 intel_wait_for_vblank(dev, intel_crtc->pipe);
3403 static void ironlake_pfit_disable(struct intel_crtc *crtc)
3405 struct drm_device *dev = crtc->base.dev;
3406 struct drm_i915_private *dev_priv = dev->dev_private;
3407 int pipe = crtc->pipe;
3409 /* To avoid upsetting the power well on haswell only disable the pfit if
3410 * it's in use. The hw state code will make sure we get this right. */
3411 if (crtc->config.pch_pfit.size) {
3412 I915_WRITE(PF_CTL(pipe), 0);
3413 I915_WRITE(PF_WIN_POS(pipe), 0);
3414 I915_WRITE(PF_WIN_SZ(pipe), 0);
3418 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3420 struct drm_device *dev = crtc->dev;
3421 struct drm_i915_private *dev_priv = dev->dev_private;
3422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3423 struct intel_encoder *encoder;
3424 int pipe = intel_crtc->pipe;
3425 int plane = intel_crtc->plane;
3429 if (!intel_crtc->active)
3432 for_each_encoder_on_crtc(dev, crtc, encoder)
3433 encoder->disable(encoder);
3435 intel_crtc_wait_for_pending_flips(crtc);
3436 drm_vblank_off(dev, pipe);
3438 if (dev_priv->cfb_plane == plane)
3439 intel_disable_fbc(dev);
3441 intel_crtc_update_cursor(crtc, false);
3442 intel_disable_planes(crtc);
3443 intel_disable_plane(dev_priv, plane, pipe);
3445 if (intel_crtc->config.has_pch_encoder)
3446 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3448 intel_disable_pipe(dev_priv, pipe);
3450 ironlake_pfit_disable(intel_crtc);
3452 for_each_encoder_on_crtc(dev, crtc, encoder)
3453 if (encoder->post_disable)
3454 encoder->post_disable(encoder);
3456 if (intel_crtc->config.has_pch_encoder) {
3457 ironlake_fdi_disable(crtc);
3459 ironlake_disable_pch_transcoder(dev_priv, pipe);
3460 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3462 if (HAS_PCH_CPT(dev)) {
3463 /* disable TRANS_DP_CTL */
3464 reg = TRANS_DP_CTL(pipe);
3465 temp = I915_READ(reg);
3466 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3467 TRANS_DP_PORT_SEL_MASK);
3468 temp |= TRANS_DP_PORT_SEL_NONE;
3469 I915_WRITE(reg, temp);
3471 /* disable DPLL_SEL */
3472 temp = I915_READ(PCH_DPLL_SEL);
3475 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3478 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3481 /* C shares PLL A or B */
3482 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3487 I915_WRITE(PCH_DPLL_SEL, temp);
3490 /* disable PCH DPLL */
3491 intel_disable_pch_pll(intel_crtc);
3493 ironlake_fdi_pll_disable(intel_crtc);
3496 intel_crtc->active = false;
3497 intel_update_watermarks(dev);
3499 mutex_lock(&dev->struct_mutex);
3500 intel_update_fbc(dev);
3501 mutex_unlock(&dev->struct_mutex);
3504 static void haswell_crtc_disable(struct drm_crtc *crtc)
3506 struct drm_device *dev = crtc->dev;
3507 struct drm_i915_private *dev_priv = dev->dev_private;
3508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3509 struct intel_encoder *encoder;
3510 int pipe = intel_crtc->pipe;
3511 int plane = intel_crtc->plane;
3512 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3514 if (!intel_crtc->active)
3517 for_each_encoder_on_crtc(dev, crtc, encoder)
3518 encoder->disable(encoder);
3520 intel_crtc_wait_for_pending_flips(crtc);
3521 drm_vblank_off(dev, pipe);
3523 /* FBC must be disabled before disabling the plane on HSW. */
3524 if (dev_priv->cfb_plane == plane)
3525 intel_disable_fbc(dev);
3527 hsw_disable_ips(intel_crtc);
3529 intel_crtc_update_cursor(crtc, false);
3530 intel_disable_planes(crtc);
3531 intel_disable_plane(dev_priv, plane, pipe);
3533 if (intel_crtc->config.has_pch_encoder)
3534 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3535 intel_disable_pipe(dev_priv, pipe);
3537 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3539 ironlake_pfit_disable(intel_crtc);
3541 intel_ddi_disable_pipe_clock(intel_crtc);
3543 for_each_encoder_on_crtc(dev, crtc, encoder)
3544 if (encoder->post_disable)
3545 encoder->post_disable(encoder);
3547 if (intel_crtc->config.has_pch_encoder) {
3548 lpt_disable_pch_transcoder(dev_priv);
3549 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3550 intel_ddi_fdi_disable(crtc);
3553 intel_crtc->active = false;
3554 intel_update_watermarks(dev);
3556 mutex_lock(&dev->struct_mutex);
3557 intel_update_fbc(dev);
3558 mutex_unlock(&dev->struct_mutex);
3561 static void ironlake_crtc_off(struct drm_crtc *crtc)
3563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3564 intel_put_pch_pll(intel_crtc);
3567 static void haswell_crtc_off(struct drm_crtc *crtc)
3569 intel_ddi_put_crtc_pll(crtc);
3572 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3574 if (!enable && intel_crtc->overlay) {
3575 struct drm_device *dev = intel_crtc->base.dev;
3576 struct drm_i915_private *dev_priv = dev->dev_private;
3578 mutex_lock(&dev->struct_mutex);
3579 dev_priv->mm.interruptible = false;
3580 (void) intel_overlay_switch_off(intel_crtc->overlay);
3581 dev_priv->mm.interruptible = true;
3582 mutex_unlock(&dev->struct_mutex);
3585 /* Let userspace switch the overlay on again. In most cases userspace
3586 * has to recompute where to put it anyway.
3591 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3592 * cursor plane briefly if not already running after enabling the display
3594 * This workaround avoids occasional blank screens when self refresh is
3598 g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3600 u32 cntl = I915_READ(CURCNTR(pipe));
3602 if ((cntl & CURSOR_MODE) == 0) {
3603 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3605 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3606 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3607 intel_wait_for_vblank(dev_priv->dev, pipe);
3608 I915_WRITE(CURCNTR(pipe), cntl);
3609 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3610 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3614 static void i9xx_pfit_enable(struct intel_crtc *crtc)
3616 struct drm_device *dev = crtc->base.dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 struct intel_crtc_config *pipe_config = &crtc->config;
3620 if (!crtc->config.gmch_pfit.control)
3624 * The panel fitter should only be adjusted whilst the pipe is disabled,
3625 * according to register description and PRM.
3627 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3628 assert_pipe_disabled(dev_priv, crtc->pipe);
3630 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3631 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
3633 /* Border color in case we don't scale up to the full screen. Black by
3634 * default, change to something else for debugging. */
3635 I915_WRITE(BCLRPAT(crtc->pipe), 0);
3638 static void valleyview_crtc_enable(struct drm_crtc *crtc)
3640 struct drm_device *dev = crtc->dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3643 struct intel_encoder *encoder;
3644 int pipe = intel_crtc->pipe;
3645 int plane = intel_crtc->plane;
3647 WARN_ON(!crtc->enabled);
3649 if (intel_crtc->active)
3652 intel_crtc->active = true;
3653 intel_update_watermarks(dev);
3655 mutex_lock(&dev_priv->dpio_lock);
3657 for_each_encoder_on_crtc(dev, crtc, encoder)
3658 if (encoder->pre_pll_enable)
3659 encoder->pre_pll_enable(encoder);
3661 intel_enable_pll(dev_priv, pipe);
3663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 if (encoder->pre_enable)
3665 encoder->pre_enable(encoder);
3667 /* VLV wants encoder enabling _before_ the pipe is up. */
3668 for_each_encoder_on_crtc(dev, crtc, encoder)
3669 encoder->enable(encoder);
3671 /* Enable panel fitting for eDP */
3672 i9xx_pfit_enable(intel_crtc);
3674 intel_crtc_load_lut(crtc);
3676 intel_enable_pipe(dev_priv, pipe, false);
3677 intel_enable_plane(dev_priv, plane, pipe);
3678 intel_enable_planes(crtc);
3679 intel_crtc_update_cursor(crtc, true);
3681 intel_update_fbc(dev);
3683 mutex_unlock(&dev_priv->dpio_lock);
3686 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3688 struct drm_device *dev = crtc->dev;
3689 struct drm_i915_private *dev_priv = dev->dev_private;
3690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3691 struct intel_encoder *encoder;
3692 int pipe = intel_crtc->pipe;
3693 int plane = intel_crtc->plane;
3695 WARN_ON(!crtc->enabled);
3697 if (intel_crtc->active)
3700 intel_crtc->active = true;
3701 intel_update_watermarks(dev);
3703 intel_enable_pll(dev_priv, pipe);
3705 for_each_encoder_on_crtc(dev, crtc, encoder)
3706 if (encoder->pre_enable)
3707 encoder->pre_enable(encoder);
3709 /* Enable panel fitting for LVDS */
3710 i9xx_pfit_enable(intel_crtc);
3712 intel_crtc_load_lut(crtc);
3714 intel_enable_pipe(dev_priv, pipe, false);
3715 intel_enable_plane(dev_priv, plane, pipe);
3716 intel_enable_planes(crtc);
3717 /* The fixup needs to happen before cursor is enabled */
3719 g4x_fixup_plane(dev_priv, pipe);
3720 intel_crtc_update_cursor(crtc, true);
3722 /* Give the overlay scaler a chance to enable if it's on this pipe */
3723 intel_crtc_dpms_overlay(intel_crtc, true);
3725 intel_update_fbc(dev);
3727 for_each_encoder_on_crtc(dev, crtc, encoder)
3728 encoder->enable(encoder);
3731 static void i9xx_pfit_disable(struct intel_crtc *crtc)
3733 struct drm_device *dev = crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3736 if (!crtc->config.gmch_pfit.control)
3739 assert_pipe_disabled(dev_priv, crtc->pipe);
3741 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3742 I915_READ(PFIT_CONTROL));
3743 I915_WRITE(PFIT_CONTROL, 0);
3746 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3748 struct drm_device *dev = crtc->dev;
3749 struct drm_i915_private *dev_priv = dev->dev_private;
3750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3751 struct intel_encoder *encoder;
3752 int pipe = intel_crtc->pipe;
3753 int plane = intel_crtc->plane;
3755 if (!intel_crtc->active)
3758 for_each_encoder_on_crtc(dev, crtc, encoder)
3759 encoder->disable(encoder);
3761 /* Give the overlay scaler a chance to disable if it's on this pipe */
3762 intel_crtc_wait_for_pending_flips(crtc);
3763 drm_vblank_off(dev, pipe);
3765 if (dev_priv->cfb_plane == plane)
3766 intel_disable_fbc(dev);
3768 intel_crtc_dpms_overlay(intel_crtc, false);
3769 intel_crtc_update_cursor(crtc, false);
3770 intel_disable_planes(crtc);
3771 intel_disable_plane(dev_priv, plane, pipe);
3773 intel_disable_pipe(dev_priv, pipe);
3775 i9xx_pfit_disable(intel_crtc);
3777 for_each_encoder_on_crtc(dev, crtc, encoder)
3778 if (encoder->post_disable)
3779 encoder->post_disable(encoder);
3781 intel_disable_pll(dev_priv, pipe);
3783 intel_crtc->active = false;
3784 intel_update_fbc(dev);
3785 intel_update_watermarks(dev);
3788 static void i9xx_crtc_off(struct drm_crtc *crtc)
3792 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3795 struct drm_device *dev = crtc->dev;
3796 struct drm_i915_master_private *master_priv;
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
3800 if (!dev->primary->master)
3803 master_priv = dev->primary->master->driver_priv;
3804 if (!master_priv->sarea_priv)
3809 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3810 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3813 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3814 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3817 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3823 * Sets the power management mode of the pipe and plane.
3825 void intel_crtc_update_dpms(struct drm_crtc *crtc)
3827 struct drm_device *dev = crtc->dev;
3828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 struct intel_encoder *intel_encoder;
3830 bool enable = false;
3832 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3833 enable |= intel_encoder->connectors_active;
3836 dev_priv->display.crtc_enable(crtc);
3838 dev_priv->display.crtc_disable(crtc);
3840 intel_crtc_update_sarea(crtc, enable);
3843 static void intel_crtc_disable(struct drm_crtc *crtc)
3845 struct drm_device *dev = crtc->dev;
3846 struct drm_connector *connector;
3847 struct drm_i915_private *dev_priv = dev->dev_private;
3848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3850 /* crtc should still be enabled when we disable it. */
3851 WARN_ON(!crtc->enabled);
3853 dev_priv->display.crtc_disable(crtc);
3854 intel_crtc->eld_vld = false;
3855 intel_crtc_update_sarea(crtc, false);
3856 dev_priv->display.off(crtc);
3858 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3859 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3862 mutex_lock(&dev->struct_mutex);
3863 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3864 mutex_unlock(&dev->struct_mutex);
3868 /* Update computed state. */
3869 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3870 if (!connector->encoder || !connector->encoder->crtc)
3873 if (connector->encoder->crtc != crtc)
3876 connector->dpms = DRM_MODE_DPMS_OFF;
3877 to_intel_encoder(connector->encoder)->connectors_active = false;
3881 void intel_modeset_disable(struct drm_device *dev)
3883 struct drm_crtc *crtc;
3885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3887 intel_crtc_disable(crtc);
3891 void intel_encoder_destroy(struct drm_encoder *encoder)
3893 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3895 drm_encoder_cleanup(encoder);
3896 kfree(intel_encoder);
3899 /* Simple dpms helper for encodres with just one connector, no cloning and only
3900 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3901 * state of the entire output pipe. */
3902 void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3904 if (mode == DRM_MODE_DPMS_ON) {
3905 encoder->connectors_active = true;
3907 intel_crtc_update_dpms(encoder->base.crtc);
3909 encoder->connectors_active = false;
3911 intel_crtc_update_dpms(encoder->base.crtc);
3915 /* Cross check the actual hw state with our own modeset state tracking (and it's
3916 * internal consistency). */
3917 static void intel_connector_check_state(struct intel_connector *connector)
3919 if (connector->get_hw_state(connector)) {
3920 struct intel_encoder *encoder = connector->encoder;
3921 struct drm_crtc *crtc;
3922 bool encoder_enabled;
3925 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3926 connector->base.base.id,
3927 drm_get_connector_name(&connector->base));
3929 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3930 "wrong connector dpms state\n");
3931 WARN(connector->base.encoder != &encoder->base,
3932 "active connector not linked to encoder\n");
3933 WARN(!encoder->connectors_active,
3934 "encoder->connectors_active not set\n");
3936 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3937 WARN(!encoder_enabled, "encoder not enabled\n");
3938 if (WARN_ON(!encoder->base.crtc))
3941 crtc = encoder->base.crtc;
3943 WARN(!crtc->enabled, "crtc not enabled\n");
3944 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3945 WARN(pipe != to_intel_crtc(crtc)->pipe,
3946 "encoder active on the wrong pipe\n");
3950 /* Even simpler default implementation, if there's really no special case to
3952 void intel_connector_dpms(struct drm_connector *connector, int mode)
3954 struct intel_encoder *encoder = intel_attached_encoder(connector);
3956 /* All the simple cases only support two dpms states. */
3957 if (mode != DRM_MODE_DPMS_ON)
3958 mode = DRM_MODE_DPMS_OFF;
3960 if (mode == connector->dpms)
3963 connector->dpms = mode;
3965 /* Only need to change hw state when actually enabled */
3966 if (encoder->base.crtc)
3967 intel_encoder_dpms(encoder, mode);
3969 WARN_ON(encoder->connectors_active != false);
3971 intel_modeset_check_state(connector->dev);
3974 /* Simple connector->get_hw_state implementation for encoders that support only
3975 * one connector and no cloning and hence the encoder state determines the state
3976 * of the connector. */
3977 bool intel_connector_get_hw_state(struct intel_connector *connector)
3980 struct intel_encoder *encoder = connector->encoder;
3982 return encoder->get_hw_state(encoder, &pipe);
3985 static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3986 struct intel_crtc_config *pipe_config)
3988 struct drm_i915_private *dev_priv = dev->dev_private;
3989 struct intel_crtc *pipe_B_crtc =
3990 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3992 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3993 pipe_name(pipe), pipe_config->fdi_lanes);
3994 if (pipe_config->fdi_lanes > 4) {
3995 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3996 pipe_name(pipe), pipe_config->fdi_lanes);
4000 if (IS_HASWELL(dev)) {
4001 if (pipe_config->fdi_lanes > 2) {
4002 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4003 pipe_config->fdi_lanes);
4010 if (INTEL_INFO(dev)->num_pipes == 2)
4013 /* Ivybridge 3 pipe is really complicated */
4018 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4019 pipe_config->fdi_lanes > 2) {
4020 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4021 pipe_name(pipe), pipe_config->fdi_lanes);
4026 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
4027 pipe_B_crtc->config.fdi_lanes <= 2) {
4028 if (pipe_config->fdi_lanes > 2) {
4029 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4030 pipe_name(pipe), pipe_config->fdi_lanes);
4034 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4044 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4045 struct intel_crtc_config *pipe_config)
4047 struct drm_device *dev = intel_crtc->base.dev;
4048 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4049 int lane, link_bw, fdi_dotclock;
4050 bool setup_ok, needs_recompute = false;
4053 /* FDI is a binary signal running at ~2.7GHz, encoding
4054 * each output octet as 10 bits. The actual frequency
4055 * is stored as a divider into a 100MHz clock, and the
4056 * mode pixel clock is stored in units of 1KHz.
4057 * Hence the bw of each lane in terms of the mode signal
4060 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4062 fdi_dotclock = adjusted_mode->clock;
4063 fdi_dotclock /= pipe_config->pixel_multiplier;
4065 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
4066 pipe_config->pipe_bpp);
4068 pipe_config->fdi_lanes = lane;
4070 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
4071 link_bw, &pipe_config->fdi_m_n);
4073 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4074 intel_crtc->pipe, pipe_config);
4075 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4076 pipe_config->pipe_bpp -= 2*3;
4077 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4078 pipe_config->pipe_bpp);
4079 needs_recompute = true;
4080 pipe_config->bw_constrained = true;
4085 if (needs_recompute)
4088 return setup_ok ? 0 : -EINVAL;
4091 static void hsw_compute_ips_config(struct intel_crtc *crtc,
4092 struct intel_crtc_config *pipe_config)
4094 pipe_config->ips_enabled = i915_enable_ips &&
4095 hsw_crtc_supports_ips(crtc) &&
4096 pipe_config->pipe_bpp == 24;
4099 static int intel_crtc_compute_config(struct drm_crtc *crtc,
4100 struct intel_crtc_config *pipe_config)
4102 struct drm_device *dev = crtc->dev;
4103 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4106 if (HAS_PCH_SPLIT(dev)) {
4107 /* FDI link clock is fixed at 2.7G */
4108 if (pipe_config->requested_mode.clock * 3
4109 > IRONLAKE_FDI_FREQ * 4)
4113 /* All interlaced capable intel hw wants timings in frames. Note though
4114 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4115 * timings, so we need to be careful not to clobber these.*/
4116 if (!pipe_config->timings_set)
4117 drm_mode_set_crtcinfo(adjusted_mode, 0);
4119 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4120 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4122 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4123 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4126 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4127 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4128 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4129 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4131 pipe_config->pipe_bpp = 8*3;
4134 if (IS_HASWELL(dev))
4135 hsw_compute_ips_config(intel_crtc, pipe_config);
4137 if (pipe_config->has_pch_encoder)
4138 return ironlake_fdi_compute_config(intel_crtc, pipe_config);
4143 static int valleyview_get_display_clock_speed(struct drm_device *dev)
4145 return 400000; /* FIXME */
4148 static int i945_get_display_clock_speed(struct drm_device *dev)
4153 static int i915_get_display_clock_speed(struct drm_device *dev)
4158 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4163 static int i915gm_get_display_clock_speed(struct drm_device *dev)
4167 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4169 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4172 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4173 case GC_DISPLAY_CLOCK_333_MHZ:
4176 case GC_DISPLAY_CLOCK_190_200_MHZ:
4182 static int i865_get_display_clock_speed(struct drm_device *dev)
4187 static int i855_get_display_clock_speed(struct drm_device *dev)
4190 /* Assume that the hardware is in the high speed state. This
4191 * should be the default.
4193 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4194 case GC_CLOCK_133_200:
4195 case GC_CLOCK_100_200:
4197 case GC_CLOCK_166_250:
4199 case GC_CLOCK_100_133:
4203 /* Shouldn't happen */
4207 static int i830_get_display_clock_speed(struct drm_device *dev)
4213 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
4215 while (*num > DATA_LINK_M_N_MASK ||
4216 *den > DATA_LINK_M_N_MASK) {
4222 static void compute_m_n(unsigned int m, unsigned int n,
4223 uint32_t *ret_m, uint32_t *ret_n)
4225 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4226 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4227 intel_reduce_m_n_ratio(ret_m, ret_n);
4231 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4232 int pixel_clock, int link_clock,
4233 struct intel_link_m_n *m_n)
4237 compute_m_n(bits_per_pixel * pixel_clock,
4238 link_clock * nlanes * 8,
4239 &m_n->gmch_m, &m_n->gmch_n);
4241 compute_m_n(pixel_clock, link_clock,
4242 &m_n->link_m, &m_n->link_n);
4245 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4247 if (i915_panel_use_ssc >= 0)
4248 return i915_panel_use_ssc != 0;
4249 return dev_priv->vbt.lvds_use_ssc
4250 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4253 static int vlv_get_refclk(struct drm_crtc *crtc)
4255 struct drm_device *dev = crtc->dev;
4256 struct drm_i915_private *dev_priv = dev->dev_private;
4257 int refclk = 27000; /* for DP & HDMI */
4259 return 100000; /* only one validated so far */
4261 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4263 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4264 if (intel_panel_use_ssc(dev_priv))
4268 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4275 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4277 struct drm_device *dev = crtc->dev;
4278 struct drm_i915_private *dev_priv = dev->dev_private;
4281 if (IS_VALLEYVIEW(dev)) {
4282 refclk = vlv_get_refclk(crtc);
4283 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4284 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4285 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
4286 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4288 } else if (!IS_GEN2(dev)) {
4297 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4299 return (1 << dpll->n) << 16 | dpll->m1 << 8 | dpll->m2;
4302 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4304 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4307 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4308 intel_clock_t *reduced_clock)
4310 struct drm_device *dev = crtc->base.dev;
4311 struct drm_i915_private *dev_priv = dev->dev_private;
4312 int pipe = crtc->pipe;
4315 if (IS_PINEVIEW(dev)) {
4316 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
4318 fp2 = pnv_dpll_compute_fp(reduced_clock);
4320 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
4322 fp2 = i9xx_dpll_compute_fp(reduced_clock);
4325 I915_WRITE(FP0(pipe), fp);
4327 crtc->lowfreq_avail = false;
4328 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4329 reduced_clock && i915_powersave) {
4330 I915_WRITE(FP1(pipe), fp2);
4331 crtc->lowfreq_avail = true;
4333 I915_WRITE(FP1(pipe), fp);
4337 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4342 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4343 * and set it to a reasonable value instead.
4345 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4346 reg_val &= 0xffffff00;
4347 reg_val |= 0x00000030;
4348 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4350 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4351 reg_val &= 0x8cffffff;
4352 reg_val = 0x8c000000;
4353 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4355 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4356 reg_val &= 0xffffff00;
4357 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4359 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4360 reg_val &= 0x00ffffff;
4361 reg_val |= 0xb0000000;
4362 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4365 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4366 struct intel_link_m_n *m_n)
4368 struct drm_device *dev = crtc->base.dev;
4369 struct drm_i915_private *dev_priv = dev->dev_private;
4370 int pipe = crtc->pipe;
4372 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4373 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4374 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4375 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4378 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4379 struct intel_link_m_n *m_n)
4381 struct drm_device *dev = crtc->base.dev;
4382 struct drm_i915_private *dev_priv = dev->dev_private;
4383 int pipe = crtc->pipe;
4384 enum transcoder transcoder = crtc->config.cpu_transcoder;
4386 if (INTEL_INFO(dev)->gen >= 5) {
4387 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4388 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4389 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4390 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4392 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4393 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4394 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4395 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
4399 static void intel_dp_set_m_n(struct intel_crtc *crtc)
4401 if (crtc->config.has_pch_encoder)
4402 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4404 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4407 static void vlv_update_pll(struct intel_crtc *crtc)
4409 struct drm_device *dev = crtc->base.dev;
4410 struct drm_i915_private *dev_priv = dev->dev_private;
4411 struct intel_encoder *encoder;
4412 int pipe = crtc->pipe;
4414 u32 bestn, bestm1, bestm2, bestp1, bestp2;
4416 u32 coreclk, reg_val, dpll_md;
4418 mutex_lock(&dev_priv->dpio_lock);
4420 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4422 bestn = crtc->config.dpll.n;
4423 bestm1 = crtc->config.dpll.m1;
4424 bestm2 = crtc->config.dpll.m2;
4425 bestp1 = crtc->config.dpll.p1;
4426 bestp2 = crtc->config.dpll.p2;
4428 /* See eDP HDMI DPIO driver vbios notes doc */
4430 /* PLL B needs special handling */
4432 vlv_pllb_recal_opamp(dev_priv);
4434 /* Set up Tx target for periodic Rcomp update */
4435 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4437 /* Disable target IRef on PLL */
4438 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4439 reg_val &= 0x00ffffff;
4440 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4442 /* Disable fast lock */
4443 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4445 /* Set idtafcrecal before PLL is enabled */
4446 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4447 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4448 mdiv |= ((bestn << DPIO_N_SHIFT));
4449 mdiv |= (1 << DPIO_K_SHIFT);
4452 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4453 * but we don't support that).
4454 * Note: don't use the DAC post divider as it seems unstable.
4456 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4457 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4459 mdiv |= DPIO_ENABLE_CALIBRATION;
4460 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4462 /* Set HBR and RBR LPF coefficients */
4463 if (crtc->config.port_clock == 162000 ||
4464 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4465 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4468 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4471 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4472 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4473 /* Use SSC source */
4475 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4478 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4480 } else { /* HDMI or VGA */
4481 /* Use bend source */
4483 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4486 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4490 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4491 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4492 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4493 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4494 coreclk |= 0x01000000;
4495 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4497 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4499 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4500 if (encoder->pre_pll_enable)
4501 encoder->pre_pll_enable(encoder);
4503 /* Enable DPIO clock input */
4504 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4505 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4507 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4509 dpll |= DPLL_VCO_ENABLE;
4510 I915_WRITE(DPLL(pipe), dpll);
4511 POSTING_READ(DPLL(pipe));
4514 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4515 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4517 dpll_md = (crtc->config.pixel_multiplier - 1)
4518 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4519 I915_WRITE(DPLL_MD(pipe), dpll_md);
4520 POSTING_READ(DPLL_MD(pipe));
4522 if (crtc->config.has_dp_encoder)
4523 intel_dp_set_m_n(crtc);
4525 mutex_unlock(&dev_priv->dpio_lock);
4528 static void i9xx_update_pll(struct intel_crtc *crtc,
4529 intel_clock_t *reduced_clock,
4532 struct drm_device *dev = crtc->base.dev;
4533 struct drm_i915_private *dev_priv = dev->dev_private;
4534 struct intel_encoder *encoder;
4535 int pipe = crtc->pipe;
4538 struct dpll *clock = &crtc->config.dpll;
4540 i9xx_update_pll_dividers(crtc, reduced_clock);
4542 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4543 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4545 dpll = DPLL_VGA_MODE_DIS;
4547 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4548 dpll |= DPLLB_MODE_LVDS;
4550 dpll |= DPLLB_MODE_DAC_SERIAL;
4552 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4553 dpll |= (crtc->config.pixel_multiplier - 1)
4554 << SDVO_MULTIPLIER_SHIFT_HIRES;
4558 dpll |= DPLL_DVO_HIGH_SPEED;
4560 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4561 dpll |= DPLL_DVO_HIGH_SPEED;
4563 /* compute bitmask from p1 value */
4564 if (IS_PINEVIEW(dev))
4565 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4567 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4568 if (IS_G4X(dev) && reduced_clock)
4569 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4571 switch (clock->p2) {
4573 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4576 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4579 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4582 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4585 if (INTEL_INFO(dev)->gen >= 4)
4586 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4588 if (crtc->config.sdvo_tv_clock)
4589 dpll |= PLL_REF_INPUT_TVCLKINBC;
4590 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4591 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4592 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4594 dpll |= PLL_REF_INPUT_DREFCLK;
4596 dpll |= DPLL_VCO_ENABLE;
4597 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4598 POSTING_READ(DPLL(pipe));
4601 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4602 if (encoder->pre_pll_enable)
4603 encoder->pre_pll_enable(encoder);
4605 if (crtc->config.has_dp_encoder)
4606 intel_dp_set_m_n(crtc);
4608 I915_WRITE(DPLL(pipe), dpll);
4610 /* Wait for the clocks to stabilize. */
4611 POSTING_READ(DPLL(pipe));
4614 if (INTEL_INFO(dev)->gen >= 4) {
4615 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4616 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4617 I915_WRITE(DPLL_MD(pipe), dpll_md);
4619 /* The pixel multiplier can only be updated once the
4620 * DPLL is enabled and the clocks are stable.
4622 * So write it again.
4624 I915_WRITE(DPLL(pipe), dpll);
4628 static void i8xx_update_pll(struct intel_crtc *crtc,
4629 intel_clock_t *reduced_clock,
4632 struct drm_device *dev = crtc->base.dev;
4633 struct drm_i915_private *dev_priv = dev->dev_private;
4634 struct intel_encoder *encoder;
4635 int pipe = crtc->pipe;
4637 struct dpll *clock = &crtc->config.dpll;
4639 i9xx_update_pll_dividers(crtc, reduced_clock);
4641 dpll = DPLL_VGA_MODE_DIS;
4643 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4644 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4647 dpll |= PLL_P1_DIVIDE_BY_TWO;
4649 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4651 dpll |= PLL_P2_DIVIDE_BY_4;
4654 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4655 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4656 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4658 dpll |= PLL_REF_INPUT_DREFCLK;
4660 dpll |= DPLL_VCO_ENABLE;
4661 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4662 POSTING_READ(DPLL(pipe));
4665 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4666 if (encoder->pre_pll_enable)
4667 encoder->pre_pll_enable(encoder);
4669 I915_WRITE(DPLL(pipe), dpll);
4671 /* Wait for the clocks to stabilize. */
4672 POSTING_READ(DPLL(pipe));
4675 /* The pixel multiplier can only be updated once the
4676 * DPLL is enabled and the clocks are stable.
4678 * So write it again.
4680 I915_WRITE(DPLL(pipe), dpll);
4683 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
4685 struct drm_device *dev = intel_crtc->base.dev;
4686 struct drm_i915_private *dev_priv = dev->dev_private;
4687 enum pipe pipe = intel_crtc->pipe;
4688 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4689 struct drm_display_mode *adjusted_mode =
4690 &intel_crtc->config.adjusted_mode;
4691 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4692 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4694 /* We need to be careful not to changed the adjusted mode, for otherwise
4695 * the hw state checker will get angry at the mismatch. */
4696 crtc_vtotal = adjusted_mode->crtc_vtotal;
4697 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4699 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4700 /* the chip adds 2 halflines automatically */
4702 crtc_vblank_end -= 1;
4703 vsyncshift = adjusted_mode->crtc_hsync_start
4704 - adjusted_mode->crtc_htotal / 2;
4709 if (INTEL_INFO(dev)->gen > 3)
4710 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4712 I915_WRITE(HTOTAL(cpu_transcoder),
4713 (adjusted_mode->crtc_hdisplay - 1) |
4714 ((adjusted_mode->crtc_htotal - 1) << 16));
4715 I915_WRITE(HBLANK(cpu_transcoder),
4716 (adjusted_mode->crtc_hblank_start - 1) |
4717 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4718 I915_WRITE(HSYNC(cpu_transcoder),
4719 (adjusted_mode->crtc_hsync_start - 1) |
4720 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4722 I915_WRITE(VTOTAL(cpu_transcoder),
4723 (adjusted_mode->crtc_vdisplay - 1) |
4724 ((crtc_vtotal - 1) << 16));
4725 I915_WRITE(VBLANK(cpu_transcoder),
4726 (adjusted_mode->crtc_vblank_start - 1) |
4727 ((crtc_vblank_end - 1) << 16));
4728 I915_WRITE(VSYNC(cpu_transcoder),
4729 (adjusted_mode->crtc_vsync_start - 1) |
4730 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4732 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4733 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4734 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4736 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4737 (pipe == PIPE_B || pipe == PIPE_C))
4738 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4740 /* pipesrc controls the size that is scaled from, which should
4741 * always be the user's requested size.
4743 I915_WRITE(PIPESRC(pipe),
4744 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4747 static void intel_get_pipe_timings(struct intel_crtc *crtc,
4748 struct intel_crtc_config *pipe_config)
4750 struct drm_device *dev = crtc->base.dev;
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4755 tmp = I915_READ(HTOTAL(cpu_transcoder));
4756 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4757 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4758 tmp = I915_READ(HBLANK(cpu_transcoder));
4759 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4760 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4761 tmp = I915_READ(HSYNC(cpu_transcoder));
4762 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4763 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4765 tmp = I915_READ(VTOTAL(cpu_transcoder));
4766 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4767 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4768 tmp = I915_READ(VBLANK(cpu_transcoder));
4769 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4770 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4771 tmp = I915_READ(VSYNC(cpu_transcoder));
4772 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4773 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4775 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4776 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4777 pipe_config->adjusted_mode.crtc_vtotal += 1;
4778 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4781 tmp = I915_READ(PIPESRC(crtc->pipe));
4782 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4783 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4786 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4788 struct drm_device *dev = intel_crtc->base.dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4792 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4794 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4795 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4798 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4801 if (intel_crtc->config.requested_mode.clock >
4802 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4803 pipeconf |= PIPECONF_DOUBLE_WIDE;
4805 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4808 /* only g4x and later have fancy bpc/dither controls */
4809 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4810 pipeconf &= ~(PIPECONF_BPC_MASK |
4811 PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4813 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4814 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4815 pipeconf |= PIPECONF_DITHER_EN |
4816 PIPECONF_DITHER_TYPE_SP;
4818 switch (intel_crtc->config.pipe_bpp) {
4820 pipeconf |= PIPECONF_6BPC;
4823 pipeconf |= PIPECONF_8BPC;
4826 pipeconf |= PIPECONF_10BPC;
4829 /* Case prevented by intel_choose_pipe_bpp_dither. */
4834 if (HAS_PIPE_CXSR(dev)) {
4835 if (intel_crtc->lowfreq_avail) {
4836 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4837 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4839 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4840 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4844 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4845 if (!IS_GEN2(dev) &&
4846 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4847 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4849 pipeconf |= PIPECONF_PROGRESSIVE;
4851 if (IS_VALLEYVIEW(dev)) {
4852 if (intel_crtc->config.limited_color_range)
4853 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4855 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4858 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4859 POSTING_READ(PIPECONF(intel_crtc->pipe));
4862 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4864 struct drm_framebuffer *fb)
4866 struct drm_device *dev = crtc->dev;
4867 struct drm_i915_private *dev_priv = dev->dev_private;
4868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4869 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4870 int pipe = intel_crtc->pipe;
4871 int plane = intel_crtc->plane;
4872 int refclk, num_connectors = 0;
4873 intel_clock_t clock, reduced_clock;
4875 bool ok, has_reduced_clock = false;
4876 bool is_lvds = false;
4877 struct intel_encoder *encoder;
4878 const intel_limit_t *limit;
4881 for_each_encoder_on_crtc(dev, crtc, encoder) {
4882 switch (encoder->type) {
4883 case INTEL_OUTPUT_LVDS:
4891 refclk = i9xx_get_refclk(crtc, num_connectors);
4894 * Returns a set of divisors for the desired target clock with the given
4895 * refclk, or FALSE. The returned values represent the clock equation:
4896 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4898 limit = intel_limit(crtc, refclk);
4899 ok = dev_priv->display.find_dpll(limit, crtc,
4900 intel_crtc->config.port_clock,
4901 refclk, NULL, &clock);
4902 if (!ok && !intel_crtc->config.clock_set) {
4903 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4907 /* Ensure that the cursor is valid for the new mode before changing... */
4908 intel_crtc_update_cursor(crtc, true);
4910 if (is_lvds && dev_priv->lvds_downclock_avail) {
4912 * Ensure we match the reduced clock's P to the target clock.
4913 * If the clocks don't match, we can't switch the display clock
4914 * by using the FP0/FP1. In such case we will disable the LVDS
4915 * downclock feature.
4918 dev_priv->display.find_dpll(limit, crtc,
4919 dev_priv->lvds_downclock,
4923 /* Compat-code for transition, will disappear. */
4924 if (!intel_crtc->config.clock_set) {
4925 intel_crtc->config.dpll.n = clock.n;
4926 intel_crtc->config.dpll.m1 = clock.m1;
4927 intel_crtc->config.dpll.m2 = clock.m2;
4928 intel_crtc->config.dpll.p1 = clock.p1;
4929 intel_crtc->config.dpll.p2 = clock.p2;
4933 i8xx_update_pll(intel_crtc,
4934 has_reduced_clock ? &reduced_clock : NULL,
4936 else if (IS_VALLEYVIEW(dev))
4937 vlv_update_pll(intel_crtc);
4939 i9xx_update_pll(intel_crtc,
4940 has_reduced_clock ? &reduced_clock : NULL,
4943 /* Set up the display plane register */
4944 dspcntr = DISPPLANE_GAMMA_ENABLE;
4946 if (!IS_VALLEYVIEW(dev)) {
4948 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4950 dspcntr |= DISPPLANE_SEL_PIPE_B;
4953 intel_set_pipe_timings(intel_crtc);
4955 /* pipesrc and dspsize control the size that is scaled from,
4956 * which should always be the user's requested size.
4958 I915_WRITE(DSPSIZE(plane),
4959 ((mode->vdisplay - 1) << 16) |
4960 (mode->hdisplay - 1));
4961 I915_WRITE(DSPPOS(plane), 0);
4963 i9xx_set_pipeconf(intel_crtc);
4965 I915_WRITE(DSPCNTR(plane), dspcntr);
4966 POSTING_READ(DSPCNTR(plane));
4968 ret = intel_pipe_set_base(crtc, x, y, fb);
4970 intel_update_watermarks(dev);
4975 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4976 struct intel_crtc_config *pipe_config)
4978 struct drm_device *dev = crtc->base.dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4982 tmp = I915_READ(PFIT_CONTROL);
4984 if (INTEL_INFO(dev)->gen < 4) {
4985 if (crtc->pipe != PIPE_B)
4988 /* gen2/3 store dither state in pfit control, needs to match */
4989 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4991 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4995 if (!(tmp & PFIT_ENABLE))
4998 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4999 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5000 if (INTEL_INFO(dev)->gen < 5)
5001 pipe_config->gmch_pfit.lvds_border_bits =
5002 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5005 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5006 struct intel_crtc_config *pipe_config)
5008 struct drm_device *dev = crtc->base.dev;
5009 struct drm_i915_private *dev_priv = dev->dev_private;
5012 pipe_config->cpu_transcoder = crtc->pipe;
5014 tmp = I915_READ(PIPECONF(crtc->pipe));
5015 if (!(tmp & PIPECONF_ENABLE))
5018 intel_get_pipe_timings(crtc, pipe_config);
5020 i9xx_get_pfit_config(crtc, pipe_config);
5022 if (INTEL_INFO(dev)->gen >= 4) {
5023 tmp = I915_READ(DPLL_MD(crtc->pipe));
5024 pipe_config->pixel_multiplier =
5025 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5026 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5027 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5028 tmp = I915_READ(DPLL(crtc->pipe));
5029 pipe_config->pixel_multiplier =
5030 ((tmp & SDVO_MULTIPLIER_MASK)
5031 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5033 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5034 * port and will be fixed up in the encoder->get_config
5036 pipe_config->pixel_multiplier = 1;
5042 static void ironlake_init_pch_refclk(struct drm_device *dev)
5044 struct drm_i915_private *dev_priv = dev->dev_private;
5045 struct drm_mode_config *mode_config = &dev->mode_config;
5046 struct intel_encoder *encoder;
5048 bool has_lvds = false;
5049 bool has_cpu_edp = false;
5050 bool has_panel = false;
5051 bool has_ck505 = false;
5052 bool can_ssc = false;
5054 /* We need to take the global config into account */
5055 list_for_each_entry(encoder, &mode_config->encoder_list,
5057 switch (encoder->type) {
5058 case INTEL_OUTPUT_LVDS:
5062 case INTEL_OUTPUT_EDP:
5064 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
5070 if (HAS_PCH_IBX(dev)) {
5071 has_ck505 = dev_priv->vbt.display_clock_mode;
5072 can_ssc = has_ck505;
5078 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5079 has_panel, has_lvds, has_ck505);
5081 /* Ironlake: try to setup display ref clock before DPLL
5082 * enabling. This is only under driver's control after
5083 * PCH B stepping, previous chipset stepping should be
5084 * ignoring this setting.
5086 val = I915_READ(PCH_DREF_CONTROL);
5088 /* As we must carefully and slowly disable/enable each source in turn,
5089 * compute the final state we want first and check if we need to
5090 * make any changes at all.
5093 final &= ~DREF_NONSPREAD_SOURCE_MASK;
5095 final |= DREF_NONSPREAD_CK505_ENABLE;
5097 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5099 final &= ~DREF_SSC_SOURCE_MASK;
5100 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5101 final &= ~DREF_SSC1_ENABLE;
5104 final |= DREF_SSC_SOURCE_ENABLE;
5106 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5107 final |= DREF_SSC1_ENABLE;
5110 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5111 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5113 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5115 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5117 final |= DREF_SSC_SOURCE_DISABLE;
5118 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5124 /* Always enable nonspread source */
5125 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5128 val |= DREF_NONSPREAD_CK505_ENABLE;
5130 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5133 val &= ~DREF_SSC_SOURCE_MASK;
5134 val |= DREF_SSC_SOURCE_ENABLE;
5136 /* SSC must be turned on before enabling the CPU output */
5137 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5138 DRM_DEBUG_KMS("Using SSC on panel\n");
5139 val |= DREF_SSC1_ENABLE;
5141 val &= ~DREF_SSC1_ENABLE;
5143 /* Get SSC going before enabling the outputs */
5144 I915_WRITE(PCH_DREF_CONTROL, val);
5145 POSTING_READ(PCH_DREF_CONTROL);
5148 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5150 /* Enable CPU source on CPU attached eDP */
5152 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5153 DRM_DEBUG_KMS("Using SSC on eDP\n");
5154 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5157 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5159 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5161 I915_WRITE(PCH_DREF_CONTROL, val);
5162 POSTING_READ(PCH_DREF_CONTROL);
5165 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5167 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5169 /* Turn off CPU output */
5170 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5172 I915_WRITE(PCH_DREF_CONTROL, val);
5173 POSTING_READ(PCH_DREF_CONTROL);
5176 /* Turn off the SSC source */
5177 val &= ~DREF_SSC_SOURCE_MASK;
5178 val |= DREF_SSC_SOURCE_DISABLE;
5181 val &= ~DREF_SSC1_ENABLE;
5183 I915_WRITE(PCH_DREF_CONTROL, val);
5184 POSTING_READ(PCH_DREF_CONTROL);
5188 BUG_ON(val != final);
5191 /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5192 static void lpt_init_pch_refclk(struct drm_device *dev)
5194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 struct drm_mode_config *mode_config = &dev->mode_config;
5196 struct intel_encoder *encoder;
5197 bool has_vga = false;
5198 bool is_sdv = false;
5201 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5202 switch (encoder->type) {
5203 case INTEL_OUTPUT_ANALOG:
5212 mutex_lock(&dev_priv->dpio_lock);
5214 /* XXX: Rip out SDV support once Haswell ships for real. */
5215 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5218 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5219 tmp &= ~SBI_SSCCTL_DISABLE;
5220 tmp |= SBI_SSCCTL_PATHALT;
5221 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5225 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5226 tmp &= ~SBI_SSCCTL_PATHALT;
5227 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5230 tmp = I915_READ(SOUTH_CHICKEN2);
5231 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5232 I915_WRITE(SOUTH_CHICKEN2, tmp);
5234 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5235 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5236 DRM_ERROR("FDI mPHY reset assert timeout\n");
5238 tmp = I915_READ(SOUTH_CHICKEN2);
5239 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5240 I915_WRITE(SOUTH_CHICKEN2, tmp);
5242 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5243 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5245 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5248 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5249 tmp &= ~(0xFF << 24);
5250 tmp |= (0x12 << 24);
5251 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5254 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5256 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5259 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5261 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5263 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5265 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5268 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5269 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5270 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5272 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5273 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5274 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5276 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5278 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5280 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5282 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5285 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5286 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5287 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5289 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5290 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5291 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5294 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5297 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5299 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5302 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5305 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5308 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5310 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5313 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5315 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5316 tmp &= ~(0xFF << 16);
5317 tmp |= (0x1C << 16);
5318 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5320 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5321 tmp &= ~(0xFF << 16);
5322 tmp |= (0x1C << 16);
5323 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5326 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5328 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5330 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5332 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5334 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5335 tmp &= ~(0xF << 28);
5337 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5339 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5340 tmp &= ~(0xF << 28);
5342 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5345 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5346 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5347 tmp |= SBI_DBUFF0_ENABLE;
5348 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
5350 mutex_unlock(&dev_priv->dpio_lock);
5354 * Initialize reference clocks when the driver loads
5356 void intel_init_pch_refclk(struct drm_device *dev)
5358 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5359 ironlake_init_pch_refclk(dev);
5360 else if (HAS_PCH_LPT(dev))
5361 lpt_init_pch_refclk(dev);
5364 static int ironlake_get_refclk(struct drm_crtc *crtc)
5366 struct drm_device *dev = crtc->dev;
5367 struct drm_i915_private *dev_priv = dev->dev_private;
5368 struct intel_encoder *encoder;
5369 int num_connectors = 0;
5370 bool is_lvds = false;
5372 for_each_encoder_on_crtc(dev, crtc, encoder) {
5373 switch (encoder->type) {
5374 case INTEL_OUTPUT_LVDS:
5381 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5382 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5383 dev_priv->vbt.lvds_ssc_freq);
5384 return dev_priv->vbt.lvds_ssc_freq * 1000;
5390 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
5392 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5394 int pipe = intel_crtc->pipe;
5397 val = I915_READ(PIPECONF(pipe));
5399 val &= ~PIPECONF_BPC_MASK;
5400 switch (intel_crtc->config.pipe_bpp) {
5402 val |= PIPECONF_6BPC;
5405 val |= PIPECONF_8BPC;
5408 val |= PIPECONF_10BPC;
5411 val |= PIPECONF_12BPC;
5414 /* Case prevented by intel_choose_pipe_bpp_dither. */
5418 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5419 if (intel_crtc->config.dither)
5420 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5422 val &= ~PIPECONF_INTERLACE_MASK;
5423 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5424 val |= PIPECONF_INTERLACED_ILK;
5426 val |= PIPECONF_PROGRESSIVE;
5428 if (intel_crtc->config.limited_color_range)
5429 val |= PIPECONF_COLOR_RANGE_SELECT;
5431 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5433 I915_WRITE(PIPECONF(pipe), val);
5434 POSTING_READ(PIPECONF(pipe));
5438 * Set up the pipe CSC unit.
5440 * Currently only full range RGB to limited range RGB conversion
5441 * is supported, but eventually this should handle various
5442 * RGB<->YCbCr scenarios as well.
5444 static void intel_set_pipe_csc(struct drm_crtc *crtc)
5446 struct drm_device *dev = crtc->dev;
5447 struct drm_i915_private *dev_priv = dev->dev_private;
5448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5449 int pipe = intel_crtc->pipe;
5450 uint16_t coeff = 0x7800; /* 1.0 */
5453 * TODO: Check what kind of values actually come out of the pipe
5454 * with these coeff/postoff values and adjust to get the best
5455 * accuracy. Perhaps we even need to take the bpc value into
5459 if (intel_crtc->config.limited_color_range)
5460 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5463 * GY/GU and RY/RU should be the other way around according
5464 * to BSpec, but reality doesn't agree. Just set them up in
5465 * a way that results in the correct picture.
5467 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5468 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5470 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5471 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5473 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5474 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5476 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5477 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5478 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5480 if (INTEL_INFO(dev)->gen > 6) {
5481 uint16_t postoff = 0;
5483 if (intel_crtc->config.limited_color_range)
5484 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5486 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5487 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5488 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5490 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5492 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5494 if (intel_crtc->config.limited_color_range)
5495 mode |= CSC_BLACK_SCREEN_OFFSET;
5497 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5501 static void haswell_set_pipeconf(struct drm_crtc *crtc)
5503 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5505 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5508 val = I915_READ(PIPECONF(cpu_transcoder));
5510 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5511 if (intel_crtc->config.dither)
5512 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5514 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5515 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5516 val |= PIPECONF_INTERLACED_ILK;
5518 val |= PIPECONF_PROGRESSIVE;
5520 I915_WRITE(PIPECONF(cpu_transcoder), val);
5521 POSTING_READ(PIPECONF(cpu_transcoder));
5524 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5525 intel_clock_t *clock,
5526 bool *has_reduced_clock,
5527 intel_clock_t *reduced_clock)
5529 struct drm_device *dev = crtc->dev;
5530 struct drm_i915_private *dev_priv = dev->dev_private;
5531 struct intel_encoder *intel_encoder;
5533 const intel_limit_t *limit;
5534 bool ret, is_lvds = false;
5536 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5537 switch (intel_encoder->type) {
5538 case INTEL_OUTPUT_LVDS:
5544 refclk = ironlake_get_refclk(crtc);
5547 * Returns a set of divisors for the desired target clock with the given
5548 * refclk, or FALSE. The returned values represent the clock equation:
5549 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5551 limit = intel_limit(crtc, refclk);
5552 ret = dev_priv->display.find_dpll(limit, crtc,
5553 to_intel_crtc(crtc)->config.port_clock,
5554 refclk, NULL, clock);
5558 if (is_lvds && dev_priv->lvds_downclock_avail) {
5560 * Ensure we match the reduced clock's P to the target clock.
5561 * If the clocks don't match, we can't switch the display clock
5562 * by using the FP0/FP1. In such case we will disable the LVDS
5563 * downclock feature.
5565 *has_reduced_clock =
5566 dev_priv->display.find_dpll(limit, crtc,
5567 dev_priv->lvds_downclock,
5575 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5577 struct drm_i915_private *dev_priv = dev->dev_private;
5580 temp = I915_READ(SOUTH_CHICKEN1);
5581 if (temp & FDI_BC_BIFURCATION_SELECT)
5584 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5585 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5587 temp |= FDI_BC_BIFURCATION_SELECT;
5588 DRM_DEBUG_KMS("enabling fdi C rx\n");
5589 I915_WRITE(SOUTH_CHICKEN1, temp);
5590 POSTING_READ(SOUTH_CHICKEN1);
5593 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
5595 struct drm_device *dev = intel_crtc->base.dev;
5596 struct drm_i915_private *dev_priv = dev->dev_private;
5598 switch (intel_crtc->pipe) {
5602 if (intel_crtc->config.fdi_lanes > 2)
5603 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5605 cpt_enable_fdi_bc_bifurcation(dev);
5609 cpt_enable_fdi_bc_bifurcation(dev);
5617 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5620 * Account for spread spectrum to avoid
5621 * oversubscribing the link. Max center spread
5622 * is 2.5%; use 5% for safety's sake.
5624 u32 bps = target_clock * bpp * 21 / 20;
5625 return bps / (link_bw * 8) + 1;
5628 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5630 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5633 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5635 intel_clock_t *reduced_clock, u32 *fp2)
5637 struct drm_crtc *crtc = &intel_crtc->base;
5638 struct drm_device *dev = crtc->dev;
5639 struct drm_i915_private *dev_priv = dev->dev_private;
5640 struct intel_encoder *intel_encoder;
5642 int factor, num_connectors = 0;
5643 bool is_lvds = false, is_sdvo = false;
5645 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5646 switch (intel_encoder->type) {
5647 case INTEL_OUTPUT_LVDS:
5650 case INTEL_OUTPUT_SDVO:
5651 case INTEL_OUTPUT_HDMI:
5659 /* Enable autotuning of the PLL clock (if permissible) */
5662 if ((intel_panel_use_ssc(dev_priv) &&
5663 dev_priv->vbt.lvds_ssc_freq == 100) ||
5664 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5666 } else if (intel_crtc->config.sdvo_tv_clock)
5669 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
5672 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5678 dpll |= DPLLB_MODE_LVDS;
5680 dpll |= DPLLB_MODE_DAC_SERIAL;
5682 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5683 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5686 dpll |= DPLL_DVO_HIGH_SPEED;
5687 if (intel_crtc->config.has_dp_encoder)
5688 dpll |= DPLL_DVO_HIGH_SPEED;
5690 /* compute bitmask from p1 value */
5691 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5693 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5695 switch (intel_crtc->config.dpll.p2) {
5697 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5700 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5703 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5706 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5710 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5711 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5713 dpll |= PLL_REF_INPUT_DREFCLK;
5718 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5720 struct drm_framebuffer *fb)
5722 struct drm_device *dev = crtc->dev;
5723 struct drm_i915_private *dev_priv = dev->dev_private;
5724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5725 int pipe = intel_crtc->pipe;
5726 int plane = intel_crtc->plane;
5727 int num_connectors = 0;
5728 intel_clock_t clock, reduced_clock;
5729 u32 dpll = 0, fp = 0, fp2 = 0;
5730 bool ok, has_reduced_clock = false;
5731 bool is_lvds = false;
5732 struct intel_encoder *encoder;
5735 for_each_encoder_on_crtc(dev, crtc, encoder) {
5736 switch (encoder->type) {
5737 case INTEL_OUTPUT_LVDS:
5745 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5746 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5748 ok = ironlake_compute_clocks(crtc, &clock,
5749 &has_reduced_clock, &reduced_clock);
5750 if (!ok && !intel_crtc->config.clock_set) {
5751 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5754 /* Compat-code for transition, will disappear. */
5755 if (!intel_crtc->config.clock_set) {
5756 intel_crtc->config.dpll.n = clock.n;
5757 intel_crtc->config.dpll.m1 = clock.m1;
5758 intel_crtc->config.dpll.m2 = clock.m2;
5759 intel_crtc->config.dpll.p1 = clock.p1;
5760 intel_crtc->config.dpll.p2 = clock.p2;
5763 /* Ensure that the cursor is valid for the new mode before changing... */
5764 intel_crtc_update_cursor(crtc, true);
5766 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5767 if (intel_crtc->config.has_pch_encoder) {
5768 struct intel_pch_pll *pll;
5770 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
5771 if (has_reduced_clock)
5772 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
5774 dpll = ironlake_compute_dpll(intel_crtc,
5775 &fp, &reduced_clock,
5776 has_reduced_clock ? &fp2 : NULL);
5778 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5780 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5785 intel_put_pch_pll(intel_crtc);
5787 if (intel_crtc->config.has_dp_encoder)
5788 intel_dp_set_m_n(intel_crtc);
5790 for_each_encoder_on_crtc(dev, crtc, encoder)
5791 if (encoder->pre_pll_enable)
5792 encoder->pre_pll_enable(encoder);
5794 if (intel_crtc->pch_pll) {
5795 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5797 /* Wait for the clocks to stabilize. */
5798 POSTING_READ(intel_crtc->pch_pll->pll_reg);
5801 /* The pixel multiplier can only be updated once the
5802 * DPLL is enabled and the clocks are stable.
5804 * So write it again.
5806 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5809 intel_crtc->lowfreq_avail = false;
5810 if (intel_crtc->pch_pll) {
5811 if (is_lvds && has_reduced_clock && i915_powersave) {
5812 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5813 intel_crtc->lowfreq_avail = true;
5815 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5819 intel_set_pipe_timings(intel_crtc);
5821 if (intel_crtc->config.has_pch_encoder) {
5822 intel_cpu_transcoder_set_m_n(intel_crtc,
5823 &intel_crtc->config.fdi_m_n);
5826 if (IS_IVYBRIDGE(dev))
5827 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
5829 ironlake_set_pipeconf(crtc);
5831 /* Set up the display plane register */
5832 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5833 POSTING_READ(DSPCNTR(plane));
5835 ret = intel_pipe_set_base(crtc, x, y, fb);
5837 intel_update_watermarks(dev);
5842 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5843 struct intel_crtc_config *pipe_config)
5845 struct drm_device *dev = crtc->base.dev;
5846 struct drm_i915_private *dev_priv = dev->dev_private;
5847 enum transcoder transcoder = pipe_config->cpu_transcoder;
5849 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5850 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5851 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5853 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5854 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5855 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5858 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5859 struct intel_crtc_config *pipe_config)
5861 struct drm_device *dev = crtc->base.dev;
5862 struct drm_i915_private *dev_priv = dev->dev_private;
5865 tmp = I915_READ(PF_CTL(crtc->pipe));
5867 if (tmp & PF_ENABLE) {
5868 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5869 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
5871 /* We currently do not free assignements of panel fitters on
5872 * ivb/hsw (since we don't use the higher upscaling modes which
5873 * differentiates them) so just WARN about this case for now. */
5875 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5876 PF_PIPE_SEL_IVB(crtc->pipe));
5881 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5882 struct intel_crtc_config *pipe_config)
5884 struct drm_device *dev = crtc->base.dev;
5885 struct drm_i915_private *dev_priv = dev->dev_private;
5888 pipe_config->cpu_transcoder = crtc->pipe;
5890 tmp = I915_READ(PIPECONF(crtc->pipe));
5891 if (!(tmp & PIPECONF_ENABLE))
5894 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5895 pipe_config->has_pch_encoder = true;
5897 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5898 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5899 FDI_DP_PORT_WIDTH_SHIFT) + 1;
5901 ironlake_get_fdi_m_n_config(crtc, pipe_config);
5903 /* XXX: Can't properly read out the pch dpll pixel multiplier
5904 * since we don't have state tracking for pch clocks yet. */
5905 pipe_config->pixel_multiplier = 1;
5907 pipe_config->pixel_multiplier = 1;
5910 intel_get_pipe_timings(crtc, pipe_config);
5912 ironlake_get_pfit_config(crtc, pipe_config);
5917 static void haswell_modeset_global_resources(struct drm_device *dev)
5919 bool enable = false;
5920 struct intel_crtc *crtc;
5922 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5923 if (!crtc->base.enabled)
5926 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5927 crtc->config.cpu_transcoder != TRANSCODER_EDP)
5931 intel_set_power_well(dev, enable);
5934 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5936 struct drm_framebuffer *fb)
5938 struct drm_device *dev = crtc->dev;
5939 struct drm_i915_private *dev_priv = dev->dev_private;
5940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5941 int plane = intel_crtc->plane;
5944 if (!intel_ddi_pll_mode_set(crtc))
5947 /* Ensure that the cursor is valid for the new mode before changing... */
5948 intel_crtc_update_cursor(crtc, true);
5950 if (intel_crtc->config.has_dp_encoder)
5951 intel_dp_set_m_n(intel_crtc);
5953 intel_crtc->lowfreq_avail = false;
5955 intel_set_pipe_timings(intel_crtc);
5957 if (intel_crtc->config.has_pch_encoder) {
5958 intel_cpu_transcoder_set_m_n(intel_crtc,
5959 &intel_crtc->config.fdi_m_n);
5962 haswell_set_pipeconf(crtc);
5964 intel_set_pipe_csc(crtc);
5966 /* Set up the display plane register */
5967 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
5968 POSTING_READ(DSPCNTR(plane));
5970 ret = intel_pipe_set_base(crtc, x, y, fb);
5972 intel_update_watermarks(dev);
5977 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5978 struct intel_crtc_config *pipe_config)
5980 struct drm_device *dev = crtc->base.dev;
5981 struct drm_i915_private *dev_priv = dev->dev_private;
5982 enum intel_display_power_domain pfit_domain;
5985 pipe_config->cpu_transcoder = crtc->pipe;
5986 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5987 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5988 enum pipe trans_edp_pipe;
5989 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5991 WARN(1, "unknown pipe linked to edp transcoder\n");
5992 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5993 case TRANS_DDI_EDP_INPUT_A_ON:
5994 trans_edp_pipe = PIPE_A;
5996 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5997 trans_edp_pipe = PIPE_B;
5999 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6000 trans_edp_pipe = PIPE_C;
6004 if (trans_edp_pipe == crtc->pipe)
6005 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6008 if (!intel_display_power_enabled(dev,
6009 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6012 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
6013 if (!(tmp & PIPECONF_ENABLE))
6017 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6018 * DDI E. So just check whether this pipe is wired to DDI E and whether
6019 * the PCH transcoder is on.
6021 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6022 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6023 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6024 pipe_config->has_pch_encoder = true;
6026 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6027 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6028 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6030 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6033 intel_get_pipe_timings(crtc, pipe_config);
6035 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6036 if (intel_display_power_enabled(dev, pfit_domain))
6037 ironlake_get_pfit_config(crtc, pipe_config);
6039 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6040 (I915_READ(IPS_CTL) & IPS_ENABLE);
6042 pipe_config->pixel_multiplier = 1;
6047 static int intel_crtc_mode_set(struct drm_crtc *crtc,
6049 struct drm_framebuffer *fb)
6051 struct drm_device *dev = crtc->dev;
6052 struct drm_i915_private *dev_priv = dev->dev_private;
6053 struct drm_encoder_helper_funcs *encoder_funcs;
6054 struct intel_encoder *encoder;
6055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6056 struct drm_display_mode *adjusted_mode =
6057 &intel_crtc->config.adjusted_mode;
6058 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6059 int pipe = intel_crtc->pipe;
6062 drm_vblank_pre_modeset(dev, pipe);
6064 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6066 drm_vblank_post_modeset(dev, pipe);
6071 for_each_encoder_on_crtc(dev, crtc, encoder) {
6072 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6073 encoder->base.base.id,
6074 drm_get_encoder_name(&encoder->base),
6075 mode->base.id, mode->name);
6076 if (encoder->mode_set) {
6077 encoder->mode_set(encoder);
6079 encoder_funcs = encoder->base.helper_private;
6080 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6087 static bool intel_eld_uptodate(struct drm_connector *connector,
6088 int reg_eldv, uint32_t bits_eldv,
6089 int reg_elda, uint32_t bits_elda,
6092 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6093 uint8_t *eld = connector->eld;
6096 i = I915_READ(reg_eldv);
6105 i = I915_READ(reg_elda);
6107 I915_WRITE(reg_elda, i);
6109 for (i = 0; i < eld[2]; i++)
6110 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6116 static void g4x_write_eld(struct drm_connector *connector,
6117 struct drm_crtc *crtc)
6119 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6120 uint8_t *eld = connector->eld;
6125 i = I915_READ(G4X_AUD_VID_DID);
6127 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6128 eldv = G4X_ELDV_DEVCL_DEVBLC;
6130 eldv = G4X_ELDV_DEVCTG;
6132 if (intel_eld_uptodate(connector,
6133 G4X_AUD_CNTL_ST, eldv,
6134 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6135 G4X_HDMIW_HDMIEDID))
6138 i = I915_READ(G4X_AUD_CNTL_ST);
6139 i &= ~(eldv | G4X_ELD_ADDR);
6140 len = (i >> 9) & 0x1f; /* ELD buffer size */
6141 I915_WRITE(G4X_AUD_CNTL_ST, i);
6146 len = min_t(uint8_t, eld[2], len);
6147 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6148 for (i = 0; i < len; i++)
6149 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6151 i = I915_READ(G4X_AUD_CNTL_ST);
6153 I915_WRITE(G4X_AUD_CNTL_ST, i);
6156 static void haswell_write_eld(struct drm_connector *connector,
6157 struct drm_crtc *crtc)
6159 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6160 uint8_t *eld = connector->eld;
6161 struct drm_device *dev = crtc->dev;
6162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6166 int pipe = to_intel_crtc(crtc)->pipe;
6169 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6170 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6171 int aud_config = HSW_AUD_CFG(pipe);
6172 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6175 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6177 /* Audio output enable */
6178 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6179 tmp = I915_READ(aud_cntrl_st2);
6180 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6181 I915_WRITE(aud_cntrl_st2, tmp);
6183 /* Wait for 1 vertical blank */
6184 intel_wait_for_vblank(dev, pipe);
6186 /* Set ELD valid state */
6187 tmp = I915_READ(aud_cntrl_st2);
6188 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6189 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6190 I915_WRITE(aud_cntrl_st2, tmp);
6191 tmp = I915_READ(aud_cntrl_st2);
6192 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6194 /* Enable HDMI mode */
6195 tmp = I915_READ(aud_config);
6196 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6197 /* clear N_programing_enable and N_value_index */
6198 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6199 I915_WRITE(aud_config, tmp);
6201 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6203 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6204 intel_crtc->eld_vld = true;
6206 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6207 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6208 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6209 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6211 I915_WRITE(aud_config, 0);
6213 if (intel_eld_uptodate(connector,
6214 aud_cntrl_st2, eldv,
6215 aud_cntl_st, IBX_ELD_ADDRESS,
6219 i = I915_READ(aud_cntrl_st2);
6221 I915_WRITE(aud_cntrl_st2, i);
6226 i = I915_READ(aud_cntl_st);
6227 i &= ~IBX_ELD_ADDRESS;
6228 I915_WRITE(aud_cntl_st, i);
6229 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6230 DRM_DEBUG_DRIVER("port num:%d\n", i);
6232 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6233 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6234 for (i = 0; i < len; i++)
6235 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6237 i = I915_READ(aud_cntrl_st2);
6239 I915_WRITE(aud_cntrl_st2, i);
6243 static void ironlake_write_eld(struct drm_connector *connector,
6244 struct drm_crtc *crtc)
6246 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6247 uint8_t *eld = connector->eld;
6255 int pipe = to_intel_crtc(crtc)->pipe;
6257 if (HAS_PCH_IBX(connector->dev)) {
6258 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6259 aud_config = IBX_AUD_CFG(pipe);
6260 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6261 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6263 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6264 aud_config = CPT_AUD_CFG(pipe);
6265 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6266 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6269 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6271 i = I915_READ(aud_cntl_st);
6272 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6274 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6275 /* operate blindly on all ports */
6276 eldv = IBX_ELD_VALIDB;
6277 eldv |= IBX_ELD_VALIDB << 4;
6278 eldv |= IBX_ELD_VALIDB << 8;
6280 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6281 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6284 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6285 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6286 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6287 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6289 I915_WRITE(aud_config, 0);
6291 if (intel_eld_uptodate(connector,
6292 aud_cntrl_st2, eldv,
6293 aud_cntl_st, IBX_ELD_ADDRESS,
6297 i = I915_READ(aud_cntrl_st2);
6299 I915_WRITE(aud_cntrl_st2, i);
6304 i = I915_READ(aud_cntl_st);
6305 i &= ~IBX_ELD_ADDRESS;
6306 I915_WRITE(aud_cntl_st, i);
6308 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6309 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6310 for (i = 0; i < len; i++)
6311 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6313 i = I915_READ(aud_cntrl_st2);
6315 I915_WRITE(aud_cntrl_st2, i);
6318 void intel_write_eld(struct drm_encoder *encoder,
6319 struct drm_display_mode *mode)
6321 struct drm_crtc *crtc = encoder->crtc;
6322 struct drm_connector *connector;
6323 struct drm_device *dev = encoder->dev;
6324 struct drm_i915_private *dev_priv = dev->dev_private;
6326 connector = drm_select_eld(encoder, mode);
6330 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6332 drm_get_connector_name(connector),
6333 connector->encoder->base.id,
6334 drm_get_encoder_name(connector->encoder));
6336 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6338 if (dev_priv->display.write_eld)
6339 dev_priv->display.write_eld(connector, crtc);
6342 /** Loads the palette/gamma unit for the CRTC with the prepared values */
6343 void intel_crtc_load_lut(struct drm_crtc *crtc)
6345 struct drm_device *dev = crtc->dev;
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6348 enum pipe pipe = intel_crtc->pipe;
6349 int palreg = PALETTE(pipe);
6351 bool reenable_ips = false;
6353 /* The clocks have to be on to load the palette. */
6354 if (!crtc->enabled || !intel_crtc->active)
6357 if (!HAS_PCH_SPLIT(dev_priv->dev))
6358 assert_pll_enabled(dev_priv, pipe);
6360 /* use legacy palette for Ironlake */
6361 if (HAS_PCH_SPLIT(dev))
6362 palreg = LGC_PALETTE(pipe);
6364 /* Workaround : Do not read or write the pipe palette/gamma data while
6365 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6367 if (intel_crtc->config.ips_enabled &&
6368 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6369 GAMMA_MODE_MODE_SPLIT)) {
6370 hsw_disable_ips(intel_crtc);
6371 reenable_ips = true;
6374 for (i = 0; i < 256; i++) {
6375 I915_WRITE(palreg + 4 * i,
6376 (intel_crtc->lut_r[i] << 16) |
6377 (intel_crtc->lut_g[i] << 8) |
6378 intel_crtc->lut_b[i]);
6382 hsw_enable_ips(intel_crtc);
6385 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6387 struct drm_device *dev = crtc->dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
6389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6390 bool visible = base != 0;
6393 if (intel_crtc->cursor_visible == visible)
6396 cntl = I915_READ(_CURACNTR);
6398 /* On these chipsets we can only modify the base whilst
6399 * the cursor is disabled.
6401 I915_WRITE(_CURABASE, base);
6403 cntl &= ~(CURSOR_FORMAT_MASK);
6404 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6405 cntl |= CURSOR_ENABLE |
6406 CURSOR_GAMMA_ENABLE |
6409 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6410 I915_WRITE(_CURACNTR, cntl);
6412 intel_crtc->cursor_visible = visible;
6415 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6417 struct drm_device *dev = crtc->dev;
6418 struct drm_i915_private *dev_priv = dev->dev_private;
6419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6420 int pipe = intel_crtc->pipe;
6421 bool visible = base != 0;
6423 if (intel_crtc->cursor_visible != visible) {
6424 uint32_t cntl = I915_READ(CURCNTR(pipe));
6426 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6427 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6428 cntl |= pipe << 28; /* Connect to correct pipe */
6430 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6431 cntl |= CURSOR_MODE_DISABLE;
6433 I915_WRITE(CURCNTR(pipe), cntl);
6435 intel_crtc->cursor_visible = visible;
6437 /* and commit changes on next vblank */
6438 I915_WRITE(CURBASE(pipe), base);
6441 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6443 struct drm_device *dev = crtc->dev;
6444 struct drm_i915_private *dev_priv = dev->dev_private;
6445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6446 int pipe = intel_crtc->pipe;
6447 bool visible = base != 0;
6449 if (intel_crtc->cursor_visible != visible) {
6450 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6452 cntl &= ~CURSOR_MODE;
6453 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6455 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6456 cntl |= CURSOR_MODE_DISABLE;
6458 if (IS_HASWELL(dev))
6459 cntl |= CURSOR_PIPE_CSC_ENABLE;
6460 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6462 intel_crtc->cursor_visible = visible;
6464 /* and commit changes on next vblank */
6465 I915_WRITE(CURBASE_IVB(pipe), base);
6468 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6469 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6472 struct drm_device *dev = crtc->dev;
6473 struct drm_i915_private *dev_priv = dev->dev_private;
6474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6475 int pipe = intel_crtc->pipe;
6476 int x = intel_crtc->cursor_x;
6477 int y = intel_crtc->cursor_y;
6483 if (on && crtc->enabled && crtc->fb) {
6484 base = intel_crtc->cursor_addr;
6485 if (x > (int) crtc->fb->width)
6488 if (y > (int) crtc->fb->height)
6494 if (x + intel_crtc->cursor_width < 0)
6497 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6500 pos |= x << CURSOR_X_SHIFT;
6503 if (y + intel_crtc->cursor_height < 0)
6506 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6509 pos |= y << CURSOR_Y_SHIFT;
6511 visible = base != 0;
6512 if (!visible && !intel_crtc->cursor_visible)
6515 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6516 I915_WRITE(CURPOS_IVB(pipe), pos);
6517 ivb_update_cursor(crtc, base);
6519 I915_WRITE(CURPOS(pipe), pos);
6520 if (IS_845G(dev) || IS_I865G(dev))
6521 i845_update_cursor(crtc, base);
6523 i9xx_update_cursor(crtc, base);
6527 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6528 struct drm_file *file,
6530 uint32_t width, uint32_t height)
6532 struct drm_device *dev = crtc->dev;
6533 struct drm_i915_private *dev_priv = dev->dev_private;
6534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6535 struct drm_i915_gem_object *obj;
6539 /* if we want to turn off the cursor ignore width and height */
6541 DRM_DEBUG_KMS("cursor off\n");
6544 mutex_lock(&dev->struct_mutex);
6548 /* Currently we only support 64x64 cursors */
6549 if (width != 64 || height != 64) {
6550 DRM_ERROR("we currently only support 64x64 cursors\n");
6554 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6555 if (&obj->base == NULL)
6558 if (obj->base.size < width * height * 4) {
6559 DRM_ERROR("buffer is to small\n");
6564 /* we only need to pin inside GTT if cursor is non-phy */
6565 mutex_lock(&dev->struct_mutex);
6566 if (!dev_priv->info->cursor_needs_physical) {
6569 if (obj->tiling_mode) {
6570 DRM_ERROR("cursor cannot be tiled\n");
6575 /* Note that the w/a also requires 2 PTE of padding following
6576 * the bo. We currently fill all unused PTE with the shadow
6577 * page and so we should always have valid PTE following the
6578 * cursor preventing the VT-d warning.
6581 if (need_vtd_wa(dev))
6582 alignment = 64*1024;
6584 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6586 DRM_ERROR("failed to move cursor bo into the GTT\n");
6590 ret = i915_gem_object_put_fence(obj);
6592 DRM_ERROR("failed to release fence for cursor");
6596 addr = obj->gtt_offset;
6598 int align = IS_I830(dev) ? 16 * 1024 : 256;
6599 ret = i915_gem_attach_phys_object(dev, obj,
6600 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6603 DRM_ERROR("failed to attach phys object\n");
6606 addr = obj->phys_obj->handle->busaddr;
6610 I915_WRITE(CURSIZE, (height << 12) | width);
6613 if (intel_crtc->cursor_bo) {
6614 if (dev_priv->info->cursor_needs_physical) {
6615 if (intel_crtc->cursor_bo != obj)
6616 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6618 i915_gem_object_unpin(intel_crtc->cursor_bo);
6619 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6622 mutex_unlock(&dev->struct_mutex);
6624 intel_crtc->cursor_addr = addr;
6625 intel_crtc->cursor_bo = obj;
6626 intel_crtc->cursor_width = width;
6627 intel_crtc->cursor_height = height;
6629 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6633 i915_gem_object_unpin(obj);
6635 mutex_unlock(&dev->struct_mutex);
6637 drm_gem_object_unreference_unlocked(&obj->base);
6641 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6645 intel_crtc->cursor_x = x;
6646 intel_crtc->cursor_y = y;
6648 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6653 /** Sets the color ramps on behalf of RandR */
6654 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6655 u16 blue, int regno)
6657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6659 intel_crtc->lut_r[regno] = red >> 8;
6660 intel_crtc->lut_g[regno] = green >> 8;
6661 intel_crtc->lut_b[regno] = blue >> 8;
6664 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6665 u16 *blue, int regno)
6667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6669 *red = intel_crtc->lut_r[regno] << 8;
6670 *green = intel_crtc->lut_g[regno] << 8;
6671 *blue = intel_crtc->lut_b[regno] << 8;
6674 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
6675 u16 *blue, uint32_t start, uint32_t size)
6677 int end = (start + size > 256) ? 256 : start + size, i;
6678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6680 for (i = start; i < end; i++) {
6681 intel_crtc->lut_r[i] = red[i] >> 8;
6682 intel_crtc->lut_g[i] = green[i] >> 8;
6683 intel_crtc->lut_b[i] = blue[i] >> 8;
6686 intel_crtc_load_lut(crtc);
6689 /* VESA 640x480x72Hz mode to set on the pipe */
6690 static struct drm_display_mode load_detect_mode = {
6691 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6692 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6695 static struct drm_framebuffer *
6696 intel_framebuffer_create(struct drm_device *dev,
6697 struct drm_mode_fb_cmd2 *mode_cmd,
6698 struct drm_i915_gem_object *obj)
6700 struct intel_framebuffer *intel_fb;
6703 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6705 drm_gem_object_unreference_unlocked(&obj->base);
6706 return ERR_PTR(-ENOMEM);
6709 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6711 drm_gem_object_unreference_unlocked(&obj->base);
6713 return ERR_PTR(ret);
6716 return &intel_fb->base;
6720 intel_framebuffer_pitch_for_width(int width, int bpp)
6722 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6723 return ALIGN(pitch, 64);
6727 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6729 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6730 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6733 static struct drm_framebuffer *
6734 intel_framebuffer_create_for_mode(struct drm_device *dev,
6735 struct drm_display_mode *mode,
6738 struct drm_i915_gem_object *obj;
6739 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
6741 obj = i915_gem_alloc_object(dev,
6742 intel_framebuffer_size_for_mode(mode, bpp));
6744 return ERR_PTR(-ENOMEM);
6746 mode_cmd.width = mode->hdisplay;
6747 mode_cmd.height = mode->vdisplay;
6748 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6750 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
6752 return intel_framebuffer_create(dev, &mode_cmd, obj);
6755 static struct drm_framebuffer *
6756 mode_fits_in_fbdev(struct drm_device *dev,
6757 struct drm_display_mode *mode)
6759 struct drm_i915_private *dev_priv = dev->dev_private;
6760 struct drm_i915_gem_object *obj;
6761 struct drm_framebuffer *fb;
6763 if (dev_priv->fbdev == NULL)
6766 obj = dev_priv->fbdev->ifb.obj;
6770 fb = &dev_priv->fbdev->ifb.base;
6771 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6772 fb->bits_per_pixel))
6775 if (obj->base.size < mode->vdisplay * fb->pitches[0])
6781 bool intel_get_load_detect_pipe(struct drm_connector *connector,
6782 struct drm_display_mode *mode,
6783 struct intel_load_detect_pipe *old)
6785 struct intel_crtc *intel_crtc;
6786 struct intel_encoder *intel_encoder =
6787 intel_attached_encoder(connector);
6788 struct drm_crtc *possible_crtc;
6789 struct drm_encoder *encoder = &intel_encoder->base;
6790 struct drm_crtc *crtc = NULL;
6791 struct drm_device *dev = encoder->dev;
6792 struct drm_framebuffer *fb;
6795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6796 connector->base.id, drm_get_connector_name(connector),
6797 encoder->base.id, drm_get_encoder_name(encoder));
6800 * Algorithm gets a little messy:
6802 * - if the connector already has an assigned crtc, use it (but make
6803 * sure it's on first)
6805 * - try to find the first unused crtc that can drive this connector,
6806 * and use that if we find one
6809 /* See if we already have a CRTC for this connector */
6810 if (encoder->crtc) {
6811 crtc = encoder->crtc;
6813 mutex_lock(&crtc->mutex);
6815 old->dpms_mode = connector->dpms;
6816 old->load_detect_temp = false;
6818 /* Make sure the crtc and connector are running */
6819 if (connector->dpms != DRM_MODE_DPMS_ON)
6820 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
6825 /* Find an unused one (if possible) */
6826 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6828 if (!(encoder->possible_crtcs & (1 << i)))
6830 if (!possible_crtc->enabled) {
6831 crtc = possible_crtc;
6837 * If we didn't find an unused CRTC, don't use any.
6840 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6844 mutex_lock(&crtc->mutex);
6845 intel_encoder->new_crtc = to_intel_crtc(crtc);
6846 to_intel_connector(connector)->new_encoder = intel_encoder;
6848 intel_crtc = to_intel_crtc(crtc);
6849 old->dpms_mode = connector->dpms;
6850 old->load_detect_temp = true;
6851 old->release_fb = NULL;
6854 mode = &load_detect_mode;
6856 /* We need a framebuffer large enough to accommodate all accesses
6857 * that the plane may generate whilst we perform load detection.
6858 * We can not rely on the fbcon either being present (we get called
6859 * during its initialisation to detect all boot displays, or it may
6860 * not even exist) or that it is large enough to satisfy the
6863 fb = mode_fits_in_fbdev(dev, mode);
6865 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6866 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6867 old->release_fb = fb;
6869 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6871 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6872 mutex_unlock(&crtc->mutex);
6876 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6877 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
6878 if (old->release_fb)
6879 old->release_fb->funcs->destroy(old->release_fb);
6880 mutex_unlock(&crtc->mutex);
6884 /* let the connector get through one full cycle before testing */
6885 intel_wait_for_vblank(dev, intel_crtc->pipe);
6889 void intel_release_load_detect_pipe(struct drm_connector *connector,
6890 struct intel_load_detect_pipe *old)
6892 struct intel_encoder *intel_encoder =
6893 intel_attached_encoder(connector);
6894 struct drm_encoder *encoder = &intel_encoder->base;
6895 struct drm_crtc *crtc = encoder->crtc;
6897 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6898 connector->base.id, drm_get_connector_name(connector),
6899 encoder->base.id, drm_get_encoder_name(encoder));
6901 if (old->load_detect_temp) {
6902 to_intel_connector(connector)->new_encoder = NULL;
6903 intel_encoder->new_crtc = NULL;
6904 intel_set_mode(crtc, NULL, 0, 0, NULL);
6906 if (old->release_fb) {
6907 drm_framebuffer_unregister_private(old->release_fb);
6908 drm_framebuffer_unreference(old->release_fb);
6911 mutex_unlock(&crtc->mutex);
6915 /* Switch crtc and encoder back off if necessary */
6916 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6917 connector->funcs->dpms(connector, old->dpms_mode);
6919 mutex_unlock(&crtc->mutex);
6922 /* Returns the clock of the currently programmed mode of the given pipe. */
6923 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6925 struct drm_i915_private *dev_priv = dev->dev_private;
6926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6927 int pipe = intel_crtc->pipe;
6928 u32 dpll = I915_READ(DPLL(pipe));
6930 intel_clock_t clock;
6932 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6933 fp = I915_READ(FP0(pipe));
6935 fp = I915_READ(FP1(pipe));
6937 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6938 if (IS_PINEVIEW(dev)) {
6939 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6940 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6942 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6943 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6946 if (!IS_GEN2(dev)) {
6947 if (IS_PINEVIEW(dev))
6948 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6949 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6951 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6952 DPLL_FPA01_P1_POST_DIV_SHIFT);
6954 switch (dpll & DPLL_MODE_MASK) {
6955 case DPLLB_MODE_DAC_SERIAL:
6956 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6959 case DPLLB_MODE_LVDS:
6960 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6964 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
6965 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6969 if (IS_PINEVIEW(dev))
6970 pineview_clock(96000, &clock);
6972 i9xx_clock(96000, &clock);
6974 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6977 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6978 DPLL_FPA01_P1_POST_DIV_SHIFT);
6981 if ((dpll & PLL_REF_INPUT_MASK) ==
6982 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6983 /* XXX: might not be 66MHz */
6984 i9xx_clock(66000, &clock);
6986 i9xx_clock(48000, &clock);
6988 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6991 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6992 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6994 if (dpll & PLL_P2_DIVIDE_BY_4)
6999 i9xx_clock(48000, &clock);
7003 /* XXX: It would be nice to validate the clocks, but we can't reuse
7004 * i830PllIsValid() because it relies on the xf86_config connector
7005 * configuration being accurate, which it isn't necessarily.
7011 /** Returns the currently programmed mode of the given pipe. */
7012 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7013 struct drm_crtc *crtc)
7015 struct drm_i915_private *dev_priv = dev->dev_private;
7016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7017 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
7018 struct drm_display_mode *mode;
7019 int htot = I915_READ(HTOTAL(cpu_transcoder));
7020 int hsync = I915_READ(HSYNC(cpu_transcoder));
7021 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7022 int vsync = I915_READ(VSYNC(cpu_transcoder));
7024 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7028 mode->clock = intel_crtc_clock_get(dev, crtc);
7029 mode->hdisplay = (htot & 0xffff) + 1;
7030 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7031 mode->hsync_start = (hsync & 0xffff) + 1;
7032 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7033 mode->vdisplay = (vtot & 0xffff) + 1;
7034 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7035 mode->vsync_start = (vsync & 0xffff) + 1;
7036 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7038 drm_mode_set_name(mode);
7043 static void intel_increase_pllclock(struct drm_crtc *crtc)
7045 struct drm_device *dev = crtc->dev;
7046 drm_i915_private_t *dev_priv = dev->dev_private;
7047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7048 int pipe = intel_crtc->pipe;
7049 int dpll_reg = DPLL(pipe);
7052 if (HAS_PCH_SPLIT(dev))
7055 if (!dev_priv->lvds_downclock_avail)
7058 dpll = I915_READ(dpll_reg);
7059 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
7060 DRM_DEBUG_DRIVER("upclocking LVDS\n");
7062 assert_panel_unlocked(dev_priv, pipe);
7064 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7065 I915_WRITE(dpll_reg, dpll);
7066 intel_wait_for_vblank(dev, pipe);
7068 dpll = I915_READ(dpll_reg);
7069 if (dpll & DISPLAY_RATE_SELECT_FPA1)
7070 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
7074 static void intel_decrease_pllclock(struct drm_crtc *crtc)
7076 struct drm_device *dev = crtc->dev;
7077 drm_i915_private_t *dev_priv = dev->dev_private;
7078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7080 if (HAS_PCH_SPLIT(dev))
7083 if (!dev_priv->lvds_downclock_avail)
7087 * Since this is called by a timer, we should never get here in
7090 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7091 int pipe = intel_crtc->pipe;
7092 int dpll_reg = DPLL(pipe);
7095 DRM_DEBUG_DRIVER("downclocking LVDS\n");
7097 assert_panel_unlocked(dev_priv, pipe);
7099 dpll = I915_READ(dpll_reg);
7100 dpll |= DISPLAY_RATE_SELECT_FPA1;
7101 I915_WRITE(dpll_reg, dpll);
7102 intel_wait_for_vblank(dev, pipe);
7103 dpll = I915_READ(dpll_reg);
7104 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7105 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7110 void intel_mark_busy(struct drm_device *dev)
7112 i915_update_gfx_val(dev->dev_private);
7115 void intel_mark_idle(struct drm_device *dev)
7117 struct drm_crtc *crtc;
7119 if (!i915_powersave)
7122 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7126 intel_decrease_pllclock(crtc);
7130 void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7131 struct intel_ring_buffer *ring)
7133 struct drm_device *dev = obj->base.dev;
7134 struct drm_crtc *crtc;
7136 if (!i915_powersave)
7139 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7143 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7146 intel_increase_pllclock(crtc);
7147 if (ring && intel_fbc_enabled(dev))
7148 ring->fbc_dirty = true;
7152 static void intel_crtc_destroy(struct drm_crtc *crtc)
7154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7155 struct drm_device *dev = crtc->dev;
7156 struct intel_unpin_work *work;
7157 unsigned long flags;
7159 spin_lock_irqsave(&dev->event_lock, flags);
7160 work = intel_crtc->unpin_work;
7161 intel_crtc->unpin_work = NULL;
7162 spin_unlock_irqrestore(&dev->event_lock, flags);
7165 cancel_work_sync(&work->work);
7169 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7171 drm_crtc_cleanup(crtc);
7176 static void intel_unpin_work_fn(struct work_struct *__work)
7178 struct intel_unpin_work *work =
7179 container_of(__work, struct intel_unpin_work, work);
7180 struct drm_device *dev = work->crtc->dev;
7182 mutex_lock(&dev->struct_mutex);
7183 intel_unpin_fb_obj(work->old_fb_obj);
7184 drm_gem_object_unreference(&work->pending_flip_obj->base);
7185 drm_gem_object_unreference(&work->old_fb_obj->base);
7187 intel_update_fbc(dev);
7188 mutex_unlock(&dev->struct_mutex);
7190 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7191 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7196 static void do_intel_finish_page_flip(struct drm_device *dev,
7197 struct drm_crtc *crtc)
7199 drm_i915_private_t *dev_priv = dev->dev_private;
7200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7201 struct intel_unpin_work *work;
7202 unsigned long flags;
7204 /* Ignore early vblank irqs */
7205 if (intel_crtc == NULL)
7208 spin_lock_irqsave(&dev->event_lock, flags);
7209 work = intel_crtc->unpin_work;
7211 /* Ensure we don't miss a work->pending update ... */
7214 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7215 spin_unlock_irqrestore(&dev->event_lock, flags);
7219 /* and that the unpin work is consistent wrt ->pending. */
7222 intel_crtc->unpin_work = NULL;
7225 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7227 drm_vblank_put(dev, intel_crtc->pipe);
7229 spin_unlock_irqrestore(&dev->event_lock, flags);
7231 wake_up_all(&dev_priv->pending_flip_queue);
7233 queue_work(dev_priv->wq, &work->work);
7235 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7238 void intel_finish_page_flip(struct drm_device *dev, int pipe)
7240 drm_i915_private_t *dev_priv = dev->dev_private;
7241 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7243 do_intel_finish_page_flip(dev, crtc);
7246 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7248 drm_i915_private_t *dev_priv = dev->dev_private;
7249 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7251 do_intel_finish_page_flip(dev, crtc);
7254 void intel_prepare_page_flip(struct drm_device *dev, int plane)
7256 drm_i915_private_t *dev_priv = dev->dev_private;
7257 struct intel_crtc *intel_crtc =
7258 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7259 unsigned long flags;
7261 /* NB: An MMIO update of the plane base pointer will also
7262 * generate a page-flip completion irq, i.e. every modeset
7263 * is also accompanied by a spurious intel_prepare_page_flip().
7265 spin_lock_irqsave(&dev->event_lock, flags);
7266 if (intel_crtc->unpin_work)
7267 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7268 spin_unlock_irqrestore(&dev->event_lock, flags);
7271 inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7273 /* Ensure that the work item is consistent when activating it ... */
7275 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7276 /* and that it is marked active as soon as the irq could fire. */
7280 static int intel_gen2_queue_flip(struct drm_device *dev,
7281 struct drm_crtc *crtc,
7282 struct drm_framebuffer *fb,
7283 struct drm_i915_gem_object *obj)
7285 struct drm_i915_private *dev_priv = dev->dev_private;
7286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7288 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7291 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7295 ret = intel_ring_begin(ring, 6);
7299 /* Can't queue multiple flips, so wait for the previous
7300 * one to finish before executing the next.
7302 if (intel_crtc->plane)
7303 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7305 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7306 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7307 intel_ring_emit(ring, MI_NOOP);
7308 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7309 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7310 intel_ring_emit(ring, fb->pitches[0]);
7311 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7312 intel_ring_emit(ring, 0); /* aux display base address, unused */
7314 intel_mark_page_flip_active(intel_crtc);
7315 intel_ring_advance(ring);
7319 intel_unpin_fb_obj(obj);
7324 static int intel_gen3_queue_flip(struct drm_device *dev,
7325 struct drm_crtc *crtc,
7326 struct drm_framebuffer *fb,
7327 struct drm_i915_gem_object *obj)
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7332 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7335 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7339 ret = intel_ring_begin(ring, 6);
7343 if (intel_crtc->plane)
7344 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7346 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7347 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7348 intel_ring_emit(ring, MI_NOOP);
7349 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7350 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7351 intel_ring_emit(ring, fb->pitches[0]);
7352 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7353 intel_ring_emit(ring, MI_NOOP);
7355 intel_mark_page_flip_active(intel_crtc);
7356 intel_ring_advance(ring);
7360 intel_unpin_fb_obj(obj);
7365 static int intel_gen4_queue_flip(struct drm_device *dev,
7366 struct drm_crtc *crtc,
7367 struct drm_framebuffer *fb,
7368 struct drm_i915_gem_object *obj)
7370 struct drm_i915_private *dev_priv = dev->dev_private;
7371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7372 uint32_t pf, pipesrc;
7373 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7376 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7380 ret = intel_ring_begin(ring, 4);
7384 /* i965+ uses the linear or tiled offsets from the
7385 * Display Registers (which do not change across a page-flip)
7386 * so we need only reprogram the base address.
7388 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7389 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7390 intel_ring_emit(ring, fb->pitches[0]);
7391 intel_ring_emit(ring,
7392 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7395 /* XXX Enabling the panel-fitter across page-flip is so far
7396 * untested on non-native modes, so ignore it for now.
7397 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7400 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7401 intel_ring_emit(ring, pf | pipesrc);
7403 intel_mark_page_flip_active(intel_crtc);
7404 intel_ring_advance(ring);
7408 intel_unpin_fb_obj(obj);
7413 static int intel_gen6_queue_flip(struct drm_device *dev,
7414 struct drm_crtc *crtc,
7415 struct drm_framebuffer *fb,
7416 struct drm_i915_gem_object *obj)
7418 struct drm_i915_private *dev_priv = dev->dev_private;
7419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7420 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7421 uint32_t pf, pipesrc;
7424 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7428 ret = intel_ring_begin(ring, 4);
7432 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7433 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7434 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7435 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7437 /* Contrary to the suggestions in the documentation,
7438 * "Enable Panel Fitter" does not seem to be required when page
7439 * flipping with a non-native mode, and worse causes a normal
7441 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7444 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7445 intel_ring_emit(ring, pf | pipesrc);
7447 intel_mark_page_flip_active(intel_crtc);
7448 intel_ring_advance(ring);
7452 intel_unpin_fb_obj(obj);
7458 * On gen7 we currently use the blit ring because (in early silicon at least)
7459 * the render ring doesn't give us interrpts for page flip completion, which
7460 * means clients will hang after the first flip is queued. Fortunately the
7461 * blit ring generates interrupts properly, so use it instead.
7463 static int intel_gen7_queue_flip(struct drm_device *dev,
7464 struct drm_crtc *crtc,
7465 struct drm_framebuffer *fb,
7466 struct drm_i915_gem_object *obj)
7468 struct drm_i915_private *dev_priv = dev->dev_private;
7469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7470 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7471 uint32_t plane_bit = 0;
7474 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7478 switch(intel_crtc->plane) {
7480 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7483 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7486 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7489 WARN_ONCE(1, "unknown plane in flip command\n");
7494 ret = intel_ring_begin(ring, 4);
7498 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7499 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7500 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7501 intel_ring_emit(ring, (MI_NOOP));
7503 intel_mark_page_flip_active(intel_crtc);
7504 intel_ring_advance(ring);
7508 intel_unpin_fb_obj(obj);
7513 static int intel_default_queue_flip(struct drm_device *dev,
7514 struct drm_crtc *crtc,
7515 struct drm_framebuffer *fb,
7516 struct drm_i915_gem_object *obj)
7521 static int intel_crtc_page_flip(struct drm_crtc *crtc,
7522 struct drm_framebuffer *fb,
7523 struct drm_pending_vblank_event *event)
7525 struct drm_device *dev = crtc->dev;
7526 struct drm_i915_private *dev_priv = dev->dev_private;
7527 struct drm_framebuffer *old_fb = crtc->fb;
7528 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7530 struct intel_unpin_work *work;
7531 unsigned long flags;
7534 /* Can't change pixel format via MI display flips. */
7535 if (fb->pixel_format != crtc->fb->pixel_format)
7539 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7540 * Note that pitch changes could also affect these register.
7542 if (INTEL_INFO(dev)->gen > 3 &&
7543 (fb->offsets[0] != crtc->fb->offsets[0] ||
7544 fb->pitches[0] != crtc->fb->pitches[0]))
7547 work = kzalloc(sizeof *work, GFP_KERNEL);
7551 work->event = event;
7553 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
7554 INIT_WORK(&work->work, intel_unpin_work_fn);
7556 ret = drm_vblank_get(dev, intel_crtc->pipe);
7560 /* We borrow the event spin lock for protecting unpin_work */
7561 spin_lock_irqsave(&dev->event_lock, flags);
7562 if (intel_crtc->unpin_work) {
7563 spin_unlock_irqrestore(&dev->event_lock, flags);
7565 drm_vblank_put(dev, intel_crtc->pipe);
7567 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
7570 intel_crtc->unpin_work = work;
7571 spin_unlock_irqrestore(&dev->event_lock, flags);
7573 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7574 flush_workqueue(dev_priv->wq);
7576 ret = i915_mutex_lock_interruptible(dev);
7580 /* Reference the objects for the scheduled work. */
7581 drm_gem_object_reference(&work->old_fb_obj->base);
7582 drm_gem_object_reference(&obj->base);
7586 work->pending_flip_obj = obj;
7588 work->enable_stall_check = true;
7590 atomic_inc(&intel_crtc->unpin_work_count);
7591 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
7593 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7595 goto cleanup_pending;
7597 intel_disable_fbc(dev);
7598 intel_mark_fb_busy(obj, NULL);
7599 mutex_unlock(&dev->struct_mutex);
7601 trace_i915_flip_request(intel_crtc->plane, obj);
7606 atomic_dec(&intel_crtc->unpin_work_count);
7608 drm_gem_object_unreference(&work->old_fb_obj->base);
7609 drm_gem_object_unreference(&obj->base);
7610 mutex_unlock(&dev->struct_mutex);
7613 spin_lock_irqsave(&dev->event_lock, flags);
7614 intel_crtc->unpin_work = NULL;
7615 spin_unlock_irqrestore(&dev->event_lock, flags);
7617 drm_vblank_put(dev, intel_crtc->pipe);
7624 static struct drm_crtc_helper_funcs intel_helper_funcs = {
7625 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7626 .load_lut = intel_crtc_load_lut,
7629 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7630 struct drm_crtc *crtc)
7632 struct drm_device *dev;
7633 struct drm_crtc *tmp;
7636 WARN(!crtc, "checking null crtc?\n");
7640 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7646 if (encoder->possible_crtcs & crtc_mask)
7652 * intel_modeset_update_staged_output_state
7654 * Updates the staged output configuration state, e.g. after we've read out the
7657 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7659 struct intel_encoder *encoder;
7660 struct intel_connector *connector;
7662 list_for_each_entry(connector, &dev->mode_config.connector_list,
7664 connector->new_encoder =
7665 to_intel_encoder(connector->base.encoder);
7668 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7671 to_intel_crtc(encoder->base.crtc);
7676 * intel_modeset_commit_output_state
7678 * This function copies the stage display pipe configuration to the real one.
7680 static void intel_modeset_commit_output_state(struct drm_device *dev)
7682 struct intel_encoder *encoder;
7683 struct intel_connector *connector;
7685 list_for_each_entry(connector, &dev->mode_config.connector_list,
7687 connector->base.encoder = &connector->new_encoder->base;
7690 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7692 encoder->base.crtc = &encoder->new_crtc->base;
7697 connected_sink_compute_bpp(struct intel_connector * connector,
7698 struct intel_crtc_config *pipe_config)
7700 int bpp = pipe_config->pipe_bpp;
7702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7703 connector->base.base.id,
7704 drm_get_connector_name(&connector->base));
7706 /* Don't use an invalid EDID bpc value */
7707 if (connector->base.display_info.bpc &&
7708 connector->base.display_info.bpc * 3 < bpp) {
7709 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7710 bpp, connector->base.display_info.bpc*3);
7711 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7714 /* Clamp bpp to 8 on screens without EDID 1.4 */
7715 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7716 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7718 pipe_config->pipe_bpp = 24;
7723 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7724 struct drm_framebuffer *fb,
7725 struct intel_crtc_config *pipe_config)
7727 struct drm_device *dev = crtc->base.dev;
7728 struct intel_connector *connector;
7731 switch (fb->pixel_format) {
7733 bpp = 8*3; /* since we go through a colormap */
7735 case DRM_FORMAT_XRGB1555:
7736 case DRM_FORMAT_ARGB1555:
7737 /* checked in intel_framebuffer_init already */
7738 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7740 case DRM_FORMAT_RGB565:
7741 bpp = 6*3; /* min is 18bpp */
7743 case DRM_FORMAT_XBGR8888:
7744 case DRM_FORMAT_ABGR8888:
7745 /* checked in intel_framebuffer_init already */
7746 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7748 case DRM_FORMAT_XRGB8888:
7749 case DRM_FORMAT_ARGB8888:
7752 case DRM_FORMAT_XRGB2101010:
7753 case DRM_FORMAT_ARGB2101010:
7754 case DRM_FORMAT_XBGR2101010:
7755 case DRM_FORMAT_ABGR2101010:
7756 /* checked in intel_framebuffer_init already */
7757 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7761 /* TODO: gen4+ supports 16 bpc floating point, too. */
7763 DRM_DEBUG_KMS("unsupported depth\n");
7767 pipe_config->pipe_bpp = bpp;
7769 /* Clamp display bpp to EDID value */
7770 list_for_each_entry(connector, &dev->mode_config.connector_list,
7772 if (!connector->new_encoder ||
7773 connector->new_encoder->new_crtc != crtc)
7776 connected_sink_compute_bpp(connector, pipe_config);
7782 static void intel_dump_pipe_config(struct intel_crtc *crtc,
7783 struct intel_crtc_config *pipe_config,
7784 const char *context)
7786 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7787 context, pipe_name(crtc->pipe));
7789 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7790 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7791 pipe_config->pipe_bpp, pipe_config->dither);
7792 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7793 pipe_config->has_pch_encoder,
7794 pipe_config->fdi_lanes,
7795 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7796 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7797 pipe_config->fdi_m_n.tu);
7798 DRM_DEBUG_KMS("requested mode:\n");
7799 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7800 DRM_DEBUG_KMS("adjusted mode:\n");
7801 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7802 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7803 pipe_config->gmch_pfit.control,
7804 pipe_config->gmch_pfit.pgm_ratios,
7805 pipe_config->gmch_pfit.lvds_border_bits);
7806 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7807 pipe_config->pch_pfit.pos,
7808 pipe_config->pch_pfit.size);
7809 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
7812 static bool check_encoder_cloning(struct drm_crtc *crtc)
7814 int num_encoders = 0;
7815 bool uncloneable_encoders = false;
7816 struct intel_encoder *encoder;
7818 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7820 if (&encoder->new_crtc->base != crtc)
7824 if (!encoder->cloneable)
7825 uncloneable_encoders = true;
7828 return !(num_encoders > 1 && uncloneable_encoders);
7831 static struct intel_crtc_config *
7832 intel_modeset_pipe_config(struct drm_crtc *crtc,
7833 struct drm_framebuffer *fb,
7834 struct drm_display_mode *mode)
7836 struct drm_device *dev = crtc->dev;
7837 struct drm_encoder_helper_funcs *encoder_funcs;
7838 struct intel_encoder *encoder;
7839 struct intel_crtc_config *pipe_config;
7840 int plane_bpp, ret = -EINVAL;
7843 if (!check_encoder_cloning(crtc)) {
7844 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7845 return ERR_PTR(-EINVAL);
7848 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7850 return ERR_PTR(-ENOMEM);
7852 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7853 drm_mode_copy(&pipe_config->requested_mode, mode);
7854 pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
7856 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7857 * plane pixel format and any sink constraints into account. Returns the
7858 * source plane bpp so that dithering can be selected on mismatches
7859 * after encoders and crtc also have had their say. */
7860 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7866 /* Ensure the port clock defaults are reset when retrying. */
7867 pipe_config->port_clock = 0;
7868 pipe_config->pixel_multiplier = 1;
7870 /* Pass our mode to the connectors and the CRTC to give them a chance to
7871 * adjust it according to limitations or connector properties, and also
7872 * a chance to reject the mode entirely.
7874 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7877 if (&encoder->new_crtc->base != crtc)
7880 if (encoder->compute_config) {
7881 if (!(encoder->compute_config(encoder, pipe_config))) {
7882 DRM_DEBUG_KMS("Encoder config failure\n");
7889 encoder_funcs = encoder->base.helper_private;
7890 if (!(encoder_funcs->mode_fixup(&encoder->base,
7891 &pipe_config->requested_mode,
7892 &pipe_config->adjusted_mode))) {
7893 DRM_DEBUG_KMS("Encoder fixup failed\n");
7898 /* Set default port clock if not overwritten by the encoder. Needs to be
7899 * done afterwards in case the encoder adjusts the mode. */
7900 if (!pipe_config->port_clock)
7901 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7903 ret = intel_crtc_compute_config(crtc, pipe_config);
7905 DRM_DEBUG_KMS("CRTC fixup failed\n");
7910 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7915 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7920 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7921 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7922 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7927 return ERR_PTR(ret);
7930 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
7931 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7933 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7934 unsigned *prepare_pipes, unsigned *disable_pipes)
7936 struct intel_crtc *intel_crtc;
7937 struct drm_device *dev = crtc->dev;
7938 struct intel_encoder *encoder;
7939 struct intel_connector *connector;
7940 struct drm_crtc *tmp_crtc;
7942 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7944 /* Check which crtcs have changed outputs connected to them, these need
7945 * to be part of the prepare_pipes mask. We don't (yet) support global
7946 * modeset across multiple crtcs, so modeset_pipes will only have one
7947 * bit set at most. */
7948 list_for_each_entry(connector, &dev->mode_config.connector_list,
7950 if (connector->base.encoder == &connector->new_encoder->base)
7953 if (connector->base.encoder) {
7954 tmp_crtc = connector->base.encoder->crtc;
7956 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7959 if (connector->new_encoder)
7961 1 << connector->new_encoder->new_crtc->pipe;
7964 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7966 if (encoder->base.crtc == &encoder->new_crtc->base)
7969 if (encoder->base.crtc) {
7970 tmp_crtc = encoder->base.crtc;
7972 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7975 if (encoder->new_crtc)
7976 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7979 /* Check for any pipes that will be fully disabled ... */
7980 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7984 /* Don't try to disable disabled crtcs. */
7985 if (!intel_crtc->base.enabled)
7988 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7990 if (encoder->new_crtc == intel_crtc)
7995 *disable_pipes |= 1 << intel_crtc->pipe;
7999 /* set_mode is also used to update properties on life display pipes. */
8000 intel_crtc = to_intel_crtc(crtc);
8002 *prepare_pipes |= 1 << intel_crtc->pipe;
8005 * For simplicity do a full modeset on any pipe where the output routing
8006 * changed. We could be more clever, but that would require us to be
8007 * more careful with calling the relevant encoder->mode_set functions.
8010 *modeset_pipes = *prepare_pipes;
8012 /* ... and mask these out. */
8013 *modeset_pipes &= ~(*disable_pipes);
8014 *prepare_pipes &= ~(*disable_pipes);
8017 * HACK: We don't (yet) fully support global modesets. intel_set_config
8018 * obies this rule, but the modeset restore mode of
8019 * intel_modeset_setup_hw_state does not.
8021 *modeset_pipes &= 1 << intel_crtc->pipe;
8022 *prepare_pipes &= 1 << intel_crtc->pipe;
8024 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8025 *modeset_pipes, *prepare_pipes, *disable_pipes);
8028 static bool intel_crtc_in_use(struct drm_crtc *crtc)
8030 struct drm_encoder *encoder;
8031 struct drm_device *dev = crtc->dev;
8033 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8034 if (encoder->crtc == crtc)
8041 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8043 struct intel_encoder *intel_encoder;
8044 struct intel_crtc *intel_crtc;
8045 struct drm_connector *connector;
8047 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8049 if (!intel_encoder->base.crtc)
8052 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8054 if (prepare_pipes & (1 << intel_crtc->pipe))
8055 intel_encoder->connectors_active = false;
8058 intel_modeset_commit_output_state(dev);
8060 /* Update computed state. */
8061 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8063 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8066 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8067 if (!connector->encoder || !connector->encoder->crtc)
8070 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8072 if (prepare_pipes & (1 << intel_crtc->pipe)) {
8073 struct drm_property *dpms_property =
8074 dev->mode_config.dpms_property;
8076 connector->dpms = DRM_MODE_DPMS_ON;
8077 drm_object_property_set_value(&connector->base,
8081 intel_encoder = to_intel_encoder(connector->encoder);
8082 intel_encoder->connectors_active = true;
8088 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8089 list_for_each_entry((intel_crtc), \
8090 &(dev)->mode_config.crtc_list, \
8092 if (mask & (1 <<(intel_crtc)->pipe))
8095 intel_pipe_config_compare(struct drm_device *dev,
8096 struct intel_crtc_config *current_config,
8097 struct intel_crtc_config *pipe_config)
8099 #define PIPE_CONF_CHECK_I(name) \
8100 if (current_config->name != pipe_config->name) { \
8101 DRM_ERROR("mismatch in " #name " " \
8102 "(expected %i, found %i)\n", \
8103 current_config->name, \
8104 pipe_config->name); \
8108 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
8109 if ((current_config->name ^ pipe_config->name) & (mask)) { \
8110 DRM_ERROR("mismatch in " #name " " \
8111 "(expected %i, found %i)\n", \
8112 current_config->name & (mask), \
8113 pipe_config->name & (mask)); \
8117 #define PIPE_CONF_QUIRK(quirk) \
8118 ((current_config->quirks | pipe_config->quirks) & (quirk))
8120 PIPE_CONF_CHECK_I(cpu_transcoder);
8122 PIPE_CONF_CHECK_I(has_pch_encoder);
8123 PIPE_CONF_CHECK_I(fdi_lanes);
8124 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8125 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8126 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8127 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8128 PIPE_CONF_CHECK_I(fdi_m_n.tu);
8130 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8131 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8132 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8133 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8134 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8135 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8137 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8138 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8139 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8140 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8141 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8142 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8144 if (!HAS_PCH_SPLIT(dev))
8145 PIPE_CONF_CHECK_I(pixel_multiplier);
8147 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8148 DRM_MODE_FLAG_INTERLACE);
8150 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8151 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8152 DRM_MODE_FLAG_PHSYNC);
8153 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8154 DRM_MODE_FLAG_NHSYNC);
8155 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8156 DRM_MODE_FLAG_PVSYNC);
8157 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8158 DRM_MODE_FLAG_NVSYNC);
8161 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8162 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8164 PIPE_CONF_CHECK_I(gmch_pfit.control);
8165 /* pfit ratios are autocomputed by the hw on gen4+ */
8166 if (INTEL_INFO(dev)->gen < 4)
8167 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8168 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8169 PIPE_CONF_CHECK_I(pch_pfit.pos);
8170 PIPE_CONF_CHECK_I(pch_pfit.size);
8172 PIPE_CONF_CHECK_I(ips_enabled);
8174 #undef PIPE_CONF_CHECK_I
8175 #undef PIPE_CONF_CHECK_FLAGS
8176 #undef PIPE_CONF_QUIRK
8182 intel_modeset_check_state(struct drm_device *dev)
8184 drm_i915_private_t *dev_priv = dev->dev_private;
8185 struct intel_crtc *crtc;
8186 struct intel_encoder *encoder;
8187 struct intel_connector *connector;
8188 struct intel_crtc_config pipe_config;
8190 list_for_each_entry(connector, &dev->mode_config.connector_list,
8192 /* This also checks the encoder/connector hw state with the
8193 * ->get_hw_state callbacks. */
8194 intel_connector_check_state(connector);
8196 WARN(&connector->new_encoder->base != connector->base.encoder,
8197 "connector's staged encoder doesn't match current encoder\n");
8200 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8202 bool enabled = false;
8203 bool active = false;
8204 enum pipe pipe, tracked_pipe;
8206 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8207 encoder->base.base.id,
8208 drm_get_encoder_name(&encoder->base));
8210 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8211 "encoder's stage crtc doesn't match current crtc\n");
8212 WARN(encoder->connectors_active && !encoder->base.crtc,
8213 "encoder's active_connectors set, but no crtc\n");
8215 list_for_each_entry(connector, &dev->mode_config.connector_list,
8217 if (connector->base.encoder != &encoder->base)
8220 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8223 WARN(!!encoder->base.crtc != enabled,
8224 "encoder's enabled state mismatch "
8225 "(expected %i, found %i)\n",
8226 !!encoder->base.crtc, enabled);
8227 WARN(active && !encoder->base.crtc,
8228 "active encoder with no crtc\n");
8230 WARN(encoder->connectors_active != active,
8231 "encoder's computed active state doesn't match tracked active state "
8232 "(expected %i, found %i)\n", active, encoder->connectors_active);
8234 active = encoder->get_hw_state(encoder, &pipe);
8235 WARN(active != encoder->connectors_active,
8236 "encoder's hw state doesn't match sw tracking "
8237 "(expected %i, found %i)\n",
8238 encoder->connectors_active, active);
8240 if (!encoder->base.crtc)
8243 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8244 WARN(active && pipe != tracked_pipe,
8245 "active encoder's pipe doesn't match"
8246 "(expected %i, found %i)\n",
8247 tracked_pipe, pipe);
8251 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8253 bool enabled = false;
8254 bool active = false;
8256 memset(&pipe_config, 0, sizeof(pipe_config));
8258 DRM_DEBUG_KMS("[CRTC:%d]\n",
8259 crtc->base.base.id);
8261 WARN(crtc->active && !crtc->base.enabled,
8262 "active crtc, but not enabled in sw tracking\n");
8264 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8266 if (encoder->base.crtc != &crtc->base)
8269 if (encoder->connectors_active)
8273 WARN(active != crtc->active,
8274 "crtc's computed active state doesn't match tracked active state "
8275 "(expected %i, found %i)\n", active, crtc->active);
8276 WARN(enabled != crtc->base.enabled,
8277 "crtc's computed enabled state doesn't match tracked enabled state "
8278 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8280 active = dev_priv->display.get_pipe_config(crtc,
8282 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8284 if (encoder->base.crtc != &crtc->base)
8286 if (encoder->get_config)
8287 encoder->get_config(encoder, &pipe_config);
8290 WARN(crtc->active != active,
8291 "crtc active state doesn't match with hw state "
8292 "(expected %i, found %i)\n", crtc->active, active);
8295 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8296 WARN(1, "pipe state doesn't match!\n");
8297 intel_dump_pipe_config(crtc, &pipe_config,
8299 intel_dump_pipe_config(crtc, &crtc->config,
8305 static int __intel_set_mode(struct drm_crtc *crtc,
8306 struct drm_display_mode *mode,
8307 int x, int y, struct drm_framebuffer *fb)
8309 struct drm_device *dev = crtc->dev;
8310 drm_i915_private_t *dev_priv = dev->dev_private;
8311 struct drm_display_mode *saved_mode, *saved_hwmode;
8312 struct intel_crtc_config *pipe_config = NULL;
8313 struct intel_crtc *intel_crtc;
8314 unsigned disable_pipes, prepare_pipes, modeset_pipes;
8317 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
8320 saved_hwmode = saved_mode + 1;
8322 intel_modeset_affected_pipes(crtc, &modeset_pipes,
8323 &prepare_pipes, &disable_pipes);
8325 *saved_hwmode = crtc->hwmode;
8326 *saved_mode = crtc->mode;
8328 /* Hack: Because we don't (yet) support global modeset on multiple
8329 * crtcs, we don't keep track of the new mode for more than one crtc.
8330 * Hence simply check whether any bit is set in modeset_pipes in all the
8331 * pieces of code that are not yet converted to deal with mutliple crtcs
8332 * changing their mode at the same time. */
8333 if (modeset_pipes) {
8334 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8335 if (IS_ERR(pipe_config)) {
8336 ret = PTR_ERR(pipe_config);
8341 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8345 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8346 intel_crtc_disable(&intel_crtc->base);
8348 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8349 if (intel_crtc->base.enabled)
8350 dev_priv->display.crtc_disable(&intel_crtc->base);
8353 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8354 * to set it here already despite that we pass it down the callchain.
8356 if (modeset_pipes) {
8358 /* mode_set/enable/disable functions rely on a correct pipe
8360 to_intel_crtc(crtc)->config = *pipe_config;
8363 /* Only after disabling all output pipelines that will be changed can we
8364 * update the the output configuration. */
8365 intel_modeset_update_state(dev, prepare_pipes);
8367 if (dev_priv->display.modeset_global_resources)
8368 dev_priv->display.modeset_global_resources(dev);
8370 /* Set up the DPLL and any encoders state that needs to adjust or depend
8373 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8374 ret = intel_crtc_mode_set(&intel_crtc->base,
8380 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
8381 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8382 dev_priv->display.crtc_enable(&intel_crtc->base);
8384 if (modeset_pipes) {
8385 /* Store real post-adjustment hardware mode. */
8386 crtc->hwmode = pipe_config->adjusted_mode;
8388 /* Calculate and store various constants which
8389 * are later needed by vblank and swap-completion
8390 * timestamping. They are derived from true hwmode.
8392 drm_calc_timestamping_constants(crtc);
8395 /* FIXME: add subpixel order */
8397 if (ret && crtc->enabled) {
8398 crtc->hwmode = *saved_hwmode;
8399 crtc->mode = *saved_mode;
8408 int intel_set_mode(struct drm_crtc *crtc,
8409 struct drm_display_mode *mode,
8410 int x, int y, struct drm_framebuffer *fb)
8414 ret = __intel_set_mode(crtc, mode, x, y, fb);
8417 intel_modeset_check_state(crtc->dev);
8422 void intel_crtc_restore_mode(struct drm_crtc *crtc)
8424 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8427 #undef for_each_intel_crtc_masked
8429 static void intel_set_config_free(struct intel_set_config *config)
8434 kfree(config->save_connector_encoders);
8435 kfree(config->save_encoder_crtcs);
8439 static int intel_set_config_save_state(struct drm_device *dev,
8440 struct intel_set_config *config)
8442 struct drm_encoder *encoder;
8443 struct drm_connector *connector;
8446 config->save_encoder_crtcs =
8447 kcalloc(dev->mode_config.num_encoder,
8448 sizeof(struct drm_crtc *), GFP_KERNEL);
8449 if (!config->save_encoder_crtcs)
8452 config->save_connector_encoders =
8453 kcalloc(dev->mode_config.num_connector,
8454 sizeof(struct drm_encoder *), GFP_KERNEL);
8455 if (!config->save_connector_encoders)
8458 /* Copy data. Note that driver private data is not affected.
8459 * Should anything bad happen only the expected state is
8460 * restored, not the drivers personal bookkeeping.
8463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
8464 config->save_encoder_crtcs[count++] = encoder->crtc;
8468 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8469 config->save_connector_encoders[count++] = connector->encoder;
8475 static void intel_set_config_restore_state(struct drm_device *dev,
8476 struct intel_set_config *config)
8478 struct intel_encoder *encoder;
8479 struct intel_connector *connector;
8483 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8485 to_intel_crtc(config->save_encoder_crtcs[count++]);
8489 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8490 connector->new_encoder =
8491 to_intel_encoder(config->save_connector_encoders[count++]);
8496 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8497 struct intel_set_config *config)
8500 /* We should be able to check here if the fb has the same properties
8501 * and then just flip_or_move it */
8502 if (set->crtc->fb != set->fb) {
8503 /* If we have no fb then treat it as a full mode set */
8504 if (set->crtc->fb == NULL) {
8505 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8506 config->mode_changed = true;
8507 } else if (set->fb == NULL) {
8508 config->mode_changed = true;
8509 } else if (set->fb->pixel_format !=
8510 set->crtc->fb->pixel_format) {
8511 config->mode_changed = true;
8513 config->fb_changed = true;
8516 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
8517 config->fb_changed = true;
8519 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8520 DRM_DEBUG_KMS("modes are different, full mode set\n");
8521 drm_mode_debug_printmodeline(&set->crtc->mode);
8522 drm_mode_debug_printmodeline(set->mode);
8523 config->mode_changed = true;
8528 intel_modeset_stage_output_state(struct drm_device *dev,
8529 struct drm_mode_set *set,
8530 struct intel_set_config *config)
8532 struct drm_crtc *new_crtc;
8533 struct intel_connector *connector;
8534 struct intel_encoder *encoder;
8537 /* The upper layers ensure that we either disable a crtc or have a list
8538 * of connectors. For paranoia, double-check this. */
8539 WARN_ON(!set->fb && (set->num_connectors != 0));
8540 WARN_ON(set->fb && (set->num_connectors == 0));
8543 list_for_each_entry(connector, &dev->mode_config.connector_list,
8545 /* Otherwise traverse passed in connector list and get encoders
8547 for (ro = 0; ro < set->num_connectors; ro++) {
8548 if (set->connectors[ro] == &connector->base) {
8549 connector->new_encoder = connector->encoder;
8554 /* If we disable the crtc, disable all its connectors. Also, if
8555 * the connector is on the changing crtc but not on the new
8556 * connector list, disable it. */
8557 if ((!set->fb || ro == set->num_connectors) &&
8558 connector->base.encoder &&
8559 connector->base.encoder->crtc == set->crtc) {
8560 connector->new_encoder = NULL;
8562 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8563 connector->base.base.id,
8564 drm_get_connector_name(&connector->base));
8568 if (&connector->new_encoder->base != connector->base.encoder) {
8569 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
8570 config->mode_changed = true;
8573 /* connector->new_encoder is now updated for all connectors. */
8575 /* Update crtc of enabled connectors. */
8577 list_for_each_entry(connector, &dev->mode_config.connector_list,
8579 if (!connector->new_encoder)
8582 new_crtc = connector->new_encoder->base.crtc;
8584 for (ro = 0; ro < set->num_connectors; ro++) {
8585 if (set->connectors[ro] == &connector->base)
8586 new_crtc = set->crtc;
8589 /* Make sure the new CRTC will work with the encoder */
8590 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8594 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8596 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8597 connector->base.base.id,
8598 drm_get_connector_name(&connector->base),
8602 /* Check for any encoders that needs to be disabled. */
8603 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8605 list_for_each_entry(connector,
8606 &dev->mode_config.connector_list,
8608 if (connector->new_encoder == encoder) {
8609 WARN_ON(!connector->new_encoder->new_crtc);
8614 encoder->new_crtc = NULL;
8616 /* Only now check for crtc changes so we don't miss encoders
8617 * that will be disabled. */
8618 if (&encoder->new_crtc->base != encoder->base.crtc) {
8619 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
8620 config->mode_changed = true;
8623 /* Now we've also updated encoder->new_crtc for all encoders. */
8628 static int intel_crtc_set_config(struct drm_mode_set *set)
8630 struct drm_device *dev;
8631 struct drm_mode_set save_set;
8632 struct intel_set_config *config;
8637 BUG_ON(!set->crtc->helper_private);
8639 /* Enforce sane interface api - has been abused by the fb helper. */
8640 BUG_ON(!set->mode && set->fb);
8641 BUG_ON(set->fb && set->num_connectors == 0);
8644 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8645 set->crtc->base.id, set->fb->base.id,
8646 (int)set->num_connectors, set->x, set->y);
8648 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
8651 dev = set->crtc->dev;
8654 config = kzalloc(sizeof(*config), GFP_KERNEL);
8658 ret = intel_set_config_save_state(dev, config);
8662 save_set.crtc = set->crtc;
8663 save_set.mode = &set->crtc->mode;
8664 save_set.x = set->crtc->x;
8665 save_set.y = set->crtc->y;
8666 save_set.fb = set->crtc->fb;
8668 /* Compute whether we need a full modeset, only an fb base update or no
8669 * change at all. In the future we might also check whether only the
8670 * mode changed, e.g. for LVDS where we only change the panel fitter in
8672 intel_set_config_compute_mode_changes(set, config);
8674 ret = intel_modeset_stage_output_state(dev, set, config);
8678 if (config->mode_changed) {
8679 ret = intel_set_mode(set->crtc, set->mode,
8680 set->x, set->y, set->fb);
8682 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8683 set->crtc->base.id, ret);
8686 } else if (config->fb_changed) {
8687 intel_crtc_wait_for_pending_flips(set->crtc);
8689 ret = intel_pipe_set_base(set->crtc,
8690 set->x, set->y, set->fb);
8693 intel_set_config_free(config);
8698 intel_set_config_restore_state(dev, config);
8700 /* Try to restore the config */
8701 if (config->mode_changed &&
8702 intel_set_mode(save_set.crtc, save_set.mode,
8703 save_set.x, save_set.y, save_set.fb))
8704 DRM_ERROR("failed to restore config after modeset failure\n");
8707 intel_set_config_free(config);
8711 static const struct drm_crtc_funcs intel_crtc_funcs = {
8712 .cursor_set = intel_crtc_cursor_set,
8713 .cursor_move = intel_crtc_cursor_move,
8714 .gamma_set = intel_crtc_gamma_set,
8715 .set_config = intel_crtc_set_config,
8716 .destroy = intel_crtc_destroy,
8717 .page_flip = intel_crtc_page_flip,
8720 static void intel_cpu_pll_init(struct drm_device *dev)
8723 intel_ddi_pll_init(dev);
8726 static void intel_pch_pll_init(struct drm_device *dev)
8728 drm_i915_private_t *dev_priv = dev->dev_private;
8731 if (dev_priv->num_pch_pll == 0) {
8732 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8736 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8737 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8738 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8739 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8743 static void intel_crtc_init(struct drm_device *dev, int pipe)
8745 drm_i915_private_t *dev_priv = dev->dev_private;
8746 struct intel_crtc *intel_crtc;
8749 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8750 if (intel_crtc == NULL)
8753 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8755 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
8756 for (i = 0; i < 256; i++) {
8757 intel_crtc->lut_r[i] = i;
8758 intel_crtc->lut_g[i] = i;
8759 intel_crtc->lut_b[i] = i;
8762 /* Swap pipes & planes for FBC on pre-965 */
8763 intel_crtc->pipe = pipe;
8764 intel_crtc->plane = pipe;
8765 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
8766 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
8767 intel_crtc->plane = !pipe;
8770 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8771 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8772 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8773 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8775 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8778 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
8779 struct drm_file *file)
8781 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
8782 struct drm_mode_object *drmmode_obj;
8783 struct intel_crtc *crtc;
8785 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8788 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8789 DRM_MODE_OBJECT_CRTC);
8792 DRM_ERROR("no such CRTC id\n");
8796 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8797 pipe_from_crtc_id->pipe = crtc->pipe;
8802 static int intel_encoder_clones(struct intel_encoder *encoder)
8804 struct drm_device *dev = encoder->base.dev;
8805 struct intel_encoder *source_encoder;
8809 list_for_each_entry(source_encoder,
8810 &dev->mode_config.encoder_list, base.head) {
8812 if (encoder == source_encoder)
8813 index_mask |= (1 << entry);
8815 /* Intel hw has only one MUX where enocoders could be cloned. */
8816 if (encoder->cloneable && source_encoder->cloneable)
8817 index_mask |= (1 << entry);
8825 static bool has_edp_a(struct drm_device *dev)
8827 struct drm_i915_private *dev_priv = dev->dev_private;
8829 if (!IS_MOBILE(dev))
8832 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8836 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8842 static void intel_setup_outputs(struct drm_device *dev)
8844 struct drm_i915_private *dev_priv = dev->dev_private;
8845 struct intel_encoder *encoder;
8846 bool dpd_is_edp = false;
8849 has_lvds = intel_lvds_init(dev);
8850 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8851 /* disable the panel fitter on everything but LVDS */
8852 I915_WRITE(PFIT_CONTROL, 0);
8856 intel_crt_init(dev);
8861 /* Haswell uses DDI functions to detect digital outputs */
8862 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8863 /* DDI A only supports eDP */
8865 intel_ddi_init(dev, PORT_A);
8867 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8869 found = I915_READ(SFUSE_STRAP);
8871 if (found & SFUSE_STRAP_DDIB_DETECTED)
8872 intel_ddi_init(dev, PORT_B);
8873 if (found & SFUSE_STRAP_DDIC_DETECTED)
8874 intel_ddi_init(dev, PORT_C);
8875 if (found & SFUSE_STRAP_DDID_DETECTED)
8876 intel_ddi_init(dev, PORT_D);
8877 } else if (HAS_PCH_SPLIT(dev)) {
8879 dpd_is_edp = intel_dpd_is_edp(dev);
8882 intel_dp_init(dev, DP_A, PORT_A);
8884 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
8885 /* PCH SDVOB multiplex with HDMIB */
8886 found = intel_sdvo_init(dev, PCH_SDVOB, true);
8888 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
8889 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8890 intel_dp_init(dev, PCH_DP_B, PORT_B);
8893 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
8894 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
8896 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
8897 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
8899 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8900 intel_dp_init(dev, PCH_DP_C, PORT_C);
8902 if (I915_READ(PCH_DP_D) & DP_DETECTED)
8903 intel_dp_init(dev, PCH_DP_D, PORT_D);
8904 } else if (IS_VALLEYVIEW(dev)) {
8905 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8906 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8907 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
8909 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
8910 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8912 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8913 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
8915 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
8918 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8919 DRM_DEBUG_KMS("probing SDVOB\n");
8920 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
8921 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8922 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
8923 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
8926 if (!found && SUPPORTS_INTEGRATED_DP(dev))
8927 intel_dp_init(dev, DP_B, PORT_B);
8930 /* Before G4X SDVOC doesn't have its own detect register */
8932 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8933 DRM_DEBUG_KMS("probing SDVOC\n");
8934 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
8937 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
8939 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8940 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
8941 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
8943 if (SUPPORTS_INTEGRATED_DP(dev))
8944 intel_dp_init(dev, DP_C, PORT_C);
8947 if (SUPPORTS_INTEGRATED_DP(dev) &&
8948 (I915_READ(DP_D) & DP_DETECTED))
8949 intel_dp_init(dev, DP_D, PORT_D);
8950 } else if (IS_GEN2(dev))
8951 intel_dvo_init(dev);
8953 if (SUPPORTS_TV(dev))
8956 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8957 encoder->base.possible_crtcs = encoder->crtc_mask;
8958 encoder->base.possible_clones =
8959 intel_encoder_clones(encoder);
8962 intel_init_pch_refclk(dev);
8964 drm_helper_move_panel_connectors_to_head(dev);
8967 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8969 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8971 drm_framebuffer_cleanup(fb);
8972 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
8977 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
8978 struct drm_file *file,
8979 unsigned int *handle)
8981 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8982 struct drm_i915_gem_object *obj = intel_fb->obj;
8984 return drm_gem_handle_create(file, &obj->base, handle);
8987 static const struct drm_framebuffer_funcs intel_fb_funcs = {
8988 .destroy = intel_user_framebuffer_destroy,
8989 .create_handle = intel_user_framebuffer_create_handle,
8992 int intel_framebuffer_init(struct drm_device *dev,
8993 struct intel_framebuffer *intel_fb,
8994 struct drm_mode_fb_cmd2 *mode_cmd,
8995 struct drm_i915_gem_object *obj)
8999 if (obj->tiling_mode == I915_TILING_Y) {
9000 DRM_DEBUG("hardware does not support tiling Y\n");
9004 if (mode_cmd->pitches[0] & 63) {
9005 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9006 mode_cmd->pitches[0]);
9010 /* FIXME <= Gen4 stride limits are bit unclear */
9011 if (mode_cmd->pitches[0] > 32768) {
9012 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
9013 mode_cmd->pitches[0]);
9017 if (obj->tiling_mode != I915_TILING_NONE &&
9018 mode_cmd->pitches[0] != obj->stride) {
9019 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9020 mode_cmd->pitches[0], obj->stride);
9024 /* Reject formats not supported by any plane early. */
9025 switch (mode_cmd->pixel_format) {
9027 case DRM_FORMAT_RGB565:
9028 case DRM_FORMAT_XRGB8888:
9029 case DRM_FORMAT_ARGB8888:
9031 case DRM_FORMAT_XRGB1555:
9032 case DRM_FORMAT_ARGB1555:
9033 if (INTEL_INFO(dev)->gen > 3) {
9034 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9038 case DRM_FORMAT_XBGR8888:
9039 case DRM_FORMAT_ABGR8888:
9040 case DRM_FORMAT_XRGB2101010:
9041 case DRM_FORMAT_ARGB2101010:
9042 case DRM_FORMAT_XBGR2101010:
9043 case DRM_FORMAT_ABGR2101010:
9044 if (INTEL_INFO(dev)->gen < 4) {
9045 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9049 case DRM_FORMAT_YUYV:
9050 case DRM_FORMAT_UYVY:
9051 case DRM_FORMAT_YVYU:
9052 case DRM_FORMAT_VYUY:
9053 if (INTEL_INFO(dev)->gen < 5) {
9054 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9059 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
9063 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9064 if (mode_cmd->offsets[0] != 0)
9067 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9068 intel_fb->obj = obj;
9070 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9072 DRM_ERROR("framebuffer init failed %d\n", ret);
9079 static struct drm_framebuffer *
9080 intel_user_framebuffer_create(struct drm_device *dev,
9081 struct drm_file *filp,
9082 struct drm_mode_fb_cmd2 *mode_cmd)
9084 struct drm_i915_gem_object *obj;
9086 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9087 mode_cmd->handles[0]));
9088 if (&obj->base == NULL)
9089 return ERR_PTR(-ENOENT);
9091 return intel_framebuffer_create(dev, mode_cmd, obj);
9094 static const struct drm_mode_config_funcs intel_mode_funcs = {
9095 .fb_create = intel_user_framebuffer_create,
9096 .output_poll_changed = intel_fb_output_poll_changed,
9099 /* Set up chip specific display functions */
9100 static void intel_init_display(struct drm_device *dev)
9102 struct drm_i915_private *dev_priv = dev->dev_private;
9104 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9105 dev_priv->display.find_dpll = g4x_find_best_dpll;
9106 else if (IS_VALLEYVIEW(dev))
9107 dev_priv->display.find_dpll = vlv_find_best_dpll;
9108 else if (IS_PINEVIEW(dev))
9109 dev_priv->display.find_dpll = pnv_find_best_dpll;
9111 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9114 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
9115 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
9116 dev_priv->display.crtc_enable = haswell_crtc_enable;
9117 dev_priv->display.crtc_disable = haswell_crtc_disable;
9118 dev_priv->display.off = haswell_crtc_off;
9119 dev_priv->display.update_plane = ironlake_update_plane;
9120 } else if (HAS_PCH_SPLIT(dev)) {
9121 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
9122 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
9123 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9124 dev_priv->display.crtc_disable = ironlake_crtc_disable;
9125 dev_priv->display.off = ironlake_crtc_off;
9126 dev_priv->display.update_plane = ironlake_update_plane;
9127 } else if (IS_VALLEYVIEW(dev)) {
9128 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9129 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9130 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9131 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9132 dev_priv->display.off = i9xx_crtc_off;
9133 dev_priv->display.update_plane = i9xx_update_plane;
9135 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9136 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9137 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9138 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9139 dev_priv->display.off = i9xx_crtc_off;
9140 dev_priv->display.update_plane = i9xx_update_plane;
9143 /* Returns the core display clock speed */
9144 if (IS_VALLEYVIEW(dev))
9145 dev_priv->display.get_display_clock_speed =
9146 valleyview_get_display_clock_speed;
9147 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
9148 dev_priv->display.get_display_clock_speed =
9149 i945_get_display_clock_speed;
9150 else if (IS_I915G(dev))
9151 dev_priv->display.get_display_clock_speed =
9152 i915_get_display_clock_speed;
9153 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
9154 dev_priv->display.get_display_clock_speed =
9155 i9xx_misc_get_display_clock_speed;
9156 else if (IS_I915GM(dev))
9157 dev_priv->display.get_display_clock_speed =
9158 i915gm_get_display_clock_speed;
9159 else if (IS_I865G(dev))
9160 dev_priv->display.get_display_clock_speed =
9161 i865_get_display_clock_speed;
9162 else if (IS_I85X(dev))
9163 dev_priv->display.get_display_clock_speed =
9164 i855_get_display_clock_speed;
9166 dev_priv->display.get_display_clock_speed =
9167 i830_get_display_clock_speed;
9169 if (HAS_PCH_SPLIT(dev)) {
9171 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
9172 dev_priv->display.write_eld = ironlake_write_eld;
9173 } else if (IS_GEN6(dev)) {
9174 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
9175 dev_priv->display.write_eld = ironlake_write_eld;
9176 } else if (IS_IVYBRIDGE(dev)) {
9177 /* FIXME: detect B0+ stepping and use auto training */
9178 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
9179 dev_priv->display.write_eld = ironlake_write_eld;
9180 dev_priv->display.modeset_global_resources =
9181 ivb_modeset_global_resources;
9182 } else if (IS_HASWELL(dev)) {
9183 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
9184 dev_priv->display.write_eld = haswell_write_eld;
9185 dev_priv->display.modeset_global_resources =
9186 haswell_modeset_global_resources;
9188 } else if (IS_G4X(dev)) {
9189 dev_priv->display.write_eld = g4x_write_eld;
9192 /* Default just returns -ENODEV to indicate unsupported */
9193 dev_priv->display.queue_flip = intel_default_queue_flip;
9195 switch (INTEL_INFO(dev)->gen) {
9197 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9201 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9206 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9210 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9213 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9219 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9220 * resume, or other times. This quirk makes sure that's the case for
9223 static void quirk_pipea_force(struct drm_device *dev)
9225 struct drm_i915_private *dev_priv = dev->dev_private;
9227 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9228 DRM_INFO("applying pipe a force quirk\n");
9232 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9234 static void quirk_ssc_force_disable(struct drm_device *dev)
9236 struct drm_i915_private *dev_priv = dev->dev_private;
9237 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9238 DRM_INFO("applying lvds SSC disable quirk\n");
9242 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9245 static void quirk_invert_brightness(struct drm_device *dev)
9247 struct drm_i915_private *dev_priv = dev->dev_private;
9248 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9249 DRM_INFO("applying inverted panel brightness quirk\n");
9252 struct intel_quirk {
9254 int subsystem_vendor;
9255 int subsystem_device;
9256 void (*hook)(struct drm_device *dev);
9259 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9260 struct intel_dmi_quirk {
9261 void (*hook)(struct drm_device *dev);
9262 const struct dmi_system_id (*dmi_id_list)[];
9265 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9267 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9271 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9273 .dmi_id_list = &(const struct dmi_system_id[]) {
9275 .callback = intel_dmi_reverse_brightness,
9276 .ident = "NCR Corporation",
9277 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9278 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9281 { } /* terminating entry */
9283 .hook = quirk_invert_brightness,
9287 static struct intel_quirk intel_quirks[] = {
9288 /* HP Mini needs pipe A force quirk (LP: #322104) */
9289 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9291 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9292 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9294 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9295 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9297 /* 830/845 need to leave pipe A & dpll A up */
9298 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9299 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9301 /* Lenovo U160 cannot use SSC on LVDS */
9302 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9304 /* Sony Vaio Y cannot use SSC on LVDS */
9305 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9307 /* Acer Aspire 5734Z must invert backlight brightness */
9308 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
9310 /* Acer/eMachines G725 */
9311 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
9313 /* Acer/eMachines e725 */
9314 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
9316 /* Acer/Packard Bell NCL20 */
9317 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
9319 /* Acer Aspire 4736Z */
9320 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
9323 static void intel_init_quirks(struct drm_device *dev)
9325 struct pci_dev *d = dev->pdev;
9328 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9329 struct intel_quirk *q = &intel_quirks[i];
9331 if (d->device == q->device &&
9332 (d->subsystem_vendor == q->subsystem_vendor ||
9333 q->subsystem_vendor == PCI_ANY_ID) &&
9334 (d->subsystem_device == q->subsystem_device ||
9335 q->subsystem_device == PCI_ANY_ID))
9338 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9339 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9340 intel_dmi_quirks[i].hook(dev);
9344 /* Disable the VGA plane that we never use */
9345 static void i915_disable_vga(struct drm_device *dev)
9347 struct drm_i915_private *dev_priv = dev->dev_private;
9349 u32 vga_reg = i915_vgacntrl_reg(dev);
9351 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9352 outb(SR01, VGA_SR_INDEX);
9353 sr1 = inb(VGA_SR_DATA);
9354 outb(sr1 | 1<<5, VGA_SR_DATA);
9355 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9358 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9359 POSTING_READ(vga_reg);
9362 void intel_modeset_init_hw(struct drm_device *dev)
9364 intel_init_power_well(dev);
9366 intel_prepare_ddi(dev);
9368 intel_init_clock_gating(dev);
9370 mutex_lock(&dev->struct_mutex);
9371 intel_enable_gt_powersave(dev);
9372 mutex_unlock(&dev->struct_mutex);
9375 void intel_modeset_suspend_hw(struct drm_device *dev)
9377 intel_suspend_hw(dev);
9380 void intel_modeset_init(struct drm_device *dev)
9382 struct drm_i915_private *dev_priv = dev->dev_private;
9385 drm_mode_config_init(dev);
9387 dev->mode_config.min_width = 0;
9388 dev->mode_config.min_height = 0;
9390 dev->mode_config.preferred_depth = 24;
9391 dev->mode_config.prefer_shadow = 1;
9393 dev->mode_config.funcs = &intel_mode_funcs;
9395 intel_init_quirks(dev);
9399 if (INTEL_INFO(dev)->num_pipes == 0)
9402 intel_init_display(dev);
9405 dev->mode_config.max_width = 2048;
9406 dev->mode_config.max_height = 2048;
9407 } else if (IS_GEN3(dev)) {
9408 dev->mode_config.max_width = 4096;
9409 dev->mode_config.max_height = 4096;
9411 dev->mode_config.max_width = 8192;
9412 dev->mode_config.max_height = 8192;
9414 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
9416 DRM_DEBUG_KMS("%d display pipe%s available.\n",
9417 INTEL_INFO(dev)->num_pipes,
9418 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
9420 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
9421 intel_crtc_init(dev, i);
9422 for (j = 0; j < dev_priv->num_plane; j++) {
9423 ret = intel_plane_init(dev, i, j);
9425 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9426 pipe_name(i), sprite_name(i, j), ret);
9430 intel_cpu_pll_init(dev);
9431 intel_pch_pll_init(dev);
9433 /* Just disable it once at startup */
9434 i915_disable_vga(dev);
9435 intel_setup_outputs(dev);
9437 /* Just in case the BIOS is doing something questionable. */
9438 intel_disable_fbc(dev);
9442 intel_connector_break_all_links(struct intel_connector *connector)
9444 connector->base.dpms = DRM_MODE_DPMS_OFF;
9445 connector->base.encoder = NULL;
9446 connector->encoder->connectors_active = false;
9447 connector->encoder->base.crtc = NULL;
9450 static void intel_enable_pipe_a(struct drm_device *dev)
9452 struct intel_connector *connector;
9453 struct drm_connector *crt = NULL;
9454 struct intel_load_detect_pipe load_detect_temp;
9456 /* We can't just switch on the pipe A, we need to set things up with a
9457 * proper mode and output configuration. As a gross hack, enable pipe A
9458 * by enabling the load detect pipe once. */
9459 list_for_each_entry(connector,
9460 &dev->mode_config.connector_list,
9462 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9463 crt = &connector->base;
9471 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9472 intel_release_load_detect_pipe(crt, &load_detect_temp);
9478 intel_check_plane_mapping(struct intel_crtc *crtc)
9480 struct drm_device *dev = crtc->base.dev;
9481 struct drm_i915_private *dev_priv = dev->dev_private;
9484 if (INTEL_INFO(dev)->num_pipes == 1)
9487 reg = DSPCNTR(!crtc->plane);
9488 val = I915_READ(reg);
9490 if ((val & DISPLAY_PLANE_ENABLE) &&
9491 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9497 static void intel_sanitize_crtc(struct intel_crtc *crtc)
9499 struct drm_device *dev = crtc->base.dev;
9500 struct drm_i915_private *dev_priv = dev->dev_private;
9503 /* Clear any frame start delays used for debugging left by the BIOS */
9504 reg = PIPECONF(crtc->config.cpu_transcoder);
9505 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9507 /* We need to sanitize the plane -> pipe mapping first because this will
9508 * disable the crtc (and hence change the state) if it is wrong. Note
9509 * that gen4+ has a fixed plane -> pipe mapping. */
9510 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
9511 struct intel_connector *connector;
9514 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9515 crtc->base.base.id);
9517 /* Pipe has the wrong plane attached and the plane is active.
9518 * Temporarily change the plane mapping and disable everything
9520 plane = crtc->plane;
9521 crtc->plane = !plane;
9522 dev_priv->display.crtc_disable(&crtc->base);
9523 crtc->plane = plane;
9525 /* ... and break all links. */
9526 list_for_each_entry(connector, &dev->mode_config.connector_list,
9528 if (connector->encoder->base.crtc != &crtc->base)
9531 intel_connector_break_all_links(connector);
9534 WARN_ON(crtc->active);
9535 crtc->base.enabled = false;
9538 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9539 crtc->pipe == PIPE_A && !crtc->active) {
9540 /* BIOS forgot to enable pipe A, this mostly happens after
9541 * resume. Force-enable the pipe to fix this, the update_dpms
9542 * call below we restore the pipe to the right state, but leave
9543 * the required bits on. */
9544 intel_enable_pipe_a(dev);
9547 /* Adjust the state of the output pipe according to whether we
9548 * have active connectors/encoders. */
9549 intel_crtc_update_dpms(&crtc->base);
9551 if (crtc->active != crtc->base.enabled) {
9552 struct intel_encoder *encoder;
9554 /* This can happen either due to bugs in the get_hw_state
9555 * functions or because the pipe is force-enabled due to the
9557 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9559 crtc->base.enabled ? "enabled" : "disabled",
9560 crtc->active ? "enabled" : "disabled");
9562 crtc->base.enabled = crtc->active;
9564 /* Because we only establish the connector -> encoder ->
9565 * crtc links if something is active, this means the
9566 * crtc is now deactivated. Break the links. connector
9567 * -> encoder links are only establish when things are
9568 * actually up, hence no need to break them. */
9569 WARN_ON(crtc->active);
9571 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9572 WARN_ON(encoder->connectors_active);
9573 encoder->base.crtc = NULL;
9578 static void intel_sanitize_encoder(struct intel_encoder *encoder)
9580 struct intel_connector *connector;
9581 struct drm_device *dev = encoder->base.dev;
9583 /* We need to check both for a crtc link (meaning that the
9584 * encoder is active and trying to read from a pipe) and the
9585 * pipe itself being active. */
9586 bool has_active_crtc = encoder->base.crtc &&
9587 to_intel_crtc(encoder->base.crtc)->active;
9589 if (encoder->connectors_active && !has_active_crtc) {
9590 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9591 encoder->base.base.id,
9592 drm_get_encoder_name(&encoder->base));
9594 /* Connector is active, but has no active pipe. This is
9595 * fallout from our resume register restoring. Disable
9596 * the encoder manually again. */
9597 if (encoder->base.crtc) {
9598 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9599 encoder->base.base.id,
9600 drm_get_encoder_name(&encoder->base));
9601 encoder->disable(encoder);
9604 /* Inconsistent output/port/pipe state happens presumably due to
9605 * a bug in one of the get_hw_state functions. Or someplace else
9606 * in our code, like the register restore mess on resume. Clamp
9607 * things to off as a safer default. */
9608 list_for_each_entry(connector,
9609 &dev->mode_config.connector_list,
9611 if (connector->encoder != encoder)
9614 intel_connector_break_all_links(connector);
9617 /* Enabled encoders without active connectors will be fixed in
9618 * the crtc fixup. */
9621 void i915_redisable_vga(struct drm_device *dev)
9623 struct drm_i915_private *dev_priv = dev->dev_private;
9624 u32 vga_reg = i915_vgacntrl_reg(dev);
9626 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9627 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
9628 i915_disable_vga(dev);
9632 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9633 * and i915 state tracking structures. */
9634 void intel_modeset_setup_hw_state(struct drm_device *dev,
9637 struct drm_i915_private *dev_priv = dev->dev_private;
9639 struct drm_plane *plane;
9640 struct intel_crtc *crtc;
9641 struct intel_encoder *encoder;
9642 struct intel_connector *connector;
9644 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9646 memset(&crtc->config, 0, sizeof(crtc->config));
9648 crtc->active = dev_priv->display.get_pipe_config(crtc,
9651 crtc->base.enabled = crtc->active;
9653 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9655 crtc->active ? "enabled" : "disabled");
9659 intel_ddi_setup_hw_pll_state(dev);
9661 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9665 if (encoder->get_hw_state(encoder, &pipe)) {
9666 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9667 encoder->base.crtc = &crtc->base;
9668 if (encoder->get_config)
9669 encoder->get_config(encoder, &crtc->config);
9671 encoder->base.crtc = NULL;
9674 encoder->connectors_active = false;
9675 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9676 encoder->base.base.id,
9677 drm_get_encoder_name(&encoder->base),
9678 encoder->base.crtc ? "enabled" : "disabled",
9682 list_for_each_entry(connector, &dev->mode_config.connector_list,
9684 if (connector->get_hw_state(connector)) {
9685 connector->base.dpms = DRM_MODE_DPMS_ON;
9686 connector->encoder->connectors_active = true;
9687 connector->base.encoder = &connector->encoder->base;
9689 connector->base.dpms = DRM_MODE_DPMS_OFF;
9690 connector->base.encoder = NULL;
9692 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9693 connector->base.base.id,
9694 drm_get_connector_name(&connector->base),
9695 connector->base.encoder ? "enabled" : "disabled");
9698 /* HW state is read out, now we need to sanitize this mess. */
9699 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9701 intel_sanitize_encoder(encoder);
9704 for_each_pipe(pipe) {
9705 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9706 intel_sanitize_crtc(crtc);
9707 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
9710 if (force_restore) {
9712 * We need to use raw interfaces for restoring state to avoid
9713 * checking (bogus) intermediate states.
9715 for_each_pipe(pipe) {
9716 struct drm_crtc *crtc =
9717 dev_priv->pipe_to_crtc_mapping[pipe];
9719 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9722 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9723 intel_plane_restore(plane);
9725 i915_redisable_vga(dev);
9727 intel_modeset_update_staged_output_state(dev);
9730 intel_modeset_check_state(dev);
9732 drm_mode_config_reset(dev);
9735 void intel_modeset_gem_init(struct drm_device *dev)
9737 intel_modeset_init_hw(dev);
9739 intel_setup_overlay(dev);
9741 intel_modeset_setup_hw_state(dev, false);
9744 void intel_modeset_cleanup(struct drm_device *dev)
9746 struct drm_i915_private *dev_priv = dev->dev_private;
9747 struct drm_crtc *crtc;
9748 struct intel_crtc *intel_crtc;
9751 * Interrupts and polling as the first thing to avoid creating havoc.
9752 * Too much stuff here (turning of rps, connectors, ...) would
9753 * experience fancy races otherwise.
9755 drm_irq_uninstall(dev);
9756 cancel_work_sync(&dev_priv->hotplug_work);
9758 * Due to the hpd irq storm handling the hotplug work can re-arm the
9759 * poll handlers. Hence disable polling after hpd handling is shut down.
9761 drm_kms_helper_poll_fini(dev);
9763 mutex_lock(&dev->struct_mutex);
9765 intel_unregister_dsm_handler();
9767 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9768 /* Skip inactive CRTCs */
9772 intel_crtc = to_intel_crtc(crtc);
9773 intel_increase_pllclock(crtc);
9776 intel_disable_fbc(dev);
9778 intel_disable_gt_powersave(dev);
9780 ironlake_teardown_rc6(dev);
9782 mutex_unlock(&dev->struct_mutex);
9784 /* flush any delayed tasks or pending work */
9785 flush_scheduled_work();
9787 /* destroy backlight, if any, before the connectors */
9788 intel_panel_destroy_backlight(dev);
9790 drm_mode_config_cleanup(dev);
9792 intel_cleanup_overlay(dev);
9796 * Return which encoder is currently attached for connector.
9798 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
9800 return &intel_attached_encoder(connector)->base;
9803 void intel_connector_attach_encoder(struct intel_connector *connector,
9804 struct intel_encoder *encoder)
9806 connector->encoder = encoder;
9807 drm_mode_connector_attach_encoder(&connector->base,
9812 * set vga decode state - true == enable VGA decode
9814 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9816 struct drm_i915_private *dev_priv = dev->dev_private;
9819 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9821 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9823 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9824 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9828 #ifdef CONFIG_DEBUG_FS
9829 #include <linux/seq_file.h>
9831 struct intel_display_error_state {
9833 u32 power_well_driver;
9835 struct intel_cursor_error_state {
9840 } cursor[I915_MAX_PIPES];
9842 struct intel_pipe_error_state {
9843 enum transcoder cpu_transcoder;
9853 } pipe[I915_MAX_PIPES];
9855 struct intel_plane_error_state {
9863 } plane[I915_MAX_PIPES];
9866 struct intel_display_error_state *
9867 intel_display_capture_error_state(struct drm_device *dev)
9869 drm_i915_private_t *dev_priv = dev->dev_private;
9870 struct intel_display_error_state *error;
9871 enum transcoder cpu_transcoder;
9874 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9878 if (HAS_POWER_WELL(dev))
9879 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
9882 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9883 error->pipe[i].cpu_transcoder = cpu_transcoder;
9885 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9886 error->cursor[i].control = I915_READ(CURCNTR(i));
9887 error->cursor[i].position = I915_READ(CURPOS(i));
9888 error->cursor[i].base = I915_READ(CURBASE(i));
9890 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9891 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9892 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9895 error->plane[i].control = I915_READ(DSPCNTR(i));
9896 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9897 if (INTEL_INFO(dev)->gen <= 3) {
9898 error->plane[i].size = I915_READ(DSPSIZE(i));
9899 error->plane[i].pos = I915_READ(DSPPOS(i));
9901 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9902 error->plane[i].addr = I915_READ(DSPADDR(i));
9903 if (INTEL_INFO(dev)->gen >= 4) {
9904 error->plane[i].surface = I915_READ(DSPSURF(i));
9905 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9908 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
9909 error->pipe[i].source = I915_READ(PIPESRC(i));
9910 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9911 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9912 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9913 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9914 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9915 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
9918 /* In the code above we read the registers without checking if the power
9919 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
9920 * prevent the next I915_WRITE from detecting it and printing an error
9922 if (HAS_POWER_WELL(dev))
9923 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
9928 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
9931 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
9932 struct drm_device *dev,
9933 struct intel_display_error_state *error)
9937 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
9938 if (HAS_POWER_WELL(dev))
9939 err_printf(m, "PWR_WELL_CTL2: %08x\n",
9940 error->power_well_driver);
9942 err_printf(m, "Pipe [%d]:\n", i);
9943 err_printf(m, " CPU transcoder: %c\n",
9944 transcoder_name(error->pipe[i].cpu_transcoder));
9945 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9946 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
9947 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9948 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9949 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9950 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9951 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9952 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9954 err_printf(m, "Plane [%d]:\n", i);
9955 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
9956 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9957 if (INTEL_INFO(dev)->gen <= 3) {
9958 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
9959 err_printf(m, " POS: %08x\n", error->plane[i].pos);
9961 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9962 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9963 if (INTEL_INFO(dev)->gen >= 4) {
9964 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
9965 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9968 err_printf(m, "Cursor [%d]:\n", i);
9969 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9970 err_printf(m, " POS: %08x\n", error->cursor[i].position);
9971 err_printf(m, " BASE: %08x\n", error->cursor[i].base);