2 * Copyright © 2006-2007 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include <drm/drm_edid.h>
33 #include "intel_drv.h"
34 #include <drm/i915_drm.h>
36 #include "i915_trace.h"
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_plane_helper.h>
40 #include <drm/drm_rect.h>
42 /* Primary plane formats supported by all gen */
43 #define COMMON_PRIMARY_FORMATS \
46 DRM_FORMAT_XRGB8888, \
49 /* Primary plane formats for gen <= 3 */
50 static const uint32_t intel_primary_formats_gen2[] = {
51 COMMON_PRIMARY_FORMATS,
56 /* Primary plane formats for gen >= 4 */
57 static const uint32_t intel_primary_formats_gen4[] = {
58 COMMON_PRIMARY_FORMATS, \
61 DRM_FORMAT_XRGB2101010,
62 DRM_FORMAT_ARGB2101010,
63 DRM_FORMAT_XBGR2101010,
64 DRM_FORMAT_ABGR2101010,
68 static const uint32_t intel_cursor_formats[] = {
72 static void intel_increase_pllclock(struct drm_device *dev,
74 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
76 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
77 struct intel_crtc_config *pipe_config);
78 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
79 struct intel_crtc_config *pipe_config);
81 static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
82 int x, int y, struct drm_framebuffer *old_fb);
83 static int intel_framebuffer_init(struct drm_device *dev,
84 struct intel_framebuffer *ifb,
85 struct drm_mode_fb_cmd2 *mode_cmd,
86 struct drm_i915_gem_object *obj);
87 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
88 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
89 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
90 struct intel_link_m_n *m_n,
91 struct intel_link_m_n *m2_n2);
92 static void ironlake_set_pipeconf(struct drm_crtc *crtc);
93 static void haswell_set_pipeconf(struct drm_crtc *crtc);
94 static void intel_set_pipe_csc(struct drm_crtc *crtc);
95 static void vlv_prepare_pll(struct intel_crtc *crtc);
96 static void chv_prepare_pll(struct intel_crtc *crtc);
104 int p2_slow, p2_fast;
107 typedef struct intel_limit intel_limit_t;
109 intel_range_t dot, vco, n, m, m1, m2, p, p1;
114 intel_pch_rawclk(struct drm_device *dev)
116 struct drm_i915_private *dev_priv = dev->dev_private;
118 WARN_ON(!HAS_PCH_SPLIT(dev));
120 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
123 static inline u32 /* units of 100MHz */
124 intel_fdi_link_freq(struct drm_device *dev)
127 struct drm_i915_private *dev_priv = dev->dev_private;
128 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
133 static const intel_limit_t intel_limits_i8xx_dac = {
134 .dot = { .min = 25000, .max = 350000 },
135 .vco = { .min = 908000, .max = 1512000 },
136 .n = { .min = 2, .max = 16 },
137 .m = { .min = 96, .max = 140 },
138 .m1 = { .min = 18, .max = 26 },
139 .m2 = { .min = 6, .max = 16 },
140 .p = { .min = 4, .max = 128 },
141 .p1 = { .min = 2, .max = 33 },
142 .p2 = { .dot_limit = 165000,
143 .p2_slow = 4, .p2_fast = 2 },
146 static const intel_limit_t intel_limits_i8xx_dvo = {
147 .dot = { .min = 25000, .max = 350000 },
148 .vco = { .min = 908000, .max = 1512000 },
149 .n = { .min = 2, .max = 16 },
150 .m = { .min = 96, .max = 140 },
151 .m1 = { .min = 18, .max = 26 },
152 .m2 = { .min = 6, .max = 16 },
153 .p = { .min = 4, .max = 128 },
154 .p1 = { .min = 2, .max = 33 },
155 .p2 = { .dot_limit = 165000,
156 .p2_slow = 4, .p2_fast = 4 },
159 static const intel_limit_t intel_limits_i8xx_lvds = {
160 .dot = { .min = 25000, .max = 350000 },
161 .vco = { .min = 908000, .max = 1512000 },
162 .n = { .min = 2, .max = 16 },
163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 1, .max = 6 },
168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 14, .p2_fast = 7 },
172 static const intel_limit_t intel_limits_i9xx_sdvo = {
173 .dot = { .min = 20000, .max = 400000 },
174 .vco = { .min = 1400000, .max = 2800000 },
175 .n = { .min = 1, .max = 6 },
176 .m = { .min = 70, .max = 120 },
177 .m1 = { .min = 8, .max = 18 },
178 .m2 = { .min = 3, .max = 7 },
179 .p = { .min = 5, .max = 80 },
180 .p1 = { .min = 1, .max = 8 },
181 .p2 = { .dot_limit = 200000,
182 .p2_slow = 10, .p2_fast = 5 },
185 static const intel_limit_t intel_limits_i9xx_lvds = {
186 .dot = { .min = 20000, .max = 400000 },
187 .vco = { .min = 1400000, .max = 2800000 },
188 .n = { .min = 1, .max = 6 },
189 .m = { .min = 70, .max = 120 },
190 .m1 = { .min = 8, .max = 18 },
191 .m2 = { .min = 3, .max = 7 },
192 .p = { .min = 7, .max = 98 },
193 .p1 = { .min = 1, .max = 8 },
194 .p2 = { .dot_limit = 112000,
195 .p2_slow = 14, .p2_fast = 7 },
199 static const intel_limit_t intel_limits_g4x_sdvo = {
200 .dot = { .min = 25000, .max = 270000 },
201 .vco = { .min = 1750000, .max = 3500000},
202 .n = { .min = 1, .max = 4 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 10, .max = 30 },
207 .p1 = { .min = 1, .max = 3},
208 .p2 = { .dot_limit = 270000,
214 static const intel_limit_t intel_limits_g4x_hdmi = {
215 .dot = { .min = 22000, .max = 400000 },
216 .vco = { .min = 1750000, .max = 3500000},
217 .n = { .min = 1, .max = 4 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 16, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 5, .max = 80 },
222 .p1 = { .min = 1, .max = 8},
223 .p2 = { .dot_limit = 165000,
224 .p2_slow = 10, .p2_fast = 5 },
227 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
228 .dot = { .min = 20000, .max = 115000 },
229 .vco = { .min = 1750000, .max = 3500000 },
230 .n = { .min = 1, .max = 3 },
231 .m = { .min = 104, .max = 138 },
232 .m1 = { .min = 17, .max = 23 },
233 .m2 = { .min = 5, .max = 11 },
234 .p = { .min = 28, .max = 112 },
235 .p1 = { .min = 2, .max = 8 },
236 .p2 = { .dot_limit = 0,
237 .p2_slow = 14, .p2_fast = 14
241 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
242 .dot = { .min = 80000, .max = 224000 },
243 .vco = { .min = 1750000, .max = 3500000 },
244 .n = { .min = 1, .max = 3 },
245 .m = { .min = 104, .max = 138 },
246 .m1 = { .min = 17, .max = 23 },
247 .m2 = { .min = 5, .max = 11 },
248 .p = { .min = 14, .max = 42 },
249 .p1 = { .min = 2, .max = 6 },
250 .p2 = { .dot_limit = 0,
251 .p2_slow = 7, .p2_fast = 7
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 },
270 static const intel_limit_t intel_limits_pineview_lvds = {
271 .dot = { .min = 20000, .max = 400000 },
272 .vco = { .min = 1700000, .max = 3500000 },
273 .n = { .min = 3, .max = 6 },
274 .m = { .min = 2, .max = 256 },
275 .m1 = { .min = 0, .max = 0 },
276 .m2 = { .min = 0, .max = 254 },
277 .p = { .min = 7, .max = 112 },
278 .p1 = { .min = 1, .max = 8 },
279 .p2 = { .dot_limit = 112000,
280 .p2_slow = 14, .p2_fast = 14 },
283 /* Ironlake / Sandybridge
285 * We calculate clock using (register_value + 2) for N/M1/M2, so here
286 * the range value for them is (actual_value - 2).
288 static const intel_limit_t intel_limits_ironlake_dac = {
289 .dot = { .min = 25000, .max = 350000 },
290 .vco = { .min = 1760000, .max = 3510000 },
291 .n = { .min = 1, .max = 5 },
292 .m = { .min = 79, .max = 127 },
293 .m1 = { .min = 12, .max = 22 },
294 .m2 = { .min = 5, .max = 9 },
295 .p = { .min = 5, .max = 80 },
296 .p1 = { .min = 1, .max = 8 },
297 .p2 = { .dot_limit = 225000,
298 .p2_slow = 10, .p2_fast = 5 },
301 static const intel_limit_t intel_limits_ironlake_single_lvds = {
302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 3 },
305 .m = { .min = 79, .max = 118 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 28, .max = 112 },
309 .p1 = { .min = 2, .max = 8 },
310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 14, .p2_fast = 14 },
314 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 3 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 14, .max = 56 },
322 .p1 = { .min = 2, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 7, .p2_fast = 7 },
327 /* LVDS 100mhz refclk limits. */
328 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
329 .dot = { .min = 25000, .max = 350000 },
330 .vco = { .min = 1760000, .max = 3510000 },
331 .n = { .min = 1, .max = 2 },
332 .m = { .min = 79, .max = 126 },
333 .m1 = { .min = 12, .max = 22 },
334 .m2 = { .min = 5, .max = 9 },
335 .p = { .min = 28, .max = 112 },
336 .p1 = { .min = 2, .max = 8 },
337 .p2 = { .dot_limit = 225000,
338 .p2_slow = 14, .p2_fast = 14 },
341 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
342 .dot = { .min = 25000, .max = 350000 },
343 .vco = { .min = 1760000, .max = 3510000 },
344 .n = { .min = 1, .max = 3 },
345 .m = { .min = 79, .max = 126 },
346 .m1 = { .min = 12, .max = 22 },
347 .m2 = { .min = 5, .max = 9 },
348 .p = { .min = 14, .max = 42 },
349 .p1 = { .min = 2, .max = 6 },
350 .p2 = { .dot_limit = 225000,
351 .p2_slow = 7, .p2_fast = 7 },
354 static const intel_limit_t intel_limits_vlv = {
356 * These are the data rate limits (measured in fast clocks)
357 * since those are the strictest limits we have. The fast
358 * clock and actual rate limits are more relaxed, so checking
359 * them would make no difference.
361 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
362 .vco = { .min = 4000000, .max = 6000000 },
363 .n = { .min = 1, .max = 7 },
364 .m1 = { .min = 2, .max = 3 },
365 .m2 = { .min = 11, .max = 156 },
366 .p1 = { .min = 2, .max = 3 },
367 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
370 static const intel_limit_t intel_limits_chv = {
372 * These are the data rate limits (measured in fast clocks)
373 * since those are the strictest limits we have. The fast
374 * clock and actual rate limits are more relaxed, so checking
375 * them would make no difference.
377 .dot = { .min = 25000 * 5, .max = 540000 * 5},
378 .vco = { .min = 4860000, .max = 6700000 },
379 .n = { .min = 1, .max = 1 },
380 .m1 = { .min = 2, .max = 2 },
381 .m2 = { .min = 24 << 22, .max = 175 << 22 },
382 .p1 = { .min = 2, .max = 4 },
383 .p2 = { .p2_slow = 1, .p2_fast = 14 },
386 static void vlv_clock(int refclk, intel_clock_t *clock)
388 clock->m = clock->m1 * clock->m2;
389 clock->p = clock->p1 * clock->p2;
390 if (WARN_ON(clock->n == 0 || clock->p == 0))
392 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
393 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
397 * Returns whether any output on the specified pipe is of the specified type
399 static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
401 struct drm_device *dev = crtc->dev;
402 struct intel_encoder *encoder;
404 for_each_encoder_on_crtc(dev, crtc, encoder)
405 if (encoder->type == type)
411 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
414 struct drm_device *dev = crtc->dev;
415 const intel_limit_t *limit;
417 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
418 if (intel_is_dual_link_lvds(dev)) {
419 if (refclk == 100000)
420 limit = &intel_limits_ironlake_dual_lvds_100m;
422 limit = &intel_limits_ironlake_dual_lvds;
424 if (refclk == 100000)
425 limit = &intel_limits_ironlake_single_lvds_100m;
427 limit = &intel_limits_ironlake_single_lvds;
430 limit = &intel_limits_ironlake_dac;
435 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
437 struct drm_device *dev = crtc->dev;
438 const intel_limit_t *limit;
440 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
441 if (intel_is_dual_link_lvds(dev))
442 limit = &intel_limits_g4x_dual_channel_lvds;
444 limit = &intel_limits_g4x_single_channel_lvds;
445 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
446 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
447 limit = &intel_limits_g4x_hdmi;
448 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
449 limit = &intel_limits_g4x_sdvo;
450 } else /* The option is for other outputs */
451 limit = &intel_limits_i9xx_sdvo;
456 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
458 struct drm_device *dev = crtc->dev;
459 const intel_limit_t *limit;
461 if (HAS_PCH_SPLIT(dev))
462 limit = intel_ironlake_limit(crtc, refclk);
463 else if (IS_G4X(dev)) {
464 limit = intel_g4x_limit(crtc);
465 } else if (IS_PINEVIEW(dev)) {
466 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
467 limit = &intel_limits_pineview_lvds;
469 limit = &intel_limits_pineview_sdvo;
470 } else if (IS_CHERRYVIEW(dev)) {
471 limit = &intel_limits_chv;
472 } else if (IS_VALLEYVIEW(dev)) {
473 limit = &intel_limits_vlv;
474 } else if (!IS_GEN2(dev)) {
475 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
476 limit = &intel_limits_i9xx_lvds;
478 limit = &intel_limits_i9xx_sdvo;
480 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
481 limit = &intel_limits_i8xx_lvds;
482 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
483 limit = &intel_limits_i8xx_dvo;
485 limit = &intel_limits_i8xx_dac;
490 /* m1 is reserved as 0 in Pineview, n is a ring counter */
491 static void pineview_clock(int refclk, intel_clock_t *clock)
493 clock->m = clock->m2 + 2;
494 clock->p = clock->p1 * clock->p2;
495 if (WARN_ON(clock->n == 0 || clock->p == 0))
497 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
498 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
501 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
503 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
506 static void i9xx_clock(int refclk, intel_clock_t *clock)
508 clock->m = i9xx_dpll_compute_m(clock);
509 clock->p = clock->p1 * clock->p2;
510 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
512 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
513 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
516 static void chv_clock(int refclk, intel_clock_t *clock)
518 clock->m = clock->m1 * clock->m2;
519 clock->p = clock->p1 * clock->p2;
520 if (WARN_ON(clock->n == 0 || clock->p == 0))
522 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
524 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
527 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
529 * Returns whether the given set of divisors are valid for a given refclk with
530 * the given connectors.
533 static bool intel_PLL_is_valid(struct drm_device *dev,
534 const intel_limit_t *limit,
535 const intel_clock_t *clock)
537 if (clock->n < limit->n.min || limit->n.max < clock->n)
538 INTELPllInvalid("n out of range\n");
539 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
540 INTELPllInvalid("p1 out of range\n");
541 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
542 INTELPllInvalid("m2 out of range\n");
543 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
544 INTELPllInvalid("m1 out of range\n");
546 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
547 if (clock->m1 <= clock->m2)
548 INTELPllInvalid("m1 <= m2\n");
550 if (!IS_VALLEYVIEW(dev)) {
551 if (clock->p < limit->p.min || limit->p.max < clock->p)
552 INTELPllInvalid("p out of range\n");
553 if (clock->m < limit->m.min || limit->m.max < clock->m)
554 INTELPllInvalid("m out of range\n");
557 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
558 INTELPllInvalid("vco out of range\n");
559 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
560 * connector, etc., rather than just a single range.
562 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
563 INTELPllInvalid("dot out of range\n");
569 i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
570 int target, int refclk, intel_clock_t *match_clock,
571 intel_clock_t *best_clock)
573 struct drm_device *dev = crtc->dev;
577 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
579 * For LVDS just rely on its current settings for dual-channel.
580 * We haven't figured out how to reliably set up different
581 * single/dual channel state, if we even can.
583 if (intel_is_dual_link_lvds(dev))
584 clock.p2 = limit->p2.p2_fast;
586 clock.p2 = limit->p2.p2_slow;
588 if (target < limit->p2.dot_limit)
589 clock.p2 = limit->p2.p2_slow;
591 clock.p2 = limit->p2.p2_fast;
594 memset(best_clock, 0, sizeof(*best_clock));
596 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
598 for (clock.m2 = limit->m2.min;
599 clock.m2 <= limit->m2.max; clock.m2++) {
600 if (clock.m2 >= clock.m1)
602 for (clock.n = limit->n.min;
603 clock.n <= limit->n.max; clock.n++) {
604 for (clock.p1 = limit->p1.min;
605 clock.p1 <= limit->p1.max; clock.p1++) {
608 i9xx_clock(refclk, &clock);
609 if (!intel_PLL_is_valid(dev, limit,
613 clock.p != match_clock->p)
616 this_err = abs(clock.dot - target);
617 if (this_err < err) {
626 return (err != target);
630 pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
631 int target, int refclk, intel_clock_t *match_clock,
632 intel_clock_t *best_clock)
634 struct drm_device *dev = crtc->dev;
638 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
640 * For LVDS just rely on its current settings for dual-channel.
641 * We haven't figured out how to reliably set up different
642 * single/dual channel state, if we even can.
644 if (intel_is_dual_link_lvds(dev))
645 clock.p2 = limit->p2.p2_fast;
647 clock.p2 = limit->p2.p2_slow;
649 if (target < limit->p2.dot_limit)
650 clock.p2 = limit->p2.p2_slow;
652 clock.p2 = limit->p2.p2_fast;
655 memset(best_clock, 0, sizeof(*best_clock));
657 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
659 for (clock.m2 = limit->m2.min;
660 clock.m2 <= limit->m2.max; clock.m2++) {
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 pineview_clock(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 g4x_find_best_dpll(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)) {
702 if (intel_is_dual_link_lvds(dev))
703 clock.p2 = limit->p2.p2_fast;
705 clock.p2 = limit->p2.p2_slow;
707 if (target < limit->p2.dot_limit)
708 clock.p2 = limit->p2.p2_slow;
710 clock.p2 = limit->p2.p2_fast;
713 memset(best_clock, 0, sizeof(*best_clock));
714 max_n = limit->n.max;
715 /* based on hardware requirement, prefer smaller n to precision */
716 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
717 /* based on hardware requirement, prefere larger m1,m2 */
718 for (clock.m1 = limit->m1.max;
719 clock.m1 >= limit->m1.min; clock.m1--) {
720 for (clock.m2 = limit->m2.max;
721 clock.m2 >= limit->m2.min; clock.m2--) {
722 for (clock.p1 = limit->p1.max;
723 clock.p1 >= limit->p1.min; clock.p1--) {
726 i9xx_clock(refclk, &clock);
727 if (!intel_PLL_is_valid(dev, limit,
731 this_err = abs(clock.dot - target);
732 if (this_err < err_most) {
746 vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
747 int target, int refclk, intel_clock_t *match_clock,
748 intel_clock_t *best_clock)
750 struct drm_device *dev = crtc->dev;
752 unsigned int bestppm = 1000000;
753 /* min update 19.2 MHz */
754 int max_n = min(limit->n.max, refclk / 19200);
757 target *= 5; /* fast clock */
759 memset(best_clock, 0, sizeof(*best_clock));
761 /* based on hardware requirement, prefer smaller n to precision */
762 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
763 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
764 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
765 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
766 clock.p = clock.p1 * clock.p2;
767 /* based on hardware requirement, prefer bigger m1,m2 values */
768 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
769 unsigned int ppm, diff;
771 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
774 vlv_clock(refclk, &clock);
776 if (!intel_PLL_is_valid(dev, limit,
780 diff = abs(clock.dot - target);
781 ppm = div_u64(1000000ULL * diff, target);
783 if (ppm < 100 && clock.p > best_clock->p) {
789 if (bestppm >= 10 && ppm < bestppm - 10) {
803 chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
804 int target, int refclk, intel_clock_t *match_clock,
805 intel_clock_t *best_clock)
807 struct drm_device *dev = crtc->dev;
812 memset(best_clock, 0, sizeof(*best_clock));
815 * Based on hardware doc, the n always set to 1, and m1 always
816 * set to 2. If requires to support 200Mhz refclk, we need to
817 * revisit this because n may not 1 anymore.
819 clock.n = 1, clock.m1 = 2;
820 target *= 5; /* fast clock */
822 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
823 for (clock.p2 = limit->p2.p2_fast;
824 clock.p2 >= limit->p2.p2_slow;
825 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
827 clock.p = clock.p1 * clock.p2;
829 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
830 clock.n) << 22, refclk * clock.m1);
832 if (m2 > INT_MAX/clock.m1)
837 chv_clock(refclk, &clock);
839 if (!intel_PLL_is_valid(dev, limit, &clock))
842 /* based on hardware requirement, prefer bigger p
844 if (clock.p > best_clock->p) {
854 bool intel_crtc_active(struct drm_crtc *crtc)
856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
858 /* Be paranoid as we can arrive here with only partial
859 * state retrieved from the hardware during setup.
861 * We can ditch the adjusted_mode.crtc_clock check as soon
862 * as Haswell has gained clock readout/fastboot support.
864 * We can ditch the crtc->primary->fb check as soon as we can
865 * properly reconstruct framebuffers.
867 return intel_crtc->active && crtc->primary->fb &&
868 intel_crtc->config.adjusted_mode.crtc_clock;
871 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
874 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
877 return intel_crtc->config.cpu_transcoder;
880 static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
882 struct drm_i915_private *dev_priv = dev->dev_private;
883 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
885 frame = I915_READ(frame_reg);
887 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
888 WARN(1, "vblank wait on pipe %c timed out\n",
893 * intel_wait_for_vblank - wait for vblank on a given pipe
895 * @pipe: pipe to wait for
897 * Wait for vblank to occur on a given pipe. Needed for various bits of
900 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
902 struct drm_i915_private *dev_priv = dev->dev_private;
903 int pipestat_reg = PIPESTAT(pipe);
905 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
906 g4x_wait_for_vblank(dev, pipe);
910 /* Clear existing vblank status. Note this will clear any other
911 * sticky status fields as well.
913 * This races with i915_driver_irq_handler() with the result
914 * that either function could miss a vblank event. Here it is not
915 * fatal, as we will either wait upon the next vblank interrupt or
916 * timeout. Generally speaking intel_wait_for_vblank() is only
917 * called during modeset at which time the GPU should be idle and
918 * should *not* be performing page flips and thus not waiting on
920 * Currently, the result of us stealing a vblank from the irq
921 * handler is that a single frame will be skipped during swapbuffers.
923 I915_WRITE(pipestat_reg,
924 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
926 /* Wait for vblank interrupt bit to set */
927 if (wait_for(I915_READ(pipestat_reg) &
928 PIPE_VBLANK_INTERRUPT_STATUS,
930 DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
934 static bool pipe_dsl_stopped(struct drm_device *dev, enum i915_pipe pipe)
936 struct drm_i915_private *dev_priv = dev->dev_private;
937 u32 reg = PIPEDSL(pipe);
942 line_mask = DSL_LINEMASK_GEN2;
944 line_mask = DSL_LINEMASK_GEN3;
946 line1 = I915_READ(reg) & line_mask;
948 line2 = I915_READ(reg) & line_mask;
950 return line1 == line2;
954 * intel_wait_for_pipe_off - wait for pipe to turn off
955 * @crtc: crtc whose pipe to wait for
957 * After disabling a pipe, we can't wait for vblank in the usual way,
958 * spinning on the vblank interrupt status bit, since we won't actually
959 * see an interrupt when the pipe is disabled.
962 * wait for the pipe register state bit to turn off
965 * wait for the display line value to settle (it usually
966 * ends up stopping at the start of the next frame).
969 static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
971 struct drm_device *dev = crtc->base.dev;
972 struct drm_i915_private *dev_priv = dev->dev_private;
973 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
974 enum i915_pipe pipe = crtc->pipe;
976 if (INTEL_INFO(dev)->gen >= 4) {
977 int reg = PIPECONF(cpu_transcoder);
979 /* Wait for the Pipe State to go off */
980 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
982 WARN(1, "pipe_off wait timed out\n");
984 /* Wait for the display line to settle */
985 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
986 WARN(1, "pipe_off wait timed out\n");
991 * ibx_digital_port_connected - is the specified port connected?
992 * @dev_priv: i915 private structure
993 * @port: the port to test
995 * Returns true if @port is connected, false otherwise.
997 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
998 struct intel_digital_port *port)
1002 if (HAS_PCH_IBX(dev_priv->dev)) {
1003 switch (port->port) {
1005 bit = SDE_PORTB_HOTPLUG;
1008 bit = SDE_PORTC_HOTPLUG;
1011 bit = SDE_PORTD_HOTPLUG;
1017 switch (port->port) {
1019 bit = SDE_PORTB_HOTPLUG_CPT;
1022 bit = SDE_PORTC_HOTPLUG_CPT;
1025 bit = SDE_PORTD_HOTPLUG_CPT;
1032 return I915_READ(SDEISR) & bit;
1035 static const char *state_string(bool enabled)
1037 return enabled ? "on" : "off";
1040 /* Only for pre-ILK configs */
1041 void assert_pll(struct drm_i915_private *dev_priv,
1042 enum i915_pipe pipe, bool state)
1049 val = I915_READ(reg);
1050 cur_state = !!(val & DPLL_VCO_ENABLE);
1051 WARN(cur_state != state,
1052 "PLL state assertion failure (expected %s, current %s)\n",
1053 state_string(state), state_string(cur_state));
1056 /* XXX: the dsi pll is shared between MIPI DSI ports */
1057 static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1062 mutex_lock(&dev_priv->dpio_lock);
1063 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1064 mutex_unlock(&dev_priv->dpio_lock);
1066 cur_state = val & DSI_PLL_VCO_EN;
1067 WARN(cur_state != state,
1068 "DSI PLL state assertion failure (expected %s, current %s)\n",
1069 state_string(state), state_string(cur_state));
1071 #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1072 #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1074 struct intel_shared_dpll *
1075 intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1077 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1079 if (crtc->config.shared_dpll < 0)
1082 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
1086 void assert_shared_dpll(struct drm_i915_private *dev_priv,
1087 struct intel_shared_dpll *pll,
1091 struct intel_dpll_hw_state hw_state;
1094 "asserting DPLL %s with no DPLL\n", state_string(state)))
1097 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
1098 WARN(cur_state != state,
1099 "%s assertion failure (expected %s, current %s)\n",
1100 pll->name, state_string(state), state_string(cur_state));
1103 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1104 enum i915_pipe pipe, bool state)
1109 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1112 if (HAS_DDI(dev_priv->dev)) {
1113 /* DDI does not have a specific FDI_TX register */
1114 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1118 reg = FDI_TX_CTL(pipe);
1119 val = I915_READ(reg);
1120 cur_state = !!(val & FDI_TX_ENABLE);
1122 WARN(cur_state != state,
1123 "FDI TX state assertion failure (expected %s, current %s)\n",
1124 state_string(state), state_string(cur_state));
1126 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1127 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1129 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1130 enum i915_pipe pipe, bool state)
1136 reg = FDI_RX_CTL(pipe);
1137 val = I915_READ(reg);
1138 cur_state = !!(val & FDI_RX_ENABLE);
1139 WARN(cur_state != state,
1140 "FDI RX state assertion failure (expected %s, current %s)\n",
1141 state_string(state), state_string(cur_state));
1143 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1144 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1146 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1147 enum i915_pipe pipe)
1152 /* ILK FDI PLL is always enabled */
1153 if (INTEL_INFO(dev_priv->dev)->gen == 5)
1156 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1157 if (HAS_DDI(dev_priv->dev))
1160 reg = FDI_TX_CTL(pipe);
1161 val = I915_READ(reg);
1162 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1165 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1166 enum i915_pipe pipe, bool state)
1172 reg = FDI_RX_CTL(pipe);
1173 val = I915_READ(reg);
1174 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1175 WARN(cur_state != state,
1176 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1177 state_string(state), state_string(cur_state));
1180 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1181 enum i915_pipe pipe)
1183 struct drm_device *dev = dev_priv->dev;
1186 enum i915_pipe panel_pipe = PIPE_A;
1189 if (WARN_ON(HAS_DDI(dev)))
1192 if (HAS_PCH_SPLIT(dev)) {
1195 pp_reg = PCH_PP_CONTROL;
1196 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1198 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1199 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1200 panel_pipe = PIPE_B;
1201 /* XXX: else fix for eDP */
1202 } else if (IS_VALLEYVIEW(dev)) {
1203 /* presumably write lock depends on pipe, not port select */
1204 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1207 pp_reg = PP_CONTROL;
1208 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1212 val = I915_READ(pp_reg);
1213 if (!(val & PANEL_POWER_ON) ||
1214 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1217 WARN(panel_pipe == pipe && locked,
1218 "panel assertion failure, pipe %c regs locked\n",
1222 static void assert_cursor(struct drm_i915_private *dev_priv,
1223 enum i915_pipe pipe, bool state)
1225 struct drm_device *dev = dev_priv->dev;
1228 if (IS_845G(dev) || IS_I865G(dev))
1229 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1231 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1233 WARN(cur_state != state,
1234 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1235 pipe_name(pipe), state_string(state), state_string(cur_state));
1237 #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1238 #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1240 void assert_pipe(struct drm_i915_private *dev_priv,
1241 enum i915_pipe pipe, bool state)
1246 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1249 /* if we need the pipe quirk it must be always on */
1250 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1251 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1254 if (!intel_display_power_enabled(dev_priv,
1255 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1258 reg = PIPECONF(cpu_transcoder);
1259 val = I915_READ(reg);
1260 cur_state = !!(val & PIPECONF_ENABLE);
1263 WARN(cur_state != state,
1264 "pipe %c assertion failure (expected %s, current %s)\n",
1265 pipe_name(pipe), state_string(state), state_string(cur_state));
1268 static void assert_plane(struct drm_i915_private *dev_priv,
1269 enum plane plane, bool state)
1275 reg = DSPCNTR(plane);
1276 val = I915_READ(reg);
1277 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1278 WARN(cur_state != state,
1279 "plane %c assertion failure (expected %s, current %s)\n",
1280 plane_name(plane), state_string(state), state_string(cur_state));
1283 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1284 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1286 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1287 enum i915_pipe pipe)
1289 struct drm_device *dev = dev_priv->dev;
1294 /* Primary planes are fixed to pipes on gen4+ */
1295 if (INTEL_INFO(dev)->gen >= 4) {
1296 reg = DSPCNTR(pipe);
1297 val = I915_READ(reg);
1298 WARN(val & DISPLAY_PLANE_ENABLE,
1299 "plane %c assertion failure, should be disabled but not\n",
1304 /* Need to check both planes against the pipe */
1305 for_each_pipe(dev_priv, i) {
1307 val = I915_READ(reg);
1308 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1309 DISPPLANE_SEL_PIPE_SHIFT;
1310 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1311 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1312 plane_name(i), pipe_name(pipe));
1316 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1317 enum i915_pipe pipe)
1319 struct drm_device *dev = dev_priv->dev;
1323 if (IS_VALLEYVIEW(dev)) {
1324 for_each_sprite(pipe, sprite) {
1325 reg = SPCNTR(pipe, sprite);
1326 val = I915_READ(reg);
1327 WARN(val & SP_ENABLE,
1328 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1329 sprite_name(pipe, sprite), pipe_name(pipe));
1331 } else if (INTEL_INFO(dev)->gen >= 7) {
1333 val = I915_READ(reg);
1334 WARN(val & SPRITE_ENABLE,
1335 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1336 plane_name(pipe), pipe_name(pipe));
1337 } else if (INTEL_INFO(dev)->gen >= 5) {
1338 reg = DVSCNTR(pipe);
1339 val = I915_READ(reg);
1340 WARN(val & DVS_ENABLE,
1341 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1342 plane_name(pipe), pipe_name(pipe));
1346 static void assert_vblank_disabled(struct drm_crtc *crtc)
1348 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1349 drm_crtc_vblank_put(crtc);
1352 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1357 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
1359 val = I915_READ(PCH_DREF_CONTROL);
1360 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1361 DREF_SUPERSPREAD_SOURCE_MASK));
1362 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1365 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1366 enum i915_pipe pipe)
1372 reg = PCH_TRANSCONF(pipe);
1373 val = I915_READ(reg);
1374 enabled = !!(val & TRANS_ENABLE);
1376 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1380 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1381 enum i915_pipe pipe, u32 port_sel, u32 val)
1383 if ((val & DP_PORT_EN) == 0)
1386 if (HAS_PCH_CPT(dev_priv->dev)) {
1387 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1388 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1389 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1391 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1392 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1395 if ((val & DP_PIPE_MASK) != (pipe << 30))
1401 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1402 enum i915_pipe pipe, u32 val)
1404 if ((val & SDVO_ENABLE) == 0)
1407 if (HAS_PCH_CPT(dev_priv->dev)) {
1408 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1410 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1411 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1414 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1420 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1421 enum i915_pipe pipe, u32 val)
1423 if ((val & LVDS_PORT_EN) == 0)
1426 if (HAS_PCH_CPT(dev_priv->dev)) {
1427 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1430 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1436 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1437 enum i915_pipe pipe, u32 val)
1439 if ((val & ADPA_DAC_ENABLE) == 0)
1441 if (HAS_PCH_CPT(dev_priv->dev)) {
1442 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1445 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1451 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1452 enum i915_pipe pipe, int reg, u32 port_sel)
1454 u32 val = I915_READ(reg);
1455 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1456 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1457 reg, pipe_name(pipe));
1459 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1460 && (val & DP_PIPEB_SELECT),
1461 "IBX PCH dp port still using transcoder B\n");
1464 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1465 enum i915_pipe pipe, int reg)
1467 u32 val = I915_READ(reg);
1468 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1469 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1470 reg, pipe_name(pipe));
1472 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1473 && (val & SDVO_PIPE_B_SELECT),
1474 "IBX PCH hdmi port still using transcoder B\n");
1477 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1478 enum i915_pipe pipe)
1483 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1484 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1485 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1488 val = I915_READ(reg);
1489 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1490 "PCH VGA enabled on transcoder %c, should be disabled\n",
1494 val = I915_READ(reg);
1495 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1496 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1499 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1500 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1501 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1504 static void intel_init_dpio(struct drm_device *dev)
1506 struct drm_i915_private *dev_priv = dev->dev_private;
1508 if (!IS_VALLEYVIEW(dev))
1512 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1513 * CHV x1 PHY (DP/HDMI D)
1514 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1516 if (IS_CHERRYVIEW(dev)) {
1517 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1518 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1520 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1524 static void vlv_enable_pll(struct intel_crtc *crtc)
1526 struct drm_device *dev = crtc->base.dev;
1527 struct drm_i915_private *dev_priv = dev->dev_private;
1528 int reg = DPLL(crtc->pipe);
1529 u32 dpll = crtc->config.dpll_hw_state.dpll;
1531 assert_pipe_disabled(dev_priv, crtc->pipe);
1533 /* No really, not for ILK+ */
1534 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1536 /* PLL is protected by panel, make sure we can write it */
1537 if (IS_MOBILE(dev_priv->dev))
1538 assert_panel_unlocked(dev_priv, crtc->pipe);
1540 I915_WRITE(reg, dpll);
1544 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1545 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1547 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1548 POSTING_READ(DPLL_MD(crtc->pipe));
1550 /* We do this three times for luck */
1551 I915_WRITE(reg, dpll);
1553 udelay(150); /* wait for warmup */
1554 I915_WRITE(reg, dpll);
1556 udelay(150); /* wait for warmup */
1557 I915_WRITE(reg, dpll);
1559 udelay(150); /* wait for warmup */
1562 static void chv_enable_pll(struct intel_crtc *crtc)
1564 struct drm_device *dev = crtc->base.dev;
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566 int pipe = crtc->pipe;
1567 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1570 assert_pipe_disabled(dev_priv, crtc->pipe);
1572 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1574 mutex_lock(&dev_priv->dpio_lock);
1576 /* Enable back the 10bit clock to display controller */
1577 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1578 tmp |= DPIO_DCLKP_EN;
1579 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1587 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
1589 /* Check PLL is locked */
1590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1593 /* not sure when this should be written */
1594 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1595 POSTING_READ(DPLL_MD(pipe));
1597 mutex_unlock(&dev_priv->dpio_lock);
1600 static int intel_num_dvo_pipes(struct drm_device *dev)
1602 struct intel_crtc *crtc;
1605 for_each_intel_crtc(dev, crtc)
1606 count += crtc->active &&
1607 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
1612 static void i9xx_enable_pll(struct intel_crtc *crtc)
1614 struct drm_device *dev = crtc->base.dev;
1615 struct drm_i915_private *dev_priv = dev->dev_private;
1616 int reg = DPLL(crtc->pipe);
1617 u32 dpll = crtc->config.dpll_hw_state.dpll;
1619 assert_pipe_disabled(dev_priv, crtc->pipe);
1621 /* No really, not for ILK+ */
1622 BUG_ON(INTEL_INFO(dev)->gen >= 5);
1624 /* PLL is protected by panel, make sure we can write it */
1625 if (IS_MOBILE(dev) && !IS_I830(dev))
1626 assert_panel_unlocked(dev_priv, crtc->pipe);
1628 /* Enable DVO 2x clock on both PLLs if necessary */
1629 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1631 * It appears to be important that we don't enable this
1632 * for the current pipe before otherwise configuring the
1633 * PLL. No idea how this should be handled if multiple
1634 * DVO outputs are enabled simultaneosly.
1636 dpll |= DPLL_DVO_2X_MODE;
1637 I915_WRITE(DPLL(!crtc->pipe),
1638 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1641 /* Wait for the clocks to stabilize. */
1645 if (INTEL_INFO(dev)->gen >= 4) {
1646 I915_WRITE(DPLL_MD(crtc->pipe),
1647 crtc->config.dpll_hw_state.dpll_md);
1649 /* The pixel multiplier can only be updated once the
1650 * DPLL is enabled and the clocks are stable.
1652 * So write it again.
1654 I915_WRITE(reg, dpll);
1657 /* We do this three times for luck */
1658 I915_WRITE(reg, dpll);
1660 udelay(150); /* wait for warmup */
1661 I915_WRITE(reg, dpll);
1663 udelay(150); /* wait for warmup */
1664 I915_WRITE(reg, dpll);
1666 udelay(150); /* wait for warmup */
1670 * i9xx_disable_pll - disable a PLL
1671 * @dev_priv: i915 private structure
1672 * @pipe: pipe PLL to disable
1674 * Disable the PLL for @pipe, making sure the pipe is off first.
1676 * Note! This is for pre-ILK only.
1678 static void i9xx_disable_pll(struct intel_crtc *crtc)
1680 struct drm_device *dev = crtc->base.dev;
1681 struct drm_i915_private *dev_priv = dev->dev_private;
1682 enum i915_pipe pipe = crtc->pipe;
1684 /* Disable DVO 2x clock on both PLLs if necessary */
1686 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
1687 intel_num_dvo_pipes(dev) == 1) {
1688 I915_WRITE(DPLL(PIPE_B),
1689 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1690 I915_WRITE(DPLL(PIPE_A),
1691 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1694 /* Don't disable pipe or pipe PLLs if needed */
1695 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1696 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
1702 I915_WRITE(DPLL(pipe), 0);
1703 POSTING_READ(DPLL(pipe));
1706 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1710 /* Make sure the pipe isn't still relying on us */
1711 assert_pipe_disabled(dev_priv, pipe);
1714 * Leave integrated clock source and reference clock enabled for pipe B.
1715 * The latter is needed for VGA hotplug / manual detection.
1718 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
1719 I915_WRITE(DPLL(pipe), val);
1720 POSTING_READ(DPLL(pipe));
1724 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1726 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1729 /* Make sure the pipe isn't still relying on us */
1730 assert_pipe_disabled(dev_priv, pipe);
1732 /* Set PLL en = 0 */
1733 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
1735 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1736 I915_WRITE(DPLL(pipe), val);
1737 POSTING_READ(DPLL(pipe));
1739 mutex_lock(&dev_priv->dpio_lock);
1741 /* Disable 10bit clock to display controller */
1742 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1743 val &= ~DPIO_DCLKP_EN;
1744 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1746 /* disable left/right clock distribution */
1747 if (pipe != PIPE_B) {
1748 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1749 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1750 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1752 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1753 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1754 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1757 mutex_unlock(&dev_priv->dpio_lock);
1760 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1761 struct intel_digital_port *dport)
1766 switch (dport->port) {
1768 port_mask = DPLL_PORTB_READY_MASK;
1772 port_mask = DPLL_PORTC_READY_MASK;
1776 port_mask = DPLL_PORTD_READY_MASK;
1777 dpll_reg = DPIO_PHY_STATUS;
1783 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
1784 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1785 port_name(dport->port), I915_READ(dpll_reg));
1788 static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1790 struct drm_device *dev = crtc->base.dev;
1791 struct drm_i915_private *dev_priv = dev->dev_private;
1792 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1794 if (WARN_ON(pll == NULL))
1797 WARN_ON(!pll->refcount);
1798 if (pll->active == 0) {
1799 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1801 assert_shared_dpll_disabled(dev_priv, pll);
1803 pll->mode_set(dev_priv, pll);
1808 * intel_enable_shared_dpll - enable PCH PLL
1809 * @dev_priv: i915 private structure
1810 * @pipe: pipe PLL to enable
1812 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1813 * drives the transcoder clock.
1815 static void intel_enable_shared_dpll(struct intel_crtc *crtc)
1817 struct drm_device *dev = crtc->base.dev;
1818 struct drm_i915_private *dev_priv = dev->dev_private;
1819 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1821 if (WARN_ON(pll == NULL))
1824 if (WARN_ON(pll->refcount == 0))
1827 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
1828 pll->name, pll->active, pll->on,
1829 crtc->base.base.id);
1831 if (pll->active++) {
1833 assert_shared_dpll_enabled(dev_priv, pll);
1838 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1840 DRM_DEBUG_KMS("enabling %s\n", pll->name);
1841 pll->enable(dev_priv, pll);
1845 static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1847 struct drm_device *dev = crtc->base.dev;
1848 struct drm_i915_private *dev_priv = dev->dev_private;
1849 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1851 /* PCH only available on ILK+ */
1852 BUG_ON(INTEL_INFO(dev)->gen < 5);
1853 if (WARN_ON(pll == NULL))
1856 if (WARN_ON(pll->refcount == 0))
1859 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1860 pll->name, pll->active, pll->on,
1861 crtc->base.base.id);
1863 if (WARN_ON(pll->active == 0)) {
1864 assert_shared_dpll_disabled(dev_priv, pll);
1868 assert_shared_dpll_enabled(dev_priv, pll);
1873 DRM_DEBUG_KMS("disabling %s\n", pll->name);
1874 pll->disable(dev_priv, pll);
1877 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1880 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1881 enum i915_pipe pipe)
1883 struct drm_device *dev = dev_priv->dev;
1884 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1886 uint32_t reg, val, pipeconf_val;
1888 /* PCH only available on ILK+ */
1889 BUG_ON(!HAS_PCH_SPLIT(dev));
1891 /* Make sure PCH DPLL is enabled */
1892 assert_shared_dpll_enabled(dev_priv,
1893 intel_crtc_to_shared_dpll(intel_crtc));
1895 /* FDI must be feeding us bits for PCH ports */
1896 assert_fdi_tx_enabled(dev_priv, pipe);
1897 assert_fdi_rx_enabled(dev_priv, pipe);
1899 if (HAS_PCH_CPT(dev)) {
1900 /* Workaround: Set the timing override bit before enabling the
1901 * pch transcoder. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1908 reg = PCH_TRANSCONF(pipe);
1909 val = I915_READ(reg);
1910 pipeconf_val = I915_READ(PIPECONF(pipe));
1912 if (HAS_PCH_IBX(dev_priv->dev)) {
1914 * make the BPC in transcoder be consistent with
1915 * that in pipeconf reg.
1917 val &= ~PIPECONF_BPC_MASK;
1918 val |= pipeconf_val & PIPECONF_BPC_MASK;
1921 val &= ~TRANS_INTERLACE_MASK;
1922 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1923 if (HAS_PCH_IBX(dev_priv->dev) &&
1924 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1925 val |= TRANS_LEGACY_INTERLACED_ILK;
1927 val |= TRANS_INTERLACED;
1929 val |= TRANS_PROGRESSIVE;
1931 I915_WRITE(reg, val | TRANS_ENABLE);
1932 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1933 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1936 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1937 enum transcoder cpu_transcoder)
1939 u32 val, pipeconf_val;
1941 /* PCH only available on ILK+ */
1942 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
1944 /* FDI must be feeding us bits for PCH ports */
1945 assert_fdi_tx_enabled(dev_priv, (enum i915_pipe) cpu_transcoder);
1946 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
1948 /* Workaround: set timing override bit. */
1949 val = I915_READ(_TRANSA_CHICKEN2);
1950 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1951 I915_WRITE(_TRANSA_CHICKEN2, val);
1954 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1956 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1957 PIPECONF_INTERLACED_ILK)
1958 val |= TRANS_INTERLACED;
1960 val |= TRANS_PROGRESSIVE;
1962 I915_WRITE(LPT_TRANSCONF, val);
1963 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
1964 DRM_ERROR("Failed to enable PCH transcoder\n");
1967 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1968 enum i915_pipe pipe)
1970 struct drm_device *dev = dev_priv->dev;
1973 /* FDI relies on the transcoder */
1974 assert_fdi_tx_disabled(dev_priv, pipe);
1975 assert_fdi_rx_disabled(dev_priv, pipe);
1977 /* Ports must be off as well */
1978 assert_pch_ports_disabled(dev_priv, pipe);
1980 reg = PCH_TRANSCONF(pipe);
1981 val = I915_READ(reg);
1982 val &= ~TRANS_ENABLE;
1983 I915_WRITE(reg, val);
1984 /* wait for PCH transcoder off, transcoder state */
1985 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
1986 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1988 if (!HAS_PCH_IBX(dev)) {
1989 /* Workaround: Clear the timing override chicken bit again. */
1990 reg = TRANS_CHICKEN2(pipe);
1991 val = I915_READ(reg);
1992 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1993 I915_WRITE(reg, val);
1997 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
2001 val = I915_READ(LPT_TRANSCONF);
2002 val &= ~TRANS_ENABLE;
2003 I915_WRITE(LPT_TRANSCONF, val);
2004 /* wait for PCH transcoder off, transcoder state */
2005 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
2006 DRM_ERROR("Failed to disable PCH transcoder\n");
2008 /* Workaround: clear timing override bit. */
2009 val = I915_READ(_TRANSA_CHICKEN2);
2010 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2011 I915_WRITE(_TRANSA_CHICKEN2, val);
2015 * intel_enable_pipe - enable a pipe, asserting requirements
2016 * @crtc: crtc responsible for the pipe
2018 * Enable @crtc's pipe, making sure that various hardware specific requirements
2019 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
2021 static void intel_enable_pipe(struct intel_crtc *crtc)
2023 struct drm_device *dev = crtc->base.dev;
2024 struct drm_i915_private *dev_priv = dev->dev_private;
2025 enum i915_pipe pipe = crtc->pipe;
2026 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2028 enum i915_pipe pch_transcoder;
2032 assert_planes_disabled(dev_priv, pipe);
2033 assert_cursor_disabled(dev_priv, pipe);
2034 assert_sprites_disabled(dev_priv, pipe);
2036 if (HAS_PCH_LPT(dev_priv->dev))
2037 pch_transcoder = TRANSCODER_A;
2039 pch_transcoder = pipe;
2042 * A pipe without a PLL won't actually be able to drive bits from
2043 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2046 if (!HAS_PCH_SPLIT(dev_priv->dev))
2047 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
2048 assert_dsi_pll_enabled(dev_priv);
2050 assert_pll_enabled(dev_priv, pipe);
2052 if (crtc->config.has_pch_encoder) {
2053 /* if driving the PCH, we need FDI enabled */
2054 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
2055 assert_fdi_tx_pll_enabled(dev_priv,
2056 (enum i915_pipe) cpu_transcoder);
2058 /* FIXME: assert CPU port conditions for SNB+ */
2061 reg = PIPECONF(cpu_transcoder);
2062 val = I915_READ(reg);
2063 if (val & PIPECONF_ENABLE) {
2064 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2065 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
2069 I915_WRITE(reg, val | PIPECONF_ENABLE);
2074 * intel_disable_pipe - disable a pipe, asserting requirements
2075 * @crtc: crtc whose pipes is to be disabled
2077 * Disable the pipe of @crtc, making sure that various hardware
2078 * specific requirements are met, if applicable, e.g. plane
2079 * disabled, panel fitter off, etc.
2081 * Will wait until the pipe has shut down before returning.
2083 static void intel_disable_pipe(struct intel_crtc *crtc)
2085 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2086 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2087 enum i915_pipe pipe = crtc->pipe;
2092 * Make sure planes won't keep trying to pump pixels to us,
2093 * or we might hang the display.
2095 assert_planes_disabled(dev_priv, pipe);
2096 assert_cursor_disabled(dev_priv, pipe);
2097 assert_sprites_disabled(dev_priv, pipe);
2099 reg = PIPECONF(cpu_transcoder);
2100 val = I915_READ(reg);
2101 if ((val & PIPECONF_ENABLE) == 0)
2105 * Double wide has implications for planes
2106 * so best keep it disabled when not needed.
2108 if (crtc->config.double_wide)
2109 val &= ~PIPECONF_DOUBLE_WIDE;
2111 /* Don't disable pipe or pipe PLLs if needed */
2112 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2113 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
2114 val &= ~PIPECONF_ENABLE;
2116 I915_WRITE(reg, val);
2117 if ((val & PIPECONF_ENABLE) == 0)
2118 intel_wait_for_pipe_off(crtc);
2122 * Plane regs are double buffered, going from enabled->disabled needs a
2123 * trigger in order to latch. The display address reg provides this.
2125 void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2128 struct drm_device *dev = dev_priv->dev;
2129 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
2131 I915_WRITE(reg, I915_READ(reg));
2136 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
2137 * @plane: plane to be enabled
2138 * @crtc: crtc for the plane
2140 * Enable @plane on @crtc, making sure that the pipe is running first.
2142 static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2143 struct drm_crtc *crtc)
2145 struct drm_device *dev = plane->dev;
2146 struct drm_i915_private *dev_priv = dev->dev_private;
2147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2149 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2150 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
2152 if (intel_crtc->primary_enabled)
2155 intel_crtc->primary_enabled = true;
2157 dev_priv->display.update_primary_plane(crtc, plane->fb,
2161 * BDW signals flip done immediately if the plane
2162 * is disabled, even if the plane enable is already
2163 * armed to occur at the next vblank :(
2165 if (IS_BROADWELL(dev))
2166 intel_wait_for_vblank(dev, intel_crtc->pipe);
2170 * intel_disable_primary_hw_plane - disable the primary hardware plane
2171 * @plane: plane to be disabled
2172 * @crtc: crtc for the plane
2174 * Disable @plane on @crtc, making sure that the pipe is running first.
2176 static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2177 struct drm_crtc *crtc)
2179 struct drm_device *dev = plane->dev;
2180 struct drm_i915_private *dev_priv = dev->dev_private;
2181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2183 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
2185 if (!intel_crtc->primary_enabled)
2188 intel_crtc->primary_enabled = false;
2190 dev_priv->display.update_primary_plane(crtc, plane->fb,
2194 static bool need_vtd_wa(struct drm_device *dev)
2196 #ifdef CONFIG_INTEL_IOMMU
2197 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2203 static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2207 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2208 return ALIGN(height, tile_height);
2212 intel_pin_and_fence_fb_obj(struct drm_device *dev,
2213 struct drm_i915_gem_object *obj,
2214 struct intel_engine_cs *pipelined)
2216 struct drm_i915_private *dev_priv = dev->dev_private;
2220 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2222 switch (obj->tiling_mode) {
2223 case I915_TILING_NONE:
2224 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2225 alignment = 128 * 1024;
2226 else if (INTEL_INFO(dev)->gen >= 4)
2227 alignment = 4 * 1024;
2229 alignment = 64 * 1024;
2232 /* pin() will align the object as required by fence */
2236 WARN(1, "Y tiled bo slipped through, driver bug!\n");
2242 /* Note that the w/a also requires 64 PTE of padding following the
2243 * bo. We currently fill all unused PTE with the shadow page and so
2244 * we should always have valid PTE following the scanout preventing
2247 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2248 alignment = 256 * 1024;
2251 * Global gtt pte registers are special registers which actually forward
2252 * writes to a chunk of system memory. Which means that there is no risk
2253 * that the register values disappear as soon as we call
2254 * intel_runtime_pm_put(), so it is correct to wrap only the
2255 * pin/unpin/fence and not more.
2257 intel_runtime_pm_get(dev_priv);
2259 dev_priv->mm.interruptible = false;
2260 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
2262 goto err_interruptible;
2264 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2265 * fence, whereas 965+ only requires a fence if using
2266 * framebuffer compression. For simplicity, we always install
2267 * a fence as the cost is not that onerous.
2269 ret = i915_gem_object_get_fence(obj);
2273 i915_gem_object_pin_fence(obj);
2275 dev_priv->mm.interruptible = true;
2276 intel_runtime_pm_put(dev_priv);
2280 i915_gem_object_unpin_from_display_plane(obj);
2282 dev_priv->mm.interruptible = true;
2283 intel_runtime_pm_put(dev_priv);
2287 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2289 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2291 i915_gem_object_unpin_fence(obj);
2292 i915_gem_object_unpin_from_display_plane(obj);
2295 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2296 * is assumed to be a power-of-two. */
2297 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2298 unsigned int tiling_mode,
2302 if (tiling_mode != I915_TILING_NONE) {
2303 unsigned int tile_rows, tiles;
2308 tiles = *x / (512/cpp);
2311 return tile_rows * pitch * 8 + tiles * 4096;
2313 unsigned int offset;
2315 offset = *y * pitch + *x * cpp;
2317 *x = (offset & 4095) / cpp;
2318 return offset & -4096;
2322 int intel_format_to_fourcc(int format)
2325 case DISPPLANE_8BPP:
2326 return DRM_FORMAT_C8;
2327 case DISPPLANE_BGRX555:
2328 return DRM_FORMAT_XRGB1555;
2329 case DISPPLANE_BGRX565:
2330 return DRM_FORMAT_RGB565;
2332 case DISPPLANE_BGRX888:
2333 return DRM_FORMAT_XRGB8888;
2334 case DISPPLANE_RGBX888:
2335 return DRM_FORMAT_XBGR8888;
2336 case DISPPLANE_BGRX101010:
2337 return DRM_FORMAT_XRGB2101010;
2338 case DISPPLANE_RGBX101010:
2339 return DRM_FORMAT_XBGR2101010;
2343 static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
2344 struct intel_plane_config *plane_config)
2346 struct drm_device *dev = crtc->base.dev;
2347 struct drm_i915_gem_object *obj = NULL;
2348 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2349 u32 base = plane_config->base;
2351 if (plane_config->size == 0)
2354 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2355 plane_config->size);
2359 if (plane_config->tiled) {
2360 obj->tiling_mode = I915_TILING_X;
2361 obj->stride = crtc->base.primary->fb->pitches[0];
2364 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2365 mode_cmd.width = crtc->base.primary->fb->width;
2366 mode_cmd.height = crtc->base.primary->fb->height;
2367 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
2369 mutex_lock(&dev->struct_mutex);
2371 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
2373 DRM_DEBUG_KMS("intel fb init failed\n");
2377 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
2378 mutex_unlock(&dev->struct_mutex);
2380 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2384 drm_gem_object_unreference(&obj->base);
2385 mutex_unlock(&dev->struct_mutex);
2389 static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2390 struct intel_plane_config *plane_config)
2392 struct drm_device *dev = intel_crtc->base.dev;
2394 struct intel_crtc *i;
2395 struct drm_i915_gem_object *obj;
2397 if (!intel_crtc->base.primary->fb)
2400 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2403 kfree(intel_crtc->base.primary->fb);
2404 intel_crtc->base.primary->fb = NULL;
2407 * Failed to alloc the obj, check to see if we should share
2408 * an fb with another CRTC instead
2410 for_each_crtc(dev, c) {
2411 i = to_intel_crtc(c);
2413 if (c == &intel_crtc->base)
2419 obj = intel_fb_obj(c->primary->fb);
2423 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
2424 drm_framebuffer_reference(c->primary->fb);
2425 intel_crtc->base.primary->fb = c->primary->fb;
2426 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
2432 static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2433 struct drm_framebuffer *fb,
2436 struct drm_device *dev = crtc->dev;
2437 struct drm_i915_private *dev_priv = dev->dev_private;
2438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2439 struct drm_i915_gem_object *obj;
2440 int plane = intel_crtc->plane;
2441 unsigned long linear_offset;
2443 u32 reg = DSPCNTR(plane);
2446 if (!intel_crtc->primary_enabled) {
2448 if (INTEL_INFO(dev)->gen >= 4)
2449 I915_WRITE(DSPSURF(plane), 0);
2451 I915_WRITE(DSPADDR(plane), 0);
2456 obj = intel_fb_obj(fb);
2457 if (WARN_ON(obj == NULL))
2460 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2462 dspcntr = DISPPLANE_GAMMA_ENABLE;
2464 dspcntr |= DISPLAY_PLANE_ENABLE;
2466 if (INTEL_INFO(dev)->gen < 4) {
2467 if (intel_crtc->pipe == PIPE_B)
2468 dspcntr |= DISPPLANE_SEL_PIPE_B;
2470 /* pipesrc and dspsize control the size that is scaled from,
2471 * which should always be the user's requested size.
2473 I915_WRITE(DSPSIZE(plane),
2474 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2475 (intel_crtc->config.pipe_src_w - 1));
2476 I915_WRITE(DSPPOS(plane), 0);
2479 switch (fb->pixel_format) {
2481 dspcntr |= DISPPLANE_8BPP;
2483 case DRM_FORMAT_XRGB1555:
2484 case DRM_FORMAT_ARGB1555:
2485 dspcntr |= DISPPLANE_BGRX555;
2487 case DRM_FORMAT_RGB565:
2488 dspcntr |= DISPPLANE_BGRX565;
2490 case DRM_FORMAT_XRGB8888:
2491 case DRM_FORMAT_ARGB8888:
2492 dspcntr |= DISPPLANE_BGRX888;
2494 case DRM_FORMAT_XBGR8888:
2495 case DRM_FORMAT_ABGR8888:
2496 dspcntr |= DISPPLANE_RGBX888;
2498 case DRM_FORMAT_XRGB2101010:
2499 case DRM_FORMAT_ARGB2101010:
2500 dspcntr |= DISPPLANE_BGRX101010;
2502 case DRM_FORMAT_XBGR2101010:
2503 case DRM_FORMAT_ABGR2101010:
2504 dspcntr |= DISPPLANE_RGBX101010;
2510 if (INTEL_INFO(dev)->gen >= 4 &&
2511 obj->tiling_mode != I915_TILING_NONE)
2512 dspcntr |= DISPPLANE_TILED;
2515 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2517 linear_offset = y * fb->pitches[0] + x * pixel_size;
2519 if (INTEL_INFO(dev)->gen >= 4) {
2520 intel_crtc->dspaddr_offset =
2521 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2524 linear_offset -= intel_crtc->dspaddr_offset;
2526 intel_crtc->dspaddr_offset = linear_offset;
2529 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2530 dspcntr |= DISPPLANE_ROTATE_180;
2532 x += (intel_crtc->config.pipe_src_w - 1);
2533 y += (intel_crtc->config.pipe_src_h - 1);
2535 /* Finding the last pixel of the last line of the display
2536 data and adding to linear_offset*/
2538 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2539 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2542 I915_WRITE(reg, dspcntr);
2544 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2545 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2547 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2548 if (INTEL_INFO(dev)->gen >= 4) {
2549 I915_WRITE(DSPSURF(plane),
2550 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2551 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2552 I915_WRITE(DSPLINOFF(plane), linear_offset);
2554 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2558 static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2559 struct drm_framebuffer *fb,
2562 struct drm_device *dev = crtc->dev;
2563 struct drm_i915_private *dev_priv = dev->dev_private;
2564 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2565 struct drm_i915_gem_object *obj;
2566 int plane = intel_crtc->plane;
2567 unsigned long linear_offset;
2569 u32 reg = DSPCNTR(plane);
2572 if (!intel_crtc->primary_enabled) {
2574 I915_WRITE(DSPSURF(plane), 0);
2579 obj = intel_fb_obj(fb);
2580 if (WARN_ON(obj == NULL))
2583 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2585 dspcntr = DISPPLANE_GAMMA_ENABLE;
2587 dspcntr |= DISPLAY_PLANE_ENABLE;
2589 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2590 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2592 switch (fb->pixel_format) {
2594 dspcntr |= DISPPLANE_8BPP;
2596 case DRM_FORMAT_RGB565:
2597 dspcntr |= DISPPLANE_BGRX565;
2599 case DRM_FORMAT_XRGB8888:
2600 case DRM_FORMAT_ARGB8888:
2601 dspcntr |= DISPPLANE_BGRX888;
2603 case DRM_FORMAT_XBGR8888:
2604 case DRM_FORMAT_ABGR8888:
2605 dspcntr |= DISPPLANE_RGBX888;
2607 case DRM_FORMAT_XRGB2101010:
2608 case DRM_FORMAT_ARGB2101010:
2609 dspcntr |= DISPPLANE_BGRX101010;
2611 case DRM_FORMAT_XBGR2101010:
2612 case DRM_FORMAT_ABGR2101010:
2613 dspcntr |= DISPPLANE_RGBX101010;
2619 if (obj->tiling_mode != I915_TILING_NONE)
2620 dspcntr |= DISPPLANE_TILED;
2622 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
2623 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2625 linear_offset = y * fb->pitches[0] + x * pixel_size;
2626 intel_crtc->dspaddr_offset =
2627 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2630 linear_offset -= intel_crtc->dspaddr_offset;
2631 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2632 dspcntr |= DISPPLANE_ROTATE_180;
2634 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2635 x += (intel_crtc->config.pipe_src_w - 1);
2636 y += (intel_crtc->config.pipe_src_h - 1);
2638 /* Finding the last pixel of the last line of the display
2639 data and adding to linear_offset*/
2641 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2642 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2646 I915_WRITE(reg, dspcntr);
2648 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2649 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2651 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2652 I915_WRITE(DSPSURF(plane),
2653 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2654 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2655 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2657 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2658 I915_WRITE(DSPLINOFF(plane), linear_offset);
2663 /* Assume fb object is pinned & idle & fenced and just update base pointers */
2665 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2666 int x, int y, enum mode_set_atomic state)
2668 struct drm_device *dev = crtc->dev;
2669 struct drm_i915_private *dev_priv = dev->dev_private;
2671 if (dev_priv->display.disable_fbc)
2672 dev_priv->display.disable_fbc(dev);
2673 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
2675 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2680 void intel_display_handle_reset(struct drm_device *dev)
2682 struct drm_i915_private *dev_priv = dev->dev_private;
2683 struct drm_crtc *crtc;
2686 * Flips in the rings have been nuked by the reset,
2687 * so complete all pending flips so that user space
2688 * will get its events and not get stuck.
2690 * Also update the base address of all primary
2691 * planes to the the last fb to make sure we're
2692 * showing the correct fb after a reset.
2694 * Need to make two loops over the crtcs so that we
2695 * don't try to grab a crtc mutex before the
2696 * pending_flip_queue really got woken up.
2699 for_each_crtc(dev, crtc) {
2700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2701 enum plane plane = intel_crtc->plane;
2703 intel_prepare_page_flip(dev, plane);
2704 intel_finish_page_flip_plane(dev, plane);
2707 for_each_crtc(dev, crtc) {
2708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2710 drm_modeset_lock(&crtc->mutex, NULL);
2712 * FIXME: Once we have proper support for primary planes (and
2713 * disabling them without disabling the entire crtc) allow again
2714 * a NULL crtc->primary->fb.
2716 if (intel_crtc->active && crtc->primary->fb)
2717 dev_priv->display.update_primary_plane(crtc,
2721 drm_modeset_unlock(&crtc->mutex);
2726 intel_finish_fb(struct drm_framebuffer *old_fb)
2728 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2729 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2730 bool was_interruptible = dev_priv->mm.interruptible;
2733 /* Big Hammer, we also need to ensure that any pending
2734 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2735 * current scanout is retired before unpinning the old
2738 * This should only fail upon a hung GPU, in which case we
2739 * can safely continue.
2741 dev_priv->mm.interruptible = false;
2742 ret = i915_gem_object_finish_gpu(obj);
2743 dev_priv->mm.interruptible = was_interruptible;
2748 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2750 struct drm_device *dev = crtc->dev;
2751 struct drm_i915_private *dev_priv = dev->dev_private;
2752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2755 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2756 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2759 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2760 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2761 lockmgr(&dev->event_lock, LK_RELEASE);
2767 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2768 struct drm_framebuffer *fb)
2770 struct drm_device *dev = crtc->dev;
2771 struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2773 enum i915_pipe pipe = intel_crtc->pipe;
2774 struct drm_framebuffer *old_fb = crtc->primary->fb;
2775 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2776 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
2779 if (intel_crtc_has_pending_flip(crtc)) {
2780 DRM_ERROR("pipe is still busy with an old pageflip\n");
2786 DRM_ERROR("No FB bound\n");
2790 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
2791 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2792 plane_name(intel_crtc->plane),
2793 INTEL_INFO(dev)->num_pipes);
2797 mutex_lock(&dev->struct_mutex);
2798 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2800 i915_gem_track_fb(old_obj, obj,
2801 INTEL_FRONTBUFFER_PRIMARY(pipe));
2802 mutex_unlock(&dev->struct_mutex);
2804 DRM_ERROR("pin & fence failed\n");
2809 * Update pipe size and adjust fitter if needed: the reason for this is
2810 * that in compute_mode_changes we check the native mode (not the pfit
2811 * mode) to see if we can flip rather than do a full mode set. In the
2812 * fastboot case, we'll flip, but if we don't update the pipesrc and
2813 * pfit state, we'll end up with a big fb scanned out into the wrong
2816 * To fix this properly, we need to hoist the checks up into
2817 * compute_mode_changes (or above), check the actual pfit state and
2818 * whether the platform allows pfit disable with pipe active, and only
2819 * then update the pipesrc and pfit state, even on the flip path.
2821 if (i915.fastboot) {
2822 const struct drm_display_mode *adjusted_mode =
2823 &intel_crtc->config.adjusted_mode;
2825 I915_WRITE(PIPESRC(intel_crtc->pipe),
2826 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2827 (adjusted_mode->crtc_vdisplay - 1));
2828 if (!intel_crtc->config.pch_pfit.enabled &&
2829 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2830 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2831 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2832 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2833 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2835 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2836 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2839 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2841 if (intel_crtc->active)
2842 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2844 crtc->primary->fb = fb;
2849 if (intel_crtc->active && old_fb != fb)
2850 intel_wait_for_vblank(dev, intel_crtc->pipe);
2851 mutex_lock(&dev->struct_mutex);
2852 intel_unpin_fb_obj(old_obj);
2853 mutex_unlock(&dev->struct_mutex);
2856 mutex_lock(&dev->struct_mutex);
2857 intel_update_fbc(dev);
2858 mutex_unlock(&dev->struct_mutex);
2863 static void intel_fdi_normal_train(struct drm_crtc *crtc)
2865 struct drm_device *dev = crtc->dev;
2866 struct drm_i915_private *dev_priv = dev->dev_private;
2867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2868 int pipe = intel_crtc->pipe;
2871 /* enable normal train */
2872 reg = FDI_TX_CTL(pipe);
2873 temp = I915_READ(reg);
2874 if (IS_IVYBRIDGE(dev)) {
2875 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2876 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
2878 temp &= ~FDI_LINK_TRAIN_NONE;
2879 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
2881 I915_WRITE(reg, temp);
2883 reg = FDI_RX_CTL(pipe);
2884 temp = I915_READ(reg);
2885 if (HAS_PCH_CPT(dev)) {
2886 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2887 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2889 temp &= ~FDI_LINK_TRAIN_NONE;
2890 temp |= FDI_LINK_TRAIN_NONE;
2892 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2894 /* wait one idle pattern time */
2898 /* IVB wants error correction enabled */
2899 if (IS_IVYBRIDGE(dev))
2900 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2901 FDI_FE_ERRC_ENABLE);
2904 static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
2906 return crtc->base.enabled && crtc->active &&
2907 crtc->config.has_pch_encoder;
2910 static void ivb_modeset_global_resources(struct drm_device *dev)
2912 struct drm_i915_private *dev_priv = dev->dev_private;
2913 struct intel_crtc *pipe_B_crtc =
2914 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2915 struct intel_crtc *pipe_C_crtc =
2916 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2920 * When everything is off disable fdi C so that we could enable fdi B
2921 * with all lanes. Note that we don't care about enabled pipes without
2922 * an enabled pch encoder.
2924 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2925 !pipe_has_enabled_pch(pipe_C_crtc)) {
2926 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2927 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2929 temp = I915_READ(SOUTH_CHICKEN1);
2930 temp &= ~FDI_BC_BIFURCATION_SELECT;
2931 DRM_DEBUG_KMS("disabling fdi C rx\n");
2932 I915_WRITE(SOUTH_CHICKEN1, temp);
2936 /* The FDI link training functions for ILK/Ibexpeak. */
2937 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2939 struct drm_device *dev = crtc->dev;
2940 struct drm_i915_private *dev_priv = dev->dev_private;
2941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2942 int pipe = intel_crtc->pipe;
2943 u32 reg, temp, tries;
2945 /* FDI needs bits from pipe first */
2946 assert_pipe_enabled(dev_priv, pipe);
2948 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2950 reg = FDI_RX_IMR(pipe);
2951 temp = I915_READ(reg);
2952 temp &= ~FDI_RX_SYMBOL_LOCK;
2953 temp &= ~FDI_RX_BIT_LOCK;
2954 I915_WRITE(reg, temp);
2958 /* enable CPU FDI TX and PCH FDI RX */
2959 reg = FDI_TX_CTL(pipe);
2960 temp = I915_READ(reg);
2961 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2962 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2963 temp &= ~FDI_LINK_TRAIN_NONE;
2964 temp |= FDI_LINK_TRAIN_PATTERN_1;
2965 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2967 reg = FDI_RX_CTL(pipe);
2968 temp = I915_READ(reg);
2969 temp &= ~FDI_LINK_TRAIN_NONE;
2970 temp |= FDI_LINK_TRAIN_PATTERN_1;
2971 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2976 /* Ironlake workaround, enable clock pointer after FDI enable*/
2977 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2978 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2979 FDI_RX_PHASE_SYNC_POINTER_EN);
2981 reg = FDI_RX_IIR(pipe);
2982 for (tries = 0; tries < 5; tries++) {
2983 temp = I915_READ(reg);
2984 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2986 if ((temp & FDI_RX_BIT_LOCK)) {
2987 DRM_DEBUG_KMS("FDI train 1 done.\n");
2988 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2993 DRM_ERROR("FDI train 1 fail!\n");
2996 reg = FDI_TX_CTL(pipe);
2997 temp = I915_READ(reg);
2998 temp &= ~FDI_LINK_TRAIN_NONE;
2999 temp |= FDI_LINK_TRAIN_PATTERN_2;
3000 I915_WRITE(reg, temp);
3002 reg = FDI_RX_CTL(pipe);
3003 temp = I915_READ(reg);
3004 temp &= ~FDI_LINK_TRAIN_NONE;
3005 temp |= FDI_LINK_TRAIN_PATTERN_2;
3006 I915_WRITE(reg, temp);
3011 reg = FDI_RX_IIR(pipe);
3012 for (tries = 0; tries < 5; tries++) {
3013 temp = I915_READ(reg);
3014 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3016 if (temp & FDI_RX_SYMBOL_LOCK) {
3017 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3018 DRM_DEBUG_KMS("FDI train 2 done.\n");
3023 DRM_ERROR("FDI train 2 fail!\n");
3025 DRM_DEBUG_KMS("FDI train done\n");
3029 static const int snb_b_fdi_train_param[] = {
3030 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3031 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3032 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3033 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3036 /* The FDI link training functions for SNB/Cougarpoint. */
3037 static void gen6_fdi_link_train(struct drm_crtc *crtc)
3039 struct drm_device *dev = crtc->dev;
3040 struct drm_i915_private *dev_priv = dev->dev_private;
3041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3042 int pipe = intel_crtc->pipe;
3043 u32 reg, temp, i, retry;
3045 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3047 reg = FDI_RX_IMR(pipe);
3048 temp = I915_READ(reg);
3049 temp &= ~FDI_RX_SYMBOL_LOCK;
3050 temp &= ~FDI_RX_BIT_LOCK;
3051 I915_WRITE(reg, temp);
3056 /* enable CPU FDI TX and PCH FDI RX */
3057 reg = FDI_TX_CTL(pipe);
3058 temp = I915_READ(reg);
3059 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3060 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3061 temp &= ~FDI_LINK_TRAIN_NONE;
3062 temp |= FDI_LINK_TRAIN_PATTERN_1;
3063 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3065 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3066 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3068 I915_WRITE(FDI_RX_MISC(pipe),
3069 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3071 reg = FDI_RX_CTL(pipe);
3072 temp = I915_READ(reg);
3073 if (HAS_PCH_CPT(dev)) {
3074 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3075 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3077 temp &= ~FDI_LINK_TRAIN_NONE;
3078 temp |= FDI_LINK_TRAIN_PATTERN_1;
3080 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3085 for (i = 0; i < 4; i++) {
3086 reg = FDI_TX_CTL(pipe);
3087 temp = I915_READ(reg);
3088 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3089 temp |= snb_b_fdi_train_param[i];
3090 I915_WRITE(reg, temp);
3095 for (retry = 0; retry < 5; retry++) {
3096 reg = FDI_RX_IIR(pipe);
3097 temp = I915_READ(reg);
3098 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3099 if (temp & FDI_RX_BIT_LOCK) {
3100 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3101 DRM_DEBUG_KMS("FDI train 1 done.\n");
3110 DRM_ERROR("FDI train 1 fail!\n");
3113 reg = FDI_TX_CTL(pipe);
3114 temp = I915_READ(reg);
3115 temp &= ~FDI_LINK_TRAIN_NONE;
3116 temp |= FDI_LINK_TRAIN_PATTERN_2;
3118 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3120 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3122 I915_WRITE(reg, temp);
3124 reg = FDI_RX_CTL(pipe);
3125 temp = I915_READ(reg);
3126 if (HAS_PCH_CPT(dev)) {
3127 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3128 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3130 temp &= ~FDI_LINK_TRAIN_NONE;
3131 temp |= FDI_LINK_TRAIN_PATTERN_2;
3133 I915_WRITE(reg, temp);
3138 for (i = 0; i < 4; i++) {
3139 reg = FDI_TX_CTL(pipe);
3140 temp = I915_READ(reg);
3141 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3142 temp |= snb_b_fdi_train_param[i];
3143 I915_WRITE(reg, temp);
3148 for (retry = 0; retry < 5; retry++) {
3149 reg = FDI_RX_IIR(pipe);
3150 temp = I915_READ(reg);
3151 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3152 if (temp & FDI_RX_SYMBOL_LOCK) {
3153 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3154 DRM_DEBUG_KMS("FDI train 2 done.\n");
3163 DRM_ERROR("FDI train 2 fail!\n");
3165 DRM_DEBUG_KMS("FDI train done.\n");
3168 /* Manual link training for Ivy Bridge A0 parts */
3169 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3171 struct drm_device *dev = crtc->dev;
3172 struct drm_i915_private *dev_priv = dev->dev_private;
3173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3174 int pipe = intel_crtc->pipe;
3175 u32 reg, temp, i, j;
3177 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3179 reg = FDI_RX_IMR(pipe);
3180 temp = I915_READ(reg);
3181 temp &= ~FDI_RX_SYMBOL_LOCK;
3182 temp &= ~FDI_RX_BIT_LOCK;
3183 I915_WRITE(reg, temp);
3188 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3189 I915_READ(FDI_RX_IIR(pipe)));
3191 /* Try each vswing and preemphasis setting twice before moving on */
3192 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3193 /* disable first in case we need to retry */
3194 reg = FDI_TX_CTL(pipe);
3195 temp = I915_READ(reg);
3196 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3197 temp &= ~FDI_TX_ENABLE;
3198 I915_WRITE(reg, temp);
3200 reg = FDI_RX_CTL(pipe);
3201 temp = I915_READ(reg);
3202 temp &= ~FDI_LINK_TRAIN_AUTO;
3203 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3204 temp &= ~FDI_RX_ENABLE;
3205 I915_WRITE(reg, temp);
3207 /* enable CPU FDI TX and PCH FDI RX */
3208 reg = FDI_TX_CTL(pipe);
3209 temp = I915_READ(reg);
3210 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3211 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3212 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
3213 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3214 temp |= snb_b_fdi_train_param[j/2];
3215 temp |= FDI_COMPOSITE_SYNC;
3216 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3218 I915_WRITE(FDI_RX_MISC(pipe),
3219 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3221 reg = FDI_RX_CTL(pipe);
3222 temp = I915_READ(reg);
3223 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3224 temp |= FDI_COMPOSITE_SYNC;
3225 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3228 udelay(1); /* should be 0.5us */
3230 for (i = 0; i < 4; i++) {
3231 reg = FDI_RX_IIR(pipe);
3232 temp = I915_READ(reg);
3233 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3235 if (temp & FDI_RX_BIT_LOCK ||
3236 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3237 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3238 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3242 udelay(1); /* should be 0.5us */
3245 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3250 reg = FDI_TX_CTL(pipe);
3251 temp = I915_READ(reg);
3252 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3253 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3254 I915_WRITE(reg, temp);
3256 reg = FDI_RX_CTL(pipe);
3257 temp = I915_READ(reg);
3258 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3259 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3260 I915_WRITE(reg, temp);
3263 udelay(2); /* should be 1.5us */
3265 for (i = 0; i < 4; i++) {
3266 reg = FDI_RX_IIR(pipe);
3267 temp = I915_READ(reg);
3268 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3270 if (temp & FDI_RX_SYMBOL_LOCK ||
3271 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3272 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3273 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3277 udelay(2); /* should be 1.5us */
3280 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
3284 DRM_DEBUG_KMS("FDI train done.\n");
3287 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
3289 struct drm_device *dev = intel_crtc->base.dev;
3290 struct drm_i915_private *dev_priv = dev->dev_private;
3291 int pipe = intel_crtc->pipe;
3295 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
3296 reg = FDI_RX_CTL(pipe);
3297 temp = I915_READ(reg);
3298 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3299 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3300 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3301 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3306 /* Switch from Rawclk to PCDclk */
3307 temp = I915_READ(reg);
3308 I915_WRITE(reg, temp | FDI_PCDCLK);
3313 /* Enable CPU FDI TX PLL, always on for Ironlake */
3314 reg = FDI_TX_CTL(pipe);
3315 temp = I915_READ(reg);
3316 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3317 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
3324 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3326 struct drm_device *dev = intel_crtc->base.dev;
3327 struct drm_i915_private *dev_priv = dev->dev_private;
3328 int pipe = intel_crtc->pipe;
3331 /* Switch from PCDclk to Rawclk */
3332 reg = FDI_RX_CTL(pipe);
3333 temp = I915_READ(reg);
3334 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3336 /* Disable CPU FDI TX PLL */
3337 reg = FDI_TX_CTL(pipe);
3338 temp = I915_READ(reg);
3339 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3344 reg = FDI_RX_CTL(pipe);
3345 temp = I915_READ(reg);
3346 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3348 /* Wait for the clocks to turn off. */
3353 static void ironlake_fdi_disable(struct drm_crtc *crtc)
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
3357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3358 int pipe = intel_crtc->pipe;
3361 /* disable CPU FDI tx and PCH FDI rx */
3362 reg = FDI_TX_CTL(pipe);
3363 temp = I915_READ(reg);
3364 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3367 reg = FDI_RX_CTL(pipe);
3368 temp = I915_READ(reg);
3369 temp &= ~(0x7 << 16);
3370 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3371 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3376 /* Ironlake workaround, disable clock pointer after downing FDI */
3377 if (HAS_PCH_IBX(dev))
3378 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3380 /* still set train pattern 1 */
3381 reg = FDI_TX_CTL(pipe);
3382 temp = I915_READ(reg);
3383 temp &= ~FDI_LINK_TRAIN_NONE;
3384 temp |= FDI_LINK_TRAIN_PATTERN_1;
3385 I915_WRITE(reg, temp);
3387 reg = FDI_RX_CTL(pipe);
3388 temp = I915_READ(reg);
3389 if (HAS_PCH_CPT(dev)) {
3390 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3391 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3393 temp &= ~FDI_LINK_TRAIN_NONE;
3394 temp |= FDI_LINK_TRAIN_PATTERN_1;
3396 /* BPC in FDI rx is consistent with that in PIPECONF */
3397 temp &= ~(0x07 << 16);
3398 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3399 I915_WRITE(reg, temp);
3405 bool intel_has_pending_fb_unpin(struct drm_device *dev)
3407 struct intel_crtc *crtc;
3409 /* Note that we don't need to be called with mode_config.lock here
3410 * as our list of CRTC objects is static for the lifetime of the
3411 * device and so cannot disappear as we iterate. Similarly, we can
3412 * happily treat the predicates as racy, atomic checks as userspace
3413 * cannot claim and pin a new fb without at least acquring the
3414 * struct_mutex and so serialising with us.
3416 for_each_intel_crtc(dev, crtc) {
3417 if (atomic_read(&crtc->unpin_work_count) == 0)
3420 if (crtc->unpin_work)
3421 intel_wait_for_vblank(dev, crtc->pipe);
3429 static void page_flip_completed(struct intel_crtc *intel_crtc)
3431 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3432 struct intel_unpin_work *work = intel_crtc->unpin_work;
3434 /* ensure that the unpin work is consistent wrt ->pending. */
3436 intel_crtc->unpin_work = NULL;
3439 drm_send_vblank_event(intel_crtc->base.dev,
3443 drm_crtc_vblank_put(&intel_crtc->base);
3445 wake_up_all(&dev_priv->pending_flip_queue);
3446 queue_work(dev_priv->wq, &work->work);
3448 trace_i915_flip_complete(intel_crtc->plane,
3449 work->pending_flip_obj);
3452 void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3454 struct drm_device *dev = crtc->dev;
3455 struct drm_i915_private *dev_priv = dev->dev_private;
3457 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3458 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3459 !intel_crtc_has_pending_flip(crtc),
3461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3463 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3464 if (intel_crtc->unpin_work) {
3465 WARN_ONCE(1, "Removing stuck page flip\n");
3466 page_flip_completed(intel_crtc);
3468 lockmgr(&dev->event_lock, LK_RELEASE);
3471 if (crtc->primary->fb) {
3472 mutex_lock(&dev->struct_mutex);
3473 intel_finish_fb(crtc->primary->fb);
3474 mutex_unlock(&dev->struct_mutex);
3478 /* Program iCLKIP clock to the desired frequency */
3479 static void lpt_program_iclkip(struct drm_crtc *crtc)
3481 struct drm_device *dev = crtc->dev;
3482 struct drm_i915_private *dev_priv = dev->dev_private;
3483 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
3484 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3487 mutex_lock(&dev_priv->dpio_lock);
3489 /* It is necessary to ungate the pixclk gate prior to programming
3490 * the divisors, and gate it back when it is done.
3492 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3494 /* Disable SSCCTL */
3495 intel_sbi_write(dev_priv, SBI_SSCCTL6,
3496 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3500 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3501 if (clock == 20000) {
3506 /* The iCLK virtual clock root frequency is in MHz,
3507 * but the adjusted_mode->crtc_clock in in KHz. To get the
3508 * divisors, it is necessary to divide one by another, so we
3509 * convert the virtual clock precision to KHz here for higher
3512 u32 iclk_virtual_root_freq = 172800 * 1000;
3513 u32 iclk_pi_range = 64;
3514 u32 desired_divisor, msb_divisor_value, pi_value;
3516 desired_divisor = (iclk_virtual_root_freq / clock);
3517 msb_divisor_value = desired_divisor / iclk_pi_range;
3518 pi_value = desired_divisor % iclk_pi_range;
3521 divsel = msb_divisor_value - 2;
3522 phaseinc = pi_value;
3525 /* This should not happen with any sane values */
3526 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3527 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3528 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3529 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3531 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3538 /* Program SSCDIVINTPHASE6 */
3539 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3540 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3541 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3542 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3543 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3544 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3545 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3546 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
3548 /* Program SSCAUXDIV */
3549 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3550 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3551 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3552 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
3554 /* Enable modulator and associated divider */
3555 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3556 temp &= ~SBI_SSCCTL_DISABLE;
3557 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3559 /* Wait for initialization time */
3562 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3564 mutex_unlock(&dev_priv->dpio_lock);
3567 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3568 enum i915_pipe pch_transcoder)
3570 struct drm_device *dev = crtc->base.dev;
3571 struct drm_i915_private *dev_priv = dev->dev_private;
3572 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3574 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3575 I915_READ(HTOTAL(cpu_transcoder)));
3576 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3577 I915_READ(HBLANK(cpu_transcoder)));
3578 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3579 I915_READ(HSYNC(cpu_transcoder)));
3581 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3582 I915_READ(VTOTAL(cpu_transcoder)));
3583 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3584 I915_READ(VBLANK(cpu_transcoder)));
3585 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3586 I915_READ(VSYNC(cpu_transcoder)));
3587 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3588 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3591 static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3593 struct drm_i915_private *dev_priv = dev->dev_private;
3596 temp = I915_READ(SOUTH_CHICKEN1);
3597 if (temp & FDI_BC_BIFURCATION_SELECT)
3600 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3601 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3603 temp |= FDI_BC_BIFURCATION_SELECT;
3604 DRM_DEBUG_KMS("enabling fdi C rx\n");
3605 I915_WRITE(SOUTH_CHICKEN1, temp);
3606 POSTING_READ(SOUTH_CHICKEN1);
3609 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3611 struct drm_device *dev = intel_crtc->base.dev;
3612 struct drm_i915_private *dev_priv = dev->dev_private;
3614 switch (intel_crtc->pipe) {
3618 if (intel_crtc->config.fdi_lanes > 2)
3619 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3621 cpt_enable_fdi_bc_bifurcation(dev);
3625 cpt_enable_fdi_bc_bifurcation(dev);
3634 * Enable PCH resources required for PCH ports:
3636 * - FDI training & RX/TX
3637 * - update transcoder timings
3638 * - DP transcoding bits
3641 static void ironlake_pch_enable(struct drm_crtc *crtc)
3643 struct drm_device *dev = crtc->dev;
3644 struct drm_i915_private *dev_priv = dev->dev_private;
3645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3646 int pipe = intel_crtc->pipe;
3649 assert_pch_transcoder_disabled(dev_priv, pipe);
3651 if (IS_IVYBRIDGE(dev))
3652 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3654 /* Write the TU size bits before fdi link training, so that error
3655 * detection works. */
3656 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3657 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3659 /* For PCH output, training FDI link */
3660 dev_priv->display.fdi_link_train(crtc);
3662 /* We need to program the right clock selection before writing the pixel
3663 * mutliplier into the DPLL. */
3664 if (HAS_PCH_CPT(dev)) {
3667 temp = I915_READ(PCH_DPLL_SEL);
3668 temp |= TRANS_DPLL_ENABLE(pipe);
3669 sel = TRANS_DPLLB_SEL(pipe);
3670 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
3674 I915_WRITE(PCH_DPLL_SEL, temp);
3677 /* XXX: pch pll's can be enabled any time before we enable the PCH
3678 * transcoder, and we actually should do this to not upset any PCH
3679 * transcoder that already use the clock when we share it.
3681 * Note that enable_shared_dpll tries to do the right thing, but
3682 * get_shared_dpll unconditionally resets the pll - we need that to have
3683 * the right LVDS enable sequence. */
3684 intel_enable_shared_dpll(intel_crtc);
3686 /* set transcoder timing, panel must allow it */
3687 assert_panel_unlocked(dev_priv, pipe);
3688 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
3690 intel_fdi_normal_train(crtc);
3692 /* For PCH DP, enable TRANS_DP_CTL */
3693 if (HAS_PCH_CPT(dev) &&
3694 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3695 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3696 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3697 reg = TRANS_DP_CTL(pipe);
3698 temp = I915_READ(reg);
3699 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3700 TRANS_DP_SYNC_MASK |
3702 temp |= (TRANS_DP_OUTPUT_ENABLE |
3703 TRANS_DP_ENH_FRAMING);
3704 temp |= bpc << 9; /* same format but at 11:9 */
3706 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
3707 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3708 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
3709 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3711 switch (intel_trans_dp_port_sel(crtc)) {
3713 temp |= TRANS_DP_PORT_SEL_B;
3716 temp |= TRANS_DP_PORT_SEL_C;
3719 temp |= TRANS_DP_PORT_SEL_D;
3725 I915_WRITE(reg, temp);
3728 ironlake_enable_pch_transcoder(dev_priv, pipe);
3731 static void lpt_pch_enable(struct drm_crtc *crtc)
3733 struct drm_device *dev = crtc->dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3736 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
3738 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
3740 lpt_program_iclkip(crtc);
3742 /* Set transcoder timing. */
3743 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
3745 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3748 void intel_put_shared_dpll(struct intel_crtc *crtc)
3750 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3755 if (pll->refcount == 0) {
3756 WARN(1, "bad %s refcount\n", pll->name);
3760 if (--pll->refcount == 0) {
3762 WARN_ON(pll->active);
3765 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
3768 struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
3770 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3771 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3772 enum intel_dpll_id i;
3775 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3776 crtc->base.base.id, pll->name);
3777 intel_put_shared_dpll(crtc);
3780 if (HAS_PCH_IBX(dev_priv->dev)) {
3781 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3782 i = (enum intel_dpll_id) crtc->pipe;
3783 pll = &dev_priv->shared_dplls[i];
3785 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3786 crtc->base.base.id, pll->name);
3788 WARN_ON(pll->refcount);
3793 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3794 pll = &dev_priv->shared_dplls[i];
3796 /* Only want to check enabled timings first */
3797 if (pll->refcount == 0)
3800 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3801 sizeof(pll->hw_state)) == 0) {
3802 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
3804 pll->name, pll->refcount, pll->active);
3810 /* Ok no matching timings, maybe there's a free one? */
3811 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3812 pll = &dev_priv->shared_dplls[i];
3813 if (pll->refcount == 0) {
3814 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3815 crtc->base.base.id, pll->name);
3823 if (pll->refcount == 0)
3824 pll->hw_state = crtc->config.dpll_hw_state;
3826 crtc->config.shared_dpll = i;
3827 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3828 pipe_name(crtc->pipe));
3835 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
3837 struct drm_i915_private *dev_priv = dev->dev_private;
3838 int dslreg = PIPEDSL(pipe);
3841 temp = I915_READ(dslreg);
3843 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3844 if (wait_for(I915_READ(dslreg) != temp, 5))
3845 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
3849 static void ironlake_pfit_enable(struct intel_crtc *crtc)
3851 struct drm_device *dev = crtc->base.dev;
3852 struct drm_i915_private *dev_priv = dev->dev_private;
3853 int pipe = crtc->pipe;
3855 if (crtc->config.pch_pfit.enabled) {
3856 /* Force use of hard-coded filter coefficients
3857 * as some pre-programmed values are broken,
3860 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3861 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3862 PF_PIPE_SEL_IVB(pipe));
3864 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3865 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3866 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3870 static void intel_enable_planes(struct drm_crtc *crtc)
3872 struct drm_device *dev = crtc->dev;
3873 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3874 struct drm_plane *plane;
3875 struct intel_plane *intel_plane;
3877 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3878 intel_plane = to_intel_plane(plane);
3879 if (intel_plane->pipe == pipe)
3880 intel_plane_restore(&intel_plane->base);
3884 static void intel_disable_planes(struct drm_crtc *crtc)
3886 struct drm_device *dev = crtc->dev;
3887 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
3888 struct drm_plane *plane;
3889 struct intel_plane *intel_plane;
3891 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3892 intel_plane = to_intel_plane(plane);
3893 if (intel_plane->pipe == pipe)
3894 intel_plane_disable(&intel_plane->base);
3898 void hsw_enable_ips(struct intel_crtc *crtc)
3900 struct drm_device *dev = crtc->base.dev;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
3903 if (!crtc->config.ips_enabled)
3906 /* We can only enable IPS after we enable a plane and wait for a vblank */
3907 intel_wait_for_vblank(dev, crtc->pipe);
3909 assert_plane_enabled(dev_priv, crtc->plane);
3910 if (IS_BROADWELL(dev)) {
3911 mutex_lock(&dev_priv->rps.hw_lock);
3912 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3913 mutex_unlock(&dev_priv->rps.hw_lock);
3914 /* Quoting Art Runyan: "its not safe to expect any particular
3915 * value in IPS_CTL bit 31 after enabling IPS through the
3916 * mailbox." Moreover, the mailbox may return a bogus state,
3917 * so we need to just enable it and continue on.
3920 I915_WRITE(IPS_CTL, IPS_ENABLE);
3921 /* The bit only becomes 1 in the next vblank, so this wait here
3922 * is essentially intel_wait_for_vblank. If we don't have this
3923 * and don't wait for vblanks until the end of crtc_enable, then
3924 * the HW state readout code will complain that the expected
3925 * IPS_CTL value is not the one we read. */
3926 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3927 DRM_ERROR("Timed out waiting for IPS enable\n");
3931 void hsw_disable_ips(struct intel_crtc *crtc)
3933 struct drm_device *dev = crtc->base.dev;
3934 struct drm_i915_private *dev_priv = dev->dev_private;
3936 if (!crtc->config.ips_enabled)
3939 assert_plane_enabled(dev_priv, crtc->plane);
3940 if (IS_BROADWELL(dev)) {
3941 mutex_lock(&dev_priv->rps.hw_lock);
3942 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3943 mutex_unlock(&dev_priv->rps.hw_lock);
3944 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3945 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3946 DRM_ERROR("Timed out waiting for IPS disable\n");
3948 I915_WRITE(IPS_CTL, 0);
3949 POSTING_READ(IPS_CTL);
3952 /* We need to wait for a vblank before we can disable the plane. */
3953 intel_wait_for_vblank(dev, crtc->pipe);
3956 /** Loads the palette/gamma unit for the CRTC with the prepared values */
3957 static void intel_crtc_load_lut(struct drm_crtc *crtc)
3959 struct drm_device *dev = crtc->dev;
3960 struct drm_i915_private *dev_priv = dev->dev_private;
3961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3962 enum i915_pipe pipe = intel_crtc->pipe;
3963 int palreg = PALETTE(pipe);
3965 bool reenable_ips = false;
3967 /* The clocks have to be on to load the palette. */
3968 if (!crtc->enabled || !intel_crtc->active)
3971 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3972 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3973 assert_dsi_pll_enabled(dev_priv);
3975 assert_pll_enabled(dev_priv, pipe);
3978 /* use legacy palette for Ironlake */
3979 if (!HAS_GMCH_DISPLAY(dev))
3980 palreg = LGC_PALETTE(pipe);
3982 /* Workaround : Do not read or write the pipe palette/gamma data while
3983 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3985 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
3986 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3987 GAMMA_MODE_MODE_SPLIT)) {
3988 hsw_disable_ips(intel_crtc);
3989 reenable_ips = true;
3992 for (i = 0; i < 256; i++) {
3993 I915_WRITE(palreg + 4 * i,
3994 (intel_crtc->lut_r[i] << 16) |
3995 (intel_crtc->lut_g[i] << 8) |
3996 intel_crtc->lut_b[i]);
4000 hsw_enable_ips(intel_crtc);
4003 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4005 if (!enable && intel_crtc->overlay) {
4006 struct drm_device *dev = intel_crtc->base.dev;
4007 struct drm_i915_private *dev_priv = dev->dev_private;
4009 mutex_lock(&dev->struct_mutex);
4010 dev_priv->mm.interruptible = false;
4011 (void) intel_overlay_switch_off(intel_crtc->overlay);
4012 dev_priv->mm.interruptible = true;
4013 mutex_unlock(&dev->struct_mutex);
4016 /* Let userspace switch the overlay on again. In most cases userspace
4017 * has to recompute where to put it anyway.
4021 static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4023 struct drm_device *dev = crtc->dev;
4024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4025 int pipe = intel_crtc->pipe;
4027 assert_vblank_disabled(crtc);
4029 drm_vblank_on(dev, pipe);
4031 intel_enable_primary_hw_plane(crtc->primary, crtc);
4032 intel_enable_planes(crtc);
4033 intel_crtc_update_cursor(crtc, true);
4034 intel_crtc_dpms_overlay(intel_crtc, true);
4036 hsw_enable_ips(intel_crtc);
4038 mutex_lock(&dev->struct_mutex);
4039 intel_update_fbc(dev);
4040 mutex_unlock(&dev->struct_mutex);
4043 * FIXME: Once we grow proper nuclear flip support out of this we need
4044 * to compute the mask of flip planes precisely. For the time being
4045 * consider this a flip from a NULL plane.
4047 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4050 static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4052 struct drm_device *dev = crtc->dev;
4053 struct drm_i915_private *dev_priv = dev->dev_private;
4054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
4056 int plane = intel_crtc->plane;
4058 intel_crtc_wait_for_pending_flips(crtc);
4060 if (dev_priv->fbc.plane == plane)
4061 intel_disable_fbc(dev);
4063 hsw_disable_ips(intel_crtc);
4065 intel_crtc_dpms_overlay(intel_crtc, false);
4066 intel_crtc_update_cursor(crtc, false);
4067 intel_disable_planes(crtc);
4068 intel_disable_primary_hw_plane(crtc->primary, crtc);
4071 * FIXME: Once we grow proper nuclear flip support out of this we need
4072 * to compute the mask of flip planes precisely. For the time being
4073 * consider this a flip to a NULL plane.
4075 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4077 drm_vblank_off(dev, pipe);
4079 assert_vblank_disabled(crtc);
4082 static void ironlake_crtc_enable(struct drm_crtc *crtc)
4084 struct drm_device *dev = crtc->dev;
4085 struct drm_i915_private *dev_priv = dev->dev_private;
4086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4087 struct intel_encoder *encoder;
4088 int pipe = intel_crtc->pipe;
4090 WARN_ON(!crtc->enabled);
4092 if (intel_crtc->active)
4095 if (intel_crtc->config.has_pch_encoder)
4096 intel_prepare_shared_dpll(intel_crtc);
4098 if (intel_crtc->config.has_dp_encoder)
4099 intel_dp_set_m_n(intel_crtc);
4101 intel_set_pipe_timings(intel_crtc);
4103 if (intel_crtc->config.has_pch_encoder) {
4104 intel_cpu_transcoder_set_m_n(intel_crtc,
4105 &intel_crtc->config.fdi_m_n, NULL);
4108 ironlake_set_pipeconf(crtc);
4110 intel_crtc->active = true;
4112 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4113 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4115 for_each_encoder_on_crtc(dev, crtc, encoder)
4116 if (encoder->pre_enable)
4117 encoder->pre_enable(encoder);
4119 if (intel_crtc->config.has_pch_encoder) {
4120 /* Note: FDI PLL enabling _must_ be done before we enable the
4121 * cpu pipes, hence this is separate from all the other fdi/pch
4123 ironlake_fdi_pll_enable(intel_crtc);
4125 assert_fdi_tx_disabled(dev_priv, pipe);
4126 assert_fdi_rx_disabled(dev_priv, pipe);
4129 ironlake_pfit_enable(intel_crtc);
4132 * On ILK+ LUT must be loaded before the pipe is running but with
4135 intel_crtc_load_lut(crtc);
4137 intel_update_watermarks(crtc);
4138 intel_enable_pipe(intel_crtc);
4140 if (intel_crtc->config.has_pch_encoder)
4141 ironlake_pch_enable(crtc);
4143 for_each_encoder_on_crtc(dev, crtc, encoder)
4144 encoder->enable(encoder);
4146 if (HAS_PCH_CPT(dev))
4147 cpt_verify_modeset(dev, intel_crtc->pipe);
4149 intel_crtc_enable_planes(crtc);
4152 /* IPS only exists on ULT machines and is tied to pipe A. */
4153 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4155 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
4159 * This implements the workaround described in the "notes" section of the mode
4160 * set sequence documentation. When going from no pipes or single pipe to
4161 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4162 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4164 static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4166 struct drm_device *dev = crtc->base.dev;
4167 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4169 /* We want to get the other_active_crtc only if there's only 1 other
4171 for_each_intel_crtc(dev, crtc_it) {
4172 if (!crtc_it->active || crtc_it == crtc)
4175 if (other_active_crtc)
4178 other_active_crtc = crtc_it;
4180 if (!other_active_crtc)
4183 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4184 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4187 static void haswell_crtc_enable(struct drm_crtc *crtc)
4189 struct drm_device *dev = crtc->dev;
4190 struct drm_i915_private *dev_priv = dev->dev_private;
4191 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4192 struct intel_encoder *encoder;
4193 int pipe = intel_crtc->pipe;
4195 WARN_ON(!crtc->enabled);
4197 if (intel_crtc->active)
4200 if (intel_crtc_to_shared_dpll(intel_crtc))
4201 intel_enable_shared_dpll(intel_crtc);
4203 if (intel_crtc->config.has_dp_encoder)
4204 intel_dp_set_m_n(intel_crtc);
4206 intel_set_pipe_timings(intel_crtc);
4208 if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
4209 I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
4210 intel_crtc->config.pixel_multiplier - 1);
4213 if (intel_crtc->config.has_pch_encoder) {
4214 intel_cpu_transcoder_set_m_n(intel_crtc,
4215 &intel_crtc->config.fdi_m_n, NULL);
4218 haswell_set_pipeconf(crtc);
4220 intel_set_pipe_csc(crtc);
4222 intel_crtc->active = true;
4224 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4225 for_each_encoder_on_crtc(dev, crtc, encoder)
4226 if (encoder->pre_enable)
4227 encoder->pre_enable(encoder);
4229 if (intel_crtc->config.has_pch_encoder) {
4230 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4231 dev_priv->display.fdi_link_train(crtc);
4234 intel_ddi_enable_pipe_clock(intel_crtc);
4236 ironlake_pfit_enable(intel_crtc);
4239 * On ILK+ LUT must be loaded before the pipe is running but with
4242 intel_crtc_load_lut(crtc);
4244 intel_ddi_set_pipe_settings(crtc);
4245 intel_ddi_enable_transcoder_func(crtc);
4247 intel_update_watermarks(crtc);
4248 intel_enable_pipe(intel_crtc);
4250 if (intel_crtc->config.has_pch_encoder)
4251 lpt_pch_enable(crtc);
4253 for_each_encoder_on_crtc(dev, crtc, encoder) {
4254 encoder->enable(encoder);
4255 intel_opregion_notify_encoder(encoder, true);
4258 /* If we change the relative order between pipe/planes enabling, we need
4259 * to change the workaround. */
4260 haswell_mode_set_planes_workaround(intel_crtc);
4261 intel_crtc_enable_planes(crtc);
4264 static void ironlake_pfit_disable(struct intel_crtc *crtc)
4266 struct drm_device *dev = crtc->base.dev;
4267 struct drm_i915_private *dev_priv = dev->dev_private;
4268 int pipe = crtc->pipe;
4270 /* To avoid upsetting the power well on haswell only disable the pfit if
4271 * it's in use. The hw state code will make sure we get this right. */
4272 if (crtc->config.pch_pfit.enabled) {
4273 I915_WRITE(PF_CTL(pipe), 0);
4274 I915_WRITE(PF_WIN_POS(pipe), 0);
4275 I915_WRITE(PF_WIN_SZ(pipe), 0);
4279 static void ironlake_crtc_disable(struct drm_crtc *crtc)
4281 struct drm_device *dev = crtc->dev;
4282 struct drm_i915_private *dev_priv = dev->dev_private;
4283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4284 struct intel_encoder *encoder;
4285 int pipe = intel_crtc->pipe;
4288 if (!intel_crtc->active)
4291 intel_crtc_disable_planes(crtc);
4293 for_each_encoder_on_crtc(dev, crtc, encoder)
4294 encoder->disable(encoder);
4296 if (intel_crtc->config.has_pch_encoder)
4297 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4299 intel_disable_pipe(intel_crtc);
4301 ironlake_pfit_disable(intel_crtc);
4303 for_each_encoder_on_crtc(dev, crtc, encoder)
4304 if (encoder->post_disable)
4305 encoder->post_disable(encoder);
4307 if (intel_crtc->config.has_pch_encoder) {
4308 ironlake_fdi_disable(crtc);
4310 ironlake_disable_pch_transcoder(dev_priv, pipe);
4312 if (HAS_PCH_CPT(dev)) {
4313 /* disable TRANS_DP_CTL */
4314 reg = TRANS_DP_CTL(pipe);
4315 temp = I915_READ(reg);
4316 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4317 TRANS_DP_PORT_SEL_MASK);
4318 temp |= TRANS_DP_PORT_SEL_NONE;
4319 I915_WRITE(reg, temp);
4321 /* disable DPLL_SEL */
4322 temp = I915_READ(PCH_DPLL_SEL);
4323 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
4324 I915_WRITE(PCH_DPLL_SEL, temp);
4327 /* disable PCH DPLL */
4328 intel_disable_shared_dpll(intel_crtc);
4330 ironlake_fdi_pll_disable(intel_crtc);
4333 intel_crtc->active = false;
4334 intel_update_watermarks(crtc);
4336 mutex_lock(&dev->struct_mutex);
4337 intel_update_fbc(dev);
4338 mutex_unlock(&dev->struct_mutex);
4341 static void haswell_crtc_disable(struct drm_crtc *crtc)
4343 struct drm_device *dev = crtc->dev;
4344 struct drm_i915_private *dev_priv = dev->dev_private;
4345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4346 struct intel_encoder *encoder;
4347 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
4349 if (!intel_crtc->active)
4352 intel_crtc_disable_planes(crtc);
4354 for_each_encoder_on_crtc(dev, crtc, encoder) {
4355 intel_opregion_notify_encoder(encoder, false);
4356 encoder->disable(encoder);
4359 if (intel_crtc->config.has_pch_encoder)
4360 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4361 intel_disable_pipe(intel_crtc);
4363 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4365 ironlake_pfit_disable(intel_crtc);
4367 intel_ddi_disable_pipe_clock(intel_crtc);
4369 if (intel_crtc->config.has_pch_encoder) {
4370 lpt_disable_pch_transcoder(dev_priv);
4371 intel_ddi_fdi_disable(crtc);
4374 for_each_encoder_on_crtc(dev, crtc, encoder)
4375 if (encoder->post_disable)
4376 encoder->post_disable(encoder);
4378 intel_crtc->active = false;
4379 intel_update_watermarks(crtc);
4381 mutex_lock(&dev->struct_mutex);
4382 intel_update_fbc(dev);
4383 mutex_unlock(&dev->struct_mutex);
4385 if (intel_crtc_to_shared_dpll(intel_crtc))
4386 intel_disable_shared_dpll(intel_crtc);
4389 static void ironlake_crtc_off(struct drm_crtc *crtc)
4391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4392 intel_put_shared_dpll(intel_crtc);
4396 static void i9xx_pfit_enable(struct intel_crtc *crtc)
4398 struct drm_device *dev = crtc->base.dev;
4399 struct drm_i915_private *dev_priv = dev->dev_private;
4400 struct intel_crtc_config *pipe_config = &crtc->config;
4402 if (!crtc->config.gmch_pfit.control)
4406 * The panel fitter should only be adjusted whilst the pipe is disabled,
4407 * according to register description and PRM.
4409 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4410 assert_pipe_disabled(dev_priv, crtc->pipe);
4412 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4413 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
4415 /* Border color in case we don't scale up to the full screen. Black by
4416 * default, change to something else for debugging. */
4417 I915_WRITE(BCLRPAT(crtc->pipe), 0);
4420 static enum intel_display_power_domain port_to_power_domain(enum port port)
4424 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4426 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4428 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4430 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4433 return POWER_DOMAIN_PORT_OTHER;
4437 enum intel_display_power_domain
4438 intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4440 struct drm_device *dev = intel_encoder->base.dev;
4441 struct intel_digital_port *intel_dig_port;
4443 switch (intel_encoder->type) {
4444 case INTEL_OUTPUT_UNKNOWN:
4445 /* Only DDI platforms should ever use this output type */
4446 WARN_ON_ONCE(!HAS_DDI(dev));
4447 case INTEL_OUTPUT_DISPLAYPORT:
4448 case INTEL_OUTPUT_HDMI:
4449 case INTEL_OUTPUT_EDP:
4450 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4451 return port_to_power_domain(intel_dig_port->port);
4452 case INTEL_OUTPUT_ANALOG:
4453 return POWER_DOMAIN_PORT_CRT;
4454 case INTEL_OUTPUT_DSI:
4455 return POWER_DOMAIN_PORT_DSI;
4457 return POWER_DOMAIN_PORT_OTHER;
4461 static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4463 struct drm_device *dev = crtc->dev;
4464 struct intel_encoder *intel_encoder;
4465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4466 enum i915_pipe pipe = intel_crtc->pipe;
4468 enum transcoder transcoder;
4470 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4472 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4473 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4474 if (intel_crtc->config.pch_pfit.enabled ||
4475 intel_crtc->config.pch_pfit.force_thru)
4476 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4478 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4479 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4484 void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4487 if (dev_priv->power_domains.init_power_on == enable)
4491 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4493 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4495 dev_priv->power_domains.init_power_on = enable;
4498 static void modeset_update_crtc_power_domains(struct drm_device *dev)
4500 struct drm_i915_private *dev_priv = dev->dev_private;
4501 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4502 struct intel_crtc *crtc;
4505 * First get all needed power domains, then put all unneeded, to avoid
4506 * any unnecessary toggling of the power wells.
4508 for_each_intel_crtc(dev, crtc) {
4509 enum intel_display_power_domain domain;
4511 if (!crtc->base.enabled)
4514 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
4516 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4517 intel_display_power_get(dev_priv, domain);
4520 for_each_intel_crtc(dev, crtc) {
4521 enum intel_display_power_domain domain;
4523 for_each_power_domain(domain, crtc->enabled_power_domains)
4524 intel_display_power_put(dev_priv, domain);
4526 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4529 intel_display_set_init_power(dev_priv, false);
4532 /* returns HPLL frequency in kHz */
4533 static int valleyview_get_vco(struct drm_i915_private *dev_priv)
4535 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
4537 /* Obtain SKU information */
4538 mutex_lock(&dev_priv->dpio_lock);
4539 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4540 CCK_FUSE_HPLL_FREQ_MASK;
4541 mutex_unlock(&dev_priv->dpio_lock);
4543 return vco_freq[hpll_freq] * 1000;
4546 static void vlv_update_cdclk(struct drm_device *dev)
4548 struct drm_i915_private *dev_priv = dev->dev_private;
4550 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4551 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4552 dev_priv->vlv_cdclk_freq);
4555 * Program the gmbus_freq based on the cdclk frequency.
4556 * BSpec erroneously claims we should aim for 4MHz, but
4557 * in fact 1MHz is the correct frequency.
4559 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
4562 /* Adjust CDclk dividers to allow high res or save power if possible */
4563 static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4565 struct drm_i915_private *dev_priv = dev->dev_private;
4568 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4570 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
4572 else if (cdclk == 266667)
4577 mutex_lock(&dev_priv->rps.hw_lock);
4578 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4579 val &= ~DSPFREQGUAR_MASK;
4580 val |= (cmd << DSPFREQGUAR_SHIFT);
4581 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4582 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4583 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4585 DRM_ERROR("timed out waiting for CDclk change\n");
4587 mutex_unlock(&dev_priv->rps.hw_lock);
4589 if (cdclk == 400000) {
4592 vco = valleyview_get_vco(dev_priv);
4593 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
4595 mutex_lock(&dev_priv->dpio_lock);
4596 /* adjust cdclk divider */
4597 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4598 val &= ~DISPLAY_FREQUENCY_VALUES;
4600 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4602 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4603 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4605 DRM_ERROR("timed out waiting for CDclk change\n");
4606 mutex_unlock(&dev_priv->dpio_lock);
4609 mutex_lock(&dev_priv->dpio_lock);
4610 /* adjust self-refresh exit latency value */
4611 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4615 * For high bandwidth configs, we set a higher latency in the bunit
4616 * so that the core display fetch happens in time to avoid underruns.
4618 if (cdclk == 400000)
4619 val |= 4500 / 250; /* 4.5 usec */
4621 val |= 3000 / 250; /* 3.0 usec */
4622 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4623 mutex_unlock(&dev_priv->dpio_lock);
4625 vlv_update_cdclk(dev);
4628 static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4630 struct drm_i915_private *dev_priv = dev->dev_private;
4633 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4654 mutex_lock(&dev_priv->rps.hw_lock);
4655 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4656 val &= ~DSPFREQGUAR_MASK_CHV;
4657 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4658 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4659 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4660 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4662 DRM_ERROR("timed out waiting for CDclk change\n");
4664 mutex_unlock(&dev_priv->rps.hw_lock);
4666 vlv_update_cdclk(dev);
4669 static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4672 int vco = valleyview_get_vco(dev_priv);
4673 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4675 /* FIXME: Punit isn't quite ready yet */
4676 if (IS_CHERRYVIEW(dev_priv->dev))
4680 * Really only a few cases to deal with, as only 4 CDclks are supported:
4683 * 320/333MHz (depends on HPLL freq)
4685 * So we check to see whether we're above 90% of the lower bin and
4688 * We seem to get an unstable or solid color picture at 200MHz.
4689 * Not sure what's wrong. For now use 200MHz only when all pipes
4692 if (max_pixclk > freq_320*9/10)
4694 else if (max_pixclk > 266667*9/10)
4696 else if (max_pixclk > 0)
4702 /* compute the max pixel clock for new configuration */
4703 static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
4705 struct drm_device *dev = dev_priv->dev;
4706 struct intel_crtc *intel_crtc;
4709 for_each_intel_crtc(dev, intel_crtc) {
4710 if (intel_crtc->new_enabled)
4711 max_pixclk = max(max_pixclk,
4712 intel_crtc->new_config->adjusted_mode.crtc_clock);
4718 static void valleyview_modeset_global_pipes(struct drm_device *dev,
4719 unsigned *prepare_pipes)
4721 struct drm_i915_private *dev_priv = dev->dev_private;
4722 struct intel_crtc *intel_crtc;
4723 int max_pixclk = intel_mode_max_pixclk(dev_priv);
4725 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4726 dev_priv->vlv_cdclk_freq)
4729 /* disable/enable all currently active pipes while we change cdclk */
4730 for_each_intel_crtc(dev, intel_crtc)
4731 if (intel_crtc->base.enabled)
4732 *prepare_pipes |= (1 << intel_crtc->pipe);
4735 static void valleyview_modeset_global_resources(struct drm_device *dev)
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 int max_pixclk = intel_mode_max_pixclk(dev_priv);
4739 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4741 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4742 if (IS_CHERRYVIEW(dev))
4743 cherryview_set_cdclk(dev, req_cdclk);
4745 valleyview_set_cdclk(dev, req_cdclk);
4748 modeset_update_crtc_power_domains(dev);
4751 static void valleyview_crtc_enable(struct drm_crtc *crtc)
4753 struct drm_device *dev = crtc->dev;
4754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4755 struct intel_encoder *encoder;
4756 int pipe = intel_crtc->pipe;
4759 WARN_ON(!crtc->enabled);
4761 if (intel_crtc->active)
4764 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4767 if (IS_CHERRYVIEW(dev))
4768 chv_prepare_pll(intel_crtc);
4770 vlv_prepare_pll(intel_crtc);
4773 if (intel_crtc->config.has_dp_encoder)
4774 intel_dp_set_m_n(intel_crtc);
4776 intel_set_pipe_timings(intel_crtc);
4778 i9xx_set_pipeconf(intel_crtc);
4780 intel_crtc->active = true;
4782 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4784 for_each_encoder_on_crtc(dev, crtc, encoder)
4785 if (encoder->pre_pll_enable)
4786 encoder->pre_pll_enable(encoder);
4789 if (IS_CHERRYVIEW(dev))
4790 chv_enable_pll(intel_crtc);
4792 vlv_enable_pll(intel_crtc);
4795 for_each_encoder_on_crtc(dev, crtc, encoder)
4796 if (encoder->pre_enable)
4797 encoder->pre_enable(encoder);
4799 i9xx_pfit_enable(intel_crtc);
4801 intel_crtc_load_lut(crtc);
4803 intel_update_watermarks(crtc);
4804 intel_enable_pipe(intel_crtc);
4806 for_each_encoder_on_crtc(dev, crtc, encoder)
4807 encoder->enable(encoder);
4809 intel_crtc_enable_planes(crtc);
4811 /* Underruns don't raise interrupts, so check manually. */
4812 i9xx_check_fifo_underruns(dev);
4815 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4817 struct drm_device *dev = crtc->base.dev;
4818 struct drm_i915_private *dev_priv = dev->dev_private;
4820 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4821 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4824 static void i9xx_crtc_enable(struct drm_crtc *crtc)
4826 struct drm_device *dev = crtc->dev;
4827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4828 struct intel_encoder *encoder;
4829 int pipe = intel_crtc->pipe;
4831 WARN_ON(!crtc->enabled);
4833 if (intel_crtc->active)
4836 i9xx_set_pll_dividers(intel_crtc);
4838 if (intel_crtc->config.has_dp_encoder)
4839 intel_dp_set_m_n(intel_crtc);
4841 intel_set_pipe_timings(intel_crtc);
4843 i9xx_set_pipeconf(intel_crtc);
4845 intel_crtc->active = true;
4848 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4850 for_each_encoder_on_crtc(dev, crtc, encoder)
4851 if (encoder->pre_enable)
4852 encoder->pre_enable(encoder);
4854 i9xx_enable_pll(intel_crtc);
4856 i9xx_pfit_enable(intel_crtc);
4858 intel_crtc_load_lut(crtc);
4860 intel_update_watermarks(crtc);
4861 intel_enable_pipe(intel_crtc);
4863 for_each_encoder_on_crtc(dev, crtc, encoder)
4864 encoder->enable(encoder);
4866 intel_crtc_enable_planes(crtc);
4869 * Gen2 reports pipe underruns whenever all planes are disabled.
4870 * So don't enable underrun reporting before at least some planes
4872 * FIXME: Need to fix the logic to work when we turn off all planes
4873 * but leave the pipe running.
4876 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4878 /* Underruns don't raise interrupts, so check manually. */
4879 i9xx_check_fifo_underruns(dev);
4882 static void i9xx_pfit_disable(struct intel_crtc *crtc)
4884 struct drm_device *dev = crtc->base.dev;
4885 struct drm_i915_private *dev_priv = dev->dev_private;
4887 if (!crtc->config.gmch_pfit.control)
4890 assert_pipe_disabled(dev_priv, crtc->pipe);
4892 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4893 I915_READ(PFIT_CONTROL));
4894 I915_WRITE(PFIT_CONTROL, 0);
4897 static void i9xx_crtc_disable(struct drm_crtc *crtc)
4899 struct drm_device *dev = crtc->dev;
4900 struct drm_i915_private *dev_priv = dev->dev_private;
4901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4902 struct intel_encoder *encoder;
4903 int pipe = intel_crtc->pipe;
4905 if (!intel_crtc->active)
4909 * Gen2 reports pipe underruns whenever all planes are disabled.
4910 * So diasble underrun reporting before all the planes get disabled.
4911 * FIXME: Need to fix the logic to work when we turn off all planes
4912 * but leave the pipe running.
4915 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4918 * Vblank time updates from the shadow to live plane control register
4919 * are blocked if the memory self-refresh mode is active at that
4920 * moment. So to make sure the plane gets truly disabled, disable
4921 * first the self-refresh mode. The self-refresh enable bit in turn
4922 * will be checked/applied by the HW only at the next frame start
4923 * event which is after the vblank start event, so we need to have a
4924 * wait-for-vblank between disabling the plane and the pipe.
4926 intel_set_memory_cxsr(dev_priv, false);
4927 intel_crtc_disable_planes(crtc);
4929 for_each_encoder_on_crtc(dev, crtc, encoder)
4930 encoder->disable(encoder);
4933 * On gen2 planes are double buffered but the pipe isn't, so we must
4934 * wait for planes to fully turn off before disabling the pipe.
4935 * We also need to wait on all gmch platforms because of the
4936 * self-refresh mode constraint explained above.
4938 intel_wait_for_vblank(dev, pipe);
4940 intel_disable_pipe(intel_crtc);
4942 i9xx_pfit_disable(intel_crtc);
4944 for_each_encoder_on_crtc(dev, crtc, encoder)
4945 if (encoder->post_disable)
4946 encoder->post_disable(encoder);
4948 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4949 if (IS_CHERRYVIEW(dev))
4950 chv_disable_pll(dev_priv, pipe);
4951 else if (IS_VALLEYVIEW(dev))
4952 vlv_disable_pll(dev_priv, pipe);
4954 i9xx_disable_pll(intel_crtc);
4958 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4960 intel_crtc->active = false;
4961 intel_update_watermarks(crtc);
4963 mutex_lock(&dev->struct_mutex);
4964 intel_update_fbc(dev);
4965 mutex_unlock(&dev->struct_mutex);
4968 static void i9xx_crtc_off(struct drm_crtc *crtc)
4972 static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4975 struct drm_device *dev = crtc->dev;
4976 struct drm_i915_master_private *master_priv = dev->dev_private;
4977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4978 int pipe = intel_crtc->pipe;
4981 if (!dev->primary->master)
4984 master_priv = dev->primary->master->driver_priv;
4986 if (!master_priv->sarea_priv)
4991 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4992 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4995 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4996 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4999 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
5004 /* Master function to enable/disable CRTC and corresponding power wells */
5005 void intel_crtc_control(struct drm_crtc *crtc, bool enable)
5007 struct drm_device *dev = crtc->dev;
5008 struct drm_i915_private *dev_priv = dev->dev_private;
5009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5010 enum intel_display_power_domain domain;
5011 unsigned long domains;
5014 if (!intel_crtc->active) {
5015 domains = get_crtc_power_domains(crtc);
5016 for_each_power_domain(domain, domains)
5017 intel_display_power_get(dev_priv, domain);
5018 intel_crtc->enabled_power_domains = domains;
5020 dev_priv->display.crtc_enable(crtc);
5023 if (intel_crtc->active) {
5024 dev_priv->display.crtc_disable(crtc);
5026 domains = intel_crtc->enabled_power_domains;
5027 for_each_power_domain(domain, domains)
5028 intel_display_power_put(dev_priv, domain);
5029 intel_crtc->enabled_power_domains = 0;
5035 * Sets the power management mode of the pipe and plane.
5037 void intel_crtc_update_dpms(struct drm_crtc *crtc)
5039 struct drm_device *dev = crtc->dev;
5040 struct intel_encoder *intel_encoder;
5041 bool enable = false;
5043 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5044 enable |= intel_encoder->connectors_active;
5046 intel_crtc_control(crtc, enable);
5048 intel_crtc_update_sarea(crtc, enable);
5051 static void intel_crtc_disable(struct drm_crtc *crtc)
5053 struct drm_device *dev = crtc->dev;
5054 struct drm_connector *connector;
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
5057 enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
5059 /* crtc should still be enabled when we disable it. */
5060 WARN_ON(!crtc->enabled);
5062 dev_priv->display.crtc_disable(crtc);
5063 intel_crtc_update_sarea(crtc, false);
5064 dev_priv->display.off(crtc);
5066 if (crtc->primary->fb) {
5067 mutex_lock(&dev->struct_mutex);
5068 intel_unpin_fb_obj(old_obj);
5069 i915_gem_track_fb(old_obj, NULL,
5070 INTEL_FRONTBUFFER_PRIMARY(pipe));
5071 mutex_unlock(&dev->struct_mutex);
5072 crtc->primary->fb = NULL;
5075 /* Update computed state. */
5076 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5077 if (!connector->encoder || !connector->encoder->crtc)
5080 if (connector->encoder->crtc != crtc)
5083 connector->dpms = DRM_MODE_DPMS_OFF;
5084 to_intel_encoder(connector->encoder)->connectors_active = false;
5088 void intel_encoder_destroy(struct drm_encoder *encoder)
5090 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5092 drm_encoder_cleanup(encoder);
5093 kfree(intel_encoder);
5096 /* Simple dpms helper for encoders with just one connector, no cloning and only
5097 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5098 * state of the entire output pipe. */
5099 static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
5101 if (mode == DRM_MODE_DPMS_ON) {
5102 encoder->connectors_active = true;
5104 intel_crtc_update_dpms(encoder->base.crtc);
5106 encoder->connectors_active = false;
5108 intel_crtc_update_dpms(encoder->base.crtc);
5112 /* Cross check the actual hw state with our own modeset state tracking (and it's
5113 * internal consistency). */
5114 static void intel_connector_check_state(struct intel_connector *connector)
5116 if (connector->get_hw_state(connector)) {
5117 struct intel_encoder *encoder = connector->encoder;
5118 struct drm_crtc *crtc;
5119 bool encoder_enabled;
5120 enum i915_pipe pipe;
5122 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5123 connector->base.base.id,
5124 connector->base.name);
5126 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5127 "wrong connector dpms state\n");
5128 WARN(connector->base.encoder != &encoder->base,
5129 "active connector not linked to encoder\n");
5132 WARN(!encoder->connectors_active,
5133 "encoder->connectors_active not set\n");
5135 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5136 WARN(!encoder_enabled, "encoder not enabled\n");
5137 if (WARN_ON(!encoder->base.crtc))
5140 crtc = encoder->base.crtc;
5142 WARN(!crtc->enabled, "crtc not enabled\n");
5143 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5144 WARN(pipe != to_intel_crtc(crtc)->pipe,
5145 "encoder active on the wrong pipe\n");
5150 /* Even simpler default implementation, if there's really no special case to
5152 void intel_connector_dpms(struct drm_connector *connector, int mode)
5154 /* All the simple cases only support two dpms states. */
5155 if (mode != DRM_MODE_DPMS_ON)
5156 mode = DRM_MODE_DPMS_OFF;
5158 if (mode == connector->dpms)
5161 connector->dpms = mode;
5163 /* Only need to change hw state when actually enabled */
5164 if (connector->encoder)
5165 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
5167 intel_modeset_check_state(connector->dev);
5170 /* Simple connector->get_hw_state implementation for encoders that support only
5171 * one connector and no cloning and hence the encoder state determines the state
5172 * of the connector. */
5173 bool intel_connector_get_hw_state(struct intel_connector *connector)
5175 enum i915_pipe pipe = 0;
5176 struct intel_encoder *encoder = connector->encoder;
5178 return encoder->get_hw_state(encoder, &pipe);
5181 static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum i915_pipe pipe,
5182 struct intel_crtc_config *pipe_config)
5184 struct drm_i915_private *dev_priv = dev->dev_private;
5185 struct intel_crtc *pipe_B_crtc =
5186 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5188 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5189 pipe_name(pipe), pipe_config->fdi_lanes);
5190 if (pipe_config->fdi_lanes > 4) {
5191 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5192 pipe_name(pipe), pipe_config->fdi_lanes);
5196 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
5197 if (pipe_config->fdi_lanes > 2) {
5198 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5199 pipe_config->fdi_lanes);
5206 if (INTEL_INFO(dev)->num_pipes == 2)
5209 /* Ivybridge 3 pipe is really complicated */
5214 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5215 pipe_config->fdi_lanes > 2) {
5216 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5217 pipe_name(pipe), pipe_config->fdi_lanes);
5222 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
5223 pipe_B_crtc->config.fdi_lanes <= 2) {
5224 if (pipe_config->fdi_lanes > 2) {
5225 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5226 pipe_name(pipe), pipe_config->fdi_lanes);
5230 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5240 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5241 struct intel_crtc_config *pipe_config)
5243 struct drm_device *dev = intel_crtc->base.dev;
5244 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
5245 int lane, link_bw, fdi_dotclock;
5246 bool setup_ok, needs_recompute = false;
5249 /* FDI is a binary signal running at ~2.7GHz, encoding
5250 * each output octet as 10 bits. The actual frequency
5251 * is stored as a divider into a 100MHz clock, and the
5252 * mode pixel clock is stored in units of 1KHz.
5253 * Hence the bw of each lane in terms of the mode signal
5256 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5258 fdi_dotclock = adjusted_mode->crtc_clock;
5260 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
5261 pipe_config->pipe_bpp);
5263 pipe_config->fdi_lanes = lane;
5265 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
5266 link_bw, &pipe_config->fdi_m_n);
5268 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5269 intel_crtc->pipe, pipe_config);
5270 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5271 pipe_config->pipe_bpp -= 2*3;
5272 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5273 pipe_config->pipe_bpp);
5274 needs_recompute = true;
5275 pipe_config->bw_constrained = true;
5280 if (needs_recompute)
5283 return setup_ok ? 0 : -EINVAL;
5286 static void hsw_compute_ips_config(struct intel_crtc *crtc,
5287 struct intel_crtc_config *pipe_config)
5289 pipe_config->ips_enabled = i915.enable_ips &&
5290 hsw_crtc_supports_ips(crtc) &&
5291 pipe_config->pipe_bpp <= 24;
5294 static int intel_crtc_compute_config(struct intel_crtc *crtc,
5295 struct intel_crtc_config *pipe_config)
5297 struct drm_device *dev = crtc->base.dev;
5298 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
5300 /* FIXME should check pixel clock limits on all platforms */
5301 if (INTEL_INFO(dev)->gen < 4) {
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5304 dev_priv->display.get_display_clock_speed(dev);
5307 * Enable pixel doubling when the dot clock
5308 * is > 90% of the (display) core speed.
5310 * GDG double wide on either pipe,
5311 * otherwise pipe A only.
5313 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
5314 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
5316 pipe_config->double_wide = true;
5319 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
5324 * Pipe horizontal size must be even in:
5326 * - LVDS dual channel mode
5327 * - Double wide pipe
5329 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5330 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5331 pipe_config->pipe_src_w &= ~1;
5333 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5334 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
5336 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5337 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
5340 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5341 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
5342 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5343 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5345 pipe_config->pipe_bpp = 8*3;
5349 hsw_compute_ips_config(crtc, pipe_config);
5352 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5353 * old clock survives for now.
5355 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
5356 pipe_config->shared_dpll = crtc->config.shared_dpll;
5358 if (pipe_config->has_pch_encoder)
5359 return ironlake_fdi_compute_config(crtc, pipe_config);
5364 static int valleyview_get_display_clock_speed(struct drm_device *dev)
5366 struct drm_i915_private *dev_priv = dev->dev_private;
5367 int vco = valleyview_get_vco(dev_priv);
5371 /* FIXME: Punit isn't quite ready yet */
5372 if (IS_CHERRYVIEW(dev))
5375 mutex_lock(&dev_priv->dpio_lock);
5376 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5377 mutex_unlock(&dev_priv->dpio_lock);
5379 divider = val & DISPLAY_FREQUENCY_VALUES;
5381 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5382 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5383 "cdclk change in progress\n");
5385 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
5388 static int i945_get_display_clock_speed(struct drm_device *dev)
5393 static int i915_get_display_clock_speed(struct drm_device *dev)
5398 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5403 static int pnv_get_display_clock_speed(struct drm_device *dev)
5407 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5409 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5410 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5412 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5414 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5416 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5419 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5420 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5422 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5427 static int i915gm_get_display_clock_speed(struct drm_device *dev)
5431 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5433 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
5436 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5437 case GC_DISPLAY_CLOCK_333_MHZ:
5440 case GC_DISPLAY_CLOCK_190_200_MHZ:
5446 static int i865_get_display_clock_speed(struct drm_device *dev)
5451 static int i855_get_display_clock_speed(struct drm_device *dev)
5454 /* Assume that the hardware is in the high speed state. This
5455 * should be the default.
5457 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5458 case GC_CLOCK_133_200:
5459 case GC_CLOCK_100_200:
5461 case GC_CLOCK_166_250:
5463 case GC_CLOCK_100_133:
5467 /* Shouldn't happen */
5471 static int i830_get_display_clock_speed(struct drm_device *dev)
5477 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
5479 while (*num > DATA_LINK_M_N_MASK ||
5480 *den > DATA_LINK_M_N_MASK) {
5486 static void compute_m_n(unsigned int m, unsigned int n,
5487 uint32_t *ret_m, uint32_t *ret_n)
5489 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5490 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5491 intel_reduce_m_n_ratio(ret_m, ret_n);
5495 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5496 int pixel_clock, int link_clock,
5497 struct intel_link_m_n *m_n)
5501 compute_m_n(bits_per_pixel * pixel_clock,
5502 link_clock * nlanes * 8,
5503 &m_n->gmch_m, &m_n->gmch_n);
5505 compute_m_n(pixel_clock, link_clock,
5506 &m_n->link_m, &m_n->link_n);
5509 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5511 if (i915.panel_use_ssc >= 0)
5512 return i915.panel_use_ssc != 0;
5513 return dev_priv->vbt.lvds_use_ssc
5514 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
5517 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5519 struct drm_device *dev = crtc->dev;
5520 struct drm_i915_private *dev_priv = dev->dev_private;
5523 if (IS_VALLEYVIEW(dev)) {
5525 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5526 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5527 refclk = dev_priv->vbt.lvds_ssc_freq;
5528 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
5529 } else if (!IS_GEN2(dev)) {
5538 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
5540 return (1 << dpll->n) << 16 | dpll->m2;
5543 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5545 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
5548 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
5549 intel_clock_t *reduced_clock)
5551 struct drm_device *dev = crtc->base.dev;
5554 if (IS_PINEVIEW(dev)) {
5555 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
5557 fp2 = pnv_dpll_compute_fp(reduced_clock);
5559 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
5561 fp2 = i9xx_dpll_compute_fp(reduced_clock);
5564 crtc->config.dpll_hw_state.fp0 = fp;
5566 crtc->lowfreq_avail = false;
5567 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5568 reduced_clock && i915.powersave) {
5569 crtc->config.dpll_hw_state.fp1 = fp2;
5570 crtc->lowfreq_avail = true;
5572 crtc->config.dpll_hw_state.fp1 = fp;
5576 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum i915_pipe
5582 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5583 * and set it to a reasonable value instead.
5585 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
5586 reg_val &= 0xffffff00;
5587 reg_val |= 0x00000030;
5588 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
5590 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
5591 reg_val &= 0x8cffffff;
5592 reg_val = 0x8c000000;
5593 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
5595 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
5596 reg_val &= 0xffffff00;
5597 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
5599 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
5600 reg_val &= 0x00ffffff;
5601 reg_val |= 0xb0000000;
5602 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
5605 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5606 struct intel_link_m_n *m_n)
5608 struct drm_device *dev = crtc->base.dev;
5609 struct drm_i915_private *dev_priv = dev->dev_private;
5610 int pipe = crtc->pipe;
5612 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5613 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5614 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5615 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
5618 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5619 struct intel_link_m_n *m_n,
5620 struct intel_link_m_n *m2_n2)
5622 struct drm_device *dev = crtc->base.dev;
5623 struct drm_i915_private *dev_priv = dev->dev_private;
5624 int pipe = crtc->pipe;
5625 enum transcoder transcoder = crtc->config.cpu_transcoder;
5627 if (INTEL_INFO(dev)->gen >= 5) {
5628 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5629 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5630 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5631 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5632 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5633 * for gen < 8) and if DRRS is supported (to make sure the
5634 * registers are not unnecessarily accessed).
5636 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5637 crtc->config.has_drrs) {
5638 I915_WRITE(PIPE_DATA_M2(transcoder),
5639 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5640 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5641 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5642 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5645 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5646 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5647 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5648 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
5652 void intel_dp_set_m_n(struct intel_crtc *crtc)
5654 if (crtc->config.has_pch_encoder)
5655 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5657 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5658 &crtc->config.dp_m2_n2);
5661 static void vlv_update_pll(struct intel_crtc *crtc)
5666 * Enable DPIO clock input. We should never disable the reference
5667 * clock for pipe B, since VGA hotplug / manual detection depends
5670 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5671 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5672 /* We should never disable this, set it here for state tracking */
5673 if (crtc->pipe == PIPE_B)
5674 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5675 dpll |= DPLL_VCO_ENABLE;
5676 crtc->config.dpll_hw_state.dpll = dpll;
5678 dpll_md = (crtc->config.pixel_multiplier - 1)
5679 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5680 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5683 static void vlv_prepare_pll(struct intel_crtc *crtc)
5685 struct drm_device *dev = crtc->base.dev;
5686 struct drm_i915_private *dev_priv = dev->dev_private;
5687 int pipe = crtc->pipe;
5689 u32 bestn, bestm1, bestm2, bestp1, bestp2;
5690 u32 coreclk, reg_val;
5692 mutex_lock(&dev_priv->dpio_lock);
5694 bestn = crtc->config.dpll.n;
5695 bestm1 = crtc->config.dpll.m1;
5696 bestm2 = crtc->config.dpll.m2;
5697 bestp1 = crtc->config.dpll.p1;
5698 bestp2 = crtc->config.dpll.p2;
5700 /* See eDP HDMI DPIO driver vbios notes doc */
5702 /* PLL B needs special handling */
5704 vlv_pllb_recal_opamp(dev_priv, pipe);
5706 /* Set up Tx target for periodic Rcomp update */
5707 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
5709 /* Disable target IRef on PLL */
5710 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
5711 reg_val &= 0x00ffffff;
5712 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
5714 /* Disable fast lock */
5715 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
5717 /* Set idtafcrecal before PLL is enabled */
5718 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5719 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5720 mdiv |= ((bestn << DPIO_N_SHIFT));
5721 mdiv |= (1 << DPIO_K_SHIFT);
5724 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5725 * but we don't support that).
5726 * Note: don't use the DAC post divider as it seems unstable.
5728 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
5729 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
5731 mdiv |= DPIO_ENABLE_CALIBRATION;
5732 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
5734 /* Set HBR and RBR LPF coefficients */
5735 if (crtc->config.port_clock == 162000 ||
5736 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
5737 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
5738 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
5741 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
5744 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5745 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5746 /* Use SSC source */
5748 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5751 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5753 } else { /* HDMI or VGA */
5754 /* Use bend source */
5756 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5759 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
5763 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
5764 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5765 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5766 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5767 coreclk |= 0x01000000;
5768 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
5770 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
5771 mutex_unlock(&dev_priv->dpio_lock);
5774 static void chv_update_pll(struct intel_crtc *crtc)
5776 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5777 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5779 if (crtc->pipe != PIPE_A)
5780 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5782 crtc->config.dpll_hw_state.dpll_md =
5783 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5786 static void chv_prepare_pll(struct intel_crtc *crtc)
5788 struct drm_device *dev = crtc->base.dev;
5789 struct drm_i915_private *dev_priv = dev->dev_private;
5790 int pipe = crtc->pipe;
5791 int dpll_reg = DPLL(crtc->pipe);
5792 enum dpio_channel port = vlv_pipe_to_channel(pipe);
5793 u32 loopfilter, intcoeff;
5794 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5797 bestn = crtc->config.dpll.n;
5798 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5799 bestm1 = crtc->config.dpll.m1;
5800 bestm2 = crtc->config.dpll.m2 >> 22;
5801 bestp1 = crtc->config.dpll.p1;
5802 bestp2 = crtc->config.dpll.p2;
5805 * Enable Refclk and SSC
5807 I915_WRITE(dpll_reg,
5808 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5810 mutex_lock(&dev_priv->dpio_lock);
5812 /* p1 and p2 divider */
5813 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5814 5 << DPIO_CHV_S1_DIV_SHIFT |
5815 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5816 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5817 1 << DPIO_CHV_K_DIV_SHIFT);
5819 /* Feedback post-divider - m2 */
5820 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5822 /* Feedback refclk divider - n and m1 */
5823 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5824 DPIO_CHV_M1_DIV_BY_2 |
5825 1 << DPIO_CHV_N_DIV_SHIFT);
5827 /* M2 fraction division */
5828 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5830 /* M2 fraction division enable */
5831 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5832 DPIO_CHV_FRAC_DIV_EN |
5833 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5836 refclk = i9xx_get_refclk(&crtc->base, 0);
5837 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5838 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5839 if (refclk == 100000)
5841 else if (refclk == 38400)
5845 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5846 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5849 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5850 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5853 mutex_unlock(&dev_priv->dpio_lock);
5856 static void i9xx_update_pll(struct intel_crtc *crtc,
5857 intel_clock_t *reduced_clock,
5860 struct drm_device *dev = crtc->base.dev;
5861 struct drm_i915_private *dev_priv = dev->dev_private;
5864 struct dpll *clock = &crtc->config.dpll;
5866 i9xx_update_pll_dividers(crtc, reduced_clock);
5868 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5869 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
5871 dpll = DPLL_VGA_MODE_DIS;
5873 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
5874 dpll |= DPLLB_MODE_LVDS;
5876 dpll |= DPLLB_MODE_DAC_SERIAL;
5878 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5879 dpll |= (crtc->config.pixel_multiplier - 1)
5880 << SDVO_MULTIPLIER_SHIFT_HIRES;
5884 dpll |= DPLL_SDVO_HIGH_SPEED;
5886 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
5887 dpll |= DPLL_SDVO_HIGH_SPEED;
5889 /* compute bitmask from p1 value */
5890 if (IS_PINEVIEW(dev))
5891 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5893 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5894 if (IS_G4X(dev) && reduced_clock)
5895 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5897 switch (clock->p2) {
5899 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5902 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5905 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5908 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5911 if (INTEL_INFO(dev)->gen >= 4)
5912 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5914 if (crtc->config.sdvo_tv_clock)
5915 dpll |= PLL_REF_INPUT_TVCLKINBC;
5916 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5917 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5918 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5920 dpll |= PLL_REF_INPUT_DREFCLK;
5922 dpll |= DPLL_VCO_ENABLE;
5923 crtc->config.dpll_hw_state.dpll = dpll;
5925 if (INTEL_INFO(dev)->gen >= 4) {
5926 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5927 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5928 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5932 static void i8xx_update_pll(struct intel_crtc *crtc,
5933 intel_clock_t *reduced_clock,
5936 struct drm_device *dev = crtc->base.dev;
5937 struct drm_i915_private *dev_priv = dev->dev_private;
5939 struct dpll *clock = &crtc->config.dpll;
5941 i9xx_update_pll_dividers(crtc, reduced_clock);
5943 dpll = DPLL_VGA_MODE_DIS;
5945 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
5946 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5949 dpll |= PLL_P1_DIVIDE_BY_TWO;
5951 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5953 dpll |= PLL_P2_DIVIDE_BY_4;
5956 if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5957 dpll |= DPLL_DVO_2X_MODE;
5959 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5960 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5961 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5963 dpll |= PLL_REF_INPUT_DREFCLK;
5965 dpll |= DPLL_VCO_ENABLE;
5966 crtc->config.dpll_hw_state.dpll = dpll;
5969 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
5971 struct drm_device *dev = intel_crtc->base.dev;
5972 struct drm_i915_private *dev_priv = dev->dev_private;
5973 enum i915_pipe pipe = intel_crtc->pipe;
5974 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
5975 struct drm_display_mode *adjusted_mode =
5976 &intel_crtc->config.adjusted_mode;
5977 uint32_t crtc_vtotal, crtc_vblank_end;
5980 /* We need to be careful not to changed the adjusted mode, for otherwise
5981 * the hw state checker will get angry at the mismatch. */
5982 crtc_vtotal = adjusted_mode->crtc_vtotal;
5983 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
5985 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5986 /* the chip adds 2 halflines automatically */
5988 crtc_vblank_end -= 1;
5990 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5991 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5993 vsyncshift = adjusted_mode->crtc_hsync_start -
5994 adjusted_mode->crtc_htotal / 2;
5996 vsyncshift += adjusted_mode->crtc_htotal;
5999 if (INTEL_INFO(dev)->gen > 3)
6000 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
6002 I915_WRITE(HTOTAL(cpu_transcoder),
6003 (adjusted_mode->crtc_hdisplay - 1) |
6004 ((adjusted_mode->crtc_htotal - 1) << 16));
6005 I915_WRITE(HBLANK(cpu_transcoder),
6006 (adjusted_mode->crtc_hblank_start - 1) |
6007 ((adjusted_mode->crtc_hblank_end - 1) << 16));
6008 I915_WRITE(HSYNC(cpu_transcoder),
6009 (adjusted_mode->crtc_hsync_start - 1) |
6010 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6012 I915_WRITE(VTOTAL(cpu_transcoder),
6013 (adjusted_mode->crtc_vdisplay - 1) |
6014 ((crtc_vtotal - 1) << 16));
6015 I915_WRITE(VBLANK(cpu_transcoder),
6016 (adjusted_mode->crtc_vblank_start - 1) |
6017 ((crtc_vblank_end - 1) << 16));
6018 I915_WRITE(VSYNC(cpu_transcoder),
6019 (adjusted_mode->crtc_vsync_start - 1) |
6020 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6022 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6023 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6024 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6026 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6027 (pipe == PIPE_B || pipe == PIPE_C))
6028 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6030 /* pipesrc controls the size that is scaled from, which should
6031 * always be the user's requested size.
6033 I915_WRITE(PIPESRC(pipe),
6034 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6035 (intel_crtc->config.pipe_src_h - 1));
6038 static void intel_get_pipe_timings(struct intel_crtc *crtc,
6039 struct intel_crtc_config *pipe_config)
6041 struct drm_device *dev = crtc->base.dev;
6042 struct drm_i915_private *dev_priv = dev->dev_private;
6043 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6046 tmp = I915_READ(HTOTAL(cpu_transcoder));
6047 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6048 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6049 tmp = I915_READ(HBLANK(cpu_transcoder));
6050 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6051 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6052 tmp = I915_READ(HSYNC(cpu_transcoder));
6053 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6054 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6056 tmp = I915_READ(VTOTAL(cpu_transcoder));
6057 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6058 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6059 tmp = I915_READ(VBLANK(cpu_transcoder));
6060 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6061 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6062 tmp = I915_READ(VSYNC(cpu_transcoder));
6063 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6064 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6066 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6067 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6068 pipe_config->adjusted_mode.crtc_vtotal += 1;
6069 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6072 tmp = I915_READ(PIPESRC(crtc->pipe));
6073 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6074 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6076 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6077 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
6080 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6081 struct intel_crtc_config *pipe_config)
6083 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6084 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6085 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6086 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
6088 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6089 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6090 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6091 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
6093 mode->flags = pipe_config->adjusted_mode.flags;
6095 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6096 mode->flags |= pipe_config->adjusted_mode.flags;
6099 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6101 struct drm_device *dev = intel_crtc->base.dev;
6102 struct drm_i915_private *dev_priv = dev->dev_private;
6107 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6108 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6109 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
6111 if (intel_crtc->config.double_wide)
6112 pipeconf |= PIPECONF_DOUBLE_WIDE;
6114 /* only g4x and later have fancy bpc/dither controls */
6115 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6116 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6117 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6118 pipeconf |= PIPECONF_DITHER_EN |
6119 PIPECONF_DITHER_TYPE_SP;
6121 switch (intel_crtc->config.pipe_bpp) {
6123 pipeconf |= PIPECONF_6BPC;
6126 pipeconf |= PIPECONF_8BPC;
6129 pipeconf |= PIPECONF_10BPC;
6132 /* Case prevented by intel_choose_pipe_bpp_dither. */
6137 if (HAS_PIPE_CXSR(dev)) {
6138 if (intel_crtc->lowfreq_avail) {
6139 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6140 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6142 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
6146 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6147 if (INTEL_INFO(dev)->gen < 4 ||
6148 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6149 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6151 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6153 pipeconf |= PIPECONF_PROGRESSIVE;
6155 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6156 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
6158 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6159 POSTING_READ(PIPECONF(intel_crtc->pipe));
6162 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
6164 struct drm_framebuffer *fb)
6166 struct drm_device *dev = crtc->dev;
6167 struct drm_i915_private *dev_priv = dev->dev_private;
6168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6169 int refclk, num_connectors = 0;
6170 intel_clock_t clock, reduced_clock;
6171 bool ok, has_reduced_clock = false;
6172 bool is_lvds = false, is_dsi = false;
6173 struct intel_encoder *encoder;
6174 const intel_limit_t *limit;
6176 for_each_encoder_on_crtc(dev, crtc, encoder) {
6177 switch (encoder->type) {
6178 case INTEL_OUTPUT_LVDS:
6181 case INTEL_OUTPUT_DSI:
6192 if (!intel_crtc->config.clock_set) {
6193 refclk = i9xx_get_refclk(crtc, num_connectors);
6196 * Returns a set of divisors for the desired target clock with
6197 * the given refclk, or FALSE. The returned values represent
6198 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6201 limit = intel_limit(crtc, refclk);
6202 ok = dev_priv->display.find_dpll(limit, crtc,
6203 intel_crtc->config.port_clock,
6204 refclk, NULL, &clock);
6206 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6210 if (is_lvds && dev_priv->lvds_downclock_avail) {
6212 * Ensure we match the reduced clock's P to the target
6213 * clock. If the clocks don't match, we can't switch
6214 * the display clock by using the FP0/FP1. In such case
6215 * we will disable the LVDS downclock feature.
6218 dev_priv->display.find_dpll(limit, crtc,
6219 dev_priv->lvds_downclock,
6223 /* Compat-code for transition, will disappear. */
6224 intel_crtc->config.dpll.n = clock.n;
6225 intel_crtc->config.dpll.m1 = clock.m1;
6226 intel_crtc->config.dpll.m2 = clock.m2;
6227 intel_crtc->config.dpll.p1 = clock.p1;
6228 intel_crtc->config.dpll.p2 = clock.p2;
6232 i8xx_update_pll(intel_crtc,
6233 has_reduced_clock ? &reduced_clock : NULL,
6235 } else if (IS_CHERRYVIEW(dev)) {
6236 chv_update_pll(intel_crtc);
6237 } else if (IS_VALLEYVIEW(dev)) {
6238 vlv_update_pll(intel_crtc);
6240 i9xx_update_pll(intel_crtc,
6241 has_reduced_clock ? &reduced_clock : NULL,
6248 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6249 struct intel_crtc_config *pipe_config)
6251 struct drm_device *dev = crtc->base.dev;
6252 struct drm_i915_private *dev_priv = dev->dev_private;
6255 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6258 tmp = I915_READ(PFIT_CONTROL);
6259 if (!(tmp & PFIT_ENABLE))
6262 /* Check whether the pfit is attached to our pipe. */
6263 if (INTEL_INFO(dev)->gen < 4) {
6264 if (crtc->pipe != PIPE_B)
6267 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6271 pipe_config->gmch_pfit.control = tmp;
6272 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6273 if (INTEL_INFO(dev)->gen < 5)
6274 pipe_config->gmch_pfit.lvds_border_bits =
6275 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6278 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6279 struct intel_crtc_config *pipe_config)
6281 struct drm_device *dev = crtc->base.dev;
6282 struct drm_i915_private *dev_priv = dev->dev_private;
6283 int pipe = pipe_config->cpu_transcoder;
6284 intel_clock_t clock;
6286 int refclk = 100000;
6288 /* In case of MIPI DPLL will not even be used */
6289 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6292 mutex_lock(&dev_priv->dpio_lock);
6293 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
6294 mutex_unlock(&dev_priv->dpio_lock);
6296 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6297 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6298 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6299 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6300 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6302 vlv_clock(refclk, &clock);
6304 /* clock.dot is the fast clock */
6305 pipe_config->port_clock = clock.dot / 5;
6308 static void i9xx_get_plane_config(struct intel_crtc *crtc,
6309 struct intel_plane_config *plane_config)
6311 struct drm_device *dev = crtc->base.dev;
6312 struct drm_i915_private *dev_priv = dev->dev_private;
6313 u32 val, base, offset;
6314 int pipe = crtc->pipe, plane = crtc->plane;
6315 int fourcc, pixel_format;
6318 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6319 if (!crtc->base.primary->fb) {
6320 DRM_DEBUG_KMS("failed to alloc fb\n");
6324 val = I915_READ(DSPCNTR(plane));
6326 if (INTEL_INFO(dev)->gen >= 4)
6327 if (val & DISPPLANE_TILED)
6328 plane_config->tiled = true;
6330 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6331 fourcc = intel_format_to_fourcc(pixel_format);
6332 crtc->base.primary->fb->pixel_format = fourcc;
6333 crtc->base.primary->fb->bits_per_pixel =
6334 drm_format_plane_cpp(fourcc, 0) * 8;
6336 if (INTEL_INFO(dev)->gen >= 4) {
6337 if (plane_config->tiled)
6338 offset = I915_READ(DSPTILEOFF(plane));
6340 offset = I915_READ(DSPLINOFF(plane));
6341 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6343 base = I915_READ(DSPADDR(plane));
6345 plane_config->base = base;
6347 val = I915_READ(PIPESRC(pipe));
6348 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6349 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
6351 val = I915_READ(DSPSTRIDE(pipe));
6352 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
6354 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
6355 plane_config->tiled);
6357 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6360 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
6361 pipe, plane, crtc->base.primary->fb->width,
6362 crtc->base.primary->fb->height,
6363 crtc->base.primary->fb->bits_per_pixel, base,
6364 crtc->base.primary->fb->pitches[0],
6365 plane_config->size);
6369 static void chv_crtc_clock_get(struct intel_crtc *crtc,
6370 struct intel_crtc_config *pipe_config)
6372 struct drm_device *dev = crtc->base.dev;
6373 struct drm_i915_private *dev_priv = dev->dev_private;
6374 int pipe = pipe_config->cpu_transcoder;
6375 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6376 intel_clock_t clock;
6377 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6378 int refclk = 100000;
6380 mutex_lock(&dev_priv->dpio_lock);
6381 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6382 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6383 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6384 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6385 mutex_unlock(&dev_priv->dpio_lock);
6387 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6388 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6389 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6390 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6391 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6393 chv_clock(refclk, &clock);
6395 /* clock.dot is the fast clock */
6396 pipe_config->port_clock = clock.dot / 5;
6399 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6400 struct intel_crtc_config *pipe_config)
6402 struct drm_device *dev = crtc->base.dev;
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6406 if (!intel_display_power_enabled(dev_priv,
6407 POWER_DOMAIN_PIPE(crtc->pipe)))
6410 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6411 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
6413 tmp = I915_READ(PIPECONF(crtc->pipe));
6414 if (!(tmp & PIPECONF_ENABLE))
6417 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6418 switch (tmp & PIPECONF_BPC_MASK) {
6420 pipe_config->pipe_bpp = 18;
6423 pipe_config->pipe_bpp = 24;
6425 case PIPECONF_10BPC:
6426 pipe_config->pipe_bpp = 30;
6433 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6434 pipe_config->limited_color_range = true;
6436 if (INTEL_INFO(dev)->gen < 4)
6437 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6439 intel_get_pipe_timings(crtc, pipe_config);
6441 i9xx_get_pfit_config(crtc, pipe_config);
6443 if (INTEL_INFO(dev)->gen >= 4) {
6444 tmp = I915_READ(DPLL_MD(crtc->pipe));
6445 pipe_config->pixel_multiplier =
6446 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6447 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
6448 pipe_config->dpll_hw_state.dpll_md = tmp;
6449 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6450 tmp = I915_READ(DPLL(crtc->pipe));
6451 pipe_config->pixel_multiplier =
6452 ((tmp & SDVO_MULTIPLIER_MASK)
6453 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6455 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6456 * port and will be fixed up in the encoder->get_config
6458 pipe_config->pixel_multiplier = 1;
6460 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6461 if (!IS_VALLEYVIEW(dev)) {
6463 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6464 * on 830. Filter it out here so that we don't
6465 * report errors due to that.
6468 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6470 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6471 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
6473 /* Mask out read-only status bits. */
6474 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6475 DPLL_PORTC_READY_MASK |
6476 DPLL_PORTB_READY_MASK);
6479 if (IS_CHERRYVIEW(dev))
6480 chv_crtc_clock_get(crtc, pipe_config);
6481 else if (IS_VALLEYVIEW(dev))
6482 vlv_crtc_clock_get(crtc, pipe_config);
6484 i9xx_crtc_clock_get(crtc, pipe_config);
6489 static void ironlake_init_pch_refclk(struct drm_device *dev)
6491 struct drm_i915_private *dev_priv = dev->dev_private;
6492 struct intel_encoder *encoder;
6494 bool has_lvds = false;
6495 bool has_cpu_edp = false;
6496 bool has_panel = false;
6497 bool has_ck505 = false;
6498 bool can_ssc = false;
6500 /* We need to take the global config into account */
6501 for_each_intel_encoder(dev, encoder) {
6502 switch (encoder->type) {
6503 case INTEL_OUTPUT_LVDS:
6507 case INTEL_OUTPUT_EDP:
6509 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
6515 if (HAS_PCH_IBX(dev)) {
6516 has_ck505 = dev_priv->vbt.display_clock_mode;
6517 can_ssc = has_ck505;
6523 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6524 has_panel, has_lvds, has_ck505);
6526 /* Ironlake: try to setup display ref clock before DPLL
6527 * enabling. This is only under driver's control after
6528 * PCH B stepping, previous chipset stepping should be
6529 * ignoring this setting.
6531 val = I915_READ(PCH_DREF_CONTROL);
6533 /* As we must carefully and slowly disable/enable each source in turn,
6534 * compute the final state we want first and check if we need to
6535 * make any changes at all.
6538 final &= ~DREF_NONSPREAD_SOURCE_MASK;
6540 final |= DREF_NONSPREAD_CK505_ENABLE;
6542 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6544 final &= ~DREF_SSC_SOURCE_MASK;
6545 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6546 final &= ~DREF_SSC1_ENABLE;
6549 final |= DREF_SSC_SOURCE_ENABLE;
6551 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6552 final |= DREF_SSC1_ENABLE;
6555 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6556 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6558 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6560 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6562 final |= DREF_SSC_SOURCE_DISABLE;
6563 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6569 /* Always enable nonspread source */
6570 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6573 val |= DREF_NONSPREAD_CK505_ENABLE;
6575 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6578 val &= ~DREF_SSC_SOURCE_MASK;
6579 val |= DREF_SSC_SOURCE_ENABLE;
6581 /* SSC must be turned on before enabling the CPU output */
6582 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
6583 DRM_DEBUG_KMS("Using SSC on panel\n");
6584 val |= DREF_SSC1_ENABLE;
6586 val &= ~DREF_SSC1_ENABLE;
6588 /* Get SSC going before enabling the outputs */
6589 I915_WRITE(PCH_DREF_CONTROL, val);
6590 POSTING_READ(PCH_DREF_CONTROL);
6593 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6595 /* Enable CPU source on CPU attached eDP */
6597 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
6598 DRM_DEBUG_KMS("Using SSC on eDP\n");
6599 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6601 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6603 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6605 I915_WRITE(PCH_DREF_CONTROL, val);
6606 POSTING_READ(PCH_DREF_CONTROL);
6609 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6611 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6613 /* Turn off CPU output */
6614 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6616 I915_WRITE(PCH_DREF_CONTROL, val);
6617 POSTING_READ(PCH_DREF_CONTROL);
6620 /* Turn off the SSC source */
6621 val &= ~DREF_SSC_SOURCE_MASK;
6622 val |= DREF_SSC_SOURCE_DISABLE;
6625 val &= ~DREF_SSC1_ENABLE;
6627 I915_WRITE(PCH_DREF_CONTROL, val);
6628 POSTING_READ(PCH_DREF_CONTROL);
6632 BUG_ON(val != final);
6635 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
6639 tmp = I915_READ(SOUTH_CHICKEN2);
6640 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6641 I915_WRITE(SOUTH_CHICKEN2, tmp);
6643 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6644 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6645 DRM_ERROR("FDI mPHY reset assert timeout\n");
6647 tmp = I915_READ(SOUTH_CHICKEN2);
6648 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6649 I915_WRITE(SOUTH_CHICKEN2, tmp);
6651 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6652 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6653 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
6656 /* WaMPhyProgramming:hsw */
6657 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6661 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6662 tmp &= ~(0xFF << 24);
6663 tmp |= (0x12 << 24);
6664 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6666 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6668 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6670 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6672 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6674 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6675 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6676 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6678 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6679 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6680 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6682 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6685 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
6687 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6690 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
6692 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6695 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6697 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6700 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6702 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6703 tmp &= ~(0xFF << 16);
6704 tmp |= (0x1C << 16);
6705 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6707 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6708 tmp &= ~(0xFF << 16);
6709 tmp |= (0x1C << 16);
6710 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6712 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6714 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
6716 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6718 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
6720 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6721 tmp &= ~(0xF << 28);
6723 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
6725 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6726 tmp &= ~(0xF << 28);
6728 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
6731 /* Implements 3 different sequences from BSpec chapter "Display iCLK
6732 * Programming" based on the parameters passed:
6733 * - Sequence to enable CLKOUT_DP
6734 * - Sequence to enable CLKOUT_DP without spread
6735 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6737 static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6740 struct drm_i915_private *dev_priv = dev->dev_private;
6743 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6745 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6746 with_fdi, "LP PCH doesn't have FDI\n"))
6749 mutex_lock(&dev_priv->dpio_lock);
6751 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6752 tmp &= ~SBI_SSCCTL_DISABLE;
6753 tmp |= SBI_SSCCTL_PATHALT;
6754 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6759 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6760 tmp &= ~SBI_SSCCTL_PATHALT;
6761 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6764 lpt_reset_fdi_mphy(dev_priv);
6765 lpt_program_fdi_mphy(dev_priv);
6769 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6770 SBI_GEN0 : SBI_DBUFF0;
6771 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6772 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6773 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6775 mutex_unlock(&dev_priv->dpio_lock);
6778 /* Sequence to disable CLKOUT_DP */
6779 static void lpt_disable_clkout_dp(struct drm_device *dev)
6781 struct drm_i915_private *dev_priv = dev->dev_private;
6784 mutex_lock(&dev_priv->dpio_lock);
6786 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6787 SBI_GEN0 : SBI_DBUFF0;
6788 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6789 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6790 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6792 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6793 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6794 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6795 tmp |= SBI_SSCCTL_PATHALT;
6796 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6799 tmp |= SBI_SSCCTL_DISABLE;
6800 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6803 mutex_unlock(&dev_priv->dpio_lock);
6806 static void lpt_init_pch_refclk(struct drm_device *dev)
6808 struct intel_encoder *encoder;
6809 bool has_vga = false;
6811 for_each_intel_encoder(dev, encoder) {
6812 switch (encoder->type) {
6813 case INTEL_OUTPUT_ANALOG:
6820 lpt_enable_clkout_dp(dev, true, true);
6822 lpt_disable_clkout_dp(dev);
6826 * Initialize reference clocks when the driver loads
6828 void intel_init_pch_refclk(struct drm_device *dev)
6830 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6831 ironlake_init_pch_refclk(dev);
6832 else if (HAS_PCH_LPT(dev))
6833 lpt_init_pch_refclk(dev);
6836 static int ironlake_get_refclk(struct drm_crtc *crtc)
6838 struct drm_device *dev = crtc->dev;
6839 struct drm_i915_private *dev_priv = dev->dev_private;
6840 struct intel_encoder *encoder;
6841 int num_connectors = 0;
6842 bool is_lvds = false;
6844 for_each_encoder_on_crtc(dev, crtc, encoder) {
6845 switch (encoder->type) {
6846 case INTEL_OUTPUT_LVDS:
6853 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
6854 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
6855 dev_priv->vbt.lvds_ssc_freq);
6856 return dev_priv->vbt.lvds_ssc_freq;
6862 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
6864 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6866 int pipe = intel_crtc->pipe;
6871 switch (intel_crtc->config.pipe_bpp) {
6873 val |= PIPECONF_6BPC;
6876 val |= PIPECONF_8BPC;
6879 val |= PIPECONF_10BPC;
6882 val |= PIPECONF_12BPC;
6885 /* Case prevented by intel_choose_pipe_bpp_dither. */
6889 if (intel_crtc->config.dither)
6890 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6892 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6893 val |= PIPECONF_INTERLACED_ILK;
6895 val |= PIPECONF_PROGRESSIVE;
6897 if (intel_crtc->config.limited_color_range)
6898 val |= PIPECONF_COLOR_RANGE_SELECT;
6900 I915_WRITE(PIPECONF(pipe), val);
6901 POSTING_READ(PIPECONF(pipe));
6905 * Set up the pipe CSC unit.
6907 * Currently only full range RGB to limited range RGB conversion
6908 * is supported, but eventually this should handle various
6909 * RGB<->YCbCr scenarios as well.
6911 static void intel_set_pipe_csc(struct drm_crtc *crtc)
6913 struct drm_device *dev = crtc->dev;
6914 struct drm_i915_private *dev_priv = dev->dev_private;
6915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6916 int pipe = intel_crtc->pipe;
6917 uint16_t coeff = 0x7800; /* 1.0 */
6920 * TODO: Check what kind of values actually come out of the pipe
6921 * with these coeff/postoff values and adjust to get the best
6922 * accuracy. Perhaps we even need to take the bpc value into
6926 if (intel_crtc->config.limited_color_range)
6927 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6930 * GY/GU and RY/RU should be the other way around according
6931 * to BSpec, but reality doesn't agree. Just set them up in
6932 * a way that results in the correct picture.
6934 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6935 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6937 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6938 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6940 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6941 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6943 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6944 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6945 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6947 if (INTEL_INFO(dev)->gen > 6) {
6948 uint16_t postoff = 0;
6950 if (intel_crtc->config.limited_color_range)
6951 postoff = (16 * (1 << 12) / 255) & 0x1fff;
6953 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6954 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6955 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6957 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6959 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6961 if (intel_crtc->config.limited_color_range)
6962 mode |= CSC_BLACK_SCREEN_OFFSET;
6964 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6968 static void haswell_set_pipeconf(struct drm_crtc *crtc)
6970 struct drm_device *dev = crtc->dev;
6971 struct drm_i915_private *dev_priv = dev->dev_private;
6972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6973 enum i915_pipe pipe = intel_crtc->pipe;
6974 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
6979 if (IS_HASWELL(dev) && intel_crtc->config.dither)
6980 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6982 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6983 val |= PIPECONF_INTERLACED_ILK;
6985 val |= PIPECONF_PROGRESSIVE;
6987 I915_WRITE(PIPECONF(cpu_transcoder), val);
6988 POSTING_READ(PIPECONF(cpu_transcoder));
6990 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6991 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
6993 if (IS_BROADWELL(dev)) {
6996 switch (intel_crtc->config.pipe_bpp) {
6998 val |= PIPEMISC_DITHER_6_BPC;
7001 val |= PIPEMISC_DITHER_8_BPC;
7004 val |= PIPEMISC_DITHER_10_BPC;
7007 val |= PIPEMISC_DITHER_12_BPC;
7010 /* Case prevented by pipe_config_set_bpp. */
7014 if (intel_crtc->config.dither)
7015 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7017 I915_WRITE(PIPEMISC(pipe), val);
7021 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
7022 intel_clock_t *clock,
7023 bool *has_reduced_clock,
7024 intel_clock_t *reduced_clock)
7026 struct drm_device *dev = crtc->dev;
7027 struct drm_i915_private *dev_priv = dev->dev_private;
7028 struct intel_encoder *intel_encoder;
7030 const intel_limit_t *limit;
7031 bool ret, is_lvds = false;
7033 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7034 switch (intel_encoder->type) {
7035 case INTEL_OUTPUT_LVDS:
7041 refclk = ironlake_get_refclk(crtc);
7044 * Returns a set of divisors for the desired target clock with the given
7045 * refclk, or FALSE. The returned values represent the clock equation:
7046 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7048 limit = intel_limit(crtc, refclk);
7049 ret = dev_priv->display.find_dpll(limit, crtc,
7050 to_intel_crtc(crtc)->config.port_clock,
7051 refclk, NULL, clock);
7055 if (is_lvds && dev_priv->lvds_downclock_avail) {
7057 * Ensure we match the reduced clock's P to the target clock.
7058 * If the clocks don't match, we can't switch the display clock
7059 * by using the FP0/FP1. In such case we will disable the LVDS
7060 * downclock feature.
7062 *has_reduced_clock =
7063 dev_priv->display.find_dpll(limit, crtc,
7064 dev_priv->lvds_downclock,
7072 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7075 * Account for spread spectrum to avoid
7076 * oversubscribing the link. Max center spread
7077 * is 2.5%; use 5% for safety's sake.
7079 u32 bps = target_clock * bpp * 21 / 20;
7080 return DIV_ROUND_UP(bps, link_bw * 8);
7083 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
7085 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
7088 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7090 intel_clock_t *reduced_clock, u32 *fp2)
7092 struct drm_crtc *crtc = &intel_crtc->base;
7093 struct drm_device *dev = crtc->dev;
7094 struct drm_i915_private *dev_priv = dev->dev_private;
7095 struct intel_encoder *intel_encoder;
7097 int factor, num_connectors = 0;
7098 bool is_lvds = false, is_sdvo = false;
7100 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7101 switch (intel_encoder->type) {
7102 case INTEL_OUTPUT_LVDS:
7105 case INTEL_OUTPUT_SDVO:
7106 case INTEL_OUTPUT_HDMI:
7114 /* Enable autotuning of the PLL clock (if permissible) */
7117 if ((intel_panel_use_ssc(dev_priv) &&
7118 dev_priv->vbt.lvds_ssc_freq == 100000) ||
7119 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
7121 } else if (intel_crtc->config.sdvo_tv_clock)
7124 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7127 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7133 dpll |= DPLLB_MODE_LVDS;
7135 dpll |= DPLLB_MODE_DAC_SERIAL;
7137 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7138 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
7141 dpll |= DPLL_SDVO_HIGH_SPEED;
7142 if (intel_crtc->config.has_dp_encoder)
7143 dpll |= DPLL_SDVO_HIGH_SPEED;
7145 /* compute bitmask from p1 value */
7146 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7148 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7150 switch (intel_crtc->config.dpll.p2) {
7152 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7155 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7158 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7161 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7165 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7166 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7168 dpll |= PLL_REF_INPUT_DREFCLK;
7170 return dpll | DPLL_VCO_ENABLE;
7173 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
7175 struct drm_framebuffer *fb)
7177 struct drm_device *dev = crtc->dev;
7178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7179 int num_connectors = 0;
7180 intel_clock_t clock, reduced_clock;
7181 u32 dpll = 0, fp = 0, fp2 = 0;
7182 bool ok, has_reduced_clock = false;
7183 bool is_lvds = false;
7184 struct intel_encoder *encoder;
7185 struct intel_shared_dpll *pll;
7187 for_each_encoder_on_crtc(dev, crtc, encoder) {
7188 switch (encoder->type) {
7189 case INTEL_OUTPUT_LVDS:
7197 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7198 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7200 ok = ironlake_compute_clocks(crtc, &clock,
7201 &has_reduced_clock, &reduced_clock);
7202 if (!ok && !intel_crtc->config.clock_set) {
7203 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7206 /* Compat-code for transition, will disappear. */
7207 if (!intel_crtc->config.clock_set) {
7208 intel_crtc->config.dpll.n = clock.n;
7209 intel_crtc->config.dpll.m1 = clock.m1;
7210 intel_crtc->config.dpll.m2 = clock.m2;
7211 intel_crtc->config.dpll.p1 = clock.p1;
7212 intel_crtc->config.dpll.p2 = clock.p2;
7215 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
7216 if (intel_crtc->config.has_pch_encoder) {
7217 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
7218 if (has_reduced_clock)
7219 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
7221 dpll = ironlake_compute_dpll(intel_crtc,
7222 &fp, &reduced_clock,
7223 has_reduced_clock ? &fp2 : NULL);
7225 intel_crtc->config.dpll_hw_state.dpll = dpll;
7226 intel_crtc->config.dpll_hw_state.fp0 = fp;
7227 if (has_reduced_clock)
7228 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7230 intel_crtc->config.dpll_hw_state.fp1 = fp;
7232 pll = intel_get_shared_dpll(intel_crtc);
7234 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
7235 pipe_name(intel_crtc->pipe));
7239 intel_put_shared_dpll(intel_crtc);
7241 if (is_lvds && has_reduced_clock && i915.powersave)
7242 intel_crtc->lowfreq_avail = true;
7244 intel_crtc->lowfreq_avail = false;
7249 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7250 struct intel_link_m_n *m_n)
7252 struct drm_device *dev = crtc->base.dev;
7253 struct drm_i915_private *dev_priv = dev->dev_private;
7254 enum i915_pipe pipe = crtc->pipe;
7256 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7257 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7258 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7260 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7261 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7262 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7265 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7266 enum transcoder transcoder,
7267 struct intel_link_m_n *m_n,
7268 struct intel_link_m_n *m2_n2)
7270 struct drm_device *dev = crtc->base.dev;
7271 struct drm_i915_private *dev_priv = dev->dev_private;
7272 enum i915_pipe pipe = crtc->pipe;
7274 if (INTEL_INFO(dev)->gen >= 5) {
7275 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7276 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7277 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7279 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7280 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7281 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7282 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7283 * gen < 8) and if DRRS is supported (to make sure the
7284 * registers are not unnecessarily read).
7286 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7287 crtc->config.has_drrs) {
7288 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7289 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7290 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7292 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7293 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7294 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7297 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7298 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7299 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7301 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7302 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7303 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7307 void intel_dp_get_m_n(struct intel_crtc *crtc,
7308 struct intel_crtc_config *pipe_config)
7310 if (crtc->config.has_pch_encoder)
7311 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7313 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7314 &pipe_config->dp_m_n,
7315 &pipe_config->dp_m2_n2);
7318 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7319 struct intel_crtc_config *pipe_config)
7321 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7322 &pipe_config->fdi_m_n, NULL);
7325 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7326 struct intel_crtc_config *pipe_config)
7328 struct drm_device *dev = crtc->base.dev;
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7332 tmp = I915_READ(PF_CTL(crtc->pipe));
7334 if (tmp & PF_ENABLE) {
7335 pipe_config->pch_pfit.enabled = true;
7336 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7337 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
7339 /* We currently do not free assignements of panel fitters on
7340 * ivb/hsw (since we don't use the higher upscaling modes which
7341 * differentiates them) so just WARN about this case for now. */
7343 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7344 PF_PIPE_SEL_IVB(crtc->pipe));
7349 static void ironlake_get_plane_config(struct intel_crtc *crtc,
7350 struct intel_plane_config *plane_config)
7352 struct drm_device *dev = crtc->base.dev;
7353 struct drm_i915_private *dev_priv = dev->dev_private;
7354 u32 val, base, offset;
7355 int pipe = crtc->pipe, plane = crtc->plane;
7356 int fourcc, pixel_format;
7359 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7360 if (!crtc->base.primary->fb) {
7361 DRM_DEBUG_KMS("failed to alloc fb\n");
7365 val = I915_READ(DSPCNTR(plane));
7367 if (INTEL_INFO(dev)->gen >= 4)
7368 if (val & DISPPLANE_TILED)
7369 plane_config->tiled = true;
7371 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7372 fourcc = intel_format_to_fourcc(pixel_format);
7373 crtc->base.primary->fb->pixel_format = fourcc;
7374 crtc->base.primary->fb->bits_per_pixel =
7375 drm_format_plane_cpp(fourcc, 0) * 8;
7377 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7378 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7379 offset = I915_READ(DSPOFFSET(plane));
7381 if (plane_config->tiled)
7382 offset = I915_READ(DSPTILEOFF(plane));
7384 offset = I915_READ(DSPLINOFF(plane));
7386 plane_config->base = base;
7388 val = I915_READ(PIPESRC(pipe));
7389 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7390 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
7392 val = I915_READ(DSPSTRIDE(pipe));
7393 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
7395 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
7396 plane_config->tiled);
7398 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7401 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7402 pipe, plane, crtc->base.primary->fb->width,
7403 crtc->base.primary->fb->height,
7404 crtc->base.primary->fb->bits_per_pixel, base,
7405 crtc->base.primary->fb->pitches[0],
7406 plane_config->size);
7409 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7410 struct intel_crtc_config *pipe_config)
7412 struct drm_device *dev = crtc->base.dev;
7413 struct drm_i915_private *dev_priv = dev->dev_private;
7416 if (!intel_display_power_enabled(dev_priv,
7417 POWER_DOMAIN_PIPE(crtc->pipe)))
7420 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
7421 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7423 tmp = I915_READ(PIPECONF(crtc->pipe));
7424 if (!(tmp & PIPECONF_ENABLE))
7427 switch (tmp & PIPECONF_BPC_MASK) {
7429 pipe_config->pipe_bpp = 18;
7432 pipe_config->pipe_bpp = 24;
7434 case PIPECONF_10BPC:
7435 pipe_config->pipe_bpp = 30;
7437 case PIPECONF_12BPC:
7438 pipe_config->pipe_bpp = 36;
7444 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7445 pipe_config->limited_color_range = true;
7447 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
7448 struct intel_shared_dpll *pll;
7450 pipe_config->has_pch_encoder = true;
7452 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7453 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7454 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7456 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7458 if (HAS_PCH_IBX(dev_priv->dev)) {
7459 pipe_config->shared_dpll =
7460 (enum intel_dpll_id) crtc->pipe;
7462 tmp = I915_READ(PCH_DPLL_SEL);
7463 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7464 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7466 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7469 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7471 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7472 &pipe_config->dpll_hw_state));
7474 tmp = pipe_config->dpll_hw_state.dpll;
7475 pipe_config->pixel_multiplier =
7476 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7477 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
7479 ironlake_pch_clock_get(crtc, pipe_config);
7481 pipe_config->pixel_multiplier = 1;
7484 intel_get_pipe_timings(crtc, pipe_config);
7486 ironlake_get_pfit_config(crtc, pipe_config);
7491 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7493 struct drm_device *dev = dev_priv->dev;
7494 struct intel_crtc *crtc;
7496 for_each_intel_crtc(dev, crtc)
7497 WARN(crtc->active, "CRTC for pipe %c enabled\n",
7498 pipe_name(crtc->pipe));
7500 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7501 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7502 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7503 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
7504 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7505 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7506 "CPU PWM1 enabled\n");
7507 if (IS_HASWELL(dev))
7508 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7509 "CPU PWM2 enabled\n");
7510 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7511 "PCH PWM1 enabled\n");
7512 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7513 "Utility pin enabled\n");
7514 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7517 * In theory we can still leave IRQs enabled, as long as only the HPD
7518 * interrupts remain enabled. We used to check for that, but since it's
7519 * gen-specific and since we only disable LCPLL after we fully disable
7520 * the interrupts, the check below should be enough.
7522 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
7525 static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7527 struct drm_device *dev = dev_priv->dev;
7529 if (IS_HASWELL(dev))
7530 return I915_READ(D_COMP_HSW);
7532 return I915_READ(D_COMP_BDW);
7535 static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7537 struct drm_device *dev = dev_priv->dev;
7539 if (IS_HASWELL(dev)) {
7540 mutex_lock(&dev_priv->rps.hw_lock);
7541 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7543 DRM_ERROR("Failed to write to D_COMP\n");
7544 mutex_unlock(&dev_priv->rps.hw_lock);
7546 I915_WRITE(D_COMP_BDW, val);
7547 POSTING_READ(D_COMP_BDW);
7552 * This function implements pieces of two sequences from BSpec:
7553 * - Sequence for display software to disable LCPLL
7554 * - Sequence for display software to allow package C8+
7555 * The steps implemented here are just the steps that actually touch the LCPLL
7556 * register. Callers should take care of disabling all the display engine
7557 * functions, doing the mode unset, fixing interrupts, etc.
7559 static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7560 bool switch_to_fclk, bool allow_power_down)
7564 assert_can_disable_lcpll(dev_priv);
7566 val = I915_READ(LCPLL_CTL);
7568 if (switch_to_fclk) {
7569 val |= LCPLL_CD_SOURCE_FCLK;
7570 I915_WRITE(LCPLL_CTL, val);
7572 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7573 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7574 DRM_ERROR("Switching to FCLK failed\n");
7576 val = I915_READ(LCPLL_CTL);
7579 val |= LCPLL_PLL_DISABLE;
7580 I915_WRITE(LCPLL_CTL, val);
7581 POSTING_READ(LCPLL_CTL);
7583 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7584 DRM_ERROR("LCPLL still locked\n");
7586 val = hsw_read_dcomp(dev_priv);
7587 val |= D_COMP_COMP_DISABLE;
7588 hsw_write_dcomp(dev_priv, val);
7591 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7593 DRM_ERROR("D_COMP RCOMP still in progress\n");
7595 if (allow_power_down) {
7596 val = I915_READ(LCPLL_CTL);
7597 val |= LCPLL_POWER_DOWN_ALLOW;
7598 I915_WRITE(LCPLL_CTL, val);
7599 POSTING_READ(LCPLL_CTL);
7604 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7607 static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
7611 val = I915_READ(LCPLL_CTL);
7613 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7614 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7618 * Make sure we're not on PC8 state before disabling PC8, otherwise
7619 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7621 * The other problem is that hsw_restore_lcpll() is called as part of
7622 * the runtime PM resume sequence, so we can't just call
7623 * gen6_gt_force_wake_get() because that function calls
7624 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7625 * while we are on the resume sequence. So to solve this problem we have
7626 * to call special forcewake code that doesn't touch runtime PM and
7627 * doesn't enable the forcewake delayed work.
7629 lockmgr(&dev_priv->uncore.lock, LK_EXCLUSIVE);
7630 if (dev_priv->uncore.forcewake_count++ == 0)
7631 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7632 lockmgr(&dev_priv->uncore.lock, LK_RELEASE);
7634 if (val & LCPLL_POWER_DOWN_ALLOW) {
7635 val &= ~LCPLL_POWER_DOWN_ALLOW;
7636 I915_WRITE(LCPLL_CTL, val);
7637 POSTING_READ(LCPLL_CTL);
7640 val = hsw_read_dcomp(dev_priv);
7641 val |= D_COMP_COMP_FORCE;
7642 val &= ~D_COMP_COMP_DISABLE;
7643 hsw_write_dcomp(dev_priv, val);
7645 val = I915_READ(LCPLL_CTL);
7646 val &= ~LCPLL_PLL_DISABLE;
7647 I915_WRITE(LCPLL_CTL, val);
7649 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7650 DRM_ERROR("LCPLL not locked yet\n");
7652 if (val & LCPLL_CD_SOURCE_FCLK) {
7653 val = I915_READ(LCPLL_CTL);
7654 val &= ~LCPLL_CD_SOURCE_FCLK;
7655 I915_WRITE(LCPLL_CTL, val);
7657 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7658 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7659 DRM_ERROR("Switching back to LCPLL failed\n");
7662 /* See the big comment above. */
7663 lockmgr(&dev_priv->uncore.lock, LK_EXCLUSIVE);
7664 if (--dev_priv->uncore.forcewake_count == 0)
7665 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7666 lockmgr(&dev_priv->uncore.lock, LK_RELEASE);
7670 * Package states C8 and deeper are really deep PC states that can only be
7671 * reached when all the devices on the system allow it, so even if the graphics
7672 * device allows PC8+, it doesn't mean the system will actually get to these
7673 * states. Our driver only allows PC8+ when going into runtime PM.
7675 * The requirements for PC8+ are that all the outputs are disabled, the power
7676 * well is disabled and most interrupts are disabled, and these are also
7677 * requirements for runtime PM. When these conditions are met, we manually do
7678 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7679 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7682 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7683 * the state of some registers, so when we come back from PC8+ we need to
7684 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7685 * need to take care of the registers kept by RC6. Notice that this happens even
7686 * if we don't put the device in PCI D3 state (which is what currently happens
7687 * because of the runtime PM support).
7689 * For more, read "Display Sequences for Package C8" on the hardware
7692 void hsw_enable_pc8(struct drm_i915_private *dev_priv)
7694 struct drm_device *dev = dev_priv->dev;
7697 DRM_DEBUG_KMS("Enabling package C8+\n");
7699 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7700 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7701 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7702 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7705 lpt_disable_clkout_dp(dev);
7706 hsw_disable_lcpll(dev_priv, true, true);
7709 void hsw_disable_pc8(struct drm_i915_private *dev_priv)
7711 struct drm_device *dev = dev_priv->dev;
7714 DRM_DEBUG_KMS("Disabling package C8+\n");
7716 hsw_restore_lcpll(dev_priv);
7717 lpt_init_pch_refclk(dev);
7719 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7720 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7721 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7722 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7725 intel_prepare_ddi(dev);
7728 static void snb_modeset_global_resources(struct drm_device *dev)
7730 modeset_update_crtc_power_domains(dev);
7733 static void haswell_modeset_global_resources(struct drm_device *dev)
7735 modeset_update_crtc_power_domains(dev);
7738 static int haswell_crtc_mode_set(struct drm_crtc *crtc,
7740 struct drm_framebuffer *fb)
7742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7744 if (!intel_ddi_pll_select(intel_crtc))
7747 intel_crtc->lowfreq_avail = false;
7752 static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7754 struct intel_crtc_config *pipe_config)
7756 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7758 switch (pipe_config->ddi_pll_sel) {
7759 case PORT_CLK_SEL_WRPLL1:
7760 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7762 case PORT_CLK_SEL_WRPLL2:
7763 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7768 static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7769 struct intel_crtc_config *pipe_config)
7771 struct drm_device *dev = crtc->base.dev;
7772 struct drm_i915_private *dev_priv = dev->dev_private;
7773 struct intel_shared_dpll *pll;
7777 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7779 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7781 haswell_get_ddi_pll(dev_priv, port, pipe_config);
7783 if (pipe_config->shared_dpll >= 0) {
7784 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7786 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7787 &pipe_config->dpll_hw_state));
7791 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7792 * DDI E. So just check whether this pipe is wired to DDI E and whether
7793 * the PCH transcoder is on.
7795 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7796 pipe_config->has_pch_encoder = true;
7798 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7799 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7800 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7802 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7806 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7807 struct intel_crtc_config *pipe_config)
7809 struct drm_device *dev = crtc->base.dev;
7810 struct drm_i915_private *dev_priv = dev->dev_private;
7811 enum intel_display_power_domain pfit_domain;
7814 if (!intel_display_power_enabled(dev_priv,
7815 POWER_DOMAIN_PIPE(crtc->pipe)))
7818 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
7819 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7821 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7822 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7823 enum i915_pipe trans_edp_pipe;
7824 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7826 WARN(1, "unknown pipe linked to edp transcoder\n");
7827 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7828 case TRANS_DDI_EDP_INPUT_A_ON:
7829 trans_edp_pipe = PIPE_A;
7831 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7832 trans_edp_pipe = PIPE_B;
7834 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7835 trans_edp_pipe = PIPE_C;
7839 if (trans_edp_pipe == crtc->pipe)
7840 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7843 if (!intel_display_power_enabled(dev_priv,
7844 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
7847 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
7848 if (!(tmp & PIPECONF_ENABLE))
7851 haswell_get_ddi_port_state(crtc, pipe_config);
7853 intel_get_pipe_timings(crtc, pipe_config);
7855 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
7856 if (intel_display_power_enabled(dev_priv, pfit_domain))
7857 ironlake_get_pfit_config(crtc, pipe_config);
7859 if (IS_HASWELL(dev))
7860 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7861 (I915_READ(IPS_CTL) & IPS_ENABLE);
7863 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
7864 pipe_config->pixel_multiplier =
7865 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
7867 pipe_config->pixel_multiplier = 1;
7876 } hdmi_audio_clock[] = {
7877 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7878 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7879 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7880 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7881 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7882 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7883 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7884 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7885 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7886 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7889 /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7890 static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7894 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7895 if (mode->clock == hdmi_audio_clock[i].clock)
7899 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7900 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7904 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7905 hdmi_audio_clock[i].clock,
7906 hdmi_audio_clock[i].config);
7908 return hdmi_audio_clock[i].config;
7911 static bool intel_eld_uptodate(struct drm_connector *connector,
7912 int reg_eldv, uint32_t bits_eldv,
7913 int reg_elda, uint32_t bits_elda,
7916 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7917 uint8_t *eld = connector->eld;
7920 i = I915_READ(reg_eldv);
7929 i = I915_READ(reg_elda);
7931 I915_WRITE(reg_elda, i);
7933 for (i = 0; i < eld[2]; i++)
7934 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7940 static void g4x_write_eld(struct drm_connector *connector,
7941 struct drm_crtc *crtc,
7942 struct drm_display_mode *mode)
7944 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7945 uint8_t *eld = connector->eld;
7950 i = I915_READ(G4X_AUD_VID_DID);
7952 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7953 eldv = G4X_ELDV_DEVCL_DEVBLC;
7955 eldv = G4X_ELDV_DEVCTG;
7957 if (intel_eld_uptodate(connector,
7958 G4X_AUD_CNTL_ST, eldv,
7959 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7960 G4X_HDMIW_HDMIEDID))
7963 i = I915_READ(G4X_AUD_CNTL_ST);
7964 i &= ~(eldv | G4X_ELD_ADDR);
7965 len = (i >> 9) & 0x1f; /* ELD buffer size */
7966 I915_WRITE(G4X_AUD_CNTL_ST, i);
7971 len = min_t(uint8_t, eld[2], len);
7972 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7973 for (i = 0; i < len; i++)
7974 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7976 i = I915_READ(G4X_AUD_CNTL_ST);
7978 I915_WRITE(G4X_AUD_CNTL_ST, i);
7981 static void haswell_write_eld(struct drm_connector *connector,
7982 struct drm_crtc *crtc,
7983 struct drm_display_mode *mode)
7985 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7986 uint8_t *eld = connector->eld;
7990 int pipe = to_intel_crtc(crtc)->pipe;
7993 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7994 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7995 int aud_config = HSW_AUD_CFG(pipe);
7996 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7998 /* Audio output enable */
7999 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
8000 tmp = I915_READ(aud_cntrl_st2);
8001 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
8002 I915_WRITE(aud_cntrl_st2, tmp);
8003 POSTING_READ(aud_cntrl_st2);
8005 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
8007 /* Set ELD valid state */
8008 tmp = I915_READ(aud_cntrl_st2);
8009 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
8010 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
8011 I915_WRITE(aud_cntrl_st2, tmp);
8012 tmp = I915_READ(aud_cntrl_st2);
8013 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
8015 /* Enable HDMI mode */
8016 tmp = I915_READ(aud_config);
8017 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
8018 /* clear N_programing_enable and N_value_index */
8019 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8020 I915_WRITE(aud_config, tmp);
8022 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8024 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8026 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8027 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8028 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8029 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
8031 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8034 if (intel_eld_uptodate(connector,
8035 aud_cntrl_st2, eldv,
8036 aud_cntl_st, IBX_ELD_ADDRESS,
8040 i = I915_READ(aud_cntrl_st2);
8042 I915_WRITE(aud_cntrl_st2, i);
8047 i = I915_READ(aud_cntl_st);
8048 i &= ~IBX_ELD_ADDRESS;
8049 I915_WRITE(aud_cntl_st, i);
8050 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8051 DRM_DEBUG_DRIVER("port num:%d\n", i);
8053 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8054 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8055 for (i = 0; i < len; i++)
8056 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8058 i = I915_READ(aud_cntrl_st2);
8060 I915_WRITE(aud_cntrl_st2, i);
8064 static void ironlake_write_eld(struct drm_connector *connector,
8065 struct drm_crtc *crtc,
8066 struct drm_display_mode *mode)
8068 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8069 uint8_t *eld = connector->eld;
8077 int pipe = to_intel_crtc(crtc)->pipe;
8079 if (HAS_PCH_IBX(connector->dev)) {
8080 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8081 aud_config = IBX_AUD_CFG(pipe);
8082 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
8083 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
8084 } else if (IS_VALLEYVIEW(connector->dev)) {
8085 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8086 aud_config = VLV_AUD_CFG(pipe);
8087 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8088 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
8090 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8091 aud_config = CPT_AUD_CFG(pipe);
8092 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
8093 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
8096 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8098 if (IS_VALLEYVIEW(connector->dev)) {
8099 struct intel_encoder *intel_encoder;
8100 struct intel_digital_port *intel_dig_port;
8102 intel_encoder = intel_attached_encoder(connector);
8103 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8104 i = intel_dig_port->port;
8106 i = I915_READ(aud_cntl_st);
8107 i = (i >> 29) & DIP_PORT_SEL_MASK;
8108 /* DIP_Port_Select, 0x1 = PortB */
8112 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8113 /* operate blindly on all ports */
8114 eldv = IBX_ELD_VALIDB;
8115 eldv |= IBX_ELD_VALIDB << 4;
8116 eldv |= IBX_ELD_VALIDB << 8;
8118 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
8119 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
8122 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8123 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8124 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8125 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
8127 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8130 if (intel_eld_uptodate(connector,
8131 aud_cntrl_st2, eldv,
8132 aud_cntl_st, IBX_ELD_ADDRESS,
8136 i = I915_READ(aud_cntrl_st2);
8138 I915_WRITE(aud_cntrl_st2, i);
8143 i = I915_READ(aud_cntl_st);
8144 i &= ~IBX_ELD_ADDRESS;
8145 I915_WRITE(aud_cntl_st, i);
8147 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8148 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8149 for (i = 0; i < len; i++)
8150 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8152 i = I915_READ(aud_cntrl_st2);
8154 I915_WRITE(aud_cntrl_st2, i);
8157 void intel_write_eld(struct drm_encoder *encoder,
8158 struct drm_display_mode *mode)
8160 struct drm_crtc *crtc = encoder->crtc;
8161 struct drm_connector *connector;
8162 struct drm_device *dev = encoder->dev;
8163 struct drm_i915_private *dev_priv = dev->dev_private;
8165 connector = drm_select_eld(encoder, mode);
8169 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8172 connector->encoder->base.id,
8173 connector->encoder->name);
8175 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8177 if (dev_priv->display.write_eld)
8178 dev_priv->display.write_eld(connector, crtc, mode);
8181 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8183 struct drm_device *dev = crtc->dev;
8184 struct drm_i915_private *dev_priv = dev->dev_private;
8185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8186 uint32_t cntl = 0, size = 0;
8189 unsigned int width = intel_crtc->cursor_width;
8190 unsigned int height = intel_crtc->cursor_height;
8191 unsigned int stride = roundup_pow_of_two(width) * 4;
8195 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8206 cntl |= CURSOR_ENABLE |
8207 CURSOR_GAMMA_ENABLE |
8208 CURSOR_FORMAT_ARGB |
8209 CURSOR_STRIDE(stride);
8211 size = (height << 12) | width;
8214 if (intel_crtc->cursor_cntl != 0 &&
8215 (intel_crtc->cursor_base != base ||
8216 intel_crtc->cursor_size != size ||
8217 intel_crtc->cursor_cntl != cntl)) {
8218 /* On these chipsets we can only modify the base/size/stride
8219 * whilst the cursor is disabled.
8221 I915_WRITE(_CURACNTR, 0);
8222 POSTING_READ(_CURACNTR);
8223 intel_crtc->cursor_cntl = 0;
8226 if (intel_crtc->cursor_base != base)
8227 I915_WRITE(_CURABASE, base);
8229 if (intel_crtc->cursor_size != size) {
8230 I915_WRITE(CURSIZE, size);
8231 intel_crtc->cursor_size = size;
8234 if (intel_crtc->cursor_cntl != cntl) {
8235 I915_WRITE(_CURACNTR, cntl);
8236 POSTING_READ(_CURACNTR);
8237 intel_crtc->cursor_cntl = cntl;
8241 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8243 struct drm_device *dev = crtc->dev;
8244 struct drm_i915_private *dev_priv = dev->dev_private;
8245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8246 int pipe = intel_crtc->pipe;
8251 cntl = MCURSOR_GAMMA_ENABLE;
8252 switch (intel_crtc->cursor_width) {
8254 cntl |= CURSOR_MODE_64_ARGB_AX;
8257 cntl |= CURSOR_MODE_128_ARGB_AX;
8260 cntl |= CURSOR_MODE_256_ARGB_AX;
8266 cntl |= pipe << 28; /* Connect to correct pipe */
8268 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8269 cntl |= CURSOR_PIPE_CSC_ENABLE;
8271 if (intel_crtc->cursor_cntl != cntl) {
8272 I915_WRITE(CURCNTR(pipe), cntl);
8273 POSTING_READ(CURCNTR(pipe));
8274 intel_crtc->cursor_cntl = cntl;
8277 /* and commit changes on next vblank */
8278 I915_WRITE(CURBASE(pipe), base);
8279 POSTING_READ(CURBASE(pipe));
8282 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
8283 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8286 struct drm_device *dev = crtc->dev;
8287 struct drm_i915_private *dev_priv = dev->dev_private;
8288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8289 int pipe = intel_crtc->pipe;
8290 int x = crtc->cursor_x;
8291 int y = crtc->cursor_y;
8292 u32 base = 0, pos = 0;
8295 base = intel_crtc->cursor_addr;
8297 if (x >= intel_crtc->config.pipe_src_w)
8300 if (y >= intel_crtc->config.pipe_src_h)
8304 if (x + intel_crtc->cursor_width <= 0)
8307 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8310 pos |= x << CURSOR_X_SHIFT;
8313 if (y + intel_crtc->cursor_height <= 0)
8316 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8319 pos |= y << CURSOR_Y_SHIFT;
8321 if (base == 0 && intel_crtc->cursor_base == 0)
8324 I915_WRITE(CURPOS(pipe), pos);
8326 if (IS_845G(dev) || IS_I865G(dev))
8327 i845_update_cursor(crtc, base);
8329 i9xx_update_cursor(crtc, base);
8330 intel_crtc->cursor_base = base;
8333 static bool cursor_size_ok(struct drm_device *dev,
8334 uint32_t width, uint32_t height)
8336 if (width == 0 || height == 0)
8340 * 845g/865g are special in that they are only limited by
8341 * the width of their cursors, the height is arbitrary up to
8342 * the precision of the register. Everything else requires
8343 * square cursors, limited to a few power-of-two sizes.
8345 if (IS_845G(dev) || IS_I865G(dev)) {
8346 if ((width & 63) != 0)
8349 if (width > (IS_845G(dev) ? 64 : 512))
8355 switch (width | height) {
8371 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8373 * Note that the object's reference will be consumed if the update fails. If
8374 * the update succeeds, the reference of the old object (if any) will be
8377 static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8378 struct drm_i915_gem_object *obj,
8379 uint32_t width, uint32_t height)
8381 struct drm_device *dev = crtc->dev;
8382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8383 enum i915_pipe pipe = intel_crtc->pipe;
8384 unsigned old_width, stride;
8388 /* if we want to turn off the cursor ignore width and height */
8390 DRM_DEBUG_KMS("cursor off\n");
8392 mutex_lock(&dev->struct_mutex);
8396 /* Check for which cursor types we support */
8397 if (!cursor_size_ok(dev, width, height)) {
8398 DRM_DEBUG("Cursor dimension not supported\n");
8402 stride = roundup_pow_of_two(width) * 4;
8403 if (obj->base.size < stride * height) {
8404 DRM_DEBUG_KMS("buffer is too small\n");
8409 /* we only need to pin inside GTT if cursor is non-phy */
8410 mutex_lock(&dev->struct_mutex);
8411 if (!INTEL_INFO(dev)->cursor_needs_physical) {
8414 if (obj->tiling_mode) {
8415 DRM_DEBUG_KMS("cursor cannot be tiled\n");
8420 /* Note that the w/a also requires 2 PTE of padding following
8421 * the bo. We currently fill all unused PTE with the shadow
8422 * page and so we should always have valid PTE following the
8423 * cursor preventing the VT-d warning.
8426 if (need_vtd_wa(dev))
8427 alignment = 64*1024;
8429 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
8431 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
8435 ret = i915_gem_object_put_fence(obj);
8437 DRM_DEBUG_KMS("failed to release fence for cursor");
8441 addr = i915_gem_obj_ggtt_offset(obj);
8443 int align = IS_I830(dev) ? 16 * 1024 : 256;
8444 ret = i915_gem_object_attach_phys(obj, align);
8446 DRM_DEBUG_KMS("failed to attach phys object\n");
8449 addr = obj->phys_handle->busaddr;
8453 if (intel_crtc->cursor_bo) {
8454 if (!INTEL_INFO(dev)->cursor_needs_physical)
8455 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
8458 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8459 INTEL_FRONTBUFFER_CURSOR(pipe));
8460 mutex_unlock(&dev->struct_mutex);
8462 old_width = intel_crtc->cursor_width;
8464 intel_crtc->cursor_addr = addr;
8465 intel_crtc->cursor_bo = obj;
8466 intel_crtc->cursor_width = width;
8467 intel_crtc->cursor_height = height;
8469 if (intel_crtc->active) {
8470 if (old_width != width)
8471 intel_update_watermarks(crtc);
8472 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
8475 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8479 i915_gem_object_unpin_from_display_plane(obj);
8481 mutex_unlock(&dev->struct_mutex);
8483 drm_gem_object_unreference_unlocked(&obj->base);
8487 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
8488 u16 *blue, uint32_t start, uint32_t size)
8490 int end = (start + size > 256) ? 256 : start + size, i;
8491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8493 for (i = start; i < end; i++) {
8494 intel_crtc->lut_r[i] = red[i] >> 8;
8495 intel_crtc->lut_g[i] = green[i] >> 8;
8496 intel_crtc->lut_b[i] = blue[i] >> 8;
8499 intel_crtc_load_lut(crtc);
8502 /* VESA 640x480x72Hz mode to set on the pipe */
8503 static struct drm_display_mode load_detect_mode = {
8504 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8505 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8508 struct drm_framebuffer *
8509 __intel_framebuffer_create(struct drm_device *dev,
8510 struct drm_mode_fb_cmd2 *mode_cmd,
8511 struct drm_i915_gem_object *obj)
8513 struct intel_framebuffer *intel_fb;
8516 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8518 drm_gem_object_unreference_unlocked(&obj->base);
8519 return ERR_PTR(-ENOMEM);
8522 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
8526 return &intel_fb->base;
8528 drm_gem_object_unreference_unlocked(&obj->base);
8531 return ERR_PTR(ret);
8534 static struct drm_framebuffer *
8535 intel_framebuffer_create(struct drm_device *dev,
8536 struct drm_mode_fb_cmd2 *mode_cmd,
8537 struct drm_i915_gem_object *obj)
8539 struct drm_framebuffer *fb;
8542 ret = i915_mutex_lock_interruptible(dev);
8544 return ERR_PTR(ret);
8545 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8546 mutex_unlock(&dev->struct_mutex);
8552 intel_framebuffer_pitch_for_width(int width, int bpp)
8554 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8555 return ALIGN(pitch, 64);
8559 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8561 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8562 return PAGE_ALIGN(pitch * mode->vdisplay);
8565 static struct drm_framebuffer *
8566 intel_framebuffer_create_for_mode(struct drm_device *dev,
8567 struct drm_display_mode *mode,
8570 struct drm_i915_gem_object *obj;
8571 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
8573 obj = i915_gem_alloc_object(dev,
8574 intel_framebuffer_size_for_mode(mode, bpp));
8576 return ERR_PTR(-ENOMEM);
8578 mode_cmd.width = mode->hdisplay;
8579 mode_cmd.height = mode->vdisplay;
8580 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8582 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
8584 return intel_framebuffer_create(dev, &mode_cmd, obj);
8587 static struct drm_framebuffer *
8588 mode_fits_in_fbdev(struct drm_device *dev,
8589 struct drm_display_mode *mode)
8591 #ifdef CONFIG_DRM_I915_FBDEV
8592 struct drm_i915_private *dev_priv = dev->dev_private;
8593 struct drm_i915_gem_object *obj;
8594 struct drm_framebuffer *fb;
8596 if (!dev_priv->fbdev)
8599 if (!dev_priv->fbdev->fb)
8602 obj = dev_priv->fbdev->fb->obj;
8605 fb = &dev_priv->fbdev->fb->base;
8606 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8607 fb->bits_per_pixel))
8610 if (obj->base.size < mode->vdisplay * fb->pitches[0])
8619 bool intel_get_load_detect_pipe(struct drm_connector *connector,
8620 struct drm_display_mode *mode,
8621 struct intel_load_detect_pipe *old,
8622 struct drm_modeset_acquire_ctx *ctx)
8624 struct intel_crtc *intel_crtc;
8625 struct intel_encoder *intel_encoder =
8626 intel_attached_encoder(connector);
8627 struct drm_crtc *possible_crtc;
8628 struct drm_encoder *encoder = &intel_encoder->base;
8629 struct drm_crtc *crtc = NULL;
8630 struct drm_device *dev = encoder->dev;
8631 struct drm_framebuffer *fb;
8632 struct drm_mode_config *config = &dev->mode_config;
8635 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8636 connector->base.id, connector->name,
8637 encoder->base.id, encoder->name);
8640 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8645 * Algorithm gets a little messy:
8647 * - if the connector already has an assigned crtc, use it (but make
8648 * sure it's on first)
8650 * - try to find the first unused crtc that can drive this connector,
8651 * and use that if we find one
8654 /* See if we already have a CRTC for this connector */
8655 if (encoder->crtc) {
8656 crtc = encoder->crtc;
8658 ret = drm_modeset_lock(&crtc->mutex, ctx);
8662 old->dpms_mode = connector->dpms;
8663 old->load_detect_temp = false;
8665 /* Make sure the crtc and connector are running */
8666 if (connector->dpms != DRM_MODE_DPMS_ON)
8667 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8672 /* Find an unused one (if possible) */
8673 for_each_crtc(dev, possible_crtc) {
8675 if (!(encoder->possible_crtcs & (1 << i)))
8677 if (possible_crtc->enabled)
8679 /* This can occur when applying the pipe A quirk on resume. */
8680 if (to_intel_crtc(possible_crtc)->new_enabled)
8683 crtc = possible_crtc;
8688 * If we didn't find an unused CRTC, don't use any.
8691 DRM_DEBUG_KMS("no pipe available for load-detect\n");
8695 ret = drm_modeset_lock(&crtc->mutex, ctx);
8698 intel_encoder->new_crtc = to_intel_crtc(crtc);
8699 to_intel_connector(connector)->new_encoder = intel_encoder;
8701 intel_crtc = to_intel_crtc(crtc);
8702 intel_crtc->new_enabled = true;
8703 intel_crtc->new_config = &intel_crtc->config;
8704 old->dpms_mode = connector->dpms;
8705 old->load_detect_temp = true;
8706 old->release_fb = NULL;
8709 mode = &load_detect_mode;
8711 /* We need a framebuffer large enough to accommodate all accesses
8712 * that the plane may generate whilst we perform load detection.
8713 * We can not rely on the fbcon either being present (we get called
8714 * during its initialisation to detect all boot displays, or it may
8715 * not even exist) or that it is large enough to satisfy the
8718 fb = mode_fits_in_fbdev(dev, mode);
8720 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
8721 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8722 old->release_fb = fb;
8724 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
8726 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
8730 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
8731 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
8732 if (old->release_fb)
8733 old->release_fb->funcs->destroy(old->release_fb);
8737 /* let the connector get through one full cycle before testing */
8738 intel_wait_for_vblank(dev, intel_crtc->pipe);
8742 intel_crtc->new_enabled = crtc->enabled;
8743 if (intel_crtc->new_enabled)
8744 intel_crtc->new_config = &intel_crtc->config;
8746 intel_crtc->new_config = NULL;
8748 if (ret == -EDEADLK) {
8749 drm_modeset_backoff(ctx);
8756 void intel_release_load_detect_pipe(struct drm_connector *connector,
8757 struct intel_load_detect_pipe *old)
8759 struct intel_encoder *intel_encoder =
8760 intel_attached_encoder(connector);
8761 struct drm_encoder *encoder = &intel_encoder->base;
8762 struct drm_crtc *crtc = encoder->crtc;
8763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8765 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8766 connector->base.id, connector->name,
8767 encoder->base.id, encoder->name);
8769 if (old->load_detect_temp) {
8770 to_intel_connector(connector)->new_encoder = NULL;
8771 intel_encoder->new_crtc = NULL;
8772 intel_crtc->new_enabled = false;
8773 intel_crtc->new_config = NULL;
8774 intel_set_mode(crtc, NULL, 0, 0, NULL);
8776 if (old->release_fb) {
8777 drm_framebuffer_unregister_private(old->release_fb);
8778 drm_framebuffer_unreference(old->release_fb);
8784 /* Switch crtc and encoder back off if necessary */
8785 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8786 connector->funcs->dpms(connector, old->dpms_mode);
8789 static int i9xx_pll_refclk(struct drm_device *dev,
8790 const struct intel_crtc_config *pipe_config)
8792 struct drm_i915_private *dev_priv = dev->dev_private;
8793 u32 dpll = pipe_config->dpll_hw_state.dpll;
8795 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
8796 return dev_priv->vbt.lvds_ssc_freq;
8797 else if (HAS_PCH_SPLIT(dev))
8799 else if (!IS_GEN2(dev))
8805 /* Returns the clock of the currently programmed mode of the given pipe. */
8806 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8807 struct intel_crtc_config *pipe_config)
8809 struct drm_device *dev = crtc->base.dev;
8810 struct drm_i915_private *dev_priv = dev->dev_private;
8811 int pipe = pipe_config->cpu_transcoder;
8812 u32 dpll = pipe_config->dpll_hw_state.dpll;
8814 intel_clock_t clock;
8815 int refclk = i9xx_pll_refclk(dev, pipe_config);
8817 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
8818 fp = pipe_config->dpll_hw_state.fp0;
8820 fp = pipe_config->dpll_hw_state.fp1;
8822 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
8823 if (IS_PINEVIEW(dev)) {
8824 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8825 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
8827 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8828 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8831 if (!IS_GEN2(dev)) {
8832 if (IS_PINEVIEW(dev))
8833 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8834 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
8836 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
8837 DPLL_FPA01_P1_POST_DIV_SHIFT);
8839 switch (dpll & DPLL_MODE_MASK) {
8840 case DPLLB_MODE_DAC_SERIAL:
8841 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8844 case DPLLB_MODE_LVDS:
8845 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8849 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
8850 "mode\n", (int)(dpll & DPLL_MODE_MASK));
8854 if (IS_PINEVIEW(dev))
8855 pineview_clock(refclk, &clock);
8857 i9xx_clock(refclk, &clock);
8859 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
8860 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
8863 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8864 DPLL_FPA01_P1_POST_DIV_SHIFT);
8866 if (lvds & LVDS_CLKB_POWER_UP)
8871 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8874 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8875 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8877 if (dpll & PLL_P2_DIVIDE_BY_4)
8883 i9xx_clock(refclk, &clock);
8887 * This value includes pixel_multiplier. We will use
8888 * port_clock to compute adjusted_mode.crtc_clock in the
8889 * encoder's get_config() function.
8891 pipe_config->port_clock = clock.dot;
8894 int intel_dotclock_calculate(int link_freq,
8895 const struct intel_link_m_n *m_n)
8898 * The calculation for the data clock is:
8899 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
8900 * But we want to avoid losing precison if possible, so:
8901 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
8903 * and the link clock is simpler:
8904 * link_clock = (m * link_clock) / n
8910 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8913 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8914 struct intel_crtc_config *pipe_config)
8916 struct drm_device *dev = crtc->base.dev;
8918 /* read out port_clock from the DPLL */
8919 i9xx_crtc_clock_get(crtc, pipe_config);
8922 * This value does not include pixel_multiplier.
8923 * We will check that port_clock and adjusted_mode.crtc_clock
8924 * agree once we know their relationship in the encoder's
8925 * get_config() function.
8927 pipe_config->adjusted_mode.crtc_clock =
8928 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8929 &pipe_config->fdi_m_n);
8932 /** Returns the currently programmed mode of the given pipe. */
8933 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8934 struct drm_crtc *crtc)
8936 struct drm_i915_private *dev_priv = dev->dev_private;
8937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8938 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8939 struct drm_display_mode *mode;
8940 struct intel_crtc_config pipe_config;
8941 int htot = I915_READ(HTOTAL(cpu_transcoder));
8942 int hsync = I915_READ(HSYNC(cpu_transcoder));
8943 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8944 int vsync = I915_READ(VSYNC(cpu_transcoder));
8945 enum i915_pipe pipe = intel_crtc->pipe;
8947 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8952 * Construct a pipe_config sufficient for getting the clock info
8953 * back out of crtc_clock_get.
8955 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8956 * to use a real value here instead.
8958 pipe_config.cpu_transcoder = (enum transcoder) pipe;
8959 pipe_config.pixel_multiplier = 1;
8960 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8961 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8962 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
8963 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8965 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
8966 mode->hdisplay = (htot & 0xffff) + 1;
8967 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8968 mode->hsync_start = (hsync & 0xffff) + 1;
8969 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8970 mode->vdisplay = (vtot & 0xffff) + 1;
8971 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8972 mode->vsync_start = (vsync & 0xffff) + 1;
8973 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8975 drm_mode_set_name(mode);
8980 static void intel_increase_pllclock(struct drm_device *dev,
8981 enum i915_pipe pipe)
8983 struct drm_i915_private *dev_priv = dev->dev_private;
8984 int dpll_reg = DPLL(pipe);
8987 if (!HAS_GMCH_DISPLAY(dev))
8990 if (!dev_priv->lvds_downclock_avail)
8993 dpll = I915_READ(dpll_reg);
8994 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
8995 DRM_DEBUG_DRIVER("upclocking LVDS\n");
8997 assert_panel_unlocked(dev_priv, pipe);
8999 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
9000 I915_WRITE(dpll_reg, dpll);
9001 intel_wait_for_vblank(dev, pipe);
9003 dpll = I915_READ(dpll_reg);
9004 if (dpll & DISPLAY_RATE_SELECT_FPA1)
9005 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
9009 static void intel_decrease_pllclock(struct drm_crtc *crtc)
9011 struct drm_device *dev = crtc->dev;
9012 struct drm_i915_private *dev_priv = dev->dev_private;
9013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9015 if (!HAS_GMCH_DISPLAY(dev))
9018 if (!dev_priv->lvds_downclock_avail)
9022 * Since this is called by a timer, we should never get here in
9025 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
9026 int pipe = intel_crtc->pipe;
9027 int dpll_reg = DPLL(pipe);
9030 DRM_DEBUG_DRIVER("downclocking LVDS\n");
9032 assert_panel_unlocked(dev_priv, pipe);
9034 dpll = I915_READ(dpll_reg);
9035 dpll |= DISPLAY_RATE_SELECT_FPA1;
9036 I915_WRITE(dpll_reg, dpll);
9037 intel_wait_for_vblank(dev, pipe);
9038 dpll = I915_READ(dpll_reg);
9039 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
9040 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
9045 void intel_mark_busy(struct drm_device *dev)
9047 struct drm_i915_private *dev_priv = dev->dev_private;
9049 if (dev_priv->mm.busy)
9052 intel_runtime_pm_get(dev_priv);
9053 i915_update_gfx_val(dev_priv);
9054 dev_priv->mm.busy = true;
9057 void intel_mark_idle(struct drm_device *dev)
9059 struct drm_i915_private *dev_priv = dev->dev_private;
9060 struct drm_crtc *crtc;
9062 if (!dev_priv->mm.busy)
9065 dev_priv->mm.busy = false;
9067 if (!i915.powersave)
9070 for_each_crtc(dev, crtc) {
9071 if (!crtc->primary->fb)
9074 intel_decrease_pllclock(crtc);
9077 if (INTEL_INFO(dev)->gen >= 6)
9078 gen6_rps_idle(dev->dev_private);
9081 intel_runtime_pm_put(dev_priv);
9086 * intel_mark_fb_busy - mark given planes as busy
9088 * @frontbuffer_bits: bits for the affected planes
9089 * @ring: optional ring for asynchronous commands
9091 * This function gets called every time the screen contents change. It can be
9092 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9094 static void intel_mark_fb_busy(struct drm_device *dev,
9095 unsigned frontbuffer_bits,
9096 struct intel_engine_cs *ring)
9098 struct drm_i915_private *dev_priv = dev->dev_private;
9099 enum i915_pipe pipe;
9101 if (!i915.powersave)
9104 for_each_pipe(dev_priv, pipe) {
9105 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
9108 intel_increase_pllclock(dev, pipe);
9109 if (ring && intel_fbc_enabled(dev))
9110 ring->fbc_dirty = true;
9115 * intel_fb_obj_invalidate - invalidate frontbuffer object
9116 * @obj: GEM object to invalidate
9117 * @ring: set for asynchronous rendering
9119 * This function gets called every time rendering on the given object starts and
9120 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9121 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9122 * until the rendering completes or a flip on this frontbuffer plane is
9125 void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9126 struct intel_engine_cs *ring)
9128 struct drm_device *dev = obj->base.dev;
9129 struct drm_i915_private *dev_priv = dev->dev_private;
9131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9133 if (!obj->frontbuffer_bits)
9137 mutex_lock(&dev_priv->fb_tracking.lock);
9138 dev_priv->fb_tracking.busy_bits
9139 |= obj->frontbuffer_bits;
9140 dev_priv->fb_tracking.flip_bits
9141 &= ~obj->frontbuffer_bits;
9142 mutex_unlock(&dev_priv->fb_tracking.lock);
9145 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9147 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
9151 * intel_frontbuffer_flush - flush frontbuffer
9153 * @frontbuffer_bits: frontbuffer plane tracking bits
9155 * This function gets called every time rendering on the given planes has
9156 * completed and frontbuffer caching can be started again. Flushes will get
9157 * delayed if they're blocked by some oustanding asynchronous rendering.
9159 * Can be called without any locks held.
9161 void intel_frontbuffer_flush(struct drm_device *dev,
9162 unsigned frontbuffer_bits)
9164 struct drm_i915_private *dev_priv = dev->dev_private;
9166 /* Delay flushing when rings are still busy.*/
9167 mutex_lock(&dev_priv->fb_tracking.lock);
9168 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9169 mutex_unlock(&dev_priv->fb_tracking.lock);
9171 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9173 intel_edp_psr_flush(dev, frontbuffer_bits);
9176 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9177 * needs to be reworked into a proper frontbuffer tracking scheme like
9180 if (IS_BROADWELL(dev))
9181 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
9185 * intel_fb_obj_flush - flush frontbuffer object
9186 * @obj: GEM object to flush
9187 * @retire: set when retiring asynchronous rendering
9189 * This function gets called every time rendering on the given object has
9190 * completed and frontbuffer caching can be started again. If @retire is true
9191 * then any delayed flushes will be unblocked.
9193 void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9196 struct drm_device *dev = obj->base.dev;
9197 struct drm_i915_private *dev_priv = dev->dev_private;
9198 unsigned frontbuffer_bits;
9200 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9202 if (!obj->frontbuffer_bits)
9205 frontbuffer_bits = obj->frontbuffer_bits;
9208 mutex_lock(&dev_priv->fb_tracking.lock);
9209 /* Filter out new bits since rendering started. */
9210 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9212 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9213 mutex_unlock(&dev_priv->fb_tracking.lock);
9216 intel_frontbuffer_flush(dev, frontbuffer_bits);
9220 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9222 * @frontbuffer_bits: frontbuffer plane tracking bits
9224 * This function gets called after scheduling a flip on @obj. The actual
9225 * frontbuffer flushing will be delayed until completion is signalled with
9226 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9227 * flush will be cancelled.
9229 * Can be called without any locks held.
9231 void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9232 unsigned frontbuffer_bits)
9234 struct drm_i915_private *dev_priv = dev->dev_private;
9236 mutex_lock(&dev_priv->fb_tracking.lock);
9237 dev_priv->fb_tracking.flip_bits
9238 |= frontbuffer_bits;
9239 mutex_unlock(&dev_priv->fb_tracking.lock);
9243 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9245 * @frontbuffer_bits: frontbuffer plane tracking bits
9247 * This function gets called after the flip has been latched and will complete
9248 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9250 * Can be called without any locks held.
9252 void intel_frontbuffer_flip_complete(struct drm_device *dev,
9253 unsigned frontbuffer_bits)
9255 struct drm_i915_private *dev_priv = dev->dev_private;
9257 mutex_lock(&dev_priv->fb_tracking.lock);
9258 /* Mask any cancelled flips. */
9259 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9260 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9261 mutex_unlock(&dev_priv->fb_tracking.lock);
9263 intel_frontbuffer_flush(dev, frontbuffer_bits);
9266 static void intel_crtc_destroy(struct drm_crtc *crtc)
9268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9269 struct drm_device *dev = crtc->dev;
9270 struct intel_unpin_work *work;
9272 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9273 work = intel_crtc->unpin_work;
9274 intel_crtc->unpin_work = NULL;
9275 lockmgr(&dev->event_lock, LK_RELEASE);
9278 cancel_work_sync(&work->work);
9282 drm_crtc_cleanup(crtc);
9287 static void intel_unpin_work_fn(struct work_struct *__work)
9289 struct intel_unpin_work *work =
9290 container_of(__work, struct intel_unpin_work, work);
9291 struct drm_device *dev = work->crtc->dev;
9292 enum i915_pipe pipe = to_intel_crtc(work->crtc)->pipe;
9294 mutex_lock(&dev->struct_mutex);
9295 intel_unpin_fb_obj(work->old_fb_obj);
9296 drm_gem_object_unreference(&work->pending_flip_obj->base);
9297 drm_gem_object_unreference(&work->old_fb_obj->base);
9299 intel_update_fbc(dev);
9300 mutex_unlock(&dev->struct_mutex);
9302 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9304 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9305 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9310 static void do_intel_finish_page_flip(struct drm_device *dev,
9311 struct drm_crtc *crtc)
9313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9314 struct intel_unpin_work *work;
9316 /* Ignore early vblank irqs */
9317 if (intel_crtc == NULL)
9320 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9321 work = intel_crtc->unpin_work;
9323 /* Ensure we don't miss a work->pending update ... */
9326 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
9327 lockmgr(&dev->event_lock, LK_RELEASE);
9331 page_flip_completed(intel_crtc);
9333 lockmgr(&dev->event_lock, LK_RELEASE);
9336 void intel_finish_page_flip(struct drm_device *dev, int pipe)
9338 struct drm_i915_private *dev_priv = dev->dev_private;
9339 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9341 do_intel_finish_page_flip(dev, crtc);
9344 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9346 struct drm_i915_private *dev_priv = dev->dev_private;
9347 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9349 do_intel_finish_page_flip(dev, crtc);
9352 /* Is 'a' after or equal to 'b'? */
9353 static bool g4x_flip_count_after_eq(u32 a, u32 b)
9355 return !((a - b) & 0x80000000);
9358 static bool page_flip_finished(struct intel_crtc *crtc)
9360 struct drm_device *dev = crtc->base.dev;
9361 struct drm_i915_private *dev_priv = dev->dev_private;
9363 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9364 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9368 * The relevant registers doen't exist on pre-ctg.
9369 * As the flip done interrupt doesn't trigger for mmio
9370 * flips on gmch platforms, a flip count check isn't
9371 * really needed there. But since ctg has the registers,
9372 * include it in the check anyway.
9374 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9378 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9379 * used the same base address. In that case the mmio flip might
9380 * have completed, but the CS hasn't even executed the flip yet.
9382 * A flip count check isn't enough as the CS might have updated
9383 * the base address just after start of vblank, but before we
9384 * managed to process the interrupt. This means we'd complete the
9387 * Combining both checks should get us a good enough result. It may
9388 * still happen that the CS flip has been executed, but has not
9389 * yet actually completed. But in case the base address is the same
9390 * anyway, we don't really care.
9392 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9393 crtc->unpin_work->gtt_offset &&
9394 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9395 crtc->unpin_work->flip_count);
9398 void intel_prepare_page_flip(struct drm_device *dev, int plane)
9400 struct drm_i915_private *dev_priv = dev->dev_private;
9401 struct intel_crtc *intel_crtc =
9402 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9404 /* NB: An MMIO update of the plane base pointer will also
9405 * generate a page-flip completion irq, i.e. every modeset
9406 * is also accompanied by a spurious intel_prepare_page_flip().
9408 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9409 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
9410 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
9411 lockmgr(&dev->event_lock, LK_RELEASE);
9414 static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
9416 /* Ensure that the work item is consistent when activating it ... */
9418 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9419 /* and that it is marked active as soon as the irq could fire. */
9423 static int intel_gen2_queue_flip(struct drm_device *dev,
9424 struct drm_crtc *crtc,
9425 struct drm_framebuffer *fb,
9426 struct drm_i915_gem_object *obj,
9427 struct intel_engine_cs *ring,
9430 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9434 ret = intel_ring_begin(ring, 6);
9438 /* Can't queue multiple flips, so wait for the previous
9439 * one to finish before executing the next.
9441 if (intel_crtc->plane)
9442 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9444 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
9445 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9446 intel_ring_emit(ring, MI_NOOP);
9447 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9448 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9449 intel_ring_emit(ring, fb->pitches[0]);
9450 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9451 intel_ring_emit(ring, 0); /* aux display base address, unused */
9453 intel_mark_page_flip_active(intel_crtc);
9454 __intel_ring_advance(ring);
9458 static int intel_gen3_queue_flip(struct drm_device *dev,
9459 struct drm_crtc *crtc,
9460 struct drm_framebuffer *fb,
9461 struct drm_i915_gem_object *obj,
9462 struct intel_engine_cs *ring,
9465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9469 ret = intel_ring_begin(ring, 6);
9473 if (intel_crtc->plane)
9474 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9476 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
9477 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9478 intel_ring_emit(ring, MI_NOOP);
9479 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9480 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9481 intel_ring_emit(ring, fb->pitches[0]);
9482 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9483 intel_ring_emit(ring, MI_NOOP);
9485 intel_mark_page_flip_active(intel_crtc);
9486 __intel_ring_advance(ring);
9490 static int intel_gen4_queue_flip(struct drm_device *dev,
9491 struct drm_crtc *crtc,
9492 struct drm_framebuffer *fb,
9493 struct drm_i915_gem_object *obj,
9494 struct intel_engine_cs *ring,
9497 struct drm_i915_private *dev_priv = dev->dev_private;
9498 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9499 uint32_t pf, pipesrc;
9502 ret = intel_ring_begin(ring, 4);
9506 /* i965+ uses the linear or tiled offsets from the
9507 * Display Registers (which do not change across a page-flip)
9508 * so we need only reprogram the base address.
9510 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9511 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9512 intel_ring_emit(ring, fb->pitches[0]);
9513 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
9516 /* XXX Enabling the panel-fitter across page-flip is so far
9517 * untested on non-native modes, so ignore it for now.
9518 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9521 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9522 intel_ring_emit(ring, pf | pipesrc);
9524 intel_mark_page_flip_active(intel_crtc);
9525 __intel_ring_advance(ring);
9529 static int intel_gen6_queue_flip(struct drm_device *dev,
9530 struct drm_crtc *crtc,
9531 struct drm_framebuffer *fb,
9532 struct drm_i915_gem_object *obj,
9533 struct intel_engine_cs *ring,
9536 struct drm_i915_private *dev_priv = dev->dev_private;
9537 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9538 uint32_t pf, pipesrc;
9541 ret = intel_ring_begin(ring, 4);
9545 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9546 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9547 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
9548 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9550 /* Contrary to the suggestions in the documentation,
9551 * "Enable Panel Fitter" does not seem to be required when page
9552 * flipping with a non-native mode, and worse causes a normal
9554 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9557 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9558 intel_ring_emit(ring, pf | pipesrc);
9560 intel_mark_page_flip_active(intel_crtc);
9561 __intel_ring_advance(ring);
9565 static int intel_gen7_queue_flip(struct drm_device *dev,
9566 struct drm_crtc *crtc,
9567 struct drm_framebuffer *fb,
9568 struct drm_i915_gem_object *obj,
9569 struct intel_engine_cs *ring,
9572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9573 uint32_t plane_bit = 0;
9576 switch (intel_crtc->plane) {
9578 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9581 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9584 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9587 WARN_ONCE(1, "unknown plane in flip command\n");
9592 if (ring->id == RCS) {
9595 * On Gen 8, SRM is now taking an extra dword to accommodate
9596 * 48bits addresses, and we need a NOOP for the batch size to
9604 * BSpec MI_DISPLAY_FLIP for IVB:
9605 * "The full packet must be contained within the same cache line."
9607 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9608 * cacheline, if we ever start emitting more commands before
9609 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9610 * then do the cacheline alignment, and finally emit the
9613 ret = intel_ring_cacheline_align(ring);
9617 ret = intel_ring_begin(ring, len);
9621 /* Unmask the flip-done completion message. Note that the bspec says that
9622 * we should do this for both the BCS and RCS, and that we must not unmask
9623 * more than one flip event at any time (or ensure that one flip message
9624 * can be sent by waiting for flip-done prior to queueing new flips).
9625 * Experimentation says that BCS works despite DERRMR masking all
9626 * flip-done completion events and that unmasking all planes at once
9627 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9628 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9630 if (ring->id == RCS) {
9631 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9632 intel_ring_emit(ring, DERRMR);
9633 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9634 DERRMR_PIPEB_PRI_FLIP_DONE |
9635 DERRMR_PIPEC_PRI_FLIP_DONE));
9637 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9638 MI_SRM_LRM_GLOBAL_GTT);
9640 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9641 MI_SRM_LRM_GLOBAL_GTT);
9642 intel_ring_emit(ring, DERRMR);
9643 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9645 intel_ring_emit(ring, 0);
9646 intel_ring_emit(ring, MI_NOOP);
9650 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
9651 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
9652 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9653 intel_ring_emit(ring, (MI_NOOP));
9655 intel_mark_page_flip_active(intel_crtc);
9656 __intel_ring_advance(ring);
9660 static bool use_mmio_flip(struct intel_engine_cs *ring,
9661 struct drm_i915_gem_object *obj)
9664 * This is not being used for older platforms, because
9665 * non-availability of flip done interrupt forces us to use
9666 * CS flips. Older platforms derive flip done using some clever
9667 * tricks involving the flip_pending status bits and vblank irqs.
9668 * So using MMIO flips there would disrupt this mechanism.
9674 if (INTEL_INFO(ring->dev)->gen < 5)
9677 if (i915.use_mmio_flip < 0)
9679 else if (i915.use_mmio_flip > 0)
9681 else if (i915.enable_execlists)
9684 return ring != obj->ring;
9687 static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9689 struct drm_device *dev = intel_crtc->base.dev;
9690 struct drm_i915_private *dev_priv = dev->dev_private;
9691 struct intel_framebuffer *intel_fb =
9692 to_intel_framebuffer(intel_crtc->base.primary->fb);
9693 struct drm_i915_gem_object *obj = intel_fb->obj;
9697 intel_mark_page_flip_active(intel_crtc);
9699 reg = DSPCNTR(intel_crtc->plane);
9700 dspcntr = I915_READ(reg);
9702 if (INTEL_INFO(dev)->gen >= 4) {
9703 if (obj->tiling_mode != I915_TILING_NONE)
9704 dspcntr |= DISPPLANE_TILED;
9706 dspcntr &= ~DISPPLANE_TILED;
9708 I915_WRITE(reg, dspcntr);
9710 I915_WRITE(DSPSURF(intel_crtc->plane),
9711 intel_crtc->unpin_work->gtt_offset);
9712 POSTING_READ(DSPSURF(intel_crtc->plane));
9715 static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9717 struct intel_engine_cs *ring;
9721 lockdep_assert_held(&obj->base.dev->struct_mutex);
9724 if (!obj->last_write_seqno)
9729 if (i915_seqno_passed(ring->get_seqno(ring, true),
9730 obj->last_write_seqno))
9733 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9737 if (WARN_ON(!ring->irq_get(ring)))
9743 void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9745 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9746 struct intel_crtc *intel_crtc;
9749 seqno = ring->get_seqno(ring, false);
9751 spin_lock(&dev_priv->mmio_flip_lock);
9752 for_each_intel_crtc(ring->dev, intel_crtc) {
9753 struct intel_mmio_flip *mmio_flip;
9755 mmio_flip = &intel_crtc->mmio_flip;
9756 if (mmio_flip->seqno == 0)
9759 if (ring->id != mmio_flip->ring_id)
9762 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9763 intel_do_mmio_flip(intel_crtc);
9764 mmio_flip->seqno = 0;
9765 ring->irq_put(ring);
9768 spin_unlock(&dev_priv->mmio_flip_lock);
9771 static int intel_queue_mmio_flip(struct drm_device *dev,
9772 struct drm_crtc *crtc,
9773 struct drm_framebuffer *fb,
9774 struct drm_i915_gem_object *obj,
9775 struct intel_engine_cs *ring,
9778 struct drm_i915_private *dev_priv = dev->dev_private;
9779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9782 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9785 ret = intel_postpone_flip(obj);
9789 intel_do_mmio_flip(intel_crtc);
9793 spin_lock(&dev_priv->mmio_flip_lock);
9794 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9795 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9796 spin_unlock(&dev_priv->mmio_flip_lock);
9799 * Double check to catch cases where irq fired before
9800 * mmio flip data was ready
9802 intel_notify_mmio_flip(obj->ring);
9806 static int intel_default_queue_flip(struct drm_device *dev,
9807 struct drm_crtc *crtc,
9808 struct drm_framebuffer *fb,
9809 struct drm_i915_gem_object *obj,
9810 struct intel_engine_cs *ring,
9816 static bool __intel_pageflip_stall_check(struct drm_device *dev,
9817 struct drm_crtc *crtc)
9819 struct drm_i915_private *dev_priv = dev->dev_private;
9820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9821 struct intel_unpin_work *work = intel_crtc->unpin_work;
9824 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9827 if (!work->enable_stall_check)
9830 if (work->flip_ready_vblank == 0) {
9831 if (work->flip_queued_ring &&
9832 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9833 work->flip_queued_seqno))
9836 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9839 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9842 /* Potential stall - if we see that the flip has happened,
9843 * assume a missed interrupt. */
9844 if (INTEL_INFO(dev)->gen >= 4)
9845 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9847 addr = I915_READ(DSPADDR(intel_crtc->plane));
9849 /* There is a potential issue here with a false positive after a flip
9850 * to the same address. We could address this by checking for a
9851 * non-incrementing frame counter.
9853 return addr == work->gtt_offset;
9856 void intel_check_page_flip(struct drm_device *dev, int pipe)
9858 struct drm_i915_private *dev_priv = dev->dev_private;
9859 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9865 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9866 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9867 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9868 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9869 page_flip_completed(intel_crtc);
9871 lockmgr(&dev->event_lock, LK_RELEASE);
9874 static int intel_crtc_page_flip(struct drm_crtc *crtc,
9875 struct drm_framebuffer *fb,
9876 struct drm_pending_vblank_event *event,
9877 uint32_t page_flip_flags)
9879 struct drm_device *dev = crtc->dev;
9880 struct drm_i915_private *dev_priv = dev->dev_private;
9881 struct drm_framebuffer *old_fb = crtc->primary->fb;
9882 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
9883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9884 enum i915_pipe pipe = intel_crtc->pipe;
9885 struct intel_unpin_work *work;
9886 struct intel_engine_cs *ring;
9890 * drm_mode_page_flip_ioctl() should already catch this, but double
9891 * check to be safe. In the future we may enable pageflipping from
9892 * a disabled primary plane.
9894 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9897 /* Can't change pixel format via MI display flips. */
9898 if (fb->pixel_format != crtc->primary->fb->pixel_format)
9902 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9903 * Note that pitch changes could also affect these register.
9905 if (INTEL_INFO(dev)->gen > 3 &&
9906 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9907 fb->pitches[0] != crtc->primary->fb->pitches[0]))
9910 if (i915_terminally_wedged(&dev_priv->gpu_error))
9913 work = kzalloc(sizeof(*work), GFP_KERNEL);
9917 work->event = event;
9919 work->old_fb_obj = intel_fb_obj(old_fb);
9920 INIT_WORK(&work->work, intel_unpin_work_fn);
9922 ret = drm_crtc_vblank_get(crtc);
9926 /* We borrow the event spin lock for protecting unpin_work */
9927 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
9928 if (intel_crtc->unpin_work) {
9929 /* Before declaring the flip queue wedged, check if
9930 * the hardware completed the operation behind our backs.
9932 if (__intel_pageflip_stall_check(dev, crtc)) {
9933 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9934 page_flip_completed(intel_crtc);
9936 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
9937 lockmgr(&dev->event_lock, LK_RELEASE);
9939 drm_crtc_vblank_put(crtc);
9944 intel_crtc->unpin_work = work;
9945 lockmgr(&dev->event_lock, LK_RELEASE);
9947 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9948 flush_workqueue(dev_priv->wq);
9950 ret = i915_mutex_lock_interruptible(dev);
9954 /* Reference the objects for the scheduled work. */
9955 drm_gem_object_reference(&work->old_fb_obj->base);
9956 drm_gem_object_reference(&obj->base);
9958 crtc->primary->fb = fb;
9960 work->pending_flip_obj = obj;
9962 atomic_inc(&intel_crtc->unpin_work_count);
9963 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
9965 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9966 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
9968 if (IS_VALLEYVIEW(dev)) {
9969 ring = &dev_priv->ring[BCS];
9970 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9971 /* vlv: DISPLAY_FLIP fails to change tiling */
9973 } else if (IS_IVYBRIDGE(dev)) {
9974 ring = &dev_priv->ring[BCS];
9975 } else if (INTEL_INFO(dev)->gen >= 7) {
9977 if (ring == NULL || ring->id != RCS)
9978 ring = &dev_priv->ring[BCS];
9980 ring = &dev_priv->ring[RCS];
9983 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
9985 goto cleanup_pending;
9988 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9990 if (use_mmio_flip(ring, obj)) {
9991 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9996 work->flip_queued_seqno = obj->last_write_seqno;
9997 work->flip_queued_ring = obj->ring;
9999 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
10002 goto cleanup_unpin;
10004 work->flip_queued_seqno = intel_ring_get_seqno(ring);
10005 work->flip_queued_ring = ring;
10008 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
10009 work->enable_stall_check = true;
10011 i915_gem_track_fb(work->old_fb_obj, obj,
10012 INTEL_FRONTBUFFER_PRIMARY(pipe));
10014 intel_disable_fbc(dev);
10015 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
10016 mutex_unlock(&dev->struct_mutex);
10018 trace_i915_flip_request(intel_crtc->plane, obj);
10023 intel_unpin_fb_obj(obj);
10025 atomic_dec(&intel_crtc->unpin_work_count);
10026 crtc->primary->fb = old_fb;
10027 drm_gem_object_unreference(&work->old_fb_obj->base);
10028 drm_gem_object_unreference(&obj->base);
10029 mutex_unlock(&dev->struct_mutex);
10032 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
10033 intel_crtc->unpin_work = NULL;
10034 lockmgr(&dev->event_lock, LK_RELEASE);
10036 drm_crtc_vblank_put(crtc);
10042 intel_crtc_wait_for_pending_flips(crtc);
10043 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
10044 if (ret == 0 && event) {
10045 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
10046 drm_send_vblank_event(dev, pipe, event);
10047 lockmgr(&dev->event_lock, LK_RELEASE);
10053 static struct drm_crtc_helper_funcs intel_helper_funcs = {
10054 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10055 .load_lut = intel_crtc_load_lut,
10059 * intel_modeset_update_staged_output_state
10061 * Updates the staged output configuration state, e.g. after we've read out the
10062 * current hw state.
10064 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10066 struct intel_crtc *crtc;
10067 struct intel_encoder *encoder;
10068 struct intel_connector *connector;
10070 list_for_each_entry(connector, &dev->mode_config.connector_list,
10072 connector->new_encoder =
10073 to_intel_encoder(connector->base.encoder);
10076 for_each_intel_encoder(dev, encoder) {
10077 encoder->new_crtc =
10078 to_intel_crtc(encoder->base.crtc);
10081 for_each_intel_crtc(dev, crtc) {
10082 crtc->new_enabled = crtc->base.enabled;
10084 if (crtc->new_enabled)
10085 crtc->new_config = &crtc->config;
10087 crtc->new_config = NULL;
10092 * intel_modeset_commit_output_state
10094 * This function copies the stage display pipe configuration to the real one.
10096 static void intel_modeset_commit_output_state(struct drm_device *dev)
10098 struct intel_crtc *crtc;
10099 struct intel_encoder *encoder;
10100 struct intel_connector *connector;
10102 list_for_each_entry(connector, &dev->mode_config.connector_list,
10104 connector->base.encoder = &connector->new_encoder->base;
10107 for_each_intel_encoder(dev, encoder) {
10108 encoder->base.crtc = &encoder->new_crtc->base;
10111 for_each_intel_crtc(dev, crtc) {
10112 crtc->base.enabled = crtc->new_enabled;
10117 connected_sink_compute_bpp(struct intel_connector *connector,
10118 struct intel_crtc_config *pipe_config)
10120 int bpp = pipe_config->pipe_bpp;
10122 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10123 connector->base.base.id,
10124 connector->base.name);
10126 /* Don't use an invalid EDID bpc value */
10127 if (connector->base.display_info.bpc &&
10128 connector->base.display_info.bpc * 3 < bpp) {
10129 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10130 bpp, connector->base.display_info.bpc*3);
10131 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10134 /* Clamp bpp to 8 on screens without EDID 1.4 */
10135 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10136 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10138 pipe_config->pipe_bpp = 24;
10143 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10144 struct drm_framebuffer *fb,
10145 struct intel_crtc_config *pipe_config)
10147 struct drm_device *dev = crtc->base.dev;
10148 struct intel_connector *connector;
10151 switch (fb->pixel_format) {
10152 case DRM_FORMAT_C8:
10153 bpp = 8*3; /* since we go through a colormap */
10155 case DRM_FORMAT_XRGB1555:
10156 case DRM_FORMAT_ARGB1555:
10157 /* checked in intel_framebuffer_init already */
10158 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10160 case DRM_FORMAT_RGB565:
10161 bpp = 6*3; /* min is 18bpp */
10163 case DRM_FORMAT_XBGR8888:
10164 case DRM_FORMAT_ABGR8888:
10165 /* checked in intel_framebuffer_init already */
10166 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10168 case DRM_FORMAT_XRGB8888:
10169 case DRM_FORMAT_ARGB8888:
10172 case DRM_FORMAT_XRGB2101010:
10173 case DRM_FORMAT_ARGB2101010:
10174 case DRM_FORMAT_XBGR2101010:
10175 case DRM_FORMAT_ABGR2101010:
10176 /* checked in intel_framebuffer_init already */
10177 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10181 /* TODO: gen4+ supports 16 bpc floating point, too. */
10183 DRM_DEBUG_KMS("unsupported depth\n");
10187 pipe_config->pipe_bpp = bpp;
10189 /* Clamp display bpp to EDID value */
10190 list_for_each_entry(connector, &dev->mode_config.connector_list,
10192 if (!connector->new_encoder ||
10193 connector->new_encoder->new_crtc != crtc)
10196 connected_sink_compute_bpp(connector, pipe_config);
10202 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10204 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10205 "type: 0x%x flags: 0x%x\n",
10207 mode->crtc_hdisplay, mode->crtc_hsync_start,
10208 mode->crtc_hsync_end, mode->crtc_htotal,
10209 mode->crtc_vdisplay, mode->crtc_vsync_start,
10210 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10213 static void intel_dump_pipe_config(struct intel_crtc *crtc,
10214 struct intel_crtc_config *pipe_config,
10215 const char *context)
10217 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10218 context, pipe_name(crtc->pipe));
10220 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10221 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10222 pipe_config->pipe_bpp, pipe_config->dither);
10223 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10224 pipe_config->has_pch_encoder,
10225 pipe_config->fdi_lanes,
10226 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10227 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10228 pipe_config->fdi_m_n.tu);
10229 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10230 pipe_config->has_dp_encoder,
10231 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10232 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10233 pipe_config->dp_m_n.tu);
10235 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10236 pipe_config->has_dp_encoder,
10237 pipe_config->dp_m2_n2.gmch_m,
10238 pipe_config->dp_m2_n2.gmch_n,
10239 pipe_config->dp_m2_n2.link_m,
10240 pipe_config->dp_m2_n2.link_n,
10241 pipe_config->dp_m2_n2.tu);
10243 DRM_DEBUG_KMS("requested mode:\n");
10244 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10245 DRM_DEBUG_KMS("adjusted mode:\n");
10246 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
10247 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
10248 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
10249 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10250 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
10251 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10252 pipe_config->gmch_pfit.control,
10253 pipe_config->gmch_pfit.pgm_ratios,
10254 pipe_config->gmch_pfit.lvds_border_bits);
10255 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
10256 pipe_config->pch_pfit.pos,
10257 pipe_config->pch_pfit.size,
10258 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
10259 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
10260 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
10263 static bool encoders_cloneable(const struct intel_encoder *a,
10264 const struct intel_encoder *b)
10266 /* masks could be asymmetric, so check both ways */
10267 return a == b || (a->cloneable & (1 << b->type) &&
10268 b->cloneable & (1 << a->type));
10271 static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10272 struct intel_encoder *encoder)
10274 struct drm_device *dev = crtc->base.dev;
10275 struct intel_encoder *source_encoder;
10277 for_each_intel_encoder(dev, source_encoder) {
10278 if (source_encoder->new_crtc != crtc)
10281 if (!encoders_cloneable(encoder, source_encoder))
10288 static bool check_encoder_cloning(struct intel_crtc *crtc)
10290 struct drm_device *dev = crtc->base.dev;
10291 struct intel_encoder *encoder;
10293 for_each_intel_encoder(dev, encoder) {
10294 if (encoder->new_crtc != crtc)
10297 if (!check_single_encoder_cloning(crtc, encoder))
10304 static struct intel_crtc_config *
10305 intel_modeset_pipe_config(struct drm_crtc *crtc,
10306 struct drm_framebuffer *fb,
10307 struct drm_display_mode *mode)
10309 struct drm_device *dev = crtc->dev;
10310 struct intel_encoder *encoder;
10311 struct intel_crtc_config *pipe_config;
10312 int plane_bpp, ret = -EINVAL;
10315 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
10316 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10317 return ERR_PTR(-EINVAL);
10320 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10322 return ERR_PTR(-ENOMEM);
10324 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10325 drm_mode_copy(&pipe_config->requested_mode, mode);
10327 pipe_config->cpu_transcoder =
10328 (enum transcoder) to_intel_crtc(crtc)->pipe;
10329 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10332 * Sanitize sync polarity flags based on requested ones. If neither
10333 * positive or negative polarity is requested, treat this as meaning
10334 * negative polarity.
10336 if (!(pipe_config->adjusted_mode.flags &
10337 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10338 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10340 if (!(pipe_config->adjusted_mode.flags &
10341 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10342 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10344 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10345 * plane pixel format and any sink constraints into account. Returns the
10346 * source plane bpp so that dithering can be selected on mismatches
10347 * after encoders and crtc also have had their say. */
10348 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10354 * Determine the real pipe dimensions. Note that stereo modes can
10355 * increase the actual pipe size due to the frame doubling and
10356 * insertion of additional space for blanks between the frame. This
10357 * is stored in the crtc timings. We use the requested mode to do this
10358 * computation to clearly distinguish it from the adjusted mode, which
10359 * can be changed by the connectors in the below retry loop.
10361 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10362 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10363 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10366 /* Ensure the port clock defaults are reset when retrying. */
10367 pipe_config->port_clock = 0;
10368 pipe_config->pixel_multiplier = 1;
10370 /* Fill in default crtc timings, allow encoders to overwrite them. */
10371 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
10373 /* Pass our mode to the connectors and the CRTC to give them a chance to
10374 * adjust it according to limitations or connector properties, and also
10375 * a chance to reject the mode entirely.
10377 for_each_intel_encoder(dev, encoder) {
10379 if (&encoder->new_crtc->base != crtc)
10382 if (!(encoder->compute_config(encoder, pipe_config))) {
10383 DRM_DEBUG_KMS("Encoder config failure\n");
10388 /* Set default port clock if not overwritten by the encoder. Needs to be
10389 * done afterwards in case the encoder adjusts the mode. */
10390 if (!pipe_config->port_clock)
10391 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10392 * pipe_config->pixel_multiplier;
10394 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
10396 DRM_DEBUG_KMS("CRTC fixup failed\n");
10400 if (ret == RETRY) {
10401 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10406 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10408 goto encoder_retry;
10411 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10412 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10413 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10415 return pipe_config;
10417 kfree(pipe_config);
10418 return ERR_PTR(ret);
10421 /* Computes which crtcs are affected and sets the relevant bits in the mask. For
10422 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10424 intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10425 unsigned *prepare_pipes, unsigned *disable_pipes)
10427 struct intel_crtc *intel_crtc;
10428 struct drm_device *dev = crtc->dev;
10429 struct intel_encoder *encoder;
10430 struct intel_connector *connector;
10431 struct drm_crtc *tmp_crtc;
10433 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10435 /* Check which crtcs have changed outputs connected to them, these need
10436 * to be part of the prepare_pipes mask. We don't (yet) support global
10437 * modeset across multiple crtcs, so modeset_pipes will only have one
10438 * bit set at most. */
10439 list_for_each_entry(connector, &dev->mode_config.connector_list,
10441 if (connector->base.encoder == &connector->new_encoder->base)
10444 if (connector->base.encoder) {
10445 tmp_crtc = connector->base.encoder->crtc;
10447 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10450 if (connector->new_encoder)
10452 1 << connector->new_encoder->new_crtc->pipe;
10455 for_each_intel_encoder(dev, encoder) {
10456 if (encoder->base.crtc == &encoder->new_crtc->base)
10459 if (encoder->base.crtc) {
10460 tmp_crtc = encoder->base.crtc;
10462 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10465 if (encoder->new_crtc)
10466 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10469 /* Check for pipes that will be enabled/disabled ... */
10470 for_each_intel_crtc(dev, intel_crtc) {
10471 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
10474 if (!intel_crtc->new_enabled)
10475 *disable_pipes |= 1 << intel_crtc->pipe;
10477 *prepare_pipes |= 1 << intel_crtc->pipe;
10481 /* set_mode is also used to update properties on life display pipes. */
10482 intel_crtc = to_intel_crtc(crtc);
10483 if (intel_crtc->new_enabled)
10484 *prepare_pipes |= 1 << intel_crtc->pipe;
10487 * For simplicity do a full modeset on any pipe where the output routing
10488 * changed. We could be more clever, but that would require us to be
10489 * more careful with calling the relevant encoder->mode_set functions.
10491 if (*prepare_pipes)
10492 *modeset_pipes = *prepare_pipes;
10494 /* ... and mask these out. */
10495 *modeset_pipes &= ~(*disable_pipes);
10496 *prepare_pipes &= ~(*disable_pipes);
10499 * HACK: We don't (yet) fully support global modesets. intel_set_config
10500 * obies this rule, but the modeset restore mode of
10501 * intel_modeset_setup_hw_state does not.
10503 *modeset_pipes &= 1 << intel_crtc->pipe;
10504 *prepare_pipes &= 1 << intel_crtc->pipe;
10506 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10507 *modeset_pipes, *prepare_pipes, *disable_pipes);
10510 static bool intel_crtc_in_use(struct drm_crtc *crtc)
10512 struct drm_encoder *encoder;
10513 struct drm_device *dev = crtc->dev;
10515 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10516 if (encoder->crtc == crtc)
10523 intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10525 struct intel_encoder *intel_encoder;
10526 struct intel_crtc *intel_crtc;
10527 struct drm_connector *connector;
10529 for_each_intel_encoder(dev, intel_encoder) {
10530 if (!intel_encoder->base.crtc)
10533 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10535 if (prepare_pipes & (1 << intel_crtc->pipe))
10536 intel_encoder->connectors_active = false;
10539 intel_modeset_commit_output_state(dev);
10541 /* Double check state. */
10542 for_each_intel_crtc(dev, intel_crtc) {
10543 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
10544 WARN_ON(intel_crtc->new_config &&
10545 intel_crtc->new_config != &intel_crtc->config);
10546 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
10549 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10550 if (!connector->encoder || !connector->encoder->crtc)
10553 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10555 if (prepare_pipes & (1 << intel_crtc->pipe)) {
10556 struct drm_property *dpms_property =
10557 dev->mode_config.dpms_property;
10559 connector->dpms = DRM_MODE_DPMS_ON;
10560 drm_object_property_set_value(&connector->base,
10564 intel_encoder = to_intel_encoder(connector->encoder);
10565 intel_encoder->connectors_active = true;
10571 static bool intel_fuzzy_clock_check(int clock1, int clock2)
10575 if (clock1 == clock2)
10578 if (!clock1 || !clock2)
10581 diff = abs(clock1 - clock2);
10583 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10589 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10590 list_for_each_entry((intel_crtc), \
10591 &(dev)->mode_config.crtc_list, \
10593 if (mask & (1 <<(intel_crtc)->pipe))
10596 intel_pipe_config_compare(struct drm_device *dev,
10597 struct intel_crtc_config *current_config,
10598 struct intel_crtc_config *pipe_config)
10600 #define PIPE_CONF_CHECK_X(name) \
10601 if (current_config->name != pipe_config->name) { \
10602 DRM_ERROR("mismatch in " #name " " \
10603 "(expected 0x%08x, found 0x%08x)\n", \
10604 current_config->name, \
10605 pipe_config->name); \
10609 #define PIPE_CONF_CHECK_I(name) \
10610 if (current_config->name != pipe_config->name) { \
10611 DRM_ERROR("mismatch in " #name " " \
10612 "(expected %i, found %i)\n", \
10613 current_config->name, \
10614 pipe_config->name); \
10618 /* This is required for BDW+ where there is only one set of registers for
10619 * switching between high and low RR.
10620 * This macro can be used whenever a comparison has to be made between one
10621 * hw state and multiple sw state variables.
10623 #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10624 if ((current_config->name != pipe_config->name) && \
10625 (current_config->alt_name != pipe_config->name)) { \
10626 DRM_ERROR("mismatch in " #name " " \
10627 "(expected %i or %i, found %i)\n", \
10628 current_config->name, \
10629 current_config->alt_name, \
10630 pipe_config->name); \
10634 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
10635 if ((current_config->name ^ pipe_config->name) & (mask)) { \
10636 DRM_ERROR("mismatch in " #name "(" #mask ") " \
10637 "(expected %i, found %i)\n", \
10638 current_config->name & (mask), \
10639 pipe_config->name & (mask)); \
10643 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10644 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10645 DRM_ERROR("mismatch in " #name " " \
10646 "(expected %i, found %i)\n", \
10647 current_config->name, \
10648 pipe_config->name); \
10652 #define PIPE_CONF_QUIRK(quirk) \
10653 ((current_config->quirks | pipe_config->quirks) & (quirk))
10655 PIPE_CONF_CHECK_I(cpu_transcoder);
10657 PIPE_CONF_CHECK_I(has_pch_encoder);
10658 PIPE_CONF_CHECK_I(fdi_lanes);
10659 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10660 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10661 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10662 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10663 PIPE_CONF_CHECK_I(fdi_m_n.tu);
10665 PIPE_CONF_CHECK_I(has_dp_encoder);
10667 if (INTEL_INFO(dev)->gen < 8) {
10668 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10669 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10670 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10671 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10672 PIPE_CONF_CHECK_I(dp_m_n.tu);
10674 if (current_config->has_drrs) {
10675 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10676 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10677 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10678 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10679 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10682 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10683 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10684 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10685 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10686 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10689 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10690 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10691 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10692 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10693 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10694 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10696 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10697 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10698 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10699 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10700 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10701 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10703 PIPE_CONF_CHECK_I(pixel_multiplier);
10704 PIPE_CONF_CHECK_I(has_hdmi_sink);
10705 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10706 IS_VALLEYVIEW(dev))
10707 PIPE_CONF_CHECK_I(limited_color_range);
10709 PIPE_CONF_CHECK_I(has_audio);
10711 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10712 DRM_MODE_FLAG_INTERLACE);
10714 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10715 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10716 DRM_MODE_FLAG_PHSYNC);
10717 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10718 DRM_MODE_FLAG_NHSYNC);
10719 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10720 DRM_MODE_FLAG_PVSYNC);
10721 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10722 DRM_MODE_FLAG_NVSYNC);
10725 PIPE_CONF_CHECK_I(pipe_src_w);
10726 PIPE_CONF_CHECK_I(pipe_src_h);
10729 * FIXME: BIOS likes to set up a cloned config with lvds+external
10730 * screen. Since we don't yet re-compute the pipe config when moving
10731 * just the lvds port away to another pipe the sw tracking won't match.
10733 * Proper atomic modesets with recomputed global state will fix this.
10734 * Until then just don't check gmch state for inherited modes.
10736 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10737 PIPE_CONF_CHECK_I(gmch_pfit.control);
10738 /* pfit ratios are autocomputed by the hw on gen4+ */
10739 if (INTEL_INFO(dev)->gen < 4)
10740 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10741 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10744 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10745 if (current_config->pch_pfit.enabled) {
10746 PIPE_CONF_CHECK_I(pch_pfit.pos);
10747 PIPE_CONF_CHECK_I(pch_pfit.size);
10750 /* BDW+ don't expose a synchronous way to read the state */
10751 if (IS_HASWELL(dev))
10752 PIPE_CONF_CHECK_I(ips_enabled);
10754 PIPE_CONF_CHECK_I(double_wide);
10756 PIPE_CONF_CHECK_X(ddi_pll_sel);
10758 PIPE_CONF_CHECK_I(shared_dpll);
10759 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
10760 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
10761 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10762 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
10763 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
10765 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10766 PIPE_CONF_CHECK_I(pipe_bpp);
10768 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10769 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
10771 #undef PIPE_CONF_CHECK_X
10772 #undef PIPE_CONF_CHECK_I
10773 #undef PIPE_CONF_CHECK_I_ALT
10774 #undef PIPE_CONF_CHECK_FLAGS
10775 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
10776 #undef PIPE_CONF_QUIRK
10782 check_connector_state(struct drm_device *dev)
10784 struct intel_connector *connector;
10786 list_for_each_entry(connector, &dev->mode_config.connector_list,
10788 /* This also checks the encoder/connector hw state with the
10789 * ->get_hw_state callbacks. */
10790 intel_connector_check_state(connector);
10792 WARN(&connector->new_encoder->base != connector->base.encoder,
10793 "connector's staged encoder doesn't match current encoder\n");
10798 check_encoder_state(struct drm_device *dev)
10800 struct intel_encoder *encoder;
10801 struct intel_connector *connector;
10803 for_each_intel_encoder(dev, encoder) {
10804 bool enabled = false;
10805 bool active = false;
10806 enum i915_pipe pipe, tracked_pipe;
10808 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10809 encoder->base.base.id,
10810 encoder->base.name);
10812 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10813 "encoder's stage crtc doesn't match current crtc\n");
10814 WARN(encoder->connectors_active && !encoder->base.crtc,
10815 "encoder's active_connectors set, but no crtc\n");
10817 list_for_each_entry(connector, &dev->mode_config.connector_list,
10819 if (connector->base.encoder != &encoder->base)
10822 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10825 WARN(!!encoder->base.crtc != enabled,
10826 "encoder's enabled state mismatch "
10827 "(expected %i, found %i)\n",
10828 !!encoder->base.crtc, enabled);
10829 WARN(active && !encoder->base.crtc,
10830 "active encoder with no crtc\n");
10832 WARN(encoder->connectors_active != active,
10833 "encoder's computed active state doesn't match tracked active state "
10834 "(expected %i, found %i)\n", active, encoder->connectors_active);
10836 active = encoder->get_hw_state(encoder, &pipe);
10837 WARN(active != encoder->connectors_active,
10838 "encoder's hw state doesn't match sw tracking "
10839 "(expected %i, found %i)\n",
10840 encoder->connectors_active, active);
10842 if (!encoder->base.crtc)
10845 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10846 WARN(active && pipe != tracked_pipe,
10847 "active encoder's pipe doesn't match"
10848 "(expected %i, found %i)\n",
10849 tracked_pipe, pipe);
10855 check_crtc_state(struct drm_device *dev)
10857 struct drm_i915_private *dev_priv = dev->dev_private;
10858 struct intel_crtc *crtc;
10859 struct intel_encoder *encoder;
10860 struct intel_crtc_config pipe_config;
10862 for_each_intel_crtc(dev, crtc) {
10863 bool enabled = false;
10864 bool active = false;
10866 memset(&pipe_config, 0, sizeof(pipe_config));
10868 DRM_DEBUG_KMS("[CRTC:%d]\n",
10869 crtc->base.base.id);
10871 WARN(crtc->active && !crtc->base.enabled,
10872 "active crtc, but not enabled in sw tracking\n");
10874 for_each_intel_encoder(dev, encoder) {
10875 if (encoder->base.crtc != &crtc->base)
10878 if (encoder->connectors_active)
10882 WARN(active != crtc->active,
10883 "crtc's computed active state doesn't match tracked active state "
10884 "(expected %i, found %i)\n", active, crtc->active);
10885 WARN(enabled != crtc->base.enabled,
10886 "crtc's computed enabled state doesn't match tracked enabled state "
10887 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10889 active = dev_priv->display.get_pipe_config(crtc,
10892 /* hw state is inconsistent with the pipe quirk */
10893 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10894 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
10895 active = crtc->active;
10897 for_each_intel_encoder(dev, encoder) {
10898 enum i915_pipe pipe;
10899 if (encoder->base.crtc != &crtc->base)
10901 if (encoder->get_hw_state(encoder, &pipe))
10902 encoder->get_config(encoder, &pipe_config);
10905 WARN(crtc->active != active,
10906 "crtc active state doesn't match with hw state "
10907 "(expected %i, found %i)\n", crtc->active, active);
10910 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10911 WARN(1, "pipe state doesn't match!\n");
10912 intel_dump_pipe_config(crtc, &pipe_config,
10914 intel_dump_pipe_config(crtc, &crtc->config,
10921 check_shared_dpll_state(struct drm_device *dev)
10923 struct drm_i915_private *dev_priv = dev->dev_private;
10924 struct intel_crtc *crtc;
10925 struct intel_dpll_hw_state dpll_hw_state;
10928 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10929 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10930 int enabled_crtcs = 0, active_crtcs = 0;
10933 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10935 DRM_DEBUG_KMS("%s\n", pll->name);
10937 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10939 WARN(pll->active > pll->refcount,
10940 "more active pll users than references: %i vs %i\n",
10941 pll->active, pll->refcount);
10942 WARN(pll->active && !pll->on,
10943 "pll in active use but not on in sw tracking\n");
10944 WARN(pll->on && !pll->active,
10945 "pll in on but not on in use in sw tracking\n");
10946 WARN(pll->on != active,
10947 "pll on state mismatch (expected %i, found %i)\n",
10950 for_each_intel_crtc(dev, crtc) {
10951 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10953 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10956 WARN(pll->active != active_crtcs,
10957 "pll active crtcs mismatch (expected %i, found %i)\n",
10958 pll->active, active_crtcs);
10959 WARN(pll->refcount != enabled_crtcs,
10960 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10961 pll->refcount, enabled_crtcs);
10963 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10964 sizeof(dpll_hw_state)),
10965 "pll hw state mismatch\n");
10970 intel_modeset_check_state(struct drm_device *dev)
10972 check_connector_state(dev);
10973 check_encoder_state(dev);
10974 check_crtc_state(dev);
10975 check_shared_dpll_state(dev);
10978 void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10982 * FDI already provided one idea for the dotclock.
10983 * Yell if the encoder disagrees.
10985 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
10986 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
10987 pipe_config->adjusted_mode.crtc_clock, dotclock);
10990 static void update_scanline_offset(struct intel_crtc *crtc)
10992 struct drm_device *dev = crtc->base.dev;
10995 * The scanline counter increments at the leading edge of hsync.
10997 * On most platforms it starts counting from vtotal-1 on the
10998 * first active line. That means the scanline counter value is
10999 * always one less than what we would expect. Ie. just after
11000 * start of vblank, which also occurs at start of hsync (on the
11001 * last active line), the scanline counter will read vblank_start-1.
11003 * On gen2 the scanline counter starts counting from 1 instead
11004 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11005 * to keep the value positive), instead of adding one.
11007 * On HSW+ the behaviour of the scanline counter depends on the output
11008 * type. For DP ports it behaves like most other platforms, but on HDMI
11009 * there's an extra 1 line difference. So we need to add two instead of
11010 * one to the value.
11012 if (IS_GEN2(dev)) {
11013 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
11016 vtotal = mode->crtc_vtotal;
11017 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11020 crtc->scanline_offset = vtotal - 1;
11021 } else if (HAS_DDI(dev) &&
11022 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
11023 crtc->scanline_offset = 2;
11025 crtc->scanline_offset = 1;
11028 static int __intel_set_mode(struct drm_crtc *crtc,
11029 struct drm_display_mode *mode,
11030 int x, int y, struct drm_framebuffer *fb)
11032 struct drm_device *dev = crtc->dev;
11033 struct drm_i915_private *dev_priv = dev->dev_private;
11034 struct drm_display_mode *saved_mode;
11035 struct intel_crtc_config *pipe_config = NULL;
11036 struct intel_crtc *intel_crtc;
11037 unsigned disable_pipes, prepare_pipes, modeset_pipes;
11040 saved_mode = kmalloc(sizeof(*saved_mode), M_DRM, M_WAITOK);
11044 intel_modeset_affected_pipes(crtc, &modeset_pipes,
11045 &prepare_pipes, &disable_pipes);
11047 *saved_mode = crtc->mode;
11049 /* Hack: Because we don't (yet) support global modeset on multiple
11050 * crtcs, we don't keep track of the new mode for more than one crtc.
11051 * Hence simply check whether any bit is set in modeset_pipes in all the
11052 * pieces of code that are not yet converted to deal with mutliple crtcs
11053 * changing their mode at the same time. */
11054 if (modeset_pipes) {
11055 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
11056 if (IS_ERR(pipe_config)) {
11057 ret = PTR_ERR(pipe_config);
11058 pipe_config = NULL;
11062 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11064 to_intel_crtc(crtc)->new_config = pipe_config;
11068 * See if the config requires any additional preparation, e.g.
11069 * to adjust global state with pipes off. We need to do this
11070 * here so we can get the modeset_pipe updated config for the new
11071 * mode set on this crtc. For other crtcs we need to use the
11072 * adjusted_mode bits in the crtc directly.
11074 if (IS_VALLEYVIEW(dev)) {
11075 valleyview_modeset_global_pipes(dev, &prepare_pipes);
11077 /* may have added more to prepare_pipes than we should */
11078 prepare_pipes &= ~disable_pipes;
11081 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11082 intel_crtc_disable(&intel_crtc->base);
11084 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11085 if (intel_crtc->base.enabled)
11086 dev_priv->display.crtc_disable(&intel_crtc->base);
11089 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11090 * to set it here already despite that we pass it down the callchain.
11092 if (modeset_pipes) {
11093 crtc->mode = *mode;
11094 /* mode_set/enable/disable functions rely on a correct pipe
11096 to_intel_crtc(crtc)->config = *pipe_config;
11097 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
11100 * Calculate and store various constants which
11101 * are later needed by vblank and swap-completion
11102 * timestamping. They are derived from true hwmode.
11104 drm_calc_timestamping_constants(crtc,
11105 &pipe_config->adjusted_mode);
11108 /* Only after disabling all output pipelines that will be changed can we
11109 * update the the output configuration. */
11110 intel_modeset_update_state(dev, prepare_pipes);
11112 if (dev_priv->display.modeset_global_resources)
11113 dev_priv->display.modeset_global_resources(dev);
11115 /* Set up the DPLL and any encoders state that needs to adjust or depend
11118 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11119 struct drm_framebuffer *old_fb = crtc->primary->fb;
11120 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11121 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11123 mutex_lock(&dev->struct_mutex);
11124 ret = intel_pin_and_fence_fb_obj(dev,
11128 DRM_ERROR("pin & fence failed\n");
11129 mutex_unlock(&dev->struct_mutex);
11133 intel_unpin_fb_obj(old_obj);
11134 i915_gem_track_fb(old_obj, obj,
11135 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11136 mutex_unlock(&dev->struct_mutex);
11138 crtc->primary->fb = fb;
11142 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11148 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
11149 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11150 update_scanline_offset(intel_crtc);
11152 dev_priv->display.crtc_enable(&intel_crtc->base);
11155 /* FIXME: add subpixel order */
11157 if (ret && crtc->enabled)
11158 crtc->mode = *saved_mode;
11161 kfree(pipe_config);
11166 static int intel_set_mode(struct drm_crtc *crtc,
11167 struct drm_display_mode *mode,
11168 int x, int y, struct drm_framebuffer *fb)
11172 ret = __intel_set_mode(crtc, mode, x, y, fb);
11175 intel_modeset_check_state(crtc->dev);
11180 void intel_crtc_restore_mode(struct drm_crtc *crtc)
11182 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
11185 #undef for_each_intel_crtc_masked
11187 static void intel_set_config_free(struct intel_set_config *config)
11192 kfree(config->save_connector_encoders);
11193 kfree(config->save_encoder_crtcs);
11194 kfree(config->save_crtc_enabled);
11198 static int intel_set_config_save_state(struct drm_device *dev,
11199 struct intel_set_config *config)
11201 struct drm_crtc *crtc;
11202 struct drm_encoder *encoder;
11203 struct drm_connector *connector;
11206 config->save_crtc_enabled =
11207 kcalloc(dev->mode_config.num_crtc,
11208 sizeof(bool), GFP_KERNEL);
11209 if (!config->save_crtc_enabled)
11212 config->save_encoder_crtcs =
11213 kcalloc(dev->mode_config.num_encoder,
11214 sizeof(struct drm_crtc *), GFP_KERNEL);
11215 if (!config->save_encoder_crtcs)
11218 config->save_connector_encoders =
11219 kcalloc(dev->mode_config.num_connector,
11220 sizeof(struct drm_encoder *), GFP_KERNEL);
11221 if (!config->save_connector_encoders)
11224 /* Copy data. Note that driver private data is not affected.
11225 * Should anything bad happen only the expected state is
11226 * restored, not the drivers personal bookkeeping.
11229 for_each_crtc(dev, crtc) {
11230 config->save_crtc_enabled[count++] = crtc->enabled;
11234 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
11235 config->save_encoder_crtcs[count++] = encoder->crtc;
11239 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11240 config->save_connector_encoders[count++] = connector->encoder;
11246 static void intel_set_config_restore_state(struct drm_device *dev,
11247 struct intel_set_config *config)
11249 struct intel_crtc *crtc;
11250 struct intel_encoder *encoder;
11251 struct intel_connector *connector;
11255 for_each_intel_crtc(dev, crtc) {
11256 crtc->new_enabled = config->save_crtc_enabled[count++];
11258 if (crtc->new_enabled)
11259 crtc->new_config = &crtc->config;
11261 crtc->new_config = NULL;
11265 for_each_intel_encoder(dev, encoder) {
11266 encoder->new_crtc =
11267 to_intel_crtc(config->save_encoder_crtcs[count++]);
11271 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11272 connector->new_encoder =
11273 to_intel_encoder(config->save_connector_encoders[count++]);
11278 is_crtc_connector_off(struct drm_mode_set *set)
11282 if (set->num_connectors == 0)
11285 if (WARN_ON(set->connectors == NULL))
11288 for (i = 0; i < set->num_connectors; i++)
11289 if (set->connectors[i]->encoder &&
11290 set->connectors[i]->encoder->crtc == set->crtc &&
11291 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
11298 intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11299 struct intel_set_config *config)
11302 /* We should be able to check here if the fb has the same properties
11303 * and then just flip_or_move it */
11304 if (is_crtc_connector_off(set)) {
11305 config->mode_changed = true;
11306 } else if (set->crtc->primary->fb != set->fb) {
11308 * If we have no fb, we can only flip as long as the crtc is
11309 * active, otherwise we need a full mode set. The crtc may
11310 * be active if we've only disabled the primary plane, or
11311 * in fastboot situations.
11313 if (set->crtc->primary->fb == NULL) {
11314 struct intel_crtc *intel_crtc =
11315 to_intel_crtc(set->crtc);
11317 if (intel_crtc->active) {
11318 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11319 config->fb_changed = true;
11321 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11322 config->mode_changed = true;
11324 } else if (set->fb == NULL) {
11325 config->mode_changed = true;
11326 } else if (set->fb->pixel_format !=
11327 set->crtc->primary->fb->pixel_format) {
11328 config->mode_changed = true;
11330 config->fb_changed = true;
11334 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
11335 config->fb_changed = true;
11337 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11338 DRM_DEBUG_KMS("modes are different, full mode set\n");
11339 drm_mode_debug_printmodeline(&set->crtc->mode);
11340 drm_mode_debug_printmodeline(set->mode);
11341 config->mode_changed = true;
11344 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11345 set->crtc->base.id, config->mode_changed, config->fb_changed);
11349 intel_modeset_stage_output_state(struct drm_device *dev,
11350 struct drm_mode_set *set,
11351 struct intel_set_config *config)
11353 struct intel_connector *connector;
11354 struct intel_encoder *encoder;
11355 struct intel_crtc *crtc;
11358 /* The upper layers ensure that we either disable a crtc or have a list
11359 * of connectors. For paranoia, double-check this. */
11360 WARN_ON(!set->fb && (set->num_connectors != 0));
11361 WARN_ON(set->fb && (set->num_connectors == 0));
11363 list_for_each_entry(connector, &dev->mode_config.connector_list,
11365 /* Otherwise traverse passed in connector list and get encoders
11367 for (ro = 0; ro < set->num_connectors; ro++) {
11368 if (set->connectors[ro] == &connector->base) {
11369 connector->new_encoder = connector->encoder;
11374 /* If we disable the crtc, disable all its connectors. Also, if
11375 * the connector is on the changing crtc but not on the new
11376 * connector list, disable it. */
11377 if ((!set->fb || ro == set->num_connectors) &&
11378 connector->base.encoder &&
11379 connector->base.encoder->crtc == set->crtc) {
11380 connector->new_encoder = NULL;
11382 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11383 connector->base.base.id,
11384 connector->base.name);
11388 if (&connector->new_encoder->base != connector->base.encoder) {
11389 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
11390 config->mode_changed = true;
11393 /* connector->new_encoder is now updated for all connectors. */
11395 /* Update crtc of enabled connectors. */
11396 list_for_each_entry(connector, &dev->mode_config.connector_list,
11398 struct drm_crtc *new_crtc;
11400 if (!connector->new_encoder)
11403 new_crtc = connector->new_encoder->base.crtc;
11405 for (ro = 0; ro < set->num_connectors; ro++) {
11406 if (set->connectors[ro] == &connector->base)
11407 new_crtc = set->crtc;
11410 /* Make sure the new CRTC will work with the encoder */
11411 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11415 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
11417 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11418 connector->base.base.id,
11419 connector->base.name,
11420 new_crtc->base.id);
11423 /* Check for any encoders that needs to be disabled. */
11424 for_each_intel_encoder(dev, encoder) {
11425 int num_connectors = 0;
11426 list_for_each_entry(connector,
11427 &dev->mode_config.connector_list,
11429 if (connector->new_encoder == encoder) {
11430 WARN_ON(!connector->new_encoder->new_crtc);
11435 if (num_connectors == 0)
11436 encoder->new_crtc = NULL;
11437 else if (num_connectors > 1)
11440 /* Only now check for crtc changes so we don't miss encoders
11441 * that will be disabled. */
11442 if (&encoder->new_crtc->base != encoder->base.crtc) {
11443 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
11444 config->mode_changed = true;
11447 /* Now we've also updated encoder->new_crtc for all encoders. */
11449 for_each_intel_crtc(dev, crtc) {
11450 crtc->new_enabled = false;
11452 for_each_intel_encoder(dev, encoder) {
11453 if (encoder->new_crtc == crtc) {
11454 crtc->new_enabled = true;
11459 if (crtc->new_enabled != crtc->base.enabled) {
11460 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11461 crtc->new_enabled ? "en" : "dis");
11462 config->mode_changed = true;
11465 if (crtc->new_enabled)
11466 crtc->new_config = &crtc->config;
11468 crtc->new_config = NULL;
11474 static void disable_crtc_nofb(struct intel_crtc *crtc)
11476 struct drm_device *dev = crtc->base.dev;
11477 struct intel_encoder *encoder;
11478 struct intel_connector *connector;
11480 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11481 pipe_name(crtc->pipe));
11483 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11484 if (connector->new_encoder &&
11485 connector->new_encoder->new_crtc == crtc)
11486 connector->new_encoder = NULL;
11489 for_each_intel_encoder(dev, encoder) {
11490 if (encoder->new_crtc == crtc)
11491 encoder->new_crtc = NULL;
11494 crtc->new_enabled = false;
11495 crtc->new_config = NULL;
11498 static int intel_crtc_set_config(struct drm_mode_set *set)
11500 struct drm_device *dev;
11501 struct drm_mode_set save_set;
11502 struct intel_set_config *config;
11506 BUG_ON(!set->crtc);
11507 BUG_ON(!set->crtc->helper_private);
11509 /* Enforce sane interface api - has been abused by the fb helper. */
11510 BUG_ON(!set->mode && set->fb);
11511 BUG_ON(set->fb && set->num_connectors == 0);
11514 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11515 set->crtc->base.id, set->fb->base.id,
11516 (int)set->num_connectors, set->x, set->y);
11518 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
11521 dev = set->crtc->dev;
11524 config = kzalloc(sizeof(*config), GFP_KERNEL);
11528 ret = intel_set_config_save_state(dev, config);
11532 save_set.crtc = set->crtc;
11533 save_set.mode = &set->crtc->mode;
11534 save_set.x = set->crtc->x;
11535 save_set.y = set->crtc->y;
11536 save_set.fb = set->crtc->primary->fb;
11538 /* Compute whether we need a full modeset, only an fb base update or no
11539 * change at all. In the future we might also check whether only the
11540 * mode changed, e.g. for LVDS where we only change the panel fitter in
11542 intel_set_config_compute_mode_changes(set, config);
11544 ret = intel_modeset_stage_output_state(dev, set, config);
11548 if (config->mode_changed) {
11549 ret = intel_set_mode(set->crtc, set->mode,
11550 set->x, set->y, set->fb);
11551 } else if (config->fb_changed) {
11552 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11554 intel_crtc_wait_for_pending_flips(set->crtc);
11556 ret = intel_pipe_set_base(set->crtc,
11557 set->x, set->y, set->fb);
11560 * We need to make sure the primary plane is re-enabled if it
11561 * has previously been turned off.
11563 if (!intel_crtc->primary_enabled && ret == 0) {
11564 WARN_ON(!intel_crtc->active);
11565 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
11569 * In the fastboot case this may be our only check of the
11570 * state after boot. It would be better to only do it on
11571 * the first update, but we don't have a nice way of doing that
11572 * (and really, set_config isn't used much for high freq page
11573 * flipping, so increasing its cost here shouldn't be a big
11576 if (i915.fastboot && ret == 0)
11577 intel_modeset_check_state(set->crtc->dev);
11581 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11582 set->crtc->base.id, ret);
11584 intel_set_config_restore_state(dev, config);
11587 * HACK: if the pipe was on, but we didn't have a framebuffer,
11588 * force the pipe off to avoid oopsing in the modeset code
11589 * due to fb==NULL. This should only happen during boot since
11590 * we don't yet reconstruct the FB from the hardware state.
11592 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11593 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11595 /* Try to restore the config */
11596 if (config->mode_changed &&
11597 intel_set_mode(save_set.crtc, save_set.mode,
11598 save_set.x, save_set.y, save_set.fb))
11599 DRM_ERROR("failed to restore config after modeset failure\n");
11603 intel_set_config_free(config);
11607 static const struct drm_crtc_funcs intel_crtc_funcs = {
11608 .gamma_set = intel_crtc_gamma_set,
11609 .set_config = intel_crtc_set_config,
11610 .destroy = intel_crtc_destroy,
11611 .page_flip = intel_crtc_page_flip,
11614 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11615 struct intel_shared_dpll *pll,
11616 struct intel_dpll_hw_state *hw_state)
11620 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11623 val = I915_READ(PCH_DPLL(pll->id));
11624 hw_state->dpll = val;
11625 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11626 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
11628 return val & DPLL_VCO_ENABLE;
11631 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11632 struct intel_shared_dpll *pll)
11634 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11635 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11638 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11639 struct intel_shared_dpll *pll)
11641 /* PCH refclock must be enabled first */
11642 ibx_assert_pch_refclk_enabled(dev_priv);
11644 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11646 /* Wait for the clocks to stabilize. */
11647 POSTING_READ(PCH_DPLL(pll->id));
11650 /* The pixel multiplier can only be updated once the
11651 * DPLL is enabled and the clocks are stable.
11653 * So write it again.
11655 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11656 POSTING_READ(PCH_DPLL(pll->id));
11660 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11661 struct intel_shared_dpll *pll)
11663 struct drm_device *dev = dev_priv->dev;
11664 struct intel_crtc *crtc;
11666 /* Make sure no transcoder isn't still depending on us. */
11667 for_each_intel_crtc(dev, crtc) {
11668 if (intel_crtc_to_shared_dpll(crtc) == pll)
11669 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11672 I915_WRITE(PCH_DPLL(pll->id), 0);
11673 POSTING_READ(PCH_DPLL(pll->id));
11677 static char *ibx_pch_dpll_names[] = {
11682 static void ibx_pch_dpll_init(struct drm_device *dev)
11684 struct drm_i915_private *dev_priv = dev->dev_private;
11687 dev_priv->num_shared_dpll = 2;
11689 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11690 dev_priv->shared_dplls[i].id = i;
11691 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
11692 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
11693 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11694 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
11695 dev_priv->shared_dplls[i].get_hw_state =
11696 ibx_pch_dpll_get_hw_state;
11700 static void intel_shared_dpll_init(struct drm_device *dev)
11702 struct drm_i915_private *dev_priv = dev->dev_private;
11705 intel_ddi_pll_init(dev);
11706 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11707 ibx_pch_dpll_init(dev);
11709 dev_priv->num_shared_dpll = 0;
11711 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
11715 intel_primary_plane_disable(struct drm_plane *plane)
11717 struct drm_device *dev = plane->dev;
11718 struct intel_crtc *intel_crtc;
11723 BUG_ON(!plane->crtc);
11725 intel_crtc = to_intel_crtc(plane->crtc);
11728 * Even though we checked plane->fb above, it's still possible that
11729 * the primary plane has been implicitly disabled because the crtc
11730 * coordinates given weren't visible, or because we detected
11731 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11732 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11733 * In either case, we need to unpin the FB and let the fb pointer get
11734 * updated, but otherwise we don't need to touch the hardware.
11736 if (!intel_crtc->primary_enabled)
11737 goto disable_unpin;
11739 intel_crtc_wait_for_pending_flips(plane->crtc);
11740 intel_disable_primary_hw_plane(plane, plane->crtc);
11743 mutex_lock(&dev->struct_mutex);
11744 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
11745 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11746 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
11747 mutex_unlock(&dev->struct_mutex);
11754 intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11755 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11756 unsigned int crtc_w, unsigned int crtc_h,
11757 uint32_t src_x, uint32_t src_y,
11758 uint32_t src_w, uint32_t src_h)
11760 struct drm_device *dev = crtc->dev;
11761 struct drm_i915_private *dev_priv = dev->dev_private;
11762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11763 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11764 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11765 struct drm_rect dest = {
11766 /* integer pixels */
11769 .x2 = crtc_x + crtc_w,
11770 .y2 = crtc_y + crtc_h,
11772 struct drm_rect src = {
11773 /* 16.16 fixed point */
11776 .x2 = src_x + src_w,
11777 .y2 = src_y + src_h,
11779 const struct drm_rect clip = {
11780 /* integer pixels */
11781 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11782 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11785 int crtc_x, crtc_y;
11786 unsigned int crtc_w, crtc_h;
11787 uint32_t src_x, src_y, src_w, src_h;
11798 struct intel_plane *intel_plane = to_intel_plane(plane);
11802 ret = drm_plane_helper_check_update(plane, crtc, fb,
11803 &src, &dest, &clip,
11804 DRM_PLANE_HELPER_NO_SCALING,
11805 DRM_PLANE_HELPER_NO_SCALING,
11806 false, true, &visible);
11812 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11813 * updating the fb pointer, and returning without touching the
11814 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11815 * turn on the display with all planes setup as desired.
11817 if (!crtc->enabled) {
11818 mutex_lock(&dev->struct_mutex);
11821 * If we already called setplane while the crtc was disabled,
11822 * we may have an fb pinned; unpin it.
11825 intel_unpin_fb_obj(old_obj);
11827 i915_gem_track_fb(old_obj, obj,
11828 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11830 /* Pin and return without programming hardware */
11831 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11832 mutex_unlock(&dev->struct_mutex);
11837 intel_crtc_wait_for_pending_flips(crtc);
11840 * If clipping results in a non-visible primary plane, we'll disable
11841 * the primary plane. Note that this is a bit different than what
11842 * happens if userspace explicitly disables the plane by passing fb=0
11843 * because plane->fb still gets set and pinned.
11846 mutex_lock(&dev->struct_mutex);
11849 * Try to pin the new fb first so that we can bail out if we
11852 if (plane->fb != fb) {
11853 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11855 mutex_unlock(&dev->struct_mutex);
11860 i915_gem_track_fb(old_obj, obj,
11861 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11863 if (intel_crtc->primary_enabled)
11864 intel_disable_primary_hw_plane(plane, crtc);
11867 if (plane->fb != fb)
11869 intel_unpin_fb_obj(old_obj);
11871 mutex_unlock(&dev->struct_mutex);
11874 if (intel_crtc && intel_crtc->active &&
11875 intel_crtc->primary_enabled) {
11877 * FBC does not work on some platforms for rotated
11878 * planes, so disable it when rotation is not 0 and
11879 * update it when rotation is set back to 0.
11881 * FIXME: This is redundant with the fbc update done in
11882 * the primary plane enable function except that that
11883 * one is done too late. We eventually need to unify
11886 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11887 dev_priv->fbc.plane == intel_crtc->plane &&
11888 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11889 intel_disable_fbc(dev);
11892 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11896 if (!intel_crtc->primary_enabled)
11897 intel_enable_primary_hw_plane(plane, crtc);
11900 intel_plane->crtc_x = orig.crtc_x;
11901 intel_plane->crtc_y = orig.crtc_y;
11902 intel_plane->crtc_w = orig.crtc_w;
11903 intel_plane->crtc_h = orig.crtc_h;
11904 intel_plane->src_x = orig.src_x;
11905 intel_plane->src_y = orig.src_y;
11906 intel_plane->src_w = orig.src_w;
11907 intel_plane->src_h = orig.src_h;
11908 intel_plane->obj = obj;
11913 /* Common destruction function for both primary and cursor planes */
11914 static void intel_plane_destroy(struct drm_plane *plane)
11916 struct intel_plane *intel_plane = to_intel_plane(plane);
11917 drm_plane_cleanup(plane);
11918 kfree(intel_plane);
11921 static const struct drm_plane_funcs intel_primary_plane_funcs = {
11922 .update_plane = intel_primary_plane_setplane,
11923 .disable_plane = intel_primary_plane_disable,
11924 .destroy = intel_plane_destroy,
11925 .set_property = intel_plane_set_property
11928 static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11931 struct intel_plane *primary;
11932 const uint32_t *intel_primary_formats;
11935 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11936 if (primary == NULL)
11939 primary->can_scale = false;
11940 primary->max_downscale = 1;
11941 primary->pipe = pipe;
11942 primary->plane = pipe;
11943 primary->rotation = BIT(DRM_ROTATE_0);
11944 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11945 primary->plane = !pipe;
11947 if (INTEL_INFO(dev)->gen <= 3) {
11948 intel_primary_formats = intel_primary_formats_gen2;
11949 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11951 intel_primary_formats = intel_primary_formats_gen4;
11952 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11955 drm_universal_plane_init(dev, &primary->base, 0,
11956 &intel_primary_plane_funcs,
11957 intel_primary_formats, num_formats,
11958 DRM_PLANE_TYPE_PRIMARY);
11960 if (INTEL_INFO(dev)->gen >= 4) {
11961 if (!dev->mode_config.rotation_property)
11962 dev->mode_config.rotation_property =
11963 drm_mode_create_rotation_property(dev,
11964 BIT(DRM_ROTATE_0) |
11965 BIT(DRM_ROTATE_180));
11966 if (dev->mode_config.rotation_property)
11967 drm_object_attach_property(&primary->base.base,
11968 dev->mode_config.rotation_property,
11969 primary->rotation);
11972 return &primary->base;
11976 intel_cursor_plane_disable(struct drm_plane *plane)
11981 BUG_ON(!plane->crtc);
11983 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11987 intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11988 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11989 unsigned int crtc_w, unsigned int crtc_h,
11990 uint32_t src_x, uint32_t src_y,
11991 uint32_t src_w, uint32_t src_h)
11993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11994 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11995 struct drm_i915_gem_object *obj = intel_fb->obj;
11996 struct drm_rect dest = {
11997 /* integer pixels */
12000 .x2 = crtc_x + crtc_w,
12001 .y2 = crtc_y + crtc_h,
12003 struct drm_rect src = {
12004 /* 16.16 fixed point */
12007 .x2 = src_x + src_w,
12008 .y2 = src_y + src_h,
12010 const struct drm_rect clip = {
12011 /* integer pixels */
12012 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
12013 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
12018 ret = drm_plane_helper_check_update(plane, crtc, fb,
12019 &src, &dest, &clip,
12020 DRM_PLANE_HELPER_NO_SCALING,
12021 DRM_PLANE_HELPER_NO_SCALING,
12022 true, true, &visible);
12026 crtc->cursor_x = crtc_x;
12027 crtc->cursor_y = crtc_y;
12028 if (fb != crtc->cursor->fb) {
12029 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
12031 intel_crtc_update_cursor(crtc, visible);
12033 intel_frontbuffer_flip(crtc->dev,
12034 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
12039 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12040 .update_plane = intel_cursor_plane_update,
12041 .disable_plane = intel_cursor_plane_disable,
12042 .destroy = intel_plane_destroy,
12045 static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12048 struct intel_plane *cursor;
12050 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12051 if (cursor == NULL)
12054 cursor->can_scale = false;
12055 cursor->max_downscale = 1;
12056 cursor->pipe = pipe;
12057 cursor->plane = pipe;
12059 drm_universal_plane_init(dev, &cursor->base, 0,
12060 &intel_cursor_plane_funcs,
12061 intel_cursor_formats,
12062 ARRAY_SIZE(intel_cursor_formats),
12063 DRM_PLANE_TYPE_CURSOR);
12064 return &cursor->base;
12067 static void intel_crtc_init(struct drm_device *dev, int pipe)
12069 struct drm_i915_private *dev_priv = dev->dev_private;
12070 struct intel_crtc *intel_crtc;
12071 struct drm_plane *primary = NULL;
12072 struct drm_plane *cursor = NULL;
12075 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
12076 if (intel_crtc == NULL)
12079 primary = intel_primary_plane_create(dev, pipe);
12083 cursor = intel_cursor_plane_create(dev, pipe);
12087 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
12088 cursor, &intel_crtc_funcs);
12092 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
12093 for (i = 0; i < 256; i++) {
12094 intel_crtc->lut_r[i] = i;
12095 intel_crtc->lut_g[i] = i;
12096 intel_crtc->lut_b[i] = i;
12100 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
12101 * is hooked to pipe B. Hence we want plane A feeding pipe B.
12103 intel_crtc->pipe = pipe;
12104 intel_crtc->plane = pipe;
12105 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
12106 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
12107 intel_crtc->plane = !pipe;
12110 intel_crtc->cursor_base = ~0;
12111 intel_crtc->cursor_cntl = ~0;
12112 intel_crtc->cursor_size = ~0;
12114 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12115 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12116 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12117 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12119 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
12121 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
12126 drm_plane_cleanup(primary);
12128 drm_plane_cleanup(cursor);
12132 enum i915_pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12134 struct drm_encoder *encoder = connector->base.encoder;
12135 struct drm_device *dev = connector->base.dev;
12137 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
12140 return INVALID_PIPE;
12142 return to_intel_crtc(encoder->crtc)->pipe;
12145 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
12146 struct drm_file *file)
12148 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
12149 struct drm_crtc *drmmode_crtc;
12150 struct intel_crtc *crtc;
12152 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12155 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
12157 if (!drmmode_crtc) {
12158 DRM_ERROR("no such CRTC id\n");
12162 crtc = to_intel_crtc(drmmode_crtc);
12163 pipe_from_crtc_id->pipe = crtc->pipe;
12168 static int intel_encoder_clones(struct intel_encoder *encoder)
12170 struct drm_device *dev = encoder->base.dev;
12171 struct intel_encoder *source_encoder;
12172 int index_mask = 0;
12175 for_each_intel_encoder(dev, source_encoder) {
12176 if (encoders_cloneable(encoder, source_encoder))
12177 index_mask |= (1 << entry);
12185 static bool has_edp_a(struct drm_device *dev)
12187 struct drm_i915_private *dev_priv = dev->dev_private;
12189 if (!IS_MOBILE(dev))
12192 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12195 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
12201 const char *intel_output_name(int output)
12203 static const char *names[] = {
12204 [INTEL_OUTPUT_UNUSED] = "Unused",
12205 [INTEL_OUTPUT_ANALOG] = "Analog",
12206 [INTEL_OUTPUT_DVO] = "DVO",
12207 [INTEL_OUTPUT_SDVO] = "SDVO",
12208 [INTEL_OUTPUT_LVDS] = "LVDS",
12209 [INTEL_OUTPUT_TVOUT] = "TV",
12210 [INTEL_OUTPUT_HDMI] = "HDMI",
12211 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12212 [INTEL_OUTPUT_EDP] = "eDP",
12213 [INTEL_OUTPUT_DSI] = "DSI",
12214 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12217 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12220 return names[output];
12223 static bool intel_crt_present(struct drm_device *dev)
12225 struct drm_i915_private *dev_priv = dev->dev_private;
12230 if (IS_CHERRYVIEW(dev))
12233 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12239 static void intel_setup_outputs(struct drm_device *dev)
12241 struct drm_i915_private *dev_priv = dev->dev_private;
12242 struct intel_encoder *encoder;
12243 bool dpd_is_edp = false;
12245 intel_lvds_init(dev);
12247 if (intel_crt_present(dev))
12248 intel_crt_init(dev);
12250 if (HAS_DDI(dev)) {
12253 /* Haswell uses DDI functions to detect digital outputs */
12254 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12255 /* DDI A only supports eDP */
12257 intel_ddi_init(dev, PORT_A);
12259 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12261 found = I915_READ(SFUSE_STRAP);
12263 if (found & SFUSE_STRAP_DDIB_DETECTED)
12264 intel_ddi_init(dev, PORT_B);
12265 if (found & SFUSE_STRAP_DDIC_DETECTED)
12266 intel_ddi_init(dev, PORT_C);
12267 if (found & SFUSE_STRAP_DDID_DETECTED)
12268 intel_ddi_init(dev, PORT_D);
12269 } else if (HAS_PCH_SPLIT(dev)) {
12271 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
12273 if (has_edp_a(dev))
12274 intel_dp_init(dev, DP_A, PORT_A);
12276 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
12277 /* PCH SDVOB multiplex with HDMIB */
12278 found = intel_sdvo_init(dev, PCH_SDVOB, true);
12280 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
12281 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
12282 intel_dp_init(dev, PCH_DP_B, PORT_B);
12285 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
12286 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
12288 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
12289 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
12291 if (I915_READ(PCH_DP_C) & DP_DETECTED)
12292 intel_dp_init(dev, PCH_DP_C, PORT_C);
12294 if (I915_READ(PCH_DP_D) & DP_DETECTED)
12295 intel_dp_init(dev, PCH_DP_D, PORT_D);
12296 } else if (IS_VALLEYVIEW(dev)) {
12298 * The DP_DETECTED bit is the latched state of the DDC
12299 * SDA pin at boot. However since eDP doesn't require DDC
12300 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12301 * eDP ports may have been muxed to an alternate function.
12302 * Thus we can't rely on the DP_DETECTED bit alone to detect
12303 * eDP ports. Consult the VBT as well as DP_DETECTED to
12304 * detect eDP ports.
12306 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
12307 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12309 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12310 intel_dp_is_edp(dev, PORT_B))
12311 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12313 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
12314 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12316 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12317 intel_dp_is_edp(dev, PORT_C))
12318 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
12320 if (IS_CHERRYVIEW(dev)) {
12321 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
12322 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12324 /* eDP not supported on port D, so don't check VBT */
12325 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12326 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12329 intel_dsi_init(dev);
12330 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
12331 bool found = false;
12333 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
12334 DRM_DEBUG_KMS("probing SDVOB\n");
12335 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
12336 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12337 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
12338 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
12341 if (!found && SUPPORTS_INTEGRATED_DP(dev))
12342 intel_dp_init(dev, DP_B, PORT_B);
12345 /* Before G4X SDVOC doesn't have its own detect register */
12347 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
12348 DRM_DEBUG_KMS("probing SDVOC\n");
12349 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
12352 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
12354 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12355 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
12356 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
12358 if (SUPPORTS_INTEGRATED_DP(dev))
12359 intel_dp_init(dev, DP_C, PORT_C);
12362 if (SUPPORTS_INTEGRATED_DP(dev) &&
12363 (I915_READ(DP_D) & DP_DETECTED))
12364 intel_dp_init(dev, DP_D, PORT_D);
12366 } else if (IS_GEN2(dev))
12367 intel_dvo_init(dev);
12371 if (SUPPORTS_TV(dev))
12372 intel_tv_init(dev);
12374 intel_edp_psr_init(dev);
12376 for_each_intel_encoder(dev, encoder) {
12377 encoder->base.possible_crtcs = encoder->crtc_mask;
12378 encoder->base.possible_clones =
12379 intel_encoder_clones(encoder);
12382 intel_init_pch_refclk(dev);
12384 drm_helper_move_panel_connectors_to_head(dev);
12387 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12389 struct drm_device *dev = fb->dev;
12390 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12392 drm_framebuffer_cleanup(fb);
12393 mutex_lock(&dev->struct_mutex);
12394 WARN_ON(!intel_fb->obj->framebuffer_references--);
12395 drm_gem_object_unreference(&intel_fb->obj->base);
12396 mutex_unlock(&dev->struct_mutex);
12400 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
12401 struct drm_file *file,
12402 unsigned int *handle)
12404 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12405 struct drm_i915_gem_object *obj = intel_fb->obj;
12407 return drm_gem_handle_create(file, &obj->base, handle);
12410 static const struct drm_framebuffer_funcs intel_fb_funcs = {
12411 .destroy = intel_user_framebuffer_destroy,
12412 .create_handle = intel_user_framebuffer_create_handle,
12415 static int intel_framebuffer_init(struct drm_device *dev,
12416 struct intel_framebuffer *intel_fb,
12417 struct drm_mode_fb_cmd2 *mode_cmd,
12418 struct drm_i915_gem_object *obj)
12420 int aligned_height;
12424 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12426 if (obj->tiling_mode == I915_TILING_Y) {
12427 DRM_DEBUG("hardware does not support tiling Y\n");
12431 if (mode_cmd->pitches[0] & 63) {
12432 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12433 mode_cmd->pitches[0]);
12437 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12438 pitch_limit = 32*1024;
12439 } else if (INTEL_INFO(dev)->gen >= 4) {
12440 if (obj->tiling_mode)
12441 pitch_limit = 16*1024;
12443 pitch_limit = 32*1024;
12444 } else if (INTEL_INFO(dev)->gen >= 3) {
12445 if (obj->tiling_mode)
12446 pitch_limit = 8*1024;
12448 pitch_limit = 16*1024;
12450 /* XXX DSPC is limited to 4k tiled */
12451 pitch_limit = 8*1024;
12453 if (mode_cmd->pitches[0] > pitch_limit) {
12454 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12455 obj->tiling_mode ? "tiled" : "linear",
12456 mode_cmd->pitches[0], pitch_limit);
12460 if (obj->tiling_mode != I915_TILING_NONE &&
12461 mode_cmd->pitches[0] != obj->stride) {
12462 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12463 mode_cmd->pitches[0], obj->stride);
12467 /* Reject formats not supported by any plane early. */
12468 switch (mode_cmd->pixel_format) {
12469 case DRM_FORMAT_C8:
12470 case DRM_FORMAT_RGB565:
12471 case DRM_FORMAT_XRGB8888:
12472 case DRM_FORMAT_ARGB8888:
12474 case DRM_FORMAT_XRGB1555:
12475 case DRM_FORMAT_ARGB1555:
12476 if (INTEL_INFO(dev)->gen > 3) {
12477 DRM_DEBUG("unsupported pixel format: %s\n",
12478 drm_get_format_name(mode_cmd->pixel_format));
12482 case DRM_FORMAT_XBGR8888:
12483 case DRM_FORMAT_ABGR8888:
12484 case DRM_FORMAT_XRGB2101010:
12485 case DRM_FORMAT_ARGB2101010:
12486 case DRM_FORMAT_XBGR2101010:
12487 case DRM_FORMAT_ABGR2101010:
12488 if (INTEL_INFO(dev)->gen < 4) {
12489 DRM_DEBUG("unsupported pixel format: %s\n",
12490 drm_get_format_name(mode_cmd->pixel_format));
12494 case DRM_FORMAT_YUYV:
12495 case DRM_FORMAT_UYVY:
12496 case DRM_FORMAT_YVYU:
12497 case DRM_FORMAT_VYUY:
12498 if (INTEL_INFO(dev)->gen < 5) {
12499 DRM_DEBUG("unsupported pixel format: %s\n",
12500 drm_get_format_name(mode_cmd->pixel_format));
12505 DRM_DEBUG("unsupported pixel format: %s\n",
12506 drm_get_format_name(mode_cmd->pixel_format));
12510 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12511 if (mode_cmd->offsets[0] != 0)
12514 aligned_height = intel_align_height(dev, mode_cmd->height,
12516 /* FIXME drm helper for size checks (especially planar formats)? */
12517 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12520 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12521 intel_fb->obj = obj;
12522 intel_fb->obj->framebuffer_references++;
12524 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12526 DRM_ERROR("framebuffer init failed %d\n", ret);
12533 static struct drm_framebuffer *
12534 intel_user_framebuffer_create(struct drm_device *dev,
12535 struct drm_file *filp,
12536 struct drm_mode_fb_cmd2 *mode_cmd)
12538 struct drm_i915_gem_object *obj;
12540 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12541 mode_cmd->handles[0]));
12542 if (&obj->base == NULL)
12543 return ERR_PTR(-ENOENT);
12545 return intel_framebuffer_create(dev, mode_cmd, obj);
12548 #ifndef CONFIG_DRM_I915_FBDEV
12549 static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
12554 static const struct drm_mode_config_funcs intel_mode_funcs = {
12555 .fb_create = intel_user_framebuffer_create,
12556 .output_poll_changed = intel_fbdev_output_poll_changed,
12559 /* Set up chip specific display functions */
12560 static void intel_init_display(struct drm_device *dev)
12562 struct drm_i915_private *dev_priv = dev->dev_private;
12564 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12565 dev_priv->display.find_dpll = g4x_find_best_dpll;
12566 else if (IS_CHERRYVIEW(dev))
12567 dev_priv->display.find_dpll = chv_find_best_dpll;
12568 else if (IS_VALLEYVIEW(dev))
12569 dev_priv->display.find_dpll = vlv_find_best_dpll;
12570 else if (IS_PINEVIEW(dev))
12571 dev_priv->display.find_dpll = pnv_find_best_dpll;
12573 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12575 if (HAS_DDI(dev)) {
12576 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
12577 dev_priv->display.get_plane_config = ironlake_get_plane_config;
12578 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
12579 dev_priv->display.crtc_enable = haswell_crtc_enable;
12580 dev_priv->display.crtc_disable = haswell_crtc_disable;
12581 dev_priv->display.off = ironlake_crtc_off;
12582 dev_priv->display.update_primary_plane =
12583 ironlake_update_primary_plane;
12584 } else if (HAS_PCH_SPLIT(dev)) {
12585 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
12586 dev_priv->display.get_plane_config = ironlake_get_plane_config;
12587 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
12588 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12589 dev_priv->display.crtc_disable = ironlake_crtc_disable;
12590 dev_priv->display.off = ironlake_crtc_off;
12591 dev_priv->display.update_primary_plane =
12592 ironlake_update_primary_plane;
12593 } else if (IS_VALLEYVIEW(dev)) {
12594 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12595 dev_priv->display.get_plane_config = i9xx_get_plane_config;
12596 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12597 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12598 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12599 dev_priv->display.off = i9xx_crtc_off;
12600 dev_priv->display.update_primary_plane =
12601 i9xx_update_primary_plane;
12603 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12604 dev_priv->display.get_plane_config = i9xx_get_plane_config;
12605 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12606 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12607 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12608 dev_priv->display.off = i9xx_crtc_off;
12609 dev_priv->display.update_primary_plane =
12610 i9xx_update_primary_plane;
12613 /* Returns the core display clock speed */
12614 if (IS_VALLEYVIEW(dev))
12615 dev_priv->display.get_display_clock_speed =
12616 valleyview_get_display_clock_speed;
12617 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
12618 dev_priv->display.get_display_clock_speed =
12619 i945_get_display_clock_speed;
12620 else if (IS_I915G(dev))
12621 dev_priv->display.get_display_clock_speed =
12622 i915_get_display_clock_speed;
12623 else if (IS_I945GM(dev) || IS_845G(dev))
12624 dev_priv->display.get_display_clock_speed =
12625 i9xx_misc_get_display_clock_speed;
12626 else if (IS_PINEVIEW(dev))
12627 dev_priv->display.get_display_clock_speed =
12628 pnv_get_display_clock_speed;
12629 else if (IS_I915GM(dev))
12630 dev_priv->display.get_display_clock_speed =
12631 i915gm_get_display_clock_speed;
12632 else if (IS_I865G(dev))
12633 dev_priv->display.get_display_clock_speed =
12634 i865_get_display_clock_speed;
12635 else if (IS_I85X(dev))
12636 dev_priv->display.get_display_clock_speed =
12637 i855_get_display_clock_speed;
12638 else /* 852, 830 */
12639 dev_priv->display.get_display_clock_speed =
12640 i830_get_display_clock_speed;
12643 dev_priv->display.write_eld = g4x_write_eld;
12644 } else if (IS_GEN5(dev)) {
12645 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12646 dev_priv->display.write_eld = ironlake_write_eld;
12647 } else if (IS_GEN6(dev)) {
12648 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12649 dev_priv->display.write_eld = ironlake_write_eld;
12650 dev_priv->display.modeset_global_resources =
12651 snb_modeset_global_resources;
12652 } else if (IS_IVYBRIDGE(dev)) {
12653 /* FIXME: detect B0+ stepping and use auto training */
12654 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12655 dev_priv->display.write_eld = ironlake_write_eld;
12656 dev_priv->display.modeset_global_resources =
12657 ivb_modeset_global_resources;
12658 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
12659 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12660 dev_priv->display.write_eld = haswell_write_eld;
12661 dev_priv->display.modeset_global_resources =
12662 haswell_modeset_global_resources;
12663 } else if (IS_VALLEYVIEW(dev)) {
12664 dev_priv->display.modeset_global_resources =
12665 valleyview_modeset_global_resources;
12666 dev_priv->display.write_eld = ironlake_write_eld;
12669 /* Default just returns -ENODEV to indicate unsupported */
12670 dev_priv->display.queue_flip = intel_default_queue_flip;
12672 switch (INTEL_INFO(dev)->gen) {
12674 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12678 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12683 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12687 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12690 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
12691 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12695 intel_panel_init_backlight_funcs(dev);
12697 lockinit(&dev_priv->pps_mutex, "i915pm", 0, LK_CANRECURSE);
12701 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12702 * resume, or other times. This quirk makes sure that's the case for
12703 * affected systems.
12705 static void quirk_pipea_force(struct drm_device *dev)
12707 struct drm_i915_private *dev_priv = dev->dev_private;
12709 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
12710 DRM_INFO("applying pipe a force quirk\n");
12713 static void quirk_pipeb_force(struct drm_device *dev)
12715 struct drm_i915_private *dev_priv = dev->dev_private;
12717 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12718 DRM_INFO("applying pipe b force quirk\n");
12722 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12724 static void quirk_ssc_force_disable(struct drm_device *dev)
12726 struct drm_i915_private *dev_priv = dev->dev_private;
12727 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
12728 DRM_INFO("applying lvds SSC disable quirk\n");
12732 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12735 static void quirk_invert_brightness(struct drm_device *dev)
12737 struct drm_i915_private *dev_priv = dev->dev_private;
12738 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
12739 DRM_INFO("applying inverted panel brightness quirk\n");
12742 /* Some VBT's incorrectly indicate no backlight is present */
12743 static void quirk_backlight_present(struct drm_device *dev)
12745 struct drm_i915_private *dev_priv = dev->dev_private;
12746 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12747 DRM_INFO("applying backlight present quirk\n");
12750 struct intel_quirk {
12752 int subsystem_vendor;
12753 int subsystem_device;
12754 void (*hook)(struct drm_device *dev);
12757 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12758 struct intel_dmi_quirk {
12759 void (*hook)(struct drm_device *dev);
12760 const struct dmi_system_id (*dmi_id_list)[];
12763 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12765 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12769 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12771 .dmi_id_list = &(const struct dmi_system_id[]) {
12773 .callback = intel_dmi_reverse_brightness,
12774 .ident = "NCR Corporation",
12775 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12776 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12779 { } /* terminating entry */
12781 .hook = quirk_invert_brightness,
12785 static struct intel_quirk intel_quirks[] = {
12786 /* HP Mini needs pipe A force quirk (LP: #322104) */
12787 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
12789 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12790 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12792 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12793 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12795 /* 830 needs to leave pipe A & dpll A up */
12796 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12798 /* 830 needs to leave pipe B & dpll B up */
12799 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12801 /* Lenovo U160 cannot use SSC on LVDS */
12802 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
12804 /* Sony Vaio Y cannot use SSC on LVDS */
12805 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
12807 /* Acer Aspire 5734Z must invert backlight brightness */
12808 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12810 /* Acer/eMachines G725 */
12811 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12813 /* Acer/eMachines e725 */
12814 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12816 /* Acer/Packard Bell NCL20 */
12817 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12819 /* Acer Aspire 4736Z */
12820 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
12822 /* Acer Aspire 5336 */
12823 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
12825 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12826 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
12828 /* Acer C720 Chromebook (Core i3 4005U) */
12829 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12831 /* Apple Macbook 2,1 (Core 2 T7400) */
12832 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
12834 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12835 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
12837 /* HP Chromebook 14 (Celeron 2955U) */
12838 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
12841 static void intel_init_quirks(struct drm_device *dev)
12843 struct device *d = dev->dev;
12846 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12847 struct intel_quirk *q = &intel_quirks[i];
12849 if (pci_get_device(d) == q->device &&
12850 (pci_get_subvendor(d) == q->subsystem_vendor ||
12851 q->subsystem_vendor == PCI_ANY_ID) &&
12852 (pci_get_subdevice(d) == q->subsystem_device ||
12853 q->subsystem_device == PCI_ANY_ID))
12856 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12857 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12858 intel_dmi_quirks[i].hook(dev);
12862 /* Disable the VGA plane that we never use */
12863 static void i915_disable_vga(struct drm_device *dev)
12865 struct drm_i915_private *dev_priv = dev->dev_private;
12867 u32 vga_reg = i915_vgacntrl_reg(dev);
12869 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
12871 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
12873 outb(VGA_SR_INDEX, SR01);
12874 sr1 = inb(VGA_SR_DATA);
12875 outb(VGA_SR_DATA, sr1 | 1 << 5);
12877 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12882 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12883 * from S3 without preserving (some of?) the other bits.
12885 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
12886 POSTING_READ(vga_reg);
12889 void intel_modeset_init_hw(struct drm_device *dev)
12891 intel_prepare_ddi(dev);
12893 if (IS_VALLEYVIEW(dev))
12894 vlv_update_cdclk(dev);
12896 intel_init_clock_gating(dev);
12898 intel_enable_gt_powersave(dev);
12901 void intel_modeset_suspend_hw(struct drm_device *dev)
12903 intel_suspend_hw(dev);
12906 void intel_modeset_init(struct drm_device *dev)
12908 struct drm_i915_private *dev_priv = dev->dev_private;
12910 enum i915_pipe pipe;
12911 struct intel_crtc *crtc;
12913 drm_mode_config_init(dev);
12915 dev->mode_config.min_width = 0;
12916 dev->mode_config.min_height = 0;
12918 dev->mode_config.preferred_depth = 24;
12919 dev->mode_config.prefer_shadow = 1;
12921 dev->mode_config.funcs = &intel_mode_funcs;
12923 intel_init_quirks(dev);
12925 intel_init_pm(dev);
12927 if (INTEL_INFO(dev)->num_pipes == 0)
12930 intel_init_display(dev);
12932 if (IS_GEN2(dev)) {
12933 dev->mode_config.max_width = 2048;
12934 dev->mode_config.max_height = 2048;
12935 } else if (IS_GEN3(dev)) {
12936 dev->mode_config.max_width = 4096;
12937 dev->mode_config.max_height = 4096;
12939 dev->mode_config.max_width = 8192;
12940 dev->mode_config.max_height = 8192;
12943 if (IS_845G(dev) || IS_I865G(dev)) {
12944 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12945 dev->mode_config.cursor_height = 1023;
12946 } else if (IS_GEN2(dev)) {
12947 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12948 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12950 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12951 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12954 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
12956 DRM_DEBUG_KMS("%d display pipe%s available.\n",
12957 INTEL_INFO(dev)->num_pipes,
12958 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
12960 for_each_pipe(dev_priv, pipe) {
12961 intel_crtc_init(dev, pipe);
12962 for_each_sprite(pipe, sprite) {
12963 ret = intel_plane_init(dev, pipe, sprite);
12965 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
12966 pipe_name(pipe), sprite_name(pipe, sprite), ret);
12970 intel_init_dpio(dev);
12972 intel_shared_dpll_init(dev);
12974 /* save the BIOS value before clobbering it */
12975 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
12976 /* Just disable it once at startup */
12977 i915_disable_vga(dev);
12978 intel_setup_outputs(dev);
12980 /* Just in case the BIOS is doing something questionable. */
12981 intel_disable_fbc(dev);
12983 drm_modeset_lock_all(dev);
12984 intel_modeset_setup_hw_state(dev, false);
12985 drm_modeset_unlock_all(dev);
12987 for_each_intel_crtc(dev, crtc) {
12992 * Note that reserving the BIOS fb up front prevents us
12993 * from stuffing other stolen allocations like the ring
12994 * on top. This prevents some ugliness at boot time, and
12995 * can even allow for smooth boot transitions if the BIOS
12996 * fb is large enough for the active pipe configuration.
12998 if (dev_priv->display.get_plane_config) {
12999 dev_priv->display.get_plane_config(crtc,
13000 &crtc->plane_config);
13002 * If the fb is shared between multiple heads, we'll
13003 * just get the first one.
13005 intel_find_plane_obj(crtc, &crtc->plane_config);
13010 static void intel_enable_pipe_a(struct drm_device *dev)
13012 struct intel_connector *connector;
13013 struct drm_connector *crt = NULL;
13014 struct intel_load_detect_pipe load_detect_temp;
13015 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
13017 /* We can't just switch on the pipe A, we need to set things up with a
13018 * proper mode and output configuration. As a gross hack, enable pipe A
13019 * by enabling the load detect pipe once. */
13020 list_for_each_entry(connector,
13021 &dev->mode_config.connector_list,
13023 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13024 crt = &connector->base;
13032 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13033 intel_release_load_detect_pipe(crt, &load_detect_temp);
13037 intel_check_plane_mapping(struct intel_crtc *crtc)
13039 struct drm_device *dev = crtc->base.dev;
13040 struct drm_i915_private *dev_priv = dev->dev_private;
13043 if (INTEL_INFO(dev)->num_pipes == 1)
13046 reg = DSPCNTR(!crtc->plane);
13047 val = I915_READ(reg);
13049 if ((val & DISPLAY_PLANE_ENABLE) &&
13050 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13056 static void intel_sanitize_crtc(struct intel_crtc *crtc)
13058 struct drm_device *dev = crtc->base.dev;
13059 struct drm_i915_private *dev_priv = dev->dev_private;
13062 /* Clear any frame start delays used for debugging left by the BIOS */
13063 reg = PIPECONF(crtc->config.cpu_transcoder);
13064 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13066 /* restore vblank interrupts to correct state */
13067 if (crtc->active) {
13068 update_scanline_offset(crtc);
13069 drm_vblank_on(dev, crtc->pipe);
13071 drm_vblank_off(dev, crtc->pipe);
13073 /* We need to sanitize the plane -> pipe mapping first because this will
13074 * disable the crtc (and hence change the state) if it is wrong. Note
13075 * that gen4+ has a fixed plane -> pipe mapping. */
13076 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
13077 struct intel_connector *connector;
13080 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13081 crtc->base.base.id);
13083 /* Pipe has the wrong plane attached and the plane is active.
13084 * Temporarily change the plane mapping and disable everything
13086 plane = crtc->plane;
13087 crtc->plane = !plane;
13088 crtc->primary_enabled = true;
13089 dev_priv->display.crtc_disable(&crtc->base);
13090 crtc->plane = plane;
13092 /* ... and break all links. */
13093 list_for_each_entry(connector, &dev->mode_config.connector_list,
13095 if (connector->encoder->base.crtc != &crtc->base)
13098 connector->base.dpms = DRM_MODE_DPMS_OFF;
13099 connector->base.encoder = NULL;
13101 /* multiple connectors may have the same encoder:
13102 * handle them and break crtc link separately */
13103 list_for_each_entry(connector, &dev->mode_config.connector_list,
13105 if (connector->encoder->base.crtc == &crtc->base) {
13106 connector->encoder->base.crtc = NULL;
13107 connector->encoder->connectors_active = false;
13110 WARN_ON(crtc->active);
13111 crtc->base.enabled = false;
13114 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13115 crtc->pipe == PIPE_A && !crtc->active) {
13116 /* BIOS forgot to enable pipe A, this mostly happens after
13117 * resume. Force-enable the pipe to fix this, the update_dpms
13118 * call below we restore the pipe to the right state, but leave
13119 * the required bits on. */
13120 intel_enable_pipe_a(dev);
13123 /* Adjust the state of the output pipe according to whether we
13124 * have active connectors/encoders. */
13125 intel_crtc_update_dpms(&crtc->base);
13127 if (crtc->active != crtc->base.enabled) {
13128 struct intel_encoder *encoder;
13130 /* This can happen either due to bugs in the get_hw_state
13131 * functions or because the pipe is force-enabled due to the
13133 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13134 crtc->base.base.id,
13135 crtc->base.enabled ? "enabled" : "disabled",
13136 crtc->active ? "enabled" : "disabled");
13138 crtc->base.enabled = crtc->active;
13140 /* Because we only establish the connector -> encoder ->
13141 * crtc links if something is active, this means the
13142 * crtc is now deactivated. Break the links. connector
13143 * -> encoder links are only establish when things are
13144 * actually up, hence no need to break them. */
13145 WARN_ON(crtc->active);
13147 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13148 WARN_ON(encoder->connectors_active);
13149 encoder->base.crtc = NULL;
13153 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
13155 * We start out with underrun reporting disabled to avoid races.
13156 * For correct bookkeeping mark this on active crtcs.
13158 * Also on gmch platforms we dont have any hardware bits to
13159 * disable the underrun reporting. Which means we need to start
13160 * out with underrun reporting disabled also on inactive pipes,
13161 * since otherwise we'll complain about the garbage we read when
13162 * e.g. coming up after runtime pm.
13164 * No protection against concurrent access is required - at
13165 * worst a fifo underrun happens which also sets this to false.
13167 crtc->cpu_fifo_underrun_disabled = true;
13168 crtc->pch_fifo_underrun_disabled = true;
13172 static void intel_sanitize_encoder(struct intel_encoder *encoder)
13174 struct intel_connector *connector;
13175 struct drm_device *dev = encoder->base.dev;
13177 /* We need to check both for a crtc link (meaning that the
13178 * encoder is active and trying to read from a pipe) and the
13179 * pipe itself being active. */
13180 bool has_active_crtc = encoder->base.crtc &&
13181 to_intel_crtc(encoder->base.crtc)->active;
13183 if (encoder->connectors_active && !has_active_crtc) {
13184 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13185 encoder->base.base.id,
13186 encoder->base.name);
13188 /* Connector is active, but has no active pipe. This is
13189 * fallout from our resume register restoring. Disable
13190 * the encoder manually again. */
13191 if (encoder->base.crtc) {
13192 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13193 encoder->base.base.id,
13194 encoder->base.name);
13195 encoder->disable(encoder);
13196 if (encoder->post_disable)
13197 encoder->post_disable(encoder);
13199 encoder->base.crtc = NULL;
13200 encoder->connectors_active = false;
13202 /* Inconsistent output/port/pipe state happens presumably due to
13203 * a bug in one of the get_hw_state functions. Or someplace else
13204 * in our code, like the register restore mess on resume. Clamp
13205 * things to off as a safer default. */
13206 list_for_each_entry(connector,
13207 &dev->mode_config.connector_list,
13209 if (connector->encoder != encoder)
13212 connector->base.dpms = DRM_MODE_DPMS_OFF;
13213 connector->base.encoder = NULL;
13216 /* Enabled encoders without active connectors will be fixed in
13217 * the crtc fixup. */
13220 void i915_redisable_vga_power_on(struct drm_device *dev)
13222 struct drm_i915_private *dev_priv = dev->dev_private;
13223 u32 vga_reg = i915_vgacntrl_reg(dev);
13225 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13226 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13227 i915_disable_vga(dev);
13231 void i915_redisable_vga(struct drm_device *dev)
13233 struct drm_i915_private *dev_priv = dev->dev_private;
13235 /* This function can be called both from intel_modeset_setup_hw_state or
13236 * at a very early point in our resume sequence, where the power well
13237 * structures are not yet restored. Since this function is at a very
13238 * paranoid "someone might have enabled VGA while we were not looking"
13239 * level, just check if the power well is enabled instead of trying to
13240 * follow the "don't touch the power well if we don't need it" policy
13241 * the rest of the driver uses. */
13242 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
13245 i915_redisable_vga_power_on(dev);
13248 static bool primary_get_hw_state(struct intel_crtc *crtc)
13250 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13255 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13258 static void intel_modeset_readout_hw_state(struct drm_device *dev)
13260 struct drm_i915_private *dev_priv = dev->dev_private;
13261 enum i915_pipe pipe;
13262 struct intel_crtc *crtc;
13263 struct intel_encoder *encoder;
13264 struct intel_connector *connector;
13267 for_each_intel_crtc(dev, crtc) {
13268 memset(&crtc->config, 0, sizeof(crtc->config));
13270 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13272 crtc->active = dev_priv->display.get_pipe_config(crtc,
13275 crtc->base.enabled = crtc->active;
13276 crtc->primary_enabled = primary_get_hw_state(crtc);
13278 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13279 crtc->base.base.id,
13280 crtc->active ? "enabled" : "disabled");
13283 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13284 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13286 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13288 for_each_intel_crtc(dev, crtc) {
13289 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13292 pll->refcount = pll->active;
13294 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13295 pll->name, pll->refcount, pll->on);
13298 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
13301 for_each_intel_encoder(dev, encoder) {
13304 if (encoder->get_hw_state(encoder, &pipe)) {
13305 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13306 encoder->base.crtc = &crtc->base;
13307 encoder->get_config(encoder, &crtc->config);
13309 encoder->base.crtc = NULL;
13312 encoder->connectors_active = false;
13313 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
13314 encoder->base.base.id,
13315 encoder->base.name,
13316 encoder->base.crtc ? "enabled" : "disabled",
13320 list_for_each_entry(connector, &dev->mode_config.connector_list,
13322 if (connector->get_hw_state(connector)) {
13323 connector->base.dpms = DRM_MODE_DPMS_ON;
13324 connector->encoder->connectors_active = true;
13325 connector->base.encoder = &connector->encoder->base;
13327 connector->base.dpms = DRM_MODE_DPMS_OFF;
13328 connector->base.encoder = NULL;
13330 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13331 connector->base.base.id,
13332 connector->base.name,
13333 connector->base.encoder ? "enabled" : "disabled");
13337 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13338 * and i915 state tracking structures. */
13339 void intel_modeset_setup_hw_state(struct drm_device *dev,
13340 bool force_restore)
13342 struct drm_i915_private *dev_priv = dev->dev_private;
13343 enum i915_pipe pipe;
13344 struct intel_crtc *crtc;
13345 struct intel_encoder *encoder;
13348 intel_modeset_readout_hw_state(dev);
13351 * Now that we have the config, copy it to each CRTC struct
13352 * Note that this could go away if we move to using crtc_config
13353 * checking everywhere.
13355 for_each_intel_crtc(dev, crtc) {
13356 if (crtc->active && i915.fastboot) {
13357 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
13358 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13359 crtc->base.base.id);
13360 drm_mode_debug_printmodeline(&crtc->base.mode);
13364 /* HW state is read out, now we need to sanitize this mess. */
13365 for_each_intel_encoder(dev, encoder) {
13366 intel_sanitize_encoder(encoder);
13369 for_each_pipe(dev_priv, pipe) {
13370 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13371 intel_sanitize_crtc(crtc);
13372 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
13375 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13376 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13378 if (!pll->on || pll->active)
13381 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13383 pll->disable(dev_priv, pll);
13387 if (HAS_PCH_SPLIT(dev))
13388 ilk_wm_get_hw_state(dev);
13390 if (force_restore) {
13391 i915_redisable_vga(dev);
13394 * We need to use raw interfaces for restoring state to avoid
13395 * checking (bogus) intermediate states.
13397 for_each_pipe(dev_priv, pipe) {
13398 struct drm_crtc *crtc =
13399 dev_priv->pipe_to_crtc_mapping[pipe];
13401 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
13402 crtc->primary->fb);
13405 intel_modeset_update_staged_output_state(dev);
13408 intel_modeset_check_state(dev);
13411 void intel_modeset_gem_init(struct drm_device *dev)
13413 struct drm_crtc *c;
13414 struct drm_i915_gem_object *obj;
13416 mutex_lock(&dev->struct_mutex);
13417 intel_init_gt_powersave(dev);
13418 mutex_unlock(&dev->struct_mutex);
13420 intel_modeset_init_hw(dev);
13422 intel_setup_overlay(dev);
13425 * Make sure any fbs we allocated at startup are properly
13426 * pinned & fenced. When we do the allocation it's too early
13429 mutex_lock(&dev->struct_mutex);
13430 for_each_crtc(dev, c) {
13431 obj = intel_fb_obj(c->primary->fb);
13435 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
13436 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13437 to_intel_crtc(c)->pipe);
13438 drm_framebuffer_unreference(c->primary->fb);
13439 c->primary->fb = NULL;
13442 mutex_unlock(&dev->struct_mutex);
13445 void intel_connector_unregister(struct intel_connector *intel_connector)
13447 struct drm_connector *connector = &intel_connector->base;
13449 intel_panel_destroy_backlight(connector);
13450 drm_connector_unregister(connector);
13453 void intel_modeset_cleanup(struct drm_device *dev)
13455 struct drm_i915_private *dev_priv = dev->dev_private;
13456 struct drm_connector *connector;
13459 * Interrupts and polling as the first thing to avoid creating havoc.
13460 * Too much stuff here (turning of rps, connectors, ...) would
13461 * experience fancy races otherwise.
13463 drm_irq_uninstall(dev);
13464 intel_hpd_cancel_work(dev_priv);
13465 dev_priv->pm._irqs_disabled = true;
13468 * Due to the hpd irq storm handling the hotplug work can re-arm the
13469 * poll handlers. Hence disable polling after hpd handling is shut down.
13471 drm_kms_helper_poll_fini(dev);
13473 mutex_lock(&dev->struct_mutex);
13475 intel_unregister_dsm_handler();
13477 intel_disable_fbc(dev);
13479 intel_disable_gt_powersave(dev);
13481 ironlake_teardown_rc6(dev);
13483 mutex_unlock(&dev->struct_mutex);
13485 /* flush any delayed tasks or pending work */
13486 flush_scheduled_work();
13488 /* destroy the backlight and sysfs files before encoders/connectors */
13489 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
13490 struct intel_connector *intel_connector;
13492 intel_connector = to_intel_connector(connector);
13493 intel_connector->unregister(intel_connector);
13496 drm_mode_config_cleanup(dev);
13498 intel_cleanup_overlay(dev);
13500 mutex_lock(&dev->struct_mutex);
13501 intel_cleanup_gt_powersave(dev);
13502 mutex_unlock(&dev->struct_mutex);
13506 * Return which encoder is currently attached for connector.
13508 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
13510 return &intel_attached_encoder(connector)->base;
13513 void intel_connector_attach_encoder(struct intel_connector *connector,
13514 struct intel_encoder *encoder)
13516 connector->encoder = encoder;
13517 drm_mode_connector_attach_encoder(&connector->base,
13522 * set vga decode state - true == enable VGA decode
13524 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13526 struct drm_i915_private *dev_priv = dev->dev_private;
13527 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
13530 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13531 DRM_ERROR("failed to read control word\n");
13535 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13539 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13541 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
13543 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13544 DRM_ERROR("failed to write control word\n");
13552 struct intel_display_error_state {
13554 u32 power_well_driver;
13556 int num_transcoders;
13558 struct intel_cursor_error_state {
13563 } cursor[I915_MAX_PIPES];
13565 struct intel_pipe_error_state {
13566 bool power_domain_on;
13569 } pipe[I915_MAX_PIPES];
13571 struct intel_plane_error_state {
13579 } plane[I915_MAX_PIPES];
13581 struct intel_transcoder_error_state {
13582 bool power_domain_on;
13583 enum transcoder cpu_transcoder;
13596 struct intel_display_error_state *
13597 intel_display_capture_error_state(struct drm_device *dev)
13599 struct drm_i915_private *dev_priv = dev->dev_private;
13600 struct intel_display_error_state *error;
13601 int transcoders[] = {
13609 if (INTEL_INFO(dev)->num_pipes == 0)
13612 error = kzalloc(sizeof(*error), GFP_ATOMIC);
13616 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
13617 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13619 for_each_pipe(dev_priv, i) {
13620 error->pipe[i].power_domain_on =
13621 intel_display_power_enabled_unlocked(dev_priv,
13622 POWER_DOMAIN_PIPE(i));
13623 if (!error->pipe[i].power_domain_on)
13626 error->cursor[i].control = I915_READ(CURCNTR(i));
13627 error->cursor[i].position = I915_READ(CURPOS(i));
13628 error->cursor[i].base = I915_READ(CURBASE(i));
13630 error->plane[i].control = I915_READ(DSPCNTR(i));
13631 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
13632 if (INTEL_INFO(dev)->gen <= 3) {
13633 error->plane[i].size = I915_READ(DSPSIZE(i));
13634 error->plane[i].pos = I915_READ(DSPPOS(i));
13636 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13637 error->plane[i].addr = I915_READ(DSPADDR(i));
13638 if (INTEL_INFO(dev)->gen >= 4) {
13639 error->plane[i].surface = I915_READ(DSPSURF(i));
13640 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13643 error->pipe[i].source = I915_READ(PIPESRC(i));
13645 if (HAS_GMCH_DISPLAY(dev))
13646 error->pipe[i].stat = I915_READ(PIPESTAT(i));
13649 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13650 if (HAS_DDI(dev_priv->dev))
13651 error->num_transcoders++; /* Account for eDP. */
13653 for (i = 0; i < error->num_transcoders; i++) {
13654 enum transcoder cpu_transcoder = transcoders[i];
13656 error->transcoder[i].power_domain_on =
13657 intel_display_power_enabled_unlocked(dev_priv,
13658 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
13659 if (!error->transcoder[i].power_domain_on)
13662 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13664 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13665 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13666 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13667 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13668 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13669 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13670 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
13676 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13679 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
13680 struct drm_device *dev,
13681 struct intel_display_error_state *error)
13683 struct drm_i915_private *dev_priv = dev->dev_private;
13689 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
13690 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
13691 err_printf(m, "PWR_WELL_CTL2: %08x\n",
13692 error->power_well_driver);
13693 for_each_pipe(dev_priv, i) {
13694 err_printf(m, "Pipe [%d]:\n", i);
13695 err_printf(m, " Power: %s\n",
13696 error->pipe[i].power_domain_on ? "on" : "off");
13697 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
13698 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
13700 err_printf(m, "Plane [%d]:\n", i);
13701 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13702 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
13703 if (INTEL_INFO(dev)->gen <= 3) {
13704 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13705 err_printf(m, " POS: %08x\n", error->plane[i].pos);
13707 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13708 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
13709 if (INTEL_INFO(dev)->gen >= 4) {
13710 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13711 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
13714 err_printf(m, "Cursor [%d]:\n", i);
13715 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13716 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13717 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
13720 for (i = 0; i < error->num_transcoders; i++) {
13721 err_printf(m, "CPU transcoder: %c\n",
13722 transcoder_name(error->transcoder[i].cpu_transcoder));
13723 err_printf(m, " Power: %s\n",
13724 error->transcoder[i].power_domain_on ? "on" : "off");
13725 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13726 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13727 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13728 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13729 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13730 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13731 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13736 void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13738 struct intel_crtc *crtc;
13740 for_each_intel_crtc(dev, crtc) {
13741 struct intel_unpin_work *work;
13743 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
13745 work = crtc->unpin_work;
13747 if (work && work->event &&
13748 work->event->base.file_priv == file) {
13749 kfree(work->event);
13750 work->event = NULL;
13753 lockmgr(&dev->event_lock, LK_RELEASE);