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++ && pll->on) {
1431 assert_pch_pll_enabled(dev_priv, pll, NULL);
1435 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1438 val = I915_READ(reg);
1439 val |= DPLL_VCO_ENABLE;
1440 I915_WRITE(reg, val);
1447 static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1449 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1450 struct intel_pch_pll *pll = intel_crtc->pch_pll;
1454 /* PCH only available on ILK+ */
1455 BUG_ON(dev_priv->info->gen < 5);
1459 if (WARN_ON(pll->refcount == 0))
1462 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1463 pll->pll_reg, pll->active, pll->on,
1464 intel_crtc->base.base.id);
1466 if (WARN_ON(pll->active == 0)) {
1467 assert_pch_pll_disabled(dev_priv, pll, NULL);
1471 if (--pll->active) {
1472 assert_pch_pll_enabled(dev_priv, pll, NULL);
1476 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1478 /* Make sure transcoder isn't still depending on us */
1479 assert_pch_transcoder_disabled(dev_priv, intel_crtc->pipe);
1482 val = I915_READ(reg);
1483 val &= ~DPLL_VCO_ENABLE;
1484 I915_WRITE(reg, val);
1491 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1494 struct drm_device *dev = dev_priv->dev;
1495 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1496 uint32_t reg, val, pipeconf_val;
1498 /* PCH only available on ILK+ */
1499 BUG_ON(dev_priv->info->gen < 5);
1501 /* Make sure PCH DPLL is enabled */
1502 assert_pch_pll_enabled(dev_priv,
1503 to_intel_crtc(crtc)->pch_pll,
1504 to_intel_crtc(crtc));
1506 /* FDI must be feeding us bits for PCH ports */
1507 assert_fdi_tx_enabled(dev_priv, pipe);
1508 assert_fdi_rx_enabled(dev_priv, pipe);
1510 if (HAS_PCH_CPT(dev)) {
1511 /* Workaround: Set the timing override bit before enabling the
1512 * pch transcoder. */
1513 reg = TRANS_CHICKEN2(pipe);
1514 val = I915_READ(reg);
1515 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1516 I915_WRITE(reg, val);
1519 reg = PCH_TRANSCONF(pipe);
1520 val = I915_READ(reg);
1521 pipeconf_val = I915_READ(PIPECONF(pipe));
1523 if (HAS_PCH_IBX(dev_priv->dev)) {
1525 * make the BPC in transcoder be consistent with
1526 * that in pipeconf reg.
1528 val &= ~PIPECONF_BPC_MASK;
1529 val |= pipeconf_val & PIPECONF_BPC_MASK;
1532 val &= ~TRANS_INTERLACE_MASK;
1533 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1534 if (HAS_PCH_IBX(dev_priv->dev) &&
1535 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1536 val |= TRANS_LEGACY_INTERLACED_ILK;
1538 val |= TRANS_INTERLACED;
1540 val |= TRANS_PROGRESSIVE;
1542 I915_WRITE(reg, val | TRANS_ENABLE);
1543 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1544 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1547 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1548 enum transcoder cpu_transcoder)
1550 u32 val, pipeconf_val;
1552 /* PCH only available on ILK+ */
1553 BUG_ON(dev_priv->info->gen < 5);
1555 /* FDI must be feeding us bits for PCH ports */
1556 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1557 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1559 /* Workaround: set timing override bit. */
1560 val = I915_READ(_TRANSA_CHICKEN2);
1561 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1562 I915_WRITE(_TRANSA_CHICKEN2, val);
1565 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1567 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1568 PIPECONF_INTERLACED_ILK)
1569 val |= TRANS_INTERLACED;
1571 val |= TRANS_PROGRESSIVE;
1573 I915_WRITE(LPT_TRANSCONF, val);
1574 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1575 DRM_ERROR("Failed to enable PCH transcoder\n");
1578 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1581 struct drm_device *dev = dev_priv->dev;
1584 /* FDI relies on the transcoder */
1585 assert_fdi_tx_disabled(dev_priv, pipe);
1586 assert_fdi_rx_disabled(dev_priv, pipe);
1588 /* Ports must be off as well */
1589 assert_pch_ports_disabled(dev_priv, pipe);
1591 reg = PCH_TRANSCONF(pipe);
1592 val = I915_READ(reg);
1593 val &= ~TRANS_ENABLE;
1594 I915_WRITE(reg, val);
1595 /* wait for PCH transcoder off, transcoder state */
1596 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1597 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1599 if (!HAS_PCH_IBX(dev)) {
1600 /* Workaround: Clear the timing override chicken bit again. */
1601 reg = TRANS_CHICKEN2(pipe);
1602 val = I915_READ(reg);
1603 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1604 I915_WRITE(reg, val);
1608 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1612 val = I915_READ(LPT_TRANSCONF);
1613 val &= ~TRANS_ENABLE;
1614 I915_WRITE(LPT_TRANSCONF, val);
1615 /* wait for PCH transcoder off, transcoder state */
1616 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
1617 DRM_ERROR("Failed to disable PCH transcoder\n");
1619 /* Workaround: clear timing override bit. */
1620 val = I915_READ(_TRANSA_CHICKEN2);
1621 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1622 I915_WRITE(_TRANSA_CHICKEN2, val);
1626 * intel_enable_pipe - enable a pipe, asserting requirements
1627 * @dev_priv: i915 private structure
1628 * @pipe: pipe to enable
1629 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1631 * Enable @pipe, making sure that various hardware specific requirements
1632 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1634 * @pipe should be %PIPE_A or %PIPE_B.
1636 * Will wait until the pipe is actually running (i.e. first vblank) before
1639 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1642 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1644 enum pipe pch_transcoder;
1648 assert_planes_disabled(dev_priv, pipe);
1649 assert_sprites_disabled(dev_priv, pipe);
1651 if (HAS_PCH_LPT(dev_priv->dev))
1652 pch_transcoder = TRANSCODER_A;
1654 pch_transcoder = pipe;
1657 * A pipe without a PLL won't actually be able to drive bits from
1658 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1661 if (!HAS_PCH_SPLIT(dev_priv->dev))
1662 assert_pll_enabled(dev_priv, pipe);
1665 /* if driving the PCH, we need FDI enabled */
1666 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1667 assert_fdi_tx_pll_enabled(dev_priv,
1668 (enum pipe) cpu_transcoder);
1670 /* FIXME: assert CPU port conditions for SNB+ */
1673 reg = PIPECONF(cpu_transcoder);
1674 val = I915_READ(reg);
1675 if (val & PIPECONF_ENABLE)
1678 I915_WRITE(reg, val | PIPECONF_ENABLE);
1679 intel_wait_for_vblank(dev_priv->dev, pipe);
1683 * intel_disable_pipe - disable a pipe, asserting requirements
1684 * @dev_priv: i915 private structure
1685 * @pipe: pipe to disable
1687 * Disable @pipe, making sure that various hardware specific requirements
1688 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1690 * @pipe should be %PIPE_A or %PIPE_B.
1692 * Will wait until the pipe has shut down before returning.
1694 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1697 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1703 * Make sure planes won't keep trying to pump pixels to us,
1704 * or we might hang the display.
1706 assert_planes_disabled(dev_priv, pipe);
1707 assert_sprites_disabled(dev_priv, pipe);
1709 /* Don't disable pipe A or pipe A PLLs if needed */
1710 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1713 reg = PIPECONF(cpu_transcoder);
1714 val = I915_READ(reg);
1715 if ((val & PIPECONF_ENABLE) == 0)
1718 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1719 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1723 * Plane regs are double buffered, going from enabled->disabled needs a
1724 * trigger in order to latch. The display address reg provides this.
1726 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1729 if (dev_priv->info->gen >= 4)
1730 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1732 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1736 * intel_enable_plane - enable a display plane on a given pipe
1737 * @dev_priv: i915 private structure
1738 * @plane: plane to enable
1739 * @pipe: pipe being fed
1741 * Enable @plane on @pipe, making sure that @pipe is running first.
1743 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1744 enum plane plane, enum pipe pipe)
1749 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1750 assert_pipe_enabled(dev_priv, pipe);
1752 reg = DSPCNTR(plane);
1753 val = I915_READ(reg);
1754 if (val & DISPLAY_PLANE_ENABLE)
1757 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1758 intel_flush_display_plane(dev_priv, plane);
1759 intel_wait_for_vblank(dev_priv->dev, pipe);
1763 * intel_disable_plane - disable a display plane
1764 * @dev_priv: i915 private structure
1765 * @plane: plane to disable
1766 * @pipe: pipe consuming the data
1768 * Disable @plane; should be an independent operation.
1770 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1771 enum plane plane, enum pipe pipe)
1776 reg = DSPCNTR(plane);
1777 val = I915_READ(reg);
1778 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1781 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1782 intel_flush_display_plane(dev_priv, plane);
1783 intel_wait_for_vblank(dev_priv->dev, pipe);
1786 static bool need_vtd_wa(struct drm_device *dev)
1788 #ifdef CONFIG_INTEL_IOMMU
1789 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1796 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1797 struct drm_i915_gem_object *obj,
1798 struct intel_ring_buffer *pipelined)
1800 struct drm_i915_private *dev_priv = dev->dev_private;
1804 switch (obj->tiling_mode) {
1805 case I915_TILING_NONE:
1806 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1807 alignment = 128 * 1024;
1808 else if (INTEL_INFO(dev)->gen >= 4)
1809 alignment = 4 * 1024;
1811 alignment = 64 * 1024;
1814 /* pin() will align the object as required by fence */
1818 /* Despite that we check this in framebuffer_init userspace can
1819 * screw us over and change the tiling after the fact. Only
1820 * pinned buffers can't change their tiling. */
1821 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
1827 /* Note that the w/a also requires 64 PTE of padding following the
1828 * bo. We currently fill all unused PTE with the shadow page and so
1829 * we should always have valid PTE following the scanout preventing
1832 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1833 alignment = 256 * 1024;
1835 dev_priv->mm.interruptible = false;
1836 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1838 goto err_interruptible;
1840 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1841 * fence, whereas 965+ only requires a fence if using
1842 * framebuffer compression. For simplicity, we always install
1843 * a fence as the cost is not that onerous.
1845 ret = i915_gem_object_get_fence(obj);
1849 i915_gem_object_pin_fence(obj);
1851 dev_priv->mm.interruptible = true;
1855 i915_gem_object_unpin(obj);
1857 dev_priv->mm.interruptible = true;
1861 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1863 i915_gem_object_unpin_fence(obj);
1864 i915_gem_object_unpin(obj);
1867 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1868 * is assumed to be a power-of-two. */
1869 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1870 unsigned int tiling_mode,
1874 if (tiling_mode != I915_TILING_NONE) {
1875 unsigned int tile_rows, tiles;
1880 tiles = *x / (512/cpp);
1883 return tile_rows * pitch * 8 + tiles * 4096;
1885 unsigned int offset;
1887 offset = *y * pitch + *x * cpp;
1889 *x = (offset & 4095) / cpp;
1890 return offset & -4096;
1894 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1897 struct drm_device *dev = crtc->dev;
1898 struct drm_i915_private *dev_priv = dev->dev_private;
1899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1900 struct intel_framebuffer *intel_fb;
1901 struct drm_i915_gem_object *obj;
1902 int plane = intel_crtc->plane;
1903 unsigned long linear_offset;
1912 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
1916 intel_fb = to_intel_framebuffer(fb);
1917 obj = intel_fb->obj;
1919 reg = DSPCNTR(plane);
1920 dspcntr = I915_READ(reg);
1921 /* Mask out pixel format bits in case we change it */
1922 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1923 switch (fb->pixel_format) {
1925 dspcntr |= DISPPLANE_8BPP;
1927 case DRM_FORMAT_XRGB1555:
1928 case DRM_FORMAT_ARGB1555:
1929 dspcntr |= DISPPLANE_BGRX555;
1931 case DRM_FORMAT_RGB565:
1932 dspcntr |= DISPPLANE_BGRX565;
1934 case DRM_FORMAT_XRGB8888:
1935 case DRM_FORMAT_ARGB8888:
1936 dspcntr |= DISPPLANE_BGRX888;
1938 case DRM_FORMAT_XBGR8888:
1939 case DRM_FORMAT_ABGR8888:
1940 dspcntr |= DISPPLANE_RGBX888;
1942 case DRM_FORMAT_XRGB2101010:
1943 case DRM_FORMAT_ARGB2101010:
1944 dspcntr |= DISPPLANE_BGRX101010;
1946 case DRM_FORMAT_XBGR2101010:
1947 case DRM_FORMAT_ABGR2101010:
1948 dspcntr |= DISPPLANE_RGBX101010;
1954 if (INTEL_INFO(dev)->gen >= 4) {
1955 if (obj->tiling_mode != I915_TILING_NONE)
1956 dspcntr |= DISPPLANE_TILED;
1958 dspcntr &= ~DISPPLANE_TILED;
1962 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1964 I915_WRITE(reg, dspcntr);
1966 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1968 if (INTEL_INFO(dev)->gen >= 4) {
1969 intel_crtc->dspaddr_offset =
1970 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1971 fb->bits_per_pixel / 8,
1973 linear_offset -= intel_crtc->dspaddr_offset;
1975 intel_crtc->dspaddr_offset = linear_offset;
1978 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
1979 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
1980 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1981 if (INTEL_INFO(dev)->gen >= 4) {
1982 I915_MODIFY_DISPBASE(DSPSURF(plane),
1983 obj->gtt_offset + intel_crtc->dspaddr_offset);
1984 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1985 I915_WRITE(DSPLINOFF(plane), linear_offset);
1987 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
1993 static int ironlake_update_plane(struct drm_crtc *crtc,
1994 struct drm_framebuffer *fb, int x, int y)
1996 struct drm_device *dev = crtc->dev;
1997 struct drm_i915_private *dev_priv = dev->dev_private;
1998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1999 struct intel_framebuffer *intel_fb;
2000 struct drm_i915_gem_object *obj;
2001 int plane = intel_crtc->plane;
2002 unsigned long linear_offset;
2012 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
2016 intel_fb = to_intel_framebuffer(fb);
2017 obj = intel_fb->obj;
2019 reg = DSPCNTR(plane);
2020 dspcntr = I915_READ(reg);
2021 /* Mask out pixel format bits in case we change it */
2022 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2023 switch (fb->pixel_format) {
2025 dspcntr |= DISPPLANE_8BPP;
2027 case DRM_FORMAT_RGB565:
2028 dspcntr |= DISPPLANE_BGRX565;
2030 case DRM_FORMAT_XRGB8888:
2031 case DRM_FORMAT_ARGB8888:
2032 dspcntr |= DISPPLANE_BGRX888;
2034 case DRM_FORMAT_XBGR8888:
2035 case DRM_FORMAT_ABGR8888:
2036 dspcntr |= DISPPLANE_RGBX888;
2038 case DRM_FORMAT_XRGB2101010:
2039 case DRM_FORMAT_ARGB2101010:
2040 dspcntr |= DISPPLANE_BGRX101010;
2042 case DRM_FORMAT_XBGR2101010:
2043 case DRM_FORMAT_ABGR2101010:
2044 dspcntr |= DISPPLANE_RGBX101010;
2050 if (obj->tiling_mode != I915_TILING_NONE)
2051 dspcntr |= DISPPLANE_TILED;
2053 dspcntr &= ~DISPPLANE_TILED;
2056 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2058 I915_WRITE(reg, dspcntr);
2060 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2061 intel_crtc->dspaddr_offset =
2062 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2063 fb->bits_per_pixel / 8,
2065 linear_offset -= intel_crtc->dspaddr_offset;
2067 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2068 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2069 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2070 I915_MODIFY_DISPBASE(DSPSURF(plane),
2071 obj->gtt_offset + intel_crtc->dspaddr_offset);
2072 if (IS_HASWELL(dev)) {
2073 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2075 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2076 I915_WRITE(DSPLINOFF(plane), linear_offset);
2083 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2085 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2086 int x, int y, enum mode_set_atomic state)
2088 struct drm_device *dev = crtc->dev;
2089 struct drm_i915_private *dev_priv = dev->dev_private;
2091 if (dev_priv->display.disable_fbc)
2092 dev_priv->display.disable_fbc(dev);
2093 intel_increase_pllclock(crtc);
2095 return dev_priv->display.update_plane(crtc, fb, x, y);
2098 void intel_display_handle_reset(struct drm_device *dev)
2100 struct drm_i915_private *dev_priv = dev->dev_private;
2101 struct drm_crtc *crtc;
2104 * Flips in the rings have been nuked by the reset,
2105 * so complete all pending flips so that user space
2106 * will get its events and not get stuck.
2108 * Also update the base address of all primary
2109 * planes to the the last fb to make sure we're
2110 * showing the correct fb after a reset.
2112 * Need to make two loops over the crtcs so that we
2113 * don't try to grab a crtc mutex before the
2114 * pending_flip_queue really got woken up.
2117 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2119 enum plane plane = intel_crtc->plane;
2121 intel_prepare_page_flip(dev, plane);
2122 intel_finish_page_flip_plane(dev, plane);
2125 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2128 mutex_lock(&crtc->mutex);
2129 if (intel_crtc->active)
2130 dev_priv->display.update_plane(crtc, crtc->fb,
2132 mutex_unlock(&crtc->mutex);
2137 intel_finish_fb(struct drm_framebuffer *old_fb)
2139 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2140 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2141 bool was_interruptible = dev_priv->mm.interruptible;
2144 /* Big Hammer, we also need to ensure that any pending
2145 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2146 * current scanout is retired before unpinning the old
2149 * This should only fail upon a hung GPU, in which case we
2150 * can safely continue.
2152 dev_priv->mm.interruptible = false;
2153 ret = i915_gem_object_finish_gpu(obj);
2154 dev_priv->mm.interruptible = was_interruptible;
2159 static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2161 struct drm_device *dev = crtc->dev;
2162 struct drm_i915_master_private *master_priv;
2163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2165 if (!dev->primary->master)
2168 master_priv = dev->primary->master->driver_priv;
2169 if (!master_priv->sarea_priv)
2172 switch (intel_crtc->pipe) {
2174 master_priv->sarea_priv->pipeA_x = x;
2175 master_priv->sarea_priv->pipeA_y = y;
2178 master_priv->sarea_priv->pipeB_x = x;
2179 master_priv->sarea_priv->pipeB_y = y;
2187 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2188 struct drm_framebuffer *fb)
2190 struct drm_device *dev = crtc->dev;
2191 struct drm_i915_private *dev_priv = dev->dev_private;
2192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2193 struct drm_framebuffer *old_fb;
2198 DRM_ERROR("No FB bound\n");
2202 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2203 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2204 plane_name(intel_crtc->plane),
2205 INTEL_INFO(dev)->num_pipes);
2209 mutex_lock(&dev->struct_mutex);
2210 ret = intel_pin_and_fence_fb_obj(dev,
2211 to_intel_framebuffer(fb)->obj,
2214 mutex_unlock(&dev->struct_mutex);
2215 DRM_ERROR("pin & fence failed\n");
2219 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2221 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2222 mutex_unlock(&dev->struct_mutex);
2223 DRM_ERROR("failed to update base address\n");
2233 if (intel_crtc->active && old_fb != fb)
2234 intel_wait_for_vblank(dev, intel_crtc->pipe);
2235 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2238 intel_update_fbc(dev);
2239 mutex_unlock(&dev->struct_mutex);
2241 intel_crtc_update_sarea_pos(crtc, x, y);
2246 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2248 struct drm_device *dev = crtc->dev;
2249 struct drm_i915_private *dev_priv = dev->dev_private;
2250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2251 int pipe = intel_crtc->pipe;
2254 /* enable normal train */
2255 reg = FDI_TX_CTL(pipe);
2256 temp = I915_READ(reg);
2257 if (IS_IVYBRIDGE(dev)) {
2258 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2259 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2261 temp &= ~FDI_LINK_TRAIN_NONE;
2262 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2264 I915_WRITE(reg, temp);
2266 reg = FDI_RX_CTL(pipe);
2267 temp = I915_READ(reg);
2268 if (HAS_PCH_CPT(dev)) {
2269 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2270 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2272 temp &= ~FDI_LINK_TRAIN_NONE;
2273 temp |= FDI_LINK_TRAIN_NONE;
2275 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2277 /* wait one idle pattern time */
2281 /* IVB wants error correction enabled */
2282 if (IS_IVYBRIDGE(dev))
2283 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2284 FDI_FE_ERRC_ENABLE);
2287 static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2289 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2292 static void ivb_modeset_global_resources(struct drm_device *dev)
2294 struct drm_i915_private *dev_priv = dev->dev_private;
2295 struct intel_crtc *pipe_B_crtc =
2296 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2297 struct intel_crtc *pipe_C_crtc =
2298 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2302 * When everything is off disable fdi C so that we could enable fdi B
2303 * with all lanes. Note that we don't care about enabled pipes without
2304 * an enabled pch encoder.
2306 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2307 !pipe_has_enabled_pch(pipe_C_crtc)) {
2308 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2309 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2311 temp = I915_READ(SOUTH_CHICKEN1);
2312 temp &= ~FDI_BC_BIFURCATION_SELECT;
2313 DRM_DEBUG_KMS("disabling fdi C rx\n");
2314 I915_WRITE(SOUTH_CHICKEN1, temp);
2318 /* The FDI link training functions for ILK/Ibexpeak. */
2319 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2321 struct drm_device *dev = crtc->dev;
2322 struct drm_i915_private *dev_priv = dev->dev_private;
2323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2324 int pipe = intel_crtc->pipe;
2325 int plane = intel_crtc->plane;
2326 u32 reg, temp, tries;
2328 /* FDI needs bits from pipe & plane first */
2329 assert_pipe_enabled(dev_priv, pipe);
2330 assert_plane_enabled(dev_priv, plane);
2332 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2334 reg = FDI_RX_IMR(pipe);
2335 temp = I915_READ(reg);
2336 temp &= ~FDI_RX_SYMBOL_LOCK;
2337 temp &= ~FDI_RX_BIT_LOCK;
2338 I915_WRITE(reg, temp);
2342 /* enable CPU FDI TX and PCH FDI RX */
2343 reg = FDI_TX_CTL(pipe);
2344 temp = I915_READ(reg);
2345 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2346 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2347 temp &= ~FDI_LINK_TRAIN_NONE;
2348 temp |= FDI_LINK_TRAIN_PATTERN_1;
2349 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2351 reg = FDI_RX_CTL(pipe);
2352 temp = I915_READ(reg);
2353 temp &= ~FDI_LINK_TRAIN_NONE;
2354 temp |= FDI_LINK_TRAIN_PATTERN_1;
2355 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2360 /* Ironlake workaround, enable clock pointer after FDI enable*/
2361 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2362 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2363 FDI_RX_PHASE_SYNC_POINTER_EN);
2365 reg = FDI_RX_IIR(pipe);
2366 for (tries = 0; tries < 5; tries++) {
2367 temp = I915_READ(reg);
2368 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2370 if ((temp & FDI_RX_BIT_LOCK)) {
2371 DRM_DEBUG_KMS("FDI train 1 done.\n");
2372 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2377 DRM_ERROR("FDI train 1 fail!\n");
2380 reg = FDI_TX_CTL(pipe);
2381 temp = I915_READ(reg);
2382 temp &= ~FDI_LINK_TRAIN_NONE;
2383 temp |= FDI_LINK_TRAIN_PATTERN_2;
2384 I915_WRITE(reg, temp);
2386 reg = FDI_RX_CTL(pipe);
2387 temp = I915_READ(reg);
2388 temp &= ~FDI_LINK_TRAIN_NONE;
2389 temp |= FDI_LINK_TRAIN_PATTERN_2;
2390 I915_WRITE(reg, temp);
2395 reg = FDI_RX_IIR(pipe);
2396 for (tries = 0; tries < 5; tries++) {
2397 temp = I915_READ(reg);
2398 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2400 if (temp & FDI_RX_SYMBOL_LOCK) {
2401 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2402 DRM_DEBUG_KMS("FDI train 2 done.\n");
2407 DRM_ERROR("FDI train 2 fail!\n");
2409 DRM_DEBUG_KMS("FDI train done\n");
2413 static const int snb_b_fdi_train_param[] = {
2414 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2415 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2416 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2417 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2420 /* The FDI link training functions for SNB/Cougarpoint. */
2421 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2423 struct drm_device *dev = crtc->dev;
2424 struct drm_i915_private *dev_priv = dev->dev_private;
2425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2426 int pipe = intel_crtc->pipe;
2427 u32 reg, temp, i, retry;
2429 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2431 reg = FDI_RX_IMR(pipe);
2432 temp = I915_READ(reg);
2433 temp &= ~FDI_RX_SYMBOL_LOCK;
2434 temp &= ~FDI_RX_BIT_LOCK;
2435 I915_WRITE(reg, temp);
2440 /* enable CPU FDI TX and PCH FDI RX */
2441 reg = FDI_TX_CTL(pipe);
2442 temp = I915_READ(reg);
2443 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2444 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2445 temp &= ~FDI_LINK_TRAIN_NONE;
2446 temp |= FDI_LINK_TRAIN_PATTERN_1;
2447 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2449 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2450 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2452 I915_WRITE(FDI_RX_MISC(pipe),
2453 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2455 reg = FDI_RX_CTL(pipe);
2456 temp = I915_READ(reg);
2457 if (HAS_PCH_CPT(dev)) {
2458 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2459 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2461 temp &= ~FDI_LINK_TRAIN_NONE;
2462 temp |= FDI_LINK_TRAIN_PATTERN_1;
2464 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2469 for (i = 0; i < 4; i++) {
2470 reg = FDI_TX_CTL(pipe);
2471 temp = I915_READ(reg);
2472 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2473 temp |= snb_b_fdi_train_param[i];
2474 I915_WRITE(reg, temp);
2479 for (retry = 0; retry < 5; retry++) {
2480 reg = FDI_RX_IIR(pipe);
2481 temp = I915_READ(reg);
2482 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2483 if (temp & FDI_RX_BIT_LOCK) {
2484 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2485 DRM_DEBUG_KMS("FDI train 1 done.\n");
2494 DRM_ERROR("FDI train 1 fail!\n");
2497 reg = FDI_TX_CTL(pipe);
2498 temp = I915_READ(reg);
2499 temp &= ~FDI_LINK_TRAIN_NONE;
2500 temp |= FDI_LINK_TRAIN_PATTERN_2;
2502 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2504 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2506 I915_WRITE(reg, temp);
2508 reg = FDI_RX_CTL(pipe);
2509 temp = I915_READ(reg);
2510 if (HAS_PCH_CPT(dev)) {
2511 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2512 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2514 temp &= ~FDI_LINK_TRAIN_NONE;
2515 temp |= FDI_LINK_TRAIN_PATTERN_2;
2517 I915_WRITE(reg, temp);
2522 for (i = 0; i < 4; i++) {
2523 reg = FDI_TX_CTL(pipe);
2524 temp = I915_READ(reg);
2525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2526 temp |= snb_b_fdi_train_param[i];
2527 I915_WRITE(reg, temp);
2532 for (retry = 0; retry < 5; retry++) {
2533 reg = FDI_RX_IIR(pipe);
2534 temp = I915_READ(reg);
2535 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2536 if (temp & FDI_RX_SYMBOL_LOCK) {
2537 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2538 DRM_DEBUG_KMS("FDI train 2 done.\n");
2547 DRM_ERROR("FDI train 2 fail!\n");
2549 DRM_DEBUG_KMS("FDI train done.\n");
2552 /* Manual link training for Ivy Bridge A0 parts */
2553 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2555 struct drm_device *dev = crtc->dev;
2556 struct drm_i915_private *dev_priv = dev->dev_private;
2557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2558 int pipe = intel_crtc->pipe;
2561 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2563 reg = FDI_RX_IMR(pipe);
2564 temp = I915_READ(reg);
2565 temp &= ~FDI_RX_SYMBOL_LOCK;
2566 temp &= ~FDI_RX_BIT_LOCK;
2567 I915_WRITE(reg, temp);
2572 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2573 I915_READ(FDI_RX_IIR(pipe)));
2575 /* enable CPU FDI TX and PCH FDI RX */
2576 reg = FDI_TX_CTL(pipe);
2577 temp = I915_READ(reg);
2578 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2579 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2580 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2581 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2583 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2584 temp |= FDI_COMPOSITE_SYNC;
2585 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2587 I915_WRITE(FDI_RX_MISC(pipe),
2588 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2590 reg = FDI_RX_CTL(pipe);
2591 temp = I915_READ(reg);
2592 temp &= ~FDI_LINK_TRAIN_AUTO;
2593 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2594 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2595 temp |= FDI_COMPOSITE_SYNC;
2596 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2601 for (i = 0; i < 4; i++) {
2602 reg = FDI_TX_CTL(pipe);
2603 temp = I915_READ(reg);
2604 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2605 temp |= snb_b_fdi_train_param[i];
2606 I915_WRITE(reg, temp);
2611 reg = FDI_RX_IIR(pipe);
2612 temp = I915_READ(reg);
2613 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2615 if (temp & FDI_RX_BIT_LOCK ||
2616 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2617 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2618 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
2623 DRM_ERROR("FDI train 1 fail!\n");
2626 reg = FDI_TX_CTL(pipe);
2627 temp = I915_READ(reg);
2628 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2629 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2630 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2631 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2632 I915_WRITE(reg, temp);
2634 reg = FDI_RX_CTL(pipe);
2635 temp = I915_READ(reg);
2636 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2637 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2638 I915_WRITE(reg, temp);
2643 for (i = 0; i < 4; i++) {
2644 reg = FDI_TX_CTL(pipe);
2645 temp = I915_READ(reg);
2646 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2647 temp |= snb_b_fdi_train_param[i];
2648 I915_WRITE(reg, temp);
2653 reg = FDI_RX_IIR(pipe);
2654 temp = I915_READ(reg);
2655 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2657 if (temp & FDI_RX_SYMBOL_LOCK) {
2658 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2659 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
2664 DRM_ERROR("FDI train 2 fail!\n");
2666 DRM_DEBUG_KMS("FDI train done.\n");
2669 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2671 struct drm_device *dev = intel_crtc->base.dev;
2672 struct drm_i915_private *dev_priv = dev->dev_private;
2673 int pipe = intel_crtc->pipe;
2677 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2678 reg = FDI_RX_CTL(pipe);
2679 temp = I915_READ(reg);
2680 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2681 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2682 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2683 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2688 /* Switch from Rawclk to PCDclk */
2689 temp = I915_READ(reg);
2690 I915_WRITE(reg, temp | FDI_PCDCLK);
2695 /* Enable CPU FDI TX PLL, always on for Ironlake */
2696 reg = FDI_TX_CTL(pipe);
2697 temp = I915_READ(reg);
2698 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2699 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2706 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2708 struct drm_device *dev = intel_crtc->base.dev;
2709 struct drm_i915_private *dev_priv = dev->dev_private;
2710 int pipe = intel_crtc->pipe;
2713 /* Switch from PCDclk to Rawclk */
2714 reg = FDI_RX_CTL(pipe);
2715 temp = I915_READ(reg);
2716 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2718 /* Disable CPU FDI TX PLL */
2719 reg = FDI_TX_CTL(pipe);
2720 temp = I915_READ(reg);
2721 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2726 reg = FDI_RX_CTL(pipe);
2727 temp = I915_READ(reg);
2728 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2730 /* Wait for the clocks to turn off. */
2735 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2740 int pipe = intel_crtc->pipe;
2743 /* disable CPU FDI tx and PCH FDI rx */
2744 reg = FDI_TX_CTL(pipe);
2745 temp = I915_READ(reg);
2746 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2749 reg = FDI_RX_CTL(pipe);
2750 temp = I915_READ(reg);
2751 temp &= ~(0x7 << 16);
2752 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2753 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2758 /* Ironlake workaround, disable clock pointer after downing FDI */
2759 if (HAS_PCH_IBX(dev)) {
2760 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2763 /* still set train pattern 1 */
2764 reg = FDI_TX_CTL(pipe);
2765 temp = I915_READ(reg);
2766 temp &= ~FDI_LINK_TRAIN_NONE;
2767 temp |= FDI_LINK_TRAIN_PATTERN_1;
2768 I915_WRITE(reg, temp);
2770 reg = FDI_RX_CTL(pipe);
2771 temp = I915_READ(reg);
2772 if (HAS_PCH_CPT(dev)) {
2773 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2774 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2776 temp &= ~FDI_LINK_TRAIN_NONE;
2777 temp |= FDI_LINK_TRAIN_PATTERN_1;
2779 /* BPC in FDI rx is consistent with that in PIPECONF */
2780 temp &= ~(0x07 << 16);
2781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2782 I915_WRITE(reg, temp);
2788 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2790 struct drm_device *dev = crtc->dev;
2791 struct drm_i915_private *dev_priv = dev->dev_private;
2792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2793 unsigned long flags;
2796 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2797 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2800 spin_lock_irqsave(&dev->event_lock, flags);
2801 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2802 spin_unlock_irqrestore(&dev->event_lock, flags);
2807 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2809 struct drm_device *dev = crtc->dev;
2810 struct drm_i915_private *dev_priv = dev->dev_private;
2812 if (crtc->fb == NULL)
2815 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2817 wait_event(dev_priv->pending_flip_queue,
2818 !intel_crtc_has_pending_flip(crtc));
2820 mutex_lock(&dev->struct_mutex);
2821 intel_finish_fb(crtc->fb);
2822 mutex_unlock(&dev->struct_mutex);
2825 /* Program iCLKIP clock to the desired frequency */
2826 static void lpt_program_iclkip(struct drm_crtc *crtc)
2828 struct drm_device *dev = crtc->dev;
2829 struct drm_i915_private *dev_priv = dev->dev_private;
2830 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2833 mutex_lock(&dev_priv->dpio_lock);
2835 /* It is necessary to ungate the pixclk gate prior to programming
2836 * the divisors, and gate it back when it is done.
2838 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2840 /* Disable SSCCTL */
2841 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2842 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2846 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2847 if (crtc->mode.clock == 20000) {
2852 /* The iCLK virtual clock root frequency is in MHz,
2853 * but the crtc->mode.clock in in KHz. To get the divisors,
2854 * it is necessary to divide one by another, so we
2855 * convert the virtual clock precision to KHz here for higher
2858 u32 iclk_virtual_root_freq = 172800 * 1000;
2859 u32 iclk_pi_range = 64;
2860 u32 desired_divisor, msb_divisor_value, pi_value;
2862 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2863 msb_divisor_value = desired_divisor / iclk_pi_range;
2864 pi_value = desired_divisor % iclk_pi_range;
2867 divsel = msb_divisor_value - 2;
2868 phaseinc = pi_value;
2871 /* This should not happen with any sane values */
2872 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2873 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2874 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2875 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2877 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2884 /* Program SSCDIVINTPHASE6 */
2885 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2886 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2887 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2888 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2889 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2890 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2891 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2892 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2894 /* Program SSCAUXDIV */
2895 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2896 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2897 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2898 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2900 /* Enable modulator and associated divider */
2901 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2902 temp &= ~SBI_SSCCTL_DISABLE;
2903 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2905 /* Wait for initialization time */
2908 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2910 mutex_unlock(&dev_priv->dpio_lock);
2913 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2914 enum pipe pch_transcoder)
2916 struct drm_device *dev = crtc->base.dev;
2917 struct drm_i915_private *dev_priv = dev->dev_private;
2918 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2920 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2921 I915_READ(HTOTAL(cpu_transcoder)));
2922 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2923 I915_READ(HBLANK(cpu_transcoder)));
2924 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2925 I915_READ(HSYNC(cpu_transcoder)));
2927 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2928 I915_READ(VTOTAL(cpu_transcoder)));
2929 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2930 I915_READ(VBLANK(cpu_transcoder)));
2931 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2932 I915_READ(VSYNC(cpu_transcoder)));
2933 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2934 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2938 * Enable PCH resources required for PCH ports:
2940 * - FDI training & RX/TX
2941 * - update transcoder timings
2942 * - DP transcoding bits
2945 static void ironlake_pch_enable(struct drm_crtc *crtc)
2947 struct drm_device *dev = crtc->dev;
2948 struct drm_i915_private *dev_priv = dev->dev_private;
2949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2950 int pipe = intel_crtc->pipe;
2953 assert_pch_transcoder_disabled(dev_priv, pipe);
2955 /* Write the TU size bits before fdi link training, so that error
2956 * detection works. */
2957 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2958 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2960 /* For PCH output, training FDI link */
2961 dev_priv->display.fdi_link_train(crtc);
2963 /* XXX: pch pll's can be enabled any time before we enable the PCH
2964 * transcoder, and we actually should do this to not upset any PCH
2965 * transcoder that already use the clock when we share it.
2967 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
2968 * unconditionally resets the pll - we need that to have the right LVDS
2969 * enable sequence. */
2970 ironlake_enable_pch_pll(intel_crtc);
2972 if (HAS_PCH_CPT(dev)) {
2975 temp = I915_READ(PCH_DPLL_SEL);
2979 temp |= TRANSA_DPLL_ENABLE;
2980 sel = TRANSA_DPLLB_SEL;
2983 temp |= TRANSB_DPLL_ENABLE;
2984 sel = TRANSB_DPLLB_SEL;
2987 temp |= TRANSC_DPLL_ENABLE;
2988 sel = TRANSC_DPLLB_SEL;
2991 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
2995 I915_WRITE(PCH_DPLL_SEL, temp);
2998 /* set transcoder timing, panel must allow it */
2999 assert_panel_unlocked(dev_priv, pipe);
3000 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3002 intel_fdi_normal_train(crtc);
3004 /* For PCH DP, enable TRANS_DP_CTL */
3005 if (HAS_PCH_CPT(dev) &&
3006 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3007 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3008 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3009 reg = TRANS_DP_CTL(pipe);
3010 temp = I915_READ(reg);
3011 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3012 TRANS_DP_SYNC_MASK |
3014 temp |= (TRANS_DP_OUTPUT_ENABLE |
3015 TRANS_DP_ENH_FRAMING);
3016 temp |= bpc << 9; /* same format but at 11:9 */
3018 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3019 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3020 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3021 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3023 switch (intel_trans_dp_port_sel(crtc)) {
3025 temp |= TRANS_DP_PORT_SEL_B;
3028 temp |= TRANS_DP_PORT_SEL_C;
3031 temp |= TRANS_DP_PORT_SEL_D;
3037 I915_WRITE(reg, temp);
3040 ironlake_enable_pch_transcoder(dev_priv, pipe);
3043 static void lpt_pch_enable(struct drm_crtc *crtc)
3045 struct drm_device *dev = crtc->dev;
3046 struct drm_i915_private *dev_priv = dev->dev_private;
3047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3048 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3050 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3052 lpt_program_iclkip(crtc);
3054 /* Set transcoder timing. */
3055 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3057 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3060 static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3062 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3067 if (pll->refcount == 0) {
3068 WARN(1, "bad PCH PLL refcount\n");
3073 intel_crtc->pch_pll = NULL;
3076 static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3078 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3079 struct intel_pch_pll *pll;
3082 pll = intel_crtc->pch_pll;
3084 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3085 intel_crtc->base.base.id, pll->pll_reg);
3089 if (HAS_PCH_IBX(dev_priv->dev)) {
3090 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3091 i = intel_crtc->pipe;
3092 pll = &dev_priv->pch_plls[i];
3094 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3095 intel_crtc->base.base.id, pll->pll_reg);
3100 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3101 pll = &dev_priv->pch_plls[i];
3103 /* Only want to check enabled timings first */
3104 if (pll->refcount == 0)
3107 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3108 fp == I915_READ(pll->fp0_reg)) {
3109 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3110 intel_crtc->base.base.id,
3111 pll->pll_reg, pll->refcount, pll->active);
3117 /* Ok no matching timings, maybe there's a free one? */
3118 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3119 pll = &dev_priv->pch_plls[i];
3120 if (pll->refcount == 0) {
3121 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3122 intel_crtc->base.base.id, pll->pll_reg);
3130 intel_crtc->pch_pll = pll;
3132 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
3133 prepare: /* separate function? */
3134 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
3136 /* Wait for the clocks to stabilize before rewriting the regs */
3137 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3138 POSTING_READ(pll->pll_reg);
3141 I915_WRITE(pll->fp0_reg, fp);
3142 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3147 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3149 struct drm_i915_private *dev_priv = dev->dev_private;
3150 int dslreg = PIPEDSL(pipe);
3153 temp = I915_READ(dslreg);
3155 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3156 if (wait_for(I915_READ(dslreg) != temp, 5))
3157 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3161 static void ironlake_pfit_enable(struct intel_crtc *crtc)
3163 struct drm_device *dev = crtc->base.dev;
3164 struct drm_i915_private *dev_priv = dev->dev_private;
3165 int pipe = crtc->pipe;
3167 if (crtc->config.pch_pfit.size) {
3168 /* Force use of hard-coded filter coefficients
3169 * as some pre-programmed values are broken,
3172 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3173 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3174 PF_PIPE_SEL_IVB(pipe));
3176 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3177 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3178 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3182 static void intel_enable_planes(struct drm_crtc *crtc)
3184 struct drm_device *dev = crtc->dev;
3185 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3186 struct intel_plane *intel_plane;
3188 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3189 if (intel_plane->pipe == pipe)
3190 intel_plane_restore(&intel_plane->base);
3193 static void intel_disable_planes(struct drm_crtc *crtc)
3195 struct drm_device *dev = crtc->dev;
3196 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3197 struct intel_plane *intel_plane;
3199 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3200 if (intel_plane->pipe == pipe)
3201 intel_plane_disable(&intel_plane->base);
3204 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3206 struct drm_device *dev = crtc->dev;
3207 struct drm_i915_private *dev_priv = dev->dev_private;
3208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3209 struct intel_encoder *encoder;
3210 int pipe = intel_crtc->pipe;
3211 int plane = intel_crtc->plane;
3214 WARN_ON(!crtc->enabled);
3216 if (intel_crtc->active)
3219 intel_crtc->active = true;
3221 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3222 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3224 intel_update_watermarks(dev);
3226 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3227 temp = I915_READ(PCH_LVDS);
3228 if ((temp & LVDS_PORT_EN) == 0)
3229 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3233 if (intel_crtc->config.has_pch_encoder) {
3234 /* Note: FDI PLL enabling _must_ be done before we enable the
3235 * cpu pipes, hence this is separate from all the other fdi/pch
3237 ironlake_fdi_pll_enable(intel_crtc);
3239 assert_fdi_tx_disabled(dev_priv, pipe);
3240 assert_fdi_rx_disabled(dev_priv, pipe);
3243 for_each_encoder_on_crtc(dev, crtc, encoder)
3244 if (encoder->pre_enable)
3245 encoder->pre_enable(encoder);
3247 /* Enable panel fitting for LVDS */
3248 ironlake_pfit_enable(intel_crtc);
3251 * On ILK+ LUT must be loaded before the pipe is running but with
3254 intel_crtc_load_lut(crtc);
3256 intel_enable_pipe(dev_priv, pipe,
3257 intel_crtc->config.has_pch_encoder);
3258 intel_enable_plane(dev_priv, plane, pipe);
3259 intel_enable_planes(crtc);
3260 intel_crtc_update_cursor(crtc, true);
3262 if (intel_crtc->config.has_pch_encoder)
3263 ironlake_pch_enable(crtc);
3265 mutex_lock(&dev->struct_mutex);
3266 intel_update_fbc(dev);
3267 mutex_unlock(&dev->struct_mutex);
3269 for_each_encoder_on_crtc(dev, crtc, encoder)
3270 encoder->enable(encoder);
3272 if (HAS_PCH_CPT(dev))
3273 cpt_verify_modeset(dev, intel_crtc->pipe);
3276 * There seems to be a race in PCH platform hw (at least on some
3277 * outputs) where an enabled pipe still completes any pageflip right
3278 * away (as if the pipe is off) instead of waiting for vblank. As soon
3279 * as the first vblank happend, everything works as expected. Hence just
3280 * wait for one vblank before returning to avoid strange things
3283 intel_wait_for_vblank(dev, intel_crtc->pipe);
3286 /* IPS only exists on ULT machines and is tied to pipe A. */
3287 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3289 return IS_ULT(crtc->base.dev) && crtc->pipe == PIPE_A;
3292 static void hsw_enable_ips(struct intel_crtc *crtc)
3294 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3296 if (!crtc->config.ips_enabled)
3299 /* We can only enable IPS after we enable a plane and wait for a vblank.
3300 * We guarantee that the plane is enabled by calling intel_enable_ips
3301 * only after intel_enable_plane. And intel_enable_plane already waits
3302 * for a vblank, so all we need to do here is to enable the IPS bit. */
3303 assert_plane_enabled(dev_priv, crtc->plane);
3304 I915_WRITE(IPS_CTL, IPS_ENABLE);
3307 static void hsw_disable_ips(struct intel_crtc *crtc)
3309 struct drm_device *dev = crtc->base.dev;
3310 struct drm_i915_private *dev_priv = dev->dev_private;
3312 if (!crtc->config.ips_enabled)
3315 assert_plane_enabled(dev_priv, crtc->plane);
3316 I915_WRITE(IPS_CTL, 0);
3318 /* We need to wait for a vblank before we can disable the plane. */
3319 intel_wait_for_vblank(dev, crtc->pipe);
3322 static void haswell_crtc_enable(struct drm_crtc *crtc)
3324 struct drm_device *dev = crtc->dev;
3325 struct drm_i915_private *dev_priv = dev->dev_private;
3326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3327 struct intel_encoder *encoder;
3328 int pipe = intel_crtc->pipe;
3329 int plane = intel_crtc->plane;
3331 WARN_ON(!crtc->enabled);
3333 if (intel_crtc->active)
3336 intel_crtc->active = true;
3338 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3339 if (intel_crtc->config.has_pch_encoder)
3340 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3342 intel_update_watermarks(dev);
3344 if (intel_crtc->config.has_pch_encoder)
3345 dev_priv->display.fdi_link_train(crtc);
3347 for_each_encoder_on_crtc(dev, crtc, encoder)
3348 if (encoder->pre_enable)
3349 encoder->pre_enable(encoder);
3351 intel_ddi_enable_pipe_clock(intel_crtc);
3353 /* Enable panel fitting for eDP */
3354 ironlake_pfit_enable(intel_crtc);
3357 * On ILK+ LUT must be loaded before the pipe is running but with
3360 intel_crtc_load_lut(crtc);
3362 intel_ddi_set_pipe_settings(crtc);
3363 intel_ddi_enable_transcoder_func(crtc);
3365 intel_enable_pipe(dev_priv, pipe,
3366 intel_crtc->config.has_pch_encoder);
3367 intel_enable_plane(dev_priv, plane, pipe);
3368 intel_enable_planes(crtc);
3369 intel_crtc_update_cursor(crtc, true);
3371 hsw_enable_ips(intel_crtc);
3373 if (intel_crtc->config.has_pch_encoder)
3374 lpt_pch_enable(crtc);
3376 mutex_lock(&dev->struct_mutex);
3377 intel_update_fbc(dev);
3378 mutex_unlock(&dev->struct_mutex);
3380 for_each_encoder_on_crtc(dev, crtc, encoder)
3381 encoder->enable(encoder);
3384 * There seems to be a race in PCH platform hw (at least on some
3385 * outputs) where an enabled pipe still completes any pageflip right
3386 * away (as if the pipe is off) instead of waiting for vblank. As soon
3387 * as the first vblank happend, everything works as expected. Hence just
3388 * wait for one vblank before returning to avoid strange things
3391 intel_wait_for_vblank(dev, intel_crtc->pipe);
3394 static void ironlake_pfit_disable(struct intel_crtc *crtc)
3396 struct drm_device *dev = crtc->base.dev;
3397 struct drm_i915_private *dev_priv = dev->dev_private;
3398 int pipe = crtc->pipe;
3400 /* To avoid upsetting the power well on haswell only disable the pfit if
3401 * it's in use. The hw state code will make sure we get this right. */
3402 if (crtc->config.pch_pfit.size) {
3403 I915_WRITE(PF_CTL(pipe), 0);
3404 I915_WRITE(PF_WIN_POS(pipe), 0);
3405 I915_WRITE(PF_WIN_SZ(pipe), 0);
3409 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3411 struct drm_device *dev = crtc->dev;
3412 struct drm_i915_private *dev_priv = dev->dev_private;
3413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3414 struct intel_encoder *encoder;
3415 int pipe = intel_crtc->pipe;
3416 int plane = intel_crtc->plane;
3420 if (!intel_crtc->active)
3423 for_each_encoder_on_crtc(dev, crtc, encoder)
3424 encoder->disable(encoder);
3426 intel_crtc_wait_for_pending_flips(crtc);
3427 drm_vblank_off(dev, pipe);
3429 if (dev_priv->cfb_plane == plane)
3430 intel_disable_fbc(dev);
3432 intel_crtc_update_cursor(crtc, false);
3433 intel_disable_planes(crtc);
3434 intel_disable_plane(dev_priv, plane, pipe);
3436 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3437 intel_disable_pipe(dev_priv, pipe);
3439 ironlake_pfit_disable(intel_crtc);
3441 for_each_encoder_on_crtc(dev, crtc, encoder)
3442 if (encoder->post_disable)
3443 encoder->post_disable(encoder);
3445 ironlake_fdi_disable(crtc);
3447 ironlake_disable_pch_transcoder(dev_priv, pipe);
3448 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3450 if (HAS_PCH_CPT(dev)) {
3451 /* disable TRANS_DP_CTL */
3452 reg = TRANS_DP_CTL(pipe);
3453 temp = I915_READ(reg);
3454 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3455 temp |= TRANS_DP_PORT_SEL_NONE;
3456 I915_WRITE(reg, temp);
3458 /* disable DPLL_SEL */
3459 temp = I915_READ(PCH_DPLL_SEL);
3462 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3465 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3468 /* C shares PLL A or B */
3469 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3474 I915_WRITE(PCH_DPLL_SEL, temp);
3477 /* disable PCH DPLL */
3478 intel_disable_pch_pll(intel_crtc);
3480 ironlake_fdi_pll_disable(intel_crtc);
3482 intel_crtc->active = false;
3483 intel_update_watermarks(dev);
3485 mutex_lock(&dev->struct_mutex);
3486 intel_update_fbc(dev);
3487 mutex_unlock(&dev->struct_mutex);
3490 static void haswell_crtc_disable(struct drm_crtc *crtc)
3492 struct drm_device *dev = crtc->dev;
3493 struct drm_i915_private *dev_priv = dev->dev_private;
3494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3495 struct intel_encoder *encoder;
3496 int pipe = intel_crtc->pipe;
3497 int plane = intel_crtc->plane;
3498 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3500 if (!intel_crtc->active)
3503 for_each_encoder_on_crtc(dev, crtc, encoder)
3504 encoder->disable(encoder);
3506 intel_crtc_wait_for_pending_flips(crtc);
3507 drm_vblank_off(dev, pipe);
3509 /* FBC must be disabled before disabling the plane on HSW. */
3510 if (dev_priv->cfb_plane == plane)
3511 intel_disable_fbc(dev);
3513 hsw_disable_ips(intel_crtc);
3515 intel_crtc_update_cursor(crtc, false);
3516 intel_disable_planes(crtc);
3517 intel_disable_plane(dev_priv, plane, pipe);
3519 if (intel_crtc->config.has_pch_encoder)
3520 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
3521 intel_disable_pipe(dev_priv, pipe);
3523 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3525 ironlake_pfit_disable(intel_crtc);
3527 intel_ddi_disable_pipe_clock(intel_crtc);
3529 for_each_encoder_on_crtc(dev, crtc, encoder)
3530 if (encoder->post_disable)
3531 encoder->post_disable(encoder);
3533 if (intel_crtc->config.has_pch_encoder) {
3534 lpt_disable_pch_transcoder(dev_priv);
3535 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3536 intel_ddi_fdi_disable(crtc);
3539 intel_crtc->active = false;
3540 intel_update_watermarks(dev);
3542 mutex_lock(&dev->struct_mutex);
3543 intel_update_fbc(dev);
3544 mutex_unlock(&dev->struct_mutex);
3547 static void ironlake_crtc_off(struct drm_crtc *crtc)
3549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3550 intel_put_pch_pll(intel_crtc);
3553 static void haswell_crtc_off(struct drm_crtc *crtc)
3555 intel_ddi_put_crtc_pll(crtc);
3558 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3560 if (!enable && intel_crtc->overlay) {
3561 struct drm_device *dev = intel_crtc->base.dev;
3562 struct drm_i915_private *dev_priv = dev->dev_private;
3564 mutex_lock(&dev->struct_mutex);
3565 dev_priv->mm.interruptible = false;
3566 (void) intel_overlay_switch_off(intel_crtc->overlay);
3567 dev_priv->mm.interruptible = true;
3568 mutex_unlock(&dev->struct_mutex);
3571 /* Let userspace switch the overlay on again. In most cases userspace
3572 * has to recompute where to put it anyway.
3577 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3578 * cursor plane briefly if not already running after enabling the display
3580 * This workaround avoids occasional blank screens when self refresh is
3584 g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3586 u32 cntl = I915_READ(CURCNTR(pipe));
3588 if ((cntl & CURSOR_MODE) == 0) {
3589 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3591 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3592 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3593 intel_wait_for_vblank(dev_priv->dev, pipe);
3594 I915_WRITE(CURCNTR(pipe), cntl);
3595 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3596 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3600 static void i9xx_pfit_enable(struct intel_crtc *crtc)
3602 struct drm_device *dev = crtc->base.dev;
3603 struct drm_i915_private *dev_priv = dev->dev_private;
3604 struct intel_crtc_config *pipe_config = &crtc->config;
3606 if (!crtc->config.gmch_pfit.control)
3610 * The panel fitter should only be adjusted whilst the pipe is disabled,
3611 * according to register description and PRM.
3613 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3614 assert_pipe_disabled(dev_priv, crtc->pipe);
3616 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3617 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
3619 /* Border color in case we don't scale up to the full screen. Black by
3620 * default, change to something else for debugging. */
3621 I915_WRITE(BCLRPAT(crtc->pipe), 0);
3624 static void valleyview_crtc_enable(struct drm_crtc *crtc)
3626 struct drm_device *dev = crtc->dev;
3627 struct drm_i915_private *dev_priv = dev->dev_private;
3628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3629 struct intel_encoder *encoder;
3630 int pipe = intel_crtc->pipe;
3631 int plane = intel_crtc->plane;
3633 WARN_ON(!crtc->enabled);
3635 if (intel_crtc->active)
3638 intel_crtc->active = true;
3639 intel_update_watermarks(dev);
3641 mutex_lock(&dev_priv->dpio_lock);
3643 for_each_encoder_on_crtc(dev, crtc, encoder)
3644 if (encoder->pre_pll_enable)
3645 encoder->pre_pll_enable(encoder);
3647 intel_enable_pll(dev_priv, pipe);
3649 for_each_encoder_on_crtc(dev, crtc, encoder)
3650 if (encoder->pre_enable)
3651 encoder->pre_enable(encoder);
3653 /* VLV wants encoder enabling _before_ the pipe is up. */
3654 for_each_encoder_on_crtc(dev, crtc, encoder)
3655 encoder->enable(encoder);
3657 /* Enable panel fitting for eDP */
3658 i9xx_pfit_enable(intel_crtc);
3660 intel_crtc_load_lut(crtc);
3662 intel_enable_pipe(dev_priv, pipe, false);
3663 intel_enable_plane(dev_priv, plane, pipe);
3664 intel_enable_planes(crtc);
3665 intel_crtc_update_cursor(crtc, true);
3667 intel_update_fbc(dev);
3669 mutex_unlock(&dev_priv->dpio_lock);
3672 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3674 struct drm_device *dev = crtc->dev;
3675 struct drm_i915_private *dev_priv = dev->dev_private;
3676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3677 struct intel_encoder *encoder;
3678 int pipe = intel_crtc->pipe;
3679 int plane = intel_crtc->plane;
3681 WARN_ON(!crtc->enabled);
3683 if (intel_crtc->active)
3686 intel_crtc->active = true;
3687 intel_update_watermarks(dev);
3689 intel_enable_pll(dev_priv, pipe);
3691 for_each_encoder_on_crtc(dev, crtc, encoder)
3692 if (encoder->pre_enable)
3693 encoder->pre_enable(encoder);
3695 /* Enable panel fitting for LVDS */
3696 i9xx_pfit_enable(intel_crtc);
3698 intel_crtc_load_lut(crtc);
3700 intel_enable_pipe(dev_priv, pipe, false);
3701 intel_enable_plane(dev_priv, plane, pipe);
3702 intel_enable_planes(crtc);
3703 intel_crtc_update_cursor(crtc, true);
3705 g4x_fixup_plane(dev_priv, pipe);
3707 /* Give the overlay scaler a chance to enable if it's on this pipe */
3708 intel_crtc_dpms_overlay(intel_crtc, true);
3710 intel_update_fbc(dev);
3712 for_each_encoder_on_crtc(dev, crtc, encoder)
3713 encoder->enable(encoder);
3716 static void i9xx_pfit_disable(struct intel_crtc *crtc)
3718 struct drm_device *dev = crtc->base.dev;
3719 struct drm_i915_private *dev_priv = dev->dev_private;
3721 if (!crtc->config.gmch_pfit.control)
3724 assert_pipe_disabled(dev_priv, crtc->pipe);
3726 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3727 I915_READ(PFIT_CONTROL));
3728 I915_WRITE(PFIT_CONTROL, 0);
3731 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3733 struct drm_device *dev = crtc->dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3736 struct intel_encoder *encoder;
3737 int pipe = intel_crtc->pipe;
3738 int plane = intel_crtc->plane;
3740 if (!intel_crtc->active)
3743 for_each_encoder_on_crtc(dev, crtc, encoder)
3744 encoder->disable(encoder);
3746 /* Give the overlay scaler a chance to disable if it's on this pipe */
3747 intel_crtc_wait_for_pending_flips(crtc);
3748 drm_vblank_off(dev, pipe);
3750 if (dev_priv->cfb_plane == plane)
3751 intel_disable_fbc(dev);
3753 intel_crtc_dpms_overlay(intel_crtc, false);
3754 intel_crtc_update_cursor(crtc, false);
3755 intel_disable_planes(crtc);
3756 intel_disable_plane(dev_priv, plane, pipe);
3758 intel_disable_pipe(dev_priv, pipe);
3760 i9xx_pfit_disable(intel_crtc);
3762 for_each_encoder_on_crtc(dev, crtc, encoder)
3763 if (encoder->post_disable)
3764 encoder->post_disable(encoder);
3766 intel_disable_pll(dev_priv, pipe);
3768 intel_crtc->active = false;
3769 intel_update_fbc(dev);
3770 intel_update_watermarks(dev);
3773 static void i9xx_crtc_off(struct drm_crtc *crtc)
3777 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3780 struct drm_device *dev = crtc->dev;
3781 struct drm_i915_master_private *master_priv;
3782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3783 int pipe = intel_crtc->pipe;
3785 if (!dev->primary->master)
3788 master_priv = dev->primary->master->driver_priv;
3789 if (!master_priv->sarea_priv)
3794 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3795 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3798 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3799 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3802 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3808 * Sets the power management mode of the pipe and plane.
3810 void intel_crtc_update_dpms(struct drm_crtc *crtc)
3812 struct drm_device *dev = crtc->dev;
3813 struct drm_i915_private *dev_priv = dev->dev_private;
3814 struct intel_encoder *intel_encoder;
3815 bool enable = false;
3817 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3818 enable |= intel_encoder->connectors_active;
3821 dev_priv->display.crtc_enable(crtc);
3823 dev_priv->display.crtc_disable(crtc);
3825 intel_crtc_update_sarea(crtc, enable);
3828 static void intel_crtc_disable(struct drm_crtc *crtc)
3830 struct drm_device *dev = crtc->dev;
3831 struct drm_connector *connector;
3832 struct drm_i915_private *dev_priv = dev->dev_private;
3833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3835 /* crtc should still be enabled when we disable it. */
3836 WARN_ON(!crtc->enabled);
3838 dev_priv->display.crtc_disable(crtc);
3839 intel_crtc->eld_vld = false;
3840 intel_crtc_update_sarea(crtc, false);
3841 dev_priv->display.off(crtc);
3843 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3844 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3847 mutex_lock(&dev->struct_mutex);
3848 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3849 mutex_unlock(&dev->struct_mutex);
3853 /* Update computed state. */
3854 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3855 if (!connector->encoder || !connector->encoder->crtc)
3858 if (connector->encoder->crtc != crtc)
3861 connector->dpms = DRM_MODE_DPMS_OFF;
3862 to_intel_encoder(connector->encoder)->connectors_active = false;
3866 void intel_modeset_disable(struct drm_device *dev)
3868 struct drm_crtc *crtc;
3870 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3872 intel_crtc_disable(crtc);
3876 void intel_encoder_destroy(struct drm_encoder *encoder)
3878 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3880 drm_encoder_cleanup(encoder);
3881 kfree(intel_encoder);
3884 /* Simple dpms helper for encodres with just one connector, no cloning and only
3885 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3886 * state of the entire output pipe. */
3887 void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3889 if (mode == DRM_MODE_DPMS_ON) {
3890 encoder->connectors_active = true;
3892 intel_crtc_update_dpms(encoder->base.crtc);
3894 encoder->connectors_active = false;
3896 intel_crtc_update_dpms(encoder->base.crtc);
3900 /* Cross check the actual hw state with our own modeset state tracking (and it's
3901 * internal consistency). */
3902 static void intel_connector_check_state(struct intel_connector *connector)
3904 if (connector->get_hw_state(connector)) {
3905 struct intel_encoder *encoder = connector->encoder;
3906 struct drm_crtc *crtc;
3907 bool encoder_enabled;
3910 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3911 connector->base.base.id,
3912 drm_get_connector_name(&connector->base));
3914 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3915 "wrong connector dpms state\n");
3916 WARN(connector->base.encoder != &encoder->base,
3917 "active connector not linked to encoder\n");
3918 WARN(!encoder->connectors_active,
3919 "encoder->connectors_active not set\n");
3921 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3922 WARN(!encoder_enabled, "encoder not enabled\n");
3923 if (WARN_ON(!encoder->base.crtc))
3926 crtc = encoder->base.crtc;
3928 WARN(!crtc->enabled, "crtc not enabled\n");
3929 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3930 WARN(pipe != to_intel_crtc(crtc)->pipe,
3931 "encoder active on the wrong pipe\n");
3935 /* Even simpler default implementation, if there's really no special case to
3937 void intel_connector_dpms(struct drm_connector *connector, int mode)
3939 struct intel_encoder *encoder = intel_attached_encoder(connector);
3941 /* All the simple cases only support two dpms states. */
3942 if (mode != DRM_MODE_DPMS_ON)
3943 mode = DRM_MODE_DPMS_OFF;
3945 if (mode == connector->dpms)
3948 connector->dpms = mode;
3950 /* Only need to change hw state when actually enabled */
3951 if (encoder->base.crtc)
3952 intel_encoder_dpms(encoder, mode);
3954 WARN_ON(encoder->connectors_active != false);
3956 intel_modeset_check_state(connector->dev);
3959 /* Simple connector->get_hw_state implementation for encoders that support only
3960 * one connector and no cloning and hence the encoder state determines the state
3961 * of the connector. */
3962 bool intel_connector_get_hw_state(struct intel_connector *connector)
3965 struct intel_encoder *encoder = connector->encoder;
3967 return encoder->get_hw_state(encoder, &pipe);
3970 static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3971 struct intel_crtc_config *pipe_config)
3973 struct drm_i915_private *dev_priv = dev->dev_private;
3974 struct intel_crtc *pipe_B_crtc =
3975 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3977 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3978 pipe_name(pipe), pipe_config->fdi_lanes);
3979 if (pipe_config->fdi_lanes > 4) {
3980 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3981 pipe_name(pipe), pipe_config->fdi_lanes);
3985 if (IS_HASWELL(dev)) {
3986 if (pipe_config->fdi_lanes > 2) {
3987 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
3988 pipe_config->fdi_lanes);
3995 if (INTEL_INFO(dev)->num_pipes == 2)
3998 /* Ivybridge 3 pipe is really complicated */
4003 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4004 pipe_config->fdi_lanes > 2) {
4005 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4006 pipe_name(pipe), pipe_config->fdi_lanes);
4011 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
4012 pipe_B_crtc->config.fdi_lanes <= 2) {
4013 if (pipe_config->fdi_lanes > 2) {
4014 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4015 pipe_name(pipe), pipe_config->fdi_lanes);
4019 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4029 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4030 struct intel_crtc_config *pipe_config)
4032 struct drm_device *dev = intel_crtc->base.dev;
4033 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4034 int lane, link_bw, fdi_dotclock;
4035 bool setup_ok, needs_recompute = false;
4038 /* FDI is a binary signal running at ~2.7GHz, encoding
4039 * each output octet as 10 bits. The actual frequency
4040 * is stored as a divider into a 100MHz clock, and the
4041 * mode pixel clock is stored in units of 1KHz.
4042 * Hence the bw of each lane in terms of the mode signal
4045 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4047 fdi_dotclock = adjusted_mode->clock;
4048 fdi_dotclock /= pipe_config->pixel_multiplier;
4050 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
4051 pipe_config->pipe_bpp);
4053 pipe_config->fdi_lanes = lane;
4055 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
4056 link_bw, &pipe_config->fdi_m_n);
4058 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4059 intel_crtc->pipe, pipe_config);
4060 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4061 pipe_config->pipe_bpp -= 2*3;
4062 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4063 pipe_config->pipe_bpp);
4064 needs_recompute = true;
4065 pipe_config->bw_constrained = true;
4070 if (needs_recompute)
4073 return setup_ok ? 0 : -EINVAL;
4076 static void hsw_compute_ips_config(struct intel_crtc *crtc,
4077 struct intel_crtc_config *pipe_config)
4079 pipe_config->ips_enabled = i915_enable_ips &&
4080 hsw_crtc_supports_ips(crtc) &&
4081 pipe_config->pipe_bpp == 24;
4084 static int intel_crtc_compute_config(struct drm_crtc *crtc,
4085 struct intel_crtc_config *pipe_config)
4087 struct drm_device *dev = crtc->dev;
4088 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
4089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4091 if (HAS_PCH_SPLIT(dev)) {
4092 /* FDI link clock is fixed at 2.7G */
4093 if (pipe_config->requested_mode.clock * 3
4094 > IRONLAKE_FDI_FREQ * 4)
4098 /* All interlaced capable intel hw wants timings in frames. Note though
4099 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4100 * timings, so we need to be careful not to clobber these.*/
4101 if (!pipe_config->timings_set)
4102 drm_mode_set_crtcinfo(adjusted_mode, 0);
4104 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4105 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4107 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4108 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4111 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
4112 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
4113 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
4114 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4116 pipe_config->pipe_bpp = 8*3;
4119 if (IS_HASWELL(dev))
4120 hsw_compute_ips_config(intel_crtc, pipe_config);
4122 if (pipe_config->has_pch_encoder)
4123 return ironlake_fdi_compute_config(intel_crtc, pipe_config);
4128 static int valleyview_get_display_clock_speed(struct drm_device *dev)
4130 return 400000; /* FIXME */
4133 static int i945_get_display_clock_speed(struct drm_device *dev)
4138 static int i915_get_display_clock_speed(struct drm_device *dev)
4143 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4148 static int i915gm_get_display_clock_speed(struct drm_device *dev)
4152 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4154 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4157 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4158 case GC_DISPLAY_CLOCK_333_MHZ:
4161 case GC_DISPLAY_CLOCK_190_200_MHZ:
4167 static int i865_get_display_clock_speed(struct drm_device *dev)
4172 static int i855_get_display_clock_speed(struct drm_device *dev)
4175 /* Assume that the hardware is in the high speed state. This
4176 * should be the default.
4178 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4179 case GC_CLOCK_133_200:
4180 case GC_CLOCK_100_200:
4182 case GC_CLOCK_166_250:
4184 case GC_CLOCK_100_133:
4188 /* Shouldn't happen */
4192 static int i830_get_display_clock_speed(struct drm_device *dev)
4198 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
4200 while (*num > DATA_LINK_M_N_MASK ||
4201 *den > DATA_LINK_M_N_MASK) {
4207 static void compute_m_n(unsigned int m, unsigned int n,
4208 uint32_t *ret_m, uint32_t *ret_n)
4210 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4211 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4212 intel_reduce_m_n_ratio(ret_m, ret_n);
4216 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4217 int pixel_clock, int link_clock,
4218 struct intel_link_m_n *m_n)
4222 compute_m_n(bits_per_pixel * pixel_clock,
4223 link_clock * nlanes * 8,
4224 &m_n->gmch_m, &m_n->gmch_n);
4226 compute_m_n(pixel_clock, link_clock,
4227 &m_n->link_m, &m_n->link_n);
4230 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4232 if (i915_panel_use_ssc >= 0)
4233 return i915_panel_use_ssc != 0;
4234 return dev_priv->vbt.lvds_use_ssc
4235 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4238 static int vlv_get_refclk(struct drm_crtc *crtc)
4240 struct drm_device *dev = crtc->dev;
4241 struct drm_i915_private *dev_priv = dev->dev_private;
4242 int refclk = 27000; /* for DP & HDMI */
4244 return 100000; /* only one validated so far */
4246 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4248 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4249 if (intel_panel_use_ssc(dev_priv))
4253 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4260 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4262 struct drm_device *dev = crtc->dev;
4263 struct drm_i915_private *dev_priv = dev->dev_private;
4266 if (IS_VALLEYVIEW(dev)) {
4267 refclk = vlv_get_refclk(crtc);
4268 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4269 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4270 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
4271 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4273 } else if (!IS_GEN2(dev)) {
4282 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4284 return (1 << dpll->n) << 16 | dpll->m1 << 8 | dpll->m2;
4287 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4289 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4292 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
4293 intel_clock_t *reduced_clock)
4295 struct drm_device *dev = crtc->base.dev;
4296 struct drm_i915_private *dev_priv = dev->dev_private;
4297 int pipe = crtc->pipe;
4300 if (IS_PINEVIEW(dev)) {
4301 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
4303 fp2 = pnv_dpll_compute_fp(reduced_clock);
4305 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
4307 fp2 = i9xx_dpll_compute_fp(reduced_clock);
4310 I915_WRITE(FP0(pipe), fp);
4312 crtc->lowfreq_avail = false;
4313 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4314 reduced_clock && i915_powersave) {
4315 I915_WRITE(FP1(pipe), fp2);
4316 crtc->lowfreq_avail = true;
4318 I915_WRITE(FP1(pipe), fp);
4322 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4327 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4328 * and set it to a reasonable value instead.
4330 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4331 reg_val &= 0xffffff00;
4332 reg_val |= 0x00000030;
4333 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4335 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4336 reg_val &= 0x8cffffff;
4337 reg_val = 0x8c000000;
4338 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4340 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
4341 reg_val &= 0xffffff00;
4342 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4344 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
4345 reg_val &= 0x00ffffff;
4346 reg_val |= 0xb0000000;
4347 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4350 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4351 struct intel_link_m_n *m_n)
4353 struct drm_device *dev = crtc->base.dev;
4354 struct drm_i915_private *dev_priv = dev->dev_private;
4355 int pipe = crtc->pipe;
4357 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4358 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4359 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4360 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4363 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4364 struct intel_link_m_n *m_n)
4366 struct drm_device *dev = crtc->base.dev;
4367 struct drm_i915_private *dev_priv = dev->dev_private;
4368 int pipe = crtc->pipe;
4369 enum transcoder transcoder = crtc->config.cpu_transcoder;
4371 if (INTEL_INFO(dev)->gen >= 5) {
4372 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4373 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4374 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4375 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4377 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4378 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4379 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4380 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
4384 static void intel_dp_set_m_n(struct intel_crtc *crtc)
4386 if (crtc->config.has_pch_encoder)
4387 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4389 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4392 static void vlv_update_pll(struct intel_crtc *crtc)
4394 struct drm_device *dev = crtc->base.dev;
4395 struct drm_i915_private *dev_priv = dev->dev_private;
4396 struct intel_encoder *encoder;
4397 int pipe = crtc->pipe;
4399 u32 bestn, bestm1, bestm2, bestp1, bestp2;
4401 u32 coreclk, reg_val, dpll_md;
4403 mutex_lock(&dev_priv->dpio_lock);
4405 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4407 bestn = crtc->config.dpll.n;
4408 bestm1 = crtc->config.dpll.m1;
4409 bestm2 = crtc->config.dpll.m2;
4410 bestp1 = crtc->config.dpll.p1;
4411 bestp2 = crtc->config.dpll.p2;
4413 /* See eDP HDMI DPIO driver vbios notes doc */
4415 /* PLL B needs special handling */
4417 vlv_pllb_recal_opamp(dev_priv);
4419 /* Set up Tx target for periodic Rcomp update */
4420 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4422 /* Disable target IRef on PLL */
4423 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4424 reg_val &= 0x00ffffff;
4425 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4427 /* Disable fast lock */
4428 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4430 /* Set idtafcrecal before PLL is enabled */
4431 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4432 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4433 mdiv |= ((bestn << DPIO_N_SHIFT));
4434 mdiv |= (1 << DPIO_K_SHIFT);
4437 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4438 * but we don't support that).
4439 * Note: don't use the DAC post divider as it seems unstable.
4441 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4442 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4444 mdiv |= DPIO_ENABLE_CALIBRATION;
4445 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4447 /* Set HBR and RBR LPF coefficients */
4448 if (crtc->config.port_clock == 162000 ||
4449 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4450 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4453 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4456 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4457 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4458 /* Use SSC source */
4460 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4463 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4465 } else { /* HDMI or VGA */
4466 /* Use bend source */
4468 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4471 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4475 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4476 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4477 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4478 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4479 coreclk |= 0x01000000;
4480 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4482 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4484 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4485 if (encoder->pre_pll_enable)
4486 encoder->pre_pll_enable(encoder);
4488 /* Enable DPIO clock input */
4489 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4490 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4492 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
4494 dpll |= DPLL_VCO_ENABLE;
4495 I915_WRITE(DPLL(pipe), dpll);
4496 POSTING_READ(DPLL(pipe));
4499 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4500 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4502 dpll_md = (crtc->config.pixel_multiplier - 1)
4503 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4504 I915_WRITE(DPLL_MD(pipe), dpll_md);
4505 POSTING_READ(DPLL_MD(pipe));
4507 if (crtc->config.has_dp_encoder)
4508 intel_dp_set_m_n(crtc);
4510 mutex_unlock(&dev_priv->dpio_lock);
4513 static void i9xx_update_pll(struct intel_crtc *crtc,
4514 intel_clock_t *reduced_clock,
4517 struct drm_device *dev = crtc->base.dev;
4518 struct drm_i915_private *dev_priv = dev->dev_private;
4519 struct intel_encoder *encoder;
4520 int pipe = crtc->pipe;
4523 struct dpll *clock = &crtc->config.dpll;
4525 i9xx_update_pll_dividers(crtc, reduced_clock);
4527 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4528 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
4530 dpll = DPLL_VGA_MODE_DIS;
4532 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
4533 dpll |= DPLLB_MODE_LVDS;
4535 dpll |= DPLLB_MODE_DAC_SERIAL;
4537 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4538 dpll |= (crtc->config.pixel_multiplier - 1)
4539 << SDVO_MULTIPLIER_SHIFT_HIRES;
4543 dpll |= DPLL_DVO_HIGH_SPEED;
4545 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4546 dpll |= DPLL_DVO_HIGH_SPEED;
4548 /* compute bitmask from p1 value */
4549 if (IS_PINEVIEW(dev))
4550 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4552 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4553 if (IS_G4X(dev) && reduced_clock)
4554 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4556 switch (clock->p2) {
4558 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4561 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4564 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4567 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4570 if (INTEL_INFO(dev)->gen >= 4)
4571 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4573 if (crtc->config.sdvo_tv_clock)
4574 dpll |= PLL_REF_INPUT_TVCLKINBC;
4575 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4576 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4577 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4579 dpll |= PLL_REF_INPUT_DREFCLK;
4581 dpll |= DPLL_VCO_ENABLE;
4582 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4583 POSTING_READ(DPLL(pipe));
4586 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4587 if (encoder->pre_pll_enable)
4588 encoder->pre_pll_enable(encoder);
4590 if (crtc->config.has_dp_encoder)
4591 intel_dp_set_m_n(crtc);
4593 I915_WRITE(DPLL(pipe), dpll);
4595 /* Wait for the clocks to stabilize. */
4596 POSTING_READ(DPLL(pipe));
4599 if (INTEL_INFO(dev)->gen >= 4) {
4600 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4601 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4602 I915_WRITE(DPLL_MD(pipe), dpll_md);
4604 /* The pixel multiplier can only be updated once the
4605 * DPLL is enabled and the clocks are stable.
4607 * So write it again.
4609 I915_WRITE(DPLL(pipe), dpll);
4613 static void i8xx_update_pll(struct intel_crtc *crtc,
4614 intel_clock_t *reduced_clock,
4617 struct drm_device *dev = crtc->base.dev;
4618 struct drm_i915_private *dev_priv = dev->dev_private;
4619 struct intel_encoder *encoder;
4620 int pipe = crtc->pipe;
4622 struct dpll *clock = &crtc->config.dpll;
4624 i9xx_update_pll_dividers(crtc, reduced_clock);
4626 dpll = DPLL_VGA_MODE_DIS;
4628 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
4629 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4632 dpll |= PLL_P1_DIVIDE_BY_TWO;
4634 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4636 dpll |= PLL_P2_DIVIDE_BY_4;
4639 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4640 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4641 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4643 dpll |= PLL_REF_INPUT_DREFCLK;
4645 dpll |= DPLL_VCO_ENABLE;
4646 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4647 POSTING_READ(DPLL(pipe));
4650 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4651 if (encoder->pre_pll_enable)
4652 encoder->pre_pll_enable(encoder);
4654 I915_WRITE(DPLL(pipe), dpll);
4656 /* Wait for the clocks to stabilize. */
4657 POSTING_READ(DPLL(pipe));
4660 /* The pixel multiplier can only be updated once the
4661 * DPLL is enabled and the clocks are stable.
4663 * So write it again.
4665 I915_WRITE(DPLL(pipe), dpll);
4668 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
4670 struct drm_device *dev = intel_crtc->base.dev;
4671 struct drm_i915_private *dev_priv = dev->dev_private;
4672 enum pipe pipe = intel_crtc->pipe;
4673 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4674 struct drm_display_mode *adjusted_mode =
4675 &intel_crtc->config.adjusted_mode;
4676 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4677 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4679 /* We need to be careful not to changed the adjusted mode, for otherwise
4680 * the hw state checker will get angry at the mismatch. */
4681 crtc_vtotal = adjusted_mode->crtc_vtotal;
4682 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4684 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4685 /* the chip adds 2 halflines automatically */
4687 crtc_vblank_end -= 1;
4688 vsyncshift = adjusted_mode->crtc_hsync_start
4689 - adjusted_mode->crtc_htotal / 2;
4694 if (INTEL_INFO(dev)->gen > 3)
4695 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4697 I915_WRITE(HTOTAL(cpu_transcoder),
4698 (adjusted_mode->crtc_hdisplay - 1) |
4699 ((adjusted_mode->crtc_htotal - 1) << 16));
4700 I915_WRITE(HBLANK(cpu_transcoder),
4701 (adjusted_mode->crtc_hblank_start - 1) |
4702 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4703 I915_WRITE(HSYNC(cpu_transcoder),
4704 (adjusted_mode->crtc_hsync_start - 1) |
4705 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4707 I915_WRITE(VTOTAL(cpu_transcoder),
4708 (adjusted_mode->crtc_vdisplay - 1) |
4709 ((crtc_vtotal - 1) << 16));
4710 I915_WRITE(VBLANK(cpu_transcoder),
4711 (adjusted_mode->crtc_vblank_start - 1) |
4712 ((crtc_vblank_end - 1) << 16));
4713 I915_WRITE(VSYNC(cpu_transcoder),
4714 (adjusted_mode->crtc_vsync_start - 1) |
4715 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4717 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4718 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4719 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4721 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4722 (pipe == PIPE_B || pipe == PIPE_C))
4723 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4725 /* pipesrc controls the size that is scaled from, which should
4726 * always be the user's requested size.
4728 I915_WRITE(PIPESRC(pipe),
4729 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4732 static void intel_get_pipe_timings(struct intel_crtc *crtc,
4733 struct intel_crtc_config *pipe_config)
4735 struct drm_device *dev = crtc->base.dev;
4736 struct drm_i915_private *dev_priv = dev->dev_private;
4737 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4740 tmp = I915_READ(HTOTAL(cpu_transcoder));
4741 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4742 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4743 tmp = I915_READ(HBLANK(cpu_transcoder));
4744 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4745 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4746 tmp = I915_READ(HSYNC(cpu_transcoder));
4747 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4748 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4750 tmp = I915_READ(VTOTAL(cpu_transcoder));
4751 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4752 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4753 tmp = I915_READ(VBLANK(cpu_transcoder));
4754 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4755 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4756 tmp = I915_READ(VSYNC(cpu_transcoder));
4757 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4758 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4760 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4761 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4762 pipe_config->adjusted_mode.crtc_vtotal += 1;
4763 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4766 tmp = I915_READ(PIPESRC(crtc->pipe));
4767 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4768 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4771 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4773 struct drm_device *dev = intel_crtc->base.dev;
4774 struct drm_i915_private *dev_priv = dev->dev_private;
4777 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4779 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4780 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4783 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4786 if (intel_crtc->config.requested_mode.clock >
4787 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4788 pipeconf |= PIPECONF_DOUBLE_WIDE;
4790 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4793 /* only g4x and later have fancy bpc/dither controls */
4794 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4795 pipeconf &= ~(PIPECONF_BPC_MASK |
4796 PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4798 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4799 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4800 pipeconf |= PIPECONF_DITHER_EN |
4801 PIPECONF_DITHER_TYPE_SP;
4803 switch (intel_crtc->config.pipe_bpp) {
4805 pipeconf |= PIPECONF_6BPC;
4808 pipeconf |= PIPECONF_8BPC;
4811 pipeconf |= PIPECONF_10BPC;
4814 /* Case prevented by intel_choose_pipe_bpp_dither. */
4819 if (HAS_PIPE_CXSR(dev)) {
4820 if (intel_crtc->lowfreq_avail) {
4821 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4822 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4824 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4825 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4829 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4830 if (!IS_GEN2(dev) &&
4831 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4832 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4834 pipeconf |= PIPECONF_PROGRESSIVE;
4836 if (IS_VALLEYVIEW(dev)) {
4837 if (intel_crtc->config.limited_color_range)
4838 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4840 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4843 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4844 POSTING_READ(PIPECONF(intel_crtc->pipe));
4847 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4849 struct drm_framebuffer *fb)
4851 struct drm_device *dev = crtc->dev;
4852 struct drm_i915_private *dev_priv = dev->dev_private;
4853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4854 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
4855 int pipe = intel_crtc->pipe;
4856 int plane = intel_crtc->plane;
4857 int refclk, num_connectors = 0;
4858 intel_clock_t clock, reduced_clock;
4860 bool ok, has_reduced_clock = false;
4861 bool is_lvds = false;
4862 struct intel_encoder *encoder;
4863 const intel_limit_t *limit;
4866 for_each_encoder_on_crtc(dev, crtc, encoder) {
4867 switch (encoder->type) {
4868 case INTEL_OUTPUT_LVDS:
4876 refclk = i9xx_get_refclk(crtc, num_connectors);
4879 * Returns a set of divisors for the desired target clock with the given
4880 * refclk, or FALSE. The returned values represent the clock equation:
4881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4883 limit = intel_limit(crtc, refclk);
4884 ok = dev_priv->display.find_dpll(limit, crtc,
4885 intel_crtc->config.port_clock,
4886 refclk, NULL, &clock);
4887 if (!ok && !intel_crtc->config.clock_set) {
4888 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4892 /* Ensure that the cursor is valid for the new mode before changing... */
4893 intel_crtc_update_cursor(crtc, true);
4895 if (is_lvds && dev_priv->lvds_downclock_avail) {
4897 * Ensure we match the reduced clock's P to the target clock.
4898 * If the clocks don't match, we can't switch the display clock
4899 * by using the FP0/FP1. In such case we will disable the LVDS
4900 * downclock feature.
4903 dev_priv->display.find_dpll(limit, crtc,
4904 dev_priv->lvds_downclock,
4908 /* Compat-code for transition, will disappear. */
4909 if (!intel_crtc->config.clock_set) {
4910 intel_crtc->config.dpll.n = clock.n;
4911 intel_crtc->config.dpll.m1 = clock.m1;
4912 intel_crtc->config.dpll.m2 = clock.m2;
4913 intel_crtc->config.dpll.p1 = clock.p1;
4914 intel_crtc->config.dpll.p2 = clock.p2;
4918 i8xx_update_pll(intel_crtc,
4919 has_reduced_clock ? &reduced_clock : NULL,
4921 else if (IS_VALLEYVIEW(dev))
4922 vlv_update_pll(intel_crtc);
4924 i9xx_update_pll(intel_crtc,
4925 has_reduced_clock ? &reduced_clock : NULL,
4928 /* Set up the display plane register */
4929 dspcntr = DISPPLANE_GAMMA_ENABLE;
4931 if (!IS_VALLEYVIEW(dev)) {
4933 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4935 dspcntr |= DISPPLANE_SEL_PIPE_B;
4938 intel_set_pipe_timings(intel_crtc);
4940 /* pipesrc and dspsize control the size that is scaled from,
4941 * which should always be the user's requested size.
4943 I915_WRITE(DSPSIZE(plane),
4944 ((mode->vdisplay - 1) << 16) |
4945 (mode->hdisplay - 1));
4946 I915_WRITE(DSPPOS(plane), 0);
4948 i9xx_set_pipeconf(intel_crtc);
4950 I915_WRITE(DSPCNTR(plane), dspcntr);
4951 POSTING_READ(DSPCNTR(plane));
4953 ret = intel_pipe_set_base(crtc, x, y, fb);
4955 intel_update_watermarks(dev);
4960 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4961 struct intel_crtc_config *pipe_config)
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4967 tmp = I915_READ(PFIT_CONTROL);
4969 if (INTEL_INFO(dev)->gen < 4) {
4970 if (crtc->pipe != PIPE_B)
4973 /* gen2/3 store dither state in pfit control, needs to match */
4974 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4976 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4980 if (!(tmp & PFIT_ENABLE))
4983 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4984 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
4985 if (INTEL_INFO(dev)->gen < 5)
4986 pipe_config->gmch_pfit.lvds_border_bits =
4987 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
4990 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4991 struct intel_crtc_config *pipe_config)
4993 struct drm_device *dev = crtc->base.dev;
4994 struct drm_i915_private *dev_priv = dev->dev_private;
4997 pipe_config->cpu_transcoder = crtc->pipe;
4999 tmp = I915_READ(PIPECONF(crtc->pipe));
5000 if (!(tmp & PIPECONF_ENABLE))
5003 intel_get_pipe_timings(crtc, pipe_config);
5005 i9xx_get_pfit_config(crtc, pipe_config);
5007 if (INTEL_INFO(dev)->gen >= 4) {
5008 tmp = I915_READ(DPLL_MD(crtc->pipe));
5009 pipe_config->pixel_multiplier =
5010 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5011 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5012 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5013 tmp = I915_READ(DPLL(crtc->pipe));
5014 pipe_config->pixel_multiplier =
5015 ((tmp & SDVO_MULTIPLIER_MASK)
5016 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5018 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5019 * port and will be fixed up in the encoder->get_config
5021 pipe_config->pixel_multiplier = 1;
5027 static void ironlake_init_pch_refclk(struct drm_device *dev)
5029 struct drm_i915_private *dev_priv = dev->dev_private;
5030 struct drm_mode_config *mode_config = &dev->mode_config;
5031 struct intel_encoder *encoder;
5033 bool has_lvds = false;
5034 bool has_cpu_edp = false;
5035 bool has_panel = false;
5036 bool has_ck505 = false;
5037 bool can_ssc = false;
5039 /* We need to take the global config into account */
5040 list_for_each_entry(encoder, &mode_config->encoder_list,
5042 switch (encoder->type) {
5043 case INTEL_OUTPUT_LVDS:
5047 case INTEL_OUTPUT_EDP:
5049 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
5055 if (HAS_PCH_IBX(dev)) {
5056 has_ck505 = dev_priv->vbt.display_clock_mode;
5057 can_ssc = has_ck505;
5063 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5064 has_panel, has_lvds, has_ck505);
5066 /* Ironlake: try to setup display ref clock before DPLL
5067 * enabling. This is only under driver's control after
5068 * PCH B stepping, previous chipset stepping should be
5069 * ignoring this setting.
5071 val = I915_READ(PCH_DREF_CONTROL);
5073 /* As we must carefully and slowly disable/enable each source in turn,
5074 * compute the final state we want first and check if we need to
5075 * make any changes at all.
5078 final &= ~DREF_NONSPREAD_SOURCE_MASK;
5080 final |= DREF_NONSPREAD_CK505_ENABLE;
5082 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5084 final &= ~DREF_SSC_SOURCE_MASK;
5085 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5086 final &= ~DREF_SSC1_ENABLE;
5089 final |= DREF_SSC_SOURCE_ENABLE;
5091 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5092 final |= DREF_SSC1_ENABLE;
5095 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5096 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5098 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5100 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5102 final |= DREF_SSC_SOURCE_DISABLE;
5103 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5109 /* Always enable nonspread source */
5110 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5113 val |= DREF_NONSPREAD_CK505_ENABLE;
5115 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5118 val &= ~DREF_SSC_SOURCE_MASK;
5119 val |= DREF_SSC_SOURCE_ENABLE;
5121 /* SSC must be turned on before enabling the CPU output */
5122 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5123 DRM_DEBUG_KMS("Using SSC on panel\n");
5124 val |= DREF_SSC1_ENABLE;
5126 val &= ~DREF_SSC1_ENABLE;
5128 /* Get SSC going before enabling the outputs */
5129 I915_WRITE(PCH_DREF_CONTROL, val);
5130 POSTING_READ(PCH_DREF_CONTROL);
5133 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5135 /* Enable CPU source on CPU attached eDP */
5137 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5138 DRM_DEBUG_KMS("Using SSC on eDP\n");
5139 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5142 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5144 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5146 I915_WRITE(PCH_DREF_CONTROL, val);
5147 POSTING_READ(PCH_DREF_CONTROL);
5150 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5152 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5154 /* Turn off CPU output */
5155 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5157 I915_WRITE(PCH_DREF_CONTROL, val);
5158 POSTING_READ(PCH_DREF_CONTROL);
5161 /* Turn off the SSC source */
5162 val &= ~DREF_SSC_SOURCE_MASK;
5163 val |= DREF_SSC_SOURCE_DISABLE;
5166 val &= ~DREF_SSC1_ENABLE;
5168 I915_WRITE(PCH_DREF_CONTROL, val);
5169 POSTING_READ(PCH_DREF_CONTROL);
5173 BUG_ON(val != final);
5176 /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5177 static void lpt_init_pch_refclk(struct drm_device *dev)
5179 struct drm_i915_private *dev_priv = dev->dev_private;
5180 struct drm_mode_config *mode_config = &dev->mode_config;
5181 struct intel_encoder *encoder;
5182 bool has_vga = false;
5183 bool is_sdv = false;
5186 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5187 switch (encoder->type) {
5188 case INTEL_OUTPUT_ANALOG:
5197 mutex_lock(&dev_priv->dpio_lock);
5199 /* XXX: Rip out SDV support once Haswell ships for real. */
5200 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5203 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5204 tmp &= ~SBI_SSCCTL_DISABLE;
5205 tmp |= SBI_SSCCTL_PATHALT;
5206 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5210 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5211 tmp &= ~SBI_SSCCTL_PATHALT;
5212 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5215 tmp = I915_READ(SOUTH_CHICKEN2);
5216 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5217 I915_WRITE(SOUTH_CHICKEN2, tmp);
5219 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5220 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5221 DRM_ERROR("FDI mPHY reset assert timeout\n");
5223 tmp = I915_READ(SOUTH_CHICKEN2);
5224 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5225 I915_WRITE(SOUTH_CHICKEN2, tmp);
5227 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5228 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5230 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5233 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5234 tmp &= ~(0xFF << 24);
5235 tmp |= (0x12 << 24);
5236 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5239 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5241 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5244 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5246 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5248 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5250 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5253 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5254 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5255 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5257 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5258 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5259 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5261 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5263 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5265 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5267 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5270 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5271 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5272 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5274 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5275 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5276 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5279 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5282 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5284 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5287 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5290 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5293 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5295 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5298 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5300 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5301 tmp &= ~(0xFF << 16);
5302 tmp |= (0x1C << 16);
5303 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5305 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5306 tmp &= ~(0xFF << 16);
5307 tmp |= (0x1C << 16);
5308 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5311 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5313 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5315 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5317 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5319 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5320 tmp &= ~(0xF << 28);
5322 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5324 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5325 tmp &= ~(0xF << 28);
5327 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5330 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5331 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5332 tmp |= SBI_DBUFF0_ENABLE;
5333 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
5335 mutex_unlock(&dev_priv->dpio_lock);
5339 * Initialize reference clocks when the driver loads
5341 void intel_init_pch_refclk(struct drm_device *dev)
5343 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5344 ironlake_init_pch_refclk(dev);
5345 else if (HAS_PCH_LPT(dev))
5346 lpt_init_pch_refclk(dev);
5349 static int ironlake_get_refclk(struct drm_crtc *crtc)
5351 struct drm_device *dev = crtc->dev;
5352 struct drm_i915_private *dev_priv = dev->dev_private;
5353 struct intel_encoder *encoder;
5354 int num_connectors = 0;
5355 bool is_lvds = false;
5357 for_each_encoder_on_crtc(dev, crtc, encoder) {
5358 switch (encoder->type) {
5359 case INTEL_OUTPUT_LVDS:
5366 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5367 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5368 dev_priv->vbt.lvds_ssc_freq);
5369 return dev_priv->vbt.lvds_ssc_freq * 1000;
5375 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
5377 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5379 int pipe = intel_crtc->pipe;
5382 val = I915_READ(PIPECONF(pipe));
5384 val &= ~PIPECONF_BPC_MASK;
5385 switch (intel_crtc->config.pipe_bpp) {
5387 val |= PIPECONF_6BPC;
5390 val |= PIPECONF_8BPC;
5393 val |= PIPECONF_10BPC;
5396 val |= PIPECONF_12BPC;
5399 /* Case prevented by intel_choose_pipe_bpp_dither. */
5403 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5404 if (intel_crtc->config.dither)
5405 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5407 val &= ~PIPECONF_INTERLACE_MASK;
5408 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5409 val |= PIPECONF_INTERLACED_ILK;
5411 val |= PIPECONF_PROGRESSIVE;
5413 if (intel_crtc->config.limited_color_range)
5414 val |= PIPECONF_COLOR_RANGE_SELECT;
5416 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5418 I915_WRITE(PIPECONF(pipe), val);
5419 POSTING_READ(PIPECONF(pipe));
5423 * Set up the pipe CSC unit.
5425 * Currently only full range RGB to limited range RGB conversion
5426 * is supported, but eventually this should handle various
5427 * RGB<->YCbCr scenarios as well.
5429 static void intel_set_pipe_csc(struct drm_crtc *crtc)
5431 struct drm_device *dev = crtc->dev;
5432 struct drm_i915_private *dev_priv = dev->dev_private;
5433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5434 int pipe = intel_crtc->pipe;
5435 uint16_t coeff = 0x7800; /* 1.0 */
5438 * TODO: Check what kind of values actually come out of the pipe
5439 * with these coeff/postoff values and adjust to get the best
5440 * accuracy. Perhaps we even need to take the bpc value into
5444 if (intel_crtc->config.limited_color_range)
5445 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5448 * GY/GU and RY/RU should be the other way around according
5449 * to BSpec, but reality doesn't agree. Just set them up in
5450 * a way that results in the correct picture.
5452 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5453 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5455 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5456 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5458 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5459 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5461 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5462 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5463 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5465 if (INTEL_INFO(dev)->gen > 6) {
5466 uint16_t postoff = 0;
5468 if (intel_crtc->config.limited_color_range)
5469 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5471 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5472 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5473 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5475 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5477 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5479 if (intel_crtc->config.limited_color_range)
5480 mode |= CSC_BLACK_SCREEN_OFFSET;
5482 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5486 static void haswell_set_pipeconf(struct drm_crtc *crtc)
5488 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5490 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5493 val = I915_READ(PIPECONF(cpu_transcoder));
5495 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5496 if (intel_crtc->config.dither)
5497 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5499 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5500 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5501 val |= PIPECONF_INTERLACED_ILK;
5503 val |= PIPECONF_PROGRESSIVE;
5505 I915_WRITE(PIPECONF(cpu_transcoder), val);
5506 POSTING_READ(PIPECONF(cpu_transcoder));
5509 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5510 intel_clock_t *clock,
5511 bool *has_reduced_clock,
5512 intel_clock_t *reduced_clock)
5514 struct drm_device *dev = crtc->dev;
5515 struct drm_i915_private *dev_priv = dev->dev_private;
5516 struct intel_encoder *intel_encoder;
5518 const intel_limit_t *limit;
5519 bool ret, is_lvds = false;
5521 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5522 switch (intel_encoder->type) {
5523 case INTEL_OUTPUT_LVDS:
5529 refclk = ironlake_get_refclk(crtc);
5532 * Returns a set of divisors for the desired target clock with the given
5533 * refclk, or FALSE. The returned values represent the clock equation:
5534 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5536 limit = intel_limit(crtc, refclk);
5537 ret = dev_priv->display.find_dpll(limit, crtc,
5538 to_intel_crtc(crtc)->config.port_clock,
5539 refclk, NULL, clock);
5543 if (is_lvds && dev_priv->lvds_downclock_avail) {
5545 * Ensure we match the reduced clock's P to the target clock.
5546 * If the clocks don't match, we can't switch the display clock
5547 * by using the FP0/FP1. In such case we will disable the LVDS
5548 * downclock feature.
5550 *has_reduced_clock =
5551 dev_priv->display.find_dpll(limit, crtc,
5552 dev_priv->lvds_downclock,
5560 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5562 struct drm_i915_private *dev_priv = dev->dev_private;
5565 temp = I915_READ(SOUTH_CHICKEN1);
5566 if (temp & FDI_BC_BIFURCATION_SELECT)
5569 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5570 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5572 temp |= FDI_BC_BIFURCATION_SELECT;
5573 DRM_DEBUG_KMS("enabling fdi C rx\n");
5574 I915_WRITE(SOUTH_CHICKEN1, temp);
5575 POSTING_READ(SOUTH_CHICKEN1);
5578 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
5580 struct drm_device *dev = intel_crtc->base.dev;
5581 struct drm_i915_private *dev_priv = dev->dev_private;
5583 switch (intel_crtc->pipe) {
5587 if (intel_crtc->config.fdi_lanes > 2)
5588 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5590 cpt_enable_fdi_bc_bifurcation(dev);
5594 cpt_enable_fdi_bc_bifurcation(dev);
5602 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5605 * Account for spread spectrum to avoid
5606 * oversubscribing the link. Max center spread
5607 * is 2.5%; use 5% for safety's sake.
5609 u32 bps = target_clock * bpp * 21 / 20;
5610 return bps / (link_bw * 8) + 1;
5613 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5615 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5618 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5620 intel_clock_t *reduced_clock, u32 *fp2)
5622 struct drm_crtc *crtc = &intel_crtc->base;
5623 struct drm_device *dev = crtc->dev;
5624 struct drm_i915_private *dev_priv = dev->dev_private;
5625 struct intel_encoder *intel_encoder;
5627 int factor, num_connectors = 0;
5628 bool is_lvds = false, is_sdvo = false;
5630 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5631 switch (intel_encoder->type) {
5632 case INTEL_OUTPUT_LVDS:
5635 case INTEL_OUTPUT_SDVO:
5636 case INTEL_OUTPUT_HDMI:
5644 /* Enable autotuning of the PLL clock (if permissible) */
5647 if ((intel_panel_use_ssc(dev_priv) &&
5648 dev_priv->vbt.lvds_ssc_freq == 100) ||
5649 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
5651 } else if (intel_crtc->config.sdvo_tv_clock)
5654 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
5657 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5663 dpll |= DPLLB_MODE_LVDS;
5665 dpll |= DPLLB_MODE_DAC_SERIAL;
5667 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5668 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5671 dpll |= DPLL_DVO_HIGH_SPEED;
5672 if (intel_crtc->config.has_dp_encoder)
5673 dpll |= DPLL_DVO_HIGH_SPEED;
5675 /* compute bitmask from p1 value */
5676 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5678 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5680 switch (intel_crtc->config.dpll.p2) {
5682 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5685 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5688 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5691 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5695 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5696 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5698 dpll |= PLL_REF_INPUT_DREFCLK;
5703 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5705 struct drm_framebuffer *fb)
5707 struct drm_device *dev = crtc->dev;
5708 struct drm_i915_private *dev_priv = dev->dev_private;
5709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5710 int pipe = intel_crtc->pipe;
5711 int plane = intel_crtc->plane;
5712 int num_connectors = 0;
5713 intel_clock_t clock, reduced_clock;
5714 u32 dpll = 0, fp = 0, fp2 = 0;
5715 bool ok, has_reduced_clock = false;
5716 bool is_lvds = false;
5717 struct intel_encoder *encoder;
5720 for_each_encoder_on_crtc(dev, crtc, encoder) {
5721 switch (encoder->type) {
5722 case INTEL_OUTPUT_LVDS:
5730 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5731 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5733 ok = ironlake_compute_clocks(crtc, &clock,
5734 &has_reduced_clock, &reduced_clock);
5735 if (!ok && !intel_crtc->config.clock_set) {
5736 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5739 /* Compat-code for transition, will disappear. */
5740 if (!intel_crtc->config.clock_set) {
5741 intel_crtc->config.dpll.n = clock.n;
5742 intel_crtc->config.dpll.m1 = clock.m1;
5743 intel_crtc->config.dpll.m2 = clock.m2;
5744 intel_crtc->config.dpll.p1 = clock.p1;
5745 intel_crtc->config.dpll.p2 = clock.p2;
5748 /* Ensure that the cursor is valid for the new mode before changing... */
5749 intel_crtc_update_cursor(crtc, true);
5751 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5752 if (intel_crtc->config.has_pch_encoder) {
5753 struct intel_pch_pll *pll;
5755 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
5756 if (has_reduced_clock)
5757 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
5759 dpll = ironlake_compute_dpll(intel_crtc,
5760 &fp, &reduced_clock,
5761 has_reduced_clock ? &fp2 : NULL);
5763 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5765 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5770 intel_put_pch_pll(intel_crtc);
5772 if (intel_crtc->config.has_dp_encoder)
5773 intel_dp_set_m_n(intel_crtc);
5775 for_each_encoder_on_crtc(dev, crtc, encoder)
5776 if (encoder->pre_pll_enable)
5777 encoder->pre_pll_enable(encoder);
5779 if (intel_crtc->pch_pll) {
5780 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5782 /* Wait for the clocks to stabilize. */
5783 POSTING_READ(intel_crtc->pch_pll->pll_reg);
5786 /* The pixel multiplier can only be updated once the
5787 * DPLL is enabled and the clocks are stable.
5789 * So write it again.
5791 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5794 intel_crtc->lowfreq_avail = false;
5795 if (intel_crtc->pch_pll) {
5796 if (is_lvds && has_reduced_clock && i915_powersave) {
5797 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5798 intel_crtc->lowfreq_avail = true;
5800 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5804 intel_set_pipe_timings(intel_crtc);
5806 if (intel_crtc->config.has_pch_encoder) {
5807 intel_cpu_transcoder_set_m_n(intel_crtc,
5808 &intel_crtc->config.fdi_m_n);
5811 if (IS_IVYBRIDGE(dev))
5812 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
5814 ironlake_set_pipeconf(crtc);
5816 /* Set up the display plane register */
5817 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5818 POSTING_READ(DSPCNTR(plane));
5820 ret = intel_pipe_set_base(crtc, x, y, fb);
5822 intel_update_watermarks(dev);
5827 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5828 struct intel_crtc_config *pipe_config)
5830 struct drm_device *dev = crtc->base.dev;
5831 struct drm_i915_private *dev_priv = dev->dev_private;
5832 enum transcoder transcoder = pipe_config->cpu_transcoder;
5834 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5835 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5836 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5838 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5839 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5840 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5843 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5844 struct intel_crtc_config *pipe_config)
5846 struct drm_device *dev = crtc->base.dev;
5847 struct drm_i915_private *dev_priv = dev->dev_private;
5850 tmp = I915_READ(PF_CTL(crtc->pipe));
5852 if (tmp & PF_ENABLE) {
5853 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5854 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
5856 /* We currently do not free assignements of panel fitters on
5857 * ivb/hsw (since we don't use the higher upscaling modes which
5858 * differentiates them) so just WARN about this case for now. */
5860 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5861 PF_PIPE_SEL_IVB(crtc->pipe));
5866 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5867 struct intel_crtc_config *pipe_config)
5869 struct drm_device *dev = crtc->base.dev;
5870 struct drm_i915_private *dev_priv = dev->dev_private;
5873 pipe_config->cpu_transcoder = crtc->pipe;
5875 tmp = I915_READ(PIPECONF(crtc->pipe));
5876 if (!(tmp & PIPECONF_ENABLE))
5879 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5880 pipe_config->has_pch_encoder = true;
5882 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5883 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5884 FDI_DP_PORT_WIDTH_SHIFT) + 1;
5886 ironlake_get_fdi_m_n_config(crtc, pipe_config);
5888 /* XXX: Can't properly read out the pch dpll pixel multiplier
5889 * since we don't have state tracking for pch clocks yet. */
5890 pipe_config->pixel_multiplier = 1;
5892 pipe_config->pixel_multiplier = 1;
5895 intel_get_pipe_timings(crtc, pipe_config);
5897 ironlake_get_pfit_config(crtc, pipe_config);
5902 static void haswell_modeset_global_resources(struct drm_device *dev)
5904 bool enable = false;
5905 struct intel_crtc *crtc;
5907 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5908 if (!crtc->base.enabled)
5911 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5912 crtc->config.cpu_transcoder != TRANSCODER_EDP)
5916 intel_set_power_well(dev, enable);
5919 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5921 struct drm_framebuffer *fb)
5923 struct drm_device *dev = crtc->dev;
5924 struct drm_i915_private *dev_priv = dev->dev_private;
5925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5926 int plane = intel_crtc->plane;
5929 if (!intel_ddi_pll_mode_set(crtc))
5932 /* Ensure that the cursor is valid for the new mode before changing... */
5933 intel_crtc_update_cursor(crtc, true);
5935 if (intel_crtc->config.has_dp_encoder)
5936 intel_dp_set_m_n(intel_crtc);
5938 intel_crtc->lowfreq_avail = false;
5940 intel_set_pipe_timings(intel_crtc);
5942 if (intel_crtc->config.has_pch_encoder) {
5943 intel_cpu_transcoder_set_m_n(intel_crtc,
5944 &intel_crtc->config.fdi_m_n);
5947 haswell_set_pipeconf(crtc);
5949 intel_set_pipe_csc(crtc);
5951 /* Set up the display plane register */
5952 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
5953 POSTING_READ(DSPCNTR(plane));
5955 ret = intel_pipe_set_base(crtc, x, y, fb);
5957 intel_update_watermarks(dev);
5962 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5963 struct intel_crtc_config *pipe_config)
5965 struct drm_device *dev = crtc->base.dev;
5966 struct drm_i915_private *dev_priv = dev->dev_private;
5967 enum intel_display_power_domain pfit_domain;
5970 pipe_config->cpu_transcoder = crtc->pipe;
5971 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5972 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5973 enum pipe trans_edp_pipe;
5974 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5976 WARN(1, "unknown pipe linked to edp transcoder\n");
5977 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5978 case TRANS_DDI_EDP_INPUT_A_ON:
5979 trans_edp_pipe = PIPE_A;
5981 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5982 trans_edp_pipe = PIPE_B;
5984 case TRANS_DDI_EDP_INPUT_C_ONOFF:
5985 trans_edp_pipe = PIPE_C;
5989 if (trans_edp_pipe == crtc->pipe)
5990 pipe_config->cpu_transcoder = TRANSCODER_EDP;
5993 if (!intel_display_power_enabled(dev,
5994 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
5997 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
5998 if (!(tmp & PIPECONF_ENABLE))
6002 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6003 * DDI E. So just check whether this pipe is wired to DDI E and whether
6004 * the PCH transcoder is on.
6006 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
6007 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
6008 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
6009 pipe_config->has_pch_encoder = true;
6011 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6012 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6013 FDI_DP_PORT_WIDTH_SHIFT) + 1;
6015 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6018 intel_get_pipe_timings(crtc, pipe_config);
6020 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6021 if (intel_display_power_enabled(dev, pfit_domain))
6022 ironlake_get_pfit_config(crtc, pipe_config);
6024 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6025 (I915_READ(IPS_CTL) & IPS_ENABLE);
6027 pipe_config->pixel_multiplier = 1;
6032 static int intel_crtc_mode_set(struct drm_crtc *crtc,
6034 struct drm_framebuffer *fb)
6036 struct drm_device *dev = crtc->dev;
6037 struct drm_i915_private *dev_priv = dev->dev_private;
6038 struct drm_encoder_helper_funcs *encoder_funcs;
6039 struct intel_encoder *encoder;
6040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6041 struct drm_display_mode *adjusted_mode =
6042 &intel_crtc->config.adjusted_mode;
6043 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
6044 int pipe = intel_crtc->pipe;
6047 drm_vblank_pre_modeset(dev, pipe);
6049 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6051 drm_vblank_post_modeset(dev, pipe);
6056 for_each_encoder_on_crtc(dev, crtc, encoder) {
6057 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6058 encoder->base.base.id,
6059 drm_get_encoder_name(&encoder->base),
6060 mode->base.id, mode->name);
6061 if (encoder->mode_set) {
6062 encoder->mode_set(encoder);
6064 encoder_funcs = encoder->base.helper_private;
6065 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6072 static bool intel_eld_uptodate(struct drm_connector *connector,
6073 int reg_eldv, uint32_t bits_eldv,
6074 int reg_elda, uint32_t bits_elda,
6077 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6078 uint8_t *eld = connector->eld;
6081 i = I915_READ(reg_eldv);
6090 i = I915_READ(reg_elda);
6092 I915_WRITE(reg_elda, i);
6094 for (i = 0; i < eld[2]; i++)
6095 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6101 static void g4x_write_eld(struct drm_connector *connector,
6102 struct drm_crtc *crtc)
6104 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6105 uint8_t *eld = connector->eld;
6110 i = I915_READ(G4X_AUD_VID_DID);
6112 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6113 eldv = G4X_ELDV_DEVCL_DEVBLC;
6115 eldv = G4X_ELDV_DEVCTG;
6117 if (intel_eld_uptodate(connector,
6118 G4X_AUD_CNTL_ST, eldv,
6119 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6120 G4X_HDMIW_HDMIEDID))
6123 i = I915_READ(G4X_AUD_CNTL_ST);
6124 i &= ~(eldv | G4X_ELD_ADDR);
6125 len = (i >> 9) & 0x1f; /* ELD buffer size */
6126 I915_WRITE(G4X_AUD_CNTL_ST, i);
6131 len = min_t(uint8_t, eld[2], len);
6132 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6133 for (i = 0; i < len; i++)
6134 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6136 i = I915_READ(G4X_AUD_CNTL_ST);
6138 I915_WRITE(G4X_AUD_CNTL_ST, i);
6141 static void haswell_write_eld(struct drm_connector *connector,
6142 struct drm_crtc *crtc)
6144 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6145 uint8_t *eld = connector->eld;
6146 struct drm_device *dev = crtc->dev;
6147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6151 int pipe = to_intel_crtc(crtc)->pipe;
6154 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6155 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6156 int aud_config = HSW_AUD_CFG(pipe);
6157 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6160 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6162 /* Audio output enable */
6163 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6164 tmp = I915_READ(aud_cntrl_st2);
6165 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6166 I915_WRITE(aud_cntrl_st2, tmp);
6168 /* Wait for 1 vertical blank */
6169 intel_wait_for_vblank(dev, pipe);
6171 /* Set ELD valid state */
6172 tmp = I915_READ(aud_cntrl_st2);
6173 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6174 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6175 I915_WRITE(aud_cntrl_st2, tmp);
6176 tmp = I915_READ(aud_cntrl_st2);
6177 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6179 /* Enable HDMI mode */
6180 tmp = I915_READ(aud_config);
6181 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6182 /* clear N_programing_enable and N_value_index */
6183 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6184 I915_WRITE(aud_config, tmp);
6186 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6188 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
6189 intel_crtc->eld_vld = true;
6191 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6192 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6193 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6194 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6196 I915_WRITE(aud_config, 0);
6198 if (intel_eld_uptodate(connector,
6199 aud_cntrl_st2, eldv,
6200 aud_cntl_st, IBX_ELD_ADDRESS,
6204 i = I915_READ(aud_cntrl_st2);
6206 I915_WRITE(aud_cntrl_st2, i);
6211 i = I915_READ(aud_cntl_st);
6212 i &= ~IBX_ELD_ADDRESS;
6213 I915_WRITE(aud_cntl_st, i);
6214 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6215 DRM_DEBUG_DRIVER("port num:%d\n", i);
6217 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6218 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6219 for (i = 0; i < len; i++)
6220 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6222 i = I915_READ(aud_cntrl_st2);
6224 I915_WRITE(aud_cntrl_st2, i);
6228 static void ironlake_write_eld(struct drm_connector *connector,
6229 struct drm_crtc *crtc)
6231 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6232 uint8_t *eld = connector->eld;
6240 int pipe = to_intel_crtc(crtc)->pipe;
6242 if (HAS_PCH_IBX(connector->dev)) {
6243 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6244 aud_config = IBX_AUD_CFG(pipe);
6245 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
6246 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
6248 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6249 aud_config = CPT_AUD_CFG(pipe);
6250 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
6251 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
6254 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6256 i = I915_READ(aud_cntl_st);
6257 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6259 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6260 /* operate blindly on all ports */
6261 eldv = IBX_ELD_VALIDB;
6262 eldv |= IBX_ELD_VALIDB << 4;
6263 eldv |= IBX_ELD_VALIDB << 8;
6265 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
6266 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
6269 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6270 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6271 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6272 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6274 I915_WRITE(aud_config, 0);
6276 if (intel_eld_uptodate(connector,
6277 aud_cntrl_st2, eldv,
6278 aud_cntl_st, IBX_ELD_ADDRESS,
6282 i = I915_READ(aud_cntrl_st2);
6284 I915_WRITE(aud_cntrl_st2, i);
6289 i = I915_READ(aud_cntl_st);
6290 i &= ~IBX_ELD_ADDRESS;
6291 I915_WRITE(aud_cntl_st, i);
6293 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6294 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6295 for (i = 0; i < len; i++)
6296 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6298 i = I915_READ(aud_cntrl_st2);
6300 I915_WRITE(aud_cntrl_st2, i);
6303 void intel_write_eld(struct drm_encoder *encoder,
6304 struct drm_display_mode *mode)
6306 struct drm_crtc *crtc = encoder->crtc;
6307 struct drm_connector *connector;
6308 struct drm_device *dev = encoder->dev;
6309 struct drm_i915_private *dev_priv = dev->dev_private;
6311 connector = drm_select_eld(encoder, mode);
6315 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6317 drm_get_connector_name(connector),
6318 connector->encoder->base.id,
6319 drm_get_encoder_name(connector->encoder));
6321 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6323 if (dev_priv->display.write_eld)
6324 dev_priv->display.write_eld(connector, crtc);
6327 /** Loads the palette/gamma unit for the CRTC with the prepared values */
6328 void intel_crtc_load_lut(struct drm_crtc *crtc)
6330 struct drm_device *dev = crtc->dev;
6331 struct drm_i915_private *dev_priv = dev->dev_private;
6332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6333 enum pipe pipe = intel_crtc->pipe;
6334 int palreg = PALETTE(pipe);
6336 bool reenable_ips = false;
6338 /* The clocks have to be on to load the palette. */
6339 if (!crtc->enabled || !intel_crtc->active)
6342 if (!HAS_PCH_SPLIT(dev_priv->dev))
6343 assert_pll_enabled(dev_priv, pipe);
6345 /* use legacy palette for Ironlake */
6346 if (HAS_PCH_SPLIT(dev))
6347 palreg = LGC_PALETTE(pipe);
6349 /* Workaround : Do not read or write the pipe palette/gamma data while
6350 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6352 if (intel_crtc->config.ips_enabled &&
6353 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6354 GAMMA_MODE_MODE_SPLIT)) {
6355 hsw_disable_ips(intel_crtc);
6356 reenable_ips = true;
6359 for (i = 0; i < 256; i++) {
6360 I915_WRITE(palreg + 4 * i,
6361 (intel_crtc->lut_r[i] << 16) |
6362 (intel_crtc->lut_g[i] << 8) |
6363 intel_crtc->lut_b[i]);
6367 hsw_enable_ips(intel_crtc);
6370 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6372 struct drm_device *dev = crtc->dev;
6373 struct drm_i915_private *dev_priv = dev->dev_private;
6374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6375 bool visible = base != 0;
6378 if (intel_crtc->cursor_visible == visible)
6381 cntl = I915_READ(_CURACNTR);
6383 /* On these chipsets we can only modify the base whilst
6384 * the cursor is disabled.
6386 I915_WRITE(_CURABASE, base);
6388 cntl &= ~(CURSOR_FORMAT_MASK);
6389 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6390 cntl |= CURSOR_ENABLE |
6391 CURSOR_GAMMA_ENABLE |
6394 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
6395 I915_WRITE(_CURACNTR, cntl);
6397 intel_crtc->cursor_visible = visible;
6400 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6402 struct drm_device *dev = crtc->dev;
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6405 int pipe = intel_crtc->pipe;
6406 bool visible = base != 0;
6408 if (intel_crtc->cursor_visible != visible) {
6409 uint32_t cntl = I915_READ(CURCNTR(pipe));
6411 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6412 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6413 cntl |= pipe << 28; /* Connect to correct pipe */
6415 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6416 cntl |= CURSOR_MODE_DISABLE;
6418 I915_WRITE(CURCNTR(pipe), cntl);
6420 intel_crtc->cursor_visible = visible;
6422 /* and commit changes on next vblank */
6423 I915_WRITE(CURBASE(pipe), base);
6426 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6428 struct drm_device *dev = crtc->dev;
6429 struct drm_i915_private *dev_priv = dev->dev_private;
6430 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6431 int pipe = intel_crtc->pipe;
6432 bool visible = base != 0;
6434 if (intel_crtc->cursor_visible != visible) {
6435 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6437 cntl &= ~CURSOR_MODE;
6438 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6440 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6441 cntl |= CURSOR_MODE_DISABLE;
6443 if (IS_HASWELL(dev))
6444 cntl |= CURSOR_PIPE_CSC_ENABLE;
6445 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6447 intel_crtc->cursor_visible = visible;
6449 /* and commit changes on next vblank */
6450 I915_WRITE(CURBASE_IVB(pipe), base);
6453 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6454 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6457 struct drm_device *dev = crtc->dev;
6458 struct drm_i915_private *dev_priv = dev->dev_private;
6459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6460 int pipe = intel_crtc->pipe;
6461 int x = intel_crtc->cursor_x;
6462 int y = intel_crtc->cursor_y;
6468 if (on && crtc->enabled && crtc->fb) {
6469 base = intel_crtc->cursor_addr;
6470 if (x > (int) crtc->fb->width)
6473 if (y > (int) crtc->fb->height)
6479 if (x + intel_crtc->cursor_width < 0)
6482 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6485 pos |= x << CURSOR_X_SHIFT;
6488 if (y + intel_crtc->cursor_height < 0)
6491 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6494 pos |= y << CURSOR_Y_SHIFT;
6496 visible = base != 0;
6497 if (!visible && !intel_crtc->cursor_visible)
6500 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
6501 I915_WRITE(CURPOS_IVB(pipe), pos);
6502 ivb_update_cursor(crtc, base);
6504 I915_WRITE(CURPOS(pipe), pos);
6505 if (IS_845G(dev) || IS_I865G(dev))
6506 i845_update_cursor(crtc, base);
6508 i9xx_update_cursor(crtc, base);
6512 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
6513 struct drm_file *file,
6515 uint32_t width, uint32_t height)
6517 struct drm_device *dev = crtc->dev;
6518 struct drm_i915_private *dev_priv = dev->dev_private;
6519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6520 struct drm_i915_gem_object *obj;
6524 /* if we want to turn off the cursor ignore width and height */
6526 DRM_DEBUG_KMS("cursor off\n");
6529 mutex_lock(&dev->struct_mutex);
6533 /* Currently we only support 64x64 cursors */
6534 if (width != 64 || height != 64) {
6535 DRM_ERROR("we currently only support 64x64 cursors\n");
6539 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
6540 if (&obj->base == NULL)
6543 if (obj->base.size < width * height * 4) {
6544 DRM_ERROR("buffer is to small\n");
6549 /* we only need to pin inside GTT if cursor is non-phy */
6550 mutex_lock(&dev->struct_mutex);
6551 if (!dev_priv->info->cursor_needs_physical) {
6554 if (obj->tiling_mode) {
6555 DRM_ERROR("cursor cannot be tiled\n");
6560 /* Note that the w/a also requires 2 PTE of padding following
6561 * the bo. We currently fill all unused PTE with the shadow
6562 * page and so we should always have valid PTE following the
6563 * cursor preventing the VT-d warning.
6566 if (need_vtd_wa(dev))
6567 alignment = 64*1024;
6569 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
6571 DRM_ERROR("failed to move cursor bo into the GTT\n");
6575 ret = i915_gem_object_put_fence(obj);
6577 DRM_ERROR("failed to release fence for cursor");
6581 addr = obj->gtt_offset;
6583 int align = IS_I830(dev) ? 16 * 1024 : 256;
6584 ret = i915_gem_attach_phys_object(dev, obj,
6585 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6588 DRM_ERROR("failed to attach phys object\n");
6591 addr = obj->phys_obj->handle->busaddr;
6595 I915_WRITE(CURSIZE, (height << 12) | width);
6598 if (intel_crtc->cursor_bo) {
6599 if (dev_priv->info->cursor_needs_physical) {
6600 if (intel_crtc->cursor_bo != obj)
6601 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6603 i915_gem_object_unpin(intel_crtc->cursor_bo);
6604 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6607 mutex_unlock(&dev->struct_mutex);
6609 intel_crtc->cursor_addr = addr;
6610 intel_crtc->cursor_bo = obj;
6611 intel_crtc->cursor_width = width;
6612 intel_crtc->cursor_height = height;
6614 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6618 i915_gem_object_unpin(obj);
6620 mutex_unlock(&dev->struct_mutex);
6622 drm_gem_object_unreference_unlocked(&obj->base);
6626 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6630 intel_crtc->cursor_x = x;
6631 intel_crtc->cursor_y = y;
6633 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
6638 /** Sets the color ramps on behalf of RandR */
6639 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6640 u16 blue, int regno)
6642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6644 intel_crtc->lut_r[regno] = red >> 8;
6645 intel_crtc->lut_g[regno] = green >> 8;
6646 intel_crtc->lut_b[regno] = blue >> 8;
6649 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6650 u16 *blue, int regno)
6652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6654 *red = intel_crtc->lut_r[regno] << 8;
6655 *green = intel_crtc->lut_g[regno] << 8;
6656 *blue = intel_crtc->lut_b[regno] << 8;
6659 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
6660 u16 *blue, uint32_t start, uint32_t size)
6662 int end = (start + size > 256) ? 256 : start + size, i;
6663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6665 for (i = start; i < end; i++) {
6666 intel_crtc->lut_r[i] = red[i] >> 8;
6667 intel_crtc->lut_g[i] = green[i] >> 8;
6668 intel_crtc->lut_b[i] = blue[i] >> 8;
6671 intel_crtc_load_lut(crtc);
6674 /* VESA 640x480x72Hz mode to set on the pipe */
6675 static struct drm_display_mode load_detect_mode = {
6676 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6677 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6680 static struct drm_framebuffer *
6681 intel_framebuffer_create(struct drm_device *dev,
6682 struct drm_mode_fb_cmd2 *mode_cmd,
6683 struct drm_i915_gem_object *obj)
6685 struct intel_framebuffer *intel_fb;
6688 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6690 drm_gem_object_unreference_unlocked(&obj->base);
6691 return ERR_PTR(-ENOMEM);
6694 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6696 drm_gem_object_unreference_unlocked(&obj->base);
6698 return ERR_PTR(ret);
6701 return &intel_fb->base;
6705 intel_framebuffer_pitch_for_width(int width, int bpp)
6707 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6708 return ALIGN(pitch, 64);
6712 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6714 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6715 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6718 static struct drm_framebuffer *
6719 intel_framebuffer_create_for_mode(struct drm_device *dev,
6720 struct drm_display_mode *mode,
6723 struct drm_i915_gem_object *obj;
6724 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
6726 obj = i915_gem_alloc_object(dev,
6727 intel_framebuffer_size_for_mode(mode, bpp));
6729 return ERR_PTR(-ENOMEM);
6731 mode_cmd.width = mode->hdisplay;
6732 mode_cmd.height = mode->vdisplay;
6733 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6735 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
6737 return intel_framebuffer_create(dev, &mode_cmd, obj);
6740 static struct drm_framebuffer *
6741 mode_fits_in_fbdev(struct drm_device *dev,
6742 struct drm_display_mode *mode)
6744 struct drm_i915_private *dev_priv = dev->dev_private;
6745 struct drm_i915_gem_object *obj;
6746 struct drm_framebuffer *fb;
6748 if (dev_priv->fbdev == NULL)
6751 obj = dev_priv->fbdev->ifb.obj;
6755 fb = &dev_priv->fbdev->ifb.base;
6756 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6757 fb->bits_per_pixel))
6760 if (obj->base.size < mode->vdisplay * fb->pitches[0])
6766 bool intel_get_load_detect_pipe(struct drm_connector *connector,
6767 struct drm_display_mode *mode,
6768 struct intel_load_detect_pipe *old)
6770 struct intel_crtc *intel_crtc;
6771 struct intel_encoder *intel_encoder =
6772 intel_attached_encoder(connector);
6773 struct drm_crtc *possible_crtc;
6774 struct drm_encoder *encoder = &intel_encoder->base;
6775 struct drm_crtc *crtc = NULL;
6776 struct drm_device *dev = encoder->dev;
6777 struct drm_framebuffer *fb;
6780 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6781 connector->base.id, drm_get_connector_name(connector),
6782 encoder->base.id, drm_get_encoder_name(encoder));
6785 * Algorithm gets a little messy:
6787 * - if the connector already has an assigned crtc, use it (but make
6788 * sure it's on first)
6790 * - try to find the first unused crtc that can drive this connector,
6791 * and use that if we find one
6794 /* See if we already have a CRTC for this connector */
6795 if (encoder->crtc) {
6796 crtc = encoder->crtc;
6798 mutex_lock(&crtc->mutex);
6800 old->dpms_mode = connector->dpms;
6801 old->load_detect_temp = false;
6803 /* Make sure the crtc and connector are running */
6804 if (connector->dpms != DRM_MODE_DPMS_ON)
6805 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
6810 /* Find an unused one (if possible) */
6811 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6813 if (!(encoder->possible_crtcs & (1 << i)))
6815 if (!possible_crtc->enabled) {
6816 crtc = possible_crtc;
6822 * If we didn't find an unused CRTC, don't use any.
6825 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6829 mutex_lock(&crtc->mutex);
6830 intel_encoder->new_crtc = to_intel_crtc(crtc);
6831 to_intel_connector(connector)->new_encoder = intel_encoder;
6833 intel_crtc = to_intel_crtc(crtc);
6834 old->dpms_mode = connector->dpms;
6835 old->load_detect_temp = true;
6836 old->release_fb = NULL;
6839 mode = &load_detect_mode;
6841 /* We need a framebuffer large enough to accommodate all accesses
6842 * that the plane may generate whilst we perform load detection.
6843 * We can not rely on the fbcon either being present (we get called
6844 * during its initialisation to detect all boot displays, or it may
6845 * not even exist) or that it is large enough to satisfy the
6848 fb = mode_fits_in_fbdev(dev, mode);
6850 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6851 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6852 old->release_fb = fb;
6854 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6856 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6857 mutex_unlock(&crtc->mutex);
6861 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6862 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
6863 if (old->release_fb)
6864 old->release_fb->funcs->destroy(old->release_fb);
6865 mutex_unlock(&crtc->mutex);
6869 /* let the connector get through one full cycle before testing */
6870 intel_wait_for_vblank(dev, intel_crtc->pipe);
6874 void intel_release_load_detect_pipe(struct drm_connector *connector,
6875 struct intel_load_detect_pipe *old)
6877 struct intel_encoder *intel_encoder =
6878 intel_attached_encoder(connector);
6879 struct drm_encoder *encoder = &intel_encoder->base;
6880 struct drm_crtc *crtc = encoder->crtc;
6882 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6883 connector->base.id, drm_get_connector_name(connector),
6884 encoder->base.id, drm_get_encoder_name(encoder));
6886 if (old->load_detect_temp) {
6887 to_intel_connector(connector)->new_encoder = NULL;
6888 intel_encoder->new_crtc = NULL;
6889 intel_set_mode(crtc, NULL, 0, 0, NULL);
6891 if (old->release_fb) {
6892 drm_framebuffer_unregister_private(old->release_fb);
6893 drm_framebuffer_unreference(old->release_fb);
6896 mutex_unlock(&crtc->mutex);
6900 /* Switch crtc and encoder back off if necessary */
6901 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6902 connector->funcs->dpms(connector, old->dpms_mode);
6904 mutex_unlock(&crtc->mutex);
6907 /* Returns the clock of the currently programmed mode of the given pipe. */
6908 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6910 struct drm_i915_private *dev_priv = dev->dev_private;
6911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6912 int pipe = intel_crtc->pipe;
6913 u32 dpll = I915_READ(DPLL(pipe));
6915 intel_clock_t clock;
6917 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6918 fp = I915_READ(FP0(pipe));
6920 fp = I915_READ(FP1(pipe));
6922 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6923 if (IS_PINEVIEW(dev)) {
6924 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6925 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6927 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6928 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6931 if (!IS_GEN2(dev)) {
6932 if (IS_PINEVIEW(dev))
6933 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6934 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6936 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6937 DPLL_FPA01_P1_POST_DIV_SHIFT);
6939 switch (dpll & DPLL_MODE_MASK) {
6940 case DPLLB_MODE_DAC_SERIAL:
6941 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6944 case DPLLB_MODE_LVDS:
6945 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6949 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
6950 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6954 if (IS_PINEVIEW(dev))
6955 pineview_clock(96000, &clock);
6957 i9xx_clock(96000, &clock);
6959 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6962 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6963 DPLL_FPA01_P1_POST_DIV_SHIFT);
6966 if ((dpll & PLL_REF_INPUT_MASK) ==
6967 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6968 /* XXX: might not be 66MHz */
6969 i9xx_clock(66000, &clock);
6971 i9xx_clock(48000, &clock);
6973 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6976 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6977 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6979 if (dpll & PLL_P2_DIVIDE_BY_4)
6984 i9xx_clock(48000, &clock);
6988 /* XXX: It would be nice to validate the clocks, but we can't reuse
6989 * i830PllIsValid() because it relies on the xf86_config connector
6990 * configuration being accurate, which it isn't necessarily.
6996 /** Returns the currently programmed mode of the given pipe. */
6997 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6998 struct drm_crtc *crtc)
7000 struct drm_i915_private *dev_priv = dev->dev_private;
7001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7002 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
7003 struct drm_display_mode *mode;
7004 int htot = I915_READ(HTOTAL(cpu_transcoder));
7005 int hsync = I915_READ(HSYNC(cpu_transcoder));
7006 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7007 int vsync = I915_READ(VSYNC(cpu_transcoder));
7009 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7013 mode->clock = intel_crtc_clock_get(dev, crtc);
7014 mode->hdisplay = (htot & 0xffff) + 1;
7015 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7016 mode->hsync_start = (hsync & 0xffff) + 1;
7017 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7018 mode->vdisplay = (vtot & 0xffff) + 1;
7019 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7020 mode->vsync_start = (vsync & 0xffff) + 1;
7021 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7023 drm_mode_set_name(mode);
7028 static void intel_increase_pllclock(struct drm_crtc *crtc)
7030 struct drm_device *dev = crtc->dev;
7031 drm_i915_private_t *dev_priv = dev->dev_private;
7032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7033 int pipe = intel_crtc->pipe;
7034 int dpll_reg = DPLL(pipe);
7037 if (HAS_PCH_SPLIT(dev))
7040 if (!dev_priv->lvds_downclock_avail)
7043 dpll = I915_READ(dpll_reg);
7044 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
7045 DRM_DEBUG_DRIVER("upclocking LVDS\n");
7047 assert_panel_unlocked(dev_priv, pipe);
7049 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7050 I915_WRITE(dpll_reg, dpll);
7051 intel_wait_for_vblank(dev, pipe);
7053 dpll = I915_READ(dpll_reg);
7054 if (dpll & DISPLAY_RATE_SELECT_FPA1)
7055 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
7059 static void intel_decrease_pllclock(struct drm_crtc *crtc)
7061 struct drm_device *dev = crtc->dev;
7062 drm_i915_private_t *dev_priv = dev->dev_private;
7063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7065 if (HAS_PCH_SPLIT(dev))
7068 if (!dev_priv->lvds_downclock_avail)
7072 * Since this is called by a timer, we should never get here in
7075 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
7076 int pipe = intel_crtc->pipe;
7077 int dpll_reg = DPLL(pipe);
7080 DRM_DEBUG_DRIVER("downclocking LVDS\n");
7082 assert_panel_unlocked(dev_priv, pipe);
7084 dpll = I915_READ(dpll_reg);
7085 dpll |= DISPLAY_RATE_SELECT_FPA1;
7086 I915_WRITE(dpll_reg, dpll);
7087 intel_wait_for_vblank(dev, pipe);
7088 dpll = I915_READ(dpll_reg);
7089 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
7090 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
7095 void intel_mark_busy(struct drm_device *dev)
7097 i915_update_gfx_val(dev->dev_private);
7100 void intel_mark_idle(struct drm_device *dev)
7102 struct drm_crtc *crtc;
7104 if (!i915_powersave)
7107 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7111 intel_decrease_pllclock(crtc);
7115 void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7116 struct intel_ring_buffer *ring)
7118 struct drm_device *dev = obj->base.dev;
7119 struct drm_crtc *crtc;
7121 if (!i915_powersave)
7124 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7128 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7131 intel_increase_pllclock(crtc);
7132 if (ring && intel_fbc_enabled(dev))
7133 ring->fbc_dirty = true;
7137 static void intel_crtc_destroy(struct drm_crtc *crtc)
7139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7140 struct drm_device *dev = crtc->dev;
7141 struct intel_unpin_work *work;
7142 unsigned long flags;
7144 spin_lock_irqsave(&dev->event_lock, flags);
7145 work = intel_crtc->unpin_work;
7146 intel_crtc->unpin_work = NULL;
7147 spin_unlock_irqrestore(&dev->event_lock, flags);
7150 cancel_work_sync(&work->work);
7154 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7156 drm_crtc_cleanup(crtc);
7161 static void intel_unpin_work_fn(struct work_struct *__work)
7163 struct intel_unpin_work *work =
7164 container_of(__work, struct intel_unpin_work, work);
7165 struct drm_device *dev = work->crtc->dev;
7167 mutex_lock(&dev->struct_mutex);
7168 intel_unpin_fb_obj(work->old_fb_obj);
7169 drm_gem_object_unreference(&work->pending_flip_obj->base);
7170 drm_gem_object_unreference(&work->old_fb_obj->base);
7172 intel_update_fbc(dev);
7173 mutex_unlock(&dev->struct_mutex);
7175 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7176 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7181 static void do_intel_finish_page_flip(struct drm_device *dev,
7182 struct drm_crtc *crtc)
7184 drm_i915_private_t *dev_priv = dev->dev_private;
7185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7186 struct intel_unpin_work *work;
7187 unsigned long flags;
7189 /* Ignore early vblank irqs */
7190 if (intel_crtc == NULL)
7193 spin_lock_irqsave(&dev->event_lock, flags);
7194 work = intel_crtc->unpin_work;
7196 /* Ensure we don't miss a work->pending update ... */
7199 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
7200 spin_unlock_irqrestore(&dev->event_lock, flags);
7204 /* and that the unpin work is consistent wrt ->pending. */
7207 intel_crtc->unpin_work = NULL;
7210 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
7212 drm_vblank_put(dev, intel_crtc->pipe);
7214 spin_unlock_irqrestore(&dev->event_lock, flags);
7216 wake_up_all(&dev_priv->pending_flip_queue);
7218 queue_work(dev_priv->wq, &work->work);
7220 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
7223 void intel_finish_page_flip(struct drm_device *dev, int pipe)
7225 drm_i915_private_t *dev_priv = dev->dev_private;
7226 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7228 do_intel_finish_page_flip(dev, crtc);
7231 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7233 drm_i915_private_t *dev_priv = dev->dev_private;
7234 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7236 do_intel_finish_page_flip(dev, crtc);
7239 void intel_prepare_page_flip(struct drm_device *dev, int plane)
7241 drm_i915_private_t *dev_priv = dev->dev_private;
7242 struct intel_crtc *intel_crtc =
7243 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7244 unsigned long flags;
7246 /* NB: An MMIO update of the plane base pointer will also
7247 * generate a page-flip completion irq, i.e. every modeset
7248 * is also accompanied by a spurious intel_prepare_page_flip().
7250 spin_lock_irqsave(&dev->event_lock, flags);
7251 if (intel_crtc->unpin_work)
7252 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
7253 spin_unlock_irqrestore(&dev->event_lock, flags);
7256 inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7258 /* Ensure that the work item is consistent when activating it ... */
7260 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7261 /* and that it is marked active as soon as the irq could fire. */
7265 static int intel_gen2_queue_flip(struct drm_device *dev,
7266 struct drm_crtc *crtc,
7267 struct drm_framebuffer *fb,
7268 struct drm_i915_gem_object *obj)
7270 struct drm_i915_private *dev_priv = dev->dev_private;
7271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7273 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7276 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7280 ret = intel_ring_begin(ring, 6);
7284 /* Can't queue multiple flips, so wait for the previous
7285 * one to finish before executing the next.
7287 if (intel_crtc->plane)
7288 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7290 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7291 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7292 intel_ring_emit(ring, MI_NOOP);
7293 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7294 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7295 intel_ring_emit(ring, fb->pitches[0]);
7296 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7297 intel_ring_emit(ring, 0); /* aux display base address, unused */
7299 intel_mark_page_flip_active(intel_crtc);
7300 intel_ring_advance(ring);
7304 intel_unpin_fb_obj(obj);
7309 static int intel_gen3_queue_flip(struct drm_device *dev,
7310 struct drm_crtc *crtc,
7311 struct drm_framebuffer *fb,
7312 struct drm_i915_gem_object *obj)
7314 struct drm_i915_private *dev_priv = dev->dev_private;
7315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7317 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7320 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7324 ret = intel_ring_begin(ring, 6);
7328 if (intel_crtc->plane)
7329 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7331 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7332 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7333 intel_ring_emit(ring, MI_NOOP);
7334 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7335 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7336 intel_ring_emit(ring, fb->pitches[0]);
7337 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7338 intel_ring_emit(ring, MI_NOOP);
7340 intel_mark_page_flip_active(intel_crtc);
7341 intel_ring_advance(ring);
7345 intel_unpin_fb_obj(obj);
7350 static int intel_gen4_queue_flip(struct drm_device *dev,
7351 struct drm_crtc *crtc,
7352 struct drm_framebuffer *fb,
7353 struct drm_i915_gem_object *obj)
7355 struct drm_i915_private *dev_priv = dev->dev_private;
7356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7357 uint32_t pf, pipesrc;
7358 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7361 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7365 ret = intel_ring_begin(ring, 4);
7369 /* i965+ uses the linear or tiled offsets from the
7370 * Display Registers (which do not change across a page-flip)
7371 * so we need only reprogram the base address.
7373 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7374 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7375 intel_ring_emit(ring, fb->pitches[0]);
7376 intel_ring_emit(ring,
7377 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7380 /* XXX Enabling the panel-fitter across page-flip is so far
7381 * untested on non-native modes, so ignore it for now.
7382 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7385 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7386 intel_ring_emit(ring, pf | pipesrc);
7388 intel_mark_page_flip_active(intel_crtc);
7389 intel_ring_advance(ring);
7393 intel_unpin_fb_obj(obj);
7398 static int intel_gen6_queue_flip(struct drm_device *dev,
7399 struct drm_crtc *crtc,
7400 struct drm_framebuffer *fb,
7401 struct drm_i915_gem_object *obj)
7403 struct drm_i915_private *dev_priv = dev->dev_private;
7404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7405 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
7406 uint32_t pf, pipesrc;
7409 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7413 ret = intel_ring_begin(ring, 4);
7417 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7418 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7419 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
7420 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7422 /* Contrary to the suggestions in the documentation,
7423 * "Enable Panel Fitter" does not seem to be required when page
7424 * flipping with a non-native mode, and worse causes a normal
7426 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7429 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7430 intel_ring_emit(ring, pf | pipesrc);
7432 intel_mark_page_flip_active(intel_crtc);
7433 intel_ring_advance(ring);
7437 intel_unpin_fb_obj(obj);
7443 * On gen7 we currently use the blit ring because (in early silicon at least)
7444 * the render ring doesn't give us interrpts for page flip completion, which
7445 * means clients will hang after the first flip is queued. Fortunately the
7446 * blit ring generates interrupts properly, so use it instead.
7448 static int intel_gen7_queue_flip(struct drm_device *dev,
7449 struct drm_crtc *crtc,
7450 struct drm_framebuffer *fb,
7451 struct drm_i915_gem_object *obj)
7453 struct drm_i915_private *dev_priv = dev->dev_private;
7454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7455 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7456 uint32_t plane_bit = 0;
7459 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7463 switch(intel_crtc->plane) {
7465 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7468 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7471 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7474 WARN_ONCE(1, "unknown plane in flip command\n");
7479 ret = intel_ring_begin(ring, 4);
7483 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
7484 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
7485 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
7486 intel_ring_emit(ring, (MI_NOOP));
7488 intel_mark_page_flip_active(intel_crtc);
7489 intel_ring_advance(ring);
7493 intel_unpin_fb_obj(obj);
7498 static int intel_default_queue_flip(struct drm_device *dev,
7499 struct drm_crtc *crtc,
7500 struct drm_framebuffer *fb,
7501 struct drm_i915_gem_object *obj)
7506 static int intel_crtc_page_flip(struct drm_crtc *crtc,
7507 struct drm_framebuffer *fb,
7508 struct drm_pending_vblank_event *event)
7510 struct drm_device *dev = crtc->dev;
7511 struct drm_i915_private *dev_priv = dev->dev_private;
7512 struct drm_framebuffer *old_fb = crtc->fb;
7513 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
7514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7515 struct intel_unpin_work *work;
7516 unsigned long flags;
7519 /* Can't change pixel format via MI display flips. */
7520 if (fb->pixel_format != crtc->fb->pixel_format)
7524 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7525 * Note that pitch changes could also affect these register.
7527 if (INTEL_INFO(dev)->gen > 3 &&
7528 (fb->offsets[0] != crtc->fb->offsets[0] ||
7529 fb->pitches[0] != crtc->fb->pitches[0]))
7532 work = kzalloc(sizeof *work, GFP_KERNEL);
7536 work->event = event;
7538 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
7539 INIT_WORK(&work->work, intel_unpin_work_fn);
7541 ret = drm_vblank_get(dev, intel_crtc->pipe);
7545 /* We borrow the event spin lock for protecting unpin_work */
7546 spin_lock_irqsave(&dev->event_lock, flags);
7547 if (intel_crtc->unpin_work) {
7548 spin_unlock_irqrestore(&dev->event_lock, flags);
7550 drm_vblank_put(dev, intel_crtc->pipe);
7552 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
7555 intel_crtc->unpin_work = work;
7556 spin_unlock_irqrestore(&dev->event_lock, flags);
7558 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7559 flush_workqueue(dev_priv->wq);
7561 ret = i915_mutex_lock_interruptible(dev);
7565 /* Reference the objects for the scheduled work. */
7566 drm_gem_object_reference(&work->old_fb_obj->base);
7567 drm_gem_object_reference(&obj->base);
7571 work->pending_flip_obj = obj;
7573 work->enable_stall_check = true;
7575 atomic_inc(&intel_crtc->unpin_work_count);
7576 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
7578 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7580 goto cleanup_pending;
7582 intel_disable_fbc(dev);
7583 intel_mark_fb_busy(obj, NULL);
7584 mutex_unlock(&dev->struct_mutex);
7586 trace_i915_flip_request(intel_crtc->plane, obj);
7591 atomic_dec(&intel_crtc->unpin_work_count);
7593 drm_gem_object_unreference(&work->old_fb_obj->base);
7594 drm_gem_object_unreference(&obj->base);
7595 mutex_unlock(&dev->struct_mutex);
7598 spin_lock_irqsave(&dev->event_lock, flags);
7599 intel_crtc->unpin_work = NULL;
7600 spin_unlock_irqrestore(&dev->event_lock, flags);
7602 drm_vblank_put(dev, intel_crtc->pipe);
7609 static struct drm_crtc_helper_funcs intel_helper_funcs = {
7610 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7611 .load_lut = intel_crtc_load_lut,
7614 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7615 struct drm_crtc *crtc)
7617 struct drm_device *dev;
7618 struct drm_crtc *tmp;
7621 WARN(!crtc, "checking null crtc?\n");
7625 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7631 if (encoder->possible_crtcs & crtc_mask)
7637 * intel_modeset_update_staged_output_state
7639 * Updates the staged output configuration state, e.g. after we've read out the
7642 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7644 struct intel_encoder *encoder;
7645 struct intel_connector *connector;
7647 list_for_each_entry(connector, &dev->mode_config.connector_list,
7649 connector->new_encoder =
7650 to_intel_encoder(connector->base.encoder);
7653 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7656 to_intel_crtc(encoder->base.crtc);
7661 * intel_modeset_commit_output_state
7663 * This function copies the stage display pipe configuration to the real one.
7665 static void intel_modeset_commit_output_state(struct drm_device *dev)
7667 struct intel_encoder *encoder;
7668 struct intel_connector *connector;
7670 list_for_each_entry(connector, &dev->mode_config.connector_list,
7672 connector->base.encoder = &connector->new_encoder->base;
7675 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7677 encoder->base.crtc = &encoder->new_crtc->base;
7682 connected_sink_compute_bpp(struct intel_connector * connector,
7683 struct intel_crtc_config *pipe_config)
7685 int bpp = pipe_config->pipe_bpp;
7687 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7688 connector->base.base.id,
7689 drm_get_connector_name(&connector->base));
7691 /* Don't use an invalid EDID bpc value */
7692 if (connector->base.display_info.bpc &&
7693 connector->base.display_info.bpc * 3 < bpp) {
7694 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7695 bpp, connector->base.display_info.bpc*3);
7696 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7699 /* Clamp bpp to 8 on screens without EDID 1.4 */
7700 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7701 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7703 pipe_config->pipe_bpp = 24;
7708 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7709 struct drm_framebuffer *fb,
7710 struct intel_crtc_config *pipe_config)
7712 struct drm_device *dev = crtc->base.dev;
7713 struct intel_connector *connector;
7716 switch (fb->pixel_format) {
7718 bpp = 8*3; /* since we go through a colormap */
7720 case DRM_FORMAT_XRGB1555:
7721 case DRM_FORMAT_ARGB1555:
7722 /* checked in intel_framebuffer_init already */
7723 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7725 case DRM_FORMAT_RGB565:
7726 bpp = 6*3; /* min is 18bpp */
7728 case DRM_FORMAT_XBGR8888:
7729 case DRM_FORMAT_ABGR8888:
7730 /* checked in intel_framebuffer_init already */
7731 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7733 case DRM_FORMAT_XRGB8888:
7734 case DRM_FORMAT_ARGB8888:
7737 case DRM_FORMAT_XRGB2101010:
7738 case DRM_FORMAT_ARGB2101010:
7739 case DRM_FORMAT_XBGR2101010:
7740 case DRM_FORMAT_ABGR2101010:
7741 /* checked in intel_framebuffer_init already */
7742 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7746 /* TODO: gen4+ supports 16 bpc floating point, too. */
7748 DRM_DEBUG_KMS("unsupported depth\n");
7752 pipe_config->pipe_bpp = bpp;
7754 /* Clamp display bpp to EDID value */
7755 list_for_each_entry(connector, &dev->mode_config.connector_list,
7757 if (!connector->new_encoder ||
7758 connector->new_encoder->new_crtc != crtc)
7761 connected_sink_compute_bpp(connector, pipe_config);
7767 static void intel_dump_pipe_config(struct intel_crtc *crtc,
7768 struct intel_crtc_config *pipe_config,
7769 const char *context)
7771 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7772 context, pipe_name(crtc->pipe));
7774 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7775 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7776 pipe_config->pipe_bpp, pipe_config->dither);
7777 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7778 pipe_config->has_pch_encoder,
7779 pipe_config->fdi_lanes,
7780 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7781 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7782 pipe_config->fdi_m_n.tu);
7783 DRM_DEBUG_KMS("requested mode:\n");
7784 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7785 DRM_DEBUG_KMS("adjusted mode:\n");
7786 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7787 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7788 pipe_config->gmch_pfit.control,
7789 pipe_config->gmch_pfit.pgm_ratios,
7790 pipe_config->gmch_pfit.lvds_border_bits);
7791 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7792 pipe_config->pch_pfit.pos,
7793 pipe_config->pch_pfit.size);
7794 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
7797 static bool check_encoder_cloning(struct drm_crtc *crtc)
7799 int num_encoders = 0;
7800 bool uncloneable_encoders = false;
7801 struct intel_encoder *encoder;
7803 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7805 if (&encoder->new_crtc->base != crtc)
7809 if (!encoder->cloneable)
7810 uncloneable_encoders = true;
7813 return !(num_encoders > 1 && uncloneable_encoders);
7816 static struct intel_crtc_config *
7817 intel_modeset_pipe_config(struct drm_crtc *crtc,
7818 struct drm_framebuffer *fb,
7819 struct drm_display_mode *mode)
7821 struct drm_device *dev = crtc->dev;
7822 struct drm_encoder_helper_funcs *encoder_funcs;
7823 struct intel_encoder *encoder;
7824 struct intel_crtc_config *pipe_config;
7825 int plane_bpp, ret = -EINVAL;
7828 if (!check_encoder_cloning(crtc)) {
7829 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7830 return ERR_PTR(-EINVAL);
7833 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7835 return ERR_PTR(-ENOMEM);
7837 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7838 drm_mode_copy(&pipe_config->requested_mode, mode);
7839 pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
7841 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7842 * plane pixel format and any sink constraints into account. Returns the
7843 * source plane bpp so that dithering can be selected on mismatches
7844 * after encoders and crtc also have had their say. */
7845 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7851 /* Ensure the port clock defaults are reset when retrying. */
7852 pipe_config->port_clock = 0;
7853 pipe_config->pixel_multiplier = 1;
7855 /* Pass our mode to the connectors and the CRTC to give them a chance to
7856 * adjust it according to limitations or connector properties, and also
7857 * a chance to reject the mode entirely.
7859 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7862 if (&encoder->new_crtc->base != crtc)
7865 if (encoder->compute_config) {
7866 if (!(encoder->compute_config(encoder, pipe_config))) {
7867 DRM_DEBUG_KMS("Encoder config failure\n");
7874 encoder_funcs = encoder->base.helper_private;
7875 if (!(encoder_funcs->mode_fixup(&encoder->base,
7876 &pipe_config->requested_mode,
7877 &pipe_config->adjusted_mode))) {
7878 DRM_DEBUG_KMS("Encoder fixup failed\n");
7883 /* Set default port clock if not overwritten by the encoder. Needs to be
7884 * done afterwards in case the encoder adjusts the mode. */
7885 if (!pipe_config->port_clock)
7886 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7888 ret = intel_crtc_compute_config(crtc, pipe_config);
7890 DRM_DEBUG_KMS("CRTC fixup failed\n");
7895 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7900 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7905 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7906 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7907 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7912 return ERR_PTR(ret);
7915 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
7916 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7918 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7919 unsigned *prepare_pipes, unsigned *disable_pipes)
7921 struct intel_crtc *intel_crtc;
7922 struct drm_device *dev = crtc->dev;
7923 struct intel_encoder *encoder;
7924 struct intel_connector *connector;
7925 struct drm_crtc *tmp_crtc;
7927 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7929 /* Check which crtcs have changed outputs connected to them, these need
7930 * to be part of the prepare_pipes mask. We don't (yet) support global
7931 * modeset across multiple crtcs, so modeset_pipes will only have one
7932 * bit set at most. */
7933 list_for_each_entry(connector, &dev->mode_config.connector_list,
7935 if (connector->base.encoder == &connector->new_encoder->base)
7938 if (connector->base.encoder) {
7939 tmp_crtc = connector->base.encoder->crtc;
7941 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7944 if (connector->new_encoder)
7946 1 << connector->new_encoder->new_crtc->pipe;
7949 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7951 if (encoder->base.crtc == &encoder->new_crtc->base)
7954 if (encoder->base.crtc) {
7955 tmp_crtc = encoder->base.crtc;
7957 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7960 if (encoder->new_crtc)
7961 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7964 /* Check for any pipes that will be fully disabled ... */
7965 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7969 /* Don't try to disable disabled crtcs. */
7970 if (!intel_crtc->base.enabled)
7973 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7975 if (encoder->new_crtc == intel_crtc)
7980 *disable_pipes |= 1 << intel_crtc->pipe;
7984 /* set_mode is also used to update properties on life display pipes. */
7985 intel_crtc = to_intel_crtc(crtc);
7987 *prepare_pipes |= 1 << intel_crtc->pipe;
7990 * For simplicity do a full modeset on any pipe where the output routing
7991 * changed. We could be more clever, but that would require us to be
7992 * more careful with calling the relevant encoder->mode_set functions.
7995 *modeset_pipes = *prepare_pipes;
7997 /* ... and mask these out. */
7998 *modeset_pipes &= ~(*disable_pipes);
7999 *prepare_pipes &= ~(*disable_pipes);
8002 * HACK: We don't (yet) fully support global modesets. intel_set_config
8003 * obies this rule, but the modeset restore mode of
8004 * intel_modeset_setup_hw_state does not.
8006 *modeset_pipes &= 1 << intel_crtc->pipe;
8007 *prepare_pipes &= 1 << intel_crtc->pipe;
8009 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8010 *modeset_pipes, *prepare_pipes, *disable_pipes);
8013 static bool intel_crtc_in_use(struct drm_crtc *crtc)
8015 struct drm_encoder *encoder;
8016 struct drm_device *dev = crtc->dev;
8018 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8019 if (encoder->crtc == crtc)
8026 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8028 struct intel_encoder *intel_encoder;
8029 struct intel_crtc *intel_crtc;
8030 struct drm_connector *connector;
8032 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8034 if (!intel_encoder->base.crtc)
8037 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8039 if (prepare_pipes & (1 << intel_crtc->pipe))
8040 intel_encoder->connectors_active = false;
8043 intel_modeset_commit_output_state(dev);
8045 /* Update computed state. */
8046 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8048 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8051 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8052 if (!connector->encoder || !connector->encoder->crtc)
8055 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8057 if (prepare_pipes & (1 << intel_crtc->pipe)) {
8058 struct drm_property *dpms_property =
8059 dev->mode_config.dpms_property;
8061 connector->dpms = DRM_MODE_DPMS_ON;
8062 drm_object_property_set_value(&connector->base,
8066 intel_encoder = to_intel_encoder(connector->encoder);
8067 intel_encoder->connectors_active = true;
8073 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8074 list_for_each_entry((intel_crtc), \
8075 &(dev)->mode_config.crtc_list, \
8077 if (mask & (1 <<(intel_crtc)->pipe))
8080 intel_pipe_config_compare(struct drm_device *dev,
8081 struct intel_crtc_config *current_config,
8082 struct intel_crtc_config *pipe_config)
8084 #define PIPE_CONF_CHECK_I(name) \
8085 if (current_config->name != pipe_config->name) { \
8086 DRM_ERROR("mismatch in " #name " " \
8087 "(expected %i, found %i)\n", \
8088 current_config->name, \
8089 pipe_config->name); \
8093 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
8094 if ((current_config->name ^ pipe_config->name) & (mask)) { \
8095 DRM_ERROR("mismatch in " #name " " \
8096 "(expected %i, found %i)\n", \
8097 current_config->name & (mask), \
8098 pipe_config->name & (mask)); \
8102 #define PIPE_CONF_QUIRK(quirk) \
8103 ((current_config->quirks | pipe_config->quirks) & (quirk))
8105 PIPE_CONF_CHECK_I(cpu_transcoder);
8107 PIPE_CONF_CHECK_I(has_pch_encoder);
8108 PIPE_CONF_CHECK_I(fdi_lanes);
8109 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8110 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8111 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8112 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8113 PIPE_CONF_CHECK_I(fdi_m_n.tu);
8115 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8116 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8117 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8118 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8119 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8120 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8122 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8123 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8124 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8125 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8126 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8127 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8129 if (!HAS_PCH_SPLIT(dev))
8130 PIPE_CONF_CHECK_I(pixel_multiplier);
8132 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8133 DRM_MODE_FLAG_INTERLACE);
8135 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8136 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8137 DRM_MODE_FLAG_PHSYNC);
8138 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8139 DRM_MODE_FLAG_NHSYNC);
8140 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8141 DRM_MODE_FLAG_PVSYNC);
8142 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8143 DRM_MODE_FLAG_NVSYNC);
8146 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8147 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8149 PIPE_CONF_CHECK_I(gmch_pfit.control);
8150 /* pfit ratios are autocomputed by the hw on gen4+ */
8151 if (INTEL_INFO(dev)->gen < 4)
8152 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8153 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8154 PIPE_CONF_CHECK_I(pch_pfit.pos);
8155 PIPE_CONF_CHECK_I(pch_pfit.size);
8157 PIPE_CONF_CHECK_I(ips_enabled);
8159 #undef PIPE_CONF_CHECK_I
8160 #undef PIPE_CONF_CHECK_FLAGS
8161 #undef PIPE_CONF_QUIRK
8167 intel_modeset_check_state(struct drm_device *dev)
8169 drm_i915_private_t *dev_priv = dev->dev_private;
8170 struct intel_crtc *crtc;
8171 struct intel_encoder *encoder;
8172 struct intel_connector *connector;
8173 struct intel_crtc_config pipe_config;
8175 list_for_each_entry(connector, &dev->mode_config.connector_list,
8177 /* This also checks the encoder/connector hw state with the
8178 * ->get_hw_state callbacks. */
8179 intel_connector_check_state(connector);
8181 WARN(&connector->new_encoder->base != connector->base.encoder,
8182 "connector's staged encoder doesn't match current encoder\n");
8185 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8187 bool enabled = false;
8188 bool active = false;
8189 enum pipe pipe, tracked_pipe;
8191 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8192 encoder->base.base.id,
8193 drm_get_encoder_name(&encoder->base));
8195 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8196 "encoder's stage crtc doesn't match current crtc\n");
8197 WARN(encoder->connectors_active && !encoder->base.crtc,
8198 "encoder's active_connectors set, but no crtc\n");
8200 list_for_each_entry(connector, &dev->mode_config.connector_list,
8202 if (connector->base.encoder != &encoder->base)
8205 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8208 WARN(!!encoder->base.crtc != enabled,
8209 "encoder's enabled state mismatch "
8210 "(expected %i, found %i)\n",
8211 !!encoder->base.crtc, enabled);
8212 WARN(active && !encoder->base.crtc,
8213 "active encoder with no crtc\n");
8215 WARN(encoder->connectors_active != active,
8216 "encoder's computed active state doesn't match tracked active state "
8217 "(expected %i, found %i)\n", active, encoder->connectors_active);
8219 active = encoder->get_hw_state(encoder, &pipe);
8220 WARN(active != encoder->connectors_active,
8221 "encoder's hw state doesn't match sw tracking "
8222 "(expected %i, found %i)\n",
8223 encoder->connectors_active, active);
8225 if (!encoder->base.crtc)
8228 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8229 WARN(active && pipe != tracked_pipe,
8230 "active encoder's pipe doesn't match"
8231 "(expected %i, found %i)\n",
8232 tracked_pipe, pipe);
8236 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8238 bool enabled = false;
8239 bool active = false;
8241 memset(&pipe_config, 0, sizeof(pipe_config));
8243 DRM_DEBUG_KMS("[CRTC:%d]\n",
8244 crtc->base.base.id);
8246 WARN(crtc->active && !crtc->base.enabled,
8247 "active crtc, but not enabled in sw tracking\n");
8249 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8251 if (encoder->base.crtc != &crtc->base)
8254 if (encoder->connectors_active)
8258 WARN(active != crtc->active,
8259 "crtc's computed active state doesn't match tracked active state "
8260 "(expected %i, found %i)\n", active, crtc->active);
8261 WARN(enabled != crtc->base.enabled,
8262 "crtc's computed enabled state doesn't match tracked enabled state "
8263 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8265 active = dev_priv->display.get_pipe_config(crtc,
8267 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8269 if (encoder->base.crtc != &crtc->base)
8271 if (encoder->get_config)
8272 encoder->get_config(encoder, &pipe_config);
8275 WARN(crtc->active != active,
8276 "crtc active state doesn't match with hw state "
8277 "(expected %i, found %i)\n", crtc->active, active);
8280 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8281 WARN(1, "pipe state doesn't match!\n");
8282 intel_dump_pipe_config(crtc, &pipe_config,
8284 intel_dump_pipe_config(crtc, &crtc->config,
8290 static int __intel_set_mode(struct drm_crtc *crtc,
8291 struct drm_display_mode *mode,
8292 int x, int y, struct drm_framebuffer *fb)
8294 struct drm_device *dev = crtc->dev;
8295 drm_i915_private_t *dev_priv = dev->dev_private;
8296 struct drm_display_mode *saved_mode, *saved_hwmode;
8297 struct intel_crtc_config *pipe_config = NULL;
8298 struct intel_crtc *intel_crtc;
8299 unsigned disable_pipes, prepare_pipes, modeset_pipes;
8302 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
8305 saved_hwmode = saved_mode + 1;
8307 intel_modeset_affected_pipes(crtc, &modeset_pipes,
8308 &prepare_pipes, &disable_pipes);
8310 *saved_hwmode = crtc->hwmode;
8311 *saved_mode = crtc->mode;
8313 /* Hack: Because we don't (yet) support global modeset on multiple
8314 * crtcs, we don't keep track of the new mode for more than one crtc.
8315 * Hence simply check whether any bit is set in modeset_pipes in all the
8316 * pieces of code that are not yet converted to deal with mutliple crtcs
8317 * changing their mode at the same time. */
8318 if (modeset_pipes) {
8319 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
8320 if (IS_ERR(pipe_config)) {
8321 ret = PTR_ERR(pipe_config);
8326 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8330 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8331 intel_crtc_disable(&intel_crtc->base);
8333 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8334 if (intel_crtc->base.enabled)
8335 dev_priv->display.crtc_disable(&intel_crtc->base);
8338 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8339 * to set it here already despite that we pass it down the callchain.
8341 if (modeset_pipes) {
8343 /* mode_set/enable/disable functions rely on a correct pipe
8345 to_intel_crtc(crtc)->config = *pipe_config;
8348 /* Only after disabling all output pipelines that will be changed can we
8349 * update the the output configuration. */
8350 intel_modeset_update_state(dev, prepare_pipes);
8352 if (dev_priv->display.modeset_global_resources)
8353 dev_priv->display.modeset_global_resources(dev);
8355 /* Set up the DPLL and any encoders state that needs to adjust or depend
8358 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
8359 ret = intel_crtc_mode_set(&intel_crtc->base,
8365 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
8366 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8367 dev_priv->display.crtc_enable(&intel_crtc->base);
8369 if (modeset_pipes) {
8370 /* Store real post-adjustment hardware mode. */
8371 crtc->hwmode = pipe_config->adjusted_mode;
8373 /* Calculate and store various constants which
8374 * are later needed by vblank and swap-completion
8375 * timestamping. They are derived from true hwmode.
8377 drm_calc_timestamping_constants(crtc);
8380 /* FIXME: add subpixel order */
8382 if (ret && crtc->enabled) {
8383 crtc->hwmode = *saved_hwmode;
8384 crtc->mode = *saved_mode;
8393 int intel_set_mode(struct drm_crtc *crtc,
8394 struct drm_display_mode *mode,
8395 int x, int y, struct drm_framebuffer *fb)
8399 ret = __intel_set_mode(crtc, mode, x, y, fb);
8402 intel_modeset_check_state(crtc->dev);
8407 void intel_crtc_restore_mode(struct drm_crtc *crtc)
8409 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8412 #undef for_each_intel_crtc_masked
8414 static void intel_set_config_free(struct intel_set_config *config)
8419 kfree(config->save_connector_encoders);
8420 kfree(config->save_encoder_crtcs);
8424 static int intel_set_config_save_state(struct drm_device *dev,
8425 struct intel_set_config *config)
8427 struct drm_encoder *encoder;
8428 struct drm_connector *connector;
8431 config->save_encoder_crtcs =
8432 kcalloc(dev->mode_config.num_encoder,
8433 sizeof(struct drm_crtc *), GFP_KERNEL);
8434 if (!config->save_encoder_crtcs)
8437 config->save_connector_encoders =
8438 kcalloc(dev->mode_config.num_connector,
8439 sizeof(struct drm_encoder *), GFP_KERNEL);
8440 if (!config->save_connector_encoders)
8443 /* Copy data. Note that driver private data is not affected.
8444 * Should anything bad happen only the expected state is
8445 * restored, not the drivers personal bookkeeping.
8448 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
8449 config->save_encoder_crtcs[count++] = encoder->crtc;
8453 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8454 config->save_connector_encoders[count++] = connector->encoder;
8460 static void intel_set_config_restore_state(struct drm_device *dev,
8461 struct intel_set_config *config)
8463 struct intel_encoder *encoder;
8464 struct intel_connector *connector;
8468 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8470 to_intel_crtc(config->save_encoder_crtcs[count++]);
8474 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8475 connector->new_encoder =
8476 to_intel_encoder(config->save_connector_encoders[count++]);
8481 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8482 struct intel_set_config *config)
8485 /* We should be able to check here if the fb has the same properties
8486 * and then just flip_or_move it */
8487 if (set->crtc->fb != set->fb) {
8488 /* If we have no fb then treat it as a full mode set */
8489 if (set->crtc->fb == NULL) {
8490 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8491 config->mode_changed = true;
8492 } else if (set->fb == NULL) {
8493 config->mode_changed = true;
8494 } else if (set->fb->pixel_format !=
8495 set->crtc->fb->pixel_format) {
8496 config->mode_changed = true;
8498 config->fb_changed = true;
8501 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
8502 config->fb_changed = true;
8504 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8505 DRM_DEBUG_KMS("modes are different, full mode set\n");
8506 drm_mode_debug_printmodeline(&set->crtc->mode);
8507 drm_mode_debug_printmodeline(set->mode);
8508 config->mode_changed = true;
8513 intel_modeset_stage_output_state(struct drm_device *dev,
8514 struct drm_mode_set *set,
8515 struct intel_set_config *config)
8517 struct drm_crtc *new_crtc;
8518 struct intel_connector *connector;
8519 struct intel_encoder *encoder;
8522 /* The upper layers ensure that we either disable a crtc or have a list
8523 * of connectors. For paranoia, double-check this. */
8524 WARN_ON(!set->fb && (set->num_connectors != 0));
8525 WARN_ON(set->fb && (set->num_connectors == 0));
8528 list_for_each_entry(connector, &dev->mode_config.connector_list,
8530 /* Otherwise traverse passed in connector list and get encoders
8532 for (ro = 0; ro < set->num_connectors; ro++) {
8533 if (set->connectors[ro] == &connector->base) {
8534 connector->new_encoder = connector->encoder;
8539 /* If we disable the crtc, disable all its connectors. Also, if
8540 * the connector is on the changing crtc but not on the new
8541 * connector list, disable it. */
8542 if ((!set->fb || ro == set->num_connectors) &&
8543 connector->base.encoder &&
8544 connector->base.encoder->crtc == set->crtc) {
8545 connector->new_encoder = NULL;
8547 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8548 connector->base.base.id,
8549 drm_get_connector_name(&connector->base));
8553 if (&connector->new_encoder->base != connector->base.encoder) {
8554 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
8555 config->mode_changed = true;
8558 /* connector->new_encoder is now updated for all connectors. */
8560 /* Update crtc of enabled connectors. */
8562 list_for_each_entry(connector, &dev->mode_config.connector_list,
8564 if (!connector->new_encoder)
8567 new_crtc = connector->new_encoder->base.crtc;
8569 for (ro = 0; ro < set->num_connectors; ro++) {
8570 if (set->connectors[ro] == &connector->base)
8571 new_crtc = set->crtc;
8574 /* Make sure the new CRTC will work with the encoder */
8575 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8579 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8581 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8582 connector->base.base.id,
8583 drm_get_connector_name(&connector->base),
8587 /* Check for any encoders that needs to be disabled. */
8588 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8590 list_for_each_entry(connector,
8591 &dev->mode_config.connector_list,
8593 if (connector->new_encoder == encoder) {
8594 WARN_ON(!connector->new_encoder->new_crtc);
8599 encoder->new_crtc = NULL;
8601 /* Only now check for crtc changes so we don't miss encoders
8602 * that will be disabled. */
8603 if (&encoder->new_crtc->base != encoder->base.crtc) {
8604 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
8605 config->mode_changed = true;
8608 /* Now we've also updated encoder->new_crtc for all encoders. */
8613 static int intel_crtc_set_config(struct drm_mode_set *set)
8615 struct drm_device *dev;
8616 struct drm_mode_set save_set;
8617 struct intel_set_config *config;
8622 BUG_ON(!set->crtc->helper_private);
8624 /* Enforce sane interface api - has been abused by the fb helper. */
8625 BUG_ON(!set->mode && set->fb);
8626 BUG_ON(set->fb && set->num_connectors == 0);
8629 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8630 set->crtc->base.id, set->fb->base.id,
8631 (int)set->num_connectors, set->x, set->y);
8633 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
8636 dev = set->crtc->dev;
8639 config = kzalloc(sizeof(*config), GFP_KERNEL);
8643 ret = intel_set_config_save_state(dev, config);
8647 save_set.crtc = set->crtc;
8648 save_set.mode = &set->crtc->mode;
8649 save_set.x = set->crtc->x;
8650 save_set.y = set->crtc->y;
8651 save_set.fb = set->crtc->fb;
8653 /* Compute whether we need a full modeset, only an fb base update or no
8654 * change at all. In the future we might also check whether only the
8655 * mode changed, e.g. for LVDS where we only change the panel fitter in
8657 intel_set_config_compute_mode_changes(set, config);
8659 ret = intel_modeset_stage_output_state(dev, set, config);
8663 if (config->mode_changed) {
8664 ret = intel_set_mode(set->crtc, set->mode,
8665 set->x, set->y, set->fb);
8667 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8668 set->crtc->base.id, ret);
8671 } else if (config->fb_changed) {
8672 intel_crtc_wait_for_pending_flips(set->crtc);
8674 ret = intel_pipe_set_base(set->crtc,
8675 set->x, set->y, set->fb);
8678 intel_set_config_free(config);
8683 intel_set_config_restore_state(dev, config);
8685 /* Try to restore the config */
8686 if (config->mode_changed &&
8687 intel_set_mode(save_set.crtc, save_set.mode,
8688 save_set.x, save_set.y, save_set.fb))
8689 DRM_ERROR("failed to restore config after modeset failure\n");
8692 intel_set_config_free(config);
8696 static const struct drm_crtc_funcs intel_crtc_funcs = {
8697 .cursor_set = intel_crtc_cursor_set,
8698 .cursor_move = intel_crtc_cursor_move,
8699 .gamma_set = intel_crtc_gamma_set,
8700 .set_config = intel_crtc_set_config,
8701 .destroy = intel_crtc_destroy,
8702 .page_flip = intel_crtc_page_flip,
8705 static void intel_cpu_pll_init(struct drm_device *dev)
8708 intel_ddi_pll_init(dev);
8711 static void intel_pch_pll_init(struct drm_device *dev)
8713 drm_i915_private_t *dev_priv = dev->dev_private;
8716 if (dev_priv->num_pch_pll == 0) {
8717 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8721 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8722 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8723 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8724 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8728 static void intel_crtc_init(struct drm_device *dev, int pipe)
8730 drm_i915_private_t *dev_priv = dev->dev_private;
8731 struct intel_crtc *intel_crtc;
8734 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8735 if (intel_crtc == NULL)
8738 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8740 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
8741 for (i = 0; i < 256; i++) {
8742 intel_crtc->lut_r[i] = i;
8743 intel_crtc->lut_g[i] = i;
8744 intel_crtc->lut_b[i] = i;
8747 /* Swap pipes & planes for FBC on pre-965 */
8748 intel_crtc->pipe = pipe;
8749 intel_crtc->plane = pipe;
8750 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
8751 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
8752 intel_crtc->plane = !pipe;
8755 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8756 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8757 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8758 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8760 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
8763 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
8764 struct drm_file *file)
8766 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
8767 struct drm_mode_object *drmmode_obj;
8768 struct intel_crtc *crtc;
8770 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8773 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8774 DRM_MODE_OBJECT_CRTC);
8777 DRM_ERROR("no such CRTC id\n");
8781 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8782 pipe_from_crtc_id->pipe = crtc->pipe;
8787 static int intel_encoder_clones(struct intel_encoder *encoder)
8789 struct drm_device *dev = encoder->base.dev;
8790 struct intel_encoder *source_encoder;
8794 list_for_each_entry(source_encoder,
8795 &dev->mode_config.encoder_list, base.head) {
8797 if (encoder == source_encoder)
8798 index_mask |= (1 << entry);
8800 /* Intel hw has only one MUX where enocoders could be cloned. */
8801 if (encoder->cloneable && source_encoder->cloneable)
8802 index_mask |= (1 << entry);
8810 static bool has_edp_a(struct drm_device *dev)
8812 struct drm_i915_private *dev_priv = dev->dev_private;
8814 if (!IS_MOBILE(dev))
8817 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8821 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8827 static void intel_setup_outputs(struct drm_device *dev)
8829 struct drm_i915_private *dev_priv = dev->dev_private;
8830 struct intel_encoder *encoder;
8831 bool dpd_is_edp = false;
8834 has_lvds = intel_lvds_init(dev);
8835 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8836 /* disable the panel fitter on everything but LVDS */
8837 I915_WRITE(PFIT_CONTROL, 0);
8841 intel_crt_init(dev);
8846 /* Haswell uses DDI functions to detect digital outputs */
8847 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8848 /* DDI A only supports eDP */
8850 intel_ddi_init(dev, PORT_A);
8852 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8854 found = I915_READ(SFUSE_STRAP);
8856 if (found & SFUSE_STRAP_DDIB_DETECTED)
8857 intel_ddi_init(dev, PORT_B);
8858 if (found & SFUSE_STRAP_DDIC_DETECTED)
8859 intel_ddi_init(dev, PORT_C);
8860 if (found & SFUSE_STRAP_DDID_DETECTED)
8861 intel_ddi_init(dev, PORT_D);
8862 } else if (HAS_PCH_SPLIT(dev)) {
8864 dpd_is_edp = intel_dpd_is_edp(dev);
8867 intel_dp_init(dev, DP_A, PORT_A);
8869 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
8870 /* PCH SDVOB multiplex with HDMIB */
8871 found = intel_sdvo_init(dev, PCH_SDVOB, true);
8873 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
8874 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8875 intel_dp_init(dev, PCH_DP_B, PORT_B);
8878 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
8879 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
8881 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
8882 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
8884 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8885 intel_dp_init(dev, PCH_DP_C, PORT_C);
8887 if (I915_READ(PCH_DP_D) & DP_DETECTED)
8888 intel_dp_init(dev, PCH_DP_D, PORT_D);
8889 } else if (IS_VALLEYVIEW(dev)) {
8890 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8891 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8892 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
8894 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
8895 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8897 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8898 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
8900 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
8903 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8904 DRM_DEBUG_KMS("probing SDVOB\n");
8905 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
8906 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8907 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
8908 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
8911 if (!found && SUPPORTS_INTEGRATED_DP(dev))
8912 intel_dp_init(dev, DP_B, PORT_B);
8915 /* Before G4X SDVOC doesn't have its own detect register */
8917 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
8918 DRM_DEBUG_KMS("probing SDVOC\n");
8919 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
8922 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
8924 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8925 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
8926 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
8928 if (SUPPORTS_INTEGRATED_DP(dev))
8929 intel_dp_init(dev, DP_C, PORT_C);
8932 if (SUPPORTS_INTEGRATED_DP(dev) &&
8933 (I915_READ(DP_D) & DP_DETECTED))
8934 intel_dp_init(dev, DP_D, PORT_D);
8935 } else if (IS_GEN2(dev))
8936 intel_dvo_init(dev);
8938 if (SUPPORTS_TV(dev))
8941 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8942 encoder->base.possible_crtcs = encoder->crtc_mask;
8943 encoder->base.possible_clones =
8944 intel_encoder_clones(encoder);
8947 intel_init_pch_refclk(dev);
8949 drm_helper_move_panel_connectors_to_head(dev);
8952 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8954 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8956 drm_framebuffer_cleanup(fb);
8957 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
8962 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
8963 struct drm_file *file,
8964 unsigned int *handle)
8966 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8967 struct drm_i915_gem_object *obj = intel_fb->obj;
8969 return drm_gem_handle_create(file, &obj->base, handle);
8972 static const struct drm_framebuffer_funcs intel_fb_funcs = {
8973 .destroy = intel_user_framebuffer_destroy,
8974 .create_handle = intel_user_framebuffer_create_handle,
8977 int intel_framebuffer_init(struct drm_device *dev,
8978 struct intel_framebuffer *intel_fb,
8979 struct drm_mode_fb_cmd2 *mode_cmd,
8980 struct drm_i915_gem_object *obj)
8984 if (obj->tiling_mode == I915_TILING_Y) {
8985 DRM_DEBUG("hardware does not support tiling Y\n");
8989 if (mode_cmd->pitches[0] & 63) {
8990 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8991 mode_cmd->pitches[0]);
8995 /* FIXME <= Gen4 stride limits are bit unclear */
8996 if (mode_cmd->pitches[0] > 32768) {
8997 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8998 mode_cmd->pitches[0]);
9002 if (obj->tiling_mode != I915_TILING_NONE &&
9003 mode_cmd->pitches[0] != obj->stride) {
9004 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9005 mode_cmd->pitches[0], obj->stride);
9009 /* Reject formats not supported by any plane early. */
9010 switch (mode_cmd->pixel_format) {
9012 case DRM_FORMAT_RGB565:
9013 case DRM_FORMAT_XRGB8888:
9014 case DRM_FORMAT_ARGB8888:
9016 case DRM_FORMAT_XRGB1555:
9017 case DRM_FORMAT_ARGB1555:
9018 if (INTEL_INFO(dev)->gen > 3) {
9019 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9023 case DRM_FORMAT_XBGR8888:
9024 case DRM_FORMAT_ABGR8888:
9025 case DRM_FORMAT_XRGB2101010:
9026 case DRM_FORMAT_ARGB2101010:
9027 case DRM_FORMAT_XBGR2101010:
9028 case DRM_FORMAT_ABGR2101010:
9029 if (INTEL_INFO(dev)->gen < 4) {
9030 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9034 case DRM_FORMAT_YUYV:
9035 case DRM_FORMAT_UYVY:
9036 case DRM_FORMAT_YVYU:
9037 case DRM_FORMAT_VYUY:
9038 if (INTEL_INFO(dev)->gen < 5) {
9039 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
9044 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
9048 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9049 if (mode_cmd->offsets[0] != 0)
9052 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9053 intel_fb->obj = obj;
9055 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9057 DRM_ERROR("framebuffer init failed %d\n", ret);
9064 static struct drm_framebuffer *
9065 intel_user_framebuffer_create(struct drm_device *dev,
9066 struct drm_file *filp,
9067 struct drm_mode_fb_cmd2 *mode_cmd)
9069 struct drm_i915_gem_object *obj;
9071 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9072 mode_cmd->handles[0]));
9073 if (&obj->base == NULL)
9074 return ERR_PTR(-ENOENT);
9076 return intel_framebuffer_create(dev, mode_cmd, obj);
9079 static const struct drm_mode_config_funcs intel_mode_funcs = {
9080 .fb_create = intel_user_framebuffer_create,
9081 .output_poll_changed = intel_fb_output_poll_changed,
9084 /* Set up chip specific display functions */
9085 static void intel_init_display(struct drm_device *dev)
9087 struct drm_i915_private *dev_priv = dev->dev_private;
9089 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9090 dev_priv->display.find_dpll = g4x_find_best_dpll;
9091 else if (IS_VALLEYVIEW(dev))
9092 dev_priv->display.find_dpll = vlv_find_best_dpll;
9093 else if (IS_PINEVIEW(dev))
9094 dev_priv->display.find_dpll = pnv_find_best_dpll;
9096 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9099 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
9100 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
9101 dev_priv->display.crtc_enable = haswell_crtc_enable;
9102 dev_priv->display.crtc_disable = haswell_crtc_disable;
9103 dev_priv->display.off = haswell_crtc_off;
9104 dev_priv->display.update_plane = ironlake_update_plane;
9105 } else if (HAS_PCH_SPLIT(dev)) {
9106 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
9107 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
9108 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9109 dev_priv->display.crtc_disable = ironlake_crtc_disable;
9110 dev_priv->display.off = ironlake_crtc_off;
9111 dev_priv->display.update_plane = ironlake_update_plane;
9112 } else if (IS_VALLEYVIEW(dev)) {
9113 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9114 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9115 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9116 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9117 dev_priv->display.off = i9xx_crtc_off;
9118 dev_priv->display.update_plane = i9xx_update_plane;
9120 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9121 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9122 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9123 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9124 dev_priv->display.off = i9xx_crtc_off;
9125 dev_priv->display.update_plane = i9xx_update_plane;
9128 /* Returns the core display clock speed */
9129 if (IS_VALLEYVIEW(dev))
9130 dev_priv->display.get_display_clock_speed =
9131 valleyview_get_display_clock_speed;
9132 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
9133 dev_priv->display.get_display_clock_speed =
9134 i945_get_display_clock_speed;
9135 else if (IS_I915G(dev))
9136 dev_priv->display.get_display_clock_speed =
9137 i915_get_display_clock_speed;
9138 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
9139 dev_priv->display.get_display_clock_speed =
9140 i9xx_misc_get_display_clock_speed;
9141 else if (IS_I915GM(dev))
9142 dev_priv->display.get_display_clock_speed =
9143 i915gm_get_display_clock_speed;
9144 else if (IS_I865G(dev))
9145 dev_priv->display.get_display_clock_speed =
9146 i865_get_display_clock_speed;
9147 else if (IS_I85X(dev))
9148 dev_priv->display.get_display_clock_speed =
9149 i855_get_display_clock_speed;
9151 dev_priv->display.get_display_clock_speed =
9152 i830_get_display_clock_speed;
9154 if (HAS_PCH_SPLIT(dev)) {
9156 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
9157 dev_priv->display.write_eld = ironlake_write_eld;
9158 } else if (IS_GEN6(dev)) {
9159 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
9160 dev_priv->display.write_eld = ironlake_write_eld;
9161 } else if (IS_IVYBRIDGE(dev)) {
9162 /* FIXME: detect B0+ stepping and use auto training */
9163 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
9164 dev_priv->display.write_eld = ironlake_write_eld;
9165 dev_priv->display.modeset_global_resources =
9166 ivb_modeset_global_resources;
9167 } else if (IS_HASWELL(dev)) {
9168 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
9169 dev_priv->display.write_eld = haswell_write_eld;
9170 dev_priv->display.modeset_global_resources =
9171 haswell_modeset_global_resources;
9173 } else if (IS_G4X(dev)) {
9174 dev_priv->display.write_eld = g4x_write_eld;
9177 /* Default just returns -ENODEV to indicate unsupported */
9178 dev_priv->display.queue_flip = intel_default_queue_flip;
9180 switch (INTEL_INFO(dev)->gen) {
9182 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9186 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9191 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9195 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9198 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9204 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9205 * resume, or other times. This quirk makes sure that's the case for
9208 static void quirk_pipea_force(struct drm_device *dev)
9210 struct drm_i915_private *dev_priv = dev->dev_private;
9212 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9213 DRM_INFO("applying pipe a force quirk\n");
9217 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9219 static void quirk_ssc_force_disable(struct drm_device *dev)
9221 struct drm_i915_private *dev_priv = dev->dev_private;
9222 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9223 DRM_INFO("applying lvds SSC disable quirk\n");
9227 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9230 static void quirk_invert_brightness(struct drm_device *dev)
9232 struct drm_i915_private *dev_priv = dev->dev_private;
9233 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9234 DRM_INFO("applying inverted panel brightness quirk\n");
9237 struct intel_quirk {
9239 int subsystem_vendor;
9240 int subsystem_device;
9241 void (*hook)(struct drm_device *dev);
9244 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9245 struct intel_dmi_quirk {
9246 void (*hook)(struct drm_device *dev);
9247 const struct dmi_system_id (*dmi_id_list)[];
9250 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9252 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9256 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9258 .dmi_id_list = &(const struct dmi_system_id[]) {
9260 .callback = intel_dmi_reverse_brightness,
9261 .ident = "NCR Corporation",
9262 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9263 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9266 { } /* terminating entry */
9268 .hook = quirk_invert_brightness,
9272 static struct intel_quirk intel_quirks[] = {
9273 /* HP Mini needs pipe A force quirk (LP: #322104) */
9274 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
9276 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9277 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9279 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9280 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9282 /* 830/845 need to leave pipe A & dpll A up */
9283 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9284 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9286 /* Lenovo U160 cannot use SSC on LVDS */
9287 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
9289 /* Sony Vaio Y cannot use SSC on LVDS */
9290 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
9292 /* Acer Aspire 5734Z must invert backlight brightness */
9293 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
9295 /* Acer/eMachines G725 */
9296 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
9298 /* Acer/eMachines e725 */
9299 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
9301 /* Acer/Packard Bell NCL20 */
9302 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
9304 /* Acer Aspire 4736Z */
9305 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
9308 static void intel_init_quirks(struct drm_device *dev)
9310 struct pci_dev *d = dev->pdev;
9313 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9314 struct intel_quirk *q = &intel_quirks[i];
9316 if (d->device == q->device &&
9317 (d->subsystem_vendor == q->subsystem_vendor ||
9318 q->subsystem_vendor == PCI_ANY_ID) &&
9319 (d->subsystem_device == q->subsystem_device ||
9320 q->subsystem_device == PCI_ANY_ID))
9323 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9324 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9325 intel_dmi_quirks[i].hook(dev);
9329 /* Disable the VGA plane that we never use */
9330 static void i915_disable_vga(struct drm_device *dev)
9332 struct drm_i915_private *dev_priv = dev->dev_private;
9334 u32 vga_reg = i915_vgacntrl_reg(dev);
9336 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9337 outb(SR01, VGA_SR_INDEX);
9338 sr1 = inb(VGA_SR_DATA);
9339 outb(sr1 | 1<<5, VGA_SR_DATA);
9340 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9343 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9344 POSTING_READ(vga_reg);
9347 void intel_modeset_init_hw(struct drm_device *dev)
9349 intel_init_power_well(dev);
9351 intel_prepare_ddi(dev);
9353 intel_init_clock_gating(dev);
9355 mutex_lock(&dev->struct_mutex);
9356 intel_enable_gt_powersave(dev);
9357 mutex_unlock(&dev->struct_mutex);
9360 void intel_modeset_suspend_hw(struct drm_device *dev)
9362 intel_suspend_hw(dev);
9365 void intel_modeset_init(struct drm_device *dev)
9367 struct drm_i915_private *dev_priv = dev->dev_private;
9370 drm_mode_config_init(dev);
9372 dev->mode_config.min_width = 0;
9373 dev->mode_config.min_height = 0;
9375 dev->mode_config.preferred_depth = 24;
9376 dev->mode_config.prefer_shadow = 1;
9378 dev->mode_config.funcs = &intel_mode_funcs;
9380 intel_init_quirks(dev);
9384 if (INTEL_INFO(dev)->num_pipes == 0)
9387 intel_init_display(dev);
9390 dev->mode_config.max_width = 2048;
9391 dev->mode_config.max_height = 2048;
9392 } else if (IS_GEN3(dev)) {
9393 dev->mode_config.max_width = 4096;
9394 dev->mode_config.max_height = 4096;
9396 dev->mode_config.max_width = 8192;
9397 dev->mode_config.max_height = 8192;
9399 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
9401 DRM_DEBUG_KMS("%d display pipe%s available.\n",
9402 INTEL_INFO(dev)->num_pipes,
9403 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
9405 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
9406 intel_crtc_init(dev, i);
9407 for (j = 0; j < dev_priv->num_plane; j++) {
9408 ret = intel_plane_init(dev, i, j);
9410 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9411 pipe_name(i), sprite_name(i, j), ret);
9415 intel_cpu_pll_init(dev);
9416 intel_pch_pll_init(dev);
9418 /* Just disable it once at startup */
9419 i915_disable_vga(dev);
9420 intel_setup_outputs(dev);
9422 /* Just in case the BIOS is doing something questionable. */
9423 intel_disable_fbc(dev);
9427 intel_connector_break_all_links(struct intel_connector *connector)
9429 connector->base.dpms = DRM_MODE_DPMS_OFF;
9430 connector->base.encoder = NULL;
9431 connector->encoder->connectors_active = false;
9432 connector->encoder->base.crtc = NULL;
9435 static void intel_enable_pipe_a(struct drm_device *dev)
9437 struct intel_connector *connector;
9438 struct drm_connector *crt = NULL;
9439 struct intel_load_detect_pipe load_detect_temp;
9441 /* We can't just switch on the pipe A, we need to set things up with a
9442 * proper mode and output configuration. As a gross hack, enable pipe A
9443 * by enabling the load detect pipe once. */
9444 list_for_each_entry(connector,
9445 &dev->mode_config.connector_list,
9447 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9448 crt = &connector->base;
9456 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9457 intel_release_load_detect_pipe(crt, &load_detect_temp);
9463 intel_check_plane_mapping(struct intel_crtc *crtc)
9465 struct drm_device *dev = crtc->base.dev;
9466 struct drm_i915_private *dev_priv = dev->dev_private;
9469 if (INTEL_INFO(dev)->num_pipes == 1)
9472 reg = DSPCNTR(!crtc->plane);
9473 val = I915_READ(reg);
9475 if ((val & DISPLAY_PLANE_ENABLE) &&
9476 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9482 static void intel_sanitize_crtc(struct intel_crtc *crtc)
9484 struct drm_device *dev = crtc->base.dev;
9485 struct drm_i915_private *dev_priv = dev->dev_private;
9488 /* Clear any frame start delays used for debugging left by the BIOS */
9489 reg = PIPECONF(crtc->config.cpu_transcoder);
9490 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9492 /* We need to sanitize the plane -> pipe mapping first because this will
9493 * disable the crtc (and hence change the state) if it is wrong. Note
9494 * that gen4+ has a fixed plane -> pipe mapping. */
9495 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
9496 struct intel_connector *connector;
9499 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9500 crtc->base.base.id);
9502 /* Pipe has the wrong plane attached and the plane is active.
9503 * Temporarily change the plane mapping and disable everything
9505 plane = crtc->plane;
9506 crtc->plane = !plane;
9507 dev_priv->display.crtc_disable(&crtc->base);
9508 crtc->plane = plane;
9510 /* ... and break all links. */
9511 list_for_each_entry(connector, &dev->mode_config.connector_list,
9513 if (connector->encoder->base.crtc != &crtc->base)
9516 intel_connector_break_all_links(connector);
9519 WARN_ON(crtc->active);
9520 crtc->base.enabled = false;
9523 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9524 crtc->pipe == PIPE_A && !crtc->active) {
9525 /* BIOS forgot to enable pipe A, this mostly happens after
9526 * resume. Force-enable the pipe to fix this, the update_dpms
9527 * call below we restore the pipe to the right state, but leave
9528 * the required bits on. */
9529 intel_enable_pipe_a(dev);
9532 /* Adjust the state of the output pipe according to whether we
9533 * have active connectors/encoders. */
9534 intel_crtc_update_dpms(&crtc->base);
9536 if (crtc->active != crtc->base.enabled) {
9537 struct intel_encoder *encoder;
9539 /* This can happen either due to bugs in the get_hw_state
9540 * functions or because the pipe is force-enabled due to the
9542 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9544 crtc->base.enabled ? "enabled" : "disabled",
9545 crtc->active ? "enabled" : "disabled");
9547 crtc->base.enabled = crtc->active;
9549 /* Because we only establish the connector -> encoder ->
9550 * crtc links if something is active, this means the
9551 * crtc is now deactivated. Break the links. connector
9552 * -> encoder links are only establish when things are
9553 * actually up, hence no need to break them. */
9554 WARN_ON(crtc->active);
9556 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9557 WARN_ON(encoder->connectors_active);
9558 encoder->base.crtc = NULL;
9563 static void intel_sanitize_encoder(struct intel_encoder *encoder)
9565 struct intel_connector *connector;
9566 struct drm_device *dev = encoder->base.dev;
9568 /* We need to check both for a crtc link (meaning that the
9569 * encoder is active and trying to read from a pipe) and the
9570 * pipe itself being active. */
9571 bool has_active_crtc = encoder->base.crtc &&
9572 to_intel_crtc(encoder->base.crtc)->active;
9574 if (encoder->connectors_active && !has_active_crtc) {
9575 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9576 encoder->base.base.id,
9577 drm_get_encoder_name(&encoder->base));
9579 /* Connector is active, but has no active pipe. This is
9580 * fallout from our resume register restoring. Disable
9581 * the encoder manually again. */
9582 if (encoder->base.crtc) {
9583 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9584 encoder->base.base.id,
9585 drm_get_encoder_name(&encoder->base));
9586 encoder->disable(encoder);
9589 /* Inconsistent output/port/pipe state happens presumably due to
9590 * a bug in one of the get_hw_state functions. Or someplace else
9591 * in our code, like the register restore mess on resume. Clamp
9592 * things to off as a safer default. */
9593 list_for_each_entry(connector,
9594 &dev->mode_config.connector_list,
9596 if (connector->encoder != encoder)
9599 intel_connector_break_all_links(connector);
9602 /* Enabled encoders without active connectors will be fixed in
9603 * the crtc fixup. */
9606 void i915_redisable_vga(struct drm_device *dev)
9608 struct drm_i915_private *dev_priv = dev->dev_private;
9609 u32 vga_reg = i915_vgacntrl_reg(dev);
9611 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9612 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
9613 i915_disable_vga(dev);
9617 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9618 * and i915 state tracking structures. */
9619 void intel_modeset_setup_hw_state(struct drm_device *dev,
9622 struct drm_i915_private *dev_priv = dev->dev_private;
9624 struct drm_plane *plane;
9625 struct intel_crtc *crtc;
9626 struct intel_encoder *encoder;
9627 struct intel_connector *connector;
9629 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9631 memset(&crtc->config, 0, sizeof(crtc->config));
9633 crtc->active = dev_priv->display.get_pipe_config(crtc,
9636 crtc->base.enabled = crtc->active;
9638 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9640 crtc->active ? "enabled" : "disabled");
9644 intel_ddi_setup_hw_pll_state(dev);
9646 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9650 if (encoder->get_hw_state(encoder, &pipe)) {
9651 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9652 encoder->base.crtc = &crtc->base;
9653 if (encoder->get_config)
9654 encoder->get_config(encoder, &crtc->config);
9656 encoder->base.crtc = NULL;
9659 encoder->connectors_active = false;
9660 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9661 encoder->base.base.id,
9662 drm_get_encoder_name(&encoder->base),
9663 encoder->base.crtc ? "enabled" : "disabled",
9667 list_for_each_entry(connector, &dev->mode_config.connector_list,
9669 if (connector->get_hw_state(connector)) {
9670 connector->base.dpms = DRM_MODE_DPMS_ON;
9671 connector->encoder->connectors_active = true;
9672 connector->base.encoder = &connector->encoder->base;
9674 connector->base.dpms = DRM_MODE_DPMS_OFF;
9675 connector->base.encoder = NULL;
9677 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9678 connector->base.base.id,
9679 drm_get_connector_name(&connector->base),
9680 connector->base.encoder ? "enabled" : "disabled");
9683 /* HW state is read out, now we need to sanitize this mess. */
9684 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9686 intel_sanitize_encoder(encoder);
9689 for_each_pipe(pipe) {
9690 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9691 intel_sanitize_crtc(crtc);
9692 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
9695 if (force_restore) {
9697 * We need to use raw interfaces for restoring state to avoid
9698 * checking (bogus) intermediate states.
9700 for_each_pipe(pipe) {
9701 struct drm_crtc *crtc =
9702 dev_priv->pipe_to_crtc_mapping[pipe];
9704 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9707 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9708 intel_plane_restore(plane);
9710 i915_redisable_vga(dev);
9712 intel_modeset_update_staged_output_state(dev);
9715 intel_modeset_check_state(dev);
9717 drm_mode_config_reset(dev);
9720 void intel_modeset_gem_init(struct drm_device *dev)
9722 intel_modeset_init_hw(dev);
9724 intel_setup_overlay(dev);
9726 intel_modeset_setup_hw_state(dev, false);
9729 void intel_modeset_cleanup(struct drm_device *dev)
9731 struct drm_i915_private *dev_priv = dev->dev_private;
9732 struct drm_crtc *crtc;
9733 struct intel_crtc *intel_crtc;
9736 * Interrupts and polling as the first thing to avoid creating havoc.
9737 * Too much stuff here (turning of rps, connectors, ...) would
9738 * experience fancy races otherwise.
9740 drm_irq_uninstall(dev);
9741 cancel_work_sync(&dev_priv->hotplug_work);
9743 * Due to the hpd irq storm handling the hotplug work can re-arm the
9744 * poll handlers. Hence disable polling after hpd handling is shut down.
9746 drm_kms_helper_poll_fini(dev);
9748 mutex_lock(&dev->struct_mutex);
9750 intel_unregister_dsm_handler();
9752 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9753 /* Skip inactive CRTCs */
9757 intel_crtc = to_intel_crtc(crtc);
9758 intel_increase_pllclock(crtc);
9761 intel_disable_fbc(dev);
9763 intel_disable_gt_powersave(dev);
9765 ironlake_teardown_rc6(dev);
9767 mutex_unlock(&dev->struct_mutex);
9769 /* flush any delayed tasks or pending work */
9770 flush_scheduled_work();
9772 /* destroy backlight, if any, before the connectors */
9773 intel_panel_destroy_backlight(dev);
9775 drm_mode_config_cleanup(dev);
9777 intel_cleanup_overlay(dev);
9781 * Return which encoder is currently attached for connector.
9783 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
9785 return &intel_attached_encoder(connector)->base;
9788 void intel_connector_attach_encoder(struct intel_connector *connector,
9789 struct intel_encoder *encoder)
9791 connector->encoder = encoder;
9792 drm_mode_connector_attach_encoder(&connector->base,
9797 * set vga decode state - true == enable VGA decode
9799 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9801 struct drm_i915_private *dev_priv = dev->dev_private;
9804 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9806 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9808 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9809 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9813 #ifdef CONFIG_DEBUG_FS
9814 #include <linux/seq_file.h>
9816 struct intel_display_error_state {
9818 u32 power_well_driver;
9820 struct intel_cursor_error_state {
9825 } cursor[I915_MAX_PIPES];
9827 struct intel_pipe_error_state {
9828 enum transcoder cpu_transcoder;
9838 } pipe[I915_MAX_PIPES];
9840 struct intel_plane_error_state {
9848 } plane[I915_MAX_PIPES];
9851 struct intel_display_error_state *
9852 intel_display_capture_error_state(struct drm_device *dev)
9854 drm_i915_private_t *dev_priv = dev->dev_private;
9855 struct intel_display_error_state *error;
9856 enum transcoder cpu_transcoder;
9859 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9863 if (HAS_POWER_WELL(dev))
9864 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
9867 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9868 error->pipe[i].cpu_transcoder = cpu_transcoder;
9870 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9871 error->cursor[i].control = I915_READ(CURCNTR(i));
9872 error->cursor[i].position = I915_READ(CURPOS(i));
9873 error->cursor[i].base = I915_READ(CURBASE(i));
9875 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9876 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9877 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9880 error->plane[i].control = I915_READ(DSPCNTR(i));
9881 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9882 if (INTEL_INFO(dev)->gen <= 3) {
9883 error->plane[i].size = I915_READ(DSPSIZE(i));
9884 error->plane[i].pos = I915_READ(DSPPOS(i));
9886 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9887 error->plane[i].addr = I915_READ(DSPADDR(i));
9888 if (INTEL_INFO(dev)->gen >= 4) {
9889 error->plane[i].surface = I915_READ(DSPSURF(i));
9890 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9893 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
9894 error->pipe[i].source = I915_READ(PIPESRC(i));
9895 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9896 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9897 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9898 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9899 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9900 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
9903 /* In the code above we read the registers without checking if the power
9904 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
9905 * prevent the next I915_WRITE from detecting it and printing an error
9907 if (HAS_POWER_WELL(dev))
9908 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
9913 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
9916 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
9917 struct drm_device *dev,
9918 struct intel_display_error_state *error)
9922 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
9923 if (HAS_POWER_WELL(dev))
9924 err_printf(m, "PWR_WELL_CTL2: %08x\n",
9925 error->power_well_driver);
9927 err_printf(m, "Pipe [%d]:\n", i);
9928 err_printf(m, " CPU transcoder: %c\n",
9929 transcoder_name(error->pipe[i].cpu_transcoder));
9930 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9931 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
9932 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9933 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9934 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9935 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9936 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9937 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9939 err_printf(m, "Plane [%d]:\n", i);
9940 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
9941 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9942 if (INTEL_INFO(dev)->gen <= 3) {
9943 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
9944 err_printf(m, " POS: %08x\n", error->plane[i].pos);
9946 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9947 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9948 if (INTEL_INFO(dev)->gen >= 4) {
9949 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
9950 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9953 err_printf(m, "Cursor [%d]:\n", i);
9954 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9955 err_printf(m, " POS: %08x\n", error->cursor[i].position);
9956 err_printf(m, " BASE: %08x\n", error->cursor[i].base);