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.
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);
69 #define INTEL_P2_NUM 2
70 typedef struct intel_limit intel_limit_t;
72 intel_range_t dot, vco, n, m, m1, m2, p, p1;
74 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
75 int, int, intel_clock_t *, intel_clock_t *);
79 #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
82 intel_pch_rawclk(struct drm_device *dev)
84 struct drm_i915_private *dev_priv = dev->dev_private;
86 WARN_ON(!HAS_PCH_SPLIT(dev));
88 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
92 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
93 int target, int refclk, intel_clock_t *match_clock,
94 intel_clock_t *best_clock);
96 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
97 int target, int refclk, intel_clock_t *match_clock,
98 intel_clock_t *best_clock);
101 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
102 int target, int refclk, intel_clock_t *match_clock,
103 intel_clock_t *best_clock);
105 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
110 intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
111 int target, int refclk, intel_clock_t *match_clock,
112 intel_clock_t *best_clock);
114 static inline u32 /* units of 100MHz */
115 intel_fdi_link_freq(struct drm_device *dev)
118 struct drm_i915_private *dev_priv = dev->dev_private;
119 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
124 static const intel_limit_t intel_limits_i8xx_dvo = {
125 .dot = { .min = 25000, .max = 350000 },
126 .vco = { .min = 930000, .max = 1400000 },
127 .n = { .min = 3, .max = 16 },
128 .m = { .min = 96, .max = 140 },
129 .m1 = { .min = 18, .max = 26 },
130 .m2 = { .min = 6, .max = 16 },
131 .p = { .min = 4, .max = 128 },
132 .p1 = { .min = 2, .max = 33 },
133 .p2 = { .dot_limit = 165000,
134 .p2_slow = 4, .p2_fast = 2 },
135 .find_pll = intel_find_best_PLL,
138 static const intel_limit_t intel_limits_i8xx_lvds = {
139 .dot = { .min = 25000, .max = 350000 },
140 .vco = { .min = 930000, .max = 1400000 },
141 .n = { .min = 3, .max = 16 },
142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 1, .max = 6 },
147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 14, .p2_fast = 7 },
149 .find_pll = intel_find_best_PLL,
152 static const intel_limit_t intel_limits_i9xx_sdvo = {
153 .dot = { .min = 20000, .max = 400000 },
154 .vco = { .min = 1400000, .max = 2800000 },
155 .n = { .min = 1, .max = 6 },
156 .m = { .min = 70, .max = 120 },
157 .m1 = { .min = 10, .max = 22 },
158 .m2 = { .min = 5, .max = 9 },
159 .p = { .min = 5, .max = 80 },
160 .p1 = { .min = 1, .max = 8 },
161 .p2 = { .dot_limit = 200000,
162 .p2_slow = 10, .p2_fast = 5 },
163 .find_pll = intel_find_best_PLL,
166 static const intel_limit_t intel_limits_i9xx_lvds = {
167 .dot = { .min = 20000, .max = 400000 },
168 .vco = { .min = 1400000, .max = 2800000 },
169 .n = { .min = 1, .max = 6 },
170 .m = { .min = 70, .max = 120 },
171 .m1 = { .min = 10, .max = 22 },
172 .m2 = { .min = 5, .max = 9 },
173 .p = { .min = 7, .max = 98 },
174 .p1 = { .min = 1, .max = 8 },
175 .p2 = { .dot_limit = 112000,
176 .p2_slow = 14, .p2_fast = 7 },
177 .find_pll = intel_find_best_PLL,
181 static const intel_limit_t intel_limits_g4x_sdvo = {
182 .dot = { .min = 25000, .max = 270000 },
183 .vco = { .min = 1750000, .max = 3500000},
184 .n = { .min = 1, .max = 4 },
185 .m = { .min = 104, .max = 138 },
186 .m1 = { .min = 17, .max = 23 },
187 .m2 = { .min = 5, .max = 11 },
188 .p = { .min = 10, .max = 30 },
189 .p1 = { .min = 1, .max = 3},
190 .p2 = { .dot_limit = 270000,
194 .find_pll = intel_g4x_find_best_PLL,
197 static const intel_limit_t intel_limits_g4x_hdmi = {
198 .dot = { .min = 22000, .max = 400000 },
199 .vco = { .min = 1750000, .max = 3500000},
200 .n = { .min = 1, .max = 4 },
201 .m = { .min = 104, .max = 138 },
202 .m1 = { .min = 16, .max = 23 },
203 .m2 = { .min = 5, .max = 11 },
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8},
206 .p2 = { .dot_limit = 165000,
207 .p2_slow = 10, .p2_fast = 5 },
208 .find_pll = intel_g4x_find_best_PLL,
211 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
212 .dot = { .min = 20000, .max = 115000 },
213 .vco = { .min = 1750000, .max = 3500000 },
214 .n = { .min = 1, .max = 3 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 28, .max = 112 },
219 .p1 = { .min = 2, .max = 8 },
220 .p2 = { .dot_limit = 0,
221 .p2_slow = 14, .p2_fast = 14
223 .find_pll = intel_g4x_find_best_PLL,
226 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
227 .dot = { .min = 80000, .max = 224000 },
228 .vco = { .min = 1750000, .max = 3500000 },
229 .n = { .min = 1, .max = 3 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 17, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 14, .max = 42 },
234 .p1 = { .min = 2, .max = 6 },
235 .p2 = { .dot_limit = 0,
236 .p2_slow = 7, .p2_fast = 7
238 .find_pll = intel_g4x_find_best_PLL,
241 static const intel_limit_t intel_limits_g4x_display_port = {
242 .dot = { .min = 161670, .max = 227000 },
243 .vco = { .min = 1750000, .max = 3500000},
244 .n = { .min = 1, .max = 2 },
245 .m = { .min = 97, .max = 108 },
246 .m1 = { .min = 0x10, .max = 0x12 },
247 .m2 = { .min = 0x05, .max = 0x06 },
248 .p = { .min = 10, .max = 20 },
249 .p1 = { .min = 1, .max = 2},
250 .p2 = { .dot_limit = 0,
251 .p2_slow = 10, .p2_fast = 10 },
252 .find_pll = intel_find_pll_g4x_dp,
255 static const intel_limit_t intel_limits_pineview_sdvo = {
256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
258 /* Pineview's Ncounter is a ring counter */
259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
261 /* Pineview only has one combined m divider, which we treat as m2. */
262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
268 .find_pll = intel_find_best_PLL,
271 static const intel_limit_t intel_limits_pineview_lvds = {
272 .dot = { .min = 20000, .max = 400000 },
273 .vco = { .min = 1700000, .max = 3500000 },
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
276 .m1 = { .min = 0, .max = 0 },
277 .m2 = { .min = 0, .max = 254 },
278 .p = { .min = 7, .max = 112 },
279 .p1 = { .min = 1, .max = 8 },
280 .p2 = { .dot_limit = 112000,
281 .p2_slow = 14, .p2_fast = 14 },
282 .find_pll = intel_find_best_PLL,
285 /* Ironlake / Sandybridge
287 * We calculate clock using (register_value + 2) for N/M1/M2, so here
288 * the range value for them is (actual_value - 2).
290 static const intel_limit_t intel_limits_ironlake_dac = {
291 .dot = { .min = 25000, .max = 350000 },
292 .vco = { .min = 1760000, .max = 3510000 },
293 .n = { .min = 1, .max = 5 },
294 .m = { .min = 79, .max = 127 },
295 .m1 = { .min = 12, .max = 22 },
296 .m2 = { .min = 5, .max = 9 },
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
299 .p2 = { .dot_limit = 225000,
300 .p2_slow = 10, .p2_fast = 5 },
301 .find_pll = intel_g4x_find_best_PLL,
304 static const intel_limit_t intel_limits_ironlake_single_lvds = {
305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 3 },
308 .m = { .min = 79, .max = 118 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 28, .max = 112 },
312 .p1 = { .min = 2, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 14, .p2_fast = 14 },
315 .find_pll = intel_g4x_find_best_PLL,
318 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 127 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 14, .max = 56 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 7, .p2_fast = 7 },
329 .find_pll = intel_g4x_find_best_PLL,
332 /* LVDS 100mhz refclk limits. */
333 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
341 .p1 = { .min = 2, .max = 8 },
342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
344 .find_pll = intel_g4x_find_best_PLL,
347 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 3 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 14, .max = 42 },
355 .p1 = { .min = 2, .max = 6 },
356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 7, .p2_fast = 7 },
358 .find_pll = intel_g4x_find_best_PLL,
361 static const intel_limit_t intel_limits_ironlake_display_port = {
362 .dot = { .min = 25000, .max = 350000 },
363 .vco = { .min = 1760000, .max = 3510000},
364 .n = { .min = 1, .max = 2 },
365 .m = { .min = 81, .max = 90 },
366 .m1 = { .min = 12, .max = 22 },
367 .m2 = { .min = 5, .max = 9 },
368 .p = { .min = 10, .max = 20 },
369 .p1 = { .min = 1, .max = 2},
370 .p2 = { .dot_limit = 0,
371 .p2_slow = 10, .p2_fast = 10 },
372 .find_pll = intel_find_pll_ironlake_dp,
375 static const intel_limit_t intel_limits_vlv_dac = {
376 .dot = { .min = 25000, .max = 270000 },
377 .vco = { .min = 4000000, .max = 6000000 },
378 .n = { .min = 1, .max = 7 },
379 .m = { .min = 22, .max = 450 }, /* guess */
380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
382 .p = { .min = 10, .max = 30 },
383 .p1 = { .min = 2, .max = 3 },
384 .p2 = { .dot_limit = 270000,
385 .p2_slow = 2, .p2_fast = 20 },
386 .find_pll = intel_vlv_find_best_pll,
389 static const intel_limit_t intel_limits_vlv_hdmi = {
390 .dot = { .min = 20000, .max = 165000 },
391 .vco = { .min = 4000000, .max = 5994000},
392 .n = { .min = 1, .max = 7 },
393 .m = { .min = 60, .max = 300 }, /* guess */
394 .m1 = { .min = 2, .max = 3 },
395 .m2 = { .min = 11, .max = 156 },
396 .p = { .min = 10, .max = 30 },
397 .p1 = { .min = 2, .max = 3 },
398 .p2 = { .dot_limit = 270000,
399 .p2_slow = 2, .p2_fast = 20 },
400 .find_pll = intel_vlv_find_best_pll,
403 static const intel_limit_t intel_limits_vlv_dp = {
404 .dot = { .min = 25000, .max = 270000 },
405 .vco = { .min = 4000000, .max = 6000000 },
406 .n = { .min = 1, .max = 7 },
407 .m = { .min = 22, .max = 450 },
408 .m1 = { .min = 2, .max = 3 },
409 .m2 = { .min = 11, .max = 156 },
410 .p = { .min = 10, .max = 30 },
411 .p1 = { .min = 2, .max = 3 },
412 .p2 = { .dot_limit = 270000,
413 .p2_slow = 2, .p2_fast = 20 },
414 .find_pll = intel_vlv_find_best_pll,
417 u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
419 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
421 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
422 DRM_ERROR("DPIO idle wait timed out\n");
426 I915_WRITE(DPIO_REG, reg);
427 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
429 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
430 DRM_ERROR("DPIO read wait timed out\n");
434 return I915_READ(DPIO_DATA);
437 static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
440 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
442 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
443 DRM_ERROR("DPIO idle wait timed out\n");
447 I915_WRITE(DPIO_DATA, val);
448 I915_WRITE(DPIO_REG, reg);
449 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
451 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
452 DRM_ERROR("DPIO write wait timed out\n");
455 static void vlv_init_dpio(struct drm_device *dev)
457 struct drm_i915_private *dev_priv = dev->dev_private;
459 /* Reset the DPIO config */
460 I915_WRITE(DPIO_CTL, 0);
461 POSTING_READ(DPIO_CTL);
462 I915_WRITE(DPIO_CTL, 1);
463 POSTING_READ(DPIO_CTL);
466 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
469 struct drm_device *dev = crtc->dev;
470 const intel_limit_t *limit;
472 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
473 if (intel_is_dual_link_lvds(dev)) {
474 /* LVDS dual channel */
475 if (refclk == 100000)
476 limit = &intel_limits_ironlake_dual_lvds_100m;
478 limit = &intel_limits_ironlake_dual_lvds;
480 if (refclk == 100000)
481 limit = &intel_limits_ironlake_single_lvds_100m;
483 limit = &intel_limits_ironlake_single_lvds;
485 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
486 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
487 limit = &intel_limits_ironlake_display_port;
489 limit = &intel_limits_ironlake_dac;
494 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
496 struct drm_device *dev = crtc->dev;
497 const intel_limit_t *limit;
499 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
500 if (intel_is_dual_link_lvds(dev))
501 /* LVDS with dual channel */
502 limit = &intel_limits_g4x_dual_channel_lvds;
504 /* LVDS with dual channel */
505 limit = &intel_limits_g4x_single_channel_lvds;
506 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
507 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
508 limit = &intel_limits_g4x_hdmi;
509 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
510 limit = &intel_limits_g4x_sdvo;
511 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
512 limit = &intel_limits_g4x_display_port;
513 } else /* The option is for other outputs */
514 limit = &intel_limits_i9xx_sdvo;
519 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
521 struct drm_device *dev = crtc->dev;
522 const intel_limit_t *limit;
524 if (HAS_PCH_SPLIT(dev))
525 limit = intel_ironlake_limit(crtc, refclk);
526 else if (IS_G4X(dev)) {
527 limit = intel_g4x_limit(crtc);
528 } else if (IS_PINEVIEW(dev)) {
529 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
530 limit = &intel_limits_pineview_lvds;
532 limit = &intel_limits_pineview_sdvo;
533 } else if (IS_VALLEYVIEW(dev)) {
534 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
535 limit = &intel_limits_vlv_dac;
536 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
537 limit = &intel_limits_vlv_hdmi;
539 limit = &intel_limits_vlv_dp;
540 } else if (!IS_GEN2(dev)) {
541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
542 limit = &intel_limits_i9xx_lvds;
544 limit = &intel_limits_i9xx_sdvo;
546 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
547 limit = &intel_limits_i8xx_lvds;
549 limit = &intel_limits_i8xx_dvo;
554 /* m1 is reserved as 0 in Pineview, n is a ring counter */
555 static void pineview_clock(int refclk, intel_clock_t *clock)
557 clock->m = clock->m2 + 2;
558 clock->p = clock->p1 * clock->p2;
559 clock->vco = refclk * clock->m / clock->n;
560 clock->dot = clock->vco / clock->p;
563 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
565 if (IS_PINEVIEW(dev)) {
566 pineview_clock(refclk, clock);
569 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
570 clock->p = clock->p1 * clock->p2;
571 clock->vco = refclk * clock->m / (clock->n + 2);
572 clock->dot = clock->vco / clock->p;
576 * Returns whether any output on the specified pipe is of the specified type
578 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
580 struct drm_device *dev = crtc->dev;
581 struct intel_encoder *encoder;
583 for_each_encoder_on_crtc(dev, crtc, encoder)
584 if (encoder->type == type)
590 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
592 * Returns whether the given set of divisors are valid for a given refclk with
593 * the given connectors.
596 static bool intel_PLL_is_valid(struct drm_device *dev,
597 const intel_limit_t *limit,
598 const intel_clock_t *clock)
600 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
601 INTELPllInvalid("p1 out of range\n");
602 if (clock->p < limit->p.min || limit->p.max < clock->p)
603 INTELPllInvalid("p out of range\n");
604 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
605 INTELPllInvalid("m2 out of range\n");
606 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
607 INTELPllInvalid("m1 out of range\n");
608 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
609 INTELPllInvalid("m1 <= m2\n");
610 if (clock->m < limit->m.min || limit->m.max < clock->m)
611 INTELPllInvalid("m out of range\n");
612 if (clock->n < limit->n.min || limit->n.max < clock->n)
613 INTELPllInvalid("n out of range\n");
614 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
615 INTELPllInvalid("vco out of range\n");
616 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
617 * connector, etc., rather than just a single range.
619 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
620 INTELPllInvalid("dot out of range\n");
626 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
627 int target, int refclk, intel_clock_t *match_clock,
628 intel_clock_t *best_clock)
631 struct drm_device *dev = crtc->dev;
635 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
637 * For LVDS just rely on its current settings for dual-channel.
638 * We haven't figured out how to reliably set up different
639 * single/dual channel state, if we even can.
641 if (intel_is_dual_link_lvds(dev))
642 clock.p2 = limit->p2.p2_fast;
644 clock.p2 = limit->p2.p2_slow;
646 if (target < limit->p2.dot_limit)
647 clock.p2 = limit->p2.p2_slow;
649 clock.p2 = limit->p2.p2_fast;
652 memset(best_clock, 0, sizeof(*best_clock));
654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
658 /* m1 is always 0 in Pineview */
659 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
667 intel_clock(dev, refclk, &clock);
668 if (!intel_PLL_is_valid(dev, limit,
672 clock.p != match_clock->p)
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
685 return (err != target);
689 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
693 struct drm_device *dev = crtc->dev;
697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
704 if (HAS_PCH_SPLIT(dev))
708 if (intel_is_dual_link_lvds(dev))
709 clock.p2 = limit->p2.p2_fast;
711 clock.p2 = limit->p2.p2_slow;
713 if (target < limit->p2.dot_limit)
714 clock.p2 = limit->p2.p2_slow;
716 clock.p2 = limit->p2.p2_fast;
719 memset(best_clock, 0, sizeof(*best_clock));
720 max_n = limit->n.max;
721 /* based on hardware requirement, prefer smaller n to precision */
722 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
723 /* based on hardware requirement, prefere larger m1,m2 */
724 for (clock.m1 = limit->m1.max;
725 clock.m1 >= limit->m1.min; clock.m1--) {
726 for (clock.m2 = limit->m2.max;
727 clock.m2 >= limit->m2.min; clock.m2--) {
728 for (clock.p1 = limit->p1.max;
729 clock.p1 >= limit->p1.min; clock.p1--) {
732 intel_clock(dev, refclk, &clock);
733 if (!intel_PLL_is_valid(dev, limit,
737 clock.p != match_clock->p)
740 this_err = abs(clock.dot - target);
741 if (this_err < err_most) {
755 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
756 int target, int refclk, intel_clock_t *match_clock,
757 intel_clock_t *best_clock)
759 struct drm_device *dev = crtc->dev;
762 if (target < 200000) {
775 intel_clock(dev, refclk, &clock);
776 memcpy(best_clock, &clock, sizeof(intel_clock_t));
780 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
782 intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
783 int target, int refclk, intel_clock_t *match_clock,
784 intel_clock_t *best_clock)
787 if (target < 200000) {
800 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
801 clock.p = (clock.p1 * clock.p2);
802 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
804 memcpy(best_clock, &clock, sizeof(intel_clock_t));
808 intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
812 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
814 u32 updrate, minupdate, fracbits, p;
815 unsigned long bestppm, ppm, absppm;
819 dotclk = target * 1000;
822 fastclk = dotclk / (2*100);
826 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
827 bestm1 = bestm2 = bestp1 = bestp2 = 0;
829 /* based on hardware requirement, prefer smaller n to precision */
830 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
831 updrate = refclk / n;
832 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
833 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
837 /* based on hardware requirement, prefer bigger m1,m2 values */
838 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
839 m2 = (((2*(fastclk * p * n / m1 )) +
840 refclk) / (2*refclk));
843 if (vco >= limit->vco.min && vco < limit->vco.max) {
844 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
845 absppm = (ppm > 0) ? ppm : (-ppm);
846 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
850 if (absppm < bestppm - 10) {
867 best_clock->n = bestn;
868 best_clock->m1 = bestm1;
869 best_clock->m2 = bestm2;
870 best_clock->p1 = bestp1;
871 best_clock->p2 = bestp2;
876 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
882 return intel_crtc->cpu_transcoder;
885 static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
887 struct drm_i915_private *dev_priv = dev->dev_private;
888 u32 frame, frame_reg = PIPEFRAME(pipe);
890 frame = I915_READ(frame_reg);
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
893 DRM_DEBUG_KMS("vblank wait timed out\n");
897 * intel_wait_for_vblank - wait for vblank on a given pipe
899 * @pipe: pipe to wait for
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
904 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
906 struct drm_i915_private *dev_priv = dev->dev_private;
907 int pipestat_reg = PIPESTAT(pipe);
909 if (INTEL_INFO(dev)->gen >= 5) {
910 ironlake_wait_for_vblank(dev, pipe);
914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
930 /* Wait for vblank interrupt bit to set */
931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
934 DRM_DEBUG_KMS("vblank wait timed out\n");
938 * intel_wait_for_pipe_off - wait for pipe to turn off
940 * @pipe: pipe to wait for
942 * After disabling a pipe, we can't wait for vblank in the usual way,
943 * spinning on the vblank interrupt status bit, since we won't actually
944 * see an interrupt when the pipe is disabled.
947 * wait for the pipe register state bit to turn off
950 * wait for the display line value to settle (it usually
951 * ends up stopping at the start of the next frame).
954 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
956 struct drm_i915_private *dev_priv = dev->dev_private;
957 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
960 if (INTEL_INFO(dev)->gen >= 4) {
961 int reg = PIPECONF(cpu_transcoder);
963 /* Wait for the Pipe State to go off */
964 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
966 WARN(1, "pipe_off wait timed out\n");
968 u32 last_line, line_mask;
969 int reg = PIPEDSL(pipe);
970 unsigned long timeout = jiffies + msecs_to_jiffies(100);
973 line_mask = DSL_LINEMASK_GEN2;
975 line_mask = DSL_LINEMASK_GEN3;
977 /* Wait for the display line to settle */
979 last_line = I915_READ(reg) & line_mask;
981 } while (((I915_READ(reg) & line_mask) != last_line) &&
982 time_after(timeout, jiffies));
983 if (time_after(jiffies, timeout))
984 WARN(1, "pipe_off wait timed out\n");
989 * ibx_digital_port_connected - is the specified port connected?
990 * @dev_priv: i915 private structure
991 * @port: the port to test
993 * Returns true if @port is connected, false otherwise.
995 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
996 struct intel_digital_port *port)
1000 if (HAS_PCH_IBX(dev_priv->dev)) {
1001 switch(port->port) {
1003 bit = SDE_PORTB_HOTPLUG;
1006 bit = SDE_PORTC_HOTPLUG;
1009 bit = SDE_PORTD_HOTPLUG;
1015 switch(port->port) {
1017 bit = SDE_PORTB_HOTPLUG_CPT;
1020 bit = SDE_PORTC_HOTPLUG_CPT;
1023 bit = SDE_PORTD_HOTPLUG_CPT;
1030 return I915_READ(SDEISR) & bit;
1033 static const char *state_string(bool enabled)
1035 return enabled ? "on" : "off";
1038 /* Only for pre-ILK configs */
1039 static void assert_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
1047 val = I915_READ(reg);
1048 cur_state = !!(val & DPLL_VCO_ENABLE);
1049 WARN(cur_state != state,
1050 "PLL state assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
1053 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
1054 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
1057 static void assert_pch_pll(struct drm_i915_private *dev_priv,
1058 struct intel_pch_pll *pll,
1059 struct intel_crtc *crtc,
1065 if (HAS_PCH_LPT(dev_priv->dev)) {
1066 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1071 "asserting PCH PLL %s with no PLL\n", state_string(state)))
1074 val = I915_READ(pll->pll_reg);
1075 cur_state = !!(val & DPLL_VCO_ENABLE);
1076 WARN(cur_state != state,
1077 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1078 pll->pll_reg, state_string(state), state_string(cur_state), val);
1080 /* Make sure the selected PLL is correctly attached to the transcoder */
1081 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
1084 pch_dpll = I915_READ(PCH_DPLL_SEL);
1085 cur_state = pll->pll_reg == _PCH_DPLL_B;
1086 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1087 "PLL[%d] not attached to this transcoder %d: %08x\n",
1088 cur_state, crtc->pipe, pch_dpll)) {
1089 cur_state = !!(val >> (4*crtc->pipe + 3));
1090 WARN(cur_state != state,
1091 "PLL[%d] not %s on this transcoder %d: %08x\n",
1092 pll->pll_reg == _PCH_DPLL_B,
1093 state_string(state),
1099 #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1100 #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
1102 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1103 enum pipe pipe, bool state)
1108 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1111 if (HAS_DDI(dev_priv->dev)) {
1112 /* DDI does not have a specific FDI_TX register */
1113 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1114 val = I915_READ(reg);
1115 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1117 reg = FDI_TX_CTL(pipe);
1118 val = I915_READ(reg);
1119 cur_state = !!(val & FDI_TX_ENABLE);
1121 WARN(cur_state != state,
1122 "FDI TX state assertion failure (expected %s, current %s)\n",
1123 state_string(state), state_string(cur_state));
1125 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1126 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1128 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
1135 reg = FDI_RX_CTL(pipe);
1136 val = I915_READ(reg);
1137 cur_state = !!(val & FDI_RX_ENABLE);
1138 WARN(cur_state != state,
1139 "FDI RX state assertion failure (expected %s, current %s)\n",
1140 state_string(state), state_string(cur_state));
1142 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1143 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1145 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1151 /* ILK FDI PLL is always enabled */
1152 if (dev_priv->info->gen == 5)
1155 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1156 if (HAS_DDI(dev_priv->dev))
1159 reg = FDI_TX_CTL(pipe);
1160 val = I915_READ(reg);
1161 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1164 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1170 reg = FDI_RX_CTL(pipe);
1171 val = I915_READ(reg);
1172 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1175 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1178 int pp_reg, lvds_reg;
1180 enum pipe panel_pipe = PIPE_A;
1183 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1184 pp_reg = PCH_PP_CONTROL;
1185 lvds_reg = PCH_LVDS;
1187 pp_reg = PP_CONTROL;
1191 val = I915_READ(pp_reg);
1192 if (!(val & PANEL_POWER_ON) ||
1193 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1196 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1197 panel_pipe = PIPE_B;
1199 WARN(panel_pipe == pipe && locked,
1200 "panel assertion failure, pipe %c regs locked\n",
1204 void assert_pipe(struct drm_i915_private *dev_priv,
1205 enum pipe pipe, bool state)
1210 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1213 /* if we need the pipe A quirk it must be always on */
1214 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1217 reg = PIPECONF(cpu_transcoder);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & PIPECONF_ENABLE);
1220 WARN(cur_state != state,
1221 "pipe %c assertion failure (expected %s, current %s)\n",
1222 pipe_name(pipe), state_string(state), state_string(cur_state));
1225 static void assert_plane(struct drm_i915_private *dev_priv,
1226 enum plane plane, bool state)
1232 reg = DSPCNTR(plane);
1233 val = I915_READ(reg);
1234 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1235 WARN(cur_state != state,
1236 "plane %c assertion failure (expected %s, current %s)\n",
1237 plane_name(plane), state_string(state), state_string(cur_state));
1240 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1241 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1243 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1250 /* Planes are fixed to pipes on ILK+ */
1251 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1252 reg = DSPCNTR(pipe);
1253 val = I915_READ(reg);
1254 WARN((val & DISPLAY_PLANE_ENABLE),
1255 "plane %c assertion failure, should be disabled but not\n",
1260 /* Need to check both planes against the pipe */
1261 for (i = 0; i < 2; i++) {
1263 val = I915_READ(reg);
1264 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1265 DISPPLANE_SEL_PIPE_SHIFT;
1266 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1267 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1268 plane_name(i), pipe_name(pipe));
1272 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1277 if (HAS_PCH_LPT(dev_priv->dev)) {
1278 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1282 val = I915_READ(PCH_DREF_CONTROL);
1283 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1284 DREF_SUPERSPREAD_SOURCE_MASK));
1285 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1288 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 reg = TRANSCONF(pipe);
1296 val = I915_READ(reg);
1297 enabled = !!(val & TRANS_ENABLE);
1299 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1303 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, u32 port_sel, u32 val)
1306 if ((val & DP_PORT_EN) == 0)
1309 if (HAS_PCH_CPT(dev_priv->dev)) {
1310 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1311 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1312 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1315 if ((val & DP_PIPE_MASK) != (pipe << 30))
1321 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, u32 val)
1324 if ((val & PORT_ENABLE) == 0)
1327 if (HAS_PCH_CPT(dev_priv->dev)) {
1328 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1331 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1337 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1338 enum pipe pipe, u32 val)
1340 if ((val & LVDS_PORT_EN) == 0)
1343 if (HAS_PCH_CPT(dev_priv->dev)) {
1344 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1347 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1353 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe, u32 val)
1356 if ((val & ADPA_DAC_ENABLE) == 0)
1358 if (HAS_PCH_CPT(dev_priv->dev)) {
1359 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1362 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1368 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, int reg, u32 port_sel)
1371 u32 val = I915_READ(reg);
1372 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1373 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1374 reg, pipe_name(pipe));
1376 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1377 && (val & DP_PIPEB_SELECT),
1378 "IBX PCH dp port still using transcoder B\n");
1381 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1382 enum pipe pipe, int reg)
1384 u32 val = I915_READ(reg);
1385 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1386 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1387 reg, pipe_name(pipe));
1389 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1390 && (val & SDVO_PIPE_B_SELECT),
1391 "IBX PCH hdmi port still using transcoder B\n");
1394 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1400 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1401 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1402 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1405 val = I915_READ(reg);
1406 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1407 "PCH VGA enabled on transcoder %c, should be disabled\n",
1411 val = I915_READ(reg);
1412 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1413 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1416 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1417 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1418 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1422 * intel_enable_pll - enable a PLL
1423 * @dev_priv: i915 private structure
1424 * @pipe: pipe PLL to enable
1426 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1427 * make sure the PLL reg is writable first though, since the panel write
1428 * protect mechanism may be enabled.
1430 * Note! This is for pre-ILK only.
1432 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
1434 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1439 /* No really, not for ILK+ */
1440 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
1442 /* PLL is protected by panel, make sure we can write it */
1443 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1444 assert_panel_unlocked(dev_priv, pipe);
1447 val = I915_READ(reg);
1448 val |= DPLL_VCO_ENABLE;
1450 /* We do this three times for luck */
1451 I915_WRITE(reg, val);
1453 udelay(150); /* wait for warmup */
1454 I915_WRITE(reg, val);
1456 udelay(150); /* wait for warmup */
1457 I915_WRITE(reg, val);
1459 udelay(150); /* wait for warmup */
1463 * intel_disable_pll - disable a PLL
1464 * @dev_priv: i915 private structure
1465 * @pipe: pipe PLL to disable
1467 * Disable the PLL for @pipe, making sure the pipe is off first.
1469 * Note! This is for pre-ILK only.
1471 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1476 /* Don't disable pipe A or pipe A PLLs if needed */
1477 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1480 /* Make sure the pipe isn't still relying on us */
1481 assert_pipe_disabled(dev_priv, pipe);
1484 val = I915_READ(reg);
1485 val &= ~DPLL_VCO_ENABLE;
1486 I915_WRITE(reg, val);
1492 intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1494 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
1496 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1498 DRM_ERROR("timeout waiting for SBI to become ready\n");
1502 I915_WRITE(SBI_ADDR,
1504 I915_WRITE(SBI_DATA,
1506 I915_WRITE(SBI_CTL_STAT,
1510 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1512 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
1518 intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1520 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
1522 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
1524 DRM_ERROR("timeout waiting for SBI to become ready\n");
1528 I915_WRITE(SBI_ADDR,
1530 I915_WRITE(SBI_CTL_STAT,
1534 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
1536 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
1540 return I915_READ(SBI_DATA);
1544 * ironlake_enable_pch_pll - enable PCH PLL
1545 * @dev_priv: i915 private structure
1546 * @pipe: pipe PLL to enable
1548 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1549 * drives the transcoder clock.
1551 static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
1553 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1554 struct intel_pch_pll *pll;
1558 /* PCH PLLs only available on ILK, SNB and IVB */
1559 BUG_ON(dev_priv->info->gen < 5);
1560 pll = intel_crtc->pch_pll;
1564 if (WARN_ON(pll->refcount == 0))
1567 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1568 pll->pll_reg, pll->active, pll->on,
1569 intel_crtc->base.base.id);
1571 /* PCH refclock must be enabled first */
1572 assert_pch_refclk_enabled(dev_priv);
1574 if (pll->active++ && pll->on) {
1575 assert_pch_pll_enabled(dev_priv, pll, NULL);
1579 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1582 val = I915_READ(reg);
1583 val |= DPLL_VCO_ENABLE;
1584 I915_WRITE(reg, val);
1591 static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
1593 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1594 struct intel_pch_pll *pll = intel_crtc->pch_pll;
1598 /* PCH only available on ILK+ */
1599 BUG_ON(dev_priv->info->gen < 5);
1603 if (WARN_ON(pll->refcount == 0))
1606 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1607 pll->pll_reg, pll->active, pll->on,
1608 intel_crtc->base.base.id);
1610 if (WARN_ON(pll->active == 0)) {
1611 assert_pch_pll_disabled(dev_priv, pll, NULL);
1615 if (--pll->active) {
1616 assert_pch_pll_enabled(dev_priv, pll, NULL);
1620 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
1622 /* Make sure transcoder isn't still depending on us */
1623 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
1626 val = I915_READ(reg);
1627 val &= ~DPLL_VCO_ENABLE;
1628 I915_WRITE(reg, val);
1635 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1638 struct drm_device *dev = dev_priv->dev;
1639 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1640 uint32_t reg, val, pipeconf_val;
1642 /* PCH only available on ILK+ */
1643 BUG_ON(dev_priv->info->gen < 5);
1645 /* Make sure PCH DPLL is enabled */
1646 assert_pch_pll_enabled(dev_priv,
1647 to_intel_crtc(crtc)->pch_pll,
1648 to_intel_crtc(crtc));
1650 /* FDI must be feeding us bits for PCH ports */
1651 assert_fdi_tx_enabled(dev_priv, pipe);
1652 assert_fdi_rx_enabled(dev_priv, pipe);
1654 if (HAS_PCH_CPT(dev)) {
1655 /* Workaround: Set the timing override bit before enabling the
1656 * pch transcoder. */
1657 reg = TRANS_CHICKEN2(pipe);
1658 val = I915_READ(reg);
1659 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1660 I915_WRITE(reg, val);
1663 reg = TRANSCONF(pipe);
1664 val = I915_READ(reg);
1665 pipeconf_val = I915_READ(PIPECONF(pipe));
1667 if (HAS_PCH_IBX(dev_priv->dev)) {
1669 * make the BPC in transcoder be consistent with
1670 * that in pipeconf reg.
1672 val &= ~PIPECONF_BPC_MASK;
1673 val |= pipeconf_val & PIPECONF_BPC_MASK;
1676 val &= ~TRANS_INTERLACE_MASK;
1677 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1678 if (HAS_PCH_IBX(dev_priv->dev) &&
1679 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1680 val |= TRANS_LEGACY_INTERLACED_ILK;
1682 val |= TRANS_INTERLACED;
1684 val |= TRANS_PROGRESSIVE;
1686 I915_WRITE(reg, val | TRANS_ENABLE);
1687 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1688 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1691 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1692 enum transcoder cpu_transcoder)
1694 u32 val, pipeconf_val;
1696 /* PCH only available on ILK+ */
1697 BUG_ON(dev_priv->info->gen < 5);
1699 /* FDI must be feeding us bits for PCH ports */
1700 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1701 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1703 /* Workaround: set timing override bit. */
1704 val = I915_READ(_TRANSA_CHICKEN2);
1705 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1706 I915_WRITE(_TRANSA_CHICKEN2, val);
1709 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1711 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1712 PIPECONF_INTERLACED_ILK)
1713 val |= TRANS_INTERLACED;
1715 val |= TRANS_PROGRESSIVE;
1717 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
1718 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1719 DRM_ERROR("Failed to enable PCH transcoder\n");
1722 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1725 struct drm_device *dev = dev_priv->dev;
1728 /* FDI relies on the transcoder */
1729 assert_fdi_tx_disabled(dev_priv, pipe);
1730 assert_fdi_rx_disabled(dev_priv, pipe);
1732 /* Ports must be off as well */
1733 assert_pch_ports_disabled(dev_priv, pipe);
1735 reg = TRANSCONF(pipe);
1736 val = I915_READ(reg);
1737 val &= ~TRANS_ENABLE;
1738 I915_WRITE(reg, val);
1739 /* wait for PCH transcoder off, transcoder state */
1740 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1741 DRM_ERROR("failed to disable transcoder %d\n", pipe);
1743 if (!HAS_PCH_IBX(dev)) {
1744 /* Workaround: Clear the timing override chicken bit again. */
1745 reg = TRANS_CHICKEN2(pipe);
1746 val = I915_READ(reg);
1747 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1748 I915_WRITE(reg, val);
1752 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1756 val = I915_READ(_TRANSACONF);
1757 val &= ~TRANS_ENABLE;
1758 I915_WRITE(_TRANSACONF, val);
1759 /* wait for PCH transcoder off, transcoder state */
1760 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1761 DRM_ERROR("Failed to disable PCH transcoder\n");
1763 /* Workaround: clear timing override bit. */
1764 val = I915_READ(_TRANSA_CHICKEN2);
1765 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1766 I915_WRITE(_TRANSA_CHICKEN2, val);
1770 * intel_enable_pipe - enable a pipe, asserting requirements
1771 * @dev_priv: i915 private structure
1772 * @pipe: pipe to enable
1773 * @pch_port: on ILK+, is this pipe driving a PCH port or not
1775 * Enable @pipe, making sure that various hardware specific requirements
1776 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1778 * @pipe should be %PIPE_A or %PIPE_B.
1780 * Will wait until the pipe is actually running (i.e. first vblank) before
1783 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1786 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1788 enum pipe pch_transcoder;
1792 if (HAS_PCH_LPT(dev_priv->dev))
1793 pch_transcoder = TRANSCODER_A;
1795 pch_transcoder = pipe;
1798 * A pipe without a PLL won't actually be able to drive bits from
1799 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1802 if (!HAS_PCH_SPLIT(dev_priv->dev))
1803 assert_pll_enabled(dev_priv, pipe);
1806 /* if driving the PCH, we need FDI enabled */
1807 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1808 assert_fdi_tx_pll_enabled(dev_priv,
1809 (enum pipe) cpu_transcoder);
1811 /* FIXME: assert CPU port conditions for SNB+ */
1814 reg = PIPECONF(cpu_transcoder);
1815 val = I915_READ(reg);
1816 if (val & PIPECONF_ENABLE)
1819 I915_WRITE(reg, val | PIPECONF_ENABLE);
1820 intel_wait_for_vblank(dev_priv->dev, pipe);
1824 * intel_disable_pipe - disable a pipe, asserting requirements
1825 * @dev_priv: i915 private structure
1826 * @pipe: pipe to disable
1828 * Disable @pipe, making sure that various hardware specific requirements
1829 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1831 * @pipe should be %PIPE_A or %PIPE_B.
1833 * Will wait until the pipe has shut down before returning.
1835 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1838 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1844 * Make sure planes won't keep trying to pump pixels to us,
1845 * or we might hang the display.
1847 assert_planes_disabled(dev_priv, pipe);
1849 /* Don't disable pipe A or pipe A PLLs if needed */
1850 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1853 reg = PIPECONF(cpu_transcoder);
1854 val = I915_READ(reg);
1855 if ((val & PIPECONF_ENABLE) == 0)
1858 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1859 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1863 * Plane regs are double buffered, going from enabled->disabled needs a
1864 * trigger in order to latch. The display address reg provides this.
1866 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1869 if (dev_priv->info->gen >= 4)
1870 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1872 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1876 * intel_enable_plane - enable a display plane on a given pipe
1877 * @dev_priv: i915 private structure
1878 * @plane: plane to enable
1879 * @pipe: pipe being fed
1881 * Enable @plane on @pipe, making sure that @pipe is running first.
1883 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1884 enum plane plane, enum pipe pipe)
1889 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1890 assert_pipe_enabled(dev_priv, pipe);
1892 reg = DSPCNTR(plane);
1893 val = I915_READ(reg);
1894 if (val & DISPLAY_PLANE_ENABLE)
1897 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1898 intel_flush_display_plane(dev_priv, plane);
1899 intel_wait_for_vblank(dev_priv->dev, pipe);
1903 * intel_disable_plane - disable a display plane
1904 * @dev_priv: i915 private structure
1905 * @plane: plane to disable
1906 * @pipe: pipe consuming the data
1908 * Disable @plane; should be an independent operation.
1910 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1911 enum plane plane, enum pipe pipe)
1916 reg = DSPCNTR(plane);
1917 val = I915_READ(reg);
1918 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1921 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1922 intel_flush_display_plane(dev_priv, plane);
1923 intel_wait_for_vblank(dev_priv->dev, pipe);
1927 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1928 struct drm_i915_gem_object *obj,
1929 struct intel_ring_buffer *pipelined)
1931 struct drm_i915_private *dev_priv = dev->dev_private;
1935 switch (obj->tiling_mode) {
1936 case I915_TILING_NONE:
1937 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1938 alignment = 128 * 1024;
1939 else if (INTEL_INFO(dev)->gen >= 4)
1940 alignment = 4 * 1024;
1942 alignment = 64 * 1024;
1945 /* pin() will align the object as required by fence */
1949 /* FIXME: Is this true? */
1950 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1956 dev_priv->mm.interruptible = false;
1957 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1959 goto err_interruptible;
1961 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1962 * fence, whereas 965+ only requires a fence if using
1963 * framebuffer compression. For simplicity, we always install
1964 * a fence as the cost is not that onerous.
1966 ret = i915_gem_object_get_fence(obj);
1970 i915_gem_object_pin_fence(obj);
1972 dev_priv->mm.interruptible = true;
1976 i915_gem_object_unpin(obj);
1978 dev_priv->mm.interruptible = true;
1982 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1984 i915_gem_object_unpin_fence(obj);
1985 i915_gem_object_unpin(obj);
1988 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1989 * is assumed to be a power-of-two. */
1990 unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1994 int tile_rows, tiles;
1998 tiles = *x / (512/bpp);
2001 return tile_rows * pitch * 8 + tiles * 4096;
2004 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2007 struct drm_device *dev = crtc->dev;
2008 struct drm_i915_private *dev_priv = dev->dev_private;
2009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2010 struct intel_framebuffer *intel_fb;
2011 struct drm_i915_gem_object *obj;
2012 int plane = intel_crtc->plane;
2013 unsigned long linear_offset;
2022 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2026 intel_fb = to_intel_framebuffer(fb);
2027 obj = intel_fb->obj;
2029 reg = DSPCNTR(plane);
2030 dspcntr = I915_READ(reg);
2031 /* Mask out pixel format bits in case we change it */
2032 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2033 switch (fb->pixel_format) {
2035 dspcntr |= DISPPLANE_8BPP;
2037 case DRM_FORMAT_XRGB1555:
2038 case DRM_FORMAT_ARGB1555:
2039 dspcntr |= DISPPLANE_BGRX555;
2041 case DRM_FORMAT_RGB565:
2042 dspcntr |= DISPPLANE_BGRX565;
2044 case DRM_FORMAT_XRGB8888:
2045 case DRM_FORMAT_ARGB8888:
2046 dspcntr |= DISPPLANE_BGRX888;
2048 case DRM_FORMAT_XBGR8888:
2049 case DRM_FORMAT_ABGR8888:
2050 dspcntr |= DISPPLANE_RGBX888;
2052 case DRM_FORMAT_XRGB2101010:
2053 case DRM_FORMAT_ARGB2101010:
2054 dspcntr |= DISPPLANE_BGRX101010;
2056 case DRM_FORMAT_XBGR2101010:
2057 case DRM_FORMAT_ABGR2101010:
2058 dspcntr |= DISPPLANE_RGBX101010;
2061 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
2065 if (INTEL_INFO(dev)->gen >= 4) {
2066 if (obj->tiling_mode != I915_TILING_NONE)
2067 dspcntr |= DISPPLANE_TILED;
2069 dspcntr &= ~DISPPLANE_TILED;
2072 I915_WRITE(reg, dspcntr);
2074 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2076 if (INTEL_INFO(dev)->gen >= 4) {
2077 intel_crtc->dspaddr_offset =
2078 intel_gen4_compute_offset_xtiled(&x, &y,
2079 fb->bits_per_pixel / 8,
2081 linear_offset -= intel_crtc->dspaddr_offset;
2083 intel_crtc->dspaddr_offset = linear_offset;
2086 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2087 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2088 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2089 if (INTEL_INFO(dev)->gen >= 4) {
2090 I915_MODIFY_DISPBASE(DSPSURF(plane),
2091 obj->gtt_offset + intel_crtc->dspaddr_offset);
2092 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2093 I915_WRITE(DSPLINOFF(plane), linear_offset);
2095 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
2101 static int ironlake_update_plane(struct drm_crtc *crtc,
2102 struct drm_framebuffer *fb, int x, int y)
2104 struct drm_device *dev = crtc->dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2107 struct intel_framebuffer *intel_fb;
2108 struct drm_i915_gem_object *obj;
2109 int plane = intel_crtc->plane;
2110 unsigned long linear_offset;
2120 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2124 intel_fb = to_intel_framebuffer(fb);
2125 obj = intel_fb->obj;
2127 reg = DSPCNTR(plane);
2128 dspcntr = I915_READ(reg);
2129 /* Mask out pixel format bits in case we change it */
2130 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2131 switch (fb->pixel_format) {
2133 dspcntr |= DISPPLANE_8BPP;
2135 case DRM_FORMAT_RGB565:
2136 dspcntr |= DISPPLANE_BGRX565;
2138 case DRM_FORMAT_XRGB8888:
2139 case DRM_FORMAT_ARGB8888:
2140 dspcntr |= DISPPLANE_BGRX888;
2142 case DRM_FORMAT_XBGR8888:
2143 case DRM_FORMAT_ABGR8888:
2144 dspcntr |= DISPPLANE_RGBX888;
2146 case DRM_FORMAT_XRGB2101010:
2147 case DRM_FORMAT_ARGB2101010:
2148 dspcntr |= DISPPLANE_BGRX101010;
2150 case DRM_FORMAT_XBGR2101010:
2151 case DRM_FORMAT_ABGR2101010:
2152 dspcntr |= DISPPLANE_RGBX101010;
2155 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
2159 if (obj->tiling_mode != I915_TILING_NONE)
2160 dspcntr |= DISPPLANE_TILED;
2162 dspcntr &= ~DISPPLANE_TILED;
2165 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2167 I915_WRITE(reg, dspcntr);
2169 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
2170 intel_crtc->dspaddr_offset =
2171 intel_gen4_compute_offset_xtiled(&x, &y,
2172 fb->bits_per_pixel / 8,
2174 linear_offset -= intel_crtc->dspaddr_offset;
2176 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2177 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
2178 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2179 I915_MODIFY_DISPBASE(DSPSURF(plane),
2180 obj->gtt_offset + intel_crtc->dspaddr_offset);
2181 if (IS_HASWELL(dev)) {
2182 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2184 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2185 I915_WRITE(DSPLINOFF(plane), linear_offset);
2192 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2194 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2195 int x, int y, enum mode_set_atomic state)
2197 struct drm_device *dev = crtc->dev;
2198 struct drm_i915_private *dev_priv = dev->dev_private;
2200 if (dev_priv->display.disable_fbc)
2201 dev_priv->display.disable_fbc(dev);
2202 intel_increase_pllclock(crtc);
2204 return dev_priv->display.update_plane(crtc, fb, x, y);
2208 intel_finish_fb(struct drm_framebuffer *old_fb)
2210 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2211 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2212 bool was_interruptible = dev_priv->mm.interruptible;
2215 wait_event(dev_priv->pending_flip_queue,
2216 atomic_read(&dev_priv->mm.wedged) ||
2217 atomic_read(&obj->pending_flip) == 0);
2219 /* Big Hammer, we also need to ensure that any pending
2220 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2221 * current scanout is retired before unpinning the old
2224 * This should only fail upon a hung GPU, in which case we
2225 * can safely continue.
2227 dev_priv->mm.interruptible = false;
2228 ret = i915_gem_object_finish_gpu(obj);
2229 dev_priv->mm.interruptible = was_interruptible;
2234 static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2236 struct drm_device *dev = crtc->dev;
2237 struct drm_i915_master_private *master_priv;
2238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2240 if (!dev->primary->master)
2243 master_priv = dev->primary->master->driver_priv;
2244 if (!master_priv->sarea_priv)
2247 switch (intel_crtc->pipe) {
2249 master_priv->sarea_priv->pipeA_x = x;
2250 master_priv->sarea_priv->pipeA_y = y;
2253 master_priv->sarea_priv->pipeB_x = x;
2254 master_priv->sarea_priv->pipeB_y = y;
2262 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2263 struct drm_framebuffer *fb)
2265 struct drm_device *dev = crtc->dev;
2266 struct drm_i915_private *dev_priv = dev->dev_private;
2267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2268 struct drm_framebuffer *old_fb;
2273 DRM_ERROR("No FB bound\n");
2277 if(intel_crtc->plane > dev_priv->num_pipe) {
2278 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2280 dev_priv->num_pipe);
2284 mutex_lock(&dev->struct_mutex);
2285 ret = intel_pin_and_fence_fb_obj(dev,
2286 to_intel_framebuffer(fb)->obj,
2289 mutex_unlock(&dev->struct_mutex);
2290 DRM_ERROR("pin & fence failed\n");
2295 intel_finish_fb(crtc->fb);
2297 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2299 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
2300 mutex_unlock(&dev->struct_mutex);
2301 DRM_ERROR("failed to update base address\n");
2311 intel_wait_for_vblank(dev, intel_crtc->pipe);
2312 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
2315 intel_update_fbc(dev);
2316 mutex_unlock(&dev->struct_mutex);
2318 intel_crtc_update_sarea_pos(crtc, x, y);
2323 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2325 struct drm_device *dev = crtc->dev;
2326 struct drm_i915_private *dev_priv = dev->dev_private;
2327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2328 int pipe = intel_crtc->pipe;
2331 /* enable normal train */
2332 reg = FDI_TX_CTL(pipe);
2333 temp = I915_READ(reg);
2334 if (IS_IVYBRIDGE(dev)) {
2335 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2336 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2338 temp &= ~FDI_LINK_TRAIN_NONE;
2339 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2341 I915_WRITE(reg, temp);
2343 reg = FDI_RX_CTL(pipe);
2344 temp = I915_READ(reg);
2345 if (HAS_PCH_CPT(dev)) {
2346 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2347 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2349 temp &= ~FDI_LINK_TRAIN_NONE;
2350 temp |= FDI_LINK_TRAIN_NONE;
2352 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2354 /* wait one idle pattern time */
2358 /* IVB wants error correction enabled */
2359 if (IS_IVYBRIDGE(dev))
2360 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2361 FDI_FE_ERRC_ENABLE);
2364 static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 u32 flags = I915_READ(SOUTH_CHICKEN1);
2369 flags |= FDI_PHASE_SYNC_OVR(pipe);
2370 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2371 flags |= FDI_PHASE_SYNC_EN(pipe);
2372 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2373 POSTING_READ(SOUTH_CHICKEN1);
2376 static void ivb_modeset_global_resources(struct drm_device *dev)
2378 struct drm_i915_private *dev_priv = dev->dev_private;
2379 struct intel_crtc *pipe_B_crtc =
2380 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2381 struct intel_crtc *pipe_C_crtc =
2382 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2385 /* When everything is off disable fdi C so that we could enable fdi B
2386 * with all lanes. XXX: This misses the case where a pipe is not using
2387 * any pch resources and so doesn't need any fdi lanes. */
2388 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2389 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2390 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2392 temp = I915_READ(SOUTH_CHICKEN1);
2393 temp &= ~FDI_BC_BIFURCATION_SELECT;
2394 DRM_DEBUG_KMS("disabling fdi C rx\n");
2395 I915_WRITE(SOUTH_CHICKEN1, temp);
2399 /* The FDI link training functions for ILK/Ibexpeak. */
2400 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2402 struct drm_device *dev = crtc->dev;
2403 struct drm_i915_private *dev_priv = dev->dev_private;
2404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2405 int pipe = intel_crtc->pipe;
2406 int plane = intel_crtc->plane;
2407 u32 reg, temp, tries;
2409 /* FDI needs bits from pipe & plane first */
2410 assert_pipe_enabled(dev_priv, pipe);
2411 assert_plane_enabled(dev_priv, plane);
2413 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2415 reg = FDI_RX_IMR(pipe);
2416 temp = I915_READ(reg);
2417 temp &= ~FDI_RX_SYMBOL_LOCK;
2418 temp &= ~FDI_RX_BIT_LOCK;
2419 I915_WRITE(reg, temp);
2423 /* enable CPU FDI TX and PCH FDI RX */
2424 reg = FDI_TX_CTL(pipe);
2425 temp = I915_READ(reg);
2427 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2428 temp &= ~FDI_LINK_TRAIN_NONE;
2429 temp |= FDI_LINK_TRAIN_PATTERN_1;
2430 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2432 reg = FDI_RX_CTL(pipe);
2433 temp = I915_READ(reg);
2434 temp &= ~FDI_LINK_TRAIN_NONE;
2435 temp |= FDI_LINK_TRAIN_PATTERN_1;
2436 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2441 /* Ironlake workaround, enable clock pointer after FDI enable*/
2442 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2443 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2444 FDI_RX_PHASE_SYNC_POINTER_EN);
2446 reg = FDI_RX_IIR(pipe);
2447 for (tries = 0; tries < 5; tries++) {
2448 temp = I915_READ(reg);
2449 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2451 if ((temp & FDI_RX_BIT_LOCK)) {
2452 DRM_DEBUG_KMS("FDI train 1 done.\n");
2453 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2458 DRM_ERROR("FDI train 1 fail!\n");
2461 reg = FDI_TX_CTL(pipe);
2462 temp = I915_READ(reg);
2463 temp &= ~FDI_LINK_TRAIN_NONE;
2464 temp |= FDI_LINK_TRAIN_PATTERN_2;
2465 I915_WRITE(reg, temp);
2467 reg = FDI_RX_CTL(pipe);
2468 temp = I915_READ(reg);
2469 temp &= ~FDI_LINK_TRAIN_NONE;
2470 temp |= FDI_LINK_TRAIN_PATTERN_2;
2471 I915_WRITE(reg, temp);
2476 reg = FDI_RX_IIR(pipe);
2477 for (tries = 0; tries < 5; tries++) {
2478 temp = I915_READ(reg);
2479 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2481 if (temp & FDI_RX_SYMBOL_LOCK) {
2482 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2483 DRM_DEBUG_KMS("FDI train 2 done.\n");
2488 DRM_ERROR("FDI train 2 fail!\n");
2490 DRM_DEBUG_KMS("FDI train done\n");
2494 static const int snb_b_fdi_train_param[] = {
2495 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2496 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2497 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2498 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2501 /* The FDI link training functions for SNB/Cougarpoint. */
2502 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2504 struct drm_device *dev = crtc->dev;
2505 struct drm_i915_private *dev_priv = dev->dev_private;
2506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2507 int pipe = intel_crtc->pipe;
2508 u32 reg, temp, i, retry;
2510 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2512 reg = FDI_RX_IMR(pipe);
2513 temp = I915_READ(reg);
2514 temp &= ~FDI_RX_SYMBOL_LOCK;
2515 temp &= ~FDI_RX_BIT_LOCK;
2516 I915_WRITE(reg, temp);
2521 /* enable CPU FDI TX and PCH FDI RX */
2522 reg = FDI_TX_CTL(pipe);
2523 temp = I915_READ(reg);
2525 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2526 temp &= ~FDI_LINK_TRAIN_NONE;
2527 temp |= FDI_LINK_TRAIN_PATTERN_1;
2528 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2530 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2531 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2533 I915_WRITE(FDI_RX_MISC(pipe),
2534 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2536 reg = FDI_RX_CTL(pipe);
2537 temp = I915_READ(reg);
2538 if (HAS_PCH_CPT(dev)) {
2539 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2540 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2542 temp &= ~FDI_LINK_TRAIN_NONE;
2543 temp |= FDI_LINK_TRAIN_PATTERN_1;
2545 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2550 cpt_phase_pointer_enable(dev, pipe);
2552 for (i = 0; i < 4; i++) {
2553 reg = FDI_TX_CTL(pipe);
2554 temp = I915_READ(reg);
2555 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2556 temp |= snb_b_fdi_train_param[i];
2557 I915_WRITE(reg, temp);
2562 for (retry = 0; retry < 5; retry++) {
2563 reg = FDI_RX_IIR(pipe);
2564 temp = I915_READ(reg);
2565 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2566 if (temp & FDI_RX_BIT_LOCK) {
2567 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2568 DRM_DEBUG_KMS("FDI train 1 done.\n");
2577 DRM_ERROR("FDI train 1 fail!\n");
2580 reg = FDI_TX_CTL(pipe);
2581 temp = I915_READ(reg);
2582 temp &= ~FDI_LINK_TRAIN_NONE;
2583 temp |= FDI_LINK_TRAIN_PATTERN_2;
2585 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2587 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2589 I915_WRITE(reg, temp);
2591 reg = FDI_RX_CTL(pipe);
2592 temp = I915_READ(reg);
2593 if (HAS_PCH_CPT(dev)) {
2594 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2595 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2597 temp &= ~FDI_LINK_TRAIN_NONE;
2598 temp |= FDI_LINK_TRAIN_PATTERN_2;
2600 I915_WRITE(reg, temp);
2605 for (i = 0; i < 4; i++) {
2606 reg = FDI_TX_CTL(pipe);
2607 temp = I915_READ(reg);
2608 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2609 temp |= snb_b_fdi_train_param[i];
2610 I915_WRITE(reg, temp);
2615 for (retry = 0; retry < 5; retry++) {
2616 reg = FDI_RX_IIR(pipe);
2617 temp = I915_READ(reg);
2618 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2619 if (temp & FDI_RX_SYMBOL_LOCK) {
2620 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2621 DRM_DEBUG_KMS("FDI train 2 done.\n");
2630 DRM_ERROR("FDI train 2 fail!\n");
2632 DRM_DEBUG_KMS("FDI train done.\n");
2635 /* Manual link training for Ivy Bridge A0 parts */
2636 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2638 struct drm_device *dev = crtc->dev;
2639 struct drm_i915_private *dev_priv = dev->dev_private;
2640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2641 int pipe = intel_crtc->pipe;
2644 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2646 reg = FDI_RX_IMR(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_RX_SYMBOL_LOCK;
2649 temp &= ~FDI_RX_BIT_LOCK;
2650 I915_WRITE(reg, temp);
2655 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2656 I915_READ(FDI_RX_IIR(pipe)));
2658 /* enable CPU FDI TX and PCH FDI RX */
2659 reg = FDI_TX_CTL(pipe);
2660 temp = I915_READ(reg);
2662 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2663 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2664 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2665 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2666 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2667 temp |= FDI_COMPOSITE_SYNC;
2668 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2670 I915_WRITE(FDI_RX_MISC(pipe),
2671 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2673 reg = FDI_RX_CTL(pipe);
2674 temp = I915_READ(reg);
2675 temp &= ~FDI_LINK_TRAIN_AUTO;
2676 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2677 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2678 temp |= FDI_COMPOSITE_SYNC;
2679 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2684 cpt_phase_pointer_enable(dev, pipe);
2686 for (i = 0; i < 4; i++) {
2687 reg = FDI_TX_CTL(pipe);
2688 temp = I915_READ(reg);
2689 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2690 temp |= snb_b_fdi_train_param[i];
2691 I915_WRITE(reg, temp);
2696 reg = FDI_RX_IIR(pipe);
2697 temp = I915_READ(reg);
2698 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2700 if (temp & FDI_RX_BIT_LOCK ||
2701 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2702 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2703 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
2708 DRM_ERROR("FDI train 1 fail!\n");
2711 reg = FDI_TX_CTL(pipe);
2712 temp = I915_READ(reg);
2713 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2714 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2715 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2716 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2717 I915_WRITE(reg, temp);
2719 reg = FDI_RX_CTL(pipe);
2720 temp = I915_READ(reg);
2721 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2722 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2723 I915_WRITE(reg, temp);
2728 for (i = 0; i < 4; i++) {
2729 reg = FDI_TX_CTL(pipe);
2730 temp = I915_READ(reg);
2731 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2732 temp |= snb_b_fdi_train_param[i];
2733 I915_WRITE(reg, temp);
2738 reg = FDI_RX_IIR(pipe);
2739 temp = I915_READ(reg);
2740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2742 if (temp & FDI_RX_SYMBOL_LOCK) {
2743 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2744 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
2749 DRM_ERROR("FDI train 2 fail!\n");
2751 DRM_DEBUG_KMS("FDI train done.\n");
2754 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2756 struct drm_device *dev = intel_crtc->base.dev;
2757 struct drm_i915_private *dev_priv = dev->dev_private;
2758 int pipe = intel_crtc->pipe;
2762 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2763 reg = FDI_RX_CTL(pipe);
2764 temp = I915_READ(reg);
2765 temp &= ~((0x7 << 19) | (0x7 << 16));
2766 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2768 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2773 /* Switch from Rawclk to PCDclk */
2774 temp = I915_READ(reg);
2775 I915_WRITE(reg, temp | FDI_PCDCLK);
2780 /* Enable CPU FDI TX PLL, always on for Ironlake */
2781 reg = FDI_TX_CTL(pipe);
2782 temp = I915_READ(reg);
2783 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2784 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2791 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2793 struct drm_device *dev = intel_crtc->base.dev;
2794 struct drm_i915_private *dev_priv = dev->dev_private;
2795 int pipe = intel_crtc->pipe;
2798 /* Switch from PCDclk to Rawclk */
2799 reg = FDI_RX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2803 /* Disable CPU FDI TX PLL */
2804 reg = FDI_TX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2811 reg = FDI_RX_CTL(pipe);
2812 temp = I915_READ(reg);
2813 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2815 /* Wait for the clocks to turn off. */
2820 static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2822 struct drm_i915_private *dev_priv = dev->dev_private;
2823 u32 flags = I915_READ(SOUTH_CHICKEN1);
2825 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2826 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2827 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2828 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2829 POSTING_READ(SOUTH_CHICKEN1);
2831 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2833 struct drm_device *dev = crtc->dev;
2834 struct drm_i915_private *dev_priv = dev->dev_private;
2835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2836 int pipe = intel_crtc->pipe;
2839 /* disable CPU FDI tx and PCH FDI rx */
2840 reg = FDI_TX_CTL(pipe);
2841 temp = I915_READ(reg);
2842 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2845 reg = FDI_RX_CTL(pipe);
2846 temp = I915_READ(reg);
2847 temp &= ~(0x7 << 16);
2848 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2849 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2854 /* Ironlake workaround, disable clock pointer after downing FDI */
2855 if (HAS_PCH_IBX(dev)) {
2856 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2857 } else if (HAS_PCH_CPT(dev)) {
2858 cpt_phase_pointer_disable(dev, pipe);
2861 /* still set train pattern 1 */
2862 reg = FDI_TX_CTL(pipe);
2863 temp = I915_READ(reg);
2864 temp &= ~FDI_LINK_TRAIN_NONE;
2865 temp |= FDI_LINK_TRAIN_PATTERN_1;
2866 I915_WRITE(reg, temp);
2868 reg = FDI_RX_CTL(pipe);
2869 temp = I915_READ(reg);
2870 if (HAS_PCH_CPT(dev)) {
2871 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2872 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2874 temp &= ~FDI_LINK_TRAIN_NONE;
2875 temp |= FDI_LINK_TRAIN_PATTERN_1;
2877 /* BPC in FDI rx is consistent with that in PIPECONF */
2878 temp &= ~(0x07 << 16);
2879 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
2880 I915_WRITE(reg, temp);
2886 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2888 struct drm_device *dev = crtc->dev;
2889 struct drm_i915_private *dev_priv = dev->dev_private;
2890 unsigned long flags;
2893 if (atomic_read(&dev_priv->mm.wedged))
2896 spin_lock_irqsave(&dev->event_lock, flags);
2897 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2898 spin_unlock_irqrestore(&dev->event_lock, flags);
2903 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2905 struct drm_device *dev = crtc->dev;
2906 struct drm_i915_private *dev_priv = dev->dev_private;
2908 if (crtc->fb == NULL)
2911 wait_event(dev_priv->pending_flip_queue,
2912 !intel_crtc_has_pending_flip(crtc));
2914 mutex_lock(&dev->struct_mutex);
2915 intel_finish_fb(crtc->fb);
2916 mutex_unlock(&dev->struct_mutex);
2919 static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
2921 struct drm_device *dev = crtc->dev;
2922 struct intel_encoder *intel_encoder;
2925 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2926 * must be driven by its own crtc; no sharing is possible.
2928 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
2929 switch (intel_encoder->type) {
2930 case INTEL_OUTPUT_EDP:
2931 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
2940 static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2942 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2945 /* Program iCLKIP clock to the desired frequency */
2946 static void lpt_program_iclkip(struct drm_crtc *crtc)
2948 struct drm_device *dev = crtc->dev;
2949 struct drm_i915_private *dev_priv = dev->dev_private;
2950 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2953 mutex_lock(&dev_priv->dpio_lock);
2955 /* It is necessary to ungate the pixclk gate prior to programming
2956 * the divisors, and gate it back when it is done.
2958 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2960 /* Disable SSCCTL */
2961 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2962 intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2963 SBI_SSCCTL_DISABLE);
2965 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2966 if (crtc->mode.clock == 20000) {
2971 /* The iCLK virtual clock root frequency is in MHz,
2972 * but the crtc->mode.clock in in KHz. To get the divisors,
2973 * it is necessary to divide one by another, so we
2974 * convert the virtual clock precision to KHz here for higher
2977 u32 iclk_virtual_root_freq = 172800 * 1000;
2978 u32 iclk_pi_range = 64;
2979 u32 desired_divisor, msb_divisor_value, pi_value;
2981 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2982 msb_divisor_value = desired_divisor / iclk_pi_range;
2983 pi_value = desired_divisor % iclk_pi_range;
2986 divsel = msb_divisor_value - 2;
2987 phaseinc = pi_value;
2990 /* This should not happen with any sane values */
2991 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2992 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2993 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2994 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2996 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3003 /* Program SSCDIVINTPHASE6 */
3004 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
3005 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3006 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3007 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3008 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3009 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3010 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3012 intel_sbi_write(dev_priv,
3013 SBI_SSCDIVINTPHASE6,
3016 /* Program SSCAUXDIV */
3017 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
3018 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3019 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3020 intel_sbi_write(dev_priv,
3025 /* Enable modulator and associated divider */
3026 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
3027 temp &= ~SBI_SSCCTL_DISABLE;
3028 intel_sbi_write(dev_priv,
3032 /* Wait for initialization time */
3035 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3037 mutex_unlock(&dev_priv->dpio_lock);
3041 * Enable PCH resources required for PCH ports:
3043 * - FDI training & RX/TX
3044 * - update transcoder timings
3045 * - DP transcoding bits
3048 static void ironlake_pch_enable(struct drm_crtc *crtc)
3050 struct drm_device *dev = crtc->dev;
3051 struct drm_i915_private *dev_priv = dev->dev_private;
3052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3053 int pipe = intel_crtc->pipe;
3056 assert_transcoder_disabled(dev_priv, pipe);
3058 /* Write the TU size bits before fdi link training, so that error
3059 * detection works. */
3060 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3061 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3063 /* For PCH output, training FDI link */
3064 dev_priv->display.fdi_link_train(crtc);
3066 /* XXX: pch pll's can be enabled any time before we enable the PCH
3067 * transcoder, and we actually should do this to not upset any PCH
3068 * transcoder that already use the clock when we share it.
3070 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3071 * unconditionally resets the pll - we need that to have the right LVDS
3072 * enable sequence. */
3073 ironlake_enable_pch_pll(intel_crtc);
3075 if (HAS_PCH_CPT(dev)) {
3078 temp = I915_READ(PCH_DPLL_SEL);
3082 temp |= TRANSA_DPLL_ENABLE;
3083 sel = TRANSA_DPLLB_SEL;
3086 temp |= TRANSB_DPLL_ENABLE;
3087 sel = TRANSB_DPLLB_SEL;
3090 temp |= TRANSC_DPLL_ENABLE;
3091 sel = TRANSC_DPLLB_SEL;
3094 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3098 I915_WRITE(PCH_DPLL_SEL, temp);
3101 /* set transcoder timing, panel must allow it */
3102 assert_panel_unlocked(dev_priv, pipe);
3103 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3104 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3105 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3107 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3108 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3109 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
3110 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
3112 intel_fdi_normal_train(crtc);
3114 /* For PCH DP, enable TRANS_DP_CTL */
3115 if (HAS_PCH_CPT(dev) &&
3116 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3117 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3118 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3119 reg = TRANS_DP_CTL(pipe);
3120 temp = I915_READ(reg);
3121 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3122 TRANS_DP_SYNC_MASK |
3124 temp |= (TRANS_DP_OUTPUT_ENABLE |
3125 TRANS_DP_ENH_FRAMING);
3126 temp |= bpc << 9; /* same format but at 11:9 */
3128 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3129 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3130 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3131 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3133 switch (intel_trans_dp_port_sel(crtc)) {
3135 temp |= TRANS_DP_PORT_SEL_B;
3138 temp |= TRANS_DP_PORT_SEL_C;
3141 temp |= TRANS_DP_PORT_SEL_D;
3147 I915_WRITE(reg, temp);
3150 ironlake_enable_pch_transcoder(dev_priv, pipe);
3153 static void lpt_pch_enable(struct drm_crtc *crtc)
3155 struct drm_device *dev = crtc->dev;
3156 struct drm_i915_private *dev_priv = dev->dev_private;
3157 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3158 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
3160 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
3162 lpt_program_iclkip(crtc);
3164 /* Set transcoder timing. */
3165 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3166 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3167 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
3169 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3170 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3171 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3172 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
3174 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3177 static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3179 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3184 if (pll->refcount == 0) {
3185 WARN(1, "bad PCH PLL refcount\n");
3190 intel_crtc->pch_pll = NULL;
3193 static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3195 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3196 struct intel_pch_pll *pll;
3199 pll = intel_crtc->pch_pll;
3201 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3202 intel_crtc->base.base.id, pll->pll_reg);
3206 if (HAS_PCH_IBX(dev_priv->dev)) {
3207 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3208 i = intel_crtc->pipe;
3209 pll = &dev_priv->pch_plls[i];
3211 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3212 intel_crtc->base.base.id, pll->pll_reg);
3217 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3218 pll = &dev_priv->pch_plls[i];
3220 /* Only want to check enabled timings first */
3221 if (pll->refcount == 0)
3224 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3225 fp == I915_READ(pll->fp0_reg)) {
3226 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3227 intel_crtc->base.base.id,
3228 pll->pll_reg, pll->refcount, pll->active);
3234 /* Ok no matching timings, maybe there's a free one? */
3235 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3236 pll = &dev_priv->pch_plls[i];
3237 if (pll->refcount == 0) {
3238 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3239 intel_crtc->base.base.id, pll->pll_reg);
3247 intel_crtc->pch_pll = pll;
3249 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3250 prepare: /* separate function? */
3251 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
3253 /* Wait for the clocks to stabilize before rewriting the regs */
3254 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3255 POSTING_READ(pll->pll_reg);
3258 I915_WRITE(pll->fp0_reg, fp);
3259 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3264 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3266 struct drm_i915_private *dev_priv = dev->dev_private;
3267 int dslreg = PIPEDSL(pipe);
3270 temp = I915_READ(dslreg);
3272 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3273 if (wait_for(I915_READ(dslreg) != temp, 5))
3274 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3278 static void ironlake_crtc_enable(struct drm_crtc *crtc)
3280 struct drm_device *dev = crtc->dev;
3281 struct drm_i915_private *dev_priv = dev->dev_private;
3282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3283 struct intel_encoder *encoder;
3284 int pipe = intel_crtc->pipe;
3285 int plane = intel_crtc->plane;
3289 WARN_ON(!crtc->enabled);
3291 if (intel_crtc->active)
3294 intel_crtc->active = true;
3295 intel_update_watermarks(dev);
3297 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3298 temp = I915_READ(PCH_LVDS);
3299 if ((temp & LVDS_PORT_EN) == 0)
3300 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3303 is_pch_port = ironlake_crtc_driving_pch(crtc);
3306 /* Note: FDI PLL enabling _must_ be done before we enable the
3307 * cpu pipes, hence this is separate from all the other fdi/pch
3309 ironlake_fdi_pll_enable(intel_crtc);
3311 assert_fdi_tx_disabled(dev_priv, pipe);
3312 assert_fdi_rx_disabled(dev_priv, pipe);
3315 for_each_encoder_on_crtc(dev, crtc, encoder)
3316 if (encoder->pre_enable)
3317 encoder->pre_enable(encoder);
3319 /* Enable panel fitting for LVDS */
3320 if (dev_priv->pch_pf_size &&
3321 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3322 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3323 /* Force use of hard-coded filter coefficients
3324 * as some pre-programmed values are broken,
3327 if (IS_IVYBRIDGE(dev))
3328 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3329 PF_PIPE_SEL_IVB(pipe));
3331 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3332 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3333 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3337 * On ILK+ LUT must be loaded before the pipe is running but with
3340 intel_crtc_load_lut(crtc);
3342 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3343 intel_enable_plane(dev_priv, plane, pipe);
3346 ironlake_pch_enable(crtc);
3348 mutex_lock(&dev->struct_mutex);
3349 intel_update_fbc(dev);
3350 mutex_unlock(&dev->struct_mutex);
3352 intel_crtc_update_cursor(crtc, true);
3354 for_each_encoder_on_crtc(dev, crtc, encoder)
3355 encoder->enable(encoder);
3357 if (HAS_PCH_CPT(dev))
3358 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3361 * There seems to be a race in PCH platform hw (at least on some
3362 * outputs) where an enabled pipe still completes any pageflip right
3363 * away (as if the pipe is off) instead of waiting for vblank. As soon
3364 * as the first vblank happend, everything works as expected. Hence just
3365 * wait for one vblank before returning to avoid strange things
3368 intel_wait_for_vblank(dev, intel_crtc->pipe);
3371 static void haswell_crtc_enable(struct drm_crtc *crtc)
3373 struct drm_device *dev = crtc->dev;
3374 struct drm_i915_private *dev_priv = dev->dev_private;
3375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3376 struct intel_encoder *encoder;
3377 int pipe = intel_crtc->pipe;
3378 int plane = intel_crtc->plane;
3381 WARN_ON(!crtc->enabled);
3383 if (intel_crtc->active)
3386 intel_crtc->active = true;
3387 intel_update_watermarks(dev);
3389 is_pch_port = haswell_crtc_driving_pch(crtc);
3392 dev_priv->display.fdi_link_train(crtc);
3394 for_each_encoder_on_crtc(dev, crtc, encoder)
3395 if (encoder->pre_enable)
3396 encoder->pre_enable(encoder);
3398 intel_ddi_enable_pipe_clock(intel_crtc);
3400 /* Enable panel fitting for eDP */
3401 if (dev_priv->pch_pf_size &&
3402 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
3403 /* Force use of hard-coded filter coefficients
3404 * as some pre-programmed values are broken,
3407 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3408 PF_PIPE_SEL_IVB(pipe));
3409 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3410 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3414 * On ILK+ LUT must be loaded before the pipe is running but with
3417 intel_crtc_load_lut(crtc);
3419 intel_ddi_set_pipe_settings(crtc);
3420 intel_ddi_enable_pipe_func(crtc);
3422 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3423 intel_enable_plane(dev_priv, plane, pipe);
3426 lpt_pch_enable(crtc);
3428 mutex_lock(&dev->struct_mutex);
3429 intel_update_fbc(dev);
3430 mutex_unlock(&dev->struct_mutex);
3432 intel_crtc_update_cursor(crtc, true);
3434 for_each_encoder_on_crtc(dev, crtc, encoder)
3435 encoder->enable(encoder);
3438 * There seems to be a race in PCH platform hw (at least on some
3439 * outputs) where an enabled pipe still completes any pageflip right
3440 * away (as if the pipe is off) instead of waiting for vblank. As soon
3441 * as the first vblank happend, everything works as expected. Hence just
3442 * wait for one vblank before returning to avoid strange things
3445 intel_wait_for_vblank(dev, intel_crtc->pipe);
3448 static void ironlake_crtc_disable(struct drm_crtc *crtc)
3450 struct drm_device *dev = crtc->dev;
3451 struct drm_i915_private *dev_priv = dev->dev_private;
3452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3453 struct intel_encoder *encoder;
3454 int pipe = intel_crtc->pipe;
3455 int plane = intel_crtc->plane;
3459 if (!intel_crtc->active)
3462 for_each_encoder_on_crtc(dev, crtc, encoder)
3463 encoder->disable(encoder);
3465 intel_crtc_wait_for_pending_flips(crtc);
3466 drm_vblank_off(dev, pipe);
3467 intel_crtc_update_cursor(crtc, false);
3469 intel_disable_plane(dev_priv, plane, pipe);
3471 if (dev_priv->cfb_plane == plane)
3472 intel_disable_fbc(dev);
3474 intel_disable_pipe(dev_priv, pipe);
3477 I915_WRITE(PF_CTL(pipe), 0);
3478 I915_WRITE(PF_WIN_SZ(pipe), 0);
3480 for_each_encoder_on_crtc(dev, crtc, encoder)
3481 if (encoder->post_disable)
3482 encoder->post_disable(encoder);
3484 ironlake_fdi_disable(crtc);
3486 ironlake_disable_pch_transcoder(dev_priv, pipe);
3488 if (HAS_PCH_CPT(dev)) {
3489 /* disable TRANS_DP_CTL */
3490 reg = TRANS_DP_CTL(pipe);
3491 temp = I915_READ(reg);
3492 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
3493 temp |= TRANS_DP_PORT_SEL_NONE;
3494 I915_WRITE(reg, temp);
3496 /* disable DPLL_SEL */
3497 temp = I915_READ(PCH_DPLL_SEL);
3500 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3503 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3506 /* C shares PLL A or B */
3507 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3512 I915_WRITE(PCH_DPLL_SEL, temp);
3515 /* disable PCH DPLL */
3516 intel_disable_pch_pll(intel_crtc);
3518 ironlake_fdi_pll_disable(intel_crtc);
3520 intel_crtc->active = false;
3521 intel_update_watermarks(dev);
3523 mutex_lock(&dev->struct_mutex);
3524 intel_update_fbc(dev);
3525 mutex_unlock(&dev->struct_mutex);
3528 static void haswell_crtc_disable(struct drm_crtc *crtc)
3530 struct drm_device *dev = crtc->dev;
3531 struct drm_i915_private *dev_priv = dev->dev_private;
3532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3533 struct intel_encoder *encoder;
3534 int pipe = intel_crtc->pipe;
3535 int plane = intel_crtc->plane;
3536 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
3539 if (!intel_crtc->active)
3542 is_pch_port = haswell_crtc_driving_pch(crtc);
3544 for_each_encoder_on_crtc(dev, crtc, encoder)
3545 encoder->disable(encoder);
3547 intel_crtc_wait_for_pending_flips(crtc);
3548 drm_vblank_off(dev, pipe);
3549 intel_crtc_update_cursor(crtc, false);
3551 intel_disable_plane(dev_priv, plane, pipe);
3553 if (dev_priv->cfb_plane == plane)
3554 intel_disable_fbc(dev);
3556 intel_disable_pipe(dev_priv, pipe);
3558 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
3561 I915_WRITE(PF_CTL(pipe), 0);
3562 I915_WRITE(PF_WIN_SZ(pipe), 0);
3564 intel_ddi_disable_pipe_clock(intel_crtc);
3566 for_each_encoder_on_crtc(dev, crtc, encoder)
3567 if (encoder->post_disable)
3568 encoder->post_disable(encoder);
3571 lpt_disable_pch_transcoder(dev_priv);
3572 intel_ddi_fdi_disable(crtc);
3575 intel_crtc->active = false;
3576 intel_update_watermarks(dev);
3578 mutex_lock(&dev->struct_mutex);
3579 intel_update_fbc(dev);
3580 mutex_unlock(&dev->struct_mutex);
3583 static void ironlake_crtc_off(struct drm_crtc *crtc)
3585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3586 intel_put_pch_pll(intel_crtc);
3589 static void haswell_crtc_off(struct drm_crtc *crtc)
3591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3593 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3594 * start using it. */
3595 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
3597 intel_ddi_put_crtc_pll(crtc);
3600 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3602 if (!enable && intel_crtc->overlay) {
3603 struct drm_device *dev = intel_crtc->base.dev;
3604 struct drm_i915_private *dev_priv = dev->dev_private;
3606 mutex_lock(&dev->struct_mutex);
3607 dev_priv->mm.interruptible = false;
3608 (void) intel_overlay_switch_off(intel_crtc->overlay);
3609 dev_priv->mm.interruptible = true;
3610 mutex_unlock(&dev->struct_mutex);
3613 /* Let userspace switch the overlay on again. In most cases userspace
3614 * has to recompute where to put it anyway.
3618 static void i9xx_crtc_enable(struct drm_crtc *crtc)
3620 struct drm_device *dev = crtc->dev;
3621 struct drm_i915_private *dev_priv = dev->dev_private;
3622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3623 struct intel_encoder *encoder;
3624 int pipe = intel_crtc->pipe;
3625 int plane = intel_crtc->plane;
3627 WARN_ON(!crtc->enabled);
3629 if (intel_crtc->active)
3632 intel_crtc->active = true;
3633 intel_update_watermarks(dev);
3635 intel_enable_pll(dev_priv, pipe);
3636 intel_enable_pipe(dev_priv, pipe, false);
3637 intel_enable_plane(dev_priv, plane, pipe);
3639 intel_crtc_load_lut(crtc);
3640 intel_update_fbc(dev);
3642 /* Give the overlay scaler a chance to enable if it's on this pipe */
3643 intel_crtc_dpms_overlay(intel_crtc, true);
3644 intel_crtc_update_cursor(crtc, true);
3646 for_each_encoder_on_crtc(dev, crtc, encoder)
3647 encoder->enable(encoder);
3650 static void i9xx_crtc_disable(struct drm_crtc *crtc)
3652 struct drm_device *dev = crtc->dev;
3653 struct drm_i915_private *dev_priv = dev->dev_private;
3654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3655 struct intel_encoder *encoder;
3656 int pipe = intel_crtc->pipe;
3657 int plane = intel_crtc->plane;
3660 if (!intel_crtc->active)
3663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 encoder->disable(encoder);
3666 /* Give the overlay scaler a chance to disable if it's on this pipe */
3667 intel_crtc_wait_for_pending_flips(crtc);
3668 drm_vblank_off(dev, pipe);
3669 intel_crtc_dpms_overlay(intel_crtc, false);
3670 intel_crtc_update_cursor(crtc, false);
3672 if (dev_priv->cfb_plane == plane)
3673 intel_disable_fbc(dev);
3675 intel_disable_plane(dev_priv, plane, pipe);
3676 intel_disable_pipe(dev_priv, pipe);
3677 intel_disable_pll(dev_priv, pipe);
3679 intel_crtc->active = false;
3680 intel_update_fbc(dev);
3681 intel_update_watermarks(dev);
3684 static void i9xx_crtc_off(struct drm_crtc *crtc)
3688 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3691 struct drm_device *dev = crtc->dev;
3692 struct drm_i915_master_private *master_priv;
3693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3694 int pipe = intel_crtc->pipe;
3696 if (!dev->primary->master)
3699 master_priv = dev->primary->master->driver_priv;
3700 if (!master_priv->sarea_priv)
3705 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3706 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3709 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3710 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3713 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
3719 * Sets the power management mode of the pipe and plane.
3721 void intel_crtc_update_dpms(struct drm_crtc *crtc)
3723 struct drm_device *dev = crtc->dev;
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725 struct intel_encoder *intel_encoder;
3726 bool enable = false;
3728 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3729 enable |= intel_encoder->connectors_active;
3732 dev_priv->display.crtc_enable(crtc);
3734 dev_priv->display.crtc_disable(crtc);
3736 intel_crtc_update_sarea(crtc, enable);
3739 static void intel_crtc_noop(struct drm_crtc *crtc)
3743 static void intel_crtc_disable(struct drm_crtc *crtc)
3745 struct drm_device *dev = crtc->dev;
3746 struct drm_connector *connector;
3747 struct drm_i915_private *dev_priv = dev->dev_private;
3749 /* crtc should still be enabled when we disable it. */
3750 WARN_ON(!crtc->enabled);
3752 dev_priv->display.crtc_disable(crtc);
3753 intel_crtc_update_sarea(crtc, false);
3754 dev_priv->display.off(crtc);
3756 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3757 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
3760 mutex_lock(&dev->struct_mutex);
3761 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
3762 mutex_unlock(&dev->struct_mutex);
3766 /* Update computed state. */
3767 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3768 if (!connector->encoder || !connector->encoder->crtc)
3771 if (connector->encoder->crtc != crtc)
3774 connector->dpms = DRM_MODE_DPMS_OFF;
3775 to_intel_encoder(connector->encoder)->connectors_active = false;
3779 void intel_modeset_disable(struct drm_device *dev)
3781 struct drm_crtc *crtc;
3783 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3785 intel_crtc_disable(crtc);
3789 void intel_encoder_noop(struct drm_encoder *encoder)
3793 void intel_encoder_destroy(struct drm_encoder *encoder)
3795 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3797 drm_encoder_cleanup(encoder);
3798 kfree(intel_encoder);
3801 /* Simple dpms helper for encodres with just one connector, no cloning and only
3802 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3803 * state of the entire output pipe. */
3804 void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3806 if (mode == DRM_MODE_DPMS_ON) {
3807 encoder->connectors_active = true;
3809 intel_crtc_update_dpms(encoder->base.crtc);
3811 encoder->connectors_active = false;
3813 intel_crtc_update_dpms(encoder->base.crtc);
3817 /* Cross check the actual hw state with our own modeset state tracking (and it's
3818 * internal consistency). */
3819 static void intel_connector_check_state(struct intel_connector *connector)
3821 if (connector->get_hw_state(connector)) {
3822 struct intel_encoder *encoder = connector->encoder;
3823 struct drm_crtc *crtc;
3824 bool encoder_enabled;
3827 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3828 connector->base.base.id,
3829 drm_get_connector_name(&connector->base));
3831 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3832 "wrong connector dpms state\n");
3833 WARN(connector->base.encoder != &encoder->base,
3834 "active connector not linked to encoder\n");
3835 WARN(!encoder->connectors_active,
3836 "encoder->connectors_active not set\n");
3838 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3839 WARN(!encoder_enabled, "encoder not enabled\n");
3840 if (WARN_ON(!encoder->base.crtc))
3843 crtc = encoder->base.crtc;
3845 WARN(!crtc->enabled, "crtc not enabled\n");
3846 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3847 WARN(pipe != to_intel_crtc(crtc)->pipe,
3848 "encoder active on the wrong pipe\n");
3852 /* Even simpler default implementation, if there's really no special case to
3854 void intel_connector_dpms(struct drm_connector *connector, int mode)
3856 struct intel_encoder *encoder = intel_attached_encoder(connector);
3858 /* All the simple cases only support two dpms states. */
3859 if (mode != DRM_MODE_DPMS_ON)
3860 mode = DRM_MODE_DPMS_OFF;
3862 if (mode == connector->dpms)
3865 connector->dpms = mode;
3867 /* Only need to change hw state when actually enabled */
3868 if (encoder->base.crtc)
3869 intel_encoder_dpms(encoder, mode);
3871 WARN_ON(encoder->connectors_active != false);
3873 intel_modeset_check_state(connector->dev);
3876 /* Simple connector->get_hw_state implementation for encoders that support only
3877 * one connector and no cloning and hence the encoder state determines the state
3878 * of the connector. */
3879 bool intel_connector_get_hw_state(struct intel_connector *connector)
3882 struct intel_encoder *encoder = connector->encoder;
3884 return encoder->get_hw_state(encoder, &pipe);
3887 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3888 const struct drm_display_mode *mode,
3889 struct drm_display_mode *adjusted_mode)
3891 struct drm_device *dev = crtc->dev;
3893 if (HAS_PCH_SPLIT(dev)) {
3894 /* FDI link clock is fixed at 2.7G */
3895 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3899 /* All interlaced capable intel hw wants timings in frames. Note though
3900 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3901 * timings, so we need to be careful not to clobber these.*/
3902 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3903 drm_mode_set_crtcinfo(adjusted_mode, 0);
3905 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3906 * with a hsync front porch of 0.
3908 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3909 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3915 static int valleyview_get_display_clock_speed(struct drm_device *dev)
3917 return 400000; /* FIXME */
3920 static int i945_get_display_clock_speed(struct drm_device *dev)
3925 static int i915_get_display_clock_speed(struct drm_device *dev)
3930 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3935 static int i915gm_get_display_clock_speed(struct drm_device *dev)
3939 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3941 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3944 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3945 case GC_DISPLAY_CLOCK_333_MHZ:
3948 case GC_DISPLAY_CLOCK_190_200_MHZ:
3954 static int i865_get_display_clock_speed(struct drm_device *dev)
3959 static int i855_get_display_clock_speed(struct drm_device *dev)
3962 /* Assume that the hardware is in the high speed state. This
3963 * should be the default.
3965 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3966 case GC_CLOCK_133_200:
3967 case GC_CLOCK_100_200:
3969 case GC_CLOCK_166_250:
3971 case GC_CLOCK_100_133:
3975 /* Shouldn't happen */
3979 static int i830_get_display_clock_speed(struct drm_device *dev)
3985 intel_reduce_ratio(uint32_t *num, uint32_t *den)
3987 while (*num > 0xffffff || *den > 0xffffff) {
3994 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3995 int pixel_clock, int link_clock,
3996 struct intel_link_m_n *m_n)
3999 m_n->gmch_m = bits_per_pixel * pixel_clock;
4000 m_n->gmch_n = link_clock * nlanes * 8;
4001 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
4002 m_n->link_m = pixel_clock;
4003 m_n->link_n = link_clock;
4004 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
4007 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4009 if (i915_panel_use_ssc >= 0)
4010 return i915_panel_use_ssc != 0;
4011 return dev_priv->lvds_use_ssc
4012 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
4016 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4017 * @crtc: CRTC structure
4018 * @mode: requested mode
4020 * A pipe may be connected to one or more outputs. Based on the depth of the
4021 * attached framebuffer, choose a good color depth to use on the pipe.
4023 * If possible, match the pipe depth to the fb depth. In some cases, this
4024 * isn't ideal, because the connected output supports a lesser or restricted
4025 * set of depths. Resolve that here:
4026 * LVDS typically supports only 6bpc, so clamp down in that case
4027 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4028 * Displays may support a restricted set as well, check EDID and clamp as
4030 * DP may want to dither down to 6bpc to fit larger modes
4033 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4034 * true if they don't match).
4036 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
4037 struct drm_framebuffer *fb,
4038 unsigned int *pipe_bpp,
4039 struct drm_display_mode *mode)
4041 struct drm_device *dev = crtc->dev;
4042 struct drm_i915_private *dev_priv = dev->dev_private;
4043 struct drm_connector *connector;
4044 struct intel_encoder *intel_encoder;
4045 unsigned int display_bpc = UINT_MAX, bpc;
4047 /* Walk the encoders & connectors on this crtc, get min bpc */
4048 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4050 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4051 unsigned int lvds_bpc;
4053 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4059 if (lvds_bpc < display_bpc) {
4060 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
4061 display_bpc = lvds_bpc;
4066 /* Not one of the known troublemakers, check the EDID */
4067 list_for_each_entry(connector, &dev->mode_config.connector_list,
4069 if (connector->encoder != &intel_encoder->base)
4072 /* Don't use an invalid EDID bpc value */
4073 if (connector->display_info.bpc &&
4074 connector->display_info.bpc < display_bpc) {
4075 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
4076 display_bpc = connector->display_info.bpc;
4081 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4082 * through, clamp it down. (Note: >12bpc will be caught below.)
4084 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4085 if (display_bpc > 8 && display_bpc < 12) {
4086 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
4089 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
4095 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4096 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4101 * We could just drive the pipe at the highest bpc all the time and
4102 * enable dithering as needed, but that costs bandwidth. So choose
4103 * the minimum value that expresses the full color range of the fb but
4104 * also stays within the max display bpc discovered above.
4107 switch (fb->depth) {
4109 bpc = 8; /* since we go through a colormap */
4113 bpc = 6; /* min is 18bpp */
4125 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4126 bpc = min((unsigned int)8, display_bpc);
4130 display_bpc = min(display_bpc, bpc);
4132 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4135 *pipe_bpp = display_bpc * 3;
4137 return display_bpc != bpc;
4140 static int vlv_get_refclk(struct drm_crtc *crtc)
4142 struct drm_device *dev = crtc->dev;
4143 struct drm_i915_private *dev_priv = dev->dev_private;
4144 int refclk = 27000; /* for DP & HDMI */
4146 return 100000; /* only one validated so far */
4148 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4150 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4151 if (intel_panel_use_ssc(dev_priv))
4155 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4162 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4164 struct drm_device *dev = crtc->dev;
4165 struct drm_i915_private *dev_priv = dev->dev_private;
4168 if (IS_VALLEYVIEW(dev)) {
4169 refclk = vlv_get_refclk(crtc);
4170 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4171 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4172 refclk = dev_priv->lvds_ssc_freq * 1000;
4173 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4175 } else if (!IS_GEN2(dev)) {
4184 static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4185 intel_clock_t *clock)
4187 /* SDVO TV has fixed PLL values depend on its clock range,
4188 this mirrors vbios setting. */
4189 if (adjusted_mode->clock >= 100000
4190 && adjusted_mode->clock < 140500) {
4196 } else if (adjusted_mode->clock >= 140500
4197 && adjusted_mode->clock <= 200000) {
4206 static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4207 intel_clock_t *clock,
4208 intel_clock_t *reduced_clock)
4210 struct drm_device *dev = crtc->dev;
4211 struct drm_i915_private *dev_priv = dev->dev_private;
4212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4213 int pipe = intel_crtc->pipe;
4216 if (IS_PINEVIEW(dev)) {
4217 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4219 fp2 = (1 << reduced_clock->n) << 16 |
4220 reduced_clock->m1 << 8 | reduced_clock->m2;
4222 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4224 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4228 I915_WRITE(FP0(pipe), fp);
4230 intel_crtc->lowfreq_avail = false;
4231 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4232 reduced_clock && i915_powersave) {
4233 I915_WRITE(FP1(pipe), fp2);
4234 intel_crtc->lowfreq_avail = true;
4236 I915_WRITE(FP1(pipe), fp);
4240 static void vlv_update_pll(struct drm_crtc *crtc,
4241 struct drm_display_mode *mode,
4242 struct drm_display_mode *adjusted_mode,
4243 intel_clock_t *clock, intel_clock_t *reduced_clock,
4246 struct drm_device *dev = crtc->dev;
4247 struct drm_i915_private *dev_priv = dev->dev_private;
4248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4249 int pipe = intel_crtc->pipe;
4250 u32 dpll, mdiv, pdiv;
4251 u32 bestn, bestm1, bestm2, bestp1, bestp2;
4255 mutex_lock(&dev_priv->dpio_lock);
4257 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4258 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4260 dpll = DPLL_VGA_MODE_DIS;
4261 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4262 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4263 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4265 I915_WRITE(DPLL(pipe), dpll);
4266 POSTING_READ(DPLL(pipe));
4275 * In Valleyview PLL and program lane counter registers are exposed
4276 * through DPIO interface
4278 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4279 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4280 mdiv |= ((bestn << DPIO_N_SHIFT));
4281 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4282 mdiv |= (1 << DPIO_K_SHIFT);
4283 mdiv |= DPIO_ENABLE_CALIBRATION;
4284 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4286 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4288 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
4289 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
4290 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4291 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
4292 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4294 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
4296 dpll |= DPLL_VCO_ENABLE;
4297 I915_WRITE(DPLL(pipe), dpll);
4298 POSTING_READ(DPLL(pipe));
4299 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4300 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4302 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
4304 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4305 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4307 I915_WRITE(DPLL(pipe), dpll);
4309 /* Wait for the clocks to stabilize. */
4310 POSTING_READ(DPLL(pipe));
4315 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4317 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4321 I915_WRITE(DPLL_MD(pipe), temp);
4322 POSTING_READ(DPLL_MD(pipe));
4324 /* Now program lane control registers */
4325 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4326 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4331 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4333 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4338 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4341 mutex_unlock(&dev_priv->dpio_lock);
4344 static void i9xx_update_pll(struct drm_crtc *crtc,
4345 struct drm_display_mode *mode,
4346 struct drm_display_mode *adjusted_mode,
4347 intel_clock_t *clock, intel_clock_t *reduced_clock,
4350 struct drm_device *dev = crtc->dev;
4351 struct drm_i915_private *dev_priv = dev->dev_private;
4352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4353 struct intel_encoder *encoder;
4354 int pipe = intel_crtc->pipe;
4358 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4360 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4361 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4363 dpll = DPLL_VGA_MODE_DIS;
4365 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4366 dpll |= DPLLB_MODE_LVDS;
4368 dpll |= DPLLB_MODE_DAC_SERIAL;
4370 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4371 if (pixel_multiplier > 1) {
4372 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4373 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4375 dpll |= DPLL_DVO_HIGH_SPEED;
4377 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4378 dpll |= DPLL_DVO_HIGH_SPEED;
4380 /* compute bitmask from p1 value */
4381 if (IS_PINEVIEW(dev))
4382 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4384 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4385 if (IS_G4X(dev) && reduced_clock)
4386 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4388 switch (clock->p2) {
4390 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4393 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4396 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4399 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4402 if (INTEL_INFO(dev)->gen >= 4)
4403 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4405 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4406 dpll |= PLL_REF_INPUT_TVCLKINBC;
4407 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4408 /* XXX: just matching BIOS for now */
4409 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4411 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4412 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4413 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4415 dpll |= PLL_REF_INPUT_DREFCLK;
4417 dpll |= DPLL_VCO_ENABLE;
4418 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4419 POSTING_READ(DPLL(pipe));
4422 for_each_encoder_on_crtc(dev, crtc, encoder)
4423 if (encoder->pre_pll_enable)
4424 encoder->pre_pll_enable(encoder);
4426 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4427 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4429 I915_WRITE(DPLL(pipe), dpll);
4431 /* Wait for the clocks to stabilize. */
4432 POSTING_READ(DPLL(pipe));
4435 if (INTEL_INFO(dev)->gen >= 4) {
4438 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4440 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4444 I915_WRITE(DPLL_MD(pipe), temp);
4446 /* The pixel multiplier can only be updated once the
4447 * DPLL is enabled and the clocks are stable.
4449 * So write it again.
4451 I915_WRITE(DPLL(pipe), dpll);
4455 static void i8xx_update_pll(struct drm_crtc *crtc,
4456 struct drm_display_mode *adjusted_mode,
4457 intel_clock_t *clock, intel_clock_t *reduced_clock,
4460 struct drm_device *dev = crtc->dev;
4461 struct drm_i915_private *dev_priv = dev->dev_private;
4462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4463 struct intel_encoder *encoder;
4464 int pipe = intel_crtc->pipe;
4467 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4469 dpll = DPLL_VGA_MODE_DIS;
4471 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4472 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4475 dpll |= PLL_P1_DIVIDE_BY_TWO;
4477 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4479 dpll |= PLL_P2_DIVIDE_BY_4;
4482 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4483 /* XXX: just matching BIOS for now */
4484 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4486 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4487 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4488 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4490 dpll |= PLL_REF_INPUT_DREFCLK;
4492 dpll |= DPLL_VCO_ENABLE;
4493 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4494 POSTING_READ(DPLL(pipe));
4497 for_each_encoder_on_crtc(dev, crtc, encoder)
4498 if (encoder->pre_pll_enable)
4499 encoder->pre_pll_enable(encoder);
4501 I915_WRITE(DPLL(pipe), dpll);
4503 /* Wait for the clocks to stabilize. */
4504 POSTING_READ(DPLL(pipe));
4507 /* The pixel multiplier can only be updated once the
4508 * DPLL is enabled and the clocks are stable.
4510 * So write it again.
4512 I915_WRITE(DPLL(pipe), dpll);
4515 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4516 struct drm_display_mode *mode,
4517 struct drm_display_mode *adjusted_mode)
4519 struct drm_device *dev = intel_crtc->base.dev;
4520 struct drm_i915_private *dev_priv = dev->dev_private;
4521 enum pipe pipe = intel_crtc->pipe;
4522 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
4523 uint32_t vsyncshift;
4525 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4526 /* the chip adds 2 halflines automatically */
4527 adjusted_mode->crtc_vtotal -= 1;
4528 adjusted_mode->crtc_vblank_end -= 1;
4529 vsyncshift = adjusted_mode->crtc_hsync_start
4530 - adjusted_mode->crtc_htotal / 2;
4535 if (INTEL_INFO(dev)->gen > 3)
4536 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
4538 I915_WRITE(HTOTAL(cpu_transcoder),
4539 (adjusted_mode->crtc_hdisplay - 1) |
4540 ((adjusted_mode->crtc_htotal - 1) << 16));
4541 I915_WRITE(HBLANK(cpu_transcoder),
4542 (adjusted_mode->crtc_hblank_start - 1) |
4543 ((adjusted_mode->crtc_hblank_end - 1) << 16));
4544 I915_WRITE(HSYNC(cpu_transcoder),
4545 (adjusted_mode->crtc_hsync_start - 1) |
4546 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4548 I915_WRITE(VTOTAL(cpu_transcoder),
4549 (adjusted_mode->crtc_vdisplay - 1) |
4550 ((adjusted_mode->crtc_vtotal - 1) << 16));
4551 I915_WRITE(VBLANK(cpu_transcoder),
4552 (adjusted_mode->crtc_vblank_start - 1) |
4553 ((adjusted_mode->crtc_vblank_end - 1) << 16));
4554 I915_WRITE(VSYNC(cpu_transcoder),
4555 (adjusted_mode->crtc_vsync_start - 1) |
4556 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4558 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4559 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4560 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4562 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4563 (pipe == PIPE_B || pipe == PIPE_C))
4564 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4566 /* pipesrc controls the size that is scaled from, which should
4567 * always be the user's requested size.
4569 I915_WRITE(PIPESRC(pipe),
4570 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4573 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4574 struct drm_display_mode *mode,
4575 struct drm_display_mode *adjusted_mode,
4577 struct drm_framebuffer *fb)
4579 struct drm_device *dev = crtc->dev;
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4582 int pipe = intel_crtc->pipe;
4583 int plane = intel_crtc->plane;
4584 int refclk, num_connectors = 0;
4585 intel_clock_t clock, reduced_clock;
4586 u32 dspcntr, pipeconf;
4587 bool ok, has_reduced_clock = false, is_sdvo = false;
4588 bool is_lvds = false, is_tv = false, is_dp = false;
4589 struct intel_encoder *encoder;
4590 const intel_limit_t *limit;
4593 for_each_encoder_on_crtc(dev, crtc, encoder) {
4594 switch (encoder->type) {
4595 case INTEL_OUTPUT_LVDS:
4598 case INTEL_OUTPUT_SDVO:
4599 case INTEL_OUTPUT_HDMI:
4601 if (encoder->needs_tv_clock)
4604 case INTEL_OUTPUT_TVOUT:
4607 case INTEL_OUTPUT_DISPLAYPORT:
4615 refclk = i9xx_get_refclk(crtc, num_connectors);
4618 * Returns a set of divisors for the desired target clock with the given
4619 * refclk, or FALSE. The returned values represent the clock equation:
4620 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4622 limit = intel_limit(crtc, refclk);
4623 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4626 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4630 /* Ensure that the cursor is valid for the new mode before changing... */
4631 intel_crtc_update_cursor(crtc, true);
4633 if (is_lvds && dev_priv->lvds_downclock_avail) {
4635 * Ensure we match the reduced clock's P to the target clock.
4636 * If the clocks don't match, we can't switch the display clock
4637 * by using the FP0/FP1. In such case we will disable the LVDS
4638 * downclock feature.
4640 has_reduced_clock = limit->find_pll(limit, crtc,
4641 dev_priv->lvds_downclock,
4647 if (is_sdvo && is_tv)
4648 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
4651 i8xx_update_pll(crtc, adjusted_mode, &clock,
4652 has_reduced_clock ? &reduced_clock : NULL,
4654 else if (IS_VALLEYVIEW(dev))
4655 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4656 has_reduced_clock ? &reduced_clock : NULL,
4659 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4660 has_reduced_clock ? &reduced_clock : NULL,
4663 /* setup pipeconf */
4664 pipeconf = I915_READ(PIPECONF(pipe));
4666 /* Set up the display plane register */
4667 dspcntr = DISPPLANE_GAMMA_ENABLE;
4670 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4672 dspcntr |= DISPPLANE_SEL_PIPE_B;
4674 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4675 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4678 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4682 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4683 pipeconf |= PIPECONF_DOUBLE_WIDE;
4685 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4688 /* default to 8bpc */
4689 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
4691 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4692 pipeconf |= PIPECONF_6BPC |
4693 PIPECONF_DITHER_EN |
4694 PIPECONF_DITHER_TYPE_SP;
4698 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4699 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4700 pipeconf |= PIPECONF_6BPC |
4702 I965_PIPECONF_ACTIVE;
4706 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4707 drm_mode_debug_printmodeline(mode);
4709 if (HAS_PIPE_CXSR(dev)) {
4710 if (intel_crtc->lowfreq_avail) {
4711 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4712 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4714 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4715 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4719 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4720 if (!IS_GEN2(dev) &&
4721 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4722 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4724 pipeconf |= PIPECONF_PROGRESSIVE;
4726 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
4728 /* pipesrc and dspsize control the size that is scaled from,
4729 * which should always be the user's requested size.
4731 I915_WRITE(DSPSIZE(plane),
4732 ((mode->vdisplay - 1) << 16) |
4733 (mode->hdisplay - 1));
4734 I915_WRITE(DSPPOS(plane), 0);
4736 I915_WRITE(PIPECONF(pipe), pipeconf);
4737 POSTING_READ(PIPECONF(pipe));
4738 intel_enable_pipe(dev_priv, pipe, false);
4740 intel_wait_for_vblank(dev, pipe);
4742 I915_WRITE(DSPCNTR(plane), dspcntr);
4743 POSTING_READ(DSPCNTR(plane));
4745 ret = intel_pipe_set_base(crtc, x, y, fb);
4747 intel_update_watermarks(dev);
4753 * Initialize reference clocks when the driver loads
4755 void ironlake_init_pch_refclk(struct drm_device *dev)
4757 struct drm_i915_private *dev_priv = dev->dev_private;
4758 struct drm_mode_config *mode_config = &dev->mode_config;
4759 struct intel_encoder *encoder;
4761 bool has_lvds = false;
4762 bool has_cpu_edp = false;
4763 bool has_pch_edp = false;
4764 bool has_panel = false;
4765 bool has_ck505 = false;
4766 bool can_ssc = false;
4768 /* We need to take the global config into account */
4769 list_for_each_entry(encoder, &mode_config->encoder_list,
4771 switch (encoder->type) {
4772 case INTEL_OUTPUT_LVDS:
4776 case INTEL_OUTPUT_EDP:
4778 if (intel_encoder_is_pch_edp(&encoder->base))
4786 if (HAS_PCH_IBX(dev)) {
4787 has_ck505 = dev_priv->display_clock_mode;
4788 can_ssc = has_ck505;
4794 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4795 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4798 /* Ironlake: try to setup display ref clock before DPLL
4799 * enabling. This is only under driver's control after
4800 * PCH B stepping, previous chipset stepping should be
4801 * ignoring this setting.
4803 temp = I915_READ(PCH_DREF_CONTROL);
4804 /* Always enable nonspread source */
4805 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
4808 temp |= DREF_NONSPREAD_CK505_ENABLE;
4810 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
4813 temp &= ~DREF_SSC_SOURCE_MASK;
4814 temp |= DREF_SSC_SOURCE_ENABLE;
4816 /* SSC must be turned on before enabling the CPU output */
4817 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4818 DRM_DEBUG_KMS("Using SSC on panel\n");
4819 temp |= DREF_SSC1_ENABLE;
4821 temp &= ~DREF_SSC1_ENABLE;
4823 /* Get SSC going before enabling the outputs */
4824 I915_WRITE(PCH_DREF_CONTROL, temp);
4825 POSTING_READ(PCH_DREF_CONTROL);
4828 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4830 /* Enable CPU source on CPU attached eDP */
4832 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4833 DRM_DEBUG_KMS("Using SSC on eDP\n");
4834 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4837 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4839 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4841 I915_WRITE(PCH_DREF_CONTROL, temp);
4842 POSTING_READ(PCH_DREF_CONTROL);
4845 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4847 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4849 /* Turn off CPU output */
4850 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4852 I915_WRITE(PCH_DREF_CONTROL, temp);
4853 POSTING_READ(PCH_DREF_CONTROL);
4856 /* Turn off the SSC source */
4857 temp &= ~DREF_SSC_SOURCE_MASK;
4858 temp |= DREF_SSC_SOURCE_DISABLE;
4861 temp &= ~ DREF_SSC1_ENABLE;
4863 I915_WRITE(PCH_DREF_CONTROL, temp);
4864 POSTING_READ(PCH_DREF_CONTROL);
4869 static int ironlake_get_refclk(struct drm_crtc *crtc)
4871 struct drm_device *dev = crtc->dev;
4872 struct drm_i915_private *dev_priv = dev->dev_private;
4873 struct intel_encoder *encoder;
4874 struct intel_encoder *edp_encoder = NULL;
4875 int num_connectors = 0;
4876 bool is_lvds = false;
4878 for_each_encoder_on_crtc(dev, crtc, encoder) {
4879 switch (encoder->type) {
4880 case INTEL_OUTPUT_LVDS:
4883 case INTEL_OUTPUT_EDP:
4884 edp_encoder = encoder;
4890 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4891 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4892 dev_priv->lvds_ssc_freq);
4893 return dev_priv->lvds_ssc_freq * 1000;
4899 static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4900 struct drm_display_mode *adjusted_mode,
4903 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4905 int pipe = intel_crtc->pipe;
4908 val = I915_READ(PIPECONF(pipe));
4910 val &= ~PIPECONF_BPC_MASK;
4911 switch (intel_crtc->bpp) {
4913 val |= PIPECONF_6BPC;
4916 val |= PIPECONF_8BPC;
4919 val |= PIPECONF_10BPC;
4922 val |= PIPECONF_12BPC;
4925 /* Case prevented by intel_choose_pipe_bpp_dither. */
4929 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4931 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4933 val &= ~PIPECONF_INTERLACE_MASK;
4934 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4935 val |= PIPECONF_INTERLACED_ILK;
4937 val |= PIPECONF_PROGRESSIVE;
4939 I915_WRITE(PIPECONF(pipe), val);
4940 POSTING_READ(PIPECONF(pipe));
4943 static void haswell_set_pipeconf(struct drm_crtc *crtc,
4944 struct drm_display_mode *adjusted_mode,
4947 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4949 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
4952 val = I915_READ(PIPECONF(cpu_transcoder));
4954 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4956 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4958 val &= ~PIPECONF_INTERLACE_MASK_HSW;
4959 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4960 val |= PIPECONF_INTERLACED_ILK;
4962 val |= PIPECONF_PROGRESSIVE;
4964 I915_WRITE(PIPECONF(cpu_transcoder), val);
4965 POSTING_READ(PIPECONF(cpu_transcoder));
4968 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4969 struct drm_display_mode *adjusted_mode,
4970 intel_clock_t *clock,
4971 bool *has_reduced_clock,
4972 intel_clock_t *reduced_clock)
4974 struct drm_device *dev = crtc->dev;
4975 struct drm_i915_private *dev_priv = dev->dev_private;
4976 struct intel_encoder *intel_encoder;
4978 const intel_limit_t *limit;
4979 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4981 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4982 switch (intel_encoder->type) {
4983 case INTEL_OUTPUT_LVDS:
4986 case INTEL_OUTPUT_SDVO:
4987 case INTEL_OUTPUT_HDMI:
4989 if (intel_encoder->needs_tv_clock)
4992 case INTEL_OUTPUT_TVOUT:
4998 refclk = ironlake_get_refclk(crtc);
5001 * Returns a set of divisors for the desired target clock with the given
5002 * refclk, or FALSE. The returned values represent the clock equation:
5003 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5005 limit = intel_limit(crtc, refclk);
5006 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5011 if (is_lvds && dev_priv->lvds_downclock_avail) {
5013 * Ensure we match the reduced clock's P to the target clock.
5014 * If the clocks don't match, we can't switch the display clock
5015 * by using the FP0/FP1. In such case we will disable the LVDS
5016 * downclock feature.
5018 *has_reduced_clock = limit->find_pll(limit, crtc,
5019 dev_priv->lvds_downclock,
5025 if (is_sdvo && is_tv)
5026 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5031 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5036 temp = I915_READ(SOUTH_CHICKEN1);
5037 if (temp & FDI_BC_BIFURCATION_SELECT)
5040 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5041 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5043 temp |= FDI_BC_BIFURCATION_SELECT;
5044 DRM_DEBUG_KMS("enabling fdi C rx\n");
5045 I915_WRITE(SOUTH_CHICKEN1, temp);
5046 POSTING_READ(SOUTH_CHICKEN1);
5049 static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5051 struct drm_device *dev = intel_crtc->base.dev;
5052 struct drm_i915_private *dev_priv = dev->dev_private;
5053 struct intel_crtc *pipe_B_crtc =
5054 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5056 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5057 intel_crtc->pipe, intel_crtc->fdi_lanes);
5058 if (intel_crtc->fdi_lanes > 4) {
5059 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5060 intel_crtc->pipe, intel_crtc->fdi_lanes);
5061 /* Clamp lanes to avoid programming the hw with bogus values. */
5062 intel_crtc->fdi_lanes = 4;
5067 if (dev_priv->num_pipe == 2)
5070 switch (intel_crtc->pipe) {
5074 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5075 intel_crtc->fdi_lanes > 2) {
5076 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5077 intel_crtc->pipe, intel_crtc->fdi_lanes);
5078 /* Clamp lanes to avoid programming the hw with bogus values. */
5079 intel_crtc->fdi_lanes = 2;
5084 if (intel_crtc->fdi_lanes > 2)
5085 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5087 cpt_enable_fdi_bc_bifurcation(dev);
5091 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5092 if (intel_crtc->fdi_lanes > 2) {
5093 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5094 intel_crtc->pipe, intel_crtc->fdi_lanes);
5095 /* Clamp lanes to avoid programming the hw with bogus values. */
5096 intel_crtc->fdi_lanes = 2;
5101 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5105 cpt_enable_fdi_bc_bifurcation(dev);
5113 static void ironlake_set_m_n(struct drm_crtc *crtc,
5114 struct drm_display_mode *mode,
5115 struct drm_display_mode *adjusted_mode)
5117 struct drm_device *dev = crtc->dev;
5118 struct drm_i915_private *dev_priv = dev->dev_private;
5119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5120 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
5121 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
5122 struct intel_link_m_n m_n = {0};
5123 int target_clock, pixel_multiplier, lane, link_bw;
5124 bool is_dp = false, is_cpu_edp = false;
5126 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5127 switch (intel_encoder->type) {
5128 case INTEL_OUTPUT_DISPLAYPORT:
5131 case INTEL_OUTPUT_EDP:
5133 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5135 edp_encoder = intel_encoder;
5141 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5143 /* CPU eDP doesn't require FDI link, so just set DP M/N
5144 according to current link config */
5146 intel_edp_link_config(edp_encoder, &lane, &link_bw);
5148 /* FDI is a binary signal running at ~2.7GHz, encoding
5149 * each output octet as 10 bits. The actual frequency
5150 * is stored as a divider into a 100MHz clock, and the
5151 * mode pixel clock is stored in units of 1KHz.
5152 * Hence the bw of each lane in terms of the mode signal
5155 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5158 /* [e]DP over FDI requires target mode clock instead of link clock. */
5160 target_clock = intel_edp_target_clock(edp_encoder, mode);
5162 target_clock = mode->clock;
5164 target_clock = adjusted_mode->clock;
5168 * Account for spread spectrum to avoid
5169 * oversubscribing the link. Max center spread
5170 * is 2.5%; use 5% for safety's sake.
5172 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
5173 lane = bps / (link_bw * 8) + 1;
5176 intel_crtc->fdi_lanes = lane;
5178 if (pixel_multiplier > 1)
5179 link_bw *= pixel_multiplier;
5180 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
5182 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5183 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5184 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5185 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
5188 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5189 struct drm_display_mode *adjusted_mode,
5190 intel_clock_t *clock, u32 fp)
5192 struct drm_crtc *crtc = &intel_crtc->base;
5193 struct drm_device *dev = crtc->dev;
5194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 struct intel_encoder *intel_encoder;
5197 int factor, pixel_multiplier, num_connectors = 0;
5198 bool is_lvds = false, is_sdvo = false, is_tv = false;
5199 bool is_dp = false, is_cpu_edp = false;
5201 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5202 switch (intel_encoder->type) {
5203 case INTEL_OUTPUT_LVDS:
5206 case INTEL_OUTPUT_SDVO:
5207 case INTEL_OUTPUT_HDMI:
5209 if (intel_encoder->needs_tv_clock)
5212 case INTEL_OUTPUT_TVOUT:
5215 case INTEL_OUTPUT_DISPLAYPORT:
5218 case INTEL_OUTPUT_EDP:
5220 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5228 /* Enable autotuning of the PLL clock (if permissible) */
5231 if ((intel_panel_use_ssc(dev_priv) &&
5232 dev_priv->lvds_ssc_freq == 100) ||
5233 intel_is_dual_link_lvds(dev))
5235 } else if (is_sdvo && is_tv)
5238 if (clock->m < factor * clock->n)
5244 dpll |= DPLLB_MODE_LVDS;
5246 dpll |= DPLLB_MODE_DAC_SERIAL;
5248 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5249 if (pixel_multiplier > 1) {
5250 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
5252 dpll |= DPLL_DVO_HIGH_SPEED;
5254 if (is_dp && !is_cpu_edp)
5255 dpll |= DPLL_DVO_HIGH_SPEED;
5257 /* compute bitmask from p1 value */
5258 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5260 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5262 switch (clock->p2) {
5264 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5267 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5270 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5273 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5277 if (is_sdvo && is_tv)
5278 dpll |= PLL_REF_INPUT_TVCLKINBC;
5280 /* XXX: just matching BIOS for now */
5281 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5283 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5284 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5286 dpll |= PLL_REF_INPUT_DREFCLK;
5291 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5292 struct drm_display_mode *mode,
5293 struct drm_display_mode *adjusted_mode,
5295 struct drm_framebuffer *fb)
5297 struct drm_device *dev = crtc->dev;
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
5301 int plane = intel_crtc->plane;
5302 int num_connectors = 0;
5303 intel_clock_t clock, reduced_clock;
5304 u32 dpll, fp = 0, fp2 = 0;
5305 bool ok, has_reduced_clock = false;
5306 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
5307 struct intel_encoder *encoder;
5309 bool dither, fdi_config_ok;
5311 for_each_encoder_on_crtc(dev, crtc, encoder) {
5312 switch (encoder->type) {
5313 case INTEL_OUTPUT_LVDS:
5316 case INTEL_OUTPUT_DISPLAYPORT:
5319 case INTEL_OUTPUT_EDP:
5321 if (!intel_encoder_is_pch_edp(&encoder->base))
5329 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5330 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5332 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5333 &has_reduced_clock, &reduced_clock);
5335 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5339 /* Ensure that the cursor is valid for the new mode before changing... */
5340 intel_crtc_update_cursor(crtc, true);
5342 /* determine panel color depth */
5343 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5345 if (is_lvds && dev_priv->lvds_dither)
5348 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5349 if (has_reduced_clock)
5350 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5353 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
5355 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5356 drm_mode_debug_printmodeline(mode);
5358 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5360 struct intel_pch_pll *pll;
5362 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5364 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5369 intel_put_pch_pll(intel_crtc);
5371 if (is_dp && !is_cpu_edp)
5372 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5374 for_each_encoder_on_crtc(dev, crtc, encoder)
5375 if (encoder->pre_pll_enable)
5376 encoder->pre_pll_enable(encoder);
5378 if (intel_crtc->pch_pll) {
5379 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5381 /* Wait for the clocks to stabilize. */
5382 POSTING_READ(intel_crtc->pch_pll->pll_reg);
5385 /* The pixel multiplier can only be updated once the
5386 * DPLL is enabled and the clocks are stable.
5388 * So write it again.
5390 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
5393 intel_crtc->lowfreq_avail = false;
5394 if (intel_crtc->pch_pll) {
5395 if (is_lvds && has_reduced_clock && i915_powersave) {
5396 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
5397 intel_crtc->lowfreq_avail = true;
5399 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
5403 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5405 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5406 * ironlake_check_fdi_lanes. */
5407 ironlake_set_m_n(crtc, mode, adjusted_mode);
5409 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
5411 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
5413 intel_wait_for_vblank(dev, pipe);
5415 /* Set up the display plane register */
5416 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5417 POSTING_READ(DSPCNTR(plane));
5419 ret = intel_pipe_set_base(crtc, x, y, fb);
5421 intel_update_watermarks(dev);
5423 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5425 return fdi_config_ok ? ret : -EINVAL;
5428 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5429 struct drm_display_mode *mode,
5430 struct drm_display_mode *adjusted_mode,
5432 struct drm_framebuffer *fb)
5434 struct drm_device *dev = crtc->dev;
5435 struct drm_i915_private *dev_priv = dev->dev_private;
5436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5437 int pipe = intel_crtc->pipe;
5438 int plane = intel_crtc->plane;
5439 int num_connectors = 0;
5440 bool is_dp = false, is_cpu_edp = false;
5441 struct intel_encoder *encoder;
5445 for_each_encoder_on_crtc(dev, crtc, encoder) {
5446 switch (encoder->type) {
5447 case INTEL_OUTPUT_DISPLAYPORT:
5450 case INTEL_OUTPUT_EDP:
5452 if (!intel_encoder_is_pch_edp(&encoder->base))
5461 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5463 intel_crtc->cpu_transcoder = pipe;
5465 /* We are not sure yet this won't happen. */
5466 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5467 INTEL_PCH_TYPE(dev));
5469 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5470 num_connectors, pipe_name(pipe));
5472 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
5473 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5475 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5477 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5480 /* Ensure that the cursor is valid for the new mode before changing... */
5481 intel_crtc_update_cursor(crtc, true);
5483 /* determine panel color depth */
5484 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5487 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5488 drm_mode_debug_printmodeline(mode);
5490 if (is_dp && !is_cpu_edp)
5491 intel_dp_set_m_n(crtc, mode, adjusted_mode);
5493 intel_crtc->lowfreq_avail = false;
5495 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5497 if (!is_dp || is_cpu_edp)
5498 ironlake_set_m_n(crtc, mode, adjusted_mode);
5500 haswell_set_pipeconf(crtc, adjusted_mode, dither);
5502 /* Set up the display plane register */
5503 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5504 POSTING_READ(DSPCNTR(plane));
5506 ret = intel_pipe_set_base(crtc, x, y, fb);
5508 intel_update_watermarks(dev);
5510 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5515 static int intel_crtc_mode_set(struct drm_crtc *crtc,
5516 struct drm_display_mode *mode,
5517 struct drm_display_mode *adjusted_mode,
5519 struct drm_framebuffer *fb)
5521 struct drm_device *dev = crtc->dev;
5522 struct drm_i915_private *dev_priv = dev->dev_private;
5523 struct drm_encoder_helper_funcs *encoder_funcs;
5524 struct intel_encoder *encoder;
5525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5526 int pipe = intel_crtc->pipe;
5529 drm_vblank_pre_modeset(dev, pipe);
5531 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
5533 drm_vblank_post_modeset(dev, pipe);
5538 for_each_encoder_on_crtc(dev, crtc, encoder) {
5539 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5540 encoder->base.base.id,
5541 drm_get_encoder_name(&encoder->base),
5542 mode->base.id, mode->name);
5543 encoder_funcs = encoder->base.helper_private;
5544 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5550 static bool intel_eld_uptodate(struct drm_connector *connector,
5551 int reg_eldv, uint32_t bits_eldv,
5552 int reg_elda, uint32_t bits_elda,
5555 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5556 uint8_t *eld = connector->eld;
5559 i = I915_READ(reg_eldv);
5568 i = I915_READ(reg_elda);
5570 I915_WRITE(reg_elda, i);
5572 for (i = 0; i < eld[2]; i++)
5573 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5579 static void g4x_write_eld(struct drm_connector *connector,
5580 struct drm_crtc *crtc)
5582 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5583 uint8_t *eld = connector->eld;
5588 i = I915_READ(G4X_AUD_VID_DID);
5590 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5591 eldv = G4X_ELDV_DEVCL_DEVBLC;
5593 eldv = G4X_ELDV_DEVCTG;
5595 if (intel_eld_uptodate(connector,
5596 G4X_AUD_CNTL_ST, eldv,
5597 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5598 G4X_HDMIW_HDMIEDID))
5601 i = I915_READ(G4X_AUD_CNTL_ST);
5602 i &= ~(eldv | G4X_ELD_ADDR);
5603 len = (i >> 9) & 0x1f; /* ELD buffer size */
5604 I915_WRITE(G4X_AUD_CNTL_ST, i);
5609 len = min_t(uint8_t, eld[2], len);
5610 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5611 for (i = 0; i < len; i++)
5612 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5614 i = I915_READ(G4X_AUD_CNTL_ST);
5616 I915_WRITE(G4X_AUD_CNTL_ST, i);
5619 static void haswell_write_eld(struct drm_connector *connector,
5620 struct drm_crtc *crtc)
5622 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5623 uint8_t *eld = connector->eld;
5624 struct drm_device *dev = crtc->dev;
5628 int pipe = to_intel_crtc(crtc)->pipe;
5631 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5632 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5633 int aud_config = HSW_AUD_CFG(pipe);
5634 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5637 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5639 /* Audio output enable */
5640 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5641 tmp = I915_READ(aud_cntrl_st2);
5642 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5643 I915_WRITE(aud_cntrl_st2, tmp);
5645 /* Wait for 1 vertical blank */
5646 intel_wait_for_vblank(dev, pipe);
5648 /* Set ELD valid state */
5649 tmp = I915_READ(aud_cntrl_st2);
5650 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5651 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5652 I915_WRITE(aud_cntrl_st2, tmp);
5653 tmp = I915_READ(aud_cntrl_st2);
5654 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5656 /* Enable HDMI mode */
5657 tmp = I915_READ(aud_config);
5658 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5659 /* clear N_programing_enable and N_value_index */
5660 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5661 I915_WRITE(aud_config, tmp);
5663 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5665 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5667 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5668 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5669 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5670 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5672 I915_WRITE(aud_config, 0);
5674 if (intel_eld_uptodate(connector,
5675 aud_cntrl_st2, eldv,
5676 aud_cntl_st, IBX_ELD_ADDRESS,
5680 i = I915_READ(aud_cntrl_st2);
5682 I915_WRITE(aud_cntrl_st2, i);
5687 i = I915_READ(aud_cntl_st);
5688 i &= ~IBX_ELD_ADDRESS;
5689 I915_WRITE(aud_cntl_st, i);
5690 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5691 DRM_DEBUG_DRIVER("port num:%d\n", i);
5693 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5694 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5695 for (i = 0; i < len; i++)
5696 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5698 i = I915_READ(aud_cntrl_st2);
5700 I915_WRITE(aud_cntrl_st2, i);
5704 static void ironlake_write_eld(struct drm_connector *connector,
5705 struct drm_crtc *crtc)
5707 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5708 uint8_t *eld = connector->eld;
5716 int pipe = to_intel_crtc(crtc)->pipe;
5718 if (HAS_PCH_IBX(connector->dev)) {
5719 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5720 aud_config = IBX_AUD_CFG(pipe);
5721 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
5722 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
5724 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5725 aud_config = CPT_AUD_CFG(pipe);
5726 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
5727 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
5730 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5732 i = I915_READ(aud_cntl_st);
5733 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5735 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5736 /* operate blindly on all ports */
5737 eldv = IBX_ELD_VALIDB;
5738 eldv |= IBX_ELD_VALIDB << 4;
5739 eldv |= IBX_ELD_VALIDB << 8;
5741 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
5742 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
5745 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5746 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5747 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5748 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5750 I915_WRITE(aud_config, 0);
5752 if (intel_eld_uptodate(connector,
5753 aud_cntrl_st2, eldv,
5754 aud_cntl_st, IBX_ELD_ADDRESS,
5758 i = I915_READ(aud_cntrl_st2);
5760 I915_WRITE(aud_cntrl_st2, i);
5765 i = I915_READ(aud_cntl_st);
5766 i &= ~IBX_ELD_ADDRESS;
5767 I915_WRITE(aud_cntl_st, i);
5769 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5770 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5771 for (i = 0; i < len; i++)
5772 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5774 i = I915_READ(aud_cntrl_st2);
5776 I915_WRITE(aud_cntrl_st2, i);
5779 void intel_write_eld(struct drm_encoder *encoder,
5780 struct drm_display_mode *mode)
5782 struct drm_crtc *crtc = encoder->crtc;
5783 struct drm_connector *connector;
5784 struct drm_device *dev = encoder->dev;
5785 struct drm_i915_private *dev_priv = dev->dev_private;
5787 connector = drm_select_eld(encoder, mode);
5791 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5793 drm_get_connector_name(connector),
5794 connector->encoder->base.id,
5795 drm_get_encoder_name(connector->encoder));
5797 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5799 if (dev_priv->display.write_eld)
5800 dev_priv->display.write_eld(connector, crtc);
5803 /** Loads the palette/gamma unit for the CRTC with the prepared values */
5804 void intel_crtc_load_lut(struct drm_crtc *crtc)
5806 struct drm_device *dev = crtc->dev;
5807 struct drm_i915_private *dev_priv = dev->dev_private;
5808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5809 int palreg = PALETTE(intel_crtc->pipe);
5812 /* The clocks have to be on to load the palette. */
5813 if (!crtc->enabled || !intel_crtc->active)
5816 /* use legacy palette for Ironlake */
5817 if (HAS_PCH_SPLIT(dev))
5818 palreg = LGC_PALETTE(intel_crtc->pipe);
5820 for (i = 0; i < 256; i++) {
5821 I915_WRITE(palreg + 4 * i,
5822 (intel_crtc->lut_r[i] << 16) |
5823 (intel_crtc->lut_g[i] << 8) |
5824 intel_crtc->lut_b[i]);
5828 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5830 struct drm_device *dev = crtc->dev;
5831 struct drm_i915_private *dev_priv = dev->dev_private;
5832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5833 bool visible = base != 0;
5836 if (intel_crtc->cursor_visible == visible)
5839 cntl = I915_READ(_CURACNTR);
5841 /* On these chipsets we can only modify the base whilst
5842 * the cursor is disabled.
5844 I915_WRITE(_CURABASE, base);
5846 cntl &= ~(CURSOR_FORMAT_MASK);
5847 /* XXX width must be 64, stride 256 => 0x00 << 28 */
5848 cntl |= CURSOR_ENABLE |
5849 CURSOR_GAMMA_ENABLE |
5852 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
5853 I915_WRITE(_CURACNTR, cntl);
5855 intel_crtc->cursor_visible = visible;
5858 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5860 struct drm_device *dev = crtc->dev;
5861 struct drm_i915_private *dev_priv = dev->dev_private;
5862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5863 int pipe = intel_crtc->pipe;
5864 bool visible = base != 0;
5866 if (intel_crtc->cursor_visible != visible) {
5867 uint32_t cntl = I915_READ(CURCNTR(pipe));
5869 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5870 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5871 cntl |= pipe << 28; /* Connect to correct pipe */
5873 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5874 cntl |= CURSOR_MODE_DISABLE;
5876 I915_WRITE(CURCNTR(pipe), cntl);
5878 intel_crtc->cursor_visible = visible;
5880 /* and commit changes on next vblank */
5881 I915_WRITE(CURBASE(pipe), base);
5884 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5886 struct drm_device *dev = crtc->dev;
5887 struct drm_i915_private *dev_priv = dev->dev_private;
5888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5889 int pipe = intel_crtc->pipe;
5890 bool visible = base != 0;
5892 if (intel_crtc->cursor_visible != visible) {
5893 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5895 cntl &= ~CURSOR_MODE;
5896 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5898 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5899 cntl |= CURSOR_MODE_DISABLE;
5901 I915_WRITE(CURCNTR_IVB(pipe), cntl);
5903 intel_crtc->cursor_visible = visible;
5905 /* and commit changes on next vblank */
5906 I915_WRITE(CURBASE_IVB(pipe), base);
5909 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
5910 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5913 struct drm_device *dev = crtc->dev;
5914 struct drm_i915_private *dev_priv = dev->dev_private;
5915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5916 int pipe = intel_crtc->pipe;
5917 int x = intel_crtc->cursor_x;
5918 int y = intel_crtc->cursor_y;
5924 if (on && crtc->enabled && crtc->fb) {
5925 base = intel_crtc->cursor_addr;
5926 if (x > (int) crtc->fb->width)
5929 if (y > (int) crtc->fb->height)
5935 if (x + intel_crtc->cursor_width < 0)
5938 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5941 pos |= x << CURSOR_X_SHIFT;
5944 if (y + intel_crtc->cursor_height < 0)
5947 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5950 pos |= y << CURSOR_Y_SHIFT;
5952 visible = base != 0;
5953 if (!visible && !intel_crtc->cursor_visible)
5956 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
5957 I915_WRITE(CURPOS_IVB(pipe), pos);
5958 ivb_update_cursor(crtc, base);
5960 I915_WRITE(CURPOS(pipe), pos);
5961 if (IS_845G(dev) || IS_I865G(dev))
5962 i845_update_cursor(crtc, base);
5964 i9xx_update_cursor(crtc, base);
5968 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
5969 struct drm_file *file,
5971 uint32_t width, uint32_t height)
5973 struct drm_device *dev = crtc->dev;
5974 struct drm_i915_private *dev_priv = dev->dev_private;
5975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5976 struct drm_i915_gem_object *obj;
5980 /* if we want to turn off the cursor ignore width and height */
5982 DRM_DEBUG_KMS("cursor off\n");
5985 mutex_lock(&dev->struct_mutex);
5989 /* Currently we only support 64x64 cursors */
5990 if (width != 64 || height != 64) {
5991 DRM_ERROR("we currently only support 64x64 cursors\n");
5995 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
5996 if (&obj->base == NULL)
5999 if (obj->base.size < width * height * 4) {
6000 DRM_ERROR("buffer is to small\n");
6005 /* we only need to pin inside GTT if cursor is non-phy */
6006 mutex_lock(&dev->struct_mutex);
6007 if (!dev_priv->info->cursor_needs_physical) {
6008 if (obj->tiling_mode) {
6009 DRM_ERROR("cursor cannot be tiled\n");
6014 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
6016 DRM_ERROR("failed to move cursor bo into the GTT\n");
6020 ret = i915_gem_object_put_fence(obj);
6022 DRM_ERROR("failed to release fence for cursor");
6026 addr = obj->gtt_offset;
6028 int align = IS_I830(dev) ? 16 * 1024 : 256;
6029 ret = i915_gem_attach_phys_object(dev, obj,
6030 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6033 DRM_ERROR("failed to attach phys object\n");
6036 addr = obj->phys_obj->handle->busaddr;
6040 I915_WRITE(CURSIZE, (height << 12) | width);
6043 if (intel_crtc->cursor_bo) {
6044 if (dev_priv->info->cursor_needs_physical) {
6045 if (intel_crtc->cursor_bo != obj)
6046 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6048 i915_gem_object_unpin(intel_crtc->cursor_bo);
6049 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
6052 mutex_unlock(&dev->struct_mutex);
6054 intel_crtc->cursor_addr = addr;
6055 intel_crtc->cursor_bo = obj;
6056 intel_crtc->cursor_width = width;
6057 intel_crtc->cursor_height = height;
6059 intel_crtc_update_cursor(crtc, true);
6063 i915_gem_object_unpin(obj);
6065 mutex_unlock(&dev->struct_mutex);
6067 drm_gem_object_unreference_unlocked(&obj->base);
6071 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6075 intel_crtc->cursor_x = x;
6076 intel_crtc->cursor_y = y;
6078 intel_crtc_update_cursor(crtc, true);
6083 /** Sets the color ramps on behalf of RandR */
6084 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6085 u16 blue, int regno)
6087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6089 intel_crtc->lut_r[regno] = red >> 8;
6090 intel_crtc->lut_g[regno] = green >> 8;
6091 intel_crtc->lut_b[regno] = blue >> 8;
6094 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6095 u16 *blue, int regno)
6097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6099 *red = intel_crtc->lut_r[regno] << 8;
6100 *green = intel_crtc->lut_g[regno] << 8;
6101 *blue = intel_crtc->lut_b[regno] << 8;
6104 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
6105 u16 *blue, uint32_t start, uint32_t size)
6107 int end = (start + size > 256) ? 256 : start + size, i;
6108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6110 for (i = start; i < end; i++) {
6111 intel_crtc->lut_r[i] = red[i] >> 8;
6112 intel_crtc->lut_g[i] = green[i] >> 8;
6113 intel_crtc->lut_b[i] = blue[i] >> 8;
6116 intel_crtc_load_lut(crtc);
6120 * Get a pipe with a simple mode set on it for doing load-based monitor
6123 * It will be up to the load-detect code to adjust the pipe as appropriate for
6124 * its requirements. The pipe will be connected to no other encoders.
6126 * Currently this code will only succeed if there is a pipe with no encoders
6127 * configured for it. In the future, it could choose to temporarily disable
6128 * some outputs to free up a pipe for its use.
6130 * \return crtc, or NULL if no pipes are available.
6133 /* VESA 640x480x72Hz mode to set on the pipe */
6134 static struct drm_display_mode load_detect_mode = {
6135 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6136 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6139 static struct drm_framebuffer *
6140 intel_framebuffer_create(struct drm_device *dev,
6141 struct drm_mode_fb_cmd2 *mode_cmd,
6142 struct drm_i915_gem_object *obj)
6144 struct intel_framebuffer *intel_fb;
6147 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6149 drm_gem_object_unreference_unlocked(&obj->base);
6150 return ERR_PTR(-ENOMEM);
6153 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6155 drm_gem_object_unreference_unlocked(&obj->base);
6157 return ERR_PTR(ret);
6160 return &intel_fb->base;
6164 intel_framebuffer_pitch_for_width(int width, int bpp)
6166 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6167 return ALIGN(pitch, 64);
6171 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6173 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6174 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6177 static struct drm_framebuffer *
6178 intel_framebuffer_create_for_mode(struct drm_device *dev,
6179 struct drm_display_mode *mode,
6182 struct drm_i915_gem_object *obj;
6183 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
6185 obj = i915_gem_alloc_object(dev,
6186 intel_framebuffer_size_for_mode(mode, bpp));
6188 return ERR_PTR(-ENOMEM);
6190 mode_cmd.width = mode->hdisplay;
6191 mode_cmd.height = mode->vdisplay;
6192 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6194 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
6196 return intel_framebuffer_create(dev, &mode_cmd, obj);
6199 static struct drm_framebuffer *
6200 mode_fits_in_fbdev(struct drm_device *dev,
6201 struct drm_display_mode *mode)
6203 struct drm_i915_private *dev_priv = dev->dev_private;
6204 struct drm_i915_gem_object *obj;
6205 struct drm_framebuffer *fb;
6207 if (dev_priv->fbdev == NULL)
6210 obj = dev_priv->fbdev->ifb.obj;
6214 fb = &dev_priv->fbdev->ifb.base;
6215 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6216 fb->bits_per_pixel))
6219 if (obj->base.size < mode->vdisplay * fb->pitches[0])
6225 bool intel_get_load_detect_pipe(struct drm_connector *connector,
6226 struct drm_display_mode *mode,
6227 struct intel_load_detect_pipe *old)
6229 struct intel_crtc *intel_crtc;
6230 struct intel_encoder *intel_encoder =
6231 intel_attached_encoder(connector);
6232 struct drm_crtc *possible_crtc;
6233 struct drm_encoder *encoder = &intel_encoder->base;
6234 struct drm_crtc *crtc = NULL;
6235 struct drm_device *dev = encoder->dev;
6236 struct drm_framebuffer *fb;
6239 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6240 connector->base.id, drm_get_connector_name(connector),
6241 encoder->base.id, drm_get_encoder_name(encoder));
6244 * Algorithm gets a little messy:
6246 * - if the connector already has an assigned crtc, use it (but make
6247 * sure it's on first)
6249 * - try to find the first unused crtc that can drive this connector,
6250 * and use that if we find one
6253 /* See if we already have a CRTC for this connector */
6254 if (encoder->crtc) {
6255 crtc = encoder->crtc;
6257 old->dpms_mode = connector->dpms;
6258 old->load_detect_temp = false;
6260 /* Make sure the crtc and connector are running */
6261 if (connector->dpms != DRM_MODE_DPMS_ON)
6262 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
6267 /* Find an unused one (if possible) */
6268 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6270 if (!(encoder->possible_crtcs & (1 << i)))
6272 if (!possible_crtc->enabled) {
6273 crtc = possible_crtc;
6279 * If we didn't find an unused CRTC, don't use any.
6282 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6286 intel_encoder->new_crtc = to_intel_crtc(crtc);
6287 to_intel_connector(connector)->new_encoder = intel_encoder;
6289 intel_crtc = to_intel_crtc(crtc);
6290 old->dpms_mode = connector->dpms;
6291 old->load_detect_temp = true;
6292 old->release_fb = NULL;
6295 mode = &load_detect_mode;
6297 /* We need a framebuffer large enough to accommodate all accesses
6298 * that the plane may generate whilst we perform load detection.
6299 * We can not rely on the fbcon either being present (we get called
6300 * during its initialisation to detect all boot displays, or it may
6301 * not even exist) or that it is large enough to satisfy the
6304 fb = mode_fits_in_fbdev(dev, mode);
6306 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6307 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6308 old->release_fb = fb;
6310 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6312 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6316 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6317 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
6318 if (old->release_fb)
6319 old->release_fb->funcs->destroy(old->release_fb);
6323 /* let the connector get through one full cycle before testing */
6324 intel_wait_for_vblank(dev, intel_crtc->pipe);
6328 void intel_release_load_detect_pipe(struct drm_connector *connector,
6329 struct intel_load_detect_pipe *old)
6331 struct intel_encoder *intel_encoder =
6332 intel_attached_encoder(connector);
6333 struct drm_encoder *encoder = &intel_encoder->base;
6335 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6336 connector->base.id, drm_get_connector_name(connector),
6337 encoder->base.id, drm_get_encoder_name(encoder));
6339 if (old->load_detect_temp) {
6340 struct drm_crtc *crtc = encoder->crtc;
6342 to_intel_connector(connector)->new_encoder = NULL;
6343 intel_encoder->new_crtc = NULL;
6344 intel_set_mode(crtc, NULL, 0, 0, NULL);
6346 if (old->release_fb)
6347 old->release_fb->funcs->destroy(old->release_fb);
6352 /* Switch crtc and encoder back off if necessary */
6353 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6354 connector->funcs->dpms(connector, old->dpms_mode);
6357 /* Returns the clock of the currently programmed mode of the given pipe. */
6358 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6360 struct drm_i915_private *dev_priv = dev->dev_private;
6361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6362 int pipe = intel_crtc->pipe;
6363 u32 dpll = I915_READ(DPLL(pipe));
6365 intel_clock_t clock;
6367 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6368 fp = I915_READ(FP0(pipe));
6370 fp = I915_READ(FP1(pipe));
6372 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6373 if (IS_PINEVIEW(dev)) {
6374 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6375 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6377 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6378 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6381 if (!IS_GEN2(dev)) {
6382 if (IS_PINEVIEW(dev))
6383 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6384 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6386 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6387 DPLL_FPA01_P1_POST_DIV_SHIFT);
6389 switch (dpll & DPLL_MODE_MASK) {
6390 case DPLLB_MODE_DAC_SERIAL:
6391 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6394 case DPLLB_MODE_LVDS:
6395 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6399 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
6400 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6404 /* XXX: Handle the 100Mhz refclk */
6405 intel_clock(dev, 96000, &clock);
6407 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6410 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6411 DPLL_FPA01_P1_POST_DIV_SHIFT);
6414 if ((dpll & PLL_REF_INPUT_MASK) ==
6415 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6416 /* XXX: might not be 66MHz */
6417 intel_clock(dev, 66000, &clock);
6419 intel_clock(dev, 48000, &clock);
6421 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6424 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6425 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6427 if (dpll & PLL_P2_DIVIDE_BY_4)
6432 intel_clock(dev, 48000, &clock);
6436 /* XXX: It would be nice to validate the clocks, but we can't reuse
6437 * i830PllIsValid() because it relies on the xf86_config connector
6438 * configuration being accurate, which it isn't necessarily.
6444 /** Returns the currently programmed mode of the given pipe. */
6445 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6446 struct drm_crtc *crtc)
6448 struct drm_i915_private *dev_priv = dev->dev_private;
6449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6450 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
6451 struct drm_display_mode *mode;
6452 int htot = I915_READ(HTOTAL(cpu_transcoder));
6453 int hsync = I915_READ(HSYNC(cpu_transcoder));
6454 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6455 int vsync = I915_READ(VSYNC(cpu_transcoder));
6457 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6461 mode->clock = intel_crtc_clock_get(dev, crtc);
6462 mode->hdisplay = (htot & 0xffff) + 1;
6463 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6464 mode->hsync_start = (hsync & 0xffff) + 1;
6465 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6466 mode->vdisplay = (vtot & 0xffff) + 1;
6467 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6468 mode->vsync_start = (vsync & 0xffff) + 1;
6469 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6471 drm_mode_set_name(mode);
6476 static void intel_increase_pllclock(struct drm_crtc *crtc)
6478 struct drm_device *dev = crtc->dev;
6479 drm_i915_private_t *dev_priv = dev->dev_private;
6480 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6481 int pipe = intel_crtc->pipe;
6482 int dpll_reg = DPLL(pipe);
6485 if (HAS_PCH_SPLIT(dev))
6488 if (!dev_priv->lvds_downclock_avail)
6491 dpll = I915_READ(dpll_reg);
6492 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
6493 DRM_DEBUG_DRIVER("upclocking LVDS\n");
6495 assert_panel_unlocked(dev_priv, pipe);
6497 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6498 I915_WRITE(dpll_reg, dpll);
6499 intel_wait_for_vblank(dev, pipe);
6501 dpll = I915_READ(dpll_reg);
6502 if (dpll & DISPLAY_RATE_SELECT_FPA1)
6503 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
6507 static void intel_decrease_pllclock(struct drm_crtc *crtc)
6509 struct drm_device *dev = crtc->dev;
6510 drm_i915_private_t *dev_priv = dev->dev_private;
6511 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6513 if (HAS_PCH_SPLIT(dev))
6516 if (!dev_priv->lvds_downclock_avail)
6520 * Since this is called by a timer, we should never get here in
6523 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
6524 int pipe = intel_crtc->pipe;
6525 int dpll_reg = DPLL(pipe);
6528 DRM_DEBUG_DRIVER("downclocking LVDS\n");
6530 assert_panel_unlocked(dev_priv, pipe);
6532 dpll = I915_READ(dpll_reg);
6533 dpll |= DISPLAY_RATE_SELECT_FPA1;
6534 I915_WRITE(dpll_reg, dpll);
6535 intel_wait_for_vblank(dev, pipe);
6536 dpll = I915_READ(dpll_reg);
6537 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
6538 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
6543 void intel_mark_busy(struct drm_device *dev)
6545 i915_update_gfx_val(dev->dev_private);
6548 void intel_mark_idle(struct drm_device *dev)
6552 void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6554 struct drm_device *dev = obj->base.dev;
6555 struct drm_crtc *crtc;
6557 if (!i915_powersave)
6560 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6564 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6565 intel_increase_pllclock(crtc);
6569 void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
6571 struct drm_device *dev = obj->base.dev;
6572 struct drm_crtc *crtc;
6574 if (!i915_powersave)
6577 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6581 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6582 intel_decrease_pllclock(crtc);
6586 static void intel_crtc_destroy(struct drm_crtc *crtc)
6588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6589 struct drm_device *dev = crtc->dev;
6590 struct intel_unpin_work *work;
6591 unsigned long flags;
6593 spin_lock_irqsave(&dev->event_lock, flags);
6594 work = intel_crtc->unpin_work;
6595 intel_crtc->unpin_work = NULL;
6596 spin_unlock_irqrestore(&dev->event_lock, flags);
6599 cancel_work_sync(&work->work);
6603 drm_crtc_cleanup(crtc);
6608 static void intel_unpin_work_fn(struct work_struct *__work)
6610 struct intel_unpin_work *work =
6611 container_of(__work, struct intel_unpin_work, work);
6612 struct drm_device *dev = work->crtc->dev;
6614 mutex_lock(&dev->struct_mutex);
6615 intel_unpin_fb_obj(work->old_fb_obj);
6616 drm_gem_object_unreference(&work->pending_flip_obj->base);
6617 drm_gem_object_unreference(&work->old_fb_obj->base);
6619 intel_update_fbc(dev);
6620 mutex_unlock(&dev->struct_mutex);
6622 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6623 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6628 static void do_intel_finish_page_flip(struct drm_device *dev,
6629 struct drm_crtc *crtc)
6631 drm_i915_private_t *dev_priv = dev->dev_private;
6632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6633 struct intel_unpin_work *work;
6634 struct drm_i915_gem_object *obj;
6635 unsigned long flags;
6637 /* Ignore early vblank irqs */
6638 if (intel_crtc == NULL)
6641 spin_lock_irqsave(&dev->event_lock, flags);
6642 work = intel_crtc->unpin_work;
6643 if (work == NULL || !work->pending) {
6644 spin_unlock_irqrestore(&dev->event_lock, flags);
6648 intel_crtc->unpin_work = NULL;
6651 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6653 drm_vblank_put(dev, intel_crtc->pipe);
6655 spin_unlock_irqrestore(&dev->event_lock, flags);
6657 obj = work->old_fb_obj;
6659 wake_up(&dev_priv->pending_flip_queue);
6661 queue_work(dev_priv->wq, &work->work);
6663 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6666 void intel_finish_page_flip(struct drm_device *dev, int pipe)
6668 drm_i915_private_t *dev_priv = dev->dev_private;
6669 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6671 do_intel_finish_page_flip(dev, crtc);
6674 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6676 drm_i915_private_t *dev_priv = dev->dev_private;
6677 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6679 do_intel_finish_page_flip(dev, crtc);
6682 void intel_prepare_page_flip(struct drm_device *dev, int plane)
6684 drm_i915_private_t *dev_priv = dev->dev_private;
6685 struct intel_crtc *intel_crtc =
6686 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6687 unsigned long flags;
6689 spin_lock_irqsave(&dev->event_lock, flags);
6690 if (intel_crtc->unpin_work) {
6691 if ((++intel_crtc->unpin_work->pending) > 1)
6692 DRM_ERROR("Prepared flip multiple times\n");
6694 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6696 spin_unlock_irqrestore(&dev->event_lock, flags);
6699 static int intel_gen2_queue_flip(struct drm_device *dev,
6700 struct drm_crtc *crtc,
6701 struct drm_framebuffer *fb,
6702 struct drm_i915_gem_object *obj)
6704 struct drm_i915_private *dev_priv = dev->dev_private;
6705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6707 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6710 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6714 ret = intel_ring_begin(ring, 6);
6718 /* Can't queue multiple flips, so wait for the previous
6719 * one to finish before executing the next.
6721 if (intel_crtc->plane)
6722 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6724 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6725 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6726 intel_ring_emit(ring, MI_NOOP);
6727 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6728 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6729 intel_ring_emit(ring, fb->pitches[0]);
6730 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6731 intel_ring_emit(ring, 0); /* aux display base address, unused */
6732 intel_ring_advance(ring);
6736 intel_unpin_fb_obj(obj);
6741 static int intel_gen3_queue_flip(struct drm_device *dev,
6742 struct drm_crtc *crtc,
6743 struct drm_framebuffer *fb,
6744 struct drm_i915_gem_object *obj)
6746 struct drm_i915_private *dev_priv = dev->dev_private;
6747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6749 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6752 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6756 ret = intel_ring_begin(ring, 6);
6760 if (intel_crtc->plane)
6761 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6763 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6764 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6765 intel_ring_emit(ring, MI_NOOP);
6766 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6767 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6768 intel_ring_emit(ring, fb->pitches[0]);
6769 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6770 intel_ring_emit(ring, MI_NOOP);
6772 intel_ring_advance(ring);
6776 intel_unpin_fb_obj(obj);
6781 static int intel_gen4_queue_flip(struct drm_device *dev,
6782 struct drm_crtc *crtc,
6783 struct drm_framebuffer *fb,
6784 struct drm_i915_gem_object *obj)
6786 struct drm_i915_private *dev_priv = dev->dev_private;
6787 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6788 uint32_t pf, pipesrc;
6789 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6792 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6796 ret = intel_ring_begin(ring, 4);
6800 /* i965+ uses the linear or tiled offsets from the
6801 * Display Registers (which do not change across a page-flip)
6802 * so we need only reprogram the base address.
6804 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6805 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6806 intel_ring_emit(ring, fb->pitches[0]);
6807 intel_ring_emit(ring,
6808 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
6811 /* XXX Enabling the panel-fitter across page-flip is so far
6812 * untested on non-native modes, so ignore it for now.
6813 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6816 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6817 intel_ring_emit(ring, pf | pipesrc);
6818 intel_ring_advance(ring);
6822 intel_unpin_fb_obj(obj);
6827 static int intel_gen6_queue_flip(struct drm_device *dev,
6828 struct drm_crtc *crtc,
6829 struct drm_framebuffer *fb,
6830 struct drm_i915_gem_object *obj)
6832 struct drm_i915_private *dev_priv = dev->dev_private;
6833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6834 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
6835 uint32_t pf, pipesrc;
6838 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6842 ret = intel_ring_begin(ring, 4);
6846 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6847 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6848 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
6849 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6851 /* Contrary to the suggestions in the documentation,
6852 * "Enable Panel Fitter" does not seem to be required when page
6853 * flipping with a non-native mode, and worse causes a normal
6855 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6858 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6859 intel_ring_emit(ring, pf | pipesrc);
6860 intel_ring_advance(ring);
6864 intel_unpin_fb_obj(obj);
6870 * On gen7 we currently use the blit ring because (in early silicon at least)
6871 * the render ring doesn't give us interrpts for page flip completion, which
6872 * means clients will hang after the first flip is queued. Fortunately the
6873 * blit ring generates interrupts properly, so use it instead.
6875 static int intel_gen7_queue_flip(struct drm_device *dev,
6876 struct drm_crtc *crtc,
6877 struct drm_framebuffer *fb,
6878 struct drm_i915_gem_object *obj)
6880 struct drm_i915_private *dev_priv = dev->dev_private;
6881 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6882 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
6883 uint32_t plane_bit = 0;
6886 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6890 switch(intel_crtc->plane) {
6892 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6895 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6898 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6901 WARN_ONCE(1, "unknown plane in flip command\n");
6906 ret = intel_ring_begin(ring, 4);
6910 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
6911 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
6912 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
6913 intel_ring_emit(ring, (MI_NOOP));
6914 intel_ring_advance(ring);
6918 intel_unpin_fb_obj(obj);
6923 static int intel_default_queue_flip(struct drm_device *dev,
6924 struct drm_crtc *crtc,
6925 struct drm_framebuffer *fb,
6926 struct drm_i915_gem_object *obj)
6931 static int intel_crtc_page_flip(struct drm_crtc *crtc,
6932 struct drm_framebuffer *fb,
6933 struct drm_pending_vblank_event *event)
6935 struct drm_device *dev = crtc->dev;
6936 struct drm_i915_private *dev_priv = dev->dev_private;
6937 struct intel_framebuffer *intel_fb;
6938 struct drm_i915_gem_object *obj;
6939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6940 struct intel_unpin_work *work;
6941 unsigned long flags;
6944 /* Can't change pixel format via MI display flips. */
6945 if (fb->pixel_format != crtc->fb->pixel_format)
6949 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6950 * Note that pitch changes could also affect these register.
6952 if (INTEL_INFO(dev)->gen > 3 &&
6953 (fb->offsets[0] != crtc->fb->offsets[0] ||
6954 fb->pitches[0] != crtc->fb->pitches[0]))
6957 work = kzalloc(sizeof *work, GFP_KERNEL);
6961 work->event = event;
6963 intel_fb = to_intel_framebuffer(crtc->fb);
6964 work->old_fb_obj = intel_fb->obj;
6965 INIT_WORK(&work->work, intel_unpin_work_fn);
6967 ret = drm_vblank_get(dev, intel_crtc->pipe);
6971 /* We borrow the event spin lock for protecting unpin_work */
6972 spin_lock_irqsave(&dev->event_lock, flags);
6973 if (intel_crtc->unpin_work) {
6974 spin_unlock_irqrestore(&dev->event_lock, flags);
6976 drm_vblank_put(dev, intel_crtc->pipe);
6978 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6981 intel_crtc->unpin_work = work;
6982 spin_unlock_irqrestore(&dev->event_lock, flags);
6984 intel_fb = to_intel_framebuffer(fb);
6985 obj = intel_fb->obj;
6987 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
6988 flush_workqueue(dev_priv->wq);
6990 ret = i915_mutex_lock_interruptible(dev);
6994 /* Reference the objects for the scheduled work. */
6995 drm_gem_object_reference(&work->old_fb_obj->base);
6996 drm_gem_object_reference(&obj->base);
7000 work->pending_flip_obj = obj;
7002 work->enable_stall_check = true;
7004 atomic_inc(&intel_crtc->unpin_work_count);
7006 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7008 goto cleanup_pending;
7010 intel_disable_fbc(dev);
7011 intel_mark_fb_busy(obj);
7012 mutex_unlock(&dev->struct_mutex);
7014 trace_i915_flip_request(intel_crtc->plane, obj);
7019 atomic_dec(&intel_crtc->unpin_work_count);
7020 drm_gem_object_unreference(&work->old_fb_obj->base);
7021 drm_gem_object_unreference(&obj->base);
7022 mutex_unlock(&dev->struct_mutex);
7025 spin_lock_irqsave(&dev->event_lock, flags);
7026 intel_crtc->unpin_work = NULL;
7027 spin_unlock_irqrestore(&dev->event_lock, flags);
7029 drm_vblank_put(dev, intel_crtc->pipe);
7036 static struct drm_crtc_helper_funcs intel_helper_funcs = {
7037 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7038 .load_lut = intel_crtc_load_lut,
7039 .disable = intel_crtc_noop,
7042 bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7044 struct intel_encoder *other_encoder;
7045 struct drm_crtc *crtc = &encoder->new_crtc->base;
7050 list_for_each_entry(other_encoder,
7051 &crtc->dev->mode_config.encoder_list,
7054 if (&other_encoder->new_crtc->base != crtc ||
7055 encoder == other_encoder)
7064 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7065 struct drm_crtc *crtc)
7067 struct drm_device *dev;
7068 struct drm_crtc *tmp;
7071 WARN(!crtc, "checking null crtc?\n");
7075 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7081 if (encoder->possible_crtcs & crtc_mask)
7087 * intel_modeset_update_staged_output_state
7089 * Updates the staged output configuration state, e.g. after we've read out the
7092 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7094 struct intel_encoder *encoder;
7095 struct intel_connector *connector;
7097 list_for_each_entry(connector, &dev->mode_config.connector_list,
7099 connector->new_encoder =
7100 to_intel_encoder(connector->base.encoder);
7103 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7106 to_intel_crtc(encoder->base.crtc);
7111 * intel_modeset_commit_output_state
7113 * This function copies the stage display pipe configuration to the real one.
7115 static void intel_modeset_commit_output_state(struct drm_device *dev)
7117 struct intel_encoder *encoder;
7118 struct intel_connector *connector;
7120 list_for_each_entry(connector, &dev->mode_config.connector_list,
7122 connector->base.encoder = &connector->new_encoder->base;
7125 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7127 encoder->base.crtc = &encoder->new_crtc->base;
7131 static struct drm_display_mode *
7132 intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7133 struct drm_display_mode *mode)
7135 struct drm_device *dev = crtc->dev;
7136 struct drm_display_mode *adjusted_mode;
7137 struct drm_encoder_helper_funcs *encoder_funcs;
7138 struct intel_encoder *encoder;
7140 adjusted_mode = drm_mode_duplicate(dev, mode);
7142 return ERR_PTR(-ENOMEM);
7144 /* Pass our mode to the connectors and the CRTC to give them a chance to
7145 * adjust it according to limitations or connector properties, and also
7146 * a chance to reject the mode entirely.
7148 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7151 if (&encoder->new_crtc->base != crtc)
7153 encoder_funcs = encoder->base.helper_private;
7154 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7156 DRM_DEBUG_KMS("Encoder fixup failed\n");
7161 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7162 DRM_DEBUG_KMS("CRTC fixup failed\n");
7165 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7167 return adjusted_mode;
7169 drm_mode_destroy(dev, adjusted_mode);
7170 return ERR_PTR(-EINVAL);
7173 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
7174 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7176 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7177 unsigned *prepare_pipes, unsigned *disable_pipes)
7179 struct intel_crtc *intel_crtc;
7180 struct drm_device *dev = crtc->dev;
7181 struct intel_encoder *encoder;
7182 struct intel_connector *connector;
7183 struct drm_crtc *tmp_crtc;
7185 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7187 /* Check which crtcs have changed outputs connected to them, these need
7188 * to be part of the prepare_pipes mask. We don't (yet) support global
7189 * modeset across multiple crtcs, so modeset_pipes will only have one
7190 * bit set at most. */
7191 list_for_each_entry(connector, &dev->mode_config.connector_list,
7193 if (connector->base.encoder == &connector->new_encoder->base)
7196 if (connector->base.encoder) {
7197 tmp_crtc = connector->base.encoder->crtc;
7199 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7202 if (connector->new_encoder)
7204 1 << connector->new_encoder->new_crtc->pipe;
7207 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7209 if (encoder->base.crtc == &encoder->new_crtc->base)
7212 if (encoder->base.crtc) {
7213 tmp_crtc = encoder->base.crtc;
7215 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7218 if (encoder->new_crtc)
7219 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7222 /* Check for any pipes that will be fully disabled ... */
7223 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7227 /* Don't try to disable disabled crtcs. */
7228 if (!intel_crtc->base.enabled)
7231 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7233 if (encoder->new_crtc == intel_crtc)
7238 *disable_pipes |= 1 << intel_crtc->pipe;
7242 /* set_mode is also used to update properties on life display pipes. */
7243 intel_crtc = to_intel_crtc(crtc);
7245 *prepare_pipes |= 1 << intel_crtc->pipe;
7247 /* We only support modeset on one single crtc, hence we need to do that
7248 * only for the passed in crtc iff we change anything else than just
7251 * This is actually not true, to be fully compatible with the old crtc
7252 * helper we automatically disable _any_ output (i.e. doesn't need to be
7253 * connected to the crtc we're modesetting on) if it's disconnected.
7254 * Which is a rather nutty api (since changed the output configuration
7255 * without userspace's explicit request can lead to confusion), but
7256 * alas. Hence we currently need to modeset on all pipes we prepare. */
7258 *modeset_pipes = *prepare_pipes;
7260 /* ... and mask these out. */
7261 *modeset_pipes &= ~(*disable_pipes);
7262 *prepare_pipes &= ~(*disable_pipes);
7265 static bool intel_crtc_in_use(struct drm_crtc *crtc)
7267 struct drm_encoder *encoder;
7268 struct drm_device *dev = crtc->dev;
7270 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7271 if (encoder->crtc == crtc)
7278 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7280 struct intel_encoder *intel_encoder;
7281 struct intel_crtc *intel_crtc;
7282 struct drm_connector *connector;
7284 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7286 if (!intel_encoder->base.crtc)
7289 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7291 if (prepare_pipes & (1 << intel_crtc->pipe))
7292 intel_encoder->connectors_active = false;
7295 intel_modeset_commit_output_state(dev);
7297 /* Update computed state. */
7298 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7300 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7303 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7304 if (!connector->encoder || !connector->encoder->crtc)
7307 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7309 if (prepare_pipes & (1 << intel_crtc->pipe)) {
7310 struct drm_property *dpms_property =
7311 dev->mode_config.dpms_property;
7313 connector->dpms = DRM_MODE_DPMS_ON;
7314 drm_object_property_set_value(&connector->base,
7318 intel_encoder = to_intel_encoder(connector->encoder);
7319 intel_encoder->connectors_active = true;
7325 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7326 list_for_each_entry((intel_crtc), \
7327 &(dev)->mode_config.crtc_list, \
7329 if (mask & (1 <<(intel_crtc)->pipe)) \
7332 intel_modeset_check_state(struct drm_device *dev)
7334 struct intel_crtc *crtc;
7335 struct intel_encoder *encoder;
7336 struct intel_connector *connector;
7338 list_for_each_entry(connector, &dev->mode_config.connector_list,
7340 /* This also checks the encoder/connector hw state with the
7341 * ->get_hw_state callbacks. */
7342 intel_connector_check_state(connector);
7344 WARN(&connector->new_encoder->base != connector->base.encoder,
7345 "connector's staged encoder doesn't match current encoder\n");
7348 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7350 bool enabled = false;
7351 bool active = false;
7352 enum pipe pipe, tracked_pipe;
7354 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7355 encoder->base.base.id,
7356 drm_get_encoder_name(&encoder->base));
7358 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7359 "encoder's stage crtc doesn't match current crtc\n");
7360 WARN(encoder->connectors_active && !encoder->base.crtc,
7361 "encoder's active_connectors set, but no crtc\n");
7363 list_for_each_entry(connector, &dev->mode_config.connector_list,
7365 if (connector->base.encoder != &encoder->base)
7368 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7371 WARN(!!encoder->base.crtc != enabled,
7372 "encoder's enabled state mismatch "
7373 "(expected %i, found %i)\n",
7374 !!encoder->base.crtc, enabled);
7375 WARN(active && !encoder->base.crtc,
7376 "active encoder with no crtc\n");
7378 WARN(encoder->connectors_active != active,
7379 "encoder's computed active state doesn't match tracked active state "
7380 "(expected %i, found %i)\n", active, encoder->connectors_active);
7382 active = encoder->get_hw_state(encoder, &pipe);
7383 WARN(active != encoder->connectors_active,
7384 "encoder's hw state doesn't match sw tracking "
7385 "(expected %i, found %i)\n",
7386 encoder->connectors_active, active);
7388 if (!encoder->base.crtc)
7391 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7392 WARN(active && pipe != tracked_pipe,
7393 "active encoder's pipe doesn't match"
7394 "(expected %i, found %i)\n",
7395 tracked_pipe, pipe);
7399 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7401 bool enabled = false;
7402 bool active = false;
7404 DRM_DEBUG_KMS("[CRTC:%d]\n",
7405 crtc->base.base.id);
7407 WARN(crtc->active && !crtc->base.enabled,
7408 "active crtc, but not enabled in sw tracking\n");
7410 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7412 if (encoder->base.crtc != &crtc->base)
7415 if (encoder->connectors_active)
7418 WARN(active != crtc->active,
7419 "crtc's computed active state doesn't match tracked active state "
7420 "(expected %i, found %i)\n", active, crtc->active);
7421 WARN(enabled != crtc->base.enabled,
7422 "crtc's computed enabled state doesn't match tracked enabled state "
7423 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7425 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7429 int intel_set_mode(struct drm_crtc *crtc,
7430 struct drm_display_mode *mode,
7431 int x, int y, struct drm_framebuffer *fb)
7433 struct drm_device *dev = crtc->dev;
7434 drm_i915_private_t *dev_priv = dev->dev_private;
7435 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
7436 struct intel_crtc *intel_crtc;
7437 unsigned disable_pipes, prepare_pipes, modeset_pipes;
7440 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
7443 saved_hwmode = saved_mode + 1;
7445 intel_modeset_affected_pipes(crtc, &modeset_pipes,
7446 &prepare_pipes, &disable_pipes);
7448 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7449 modeset_pipes, prepare_pipes, disable_pipes);
7451 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7452 intel_crtc_disable(&intel_crtc->base);
7454 *saved_hwmode = crtc->hwmode;
7455 *saved_mode = crtc->mode;
7457 /* Hack: Because we don't (yet) support global modeset on multiple
7458 * crtcs, we don't keep track of the new mode for more than one crtc.
7459 * Hence simply check whether any bit is set in modeset_pipes in all the
7460 * pieces of code that are not yet converted to deal with mutliple crtcs
7461 * changing their mode at the same time. */
7462 adjusted_mode = NULL;
7463 if (modeset_pipes) {
7464 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7465 if (IS_ERR(adjusted_mode)) {
7466 ret = PTR_ERR(adjusted_mode);
7471 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7472 if (intel_crtc->base.enabled)
7473 dev_priv->display.crtc_disable(&intel_crtc->base);
7476 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7477 * to set it here already despite that we pass it down the callchain.
7482 /* Only after disabling all output pipelines that will be changed can we
7483 * update the the output configuration. */
7484 intel_modeset_update_state(dev, prepare_pipes);
7486 if (dev_priv->display.modeset_global_resources)
7487 dev_priv->display.modeset_global_resources(dev);
7489 /* Set up the DPLL and any encoders state that needs to adjust or depend
7492 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7493 ret = intel_crtc_mode_set(&intel_crtc->base,
7494 mode, adjusted_mode,
7500 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
7501 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7502 dev_priv->display.crtc_enable(&intel_crtc->base);
7504 if (modeset_pipes) {
7505 /* Store real post-adjustment hardware mode. */
7506 crtc->hwmode = *adjusted_mode;
7508 /* Calculate and store various constants which
7509 * are later needed by vblank and swap-completion
7510 * timestamping. They are derived from true hwmode.
7512 drm_calc_timestamping_constants(crtc);
7515 /* FIXME: add subpixel order */
7517 drm_mode_destroy(dev, adjusted_mode);
7518 if (ret && crtc->enabled) {
7519 crtc->hwmode = *saved_hwmode;
7520 crtc->mode = *saved_mode;
7522 intel_modeset_check_state(dev);
7530 void intel_crtc_restore_mode(struct drm_crtc *crtc)
7532 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
7535 #undef for_each_intel_crtc_masked
7537 static void intel_set_config_free(struct intel_set_config *config)
7542 kfree(config->save_connector_encoders);
7543 kfree(config->save_encoder_crtcs);
7547 static int intel_set_config_save_state(struct drm_device *dev,
7548 struct intel_set_config *config)
7550 struct drm_encoder *encoder;
7551 struct drm_connector *connector;
7554 config->save_encoder_crtcs =
7555 kcalloc(dev->mode_config.num_encoder,
7556 sizeof(struct drm_crtc *), GFP_KERNEL);
7557 if (!config->save_encoder_crtcs)
7560 config->save_connector_encoders =
7561 kcalloc(dev->mode_config.num_connector,
7562 sizeof(struct drm_encoder *), GFP_KERNEL);
7563 if (!config->save_connector_encoders)
7566 /* Copy data. Note that driver private data is not affected.
7567 * Should anything bad happen only the expected state is
7568 * restored, not the drivers personal bookkeeping.
7571 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
7572 config->save_encoder_crtcs[count++] = encoder->crtc;
7576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7577 config->save_connector_encoders[count++] = connector->encoder;
7583 static void intel_set_config_restore_state(struct drm_device *dev,
7584 struct intel_set_config *config)
7586 struct intel_encoder *encoder;
7587 struct intel_connector *connector;
7591 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7593 to_intel_crtc(config->save_encoder_crtcs[count++]);
7597 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7598 connector->new_encoder =
7599 to_intel_encoder(config->save_connector_encoders[count++]);
7604 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7605 struct intel_set_config *config)
7608 /* We should be able to check here if the fb has the same properties
7609 * and then just flip_or_move it */
7610 if (set->crtc->fb != set->fb) {
7611 /* If we have no fb then treat it as a full mode set */
7612 if (set->crtc->fb == NULL) {
7613 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7614 config->mode_changed = true;
7615 } else if (set->fb == NULL) {
7616 config->mode_changed = true;
7617 } else if (set->fb->depth != set->crtc->fb->depth) {
7618 config->mode_changed = true;
7619 } else if (set->fb->bits_per_pixel !=
7620 set->crtc->fb->bits_per_pixel) {
7621 config->mode_changed = true;
7623 config->fb_changed = true;
7626 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
7627 config->fb_changed = true;
7629 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7630 DRM_DEBUG_KMS("modes are different, full mode set\n");
7631 drm_mode_debug_printmodeline(&set->crtc->mode);
7632 drm_mode_debug_printmodeline(set->mode);
7633 config->mode_changed = true;
7638 intel_modeset_stage_output_state(struct drm_device *dev,
7639 struct drm_mode_set *set,
7640 struct intel_set_config *config)
7642 struct drm_crtc *new_crtc;
7643 struct intel_connector *connector;
7644 struct intel_encoder *encoder;
7647 /* The upper layers ensure that we either disabl a crtc or have a list
7648 * of connectors. For paranoia, double-check this. */
7649 WARN_ON(!set->fb && (set->num_connectors != 0));
7650 WARN_ON(set->fb && (set->num_connectors == 0));
7653 list_for_each_entry(connector, &dev->mode_config.connector_list,
7655 /* Otherwise traverse passed in connector list and get encoders
7657 for (ro = 0; ro < set->num_connectors; ro++) {
7658 if (set->connectors[ro] == &connector->base) {
7659 connector->new_encoder = connector->encoder;
7664 /* If we disable the crtc, disable all its connectors. Also, if
7665 * the connector is on the changing crtc but not on the new
7666 * connector list, disable it. */
7667 if ((!set->fb || ro == set->num_connectors) &&
7668 connector->base.encoder &&
7669 connector->base.encoder->crtc == set->crtc) {
7670 connector->new_encoder = NULL;
7672 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7673 connector->base.base.id,
7674 drm_get_connector_name(&connector->base));
7678 if (&connector->new_encoder->base != connector->base.encoder) {
7679 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
7680 config->mode_changed = true;
7683 /* Disable all disconnected encoders. */
7684 if (connector->base.status == connector_status_disconnected)
7685 connector->new_encoder = NULL;
7687 /* connector->new_encoder is now updated for all connectors. */
7689 /* Update crtc of enabled connectors. */
7691 list_for_each_entry(connector, &dev->mode_config.connector_list,
7693 if (!connector->new_encoder)
7696 new_crtc = connector->new_encoder->base.crtc;
7698 for (ro = 0; ro < set->num_connectors; ro++) {
7699 if (set->connectors[ro] == &connector->base)
7700 new_crtc = set->crtc;
7703 /* Make sure the new CRTC will work with the encoder */
7704 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7708 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7710 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7711 connector->base.base.id,
7712 drm_get_connector_name(&connector->base),
7716 /* Check for any encoders that needs to be disabled. */
7717 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7719 list_for_each_entry(connector,
7720 &dev->mode_config.connector_list,
7722 if (connector->new_encoder == encoder) {
7723 WARN_ON(!connector->new_encoder->new_crtc);
7728 encoder->new_crtc = NULL;
7730 /* Only now check for crtc changes so we don't miss encoders
7731 * that will be disabled. */
7732 if (&encoder->new_crtc->base != encoder->base.crtc) {
7733 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
7734 config->mode_changed = true;
7737 /* Now we've also updated encoder->new_crtc for all encoders. */
7742 static int intel_crtc_set_config(struct drm_mode_set *set)
7744 struct drm_device *dev;
7745 struct drm_mode_set save_set;
7746 struct intel_set_config *config;
7751 BUG_ON(!set->crtc->helper_private);
7756 /* The fb helper likes to play gross jokes with ->mode_set_config.
7757 * Unfortunately the crtc helper doesn't do much at all for this case,
7758 * so we have to cope with this madness until the fb helper is fixed up. */
7759 if (set->fb && set->num_connectors == 0)
7763 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7764 set->crtc->base.id, set->fb->base.id,
7765 (int)set->num_connectors, set->x, set->y);
7767 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
7770 dev = set->crtc->dev;
7773 config = kzalloc(sizeof(*config), GFP_KERNEL);
7777 ret = intel_set_config_save_state(dev, config);
7781 save_set.crtc = set->crtc;
7782 save_set.mode = &set->crtc->mode;
7783 save_set.x = set->crtc->x;
7784 save_set.y = set->crtc->y;
7785 save_set.fb = set->crtc->fb;
7787 /* Compute whether we need a full modeset, only an fb base update or no
7788 * change at all. In the future we might also check whether only the
7789 * mode changed, e.g. for LVDS where we only change the panel fitter in
7791 intel_set_config_compute_mode_changes(set, config);
7793 ret = intel_modeset_stage_output_state(dev, set, config);
7797 if (config->mode_changed) {
7799 DRM_DEBUG_KMS("attempting to set mode from"
7801 drm_mode_debug_printmodeline(set->mode);
7804 ret = intel_set_mode(set->crtc, set->mode,
7805 set->x, set->y, set->fb);
7807 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
7808 set->crtc->base.id, ret);
7811 } else if (config->fb_changed) {
7812 ret = intel_pipe_set_base(set->crtc,
7813 set->x, set->y, set->fb);
7816 intel_set_config_free(config);
7821 intel_set_config_restore_state(dev, config);
7823 /* Try to restore the config */
7824 if (config->mode_changed &&
7825 intel_set_mode(save_set.crtc, save_set.mode,
7826 save_set.x, save_set.y, save_set.fb))
7827 DRM_ERROR("failed to restore config after modeset failure\n");
7830 intel_set_config_free(config);
7834 static const struct drm_crtc_funcs intel_crtc_funcs = {
7835 .cursor_set = intel_crtc_cursor_set,
7836 .cursor_move = intel_crtc_cursor_move,
7837 .gamma_set = intel_crtc_gamma_set,
7838 .set_config = intel_crtc_set_config,
7839 .destroy = intel_crtc_destroy,
7840 .page_flip = intel_crtc_page_flip,
7843 static void intel_cpu_pll_init(struct drm_device *dev)
7846 intel_ddi_pll_init(dev);
7849 static void intel_pch_pll_init(struct drm_device *dev)
7851 drm_i915_private_t *dev_priv = dev->dev_private;
7854 if (dev_priv->num_pch_pll == 0) {
7855 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7859 for (i = 0; i < dev_priv->num_pch_pll; i++) {
7860 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7861 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7862 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7866 static void intel_crtc_init(struct drm_device *dev, int pipe)
7868 drm_i915_private_t *dev_priv = dev->dev_private;
7869 struct intel_crtc *intel_crtc;
7872 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7873 if (intel_crtc == NULL)
7876 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7878 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
7879 for (i = 0; i < 256; i++) {
7880 intel_crtc->lut_r[i] = i;
7881 intel_crtc->lut_g[i] = i;
7882 intel_crtc->lut_b[i] = i;
7885 /* Swap pipes & planes for FBC on pre-965 */
7886 intel_crtc->pipe = pipe;
7887 intel_crtc->plane = pipe;
7888 intel_crtc->cpu_transcoder = pipe;
7889 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
7890 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
7891 intel_crtc->plane = !pipe;
7894 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7895 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7896 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7897 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7899 intel_crtc->bpp = 24; /* default for pre-Ironlake */
7901 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
7904 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
7905 struct drm_file *file)
7907 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7908 struct drm_mode_object *drmmode_obj;
7909 struct intel_crtc *crtc;
7911 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7914 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7915 DRM_MODE_OBJECT_CRTC);
7918 DRM_ERROR("no such CRTC id\n");
7922 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7923 pipe_from_crtc_id->pipe = crtc->pipe;
7928 static int intel_encoder_clones(struct intel_encoder *encoder)
7930 struct drm_device *dev = encoder->base.dev;
7931 struct intel_encoder *source_encoder;
7935 list_for_each_entry(source_encoder,
7936 &dev->mode_config.encoder_list, base.head) {
7938 if (encoder == source_encoder)
7939 index_mask |= (1 << entry);
7941 /* Intel hw has only one MUX where enocoders could be cloned. */
7942 if (encoder->cloneable && source_encoder->cloneable)
7943 index_mask |= (1 << entry);
7951 static bool has_edp_a(struct drm_device *dev)
7953 struct drm_i915_private *dev_priv = dev->dev_private;
7955 if (!IS_MOBILE(dev))
7958 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7962 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7968 static void intel_setup_outputs(struct drm_device *dev)
7970 struct drm_i915_private *dev_priv = dev->dev_private;
7971 struct intel_encoder *encoder;
7972 bool dpd_is_edp = false;
7975 has_lvds = intel_lvds_init(dev);
7976 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7977 /* disable the panel fitter on everything but LVDS */
7978 I915_WRITE(PFIT_CONTROL, 0);
7981 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
7982 intel_crt_init(dev);
7987 /* Haswell uses DDI functions to detect digital outputs */
7988 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7989 /* DDI A only supports eDP */
7991 intel_ddi_init(dev, PORT_A);
7993 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
7995 found = I915_READ(SFUSE_STRAP);
7997 if (found & SFUSE_STRAP_DDIB_DETECTED)
7998 intel_ddi_init(dev, PORT_B);
7999 if (found & SFUSE_STRAP_DDIC_DETECTED)
8000 intel_ddi_init(dev, PORT_C);
8001 if (found & SFUSE_STRAP_DDID_DETECTED)
8002 intel_ddi_init(dev, PORT_D);
8003 } else if (HAS_PCH_SPLIT(dev)) {
8005 dpd_is_edp = intel_dpd_is_edp(dev);
8008 intel_dp_init(dev, DP_A, PORT_A);
8010 if (I915_READ(HDMIB) & PORT_DETECTED) {
8011 /* PCH SDVOB multiplex with HDMIB */
8012 found = intel_sdvo_init(dev, PCH_SDVOB, true);
8014 intel_hdmi_init(dev, HDMIB, PORT_B);
8015 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
8016 intel_dp_init(dev, PCH_DP_B, PORT_B);
8019 if (I915_READ(HDMIC) & PORT_DETECTED)
8020 intel_hdmi_init(dev, HDMIC, PORT_C);
8022 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
8023 intel_hdmi_init(dev, HDMID, PORT_D);
8025 if (I915_READ(PCH_DP_C) & DP_DETECTED)
8026 intel_dp_init(dev, PCH_DP_C, PORT_C);
8028 if (I915_READ(PCH_DP_D) & DP_DETECTED)
8029 intel_dp_init(dev, PCH_DP_D, PORT_D);
8030 } else if (IS_VALLEYVIEW(dev)) {
8033 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8034 if (I915_READ(DP_C) & DP_DETECTED)
8035 intel_dp_init(dev, DP_C, PORT_C);
8037 if (I915_READ(SDVOB) & PORT_DETECTED) {
8038 /* SDVOB multiplex with HDMIB */
8039 found = intel_sdvo_init(dev, SDVOB, true);
8041 intel_hdmi_init(dev, SDVOB, PORT_B);
8042 if (!found && (I915_READ(DP_B) & DP_DETECTED))
8043 intel_dp_init(dev, DP_B, PORT_B);
8046 if (I915_READ(SDVOC) & PORT_DETECTED)
8047 intel_hdmi_init(dev, SDVOC, PORT_C);
8049 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
8052 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8053 DRM_DEBUG_KMS("probing SDVOB\n");
8054 found = intel_sdvo_init(dev, SDVOB, true);
8055 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8056 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
8057 intel_hdmi_init(dev, SDVOB, PORT_B);
8060 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8061 DRM_DEBUG_KMS("probing DP_B\n");
8062 intel_dp_init(dev, DP_B, PORT_B);
8066 /* Before G4X SDVOC doesn't have its own detect register */
8068 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8069 DRM_DEBUG_KMS("probing SDVOC\n");
8070 found = intel_sdvo_init(dev, SDVOC, false);
8073 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8075 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8076 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
8077 intel_hdmi_init(dev, SDVOC, PORT_C);
8079 if (SUPPORTS_INTEGRATED_DP(dev)) {
8080 DRM_DEBUG_KMS("probing DP_C\n");
8081 intel_dp_init(dev, DP_C, PORT_C);
8085 if (SUPPORTS_INTEGRATED_DP(dev) &&
8086 (I915_READ(DP_D) & DP_DETECTED)) {
8087 DRM_DEBUG_KMS("probing DP_D\n");
8088 intel_dp_init(dev, DP_D, PORT_D);
8090 } else if (IS_GEN2(dev))
8091 intel_dvo_init(dev);
8093 if (SUPPORTS_TV(dev))
8096 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8097 encoder->base.possible_crtcs = encoder->crtc_mask;
8098 encoder->base.possible_clones =
8099 intel_encoder_clones(encoder);
8102 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8103 ironlake_init_pch_refclk(dev);
8105 drm_helper_move_panel_connectors_to_head(dev);
8108 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8110 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8112 drm_framebuffer_cleanup(fb);
8113 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
8118 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
8119 struct drm_file *file,
8120 unsigned int *handle)
8122 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
8123 struct drm_i915_gem_object *obj = intel_fb->obj;
8125 return drm_gem_handle_create(file, &obj->base, handle);
8128 static const struct drm_framebuffer_funcs intel_fb_funcs = {
8129 .destroy = intel_user_framebuffer_destroy,
8130 .create_handle = intel_user_framebuffer_create_handle,
8133 int intel_framebuffer_init(struct drm_device *dev,
8134 struct intel_framebuffer *intel_fb,
8135 struct drm_mode_fb_cmd2 *mode_cmd,
8136 struct drm_i915_gem_object *obj)
8140 if (obj->tiling_mode == I915_TILING_Y)
8143 if (mode_cmd->pitches[0] & 63)
8146 /* FIXME <= Gen4 stride limits are bit unclear */
8147 if (mode_cmd->pitches[0] > 32768)
8150 if (obj->tiling_mode != I915_TILING_NONE &&
8151 mode_cmd->pitches[0] != obj->stride)
8154 /* Reject formats not supported by any plane early. */
8155 switch (mode_cmd->pixel_format) {
8157 case DRM_FORMAT_RGB565:
8158 case DRM_FORMAT_XRGB8888:
8159 case DRM_FORMAT_ARGB8888:
8161 case DRM_FORMAT_XRGB1555:
8162 case DRM_FORMAT_ARGB1555:
8163 if (INTEL_INFO(dev)->gen > 3)
8166 case DRM_FORMAT_XBGR8888:
8167 case DRM_FORMAT_ABGR8888:
8168 case DRM_FORMAT_XRGB2101010:
8169 case DRM_FORMAT_ARGB2101010:
8170 case DRM_FORMAT_XBGR2101010:
8171 case DRM_FORMAT_ABGR2101010:
8172 if (INTEL_INFO(dev)->gen < 4)
8175 case DRM_FORMAT_YUYV:
8176 case DRM_FORMAT_UYVY:
8177 case DRM_FORMAT_YVYU:
8178 case DRM_FORMAT_VYUY:
8179 if (INTEL_INFO(dev)->gen < 6)
8183 DRM_DEBUG_KMS("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
8187 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8188 if (mode_cmd->offsets[0] != 0)
8191 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8193 DRM_ERROR("framebuffer init failed %d\n", ret);
8197 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8198 intel_fb->obj = obj;
8202 static struct drm_framebuffer *
8203 intel_user_framebuffer_create(struct drm_device *dev,
8204 struct drm_file *filp,
8205 struct drm_mode_fb_cmd2 *mode_cmd)
8207 struct drm_i915_gem_object *obj;
8209 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8210 mode_cmd->handles[0]));
8211 if (&obj->base == NULL)
8212 return ERR_PTR(-ENOENT);
8214 return intel_framebuffer_create(dev, mode_cmd, obj);
8217 static const struct drm_mode_config_funcs intel_mode_funcs = {
8218 .fb_create = intel_user_framebuffer_create,
8219 .output_poll_changed = intel_fb_output_poll_changed,
8222 /* Set up chip specific display functions */
8223 static void intel_init_display(struct drm_device *dev)
8225 struct drm_i915_private *dev_priv = dev->dev_private;
8227 /* We always want a DPMS function */
8229 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
8230 dev_priv->display.crtc_enable = haswell_crtc_enable;
8231 dev_priv->display.crtc_disable = haswell_crtc_disable;
8232 dev_priv->display.off = haswell_crtc_off;
8233 dev_priv->display.update_plane = ironlake_update_plane;
8234 } else if (HAS_PCH_SPLIT(dev)) {
8235 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
8236 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8237 dev_priv->display.crtc_disable = ironlake_crtc_disable;
8238 dev_priv->display.off = ironlake_crtc_off;
8239 dev_priv->display.update_plane = ironlake_update_plane;
8241 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
8242 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8243 dev_priv->display.crtc_disable = i9xx_crtc_disable;
8244 dev_priv->display.off = i9xx_crtc_off;
8245 dev_priv->display.update_plane = i9xx_update_plane;
8248 /* Returns the core display clock speed */
8249 if (IS_VALLEYVIEW(dev))
8250 dev_priv->display.get_display_clock_speed =
8251 valleyview_get_display_clock_speed;
8252 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
8253 dev_priv->display.get_display_clock_speed =
8254 i945_get_display_clock_speed;
8255 else if (IS_I915G(dev))
8256 dev_priv->display.get_display_clock_speed =
8257 i915_get_display_clock_speed;
8258 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
8259 dev_priv->display.get_display_clock_speed =
8260 i9xx_misc_get_display_clock_speed;
8261 else if (IS_I915GM(dev))
8262 dev_priv->display.get_display_clock_speed =
8263 i915gm_get_display_clock_speed;
8264 else if (IS_I865G(dev))
8265 dev_priv->display.get_display_clock_speed =
8266 i865_get_display_clock_speed;
8267 else if (IS_I85X(dev))
8268 dev_priv->display.get_display_clock_speed =
8269 i855_get_display_clock_speed;
8271 dev_priv->display.get_display_clock_speed =
8272 i830_get_display_clock_speed;
8274 if (HAS_PCH_SPLIT(dev)) {
8276 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
8277 dev_priv->display.write_eld = ironlake_write_eld;
8278 } else if (IS_GEN6(dev)) {
8279 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
8280 dev_priv->display.write_eld = ironlake_write_eld;
8281 } else if (IS_IVYBRIDGE(dev)) {
8282 /* FIXME: detect B0+ stepping and use auto training */
8283 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
8284 dev_priv->display.write_eld = ironlake_write_eld;
8285 dev_priv->display.modeset_global_resources =
8286 ivb_modeset_global_resources;
8287 } else if (IS_HASWELL(dev)) {
8288 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
8289 dev_priv->display.write_eld = haswell_write_eld;
8291 } else if (IS_G4X(dev)) {
8292 dev_priv->display.write_eld = g4x_write_eld;
8295 /* Default just returns -ENODEV to indicate unsupported */
8296 dev_priv->display.queue_flip = intel_default_queue_flip;
8298 switch (INTEL_INFO(dev)->gen) {
8300 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8304 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8309 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8313 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8316 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8322 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8323 * resume, or other times. This quirk makes sure that's the case for
8326 static void quirk_pipea_force(struct drm_device *dev)
8328 struct drm_i915_private *dev_priv = dev->dev_private;
8330 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
8331 DRM_INFO("applying pipe a force quirk\n");
8335 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8337 static void quirk_ssc_force_disable(struct drm_device *dev)
8339 struct drm_i915_private *dev_priv = dev->dev_private;
8340 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
8341 DRM_INFO("applying lvds SSC disable quirk\n");
8345 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8348 static void quirk_invert_brightness(struct drm_device *dev)
8350 struct drm_i915_private *dev_priv = dev->dev_private;
8351 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
8352 DRM_INFO("applying inverted panel brightness quirk\n");
8355 struct intel_quirk {
8357 int subsystem_vendor;
8358 int subsystem_device;
8359 void (*hook)(struct drm_device *dev);
8362 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8363 struct intel_dmi_quirk {
8364 void (*hook)(struct drm_device *dev);
8365 const struct dmi_system_id (*dmi_id_list)[];
8368 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8370 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8374 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8376 .dmi_id_list = &(const struct dmi_system_id[]) {
8378 .callback = intel_dmi_reverse_brightness,
8379 .ident = "NCR Corporation",
8380 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8381 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8384 { } /* terminating entry */
8386 .hook = quirk_invert_brightness,
8390 static struct intel_quirk intel_quirks[] = {
8391 /* HP Mini needs pipe A force quirk (LP: #322104) */
8392 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
8394 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8395 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8397 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8398 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8400 /* 830/845 need to leave pipe A & dpll A up */
8401 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
8402 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
8404 /* Lenovo U160 cannot use SSC on LVDS */
8405 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
8407 /* Sony Vaio Y cannot use SSC on LVDS */
8408 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
8410 /* Acer Aspire 5734Z must invert backlight brightness */
8411 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
8414 static void intel_init_quirks(struct drm_device *dev)
8416 struct pci_dev *d = dev->pdev;
8419 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8420 struct intel_quirk *q = &intel_quirks[i];
8422 if (d->device == q->device &&
8423 (d->subsystem_vendor == q->subsystem_vendor ||
8424 q->subsystem_vendor == PCI_ANY_ID) &&
8425 (d->subsystem_device == q->subsystem_device ||
8426 q->subsystem_device == PCI_ANY_ID))
8429 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8430 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8431 intel_dmi_quirks[i].hook(dev);
8435 /* Disable the VGA plane that we never use */
8436 static void i915_disable_vga(struct drm_device *dev)
8438 struct drm_i915_private *dev_priv = dev->dev_private;
8442 if (HAS_PCH_SPLIT(dev))
8443 vga_reg = CPU_VGACNTRL;
8447 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
8448 outb(SR01, VGA_SR_INDEX);
8449 sr1 = inb(VGA_SR_DATA);
8450 outb(sr1 | 1<<5, VGA_SR_DATA);
8451 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8454 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8455 POSTING_READ(vga_reg);
8458 void intel_modeset_init_hw(struct drm_device *dev)
8460 /* We attempt to init the necessary power wells early in the initialization
8461 * time, so the subsystems that expect power to be enabled can work.
8463 intel_init_power_wells(dev);
8465 intel_prepare_ddi(dev);
8467 intel_init_clock_gating(dev);
8469 mutex_lock(&dev->struct_mutex);
8470 intel_enable_gt_powersave(dev);
8471 mutex_unlock(&dev->struct_mutex);
8474 void intel_modeset_init(struct drm_device *dev)
8476 struct drm_i915_private *dev_priv = dev->dev_private;
8479 drm_mode_config_init(dev);
8481 dev->mode_config.min_width = 0;
8482 dev->mode_config.min_height = 0;
8484 dev->mode_config.preferred_depth = 24;
8485 dev->mode_config.prefer_shadow = 1;
8487 dev->mode_config.funcs = &intel_mode_funcs;
8489 intel_init_quirks(dev);
8493 intel_init_display(dev);
8496 dev->mode_config.max_width = 2048;
8497 dev->mode_config.max_height = 2048;
8498 } else if (IS_GEN3(dev)) {
8499 dev->mode_config.max_width = 4096;
8500 dev->mode_config.max_height = 4096;
8502 dev->mode_config.max_width = 8192;
8503 dev->mode_config.max_height = 8192;
8505 dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
8507 DRM_DEBUG_KMS("%d display pipe%s available.\n",
8508 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
8510 for (i = 0; i < dev_priv->num_pipe; i++) {
8511 intel_crtc_init(dev, i);
8512 ret = intel_plane_init(dev, i);
8514 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
8517 intel_cpu_pll_init(dev);
8518 intel_pch_pll_init(dev);
8520 /* Just disable it once at startup */
8521 i915_disable_vga(dev);
8522 intel_setup_outputs(dev);
8524 /* Just in case the BIOS is doing something questionable. */
8525 intel_disable_fbc(dev);
8529 intel_connector_break_all_links(struct intel_connector *connector)
8531 connector->base.dpms = DRM_MODE_DPMS_OFF;
8532 connector->base.encoder = NULL;
8533 connector->encoder->connectors_active = false;
8534 connector->encoder->base.crtc = NULL;
8537 static void intel_enable_pipe_a(struct drm_device *dev)
8539 struct intel_connector *connector;
8540 struct drm_connector *crt = NULL;
8541 struct intel_load_detect_pipe load_detect_temp;
8543 /* We can't just switch on the pipe A, we need to set things up with a
8544 * proper mode and output configuration. As a gross hack, enable pipe A
8545 * by enabling the load detect pipe once. */
8546 list_for_each_entry(connector,
8547 &dev->mode_config.connector_list,
8549 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8550 crt = &connector->base;
8558 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8559 intel_release_load_detect_pipe(crt, &load_detect_temp);
8565 intel_check_plane_mapping(struct intel_crtc *crtc)
8567 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8570 if (dev_priv->num_pipe == 1)
8573 reg = DSPCNTR(!crtc->plane);
8574 val = I915_READ(reg);
8576 if ((val & DISPLAY_PLANE_ENABLE) &&
8577 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8583 static void intel_sanitize_crtc(struct intel_crtc *crtc)
8585 struct drm_device *dev = crtc->base.dev;
8586 struct drm_i915_private *dev_priv = dev->dev_private;
8589 /* Clear any frame start delays used for debugging left by the BIOS */
8590 reg = PIPECONF(crtc->cpu_transcoder);
8591 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8593 /* We need to sanitize the plane -> pipe mapping first because this will
8594 * disable the crtc (and hence change the state) if it is wrong. Note
8595 * that gen4+ has a fixed plane -> pipe mapping. */
8596 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
8597 struct intel_connector *connector;
8600 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8601 crtc->base.base.id);
8603 /* Pipe has the wrong plane attached and the plane is active.
8604 * Temporarily change the plane mapping and disable everything
8606 plane = crtc->plane;
8607 crtc->plane = !plane;
8608 dev_priv->display.crtc_disable(&crtc->base);
8609 crtc->plane = plane;
8611 /* ... and break all links. */
8612 list_for_each_entry(connector, &dev->mode_config.connector_list,
8614 if (connector->encoder->base.crtc != &crtc->base)
8617 intel_connector_break_all_links(connector);
8620 WARN_ON(crtc->active);
8621 crtc->base.enabled = false;
8624 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8625 crtc->pipe == PIPE_A && !crtc->active) {
8626 /* BIOS forgot to enable pipe A, this mostly happens after
8627 * resume. Force-enable the pipe to fix this, the update_dpms
8628 * call below we restore the pipe to the right state, but leave
8629 * the required bits on. */
8630 intel_enable_pipe_a(dev);
8633 /* Adjust the state of the output pipe according to whether we
8634 * have active connectors/encoders. */
8635 intel_crtc_update_dpms(&crtc->base);
8637 if (crtc->active != crtc->base.enabled) {
8638 struct intel_encoder *encoder;
8640 /* This can happen either due to bugs in the get_hw_state
8641 * functions or because the pipe is force-enabled due to the
8643 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8645 crtc->base.enabled ? "enabled" : "disabled",
8646 crtc->active ? "enabled" : "disabled");
8648 crtc->base.enabled = crtc->active;
8650 /* Because we only establish the connector -> encoder ->
8651 * crtc links if something is active, this means the
8652 * crtc is now deactivated. Break the links. connector
8653 * -> encoder links are only establish when things are
8654 * actually up, hence no need to break them. */
8655 WARN_ON(crtc->active);
8657 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8658 WARN_ON(encoder->connectors_active);
8659 encoder->base.crtc = NULL;
8664 static void intel_sanitize_encoder(struct intel_encoder *encoder)
8666 struct intel_connector *connector;
8667 struct drm_device *dev = encoder->base.dev;
8669 /* We need to check both for a crtc link (meaning that the
8670 * encoder is active and trying to read from a pipe) and the
8671 * pipe itself being active. */
8672 bool has_active_crtc = encoder->base.crtc &&
8673 to_intel_crtc(encoder->base.crtc)->active;
8675 if (encoder->connectors_active && !has_active_crtc) {
8676 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8677 encoder->base.base.id,
8678 drm_get_encoder_name(&encoder->base));
8680 /* Connector is active, but has no active pipe. This is
8681 * fallout from our resume register restoring. Disable
8682 * the encoder manually again. */
8683 if (encoder->base.crtc) {
8684 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8685 encoder->base.base.id,
8686 drm_get_encoder_name(&encoder->base));
8687 encoder->disable(encoder);
8690 /* Inconsistent output/port/pipe state happens presumably due to
8691 * a bug in one of the get_hw_state functions. Or someplace else
8692 * in our code, like the register restore mess on resume. Clamp
8693 * things to off as a safer default. */
8694 list_for_each_entry(connector,
8695 &dev->mode_config.connector_list,
8697 if (connector->encoder != encoder)
8700 intel_connector_break_all_links(connector);
8703 /* Enabled encoders without active connectors will be fixed in
8704 * the crtc fixup. */
8707 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8708 * and i915 state tracking structures. */
8709 void intel_modeset_setup_hw_state(struct drm_device *dev,
8712 struct drm_i915_private *dev_priv = dev->dev_private;
8715 struct intel_crtc *crtc;
8716 struct intel_encoder *encoder;
8717 struct intel_connector *connector;
8720 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8722 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8723 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8724 case TRANS_DDI_EDP_INPUT_A_ON:
8725 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8728 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8731 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8736 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8737 crtc->cpu_transcoder = TRANSCODER_EDP;
8739 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
8744 for_each_pipe(pipe) {
8745 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8747 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
8748 if (tmp & PIPECONF_ENABLE)
8749 crtc->active = true;
8751 crtc->active = false;
8753 crtc->base.enabled = crtc->active;
8755 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8757 crtc->active ? "enabled" : "disabled");
8761 intel_ddi_setup_hw_pll_state(dev);
8763 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8767 if (encoder->get_hw_state(encoder, &pipe)) {
8768 encoder->base.crtc =
8769 dev_priv->pipe_to_crtc_mapping[pipe];
8771 encoder->base.crtc = NULL;
8774 encoder->connectors_active = false;
8775 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8776 encoder->base.base.id,
8777 drm_get_encoder_name(&encoder->base),
8778 encoder->base.crtc ? "enabled" : "disabled",
8782 list_for_each_entry(connector, &dev->mode_config.connector_list,
8784 if (connector->get_hw_state(connector)) {
8785 connector->base.dpms = DRM_MODE_DPMS_ON;
8786 connector->encoder->connectors_active = true;
8787 connector->base.encoder = &connector->encoder->base;
8789 connector->base.dpms = DRM_MODE_DPMS_OFF;
8790 connector->base.encoder = NULL;
8792 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8793 connector->base.base.id,
8794 drm_get_connector_name(&connector->base),
8795 connector->base.encoder ? "enabled" : "disabled");
8798 /* HW state is read out, now we need to sanitize this mess. */
8799 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8801 intel_sanitize_encoder(encoder);
8804 for_each_pipe(pipe) {
8805 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8806 intel_sanitize_crtc(crtc);
8809 if (force_restore) {
8811 intel_crtc_restore_mode(dev_priv->pipe_to_crtc_mapping[pipe]);
8813 intel_modeset_update_staged_output_state(dev);
8816 intel_modeset_check_state(dev);
8818 drm_mode_config_reset(dev);
8821 void intel_modeset_gem_init(struct drm_device *dev)
8823 intel_modeset_init_hw(dev);
8825 intel_setup_overlay(dev);
8827 intel_modeset_setup_hw_state(dev, false);
8830 void intel_modeset_cleanup(struct drm_device *dev)
8832 struct drm_i915_private *dev_priv = dev->dev_private;
8833 struct drm_crtc *crtc;
8834 struct intel_crtc *intel_crtc;
8836 drm_kms_helper_poll_fini(dev);
8837 mutex_lock(&dev->struct_mutex);
8839 intel_unregister_dsm_handler();
8842 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8843 /* Skip inactive CRTCs */
8847 intel_crtc = to_intel_crtc(crtc);
8848 intel_increase_pllclock(crtc);
8851 intel_disable_fbc(dev);
8853 intel_disable_gt_powersave(dev);
8855 ironlake_teardown_rc6(dev);
8857 if (IS_VALLEYVIEW(dev))
8860 mutex_unlock(&dev->struct_mutex);
8862 /* Disable the irq before mode object teardown, for the irq might
8863 * enqueue unpin/hotplug work. */
8864 drm_irq_uninstall(dev);
8865 cancel_work_sync(&dev_priv->hotplug_work);
8866 cancel_work_sync(&dev_priv->rps.work);
8868 /* flush any delayed tasks or pending work */
8869 flush_scheduled_work();
8871 drm_mode_config_cleanup(dev);
8873 intel_cleanup_overlay(dev);
8877 * Return which encoder is currently attached for connector.
8879 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
8881 return &intel_attached_encoder(connector)->base;
8884 void intel_connector_attach_encoder(struct intel_connector *connector,
8885 struct intel_encoder *encoder)
8887 connector->encoder = encoder;
8888 drm_mode_connector_attach_encoder(&connector->base,
8893 * set vga decode state - true == enable VGA decode
8895 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8897 struct drm_i915_private *dev_priv = dev->dev_private;
8900 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8902 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8904 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8905 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8909 #ifdef CONFIG_DEBUG_FS
8910 #include <linux/seq_file.h>
8912 struct intel_display_error_state {
8913 struct intel_cursor_error_state {
8918 } cursor[I915_MAX_PIPES];
8920 struct intel_pipe_error_state {
8930 } pipe[I915_MAX_PIPES];
8932 struct intel_plane_error_state {
8940 } plane[I915_MAX_PIPES];
8943 struct intel_display_error_state *
8944 intel_display_capture_error_state(struct drm_device *dev)
8946 drm_i915_private_t *dev_priv = dev->dev_private;
8947 struct intel_display_error_state *error;
8948 enum transcoder cpu_transcoder;
8951 error = kmalloc(sizeof(*error), GFP_ATOMIC);
8956 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
8958 error->cursor[i].control = I915_READ(CURCNTR(i));
8959 error->cursor[i].position = I915_READ(CURPOS(i));
8960 error->cursor[i].base = I915_READ(CURBASE(i));
8962 error->plane[i].control = I915_READ(DSPCNTR(i));
8963 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8964 error->plane[i].size = I915_READ(DSPSIZE(i));
8965 error->plane[i].pos = I915_READ(DSPPOS(i));
8966 error->plane[i].addr = I915_READ(DSPADDR(i));
8967 if (INTEL_INFO(dev)->gen >= 4) {
8968 error->plane[i].surface = I915_READ(DSPSURF(i));
8969 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8972 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
8973 error->pipe[i].source = I915_READ(PIPESRC(i));
8974 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
8975 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
8976 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
8977 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
8978 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
8979 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
8986 intel_display_print_error_state(struct seq_file *m,
8987 struct drm_device *dev,
8988 struct intel_display_error_state *error)
8990 drm_i915_private_t *dev_priv = dev->dev_private;
8993 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8995 seq_printf(m, "Pipe [%d]:\n", i);
8996 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
8997 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
8998 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
8999 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9000 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9001 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9002 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9003 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9005 seq_printf(m, "Plane [%d]:\n", i);
9006 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9007 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9008 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9009 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9010 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9011 if (INTEL_INFO(dev)->gen >= 4) {
9012 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9013 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9016 seq_printf(m, "Cursor [%d]:\n", i);
9017 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9018 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9019 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);